/
wifi.c
1289 lines (1129 loc) · 33.6 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
// Module for interfacing with WIFI
//#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"
#include "platform.h"
#include "auxmods.h"
#include "lrotable.h"
#include "c_string.h"
#include "c_stdlib.h"
#include "c_types.h"
#include "user_interface.h"
#include "smart.h"
#include "smartconfig.h"
static int wifi_smart_succeed = LUA_NOREF;
static uint8 getap_output_format = 0;
#if defined( NODE_SMART_OLDSTYLE )
#else
static lua_State* smart_L = NULL;
#endif
static void wifi_smart_succeed_cb(sc_status status, void *arg)
{
switch(status) {
// case SC_STATUS_WAIT:
// os_printf("SC_STATUS_WAIT\n");
// break;
// case SC_STATUS_FIND_CHANNEL:
// os_printf("SC_STATUS_FIND_CHANNEL\n");
// break;
// case SC_STATUS_GETTING_SSID_PSWD:
// os_printf("SC_STATUS_GETTING_SSID_PSWD\n");
// sc_type *type = pdata;
// if (*type == SC_TYPE_ESPTOUCH) os_printf("SC_TYPE:SC_TYPE_ESPTOUCH\n");
// else os_printf("SC_TYPE:SC_TYPE_AIRKISS\n");
// break;
case SC_STATUS_LINK:
NODE_DBG("wifi_smart_succeed_cb is called.\n");
struct station_config *sta_conf = arg;
wifi_station_set_config(sta_conf);
wifi_station_disconnect();
wifi_station_connect();
if(wifi_smart_succeed != LUA_NOREF)
{
lua_rawgeti(smart_L, LUA_REGISTRYINDEX, wifi_smart_succeed);
lua_pushstring(smart_L, sta_conf->ssid);
lua_pushstring(smart_L, sta_conf->password);
lua_call(smart_L, 2, 0);
luaL_unref(smart_L, LUA_REGISTRYINDEX, wifi_smart_succeed);
wifi_smart_succeed = LUA_NOREF;
}
smartconfig_stop();
break;
case SC_STATUS_LINK_OVER:
// os_printf("SC_STATUS_LINK_OVER\n");
// if (pdata != NULL) {
// uint8 phone_ip[4] = {0};
// memcpy(phone_ip, (uint8*)pdata, 4);
// os_printf("Phone ip: %d.%d.%d.%d\n",phone_ip[0],phone_ip[1],phone_ip[2],phone_ip[3]);
// }
smartconfig_stop();
break;
}
}
static int wifi_scan_succeed = LUA_NOREF;
static lua_State* gL = NULL;
/**
* @brief Wifi ap scan over callback to display.
* @param arg: contain the aps information
* @param status: scan over status
* @retval None
*/
static void wifi_scan_done(void *arg, STATUS status)
{
uint8 ssid[33];
char temp[128];
if(wifi_scan_succeed == LUA_NOREF)
return;
if(arg == NULL)
return;
lua_rawgeti(gL, LUA_REGISTRYINDEX, wifi_scan_succeed);
if (status == OK)
{
struct bss_info *bss_link = (struct bss_info *)arg;
bss_link = bss_link->next.stqe_next;//ignore first
lua_newtable( gL );
while (bss_link != NULL)
{
c_memset(ssid, 0, 33);
if (c_strlen(bss_link->ssid) <= 32)
{
c_memcpy(ssid, bss_link->ssid, c_strlen(bss_link->ssid));
}
else
{
c_memcpy(ssid, bss_link->ssid, 32);
}
if(getap_output_format==1) //use new format(BSSID : SSID, RSSI, Authmode, Channel)
{
c_sprintf(temp,"%s,%d,%d,%d", ssid, bss_link->rssi, bss_link->authmode, bss_link->channel);
lua_pushstring(gL, temp);
NODE_DBG(MACSTR" : %s\n",MAC2STR(bss_link->bssid) , temp);
c_sprintf(temp,MACSTR, MAC2STR(bss_link->bssid));
lua_setfield( gL, -2, temp);
}
else//use old format(SSID : Authmode, RSSI, BSSID, Channel)
{
c_sprintf(temp,"%d,%d,"MACSTR",%d", bss_link->authmode, bss_link->rssi, MAC2STR(bss_link->bssid),bss_link->channel);
lua_pushstring(gL, temp);
lua_setfield( gL, -2, ssid );
NODE_DBG("%s : %s\n", ssid, temp);
}
bss_link = bss_link->next.stqe_next;
}
}
else
{
lua_newtable( gL );
}
lua_call(gL, 1, 0);
if(wifi_scan_succeed != LUA_NOREF)
{
luaL_unref(gL, LUA_REGISTRYINDEX, wifi_scan_succeed);
wifi_scan_succeed = LUA_NOREF;
}
}
// Lua: smart(channel, function succeed_cb)
// Lua: smart(type, function succeed_cb)
static int wifi_start_smart( lua_State* L )
{
#if defined( NODE_SMART_OLDSTYLE )
unsigned channel;
int stack = 1;
if ( lua_isnumber(L, stack) ){
channel = lua_tointeger(L, stack);
stack++;
} else {
channel = 6;
}
// luaL_checkanyfunction(L, stack);
if (lua_type(L, stack) == LUA_TFUNCTION || lua_type(L, stack) == LUA_TLIGHTFUNCTION){
lua_pushvalue(L, stack); // copy argument (func) to the top of stack
if(wifi_smart_succeed != LUA_NOREF)
luaL_unref(L, LUA_REGISTRYINDEX, wifi_smart_succeed);
wifi_smart_succeed = luaL_ref(L, LUA_REGISTRYINDEX);
}
if ( channel > 14 || channel < 1 )
return luaL_error( L, "wrong arg range" );
if(wifi_smart_succeed == LUA_NOREF){
smart_begin(channel, NULL, NULL);
}else{
smart_begin(channel, (smart_succeed )wifi_smart_succeed_cb, L);
}
#else
if(wifi_get_opmode() != STATION_MODE)
{
return luaL_error( L, "Smart link only in STATION mode" );
}
uint8_t smart_type = 0;
int stack = 1;
smart_L = L;
if ( lua_isnumber(L, stack) )
{
smart_type = lua_tointeger(L, stack);
stack++;
}
if (lua_type(L, stack) == LUA_TFUNCTION || lua_type(L, stack) == LUA_TLIGHTFUNCTION)
{
lua_pushvalue(L, stack); // copy argument (func) to the top of stack
if(wifi_smart_succeed != LUA_NOREF)
luaL_unref(L, LUA_REGISTRYINDEX, wifi_smart_succeed);
wifi_smart_succeed = luaL_ref(L, LUA_REGISTRYINDEX);
}
if ( smart_type > 1 )
return luaL_error( L, "wrong arg range" );
// smartconfig_start(smart_type, wifi_smart_succeed_cb);
smartconfig_start(wifi_smart_succeed_cb);
#endif // defined( NODE_SMART_OLDSTYLE )
return 0;
}
// Lua: exit_smart()
static int wifi_exit_smart( lua_State* L )
{
#if defined( NODE_SMART_OLDSTYLE )
smart_end();
#else
smartconfig_stop();
#endif // defined( NODE_SMART_OLDSTYLE )
if(wifi_smart_succeed != LUA_NOREF)
luaL_unref(L, LUA_REGISTRYINDEX, wifi_smart_succeed);
wifi_smart_succeed = LUA_NOREF;
return 0;
}
// Lua: realmode = setmode(mode)
static int wifi_setmode( lua_State* L )
{
unsigned mode;
mode = luaL_checkinteger( L, 1 );
if ( mode != STATION_MODE && mode != SOFTAP_MODE && mode != STATIONAP_MODE )
return luaL_error( L, "wrong arg type" );
wifi_set_opmode( (uint8_t)mode);
mode = (unsigned)wifi_get_opmode();
lua_pushinteger( L, mode );
return 1;
}
// Lua: realmode = getmode()
static int wifi_getmode( lua_State* L )
{
unsigned mode;
mode = (unsigned)wifi_get_opmode();
lua_pushinteger( L, mode );
return 1;
}
/**
* wifi.getchannel()
* Description:
* Get current wifi Channel
*
* Syntax:
* wifi.getchannel()
* Parameters:
* nil
*
* Returns:
* Current wifi channel
*/
static int wifi_getchannel( lua_State* L )
{
unsigned channel;
channel = (unsigned)wifi_get_channel();
lua_pushinteger( L, channel );
return 1;
}
/**
* wifi.setphymode()
* Description:
* Set wifi physical mode(802.11 b/g/n)
* Note: SoftAP only supports 802.11 b/g.
* Syntax:
* wifi.setphymode(mode)
* Parameters:
* mode:
* wifi.PHYMODE_B
* wifi.PHYMODE_G
* wifi.PHYMODE_N
* Returns:
* Current physical mode after setup
*/
static int wifi_setphymode( lua_State* L )
{
unsigned mode;
mode = luaL_checkinteger( L, 1 );
if ( mode != PHY_MODE_11B && mode != PHY_MODE_11G && mode != PHY_MODE_11N )
return luaL_error( L, "wrong arg type" );
wifi_set_phy_mode( (uint8_t)mode);
mode = (unsigned)wifi_get_phy_mode();
lua_pushinteger( L, mode );
return 1;
}
/**
* wifi.getphymode()
* Description:
* Get wifi physical mode(802.11 b/g/n)
* Syntax:
* wifi.getphymode()
* Parameters:
* nil
* Returns:
* Current physical mode.
*
*/
static int wifi_getphymode( lua_State* L )
{
unsigned mode;
mode = (unsigned)wifi_get_phy_mode();
lua_pushinteger( L, mode );
return 1;
}
// Lua: mac = wifi.xx.getmac()
static int wifi_getmac( lua_State* L, uint8_t mode )
{
char temp[64];
uint8_t mac[6];
wifi_get_macaddr(mode, mac);
c_sprintf(temp, MACSTR, MAC2STR(mac));
lua_pushstring( L, temp );
return 1;
}
// Lua: mac = wifi.xx.setmac()
static int wifi_setmac( lua_State* L, uint8_t mode )
{
uint8_t mac[6];
unsigned len = 0;
const char *macaddr = luaL_checklstring( L, 1, &len );
if(len!=17)
return luaL_error( L, "wrong arg type" );
os_str2macaddr(mac, macaddr);
lua_pushboolean(L,wifi_set_macaddr(mode, (uint8 *)mac));
return 1;
}
// Lua: ip = wifi.xx.getip()
static int wifi_getip( lua_State* L, uint8_t mode )
{
struct ip_info pTempIp;
char temp[64];
wifi_get_ip_info(mode, &pTempIp);
if(pTempIp.ip.addr==0){
lua_pushnil(L);
return 1;
} else {
c_sprintf(temp, "%d.%d.%d.%d", IP2STR(&pTempIp.ip) );
lua_pushstring( L, temp );
c_sprintf(temp, "%d.%d.%d.%d", IP2STR(&pTempIp.netmask) );
lua_pushstring( L, temp );
c_sprintf(temp, "%d.%d.%d.%d", IP2STR(&pTempIp.gw) );
lua_pushstring( L, temp );
return 3;
}
}
// Lua: broadcast = wifi.xx.getbroadcast()
static int wifi_getbroadcast( lua_State* L, uint8_t mode )
{
struct ip_info pTempIp;
char temp[64];
wifi_get_ip_info(mode, &pTempIp);
if(pTempIp.ip.addr==0){
lua_pushnil(L);
return 1;
} else {
struct ip_addr broadcast_address;
uint32 subnet_mask32 = pTempIp.netmask.addr & pTempIp.ip.addr;
uint32 broadcast_address32 = ~pTempIp.netmask.addr | subnet_mask32;
broadcast_address.addr = broadcast_address32;
c_sprintf(temp, "%d.%d.%d.%d", IP2STR(&broadcast_address) );
lua_pushstring( L, temp );
return 1;
}
}
static uint32_t parse_key(lua_State* L, const char * key){
lua_getfield(L, 1, key);
if( lua_isstring(L, -1) ) // deal with the ip/netmask/gw string
{
const char *ip = luaL_checkstring( L, -1 );
return ipaddr_addr(ip);
}
lua_pop(L, 1);
return 0;
}
// Lua: ip = wifi.xx.setip()
static int wifi_setip( lua_State* L, uint8_t mode )
{
struct ip_info pTempIp;
wifi_get_ip_info(mode, &pTempIp);
if (!lua_istable(L, 1))
return luaL_error( L, "wrong arg type" );
uint32_t ip = parse_key(L, "ip");
if(ip!=0)
pTempIp.ip.addr = ip;
ip = parse_key(L, "netmask");
if(ip!=0)
pTempIp.netmask.addr = ip;
ip = parse_key(L, "gateway");
if(mode==SOFTAP_IF || ip!=0)
pTempIp.gw.addr = ip;
if(STATION_IF == mode)
{
wifi_station_dhcpc_stop();
lua_pushboolean(L,wifi_set_ip_info(mode, &pTempIp));
}
else
{
wifi_softap_dhcps_stop();
lua_pushboolean(L,wifi_set_ip_info(mode, &pTempIp));
wifi_softap_dhcps_start();
}
return 1;
}
// Lua: realtype = sleeptype(type)
static int wifi_sleeptype( lua_State* L )
{
unsigned type;
if ( lua_isnumber(L, 1) ){
type = lua_tointeger(L, 1);
if ( type != NONE_SLEEP_T && type != LIGHT_SLEEP_T && type != MODEM_SLEEP_T )
return luaL_error( L, "wrong arg type" );
if(!wifi_set_sleep_type(type)){
lua_pushnil(L);
return 1;
}
}
type = wifi_get_sleep_type();
lua_pushinteger( L, type );
return 1;
}
// Lua: wifi.sta.getmac()
static int wifi_station_getmac( lua_State* L ){
return wifi_getmac(L, STATION_IF);
}
// Lua: wifi.sta.setmac()
static int wifi_station_setmac( lua_State* L ){
return wifi_setmac(L, STATION_IF);
}
// Lua: wifi.sta.getip()
static int wifi_station_getip( lua_State* L ){
return wifi_getip(L, STATION_IF);
}
// Lua: wifi.sta.setip()
static int wifi_station_setip( lua_State* L ){
return wifi_setip(L, STATION_IF);
}
// Lua: wifi.sta.getbroadcast()
static int wifi_station_getbroadcast( lua_State* L ){
return wifi_getbroadcast(L, STATION_IF);
}
/**
* wifi.sta.getconfig()
* Description:
* Get current Station configuration.
* Note: if bssid_set==1 STATION is configured to connect to specified BSSID
* if bssid_set==0 specified BSSID address is irrelevant.
* Syntax:
* ssid, pwd, bssid_set, bssid=wifi.sta.getconfig()
* Parameters:
* none
* Returns:
* SSID, Password, BSSID_set, BSSID
*/
static int wifi_station_getconfig( lua_State* L )
{
struct station_config sta_conf;
char bssid[17];
wifi_station_get_config(&sta_conf);
if(sta_conf.ssid==0)
{
lua_pushnil(L);
return 1;
}
else
{
lua_pushstring( L, sta_conf.ssid );
lua_pushstring( L, sta_conf.password );
lua_pushinteger( L, sta_conf.bssid_set);
c_sprintf(bssid, MACSTR, MAC2STR(sta_conf.bssid));
lua_pushstring( L, bssid);
return 4;
}
}
/**
* wifi.sta.config()
* Description:
* Set current Station configuration.
* Note: If there are multiple APs with the same ssid, you can connect to a specific one by entering it's MAC address into the "bssid" field.
* Syntax:
* wifi.sta.getconfig(ssid, password) --Set STATION configuration, Auto-connect by default, Connects to any BSSID
* wifi.sta.getconfig(ssid, password, Auto_connect) --Set STATION configuration, Auto-connect(0 or 1), Connects to any BSSID
* wifi.sta.getconfig(ssid, password, bssid) --Set STATION configuration, Auto-connect by default, Connects to specific BSSID
* wifi.sta.getconfig(ssid, password, Auto_connect, bssid) --Set STATION configuration, Auto-connect(0 or 1), Connects to specific BSSID
* Parameters:
* ssid: string which is less than 32 bytes.
* Password: string which is less than 64 bytes.
* Auto_connect: 0 (disable Auto-connect) or 1 (to enable Auto-connect).
* bssid: MAC address of Access Point you would like to connect to.
* Returns:
* Nothing.
*
* Example:
--Connect to Access Point automatically when in range
wifi.sta.getconfig("myssid", "password")
--Connect to Access Point, User decides when to connect/disconnect to/from AP
wifi.sta.getconfig("myssid", "mypassword", 0)
wifi.sta.connect()
--do some wifi stuff
wifi.sta.disconnect()
--Connect to specific Access Point automatically when in range
wifi.sta.getconfig("myssid", "mypassword", "12:34:56:78:90:12")
--Connect to specific Access Point, User decides when to connect/disconnect to/from AP
wifi.sta.getconfig("myssid", "mypassword", 0)
wifi.sta.connect()
--do some wifi stuff
wifi.sta.disconnect()
*
*/
static int wifi_station_config( lua_State* L )
{
size_t sl, pl, ml;
struct station_config sta_conf;
int auto_connect=0;
const char *ssid = luaL_checklstring( L, 1, &sl );
if (sl>32 || ssid == NULL)
return luaL_error( L, "ssid:<32" );
const char *password = luaL_checklstring( L, 2, &pl );
if (pl!=0 && (pl<8 || pl>64) || password == NULL)
return luaL_error( L, "pwd:0,8~64" );
if(lua_isnumber(L, 3))
{
auto_connect=luaL_checkinteger( L, 3 );;
if ( auto_connect != 0 && auto_connect != 1)
return luaL_error( L, "wrong arg type" );
}
else if (lua_isstring(L, 3)&& !(lua_isnumber(L, 3)))
{
lua_pushnil(L);
lua_insert(L, 3);
auto_connect=1;
}
else
{
if(lua_isnil(L, 3))
return luaL_error( L, "wrong arg type" );
auto_connect=1;
}
if(lua_isnumber(L, 4))
{
sta_conf.bssid_set = 0;
c_memset(sta_conf.bssid, 0, 6);
}
else
{
if (lua_isstring(L, 4))
{
const char *macaddr = luaL_checklstring( L, 4, &ml );
if (ml!=17)
return luaL_error( L, "MAC:FF:FF:FF:FF:FF:FF" );
c_memset(sta_conf.bssid, 0, 6);
os_str2macaddr(sta_conf.bssid, macaddr);
sta_conf.bssid_set = 1;
}
else
{
sta_conf.bssid_set = 0;
c_memset(sta_conf.bssid, 0, 6);
}
}
c_memset(sta_conf.ssid, 0, 32);
c_memset(sta_conf.password, 0, 64);
c_memcpy(sta_conf.ssid, ssid, sl);
c_memcpy(sta_conf.password, password, pl);
NODE_DBG_(sta_conf.ssid);
NODE_DBG(" %d\n", sl);
NODE_DBG_(sta_conf.password);
NODE_DBG(" %d\n", pl);
NODE_DBG(" %d\n", sta_conf.bssid_set);
NODE_DBG( MACSTR, MAC2STR(sta_conf.bssid));
NODE_DBG("\n");
wifi_station_set_config(&sta_conf);
wifi_station_disconnect();
if(auto_connect==0)
{
wifi_station_set_auto_connect(false);
}
else
{
wifi_station_set_auto_connect(true);
wifi_station_connect();
}
// station_check_connect(0);
return 0;
}
// Lua: wifi.sta.connect()
static int wifi_station_connect4lua( lua_State* L )
{
wifi_station_connect();
return 0;
}
// Lua: wifi.sta.disconnect()
static int wifi_station_disconnect4lua( lua_State* L )
{
wifi_station_disconnect();
return 0;
}
// Lua: wifi.sta.auto(true/false)
static int wifi_station_setauto( lua_State* L )
{
unsigned a;
a = luaL_checkinteger( L, 1 );
if ( a != 0 && a != 1 )
return luaL_error( L, "wrong arg type" );
wifi_station_set_auto_connect(a);
if(a){
// station_check_connect(0);
}
return 0;
}
/**
* wifi.sta.listap()
* Description:
* scan and get ap list as a lua table into callback function.
* Syntax:
* wifi.sta.getap(function(table))
* wifi.sta.getap(format, function(table))
* wifi.sta.getap(cfg, function(table))
* wifi.sta.getap(cfg, format, function(table))
* Parameters:
* cfg: table that contains scan configuration
* Format:Select output table format.
* 0 for the old format (SSID : Authmode, RSSI, BSSID, Channel) (Default)
* 1 for the new format (BSSID : SSID, RSSI, Authmode, Channel)
* function(table): a callback function to receive ap table when scan is done
this function receive a table, the key is the ssid,
value is other info in format: authmode,rssi,bssid,channel
* Returns:
* nil
*
* Example:
--original function left intact to preserve backward compatibility
wifi.sta.getap(function(T) for k,v in pairs(T) do print(k..":"..v) end end)
--if no scan configuration is desired cfg can be set to nil or previous example can be used
wifi.sta.getap(nil, function(T) for k,v in pairs(T) do print(k..":"..v) end end)
--scan configuration
scan_cfg={}
scan_cfg.ssid="myssid" --if set to nil, ssid is not filtered
scan_cfg.bssid="AA:AA:AA:AA:AA:AA" --if set to nil, MAC address is not filtered
scan_cfg.channel=0 --if set to nil, channel will default to 0(scans all channels), if set scan will be faster
scan_cfg.show_hidden=1 --if set to nil, show_hidden will default to 0
wifi.sta.getap(scan_cfg, function(T) for k,v in pairs(T) do print(k..":"..v) end end)
*/
static int wifi_station_listap( lua_State* L )
{
if(wifi_get_opmode() == SOFTAP_MODE)
{
return luaL_error( L, "Can't list ap in SOFTAP mode" );
}
gL = L;
struct scan_config scan_cfg;
getap_output_format=0;
if (lua_type(L, 1)==LUA_TTABLE)
{
char ssid[32];
char bssid[6];
uint8 channel=0;
uint8 show_hidden=0;
size_t len;
lua_getfield(L, 1, "ssid");
if (!lua_isnil(L, -1)){ /* found? */
if( lua_isstring(L, -1) ) // deal with the ssid string
{
const char *ssidstr = luaL_checklstring( L, -1, &len );
if(len>32)
return luaL_error( L, "ssid:<32" );
c_memset(ssid, 0, 32);
c_memcpy(ssid, ssidstr, len);
scan_cfg.ssid=ssid;
NODE_DBG_(scan_cfg.ssid);
NODE_DBG("\n");
}
else
return luaL_error( L, "wrong arg type" );
}
else
scan_cfg.ssid=NULL;
lua_getfield(L, 1, "bssid");
if (!lua_isnil(L, -1)){ /* found? */
if( lua_isstring(L, -1) ) // deal with the ssid string
{
const char *macaddr = luaL_checklstring( L, -1, &len );
if(len!=17)
return luaL_error( L, "bssid: FF:FF:FF:FF:FF:FF" );
c_memset(bssid, 0, 6);
os_str2macaddr(bssid, macaddr);
scan_cfg.bssid=bssid;
NODE_DBG(MACSTR, MAC2STR(scan_cfg.bssid));
NODE_DBG("\n");
}
else
return luaL_error( L, "wrong arg type" );
}
else
scan_cfg.bssid=NULL;
lua_getfield(L, 1, "channel");
if (!lua_isnil(L, -1)){ /* found? */
if( lua_isnumber(L, -1) ) // deal with the ssid string
{
channel = luaL_checknumber( L, -1);
if(!(channel>=0 && channel<=13))
return luaL_error( L, "channel: 0 or 1-13" );
scan_cfg.channel=channel;
NODE_DBG("%d\n", scan_cfg.channel);
}
else
return luaL_error( L, "wrong arg type" );
}
else
scan_cfg.channel=0;
lua_getfield(L, 1, "show_hidden");
if (!lua_isnil(L, -1)){ /* found? */
if( lua_isnumber(L, -1) ) // deal with the ssid string
{
show_hidden = luaL_checknumber( L, -1);
if(show_hidden!=0 && show_hidden!=1)
return luaL_error( L, "show_hidden: 0 or 1" );
scan_cfg.show_hidden=show_hidden;
NODE_DBG("%d\n", scan_cfg.show_hidden);
}
else
return luaL_error( L, "wrong arg type" );
}
else
scan_cfg.show_hidden=0;
if (lua_type(L, 2) == LUA_TFUNCTION || lua_type(L, 2) == LUA_TLIGHTFUNCTION)
{
lua_pushnil(L);
lua_insert(L, 2);
}
lua_pop(L, -4);
}
else if (lua_type(L, 1) == LUA_TNUMBER)
{
lua_pushnil(L);
lua_insert(L, 1);
}
else if (lua_type(L, 1) == LUA_TFUNCTION || lua_type(L, 1) == LUA_TLIGHTFUNCTION)
{
lua_pushnil(L);
lua_insert(L, 1);
lua_pushnil(L);
lua_insert(L, 1);
}
else if(lua_isnil(L, 1))
{
if (lua_type(L, 2) == LUA_TFUNCTION || lua_type(L, 2) == LUA_TLIGHTFUNCTION)
{
lua_pushnil(L);
lua_insert(L, 2);
}
}
else
{
return luaL_error( L, "wrong arg type" );
}
if (lua_type(L, 2) == LUA_TNUMBER) //this section changes the output format
{
getap_output_format=luaL_checkinteger( L, 2 );
if ( getap_output_format != 0 && getap_output_format != 1)
return luaL_error( L, "wrong arg type" );
}
NODE_DBG("Use alternate output format: %d\n", getap_output_format);
if (lua_type(L, 3) == LUA_TFUNCTION || lua_type(L, 3) == LUA_TLIGHTFUNCTION)
{
lua_pushvalue(L, 3); // copy argument (func) to the top of stack
if(wifi_scan_succeed != LUA_NOREF)
luaL_unref(L, LUA_REGISTRYINDEX, wifi_scan_succeed);
wifi_scan_succeed = luaL_ref(L, LUA_REGISTRYINDEX);
if (lua_type(L, 1)==LUA_TTABLE)
{
wifi_station_scan(&scan_cfg,wifi_scan_done);
}
else
{
wifi_station_scan(NULL,wifi_scan_done);
}
}
else
{
if(wifi_scan_succeed != LUA_NOREF)
luaL_unref(L, LUA_REGISTRYINDEX, wifi_scan_succeed);
wifi_scan_succeed = LUA_NOREF;
}
}
// Lua: wifi.sta.status()
static int wifi_station_status( lua_State* L )
{
uint8_t status = wifi_station_get_connect_status();
lua_pushinteger( L, status );
return 1;
}
// Lua: wifi.ap.getmac()
static int wifi_ap_getmac( lua_State* L ){
return wifi_getmac(L, SOFTAP_IF);
}
// Lua: wifi.ap.setmac()
static int wifi_ap_setmac( lua_State* L ){
return wifi_setmac(L, SOFTAP_IF);
}
// Lua: wifi.ap.getip()
static int wifi_ap_getip( lua_State* L ){
return wifi_getip(L, SOFTAP_IF);
}
// Lua: wifi.ap.setip()
static int wifi_ap_setip( lua_State* L ){
return wifi_setip(L, SOFTAP_IF);
}
// Lua: wifi.ap.getbroadcast()
static int wifi_ap_getbroadcast( lua_State* L ){
return wifi_getbroadcast(L, SOFTAP_IF);
}
// Lua: wifi.ap.getconfig()
static int wifi_ap_getconfig( lua_State* L )
{
struct softap_config config;
wifi_softap_get_config(&config);
lua_pushstring( L, config.ssid );
if(config.authmode = AUTH_OPEN)
lua_pushnil(L);
else
lua_pushstring( L, config.password );
return 2;
}
// Lua: wifi.ap.config(table)
static int wifi_ap_config( lua_State* L )
{
if (!lua_istable(L, 1))
return luaL_error( L, "wrong arg type" );
struct softap_config config;
wifi_softap_get_config(&config);
size_t len;
lua_getfield(L, 1, "ssid");
if (!lua_isnil(L, -1)){ /* found? */
if( lua_isstring(L, -1) ) // deal with the ssid string
{
const char *ssid = luaL_checklstring( L, -1, &len );
if(len<1 || len>32 || ssid == NULL)
return luaL_error( L, "ssid:1~32" );
c_memset(config.ssid, 0, 32);
c_memcpy(config.ssid, ssid, len);
NODE_DBG_(config.ssid);
NODE_DBG("\n");
config.ssid_len = len;
config.ssid_hidden = 0;
}
else
return luaL_error( L, "wrong arg type" );
}
else
return luaL_error( L, "ssid required" );
lua_getfield(L, 1, "pwd");
if (!lua_isnil(L, -1)){ /* found? */
if( lua_isstring(L, -1) ) // deal with the password string
{
const char *pwd = luaL_checklstring( L, -1, &len );
if(len<8 || len>64 || pwd == NULL)
return luaL_error( L, "pwd:8~64" );
c_memset(config.password, 0, 64);
c_memcpy(config.password, pwd, len);
NODE_DBG_(config.password);
NODE_DBG("\n");
config.authmode = AUTH_WPA_WPA2_PSK;
}
else
return luaL_error( L, "wrong arg type" );
}
else{
config.authmode = AUTH_OPEN;
}
lua_getfield(L, 1, "auth");
if (!lua_isnil(L, -1))
{
config.authmode = (uint8_t)luaL_checkinteger(L, -1);
NODE_DBG("%d\n", config.authmode);
}
else
{
// keep whatever value resulted from "pwd" logic above
}
lua_getfield(L, 1, "channel");
if (!lua_isnil(L, -1))
{
unsigned channel = luaL_checkinteger(L, -1);
if (channel < 1 || channel > 13)
return luaL_error( L, "channel:1~13" );
config.channel = (uint8_t)channel;
NODE_DBG("%d\n", config.channel);
}
else
{
config.channel = 6;
}
lua_getfield(L, 1, "hidden");
if (!lua_isnil(L, -1))
{
config.ssid_hidden = (uint8_t)luaL_checkinteger(L, -1);
NODE_DBG("%d\n", config.ssid_hidden);
NODE_DBG("\n");
}
else
{
config.ssid_hidden = 0;
}
lua_getfield(L, 1, "max");
if (!lua_isnil(L, -1))
{
unsigned max = luaL_checkinteger(L, -1);
if (max < 1 || max > 4)
return luaL_error( L, "max:1~4" );
config.max_connection = (uint8_t)max;
NODE_DBG("%d\n", config.max_connection);
}
else