Customized
Dependents: MurataTypeYD_RPC_Sample
Fork of SNICInterface_mod by
SNIC_WifiInterface.cpp@43:d80bbb12ffe6, 2014-11-14 (annotated)
- Committer:
- kishino
- Date:
- Fri Nov 14 00:52:31 2014 +0000
- Revision:
- 43:d80bbb12ffe6
- Parent:
- 40:b6b10c22a121
- Child:
- 47:62ba69ff80d2
It was reviewed and modified the Receive logic of socket communication(TCP and UDP).
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
kishino | 39:a1233ca02edf | 1 | /* Copyright (C) 2014 Murata Manufacturing Co.,Ltd., MIT License |
kishino | 39:a1233ca02edf | 2 | * muRata, SWITCH SCIENCE Wi-FI module TypeYD-SNIC UART. |
kishino | 14:54378c96d285 | 3 | * |
kishino | 39:a1233ca02edf | 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software |
kishino | 39:a1233ca02edf | 5 | * and associated documentation files (the "Software"), to deal in the Software without restriction, |
kishino | 39:a1233ca02edf | 6 | * including without limitation the rights to use, copy, modify, merge, publish, distribute, |
kishino | 39:a1233ca02edf | 7 | * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is |
kishino | 39:a1233ca02edf | 8 | * furnished to do so, subject to the following conditions: |
kishino | 14:54378c96d285 | 9 | * |
kishino | 39:a1233ca02edf | 10 | * The above copyright notice and this permission notice shall be included in all copies or |
kishino | 39:a1233ca02edf | 11 | * substantial portions of the Software. |
kishino | 14:54378c96d285 | 12 | * |
kishino | 39:a1233ca02edf | 13 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING |
kishino | 39:a1233ca02edf | 14 | * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
kishino | 39:a1233ca02edf | 15 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
kishino | 39:a1233ca02edf | 16 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
kishino | 39:a1233ca02edf | 17 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
kishino | 39:a1233ca02edf | 18 | */ |
kishino | 12:0254eaccfda2 | 19 | #include "SNIC_WifiInterface.h" |
kishino | 12:0254eaccfda2 | 20 | #include "SNIC_UartMsgUtil.h" |
kishino | 12:0254eaccfda2 | 21 | |
kishino | 36:f33fcf5975ab | 22 | #define UART_CONNECT_BUF_SIZE 512 |
kishino | 36:f33fcf5975ab | 23 | unsigned char gCONNECT_BUF[UART_CONNECT_BUF_SIZE]; |
kishino | 12:0254eaccfda2 | 24 | |
kishino | 12:0254eaccfda2 | 25 | C_SNIC_WifiInterface::C_SNIC_WifiInterface( PinName tx, PinName rx, PinName cts, PinName rts, PinName reset, PinName alarm, int baud) |
kishino | 12:0254eaccfda2 | 26 | { |
kishino | 12:0254eaccfda2 | 27 | mUART_tx = tx; |
kishino | 12:0254eaccfda2 | 28 | mUART_rx = rx; |
kishino | 12:0254eaccfda2 | 29 | mUART_cts = cts; |
kishino | 12:0254eaccfda2 | 30 | mUART_rts = rts;; |
kishino | 12:0254eaccfda2 | 31 | mUART_baud = baud; |
kishino | 12:0254eaccfda2 | 32 | mModuleReset = reset; |
kishino | 12:0254eaccfda2 | 33 | } |
kishino | 12:0254eaccfda2 | 34 | |
kishino | 26:f2e1030964e4 | 35 | C_SNIC_WifiInterface::~C_SNIC_WifiInterface() |
kishino | 26:f2e1030964e4 | 36 | { |
kishino | 26:f2e1030964e4 | 37 | } |
kishino | 26:f2e1030964e4 | 38 | |
kishino | 12:0254eaccfda2 | 39 | int C_SNIC_WifiInterface::init() |
kishino | 24:987e412ae879 | 40 | { |
kishino | 24:987e412ae879 | 41 | C_SNIC_Core *snic_core_p = C_SNIC_Core::getInstance(); |
kishino | 24:987e412ae879 | 42 | C_SNIC_UartCommandManager *uartCmdMgr_p = snic_core_p->getUartCommand(); |
kishino | 16:6100acfeb1f1 | 43 | |
kishino | 37:f3a2053627c2 | 44 | /* Initialize UART */ |
kishino | 37:f3a2053627c2 | 45 | snic_core_p->initUart( mUART_tx, mUART_rx, mUART_baud ); |
kishino | 37:f3a2053627c2 | 46 | |
kishino | 29:6a0ba999597d | 47 | /* Module reset */ |
kishino | 29:6a0ba999597d | 48 | snic_core_p->resetModule( mModuleReset ); |
kishino | 29:6a0ba999597d | 49 | |
kishino | 37:f3a2053627c2 | 50 | wait(1); |
kishino | 12:0254eaccfda2 | 51 | /* Initialize SNIC API */ |
kishino | 12:0254eaccfda2 | 52 | // Get buffer for response payload from MemoryPool |
kishino | 38:f13e4e563d65 | 53 | tagMEMPOOL_BLOCK_T *payload_buf_p = snic_core_p->allocCmdBuf(); |
kishino | 38:f13e4e563d65 | 54 | if( payload_buf_p == NULL ) |
kishino | 12:0254eaccfda2 | 55 | { |
kishino | 40:b6b10c22a121 | 56 | DEBUG_PRINT("snic_init payload_buf_p NULL\r\n"); |
kishino | 12:0254eaccfda2 | 57 | return -1; |
kishino | 12:0254eaccfda2 | 58 | } |
kishino | 12:0254eaccfda2 | 59 | |
kishino | 22:a9ec0cad4f84 | 60 | C_SNIC_Core::tagSNIC_INIT_REQ_T req; |
kishino | 12:0254eaccfda2 | 61 | // Make request |
kishino | 12:0254eaccfda2 | 62 | req.cmd_sid = UART_CMD_SID_SNIC_INIT_REQ; |
kishino | 12:0254eaccfda2 | 63 | req.seq = mUartRequestSeq++; |
kishino | 28:b796031f6519 | 64 | req.buf_size[0] = 0x08; |
kishino | 28:b796031f6519 | 65 | req.buf_size[1] = 0x00; |
kishino | 12:0254eaccfda2 | 66 | |
kishino | 38:f13e4e563d65 | 67 | unsigned char *command_array_p = snic_core_p->getCommandBuf(); |
kishino | 12:0254eaccfda2 | 68 | unsigned int command_len; |
kishino | 12:0254eaccfda2 | 69 | // Preparation of command |
kishino | 24:987e412ae879 | 70 | command_len = snic_core_p->preparationSendCommand( UART_CMD_ID_SNIC, req.cmd_sid, (unsigned char *)&req |
kishino | 38:f13e4e563d65 | 71 | , sizeof(C_SNIC_Core::tagSNIC_INIT_REQ_T), payload_buf_p->buf, command_array_p ); |
kishino | 12:0254eaccfda2 | 72 | |
kishino | 12:0254eaccfda2 | 73 | // Send uart command request |
kishino | 38:f13e4e563d65 | 74 | snic_core_p->sendUart( command_len, command_array_p ); |
kishino | 12:0254eaccfda2 | 75 | |
kishino | 12:0254eaccfda2 | 76 | int ret; |
kishino | 12:0254eaccfda2 | 77 | // Wait UART response |
kishino | 16:6100acfeb1f1 | 78 | ret = uartCmdMgr_p->wait(); |
kishino | 12:0254eaccfda2 | 79 | if( ret != 0 ) |
kishino | 12:0254eaccfda2 | 80 | { |
kishino | 40:b6b10c22a121 | 81 | DEBUG_PRINT( "snic_init failed\r\n" ); |
kishino | 38:f13e4e563d65 | 82 | snic_core_p->freeCmdBuf( payload_buf_p ); |
kishino | 12:0254eaccfda2 | 83 | return -1; |
kishino | 12:0254eaccfda2 | 84 | } |
kishino | 12:0254eaccfda2 | 85 | |
kishino | 16:6100acfeb1f1 | 86 | if( uartCmdMgr_p->getCommandStatus() != 0 ) |
kishino | 12:0254eaccfda2 | 87 | { |
kishino | 40:b6b10c22a121 | 88 | DEBUG_PRINT("snic_init status:%02x\r\n", uartCmdMgr_p->getCommandStatus()); |
kishino | 39:a1233ca02edf | 89 | snic_core_p->freeCmdBuf( payload_buf_p ); |
kishino | 39:a1233ca02edf | 90 | return -1; |
kishino | 12:0254eaccfda2 | 91 | } |
kishino | 38:f13e4e563d65 | 92 | snic_core_p->freeCmdBuf( payload_buf_p ); |
kishino | 12:0254eaccfda2 | 93 | |
kishino | 12:0254eaccfda2 | 94 | return ret; |
kishino | 12:0254eaccfda2 | 95 | } |
kishino | 12:0254eaccfda2 | 96 | |
kishino | 12:0254eaccfda2 | 97 | int C_SNIC_WifiInterface::getFWVersion( unsigned char *version_p ) |
kishino | 12:0254eaccfda2 | 98 | { |
kishino | 24:987e412ae879 | 99 | C_SNIC_Core *snic_core_p = C_SNIC_Core::getInstance(); |
kishino | 24:987e412ae879 | 100 | C_SNIC_UartCommandManager *uartCmdMgr_p = snic_core_p->getUartCommand(); |
kishino | 16:6100acfeb1f1 | 101 | |
kishino | 12:0254eaccfda2 | 102 | // Get buffer for response payload from MemoryPool |
kishino | 38:f13e4e563d65 | 103 | tagMEMPOOL_BLOCK_T *payload_buf_p = snic_core_p->allocCmdBuf(); |
kishino | 38:f13e4e563d65 | 104 | if( payload_buf_p == NULL ) |
kishino | 12:0254eaccfda2 | 105 | { |
kishino | 40:b6b10c22a121 | 106 | DEBUG_PRINT("getFWVersion payload_buf_p NULL\r\n"); |
kishino | 12:0254eaccfda2 | 107 | return -1; |
kishino | 12:0254eaccfda2 | 108 | } |
kishino | 12:0254eaccfda2 | 109 | |
kishino | 22:a9ec0cad4f84 | 110 | C_SNIC_Core::tagGEN_FW_VER_GET_REQ_T req; |
kishino | 12:0254eaccfda2 | 111 | // Make request |
kishino | 12:0254eaccfda2 | 112 | req.cmd_sid = UART_CMD_SID_GEN_FW_VER_GET_REQ; |
kishino | 12:0254eaccfda2 | 113 | req.seq = mUartRequestSeq++; |
kishino | 12:0254eaccfda2 | 114 | |
kishino | 38:f13e4e563d65 | 115 | unsigned char *command_array_p = snic_core_p->getCommandBuf(); |
kishino | 12:0254eaccfda2 | 116 | unsigned int command_len; |
kishino | 12:0254eaccfda2 | 117 | // Preparation of command |
kishino | 24:987e412ae879 | 118 | command_len = snic_core_p->preparationSendCommand( UART_CMD_ID_GEN, req.cmd_sid, (unsigned char *)&req |
kishino | 38:f13e4e563d65 | 119 | , sizeof(C_SNIC_Core::tagGEN_FW_VER_GET_REQ_T), payload_buf_p->buf, command_array_p ); |
kishino | 12:0254eaccfda2 | 120 | |
kishino | 12:0254eaccfda2 | 121 | int ret; |
kishino | 12:0254eaccfda2 | 122 | |
kishino | 12:0254eaccfda2 | 123 | // Send uart command request |
kishino | 38:f13e4e563d65 | 124 | snic_core_p->sendUart( command_len, command_array_p ); |
kishino | 12:0254eaccfda2 | 125 | |
kishino | 12:0254eaccfda2 | 126 | // Wait UART response |
kishino | 16:6100acfeb1f1 | 127 | ret = uartCmdMgr_p->wait(); |
kishino | 12:0254eaccfda2 | 128 | if( ret != 0 ) |
kishino | 12:0254eaccfda2 | 129 | { |
kishino | 40:b6b10c22a121 | 130 | DEBUG_PRINT( "getFWversion failed\r\n" ); |
kishino | 38:f13e4e563d65 | 131 | snic_core_p->freeCmdBuf( payload_buf_p ); |
kishino | 12:0254eaccfda2 | 132 | return -1; |
kishino | 12:0254eaccfda2 | 133 | } |
kishino | 12:0254eaccfda2 | 134 | |
kishino | 16:6100acfeb1f1 | 135 | if( uartCmdMgr_p->getCommandStatus() == 0 ) |
kishino | 12:0254eaccfda2 | 136 | { |
kishino | 38:f13e4e563d65 | 137 | unsigned char version_len = payload_buf_p->buf[3]; |
kishino | 38:f13e4e563d65 | 138 | memcpy( version_p, &payload_buf_p->buf[4], version_len ); |
kishino | 12:0254eaccfda2 | 139 | } |
kishino | 38:f13e4e563d65 | 140 | snic_core_p->freeCmdBuf( payload_buf_p ); |
kishino | 12:0254eaccfda2 | 141 | return 0; |
kishino | 12:0254eaccfda2 | 142 | } |
kishino | 12:0254eaccfda2 | 143 | |
kishino | 12:0254eaccfda2 | 144 | int C_SNIC_WifiInterface::connect(const char *ssid_p, unsigned char ssid_len, E_SECURITY sec_type |
kishino | 12:0254eaccfda2 | 145 | , const char *sec_key_p, unsigned char sec_key_len) |
kishino | 12:0254eaccfda2 | 146 | { |
kishino | 24:987e412ae879 | 147 | C_SNIC_Core *snic_core_p = C_SNIC_Core::getInstance(); |
kishino | 24:987e412ae879 | 148 | C_SNIC_UartCommandManager *uartCmdMgr_p = snic_core_p->getUartCommand(); |
kishino | 16:6100acfeb1f1 | 149 | |
kishino | 12:0254eaccfda2 | 150 | // Parameter check(SSID) |
kishino | 12:0254eaccfda2 | 151 | if( (ssid_p == NULL) || (ssid_len == 0) ) |
kishino | 12:0254eaccfda2 | 152 | { |
kishino | 40:b6b10c22a121 | 153 | DEBUG_PRINT( "connect failed [ parameter NG:SSID ]\r\n" ); |
kishino | 12:0254eaccfda2 | 154 | return -1; |
kishino | 12:0254eaccfda2 | 155 | } |
kishino | 12:0254eaccfda2 | 156 | |
kishino | 12:0254eaccfda2 | 157 | // Parameter check(Security key) |
kishino | 12:0254eaccfda2 | 158 | if( (sec_type != e_SEC_OPEN) && ( (sec_key_len == 0) || (sec_key_p == NULL) ) ) |
kishino | 12:0254eaccfda2 | 159 | { |
kishino | 40:b6b10c22a121 | 160 | DEBUG_PRINT( "connect failed [ parameter NG:Security key ]\r\n" ); |
kishino | 12:0254eaccfda2 | 161 | return -1; |
kishino | 12:0254eaccfda2 | 162 | } |
kishino | 12:0254eaccfda2 | 163 | |
kishino | 12:0254eaccfda2 | 164 | // Get buffer for response payload from MemoryPool |
kishino | 38:f13e4e563d65 | 165 | tagMEMPOOL_BLOCK_T *payload_buf_p = snic_core_p->allocCmdBuf(); |
kishino | 38:f13e4e563d65 | 166 | if( payload_buf_p == NULL ) |
kishino | 12:0254eaccfda2 | 167 | { |
kishino | 40:b6b10c22a121 | 168 | DEBUG_PRINT("connect payload_buf_p NULL\r\n"); |
kishino | 12:0254eaccfda2 | 169 | return -1; |
kishino | 12:0254eaccfda2 | 170 | } |
kishino | 12:0254eaccfda2 | 171 | |
kishino | 29:6a0ba999597d | 172 | unsigned char *buf = &gCONNECT_BUF[0]; |
kishino | 12:0254eaccfda2 | 173 | unsigned int buf_len = 0; |
kishino | 12:0254eaccfda2 | 174 | unsigned int command_len; |
kishino | 12:0254eaccfda2 | 175 | |
kishino | 36:f33fcf5975ab | 176 | memset( buf, 0, UART_CONNECT_BUF_SIZE ); |
kishino | 12:0254eaccfda2 | 177 | // Make request |
kishino | 12:0254eaccfda2 | 178 | buf[0] = UART_CMD_SID_WIFI_JOIN_REQ; |
kishino | 12:0254eaccfda2 | 179 | buf_len++; |
kishino | 12:0254eaccfda2 | 180 | buf[1] = mUartRequestSeq++; |
kishino | 12:0254eaccfda2 | 181 | buf_len++; |
kishino | 12:0254eaccfda2 | 182 | // SSID |
kishino | 12:0254eaccfda2 | 183 | memcpy( &buf[2], ssid_p, ssid_len ); |
kishino | 12:0254eaccfda2 | 184 | buf_len += ssid_len; |
kishino | 12:0254eaccfda2 | 185 | buf_len++; |
kishino | 12:0254eaccfda2 | 186 | |
kishino | 12:0254eaccfda2 | 187 | // Security mode |
kishino | 12:0254eaccfda2 | 188 | buf[ buf_len ] = (unsigned char)sec_type; |
kishino | 12:0254eaccfda2 | 189 | buf_len++; |
kishino | 12:0254eaccfda2 | 190 | |
kishino | 12:0254eaccfda2 | 191 | // Security key |
kishino | 12:0254eaccfda2 | 192 | if( sec_type != e_SEC_OPEN ) |
kishino | 12:0254eaccfda2 | 193 | { |
kishino | 12:0254eaccfda2 | 194 | buf[ buf_len ] = sec_key_len; |
kishino | 12:0254eaccfda2 | 195 | buf_len++; |
kishino | 12:0254eaccfda2 | 196 | if( sec_key_len > 0 ) |
kishino | 12:0254eaccfda2 | 197 | { |
kishino | 12:0254eaccfda2 | 198 | memcpy( &buf[buf_len], sec_key_p, sec_key_len ); |
kishino | 12:0254eaccfda2 | 199 | buf_len += sec_key_len; |
kishino | 12:0254eaccfda2 | 200 | } |
kishino | 12:0254eaccfda2 | 201 | } |
kishino | 12:0254eaccfda2 | 202 | |
kishino | 38:f13e4e563d65 | 203 | unsigned char *command_array_p = snic_core_p->getCommandBuf(); |
kishino | 12:0254eaccfda2 | 204 | // Preparation of command |
kishino | 24:987e412ae879 | 205 | command_len = snic_core_p->preparationSendCommand( UART_CMD_ID_WIFI, UART_CMD_SID_WIFI_JOIN_REQ, buf |
kishino | 38:f13e4e563d65 | 206 | , buf_len, payload_buf_p->buf, command_array_p ); |
kishino | 12:0254eaccfda2 | 207 | |
kishino | 12:0254eaccfda2 | 208 | // Send uart command request |
kishino | 38:f13e4e563d65 | 209 | snic_core_p->sendUart( command_len, command_array_p ); |
kishino | 12:0254eaccfda2 | 210 | |
kishino | 12:0254eaccfda2 | 211 | int ret; |
kishino | 12:0254eaccfda2 | 212 | // Wait UART response |
kishino | 16:6100acfeb1f1 | 213 | ret = uartCmdMgr_p->wait(); |
kishino | 43:d80bbb12ffe6 | 214 | if(uartCmdMgr_p->getCommandStatus() != UART_CMD_RES_WIFI_ERR_ALREADY_JOINED) |
kishino | 43:d80bbb12ffe6 | 215 | { |
kishino | 43:d80bbb12ffe6 | 216 | DEBUG_PRINT( "Already connected\r\n" ); |
kishino | 43:d80bbb12ffe6 | 217 | } |
kishino | 43:d80bbb12ffe6 | 218 | else |
kishino | 12:0254eaccfda2 | 219 | { |
kishino | 43:d80bbb12ffe6 | 220 | if( ret != 0 ) |
kishino | 43:d80bbb12ffe6 | 221 | { |
kishino | 43:d80bbb12ffe6 | 222 | DEBUG_PRINT( "join failed\r\n" ); |
kishino | 43:d80bbb12ffe6 | 223 | snic_core_p->freeCmdBuf( payload_buf_p ); |
kishino | 43:d80bbb12ffe6 | 224 | return -1; |
kishino | 43:d80bbb12ffe6 | 225 | } |
kishino | 12:0254eaccfda2 | 226 | } |
kishino | 12:0254eaccfda2 | 227 | |
kishino | 43:d80bbb12ffe6 | 228 | if(uartCmdMgr_p->getCommandStatus() != 0) |
kishino | 12:0254eaccfda2 | 229 | { |
kishino | 40:b6b10c22a121 | 230 | DEBUG_PRINT("join status:%02x\r\n", uartCmdMgr_p->getCommandStatus()); |
kishino | 39:a1233ca02edf | 231 | snic_core_p->freeCmdBuf( payload_buf_p ); |
kishino | 39:a1233ca02edf | 232 | return -1; |
kishino | 12:0254eaccfda2 | 233 | } |
kishino | 38:f13e4e563d65 | 234 | snic_core_p->freeCmdBuf( payload_buf_p ); |
kishino | 12:0254eaccfda2 | 235 | |
kishino | 12:0254eaccfda2 | 236 | return ret; |
kishino | 12:0254eaccfda2 | 237 | } |
kishino | 12:0254eaccfda2 | 238 | |
kishino | 12:0254eaccfda2 | 239 | int C_SNIC_WifiInterface::disconnect() |
kishino | 12:0254eaccfda2 | 240 | { |
kishino | 24:987e412ae879 | 241 | C_SNIC_Core *snic_core_p = C_SNIC_Core::getInstance(); |
kishino | 24:987e412ae879 | 242 | C_SNIC_UartCommandManager *uartCmdMgr_p = snic_core_p->getUartCommand(); |
kishino | 16:6100acfeb1f1 | 243 | |
kishino | 12:0254eaccfda2 | 244 | // Get buffer for response payload from MemoryPool |
kishino | 38:f13e4e563d65 | 245 | tagMEMPOOL_BLOCK_T *payload_buf_p = snic_core_p->allocCmdBuf(); |
kishino | 38:f13e4e563d65 | 246 | if( payload_buf_p == NULL ) |
kishino | 12:0254eaccfda2 | 247 | { |
kishino | 40:b6b10c22a121 | 248 | DEBUG_PRINT("disconnect payload_buf_p NULL\r\n"); |
kishino | 12:0254eaccfda2 | 249 | return -1; |
kishino | 12:0254eaccfda2 | 250 | } |
kishino | 12:0254eaccfda2 | 251 | |
kishino | 22:a9ec0cad4f84 | 252 | C_SNIC_Core::tagWIFI_DISCONNECT_REQ_T req; |
kishino | 12:0254eaccfda2 | 253 | // Make request |
kishino | 12:0254eaccfda2 | 254 | req.cmd_sid = UART_CMD_SID_WIFI_DISCONNECT_REQ; |
kishino | 12:0254eaccfda2 | 255 | req.seq = mUartRequestSeq++; |
kishino | 12:0254eaccfda2 | 256 | |
kishino | 38:f13e4e563d65 | 257 | unsigned char *command_array_p = snic_core_p->getCommandBuf(); |
kishino | 12:0254eaccfda2 | 258 | unsigned int command_len; |
kishino | 12:0254eaccfda2 | 259 | // Preparation of command |
kishino | 24:987e412ae879 | 260 | command_len = snic_core_p->preparationSendCommand( UART_CMD_ID_WIFI, req.cmd_sid, (unsigned char *)&req |
kishino | 38:f13e4e563d65 | 261 | , sizeof(C_SNIC_Core::tagWIFI_DISCONNECT_REQ_T), payload_buf_p->buf, command_array_p ); |
kishino | 12:0254eaccfda2 | 262 | |
kishino | 12:0254eaccfda2 | 263 | // Send uart command request |
kishino | 38:f13e4e563d65 | 264 | snic_core_p->sendUart( command_len, command_array_p ); |
kishino | 12:0254eaccfda2 | 265 | |
kishino | 12:0254eaccfda2 | 266 | int ret; |
kishino | 12:0254eaccfda2 | 267 | // Wait UART response |
kishino | 16:6100acfeb1f1 | 268 | ret = uartCmdMgr_p->wait(); |
kishino | 12:0254eaccfda2 | 269 | if( ret != 0 ) |
kishino | 12:0254eaccfda2 | 270 | { |
kishino | 40:b6b10c22a121 | 271 | DEBUG_PRINT( "disconnect failed\r\n" ); |
kishino | 38:f13e4e563d65 | 272 | snic_core_p->freeCmdBuf( payload_buf_p ); |
kishino | 12:0254eaccfda2 | 273 | return -1; |
kishino | 12:0254eaccfda2 | 274 | } |
kishino | 12:0254eaccfda2 | 275 | |
kishino | 16:6100acfeb1f1 | 276 | if( uartCmdMgr_p->getCommandStatus() != 0 ) |
kishino | 12:0254eaccfda2 | 277 | { |
kishino | 40:b6b10c22a121 | 278 | DEBUG_PRINT("disconnect status:%02x\r\n", uartCmdMgr_p->getCommandStatus()); |
kishino | 12:0254eaccfda2 | 279 | ret = -1; |
kishino | 12:0254eaccfda2 | 280 | } |
kishino | 38:f13e4e563d65 | 281 | snic_core_p->freeCmdBuf( payload_buf_p ); |
kishino | 12:0254eaccfda2 | 282 | return ret; |
kishino | 12:0254eaccfda2 | 283 | } |
kishino | 12:0254eaccfda2 | 284 | |
kishino | 12:0254eaccfda2 | 285 | int C_SNIC_WifiInterface::scan( const char *ssid_p, unsigned char *bssid_p |
kishino | 12:0254eaccfda2 | 286 | , void (*result_handler_p)(tagSCAN_RESULT_T *scan_result) ) |
kishino | 12:0254eaccfda2 | 287 | { |
kishino | 24:987e412ae879 | 288 | C_SNIC_Core *snic_core_p = C_SNIC_Core::getInstance(); |
kishino | 24:987e412ae879 | 289 | C_SNIC_UartCommandManager *uartCmdMgr_p = snic_core_p->getUartCommand(); |
kishino | 16:6100acfeb1f1 | 290 | |
kishino | 12:0254eaccfda2 | 291 | // Get buffer for response payload from MemoryPool |
kishino | 38:f13e4e563d65 | 292 | tagMEMPOOL_BLOCK_T *payload_buf_p = snic_core_p->allocCmdBuf(); |
kishino | 38:f13e4e563d65 | 293 | if( payload_buf_p == NULL ) |
kishino | 12:0254eaccfda2 | 294 | { |
kishino | 40:b6b10c22a121 | 295 | DEBUG_PRINT("scan payload_buf_p NULL\r\n"); |
kishino | 12:0254eaccfda2 | 296 | return -1; |
kishino | 12:0254eaccfda2 | 297 | } |
kishino | 12:0254eaccfda2 | 298 | |
kishino | 22:a9ec0cad4f84 | 299 | C_SNIC_Core::tagWIFI_SCAN_REQ_T req; |
kishino | 12:0254eaccfda2 | 300 | unsigned int buf_len = 0; |
kishino | 12:0254eaccfda2 | 301 | |
kishino | 22:a9ec0cad4f84 | 302 | memset( &req, 0, sizeof(C_SNIC_Core::tagWIFI_SCAN_REQ_T) ); |
kishino | 12:0254eaccfda2 | 303 | // Make request |
kishino | 12:0254eaccfda2 | 304 | req.cmd_sid = UART_CMD_SID_WIFI_SCAN_REQ; |
kishino | 12:0254eaccfda2 | 305 | buf_len++; |
kishino | 12:0254eaccfda2 | 306 | req.seq = mUartRequestSeq++; |
kishino | 12:0254eaccfda2 | 307 | buf_len++; |
kishino | 12:0254eaccfda2 | 308 | |
kishino | 12:0254eaccfda2 | 309 | // Set scan type(Active scan) |
kishino | 12:0254eaccfda2 | 310 | req.scan_type = 0; |
kishino | 12:0254eaccfda2 | 311 | buf_len++; |
kishino | 12:0254eaccfda2 | 312 | // Set bss type(any) |
kishino | 12:0254eaccfda2 | 313 | req.bss_type = 2; |
kishino | 12:0254eaccfda2 | 314 | buf_len++; |
kishino | 12:0254eaccfda2 | 315 | // Set BSSID |
kishino | 12:0254eaccfda2 | 316 | if( bssid_p != NULL ) |
kishino | 12:0254eaccfda2 | 317 | { |
kishino | 12:0254eaccfda2 | 318 | memcpy( req.bssid, bssid_p, BSSID_MAC_LENTH ); |
kishino | 12:0254eaccfda2 | 319 | } |
kishino | 12:0254eaccfda2 | 320 | buf_len += BSSID_MAC_LENTH; |
kishino | 12:0254eaccfda2 | 321 | // Set channel list(0) |
kishino | 12:0254eaccfda2 | 322 | req.chan_list = 0; |
kishino | 12:0254eaccfda2 | 323 | buf_len++; |
kishino | 12:0254eaccfda2 | 324 | //Set SSID |
kishino | 12:0254eaccfda2 | 325 | if( ssid_p != NULL ) |
kishino | 12:0254eaccfda2 | 326 | { |
kishino | 12:0254eaccfda2 | 327 | strcpy( (char *)req.ssid, ssid_p ); |
kishino | 12:0254eaccfda2 | 328 | buf_len += strlen(ssid_p); |
kishino | 12:0254eaccfda2 | 329 | } |
kishino | 12:0254eaccfda2 | 330 | buf_len++; |
kishino | 12:0254eaccfda2 | 331 | |
kishino | 38:f13e4e563d65 | 332 | unsigned char *command_array_p = snic_core_p->getCommandBuf(); |
kishino | 12:0254eaccfda2 | 333 | unsigned int command_len; |
kishino | 12:0254eaccfda2 | 334 | // Preparation of command |
kishino | 24:987e412ae879 | 335 | command_len = snic_core_p->preparationSendCommand( UART_CMD_ID_WIFI, req.cmd_sid, (unsigned char *)&req |
kishino | 38:f13e4e563d65 | 336 | , buf_len, payload_buf_p->buf, command_array_p ); |
kishino | 12:0254eaccfda2 | 337 | |
kishino | 12:0254eaccfda2 | 338 | // Set scan result callback |
kishino | 16:6100acfeb1f1 | 339 | uartCmdMgr_p->setScanResultHandler( result_handler_p ); |
kishino | 12:0254eaccfda2 | 340 | |
kishino | 12:0254eaccfda2 | 341 | // Send uart command request |
kishino | 38:f13e4e563d65 | 342 | snic_core_p->sendUart( command_len, command_array_p ); |
kishino | 12:0254eaccfda2 | 343 | |
kishino | 12:0254eaccfda2 | 344 | int ret; |
kishino | 12:0254eaccfda2 | 345 | // Wait UART response |
kishino | 16:6100acfeb1f1 | 346 | ret = uartCmdMgr_p->wait(); |
kishino | 40:b6b10c22a121 | 347 | DEBUG_PRINT( "scan wait:%d\r\n", ret ); |
kishino | 12:0254eaccfda2 | 348 | if( ret != 0 ) |
kishino | 12:0254eaccfda2 | 349 | { |
kishino | 40:b6b10c22a121 | 350 | DEBUG_PRINT( "scan failed\r\n" ); |
kishino | 38:f13e4e563d65 | 351 | snic_core_p->freeCmdBuf( payload_buf_p ); |
kishino | 12:0254eaccfda2 | 352 | return -1; |
kishino | 12:0254eaccfda2 | 353 | } |
kishino | 12:0254eaccfda2 | 354 | |
kishino | 16:6100acfeb1f1 | 355 | if( uartCmdMgr_p->getCommandStatus() != 0 ) |
kishino | 12:0254eaccfda2 | 356 | { |
kishino | 40:b6b10c22a121 | 357 | DEBUG_PRINT("scan status:%02x\r\n", uartCmdMgr_p->getCommandStatus()); |
kishino | 39:a1233ca02edf | 358 | snic_core_p->freeCmdBuf( payload_buf_p ); |
kishino | 39:a1233ca02edf | 359 | return -1; |
kishino | 12:0254eaccfda2 | 360 | } |
kishino | 12:0254eaccfda2 | 361 | |
kishino | 38:f13e4e563d65 | 362 | snic_core_p->freeCmdBuf( payload_buf_p ); |
kishino | 12:0254eaccfda2 | 363 | |
kishino | 12:0254eaccfda2 | 364 | return ret; |
kishino | 12:0254eaccfda2 | 365 | } |
kishino | 12:0254eaccfda2 | 366 | |
kishino | 12:0254eaccfda2 | 367 | int C_SNIC_WifiInterface::wifi_on( const char *country_p ) |
kishino | 12:0254eaccfda2 | 368 | { |
kishino | 24:987e412ae879 | 369 | C_SNIC_Core *snic_core_p = C_SNIC_Core::getInstance(); |
kishino | 24:987e412ae879 | 370 | C_SNIC_UartCommandManager *uartCmdMgr_p = snic_core_p->getUartCommand(); |
kishino | 16:6100acfeb1f1 | 371 | |
kishino | 12:0254eaccfda2 | 372 | // Parameter check |
kishino | 12:0254eaccfda2 | 373 | if( country_p == NULL ) |
kishino | 12:0254eaccfda2 | 374 | { |
kishino | 40:b6b10c22a121 | 375 | DEBUG_PRINT("wifi_on parameter error\r\n"); |
kishino | 12:0254eaccfda2 | 376 | return -1; |
kishino | 12:0254eaccfda2 | 377 | } |
kishino | 12:0254eaccfda2 | 378 | |
kishino | 12:0254eaccfda2 | 379 | // Get buffer for response payload from MemoryPool |
kishino | 38:f13e4e563d65 | 380 | tagMEMPOOL_BLOCK_T *payload_buf_p = snic_core_p->allocCmdBuf(); |
kishino | 38:f13e4e563d65 | 381 | if( payload_buf_p == NULL ) |
kishino | 12:0254eaccfda2 | 382 | { |
kishino | 40:b6b10c22a121 | 383 | DEBUG_PRINT("wifi_on payload_buf_p NULL\r\n"); |
kishino | 12:0254eaccfda2 | 384 | return -1; |
kishino | 12:0254eaccfda2 | 385 | } |
kishino | 12:0254eaccfda2 | 386 | |
kishino | 22:a9ec0cad4f84 | 387 | C_SNIC_Core::tagWIFI_ON_REQ_T req; |
kishino | 12:0254eaccfda2 | 388 | // Make request |
kishino | 12:0254eaccfda2 | 389 | req.cmd_sid = UART_CMD_SID_WIFI_ON_REQ; |
kishino | 12:0254eaccfda2 | 390 | req.seq = mUartRequestSeq++; |
kishino | 12:0254eaccfda2 | 391 | memcpy( req.country, country_p, COUNTRYC_CODE_LENTH ); |
kishino | 12:0254eaccfda2 | 392 | |
kishino | 38:f13e4e563d65 | 393 | unsigned char *command_array_p = snic_core_p->getCommandBuf(); |
kishino | 12:0254eaccfda2 | 394 | unsigned int command_len; |
kishino | 12:0254eaccfda2 | 395 | // Preparation of command |
kishino | 24:987e412ae879 | 396 | command_len = snic_core_p->preparationSendCommand( UART_CMD_ID_WIFI, req.cmd_sid, (unsigned char *)&req |
kishino | 38:f13e4e563d65 | 397 | , sizeof(C_SNIC_Core::tagWIFI_ON_REQ_T), payload_buf_p->buf, command_array_p ); |
kishino | 12:0254eaccfda2 | 398 | |
kishino | 12:0254eaccfda2 | 399 | // Send uart command request |
kishino | 38:f13e4e563d65 | 400 | snic_core_p->sendUart( command_len, command_array_p ); |
kishino | 12:0254eaccfda2 | 401 | |
kishino | 12:0254eaccfda2 | 402 | int ret; |
kishino | 12:0254eaccfda2 | 403 | // Wait UART response |
kishino | 16:6100acfeb1f1 | 404 | ret = uartCmdMgr_p->wait(); |
kishino | 12:0254eaccfda2 | 405 | if( ret != 0 ) |
kishino | 12:0254eaccfda2 | 406 | { |
kishino | 40:b6b10c22a121 | 407 | DEBUG_PRINT( "wifi_on failed\r\n" ); |
kishino | 38:f13e4e563d65 | 408 | snic_core_p->freeCmdBuf( payload_buf_p ); |
kishino | 12:0254eaccfda2 | 409 | return -1; |
kishino | 12:0254eaccfda2 | 410 | } |
kishino | 12:0254eaccfda2 | 411 | |
kishino | 16:6100acfeb1f1 | 412 | if( uartCmdMgr_p->getCommandStatus() != 0 ) |
kishino | 12:0254eaccfda2 | 413 | { |
kishino | 40:b6b10c22a121 | 414 | DEBUG_PRINT("wifi_on status:%02x\r\n", uartCmdMgr_p->getCommandStatus()); |
kishino | 39:a1233ca02edf | 415 | snic_core_p->freeCmdBuf( payload_buf_p ); |
kishino | 39:a1233ca02edf | 416 | return -1; |
kishino | 12:0254eaccfda2 | 417 | } |
kishino | 38:f13e4e563d65 | 418 | snic_core_p->freeCmdBuf( payload_buf_p ); |
kishino | 12:0254eaccfda2 | 419 | |
kishino | 12:0254eaccfda2 | 420 | return ret; |
kishino | 12:0254eaccfda2 | 421 | } |
kishino | 12:0254eaccfda2 | 422 | |
kishino | 12:0254eaccfda2 | 423 | int C_SNIC_WifiInterface::wifi_off() |
kishino | 12:0254eaccfda2 | 424 | { |
kishino | 24:987e412ae879 | 425 | C_SNIC_Core *snic_core_p = C_SNIC_Core::getInstance(); |
kishino | 24:987e412ae879 | 426 | C_SNIC_UartCommandManager *uartCmdMgr_p = snic_core_p->getUartCommand(); |
kishino | 16:6100acfeb1f1 | 427 | |
kishino | 12:0254eaccfda2 | 428 | // Get buffer for response payload from MemoryPool |
kishino | 38:f13e4e563d65 | 429 | tagMEMPOOL_BLOCK_T *payload_buf_p = snic_core_p->allocCmdBuf(); |
kishino | 38:f13e4e563d65 | 430 | if( payload_buf_p == NULL ) |
kishino | 12:0254eaccfda2 | 431 | { |
kishino | 40:b6b10c22a121 | 432 | DEBUG_PRINT("wifi_off payload_buf_p NULL\r\n"); |
kishino | 12:0254eaccfda2 | 433 | return -1; |
kishino | 12:0254eaccfda2 | 434 | } |
kishino | 12:0254eaccfda2 | 435 | |
kishino | 22:a9ec0cad4f84 | 436 | C_SNIC_Core::tagWIFI_OFF_REQ_T req; |
kishino | 12:0254eaccfda2 | 437 | // Make request |
kishino | 12:0254eaccfda2 | 438 | req.cmd_sid = UART_CMD_SID_WIFI_OFF_REQ; |
kishino | 12:0254eaccfda2 | 439 | req.seq = mUartRequestSeq++; |
kishino | 12:0254eaccfda2 | 440 | |
kishino | 38:f13e4e563d65 | 441 | unsigned char *command_array_p = snic_core_p->getCommandBuf(); |
kishino | 12:0254eaccfda2 | 442 | unsigned int command_len; |
kishino | 12:0254eaccfda2 | 443 | // Preparation of command |
kishino | 24:987e412ae879 | 444 | command_len = snic_core_p->preparationSendCommand( UART_CMD_ID_WIFI, req.cmd_sid, (unsigned char *)&req |
kishino | 38:f13e4e563d65 | 445 | , sizeof(C_SNIC_Core::tagWIFI_OFF_REQ_T), payload_buf_p->buf, command_array_p ); |
kishino | 12:0254eaccfda2 | 446 | |
kishino | 12:0254eaccfda2 | 447 | // Send uart command request |
kishino | 38:f13e4e563d65 | 448 | snic_core_p->sendUart( command_len, command_array_p ); |
kishino | 12:0254eaccfda2 | 449 | |
kishino | 12:0254eaccfda2 | 450 | int ret; |
kishino | 12:0254eaccfda2 | 451 | // Wait UART response |
kishino | 16:6100acfeb1f1 | 452 | ret = uartCmdMgr_p->wait(); |
kishino | 12:0254eaccfda2 | 453 | if( ret != 0 ) |
kishino | 12:0254eaccfda2 | 454 | { |
kishino | 40:b6b10c22a121 | 455 | DEBUG_PRINT( "wifi_off failed\r\n" ); |
kishino | 38:f13e4e563d65 | 456 | snic_core_p->freeCmdBuf( payload_buf_p ); |
kishino | 12:0254eaccfda2 | 457 | return -1; |
kishino | 12:0254eaccfda2 | 458 | } |
kishino | 12:0254eaccfda2 | 459 | |
kishino | 16:6100acfeb1f1 | 460 | if( uartCmdMgr_p->getCommandStatus() != 0 ) |
kishino | 12:0254eaccfda2 | 461 | { |
kishino | 40:b6b10c22a121 | 462 | DEBUG_PRINT("wifi_off status:%02x\r\n", uartCmdMgr_p->getCommandStatus()); |
kishino | 39:a1233ca02edf | 463 | snic_core_p->freeCmdBuf( payload_buf_p ); |
kishino | 39:a1233ca02edf | 464 | return -1; |
kishino | 12:0254eaccfda2 | 465 | } |
kishino | 38:f13e4e563d65 | 466 | snic_core_p->freeCmdBuf( payload_buf_p ); |
kishino | 12:0254eaccfda2 | 467 | |
kishino | 12:0254eaccfda2 | 468 | return ret; |
kishino | 12:0254eaccfda2 | 469 | } |
kishino | 12:0254eaccfda2 | 470 | |
kishino | 12:0254eaccfda2 | 471 | int C_SNIC_WifiInterface::getRssi( signed char *rssi_p ) |
kishino | 12:0254eaccfda2 | 472 | { |
kishino | 24:987e412ae879 | 473 | C_SNIC_Core *snic_core_p = C_SNIC_Core::getInstance(); |
kishino | 24:987e412ae879 | 474 | C_SNIC_UartCommandManager *uartCmdMgr_p = snic_core_p->getUartCommand(); |
kishino | 12:0254eaccfda2 | 475 | if( rssi_p == NULL ) |
kishino | 12:0254eaccfda2 | 476 | { |
kishino | 40:b6b10c22a121 | 477 | DEBUG_PRINT("getRssi parameter error\r\n"); |
kishino | 12:0254eaccfda2 | 478 | return -1; |
kishino | 12:0254eaccfda2 | 479 | } |
kishino | 12:0254eaccfda2 | 480 | |
kishino | 12:0254eaccfda2 | 481 | // Get buffer for response payload from MemoryPool |
kishino | 38:f13e4e563d65 | 482 | tagMEMPOOL_BLOCK_T *payload_buf_p = snic_core_p->allocCmdBuf(); |
kishino | 38:f13e4e563d65 | 483 | if( payload_buf_p == NULL ) |
kishino | 12:0254eaccfda2 | 484 | { |
kishino | 40:b6b10c22a121 | 485 | DEBUG_PRINT("getRssi payload_buf_p NULL\r\n"); |
kishino | 12:0254eaccfda2 | 486 | return -1; |
kishino | 12:0254eaccfda2 | 487 | } |
kishino | 12:0254eaccfda2 | 488 | |
kishino | 22:a9ec0cad4f84 | 489 | C_SNIC_Core::tagWIFI_GET_STA_RSSI_REQ_T req; |
kishino | 12:0254eaccfda2 | 490 | |
kishino | 12:0254eaccfda2 | 491 | // Make request |
kishino | 12:0254eaccfda2 | 492 | req.cmd_sid = UART_CMD_SID_WIFI_GET_STA_RSSI_REQ; |
kishino | 12:0254eaccfda2 | 493 | req.seq = mUartRequestSeq++; |
kishino | 12:0254eaccfda2 | 494 | |
kishino | 38:f13e4e563d65 | 495 | unsigned char *command_array_p = snic_core_p->getCommandBuf(); |
kishino | 12:0254eaccfda2 | 496 | unsigned int command_len; |
kishino | 24:987e412ae879 | 497 | command_len = snic_core_p->preparationSendCommand( UART_CMD_ID_WIFI, req.cmd_sid, (unsigned char *)&req |
kishino | 38:f13e4e563d65 | 498 | , sizeof(C_SNIC_Core::tagWIFI_GET_STA_RSSI_REQ_T), payload_buf_p->buf, command_array_p ); |
kishino | 12:0254eaccfda2 | 499 | |
kishino | 12:0254eaccfda2 | 500 | int ret; |
kishino | 12:0254eaccfda2 | 501 | // Send uart command request |
kishino | 38:f13e4e563d65 | 502 | snic_core_p->sendUart( command_len, command_array_p ); |
kishino | 12:0254eaccfda2 | 503 | |
kishino | 12:0254eaccfda2 | 504 | // Wait UART response |
kishino | 16:6100acfeb1f1 | 505 | ret = uartCmdMgr_p->wait(); |
kishino | 12:0254eaccfda2 | 506 | if( ret != 0 ) |
kishino | 12:0254eaccfda2 | 507 | { |
kishino | 40:b6b10c22a121 | 508 | DEBUG_PRINT( "getRssi failed\r\n" ); |
kishino | 38:f13e4e563d65 | 509 | snic_core_p->freeCmdBuf( payload_buf_p ); |
kishino | 12:0254eaccfda2 | 510 | return -1; |
kishino | 12:0254eaccfda2 | 511 | } |
kishino | 12:0254eaccfda2 | 512 | |
kishino | 38:f13e4e563d65 | 513 | *rssi_p = (signed char)payload_buf_p->buf[2]; |
kishino | 12:0254eaccfda2 | 514 | |
kishino | 38:f13e4e563d65 | 515 | snic_core_p->freeCmdBuf( payload_buf_p ); |
kishino | 12:0254eaccfda2 | 516 | return 0; |
kishino | 12:0254eaccfda2 | 517 | } |
kishino | 12:0254eaccfda2 | 518 | |
kishino | 12:0254eaccfda2 | 519 | int C_SNIC_WifiInterface::getWifiStatus( tagWIFI_STATUS_T *status_p) |
kishino | 12:0254eaccfda2 | 520 | { |
kishino | 24:987e412ae879 | 521 | C_SNIC_Core *snic_core_p = C_SNIC_Core::getInstance(); |
kishino | 24:987e412ae879 | 522 | C_SNIC_UartCommandManager *uartCmdMgr_p = snic_core_p->getUartCommand(); |
kishino | 16:6100acfeb1f1 | 523 | |
kishino | 12:0254eaccfda2 | 524 | if( status_p == NULL ) |
kishino | 12:0254eaccfda2 | 525 | { |
kishino | 40:b6b10c22a121 | 526 | DEBUG_PRINT("getWifiStatus parameter error\r\n"); |
kishino | 12:0254eaccfda2 | 527 | return -1; |
kishino | 12:0254eaccfda2 | 528 | } |
kishino | 12:0254eaccfda2 | 529 | |
kishino | 12:0254eaccfda2 | 530 | // Get buffer for response payload from MemoryPool |
kishino | 38:f13e4e563d65 | 531 | tagMEMPOOL_BLOCK_T *payload_buf_p = snic_core_p->allocCmdBuf(); |
kishino | 38:f13e4e563d65 | 532 | if( payload_buf_p == NULL ) |
kishino | 12:0254eaccfda2 | 533 | { |
kishino | 40:b6b10c22a121 | 534 | DEBUG_PRINT("getWifiStatus payload_buf_p NULL\r\n"); |
kishino | 12:0254eaccfda2 | 535 | return -1; |
kishino | 12:0254eaccfda2 | 536 | } |
kishino | 12:0254eaccfda2 | 537 | |
kishino | 22:a9ec0cad4f84 | 538 | C_SNIC_Core::tagWIFI_GET_STATUS_REQ_T req; |
kishino | 12:0254eaccfda2 | 539 | // Make request |
kishino | 12:0254eaccfda2 | 540 | req.cmd_sid = UART_CMD_SID_WIFI_GET_STATUS_REQ; |
kishino | 12:0254eaccfda2 | 541 | req.seq = mUartRequestSeq++; |
kishino | 12:0254eaccfda2 | 542 | req.interface = 0; |
kishino | 12:0254eaccfda2 | 543 | |
kishino | 38:f13e4e563d65 | 544 | unsigned char *command_array_p = snic_core_p->getCommandBuf(); |
kishino | 12:0254eaccfda2 | 545 | unsigned int command_len; |
kishino | 24:987e412ae879 | 546 | command_len = snic_core_p->preparationSendCommand( UART_CMD_ID_WIFI, req.cmd_sid, (unsigned char *)&req |
kishino | 38:f13e4e563d65 | 547 | , sizeof(C_SNIC_Core::tagWIFI_GET_STATUS_REQ_T), payload_buf_p->buf, command_array_p ); |
kishino | 12:0254eaccfda2 | 548 | |
kishino | 12:0254eaccfda2 | 549 | // Send uart command request |
kishino | 38:f13e4e563d65 | 550 | snic_core_p->sendUart( command_len, command_array_p ); |
kishino | 12:0254eaccfda2 | 551 | |
kishino | 12:0254eaccfda2 | 552 | int ret; |
kishino | 12:0254eaccfda2 | 553 | // Wait UART response |
kishino | 16:6100acfeb1f1 | 554 | ret = uartCmdMgr_p->wait(); |
kishino | 12:0254eaccfda2 | 555 | if( ret != 0 ) |
kishino | 12:0254eaccfda2 | 556 | { |
kishino | 40:b6b10c22a121 | 557 | DEBUG_PRINT( "getWifiStatus failed\r\n" ); |
kishino | 38:f13e4e563d65 | 558 | snic_core_p->freeCmdBuf( payload_buf_p ); |
kishino | 12:0254eaccfda2 | 559 | return -1; |
kishino | 12:0254eaccfda2 | 560 | } |
kishino | 12:0254eaccfda2 | 561 | |
kishino | 12:0254eaccfda2 | 562 | // set status |
kishino | 38:f13e4e563d65 | 563 | status_p->status = (E_WIFI_STATUS)payload_buf_p->buf[2]; |
kishino | 12:0254eaccfda2 | 564 | |
kishino | 12:0254eaccfda2 | 565 | // set Mac address |
kishino | 12:0254eaccfda2 | 566 | if( status_p->status != e_STATUS_OFF ) |
kishino | 12:0254eaccfda2 | 567 | { |
kishino | 38:f13e4e563d65 | 568 | memcpy( status_p->mac_address, &payload_buf_p->buf[3], BSSID_MAC_LENTH ); |
kishino | 12:0254eaccfda2 | 569 | } |
kishino | 12:0254eaccfda2 | 570 | |
kishino | 12:0254eaccfda2 | 571 | // set SSID |
kishino | 37:f3a2053627c2 | 572 | if( ( status_p->status == e_STA_JOINED ) || ( status_p->status == e_AP_STARTED ) ) |
kishino | 12:0254eaccfda2 | 573 | { |
kishino | 38:f13e4e563d65 | 574 | memcpy( status_p->ssid, &payload_buf_p->buf[9], strlen( (char *)&payload_buf_p->buf[9]) ); |
kishino | 12:0254eaccfda2 | 575 | } |
kishino | 12:0254eaccfda2 | 576 | |
kishino | 38:f13e4e563d65 | 577 | snic_core_p->freeCmdBuf( payload_buf_p ); |
kishino | 12:0254eaccfda2 | 578 | return 0; |
kishino | 12:0254eaccfda2 | 579 | } |
kishino | 31:15c22824cc46 | 580 | |
kishino | 31:15c22824cc46 | 581 | int C_SNIC_WifiInterface::setIPConfig( bool is_DHCP |
kishino | 31:15c22824cc46 | 582 | , const char *ip_p, const char *mask_p, const char *gateway_p ) |
kishino | 31:15c22824cc46 | 583 | { |
kishino | 31:15c22824cc46 | 584 | // Parameter check |
kishino | 31:15c22824cc46 | 585 | if( is_DHCP == false ) |
kishino | 31:15c22824cc46 | 586 | { |
kishino | 31:15c22824cc46 | 587 | if( (ip_p == NULL) || (mask_p == NULL) ||(gateway_p == NULL) ) |
kishino | 31:15c22824cc46 | 588 | { |
kishino | 40:b6b10c22a121 | 589 | DEBUG_PRINT("setIPConfig parameter error\r\n"); |
kishino | 31:15c22824cc46 | 590 | return -1; |
kishino | 31:15c22824cc46 | 591 | } |
kishino | 31:15c22824cc46 | 592 | } |
kishino | 31:15c22824cc46 | 593 | |
kishino | 31:15c22824cc46 | 594 | C_SNIC_Core *snic_core_p = C_SNIC_Core::getInstance(); |
kishino | 31:15c22824cc46 | 595 | C_SNIC_UartCommandManager *uartCmdMgr_p = snic_core_p->getUartCommand(); |
kishino | 31:15c22824cc46 | 596 | |
kishino | 31:15c22824cc46 | 597 | // Get buffer for response payload from MemoryPool |
kishino | 38:f13e4e563d65 | 598 | tagMEMPOOL_BLOCK_T *payload_buf_p = snic_core_p->allocCmdBuf(); |
kishino | 38:f13e4e563d65 | 599 | if( payload_buf_p == NULL ) |
kishino | 31:15c22824cc46 | 600 | { |
kishino | 40:b6b10c22a121 | 601 | DEBUG_PRINT("setIPConfig payload_buf_p NULL\r\n"); |
kishino | 31:15c22824cc46 | 602 | return -1; |
kishino | 31:15c22824cc46 | 603 | } |
kishino | 31:15c22824cc46 | 604 | |
kishino | 38:f13e4e563d65 | 605 | unsigned char *command_array_p = snic_core_p->getCommandBuf(); |
kishino | 31:15c22824cc46 | 606 | unsigned int command_len; |
kishino | 31:15c22824cc46 | 607 | if( is_DHCP == true ) |
kishino | 31:15c22824cc46 | 608 | { |
kishino | 31:15c22824cc46 | 609 | C_SNIC_Core::tagSNIC_IP_CONFIG_REQ_DHCP_T req; |
kishino | 31:15c22824cc46 | 610 | // Make request |
kishino | 31:15c22824cc46 | 611 | req.cmd_sid = UART_CMD_SID_SNIC_IP_CONFIG_REQ; |
kishino | 31:15c22824cc46 | 612 | req.seq = mUartRequestSeq++; |
kishino | 31:15c22824cc46 | 613 | req.interface = 0; |
kishino | 31:15c22824cc46 | 614 | req.dhcp = 1; |
kishino | 31:15c22824cc46 | 615 | |
kishino | 31:15c22824cc46 | 616 | // Preparation of command |
kishino | 31:15c22824cc46 | 617 | command_len = snic_core_p->preparationSendCommand( UART_CMD_ID_SNIC, req.cmd_sid, (unsigned char *)&req |
kishino | 38:f13e4e563d65 | 618 | , sizeof(C_SNIC_Core::tagSNIC_IP_CONFIG_REQ_DHCP_T), payload_buf_p->buf, command_array_p ); |
kishino | 31:15c22824cc46 | 619 | } |
kishino | 31:15c22824cc46 | 620 | else |
kishino | 31:15c22824cc46 | 621 | { |
kishino | 31:15c22824cc46 | 622 | C_SNIC_Core::tagSNIC_IP_CONFIG_REQ_STATIC_T req; |
kishino | 31:15c22824cc46 | 623 | // Make request |
kishino | 31:15c22824cc46 | 624 | req.cmd_sid = UART_CMD_SID_SNIC_IP_CONFIG_REQ; |
kishino | 31:15c22824cc46 | 625 | req.seq = mUartRequestSeq++; |
kishino | 31:15c22824cc46 | 626 | req.interface = 0; |
kishino | 31:15c22824cc46 | 627 | req.dhcp = 0; |
kishino | 31:15c22824cc46 | 628 | |
kishino | 31:15c22824cc46 | 629 | // Set paramter of address |
kishino | 31:15c22824cc46 | 630 | int addr_temp; |
kishino | 31:15c22824cc46 | 631 | addr_temp = C_SNIC_UartMsgUtil::addrToInteger( ip_p ); |
kishino | 31:15c22824cc46 | 632 | C_SNIC_UartMsgUtil::convertIntToByteAdday( addr_temp, (char *)req.ip_addr ); |
kishino | 31:15c22824cc46 | 633 | addr_temp = C_SNIC_UartMsgUtil::addrToInteger( mask_p ); |
kishino | 31:15c22824cc46 | 634 | C_SNIC_UartMsgUtil::convertIntToByteAdday( addr_temp, (char *)req.netmask ); |
kishino | 31:15c22824cc46 | 635 | addr_temp = C_SNIC_UartMsgUtil::addrToInteger( gateway_p ); |
kishino | 31:15c22824cc46 | 636 | C_SNIC_UartMsgUtil::convertIntToByteAdday( addr_temp, (char *)req.gateway ); |
kishino | 31:15c22824cc46 | 637 | |
kishino | 31:15c22824cc46 | 638 | // Preparation of command |
kishino | 31:15c22824cc46 | 639 | command_len = snic_core_p->preparationSendCommand( UART_CMD_ID_SNIC, req.cmd_sid, (unsigned char *)&req |
kishino | 38:f13e4e563d65 | 640 | , sizeof(C_SNIC_Core::tagSNIC_IP_CONFIG_REQ_STATIC_T), payload_buf_p->buf, command_array_p ); |
kishino | 31:15c22824cc46 | 641 | } |
kishino | 31:15c22824cc46 | 642 | // Send uart command request |
kishino | 38:f13e4e563d65 | 643 | snic_core_p->sendUart( command_len, command_array_p ); |
kishino | 31:15c22824cc46 | 644 | |
kishino | 31:15c22824cc46 | 645 | int ret; |
kishino | 31:15c22824cc46 | 646 | // Wait UART response |
kishino | 31:15c22824cc46 | 647 | ret = uartCmdMgr_p->wait(); |
kishino | 31:15c22824cc46 | 648 | if( ret != 0 ) |
kishino | 31:15c22824cc46 | 649 | { |
kishino | 40:b6b10c22a121 | 650 | DEBUG_PRINT( "setIPConfig failed\r\n" ); |
kishino | 38:f13e4e563d65 | 651 | snic_core_p->freeCmdBuf( payload_buf_p ); |
kishino | 31:15c22824cc46 | 652 | return -1; |
kishino | 31:15c22824cc46 | 653 | } |
kishino | 31:15c22824cc46 | 654 | |
kishino | 31:15c22824cc46 | 655 | if( uartCmdMgr_p->getCommandStatus() != 0 ) |
kishino | 31:15c22824cc46 | 656 | { |
kishino | 40:b6b10c22a121 | 657 | DEBUG_PRINT("setIPConfig status:%02x\r\n", uartCmdMgr_p->getCommandStatus()); |
kishino | 39:a1233ca02edf | 658 | snic_core_p->freeCmdBuf( payload_buf_p ); |
kishino | 39:a1233ca02edf | 659 | return -1; |
kishino | 31:15c22824cc46 | 660 | } |
kishino | 31:15c22824cc46 | 661 | |
kishino | 38:f13e4e563d65 | 662 | snic_core_p->freeCmdBuf( payload_buf_p ); |
kishino | 36:f33fcf5975ab | 663 | return ret; |
kishino | 31:15c22824cc46 | 664 | } |