Asked claude opus to make an audit of code and use coding guidelines etc. This is the fix

This commit is contained in:
2026-03-09 22:26:16 -04:00
parent 75d88f441c
commit b702839f8e
15 changed files with 459 additions and 1291 deletions

View File

@@ -298,9 +298,11 @@ internal httpd_handle_t start_webserver(void)
httpd_register_uri_handler(server, &api_tasks_update_uri);
httpd_register_uri_handler(server, &api_tasks_delete_uri);
// Populate dummy data for development
// Populate dummy data for development (debug builds only)
#ifndef NDEBUG
seed_users();
seed_tasks();
#endif
#ifdef CONFIG_CALENDINK_DEPLOY_WEB_PAGES
// Register static file handler last as a catch-all wildcard if deployed

View File

@@ -26,6 +26,8 @@
#include "mdns_service.cpp"
// clang-format on
internal const char *kTagMain = "MAIN";
// Global Application State Definitions
bool g_Ethernet_Initialized = false;
bool g_Wifi_Initialized = false;
@@ -35,9 +37,8 @@ constexpr bool kBlockUntilEthernetEstablished = false;
extern "C" void app_main()
{
printf("Hello, Calendink OTA! [V1.1]\n");
printf("PSRAM size: %d bytes\n", esp_psram_get_size());
ESP_LOGI(kTagMain, "Hello, Calendink OTA! [V0.1.1]");
ESP_LOGI(kTagMain, "PSRAM size: %d bytes", esp_psram_get_size());
httpd_handle_t web_server = NULL;
@@ -56,22 +57,22 @@ extern "C" void app_main()
err = nvs_get_u8(my_handle, "www_part", &g_Active_WWW_Partition);
if (err == ESP_OK)
{
printf("NVS: Found active www partition: %d\n", g_Active_WWW_Partition);
ESP_LOGI(kTagMain, "NVS: Found active www partition: %d",
g_Active_WWW_Partition);
}
if (err == ESP_ERR_NVS_NOT_FOUND)
{
// First boot (no NVS key yet): default to www_0
// This ensures that after a fresh USB flash (which only writes www_0),
// we start from the correct partition.
printf("No www_part in NVS, defaulting to 0.\n");
ESP_LOGI(kTagMain, "No www_part in NVS, defaulting to 0.");
g_Active_WWW_Partition = 0;
nvs_set_u8(my_handle, "www_part", 0);
nvs_commit(my_handle);
}
else if (err != ESP_OK)
{
printf("Error reading www_part from NVS: %s\n", esp_err_to_name(err));
ESP_LOGE(kTagMain, "Error reading www_part from NVS: %s",
esp_err_to_name(err));
g_Active_WWW_Partition = 0;
}
@@ -83,7 +84,7 @@ extern "C" void app_main()
}
else
{
printf("Error opening NVS handle!\n");
ESP_LOGE(kTagMain, "Error opening NVS handle!");
}
// Detect if this is the first boot after a new flash (Firmware or Frontend)
@@ -98,8 +99,8 @@ extern "C" void app_main()
ESP_OK ||
strcmp(last_time, current_time) != 0)
{
printf("New firmware detected! (Last: %s, Current: %s)\n",
last_time[0] ? last_time : "None", current_time);
ESP_LOGI(kTagMain, "New firmware detected! (Last: %s, Current: %s)",
last_time[0] ? last_time : "None", current_time);
is_new_flash = true;
nvs_set_str(my_handle, "last_fw_time", current_time);
}
@@ -118,7 +119,7 @@ extern "C" void app_main()
ESP_OK ||
memcmp(last_sha, current_sha, 32) != 0)
{
printf("New frontend partition detected via SHA256!\n");
ESP_LOGI(kTagMain, "New frontend partition detected via SHA256!");
is_new_flash = true;
nvs_set_blob(my_handle, "www0_sha", current_sha, 32);
}
@@ -141,8 +142,9 @@ extern "C" void app_main()
{
if (is_new_flash && g_Active_WWW_Partition != 0)
{
printf("FACTORY APP + NEW FLASH: Overriding www_part to 0 (was %d)\n",
g_Active_WWW_Partition);
ESP_LOGW(kTagMain,
"FACTORY APP + NEW FLASH: Overriding www_part to 0 (was %d)",
g_Active_WWW_Partition);
g_Active_WWW_Partition = 0;
// Persist the override
@@ -181,7 +183,7 @@ extern "C" void app_main()
if (result == ESP_ERR_INVALID_STATE)
{
printf("Ethernet cable not plugged in, skipping retries.\n");
ESP_LOGW(kTagMain, "Ethernet cable not plugged in, skipping retries.");
break;
}
@@ -198,7 +200,7 @@ extern "C" void app_main()
if (result != ESP_OK)
{
printf("Ethernet failed, trying wifi\n");
ESP_LOGW(kTagMain, "Ethernet failed, trying wifi");
disconnect_ethernet();
g_Ethernet_Initialized = false;
@@ -235,20 +237,20 @@ extern "C" void app_main()
if (result != ESP_OK)
{
printf("Wifi failed.\n");
ESP_LOGE(kTagMain, "Wifi failed.");
goto shutdown;
}
set_led_status(led_status::ReadyWifi);
printf("Will use Wifi!\n");
ESP_LOGI(kTagMain, "Will use Wifi!");
}
else
{
set_led_status(led_status::ReadyEthernet);
printf("Will use Ethernet!\n");
ESP_LOGI(kTagMain, "Will use Ethernet!");
}
printf("Connected! IP acquired.\n");
ESP_LOGI(kTagMain, "Connected! IP acquired.");
// Start the webserver
web_server = start_webserver();
@@ -274,7 +276,7 @@ extern "C" void app_main()
}
shutdown:
printf("Shutting down.\n");
ESP_LOGE(kTagMain, "Shutting down.");
if (web_server)
{

View File

@@ -2,7 +2,9 @@
#include "mdns.h"
#include "sdkconfig.h"
static const char *kLogMDNS = "MDNS";
#include "types.hpp"
internal const char *kTagMDNS = "MDNS";
void start_mdns()
{
@@ -10,14 +12,14 @@ void start_mdns()
esp_err_t err = mdns_init();
if (err != ESP_OK)
{
ESP_LOGE(kLogMDNS, "mDNS Init failed: %d", err);
ESP_LOGE(kTagMDNS, "mDNS Init failed: %d", err);
return;
}
// Set mDNS hostname (from Kconfig)
const char *hostname = CONFIG_CALENDINK_MDNS_HOSTNAME;
mdns_hostname_set(hostname);
ESP_LOGI(kLogMDNS, "mDNS Hostname set to: [%s.local]", hostname);
ESP_LOGI(kTagMDNS, "mDNS Hostname set to: [%s.local]", hostname);
// Set mDNS instance name
mdns_instance_name_set("Calendink Provider");
@@ -26,9 +28,8 @@ void start_mdns()
err = mdns_service_add(NULL, "_http", "_tcp", 80, NULL, 0);
if (err != ESP_OK)
{
ESP_LOGE(kLogMDNS, "mDNS Service add failed: %d", err);
ESP_LOGE(kTagMDNS, "mDNS Service add failed: %d", err);
}
printf("MDNS: Service initialized with hostname [%s.local]\n", hostname);
ESP_LOGI(kLogMDNS, "mDNS Service initialized");
ESP_LOGI(kTagMDNS, "mDNS Service initialized with hostname [%s.local]", hostname);
}