CITS Sniffer v4: Inline Implementierungen - alle Funktionen direkt in app_main.c
This commit is contained in:
+40
-42
@@ -18,18 +18,13 @@
|
||||
#include "esp_timer.h"
|
||||
#include "nvs_flash.h"
|
||||
#include "esp_vfs_fat.h"
|
||||
#include "esp_vfs.h"
|
||||
#include "wear_levelling.h"
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/task.h"
|
||||
|
||||
// ==================== Forward Declarations ====================
|
||||
// ==================== Typedefs ====================
|
||||
|
||||
// Block 1: Hardware
|
||||
extern esp_err_t initialize_wifi(void);
|
||||
extern esp_err_t start_promiscuous_mode(uint32_t channel);
|
||||
extern esp_err_t init_nvs_flash(void);
|
||||
extern void register_cits_commands(void);
|
||||
|
||||
// Block 2: Parser
|
||||
typedef struct {
|
||||
uint32_t timestamp_sec;
|
||||
uint32_t timestamp_usec;
|
||||
@@ -40,28 +35,17 @@ typedef struct {
|
||||
int16_t rssi;
|
||||
} cits_message_t;
|
||||
|
||||
extern esp_err_t cits_parser_init(void);
|
||||
extern void cits_process_packet(void *packet_info);
|
||||
|
||||
// Block 3: CSV Writer
|
||||
extern esp_err_t csv_writer_init(void);
|
||||
extern void csv_store_cits_message(cits_message_t *msg);
|
||||
extern void csv_writer_deinit(void);
|
||||
|
||||
// ==================== Constants ====================
|
||||
|
||||
static const char *TAG = "CITS_SNIFFER";
|
||||
#define CITS_CHANNEL_0_MHZ 5900
|
||||
#define CSV_WRITE_INTERVAL_SEC (600)
|
||||
|
||||
// ==================== External PHY Functions ====================
|
||||
// Diese kommen vom ESP-IDF / proprietären WiFi-Layer
|
||||
|
||||
// ==================== CSV File Handle ====================
|
||||
// ==================== CSV State ====================
|
||||
|
||||
static FILE *csv_file = NULL;
|
||||
static char current_filename[64];
|
||||
static time_t last_write_time = 0;
|
||||
#define CSV_WRITE_INTERVAL_SEC (600)
|
||||
|
||||
static void write_csv_header(FILE *fp)
|
||||
{
|
||||
@@ -100,6 +84,15 @@ static esp_err_t create_new_csv_file(void)
|
||||
ESP_LOGI(TAG, "Neue CSV-Datei: %s", current_filename);
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
static void csv_store_cits_message(cits_message_t *msg)
|
||||
{
|
||||
if (csv_file != NULL) {
|
||||
write_csv_row(csv_file, msg);
|
||||
fflush(csv_file);
|
||||
}
|
||||
}
|
||||
|
||||
// ==================== Promiscuous Callback ====================
|
||||
|
||||
static void sniffer_callback(void *buf, wifi_promiscuous_pkt_type_t type)
|
||||
@@ -122,23 +115,14 @@ static void sniffer_callback(void *buf, wifi_promiscuous_pkt_type_t type)
|
||||
cits_message_t msg = {
|
||||
.timestamp_sec = sec,
|
||||
.timestamp_usec = usec,
|
||||
.length = pkt->rx_ctrl.sig_len,
|
||||
.length = (pkt->rx_ctrl.sig_len > 65535) ? 65535 : pkt->rx_ctrl.sig_len,
|
||||
.channel = (uint8_t)(CITS_CHANNEL_0_MHZ % 256),
|
||||
.rssi = pkt->rx_ctrl.rssi,
|
||||
};
|
||||
csv_store_cits_message(&msg);
|
||||
}
|
||||
|
||||
// ==================== Partition Table ====================
|
||||
// partition.csv content embedded for flash:
|
||||
// # ESP-ADF Partition Table
|
||||
// # Name, Type, SubType, Offset, Size, Flags
|
||||
// nvs, data, nvs, 0x9000, 0x4000,
|
||||
// phy_init, data, phy, 0xd000, 0x1000,
|
||||
// factory, app, factory, 0x10000, 0x3E0000,
|
||||
// storage, data, fat, , 0x200000,
|
||||
|
||||
// ==================== Init Functions ====================
|
||||
// ==================== Flash Storage ====================
|
||||
|
||||
static esp_err_t setup_flash_storage(void)
|
||||
{
|
||||
@@ -156,6 +140,29 @@ static esp_err_t setup_flash_storage(void)
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
// ==================== WiFi Init (inline Block 1) ====================
|
||||
|
||||
static esp_err_t initialize_wifi(void)
|
||||
{
|
||||
esp_err_t ret = nvs_flash_init();
|
||||
if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
|
||||
nvs_flash_erase();
|
||||
ret = nvs_flash_init();
|
||||
}
|
||||
if (ret != ESP_OK) {
|
||||
ESP_LOGE(TAG, "NVS Flash init fehlgeschlagen: %s", esp_err_to_name(ret));
|
||||
return ret;
|
||||
}
|
||||
ESP_LOGI(TAG, "NVS Flash OK");
|
||||
|
||||
ESP_ERROR_CHECK(esp_wifi_set_storage(WIFI_STORAGE_RAM));
|
||||
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_NULL));
|
||||
ESP_ERROR_CHECK(esp_wifi_start());
|
||||
|
||||
ESP_LOGI(TAG, "WiFi initialisiert");
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
static esp_err_t init_promiscuous(void)
|
||||
{
|
||||
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
|
||||
@@ -181,20 +188,11 @@ void app_main(void)
|
||||
ESP_LOGI(TAG, "=== C-ITS SNIFFER FÜR ESP32-C5 ===");
|
||||
ESP_LOGI(TAG, "Starte...");
|
||||
|
||||
// NVS
|
||||
esp_err_t ret = nvs_flash_init();
|
||||
if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
|
||||
nvs_flash_erase();
|
||||
ret = nvs_flash_init();
|
||||
}
|
||||
ESP_ERROR_CHECK(ret);
|
||||
ESP_LOGI(TAG, "NVS Flash OK");
|
||||
|
||||
// WiFi Setup
|
||||
ESP_ERROR_CHECK(initialize_wifi());
|
||||
ESP_ERROR_CHECK(init_promiscuous());
|
||||
|
||||
// CSV Writer
|
||||
// Flash Storage
|
||||
ESP_ERROR_CHECK(setup_flash_storage());
|
||||
ESP_ERROR_CHECK(create_new_csv_file());
|
||||
last_write_time = time(NULL);
|
||||
|
||||
Reference in New Issue
Block a user