#include "busdevice.h" #include "drivers/serial/pcserialport.h" #include "drivers/tcpip/tcpclient.h" #include "drivers/ftdi_d2xx/sm_d2xx.h" #include #include #include #define BD_NONE 0 #define BD_RS 1 #define BD_FTDI 2 #define BD_TCP 3 //how much bytes available in transmit buffer #define TANSMIT_BUFFER_LENGTH 128 unsigned long SMBusBaudrate=SM_BAUDRATE; //the next opened port (with smOpenBus) will be opened with the PBS defined here (default 460800 BPS) typedef struct _SMBusDevice { //common smint8 bdType;//bus device type (such as rs232 or ftdi lib or mcu UART etc). 1=rs232 lib smbool opened; SM_STATUS cumulativeSmStatus; //used for rs232 lib only int comPort; smuint8 txBuffer[TANSMIT_BUFFER_LENGTH]; smint32 txBufferUsed;//how many bytes in buffer currently //used for FTDI lib only } SMBusDevice; //init on first open smbool bdInitialized=smfalse; SMBusDevice BusDevice[SM_MAX_BUSES]; //init device struct table void smBDinit() { int i; for(i=0;i=SM_MAX_BUSES) return -1; if(strncmp(devicename,"COM",3) == 0 || strncmp(devicename,"/dev/tty",8) == 0 || strncmp(devicename,"/dev/cu.",8) == 0) //use rs232 lib { BusDevice[handle].comPort=serialPortOpen( devicename, SMBusBaudrate ); if( BusDevice[handle].comPort == -1 ) { return -1; //failed to open } BusDevice[handle].bdType=BD_RS; BusDevice[handle].txBufferUsed=0; } else if (validateIpAddress(devicename, NULL, NULL) == 0) { char ip[128]; short port = 4001; if (parseIpAddress(devicename, ip, sizeof(ip), &port) < 0) return -1; BusDevice[handle].comPort=OpenTCPPort( ip, port ); if( BusDevice[handle].comPort == -1 ) { return -1; //failed to open } BusDevice[handle].bdType=BD_TCP; BusDevice[handle].txBufferUsed=0; } #ifdef FTDI_D2XX_SUPPORT //try to open FTDI bus by any name: FTDIn (n=index, 0 or greater) or device name (any string programmed in FTDI EEPROM) else { BusDevice[handle].comPort=d2xxPortOpen( devicename, SMBusBaudrate ); if( BusDevice[handle].comPort == -1 ) { return -1; //failed to open } BusDevice[handle].bdType=BD_FTDI; BusDevice[handle].txBufferUsed=0; } #else else//no other bus types supproted yet { smDebug( -1, Low, "smBDOpen device name argument syntax didn't match any supported driver port name"); return -1; } #endif //success BusDevice[handle].cumulativeSmStatus=0; BusDevice[handle].opened=smtrue; return handle; } smbool smIsBDHandleOpen( const smbusdevicehandle handle ) { if(handle<0) return smfalse; if(handle>=SM_MAX_BUSES) return smfalse; return BusDevice[handle].opened; } //return true if ok smbool smBDClose( const smbusdevicehandle handle ) { //check if handle valid & open if( smIsBDHandleOpen(handle)==smfalse ) return smfalse; if( BusDevice[handle].bdType==BD_RS ) { serialPortClose( BusDevice[handle].comPort ); BusDevice[handle].opened=smfalse; return smtrue; } else if( BusDevice[handle].bdType==BD_TCP ) { CloseTCPport( BusDevice[handle].comPort ); BusDevice[handle].opened=smfalse; return smtrue; } #ifdef FTDI_D2XX_SUPPORT else if( BusDevice[handle].bdType==BD_FTDI ) { d2xxPortClose( BusDevice[handle].comPort ); BusDevice[handle].opened=smfalse; return smtrue; } #endif return smfalse; } //write one byte to buffer and send later with smBDTransmit() //returns true on success smbool smBDWrite(const smbusdevicehandle handle, const smuint8 byte ) { //check if handle valid & open if( smIsBDHandleOpen(handle)==smfalse ) return smfalse; if(BusDevice[handle].txBufferUsed