-
Notifications
You must be signed in to change notification settings - Fork 28
/
mgos_wifi.c
437 lines (397 loc) · 13.3 KB
/
mgos_wifi.c
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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
/*
* Copyright (c) 2014-2018 Cesanta Software Limited
* All rights reserved
*
* Licensed under the Apache License, Version 2.0 (the ""License"");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an ""AS IS"" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "mgos_wifi.h"
#include "mgos_wifi_hal.h"
#include <stdbool.h>
#include <stdlib.h>
#include "common/cs_dbg.h"
#include "common/queue.h"
#include "mgos_gpio.h"
#include "mgos_mongoose.h"
#include "mgos_net.h"
#include "mgos_net_hal.h"
#include "mgos_sys_config.h"
#include "mgos_system.h"
#include "mgos_time.h"
#include "mgos_timers.h"
#include "mongoose.h"
#include "mgos_wifi_sta.h"
struct cb_info {
void *cb;
void *arg;
SLIST_ENTRY(cb_info) next;
};
static SLIST_HEAD(s_scan_cbs, cb_info) s_scan_cbs;
static bool s_scan_in_progress = false;
struct mgos_rlock_type *s_wifi_lock = NULL;
void wifi_lock(void) {
mgos_rlock(s_wifi_lock);
}
void wifi_unlock(void) {
mgos_runlock(s_wifi_lock);
}
extern void mgos_wifi_sta_ev_handler(int ev, void *evd, void *cb_arg);
static void mgos_wifi_event_cb(void *arg) {
bool net_event = true;
struct mgos_wifi_dev_event_info *dei =
(struct mgos_wifi_dev_event_info *) arg;
void *ev_arg = NULL;
enum mgos_net_event nev = MGOS_NET_EV_DISCONNECTED;
switch (dei->ev) {
case MGOS_WIFI_EV_STA_DISCONNECTED: {
ev_arg = &dei->sta_disconnected;
nev = MGOS_NET_EV_DISCONNECTED;
LOG(LL_INFO,
("WiFi STA: Disconnected, reason: %d", dei->sta_disconnected.reason));
break;
}
case MGOS_WIFI_EV_STA_CONNECTING: {
nev = MGOS_NET_EV_CONNECTING;
break;
}
case MGOS_WIFI_EV_STA_CONNECTED: {
ev_arg = &dei->sta_connected;
nev = MGOS_NET_EV_CONNECTED;
struct mgos_wifi_sta_connected_arg *ea = &dei->sta_connected;
ea->rssi = mgos_wifi_sta_get_rssi();
LOG(LL_INFO, ("WiFi STA: Connected, BSSID %02x:%02x:%02x:%02x:%02x:%02x "
"ch %d RSSI %d",
ea->bssid[0], ea->bssid[1], ea->bssid[2], ea->bssid[3],
ea->bssid[4], ea->bssid[5], ea->channel, ea->rssi));
break;
}
case MGOS_WIFI_EV_STA_IP_ACQUIRED: {
nev = MGOS_NET_EV_IP_ACQUIRED;
break;
}
case MGOS_WIFI_EV_AP_STA_CONNECTED:
case MGOS_WIFI_EV_AP_STA_DISCONNECTED: {
struct mgos_wifi_ap_sta_connected_arg *ea = &dei->ap_sta_connected;
LOG(LL_INFO,
("%02x:%02x:%02x:%02x:%02x:%02x %s", ea->mac[0], ea->mac[1],
ea->mac[2], ea->mac[3], ea->mac[4], ea->mac[5],
(dei->ev == MGOS_WIFI_EV_AP_STA_CONNECTED ? "connected"
: "disconnected")));
net_event = false;
ev_arg = &dei->ap_sta_connected;
(void) ea;
break;
}
}
/* Deliver to STA code to ensure it is the first handler. */
mgos_wifi_sta_ev_handler(dei->ev, ev_arg, NULL);
mgos_event_trigger(dei->ev, ev_arg);
if (net_event) {
mgos_net_dev_event_cb(MGOS_NET_IF_TYPE_WIFI, MGOS_NET_IF_WIFI_STA, nev);
}
free(dei);
}
void mgos_wifi_dev_event_cb(const struct mgos_wifi_dev_event_info *dei) {
struct mgos_wifi_dev_event_info *deic =
(struct mgos_wifi_dev_event_info *) calloc(1, sizeof(*deic));
if (deic == NULL) return;
memcpy(deic, dei, sizeof(*deic));
mgos_invoke_cb(mgos_wifi_event_cb, deic, false /* from_isr */);
}
bool mgos_wifi_validate_sta_cfg(const struct mgos_config_wifi_sta *cfg,
char **msg) {
if (!cfg->enable) return true;
if (mgos_conf_str_empty(cfg->ssid) || strlen(cfg->ssid) > 31) {
if (!mg_asprintf(msg, 0, "%s %s must be between %d and %d chars", "STA",
"SSID", 1, 31)) {
}
return false;
}
/* Note: WEP allows 5 char passwords. */
if (!mgos_conf_str_empty(cfg->pass) &&
(strlen(cfg->pass) < 5 || strlen(cfg->pass) > 63)) {
if (!mg_asprintf(msg, 0, "%s %s must be between %d and %d chars", "STA",
"password", 8, 63)) {
}
return false;
}
if (!mgos_conf_str_empty(cfg->ip)) {
if (mgos_conf_str_empty(cfg->netmask)) {
if (!mg_asprintf(msg, 0, "Static IP is set but no netmask provided")) {
}
return false;
}
struct sockaddr_in ip, netmask;
if (!mgos_net_str_to_ip(cfg->ip, &ip)) {
if (!mg_asprintf(msg, 0, "Static %s is invalid", "IP")) {
}
return false;
}
if (!mgos_net_str_to_ip(cfg->netmask, &netmask)) {
if (!mg_asprintf(msg, 0, "Static %s is invalid", "netmask")) {
}
return false;
}
uint32_t nm = ntohl(netmask.sin_addr.s_addr);
int num_bits = 0;
while ((nm & 0x80000000) != 0) {
num_bits++;
nm <<= 1;
}
if (num_bits == 0 || nm != 0) {
if (!mg_asprintf(msg, 0, "Static %s is invalid", "netmask")) {
}
return false;
}
if (!mgos_conf_str_empty(cfg->gw)) {
struct sockaddr_in gw;
if (!mgos_net_str_to_ip(cfg->gw, &gw)) {
if (!mg_asprintf(msg, 0, "Static %s is invalid", "gateway")) {
}
return false;
}
if ((ip.sin_addr.s_addr & netmask.sin_addr.s_addr) !=
(gw.sin_addr.s_addr & netmask.sin_addr.s_addr)) {
if (!mg_asprintf(msg, 0, "Static %s is invalid", "gateway")) {
}
return false;
}
}
}
return true;
}
bool mgos_wifi_validate_ap_cfg(const struct mgos_config_wifi_ap *cfg,
char **msg) {
if (!cfg->enable) return true;
if (mgos_conf_str_empty(cfg->ssid) || strlen(cfg->ssid) > 31) {
if (!mg_asprintf(msg, 0, "%s %s must be between %d and %d chars", "AP",
"SSID", 1, 31)) {
}
return false;
}
if (!mgos_conf_str_empty(cfg->pass) &&
(strlen(cfg->pass) < 8 || strlen(cfg->pass) > 63)) {
if (!mg_asprintf(msg, 0, "%s %s must be between %d and %d chars", "AP",
"password", 8, 63)) {
}
return false;
}
if (mgos_conf_str_empty(cfg->ip) || mgos_conf_str_empty(cfg->netmask) ||
mgos_conf_str_empty(cfg->dhcp_start) ||
mgos_conf_str_empty(cfg->dhcp_end)) {
*msg = strdup("AP IP, netmask, DHCP start and end addresses must be set");
return false;
}
/* TODO(rojer): More validation here. DHCP range, netmask, GW (if set). */
return true;
}
static bool validate_wifi_cfg(const struct mgos_config *cfg, char **msg) {
return (mgos_wifi_validate_ap_cfg(&cfg->wifi.ap, msg) &&
mgos_wifi_validate_sta_cfg(&cfg->wifi.sta, msg));
}
bool mgos_wifi_setup_sta(const struct mgos_config_wifi_sta *cfg) {
int ret = false;
char *err_msg = NULL;
if (!mgos_wifi_validate_sta_cfg(cfg, &err_msg)) {
LOG(LL_ERROR, ("WiFi STA: %s", err_msg));
free(err_msg);
return false;
}
mgos_wifi_sta_clear_cfgs();
if (cfg->enable) {
ret = mgos_wifi_sta_add_cfg(cfg);
if (ret) {
ret = mgos_wifi_connect();
}
} else {
// May turn off STA mode, depending on the hardware.
mgos_wifi_dev_sta_setup(cfg);
ret = true;
}
return ret;
}
static void wifi_ap_disable_timer_cb(void *arg) {
if (!mgos_sys_config_get_wifi_ap_enable()) return;
LOG(LL_INFO, ("Disabling AP"));
mgos_sys_config_set_wifi_ap_enable(false);
save_cfg(&mgos_sys_config, NULL);
mgos_wifi_setup_ap(&mgos_sys_config.wifi.ap);
(void) arg;
}
bool mgos_wifi_setup_ap(const struct mgos_config_wifi_ap *cfg) {
char *err_msg = NULL;
if (!mgos_wifi_validate_ap_cfg(cfg, &err_msg)) {
LOG(LL_ERROR, ("WiFi AP: %s", err_msg));
free(err_msg);
return false;
}
wifi_lock();
bool ret = mgos_wifi_dev_ap_setup(cfg);
wifi_unlock();
if (cfg->enable && ret && cfg->disable_after > 0) {
LOG(LL_INFO, ("WiFi AP: Enabled for %d seconds", cfg->disable_after));
mgos_set_timer(cfg->disable_after * 1000, 0, wifi_ap_disable_timer_cb,
NULL);
}
return ret;
}
struct scan_result_info {
int num_res;
struct mgos_wifi_scan_result *res;
};
static void scan_cb_cb(void *arg) {
struct scan_result_info *ri = (struct scan_result_info *) arg;
wifi_lock();
SLIST_HEAD(scan_cbs, cb_info) scan_cbs;
memcpy(&scan_cbs, &s_scan_cbs, sizeof(scan_cbs));
memset(&s_scan_cbs, 0, sizeof(s_scan_cbs));
wifi_unlock();
struct cb_info *cbi, *cbit;
SLIST_FOREACH_SAFE(cbi, &scan_cbs, next, cbit) {
((mgos_wifi_scan_cb_t) cbi->cb)(ri->num_res, ri->res, cbi->arg);
free(cbi);
}
free(ri->res);
free(ri);
}
void mgos_wifi_dev_scan_cb(int num_res, struct mgos_wifi_scan_result *res) {
if (!s_scan_in_progress) return;
LOG(LL_DEBUG, ("WiFi scan done, num_res %d", num_res));
struct scan_result_info *ri =
(struct scan_result_info *) calloc(1, sizeof(*ri));
ri->num_res = num_res;
ri->res = res;
s_scan_in_progress = false;
mgos_invoke_cb(scan_cb_cb, ri, false /* from_isr */);
}
void mgos_wifi_scan(mgos_wifi_scan_cb_t cb, void *arg) {
struct cb_info *cbi = (struct cb_info *) calloc(1, sizeof(*cbi));
if (cbi == NULL) return;
cbi->cb = cb;
cbi->arg = arg;
wifi_lock();
SLIST_INSERT_HEAD(&s_scan_cbs, cbi, next);
if (!s_scan_in_progress) {
if (mgos_wifi_dev_start_scan()) {
s_scan_in_progress = true;
} else {
mgos_wifi_dev_scan_cb(-1, NULL);
}
}
wifi_unlock();
}
bool mgos_wifi_setup(struct mgos_config_wifi *cfg) {
bool result = false, trigger_ap = false;
int gpio = cfg->ap.trigger_on_gpio;
if (gpio >= 0) {
mgos_gpio_set_mode(gpio, MGOS_GPIO_MODE_INPUT);
mgos_gpio_set_pull(gpio, MGOS_GPIO_PULL_UP);
trigger_ap = (mgos_gpio_read(gpio) == 0);
}
const struct mgos_config_wifi_ap dummy_ap_cfg = {.enable = false};
const struct mgos_config_wifi_sta dummy_sta_cfg = {.enable = false};
const struct mgos_config_wifi_sta *sta_cfg = &cfg->sta;
const struct mgos_config_wifi_sta *sta_cfg1 = &cfg->sta1;
const struct mgos_config_wifi_sta *sta_cfg2 = &cfg->sta2;
bool sta_enabled = (sta_cfg->enable || sta_cfg1->enable || sta_cfg2->enable);
if (trigger_ap || (cfg->ap.enable && !sta_enabled)) {
struct mgos_config_wifi_ap ap_cfg;
memcpy(&ap_cfg, &cfg->ap, sizeof(ap_cfg));
ap_cfg.enable = true;
LOG(LL_INFO, ("WiFi mode: %s", "AP"));
/* Disable STA if it was enabled. */
mgos_wifi_setup_sta(&dummy_sta_cfg);
result = mgos_wifi_setup_ap(&ap_cfg);
#ifdef MGOS_WIFI_ENABLE_AP_STA /* ifdef-ok */
} else if (cfg->ap.enable && sta_enabled) {
LOG(LL_INFO, ("WiFi mode: %s", "AP+STA"));
result = mgos_wifi_setup_ap(&cfg->ap);
mgos_wifi_sta_clear_cfgs();
bool sta_result = mgos_wifi_sta_add_cfg(sta_cfg);
sta_result |= mgos_wifi_sta_add_cfg(sta_cfg1);
sta_result |= mgos_wifi_sta_add_cfg(sta_cfg2);
if (sta_result) {
sta_result = mgos_wifi_connect();
}
result |= sta_result;
#endif
} else if (sta_enabled) {
LOG(LL_INFO, ("WiFi mode: %s", "STA"));
/* Disable AP if it was enabled. */
mgos_wifi_setup_ap(&dummy_ap_cfg);
mgos_wifi_sta_clear_cfgs();
result |= mgos_wifi_sta_add_cfg(sta_cfg);
result |= mgos_wifi_sta_add_cfg(sta_cfg1);
result |= mgos_wifi_sta_add_cfg(sta_cfg2);
if (result) mgos_wifi_connect();
} else {
LOG(LL_INFO, ("WiFi mode: %s", "off"));
mgos_wifi_setup_sta(&dummy_sta_cfg);
mgos_wifi_setup_ap(&dummy_ap_cfg);
result = true;
}
return result;
}
/*
* Handler of DNS requests, it resolves mgos_sys_config_get_wifi_ap_hostname()
* to the IP address of wifi AP (mgos_sys_config_get_wifi_ap_ip()).
*/
static void mgos_wifi_dns_ev_handler(struct mg_connection *c, int ev,
void *ev_data, void *user_data) {
struct mg_dns_message *msg = (struct mg_dns_message *) ev_data;
struct mbuf reply_buf;
int i;
if (ev != MG_DNS_MESSAGE) return;
mbuf_init(&reply_buf, 512);
struct mg_dns_reply reply = mg_dns_create_reply(&reply_buf, msg);
for (i = 0; i < msg->num_questions; i++) {
char rname[256];
struct mg_dns_resource_record *rr = &msg->questions[i];
mg_dns_uncompress_name(msg, &rr->name, rname, sizeof(rname) - 1);
if (rr->rtype == MG_DNS_A_RECORD &&
strcmp(rname, mgos_sys_config_get_wifi_ap_hostname()) == 0) {
struct sockaddr_in ip;
if (mgos_net_str_to_ip(mgos_sys_config_get_wifi_ap_ip(), &ip)) {
mg_dns_reply_record(&reply, rr, NULL, rr->rtype, 10,
&ip.sin_addr.s_addr, 4);
}
}
}
mg_dns_send_reply(c, &reply);
mbuf_free(&reply_buf);
(void) user_data;
}
bool mgos_wifi_init(void) {
s_wifi_lock = mgos_rlock_create();
mgos_event_register_base(MGOS_WIFI_EV_BASE, "wifi");
mgos_sys_config_register_validator(validate_wifi_cfg);
mgos_wifi_dev_init();
bool ret =
mgos_wifi_setup((struct mgos_config_wifi *) mgos_sys_config_get_wifi());
if (!ret) {
return ret;
}
/* Setup DNS handler if needed */
if (mgos_sys_config_get_wifi_ap_enable() &&
mgos_sys_config_get_wifi_ap_hostname() != NULL) {
char buf[50];
sprintf(buf, "udp://%s:53", mgos_sys_config_get_wifi_ap_ip());
struct mg_connection *dns_conn =
mg_bind(mgos_get_mgr(), buf, mgos_wifi_dns_ev_handler, 0);
mg_set_protocol_dns(dns_conn);
}
mgos_wifi_sta_init();
return true;
}
void mgos_wifi_deinit(void) {
mgos_wifi_dev_deinit();
}