Websocket_Sample for MurataTypeYD

Dependencies:   mbed picojson

Committer:
komoritan
Date:
Thu Mar 12 12:15:46 2015 +0000
Revision:
1:b5ac0f971f43
Parent:
0:14bd24b5a77f
Fixed

Who changed what in which revision?

UserRevisionLine numberNew contents of line
komoritan 0:14bd24b5a77f 1 /* Copyright (C) 2012 mbed.org, MIT License
komoritan 0:14bd24b5a77f 2 *
komoritan 0:14bd24b5a77f 3 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
komoritan 0:14bd24b5a77f 4 * and associated documentation files (the "Software"), to deal in the Software without restriction,
komoritan 0:14bd24b5a77f 5 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
komoritan 0:14bd24b5a77f 6 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
komoritan 0:14bd24b5a77f 7 * furnished to do so, subject to the following conditions:
komoritan 0:14bd24b5a77f 8 *
komoritan 0:14bd24b5a77f 9 * The above copyright notice and this permission notice shall be included in all copies or
komoritan 0:14bd24b5a77f 10 * substantial portions of the Software.
komoritan 0:14bd24b5a77f 11 *
komoritan 0:14bd24b5a77f 12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
komoritan 0:14bd24b5a77f 13 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
komoritan 0:14bd24b5a77f 14 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
komoritan 0:14bd24b5a77f 15 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
komoritan 0:14bd24b5a77f 16 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
komoritan 0:14bd24b5a77f 17 */
komoritan 0:14bd24b5a77f 18 /* Copyright (C) 2014 Murata Manufacturing Co.,Ltd., MIT License
komoritan 0:14bd24b5a77f 19 * port to the muRata, SWITCH SCIENCE Wi-FI module TypeYD SNIC-UART.
komoritan 0:14bd24b5a77f 20 */
komoritan 0:14bd24b5a77f 21 #include "TCPSocketServer.h"
komoritan 0:14bd24b5a77f 22 #include "SNIC_Core.h"
komoritan 0:14bd24b5a77f 23
komoritan 0:14bd24b5a77f 24 #include <cstring>
komoritan 0:14bd24b5a77f 25
komoritan 0:14bd24b5a77f 26 TCPSocketServer::TCPSocketServer()
komoritan 0:14bd24b5a77f 27 {
komoritan 0:14bd24b5a77f 28 }
komoritan 0:14bd24b5a77f 29
komoritan 0:14bd24b5a77f 30 TCPSocketServer::~TCPSocketServer()
komoritan 0:14bd24b5a77f 31 {
komoritan 0:14bd24b5a77f 32 }
komoritan 0:14bd24b5a77f 33
komoritan 0:14bd24b5a77f 34 int TCPSocketServer::bind(unsigned short port)
komoritan 0:14bd24b5a77f 35 {
komoritan 0:14bd24b5a77f 36 int ret;
komoritan 0:14bd24b5a77f 37 C_SNIC_Core *snic_core_p = C_SNIC_Core::getInstance();
komoritan 0:14bd24b5a77f 38 C_SNIC_UartCommandManager *uartCmdMgr_p = snic_core_p->getUartCommand();
komoritan 0:14bd24b5a77f 39
komoritan 0:14bd24b5a77f 40 snic_core_p->lockAPI();
komoritan 0:14bd24b5a77f 41 // Get local ip address.
komoritan 0:14bd24b5a77f 42 // Get buffer for response payload from MemoryPool
komoritan 0:14bd24b5a77f 43 tagMEMPOOL_BLOCK_T *payload_buf_p = snic_core_p->allocCmdBuf();
komoritan 0:14bd24b5a77f 44 if( payload_buf_p == NULL )
komoritan 0:14bd24b5a77f 45 {
komoritan 0:14bd24b5a77f 46 DEBUG_PRINT("bind payload_buf_p NULL\r\n");
komoritan 0:14bd24b5a77f 47 snic_core_p->unlockAPI();
komoritan 0:14bd24b5a77f 48 return -1;
komoritan 0:14bd24b5a77f 49 }
komoritan 0:14bd24b5a77f 50
komoritan 0:14bd24b5a77f 51 C_SNIC_Core::tagSNIC_GET_DHCP_INFO_REQ_T req;
komoritan 0:14bd24b5a77f 52 // Make request
komoritan 0:14bd24b5a77f 53 req.cmd_sid = UART_CMD_SID_SNIC_GET_DHCP_INFO_REQ;
komoritan 0:14bd24b5a77f 54 req.seq = mUartRequestSeq++;
komoritan 0:14bd24b5a77f 55 req.interface = 0;
komoritan 0:14bd24b5a77f 56
komoritan 0:14bd24b5a77f 57 unsigned char *command_array_p = snic_core_p->getCommandBuf();
komoritan 0:14bd24b5a77f 58 unsigned int command_len;
komoritan 0:14bd24b5a77f 59 // Preparation of command
komoritan 0:14bd24b5a77f 60 command_len = snic_core_p->preparationSendCommand( UART_CMD_ID_SNIC, req.cmd_sid, (unsigned char *)&req
komoritan 0:14bd24b5a77f 61 , sizeof(C_SNIC_Core::tagSNIC_GET_DHCP_INFO_REQ_T), payload_buf_p->buf, command_array_p );
komoritan 0:14bd24b5a77f 62 // Send uart command request
komoritan 0:14bd24b5a77f 63 snic_core_p->sendUart( command_len, command_array_p );
komoritan 0:14bd24b5a77f 64 // Wait UART response
komoritan 0:14bd24b5a77f 65 ret = uartCmdMgr_p->wait();
komoritan 0:14bd24b5a77f 66 if( ret != 0 )
komoritan 0:14bd24b5a77f 67 {
komoritan 0:14bd24b5a77f 68 DEBUG_PRINT( "bind failed\r\n" );
komoritan 0:14bd24b5a77f 69 snic_core_p->freeCmdBuf( payload_buf_p );
komoritan 0:14bd24b5a77f 70 snic_core_p->unlockAPI();
komoritan 0:14bd24b5a77f 71 return -1;
komoritan 0:14bd24b5a77f 72 }
komoritan 0:14bd24b5a77f 73
komoritan 0:14bd24b5a77f 74 if( uartCmdMgr_p->getCommandStatus() != UART_CMD_RES_SNIC_SUCCESS )
komoritan 0:14bd24b5a77f 75 {
komoritan 0:14bd24b5a77f 76 DEBUG_PRINT("bind status:%02x\r\n", uartCmdMgr_p->getCommandStatus());
komoritan 0:14bd24b5a77f 77 snic_core_p->freeCmdBuf( payload_buf_p );
komoritan 0:14bd24b5a77f 78 snic_core_p->unlockAPI();
komoritan 0:14bd24b5a77f 79 return -1;
komoritan 0:14bd24b5a77f 80 }
komoritan 0:14bd24b5a77f 81
komoritan 0:14bd24b5a77f 82 snic_core_p->freeCmdBuf( payload_buf_p );
komoritan 0:14bd24b5a77f 83 snic_core_p->unlockAPI();
komoritan 0:14bd24b5a77f 84
komoritan 0:14bd24b5a77f 85 unsigned int local_addr = (payload_buf_p->buf[9] << 24)
komoritan 0:14bd24b5a77f 86 | (payload_buf_p->buf[10] << 16)
komoritan 0:14bd24b5a77f 87 | (payload_buf_p->buf[11] << 8)
komoritan 0:14bd24b5a77f 88 | (payload_buf_p->buf[12]);
komoritan 0:14bd24b5a77f 89
komoritan 0:14bd24b5a77f 90 // Socket create
komoritan 0:14bd24b5a77f 91 ret = createSocket( 1, local_addr, port );
komoritan 0:14bd24b5a77f 92 if( ret != 0 )
komoritan 0:14bd24b5a77f 93 {
komoritan 0:14bd24b5a77f 94 DEBUG_PRINT("bind error : %d\r\n", ret);
komoritan 0:14bd24b5a77f 95 return -1;
komoritan 0:14bd24b5a77f 96 }
komoritan 0:14bd24b5a77f 97
komoritan 0:14bd24b5a77f 98 return 0;
komoritan 0:14bd24b5a77f 99 }
komoritan 0:14bd24b5a77f 100
komoritan 0:14bd24b5a77f 101 int TCPSocketServer::listen(int max)
komoritan 0:14bd24b5a77f 102 {
komoritan 0:14bd24b5a77f 103 C_SNIC_Core *snic_core_p = C_SNIC_Core::getInstance();
komoritan 0:14bd24b5a77f 104 C_SNIC_UartCommandManager *uartCmdMgr_p = snic_core_p->getUartCommand();
komoritan 0:14bd24b5a77f 105
komoritan 0:14bd24b5a77f 106 snic_core_p->lockAPI();
komoritan 0:14bd24b5a77f 107 // Get buffer for response payload from MemoryPool
komoritan 0:14bd24b5a77f 108 tagMEMPOOL_BLOCK_T *payload_buf_p = snic_core_p->allocCmdBuf();
komoritan 0:14bd24b5a77f 109 if( payload_buf_p == NULL )
komoritan 0:14bd24b5a77f 110 {
komoritan 0:14bd24b5a77f 111 DEBUG_PRINT("listen payload_buf_p NULL\r\n");
komoritan 0:14bd24b5a77f 112 snic_core_p->unlockAPI();
komoritan 0:14bd24b5a77f 113 return -1;
komoritan 0:14bd24b5a77f 114 }
komoritan 0:14bd24b5a77f 115
komoritan 0:14bd24b5a77f 116 C_SNIC_Core::tagSNIC_TCP_CREATE_CONNECTION_REQ_T req;
komoritan 0:14bd24b5a77f 117 // Make request
komoritan 0:14bd24b5a77f 118 req.cmd_sid = UART_CMD_SID_SNIC_TCP_CREATE_CONNECTION_REQ;
komoritan 0:14bd24b5a77f 119 req.seq = mUartRequestSeq++;
komoritan 0:14bd24b5a77f 120 req.socket_id = mSocketID;
komoritan 0:14bd24b5a77f 121 req.recv_bufsize[0] = ( (SNIC_UART_RECVBUF_SIZE & 0xFF00) >> 8 );
komoritan 0:14bd24b5a77f 122 req.recv_bufsize[1] = (SNIC_UART_RECVBUF_SIZE & 0xFF);
komoritan 0:14bd24b5a77f 123 req.max_client = max;
komoritan 0:14bd24b5a77f 124
komoritan 0:14bd24b5a77f 125 unsigned char *command_array_p = snic_core_p->getCommandBuf();
komoritan 0:14bd24b5a77f 126 unsigned int command_len;
komoritan 0:14bd24b5a77f 127 // Preparation of command
komoritan 0:14bd24b5a77f 128 command_len = snic_core_p->preparationSendCommand( UART_CMD_ID_SNIC, req.cmd_sid, (unsigned char *)&req
komoritan 0:14bd24b5a77f 129 , sizeof(C_SNIC_Core::tagSNIC_TCP_CREATE_CONNECTION_REQ_T), payload_buf_p->buf, command_array_p );
komoritan 0:14bd24b5a77f 130
komoritan 0:14bd24b5a77f 131 int ret;
komoritan 0:14bd24b5a77f 132
komoritan 0:14bd24b5a77f 133 // Send uart command request
komoritan 0:14bd24b5a77f 134 snic_core_p->sendUart( command_len, command_array_p );
komoritan 0:14bd24b5a77f 135
komoritan 0:14bd24b5a77f 136 // Wait UART response
komoritan 0:14bd24b5a77f 137 ret = uartCmdMgr_p->wait();
komoritan 0:14bd24b5a77f 138 if( ret != 0 )
komoritan 0:14bd24b5a77f 139 {
komoritan 0:14bd24b5a77f 140 DEBUG_PRINT( "listen failed\r\n" );
komoritan 0:14bd24b5a77f 141 snic_core_p->freeCmdBuf( payload_buf_p );
komoritan 0:14bd24b5a77f 142 snic_core_p->unlockAPI();
komoritan 0:14bd24b5a77f 143 return -1;
komoritan 0:14bd24b5a77f 144 }
komoritan 0:14bd24b5a77f 145
komoritan 0:14bd24b5a77f 146 if( uartCmdMgr_p->getCommandStatus() != 0 )
komoritan 0:14bd24b5a77f 147 {
komoritan 0:14bd24b5a77f 148 DEBUG_PRINT("listen status:%02x\r\n", uartCmdMgr_p->getCommandStatus());
komoritan 0:14bd24b5a77f 149 snic_core_p->freeCmdBuf( payload_buf_p );
komoritan 0:14bd24b5a77f 150 snic_core_p->unlockAPI();
komoritan 0:14bd24b5a77f 151 return -1;
komoritan 0:14bd24b5a77f 152 }
komoritan 0:14bd24b5a77f 153
komoritan 0:14bd24b5a77f 154 snic_core_p->freeCmdBuf( payload_buf_p );
komoritan 0:14bd24b5a77f 155 snic_core_p->unlockAPI();
komoritan 0:14bd24b5a77f 156 return 0;
komoritan 0:14bd24b5a77f 157 }
komoritan 0:14bd24b5a77f 158
komoritan 0:14bd24b5a77f 159 int TCPSocketServer::accept(TCPSocketConnection& connection)
komoritan 0:14bd24b5a77f 160 {
komoritan 0:14bd24b5a77f 161 C_SNIC_Core *snic_core_p = C_SNIC_Core::getInstance();
komoritan 0:14bd24b5a77f 162 int i;
komoritan 0:14bd24b5a77f 163 int ret = -1;
komoritan 0:14bd24b5a77f 164 bool accepted = false;
komoritan 0:14bd24b5a77f 165
komoritan 0:14bd24b5a77f 166 C_SNIC_Core::tagCONNECT_INFO_T *con_info_p;
komoritan 0:14bd24b5a77f 167
komoritan 0:14bd24b5a77f 168 do
komoritan 0:14bd24b5a77f 169 {
komoritan 0:14bd24b5a77f 170 for( i = 0; i < MAX_SOCKET_ID+1; i++ )
komoritan 0:14bd24b5a77f 171 {
komoritan 0:14bd24b5a77f 172 // Get connection information
komoritan 0:14bd24b5a77f 173 con_info_p = snic_core_p->getConnectInfo( i );
komoritan 0:14bd24b5a77f 174 if( (con_info_p->is_connected == true)
komoritan 0:14bd24b5a77f 175 && (con_info_p->is_accept == true)
komoritan 0:14bd24b5a77f 176 && (con_info_p->parent_socket == mSocketID) )
komoritan 0:14bd24b5a77f 177 {
komoritan 0:14bd24b5a77f 178 // Set socket id
komoritan 0:14bd24b5a77f 179 connection.setAcceptSocket( i );
komoritan 0:14bd24b5a77f 180 ret = 0;
komoritan 0:14bd24b5a77f 181 accepted = true;
komoritan 0:14bd24b5a77f 182 break;
komoritan 0:14bd24b5a77f 183 }
komoritan 0:14bd24b5a77f 184 }
komoritan 0:14bd24b5a77f 185 }while( accepted == false );
komoritan 0:14bd24b5a77f 186 con_info_p->is_accept = false;
komoritan 0:14bd24b5a77f 187
komoritan 0:14bd24b5a77f 188 char remote_addr[ 20 ] = {'\0'};
komoritan 0:14bd24b5a77f 189 sprintf( remote_addr, "%d.%d.%d.%d"
komoritan 0:14bd24b5a77f 190 , (con_info_p->from_ip >> 24 ) & 0xff
komoritan 0:14bd24b5a77f 191 , (con_info_p->from_ip >> 16 ) & 0xff
komoritan 0:14bd24b5a77f 192 , (con_info_p->from_ip >> 8 ) & 0xff
komoritan 0:14bd24b5a77f 193 , con_info_p->from_ip & 0xff );
komoritan 0:14bd24b5a77f 194
komoritan 0:14bd24b5a77f 195 connection.set_address( remote_addr, (int)con_info_p->from_port );
komoritan 0:14bd24b5a77f 196
komoritan 0:14bd24b5a77f 197 return ret;
komoritan 0:14bd24b5a77f 198 }