KDDI Fx0 hackathon / SNICInterface_Tweet
Committer:
komoritan
Date:
Tue Feb 10 12:26:31 2015 +0000
Revision:
0:edaa24c1f5cd
Debug Code

Who changed what in which revision?

UserRevisionLine numberNew contents of line
komoritan 0:edaa24c1f5cd 1 /* Copyright (C) 2012 mbed.org, MIT License
komoritan 0:edaa24c1f5cd 2 *
komoritan 0:edaa24c1f5cd 3 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
komoritan 0:edaa24c1f5cd 4 * and associated documentation files (the "Software"), to deal in the Software without restriction,
komoritan 0:edaa24c1f5cd 5 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
komoritan 0:edaa24c1f5cd 6 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
komoritan 0:edaa24c1f5cd 7 * furnished to do so, subject to the following conditions:
komoritan 0:edaa24c1f5cd 8 *
komoritan 0:edaa24c1f5cd 9 * The above copyright notice and this permission notice shall be included in all copies or
komoritan 0:edaa24c1f5cd 10 * substantial portions of the Software.
komoritan 0:edaa24c1f5cd 11 *
komoritan 0:edaa24c1f5cd 12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
komoritan 0:edaa24c1f5cd 13 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
komoritan 0:edaa24c1f5cd 14 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
komoritan 0:edaa24c1f5cd 15 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
komoritan 0:edaa24c1f5cd 16 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
komoritan 0:edaa24c1f5cd 17 */
komoritan 0:edaa24c1f5cd 18 /* Copyright (C) 2014 Murata Manufacturing Co.,Ltd., MIT License
komoritan 0:edaa24c1f5cd 19 * port to the muRata, SWITCH SCIENCE Wi-FI module TypeYD SNIC-UART.
komoritan 0:edaa24c1f5cd 20 */
komoritan 0:edaa24c1f5cd 21 #include "Socket/UDPSocket.h"
komoritan 0:edaa24c1f5cd 22 #include <cstring>
komoritan 0:edaa24c1f5cd 23
komoritan 0:edaa24c1f5cd 24 UDPSocket::UDPSocket() {
komoritan 0:edaa24c1f5cd 25 }
komoritan 0:edaa24c1f5cd 26
komoritan 0:edaa24c1f5cd 27 UDPSocket::~UDPSocket()
komoritan 0:edaa24c1f5cd 28 {
komoritan 0:edaa24c1f5cd 29 }
komoritan 0:edaa24c1f5cd 30
komoritan 0:edaa24c1f5cd 31 int UDPSocket::init(void)
komoritan 0:edaa24c1f5cd 32 {
komoritan 0:edaa24c1f5cd 33 return 0;
komoritan 0:edaa24c1f5cd 34 }
komoritan 0:edaa24c1f5cd 35
komoritan 0:edaa24c1f5cd 36 // Server initialization
komoritan 0:edaa24c1f5cd 37 int UDPSocket::bind(short port)
komoritan 0:edaa24c1f5cd 38 {
komoritan 0:edaa24c1f5cd 39 int ret;
komoritan 0:edaa24c1f5cd 40 C_SNIC_Core *snic_core_p = C_SNIC_Core::getInstance();
komoritan 0:edaa24c1f5cd 41 C_SNIC_UartCommandManager *uartCmdMgr_p = snic_core_p->getUartCommand();
komoritan 0:edaa24c1f5cd 42
komoritan 0:edaa24c1f5cd 43 FUNC_IN();
komoritan 0:edaa24c1f5cd 44 // Get local ip address.
komoritan 0:edaa24c1f5cd 45 // Get buffer for response payload from MemoryPool
komoritan 0:edaa24c1f5cd 46 tagMEMPOOL_BLOCK_T *payload_buf_p = snic_core_p->allocCmdBuf();
komoritan 0:edaa24c1f5cd 47 if( payload_buf_p == NULL )
komoritan 0:edaa24c1f5cd 48 {
komoritan 0:edaa24c1f5cd 49 DEBUG_PRINT("UDP bind payload_buf_p NULL\r\n");
komoritan 0:edaa24c1f5cd 50 FUNC_OUT();
komoritan 0:edaa24c1f5cd 51 return -1;
komoritan 0:edaa24c1f5cd 52 }
komoritan 0:edaa24c1f5cd 53
komoritan 0:edaa24c1f5cd 54 C_SNIC_Core::tagSNIC_GET_DHCP_INFO_REQ_T req;
komoritan 0:edaa24c1f5cd 55 // Make request
komoritan 0:edaa24c1f5cd 56 req.cmd_sid = UART_CMD_SID_SNIC_GET_DHCP_INFO_REQ;
komoritan 0:edaa24c1f5cd 57 req.seq = mUartRequestSeq++;
komoritan 0:edaa24c1f5cd 58 req.interface = 0;
komoritan 0:edaa24c1f5cd 59
komoritan 0:edaa24c1f5cd 60 unsigned char *command_array_p = snic_core_p->getCommandBuf();
komoritan 0:edaa24c1f5cd 61 unsigned int command_len;
komoritan 0:edaa24c1f5cd 62 // Preparation of command
komoritan 0:edaa24c1f5cd 63 command_len = snic_core_p->preparationSendCommand( UART_CMD_ID_SNIC, req.cmd_sid, (unsigned char *)&req
komoritan 0:edaa24c1f5cd 64 , sizeof(C_SNIC_Core::tagSNIC_GET_DHCP_INFO_REQ_T), payload_buf_p->buf, command_array_p );
komoritan 0:edaa24c1f5cd 65 // Send uart command request
komoritan 0:edaa24c1f5cd 66 snic_core_p->sendUart( command_len, command_array_p );
komoritan 0:edaa24c1f5cd 67 // Wait UART response
komoritan 0:edaa24c1f5cd 68 ret = uartCmdMgr_p->wait();
komoritan 0:edaa24c1f5cd 69 if( ret != 0 )
komoritan 0:edaa24c1f5cd 70 {
komoritan 0:edaa24c1f5cd 71 DEBUG_PRINT( "UDP bind failed\r\n" );
komoritan 0:edaa24c1f5cd 72 snic_core_p->freeCmdBuf( payload_buf_p );
komoritan 0:edaa24c1f5cd 73 FUNC_OUT();
komoritan 0:edaa24c1f5cd 74 return -1;
komoritan 0:edaa24c1f5cd 75 }
komoritan 0:edaa24c1f5cd 76
komoritan 0:edaa24c1f5cd 77 if( uartCmdMgr_p->getCommandStatus() != UART_CMD_RES_SNIC_SUCCESS )
komoritan 0:edaa24c1f5cd 78 {
komoritan 0:edaa24c1f5cd 79 DEBUG_PRINT("UDP bind status:%02x\r\n", uartCmdMgr_p->getCommandStatus());
komoritan 0:edaa24c1f5cd 80 snic_core_p->freeCmdBuf( payload_buf_p );
komoritan 0:edaa24c1f5cd 81 FUNC_OUT();
komoritan 0:edaa24c1f5cd 82 return -1;
komoritan 0:edaa24c1f5cd 83 }
komoritan 0:edaa24c1f5cd 84
komoritan 0:edaa24c1f5cd 85 unsigned int local_addr = (payload_buf_p->buf[9] << 24)
komoritan 0:edaa24c1f5cd 86 | (payload_buf_p->buf[10] << 16)
komoritan 0:edaa24c1f5cd 87 | (payload_buf_p->buf[11] << 8)
komoritan 0:edaa24c1f5cd 88 | (payload_buf_p->buf[12]);
komoritan 0:edaa24c1f5cd 89
komoritan 0:edaa24c1f5cd 90
komoritan 0:edaa24c1f5cd 91 C_SNIC_Core::tagSNIC_UDP_CREATE_SOCKET_REQ_T create_req;
komoritan 0:edaa24c1f5cd 92
komoritan 0:edaa24c1f5cd 93 // Make request
komoritan 0:edaa24c1f5cd 94 create_req.cmd_sid = UART_CMD_SID_SNIC_UDP_CREATE_SOCKET_REQ;
komoritan 0:edaa24c1f5cd 95 create_req.seq = mUartRequestSeq++;
komoritan 0:edaa24c1f5cd 96 create_req.bind = 1;
komoritan 0:edaa24c1f5cd 97 // set ip addr ( byte order )
komoritan 0:edaa24c1f5cd 98 C_SNIC_UartMsgUtil::convertIntToByteAdday( local_addr, (char *)create_req.local_addr );
komoritan 0:edaa24c1f5cd 99 create_req.local_port[0] = ( (port & 0xFF00) >> 8 );
komoritan 0:edaa24c1f5cd 100 create_req.local_port[1] = (port & 0xFF);
komoritan 0:edaa24c1f5cd 101
komoritan 0:edaa24c1f5cd 102 // Preparation of command
komoritan 0:edaa24c1f5cd 103 command_len = snic_core_p->preparationSendCommand( UART_CMD_ID_SNIC, create_req.cmd_sid, (unsigned char *)&create_req
komoritan 0:edaa24c1f5cd 104 , sizeof(C_SNIC_Core::tagSNIC_UDP_CREATE_SOCKET_REQ_T), payload_buf_p->buf, command_array_p );
komoritan 0:edaa24c1f5cd 105 // Send uart command request
komoritan 0:edaa24c1f5cd 106 snic_core_p->sendUart( command_len, command_array_p );
komoritan 0:edaa24c1f5cd 107
komoritan 0:edaa24c1f5cd 108 // Wait UART response
komoritan 0:edaa24c1f5cd 109 ret = uartCmdMgr_p->wait();
komoritan 0:edaa24c1f5cd 110 if( ret != 0 )
komoritan 0:edaa24c1f5cd 111 {
komoritan 0:edaa24c1f5cd 112 DEBUG_PRINT( "UDP bind failed\r\n" );
komoritan 0:edaa24c1f5cd 113 snic_core_p->freeCmdBuf( payload_buf_p );
komoritan 0:edaa24c1f5cd 114 FUNC_OUT();
komoritan 0:edaa24c1f5cd 115 return -1;
komoritan 0:edaa24c1f5cd 116 }
komoritan 0:edaa24c1f5cd 117
komoritan 0:edaa24c1f5cd 118 if( uartCmdMgr_p->getCommandStatus() != 0 )
komoritan 0:edaa24c1f5cd 119 {
komoritan 0:edaa24c1f5cd 120 DEBUG_PRINT("UDP bind status:%02x\r\n", uartCmdMgr_p->getCommandStatus());
komoritan 0:edaa24c1f5cd 121 snic_core_p->freeCmdBuf( payload_buf_p );
komoritan 0:edaa24c1f5cd 122 FUNC_OUT();
komoritan 0:edaa24c1f5cd 123 return -1;
komoritan 0:edaa24c1f5cd 124 }
komoritan 0:edaa24c1f5cd 125 mSocketID = payload_buf_p->buf[3];
komoritan 0:edaa24c1f5cd 126
komoritan 0:edaa24c1f5cd 127 // Initialize connection information
komoritan 0:edaa24c1f5cd 128 C_SNIC_Core::tagCONNECT_INFO_T *con_info_p = snic_core_p->getConnectInfo( mSocketID );
komoritan 0:edaa24c1f5cd 129 if( con_info_p->recvbuf_p == NULL )
komoritan 0:edaa24c1f5cd 130 {
komoritan 0:edaa24c1f5cd 131 DEBUG_PRINT( "create recv buffer[socket:%d]\r\n", mSocketID);
komoritan 0:edaa24c1f5cd 132 con_info_p->recvbuf_p = new CircBuffer<char>(SNIC_UART_RECVBUF_SIZE);
komoritan 0:edaa24c1f5cd 133 }
komoritan 0:edaa24c1f5cd 134 con_info_p->is_connected = true;
komoritan 0:edaa24c1f5cd 135 con_info_p->is_received = false;
komoritan 0:edaa24c1f5cd 136
komoritan 0:edaa24c1f5cd 137 C_SNIC_Core::tagSNIC_UDP_START_RECV_REQ_T recv_start_req;
komoritan 0:edaa24c1f5cd 138
komoritan 0:edaa24c1f5cd 139 // Make request
komoritan 0:edaa24c1f5cd 140 recv_start_req.cmd_sid = UART_CMD_SID_SNIC_UDP_START_RECV_REQ;
komoritan 0:edaa24c1f5cd 141 recv_start_req.seq = mUartRequestSeq++;
komoritan 0:edaa24c1f5cd 142 recv_start_req.socket_id = mSocketID;
komoritan 0:edaa24c1f5cd 143 recv_start_req.recv_bufsize[0] = ( (SNIC_UART_RECVBUF_SIZE & 0xFF00) >> 8 );
komoritan 0:edaa24c1f5cd 144 recv_start_req.recv_bufsize[1] = (SNIC_UART_RECVBUF_SIZE & 0xFF);
komoritan 0:edaa24c1f5cd 145
komoritan 0:edaa24c1f5cd 146 // Preparation of command
komoritan 0:edaa24c1f5cd 147 command_len = snic_core_p->preparationSendCommand( UART_CMD_ID_SNIC, recv_start_req.cmd_sid, (unsigned char *)&recv_start_req
komoritan 0:edaa24c1f5cd 148 , sizeof(C_SNIC_Core::tagSNIC_UDP_START_RECV_REQ_T), payload_buf_p->buf, command_array_p );
komoritan 0:edaa24c1f5cd 149 // Send uart command request
komoritan 0:edaa24c1f5cd 150 snic_core_p->sendUart( command_len, command_array_p );
komoritan 0:edaa24c1f5cd 151
komoritan 0:edaa24c1f5cd 152 // Wait UART response
komoritan 0:edaa24c1f5cd 153 ret = uartCmdMgr_p->wait();
komoritan 0:edaa24c1f5cd 154 if( ret != 0 )
komoritan 0:edaa24c1f5cd 155 {
komoritan 0:edaa24c1f5cd 156 DEBUG_PRINT( "UDP recv start failed\r\n" );
komoritan 0:edaa24c1f5cd 157 snic_core_p->freeCmdBuf( payload_buf_p );
komoritan 0:edaa24c1f5cd 158 FUNC_OUT();
komoritan 0:edaa24c1f5cd 159 return -1;
komoritan 0:edaa24c1f5cd 160 }
komoritan 0:edaa24c1f5cd 161
komoritan 0:edaa24c1f5cd 162 if( uartCmdMgr_p->getCommandStatus() != 0 )
komoritan 0:edaa24c1f5cd 163 {
komoritan 0:edaa24c1f5cd 164 DEBUG_PRINT("UDP recv start status:%02x\r\n", uartCmdMgr_p->getCommandStatus());
komoritan 0:edaa24c1f5cd 165 snic_core_p->freeCmdBuf( payload_buf_p );
komoritan 0:edaa24c1f5cd 166 FUNC_OUT();
komoritan 0:edaa24c1f5cd 167 return -1;
komoritan 0:edaa24c1f5cd 168 }
komoritan 0:edaa24c1f5cd 169
komoritan 0:edaa24c1f5cd 170 snic_core_p->freeCmdBuf( payload_buf_p );
komoritan 0:edaa24c1f5cd 171 FUNC_OUT();
komoritan 0:edaa24c1f5cd 172 return 0;
komoritan 0:edaa24c1f5cd 173 }
komoritan 0:edaa24c1f5cd 174
komoritan 0:edaa24c1f5cd 175 // -1 if unsuccessful, else number of bytes written
komoritan 0:edaa24c1f5cd 176 int UDPSocket::sendTo(Endpoint &remote, char *packet, int length)
komoritan 0:edaa24c1f5cd 177 {
komoritan 0:edaa24c1f5cd 178 C_SNIC_Core *snic_core_p = C_SNIC_Core::getInstance();
komoritan 0:edaa24c1f5cd 179 C_SNIC_UartCommandManager *uartCmdMgr_p = snic_core_p->getUartCommand();
komoritan 0:edaa24c1f5cd 180
komoritan 0:edaa24c1f5cd 181 osThreadId tid = Thread::gettid();
komoritan 0:edaa24c1f5cd 182
komoritan 0:edaa24c1f5cd 183 // pc.printf("send[%08x] len:%d(%04x)\r\n", tid, length, length);
komoritan 0:edaa24c1f5cd 184
komoritan 0:edaa24c1f5cd 185 #if 0 // TODO: Not wait for command response(Tentative)
komoritan 0:edaa24c1f5cd 186 snic_core_p->lockAPI();
komoritan 0:edaa24c1f5cd 187 #endif
komoritan 0:edaa24c1f5cd 188 FUNC_IN();
komoritan 0:edaa24c1f5cd 189
komoritan 0:edaa24c1f5cd 190 #if 0 // TODO: Not wait for command response(Tentative)
komoritan 0:edaa24c1f5cd 191 // Get buffer for response payload from MemoryPool
komoritan 0:edaa24c1f5cd 192 tagMEMPOOL_BLOCK_T *payload_buf_p = snic_core_p->allocCmdBuf();
komoritan 0:edaa24c1f5cd 193 if( payload_buf_p == NULL )
komoritan 0:edaa24c1f5cd 194 {
komoritan 0:edaa24c1f5cd 195 DEBUG_PRINT("connect payload_buf_p NULL\r\n");
komoritan 0:edaa24c1f5cd 196 FUNC_OUT();
komoritan 0:edaa24c1f5cd 197 snic_core_p->unlockAPI();
komoritan 0:edaa24c1f5cd 198 return -1;
komoritan 0:edaa24c1f5cd 199 }
komoritan 0:edaa24c1f5cd 200 #endif
komoritan 0:edaa24c1f5cd 201
komoritan 0:edaa24c1f5cd 202 C_SNIC_Core::tagSNIC_UDP_SEND_FROM_SOCKET_REQ_T req;
komoritan 0:edaa24c1f5cd 203 // Make request
komoritan 0:edaa24c1f5cd 204 req.cmd_sid = UART_CMD_SID_SNIC_UDP_SEND_FROM_SOCKET_REQ;
komoritan 0:edaa24c1f5cd 205 req.seq = mUartRequestSeq++;
komoritan 0:edaa24c1f5cd 206
komoritan 0:edaa24c1f5cd 207 int addr_temp;
komoritan 0:edaa24c1f5cd 208 addr_temp = C_SNIC_UartMsgUtil::addrToInteger( remote.get_address() );
komoritan 0:edaa24c1f5cd 209 C_SNIC_UartMsgUtil::convertIntToByteAdday( addr_temp, (char *)req.remote_ip );
komoritan 0:edaa24c1f5cd 210 req.remote_port[0] = ( (remote.get_port() & 0xFF00) >> 8 );
komoritan 0:edaa24c1f5cd 211 req.remote_port[1] = (remote.get_port() & 0xFF);
komoritan 0:edaa24c1f5cd 212 req.payload_len[0] = ( (length & 0xFF00) >> 8 );
komoritan 0:edaa24c1f5cd 213 req.payload_len[1] = (length & 0xFF);
komoritan 0:edaa24c1f5cd 214 req.socket_id = mSocketID;
komoritan 0:edaa24c1f5cd 215 req.connection_mode = 1;
komoritan 0:edaa24c1f5cd 216
komoritan 0:edaa24c1f5cd 217 // Initialize connection information
komoritan 0:edaa24c1f5cd 218 C_SNIC_Core::tagCONNECT_INFO_T *con_info_p = snic_core_p->getConnectInfo( mSocketID );
komoritan 0:edaa24c1f5cd 219 if( con_info_p != NULL )
komoritan 0:edaa24c1f5cd 220 {
komoritan 0:edaa24c1f5cd 221 con_info_p->from_ip = addr_temp;
komoritan 0:edaa24c1f5cd 222 con_info_p->from_port = remote.get_port();
komoritan 0:edaa24c1f5cd 223 }
komoritan 0:edaa24c1f5cd 224
komoritan 0:edaa24c1f5cd 225 int req_size = sizeof(C_SNIC_Core::tagSNIC_UDP_SEND_FROM_SOCKET_REQ_T);
komoritan 0:edaa24c1f5cd 226
komoritan 0:edaa24c1f5cd 227 char *send_buf_p = getSocketSendBuf();
komoritan 0:edaa24c1f5cd 228 memcpy( send_buf_p, &req, req_size );
komoritan 0:edaa24c1f5cd 229 memcpy( &send_buf_p[req_size], packet, length );
komoritan 0:edaa24c1f5cd 230
komoritan 0:edaa24c1f5cd 231 unsigned char *command_array_p = snic_core_p->getCommandBuf();
komoritan 0:edaa24c1f5cd 232 unsigned int command_len;
komoritan 0:edaa24c1f5cd 233
komoritan 0:edaa24c1f5cd 234 // Make all command request
komoritan 0:edaa24c1f5cd 235 command_len = C_SNIC_UartMsgUtil::makeRequest( UART_CMD_ID_SNIC, (unsigned char *)send_buf_p, req_size + length, command_array_p );
komoritan 0:edaa24c1f5cd 236
komoritan 0:edaa24c1f5cd 237 // Send uart command request
komoritan 0:edaa24c1f5cd 238 snic_core_p->sendUart( command_len, command_array_p );
komoritan 0:edaa24c1f5cd 239
komoritan 0:edaa24c1f5cd 240 #if 0 // TODO: Not wait for command response(Tentative)
komoritan 0:edaa24c1f5cd 241 // Wait UART response
komoritan 0:edaa24c1f5cd 242 int ret = uartCmdMgr_p->wait();
komoritan 0:edaa24c1f5cd 243 if( ret != 0 )
komoritan 0:edaa24c1f5cd 244 {
komoritan 0:edaa24c1f5cd 245 DEBUG_PRINT( "send failed\r\n" );
komoritan 0:edaa24c1f5cd 246 snic_core_p->freeCmdBuf( payload_buf_p );
komoritan 0:edaa24c1f5cd 247 FUNC_OUT();
komoritan 0:edaa24c1f5cd 248 snic_core_p->unlockAPI();
komoritan 0:edaa24c1f5cd 249 return -1;
komoritan 0:edaa24c1f5cd 250 }
komoritan 0:edaa24c1f5cd 251
komoritan 0:edaa24c1f5cd 252 if( uartCmdMgr_p->getCommandStatus() != UART_CMD_RES_SNIC_SUCCESS )
komoritan 0:edaa24c1f5cd 253 {
komoritan 0:edaa24c1f5cd 254 DEBUG_PRINT("send status:%02x\r\n", uartCmdMgr_p->getCommandStatus());
komoritan 0:edaa24c1f5cd 255 snic_core_p->freeCmdBuf( payload_buf_p );
komoritan 0:edaa24c1f5cd 256 FUNC_OUT();
komoritan 0:edaa24c1f5cd 257 snic_core_p->unlockAPI();
komoritan 0:edaa24c1f5cd 258 return -1;
komoritan 0:edaa24c1f5cd 259 }
komoritan 0:edaa24c1f5cd 260 snic_core_p->freeCmdBuf( payload_buf_p );
komoritan 0:edaa24c1f5cd 261 #endif
komoritan 0:edaa24c1f5cd 262
komoritan 0:edaa24c1f5cd 263 FUNC_OUT();
komoritan 0:edaa24c1f5cd 264 #if 0 // TODO: Not wait for command response(Tentative)
komoritan 0:edaa24c1f5cd 265 snic_core_p->unlockAPI();
komoritan 0:edaa24c1f5cd 266 #endif
komoritan 0:edaa24c1f5cd 267 // SNIC_SEND_FROM_SOCKET_REQ
komoritan 0:edaa24c1f5cd 268 wait(0.05);
komoritan 0:edaa24c1f5cd 269
komoritan 0:edaa24c1f5cd 270 return length;
komoritan 0:edaa24c1f5cd 271 // return 0;
komoritan 0:edaa24c1f5cd 272 }
komoritan 0:edaa24c1f5cd 273
komoritan 0:edaa24c1f5cd 274 // -1 if unsuccessful, else number of bytes received
komoritan 0:edaa24c1f5cd 275 int UDPSocket::receiveFrom(Endpoint &remote, char *data_p, int length)
komoritan 0:edaa24c1f5cd 276 {
komoritan 0:edaa24c1f5cd 277 FUNC_IN();
komoritan 0:edaa24c1f5cd 278 if( (data_p == NULL) || (length < 1) )
komoritan 0:edaa24c1f5cd 279 {
komoritan 0:edaa24c1f5cd 280 DEBUG_PRINT("UDPSocket::receiveFrom parameter error\r\n");
komoritan 0:edaa24c1f5cd 281 FUNC_OUT();
komoritan 0:edaa24c1f5cd 282 return -1;
komoritan 0:edaa24c1f5cd 283 }
komoritan 0:edaa24c1f5cd 284
komoritan 0:edaa24c1f5cd 285 C_SNIC_Core *snic_core_p = C_SNIC_Core::getInstance();
komoritan 0:edaa24c1f5cd 286 // Initialize connection information
komoritan 0:edaa24c1f5cd 287 C_SNIC_Core::tagCONNECT_INFO_T *con_info_p = snic_core_p->getConnectInfo( mSocketID );
komoritan 0:edaa24c1f5cd 288 if( con_info_p->recvbuf_p == NULL )
komoritan 0:edaa24c1f5cd 289 {
komoritan 0:edaa24c1f5cd 290 DEBUG_PRINT("UDPSocket::receiveFrom Conncection info error\r\n");
komoritan 0:edaa24c1f5cd 291 FUNC_OUT();
komoritan 0:edaa24c1f5cd 292 return -1;
komoritan 0:edaa24c1f5cd 293 }
komoritan 0:edaa24c1f5cd 294
komoritan 0:edaa24c1f5cd 295 char remote_ip[20] = {'\0'};
komoritan 0:edaa24c1f5cd 296 sprintf( remote_ip, "%d.%d.%d.%d"
komoritan 0:edaa24c1f5cd 297 , (con_info_p->from_ip >>24) & 0x000000ff
komoritan 0:edaa24c1f5cd 298 , (con_info_p->from_ip >>16) & 0x000000ff
komoritan 0:edaa24c1f5cd 299 , (con_info_p->from_ip >>8) & 0x000000ff
komoritan 0:edaa24c1f5cd 300 , (con_info_p->from_ip) & 0x000000ff );
komoritan 0:edaa24c1f5cd 301 remote.set_address( remote_ip, con_info_p->from_port );
komoritan 0:edaa24c1f5cd 302
komoritan 0:edaa24c1f5cd 303 con_info_p->mutex.lock();
komoritan 0:edaa24c1f5cd 304 con_info_p->is_receive_complete = true;
komoritan 0:edaa24c1f5cd 305 con_info_p->mutex.unlock();
komoritan 0:edaa24c1f5cd 306 if( con_info_p->is_received == false )
komoritan 0:edaa24c1f5cd 307 {
komoritan 0:edaa24c1f5cd 308 // Try receive
komoritan 0:edaa24c1f5cd 309 Thread::yield();
komoritan 0:edaa24c1f5cd 310
komoritan 0:edaa24c1f5cd 311 if( con_info_p->is_received == false )
komoritan 0:edaa24c1f5cd 312 {
komoritan 0:edaa24c1f5cd 313 // No data received.
komoritan 0:edaa24c1f5cd 314 FUNC_OUT();
komoritan 0:edaa24c1f5cd 315 return 0;
komoritan 0:edaa24c1f5cd 316 }
komoritan 0:edaa24c1f5cd 317 }
komoritan 0:edaa24c1f5cd 318 // Get packet data from buffer for receive.
komoritan 0:edaa24c1f5cd 319 int i;
komoritan 0:edaa24c1f5cd 320 for (i = 0; i < length; i ++)
komoritan 0:edaa24c1f5cd 321 {
komoritan 0:edaa24c1f5cd 322 if (con_info_p->recvbuf_p->dequeue(&data_p[i]) == false)
komoritan 0:edaa24c1f5cd 323 {
komoritan 0:edaa24c1f5cd 324 break;
komoritan 0:edaa24c1f5cd 325 }
komoritan 0:edaa24c1f5cd 326 }
komoritan 0:edaa24c1f5cd 327 if( con_info_p->recvbuf_p->isEmpty() )
komoritan 0:edaa24c1f5cd 328 {
komoritan 0:edaa24c1f5cd 329 con_info_p->mutex.lock();
komoritan 0:edaa24c1f5cd 330 con_info_p->is_received = false;
komoritan 0:edaa24c1f5cd 331 con_info_p->mutex.unlock();
komoritan 0:edaa24c1f5cd 332 }
komoritan 0:edaa24c1f5cd 333 FUNC_OUT();
komoritan 0:edaa24c1f5cd 334 return i;
komoritan 0:edaa24c1f5cd 335 }