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