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