Wifi
Source: src/wifi.c · 255 lines · 17 functions.
Module entry points
These functions are exposed to the rest of the project. Open one to follow its decisions and the helpers it calls.
How to read these diagrams
Each function has its own expandable diagram using the actual expressions and statements from this file. Decisions split into True and False; loop bodies are indented. Returns end the function immediately; break and continue follow their C meanings. Arrows between statements show their order when execution reaches them.
These are structural source diagrams before preprocessing, not runtime traces. Conditional compilation and macros remain source expressions. Ternary choices remain inside their statement. Calls are linked separately; callback execution and task scheduling are not inferred.
Functions in this file
wifi_event_handler() · source line 24
static void wifi_event_handler(void *argument, esp_event_base_t event_base,
int32_t event_id, void *event_data)- Execute
(void)argument; - Execute
(void)event_data; - Decision
event_base == WIFI_EVENT && event_id == WIFI_EVENT_SCAN_DONETrue- Execute
scan_done = true;
False- Decision
event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTEDTrue- Execute
connection_state = WIFI_CONNECTION_FAILED; - Execute
last_error = "CONNECTION FAILED";
False- Decision
event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IPTrue- Execute
connection_state = WIFI_CONNECTION_CONNECTED; - Execute
last_error = "";
FalseContinue after this decision.
Read original function source
static void wifi_event_handler(void *argument, esp_event_base_t event_base,
int32_t event_id, void *event_data)
{
(void)argument;
(void)event_data;
if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_SCAN_DONE) {
scan_done = true;
} else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED) {
connection_state = WIFI_CONNECTION_FAILED;
last_error = "CONNECTION FAILED";
} else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP) {
connection_state = WIFI_CONNECTION_CONNECTED;
last_error = "";
}
}accept_already_initialized() · source line 40
static bool accept_already_initialized(esp_err_t result)
- Exit this function
return result == ESP_OK || result == ESP_ERR_INVALID_STATE;
Read original function source
static bool accept_already_initialized(esp_err_t result)
{
return result == ESP_OK || result == ESP_ERR_INVALID_STATE;
}record_error() · source line 45
static bool record_error(esp_err_t result)
- Execute
service_state = WIFI_SERVICE_FAILED; - Execute
last_error = esp_err_to_name(result); - Exit this function
return false;
Read original function source
static bool record_error(esp_err_t result)
{
service_state = WIFI_SERVICE_FAILED;
last_error = esp_err_to_name(result);
return false;
}wifiServicePrepare() · source line 52
bool wifiServicePrepare(void)
Calls into this documentation:
- Decision
service_state == WIFI_SERVICE_READYTrue- Exit this function
return true;
FalseContinue after this decision.
- Decision
service_state == WIFI_SERVICE_FAILEDTrue- Exit this function
return false;
FalseContinue after this decision.
- Execute
const esp_err_t netif_result = esp_netif_init(); - Decision
!accept_already_initialized(netif_result)True- Exit this function
return record_error(netif_result);
FalseContinue after this decision.
- Execute
const esp_err_t event_result = esp_event_loop_create_default(); - Decision
!accept_already_initialized(event_result)True- Exit this function
return record_error(event_result);
FalseContinue after this decision.
- Execute
station_interface = esp_netif_create_default_wifi_sta(); - Decision
station_interface == NULLTrue- Execute
service_state = WIFI_SERVICE_FAILED; - Execute
last_error = "NO STATION INTERFACE"; - Exit this function
return false;
FalseContinue after this decision.
- Execute
wifi_init_config_t configuration = WIFI_INIT_CONFIG_DEFAULT(); - Execute
const esp_err_t init_result = esp_wifi_init(&configuration); - Decision
init_result != ESP_OKTrue- Exit this function
return record_error(init_result);
FalseContinue after this decision.
- Execute
const esp_err_t storage_result = esp_wifi_set_storage(WIFI_STORAGE_RAM); - Decision
storage_result != ESP_OKTrue- Exit this function
return record_error(storage_result);
FalseContinue after this decision.
- Execute
const esp_err_t mode_result = esp_wifi_set_mode(WIFI_MODE_STA); - Decision
mode_result != ESP_OKTrue- Exit this function
return record_error(mode_result);
FalseContinue after this decision.
- Execute
const esp_err_t handler_result = esp_event_handler_instance_register( WIFI_EVENT, WIFI_EVENT_SCAN_DONE, wifi_event_handler, NULL, &scan_event_handler); - Decision
handler_result != ESP_OKTrue- Exit this function
return record_error(handler_result);
FalseContinue after this decision.
- Execute
const esp_err_t connected_handler_result = esp_event_handler_instance_register( WIFI_EVENT, WIFI_EVENT_STA_CONNECTED, wifi_event_handler, NULL, &connected_event_handler); - Decision
connected_handler_result != ESP_OKTrue- Exit this function
return record_error(connected_handler_result);
FalseContinue after this decision.
- Execute
const esp_err_t disconnected_handler_result = esp_event_handler_instance_register( WIFI_EVENT, WIFI_EVENT_STA_DISCONNECTED, wifi_event_handler, NULL, &disconnected_event_handler); - Decision
disconnected_handler_result != ESP_OKTrue- Exit this function
return record_error(disconnected_handler_result);
FalseContinue after this decision.
- Execute
const esp_err_t got_ip_handler_result = esp_event_handler_instance_register( IP_EVENT, IP_EVENT_STA_GOT_IP, wifi_event_handler, NULL, &got_ip_event_handler); - Decision
got_ip_handler_result != ESP_OKTrue- Exit this function
return record_error(got_ip_handler_result);
FalseContinue after this decision.
- Execute
service_state = WIFI_SERVICE_READY; - Execute
last_error = ""; - Exit this function
return true;
Read original function source
bool wifiServicePrepare(void)
{
if (service_state == WIFI_SERVICE_READY) return true;
if (service_state == WIFI_SERVICE_FAILED) return false;
const esp_err_t netif_result = esp_netif_init();
if (!accept_already_initialized(netif_result)) return record_error(netif_result);
const esp_err_t event_result = esp_event_loop_create_default();
if (!accept_already_initialized(event_result)) return record_error(event_result);
station_interface = esp_netif_create_default_wifi_sta();
if (station_interface == NULL) {
service_state = WIFI_SERVICE_FAILED;
last_error = "NO STATION INTERFACE";
return false;
}
wifi_init_config_t configuration = WIFI_INIT_CONFIG_DEFAULT();
const esp_err_t init_result = esp_wifi_init(&configuration);
if (init_result != ESP_OK) return record_error(init_result);
const esp_err_t storage_result = esp_wifi_set_storage(WIFI_STORAGE_RAM);
if (storage_result != ESP_OK) return record_error(storage_result);
const esp_err_t mode_result = esp_wifi_set_mode(WIFI_MODE_STA);
if (mode_result != ESP_OK) return record_error(mode_result);
const esp_err_t handler_result = esp_event_handler_instance_register(
WIFI_EVENT, WIFI_EVENT_SCAN_DONE, wifi_event_handler, NULL, &scan_event_handler);
if (handler_result != ESP_OK) return record_error(handler_result);
const esp_err_t connected_handler_result = esp_event_handler_instance_register(
WIFI_EVENT, WIFI_EVENT_STA_CONNECTED, wifi_event_handler, NULL, &connected_event_handler);
if (connected_handler_result != ESP_OK) return record_error(connected_handler_result);
const esp_err_t disconnected_handler_result = esp_event_handler_instance_register(
WIFI_EVENT, WIFI_EVENT_STA_DISCONNECTED, wifi_event_handler, NULL, &disconnected_event_handler);
if (disconnected_handler_result != ESP_OK) return record_error(disconnected_handler_result);
const esp_err_t got_ip_handler_result = esp_event_handler_instance_register(
IP_EVENT, IP_EVENT_STA_GOT_IP, wifi_event_handler, NULL, &got_ip_event_handler);
if (got_ip_handler_result != ESP_OK) return record_error(got_ip_handler_result);
service_state = WIFI_SERVICE_READY;
last_error = "";
return true;
}wifiServiceStart() · source line 98
bool wifiServiceStart(void)
Calls into this documentation:
- Decision
!wifiServicePrepare()True- Exit this function
return false;
FalseContinue after this decision.
- Decision
radio_startedTrue- Exit this function
return true;
FalseContinue after this decision.
- Execute
const esp_err_t result = esp_wifi_start(); - Decision
result != ESP_OKTrue- Execute
last_error = esp_err_to_name(result); - Exit this function
return false;
FalseContinue after this decision.
- Execute
radio_started = true; - Execute
last_error = ""; - Exit this function
return true;
Read original function source
bool wifiServiceStart(void)
{
if (!wifiServicePrepare()) return false;
if (radio_started) return true;
const esp_err_t result = esp_wifi_start();
if (result != ESP_OK) {
last_error = esp_err_to_name(result);
return false;
}
radio_started = true;
last_error = "";
return true;
}wifiServiceStop() · source line 113
bool wifiServiceStop(void)
- Decision
!radio_startedTrue- Exit this function
return true;
FalseContinue after this decision.
- Execute
const esp_err_t result = esp_wifi_stop(); - Decision
result != ESP_OKTrue- Execute
last_error = esp_err_to_name(result); - Exit this function
return false;
FalseContinue after this decision.
- Execute
radio_started = false; - Execute
connection_state = WIFI_CONNECTION_DISCONNECTED; - Execute
scan_in_progress = false; - Execute
scan_done = false; - Execute
last_error = ""; - Exit this function
return true;
Read original function source
bool wifiServiceStop(void)
{
if (!radio_started) return true;
const esp_err_t result = esp_wifi_stop();
if (result != ESP_OK) {
last_error = esp_err_to_name(result);
return false;
}
radio_started = false;
connection_state = WIFI_CONNECTION_DISCONNECTED;
scan_in_progress = false;
scan_done = false;
last_error = "";
return true;
}wifiServiceStartScan() · source line 130
bool wifiServiceStartScan(void)
- Decision
!radio_startedTrue- Execute
last_error = "RADIO OFF"; - Exit this function
return false;
FalseContinue after this decision.
- Decision
scan_in_progressTrue- Exit this function
return true;
FalseContinue after this decision.
- Execute
const wifi_scan_config_t configuration = {0}; - Execute
const esp_err_t result = esp_wifi_scan_start(&configuration, false); - Decision
result != ESP_OKTrue- Execute
last_error = esp_err_to_name(result); - Exit this function
return false;
FalseContinue after this decision.
- Execute
scan_entry_count = 0U; - Execute
scan_done = false; - Execute
scan_in_progress = true; - Execute
last_error = ""; - Exit this function
return true;
Read original function source
bool wifiServiceStartScan(void)
{
if (!radio_started) {
last_error = "RADIO OFF";
return false;
}
if (scan_in_progress) return true;
const wifi_scan_config_t configuration = {0};
const esp_err_t result = esp_wifi_scan_start(&configuration, false);
if (result != ESP_OK) {
last_error = esp_err_to_name(result);
return false;
}
scan_entry_count = 0U;
scan_done = false;
scan_in_progress = true;
last_error = "";
return true;
}wifiServiceUpdateScan() · source line 151
void wifiServiceUpdateScan(void)
- Decision
!scan_in_progress || !scan_doneTrue- Exit this function
return;
FalseContinue after this decision.
- Execute
wifi_ap_record_t records[WIFI_SCAN_MAX_NETWORKS] = {0}; - Execute
uint16_t record_count = WIFI_SCAN_MAX_NETWORKS; - Execute
const esp_err_t result = esp_wifi_scan_get_ap_records(&record_count, records); - Execute
scan_done = false; - Execute
scan_in_progress = false; - Decision
result != ESP_OKTrue- Execute
last_error = esp_err_to_name(result); - Exit this function
return;
FalseContinue after this decision.
- Execute
scan_entry_count = record_count; - FOR loop
size_t index = 0; index < scan_entry_count; ++indexRepeat body while the loop condition permits; then continue below.
- Execute
memcpy(scan_entries[index].ssid, records[index].ssid, WIFI_SCAN_SSID_CHARS); - Execute
scan_entries[index].ssid[WIFI_SCAN_SSID_CHARS] = '\0'; - Execute
scan_entries[index].rssi = records[index].rssi;
Read original function source
void wifiServiceUpdateScan(void)
{
if (!scan_in_progress || !scan_done) return;
wifi_ap_record_t records[WIFI_SCAN_MAX_NETWORKS] = {0};
uint16_t record_count = WIFI_SCAN_MAX_NETWORKS;
const esp_err_t result = esp_wifi_scan_get_ap_records(&record_count, records);
scan_done = false;
scan_in_progress = false;
if (result != ESP_OK) {
last_error = esp_err_to_name(result);
return;
}
scan_entry_count = record_count;
for (size_t index = 0; index < scan_entry_count; ++index) {
memcpy(scan_entries[index].ssid, records[index].ssid, WIFI_SCAN_SSID_CHARS);
scan_entries[index].ssid[WIFI_SCAN_SSID_CHARS] = '\0';
scan_entries[index].rssi = records[index].rssi;
}
}wifiServiceScanInProgress() · source line 173
bool wifiServiceScanInProgress(void)
- Exit this function
return scan_in_progress;
Read original function source
bool wifiServiceScanInProgress(void)
{
return scan_in_progress;
}wifiServiceScanCount() · source line 178
size_t wifiServiceScanCount(void)
- Exit this function
return scan_entry_count;
Read original function source
size_t wifiServiceScanCount(void)
{
return scan_entry_count;
}wifiServiceScanEntries() · source line 183
const wifi_scan_entry_t *wifiServiceScanEntries(void)
- Exit this function
return scan_entries;
Read original function source
const wifi_scan_entry_t *wifiServiceScanEntries(void)
{
return scan_entries;
}wifiServiceConnect() · source line 188
bool wifiServiceConnect(const char *ssid, const char *password)
Calls into this documentation:
- Decision
ssid == NULL || password == NULL || ssid[0] == '\0' || !wifiServiceStart()True- Exit this function
return false;
FalseContinue after this decision.
- Execute
wifi_config_t configuration = {0}; - Execute
strncpy((char *)configuration.sta.ssid, ssid, sizeof(configuration.sta.ssid) - 1U); - Execute
strncpy((char *)configuration.sta.password, password, sizeof(configuration.sta.password) - 1U); - Execute
const esp_err_t config_result = esp_wifi_set_config(WIFI_IF_STA, &configuration); - Decision
config_result != ESP_OKTrue- Execute
last_error = esp_err_to_name(config_result); - Exit this function
return false;
FalseContinue after this decision.
- Execute
(void)esp_wifi_disconnect(); - Execute
const esp_err_t connect_result = esp_wifi_connect(); - Decision
connect_result != ESP_OKTrue- Execute
last_error = esp_err_to_name(connect_result); - Exit this function
return false;
FalseContinue after this decision.
- Execute
connection_state = WIFI_CONNECTION_CONNECTING; - Execute
last_error = ""; - Exit this function
return true;
Read original function source
bool wifiServiceConnect(const char *ssid, const char *password)
{
if (ssid == NULL || password == NULL || ssid[0] == '\0' || !wifiServiceStart()) return false;
wifi_config_t configuration = {0};
strncpy((char *)configuration.sta.ssid, ssid, sizeof(configuration.sta.ssid) - 1U);
strncpy((char *)configuration.sta.password, password, sizeof(configuration.sta.password) - 1U);
const esp_err_t config_result = esp_wifi_set_config(WIFI_IF_STA, &configuration);
if (config_result != ESP_OK) {
last_error = esp_err_to_name(config_result);
return false;
}
(void)esp_wifi_disconnect();
const esp_err_t connect_result = esp_wifi_connect();
if (connect_result != ESP_OK) {
last_error = esp_err_to_name(connect_result);
return false;
}
connection_state = WIFI_CONNECTION_CONNECTING;
last_error = "";
return true;
}wifiServiceConnectionState() · source line 212
wifi_connection_state_t wifiServiceConnectionState(void)
- Exit this function
return connection_state;
Read original function source
wifi_connection_state_t wifiServiceConnectionState(void)
{
return connection_state;
}wifiServiceRelease() · source line 217
void wifiServiceRelease(void)
- Decision
radio_startedTrue- Execute
(void)esp_wifi_stop();
FalseContinue after this decision.
- Execute
(void)esp_event_handler_instance_unregister(WIFI_EVENT, WIFI_EVENT_SCAN_DONE, scan_event_handler); - Execute
(void)esp_event_handler_instance_unregister(WIFI_EVENT, WIFI_EVENT_STA_CONNECTED, connected_event_handler); - Execute
(void)esp_event_handler_instance_unregister(WIFI_EVENT, WIFI_EVENT_STA_DISCONNECTED, disconnected_event_handler); - Execute
(void)esp_event_handler_instance_unregister(IP_EVENT, IP_EVENT_STA_GOT_IP, got_ip_event_handler); - Decision
service_state == WIFI_SERVICE_READYTrue- Execute
(void)esp_wifi_deinit();
FalseContinue after this decision.
- Decision
station_interface != NULLTrue- Execute
esp_netif_destroy_default_wifi(station_interface); - Execute
station_interface = NULL;
FalseContinue after this decision.
- Execute
service_state = WIFI_SERVICE_IDLE; - Execute
radio_started = false; - Execute
scan_in_progress = false; - Execute
scan_done = false; - Execute
scan_entry_count = 0U; - Execute
connection_state = WIFI_CONNECTION_DISCONNECTED; - Execute
last_error = "";
Read original function source
void wifiServiceRelease(void)
{
if (radio_started) (void)esp_wifi_stop();
(void)esp_event_handler_instance_unregister(WIFI_EVENT, WIFI_EVENT_SCAN_DONE,
scan_event_handler);
(void)esp_event_handler_instance_unregister(WIFI_EVENT, WIFI_EVENT_STA_CONNECTED,
connected_event_handler);
(void)esp_event_handler_instance_unregister(WIFI_EVENT, WIFI_EVENT_STA_DISCONNECTED,
disconnected_event_handler);
(void)esp_event_handler_instance_unregister(IP_EVENT, IP_EVENT_STA_GOT_IP,
got_ip_event_handler);
if (service_state == WIFI_SERVICE_READY) (void)esp_wifi_deinit();
if (station_interface != NULL) {
esp_netif_destroy_default_wifi(station_interface);
station_interface = NULL;
}
service_state = WIFI_SERVICE_IDLE;
radio_started = false;
scan_in_progress = false;
scan_done = false;
scan_entry_count = 0U;
connection_state = WIFI_CONNECTION_DISCONNECTED;
last_error = "";
}wifiServiceState() · source line 242
wifi_service_state_t wifiServiceState(void)
- Exit this function
return service_state;
Read original function source
wifi_service_state_t wifiServiceState(void)
{
return service_state;
}wifiServiceRadioStarted() · source line 247
bool wifiServiceRadioStarted(void)
- Exit this function
return radio_started;
Read original function source
bool wifiServiceRadioStarted(void)
{
return radio_started;
}wifiServiceError() · source line 252
const char *wifiServiceError(void)
- Exit this function
return last_error;
Read original function source
const char *wifiServiceError(void)
{
return last_error;
}Complete original file, including constants and data
/* Coded By JD(2026) */
#error "Reference listing only — not provided as build-ready source."
#include "wifi.h"
#include "esp_err.h"
#include "esp_event.h"
#include "esp_netif.h"
#include "esp_wifi.h"
#include <string.h>
static wifi_service_state_t service_state = WIFI_SERVICE_IDLE;
static esp_netif_t *station_interface;
static const char *last_error = "";
static bool radio_started;
static volatile bool scan_done;
static bool scan_in_progress;
static wifi_scan_entry_t scan_entries[WIFI_SCAN_MAX_NETWORKS];
static size_t scan_entry_count;
static esp_event_handler_instance_t scan_event_handler;
static esp_event_handler_instance_t connected_event_handler;
static esp_event_handler_instance_t disconnected_event_handler;
static esp_event_handler_instance_t got_ip_event_handler;
static wifi_connection_state_t connection_state = WIFI_CONNECTION_DISCONNECTED;
static void wifi_event_handler(void *argument, esp_event_base_t event_base,
int32_t event_id, void *event_data)
{
(void)argument;
(void)event_data;
if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_SCAN_DONE) {
scan_done = true;
} else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED) {
connection_state = WIFI_CONNECTION_FAILED;
last_error = "CONNECTION FAILED";
} else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP) {
connection_state = WIFI_CONNECTION_CONNECTED;
last_error = "";
}
}
static bool accept_already_initialized(esp_err_t result)
{
return result == ESP_OK || result == ESP_ERR_INVALID_STATE;
}
static bool record_error(esp_err_t result)
{
service_state = WIFI_SERVICE_FAILED;
last_error = esp_err_to_name(result);
return false;
}
bool wifiServicePrepare(void)
{
if (service_state == WIFI_SERVICE_READY) return true;
if (service_state == WIFI_SERVICE_FAILED) return false;
const esp_err_t netif_result = esp_netif_init();
if (!accept_already_initialized(netif_result)) return record_error(netif_result);
const esp_err_t event_result = esp_event_loop_create_default();
if (!accept_already_initialized(event_result)) return record_error(event_result);
station_interface = esp_netif_create_default_wifi_sta();
if (station_interface == NULL) {
service_state = WIFI_SERVICE_FAILED;
last_error = "NO STATION INTERFACE";
return false;
}
wifi_init_config_t configuration = WIFI_INIT_CONFIG_DEFAULT();
const esp_err_t init_result = esp_wifi_init(&configuration);
if (init_result != ESP_OK) return record_error(init_result);
const esp_err_t storage_result = esp_wifi_set_storage(WIFI_STORAGE_RAM);
if (storage_result != ESP_OK) return record_error(storage_result);
const esp_err_t mode_result = esp_wifi_set_mode(WIFI_MODE_STA);
if (mode_result != ESP_OK) return record_error(mode_result);
const esp_err_t handler_result = esp_event_handler_instance_register(
WIFI_EVENT, WIFI_EVENT_SCAN_DONE, wifi_event_handler, NULL, &scan_event_handler);
if (handler_result != ESP_OK) return record_error(handler_result);
const esp_err_t connected_handler_result = esp_event_handler_instance_register(
WIFI_EVENT, WIFI_EVENT_STA_CONNECTED, wifi_event_handler, NULL, &connected_event_handler);
if (connected_handler_result != ESP_OK) return record_error(connected_handler_result);
const esp_err_t disconnected_handler_result = esp_event_handler_instance_register(
WIFI_EVENT, WIFI_EVENT_STA_DISCONNECTED, wifi_event_handler, NULL, &disconnected_event_handler);
if (disconnected_handler_result != ESP_OK) return record_error(disconnected_handler_result);
const esp_err_t got_ip_handler_result = esp_event_handler_instance_register(
IP_EVENT, IP_EVENT_STA_GOT_IP, wifi_event_handler, NULL, &got_ip_event_handler);
if (got_ip_handler_result != ESP_OK) return record_error(got_ip_handler_result);
service_state = WIFI_SERVICE_READY;
last_error = "";
return true;
}
bool wifiServiceStart(void)
{
if (!wifiServicePrepare()) return false;
if (radio_started) return true;
const esp_err_t result = esp_wifi_start();
if (result != ESP_OK) {
last_error = esp_err_to_name(result);
return false;
}
radio_started = true;
last_error = "";
return true;
}
bool wifiServiceStop(void)
{
if (!radio_started) return true;
const esp_err_t result = esp_wifi_stop();
if (result != ESP_OK) {
last_error = esp_err_to_name(result);
return false;
}
radio_started = false;
connection_state = WIFI_CONNECTION_DISCONNECTED;
scan_in_progress = false;
scan_done = false;
last_error = "";
return true;
}
bool wifiServiceStartScan(void)
{
if (!radio_started) {
last_error = "RADIO OFF";
return false;
}
if (scan_in_progress) return true;
const wifi_scan_config_t configuration = {0};
const esp_err_t result = esp_wifi_scan_start(&configuration, false);
if (result != ESP_OK) {
last_error = esp_err_to_name(result);
return false;
}
scan_entry_count = 0U;
scan_done = false;
scan_in_progress = true;
last_error = "";
return true;
}
void wifiServiceUpdateScan(void)
{
if (!scan_in_progress || !scan_done) return;
wifi_ap_record_t records[WIFI_SCAN_MAX_NETWORKS] = {0};
uint16_t record_count = WIFI_SCAN_MAX_NETWORKS;
const esp_err_t result = esp_wifi_scan_get_ap_records(&record_count, records);
scan_done = false;
scan_in_progress = false;
if (result != ESP_OK) {
last_error = esp_err_to_name(result);
return;
}
scan_entry_count = record_count;
for (size_t index = 0; index < scan_entry_count; ++index) {
memcpy(scan_entries[index].ssid, records[index].ssid, WIFI_SCAN_SSID_CHARS);
scan_entries[index].ssid[WIFI_SCAN_SSID_CHARS] = '\0';
scan_entries[index].rssi = records[index].rssi;
}
}
bool wifiServiceScanInProgress(void)
{
return scan_in_progress;
}
size_t wifiServiceScanCount(void)
{
return scan_entry_count;
}
const wifi_scan_entry_t *wifiServiceScanEntries(void)
{
return scan_entries;
}
bool wifiServiceConnect(const char *ssid, const char *password)
{
if (ssid == NULL || password == NULL || ssid[0] == '\0' || !wifiServiceStart()) return false;
wifi_config_t configuration = {0};
strncpy((char *)configuration.sta.ssid, ssid, sizeof(configuration.sta.ssid) - 1U);
strncpy((char *)configuration.sta.password, password, sizeof(configuration.sta.password) - 1U);
const esp_err_t config_result = esp_wifi_set_config(WIFI_IF_STA, &configuration);
if (config_result != ESP_OK) {
last_error = esp_err_to_name(config_result);
return false;
}
(void)esp_wifi_disconnect();
const esp_err_t connect_result = esp_wifi_connect();
if (connect_result != ESP_OK) {
last_error = esp_err_to_name(connect_result);
return false;
}
connection_state = WIFI_CONNECTION_CONNECTING;
last_error = "";
return true;
}
wifi_connection_state_t wifiServiceConnectionState(void)
{
return connection_state;
}
void wifiServiceRelease(void)
{
if (radio_started) (void)esp_wifi_stop();
(void)esp_event_handler_instance_unregister(WIFI_EVENT, WIFI_EVENT_SCAN_DONE,
scan_event_handler);
(void)esp_event_handler_instance_unregister(WIFI_EVENT, WIFI_EVENT_STA_CONNECTED,
connected_event_handler);
(void)esp_event_handler_instance_unregister(WIFI_EVENT, WIFI_EVENT_STA_DISCONNECTED,
disconnected_event_handler);
(void)esp_event_handler_instance_unregister(IP_EVENT, IP_EVENT_STA_GOT_IP,
got_ip_event_handler);
if (service_state == WIFI_SERVICE_READY) (void)esp_wifi_deinit();
if (station_interface != NULL) {
esp_netif_destroy_default_wifi(station_interface);
station_interface = NULL;
}
service_state = WIFI_SERVICE_IDLE;
radio_started = false;
scan_in_progress = false;
scan_done = false;
scan_entry_count = 0U;
connection_state = WIFI_CONNECTION_DISCONNECTED;
last_error = "";
}
wifi_service_state_t wifiServiceState(void)
{
return service_state;
}
bool wifiServiceRadioStarted(void)
{
return radio_started;
}
const char *wifiServiceError(void)
{
return last_error;
}