Skip to content

Commit

Permalink
Improvement
Browse files Browse the repository at this point in the history
  • Loading branch information
mads256h committed Sep 26, 2023
1 parent bb0274f commit d0d6d2c
Show file tree
Hide file tree
Showing 5 changed files with 162 additions and 26 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
cmake_minimum_required(VERSION 3.16)

include($ENV{IDF_PATH}/tools/cmake/project.cmake)
project(wifi-scanner)
project(wifi-sniffer)
36 changes: 36 additions & 0 deletions main/Kconfig.projbuild
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
menu "Sniffer Configuration"
config BLINK_GPIO_NUM
int "The GPIO number that is blinked"
range 0 33
default 2
help
"No help :)"

config BLINK_SPEED
int "The time between blinks in milliseconds"
range 10 100
default 25
help
"No help :)"

config PACKET_QUEUE_SIZE
int "The size of the packet queue"
range 8 128
default 32
help
"The size of the packet queue. Too small of a value may drop some packets."

config PACKET_QUEUE_TIMEOUT
int "The timeout in ms when inserting into the queue"
range 1 100
default 10
help
"The timeout in milliseconds of trying to insert a packet into the queue. A too small or too big value may drop packets."

config CHANNEL_SWITCHER_FREQUENCY
int "The time in milliseconds between channel switches"
range 10 1000
default 100
help
"No help :)"
endmenu
115 changes: 97 additions & 18 deletions main/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,42 +2,119 @@
#include "wifi.h"

#include "sdkconfig.h"
#include <driver/gpio.h>
#include <esp_event.h>
#include <esp_log.h>
#include <esp_wifi.h>
#include <freertos/FreeRTOS.h>
#include <freertos/queue.h>
#include <freertos/task.h>
#include <inttypes.h>
#include <string.h>

void packet_callback(void *buf, wifi_promiscuous_pkt_type_t type) {
constexpr static auto blink_port = (gpio_num_t)CONFIG_BLINK_GPIO_NUM;
constexpr static auto blink_speed = CONFIG_BLINK_SPEED; // ms

constexpr static auto sniffer_tag = "sniffer";

constexpr static auto queue_size = CONFIG_PACKET_QUEUE_SIZE;
constexpr static auto queue_timeout = CONFIG_PACKET_QUEUE_TIMEOUT; // ms

constexpr static auto channel_switcher_frequency = CONFIG_CHANNEL_SWITCHER_FREQUENCY; // ms


static QueueHandle_t packet_queue;

volatile int new_packet = 0;

struct queue_item {
wifi_pkt_rx_ctrl_t rx_ctrl;
void *payload;
};

extern "C" void app_main(void) {
packet_queue = xQueueCreate(queue_size, sizeof(queue_item));

setup_blinker();

setup_wifi();


while (true) {
queue_item item;

// Block forever until we receive a packet from the queue.
if (xQueueReceive(packet_queue, &item, portMAX_DELAY) != pdTRUE) {
// Keep trying :)
continue;
}

auto *frame = (mac_frame *)item.payload;
print_frame_control_type(frame->frame_control);

printf("rssi: %d\n"
"channel: %u\n",
item.rx_ctrl.rssi, item.rx_ctrl.channel);

printf("receiver: ");
print_mac_address(frame->receiver_address);

printf("transmitter: ");
print_mac_address(frame->transmitter_address);

printf("\n\n\n");

free(item.payload);
}
}

static void packet_callback(void *buf, wifi_promiscuous_pkt_type_t type) {
auto *pkt = (wifi_promiscuous_pkt_t *)buf;
auto *rx_ctrl = &pkt->rx_ctrl;
auto *frame = (mac_frame *)pkt->payload;
print_frame_control_type(frame->frame_control);

printf("rssi: %d\n"
"channel: %u\n"
"sig_len: %u\n",
rx_ctrl->rssi, rx_ctrl->channel, rx_ctrl->sig_len);
auto *new_payload = malloc(rx_ctrl->sig_len);
memcpy(new_payload, pkt->payload, rx_ctrl->sig_len);

printf("duration: %u\n", frame->duration);
queue_item item{pkt->rx_ctrl, new_payload};

printf("receiver: ");
print_mac_address(frame->receiver_address);
new_packet = 1;

printf("transmitter: ");
print_mac_address(frame->transmitter_address);
if (xQueueSend(packet_queue, &item, pdMS_TO_TICKS(queue_timeout)) != pdTRUE) {
ESP_LOGE(sniffer_tag, "queue full");
free(new_payload);
}
}

printf("destination: ");
print_mac_address(frame->destination_address);
static void task_blinker(void *parameters) {
while (true) {
gpio_set_level(blink_port, new_packet);
new_packet = 0;
vTaskDelay(blink_speed / portTICK_PERIOD_MS);
gpio_set_level(blink_port, 0);
vTaskDelay(blink_speed / portTICK_PERIOD_MS);
}
}

printf("source: ");
print_mac_address(frame->source_address);
static void task_channel_hopper(void *parameters) {
// TODO: Give more time to channels with more packets.
// Ignore packets that are probe requests or beacon as they are outside channels.
auto channel = 0;
while (true) {
esp_wifi_set_channel(channel + 1, WIFI_SECOND_CHAN_NONE);
channel = (channel + 1) % 10;
vTaskDelay(channel_switcher_frequency / portTICK_PERIOD_MS);
}
}

printf("\n\n\n");
static void setup_blinker() {
gpio_reset_pin(blink_port);
gpio_set_direction(blink_port, GPIO_MODE_OUTPUT);

xTaskCreate(task_blinker, "blinker", 2048, (void *)nullptr, tskIDLE_PRIORITY,
NULL);
}

extern "C" void app_main(void) {
static void setup_wifi() {
esp_netif_init();
esp_event_loop_create_default();
auto *sta_netif = esp_netif_create_default_wifi_sta();
Expand All @@ -50,4 +127,6 @@ extern "C" void app_main(void) {

esp_wifi_set_promiscuous(true);
esp_wifi_set_promiscuous_rx_cb(packet_callback);

xTaskCreate(task_channel_hopper, "channel hopper", 2048, (void*)nullptr, tskIDLE_PRIORITY, NULL);
}
12 changes: 12 additions & 0 deletions main/main.h
Original file line number Diff line number Diff line change
@@ -1 +1,13 @@
#pragma once

#include <esp_wifi.h>

static void packet_callback(void *buf, wifi_promiscuous_pkt_type_t type);

static void task_blinker(void* parameters);

static void task_channel_hopper(void *parameters);

static void setup_blinker();

static void setup_wifi();
23 changes: 16 additions & 7 deletions sdkconfig
Original file line number Diff line number Diff line change
Expand Up @@ -338,12 +338,22 @@ CONFIG_PARTITION_TABLE_OFFSET=0x8000
CONFIG_PARTITION_TABLE_MD5=y
# end of Partition Table

#
# Sniffer Configuration
#
CONFIG_BLINK_GPIO_NUM=2
CONFIG_BLINK_SPEED=25
CONFIG_PACKET_QUEUE_SIZE=32
CONFIG_PACKET_QUEUE_TIMEOUT=10
CONFIG_CHANNEL_SWITCHER_FREQUENCY=100
# end of Sniffer Configuration

#
# Compiler options
#
CONFIG_COMPILER_OPTIMIZATION_DEFAULT=y
# CONFIG_COMPILER_OPTIMIZATION_DEFAULT is not set
# CONFIG_COMPILER_OPTIMIZATION_SIZE is not set
# CONFIG_COMPILER_OPTIMIZATION_PERF is not set
CONFIG_COMPILER_OPTIMIZATION_PERF=y
# CONFIG_COMPILER_OPTIMIZATION_NONE is not set
CONFIG_COMPILER_OPTIMIZATION_ASSERTIONS_ENABLE=y
# CONFIG_COMPILER_OPTIMIZATION_ASSERTIONS_SILENT is not set
Expand All @@ -358,7 +368,7 @@ CONFIG_COMPILER_STACK_CHECK_MODE_NONE=y
# CONFIG_COMPILER_STACK_CHECK_MODE_NORM is not set
# CONFIG_COMPILER_STACK_CHECK_MODE_STRONG is not set
# CONFIG_COMPILER_STACK_CHECK_MODE_ALL is not set
# CONFIG_COMPILER_WARN_WRITE_STRINGS is not set
CONFIG_COMPILER_WARN_WRITE_STRINGS=y
# CONFIG_COMPILER_DISABLE_GCC12_WARNINGS is not set
# CONFIG_COMPILER_DUMP_RTL_FILES is not set
# end of Compiler options
Expand Down Expand Up @@ -1124,7 +1134,6 @@ CONFIG_FREERTOS_TASK_NOTIFICATION_ARRAY_ENTRIES=1
#
# Port
#
CONFIG_FREERTOS_TASK_FUNCTION_WRAPPER=y
# CONFIG_FREERTOS_WATCHPOINT_END_OF_STACK is not set
CONFIG_FREERTOS_TLSP_DELETION_CALLBACKS=y
# CONFIG_FREERTOS_ENABLE_STATIC_TASK_CLEAN_UP is not set
Expand Down Expand Up @@ -1738,8 +1747,8 @@ CONFIG_LOG_BOOTLOADER_LEVEL=3
CONFIG_FLASHMODE_DIO=y
# CONFIG_FLASHMODE_DOUT is not set
CONFIG_MONITOR_BAUD=115200
CONFIG_OPTIMIZATION_LEVEL_DEBUG=y
CONFIG_COMPILER_OPTIMIZATION_LEVEL_DEBUG=y
# CONFIG_OPTIMIZATION_LEVEL_DEBUG is not set
# CONFIG_COMPILER_OPTIMIZATION_LEVEL_DEBUG is not set
# CONFIG_OPTIMIZATION_LEVEL_RELEASE is not set
# CONFIG_COMPILER_OPTIMIZATION_LEVEL_RELEASE is not set
CONFIG_OPTIMIZATION_ASSERTIONS_ENABLED=y
Expand All @@ -1751,7 +1760,7 @@ CONFIG_STACK_CHECK_NONE=y
# CONFIG_STACK_CHECK_NORM is not set
# CONFIG_STACK_CHECK_STRONG is not set
# CONFIG_STACK_CHECK_ALL is not set
# CONFIG_WARN_WRITE_STRINGS is not set
CONFIG_WARN_WRITE_STRINGS=y
# CONFIG_ESP32_APPTRACE_DEST_TRAX is not set
CONFIG_ESP32_APPTRACE_DEST_NONE=y
CONFIG_ESP32_APPTRACE_LOCK_ENABLE=y
Expand Down

0 comments on commit d0d6d2c

Please sign in to comment.