#include #include #include "freertos/FreeRTOS.h" #include "freertos/task.h" #include "esp_log.h" #include "esp_event.h" #include "esp_timer.h" #include "esp_err.h" #include "esp_wifi.h" #include "esp_system.h" #include "esp_console.h" #include "nvs_flash.h" #include "led.h" #include "cits_store.h" #include "cits_rx.h" #include "st7735.h" #include static const char *TAG = "cits_main"; static int cmd_stats(int argc, char **argv) { (void)argc; (void)argv; printf("rx=%llu cits=%llu drops=%llu\n", (unsigned long long)cits_rx_frames(), (unsigned long long)cits_rx_cits(), (unsigned long long)cits_rx_drops()); printf("storage=%s ready=%d count=%llu path=%s\n", cits_store_source(), (int)cits_store_ready(), (unsigned long long)cits_store_count(), cits_store_path()); return 0; } static int cmd_seron(int argc, char **argv) { (void)argc; (void)argv; cits_rx_set_serial(true); printf("live serial logging ON\n"); return 0; } static int cmd_seroff(int argc, char **argv) { (void)argc; (void)argv; cits_rx_set_serial(false); printf("live serial logging OFF (CSV continues)\n"); return 0; } static int cmd_reboot(int argc, char **argv) { (void)argc; (void)argv; esp_restart(); return 0; } static esp_err_t reg_cmd(const char *name, const char *help, esp_console_cmd_func_t func) { esp_console_cmd_t c; c.command = name; c.help = help; c.hint = NULL; c.func = func; return esp_console_cmd_register(&c); } /* ---- ST7735 debug panel (80x160) ---- */ static void render_panel(void) { if (!st7735_ready()) return; char line[24]; st7735_set_fg(0x07E0); /* green */ st7735_set_bg(0x0000); /* black */ st7735_clear(0x0000); st7735_text(0, 0, "C-ITS SNIFFER"); snprintf(line, sizeof(line), "5MIN: %lu", (unsigned long)cits_rx_count_5min()); st7735_text(0, 10, line); snprintf(line, sizeof(line), "FRM: %llu", (unsigned long long)cits_rx_frames()); st7735_text(0, 20, line); snprintf(line, sizeof(line), "DROP: %llu", (unsigned long long)cits_rx_drops()); st7735_text(0, 30, line); st7735_text(0, 40, "SRC: "); st7735_text(26, 40, cits_store_source()); const char *src = cits_store_source(); if (cits_store_ready() && src && !strcmp(src, "sd-card")) { struct statvfs sv; if (statvfs("/sd", &sv) == 0) { uint64_t freeb = (uint64_t)sv.f_bavail * (uint64_t)sv.f_bsize; uint64_t totb = (uint64_t)sv.f_blocks * (uint64_t)sv.f_bsize; int pct = totb ? (int)(100ULL * freeb / totb) : 0; snprintf(line, sizeof(line), "FREE: %d%%", pct); st7735_text(0, 50, line); } else { st7735_text(0, 50, "FREE: n/a"); } } else { st7735_text(0, 50, "FREE: flash"); } st7735_text(0, 60, "CH: 140 / 5.9G"); } static void display_task(void *arg) { (void)arg; for (;;) { render_panel(); vTaskDelay(pdMS_TO_TICKS(1000)); } } void app_main(void) { esp_log_level_set("*", ESP_LOG_INFO); esp_log_level_set("sdmmc_host", ESP_LOG_WARN); esp_log_level_set("sdmmc", ESP_LOG_WARN); esp_log_level_set("fatfs", ESP_LOG_WARN); esp_log_level_set("sdspi", ESP_LOG_WARN); esp_log_level_set("spi", ESP_LOG_WARN); ESP_ERROR_CHECK(esp_event_loop_create_default()); esp_timer_init(); ESP_LOGI(TAG, "=========================================="); ESP_LOGI(TAG, " C-ITS Sniffer - ESP32-C5 (Lilygo T-Dongle C5)"); ESP_LOGI(TAG, " 5.9 GHz / 802.11p capture -> SD card + serial"); ESP_LOGI(TAG, "=========================================="); led_init(); led_set(0, 0, 60); /* blue: booting */ /* NVS (required by the Wi-Fi driver) */ esp_err_t e = nvs_flash_init(); if (e == ESP_ERR_NVS_NO_FREE_PAGES || e == ESP_ERR_NVS_NEW_VERSION_FOUND) { ESP_ERROR_CHECK(nvs_flash_erase()); e = nvs_flash_init(); } if (e != ESP_OK) { ESP_LOGW(TAG, "nvs_flash_init: %s (continuing)", esp_err_to_name(e)); } /* Storage: SD card primary, flash 'storage' partition as fallback. */ if (cits_store_init() == ESP_OK) { ESP_LOGI(TAG, "storage backend: %s", cits_store_source()); led_set(0, 60, 0); } else { ESP_LOGE(TAG, "no storage (no SD card, no flash) - serial-only logging"); led_set(60, 0, 0); } /* Arm the 5.9 GHz C-ITS receiver. */ e = cits_rx_start(); if (e != ESP_OK) { ESP_LOGE(TAG, "cits_rx_start failed: %s", esp_err_to_name(e)); led_set(60, 0, 0); } else { ESP_LOGI(TAG, "armed - listening for C-ITS on 5.9 GHz"); led_set(0, 60, 0); } /* ST7735 debug display (shares SPI2_HOST bus with the SD card). */ if (st7735_init() == ESP_OK) { ESP_LOGI(TAG, "display ready (ST7735 80x160)"); xTaskCreate(display_task, "disp", 8192, NULL, 3, NULL); } else { ESP_LOGW(TAG, "display init failed (continuing)"); } /* Console REPL (USB-JTAG primary; UART fallback) */ esp_console_repl_config_t repl_cfg = ESP_CONSOLE_REPL_CONFIG_DEFAULT(); repl_cfg.prompt = "cits> "; esp_console_repl_t *repl = NULL; #if CONFIG_ESP_CONSOLE_USB_SERIAL_JTAG esp_console_dev_usb_serial_jtag_config_t usbj = ESP_CONSOLE_DEV_USB_SERIAL_JTAG_CONFIG_DEFAULT(); ESP_ERROR_CHECK(esp_console_new_repl_usb_serial_jtag(&usbj, &repl_cfg, &repl)); #elif CONFIG_ESP_CONSOLE_UART esp_console_dev_uart_config_t uart = ESP_CONSOLE_DEV_UART_CONFIG_DEFAULT(); ESP_ERROR_CHECK(esp_console_new_repl_uart(&uart, &repl_cfg, &repl)); #endif reg_cmd("stats", "show rx + storage stats", cmd_stats); reg_cmd("seron", "enable live serial logging", cmd_seron); reg_cmd("seroff", "disable live serial logging (CSV continues)", cmd_seroff); reg_cmd("reboot", "reboot the device", cmd_reboot); ESP_ERROR_CHECK(esp_console_start_repl(repl)); }