[7872] [7570] [7885] Reduce wifi-hal logGing
[GitHub/LineageOS/android_hardware_samsung_slsi_scsc_wifibt_wifi_hal.git] / wifi_hal.cpp
CommitLineData
7753f181
DD
1#include <errno.h>
2#include <stdint.h>
c0b490a2 3#include <string.h>
7753f181
DD
4#include <fcntl.h>
5#include <sys/socket.h>
6#include <netlink/genl/genl.h>
7#include <netlink/genl/family.h>
8#include <netlink/genl/ctrl.h>
9#include <linux/rtnetlink.h>
10#include <netpacket/packet.h>
11#include <linux/filter.h>
12#include <linux/errqueue.h>
13
14#include <linux/pkt_sched.h>
15#include <netlink/object-api.h>
16#include <netlink/netlink.h>
17#include <netlink/socket.h>
18#include <netlink/attr.h>
19#include <netlink/handlers.h>
20#include <netlink/msg.h>
21
22#include <dirent.h>
23#include <net/if.h>
24
25#include "sync.h"
26
27#define LOG_TAG "WifiHAL"
28
29#include <utils/Log.h>
7753f181
DD
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
1fdbd4c1 41#define ATTR_COUNTRY_CODE 4
7753f181
DD
42
43static void internal_event_handler(wifi_handle handle, int events);
44static int internal_no_seq_check(nl_msg *msg, void *arg);
45static int internal_valid_message_handler(nl_msg *msg, void *arg);
46static int wifi_get_multicast_id(wifi_handle handle, const char *name, const char *group);
47static int wifi_add_membership(wifi_handle handle, const char *group);
48static wifi_error wifi_init_interfaces(wifi_handle handle);
49
50typedef enum wifi_attr {
51 ANDR_WIFI_ATTRIBUTE_NUM_FEATURE_SET,
52 ANDR_WIFI_ATTRIBUTE_FEATURE_SET,
53 ANDR_WIFI_ATTRIBUTE_PNO_RANDOM_MAC_OUI
54} wifi_attr_t;
55
f425b4a8
PG
56enum wifi_rssi_monitor_attr {
57 RSSI_MONITOR_ATTRIBUTE_MAX_RSSI,
58 RSSI_MONITOR_ATTRIBUTE_MIN_RSSI,
59 RSSI_MONITOR_ATTRIBUTE_START,
60};
61
62static wifi_error wifi_start_rssi_monitoring(wifi_request_id id, wifi_interface_handle
63 iface, s8 max_rssi, s8 min_rssi, wifi_rssi_event_handler eh);
64static wifi_error wifi_stop_rssi_monitoring(wifi_request_id id, wifi_interface_handle iface);
65
7753f181
DD
66/* Initialize/Cleanup */
67
68void wifi_socket_set_local_port(struct nl_sock *sock, uint32_t port)
69{
70 uint32_t pid = getpid() & 0x3FFFFF;
71 nl_socket_set_local_port(sock, pid + (port << 22));
72}
73
74static nl_sock * wifi_create_nl_socket(int port)
75{
7753f181
DD
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
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 122 fn->wifi_set_epno_list = wifi_set_epno_list;
c15187c1 123 fn->wifi_reset_epno_list = wifi_reset_epno_list;
6ff2d683
JPS
124 fn->wifi_set_passpoint_list = wifi_set_passpoint_list;
125 fn->wifi_reset_passpoint_list = wifi_reset_passpoint_list;
10d7569e 126 fn->wifi_set_bssid_blacklist = wifi_set_bssid_blacklist;
f425b4a8
PG
127 fn->wifi_start_rssi_monitoring = wifi_start_rssi_monitoring;
128 fn->wifi_stop_rssi_monitoring = wifi_stop_rssi_monitoring;
d583845d 129 fn->wifi_set_link_stats = wifi_set_link_stats;
058cae5f 130 fn->wifi_get_link_stats = wifi_get_link_stats;
d583845d 131 fn->wifi_clear_link_stats = wifi_clear_link_stats;
1fdbd4c1 132 fn->wifi_set_country_code = wifi_set_country_code;
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
7753f181
DD
150 if (socketpair(AF_UNIX, SOCK_STREAM, 0, info->cleanup_socks) == -1) {
151 ALOGE("Could not create cleanup sockets");
152 free(info);
153 return WIFI_ERROR_UNKNOWN;
154 }
155
156 struct nl_sock *cmd_sock = wifi_create_nl_socket(WIFI_HAL_CMD_SOCK_PORT);
157 if (cmd_sock == NULL) {
158 ALOGE("Could not create handle");
159 free(info);
160 return WIFI_ERROR_UNKNOWN;
161 }
162
163 struct nl_sock *event_sock = wifi_create_nl_socket(WIFI_HAL_EVENT_SOCK_PORT);
164 if (event_sock == NULL) {
165 ALOGE("Could not create handle");
166 nl_socket_free(cmd_sock);
167 free(info);
168 return WIFI_ERROR_UNKNOWN;
169 }
170
171 struct nl_cb *cb = nl_socket_get_cb(event_sock);
172 if (cb == NULL) {
173 ALOGE("Could not create handle");
174 nl_socket_free(cmd_sock);
175 nl_socket_free(event_sock);
176 free(info);
177 return WIFI_ERROR_UNKNOWN;
178 }
179
180// ALOGI("cb->refcnt = %d", cb->cb_refcnt);
181 nl_cb_set(cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM, internal_no_seq_check, info);
182 nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, internal_valid_message_handler, info);
183 nl_cb_put(cb);
184
185 info->cmd_sock = cmd_sock;
186 info->event_sock = event_sock;
187 info->clean_up = false;
188 info->in_event_loop = false;
189
190 info->event_cb = (cb_info *)malloc(sizeof(cb_info) * DEFAULT_EVENT_CB_SIZE);
191 info->alloc_event_cb = DEFAULT_EVENT_CB_SIZE;
192 info->num_event_cb = 0;
193
194 info->cmd = (cmd_info *)malloc(sizeof(cmd_info) * DEFAULT_CMD_SIZE);
195 info->alloc_cmd = DEFAULT_CMD_SIZE;
196 info->num_cmd = 0;
197
198 info->nl80211_family_id = genl_ctrl_resolve(cmd_sock, "nl80211");
199 if (info->nl80211_family_id < 0) {
200 ALOGE("Could not resolve nl80211 familty id");
201 nl_socket_free(cmd_sock);
202 nl_socket_free(event_sock);
203 free(info);
204 return WIFI_ERROR_UNKNOWN;
205 }
206
207 pthread_mutex_init(&info->cb_lock, NULL);
208
209 *handle = (wifi_handle) info;
c0b490a2 210
7753f181
DD
211 wifi_add_membership(*handle, "scan");
212 wifi_add_membership(*handle, "mlme");
213 wifi_add_membership(*handle, "regulatory");
214 wifi_add_membership(*handle, "vendor");
215
216 wifi_init_interfaces(*handle);
c0b490a2
JPS
217 char intf_name_buff[10 * 10 + 4]; /* Randomly choosen max interface 10. each interface name max 9 + 1(for space) */
218 char *pos = intf_name_buff;
219 for (int i = 0; i < (info->num_interfaces < 10 ? info->num_interfaces : 10); i++) {
220 strncpy(pos, info->interfaces[i]->name, sizeof(intf_name_buff) - (pos - intf_name_buff));
221 pos += strlen(pos);
222 }
223 if (info->num_interfaces > 10) {
224 strncpy(pos, "...", 3);
225 }
7753f181 226
c0b490a2 227 ALOGD("Found %d interfaces[%s]. Initialized Wifi HAL Successfully", info->num_interfaces, intf_name_buff);
7753f181 228
7753f181
DD
229 return WIFI_SUCCESS;
230}
231
232static int wifi_add_membership(wifi_handle handle, const char *group)
233{
234 hal_info *info = getHalInfo(handle);
235
236 int id = wifi_get_multicast_id(handle, "nl80211", group);
237 if (id < 0) {
238 ALOGE("Could not find group %s", group);
239 return id;
240 }
241
242 int ret = nl_socket_add_membership(info->event_sock, id);
243 if (ret < 0) {
244 ALOGE("Could not add membership to group %s", group);
245 }
7753f181
DD
246 return ret;
247}
248
249static void internal_cleaned_up_handler(wifi_handle handle)
250{
251 hal_info *info = getHalInfo(handle);
252 wifi_cleaned_up_handler cleaned_up_handler = info->cleaned_up_handler;
253
254 if (info->cmd_sock != 0) {
255 close(info->cleanup_socks[0]);
256 close(info->cleanup_socks[1]);
257 nl_socket_free(info->cmd_sock);
258 nl_socket_free(info->event_sock);
259 info->cmd_sock = NULL;
260 info->event_sock = NULL;
261 }
262
263 (*cleaned_up_handler)(handle);
264 pthread_mutex_destroy(&info->cb_lock);
265 free(info);
7753f181
DD
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);
c0b490a2 285 if (strncmp(buf, "Done", 4) != 0) {
7753f181
DD
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);
7753f181
DD
322 struct nl_cb *cb = nl_socket_get_cb(info->event_sock);
323 int res = nl_recvmsgs(info->event_sock, cb);
7753f181
DD
324 nl_cb_put(cb);
325 return res;
326}
327
328/* Run event handler */
329void wifi_event_loop(wifi_handle handle)
330{
331 hal_info *info = getHalInfo(handle);
7753f181
DD
332 if (info->in_event_loop) {
333 return;
334 } else {
335 info->in_event_loop = true;
336 }
337
338 pollfd pfd[2];
339 memset(&pfd[0], 0, sizeof(pollfd) * 2);
340
341 pfd[0].fd = nl_socket_get_fd(info->event_sock);
342 pfd[0].events = POLLIN;
343 pfd[1].fd = info->cleanup_socks[1];
344 pfd[1].events = POLLIN;
345
346 char buf[2048];
347
348 do {
349 int timeout = -1; /* Infinite timeout */
350 pfd[0].revents = 0;
351 pfd[1].revents = 0;
352 int result = poll(pfd, 2, timeout);
353 if (result < 0) {
354 } else if (pfd[0].revents & POLLERR) {
c0b490a2 355 int prev_err = (int)errno;
7753f181 356 int result2 = read(pfd[0].fd, buf, sizeof(buf));
c0b490a2 357 ALOGE("Poll err:%d | Read after POLL returned %d, error no = %d", prev_err, result2, errno);
7753f181
DD
358 } else if (pfd[0].revents & POLLHUP) {
359 ALOGE("Remote side hung up");
360 break;
361 } else if (pfd[0].revents & POLLIN) {
362 internal_pollin_handler(handle);
363 } else if (pfd[1].revents & POLLIN) {
364 memset(buf, 0, sizeof(buf));
365 int result2 = read(pfd[1].fd, buf, sizeof(buf));
366 ALOGE("%s: Read after POLL returned %d, error no = %d", __FUNCTION__, result2, errno);
367 if (strncmp(buf, "Exit", 4) == 0) {
368 ALOGD("Got a signal to exit!!!");
369 if (write(pfd[1].fd, "Done", 4) < 1) {
370 ALOGE("could not write to the cleanup socket");
371 }
372 break;
373 } else {
374 ALOGD("Rx'ed %s on the cleanup socket\n", buf);
375 }
376 } else {
377 ALOGE("Unknown event - %0x, %0x", pfd[0].revents, pfd[1].revents);
378 }
379 } while (!info->clean_up);
7753f181
DD
380}
381
382///////////////////////////////////////////////////////////////////////////////////////
383
384static int internal_no_seq_check(struct nl_msg *msg, void *arg)
385{
386 return NL_OK;
387}
388
389static int internal_valid_message_handler(nl_msg *msg, void *arg)
390{
391 wifi_handle handle = (wifi_handle)arg;
392 hal_info *info = getHalInfo(handle);
7753f181
DD
393
394 WifiEvent event(msg);
395 int res = event.parse();
396 if (res < 0) {
397 ALOGE("Failed to parse event: %d", res);
398 return NL_SKIP;
399 }
400
401 int cmd = event.get_cmd();
402 uint32_t vendor_id = 0;
403 int subcmd = 0;
404
405 if (cmd == NL80211_CMD_VENDOR) {
406 vendor_id = event.get_u32(NL80211_ATTR_VENDOR_ID);
407 subcmd = event.get_u32(NL80211_ATTR_VENDOR_SUBCMD);
408 ALOGI("event received %s, vendor_id = 0x%0x, subcmd = 0x%0x",
409 event.get_cmdString(), vendor_id, subcmd);
7753f181
DD
410 }
411
412 //ALOGI("event received %s, vendor_id = 0x%0x", event.get_cmdString(), vendor_id);
413 //event.log();
414
7753f181 415 pthread_mutex_lock(&info->cb_lock);
3c0c6ab1 416
7753f181
DD
417 for (int i = 0; i < info->num_event_cb; i++) {
418 if (cmd == info->event_cb[i].nl_cmd) {
419 if (cmd == NL80211_CMD_VENDOR
420 && ((vendor_id != info->event_cb[i].vendor_id)
421 || (subcmd != info->event_cb[i].vendor_subcmd)))
422 {
423 /* event for a different vendor, ignore it */
424 continue;
425 }
426
427 cb_info *cbi = &(info->event_cb[i]);
428 nl_recvmsg_msg_cb_t cb_func = cbi->cb_func;
429 void *cb_arg = cbi->cb_arg;
430 WifiCommand *cmd = (WifiCommand *)cbi->cb_arg;
431 if (cmd != NULL) {
432 cmd->addRef();
433 }
434
435 pthread_mutex_unlock(&info->cb_lock);
436 if (cb_func)
437 (*cb_func)(msg, cb_arg);
438 if (cmd != NULL) {
439 cmd->releaseRef();
440 }
441
442 return NL_OK;
443 }
444 }
445
446 pthread_mutex_unlock(&info->cb_lock);
447 return NL_OK;
448}
449
450///////////////////////////////////////////////////////////////////////////////////////
451
452class GetMulticastIdCommand : public WifiCommand
453{
454private:
455 const char *mName;
456 const char *mGroup;
457 int mId;
458public:
459 GetMulticastIdCommand(wifi_handle handle, const char *name, const char *group)
460 : WifiCommand(handle, 0)
461 {
462 mName = name;
463 mGroup = group;
464 mId = -1;
465 }
466
467 int getId() {
468 return mId;
469 }
470
471 virtual int create() {
472 int nlctrlFamily = genl_ctrl_resolve(mInfo->cmd_sock, "nlctrl");
7753f181
DD
473 int ret = mMsg.create(nlctrlFamily, CTRL_CMD_GETFAMILY, 0, 0);
474 if (ret < 0) {
475 return ret;
476 }
477 ret = mMsg.put_string(CTRL_ATTR_FAMILY_NAME, mName);
478 return ret;
479 }
480
481 virtual int handleResponse(WifiEvent& reply) {
7753f181 482 struct nlattr **tb = reply.attributes();
7753f181
DD
483 struct nlattr *mcgrp = NULL;
484 int i;
485
486 if (!tb[CTRL_ATTR_MCAST_GROUPS]) {
487 ALOGE("No multicast groups found");
488 return NL_SKIP;
7753f181
DD
489 }
490
491 for_each_attr(mcgrp, tb[CTRL_ATTR_MCAST_GROUPS], i) {
492
7753f181
DD
493 struct nlattr *tb2[CTRL_ATTR_MCAST_GRP_MAX + 1];
494 nla_parse(tb2, CTRL_ATTR_MCAST_GRP_MAX, (nlattr *)nla_data(mcgrp),
495 nla_len(mcgrp), NULL);
496 if (!tb2[CTRL_ATTR_MCAST_GRP_NAME] || !tb2[CTRL_ATTR_MCAST_GRP_ID]) {
497 continue;
498 }
499
500 char *grpName = (char *)nla_data(tb2[CTRL_ATTR_MCAST_GRP_NAME]);
501 int grpNameLen = nla_len(tb2[CTRL_ATTR_MCAST_GRP_NAME]);
502
7753f181
DD
503 if (strncmp(grpName, mGroup, grpNameLen) != 0)
504 continue;
505
506 mId = nla_get_u32(tb2[CTRL_ATTR_MCAST_GRP_ID]);
507 break;
508 }
509
510 return NL_SKIP;
511 }
512
513};
514
515class SetPnoMacAddrOuiCommand : public WifiCommand {
516
517private:
518 byte *mOui;
519 feature_set *fset;
520 feature_set *feature_matrix;
521 int *fm_size;
522 int set_size_max;
523public:
524 SetPnoMacAddrOuiCommand(wifi_interface_handle handle, oui scan_oui)
525 : WifiCommand(handle, 0)
526 {
527 mOui = scan_oui;
528 }
529
530 int createRequest(WifiRequest& request, int subcmd, byte *scan_oui) {
531 int result = request.create(GOOGLE_OUI, subcmd);
532 if (result < 0) {
533 return result;
534 }
535
536 nlattr *data = request.attr_start(NL80211_ATTR_VENDOR_DATA);
537 result = request.put(ANDR_WIFI_ATTRIBUTE_PNO_RANDOM_MAC_OUI, scan_oui, DOT11_OUI_LEN);
538 if (result < 0) {
539 return result;
540 }
541
542 request.attr_end(data);
543 return WIFI_SUCCESS;
544
545 }
546
547 int start() {
7753f181
DD
548 WifiRequest request(familyId(), ifaceId());
549 int result = createRequest(request, SLSI_NL80211_VENDOR_SUBCMD_SET_GSCAN_OUI, mOui);
550 if (result != WIFI_SUCCESS) {
551 ALOGE("failed to create request; result = %d", result);
552 return result;
553 }
554
555 result = requestResponse(request);
556 if (result != WIFI_SUCCESS) {
557 ALOGE("failed to set scanning mac OUI; result = %d", result);
558 }
559
560 return result;
561 }
562protected:
563 virtual int handleResponse(WifiEvent& reply) {
7753f181
DD
564 /* Nothing to do on response! */
565 return NL_SKIP;
566 }
567};
568
569class SetNodfsCommand : public WifiCommand {
570
571private:
572 u32 mNoDfs;
573public:
574 SetNodfsCommand(wifi_interface_handle handle, u32 nodfs)
575 : WifiCommand(handle, 0) {
576 mNoDfs = nodfs;
577 }
578 virtual int create() {
579 int ret;
580
581 ret = mMsg.create(GOOGLE_OUI, SLSI_NL80211_VENDOR_SUBCMD_SET_NODFS);
582 if (ret < 0) {
583 ALOGE("Can't create message to send to driver - %d", ret);
584 return ret;
585 }
586
587 nlattr *data = mMsg.attr_start(NL80211_ATTR_VENDOR_DATA);
588 ret = mMsg.put_u32(ATTR_NODFS_VALUE, mNoDfs);
589 if (ret < 0) {
590 return ret;
591 }
592
593 mMsg.attr_end(data);
594 return WIFI_SUCCESS;
595 }
596};
597
f425b4a8
PG
598class SetRSSIMonitorCommand : public WifiCommand {
599private:
600 s8 mMax_rssi;
601 s8 mMin_rssi;
602 wifi_rssi_event_handler mHandler;
603public:
604 SetRSSIMonitorCommand(wifi_request_id id, wifi_interface_handle handle,
605 s8 max_rssi, s8 min_rssi, wifi_rssi_event_handler eh)
606 : WifiCommand(handle, id), mMax_rssi(max_rssi), mMin_rssi
607 (min_rssi), mHandler(eh)
608 {
609 }
610 int createRequest(WifiRequest& request, int enable) {
611 int result = request.create(GOOGLE_OUI, SLSI_NL80211_VENDOR_SUBCMD_SET_RSSI_MONITOR);
612 if (result < 0) {
613 return result;
614 }
615
616 nlattr *data = request.attr_start(NL80211_ATTR_VENDOR_DATA);
617 result = request.put_u8(RSSI_MONITOR_ATTRIBUTE_MAX_RSSI, (enable ? mMax_rssi: 0));
618 if (result < 0) {
619 return result;
620 }
c0b490a2 621
f425b4a8
PG
622 result = request.put_u8(RSSI_MONITOR_ATTRIBUTE_MIN_RSSI, (enable? mMin_rssi: 0));
623 if (result < 0) {
624 return result;
625 }
626 result = request.put_u8(RSSI_MONITOR_ATTRIBUTE_START, enable);
627 if (result < 0) {
628 return result;
629 }
630 request.attr_end(data);
631 return result;
632 }
633
634 int start() {
635 WifiRequest request(familyId(), ifaceId());
636 int result = createRequest(request, 1);
637 if (result < 0) {
638 return result;
639 }
640 result = requestResponse(request);
641 if (result < 0) {
642 ALOGI("Failed to set RSSI Monitor, result = %d", result);
643 return result;
644 }
645 ALOGI("Successfully set RSSI monitoring");
646 registerVendorHandler(GOOGLE_OUI, WIFI_RSSI_REPORT_EVENT);
647
f425b4a8
PG
648 return result;
649 }
650
651 virtual int cancel() {
652
653 WifiRequest request(familyId(), ifaceId());
654 int result = createRequest(request, 0);
655 if (result != WIFI_SUCCESS) {
656 ALOGE("failed to create request; result = %d", result);
657 } else {
658 result = requestResponse(request);
659 if (result != WIFI_SUCCESS) {
660 ALOGE("failed to stop RSSI monitoring = %d", result);
661 }
662 }
663 unregisterVendorHandler(GOOGLE_OUI, WIFI_RSSI_REPORT_EVENT);
664 return WIFI_SUCCESS;
665 }
666
667 virtual int handleResponse(WifiEvent& reply) {
668 /* Nothing to do on response! */
669 return NL_SKIP;
670 }
671
672 virtual int handleEvent(WifiEvent& event) {
f425b4a8
PG
673
674 nlattr *vendor_data = event.get_attribute(NL80211_ATTR_VENDOR_DATA);
675 int len = event.get_vendor_data_len();
676
677 if (vendor_data == NULL || len == 0) {
678 ALOGI("RSSI monitor: No data");
679 return NL_SKIP;
680 }
681
682 typedef struct {
683 s8 cur_rssi;
684 mac_addr BSSID;
685 } rssi_monitor_evt;
686
687 rssi_monitor_evt *data = (rssi_monitor_evt *)event.get_vendor_data();
688
689 if (*mHandler.on_rssi_threshold_breached) {
690 (*mHandler.on_rssi_threshold_breached)(id(), data->BSSID, data->cur_rssi);
691 } else {
692 ALOGW("No RSSI monitor handler registered");
693 }
694
695 return NL_SKIP;
696 }
697
698};
699
1fdbd4c1
MG
700class SetCountryCodeCommand : public WifiCommand {
701private:
702 const char *mCountryCode;
703public:
704 SetCountryCodeCommand(wifi_interface_handle handle, const char *country_code)
705 : WifiCommand(handle, 0) {
706 mCountryCode = country_code;
707 }
708 virtual int create() {
709 int ret;
710
711 ret = mMsg.create(GOOGLE_OUI, SLSI_NL80211_VENDOR_SUBCMD_SET_COUNTRY_CODE);
712 if (ret < 0) {
713 ALOGE("Can't create message to send to driver - %d", ret);
714 return ret;
715 }
716
717 nlattr *data = mMsg.attr_start(NL80211_ATTR_VENDOR_DATA);
718 ret = mMsg.put_string(ATTR_COUNTRY_CODE, mCountryCode);
719 if (ret < 0) {
720 return ret;
721 }
722
723 mMsg.attr_end(data);
724 return WIFI_SUCCESS;
725
726 }
727};
728
0318783f 729class GetFeatureSetCommand : public WifiCommand {
f425b4a8 730
0318783f 731private:
0fe9bfaf 732
0318783f 733 feature_set *fset;
0fe9bfaf 734
0318783f
JPS
735public:
736 GetFeatureSetCommand(wifi_interface_handle handle, feature_set *set)
737 : WifiCommand(handle, 0)
738 {
739 fset = set;
740 }
741
742 virtual int create() {
743 int ret;
744
745 ret = mMsg.create(GOOGLE_OUI, SLSI_NL80211_VENDOR_SUBCMD_GET_FEATURE_SET);
746 if (ret < 0) {
747 ALOGE("create failed - %d", ret);
748 }
749
750 return ret;
751 }
752
753protected:
754 virtual int handleResponse(WifiEvent& reply) {
755
756 int id = reply.get_vendor_id();
757 int subcmd = reply.get_vendor_subcmd();
758
759 if (reply.get_cmd() != NL80211_CMD_VENDOR) {
760 ALOGD("Ignore reply; cmd = %d", reply.get_cmd());
761 return NL_SKIP;
762 }
763
764 nlattr *vendor_data = reply.get_attribute(NL80211_ATTR_VENDOR_DATA);
765 int len = reply.get_vendor_data_len();
766
767 if (vendor_data == NULL || len == 0) {
768 ALOGE("vendor data in GetFeatureSetCommand missing!!");
769 return NL_SKIP;
770 }
771
772 void *data = reply.get_vendor_data();
773 if(!fset) {
774 ALOGE("feature_set Pointer not set");
775 return NL_SKIP;
776 }
777 memcpy(fset, data, min(len, (int) sizeof(*fset)));
778 return NL_OK;
779 }
780
781};
f425b4a8 782
7753f181
DD
783static int wifi_get_multicast_id(wifi_handle handle, const char *name, const char *group)
784{
785 GetMulticastIdCommand cmd(handle, name, group);
786 int res = cmd.requestResponse();
787 if (res < 0)
788 return res;
789 else
790 return cmd.getId();
791}
792
793/////////////////////////////////////////////////////////////////////////
794
795static bool is_wifi_interface(const char *name)
796{
797 if (strncmp(name, "wlan", 4) != 0 && strncmp(name, "p2p", 3) != 0) {
798 /* not a wifi interface; ignore it */
799 return false;
800 } else {
801 return true;
802 }
803}
804
805static int get_interface(const char *name, interface_info *info)
806{
807 strcpy(info->name, name);
808 info->id = if_nametoindex(name);
7753f181
DD
809 return WIFI_SUCCESS;
810}
811
812wifi_error wifi_init_interfaces(wifi_handle handle)
813{
814 hal_info *info = (hal_info *)handle;
7753f181
DD
815 struct dirent *de;
816
817 DIR *d = opendir("/sys/class/net");
818 if (d == 0)
819 return WIFI_ERROR_UNKNOWN;
820
821 int n = 0;
822 while ((de = readdir(d))) {
823 if (de->d_name[0] == '.')
824 continue;
825 if (is_wifi_interface(de->d_name) ) {
826 n++;
827 }
828 }
829
830 closedir(d);
831
832 d = opendir("/sys/class/net");
833 if (d == 0)
834 return WIFI_ERROR_UNKNOWN;
835
836 info->interfaces = (interface_info **)malloc(sizeof(interface_info *) * n);
837
838 int i = 0;
839 while ((de = readdir(d))) {
840 if (de->d_name[0] == '.')
841 continue;
842 if (is_wifi_interface(de->d_name)) {
843 interface_info *ifinfo = (interface_info *)malloc(sizeof(interface_info));
844 if (get_interface(de->d_name, ifinfo) != WIFI_SUCCESS) {
845 free(ifinfo);
846 continue;
847 }
848 ifinfo->handle = handle;
849 info->interfaces[i] = ifinfo;
850 i++;
851 }
852 }
853
854 closedir(d);
855
856 info->num_interfaces = n;
857 return WIFI_SUCCESS;
858}
859
860wifi_error wifi_get_ifaces(wifi_handle handle, int *num, wifi_interface_handle **interfaces)
861{
862 hal_info *info = (hal_info *)handle;
863
864 *interfaces = (wifi_interface_handle *)info->interfaces;
865 *num = info->num_interfaces;
866
867 return WIFI_SUCCESS;
868}
869
870wifi_error wifi_get_iface_name(wifi_interface_handle handle, char *name, size_t size)
871{
872 interface_info *info = (interface_info *)handle;
873 strcpy(name, info->name);
874 return WIFI_SUCCESS;
875}
876
7753f181
DD
877wifi_error wifi_get_concurrency_matrix(wifi_interface_handle handle, int set_size_max,
878 feature_set set[], int *set_size)
879{
880 return WIFI_ERROR_NOT_SUPPORTED;
881}
882
883wifi_error wifi_set_scanning_mac_oui(wifi_interface_handle handle, oui scan_oui)
884{
885 SetPnoMacAddrOuiCommand command(handle, scan_oui);
886 return (wifi_error)command.start();
887
888}
889
890wifi_error wifi_set_nodfs_flag(wifi_interface_handle handle, u32 nodfs)
891{
892 SetNodfsCommand command(handle, nodfs);
893 return (wifi_error) command.requestResponse();
894}
895
f425b4a8
PG
896static wifi_error wifi_start_rssi_monitoring(wifi_request_id id, wifi_interface_handle
897 iface, s8 max_rssi, s8 min_rssi, wifi_rssi_event_handler eh)
898{
899 ALOGD("Start RSSI monitor %d", id);
900 wifi_handle handle = getWifiHandle(iface);
901 SetRSSIMonitorCommand *cmd = new SetRSSIMonitorCommand(id, iface, max_rssi, min_rssi, eh);
902 wifi_register_cmd(handle, id, cmd);
903
904 wifi_error result = (wifi_error)cmd->start();
905 if (result != WIFI_SUCCESS) {
906 wifi_unregister_cmd(handle, id);
907 }
908 return result;
909}
910
911
912static wifi_error wifi_stop_rssi_monitoring(wifi_request_id id, wifi_interface_handle iface)
913{
914 ALOGD("Stopping RSSI monitor");
915
916 if(id == -1) {
917 wifi_rssi_event_handler handler;
918 wifi_handle handle = getWifiHandle(iface);
919 memset(&handler, 0, sizeof(handler));
920 SetRSSIMonitorCommand *cmd = new SetRSSIMonitorCommand(id, iface,
921 0, 0, handler);
922 cmd->cancel();
923 cmd->releaseRef();
924 return WIFI_SUCCESS;
925 }
926 return wifi_cancel_cmd(id, iface);
927}
928
0318783f
JPS
929wifi_error wifi_get_supported_feature_set(wifi_interface_handle handle, feature_set *set)
930{
931 GetFeatureSetCommand command(handle, set);
932 return (wifi_error) command.requestResponse();
933}
934
1fdbd4c1
MG
935wifi_error wifi_set_country_code(wifi_interface_handle handle, const char *country_code)
936{
937 SetCountryCodeCommand command(handle, country_code);
938 return (wifi_error) command.requestResponse();
939}
940
7753f181 941/////////////////////////////////////////////////////////////////////////////