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_module.c
00001 /** 00002 ****************************************************************************** 00003 * @file wifi_module.c 00004 * @author Central LAB 00005 * @version V2.0.0 00006 * @date 10-February-2016 00007 * @brief Enable Wi-Fi functionality using AT cmd set 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 /* Includes ------------------------------------------------------------------*/ 00038 #include "wifi_module.h" 00039 //#include "stm32_spwf_wifi.h" 00040 #include "ring_buffer.h" 00041 #include "stdio.h" 00042 #include "string.h" 00043 #include "event_buffer.h" 00044 #include "device.h" 00045 #include "wait_api.h" 00046 #include <stddef.h> 00047 00048 00049 /** @addtogroup MIDDLEWARES 00050 * @{ 00051 */ 00052 00053 00054 /** @defgroup NUCLEO_WIFI_MODULE 00055 * @brief Wi-Fi driver modules 00056 * @{ 00057 */ 00058 00059 00060 /** @defgroup NUCLEO_WIFI_MODULE_Private_Defines 00061 * @{ 00062 */ 00063 00064 00065 /** 00066 * @} 00067 */ 00068 00069 00070 /** @addtogroup NUCLEO_WIFI_MODULE_Private_Variables 00071 * @{ 00072 */ 00073 /* Private variables ---------------------------------------------------------*/ 00074 00075 /*All Buffers*/ 00076 //uint8_t WiFi_AT_Cmd_Buff[1024]; 00077 uint8_t process_buffer[MAX_BUFFER_GLOBAL]; 00078 char UserDataBuff[MAX_BUFFER_GLOBAL]; 00079 uint8_t USART_RxBuffer[64];//This buffer is only used in the Init phase (to receive "\r\nOK\r\n") 00080 char print_msg_buff[MAX_BUFFER_GLOBAL]; 00081 volatile uint8_t ring_buffer[RINGBUF_SIZE];/* Default size for ring buffer */ 00082 00083 #ifdef USE_STM32L0XX_NUCLEO 00084 event_s_TypeDef event_buffer[10]; 00085 #else 00086 event_s_TypeDef event_buffer[50]; 00087 #endif 00088 00089 volatile uint8_t pop_buffer[MAX_BUFFER_GLOBAL]; 00090 volatile uint8_t prefetch_buffer[20]; 00091 uint8_t uart_byte[1]; 00092 buffer_td big_buff; 00093 buffer_e event_buff; 00094 event_s_TypeDef element; 00095 uint8_t HTTP_Runway_Buff[6];//Used to store the last 6 bytes in between User Callbacks during HTTP tx 00096 wifi_scan *wifi_scanned_list; //[MAX_WIFI_SCAN_NETWORK]; 00097 00098 volatile Wifi_Status_Var status_flag; 00099 00100 uint32_t wind64_DQ_wait = 0; 00101 uint32_t UserDataBuff_index; 00102 volatile uint32_t tickcount; 00103 uint8_t SocketId; 00104 uint32_t SockON_Data_Len; 00105 uint32_t Socket_Data_Length =0; 00106 uint8_t Socket_Open_ID, sockon_query_id,sockon_id_user; 00107 uint32_t SockON_Data_Length; 00108 uint8_t enable_pending_data =0; 00109 00110 volatile WiFi_WIND_State_TypeDef WiFi_WIND_State;//describes the current WIND number in processing 00111 00112 uint8_t WiFi_Resp_OK = 0; 00113 uint32_t number_of_bytes=0; 00114 uint32_t interim_number_of_bytes=0; 00115 uint32_t Interim_SockON_Data_Len=0; 00116 uint32_t bytes_to_be_read = 0; 00117 uint32_t sock_total_count=0; 00118 uint32_t sockD_total_count=0; 00119 uint32_t ip_fragment_count=0; 00120 uint32_t chunk_size; 00121 int write_size; 00122 uint32_t message_size; 00123 uint32_t WIND55_count=0; 00124 volatile uint32_t WIND64_count=0; 00125 uint8_t user_scan_number; 00126 uint32_t pop_queue_length; 00127 uint32_t pop_buffer_size=0; 00128 uint32_t process_buffer_index = 5; 00129 uint32_t epoch_time = 0; 00130 00131 volatile WiFi_AT_CMD_Response_t WiFi_Module_State; 00132 00133 uint8_t *WiFi_Scan_Buffer; 00134 uint8_t * curr_hostname; 00135 uint8_t * curr_path; 00136 uint8_t * curr_pURL; 00137 uint8_t * curr_protocol; 00138 uint32_t curr_port_number; 00139 char * curr_data; 00140 uint8_t curr_sockID; 00141 uint8_t * curr_filename; 00142 uint16_t curr_DataLength; 00143 uint16_t sock_server_write_datalength; 00144 char *sock_server_write_pdata; 00145 00146 uint8_t socket_closed_id_callback; 00147 uint8_t remote_socket_closed_id; 00148 uint8_t client_socket_close_id; 00149 volatile uint8_t wifi_ready = WIFI_FALSE;//Set once if wifi is ready for first time 00150 volatile uint8_t wifi_connected = WIFI_FALSE;//Set once if wifi is connected for first time 00151 volatile uint8_t wifi_client_connected = 0;//Set once if client is connected 00152 volatile uint8_t wifi_client_disconnected = 0;//Set once if client is dis-connected 00153 uint8_t gpio_value, gpio_direc, get_cfg_value[64]; 00154 WiFi_Status_t user_error_code = WiFi_MODULE_SUCCESS; 00155 00156 uint8_t no_of_open_client_sockets = 0; 00157 wifi_bool open_sockets[8]; //Max open sockets allowed is 8. Each array element depicts one socket (true=open, false=closed) 00158 uint8_t client_MAC_address[17];//current client MAC address store 00159 00160 uint8_t enable_uart_byte_data_receive=1; 00161 uint8_t uart_data_receive_ready=1; 00162 00163 WiFi_Status_t AT_RESPONSE = WiFi_AT_CMD_RESP_ERROR; 00164 uint32_t sleep_count = 0; 00165 uint32_t standby_time = 0; 00166 uint8_t scanned_ssids = 0; 00167 char * prefetch_str; 00168 00169 uint8_t WiFi_AT_Cmd_Buff[2052]; 00170 00171 #ifdef USART_PRINT_MSG 00172 #define printf(arg) {sprintf((char*)print_msg_buff,arg); \ 00173 HAL_UART_Transmit(&UartMsgHandle, (uint8_t*)print_msg_buff, strlen(print_msg_buff), 1000);} 00174 #endif 00175 00176 void * spwf_dev_; 00177 00178 extern void callSpwfSADevice_debug(void* object, const char * string); 00179 extern void callSpwfSADevice_attach(wifi_bool attach); 00180 00181 #if defined (__CC_ARM) 00182 size_t strnlen (const char* s, size_t maxlen); 00183 00184 size_t strnlen (const char* s, size_t maxlen) 00185 { 00186 size_t len = 0; 00187 00188 while ((len <= maxlen) && (*s)) 00189 { 00190 s++; 00191 len++; 00192 } 00193 00194 return len; 00195 } 00196 #endif 00197 00198 event_s_TypeDef event_s; 00199 event_s_TypeDef * event_pop_s; 00200 00201 // [DLI] 00202 #ifdef WIFI_USE_VCOM 00203 00204 uint8_t console_listen_char[1]; 00205 uint8_t console_input_char[1]; 00206 uint8_t console_send_char[1]; 00207 uint8_t console_echo_char[1]; 00208 uint8_t console_send_ready = 0; 00209 uint8_t console_echo_ready = 1; 00210 uint8_t console_push_ready = 0; 00211 00212 // Virtual-COM UART 00213 void console_input() { 00214 //HAL_UART_Receive_IT(&UartMsgHandle, (uint8_t *)console_input_char, 1); 00215 } 00216 00217 void wifi_vcom() { 00218 00219 uint8_t * temp; 00220 00221 if (console_push_ready == 1) { 00222 push_buffer(&big_buff, uart_byte); 00223 console_push_ready = 0; 00224 //HAL_UART_Receive_IT(&UartWiFiHandle, (uint8_t *)uart_byte, 1); 00225 } 00226 if(console_echo_ready == 0) { 00227 temp = pop_buffer_queue(&big_buff); 00228 if(temp != NULL) { 00229 console_echo_ready = 1; 00230 //HAL_UART_Transmit_IT(&UartMsgHandle, temp, 1); 00231 } 00232 } 00233 } 00234 00235 #endif 00236 00237 /** 00238 * @} 00239 */ 00240 00241 /** @defgroup NUCLEO_WIFI_MODULE_Private_Functions 00242 * @{ 00243 */ 00244 00245 /** 00246 *Changed/introduced functions for MBED implementation: 00247 00248 - void Wifi_TIM_Handler(void); - new fn 00249 - void Rx_irq_handler(void); - new fn - in place of HAL_UART_RxCpltCallback 00250 - Receive_Data(); - modified fn 00251 - void wifi_reset(void) - modified fn 00252 - void PowerUp_WiFi_Module(void) - modified fn 00253 - void RX_EXTI_Isr(uint16_t GPIO_Pin) - removed fn 00254 - void HAL_UART_TxCpltCallback(UART_HandleTypeDef *UartHandleArg) - removed fn 00255 - void HAL_UART_RxCpltCallback(UART_HandleTypeDef *UartHandleArg) - removed fn 00256 - void HAL_UART_ErrorCallback(UART_HandleTypeDef *UartHandle) - removed fn 00257 - WiFi_Status_t USART_Transmit_AT_Cmd(uint16_t size)- modified fn 00258 */ 00259 00260 00261 00262 /** 00263 * @brief WiFi_Module_Init 00264 * Initialize wifi module 00265 * @param None 00266 * @retval None 00267 */ 00268 void WiFi_Module_Init(void) 00269 { 00270 #ifdef WIFI_USE_VCOM 00271 console_input(); 00272 #else 00273 WiFi_Module_State = Process_Event; 00274 #endif 00275 00276 initialize_status_flags(); 00277 init(&big_buff, RINGBUF_SIZE);//Init the ring buffer 00278 wifi_ready = 0; //reset to get user callback on HW started 00279 wifi_connected = 0; //reset to get user callback on WiFi UP 00280 Receive_Data(); 00281 00282 #ifdef USE_STM32L0XX_NUCLEO 00283 event_init(&event_buff, 10); //max 15 events can be Q'ed (Event Buffer is of size 15) 00284 #else 00285 event_init(&event_buff, 50); //max 50 events can be Q'ed (Event Buffer is of size 50) 00286 #endif 00287 00288 #ifndef WIFI_USE_VCOM 00289 Start_Timer(); 00290 memset(open_sockets,0x00, 8); //init the open socket array 00291 00292 #endif 00293 } 00294 00295 /** 00296 * @brief initialize_status_flags 00297 * Default Wifi status values 00298 * @param None 00299 * @retval None 00300 */ 00301 void initialize_status_flags() 00302 { 00303 status_flag.Single_Digit_Indication = WIFI_FALSE; 00304 status_flag.WiFi_Enabled = WIFI_FALSE; 00305 status_flag.http_req_pending = WIFI_FALSE; 00306 status_flag.WiFi_Configuration_Done = WIFI_FALSE; 00307 status_flag.Timer_Running = WIFI_FALSE; 00308 status_flag.resume_receive_data = WIFI_FALSE; 00309 status_flag.enable_dequeue = WIFI_TRUE; 00310 status_flag.stop_event_dequeue = WIFI_FALSE; 00311 status_flag.Standby_Timer_Running = WIFI_FALSE; 00312 status_flag.trigger_wakeup_callback = WIFI_FALSE; 00313 status_flag.Deep_Sleep_Enabled = WIFI_FALSE; 00314 status_flag.Standby_Enabled = WIFI_FALSE; 00315 status_flag.Low_Power_Enabled = WIFI_FALSE; 00316 status_flag.command_mode=WIFI_TRUE; 00317 status_flag.data_mode=WIFI_FALSE; 00318 status_flag.Scan_Ongoing = WIFI_FALSE; 00319 status_flag.AT_Cmd_Ongoing = WIFI_FALSE; 00320 status_flag.AT_Cmd_Processing = WIFI_FALSE; 00321 status_flag.Uartx_Rx_Processing = WIFI_FALSE; 00322 status_flag.Client_Connected = WIFI_FALSE; 00323 status_flag.Client_Disconnected = WIFI_FALSE; 00324 status_flag.switch_by_default_to_command_mode = WIFI_TRUE; 00325 status_flag.start_sock_read = WIFI_FALSE; 00326 status_flag.enable_receive_data_chunk = WIFI_FALSE; 00327 status_flag.data_pending_sockD=WIFI_FALSE; 00328 status_flag.enable_sock_read = WIFI_FALSE; 00329 status_flag.enable_sock_data = WIFI_FALSE; 00330 status_flag.enable_query = WIFI_FALSE; 00331 status_flag.Set_AT_Cmd_Response_False = WIFI_FALSE; 00332 status_flag.enable_fw_update_read = WIFI_FALSE; 00333 status_flag.Q_Contains_Message = WIFI_FALSE; 00334 status_flag.Q_Contains_Data = WIFI_FALSE; 00335 status_flag.enable_receive_http_response = WIFI_FALSE; 00336 status_flag.enable_receive_file_response = WIFI_FALSE; 00337 status_flag.enable_receive_wifi_scan_response = WIFI_FALSE; 00338 status_flag.prevent_push_OK_event = WIFI_FALSE; 00339 status_flag.client_socket_close_ongoing = WIFI_FALSE; 00340 status_flag.prevent_push_WIFI_event = WIFI_FALSE; 00341 status_flag.sock_read_ongoing = WIFI_FALSE; 00342 status_flag.enable_client_socket_write = WIFI_FALSE; 00343 status_flag.event_deQ_x_wind64 = WIFI_FALSE; 00344 status_flag.do_not_reset_push_WIFI_event = WIFI_FALSE; 00345 status_flag.message_pending = WIFI_FALSE; 00346 status_flag.Pending_SockON_Callback = WIFI_FALSE; 00347 status_flag.Pending_SockD_Callback = WIFI_FALSE; 00348 status_flag.SockON_Server_Closed_Callback = WIFI_FALSE; 00349 status_flag.Client_Socket_Close_Cmd = WIFI_FALSE; 00350 status_flag.standby_resume_callback = WIFI_FALSE; 00351 status_flag.HTTP_Data_available = WIFI_FALSE; 00352 status_flag.FILE_Data_available = WIFI_FALSE; 00353 status_flag.AT_Response_Received = WIFI_FALSE; 00354 status_flag.Deep_Sleep_Timer = WIFI_FALSE; 00355 status_flag.Deep_Sleep_Callback = WIFI_FALSE; 00356 } 00357 00358 00359 /** 00360 * @brief Period elapsed callback in non blocking mode 00361 * This timer is used for calling back User registered functions with information 00362 * @param htim : TIM handle 00363 * @retval None 00364 */ 00365 00366 void Wifi_TIM_Handler(void) 00367 { 00368 /********************************************************************** 00369 * * 00370 * Be careful not to make a blocking * 00371 * call from this function, see * 00372 * example Socket_Read() and Socket_Close() * 00373 * * 00374 **********************************************************************/ 00375 WiFi_Status_t status = WiFi_MODULE_SUCCESS; 00376 00377 if(status_flag.stop_event_dequeue == WIFI_FALSE /*&& !status_flag.enable_receive_data_chunk*/) 00378 { 00379 __disable_irq(); 00380 event_pop_s = pop_eventbuffer_queue(&event_buff); 00381 __enable_irq(); 00382 00383 if(event_pop_s!=NULL && event_pop_s->event_pop == WIFI_TRUE) 00384 { 00385 switch(event_pop_s->event) 00386 { 00387 case WIFI_WIND_EVENT: 00388 Process_WiFi_Indication_Cmd(event_pop_s); 00389 break; 00390 00391 case WIFI_OK_EVENT: 00392 status_flag.AT_Response_Received = WIFI_TRUE; 00393 AT_RESPONSE = WiFi_MODULE_SUCCESS; 00394 if(status_flag.Set_AT_Cmd_Response_False == WIFI_TRUE) 00395 { 00396 status_flag.Set_AT_Cmd_Response_False = WIFI_FALSE; 00397 status_flag.AT_Response_Received = WIFI_FALSE; 00398 } 00399 if(status_flag.enable_client_socket_write) //allows the OK received after socket client write to be processed 00400 { 00401 status_flag.AT_Response_Received = WIFI_TRUE; 00402 status_flag.enable_client_socket_write = WIFI_FALSE; 00403 } 00404 break; 00405 00406 case WIFI_SOCK_ID_EVENT: 00407 /*check ID and update SocketID array*/ 00408 no_of_open_client_sockets++; 00409 00410 if(no_of_open_client_sockets > 8) //Max number of clients is 8 00411 { 00412 status_flag.AT_Response_Received = WIFI_TRUE; 00413 AT_RESPONSE = WiFi_NOT_SUPPORTED; 00414 break; 00415 } 00416 00417 open_sockets[event_pop_s->socket_id] = WIFI_TRUE; 00418 Socket_Open_ID = event_pop_s->socket_id; 00419 status_flag.AT_Response_Received = WIFI_TRUE; 00420 AT_RESPONSE = WiFi_MODULE_SUCCESS; 00421 break; 00422 00423 case WIFI_HTTP_EVENT: 00424 Reset_AT_CMD_Buffer(); 00425 memset(UserDataBuff, 0x00, MAX_BUFFER_GLOBAL);//Flush the buffer 00426 00427 if(curr_pURL) { 00428 sprintf((char*)WiFi_AT_Cmd_Buff,AT_HTTPPOST_REQUEST,curr_pURL); 00429 } 00430 else { 00431 if(curr_port_number!=0) 00432 sprintf((char*)WiFi_AT_Cmd_Buff,"AT+S.HTTPGET=%s,%s,%d\r",curr_hostname, curr_path, (int)curr_port_number); 00433 else 00434 sprintf((char*)WiFi_AT_Cmd_Buff,"AT+S.HTTPGET=%s,%s\r",curr_hostname, curr_path); 00435 } 00436 00437 status = USART_Transmit_AT_Cmd(strlen((char*)WiFi_AT_Cmd_Buff)); 00438 if(status == WiFi_MODULE_SUCCESS) { 00439 WiFi_Module_State = Process_Event; 00440 status_flag.stop_event_dequeue = WIFI_TRUE; 00441 status_flag.http_req_pending = WIFI_TRUE; 00442 } 00443 else { 00444 #if DEBUG_PRINT 00445 printf("\r\n ERROR IN HTTP \r\n"); 00446 #endif 00447 AT_RESPONSE = WiFi_AT_CMD_RESP_ERROR; 00448 status_flag.AT_Response_Received = WIFI_TRUE; 00449 } 00450 break; 00451 00452 case WIFI_CLIENT_SOCKET_WRITE_EVENT: 00453 Reset_AT_CMD_Buffer(); 00454 /* AT+S.SOCKW=00,11<cr> */ 00455 sprintf((char*)WiFi_AT_Cmd_Buff,AT_SOCKET_WRITE,curr_sockID,curr_DataLength); 00456 status = USART_Transmit_AT_Cmd(strlen((char*)WiFi_AT_Cmd_Buff)); 00457 00458 if(status == WiFi_MODULE_SUCCESS) { 00459 Reset_AT_CMD_Buffer(); 00460 memcpy((char*)(char*)WiFi_AT_Cmd_Buff, (char*)curr_data,curr_DataLength); 00461 WiFi_AT_Cmd_Buff[curr_DataLength]='\r'; 00462 00463 status = USART_Transmit_AT_Cmd(curr_DataLength+2); 00464 if(status == WiFi_MODULE_SUCCESS) { 00465 WiFi_Module_State = Process_Event; 00466 } 00467 else { 00468 #if DEBUG_PRINT 00469 printf("\r\n ERROR IN SOCKET\r\n"); 00470 #endif 00471 status_flag.AT_Response_Received = WIFI_TRUE; 00472 AT_RESPONSE = WiFi_AT_CMD_RESP_ERROR; 00473 } 00474 } 00475 else { 00476 #if DEBUG_PRINT 00477 printf("\r\n ERROR In Socket\r\n"); 00478 #endif 00479 status_flag.AT_Response_Received = WIFI_TRUE; 00480 AT_RESPONSE = WiFi_AT_CMD_RESP_ERROR; 00481 } 00482 break; 00483 00484 case WIFI_CLIENT_SOCKET_OPEN_EVENT: 00485 00486 //if(!open_sockets[event_pop_s->socket_id])//make sure socket is closed 00487 //{ 00488 Reset_AT_CMD_Buffer(); 00489 00490 /* AT+S.SOCKON = myserver,1234,t <cr> */ 00491 sprintf((char*)WiFi_AT_Cmd_Buff,AT_SOCKET_OPEN,curr_hostname,(int)curr_port_number,curr_protocol); 00492 status = USART_Transmit_AT_Cmd(strlen((char*)WiFi_AT_Cmd_Buff)); 00493 if(status != WiFi_MODULE_SUCCESS) 00494 { 00495 #if DEBUG_PRINT 00496 printf("\r\n ERROR During Socket Open \r\n"); 00497 #endif 00498 status_flag.AT_Response_Received = WIFI_TRUE; 00499 AT_RESPONSE = WiFi_AT_CMD_RESP_ERROR; 00500 } 00501 //} 00502 break; 00503 00504 case WIFI_CLIENT_SOCKET_CLOSE_EVENT: 00505 00506 if(open_sockets[event_pop_s->socket_id])//make sure socket is open 00507 { 00508 Reset_AT_CMD_Buffer(); 00509 00510 /* AT+S.SOCKC=00<cr> */ 00511 sprintf((char*)WiFi_AT_Cmd_Buff,AT_SOCKET_CLOSE,event_pop_s->socket_id); 00512 status = USART_Transmit_AT_Cmd(strlen((char*)WiFi_AT_Cmd_Buff)); 00513 if(status == WiFi_MODULE_SUCCESS) 00514 { 00515 AT_RESPONSE = WiFi_MODULE_SUCCESS; 00516 status_flag.prevent_push_OK_event = WIFI_TRUE; //prevent the OK received after socket close command to be Q'ed 00517 status_flag.stop_event_dequeue = WIFI_TRUE; 00518 remote_socket_closed_id = event_pop_s->socket_id; 00519 status_flag.client_socket_close_ongoing = WIFI_TRUE; //used for making changes in the value of open_sockets[sock_id] if no error is returned 00520 // status_flag.SockON_Server_Closed_Callback = WIFI_TRUE; 00521 } 00522 else 00523 { 00524 #if DEBUG_PRINT 00525 printf("\r\n ERROR During Socket Close \r\n"); 00526 #endif 00527 } 00528 } 00529 break; 00530 00531 00532 case WIFI_FILE_EVENT: 00533 Reset_AT_CMD_Buffer(); 00534 00535 if(curr_filename == NULL) 00536 { 00537 /* AT+S.FSL */ 00538 sprintf((char*)WiFi_AT_Cmd_Buff,AT_DISPLAY_FILE_NAME); 00539 } 00540 else if(curr_hostname == NULL) 00541 { 00542 /* AT+S.FSP=/index.html */ 00543 sprintf((char*)WiFi_AT_Cmd_Buff,AT_DISPLAY_FILE_CONTENT,curr_filename); 00544 } 00545 else 00546 { 00547 /* AT+S.HTTPDFSUPDATE=%s,/outfile.img */ 00548 sprintf((char*)WiFi_AT_Cmd_Buff,AT_DOWNLOAD_IMAGE_FILE,curr_hostname,curr_filename,(int)curr_port_number); 00549 } 00550 status = USART_Transmit_AT_Cmd(strlen((char*)WiFi_AT_Cmd_Buff)); 00551 if(status == WiFi_MODULE_SUCCESS) 00552 { 00553 WiFi_Module_State = Process_Event; 00554 status_flag.enable_receive_http_response = WIFI_TRUE; 00555 status_flag.enable_receive_data_chunk = WIFI_TRUE; 00556 status_flag.enable_receive_file_response = WIFI_TRUE; 00557 } 00558 else { 00559 #if DEBUG_PRINT 00560 printf("\r\n ERROR in FILE \r\n"); 00561 #endif 00562 status_flag.AT_Response_Received = WIFI_TRUE; 00563 AT_RESPONSE = WiFi_AT_CMD_RESP_ERROR; 00564 } 00565 break; 00566 00567 case WIFI_FW_UPDATE_EVENT: 00568 Reset_AT_CMD_Buffer(); 00569 sprintf((char*)WiFi_AT_Cmd_Buff,"AT+S.FWUPDATE=%s,/%s,%d\r",curr_hostname,curr_filename,(int)curr_port_number); 00570 status = USART_Transmit_AT_Cmd(strlen((char*)WiFi_AT_Cmd_Buff)); 00571 if(status == WiFi_MODULE_SUCCESS) 00572 { 00573 WiFi_Module_State = Process_Event; 00574 status_flag.enable_fw_update_read = WIFI_TRUE; 00575 status_flag.enable_receive_data_chunk = WIFI_TRUE; 00576 } 00577 else 00578 { 00579 #if DEBUG_PRINT 00580 printf("\r\n ERROR in Firmware \r\n"); 00581 #endif 00582 status_flag.AT_Response_Received = WIFI_TRUE; 00583 AT_RESPONSE = WiFi_AT_CMD_RESP_ERROR; 00584 } 00585 break; 00586 00587 case WIFI_ERROR_EVENT: 00588 #if DEBUG_PRINT 00589 printf("\r\n ERROR!\r\n"); 00590 #endif 00591 status_flag.AT_Response_Received = WIFI_TRUE; 00592 AT_RESPONSE = WiFi_AT_CMD_RESP_ERROR; 00593 break; 00594 00595 case WIFI_GCFG_EVENT: 00596 case WIFI_GPIO_EVENT: 00597 status_flag.AT_Response_Received = WIFI_TRUE; 00598 AT_RESPONSE = WiFi_MODULE_SUCCESS; 00599 break; 00600 00601 case WIFI_STANDBY_CONFIG_EVENT: 00602 #if DEBUG_PRINT 00603 printf("\r\nGoing into standby..\r\n"); 00604 #endif 00605 WiFi_Module_State = Process_Event; 00606 break; 00607 00608 case WIFI_RESUME_CONFIG_EVENT: 00609 #if DEBUG_PRINT 00610 printf("\r\nResuming from standby..\r\n"); 00611 #endif 00612 status_flag.AT_Response_Received = WIFI_TRUE;//let main run-on 00613 break; 00614 00615 case WIFI_NO_EVENT: 00616 break; 00617 } 00618 } 00619 } 00620 /* If data is pending on client socket SOCKON, make read requests*/ 00621 if(status_flag.start_sock_read == WIFI_TRUE) 00622 { 00623 //flush_buffer_queue(&big_buff); 00624 Socket_Read(Socket_Data_Length); 00625 status_flag.start_sock_read = WIFI_FALSE; 00626 } 00627 00628 /* Call Query, after notification for TLS is received */ 00629 else if(status_flag.enable_query == WIFI_TRUE && status_flag.enable_dequeue == WIFI_TRUE) 00630 { 00631 //@TBD: Flushing the buffer may be detrimental if we have genuine follow on WIND55? 00632 //flush_buffer_queue(&big_buff); //Flush the buffer to remove WIND:55 in pipeline (This maybe a problem) 00633 Socket_Pending_Data(); 00634 status_flag.enable_query = WIFI_FALSE; 00635 } 00636 00637 else if(status_flag.Pending_SockON_Callback==WIFI_TRUE)//for client socket 00638 { 00639 //Now callback to user with user_data pointer <UserDataBuff> 00640 ind_wifi_socket_data_received(sockon_id_user, (uint8_t *)UserDataBuff, message_size, chunk_size); 00641 memset(UserDataBuff, 0x00, MAX_BUFFER_GLOBAL);//Flush the buffer 00642 Resume_Dequeue(); 00643 status_flag.Pending_SockON_Callback=WIFI_FALSE; 00644 } 00645 00646 else if(status_flag.Pending_SockD_Callback == WIFI_TRUE)//for server socket 00647 { 00648 //if(status_flag.command_mode)//if command_mode is achieved then callback else loop in this state 00649 { 00650 //Now callback to user with user_data pointer <UserDataBuff> 00651 ind_wifi_socket_data_received(9, (uint8_t *)UserDataBuff, message_size, chunk_size); 00652 memset(UserDataBuff, 0x00, MAX_BUFFER_GLOBAL);//Flush the buffer 00653 Resume_Dequeue(); 00654 status_flag.Pending_SockD_Callback=WIFI_FALSE; 00655 } 00656 } 00657 00658 else if(status_flag.Client_Socket_Close_Cmd==WIFI_TRUE)//for client socket 00659 { 00660 //Close the socket 00661 //Change State to AT_Cmd_Response before calling socket_close() 00662 WiFi_Module_State = Process_Event; 00663 wifi_socket_client_close(client_socket_close_id); 00664 status_flag.Client_Socket_Close_Cmd = WIFI_FALSE; 00665 // status_flag.SockON_Server_Closed_Callback = WIFI_TRUE; 00666 } 00667 00668 else if(status_flag.SockON_Server_Closed_Callback==WIFI_TRUE)//for client socket 00669 { 00670 //callback the user 00671 ind_wifi_socket_client_remote_server_closed(&socket_closed_id_callback); 00672 status_flag.SockON_Server_Closed_Callback = WIFI_FALSE; 00673 } 00674 00675 else if(status_flag.HTTP_Data_available == WIFI_TRUE) 00676 { 00677 ind_wifi_http_data_available((uint8_t *)UserDataBuff,UserDataBuff_index); 00678 memset(UserDataBuff, 0x00, MAX_BUFFER_GLOBAL);//Flush the buffer 00679 // memcpy(&UserDataBuff, &HTTP_Runway_Buff, 6); 00680 // memset(HTTP_Runway_Buff, 0x00, 6); 00681 Resume_Dequeue(); 00682 status_flag.HTTP_Data_available=WIFI_FALSE; 00683 } 00684 00685 else if (status_flag.FILE_Data_available == WIFI_TRUE) 00686 { 00687 ind_wifi_file_data_available((uint8_t *) UserDataBuff); 00688 memset(UserDataBuff, 0x00, MAX_BUFFER_GLOBAL);//Flush the buffer 00689 Resume_Dequeue(); 00690 status_flag.FILE_Data_available = WIFI_FALSE; 00691 } 00692 else if(status_flag.Client_Connected == WIFI_TRUE) 00693 { 00694 ind_socket_server_client_joined(); 00695 status_flag.Client_Connected = WIFI_FALSE; 00696 } 00697 00698 else if(status_flag.Client_Disconnected == WIFI_TRUE) 00699 { 00700 ind_socket_server_client_left(); 00701 status_flag.Client_Disconnected = WIFI_FALSE; 00702 } 00703 00704 //Make callbacks from here to user for pending events 00705 00706 if(WiFi_WIND_State.WiFiHWStarted==WIFI_TRUE) 00707 { 00708 if(wifi_ready == 2)//Twice reset for User Callback 00709 { 00710 wifi_ready++; 00711 ind_wifi_on();//Call this once only...This if for wifi_on (instead of console active 00712 } 00713 } 00714 00715 if(WiFi_WIND_State.WiFiUp == WIFI_TRUE) 00716 { 00717 if(wifi_connected == 0) 00718 { 00719 wifi_connected = 1; 00720 ind_wifi_connected();//wifi connected 00721 } 00722 WiFi_WIND_State.WiFiUp = WIFI_FALSE; 00723 } 00724 00725 else if(WiFi_WIND_State.WiFiStarted_MiniAPMode == WIFI_TRUE) 00726 { 00727 ind_wifi_ap_ready(); 00728 WiFi_WIND_State.WiFiStarted_MiniAPMode = WIFI_FALSE; 00729 } 00730 00731 else if(WiFi_WIND_State.WiFiAPClientJoined == WIFI_TRUE) 00732 { 00733 ind_wifi_ap_client_joined(client_MAC_address); 00734 WiFi_WIND_State.WiFiAPClientJoined = WIFI_FALSE; 00735 } 00736 00737 else if(WiFi_WIND_State.WiFiAPClientLeft == WIFI_TRUE) 00738 { 00739 ind_wifi_ap_client_left(client_MAC_address); 00740 WiFi_WIND_State.WiFiAPClientLeft = WIFI_FALSE; 00741 } 00742 00743 else if(status_flag.Deep_Sleep_Callback == WIFI_TRUE) 00744 { 00745 ind_wifi_resuming(); 00746 status_flag.Deep_Sleep_Callback = WIFI_FALSE; 00747 } 00748 00749 else if(status_flag.standby_resume_callback == WIFI_TRUE) 00750 { 00751 ind_wifi_resuming(); 00752 status_flag.standby_resume_callback = WIFI_FALSE; 00753 } 00754 00755 else if(WiFi_WIND_State.WiFiHWFailure==WIFI_TRUE) 00756 { 00757 WiFi_WIND_State.WiFiHWFailure=WIFI_FALSE; 00758 ind_wifi_error(WiFi_HW_FAILURE_ERROR);//call with error number 00759 } 00760 00761 else if(WiFi_WIND_State.HardFault==WIFI_TRUE) 00762 { 00763 WiFi_WIND_State.HardFault=WIFI_FALSE; 00764 ind_wifi_error(WiFi_HARD_FAULT_ERROR);//call with error number 00765 } 00766 00767 else if(WiFi_WIND_State.StackOverflow==WIFI_TRUE) 00768 { 00769 WiFi_WIND_State.StackOverflow=WIFI_FALSE; 00770 ind_wifi_error(WiFi_STACK_OVERFLOW_ERROR);//call with error number 00771 } 00772 00773 else if(WiFi_WIND_State.MallocFailed==WIFI_TRUE) 00774 { 00775 WiFi_WIND_State.MallocFailed=WIFI_FALSE; 00776 ind_wifi_error(WiFi_MALLOC_FAILED_ERROR);//call with error number 00777 } 00778 00779 else if(WiFi_WIND_State.InitFailure==WIFI_TRUE) 00780 { 00781 WiFi_WIND_State.InitFailure=WIFI_FALSE; 00782 ind_wifi_error(WiFi_INIT_ERROR);//call with error number 00783 } 00784 00785 else if(WiFi_WIND_State.StartFailed==WIFI_TRUE) 00786 { 00787 WiFi_WIND_State.StartFailed=WIFI_FALSE; 00788 ind_wifi_error(WiFi_START_FAILED_ERROR);//call with error number 00789 } 00790 00791 else if(WiFi_WIND_State.WiFiException==WIFI_TRUE) 00792 { 00793 WiFi_WIND_State.WiFiException=WIFI_FALSE; 00794 ind_wifi_error(WiFi_EXCEPTION_ERROR);//call with error number 00795 } 00796 00797 else if(WiFi_WIND_State.PS_Mode_Failure==WIFI_TRUE) 00798 { 00799 WiFi_WIND_State.PS_Mode_Failure=WIFI_FALSE; 00800 ind_wifi_warning(WiFi_POWER_SAVE_WARNING);//call with error number 00801 } 00802 00803 else if(WiFi_WIND_State.HeapTooSmall==WIFI_TRUE) 00804 { 00805 WiFi_WIND_State.HeapTooSmall=WIFI_FALSE; 00806 ind_wifi_warning(WiFi_HEAP_TOO_SMALL_WARNING);//call with error number 00807 } 00808 00809 else if(WiFi_WIND_State.WiFiSignalLOW==WIFI_TRUE) 00810 { 00811 WiFi_WIND_State.WiFiSignalLOW=WIFI_FALSE; 00812 ind_wifi_warning(WiFi_SIGNAL_LOW_WARNING);//call with error number 00813 } 00814 00815 else if(WiFi_WIND_State.WiFiDeauthentication == WIFI_TRUE) 00816 { 00817 WiFi_WIND_State.WiFiDeauthentication = WIFI_FALSE; 00818 ind_wifi_connection_error(WiFi_DE_AUTH); 00819 } 00820 00821 else if(WiFi_WIND_State.WiFiDisAssociation == WIFI_TRUE) 00822 { 00823 WiFi_WIND_State.WiFiDisAssociation = WIFI_FALSE; 00824 ind_wifi_connection_error(WiFi_DISASSOCIATION); 00825 } 00826 00827 else if(WiFi_WIND_State.WiFiJoinFailed == WIFI_TRUE) 00828 { 00829 WiFi_WIND_State.WiFiJoinFailed = WIFI_FALSE; 00830 ind_wifi_connection_error(WiFi_JOIN_FAILED); 00831 } 00832 00833 else if(WiFi_WIND_State.WiFiScanBlewUp == WIFI_TRUE) 00834 { 00835 WiFi_WIND_State.WiFiScanBlewUp = WIFI_FALSE; 00836 ind_wifi_connection_error(WiFi_SCAN_BLEWUP); //@TBD to check if user made call, so not call callback if true 00837 } 00838 00839 else if(WiFi_WIND_State.WiFiScanFailed == WIFI_TRUE) 00840 { 00841 WiFi_WIND_State.WiFiScanFailed = WIFI_FALSE; 00842 ind_wifi_connection_error(WiFi_SCAN_FAILED); //@TBD to check if user made call, so not call callback if true 00843 } 00844 00845 else if(WiFi_WIND_State.WiFiUnHandledInd == WIFI_TRUE) 00846 { 00847 WiFi_WIND_State.WiFiUnHandledInd = WIFI_FALSE; 00848 ind_wifi_packet_lost(WiFi_UNHANDLED_IND_ERROR); //@TBD to check if user made call, so not call callback if true 00849 } 00850 00851 else if(WiFi_WIND_State.WiFiRXMgmt == WIFI_TRUE) 00852 { 00853 WiFi_WIND_State.WiFiRXMgmt = WIFI_FALSE; 00854 ind_wifi_packet_lost(WiFi_RX_MGMT); //@TBD to check if user made call, so not call callback if true 00855 } 00856 00857 else if(WiFi_WIND_State.WiFiRXData == WIFI_TRUE) 00858 { 00859 WiFi_WIND_State.WiFiRXData = WIFI_FALSE; 00860 ind_wifi_packet_lost(WiFi_RX_DATA); //@TBD to check if user made call, so not call callback if true 00861 } 00862 00863 else if(WiFi_WIND_State.WiFiRxUnk == WIFI_TRUE) 00864 { 00865 WiFi_WIND_State.WiFiRxUnk = WIFI_FALSE; 00866 ind_wifi_packet_lost(WiFi_RX_UNK); //@TBD to check if user made call, so not call callback if true 00867 } 00868 00869 else if(WiFi_WIND_State.WiFiSockdDataLost == WIFI_TRUE) 00870 { 00871 WiFi_WIND_State.WiFiSockdDataLost = WIFI_FALSE; 00872 ind_wifi_socket_server_data_lost(); //@TBD to check if user made call, so not call callback if true 00873 } 00874 00875 } 00876 00877 00878 /** 00879 * @brief Start_Timer 00880 * Start Timer 00881 * @param None 00882 * @retval None 00883 */ 00884 void Start_Timer() 00885 { 00886 //tickcount = WIFI_FALSE; 00887 status_flag.Timer_Running = WIFI_TRUE; 00888 } 00889 00890 /** 00891 * @brief Stop_Timer 00892 * Stop Timer request 00893 * @param None 00894 * @retval None 00895 */ 00896 void Stop_Timer() 00897 { 00898 //tickcount = WIFI_FALSE; 00899 status_flag.Timer_Running = WIFI_FALSE; 00900 //UartReady = SET; 00901 } 00902 00903 /** 00904 * @brief Stop_Dequeue 00905 * Stop dequeuing data from the ring buffer 00906 * @param None 00907 * @retval None 00908 */ 00909 void Stop_Dequeue() 00910 { 00911 status_flag.enable_dequeue = WIFI_FALSE; 00912 } 00913 00914 /** 00915 * @brief Resume_Dequeue 00916 * Resume dequeuing data from the ring buffer 00917 * @param None 00918 * @retval None 00919 */ 00920 void Resume_Dequeue() 00921 { 00922 status_flag.enable_dequeue = WIFI_TRUE; 00923 } 00924 00925 /** 00926 * @brief Wifi_SysTick_Isr 00927 * Function called every SysTick to process buffer 00928 * @param None 00929 * @retval None 00930 */ 00931 void Wifi_SysTick_Isr() 00932 { 00933 //Check if Data is Paused 00934 if((status_flag.Timer_Running) && (status_flag.enable_dequeue==WIFI_TRUE)) 00935 { 00936 Process_WiFi(); 00937 } 00938 00939 if(status_flag.Timeout_Timer) tickcount++; 00940 00941 /*if(status_flag.resume_receive_data == WIFI_TRUE) 00942 { 00943 if(is_half_empty(&big_buff)) 00944 { 00945 status_flag.resume_receive_data = WIFI_FALSE; 00946 //callSpwfSADevice_attach(1); 00947 Receive_Data(); 00948 00949 } 00950 }*/ 00951 00952 00953 if(status_flag.Standby_Timer_Running) // module is in sleep and after expiry RX will be conf as EXTI 00954 { 00955 if((standby_time++) >= EXTI_CONF_TIMER) 00956 { 00957 status_flag.Standby_Timer_Running=WIFI_FALSE; 00958 standby_time = 0; 00959 //configure_to_exti(); 00960 } 00961 } 00962 00963 /*A Resume WIND:70 has come and triggered this 00964 So checking here if after that resume we fall back to sleep (another WIND69) within SLEEP_RESUME_PREVENT time. 00965 If yes, we assume it is a false resume and hence do nothing and go back to sleep 00966 If no WIND69 (going into sleep) has come, we can assume the resume was genuine and then enable the callback 00967 */ 00968 if((status_flag.Deep_Sleep_Timer) && ( sleep_count++) >= SLEEP_RESUME_PREVENT) 00969 { 00970 if(status_flag.Deep_Sleep_Enabled == WIFI_TRUE)//which means we have received another WIND69 in the 2 seconds 00971 { 00972 //do nothing, go back to sleep 00973 status_flag.Deep_Sleep_Enabled = WIFI_TRUE; 00974 status_flag.Deep_Sleep_Callback = WIFI_FALSE; 00975 } 00976 else if (status_flag.Deep_Sleep_Enabled == WIFI_FALSE) //which means we have not received any WIND69 during the last 2 seconds 00977 { 00978 //enable the user callback as it is a genuine WIND70 00979 status_flag.Deep_Sleep_Callback = WIFI_TRUE; 00980 } 00981 Stop_DeepSleep_Timer(); 00982 } 00983 } 00984 00985 00986 /** 00987 * @brief USART_Receive_AT_Resp 00988 * Receive and check AT cmd response 00989 * @param WiFi_AT_CMD_Response_t : WIFi module next state 00990 * @retval WiFi_Status_t : Response of AT cmd 00991 */ 00992 00993 WiFi_Status_t USART_Receive_AT_Resp(WiFi_AT_CMD_Response_t state) 00994 { 00995 WiFi_Module_State = state; 00996 tickcount=0;//reset timer 00997 status_flag.Timeout_Timer = WIFI_TRUE; 00998 while(status_flag.AT_Response_Received != WIFI_TRUE) { 00999 if(tickcount >=20000)//20 sec wait 01000 { 01001 AT_RESPONSE = WiFi_AT_CMD_RESP_ERROR; 01002 break; 01003 } 01004 __NOP(); 01005 } 01006 status_flag.Timeout_Timer = WIFI_FALSE; 01007 status_flag.AT_Response_Received = WIFI_FALSE; 01008 return AT_RESPONSE; 01009 } 01010 01011 01012 /** 01013 * @brief Process_WiFi 01014 * Pop a byte from the circular buffer and send the byte for processing 01015 * This function should be called from main or should be run with a periodic timer 01016 * @param None 01017 * @retval None 01018 */ 01019 void Process_WiFi(void) 01020 { 01021 uint8_t * temp;//pop buffer temporary 01022 01023 __disable_irq(); 01024 temp=pop_buffer_queue(&big_buff); //contents of temp(pop_buffer) will not change till another de-queue is made 01025 __enable_irq(); 01026 01027 if(temp!=NULL) 01028 { 01029 Process_Buffer(temp); 01030 } 01031 01032 if(status_flag.event_deQ_x_wind64)//if de-Q is stopped due to WIND64 wait 01033 { 01034 wind64_DQ_wait++;//1ms for each count 01035 if(wind64_DQ_wait>200)//wait for 50ms for example 01036 { 01037 wind64_DQ_wait=0; 01038 status_flag.event_deQ_x_wind64 = WIFI_FALSE; 01039 //re-enable event Q after 200ms 01040 status_flag.stop_event_dequeue = WIFI_FALSE; 01041 } 01042 } 01043 } 01044 01045 /** 01046 * @brief Process_Buffer 01047 * Process and construct a Wind Line buffer 01048 * @param ptr: pointer to one single byte 01049 * @retval None 01050 */ 01051 01052 void Process_Buffer(uint8_t * ptr) 01053 { 01054 static uint32_t Fillptr=0;//count=0; 01055 static uint8_t index, chan_value; 01056 unsigned char rxdata = 0; 01057 int rssi_value = 0; 01058 char SocketId_No[2]; 01059 char databytes_No[4]; 01060 char * pStr; 01061 01062 rxdata = *(ptr+0); 01063 //printf(&rxdata);//check prints for debug...to be removed or kept in DEBUG statement 01064 if(status_flag.enable_receive_data_chunk == WIFI_FALSE) 01065 process_buffer[Fillptr++] = rxdata; 01066 reset_event(&event_s); 01067 switch (WiFi_Module_State) 01068 { 01069 case Process_Event: 01070 01071 if((process_buffer[Fillptr-2]==0xD) && (process_buffer[Fillptr-1]==0xA) && !status_flag.enable_receive_http_response && !status_flag.sock_read_ongoing) 01072 { 01073 if((strstr((const char *)process_buffer,"WIND:")) != NULL) 01074 { 01075 if((strstr((const char *)process_buffer,"+WIND:67:")) !=NULL) 01076 event_s.event = WIFI_STANDBY_CONFIG_EVENT; 01077 01078 else if ((strstr((const char *)process_buffer,"+WIND:68:"))!= NULL) 01079 event_s.event = WIFI_RESUME_CONFIG_EVENT; 01080 01081 else 01082 { 01083 //end of msg received. Will not receive any other msg till we process this. 01084 //Stop_Timer(); 01085 //#if defined (USE_STM32L0XX_NUCLEO) || (USE_STM32F4XX_NUCLEO) || (USE_STM32L4XX_NUCLEO) 01086 //__disable_irq(); 01087 //#endif 01088 Process_Wind_Indication(&process_buffer[0]); 01089 //#if defined (USE_STM32L0XX_NUCLEO) || (USE_STM32F4XX_NUCLEO) || (USE_STM32L4XX_NUCLEO) 01090 //__enable_irq(); 01091 //#endif 01092 //Start_Timer(); 01093 sock_total_count=0; 01094 } 01095 01096 if(!status_flag.prevent_push_WIFI_event) 01097 { 01098 __disable_irq(); 01099 push_eventbuffer(&event_buff, event_s); 01100 __enable_irq(); 01101 reset_event(&event_s); 01102 } 01103 01104 if (!status_flag.do_not_reset_push_WIFI_event) status_flag.prevent_push_WIFI_event = WIFI_FALSE; 01105 if(status_flag.enable_sock_read) 01106 { 01107 status_flag.sock_read_ongoing = WIFI_FALSE;//we finished processing a message and now sock read will commence 01108 status_flag.Q_Contains_Message = WIFI_FALSE; 01109 } 01110 Fillptr=0; 01111 memset(process_buffer, 0x00, strlen((const char*)process_buffer)); 01112 } 01113 01114 else if((strstr((const char *)process_buffer,"\r\nOK\r\n")) != NULL) 01115 { 01116 /*Now Check to which AT Cmd response this OK belongs to so that correct parsing can be done*/ 01117 01118 // SOCKON ID (Open a client socket) 01119 if(((pStr=(strstr((const char *)process_buffer,"ID: "))) != NULL)) 01120 { 01121 SocketId_No[0] = *(pStr + 4) ; 01122 SocketId_No[1] = *(pStr + 5) ; 01123 SocketId = (((SocketId_No[0] - '0') * 10 ) + (SocketId_No[1] - '0')); 01124 event_s.socket_id = SocketId; 01125 event_s.event = WIFI_SOCK_ID_EVENT; 01126 __disable_irq(); 01127 push_eventbuffer(&event_buff, event_s); 01128 __enable_irq(); 01129 reset_event(&event_s); 01130 if(status_flag.enable_sock_read) 01131 { 01132 status_flag.sock_read_ongoing = WIFI_FALSE; //we finished processing a message and now sock read will commence 01133 status_flag.Q_Contains_Message = WIFI_FALSE; 01134 } 01135 Fillptr=0; 01136 sock_total_count=0; 01137 memset(process_buffer, 0x00, MAX_BUFFER_GLOBAL); 01138 } 01139 01140 // DATALEN from SOCKQ 01141 else if((pStr=(strstr((const char *)process_buffer,"DATALEN: "))) != NULL) 01142 { 01143 //Find the DataLength and do a socket read 01144 databytes_No[0] = *(pStr + 9); 01145 databytes_No[1] = *(pStr + 10); 01146 databytes_No[2] = *(pStr + 11); 01147 databytes_No[3] = *(pStr + 12); 01148 01149 if( databytes_No[1] == '\r') 01150 { 01151 SockON_Data_Len = databytes_No[0] - '0'; 01152 } 01153 else if( databytes_No[2] == '\r') 01154 { 01155 SockON_Data_Len = (((databytes_No[0] - '0') * 10 ) + (databytes_No[1] - '0')); 01156 } 01157 else if( databytes_No[3] == '\r') 01158 { 01159 SockON_Data_Len = (((databytes_No[0] - '0') * 100 ) + ((databytes_No[1] - '0') * 10 ) + (databytes_No[2] - '0')); 01160 } 01161 else //it's a 4-digit number 01162 { 01163 SockON_Data_Len = ((databytes_No[0] - '0') * 1000 ) + ((databytes_No[1] - '0') * 100 ) + ((databytes_No[2] - '0') * 10) + (databytes_No[3] - '0'); 01164 } 01165 Socket_Data_Length = SockON_Data_Len; 01166 if(Socket_Data_Length != 0) 01167 { 01168 status_flag.start_sock_read = WIFI_TRUE; 01169 } 01170 else if(Socket_Data_Length == 0) 01171 { 01172 status_flag.stop_event_dequeue = WIFI_FALSE; //continue popping events if nothing to read 01173 status_flag.Set_AT_Cmd_Response_False = WIFI_FALSE; 01174 } 01175 Fillptr = 0; 01176 sock_total_count = 0; 01177 if(status_flag.enable_sock_read) 01178 { 01179 status_flag.sock_read_ongoing = WIFI_FALSE; //we finished processing a message and now sock read will commence 01180 status_flag.Q_Contains_Message = WIFI_FALSE; 01181 pop_buffer_size = 0; 01182 pop_buffer[0]='\0'; // required as this byte is already used in process_buffer 01183 } 01184 memset(process_buffer, 0x00, MAX_BUFFER_GLOBAL); 01185 } 01186 01187 else if((((strstr((const char *)process_buffer," = "))) != NULL)) 01188 { 01189 // AT command GCFG 01190 pStr = (char *) strstr((const char *)process_buffer," = "); 01191 event_s.event = WIFI_GCFG_EVENT; 01192 memcpy(get_cfg_value, pStr+2, (strlen(pStr)-2)); 01193 __disable_irq(); 01194 push_eventbuffer(&event_buff, event_s); 01195 __enable_irq(); 01196 reset_event(&event_s); 01197 if(status_flag.enable_sock_read) 01198 { 01199 status_flag.sock_read_ongoing = WIFI_FALSE; //we finished processing a message and now sock read will commence 01200 status_flag.Q_Contains_Message = WIFI_FALSE; 01201 } 01202 Fillptr=0; 01203 memset(process_buffer, 0x00, MAX_BUFFER_GLOBAL); 01204 } 01205 01206 else 01207 { 01208 //This is a standalone OK 01209 /*Cases possible 01210 - TLSCERT,TLSCERT2, TLSDOMAIN, SETTIME 01211 - S.SOCKW, SOCKR, S.SOCKC, S.SOCKD (open a server socket) 01212 - File Operations 01213 - S.GPIOC and S.GPIOW 01214 */ 01215 //Push a simple OK Event, if this is an OK event required to be pushed to Q 01216 if(status_flag.prevent_push_OK_event) 01217 { 01218 //This OK is not to be handled, hence the pop action on OK completion to be done here 01219 //since pop will not happen 01220 // stop_event_dequeue=WIFI_FALSE; 01221 01222 if(status_flag.client_socket_close_ongoing) 01223 { 01224 if(no_of_open_client_sockets > 0) 01225 no_of_open_client_sockets--; 01226 status_flag.prevent_push_OK_event = WIFI_FALSE; 01227 open_sockets[remote_socket_closed_id] = WIFI_FALSE; 01228 socket_closed_id_callback = remote_socket_closed_id; 01229 status_flag.Set_AT_Cmd_Response_False = WIFI_FALSE; 01230 status_flag.client_socket_close_ongoing = WIFI_FALSE; 01231 status_flag.SockON_Server_Closed_Callback = WIFI_TRUE; 01232 status_flag.stop_event_dequeue=WIFI_FALSE; 01233 } 01234 01235 } 01236 01237 else 01238 { 01239 event_s.ok_eval = WIFI_TRUE; 01240 event_s.event = WIFI_OK_EVENT; 01241 __disable_irq(); 01242 push_eventbuffer(&event_buff, event_s); 01243 __enable_irq(); 01244 reset_event(&event_s); 01245 } 01246 status_flag.prevent_push_OK_event = WIFI_FALSE; 01247 memset(process_buffer, 0x00, Fillptr); 01248 Fillptr=0; 01249 sock_total_count=0; 01250 if(status_flag.enable_sock_read) 01251 { 01252 status_flag.sock_read_ongoing = WIFI_FALSE;//we finished processing a message and now sock read will commence 01253 status_flag.Q_Contains_Message = WIFI_FALSE; 01254 } 01255 break; 01256 } 01257 } 01258 01259 else if((((strstr((const char *)process_buffer,"ERROR"))) != NULL)) 01260 { 01261 //This is an ERROR 01262 //There can be only ONE outstanding AT command and hence this ERROR belongs to that 01263 //HTTP -> ERROR: host not found 01264 //@TBD: Check all Errors Possible here??? 01265 if((strstr((const char *)process_buffer,"\r\nERROR: Pending data\r\n")) != NULL) //if Error after sock close command and not 'OK' 01266 { 01267 printf("\r\nERROR: Socket could not be closed..PENDING DATA\r\n"); 01268 status_flag.prevent_push_OK_event = WIFI_FALSE; //prevent the OK received after socket close command to be Q'ed 01269 status_flag.client_socket_close_ongoing = WIFI_FALSE; 01270 status_flag.stop_event_dequeue = WIFI_FALSE; 01271 } 01272 else 01273 { 01274 event_s.event = WIFI_ERROR_EVENT; 01275 __disable_irq(); 01276 push_eventbuffer(&event_buff, event_s); 01277 __enable_irq(); 01278 reset_event(&event_s); 01279 if(status_flag.stop_event_dequeue) 01280 /*ERROR:Illegal Socket ID*/ 01281 status_flag.stop_event_dequeue= WIFI_FALSE;//continue popping events if nothing to read 01282 } 01283 Fillptr=0; 01284 sock_total_count=0; 01285 if(status_flag.enable_sock_read) 01286 { 01287 status_flag.sock_read_ongoing = WIFI_FALSE;//we finished processing a message and now sock read will commence 01288 status_flag.Q_Contains_Message = WIFI_FALSE; 01289 } 01290 memset(process_buffer, 0x00, MAX_BUFFER_GLOBAL); 01291 } 01292 01293 else if((((strstr((const char *)process_buffer,"GPIO "))) != NULL)) 01294 { 01295 // Receive GPIO Read 01296 pStr = (char *) strstr((const char *)process_buffer,"= 0,"); 01297 if(pStr != NULL) 01298 { 01299 gpio_value = 0; 01300 } 01301 else 01302 gpio_value = 1; 01303 01304 pStr = (char *) strstr((const char *)process_buffer,"out"); 01305 if(pStr != NULL) 01306 { 01307 gpio_direc= 0; //out 01308 } 01309 else 01310 { 01311 gpio_direc= 1; //in 01312 } 01313 //Push GPIO Read Event on Event_Queue 01314 event_s.event = WIFI_GPIO_EVENT; 01315 __disable_irq(); 01316 push_eventbuffer(&event_buff, event_s); 01317 __enable_irq(); 01318 reset_event(&event_s); 01319 if(status_flag.enable_sock_read) 01320 { 01321 status_flag.sock_read_ongoing = WIFI_FALSE; //we finished processing a message and now sock read will commence 01322 status_flag.Q_Contains_Message = WIFI_FALSE; 01323 } 01324 Fillptr = 0; 01325 memset(process_buffer, 0x00, strlen((const char*)process_buffer)); 01326 break; 01327 } 01328 01329 else 01330 { 01331 if(status_flag.enable_sock_read && status_flag.Q_Contains_Message && !status_flag.message_pending) 01332 { 01333 /* data is D Q'ed in pop_buffer and we return from here(process_buffer()) without processing this data. Next time we enter in this procedure the data will be overe written in pop_buffer 01334 To avoid this, rewinding is necessary. */ 01335 if(pop_buffer_size) 01336 { 01337 __disable_irq(); 01338 rewind_buffer_queue(&big_buff,pop_buffer_size); //in this the case of rewinding past the buffer->end can happen 01339 __enable_irq(); 01340 memset(ptr, 0x00, pop_buffer_size); 01341 } 01342 //used for bypassing enable_receive_data_chunk part for the first time \r\n..\r\n is received in case of socket data 01343 status_flag.enable_sock_data = WIFI_TRUE; 01344 01345 status_flag.Q_Contains_Message = WIFI_FALSE; 01346 pop_queue_length = Fillptr; 01347 } 01348 01349 //if in data mode, reset on \r\n 01350 if(!status_flag.sock_read_ongoing && status_flag.data_mode) 01351 { 01352 Fillptr = 0; 01353 memset(process_buffer, 0x00, strlen((const char*)process_buffer)); 01354 } 01355 } 01356 } 01357 01358 else if (status_flag.http_req_pending) //HTTP Response Check 01359 { 01360 if((strstr((const char *)&process_buffer[0],"200 OK\r")) != NULL || (strstr((const char *)&process_buffer[0],"400 Bad Request\r")) != NULL 01361 || (strstr((const char *)&process_buffer[0],"401 Unauthorized\r")) != NULL || (strstr((const char *)&process_buffer[0],"403 Forbidden\r")) != NULL 01362 || (strstr((const char *)&process_buffer[0],"404 Not Found\r")) != NULL || (strstr((const char *)&process_buffer[0],"408 Request Timeout\r")) != NULL 01363 || (strstr((const char *)&process_buffer[0],"500 Internal Server Error\r")) != NULL || (strstr((const char *)&process_buffer[0],"502 Bad Gateway\r")) != NULL 01364 || (strstr((const char *)&process_buffer[0],"504 Gateway Timeout\r")) != NULL) 01365 { 01366 status_flag.enable_receive_http_response = WIFI_TRUE; 01367 status_flag.enable_receive_data_chunk = WIFI_TRUE; 01368 status_flag.http_req_pending = WIFI_FALSE; 01369 pop_buffer[0]='\0'; 01370 pop_buffer_size=0; 01371 process_buffer_index =6; 01372 } 01373 } 01374 01375 else if ((process_buffer[Fillptr-1]==0x09) && (process_buffer[Fillptr-2]==':') && (process_buffer[Fillptr-3]=='1'))//<ht> Horizontal Tab for Scan Result? 01376 { 01377 status_flag.enable_receive_wifi_scan_response = WIFI_TRUE; 01378 } 01379 01380 //if(!status_flag.Pending_SockON_Callback && !status_flag.HTTP_Data_available && !status_flag.FILE_Data_available) 01381 { 01382 //Check Process Buffer for any pending message 01383 if(status_flag.enable_receive_data_chunk && !status_flag.enable_sock_data) 01384 { 01385 if(pop_buffer_size) 01386 { 01387 pop_queue_length = pop_buffer_size; 01388 if(Fillptr + pop_queue_length > 511) 01389 { 01390 uint32_t length = (Fillptr + pop_queue_length) - 511; 01391 __disable_irq(); 01392 rewind_buffer_queue(&big_buff,length); 01393 __enable_irq(); 01394 memset(ptr+(511 - Fillptr), 0x00, length); 01395 pop_queue_length = 511 - Fillptr; 01396 } 01397 memcpy(process_buffer+Fillptr,(char const *)pop_buffer, pop_queue_length); 01398 Fillptr = Fillptr + pop_queue_length; 01399 01400 if(Fillptr > 512) 01401 { 01402 printf("\r\nFillptr corrupted!!\r\n"); 01403 } 01404 01405 if((strstr((const char *)process_buffer,"ERROR: ")) != NULL) 01406 { 01407 status_flag.Q_Contains_Message = WIFI_FALSE; 01408 status_flag.message_pending = WIFI_FALSE; 01409 } 01410 else if(!status_flag.sock_read_ongoing && !status_flag.enable_receive_http_response) 01411 { 01412 if(process_buffer[0]!='\0') 01413 { 01414 if(((process_buffer[0]==0xD) && (process_buffer[1]==0xA)) && process_buffer[2] != 0xD && SockON_Data_Length != 2) 01415 { 01416 status_flag.message_pending = WIFI_TRUE; 01417 if((pStr = (strstr((const char *)process_buffer+2,"\r\n"))) != NULL) 01418 { // process buffer has complete message 01419 int wind_length = ((uint8_t *)pStr - (uint8_t *)process_buffer)+2; 01420 01421 if(strstr((const char *)process_buffer+2,"DATALEN:")) 01422 { 01423 pStr = strstr((const char *)process_buffer + wind_length,"\r\nOK\r\n"); //find OK, as DATALEN has to be terminated by OK 01424 if(pStr!=NULL) 01425 { 01426 wind_length = ((uint8_t *)pStr-(uint8_t *)process_buffer)+6; 01427 } 01428 } 01429 01430 if(Fillptr-wind_length) 01431 { 01432 __disable_irq(); 01433 rewind_buffer_queue(&big_buff, Fillptr - wind_length); 01434 __enable_irq(); 01435 memset(process_buffer + wind_length,0x00,Fillptr - wind_length); 01436 Fillptr = wind_length; 01437 } 01438 status_flag.message_pending = WIFI_FALSE; 01439 } 01440 status_flag.Q_Contains_Message = WIFI_TRUE; 01441 } 01442 } 01443 } 01444 } 01445 } 01446 01447 if(!status_flag.Q_Contains_Message && status_flag.enable_sock_read && pop_buffer_size) /*read is enabled*/ 01448 { 01449 status_flag.sock_read_ongoing = WIFI_TRUE; 01450 sock_total_count = sock_total_count + pop_queue_length; 01451 01452 if(status_flag.enable_sock_data) 01453 status_flag.enable_sock_data = WIFI_FALSE; 01454 01455 /* Check for "ERROR: Not enough data in buffer " */ 01456 pStr = (char *) strstr((const char *)&process_buffer,"ERROR: "); 01457 01458 if (pStr != NULL) 01459 { 01460 if((process_buffer[Fillptr-2]==0xD) && (process_buffer[Fillptr-1]==0xA)) 01461 { 01462 if((pStr = (char *)strstr((const char *)&process_buffer,"\r\nERROR: Too many sockets\r\n")) !=NULL) 01463 { 01464 #if DEBUG_PRINT 01465 printf("\r\nERROR: TOO MANY SOCKETS \r\n"); 01466 #endif 01467 01468 if(*(pStr+27)!='\0') 01469 { 01470 int len = (uint8_t *)pStr - (uint8_t *)process_buffer; 01471 int extra_bytes = Fillptr - (len+27); 01472 __disable_irq(); 01473 rewind_buffer_queue(&big_buff, extra_bytes); 01474 __enable_irq(); 01475 } 01476 Fillptr=0; 01477 sock_total_count =0; 01478 memset(process_buffer, 0x00, MAX_BUFFER_GLOBAL); 01479 status_flag.AT_Response_Received = WIFI_TRUE; 01480 AT_RESPONSE = WiFi_AT_CMD_RESP_ERROR; 01481 break; 01482 } 01483 else if((pStr = (char *)strstr((const char *)&process_buffer,"\r\nERROR: Pending data\r\n")) !=NULL) 01484 { 01485 #if DEBUG_PRINT 01486 printf("\r\nERROR: PENDING DATA \r\n"); 01487 #endif 01488 01489 if(*(pStr+23)!='\0') 01490 { 01491 int len = (uint8_t *)pStr - (uint8_t *)process_buffer; 01492 int extra_bytes = Fillptr - (len+23); 01493 __disable_irq(); 01494 rewind_buffer_queue(&big_buff, extra_bytes); 01495 __enable_irq(); 01496 } 01497 Fillptr=0; 01498 sock_total_count =0; 01499 memset(process_buffer, 0x00, MAX_BUFFER_GLOBAL); 01500 status_flag.AT_Response_Received = WIFI_TRUE; 01501 AT_RESPONSE = WiFi_AT_CMD_RESP_ERROR; 01502 break; 01503 } 01504 01505 printf("\rERROR DURING SOCK READ\r\n"); 01506 sock_total_count=0; 01507 SockON_Data_Len=0; 01508 SockON_Data_Length=0; 01509 status_flag.enable_sock_read = WIFI_FALSE; 01510 status_flag.stop_event_dequeue = WIFI_FALSE; 01511 status_flag.Q_Contains_Message = WIFI_FALSE; 01512 status_flag.Q_Contains_Data = WIFI_FALSE; 01513 enable_pending_data = 0; 01514 status_flag.enable_receive_data_chunk = WIFI_FALSE; 01515 WIND64_count=0; 01516 process_buffer_index =5; 01517 //status_flag.enable_query = TRUE; //do we have more data? 01518 Fillptr=0; 01519 status_flag.sock_read_ongoing = WIFI_FALSE; 01520 if(status_flag.data_pending_sockD) 01521 { 01522 status_flag.data_pending_sockD=WIFI_FALSE; 01523 number_of_bytes=0; 01524 status_flag.switch_by_default_to_command_mode=WIFI_TRUE; 01525 WiFi_switch_to_command_mode(); //switch by default 01526 } 01527 01528 memset(process_buffer, 0x00, MAX_BUFFER_GLOBAL); 01529 break; 01530 } 01531 } 01532 01533 /*now check if end of msg received*/ 01534 if(sock_total_count >= SockON_Data_Length) 01535 { 01536 #if DEBUG_PRINT 01537 printf("\nReached SockON_Data_Len \r\n"); 01538 #endif 01539 01540 if(sock_total_count > SockON_Data_Length) 01541 { 01542 int databytes = sock_total_count - SockON_Data_Length; 01543 __disable_irq(); 01544 rewind_buffer_queue(&big_buff, databytes); 01545 __enable_irq(); 01546 memset(process_buffer+(Fillptr - databytes), 0x00, databytes); 01547 Fillptr = Fillptr - databytes; 01548 } 01549 chunk_size = Fillptr; 01550 message_size = Socket_Data_Length; 01551 memcpy(UserDataBuff, process_buffer, Fillptr); 01552 Fillptr = 0; 01553 sock_total_count = 0; 01554 SockON_Data_Len = 0; 01555 status_flag.Q_Contains_Data = WIFI_FALSE; 01556 status_flag.Q_Contains_Message = WIFI_FALSE; 01557 status_flag.enable_receive_data_chunk = WIFI_FALSE; 01558 enable_pending_data = 0; 01559 01560 if(status_flag.data_pending_sockD) 01561 { 01562 // status_flag.data_pending_sockD=WIFI_FALSE; 01563 number_of_bytes=0; 01564 sockon_id_user = 0; 01565 if(WIND64_count>0) 01566 WIND64_count--; //decrease the number of pending WIND:64 Events 01567 if(WIND64_count==0) { 01568 status_flag.switch_by_default_to_command_mode=WIFI_TRUE; 01569 WiFi_switch_to_command_mode(); //switch by default 01570 } 01571 status_flag.enable_query = WIFI_FALSE; 01572 } 01573 else 01574 { 01575 status_flag.enable_query = WIFI_TRUE; 01576 sockon_id_user = sockon_query_id; 01577 /*@TODO: Do not need to prevent OK push in case of server socket*/ 01578 status_flag.prevent_push_OK_event = WIFI_TRUE; //prevent the qeueuing of the OK after this read operation 01579 } 01580 01581 status_flag.enable_sock_read = WIFI_FALSE; 01582 status_flag.sock_read_ongoing = WIFI_FALSE; 01583 Stop_Dequeue(); //Stop dequeue till user callback returns 01584 status_flag.Pending_SockON_Callback = WIFI_TRUE; //set this to callback to user with User Buffer pointer 01585 //do we have more data? 01586 if(status_flag.data_pending_sockD) 01587 { 01588 status_flag.stop_event_dequeue = WIFI_FALSE; 01589 status_flag.data_pending_sockD = WIFI_FALSE; 01590 } 01591 memset(process_buffer, 0x00, MAX_BUFFER_GLOBAL); 01592 break; 01593 } 01594 01595 if(Fillptr >= MAX_BUFFER_GLOBAL-1) 01596 { 01597 message_size = SockON_Data_Length; 01598 chunk_size = 511; 01599 memcpy(UserDataBuff, process_buffer, Fillptr); 01600 Fillptr = 0; 01601 process_buffer_index = 5; 01602 Stop_Dequeue(); 01603 memset(process_buffer, 0x00, MAX_BUFFER_GLOBAL); 01604 status_flag.Pending_SockON_Callback = WIFI_TRUE; //set this to callback to user with User Buffer pointer 01605 } 01606 break; 01607 } 01608 01609 if (status_flag.enable_receive_http_response && pop_buffer_size) // http response enabled 01610 { 01611 status_flag.sock_read_ongoing = WIFI_TRUE; 01612 if((pStr = (strstr((const char *)process_buffer + process_buffer_index - 6,"\r\nOK\r\n"))) != NULL) 01613 { 01614 #if DEBUG_PRINT 01615 printf("\r\nOK\r\n"); //http response completed 01616 #endif 01617 01618 if(*(pStr+7) != '\0') 01619 { 01620 int len = (uint8_t *)pStr - (uint8_t *)process_buffer; 01621 int extra_bytes = Fillptr - (len+6); 01622 __disable_irq(); 01623 rewind_buffer_queue(&big_buff, extra_bytes); 01624 __enable_irq(); 01625 01626 memset(process_buffer+len+7, 0x00, extra_bytes); 01627 Fillptr = Fillptr - extra_bytes; 01628 } 01629 01630 memcpy(UserDataBuff, process_buffer, Fillptr); 01631 UserDataBuff_index = Fillptr; 01632 status_flag.AT_Response_Received = WIFI_TRUE; 01633 status_flag.enable_receive_data_chunk = WIFI_FALSE; 01634 status_flag.enable_receive_http_response = WIFI_FALSE; 01635 Stop_Dequeue(); 01636 AT_RESPONSE = WiFi_MODULE_SUCCESS; 01637 status_flag.sock_read_ongoing = WIFI_FALSE; 01638 status_flag.Q_Contains_Data = WIFI_FALSE; 01639 status_flag.Q_Contains_Message = WIFI_FALSE; 01640 status_flag.stop_event_dequeue = WIFI_FALSE; 01641 process_buffer_index = 6; 01642 memset(process_buffer, 0x00, Fillptr); 01643 Fillptr=0; 01644 01645 if(status_flag.enable_receive_file_response) 01646 status_flag.FILE_Data_available = WIFI_TRUE; 01647 else 01648 status_flag.HTTP_Data_available=WIFI_TRUE; 01649 } 01650 01651 else if(((strstr((const char *)process_buffer + process_buffer_index-6,"ERROR"))) != NULL) 01652 { 01653 #if DEBUG_PRINT 01654 printf("\r\nERROR\r\n"); 01655 #endif 01656 status_flag.AT_Response_Received = WIFI_TRUE; 01657 AT_RESPONSE = WiFi_AT_CMD_RESP_ERROR; 01658 status_flag.sock_read_ongoing = WIFI_FALSE; 01659 status_flag.enable_receive_data_chunk = WIFI_FALSE; 01660 status_flag.Q_Contains_Data = WIFI_FALSE; 01661 status_flag.Q_Contains_Message = WIFI_FALSE; 01662 Fillptr=0; 01663 process_buffer_index=6; 01664 status_flag.enable_receive_http_response = WIFI_FALSE; 01665 memset(process_buffer, 0x00, MAX_BUFFER_GLOBAL); 01666 01667 if(status_flag.enable_receive_file_response) 01668 status_flag.FILE_Data_available = WIFI_FALSE; 01669 else 01670 status_flag.HTTP_Data_available=WIFI_FALSE; 01671 } 01672 01673 process_buffer_index = Fillptr; 01674 if(Fillptr == MAX_BUFFER_GLOBAL-1 ) 01675 { 01676 memcpy(UserDataBuff, process_buffer, Fillptr); 01677 memcpy(&HTTP_Runway_Buff, &UserDataBuff[505], 6); 01678 memset(&UserDataBuff[505], 0x00, 6); 01679 memset(process_buffer, 0x00, MAX_BUFFER_GLOBAL); 01680 memcpy(&process_buffer, &HTTP_Runway_Buff, 6); 01681 memset(HTTP_Runway_Buff, 0x00, 6); 01682 Fillptr = 6; 01683 process_buffer_index = 6; 01684 UserDataBuff_index = 505; 01685 Stop_Dequeue(); 01686 01687 if(status_flag.enable_receive_file_response) 01688 status_flag.FILE_Data_available = WIFI_TRUE; 01689 else 01690 status_flag.HTTP_Data_available=WIFI_TRUE; 01691 } 01692 break; 01693 } 01694 } 01695 if((!status_flag.Q_Contains_Message) && status_flag.enable_receive_wifi_scan_response) 01696 { 01697 /*now check if end of msg received*/ 01698 if((process_buffer[Fillptr-2]==0xD) && (process_buffer[Fillptr-1]==0xA)) 01699 { 01700 if(scanned_ssids < user_scan_number) 01701 { 01702 pStr = (char *) strstr((const char *)&process_buffer,"CHAN:"); 01703 if(pStr != NULL) 01704 { 01705 databytes_No[0] = *(pStr + 6) ; 01706 databytes_No[1] = *(pStr + 7) ; 01707 01708 chan_value = (((databytes_No[0] - '0') * 10 ) + (databytes_No[1] - '0')); 01709 } 01710 01711 wifi_scanned_list[scanned_ssids].channel_num = chan_value; 01712 01713 pStr = (char *) strstr((const char *)&process_buffer,"RSSI:"); 01714 if(pStr != NULL) 01715 { 01716 databytes_No[0] = *(pStr + 7) ; 01717 databytes_No[1] = *(pStr + 8) ; 01718 01719 rssi_value = (((databytes_No[0] - '0') * 10 ) + (databytes_No[1] - '0')); 01720 } 01721 01722 wifi_scanned_list[scanned_ssids].rssi = -(rssi_value); 01723 01724 pStr = (char *) strstr((const char *)&process_buffer,"SSID:"); 01725 if(pStr != NULL) 01726 { 01727 index = 7; 01728 while(*(pStr + index) != 0x27) 01729 { 01730 wifi_scanned_list[scanned_ssids].ssid[index-7] = *(pStr + index); 01731 index++; 01732 if(index==35) break; //max ssid lenght is 30 characters 01733 } 01734 } 01735 01736 pStr = (char *) strstr((const char *)&process_buffer,"WPA "); 01737 if(pStr != NULL) 01738 { 01739 wifi_scanned_list[scanned_ssids].sec_type.wpa = WIFI_TRUE; 01740 } else 01741 wifi_scanned_list[scanned_ssids].sec_type.wpa = WIFI_FALSE; 01742 01743 pStr = (char *) strstr((const char *)&process_buffer,"WPA2 "); 01744 if(pStr != NULL) 01745 { 01746 wifi_scanned_list[scanned_ssids].sec_type.wpa2 = WIFI_TRUE; 01747 } else 01748 wifi_scanned_list[scanned_ssids].sec_type.wpa2 = WIFI_FALSE; 01749 01750 pStr = (char *) strstr((const char *)&process_buffer,"WPS "); 01751 if(pStr != NULL) 01752 { 01753 wifi_scanned_list[scanned_ssids].sec_type.wps = WIFI_TRUE; 01754 } else 01755 wifi_scanned_list[scanned_ssids].sec_type.wps = WIFI_FALSE; 01756 01757 scanned_ssids++;//increment total_networks 01758 //callSpwfSADevice_debug(spwf_dev_,(const char*)scanned_ssids); 01759 } 01760 01761 //end of one line from SCAN result 01762 pStr = (char *) strstr((const char *)&process_buffer,"ERROR"); 01763 if(pStr != NULL) 01764 { 01765 #if DEBUG_PRINT 01766 printf("ERROR Scan Failed"); 01767 #endif 01768 memset(process_buffer, 0x00, MAX_BUFFER_GLOBAL); 01769 Fillptr=0; 01770 status_flag.AT_Response_Received = WIFI_TRUE; 01771 status_flag.enable_receive_wifi_scan_response = WIFI_FALSE; 01772 status_flag.Scan_Ongoing = WIFI_FALSE; //Enable next scan 01773 AT_RESPONSE = WiFi_AT_CMD_RESP_ERROR; 01774 break; 01775 } 01776 01777 #if DEBUG_PRINT 01778 printf((const char*)process_buffer); 01779 #endif 01780 01781 callSpwfSADevice_debug(spwf_dev_,(const char*)process_buffer); 01782 01783 if(((strstr((const char *)process_buffer,"OK\r\n"))) != NULL /*|| scanned_ssids==10*/)/*Max 10 networks supported*/ 01784 { 01785 //print and go for next line 01786 //If Any part of scan line contains "OK" this will exit!! 01787 #if DEBUG_PRINT 01788 printf("\nOK\r\n"); 01789 #endif 01790 status_flag.Scan_Ongoing = WIFI_FALSE; //Enable next scan 01791 scanned_ssids=0; 01792 Fillptr=0; 01793 status_flag.AT_Response_Received = WIFI_TRUE; 01794 status_flag.enable_receive_wifi_scan_response = WIFI_FALSE; 01795 AT_RESPONSE = WiFi_MODULE_SUCCESS; 01796 memset(process_buffer, 0x00, MAX_BUFFER_GLOBAL); 01797 break; 01798 } 01799 01800 Fillptr=0; 01801 memset(process_buffer, 0x00, MAX_BUFFER_GLOBAL); 01802 } 01803 01804 if(Fillptr>=MAX_BUFFER_GLOBAL-1) 01805 { 01806 #if DEBUG_PRINT 01807 printf("\rHTTP: process_buffer Max Buffer Size reached\r\n"); 01808 #endif 01809 Fillptr=0; 01810 memset(process_buffer, 0x00, MAX_BUFFER_GLOBAL); 01811 } 01812 } 01813 01814 if((!status_flag.Q_Contains_Message) && status_flag.enable_fw_update_read) 01815 { 01816 #ifdef DEBUG_PRINT 01817 printf("*"); //print * till finish 01818 #endif 01819 01820 pStr = (char *) strstr((const char *)process_buffer,"Complete!"); 01821 01822 if(pStr != NULL) 01823 { 01824 #ifdef DEBUG_PRINT 01825 printf("\r\nUpdate complete\r\n"); 01826 #endif 01827 status_flag.AT_Response_Received = WIFI_TRUE; 01828 AT_RESPONSE = WiFi_MODULE_SUCCESS; 01829 status_flag.enable_fw_update_read = WIFI_FALSE; 01830 Fillptr=0; 01831 memset(process_buffer, 0x00, strlen((char const *)process_buffer)); 01832 } 01833 if(Fillptr>=MAX_BUFFER_GLOBAL) 01834 { 01835 Fillptr=0; 01836 memset(process_buffer, 0x00, strlen((char const *)process_buffer)); 01837 } 01838 01839 //No change of state till we get "+WIND:17:F/W update complete!" 01840 } 01841 01842 /*else no other case is satisfied at least do the following*/ 01843 { 01844 if(Fillptr>=MAX_BUFFER_GLOBAL-1) 01845 { 01846 Fillptr=0; 01847 memset(process_buffer, 0x00,MAX_BUFFER_GLOBAL-1); 01848 } 01849 } 01850 } 01851 } 01852 01853 /** 01854 * @brief Process_Wind_Indication_Cmd 01855 * Process Wind indication command 01856 * @param process_buff_ptr: pointer of WiFi indication buffer 01857 * @retval None 01858 */ 01859 01860 void Process_Wind_Indication(uint8_t *process_buff_ptr) 01861 { 01862 char * pStr = (char*)process_buff_ptr; 01863 char Indication_No[2]; 01864 char databytes_No[4]; 01865 #if DEBUG_PRINT 01866 printf((const char*)process_buff_ptr); 01867 #endif 01868 //callSpwfSADevice_debug(spwf_dev_,(const char*)process_buff_ptr); 01869 01870 char * ptr_offset; 01871 int i; 01872 01873 uint32_t wind55_data_len=0; 01874 WiFi_Indication_t Wind_No = Undefine_Indication; 01875 01876 if(pStr != NULL) 01877 { 01878 pStr = (char *) strstr((const char *)(pStr),"WIND:");/////// 01879 01880 if(pStr != NULL) 01881 { 01882 Indication_No[0] = *(pStr + 5) ; 01883 Indication_No[1] = *(pStr + 6) ; 01884 01885 if( Indication_No[1] == ':') 01886 { 01887 status_flag.Single_Digit_Indication = WIFI_TRUE; 01888 /* Convert char to integer */ 01889 Wind_No = (WiFi_Indication_t)(Indication_No[0] - '0'); 01890 } 01891 else 01892 { 01893 status_flag.Single_Digit_Indication = WIFI_FALSE; 01894 /* Convert char to integer */ 01895 Wind_No = (WiFi_Indication_t)(((Indication_No[0] - '0') * 10 ) + (Indication_No[1] - '0')); 01896 } 01897 01898 event_s.wind = Wind_No; 01899 event_s.event = WIFI_WIND_EVENT; 01900 01901 switch (Wind_No) 01902 { 01903 case SockON_Data_Pending: /*WIND:55*/ 01904 01905 /*+WIND:55:Pending Data:%d:%d*/ 01906 ptr_offset = (char *) strstr((const char *)&process_buffer,"Data:"); 01907 01908 /*Need to find out which socket ID has data pending*/ 01909 databytes_No[0] = *(ptr_offset + 5); 01910 01911 SocketId = (databytes_No[0] - '0');//Max number of sockets is 8 (so single digit) 01912 event_s.socket_id = SocketId; 01913 enable_pending_data = 1; 01914 01915 /* EQ. Check for ENC string to identify TLS case. Set enable_query */ 01916 if ( (*(ptr_offset + 7) == 'E') && (*(ptr_offset + 8) == 'N') && (*(ptr_offset + 9) == 'C') ) 01917 { 01918 event_s.enc = WIFI_TRUE; 01919 } 01920 else 01921 { 01922 //And find the length of the data 01923 databytes_No[0] = *(ptr_offset + 7); 01924 databytes_No[1] = *(ptr_offset + 8); 01925 databytes_No[2] = *(ptr_offset + 9); 01926 databytes_No[3] = *(ptr_offset + 10); 01927 01928 if( databytes_No[1] == '\r') 01929 { 01930 wind55_data_len = databytes_No[0] - '0'; 01931 } 01932 else if( databytes_No[2] == '\r') 01933 { 01934 wind55_data_len = (((databytes_No[0] - '0') * 10 ) + (databytes_No[1] - '0')); 01935 } 01936 else if( databytes_No[3] == '\r') //it's a 3-digit number 01937 { 01938 wind55_data_len = (((databytes_No[0] - '0') * 100 ) + ((databytes_No[1] - '0') * 10 ) + (databytes_No[2] - '0')); 01939 } 01940 else //it's a 4-digit number 01941 { 01942 wind55_data_len = ((databytes_No[0] - '0') * 1000 ) + ((databytes_No[1] - '0') * 100 ) + ((databytes_No[2] - '0') * 10) + (databytes_No[3] - '0'); 01943 } 01944 01945 event_s.data_length = wind55_data_len; 01946 wind55_data_len = 0; 01947 } 01948 break; 01949 01950 case SockON_Server_Socket_Closed: 01951 //Find the id of the socket closed 01952 //ptr_offset = (char *) strstr((const char *)pStr,"+WIND:58"); 01953 databytes_No[0] = *(pStr + 22) ; 01954 databytes_No[1] = *(pStr + 23) ; 01955 if( databytes_No[1] == '\r') 01956 { 01957 remote_socket_closed_id = databytes_No[0] - '0'; 01958 } 01959 else 01960 { 01961 remote_socket_closed_id = (((databytes_No[0] - '0') * 10 ) + (databytes_No[1] - '0')); 01962 } 01963 event_s.socket_id = remote_socket_closed_id; 01964 break; 01965 01966 case SockD_Pending_Data: 01967 if(status_flag.prevent_push_WIFI_event) 01968 { 01969 #ifdef DEBUG_PRINT 01970 printf(">>IG\r\n"); 01971 #endif 01972 return; ///exit if push is prevented 01973 } 01974 01975 /* @TODO: Do something to delay the de-Q of pending packets so that any further 01976 pending WIND64 packets are allowed to arrive and get Q-ed */ 01977 01978 if(status_flag.event_deQ_x_wind64) //which means there is already a previous WIND64 01979 { 01980 wind64_DQ_wait=0; //reset the timer 01981 } 01982 else 01983 { 01984 status_flag.stop_event_dequeue = WIFI_TRUE;//Stop the event de-Q 01985 status_flag.event_deQ_x_wind64 = WIFI_TRUE;//Set the flag 01986 } 01987 //Start Reading data from Client Here. 01988 // +WIND:64:Sockd Pending Data:1:130:130 01989 ptr_offset = (char *) strstr((const char *)&process_buffer,"Data:"); 01990 01991 //Store the packet number 01992 databytes_No[0] = *(ptr_offset + 5) ; 01993 event_s.wind64_pending_packet_no = databytes_No[0] - '0'; 01994 01995 //And now find the data length 01996 databytes_No[0] = *(ptr_offset + 8) ;//points to number just after 2nd colon 01997 databytes_No[1] = *(ptr_offset + 9) ; 01998 databytes_No[2] = *(ptr_offset + 10) ; 01999 databytes_No[3] = *(ptr_offset + 11) ; 02000 02001 if( databytes_No[0] == ':')//then it is a 1 digit number 02002 { 02003 databytes_No[0] = *(ptr_offset + 7) ; 02004 databytes_No[1] = *(ptr_offset + 8) ; 02005 } 02006 else if(databytes_No[1] == ':')//two digit number 02007 { 02008 databytes_No[0] = *(ptr_offset + 7) ; 02009 databytes_No[1] = *(ptr_offset + 8) ; 02010 databytes_No[2] = *(ptr_offset + 9) ; 02011 //databytes_No[3] = *(ptr_offset + 13) ; 02012 } 02013 else if(databytes_No[2] == ':')//three digit number 02014 { 02015 databytes_No[0] = *(ptr_offset + 7) ; 02016 databytes_No[1] = *(ptr_offset + 8) ; 02017 databytes_No[2] = *(ptr_offset + 9) ; 02018 databytes_No[3] = *(ptr_offset + 10) ; 02019 } 02020 else if(databytes_No[3] == ':')//four digit number 02021 { 02022 databytes_No[0] = *(ptr_offset + 7) ; 02023 databytes_No[1] = *(ptr_offset + 8) ; 02024 databytes_No[2] = *(ptr_offset + 9) ; 02025 databytes_No[3] = *(ptr_offset + 10) ; 02026 } 02027 02028 if( databytes_No[1] == ':') 02029 { 02030 interim_number_of_bytes = databytes_No[0] - '0'; 02031 } 02032 else if( databytes_No[2] == ':') 02033 { 02034 interim_number_of_bytes = (((databytes_No[0] - '0') * 10 ) + (databytes_No[1] - '0')); 02035 } 02036 else if( databytes_No[3] == ':') 02037 { 02038 interim_number_of_bytes = (((databytes_No[0] - '0') * 100 ) + ((databytes_No[1] - '0') * 10 ) + (databytes_No[2] - '0')); 02039 } 02040 else //it's a 4-digit number 02041 { 02042 interim_number_of_bytes = (((databytes_No[0] - '0') * 1000 ) + ((databytes_No[1] - '0') * 100 ) + ((databytes_No[2] - '0') * 10 ) + (databytes_No[3] - '0')); 02043 } 02044 02045 if(WIND64_count >= 1) /*WIND:64 came after pop of previous event and switch to data mode was issued*/ 02046 /*WIND:64 came before pop of previous event and switch to data mode was issued*/ 02047 { 02048 event_s.data_length = interim_number_of_bytes;// - (730*WIND64_count); 02049 WIND64_count++; //Count of the number of queued WIND:64 Events 02050 interim_number_of_bytes = 0; 02051 break; 02052 } 02053 else 02054 { 02055 event_s.data_length = interim_number_of_bytes; 02056 WIND64_count++; //Count of the number of queued WIND:64 Events 02057 interim_number_of_bytes = 0; 02058 } 02059 break; 02060 02061 case In_Command_Mode: 02062 status_flag.command_mode= WIFI_TRUE; 02063 status_flag.data_mode= WIFI_FALSE; 02064 WIND64_count=0; //reset the WIND64 count since the previous data mode would have consumed all data 02065 break; 02066 02067 case In_Data_Mode: 02068 status_flag.command_mode = WIFI_FALSE; 02069 status_flag.data_mode = WIFI_TRUE; 02070 02071 if(status_flag.switch_by_default_to_command_mode == WIFI_TRUE) 02072 { 02073 if(!status_flag.command_mode) 02074 { 02075 WiFi_switch_to_command_mode();//switch by default 02076 } 02077 } 02078 if(status_flag.data_pending_sockD == WIFI_TRUE) 02079 { 02080 02081 process_buffer_index =5; 02082 status_flag.enable_sock_read = WIFI_TRUE;//now data will start coming 02083 //Set the data-length to read 02084 SockON_Data_Length = number_of_bytes; 02085 } 02086 break; 02087 02088 case WiFi__MiniAP_Associated: 02089 //Find out which client joined by parsing the WIND //+WIND:28 02090 ptr_offset = (char *) strstr((const char *)&process_buffer,"+WIND:28"); 02091 for(i=17;i<=33;i++) 02092 client_MAC_address[i-17] = *(ptr_offset + i) ; 02093 WiFi_WIND_State.WiFiAPClientJoined = WIFI_TRUE; 02094 break; 02095 02096 case WiFi_MiniAP_Disassociated: 02097 //Find out which client left by parsing the WIND //+WIND:72 02098 ptr_offset = (char *) strstr((const char *)&process_buffer,"+WIND:72"); 02099 for(i=17;i<=33;i++) 02100 client_MAC_address[i-17] = *(ptr_offset + i) ; 02101 WiFi_WIND_State.WiFiAPClientLeft = WIFI_TRUE; 02102 break; 02103 02104 case Console_Active: 02105 case Poweron : 02106 case WiFi_Reset: 02107 case Watchdog_Running: 02108 case Heap_Too_Small: 02109 case WiFi_Hardware_Dead: 02110 case Watchdog_Terminating: 02111 case SysTickConfigure: 02112 case Hard_Fault: 02113 case StackOverflow: 02114 case MallocFailed: 02115 case Error: 02116 case WiFi_PS_Mode_Failure: 02117 case CopyrightInfo: 02118 case WiFi_BSS_Regained: 02119 case WiFi_Signal_LOW: 02120 case WiFi_Signal_OK: 02121 case FW_update: 02122 case Encryption_key_Not_Recognized: 02123 case WiFi_Join: 02124 case JOINFAILED: 02125 case WiFi_Scanning: 02126 case SCANBLEWUP: 02127 case SCANFAILED: 02128 case WiFi_Up: 02129 case WiFi_Association_Successful: 02130 case WiFi_Started_MiniAP_Mode: 02131 case Start_Failed : 02132 case WiFi_EXCEPTION : 02133 case WiFi_Hardware_Started : 02134 case WiFi_BSS_LOST: 02135 case WiFi_Unhandled_Event: 02136 case Scan_Complete: 02137 case WiFi_UNHANDLED_IND: 02138 case WiFi_UNHANDLED: 02139 case WiFi_Powered_Down: 02140 case WiFi_MiniAP_Mode : 02141 case WiFi_Deauthentication: 02142 case WiFi_Disassociation: 02143 case RX_MGMT: 02144 case RX_DATA: 02145 case RX_UNK: 02146 case DOT11_AUTHILLEGAL: 02147 case Creating_PSK: 02148 case WPA_Terminated : 02149 case WPA_Supplicant_Failed: 02150 case WPA_Handshake_Complete: 02151 case GPIO_line: 02152 case Wakeup: 02153 case Factory_debug: 02154 case Remote_Configuration: 02155 break; 02156 default: 02157 break; 02158 } 02159 memset(process_buffer, 0x00, MAX_BUFFER_GLOBAL); 02160 } 02161 } 02162 } 02163 02164 /** 02165 * @brief Process_Dequeued_Wind_Indication 02166 * Process Wind Indication after popping from Queue 02167 * @param event_pop_s1 popped event contents 02168 * @retval None 02169 */ 02170 02171 void Process_WiFi_Indication_Cmd(event_s_TypeDef * event_pop_s1) 02172 { 02173 switch(event_pop_s1->wind) 02174 { 02175 case Console_Active: 02176 WiFi_WIND_State.ConsoleActive = WIFI_TRUE; 02177 break; 02178 case Poweron : 02179 WiFi_WIND_State.WiFiPowerON = WIFI_TRUE; 02180 break; 02181 case WiFi_Reset: 02182 WiFi_WIND_State.WiFiReset = WIFI_TRUE; 02183 break; 02184 case Watchdog_Running: 02185 break; 02186 case Heap_Too_Small: 02187 WiFi_WIND_State.HeapTooSmall=WIFI_TRUE; 02188 break; 02189 case WiFi_Hardware_Dead: 02190 WiFi_WIND_State.WiFiHWFailure = WIFI_TRUE; 02191 break; 02192 case Watchdog_Terminating: 02193 break; 02194 case SysTickConfigure: 02195 break; 02196 case Hard_Fault: 02197 WiFi_WIND_State.HardFault = WIFI_TRUE; 02198 break; 02199 case StackOverflow: 02200 WiFi_WIND_State.StackOverflow = WIFI_TRUE; 02201 break; 02202 case MallocFailed: 02203 WiFi_WIND_State.MallocFailed = WIFI_TRUE; 02204 break; 02205 case Error: 02206 WiFi_WIND_State.InitFailure = WIFI_TRUE; 02207 break; 02208 case WiFi_PS_Mode_Failure: 02209 WiFi_WIND_State.PS_Mode_Failure = WIFI_TRUE; 02210 break; 02211 case CopyrightInfo: 02212 break; 02213 case WiFi_BSS_Regained: 02214 break; 02215 case WiFi_Signal_LOW: 02216 WiFi_WIND_State.WiFiSignalLOW = WIFI_TRUE; 02217 break; 02218 case WiFi_Signal_OK : 02219 break; 02220 case FW_update: 02221 break; 02222 case Encryption_key_Not_Recognized: 02223 break; 02224 case WiFi_Join : 02225 WiFi_WIND_State.WiFiJoin = WIFI_TRUE; 02226 break; 02227 case JOINFAILED : 02228 WiFi_WIND_State.WiFiJoinFailed = WIFI_TRUE; 02229 break; 02230 case WiFi_Scanning : 02231 WiFi_WIND_State.WiFiScanning = WIFI_TRUE; 02232 break; 02233 case SCANBLEWUP: 02234 WiFi_WIND_State.WiFiScanBlewUp = WIFI_TRUE; 02235 break; 02236 case SCANFAILED: 02237 WiFi_WIND_State.WiFiScanFailed = WIFI_TRUE; 02238 break; 02239 case WiFi_Up: 02240 WiFi_WIND_State.WiFiUp = WIFI_TRUE; 02241 break; 02242 case WiFi_Association_Successful: 02243 WiFi_WIND_State.WiFiAssociation = WIFI_TRUE; 02244 break; 02245 case WiFi_Started_MiniAP_Mode: 02246 WiFi_WIND_State.WiFiStarted_MiniAPMode = WIFI_TRUE; 02247 break; 02248 case Start_Failed : 02249 WiFi_WIND_State.StartFailed = WIFI_TRUE; 02250 break; 02251 case WiFi_EXCEPTION : 02252 WiFi_WIND_State.WiFiException = WIFI_TRUE; 02253 break; 02254 case WiFi_Hardware_Started : 02255 wifi_ready++; 02256 status_flag.WiFi_Enabled = WIFI_TRUE; 02257 WiFi_WIND_State.WiFiHWStarted = WIFI_TRUE; 02258 /*If this is a start-up after standby*/ 02259 if(status_flag.trigger_wakeup_callback == WIFI_TRUE) 02260 { 02261 status_flag.trigger_wakeup_callback = WIFI_FALSE; 02262 status_flag.Standby_Enabled = WIFI_FALSE; 02263 status_flag.standby_resume_callback = WIFI_TRUE; 02264 } 02265 break; 02266 case WiFi_BSS_LOST: 02267 break; 02268 case WiFi_Unhandled_Event: 02269 break; 02270 case Scan_Complete: 02271 WiFi_WIND_State.WiFiScanComplete = WIFI_TRUE; 02272 status_flag.Scan_Ongoing = WIFI_FALSE; 02273 break; 02274 case WiFi_UNHANDLED_IND: 02275 WiFi_WIND_State.WiFiUnHandledInd = WIFI_TRUE; 02276 break; 02277 case WiFi_UNHANDLED: 02278 break; 02279 case WiFi_Powered_Down: 02280 status_flag.WiFi_Enabled = WIFI_FALSE; 02281 //wifi_ready = 0; 02282 WiFi_WIND_State.WiFiHWStarted = WIFI_FALSE; 02283 WiFi_WIND_State.WiFiPowerDown = WIFI_TRUE; 02284 break; 02285 case WiFi_MiniAP_Mode : 02286 WiFi_WIND_State.WiFiMiniAPMode = WIFI_TRUE; 02287 break; 02288 case WiFi_Deauthentication: 02289 WiFi_WIND_State.WiFiDeauthentication = WIFI_TRUE; 02290 break; 02291 case WiFi_Disassociation: 02292 WiFi_WIND_State.WiFiDisAssociation = WIFI_TRUE; 02293 break; 02294 case RX_MGMT: 02295 WiFi_WIND_State.WiFiRXMgmt = WIFI_TRUE; 02296 break; 02297 case RX_DATA: 02298 WiFi_WIND_State.WiFiRXData = WIFI_TRUE; 02299 break; 02300 case RX_UNK: 02301 WiFi_WIND_State.WiFiRxUnk = WIFI_TRUE; 02302 break; 02303 case DOT11_AUTHILLEGAL: 02304 break; 02305 case Creating_PSK: 02306 break; 02307 case WPA_Terminated : 02308 break; 02309 case WPA_Supplicant_Failed: 02310 break; 02311 case WPA_Handshake_Complete: 02312 break; 02313 case GPIO_line: 02314 break; 02315 case Wakeup: 02316 break; 02317 case Factory_debug: 02318 break; 02319 02320 case SockON_Data_Pending: 02321 02322 /* +WIND:55:Pending Data:%d:%d */ 02323 if (status_flag.enable_sock_read == WIFI_TRUE) 02324 { 02325 #if DEBUG_PRINT 02326 printf ("\nAlert!\r\n"); 02327 #endif 02328 status_flag.enable_sock_read = WIFI_FALSE; 02329 status_flag.enable_receive_data_chunk = WIFI_FALSE; 02330 //break; 02331 } 02332 sockon_query_id = event_pop_s1->socket_id; 02333 if(open_sockets[sockon_query_id]) 02334 { 02335 status_flag.enable_query = WIFI_TRUE; 02336 status_flag.stop_event_dequeue = WIFI_TRUE; 02337 } 02338 02339 break; 02340 02341 case SockON_Server_Socket_Closed: 02342 client_socket_close_id = event_pop_s1->socket_id; 02343 status_flag.Client_Socket_Close_Cmd = WIFI_TRUE; 02344 break; 02345 02346 case SockD_Pending_Data: 02347 number_of_bytes = event_pop_s1->data_length; 02348 status_flag.data_pending_sockD = WIFI_TRUE; 02349 status_flag.stop_event_dequeue = WIFI_TRUE; //Stop any more event de-queue 02350 status_flag.enable_receive_data_chunk = WIFI_TRUE; // read data in chunk now from ring buffer 02351 02352 if(!status_flag.data_mode) 02353 { 02354 if(event_pop_s1->wind64_pending_packet_no == 1) { //If this is the first WIND64 pending event de-Q'ed 02355 status_flag.switch_by_default_to_command_mode = WIFI_FALSE; //we don't want to switch back to command mode after changing to data mode here 02356 WiFi_switch_to_data_mode(); //switch by default 02357 } 02358 } 02359 else //already data is coming from previous WIND:64 02360 { 02361 process_buffer_index =5; 02362 status_flag.enable_sock_read = WIFI_TRUE;//n 02363 SockON_Data_Length = number_of_bytes; 02364 } 02365 break; 02366 02367 case Incoming_socket_client: 02368 status_flag.Client_Connected = WIFI_TRUE; 02369 wifi_client_connected=1; //Set this so that the callback can be made to the user 02370 break; 02371 02372 case Outgoing_socket_client: 02373 status_flag.Client_Disconnected = WIFI_TRUE; 02374 wifi_client_disconnected=0;//Set this so that the callback can be made to the user 02375 wifi_client_connected = 0; 02376 break; 02377 02378 case SockD_Dropping_Data: 02379 WiFi_WIND_State.WiFiSockdDataLost = WIFI_TRUE; 02380 break; 02381 02382 case Low_Power_Mode_Enabled: 02383 status_flag.Low_Power_Enabled = WIFI_TRUE; 02384 break; 02385 02386 case Going_Into_Standby: 02387 status_flag.Standby_Enabled = WIFI_TRUE; 02388 break; 02389 02390 case Resuming_From_Standby: 02391 status_flag.Standby_Enabled = WIFI_FALSE; 02392 status_flag.standby_resume_callback = WIFI_TRUE; 02393 break; 02394 case Going_Into_DeepSleep: 02395 status_flag.Deep_Sleep_Enabled = WIFI_TRUE; 02396 break; 02397 case Resuming_From_DeepSleep: 02398 status_flag.Deep_Sleep_Enabled = WIFI_FALSE; 02399 Start_DeepSleep_Timer(); 02400 break; 02401 default: 02402 break; 02403 } 02404 } 02405 02406 /** 02407 * @brief Queue_Http_Get_ Event 02408 * Queue an HTTP-Request Event (GET/POST) 02409 * @param hostname hostname for HTTP-GET/POST 02410 * @param path path for HTTP-GET 02411 * @param port_number port_number for HTTP-GET 02412 * @param pURL_path full URL for HTTP-POST 02413 * @retval None 02414 */ 02415 02416 void Queue_Http_Event(uint8_t * hostname, uint8_t * path, uint32_t port_number, uint8_t * pURL_path) 02417 { 02418 Wait_For_Sock_Read_To_Complete(); 02419 if(pURL_path == NULL) 02420 { 02421 curr_hostname = hostname; 02422 curr_path = path; 02423 curr_port_number = port_number; 02424 } 02425 else 02426 curr_pURL = pURL_path; 02427 02428 event_s.event = WIFI_HTTP_EVENT; 02429 __disable_irq(); 02430 push_eventbuffer(&event_buff, event_s); 02431 __enable_irq(); 02432 02433 reset_event(&event_s); 02434 } 02435 02436 /** 02437 * @brief Queue_Client_Write_Event 02438 * Queues a Client Socket write event. 02439 * @param sock_id socket ID to write to 02440 * @param DataLength length of the data to be written 02441 * @param pData pointer to data 02442 * @retval None 02443 */ 02444 void Queue_Client_Write_Event(uint8_t sock_id, uint16_t DataLength, char * pData) 02445 { 02446 Wait_For_Sock_Read_To_Complete(); 02447 curr_DataLength = DataLength; 02448 curr_data = pData; 02449 curr_sockID = sock_id; 02450 status_flag.enable_client_socket_write = WIFI_TRUE; 02451 02452 event_s.event = WIFI_CLIENT_SOCKET_WRITE_EVENT; 02453 __disable_irq(); 02454 push_eventbuffer(&event_buff, event_s); 02455 __enable_irq(); 02456 02457 reset_event(&event_s); 02458 } 02459 02460 /** 02461 * @brief Queue_Wifi_File_Image_Create_Event 02462 * Queue a File Image Create Event 02463 * @param pHostName hostname 02464 * @param pFileName filename within host 02465 * @param port_number port number to connect to 02466 * @retval None 02467 */ 02468 void Queue_Wifi_File_Event(uint8_t * pHostName, uint8_t * pFileName, uint32_t port_number) 02469 { 02470 Wait_For_Sock_Read_To_Complete(); 02471 event_s.event = WIFI_FILE_EVENT; 02472 curr_filename = pFileName; 02473 curr_hostname = pHostName; 02474 curr_port_number = port_number; 02475 02476 __disable_irq(); 02477 push_eventbuffer(&event_buff, event_s); 02478 __enable_irq(); 02479 02480 reset_event(&event_s); 02481 } 02482 02483 /** 02484 * @brief Queue_Wifi_FW_Update_Event 02485 * Queue a Firmware update Event 02486 * @param hostname hostname 02487 * @param filename_path filename and path within host 02488 * @param port_number port number to connect to 02489 * @retval None 02490 */ 02491 void Queue_Wifi_FW_Update_Event(uint8_t * hostname, uint8_t * filename_path, uint32_t port_number) 02492 { 02493 Wait_For_Sock_Read_To_Complete(); 02494 event_s.event = WIFI_FW_UPDATE_EVENT; 02495 curr_filename = filename_path; 02496 curr_hostname = hostname; 02497 curr_port_number = port_number; 02498 02499 __disable_irq(); 02500 push_eventbuffer(&event_buff, event_s); 02501 __enable_irq(); 02502 02503 reset_event(&event_s); 02504 } 02505 02506 void Queue_Client_Open_Event(uint8_t * hostname, uint32_t port_number, uint8_t * protocol) 02507 { 02508 Wait_For_Sock_Read_To_Complete(); 02509 curr_hostname = hostname; 02510 curr_port_number = port_number; 02511 curr_protocol = protocol; 02512 02513 event_s.event = WIFI_CLIENT_SOCKET_OPEN_EVENT; 02514 __disable_irq(); 02515 push_eventbuffer(&event_buff, event_s); 02516 __enable_irq(); 02517 02518 reset_event(&event_s); 02519 } 02520 02521 void Queue_Client_Close_Event(uint8_t sock_id) 02522 { 02523 Wait_For_Sock_Read_To_Complete(); 02524 02525 event_s.event = WIFI_CLIENT_SOCKET_CLOSE_EVENT; 02526 event_s.socket_id = sock_id; 02527 __disable_irq(); 02528 push_eventbuffer(&event_buff, event_s); 02529 __enable_irq(); 02530 02531 reset_event(&event_s); 02532 } 02533 02534 /** 02535 * @brief Wait_For_Sock_Read_To_Complete 02536 * Wait till sock is over and the OK of read arrives 02537 * @param None 02538 * @retval None 02539 */ 02540 void Wait_For_Sock_Read_To_Complete(void) 02541 { 02542 //wait if read is ongoing or read OK is yet to arrive 02543 while(status_flag.sock_read_ongoing==WIFI_TRUE || status_flag.prevent_push_OK_event==WIFI_TRUE) 02544 { 02545 asm("NOP"); 02546 } 02547 } 02548 02549 /** 02550 * @brief Reset_AT_CMD_Buffer 02551 * Clear USART2 Rx buffer and Wi-Fi AT cmd buffer 02552 * @param None 02553 * @retval None 02554 */ 02555 void Reset_AT_CMD_Buffer() 02556 { 02557 memset(WiFi_AT_Cmd_Buff, 0x00, sizeof WiFi_AT_Cmd_Buff); 02558 } 02559 02560 #ifdef USE_FULL_ASSERT 02561 02562 /** 02563 * @brief Reports the name of the source file and the source line number 02564 * where the assert_param error has occurred. 02565 * @param file: pointer to the source file name 02566 * @param line: assert_param error line source number 02567 * @retval None 02568 */ 02569 void assert_failed(uint8_t* file, uint32_t line) 02570 { 02571 /* User can add his own implementation to report the file name and line number, 02572 ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */ 02573 02574 /* Infinite loop */ 02575 while (1) 02576 { 02577 } 02578 } 02579 02580 #endif 02581 02582 02583 /** 02584 * @brief search_buffer 02585 * search for substring in a buffer that contains null 02586 * @param pSourceBuff : pointer of source buffer 02587 * sourceBuffLen : length of string buffer 02588 * pSearchStringBuff : pointer of search string buffer 02589 * seartchStringLen : length of search string buffer 02590 * @retval pointer of source buffer 02591 */ 02592 char *search_buffer(char *pSourceBuff, uint16_t sourceBuffLen, char *pSearchStringBuff, uint16_t seartchStringLen) 02593 { /* warning: O(n^2) */ 02594 int searchlen = sourceBuffLen - seartchStringLen + 1; 02595 for ( ; searchlen-- > 0; pSourceBuff++) 02596 if (!memcmp(pSourceBuff, pSearchStringBuff, seartchStringLen)) 02597 return pSourceBuff; 02598 return NULL; 02599 } 02600 02601 /** 02602 * @brief Delete_Colon 02603 * delete colon from input buffer 02604 * @param input : pointer of input buffer 02605 * @retval return pointer of updated buffer 02606 */ 02607 char* Delete_Colon(char* input) 02608 { 02609 int i,j; 02610 char *output=input; 02611 02612 /* Delete Colon */ 02613 for (i = 0, j = 0; i<strlen(input); i++,j++) 02614 { 02615 #if 0 02616 if (input[i]!=':') && (input[i]!='\r')&& (input[i]!='\n')&& (input[i]!='O')&& (input[i]!='K')) 02617 output[j]=input[i]; 02618 else 02619 j--; 02620 #else 02621 if ((input[i] ==':') || (input[i]=='\r')|| (input[i]=='\n')|| (input[i]=='O')|| (input[i]=='K')) 02622 j--; 02623 else 02624 output[j]=input[i]; 02625 } 02626 //output[j]=NULL; 02627 #endif 02628 return output; 02629 } 02630 02631 /** 02632 * @brief Read_WiFi_SSID 02633 * Read SSID of WiFi module store in flash 02634 * @param string : pointer of SSID 02635 * @retval return status of AT cmd request 02636 */ 02637 WiFi_Status_t Read_WiFi_SSID(char *string) 02638 { 02639 WiFi_Status_t status = WiFi_MODULE_SUCCESS; 02640 char * ssid = "wifi_ssid"; 02641 char * pStr; 02642 /* AT+S.GCFG=wifi_ssid read SSID */ 02643 Reset_AT_CMD_Buffer(); 02644 02645 /* AT : send AT command */ 02646 sprintf((char*)WiFi_AT_Cmd_Buff,AT_GET_CONFIGURATION_VALUE,ssid); 02647 02648 /* 02649 Response : 02650 # wifi_ssid = 41:6E:53:53:49:44:00:00:00:00:00:00:00:00:00:00:00:00:00: 02651 00:00:00:00:00:00:00:00:00:00:00:00:00<cr><lf> 02652 <cr><lf>OK<cr><lf> 02653 */ 02654 02655 status = USART_Transmit_AT_Cmd(strlen((char*)WiFi_AT_Cmd_Buff)); 02656 if(status == WiFi_MODULE_SUCCESS) 02657 { 02658 status = USART_Receive_AT_Resp(Process_Event); 02659 } 02660 02661 /* find a substring (# wifi_ssid = )inside a USART2_RxBuffer that may contain nulls */ 02662 uint16_t sourceBuffLen = sizeof(get_cfg_value)-1; /* exclude null terminator from length */ 02663 char searchStringBuff[] = "# wifi_ssid = "; 02664 uint16_t stringBuffLen = sizeof(searchStringBuff)-1; /* exclude null terminator from length */ 02665 char *res = search_buffer((char *)&get_cfg_value, sourceBuffLen, searchStringBuff, stringBuffLen); 02666 02667 pStr = (char *) (strstr((const char *)res,"= ")); 02668 if(pStr != NULL) 02669 { 02670 strcat( string, (pStr + 2)); 02671 /* Remove colon,\r,\n,OK strings */ 02672 memcpy(string, Delete_Colon(string) , 32); 02673 } 02674 return status; 02675 } 02676 02677 /** 02678 * @brief Read_WiFi_SecKey 02679 * Read Security key of WiFi module store in flash 02680 * @param string : pointer of Security key 02681 * @retval return status of AT cmd request 02682 */ 02683 WiFi_Status_t Read_WiFi_SecKey(char *string) 02684 { 02685 WiFi_Status_t status = WiFi_MODULE_SUCCESS; 02686 char *seckey = "wifi_wpa_psk_text"; 02687 char *pStr; 02688 02689 /* AT+S.GCFG=wifi_ssid read SSID */ 02690 Reset_AT_CMD_Buffer(); 02691 02692 /* AT : send AT command */ 02693 sprintf((char*)WiFi_AT_Cmd_Buff,AT_GET_CONFIGURATION_VALUE,seckey); 02694 02695 status = USART_Transmit_AT_Cmd(strlen((char*)WiFi_AT_Cmd_Buff)); 02696 if(status == WiFi_MODULE_SUCCESS) 02697 { 02698 status = USART_Receive_AT_Resp(Process_Event); 02699 } 02700 02701 02702 /* find a substring (wifi_wpa_psk_text = )inside a USART2_RxBuffer that may contain nulls */ 02703 uint16_t sourceBuffLen = sizeof(get_cfg_value)-1; /* exclude null terminator from length */ 02704 char searchStringBuff[] = "wifi_wpa_psk_text = "; 02705 uint16_t stringBuffLen = sizeof(searchStringBuff)-1; /* exclude null terminator from length */ 02706 char *res = search_buffer((char *)&get_cfg_value, sourceBuffLen, searchStringBuff, stringBuffLen); 02707 02708 pStr = (char *) (strstr((const char *)res,"= ")); 02709 if(pStr != NULL) 02710 { 02711 strcat( string, (pStr + 2)); 02712 /* Remove colon,\r,\n,OK strings */ 02713 memcpy(string, Delete_Colon(string) , 32); 02714 } 02715 02716 return status; 02717 } 02718 02719 /** 02720 * @brief Read_WiFi_Mode 02721 * Read Wi-Fi mode 0: idle,1 =STA,2 =IBSS,3 =MiniAP 02722 * @param string : return wifi mode type 02723 * @retval return status of AT cmd request 02724 */ 02725 WiFi_Status_t Read_WiFi_Mode(char *string) 02726 { 02727 WiFi_Status_t status = WiFi_MODULE_SUCCESS; 02728 char *mode = "wifi_mode"; 02729 char *pStr; 02730 02731 /* AT+S.GCFG=wifi_mode */ 02732 Reset_AT_CMD_Buffer(); 02733 02734 /* AT : send AT command */ 02735 sprintf((char*)WiFi_AT_Cmd_Buff,AT_GET_CONFIGURATION_VALUE,mode); 02736 02737 status = USART_Transmit_AT_Cmd(strlen((char*)WiFi_AT_Cmd_Buff)); 02738 if(status == WiFi_MODULE_SUCCESS) 02739 { 02740 status = USART_Receive_AT_Resp(Process_Event); 02741 } 02742 02743 pStr = (char *) strstr((const char *)&get_cfg_value,"wifi_mode = "); 02744 if(pStr != NULL) 02745 { 02746 string[0] = *(pStr + 12) ; 02747 } 02748 02749 return status ; 02750 } 02751 02752 /** 02753 * @brief Write_WiFi_SSID 02754 * Store SSID in flash memory of WiFi module 02755 * @param string : pointer of SSID 02756 * @retval return status of AT cmd request 02757 */ 02758 WiFi_Status_t Write_WiFi_SSID(char *string) 02759 { 02760 WiFi_Status_t status = WiFi_MODULE_SUCCESS; 02761 Reset_AT_CMD_Buffer(); 02762 02763 /* AT+S.SSIDTXT=abcd <ExampleSSID> //set SSID */ 02764 sprintf((char*)WiFi_AT_Cmd_Buff,AT_SET_SSID,string); 02765 02766 status = USART_Transmit_AT_Cmd(strlen((char*)WiFi_AT_Cmd_Buff)); 02767 if(status == WiFi_MODULE_SUCCESS) 02768 { 02769 status = USART_Receive_AT_Resp(Process_Event); 02770 } 02771 02772 /* AT&W :Save the settings on the flash memory */ 02773 Reset_AT_CMD_Buffer(); 02774 Save_Current_Setting(); 02775 02776 return status; 02777 02778 } 02779 02780 02781 /** 02782 * @brief Write_WiFi_SecKey 02783 * Store security key in flash memory of WiFi module 02784 * @param string : pointer of security key 02785 * @retval return status of AT cmd request 02786 */ 02787 WiFi_Status_t Write_WiFi_SecKey(char *string) 02788 { 02789 WiFi_Status_t status = WiFi_MODULE_SUCCESS; 02790 Reset_AT_CMD_Buffer(); 02791 02792 /* AT+S.SCFG=wifi_wpa_psk_text,helloworld : set password */ 02793 sprintf((char*)WiFi_AT_Cmd_Buff,"AT+S.SCFG=wifi_wpa_psk_text,%s\r",string); 02794 02795 status = USART_Transmit_AT_Cmd(strlen((char*)WiFi_AT_Cmd_Buff)); 02796 if(status == WiFi_MODULE_SUCCESS) 02797 { 02798 status = USART_Receive_AT_Resp(Process_Event); 02799 } 02800 02801 /* AT&W :Save the settings on the flash memory */ 02802 Reset_AT_CMD_Buffer(); 02803 Save_Current_Setting(); 02804 02805 return status; 02806 } 02807 02808 02809 02810 /** 02811 * @brief PrintErrorMsg 02812 * Print error message on UART terminal 02813 * @param None 02814 * @retval None 02815 */ 02816 void PrintErrorMsg (void) 02817 { 02818 Print_Msg("error in AT cmd",sizeof("error in AT cmd")); 02819 } 02820 02821 /** 02822 * @brief Print_Msg 02823 * Print messages on UART terminal 02824 * @param msgBuff : Contains data that need to be print 02825 * @param length : leghth of the data 02826 * @retval None 02827 */ 02828 void Print_Msg(char * msgBuff,uint8_t length) 02829 { 02830 02831 } 02832 02833 /** 02834 * @brief Error_Handler 02835 * This function is executed in case of error occurrence. 02836 * @param None 02837 * @retval None 02838 */ 02839 void Error_Handler(void) 02840 { 02841 /* Turn LED2 on */ 02842 //BSP_LED_On(LED2); 02843 //The following while(1) is commented as it prevents standby functionality 02844 /*while(1) 02845 { 02846 //Error if LED2 is slowly blinking (1 sec. period) 02847 BSP_LED_Toggle(LED2); 02848 HAL_Delay(1000); 02849 } */ 02850 Receive_Data(); 02851 } 02852 02853 02854 /** 02855 * @brief Start_DeepSleep_Timer 02856 * start the deep sleep timer. 02857 * @param None 02858 * @retval void 02859 */ 02860 void Start_DeepSleep_Timer(void) 02861 { 02862 status_flag.Deep_Sleep_Timer = WIFI_TRUE; 02863 sleep_count = 0; 02864 } 02865 02866 /** 02867 * @brief Stop_DeepSleep_Timer 02868 * stop the deep sleep timer. 02869 * @param None 02870 * @retval void 02871 */ 02872 void Stop_DeepSleep_Timer() 02873 { 02874 status_flag.Deep_Sleep_Timer = WIFI_FALSE; 02875 sleep_count = 0; 02876 } 02877 02878 #if 0 02879 /** 02880 * @brief configure_to_exti 02881 * Configured the USART Rx pin to EXTI pin to capture standby wakeup interrupt 02882 * @param None 02883 * @retval None 02884 */ 02885 void configure_to_exti() 02886 { 02887 /*configure the pin*/ 02888 02889 HAL_NVIC_DisableIRQ(USARTx_IRQn);//Disable UART IRQ 02890 02891 /* USART_RX Pin as EXTI IRQ*/ 02892 GPIO_InitTypeDef GPIO_InitStruct; 02893 GPIO_InitStruct.Pin = WiFi_USART_RX_PIN; 02894 GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING; 02895 GPIO_InitStruct.Pull = GPIO_NOPULL; 02896 GPIO_InitStruct.Speed = GPIO_SPEED_HIGH; 02897 #if defined (USE_STM32L0XX_NUCLEO) || (USE_STM32F4XX_NUCLEO) 02898 GPIO_InitStruct.Alternate = 0; 02899 #endif 02900 HAL_GPIO_Init(WiFi_USART_RX_GPIO_PORT, &GPIO_InitStruct); 02901 02902 /* Configure the NVIC for EXTI */ 02903 HAL_NVIC_SetPriority(USARTx_EXTI_IRQn, 3, 0); 02904 HAL_NVIC_EnableIRQ(USARTx_EXTI_IRQn); 02905 } 02906 #endif 02907 02908 /** 02909 * @brief WiFi_switch_to_command_mode 02910 * switch to command mode from data mode 02911 * @param None 02912 * @retval None 02913 */ 02914 void WiFi_switch_to_command_mode(void) 02915 { 02916 WiFi_Status_t status = WiFi_MODULE_SUCCESS; 02917 02918 /* AT+S.*/ 02919 Reset_AT_CMD_Buffer(); 02920 02921 sprintf((char*)WiFi_AT_Cmd_Buff,AT_DATA_TO_CMD_MODE); //Notice the lower case 02922 02923 status = USART_Transmit_AT_Cmd(strlen((char*)WiFi_AT_Cmd_Buff)); 02924 if(status == WiFi_MODULE_SUCCESS) 02925 { 02926 //nothing to do 02927 } 02928 } 02929 02930 02931 /** 02932 * @brief WiFi_switch_to_data_mode 02933 * switch to data mode from command mode 02934 * @param None 02935 * @retval None 02936 */ 02937 void WiFi_switch_to_data_mode(void) 02938 { 02939 WiFi_Status_t status = WiFi_MODULE_SUCCESS; 02940 02941 /* AT+S.*/ 02942 Reset_AT_CMD_Buffer(); 02943 02944 sprintf((char*)WiFi_AT_Cmd_Buff,AT_CMD_TO_DATA_MODE); //Notice the upper case 02945 02946 status = USART_Transmit_AT_Cmd(strlen((char*)WiFi_AT_Cmd_Buff)); 02947 02948 if(status == WiFi_MODULE_SUCCESS) 02949 { 02950 //nothing to do 02951 } 02952 02953 } 02954 02955 02956 /** 02957 * @brief Attention_Cmd 02958 * Attention command 02959 * @param None 02960 * @retval WiFi_Status_t : status of AT cmd Request 02961 */ 02962 WiFi_Status_t Attention_Cmd() 02963 { 02964 WiFi_Status_t status = WiFi_MODULE_SUCCESS; 02965 02966 Reset_AT_CMD_Buffer(); 02967 02968 /* AT : send AT command */ 02969 sprintf((char*)WiFi_AT_Cmd_Buff,AT_ATTENTION); 02970 02971 status = USART_Transmit_AT_Cmd(strlen((char*)WiFi_AT_Cmd_Buff)); 02972 if(status == WiFi_MODULE_SUCCESS) 02973 { 02974 status = USART_Receive_AT_Resp(Process_Event); 02975 } 02976 return status; 02977 } 02978 02979 02980 /** 02981 * @brief SET_Power_State 02982 * SET power mode of wifi module 02983 * @param state : power mode of wi-fi module i.e active,sleep,standby,powersave 02984 * @retval WiFi_Status_t : status of AT cmd Request 02985 */ 02986 WiFi_Status_t SET_Power_State(WiFi_Power_State_t state) 02987 { 02988 WiFi_Status_t status = WiFi_MODULE_SUCCESS; 02989 02990 #if DEBUG_PRINT 02991 printf("\r\n >>Soft Reset Wi-Fi module\r\n"); 02992 #endif 02993 02994 Reset_AT_CMD_Buffer(); 02995 02996 /* AT : send AT command */ 02997 sprintf((char*)WiFi_AT_Cmd_Buff,AT_SET_POWER_STATE,state); 02998 // WiFi_Module_State = Process_Event; 02999 // WiFi_WIND_State.WiFiReset = WIFI_FALSE; 03000 WiFi_WIND_State.WiFiHWStarted = WIFI_FALSE; 03001 status = USART_Transmit_AT_Cmd(strlen((char*)WiFi_AT_Cmd_Buff)); 03002 if(status != WiFi_MODULE_SUCCESS) 03003 return status; 03004 memset((void*)&WiFi_WIND_State,0x00,sizeof(WiFi_WIND_State)); /*reset the WIND State?*/ 03005 /* AT+CFUN=1 //Soft reset */ 03006 while(WiFi_WIND_State.WiFiHWStarted != WIFI_TRUE) 03007 { 03008 asm("NOP"); 03009 } 03010 return status; 03011 } 03012 03013 03014 /** 03015 * @brief Display_Help_Text 03016 * this function will print a list of all commands supported with a brief help text for each cmd 03017 * @param None 03018 * @retval WiFi_Status_t : status of AT cmd Request 03019 */ 03020 WiFi_Status_t Display_Help_Text() 03021 { 03022 WiFi_Status_t status = WiFi_MODULE_SUCCESS; 03023 Reset_AT_CMD_Buffer(); 03024 03025 /* AT : send AT command */ 03026 sprintf((char*)WiFi_AT_Cmd_Buff,AT_HELP_TEXT); 03027 03028 status = USART_Transmit_AT_Cmd(strlen((char*)WiFi_AT_Cmd_Buff)); 03029 if(status == WiFi_MODULE_SUCCESS) 03030 { 03031 status = USART_Receive_AT_Resp(Process_Event); 03032 } 03033 return status; 03034 } 03035 03036 /** 03037 * @brief WiFi_Get_IP_address 03038 * Get the ip address 03039 * @param ip_addr : pointer to ip address 03040 * @retval status : status of AT cmd request 03041 */ 03042 03043 WiFi_Status_t WiFi_Get_IP_Address(uint8_t *ip_addr) 03044 { 03045 WiFi_Status_t status = WiFi_MODULE_SUCCESS; 03046 int cfg_value_length, i; 03047 03048 /* AT : send AT command */ 03049 sprintf((char*)WiFi_AT_Cmd_Buff,AT_GET_STATUS_VALUE,"ip_ipaddr"); 03050 status = USART_Transmit_AT_Cmd(strlen((char*)WiFi_AT_Cmd_Buff)); 03051 if(status == WiFi_MODULE_SUCCESS) 03052 { 03053 status = USART_Receive_AT_Resp(Process_Event); 03054 cfg_value_length = strlen((const char*)get_cfg_value); 03055 03056 //Optimise the following loop?? 03057 for(i=7; i<=16; i++)//0th index is always ' ' which we ignore 03058 if(get_cfg_value[i] == '\r')//find the first '\r' 03059 { 03060 get_cfg_value[i] = '\0'; //Terminate the string by NULL 03061 break; 03062 } 03063 03064 /* copy user pointer to get_cfg_value */ 03065 memcpy(ip_addr,&get_cfg_value[1],i);//IP address will have max lenght fixed at 16 bytes (null terminated included). 03066 memset(get_cfg_value, 0x00,cfg_value_length); 03067 } 03068 return status; 03069 } 03070 03071 /** 03072 * @brief WiFi_Get_MAC_address 03073 * Get the MAC address 03074 * @param ip_addr : pointer to MAC address 03075 * @retval status : status of AT cmd request 03076 */ 03077 03078 WiFi_Status_t WiFi_Get_MAC_Address(uint8_t *mac_addr) 03079 { 03080 WiFi_Status_t status = WiFi_MODULE_SUCCESS; 03081 int cfg_value_length; 03082 03083 /* AT : send AT command */ 03084 sprintf((char*)WiFi_AT_Cmd_Buff,AT_GET_CONFIGURATION_VALUE,"nv_wifi_macaddr"); 03085 status = USART_Transmit_AT_Cmd(strlen((char*)WiFi_AT_Cmd_Buff)); 03086 if(status == WiFi_MODULE_SUCCESS) 03087 { 03088 status = USART_Receive_AT_Resp(Process_Event); 03089 cfg_value_length = strlen((const char*)get_cfg_value); 03090 get_cfg_value[18] = '\0';//Terminate by NULL 03091 /* copy user pointer to get_cfg_value */ 03092 memcpy(mac_addr,&get_cfg_value[1],18);//IP address will have max lenght fixed at 18 bytes. 03093 memset(get_cfg_value, 0x00,cfg_value_length); 03094 } 03095 return status; 03096 } 03097 03098 /** 03099 * @brief GET_Configuration_Value 03100 * Get a wifi configuration value from the module 03101 * @param sVar_name : Name of the config variable 03102 * aValue : value of config variable to be returned to user 03103 * @retval WiFi_Status_t : status of AT cmd Request 03104 */ 03105 WiFi_Status_t GET_Configuration_Value(char* sVar_name,uint32_t *aValue) 03106 { 03107 int cfg_value_length; 03108 WiFi_Status_t status = WiFi_MODULE_SUCCESS; 03109 03110 Reset_AT_CMD_Buffer(); 03111 03112 /* AT : send AT command */ 03113 sprintf((char*)WiFi_AT_Cmd_Buff,AT_GET_CONFIGURATION_VALUE,sVar_name); 03114 03115 status = USART_Transmit_AT_Cmd(strlen((char*)WiFi_AT_Cmd_Buff)); 03116 if(status == WiFi_MODULE_SUCCESS) 03117 { 03118 status = USART_Receive_AT_Resp(Process_Event); 03119 cfg_value_length = strlen((const char*)get_cfg_value); 03120 memcpy(aValue,get_cfg_value,cfg_value_length); //copy user pointer to get_cfg_value 03121 memset(get_cfg_value, 0x00,cfg_value_length); 03122 } 03123 return status; 03124 } 03125 03126 /** 03127 * @brief SET_Configuration_Addr 03128 * Get a wifi configuration address from the module 03129 * @param sVar_name : Name of the config variable 03130 * addr : value of config address to be returned to user 03131 * @retval WiFi_Status_t : status of AT cmd Request 03132 */ 03133 WiFi_Status_t SET_Configuration_Addr(char* sVar_name,char* addr) 03134 { 03135 WiFi_Status_t status = WiFi_MODULE_SUCCESS; 03136 03137 Reset_AT_CMD_Buffer(); 03138 03139 /* AT : send AT command */ 03140 sprintf((char*)WiFi_AT_Cmd_Buff,AT_SET_CONFIGURATION_ADDRESS,sVar_name,addr); 03141 03142 status = USART_Transmit_AT_Cmd(strlen((char*)WiFi_AT_Cmd_Buff)); 03143 if(status == WiFi_MODULE_SUCCESS) 03144 { 03145 status = USART_Receive_AT_Resp(Process_Event); 03146 } 03147 return status; 03148 } 03149 03150 /** 03151 * @brief SET_Configuration_Value 03152 * SET the value of configuration variable 03153 * @param sVar_name : Name of the config variable 03154 * aValue : value of config variable 03155 * @retval WiFi_Status_t : status of AT cmd Request 03156 */ 03157 WiFi_Status_t SET_Configuration_Value(char* sVar_name,uint32_t aValue) 03158 { 03159 WiFi_Status_t status = WiFi_MODULE_SUCCESS; 03160 03161 Reset_AT_CMD_Buffer(); 03162 03163 /* AT : send AT command */ 03164 sprintf((char*)WiFi_AT_Cmd_Buff,AT_SET_CONFIGURATION_VALUE,sVar_name,(int)aValue); 03165 03166 status = USART_Transmit_AT_Cmd(strlen((char*)WiFi_AT_Cmd_Buff)); 03167 if(status == WiFi_MODULE_SUCCESS) 03168 { 03169 status = USART_Receive_AT_Resp(Process_Event); 03170 } 03171 return status; 03172 } 03173 03174 /** 03175 * @brief SET_SSID 03176 * SET SSID in flash memory of Wi-Fi module 03177 * @param ssid : pointer of SSID 03178 * @retval WiFi_Status_t : status of AT cmd Request 03179 */ 03180 WiFi_Status_t SET_SSID(char* ssid) 03181 { 03182 WiFi_Status_t status = WiFi_MODULE_SUCCESS; 03183 03184 Reset_AT_CMD_Buffer(); 03185 03186 /* AT+S.SSIDTXT=abcd <ExampleSSID> */ 03187 sprintf((char*)WiFi_AT_Cmd_Buff,AT_SET_SSID,ssid); 03188 03189 status = USART_Transmit_AT_Cmd(strlen((char*)WiFi_AT_Cmd_Buff)); 03190 if(status == WiFi_MODULE_SUCCESS) 03191 { 03192 status = USART_Receive_AT_Resp(Process_Event); 03193 } 03194 return status; 03195 } 03196 03197 03198 /** 03199 * @brief SET_WiFi_SecKey 03200 * SET wifi security key 03201 * @param seckey : pointer of security key 03202 * @retval WiFi_Status_t : status of AT cmd Request 03203 */ 03204 WiFi_Status_t SET_WiFi_SecKey(char* seckey) 03205 { 03206 WiFi_Status_t status = WiFi_MODULE_SUCCESS; 03207 03208 Reset_AT_CMD_Buffer(); 03209 03210 /* AT+S.SCFG=wifi_wpa_psk_text,helloworld : set password */ 03211 sprintf((char*)WiFi_AT_Cmd_Buff,AT_SET_SEC_KEY,seckey); 03212 03213 status = USART_Transmit_AT_Cmd(strlen((char*)WiFi_AT_Cmd_Buff)); 03214 if(status == WiFi_MODULE_SUCCESS) 03215 { 03216 status = USART_Receive_AT_Resp(Process_Event); 03217 } 03218 return status; 03219 } 03220 03221 03222 /** 03223 * @brief Restore_Default_Setting 03224 * Restore the factory default values of the configuration variables 03225 * and writes them to non volatile storage 03226 * @param None 03227 * @retval WiFi_Status_t : status of AT cmd Request 03228 */ 03229 WiFi_Status_t Restore_Default_Setting() 03230 { 03231 WiFi_Status_t status = WiFi_MODULE_SUCCESS; 03232 03233 //Reset_AT_CMD_Buffer(); 03234 03235 /* AT&F: restore default setting */ 03236 sprintf((char*)WiFi_AT_Cmd_Buff,AT_RESTORE_DEFAULT_SETTING); 03237 03238 status = USART_Transmit_AT_Cmd(strlen((char*)WiFi_AT_Cmd_Buff)); 03239 03240 if(status == WiFi_MODULE_SUCCESS) 03241 { 03242 status = USART_Receive_AT_Resp(Process_Event); 03243 } 03244 return status; 03245 03246 } 03247 03248 /** 03249 * @brief Save_Current_Setting 03250 * Store the current RAM-based setting to non-volatile storage 03251 * @param None 03252 * @retval WiFi_Status_t : status of AT cmd Request 03253 */ 03254 WiFi_Status_t Save_Current_Setting() 03255 { 03256 WiFi_Status_t status = WiFi_MODULE_SUCCESS; 03257 03258 Reset_AT_CMD_Buffer(); 03259 03260 /* AT&W :Save the settings on the flash memory */ 03261 sprintf((char*)WiFi_AT_Cmd_Buff,AT_SAVE_CURRENT_SETTING); 03262 03263 status = USART_Transmit_AT_Cmd(strlen((char*)WiFi_AT_Cmd_Buff)); 03264 if(status == WiFi_MODULE_SUCCESS) 03265 { 03266 status = USART_Receive_AT_Resp(Process_Event); 03267 } 03268 return status; 03269 } 03270 03271 03272 /** 03273 * @brief ResetBuffer 03274 * Reset receive data/indication msg buffer 03275 * @param None 03276 * @retval None 03277 */ 03278 void ResetBuffer() 03279 { 03280 03281 } 03282 03283 03284 /** 03285 * @brief config_init_value 03286 * initalize config values before reset 03287 * @param sVar_name : Name of the config variable 03288 * aValue : value of config variable 03289 * @retval None 03290 */ 03291 WiFi_Status_t config_init_value(char* sVar_name,uint8_t aValue) 03292 { 03293 WiFi_Status_t status = WiFi_MODULE_SUCCESS; 03294 Reset_AT_CMD_Buffer(); 03295 sprintf((char*)WiFi_AT_Cmd_Buff,AT_SET_CONFIGURATION_VALUE,sVar_name,aValue); 03296 /*if(HAL_UART_Transmit(&UartWiFiHandle, (uint8_t *)WiFi_AT_Cmd_Buff, strlen((char*)WiFi_AT_Cmd_Buff),1000)!= HAL_OK) 03297 { 03298 Error_Handler(); 03299 return WiFi_HAL_UART_ERROR; 03300 }*/ 03301 03302 status = WaitForResponse(AT_RESP_LEN_OK); 03303 return status; 03304 } 03305 03306 /** 03307 * @brief config_init_addr 03308 * initalize config strings/addresses before reset 03309 * @param sVar_name : Name of the config variable 03310 * addr : value of config address to be returned to user 03311 * @retval None 03312 */ 03313 WiFi_Status_t config_init_addr(char* sVar_name,char* addr) 03314 { 03315 WiFi_Status_t status = WiFi_MODULE_SUCCESS; 03316 Reset_AT_CMD_Buffer(); 03317 sprintf((char*)WiFi_AT_Cmd_Buff,AT_SET_CONFIGURATION_ADDRESS,sVar_name,addr); 03318 /*if(HAL_UART_Transmit(&UartWiFiHandle, (uint8_t *)WiFi_AT_Cmd_Buff, strlen((char*)WiFi_AT_Cmd_Buff),1000)!= HAL_OK) 03319 { 03320 Error_Handler(); 03321 return WiFi_HAL_UART_ERROR; 03322 }*/ 03323 03324 status = WaitForResponse(AT_RESP_LEN_OK); 03325 return status; 03326 03327 } 03328 03329 03330 /** 03331 * @brief WaitForResponse 03332 * Wait for OK response 03333 * @param alength length of the data to be received 03334 * @retval None 03335 */ 03336 WiFi_Status_t WaitForResponse(uint16_t alength) 03337 { 03338 WiFi_Status_t status = WiFi_MODULE_SUCCESS; 03339 03340 if(alength <= RxBufferSize) 03341 { 03342 /*if(HAL_UART_Receive(&UartWiFiHandle, (uint8_t *)USART_RxBuffer, alength,5000)!= HAL_OK) 03343 { 03344 Error_Handler(); 03345 return WiFi_HAL_UART_ERROR; 03346 }*/ 03347 03348 if(((strstr((const char *)&USART_RxBuffer,"OK"))) == NULL) 03349 { 03350 return WiFi_AT_CMD_RESP_ERROR; 03351 } 03352 03353 } 03354 03355 return status; 03356 } 03357 /**** Wi-Fi indication call back *************/ 03358 __weak void ind_wifi_warning(WiFi_Status_t warning_code) 03359 { 03360 } 03361 03362 __weak void ind_wifi_error(WiFi_Status_t error_code) 03363 { 03364 } 03365 03366 __weak void ind_wifi_connection_error(WiFi_Status_t status_code) 03367 { 03368 } 03369 03370 __weak void ind_wifi_connected(void) 03371 { 03372 } 03373 03374 __weak void ind_wifi_ap_ready(void) 03375 { 03376 } 03377 03378 __weak void ind_wifi_ap_client_joined(uint8_t * client_mac_address) 03379 { 03380 } 03381 03382 __weak void ind_wifi_ap_client_left(uint8_t * client_mac_address) 03383 { 03384 } 03385 03386 __weak void ind_wifi_on(void) 03387 { 03388 } 03389 03390 __weak void ind_wifi_packet_lost(WiFi_Status_t status_code) 03391 { 03392 } 03393 03394 __weak void ind_wifi_gpio_changed(void) 03395 { 03396 } 03397 03398 __weak void ind_wifi_socket_data_received(uint8_t socket_id, uint8_t * data_ptr, uint32_t message_size, uint32_t chunk_size) 03399 { 03400 } 03401 03402 __weak void ind_wifi_socket_client_remote_server_closed(uint8_t * socketID) 03403 { 03404 } 03405 03406 __weak void ind_wifi_socket_server_data_lost(void) 03407 { 03408 } 03409 03410 __weak void ind_socket_server_client_joined(void) 03411 { 03412 } 03413 03414 __weak void ind_socket_server_client_left(void) 03415 { 03416 } 03417 03418 __weak void ind_wifi_http_data_available(uint8_t * data_ptr,uint32_t message_size) 03419 { 03420 } 03421 03422 __weak void ind_wifi_file_data_available(uint8_t * data_ptr) 03423 { 03424 } 03425 __weak void ind_wifi_resuming(void) 03426 { 03427 } 03428 03429 03430 03431 /** 03432 * @} 03433 */ 03434 03435 /** 03436 * @} 03437 */ 03438 03439 03440 /** 03441 * @} 03442 */ 03443 03444 /******************* (C) COPYRIGHT 2015 STMicroelectronics *****END OF FILE****/ 03445
Generated on Tue Jul 12 2022 16:20:36 by
1.7.2

X-NUCLEO-IDW01M1 Wi-Fi expansion board