SNIC UART Interface library for Murata Type-YD module
Dependents: WebSocketServerTest
Fork of SNICInterface_mod by
SNIC_WifiInterface.cpp@22:a9ec0cad4f84, 2014-03-27 (annotated)
- Committer:
- kishino
- Date:
- Thu Mar 27 07:37:55 2014 +0000
- Revision:
- 22:a9ec0cad4f84
- Parent:
- 16:6100acfeb1f1
- Child:
- 24:987e412ae879
Refactoring
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
kishino | 14:54378c96d285 | 1 | /******************* Murata Manufacturing Co.,Ltd. 2014 ***************** |
kishino | 14:54378c96d285 | 2 | * |
kishino | 14:54378c96d285 | 3 | * Filename: SNIC_WifiInterface.cpp |
kishino | 14:54378c96d285 | 4 | * |
kishino | 14:54378c96d285 | 5 | * Purpose: This module has implementation of API for SNIC UART of Wi-Fi. |
kishino | 14:54378c96d285 | 6 | * |
kishino | 14:54378c96d285 | 7 | * $Author: kishino $ |
kishino | 14:54378c96d285 | 8 | * |
kishino | 14:54378c96d285 | 9 | * $Date: 2014/03/26 $ |
kishino | 14:54378c96d285 | 10 | * |
kishino | 14:54378c96d285 | 11 | * $Revision: 0.0.0.1 $ |
kishino | 14:54378c96d285 | 12 | * ***********************************************************************/ |
kishino | 12:0254eaccfda2 | 13 | #include "SNIC_WifiInterface.h" |
kishino | 12:0254eaccfda2 | 14 | #include "SNIC_UartMsgUtil.h" |
kishino | 12:0254eaccfda2 | 15 | |
kishino | 12:0254eaccfda2 | 16 | using namespace murata_wifi; |
kishino | 12:0254eaccfda2 | 17 | |
kishino | 12:0254eaccfda2 | 18 | C_SNIC_WifiInterface::C_SNIC_WifiInterface( PinName tx, PinName rx, PinName cts, PinName rts, PinName reset, PinName alarm, int baud) |
kishino | 12:0254eaccfda2 | 19 | { |
kishino | 12:0254eaccfda2 | 20 | mSNICWifi_p = C_SNIC_Core::getInstance(); |
kishino | 12:0254eaccfda2 | 21 | |
kishino | 12:0254eaccfda2 | 22 | mUART_tx = tx; |
kishino | 12:0254eaccfda2 | 23 | mUART_rx = rx; |
kishino | 12:0254eaccfda2 | 24 | mUART_cts = cts; |
kishino | 12:0254eaccfda2 | 25 | mUART_rts = rts;; |
kishino | 12:0254eaccfda2 | 26 | mUART_baud = baud; |
kishino | 12:0254eaccfda2 | 27 | mModuleReset = reset; |
kishino | 12:0254eaccfda2 | 28 | } |
kishino | 12:0254eaccfda2 | 29 | |
kishino | 12:0254eaccfda2 | 30 | int C_SNIC_WifiInterface::init() |
kishino | 12:0254eaccfda2 | 31 | { |
kishino | 16:6100acfeb1f1 | 32 | C_SNIC_UartCommandManager *uartCmdMgr_p = mSNICWifi_p->getUartCommand(); |
kishino | 16:6100acfeb1f1 | 33 | |
kishino | 12:0254eaccfda2 | 34 | /* Initialize UART */ |
kishino | 12:0254eaccfda2 | 35 | mSNICWifi_p->initUart( mUART_tx, mUART_rx, mUART_baud ); |
kishino | 12:0254eaccfda2 | 36 | |
kishino | 12:0254eaccfda2 | 37 | /* Initialize SNIC API */ |
kishino | 12:0254eaccfda2 | 38 | // Get buffer for response payload from MemoryPool |
kishino | 22:a9ec0cad4f84 | 39 | C_SNIC_Core::tagMEMPOOL_BLOCK_T *payload_buf = mSNICWifi_p->allocCmdBuf(); |
kishino | 12:0254eaccfda2 | 40 | if( payload_buf == NULL ) |
kishino | 12:0254eaccfda2 | 41 | { |
kishino | 12:0254eaccfda2 | 42 | printf("snic_init payload_buf NULL\r\n"); |
kishino | 12:0254eaccfda2 | 43 | return -1; |
kishino | 12:0254eaccfda2 | 44 | } |
kishino | 12:0254eaccfda2 | 45 | |
kishino | 22:a9ec0cad4f84 | 46 | C_SNIC_Core::tagSNIC_INIT_REQ_T req; |
kishino | 12:0254eaccfda2 | 47 | // Make request |
kishino | 12:0254eaccfda2 | 48 | req.cmd_sid = UART_CMD_SID_SNIC_INIT_REQ; |
kishino | 12:0254eaccfda2 | 49 | req.seq = mUartRequestSeq++; |
kishino | 12:0254eaccfda2 | 50 | req.buf_size = 0x800; |
kishino | 12:0254eaccfda2 | 51 | |
kishino | 12:0254eaccfda2 | 52 | unsigned char command_array[UART_REQUEST_PAYLOAD_MAX]; |
kishino | 12:0254eaccfda2 | 53 | unsigned int command_len; |
kishino | 12:0254eaccfda2 | 54 | // Preparation of command |
kishino | 12:0254eaccfda2 | 55 | command_len = mSNICWifi_p->preparationSendCommand( UART_CMD_ID_SNIC, req.cmd_sid, (unsigned char *)&req |
kishino | 22:a9ec0cad4f84 | 56 | , sizeof(C_SNIC_Core::tagSNIC_INIT_REQ_T), payload_buf->buf, command_array ); |
kishino | 12:0254eaccfda2 | 57 | |
kishino | 12:0254eaccfda2 | 58 | // Send uart command request |
kishino | 12:0254eaccfda2 | 59 | mSNICWifi_p->sendUart( command_len, command_array ); |
kishino | 12:0254eaccfda2 | 60 | |
kishino | 12:0254eaccfda2 | 61 | int ret; |
kishino | 12:0254eaccfda2 | 62 | // Wait UART response |
kishino | 16:6100acfeb1f1 | 63 | ret = uartCmdMgr_p->wait(); |
kishino | 12:0254eaccfda2 | 64 | if( ret != 0 ) |
kishino | 12:0254eaccfda2 | 65 | { |
kishino | 12:0254eaccfda2 | 66 | printf( "snic_init failed\r\n" ); |
kishino | 12:0254eaccfda2 | 67 | mSNICWifi_p->freeCmdBuf( payload_buf ); |
kishino | 12:0254eaccfda2 | 68 | return -1; |
kishino | 12:0254eaccfda2 | 69 | } |
kishino | 12:0254eaccfda2 | 70 | |
kishino | 16:6100acfeb1f1 | 71 | if( uartCmdMgr_p->getCommandStatus() != 0 ) |
kishino | 12:0254eaccfda2 | 72 | { |
kishino | 16:6100acfeb1f1 | 73 | printf("snic_init status:%02x\r\n", uartCmdMgr_p->getCommandStatus()); |
kishino | 12:0254eaccfda2 | 74 | ret = -1; |
kishino | 12:0254eaccfda2 | 75 | } |
kishino | 12:0254eaccfda2 | 76 | mSNICWifi_p->freeCmdBuf( payload_buf ); |
kishino | 12:0254eaccfda2 | 77 | |
kishino | 12:0254eaccfda2 | 78 | return ret; |
kishino | 12:0254eaccfda2 | 79 | } |
kishino | 12:0254eaccfda2 | 80 | |
kishino | 12:0254eaccfda2 | 81 | int C_SNIC_WifiInterface::getFWVersion( unsigned char *version_p ) |
kishino | 12:0254eaccfda2 | 82 | { |
kishino | 16:6100acfeb1f1 | 83 | C_SNIC_UartCommandManager *uartCmdMgr_p = mSNICWifi_p->getUartCommand(); |
kishino | 16:6100acfeb1f1 | 84 | |
kishino | 12:0254eaccfda2 | 85 | // Get buffer for response payload from MemoryPool |
kishino | 22:a9ec0cad4f84 | 86 | C_SNIC_Core::tagMEMPOOL_BLOCK_T *payload_buf = mSNICWifi_p->allocCmdBuf(); |
kishino | 12:0254eaccfda2 | 87 | if( payload_buf == NULL ) |
kishino | 12:0254eaccfda2 | 88 | { |
kishino | 12:0254eaccfda2 | 89 | printf("getFWVersion payload_buf NULL\r\n"); |
kishino | 12:0254eaccfda2 | 90 | return -1; |
kishino | 12:0254eaccfda2 | 91 | } |
kishino | 12:0254eaccfda2 | 92 | |
kishino | 22:a9ec0cad4f84 | 93 | C_SNIC_Core::tagGEN_FW_VER_GET_REQ_T req; |
kishino | 12:0254eaccfda2 | 94 | // Make request |
kishino | 12:0254eaccfda2 | 95 | req.cmd_sid = UART_CMD_SID_GEN_FW_VER_GET_REQ; |
kishino | 12:0254eaccfda2 | 96 | req.seq = mUartRequestSeq++; |
kishino | 12:0254eaccfda2 | 97 | |
kishino | 12:0254eaccfda2 | 98 | unsigned char command_array[UART_REQUEST_PAYLOAD_MAX]; |
kishino | 12:0254eaccfda2 | 99 | unsigned int command_len; |
kishino | 12:0254eaccfda2 | 100 | // Preparation of command |
kishino | 12:0254eaccfda2 | 101 | command_len = mSNICWifi_p->preparationSendCommand( UART_CMD_ID_GEN, req.cmd_sid, (unsigned char *)&req |
kishino | 22:a9ec0cad4f84 | 102 | , sizeof(C_SNIC_Core::tagGEN_FW_VER_GET_REQ_T), payload_buf->buf, command_array ); |
kishino | 12:0254eaccfda2 | 103 | |
kishino | 12:0254eaccfda2 | 104 | int ret; |
kishino | 12:0254eaccfda2 | 105 | |
kishino | 12:0254eaccfda2 | 106 | // Send uart command request |
kishino | 12:0254eaccfda2 | 107 | mSNICWifi_p->sendUart( command_len, command_array ); |
kishino | 12:0254eaccfda2 | 108 | |
kishino | 12:0254eaccfda2 | 109 | // Wait UART response |
kishino | 16:6100acfeb1f1 | 110 | ret = uartCmdMgr_p->wait(); |
kishino | 12:0254eaccfda2 | 111 | if( ret != 0 ) |
kishino | 12:0254eaccfda2 | 112 | { |
kishino | 12:0254eaccfda2 | 113 | printf( "getFWversion failed\r\n" ); |
kishino | 12:0254eaccfda2 | 114 | mSNICWifi_p->freeCmdBuf( payload_buf ); |
kishino | 12:0254eaccfda2 | 115 | return -1; |
kishino | 12:0254eaccfda2 | 116 | } |
kishino | 12:0254eaccfda2 | 117 | |
kishino | 16:6100acfeb1f1 | 118 | if( uartCmdMgr_p->getCommandStatus() == 0 ) |
kishino | 12:0254eaccfda2 | 119 | { |
kishino | 12:0254eaccfda2 | 120 | unsigned char version_len = payload_buf->buf[3]; |
kishino | 12:0254eaccfda2 | 121 | memcpy( version_p, &payload_buf->buf[4], version_len ); |
kishino | 12:0254eaccfda2 | 122 | } |
kishino | 12:0254eaccfda2 | 123 | mSNICWifi_p->freeCmdBuf( payload_buf ); |
kishino | 12:0254eaccfda2 | 124 | return 0; |
kishino | 12:0254eaccfda2 | 125 | } |
kishino | 12:0254eaccfda2 | 126 | |
kishino | 12:0254eaccfda2 | 127 | int C_SNIC_WifiInterface::connect(const char *ssid_p, unsigned char ssid_len, E_SECURITY sec_type |
kishino | 12:0254eaccfda2 | 128 | , const char *sec_key_p, unsigned char sec_key_len) |
kishino | 12:0254eaccfda2 | 129 | { |
kishino | 16:6100acfeb1f1 | 130 | C_SNIC_UartCommandManager *uartCmdMgr_p = mSNICWifi_p->getUartCommand(); |
kishino | 16:6100acfeb1f1 | 131 | |
kishino | 12:0254eaccfda2 | 132 | // Parameter check(SSID) |
kishino | 12:0254eaccfda2 | 133 | if( (ssid_p == NULL) || (ssid_len == 0) ) |
kishino | 12:0254eaccfda2 | 134 | { |
kishino | 12:0254eaccfda2 | 135 | printf( "connect failed [ parameter NG:SSID ]\r\n" ); |
kishino | 12:0254eaccfda2 | 136 | return -1; |
kishino | 12:0254eaccfda2 | 137 | } |
kishino | 12:0254eaccfda2 | 138 | |
kishino | 12:0254eaccfda2 | 139 | // Parameter check(Security key) |
kishino | 12:0254eaccfda2 | 140 | if( (sec_type != e_SEC_OPEN) && ( (sec_key_len == 0) || (sec_key_p == NULL) ) ) |
kishino | 12:0254eaccfda2 | 141 | { |
kishino | 12:0254eaccfda2 | 142 | printf( "connect failed [ parameter NG:Security key ]\r\n" ); |
kishino | 12:0254eaccfda2 | 143 | return -1; |
kishino | 12:0254eaccfda2 | 144 | } |
kishino | 12:0254eaccfda2 | 145 | |
kishino | 12:0254eaccfda2 | 146 | // Get buffer for response payload from MemoryPool |
kishino | 22:a9ec0cad4f84 | 147 | C_SNIC_Core::tagMEMPOOL_BLOCK_T *payload_buf = mSNICWifi_p->allocCmdBuf(); |
kishino | 12:0254eaccfda2 | 148 | if( payload_buf == NULL ) |
kishino | 12:0254eaccfda2 | 149 | { |
kishino | 12:0254eaccfda2 | 150 | printf("connect payload_buf NULL\r\n"); |
kishino | 12:0254eaccfda2 | 151 | return -1; |
kishino | 12:0254eaccfda2 | 152 | } |
kishino | 12:0254eaccfda2 | 153 | |
kishino | 12:0254eaccfda2 | 154 | unsigned char buf[UART_REQUEST_PAYLOAD_MAX]; |
kishino | 12:0254eaccfda2 | 155 | unsigned int buf_len = 0; |
kishino | 12:0254eaccfda2 | 156 | unsigned char command_array[UART_REQUEST_PAYLOAD_MAX]; |
kishino | 12:0254eaccfda2 | 157 | unsigned int command_len; |
kishino | 12:0254eaccfda2 | 158 | |
kishino | 12:0254eaccfda2 | 159 | memset( buf, 0, UART_REQUEST_PAYLOAD_MAX ); |
kishino | 12:0254eaccfda2 | 160 | // Make request |
kishino | 12:0254eaccfda2 | 161 | buf[0] = UART_CMD_SID_WIFI_JOIN_REQ; |
kishino | 12:0254eaccfda2 | 162 | buf_len++; |
kishino | 12:0254eaccfda2 | 163 | buf[1] = mUartRequestSeq++; |
kishino | 12:0254eaccfda2 | 164 | buf_len++; |
kishino | 12:0254eaccfda2 | 165 | // SSID |
kishino | 12:0254eaccfda2 | 166 | memcpy( &buf[2], ssid_p, ssid_len ); |
kishino | 12:0254eaccfda2 | 167 | buf_len += ssid_len; |
kishino | 12:0254eaccfda2 | 168 | buf_len++; |
kishino | 12:0254eaccfda2 | 169 | |
kishino | 12:0254eaccfda2 | 170 | // Security mode |
kishino | 12:0254eaccfda2 | 171 | buf[ buf_len ] = (unsigned char)sec_type; |
kishino | 12:0254eaccfda2 | 172 | buf_len++; |
kishino | 12:0254eaccfda2 | 173 | |
kishino | 12:0254eaccfda2 | 174 | // Security key |
kishino | 12:0254eaccfda2 | 175 | if( sec_type != e_SEC_OPEN ) |
kishino | 12:0254eaccfda2 | 176 | { |
kishino | 12:0254eaccfda2 | 177 | buf[ buf_len ] = sec_key_len; |
kishino | 12:0254eaccfda2 | 178 | buf_len++; |
kishino | 12:0254eaccfda2 | 179 | if( sec_key_len > 0 ) |
kishino | 12:0254eaccfda2 | 180 | { |
kishino | 12:0254eaccfda2 | 181 | memcpy( &buf[buf_len], sec_key_p, sec_key_len ); |
kishino | 12:0254eaccfda2 | 182 | buf_len += sec_key_len; |
kishino | 12:0254eaccfda2 | 183 | } |
kishino | 12:0254eaccfda2 | 184 | } |
kishino | 12:0254eaccfda2 | 185 | |
kishino | 12:0254eaccfda2 | 186 | // Preparation of command |
kishino | 12:0254eaccfda2 | 187 | command_len = mSNICWifi_p->preparationSendCommand( UART_CMD_ID_WIFI, UART_CMD_SID_WIFI_JOIN_REQ, buf |
kishino | 12:0254eaccfda2 | 188 | , buf_len, payload_buf->buf, command_array ); |
kishino | 12:0254eaccfda2 | 189 | |
kishino | 12:0254eaccfda2 | 190 | // Send uart command request |
kishino | 12:0254eaccfda2 | 191 | mSNICWifi_p->sendUart( command_len, command_array ); |
kishino | 12:0254eaccfda2 | 192 | |
kishino | 12:0254eaccfda2 | 193 | int ret; |
kishino | 12:0254eaccfda2 | 194 | // Wait UART response |
kishino | 16:6100acfeb1f1 | 195 | ret = uartCmdMgr_p->wait(); |
kishino | 12:0254eaccfda2 | 196 | if( ret != 0 ) |
kishino | 12:0254eaccfda2 | 197 | { |
kishino | 12:0254eaccfda2 | 198 | printf( "join failed\r\n" ); |
kishino | 12:0254eaccfda2 | 199 | mSNICWifi_p->freeCmdBuf( payload_buf ); |
kishino | 12:0254eaccfda2 | 200 | return -1; |
kishino | 12:0254eaccfda2 | 201 | } |
kishino | 12:0254eaccfda2 | 202 | |
kishino | 16:6100acfeb1f1 | 203 | if( uartCmdMgr_p->getCommandStatus() != 0 ) |
kishino | 12:0254eaccfda2 | 204 | { |
kishino | 16:6100acfeb1f1 | 205 | printf("join status:%02x\r\n", uartCmdMgr_p->getCommandStatus()); |
kishino | 12:0254eaccfda2 | 206 | ret = -1; |
kishino | 12:0254eaccfda2 | 207 | } |
kishino | 12:0254eaccfda2 | 208 | mSNICWifi_p->freeCmdBuf( payload_buf ); |
kishino | 12:0254eaccfda2 | 209 | |
kishino | 12:0254eaccfda2 | 210 | return ret; |
kishino | 12:0254eaccfda2 | 211 | } |
kishino | 12:0254eaccfda2 | 212 | |
kishino | 12:0254eaccfda2 | 213 | int C_SNIC_WifiInterface::disconnect() |
kishino | 12:0254eaccfda2 | 214 | { |
kishino | 16:6100acfeb1f1 | 215 | C_SNIC_UartCommandManager *uartCmdMgr_p = mSNICWifi_p->getUartCommand(); |
kishino | 16:6100acfeb1f1 | 216 | |
kishino | 12:0254eaccfda2 | 217 | // Get buffer for response payload from MemoryPool |
kishino | 22:a9ec0cad4f84 | 218 | C_SNIC_Core::tagMEMPOOL_BLOCK_T *payload_buf = mSNICWifi_p->allocCmdBuf(); |
kishino | 12:0254eaccfda2 | 219 | if( payload_buf == NULL ) |
kishino | 12:0254eaccfda2 | 220 | { |
kishino | 12:0254eaccfda2 | 221 | printf("disconnect payload_buf NULL\r\n"); |
kishino | 12:0254eaccfda2 | 222 | return -1; |
kishino | 12:0254eaccfda2 | 223 | } |
kishino | 12:0254eaccfda2 | 224 | |
kishino | 22:a9ec0cad4f84 | 225 | C_SNIC_Core::tagWIFI_DISCONNECT_REQ_T req; |
kishino | 12:0254eaccfda2 | 226 | // Make request |
kishino | 12:0254eaccfda2 | 227 | req.cmd_sid = UART_CMD_SID_WIFI_DISCONNECT_REQ; |
kishino | 12:0254eaccfda2 | 228 | req.seq = mUartRequestSeq++; |
kishino | 12:0254eaccfda2 | 229 | |
kishino | 12:0254eaccfda2 | 230 | unsigned char command_array[UART_REQUEST_PAYLOAD_MAX]; |
kishino | 12:0254eaccfda2 | 231 | unsigned int command_len; |
kishino | 12:0254eaccfda2 | 232 | // Preparation of command |
kishino | 12:0254eaccfda2 | 233 | command_len = mSNICWifi_p->preparationSendCommand( UART_CMD_ID_WIFI, req.cmd_sid, (unsigned char *)&req |
kishino | 22:a9ec0cad4f84 | 234 | , sizeof(C_SNIC_Core::tagWIFI_DISCONNECT_REQ_T), payload_buf->buf, command_array ); |
kishino | 12:0254eaccfda2 | 235 | |
kishino | 12:0254eaccfda2 | 236 | // Send uart command request |
kishino | 12:0254eaccfda2 | 237 | mSNICWifi_p->sendUart( command_len, command_array ); |
kishino | 12:0254eaccfda2 | 238 | |
kishino | 12:0254eaccfda2 | 239 | int ret; |
kishino | 12:0254eaccfda2 | 240 | // Wait UART response |
kishino | 16:6100acfeb1f1 | 241 | ret = uartCmdMgr_p->wait(); |
kishino | 12:0254eaccfda2 | 242 | if( ret != 0 ) |
kishino | 12:0254eaccfda2 | 243 | { |
kishino | 12:0254eaccfda2 | 244 | printf( "disconnect failed\r\n" ); |
kishino | 12:0254eaccfda2 | 245 | mSNICWifi_p->freeCmdBuf( payload_buf ); |
kishino | 12:0254eaccfda2 | 246 | return -1; |
kishino | 12:0254eaccfda2 | 247 | } |
kishino | 12:0254eaccfda2 | 248 | |
kishino | 16:6100acfeb1f1 | 249 | if( uartCmdMgr_p->getCommandStatus() != 0 ) |
kishino | 12:0254eaccfda2 | 250 | { |
kishino | 16:6100acfeb1f1 | 251 | printf("disconnect status:%02x\r\n", uartCmdMgr_p->getCommandStatus()); |
kishino | 12:0254eaccfda2 | 252 | ret = -1; |
kishino | 12:0254eaccfda2 | 253 | } |
kishino | 12:0254eaccfda2 | 254 | mSNICWifi_p->freeCmdBuf( payload_buf ); |
kishino | 12:0254eaccfda2 | 255 | return ret; |
kishino | 12:0254eaccfda2 | 256 | } |
kishino | 12:0254eaccfda2 | 257 | |
kishino | 12:0254eaccfda2 | 258 | int C_SNIC_WifiInterface::scan( const char *ssid_p, unsigned char *bssid_p |
kishino | 12:0254eaccfda2 | 259 | , void (*result_handler_p)(tagSCAN_RESULT_T *scan_result) ) |
kishino | 12:0254eaccfda2 | 260 | { |
kishino | 16:6100acfeb1f1 | 261 | C_SNIC_UartCommandManager *uartCmdMgr_p = mSNICWifi_p->getUartCommand(); |
kishino | 16:6100acfeb1f1 | 262 | |
kishino | 12:0254eaccfda2 | 263 | // Get buffer for response payload from MemoryPool |
kishino | 22:a9ec0cad4f84 | 264 | C_SNIC_Core::tagMEMPOOL_BLOCK_T *payload_buf = mSNICWifi_p->allocCmdBuf(); |
kishino | 12:0254eaccfda2 | 265 | if( payload_buf == NULL ) |
kishino | 12:0254eaccfda2 | 266 | { |
kishino | 12:0254eaccfda2 | 267 | printf("scan payload_buf NULL\r\n"); |
kishino | 12:0254eaccfda2 | 268 | return -1; |
kishino | 12:0254eaccfda2 | 269 | } |
kishino | 12:0254eaccfda2 | 270 | |
kishino | 22:a9ec0cad4f84 | 271 | C_SNIC_Core::tagWIFI_SCAN_REQ_T req; |
kishino | 12:0254eaccfda2 | 272 | unsigned int buf_len = 0; |
kishino | 12:0254eaccfda2 | 273 | |
kishino | 22:a9ec0cad4f84 | 274 | memset( &req, 0, sizeof(C_SNIC_Core::tagWIFI_SCAN_REQ_T) ); |
kishino | 12:0254eaccfda2 | 275 | // Make request |
kishino | 12:0254eaccfda2 | 276 | req.cmd_sid = UART_CMD_SID_WIFI_SCAN_REQ; |
kishino | 12:0254eaccfda2 | 277 | buf_len++; |
kishino | 12:0254eaccfda2 | 278 | req.seq = mUartRequestSeq++; |
kishino | 12:0254eaccfda2 | 279 | buf_len++; |
kishino | 12:0254eaccfda2 | 280 | |
kishino | 12:0254eaccfda2 | 281 | // Set scan type(Active scan) |
kishino | 12:0254eaccfda2 | 282 | req.scan_type = 0; |
kishino | 12:0254eaccfda2 | 283 | buf_len++; |
kishino | 12:0254eaccfda2 | 284 | // Set bss type(any) |
kishino | 12:0254eaccfda2 | 285 | req.bss_type = 2; |
kishino | 12:0254eaccfda2 | 286 | buf_len++; |
kishino | 12:0254eaccfda2 | 287 | // Set BSSID |
kishino | 12:0254eaccfda2 | 288 | if( bssid_p != NULL ) |
kishino | 12:0254eaccfda2 | 289 | { |
kishino | 12:0254eaccfda2 | 290 | memcpy( req.bssid, bssid_p, BSSID_MAC_LENTH ); |
kishino | 12:0254eaccfda2 | 291 | } |
kishino | 12:0254eaccfda2 | 292 | buf_len += BSSID_MAC_LENTH; |
kishino | 12:0254eaccfda2 | 293 | // Set channel list(0) |
kishino | 12:0254eaccfda2 | 294 | req.chan_list = 0; |
kishino | 12:0254eaccfda2 | 295 | buf_len++; |
kishino | 12:0254eaccfda2 | 296 | //Set SSID |
kishino | 12:0254eaccfda2 | 297 | if( ssid_p != NULL ) |
kishino | 12:0254eaccfda2 | 298 | { |
kishino | 12:0254eaccfda2 | 299 | strcpy( (char *)req.ssid, ssid_p ); |
kishino | 12:0254eaccfda2 | 300 | buf_len += strlen(ssid_p); |
kishino | 12:0254eaccfda2 | 301 | } |
kishino | 12:0254eaccfda2 | 302 | buf_len++; |
kishino | 12:0254eaccfda2 | 303 | |
kishino | 12:0254eaccfda2 | 304 | unsigned char command_array[UART_REQUEST_PAYLOAD_MAX]; |
kishino | 12:0254eaccfda2 | 305 | unsigned int command_len; |
kishino | 12:0254eaccfda2 | 306 | // Preparation of command |
kishino | 12:0254eaccfda2 | 307 | command_len = mSNICWifi_p->preparationSendCommand( UART_CMD_ID_WIFI, req.cmd_sid, (unsigned char *)&req |
kishino | 12:0254eaccfda2 | 308 | , buf_len, payload_buf->buf, command_array ); |
kishino | 12:0254eaccfda2 | 309 | |
kishino | 12:0254eaccfda2 | 310 | // Set scan result callback |
kishino | 16:6100acfeb1f1 | 311 | uartCmdMgr_p->setScanResultHandler( result_handler_p ); |
kishino | 12:0254eaccfda2 | 312 | |
kishino | 12:0254eaccfda2 | 313 | // Send uart command request |
kishino | 12:0254eaccfda2 | 314 | mSNICWifi_p->sendUart( command_len, command_array ); |
kishino | 12:0254eaccfda2 | 315 | |
kishino | 12:0254eaccfda2 | 316 | int ret; |
kishino | 12:0254eaccfda2 | 317 | // Wait UART response |
kishino | 16:6100acfeb1f1 | 318 | ret = uartCmdMgr_p->wait(); |
kishino | 12:0254eaccfda2 | 319 | printf( "scan wait:%d\r\n", ret ); |
kishino | 12:0254eaccfda2 | 320 | if( ret != 0 ) |
kishino | 12:0254eaccfda2 | 321 | { |
kishino | 12:0254eaccfda2 | 322 | printf( "scan failed\r\n" ); |
kishino | 12:0254eaccfda2 | 323 | mSNICWifi_p->freeCmdBuf( payload_buf ); |
kishino | 12:0254eaccfda2 | 324 | return -1; |
kishino | 12:0254eaccfda2 | 325 | } |
kishino | 12:0254eaccfda2 | 326 | |
kishino | 16:6100acfeb1f1 | 327 | if( uartCmdMgr_p->getCommandStatus() != 0 ) |
kishino | 12:0254eaccfda2 | 328 | { |
kishino | 16:6100acfeb1f1 | 329 | printf("scan status:%02x\r\n", uartCmdMgr_p->getCommandStatus()); |
kishino | 12:0254eaccfda2 | 330 | ret = -1; |
kishino | 12:0254eaccfda2 | 331 | } |
kishino | 12:0254eaccfda2 | 332 | |
kishino | 12:0254eaccfda2 | 333 | mSNICWifi_p->freeCmdBuf( payload_buf ); |
kishino | 12:0254eaccfda2 | 334 | |
kishino | 12:0254eaccfda2 | 335 | return ret; |
kishino | 12:0254eaccfda2 | 336 | } |
kishino | 12:0254eaccfda2 | 337 | |
kishino | 12:0254eaccfda2 | 338 | int C_SNIC_WifiInterface::wifi_on( const char *country_p ) |
kishino | 12:0254eaccfda2 | 339 | { |
kishino | 16:6100acfeb1f1 | 340 | C_SNIC_UartCommandManager *uartCmdMgr_p = mSNICWifi_p->getUartCommand(); |
kishino | 16:6100acfeb1f1 | 341 | |
kishino | 12:0254eaccfda2 | 342 | // Parameter check |
kishino | 12:0254eaccfda2 | 343 | if( country_p == NULL ) |
kishino | 12:0254eaccfda2 | 344 | { |
kishino | 12:0254eaccfda2 | 345 | printf("wifi_on parameter error\r\n"); |
kishino | 12:0254eaccfda2 | 346 | return -1; |
kishino | 12:0254eaccfda2 | 347 | } |
kishino | 12:0254eaccfda2 | 348 | |
kishino | 12:0254eaccfda2 | 349 | // Get buffer for response payload from MemoryPool |
kishino | 22:a9ec0cad4f84 | 350 | C_SNIC_Core::tagMEMPOOL_BLOCK_T *payload_buf = mSNICWifi_p->allocCmdBuf(); |
kishino | 12:0254eaccfda2 | 351 | if( payload_buf == NULL ) |
kishino | 12:0254eaccfda2 | 352 | { |
kishino | 12:0254eaccfda2 | 353 | printf("wifi_on payload_buf NULL\r\n"); |
kishino | 12:0254eaccfda2 | 354 | return -1; |
kishino | 12:0254eaccfda2 | 355 | } |
kishino | 12:0254eaccfda2 | 356 | |
kishino | 22:a9ec0cad4f84 | 357 | C_SNIC_Core::tagWIFI_ON_REQ_T req; |
kishino | 12:0254eaccfda2 | 358 | // Make request |
kishino | 12:0254eaccfda2 | 359 | req.cmd_sid = UART_CMD_SID_WIFI_ON_REQ; |
kishino | 12:0254eaccfda2 | 360 | req.seq = mUartRequestSeq++; |
kishino | 12:0254eaccfda2 | 361 | memcpy( req.country, country_p, COUNTRYC_CODE_LENTH ); |
kishino | 12:0254eaccfda2 | 362 | |
kishino | 12:0254eaccfda2 | 363 | unsigned char command_array[UART_REQUEST_PAYLOAD_MAX]; |
kishino | 12:0254eaccfda2 | 364 | unsigned int command_len; |
kishino | 12:0254eaccfda2 | 365 | // Preparation of command |
kishino | 12:0254eaccfda2 | 366 | command_len = mSNICWifi_p->preparationSendCommand( UART_CMD_ID_WIFI, req.cmd_sid, (unsigned char *)&req |
kishino | 22:a9ec0cad4f84 | 367 | , sizeof(C_SNIC_Core::tagWIFI_ON_REQ_T), payload_buf->buf, command_array ); |
kishino | 12:0254eaccfda2 | 368 | |
kishino | 12:0254eaccfda2 | 369 | // Send uart command request |
kishino | 12:0254eaccfda2 | 370 | mSNICWifi_p->sendUart( command_len, command_array ); |
kishino | 12:0254eaccfda2 | 371 | |
kishino | 12:0254eaccfda2 | 372 | int ret; |
kishino | 12:0254eaccfda2 | 373 | // Wait UART response |
kishino | 16:6100acfeb1f1 | 374 | ret = uartCmdMgr_p->wait(); |
kishino | 12:0254eaccfda2 | 375 | if( ret != 0 ) |
kishino | 12:0254eaccfda2 | 376 | { |
kishino | 12:0254eaccfda2 | 377 | printf( "wifi_on failed\r\n" ); |
kishino | 12:0254eaccfda2 | 378 | mSNICWifi_p->freeCmdBuf( payload_buf ); |
kishino | 12:0254eaccfda2 | 379 | return -1; |
kishino | 12:0254eaccfda2 | 380 | } |
kishino | 12:0254eaccfda2 | 381 | |
kishino | 16:6100acfeb1f1 | 382 | if( uartCmdMgr_p->getCommandStatus() != 0 ) |
kishino | 12:0254eaccfda2 | 383 | { |
kishino | 16:6100acfeb1f1 | 384 | printf("wifi_on status:%02x\r\n", uartCmdMgr_p->getCommandStatus()); |
kishino | 12:0254eaccfda2 | 385 | ret = -1; |
kishino | 12:0254eaccfda2 | 386 | } |
kishino | 12:0254eaccfda2 | 387 | mSNICWifi_p->freeCmdBuf( payload_buf ); |
kishino | 12:0254eaccfda2 | 388 | |
kishino | 12:0254eaccfda2 | 389 | return ret; |
kishino | 12:0254eaccfda2 | 390 | } |
kishino | 12:0254eaccfda2 | 391 | |
kishino | 12:0254eaccfda2 | 392 | int C_SNIC_WifiInterface::wifi_off() |
kishino | 12:0254eaccfda2 | 393 | { |
kishino | 16:6100acfeb1f1 | 394 | C_SNIC_UartCommandManager *uartCmdMgr_p = mSNICWifi_p->getUartCommand(); |
kishino | 16:6100acfeb1f1 | 395 | |
kishino | 12:0254eaccfda2 | 396 | // Get buffer for response payload from MemoryPool |
kishino | 22:a9ec0cad4f84 | 397 | C_SNIC_Core::tagMEMPOOL_BLOCK_T *payload_buf = mSNICWifi_p->allocCmdBuf(); |
kishino | 12:0254eaccfda2 | 398 | if( payload_buf == NULL ) |
kishino | 12:0254eaccfda2 | 399 | { |
kishino | 12:0254eaccfda2 | 400 | printf("wifi_off payload_buf NULL\r\n"); |
kishino | 12:0254eaccfda2 | 401 | return -1; |
kishino | 12:0254eaccfda2 | 402 | } |
kishino | 12:0254eaccfda2 | 403 | |
kishino | 22:a9ec0cad4f84 | 404 | C_SNIC_Core::tagWIFI_OFF_REQ_T req; |
kishino | 12:0254eaccfda2 | 405 | // Make request |
kishino | 12:0254eaccfda2 | 406 | req.cmd_sid = UART_CMD_SID_WIFI_OFF_REQ; |
kishino | 12:0254eaccfda2 | 407 | req.seq = mUartRequestSeq++; |
kishino | 12:0254eaccfda2 | 408 | |
kishino | 12:0254eaccfda2 | 409 | unsigned char command_array[UART_REQUEST_PAYLOAD_MAX]; |
kishino | 12:0254eaccfda2 | 410 | unsigned int command_len; |
kishino | 12:0254eaccfda2 | 411 | // Preparation of command |
kishino | 12:0254eaccfda2 | 412 | command_len = mSNICWifi_p->preparationSendCommand( UART_CMD_ID_WIFI, req.cmd_sid, (unsigned char *)&req |
kishino | 22:a9ec0cad4f84 | 413 | , sizeof(C_SNIC_Core::tagWIFI_OFF_REQ_T), payload_buf->buf, command_array ); |
kishino | 12:0254eaccfda2 | 414 | |
kishino | 12:0254eaccfda2 | 415 | // Send uart command request |
kishino | 12:0254eaccfda2 | 416 | mSNICWifi_p->sendUart( command_len, command_array ); |
kishino | 12:0254eaccfda2 | 417 | |
kishino | 12:0254eaccfda2 | 418 | int ret; |
kishino | 12:0254eaccfda2 | 419 | // Wait UART response |
kishino | 16:6100acfeb1f1 | 420 | ret = uartCmdMgr_p->wait(); |
kishino | 12:0254eaccfda2 | 421 | if( ret != 0 ) |
kishino | 12:0254eaccfda2 | 422 | { |
kishino | 12:0254eaccfda2 | 423 | printf( "wifi_off failed\r\n" ); |
kishino | 12:0254eaccfda2 | 424 | mSNICWifi_p->freeCmdBuf( payload_buf ); |
kishino | 12:0254eaccfda2 | 425 | return -1; |
kishino | 12:0254eaccfda2 | 426 | } |
kishino | 12:0254eaccfda2 | 427 | |
kishino | 16:6100acfeb1f1 | 428 | if( uartCmdMgr_p->getCommandStatus() != 0 ) |
kishino | 12:0254eaccfda2 | 429 | { |
kishino | 16:6100acfeb1f1 | 430 | printf("wifi_off status:%02x\r\n", uartCmdMgr_p->getCommandStatus()); |
kishino | 12:0254eaccfda2 | 431 | ret = -1; |
kishino | 12:0254eaccfda2 | 432 | } |
kishino | 12:0254eaccfda2 | 433 | mSNICWifi_p->freeCmdBuf( payload_buf ); |
kishino | 12:0254eaccfda2 | 434 | |
kishino | 12:0254eaccfda2 | 435 | return ret; |
kishino | 12:0254eaccfda2 | 436 | } |
kishino | 12:0254eaccfda2 | 437 | |
kishino | 12:0254eaccfda2 | 438 | int C_SNIC_WifiInterface::getRssi( signed char *rssi_p ) |
kishino | 12:0254eaccfda2 | 439 | { |
kishino | 16:6100acfeb1f1 | 440 | C_SNIC_UartCommandManager *uartCmdMgr_p = mSNICWifi_p->getUartCommand(); |
kishino | 12:0254eaccfda2 | 441 | if( rssi_p == NULL ) |
kishino | 12:0254eaccfda2 | 442 | { |
kishino | 12:0254eaccfda2 | 443 | printf("getRssi parameter error\r\n"); |
kishino | 12:0254eaccfda2 | 444 | return -1; |
kishino | 12:0254eaccfda2 | 445 | } |
kishino | 12:0254eaccfda2 | 446 | |
kishino | 12:0254eaccfda2 | 447 | // Get buffer for response payload from MemoryPool |
kishino | 22:a9ec0cad4f84 | 448 | C_SNIC_Core::tagMEMPOOL_BLOCK_T *payload_buf = mSNICWifi_p->allocCmdBuf(); |
kishino | 12:0254eaccfda2 | 449 | if( payload_buf == NULL ) |
kishino | 12:0254eaccfda2 | 450 | { |
kishino | 12:0254eaccfda2 | 451 | printf("getRssi payload_buf NULL\r\n"); |
kishino | 12:0254eaccfda2 | 452 | return -1; |
kishino | 12:0254eaccfda2 | 453 | } |
kishino | 12:0254eaccfda2 | 454 | |
kishino | 22:a9ec0cad4f84 | 455 | C_SNIC_Core::tagWIFI_GET_STA_RSSI_REQ_T req; |
kishino | 12:0254eaccfda2 | 456 | |
kishino | 12:0254eaccfda2 | 457 | // Make request |
kishino | 12:0254eaccfda2 | 458 | req.cmd_sid = UART_CMD_SID_WIFI_GET_STA_RSSI_REQ; |
kishino | 12:0254eaccfda2 | 459 | req.seq = mUartRequestSeq++; |
kishino | 12:0254eaccfda2 | 460 | |
kishino | 12:0254eaccfda2 | 461 | unsigned char command_array[UART_REQUEST_PAYLOAD_MAX]; |
kishino | 12:0254eaccfda2 | 462 | unsigned int command_len; |
kishino | 12:0254eaccfda2 | 463 | command_len = mSNICWifi_p->preparationSendCommand( UART_CMD_ID_WIFI, req.cmd_sid, (unsigned char *)&req |
kishino | 22:a9ec0cad4f84 | 464 | , sizeof(C_SNIC_Core::tagWIFI_GET_STA_RSSI_REQ_T), payload_buf->buf, command_array ); |
kishino | 12:0254eaccfda2 | 465 | |
kishino | 12:0254eaccfda2 | 466 | int ret; |
kishino | 12:0254eaccfda2 | 467 | // Send uart command request |
kishino | 12:0254eaccfda2 | 468 | mSNICWifi_p->sendUart( command_len, command_array ); |
kishino | 12:0254eaccfda2 | 469 | |
kishino | 12:0254eaccfda2 | 470 | // Wait UART response |
kishino | 16:6100acfeb1f1 | 471 | ret = uartCmdMgr_p->wait(); |
kishino | 12:0254eaccfda2 | 472 | if( ret != 0 ) |
kishino | 12:0254eaccfda2 | 473 | { |
kishino | 12:0254eaccfda2 | 474 | printf( "getRssi failed\r\n" ); |
kishino | 12:0254eaccfda2 | 475 | mSNICWifi_p->freeCmdBuf( payload_buf ); |
kishino | 12:0254eaccfda2 | 476 | return -1; |
kishino | 12:0254eaccfda2 | 477 | } |
kishino | 12:0254eaccfda2 | 478 | |
kishino | 12:0254eaccfda2 | 479 | *rssi_p = (signed char)payload_buf->buf[2]; |
kishino | 12:0254eaccfda2 | 480 | |
kishino | 12:0254eaccfda2 | 481 | mSNICWifi_p->freeCmdBuf( payload_buf ); |
kishino | 12:0254eaccfda2 | 482 | return 0; |
kishino | 12:0254eaccfda2 | 483 | } |
kishino | 12:0254eaccfda2 | 484 | |
kishino | 12:0254eaccfda2 | 485 | int C_SNIC_WifiInterface::getWifiStatus( tagWIFI_STATUS_T *status_p) |
kishino | 12:0254eaccfda2 | 486 | { |
kishino | 16:6100acfeb1f1 | 487 | C_SNIC_UartCommandManager *uartCmdMgr_p = mSNICWifi_p->getUartCommand(); |
kishino | 16:6100acfeb1f1 | 488 | |
kishino | 12:0254eaccfda2 | 489 | if( status_p == NULL ) |
kishino | 12:0254eaccfda2 | 490 | { |
kishino | 12:0254eaccfda2 | 491 | printf("getWifiStatus parameter error\r\n"); |
kishino | 12:0254eaccfda2 | 492 | return -1; |
kishino | 12:0254eaccfda2 | 493 | } |
kishino | 12:0254eaccfda2 | 494 | |
kishino | 12:0254eaccfda2 | 495 | // Get buffer for response payload from MemoryPool |
kishino | 22:a9ec0cad4f84 | 496 | C_SNIC_Core::tagMEMPOOL_BLOCK_T *payload_buf = mSNICWifi_p->allocCmdBuf(); |
kishino | 12:0254eaccfda2 | 497 | if( payload_buf == NULL ) |
kishino | 12:0254eaccfda2 | 498 | { |
kishino | 12:0254eaccfda2 | 499 | printf("getWifiStatus payload_buf NULL\r\n"); |
kishino | 12:0254eaccfda2 | 500 | return -1; |
kishino | 12:0254eaccfda2 | 501 | } |
kishino | 12:0254eaccfda2 | 502 | |
kishino | 22:a9ec0cad4f84 | 503 | C_SNIC_Core::tagWIFI_GET_STATUS_REQ_T req; |
kishino | 12:0254eaccfda2 | 504 | // Make request |
kishino | 12:0254eaccfda2 | 505 | req.cmd_sid = UART_CMD_SID_WIFI_GET_STATUS_REQ; |
kishino | 12:0254eaccfda2 | 506 | req.seq = mUartRequestSeq++; |
kishino | 12:0254eaccfda2 | 507 | req.interface = 0; |
kishino | 12:0254eaccfda2 | 508 | |
kishino | 12:0254eaccfda2 | 509 | unsigned char command_array[UART_REQUEST_PAYLOAD_MAX]; |
kishino | 12:0254eaccfda2 | 510 | unsigned int command_len; |
kishino | 12:0254eaccfda2 | 511 | command_len = mSNICWifi_p->preparationSendCommand( UART_CMD_ID_WIFI, req.cmd_sid, (unsigned char *)&req |
kishino | 22:a9ec0cad4f84 | 512 | , sizeof(C_SNIC_Core::tagWIFI_GET_STATUS_REQ_T), payload_buf->buf, command_array ); |
kishino | 12:0254eaccfda2 | 513 | |
kishino | 12:0254eaccfda2 | 514 | // Send uart command request |
kishino | 12:0254eaccfda2 | 515 | mSNICWifi_p->sendUart( command_len, command_array ); |
kishino | 12:0254eaccfda2 | 516 | |
kishino | 12:0254eaccfda2 | 517 | int ret; |
kishino | 12:0254eaccfda2 | 518 | // Wait UART response |
kishino | 16:6100acfeb1f1 | 519 | ret = uartCmdMgr_p->wait(); |
kishino | 12:0254eaccfda2 | 520 | if( ret != 0 ) |
kishino | 12:0254eaccfda2 | 521 | { |
kishino | 12:0254eaccfda2 | 522 | printf( "getWifiStatus failed\r\n" ); |
kishino | 12:0254eaccfda2 | 523 | mSNICWifi_p->freeCmdBuf( payload_buf ); |
kishino | 12:0254eaccfda2 | 524 | return -1; |
kishino | 12:0254eaccfda2 | 525 | } |
kishino | 12:0254eaccfda2 | 526 | |
kishino | 12:0254eaccfda2 | 527 | // set status |
kishino | 12:0254eaccfda2 | 528 | status_p->status = (E_WIFI_STATUS)payload_buf->buf[2]; |
kishino | 12:0254eaccfda2 | 529 | |
kishino | 12:0254eaccfda2 | 530 | // set Mac address |
kishino | 12:0254eaccfda2 | 531 | if( status_p->status != e_STATUS_OFF ) |
kishino | 12:0254eaccfda2 | 532 | { |
kishino | 12:0254eaccfda2 | 533 | memcpy( status_p->mac_address, &payload_buf->buf[3], BSSID_MAC_LENTH ); |
kishino | 12:0254eaccfda2 | 534 | } |
kishino | 12:0254eaccfda2 | 535 | |
kishino | 12:0254eaccfda2 | 536 | // set SSID |
kishino | 12:0254eaccfda2 | 537 | if( ( status_p->status == e_STA_JOINED ) == ( status_p->status == e_AP_STARTED ) ) |
kishino | 12:0254eaccfda2 | 538 | { |
kishino | 12:0254eaccfda2 | 539 | memcpy( status_p->ssid, &payload_buf->buf[9], strlen( (char *)&payload_buf->buf[9]) ); |
kishino | 12:0254eaccfda2 | 540 | } |
kishino | 12:0254eaccfda2 | 541 | |
kishino | 12:0254eaccfda2 | 542 | mSNICWifi_p->freeCmdBuf( payload_buf ); |
kishino | 12:0254eaccfda2 | 543 | return 0; |
kishino | 12:0254eaccfda2 | 544 | } |