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