SNICInterface for development

Fork of SNICInterface by muRata

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?

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 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 "Socket/UDPSocket.h"
kishino 20:dd736d328de6 22 #include <cstring>
kishino 20:dd736d328de6 23
kishino 20:dd736d328de6 24 UDPSocket::UDPSocket() {
kishino 20:dd736d328de6 25 }
kishino 20:dd736d328de6 26
kishino 26:f2e1030964e4 27 UDPSocket::~UDPSocket()
kishino 26:f2e1030964e4 28 {
kishino 26:f2e1030964e4 29 }
kishino 26:f2e1030964e4 30
kishino 20:dd736d328de6 31 int UDPSocket::init(void)
kishino 20:dd736d328de6 32 {
kishino 20:dd736d328de6 33 return 0;
kishino 20:dd736d328de6 34 }
kishino 20:dd736d328de6 35
kishino 20:dd736d328de6 36 // Server initialization
kishino 33:33f1bc919486 37 int UDPSocket::bind(short port)
kishino 20:dd736d328de6 38 {
kishino 33:33f1bc919486 39 int ret;
kishino 33:33f1bc919486 40 C_SNIC_Core *snic_core_p = C_SNIC_Core::getInstance();
kishino 33:33f1bc919486 41 C_SNIC_UartCommandManager *uartCmdMgr_p = snic_core_p->getUartCommand();
kishino 33:33f1bc919486 42
kishino 33:33f1bc919486 43 // Get local ip address.
kishino 33:33f1bc919486 44 // Get buffer for response payload from MemoryPool
kishino 38:f13e4e563d65 45 tagMEMPOOL_BLOCK_T *payload_buf_p = snic_core_p->allocCmdBuf();
kishino 38:f13e4e563d65 46 if( payload_buf_p == NULL )
kishino 33:33f1bc919486 47 {
kishino 38:f13e4e563d65 48 printf("UDP bind payload_buf_p NULL\r\n");
kishino 33:33f1bc919486 49 return -1;
kishino 33:33f1bc919486 50 }
kishino 33:33f1bc919486 51
kishino 33:33f1bc919486 52 C_SNIC_Core::tagSNIC_GET_DHCP_INFO_REQ_T req;
kishino 33:33f1bc919486 53 // Make request
kishino 33:33f1bc919486 54 req.cmd_sid = UART_CMD_SID_SNIC_GET_DHCP_INFO_REQ;
kishino 33:33f1bc919486 55 req.seq = mUartRequestSeq++;
kishino 33:33f1bc919486 56 req.interface = 0;
kishino 33:33f1bc919486 57
kishino 38:f13e4e563d65 58 unsigned char *command_array_p = snic_core_p->getCommandBuf();
kishino 33:33f1bc919486 59 unsigned int command_len;
kishino 33:33f1bc919486 60 // Preparation of command
kishino 33:33f1bc919486 61 command_len = snic_core_p->preparationSendCommand( UART_CMD_ID_SNIC, req.cmd_sid, (unsigned char *)&req
kishino 38:f13e4e563d65 62 , sizeof(C_SNIC_Core::tagSNIC_GET_DHCP_INFO_REQ_T), payload_buf_p->buf, command_array_p );
kishino 33:33f1bc919486 63 // Send uart command request
kishino 38:f13e4e563d65 64 snic_core_p->sendUart( command_len, command_array_p );
kishino 33:33f1bc919486 65 // Wait UART response
kishino 33:33f1bc919486 66 ret = uartCmdMgr_p->wait();
kishino 33:33f1bc919486 67 if( ret != 0 )
kishino 33:33f1bc919486 68 {
kishino 33:33f1bc919486 69 printf( "UDP bind failed\r\n" );
kishino 38:f13e4e563d65 70 snic_core_p->freeCmdBuf( payload_buf_p );
kishino 33:33f1bc919486 71 return -1;
kishino 33:33f1bc919486 72 }
kishino 33:33f1bc919486 73
kishino 33:33f1bc919486 74 if( uartCmdMgr_p->getCommandStatus() != UART_CMD_RES_SNIC_SUCCESS )
kishino 33:33f1bc919486 75 {
kishino 33:33f1bc919486 76 printf("UDP bind status:%02x\r\n", uartCmdMgr_p->getCommandStatus());
kishino 38:f13e4e563d65 77 snic_core_p->freeCmdBuf( payload_buf_p );
kishino 33:33f1bc919486 78 return -1;
kishino 33:33f1bc919486 79 }
kishino 33:33f1bc919486 80
kishino 38:f13e4e563d65 81 unsigned int local_addr = (payload_buf_p->buf[9] << 24)
kishino 38:f13e4e563d65 82 | (payload_buf_p->buf[10] << 16)
kishino 38:f13e4e563d65 83 | (payload_buf_p->buf[11] << 8)
kishino 38:f13e4e563d65 84 | (payload_buf_p->buf[12]);
kishino 33:33f1bc919486 85
kishino 33:33f1bc919486 86
kishino 33:33f1bc919486 87 C_SNIC_Core::tagSNIC_UDP_CREATE_SOCKET_REQ_T create_req;
kishino 33:33f1bc919486 88
kishino 33:33f1bc919486 89 // Make request
kishino 33:33f1bc919486 90 create_req.cmd_sid = UART_CMD_SID_SNIC_UDP_CREATE_SOCKET_REQ;
kishino 33:33f1bc919486 91 create_req.seq = mUartRequestSeq++;
kishino 33:33f1bc919486 92 create_req.bind = 1;
kishino 33:33f1bc919486 93 // set ip addr ( byte order )
kishino 33:33f1bc919486 94 C_SNIC_UartMsgUtil::convertIntToByteAdday( local_addr, (char *)create_req.local_addr );
kishino 33:33f1bc919486 95 create_req.local_port[0] = ( (port & 0xFF00) >> 8 );
kishino 33:33f1bc919486 96 create_req.local_port[1] = (port & 0xFF);
kishino 33:33f1bc919486 97
kishino 33:33f1bc919486 98 // Preparation of command
kishino 33:33f1bc919486 99 command_len = snic_core_p->preparationSendCommand( UART_CMD_ID_SNIC, create_req.cmd_sid, (unsigned char *)&create_req
kishino 38:f13e4e563d65 100 , sizeof(C_SNIC_Core::tagSNIC_UDP_CREATE_SOCKET_REQ_T), payload_buf_p->buf, command_array_p );
kishino 33:33f1bc919486 101 // Send uart command request
kishino 38:f13e4e563d65 102 snic_core_p->sendUart( command_len, command_array_p );
kishino 33:33f1bc919486 103
kishino 33:33f1bc919486 104 // Wait UART response
kishino 33:33f1bc919486 105 ret = uartCmdMgr_p->wait();
kishino 33:33f1bc919486 106 if( ret != 0 )
kishino 33:33f1bc919486 107 {
kishino 33:33f1bc919486 108 printf( "UDP bind failed\r\n" );
kishino 38:f13e4e563d65 109 snic_core_p->freeCmdBuf( payload_buf_p );
kishino 33:33f1bc919486 110 return -1;
kishino 33:33f1bc919486 111 }
kishino 33:33f1bc919486 112
kishino 33:33f1bc919486 113 if( uartCmdMgr_p->getCommandStatus() != 0 )
kishino 33:33f1bc919486 114 {
kishino 33:33f1bc919486 115 printf("UDP bind status:%02x\r\n", uartCmdMgr_p->getCommandStatus());
kishino 38:f13e4e563d65 116 snic_core_p->freeCmdBuf( payload_buf_p );
kishino 33:33f1bc919486 117 return -1;
kishino 33:33f1bc919486 118 }
kishino 38:f13e4e563d65 119 mSocketID = payload_buf_p->buf[3];
kishino 33:33f1bc919486 120
kishino 33:33f1bc919486 121 C_SNIC_Core::tagSNIC_UDP_START_RECV_REQ_T recv_start_req;
kishino 33:33f1bc919486 122
kishino 33:33f1bc919486 123 // Make request
kishino 33:33f1bc919486 124 recv_start_req.cmd_sid = UART_CMD_SID_SNIC_UDP_START_RECV_REQ;
kishino 33:33f1bc919486 125 recv_start_req.seq = mUartRequestSeq++;
kishino 33:33f1bc919486 126 recv_start_req.socket_id = mSocketID;
kishino 33:33f1bc919486 127 recv_start_req.recv_bufsize[0] = ( (SNIC_UART_RECVBUF_SIZE & 0xFF00) >> 8 );
kishino 33:33f1bc919486 128 recv_start_req.recv_bufsize[1] = (SNIC_UART_RECVBUF_SIZE & 0xFF);
kishino 33:33f1bc919486 129
kishino 33:33f1bc919486 130 // Preparation of command
kishino 33:33f1bc919486 131 command_len = snic_core_p->preparationSendCommand( UART_CMD_ID_SNIC, recv_start_req.cmd_sid, (unsigned char *)&recv_start_req
kishino 38:f13e4e563d65 132 , sizeof(C_SNIC_Core::tagSNIC_UDP_START_RECV_REQ_T), payload_buf_p->buf, command_array_p );
kishino 33:33f1bc919486 133 // Send uart command request
kishino 38:f13e4e563d65 134 snic_core_p->sendUart( command_len, command_array_p );
kishino 33:33f1bc919486 135
kishino 33:33f1bc919486 136 // Wait UART response
kishino 33:33f1bc919486 137 ret = uartCmdMgr_p->wait();
kishino 33:33f1bc919486 138 if( ret != 0 )
kishino 33:33f1bc919486 139 {
kishino 33:33f1bc919486 140 printf( "UDP recv start failed\r\n" );
kishino 38:f13e4e563d65 141 snic_core_p->freeCmdBuf( payload_buf_p );
kishino 33:33f1bc919486 142 return -1;
kishino 33:33f1bc919486 143 }
kishino 33:33f1bc919486 144
kishino 33:33f1bc919486 145 if( uartCmdMgr_p->getCommandStatus() != 0 )
kishino 33:33f1bc919486 146 {
kishino 33:33f1bc919486 147 printf("UDP recv start status:%02x\r\n", uartCmdMgr_p->getCommandStatus());
kishino 38:f13e4e563d65 148 snic_core_p->freeCmdBuf( payload_buf_p );
kishino 33:33f1bc919486 149 return -1;
kishino 33:33f1bc919486 150 }
kishino 33:33f1bc919486 151
kishino 38:f13e4e563d65 152 snic_core_p->freeCmdBuf( payload_buf_p );
kishino 33:33f1bc919486 153
kishino 20:dd736d328de6 154 return 0;
kishino 20:dd736d328de6 155 }
kishino 20:dd736d328de6 156
kishino 20:dd736d328de6 157 // -1 if unsuccessful, else number of bytes written
kishino 20:dd736d328de6 158 int UDPSocket::sendTo(Endpoint &remote, char *packet, int length)
kishino 20:dd736d328de6 159 {
kishino 33:33f1bc919486 160 C_SNIC_Core *snic_core_p = C_SNIC_Core::getInstance();
kishino 33:33f1bc919486 161 C_SNIC_UartCommandManager *uartCmdMgr_p = snic_core_p->getUartCommand();
kishino 33:33f1bc919486 162
kishino 33:33f1bc919486 163 // Get buffer for response payload from MemoryPool
kishino 38:f13e4e563d65 164 tagMEMPOOL_BLOCK_T *payload_buf_p = snic_core_p->allocCmdBuf();
kishino 38:f13e4e563d65 165 if( payload_buf_p == NULL )
kishino 33:33f1bc919486 166 {
kishino 38:f13e4e563d65 167 printf("connect payload_buf_p NULL\r\n");
kishino 33:33f1bc919486 168 return -1;
kishino 33:33f1bc919486 169 }
kishino 33:33f1bc919486 170
kishino 33:33f1bc919486 171 C_SNIC_Core::tagSNIC_UDP_SIMPLE_SEND_REQ_T req;
kishino 33:33f1bc919486 172 // Make request
kishino 33:33f1bc919486 173 req.cmd_sid = UART_CMD_SID_SNIC_UDP_SIMPLE_SEND_REQ;
kishino 33:33f1bc919486 174 req.seq = mUartRequestSeq++;
kishino 33:33f1bc919486 175
kishino 33:33f1bc919486 176 int addr_temp;
kishino 33:33f1bc919486 177 addr_temp = C_SNIC_UartMsgUtil::addrToInteger( remote.get_address() );
kishino 33:33f1bc919486 178 C_SNIC_UartMsgUtil::convertIntToByteAdday( addr_temp, (char *)req.remote_ip );
kishino 33:33f1bc919486 179 req.remote_port[0]= ( (remote.get_port() & 0xFF00) >> 8 );
kishino 33:33f1bc919486 180 req.remote_port[1]= (remote.get_port() & 0xFF);
kishino 33:33f1bc919486 181 req.payload_len[0]= ( (length & 0xFF00) >> 8 );
kishino 33:33f1bc919486 182 req.payload_len[1]= (length & 0xFF);
kishino 33:33f1bc919486 183
kishino 33:33f1bc919486 184 int req_size = sizeof(C_SNIC_Core::tagSNIC_UDP_SIMPLE_SEND_REQ_T);
kishino 33:33f1bc919486 185
kishino 33:33f1bc919486 186 char *send_buf_p = getSocketSendBuf();
kishino 33:33f1bc919486 187 memcpy( send_buf_p, &req, req_size );
kishino 33:33f1bc919486 188 memcpy( &send_buf_p[req_size], packet, length );
kishino 33:33f1bc919486 189
kishino 38:f13e4e563d65 190 unsigned char *command_array_p = snic_core_p->getCommandBuf();
kishino 33:33f1bc919486 191 unsigned int command_len;
kishino 33:33f1bc919486 192 // Preparation of command
kishino 33:33f1bc919486 193 command_len = snic_core_p->preparationSendCommand( UART_CMD_ID_SNIC, req.cmd_sid, (unsigned char *)send_buf_p
kishino 38:f13e4e563d65 194 , req_size + length, payload_buf_p->buf, command_array_p );
kishino 33:33f1bc919486 195
kishino 33:33f1bc919486 196 // Send uart command request
kishino 38:f13e4e563d65 197 snic_core_p->sendUart( command_len, command_array_p );
kishino 33:33f1bc919486 198
kishino 33:33f1bc919486 199 // Wait UART response
kishino 33:33f1bc919486 200 int ret = uartCmdMgr_p->wait();
kishino 33:33f1bc919486 201 if( ret != 0 )
kishino 33:33f1bc919486 202 {
kishino 33:33f1bc919486 203 printf( "send failed\r\n" );
kishino 38:f13e4e563d65 204 snic_core_p->freeCmdBuf( payload_buf_p );
kishino 33:33f1bc919486 205 return -1;
kishino 33:33f1bc919486 206 }
kishino 33:33f1bc919486 207
kishino 33:33f1bc919486 208 if( uartCmdMgr_p->getCommandStatus() != UART_CMD_RES_SNIC_SUCCESS )
kishino 33:33f1bc919486 209 {
kishino 33:33f1bc919486 210 printf("send status:%02x\r\n", uartCmdMgr_p->getCommandStatus());
kishino 38:f13e4e563d65 211 snic_core_p->freeCmdBuf( payload_buf_p );
kishino 33:33f1bc919486 212 return -1;
kishino 33:33f1bc919486 213 }
kishino 38:f13e4e563d65 214 snic_core_p->freeCmdBuf( payload_buf_p );
kishino 33:33f1bc919486 215
kishino 33:33f1bc919486 216 // SNIC_SEND_FROM_SOCKET_REQ
kishino 20:dd736d328de6 217 return 0;
kishino 20:dd736d328de6 218 }
kishino 20:dd736d328de6 219
kishino 20:dd736d328de6 220 // -1 if unsuccessful, else number of bytes received
kishino 33:33f1bc919486 221 int UDPSocket::receiveFrom(Endpoint &remote, char *data_p, int length)
kishino 20:dd736d328de6 222 {
kishino 33:33f1bc919486 223 if( (data_p == NULL) || (length < 1) )
kishino 33:33f1bc919486 224 {
kishino 33:33f1bc919486 225 printf("UDPSocket::receiveFrom parameter error\r\n");
kishino 33:33f1bc919486 226 return -1;
kishino 33:33f1bc919486 227 }
kishino 33:33f1bc919486 228
kishino 33:33f1bc919486 229 C_SNIC_Core *snic_core_p = C_SNIC_Core::getInstance();
kishino 33:33f1bc919486 230 // Initialize connection information
kishino 33:33f1bc919486 231 C_SNIC_Core::tagUDP_RECVINFO_T *con_info_p = snic_core_p->getUdpRecvInfo( mSocketID );
kishino 33:33f1bc919486 232 if( con_info_p->recvbuf_p == NULL )
kishino 33:33f1bc919486 233 {
kishino 33:33f1bc919486 234 // printf("UDPSocket::receiveFrom Conncection info error\r\n");
kishino 33:33f1bc919486 235 return 0;
kishino 33:33f1bc919486 236 }
kishino 33:33f1bc919486 237 if( con_info_p->is_received == false )
kishino 33:33f1bc919486 238 {
kishino 33:33f1bc919486 239 return 0;
kishino 33:33f1bc919486 240 }
kishino 33:33f1bc919486 241
kishino 33:33f1bc919486 242 char remote_ip[20] = {'\0'};
kishino 33:33f1bc919486 243 sprintf( remote_ip, "%d.%d.%d.%d"
kishino 33:33f1bc919486 244 , (con_info_p->from_ip >>24) & 0x000000ff
kishino 33:33f1bc919486 245 , (con_info_p->from_ip >>16) & 0x000000ff
kishino 33:33f1bc919486 246 , (con_info_p->from_ip >>8) & 0x000000ff
kishino 33:33f1bc919486 247 , (con_info_p->from_ip) & 0x000000ff );
kishino 33:33f1bc919486 248 remote.set_address( remote_ip, con_info_p->from_port );
kishino 33:33f1bc919486 249
kishino 33:33f1bc919486 250 int i;
kishino 33:33f1bc919486 251 // Get packet data from buffer for receive.
kishino 33:33f1bc919486 252 for (i = 0; i < length; i ++)
kishino 33:33f1bc919486 253 {
kishino 33:33f1bc919486 254 if (con_info_p->recvbuf_p->dequeue(&data_p[i]) == false)
kishino 33:33f1bc919486 255 {
kishino 33:33f1bc919486 256 break;
kishino 33:33f1bc919486 257 }
kishino 33:33f1bc919486 258 }
kishino 33:33f1bc919486 259
kishino 33:33f1bc919486 260 if( con_info_p->recvbuf_p->isEmpty() )
kishino 33:33f1bc919486 261 {
kishino 39:a1233ca02edf 262 con_info_p->mutex.lock();
kishino 33:33f1bc919486 263 con_info_p->is_received = false;
kishino 39:a1233ca02edf 264 con_info_p->mutex.unlock();
kishino 33:33f1bc919486 265 }
kishino 33:33f1bc919486 266
kishino 33:33f1bc919486 267 return i;
kishino 20:dd736d328de6 268 }