/*
 * wificap — a receive-only, dual-band 802.11 capture appliance.
 *
 * Waveshare ESP32-C5-LCD-1.47. Built for the IoT Security lectures as a
 * demonstration instrument: it shows, live and on its own screen, the moment
 * a WPA2 four-way handshake goes past, and it writes that handshake to an SD
 * card as a pcap that aircrack-ng and Wireshark read without conversion.
 *
 * WHY THIS BOARD AND NOT ESP32 MARAUDER
 *
 * Marauder is the obvious thing to reach for and it does support the
 * ESP32-C5 as a first-class target. It cannot run here: every Marauder C5
 * target requires the 8 MB `default_8MB` flash layout, and this board carries
 * an ESP32-C5HF4, whose 4 MB of flash is inside the package. The 1.47" board
 * is also not one of Marauder's listed targets -- the Waveshare board it
 * supports is the larger Touch-LCD-3.5. If you want Marauder, use a
 * C5-DevKitC-1 or a XIAO ESP32-C5.
 *
 * THIS DEVICE ONLY EVER RECEIVES
 *
 * It never associates, never probes, never deauthenticates, never transmits.
 * The radio is brought up in WIFI_MODE_NULL, so there is no interface that
 * could do any of those things. That is deliberate and it is the difference
 * between a demonstration you can run in a lecture hall and one you cannot:
 * a deauth to hurry a handshake along would touch networks belonging to
 * everyone in the building.
 *
 * SPDX-License-Identifier: MIT
 */

#include "esp_app_desc.h"
#include "esp_err.h"
#include "esp_log.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "nvs_flash.h"

#include "bsp/esp-bsp.h"

#include "wcap.h"

static const char *TAG = "wcap";

void app_main(void)
{
    esp_err_t err = nvs_flash_init();
    if (err == ESP_ERR_NVS_NO_FREE_PAGES || err == ESP_ERR_NVS_NEW_VERSION_FOUND) {
        ESP_ERROR_CHECK(nvs_flash_erase());
        err = nvs_flash_init();
    }
    ESP_ERROR_CHECK(err);

    const esp_app_desc_t *d = esp_app_get_description();
    ESP_LOGI(TAG, "wificap %s (idf %s)", d->version, d->idf_ver);
    ESP_LOGI(TAG, "receive only: mode NULL, no transmit path");

    ESP_ERROR_CHECK(wcap_capture_init());
    ESP_ERROR_CHECK(wcap_ui_init());

    /*
     * The card is mounted after the display on purpose, to prove the point in
     * pcap.c: they share SPI2 and the BSP's init is idempotent, so this
     * ordering works. A failed mount is not fatal -- the screen and the live
     * handshake detection are still worth having, you just cannot record.
     */
    if (wcap_sd_mount() != ESP_OK) {
        ESP_LOGW(TAG, "no SD card: live detection only, recording unavailable");
        wcap_banner_set("no SD card - live only");
    }

    /*
     * A radio that will not start is fatal to the demo but not to the board.
     * Keep the screen and the console alive and say so there, rather than
     * aborting into a reboot loop that hides the reason.
     */
    const bool radio = (wcap_wifi_start() == ESP_OK);
    if (radio) {
        ESP_ERROR_CHECK(wcap_capture_attach());
        xTaskCreate(wcap_writer_task,  "wcap_wr",   4096, NULL, 6, NULL);
        xTaskCreate(wcap_scanner_task, "wcap_scan", 3072, NULL, 5, NULL);
    } else {
        ESP_LOGE(TAG, "radio down; screen and console only");
        wcap_banner_set("RADIO FAILED - see log");
    }

    xTaskCreate(wcap_ui_task, "wcap_ui", 4096, NULL, 3, NULL);

    wcap_console_start();

    ESP_LOGI(TAG, "up; survey mode, not recording. 'help' on the console.");
}
