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

Public Member Functions

WifiError Enable () override
 启用 WiFi 模块 / Enable the WiFi module
 
WifiError Disable () override
 禁用 WiFi 模块 / Disable the WiFi module
 
WifiError Connect (const Config &config) override
 连接到 WiFi 网络 / Connect to a WiFi network
 
WifiError Disconnect () override
 断开当前 WiFi 连接 / Disconnect from the current WiFi connection
 
bool IsConnected () const override
 检查是否已连接 / Check if currently connected
 
const charGetIPAddress () const override
 获取当前 IP 地址 / Get the current IP address
 
WifiError Scan (std::vector< ScanResult > &results) override
 扫描可用 WiFi 网络 / Scan for available WiFi networks
 
int GetRSSI () const override
 获取当前 WiFi 信号强度 / Get the current WiFi signal strength
 

Static Private Member Functions

static void EventHandler (void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data)
 

Private Attributes

bool enabled_ = false
 
bool connected_ = false
 
bool got_ip_ = false
 
char ip_str_ [IP4ADDR_STRLEN_MAX] = {}
 
LibXR::Semaphore semaphore_
 

Static Private Attributes

static bool is_initialized_ = false
 
static esp_netif_tnetif_ = nullptr
 

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

Definition at line 11 of file esp_wifi_client.hpp.

Constructor & Destructor Documentation

◆ ESP32WifiClient()

LibXR::ESP32WifiClient::ESP32WifiClient ( )

Definition at line 15 of file esp_wifi_client.cpp.

16{
17 if (!is_initialized_)
18 {
22
25
26 is_initialized_ = true;
27 }
28}
constexpr auto min(T1 a, T2 b) -> typename std::common_type< T1, T2 >::type
计算两个数的最小值

◆ ~ESP32WifiClient()

LibXR::ESP32WifiClient::~ESP32WifiClient ( )
override

Definition at line 30 of file esp_wifi_client.cpp.

31{
32 if (enabled_)
33 {
34 Disable();
35 }
36}
WifiError Disable() override
禁用 WiFi 模块 / Disable the WiFi module

Member Function Documentation

◆ Connect()

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

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

Parameters
config配置参数 / Configuration parameters

Implements LibXR::WifiClient.

Definition at line 64 of file esp_wifi_client.cpp.

65{
66 if (!enabled_) return WifiError::NOT_ENABLED;
67
69 std::strncpy(reinterpret_cast<char*>(wifi_config.sta.ssid), config.ssid.c_str(),
70 sizeof(wifi_config.sta.ssid));
71 std::strncpy(reinterpret_cast<char*>(wifi_config.sta.password), config.password.c_str(),
72 sizeof(wifi_config.sta.password));
75
76 while (semaphore_.Wait(0) == ErrorCode::OK)
77 {
78 }
79
80 semaphore_.Wait();
81 if (!connected_)
82 {
84 }
85
86 semaphore_.Wait();
87 if (!got_ip_)
88 {
90 }
91
92 return WifiError::NONE;
93}
ErrorCode Wait(uint32_t timeout=UINT32_MAX)
等待(减少)信号量 Waits (decrements) the semaphore
Definition semaphore.cpp:15
@ DHCP_FAILED
DHCP 获取失败 / DHCP acquisition failed.
@ NOT_ENABLED
未启用 / Not enabled
@ NONE
无错误 / No error
@ CONNECTION_TIMEOUT
连接超时 / Connection timeout

◆ Disable()

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

禁用 WiFi 模块 / Disable the WiFi module

Implements LibXR::WifiClient.

Definition at line 54 of file esp_wifi_client.cpp.

55{
56 if (!enabled_) return WifiError::NOT_ENABLED;
57
59 enabled_ = false;
60 connected_ = false;
61 return WifiError::NONE;
62}

◆ Disconnect()

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

断开当前 WiFi 连接 / Disconnect from the current WiFi connection

Implements LibXR::WifiClient.

Definition at line 95 of file esp_wifi_client.cpp.

96{
97 if (!enabled_)
98 {
100 }
101 if (!connected_)
102 {
103 return WifiError::NONE;
104 }
105 while (semaphore_.Wait(0) == ErrorCode::OK)
106 {
107 }
109 semaphore_.Wait();
110 return WifiError::NONE;
111}

◆ Enable()

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

启用 WiFi 模块 / Enable the WiFi module

Implements LibXR::WifiClient.

Definition at line 38 of file esp_wifi_client.cpp.

39{
40 if (enabled_) return WifiError::ALREADY_ENABLED;
41
44 enabled_ = true;
45
48 this);
51 return WifiError::NONE;
52}
@ ALREADY_ENABLED
已启用 / Already enabled

◆ EventHandler()

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

Definition at line 149 of file esp_wifi_client.cpp.

151{
152 auto* self = static_cast<ESP32WifiClient*>(arg);
153
154 if (event_base == WIFI_EVENT)
155 {
156 switch (event_id)
157 {
159 self->connected_ = true;
160 self->semaphore_.Post();
161 break;
163 self->connected_ = false;
164 self->semaphore_.Post();
165 break;
166 default:
167 break;
168 }
169 }
170
171 if (event_base == IP_EVENT)
172 {
173 switch (event_id)
174 {
176 {
177 ip_event_got_ip_t* event = reinterpret_cast<ip_event_got_ip_t*>(event_data);
178 self->got_ip_ = true;
179 esp_ip4addr_ntoa(&event->ip_info.ip, self->ip_str_, sizeof(self->ip_str_));
180 self->semaphore_.Post();
181 break;
182 }
184 {
185 self->got_ip_ = false;
186 break;
187 }
188 }
189 }
190}

◆ GetIPAddress()

const char * LibXR::ESP32WifiClient::GetIPAddress ( ) const
overridevirtual

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

Returns
IP 字符串 / IP address string

Implements LibXR::WifiClient.

Definition at line 115 of file esp_wifi_client.cpp.

115{ return ip_str_; }

◆ GetRSSI()

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

获取当前 WiFi 信号强度 / Get the current WiFi signal strength

Returns
int

Implements LibXR::WifiClient.

Definition at line 192 of file esp_wifi_client.cpp.

193{
194 if (!connected_) return -127;
195
198 {
199 return ap_info.rssi;
200 }
201 return -127;
202}

◆ IsConnected()

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

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

Returns
true 表示已连接 / true if connected

Implements LibXR::WifiClient.

Definition at line 113 of file esp_wifi_client.cpp.

113{ return connected_; }

◆ Scan()

WifiClient::WifiError LibXR::ESP32WifiClient::Scan ( std::vector< ScanResult > &  results)
overridevirtual

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

Parameters
results扫描结果列表 / Output list of scan results

Implements LibXR::WifiClient.

Definition at line 117 of file esp_wifi_client.cpp.

118{
121 {
123 }
124
125 uint16_t ap_num = 0;
127
128 std::vector<wifi_ap_record_t> ap_records(ap_num);
130 {
132 }
133
134 results.clear();
135 for (const auto& record : ap_records)
136 {
137 ScanResult r;
138 r.ssid = reinterpret_cast<const char*>(record.ssid);
139 r.rssi = record.rssi;
140 r.security = (record.authmode == WIFI_AUTH_OPEN) ? Security::OPEN
142 : Security::UNKNOWN;
143 results.push_back(r);
144 }
145
146 return WifiError::NONE;
147}
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

Definition at line 34 of file esp_wifi_client.hpp.

◆ enabled_

bool LibXR::ESP32WifiClient::enabled_ = false
private

Definition at line 33 of file esp_wifi_client.hpp.

◆ got_ip_

bool LibXR::ESP32WifiClient::got_ip_ = false
private

Definition at line 35 of file esp_wifi_client.hpp.

◆ ip_str_

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

Definition at line 36 of file esp_wifi_client.hpp.

36{};

◆ is_initialized_

bool LibXR::ESP32WifiClient::is_initialized_ = false
inlinestaticprivate

Definition at line 30 of file esp_wifi_client.hpp.

◆ netif_

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

Definition at line 31 of file esp_wifi_client.hpp.

◆ semaphore_

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

Definition at line 38 of file esp_wifi_client.hpp.


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