Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: SpwfInterface_NSAPI_Tests HelloWorld_IDW01M1
Fork of X_NUCLEO_IDW01M1 by
wifi_interface.c
00001 /** 00002 ****************************************************************************** 00003 * @file wifi_interface.c 00004 * @author Central LAB 00005 * @version V2.0.0 00006 * @date 10-February-2016 00007 * @brief User APIs implementation for X-CUBE-WIFI1 00008 ****************************************************************************** 00009 * @attention 00010 * 00011 * <h2><center>© COPYRIGHT(c) 2015 STMicroelectronics</center></h2> 00012 * 00013 * Redistribution and use in source and binary forms, with or without modification, 00014 * are permitted provided that the following conditions are met: 00015 * 1. Redistributions of source code must retain the above copyright notice, 00016 * this list of conditions and the following disclaimer. 00017 * 2. Redistributions in binary form must reproduce the above copyright notice, 00018 * this list of conditions and the following disclaimer in the documentation 00019 * and/or other materials provided with the distribution. 00020 * 3. Neither the name of STMicroelectronics nor the names of its contributors 00021 * may be used to endorse or promote products derived from this software 00022 * without specific prior written permission. 00023 * 00024 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 00025 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00026 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 00027 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 00028 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 00029 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 00030 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 00031 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 00032 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 00033 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00034 * 00035 ****************************************************************************** 00036 */ 00037 00038 /* Includes ------------------------------------------------------------------*/ 00039 #include "wifi_module.h" 00040 //#include "stm32_spwf_wifi.h" 00041 #include "ring_buffer.h" 00042 #include "stdio.h" 00043 #include "string.h" 00044 #include "gpio_irq_api.h" 00045 #include "gpio_api.h" 00046 #include "wait_api.h" 00047 #include "serial_api.h" 00048 00049 /** @addtogroup MIDDLEWARES 00050 * @{ 00051 */ 00052 00053 00054 /** @defgroup NUCLEO_WIFI_INTERFACE 00055 * @brief Wi-Fi User API modules 00056 * @{ 00057 */ 00058 00059 00060 /** @defgroup NUCLEO_WIFI_INTERFACE_Private_Defines 00061 * @{ 00062 */ 00063 00064 00065 /** 00066 * @} 00067 */ 00068 00069 /** @addtogroup NUCLEO_WIFI_INTERFACE_Private_Variables 00070 * @{ 00071 */ 00072 /* Private variables ---------------------------------------------------------*/ 00073 00074 extern uint8_t WiFi_AT_Cmd_Buff[2052]; 00075 00076 extern volatile Wifi_Status_Var status_flag; 00077 00078 extern char UserDataBuff[MAX_BUFFER_GLOBAL]; 00079 extern buffer_e event_buff; 00080 extern wifi_scan *wifi_scanned_list;//[15]; 00081 extern char print_msg_buff[MAX_BUFFER_GLOBAL]; 00082 extern uint8_t user_scan_number; 00083 extern uint8_t no_of_open_client_sockets; 00084 extern wifi_bool open_sockets[8];//Max open sockets allowed is 8. Each array element depicts one socket (true=open, false=closed) 00085 extern WiFi_AT_CMD_Response_t WiFi_Module_State; 00086 extern volatile uint8_t wifi_client_connected; 00087 extern uint8_t gpio_value, gpio_direc; 00088 extern volatile uint8_t wifi_connected; 00089 extern uint32_t SockON_Data_Length; 00090 extern uint8_t Socket_Open_ID, sockon_query_id; 00091 extern volatile uint32_t WIND64_count; 00092 extern uint32_t process_buffer_index; 00093 extern uint32_t epoch_time; 00094 extern int write_size; 00095 /*SPWFSADevice Class functions and variables*/ 00096 void * spwf_device_class; 00097 extern void callSpwfSADevice_wakeup(void* object, int wake); 00098 extern void callSpwfSADevice_debug(void* object, const char * string); 00099 /** 00100 * @} 00101 */ 00102 00103 /** @defgroup NUCLEO_WIFI_INTERFACE_Private_Functions 00104 * @{ 00105 */ 00106 #ifdef USART_PRINT_MSG 00107 #define printf(arg) {sprintf((char*)print_msg_buff,arg); \ 00108 HAL_UART_Transmit(&UartMsgHandle, (uint8_t*)print_msg_buff, strlen(print_msg_buff), 1000);} 00109 #endif 00110 00111 /** 00112 *Changed/introduced functions for MBED implementation: 00113 00114 - WiFi_Status_t wifi_init(wifi_config* config) 00115 - WiFi_Status_t wifi_wakeup(wifi_bool wakeup) 00116 - WiFi_Status_t wifi_socket_server_write(uint16_t DataLength,char * pData) 00117 - void wifi_reset(void); 00118 - WiFi_Status_t wifi_enable(wifi_bool enable) 00119 00120 */ 00121 00122 00123 /** 00124 * @brief wifi_init 00125 * User API for wifi init 00126 * @param None 00127 * @retval None 00128 */ 00129 WiFi_Status_t wifi_init(wifi_config* config) 00130 { 00131 #ifndef WIFI_USE_VCOM 00132 uint8_t tx_level; 00133 #endif 00134 00135 WiFi_Status_t status = WiFi_MODULE_SUCCESS; 00136 00137 #if DEBUG_PRINT 00138 printf("\r\n[SPWF]Initializing SPWF01SA1 Interface\r\n"); 00139 #endif 00140 //callSpwfSADevice_debug(spwf_device_class, "\r\n[SPWF] Initializing mbed SPWF01SA1 Interface\r\n"); 00141 00142 WiFi_Module_Init(); 00143 00144 //callSpwfSADevice_debug(spwf_device_class, "\r\n[SPWF] Module Init done\r\n"); 00145 00146 #ifndef WIFI_USE_VCOM 00147 00148 wifi_wakeup(WIFI_TRUE);//Prevent from going to sleep during configuration 00149 00150 /* Soft reset the module */ 00151 wifi_reset(); 00152 00153 //callSpwfSADevice_debug(spwf_device_class, "\r\n[SPWF] Hardware started\r\n"); 00154 00155 /* Set localecho1 to 0*/ 00156 status = SET_Configuration_Value(LOCALECHO1, 0); 00157 if(status != WiFi_MODULE_SUCCESS) return status; 00158 00159 /* Restore default setting*/ 00160 Reset_AT_CMD_Buffer(); 00161 sprintf((char*)WiFi_AT_Cmd_Buff,AT_RESTORE_DEFAULT_SETTING); 00162 status = USART_Transmit_AT_Cmd(strlen((char*)WiFi_AT_Cmd_Buff)); 00163 if(status == WiFi_MODULE_SUCCESS) 00164 { 00165 status = USART_Receive_AT_Resp(Process_Event); 00166 if(status != WiFi_MODULE_SUCCESS) return status; 00167 } 00168 00169 /* Switch on HW Flow Control*/ 00170 status = SET_Configuration_Value(CONSOLE1_HWFC, 1); 00171 if(status != WiFi_MODULE_SUCCESS) return status; 00172 00173 if(config->wifi_baud_rate) 00174 { 00175 /* Set USART Speed*/ 00176 status = SET_Configuration_Value(CONSOLE1_SPEED, config->wifi_baud_rate); 00177 if(status != WiFi_MODULE_SUCCESS) return status; 00178 } 00179 00180 /* Set wifi_mode to idle*/ 00181 status = SET_Configuration_Value(WIFI_MODE, WiFi_IDLE_MODE); 00182 if(status != WiFi_MODULE_SUCCESS) return status; 00183 00184 switch(config->ht_mode) 00185 { 00186 case WIFI_FALSE: 00187 status = SET_Configuration_Value(WIFI_HT_MODE, 0); 00188 if(status != WiFi_MODULE_SUCCESS) return status; 00189 status = SET_Configuration_Addr(WIFI_OPR_RATE_MASK, "0x00003FCF"); 00190 if(status != WiFi_MODULE_SUCCESS) return status; 00191 break; 00192 case WIFI_TRUE: 00193 status = SET_Configuration_Value(WIFI_HT_MODE, 1); 00194 if(status != WiFi_MODULE_SUCCESS) return status; 00195 status = SET_Configuration_Addr(WIFI_OPR_RATE_MASK, "0x003FFFCF"); 00196 if(status != WiFi_MODULE_SUCCESS) return status; 00197 break; 00198 default: 00199 break; 00200 } 00201 00202 switch(config->power) 00203 { 00204 case wifi_active: 00205 status = SET_Configuration_Value(WIFI_POWERSAVE, 0); 00206 if(status != WiFi_MODULE_SUCCESS) return status; 00207 status = SET_Configuration_Value(WIFI_SLEEP_ENABLED, 0); 00208 if(status != WiFi_MODULE_SUCCESS) return status; 00209 break; 00210 case wifi_reactive: 00211 status = SET_Configuration_Value(WIFI_POWERSAVE, 1); 00212 if(status != WiFi_MODULE_SUCCESS) return status; 00213 status = SET_Configuration_Value(WIFI_OPERATIONAL_MODE, 11); 00214 if(status != WiFi_MODULE_SUCCESS) return status; 00215 status = SET_Configuration_Value(WIFI_BEACON_WAKEUP, 0); 00216 if(status != WiFi_MODULE_SUCCESS) return status; 00217 status = SET_Configuration_Value(WIFI_LISTEN_INTERVAL, 0); 00218 if(status != WiFi_MODULE_SUCCESS) return status; 00219 status = SET_Configuration_Value(WIFI_SLEEP_ENABLED, 0); 00220 if(status != WiFi_MODULE_SUCCESS) return status; 00221 break; 00222 case wifi_sleep: 00223 status = SET_Configuration_Value(WIFI_POWERSAVE, 1); 00224 if(status != WiFi_MODULE_SUCCESS) return status; 00225 status = SET_Configuration_Value(WIFI_OPERATIONAL_MODE, 12); 00226 if(status != WiFi_MODULE_SUCCESS) return status; 00227 status = SET_Configuration_Value(WIFI_BEACON_WAKEUP, 10); 00228 if(status != WiFi_MODULE_SUCCESS) return status; 00229 status = SET_Configuration_Value(WIFI_LISTEN_INTERVAL, 1); 00230 if(status != WiFi_MODULE_SUCCESS) return status; 00231 status = SET_Configuration_Value(WIFI_SLEEP_ENABLED, 1); 00232 if(status != WiFi_MODULE_SUCCESS) return status; 00233 break; 00234 default: 00235 break; 00236 } 00237 00238 switch(config->power_level) 00239 { 00240 case low: 00241 case medium: 00242 case high: 00243 case max: 00244 tx_level=config->power_level*6; 00245 status = SET_Configuration_Value(WIFI_TX_POWER, tx_level); 00246 if(status != WiFi_MODULE_SUCCESS) return status; 00247 break; 00248 default: 00249 break; 00250 } 00251 00252 switch(config->dhcp) 00253 { 00254 case off: 00255 case on: 00256 case custom: 00257 status = SET_Configuration_Value(IP_USE_DHCP_SERVER, config->dhcp); 00258 if(status != WiFi_MODULE_SUCCESS) return status; 00259 break; 00260 default: 00261 break; 00262 } 00263 00264 /* Set IP address */ 00265 if(config->ip_addr) 00266 { 00267 status = SET_Configuration_Addr(WIFI_IP_ADDRESS, config->ip_addr); 00268 if(status != WiFi_MODULE_SUCCESS) return status; 00269 } 00270 /* Set netmask address */ 00271 if(config->netmask_addr) 00272 { 00273 status = SET_Configuration_Addr(WIFI_IP_NETMASK, config->netmask_addr); 00274 if(status != WiFi_MODULE_SUCCESS) return status; 00275 } 00276 /* Set default gateway address */ 00277 if(config->gateway_addr) 00278 { 00279 status = SET_Configuration_Addr(WIFI_IP_DEFAULT_GATEWAY, config->gateway_addr); 00280 if(status != WiFi_MODULE_SUCCESS) return status; 00281 } 00282 /* Set dns address */ 00283 if(config->dns_addr) 00284 { 00285 status = SET_Configuration_Addr(WIFI_IP_DNS, config->dns_addr); 00286 if(status != WiFi_MODULE_SUCCESS) return status; 00287 } 00288 /* Set hostname */ 00289 if(config->host_name) 00290 { 00291 status = SET_Configuration_Addr(WIFI_IP_HOSTNAME, config->host_name); 00292 if(status != WiFi_MODULE_SUCCESS) return status; 00293 } 00294 00295 if(config->ap_domain_name) 00296 { 00297 status = SET_Configuration_Addr(WIFI_IP_APDOMAINNAME, config->ap_domain_name); 00298 if(status != WiFi_MODULE_SUCCESS) return status; 00299 } 00300 00301 if(config->ap_config_page_name) 00302 { 00303 status = SET_Configuration_Addr(WIFI_IP_APREDIRECT, config->ap_config_page_name); 00304 if(status != WiFi_MODULE_SUCCESS) return status; 00305 } 00306 00307 if(config->http_timeout) 00308 { 00309 status = SET_Configuration_Value(WIFI_IP_HTTP_TIMEOUT, config->http_timeout*1000); 00310 if(status != WiFi_MODULE_SUCCESS) return status; 00311 } 00312 if(config->dhcp_timeout) 00313 { 00314 status = SET_Configuration_Value(WIFI_IP_DHCP_TIMEOUT, config->dhcp_timeout); 00315 if(status != WiFi_MODULE_SUCCESS) return status; 00316 } 00317 00318 #ifdef MODULE_VERSION_SPWF01Sx_1y 00319 Reset_AT_CMD_Buffer(); 00320 sprintf((char*)WiFi_AT_Cmd_Buff,AT_HTTPD, config->web_server); 00321 status = USART_Transmit_AT_Cmd(strlen((char*)WiFi_AT_Cmd_Buff)); 00322 if(status == WiFi_MODULE_SUCCESS) 00323 { 00324 status = USART_Receive_AT_Resp(Process_Event); 00325 if(status != WiFi_MODULE_SUCCESS) return status; 00326 } 00327 #endif 00328 00329 /*AT+S.TLSCERT2=clean,all */ 00330 Reset_AT_CMD_Buffer(); 00331 sprintf((char*)WiFi_AT_Cmd_Buff,"AT+S.TLSCERT2=clean,all\r"); 00332 status = USART_Transmit_AT_Cmd(strlen((char*)WiFi_AT_Cmd_Buff)); 00333 if(status == WiFi_MODULE_SUCCESS) 00334 { 00335 status = USART_Receive_AT_Resp(Process_Event); 00336 if(status != WiFi_MODULE_SUCCESS) return status; 00337 } 00338 00339 /* save current setting in flash */ 00340 Reset_AT_CMD_Buffer(); 00341 sprintf((char*)WiFi_AT_Cmd_Buff,AT_SAVE_CURRENT_SETTING); 00342 status = USART_Transmit_AT_Cmd(strlen((char*)WiFi_AT_Cmd_Buff)); 00343 if(status == WiFi_MODULE_SUCCESS) 00344 { 00345 status = USART_Receive_AT_Resp(Process_Event); 00346 if(status != WiFi_MODULE_SUCCESS) return status; 00347 } 00348 00349 if(config->wifi_baud_rate) 00350 { 00351 UART_Configuration(config->wifi_baud_rate); 00352 Receive_Data();//Restart data reception 00353 } 00354 00355 /* Soft reset the module, Do the second reset after setting all parameters and saving in flash */ 00356 wifi_reset(); 00357 00358 wifi_wakeup(WIFI_FALSE);//De-assert wakeup signal (PC8) to allow sleep if enabled 00359 #endif //WIFI_USE_VCOM 00360 00361 #if DEBUG_PRINT 00362 printf("\r\nEnd of Initialization..\r\n"); 00363 #endif 00364 //callSpwfSADevice_debug(spwf_device_class, "\r\n[SPWF] End of mbed Initialization\r\n"); 00365 00366 return status; 00367 } 00368 00369 00370 /** 00371 * @brief wifi_socket_client_security 00372 * Set the security certificates and key for secure socket (TLS) 00373 * @param None 00374 * @retval WiFi_Status_t : return status 00375 */ 00376 WiFi_Status_t wifi_socket_client_security(uint8_t* tls_mode, uint8_t* root_ca_server, uint8_t* client_cert, uint8_t* client_key, uint8_t* client_domain, uint32_t tls_epoch_time) 00377 { 00378 WiFi_Status_t status = WiFi_MODULE_SUCCESS; 00379 00380 /*AT+S.TLSCERT2=clean,all */ 00381 Reset_AT_CMD_Buffer(); 00382 sprintf((char*)WiFi_AT_Cmd_Buff,"AT+S.TLSCERT2=clean,all\r"); 00383 status = USART_Transmit_AT_Cmd(strlen((char*)WiFi_AT_Cmd_Buff)); 00384 if(status == WiFi_MODULE_SUCCESS) 00385 { 00386 status = USART_Receive_AT_Resp(Process_Event); 00387 if(status != WiFi_MODULE_SUCCESS) return status; 00388 } 00389 00390 /* AT+S.SETTIME=<seconds> */ 00391 Reset_AT_CMD_Buffer(); 00392 if(tls_epoch_time==0) 00393 epoch_time = EPOCH_TIME; 00394 else 00395 epoch_time = tls_epoch_time; 00396 00397 sprintf((char*)WiFi_AT_Cmd_Buff,"AT+S.SETTIME=%lu\r",(unsigned long)epoch_time); 00398 status = USART_Transmit_AT_Cmd(strlen((char*)WiFi_AT_Cmd_Buff)); 00399 if(status == WiFi_MODULE_SUCCESS) 00400 { 00401 status = USART_Receive_AT_Resp(Process_Event); 00402 if(status != WiFi_MODULE_SUCCESS) return status; 00403 } 00404 00405 /*AT+S.TLSCERT=f_ca,<size><CR><data>*/ 00406 Reset_AT_CMD_Buffer(); 00407 sprintf((char*)WiFi_AT_Cmd_Buff,"AT+S.TLSCERT=f_ca,%d\r%s",strlen((const char *)root_ca_server) - 1, root_ca_server); 00408 00409 status = USART_Transmit_AT_Cmd(strlen((char*)WiFi_AT_Cmd_Buff)); 00410 if(status == WiFi_MODULE_SUCCESS) 00411 { 00412 status = USART_Receive_AT_Resp(Process_Event); 00413 if(status != WiFi_MODULE_SUCCESS) return status; 00414 } 00415 00416 /*AT+S.TLSCERT=f_cert,<size><CR><data>*/ 00417 if(tls_mode[0]=='m') 00418 { 00419 Reset_AT_CMD_Buffer(); 00420 sprintf((char*)WiFi_AT_Cmd_Buff,"AT+S.TLSCERT=f_cert,%d\r%s",strlen((const char *)client_cert) - 1, client_cert); 00421 00422 status = USART_Transmit_AT_Cmd(strlen((char*)WiFi_AT_Cmd_Buff)); 00423 if(status == WiFi_MODULE_SUCCESS) 00424 { 00425 status = USART_Receive_AT_Resp(Process_Event); 00426 if(status != WiFi_MODULE_SUCCESS) return status; 00427 } 00428 00429 /*AT+S.TLSCERT=f_key,<size><CR><data>*/ 00430 Reset_AT_CMD_Buffer(); 00431 sprintf((char*)WiFi_AT_Cmd_Buff,"AT+S.TLSCERT=f_key,%d\r%s",strlen((const char *)client_key) - 1, client_key); 00432 00433 status = USART_Transmit_AT_Cmd(strlen((char*)WiFi_AT_Cmd_Buff)); 00434 if(status == WiFi_MODULE_SUCCESS) 00435 { 00436 status = USART_Receive_AT_Resp(Process_Event); 00437 if(status != WiFi_MODULE_SUCCESS) return status; 00438 } 00439 } 00440 00441 /*AT+S.TLSDOMAIN=f_domain,<server domain>*/ 00442 Reset_AT_CMD_Buffer(); 00443 sprintf((char*)WiFi_AT_Cmd_Buff,"AT+S.TLSDOMAIN=f_domain,%s\r", client_domain); 00444 status = USART_Transmit_AT_Cmd(strlen((char*)WiFi_AT_Cmd_Buff)); 00445 if(status == WiFi_MODULE_SUCCESS) 00446 { 00447 status = USART_Receive_AT_Resp(Process_Event); 00448 if(status != WiFi_MODULE_SUCCESS) return status; 00449 } 00450 00451 return status; 00452 } 00453 00454 /** 00455 * @brief wifi_socket_client_open 00456 * Open a network socket 00457 * @param Hostname hostname to connect to 00458 * portnumber portnumber of the Host to connect to 00459 * protocol tcp or udp protocol 00460 * sock_id socket id of the opened socket returned to the user 00461 * @retval WiFi_Status_t : return status of socket open request 00462 */ 00463 WiFi_Status_t wifi_socket_client_open(uint8_t * hostname, uint32_t port_number, uint8_t * protocol, uint8_t * sock_id) 00464 { 00465 WiFi_Status_t status = WiFi_MODULE_SUCCESS; 00466 00467 Queue_Client_Open_Event(hostname,port_number,protocol); 00468 status = USART_Receive_AT_Resp(Process_Event); 00469 00470 *sock_id = Socket_Open_ID; //return the socket id to the user 00471 00472 return status; 00473 } 00474 00475 /** 00476 * @brief Open_Serial_Port 00477 * Open a network socket 00478 * @param None 00479 * @retval WiFi_Status_t : Wifi status 00480 */ 00481 WiFi_Status_t Open_Serial_Port() 00482 { 00483 WiFi_Status_t status = WiFi_MODULE_SUCCESS; 00484 /* AT+S.SOCKOS=2<cr> */ 00485 Reset_AT_CMD_Buffer(); 00486 //sprintf((char*)WiFi_AT_Cmd_Buff,"\rAT+S.SOCKOS=%d\r",SerialPortNo); 00487 status = USART_Transmit_AT_Cmd(strlen((char*)WiFi_AT_Cmd_Buff)); 00488 00489 if(status==WiFi_MODULE_SUCCESS) 00490 { 00491 status = USART_Receive_AT_Resp(Process_Event); 00492 } 00493 return status; 00494 } 00495 00496 /** 00497 * @brief wifi_socket_client_write 00498 * Write len bytes of data to socket 00499 * @param sock_id socket ID of the socket to write to 00500 * DataLength: data length to send 00501 * pData : pointer of data buffer to be written 00502 * @retval int : return number of bytes sent 00503 */ 00504 int wifi_socket_client_write(uint8_t sock_id, uint16_t DataLength, char * pData) 00505 { 00506 /* AT+S.SOCKW=00,11<cr> */ 00507 WiFi_Status_t status = WiFi_MODULE_SUCCESS; 00508 00509 //Check if sock_id is open 00510 if(!open_sockets[sock_id]) 00511 return -1; 00512 00513 if(DataLength>2048 || DataLength<=0) 00514 return -1; 00515 00516 Queue_Client_Write_Event(sock_id,DataLength,pData); 00517 status = USART_Receive_AT_Resp(Process_Event); 00518 00519 if(status==WiFi_MODULE_SUCCESS) 00520 return write_size; 00521 else 00522 return -1; 00523 } 00524 00525 /** 00526 * @brief Socket_Read 00527 * Return len bytes of data from socket 00528 * @param DataLength: data length to read 00529 * @retval WiFi_Status_t : return status of socket read request 00530 */ 00531 WiFi_Status_t Socket_Read(uint16_t DataLength) 00532 { 00533 /* AT+S.SOCKR=01,12<cr> */ 00534 WiFi_Status_t status = WiFi_MODULE_SUCCESS; 00535 wait_for_command_mode(); 00536 00537 /* AT+S.SOCKON=myserver,1234,t<cr> */ 00538 sprintf((char*)WiFi_AT_Cmd_Buff,AT_SOCKET_READ,sockon_query_id,DataLength); 00539 status = USART_Transmit_AT_Cmd(strlen((char*)WiFi_AT_Cmd_Buff)); 00540 if(status == WiFi_MODULE_SUCCESS) 00541 { 00542 if(status_flag.stop_event_dequeue == WIFI_FALSE) 00543 status_flag.stop_event_dequeue = WIFI_TRUE; 00544 SockON_Data_Length = DataLength; 00545 status_flag.enable_sock_read = WIFI_TRUE; 00546 process_buffer_index =5; 00547 status_flag.enable_receive_data_chunk = WIFI_TRUE; 00548 WiFi_Module_State = Process_Event; 00549 status_flag.Set_AT_Cmd_Response_False = WIFI_TRUE; 00550 memset(UserDataBuff, 0x00, MAX_BUFFER_GLOBAL); 00551 } 00552 return status; 00553 } 00554 00555 /** 00556 * @brief wifi_socket_client_close 00557 * The SOCKC command allows to close socket 00558 * @param sock_close_id the socket ID of the socket which needs to be closed. 00559 * @retval WiFi_Status_t : return status of socket close request 00560 */ 00561 WiFi_Status_t wifi_socket_client_close(uint8_t sock_close_id) 00562 { 00563 WiFi_Status_t status = WiFi_MODULE_SUCCESS; 00564 00565 if(open_sockets[sock_close_id]) 00566 { 00567 Queue_Client_Close_Event(sock_close_id); 00568 return status; 00569 } 00570 else 00571 return WiFi_MODULE_ERROR; 00572 00573 } 00574 00575 /** 00576 * @brief Socket_Pending_Data 00577 * Query pending data.It will returns the number of bytes of data waiting on socket 00578 * @param None 00579 * @retval uint8_t :number of bytes of data waiting on socket 00580 */ 00581 void Socket_Pending_Data() 00582 { 00583 WiFi_Status_t status = WiFi_MODULE_SUCCESS; 00584 00585 /* AT+S.SOCKQ=01<cr> */ 00586 Reset_AT_CMD_Buffer(); 00587 00588 wait_for_command_mode(); 00589 00590 if(open_sockets[sockon_query_id]) 00591 { 00592 if(status_flag.stop_event_dequeue == WIFI_FALSE) 00593 status_flag.stop_event_dequeue = WIFI_TRUE; 00594 00595 sprintf((char*)WiFi_AT_Cmd_Buff,AT_QUERY_PENDING_DATA,sockon_query_id); 00596 status = USART_Transmit_AT_Cmd(strlen((char*)WiFi_AT_Cmd_Buff)); 00597 if(status==WiFi_MODULE_SUCCESS) 00598 { 00599 /* EQ. Set state to Process_Event */ 00600 WiFi_Module_State = Process_Event; 00601 status_flag.Set_AT_Cmd_Response_False = WIFI_TRUE; 00602 } 00603 } 00604 } 00605 00606 /** 00607 * @brief wifi_socket_server_open 00608 * Open a Server socket 00609 * @param None 00610 * @retval WiFi_Status_t : return status of server socket request 00611 */ 00612 WiFi_Status_t wifi_socket_server_open(uint32_t port_number, uint8_t * protocol) 00613 { 00614 WiFi_Status_t status = WiFi_MODULE_SUCCESS; 00615 00616 //Wait till all queued events are executed 00617 while(!event_empty(&event_buff)); 00618 Wait_For_Sock_Read_To_Complete(); 00619 00620 Reset_AT_CMD_Buffer(); 00621 00622 /* AT+S.SOCKD=portNo,t<cr> */ 00623 sprintf((char*)WiFi_AT_Cmd_Buff,AT_SERVER_SOCKET_OPEN,(int)port_number,protocol); 00624 status = USART_Transmit_AT_Cmd(strlen((char*)WiFi_AT_Cmd_Buff)); 00625 if(status == WiFi_MODULE_SUCCESS) 00626 { 00627 status = USART_Receive_AT_Resp(Process_Event); 00628 } 00629 return status; 00630 } 00631 00632 00633 00634 /** 00635 * @brief Server Socket Close 00636 * Close a Server socket 00637 * @param None 00638 * @retval WiFi_Status_t : return status of server socket request 00639 */ 00640 WiFi_Status_t wifi_socket_server_close() 00641 { 00642 WiFi_Status_t status = WiFi_MODULE_SUCCESS; 00643 00644 //Wait till all queued events are executed 00645 while(!event_empty(&event_buff)); 00646 Wait_For_Sock_Read_To_Complete(); 00647 00648 Reset_AT_CMD_Buffer(); 00649 00650 /* AT+S.SOCKD=portNo,t<cr> */ 00651 sprintf((char*)WiFi_AT_Cmd_Buff,AT_SERVER_SOCKET_CLOSE); 00652 status = USART_Transmit_AT_Cmd(strlen((char*)WiFi_AT_Cmd_Buff)); 00653 if(status == WiFi_MODULE_SUCCESS) 00654 { 00655 status = USART_Receive_AT_Resp(Process_Event); 00656 } 00657 00658 return status; 00659 } 00660 00661 00662 /** 00663 * @brief wait_for_command_mode 00664 * Waits till we are in command mode 00665 * @param None 00666 * @retval None 00667 */ 00668 void wait_for_command_mode(void) 00669 { 00670 while(!status_flag.command_mode) 00671 { 00672 //Make sure we are in command mode, ideally we should do this in every User API? 00673 asm("NOP"); 00674 } 00675 } 00676 00677 00678 /** 00679 * @brief wifi_file_delete 00680 * Delete a file 00681 * @param pFileName : File Name to be deleted 00682 * @retval WiFi_Status_t : return status of delete file request 00683 */ 00684 WiFi_Status_t wifi_file_delete(char * pFileName) 00685 { 00686 /* AT+S.FSD: delete an existing file */ 00687 WiFi_Status_t status = WiFi_MODULE_SUCCESS; 00688 Reset_AT_CMD_Buffer(); 00689 /* AT+S.FSL */ 00690 sprintf((char*)WiFi_AT_Cmd_Buff,AT_DELETE_FILE,pFileName); 00691 00692 status = USART_Transmit_AT_Cmd(strlen((char*)WiFi_AT_Cmd_Buff)); 00693 if(status == WiFi_MODULE_SUCCESS) 00694 { 00695 status = USART_Receive_AT_Resp(Process_Event); 00696 } 00697 return status; 00698 } 00699 00700 /** 00701 * @brief wifi_file_list 00702 * List existing filename 00703 * @param None 00704 * @retval WiFi_Status_t : return status of AT cmd request 00705 */ 00706 00707 WiFi_Status_t wifi_file_list() 00708 { 00709 WiFi_Status_t status = WiFi_MODULE_SUCCESS; 00710 Queue_Wifi_File_Event(NULL,NULL,0); 00711 status = USART_Receive_AT_Resp(Process_Event); 00712 return status; 00713 } 00714 00715 /** 00716 * @brief wifi_file_show 00717 * Print the contents of an existing file 00718 * @param pFileName : pinter of file name 00719 * @retval WiFi_Status_t : return status of AT cmd request 00720 */ 00721 00722 WiFi_Status_t wifi_file_show(uint8_t * pFileName) 00723 { 00724 if(pFileName==NULL) 00725 return WiFi_MODULE_ERROR; 00726 00727 WiFi_Status_t status = WiFi_MODULE_SUCCESS; 00728 Queue_Wifi_File_Event(NULL,pFileName,0); 00729 status = USART_Receive_AT_Resp(Process_Event); 00730 return status; 00731 } 00732 00733 /** 00734 * @brief wifi_file_create 00735 * Create file for HTTP server 00736 * @param pFileName : pointer of file name to be created 00737 * alength : length of file 00738 * @retval WiFi_Status_t : return status of AT cmd request 00739 */ 00740 00741 WiFi_Status_t wifi_file_create(char *pFileName, uint16_t alength, char * pUserFileBuff) 00742 { 00743 WiFi_Status_t status = WiFi_MODULE_SUCCESS; 00744 00745 if(alength >1024) 00746 return WiFi_AT_FILE_LENGTH_ERROR; 00747 00748 Reset_AT_CMD_Buffer(); 00749 00750 /* AT+S.FSC=/index.html */ 00751 sprintf((char*)WiFi_AT_Cmd_Buff, AT_CREATE_NEW_HTML_FILE, pFileName, alength); 00752 00753 status = USART_Transmit_AT_Cmd(strlen((char*)WiFi_AT_Cmd_Buff)); 00754 if(status == WiFi_MODULE_SUCCESS) 00755 { 00756 status = USART_Receive_AT_Resp(Process_Event); 00757 int len = strlen(pUserFileBuff); 00758 00759 if(len >= 1024) 00760 return WiFi_AT_FILE_LENGTH_ERROR; 00761 00762 /* AT+S.FSA=/index.html */ 00763 sprintf((char*)WiFi_AT_Cmd_Buff,AT_APPEND_FILE,pFileName,len); 00764 00765 status = USART_Transmit_AT_Cmd(strlen((char*)WiFi_AT_Cmd_Buff)); 00766 if(status == WiFi_MODULE_SUCCESS) 00767 { 00768 memset(WiFi_AT_Cmd_Buff, 0x00, sizeof WiFi_AT_Cmd_Buff); 00769 memcpy((char*)(char*)WiFi_AT_Cmd_Buff, (char*) pUserFileBuff,len); 00770 WiFi_AT_Cmd_Buff[len+1]='\r'; 00771 status = USART_Transmit_AT_Cmd(strlen((char*)WiFi_AT_Cmd_Buff)); 00772 if(status == WiFi_MODULE_SUCCESS) 00773 { 00774 status = USART_Receive_AT_Resp(Process_Event); 00775 } 00776 } 00777 } 00778 return status; 00779 } 00780 00781 /** 00782 * @brief wifi_http_get 00783 * Issue an HTTP GET of the given path to the specified host 00784 * @param None 00785 * @retval WiFi_Status_t : return status of AT cmd response 00786 */ 00787 00788 WiFi_Status_t wifi_http_get(uint8_t * hostname, uint8_t * path, uint32_t port_number) 00789 { 00790 if(hostname == NULL || path == NULL) 00791 return WiFi_MODULE_ERROR; 00792 00793 WiFi_Status_t status = WiFi_MODULE_SUCCESS; 00794 00795 while(status_flag.sock_read_ongoing || WIND64_count!= 0); //wait till any pending data is read 00796 { 00797 asm("NOP"); 00798 } 00799 00800 // AT+S.HTTPGET=host.example.com,/index.html, port_number<cr> 00801 //Queue the http-get command 00802 Queue_Http_Event(hostname, path, port_number,NULL); 00803 00804 //Make the user wait anyway 00805 status = USART_Receive_AT_Resp(Process_Event); 00806 00807 return status; 00808 00809 } 00810 00811 /** 00812 * @brief wifi_http_post 00813 * Issue an HTTP GET of the given path to the specified host 00814 * @param None 00815 * @retval WiFi_Status_t : status of Http Post Request 00816 */ 00817 00818 WiFi_Status_t wifi_http_post(uint8_t * pURL_path) 00819 { 00820 if(pURL_path == NULL) 00821 return WiFi_MODULE_ERROR; 00822 00823 WiFi_Status_t status = WiFi_MODULE_SUCCESS; 00824 00825 while(status_flag.sock_read_ongoing || WIND64_count!= 0);//wait till any pending data is read 00826 { 00827 asm("NOP"); 00828 } 00829 00830 // AT+S.HTTPPOST = posttestserver.com,/post.php,name=demo&email=mymail&subject=subj&body=message<cr> 00831 Queue_Http_Event(NULL,NULL,0,pURL_path); 00832 00833 //Make the user wait anyway 00834 status = USART_Receive_AT_Resp(Process_Event); 00835 return status; 00836 } 00837 00838 /** 00839 * @brief wifi_file_image_create 00840 * Downloads an updated file system via a single HTTP GET request to the 00841 * named host and path. 00842 * @param None 00843 * @retval WiFi_Status_t 00844 */ 00845 WiFi_Status_t wifi_file_image_create(uint8_t * pHostName, uint8_t * pFileName, uint32_t port_number) 00846 { 00847 if(pHostName == NULL || pFileName == NULL || port_number ==0) 00848 return WiFi_MODULE_ERROR; 00849 00850 WiFi_Status_t status = WiFi_MODULE_SUCCESS; 00851 Queue_Wifi_File_Event(pHostName,pFileName,port_number); 00852 status = USART_Receive_AT_Resp(Process_Event); 00853 00854 /* Soft reset the module */ 00855 SET_Power_State(PowerSave_State); 00856 return status; 00857 } 00858 00859 /** 00860 * @brief wifi_file_erase_external_flash 00861 * This API allows to erase the content of the external flash 00862 * @param None 00863 * @retval WiFi_Status_t 00864 */ 00865 WiFi_Status_t wifi_file_erase_external_flash() 00866 { 00867 WiFi_Status_t status = WiFi_MODULE_SUCCESS; 00868 00869 Reset_AT_CMD_Buffer(); 00870 ResetBuffer(); 00871 00872 /* AT+S.HTTPDFSERASE */ 00873 sprintf((char*)WiFi_AT_Cmd_Buff,AT_ERASE_FLASH_MEMORY); 00874 status = USART_Transmit_AT_Cmd(strlen((char*)WiFi_AT_Cmd_Buff)); 00875 00876 /* Soft reset the module */ 00877 SET_Power_State(PowerSave_State); 00878 00879 return status; 00880 } 00881 00882 /** 00883 * @brief wifi_fw_update 00884 * Issue an HTTP GET of the given path to the specified host and get the firmware updated 00885 * @param None 00886 * @retval None 00887 */ 00888 WiFi_Status_t wifi_fw_update(uint8_t * hostname, uint8_t * filename_path, uint32_t port_number) 00889 { 00890 WiFi_Status_t status = WiFi_MODULE_SUCCESS; 00891 Queue_Wifi_FW_Update_Event(hostname,filename_path,port_number); 00892 status = USART_Receive_AT_Resp(Process_Event); 00893 /* Soft reset the module */ 00894 SET_Power_State(PowerSave_State); 00895 return status; 00896 } 00897 00898 /** 00899 * @brief wifi_network_scan 00900 * Performs an immediate scan for available network 00901 * @param None 00902 * @retval WiFi_Status_t : WiFi status error 00903 */ 00904 WiFi_Status_t wifi_network_scan(wifi_scan *scan_result, uint16_t max_scan_number) 00905 { 00906 WiFi_Status_t status = WiFi_MODULE_SUCCESS; 00907 wifi_scanned_list = scan_result; 00908 if(max_scan_number>MAX_WIFI_SCAN_NETWORK) 00909 return WiFi_NOT_SUPPORTED; 00910 user_scan_number = max_scan_number; 00911 00912 if(status_flag.Scan_Ongoing) 00913 { 00914 return WiFi_AT_CMD_BUSY; 00915 } 00916 00917 status_flag.Scan_Ongoing = WIFI_TRUE; 00918 00919 #if 0 //TBD 00920 if(WiFi_Param.WiFi_Module_State == WiFi_MiniAP_MODE) 00921 return WiFi_NOT_SUPPORTED; 00922 #endif 00923 00924 /* AT+S.SCAN: performs an immediate scan for available networks */ 00925 Reset_AT_CMD_Buffer(); 00926 sprintf((char*)WiFi_AT_Cmd_Buff,AT_WiFi_SCAN); 00927 status = USART_Transmit_AT_Cmd(strlen((char*)WiFi_AT_Cmd_Buff)); 00928 if(status == WiFi_MODULE_SUCCESS) 00929 { 00930 status = USART_Receive_AT_Resp(Process_Event); 00931 } 00932 00933 /*At this point we have WiFi_Scan_Buffer filled with RSSI and SSID values*/ 00934 return status; 00935 } 00936 00937 /** 00938 * @brief Set_MiniAP_Mode 00939 * Configure Wi-Fi module in AP mode. 00940 MiniAP is always configured in open mode (WEP not supported) 00941 * @param None 00942 * @retval WiFi_Status_t : status of AT cmd 00943 */ 00944 WiFi_Status_t wifi_ap_start(uint8_t * ssid, uint8_t channel_num) 00945 { 00946 WiFi_Status_t status = WiFi_MODULE_SUCCESS; 00947 00948 /* Set the SSID : AT+S.SSIDTXT=<SSID>*/ 00949 //if(ssid) 00950 status = SET_SSID((char*)ssid); 00951 00952 00953 if(status != WiFi_MODULE_SUCCESS) 00954 return WiFi_SSID_ERROR; 00955 00956 /* Set the network privacy mode : AT+S.SCFG=wifi_priv_mode,0*/ 00957 status = SET_Configuration_Value(WIFI_PRIV_MODE, None); 00958 if(status != WiFi_MODULE_SUCCESS) 00959 return WiFi_CONFIG_ERROR; 00960 00961 /* Set the network mode (1 = STA, 2 = IBSS, 3 = MiniAP) :: AT+S.SCFG=wifi_mode,3*/ 00962 status = SET_Configuration_Value(WIFI_MODE, WiFi_MiniAP_MODE); 00963 if(status != WiFi_MODULE_SUCCESS) 00964 return WiFi_CONFIG_ERROR; 00965 00966 /* Set the channel number */ 00967 status = SET_Configuration_Value(WIFI_CHANNEL_NUMBER, channel_num); 00968 if(status != WiFi_MODULE_SUCCESS) 00969 return WiFi_CONFIG_ERROR; 00970 00971 /* Save the settings on the flash memory : AT&W*/ 00972 Save_Current_Setting(); 00973 00974 status_flag.WiFi_Configuration_Done = WIFI_TRUE; 00975 WiFi_Module_State = Process_Event; 00976 00977 /* Soft reset the module */ 00978 SET_Power_State(PowerSave_State); 00979 00980 return status; 00981 } 00982 00983 00984 /** 00985 * @brief SET_WiFi_STA_Mode 00986 * Configure Wi-Fi module in STA mode 00987 * @param SSID : SSID name 00988 * @param sec_key : security key 00989 * @param priv_mode : network privecy mode 00990 * @retval WiFi_Status_t : status of AT cmd 00991 */ 00992 WiFi_Status_t wifi_connect(char * ssid, char * sec_key, WiFi_Priv_Mode priv_mode) 00993 { 00994 WiFi_Status_t status = WiFi_MODULE_SUCCESS; 00995 if(status_flag.AT_Cmd_Ongoing == WIFI_FALSE) 00996 status_flag.AT_Cmd_Ongoing = WIFI_TRUE; 00997 else 00998 { 00999 return WiFi_AT_CMD_BUSY; 01000 } 01001 01002 if(sec_key) { 01003 status = SET_WiFi_SecKey((char*)sec_key); 01004 if(status != WiFi_MODULE_SUCCESS) 01005 return WiFi_SecKey_ERROR; 01006 } 01007 01008 /* Set the SSID : AT+S.SSIDTXT=<SSID>*/ 01009 01010 status = SET_SSID((char*)ssid); 01011 01012 if(status != WiFi_MODULE_SUCCESS) 01013 return WiFi_SSID_ERROR; 01014 01015 /* Set the network privacy mode : AT+S.SCFG=wifi_priv_mode,2*/ 01016 status = SET_Configuration_Value(WIFI_PRIV_MODE, priv_mode); 01017 if(status != WiFi_MODULE_SUCCESS) 01018 return WiFi_CONFIG_ERROR; 01019 01020 /* Set the network mode (1 = STA, 2 = IBSS, 3 = MiniAP) :: AT+S.SCFG=wifi_mode,1*/ 01021 status = SET_Configuration_Value(WIFI_MODE, WiFi_STA_MODE); 01022 if(status != WiFi_MODULE_SUCCESS) 01023 return WiFi_CONFIG_ERROR; 01024 01025 /* Save the settings on the flash memory : AT&W*/ 01026 Save_Current_Setting(); 01027 01028 status_flag.WiFi_Configuration_Done = WIFI_TRUE; 01029 //WiFi_Module_State = WiFi_Process_Event; 01030 01031 /* Soft reset the module */ 01032 SET_Power_State(PowerSave_State); 01033 01034 status_flag.AT_Cmd_Ongoing = WIFI_FALSE; 01035 01036 return status; 01037 } 01038 01039 /** 01040 * @brief SET_WiFi_IBSS_Mode 01041 * Configure Wi-Fi module in IBSS mode 01042 * @param SSID : SSID name 01043 * @param priv_mode : network privecy mode 01044 * @retval WiFi_Status_t : status of AT cmd 01045 */ 01046 WiFi_Status_t wifi_adhoc_create(uint8_t * ssid, WiFi_Priv_Mode priv_mode) 01047 { 01048 WiFi_Status_t status = WiFi_MODULE_SUCCESS; 01049 01050 Attention_Cmd(); 01051 /* Set the SSID : AT+S.SSIDTXT=<SSID>*/ 01052 status = SET_SSID((char*)ssid); 01053 if(status != WiFi_MODULE_SUCCESS) 01054 return WiFi_SSID_ERROR; 01055 01056 /* Set the network privacy mode : AT+S.SCFG=wifi_priv_mode,0*/ 01057 status = SET_Configuration_Value(WIFI_PRIV_MODE, priv_mode); 01058 if(status != WiFi_MODULE_SUCCESS) 01059 return WiFi_CONFIG_ERROR; 01060 01061 01062 /* Set the network mode (1 = STA, 2 = IBSS, 3 = MiniAP) :: AT+S.SCFG=wifi_mode,2*/ 01063 status = SET_Configuration_Value(WIFI_MODE, WiFi_IBSS_MODE); 01064 if(status != WiFi_MODULE_SUCCESS) 01065 return WiFi_CONFIG_ERROR; 01066 01067 /* Set IP address */ 01068 status = SET_Configuration_Addr(WIFI_IP_ADDRESS, WIFI_IBSS_IP_ADDR); 01069 if(status != WiFi_MODULE_SUCCESS) 01070 return WiFi_CONFIG_ERROR; 01071 01072 /* Set IP default gateway */ 01073 status = SET_Configuration_Addr(WIFI_IP_DEFAULT_GATEWAY, WIFI_IBSS_DEFAULT_GATEWAY); 01074 if(status != WiFi_MODULE_SUCCESS) 01075 return WiFi_CONFIG_ERROR; 01076 01077 01078 /* Set IP DNS */ 01079 status = SET_Configuration_Addr(WIFI_IP_DNS, WIFI_IBSS_IP_DNS_ADDR); 01080 if(status != WiFi_MODULE_SUCCESS) 01081 return WiFi_CONFIG_ERROR; 01082 01083 /* Set IP netmask */ 01084 status = SET_Configuration_Addr(WIFI_IP_NETMASK, WIFI_IBSS_IP_MASK); 01085 if(status != WiFi_MODULE_SUCCESS) 01086 return WiFi_CONFIG_ERROR; 01087 01088 /* Turn OFF the DHCP */ 01089 SET_Configuration_Value(IP_USE_DHCP_SERVER, WIFI_IP_USE_DHCP); 01090 if(status != WiFi_MODULE_SUCCESS) 01091 return WiFi_CONFIG_ERROR; 01092 01093 /* Save the settings on the flash memory : AT&W*/ 01094 Save_Current_Setting(); 01095 01096 /* Soft reset the module */ 01097 SET_Power_State(PowerSave_State); 01098 return status; 01099 } 01100 01101 01102 /** 01103 * @brief wifi_standby 01104 * Configured WiFi module to enter standby 01105 * @param arg_standby_time: standby time 01106 * @retval WiFi_Status_t : status of AT cmd 01107 */ 01108 WiFi_Status_t wifi_standby(uint8_t arg_standby_time) 01109 { 01110 /* 01111 For Standby, the presence of Jumpers on JP4 and JP3 has the following behaviour: 01112 JP3 (middle and bottom): prevents standby and immediately wakes-up module 01113 JP3 (middle and top): no effect on standby 01114 JP4 (middle and right): prevents wakeup and standby runs forever 01115 JP4 (middle and left): no effect on standby 01116 */ 01117 01118 WiFi_Status_t status = WiFi_MODULE_SUCCESS; 01119 01120 /*if(arg_standby_time<2) 01121 return WiFi_NOT_SUPPORTED;*/ 01122 01123 SET_Configuration_Value(WIFI_SLEEP_ENABLED, 0); 01124 SET_Configuration_Value(WIFI_STANDBY_ENABLED, 1); 01125 status = SET_Configuration_Value(WIFI_STANDBY_TIME, arg_standby_time); 01126 01127 /* save current setting in flash */ 01128 Save_Current_Setting(); 01129 01130 /* AT : send AT command */ 01131 sprintf((char*)WiFi_AT_Cmd_Buff,AT_SET_POWER_STATE,1); //cfun=4 01132 WiFi_Module_State = Process_Event; //make sure the WiFi module is in this state to receive WINDS after wakeup 01133 status = USART_Transmit_AT_Cmd(strlen((char*)WiFi_AT_Cmd_Buff)); 01134 01135 if(status == WiFi_MODULE_SUCCESS)//if transmit is success, prepare for resume 01136 { 01137 /* 01138 status_flag.AT_Cmd_Processing = WIFI_TRUE;//We do not want the next UART data_byte fetch to be done 01139 HAL_NVIC_DisableIRQ(USARTx_IRQn);//Disable UART IRQ 01140 status_flag.Standby_Timer_Running=WIFI_TRUE; 01141 printf("\r\nGoing into Standby Mode...\r\n");*/ 01142 } 01143 01144 return status; 01145 01146 } 01147 01148 /** 01149 * @brief wifi_wakeup 01150 * wakeup the module from sleep by setting the GPIO6 through PC13 01151 * or allow it to go to sleep 01152 * Jumper needed on JP4 01153 * @param wakeup wakeup (WIFI_TRUE) or allow sleep(WIFI_FALSE) 01154 * @retval WiFi_Status_t : status of function call 01155 */ 01156 WiFi_Status_t wifi_wakeup(wifi_bool wakeup) 01157 { 01158 WiFi_Status_t status = WiFi_MODULE_SUCCESS; 01159 01160 if(wakeup) 01161 callSpwfSADevice_wakeup(spwf_device_class, 0); 01162 else 01163 callSpwfSADevice_wakeup(spwf_device_class, 0); 01164 wait_ms(100); 01165 return status; 01166 } 01167 01168 01169 /** 01170 * @brief wifi_disconnect 01171 * disconnect the module from any AP 01172 * @param None 01173 * @retval WiFi_Status_t : status of AT cmd 01174 */ 01175 WiFi_Status_t wifi_disconnect(void) 01176 { 01177 WiFi_Status_t status = WiFi_MODULE_SUCCESS; 01178 01179 //Wait till all queued events are executed 01180 while(!event_empty(&event_buff)); 01181 Wait_For_Sock_Read_To_Complete(); 01182 01183 /* Set wifi_mode to idle*/ 01184 status = SET_Configuration_Value(WIFI_MODE, WiFi_IDLE_MODE); 01185 01186 /*If module was connected, reset the status*/ 01187 if(wifi_connected == 1) 01188 { 01189 wifi_connected = 0;//this will allow the TIM handler to make the callback on connection(WiFiUp) 01190 } 01191 01192 /* save current setting in flash */ 01193 Save_Current_Setting(); 01194 01195 /* Soft reset the module */ 01196 status = SET_Power_State(PowerSave_State);//CFUN=1 01197 01198 return status; 01199 } 01200 01201 /** 01202 * @brief wifi_enable 01203 * Enable/Disable the Wi-Fi interface 01204 * @param enable enable Wi-Fi (WIFI_TRUE) disable Wi-Fi (WIFI_FALSE) 01205 * @retval WiFi_Status_t : status of AT cmd 01206 */ 01207 WiFi_Status_t wifi_enable(wifi_bool enable) 01208 { 01209 WiFi_Status_t status = WiFi_MODULE_SUCCESS; 01210 01211 /* Enable or Disable wifi interface*/ 01212 Reset_AT_CMD_Buffer(); 01213 sprintf((char*)WiFi_AT_Cmd_Buff,AT_WIFI_ENABLE, enable); 01214 01215 /*if(HAL_UART_Transmit(&UartWiFiHandle, (uint8_t *)WiFi_AT_Cmd_Buff, strlen((char*)WiFi_AT_Cmd_Buff),1000)!= HAL_OK) 01216 { 01217 Error_Handler(); 01218 #if DEBUG_PRINT 01219 printf("HAL_UART_Transmit Error"); 01220 #endif 01221 return WiFi_HAL_UART_ERROR; 01222 } */ 01223 01224 status = USART_Receive_AT_Resp(Process_Event); 01225 01226 //wait for power down/hw started 01227 if(enable) 01228 while(status_flag.WiFi_Enabled != WIFI_TRUE) 01229 { 01230 asm("NOP"); 01231 } 01232 else 01233 while(status_flag.WiFi_Enabled != WIFI_FALSE) 01234 { 01235 asm("NOP"); 01236 } 01237 01238 return status; 01239 01240 } 01241 01242 01243 01244 /** 01245 * @brief wifi_restore 01246 * Restore the Wi-Fi with default values. 01247 * @param None 01248 * @retval WiFi_Status_t : status of AT cmd 01249 */ 01250 WiFi_Status_t wifi_restore() 01251 { 01252 WiFi_Status_t status = WiFi_MODULE_SUCCESS; 01253 01254 /* Restore default setting*/ 01255 Restore_Default_Setting(); 01256 01257 /* Set wifi_mode to idle*/ 01258 SET_Configuration_Value(WIFI_MODE, WiFi_IDLE_MODE); 01259 01260 /* set the local echo */ 01261 SET_Configuration_Value(LOCALECHO1, 0); 01262 01263 /* save current setting in flash */ 01264 Save_Current_Setting(); 01265 01266 /* Soft reset the module */ 01267 status = SET_Power_State(PowerSave_State);//CFUN=1 01268 01269 return status; 01270 } 01271 01272 /*GPIO Configuration Functions*/ 01273 01274 /** 01275 * @brief wifi_gpio_init 01276 * Configure a GPIO pin as in or out with IRQ setting 01277 * @param pin GPIO pin number 01278 * @param irq configuration of the pin 01279 * @retval WiFi_Status_t : status of AT cmd 01280 */ 01281 uint8_t wifi_gpio_init(GpioPin pin, char* dir, char irq) 01282 { 01283 WiFi_Status_t status = WiFi_MODULE_SUCCESS; 01284 01285 /* AT+S.GPIOC=pin,dir,irq */ 01286 Reset_AT_CMD_Buffer(); 01287 01288 memset(WiFi_AT_Cmd_Buff, 0x00, sizeof WiFi_AT_Cmd_Buff); 01289 01290 if(irq!=GPIO_Off) 01291 sprintf((char*)WiFi_AT_Cmd_Buff,"AT+S.GPIOC=%d,%s,%c\r", pin, dir, irq); 01292 else 01293 sprintf((char*)WiFi_AT_Cmd_Buff,"AT+S.GPIOC=%d,%s\r", pin, dir); 01294 01295 status = USART_Transmit_AT_Cmd(strlen((char*)WiFi_AT_Cmd_Buff)); 01296 if(status == WiFi_MODULE_SUCCESS) 01297 { 01298 status = USART_Receive_AT_Resp(Process_Event); 01299 } 01300 01301 01302 return status; 01303 01304 } 01305 01306 /** 01307 * @brief wifi_gpio_read 01308 * Read the configuration of a GPIO pin 01309 * @param pin GPIO pin number 01310 * @param val value returned 01311 * @param dir configuration direction returned 01312 * @retval WiFi_Status_t : status of AT cmd 01313 */ 01314 uint8_t wifi_gpio_read(GpioPin pin, uint8_t *val, uint8_t *dir) 01315 { 01316 WiFi_Status_t status = WiFi_MODULE_SUCCESS; 01317 01318 /* AT+S.GPIOR=pin */ 01319 Reset_AT_CMD_Buffer(); 01320 01321 sprintf((char*)WiFi_AT_Cmd_Buff,"AT+S.GPIOR=%d\r", pin); 01322 01323 status = USART_Transmit_AT_Cmd(strlen((char*)WiFi_AT_Cmd_Buff)); 01324 if(status == WiFi_MODULE_SUCCESS) 01325 { 01326 status = USART_Receive_AT_Resp(Process_Event); 01327 } 01328 *val = gpio_value; 01329 *dir = gpio_direc; 01330 01331 return status; 01332 } 01333 01334 /** 01335 * @brief wifi_gpio_write 01336 * Read the value of a GPIO pin 01337 * @param pin GPIO pin number 01338 * @param val value to be configured 01339 * @retval WiFi_Status_t : status of AT cmd 01340 */ 01341 uint8_t wifi_gpio_write(GpioPin pin, GpioWriteValue value) 01342 { 01343 WiFi_Status_t status = WiFi_MODULE_SUCCESS; 01344 01345 /* AT+S.GPIOW=pin,value */ 01346 Reset_AT_CMD_Buffer(); 01347 01348 sprintf((char*)WiFi_AT_Cmd_Buff,"AT+S.GPIOW=%d,%d\r", pin, value); 01349 01350 status = USART_Transmit_AT_Cmd(strlen((char*)WiFi_AT_Cmd_Buff)); 01351 if(status == WiFi_MODULE_SUCCESS) 01352 { 01353 status = USART_Receive_AT_Resp(Process_Event); 01354 } 01355 01356 return status; 01357 } 01358 01359 void UART_Configuration(uint32_t baud_rate) 01360 { 01361 } 01362 01363 #ifdef USART_PRINT_MSG 01364 void USART_PRINT_MSG_Configuration(uint32_t baud_rate) 01365 { 01366 } 01367 #endif 01368 01369 /** 01370 * @} 01371 */ 01372 01373 /** 01374 * @} 01375 */ 01376 01377 01378 /** 01379 * @} 01380 */ 01381 01382 /******************* (C) COPYRIGHT 2015 STMicroelectronics *****END OF FILE****/ 01383 01384
Generated on Tue Jul 12 2022 16:20:36 by
1.7.2

X-NUCLEO-IDW01M1 Wi-Fi expansion board