/* hca.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 940721 Created by Henrik Bjorkman 000418 Small cleanup by Henrik 010718 Renamed to hsca. Henrik (www.eit.se) 010719 ported to unix again. Henrik 010721 Both the client and server API merged into one file. Henrik 010721 Support for use in client or server. Henrik 030313 Adapted for C++ also. Ragnar 030504 Improved comments to make code more understandable. Henrik 030508 Support for standard input and serial port. Henrik 050709 Improved comments and renamed some function to make things more clear. Henrik 080921 Improved serial port handling in windows version. Henrik 080923 Added parameter only_localhost. Henrik */ #ifndef HCA_H #define HCA_H #ifdef __cplusplus extern "C" { #endif #ifdef WIN32 #include #include #endif #ifdef WIN32 #define HCA_NO_FD INVALID_SOCKET #else #define HCA_NO_FD -1 #endif /*#ifdef WIN32 extern int hca_serial_needed_delay; #endif*/ /* This function should be called before any of the other hca function are used. */ int hca_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. */ int hca_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. */ int hca_client(const char *hostname, unsigned short port); /* Open a serial port It returns the client socket if successful or a negative value if not. */ #ifdef WIN32 HANDLE hca_open_serial_port(const char *dev_name, int baudrate); #else int hca_open_serial_port(const char *dev_name, int baudrate); #endif /* Open for input from standard input (console) It returns the client socket if successful or a negative value if not. */ int hca_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. */ int hca_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. */ int hca_accept(int server_socket, int only_localhost); /* After "hca_wait" this function can be called to find out on which socket there is new data. parameter handled_socket shall be -1 at first call after wait. After that it shall be the previous socket returned from this function This function is a little tricky, all sockets can be scanned with read instead. You need to loop this until it returns -1. */ /*int hca_get_next_socket_to_read();*/ /* Call this function to read the data received on a socket. */ int hca_read(int socket, char *buf, int len); /* Send data to a specific client */ int hca_write(int socket, const char *buf, int len); #ifdef WIN32 int hca_write_handle(HANDLE h, const char *buf, const int len); int hca_read_handle(HANDLE h, char *buf, const int len); #endif /*#ifndef WIN32 int hca_data_available(int fd); #endif*/ /* Close a connection. */ int hca_close(int socket); #ifdef WIN32 int hca_close_handle(HANDLE h); #endif /* This is supposed to be called before program terminates to clean things up. */ int hca_cleanup(); #ifdef __cplusplus } #endif #endif