SNICInterface for ThingPlug Example
Dependents: ThingPlug_WiFi_Example ThingPlug_WiFi_Example_2
Fork of SNICInterface by
Socket/UDPSocket.cpp@54:cb4c105a2ad2, 2015-08-13 (annotated)
- Committer:
- lesmin
- Date:
- Thu Aug 13 06:44:01 2015 +0000
- Revision:
- 54:cb4c105a2ad2
- Parent:
- 50:cd632a13e4dc
retry codes are added by STKorea
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 | 39:a1233ca02edf | 18 | /* Copyright (C) 2014 Murata Manufacturing Co.,Ltd., MIT License |
kishino | 41:1c1b5ad4d491 | 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 | 43:d80bbb12ffe6 | 43 | FUNC_IN(); |
kishino | 33:33f1bc919486 | 44 | // Get local ip address. |
kishino | 33:33f1bc919486 | 45 | // Get buffer for response payload from MemoryPool |
kishino | 38:f13e4e563d65 | 46 | tagMEMPOOL_BLOCK_T *payload_buf_p = snic_core_p->allocCmdBuf(); |
kishino | 38:f13e4e563d65 | 47 | if( payload_buf_p == NULL ) |
kishino | 33:33f1bc919486 | 48 | { |
kishino | 40:b6b10c22a121 | 49 | DEBUG_PRINT("UDP bind payload_buf_p NULL\r\n"); |
kishino | 43:d80bbb12ffe6 | 50 | FUNC_OUT(); |
kishino | 33:33f1bc919486 | 51 | return -1; |
kishino | 33:33f1bc919486 | 52 | } |
kishino | 33:33f1bc919486 | 53 | |
kishino | 33:33f1bc919486 | 54 | C_SNIC_Core::tagSNIC_GET_DHCP_INFO_REQ_T req; |
kishino | 33:33f1bc919486 | 55 | // Make request |
kishino | 33:33f1bc919486 | 56 | req.cmd_sid = UART_CMD_SID_SNIC_GET_DHCP_INFO_REQ; |
kishino | 33:33f1bc919486 | 57 | req.seq = mUartRequestSeq++; |
kishino | 33:33f1bc919486 | 58 | req.interface = 0; |
kishino | 33:33f1bc919486 | 59 | |
kishino | 38:f13e4e563d65 | 60 | unsigned char *command_array_p = snic_core_p->getCommandBuf(); |
kishino | 33:33f1bc919486 | 61 | unsigned int command_len; |
kishino | 33:33f1bc919486 | 62 | // Preparation of command |
kishino | 33:33f1bc919486 | 63 | command_len = snic_core_p->preparationSendCommand( UART_CMD_ID_SNIC, req.cmd_sid, (unsigned char *)&req |
kishino | 38:f13e4e563d65 | 64 | , sizeof(C_SNIC_Core::tagSNIC_GET_DHCP_INFO_REQ_T), payload_buf_p->buf, command_array_p ); |
kishino | 33:33f1bc919486 | 65 | // Send uart command request |
kishino | 38:f13e4e563d65 | 66 | snic_core_p->sendUart( command_len, command_array_p ); |
kishino | 33:33f1bc919486 | 67 | // Wait UART response |
kishino | 33:33f1bc919486 | 68 | ret = uartCmdMgr_p->wait(); |
kishino | 33:33f1bc919486 | 69 | if( ret != 0 ) |
kishino | 33:33f1bc919486 | 70 | { |
kishino | 40:b6b10c22a121 | 71 | DEBUG_PRINT( "UDP bind failed\r\n" ); |
kishino | 38:f13e4e563d65 | 72 | snic_core_p->freeCmdBuf( payload_buf_p ); |
kishino | 43:d80bbb12ffe6 | 73 | FUNC_OUT(); |
kishino | 33:33f1bc919486 | 74 | return -1; |
kishino | 33:33f1bc919486 | 75 | } |
kishino | 33:33f1bc919486 | 76 | |
kishino | 33:33f1bc919486 | 77 | if( uartCmdMgr_p->getCommandStatus() != UART_CMD_RES_SNIC_SUCCESS ) |
kishino | 33:33f1bc919486 | 78 | { |
kishino | 40:b6b10c22a121 | 79 | DEBUG_PRINT("UDP bind status:%02x\r\n", uartCmdMgr_p->getCommandStatus()); |
kishino | 38:f13e4e563d65 | 80 | snic_core_p->freeCmdBuf( payload_buf_p ); |
kishino | 43:d80bbb12ffe6 | 81 | FUNC_OUT(); |
kishino | 33:33f1bc919486 | 82 | return -1; |
kishino | 33:33f1bc919486 | 83 | } |
kishino | 33:33f1bc919486 | 84 | |
kishino | 38:f13e4e563d65 | 85 | unsigned int local_addr = (payload_buf_p->buf[9] << 24) |
kishino | 38:f13e4e563d65 | 86 | | (payload_buf_p->buf[10] << 16) |
kishino | 38:f13e4e563d65 | 87 | | (payload_buf_p->buf[11] << 8) |
kishino | 38:f13e4e563d65 | 88 | | (payload_buf_p->buf[12]); |
kishino | 33:33f1bc919486 | 89 | |
kishino | 33:33f1bc919486 | 90 | |
kishino | 33:33f1bc919486 | 91 | C_SNIC_Core::tagSNIC_UDP_CREATE_SOCKET_REQ_T create_req; |
kishino | 33:33f1bc919486 | 92 | |
kishino | 33:33f1bc919486 | 93 | // Make request |
kishino | 33:33f1bc919486 | 94 | create_req.cmd_sid = UART_CMD_SID_SNIC_UDP_CREATE_SOCKET_REQ; |
kishino | 33:33f1bc919486 | 95 | create_req.seq = mUartRequestSeq++; |
kishino | 33:33f1bc919486 | 96 | create_req.bind = 1; |
kishino | 33:33f1bc919486 | 97 | // set ip addr ( byte order ) |
kishino | 33:33f1bc919486 | 98 | C_SNIC_UartMsgUtil::convertIntToByteAdday( local_addr, (char *)create_req.local_addr ); |
kishino | 33:33f1bc919486 | 99 | create_req.local_port[0] = ( (port & 0xFF00) >> 8 ); |
kishino | 33:33f1bc919486 | 100 | create_req.local_port[1] = (port & 0xFF); |
kishino | 33:33f1bc919486 | 101 | |
kishino | 33:33f1bc919486 | 102 | // Preparation of command |
kishino | 33:33f1bc919486 | 103 | command_len = snic_core_p->preparationSendCommand( UART_CMD_ID_SNIC, create_req.cmd_sid, (unsigned char *)&create_req |
kishino | 38:f13e4e563d65 | 104 | , sizeof(C_SNIC_Core::tagSNIC_UDP_CREATE_SOCKET_REQ_T), payload_buf_p->buf, command_array_p ); |
kishino | 33:33f1bc919486 | 105 | // Send uart command request |
kishino | 38:f13e4e563d65 | 106 | snic_core_p->sendUart( command_len, command_array_p ); |
kishino | 33:33f1bc919486 | 107 | |
kishino | 33:33f1bc919486 | 108 | // Wait UART response |
kishino | 33:33f1bc919486 | 109 | ret = uartCmdMgr_p->wait(); |
kishino | 33:33f1bc919486 | 110 | if( ret != 0 ) |
kishino | 33:33f1bc919486 | 111 | { |
kishino | 40:b6b10c22a121 | 112 | DEBUG_PRINT( "UDP bind failed\r\n" ); |
kishino | 38:f13e4e563d65 | 113 | snic_core_p->freeCmdBuf( payload_buf_p ); |
kishino | 43:d80bbb12ffe6 | 114 | FUNC_OUT(); |
kishino | 33:33f1bc919486 | 115 | return -1; |
kishino | 33:33f1bc919486 | 116 | } |
kishino | 33:33f1bc919486 | 117 | |
kishino | 33:33f1bc919486 | 118 | if( uartCmdMgr_p->getCommandStatus() != 0 ) |
kishino | 33:33f1bc919486 | 119 | { |
kishino | 40:b6b10c22a121 | 120 | DEBUG_PRINT("UDP bind status:%02x\r\n", uartCmdMgr_p->getCommandStatus()); |
kishino | 38:f13e4e563d65 | 121 | snic_core_p->freeCmdBuf( payload_buf_p ); |
kishino | 43:d80bbb12ffe6 | 122 | FUNC_OUT(); |
kishino | 33:33f1bc919486 | 123 | return -1; |
kishino | 33:33f1bc919486 | 124 | } |
kishino | 38:f13e4e563d65 | 125 | mSocketID = payload_buf_p->buf[3]; |
kishino | 33:33f1bc919486 | 126 | |
kishino | 43:d80bbb12ffe6 | 127 | // Initialize connection information |
kishino | 43:d80bbb12ffe6 | 128 | C_SNIC_Core::tagCONNECT_INFO_T *con_info_p = snic_core_p->getConnectInfo( mSocketID ); |
kishino | 43:d80bbb12ffe6 | 129 | if( con_info_p->recvbuf_p == NULL ) |
kishino | 43:d80bbb12ffe6 | 130 | { |
kishino | 43:d80bbb12ffe6 | 131 | DEBUG_PRINT( "create recv buffer[socket:%d]\r\n", mSocketID); |
kishino | 43:d80bbb12ffe6 | 132 | con_info_p->recvbuf_p = new CircBuffer<char>(SNIC_UART_RECVBUF_SIZE); |
kishino | 43:d80bbb12ffe6 | 133 | } |
kishino | 43:d80bbb12ffe6 | 134 | con_info_p->is_connected = true; |
kishino | 43:d80bbb12ffe6 | 135 | con_info_p->is_received = false; |
kishino | 43:d80bbb12ffe6 | 136 | |
kishino | 33:33f1bc919486 | 137 | C_SNIC_Core::tagSNIC_UDP_START_RECV_REQ_T recv_start_req; |
kishino | 33:33f1bc919486 | 138 | |
kishino | 33:33f1bc919486 | 139 | // Make request |
kishino | 33:33f1bc919486 | 140 | recv_start_req.cmd_sid = UART_CMD_SID_SNIC_UDP_START_RECV_REQ; |
kishino | 33:33f1bc919486 | 141 | recv_start_req.seq = mUartRequestSeq++; |
kishino | 33:33f1bc919486 | 142 | recv_start_req.socket_id = mSocketID; |
kishino | 33:33f1bc919486 | 143 | recv_start_req.recv_bufsize[0] = ( (SNIC_UART_RECVBUF_SIZE & 0xFF00) >> 8 ); |
kishino | 33:33f1bc919486 | 144 | recv_start_req.recv_bufsize[1] = (SNIC_UART_RECVBUF_SIZE & 0xFF); |
kishino | 33:33f1bc919486 | 145 | |
kishino | 33:33f1bc919486 | 146 | // Preparation of command |
kishino | 33:33f1bc919486 | 147 | command_len = snic_core_p->preparationSendCommand( UART_CMD_ID_SNIC, recv_start_req.cmd_sid, (unsigned char *)&recv_start_req |
kishino | 38:f13e4e563d65 | 148 | , sizeof(C_SNIC_Core::tagSNIC_UDP_START_RECV_REQ_T), payload_buf_p->buf, command_array_p ); |
kishino | 33:33f1bc919486 | 149 | // Send uart command request |
kishino | 38:f13e4e563d65 | 150 | snic_core_p->sendUart( command_len, command_array_p ); |
kishino | 33:33f1bc919486 | 151 | |
kishino | 33:33f1bc919486 | 152 | // Wait UART response |
kishino | 33:33f1bc919486 | 153 | ret = uartCmdMgr_p->wait(); |
kishino | 33:33f1bc919486 | 154 | if( ret != 0 ) |
kishino | 33:33f1bc919486 | 155 | { |
kishino | 40:b6b10c22a121 | 156 | DEBUG_PRINT( "UDP recv start failed\r\n" ); |
kishino | 38:f13e4e563d65 | 157 | snic_core_p->freeCmdBuf( payload_buf_p ); |
kishino | 43:d80bbb12ffe6 | 158 | FUNC_OUT(); |
kishino | 33:33f1bc919486 | 159 | return -1; |
kishino | 33:33f1bc919486 | 160 | } |
kishino | 33:33f1bc919486 | 161 | |
kishino | 33:33f1bc919486 | 162 | if( uartCmdMgr_p->getCommandStatus() != 0 ) |
kishino | 33:33f1bc919486 | 163 | { |
kishino | 40:b6b10c22a121 | 164 | DEBUG_PRINT("UDP recv start status:%02x\r\n", uartCmdMgr_p->getCommandStatus()); |
kishino | 38:f13e4e563d65 | 165 | snic_core_p->freeCmdBuf( payload_buf_p ); |
kishino | 43:d80bbb12ffe6 | 166 | FUNC_OUT(); |
kishino | 33:33f1bc919486 | 167 | return -1; |
kishino | 33:33f1bc919486 | 168 | } |
kishino | 33:33f1bc919486 | 169 | |
kishino | 38:f13e4e563d65 | 170 | snic_core_p->freeCmdBuf( payload_buf_p ); |
kishino | 43:d80bbb12ffe6 | 171 | FUNC_OUT(); |
kishino | 20:dd736d328de6 | 172 | return 0; |
kishino | 20:dd736d328de6 | 173 | } |
kishino | 20:dd736d328de6 | 174 | |
kishino | 20:dd736d328de6 | 175 | // -1 if unsuccessful, else number of bytes written |
kishino | 20:dd736d328de6 | 176 | int UDPSocket::sendTo(Endpoint &remote, char *packet, int length) |
kishino | 20:dd736d328de6 | 177 | { |
kishino | 33:33f1bc919486 | 178 | C_SNIC_Core *snic_core_p = C_SNIC_Core::getInstance(); |
kishino | 33:33f1bc919486 | 179 | C_SNIC_UartCommandManager *uartCmdMgr_p = snic_core_p->getUartCommand(); |
kishino | 33:33f1bc919486 | 180 | |
kishino | 43:d80bbb12ffe6 | 181 | osThreadId tid = Thread::gettid(); |
kishino | 43:d80bbb12ffe6 | 182 | |
kishino | 43:d80bbb12ffe6 | 183 | // pc.printf("send[%08x] len:%d(%04x)\r\n", tid, length, length); |
kishino | 43:d80bbb12ffe6 | 184 | |
kishino | 43:d80bbb12ffe6 | 185 | #if 0 // TODO: Not wait for command response(Tentative) |
kishino | 43:d80bbb12ffe6 | 186 | snic_core_p->lockAPI(); |
kishino | 43:d80bbb12ffe6 | 187 | #endif |
kishino | 43:d80bbb12ffe6 | 188 | FUNC_IN(); |
kishino | 43:d80bbb12ffe6 | 189 | |
kishino | 43:d80bbb12ffe6 | 190 | #if 0 // TODO: Not wait for command response(Tentative) |
kishino | 33:33f1bc919486 | 191 | // Get buffer for response payload from MemoryPool |
kishino | 38:f13e4e563d65 | 192 | tagMEMPOOL_BLOCK_T *payload_buf_p = snic_core_p->allocCmdBuf(); |
kishino | 38:f13e4e563d65 | 193 | if( payload_buf_p == NULL ) |
kishino | 33:33f1bc919486 | 194 | { |
kishino | 40:b6b10c22a121 | 195 | DEBUG_PRINT("connect payload_buf_p NULL\r\n"); |
kishino | 43:d80bbb12ffe6 | 196 | FUNC_OUT(); |
kishino | 43:d80bbb12ffe6 | 197 | snic_core_p->unlockAPI(); |
kishino | 33:33f1bc919486 | 198 | return -1; |
kishino | 33:33f1bc919486 | 199 | } |
kishino | 43:d80bbb12ffe6 | 200 | #endif |
kishino | 43:d80bbb12ffe6 | 201 | |
kishino | 43:d80bbb12ffe6 | 202 | C_SNIC_Core::tagSNIC_UDP_SEND_FROM_SOCKET_REQ_T req; |
kishino | 33:33f1bc919486 | 203 | // Make request |
kishino | 43:d80bbb12ffe6 | 204 | req.cmd_sid = UART_CMD_SID_SNIC_UDP_SEND_FROM_SOCKET_REQ; |
kishino | 33:33f1bc919486 | 205 | req.seq = mUartRequestSeq++; |
kishino | 33:33f1bc919486 | 206 | |
kishino | 33:33f1bc919486 | 207 | int addr_temp; |
kishino | 33:33f1bc919486 | 208 | addr_temp = C_SNIC_UartMsgUtil::addrToInteger( remote.get_address() ); |
kishino | 33:33f1bc919486 | 209 | C_SNIC_UartMsgUtil::convertIntToByteAdday( addr_temp, (char *)req.remote_ip ); |
kishino | 43:d80bbb12ffe6 | 210 | req.remote_port[0] = ( (remote.get_port() & 0xFF00) >> 8 ); |
kishino | 43:d80bbb12ffe6 | 211 | req.remote_port[1] = (remote.get_port() & 0xFF); |
kishino | 43:d80bbb12ffe6 | 212 | req.payload_len[0] = ( (length & 0xFF00) >> 8 ); |
kishino | 43:d80bbb12ffe6 | 213 | req.payload_len[1] = (length & 0xFF); |
kishino | 43:d80bbb12ffe6 | 214 | req.socket_id = mSocketID; |
kishino | 43:d80bbb12ffe6 | 215 | req.connection_mode = 1; |
kishino | 33:33f1bc919486 | 216 | |
kishino | 43:d80bbb12ffe6 | 217 | // Initialize connection information |
kishino | 43:d80bbb12ffe6 | 218 | C_SNIC_Core::tagCONNECT_INFO_T *con_info_p = snic_core_p->getConnectInfo( mSocketID ); |
kishino | 43:d80bbb12ffe6 | 219 | if( con_info_p != NULL ) |
kishino | 43:d80bbb12ffe6 | 220 | { |
kishino | 43:d80bbb12ffe6 | 221 | con_info_p->from_ip = addr_temp; |
kishino | 43:d80bbb12ffe6 | 222 | con_info_p->from_port = remote.get_port(); |
kishino | 43:d80bbb12ffe6 | 223 | } |
kishino | 43:d80bbb12ffe6 | 224 | |
kishino | 43:d80bbb12ffe6 | 225 | int req_size = sizeof(C_SNIC_Core::tagSNIC_UDP_SEND_FROM_SOCKET_REQ_T); |
kishino | 33:33f1bc919486 | 226 | |
kishino | 33:33f1bc919486 | 227 | char *send_buf_p = getSocketSendBuf(); |
kishino | 33:33f1bc919486 | 228 | memcpy( send_buf_p, &req, req_size ); |
kishino | 33:33f1bc919486 | 229 | memcpy( &send_buf_p[req_size], packet, length ); |
kishino | 33:33f1bc919486 | 230 | |
kishino | 38:f13e4e563d65 | 231 | unsigned char *command_array_p = snic_core_p->getCommandBuf(); |
kishino | 33:33f1bc919486 | 232 | unsigned int command_len; |
kishino | 43:d80bbb12ffe6 | 233 | |
kishino | 43:d80bbb12ffe6 | 234 | // Make all command request |
kishino | 43:d80bbb12ffe6 | 235 | command_len = C_SNIC_UartMsgUtil::makeRequest( UART_CMD_ID_SNIC, (unsigned char *)send_buf_p, req_size + length, command_array_p ); |
kishino | 33:33f1bc919486 | 236 | |
kishino | 33:33f1bc919486 | 237 | // Send uart command request |
kishino | 38:f13e4e563d65 | 238 | snic_core_p->sendUart( command_len, command_array_p ); |
kishino | 33:33f1bc919486 | 239 | |
kishino | 43:d80bbb12ffe6 | 240 | #if 0 // TODO: Not wait for command response(Tentative) |
kishino | 33:33f1bc919486 | 241 | // Wait UART response |
kishino | 33:33f1bc919486 | 242 | int ret = uartCmdMgr_p->wait(); |
kishino | 33:33f1bc919486 | 243 | if( ret != 0 ) |
kishino | 33:33f1bc919486 | 244 | { |
kishino | 40:b6b10c22a121 | 245 | DEBUG_PRINT( "send failed\r\n" ); |
kishino | 38:f13e4e563d65 | 246 | snic_core_p->freeCmdBuf( payload_buf_p ); |
kishino | 43:d80bbb12ffe6 | 247 | FUNC_OUT(); |
kishino | 43:d80bbb12ffe6 | 248 | snic_core_p->unlockAPI(); |
kishino | 33:33f1bc919486 | 249 | return -1; |
kishino | 33:33f1bc919486 | 250 | } |
kishino | 33:33f1bc919486 | 251 | |
kishino | 33:33f1bc919486 | 252 | if( uartCmdMgr_p->getCommandStatus() != UART_CMD_RES_SNIC_SUCCESS ) |
kishino | 33:33f1bc919486 | 253 | { |
kishino | 40:b6b10c22a121 | 254 | DEBUG_PRINT("send status:%02x\r\n", uartCmdMgr_p->getCommandStatus()); |
kishino | 38:f13e4e563d65 | 255 | snic_core_p->freeCmdBuf( payload_buf_p ); |
kishino | 43:d80bbb12ffe6 | 256 | FUNC_OUT(); |
kishino | 43:d80bbb12ffe6 | 257 | snic_core_p->unlockAPI(); |
kishino | 33:33f1bc919486 | 258 | return -1; |
kishino | 33:33f1bc919486 | 259 | } |
kishino | 38:f13e4e563d65 | 260 | snic_core_p->freeCmdBuf( payload_buf_p ); |
kishino | 43:d80bbb12ffe6 | 261 | #endif |
kishino | 43:d80bbb12ffe6 | 262 | |
kishino | 43:d80bbb12ffe6 | 263 | FUNC_OUT(); |
kishino | 43:d80bbb12ffe6 | 264 | #if 0 // TODO: Not wait for command response(Tentative) |
kishino | 43:d80bbb12ffe6 | 265 | snic_core_p->unlockAPI(); |
kishino | 43:d80bbb12ffe6 | 266 | #endif |
kishino | 33:33f1bc919486 | 267 | // SNIC_SEND_FROM_SOCKET_REQ |
kishino | 43:d80bbb12ffe6 | 268 | wait(0.05); |
kishino | 43:d80bbb12ffe6 | 269 | |
kishino | 43:d80bbb12ffe6 | 270 | return length; |
kishino | 43:d80bbb12ffe6 | 271 | // return 0; |
kishino | 20:dd736d328de6 | 272 | } |
kishino | 20:dd736d328de6 | 273 | |
kishino | 20:dd736d328de6 | 274 | // -1 if unsuccessful, else number of bytes received |
kishino | 33:33f1bc919486 | 275 | int UDPSocket::receiveFrom(Endpoint &remote, char *data_p, int length) |
kishino | 20:dd736d328de6 | 276 | { |
kishino | 43:d80bbb12ffe6 | 277 | FUNC_IN(); |
kishino | 33:33f1bc919486 | 278 | if( (data_p == NULL) || (length < 1) ) |
kishino | 33:33f1bc919486 | 279 | { |
kishino | 40:b6b10c22a121 | 280 | DEBUG_PRINT("UDPSocket::receiveFrom parameter error\r\n"); |
kishino | 43:d80bbb12ffe6 | 281 | FUNC_OUT(); |
kishino | 33:33f1bc919486 | 282 | return -1; |
kishino | 33:33f1bc919486 | 283 | } |
kishino | 43:d80bbb12ffe6 | 284 | |
kishino | 33:33f1bc919486 | 285 | C_SNIC_Core *snic_core_p = C_SNIC_Core::getInstance(); |
kishino | 33:33f1bc919486 | 286 | // Initialize connection information |
kishino | 43:d80bbb12ffe6 | 287 | C_SNIC_Core::tagCONNECT_INFO_T *con_info_p = snic_core_p->getConnectInfo( mSocketID ); |
kishino | 33:33f1bc919486 | 288 | if( con_info_p->recvbuf_p == NULL ) |
kishino | 33:33f1bc919486 | 289 | { |
kishino | 43:d80bbb12ffe6 | 290 | DEBUG_PRINT("UDPSocket::receiveFrom Conncection info error\r\n"); |
kishino | 43:d80bbb12ffe6 | 291 | FUNC_OUT(); |
kishino | 43:d80bbb12ffe6 | 292 | return -1; |
kishino | 33:33f1bc919486 | 293 | } |
kishino | 33:33f1bc919486 | 294 | |
kishino | 33:33f1bc919486 | 295 | char remote_ip[20] = {'\0'}; |
kishino | 33:33f1bc919486 | 296 | sprintf( remote_ip, "%d.%d.%d.%d" |
kishino | 33:33f1bc919486 | 297 | , (con_info_p->from_ip >>24) & 0x000000ff |
kishino | 33:33f1bc919486 | 298 | , (con_info_p->from_ip >>16) & 0x000000ff |
kishino | 33:33f1bc919486 | 299 | , (con_info_p->from_ip >>8) & 0x000000ff |
kishino | 33:33f1bc919486 | 300 | , (con_info_p->from_ip) & 0x000000ff ); |
kishino | 33:33f1bc919486 | 301 | remote.set_address( remote_ip, con_info_p->from_port ); |
kishino | 33:33f1bc919486 | 302 | |
kishino | 43:d80bbb12ffe6 | 303 | con_info_p->mutex.lock(); |
kishino | 43:d80bbb12ffe6 | 304 | con_info_p->is_receive_complete = true; |
kishino | 43:d80bbb12ffe6 | 305 | con_info_p->mutex.unlock(); |
MACRUM | 50:cd632a13e4dc | 306 | if( con_info_p->is_received == false ) |
kishino | 43:d80bbb12ffe6 | 307 | { |
MACRUM | 50:cd632a13e4dc | 308 | // Try receive |
kishino | 43:d80bbb12ffe6 | 309 | Thread::yield(); |
MACRUM | 50:cd632a13e4dc | 310 | |
MACRUM | 50:cd632a13e4dc | 311 | if( con_info_p->is_received == false ) |
MACRUM | 50:cd632a13e4dc | 312 | { |
MACRUM | 50:cd632a13e4dc | 313 | // No data received. |
MACRUM | 50:cd632a13e4dc | 314 | FUNC_OUT(); |
MACRUM | 50:cd632a13e4dc | 315 | return 0; |
MACRUM | 50:cd632a13e4dc | 316 | } |
kishino | 43:d80bbb12ffe6 | 317 | } |
kishino | 43:d80bbb12ffe6 | 318 | // Get packet data from buffer for receive. |
kishino | 33:33f1bc919486 | 319 | int i; |
kishino | 33:33f1bc919486 | 320 | for (i = 0; i < length; i ++) |
kishino | 33:33f1bc919486 | 321 | { |
kishino | 33:33f1bc919486 | 322 | if (con_info_p->recvbuf_p->dequeue(&data_p[i]) == false) |
kishino | 33:33f1bc919486 | 323 | { |
kishino | 33:33f1bc919486 | 324 | break; |
kishino | 33:33f1bc919486 | 325 | } |
kishino | 33:33f1bc919486 | 326 | } |
kishino | 33:33f1bc919486 | 327 | if( con_info_p->recvbuf_p->isEmpty() ) |
kishino | 33:33f1bc919486 | 328 | { |
kishino | 39:a1233ca02edf | 329 | con_info_p->mutex.lock(); |
kishino | 33:33f1bc919486 | 330 | con_info_p->is_received = false; |
kishino | 39:a1233ca02edf | 331 | con_info_p->mutex.unlock(); |
kishino | 33:33f1bc919486 | 332 | } |
kishino | 43:d80bbb12ffe6 | 333 | FUNC_OUT(); |
kishino | 33:33f1bc919486 | 334 | return i; |
kishino | 20:dd736d328de6 | 335 | } |