KDDI Fx0 hackathon / SNICInterface_Tweet
Committer:
komoritan
Date:
Tue Feb 10 12:26:31 2015 +0000
Revision:
0:edaa24c1f5cd
Debug Code

Who changed what in which revision?

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