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