Skip to content

Commit

Permalink
feat: Channel hopping
Browse files Browse the repository at this point in the history
  • Loading branch information
JahazielLem committed Nov 22, 2024
1 parent eccb1c4 commit 5a1f149
Show file tree
Hide file tree
Showing 5 changed files with 177 additions and 1 deletion.
26 changes: 25 additions & 1 deletion firmware/components/ieee_sniffer/ieee_sniffer.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
#include "freertos/queue.h"
#include "freertos/task.h"

#define HOPPING_TIME 3500

static esp_err_t err;
static QueueHandle_t packet_rx_queue = NULL;
static ieee_sniffer_cb_t packet_callback = NULL;
Expand All @@ -34,6 +36,10 @@ void ieee_sniffer_register_cb(ieee_sniffer_cb_t callback) {
packet_callback = callback;
}

uint8_t ieee_sniffer_get_channel() {
return current_channel;
}

void ieee_sniffer_set_channel(int channel) {
current_channel = channel;
if (channel < IEEE_SNIFFER_CHANNEL_MIN) {
Expand All @@ -51,7 +57,7 @@ void ieee_sniffer_set_channel(int channel) {
ESP_LOGI(TAG_IEEE_SNIFFER, "Channel set to %d", current_channel);
}

void ieee_sniffer_begin(void) {
static void ieee_sniffer_configure() {
#if !defined(CONFIG_IEEE_SNIFFER_DEBUG)
esp_log_level_set(TAG_IEEE_SNIFFER, ESP_LOG_NONE);
#endif
Expand Down Expand Up @@ -92,10 +98,28 @@ void ieee_sniffer_begin(void) {
}
esp_ieee802154_set_extended_address(eui64_rev);
ESP_ERROR_CHECK(esp_ieee802154_receive());
}

void ieee_sniffer_begin(void) {
ieee_sniffer_configure();
while (true) {
vTaskDelay(10 / portTICK_PERIOD_MS);
}
vTaskDelete(NULL);
}

void ieee_sniffer_channel_hop() {
ieee_sniffer_configure();
esp_ieee802154_disable();
while (true) {
// vTaskDelay(10 / portTICK_PERIOD_MS);
esp_ieee802154_enable();
ieee_sniffer_set_channel(current_channel + 1);
esp_ieee802154_receive();
vTaskDelay(HOPPING_TIME / portTICK_PERIOD_MS);
esp_ieee802154_disable();
}
vTaskDelete(NULL);
}

void ieee_sniffer_stop(void) {
Expand Down
4 changes: 4 additions & 0 deletions firmware/components/ieee_sniffer/ieee_sniffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,8 @@ void ieee_sniffer_stop(void);
* @param channel The channel to set
*/
void ieee_sniffer_set_channel(int channel);

uint8_t ieee_sniffer_get_channel();

void ieee_sniffer_channel_hop();
#endif // IEEE_SNIFFER_H
2 changes: 2 additions & 0 deletions firmware/main/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "open_thread.h"
#include "preferences.h"
#include "sd_card.h"
#include "warbee_module.h"
#include "wardriving_module.h"
#include "web_file_browser.h"
#include "wifi_app.h"
Expand All @@ -41,5 +42,6 @@ void app_main() {
menus_module_begin();
leds_off();
preferences_put_bool("wifi_connected", false);
warbee_module_begin();
cat_console_begin();
}
138 changes: 138 additions & 0 deletions firmware/main/modules/gps/war_bee/warbee_module.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
#include "warbee_module.h"
#include "esp_log.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "ieee_sniffer.h"
#include "radio_selector.h"

#define DIR_NAME "Wardriving"
#define FILE_NAME DIR_NAME "/Minino"
#define FORMAT_VERSION "ElecCats-1.0"
#define APP_VERSION CONFIG_PROJECT_VERSION
#define MODEL "MININO"
#define RELEASE APP_VERSION
#define DEVICE "MININO"
#define DISPLAY "SH1106 OLED"
#define BOARD "ESP32C6"
#define BRAND "Electronic Cats"
#define STAR "Sol"
#define BODY "3"
#define SUB_BODY "0"

static bool running = false;

static TaskHandle_t zigbee_task_sniffer = NULL;
static int current_channel = IEEE_SNIFFER_CHANNEL_DEFAULT;

static char addressing_mode[4][15] = {"None", "Reserved", "Short/16-bit",
"Long/64-bit"};

const char* csv_header = FORMAT_VERSION ",appRelease=" APP_VERSION
",model=" MODEL ",release=" RELEASE;

static void warbee_packet_dissector(uint8_t* packet, uint8_t packet_length) {
uint8_t position = 0;

mac_fcs_t* fcs = (mac_fcs_t*) &packet[position];
position += sizeof(uint16_t);
// printf("Frame Control Field\n");
// printf("└Channel: %d\n", current_channel);
// printf("└Frame type: %x\n", fcs->frameType);
// printf("└Security Enabled: %s\n", fcs->secure ? "True" :
// "False"); printf("└Frame pending: %s\n",
// fcs->framePending ? "True" : "False");
// printf("└Acknowledge request: %s\n",
// fcs->ackReqd ? "True" : "False");
// printf("└PAN ID Compression: %s\n",
// fcs->panIdCompressed ? "True" : "False");
// printf("└Reserved: %s\n", fcs->rfu1 ? "True" :
// "False"); printf("└Sequence Number Suppression: %s\n",
// fcs->sequenceNumberSuppression ? "True" : "False");
// printf("└Information Elements Present: %s\n",
// fcs->informationElementsPresent ? "True" : "False");
// printf("└Destination addressing mode: %02X %s\n", fcs->destAddrType,
// addressing_mode[fcs->destAddrType]);
// printf("└Frame version: %x\n", fcs->frameVer);
// printf("└Source addressing mode: %02X %s\n", fcs->srcAddrType,
// addressing_mode[fcs->srcAddrType]);

if (fcs->rfu1) {
ESP_LOGE(TAG_IEEE_SNIFFER, "Reserved field 1 is set, ignoring packet");
return;
}

switch (fcs->frameType) {
case FRAME_TYPE_BEACON:
printf("Beacon frame\n");
break;
case FRAME_TYPE_DATA:
printf("Data frame\n");
break;
case FRAME_TYPE_MAC_COMMAND:
printf("Beacon Request\n");
printf("└Channel: %d\n", ieee_sniffer_get_channel());
uint8_t sequence_number = packet[position];
position += sizeof(uint8_t);
printf("Sequence number: %u\n", sequence_number);

uint16_t pan_id = 0;
uint8_t dst_addr[8] = {0};
uint8_t src_addr[8] = {0};
uint16_t short_dst_addr = 0;
uint16_t short_src_addr = 0;

switch (fcs->destAddrType) {
case ADDR_MODE_NONE:
printf("Originating from the PAN coordinator\n");
break;
// Device is sending to a short address
case ADDR_MODE_SHORT:
pan_id = *((uint8_t*) &packet[position]);
position += sizeof(uint8_t);
short_dst_addr = *((uint16_t*) &packet[position]);
position += sizeof(uint16_t);
if (pan_id == 0xFFFF && short_dst_addr == 0xFFFF) {
pan_id = *((uint16_t*) &packet[position]); // srcPan
position += sizeof(uint16_t);
printf("Broadcast on PAN %04x\n", pan_id);
} else {
printf("Destination PAN: 0x%04x\n", pan_id);
printf("Destination : 0x%04x\n", short_dst_addr);
}
break;
default: {
ESP_LOGE(TAG_IEEE_SNIFFER,
"With reserved destination address type, ignoring packet\n");
return;
}
}
break;
default:
printf("Packet ignored because of frame type (%u)\n", fcs->frameType);
break;
}

esp_log_buffer_hex(">", packet, packet_length);
}

static void warbee_channel_hopp_task() {
while (running) {
current_channel = (current_channel == IEEE_SNIFFER_CHANNEL_MAX)
? IEEE_SNIFFER_CHANNEL_MIN
: (current_channel + 1);
ieee_sniffer_set_channel(current_channel);

vTaskDelay(3500 / portTICK_PERIOD_MS);
}
vTaskDelete(NULL);
}

void warbee_module_begin() {
running = true;
radio_selector_set_zigbee_sniffer();
ieee_sniffer_register_cb(warbee_packet_dissector);
xTaskCreate(ieee_sniffer_channel_hop, "ieee_sniffer_task", 4096, NULL, 5,
&zigbee_task_sniffer);
xTaskCreate(warbee_channel_hopp_task, "warbee_channel_hopp_task", 4096, NULL,
5, NULL);
}
8 changes: 8 additions & 0 deletions firmware/main/modules/gps/war_bee/warbee_module.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#pragma once

/**
* @brief Initialize the wardriving zigbee module
*
* @return void
*/
void warbee_module_begin();

0 comments on commit 5a1f149

Please sign in to comment.