/* hcb.h Henriks Communication Api www.eit.se/hcpips Copyright 2008 Henrik Björkman www.eit.se May be redistributed as under Gnu General Public License version 3. http://www.gnu.org/licenses/licenses.html#GPL You may modify this code as long as you document the modifications you have made in the History section below and do not remove this copyright notice and the file history section. It should work with both Windows and UNIX (such as Solaris & Linux). But the latest version is only tested under Win32. It should work with both Windows and UNIX (such as Solaris & Linux). History 080917 Created by Henrik Bjorkman using code from hca.c */ #ifndef HCB_H #define HCB_H #include #ifdef WIN32 #include #include #endif #define HCB_NO_FD -1 class Hcb { public: /* This function should be called before any of the other hcb function are used. */ static int init(); /* If program shall be a server then call this function to setup the port. It returns the server socket if successful or a negative value if not. */ static int server(unsigned short port); /* If program shall be a client then call this function to connect to the remote host. It returns the client socket if successful or a negative value if not. */ static Hcb* client(const char *hostname, unsigned short port); /* Open a serial port It returns the client socket if successful or a negative value if not. */ static Hcb* open_serial_port(const char *dev_name, int baudrate); /* Open for input from standard input (console) It returns the client socket if successful or a negative value if not. */ static Hcb* open_standard_input(); /* Wait for something to happen Return codes are: <0 Something went wrong 0 Timeout >0 something received from one of the sockets or a new client wants to connect. When this function returns: - use "hca_accept" to check if a new client connected. - use "hca_get_next_socket_to_read" to check from which socket something was received. */ static int wait(int timeout_us); /* If program is a server then this function should be called after "hca_wait" to accept connections from clients if any. It returns the socket if there was a new client or a negative value if not. */ static Hcb* accept(int server_socket, bool only_localhost); /* This is supposed to be called before program terminates to clean things up. */ static void cleanUp(); /* Call this function to read the data received on a socket. */ int read(char *buf, unsigned long len); /* Send data to a specific client */ int write(const char *buf, unsigned long len); /* Close a connection. */ ~Hcb(); private: /*Hcb();*/ Hcb(int fd); #ifdef WIN32 Hcb(HANDLE hSerialPort); #endif Hcb(const Hcb&); class OutFifoPost { public: explicit OutFifoPost(int len); ~OutFifoPost(); // returns number of bytes remaining if ok. // <0 if not ok int write(Hcb *hca) { const int n=hca->write(str+first, len-first); if (n>=0) { first+=n; const int r=len-first; return r; } return n; } char *str; int first; int len; OutFifoPost *next; private: OutFifoPost(const OutFifoPost &) #ifdef WIN32 { ::exit(0); } #else ; #endif }; public: // returns >=0 if ok, <0 if something wrong. int process(); bool isEmpty() const; char *getWritePtr(int len) { OutFifoPost *p=newPostLastInList(len); return p->str; } void writeDone(char *str, int len); bool isReadyForRead() const {return cmdEnd!=NULL;}; const char *getReadPtr() const {return buf;}; int getReadLen() const {return bufIndex;}; void readDone(const char *str); //void close(); bool isConnected() const; void setTransparentMode() {transparentMode=true;}; void setCononicalMode() {transparentMode=false;}; void activateEcho() {echo=true;}; void deactivateEcho() {echo=false;}; private: OutFifoPost *newPostLastInList(int len) { OutFifoPost *p=new OutFifoPost(len); if (isEmpty()) { tail=p; head=p; } else { tail->next=p; tail=p; } return p; }; void deleteFirstPostFromList() { OutFifoPost *p=head; head=p->next; //free(p->str); delete p; }; #ifdef WIN32 HANDLE hSerialPort; #endif int nError; int fd; OutFifoPost *head; OutFifoPost *tail; char buf[1024]; //char *cmdBegin; char *cmdEnd; unsigned int bufIndex; bool transparentMode; bool echo; }; #endif