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_netif_ip_addr.h"
9#include "esp_wifi.h"
10#include "nvs_flash.h"
11
12namespace LibXR
13{
14
15ESP32WifiClient::ESP32WifiClient()
16{
17 if (!is_initialized_)
18 {
19 esp_netif_init();
20 esp_event_loop_create_default();
21 netif_ = esp_netif_create_default_wifi_sta();
22
23 wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
24 esp_wifi_init(&cfg);
25
26 is_initialized_ = true;
27 }
28}
29
30ESP32WifiClient::~ESP32WifiClient()
31{
32 if (enabled_)
33 {
34 Disable();
35 }
36}
37
39{
40 if (enabled_) return WifiError::ALREADY_ENABLED;
41
42 esp_wifi_set_mode(WIFI_MODE_STA);
43 esp_wifi_start();
44 enabled_ = true;
45
46 esp_event_handler_register(WIFI_EVENT, WIFI_EVENT_STA_CONNECTED, &EventHandler, this);
47 esp_event_handler_register(WIFI_EVENT, WIFI_EVENT_STA_DISCONNECTED, &EventHandler,
48 this);
49 esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &EventHandler, this);
50 esp_event_handler_register(IP_EVENT, IP_EVENT_STA_LOST_IP, &EventHandler, this);
51 return WifiError::NONE;
52}
53
55{
56 if (!enabled_) return WifiError::NOT_ENABLED;
57
58 esp_wifi_stop();
59 enabled_ = false;
60 connected_ = false;
61 return WifiError::NONE;
62}
63
65{
66 if (!enabled_) return WifiError::NOT_ENABLED;
67
68 wifi_config_t wifi_config{};
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));
73 esp_wifi_set_config(WIFI_IF_STA, &wifi_config);
74 esp_wifi_connect();
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}
94
96{
97 if (!enabled_)
98 {
100 }
101 if (!connected_)
102 {
103 return WifiError::NONE;
104 }
105 while (semaphore_.Wait(0) == ErrorCode::OK)
106 {
107 }
108 esp_wifi_disconnect();
109 semaphore_.Wait();
110 return WifiError::NONE;
111}
112
113bool ESP32WifiClient::IsConnected() const { return connected_; }
114
115const char* ESP32WifiClient::GetIPAddress() const { return ip_str_; }
116
117WifiClient::WifiError ESP32WifiClient::Scan(std::vector<ScanResult>& results)
118{
119 wifi_scan_config_t scan_config = {};
120 if (esp_wifi_scan_start(&scan_config, true) != ESP_OK)
121 {
123 }
124
125 uint16_t ap_num = 0;
126 esp_wifi_scan_get_ap_num(&ap_num);
127
128 std::vector<wifi_ap_record_t> ap_records(ap_num);
129 if (esp_wifi_scan_get_ap_records(&ap_num, ap_records.data()) != ESP_OK)
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
141 : (record.authmode == WIFI_AUTH_WPA2_PSK) ? Security::WPA2_PSK
143 results.push_back(r);
144 }
145
146 return WifiError::NONE;
147}
148
149void ESP32WifiClient::EventHandler(void* arg, esp_event_base_t event_base,
150 int32_t event_id, void* event_data)
151{
152 auto* self = static_cast<ESP32WifiClient*>(arg);
153
154 if (event_base == WIFI_EVENT)
155 {
156 switch (event_id)
157 {
158 case WIFI_EVENT_STA_CONNECTED:
159 self->connected_ = true;
160 self->semaphore_.Post();
161 break;
162 case WIFI_EVENT_STA_DISCONNECTED:
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 {
175 case IP_EVENT_STA_GOT_IP:
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 }
183 case IP_EVENT_STA_LOST_IP:
184 {
185 self->got_ip_ = false;
186 break;
187 }
188 }
189 }
190}
191
193{
194 if (!connected_) return -127;
195
196 wifi_ap_record_t ap_info;
197 if (esp_wifi_sta_get_ap_info(&ap_info) == ESP_OK)
198 {
199 return ap_info.rssi;
200 }
201 return -127;
202}
203
204} // namespace LibXR
WifiError Connect(const Config &config) override
连接到 WiFi 网络 / Connect to a WiFi network
WifiError Disable() override
禁用 WiFi 模块 / Disable the WiFi module
int GetRSSI() const override
获取当前 WiFi 信号强度 / Get the current WiFi signal strength
WifiError Disconnect() override
断开当前 WiFi 连接 / Disconnect from the current WiFi connection
bool IsConnected() const override
检查是否已连接 / Check if currently connected
WifiError Scan(std::vector< ScanResult > &results) override
扫描可用 WiFi 网络 / Scan for available WiFi networks
const char * GetIPAddress() const override
获取当前 IP 地址 / Get the current IP address
WifiError Enable() override
启用 WiFi 模块 / Enable the WiFi module
ErrorCode Wait(uint32_t timeout=UINT32_MAX)
等待(减少)信号量 Waits (decrements) the semaphore
Definition semaphore.cpp:15
@ 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
@ ALREADY_ENABLED
已启用 / Already enabled
@ NOT_ENABLED
未启用 / Not enabled
@ NONE
无错误 / No error
@ CONNECTION_TIMEOUT
连接超时 / Connection timeout
LibXR Color Control Library / LibXR终端颜色控制库
Definition esp_gpio.hpp:8
WiFi 连接配置 / WiFi connection configuration.
std::string password
密码 / Password
std::string ssid
SSID 名称 / SSID name.
WiFi 扫描结果 / WiFi scan result.
std::string ssid
发现的 SSID / Detected SSID
int rssi
信号强度 / Signal strength (RSSI)
Security security
安全类型 / Security type