Murata Type YD Wi-Fi driver
Dependents: easy-connect-type-yd
SNICInterface/Socket/SNIC_Socket.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_Socket.h" |
MACRUM | 0:35a2186cf186 | 22 | #include <cstring> |
MACRUM | 0:35a2186cf186 | 23 | |
MACRUM | 0:35a2186cf186 | 24 | #include "mbed.h" |
MACRUM | 0:35a2186cf186 | 25 | #include "mbed-trace/mbed_trace.h" |
MACRUM | 0:35a2186cf186 | 26 | #define TRACE_GROUP "SNIC" |
MACRUM | 0:35a2186cf186 | 27 | |
MACRUM | 0:35a2186cf186 | 28 | char gSOCKET_SEND_BUF[2048] __attribute__((section("AHBSRAM1"))); |
MACRUM | 0:35a2186cf186 | 29 | |
MACRUM | 0:35a2186cf186 | 30 | SnicSocket::SnicSocket() |
MACRUM | 0:35a2186cf186 | 31 | { |
MACRUM | 0:35a2186cf186 | 32 | mSocketID = -1; |
MACRUM | 0:35a2186cf186 | 33 | _callback = NULL; |
MACRUM | 0:35a2186cf186 | 34 | _data = NULL; |
MACRUM | 0:35a2186cf186 | 35 | } |
MACRUM | 0:35a2186cf186 | 36 | |
MACRUM | 0:35a2186cf186 | 37 | void SnicSocket::set_blocking(bool blocking, unsigned int timeout) { |
MACRUM | 0:35a2186cf186 | 38 | _blocking = blocking; |
MACRUM | 0:35a2186cf186 | 39 | _timeout = timeout; |
MACRUM | 0:35a2186cf186 | 40 | } |
MACRUM | 0:35a2186cf186 | 41 | |
MACRUM | 0:35a2186cf186 | 42 | SnicSocket::~SnicSocket() { |
MACRUM | 0:35a2186cf186 | 43 | // close(); //Don't want to leak |
MACRUM | 0:35a2186cf186 | 44 | } |
MACRUM | 0:35a2186cf186 | 45 | |
MACRUM | 0:35a2186cf186 | 46 | int SnicSocket::set_option(int level, int optname, const void *optval, socklen_t optlen) { |
MACRUM | 0:35a2186cf186 | 47 | return 0; |
MACRUM | 0:35a2186cf186 | 48 | } |
MACRUM | 0:35a2186cf186 | 49 | |
MACRUM | 0:35a2186cf186 | 50 | int SnicSocket::get_option(int level, int optname, void *optval, socklen_t *optlen) { |
MACRUM | 0:35a2186cf186 | 51 | return 0; |
MACRUM | 0:35a2186cf186 | 52 | } |
MACRUM | 0:35a2186cf186 | 53 | |
MACRUM | 0:35a2186cf186 | 54 | int SnicSocket::close(bool shutdown) |
MACRUM | 0:35a2186cf186 | 55 | { |
MACRUM | 0:35a2186cf186 | 56 | C_SNIC_Core *snic_core_p = C_SNIC_Core::getInstance(); |
MACRUM | 0:35a2186cf186 | 57 | C_SNIC_UartCommandManager *uartCmdMgr_p = snic_core_p->getUartCommand(); |
MACRUM | 0:35a2186cf186 | 58 | |
MACRUM | 0:35a2186cf186 | 59 | FUNC_IN(); |
MACRUM | 0:35a2186cf186 | 60 | // Get buffer for response payload from MemoryPool |
MACRUM | 0:35a2186cf186 | 61 | tagMEMPOOL_BLOCK_T *payload_buf = snic_core_p->allocCmdBuf(); |
MACRUM | 0:35a2186cf186 | 62 | if( payload_buf == NULL ) |
MACRUM | 0:35a2186cf186 | 63 | { |
MACRUM | 0:35a2186cf186 | 64 | DEBUG_PRINT("SnicSocket close payload_buf NULL\r\n"); |
MACRUM | 0:35a2186cf186 | 65 | FUNC_OUT(); |
MACRUM | 0:35a2186cf186 | 66 | return -1; |
MACRUM | 0:35a2186cf186 | 67 | } |
MACRUM | 0:35a2186cf186 | 68 | |
MACRUM | 0:35a2186cf186 | 69 | C_SNIC_Core::tagSNIC_CLOSE_SOCKET_REQ_T req; |
MACRUM | 0:35a2186cf186 | 70 | |
MACRUM | 0:35a2186cf186 | 71 | // Make request |
MACRUM | 0:35a2186cf186 | 72 | req.cmd_sid = UART_CMD_SID_SNIC_CLOSE_SOCKET_REQ; |
MACRUM | 0:35a2186cf186 | 73 | req.seq = mUartRequestSeq++; |
MACRUM | 0:35a2186cf186 | 74 | req.socket_id = mSocketID; |
MACRUM | 0:35a2186cf186 | 75 | |
MACRUM | 0:35a2186cf186 | 76 | unsigned char *command_array_p = snic_core_p->getCommandBuf(); |
MACRUM | 0:35a2186cf186 | 77 | unsigned int command_len; |
MACRUM | 0:35a2186cf186 | 78 | // Preparation of command |
MACRUM | 0:35a2186cf186 | 79 | command_len = snic_core_p->preparationSendCommand( UART_CMD_ID_SNIC, req.cmd_sid, (unsigned char *)&req |
MACRUM | 0:35a2186cf186 | 80 | , sizeof(C_SNIC_Core::tagSNIC_CLOSE_SOCKET_REQ_T), payload_buf->buf, command_array_p ); |
MACRUM | 0:35a2186cf186 | 81 | |
MACRUM | 0:35a2186cf186 | 82 | // Send uart command request |
MACRUM | 0:35a2186cf186 | 83 | snic_core_p->sendUart( command_len, command_array_p ); |
MACRUM | 0:35a2186cf186 | 84 | |
MACRUM | 0:35a2186cf186 | 85 | int ret; |
MACRUM | 0:35a2186cf186 | 86 | // Wait UART response |
MACRUM | 0:35a2186cf186 | 87 | ret = uartCmdMgr_p->wait(); |
MACRUM | 0:35a2186cf186 | 88 | if( ret != 0 ) |
MACRUM | 0:35a2186cf186 | 89 | { |
MACRUM | 0:35a2186cf186 | 90 | DEBUG_PRINT( "SnicSocket close failed:%d\r\n", ret ); |
MACRUM | 0:35a2186cf186 | 91 | snic_core_p->freeCmdBuf( payload_buf ); |
MACRUM | 0:35a2186cf186 | 92 | FUNC_OUT(); |
MACRUM | 0:35a2186cf186 | 93 | return -1; |
MACRUM | 0:35a2186cf186 | 94 | } |
MACRUM | 0:35a2186cf186 | 95 | |
MACRUM | 0:35a2186cf186 | 96 | if( uartCmdMgr_p->getCommandStatus() != 0 ) |
MACRUM | 0:35a2186cf186 | 97 | { |
MACRUM | 0:35a2186cf186 | 98 | DEBUG_PRINT("SnicSocket close status:%02x\r\n", uartCmdMgr_p->getCommandStatus()); |
MACRUM | 0:35a2186cf186 | 99 | snic_core_p->freeCmdBuf( payload_buf ); |
MACRUM | 0:35a2186cf186 | 100 | FUNC_OUT(); |
MACRUM | 0:35a2186cf186 | 101 | return -1; |
MACRUM | 0:35a2186cf186 | 102 | } |
MACRUM | 0:35a2186cf186 | 103 | snic_core_p->freeCmdBuf( payload_buf ); |
MACRUM | 0:35a2186cf186 | 104 | FUNC_OUT(); |
MACRUM | 0:35a2186cf186 | 105 | return 0; |
MACRUM | 0:35a2186cf186 | 106 | } |
MACRUM | 0:35a2186cf186 | 107 | |
MACRUM | 0:35a2186cf186 | 108 | int SnicSocket::createSocket( unsigned char bind, unsigned int local_addr, unsigned short port ) |
MACRUM | 0:35a2186cf186 | 109 | { |
MACRUM | 0:35a2186cf186 | 110 | C_SNIC_Core *snic_core_p = C_SNIC_Core::getInstance(); |
MACRUM | 0:35a2186cf186 | 111 | C_SNIC_UartCommandManager *uartCmdMgr_p = snic_core_p->getUartCommand(); |
MACRUM | 0:35a2186cf186 | 112 | |
MACRUM | 0:35a2186cf186 | 113 | FUNC_IN(); |
MACRUM | 0:35a2186cf186 | 114 | // Get buffer for response payload from MemoryPool |
MACRUM | 0:35a2186cf186 | 115 | tagMEMPOOL_BLOCK_T *payload_buf = snic_core_p->allocCmdBuf(); |
MACRUM | 0:35a2186cf186 | 116 | if( payload_buf == NULL ) |
MACRUM | 0:35a2186cf186 | 117 | { |
MACRUM | 0:35a2186cf186 | 118 | DEBUG_PRINT("createSocket payload_buf NULL\r\n"); |
MACRUM | 0:35a2186cf186 | 119 | FUNC_OUT(); |
MACRUM | 0:35a2186cf186 | 120 | return -1; |
MACRUM | 0:35a2186cf186 | 121 | } |
MACRUM | 0:35a2186cf186 | 122 | |
MACRUM | 0:35a2186cf186 | 123 | C_SNIC_Core::tagSNIC_TCP_CREATE_SOCKET_REQ_T req; |
MACRUM | 0:35a2186cf186 | 124 | int req_len = 0; |
MACRUM | 0:35a2186cf186 | 125 | |
MACRUM | 0:35a2186cf186 | 126 | // Make request |
MACRUM | 0:35a2186cf186 | 127 | req.cmd_sid = UART_CMD_SID_SNIC_TCP_CREATE_SOCKET_REQ; |
MACRUM | 0:35a2186cf186 | 128 | req_len++; |
MACRUM | 0:35a2186cf186 | 129 | req.seq = mUartRequestSeq++; |
MACRUM | 0:35a2186cf186 | 130 | req_len++; |
MACRUM | 0:35a2186cf186 | 131 | req.bind = bind; |
MACRUM | 0:35a2186cf186 | 132 | req_len++; |
MACRUM | 0:35a2186cf186 | 133 | if( bind != 0 ) |
MACRUM | 0:35a2186cf186 | 134 | { |
MACRUM | 0:35a2186cf186 | 135 | // set ip addr ( byte order ) |
MACRUM | 0:35a2186cf186 | 136 | C_SNIC_UartMsgUtil::convertIntToByteAdday( local_addr, (char *)req.local_addr ); |
MACRUM | 0:35a2186cf186 | 137 | req.local_port[0] = ( (port & 0xFF00) >> 8 ); |
MACRUM | 0:35a2186cf186 | 138 | req.local_port[1] = (port & 0xFF); |
MACRUM | 0:35a2186cf186 | 139 | |
MACRUM | 0:35a2186cf186 | 140 | req_len = sizeof(C_SNIC_Core::tagSNIC_TCP_CREATE_SOCKET_REQ_T); |
MACRUM | 0:35a2186cf186 | 141 | } |
MACRUM | 0:35a2186cf186 | 142 | |
MACRUM | 0:35a2186cf186 | 143 | unsigned char *command_array_p = snic_core_p->getCommandBuf(); |
MACRUM | 0:35a2186cf186 | 144 | unsigned int command_len; |
MACRUM | 0:35a2186cf186 | 145 | // Preparation of command |
MACRUM | 0:35a2186cf186 | 146 | command_len = snic_core_p->preparationSendCommand( UART_CMD_ID_SNIC, req.cmd_sid, (unsigned char *)&req |
MACRUM | 0:35a2186cf186 | 147 | , req_len, payload_buf->buf, command_array_p ); |
MACRUM | 0:35a2186cf186 | 148 | // Send uart command request |
MACRUM | 0:35a2186cf186 | 149 | snic_core_p->sendUart( command_len, command_array_p ); |
MACRUM | 0:35a2186cf186 | 150 | |
MACRUM | 0:35a2186cf186 | 151 | int ret; |
MACRUM | 0:35a2186cf186 | 152 | // Wait UART response |
MACRUM | 0:35a2186cf186 | 153 | ret = uartCmdMgr_p->wait(); |
MACRUM | 0:35a2186cf186 | 154 | if( ret != 0 ) |
MACRUM | 0:35a2186cf186 | 155 | { |
MACRUM | 0:35a2186cf186 | 156 | DEBUG_PRINT( "createSocket failed\r\n" ); |
MACRUM | 0:35a2186cf186 | 157 | snic_core_p->freeCmdBuf( payload_buf ); |
MACRUM | 0:35a2186cf186 | 158 | FUNC_OUT(); |
MACRUM | 0:35a2186cf186 | 159 | return -1; |
MACRUM | 0:35a2186cf186 | 160 | } |
MACRUM | 0:35a2186cf186 | 161 | |
MACRUM | 0:35a2186cf186 | 162 | if( uartCmdMgr_p->getCommandStatus() != 0 ) |
MACRUM | 0:35a2186cf186 | 163 | { |
MACRUM | 0:35a2186cf186 | 164 | DEBUG_PRINT("createSocket status:%02x\r\n", uartCmdMgr_p->getCommandStatus()); |
MACRUM | 0:35a2186cf186 | 165 | snic_core_p->freeCmdBuf( payload_buf ); |
MACRUM | 0:35a2186cf186 | 166 | FUNC_OUT(); |
MACRUM | 0:35a2186cf186 | 167 | return -1; |
MACRUM | 0:35a2186cf186 | 168 | } |
MACRUM | 0:35a2186cf186 | 169 | mSocketID = payload_buf->buf[3]; |
MACRUM | 0:35a2186cf186 | 170 | tr_debug("createSocket:mSocketID=%d", mSocketID); |
MACRUM | 0:35a2186cf186 | 171 | snic_core_p->freeCmdBuf( payload_buf ); |
MACRUM | 0:35a2186cf186 | 172 | FUNC_OUT(); |
MACRUM | 0:35a2186cf186 | 173 | return 0; |
MACRUM | 0:35a2186cf186 | 174 | } |
MACRUM | 0:35a2186cf186 | 175 | |
MACRUM | 0:35a2186cf186 | 176 | int SnicSocket::resolveHostName( const char *host_p ) |
MACRUM | 0:35a2186cf186 | 177 | { |
MACRUM | 0:35a2186cf186 | 178 | C_SNIC_Core *snic_core_p = C_SNIC_Core::getInstance(); |
MACRUM | 0:35a2186cf186 | 179 | C_SNIC_UartCommandManager *uartCmdMgr_p = snic_core_p->getUartCommand(); |
MACRUM | 0:35a2186cf186 | 180 | int ip_addr = 0; |
MACRUM | 0:35a2186cf186 | 181 | |
MACRUM | 0:35a2186cf186 | 182 | if( host_p == NULL ) |
MACRUM | 0:35a2186cf186 | 183 | { |
MACRUM | 0:35a2186cf186 | 184 | DEBUG_PRINT("resolveHostName parameter error\r\n"); |
MACRUM | 0:35a2186cf186 | 185 | return -1; |
MACRUM | 0:35a2186cf186 | 186 | } |
MACRUM | 0:35a2186cf186 | 187 | |
MACRUM | 0:35a2186cf186 | 188 | // Get buffer for response payload from MemoryPool |
MACRUM | 0:35a2186cf186 | 189 | tagMEMPOOL_BLOCK_T *payload_buf = snic_core_p->allocCmdBuf(); |
MACRUM | 0:35a2186cf186 | 190 | if( payload_buf == NULL ) |
MACRUM | 0:35a2186cf186 | 191 | { |
MACRUM | 0:35a2186cf186 | 192 | DEBUG_PRINT("resolveHostName payload_buf NULL\r\n"); |
MACRUM | 0:35a2186cf186 | 193 | return -1; |
MACRUM | 0:35a2186cf186 | 194 | } |
MACRUM | 0:35a2186cf186 | 195 | |
MACRUM | 0:35a2186cf186 | 196 | unsigned char *buf_p = (unsigned char *)getSocketSendBuf(); |
MACRUM | 0:35a2186cf186 | 197 | unsigned int buf_len = 0; |
MACRUM | 0:35a2186cf186 | 198 | |
MACRUM | 0:35a2186cf186 | 199 | memset( buf_p, 0, UART_REQUEST_PAYLOAD_MAX ); |
MACRUM | 0:35a2186cf186 | 200 | // Make request |
MACRUM | 0:35a2186cf186 | 201 | buf_p[0] = UART_CMD_SID_SNIC_RESOLVE_NAME_REQ; |
MACRUM | 0:35a2186cf186 | 202 | buf_len++; |
MACRUM | 0:35a2186cf186 | 203 | buf_p[1] = mUartRequestSeq++; |
MACRUM | 0:35a2186cf186 | 204 | buf_len++; |
MACRUM | 0:35a2186cf186 | 205 | // Interface |
MACRUM | 0:35a2186cf186 | 206 | buf_p[2] = 0; |
MACRUM | 0:35a2186cf186 | 207 | buf_len++; |
MACRUM | 0:35a2186cf186 | 208 | |
MACRUM | 0:35a2186cf186 | 209 | // Host name length |
MACRUM | 0:35a2186cf186 | 210 | int hostname_len = strlen(host_p); |
MACRUM | 0:35a2186cf186 | 211 | buf_p[3] = (unsigned char)hostname_len; |
MACRUM | 0:35a2186cf186 | 212 | buf_len++; |
MACRUM | 0:35a2186cf186 | 213 | memcpy( &buf_p[4], host_p, hostname_len ); |
MACRUM | 0:35a2186cf186 | 214 | buf_len += hostname_len; |
MACRUM | 0:35a2186cf186 | 215 | |
MACRUM | 0:35a2186cf186 | 216 | unsigned char *command_array_p = snic_core_p->getCommandBuf(); |
MACRUM | 0:35a2186cf186 | 217 | unsigned int command_len; |
MACRUM | 0:35a2186cf186 | 218 | command_len = snic_core_p->preparationSendCommand( UART_CMD_ID_SNIC, UART_CMD_SID_SNIC_RESOLVE_NAME_REQ, buf_p |
MACRUM | 0:35a2186cf186 | 219 | , buf_len, payload_buf->buf, command_array_p ); |
MACRUM | 0:35a2186cf186 | 220 | // Send uart command request |
MACRUM | 0:35a2186cf186 | 221 | snic_core_p->sendUart( command_len, command_array_p ); |
MACRUM | 0:35a2186cf186 | 222 | |
MACRUM | 0:35a2186cf186 | 223 | int ret; |
MACRUM | 0:35a2186cf186 | 224 | // Wait UART response |
MACRUM | 0:35a2186cf186 | 225 | ret = uartCmdMgr_p->wait(); |
MACRUM | 0:35a2186cf186 | 226 | if( ret != 0 ) |
MACRUM | 0:35a2186cf186 | 227 | { |
MACRUM | 0:35a2186cf186 | 228 | DEBUG_PRINT( "resolveHostName failed\r\n" ); |
MACRUM | 0:35a2186cf186 | 229 | snic_core_p->freeCmdBuf( payload_buf ); |
MACRUM | 0:35a2186cf186 | 230 | return -1; |
MACRUM | 0:35a2186cf186 | 231 | } |
MACRUM | 0:35a2186cf186 | 232 | |
MACRUM | 0:35a2186cf186 | 233 | // check status |
MACRUM | 0:35a2186cf186 | 234 | if( uartCmdMgr_p->getCommandStatus() == 0 ) |
MACRUM | 0:35a2186cf186 | 235 | { |
MACRUM | 0:35a2186cf186 | 236 | ip_addr = ((payload_buf->buf[3] << 24) & 0xFF000000) |
MACRUM | 0:35a2186cf186 | 237 | | ((payload_buf->buf[4] << 16) & 0xFF0000) |
MACRUM | 0:35a2186cf186 | 238 | | ((payload_buf->buf[5] << 8) & 0xFF00) |
MACRUM | 0:35a2186cf186 | 239 | | (payload_buf->buf[6]); |
MACRUM | 0:35a2186cf186 | 240 | } |
MACRUM | 0:35a2186cf186 | 241 | |
MACRUM | 0:35a2186cf186 | 242 | snic_core_p->freeCmdBuf( payload_buf ); |
MACRUM | 0:35a2186cf186 | 243 | return ip_addr; |
MACRUM | 0:35a2186cf186 | 244 | } |
MACRUM | 0:35a2186cf186 | 245 | |
MACRUM | 0:35a2186cf186 | 246 | char *SnicSocket::getSocketSendBuf() |
MACRUM | 0:35a2186cf186 | 247 | { |
MACRUM | 0:35a2186cf186 | 248 | return gSOCKET_SEND_BUF; |
MACRUM | 0:35a2186cf186 | 249 | } |
MACRUM | 0:35a2186cf186 | 250 | |
MACRUM | 0:35a2186cf186 | 251 | void SnicSocket::socket_attach(void (*callback)(void *), void *data) |
MACRUM | 0:35a2186cf186 | 252 | { |
MACRUM | 0:35a2186cf186 | 253 | _callback = callback; |
MACRUM | 0:35a2186cf186 | 254 | _data = data; |
MACRUM | 0:35a2186cf186 | 255 | } |
MACRUM | 0:35a2186cf186 | 256 | |
MACRUM | 0:35a2186cf186 | 257 | void SnicSocket::event() |
MACRUM | 0:35a2186cf186 | 258 | { |
MACRUM | 0:35a2186cf186 | 259 | if (_callback) { |
MACRUM | 0:35a2186cf186 | 260 | (*_callback)(_data); |
MACRUM | 0:35a2186cf186 | 261 | } |
MACRUM | 0:35a2186cf186 | 262 | } |