-
Notifications
You must be signed in to change notification settings - Fork 6
/
iot_bsp_wifi_rpi.c
1829 lines (1337 loc) · 50.6 KB
/
iot_bsp_wifi_rpi.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
/*******************************************************************************************************************************************
Enabling Raspberry Pi to run SmartThings direct-connected device applications
This file replaces iot_bsp_wifi_posix.c in the SmartThings Core SDK
Version 0.20210405
Copyright 2021 Todd A. Austin
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.
Description:
This module enables Raspberry Pi-based IOT devices to connect with Samsung SmartThings direct connected device architecture.
This file must be compiled as part of the SmartThings SDK for Direct Connect Devices.
This module replaces the posix wifi module in the BSP porting directory of the SDK:
~/st-device-sdk/src/port/bsp/posic/iot_bsp_wifi_posix.c
Most remaining posix modules in the SDK BSP port files are used for Raspberry Pi builds, namely:
iot_os_util_posix.c, iot_bsp_debug_posix.c, iot_bsp_nv_data_posix.c, iot_bsp_random_posix.c
iot_bsp_system_posix.c should be modifed to ignore the clock set function since permissions may not allow it on a Pi.
With thanks to Kwang-Hui of Samsung who patiently answered my many questions during development.
********************************************************************************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include "iot_bsp_wifi.h"
#include "iot_error.h"
#include "iot_debug.h"
#include "iot_os_util.h"
#include "iot_util.h"
/*****************************
UPDATE!!!!
*****************************/
#define MODVERSION "20210405"
#define RPICONFFILE "RPISetup.conf"
#define DEFAULTDIR "./"
#define SOFTAPCONFFILE "/etc/hostapd/hostapd.conf"
#define RPIPKGSUBDIR "/rpi-st-device/"
#define SOFTAPSTARTFILE "softapstart"
#define SOFTAPSTOPFILE "softapstop"
#define PRIORCONF "./__prior_hostapd.conf"
#define DHCPCDCONF "/etc/dhcpcd.conf"
#define DHCPCDSAVE "/etc/dhcpcd_saved.conf"
#define DHCPCD_AP "/etc/dhcpcd_ap.conf"
#define configtag_ETH "USE_ETHERNET"
#define configtag_AP "AP_SHUTDOWN"
#define configtag_devSTA "STATION_DEV"
#define configtag_devAP "AP_DEV"
#define configtag_devETH "ETH_DEV"
#define MAXDEVNAMESIZE 10
#define SSIDWAITRETRIES 6
#define SSIDWAITTIME 999999
#define SCANMODEWAITTIME 800000
#define SOFTAPWAITTIME 999999
#define SEQSYSCMDWAIT 500000
#define SCANRETRIES 4
extern int errno;
/** DECLARE FUNCTIONS CONTAINED IN THIS FILE **/
int _perform_scan();
void _parsemac(char *textptr, uint8_t *hexbuf);
unsigned int _htoi (const char *ptr);
int _getnumeric(int maxdigits, char *text);
bool _getrpiconf(char *currdir);
int _isupIface(char *devname);
int _isconfWifi(char *devname, char *ssid);
int _parseconfparm(char *parmstr, char *text);
int _enableWifi(char *dev);
int _softblock(char *dev, char set);
int _waitWifiConn(char *dev, char *ssid);
int _SoftAPControl(char *cmd);
int _initDevNames();
int _setupHostapd(char*ssid, char *password, char *iface);
int _updateHfile(char *fname, char *ssid,char *password, char*iface);
int _switchSSID(char *dev, char *ssid);
bool _checkfortestdevfile();
bool _checksoftapcontrol(char *dir);
bool _switchmode(char *mode);
bool _checkstartSoftAP(char *service);
bool _restorehfile(char *backupfile);
bool _restoreAP();
int _pipecommand(char *command);
int _checkexistfile(char *filename);
/** DEFINE GLOBAL STATIC VARIABLES **/
struct scandata {
int apcount;
iot_wifi_scan_result_t apdata[IOT_WIFI_MAX_SCAN_RESULT];
};
static struct scandata scanstore;
static bool Ethernet = true;
static bool ManageAP = true;
static bool ConcurrentWifi = false;
static bool DualWifidev = false;
static bool APWifionly = false;
static bool APWifionlyRestore = false;
static bool STWifionly = true;
static bool AP_ON = false;
static char PHYSWIFIDEV[5] = "phy0";
static char wifi_sta_dev[MAXDEVNAMESIZE+1] = "";
static char wifi_ap_dev[MAXDEVNAMESIZE+1] = "";
static char eth_dev[MAXDEVNAMESIZE+1] = "";
static char SOFTAPSTART[60] = "";
static char SOFTAPSTOP[60] = "";
static uint8_t wifimacaddr[IOT_WIFI_MAX_BSSID_LEN];
static uint8_t ethmacaddr[IOT_WIFI_MAX_BSSID_LEN];
static int WIFI_INITIALIZED = false;
/**********************************************************************************************************************
Required BSP fuction: iot_bsp_wifi_init()
Purpose: Validate & Initialize RPI wireless devices that will be needed; includes retrieving wifi devices,
reading optional RPI configuration file, and initializing global static flags
Input: none
Output: return IOT_ERROR_ value (IOT_ERROR_NONE if no errors)
***********************************************************************************************************************/
iot_error_t iot_bsp_wifi_init()
{
int errnum;
IOT_INFO("[rpi] iot_bsp_wifi_init");
IOT_INFO("[rpi] wifi module version %s",MODVERSION);
if (!WIFI_INITIALIZED) {
if (!_getrpiconf(DEFAULTDIR)) { // read config file
if (!_initDevNames()) { // initialize device names & info
IOT_ERROR("[rpi] Failure initializing interface device names");
return IOT_ERROR_CONN_OPERATE_FAIL;
}
//Check Ethernet exists
if (Ethernet)
IOT_INFO("[rpi] Ethernet connection available: %s",eth_dev);
else
IOT_INFO("[rpi] No Ethernet");
//Station device only
if ((strcmp(wifi_sta_dev,"") != 0) && (strcmp(wifi_ap_dev,"") == 0)) {
STWifionly=true;
ManageAP=true;
ConcurrentWifi=false;
DualWifidev=false;
APWifionly=false;
IOT_INFO("[rpi] Wifi station device %s found",wifi_sta_dev);
}
// AP device only
if ((strcmp(wifi_sta_dev,"") == 0) && (strcmp(wifi_ap_dev,"") != 0)) {
IOT_INFO("[rpi] Wifi AP device %s found",wifi_ap_dev);
if (Ethernet) {
APWifionly=true;
ManageAP=false; // leave AP always on
ConcurrentWifi=false;
DualWifidev=false;
STWifionly=false;
} else {
IOT_ERROR("[rpi] Invalid configuration: must have Ethernet with full time AP wifi");
return IOT_ERROR_NET_INVALID_INTERFACE;
}
}
// Found both Station and AP devices
if ((strcmp(wifi_sta_dev,"") != 0) && (strcmp(wifi_ap_dev,"") != 0)) {
DualWifidev=true;
ManageAP=true; // default is to manage them up and down
ConcurrentWifi=false; // no Concurrent support for now
APWifionly=false;
STWifionly=false;
IOT_INFO("[rpi] %s station and %s AP devices found",wifi_sta_dev,wifi_ap_dev);
}
}
// Make sure SoftAP control scripts are present
if (!_checksoftapcontrol("./")) {
IOT_ERROR("[rpi] Missing SoftAP control scripts");
return IOT_ERROR_CONN_OPERATE_FAIL;
}
// Make sure dhcpcd AP config file exists
if (STWifionly) {
errnum = _checkexistfile(DHCPCD_AP);
if (errnum != 0) {
if (errnum == ENOENT)
IOT_ERROR("[rpi] Missing dhcpcd AP config file");
else
IOT_ERROR("[rpi] Cannot verify dhcpcd AP config file");
return IOT_ERROR_CONN_OPERATE_FAIL;
}
}
}
WIFI_INITIALIZED = true;
IOT_INFO("[rpi] Wifi Initialization Done");
IOT_DUMP(IOT_DEBUG_LEVEL_DEBUG, IOT_DUMP_BSP_WIFI_INIT_SUCCESS, 0, 0);
return IOT_ERROR_NONE;
}
/*************************************************************************************
Subroutine: _getrpiconf (usage TBD)
Purpose: Read optional RPI config file and use parameters to initialize global values
Input: String pointer to configuration file name
Ouput: Global values:
**************************************************************************************/
bool _getrpiconf(char *currdir) {
FILE *pf;
char pathname[100];
char *readline = NULL;
size_t len = 0;
int errnum;
char *textptr;
char parmstr[50];
// Initialize global static defaults
strcpy(pathname,currdir);
strcat(pathname,RPICONFFILE);
if ((pf = fopen(pathname,"r")) != NULL) {
while (getline(&readline,&len,pf)!= EOF) {
if ((readline[0] != '#') && (readline[0] != '\n')) { // skip comments and blank lines
if((textptr = strstr(readline,configtag_ETH))) {
if(_parseconfparm(parmstr, textptr)) {
if ((parmstr[0] == 'Y') || (parmstr[0] == 'y'))
Ethernet = true;
else if ((parmstr[0] == 'N') || (parmstr[0] == 'n'))
Ethernet = false;
}
} else {
if ((textptr = strstr(readline,configtag_AP))) {
if (_parseconfparm(parmstr, textptr)) {
if ((*parmstr == 'Y') || (*parmstr == 'y'))
ManageAP = true;
else if ((*parmstr == 'N') || (*parmstr == 'n'))
ManageAP = false;
}
} else {
if ((textptr = strstr(readline,configtag_devSTA))) {
if(_parseconfparm(parmstr,textptr))
strcpy(wifi_sta_dev,parmstr);
}
else {
if ((textptr = strstr(readline,configtag_devAP))) {
if(_parseconfparm(parmstr,textptr))
strcpy(wifi_ap_dev,parmstr);
}
else {
if ((textptr = strstr(readline,configtag_devETH))) {
if(_parseconfparm(parmstr,textptr))
strcpy(eth_dev,parmstr);
}
}
}
}
}
}
}
free(readline);
fclose(pf);
} else {
errnum=errno;
if (errnum != ENOENT)
IOT_INFO("[rpi] Could read RPI configuration file");
return false;
}
return true;
}
// Secret test file for forcing wifi device definitions (not used in production)
bool _checkfortestdevfile() {
#define TESTDEVFILE "./.dev"
FILE *pf;
char pathname[100];
char *readline = NULL;
size_t len = 0;
char *textptr;
char parmstr[50];
int errnum;
// Initialize global static defaults
strcpy(pathname,TESTDEVFILE);
if ((pf = fopen(pathname,"r")) != NULL) {
while (getline(&readline,&len,pf)!= EOF) {
if ((readline[0] != '#') && (readline[0] != '\n')) { // skip comments and blank lines
if((textptr = strstr(readline,configtag_devSTA))) {
if(_parseconfparm(parmstr, textptr))
strcpy(wifi_sta_dev,parmstr);
}
else {
if((textptr = strstr(readline,configtag_devAP))) {
if(_parseconfparm(parmstr, textptr))
strcpy(wifi_ap_dev,parmstr);
}
else {
if((textptr = strstr(readline,configtag_devETH))) {
if(_parseconfparm(parmstr, textptr))
strcpy(eth_dev,parmstr);
}
}
}
} //end if blank line
}
free(readline);
fclose(pf);
if (strcmp(eth_dev, "") == 0)
Ethernet = false;
else
Ethernet = true;
return true;
} else {
errnum = errno;
if (errnum != ENOENT) // anything but file not found is unexpected
IOT_INFO("[rpi] Warning: file error %d on dev test file open",errnum);
return false;
}
}
/****************************************************************
Initialize global variables for station, AP, and Ethernet devices
****************************************************************/
int _initDevNames() {
FILE *pf;
const int maxdatasize = 100;
char data[maxdatasize];
char devname[MAXDEVNAMESIZE+1];
char devtype[10];
char command[40];
uint8_t tmpmac[IOT_WIFI_MAX_BSSID_LEN];
bool found = false;
bool done = false;
int i;
char *textptr;
if(_checkfortestdevfile()) // If test device file exists, we'll pick up device names from there
return true;
// FIRST GET ETHERNET NAME AND CONFIRM CONNECTED
strcpy(eth_dev,"");
pf = popen("ls /sys/class/net","r");
if (pf) {
while (! done) {
if(fgets(data,maxdatasize,pf)) {
i = 0;
while ((*(data+i) != ' ') && (*(data+i) != '\n')) { // Ethernet device will be first text in ls command output
devname[i] = *(data+i);
++i;
}
devname[i] = '\0';
if (strcmp(devname,"eth0") == 0) {
done = true;
found = true;
}
} else
done = true;
} // end while loop
pclose(pf);
if (! found) {
IOT_WARN("[rpi] eth0 not found",devname);
Ethernet = false;
} else {
strcpy(eth_dev,devname); // Found device name; save it in global static variable
sprintf(command,"cat /sys/class/net/%s/carrier",devname);
pf = popen(command,"r");
if (pf) {
if(fgets(data,maxdatasize,pf)) {
pclose(pf);
if (*data == '1') {
Ethernet = true;
} else {
IOT_INFO("Ethernet device %s not connected",devname);
Ethernet = false;
}
} else {
IOT_ERROR("[rpi] Could not read Ethernet status %s",devname);
pclose(pf);
Ethernet = false;
}
} else {
IOT_ERROR("[rpi] Carrier file not found for %s",devname);
Ethernet = false;
}
// Now get the Ethernet mac address
if (Ethernet) {
sprintf(command,"cat /sys/class/net/%s/address",devname);
pf = popen(command,"r");
if (pf) {
if(fgets(data,maxdatasize,pf)) {
pclose(pf);
_parsemac(data,ethmacaddr);
} else {
IOT_ERROR("[rpi] Could not read Ethernet mac address %s",devname);
pclose(pf);
}
} else {
IOT_ERROR("[rpi] Could not check Ethernet mac address %s",devname);
}
}
}
} else {
IOT_ERROR("[rpi] Could not read network device directory");
Ethernet = false;
}
// NOW GET WIRELESS DEVICE NAMES
strcpy(wifi_ap_dev,"");
strcpy(wifi_sta_dev, "");
pf = popen("iw dev","r");
if (pf) {
while(fgets(data,maxdatasize,pf)) { // search for Interface line
if ((textptr = strstr(data,"Interface"))) {
i = 0;
textptr += 10;
while ((*(textptr+i) != ' ') && (*(textptr+i) != '\n')) { // get the device name
*(devname+i) = *(textptr+i);
++i;
}
devname[i] ='\0';
found = false;
while(fgets(data,maxdatasize,pf) && !found) { // search for type line
if ((textptr = strstr(data,"type"))) {
i = 0;
textptr += 5;
while ((*(textptr+i) != ' ') && (*(textptr+i) != '\n')) { // get the type descriptor
*(devtype+i) = *(textptr+i);
++i;
}
devtype[i] = '\0';
if (strcmp(devtype,"managed") == 0) {
strcpy(wifi_sta_dev, devname);
memcpy(wifimacaddr,tmpmac,IOT_WIFI_MAX_BSSID_LEN);
}
else {
if (strcmp(devtype,"AP") == 0) {
strcpy(wifi_ap_dev,devname);
if (wifimacaddr[0] == 0)
memcpy(wifimacaddr,tmpmac,IOT_WIFI_MAX_BSSID_LEN);
}
}
found = true;
} else {
if ((textptr = strstr(data,"addr")))
_parsemac(textptr+5,tmpmac); // grab the mac address
}
} // end search-for-type loop
}
} // end Interface search loop
pclose(pf);
} else {
IOT_ERROR("[rpi] Could not perform iw dev command");
return 0;
}
return 1;
}
// Make sure start and stop script files are found
bool _checksoftapcontrol(char *dir) {
char filename[70];
char filedir[50];
int okflag=0;
int errnum;
strcpy(filedir,getenv("HOME"));
strcat(filedir,RPIPKGSUBDIR);
// Check for Start script file in both given dir and current dir
strcpy(filename,dir);
strcat(filename,SOFTAPSTARTFILE);
errnum = _checkexistfile(filename);
if (errnum == 0) {
strcpy(SOFTAPSTART,filename);
okflag++;
} else {
if (errnum == ENOENT) {
strcpy(filename,filedir);
strcat(filename,SOFTAPSTARTFILE);
errnum = _checkexistfile(filename);
if (errnum == 0) {
strcpy(SOFTAPSTART,filename);
okflag++;
} else {
if (errnum == ENOENT)
IOT_ERROR("[rpi] SoftAP start script not found");
else
IOT_ERROR("[rpi] Can't validate SoftAP start script");
}
} else
IOT_ERROR("[rpi] Can't validate SoftAP start script");
}
// Check for Stop script file in both given dir and current dir
strcpy(filename,dir);
strcat(filename,SOFTAPSTOPFILE);
errnum = _checkexistfile(filename);
if (errnum == 0) {
strcpy(SOFTAPSTOP,filename);
okflag++;
} else {
if (errnum == ENOENT) {
strcpy(filename,filedir);
strcat(filename,SOFTAPSTOPFILE);
errnum = _checkexistfile(filename);
if (errnum == 0) {
strcpy(SOFTAPSTOP,filename);
okflag++;
} else {
if (errnum == ENOENT)
IOT_ERROR("[rpi] SoftAP stop script not found");
else
IOT_ERROR("[rpi] Can't validate SoftAP stop script");
}
} else
IOT_ERROR("[rpi] Can't validate SoftAP stop script");
}
if (okflag == 2)
return true;
else
return false;
}
// Parse parameter / value pairs from config file
int _parseconfparm(char *parmstr, char *text) {
char *subtxtptr;
int i;
subtxtptr = strstr(text,"=");
if (subtxtptr) {
subtxtptr++;
while(*subtxtptr == ' ')
subtxtptr++;
i=0;
while((*(subtxtptr+i) != '\0') && (*(subtxtptr+i) != '\n')) {
*(parmstr+i) = *(subtxtptr+i);
i++;
}
*(parmstr+i) = 0;
if (i>0)
return(1);
else
return(0);
} else
IOT_ERROR("[rpi] Config file format error");
return(0);
}
// Validate network interface device is up
int _isupIface(char *devname) {
FILE *pf;
const int maxdatasize = 1200;
char data[maxdatasize];
char command[300];
char devsearch[10];
char *lineptr;
char *delim1;
char *delim2;
char *sublineptr;
sprintf(command,"ip link show ");
strcat(command,devname);
pf = popen(command,"r");
if (pf) {
if(fgets(data,maxdatasize,pf)) {
pclose(pf);
strcpy(devsearch,devname);
strcat(devsearch,":");
lineptr = strstr(data,devsearch);
if (lineptr) {
delim1 = strstr(lineptr,"<");
if (delim1) {
delim2 = strstr(lineptr,">");
if (delim2) {
sublineptr = strstr(lineptr,",UP");
if ((sublineptr) && ((sublineptr > delim1) && (sublineptr < delim2)))
return(1);
}
}
}
} else
pclose(pf);
}
IOT_ERROR("[rpi] Can't execute ip link show command");
return(0);
}
// check if wifi device is configured
int _isconfWifi(char *devname, char *ssid) {
FILE *pf;
const int maxdatasize = 1200;
char data[maxdatasize];
char command[300];
char searchstr[30];
char *lineptr;
int i;
strcpy(command,"iw ");
strcat(command,devname);
strcat(command," info");
strcpy(searchstr,"Interface ");
strcat(searchstr,devname);
pf = popen(command,"r");
if(fgets(data,maxdatasize,pf)) {
lineptr = strstr(data,searchstr);
if (lineptr) {
while(fgets(data,maxdatasize,pf)) {
lineptr = strstr(data,"ssid");
if (lineptr) {
lineptr = lineptr + 5;
i=0;
while ((*(lineptr+i) != ' ') && (*(lineptr+i) != '\n')) {
*(ssid+i) = *(lineptr+i);
i++;
}
*(ssid+i) = '\0';
break;
}
}
} else {
pclose(pf);
return(0);
}
pclose(pf);
return(1);
}
return(0);
}
int _checkexistfile(char *filename) {
FILE *pf;
int errnum;
pf = fopen(filename,"r");
errnum=errno;
if (! pf)
return errnum;
else {
fclose(pf);
return 0;
}
}
/*******************************************************************************************
Required BSP fuction: iot_bsp_wifi_set_mode()
Purpose: Switch wireless operation to request modes (off/scan/station/AP)
Input: iot_wifi_conf
Output: return IOT_ERROR_ value (IOT_ERROR_NONE if no errors)
*******************************************************************************************/
iot_error_t iot_bsp_wifi_set_mode(iot_wifi_conf *conf)
{
//iot_wifi_scan_result_t scanresult[IOT_WIFI_MAX_SCAN_RESULT];
char connected_ssid[IOT_WIFI_MAX_SSID_LEN+1];
int scancount;
int sretry;
char SoftAPdev[16];
// IOT_INFO("[rpi] iot_bsp_wifi_set_mode = %d", conf->mode);
IOT_DUMP(IOT_DEBUG_LEVEL_DEBUG, IOT_DUMP_BSP_WIFI_SETMODE, conf->mode, 0);
switch(conf->mode) {
case IOT_WIFI_MODE_OFF:
IOT_INFO("[rpi] Requested mode OFF");
if (AP_ON && ManageAP) {
_SoftAPControl("stop"); // make sure hostapd/dnsmasq are stopped
if (STWifionly) { // if wlan0 only then switch mode back to station
if (! _switchmode("STA")) {
IOT_ERROR("[rpi] Failed to switch wireless modes");
return IOT_ERROR_CONN_OPERATE_FAIL;
}
}
}
_restoreAP(); // restore prior AP config if AP-only wifi
break;
case IOT_WIFI_MODE_SCAN:
IOT_INFO("[rpi] Requested mode SCAN");
if (!AP_ON || DualWifidev || APWifionly) {
scancount = 0;
sretry = SCANRETRIES;
while ((scancount == 0) && (sretry > 0)) {
scancount = _perform_scan(); // do scan and check resulting AP count
if (scancount == 0) { // if no results...
usleep(SCANMODEWAITTIME);
usleep(SCANMODEWAITTIME); // pause and try again
--sretry;
}
}
if (scancount > 0)
IOT_INFO("[rpi] WiFi scan completed. %d APs found",scancount);
else {
IOT_ERROR("[rpi] WiFi scan unable to find available APs");
return IOT_ERROR_CONN_OPERATE_FAIL;
}
} else
IOT_INFO("[rpi] Scan not performed while in AP mode");
break;
case IOT_WIFI_MODE_STATION: // For PI this could be either Wifi client or use ETH0
IOT_INFO("[rpi] Requested mode STATION");
if (!WIFI_INITIALIZED)
return IOT_ERROR_CONN_CONNECT_FAIL;
if (AP_ON && ManageAP) { // Turn off SoftAP
if (!_SoftAPControl("stop"))
IOT_INFO("[rpi] Problem stopping SoftAP");
if (STWifionly) {
if (! _switchmode("STA")) {
IOT_ERROR("[rpi] Failed to switch wifi mode");
return IOT_ERROR_CONN_OPERATE_FAIL;
}
}
}
_restoreAP(); // restore prior AP config if AP only wifi
if (STWifionly) {
usleep(SOFTAPWAITTIME); // Switching wlan0; give it some time to transition
usleep(SOFTAPWAITTIME);
}
if(DualWifidev || STWifionly) {
//NOW CHANGE CONNECTION PER conf->ssid
if (!_switchSSID(wifi_sta_dev,conf->ssid)) { // Switch to ssid; if failed...
if (Ethernet)
IOT_INFO("[rpi] Could not connect to ssid %s. Will use Ethernet",conf->ssid);
else {
IOT_ERROR("[rpi] Failed to connect to ssid %s",conf->ssid);
return IOT_ERROR_CONNECT_FAIL;
}
} else { // Switch SSID went OK
usleep(SSIDWAITTIME);
if (!_waitWifiConn(wifi_sta_dev,connected_ssid)) { // confirm connected SSID
if (Ethernet)
IOT_INFO("[rpi] Didn't connect to ssid %s. Will use Ethernet",conf->ssid);
else {
IOT_ERROR("[rpi] Failed to connect to ssid %s",conf->ssid);
return IOT_ERROR_NET_CONNECT;
}
} else
IOT_INFO("[rpi] Connected to AP SSID: %s", conf->ssid);
}
}
break;
case IOT_WIFI_MODE_SOFTAP:
IOT_INFO("[rpi] Requested mode SoftAP");
if (!WIFI_INITIALIZED)
return IOT_ERROR_CONN_CONNECT_FAIL;
if (STWifionly)
strcpy(SoftAPdev,wifi_sta_dev);
else
strcpy(SoftAPdev,wifi_ap_dev);
if (!_setupHostapd(conf->ssid,conf->pass,SoftAPdev)) { // Setup hostapd.conf file with ssid & password
IOT_ERROR("[rpi] Couldn't update hostapd.conf file");
return IOT_ERROR_CONN_OPERATE_FAIL;
}
if (!DualWifidev)
usleep(SOFTAPWAITTIME); // pause to let wireless come up
if (STWifionly) {
if (!_switchmode("AP")) {
IOT_ERROR("[rpi] Failed to switch to AP wifi mode on device %s",SoftAPdev);
return IOT_ERROR_CONN_OPERATE_FAIL;
}
}
usleep(SOFTAPWAITTIME);
usleep(SOFTAPWAITTIME);
// If Full-time AP, then shut down current SoftAP config (it was saved prior)
if (APWifionly) {
if (!_SoftAPControl("stop")) {
IOT_ERROR("[rpi] Problem stopping SoftAP");
return IOT_ERROR_CONN_OPERATE_FAIL;
}
usleep(SOFTAPWAITTIME);
}