Can wait for network to finish boot

This commit is contained in:
Pierre HUBERT 2024-08-17 17:40:14 +02:00
parent f60f6f6ccc
commit 59ba55793e
3 changed files with 39 additions and 3 deletions

View File

@ -14,6 +14,8 @@
static const char *TAG = "ethernet"; static const char *TAG = "ethernet";
static bool network_ready = false;
/** Event handler for Ethernet events */ /** Event handler for Ethernet events */
static void eth_event_handler(void *arg, esp_event_base_t event_base, static void eth_event_handler(void *arg, esp_event_base_t event_base,
int32_t event_id, void *event_data) int32_t event_id, void *event_data)
@ -32,12 +34,14 @@ static void eth_event_handler(void *arg, esp_event_base_t event_base,
break; break;
case ETHERNET_EVENT_DISCONNECTED: case ETHERNET_EVENT_DISCONNECTED:
ESP_LOGI(TAG, "Ethernet Link Down"); ESP_LOGI(TAG, "Ethernet Link Down");
network_ready = false;
break; break;
case ETHERNET_EVENT_START: case ETHERNET_EVENT_START:
ESP_LOGI(TAG, "Ethernet Started"); ESP_LOGI(TAG, "Ethernet Started");
break; break;
case ETHERNET_EVENT_STOP: case ETHERNET_EVENT_STOP:
ESP_LOGI(TAG, "Ethernet Stopped"); ESP_LOGI(TAG, "Ethernet Stopped");
network_ready = false;
break; break;
default: default:
break; break;
@ -57,6 +61,8 @@ static void got_ip_event_handler(void *arg, esp_event_base_t event_base,
ESP_LOGI(TAG, "ETHMASK:" IPSTR, IP2STR(&ip_info->netmask)); ESP_LOGI(TAG, "ETHMASK:" IPSTR, IP2STR(&ip_info->netmask));
ESP_LOGI(TAG, "ETHGW:" IPSTR, IP2STR(&ip_info->gw)); ESP_LOGI(TAG, "ETHGW:" IPSTR, IP2STR(&ip_info->gw));
ESP_LOGI(TAG, "~~~~~~~~~~~"); ESP_LOGI(TAG, "~~~~~~~~~~~");
network_ready = true;
} }
void ethernet_init() void ethernet_init()
@ -126,4 +132,26 @@ void ethernet_init()
// Register user defined event handers // Register user defined event handers
ESP_ERROR_CHECK(esp_event_handler_register(ETH_EVENT, ESP_EVENT_ANY_ID, &eth_event_handler, NULL)); ESP_ERROR_CHECK(esp_event_handler_register(ETH_EVENT, ESP_EVENT_ANY_ID, &eth_event_handler, NULL));
ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_ETH_GOT_IP, &got_ip_event_handler, NULL)); ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_ETH_GOT_IP, &got_ip_event_handler, NULL));
}
bool ethernet_is_ready()
{
return network_ready;
}
void ethernet_wait_for_network()
{
for (size_t i = 0; i < 1000 && !network_ready; i++)
{
ESP_LOGI(TAG, "Wait for network %d / 1000", i);
system_sleep(1);
}
if (!network_ready)
{
ESP_LOGE(TAG, "Failed to acquire network, will reboot!");
reboot();
}
ESP_LOGI(TAG, "Network is now available");
} }

View File

@ -4,6 +4,8 @@
#pragma once #pragma once
#include "stdbool.h"
#ifdef __cplusplus #ifdef __cplusplus
extern "C" extern "C"
{ {
@ -15,9 +17,14 @@ extern "C"
void ethernet_init(); void ethernet_init();
/** /**
* De-initialize Ethernet connection * Check if Ethernet connection is ready or not
*/ */
void ethernet_deinit(); bool ethernet_is_ready();
/**
* Wait for network connection to become ready
*/
void ethernet_wait_for_network();
#ifdef __cplusplus #ifdef __cplusplus
} }

View File

@ -45,13 +45,14 @@ void app_main(void)
ESP_LOGI(TAG, "Initialize network\n"); ESP_LOGI(TAG, "Initialize network\n");
ethernet_init(); ethernet_init();
ethernet_wait_for_network();
ESP_LOGI(TAG, "Check secure origin\n"); ESP_LOGI(TAG, "Check secure origin\n");
char *sec_orig = unsecure_api_get_secure_origin(); char *sec_orig = unsecure_api_get_secure_origin();
assert(sec_orig != NULL); assert(sec_orig != NULL);
printf("Res = %s\n", sec_orig); printf("Res = %s\n", sec_orig);
system_sleep(20); system_sleep(120);
reboot(); reboot();
} }