libxr  1.0
Want to be the best embedded framework
Loading...
Searching...
No Matches
esp_wifi_client.cpp
1#include "esp_wifi_client.hpp"
2
3#include <cstring>
4
5#include "esp_event.h"
6#include "esp_log.h"
7#include "esp_netif.h"
8#include "esp_wifi.h"
9#include "nvs_flash.h"
10
11namespace LibXR
12{
13
14ESP32WifiClient::ESP32WifiClient()
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}
28
29ESP32WifiClient::~ESP32WifiClient()
30{
31 if (enabled_)
32 {
33 Disable();
34 }
35}
36
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}
79
81{
82 if (!enabled_) return;
83
84 esp_wifi_stop();
85 enabled_ = false;
86 connected_ = false;
87}
88
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}
113
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}
127
129
131{
132 return IPAddressRaw::FromString(ip_str_);
133}
134
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}
143
145 size_t& out_found)
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)
170 }
171
172 return WifiError::NONE;
173}
174
175void ESP32WifiClient::EventHandler(void* arg, esp_event_base_t event_base,
176 int32_t event_id, void* event_data)
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}
215
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}
227
228} // namespace LibXR
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
Definition semaphore.cpp:25
@ 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
@ NONE
无错误 / No error
@ CONNECTION_TIMEOUT
连接超时 / Connection timeout
LibXR 命名空间
Definition ch32_gpio.hpp:9
原始 IPv4 地址 / Raw IPv4 address
Definition net.hpp:17
原始 MAC 地址 / Raw MAC address
Definition net.hpp:69
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