SNICInterface for ThingPlug Example
Dependents: ThingPlug_WiFi_Example ThingPlug_WiFi_Example_2
Fork of SNICInterface by
SNIC_WifiInterface.cpp@53:b53ccb9989c4, 2015-03-31 (annotated)
- Committer:
- MACRUM
- Date:
- Tue Mar 31 03:16:46 2015 +0000
- Revision:
- 53:b53ccb9989c4
- Parent:
- 51:69a9cf901d54
Added default constructor and create() method
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]; |
MACRUM | 51:69a9cf901d54 | 24 | static char ip_addr[17] = "\0"; |
kishino | 12:0254eaccfda2 | 25 | |
kishino | 12:0254eaccfda2 | 26 | C_SNIC_WifiInterface::C_SNIC_WifiInterface( PinName tx, PinName rx, PinName cts, PinName rts, PinName reset, PinName alarm, int baud) |
kishino | 12:0254eaccfda2 | 27 | { |
kishino | 12:0254eaccfda2 | 28 | mUART_tx = tx; |
kishino | 12:0254eaccfda2 | 29 | mUART_rx = rx; |
kishino | 12:0254eaccfda2 | 30 | mUART_cts = cts; |
kishino | 12:0254eaccfda2 | 31 | mUART_rts = rts;; |
kishino | 12:0254eaccfda2 | 32 | mUART_baud = baud; |
kishino | 12:0254eaccfda2 | 33 | mModuleReset = reset; |
kishino | 12:0254eaccfda2 | 34 | } |
kishino | 12:0254eaccfda2 | 35 | |
MACRUM | 53:b53ccb9989c4 | 36 | void C_SNIC_WifiInterface::create( PinName tx, PinName rx, PinName cts, PinName rts, PinName reset, PinName alarm, int baud) |
MACRUM | 53:b53ccb9989c4 | 37 | { |
MACRUM | 53:b53ccb9989c4 | 38 | mUART_tx = tx; |
MACRUM | 53:b53ccb9989c4 | 39 | mUART_rx = rx; |
MACRUM | 53:b53ccb9989c4 | 40 | mUART_cts = cts; |
MACRUM | 53:b53ccb9989c4 | 41 | mUART_rts = rts;; |
MACRUM | 53:b53ccb9989c4 | 42 | mUART_baud = baud; |
MACRUM | 53:b53ccb9989c4 | 43 | mModuleReset = reset; |
MACRUM | 53:b53ccb9989c4 | 44 | } |
MACRUM | 53:b53ccb9989c4 | 45 | |
kishino | 26:f2e1030964e4 | 46 | C_SNIC_WifiInterface::~C_SNIC_WifiInterface() |
kishino | 26:f2e1030964e4 | 47 | { |
kishino | 26:f2e1030964e4 | 48 | } |
kishino | 26:f2e1030964e4 | 49 | |
kishino | 12:0254eaccfda2 | 50 | int C_SNIC_WifiInterface::init() |
kishino | 24:987e412ae879 | 51 | { |
kishino | 24:987e412ae879 | 52 | C_SNIC_Core *snic_core_p = C_SNIC_Core::getInstance(); |
kishino | 24:987e412ae879 | 53 | C_SNIC_UartCommandManager *uartCmdMgr_p = snic_core_p->getUartCommand(); |
kishino | 16:6100acfeb1f1 | 54 | |
kishino | 37:f3a2053627c2 | 55 | /* Initialize UART */ |
kishino | 37:f3a2053627c2 | 56 | snic_core_p->initUart( mUART_tx, mUART_rx, mUART_baud ); |
kishino | 37:f3a2053627c2 | 57 | |
kishino | 29:6a0ba999597d | 58 | /* Module reset */ |
kishino | 29:6a0ba999597d | 59 | snic_core_p->resetModule( mModuleReset ); |
kishino | 29:6a0ba999597d | 60 | |
kishino | 37:f3a2053627c2 | 61 | wait(1); |
kishino | 12:0254eaccfda2 | 62 | /* Initialize SNIC API */ |
kishino | 12:0254eaccfda2 | 63 | // Get buffer for response payload from MemoryPool |
kishino | 38:f13e4e563d65 | 64 | tagMEMPOOL_BLOCK_T *payload_buf_p = snic_core_p->allocCmdBuf(); |
kishino | 38:f13e4e563d65 | 65 | if( payload_buf_p == NULL ) |
kishino | 12:0254eaccfda2 | 66 | { |
kishino | 40:b6b10c22a121 | 67 | DEBUG_PRINT("snic_init payload_buf_p NULL\r\n"); |
kishino | 12:0254eaccfda2 | 68 | return -1; |
kishino | 12:0254eaccfda2 | 69 | } |
kishino | 12:0254eaccfda2 | 70 | |
kishino | 22:a9ec0cad4f84 | 71 | C_SNIC_Core::tagSNIC_INIT_REQ_T req; |
kishino | 12:0254eaccfda2 | 72 | // Make request |
kishino | 12:0254eaccfda2 | 73 | req.cmd_sid = UART_CMD_SID_SNIC_INIT_REQ; |
kishino | 12:0254eaccfda2 | 74 | req.seq = mUartRequestSeq++; |
kishino | 28:b796031f6519 | 75 | req.buf_size[0] = 0x08; |
kishino | 28:b796031f6519 | 76 | req.buf_size[1] = 0x00; |
kishino | 12:0254eaccfda2 | 77 | |
kishino | 38:f13e4e563d65 | 78 | unsigned char *command_array_p = snic_core_p->getCommandBuf(); |
kishino | 12:0254eaccfda2 | 79 | unsigned int command_len; |
kishino | 12:0254eaccfda2 | 80 | // Preparation of command |
kishino | 24:987e412ae879 | 81 | command_len = snic_core_p->preparationSendCommand( UART_CMD_ID_SNIC, req.cmd_sid, (unsigned char *)&req |
kishino | 38:f13e4e563d65 | 82 | , sizeof(C_SNIC_Core::tagSNIC_INIT_REQ_T), payload_buf_p->buf, command_array_p ); |
kishino | 12:0254eaccfda2 | 83 | |
kishino | 12:0254eaccfda2 | 84 | // Send uart command request |
kishino | 38:f13e4e563d65 | 85 | snic_core_p->sendUart( command_len, command_array_p ); |
kishino | 12:0254eaccfda2 | 86 | |
kishino | 12:0254eaccfda2 | 87 | int ret; |
kishino | 12:0254eaccfda2 | 88 | // Wait UART response |
kishino | 16:6100acfeb1f1 | 89 | ret = uartCmdMgr_p->wait(); |
kishino | 12:0254eaccfda2 | 90 | if( ret != 0 ) |
kishino | 12:0254eaccfda2 | 91 | { |
kishino | 40:b6b10c22a121 | 92 | DEBUG_PRINT( "snic_init failed\r\n" ); |
kishino | 38:f13e4e563d65 | 93 | snic_core_p->freeCmdBuf( payload_buf_p ); |
kishino | 12:0254eaccfda2 | 94 | return -1; |
kishino | 12:0254eaccfda2 | 95 | } |
kishino | 12:0254eaccfda2 | 96 | |
kishino | 16:6100acfeb1f1 | 97 | if( uartCmdMgr_p->getCommandStatus() != 0 ) |
kishino | 12:0254eaccfda2 | 98 | { |
kishino | 40:b6b10c22a121 | 99 | DEBUG_PRINT("snic_init status:%02x\r\n", uartCmdMgr_p->getCommandStatus()); |
kishino | 39:a1233ca02edf | 100 | snic_core_p->freeCmdBuf( payload_buf_p ); |
kishino | 39:a1233ca02edf | 101 | return -1; |
kishino | 12:0254eaccfda2 | 102 | } |
kishino | 38:f13e4e563d65 | 103 | snic_core_p->freeCmdBuf( payload_buf_p ); |
kishino | 12:0254eaccfda2 | 104 | |
kishino | 12:0254eaccfda2 | 105 | return ret; |
kishino | 12:0254eaccfda2 | 106 | } |
kishino | 12:0254eaccfda2 | 107 | |
kishino | 12:0254eaccfda2 | 108 | int C_SNIC_WifiInterface::getFWVersion( unsigned char *version_p ) |
kishino | 12:0254eaccfda2 | 109 | { |
kishino | 24:987e412ae879 | 110 | C_SNIC_Core *snic_core_p = C_SNIC_Core::getInstance(); |
kishino | 24:987e412ae879 | 111 | C_SNIC_UartCommandManager *uartCmdMgr_p = snic_core_p->getUartCommand(); |
kishino | 16:6100acfeb1f1 | 112 | |
kishino | 12:0254eaccfda2 | 113 | // Get buffer for response payload from MemoryPool |
kishino | 38:f13e4e563d65 | 114 | tagMEMPOOL_BLOCK_T *payload_buf_p = snic_core_p->allocCmdBuf(); |
kishino | 38:f13e4e563d65 | 115 | if( payload_buf_p == NULL ) |
kishino | 12:0254eaccfda2 | 116 | { |
kishino | 40:b6b10c22a121 | 117 | DEBUG_PRINT("getFWVersion payload_buf_p NULL\r\n"); |
kishino | 12:0254eaccfda2 | 118 | return -1; |
kishino | 12:0254eaccfda2 | 119 | } |
kishino | 12:0254eaccfda2 | 120 | |
kishino | 22:a9ec0cad4f84 | 121 | C_SNIC_Core::tagGEN_FW_VER_GET_REQ_T req; |
kishino | 12:0254eaccfda2 | 122 | // Make request |
kishino | 12:0254eaccfda2 | 123 | req.cmd_sid = UART_CMD_SID_GEN_FW_VER_GET_REQ; |
kishino | 12:0254eaccfda2 | 124 | req.seq = mUartRequestSeq++; |
kishino | 12:0254eaccfda2 | 125 | |
kishino | 38:f13e4e563d65 | 126 | unsigned char *command_array_p = snic_core_p->getCommandBuf(); |
kishino | 12:0254eaccfda2 | 127 | unsigned int command_len; |
kishino | 12:0254eaccfda2 | 128 | // Preparation of command |
kishino | 24:987e412ae879 | 129 | command_len = snic_core_p->preparationSendCommand( UART_CMD_ID_GEN, req.cmd_sid, (unsigned char *)&req |
kishino | 38:f13e4e563d65 | 130 | , sizeof(C_SNIC_Core::tagGEN_FW_VER_GET_REQ_T), payload_buf_p->buf, command_array_p ); |
kishino | 12:0254eaccfda2 | 131 | |
kishino | 12:0254eaccfda2 | 132 | int ret; |
kishino | 12:0254eaccfda2 | 133 | |
kishino | 12:0254eaccfda2 | 134 | // Send uart command request |
kishino | 38:f13e4e563d65 | 135 | snic_core_p->sendUart( command_len, command_array_p ); |
kishino | 12:0254eaccfda2 | 136 | |
kishino | 12:0254eaccfda2 | 137 | // Wait UART response |
kishino | 16:6100acfeb1f1 | 138 | ret = uartCmdMgr_p->wait(); |
kishino | 12:0254eaccfda2 | 139 | if( ret != 0 ) |
kishino | 12:0254eaccfda2 | 140 | { |
kishino | 40:b6b10c22a121 | 141 | DEBUG_PRINT( "getFWversion failed\r\n" ); |
kishino | 38:f13e4e563d65 | 142 | snic_core_p->freeCmdBuf( payload_buf_p ); |
kishino | 12:0254eaccfda2 | 143 | return -1; |
kishino | 12:0254eaccfda2 | 144 | } |
kishino | 12:0254eaccfda2 | 145 | |
kishino | 16:6100acfeb1f1 | 146 | if( uartCmdMgr_p->getCommandStatus() == 0 ) |
kishino | 12:0254eaccfda2 | 147 | { |
kishino | 38:f13e4e563d65 | 148 | unsigned char version_len = payload_buf_p->buf[3]; |
kishino | 38:f13e4e563d65 | 149 | memcpy( version_p, &payload_buf_p->buf[4], version_len ); |
kishino | 12:0254eaccfda2 | 150 | } |
kishino | 38:f13e4e563d65 | 151 | snic_core_p->freeCmdBuf( payload_buf_p ); |
kishino | 12:0254eaccfda2 | 152 | return 0; |
kishino | 12:0254eaccfda2 | 153 | } |
kishino | 12:0254eaccfda2 | 154 | |
kishino | 12:0254eaccfda2 | 155 | int C_SNIC_WifiInterface::connect(const char *ssid_p, unsigned char ssid_len, E_SECURITY sec_type |
kishino | 12:0254eaccfda2 | 156 | , const char *sec_key_p, unsigned char sec_key_len) |
kishino | 12:0254eaccfda2 | 157 | { |
kishino | 24:987e412ae879 | 158 | C_SNIC_Core *snic_core_p = C_SNIC_Core::getInstance(); |
kishino | 24:987e412ae879 | 159 | C_SNIC_UartCommandManager *uartCmdMgr_p = snic_core_p->getUartCommand(); |
kishino | 16:6100acfeb1f1 | 160 | |
kishino | 12:0254eaccfda2 | 161 | // Parameter check(SSID) |
kishino | 12:0254eaccfda2 | 162 | if( (ssid_p == NULL) || (ssid_len == 0) ) |
kishino | 12:0254eaccfda2 | 163 | { |
kishino | 40:b6b10c22a121 | 164 | DEBUG_PRINT( "connect failed [ parameter NG:SSID ]\r\n" ); |
kishino | 12:0254eaccfda2 | 165 | return -1; |
kishino | 12:0254eaccfda2 | 166 | } |
kishino | 12:0254eaccfda2 | 167 | |
kishino | 12:0254eaccfda2 | 168 | // Parameter check(Security key) |
kishino | 12:0254eaccfda2 | 169 | if( (sec_type != e_SEC_OPEN) && ( (sec_key_len == 0) || (sec_key_p == NULL) ) ) |
kishino | 12:0254eaccfda2 | 170 | { |
kishino | 40:b6b10c22a121 | 171 | DEBUG_PRINT( "connect failed [ parameter NG:Security key ]\r\n" ); |
kishino | 12:0254eaccfda2 | 172 | return -1; |
kishino | 12:0254eaccfda2 | 173 | } |
kishino | 12:0254eaccfda2 | 174 | |
kishino | 12:0254eaccfda2 | 175 | // Get buffer for response payload from MemoryPool |
kishino | 38:f13e4e563d65 | 176 | tagMEMPOOL_BLOCK_T *payload_buf_p = snic_core_p->allocCmdBuf(); |
kishino | 38:f13e4e563d65 | 177 | if( payload_buf_p == NULL ) |
kishino | 12:0254eaccfda2 | 178 | { |
kishino | 40:b6b10c22a121 | 179 | DEBUG_PRINT("connect payload_buf_p NULL\r\n"); |
kishino | 12:0254eaccfda2 | 180 | return -1; |
kishino | 12:0254eaccfda2 | 181 | } |
kishino | 12:0254eaccfda2 | 182 | |
kishino | 29:6a0ba999597d | 183 | unsigned char *buf = &gCONNECT_BUF[0]; |
kishino | 12:0254eaccfda2 | 184 | unsigned int buf_len = 0; |
kishino | 12:0254eaccfda2 | 185 | unsigned int command_len; |
kishino | 12:0254eaccfda2 | 186 | |
kishino | 36:f33fcf5975ab | 187 | memset( buf, 0, UART_CONNECT_BUF_SIZE ); |
kishino | 12:0254eaccfda2 | 188 | // Make request |
kishino | 12:0254eaccfda2 | 189 | buf[0] = UART_CMD_SID_WIFI_JOIN_REQ; |
kishino | 12:0254eaccfda2 | 190 | buf_len++; |
kishino | 12:0254eaccfda2 | 191 | buf[1] = mUartRequestSeq++; |
kishino | 12:0254eaccfda2 | 192 | buf_len++; |
kishino | 12:0254eaccfda2 | 193 | // SSID |
kishino | 12:0254eaccfda2 | 194 | memcpy( &buf[2], ssid_p, ssid_len ); |
kishino | 12:0254eaccfda2 | 195 | buf_len += ssid_len; |
kishino | 12:0254eaccfda2 | 196 | buf_len++; |
kishino | 12:0254eaccfda2 | 197 | |
kishino | 12:0254eaccfda2 | 198 | // Security mode |
kishino | 12:0254eaccfda2 | 199 | buf[ buf_len ] = (unsigned char)sec_type; |
kishino | 12:0254eaccfda2 | 200 | buf_len++; |
kishino | 12:0254eaccfda2 | 201 | |
kishino | 12:0254eaccfda2 | 202 | // Security key |
kishino | 12:0254eaccfda2 | 203 | if( sec_type != e_SEC_OPEN ) |
kishino | 12:0254eaccfda2 | 204 | { |
kishino | 12:0254eaccfda2 | 205 | buf[ buf_len ] = sec_key_len; |
kishino | 12:0254eaccfda2 | 206 | buf_len++; |
kishino | 12:0254eaccfda2 | 207 | if( sec_key_len > 0 ) |
kishino | 12:0254eaccfda2 | 208 | { |
kishino | 12:0254eaccfda2 | 209 | memcpy( &buf[buf_len], sec_key_p, sec_key_len ); |
kishino | 12:0254eaccfda2 | 210 | buf_len += sec_key_len; |
kishino | 12:0254eaccfda2 | 211 | } |
kishino | 12:0254eaccfda2 | 212 | } |
kishino | 12:0254eaccfda2 | 213 | |
kishino | 38:f13e4e563d65 | 214 | unsigned char *command_array_p = snic_core_p->getCommandBuf(); |
kishino | 12:0254eaccfda2 | 215 | // Preparation of command |
kishino | 24:987e412ae879 | 216 | command_len = snic_core_p->preparationSendCommand( UART_CMD_ID_WIFI, UART_CMD_SID_WIFI_JOIN_REQ, buf |
kishino | 38:f13e4e563d65 | 217 | , buf_len, payload_buf_p->buf, command_array_p ); |
kishino | 12:0254eaccfda2 | 218 | |
kishino | 12:0254eaccfda2 | 219 | // Send uart command request |
kishino | 38:f13e4e563d65 | 220 | snic_core_p->sendUart( command_len, command_array_p ); |
kishino | 12:0254eaccfda2 | 221 | |
kishino | 12:0254eaccfda2 | 222 | int ret; |
kishino | 12:0254eaccfda2 | 223 | // Wait UART response |
kishino | 16:6100acfeb1f1 | 224 | ret = uartCmdMgr_p->wait(); |
kishino | 43:d80bbb12ffe6 | 225 | if(uartCmdMgr_p->getCommandStatus() != UART_CMD_RES_WIFI_ERR_ALREADY_JOINED) |
kishino | 43:d80bbb12ffe6 | 226 | { |
kishino | 43:d80bbb12ffe6 | 227 | DEBUG_PRINT( "Already connected\r\n" ); |
kishino | 43:d80bbb12ffe6 | 228 | } |
kishino | 43:d80bbb12ffe6 | 229 | else |
kishino | 12:0254eaccfda2 | 230 | { |
kishino | 43:d80bbb12ffe6 | 231 | if( ret != 0 ) |
kishino | 43:d80bbb12ffe6 | 232 | { |
kishino | 43:d80bbb12ffe6 | 233 | DEBUG_PRINT( "join failed\r\n" ); |
kishino | 43:d80bbb12ffe6 | 234 | snic_core_p->freeCmdBuf( payload_buf_p ); |
kishino | 43:d80bbb12ffe6 | 235 | return -1; |
kishino | 43:d80bbb12ffe6 | 236 | } |
kishino | 12:0254eaccfda2 | 237 | } |
kishino | 12:0254eaccfda2 | 238 | |
kishino | 43:d80bbb12ffe6 | 239 | if(uartCmdMgr_p->getCommandStatus() != 0) |
kishino | 12:0254eaccfda2 | 240 | { |
kishino | 40:b6b10c22a121 | 241 | DEBUG_PRINT("join status:%02x\r\n", uartCmdMgr_p->getCommandStatus()); |
kishino | 39:a1233ca02edf | 242 | snic_core_p->freeCmdBuf( payload_buf_p ); |
kishino | 39:a1233ca02edf | 243 | return -1; |
kishino | 12:0254eaccfda2 | 244 | } |
kishino | 38:f13e4e563d65 | 245 | snic_core_p->freeCmdBuf( payload_buf_p ); |
kishino | 12:0254eaccfda2 | 246 | |
kishino | 12:0254eaccfda2 | 247 | return ret; |
kishino | 12:0254eaccfda2 | 248 | } |
kishino | 12:0254eaccfda2 | 249 | |
kishino | 12:0254eaccfda2 | 250 | int C_SNIC_WifiInterface::disconnect() |
kishino | 12:0254eaccfda2 | 251 | { |
kishino | 24:987e412ae879 | 252 | C_SNIC_Core *snic_core_p = C_SNIC_Core::getInstance(); |
kishino | 24:987e412ae879 | 253 | C_SNIC_UartCommandManager *uartCmdMgr_p = snic_core_p->getUartCommand(); |
kishino | 16:6100acfeb1f1 | 254 | |
kishino | 12:0254eaccfda2 | 255 | // Get buffer for response payload from MemoryPool |
kishino | 38:f13e4e563d65 | 256 | tagMEMPOOL_BLOCK_T *payload_buf_p = snic_core_p->allocCmdBuf(); |
kishino | 38:f13e4e563d65 | 257 | if( payload_buf_p == NULL ) |
kishino | 12:0254eaccfda2 | 258 | { |
kishino | 40:b6b10c22a121 | 259 | DEBUG_PRINT("disconnect payload_buf_p NULL\r\n"); |
kishino | 12:0254eaccfda2 | 260 | return -1; |
kishino | 12:0254eaccfda2 | 261 | } |
kishino | 12:0254eaccfda2 | 262 | |
kishino | 22:a9ec0cad4f84 | 263 | C_SNIC_Core::tagWIFI_DISCONNECT_REQ_T req; |
kishino | 12:0254eaccfda2 | 264 | // Make request |
kishino | 12:0254eaccfda2 | 265 | req.cmd_sid = UART_CMD_SID_WIFI_DISCONNECT_REQ; |
kishino | 12:0254eaccfda2 | 266 | req.seq = mUartRequestSeq++; |
kishino | 12:0254eaccfda2 | 267 | |
kishino | 38:f13e4e563d65 | 268 | unsigned char *command_array_p = snic_core_p->getCommandBuf(); |
kishino | 12:0254eaccfda2 | 269 | unsigned int command_len; |
kishino | 12:0254eaccfda2 | 270 | // Preparation of command |
kishino | 24:987e412ae879 | 271 | command_len = snic_core_p->preparationSendCommand( UART_CMD_ID_WIFI, req.cmd_sid, (unsigned char *)&req |
kishino | 38:f13e4e563d65 | 272 | , sizeof(C_SNIC_Core::tagWIFI_DISCONNECT_REQ_T), payload_buf_p->buf, command_array_p ); |
kishino | 12:0254eaccfda2 | 273 | |
kishino | 12:0254eaccfda2 | 274 | // Send uart command request |
kishino | 38:f13e4e563d65 | 275 | snic_core_p->sendUart( command_len, command_array_p ); |
kishino | 12:0254eaccfda2 | 276 | |
kishino | 12:0254eaccfda2 | 277 | int ret; |
kishino | 12:0254eaccfda2 | 278 | // Wait UART response |
kishino | 16:6100acfeb1f1 | 279 | ret = uartCmdMgr_p->wait(); |
kishino | 12:0254eaccfda2 | 280 | if( ret != 0 ) |
kishino | 12:0254eaccfda2 | 281 | { |
kishino | 40:b6b10c22a121 | 282 | DEBUG_PRINT( "disconnect failed\r\n" ); |
kishino | 38:f13e4e563d65 | 283 | snic_core_p->freeCmdBuf( payload_buf_p ); |
kishino | 12:0254eaccfda2 | 284 | return -1; |
kishino | 12:0254eaccfda2 | 285 | } |
kishino | 12:0254eaccfda2 | 286 | |
kishino | 16:6100acfeb1f1 | 287 | if( uartCmdMgr_p->getCommandStatus() != 0 ) |
kishino | 12:0254eaccfda2 | 288 | { |
kishino | 40:b6b10c22a121 | 289 | DEBUG_PRINT("disconnect status:%02x\r\n", uartCmdMgr_p->getCommandStatus()); |
kishino | 12:0254eaccfda2 | 290 | ret = -1; |
kishino | 12:0254eaccfda2 | 291 | } |
kishino | 38:f13e4e563d65 | 292 | snic_core_p->freeCmdBuf( payload_buf_p ); |
kishino | 12:0254eaccfda2 | 293 | return ret; |
kishino | 12:0254eaccfda2 | 294 | } |
kishino | 12:0254eaccfda2 | 295 | |
kishino | 12:0254eaccfda2 | 296 | int C_SNIC_WifiInterface::scan( const char *ssid_p, unsigned char *bssid_p |
kishino | 12:0254eaccfda2 | 297 | , void (*result_handler_p)(tagSCAN_RESULT_T *scan_result) ) |
kishino | 12:0254eaccfda2 | 298 | { |
kishino | 24:987e412ae879 | 299 | C_SNIC_Core *snic_core_p = C_SNIC_Core::getInstance(); |
kishino | 24:987e412ae879 | 300 | C_SNIC_UartCommandManager *uartCmdMgr_p = snic_core_p->getUartCommand(); |
kishino | 16:6100acfeb1f1 | 301 | |
kishino | 12:0254eaccfda2 | 302 | // Get buffer for response payload from MemoryPool |
kishino | 38:f13e4e563d65 | 303 | tagMEMPOOL_BLOCK_T *payload_buf_p = snic_core_p->allocCmdBuf(); |
kishino | 38:f13e4e563d65 | 304 | if( payload_buf_p == NULL ) |
kishino | 12:0254eaccfda2 | 305 | { |
kishino | 40:b6b10c22a121 | 306 | DEBUG_PRINT("scan payload_buf_p NULL\r\n"); |
kishino | 12:0254eaccfda2 | 307 | return -1; |
kishino | 12:0254eaccfda2 | 308 | } |
kishino | 12:0254eaccfda2 | 309 | |
kishino | 22:a9ec0cad4f84 | 310 | C_SNIC_Core::tagWIFI_SCAN_REQ_T req; |
kishino | 12:0254eaccfda2 | 311 | unsigned int buf_len = 0; |
kishino | 12:0254eaccfda2 | 312 | |
kishino | 22:a9ec0cad4f84 | 313 | memset( &req, 0, sizeof(C_SNIC_Core::tagWIFI_SCAN_REQ_T) ); |
kishino | 12:0254eaccfda2 | 314 | // Make request |
kishino | 12:0254eaccfda2 | 315 | req.cmd_sid = UART_CMD_SID_WIFI_SCAN_REQ; |
kishino | 12:0254eaccfda2 | 316 | buf_len++; |
kishino | 12:0254eaccfda2 | 317 | req.seq = mUartRequestSeq++; |
kishino | 12:0254eaccfda2 | 318 | buf_len++; |
kishino | 12:0254eaccfda2 | 319 | |
kishino | 12:0254eaccfda2 | 320 | // Set scan type(Active scan) |
kishino | 12:0254eaccfda2 | 321 | req.scan_type = 0; |
kishino | 12:0254eaccfda2 | 322 | buf_len++; |
kishino | 12:0254eaccfda2 | 323 | // Set bss type(any) |
kishino | 12:0254eaccfda2 | 324 | req.bss_type = 2; |
kishino | 12:0254eaccfda2 | 325 | buf_len++; |
kishino | 12:0254eaccfda2 | 326 | // Set BSSID |
kishino | 12:0254eaccfda2 | 327 | if( bssid_p != NULL ) |
kishino | 12:0254eaccfda2 | 328 | { |
kishino | 12:0254eaccfda2 | 329 | memcpy( req.bssid, bssid_p, BSSID_MAC_LENTH ); |
kishino | 12:0254eaccfda2 | 330 | } |
kishino | 12:0254eaccfda2 | 331 | buf_len += BSSID_MAC_LENTH; |
kishino | 12:0254eaccfda2 | 332 | // Set channel list(0) |
kishino | 12:0254eaccfda2 | 333 | req.chan_list = 0; |
kishino | 12:0254eaccfda2 | 334 | buf_len++; |
kishino | 12:0254eaccfda2 | 335 | //Set SSID |
kishino | 12:0254eaccfda2 | 336 | if( ssid_p != NULL ) |
kishino | 12:0254eaccfda2 | 337 | { |
kishino | 12:0254eaccfda2 | 338 | strcpy( (char *)req.ssid, ssid_p ); |
kishino | 12:0254eaccfda2 | 339 | buf_len += strlen(ssid_p); |
kishino | 12:0254eaccfda2 | 340 | } |
kishino | 12:0254eaccfda2 | 341 | buf_len++; |
kishino | 12:0254eaccfda2 | 342 | |
kishino | 38:f13e4e563d65 | 343 | unsigned char *command_array_p = snic_core_p->getCommandBuf(); |
kishino | 12:0254eaccfda2 | 344 | unsigned int command_len; |
kishino | 12:0254eaccfda2 | 345 | // Preparation of command |
kishino | 24:987e412ae879 | 346 | command_len = snic_core_p->preparationSendCommand( UART_CMD_ID_WIFI, req.cmd_sid, (unsigned char *)&req |
kishino | 38:f13e4e563d65 | 347 | , buf_len, payload_buf_p->buf, command_array_p ); |
kishino | 12:0254eaccfda2 | 348 | |
kishino | 12:0254eaccfda2 | 349 | // Set scan result callback |
kishino | 16:6100acfeb1f1 | 350 | uartCmdMgr_p->setScanResultHandler( result_handler_p ); |
kishino | 12:0254eaccfda2 | 351 | |
kishino | 12:0254eaccfda2 | 352 | // Send uart command request |
kishino | 38:f13e4e563d65 | 353 | snic_core_p->sendUart( command_len, command_array_p ); |
kishino | 12:0254eaccfda2 | 354 | |
kishino | 12:0254eaccfda2 | 355 | int ret; |
kishino | 12:0254eaccfda2 | 356 | // Wait UART response |
kishino | 16:6100acfeb1f1 | 357 | ret = uartCmdMgr_p->wait(); |
kishino | 40:b6b10c22a121 | 358 | DEBUG_PRINT( "scan wait:%d\r\n", ret ); |
kishino | 12:0254eaccfda2 | 359 | if( ret != 0 ) |
kishino | 12:0254eaccfda2 | 360 | { |
kishino | 40:b6b10c22a121 | 361 | DEBUG_PRINT( "scan failed\r\n" ); |
kishino | 38:f13e4e563d65 | 362 | snic_core_p->freeCmdBuf( payload_buf_p ); |
kishino | 12:0254eaccfda2 | 363 | return -1; |
kishino | 12:0254eaccfda2 | 364 | } |
kishino | 12:0254eaccfda2 | 365 | |
kishino | 16:6100acfeb1f1 | 366 | if( uartCmdMgr_p->getCommandStatus() != 0 ) |
kishino | 12:0254eaccfda2 | 367 | { |
kishino | 40:b6b10c22a121 | 368 | DEBUG_PRINT("scan status:%02x\r\n", uartCmdMgr_p->getCommandStatus()); |
kishino | 39:a1233ca02edf | 369 | snic_core_p->freeCmdBuf( payload_buf_p ); |
kishino | 39:a1233ca02edf | 370 | return -1; |
kishino | 12:0254eaccfda2 | 371 | } |
kishino | 12:0254eaccfda2 | 372 | |
kishino | 38:f13e4e563d65 | 373 | snic_core_p->freeCmdBuf( payload_buf_p ); |
kishino | 12:0254eaccfda2 | 374 | |
kishino | 12:0254eaccfda2 | 375 | return ret; |
kishino | 12:0254eaccfda2 | 376 | } |
kishino | 12:0254eaccfda2 | 377 | |
kishino | 12:0254eaccfda2 | 378 | int C_SNIC_WifiInterface::wifi_on( const char *country_p ) |
kishino | 12:0254eaccfda2 | 379 | { |
kishino | 24:987e412ae879 | 380 | C_SNIC_Core *snic_core_p = C_SNIC_Core::getInstance(); |
kishino | 24:987e412ae879 | 381 | C_SNIC_UartCommandManager *uartCmdMgr_p = snic_core_p->getUartCommand(); |
kishino | 16:6100acfeb1f1 | 382 | |
kishino | 12:0254eaccfda2 | 383 | // Parameter check |
kishino | 12:0254eaccfda2 | 384 | if( country_p == NULL ) |
kishino | 12:0254eaccfda2 | 385 | { |
kishino | 40:b6b10c22a121 | 386 | DEBUG_PRINT("wifi_on parameter error\r\n"); |
kishino | 12:0254eaccfda2 | 387 | return -1; |
kishino | 12:0254eaccfda2 | 388 | } |
kishino | 12:0254eaccfda2 | 389 | |
kishino | 12:0254eaccfda2 | 390 | // Get buffer for response payload from MemoryPool |
kishino | 38:f13e4e563d65 | 391 | tagMEMPOOL_BLOCK_T *payload_buf_p = snic_core_p->allocCmdBuf(); |
kishino | 38:f13e4e563d65 | 392 | if( payload_buf_p == NULL ) |
kishino | 12:0254eaccfda2 | 393 | { |
kishino | 40:b6b10c22a121 | 394 | DEBUG_PRINT("wifi_on payload_buf_p NULL\r\n"); |
kishino | 12:0254eaccfda2 | 395 | return -1; |
kishino | 12:0254eaccfda2 | 396 | } |
kishino | 12:0254eaccfda2 | 397 | |
kishino | 22:a9ec0cad4f84 | 398 | C_SNIC_Core::tagWIFI_ON_REQ_T req; |
kishino | 12:0254eaccfda2 | 399 | // Make request |
kishino | 12:0254eaccfda2 | 400 | req.cmd_sid = UART_CMD_SID_WIFI_ON_REQ; |
kishino | 12:0254eaccfda2 | 401 | req.seq = mUartRequestSeq++; |
kishino | 12:0254eaccfda2 | 402 | memcpy( req.country, country_p, COUNTRYC_CODE_LENTH ); |
kishino | 12:0254eaccfda2 | 403 | |
kishino | 38:f13e4e563d65 | 404 | unsigned char *command_array_p = snic_core_p->getCommandBuf(); |
kishino | 12:0254eaccfda2 | 405 | unsigned int command_len; |
kishino | 12:0254eaccfda2 | 406 | // Preparation of command |
kishino | 24:987e412ae879 | 407 | command_len = snic_core_p->preparationSendCommand( UART_CMD_ID_WIFI, req.cmd_sid, (unsigned char *)&req |
kishino | 38:f13e4e563d65 | 408 | , sizeof(C_SNIC_Core::tagWIFI_ON_REQ_T), payload_buf_p->buf, command_array_p ); |
kishino | 12:0254eaccfda2 | 409 | |
kishino | 12:0254eaccfda2 | 410 | // Send uart command request |
kishino | 38:f13e4e563d65 | 411 | snic_core_p->sendUart( command_len, command_array_p ); |
kishino | 12:0254eaccfda2 | 412 | |
kishino | 12:0254eaccfda2 | 413 | int ret; |
kishino | 12:0254eaccfda2 | 414 | // Wait UART response |
kishino | 16:6100acfeb1f1 | 415 | ret = uartCmdMgr_p->wait(); |
kishino | 12:0254eaccfda2 | 416 | if( ret != 0 ) |
kishino | 12:0254eaccfda2 | 417 | { |
kishino | 40:b6b10c22a121 | 418 | DEBUG_PRINT( "wifi_on failed\r\n" ); |
kishino | 38:f13e4e563d65 | 419 | snic_core_p->freeCmdBuf( payload_buf_p ); |
kishino | 12:0254eaccfda2 | 420 | return -1; |
kishino | 12:0254eaccfda2 | 421 | } |
kishino | 12:0254eaccfda2 | 422 | |
kishino | 16:6100acfeb1f1 | 423 | if( uartCmdMgr_p->getCommandStatus() != 0 ) |
kishino | 12:0254eaccfda2 | 424 | { |
kishino | 40:b6b10c22a121 | 425 | DEBUG_PRINT("wifi_on status:%02x\r\n", uartCmdMgr_p->getCommandStatus()); |
kishino | 39:a1233ca02edf | 426 | snic_core_p->freeCmdBuf( payload_buf_p ); |
kishino | 39:a1233ca02edf | 427 | return -1; |
kishino | 12:0254eaccfda2 | 428 | } |
kishino | 38:f13e4e563d65 | 429 | snic_core_p->freeCmdBuf( payload_buf_p ); |
kishino | 12:0254eaccfda2 | 430 | |
kishino | 12:0254eaccfda2 | 431 | return ret; |
kishino | 12:0254eaccfda2 | 432 | } |
kishino | 12:0254eaccfda2 | 433 | |
kishino | 12:0254eaccfda2 | 434 | int C_SNIC_WifiInterface::wifi_off() |
kishino | 12:0254eaccfda2 | 435 | { |
kishino | 24:987e412ae879 | 436 | C_SNIC_Core *snic_core_p = C_SNIC_Core::getInstance(); |
kishino | 24:987e412ae879 | 437 | C_SNIC_UartCommandManager *uartCmdMgr_p = snic_core_p->getUartCommand(); |
kishino | 16:6100acfeb1f1 | 438 | |
kishino | 12:0254eaccfda2 | 439 | // Get buffer for response payload from MemoryPool |
kishino | 38:f13e4e563d65 | 440 | tagMEMPOOL_BLOCK_T *payload_buf_p = snic_core_p->allocCmdBuf(); |
kishino | 38:f13e4e563d65 | 441 | if( payload_buf_p == NULL ) |
kishino | 12:0254eaccfda2 | 442 | { |
kishino | 40:b6b10c22a121 | 443 | DEBUG_PRINT("wifi_off payload_buf_p NULL\r\n"); |
kishino | 12:0254eaccfda2 | 444 | return -1; |
kishino | 12:0254eaccfda2 | 445 | } |
kishino | 12:0254eaccfda2 | 446 | |
kishino | 22:a9ec0cad4f84 | 447 | C_SNIC_Core::tagWIFI_OFF_REQ_T req; |
kishino | 12:0254eaccfda2 | 448 | // Make request |
kishino | 12:0254eaccfda2 | 449 | req.cmd_sid = UART_CMD_SID_WIFI_OFF_REQ; |
kishino | 12:0254eaccfda2 | 450 | req.seq = mUartRequestSeq++; |
kishino | 12:0254eaccfda2 | 451 | |
kishino | 38:f13e4e563d65 | 452 | unsigned char *command_array_p = snic_core_p->getCommandBuf(); |
kishino | 12:0254eaccfda2 | 453 | unsigned int command_len; |
kishino | 12:0254eaccfda2 | 454 | // Preparation of command |
kishino | 24:987e412ae879 | 455 | command_len = snic_core_p->preparationSendCommand( UART_CMD_ID_WIFI, req.cmd_sid, (unsigned char *)&req |
kishino | 38:f13e4e563d65 | 456 | , sizeof(C_SNIC_Core::tagWIFI_OFF_REQ_T), payload_buf_p->buf, command_array_p ); |
kishino | 12:0254eaccfda2 | 457 | |
kishino | 12:0254eaccfda2 | 458 | // Send uart command request |
kishino | 38:f13e4e563d65 | 459 | snic_core_p->sendUart( command_len, command_array_p ); |
kishino | 12:0254eaccfda2 | 460 | |
kishino | 12:0254eaccfda2 | 461 | int ret; |
kishino | 12:0254eaccfda2 | 462 | // Wait UART response |
kishino | 16:6100acfeb1f1 | 463 | ret = uartCmdMgr_p->wait(); |
kishino | 12:0254eaccfda2 | 464 | if( ret != 0 ) |
kishino | 12:0254eaccfda2 | 465 | { |
kishino | 40:b6b10c22a121 | 466 | DEBUG_PRINT( "wifi_off failed\r\n" ); |
kishino | 38:f13e4e563d65 | 467 | snic_core_p->freeCmdBuf( payload_buf_p ); |
kishino | 12:0254eaccfda2 | 468 | return -1; |
kishino | 12:0254eaccfda2 | 469 | } |
kishino | 12:0254eaccfda2 | 470 | |
kishino | 16:6100acfeb1f1 | 471 | if( uartCmdMgr_p->getCommandStatus() != 0 ) |
kishino | 12:0254eaccfda2 | 472 | { |
kishino | 40:b6b10c22a121 | 473 | DEBUG_PRINT("wifi_off status:%02x\r\n", uartCmdMgr_p->getCommandStatus()); |
kishino | 39:a1233ca02edf | 474 | snic_core_p->freeCmdBuf( payload_buf_p ); |
kishino | 39:a1233ca02edf | 475 | return -1; |
kishino | 12:0254eaccfda2 | 476 | } |
kishino | 38:f13e4e563d65 | 477 | snic_core_p->freeCmdBuf( payload_buf_p ); |
kishino | 12:0254eaccfda2 | 478 | |
kishino | 12:0254eaccfda2 | 479 | return ret; |
kishino | 12:0254eaccfda2 | 480 | } |
kishino | 12:0254eaccfda2 | 481 | |
kishino | 12:0254eaccfda2 | 482 | int C_SNIC_WifiInterface::getRssi( signed char *rssi_p ) |
kishino | 12:0254eaccfda2 | 483 | { |
kishino | 24:987e412ae879 | 484 | C_SNIC_Core *snic_core_p = C_SNIC_Core::getInstance(); |
kishino | 24:987e412ae879 | 485 | C_SNIC_UartCommandManager *uartCmdMgr_p = snic_core_p->getUartCommand(); |
kishino | 12:0254eaccfda2 | 486 | if( rssi_p == NULL ) |
kishino | 12:0254eaccfda2 | 487 | { |
kishino | 40:b6b10c22a121 | 488 | DEBUG_PRINT("getRssi parameter error\r\n"); |
kishino | 12:0254eaccfda2 | 489 | return -1; |
kishino | 12:0254eaccfda2 | 490 | } |
kishino | 12:0254eaccfda2 | 491 | |
kishino | 12:0254eaccfda2 | 492 | // Get buffer for response payload from MemoryPool |
kishino | 38:f13e4e563d65 | 493 | tagMEMPOOL_BLOCK_T *payload_buf_p = snic_core_p->allocCmdBuf(); |
kishino | 38:f13e4e563d65 | 494 | if( payload_buf_p == NULL ) |
kishino | 12:0254eaccfda2 | 495 | { |
kishino | 40:b6b10c22a121 | 496 | DEBUG_PRINT("getRssi payload_buf_p NULL\r\n"); |
kishino | 12:0254eaccfda2 | 497 | return -1; |
kishino | 12:0254eaccfda2 | 498 | } |
kishino | 12:0254eaccfda2 | 499 | |
kishino | 22:a9ec0cad4f84 | 500 | C_SNIC_Core::tagWIFI_GET_STA_RSSI_REQ_T req; |
kishino | 12:0254eaccfda2 | 501 | |
kishino | 12:0254eaccfda2 | 502 | // Make request |
kishino | 12:0254eaccfda2 | 503 | req.cmd_sid = UART_CMD_SID_WIFI_GET_STA_RSSI_REQ; |
kishino | 12:0254eaccfda2 | 504 | req.seq = mUartRequestSeq++; |
kishino | 12:0254eaccfda2 | 505 | |
kishino | 38:f13e4e563d65 | 506 | unsigned char *command_array_p = snic_core_p->getCommandBuf(); |
kishino | 12:0254eaccfda2 | 507 | unsigned int command_len; |
kishino | 24:987e412ae879 | 508 | command_len = snic_core_p->preparationSendCommand( UART_CMD_ID_WIFI, req.cmd_sid, (unsigned char *)&req |
kishino | 38:f13e4e563d65 | 509 | , sizeof(C_SNIC_Core::tagWIFI_GET_STA_RSSI_REQ_T), payload_buf_p->buf, command_array_p ); |
kishino | 12:0254eaccfda2 | 510 | |
kishino | 12:0254eaccfda2 | 511 | int ret; |
kishino | 12:0254eaccfda2 | 512 | // Send uart command request |
kishino | 38:f13e4e563d65 | 513 | snic_core_p->sendUart( command_len, command_array_p ); |
kishino | 12:0254eaccfda2 | 514 | |
kishino | 12:0254eaccfda2 | 515 | // Wait UART response |
kishino | 16:6100acfeb1f1 | 516 | ret = uartCmdMgr_p->wait(); |
kishino | 12:0254eaccfda2 | 517 | if( ret != 0 ) |
kishino | 12:0254eaccfda2 | 518 | { |
kishino | 40:b6b10c22a121 | 519 | DEBUG_PRINT( "getRssi failed\r\n" ); |
kishino | 38:f13e4e563d65 | 520 | snic_core_p->freeCmdBuf( payload_buf_p ); |
kishino | 12:0254eaccfda2 | 521 | return -1; |
kishino | 12:0254eaccfda2 | 522 | } |
kishino | 12:0254eaccfda2 | 523 | |
kishino | 38:f13e4e563d65 | 524 | *rssi_p = (signed char)payload_buf_p->buf[2]; |
kishino | 12:0254eaccfda2 | 525 | |
kishino | 38:f13e4e563d65 | 526 | snic_core_p->freeCmdBuf( payload_buf_p ); |
kishino | 12:0254eaccfda2 | 527 | return 0; |
kishino | 12:0254eaccfda2 | 528 | } |
kishino | 12:0254eaccfda2 | 529 | |
kishino | 12:0254eaccfda2 | 530 | int C_SNIC_WifiInterface::getWifiStatus( tagWIFI_STATUS_T *status_p) |
kishino | 12:0254eaccfda2 | 531 | { |
kishino | 24:987e412ae879 | 532 | C_SNIC_Core *snic_core_p = C_SNIC_Core::getInstance(); |
kishino | 24:987e412ae879 | 533 | C_SNIC_UartCommandManager *uartCmdMgr_p = snic_core_p->getUartCommand(); |
kishino | 16:6100acfeb1f1 | 534 | |
kishino | 12:0254eaccfda2 | 535 | if( status_p == NULL ) |
kishino | 12:0254eaccfda2 | 536 | { |
kishino | 40:b6b10c22a121 | 537 | DEBUG_PRINT("getWifiStatus parameter error\r\n"); |
kishino | 12:0254eaccfda2 | 538 | return -1; |
kishino | 12:0254eaccfda2 | 539 | } |
kishino | 12:0254eaccfda2 | 540 | |
kishino | 12:0254eaccfda2 | 541 | // Get buffer for response payload from MemoryPool |
kishino | 38:f13e4e563d65 | 542 | tagMEMPOOL_BLOCK_T *payload_buf_p = snic_core_p->allocCmdBuf(); |
kishino | 38:f13e4e563d65 | 543 | if( payload_buf_p == NULL ) |
kishino | 12:0254eaccfda2 | 544 | { |
kishino | 40:b6b10c22a121 | 545 | DEBUG_PRINT("getWifiStatus payload_buf_p NULL\r\n"); |
kishino | 12:0254eaccfda2 | 546 | return -1; |
kishino | 12:0254eaccfda2 | 547 | } |
kishino | 12:0254eaccfda2 | 548 | |
kishino | 22:a9ec0cad4f84 | 549 | C_SNIC_Core::tagWIFI_GET_STATUS_REQ_T req; |
kishino | 12:0254eaccfda2 | 550 | // Make request |
kishino | 12:0254eaccfda2 | 551 | req.cmd_sid = UART_CMD_SID_WIFI_GET_STATUS_REQ; |
kishino | 12:0254eaccfda2 | 552 | req.seq = mUartRequestSeq++; |
kishino | 12:0254eaccfda2 | 553 | req.interface = 0; |
kishino | 12:0254eaccfda2 | 554 | |
kishino | 38:f13e4e563d65 | 555 | unsigned char *command_array_p = snic_core_p->getCommandBuf(); |
kishino | 12:0254eaccfda2 | 556 | unsigned int command_len; |
kishino | 24:987e412ae879 | 557 | command_len = snic_core_p->preparationSendCommand( UART_CMD_ID_WIFI, req.cmd_sid, (unsigned char *)&req |
kishino | 38:f13e4e563d65 | 558 | , sizeof(C_SNIC_Core::tagWIFI_GET_STATUS_REQ_T), payload_buf_p->buf, command_array_p ); |
kishino | 12:0254eaccfda2 | 559 | |
kishino | 12:0254eaccfda2 | 560 | // Send uart command request |
kishino | 38:f13e4e563d65 | 561 | snic_core_p->sendUart( command_len, command_array_p ); |
kishino | 12:0254eaccfda2 | 562 | |
kishino | 12:0254eaccfda2 | 563 | int ret; |
kishino | 12:0254eaccfda2 | 564 | // Wait UART response |
kishino | 16:6100acfeb1f1 | 565 | ret = uartCmdMgr_p->wait(); |
kishino | 12:0254eaccfda2 | 566 | if( ret != 0 ) |
kishino | 12:0254eaccfda2 | 567 | { |
kishino | 40:b6b10c22a121 | 568 | DEBUG_PRINT( "getWifiStatus failed\r\n" ); |
kishino | 38:f13e4e563d65 | 569 | snic_core_p->freeCmdBuf( payload_buf_p ); |
kishino | 12:0254eaccfda2 | 570 | return -1; |
kishino | 12:0254eaccfda2 | 571 | } |
kishino | 12:0254eaccfda2 | 572 | |
kishino | 12:0254eaccfda2 | 573 | // set status |
kishino | 38:f13e4e563d65 | 574 | status_p->status = (E_WIFI_STATUS)payload_buf_p->buf[2]; |
kishino | 12:0254eaccfda2 | 575 | |
kishino | 12:0254eaccfda2 | 576 | // set Mac address |
kishino | 12:0254eaccfda2 | 577 | if( status_p->status != e_STATUS_OFF ) |
kishino | 12:0254eaccfda2 | 578 | { |
kishino | 38:f13e4e563d65 | 579 | memcpy( status_p->mac_address, &payload_buf_p->buf[3], BSSID_MAC_LENTH ); |
kishino | 12:0254eaccfda2 | 580 | } |
kishino | 12:0254eaccfda2 | 581 | |
kishino | 12:0254eaccfda2 | 582 | // set SSID |
kishino | 37:f3a2053627c2 | 583 | if( ( status_p->status == e_STA_JOINED ) || ( status_p->status == e_AP_STARTED ) ) |
kishino | 12:0254eaccfda2 | 584 | { |
kishino | 38:f13e4e563d65 | 585 | memcpy( status_p->ssid, &payload_buf_p->buf[9], strlen( (char *)&payload_buf_p->buf[9]) ); |
kishino | 12:0254eaccfda2 | 586 | } |
kishino | 12:0254eaccfda2 | 587 | |
kishino | 38:f13e4e563d65 | 588 | snic_core_p->freeCmdBuf( payload_buf_p ); |
kishino | 12:0254eaccfda2 | 589 | return 0; |
kishino | 12:0254eaccfda2 | 590 | } |
kishino | 31:15c22824cc46 | 591 | |
kishino | 31:15c22824cc46 | 592 | int C_SNIC_WifiInterface::setIPConfig( bool is_DHCP |
kishino | 31:15c22824cc46 | 593 | , const char *ip_p, const char *mask_p, const char *gateway_p ) |
kishino | 31:15c22824cc46 | 594 | { |
kishino | 31:15c22824cc46 | 595 | // Parameter check |
kishino | 31:15c22824cc46 | 596 | if( is_DHCP == false ) |
kishino | 31:15c22824cc46 | 597 | { |
kishino | 31:15c22824cc46 | 598 | if( (ip_p == NULL) || (mask_p == NULL) ||(gateway_p == NULL) ) |
kishino | 31:15c22824cc46 | 599 | { |
kishino | 40:b6b10c22a121 | 600 | DEBUG_PRINT("setIPConfig parameter error\r\n"); |
kishino | 31:15c22824cc46 | 601 | return -1; |
kishino | 31:15c22824cc46 | 602 | } |
kishino | 31:15c22824cc46 | 603 | } |
kishino | 31:15c22824cc46 | 604 | |
kishino | 31:15c22824cc46 | 605 | C_SNIC_Core *snic_core_p = C_SNIC_Core::getInstance(); |
kishino | 31:15c22824cc46 | 606 | C_SNIC_UartCommandManager *uartCmdMgr_p = snic_core_p->getUartCommand(); |
kishino | 31:15c22824cc46 | 607 | |
kishino | 31:15c22824cc46 | 608 | // Get buffer for response payload from MemoryPool |
kishino | 38:f13e4e563d65 | 609 | tagMEMPOOL_BLOCK_T *payload_buf_p = snic_core_p->allocCmdBuf(); |
kishino | 38:f13e4e563d65 | 610 | if( payload_buf_p == NULL ) |
kishino | 31:15c22824cc46 | 611 | { |
kishino | 40:b6b10c22a121 | 612 | DEBUG_PRINT("setIPConfig payload_buf_p NULL\r\n"); |
kishino | 31:15c22824cc46 | 613 | return -1; |
kishino | 31:15c22824cc46 | 614 | } |
kishino | 31:15c22824cc46 | 615 | |
kishino | 38:f13e4e563d65 | 616 | unsigned char *command_array_p = snic_core_p->getCommandBuf(); |
kishino | 31:15c22824cc46 | 617 | unsigned int command_len; |
kishino | 31:15c22824cc46 | 618 | if( is_DHCP == true ) |
kishino | 31:15c22824cc46 | 619 | { |
kishino | 31:15c22824cc46 | 620 | C_SNIC_Core::tagSNIC_IP_CONFIG_REQ_DHCP_T req; |
kishino | 31:15c22824cc46 | 621 | // Make request |
kishino | 31:15c22824cc46 | 622 | req.cmd_sid = UART_CMD_SID_SNIC_IP_CONFIG_REQ; |
kishino | 31:15c22824cc46 | 623 | req.seq = mUartRequestSeq++; |
kishino | 31:15c22824cc46 | 624 | req.interface = 0; |
kishino | 31:15c22824cc46 | 625 | req.dhcp = 1; |
kishino | 31:15c22824cc46 | 626 | |
kishino | 31:15c22824cc46 | 627 | // Preparation of command |
kishino | 31:15c22824cc46 | 628 | command_len = snic_core_p->preparationSendCommand( UART_CMD_ID_SNIC, req.cmd_sid, (unsigned char *)&req |
kishino | 38:f13e4e563d65 | 629 | , sizeof(C_SNIC_Core::tagSNIC_IP_CONFIG_REQ_DHCP_T), payload_buf_p->buf, command_array_p ); |
kishino | 31:15c22824cc46 | 630 | } |
kishino | 31:15c22824cc46 | 631 | else |
kishino | 31:15c22824cc46 | 632 | { |
kishino | 31:15c22824cc46 | 633 | C_SNIC_Core::tagSNIC_IP_CONFIG_REQ_STATIC_T req; |
kishino | 31:15c22824cc46 | 634 | // Make request |
kishino | 31:15c22824cc46 | 635 | req.cmd_sid = UART_CMD_SID_SNIC_IP_CONFIG_REQ; |
kishino | 31:15c22824cc46 | 636 | req.seq = mUartRequestSeq++; |
kishino | 31:15c22824cc46 | 637 | req.interface = 0; |
kishino | 31:15c22824cc46 | 638 | req.dhcp = 0; |
kishino | 31:15c22824cc46 | 639 | |
kishino | 31:15c22824cc46 | 640 | // Set paramter of address |
kishino | 31:15c22824cc46 | 641 | int addr_temp; |
kishino | 31:15c22824cc46 | 642 | addr_temp = C_SNIC_UartMsgUtil::addrToInteger( ip_p ); |
kishino | 31:15c22824cc46 | 643 | C_SNIC_UartMsgUtil::convertIntToByteAdday( addr_temp, (char *)req.ip_addr ); |
kishino | 31:15c22824cc46 | 644 | addr_temp = C_SNIC_UartMsgUtil::addrToInteger( mask_p ); |
kishino | 31:15c22824cc46 | 645 | C_SNIC_UartMsgUtil::convertIntToByteAdday( addr_temp, (char *)req.netmask ); |
kishino | 31:15c22824cc46 | 646 | addr_temp = C_SNIC_UartMsgUtil::addrToInteger( gateway_p ); |
kishino | 31:15c22824cc46 | 647 | C_SNIC_UartMsgUtil::convertIntToByteAdday( addr_temp, (char *)req.gateway ); |
kishino | 31:15c22824cc46 | 648 | |
kishino | 31:15c22824cc46 | 649 | // Preparation of command |
kishino | 31:15c22824cc46 | 650 | command_len = snic_core_p->preparationSendCommand( UART_CMD_ID_SNIC, req.cmd_sid, (unsigned char *)&req |
kishino | 38:f13e4e563d65 | 651 | , sizeof(C_SNIC_Core::tagSNIC_IP_CONFIG_REQ_STATIC_T), payload_buf_p->buf, command_array_p ); |
kishino | 31:15c22824cc46 | 652 | } |
kishino | 31:15c22824cc46 | 653 | // Send uart command request |
kishino | 38:f13e4e563d65 | 654 | snic_core_p->sendUart( command_len, command_array_p ); |
kishino | 31:15c22824cc46 | 655 | |
kishino | 31:15c22824cc46 | 656 | int ret; |
kishino | 31:15c22824cc46 | 657 | // Wait UART response |
kishino | 31:15c22824cc46 | 658 | ret = uartCmdMgr_p->wait(); |
kishino | 31:15c22824cc46 | 659 | if( ret != 0 ) |
kishino | 31:15c22824cc46 | 660 | { |
kishino | 40:b6b10c22a121 | 661 | DEBUG_PRINT( "setIPConfig failed\r\n" ); |
kishino | 38:f13e4e563d65 | 662 | snic_core_p->freeCmdBuf( payload_buf_p ); |
kishino | 31:15c22824cc46 | 663 | return -1; |
kishino | 31:15c22824cc46 | 664 | } |
kishino | 31:15c22824cc46 | 665 | |
kishino | 31:15c22824cc46 | 666 | if( uartCmdMgr_p->getCommandStatus() != 0 ) |
kishino | 31:15c22824cc46 | 667 | { |
kishino | 40:b6b10c22a121 | 668 | DEBUG_PRINT("setIPConfig status:%02x\r\n", uartCmdMgr_p->getCommandStatus()); |
kishino | 39:a1233ca02edf | 669 | snic_core_p->freeCmdBuf( payload_buf_p ); |
kishino | 39:a1233ca02edf | 670 | return -1; |
kishino | 31:15c22824cc46 | 671 | } |
kishino | 31:15c22824cc46 | 672 | |
kishino | 38:f13e4e563d65 | 673 | snic_core_p->freeCmdBuf( payload_buf_p ); |
kishino | 36:f33fcf5975ab | 674 | return ret; |
kishino | 31:15c22824cc46 | 675 | } |
MACRUM | 51:69a9cf901d54 | 676 | |
MACRUM | 51:69a9cf901d54 | 677 | char* C_SNIC_WifiInterface::getIPAddress() { |
MACRUM | 51:69a9cf901d54 | 678 | C_SNIC_Core *snic_core_p = C_SNIC_Core::getInstance(); |
MACRUM | 51:69a9cf901d54 | 679 | C_SNIC_UartCommandManager *uartCmdMgr_p = snic_core_p->getUartCommand(); |
MACRUM | 51:69a9cf901d54 | 680 | |
MACRUM | 51:69a9cf901d54 | 681 | snic_core_p->lockAPI(); |
MACRUM | 51:69a9cf901d54 | 682 | // Get local ip address. |
MACRUM | 51:69a9cf901d54 | 683 | // Get buffer for response payload from MemoryPool |
MACRUM | 51:69a9cf901d54 | 684 | tagMEMPOOL_BLOCK_T *payload_buf_p = snic_core_p->allocCmdBuf(); |
MACRUM | 51:69a9cf901d54 | 685 | if( payload_buf_p == NULL ) |
MACRUM | 51:69a9cf901d54 | 686 | { |
MACRUM | 51:69a9cf901d54 | 687 | DEBUG_PRINT("getIPAddress payload_buf_p NULL\r\n"); |
MACRUM | 51:69a9cf901d54 | 688 | snic_core_p->unlockAPI(); |
MACRUM | 51:69a9cf901d54 | 689 | return 0; |
MACRUM | 51:69a9cf901d54 | 690 | } |
MACRUM | 51:69a9cf901d54 | 691 | |
MACRUM | 51:69a9cf901d54 | 692 | C_SNIC_Core::tagSNIC_GET_DHCP_INFO_REQ_T req; |
MACRUM | 51:69a9cf901d54 | 693 | // Make request |
MACRUM | 51:69a9cf901d54 | 694 | req.cmd_sid = UART_CMD_SID_SNIC_GET_DHCP_INFO_REQ; |
MACRUM | 51:69a9cf901d54 | 695 | req.seq = mUartRequestSeq++; |
MACRUM | 51:69a9cf901d54 | 696 | req.interface = 0; |
MACRUM | 51:69a9cf901d54 | 697 | |
MACRUM | 51:69a9cf901d54 | 698 | unsigned char *command_array_p = snic_core_p->getCommandBuf(); |
MACRUM | 51:69a9cf901d54 | 699 | unsigned int command_len; |
MACRUM | 51:69a9cf901d54 | 700 | // Preparation of command |
MACRUM | 51:69a9cf901d54 | 701 | command_len = snic_core_p->preparationSendCommand( UART_CMD_ID_SNIC, req.cmd_sid, (unsigned char *)&req |
MACRUM | 51:69a9cf901d54 | 702 | , sizeof(C_SNIC_Core::tagSNIC_GET_DHCP_INFO_REQ_T), payload_buf_p->buf, command_array_p ); |
MACRUM | 51:69a9cf901d54 | 703 | // Send uart command request |
MACRUM | 51:69a9cf901d54 | 704 | snic_core_p->sendUart( command_len, command_array_p ); |
MACRUM | 51:69a9cf901d54 | 705 | // Wait UART response |
MACRUM | 51:69a9cf901d54 | 706 | int ret = uartCmdMgr_p->wait(); |
MACRUM | 51:69a9cf901d54 | 707 | if( ret != 0 ) |
MACRUM | 51:69a9cf901d54 | 708 | { |
MACRUM | 51:69a9cf901d54 | 709 | DEBUG_PRINT( "getIPAddress failed\r\n" ); |
MACRUM | 51:69a9cf901d54 | 710 | snic_core_p->freeCmdBuf( payload_buf_p ); |
MACRUM | 51:69a9cf901d54 | 711 | snic_core_p->unlockAPI(); |
MACRUM | 51:69a9cf901d54 | 712 | return 0; |
MACRUM | 51:69a9cf901d54 | 713 | } |
MACRUM | 51:69a9cf901d54 | 714 | |
MACRUM | 51:69a9cf901d54 | 715 | if( uartCmdMgr_p->getCommandStatus() != UART_CMD_RES_SNIC_SUCCESS ) |
MACRUM | 51:69a9cf901d54 | 716 | { |
MACRUM | 51:69a9cf901d54 | 717 | snic_core_p->freeCmdBuf( payload_buf_p ); |
MACRUM | 51:69a9cf901d54 | 718 | snic_core_p->unlockAPI(); |
MACRUM | 51:69a9cf901d54 | 719 | return 0; |
MACRUM | 51:69a9cf901d54 | 720 | } |
MACRUM | 51:69a9cf901d54 | 721 | |
MACRUM | 51:69a9cf901d54 | 722 | snic_core_p->freeCmdBuf( payload_buf_p ); |
MACRUM | 51:69a9cf901d54 | 723 | snic_core_p->unlockAPI(); |
MACRUM | 51:69a9cf901d54 | 724 | |
MACRUM | 51:69a9cf901d54 | 725 | sprintf(ip_addr, "%d.%d.%d.%d\0", payload_buf_p->buf[9], payload_buf_p->buf[10], payload_buf_p->buf[11], payload_buf_p->buf[12]); |
MACRUM | 51:69a9cf901d54 | 726 | |
MACRUM | 51:69a9cf901d54 | 727 | return ip_addr; |
MACRUM | 51:69a9cf901d54 | 728 | } |