SNICInterface for ThingPlug Example
Dependents: ThingPlug_WiFi_Example ThingPlug_WiFi_Example_2
Fork of SNICInterface by
Diff: Socket/Socket.cpp
- Revision:
- 29:6a0ba999597d
- Parent:
- 27:dcc4f34448f0
- Child:
- 31:15c22824cc46
diff -r b796031f6519 -r 6a0ba999597d Socket/Socket.cpp --- a/Socket/Socket.cpp Tue Apr 01 07:19:19 2014 +0000 +++ b/Socket/Socket.cpp Mon May 26 05:17:28 2014 +0000 @@ -30,13 +30,16 @@ #include "Socket.h" #include <cstring> -using namespace murata_wifi; - Socket::Socket() { mSocketID = -1; } +void Socket::set_blocking(bool blocking, unsigned int timeout) { + _blocking = blocking; + _timeout = timeout; +} + Socket::~Socket() { // close(); //Don't want to leak } @@ -54,7 +57,7 @@ C_SNIC_Core *snic_core_p = C_SNIC_Core::getInstance(); C_SNIC_UartCommandManager *uartCmdMgr_p = snic_core_p->getUartCommand(); // Get buffer for response payload from MemoryPool - C_SNIC_Core::tagMEMPOOL_BLOCK_T *payload_buf = snic_core_p->allocCmdBuf(); + tagMEMPOOL_BLOCK_T *payload_buf = snic_core_p->allocCmdBuf(); if( payload_buf == NULL ) { printf("socket close payload_buf NULL\r\n"); @@ -68,7 +71,7 @@ req.seq = mUartRequestSeq++; req.socket_id = mSocketID; - unsigned char command_array[UART_REQUEST_PAYLOAD_MAX]; + unsigned char *command_array = snic_core_p->getCommandBuf(); unsigned int command_len; // Preparation of command command_len = snic_core_p->preparationSendCommand( UART_CMD_ID_SNIC, req.cmd_sid, (unsigned char *)&req @@ -110,7 +113,7 @@ C_SNIC_Core *snic_core_p = C_SNIC_Core::getInstance(); C_SNIC_UartCommandManager *uartCmdMgr_p = snic_core_p->getUartCommand(); // Get buffer for response payload from MemoryPool - C_SNIC_Core::tagMEMPOOL_BLOCK_T *payload_buf = snic_core_p->allocCmdBuf(); + tagMEMPOOL_BLOCK_T *payload_buf = snic_core_p->allocCmdBuf(); if( payload_buf == NULL ) { printf("createSocket payload_buf NULL\r\n"); @@ -137,12 +140,11 @@ */ } - unsigned char command_array[UART_REQUEST_PAYLOAD_MAX]; + unsigned char *command_array = snic_core_p->getCommandBuf(); unsigned int command_len; // Preparation of command command_len = snic_core_p->preparationSendCommand( UART_CMD_ID_SNIC, req.cmd_sid, (unsigned char *)&req , req_len, payload_buf->buf, command_array ); - // Send uart command request snic_core_p->sendUart( command_len, command_array ); @@ -155,7 +157,7 @@ snic_core_p->freeCmdBuf( payload_buf ); return -1; } - + if( uartCmdMgr_p->getCommandStatus() != 0 ) { printf("createSocket status:%02x\r\n", uartCmdMgr_p->getCommandStatus()); @@ -208,3 +210,75 @@ return addr; } + +int Socket::resolveHostName( const char *host_p ) +{ + C_SNIC_Core *snic_core_p = C_SNIC_Core::getInstance(); + C_SNIC_UartCommandManager *uartCmdMgr_p = snic_core_p->getUartCommand(); + int ip_addr = 0; + + if( host_p == NULL ) + { + printf("resolveHostName parameter error\r\n"); + return -1; + } + + // Get buffer for response payload from MemoryPool + tagMEMPOOL_BLOCK_T *payload_buf = snic_core_p->allocCmdBuf(); + if( payload_buf == NULL ) + { + printf("resolveHostName payload_buf NULL\r\n"); + return -1; + } + + unsigned char buf[UART_REQUEST_PAYLOAD_MAX]; + unsigned int buf_len = 0; + + memset( buf, 0, UART_REQUEST_PAYLOAD_MAX ); + // Make request + buf[0] = UART_CMD_SID_SNIC_RESOLVE_NAME_REQ; + buf_len++; + buf[1] = mUartRequestSeq++; + buf_len++; + // Interface + buf[2] = 0; + buf_len++; + + // Host name length + int hostname_len = strlen(host_p); + buf[3] = (unsigned char)hostname_len; + buf_len++; + memcpy( &buf[4], host_p, hostname_len ); + buf_len += hostname_len; + + unsigned char *command_array = snic_core_p->getCommandBuf(); + unsigned int command_len; + command_len = snic_core_p->preparationSendCommand( UART_CMD_ID_SNIC, UART_CMD_SID_SNIC_RESOLVE_NAME_REQ, buf + , buf_len, payload_buf->buf, command_array ); + + // Send uart command request + snic_core_p->sendUart( command_len, command_array ); + + int ret; + // Wait UART response + ret = uartCmdMgr_p->wait(); + if( ret != 0 ) + { + printf( "resolveHostName failed\r\n" ); + snic_core_p->freeCmdBuf( payload_buf ); + return -1; + } + + // check status + if( uartCmdMgr_p->getCommandStatus() == 0 ) + { + ip_addr = ((payload_buf->buf[3] << 24) & 0xFF000000) + | ((payload_buf->buf[4] << 16) & 0xFF0000) + | ((payload_buf->buf[5] << 8) & 0xFF00) + | (payload_buf->buf[6]); + } + + snic_core_p->freeCmdBuf( payload_buf ); + + return ip_addr; +}