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