customized SNICInterface

Dependents:   MurataTypeYD_Ping

Fork of NySNICInterface by Ryo Iizuka

Committer:
kishino
Date:
Thu Jun 19 10:15:47 2014 +0000
Revision:
36:f33fcf5975ab
Parent:
33:33f1bc919486
Child:
38:f13e4e563d65
Supported a new command format of SNIC UART.

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