libxr  1.0
Want to be the best embedded framework
Loading...
Searching...
No Matches
LibXR::ESP32WifiClient Class Reference

ESP32 Wi-Fi 客户端实现 / ESP32 Wi-Fi client implementation. More...

#include <esp_wifi_client.hpp>

Inheritance diagram for LibXR::ESP32WifiClient:
[legend]
Collaboration diagram for LibXR::ESP32WifiClient:
[legend]

Public Member Functions

bool Enable () override
 启用网络接口(WiFi) / Enable the network interface
 
void Disable () override
 禁用网络接口(WiFi) / Disable the network interface
 
WifiError Connect (const Config &config) override
 连接到指定 WiFi 网络 / Connect to a WiFi network
 
WifiError Disconnect () override
 断开当前 WiFi 连接 / Disconnect from the WiFi network
 
bool IsConnected () const override
 检查是否已连接 / Check if currently connected
 
IPAddressRaw GetIPAddress () const override
 获取当前 IP 地址 / Get current IP address
 
MACAddressRaw GetMACAddress () const override
 获取当前 MAC 地址 / Get MAC address
 
WifiError Scan (ScanResult *out_list, size_t max_count, size_t &out_found) override
 扫描可用网络 / Scan for available WiFi networks
 
int GetRSSI () const override
 获取当前 WiFi 信号强度(RSSI) / Get current signal strength
 
- Public Member Functions inherited from LibXR::WifiClient
virtual ~WifiClient ()=default
 析构函数 / Destructor
 
- Public Member Functions inherited from LibXR::NetworkInterface

Private Member Functions

bool RegisterHandlers ()
 
void UnregisterHandlers ()
 
void ResetConnectionState ()
 
void DrainEvents ()
 

Static Private Member Functions

static void EventHandler (void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data)
 事件处理回调 / Event handler callback
 

Private Attributes

bool init_ok_ = false
 
bool enabled_ = false
 是否启用 / Whether WiFi is enabled
 
bool handlers_registered_ = false
 
bool connected_ = false
 是否连接 / Whether WiFi is connected
 
bool got_ip_ = false
 是否获取 IP / Whether IP is acquired
 
char ip_str_ [16] = {}
 当前 IP 字符串 / Current IP string
 
esp_event_handler_instance_t wifi_connected_handler_ = nullptr
 
esp_event_handler_instance_t wifi_disconnected_handler_ = nullptr
 
esp_event_handler_instance_t got_ip_handler_ = nullptr
 
esp_event_handler_instance_t lost_ip_handler_ = nullptr
 
LibXR::Semaphore semaphore_
 状态同步信号量 / Event wait semaphore
 

Static Private Attributes

static bool is_initialized_
 ESP 网络是否已初始化 / Netif initialized.
 
static esp_netif_t * netif_ = nullptr
 ESP 默认 netif 对象 / Default netif.
 

Additional Inherited Members

- Public Types inherited from LibXR::WifiClient
enum class  WifiError {
  NONE , ALREADY_ENABLED , NOT_ENABLED , CONNECTION_TIMEOUT ,
  AUTHENTICATION_FAILED , DHCP_FAILED , SSID_NOT_FOUND , INVALID_CONFIG ,
  HARDWARE_FAILURE , SCAN_FAILED , UNKNOWN
}
 WiFi 错误码 / Enumeration of WiFi error codes. More...
 
enum class  Security { OPEN , WPA2_PSK , WPA2_ENTERPRISE , UNKNOWN }
 WiFi 安全类型 / WiFi security types. More...
 
using WifiCallback = LibXR::Callback<WifiError>
 WiFi 状态回调类型 / Callback type for WiFi status.
 

Detailed Description

ESP32 Wi-Fi 客户端实现 / ESP32 Wi-Fi client implementation.

基于 ESP-IDF 提供连接、断开、扫描、RSSI 查询等接口。 Provides connect/disconnect/scan/RSSI operations via ESP-IDF.

Definition at line 19 of file esp_wifi_client.hpp.

Constructor & Destructor Documentation

◆ ESP32WifiClient()

LibXR::ESP32WifiClient::ESP32WifiClient ( )

Definition at line 92 of file esp_wifi_client.cpp.

93{
95 {
96 init_ok_ = true;
97 return;
98 }
99
100 esp_err_t err = nvs_flash_init();
101 if ((err == ESP_ERR_NVS_NO_FREE_PAGES) || (err == ESP_ERR_NVS_NEW_VERSION_FOUND))
102 {
103 if (nvs_flash_erase() != ESP_OK)
104 {
105 return;
106 }
107 err = nvs_flash_init();
108 }
109 if (err != ESP_OK)
110 {
111 return;
112 }
113
114 err = esp_netif_init();
115 if ((err != ESP_OK) && (err != ESP_ERR_INVALID_STATE))
116 {
117 return;
118 }
119
120 err = esp_event_loop_create_default();
121 if ((err != ESP_OK) && (err != ESP_ERR_INVALID_STATE))
122 {
123 return;
124 }
125
126 netif_ = esp_netif_create_default_wifi_sta();
127 if (netif_ == nullptr)
128 {
129 return;
130 }
131
132 wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
133 err = esp_wifi_init(&cfg);
134 if ((err != ESP_OK) && (err != ESP_ERR_INVALID_STATE))
135 {
136 return;
137 }
138
139 is_initialized_ = true;
140 init_ok_ = true;
141}
static bool is_initialized_
ESP 网络是否已初始化 / Netif initialized.
static esp_netif_t * netif_
ESP 默认 netif 对象 / Default netif.

Member Function Documentation

◆ Connect()

WifiClient::WifiError LibXR::ESP32WifiClient::Connect ( const Config & config)
overridevirtual

连接到指定 WiFi 网络 / Connect to a WiFi network

Parameters
[in]configWiFi 连接配置 / Configuration parameters
Returns
WifiError 连接结果 / Error code

Implements LibXR::WifiClient.

Definition at line 184 of file esp_wifi_client.cpp.

185{
186 if (!enabled_) return WifiError::NOT_ENABLED;
187
188 ResetConnectionState();
189 DrainEvents();
190
191 wifi_config_t wifi_config{};
192 CopyToWifiField(wifi_config.sta.ssid, sizeof(wifi_config.sta.ssid), config.ssid);
193 CopyToWifiField(wifi_config.sta.password, sizeof(wifi_config.sta.password),
194 config.password);
195 if (esp_wifi_set_config(WIFI_IF_STA, &wifi_config) != ESP_OK)
196 {
198 }
199 if (esp_wifi_connect() != ESP_OK)
200 {
202 }
203
204 if (!WaitForPredicate(semaphore_, WIFI_CONNECT_TIMEOUT_MS,
205 [&]() { return connected_; }))
206 {
208 }
209
210 if (!WaitForPredicate(semaphore_, WIFI_DHCP_TIMEOUT_MS,
211 [&]() { return got_ip_ || !connected_; }))
212 {
214 }
216 if (!got_ip_) return WifiError::DHCP_FAILED;
217
218 return WifiError::NONE;
219}
bool connected_
是否连接 / Whether WiFi is connected
LibXR::Semaphore semaphore_
状态同步信号量 / Event wait semaphore
bool enabled_
是否启用 / Whether WiFi is enabled
bool got_ip_
是否获取 IP / Whether IP is acquired
@ DHCP_FAILED
DHCP 获取失败 / DHCP acquisition failed.
@ INVALID_CONFIG
配置无效 / Invalid configuration
@ HARDWARE_FAILURE
硬件故障 / Hardware failure
@ NOT_ENABLED
未启用 / Not enabled
@ NONE
无错误 / No error
@ CONNECTION_TIMEOUT
连接超时 / Connection timeout

◆ Disable()

void LibXR::ESP32WifiClient::Disable ( )
overridevirtual

禁用网络接口(WiFi) / Disable the network interface

Implements LibXR::WifiClient.

Definition at line 174 of file esp_wifi_client.cpp.

175{
176 if (!enabled_) return;
177
178 (void)esp_wifi_stop();
179 enabled_ = false;
180 ResetConnectionState();
181 UnregisterHandlers();
182}

◆ Disconnect()

WifiClient::WifiError LibXR::ESP32WifiClient::Disconnect ( )
overridevirtual

断开当前 WiFi 连接 / Disconnect from the WiFi network

Returns
WifiError 断开结果 / Error code

Implements LibXR::WifiClient.

Definition at line 221 of file esp_wifi_client.cpp.

222{
223 if (!enabled_) return WifiError::NOT_ENABLED;
224 if (!connected_) return WifiError::NONE;
225
226 DrainEvents();
227 if (esp_wifi_disconnect() != ESP_OK)
228 {
230 }
231
232 if (!WaitForPredicate(semaphore_, WIFI_DISCONNECT_TIMEOUT_MS,
233 [&]() { return !connected_; }))
234 {
235 return WifiError::UNKNOWN;
236 }
237
238 return WifiError::NONE;
239}
@ UNKNOWN
未知错误 / Unknown error

◆ DrainEvents()

void LibXR::ESP32WifiClient::DrainEvents ( )
private

Definition at line 442 of file esp_wifi_client.cpp.

443{
444 while (semaphore_.Wait(0) == ErrorCode::OK)
445 {
446 }
447}
ErrorCode Wait(uint32_t timeout=UINT32_MAX)
等待(减少)信号量 Waits (decrements) the semaphore
Definition semaphore.cpp:53
@ OK
操作成功 | Operation successful

◆ Enable()

bool LibXR::ESP32WifiClient::Enable ( )
overridevirtual

启用网络接口(WiFi) / Enable the network interface

Returns
true 表示成功 / true if enabled successfully

Implements LibXR::WifiClient.

Definition at line 143 of file esp_wifi_client.cpp.

144{
145 if (enabled_) return true;
146
147 if (!init_ok_)
148 {
149 return false;
150 }
151
152 if (!RegisterHandlers())
153 {
154 return false;
155 }
156
157 if (esp_wifi_set_mode(WIFI_MODE_STA) != ESP_OK)
158 {
159 UnregisterHandlers();
160 return false;
161 }
162
163 ResetConnectionState();
164 DrainEvents();
165 if (esp_wifi_start() != ESP_OK)
166 {
167 UnregisterHandlers();
168 return false;
169 }
170 enabled_ = true;
171 return true;
172}

◆ EventHandler()

void LibXR::ESP32WifiClient::EventHandler ( void * arg,
esp_event_base_t event_base,
int32_t event_id,
void * event_data )
staticprivate

事件处理回调 / Event handler callback

Parameters
arg用户数据 / User pointer
event_base事件域 / Event base type
event_id事件编号 / Event ID
event_data附加数据 / Event payload

Definition at line 314 of file esp_wifi_client.cpp.

316{
317 auto* self = static_cast<ESP32WifiClient*>(arg);
318
319 if (event_base == WIFI_EVENT)
320 {
321 switch (event_id)
322 {
323 case WIFI_EVENT_STA_CONNECTED:
324 self->connected_ = true;
325 self->semaphore_.Post();
326 break;
327 case WIFI_EVENT_STA_DISCONNECTED:
328 self->connected_ = false;
329 self->got_ip_ = false;
330 self->ip_str_[0] = '\0';
331 self->semaphore_.Post();
332 break;
333 default:
334 break;
335 }
336 }
337
338 if (event_base == IP_EVENT)
339 {
340 switch (event_id)
341 {
342 case IP_EVENT_STA_GOT_IP:
343 {
344 ip_event_got_ip_t* event = reinterpret_cast<ip_event_got_ip_t*>(event_data);
345 self->got_ip_ = true;
346 esp_ip4addr_ntoa(&event->ip_info.ip, self->ip_str_, sizeof(self->ip_str_));
347 self->semaphore_.Post();
348 break;
349 }
350 case IP_EVENT_STA_LOST_IP:
351 self->got_ip_ = false;
352 self->ip_str_[0] = '\0';
353 self->semaphore_.Post();
354 break;
355 default:
356 break;
357 }
358 }
359}

◆ GetIPAddress()

IPAddressRaw LibXR::ESP32WifiClient::GetIPAddress ( ) const
overridevirtual

获取当前 IP 地址 / Get current IP address

Returns
当前 IP 地址(结构形式) / Current IP address in raw form

Implements LibXR::WifiClient.

Definition at line 243 of file esp_wifi_client.cpp.

244{
245 return IPAddressRaw::FromString(ip_str_);
246}
char ip_str_[16]
当前 IP 字符串 / Current IP string

◆ GetMACAddress()

MACAddressRaw LibXR::ESP32WifiClient::GetMACAddress ( ) const
overridevirtual

获取当前 MAC 地址 / Get MAC address

Returns
当前 MAC 地址(结构形式) / Current MAC address in raw form

Implements LibXR::WifiClient.

Definition at line 248 of file esp_wifi_client.cpp.

249{
250 uint8_t mac[6] = {};
251 esp_wifi_get_mac(WIFI_IF_STA, mac);
252 MACAddressRaw result;
253 std::memcpy(result.bytes, mac, 6);
254 return result;
255}

◆ GetRSSI()

int LibXR::ESP32WifiClient::GetRSSI ( ) const
overridevirtual

获取当前 WiFi 信号强度(RSSI) / Get current signal strength

Returns
信号强度(dBm) / Signal strength in dBm

Implements LibXR::WifiClient.

Definition at line 361 of file esp_wifi_client.cpp.

362{
363 if (!connected_) return -127;
364
365 wifi_ap_record_t ap_info;
366 if (esp_wifi_sta_get_ap_info(&ap_info) == ESP_OK)
367 {
368 return ap_info.rssi;
369 }
370 return -127;
371}

◆ IsConnected()

bool LibXR::ESP32WifiClient::IsConnected ( ) const
overridevirtual

检查是否已连接 / Check if currently connected

Returns
true 如果连接上了 / true if connected

Implements LibXR::WifiClient.

Definition at line 241 of file esp_wifi_client.cpp.

241{ return connected_; }

◆ RegisterHandlers()

bool LibXR::ESP32WifiClient::RegisterHandlers ( )
private

Definition at line 373 of file esp_wifi_client.cpp.

374{
375 if (handlers_registered_)
376 {
377 return true;
378 }
379
380 if ((esp_event_handler_instance_register(WIFI_EVENT, WIFI_EVENT_STA_CONNECTED,
381 &EventHandler, this,
382 &wifi_connected_handler_) != ESP_OK) ||
383 (esp_event_handler_instance_register(WIFI_EVENT, WIFI_EVENT_STA_DISCONNECTED,
384 &EventHandler, this,
385 &wifi_disconnected_handler_) != ESP_OK) ||
386 (esp_event_handler_instance_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &EventHandler,
387 this, &got_ip_handler_) != ESP_OK) ||
388 (esp_event_handler_instance_register(IP_EVENT, IP_EVENT_STA_LOST_IP, &EventHandler,
389 this, &lost_ip_handler_) != ESP_OK))
390 {
391 UnregisterHandlers();
392 return false;
393 }
394
395 handlers_registered_ = true;
396 return true;
397}
static void EventHandler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data)
事件处理回调 / Event handler callback

◆ ResetConnectionState()

void LibXR::ESP32WifiClient::ResetConnectionState ( )
private

Definition at line 435 of file esp_wifi_client.cpp.

436{
437 connected_ = false;
438 got_ip_ = false;
439 ip_str_[0] = '\0';
440}

◆ Scan()

WifiClient::WifiError LibXR::ESP32WifiClient::Scan ( ScanResult * out_list,
size_t max_count,
size_t & out_found )
overridevirtual

扫描可用网络 / Scan for available WiFi networks

Parameters
[out]out_list扫描结果数组 / Output list buffer
[in]max_count最大可填入数量 / Max result count
[out]out_found实际找到数量 / Number found
Returns
WifiError 错误码 / Scan result code

Implements LibXR::WifiClient.

Definition at line 257 of file esp_wifi_client.cpp.

259{
260 out_found = 0;
261 if (!enabled_)
262 {
264 }
265 if ((out_list == nullptr) && (max_count != 0U))
266 {
268 }
269
270 wifi_scan_config_t scan_config = {};
271 if (esp_wifi_scan_start(&scan_config, true) != ESP_OK)
272 {
274 }
275
276 uint16_t ap_num = 0;
277 if (esp_wifi_scan_get_ap_num(&ap_num) != ESP_OK)
278 {
280 }
281
282 uint16_t copy_count = ap_num;
283 if (copy_count > max_count)
284 {
285 copy_count = static_cast<uint16_t>(max_count);
286 }
287 if (copy_count > MAX_SCAN_RESULTS)
288 {
289 copy_count = MAX_SCAN_RESULTS;
290 }
291
292 wifi_ap_record_t ap_records[MAX_SCAN_RESULTS] = {};
293 if ((copy_count > 0U) &&
294 (esp_wifi_scan_get_ap_records(&copy_count, ap_records) != ESP_OK))
295 {
297 }
298
299 out_found = copy_count;
300 for (uint16_t i = 0; i < copy_count; ++i)
301 {
302 CopyToCharField(out_list[i].ssid, sizeof(out_list[i].ssid),
303 reinterpret_cast<const char*>(ap_records[i].ssid));
304 out_list[i].rssi = ap_records[i].rssi;
305 out_list[i].security = (ap_records[i].authmode == WIFI_AUTH_OPEN) ? Security::OPEN
306 : (ap_records[i].authmode == WIFI_AUTH_WPA2_PSK)
308 : Security::UNKNOWN;
309 }
310
311 return WifiError::NONE;
312}
Security
WiFi 安全类型 / WiFi security types.
@ WPA2_PSK
WPA2-PSK / WPA2-PSK.
@ OPEN
开放网络 / Open network
@ SCAN_FAILED
扫描失败 / Scan failed

◆ UnregisterHandlers()

void LibXR::ESP32WifiClient::UnregisterHandlers ( )
private

Definition at line 399 of file esp_wifi_client.cpp.

400{
401 if ((wifi_connected_handler_ == nullptr) && (wifi_disconnected_handler_ == nullptr) &&
402 (got_ip_handler_ == nullptr) && (lost_ip_handler_ == nullptr))
403 {
404 handlers_registered_ = false;
405 return;
406 }
407
408 if (wifi_connected_handler_ != nullptr)
409 {
410 (void)esp_event_handler_instance_unregister(WIFI_EVENT, WIFI_EVENT_STA_CONNECTED,
411 wifi_connected_handler_);
412 wifi_connected_handler_ = nullptr;
413 }
414 if (wifi_disconnected_handler_ != nullptr)
415 {
416 (void)esp_event_handler_instance_unregister(WIFI_EVENT, WIFI_EVENT_STA_DISCONNECTED,
417 wifi_disconnected_handler_);
418 wifi_disconnected_handler_ = nullptr;
419 }
420 if (got_ip_handler_ != nullptr)
421 {
422 (void)esp_event_handler_instance_unregister(IP_EVENT, IP_EVENT_STA_GOT_IP,
423 got_ip_handler_);
424 got_ip_handler_ = nullptr;
425 }
426 if (lost_ip_handler_ != nullptr)
427 {
428 (void)esp_event_handler_instance_unregister(IP_EVENT, IP_EVENT_STA_LOST_IP,
429 lost_ip_handler_);
430 lost_ip_handler_ = nullptr;
431 }
432 handlers_registered_ = false;
433}

Field Documentation

◆ connected_

bool LibXR::ESP32WifiClient::connected_ = false
private

是否连接 / Whether WiFi is connected

Definition at line 66 of file esp_wifi_client.hpp.

◆ enabled_

bool LibXR::ESP32WifiClient::enabled_ = false
private

是否启用 / Whether WiFi is enabled

Definition at line 64 of file esp_wifi_client.hpp.

◆ got_ip_

bool LibXR::ESP32WifiClient::got_ip_ = false
private

是否获取 IP / Whether IP is acquired

Definition at line 67 of file esp_wifi_client.hpp.

◆ got_ip_handler_

esp_event_handler_instance_t LibXR::ESP32WifiClient::got_ip_handler_ = nullptr
private

Definition at line 71 of file esp_wifi_client.hpp.

◆ handlers_registered_

bool LibXR::ESP32WifiClient::handlers_registered_ = false
private

Definition at line 65 of file esp_wifi_client.hpp.

◆ init_ok_

bool LibXR::ESP32WifiClient::init_ok_ = false
private

Definition at line 63 of file esp_wifi_client.hpp.

◆ ip_str_

char LibXR::ESP32WifiClient::ip_str_[16] = {}
private

当前 IP 字符串 / Current IP string

Definition at line 68 of file esp_wifi_client.hpp.

68{};

◆ is_initialized_

bool LibXR::ESP32WifiClient::is_initialized_
inlinestaticprivate
Initial value:
=
false

ESP 网络是否已初始化 / Netif initialized.

Definition at line 59 of file esp_wifi_client.hpp.

◆ lost_ip_handler_

esp_event_handler_instance_t LibXR::ESP32WifiClient::lost_ip_handler_ = nullptr
private

Definition at line 72 of file esp_wifi_client.hpp.

◆ netif_

esp_netif_t* LibXR::ESP32WifiClient::netif_ = nullptr
inlinestaticprivate

ESP 默认 netif 对象 / Default netif.

Definition at line 61 of file esp_wifi_client.hpp.

◆ semaphore_

LibXR::Semaphore LibXR::ESP32WifiClient::semaphore_
private

状态同步信号量 / Event wait semaphore

Definition at line 74 of file esp_wifi_client.hpp.

◆ wifi_connected_handler_

esp_event_handler_instance_t LibXR::ESP32WifiClient::wifi_connected_handler_ = nullptr
private

Definition at line 69 of file esp_wifi_client.hpp.

◆ wifi_disconnected_handler_

esp_event_handler_instance_t LibXR::ESP32WifiClient::wifi_disconnected_handler_ = nullptr
private

Definition at line 70 of file esp_wifi_client.hpp.


The documentation for this class was generated from the following files: