-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSoftapController.cpp
More file actions
1254 lines (1054 loc) · 30.4 KB
/
SoftapController.cpp
File metadata and controls
1254 lines (1054 loc) · 30.4 KB
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
/*
* Copyright (C) 2008 The Android Open Source Project
* Copyright (c) 2012 Eduardo José Tagle <ejtagle@tutopia.com>
* -Added AR6002 hostAp support
*
* 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 <stdlib.h>
#include <errno.h>
#include <fcntl.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <dirent.h>
#include <poll.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <linux/wireless.h>
#include <openssl/evp.h>
#include <openssl/sha.h>
#define LOG_TAG "SoftapController"
#include <cutils/log.h>
#include <cutils/properties.h>
#include <netutils/ifc.h>
#include <private/android_filesystem_config.h>
#include "wifi.h"
#include "SoftapController.h"
static const char HOSTAPD_CONF_FILE[] = "/data/misc/wifi/hostapd.conf";
#ifdef AR6002_WIFI
/* AR6002 support */
extern "C" int delete_module(const char *, unsigned int);
extern "C" int init_module(void * , unsigned int, const char *);
extern "C" void *load_file(const char *fn, unsigned *_sz);
static int insmod(const char *filename, const char *args)
{
void *module;
unsigned int size;
int ret;
module = load_file(filename, &size);
if (!module)
return -1;
ret = init_module(module, size, args);
free(module);
return ret;
}
static int rmmod(const char *modname)
{
int ret = -1;
int maxtry = 10;
while (maxtry-- > 0) {
ret = delete_module(modname, O_NONBLOCK | O_EXCL);
if (ret < 0 && errno == EAGAIN)
usleep(500000);
else
break;
}
if (ret != 0)
LOGD("Unable to unload driver module \"%s\": %s\n",
modname, strerror(errno));
return ret;
}
#ifdef HAVE_LIBC_SYSTEM_PROPERTIES
#define _REALLY_INCLUDE_SYS__SYSTEM_PROPERTIES_H_
#include <sys/_system_properties.h>
#endif
#include <sys/system_properties.h>
#include "libwpa_client/wpa_ctrl.h"
static const char IFACE_DIR[] = "/data/misc/wifi/hostapd";
static const char HOSTAPD_NAME[] = "hostapd";
static const char HOSTAPD_CONF_TEMPLATE[]= "/system/etc/wifi/hostapd.conf";
static const char HOSTAPD_PROP_NAME[] = "init.svc.hostapd";
static const char MODULE_FILE[] = "/proc/modules";
static const char DRIVER_MODULE_NAME[] = WIFI_DRIVER_MODULE_NAME;
static const char DRIVER_MODULE_TAG[] = WIFI_DRIVER_MODULE_NAME " ";
static const char DRIVER_MODULE_PATH[] = WIFI_DRIVER_MODULE_PATH;
/*#define AP_IFNAME_PREFIX "athap"*/
#define AP_IFNAME_PREFIX "wlan"
#define AR6002AP_IFNAME AP_IFNAME_PREFIX "0"
#ifndef WIFI_DRIVER_LOADER_DELAY
#define WIFI_DRIVER_LOADER_DELAY 1000000
#endif
#define WIFI_DEFAULT_BI 100 /* in TU */
#define WIFI_DEFAULT_DTIM 1 /* in beacon */
#define WIFI_DEFAULT_CHANNEL 6
#define WIFI_DEFAULT_MAX_STA 8
#define WIFI_DEFAULT_PREAMBLE 0
static struct wpa_ctrl *ctrl_conn = NULL;
static int mProfileValid = 0;
/* make sure the hostapd config exists */
static int ensure_config_file_exists()
{
char buf[2048];
int srcfd, destfd;
int nread;
/* If the file exists, we are done! */
if (access(HOSTAPD_CONF_FILE, R_OK|W_OK) == 0) {
return 0;
} else if (errno != ENOENT) {
LOGE("Cannot access \"%s\": %s", HOSTAPD_CONF_FILE, strerror(errno));
return -1;
}
/* Otherwise, copy the template as a default starting point */
srcfd = open(HOSTAPD_CONF_TEMPLATE, O_RDONLY);
if (srcfd < 0) {
LOGE("Cannot open \"%s\": %s", HOSTAPD_CONF_TEMPLATE, strerror(errno));
return -1;
}
destfd = open(HOSTAPD_CONF_FILE, O_CREAT|O_WRONLY, 0660);
if (destfd < 0) {
close(srcfd);
LOGE("Cannot create \"%s\": %s", HOSTAPD_CONF_FILE, strerror(errno));
return -1;
}
while ((nread = read(srcfd, buf, sizeof(buf))) != 0) {
if (nread < 0) {
LOGE("Error reading \"%s\": %s", HOSTAPD_CONF_TEMPLATE, strerror(errno));
close(srcfd);
close(destfd);
unlink(HOSTAPD_CONF_FILE);
return -1;
}
write(destfd, buf, nread);
}
close(destfd);
close(srcfd);
/* chmod is needed because open() didn't set permisions properly */
if (chmod(HOSTAPD_CONF_FILE, 0660) < 0) {
LOGE("Error changing permissions of %s to 0660: %s",
HOSTAPD_CONF_FILE, strerror(errno));
unlink(HOSTAPD_CONF_FILE);
return -1;
}
if (chown(HOSTAPD_CONF_FILE, AID_SYSTEM, AID_WIFI) < 0) {
LOGE("Error changing group ownership of %s to %d: %s",
HOSTAPD_CONF_FILE, AID_WIFI, strerror(errno));
unlink(HOSTAPD_CONF_FILE);
return -1;
}
return 0;
}
/**
* wifi_hostap_ctrl_cleanup() - Delete any local UNIX domain socket files that
* may be left over from clients that were previously connected to
* wpa_supplicant. This keeps these files from being orphaned in the
* event of crashes that prevented them from being removed as part
* of the normal orderly shutdown.
*/
static void wifi_hostap_ctrl_cleanup(void)
{
DIR *dir;
struct dirent entry;
struct dirent *result;
size_t dirnamelen;
size_t maxcopy;
char pathname[PATH_MAX];
char *namep;
const char *local_socket_dir = IFACE_DIR;
const char *local_socket_prefix = AP_IFNAME_PREFIX;
if ((dir = opendir(local_socket_dir)) == NULL)
return;
dirnamelen = (size_t)snprintf(pathname, sizeof(pathname), "%s/", local_socket_dir);
if (dirnamelen >= sizeof(pathname)) {
closedir(dir);
return;
}
namep = pathname + dirnamelen;
maxcopy = PATH_MAX - dirnamelen;
while (readdir_r(dir, &entry, &result) == 0 && result != NULL) {
if (strncmp(entry.d_name, local_socket_prefix, strlen(local_socket_prefix)) == 0) {
if (strlcpy(namep, entry.d_name, maxcopy) < maxcopy) {
unlink(pathname);
}
}
}
closedir(dir);
}
/* check if wifi driver is loaded */
static int is_wifi_module_loaded()
{
FILE *proc;
char line[sizeof(DRIVER_MODULE_TAG)+10];
/*
* If the property says the driver is loaded, check to
* make sure that the property setting isn't just left
* over from a previous manual shutdown or a runtime
* crash.
*/
if ((proc = fopen(MODULE_FILE, "r")) == NULL) {
LOGW("Could not open %s: %s", MODULE_FILE, strerror(errno));
return 0;
}
while ((fgets(line, sizeof(line), proc)) != NULL) {
if (strncmp(line, DRIVER_MODULE_TAG, sizeof(DRIVER_MODULE_TAG)) == 0) {
fclose(proc);
return 1;
}
}
fclose(proc);
return 0;
}
/* unload wifi driver */
static int wifi_unload_module()
{
usleep(200000); /* allow to finish interface down */
if (rmmod(DRIVER_MODULE_NAME) == 0) {
int count = 20; /* wait at most 10 seconds for completion */
while (count-- > 0) {
if (!is_wifi_module_loaded())
break;
usleep(500000);
}
usleep(500000); /* allow card removal */
if (count) {
return 0;
}
return -1;
} else
return -1;
}
/* start the hostapd daemon */
static int wifi_start_hostapd()
{
char supp_status[PROPERTY_VALUE_MAX] = {'\0'};
int count = 300; /* wait at most 30 seconds for completion */
char mac_buff[15] = {'\0'};
#ifdef HAVE_LIBC_SYSTEM_PROPERTIES
const prop_info *pi;
unsigned serial = 0;
#endif
/* Check whether already running */
if (property_get(HOSTAPD_PROP_NAME, supp_status, NULL)
&& strcmp(supp_status, "running") == 0) {
return 0;
}
/* Before starting the daemon, make sure its config file exists */
if (ensure_config_file_exists() < 0) {
LOGE("Wi-Fi HostAP will not be enabled");
return -1;
}
/* Clear out any stale socket files that might be left over. */
wifi_hostap_ctrl_cleanup();
#ifdef HAVE_LIBC_SYSTEM_PROPERTIES
/*
* Get a reference to the status property, so we can distinguish
* the case where it goes stopped => running => stopped (i.e.,
* it start up, but fails right away) from the case in which
* it starts in the stopped state and never manages to start
* running at all.
*/
pi = __system_property_find(HOSTAPD_PROP_NAME);
if (pi != NULL) {
serial = pi->serial;
}
#endif
property_set("ctl.start", HOSTAPD_NAME);
sched_yield();
while (count-- > 0) {
#ifdef HAVE_LIBC_SYSTEM_PROPERTIES
if (pi == NULL) {
pi = __system_property_find(HOSTAPD_PROP_NAME);
}
if (pi != NULL) {
__system_property_read(pi, NULL, supp_status);
if (strcmp(supp_status, "running") == 0) {
return 0;
} else if (pi->serial != serial &&
strcmp(supp_status, "stopped") == 0) {
return -1;
}
}
#else
if (property_get(HOSTAPD_PROP_NAME, supp_status, NULL)) {
if (strcmp(supp_status, "running") == 0)
return 0;
}
#endif
usleep(100000);
}
return -1;
}
/* stop the hostapd daemon */
static int wifi_stop_hostapd()
{
char supp_status[PROPERTY_VALUE_MAX] = {'\0'};
int count = 50; /* wait at most 5 seconds for completion */
/* Check whether hostapd already stopped */
if (property_get(HOSTAPD_PROP_NAME, supp_status, NULL)
&& strcmp(supp_status, "stopped") == 0) {
return 0;
}
property_set("ctl.stop", HOSTAPD_NAME);
sched_yield();
while (count-- > 0) {
if (property_get(HOSTAPD_PROP_NAME, supp_status, NULL)) {
if (strcmp(supp_status, "stopped") == 0)
return 0;
}
usleep(100000);
}
return -1;
}
/* Connect to hostapd */
static int wifi_connect_to_hostapd()
{
char ifname[256];
char supp_status[PROPERTY_VALUE_MAX] = {'\0'};
/* Make sure hostapd is running */
if (!property_get(HOSTAPD_PROP_NAME, supp_status, NULL)
|| strcmp(supp_status, "running") != 0) {
LOGE("hostapd not running, cannot connect");
return -1;
}
if (access(IFACE_DIR, F_OK) == 0) {
snprintf(ifname, sizeof(ifname), "%s/%s", IFACE_DIR, AR6002AP_IFNAME);
} else {
strlcpy(ifname, AR6002AP_IFNAME, sizeof(ifname));
}
LOGD("ifname = %s\n", ifname);
{ /* check iface file is ready */
int cnt = 160; /* 8 seconds (160*50)*/
sched_yield();
while (cnt--) {
if (access(ifname, F_OK|W_OK)==0) {
LOGD("ifname %s is ready to read/write cnt=%d\n", ifname, cnt);
/* Attach to monitor hostapd activity */
ctrl_conn = wpa_ctrl_open(ifname);
/* if done, break */
if (ctrl_conn)
break;
}
usleep(50000);
}
}
if (ctrl_conn == NULL) {
LOGE("Unable to open connection to hostapd on \"%s\": %s",
ifname, strerror(errno));
return -1;
}
if (wpa_ctrl_attach(ctrl_conn) != 0) {
wpa_ctrl_close(ctrl_conn);
ctrl_conn = NULL;
return -1;
}
return 0;
}
/* Called to dump hostapd messages */
static void hostapd_cli_msg_cb(char *msg, size_t len)
{
LOGD("%s\n", msg);
}
static int wpa_ctrl_command_sta(const char *cmd, char *addr, size_t addr_len)
{
char buf[4096], *pos;
size_t len;
int ret;
if (ctrl_conn == NULL) {
LOGE("Not connected to hostapd - command dropped.\n");
return -1;
}
len = sizeof(buf) - 1;
ret = wpa_ctrl_request(ctrl_conn, cmd, strlen(cmd), buf, &len,
hostapd_cli_msg_cb);
if (ret == -2) {
LOGE("'%s' command timed out.\n", cmd);
return -2;
} else if (ret < 0) {
LOGE("'%s' command failed.\n", cmd);
return -1;
}
buf[len] = '\0';
if (memcmp(buf, "FAIL", 4) == 0)
return -1;
LOGD("%s", buf);
pos = buf;
while (*pos != '\0' && *pos != '\n')
pos++;
*pos = '\0';
strlcpy(addr, buf, addr_len);
return 0;
}
static void wifi_close_hostapd_connection()
{
if (ctrl_conn != NULL) {
wpa_ctrl_close(ctrl_conn);
ctrl_conn = NULL;
}
}
static int wifi_load_profile(bool started)
{
if ((started) && (mProfileValid)) {
if (ctrl_conn == NULL) {
return -1;
}
}
return 0;
}
#endif
SoftapController::SoftapController() {
mPid = 0;
mSock = socket(AF_INET, SOCK_DGRAM, 0);
if (mSock < 0)
LOGE("Failed to open socket");
memset(mIface, 0, sizeof(mIface));
#ifdef AR6002_WIFI
mProfileValid = 0;
ctrl_conn = NULL;
#endif
}
SoftapController::~SoftapController() {
if (mSock >= 0)
close(mSock);
}
int SoftapController::setCommand(char *iface, const char *fname, unsigned buflen) {
#ifdef HAVE_HOSTAPD
return 0;
#else
char tBuf[SOFTAP_MAX_BUFFER_SIZE];
struct iwreq wrq;
struct iw_priv_args *priv_ptr;
int i, j, ret;
int cmd = 0, sub_cmd = 0;
strncpy(wrq.ifr_name, iface, sizeof(wrq.ifr_name));
wrq.u.data.pointer = tBuf;
wrq.u.data.length = sizeof(tBuf) / sizeof(struct iw_priv_args);
wrq.u.data.flags = 0;
if ((ret = ioctl(mSock, SIOCGIWPRIV, &wrq)) < 0) {
LOGE("SIOCGIPRIV failed: %d", ret);
return ret;
}
priv_ptr = (struct iw_priv_args *)wrq.u.data.pointer;
for(i=0; i < wrq.u.data.length;i++) {
if (strcmp(priv_ptr[i].name, fname) == 0) {
cmd = priv_ptr[i].cmd;
break;
}
}
if (i == wrq.u.data.length) {
LOGE("iface:%s, fname: %s - function not supported", iface, fname);
return -1;
}
if (cmd < SIOCDEVPRIVATE) {
for(j=0; j < i; j++) {
if ((priv_ptr[j].set_args == priv_ptr[i].set_args) &&
(priv_ptr[j].get_args == priv_ptr[i].get_args) &&
(priv_ptr[j].name[0] == '\0'))
break;
}
if (j == i) {
LOGE("iface:%s, fname: %s - invalid private ioctl", iface, fname);
return -1;
}
sub_cmd = cmd;
cmd = priv_ptr[j].cmd;
}
strncpy(wrq.ifr_name, iface, sizeof(wrq.ifr_name));
if ((buflen == 0) && (*mBuf != 0))
wrq.u.data.length = strlen(mBuf) + 1;
else
wrq.u.data.length = buflen;
wrq.u.data.pointer = mBuf;
wrq.u.data.flags = sub_cmd;
ret = ioctl(mSock, cmd, &wrq);
return ret;
#endif
}
int SoftapController::startDriver(char *iface) {
int ret;
if (mSock < 0) {
LOGE("Softap driver start - failed to open socket");
return -1;
}
if (!iface || (iface[0] == '\0')) {
LOGD("Softap driver start - wrong interface");
iface = mIface;
}
#ifndef AR6002_WIFI
/* Original way */
*mBuf = 0;
ret = setCommand(iface, "START");
if (ret < 0) {
LOGE("Softap driver start: %d", ret);
return ret;
}
#ifdef HAVE_HOSTAPD
ifc_init();
ret = ifc_up(iface);
ifc_close();
#endif
#else
/* Just bring up the interface */
ifc_init();
ret = ifc_up(AR6002AP_IFNAME);
ifc_close();
#endif
usleep(AP_DRIVER_START_DELAY);
LOGD("Softap driver start: %d", ret);
return ret;
}
int SoftapController::stopDriver(char *iface) {
int ret;
if (mSock < 0) {
LOGE("Softap driver stop - failed to open socket");
return -1;
}
if (!iface || (iface[0] == '\0')) {
LOGD("Softap driver stop - wrong interface");
iface = mIface;
}
*mBuf = 0;
#ifndef AR6002_WIFI
/* Original way */
#ifdef HAVE_HOSTAPD
ifc_init();
ret = ifc_down(iface);
ifc_close();
if (ret < 0) {
LOGE("Softap %s down: %d", iface, ret);
}
#endif
ret = setCommand(iface, "STOP");
#else
/* Just bring down the interface */
ifc_init();
ret = ifc_down(AR6002AP_IFNAME);
ifc_close();
if (ret < 0) {
LOGE("Softap %s down: %d", AR6002AP_IFNAME, ret);
}
#endif
LOGD("Softap driver stop: %d", ret);
return ret;
}
int SoftapController::startSoftap() {
pid_t pid = 1;
int ret = 0;
if (mPid) {
LOGE("Softap already started");
return 0;
}
if (mSock < 0) {
LOGE("Softap startap - failed to open socket");
return -1;
}
#ifndef AR6002_WIFI
/* original way */
#ifdef HAVE_HOSTAPD
if ((pid = fork()) < 0) {
LOGE("fork failed (%s)", strerror(errno));
return -1;
}
#endif
if (!pid) {
#ifdef HAVE_HOSTAPD
ensure_entropy_file_exists();
if (execl("/system/bin/hostapd", "/system/bin/hostapd",
"-e", WIFI_ENTROPY_FILE,
HOSTAPD_CONF_FILE, (char *) NULL)) {
LOGE("execl failed (%s)", strerror(errno));
}
#endif
LOGE("Should never get here!");
return -1;
} else {
*mBuf = 0;
ret = setCommand(mIface, "AP_BSS_START");
if (ret) {
LOGE("Softap startap - failed: %d", ret);
}
else {
mPid = pid;
LOGD("Softap startap - Ok");
usleep(AP_BSS_START_DELAY);
}
}
#else
/* AR6002 specific */
/* Before starting the daemon, make sure its config file exists */
ret = ensure_config_file_exists();
if (ret < 0) {
LOGE("Softap startup - configuration file missing");
stopDriver((char*)AR6002AP_IFNAME);
return -1;
}
ret = wifi_start_hostapd();
if (ret < 0) {
LOGE("Softap startap - starting hostapd fails");
stopDriver((char*)AR6002AP_IFNAME);
return -1;
}
sched_yield();
usleep(100000);
ret = wifi_connect_to_hostapd();
if (ret < 0) {
LOGE("Softap startap - connect to hostapd fails");
return -1;
}
/* Indicate interface up */
ret = wifi_load_profile(true);
if (ret < 0) {
LOGE("Softap startap - load new configuration fails");
return -1;
}
if (ret) {
LOGE("Softap startap - failed: %d", ret);
}
else {
mPid = pid;
LOGD("Softap startap - Ok");
usleep(AP_BSS_START_DELAY);
}
#endif
return ret;
}
int SoftapController::stopSoftap() {
int ret;
if (mPid == 0) {
LOGE("Softap already stopped");
return 0;
}
#ifndef AR6002_WIFI
/* Original way */
#ifdef HAVE_HOSTAPD
LOGD("Stopping Softap service");
kill(mPid, SIGTERM);
waitpid(mPid, NULL, 0);
#endif
if (mSock < 0) {
LOGE("Softap stopap - failed to open socket");
return -1;
}
*mBuf = 0;
ret = setCommand(mIface, "AP_BSS_STOP");
#else
/* Stop hostapd service */
ret = wifi_stop_hostapd();
#endif
mPid = 0;
LOGD("Softap service stopped: %d", ret);
usleep(AP_BSS_STOP_DELAY);
return ret;
}
bool SoftapController::isSoftapStarted() {
return (mPid != 0 ? true : false);
}
int SoftapController::addParam(int pos, const char *cmd, const char *arg)
{
if (pos < 0)
return pos;
if ((unsigned)(pos + strlen(cmd) + strlen(arg) + 1) >= sizeof(mBuf)) {
LOGE("Command line is too big");
return -1;
}
pos += sprintf(&mBuf[pos], "%s=%s,", cmd, arg);
return pos;
}
/*
* Arguments:
* argv[2] - wlan interface
* argv[3] - softap interface
* argv[4] - SSID
* argv[5] - Security
* argv[6] - Key
* argv[7] - Channel
* argv[8] - Preamble
* argv[9] - Max SCB
*/
int SoftapController::setSoftap(int argc, char *argv[]) {
char psk_str[2*SHA256_DIGEST_LENGTH+1];
int ret = 0, i = 0, fd;
char *ssid, *iface;
if (mSock < 0) {
LOGE("Softap set - failed to open socket");
return -1;
}
if (argc < 4) {
LOGE("Softap set - missing arguments");
return -1;
}
strncpy(mIface, argv[3], sizeof(mIface));
iface = argv[2];
#ifndef AR6002_WIFI
/* Original way */
#ifdef HAVE_HOSTAPD
char *wbuf = NULL;
char *fbuf = NULL;
if (argc > 4) {
ssid = argv[4];
} else {
ssid = (char *)"AndroidAP";
}
asprintf(&wbuf, "interface=%s\ndriver=nl80211\nctrl_interface="
"/data/misc/wifi/hostapd\nssid=%s\nchannel=6\n", iface, ssid);
if (argc > 5) {
if (!strcmp(argv[5], "wpa-psk")) {
generatePsk(ssid, argv[6], psk_str);
asprintf(&fbuf, "%swpa=1\nwpa_pairwise=TKIP CCMP\nwpa_psk=%s\n", wbuf, psk_str);
} else if (!strcmp(argv[5], "wpa2-psk")) {
generatePsk(ssid, argv[6], psk_str);
asprintf(&fbuf, "%swpa=2\nrsn_pairwise=CCMP\nwpa_psk=%s\n", wbuf, psk_str);
} else if (!strcmp(argv[5], "open")) {
asprintf(&fbuf, "%s", wbuf);
}
} else {
asprintf(&fbuf, "%s", wbuf);
}
fd = open(HOSTAPD_CONF_FILE, O_CREAT | O_TRUNC | O_WRONLY, 0660);
if (fd < 0) {
LOGE("Cannot update \"%s\": %s", HOSTAPD_CONF_FILE, strerror(errno));
free(wbuf);
free(fbuf);
return -1;
}
if (write(fd, fbuf, strlen(fbuf)) < 0) {
LOGE("Cannot write to \"%s\": %s", HOSTAPD_CONF_FILE, strerror(errno));
ret = -1;
}
close(fd);
free(wbuf);
free(fbuf);
/* Note: apparently open can fail to set permissions correctly at times */
if (chmod(HOSTAPD_CONF_FILE, 0660) < 0) {
LOGE("Error changing permissions of %s to 0660: %s",
HOSTAPD_CONF_FILE, strerror(errno));
unlink(HOSTAPD_CONF_FILE);
return -1;
}
if (chown(HOSTAPD_CONF_FILE, AID_SYSTEM, AID_WIFI) < 0) {
LOGE("Error changing group ownership of %s to %d: %s",
HOSTAPD_CONF_FILE, AID_WIFI, strerror(errno));
unlink(HOSTAPD_CONF_FILE);
return -1;
}
#else
/* Create command line */
i = addParam(i, "ASCII_CMD", "AP_CFG");
if (argc > 4) {
ssid = argv[4];
} else {
ssid = (char *)"AndroidAP";
}
i = addParam(i, "SSID", ssid);
if (argc > 5) {
i = addParam(i, "SEC", argv[5]);
} else {
i = addParam(i, "SEC", "open");
}
if (argc > 6) {
generatePsk(ssid, argv[6], psk_str);
i = addParam(i, "KEY", psk_str);
} else {
i = addParam(i, "KEY", "12345678");
}
if (argc > 7) {
i = addParam(i, "CHANNEL", argv[7]);
} else {
i = addParam(i, "CHANNEL", "6");
}
if (argc > 8) {
i = addParam(i, "PREAMBLE", argv[8]);
} else {
i = addParam(i, "PREAMBLE", "0");
}
if (argc > 9) {
i = addParam(i, "MAX_SCB", argv[9]);
} else {
i = addParam(i, "MAX_SCB", "8");
}
if ((i < 0) || ((unsigned)(i + 4) >= sizeof(mBuf))) {
LOGE("Softap set - command is too big");
return i;
}
sprintf(&mBuf[i], "END");
/* system("iwpriv eth0 WL_AP_CFG ASCII_CMD=AP_CFG,SSID=\"AndroidAP\",SEC=\"open\",KEY=12345,CHANNEL=1,PREAMBLE=0,MAX_SCB=8,END"); */
ret = setCommand(iface, "AP_SET_CFG");
if (ret) {
LOGE("Softap set - failed: %d", ret);
}
else {
LOGD("Softap set - Ok");
usleep(AP_SET_CFG_DELAY);
}
#endif
#else
/* AR6002 specific way */
char *fbuf = NULL;
if (argc > 4) {
ssid = argv[4];
} else {
ssid = (char *)"AndroidAP";
}
fd = open(HOSTAPD_CONF_FILE, O_CREAT|O_WRONLY|O_TRUNC, 0660);
if (fd < 0) {
LOGE("Cannot create \"%s\": %s", HOSTAPD_CONF_FILE, strerror(errno));
return -1;
}
asprintf(&fbuf, "interface=" AR6002AP_IFNAME "\n");
if (write(fd, fbuf, strlen(fbuf)) < 0) goto wrerr;
free(fbuf);
asprintf(&fbuf, "ctrl_interface=%s\n" ,IFACE_DIR);
if (write(fd, fbuf, strlen(fbuf)) < 0) goto wrerr;
free(fbuf);
asprintf(&fbuf, "ssid=%s\n" ,ssid);
if (write(fd, fbuf, strlen(fbuf)) < 0) goto wrerr;
free(fbuf);
/* set open auth */
asprintf(&fbuf, "auth_algs=1\n");
if (write(fd, fbuf, strlen(fbuf)) < 0) goto wrerr;
free(fbuf);
asprintf(&fbuf, "max_num_sta=%d\n",WIFI_DEFAULT_MAX_STA);
if (write(fd, fbuf, strlen(fbuf)) < 0) goto wrerr;
free(fbuf);
asprintf(&fbuf, "beacon_int=%d\n" ,WIFI_DEFAULT_BI);
if (write(fd, fbuf, strlen(fbuf)) < 0) goto wrerr;
free(fbuf);
asprintf(&fbuf, "dtim_period=%d\n",WIFI_DEFAULT_DTIM);
if (write(fd, fbuf, strlen(fbuf)) < 0) goto wrerr;
free(fbuf);
if (argc > 5) {
if (!strcmp(argv[5], "wpa-psk")) {
generatePsk(ssid, argv[6], psk_str);
asprintf(&fbuf, "wpa=1\n");
if (write(fd, fbuf, strlen(fbuf)) < 0) goto wrerr;
free(fbuf);
asprintf(&fbuf, "wpa_key_mgmt=WPA-PSK\n");
if (write(fd, fbuf, strlen(fbuf)) < 0) goto wrerr;
free(fbuf);
asprintf(&fbuf, "wpa_pairwise=TKIP\n");
if (write(fd, fbuf, strlen(fbuf)) < 0) goto wrerr;
free(fbuf);
asprintf(&fbuf, "wpa_psk=%s\n", psk_str);