Customized
Dependents: MurataTypeYD_RPC_Sample
Fork of SNICInterface_mod by
Socket/TCPSocketServer.cpp@48:e3a26a484d1c, 2015-01-27 (annotated)
- Committer:
- MACRUM
- Date:
- Tue Jan 27 07:25:21 2015 +0000
- Revision:
- 48:e3a26a484d1c
- Parent:
- 43:d80bbb12ffe6
- Child:
- 49:5a5cae02bdf0
TCPSocketServer::accept is now blocking which is same behavior of original EthernetInterface library.
Who changed what in which revision?
User | Revision | Line number | New 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 | 39:a1233ca02edf | 18 | /* Copyright (C) 2014 Murata Manufacturing Co.,Ltd., MIT License |
kishino | 41:1c1b5ad4d491 | 19 | * port to the muRata, SWITCH SCIENCE Wi-FI module TypeYD SNIC-UART. |
kishino | 39:a1233ca02edf | 20 | */ |
kishino | 20:dd736d328de6 | 21 | #include "TCPSocketServer.h" |
kishino | 32:ae95309643aa | 22 | #include "SNIC_Core.h" |
kishino | 20:dd736d328de6 | 23 | |
kishino | 20:dd736d328de6 | 24 | #include <cstring> |
kishino | 20:dd736d328de6 | 25 | |
kishino | 20:dd736d328de6 | 26 | TCPSocketServer::TCPSocketServer() |
kishino | 20:dd736d328de6 | 27 | { |
kishino | 20:dd736d328de6 | 28 | } |
kishino | 20:dd736d328de6 | 29 | |
kishino | 26:f2e1030964e4 | 30 | TCPSocketServer::~TCPSocketServer() |
kishino | 26:f2e1030964e4 | 31 | { |
kishino | 26:f2e1030964e4 | 32 | } |
kishino | 26:f2e1030964e4 | 33 | |
kishino | 32:ae95309643aa | 34 | int TCPSocketServer::bind(unsigned short port) |
kishino | 20:dd736d328de6 | 35 | { |
kishino | 32:ae95309643aa | 36 | int ret; |
kishino | 32:ae95309643aa | 37 | C_SNIC_Core *snic_core_p = C_SNIC_Core::getInstance(); |
kishino | 32:ae95309643aa | 38 | C_SNIC_UartCommandManager *uartCmdMgr_p = snic_core_p->getUartCommand(); |
kishino | 32:ae95309643aa | 39 | |
kishino | 43:d80bbb12ffe6 | 40 | snic_core_p->lockAPI(); |
kishino | 32:ae95309643aa | 41 | // Get local ip address. |
kishino | 32:ae95309643aa | 42 | // Get buffer for response payload from MemoryPool |
kishino | 38:f13e4e563d65 | 43 | tagMEMPOOL_BLOCK_T *payload_buf_p = snic_core_p->allocCmdBuf(); |
kishino | 38:f13e4e563d65 | 44 | if( payload_buf_p == NULL ) |
kishino | 32:ae95309643aa | 45 | { |
kishino | 40:b6b10c22a121 | 46 | DEBUG_PRINT("bind payload_buf_p NULL\r\n"); |
kishino | 43:d80bbb12ffe6 | 47 | snic_core_p->unlockAPI(); |
kishino | 32:ae95309643aa | 48 | return -1; |
kishino | 32:ae95309643aa | 49 | } |
kishino | 32:ae95309643aa | 50 | |
kishino | 32:ae95309643aa | 51 | C_SNIC_Core::tagSNIC_GET_DHCP_INFO_REQ_T req; |
kishino | 32:ae95309643aa | 52 | // Make request |
kishino | 32:ae95309643aa | 53 | req.cmd_sid = UART_CMD_SID_SNIC_GET_DHCP_INFO_REQ; |
kishino | 32:ae95309643aa | 54 | req.seq = mUartRequestSeq++; |
kishino | 32:ae95309643aa | 55 | req.interface = 0; |
kishino | 32:ae95309643aa | 56 | |
kishino | 38:f13e4e563d65 | 57 | unsigned char *command_array_p = snic_core_p->getCommandBuf(); |
kishino | 32:ae95309643aa | 58 | unsigned int command_len; |
kishino | 32:ae95309643aa | 59 | // Preparation of command |
kishino | 32:ae95309643aa | 60 | command_len = snic_core_p->preparationSendCommand( UART_CMD_ID_SNIC, req.cmd_sid, (unsigned char *)&req |
kishino | 38:f13e4e563d65 | 61 | , sizeof(C_SNIC_Core::tagSNIC_GET_DHCP_INFO_REQ_T), payload_buf_p->buf, command_array_p ); |
kishino | 32:ae95309643aa | 62 | // Send uart command request |
kishino | 38:f13e4e563d65 | 63 | snic_core_p->sendUart( command_len, command_array_p ); |
kishino | 32:ae95309643aa | 64 | // Wait UART response |
kishino | 32:ae95309643aa | 65 | ret = uartCmdMgr_p->wait(); |
kishino | 32:ae95309643aa | 66 | if( ret != 0 ) |
kishino | 32:ae95309643aa | 67 | { |
kishino | 40:b6b10c22a121 | 68 | DEBUG_PRINT( "bind failed\r\n" ); |
kishino | 38:f13e4e563d65 | 69 | snic_core_p->freeCmdBuf( payload_buf_p ); |
kishino | 43:d80bbb12ffe6 | 70 | snic_core_p->unlockAPI(); |
kishino | 32:ae95309643aa | 71 | return -1; |
kishino | 32:ae95309643aa | 72 | } |
kishino | 32:ae95309643aa | 73 | |
kishino | 32:ae95309643aa | 74 | if( uartCmdMgr_p->getCommandStatus() != UART_CMD_RES_SNIC_SUCCESS ) |
kishino | 32:ae95309643aa | 75 | { |
kishino | 40:b6b10c22a121 | 76 | DEBUG_PRINT("bind status:%02x\r\n", uartCmdMgr_p->getCommandStatus()); |
kishino | 38:f13e4e563d65 | 77 | snic_core_p->freeCmdBuf( payload_buf_p ); |
kishino | 43:d80bbb12ffe6 | 78 | snic_core_p->unlockAPI(); |
kishino | 32:ae95309643aa | 79 | return -1; |
kishino | 32:ae95309643aa | 80 | } |
kishino | 32:ae95309643aa | 81 | |
kishino | 38:f13e4e563d65 | 82 | snic_core_p->freeCmdBuf( payload_buf_p ); |
kishino | 43:d80bbb12ffe6 | 83 | snic_core_p->unlockAPI(); |
kishino | 32:ae95309643aa | 84 | |
kishino | 38:f13e4e563d65 | 85 | unsigned int local_addr = (payload_buf_p->buf[9] << 24) |
kishino | 38:f13e4e563d65 | 86 | | (payload_buf_p->buf[10] << 16) |
kishino | 38:f13e4e563d65 | 87 | | (payload_buf_p->buf[11] << 8) |
kishino | 38:f13e4e563d65 | 88 | | (payload_buf_p->buf[12]); |
kishino | 32:ae95309643aa | 89 | |
kishino | 32:ae95309643aa | 90 | // Socket create |
kishino | 32:ae95309643aa | 91 | ret = createSocket( 1, local_addr, port ); |
kishino | 32:ae95309643aa | 92 | if( ret != 0 ) |
kishino | 32:ae95309643aa | 93 | { |
kishino | 40:b6b10c22a121 | 94 | DEBUG_PRINT("bind error : %d\r\n", ret); |
kishino | 32:ae95309643aa | 95 | return -1; |
kishino | 32:ae95309643aa | 96 | } |
kishino | 32:ae95309643aa | 97 | |
kishino | 20:dd736d328de6 | 98 | return 0; |
kishino | 20:dd736d328de6 | 99 | } |
kishino | 20:dd736d328de6 | 100 | |
kishino | 20:dd736d328de6 | 101 | int TCPSocketServer::listen(int max) |
kishino | 20:dd736d328de6 | 102 | { |
kishino | 32:ae95309643aa | 103 | C_SNIC_Core *snic_core_p = C_SNIC_Core::getInstance(); |
kishino | 32:ae95309643aa | 104 | C_SNIC_UartCommandManager *uartCmdMgr_p = snic_core_p->getUartCommand(); |
kishino | 32:ae95309643aa | 105 | |
kishino | 43:d80bbb12ffe6 | 106 | snic_core_p->lockAPI(); |
kishino | 32:ae95309643aa | 107 | // Get buffer for response payload from MemoryPool |
kishino | 38:f13e4e563d65 | 108 | tagMEMPOOL_BLOCK_T *payload_buf_p = snic_core_p->allocCmdBuf(); |
kishino | 38:f13e4e563d65 | 109 | if( payload_buf_p == NULL ) |
kishino | 32:ae95309643aa | 110 | { |
kishino | 40:b6b10c22a121 | 111 | DEBUG_PRINT("listen payload_buf_p NULL\r\n"); |
kishino | 43:d80bbb12ffe6 | 112 | snic_core_p->unlockAPI(); |
kishino | 32:ae95309643aa | 113 | return -1; |
kishino | 32:ae95309643aa | 114 | } |
kishino | 32:ae95309643aa | 115 | |
kishino | 32:ae95309643aa | 116 | C_SNIC_Core::tagSNIC_TCP_CREATE_CONNECTION_REQ_T req; |
kishino | 32:ae95309643aa | 117 | // Make request |
kishino | 32:ae95309643aa | 118 | req.cmd_sid = UART_CMD_SID_SNIC_TCP_CREATE_CONNECTION_REQ; |
kishino | 32:ae95309643aa | 119 | req.seq = mUartRequestSeq++; |
kishino | 32:ae95309643aa | 120 | req.socket_id = mSocketID; |
kishino | 32:ae95309643aa | 121 | req.recv_bufsize[0] = ( (SNIC_UART_RECVBUF_SIZE & 0xFF00) >> 8 ); |
kishino | 32:ae95309643aa | 122 | req.recv_bufsize[1] = (SNIC_UART_RECVBUF_SIZE & 0xFF); |
kishino | 32:ae95309643aa | 123 | req.max_client = max; |
kishino | 32:ae95309643aa | 124 | |
kishino | 38:f13e4e563d65 | 125 | unsigned char *command_array_p = snic_core_p->getCommandBuf(); |
kishino | 32:ae95309643aa | 126 | unsigned int command_len; |
kishino | 32:ae95309643aa | 127 | // Preparation of command |
kishino | 32:ae95309643aa | 128 | command_len = snic_core_p->preparationSendCommand( UART_CMD_ID_SNIC, req.cmd_sid, (unsigned char *)&req |
kishino | 38:f13e4e563d65 | 129 | , sizeof(C_SNIC_Core::tagSNIC_TCP_CREATE_CONNECTION_REQ_T), payload_buf_p->buf, command_array_p ); |
kishino | 32:ae95309643aa | 130 | |
kishino | 32:ae95309643aa | 131 | int ret; |
kishino | 32:ae95309643aa | 132 | |
kishino | 32:ae95309643aa | 133 | // Send uart command request |
kishino | 38:f13e4e563d65 | 134 | snic_core_p->sendUart( command_len, command_array_p ); |
kishino | 32:ae95309643aa | 135 | |
kishino | 32:ae95309643aa | 136 | // Wait UART response |
kishino | 32:ae95309643aa | 137 | ret = uartCmdMgr_p->wait(); |
kishino | 32:ae95309643aa | 138 | if( ret != 0 ) |
kishino | 32:ae95309643aa | 139 | { |
kishino | 40:b6b10c22a121 | 140 | DEBUG_PRINT( "listen failed\r\n" ); |
kishino | 38:f13e4e563d65 | 141 | snic_core_p->freeCmdBuf( payload_buf_p ); |
kishino | 43:d80bbb12ffe6 | 142 | snic_core_p->unlockAPI(); |
kishino | 32:ae95309643aa | 143 | return -1; |
kishino | 32:ae95309643aa | 144 | } |
kishino | 32:ae95309643aa | 145 | |
kishino | 32:ae95309643aa | 146 | if( uartCmdMgr_p->getCommandStatus() != 0 ) |
kishino | 32:ae95309643aa | 147 | { |
kishino | 40:b6b10c22a121 | 148 | DEBUG_PRINT("listen status:%02x\r\n", uartCmdMgr_p->getCommandStatus()); |
kishino | 38:f13e4e563d65 | 149 | snic_core_p->freeCmdBuf( payload_buf_p ); |
kishino | 43:d80bbb12ffe6 | 150 | snic_core_p->unlockAPI(); |
kishino | 32:ae95309643aa | 151 | return -1; |
kishino | 32:ae95309643aa | 152 | } |
kishino | 32:ae95309643aa | 153 | |
kishino | 38:f13e4e563d65 | 154 | snic_core_p->freeCmdBuf( payload_buf_p ); |
kishino | 43:d80bbb12ffe6 | 155 | snic_core_p->unlockAPI(); |
kishino | 20:dd736d328de6 | 156 | return 0; |
kishino | 20:dd736d328de6 | 157 | } |
kishino | 20:dd736d328de6 | 158 | |
kishino | 32:ae95309643aa | 159 | int TCPSocketServer::accept(TCPSocketConnection *connection) |
kishino | 20:dd736d328de6 | 160 | { |
kishino | 32:ae95309643aa | 161 | C_SNIC_Core *snic_core_p = C_SNIC_Core::getInstance(); |
kishino | 32:ae95309643aa | 162 | int i; |
kishino | 32:ae95309643aa | 163 | int ret = -1; |
MACRUM | 48:e3a26a484d1c | 164 | bool accepted = false; |
kishino | 32:ae95309643aa | 165 | |
kishino | 32:ae95309643aa | 166 | C_SNIC_Core::tagCONNECT_INFO_T *con_info_p; |
MACRUM | 48:e3a26a484d1c | 167 | |
MACRUM | 48:e3a26a484d1c | 168 | do |
MACRUM | 48:e3a26a484d1c | 169 | { |
MACRUM | 48:e3a26a484d1c | 170 | for( i = 0; i < MAX_SOCKET_ID+1; i++ ) |
kishino | 32:ae95309643aa | 171 | { |
MACRUM | 48:e3a26a484d1c | 172 | // Get connection information |
MACRUM | 48:e3a26a484d1c | 173 | con_info_p = snic_core_p->getConnectInfo( i ); |
MACRUM | 48:e3a26a484d1c | 174 | if( (con_info_p->is_connected == true) |
MACRUM | 48:e3a26a484d1c | 175 | && (con_info_p->is_accept == true) |
MACRUM | 48:e3a26a484d1c | 176 | && (con_info_p->parent_socket == mSocketID) ) |
MACRUM | 48:e3a26a484d1c | 177 | { |
MACRUM | 48:e3a26a484d1c | 178 | // Set socket id |
MACRUM | 48:e3a26a484d1c | 179 | connection->setAcceptSocket( i ); |
MACRUM | 48:e3a26a484d1c | 180 | ret = 0; |
MACRUM | 48:e3a26a484d1c | 181 | accepted = true; |
MACRUM | 48:e3a26a484d1c | 182 | break; |
MACRUM | 48:e3a26a484d1c | 183 | } |
MACRUM | 48:e3a26a484d1c | 184 | } |
MACRUM | 48:e3a26a484d1c | 185 | }while( accepted == false ); |
MACRUM | 48:e3a26a484d1c | 186 | con_info_p->is_accept = false; |
MACRUM | 48:e3a26a484d1c | 187 | |
MACRUM | 48:e3a26a484d1c | 188 | char remote_addr[ 20 ] = {'\0'}; |
MACRUM | 48:e3a26a484d1c | 189 | sprintf( remote_addr, "%d.%d.%d.%d" |
MACRUM | 48:e3a26a484d1c | 190 | , (con_info_p->from_ip >> 24 ) & 0xff |
MACRUM | 48:e3a26a484d1c | 191 | , (con_info_p->from_ip >> 16 ) & 0xff |
MACRUM | 48:e3a26a484d1c | 192 | , (con_info_p->from_ip >> 8 ) & 0xff |
MACRUM | 48:e3a26a484d1c | 193 | , con_info_p->from_ip & 0xff ); |
MACRUM | 48:e3a26a484d1c | 194 | |
MACRUM | 48:e3a26a484d1c | 195 | connection->set_address( remote_addr, (int)con_info_p->from_port ); |
kishino | 32:ae95309643aa | 196 | |
kishino | 32:ae95309643aa | 197 | return ret; |
kishino | 20:dd736d328de6 | 198 | } |