-
Notifications
You must be signed in to change notification settings - Fork 8
/
SPWFSAxx.cpp
1287 lines (1076 loc) · 42.3 KB
/
SPWFSAxx.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
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
/* SPWFSAxx Devices
* Copyright (c) 2015 ARM Limited
*
* 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 "mbed_debug.h"
#include "SpwfSAInterface.h" /* must be included first */
#include "SPWFSAxx.h"
static const char out_delim[] = {SPWFSAxx::_cr_, '\0'};
SPWFSAxx::SPWFSAxx(PinName tx, PinName rx,
PinName rts, PinName cts,
SpwfSAInterface &ifce, bool debug,
PinName wakeup, PinName reset)
: _serial(tx, rx, SPWFXX_DEFAULT_BAUD_RATE), _parser(&_serial, out_delim),
_wakeup(wakeup, 1), _reset(reset, 1),
_rts(rts), _cts(cts),
_timeout(SPWF_INIT_TIMEOUT), _dbg_on(debug),
_pending_sockets_bitmap(0),
_network_lost_flag(false),
_associated_interface(ifce),
_call_event_callback_blocked(0),
_callback_func(),
_packets(0), _packets_end(&_packets)
{
memset(_pending_pkt_sizes, 0, sizeof(_pending_pkt_sizes));
_serial.sigio(Callback<void()>(this, &SPWFSAxx::_event_handler));
_parser.debug_on(debug);
_parser.set_timeout(_timeout);
/* unlikely OOBs */
_parser.oob("+WIND:5:WiFi Hardware Failure", callback(this, &SPWFSAxx::_wifi_hwfault_handler));
_parser.oob("+WIND:33:WiFi Network Lost", callback(this, &SPWFSAxx::_network_lost_handler_th));
#if MBED_CONF_IDW0XX1_EXPANSION_BOARD == IDW04A1
_parser.oob("+WIND:24:WiFi Up::", callback(this, &SPWFSAxx::_skip_oob));
#endif
_parser.oob("+WIND:8:Hard Fault", callback(this, &SPWFSAxx::_hard_fault_handler));
/* most likely OOBs */
_parser.oob(SPWFXX_OOB_ERROR, callback(this, &SPWFSAxx::_error_handler));
_parser.oob("+WIND:58:Socket Closed", callback(this, &SPWFSAxx::_server_gone_handler));
_parser.oob("+WIND:55:Pending Data", callback(this, &SPWFSAxx::_packet_handler_th));
}
bool SPWFSAxx::startup(int mode)
{
BlockExecuter netsock_wa_obj(Callback<void()>(this, &SPWFSAxx::_unblock_event_callback),
Callback<void()>(this, &SPWFSAxx::_block_event_callback)); /* disable calling (external) callback in IRQ context */
/*Reset module*/
if(!hw_reset()) {
debug_if(_dbg_on, "\r\nSPWF> HW reset failed\r\n");
return false;
}
/* factory reset */
if(!(_parser.send(SPWFXX_SEND_FWCFG) && _recv_ok()))
{
debug_if(_dbg_on, "\r\nSPWF> error restore factory default settings\r\n");
return false;
}
/*switch off led*/
if(!(_parser.send("AT+S.SCFG=blink_led,0") && _recv_ok()))
{
debug_if(_dbg_on, "\r\nSPWF> error stop blinking led (%d)\r\n", __LINE__);
return false;
}
/*set local echo to 0*/
if(!(_parser.send(SPWFXX_SEND_DISABLE_LE) && _recv_ok()))
{
debug_if(_dbg_on, "\r\nSPWF> error local echo set\r\n");
return false;
}
/*set the operational rates*/
if(!(_parser.send("AT+S.SCFG=wifi_opr_rate_mask,0x003FFFCF") && _recv_ok()))
{
debug_if(_dbg_on, "\r\nSPWF> error setting ht_mode\r\n");
return false;
}
/*enable the 802.11n mode*/
if(!(_parser.send("AT+S.SCFG=wifi_ht_mode,1") && _recv_ok()))
{
debug_if(_dbg_on, "\r\nSPWF> error setting operational rates\r\n");
return false;
}
/*set idle mode (0->idle, 1->STA,3->miniAP, 2->IBSS)*/
if(!(_parser.send("AT+S.SCFG=wifi_mode,%d", mode) && _recv_ok()))
{
debug_if(_dbg_on, "\r\nSPWF> error WiFi mode set idle (%d)\r\n", __LINE__);
return false;
}
#if defined(MBED_MAJOR_VERSION)
#if !DEVICE_SERIAL_FC || (MBED_VERSION < MBED_ENCODE_VERSION(5, 7, 0))
/*disable HW flow control*/
if(!(_parser.send(SPWFXX_SEND_DISABLE_FC) && _recv_ok()))
{
debug_if(_dbg_on, "\r\nSPWF> error disabling HW flow control\r\n");
return false;
}
#else // DEVICE_SERIAL_FC && (MBED_VERSION >= MBED_ENCODE_VERSION(5, 7, 0))
if((_rts != NC) && (_cts != NC)) {
/*enable HW flow control*/
if(!(_parser.send(SPWFXX_SEND_ENABLE_FC) && _recv_ok()))
{
debug_if(_dbg_on, "\r\nSPWF> error enabling HW flow control\r\n");
return false;
}
/*configure pins for HW flow control*/
_serial.set_flow_control(SerialBase::RTSCTS, _rts, _cts);
} else {
/*disable HW flow control*/
if(!(_parser.send(SPWFXX_SEND_DISABLE_FC) && _recv_ok()))
{
debug_if(_dbg_on, "\r\nSPWF> error disabling HW flow control\r\n");
return false;
}
}
#endif // DEVICE_SERIAL_FC && (MBED_VERSION >= MBED_ENCODE_VERSION(5, 7, 0))
#else // !defined(MBED_MAJOR_VERSION) - Assuming `master` branch
#if !DEVICE_SERIAL_FC
/*disable HW flow control*/
if(!(_parser.send(SPWFXX_SEND_DISABLE_FC) && _recv_ok()))
{
debug_if(_dbg_on, "\r\nSPWF> error disabling HW flow control\r\n");
return false;
}
#else // DEVICE_SERIAL_FC
if((_rts != NC) && (_cts != NC)) {
/*enable HW flow control*/
if(!(_parser.send(SPWFXX_SEND_ENABLE_FC) && _recv_ok()))
{
debug_if(_dbg_on, "\r\nSPWF> error enabling HW flow control\r\n");
return false;
}
/*configure pins for HW flow control*/
_serial.set_flow_control(SerialBase::RTSCTS, _rts, _cts);
} else {
/*disable HW flow control*/
if(!(_parser.send(SPWFXX_SEND_DISABLE_FC) && _recv_ok()))
{
debug_if(_dbg_on, "\r\nSPWF> error disabling HW flow control\r\n");
return false;
}
}
#endif // DEVICE_SERIAL_FC
#endif // !defined(MBED_MAJOR_VERSION)
/* Disable selected WINDs */
_winds_on();
/* sw reset */
if(!reset()) {
debug_if(_dbg_on, "\r\nSPWF> SW reset failed (%s, %d)\r\n", __func__, __LINE__);
return false;
}
#ifndef NDEBUG
if (!(_parser.send(SPWFXX_SEND_GET_CONS_STATE)
&& _recv_ok())) {
debug_if(_dbg_on, "\r\nSPWF> error getting console state\r\n");
return false;
}
if (!(_parser.send(SPWFXX_SEND_GET_CONS_SPEED)
&& _recv_ok())) {
debug_if(_dbg_on, "\r\nSPWF> error getting console speed\r\n");
return false;
}
if (!(_parser.send(SPWFXX_SEND_GET_HWFC_STATE)
&& _recv_ok())) {
debug_if(_dbg_on, "\r\nSPWF> error getting hwfc state\r\n");
return false;
}
#if (MBED_CONF_IDW0XX1_EXPANSION_BOARD == IDW04A1) || defined(IDW01M1_FW_REL_35X)
/* betzw: IDW01M1 FW versions <3.5 seem to have problems with the following two commands.
* For the sake of simplicity, just excluding them for IDW01M1 in general.
*/
if (!(_parser.send(SPWFXX_SEND_GET_CONS_DELIM)
&& _recv_ok())) {
debug_if(_dbg_on, "\r\nSPWF> error getting console delimiter\r\n");
return false;
}
if (!(_parser.send(SPWFXX_SEND_GET_CONS_ERRS)
&& _recv_ok())) {
debug_if(_dbg_on, "\r\nSPWF> error getting console error setting\r\n");
return false;
}
#endif // (MBED_CONF_IDW0XX1_EXPANSION_BOARD == IDW04A1) || defined(IDW01M1_FW_REL_35X)
if (!(_parser.send("AT+S.GCFG=sleep_enabled")
&& _recv_ok())) {
debug_if(_dbg_on, "\r\nSPWF> error getting sleep state enabled\r\n");
return false;
}
if (!(_parser.send("AT+S.GCFG=wifi_powersave")
&& _recv_ok())) {
debug_if(_dbg_on, "\r\nSPWF> error getting powersave mode\r\n");
return false;
}
if (!(_parser.send("AT+S.GCFG=standby_enabled")
&& _recv_ok())) {
debug_if(_dbg_on, "\r\nSPWF> error getting standby state enabled\r\n");
return false;
}
#endif
return true;
}
bool SPWFSAxx::_wait_console_active(void) {
int trials = 0;
while(true) {
if (_parser.recv("+WIND:0:Console active\n") && _recv_delim_lf()) {
debug_if(_dbg_on, "AT^ +WIND:0:Console active\r\n");
return true;
}
if(++trials >= SPWFXX_MAX_TRIALS) {
debug("\r\nSPWF> ERROR: Should never happen! (%s, %d)\r\n", __func__, __LINE__);
empty_rx_buffer();
return false;
}
}
}
bool SPWFSAxx::_wait_wifi_hw_started(void) {
int trials = 0;
while(true) {
if (_parser.recv("+WIND:32:WiFi Hardware Started\n") && _recv_delim_lf()) {
debug_if(_dbg_on, "AT^ +WIND:32:WiFi Hardware Started\r\n");
return true;
}
if(++trials >= SPWFXX_MAX_TRIALS) {
debug("\r\nSPWF> ERROR: Should never happen! (%s, %d)\r\n", __func__, __LINE__);
empty_rx_buffer();
return false;
}
}
}
bool SPWFSAxx::hw_reset(void)
{
#if (MBED_CONF_IDW0XX1_EXPANSION_BOARD != IDW04A1) || !defined(IDW04A1_WIFI_HW_BUG_WA) // betzw: HW reset doesn't work as expected on unmodified X_NUCLEO_IDW04A1 expansion boards
_reset.write(0);
wait_ms(200);
_reset.write(1);
#else // (MBED_CONF_IDW0XX1_EXPANSION_BOARD == IDW04A1) && defined(IDW04A1_WIFI_HW_BUG_WA): substitute with SW reset
_parser.send(SPWFXX_SEND_SW_RESET);
#endif // (MBED_CONF_IDW0XX1_EXPANSION_BOARD == IDW04A1) && defined(IDW04A1_WIFI_HW_BUG_WA)
return _wait_console_active();
}
bool SPWFSAxx::reset(void)
{
bool ret;
/* save current setting in flash */
if(!(_parser.send(SPWFXX_SEND_SAVE_SETTINGS) && _recv_ok()))
{
debug_if(_dbg_on, "\r\nSPWF> error saving configuration to flash (%s, %d)\r\n", __func__, __LINE__);
return false;
}
if(!_parser.send(SPWFXX_SEND_SW_RESET)) return false; /* betzw - NOTE: "keep the current state and reset the device".
We assume that the module informs us about the
eventual closing of sockets via "WIND" asynchronous
indications! So everything regarding the clean-up
of these situations is handled there. */
/* waiting for HW to start */
ret = _wait_wifi_hw_started();
return ret;
}
/* Security Mode
None = 0,
WEP = 1,
WPA_Personal = 2,
*/
bool SPWFSAxx::connect(const char *ap, const char *passPhrase, int securityMode)
{
int trials;
BlockExecuter netsock_wa_obj(Callback<void()>(this, &SPWFSAxx::_unblock_event_callback),
Callback<void()>(this, &SPWFSAxx::_block_event_callback)); /* disable calling (external) callback in IRQ context */
//AT+S.SCFG=wifi_wpa_psk_text,%s
if(!(_parser.send("AT+S.SCFG=wifi_wpa_psk_text,%s", passPhrase) && _recv_ok()))
{
debug_if(_dbg_on, "\r\nSPWF> error pass set\r\n");
return false;
}
//AT+S.SSIDTXT=%s
if(!(_parser.send("AT+S.SSIDTXT=%s", ap) && _recv_ok()))
{
debug_if(_dbg_on, "\r\nSPWF> error ssid set\r\n");
return false;
}
//AT+S.SCFG=wifi_priv_mode,%d
if(!(_parser.send("AT+S.SCFG=wifi_priv_mode,%d", securityMode) && _recv_ok()))
{
debug_if(_dbg_on, "\r\nSPWF> error security mode set\r\n");
return false;
}
/*set STA mode (0->idle, 1->STA,3->miniAP, 2->IBSS)*/
if(!(_parser.send("AT+S.SCFG=wifi_mode,1") && _recv_ok()))
{
debug_if(_dbg_on, "\r\nSPWF> error WiFi mode set 1 (STA)\r\n");
return false;
}
/* sw reset */
if(!reset()) {
debug_if(_dbg_on, "\r\nSPWF> SW reset failed (%s, %d)\r\n", __func__, __LINE__);
return false;
}
trials = 0;
while(true) {
if(_parser.recv("%255[^\n]\n", _msg_buffer) && _recv_delim_lf())
{
if(strstr(_msg_buffer, ":24:") != NULL) { // WiFi Up
debug_if(_dbg_on, "AT^ %s\n", _msg_buffer);
if(strchr(_msg_buffer, '.') != NULL) { // IPv4 address
break;
} else {
continue;
}
}
if(strstr(_msg_buffer, ":40:") != NULL) { // Deauthentication
debug_if(_dbg_on, "AT~ %s\n", _msg_buffer);
if(++trials < SPWFXX_MAX_TRIALS) { // give it three trials
continue;
}
disconnect();
empty_rx_buffer();
return false;
} else {
debug_if(_dbg_on, "AT] %s\n", _msg_buffer);
}
continue;
}
if(++trials >= SPWFXX_MAX_TRIALS) {
debug("\r\nSPWF> ERROR: Should never happen! (%s, %d)\r\n", __func__, __LINE__);
empty_rx_buffer();
return false;
}
}
return true;
}
bool SPWFSAxx::disconnect(void)
{
BlockExecuter netsock_wa_obj(Callback<void()>(this, &SPWFSAxx::_unblock_event_callback),
Callback<void()>(this, &SPWFSAxx::_block_event_callback)); /* disable calling (external) callback in IRQ context */
#if MBED_CONF_IDW0XX1_EXPANSION_BOARD == IDW04A1
/*disable Wi-Fi device*/
if(!(_parser.send("AT+S.WIFI=0") && _recv_ok()))
{
debug_if(_dbg_on, "\r\nSPWF> error disabling WiFi\r\n");
return false;
}
#endif // IDW04A1
/*set idle mode (0->idle, 1->STA,3->miniAP, 2->IBSS)*/
if(!(_parser.send("AT+S.SCFG=wifi_mode,0") && _recv_ok()))
{
debug_if(_dbg_on, "\r\nSPWF> error WiFi mode set idle (%d)\r\n", __LINE__);
return false;
}
#if MBED_CONF_IDW0XX1_EXPANSION_BOARD == IDW04A1
/*enable Wi-Fi device*/
if(!(_parser.send("AT+S.WIFI=1") && _recv_ok()))
{
debug_if(_dbg_on, "\r\nSPWF> error enabling WiFi\r\n");
return false;
}
#endif // IDW04A1
// reset module
if(!reset()) {
debug_if(_dbg_on, "\r\nSPWF> SW reset failed (%s, %d)\r\n", __func__, __LINE__);
return false;
}
/* clean up state */
_associated_interface.inner_constructor();
_free_all_packets();
return true;
}
const char *SPWFSAxx::getIPAddress(void)
{
unsigned int n1, n2, n3, n4;
BlockExecuter netsock_wa_obj(Callback<void()>(this, &SPWFSAxx::_unblock_event_callback),
Callback<void()>(this, &SPWFSAxx::_block_event_callback)); /* disable calling (external) callback in IRQ context */
if (!(_parser.send("AT+S.STS=ip_ipaddr")
&& _parser.recv(SPWFXX_RECV_IP_ADDR, &n1, &n2, &n3, &n4)
&& _recv_ok())) {
debug_if(_dbg_on, "\r\nSPWF> get IP address error\r\n");
return NULL;
}
debug_if(_dbg_on, "AT^ ip_ipaddr = %u.%u.%u.%u\r\n", n1, n2, n3, n4);
sprintf((char*)_ip_buffer,"%u.%u.%u.%u", n1, n2, n3, n4);
return _ip_buffer;
}
const char *SPWFSAxx::getGateway(void)
{
unsigned int n1, n2, n3, n4;
BlockExecuter netsock_wa_obj(Callback<void()>(this, &SPWFSAxx::_unblock_event_callback),
Callback<void()>(this, &SPWFSAxx::_block_event_callback)); /* disable calling (external) callback in IRQ context */
if (!(_parser.send("AT+S.STS=ip_gw")
&& _parser.recv(SPWFXX_RECV_GATEWAY, &n1, &n2, &n3, &n4)
&& _recv_ok())) {
debug_if(_dbg_on, "\r\nSPWF> get gateway error\r\n");
return NULL;
}
debug_if(_dbg_on, "AT^ ip_gw = %u.%u.%u.%u\r\n", n1, n2, n3, n4);
sprintf((char*)_gateway_buffer,"%u.%u.%u.%u", n1, n2, n3, n4);
return _gateway_buffer;
}
const char *SPWFSAxx::getNetmask(void)
{
unsigned int n1, n2, n3, n4;
BlockExecuter netsock_wa_obj(Callback<void()>(this, &SPWFSAxx::_unblock_event_callback),
Callback<void()>(this, &SPWFSAxx::_block_event_callback)); /* disable calling (external) callback in IRQ context */
if (!(_parser.send("AT+S.STS=ip_netmask")
&& _parser.recv(SPWFXX_RECV_NETMASK, &n1, &n2, &n3, &n4)
&& _recv_ok())) {
debug_if(_dbg_on, "\r\nSPWF> get netmask error\r\n");
return NULL;
}
debug_if(_dbg_on, "AT^ ip_netmask = %u.%u.%u.%u\r\n", n1, n2, n3, n4);
sprintf((char*)_netmask_buffer,"%u.%u.%u.%u", n1, n2, n3, n4);
return _netmask_buffer;
}
int8_t SPWFSAxx::getRssi(void)
{
int ret;
BlockExecuter netsock_wa_obj(Callback<void()>(this, &SPWFSAxx::_unblock_event_callback),
Callback<void()>(this, &SPWFSAxx::_block_event_callback)); /* disable calling (external) callback in IRQ context */
if (!(_parser.send("AT+S.PEERS=0,rx_rssi")
&& _parser.recv(SPWFXX_RECV_RX_RSSI, &ret)
&& _recv_ok())) {
debug_if(_dbg_on, "\r\nSPWF> get RX rssi error\r\n");
return 0;
}
return (int8_t)ret;
}
const char *SPWFSAxx::getMACAddress(void)
{
unsigned int n1, n2, n3, n4, n5, n6;
BlockExecuter netsock_wa_obj(Callback<void()>(this, &SPWFSAxx::_unblock_event_callback),
Callback<void()>(this, &SPWFSAxx::_block_event_callback)); /* disable calling (external) callback in IRQ context */
if (!(_parser.send("AT+S.GCFG=nv_wifi_macaddr")
&& _parser.recv(SPWFXX_RECV_MAC_ADDR, &n1, &n2, &n3, &n4, &n5, &n6)
&& _recv_ok())) {
debug_if(_dbg_on, "\r\nSPWF> get MAC address error\r\n");
return 0;
}
debug_if(_dbg_on, "AT^ nv_wifi_macaddr = %x:%x:%x:%x:%x:%x\r\n", n1, n2, n3, n4, n5, n6);
sprintf((char*)_mac_buffer,"%02X:%02X:%02X:%02X:%02X:%02X", n1, n2, n3, n4, n5, n6);
return _mac_buffer;
}
bool SPWFSAxx::isConnected(void)
{
return _associated_interface._connected_to_network;
}
nsapi_size_or_error_t SPWFSAxx::send(int spwf_id, const void *data, uint32_t amount, int internal_id)
{
uint32_t sent = 0U, to_send;
nsapi_size_or_error_t ret;
BlockExecuter netsock_wa_obj(Callback<void()>(this, &SPWFSAxx::_unblock_event_callback),
Callback<void()>(this, &SPWFSAxx::_block_event_callback)); /* disable calling (external) callback in IRQ context */
_process_winds(); // perform async indication handling (to early detect eventually closed sockets)
/* betzw - WORK AROUND module FW issues: split up big packages in smaller ones */
for(to_send = (amount > SPWFXX_SEND_RECV_PKTSIZE) ? SPWFXX_SEND_RECV_PKTSIZE : amount;
sent < amount;
to_send = ((amount - sent) > SPWFXX_SEND_RECV_PKTSIZE) ? SPWFXX_SEND_RECV_PKTSIZE : (amount - sent)) {
{
BlockExecuter bh_handler(Callback<void()>(this, &SPWFSAxx::_execute_bottom_halves));
// betzw - TODO: handle different errors more accurately!
if (!_associated_interface._socket_is_still_connected(internal_id)) {
debug_if(_dbg_on, "\r\nSPWF> Socket not connected anymore: sent=%u, to_send=%u! (%s, %d)\r\n", sent, to_send, __func__, __LINE__);
break;
} else if(!_parser.send("AT+S.SOCKW=%d,%d", spwf_id, (unsigned int)to_send)) {
debug_if(_dbg_on, "\r\nSPWF> Sending command failed: sent=%u, to_send=%u! (%s, %d)\r\n", sent, to_send, __func__, __LINE__);
break;
} else if(_parser.write(((char*)data)+sent, (int)to_send) != (int)to_send) {
debug_if(_dbg_on, "\r\nSPWF> Sending data failed: sent=%u, to_send=%u! (%s, %d)\r\n", sent, to_send, __func__, __LINE__);
break;
} else if(!_recv_ok()) {
debug_if(_dbg_on, "\r\nSPWF> Sending did not receive OK: sent=%u, to_send=%u! (%s, %d)\r\n", sent, to_send, __func__, __LINE__);
break;
}
}
sent += to_send;
}
if(sent > 0) { // `sent == 0` indicates a potential error
ret = sent;
} else if(amount == 0) {
ret = NSAPI_ERROR_OK;
} else if(_associated_interface._socket_is_still_connected(internal_id)) {
ret = NSAPI_ERROR_DEVICE_ERROR;
} else {
ret = NSAPI_ERROR_CONNECTION_LOST;
}
return ret;
}
int SPWFSAxx::_read_len(int spwf_id) {
unsigned int amount;
if (!(_parser.send("AT+S.SOCKQ=%d", spwf_id)
&& _parser.recv(SPWFXX_RECV_DATALEN, &amount)
&& _recv_ok())) {
debug_if(_dbg_on, "\r\nSPWF> %s failed\r\n", __func__);
return SPWFXX_ERR_LEN;
}
if(amount > 0) {
debug_if(_dbg_on, "\r\nSPWF> %s():\t\t%d:%d\r\n", __func__, spwf_id, amount);
}
MBED_ASSERT(((int)amount) >= 0);
return (int)amount;
}
#define SPWFXX_WINDS_OFF "0xFFFFFFFF"
void SPWFSAxx::_winds_on(void) {
MBED_ASSERT(_is_event_callback_blocked());
if(!(_parser.send(SPWFXX_SEND_WIND_OFF_HIGH SPWFXX_WINDS_HIGH_ON) && _recv_ok())) {
debug_if(_dbg_on, "\r\nSPWF> %s failed at line #%d\r\n", __func__, __LINE__);
}
if(!(_parser.send(SPWFXX_SEND_WIND_OFF_MEDIUM SPWFXX_WINDS_MEDIUM_ON) && _recv_ok())) {
debug_if(_dbg_on, "\r\nSPWF> %s failed at line #%d\r\n", __func__, __LINE__);
}
if(!(_parser.send(SPWFXX_SEND_WIND_OFF_LOW SPWFXX_WINDS_LOW_ON) && _recv_ok())) {
debug_if(_dbg_on, "\r\nSPWF> %s failed at line #%d\r\n", __func__, __LINE__);
}
}
/* Define beyond macro in case you want to report back failures in switching off WINDs to the caller */
// #define SPWFXX_SOWF
/* Note: in case of error blocking has been (tried to be) lifted */
bool SPWFSAxx::_winds_off(void) {
MBED_ASSERT(_is_event_callback_blocked());
if (!(_parser.send(SPWFXX_SEND_WIND_OFF_LOW SPWFXX_WINDS_OFF)
&& _recv_ok())) {
debug_if(_dbg_on, "\r\nSPWF> %s failed at line #%d\r\n", __func__, __LINE__);
#ifdef SPWFXX_SOWF // betzw: try to continue
_winds_on();
return false;
#endif
}
if (!(_parser.send(SPWFXX_SEND_WIND_OFF_MEDIUM SPWFXX_WINDS_OFF)
&& _recv_ok())) {
debug_if(_dbg_on, "\r\nSPWF> %s failed at line #%d\r\n", __func__, __LINE__);
#ifdef SPWFXX_SOWF // betzw: try to continue
_winds_on();
return false;
#endif
}
if (!(_parser.send(SPWFXX_SEND_WIND_OFF_HIGH SPWFXX_WINDS_OFF)
&& _recv_ok())) {
debug_if(_dbg_on, "\r\nSPWF> %s failed at line #%d\r\n", __func__, __LINE__);
#ifdef SPWFXX_SOWF // betzw: try to continue
_winds_on();
return false;
#endif
}
return true;
}
void SPWFSAxx::_execute_bottom_halves(void) {
_network_lost_handler_bh();
_packet_handler_bh();
}
void SPWFSAxx::_read_in_pending(void) {
static int internal_id_cnt = 0;
while(_is_data_pending()) {
if(_associated_interface._socket_has_connected(internal_id_cnt)) {
int spwf_id = _associated_interface._ids[internal_id_cnt].spwf_id;
if(_is_data_pending(spwf_id)) {
int amount;
amount = _read_in_pkt(spwf_id, false);
if(amount == SPWFXX_ERR_OOM) { /* consider only 'SPWFXX_ERR_OOM' as non recoverable */
return;
}
}
if(!_is_data_pending(spwf_id)) {
internal_id_cnt++;
internal_id_cnt %= SPWFSA_SOCKET_COUNT;
}
} else {
internal_id_cnt++;
internal_id_cnt %= SPWFSA_SOCKET_COUNT;
}
}
}
/* Note: returns
* 'SPWFXX_ERR_OK' in case of success
* 'SPWFXX_ERR_OOM' in case of "out of memory"
* 'SPWFXX_ERR_READ' in case of `_read_in()` error
*/
int SPWFSAxx::_read_in_packet(int spwf_id, uint32_t amount) {
struct packet *packet = (struct packet*)malloc(sizeof(struct packet) + amount);
if (!packet) {
#ifndef NDEBUG
error("\r\nSPWF> %s(%d): Out of memory!\r\n", __func__, __LINE__);
#else // NDEBUG
debug("\r\nSPWF> %s(%d): Out of memory!\r\n", __func__, __LINE__);
#endif
debug_if(_dbg_on, "\r\nSPWF> %s failed (%d)\r\n", __func__, __LINE__);
return SPWFXX_ERR_OOM; /* out of memory: give up here! */
}
/* init packet */
packet->id = spwf_id;
packet->len = amount;
packet->next = 0;
/* read data in */
if(!(_read_in((char*)(packet + 1), spwf_id, amount) > 0)) {
free(packet);
debug_if(_dbg_on, "\r\nSPWF> %s failed (%d)\r\n", __func__, __LINE__);
return SPWFXX_ERR_READ;
} else {
debug_if(_dbg_on, "\r\nSPWF> %s():\t%d:%d\r\n", __func__, spwf_id, amount);
/* append to packet list */
*_packets_end = packet;
_packets_end = &packet->next;
/* force call of (external) callback */
_call_callback();
}
return SPWFXX_ERR_OK;
}
void SPWFSAxx::_free_packets(int spwf_id) {
// check if any packets are ready for `spwf_id`
for(struct packet **p = &_packets; *p;) {
if ((*p)->id == spwf_id) {
struct packet *q = *p;
if (_packets_end == &(*p)->next) {
_packets_end = p;
}
*p = (*p)->next;
free(q);
} else {
p = &(*p)->next;
}
}
}
void SPWFSAxx::_free_all_packets() {
for (int spwf_id = 0; spwf_id < SPWFSA_SOCKET_COUNT; spwf_id++) {
_free_packets(spwf_id);
}
}
bool SPWFSAxx::close(int spwf_id)
{
bool ret = false;
BlockExecuter netsock_wa_obj(Callback<void()>(this, &SPWFSAxx::_unblock_event_callback),
Callback<void()>(this, &SPWFSAxx::_block_event_callback)); /* disable calling (external) callback in IRQ context */
MBED_ASSERT(((unsigned int)spwf_id) < ((unsigned int)SPWFSA_SOCKET_COUNT)); // `spwf_id` is valid
for(int retry_cnt = 0; retry_cnt < SPWFXX_MAX_TRIALS; retry_cnt++) {
Timer timer;
timer.start();
// Flush out pending data
while(true) {
int amount = _read_in_pkt(spwf_id, true);
if(amount < 0) { // SPWFXX error
/* empty RX buffer & try to close */
empty_rx_buffer();
break;
}
if(amount == 0) break; // no more data to be read
/* Try to work around module API bug:
* break out & try to close after 20 seconds
*/
if(timer.read() > 20) {
break;
}
/* immediately free packet(s) (to avoid "out of memory") */
_free_packets(spwf_id);
/* interleave bottom halves */
_execute_bottom_halves();
}
// Close socket
if (_parser.send("AT+S.SOCKC=%d", spwf_id)
&& _recv_ok()) {
ret = true;
break; // finish closing
} else { // close failed
debug_if(_dbg_on, "\r\nSPWF> %s failed (%d)\r\n", __func__, __LINE__);
/* interleave bottom halves */
_execute_bottom_halves();
/* free packets */
_free_packets(spwf_id);
}
}
/* anticipate bottom halves */
_execute_bottom_halves();
if(ret) {
/* clear pending data flag (should be redundant) */
_clear_pending_data(spwf_id);
/* free packets for this socket */
_free_packets(spwf_id);
/* reset pending data sizes */
_reset_pending_pkt_sizes(spwf_id);
} else {
debug_if(_dbg_on, "\r\nSPWF> SPWFSAxx::close failed (%d)\r\n", __LINE__);
int internal_id = _associated_interface.get_internal_id(spwf_id);
if(!_associated_interface._socket_is_still_connected(internal_id)) {
/* clear pending data flag (should be redundant) */
_clear_pending_data(spwf_id);
/* free packets for this socket */
_free_packets(spwf_id);
/* reset pending data sizes */
_reset_pending_pkt_sizes(spwf_id);
ret = true;
}
}
return ret;
}
/*
* Buffered serial event handler
*
* Note: executed in IRQ context!
* Note: do not call (external) callback in IRQ context while performing critical module operations
*/
void SPWFSAxx::_event_handler(void)
{
if(!_is_event_callback_blocked()) {
_call_callback();
}
}
/*
* Common error handler
*/
void SPWFSAxx::_error_handler(void)
{
if(_parser.recv("%255[^\n]\n", _msg_buffer) && _recv_delim_lf()) {
debug_if(_dbg_on, "AT^ ERROR:%s (%d)\r\n", _msg_buffer, __LINE__);
} else {
debug_if(_dbg_on, "\r\nSPWF> Unknown ERROR string in SPWFSAxx::_error_handler (%d)\r\n", __LINE__);
}
/* force call of (external) callback */
_call_callback();
}
/*
* Handling oob ("+WIND:33:WiFi Network Lost")
*/
void SPWFSAxx::_network_lost_handler_th(void)
{
#ifndef NDEBUG
static unsigned int net_loss_cnt = 0;
net_loss_cnt++;
#endif
_recv_delim_cr_lf();
debug_if(_dbg_on, "AT^ +WIND:33:WiFi Network Lost\r\n");
#ifndef NDEBUG
debug_if(_dbg_on, "\r\nSPWF> Getting out of SPWFSAxx::_network_lost_handler_th: %d\r\n", net_loss_cnt);
#else // NDEBUG
debug_if(_dbg_on, "\r\nSPWF> Getting out of SPWFSAxx::_network_lost_handler_th: %d\r\n", __LINE__);
#endif // NDEBUG
/* set flag to signal network loss */
_network_lost_flag = true;
/* force call of (external) callback */
_call_callback();
return;
}
/* betzw - WORK AROUND module FW issues: split up big packages in smaller ones */
void SPWFSAxx::_add_pending_packet_sz(int spwf_id, uint32_t size) {
uint32_t to_add;
uint32_t added = _get_cumulative_size(spwf_id);
if(size <= added) { // might happen due to delayed WIND delivery
debug_if(_dbg_on, "\r\nSPWF> WARNING: %s failed at line #%d\r\n", __func__, __LINE__);
return;
}
for(to_add = ((size - added) > SPWFXX_SEND_RECV_PKTSIZE) ? SPWFXX_SEND_RECV_PKTSIZE : (size - added);
added < size;
to_add = ((size - added) > SPWFXX_SEND_RECV_PKTSIZE) ? SPWFXX_SEND_RECV_PKTSIZE : (size - added)) {
_add_pending_pkt_size(spwf_id, added + to_add);
added += to_add;
}
/* force call of (external) callback */
_call_callback();
/* set that data is pending */
_set_pending_data(spwf_id);
}
/*
* Handling oob ("+WIND:55:Pending Data")
*/
void SPWFSAxx::_packet_handler_th(void)
{
int internal_id, spwf_id;
int amount;
/* parse out the socket id & amount */
if (!(_parser.recv(SPWFXX_RECV_PENDING_DATA, &spwf_id, &amount) && _recv_delim_lf())) {
#ifndef NDEBUG
error("\r\nSPWF> SPWFSAxx::%s failed!\r\n", __func__);
#endif
return;
}
debug_if(_dbg_on, "AT^ +WIND:55:Pending Data:%d:%d\r\n", spwf_id, amount);
/* check for the module to report a valid id */
MBED_ASSERT(((unsigned int)spwf_id) < ((unsigned int)SPWFSA_SOCKET_COUNT));
/* set that there is pending data for socket */
/* NOTE: it seems as if asynchronous indications might report not up-to-date data length values
* therefore we just record the socket id without considering the `amount` of data reported!
*/
internal_id = _associated_interface.get_internal_id(spwf_id);
if(internal_id != SPWFSA_SOCKET_COUNT) {
debug_if(_dbg_on, "AT^ +WIND:55:Pending Data:%d:%d - #2\r\n", spwf_id, amount);
_add_pending_packet_sz(spwf_id, amount);
MBED_ASSERT(_get_pending_pkt_size(spwf_id) != 0);
} else {
debug_if(_dbg_on, "\r\nSPWFSAxx::%s got invalid id %d\r\n", __func__, spwf_id);
}
}
void SPWFSAxx::_network_lost_handler_bh(void)
{
if(!_network_lost_flag) return;
_network_lost_flag = false;
{
bool were_connected;
BlockExecuter netsock_wa_obj(Callback<void()>(this, &SPWFSAxx::_unblock_event_callback),
Callback<void()>(this, &SPWFSAxx::_block_event_callback)); /* do not call (external) callback in IRQ context as long as network is lost */
Timer timer;
timer.start();
_parser.set_timeout(SPWF_NETLOST_TIMEOUT);
were_connected = isConnected();
_associated_interface._connected_to_network = false;
if(were_connected) {
unsigned int n1, n2, n3, n4;
while(true) {
if (timer.read_ms() > SPWF_CONNECT_TIMEOUT) {
debug_if(_dbg_on, "\r\nSPWF> SPWFSAxx::_network_lost_handler_bh() #%d\r\n", __LINE__);
disconnect();
empty_rx_buffer();
goto nlh_get_out;
}
if((_parser.recv(SPWFXX_RECV_WIFI_UP, &n1, &n2, &n3, &n4)) && _recv_delim_lf()) {
debug_if(_dbg_on, "\r\nSPWF> Re-connected (%u.%u.%u.%u)!\r\n", n1, n2, n3, n4);
_associated_interface._connected_to_network = true;
goto nlh_get_out;
}
}
} else {
debug_if(_dbg_on, "\r\nSPWF> Leaving SPWFSAxx::_network_lost_handler_bh\r\n");
goto nlh_get_out;
}
nlh_get_out:
debug_if(_dbg_on, "\r\nSPWF> Getting out of SPWFSAxx::_network_lost_handler_bh\r\n");
_parser.set_timeout(_timeout);
/* force call of (external) callback */
_call_callback();
return;
}
}
void SPWFSAxx::_recover_from_hard_faults(void) {
disconnect();
empty_rx_buffer();