[7570] wlbt : Returning WIFI_ERROR_NOT_SUPPORTED for NAT KeepAlive APIs.
[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 #include "wifi_hal.h"
30 #include "common.h"
31 #include "cpp_bindings.h"
32
33
34 #define WIFI_HAL_CMD_SOCK_PORT 644
35 #define WIFI_HAL_EVENT_SOCK_PORT 645
36
37 #define FEATURE_SET 0
38 #define FEATURE_SET_MATRIX 1
39 #define ATTR_NODFS_VALUE 3
40 #define ATTR_COUNTRY_CODE 4
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 enum wifi_rssi_monitor_attr {
56 RSSI_MONITOR_ATTRIBUTE_MAX_RSSI,
57 RSSI_MONITOR_ATTRIBUTE_MIN_RSSI,
58 RSSI_MONITOR_ATTRIBUTE_START,
59 };
60
61 static wifi_error wifi_start_rssi_monitoring(wifi_request_id id, wifi_interface_handle
62 iface, s8 max_rssi, s8 min_rssi, wifi_rssi_event_handler eh);
63 static wifi_error wifi_stop_rssi_monitoring(wifi_request_id id, wifi_interface_handle iface);
64
65 /* Initialize/Cleanup */
66
67 void wifi_socket_set_local_port(struct nl_sock *sock, uint32_t port)
68 {
69 uint32_t pid = getpid() & 0x3FFFFF;
70 nl_socket_set_local_port(sock, pid + (port << 22));
71 }
72
73 static nl_sock * wifi_create_nl_socket(int port)
74 {
75 ALOGI("Creating socket");
76 struct nl_sock *sock = nl_socket_alloc();
77 if (sock == NULL) {
78 ALOGE("Could not create handle");
79 return NULL;
80 }
81
82 wifi_socket_set_local_port(sock, port);
83
84 ALOGI("Connecting socket");
85 if (nl_connect(sock, NETLINK_GENERIC)) {
86 ALOGE("Could not connect handle");
87 nl_socket_free(sock);
88 return NULL;
89 }
90
91 return sock;
92 }
93
94 /* Initialize HAL function pointer table */
95 wifi_error init_wifi_vendor_hal_func_table(wifi_hal_fn *fn)
96 {
97 if (fn == NULL) {
98 return WIFI_ERROR_UNKNOWN;
99 }
100 fn->wifi_initialize = wifi_initialize;
101 fn->wifi_cleanup = wifi_cleanup;
102 fn->wifi_event_loop = wifi_event_loop;
103 fn->wifi_get_supported_feature_set = wifi_get_supported_feature_set;
104 fn->wifi_get_concurrency_matrix = wifi_get_concurrency_matrix;
105 fn->wifi_set_scanning_mac_oui = wifi_set_scanning_mac_oui;
106 fn->wifi_get_ifaces = wifi_get_ifaces;
107 fn->wifi_get_iface_name = wifi_get_iface_name;
108 fn->wifi_start_gscan = wifi_start_gscan;
109 fn->wifi_stop_gscan = wifi_stop_gscan;
110 fn->wifi_get_cached_gscan_results = wifi_get_cached_gscan_results;
111 fn->wifi_set_bssid_hotlist = wifi_set_bssid_hotlist;
112 fn->wifi_reset_bssid_hotlist = wifi_reset_bssid_hotlist;
113 fn->wifi_set_significant_change_handler = wifi_set_significant_change_handler;
114 fn->wifi_reset_significant_change_handler = wifi_reset_significant_change_handler;
115 fn->wifi_get_gscan_capabilities = wifi_get_gscan_capabilities;
116 fn->wifi_get_valid_channels = wifi_get_valid_channels;
117 fn->wifi_rtt_range_request = wifi_rtt_range_request;
118 fn->wifi_rtt_range_cancel = wifi_rtt_range_cancel;
119 fn->wifi_get_rtt_capabilities = wifi_get_rtt_capabilities;
120 fn->wifi_set_nodfs_flag = wifi_set_nodfs_flag;
121 fn->wifi_start_sending_offloaded_packet = wifi_start_sending_offloaded_packet;
122 fn->wifi_stop_sending_offloaded_packet = wifi_stop_sending_offloaded_packet;
123 fn->wifi_set_epno_list = wifi_set_epno_list;
124 fn->wifi_reset_epno_list = wifi_reset_epno_list;
125 fn->wifi_set_passpoint_list = wifi_set_passpoint_list;
126 fn->wifi_reset_passpoint_list = wifi_reset_passpoint_list;
127 fn->wifi_set_bssid_blacklist = wifi_set_bssid_blacklist;
128 fn->wifi_start_rssi_monitoring = wifi_start_rssi_monitoring;
129 fn->wifi_stop_rssi_monitoring = wifi_stop_rssi_monitoring;
130 fn->wifi_set_link_stats = wifi_set_link_stats;
131 fn->wifi_get_link_stats = wifi_get_link_stats;
132 fn->wifi_clear_link_stats = wifi_clear_link_stats;
133 fn->wifi_set_country_code = wifi_set_country_code;
134
135 return WIFI_SUCCESS;
136 }
137
138 wifi_error wifi_initialize(wifi_handle *handle)
139 {
140 srand(getpid());
141
142 ALOGI("Initializing wifi");
143 hal_info *info = (hal_info *)malloc(sizeof(hal_info));
144 if (info == NULL) {
145 ALOGE("Could not allocate hal_info");
146 return WIFI_ERROR_UNKNOWN;
147 }
148
149 memset(info, 0, sizeof(*info));
150
151 ALOGI("Creating socket");
152 if (socketpair(AF_UNIX, SOCK_STREAM, 0, info->cleanup_socks) == -1) {
153 ALOGE("Could not create cleanup sockets");
154 free(info);
155 return WIFI_ERROR_UNKNOWN;
156 }
157
158 struct nl_sock *cmd_sock = wifi_create_nl_socket(WIFI_HAL_CMD_SOCK_PORT);
159 if (cmd_sock == NULL) {
160 ALOGE("Could not create handle");
161 free(info);
162 return WIFI_ERROR_UNKNOWN;
163 }
164
165 struct nl_sock *event_sock = wifi_create_nl_socket(WIFI_HAL_EVENT_SOCK_PORT);
166 if (event_sock == NULL) {
167 ALOGE("Could not create handle");
168 nl_socket_free(cmd_sock);
169 free(info);
170 return WIFI_ERROR_UNKNOWN;
171 }
172
173 struct nl_cb *cb = nl_socket_get_cb(event_sock);
174 if (cb == NULL) {
175 ALOGE("Could not create handle");
176 nl_socket_free(cmd_sock);
177 nl_socket_free(event_sock);
178 free(info);
179 return WIFI_ERROR_UNKNOWN;
180 }
181
182 // ALOGI("cb->refcnt = %d", cb->cb_refcnt);
183 nl_cb_set(cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM, internal_no_seq_check, info);
184 nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, internal_valid_message_handler, info);
185 nl_cb_put(cb);
186
187 info->cmd_sock = cmd_sock;
188 info->event_sock = event_sock;
189 info->clean_up = false;
190 info->in_event_loop = false;
191
192 info->event_cb = (cb_info *)malloc(sizeof(cb_info) * DEFAULT_EVENT_CB_SIZE);
193 info->alloc_event_cb = DEFAULT_EVENT_CB_SIZE;
194 info->num_event_cb = 0;
195
196 info->cmd = (cmd_info *)malloc(sizeof(cmd_info) * DEFAULT_CMD_SIZE);
197 info->alloc_cmd = DEFAULT_CMD_SIZE;
198 info->num_cmd = 0;
199
200 info->nl80211_family_id = genl_ctrl_resolve(cmd_sock, "nl80211");
201 if (info->nl80211_family_id < 0) {
202 ALOGE("Could not resolve nl80211 familty id");
203 nl_socket_free(cmd_sock);
204 nl_socket_free(event_sock);
205 free(info);
206 return WIFI_ERROR_UNKNOWN;
207 }
208
209 pthread_mutex_init(&info->cb_lock, NULL);
210
211 *handle = (wifi_handle) info;
212 ALOGD("wifi_initialize, handle = %p\n", handle);
213 ALOGD("wifi_initialize, *handle = %p\n", *handle);
214 ALOGD("wifi_initialize, info = %p\n", info);
215 ALOGD("wifi_initialize, *info = %pn", *info);
216 wifi_add_membership(*handle, "scan");
217 wifi_add_membership(*handle, "mlme");
218 wifi_add_membership(*handle, "regulatory");
219 wifi_add_membership(*handle, "vendor");
220
221 wifi_init_interfaces(*handle);
222 ALOGD("Found %d interfaces", info->num_interfaces);
223
224
225 ALOGI("Initialized Wifi HAL Successfully; vendor cmd = %d", NL80211_CMD_VENDOR);
226 return WIFI_SUCCESS;
227 }
228
229 static int wifi_add_membership(wifi_handle handle, const char *group)
230 {
231 hal_info *info = getHalInfo(handle);
232
233 int id = wifi_get_multicast_id(handle, "nl80211", group);
234 if (id < 0) {
235 ALOGE("Could not find group %s", group);
236 return id;
237 }
238
239 int ret = nl_socket_add_membership(info->event_sock, id);
240 if (ret < 0) {
241 ALOGE("Could not add membership to group %s", group);
242 }
243
244 ALOGI("Successfully added membership for group %s", group);
245 return ret;
246 }
247
248 static void internal_cleaned_up_handler(wifi_handle handle)
249 {
250 hal_info *info = getHalInfo(handle);
251 wifi_cleaned_up_handler cleaned_up_handler = info->cleaned_up_handler;
252
253 if (info->cmd_sock != 0) {
254 close(info->cleanup_socks[0]);
255 close(info->cleanup_socks[1]);
256 nl_socket_free(info->cmd_sock);
257 nl_socket_free(info->event_sock);
258 info->cmd_sock = NULL;
259 info->event_sock = NULL;
260 }
261
262 (*cleaned_up_handler)(handle);
263 pthread_mutex_destroy(&info->cb_lock);
264 free(info);
265
266 ALOGI("Internal cleanup completed");
267 }
268
269 void wifi_cleanup(wifi_handle handle, wifi_cleaned_up_handler handler)
270 {
271 hal_info *info = getHalInfo(handle);
272 char buf[64];
273
274 info->cleaned_up_handler = handler;
275 if (write(info->cleanup_socks[0], "Exit", 4) < 1) {
276 ALOGE("could not write to the cleanup socket");
277 } else {
278 // Listen to the response
279 // Hopefully we dont get errors or get hung up
280 // Not much can be done in that case, but assume that
281 // it has rx'ed the Exit message to exit the thread.
282 // As a fallback set the cleanup flag to TRUE
283 memset(buf, 0, sizeof(buf));
284 int result = read(info->cleanup_socks[0], buf, sizeof(buf));
285 ALOGE("%s: Read after POLL returned %d, error no = %d", __FUNCTION__, result, errno);
286 if (strncmp(buf, "Done", 4) == 0) {
287 ALOGE("Event processing terminated");
288 } else {
289 ALOGD("Rx'ed %s", buf);
290 }
291 }
292 info->clean_up = true;
293 pthread_mutex_lock(&info->cb_lock);
294
295 int bad_commands = 0;
296
297 while (info->num_cmd > bad_commands) {
298 int num_cmd = info->num_cmd;
299 cmd_info *cmdi = &(info->cmd[bad_commands]);
300 WifiCommand *cmd = cmdi->cmd;
301 if (cmd != NULL) {
302 pthread_mutex_unlock(&info->cb_lock);
303 cmd->cancel();
304 pthread_mutex_lock(&info->cb_lock);
305 /* release reference added when command is saved */
306 cmd->releaseRef();
307 if (num_cmd == info->num_cmd) {
308 bad_commands++;
309 }
310 }
311 }
312
313 for (int i = 0; i < info->num_event_cb; i++) {
314 cb_info *cbi = &(info->event_cb[i]);
315 WifiCommand *cmd = (WifiCommand *)cbi->cb_arg;
316 ALOGE("Leaked command %p", cmd);
317 }
318 pthread_mutex_unlock(&info->cb_lock);
319 internal_cleaned_up_handler(handle);
320 }
321
322 static int internal_pollin_handler(wifi_handle handle)
323 {
324 hal_info *info = getHalInfo(handle);
325 ALOGI("even_loop info = %p", info);
326 struct nl_cb *cb = nl_socket_get_cb(info->event_sock);
327 int res = nl_recvmsgs(info->event_sock, cb);
328 ALOGD("nl_recvmsgs returned %d", res);
329 nl_cb_put(cb);
330 return res;
331 }
332
333 /* Run event handler */
334 void wifi_event_loop(wifi_handle handle)
335 {
336 hal_info *info = getHalInfo(handle);
337 ALOGI("even_loop info = %p", info);
338 ALOGI("even_loop info = %p", handle);
339 if (info->in_event_loop) {
340 return;
341 } else {
342 info->in_event_loop = true;
343 }
344
345 pollfd pfd[2];
346 memset(&pfd[0], 0, sizeof(pollfd) * 2);
347
348 pfd[0].fd = nl_socket_get_fd(info->event_sock);
349 pfd[0].events = POLLIN;
350 pfd[1].fd = info->cleanup_socks[1];
351 pfd[1].events = POLLIN;
352
353 char buf[2048];
354
355 do {
356 int timeout = -1; /* Infinite timeout */
357 pfd[0].revents = 0;
358 pfd[1].revents = 0;
359 int result = poll(pfd, 2, timeout);
360 if (result < 0) {
361 } else if (pfd[0].revents & POLLERR) {
362 ALOGE("POLL Error; error no = %d", errno);
363 int result2 = read(pfd[0].fd, buf, sizeof(buf));
364 ALOGE("Read after POLL returned %d, error no = %d", result2, errno);
365 } else if (pfd[0].revents & POLLHUP) {
366 ALOGE("Remote side hung up");
367 break;
368 } else if (pfd[0].revents & POLLIN) {
369 internal_pollin_handler(handle);
370 } else if (pfd[1].revents & POLLIN) {
371 memset(buf, 0, sizeof(buf));
372 int result2 = read(pfd[1].fd, buf, sizeof(buf));
373 ALOGE("%s: Read after POLL returned %d, error no = %d", __FUNCTION__, result2, errno);
374 if (strncmp(buf, "Exit", 4) == 0) {
375 ALOGD("Got a signal to exit!!!");
376 if (write(pfd[1].fd, "Done", 4) < 1) {
377 ALOGE("could not write to the cleanup socket");
378 }
379 break;
380 } else {
381 ALOGD("Rx'ed %s on the cleanup socket\n", buf);
382 }
383 } else {
384 ALOGE("Unknown event - %0x, %0x", pfd[0].revents, pfd[1].revents);
385 }
386 } while (!info->clean_up);
387 ALOGI("Exit %s", __FUNCTION__);
388 }
389
390 ///////////////////////////////////////////////////////////////////////////////////////
391
392 static int internal_no_seq_check(struct nl_msg *msg, void *arg)
393 {
394 return NL_OK;
395 }
396
397 static int internal_valid_message_handler(nl_msg *msg, void *arg)
398 {
399 wifi_handle handle = (wifi_handle)arg;
400 hal_info *info = getHalInfo(handle);
401 ALOGI("even_loop info = %p", handle);
402 ALOGD("internal_valid_message_handler, info = %p", info);
403
404 WifiEvent event(msg);
405 int res = event.parse();
406 if (res < 0) {
407 ALOGE("Failed to parse event: %d", res);
408 return NL_SKIP;
409 }
410
411 int cmd = event.get_cmd();
412 uint32_t vendor_id = 0;
413 int subcmd = 0;
414
415 if (cmd == NL80211_CMD_VENDOR) {
416 vendor_id = event.get_u32(NL80211_ATTR_VENDOR_ID);
417 subcmd = event.get_u32(NL80211_ATTR_VENDOR_SUBCMD);
418 ALOGI("event received %s, vendor_id = 0x%0x, subcmd = 0x%0x",
419 event.get_cmdString(), vendor_id, subcmd);
420 } else {
421 ALOGI("event received %s", event.get_cmdString());
422 }
423
424 //ALOGI("event received %s, vendor_id = 0x%0x", event.get_cmdString(), vendor_id);
425 //event.log();
426
427 pthread_mutex_lock(&info->cb_lock);
428
429 ALOGI("Number of events %d", info->num_event_cb);
430
431 for (int i = 0; i < info->num_event_cb; i++) {
432 if (cmd == info->event_cb[i].nl_cmd) {
433 if (cmd == NL80211_CMD_VENDOR
434 && ((vendor_id != info->event_cb[i].vendor_id)
435 || (subcmd != info->event_cb[i].vendor_subcmd)))
436 {
437 /* event for a different vendor, ignore it */
438 continue;
439 }
440
441 cb_info *cbi = &(info->event_cb[i]);
442 nl_recvmsg_msg_cb_t cb_func = cbi->cb_func;
443 void *cb_arg = cbi->cb_arg;
444 WifiCommand *cmd = (WifiCommand *)cbi->cb_arg;
445 if (cmd != NULL) {
446 cmd->addRef();
447 }
448
449 pthread_mutex_unlock(&info->cb_lock);
450 if (cb_func)
451 (*cb_func)(msg, cb_arg);
452 if (cmd != NULL) {
453 cmd->releaseRef();
454 }
455
456 return NL_OK;
457 }
458 }
459
460 pthread_mutex_unlock(&info->cb_lock);
461 return NL_OK;
462 }
463
464 ///////////////////////////////////////////////////////////////////////////////////////
465
466 class GetMulticastIdCommand : public WifiCommand
467 {
468 private:
469 const char *mName;
470 const char *mGroup;
471 int mId;
472 public:
473 GetMulticastIdCommand(wifi_handle handle, const char *name, const char *group)
474 : WifiCommand(handle, 0)
475 {
476 mName = name;
477 mGroup = group;
478 mId = -1;
479 }
480
481 int getId() {
482 return mId;
483 }
484
485 virtual int create() {
486 int nlctrlFamily = genl_ctrl_resolve(mInfo->cmd_sock, "nlctrl");
487 ALOGI("ctrl family = %d", nlctrlFamily);
488 int ret = mMsg.create(nlctrlFamily, CTRL_CMD_GETFAMILY, 0, 0);
489 if (ret < 0) {
490 return ret;
491 }
492 ret = mMsg.put_string(CTRL_ATTR_FAMILY_NAME, mName);
493 return ret;
494 }
495
496 virtual int handleResponse(WifiEvent& reply) {
497
498 ALOGE("handling reponse in %s", __func__);
499
500 struct nlattr **tb = reply.attributes();
501 struct nlattr *mcgrp = NULL;
502 int i;
503
504 if (!tb[CTRL_ATTR_MCAST_GROUPS]) {
505 ALOGE("No multicast groups found");
506 return NL_SKIP;
507 } else {
508 ALOGE("Multicast groups attr size = %d", nla_len(tb[CTRL_ATTR_MCAST_GROUPS]));
509 }
510
511 for_each_attr(mcgrp, tb[CTRL_ATTR_MCAST_GROUPS], i) {
512
513 ALOGE("Processing group");
514 struct nlattr *tb2[CTRL_ATTR_MCAST_GRP_MAX + 1];
515 nla_parse(tb2, CTRL_ATTR_MCAST_GRP_MAX, (nlattr *)nla_data(mcgrp),
516 nla_len(mcgrp), NULL);
517 if (!tb2[CTRL_ATTR_MCAST_GRP_NAME] || !tb2[CTRL_ATTR_MCAST_GRP_ID]) {
518 continue;
519 }
520
521 char *grpName = (char *)nla_data(tb2[CTRL_ATTR_MCAST_GRP_NAME]);
522 int grpNameLen = nla_len(tb2[CTRL_ATTR_MCAST_GRP_NAME]);
523
524 ALOGE("Found group name %s", grpName);
525
526 if (strncmp(grpName, mGroup, grpNameLen) != 0)
527 continue;
528
529 mId = nla_get_u32(tb2[CTRL_ATTR_MCAST_GRP_ID]);
530 break;
531 }
532
533 return NL_SKIP;
534 }
535
536 };
537
538 class SetPnoMacAddrOuiCommand : public WifiCommand {
539
540 private:
541 byte *mOui;
542 feature_set *fset;
543 feature_set *feature_matrix;
544 int *fm_size;
545 int set_size_max;
546 public:
547 SetPnoMacAddrOuiCommand(wifi_interface_handle handle, oui scan_oui)
548 : WifiCommand(handle, 0)
549 {
550 mOui = scan_oui;
551 }
552
553 int createRequest(WifiRequest& request, int subcmd, byte *scan_oui) {
554 int result = request.create(GOOGLE_OUI, subcmd);
555 if (result < 0) {
556 return result;
557 }
558
559 nlattr *data = request.attr_start(NL80211_ATTR_VENDOR_DATA);
560 result = request.put(ANDR_WIFI_ATTRIBUTE_PNO_RANDOM_MAC_OUI, scan_oui, DOT11_OUI_LEN);
561 if (result < 0) {
562 return result;
563 }
564
565 request.attr_end(data);
566 return WIFI_SUCCESS;
567
568 }
569
570 int start() {
571 ALOGD("Sending mac address OUI");
572 WifiRequest request(familyId(), ifaceId());
573 int result = createRequest(request, SLSI_NL80211_VENDOR_SUBCMD_SET_GSCAN_OUI, mOui);
574 if (result != WIFI_SUCCESS) {
575 ALOGE("failed to create request; result = %d", result);
576 return result;
577 }
578
579 result = requestResponse(request);
580 if (result != WIFI_SUCCESS) {
581 ALOGE("failed to set scanning mac OUI; result = %d", result);
582 }
583
584 return result;
585 }
586 protected:
587 virtual int handleResponse(WifiEvent& reply) {
588 ALOGD("Request complete!");
589 /* Nothing to do on response! */
590 return NL_SKIP;
591 }
592 };
593
594 class SetNodfsCommand : public WifiCommand {
595
596 private:
597 u32 mNoDfs;
598 public:
599 SetNodfsCommand(wifi_interface_handle handle, u32 nodfs)
600 : WifiCommand(handle, 0) {
601 mNoDfs = nodfs;
602 }
603 virtual int create() {
604 int ret;
605
606 ret = mMsg.create(GOOGLE_OUI, SLSI_NL80211_VENDOR_SUBCMD_SET_NODFS);
607 if (ret < 0) {
608 ALOGE("Can't create message to send to driver - %d", ret);
609 return ret;
610 }
611
612 nlattr *data = mMsg.attr_start(NL80211_ATTR_VENDOR_DATA);
613 ret = mMsg.put_u32(ATTR_NODFS_VALUE, mNoDfs);
614 if (ret < 0) {
615 return ret;
616 }
617
618 mMsg.attr_end(data);
619 return WIFI_SUCCESS;
620 }
621 };
622
623 class SetRSSIMonitorCommand : public WifiCommand {
624 private:
625 s8 mMax_rssi;
626 s8 mMin_rssi;
627 wifi_rssi_event_handler mHandler;
628 public:
629 SetRSSIMonitorCommand(wifi_request_id id, wifi_interface_handle handle,
630 s8 max_rssi, s8 min_rssi, wifi_rssi_event_handler eh)
631 : WifiCommand(handle, id), mMax_rssi(max_rssi), mMin_rssi
632 (min_rssi), mHandler(eh)
633 {
634 }
635 int createRequest(WifiRequest& request, int enable) {
636 int result = request.create(GOOGLE_OUI, SLSI_NL80211_VENDOR_SUBCMD_SET_RSSI_MONITOR);
637 if (result < 0) {
638 return result;
639 }
640
641 nlattr *data = request.attr_start(NL80211_ATTR_VENDOR_DATA);
642 result = request.put_u8(RSSI_MONITOR_ATTRIBUTE_MAX_RSSI, (enable ? mMax_rssi: 0));
643 if (result < 0) {
644 return result;
645 }
646 ALOGD("create request");
647 result = request.put_u8(RSSI_MONITOR_ATTRIBUTE_MIN_RSSI, (enable? mMin_rssi: 0));
648 if (result < 0) {
649 return result;
650 }
651 result = request.put_u8(RSSI_MONITOR_ATTRIBUTE_START, enable);
652 if (result < 0) {
653 return result;
654 }
655 request.attr_end(data);
656 return result;
657 }
658
659 int start() {
660 WifiRequest request(familyId(), ifaceId());
661 int result = createRequest(request, 1);
662 if (result < 0) {
663 return result;
664 }
665 result = requestResponse(request);
666 if (result < 0) {
667 ALOGI("Failed to set RSSI Monitor, result = %d", result);
668 return result;
669 }
670 ALOGI("Successfully set RSSI monitoring");
671 registerVendorHandler(GOOGLE_OUI, WIFI_RSSI_REPORT_EVENT);
672
673
674 if (result < 0) {
675 unregisterVendorHandler(GOOGLE_OUI, WIFI_RSSI_REPORT_EVENT);
676 return result;
677 }
678 ALOGI("Done!");
679 return result;
680 }
681
682 virtual int cancel() {
683
684 WifiRequest request(familyId(), ifaceId());
685 int result = createRequest(request, 0);
686 if (result != WIFI_SUCCESS) {
687 ALOGE("failed to create request; result = %d", result);
688 } else {
689 result = requestResponse(request);
690 if (result != WIFI_SUCCESS) {
691 ALOGE("failed to stop RSSI monitoring = %d", result);
692 }
693 }
694 unregisterVendorHandler(GOOGLE_OUI, WIFI_RSSI_REPORT_EVENT);
695 return WIFI_SUCCESS;
696 }
697
698 virtual int handleResponse(WifiEvent& reply) {
699 /* Nothing to do on response! */
700 return NL_SKIP;
701 }
702
703 virtual int handleEvent(WifiEvent& event) {
704 ALOGI("Got a RSSI monitor event");
705
706 nlattr *vendor_data = event.get_attribute(NL80211_ATTR_VENDOR_DATA);
707 int len = event.get_vendor_data_len();
708
709 if (vendor_data == NULL || len == 0) {
710 ALOGI("RSSI monitor: No data");
711 return NL_SKIP;
712 }
713
714 typedef struct {
715 s8 cur_rssi;
716 mac_addr BSSID;
717 } rssi_monitor_evt;
718
719 rssi_monitor_evt *data = (rssi_monitor_evt *)event.get_vendor_data();
720
721 if (*mHandler.on_rssi_threshold_breached) {
722 (*mHandler.on_rssi_threshold_breached)(id(), data->BSSID, data->cur_rssi);
723 } else {
724 ALOGW("No RSSI monitor handler registered");
725 }
726
727 return NL_SKIP;
728 }
729
730 };
731
732 class SetCountryCodeCommand : public WifiCommand {
733 private:
734 const char *mCountryCode;
735 public:
736 SetCountryCodeCommand(wifi_interface_handle handle, const char *country_code)
737 : WifiCommand(handle, 0) {
738 mCountryCode = country_code;
739 }
740 virtual int create() {
741 int ret;
742
743 ret = mMsg.create(GOOGLE_OUI, SLSI_NL80211_VENDOR_SUBCMD_SET_COUNTRY_CODE);
744 if (ret < 0) {
745 ALOGE("Can't create message to send to driver - %d", ret);
746 return ret;
747 }
748
749 nlattr *data = mMsg.attr_start(NL80211_ATTR_VENDOR_DATA);
750 ret = mMsg.put_string(ATTR_COUNTRY_CODE, mCountryCode);
751 if (ret < 0) {
752 return ret;
753 }
754
755 mMsg.attr_end(data);
756 return WIFI_SUCCESS;
757
758 }
759 };
760
761 class GetFeatureSetCommand : public WifiCommand {
762
763 private:
764 int feature_type;
765 feature_set *fset;
766 int *fm_size;
767 int set_size_max;
768 public:
769 GetFeatureSetCommand(wifi_interface_handle handle, feature_set *set)
770 : WifiCommand(handle, 0)
771 {
772 fset = set;
773 }
774
775 virtual int create() {
776 int ret;
777
778 ret = mMsg.create(GOOGLE_OUI, SLSI_NL80211_VENDOR_SUBCMD_GET_FEATURE_SET);
779 if (ret < 0) {
780 ALOGE("create failed - %d", ret);
781 }
782
783 return ret;
784 }
785
786 protected:
787 virtual int handleResponse(WifiEvent& reply) {
788
789 int id = reply.get_vendor_id();
790 int subcmd = reply.get_vendor_subcmd();
791
792 if (reply.get_cmd() != NL80211_CMD_VENDOR) {
793 ALOGD("Ignore reply; cmd = %d", reply.get_cmd());
794 return NL_SKIP;
795 }
796
797 nlattr *vendor_data = reply.get_attribute(NL80211_ATTR_VENDOR_DATA);
798 int len = reply.get_vendor_data_len();
799
800 if (vendor_data == NULL || len == 0) {
801 ALOGE("vendor data in GetFeatureSetCommand missing!!");
802 return NL_SKIP;
803 }
804
805 void *data = reply.get_vendor_data();
806 if(!fset) {
807 ALOGE("feature_set Pointer not set");
808 return NL_SKIP;
809 }
810 memcpy(fset, data, min(len, (int) sizeof(*fset)));
811 return NL_OK;
812 }
813
814 };
815
816 static int wifi_get_multicast_id(wifi_handle handle, const char *name, const char *group)
817 {
818 GetMulticastIdCommand cmd(handle, name, group);
819 int res = cmd.requestResponse();
820 if (res < 0)
821 return res;
822 else
823 return cmd.getId();
824 }
825
826 /////////////////////////////////////////////////////////////////////////
827
828 static bool is_wifi_interface(const char *name)
829 {
830 if (strncmp(name, "wlan", 4) != 0 && strncmp(name, "p2p", 3) != 0) {
831 /* not a wifi interface; ignore it */
832 return false;
833 } else {
834 return true;
835 }
836 }
837
838 static int get_interface(const char *name, interface_info *info)
839 {
840 strcpy(info->name, name);
841 info->id = if_nametoindex(name);
842 ALOGI("found an interface : %s, id = %d", name, info->id);
843 return WIFI_SUCCESS;
844 }
845
846 wifi_error wifi_init_interfaces(wifi_handle handle)
847 {
848 hal_info *info = (hal_info *)handle;
849 ALOGD("wifi_init_interfaces, info = %p", info);
850
851 struct dirent *de;
852
853 DIR *d = opendir("/sys/class/net");
854 if (d == 0)
855 return WIFI_ERROR_UNKNOWN;
856
857 int n = 0;
858 while ((de = readdir(d))) {
859 if (de->d_name[0] == '.')
860 continue;
861 if (is_wifi_interface(de->d_name) ) {
862 n++;
863 }
864 }
865
866 closedir(d);
867
868 d = opendir("/sys/class/net");
869 if (d == 0)
870 return WIFI_ERROR_UNKNOWN;
871
872 info->interfaces = (interface_info **)malloc(sizeof(interface_info *) * n);
873
874 int i = 0;
875 while ((de = readdir(d))) {
876 if (de->d_name[0] == '.')
877 continue;
878 if (is_wifi_interface(de->d_name)) {
879 interface_info *ifinfo = (interface_info *)malloc(sizeof(interface_info));
880 if (get_interface(de->d_name, ifinfo) != WIFI_SUCCESS) {
881 free(ifinfo);
882 continue;
883 }
884 ifinfo->handle = handle;
885 info->interfaces[i] = ifinfo;
886 i++;
887 }
888 }
889
890 closedir(d);
891
892 info->num_interfaces = n;
893 return WIFI_SUCCESS;
894 }
895
896 wifi_error wifi_get_ifaces(wifi_handle handle, int *num, wifi_interface_handle **interfaces)
897 {
898 hal_info *info = (hal_info *)handle;
899
900 *interfaces = (wifi_interface_handle *)info->interfaces;
901 *num = info->num_interfaces;
902
903 return WIFI_SUCCESS;
904 }
905
906 wifi_error wifi_get_iface_name(wifi_interface_handle handle, char *name, size_t size)
907 {
908 interface_info *info = (interface_info *)handle;
909 strcpy(name, info->name);
910 return WIFI_SUCCESS;
911 }
912
913 wifi_error wifi_get_concurrency_matrix(wifi_interface_handle handle, int set_size_max,
914 feature_set set[], int *set_size)
915 {
916 return WIFI_ERROR_NOT_SUPPORTED;
917 }
918
919 wifi_error wifi_set_scanning_mac_oui(wifi_interface_handle handle, oui scan_oui)
920 {
921 SetPnoMacAddrOuiCommand command(handle, scan_oui);
922 return (wifi_error)command.start();
923
924 }
925
926 wifi_error wifi_set_nodfs_flag(wifi_interface_handle handle, u32 nodfs)
927 {
928 SetNodfsCommand command(handle, nodfs);
929 return (wifi_error) command.requestResponse();
930 }
931
932 static wifi_error wifi_start_rssi_monitoring(wifi_request_id id, wifi_interface_handle
933 iface, s8 max_rssi, s8 min_rssi, wifi_rssi_event_handler eh)
934 {
935 ALOGD("Start RSSI monitor %d", id);
936 wifi_handle handle = getWifiHandle(iface);
937 SetRSSIMonitorCommand *cmd = new SetRSSIMonitorCommand(id, iface, max_rssi, min_rssi, eh);
938 wifi_register_cmd(handle, id, cmd);
939
940 wifi_error result = (wifi_error)cmd->start();
941 if (result != WIFI_SUCCESS) {
942 wifi_unregister_cmd(handle, id);
943 }
944 return result;
945 }
946
947
948 static wifi_error wifi_stop_rssi_monitoring(wifi_request_id id, wifi_interface_handle iface)
949 {
950 ALOGD("Stopping RSSI monitor");
951
952 if(id == -1) {
953 wifi_rssi_event_handler handler;
954 wifi_handle handle = getWifiHandle(iface);
955 memset(&handler, 0, sizeof(handler));
956 SetRSSIMonitorCommand *cmd = new SetRSSIMonitorCommand(id, iface,
957 0, 0, handler);
958 cmd->cancel();
959 cmd->releaseRef();
960 return WIFI_SUCCESS;
961 }
962 return wifi_cancel_cmd(id, iface);
963 }
964
965 wifi_error wifi_get_supported_feature_set(wifi_interface_handle handle, feature_set *set)
966 {
967 GetFeatureSetCommand command(handle, set);
968 return (wifi_error) command.requestResponse();
969 }
970
971 wifi_error wifi_set_country_code(wifi_interface_handle handle, const char *country_code)
972 {
973 SetCountryCodeCommand command(handle, country_code);
974 return (wifi_error) command.requestResponse();
975 }
976
977 /////////////////////////////////////////////////////////////////////////////