Files
c5-project/ESP32-C5-Toolkit/main/wifi_init.c
2026-05-03 23:19:51 -07:00

124 lines
4.1 KiB
C

#include <stdint.h>
#include <stdbool.h>
#include <string.h>
#include "esp_wifi.h"
#include "esp_log.h"
#include "esp_event.h"
#include "esp_netif.h"
#include "nvs_flash.h"
#include "board_config.h"
// Define MACSTR and MAC2STR if not already defined
#ifndef MAC2STR
#define MAC2STR(a) (a)[0], (a)[1], (a)[2], (a)[3], (a)[4], (a)[5]
#define MACSTR "%02x:%02x:%02x:%02x:%02x:%02x"
#endif
static const char *TAG = "wifi_init";
// Core frame bypass functions - use weak symbols to override library functions
__attribute__((weak))
int ieee80211_raw_frame_sanity_check(void* frame_ctrl, int32_t frame_len, int32_t ampdu_flag) {
// Always allow frame to pass the sanity check
return 0;
}
__attribute__((weak))
bool ieee80211_is_frame_type_supported(uint8_t frame_type) {
// Support all frame types for packet sniffing
return true;
}
__attribute__((weak))
int ieee80211_handle_frame_type(void* frame_ctrl, uint32_t* dport) {
// Allow all frame types
return 0;
}
// WiFi event handler
static void wifi_event_handler(void* arg, esp_event_base_t event_base,
int32_t event_id, void* event_data) {
if (event_base == WIFI_EVENT) {
switch (event_id) {
case WIFI_EVENT_AP_START:
ESP_LOGI(TAG, "✓✓✓ WiFi AP STARTED - SSID 'ESP32-C5-Toolkit' is now visible ✓✓✓");
break;
case WIFI_EVENT_AP_STOP:
ESP_LOGW(TAG, "WiFi AP STOPPED");
break;
case WIFI_EVENT_AP_STACONNECTED:
{
wifi_event_ap_staconnected_t* event = (wifi_event_ap_staconnected_t*) event_data;
ESP_LOGI(TAG, "Station "MACSTR" joined, AID=%d",
MAC2STR(event->mac), event->aid);
}
break;
case WIFI_EVENT_AP_STADISCONNECTED:
{
wifi_event_ap_stadisconnected_t* event = (wifi_event_ap_stadisconnected_t*) event_data;
ESP_LOGI(TAG, "Station "MACSTR" left, AID=%d",
MAC2STR(event->mac), event->aid);
}
break;
default:
break;
}
}
}
// Initialize WiFi in AP+STA mode (copied from working WiFi-Scanner project)
void wifi_init_ap(void) {
// Initialize network interface
ESP_ERROR_CHECK(esp_netif_init());
// Create default event loop
ESP_ERROR_CHECK(esp_event_loop_create_default());
// IMPORTANT: Create BOTH STA and AP network interfaces
// This is required for APSTA mode to work properly
esp_netif_create_default_wifi_sta();
esp_netif_t *ap_netif = esp_netif_create_default_wifi_ap();
if (ap_netif == NULL) {
ESP_LOGE(TAG, "Failed to create AP netif!");
return;
}
// Initialize WiFi with default config
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
ESP_ERROR_CHECK(esp_wifi_init(&cfg));
// Register event handler
ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, ESP_EVENT_ANY_ID,
&wifi_event_handler, NULL));
// Configure AP settings
wifi_config_t wifi_config = {
.ap = {
.ssid = "ESP32-C5-Toolkit",
.password = "h4ck3rm4n",
.ssid_len = strlen("ESP32-C5-Toolkit"),
.channel = 1,
.authmode = WIFI_AUTH_WPA2_PSK,
.max_connection = 4,
},
};
// Set WiFi to AP+Station mode (required for scanning while serving AP)
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_APSTA));
ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_AP, &wifi_config));
ESP_ERROR_CHECK(esp_wifi_start());
// Get and log the AP IP address (after WiFi starts)
esp_netif_ip_info_t ip_info;
esp_err_t ret = esp_netif_get_ip_info(ap_netif, &ip_info);
if (ret == ESP_OK) {
ESP_LOGI(TAG, "AP IP Address: " IPSTR, IP2STR(&ip_info.ip));
} else {
ESP_LOGI(TAG, "AP IP Address: 192.168.4.1 (default)");
}
ESP_LOGI(TAG, "Wi-Fi AP+STA Started: SSID=ESP32-C5-Toolkit");
ESP_LOGI(TAG, "Web interface available at: http://192.168.4.1");
}