130 lines
3.4 KiB
C
130 lines
3.4 KiB
C
/**
|
|
* Block 4: Integration und Flashen der Firmware auf ESP32-C5
|
|
*
|
|
* Ziel: C-ITS Sniffer Firmware für ESP32-C5 compilieren und flashen
|
|
*/
|
|
|
|
#include <stdio.h>
|
|
#include <string.h>
|
|
#include <stdlib.h>
|
|
#include <stdint.h>
|
|
#include <stdbool.h>
|
|
#include <time.h>
|
|
|
|
#include "sdkconfig.h"
|
|
#include "esp_err.h"
|
|
#include "esp_log.h"
|
|
#include "esp_wifi.h"
|
|
#include "nvs_flash.h"
|
|
#include "freertos/FreeRTOS.h"
|
|
#include "freertos/task.h"
|
|
|
|
#include "block1_hardware_init.h"
|
|
#include "block2_cits_parser.h"
|
|
#include "block3_csv_writer.h"
|
|
|
|
static const char *TAG = "CITS_SNIFFER";
|
|
|
|
/**
|
|
* Sniffer Callback - Wird vom WiFi Promiscuous Mode aufgerufen
|
|
* Jede empfangene 802.11p Nachricht wird hier geloggt
|
|
*/
|
|
static void sniffer_callback(void *recv_buf, wifi_promiscuous_pkt_type_t type)
|
|
{
|
|
wifi_promiscuous_pkt_t *packet = (wifi_promiscuous_pkt_t *)recv_buf;
|
|
|
|
// Nur MISC Typ für 802.11p verarbeiten
|
|
if (type != WIFI_PKT_MISC) {
|
|
return;
|
|
}
|
|
|
|
if (packet->payload == NULL || packet->rx_ctrl.sig_len < 20) {
|
|
return;
|
|
}
|
|
|
|
// RSSI aus dem Paket
|
|
int8_t rssi = packet->rx_ctrl.rssi;
|
|
|
|
ESP_LOGI(TAG, "[SNIFFER] 802.11p Paket empfangen: %d Bytes | RSSI: %d dBm",
|
|
packet->rx_ctrl.sig_len, rssi);
|
|
|
|
// Verarbeite C-ITS Nachricht
|
|
cits_process_packet(packet->payload, packet->rx_ctrl.sig_len,
|
|
packet->rx_ctrl.timestamp / 1000000U,
|
|
packet->rx_ctrl.timestamp % 1000000U);
|
|
}
|
|
|
|
/**
|
|
* C-ITS Sniffer aufsetzen
|
|
*/
|
|
static esp_err_t setup_cits_sniffer(void)
|
|
{
|
|
// WiFi Promiscuous Mode starten
|
|
esp_err_t ret = start_promiscuous_mode(CITS_CHANNEL_0_MHZ);
|
|
if (ret != ESP_OK) {
|
|
ESP_LOGE(TAG, "Failed to start promiscuous mode");
|
|
return ret;
|
|
}
|
|
|
|
// C-ITS Parser initialisieren
|
|
ret = cits_parser_init();
|
|
if (ret != ESP_OK) {
|
|
ESP_LOGE(TAG, "Failed to initialize C-ITS parser");
|
|
return ret;
|
|
}
|
|
|
|
// CSV Writer initialisieren
|
|
ret = csv_writer_init();
|
|
if (ret != ESP_OK) {
|
|
ESP_LOGE(TAG, "Failed to initialize CSV writer");
|
|
return ret;
|
|
}
|
|
|
|
ESP_LOGI(TAG, "C-ITS Sniffer Setup abgeschlossen");
|
|
return ESP_OK;
|
|
}
|
|
|
|
/**
|
|
* Hauptfunktion
|
|
*/
|
|
void app_main(void)
|
|
{
|
|
ESP_LOGI(TAG, "C-ITS Sniffer für ESP32-C5 startet...");
|
|
|
|
// NVS initialisieren
|
|
esp_err_t ret = init_nvs_flash();
|
|
if (ret != ESP_OK) {
|
|
ESP_LOGE(TAG, "Failed to initialize NVS");
|
|
return;
|
|
}
|
|
|
|
// WiFi initialisieren
|
|
ret = initialize_wifi();
|
|
if (ret != ESP_OK) {
|
|
ESP_LOGE(TAG, "Failed to initialize WiFi");
|
|
return;
|
|
}
|
|
|
|
// C-ITS Sniffer aufsetzen
|
|
ret = setup_cits_sniffer();
|
|
if (ret != ESP_OK) {
|
|
ESP_LOGE(TAG, "Failed to setup C-ITS Sniffer");
|
|
return;
|
|
}
|
|
|
|
// Hauptschleife - C-ITS Nachrichten werden im Callback verarbeitet
|
|
ESP_LOGI(TAG, "C-ITS Sniffer läuft - Empfange DSRC/C-ITS Nachrichten auf %u MHz...",
|
|
CITS_CHANNEL_0_MHZ);
|
|
ESP_LOGI(TAG, "Jede empfangene Nachricht wird geloggt und in CSV gespeichert");
|
|
|
|
// Zähler für Statistik
|
|
uint32_t packet_count = 0;
|
|
uint32_t last_report = 0;
|
|
|
|
while (1) {
|
|
vTaskDelay(pdMS_TO_TICKS(60000)); // Alle 60 Sekunden Statistik
|
|
last_report++;
|
|
ESP_LOGI(TAG, "Laufzeit: %lu Min. | Warte auf C-ITS Nachrichten...",
|
|
(unsigned long)last_report);
|
|
}
|
|
} |