debug version

Dependents:   HTTPClient_WiFi_HelloWorld_src

Fork of SNICInterface by muRata

Committer:
kishino
Date:
Mon Jun 09 08:28:07 2014 +0000
Revision:
34:8c3527b8f44e
Parent:
33:33f1bc919486
Child:
36:f33fcf5975ab
[Refactoring] ; Changed how to define of buffer for TCP socket.; About setting IP address, changed to using common function for convert to binary array from decimal.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
kishino 20:dd736d328de6 1 /* Copyright (C) 2012 mbed.org, MIT License
kishino 20:dd736d328de6 2 *
kishino 20:dd736d328de6 3 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
kishino 20:dd736d328de6 4 * and associated documentation files (the "Software"), to deal in the Software without restriction,
kishino 20:dd736d328de6 5 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
kishino 20:dd736d328de6 6 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
kishino 20:dd736d328de6 7 * furnished to do so, subject to the following conditions:
kishino 20:dd736d328de6 8 *
kishino 20:dd736d328de6 9 * The above copyright notice and this permission notice shall be included in all copies or
kishino 20:dd736d328de6 10 * substantial portions of the Software.
kishino 20:dd736d328de6 11 *
kishino 20:dd736d328de6 12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
kishino 20:dd736d328de6 13 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
kishino 20:dd736d328de6 14 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
kishino 20:dd736d328de6 15 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
kishino 20:dd736d328de6 16 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
kishino 20:dd736d328de6 17 */
kishino 20:dd736d328de6 18 /******************* Murata Manufacturing Co.,Ltd. 2014 *****************
kishino 20:dd736d328de6 19 *
kishino 20:dd736d328de6 20 * Filename: Socket.cpp
kishino 20:dd736d328de6 21 *
kishino 20:dd736d328de6 22 * Purpose: This module has implementation of socket.
kishino 20:dd736d328de6 23 *
kishino 20:dd736d328de6 24 * $Author: kishino $
kishino 20:dd736d328de6 25 *
kishino 20:dd736d328de6 26 * $Date: 2014/03/26 $
kishino 20:dd736d328de6 27 *
kishino 20:dd736d328de6 28 * $Revision: 0.0.0.1 $
kishino 20:dd736d328de6 29 * ***********************************************************************/
kishino 20:dd736d328de6 30 #include "Socket.h"
kishino 20:dd736d328de6 31 #include <cstring>
kishino 20:dd736d328de6 32
kishino 33:33f1bc919486 33 char gSOCKET_SEND_BUF[2048] __attribute__((section("AHBSRAM1")));
kishino 20:dd736d328de6 34 Socket::Socket()
kishino 20:dd736d328de6 35 {
kishino 20:dd736d328de6 36 mSocketID = -1;
kishino 20:dd736d328de6 37 }
kishino 20:dd736d328de6 38
kishino 29:6a0ba999597d 39 void Socket::set_blocking(bool blocking, unsigned int timeout) {
kishino 29:6a0ba999597d 40 _blocking = blocking;
kishino 29:6a0ba999597d 41 _timeout = timeout;
kishino 29:6a0ba999597d 42 }
kishino 29:6a0ba999597d 43
kishino 20:dd736d328de6 44 Socket::~Socket() {
kishino 20:dd736d328de6 45 // close(); //Don't want to leak
kishino 20:dd736d328de6 46 }
kishino 20:dd736d328de6 47
kishino 20:dd736d328de6 48 int Socket::set_option(int level, int optname, const void *optval, socklen_t optlen) {
kishino 20:dd736d328de6 49 return 0;
kishino 20:dd736d328de6 50 }
kishino 20:dd736d328de6 51
kishino 20:dd736d328de6 52 int Socket::get_option(int level, int optname, void *optval, socklen_t *optlen) {
kishino 20:dd736d328de6 53 return 0;
kishino 20:dd736d328de6 54 }
kishino 20:dd736d328de6 55
kishino 20:dd736d328de6 56 int Socket::close(bool shutdown)
kishino 20:dd736d328de6 57 {
kishino 27:dcc4f34448f0 58 C_SNIC_Core *snic_core_p = C_SNIC_Core::getInstance();
kishino 27:dcc4f34448f0 59 C_SNIC_UartCommandManager *uartCmdMgr_p = snic_core_p->getUartCommand();
kishino 27:dcc4f34448f0 60 // Get buffer for response payload from MemoryPool
kishino 29:6a0ba999597d 61 tagMEMPOOL_BLOCK_T *payload_buf = snic_core_p->allocCmdBuf();
kishino 27:dcc4f34448f0 62 if( payload_buf == NULL )
kishino 27:dcc4f34448f0 63 {
kishino 27:dcc4f34448f0 64 printf("socket close payload_buf NULL\r\n");
kishino 27:dcc4f34448f0 65 return -1;
kishino 27:dcc4f34448f0 66 }
kishino 27:dcc4f34448f0 67
kishino 27:dcc4f34448f0 68 C_SNIC_Core::tagSNIC_CLOSE_SOCKET_REQ_T req;
kishino 20:dd736d328de6 69
kishino 27:dcc4f34448f0 70 // Make request
kishino 27:dcc4f34448f0 71 req.cmd_sid = UART_CMD_SID_SNIC_CLOSE_SOCKET_REQ;
kishino 27:dcc4f34448f0 72 req.seq = mUartRequestSeq++;
kishino 27:dcc4f34448f0 73 req.socket_id = mSocketID;
kishino 27:dcc4f34448f0 74
kishino 29:6a0ba999597d 75 unsigned char *command_array = snic_core_p->getCommandBuf();
kishino 27:dcc4f34448f0 76 unsigned int command_len;
kishino 27:dcc4f34448f0 77 // Preparation of command
kishino 27:dcc4f34448f0 78 command_len = snic_core_p->preparationSendCommand( UART_CMD_ID_SNIC, req.cmd_sid, (unsigned char *)&req
kishino 27:dcc4f34448f0 79 , sizeof(C_SNIC_Core::tagSNIC_CLOSE_SOCKET_REQ_T), payload_buf->buf, command_array );
kishino 27:dcc4f34448f0 80
kishino 27:dcc4f34448f0 81 // Send uart command request
kishino 27:dcc4f34448f0 82 snic_core_p->sendUart( command_len, command_array );
kishino 27:dcc4f34448f0 83
kishino 27:dcc4f34448f0 84 int ret;
kishino 27:dcc4f34448f0 85 // Wait UART response
kishino 27:dcc4f34448f0 86 ret = uartCmdMgr_p->wait();
kishino 27:dcc4f34448f0 87 if( ret != 0 )
kishino 27:dcc4f34448f0 88 {
kishino 27:dcc4f34448f0 89 printf( "socket close failed\r\n" );
kishino 27:dcc4f34448f0 90 snic_core_p->freeCmdBuf( payload_buf );
kishino 27:dcc4f34448f0 91 return -1;
kishino 27:dcc4f34448f0 92 }
kishino 27:dcc4f34448f0 93
kishino 27:dcc4f34448f0 94 if( uartCmdMgr_p->getCommandStatus() != 0 )
kishino 27:dcc4f34448f0 95 {
kishino 27:dcc4f34448f0 96 printf("socket close status:%02x\r\n", uartCmdMgr_p->getCommandStatus());
kishino 27:dcc4f34448f0 97 snic_core_p->freeCmdBuf( payload_buf );
kishino 27:dcc4f34448f0 98 return -1;
kishino 27:dcc4f34448f0 99 }
kishino 27:dcc4f34448f0 100 snic_core_p->freeCmdBuf( payload_buf );
kishino 27:dcc4f34448f0 101
kishino 20:dd736d328de6 102 return 0;
kishino 20:dd736d328de6 103 }
kishino 20:dd736d328de6 104
kishino 20:dd736d328de6 105 #if 0
kishino 20:dd736d328de6 106 int Socket::select(struct timeval *timeout, bool read, bool write)
kishino 20:dd736d328de6 107 {
kishino 20:dd736d328de6 108 return 0;
kishino 20:dd736d328de6 109 }
kishino 20:dd736d328de6 110 #endif
kishino 20:dd736d328de6 111
kishino 20:dd736d328de6 112 int Socket::createSocket( unsigned char bind, unsigned int local_addr, unsigned short port )
kishino 20:dd736d328de6 113 {
kishino 24:987e412ae879 114 C_SNIC_Core *snic_core_p = C_SNIC_Core::getInstance();
kishino 24:987e412ae879 115 C_SNIC_UartCommandManager *uartCmdMgr_p = snic_core_p->getUartCommand();
kishino 20:dd736d328de6 116 // Get buffer for response payload from MemoryPool
kishino 29:6a0ba999597d 117 tagMEMPOOL_BLOCK_T *payload_buf = snic_core_p->allocCmdBuf();
kishino 20:dd736d328de6 118 if( payload_buf == NULL )
kishino 20:dd736d328de6 119 {
kishino 20:dd736d328de6 120 printf("createSocket payload_buf NULL\r\n");
kishino 20:dd736d328de6 121 return -1;
kishino 20:dd736d328de6 122 }
kishino 20:dd736d328de6 123
kishino 22:a9ec0cad4f84 124 C_SNIC_Core::tagSNIC_TCP_CREATE_SOCKET_REQ_T req;
kishino 20:dd736d328de6 125 int req_len = 0;
kishino 20:dd736d328de6 126
kishino 20:dd736d328de6 127 // Make request
kishino 20:dd736d328de6 128 req.cmd_sid = UART_CMD_SID_SNIC_TCP_CREATE_SOCKET_REQ;
kishino 20:dd736d328de6 129 req_len++;
kishino 20:dd736d328de6 130 req.seq = mUartRequestSeq++;
kishino 20:dd736d328de6 131 req_len++;
kishino 20:dd736d328de6 132 req.bind = bind;
kishino 20:dd736d328de6 133 req_len++;
kishino 20:dd736d328de6 134 if( bind != 0 )
kishino 20:dd736d328de6 135 {
kishino 32:ae95309643aa 136 // set ip addr ( byte order )
kishino 34:8c3527b8f44e 137 C_SNIC_UartMsgUtil::convertIntToByteAdday( local_addr, (char *)req.local_addr );
kishino 32:ae95309643aa 138 req.local_port[0] = ( (port & 0xFF00) >> 8 );
kishino 32:ae95309643aa 139 req.local_port[1] = (port & 0xFF);
kishino 32:ae95309643aa 140
kishino 32:ae95309643aa 141 req_len = sizeof(C_SNIC_Core::tagSNIC_TCP_CREATE_SOCKET_REQ_T);
kishino 20:dd736d328de6 142 }
kishino 20:dd736d328de6 143
kishino 29:6a0ba999597d 144 unsigned char *command_array = snic_core_p->getCommandBuf();
kishino 20:dd736d328de6 145 unsigned int command_len;
kishino 20:dd736d328de6 146 // Preparation of command
kishino 24:987e412ae879 147 command_len = snic_core_p->preparationSendCommand( UART_CMD_ID_SNIC, req.cmd_sid, (unsigned char *)&req
kishino 20:dd736d328de6 148 , req_len, payload_buf->buf, command_array );
kishino 20:dd736d328de6 149 // Send uart command request
kishino 24:987e412ae879 150 snic_core_p->sendUart( command_len, command_array );
kishino 20:dd736d328de6 151
kishino 20:dd736d328de6 152 int ret;
kishino 20:dd736d328de6 153 // Wait UART response
kishino 20:dd736d328de6 154 ret = uartCmdMgr_p->wait();
kishino 20:dd736d328de6 155 if( ret != 0 )
kishino 20:dd736d328de6 156 {
kishino 20:dd736d328de6 157 printf( "createSocket failed\r\n" );
kishino 24:987e412ae879 158 snic_core_p->freeCmdBuf( payload_buf );
kishino 20:dd736d328de6 159 return -1;
kishino 20:dd736d328de6 160 }
kishino 29:6a0ba999597d 161
kishino 20:dd736d328de6 162 if( uartCmdMgr_p->getCommandStatus() != 0 )
kishino 20:dd736d328de6 163 {
kishino 20:dd736d328de6 164 printf("createSocket status:%02x\r\n", uartCmdMgr_p->getCommandStatus());
kishino 24:987e412ae879 165 snic_core_p->freeCmdBuf( payload_buf );
kishino 20:dd736d328de6 166 return -1;
kishino 20:dd736d328de6 167 }
kishino 20:dd736d328de6 168 mSocketID = payload_buf->buf[3];
kishino 24:987e412ae879 169 snic_core_p->freeCmdBuf( payload_buf );
kishino 20:dd736d328de6 170
kishino 20:dd736d328de6 171 return 0;
kishino 20:dd736d328de6 172 }
kishino 27:dcc4f34448f0 173
kishino 29:6a0ba999597d 174 int Socket::resolveHostName( const char *host_p )
kishino 29:6a0ba999597d 175 {
kishino 29:6a0ba999597d 176 C_SNIC_Core *snic_core_p = C_SNIC_Core::getInstance();
kishino 29:6a0ba999597d 177 C_SNIC_UartCommandManager *uartCmdMgr_p = snic_core_p->getUartCommand();
kishino 29:6a0ba999597d 178 int ip_addr = 0;
kishino 29:6a0ba999597d 179
kishino 29:6a0ba999597d 180 if( host_p == NULL )
kishino 29:6a0ba999597d 181 {
kishino 29:6a0ba999597d 182 printf("resolveHostName parameter error\r\n");
kishino 29:6a0ba999597d 183 return -1;
kishino 29:6a0ba999597d 184 }
kishino 29:6a0ba999597d 185
kishino 29:6a0ba999597d 186 // Get buffer for response payload from MemoryPool
kishino 29:6a0ba999597d 187 tagMEMPOOL_BLOCK_T *payload_buf = snic_core_p->allocCmdBuf();
kishino 29:6a0ba999597d 188 if( payload_buf == NULL )
kishino 29:6a0ba999597d 189 {
kishino 29:6a0ba999597d 190 printf("resolveHostName payload_buf NULL\r\n");
kishino 29:6a0ba999597d 191 return -1;
kishino 29:6a0ba999597d 192 }
kishino 29:6a0ba999597d 193
kishino 29:6a0ba999597d 194 unsigned char buf[UART_REQUEST_PAYLOAD_MAX];
kishino 29:6a0ba999597d 195 unsigned int buf_len = 0;
kishino 29:6a0ba999597d 196
kishino 29:6a0ba999597d 197 memset( buf, 0, UART_REQUEST_PAYLOAD_MAX );
kishino 29:6a0ba999597d 198 // Make request
kishino 29:6a0ba999597d 199 buf[0] = UART_CMD_SID_SNIC_RESOLVE_NAME_REQ;
kishino 29:6a0ba999597d 200 buf_len++;
kishino 29:6a0ba999597d 201 buf[1] = mUartRequestSeq++;
kishino 29:6a0ba999597d 202 buf_len++;
kishino 29:6a0ba999597d 203 // Interface
kishino 29:6a0ba999597d 204 buf[2] = 0;
kishino 29:6a0ba999597d 205 buf_len++;
kishino 29:6a0ba999597d 206
kishino 29:6a0ba999597d 207 // Host name length
kishino 29:6a0ba999597d 208 int hostname_len = strlen(host_p);
kishino 29:6a0ba999597d 209 buf[3] = (unsigned char)hostname_len;
kishino 29:6a0ba999597d 210 buf_len++;
kishino 29:6a0ba999597d 211 memcpy( &buf[4], host_p, hostname_len );
kishino 29:6a0ba999597d 212 buf_len += hostname_len;
kishino 29:6a0ba999597d 213
kishino 29:6a0ba999597d 214 unsigned char *command_array = snic_core_p->getCommandBuf();
kishino 29:6a0ba999597d 215 unsigned int command_len;
kishino 29:6a0ba999597d 216 command_len = snic_core_p->preparationSendCommand( UART_CMD_ID_SNIC, UART_CMD_SID_SNIC_RESOLVE_NAME_REQ, buf
kishino 29:6a0ba999597d 217 , buf_len, payload_buf->buf, command_array );
kishino 29:6a0ba999597d 218
kishino 29:6a0ba999597d 219 // Send uart command request
kishino 29:6a0ba999597d 220 snic_core_p->sendUart( command_len, command_array );
kishino 29:6a0ba999597d 221
kishino 29:6a0ba999597d 222 int ret;
kishino 29:6a0ba999597d 223 // Wait UART response
kishino 29:6a0ba999597d 224 ret = uartCmdMgr_p->wait();
kishino 29:6a0ba999597d 225 if( ret != 0 )
kishino 29:6a0ba999597d 226 {
kishino 29:6a0ba999597d 227 printf( "resolveHostName failed\r\n" );
kishino 29:6a0ba999597d 228 snic_core_p->freeCmdBuf( payload_buf );
kishino 29:6a0ba999597d 229 return -1;
kishino 29:6a0ba999597d 230 }
kishino 29:6a0ba999597d 231
kishino 29:6a0ba999597d 232 // check status
kishino 29:6a0ba999597d 233 if( uartCmdMgr_p->getCommandStatus() == 0 )
kishino 29:6a0ba999597d 234 {
kishino 29:6a0ba999597d 235 ip_addr = ((payload_buf->buf[3] << 24) & 0xFF000000)
kishino 29:6a0ba999597d 236 | ((payload_buf->buf[4] << 16) & 0xFF0000)
kishino 29:6a0ba999597d 237 | ((payload_buf->buf[5] << 8) & 0xFF00)
kishino 29:6a0ba999597d 238 | (payload_buf->buf[6]);
kishino 29:6a0ba999597d 239 }
kishino 29:6a0ba999597d 240
kishino 29:6a0ba999597d 241 snic_core_p->freeCmdBuf( payload_buf );
kishino 29:6a0ba999597d 242
kishino 29:6a0ba999597d 243 return ip_addr;
kishino 29:6a0ba999597d 244 }
kishino 33:33f1bc919486 245
kishino 33:33f1bc919486 246 char *Socket::getSocketSendBuf()
kishino 33:33f1bc919486 247 {
kishino 33:33f1bc919486 248 return gSOCKET_SEND_BUF;
kishino 33:33f1bc919486 249 }