forked from grblHAL/RP2040
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wifi.c
1428 lines (1140 loc) · 42.4 KB
/
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
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
wifi.c - An embedded CNC Controller with rs274/ngc (g-code) support
WiFi comms for RP2040 (Pi Pico W)
Part of grblHAL
Copyright (c) 2022-2023 Terje Io
Grbl is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Grbl is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Grbl. If not, see <http://www.gnu.org/licenses/>.
*/
#include "driver.h"
#if WIFI_ENABLE
#include <string.h>
#include "pico/stdlib.h"
#include "pico/cyw43_arch.h"
#include "networking/networking.h"
#include "networking/utils.h"
#include "lwip/timeouts.h"
#include "lwip/apps/mdns.h"
#include "wifi.h"
//#include "dnsserver.h"
#include "dhcpserver.h"
#include "grbl/report.h"
#include "grbl/nvs_buffer.h"
#include "grbl/protocol.h"
#define WIFI_DEBUG
#define USE_LWIP_POLLING 1
typedef struct
{
grbl_wifi_mode_t mode;
wifi_sta_settings_t sta;
wifi_ap_settings_t ap;
} wifi_settings_t;
static int interface;
static volatile bool linkUp = false;
static bool scan_in_progress = false;
static char IPAddress[IP4ADDR_STRLEN_MAX];
static stream_type_t active_stream = StreamType_Null;
static wifi_settings_t wifi;
static network_settings_t network;
static network_services_t services = {0}, allowed_services;
static ap_list_t ap_list = {0}, ap_list_found = {0};
//static dns_server_t dns_server;
static dhcp_server_t dhcp_server;
static nvs_address_t nvs_address;
static on_report_options_ptr on_report_options;
static on_execute_realtime_ptr on_execute_realtime;
static on_stream_changed_ptr on_stream_changed;
static char netservices[NETWORK_SERVICES_LEN] = "";
static uint32_t country_codes[] = {
CYW43_COUNTRY_WORLDWIDE,
CYW43_COUNTRY_AUSTRALIA,
CYW43_COUNTRY_AUSTRIA,
CYW43_COUNTRY_BELGIUM,
CYW43_COUNTRY_BRAZIL,
CYW43_COUNTRY_CANADA,
CYW43_COUNTRY_CHILE,
CYW43_COUNTRY_CHINA,
CYW43_COUNTRY_COLOMBIA,
CYW43_COUNTRY_CZECH_REPUBLIC,
CYW43_COUNTRY_DENMARK,
CYW43_COUNTRY_ESTONIA,
CYW43_COUNTRY_FINLAND,
CYW43_COUNTRY_FRANCE,
CYW43_COUNTRY_GERMANY,
CYW43_COUNTRY_GREECE,
CYW43_COUNTRY_HONG_KONG,
CYW43_COUNTRY_HUNGARY,
CYW43_COUNTRY_ICELAND,
CYW43_COUNTRY_INDIA,
CYW43_COUNTRY_ISRAEL,
CYW43_COUNTRY_ITALY,
CYW43_COUNTRY_JAPAN,
CYW43_COUNTRY_KENYA,
CYW43_COUNTRY_LATVIA,
CYW43_COUNTRY_LIECHTENSTEIN,
CYW43_COUNTRY_LITHUANIA,
CYW43_COUNTRY_LUXEMBOURG,
CYW43_COUNTRY_MALAYSIA,
CYW43_COUNTRY_MALTA,
CYW43_COUNTRY_MEXICO,
CYW43_COUNTRY_NETHERLANDS,
CYW43_COUNTRY_NEW_ZEALAND,
CYW43_COUNTRY_NIGERIA,
CYW43_COUNTRY_NORWAY,
CYW43_COUNTRY_PERU,
CYW43_COUNTRY_PHILIPPINES,
CYW43_COUNTRY_POLAND,
CYW43_COUNTRY_PORTUGAL,
CYW43_COUNTRY_SINGAPORE,
CYW43_COUNTRY_SLOVAKIA,
CYW43_COUNTRY_SLOVENIA,
CYW43_COUNTRY_SOUTH_AFRICA,
CYW43_COUNTRY_SOUTH_KOREA,
CYW43_COUNTRY_SPAIN,
CYW43_COUNTRY_SWEDEN,
CYW43_COUNTRY_SWITZERLAND,
CYW43_COUNTRY_TAIWAN,
CYW43_COUNTRY_THAILAND,
CYW43_COUNTRY_TURKEY,
CYW43_COUNTRY_UK,
CYW43_COUNTRY_USA
};
#if MQTT_ENABLE
static bool mqtt_connected = false;
static on_mqtt_client_connected_ptr on_client_connected;
static void mqtt_connection_changed (bool connected)
{
mqtt_connected = connected;
if(on_client_connected)
on_client_connected(connected);
}
#endif
static uint32_t get_country_code (char *country_id)
{
char *country;
uint_fast8_t idx = sizeof(country_codes) / sizeof(uint32_t);
if(*country_id) do {
country = (char *)&country_codes[--idx];
if(*country_id == *country && *(country_id + 1) == *(country + 1))
return country_codes[idx];
} while(idx);
return 0;
}
ap_list_t *wifi_get_aplist (void)
{
if(ap_list_found.ap_records)
return &ap_list_found;
else
return NULL;
}
void wifi_release_aplist (void)
{
if(ap_list_found.ap_records) {
free(ap_list_found.ap_records);
ap_list_found.ap_records = NULL;
}
}
char *wifi_get_authmode_name (uint32_t authmode)
{
return authmode == CYW43_AUTH_OPEN ? "open" :
authmode == CYW43_AUTH_WPA_TKIP_PSK ? "wpa-psk" :
authmode == CYW43_AUTH_WPA2_AES_PSK ? "wpa2-psk" :
authmode == CYW43_AUTH_WPA2_MIXED_PSK ? "wpa-wpa2-psk" :
"unknown";
}
static char *wifi_get_ipaddr (void)
{
return IPAddress;
}
static char *wifi_get_mac (void)
{
static char mac[18];
uint8_t bmac[6];
cyw43_wifi_get_mac(&cyw43_state, interface, bmac);
sprintf(mac, MAC_FORMAT_STRING, bmac[0], bmac[1], bmac[2], bmac[3], bmac[4], bmac[5]);
return mac;
}
static void reportIP (bool newopt)
{
on_report_options(newopt);
if(newopt) {
#if FTP_ENABLE
hal.stream.write(",WIFI,FTP");
#else
hal.stream.write(",WIFI");
#endif
#if WEBDAV_ENABLE
if(services.webdav)
hal.stream.write(",WebDAV");
#endif
#if MDNS_ENABLE
if(services.mdns)
hal.stream.write(",mDNS");
#endif
#if SSDP_ENABLE
if(services.ssdp)
hal.stream.write(",SSDP");
#endif
} else {
hal.stream.write("[WIFI MAC:");
hal.stream.write(wifi_get_mac());
hal.stream.write("]" ASCII_EOL);
hal.stream.write("[IP:");
hal.stream.write(wifi_get_ipaddr());
hal.stream.write("]" ASCII_EOL);
if(active_stream == StreamType_Telnet || active_stream == StreamType_WebSocket) {
hal.stream.write("[NETCON:");
hal.stream.write(active_stream == StreamType_Telnet ? "Telnet" : "Websocket");
hal.stream.write("]" ASCII_EOL);
}
#if MQTT_ENABLE
char *client_id;
if(*(client_id = networking_get_info()->mqtt_client_id)) {
hal.stream.write("[MQTT CLIENTID:");
hal.stream.write(client_id);
hal.stream.write(mqtt_connected ? "]" ASCII_EOL : " (offline)]" ASCII_EOL);
}
#endif
}
}
network_info_t *networking_get_info (void)
{
static network_info_t info;
memcpy(&info.status, &network, sizeof(network_settings_t));
strcpy(info.mac, wifi_get_mac());
strcpy(info.status.ip, wifi_get_ipaddr());
if(info.status.ip_mode == IpMode_DHCP) {
*info.status.gateway = '\0';
*info.status.mask = '\0';
}
info.is_ethernet = false;
info.link_up = false;
// info.mbps = 100;
info.status.services = services;
#if MQTT_ENABLE
networking_make_mqtt_clientid(info.mac, info.mqtt_client_id);
#endif
return &info;
}
#if MDNS_ENABLE
static void mdns_device_info (struct mdns_service *service, void *txt_userdata)
{
char build[20] = "build=";
strcat(build, uitoa(GRBL_BUILD));
mdns_resp_add_service_txtitem(service, "model=grblHAL", 13);
mdns_resp_add_service_txtitem(service, (char *)txt_userdata, strlen((char *)txt_userdata));
mdns_resp_add_service_txtitem(service, build, strlen(build));
}
static void mdns_service_info (struct mdns_service *service, void *txt_userdata)
{
if(txt_userdata)
mdns_resp_add_service_txtitem(service, (char *)txt_userdata, strlen((char *)txt_userdata));
}
#endif
static inline void services_poll (void)
{
#if TELNET_ENABLE
if(services.telnet)
telnetd_poll();
#endif
#if WEBSOCKET_ENABLE
if(services.websocket)
websocketd_poll();
#endif
#if FTP_ENABLE
if(services.ftp)
ftpd_poll();
#endif
}
static void lwIPHostTimerHandler (void *arg)
{
if(services.mask)
sys_timeout(STREAM_POLL_INTERVAL, lwIPHostTimerHandler, NULL);
services_poll();
}
static void start_services (void)
{
#if TELNET_ENABLE
if(network.services.telnet && !services.telnet)
services.telnet = telnetd_init(network.telnet_port);
#endif
#if WEBSOCKET_ENABLE
if(network.services.websocket && !services.websocket)
services.websocket = websocketd_init(network.websocket_port);
#endif
#if FTP_ENABLE
if(network.services.ftp && !services.ftp)
services.ftp = ftpd_init(network.ftp_port);
#endif
#if HTTP_ENABLE
if(network.services.http && !services.http) {
services.http = httpd_init(network.http_port);
#if WEBDAV_ENABLE
if(network.services.webdav && !services.webdav)
services.webdav = webdav_init();
#endif
#if SSDP_ENABLE
if(network.services.ssdp && !services.ssdp)
services.ssdp = ssdp_init(network.http_port);
#endif
}
#endif
#if MQTT_ENABLE
if(wifi.mode == WiFiMode_STA && !mqtt_connected)
mqtt_connect(&network.mqtt, networking_get_info()->mqtt_client_id);
#endif
#if MDNS_ENABLE
if(*network.hostname && network.services.mdns && !services.mdns) {
mdns_resp_init();
if((services.mdns = mdns_resp_add_netif(netif_default, network.hostname) == ERR_OK)) {
mdns_resp_add_service(netif_default, network.hostname, "_device-info", DNSSD_PROTO_TCP, 0, mdns_device_info, "version=" GRBL_VERSION);
if(services.http)
mdns_resp_add_service(netif_default, network.hostname, "_http", DNSSD_PROTO_TCP, network.http_port, mdns_service_info, "path=/");
if(services.webdav)
mdns_resp_add_service(netif_default, network.hostname, "_webdav", DNSSD_PROTO_TCP, network.http_port, mdns_service_info, "path=/");
if(services.websocket)
mdns_resp_add_service(netif_default, network.hostname, "_websocket", DNSSD_PROTO_TCP, network.websocket_port, mdns_service_info, NULL);
if(services.telnet)
mdns_resp_add_service(netif_default, network.hostname, "_telnet", DNSSD_PROTO_TCP, network.telnet_port, mdns_service_info, NULL);
if(services.ftp)
mdns_resp_add_service(netif_default, network.hostname, "_ftp", DNSSD_PROTO_TCP, network.ftp_port, mdns_service_info, "path=/");
// mdns_resp_announce(netif_default);
}
}
#endif
#if USE_LWIP_POLLING && (TELNET_ENABLE || WEBSOCKET_ENABLE || FTP_ENABLE)
sys_timeout(STREAM_POLL_INTERVAL, lwIPHostTimerHandler, NULL);
#endif
}
static void stop_services (void)
{
network_services_t running;
running.mask = services.mask;
services.mask = 0;
#if xHTTP_ENABLE
if(running.http)
httpdaemon_stop();
#endif
#if TELNET_ENABLE
if(running.telnet)
telnetd_stop();
#endif
#if WEBSOCKET_ENABLE
if(running.websocket)
websocketd_stop();
#endif
#if SSDP_ENABLE
if(!running.ssdp)
ssdp_stop();
#endif
// if(running.dns)
// dns_server_stop();
}
static void msg_ap_scan_completed (sys_state_t state)
{
hal.stream.write_all("[MSG:WIFI AP SCAN COMPLETED]" ASCII_EOL);
}
static void msg_sta_active (sys_state_t state)
{
char buf[50];
sprintf(buf, "[MSG:WIFI STA ACTIVE, IP=%s]" ASCII_EOL, wifi_get_ipaddr());
hal.stream.write_all(buf);
}
static void msg_sta_disconnected (sys_state_t state)
{
hal.stream.write_all("[MSG:WIFI STA DISCONNECTED]" ASCII_EOL);
}
static void msg_sta_failed (sys_state_t state)
{
hal.stream.write_all("[MSG:WIFI STA CONNECT FAILED]" ASCII_EOL);
}
static void msg_wifi_failed (sys_state_t state)
{
hal.stream.write_all("[MSG:WIFI STARTUP FAILED]" ASCII_EOL);
}
static void msg_wifi_joined (sys_state_t state)
{
hal.stream.write_all("[MSG:WIFI JOIN OK]" ASCII_EOL);
}
static void msg_wifi_noip (sys_state_t state)
{
hal.stream.write_all("[MSG:WIFI NOIP]" ASCII_EOL);
}
static void msg_wifi_nonet (sys_state_t state)
{
hal.stream.write_all("[MSG:WIFI NONET]" ASCII_EOL);
}
static void msg_wifi_badauth (sys_state_t state)
{
hal.stream.write_all("[MSG:BAD AUTH]" ASCII_EOL);
}
static void msg_wifi_nolink (sys_state_t state)
{
hal.stream.write_all("[MSG:LINK FAIL]" ASCII_EOL);
}
static void msg_wifi_start (sys_state_t state)
{
hal.stream.write_all("[MSG:WIFI START]" ASCII_EOL);
}
#ifdef WIFI_SOFTAP
static void msg_ap_ready (sys_state_t state)
{
hal.stream.write_all("[MSG:WIFI AP READY]" ASCII_EOL);
}
static void msg_ap_connected (sys_state_t state)
{
hal.stream.write_all("[MSG:WIFI AP CONNECTED]" ASCII_EOL);
}
static void msg_ap_disconnected (sys_state_t state)
{
hal.stream.write_all("[MSG:WIFI AP DISCONNECTED]" ASCII_EOL);
}
#endif
static int scan_result (void *env, const cyw43_ev_scan_result_t *result)
{
if (result) {
if(ap_list.ap_records) { // do not add duplicates
ssid_t ssid;
uint_fast8_t idx = ap_list.ap_num;
strncpy(ssid, result->ssid, result->ssid_len);
ssid[result->ssid_len] = '\0';
do {
if(!strcmp(ap_list.ap_records[--idx].ssid, ssid))
return 0;
} while(idx);
}
ap_record_t *records = realloc((void *)ap_list.ap_records, (ap_list.ap_num + 1) * sizeof(ap_record_t));
if(records) {
ap_list.ap_records = records;
ap_list.ap_records[ap_list.ap_num].authmode = result->auth_mode;
ap_list.ap_records[ap_list.ap_num].rssi = result->rssi;
ap_list.ap_records[ap_list.ap_num].primary = true;
ap_list.ap_records[ap_list.ap_num].channel = result->channel;
memcpy(&ap_list.ap_records[ap_list.ap_num].bssid, result->bssid, sizeof(result->bssid));
strncpy(ap_list.ap_records[ap_list.ap_num].ssid, result->ssid, result->ssid_len);
ap_list.ap_records[ap_list.ap_num].ssid[result->ssid_len] = '\0';
ap_list.ap_num++;
}
}
return 0;
}
#ifdef WIFI_DEBUG
void wifi_debug (char *context)
{
int status = cyw43_tcpip_link_status(&cyw43_state, interface);
hal.stream.write("[MSG:");
hal.stream.write(context);
if(status >= 0) {
hal.stream.write("_STATUS=");
hal.stream.write(uitoa(status));
} else {
hal.stream.write("_STATUS=-");
hal.stream.write(uitoa(-status));
}
hal.stream.write("]" ASCII_EOL);
}
#endif
static void enet_poll (sys_state_t state)
{
static bool led_on = false, poll = true;
static uint32_t last_ms0, next_ms1;
static int last_status = -100;
int status;
uint32_t ms = hal.get_elapsed_ticks();
if(last_ms0 != ms) {
last_ms0 = ms;
cyw43_arch_poll();
#if !USE_LWIP_POLLING
services_poll();
#endif
if((status = cyw43_tcpip_link_status(&cyw43_state, interface)) != last_status) {
linkUp = status == CYW43_LINK_UP;
last_status = status;
switch(status) {
case CYW43_LINK_FAIL:
protocol_enqueue_rt_command(msg_wifi_nolink);
break;
case CYW43_LINK_BADAUTH:
protocol_enqueue_rt_command(msg_wifi_badauth);
break;
// case CYW43_LINK_JOIN:
// protocol_enqueue_rt_command(msg_wifi_joined);
// break;
#ifdef WIFI_SOFTAP
case CYW43_LINK_UP:
if(wifi.mode == WiFiMode_AP) {
struct netif *netif = netif_default; //netif_get_by_index(0);
if(netif) {
ip4addr_ntoa_r(netif_ip_addr4(netif), IPAddress, IP4ADDR_STRLEN_MAX);
start_services();
}
}
break;
#endif
// case CYW43_LINK_NOIP:
// protocol_enqueue_rt_command(msg_wifi_noip);
// break;
case CYW43_LINK_NONET:
protocol_enqueue_rt_command(msg_wifi_nonet);
break;
default:
#ifdef WIFI_DEBUG
wifi_debug("POLL");
#endif
break;
}
}
}
if(scan_in_progress && (ms > next_ms1)) {
led_on = !led_on;
next_ms1 = ms + 1000;
cyw43_arch_gpio_put(CYW43_WL_GPIO_LED_PIN, led_on);
if(!(scan_in_progress = cyw43_wifi_scan_active(&cyw43_state))) {
if(ap_list.ap_records) {
wifi_release_aplist();
memcpy(&ap_list_found, &ap_list, sizeof(ap_list_t));
ap_list_found.timestamp = hal.get_elapsed_ticks();
memset(&ap_list, 0, sizeof(ap_list_t));
}
protocol_enqueue_rt_command(msg_ap_scan_completed);
}
}
on_execute_realtime(state);
}
void wifi_ap_scan (void)
{
ap_list.ap_num = 0;
if(ap_list.ap_records) {
free(ap_list.ap_records);
ap_list.ap_records = NULL;
}
if (!scan_in_progress) {
cyw43_wifi_scan_options_t scan_options = {0};
scan_in_progress = cyw43_wifi_scan(&cyw43_state, &scan_options, NULL, scan_result) == 0;
} else if (!cyw43_wifi_scan_active(&cyw43_state))
scan_in_progress = false;
}
/*
define CYW43_LINK_DOWN (0) ///< link is down
#define CYW43_LINK_JOIN (1) ///< Connected to wifi
#define CYW43_LINK_NOIP (2) ///< Connected to wifi, but no IP address
#define CYW43_LINK_UP (3) ///< Connect to wifi with an IP address
#define CYW43_LINK_FAIL (-1) ///< Connection failed
#define CYW43_LINK_NONET (-2) ///< No matching SSID found (could be out of range, or down)
#define CYW43_LINK_BADAUTH (-3) ///< Authenticatation failure
*/
static void netif_sta_status_callback (struct netif *netif)
{
#ifdef WIFI_DEBUG
wifi_debug("STA_NETIF");
#endif
switch(cyw43_tcpip_link_status(&cyw43_state, interface)) {
case CYW43_LINK_UP:
if(netif->ip_addr.addr != 0) {
ip4addr_ntoa_r(netif_ip_addr4(netif), IPAddress, IP4ADDR_STRLEN_MAX);
start_services();
}
protocol_enqueue_rt_command(msg_sta_active);
break;
case CYW43_LINK_DOWN:
*IPAddress = '\0';
protocol_enqueue_rt_command(msg_sta_disconnected);
break;
case CYW43_LINK_FAIL:
case CYW43_LINK_BADAUTH:
*IPAddress = '\0';
protocol_enqueue_rt_command(msg_sta_failed);
break;
// case CYW43_LINK_JOIN:
// protocol_enqueue_rt_command(msg_wifi_joined);
// break;
// case CYW43_LINK_NOIP:
// protocol_enqueue_rt_command(msg_wifi_noip);
// break;
case CYW43_LINK_NONET:
protocol_enqueue_rt_command(msg_wifi_nonet);
break;
default:
break;
}
}
static void link_sta_status_callback (struct netif *netif)
{
#ifdef WIFI_DEBUG
wifi_debug("STA_LINK");
#endif
switch(cyw43_tcpip_link_status(&cyw43_state, interface)) {
case CYW43_LINK_UP:
if(netif->ip_addr.addr != 0) {
ip4addr_ntoa_r(netif_ip_addr4(netif), IPAddress, IP4ADDR_STRLEN_MAX);
start_services();
wifi_ap_scan();
}
protocol_enqueue_rt_command(msg_sta_active);
break;
case CYW43_LINK_DOWN:
*IPAddress = '\0';
protocol_enqueue_rt_command(msg_sta_disconnected);
break;
case CYW43_LINK_FAIL:
case CYW43_LINK_BADAUTH:
*IPAddress = '\0';
protocol_enqueue_rt_command(msg_sta_failed);
break;
// case CYW43_LINK_JOIN:
// protocol_enqueue_rt_command(msg_wifi_joined);
// break;
// case CYW43_LINK_NOIP:
// protocol_enqueue_rt_command(msg_wifi_noip);
// break;
case CYW43_LINK_NONET:
protocol_enqueue_rt_command(msg_wifi_nonet);
break;
default:
break;
}
}
#ifdef WIFI_SOFTAP
static void link_ap_status_callback (struct netif *netif)
{
#ifdef WIFI_DEBUG
wifi_debug("AP_LINK");
#endif
switch(cyw43_tcpip_link_status(&cyw43_state, interface)) {
case CYW43_LINK_UP:
if((linkUp = netif->ip_addr.addr != 0)) {
ip4addr_ntoa_r(netif_ip_addr4(netif), IPAddress, IP4ADDR_STRLEN_MAX);
start_services();
// wifi_ap_scan();
}
protocol_enqueue_rt_command(msg_ap_ready);
break;
case CYW43_LINK_DOWN:
*IPAddress = '\0';
protocol_enqueue_rt_command(msg_sta_disconnected);
break;
}
}
static void netif_ap_status_callback (struct netif *netif)
{
#ifdef WIFI_DEBUG
wifi_debug("AP_NETIF");
#endif
switch(cyw43_tcpip_link_status(&cyw43_state, interface)) {
case CYW43_LINK_UP:
if(netif->ip_addr.addr != 0) {
ip4addr_ntoa_r(netif_ip_addr4(netif), IPAddress, IP4ADDR_STRLEN_MAX);
start_services();
}
protocol_enqueue_rt_command(msg_ap_ready);
break;
case CYW43_LINK_DOWN:
*IPAddress = '\0';
protocol_enqueue_rt_command(msg_sta_disconnected);
break;
default:
break;
}
}
#endif
static inline void set_addr (char *ip, ip4_addr_t *addr)
{
memcpy(ip, addr, sizeof(ip4_addr_t));
}
static inline void get_addr (ip4_addr_t *addr, char *ip)
{
memcpy(addr, ip, sizeof(ip4_addr_t));
}
static void init_settings (int itf, network_settings_t *settings)
{
interface = itf;
memcpy(&network, settings, sizeof(network_settings_t));
if(network.telnet_port == 0)
network.telnet_port = NETWORK_TELNET_PORT;
if(network.websocket_port == 0)
network.websocket_port = NETWORK_WEBSOCKET_PORT;
if(network.http_port == 0)
network.http_port = NETWORK_HTTP_PORT;
if(network.ftp_port == 0)
network.ftp_port = NETWORK_FTP_PORT;
#if MQTT_ENABLE
if(network.mqtt.port == 0)
network.mqtt.port = NETWORK_MQTT_PORT;
#endif
}
bool wifi_start (void)
{
int ret;
uint32_t country_code;
if(nvs_address == 0)
return false;
if((country_code = get_country_code(wifi.ap.country)))
ret = cyw43_arch_init_with_country(country_code);
else
ret = cyw43_arch_init();
if (ret != 0) {
protocol_enqueue_rt_command(msg_wifi_failed);
return false;
}
#ifdef WIFI_SOFTAP
if(wifi.mode == WiFiMode_AP && *wifi.ap.ssid) {
init_settings(CYW43_ITF_AP, &wifi.ap.network);
cyw43_arch_enable_ap_mode(wifi.ap.ssid,
*wifi.ap.password ? wifi.ap.password : NULL,
*wifi.ap.password ? CYW43_AUTH_WPA2_AES_PSK : CYW43_AUTH_OPEN);
netif_set_status_callback(netif_default, netif_ap_status_callback);
netif_set_link_callback(netif_default, link_ap_status_callback);
ip4_addr_t gw, mask;
// get_addr(&ip, network.ip);
// get_addr(&mask, network.mask);
IP4_ADDR(&gw, 192, 168, 1, 4);
IP4_ADDR(&mask, 255, 255, 255, 0);
dhcp_server_init(&dhcp_server, &gw, &mask);
// dns_server_init(&dns_server, &gw);
}
#endif
if(wifi.mode == WiFiMode_STA && *wifi.sta.ssid) {
init_settings(CYW43_ITF_STA, &wifi.sta.network);
cyw43_arch_enable_sta_mode();
netif_set_status_callback(netif_default, netif_sta_status_callback);
netif_set_link_callback(netif_default, link_sta_status_callback);
if((ret = cyw43_arch_wifi_connect_bssid_async(wifi.sta.ssid,
networking_ismemnull(wifi.ap.bssid, sizeof(bssid_t)) ? NULL : wifi.ap.bssid,
*wifi.sta.password ? wifi.sta.password : NULL,
*wifi.sta.password ? CYW43_AUTH_WPA2_MIXED_PSK : CYW43_AUTH_OPEN)) != 0)
protocol_enqueue_rt_command(msg_wifi_failed);
}
#if LWIP_NETIF_HOSTNAME
netif_set_hostname(netif_default, network.hostname);
#endif
#if PICO_CYW43_ARCH_POLL
on_execute_realtime = grbl.on_execute_realtime;
grbl.on_execute_realtime = enet_poll;
#endif
return ret == 0;
}
bool wifi_ap_connect (char *ssid, char *password)
{
bool ok = !ssid || (strlen(ssid) > 0 && strlen(ssid) < sizeof(ssid_t) && strlen(password) < sizeof(password_t));
if(!ok)
return false;
/*
if(xEventGroupGetBits(wifi_event_group) & CONNECTED_BIT)
esp_wifi_disconnect(); // TODO: delay until response is sent...
if(xSemaphoreTake(aplist_mutex, pdMS_TO_TICKS(10)) == pdTRUE) {
ap_list.ap_selected = NULL;
memset(&ap_list.ip_addr, 0, sizeof(ip4_addr_t));
strcpy(ap_list.ap_status, ssid ? "Connecting..." : "");
xSemaphoreGive(aplist_mutex);
}
memset(&wifi_sta_config, 0, sizeof(wifi_config_t));
if(ssid) {
strcpy((char *)wifi_sta_config.sta.ssid, ssid);
strcpy((char *)wifi_sta_config.sta.password, password);
ok = esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_sta_config) == ESP_OK && esp_wifi_connect() == ESP_OK;
}
*/
return ok;
}
bool wifi_stop (void)
{
stop_services();
cyw43_arch_deinit();
return true;
}
// Settings
static status_code_t wifi_set_country (setting_id_t setting, char *value)
{
status_code_t status;
strcaps(value);
if((status = *value == '\0' || get_country_code(value) != 0 ? Status_OK : Status_GcodeValueOutOfRange) == Status_OK)
strncpy(wifi.ap.country, value, 2);
return status;
}
static char *wifi_get_country (setting_id_t setting)
{
return wifi.ap.country;
}
static status_code_t wifi_set_bssid (setting_id_t setting, char *value)
{
if(*value) {
uint32_t bssid[6];
if(sscanf(value,"%2X:%2X:%2X:%2X:%2X:%2X", &bssid[5], &bssid[4], &bssid[3],
&bssid[2], &bssid[1], &bssid[0]) == 6) {
char c = LCAPS(value[strlen(value) - 1]);
if(!((c >= '0' && c <= '9') || (c >= 'a' && c <= 'f')))
return Status_InvalidStatement;
uint_fast8_t idx;
for(idx = 0; idx < 6; idx++)
wifi.ap.bssid[idx] = bssid[idx];
} else
return Status_InvalidStatement;
} else
memset(wifi.ap.bssid, 0, sizeof(bssid_t));
return Status_OK;
}
static char *wifi_get_bssid (setting_id_t setting)
{
static char bssid[18];
if(networking_ismemnull(wifi.ap.bssid, sizeof(bssid_t)))
*bssid = '\0';
else
sprintf(bssid, MAC_FORMAT_STRING, wifi.ap.bssid[5], wifi.ap.bssid[4], wifi.ap.bssid[3],
wifi.ap.bssid[2], wifi.ap.bssid[1], wifi.ap.bssid[0]);
return bssid;
}
static status_code_t wifi_set_int (setting_id_t setting, uint_fast16_t value)
{
switch(setting) {
case Setting_NetworkServices:
wifi.sta.network.services.mask = wifi.ap.network.services.mask = (uint8_t)value & allowed_services.mask;
break;
#if TELNET_ENABLE
case Setting_TelnetPort3:
wifi.sta.network.telnet_port = wifi.ap.network.telnet_port = (uint16_t)value;
break;