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