-
Notifications
You must be signed in to change notification settings - Fork 5
/
pa_wifi_client_ti.c
1364 lines (1246 loc) · 45.9 KB
/
pa_wifi_client_ti.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 Client Platform Adapter
*
* Copyright (C) Sierra Wireless Inc.
*
*/
// -------------------------------------------------------------------------------------------------
#include <sys/types.h>
#include <sys/wait.h>
#include "legato.h"
#include "interfaces.h"
#include "pa_wifi.h"
#include "stdio.h"
//--------------------------------------------------------------------------------------------------
/**
* WiFi platform adaptor shell script
*/
//--------------------------------------------------------------------------------------------------
#define WIFI_SCRIPT_PATH "/legato/systems/current/apps/wifiService/read-only/pa_wifi.sh "
// Set of commands to drive the WiFi features.
#define COMMAND_WIFI_HW_START "wlan0 WIFI_START"
#define COMMAND_WIFI_HW_STOP "wlan0 WIFI_STOP" /* not sure that this works */
#define COMMAND_WIFI_WLAN_UP "wlan0 WIFI_WLAN_UP"
#define COMMAND_WIFI_SET_EVENT "wlan0 WIFI_SET_EVENT"
#define COMMAND_WIFICLIENT_START_SCAN "wlan0 WIFICLIENT_START_SCAN"
#define COMMAND_WIFICLIENT_DISCONNECT "wlan0 WIFICLIENT_DISCONNECT"
#define COMMAND_WIFICLIENT_CONNECT_SECURITY_NONE \
"wlan0 WIFICLIENT_CONNECT_SECURITY_NONE \"%.*s\" \"%d\""
#define COMMAND_WIFICLIENT_CONNECT_SECURITY_WEP \
"wlan0 WIFICLIENT_CONNECT_SECURITY_WEP \"%.*s\" \"%d\" \"%s\""
#define COMMAND_WIFICLIENT_CONNECT_SECURITY_WPA_PASS_PERSONAL \
"wlan0 WIFICLIENT_CONNECT_SECURITY_WPA_PSK_PERSONAL \"%.*s\""
#define COMMAND_WIFICLIENT_CONNECT_SECURITY_WPA2_PASS_PERSONAL \
"wlan0 WIFICLIENT_CONNECT_SECURITY_WPA2_PSK_PERSONAL \"%.*s\""
#define COMMAND_WIFICLIENT_CONNECT_SECURITY_WPA_PSK_PERSONAL \
"wlan0 WIFICLIENT_CONNECT_SECURITY_WPA_PSK_PERSONAL \"%.*s\""
#define COMMAND_WIFICLIENT_CONNECT_SECURITY_WPA2_PSK_PERSONAL \
"wlan0 WIFICLIENT_CONNECT_SECURITY_WPA2_PSK_PERSONAL \"%.*s\""
#define COMMAND_WIFICLIENT_CONNECT_SECURITY_WPA_EAP_PEAP0_ENTERPRISE \
"wlan0 WIFICLIENT_CONNECT_SECURITY_WPA_EAP_PEAP0_ENTERPRISE \"%.*s\" \"%d\" \"%s\" \"%s\""
#define COMMAND_WIFICLIENT_CONNECT_SECURITY_WPA2_EAP_PEAP0_ENTERPRISE \
"wlan0 WIFICLIENT_CONNECT_SECURITY_WPA2_EAP_PEAP0_ENTERPRISE \"%.*s\" \"%d\" \"%s\" \"%s\""
#define COMMAND_WIFICLIENT_CONNECT_WPA_PASSPHRASE \
"wlan0 WIFICLIENT_CONNECT_WPA_PASSPHRASE \"%.*s\" %s"
//--------------------------------------------------------------------------------------------------
/**
* The current security protocol.
*/
//--------------------------------------------------------------------------------------------------
#define WPA_SUPPLICANT_FILE "/tmp/wpa_supplicant.conf"
#define WPA_SUPPLICANT_DATA \
"ctrl_interface=/var/run/wpa_supplicant\n \
ctrl_interface_group=0\n \
update_config=1\n \
network={\n \
ssid=\"%.*s\"\n \
scan_ssid=%d\n \
psk=%s\n \
}\n"
#define WPA_SUPPLICANT_DATA_NO_SECURITY \
"ctrl_interface=/var/run/wpa_supplicant\n \
ctrl_interface_group=0\n \
update_config=1\n \
network={\n \
ssid=\"%.*s\"\n \
scan_ssid=%d\n \
key_mgmt=NONE\n \
}\n"
//--------------------------------------------------------------------------------------------------
/**
* The current security protocol.
*/
//--------------------------------------------------------------------------------------------------
static le_wifiClient_SecurityProtocol_t SavedSecurityProtocol;
// WEP
//--------------------------------------------------------------------------------------------------
/**
* WEP key used for authentication. Used only with WEP protocol.
*/
//--------------------------------------------------------------------------------------------------
static char SavedWepKey[LE_WIFIDEFS_MAX_WEPKEY_BYTES];
// WPA-Personal
//--------------------------------------------------------------------------------------------------
/**
* Passphrase used for authentication. Used only with WPA/WPA2 protocol.
*/
//--------------------------------------------------------------------------------------------------
static char SavedPassphrase[LE_WIFIDEFS_MAX_PASSPHRASE_BYTES];
//--------------------------------------------------------------------------------------------------
/**
* Pre-Shared-Key used for authentication. Used only with WPA/WPA2 protocol.
*/
//--------------------------------------------------------------------------------------------------
static char SavedPreSharedKey[LE_WIFIDEFS_MAX_PSK_BYTES];
// WPA-Enterprise
//--------------------------------------------------------------------------------------------------
/**
* Username used for authentication. Used only with WPA/WPA2 protocol with RADIUS server.
*/
//--------------------------------------------------------------------------------------------------
static char SavedUsername[LE_WIFIDEFS_MAX_USERNAME_BYTES];
//--------------------------------------------------------------------------------------------------
/**
* Password used for authentication. Used only with WPA/WPA2 protocol with RADIUS server.
*/
//--------------------------------------------------------------------------------------------------
static char SavedPassword[LE_WIFIDEFS_MAX_PASSWORD_BYTES];
//--------------------------------------------------------------------------------------------------
/**
* Indicates if the Access Point SSID is hidden from scan or not
*/
//--------------------------------------------------------------------------------------------------
static bool HiddenAccessPoint = false;
//--------------------------------------------------------------------------------------------------
/**
* The handle of the input pipe used to be notified of the WiFi events during the scan.
*/
//--------------------------------------------------------------------------------------------------
static FILE *IwScanPipePtr = NULL;
//--------------------------------------------------------------------------------------------------
/**
* The handle of the input pipe used to be notified of the WiFi (dis)connections.
*/
//--------------------------------------------------------------------------------------------------
static FILE *IwConnectPipePtr = NULL;
//--------------------------------------------------------------------------------------------------
/**
* The handle of the input pipe used to be notified of the WiFi events.
*/
//--------------------------------------------------------------------------------------------------
static FILE *IwThreadPipePtr = NULL;
//--------------------------------------------------------------------------------------------------
/**
* Flag set when a WiFi scan is in progress.
*/
//--------------------------------------------------------------------------------------------------
static bool IsScanRunning = false;
//--------------------------------------------------------------------------------------------------
/**
* The main thread running the WiFi platform adaptor.
*/
//--------------------------------------------------------------------------------------------------
static void *WifiClientPaThreadMain(void *contextPtr);
//--------------------------------------------------------------------------------------------------
/**
* The handle of the WiFi platform adaptor thread.
*/
//--------------------------------------------------------------------------------------------------
static le_thread_Ref_t WifiClientPaThread = NULL;
//--------------------------------------------------------------------------------------------------
/**
* WifiClient state event ID used to report WifiClient state events to the registered event handlers.
*/
//--------------------------------------------------------------------------------------------------
static le_event_Id_t WifiClientPaEvent;
//--------------------------------------------------------------------------------------------------
/**
* Thread destructor
*/
//--------------------------------------------------------------------------------------------------
static void ThreadDestructor
(
void *contextPtr
)
{
int status;
// Kill the script launched by popen() in Client thread
status = system("pid=`pgrep -f \""COMMAND_WIFI_SET_EVENT"\"`; [ -n \"$pid\" ] && kill -9 $pid");
if (!WIFEXITED(status) || (0 != WEXITSTATUS(status)))
{
LE_WARN("Unable to kill the WIFI events script");
}
if (IwThreadPipePtr)
{
// And close FP used in created thread
pclose(IwThreadPipePtr);
IwThreadPipePtr = NULL;
}
}
//--------------------------------------------------------------------------------------------------
/**
* The first-layer WiFi Client Event Handler.
*
*/
//--------------------------------------------------------------------------------------------------
static void FirstLayerWifiClientEventHandler
(
void *reportPtr,
void *secondLayerHandlerFuncPtr
)
{
pa_wifiClient_NewEventHandlerFunc_t clientHandlerFunc = secondLayerHandlerFuncPtr;
le_wifiClient_Event_t *wifiEventPtr = (le_wifiClient_Event_t *)reportPtr;
if (NULL != wifiEventPtr)
{
LE_DEBUG("Event: %d", *wifiEventPtr);
clientHandlerFunc(*wifiEventPtr, le_event_GetContextPtr());
}
else
{
LE_ERROR("Event is NULL");
}
}
//--------------------------------------------------------------------------------------------------
/**
* WiFi Client PA Thread
*
*/
//--------------------------------------------------------------------------------------------------
static void *WifiClientPaThreadMain
(
void *contextPtr
)
{
char tmpString[] = (WIFI_SCRIPT_PATH COMMAND_WIFI_SET_EVENT);
char path[1024];
LE_INFO("Started!");
// Open the command "iw events" for reading.
IwThreadPipePtr = popen(tmpString, "r");
if (NULL == IwThreadPipePtr)
{
LE_ERROR("Failed to run command:\"%s\" errno:%d %s",
(tmpString),
errno,
strerror(errno));
return NULL;
}
// Read the output one line at a time - output it.
while (NULL != fgets(path, sizeof(path) - 1, IwThreadPipePtr))
{
LE_DEBUG("PARSING:%s: len:%d", path, (int) strnlen(path, sizeof(path) - 1));
if (NULL != strstr(path, "connected to"))
{
LE_INFO("FOUND connected");
// Report event: LE_WIFICLIENT_EVENT_CONNECTED
le_wifiClient_Event_t event = LE_WIFICLIENT_EVENT_CONNECTED;
LE_DEBUG("InternalWifiClientStateEvent event: %d ", event);
le_event_Report(WifiClientPaEvent, (void *)&event, sizeof(le_wifiClient_Event_t));
}
else if (NULL != strstr(path, "disconnected"))
{
LE_INFO("FOUND disconnected");
// Report event: LE_WIFICLIENT_EVENT_DISCONNECTED
le_wifiClient_Event_t event = LE_WIFICLIENT_EVENT_DISCONNECTED;
LE_DEBUG("InternalWifiClientStateEvent event: %d ", event);
le_event_Report(WifiClientPaEvent, (void *)&event, sizeof(le_wifiClient_Event_t));
}
}
// Run the event loop
le_event_RunLoop();
return NULL;
}
//--------------------------------------------------------------------------------------------------
// Public declarations
//--------------------------------------------------------------------------------------------------
//--------------------------------------------------------------------------------------------------
/**
* This function must be called to initialize the PA WiFi Module.
*
* @return LE_OK The function succeeded.
*/
//--------------------------------------------------------------------------------------------------
le_result_t pa_wifiClient_Init
(
void
)
{
LE_INFO("Init called");
// Create the event for signaling user handlers.
WifiClientPaEvent = le_event_CreateId("WifiClientPaEvent", sizeof(le_wifiClient_Event_t));
return LE_OK;
}
//--------------------------------------------------------------------------------------------------
/**
* This function must be called to release the PA WiFi Module.
*
* @return LE_OK The function succeeded.
*/
//--------------------------------------------------------------------------------------------------
le_result_t pa_wifiClient_Release
(
void
)
{
LE_INFO("Release called");
return LE_OK;
}
//--------------------------------------------------------------------------------------------------
/**
* Start WiFi Client PA
*
* @return LE_FAULT The function failed.
* @return LE_OK The function succeeded.
*/
//--------------------------------------------------------------------------------------------------
le_result_t pa_wifiClient_Start
(
void
)
{
int status;
/* Create WiFi Client PA Thread */
WifiClientPaThread = le_thread_Create("WifiClientPaThread", WifiClientPaThreadMain, NULL);
le_thread_SetJoinable(WifiClientPaThread);
le_thread_AddChildDestructor(WifiClientPaThread, ThreadDestructor, NULL);
le_thread_Start(WifiClientPaThread);
status = system(WIFI_SCRIPT_PATH COMMAND_WIFI_HW_START);
/**
* Returned values:
* 0: if the interface is correctly moutned
* -1: if the fork() has failed (see man system)
* 91: if module is not loaded or interface not seen
*/
if (!WIFEXITED(status) || (0 != WEXITSTATUS(status)))
{
LE_WARN("Unable to start WiFi client; command (%d)" COMMAND_WIFI_HW_START, status);
le_thread_Cancel(WifiClientPaThread);
le_thread_Join(WifiClientPaThread, NULL);
return LE_FAULT;
}
LE_DEBUG("WiFi client started correctly");
return LE_OK;
}
//--------------------------------------------------------------------------------------------------
/**
* Stop WiFi Client PA
*
* @return LE_FAULT The function failed.
* @return LE_OK The function succeeded.
*/
//--------------------------------------------------------------------------------------------------
le_result_t pa_wifiClient_Stop
(
void
)
{
int status = system(WIFI_SCRIPT_PATH COMMAND_WIFI_HW_STOP);
/**
* Returned values:
* 0: if the interface is correctly unmounted
* -1: if the fork() has failed (see man system)
* 92: if unable to stop the interface
*/
if (!WIFEXITED(status) || (0 != WEXITSTATUS(status)))
{
LE_ERROR("WiFi Client Command Failed: (%d)" COMMAND_WIFI_HW_STOP, status);
return LE_FAULT;
}
/* Terminate the created thread */
le_thread_Cancel(WifiClientPaThread);
if (LE_OK != le_thread_Join(WifiClientPaThread, NULL))
{
return LE_FAULT;
}
LE_DEBUG("WiFi client stopped correctly");
return LE_OK;
}
//--------------------------------------------------------------------------------------------------
/**
* This function will start a scan and returns when it is done.
* It should NOT return until the scan is done.
* Results are read via pa_wifiClient_GetScanResult.
* When the reading is done pa_wifiClient_ScanDone MUST be called.
*
* @return LE_FAULT The function failed.
* @return LE_BUSY The function is already ongoing.
* @return LE_OK The function succeeded.
*/
//--------------------------------------------------------------------------------------------------
le_result_t pa_wifiClient_Scan
(
void
)
{
le_result_t result = LE_OK;
LE_INFO("Scanning");
if (IsScanRunning)
{
LE_ERROR("Scan is already running");
return LE_BUSY;
}
if (NULL != IwScanPipePtr)
{
return LE_BUSY;
}
IsScanRunning = true;
/* Open the command for reading. */
IwScanPipePtr = popen(WIFI_SCRIPT_PATH COMMAND_WIFICLIENT_START_SCAN, "r");
if (NULL == IwScanPipePtr)
{
LE_ERROR("Failed to run command: errno:%d: \"%s\" Cmd:"
COMMAND_WIFICLIENT_START_SCAN,
errno,
strerror(errno));
result = LE_FAULT;
}
IsScanRunning = false;
return result;
}
//--------------------------------------------------------------------------------------------------
/**
* This function is used to find out if a scan is currently running.
*
* @return TRUE Scan is running.
* @return FALSE Scan is not running
*/
//--------------------------------------------------------------------------------------------------
bool pa_wifiClient_IsScanRunning
(
)
{
return IsScanRunning;
}
//--------------------------------------------------------------------------------------------------
/**
* This function can be called after pa_wifi_Scan.
* When the reading is done, it no longer returns LE_OK,
* pa_wifiClient_ScanDone MUST be called.
*
* @return LE_NOT_FOUND There is no more AP:s found.
* @return LE_OK The function succeeded.
* @return LE_FAULT The function failed.
*/
//--------------------------------------------------------------------------------------------------
le_result_t pa_wifiClient_GetScanResult
(
pa_wifiClient_AccessPoint_t *accessPointPtr
///< [IN][OUT]
///< Structure provided by calling function.
///< Results filled out if result was LE_OK.
)
{
const char bssidPrefix[] = "BSS ";
const char ssidPrefix[] = "\tSSID: ";
const char signalPrefix[] = "\tsignal: ";
const unsigned int bssidPrefixLen = NUM_ARRAY_MEMBERS(bssidPrefix) - 1;
const unsigned int ssidPrefixLen = NUM_ARRAY_MEMBERS(ssidPrefix) - 1;
const unsigned int signalPrefixLen = NUM_ARRAY_MEMBERS(signalPrefix) - 1;
char path[1024];
struct timeval tv;
fd_set fds;
time_t start = time(NULL);
le_result_t ret = LE_NOT_FOUND;
int err;
LE_INFO("Scan results");
if (NULL == IwScanPipePtr)
{
LE_ERROR("ERROR must call pa_wifi_Scan first");
return LE_FAULT;
}
if (NULL == accessPointPtr)
{
LE_ERROR("ERROR : accessPoint == NULL");
return LE_BAD_PARAMETER;
}
/* Default values */
accessPointPtr->signalStrength = LE_WIFICLIENT_NO_SIGNAL_STRENGTH;
accessPointPtr->ssidLength = 0;
memset(&accessPointPtr->ssidBytes, 0, LE_WIFIDEFS_MAX_SSID_BYTES);
memset(&accessPointPtr->bssid, 0, LE_WIFIDEFS_MAX_BSSID_BYTES);
/* Read the output a line at a time - output it. */
while (IwScanPipePtr)
{
// Set up the timeout. here we can wait for 1 second
tv.tv_sec = 1;
tv.tv_usec = 0;
FD_ZERO(&fds);
FD_SET(fileno(IwScanPipePtr), &fds);
err = select(fileno(IwScanPipePtr) + 1, &fds, NULL, NULL, &tv);
if (!err)
{
LE_DEBUG("loop=%lu", time(NULL) - start);
if ((time(NULL) - start) >= 5)
{
LE_WARN("Scan timeout");
goto cleanup;
}
continue;
}
else if (err < 0)
{
LE_ERROR("select() failed(%d)", errno);
goto cleanup;
}
else if (FD_ISSET(fileno(IwScanPipePtr), &fds))
{
LE_DEBUG("Read next scan result");
if (NULL != fgets(path, sizeof(path), IwScanPipePtr))
{
LE_DEBUG("PARSING: '%s'", path);
if (0 == strncmp(ssidPrefix, path, ssidPrefixLen))
{
accessPointPtr->ssidLength =
strnlen(path, LE_WIFIDEFS_MAX_SSID_BYTES + ssidPrefixLen) - ssidPrefixLen - 1;
LE_DEBUG("FOUND SSID: '%s'", &path[ssidPrefixLen]);
memcpy(&accessPointPtr->ssidBytes, &path[ssidPrefixLen],
accessPointPtr->ssidLength);
LE_DEBUG("SSID: '%s'", &accessPointPtr->ssidBytes[0]);
ret = LE_OK;
goto cleanup;
}
else if (0 == strncmp(signalPrefix, path, signalPrefixLen))
{
LE_DEBUG("FOUND SIGNAL STRENGTH: '%s'", &path[signalPrefixLen]);
accessPointPtr->signalStrength = atoi(&path[signalPrefixLen]);
LE_DEBUG("signal(%d)", accessPointPtr->signalStrength);
}
else if (0 == strncmp(bssidPrefix, path, bssidPrefixLen))
{
LE_DEBUG("FOUND BSSID: '%s'", &path[bssidPrefixLen]);
memcpy(&accessPointPtr->bssid, &path[bssidPrefixLen],
LE_WIFIDEFS_MAX_BSSID_LENGTH);
LE_DEBUG("BSSID: '%s'", &accessPointPtr->bssid[0]);
}
}
else
{
LE_DEBUG("End of scan results");
goto cleanup;
}
}
}
cleanup:
return ret;
}
//--------------------------------------------------------------------------------------------------
/**
* This function must be called after the pa_wifiClient_Scan() has been done.
* It signals that the scan results are no longer needed and frees some internal resources.
*
* @return LE_OK The function succeeded.
* @return LE_FAULT The scan failed.
*/
//--------------------------------------------------------------------------------------------------
le_result_t pa_wifiClient_ScanDone
(
void
)
{
le_result_t res = LE_OK;
if (NULL != IwScanPipePtr)
{
int st = pclose(IwScanPipePtr);
if (WIFEXITED(st))
{
LE_DEBUG("Scan exit status(%d)", WEXITSTATUS(st));
res = WEXITSTATUS(st) ? LE_FAULT:LE_OK;
if (res != LE_OK)
{
LE_ERROR("Scan failed(%d)", WEXITSTATUS(st));
res = LE_FAULT;
}
}
IwScanPipePtr = NULL;
IsScanRunning = false;
}
return res;
}
//--------------------------------------------------------------------------------------------------
/**
* Set the security protocol for communication.
*
* @return LE_BAD_PARAMETER The function failed due to an invalid parameter.
* @return LE_OK The function succeeded.
*/
//--------------------------------------------------------------------------------------------------
le_result_t pa_wifiClient_SetSecurityProtocol
(
const le_wifiClient_SecurityProtocol_t securityProtocol
///< [IN]
///< Security protocol used for communication.
)
{
le_result_t result = LE_OK;
LE_DEBUG("Security protocol: %d", securityProtocol);
switch (securityProtocol)
{
case LE_WIFICLIENT_SECURITY_NONE:
case LE_WIFICLIENT_SECURITY_WEP:
case LE_WIFICLIENT_SECURITY_WPA_PSK_PERSONAL:
case LE_WIFICLIENT_SECURITY_WPA2_PSK_PERSONAL:
case LE_WIFICLIENT_SECURITY_WPA_EAP_PEAP0_ENTERPRISE:
case LE_WIFICLIENT_SECURITY_WPA2_EAP_PEAP0_ENTERPRISE:
SavedSecurityProtocol = securityProtocol;
result = LE_OK;
break;
default:
result = LE_BAD_PARAMETER;
break;
}
return result;
}
//--------------------------------------------------------------------------------------------------
/**
* Compute the PSK from the SSID and the passphrase.
*
* @return LE_BAD_PARAMETER The function failed due to an invalid parameter.
* @return LE_FAIL The function failed.
* @return LE_OK The function succeeded.
*/
//--------------------------------------------------------------------------------------------------
static le_result_t GeneratePsk
(
char *ssidBytesPtr,
///< [IN]
///< Contains ssidLength number of bytes
uint8_t ssidLength,
///< [IN]
///< The number of bytes in the ssidBytesPtr
char *passphrasePtr,
///< [IN]
///< The passphrase
char *pskPtr
///< [OUT]
///< The generated PSK
)
{
char tmpString[255];
le_result_t result = LE_OK;
if (NULL == pskPtr)
{
LE_ERROR("Invalid PSK pointer");
return LE_BAD_PARAMETER;
}
if (NULL == passphrasePtr)
{
LE_ERROR("Invalid passphrase pointer");
return LE_BAD_PARAMETER;
}
LE_DEBUG("Step 1: Generate Passphrase/PSK");
snprintf(tmpString,
sizeof(tmpString),
(WIFI_SCRIPT_PATH COMMAND_WIFICLIENT_CONNECT_WPA_PASSPHRASE),
ssidLength,
(char *)ssidBytesPtr,
passphrasePtr);
LE_DEBUG("Cmd: %s", tmpString);
// Open the command for reading.
IwConnectPipePtr = popen(tmpString, "r");
if (NULL == IwConnectPipePtr)
{
LE_ERROR("Failed to run command:\"%s\" errno:%d %s",
(tmpString),
errno,
strerror(errno));
result = LE_FAULT;
}
else
{
char path[255];
LE_DEBUG("Cmd successful: %s", tmpString);
// Read the output a line at a time - output it.
while (NULL != fgets(path, sizeof(path) - 1, IwConnectPipePtr))
{
LE_DEBUG("PARSING: len=%3d | %s", (int)strnlen(path, sizeof(path) - 1), path);
if (0 == strncmp("\tpsk=", path, strlen("\tpsk=")))
{
char *p1Ptr = strchr(&path[5], '\n');
char *p2Ptr = &path[5];
uint32_t n = p1Ptr - p2Ptr;
LE_DEBUG("FOUND :%s", path);
memset(pskPtr, 0, LE_WIFIDEFS_MAX_PSK_BYTES);
if (n < LE_WIFIDEFS_MAX_PSK_BYTES)
{
strncpy(pskPtr, &path[5], n);
LE_DEBUG("PSK=%s | length: %d", pskPtr, n);
result = LE_OK;
break;
}
else
{
LE_ERROR("PSK length > %d", LE_WIFIDEFS_MAX_PSK_LENGTH);
result = LE_FAULT;
}
}
}
pclose(IwConnectPipePtr);
}
return result;
}
//--------------------------------------------------------------------------------------------------
/**
* This function generates the WPA supplicant file.
*/
//--------------------------------------------------------------------------------------------------
static le_result_t GenerateWpaSupplicant
(
char *ssidPtr,
const uint32_t ssidLength,
char *pskPtr
)
{
FILE *filePtr;
char tmpString[255];
uint8_t length;
LE_DEBUG("Step 2: SH script");
// Check parameter
if ((ssidPtr == NULL) || (ssidLength == 0) || (ssidLength > LE_WIFIDEFS_MAX_SSID_LENGTH))
{
LE_ERROR("Invalid parameter(s)");
return LE_FAULT;
}
// Create the WPA supplicant file
filePtr = fopen(WPA_SUPPLICANT_FILE, "w");
if (filePtr == NULL)
{
LE_ERROR("Unable to create %s file.", WPA_SUPPLICANT_FILE);
return LE_FAULT;
}
if (pskPtr)
{
snprintf(tmpString, sizeof(tmpString), WPA_SUPPLICANT_DATA,
ssidLength, (char *)ssidPtr, HiddenAccessPoint, pskPtr);
}
else
{
snprintf(tmpString, sizeof(tmpString), WPA_SUPPLICANT_DATA_NO_SECURITY,
ssidLength, (char *)ssidPtr, HiddenAccessPoint);
}
length = strlen(tmpString);
if (fwrite(&tmpString, 1, length, filePtr) != length)
{
LE_ERROR("Unable to generate the WPA supplicant file (%s).", WPA_SUPPLICANT_FILE);
fclose(filePtr);
return LE_FAULT;
}
fclose(filePtr);
return LE_OK;
}
//--------------------------------------------------------------------------------------------------
/**
* This function connects a wifiClient.
*
* @return LE_FAULT The function failed.
* @return LE_OK The function succeeded.
*/
//--------------------------------------------------------------------------------------------------
le_result_t pa_wifiClient_Connect
(
uint8_t ssidBytes[LE_WIFIDEFS_MAX_SSID_BYTES],
///< [IN]
///< Contains ssidLength number of bytes
uint8_t ssidLength
///< [IN]
///< The number of Bytes in the ssidBytes
)
{
int systemResult;
char tmpString[1024];
le_result_t result = LE_OK;
LE_INFO("Connecting over SSID length %d SSID: \"%.*s\"", ssidLength, ssidLength,
(char *)ssidBytes);
if (0 == ssidLength)
{
LE_ERROR("Invalid SSID");
return LE_FAULT;
}
// Which type of WiFi client connection is requested?
switch (SavedSecurityProtocol)
{
case LE_WIFICLIENT_SECURITY_NONE:
// Opened WiFi hotspot (SSID with no password)
LE_DEBUG("SwitchCase: LE_WIFICLIENT_SECURITY_NONE");
// 1. Now WPA_CLI for LE_WIFICLIENT_SECURITY_NONE
LE_DEBUG("Step 2: SH script");
snprintf(tmpString,
sizeof(tmpString),
(WIFI_SCRIPT_PATH COMMAND_WIFICLIENT_CONNECT_SECURITY_NONE),
ssidLength,
(char *)ssidBytes,
HiddenAccessPoint);
systemResult = system(tmpString);
// Return value of -1 means that the fork() has failed (see man system).
if (0 == WEXITSTATUS(systemResult))
{
LE_DEBUG("WiFi Client Command OK: %s", tmpString);
result = LE_OK;
}
else
{
LE_ERROR("WiFi Client Command %s Failed: (%d)", tmpString, systemResult);
result = LE_FAULT;
}
break;
case LE_WIFICLIENT_SECURITY_WEP:
// Connect to secured network - WEP
LE_DEBUG("SwitchCase: LE_WIFICLIENT_SECURITY_WEP");
if (0 == SavedWepKey[0])
{
LE_ERROR("No valid WEP key");
return LE_FAULT;
}
// 1. WPA_CLI for LE_WIFICLIENT_SECURITY_WEP
if (LE_OK == result)
{
LE_DEBUG("Step 2: SH script");
snprintf(tmpString,
sizeof(tmpString),
(WIFI_SCRIPT_PATH COMMAND_WIFICLIENT_CONNECT_SECURITY_WEP),
ssidLength,
(char *)ssidBytes,
HiddenAccessPoint,
SavedWepKey);
systemResult = system(tmpString);
// Return value of -1 means that the fork() has failed (see man system).
if (0 == WEXITSTATUS(systemResult))
{
LE_DEBUG("WiFi Client Command OK: %s", tmpString);
result = LE_OK;
}
else
{
LE_ERROR("WiFi Client Command %s Failed: (%d)", tmpString, systemResult);
result = LE_FAULT;
}
}
break;
case LE_WIFICLIENT_SECURITY_WPA_PSK_PERSONAL:
// Connect to secured network - WPA
LE_DEBUG("SwitchCase: LE_WIFICLIENT_SECURITY_WPA_PSK_PERSONAL");
if ((0 == SavedPassphrase[0]) && (0 == SavedPreSharedKey[0]))
{
LE_ERROR("No valid PassPhrase or PreSharedKey");
return LE_FAULT;
}
// Passphrase is defined...
if (0 != SavedPassphrase[0])
{
// ... so the PSK is derived from it.
result = GeneratePsk((char *)&ssidBytes[0],
ssidLength,
(char *)&SavedPassphrase[0],
(char *)&SavedPreSharedKey[0]);
if (LE_OK != result)
{
LE_ERROR("No valid PassPhrase or PreSharedKey");
return LE_FAULT;
}
}
if (LE_OK == result)
{
LE_DEBUG("Step 2: Generate WPA supplicant file");
// Generate the WPA supplicant file
if (GenerateWpaSupplicant((char *)&ssidBytes[0],
ssidLength,
(char *)&SavedPreSharedKey) == LE_OK)
{
LE_DEBUG("Step 3: SH script");
systemResult = system(WIFI_SCRIPT_PATH
COMMAND_WIFICLIENT_CONNECT_SECURITY_WPA_PSK_PERSONAL);
// Return value of -1 means that the fork() has failed (see man system).
if (0 == WEXITSTATUS(systemResult))
{
LE_DEBUG("WiFi Client Command OK: %s",
WIFI_SCRIPT_PATH COMMAND_WIFICLIENT_CONNECT_SECURITY_WPA_PSK_PERSONAL);
result = LE_OK;
}
else
{
LE_ERROR("WiFi Client Command %s Failed: (%d)",
WIFI_SCRIPT_PATH COMMAND_WIFICLIENT_CONNECT_SECURITY_WPA_PSK_PERSONAL,
systemResult);
result = LE_FAULT;
}
}
}
break;
case LE_WIFICLIENT_SECURITY_WPA2_PSK_PERSONAL:
// Connect to secured network - WPA2
LE_DEBUG("SwitchCase: LE_WIFICLIENT_SECURITY_WPA2_PSK_PERSONAL");
if ((0 == SavedPassphrase[0]) && (0 == SavedPreSharedKey[0]))
{
LE_ERROR("No valid PassPhrase or PreSharedKey");
return LE_FAULT;
}
// Passphrase is defined...
if (0 != SavedPassphrase[0])
{
// ... so the PSK is derived from it.
result = GeneratePsk((char *)&ssidBytes[0],
ssidLength,
(char *)&SavedPassphrase,
(char *)&SavedPreSharedKey);
if (LE_OK != result)
{
LE_ERROR("No valid PassPhrase or PreSharedKey");
return LE_FAULT;
}
}
if (LE_OK == result)
{
LE_DEBUG("Step 2: Generate WPA supplicant file");
// Generate the WPA supplicant file
if (GenerateWpaSupplicant((char *)&ssidBytes[0],
ssidLength,
(char *)&SavedPreSharedKey) == LE_OK)
{
LE_DEBUG("Step 3: SH script");
systemResult = system(WIFI_SCRIPT_PATH
COMMAND_WIFICLIENT_CONNECT_SECURITY_WPA2_PSK_PERSONAL);
// Return value of -1 means that the fork() has failed (see man system).
if (0 == WEXITSTATUS(systemResult))
{
LE_DEBUG("WiFi Client Command OK: %s",