network changes

This commit is contained in:
2026-03-28 12:48:41 -04:00
parent 1973b3a0ff
commit a321ce46e2
5 changed files with 74 additions and 65 deletions
+52 -51
View File
@@ -1,5 +1,6 @@
#include <stdio.h> #include <stdio.h>
#include "esp_event.h"
#include "esp_log.h" #include "esp_log.h"
#include "esp_pm.h" #include "esp_pm.h"
#include "esp_system.h" #include "esp_system.h"
@@ -9,70 +10,70 @@
#include "nvs_flash.h" #include "nvs_flash.h"
#include "sdkconfig.h" #include "sdkconfig.h"
#include "soc/gpio_num.h" #include "soc/gpio_num.h"
#include "esp_event.h"
#include "types.hpp"
#include "led.hpp" #include "led.hpp"
#include "network.hpp" #include "network.hpp"
#include "types.hpp"
static const char *TAG = "ClientMain"; static const char *TAG = "ClientMain";
extern "C" void app_main() 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) // Initialize NVS (required for some Wi-Fi configurations and network
esp_err_t ret = nvs_flash_init(); // features)
if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) { esp_err_t ret = nvs_flash_init();
ESP_ERROR_CHECK(nvs_flash_erase()); if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND)
ret = nvs_flash_init(); {
} ESP_ERROR_CHECK(nvs_flash_erase());
ESP_ERROR_CHECK(ret); 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(); setup_led();
ESP_LOGI(TAG, "LED setup complete");
vTaskDelay(pdMS_TO_TICKS(1000));
ESP_LOGI(TAG, "LED blink 4"); // Connect to WiFi ESP_LOGI(TAG, "Initializing WiFi connection");
led_blink_number(4, 255,255,255); initialize_network();
vTaskDelay(pdMS_TO_TICKS(1000));
ESP_LOGI(TAG, "LED blink 2"); esp_err_t err = connect_wifi(CONFIG_CALENDINK_WIFI_SSID,
led_blink_number(2, 255,255,255); CONFIG_CALENDINK_WIFI_PASSWORD, false);
turn_off_led(); if (err == ESP_OK)
ESP_LOGI(TAG, "LED blink complete"); {
vTaskDelay(pdMS_TO_TICKS(1000)); uint8_t retries = 1;
do
// 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)
{ {
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();
} }
+3
View File
@@ -5,6 +5,9 @@
}, },
{ {
"path": "../components" "path": "../components"
},
{
"path": "../Client"
} }
], ],
"settings": { "settings": {
+9 -5
View File
@@ -16,9 +16,9 @@
#include "soc/gpio_num.h" #include "soc/gpio_num.h"
// Project headers // Project headers
#include "types.hpp"
#include "network.hpp"
#include "led.hpp" #include "led.hpp"
#include "network.hpp"
#include "types.hpp"
// Project cpp (Unity Build entry) // Project cpp (Unity Build entry)
// clang-format off // clang-format off
@@ -186,6 +186,8 @@ extern "C" void app_main()
setup_led(); setup_led();
initialize_network();
set_led_status(led_status::ConnectingEthernet); set_led_status(led_status::ConnectingEthernet);
g_Ethernet_Initialized = true; g_Ethernet_Initialized = true;
esp_err_t result = connect_ethernet(kBlockUntilEthernetEstablished); esp_err_t result = connect_ethernet(kBlockUntilEthernetEstablished);
@@ -346,9 +348,10 @@ extern "C" void app_main()
{ {
char *saveptr; char *saveptr;
char *line = strtok_r(ptr, "\n", &saveptr); char *line = strtok_r(ptr, "\n", &saveptr);
while (line != nullptr) { while (line != nullptr)
ESP_LOGI(kTagMain, "%s", line); {
line = strtok_r(nullptr, "\n", &saveptr); ESP_LOGI(kTagMain, "%s", line);
line = strtok_r(nullptr, "\n", &saveptr);
} }
free(ptr); free(ptr);
} }
@@ -382,4 +385,5 @@ shutdown:
ESP_ERROR_CHECK(esp_event_loop_delete_default()); ESP_ERROR_CHECK(esp_event_loop_delete_default());
ESP_ERROR_CHECK(nvs_flash_deinit()); ESP_ERROR_CHECK(nvs_flash_deinit());
shutdown_network();
} }
+5 -8
View File
@@ -19,8 +19,8 @@
#include "types.hpp" #include "types.hpp"
// Project includes // Project includes
#include "network.hpp"
#include "led.hpp" #include "led.hpp"
#include "network.hpp"
// Forward declarations // Forward declarations
#if CONFIG_CALENDINK_BLINK_IP #if CONFIG_CALENDINK_BLINK_IP
@@ -47,6 +47,9 @@ internal volatile bool s_eth_link_up = false;
internal bool s_ethernet_connected = false; internal bool s_ethernet_connected = false;
#endif #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, void ethernet_event_handler(void *arg, esp_event_base_t event_base,
int32_t event_id, void *event_data) int32_t event_id, void *event_data)
{ {
@@ -114,8 +117,6 @@ void teardown_ethernet()
s_eth_netif = nullptr; s_eth_netif = nullptr;
s_eth_handles = nullptr; s_eth_handles = nullptr;
s_eth_count = 0; s_eth_count = 0;
esp_netif_deinit();
} }
esp_err_t connect_ethernet(bool blockUntilIPAcquired) esp_err_t connect_ethernet(bool blockUntilIPAcquired)
@@ -132,10 +133,6 @@ esp_err_t connect_ethernet(bool blockUntilIPAcquired)
{ {
return ESP_ERR_NO_MEM; 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_ERROR_CHECK(ethernet_init_all(&s_eth_handles, &s_eth_count));
esp_netif_inherent_config_t esp_netif_config = 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, esp_err_t connect_wifi(const char *ssid, const char *password,
bool blockUntilIPAcquired) bool blockUntilIPAcquired)
{ {
#ifndef NDEBUG #ifndef NDEBUG
assert(!s_wifi_connected && "WiFi connect called but already connected!"); assert(!s_wifi_connected && "WiFi connect called but already connected!");
+5 -1
View File
@@ -4,10 +4,14 @@
#include "esp_err.h" #include "esp_err.h"
void initialize_network();
void shutdown_network();
esp_err_t connect_ethernet(bool blockUntilIPAcquired); esp_err_t connect_ethernet(bool blockUntilIPAcquired);
void disconnect_ethernet(); void disconnect_ethernet();
esp_err_t check_ethernet_connection(uint32_t timeoutSeconds); 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(); void disconnect_wifi();
esp_err_t check_wifi_connection(uint32_t timeoutSeconds); esp_err_t check_wifi_connection(uint32_t timeoutSeconds);