287 lines
12 KiB
C++
287 lines
12 KiB
C++
/**
|
|
* @file inet_mon_main.cpp
|
|
* @brief Internet Monitor — Main State Machine
|
|
*
|
|
* State machine sequence:
|
|
* 1. SPLASH — Show splash screen for 2 seconds
|
|
* 2. LINK_CHECK — Wait for Ethernet link up (30s timeout)
|
|
* 3. DHCP_WAIT — Wait for DHCP IPv4 address (30s timeout)
|
|
* 4. RUN_TESTS — Execute diagnostic tests with live progress display
|
|
* 5. SHOW_STATUS — Display results for 60 seconds
|
|
* 6. On link drop or any test failure: show FAILURE screen for 30s
|
|
* 7. Loop back to LINK_CHECK
|
|
*/
|
|
|
|
#include <stdio.h>
|
|
#include <string.h>
|
|
#include <inttypes.h>
|
|
#include "sdkconfig.h"
|
|
#include "freertos/FreeRTOS.h"
|
|
#include "freertos/task.h"
|
|
#include "esp_chip_info.h"
|
|
#include "esp_flash.h"
|
|
#include "esp_system.h"
|
|
#include "esp_log.h"
|
|
#include "nvs_flash.h"
|
|
#include "driver/gpio.h"
|
|
|
|
#include "display.h"
|
|
#include "network.h"
|
|
#include "diagnostics.h"
|
|
#include "temperature.h"
|
|
|
|
static const char *TAG = "INETMON";
|
|
|
|
// ─── Animation ──────────────────────────────────────────────────────────────
|
|
|
|
static int s_anim_frame = 0;
|
|
|
|
// ─── Helper: wait with display update ───────────────────────────────────────
|
|
|
|
// ─── Boot wait helpers ─────────────────────────────────────────────────────
|
|
|
|
/// Wait up to `timeout_sec` seconds for Ethernet link, showing booting screen.
|
|
/// Returns true once link is up.
|
|
static bool wait_for_link(int timeout_sec)
|
|
{
|
|
display_booting(false, false, false); // initial draw
|
|
for (int i = 0; i < timeout_sec; i++) {
|
|
if (network_is_link_up()) return true;
|
|
vTaskDelay(pdMS_TO_TICKS(1000));
|
|
}
|
|
return network_is_link_up();
|
|
}
|
|
|
|
/// Wait up to `timeout_sec` seconds for DHCP IP, showing booting screen.
|
|
/// link_done must be true (link must already be up).
|
|
/// Returns true once DHCP gives us an IPv4 address.
|
|
static bool wait_for_dhcp(int timeout_sec)
|
|
{
|
|
display_booting(true, false, false); // initial draw
|
|
for (int i = 0; i < timeout_sec; i++) {
|
|
if (network_has_ipv4()) return true;
|
|
vTaskDelay(pdMS_TO_TICKS(1000));
|
|
}
|
|
return network_has_ipv4();
|
|
}
|
|
|
|
/// Wait up to `timeout_sec` seconds for a global IPv6 address (GUA).
|
|
/// Shows the booting screen with [X] link, [X] dhcp, and [ ]/[X] slaac.
|
|
/// Returns true once a GUA is assigned, false on timeout.
|
|
static bool wait_for_slaac(int timeout_sec)
|
|
{
|
|
// If already assigned, skip the boot screen entirely
|
|
if (network_has_ipv6_gua()) return true;
|
|
|
|
display_booting(true, true, false); // initial draw ([ ] SLAAC)
|
|
int timeout_ms = timeout_sec * 1000;
|
|
int elapsed = 0;
|
|
while (elapsed < timeout_ms) {
|
|
if (network_has_ipv6_gua()) {
|
|
display_booting(true, true, true);
|
|
vTaskDelay(pdMS_TO_TICKS(250)); // let user see the green [X]
|
|
return true;
|
|
}
|
|
vTaskDelay(pdMS_TO_TICKS(100));
|
|
elapsed += 100;
|
|
}
|
|
bool got = network_has_ipv6_gua();
|
|
display_booting(true, true, got);
|
|
return got;
|
|
}
|
|
|
|
// ─── app_main ───────────────────────────────────────────────────────────────
|
|
|
|
extern "C" void app_main(void)
|
|
{
|
|
printf("Hello world! Internet Monitor v0.1\n");
|
|
|
|
// ── 1. Print chip info ──
|
|
esp_chip_info_t chip_info;
|
|
uint32_t flash_size;
|
|
esp_chip_info(&chip_info);
|
|
printf("This is %s chip with %d CPU core(s), %s%s%s%s, ",
|
|
CONFIG_IDF_TARGET, chip_info.cores,
|
|
(chip_info.features & CHIP_FEATURE_WIFI_BGN) ? "WiFi/" : "",
|
|
(chip_info.features & CHIP_FEATURE_BT) ? "BT" : "",
|
|
(chip_info.features & CHIP_FEATURE_BLE) ? "BLE" : "",
|
|
(chip_info.features & CHIP_FEATURE_IEEE802154) ? ", 802.15.4 (Zigbee/Thread)" : "");
|
|
|
|
unsigned major_rev = chip_info.revision / 100;
|
|
unsigned minor_rev = chip_info.revision % 100;
|
|
printf("silicon revision v%d.%d, ", major_rev, minor_rev);
|
|
|
|
if (esp_flash_get_size(NULL, &flash_size) != ESP_OK) {
|
|
printf("Get flash size failed");
|
|
return;
|
|
}
|
|
printf("%" PRIu32 "MB %s flash\n", flash_size / (uint32_t)(1024 * 1024),
|
|
(chip_info.features & CHIP_FEATURE_EMB_FLASH) ? "embedded" : "external");
|
|
printf("Minimum free heap size: %" PRIu32 " bytes\n", esp_get_minimum_free_heap_size());
|
|
fflush(stdout);
|
|
|
|
// ── 2. Initialize system services ──
|
|
ESP_ERROR_CHECK(nvs_flash_init());
|
|
ESP_ERROR_CHECK(esp_netif_init());
|
|
ESP_ERROR_CHECK(esp_event_loop_create_default());
|
|
ESP_ERROR_CHECK(gpio_install_isr_service(0));
|
|
|
|
// ── 3. Initialize SPI bus for W5500 (SPI2_HOST) ──
|
|
network_spi_init();
|
|
|
|
// ── 4. Initialize display (SPI3_HOST, independent) ──
|
|
display_init();
|
|
|
|
// ── 5. Show splash screen ──
|
|
display_splash("INET MON");
|
|
vTaskDelay(pdMS_TO_TICKS(2000));
|
|
|
|
// ── 6. Initialize Ethernet ──
|
|
network_register_handlers();
|
|
esp_eth_handle_t eth = network_eth_init();
|
|
network_netif_init(eth);
|
|
network_eth_start(eth);
|
|
|
|
// ── 7. Initialize temperature sensor (best-effort) ──
|
|
temperature_init();
|
|
|
|
// ── 8. Main test loop ──
|
|
struct test_results_t results;
|
|
memset(&results, 0, sizeof(results));
|
|
results.tests_total = DIAG_TEST_COUNT;
|
|
|
|
while (true) {
|
|
// ═══════════════════════════════════════════════════════════
|
|
// STATE: LINK_CHECK
|
|
// ═══════════════════════════════════════════════════════════
|
|
ESP_LOGI(TAG, "State: LINK_CHECK");
|
|
|
|
if (!network_is_link_up()) {
|
|
ESP_LOGI(TAG, "Waiting for Ethernet link...");
|
|
bool link_up = wait_for_link(30);
|
|
|
|
if (!link_up) {
|
|
ESP_LOGW(TAG, "Link check timeout");
|
|
results.link_up = false;
|
|
display_failure(&results);
|
|
vTaskDelay(pdMS_TO_TICKS(30000));
|
|
continue; // retry link
|
|
}
|
|
}
|
|
|
|
// Start IPv6 SLAAC now that link is up (not automatic for Ethernet in IDF v6)
|
|
network_start_ipv6();
|
|
|
|
// ═══════════════════════════════════════════════════════════
|
|
// STATE: DHCP_WAIT
|
|
// ═══════════════════════════════════════════════════════════
|
|
ESP_LOGI(TAG, "State: DHCP_WAIT");
|
|
|
|
if (!network_has_ipv4()) {
|
|
ESP_LOGI(TAG, "Waiting for DHCP...");
|
|
bool got_ip = wait_for_dhcp(30);
|
|
|
|
if (!got_ip) {
|
|
ESP_LOGW(TAG, "DHCP timeout");
|
|
results.link_up = network_is_link_up();
|
|
results.has_ipv4 = false;
|
|
results.ipv4 = "--";
|
|
display_failure(&results);
|
|
vTaskDelay(pdMS_TO_TICKS(30000));
|
|
|
|
// If link dropped, go back to link check
|
|
if (!network_is_link_up()) continue;
|
|
// Otherwise retry DHCP
|
|
continue;
|
|
}
|
|
}
|
|
|
|
// Both link and DHCP done — wait for IPv6 GUA via SLAAC
|
|
// Shows [X] link, [X] dhcp, [ ] slaac while waiting
|
|
wait_for_slaac(20);
|
|
|
|
// ═══════════════════════════════════════════════════════════
|
|
// STATE: RUN_TESTS
|
|
// ═══════════════════════════════════════════════════════════
|
|
ESP_LOGI(TAG, "State: RUN_TESTS");
|
|
|
|
// Reset results for new test cycle
|
|
results.link_up = network_is_link_up();
|
|
results.has_ipv4 = network_has_ipv4();
|
|
results.ipv4 = results.has_ipv4 ? network_get_ipv4_str() : "--";
|
|
results.has_ipv6 = network_has_ipv6();
|
|
results.ipv6_ll = network_has_ipv6_ll() ? network_get_ipv6_ll_str() : "--";
|
|
results.ipv6_gua = network_has_ipv6_gua() ? network_get_ipv6_gua_str() : "--";
|
|
results.ipv6 = results.ipv6_gua[0] != '-' ? results.ipv6_gua : results.ipv6_ll;
|
|
results.gateway_ip = network_has_ipv4() ? network_get_gateway_str() : "--";
|
|
results.gateway6_ip = network_has_ipv6() ? network_get_gateway6_str() : "--";
|
|
results.gateway_reachable_v4 = false;
|
|
results.gateway_reachable_v6 = false;
|
|
results.uplink_reachable_v4 = false;
|
|
results.uplink_reachable_v6 = false;
|
|
results.dns_ok = false;
|
|
results.dns4_ok = false;
|
|
results.dns6_ok = false;
|
|
results.dns_timeout = false;
|
|
results.vpn_reachable = false;
|
|
results.latency_ms = -1;
|
|
results.packet_loss_pct = -1.0f;
|
|
results.temperature_c = -273.0f;
|
|
results.tests_completed = 0;
|
|
|
|
// Show initial progress screen (before any test starts) — pass -1 so test 0 shows
|
|
display_progress(&results, -1, s_anim_frame++);
|
|
|
|
// Run diagnostics with live progress updates
|
|
diagnostics_run_all(&results, 5000,
|
|
[](const struct test_results_t *r, int test_idx) {
|
|
display_progress(r, test_idx, s_anim_frame++);
|
|
}
|
|
);
|
|
|
|
// ═══════════════════════════════════════════════════════════
|
|
// STATE: EVALUATE — check if any test failed
|
|
// ═══════════════════════════════════════════════════════════
|
|
bool all_passed = results.link_up &&
|
|
results.has_ipv4 &&
|
|
results.gateway_reachable_v4;
|
|
|
|
if (!all_passed) {
|
|
ESP_LOGW(TAG, "Some tests failed, showing failure screen");
|
|
display_failure(&results);
|
|
vTaskDelay(pdMS_TO_TICKS(30000));
|
|
|
|
// If link dropped, go back to link check
|
|
if (!network_is_link_up()) continue;
|
|
// Otherwise re-run tests
|
|
continue;
|
|
}
|
|
|
|
// ═══════════════════════════════════════════════════════════
|
|
// STATE: SHOW_STATUS — display results for 60 seconds
|
|
// ═══════════════════════════════════════════════════════════
|
|
ESP_LOGI(TAG, "State: SHOW_STATUS");
|
|
|
|
// Re-read IPv6 state — may have arrived during tests (SLAAC RAs)
|
|
results.has_ipv6 = network_has_ipv6();
|
|
results.ipv6_ll = network_has_ipv6_ll() ? network_get_ipv6_ll_str() : "--";
|
|
results.ipv6_gua = network_has_ipv6_gua() ? network_get_ipv6_gua_str() : "--";
|
|
results.ipv6 = results.ipv6_gua[0] != '-' ? results.ipv6_gua : results.ipv6_ll;
|
|
results.gateway6_ip = network_has_ipv6() ? network_get_gateway6_str() : "--";
|
|
|
|
display_status(&results);
|
|
|
|
for (int i = 0; i < 60; i++) {
|
|
vTaskDelay(pdMS_TO_TICKS(1000));
|
|
|
|
// If link drops, go back to link check early
|
|
if (!network_is_link_up()) {
|
|
ESP_LOGI(TAG, "Link dropped during status display");
|
|
break;
|
|
}
|
|
}
|
|
|
|
// Loop back to LINK_CHECK
|
|
ESP_LOGI(TAG, "Test cycle complete, restarting...");
|
|
}
|
|
}
|