44 * Created on: Apr 28, 2024
55 * Author: karl
66 */
7-
8- #include "wifi_provisioning.h"
9-
7+ #include <stdlib.h>
108#include <string.h>
119
10+ #include "driver/uart.h"
1211#include "esp_err.h"
1312#include "esp_log.h"
1413#include "esp_wifi.h"
1514#include "freertos/FreeRTOS.h"
1615#include "freertos/event_groups.h"
16+ #include "freertos/projdefs.h"
1717#include "freertos/task.h"
1818#include "improv_wrapper.h"
19+ #include "network_interface.h"
1920#include "wifi_interface.h"
2021
22+ #include "wifi_provisioning.h"
23+
24+ #define TAG "IMPROV"
25+
2126#ifdef CONFIG_ESP_CONSOLE_UART_DEFAULT
2227#include "driver/uart.h"
2328#endif
@@ -60,11 +65,8 @@ void uart_event_handler(void) {
6065 events than other types of events. If we take too much time on data event,
6166 the queue might be full. */
6267 case UART_DATA :
63- ESP_LOGD (TAG , "[UART DATA]: %d bytes" , event .size );
64-
68+ //ESP_LOGD(TAG, "uart data: %d bytes", event.size);
6569 uart_read_bytes (uart_num , dtmp , event .size , portMAX_DELAY );
66- // ESP_LOGD(TAG, "[DATA EVT]:");
67-
6870 improv_wifi_handle_serial (dtmp , event .size );
6971 break ;
7072 // Event of HW FIFO overflow detected
@@ -87,6 +89,8 @@ void uart_event_handler(void) {
8789 uart_flush_input (uart_num );
8890 xQueueReset (uart_queue );
8991 break ;
92+ case UART_BREAK :
93+ break ;
9094 // Others
9195 default :
9296 ESP_LOGD (TAG , "uart event type: %d" , event .type );
@@ -138,7 +142,7 @@ void improv_wifi_scan(unsigned char *scanResponse, int bufLen,
138142 wifi_ap_record_t ap_info [16 ];
139143
140144 memset (ap_info , 0 , sizeof (ap_info ));
141-
145+ ESP_LOGD ( TAG , "Starting WiFi scan for improv" );
142146 if (esp_wifi_scan_start (NULL , true) == ESP_ERR_WIFI_STATE ) {
143147 wifi_ap_record_t ap_info_tmp ;
144148
@@ -231,18 +235,31 @@ bool improv_wifi_is_connected(void) {
231235void improv_wifi_get_local_ip (uint8_t * address ) {
232236 esp_netif_ip_info_t ip_info ;
233237
234- // TODO: find a better way to do this
235- do {
236- esp_netif_get_ip_info (get_current_netif (), & ip_info );
237- vTaskDelay (pdMS_TO_TICKS (200 ));
238- } while (ip_info .ip .addr == 0 );
238+ /* Wait a short time for an IP to become available. Improv frequently asks
239+ for the local IP during onboarding; when called too early we should
240+ wait briefly rather than returning 0.0.0.0 immediately. */
241+ const TickType_t timeout = pdMS_TO_TICKS (2000 ); /* 2s */
242+ TickType_t start = xTaskGetTickCount ();
243+
244+ while (network_if_get_ip (& ip_info ) == false) {
245+ if ((xTaskGetTickCount () - start ) > timeout ) {
246+ ESP_LOGW (TAG , "%s: no valid IP available after timeout" , __func__ );
247+ /* return all-zero address (caller should handle this) */
248+ address [0 ] = 0 ;
249+ address [1 ] = 0 ;
250+ address [2 ] = 0 ;
251+ address [3 ] = 0 ;
252+ return ;
253+ }
254+ vTaskDelay (pdMS_TO_TICKS (100 ));
255+ }
239256
240- address [0 ] = ip_info .ip .addr >> 0 ;
241- address [1 ] = ip_info .ip .addr >> 8 ;
242- address [2 ] = ip_info .ip .addr >> 16 ;
243- address [3 ] = ip_info .ip .addr >> 24 ;
257+ address [0 ] = ( uint8_t )( ip_info .ip .addr & 0xFF ) ;
258+ address [1 ] = ( uint8_t )(( ip_info .ip .addr >> 8 ) & 0xFF ) ;
259+ address [2 ] = ( uint8_t )(( ip_info .ip .addr >> 16 ) & 0xFF ) ;
260+ address [3 ] = ( uint8_t )(( ip_info .ip .addr >> 24 ) & 0xFF ) ;
244261
245- ESP_LOGD (TAG , "%d.%d.%d.%d" , address [0 ], address [1 ], address [2 ], address [3 ]);
262+ ESP_LOGI (TAG , "%d.%d.%d.%d" , address [0 ], address [1 ], address [2 ], address [3 ]);
246263}
247264
248265void improv_init (void ) {
@@ -286,7 +303,7 @@ void improv_init(void) {
286303 uart_buffer_size , 10 , & uart_queue , 0 );
287304
288305 if (ret != ESP_OK ) {
289- ESP_LOGE (TAG , "Failed to install UART driver: %s" , esp_err_to_name (ret ));
306+ ESP_LOGE (TAG , "Failed to install UART driver: %s" , esp_err_to_name (ret ));
290307 } else {
291308 BaseType_t task_ret = xTaskCreatePinnedToCore (& improv_uart_task , "improv_uart" , 8 * 1024 , NULL , 4 ,
292309 & t_improv_uart_task , tskNO_AFFINITY );
@@ -306,7 +323,6 @@ void improv_init(void) {
306323 esp_err_t usb_ret = usb_serial_jtag_driver_install (& usb_serial_config );
307324 if (usb_ret == ESP_OK ) {
308325 ESP_LOGD (TAG , "USB Serial JTAG driver installed successfully" );
309-
310326 // Create USB Serial task
311327 BaseType_t usb_task_ret = xTaskCreatePinnedToCore (& usb_serial_improv_task , "usb_improv" ,
312328 8 * 1024 , NULL , 4 , & t_usb_serial_task , tskNO_AFFINITY );
0 commit comments