Skip to content

Commit 585ac60

Browse files
author
Andriy Malyshenko
committed
Implemented Improv deinit when Ip is received. Fixed IP discovery for Improv (was returning 0.0.0.0 all the times)
1 parent b67f930 commit 585ac60

2 files changed

Lines changed: 59 additions & 20 deletions

File tree

components/improv_wifi/wifi_provisioning.c

Lines changed: 36 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,25 @@
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) {
231235
void 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

248265
void 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);

components/network_interface/wifi_interface.c

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
#if ENABLE_WIFI_PROVISIONING
2424
#include "wifi_provisioning.h"
25+
#include "freertos/task.h"
2526
#endif
2627

2728
static const char *TAG = "WIFI_IF";
@@ -61,6 +62,20 @@ static void event_handler(void *arg, esp_event_base_t event_base, int event_id,
6162
}
6263
}
6364

65+
#if ENABLE_WIFI_PROVISIONING
66+
/*
67+
* Short-lived task to deinitialize Improv provisioning after a delay.
68+
* Defined at file scope because C does not support nested functions.
69+
*/
70+
static void improv_deinit_task(void *pv) {
71+
(void)pv;
72+
vTaskDelay(pdMS_TO_TICKS(500));
73+
ESP_LOGI(TAG, "Deinitiating improv provisioning");
74+
improv_deinit();
75+
vTaskDelete(NULL);
76+
}
77+
#endif
78+
6479
/** Event handler for IP_EVENT_ETH_GOT_IP */
6580
static void got_ip_event_handler(void *arg, esp_event_base_t event_base,
6681
int32_t event_id, void *event_data) {
@@ -85,6 +100,14 @@ static void got_ip_event_handler(void *arg, esp_event_base_t event_base,
85100
ESP_LOGI(TAG, "~~~~~~~~~~~");
86101

87102
s_retry_num = 0;
103+
104+
#if ENABLE_WIFI_PROVISIONING
105+
BaseType_t r = xTaskCreate(improv_deinit_task, "improv_deinit", 2048,
106+
NULL, tskIDLE_PRIORITY + 1, NULL);
107+
if (r != pdPASS) {
108+
ESP_LOGW(TAG, "failed to create improv_deinit task");
109+
}
110+
#endif
88111
}
89112

90113
static void lost_ip_event_handler(void *arg, esp_event_base_t event_base,

0 commit comments

Comments
 (0)