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