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 "TCPSocketServer.h"
komoritan 0:edaa24c1f5cd 22 #include "SNIC_Core.h"
komoritan 0:edaa24c1f5cd 23
komoritan 0:edaa24c1f5cd 24 #include <cstring>
komoritan 0:edaa24c1f5cd 25
komoritan 0:edaa24c1f5cd 26 TCPSocketServer::TCPSocketServer()
komoritan 0:edaa24c1f5cd 27 {
komoritan 0:edaa24c1f5cd 28 }
komoritan 0:edaa24c1f5cd 29
komoritan 0:edaa24c1f5cd 30 TCPSocketServer::~TCPSocketServer()
komoritan 0:edaa24c1f5cd 31 {
komoritan 0:edaa24c1f5cd 32 }
komoritan 0:edaa24c1f5cd 33
komoritan 0:edaa24c1f5cd 34 int TCPSocketServer::bind(unsigned short port)
komoritan 0:edaa24c1f5cd 35 {
komoritan 0:edaa24c1f5cd 36 int ret;
komoritan 0:edaa24c1f5cd 37 C_SNIC_Core *snic_core_p = C_SNIC_Core::getInstance();
komoritan 0:edaa24c1f5cd 38 C_SNIC_UartCommandManager *uartCmdMgr_p = snic_core_p->getUartCommand();
komoritan 0:edaa24c1f5cd 39
komoritan 0:edaa24c1f5cd 40 snic_core_p->lockAPI();
komoritan 0:edaa24c1f5cd 41 // Get local ip address.
komoritan 0:edaa24c1f5cd 42 // Get buffer for response payload from MemoryPool
komoritan 0:edaa24c1f5cd 43 tagMEMPOOL_BLOCK_T *payload_buf_p = snic_core_p->allocCmdBuf();
komoritan 0:edaa24c1f5cd 44 if( payload_buf_p == NULL )
komoritan 0:edaa24c1f5cd 45 {
komoritan 0:edaa24c1f5cd 46 DEBUG_PRINT("bind payload_buf_p NULL\r\n");
komoritan 0:edaa24c1f5cd 47 snic_core_p->unlockAPI();
komoritan 0:edaa24c1f5cd 48 return -1;
komoritan 0:edaa24c1f5cd 49 }
komoritan 0:edaa24c1f5cd 50
komoritan 0:edaa24c1f5cd 51 C_SNIC_Core::tagSNIC_GET_DHCP_INFO_REQ_T req;
komoritan 0:edaa24c1f5cd 52 // Make request
komoritan 0:edaa24c1f5cd 53 req.cmd_sid = UART_CMD_SID_SNIC_GET_DHCP_INFO_REQ;
komoritan 0:edaa24c1f5cd 54 req.seq = mUartRequestSeq++;
komoritan 0:edaa24c1f5cd 55 req.interface = 0;
komoritan 0:edaa24c1f5cd 56
komoritan 0:edaa24c1f5cd 57 unsigned char *command_array_p = snic_core_p->getCommandBuf();
komoritan 0:edaa24c1f5cd 58 unsigned int command_len;
komoritan 0:edaa24c1f5cd 59 // Preparation of command
komoritan 0:edaa24c1f5cd 60 command_len = snic_core_p->preparationSendCommand( UART_CMD_ID_SNIC, req.cmd_sid, (unsigned char *)&req
komoritan 0:edaa24c1f5cd 61 , sizeof(C_SNIC_Core::tagSNIC_GET_DHCP_INFO_REQ_T), payload_buf_p->buf, command_array_p );
komoritan 0:edaa24c1f5cd 62 // Send uart command request
komoritan 0:edaa24c1f5cd 63 snic_core_p->sendUart( command_len, command_array_p );
komoritan 0:edaa24c1f5cd 64 // Wait UART response
komoritan 0:edaa24c1f5cd 65 ret = uartCmdMgr_p->wait();
komoritan 0:edaa24c1f5cd 66 if( ret != 0 )
komoritan 0:edaa24c1f5cd 67 {
komoritan 0:edaa24c1f5cd 68 DEBUG_PRINT( "bind failed\r\n" );
komoritan 0:edaa24c1f5cd 69 snic_core_p->freeCmdBuf( payload_buf_p );
komoritan 0:edaa24c1f5cd 70 snic_core_p->unlockAPI();
komoritan 0:edaa24c1f5cd 71 return -1;
komoritan 0:edaa24c1f5cd 72 }
komoritan 0:edaa24c1f5cd 73
komoritan 0:edaa24c1f5cd 74 if( uartCmdMgr_p->getCommandStatus() != UART_CMD_RES_SNIC_SUCCESS )
komoritan 0:edaa24c1f5cd 75 {
komoritan 0:edaa24c1f5cd 76 DEBUG_PRINT("bind status:%02x\r\n", uartCmdMgr_p->getCommandStatus());
komoritan 0:edaa24c1f5cd 77 snic_core_p->freeCmdBuf( payload_buf_p );
komoritan 0:edaa24c1f5cd 78 snic_core_p->unlockAPI();
komoritan 0:edaa24c1f5cd 79 return -1;
komoritan 0:edaa24c1f5cd 80 }
komoritan 0:edaa24c1f5cd 81
komoritan 0:edaa24c1f5cd 82 snic_core_p->freeCmdBuf( payload_buf_p );
komoritan 0:edaa24c1f5cd 83 snic_core_p->unlockAPI();
komoritan 0:edaa24c1f5cd 84
komoritan 0:edaa24c1f5cd 85 unsigned int local_addr = (payload_buf_p->buf[9] << 24)
komoritan 0:edaa24c1f5cd 86 | (payload_buf_p->buf[10] << 16)
komoritan 0:edaa24c1f5cd 87 | (payload_buf_p->buf[11] << 8)
komoritan 0:edaa24c1f5cd 88 | (payload_buf_p->buf[12]);
komoritan 0:edaa24c1f5cd 89
komoritan 0:edaa24c1f5cd 90 // Socket create
komoritan 0:edaa24c1f5cd 91 ret = createSocket( 1, local_addr, port );
komoritan 0:edaa24c1f5cd 92 if( ret != 0 )
komoritan 0:edaa24c1f5cd 93 {
komoritan 0:edaa24c1f5cd 94 DEBUG_PRINT("bind error : %d\r\n", ret);
komoritan 0:edaa24c1f5cd 95 return -1;
komoritan 0:edaa24c1f5cd 96 }
komoritan 0:edaa24c1f5cd 97
komoritan 0:edaa24c1f5cd 98 return 0;
komoritan 0:edaa24c1f5cd 99 }
komoritan 0:edaa24c1f5cd 100
komoritan 0:edaa24c1f5cd 101 int TCPSocketServer::listen(int max)
komoritan 0:edaa24c1f5cd 102 {
komoritan 0:edaa24c1f5cd 103 C_SNIC_Core *snic_core_p = C_SNIC_Core::getInstance();
komoritan 0:edaa24c1f5cd 104 C_SNIC_UartCommandManager *uartCmdMgr_p = snic_core_p->getUartCommand();
komoritan 0:edaa24c1f5cd 105
komoritan 0:edaa24c1f5cd 106 snic_core_p->lockAPI();
komoritan 0:edaa24c1f5cd 107 // Get buffer for response payload from MemoryPool
komoritan 0:edaa24c1f5cd 108 tagMEMPOOL_BLOCK_T *payload_buf_p = snic_core_p->allocCmdBuf();
komoritan 0:edaa24c1f5cd 109 if( payload_buf_p == NULL )
komoritan 0:edaa24c1f5cd 110 {
komoritan 0:edaa24c1f5cd 111 DEBUG_PRINT("listen payload_buf_p NULL\r\n");
komoritan 0:edaa24c1f5cd 112 snic_core_p->unlockAPI();
komoritan 0:edaa24c1f5cd 113 return -1;
komoritan 0:edaa24c1f5cd 114 }
komoritan 0:edaa24c1f5cd 115
komoritan 0:edaa24c1f5cd 116 C_SNIC_Core::tagSNIC_TCP_CREATE_CONNECTION_REQ_T req;
komoritan 0:edaa24c1f5cd 117 // Make request
komoritan 0:edaa24c1f5cd 118 req.cmd_sid = UART_CMD_SID_SNIC_TCP_CREATE_CONNECTION_REQ;
komoritan 0:edaa24c1f5cd 119 req.seq = mUartRequestSeq++;
komoritan 0:edaa24c1f5cd 120 req.socket_id = mSocketID;
komoritan 0:edaa24c1f5cd 121 req.recv_bufsize[0] = ( (SNIC_UART_RECVBUF_SIZE & 0xFF00) >> 8 );
komoritan 0:edaa24c1f5cd 122 req.recv_bufsize[1] = (SNIC_UART_RECVBUF_SIZE & 0xFF);
komoritan 0:edaa24c1f5cd 123 req.max_client = max;
komoritan 0:edaa24c1f5cd 124
komoritan 0:edaa24c1f5cd 125 unsigned char *command_array_p = snic_core_p->getCommandBuf();
komoritan 0:edaa24c1f5cd 126 unsigned int command_len;
komoritan 0:edaa24c1f5cd 127 // Preparation of command
komoritan 0:edaa24c1f5cd 128 command_len = snic_core_p->preparationSendCommand( UART_CMD_ID_SNIC, req.cmd_sid, (unsigned char *)&req
komoritan 0:edaa24c1f5cd 129 , sizeof(C_SNIC_Core::tagSNIC_TCP_CREATE_CONNECTION_REQ_T), payload_buf_p->buf, command_array_p );
komoritan 0:edaa24c1f5cd 130
komoritan 0:edaa24c1f5cd 131 int ret;
komoritan 0:edaa24c1f5cd 132
komoritan 0:edaa24c1f5cd 133 // Send uart command request
komoritan 0:edaa24c1f5cd 134 snic_core_p->sendUart( command_len, command_array_p );
komoritan 0:edaa24c1f5cd 135
komoritan 0:edaa24c1f5cd 136 // Wait UART response
komoritan 0:edaa24c1f5cd 137 ret = uartCmdMgr_p->wait();
komoritan 0:edaa24c1f5cd 138 if( ret != 0 )
komoritan 0:edaa24c1f5cd 139 {
komoritan 0:edaa24c1f5cd 140 DEBUG_PRINT( "listen failed\r\n" );
komoritan 0:edaa24c1f5cd 141 snic_core_p->freeCmdBuf( payload_buf_p );
komoritan 0:edaa24c1f5cd 142 snic_core_p->unlockAPI();
komoritan 0:edaa24c1f5cd 143 return -1;
komoritan 0:edaa24c1f5cd 144 }
komoritan 0:edaa24c1f5cd 145
komoritan 0:edaa24c1f5cd 146 if( uartCmdMgr_p->getCommandStatus() != 0 )
komoritan 0:edaa24c1f5cd 147 {
komoritan 0:edaa24c1f5cd 148 DEBUG_PRINT("listen status:%02x\r\n", uartCmdMgr_p->getCommandStatus());
komoritan 0:edaa24c1f5cd 149 snic_core_p->freeCmdBuf( payload_buf_p );
komoritan 0:edaa24c1f5cd 150 snic_core_p->unlockAPI();
komoritan 0:edaa24c1f5cd 151 return -1;
komoritan 0:edaa24c1f5cd 152 }
komoritan 0:edaa24c1f5cd 153
komoritan 0:edaa24c1f5cd 154 snic_core_p->freeCmdBuf( payload_buf_p );
komoritan 0:edaa24c1f5cd 155 snic_core_p->unlockAPI();
komoritan 0:edaa24c1f5cd 156 return 0;
komoritan 0:edaa24c1f5cd 157 }
komoritan 0:edaa24c1f5cd 158
komoritan 0:edaa24c1f5cd 159 int TCPSocketServer::accept(TCPSocketConnection *connection)
komoritan 0:edaa24c1f5cd 160 {
komoritan 0:edaa24c1f5cd 161 C_SNIC_Core *snic_core_p = C_SNIC_Core::getInstance();
komoritan 0:edaa24c1f5cd 162 int i;
komoritan 0:edaa24c1f5cd 163 int ret = -1;
komoritan 0:edaa24c1f5cd 164
komoritan 0:edaa24c1f5cd 165 C_SNIC_Core::tagCONNECT_INFO_T *con_info_p;
komoritan 0:edaa24c1f5cd 166 for( i = 0; i < MAX_SOCKET_ID+1; i++ )
komoritan 0:edaa24c1f5cd 167 {
komoritan 0:edaa24c1f5cd 168 // Get connection information
komoritan 0:edaa24c1f5cd 169 con_info_p = snic_core_p->getConnectInfo( i );
komoritan 0:edaa24c1f5cd 170 if( (con_info_p->is_connected == true)
komoritan 0:edaa24c1f5cd 171 && (con_info_p->is_accept == true)
komoritan 0:edaa24c1f5cd 172 && (con_info_p->parent_socket == mSocketID) )
komoritan 0:edaa24c1f5cd 173 {
komoritan 0:edaa24c1f5cd 174 // Set socket id
komoritan 0:edaa24c1f5cd 175 connection->setAcceptSocket( i );
komoritan 0:edaa24c1f5cd 176 ret = 0;
komoritan 0:edaa24c1f5cd 177 }
komoritan 0:edaa24c1f5cd 178 }
komoritan 0:edaa24c1f5cd 179
komoritan 0:edaa24c1f5cd 180 return ret;
komoritan 0:edaa24c1f5cd 181 }