diff --git a/.vscode/settings.json b/.vscode/settings.json index 5f3816a..becdf2b 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,11 +1,11 @@ { "C_Cpp.intelliSenseEngine": "default", - "idf.espIdfPath": "/home/bruno/esp/v5.4.1/esp-idf", + "idf.espIdfPath": "/home/bruno/esp/master/esp-idf", "idf.pythonInstallPath": "/usr/bin/python", "idf.openOcdConfigs": [ "board/esp32s3-builtin.cfg" ], - "idf.port": "/dev/ttyUSB0", + "idf.port": "/dev/ttyUSB1", "idf.toolsPath": "/home/bruno/.espressif", "idf.customExtraVars": { "OPENOCD_SCRIPTS": "/home/bruno/.espressif/tools/openocd-esp32/v0.12.0-esp32-20240821/openocd-esp32/share/openocd/scripts", @@ -19,6 +19,10 @@ "array": "c", "string": "c", "string_view": "c", - "span": "c" - } + "span": "c", + "esp_timer.h": "c", + "sx1262.h": "c" + }, + "idf.flashBaudRate": "921600", + "idf.monitorBaudRate": "921600", } diff --git a/main/main.c b/main/main.c index 3f0270a..8293617 100644 --- a/main/main.c +++ b/main/main.c @@ -2,21 +2,91 @@ #include "freertos/FreeRTOS.h" #include "freertos/task.h" #include "driver/gpio.h" -#include "esp_log.h" #include "sdkconfig.h" -#include "esp_mac.h" #include #include "radio.h" #include "buscfg.h" #include "driver/spi_master.h" +#include "esp_event.h" +#include "esp_system.h" +#include "esp_event.h" +#include "esp_timer.h" +#include "esp_wifi.h" +#include "esp_log.h" +#include "esp_mac.h" +#include "nvs_flash.h" +#include "string.h" +#include "mbedtls/base64.h" +#include "driver/uart.h" #define TAG "canZem" +void wifi_sniffer_packet_handler(void *buf, wifi_promiscuous_pkt_type_t type) +{ + if (type != WIFI_PKT_MGMT && type != WIFI_PKT_DATA) + return; + + const wifi_promiscuous_pkt_t *ppkt = (wifi_promiscuous_pkt_t *)buf; + const uint8_t *payload = ppkt->payload; + uint32_t len = ppkt->rx_ctrl.sig_len + 44; + + // Simple PlecyC header check + if (len > 24 + 11 && + payload[29] == 'P' && + payload[30] == 'l' && + payload[31] == 'e' && + payload[32] == 'c' && + payload[33] == 'y' && + payload[34] == 'C') + { + unsigned char outBuf[3136]; + size_t output_len = 0; // will store the real output size + + mbedtls_base64_encode(outBuf, sizeof(outBuf), &output_len, buf, len); + printf("\n the frame is %ld bytes long\n", len); + printf("\nGot 2400mhZ frame: %s\n", outBuf); + // // Parse further as needed + // ESP_LOGI(TAG, "HEXko\n"); + // ESP_LOG_BUFFER_HEXDUMP(TAG, buf, len, ESP_LOG_INFO); + } +} + +void start_sniffer() +{ + nvs_flash_init(); + ESP_ERROR_CHECK(esp_netif_init()); + ESP_ERROR_CHECK(esp_event_loop_create_default()); + + wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); + cfg.static_rx_buf_num = 32; + cfg.dynamic_rx_buf_num = 32; + cfg.ampdu_tx_enable = 1; + ESP_ERROR_CHECK(esp_wifi_init(&cfg)); + ESP_ERROR_CHECK(esp_wifi_set_storage(WIFI_STORAGE_RAM)); + + // Set mode to NULL (or STA) + ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_NULL)); + ESP_ERROR_CHECK(esp_wifi_start()); + + // Set channel 10 explicitly + ESP_ERROR_CHECK(esp_wifi_set_channel(10, WIFI_SECOND_CHAN_NONE)); + + // Enable promiscuous mode and set callback + ESP_ERROR_CHECK(esp_wifi_set_promiscuous_rx_cb(&wifi_sniffer_packet_handler)); + ESP_ERROR_CHECK(esp_wifi_set_promiscuous(true)); + + // Optional: Disable power saving + ESP_ERROR_CHECK(esp_wifi_set_ps(WIFI_PS_NONE)); +} + void app_main(void) { + uart_set_baudrate(UART_NUM_0, 921600); ESP_LOGI("BOOT", "BRN Systems incorporated CanSat flight firmware build: %s %s", __DATE__, __TIME__); + start_sniffer(); + spi_bus_config_t HighSpeedBusCfg = { // CONNECTED to LoRa and SD card .mosi_io_num = HSPI_MOSI_GPIO,