SNIC UART Interface library for Murata Type-YD module

Dependents:   WebSocketServerTest

Fork of SNICInterface_mod by Toyomasa Watarai

Committer:
kishino
Date:
Mon May 26 05:17:28 2014 +0000
Revision:
29:6a0ba999597d
Parent:
27:dcc4f34448f0
Child:
31:15c22824cc46
Changed the receive logic of UART.; Changed the use area of the global buffer.

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 20:dd736d328de6 33 Socket::Socket()
kishino 20:dd736d328de6 34 {
kishino 20:dd736d328de6 35 mSocketID = -1;
kishino 20:dd736d328de6 36 }
kishino 20:dd736d328de6 37
kishino 29:6a0ba999597d 38 void Socket::set_blocking(bool blocking, unsigned int timeout) {
kishino 29:6a0ba999597d 39 _blocking = blocking;
kishino 29:6a0ba999597d 40 _timeout = timeout;
kishino 29:6a0ba999597d 41 }
kishino 29:6a0ba999597d 42
kishino 20:dd736d328de6 43 Socket::~Socket() {
kishino 20:dd736d328de6 44 // close(); //Don't want to leak
kishino 20:dd736d328de6 45 }
kishino 20:dd736d328de6 46
kishino 20:dd736d328de6 47 int Socket::set_option(int level, int optname, const void *optval, socklen_t optlen) {
kishino 20:dd736d328de6 48 return 0;
kishino 20:dd736d328de6 49 }
kishino 20:dd736d328de6 50
kishino 20:dd736d328de6 51 int Socket::get_option(int level, int optname, void *optval, socklen_t *optlen) {
kishino 20:dd736d328de6 52 return 0;
kishino 20:dd736d328de6 53 }
kishino 20:dd736d328de6 54
kishino 20:dd736d328de6 55 int Socket::close(bool shutdown)
kishino 20:dd736d328de6 56 {
kishino 27:dcc4f34448f0 57 C_SNIC_Core *snic_core_p = C_SNIC_Core::getInstance();
kishino 27:dcc4f34448f0 58 C_SNIC_UartCommandManager *uartCmdMgr_p = snic_core_p->getUartCommand();
kishino 27:dcc4f34448f0 59 // Get buffer for response payload from MemoryPool
kishino 29:6a0ba999597d 60 tagMEMPOOL_BLOCK_T *payload_buf = snic_core_p->allocCmdBuf();
kishino 27:dcc4f34448f0 61 if( payload_buf == NULL )
kishino 27:dcc4f34448f0 62 {
kishino 27:dcc4f34448f0 63 printf("socket close payload_buf NULL\r\n");
kishino 27:dcc4f34448f0 64 return -1;
kishino 27:dcc4f34448f0 65 }
kishino 27:dcc4f34448f0 66
kishino 27:dcc4f34448f0 67 C_SNIC_Core::tagSNIC_CLOSE_SOCKET_REQ_T req;
kishino 20:dd736d328de6 68
kishino 27:dcc4f34448f0 69 // Make request
kishino 27:dcc4f34448f0 70 req.cmd_sid = UART_CMD_SID_SNIC_CLOSE_SOCKET_REQ;
kishino 27:dcc4f34448f0 71 req.seq = mUartRequestSeq++;
kishino 27:dcc4f34448f0 72 req.socket_id = mSocketID;
kishino 27:dcc4f34448f0 73
kishino 29:6a0ba999597d 74 unsigned char *command_array = snic_core_p->getCommandBuf();
kishino 27:dcc4f34448f0 75 unsigned int command_len;
kishino 27:dcc4f34448f0 76 // Preparation of command
kishino 27:dcc4f34448f0 77 command_len = snic_core_p->preparationSendCommand( UART_CMD_ID_SNIC, req.cmd_sid, (unsigned char *)&req
kishino 27:dcc4f34448f0 78 , sizeof(C_SNIC_Core::tagSNIC_CLOSE_SOCKET_REQ_T), payload_buf->buf, command_array );
kishino 27:dcc4f34448f0 79
kishino 27:dcc4f34448f0 80 // Send uart command request
kishino 27:dcc4f34448f0 81 snic_core_p->sendUart( command_len, command_array );
kishino 27:dcc4f34448f0 82
kishino 27:dcc4f34448f0 83 int ret;
kishino 27:dcc4f34448f0 84 // Wait UART response
kishino 27:dcc4f34448f0 85 ret = uartCmdMgr_p->wait();
kishino 27:dcc4f34448f0 86 if( ret != 0 )
kishino 27:dcc4f34448f0 87 {
kishino 27:dcc4f34448f0 88 printf( "socket close failed\r\n" );
kishino 27:dcc4f34448f0 89 snic_core_p->freeCmdBuf( payload_buf );
kishino 27:dcc4f34448f0 90 return -1;
kishino 27:dcc4f34448f0 91 }
kishino 27:dcc4f34448f0 92
kishino 27:dcc4f34448f0 93 if( uartCmdMgr_p->getCommandStatus() != 0 )
kishino 27:dcc4f34448f0 94 {
kishino 27:dcc4f34448f0 95 printf("socket close status:%02x\r\n", uartCmdMgr_p->getCommandStatus());
kishino 27:dcc4f34448f0 96 snic_core_p->freeCmdBuf( payload_buf );
kishino 27:dcc4f34448f0 97 return -1;
kishino 27:dcc4f34448f0 98 }
kishino 27:dcc4f34448f0 99 snic_core_p->freeCmdBuf( payload_buf );
kishino 27:dcc4f34448f0 100
kishino 20:dd736d328de6 101 return 0;
kishino 20:dd736d328de6 102 }
kishino 20:dd736d328de6 103
kishino 20:dd736d328de6 104 #if 0
kishino 20:dd736d328de6 105 int Socket::select(struct timeval *timeout, bool read, bool write)
kishino 20:dd736d328de6 106 {
kishino 20:dd736d328de6 107 return 0;
kishino 20:dd736d328de6 108 }
kishino 20:dd736d328de6 109 #endif
kishino 20:dd736d328de6 110
kishino 20:dd736d328de6 111 int Socket::createSocket( unsigned char bind, unsigned int local_addr, unsigned short port )
kishino 20:dd736d328de6 112 {
kishino 24:987e412ae879 113 C_SNIC_Core *snic_core_p = C_SNIC_Core::getInstance();
kishino 24:987e412ae879 114 C_SNIC_UartCommandManager *uartCmdMgr_p = snic_core_p->getUartCommand();
kishino 20:dd736d328de6 115 // Get buffer for response payload from MemoryPool
kishino 29:6a0ba999597d 116 tagMEMPOOL_BLOCK_T *payload_buf = snic_core_p->allocCmdBuf();
kishino 20:dd736d328de6 117 if( payload_buf == NULL )
kishino 20:dd736d328de6 118 {
kishino 20:dd736d328de6 119 printf("createSocket payload_buf NULL\r\n");
kishino 20:dd736d328de6 120 return -1;
kishino 20:dd736d328de6 121 }
kishino 20:dd736d328de6 122
kishino 22:a9ec0cad4f84 123 C_SNIC_Core::tagSNIC_TCP_CREATE_SOCKET_REQ_T req;
kishino 20:dd736d328de6 124 int req_len = 0;
kishino 20:dd736d328de6 125
kishino 20:dd736d328de6 126 // Make request
kishino 20:dd736d328de6 127 req.cmd_sid = UART_CMD_SID_SNIC_TCP_CREATE_SOCKET_REQ;
kishino 20:dd736d328de6 128 req_len++;
kishino 20:dd736d328de6 129 req.seq = mUartRequestSeq++;
kishino 20:dd736d328de6 130 req_len++;
kishino 20:dd736d328de6 131 req.bind = bind;
kishino 20:dd736d328de6 132 req_len++;
kishino 20:dd736d328de6 133 if( bind != 0 )
kishino 20:dd736d328de6 134 {
kishino 20:dd736d328de6 135 /*
kishino 20:dd736d328de6 136 req.local_addr = local_addr;
kishino 20:dd736d328de6 137 req_len++;
kishino 20:dd736d328de6 138 req.local_port = port;
kishino 20:dd736d328de6 139 req_len++;
kishino 20:dd736d328de6 140 */
kishino 20:dd736d328de6 141 }
kishino 20:dd736d328de6 142
kishino 29:6a0ba999597d 143 unsigned char *command_array = snic_core_p->getCommandBuf();
kishino 20:dd736d328de6 144 unsigned int command_len;
kishino 20:dd736d328de6 145 // Preparation of command
kishino 24:987e412ae879 146 command_len = snic_core_p->preparationSendCommand( UART_CMD_ID_SNIC, req.cmd_sid, (unsigned char *)&req
kishino 20:dd736d328de6 147 , req_len, payload_buf->buf, command_array );
kishino 20:dd736d328de6 148 // Send uart command request
kishino 24:987e412ae879 149 snic_core_p->sendUart( command_len, command_array );
kishino 20:dd736d328de6 150
kishino 20:dd736d328de6 151 int ret;
kishino 20:dd736d328de6 152 // Wait UART response
kishino 20:dd736d328de6 153 ret = uartCmdMgr_p->wait();
kishino 20:dd736d328de6 154 if( ret != 0 )
kishino 20:dd736d328de6 155 {
kishino 20:dd736d328de6 156 printf( "createSocket failed\r\n" );
kishino 24:987e412ae879 157 snic_core_p->freeCmdBuf( payload_buf );
kishino 20:dd736d328de6 158 return -1;
kishino 20:dd736d328de6 159 }
kishino 29:6a0ba999597d 160
kishino 20:dd736d328de6 161 if( uartCmdMgr_p->getCommandStatus() != 0 )
kishino 20:dd736d328de6 162 {
kishino 20:dd736d328de6 163 printf("createSocket status:%02x\r\n", uartCmdMgr_p->getCommandStatus());
kishino 24:987e412ae879 164 snic_core_p->freeCmdBuf( payload_buf );
kishino 20:dd736d328de6 165 return -1;
kishino 20:dd736d328de6 166 }
kishino 20:dd736d328de6 167 mSocketID = payload_buf->buf[3];
kishino 24:987e412ae879 168 snic_core_p->freeCmdBuf( payload_buf );
kishino 20:dd736d328de6 169
kishino 20:dd736d328de6 170 return 0;
kishino 20:dd736d328de6 171 }
kishino 27:dcc4f34448f0 172
kishino 27:dcc4f34448f0 173 int Socket::addrToInteger( const char *addr_p )
kishino 27:dcc4f34448f0 174 {
kishino 27:dcc4f34448f0 175 if( addr_p == NULL )
kishino 27:dcc4f34448f0 176 {
kishino 27:dcc4f34448f0 177 printf("addrToInteger parameter error\r\n");
kishino 27:dcc4f34448f0 178 return 0;
kishino 27:dcc4f34448f0 179 }
kishino 27:dcc4f34448f0 180
kishino 27:dcc4f34448f0 181 unsigned char ipadr[4];
kishino 27:dcc4f34448f0 182 unsigned char temp= 0;
kishino 27:dcc4f34448f0 183 int i,j,k;
kishino 27:dcc4f34448f0 184
kishino 27:dcc4f34448f0 185 /* convert to char[4] */
kishino 27:dcc4f34448f0 186 k=0;
kishino 27:dcc4f34448f0 187 for(i=0; i<4; i ++)
kishino 27:dcc4f34448f0 188 {
kishino 27:dcc4f34448f0 189 for(j=0; j<4; j ++)
kishino 27:dcc4f34448f0 190 {
kishino 27:dcc4f34448f0 191 if((addr_p[k] > 0x2F)&&(addr_p[k] < 0x3A))
kishino 27:dcc4f34448f0 192 {
kishino 27:dcc4f34448f0 193 temp = (temp * 10) + addr_p[k]-0x30;
kishino 27:dcc4f34448f0 194 }
kishino 27:dcc4f34448f0 195 else if((addr_p[k] == 0x20)&&(temp == 0))
kishino 27:dcc4f34448f0 196 {
kishino 27:dcc4f34448f0 197 }
kishino 27:dcc4f34448f0 198 else
kishino 27:dcc4f34448f0 199 {
kishino 27:dcc4f34448f0 200 ipadr[i]=temp;
kishino 27:dcc4f34448f0 201 temp = 0;
kishino 27:dcc4f34448f0 202 k++;
kishino 27:dcc4f34448f0 203 break;
kishino 27:dcc4f34448f0 204 }
kishino 27:dcc4f34448f0 205 k++;
kishino 27:dcc4f34448f0 206 }
kishino 27:dcc4f34448f0 207 }
kishino 27:dcc4f34448f0 208
kishino 27:dcc4f34448f0 209 int addr = ( (ipadr[0]<<24) | (ipadr[1]<<16) | (ipadr[2]<<8) | ipadr[3] );
kishino 27:dcc4f34448f0 210
kishino 27:dcc4f34448f0 211 return addr;
kishino 27:dcc4f34448f0 212 }
kishino 29:6a0ba999597d 213
kishino 29:6a0ba999597d 214 int Socket::resolveHostName( const char *host_p )
kishino 29:6a0ba999597d 215 {
kishino 29:6a0ba999597d 216 C_SNIC_Core *snic_core_p = C_SNIC_Core::getInstance();
kishino 29:6a0ba999597d 217 C_SNIC_UartCommandManager *uartCmdMgr_p = snic_core_p->getUartCommand();
kishino 29:6a0ba999597d 218 int ip_addr = 0;
kishino 29:6a0ba999597d 219
kishino 29:6a0ba999597d 220 if( host_p == NULL )
kishino 29:6a0ba999597d 221 {
kishino 29:6a0ba999597d 222 printf("resolveHostName parameter error\r\n");
kishino 29:6a0ba999597d 223 return -1;
kishino 29:6a0ba999597d 224 }
kishino 29:6a0ba999597d 225
kishino 29:6a0ba999597d 226 // Get buffer for response payload from MemoryPool
kishino 29:6a0ba999597d 227 tagMEMPOOL_BLOCK_T *payload_buf = snic_core_p->allocCmdBuf();
kishino 29:6a0ba999597d 228 if( payload_buf == NULL )
kishino 29:6a0ba999597d 229 {
kishino 29:6a0ba999597d 230 printf("resolveHostName payload_buf NULL\r\n");
kishino 29:6a0ba999597d 231 return -1;
kishino 29:6a0ba999597d 232 }
kishino 29:6a0ba999597d 233
kishino 29:6a0ba999597d 234 unsigned char buf[UART_REQUEST_PAYLOAD_MAX];
kishino 29:6a0ba999597d 235 unsigned int buf_len = 0;
kishino 29:6a0ba999597d 236
kishino 29:6a0ba999597d 237 memset( buf, 0, UART_REQUEST_PAYLOAD_MAX );
kishino 29:6a0ba999597d 238 // Make request
kishino 29:6a0ba999597d 239 buf[0] = UART_CMD_SID_SNIC_RESOLVE_NAME_REQ;
kishino 29:6a0ba999597d 240 buf_len++;
kishino 29:6a0ba999597d 241 buf[1] = mUartRequestSeq++;
kishino 29:6a0ba999597d 242 buf_len++;
kishino 29:6a0ba999597d 243 // Interface
kishino 29:6a0ba999597d 244 buf[2] = 0;
kishino 29:6a0ba999597d 245 buf_len++;
kishino 29:6a0ba999597d 246
kishino 29:6a0ba999597d 247 // Host name length
kishino 29:6a0ba999597d 248 int hostname_len = strlen(host_p);
kishino 29:6a0ba999597d 249 buf[3] = (unsigned char)hostname_len;
kishino 29:6a0ba999597d 250 buf_len++;
kishino 29:6a0ba999597d 251 memcpy( &buf[4], host_p, hostname_len );
kishino 29:6a0ba999597d 252 buf_len += hostname_len;
kishino 29:6a0ba999597d 253
kishino 29:6a0ba999597d 254 unsigned char *command_array = snic_core_p->getCommandBuf();
kishino 29:6a0ba999597d 255 unsigned int command_len;
kishino 29:6a0ba999597d 256 command_len = snic_core_p->preparationSendCommand( UART_CMD_ID_SNIC, UART_CMD_SID_SNIC_RESOLVE_NAME_REQ, buf
kishino 29:6a0ba999597d 257 , buf_len, payload_buf->buf, command_array );
kishino 29:6a0ba999597d 258
kishino 29:6a0ba999597d 259 // Send uart command request
kishino 29:6a0ba999597d 260 snic_core_p->sendUart( command_len, command_array );
kishino 29:6a0ba999597d 261
kishino 29:6a0ba999597d 262 int ret;
kishino 29:6a0ba999597d 263 // Wait UART response
kishino 29:6a0ba999597d 264 ret = uartCmdMgr_p->wait();
kishino 29:6a0ba999597d 265 if( ret != 0 )
kishino 29:6a0ba999597d 266 {
kishino 29:6a0ba999597d 267 printf( "resolveHostName failed\r\n" );
kishino 29:6a0ba999597d 268 snic_core_p->freeCmdBuf( payload_buf );
kishino 29:6a0ba999597d 269 return -1;
kishino 29:6a0ba999597d 270 }
kishino 29:6a0ba999597d 271
kishino 29:6a0ba999597d 272 // check status
kishino 29:6a0ba999597d 273 if( uartCmdMgr_p->getCommandStatus() == 0 )
kishino 29:6a0ba999597d 274 {
kishino 29:6a0ba999597d 275 ip_addr = ((payload_buf->buf[3] << 24) & 0xFF000000)
kishino 29:6a0ba999597d 276 | ((payload_buf->buf[4] << 16) & 0xFF0000)
kishino 29:6a0ba999597d 277 | ((payload_buf->buf[5] << 8) & 0xFF00)
kishino 29:6a0ba999597d 278 | (payload_buf->buf[6]);
kishino 29:6a0ba999597d 279 }
kishino 29:6a0ba999597d 280
kishino 29:6a0ba999597d 281 snic_core_p->freeCmdBuf( payload_buf );
kishino 29:6a0ba999597d 282
kishino 29:6a0ba999597d 283 return ip_addr;
kishino 29:6a0ba999597d 284 }