for EthernetInterface library compatibility.\\ ** Unoffical fix. may be a problem. **

Dependents:   SNIC-httpclient-example SNIC-ntpclient-example

Fork of SNICInterface by muRata

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Socket.cpp Source File

Socket.cpp

00001 /* Copyright (C) 2012 mbed.org, MIT License
00002  *
00003  * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
00004  * and associated documentation files (the "Software"), to deal in the Software without restriction,
00005  * including without limitation the rights to use, copy, modify, merge, publish, distribute,
00006  * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
00007  * furnished to do so, subject to the following conditions:
00008  *
00009  * The above copyright notice and this permission notice shall be included in all copies or
00010  * substantial portions of the Software.
00011  *
00012  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
00013  * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00014  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
00015  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00016  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00017  */
00018 /* Copyright (C) 2014 Murata Manufacturing Co.,Ltd., MIT License
00019  *  port to the muRata, SWITCH SCIENCE Wi-FI module TypeYD SNIC-UART.
00020  */
00021 #include "Socket.h"
00022 #include <cstring>
00023 
00024 char gSOCKET_SEND_BUF[2048] __attribute__((section("AHBSRAM1")));
00025 
00026 Socket::Socket()
00027 {
00028     mSocketID = -1;
00029 }
00030 
00031 void Socket::set_blocking(bool blocking, unsigned int timeout) {
00032     _blocking = blocking;
00033     _timeout = timeout;
00034 }
00035 
00036 Socket::~Socket() {
00037 //    close(); //Don't want to leak
00038 }
00039 
00040 int Socket::set_option(int level, int optname, const void *optval, socklen_t optlen) {
00041     return 0;
00042 }
00043 
00044 int Socket::get_option(int level, int optname, void *optval, socklen_t *optlen) {
00045     return 0;
00046 }
00047 
00048 int Socket::close(bool shutdown)
00049 {
00050     C_SNIC_Core               *snic_core_p  = C_SNIC_Core::getInstance();
00051     C_SNIC_UartCommandManager *uartCmdMgr_p = snic_core_p->getUartCommand();
00052     
00053     FUNC_IN();
00054     // Get buffer for response payload from MemoryPool
00055     tagMEMPOOL_BLOCK_T *payload_buf = snic_core_p->allocCmdBuf();
00056     if( payload_buf == NULL )
00057     {
00058         DEBUG_PRINT("socket close payload_buf NULL\r\n");
00059         FUNC_OUT();
00060         return -1;
00061     }
00062 
00063     C_SNIC_Core::tagSNIC_CLOSE_SOCKET_REQ_T req;
00064     
00065     // Make request
00066     req.cmd_sid   = UART_CMD_SID_SNIC_CLOSE_SOCKET_REQ;
00067     req.seq       = mUartRequestSeq++;
00068     req.socket_id = mSocketID;
00069 
00070     unsigned char *command_array_p = snic_core_p->getCommandBuf();
00071     unsigned int  command_len;
00072     // Preparation of command
00073     command_len = snic_core_p->preparationSendCommand( UART_CMD_ID_SNIC, req.cmd_sid, (unsigned char *)&req
00074                             , sizeof(C_SNIC_Core::tagSNIC_CLOSE_SOCKET_REQ_T), payload_buf->buf, command_array_p );
00075 
00076     // Send uart command request
00077     snic_core_p->sendUart( command_len, command_array_p );
00078 
00079     int ret;
00080     // Wait UART response
00081     ret = uartCmdMgr_p->wait();
00082     if( ret != 0 )
00083     {
00084         DEBUG_PRINT( "socket close failed\r\n" );
00085         snic_core_p->freeCmdBuf( payload_buf );
00086         FUNC_OUT();
00087         return -1;
00088     }
00089     
00090     if( uartCmdMgr_p->getCommandStatus() != 0 )
00091     {
00092         DEBUG_PRINT("socket close status:%02x\r\n", uartCmdMgr_p->getCommandStatus());
00093         snic_core_p->freeCmdBuf( payload_buf );
00094         FUNC_OUT();
00095         return -1;
00096     }
00097     snic_core_p->freeCmdBuf( payload_buf );
00098     FUNC_OUT();
00099     return 0;
00100 }
00101 
00102 int Socket::createSocket( unsigned char bind, unsigned int local_addr, unsigned short port )
00103 {
00104     C_SNIC_Core               *snic_core_p  = C_SNIC_Core::getInstance();
00105     C_SNIC_UartCommandManager *uartCmdMgr_p = snic_core_p->getUartCommand();
00106     
00107     FUNC_IN();
00108     // Get buffer for response payload from MemoryPool
00109     tagMEMPOOL_BLOCK_T *payload_buf = snic_core_p->allocCmdBuf();
00110     if( payload_buf == NULL )
00111     {
00112         DEBUG_PRINT("createSocket payload_buf NULL\r\n");
00113         FUNC_OUT();
00114         return -1;
00115     }
00116 
00117     C_SNIC_Core::tagSNIC_TCP_CREATE_SOCKET_REQ_T req;
00118     int req_len = 0;
00119     
00120     // Make request
00121     req.cmd_sid  = UART_CMD_SID_SNIC_TCP_CREATE_SOCKET_REQ;
00122     req_len++;
00123     req.seq      = mUartRequestSeq++;
00124     req_len++;
00125     req.bind     = bind;
00126     req_len++;
00127     if( bind != 0 )
00128     {
00129         // set ip addr ( byte order )
00130         C_SNIC_UartMsgUtil::convertIntToByteAdday( local_addr, (char *)req.local_addr );
00131         req.local_port[0] = ( (port & 0xFF00) >> 8 );
00132         req.local_port[1] = (port & 0xFF);
00133 
00134         req_len = sizeof(C_SNIC_Core::tagSNIC_TCP_CREATE_SOCKET_REQ_T);
00135     }
00136 
00137     unsigned char *command_array_p = snic_core_p->getCommandBuf();
00138     unsigned int  command_len;
00139     // Preparation of command
00140     command_len = snic_core_p->preparationSendCommand( UART_CMD_ID_SNIC, req.cmd_sid, (unsigned char *)&req
00141                             , req_len, payload_buf->buf, command_array_p );
00142     // Send uart command request
00143     snic_core_p->sendUart( command_len, command_array_p );
00144 
00145     int ret;
00146     // Wait UART response
00147     ret = uartCmdMgr_p->wait();
00148     if( ret != 0 )
00149     {
00150         DEBUG_PRINT( "createSocket failed\r\n" );
00151         snic_core_p->freeCmdBuf( payload_buf );
00152         FUNC_OUT();
00153         return -1;
00154     }
00155 
00156     if( uartCmdMgr_p->getCommandStatus() != 0 )
00157     {
00158         DEBUG_PRINT("createSocket status:%02x\r\n", uartCmdMgr_p->getCommandStatus());
00159         snic_core_p->freeCmdBuf( payload_buf );
00160         FUNC_OUT();
00161         return -1;
00162     }
00163     mSocketID = payload_buf->buf[3];
00164     snic_core_p->freeCmdBuf( payload_buf );
00165     FUNC_OUT();
00166     return 0;
00167 }
00168 
00169 int Socket::resolveHostName( const char *host_p )
00170 {
00171     C_SNIC_Core               *snic_core_p  = C_SNIC_Core::getInstance();
00172     C_SNIC_UartCommandManager *uartCmdMgr_p = snic_core_p->getUartCommand();
00173     int ip_addr = 0;
00174 
00175     if( host_p == NULL )
00176     {
00177         DEBUG_PRINT("resolveHostName parameter error\r\n");
00178         return -1;
00179     }
00180     
00181     // Get buffer for response payload from MemoryPool
00182     tagMEMPOOL_BLOCK_T *payload_buf = snic_core_p->allocCmdBuf();
00183     if( payload_buf == NULL )
00184     {
00185         DEBUG_PRINT("resolveHostName payload_buf NULL\r\n");
00186         return -1;
00187     }
00188     
00189     unsigned char *buf_p = (unsigned char *)getSocketSendBuf();
00190     unsigned int  buf_len = 0;
00191 
00192     memset( buf_p, 0, UART_REQUEST_PAYLOAD_MAX );
00193     // Make request
00194     buf_p[0] = UART_CMD_SID_SNIC_RESOLVE_NAME_REQ;
00195     buf_len++;
00196     buf_p[1] = mUartRequestSeq++;
00197     buf_len++;
00198     // Interface 
00199     buf_p[2] = 0;
00200     buf_len++;
00201     
00202     // Host name length
00203     int hostname_len = strlen(host_p);
00204     buf_p[3] = (unsigned char)hostname_len;
00205     buf_len++;
00206     memcpy( &buf_p[4], host_p, hostname_len );
00207     buf_len += hostname_len;
00208     
00209     unsigned char *command_array_p = snic_core_p->getCommandBuf();
00210     unsigned int   command_len;
00211     command_len = snic_core_p->preparationSendCommand( UART_CMD_ID_SNIC, UART_CMD_SID_SNIC_RESOLVE_NAME_REQ, buf_p
00212                         , buf_len, payload_buf->buf, command_array_p );
00213     // Send uart command request
00214     snic_core_p->sendUart( command_len, command_array_p );
00215     
00216     int ret;
00217     // Wait UART response
00218     ret = uartCmdMgr_p->wait();
00219     if( ret != 0 )
00220     {
00221         DEBUG_PRINT( "resolveHostName failed\r\n" );
00222         snic_core_p->freeCmdBuf( payload_buf );
00223         return -1;
00224     }
00225     
00226     // check status
00227     if( uartCmdMgr_p->getCommandStatus() == 0 )
00228     {
00229         ip_addr = ((payload_buf->buf[3] << 24) & 0xFF000000)
00230                 | ((payload_buf->buf[4] << 16) & 0xFF0000)
00231                 | ((payload_buf->buf[5] << 8)  & 0xFF00)
00232                 | (payload_buf->buf[6]);
00233     }
00234 
00235     snic_core_p->freeCmdBuf( payload_buf );
00236     return ip_addr;
00237 }
00238 
00239 char *Socket::getSocketSendBuf()
00240 {
00241     return gSOCKET_SEND_BUF;
00242 }