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