-
Notifications
You must be signed in to change notification settings - Fork 0
/
wifi.cpp
306 lines (275 loc) · 7.62 KB
/
wifi.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
#include <espp/wifi.h>
#include <esp_event_loop.h>
namespace lamp {
WiFi::WiFi()
{
DEBUG << "Init wifi";
tcpip_adapter_init();
ESP_ERROR_CHECK(esp_event_loop_init(WiFi::StaticEventHandler, this));
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
ESP_ERROR_CHECK(esp_wifi_init(&cfg));
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
}
Data WiFi::accessPointSsid() const
{
if(!_isAccessPoint) {
return {};
}
wifi_config_t config;
ESP_ERROR_CHECK(esp_wifi_get_config(ESP_IF_WIFI_AP, &config));
return {config.ap.ssid, config.ap.ssid_len};
}
Data WiFi::stationSsid() const
{
if(!_isStation) {
return {};
}
wifi_config_t config;
ESP_ERROR_CHECK(esp_wifi_get_config(ESP_IF_WIFI_STA, &config));
return {config.sta.ssid};
}
void WiFi::SetAccessPoint(const Buffer& ssid)
{
INFO << "Set access point: SSID" << ssid;
Mutex::LockGuard lock(_mutex);
ESPP_CHECK(_state == State::none);
if(ssid.empty()) {
DEBUG << "Disable access point";
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
_isAccessPoint = false;
} else {
DEBUG << "Enable access point";
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_APSTA));
wifi_config_t wifi_config;
wifi_config.ap.ssid_len = ssid.CopyTo(wifi_config.ap.ssid, 32, true);
wifi_config.ap.max_connection = 5;
wifi_config.ap.authmode = WIFI_AUTH_OPEN;
_isAccessPoint = true;
ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_AP, &wifi_config));
}
}
void WiFi::SetConnection(const Buffer& ssid, const Buffer& password)
{
INFO << "Set connection to" << ssid << "with password length" << password.length();
Mutex::LockGuard lock(_mutex);
ESPP_CHECK(_state == State::none || _state == State::wait_start || _state == State::started);
if(ssid.empty()) {
DEBUG << "Disable station";
_isStation = false;
} else {
wifi_config_t config = {};
ssid.StringCopyTo(config.sta.ssid, 32);
password.StringCopyTo(config.sta.password, 64);
_isStation = true;
ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_STA, &config));
}
}
bool WiFi::Start()
{
INFO << "Start WiFi";
Mutex::LockGuard lock(_mutex);
if(_state != State::none) {
DEBUG << "Invalid state";
return false;
}
_state = State::wait_start;
ESP_ERROR_CHECK(esp_wifi_start());
return true;
}
bool WiFi::Connect()
{
INFO << "Connect WiFi station";
Mutex::LockGuard lock(_mutex);
if(!_isStation) {
DEBUG << "Station isn't inited";
return false;
}
switch(_state) {
case State::wait_start:
DEBUG << "Wait start. Append wait connection";
_state = State::wait_start_connect;
break;
case State::started:
DEBUG << "Connect station";
_state = State::wait_connect;
ESP_ERROR_CHECK(esp_wifi_connect());
break;
default:
DEBUG << "Invalid state";
return false;
}
return true;
}
bool WiFi::Disconnect()
{
INFO << "Disconnect WiFi station";
Mutex::LockGuard lock(_mutex);
if(!_isStation) {
DEBUG << "Station isn't inited";
return false;
}
switch(_state) {
case State::wait_start_connect:
DEBUG << "Wait start and connect. Reset wait connect";
_state = State::wait_start;
break;
case State::wait_connect:
case State::connected:
DEBUG << "Station connected. Disconnect";
ESP_ERROR_CHECK(esp_wifi_disconnect());
break;
default:
DEBUG << "Invalid state";
return false;
}
return true;
}
bool WiFi::Stop()
{
INFO << "Stop wifi";
Mutex::LockGuard lock(_mutex);
if(_state == State::none) {
DEBUG << "Wifi wasn't started";
return false;
}
ESP_ERROR_CHECK(esp_wifi_stop());
return true;
}
void WiFi::OnStarted()
{
INFO << "On started";
Mutex::LockGuard lock(_mutex);
switch(_state) {
case State::wait_start:
DEBUG << "WiFi started";
_state = State::started;
break;
case State::wait_start_connect:
DEBUG << "WiFi started and wait connection";
_state = State::wait_connect;
ESP_ERROR_CHECK(esp_wifi_connect());
break;
default:
DEBUG << "Invalid state";
}
}
void WiFi::OnConnected()
{
INFO << "On connected";
Mutex::LockGuard lock(_mutex);
switch(_state) {
case State::wait_connect:
_state = State::connected;
break;
default:
ERROR << "Invalid state";
}
}
void WiFi::OnGotIp()
{
INFO << "On got IP";
Mutex::LockGuard lock(_mutex);
switch(_state) {
case State::connected:
_hasIp = true;
break;
default:
ERROR << "Invalid state";
}
}
void WiFi::OnDisconnected()
{
INFO << "On disconnected";
Mutex::LockGuard lock(_mutex);
switch(_state) {
case State::wait_connect:
DEBUG << "Wait connect reset";
_state = State::started;
break;
case State::connected:
DEBUG << "Reset connected state";
_state = State::started;
break;
default:
ERROR << "Invalid state";
}
_hasIp = false;
}
void WiFi::OnStop()
{
INFO << "On stop";
Mutex::LockGuard lock(_mutex);
_state = State::none;
}
esp_err_t WiFi::StaticEventHandler(void* context, system_event_t* event)
{
WiFi* instance = reinterpret_cast<WiFi*>(context);
instance->HandleEvent(*event);
return ESP_OK;
}
void WiFi::HandleEvent(const system_event_t& event)
{
switch(event.event_id) {
case SYSTEM_EVENT_STA_START:
OnStarted();
break;
case SYSTEM_EVENT_STA_STOP:
OnStop();
break;
case SYSTEM_EVENT_STA_CONNECTED:
OnConnected();
break;
case SYSTEM_EVENT_STA_GOT_IP:
OnGotIp();
break;
case SYSTEM_EVENT_STA_DISCONNECTED:
OnDisconnected();
break;
// case SYSTEM_EVENT_AP_START:
// OnStarted();
// break;
// case SYSTEM_EVENT_AP_STOP:
// OnStop();
// break;
case SYSTEM_EVENT_AP_STACONNECTED:
OnStationConnected(event.event_info.sta_connected);
break;
case SYSTEM_EVENT_AP_STADISCONNECTED:
OnStationDisconnected(event.event_info.sta_disconnected);
break;
case SYSTEM_EVENT_AP_STAIPASSIGNED:
OnStationIpAssigned(event.event_info.ap_staipassigned);
break;
default:
DEBUG << "Unprocessed network event with id" << event.event_id;
}
}
void WiFi::OnStationConnected(const system_event_ap_staconnected_t&)
{
DEBUG << "station connected to" << "access point";
Mutex::LockGuard lock(_mutex);
if(!isStarted()) {
ERROR << "Unexpected event";
return;
}
}
void WiFi::OnStationDisconnected(const system_event_ap_stadisconnected_t&)
{
DEBUG << "station disconnected from" << "access point";
Mutex::LockGuard lock(_mutex);
if(!isStarted()) {
ERROR << "Unexpected event";
return;
}
}
void WiFi::OnStationIpAssigned(const system_event_ap_staipassigned_t& event)
{
DEBUG << "station got ip from" << "access point";
Mutex::LockGuard lock(_mutex);
if(!isStarted()) {
ERROR << "Unexpected event";
return;
}
DEBUG << "Assign ip" << ip4addr_ntoa(&event.ip);
}
}