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