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