SNIC UART Interface library for Murata Type-YD module
Dependents: WebSocketServerTest
Fork of SNICInterface_mod by
Socket/TCPSocketServer.cpp@39:a1233ca02edf, 2014-07-15 (annotated)
- Committer:
- kishino
- Date:
- Tue Jul 15 02:08:42 2014 +0000
- Revision:
- 39:a1233ca02edf
- Parent:
- 38:f13e4e563d65
- Child:
- 40:b6b10c22a121
The pointing out of in-house reviews was modified.
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 | 39:a1233ca02edf | 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 | 32:ae95309643aa | 40 | // Get local ip address. |
kishino | 32:ae95309643aa | 41 | // Get buffer for response payload from MemoryPool |
kishino | 38:f13e4e563d65 | 42 | tagMEMPOOL_BLOCK_T *payload_buf_p = snic_core_p->allocCmdBuf(); |
kishino | 38:f13e4e563d65 | 43 | if( payload_buf_p == NULL ) |
kishino | 32:ae95309643aa | 44 | { |
kishino | 38:f13e4e563d65 | 45 | printf("bind payload_buf_p NULL\r\n"); |
kishino | 32:ae95309643aa | 46 | return -1; |
kishino | 32:ae95309643aa | 47 | } |
kishino | 32:ae95309643aa | 48 | |
kishino | 32:ae95309643aa | 49 | C_SNIC_Core::tagSNIC_GET_DHCP_INFO_REQ_T req; |
kishino | 32:ae95309643aa | 50 | // Make request |
kishino | 32:ae95309643aa | 51 | req.cmd_sid = UART_CMD_SID_SNIC_GET_DHCP_INFO_REQ; |
kishino | 32:ae95309643aa | 52 | req.seq = mUartRequestSeq++; |
kishino | 32:ae95309643aa | 53 | req.interface = 0; |
kishino | 32:ae95309643aa | 54 | |
kishino | 38:f13e4e563d65 | 55 | unsigned char *command_array_p = snic_core_p->getCommandBuf(); |
kishino | 32:ae95309643aa | 56 | unsigned int command_len; |
kishino | 32:ae95309643aa | 57 | // Preparation of command |
kishino | 32:ae95309643aa | 58 | command_len = snic_core_p->preparationSendCommand( UART_CMD_ID_SNIC, req.cmd_sid, (unsigned char *)&req |
kishino | 38:f13e4e563d65 | 59 | , sizeof(C_SNIC_Core::tagSNIC_GET_DHCP_INFO_REQ_T), payload_buf_p->buf, command_array_p ); |
kishino | 32:ae95309643aa | 60 | // Send uart command request |
kishino | 38:f13e4e563d65 | 61 | snic_core_p->sendUart( command_len, command_array_p ); |
kishino | 32:ae95309643aa | 62 | // Wait UART response |
kishino | 32:ae95309643aa | 63 | ret = uartCmdMgr_p->wait(); |
kishino | 32:ae95309643aa | 64 | if( ret != 0 ) |
kishino | 32:ae95309643aa | 65 | { |
kishino | 32:ae95309643aa | 66 | printf( "bind failed\r\n" ); |
kishino | 38:f13e4e563d65 | 67 | snic_core_p->freeCmdBuf( payload_buf_p ); |
kishino | 32:ae95309643aa | 68 | return -1; |
kishino | 32:ae95309643aa | 69 | } |
kishino | 32:ae95309643aa | 70 | |
kishino | 32:ae95309643aa | 71 | if( uartCmdMgr_p->getCommandStatus() != UART_CMD_RES_SNIC_SUCCESS ) |
kishino | 32:ae95309643aa | 72 | { |
kishino | 32:ae95309643aa | 73 | printf("bind status:%02x\r\n", uartCmdMgr_p->getCommandStatus()); |
kishino | 38:f13e4e563d65 | 74 | snic_core_p->freeCmdBuf( payload_buf_p ); |
kishino | 32:ae95309643aa | 75 | return -1; |
kishino | 32:ae95309643aa | 76 | } |
kishino | 32:ae95309643aa | 77 | |
kishino | 38:f13e4e563d65 | 78 | snic_core_p->freeCmdBuf( payload_buf_p ); |
kishino | 32:ae95309643aa | 79 | |
kishino | 38:f13e4e563d65 | 80 | unsigned int local_addr = (payload_buf_p->buf[9] << 24) |
kishino | 38:f13e4e563d65 | 81 | | (payload_buf_p->buf[10] << 16) |
kishino | 38:f13e4e563d65 | 82 | | (payload_buf_p->buf[11] << 8) |
kishino | 38:f13e4e563d65 | 83 | | (payload_buf_p->buf[12]); |
kishino | 32:ae95309643aa | 84 | |
kishino | 32:ae95309643aa | 85 | // Socket create |
kishino | 32:ae95309643aa | 86 | ret = createSocket( 1, local_addr, port ); |
kishino | 32:ae95309643aa | 87 | if( ret != 0 ) |
kishino | 32:ae95309643aa | 88 | { |
kishino | 32:ae95309643aa | 89 | printf("bind error : %d\r\n", ret); |
kishino | 32:ae95309643aa | 90 | return -1; |
kishino | 32:ae95309643aa | 91 | } |
kishino | 32:ae95309643aa | 92 | |
kishino | 20:dd736d328de6 | 93 | return 0; |
kishino | 20:dd736d328de6 | 94 | } |
kishino | 20:dd736d328de6 | 95 | |
kishino | 20:dd736d328de6 | 96 | int TCPSocketServer::listen(int max) |
kishino | 20:dd736d328de6 | 97 | { |
kishino | 32:ae95309643aa | 98 | C_SNIC_Core *snic_core_p = C_SNIC_Core::getInstance(); |
kishino | 32:ae95309643aa | 99 | C_SNIC_UartCommandManager *uartCmdMgr_p = snic_core_p->getUartCommand(); |
kishino | 32:ae95309643aa | 100 | |
kishino | 32:ae95309643aa | 101 | // Get buffer for response payload from MemoryPool |
kishino | 38:f13e4e563d65 | 102 | tagMEMPOOL_BLOCK_T *payload_buf_p = snic_core_p->allocCmdBuf(); |
kishino | 38:f13e4e563d65 | 103 | if( payload_buf_p == NULL ) |
kishino | 32:ae95309643aa | 104 | { |
kishino | 38:f13e4e563d65 | 105 | printf("listen payload_buf_p NULL\r\n"); |
kishino | 32:ae95309643aa | 106 | return -1; |
kishino | 32:ae95309643aa | 107 | } |
kishino | 32:ae95309643aa | 108 | |
kishino | 32:ae95309643aa | 109 | C_SNIC_Core::tagSNIC_TCP_CREATE_CONNECTION_REQ_T req; |
kishino | 32:ae95309643aa | 110 | // Make request |
kishino | 32:ae95309643aa | 111 | req.cmd_sid = UART_CMD_SID_SNIC_TCP_CREATE_CONNECTION_REQ; |
kishino | 32:ae95309643aa | 112 | req.seq = mUartRequestSeq++; |
kishino | 32:ae95309643aa | 113 | req.socket_id = mSocketID; |
kishino | 32:ae95309643aa | 114 | req.recv_bufsize[0] = ( (SNIC_UART_RECVBUF_SIZE & 0xFF00) >> 8 ); |
kishino | 32:ae95309643aa | 115 | req.recv_bufsize[1] = (SNIC_UART_RECVBUF_SIZE & 0xFF); |
kishino | 32:ae95309643aa | 116 | req.max_client = max; |
kishino | 32:ae95309643aa | 117 | |
kishino | 38:f13e4e563d65 | 118 | unsigned char *command_array_p = snic_core_p->getCommandBuf(); |
kishino | 32:ae95309643aa | 119 | unsigned int command_len; |
kishino | 32:ae95309643aa | 120 | // Preparation of command |
kishino | 32:ae95309643aa | 121 | command_len = snic_core_p->preparationSendCommand( UART_CMD_ID_SNIC, req.cmd_sid, (unsigned char *)&req |
kishino | 38:f13e4e563d65 | 122 | , sizeof(C_SNIC_Core::tagSNIC_TCP_CREATE_CONNECTION_REQ_T), payload_buf_p->buf, command_array_p ); |
kishino | 32:ae95309643aa | 123 | |
kishino | 32:ae95309643aa | 124 | int ret; |
kishino | 32:ae95309643aa | 125 | |
kishino | 32:ae95309643aa | 126 | // Send uart command request |
kishino | 38:f13e4e563d65 | 127 | snic_core_p->sendUart( command_len, command_array_p ); |
kishino | 32:ae95309643aa | 128 | |
kishino | 32:ae95309643aa | 129 | // Wait UART response |
kishino | 32:ae95309643aa | 130 | ret = uartCmdMgr_p->wait(); |
kishino | 32:ae95309643aa | 131 | if( ret != 0 ) |
kishino | 32:ae95309643aa | 132 | { |
kishino | 32:ae95309643aa | 133 | printf( "listen failed\r\n" ); |
kishino | 38:f13e4e563d65 | 134 | snic_core_p->freeCmdBuf( payload_buf_p ); |
kishino | 32:ae95309643aa | 135 | return -1; |
kishino | 32:ae95309643aa | 136 | } |
kishino | 32:ae95309643aa | 137 | |
kishino | 32:ae95309643aa | 138 | if( uartCmdMgr_p->getCommandStatus() != 0 ) |
kishino | 32:ae95309643aa | 139 | { |
kishino | 32:ae95309643aa | 140 | printf("listen status:%02x\r\n", uartCmdMgr_p->getCommandStatus()); |
kishino | 38:f13e4e563d65 | 141 | snic_core_p->freeCmdBuf( payload_buf_p ); |
kishino | 32:ae95309643aa | 142 | return -1; |
kishino | 32:ae95309643aa | 143 | } |
kishino | 32:ae95309643aa | 144 | |
kishino | 38:f13e4e563d65 | 145 | snic_core_p->freeCmdBuf( payload_buf_p ); |
kishino | 20:dd736d328de6 | 146 | return 0; |
kishino | 20:dd736d328de6 | 147 | } |
kishino | 20:dd736d328de6 | 148 | |
kishino | 32:ae95309643aa | 149 | int TCPSocketServer::accept(TCPSocketConnection *connection) |
kishino | 20:dd736d328de6 | 150 | { |
kishino | 32:ae95309643aa | 151 | C_SNIC_Core *snic_core_p = C_SNIC_Core::getInstance(); |
kishino | 32:ae95309643aa | 152 | int i; |
kishino | 32:ae95309643aa | 153 | int ret = -1; |
kishino | 32:ae95309643aa | 154 | |
kishino | 32:ae95309643aa | 155 | C_SNIC_Core::tagCONNECT_INFO_T *con_info_p; |
kishino | 32:ae95309643aa | 156 | for( i = 0; i < MAX_SOCKET_ID+1; i++ ) |
kishino | 32:ae95309643aa | 157 | { |
kishino | 32:ae95309643aa | 158 | // Get connection information |
kishino | 32:ae95309643aa | 159 | con_info_p = snic_core_p->getConnectInfo( i ); |
kishino | 32:ae95309643aa | 160 | if( (con_info_p->is_connected == true) |
kishino | 32:ae95309643aa | 161 | && (con_info_p->is_accept == true) |
kishino | 32:ae95309643aa | 162 | && (con_info_p->parent_socket == mSocketID) ) |
kishino | 32:ae95309643aa | 163 | { |
kishino | 32:ae95309643aa | 164 | // Set socket id |
kishino | 32:ae95309643aa | 165 | connection->setAcceptSocket( i ); |
kishino | 32:ae95309643aa | 166 | ret = 0; |
kishino | 32:ae95309643aa | 167 | } |
kishino | 32:ae95309643aa | 168 | } |
kishino | 32:ae95309643aa | 169 | |
kishino | 32:ae95309643aa | 170 | return ret; |
kishino | 20:dd736d328de6 | 171 | } |