Clang format changes for more readability

This commit is contained in:
2026-03-03 14:11:55 -05:00
parent cdbabe0e58
commit cfe7332899
8 changed files with 554 additions and 117 deletions

View File

@@ -8,10 +8,11 @@
#include "esp_eth_driver.h"
#include "esp_event.h"
#include "esp_log.h"
#include "esp_netif.h"
#include "esp_system.h"
#include "esp_wifi.h"
#include "ethernet_init.h"
#include "freertos/FreeRTOS.h"
#include "freertos/semphr.h"
#include "freertos/timers.h"
// Project includes
#include "types.hpp"
@@ -41,10 +42,13 @@ internal bool s_ethernet_connected = false;
#endif
void ethernet_event_handler(void *arg, esp_event_base_t event_base,
int32_t event_id, void *event_data) {
if (event_base == IP_EVENT && event_id == IP_EVENT_ETH_GOT_IP) {
int32_t event_id, void *event_data)
{
if (event_base == IP_EVENT && event_id == IP_EVENT_ETH_GOT_IP)
{
ip_event_got_ip_t *event = (ip_event_got_ip_t *)event_data;
if (event->esp_netif != s_eth_netif) {
if (event->esp_netif != s_eth_netif)
{
return;
}
ESP_LOGI(kLogEthernet, "Got IPv4 event: Interface \"%s\" address: " IPSTR,
@@ -52,31 +56,39 @@ void ethernet_event_handler(void *arg, esp_event_base_t event_base,
s_current_ip_info = event->ip_info;
xSemaphoreGive(s_semph_get_ip_addrs);
} else if (event_base == ETH_EVENT && event_id == ETHERNET_EVENT_CONNECTED) {
}
else if (event_base == ETH_EVENT && event_id == ETHERNET_EVENT_CONNECTED)
{
ESP_LOGI(kLogEthernet, "Ethernet Link Up");
s_eth_link_up = true;
if (s_semph_eth_link) {
if (s_semph_eth_link)
{
xSemaphoreGive(s_semph_eth_link);
}
} else if (event_base == ETH_EVENT &&
event_id == ETHERNET_EVENT_DISCONNECTED) {
}
else if (event_base == ETH_EVENT && event_id == ETHERNET_EVENT_DISCONNECTED)
{
ESP_LOGI(kLogEthernet, "Ethernet Link Down");
s_eth_link_up = false;
}
}
void teardown_ethernet() {
if (s_semph_eth_link != nullptr) {
void teardown_ethernet()
{
if (s_semph_eth_link != nullptr)
{
vSemaphoreDelete(s_semph_eth_link);
s_semph_eth_link = nullptr;
}
if (s_semph_get_ip_addrs != nullptr) {
if (s_semph_get_ip_addrs != nullptr)
{
vSemaphoreDelete(s_semph_get_ip_addrs);
s_semph_get_ip_addrs = nullptr;
}
if (s_eth_netif) {
if (s_eth_netif)
{
ESP_ERROR_CHECK(esp_event_handler_unregister(IP_EVENT, IP_EVENT_ETH_GOT_IP,
&ethernet_event_handler));
@@ -96,7 +108,8 @@ void teardown_ethernet() {
esp_netif_deinit();
}
internal esp_err_t connect_ethernet(bool blockUntilIPAcquired) {
internal esp_err_t connect_ethernet(bool blockUntilIPAcquired)
{
#ifndef NDEBUG
assert(!s_ethernet_connected &&
"Ethernet connect called but already connected!");
@@ -105,7 +118,8 @@ internal esp_err_t connect_ethernet(bool blockUntilIPAcquired) {
s_semph_get_ip_addrs = xSemaphoreCreateBinary();
s_semph_eth_link = xSemaphoreCreateBinary();
if (s_semph_get_ip_addrs == NULL || s_semph_eth_link == NULL) {
if (s_semph_get_ip_addrs == NULL || s_semph_eth_link == NULL)
{
return ESP_ERR_NO_MEM;
}
// Connection is split in two steps. First we open the connection and ask for
@@ -139,7 +153,8 @@ internal esp_err_t connect_ethernet(bool blockUntilIPAcquired) {
// Will teardown connection properly on shutdown
ESP_ERROR_CHECK(esp_register_shutdown_handler(&teardown_ethernet));
if (blockUntilIPAcquired) {
if (blockUntilIPAcquired)
{
ESP_LOGI(kLogEthernet, "Waiting for IP address.");
xSemaphoreTake(s_semph_get_ip_addrs, portMAX_DELAY);
blink_last_ip_octet();
@@ -148,7 +163,8 @@ internal esp_err_t connect_ethernet(bool blockUntilIPAcquired) {
return ESP_OK;
}
void disconnect_ethernet() {
void disconnect_ethernet()
{
#ifndef NDEBUG
assert(s_ethernet_connected &&
"Ethernet disconnect called but not connected!");
@@ -159,10 +175,13 @@ void disconnect_ethernet() {
ESP_ERROR_CHECK(esp_unregister_shutdown_handler(&teardown_ethernet));
}
internal esp_err_t check_ethernet_connection(uint32_t timeoutSeconds) {
internal esp_err_t check_ethernet_connection(uint32_t timeoutSeconds)
{
// Wait up to 5000ms for the physical link to negotiate
if (!s_eth_link_up) {
if (!xSemaphoreTake(s_semph_eth_link, pdMS_TO_TICKS(5000))) {
if (!s_eth_link_up)
{
if (!xSemaphoreTake(s_semph_eth_link, pdMS_TO_TICKS(5000)))
{
ESP_LOGE(kLogEthernet,
"No physical Ethernet link detected. Skipping DHCP wait.");
return ESP_ERR_INVALID_STATE; // Special error to skip retries
@@ -172,10 +191,13 @@ internal esp_err_t check_ethernet_connection(uint32_t timeoutSeconds) {
ESP_LOGI(kLogEthernet, "Waiting for IP address for %d seconds.",
timeoutSeconds);
if (xSemaphoreTake(s_semph_get_ip_addrs,
pdMS_TO_TICKS(timeoutSeconds * 1000))) {
pdMS_TO_TICKS(timeoutSeconds * 1000)))
{
blink_last_ip_octet();
return ESP_OK;
} else {
}
else
{
return ESP_ERR_TIMEOUT;
}
}
@@ -187,25 +209,41 @@ internal esp_netif_t *s_wifi_netif = nullptr;
internal SemaphoreHandle_t s_semph_get_wifi_ip_addrs = nullptr;
internal SemaphoreHandle_t s_semph_wifi_link = nullptr;
internal volatile bool s_wifi_link_up = false;
internal TimerHandle_t s_wifi_reconnect_timer = nullptr;
#ifndef NDEBUG
internal bool s_wifi_connected = false;
#endif
// Forward declaration for timer callback
internal void wifi_reconnect_timer_cb(TimerHandle_t xTimer);
void wifi_event_handler(void *arg, esp_event_base_t event_base,
int32_t event_id, void *event_data) {
if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_CONNECTED) {
int32_t event_id, void *event_data)
{
if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_CONNECTED)
{
ESP_LOGI(kLogWifi, "WiFi associated with AP.");
s_wifi_link_up = true;
if (s_semph_wifi_link) {
if (s_semph_wifi_link)
{
xSemaphoreGive(s_semph_wifi_link);
}
} else if (event_base == WIFI_EVENT &&
event_id == WIFI_EVENT_STA_DISCONNECTED) {
ESP_LOGI(kLogWifi, "WiFi disconnected.");
}
else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED)
{
ESP_LOGI(kLogWifi, "WiFi disconnected. Scheduling reconnect...");
s_wifi_link_up = false;
} else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP) {
if (s_wifi_reconnect_timer != nullptr)
{
xTimerStart(s_wifi_reconnect_timer, 0);
}
}
else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP)
{
ip_event_got_ip_t *event = (ip_event_got_ip_t *)event_data;
if (event->esp_netif != s_wifi_netif) {
if (event->esp_netif != s_wifi_netif)
{
return;
}
ESP_LOGI(kLogWifi, "Got IPv4 event: Interface \"%s\" address: " IPSTR,
@@ -216,13 +254,29 @@ void wifi_event_handler(void *arg, esp_event_base_t event_base,
}
}
void teardown_wifi() {
if (s_semph_wifi_link != nullptr) {
internal void wifi_reconnect_timer_cb(TimerHandle_t xTimer)
{
ESP_LOGI(kLogWifi, "Timer expired. Executing esp_wifi_connect()...");
esp_wifi_connect();
}
void teardown_wifi()
{
if (s_wifi_reconnect_timer != nullptr)
{
xTimerStop(s_wifi_reconnect_timer, 0);
xTimerDelete(s_wifi_reconnect_timer, 0);
s_wifi_reconnect_timer = nullptr;
}
if (s_semph_wifi_link != nullptr)
{
vSemaphoreDelete(s_semph_wifi_link);
s_semph_wifi_link = nullptr;
}
if (s_semph_get_wifi_ip_addrs != nullptr) {
if (s_semph_get_wifi_ip_addrs != nullptr)
{
vSemaphoreDelete(s_semph_get_wifi_ip_addrs);
s_semph_get_wifi_ip_addrs = nullptr;
}
@@ -234,7 +288,8 @@ void teardown_wifi() {
esp_event_handler_unregister(IP_EVENT, IP_EVENT_STA_GOT_IP,
&wifi_event_handler);
if (s_wifi_netif) {
if (s_wifi_netif)
{
esp_wifi_disconnect();
esp_wifi_stop();
esp_wifi_deinit();
@@ -244,7 +299,8 @@ void teardown_wifi() {
}
internal 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!");
s_wifi_connected = true;
@@ -252,10 +308,16 @@ internal esp_err_t connect_wifi(const char *ssid, const char *password,
s_semph_get_wifi_ip_addrs = xSemaphoreCreateBinary();
s_semph_wifi_link = xSemaphoreCreateBinary();
if (s_semph_get_wifi_ip_addrs == NULL || s_semph_wifi_link == NULL) {
if (s_semph_get_wifi_ip_addrs == NULL || s_semph_wifi_link == NULL)
{
return ESP_ERR_NO_MEM;
}
// Create a 5-second timer to avoid spamming connect requests
s_wifi_reconnect_timer =
xTimerCreate("wifi_recon", pdMS_TO_TICKS(5000), pdFALSE, (void *)0,
wifi_reconnect_timer_cb);
// esp_netif_init() is already called by Ethernet, but safe to call multiple
// times or we can assume it's initialized.
s_wifi_netif = esp_netif_create_default_wifi_sta();
@@ -285,7 +347,8 @@ internal esp_err_t connect_wifi(const char *ssid, const char *password,
ESP_ERROR_CHECK(esp_register_shutdown_handler(&teardown_wifi));
if (blockUntilIPAcquired) {
if (blockUntilIPAcquired)
{
ESP_LOGI(kLogWifi, "Waiting for IP address.");
xSemaphoreTake(s_semph_get_wifi_ip_addrs, portMAX_DELAY);
blink_last_ip_octet();
@@ -294,7 +357,8 @@ internal esp_err_t connect_wifi(const char *ssid, const char *password,
return ESP_OK;
}
void disconnect_wifi() {
void disconnect_wifi()
{
#ifndef NDEBUG
assert(s_wifi_connected && "WiFi disconnect called but not connected!");
s_wifi_connected = false;
@@ -304,36 +368,57 @@ void disconnect_wifi() {
ESP_ERROR_CHECK(esp_unregister_shutdown_handler(&teardown_wifi));
}
internal esp_err_t check_wifi_connection(uint32_t timeoutSeconds) {
internal esp_err_t check_wifi_connection(uint32_t timeoutSeconds)
{
// Wait up to 10000ms for the physical link to associate with the AP
if (!s_wifi_link_up) {
if (!xSemaphoreTake(s_semph_wifi_link, pdMS_TO_TICKS(10000))) {
ESP_LOGE(kLogWifi,
"Failed to associate with WiFi AP. Skipping DHCP wait.");
return ESP_ERR_INVALID_STATE; // Special error to skip retries
if (!s_wifi_link_up)
{
// If the timer isn't already running, kickstart a connection attempt now
if (s_wifi_reconnect_timer != nullptr &&
xTimerIsTimerActive(s_wifi_reconnect_timer) == pdFALSE)
{
ESP_LOGI(kLogWifi,
"Physical link is down. Requesting immediate AP association...");
esp_err_t err = esp_wifi_connect();
if (err != ESP_OK && err != ESP_ERR_WIFI_CONN)
{
ESP_LOGE(kLogWifi, "esp_wifi_connect failed: %s", esp_err_to_name(err));
}
}
if (!xSemaphoreTake(s_semph_wifi_link, pdMS_TO_TICKS(10000)))
{
ESP_LOGE(kLogWifi, "Failed to associate with WiFi AP.");
return ESP_ERR_TIMEOUT; // Return timeout so main.cpp triggers a retry
}
}
ESP_LOGI(kLogWifi, "Waiting for IP address for %d seconds.", timeoutSeconds);
if (xSemaphoreTake(s_semph_get_wifi_ip_addrs,
pdMS_TO_TICKS(timeoutSeconds * 1000))) {
pdMS_TO_TICKS(timeoutSeconds * 1000)))
{
blink_last_ip_octet();
return ESP_OK;
} else {
}
else
{
return ESP_ERR_TIMEOUT;
}
}
internal esp_err_t get_ip_info(esp_netif_ip_info_t *ip_info) {
internal esp_err_t get_ip_info(esp_netif_ip_info_t *ip_info)
{
*ip_info = s_current_ip_info;
return ESP_OK;
}
internal void led_blink_number(int n, uint8_t r, uint8_t g, uint8_t b);
internal void blink_last_ip_octet() {
internal void blink_last_ip_octet()
{
esp_netif_ip_info_t ip_info;
if (get_ip_info(&ip_info) == ESP_OK) {
if (get_ip_info(&ip_info) == ESP_OK)
{
uint8_t last_octet = (ip_info.ip.addr >> 24) & 0xFF;
printf("IP Address: " IPSTR "\n", IP2STR(&ip_info.ip));
@@ -342,10 +427,12 @@ internal void blink_last_ip_octet() {
int t = (last_octet / 10) % 10;
int u = last_octet % 10;
if (h > 0) {
if (h > 0)
{
led_blink_number(h, 255, 255, 255);
}
if (h > 0 || t > 0) {
if (h > 0 || t > 0)
{
led_blink_number(t, 255, 255, 255);
}
led_blink_number(u, 255, 255, 255);