From c576063923d927d1f8c8d5d3acf1b2eee18297db Mon Sep 17 00:00:00 2001 From: Oskari Timperi Date: Tue, 11 Apr 2017 09:38:28 +0300 Subject: Add TCP/IP support to busdevice.c --- busdevice.c | 168 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 163 insertions(+), 5 deletions(-) (limited to 'busdevice.c') diff --git a/busdevice.c b/busdevice.c index c4f89a1..6a89396 100644 --- a/busdevice.c +++ b/busdevice.c @@ -1,11 +1,17 @@ #include "busdevice.h" #include "pcserialport.h" +#include "tcpclient.h" + #include +#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 @@ -46,6 +52,116 @@ void smBDinit() bdInitialized=smtrue; } +static int validateIpAddress(const char *s, const char **pip_end, + const char **pport_start) +{ + int octets = 0; + int ch = 0, prev = 0; + int len = 0; + const char *ip_end = NULL; + const char *port_start = NULL; + + while (*s) + { + ch = *s; + + if (isdigit(ch)) + { + ++len; + // Octet len must be 1-3 digits + if (len > 3) + { + return -1; + } + } + else if (ch == '.' && isdigit(prev)) + { + ++octets; + len = 0; + // No more than 4 octets please + if (octets > 4) + { + return -1; + } + } + else if (ch == ':' && isdigit(prev)) + { + ++octets; + // We want exactly 4 octets at this point + if (octets != 4) + { + return -1; + } + ip_end = s; + ++s; + port_start = s; + while (isdigit((ch = *s))) + ++s; + // After port we want the end of the string + if (ch != '\0') + return -1; + // This will skip over the ++s below + continue; + } + else + { + return -1; + } + + prev = ch; + ++s; + } + + // We reached the end of the string and did not encounter the port + if (*s == '\0' && ip_end == NULL) + { + ++octets; + ip_end = s; + } + + // Check that there are exactly 4 octets + if (octets != 4) + return -1; + + if (pip_end) + *pip_end = ip_end; + + if (pport_start) + *pport_start = port_start; + + return 0; +} + +static int parseIpAddress(const char *s, char *ip, size_t ipsize, short *port) +{ + const char *ip_end, *port_start; + + if (validateIpAddress(s, &ip_end, &port_start) == -1) + return -1; + + // If ip=NULL, we just report that the parsing was ok + if (!ip) + return 0; + + if (ipsize < ip_end - s + 1) + return -1; + + memcpy(ip, s, ip_end - s); + ip[ip_end - s] = '\0'; + + if (port_start) + { + *port = 0; + while (*port_start) + { + *port = *port * 10 + (*port_start - '0'); + ++port_start; + } + } + + return 0; +} + //ie "COM1" "VSD2USB" //return -1 if fails, otherwise handle number smbusdevicehandle smBDOpen( const char *devicename ) @@ -75,10 +191,24 @@ smbusdevicehandle smBDOpen( const char *devicename ) BusDevice[handle].bdType=BD_RS; BusDevice[handle].txBufferUsed=0; } - else//no other bus types supproted yet - { - return -1; - } + 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; + } + else//no other bus types supproted yet + { + return -1; + } //success BusDevice[handle].cumulativeSmStatus=0; @@ -105,6 +235,12 @@ smbool smBDClose( const smbusdevicehandle handle ) BusDevice[handle].opened=smfalse; return smtrue; } + else if( BusDevice[handle].bdType==BD_TCP ) + { + CloseTCPport( BusDevice[handle].comPort ); + BusDevice[handle].opened=smfalse; + return smtrue; + } return smfalse; } @@ -119,7 +255,7 @@ smbool smBDWrite(const smbusdevicehandle handle, const smuint8 byte ) //check if handle valid & open if( smIsBDHandleOpen(handle)==smfalse ) return smfalse; - if( BusDevice[handle].bdType==BD_RS ) + if( BusDevice[handle].bdType==BD_RS || BusDevice[handle].bdType==BD_TCP ) { if(BusDevice[handle].txBufferUsed Date: Fri, 12 May 2017 16:38:52 +0300 Subject: Add info about valid IP address format --- busdevice.c | 1 + 1 file changed, 1 insertion(+) (limited to 'busdevice.c') diff --git a/busdevice.c b/busdevice.c index 6a89396..b73da3f 100644 --- a/busdevice.c +++ b/busdevice.c @@ -52,6 +52,7 @@ void smBDinit() bdInitialized=smtrue; } +//accepted TCP/IP address format is nnn.nnn.nnn.nnn:pppp where n is IP address numbers and p is port number static int validateIpAddress(const char *s, const char **pip_end, const char **pport_start) { -- cgit v1.2.3