SSB-15430: AP restarts automatically during connection
[GitHub/MotorolaMobilityLLC/hardware-samsung_slsi-scsc_wifibt-wifi_hal.git] / wifi_hal.cpp
1 #include <errno.h>
2 #include <stdint.h>
3 #include <fcntl.h>
4 #include <sys/socket.h>
5 #include <netlink/genl/genl.h>
6 #include <netlink/genl/family.h>
7 #include <netlink/genl/ctrl.h>
8 #include <linux/rtnetlink.h>
9 #include <netpacket/packet.h>
10 #include <linux/filter.h>
11 #include <linux/errqueue.h>
12
13 #include <linux/pkt_sched.h>
14 #include <netlink/object-api.h>
15 #include <netlink/netlink.h>
16 #include <netlink/socket.h>
17 #include <netlink/attr.h>
18 #include <netlink/handlers.h>
19 #include <netlink/msg.h>
20
21 #include <dirent.h>
22 #include <net/if.h>
23
24 #include "sync.h"
25
26 #define LOG_TAG "WifiHAL"
27
28 #include <utils/Log.h>
29
30 #include "wifi_hal.h"
31 #include "common.h"
32 #include "cpp_bindings.h"
33
34
35 #define WIFI_HAL_CMD_SOCK_PORT 644
36 #define WIFI_HAL_EVENT_SOCK_PORT 645
37
38 #define FEATURE_SET 0
39 #define FEATURE_SET_MATRIX 1
40 #define ATTR_NODFS_VALUE 3
41
42 static void internal_event_handler(wifi_handle handle, int events);
43 static int internal_no_seq_check(nl_msg *msg, void *arg);
44 static int internal_valid_message_handler(nl_msg *msg, void *arg);
45 static int wifi_get_multicast_id(wifi_handle handle, const char *name, const char *group);
46 static int wifi_add_membership(wifi_handle handle, const char *group);
47 static wifi_error wifi_init_interfaces(wifi_handle handle);
48
49 typedef enum wifi_attr {
50 ANDR_WIFI_ATTRIBUTE_NUM_FEATURE_SET,
51 ANDR_WIFI_ATTRIBUTE_FEATURE_SET,
52 ANDR_WIFI_ATTRIBUTE_PNO_RANDOM_MAC_OUI
53 } wifi_attr_t;
54
55 /* Initialize/Cleanup */
56
57 void wifi_socket_set_local_port(struct nl_sock *sock, uint32_t port)
58 {
59 uint32_t pid = getpid() & 0x3FFFFF;
60 nl_socket_set_local_port(sock, pid + (port << 22));
61 }
62
63 static nl_sock * wifi_create_nl_socket(int port)
64 {
65 ALOGI("Creating socket");
66 struct nl_sock *sock = nl_socket_alloc();
67 if (sock == NULL) {
68 ALOGE("Could not create handle");
69 return NULL;
70 }
71
72 wifi_socket_set_local_port(sock, port);
73
74 struct sockaddr *addr = NULL;
75 // ALOGI("sizeof(sockaddr) = %d, sizeof(sockaddr_nl) = %d", sizeof(*addr), sizeof(*addr_nl));
76
77 ALOGI("Connecting socket");
78 if (nl_connect(sock, NETLINK_GENERIC)) {
79 ALOGE("Could not connect handle");
80 nl_socket_free(sock);
81 return NULL;
82 }
83
84 ALOGI("Making socket nonblocking");
85 /*
86 if (nl_socket_set_nonblocking(sock)) {
87 ALOGE("Could make socket non-blocking");
88 nl_socket_free(sock);
89 return NULL;
90 }
91 */
92
93 return sock;
94 }
95
96 /*initialize function pointer table with Broadcom HHAL API*/
97 wifi_error init_wifi_vendor_hal_func_table(wifi_hal_fn *fn)
98 {
99 if (fn == NULL) {
100 return WIFI_ERROR_UNKNOWN;
101 }
102 fn->wifi_initialize = wifi_initialize;
103 fn->wifi_cleanup = wifi_cleanup;
104 fn->wifi_event_loop = wifi_event_loop;
105 fn->wifi_get_supported_feature_set = wifi_get_supported_feature_set;
106 fn->wifi_get_concurrency_matrix = wifi_get_concurrency_matrix;
107 fn->wifi_set_scanning_mac_oui = wifi_set_scanning_mac_oui;
108 fn->wifi_get_ifaces = wifi_get_ifaces;
109 fn->wifi_get_iface_name = wifi_get_iface_name;
110 fn->wifi_start_gscan = wifi_start_gscan;
111 fn->wifi_stop_gscan = wifi_stop_gscan;
112 fn->wifi_get_cached_gscan_results = wifi_get_cached_gscan_results;
113 fn->wifi_set_bssid_hotlist = wifi_set_bssid_hotlist;
114 fn->wifi_reset_bssid_hotlist = wifi_reset_bssid_hotlist;
115 fn->wifi_set_significant_change_handler = wifi_set_significant_change_handler;
116 fn->wifi_reset_significant_change_handler = wifi_reset_significant_change_handler;
117 fn->wifi_get_gscan_capabilities = wifi_get_gscan_capabilities;
118 fn->wifi_get_link_stats = wifi_get_link_stats;
119 fn->wifi_get_valid_channels = wifi_get_valid_channels;
120 fn->wifi_rtt_range_request = wifi_rtt_range_request;
121 fn->wifi_rtt_range_cancel = wifi_rtt_range_cancel;
122 fn->wifi_get_rtt_capabilities = wifi_get_rtt_capabilities;
123 fn->wifi_set_nodfs_flag = wifi_set_nodfs_flag;
124 return WIFI_SUCCESS;
125 }
126
127 wifi_error wifi_initialize(wifi_handle *handle)
128 {
129 srand(getpid());
130
131 ALOGI("Initializing wifi");
132 hal_info *info = (hal_info *)malloc(sizeof(hal_info));
133 if (info == NULL) {
134 ALOGE("Could not allocate hal_info");
135 return WIFI_ERROR_UNKNOWN;
136 }
137
138 memset(info, 0, sizeof(*info));
139
140 ALOGI("Creating socket");
141 if (socketpair(AF_UNIX, SOCK_STREAM, 0, info->cleanup_socks) == -1) {
142 ALOGE("Could not create cleanup sockets");
143 free(info);
144 return WIFI_ERROR_UNKNOWN;
145 }
146
147 struct nl_sock *cmd_sock = wifi_create_nl_socket(WIFI_HAL_CMD_SOCK_PORT);
148 if (cmd_sock == NULL) {
149 ALOGE("Could not create handle");
150 free(info);
151 return WIFI_ERROR_UNKNOWN;
152 }
153
154 struct nl_sock *event_sock = wifi_create_nl_socket(WIFI_HAL_EVENT_SOCK_PORT);
155 if (event_sock == NULL) {
156 ALOGE("Could not create handle");
157 nl_socket_free(cmd_sock);
158 free(info);
159 return WIFI_ERROR_UNKNOWN;
160 }
161
162 struct nl_cb *cb = nl_socket_get_cb(event_sock);
163 if (cb == NULL) {
164 ALOGE("Could not create handle");
165 nl_socket_free(cmd_sock);
166 nl_socket_free(event_sock);
167 free(info);
168 return WIFI_ERROR_UNKNOWN;
169 }
170
171 // ALOGI("cb->refcnt = %d", cb->cb_refcnt);
172 nl_cb_set(cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM, internal_no_seq_check, info);
173 nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, internal_valid_message_handler, info);
174 nl_cb_put(cb);
175
176 info->cmd_sock = cmd_sock;
177 info->event_sock = event_sock;
178 info->clean_up = false;
179 info->in_event_loop = false;
180
181 info->event_cb = (cb_info *)malloc(sizeof(cb_info) * DEFAULT_EVENT_CB_SIZE);
182 info->alloc_event_cb = DEFAULT_EVENT_CB_SIZE;
183 info->num_event_cb = 0;
184
185 info->cmd = (cmd_info *)malloc(sizeof(cmd_info) * DEFAULT_CMD_SIZE);
186 info->alloc_cmd = DEFAULT_CMD_SIZE;
187 info->num_cmd = 0;
188
189 info->nl80211_family_id = genl_ctrl_resolve(cmd_sock, "nl80211");
190 if (info->nl80211_family_id < 0) {
191 ALOGE("Could not resolve nl80211 familty id");
192 nl_socket_free(cmd_sock);
193 nl_socket_free(event_sock);
194 free(info);
195 return WIFI_ERROR_UNKNOWN;
196 }
197
198 pthread_mutex_init(&info->cb_lock, NULL);
199
200 *handle = (wifi_handle) info;
201 ALOGD("wifi_initialize, handle = %p\n", handle);
202 ALOGD("wifi_initialize, *handle = %p\n", *handle);
203 ALOGD("wifi_initialize, info = %p\n", info);
204 ALOGD("wifi_initialize, *info = %pn", *info);
205 wifi_add_membership(*handle, "scan");
206 wifi_add_membership(*handle, "mlme");
207 wifi_add_membership(*handle, "regulatory");
208 wifi_add_membership(*handle, "vendor");
209
210 wifi_init_interfaces(*handle);
211 ALOGD("Found %d interfaces", info->num_interfaces);
212
213
214 ALOGI("Initialized Wifi HAL Successfully; vendor cmd = %d", NL80211_CMD_VENDOR);
215 return WIFI_SUCCESS;
216 }
217
218 static int wifi_add_membership(wifi_handle handle, const char *group)
219 {
220 hal_info *info = getHalInfo(handle);
221
222 int id = wifi_get_multicast_id(handle, "nl80211", group);
223 if (id < 0) {
224 ALOGE("Could not find group %s", group);
225 return id;
226 }
227
228 int ret = nl_socket_add_membership(info->event_sock, id);
229 if (ret < 0) {
230 ALOGE("Could not add membership to group %s", group);
231 }
232
233 ALOGI("Successfully added membership for group %s", group);
234 return ret;
235 }
236
237 static void internal_cleaned_up_handler(wifi_handle handle)
238 {
239 hal_info *info = getHalInfo(handle);
240 wifi_cleaned_up_handler cleaned_up_handler = info->cleaned_up_handler;
241
242 if (info->cmd_sock != 0) {
243 close(info->cleanup_socks[0]);
244 close(info->cleanup_socks[1]);
245 nl_socket_free(info->cmd_sock);
246 nl_socket_free(info->event_sock);
247 info->cmd_sock = NULL;
248 info->event_sock = NULL;
249 }
250
251 (*cleaned_up_handler)(handle);
252 pthread_mutex_destroy(&info->cb_lock);
253 free(info);
254
255 ALOGI("Internal cleanup completed");
256 }
257
258 void wifi_cleanup(wifi_handle handle, wifi_cleaned_up_handler handler)
259 {
260 hal_info *info = getHalInfo(handle);
261 char buf[64];
262
263 info->cleaned_up_handler = handler;
264 if (write(info->cleanup_socks[0], "Exit", 4) < 1) {
265 ALOGE("could not write to the cleanup socket");
266 } else {
267 // Listen to the response
268 // Hopefully we dont get errors or get hung up
269 // Not much can be done in that case, but assume that
270 // it has rx'ed the Exit message to exit the thread.
271 // As a fallback set the cleanup flag to TRUE
272 memset(buf, 0, sizeof(buf));
273 int result = read(info->cleanup_socks[0], buf, sizeof(buf));
274 ALOGE("%s: Read after POLL returned %d, error no = %d", __FUNCTION__, result, errno);
275 if (strncmp(buf, "Done", 4) == 0) {
276 ALOGE("Event processing terminated");
277 } else {
278 ALOGD("Rx'ed %s", buf);
279 }
280 }
281 info->clean_up = true;
282 pthread_mutex_lock(&info->cb_lock);
283
284 int bad_commands = 0;
285
286 for (int i = 0; i < info->num_event_cb; i++) {
287 cb_info *cbi = &(info->event_cb[i]);
288 WifiCommand *cmd = (WifiCommand *)cbi->cb_arg;
289 }
290
291 while (info->num_cmd > bad_commands) {
292 int num_cmd = info->num_cmd;
293 cmd_info *cmdi = &(info->cmd[bad_commands]);
294 WifiCommand *cmd = cmdi->cmd;
295 if (cmd != NULL) {
296 pthread_mutex_unlock(&info->cb_lock);
297 cmd->cancel();
298 pthread_mutex_lock(&info->cb_lock);
299 /* release reference added when command is saved */
300 cmd->releaseRef();
301 if (num_cmd == info->num_cmd) {
302 bad_commands++;
303 }
304 }
305 }
306
307 for (int i = 0; i < info->num_event_cb; i++) {
308 cb_info *cbi = &(info->event_cb[i]);
309 WifiCommand *cmd = (WifiCommand *)cbi->cb_arg;
310 ALOGE("Leaked command %p", cmd);
311 }
312 pthread_mutex_unlock(&info->cb_lock);
313 internal_cleaned_up_handler(handle);
314 }
315
316 static int internal_pollin_handler(wifi_handle handle)
317 {
318 hal_info *info = getHalInfo(handle);
319 ALOGI("even_loop info = %p", info);
320 struct nl_cb *cb = nl_socket_get_cb(info->event_sock);
321 int res = nl_recvmsgs(info->event_sock, cb);
322 ALOGD("nl_recvmsgs returned %d", res);
323 nl_cb_put(cb);
324 return res;
325 }
326
327 /* Run event handler */
328 void wifi_event_loop(wifi_handle handle)
329 {
330 hal_info *info = getHalInfo(handle);
331 ALOGI("even_loop info = %p", info);
332 ALOGI("even_loop info = %p", handle);
333 if (info->in_event_loop) {
334 return;
335 } else {
336 info->in_event_loop = true;
337 }
338
339 pollfd pfd[2];
340 memset(&pfd[0], 0, sizeof(pollfd) * 2);
341
342 pfd[0].fd = nl_socket_get_fd(info->event_sock);
343 pfd[0].events = POLLIN;
344 pfd[1].fd = info->cleanup_socks[1];
345 pfd[1].events = POLLIN;
346
347 char buf[2048];
348
349 do {
350 int timeout = -1; /* Infinite timeout */
351 pfd[0].revents = 0;
352 pfd[1].revents = 0;
353 int result = poll(pfd, 2, timeout);
354 if (result < 0) {
355 } else if (pfd[0].revents & POLLERR) {
356 ALOGE("POLL Error; error no = %d", errno);
357 int result2 = read(pfd[0].fd, buf, sizeof(buf));
358 ALOGE("Read after POLL returned %d, error no = %d", result2, errno);
359 } else if (pfd[0].revents & POLLHUP) {
360 ALOGE("Remote side hung up");
361 break;
362 } else if (pfd[0].revents & POLLIN) {
363 internal_pollin_handler(handle);
364 } else if (pfd[1].revents & POLLIN) {
365 memset(buf, 0, sizeof(buf));
366 int result2 = read(pfd[1].fd, buf, sizeof(buf));
367 ALOGE("%s: Read after POLL returned %d, error no = %d", __FUNCTION__, result2, errno);
368 if (strncmp(buf, "Exit", 4) == 0) {
369 ALOGD("Got a signal to exit!!!");
370 if (write(pfd[1].fd, "Done", 4) < 1) {
371 ALOGE("could not write to the cleanup socket");
372 }
373 break;
374 } else {
375 ALOGD("Rx'ed %s on the cleanup socket\n", buf);
376 }
377 } else {
378 ALOGE("Unknown event - %0x, %0x", pfd[0].revents, pfd[1].revents);
379 }
380 } while (!info->clean_up);
381 ALOGI("Exit %s", __FUNCTION__);
382 }
383
384 ///////////////////////////////////////////////////////////////////////////////////////
385
386 static int internal_no_seq_check(struct nl_msg *msg, void *arg)
387 {
388 return NL_OK;
389 }
390
391 static int internal_valid_message_handler(nl_msg *msg, void *arg)
392 {
393 wifi_handle handle = (wifi_handle)arg;
394 hal_info *info = getHalInfo(handle);
395 ALOGI("even_loop info = %p", handle);
396 ALOGD("internal_valid_message_handler, info = %p", info);
397
398 WifiEvent event(msg);
399 int res = event.parse();
400 if (res < 0) {
401 ALOGE("Failed to parse event: %d", res);
402 return NL_SKIP;
403 }
404
405 int cmd = event.get_cmd();
406 uint32_t vendor_id = 0;
407 int subcmd = 0;
408
409 if (cmd == NL80211_CMD_VENDOR) {
410 vendor_id = event.get_u32(NL80211_ATTR_VENDOR_ID);
411 subcmd = event.get_u32(NL80211_ATTR_VENDOR_SUBCMD);
412 ALOGI("event received %s, vendor_id = 0x%0x, subcmd = 0x%0x",
413 event.get_cmdString(), vendor_id, subcmd);
414 } else {
415 ALOGI("event received %s", event.get_cmdString());
416 }
417
418 //ALOGI("event received %s, vendor_id = 0x%0x", event.get_cmdString(), vendor_id);
419 //event.log();
420
421 bool dispatched = false;
422
423 pthread_mutex_lock(&info->cb_lock);
424
425 ALOGI("Number of events %d", info->num_event_cb);
426
427 for (int i = 0; i < info->num_event_cb; i++) {
428 if (cmd == info->event_cb[i].nl_cmd) {
429 if (cmd == NL80211_CMD_VENDOR
430 && ((vendor_id != info->event_cb[i].vendor_id)
431 || (subcmd != info->event_cb[i].vendor_subcmd)))
432 {
433 /* event for a different vendor, ignore it */
434 continue;
435 }
436
437 cb_info *cbi = &(info->event_cb[i]);
438 nl_recvmsg_msg_cb_t cb_func = cbi->cb_func;
439 void *cb_arg = cbi->cb_arg;
440 WifiCommand *cmd = (WifiCommand *)cbi->cb_arg;
441 if (cmd != NULL) {
442 cmd->addRef();
443 }
444
445 pthread_mutex_unlock(&info->cb_lock);
446 if (cb_func)
447 (*cb_func)(msg, cb_arg);
448 if (cmd != NULL) {
449 cmd->releaseRef();
450 }
451
452 return NL_OK;
453 }
454 }
455
456 pthread_mutex_unlock(&info->cb_lock);
457 return NL_OK;
458 }
459
460 ///////////////////////////////////////////////////////////////////////////////////////
461
462 class GetMulticastIdCommand : public WifiCommand
463 {
464 private:
465 const char *mName;
466 const char *mGroup;
467 int mId;
468 public:
469 GetMulticastIdCommand(wifi_handle handle, const char *name, const char *group)
470 : WifiCommand(handle, 0)
471 {
472 mName = name;
473 mGroup = group;
474 mId = -1;
475 }
476
477 int getId() {
478 return mId;
479 }
480
481 virtual int create() {
482 int nlctrlFamily = genl_ctrl_resolve(mInfo->cmd_sock, "nlctrl");
483 ALOGI("ctrl family = %d", nlctrlFamily);
484 int ret = mMsg.create(nlctrlFamily, CTRL_CMD_GETFAMILY, 0, 0);
485 if (ret < 0) {
486 return ret;
487 }
488 ret = mMsg.put_string(CTRL_ATTR_FAMILY_NAME, mName);
489 return ret;
490 }
491
492 virtual int handleResponse(WifiEvent& reply) {
493
494 ALOGE("handling reponse in %s", __func__);
495
496 struct nlattr **tb = reply.attributes();
497 struct genlmsghdr *gnlh = reply.header();
498 struct nlattr *mcgrp = NULL;
499 int i;
500
501 if (!tb[CTRL_ATTR_MCAST_GROUPS]) {
502 ALOGE("No multicast groups found");
503 return NL_SKIP;
504 } else {
505 ALOGE("Multicast groups attr size = %d", nla_len(tb[CTRL_ATTR_MCAST_GROUPS]));
506 }
507
508 for_each_attr(mcgrp, tb[CTRL_ATTR_MCAST_GROUPS], i) {
509
510 ALOGE("Processing group");
511 struct nlattr *tb2[CTRL_ATTR_MCAST_GRP_MAX + 1];
512 nla_parse(tb2, CTRL_ATTR_MCAST_GRP_MAX, (nlattr *)nla_data(mcgrp),
513 nla_len(mcgrp), NULL);
514 if (!tb2[CTRL_ATTR_MCAST_GRP_NAME] || !tb2[CTRL_ATTR_MCAST_GRP_ID]) {
515 continue;
516 }
517
518 char *grpName = (char *)nla_data(tb2[CTRL_ATTR_MCAST_GRP_NAME]);
519 int grpNameLen = nla_len(tb2[CTRL_ATTR_MCAST_GRP_NAME]);
520
521 ALOGE("Found group name %s", grpName);
522
523 if (strncmp(grpName, mGroup, grpNameLen) != 0)
524 continue;
525
526 mId = nla_get_u32(tb2[CTRL_ATTR_MCAST_GRP_ID]);
527 break;
528 }
529
530 return NL_SKIP;
531 }
532
533 };
534
535 class SetPnoMacAddrOuiCommand : public WifiCommand {
536
537 private:
538 byte *mOui;
539 feature_set *fset;
540 feature_set *feature_matrix;
541 int *fm_size;
542 int set_size_max;
543 public:
544 SetPnoMacAddrOuiCommand(wifi_interface_handle handle, oui scan_oui)
545 : WifiCommand(handle, 0)
546 {
547 mOui = scan_oui;
548 }
549
550 int createRequest(WifiRequest& request, int subcmd, byte *scan_oui) {
551 int result = request.create(GOOGLE_OUI, subcmd);
552 if (result < 0) {
553 return result;
554 }
555
556 nlattr *data = request.attr_start(NL80211_ATTR_VENDOR_DATA);
557 result = request.put(ANDR_WIFI_ATTRIBUTE_PNO_RANDOM_MAC_OUI, scan_oui, DOT11_OUI_LEN);
558 if (result < 0) {
559 return result;
560 }
561
562 request.attr_end(data);
563 return WIFI_SUCCESS;
564
565 }
566
567 int start() {
568 ALOGD("Sending mac address OUI");
569 WifiRequest request(familyId(), ifaceId());
570 int result = createRequest(request, SLSI_NL80211_VENDOR_SUBCMD_SET_GSCAN_OUI, mOui);
571 if (result != WIFI_SUCCESS) {
572 ALOGE("failed to create request; result = %d", result);
573 return result;
574 }
575
576 result = requestResponse(request);
577 if (result != WIFI_SUCCESS) {
578 ALOGE("failed to set scanning mac OUI; result = %d", result);
579 }
580
581 return result;
582 }
583 protected:
584 virtual int handleResponse(WifiEvent& reply) {
585 ALOGD("Request complete!");
586 /* Nothing to do on response! */
587 return NL_SKIP;
588 }
589 };
590
591 class SetNodfsCommand : public WifiCommand {
592
593 private:
594 u32 mNoDfs;
595 public:
596 SetNodfsCommand(wifi_interface_handle handle, u32 nodfs)
597 : WifiCommand(handle, 0) {
598 mNoDfs = nodfs;
599 }
600 virtual int create() {
601 int ret;
602
603 ret = mMsg.create(GOOGLE_OUI, SLSI_NL80211_VENDOR_SUBCMD_SET_NODFS);
604 if (ret < 0) {
605 ALOGE("Can't create message to send to driver - %d", ret);
606 return ret;
607 }
608
609 nlattr *data = mMsg.attr_start(NL80211_ATTR_VENDOR_DATA);
610 ret = mMsg.put_u32(ATTR_NODFS_VALUE, mNoDfs);
611 if (ret < 0) {
612 return ret;
613 }
614
615 mMsg.attr_end(data);
616 return WIFI_SUCCESS;
617 }
618 };
619
620 static int wifi_get_multicast_id(wifi_handle handle, const char *name, const char *group)
621 {
622 GetMulticastIdCommand cmd(handle, name, group);
623 int res = cmd.requestResponse();
624 if (res < 0)
625 return res;
626 else
627 return cmd.getId();
628 }
629
630 /////////////////////////////////////////////////////////////////////////
631
632 static bool is_wifi_interface(const char *name)
633 {
634 if (strncmp(name, "wlan", 4) != 0 && strncmp(name, "p2p", 3) != 0) {
635 /* not a wifi interface; ignore it */
636 return false;
637 } else {
638 return true;
639 }
640 }
641
642 static int get_interface(const char *name, interface_info *info)
643 {
644 strcpy(info->name, name);
645 info->id = if_nametoindex(name);
646 ALOGI("found an interface : %s, id = %d", name, info->id);
647 return WIFI_SUCCESS;
648 }
649
650 wifi_error wifi_init_interfaces(wifi_handle handle)
651 {
652 hal_info *info = (hal_info *)handle;
653 ALOGD("wifi_init_interfaces, info = %p", info);
654
655 struct dirent *de;
656
657 DIR *d = opendir("/sys/class/net");
658 if (d == 0)
659 return WIFI_ERROR_UNKNOWN;
660
661 int n = 0;
662 while ((de = readdir(d))) {
663 if (de->d_name[0] == '.')
664 continue;
665 if (is_wifi_interface(de->d_name) ) {
666 n++;
667 }
668 }
669
670 closedir(d);
671
672 d = opendir("/sys/class/net");
673 if (d == 0)
674 return WIFI_ERROR_UNKNOWN;
675
676 info->interfaces = (interface_info **)malloc(sizeof(interface_info *) * n);
677
678 int i = 0;
679 while ((de = readdir(d))) {
680 if (de->d_name[0] == '.')
681 continue;
682 if (is_wifi_interface(de->d_name)) {
683 interface_info *ifinfo = (interface_info *)malloc(sizeof(interface_info));
684 if (get_interface(de->d_name, ifinfo) != WIFI_SUCCESS) {
685 free(ifinfo);
686 continue;
687 }
688 ifinfo->handle = handle;
689 info->interfaces[i] = ifinfo;
690 i++;
691 }
692 }
693
694 closedir(d);
695
696 info->num_interfaces = n;
697 return WIFI_SUCCESS;
698 }
699
700 wifi_error wifi_get_ifaces(wifi_handle handle, int *num, wifi_interface_handle **interfaces)
701 {
702 hal_info *info = (hal_info *)handle;
703
704 *interfaces = (wifi_interface_handle *)info->interfaces;
705 *num = info->num_interfaces;
706
707 return WIFI_SUCCESS;
708 }
709
710 wifi_error wifi_get_iface_name(wifi_interface_handle handle, char *name, size_t size)
711 {
712 interface_info *info = (interface_info *)handle;
713 strcpy(name, info->name);
714 return WIFI_SUCCESS;
715 }
716
717 wifi_error wifi_get_supported_feature_set(wifi_interface_handle handle, feature_set *set)
718 {
719 return WIFI_ERROR_NOT_SUPPORTED;
720 }
721
722 wifi_error wifi_get_concurrency_matrix(wifi_interface_handle handle, int set_size_max,
723 feature_set set[], int *set_size)
724 {
725 return WIFI_ERROR_NOT_SUPPORTED;
726 }
727
728 wifi_error wifi_set_scanning_mac_oui(wifi_interface_handle handle, oui scan_oui)
729 {
730 SetPnoMacAddrOuiCommand command(handle, scan_oui);
731 return (wifi_error)command.start();
732
733 }
734
735 wifi_error wifi_set_nodfs_flag(wifi_interface_handle handle, u32 nodfs)
736 {
737 SetNodfsCommand command(handle, nodfs);
738 return (wifi_error) command.requestResponse();
739 }
740
741 /////////////////////////////////////////////////////////////////////////////