SNIC UART Interface library for Murata Type-YD module
Dependents: WebSocketServerTest
Fork of SNICInterface_mod by
Socket/Socket.cpp@27:dcc4f34448f0, 2014-03-31 (annotated)
- Committer:
- kishino
- Date:
- Mon Mar 31 02:06:14 2014 +0000
- Revision:
- 27:dcc4f34448f0
- Parent:
- 24:987e412ae879
- Child:
- 29:6a0ba999597d
Created a SNIC_CLOSE_SOCKET_REQ command.
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 | 20:dd736d328de6 | 18 | /******************* Murata Manufacturing Co.,Ltd. 2014 ***************** |
kishino | 20:dd736d328de6 | 19 | * |
kishino | 20:dd736d328de6 | 20 | * Filename: Socket.cpp |
kishino | 20:dd736d328de6 | 21 | * |
kishino | 20:dd736d328de6 | 22 | * Purpose: This module has implementation of socket. |
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 "Socket.h" |
kishino | 20:dd736d328de6 | 31 | #include <cstring> |
kishino | 20:dd736d328de6 | 32 | |
kishino | 20:dd736d328de6 | 33 | using namespace murata_wifi; |
kishino | 20:dd736d328de6 | 34 | |
kishino | 20:dd736d328de6 | 35 | Socket::Socket() |
kishino | 20:dd736d328de6 | 36 | { |
kishino | 20:dd736d328de6 | 37 | mSocketID = -1; |
kishino | 20:dd736d328de6 | 38 | } |
kishino | 20:dd736d328de6 | 39 | |
kishino | 20:dd736d328de6 | 40 | Socket::~Socket() { |
kishino | 20:dd736d328de6 | 41 | // close(); //Don't want to leak |
kishino | 20:dd736d328de6 | 42 | } |
kishino | 20:dd736d328de6 | 43 | |
kishino | 20:dd736d328de6 | 44 | int Socket::set_option(int level, int optname, const void *optval, socklen_t optlen) { |
kishino | 20:dd736d328de6 | 45 | return 0; |
kishino | 20:dd736d328de6 | 46 | } |
kishino | 20:dd736d328de6 | 47 | |
kishino | 20:dd736d328de6 | 48 | int Socket::get_option(int level, int optname, void *optval, socklen_t *optlen) { |
kishino | 20:dd736d328de6 | 49 | return 0; |
kishino | 20:dd736d328de6 | 50 | } |
kishino | 20:dd736d328de6 | 51 | |
kishino | 20:dd736d328de6 | 52 | int Socket::close(bool shutdown) |
kishino | 20:dd736d328de6 | 53 | { |
kishino | 27:dcc4f34448f0 | 54 | C_SNIC_Core *snic_core_p = C_SNIC_Core::getInstance(); |
kishino | 27:dcc4f34448f0 | 55 | C_SNIC_UartCommandManager *uartCmdMgr_p = snic_core_p->getUartCommand(); |
kishino | 27:dcc4f34448f0 | 56 | // Get buffer for response payload from MemoryPool |
kishino | 27:dcc4f34448f0 | 57 | C_SNIC_Core::tagMEMPOOL_BLOCK_T *payload_buf = snic_core_p->allocCmdBuf(); |
kishino | 27:dcc4f34448f0 | 58 | if( payload_buf == NULL ) |
kishino | 27:dcc4f34448f0 | 59 | { |
kishino | 27:dcc4f34448f0 | 60 | printf("socket close payload_buf NULL\r\n"); |
kishino | 27:dcc4f34448f0 | 61 | return -1; |
kishino | 27:dcc4f34448f0 | 62 | } |
kishino | 27:dcc4f34448f0 | 63 | |
kishino | 27:dcc4f34448f0 | 64 | C_SNIC_Core::tagSNIC_CLOSE_SOCKET_REQ_T req; |
kishino | 20:dd736d328de6 | 65 | |
kishino | 27:dcc4f34448f0 | 66 | // Make request |
kishino | 27:dcc4f34448f0 | 67 | req.cmd_sid = UART_CMD_SID_SNIC_CLOSE_SOCKET_REQ; |
kishino | 27:dcc4f34448f0 | 68 | req.seq = mUartRequestSeq++; |
kishino | 27:dcc4f34448f0 | 69 | req.socket_id = mSocketID; |
kishino | 27:dcc4f34448f0 | 70 | |
kishino | 27:dcc4f34448f0 | 71 | unsigned char command_array[UART_REQUEST_PAYLOAD_MAX]; |
kishino | 27:dcc4f34448f0 | 72 | unsigned int command_len; |
kishino | 27:dcc4f34448f0 | 73 | // Preparation of command |
kishino | 27:dcc4f34448f0 | 74 | command_len = snic_core_p->preparationSendCommand( UART_CMD_ID_SNIC, req.cmd_sid, (unsigned char *)&req |
kishino | 27:dcc4f34448f0 | 75 | , sizeof(C_SNIC_Core::tagSNIC_CLOSE_SOCKET_REQ_T), payload_buf->buf, command_array ); |
kishino | 27:dcc4f34448f0 | 76 | |
kishino | 27:dcc4f34448f0 | 77 | // Send uart command request |
kishino | 27:dcc4f34448f0 | 78 | snic_core_p->sendUart( command_len, command_array ); |
kishino | 27:dcc4f34448f0 | 79 | |
kishino | 27:dcc4f34448f0 | 80 | int ret; |
kishino | 27:dcc4f34448f0 | 81 | // Wait UART response |
kishino | 27:dcc4f34448f0 | 82 | ret = uartCmdMgr_p->wait(); |
kishino | 27:dcc4f34448f0 | 83 | if( ret != 0 ) |
kishino | 27:dcc4f34448f0 | 84 | { |
kishino | 27:dcc4f34448f0 | 85 | printf( "socket close failed\r\n" ); |
kishino | 27:dcc4f34448f0 | 86 | snic_core_p->freeCmdBuf( payload_buf ); |
kishino | 27:dcc4f34448f0 | 87 | return -1; |
kishino | 27:dcc4f34448f0 | 88 | } |
kishino | 27:dcc4f34448f0 | 89 | |
kishino | 27:dcc4f34448f0 | 90 | if( uartCmdMgr_p->getCommandStatus() != 0 ) |
kishino | 27:dcc4f34448f0 | 91 | { |
kishino | 27:dcc4f34448f0 | 92 | printf("socket close status:%02x\r\n", uartCmdMgr_p->getCommandStatus()); |
kishino | 27:dcc4f34448f0 | 93 | snic_core_p->freeCmdBuf( payload_buf ); |
kishino | 27:dcc4f34448f0 | 94 | return -1; |
kishino | 27:dcc4f34448f0 | 95 | } |
kishino | 27:dcc4f34448f0 | 96 | snic_core_p->freeCmdBuf( payload_buf ); |
kishino | 27:dcc4f34448f0 | 97 | |
kishino | 20:dd736d328de6 | 98 | return 0; |
kishino | 20:dd736d328de6 | 99 | } |
kishino | 20:dd736d328de6 | 100 | |
kishino | 20:dd736d328de6 | 101 | #if 0 |
kishino | 20:dd736d328de6 | 102 | int Socket::select(struct timeval *timeout, bool read, bool write) |
kishino | 20:dd736d328de6 | 103 | { |
kishino | 20:dd736d328de6 | 104 | return 0; |
kishino | 20:dd736d328de6 | 105 | } |
kishino | 20:dd736d328de6 | 106 | #endif |
kishino | 20:dd736d328de6 | 107 | |
kishino | 20:dd736d328de6 | 108 | int Socket::createSocket( unsigned char bind, unsigned int local_addr, unsigned short port ) |
kishino | 20:dd736d328de6 | 109 | { |
kishino | 24:987e412ae879 | 110 | C_SNIC_Core *snic_core_p = C_SNIC_Core::getInstance(); |
kishino | 24:987e412ae879 | 111 | C_SNIC_UartCommandManager *uartCmdMgr_p = snic_core_p->getUartCommand(); |
kishino | 20:dd736d328de6 | 112 | // Get buffer for response payload from MemoryPool |
kishino | 24:987e412ae879 | 113 | C_SNIC_Core::tagMEMPOOL_BLOCK_T *payload_buf = snic_core_p->allocCmdBuf(); |
kishino | 20:dd736d328de6 | 114 | if( payload_buf == NULL ) |
kishino | 20:dd736d328de6 | 115 | { |
kishino | 20:dd736d328de6 | 116 | printf("createSocket payload_buf NULL\r\n"); |
kishino | 20:dd736d328de6 | 117 | return -1; |
kishino | 20:dd736d328de6 | 118 | } |
kishino | 20:dd736d328de6 | 119 | |
kishino | 22:a9ec0cad4f84 | 120 | C_SNIC_Core::tagSNIC_TCP_CREATE_SOCKET_REQ_T req; |
kishino | 20:dd736d328de6 | 121 | int req_len = 0; |
kishino | 20:dd736d328de6 | 122 | |
kishino | 20:dd736d328de6 | 123 | // Make request |
kishino | 20:dd736d328de6 | 124 | req.cmd_sid = UART_CMD_SID_SNIC_TCP_CREATE_SOCKET_REQ; |
kishino | 20:dd736d328de6 | 125 | req_len++; |
kishino | 20:dd736d328de6 | 126 | req.seq = mUartRequestSeq++; |
kishino | 20:dd736d328de6 | 127 | req_len++; |
kishino | 20:dd736d328de6 | 128 | req.bind = bind; |
kishino | 20:dd736d328de6 | 129 | req_len++; |
kishino | 20:dd736d328de6 | 130 | if( bind != 0 ) |
kishino | 20:dd736d328de6 | 131 | { |
kishino | 20:dd736d328de6 | 132 | /* |
kishino | 20:dd736d328de6 | 133 | req.local_addr = local_addr; |
kishino | 20:dd736d328de6 | 134 | req_len++; |
kishino | 20:dd736d328de6 | 135 | req.local_port = port; |
kishino | 20:dd736d328de6 | 136 | req_len++; |
kishino | 20:dd736d328de6 | 137 | */ |
kishino | 20:dd736d328de6 | 138 | } |
kishino | 20:dd736d328de6 | 139 | |
kishino | 20:dd736d328de6 | 140 | unsigned char command_array[UART_REQUEST_PAYLOAD_MAX]; |
kishino | 20:dd736d328de6 | 141 | unsigned int command_len; |
kishino | 20:dd736d328de6 | 142 | // Preparation of command |
kishino | 24:987e412ae879 | 143 | command_len = snic_core_p->preparationSendCommand( UART_CMD_ID_SNIC, req.cmd_sid, (unsigned char *)&req |
kishino | 20:dd736d328de6 | 144 | , req_len, payload_buf->buf, command_array ); |
kishino | 20:dd736d328de6 | 145 | |
kishino | 20:dd736d328de6 | 146 | // Send uart command request |
kishino | 24:987e412ae879 | 147 | snic_core_p->sendUart( command_len, command_array ); |
kishino | 20:dd736d328de6 | 148 | |
kishino | 20:dd736d328de6 | 149 | int ret; |
kishino | 20:dd736d328de6 | 150 | // Wait UART response |
kishino | 20:dd736d328de6 | 151 | ret = uartCmdMgr_p->wait(); |
kishino | 20:dd736d328de6 | 152 | if( ret != 0 ) |
kishino | 20:dd736d328de6 | 153 | { |
kishino | 20:dd736d328de6 | 154 | printf( "createSocket failed\r\n" ); |
kishino | 24:987e412ae879 | 155 | snic_core_p->freeCmdBuf( payload_buf ); |
kishino | 20:dd736d328de6 | 156 | return -1; |
kishino | 20:dd736d328de6 | 157 | } |
kishino | 20:dd736d328de6 | 158 | |
kishino | 20:dd736d328de6 | 159 | if( uartCmdMgr_p->getCommandStatus() != 0 ) |
kishino | 20:dd736d328de6 | 160 | { |
kishino | 20:dd736d328de6 | 161 | printf("createSocket status:%02x\r\n", uartCmdMgr_p->getCommandStatus()); |
kishino | 24:987e412ae879 | 162 | snic_core_p->freeCmdBuf( payload_buf ); |
kishino | 20:dd736d328de6 | 163 | return -1; |
kishino | 20:dd736d328de6 | 164 | } |
kishino | 20:dd736d328de6 | 165 | mSocketID = payload_buf->buf[3]; |
kishino | 24:987e412ae879 | 166 | snic_core_p->freeCmdBuf( payload_buf ); |
kishino | 20:dd736d328de6 | 167 | |
kishino | 20:dd736d328de6 | 168 | return 0; |
kishino | 20:dd736d328de6 | 169 | } |
kishino | 27:dcc4f34448f0 | 170 | |
kishino | 27:dcc4f34448f0 | 171 | int Socket::addrToInteger( const char *addr_p ) |
kishino | 27:dcc4f34448f0 | 172 | { |
kishino | 27:dcc4f34448f0 | 173 | if( addr_p == NULL ) |
kishino | 27:dcc4f34448f0 | 174 | { |
kishino | 27:dcc4f34448f0 | 175 | printf("addrToInteger parameter error\r\n"); |
kishino | 27:dcc4f34448f0 | 176 | return 0; |
kishino | 27:dcc4f34448f0 | 177 | } |
kishino | 27:dcc4f34448f0 | 178 | |
kishino | 27:dcc4f34448f0 | 179 | unsigned char ipadr[4]; |
kishino | 27:dcc4f34448f0 | 180 | unsigned char temp= 0; |
kishino | 27:dcc4f34448f0 | 181 | int i,j,k; |
kishino | 27:dcc4f34448f0 | 182 | |
kishino | 27:dcc4f34448f0 | 183 | /* convert to char[4] */ |
kishino | 27:dcc4f34448f0 | 184 | k=0; |
kishino | 27:dcc4f34448f0 | 185 | for(i=0; i<4; i ++) |
kishino | 27:dcc4f34448f0 | 186 | { |
kishino | 27:dcc4f34448f0 | 187 | for(j=0; j<4; j ++) |
kishino | 27:dcc4f34448f0 | 188 | { |
kishino | 27:dcc4f34448f0 | 189 | if((addr_p[k] > 0x2F)&&(addr_p[k] < 0x3A)) |
kishino | 27:dcc4f34448f0 | 190 | { |
kishino | 27:dcc4f34448f0 | 191 | temp = (temp * 10) + addr_p[k]-0x30; |
kishino | 27:dcc4f34448f0 | 192 | } |
kishino | 27:dcc4f34448f0 | 193 | else if((addr_p[k] == 0x20)&&(temp == 0)) |
kishino | 27:dcc4f34448f0 | 194 | { |
kishino | 27:dcc4f34448f0 | 195 | } |
kishino | 27:dcc4f34448f0 | 196 | else |
kishino | 27:dcc4f34448f0 | 197 | { |
kishino | 27:dcc4f34448f0 | 198 | ipadr[i]=temp; |
kishino | 27:dcc4f34448f0 | 199 | temp = 0; |
kishino | 27:dcc4f34448f0 | 200 | k++; |
kishino | 27:dcc4f34448f0 | 201 | break; |
kishino | 27:dcc4f34448f0 | 202 | } |
kishino | 27:dcc4f34448f0 | 203 | k++; |
kishino | 27:dcc4f34448f0 | 204 | } |
kishino | 27:dcc4f34448f0 | 205 | } |
kishino | 27:dcc4f34448f0 | 206 | |
kishino | 27:dcc4f34448f0 | 207 | int addr = ( (ipadr[0]<<24) | (ipadr[1]<<16) | (ipadr[2]<<8) | ipadr[3] ); |
kishino | 27:dcc4f34448f0 | 208 | |
kishino | 27:dcc4f34448f0 | 209 | return addr; |
kishino | 27:dcc4f34448f0 | 210 | } |