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

ESP32 平台的 WiFi 客户端实现 / WiFi client implementation for ESP32. 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

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 enabled_ = false
 是否启用 / Whether WiFi is enabled
 
bool connected_ = false
 是否连接 / Whether WiFi is connected
 
bool got_ip_ = false
 是否获取 IP / Whether IP is acquired
 
char ip_str_ [16] = {}
 当前 IP 字符串 / Current IP string
 
uint8_t mac_ [6] = {}
 当前 MAC 缓存 / Cached MAC address
 
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 平台的 WiFi 客户端实现 / WiFi client implementation for ESP32.

提供基于 ESP-IDF 的 WiFi 接口实现,包含连接、断开、扫描、RSSI 查询等功能, 继承自抽象类 WifiClient 并实现其所有接口。

Definition at line 18 of file esp_wifi_client.hpp.

Constructor & Destructor Documentation

◆ ESP32WifiClient()

LibXR::ESP32WifiClient::ESP32WifiClient ( )

Definition at line 14 of file esp_wifi_client.cpp.

15{
16 if (!is_initialized_)
17 {
18 esp_netif_init();
19 esp_event_loop_create_default();
20 netif_ = esp_netif_create_default_wifi_sta();
21
22 wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
23 esp_wifi_init(&cfg);
24
25 is_initialized_ = true;
26 }
27}
static bool is_initialized_
ESP 网络是否已初始化 / Netif initialized.
static esp_netif_t * netif_
ESP 默认 netif 对象 / Default netif.

◆ ~ESP32WifiClient()

LibXR::ESP32WifiClient::~ESP32WifiClient ( )
override

Definition at line 29 of file esp_wifi_client.cpp.

30{
31 if (enabled_)
32 {
33 Disable();
34 }
35}
void Disable() override
禁用网络接口(WiFi) / Disable the network interface
bool enabled_
是否启用 / Whether WiFi is enabled

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 89 of file esp_wifi_client.cpp.

90{
92
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);
99 esp_wifi_connect();
100
101 while (semaphore_.Wait(0) == ErrorCode::OK)
102 {
103 }
104
107
109 if (!got_ip_) return WifiError::DHCP_FAILED;
110
111 return WifiError::NONE;
112}
bool connected_
是否连接 / Whether WiFi is connected
LibXR::Semaphore semaphore_
状态同步信号量 / Event wait semaphore
bool got_ip_
是否获取 IP / Whether IP is acquired
ErrorCode Wait(uint32_t timeout=UINT32_MAX)
等待(减少)信号量 Waits (decrements) the semaphore
Definition semaphore.cpp:25
@ DHCP_FAILED
DHCP 获取失败 / DHCP acquisition failed.
@ 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 80 of file esp_wifi_client.cpp.

81{
82 if (!enabled_) return;
83
84 esp_wifi_stop();
85 enabled_ = false;
86 connected_ = false;
87}

◆ Disconnect()

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

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

Returns
WifiError 断开结果 / Error code

Implements LibXR::WifiClient.

Definition at line 114 of file esp_wifi_client.cpp.

115{
116 if (!enabled_) return WifiError::NOT_ENABLED;
117 if (!connected_) return WifiError::NONE;
118
119 while (semaphore_.Wait(0) == ErrorCode::OK)
120 {
121 }
122 esp_wifi_disconnect();
124
125 return WifiError::NONE;
126}

◆ Enable()

bool LibXR::ESP32WifiClient::Enable ( )
overridevirtual

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

Returns
true 表示成功 / true if enabled successfully

Implements LibXR::WifiClient.

Definition at line 37 of file esp_wifi_client.cpp.

38{
39 if (enabled_) return true;
40
41 esp_wifi_set_mode(WIFI_MODE_STA);
42
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,
45 this);
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);
48
49 esp_wifi_start();
50 enabled_ = true;
51
52 wifi_config_t cfg;
53 esp_err_t err = esp_wifi_get_config(WIFI_IF_STA, &cfg);
54 if (err == ESP_OK && cfg.sta.ssid[0] != 0)
55 {
56 esp_wifi_connect();
58 if (!connected_) return true;
59
61 if (!got_ip_) return true;
62 }
63
64 wifi_ap_record_t ap_info;
65 if (esp_wifi_sta_get_ap_info(&ap_info) == ESP_OK)
66 {
67 connected_ = true;
68 got_ip_ = true;
69
70 esp_netif_ip_info_t ip_info;
71 if (esp_netif_get_ip_info(netif_, &ip_info) == ESP_OK)
72 {
73 esp_ip4addr_ntoa(&ip_info.ip, ip_str_, sizeof(ip_str_));
74 }
75 }
76
77 return true;
78}
static void EventHandler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data)
事件处理回调 / Event handler callback
char ip_str_[16]
当前 IP 字符串 / Current IP string

◆ 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 175 of file esp_wifi_client.cpp.

177{
178 auto* self = static_cast<ESP32WifiClient*>(arg);
179
180 if (event_base == WIFI_EVENT)
181 {
182 switch (event_id)
183 {
184 case WIFI_EVENT_STA_CONNECTED:
185 self->connected_ = true;
186 self->semaphore_.Post();
187 break;
188 case WIFI_EVENT_STA_DISCONNECTED:
189 self->connected_ = false;
190 self->semaphore_.Post();
191 break;
192 default:
193 break;
194 }
195 }
196
197 if (event_base == IP_EVENT)
198 {
199 switch (event_id)
200 {
201 case IP_EVENT_STA_GOT_IP:
202 {
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();
207 break;
208 }
209 case IP_EVENT_STA_LOST_IP:
210 self->got_ip_ = false;
211 break;
212 }
213 }
214}

◆ 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 130 of file esp_wifi_client.cpp.

131{
132 return IPAddressRaw::FromString(ip_str_);
133}

◆ 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 135 of file esp_wifi_client.cpp.

136{
137 uint8_t mac[6] = {};
138 esp_wifi_get_mac(WIFI_IF_STA, mac);
139 MACAddressRaw result;
140 std::memcpy(result.bytes, mac, 6);
141 return result;
142}

◆ 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 216 of file esp_wifi_client.cpp.

217{
218 if (!connected_) return -127;
219
220 wifi_ap_record_t ap_info;
221 if (esp_wifi_sta_get_ap_info(&ap_info) == ESP_OK)
222 {
223 return ap_info.rssi;
224 }
225 return -127;
226}

◆ IsConnected()

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

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

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

Implements LibXR::WifiClient.

Definition at line 128 of file esp_wifi_client.cpp.

128{ return connected_; }

◆ 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 144 of file esp_wifi_client.cpp.

146{
147 wifi_scan_config_t scan_config = {};
148 if (esp_wifi_scan_start(&scan_config, true) != ESP_OK)
149 {
151 }
152
153 uint16_t ap_num = 0;
154 esp_wifi_scan_get_ap_num(&ap_num);
155 if (ap_num > max_count) ap_num = max_count;
156
157 wifi_ap_record_t ap_records[20] = {};
158 esp_wifi_scan_get_ap_records(&ap_num, ap_records);
159
160 out_found = ap_num;
161 for (int i = 0; i < ap_num; ++i)
162 {
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;
166 out_list[i].security = (ap_records[i].authmode == WIFI_AUTH_OPEN) ? Security::OPEN
167 : (ap_records[i].authmode == WIFI_AUTH_WPA2_PSK)
169 : Security::UNKNOWN;
170 }
171
172 return WifiError::NONE;
173}
Security
WiFi 安全类型 / WiFi security types.
@ WPA2_PSK
WPA2-PSK / WPA2-PSK.
@ OPEN
开放网络 / Open network
@ UNKNOWN
未知错误 / Unknown error
@ SCAN_FAILED
扫描失败 / Scan failed

Field Documentation

◆ connected_

bool LibXR::ESP32WifiClient::connected_ = false
private

是否连接 / Whether WiFi is connected

Definition at line 59 of file esp_wifi_client.hpp.

◆ enabled_

bool LibXR::ESP32WifiClient::enabled_ = false
private

是否启用 / Whether WiFi is enabled

Definition at line 58 of file esp_wifi_client.hpp.

◆ got_ip_

bool LibXR::ESP32WifiClient::got_ip_ = false
private

是否获取 IP / Whether IP is acquired

Definition at line 60 of file esp_wifi_client.hpp.

◆ ip_str_

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

当前 IP 字符串 / Current IP string

Definition at line 61 of file esp_wifi_client.hpp.

61{};

◆ is_initialized_

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

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

Definition at line 54 of file esp_wifi_client.hpp.

◆ mac_

uint8_t LibXR::ESP32WifiClient::mac_[6] = {}
private

当前 MAC 缓存 / Cached MAC address

Definition at line 62 of file esp_wifi_client.hpp.

62{};

◆ netif_

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

ESP 默认 netif 对象 / Default netif.

Definition at line 56 of file esp_wifi_client.hpp.

◆ semaphore_

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

状态同步信号量 / Event wait semaphore

Definition at line 64 of file esp_wifi_client.hpp.


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