1#include "esp_wifi_client.hpp"
14ESP32WifiClient::ESP32WifiClient()
19 esp_event_loop_create_default();
20 netif_ = esp_netif_create_default_wifi_sta();
22 wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
29ESP32WifiClient::~ESP32WifiClient()
41 esp_wifi_set_mode(WIFI_MODE_STA);
43 esp_event_handler_register(WIFI_EVENT, WIFI_EVENT_STA_CONNECTED, &
EventHandler,
this);
44 esp_event_handler_register(WIFI_EVENT, WIFI_EVENT_STA_DISCONNECTED, &
EventHandler,
46 esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &
EventHandler,
this);
47 esp_event_handler_register(IP_EVENT, IP_EVENT_STA_LOST_IP, &
EventHandler,
this);
53 esp_err_t err = esp_wifi_get_config(WIFI_IF_STA, &cfg);
54 if (err == ESP_OK && cfg.sta.ssid[0] != 0)
64 wifi_ap_record_t ap_info;
65 if (esp_wifi_sta_get_ap_info(&ap_info) == ESP_OK)
70 esp_netif_ip_info_t ip_info;
71 if (esp_netif_get_ip_info(
netif_, &ip_info) == ESP_OK)
93 wifi_config_t wifi_config{};
94 std::strncpy(
reinterpret_cast<char*
>(wifi_config.sta.ssid), config.
ssid,
95 sizeof(wifi_config.sta.ssid));
96 std::strncpy(
reinterpret_cast<char*
>(wifi_config.sta.password), config.
password,
97 sizeof(wifi_config.sta.password));
98 esp_wifi_set_config(WIFI_IF_STA, &wifi_config);
122 esp_wifi_disconnect();
132 return IPAddressRaw::FromString(
ip_str_);
138 esp_wifi_get_mac(WIFI_IF_STA, mac);
140 std::memcpy(result.bytes, mac, 6);
147 wifi_scan_config_t scan_config = {};
148 if (esp_wifi_scan_start(&scan_config,
true) != ESP_OK)
154 esp_wifi_scan_get_ap_num(&ap_num);
155 if (ap_num > max_count) ap_num = max_count;
157 wifi_ap_record_t ap_records[20] = {};
158 esp_wifi_scan_get_ap_records(&ap_num, ap_records);
161 for (
int i = 0; i < ap_num; ++i)
163 std::strncpy(out_list[i].ssid,
reinterpret_cast<const char*
>(ap_records[i].ssid),
164 sizeof(out_list[i].ssid));
165 out_list[i].
rssi = ap_records[i].rssi;
167 : (ap_records[i].authmode == WIFI_AUTH_WPA2_PSK)
176 int32_t event_id,
void* event_data)
180 if (event_base == WIFI_EVENT)
184 case WIFI_EVENT_STA_CONNECTED:
185 self->connected_ =
true;
186 self->semaphore_.Post();
188 case WIFI_EVENT_STA_DISCONNECTED:
189 self->connected_ =
false;
190 self->semaphore_.Post();
197 if (event_base == IP_EVENT)
201 case IP_EVENT_STA_GOT_IP:
203 ip_event_got_ip_t*
event =
reinterpret_cast<ip_event_got_ip_t*
>(event_data);
204 self->got_ip_ =
true;
205 esp_ip4addr_ntoa(&event->ip_info.ip, self->ip_str_,
sizeof(self->ip_str_));
206 self->semaphore_.Post();
209 case IP_EVENT_STA_LOST_IP:
210 self->got_ip_ =
false;
220 wifi_ap_record_t ap_info;
221 if (esp_wifi_sta_get_ap_info(&ap_info) == ESP_OK)
ESP32 平台的 WiFi 客户端实现 / WiFi client implementation for ESP32.
MACAddressRaw GetMACAddress() const override
获取当前 MAC 地址 / Get MAC address
static bool is_initialized_
ESP 网络是否已初始化 / Netif initialized.
bool connected_
是否连接 / Whether WiFi is connected
IPAddressRaw GetIPAddress() const override
获取当前 IP 地址 / Get current IP address
LibXR::Semaphore semaphore_
状态同步信号量 / Event wait semaphore
WifiError Connect(const Config &config) override
连接到指定 WiFi 网络 / Connect to a WiFi network
void Disable() override
禁用网络接口(WiFi) / Disable the network interface
static esp_netif_t * netif_
ESP 默认 netif 对象 / Default netif.
bool enabled_
是否启用 / Whether WiFi is enabled
int GetRSSI() const override
获取当前 WiFi 信号强度(RSSI) / Get current signal strength
bool got_ip_
是否获取 IP / Whether IP is acquired
bool Enable() override
启用网络接口(WiFi) / Enable the network interface
WifiError Disconnect() override
断开当前 WiFi 连接 / Disconnect from the WiFi network
bool IsConnected() const override
检查是否已连接 / Check if currently connected
static void EventHandler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data)
事件处理回调 / Event handler callback
WifiError Scan(ScanResult *out_list, size_t max_count, size_t &out_found) override
扫描可用网络 / Scan for available WiFi networks
char ip_str_[16]
当前 IP 字符串 / Current IP string
ErrorCode Wait(uint32_t timeout=UINT32_MAX)
等待(减少)信号量 Waits (decrements) the semaphore
@ WPA2_PSK
WPA2-PSK / WPA2-PSK.
@ UNKNOWN
未知类型 / Unknown type
@ OPEN
开放网络 / Open network
WifiError
WiFi 错误码 / Enumeration of WiFi error codes.
@ DHCP_FAILED
DHCP 获取失败 / DHCP acquisition failed.
@ SCAN_FAILED
扫描失败 / Scan failed
@ NOT_ENABLED
未启用 / Not enabled
@ CONNECTION_TIMEOUT
连接超时 / Connection timeout
原始 IPv4 地址 / Raw IPv4 address
原始 MAC 地址 / Raw MAC address
WiFi 连接配置 / WiFi connection configuration.
char ssid[33]
SSID 名称 / SSID name.
char password[64]
密码 / Password
WiFi 扫描结果 / WiFi scan result.
int rssi
信号强度 / Signal strength (RSSI)
Security security
安全类型 / Security type