SNIC UART Interface library: Serial to Wi-Fi library for Murata TypeYD Wi-Fi module. For more information about TypeYD: http://www.murata.co.jp/products/microwave/module/lbwb1zzydz/index.html

Dependents:   SNIC-xively-jumpstart-demo SNIC-FluentLogger-example TCPEchoServer murataDemo ... more

Fork of YDwifiInterface by Takao Kishino

Committer:
kishino
Date:
Wed Jun 25 00:04:11 2014 +0000
Revision:
38:f13e4e563d65
Parent:
36:f33fcf5975ab
Child:
39:a1233ca02edf
Fixed a variable name of pointer type.

Who changed what in which revision?

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