pcsl_network.c
来自「This is a resource based on j2me embedde」· C语言 代码 · 共 928 行 · 第 1/2 页
C
928 行
* See pcsl_network.h for definition. */ int pcsl_socket_setsockopt( void *handle, int flag, int optval){ //Dummy stub return PCSL_NET_IOERROR;}/** * See pcsl_network.h for definition. */ int pcsl_socket_getlocaladdr( void *handle, char *pAddress){ //Dummy stub return PCSL_NET_IOERROR;}/** * See pcsl_network.h for definition. */ int pcsl_socket_getremoteaddr( void *handle, char *pAddress){ //Dummy stub return PCSL_NET_IOERROR;}/** * See pcsl_network.h for definition. */ int pcsl_socket_getlocalport( void *handle, int *pPortNumber){ //Dummy stub return PCSL_NET_IOERROR;}/** * See pcsl_network.h for definition. */ int pcsl_socket_getremoteport( void *handle, int *pPortNumber){ //Dummy stub return PCSL_NET_IOERROR;}/** * See pcsl_network.h for definition. */ int pcsl_network_init(void){ return pcsl_network_init_start(NULL);}/** * See pcsl_network.h for definition. */ int pcsl_network_init_start(PCSL_NET_CALLBACK pcsl_network_callback) { int result; (void)pcsl_network_callback; /* Initially open the linux serial port */ if (portfd == -1) { result = pcsl_serial_open(0); if (result == -1 ) { return PCSL_NET_IOERROR; } else { portfd = result; } } printf("Serial port fd = %d\n", portfd); return PCSL_NET_SUCCESS;} /** * See pcsl_network.h for definition. */int pcsl_network_init_finish(void) { return PCSL_NET_SUCCESS;}/** * See pcsl_network.h for definition. */int pcsl_network_finalize_start(PCSL_NET_CALLBACK pcsl_network_callback) { (void)pcsl_network_callback; return PCSL_NET_SUCCESS;}/** * See pcsl_network.h for definition. */int pcsl_network_finalize_finish(void) { return PCSL_NET_SUCCESS;}/** * See pcsl_network.h for definition. */ int pcsl_network_error(void *handle) { //Value of portfd is > 0 if it is opened successfully, hence it can //be used to represent an error return ((portfd < 0)? portfd : 0);}/** * See pcsl_network.h for definition. */ int pcsl_network_getLocalHostName(char *pLocalHost) { /* Initialize the network */ if (pcsl_network_init_start() != PCSL_NET_SUCCESS) { return PCSL_NET_IOERROR; } if (gethostname(pLocalHost, MAX_HOST_LENGTH) == 0) { return PCSL_NET_IOERROR; } return PCSL_NET_IOERROR;}/** * See pcsl_network.h for definition. */ int pcsl_network_getLocalIPAddressAsString( char *pLocalIPAddress){ struct in_addr addr; char hostname[MAX_HOST_LENGTH]; int len; int status; unsigned char IPAddressBytes[16]; void *dummy; /* hostname = pcsl_socket_getLocalHostName(); addr.s_addr = pcsl_socket_getIpNumber(hostname); return inet_ntoa(addr); */ status = pcsl_network_getLocalHostName(hostname); if (status == PCSL_NET_IOERROR) { return PCSL_NET_IOERROR; } status = pcsl_network_gethostbyname_start( hostname, IPAddressBytes, sizeof(IPAddressBytes), &len, &dummy, &dummy); if (status == PCSL_NET_IOERROR) { return PCSL_NET_IOERROR; } memcpy(&addr.s_addr, IPAddressBytes, sizeof(addr.s_addr)); strcpy(pLocalIPAddress, (char *)inet_ntoa(addr)); return PCSL_NET_SUCCESS;}/** * Receive bytes from the serial port * * @param numbytes Number of characters to be read from the serial port * * @return Array of bytes(characters) of size equal to "numbytes" * */static char* serial_receive(int numbytes) { int bytes_read = 0; int count; char read_buffer[numbytes]; char *p; memset(read_buffer, 0, numbytes); while (bytes_read < numbytes) { count = 0; count = pcsl_serial_readchar(portfd, (read_buffer + bytes_read), (numbytes - bytes_read)); if (count > 0) { bytes_read += count; } } p = (char *)&read_buffer; return p;} /** * Receive four bytes from the serial port * */static char* serial_receive_int() { return serial_receive(4);} /** * Write to serial port * * @param nBuf Integer to be written at the serial port */static void serial_send_int(int nBuf) { char buffer[4]; pcsl_serial_encode_INT(nBuf, buffer); pcsl_serial_writechar(portfd, buffer, sizeof(buffer)); }/** * Copy array of characters in another string. This function is * similar to "strncpy" but it does not depend on null terminated strings * * @param dest Destination string * @param src Source string * @param numbytes No of characters to be copied * */static void pcsl_strncpy(char* dest, char* src, int numbytes) { int i; char *p = dest; for (i=0; i < numbytes; i++) { *p++ = *src++; }}/** * Parse an array of characters at serial port into an integer * * This function may need to be ported according to the * endianness of the host * * @param buffer : An array of characters * @return Integer value of parsed characters * */static int pcsl_serial_parse_INT(unsigned char buffer[]) { int n = 0; int i; for(i = 0; i < 4; i++){ n += ((0xFF & buffer[i]) << (8*(3 - i))); } return n;} /** * Encode an integer into a character array. The first * element in the array represents the most significant byte * * \verbatim 31 24 16 8 0 [ -------- | -------- | --------| -------- ] buf[0] buf[1] buf[2] buf[3] \endverbatim * * @param nBuf : integer number * @param buf : An array of output characters * */static void pcsl_serial_encode_INT(int nBuf, unsigned char* buffer) { int i; for(i = 0; i < 4; i++) { buffer[3 - i] = (unsigned char)((nBuf >> 8*i) & 0xFF); }}/** * Opens a platform specific TCP socket. This function is not a part of * PCSL interface * * @param host Host name of the server * @param port number of the port to open * * @return a non-negative platform-specific handle if the function completes * successfully; IO_WOULDBLOCK if the operation would be blocked; otherwise, * any other negative value if there was an error (If there was an error, * exception will also set.) */static int pcsl_socket_open_by_host(char* host, int port) { int handle = -1; char* proxy_char_data; int length; int result; /* Initially open the linux serial port */ if (portfd == -1) { portfd = pcsl_serial_open(0); } printf("Serial port fd = %d\n", portfd); if (portfd == -1) { return -1; } /* send open request to sos server */ serial_send_int(FUNC_OPEN_HOST); /* Send the host address to server */ length = strlen(host); serial_send_int(length); pcsl_serial_writechar(portfd, host, length); /* Send the port-id to server */ serial_send_int(port); /* Receive handle from proxy */ proxy_char_data = (char *)serial_receive_int(); /* Parse the char array into an integer */ result = pcsl_serial_parse_INT(proxy_char_data); if (result == PCSL_NET_IOERROR) { printf("ALERT in pcsl_socket_open_by_host0 : IO exception occurred at proxy\n"); } else if (result == UNKNOWNHOST_EXCEPTION_ERROR) { printf("ALERT in pcsl_socket_open_by_host0 : " " UnknownHost exception occurred at proxy\n"); } else { handle = result; // Increment the count for open connections open_connections++; } printf("socketid = %d\n", handle); return handle;}#ifdef ENABLE_SERVER_SOCKET/** * See pcsl_serversocket.h for definition. */int pcsl_serversocket_open( int port, void **pHandle){ /* need revisit */ return PCSL_NET_IOERROR;} /** * See pcsl_serversocket.h for definition. */int pcsl_serversocket_accept_start( void *handle, void **pConnectionHandle, void **pContext){ /* need revisit */ return PCSL_NET_IOERROR;}/** * See pcsl_serversocket.h for definition. */int pcsl_serversocket_accept_finish( void *handle, void **pConnectionHandle, void **pContext){ /* need revisit */ return PCSL_NET_IOERROR;}/** * See pcsl_serversocket.h for definition. */int pcsl_sereversocket_close_start( void *handle, void **pContext){ return pcsl_socket_close_start(handle, pContext);}/** * See pcsl_serversocket.h for definition. */int pcsl_serversocket_close_finish( void *handle, void *context){ return pcsl_socket_close_finish(handle, context);}#endif /* ifdef ENABLE_SERVER_SOCKET *//** * See pcsl_network.h for definition. */void pcsl_add_network_notifier( void *handle, int event) { /* need revisit */}/** * See pcsl_network.h for definition. */void pcsl_remove_network_notifier( void *handle, int event) { /* need revisit */}/** * See pcsl_network.h for definition. */unsigned int pcsl_network_htonl( unsigned int value) { return htonl(value);}/** * See pcsl_network.h for definition. */unsigned int pcsl_network_ntohl( unsigned int value) { return ntohl(value);} /** * See pcsl_network.h for definition. */unsigned short pcsl_network_htons( unsigned short value) { return htons(value);}/** * See pcsl_network.h for definition. */unsigned short pcsl_network_ntohs( unsigned short value) { return ntohs(value);}/** * See pcsl_network.h for definition. */char * pcsl_inet_ntoa (void *ipBytes) { static char buffer[] = {'\0'}; return buffer;}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?