From a321ce46e21f11c22993fc2f32ff9b1688e4c136 Mon Sep 17 00:00:00 2001 From: Patedam Date: Sat, 28 Mar 2026 12:48:41 -0400 Subject: [PATCH] network changes --- Client/main/main.cpp | 103 ++++++++++++++++--------------- Provider/Provider.code-workspace | 3 + Provider/main/main.cpp | 14 +++-- components/network/network.cpp | 13 ++-- components/network/network.hpp | 6 +- 5 files changed, 74 insertions(+), 65 deletions(-) diff --git a/Client/main/main.cpp b/Client/main/main.cpp index aba0206..9ebed98 100644 --- a/Client/main/main.cpp +++ b/Client/main/main.cpp @@ -1,5 +1,6 @@ #include +#include "esp_event.h" #include "esp_log.h" #include "esp_pm.h" #include "esp_system.h" @@ -9,70 +10,70 @@ #include "nvs_flash.h" #include "sdkconfig.h" #include "soc/gpio_num.h" -#include "esp_event.h" -#include "types.hpp" #include "led.hpp" #include "network.hpp" +#include "types.hpp" static const char *TAG = "ClientMain"; extern "C" void app_main() { - ESP_LOGI(TAG, "Hello, Calendink Client!"); + ESP_LOGI(TAG, "Hello, Calendink Client!"); - // Initialize NVS (required for some Wi-Fi configurations and network features) - esp_err_t ret = nvs_flash_init(); - if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) { - ESP_ERROR_CHECK(nvs_flash_erase()); - ret = nvs_flash_init(); - } - ESP_ERROR_CHECK(ret); + // Initialize NVS (required for some Wi-Fi configurations and network + // features) + esp_err_t ret = nvs_flash_init(); + if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) + { + ESP_ERROR_CHECK(nvs_flash_erase()); + ret = nvs_flash_init(); + } + ESP_ERROR_CHECK(ret); - ESP_ERROR_CHECK(esp_event_loop_create_default()); + ESP_ERROR_CHECK(esp_event_loop_create_default()); - setup_led(); - ESP_LOGI(TAG, "LED setup complete"); - vTaskDelay(pdMS_TO_TICKS(1000)); + setup_led(); - ESP_LOGI(TAG, "LED blink 4"); - led_blink_number(4, 255,255,255); - vTaskDelay(pdMS_TO_TICKS(1000)); + // Connect to WiFi ESP_LOGI(TAG, "Initializing WiFi connection"); + initialize_network(); - ESP_LOGI(TAG, "LED blink 2"); - led_blink_number(2, 255,255,255); + esp_err_t err = connect_wifi(CONFIG_CALENDINK_WIFI_SSID, + CONFIG_CALENDINK_WIFI_PASSWORD, false); - turn_off_led(); - ESP_LOGI(TAG, "LED blink complete"); - vTaskDelay(pdMS_TO_TICKS(1000)); - - // Connect to WiFi - // ESP_LOGI(TAG, "Initializing WiFi connection"); - - // // Attempting to connect, we don't necessarily need to block forever here - // // as network.cpp already handles some of the retries - // esp_err_t err = connect_wifi(CONFIG_CALENDINK_WIFI_SSID, CONFIG_CALENDINK_WIFI_PASSWORD, false); - - // if (err == ESP_OK) { - // uint8_t retries = 1; - // do { - // err = check_wifi_connection(retries); - // if (err != ESP_OK) { - // ESP_LOGW(TAG, "WiFi connection check timeout, retrying... (%d)", retries); - // vTaskDelay(pdMS_TO_TICKS(1000)); - // } - // retries++; - // } while (err == ESP_ERR_TIMEOUT && retries <= CONFIG_CALENDINK_WIFI_RETRIES); - // } - - // if (err == ESP_OK) { - // ESP_LOGI(TAG, "Successfully connected to WiFi!"); - // } else { - // ESP_LOGE(TAG, "Failed to connect to WiFi."); - // } - - while (true) + if (err == ESP_OK) + { + uint8_t retries = 1; + do { - vTaskDelay(pdMS_TO_TICKS(1000)); - } + err = check_wifi_connection(retries); + if (err != ESP_OK) + { + ESP_LOGW(TAG, "WiFi connection check timeout, retrying... (%d)", + retries); + + led_blink_number(3, 255, 0, 0); + } + retries++; + } while (err == ESP_ERR_TIMEOUT && + retries <= CONFIG_CALENDINK_WIFI_RETRIES); + } + + if (err == ESP_OK) + { + ESP_LOGI(TAG, "Successfully connected to WiFi!"); + } + else + { + ESP_LOGE(TAG, "Failed to connect to WiFi."); + } + + turn_off_led(); + + while (true) + { + vTaskDelay(pdMS_TO_TICKS(1000)); + } + + shutdown_network(); } diff --git a/Provider/Provider.code-workspace b/Provider/Provider.code-workspace index fdbfd33..76ae18c 100644 --- a/Provider/Provider.code-workspace +++ b/Provider/Provider.code-workspace @@ -5,6 +5,9 @@ }, { "path": "../components" + }, + { + "path": "../Client" } ], "settings": { diff --git a/Provider/main/main.cpp b/Provider/main/main.cpp index 7632a32..028dd0c 100644 --- a/Provider/main/main.cpp +++ b/Provider/main/main.cpp @@ -16,9 +16,9 @@ #include "soc/gpio_num.h" // Project headers -#include "types.hpp" -#include "network.hpp" #include "led.hpp" +#include "network.hpp" +#include "types.hpp" // Project cpp (Unity Build entry) // clang-format off @@ -186,6 +186,8 @@ extern "C" void app_main() setup_led(); + initialize_network(); + set_led_status(led_status::ConnectingEthernet); g_Ethernet_Initialized = true; esp_err_t result = connect_ethernet(kBlockUntilEthernetEstablished); @@ -346,9 +348,10 @@ extern "C" void app_main() { char *saveptr; char *line = strtok_r(ptr, "\n", &saveptr); - while (line != nullptr) { - ESP_LOGI(kTagMain, "%s", line); - line = strtok_r(nullptr, "\n", &saveptr); + while (line != nullptr) + { + ESP_LOGI(kTagMain, "%s", line); + line = strtok_r(nullptr, "\n", &saveptr); } free(ptr); } @@ -382,4 +385,5 @@ shutdown: ESP_ERROR_CHECK(esp_event_loop_delete_default()); ESP_ERROR_CHECK(nvs_flash_deinit()); + shutdown_network(); } diff --git a/components/network/network.cpp b/components/network/network.cpp index bf7afb7..6ec9d42 100644 --- a/components/network/network.cpp +++ b/components/network/network.cpp @@ -19,8 +19,8 @@ #include "types.hpp" // Project includes -#include "network.hpp" #include "led.hpp" +#include "network.hpp" // Forward declarations #if CONFIG_CALENDINK_BLINK_IP @@ -47,6 +47,9 @@ internal volatile bool s_eth_link_up = false; internal bool s_ethernet_connected = false; #endif +void initialize_network() { ESP_ERROR_CHECK(esp_netif_init()); } +void shutdown_network() { ESP_ERROR_CHECK(esp_netif_deinit()); } + void ethernet_event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data) { @@ -114,8 +117,6 @@ void teardown_ethernet() s_eth_netif = nullptr; s_eth_handles = nullptr; s_eth_count = 0; - - esp_netif_deinit(); } esp_err_t connect_ethernet(bool blockUntilIPAcquired) @@ -132,10 +133,6 @@ esp_err_t connect_ethernet(bool blockUntilIPAcquired) { return ESP_ERR_NO_MEM; } - // Connection is split in two steps. First we open the connection and ask for - // an ip. Second a semaphor will block until the ip is acquired. If we dont - // block then the user have to verify the semaphore before continuing. - ESP_ERROR_CHECK(esp_netif_init()); ESP_ERROR_CHECK(ethernet_init_all(&s_eth_handles, &s_eth_count)); esp_netif_inherent_config_t esp_netif_config = @@ -305,7 +302,7 @@ void teardown_wifi() } esp_err_t connect_wifi(const char *ssid, const char *password, - bool blockUntilIPAcquired) + bool blockUntilIPAcquired) { #ifndef NDEBUG assert(!s_wifi_connected && "WiFi connect called but already connected!"); diff --git a/components/network/network.hpp b/components/network/network.hpp index 371f197..d9d306c 100644 --- a/components/network/network.hpp +++ b/components/network/network.hpp @@ -4,10 +4,14 @@ #include "esp_err.h" +void initialize_network(); +void shutdown_network(); + esp_err_t connect_ethernet(bool blockUntilIPAcquired); void disconnect_ethernet(); esp_err_t check_ethernet_connection(uint32_t timeoutSeconds); -esp_err_t connect_wifi(const char *ssid, const char *password, bool blockUntilIPAcquired); +esp_err_t connect_wifi(const char *ssid, const char *password, + bool blockUntilIPAcquired); void disconnect_wifi(); esp_err_t check_wifi_connection(uint32_t timeoutSeconds);