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; | |
d583845d | 128 | fn->wifi_set_link_stats = wifi_set_link_stats; |
058cae5f | 129 | fn->wifi_get_link_stats = wifi_get_link_stats; |
d583845d | 130 | fn->wifi_clear_link_stats = wifi_clear_link_stats; |
058cae5f | 131 | |
7753f181 DD |
132 | return WIFI_SUCCESS; |
133 | } | |
134 | ||
135 | wifi_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 | ||
226 | static 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 | ||
245 | static 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 | ||
266 | void 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 | ||
319 | static 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 */ | |
331 | void 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 | ||
389 | static int internal_no_seq_check(struct nl_msg *msg, void *arg) | |
390 | { | |
391 | return NL_OK; | |
392 | } | |
393 | ||
394 | static 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 | ||
463 | class GetMulticastIdCommand : public WifiCommand | |
464 | { | |
465 | private: | |
466 | const char *mName; | |
467 | const char *mGroup; | |
468 | int mId; | |
469 | public: | |
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 | ||
535 | class SetPnoMacAddrOuiCommand : public WifiCommand { | |
536 | ||
537 | private: | |
538 | byte *mOui; | |
539 | feature_set *fset; | |
540 | feature_set *feature_matrix; | |
541 | int *fm_size; | |
542 | int set_size_max; | |
543 | public: | |
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 | } | |
583 | protected: | |
584 | virtual int handleResponse(WifiEvent& reply) { | |
585 | ALOGD("Request complete!"); | |
586 | /* Nothing to do on response! */ | |
587 | return NL_SKIP; | |
588 | } | |
589 | }; | |
590 | ||
591 | class SetNodfsCommand : public WifiCommand { | |
592 | ||
593 | private: | |
594 | u32 mNoDfs; | |
595 | public: | |
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 |
620 | class SetRSSIMonitorCommand : public WifiCommand { |
621 | private: | |
622 | s8 mMax_rssi; | |
623 | s8 mMin_rssi; | |
624 | wifi_rssi_event_handler mHandler; | |
625 | public: | |
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 | ||
0318783f | 729 | class GetFeatureSetCommand : public WifiCommand { |
f425b4a8 | 730 | |
0318783f JPS |
731 | private: |
732 | int feature_type; | |
733 | feature_set *fset; | |
734 | int *fm_size; | |
735 | int set_size_max; | |
736 | public: | |
737 | GetFeatureSetCommand(wifi_interface_handle handle, feature_set *set) | |
738 | : WifiCommand(handle, 0) | |
739 | { | |
740 | fset = set; | |
741 | } | |
742 | ||
743 | virtual int create() { | |
744 | int ret; | |
745 | ||
746 | ret = mMsg.create(GOOGLE_OUI, SLSI_NL80211_VENDOR_SUBCMD_GET_FEATURE_SET); | |
747 | if (ret < 0) { | |
748 | ALOGE("create failed - %d", ret); | |
749 | } | |
750 | ||
751 | return ret; | |
752 | } | |
753 | ||
754 | protected: | |
755 | virtual int handleResponse(WifiEvent& reply) { | |
756 | ||
757 | int id = reply.get_vendor_id(); | |
758 | int subcmd = reply.get_vendor_subcmd(); | |
759 | ||
760 | if (reply.get_cmd() != NL80211_CMD_VENDOR) { | |
761 | ALOGD("Ignore reply; cmd = %d", reply.get_cmd()); | |
762 | return NL_SKIP; | |
763 | } | |
764 | ||
765 | nlattr *vendor_data = reply.get_attribute(NL80211_ATTR_VENDOR_DATA); | |
766 | int len = reply.get_vendor_data_len(); | |
767 | ||
768 | if (vendor_data == NULL || len == 0) { | |
769 | ALOGE("vendor data in GetFeatureSetCommand missing!!"); | |
770 | return NL_SKIP; | |
771 | } | |
772 | ||
773 | void *data = reply.get_vendor_data(); | |
774 | if(!fset) { | |
775 | ALOGE("feature_set Pointer not set"); | |
776 | return NL_SKIP; | |
777 | } | |
778 | memcpy(fset, data, min(len, (int) sizeof(*fset))); | |
779 | return NL_OK; | |
780 | } | |
781 | ||
782 | }; | |
f425b4a8 | 783 | |
7753f181 DD |
784 | static int wifi_get_multicast_id(wifi_handle handle, const char *name, const char *group) |
785 | { | |
786 | GetMulticastIdCommand cmd(handle, name, group); | |
787 | int res = cmd.requestResponse(); | |
788 | if (res < 0) | |
789 | return res; | |
790 | else | |
791 | return cmd.getId(); | |
792 | } | |
793 | ||
794 | ///////////////////////////////////////////////////////////////////////// | |
795 | ||
796 | static bool is_wifi_interface(const char *name) | |
797 | { | |
798 | if (strncmp(name, "wlan", 4) != 0 && strncmp(name, "p2p", 3) != 0) { | |
799 | /* not a wifi interface; ignore it */ | |
800 | return false; | |
801 | } else { | |
802 | return true; | |
803 | } | |
804 | } | |
805 | ||
806 | static int get_interface(const char *name, interface_info *info) | |
807 | { | |
808 | strcpy(info->name, name); | |
809 | info->id = if_nametoindex(name); | |
810 | ALOGI("found an interface : %s, id = %d", name, info->id); | |
811 | return WIFI_SUCCESS; | |
812 | } | |
813 | ||
814 | wifi_error wifi_init_interfaces(wifi_handle handle) | |
815 | { | |
816 | hal_info *info = (hal_info *)handle; | |
817 | ALOGD("wifi_init_interfaces, info = %p", info); | |
818 | ||
819 | struct dirent *de; | |
820 | ||
821 | DIR *d = opendir("/sys/class/net"); | |
822 | if (d == 0) | |
823 | return WIFI_ERROR_UNKNOWN; | |
824 | ||
825 | int n = 0; | |
826 | while ((de = readdir(d))) { | |
827 | if (de->d_name[0] == '.') | |
828 | continue; | |
829 | if (is_wifi_interface(de->d_name) ) { | |
830 | n++; | |
831 | } | |
832 | } | |
833 | ||
834 | closedir(d); | |
835 | ||
836 | d = opendir("/sys/class/net"); | |
837 | if (d == 0) | |
838 | return WIFI_ERROR_UNKNOWN; | |
839 | ||
840 | info->interfaces = (interface_info **)malloc(sizeof(interface_info *) * n); | |
841 | ||
842 | int i = 0; | |
843 | while ((de = readdir(d))) { | |
844 | if (de->d_name[0] == '.') | |
845 | continue; | |
846 | if (is_wifi_interface(de->d_name)) { | |
847 | interface_info *ifinfo = (interface_info *)malloc(sizeof(interface_info)); | |
848 | if (get_interface(de->d_name, ifinfo) != WIFI_SUCCESS) { | |
849 | free(ifinfo); | |
850 | continue; | |
851 | } | |
852 | ifinfo->handle = handle; | |
853 | info->interfaces[i] = ifinfo; | |
854 | i++; | |
855 | } | |
856 | } | |
857 | ||
858 | closedir(d); | |
859 | ||
860 | info->num_interfaces = n; | |
861 | return WIFI_SUCCESS; | |
862 | } | |
863 | ||
864 | wifi_error wifi_get_ifaces(wifi_handle handle, int *num, wifi_interface_handle **interfaces) | |
865 | { | |
866 | hal_info *info = (hal_info *)handle; | |
867 | ||
868 | *interfaces = (wifi_interface_handle *)info->interfaces; | |
869 | *num = info->num_interfaces; | |
870 | ||
871 | return WIFI_SUCCESS; | |
872 | } | |
873 | ||
874 | wifi_error wifi_get_iface_name(wifi_interface_handle handle, char *name, size_t size) | |
875 | { | |
876 | interface_info *info = (interface_info *)handle; | |
877 | strcpy(name, info->name); | |
878 | return WIFI_SUCCESS; | |
879 | } | |
880 | ||
7753f181 DD |
881 | wifi_error wifi_get_concurrency_matrix(wifi_interface_handle handle, int set_size_max, |
882 | feature_set set[], int *set_size) | |
883 | { | |
884 | return WIFI_ERROR_NOT_SUPPORTED; | |
885 | } | |
886 | ||
887 | wifi_error wifi_set_scanning_mac_oui(wifi_interface_handle handle, oui scan_oui) | |
888 | { | |
889 | SetPnoMacAddrOuiCommand command(handle, scan_oui); | |
890 | return (wifi_error)command.start(); | |
891 | ||
892 | } | |
893 | ||
894 | wifi_error wifi_set_nodfs_flag(wifi_interface_handle handle, u32 nodfs) | |
895 | { | |
896 | SetNodfsCommand command(handle, nodfs); | |
897 | return (wifi_error) command.requestResponse(); | |
898 | } | |
899 | ||
f425b4a8 PG |
900 | static wifi_error wifi_start_rssi_monitoring(wifi_request_id id, wifi_interface_handle |
901 | iface, s8 max_rssi, s8 min_rssi, wifi_rssi_event_handler eh) | |
902 | { | |
903 | ALOGD("Start RSSI monitor %d", id); | |
904 | wifi_handle handle = getWifiHandle(iface); | |
905 | SetRSSIMonitorCommand *cmd = new SetRSSIMonitorCommand(id, iface, max_rssi, min_rssi, eh); | |
906 | wifi_register_cmd(handle, id, cmd); | |
907 | ||
908 | wifi_error result = (wifi_error)cmd->start(); | |
909 | if (result != WIFI_SUCCESS) { | |
910 | wifi_unregister_cmd(handle, id); | |
911 | } | |
912 | return result; | |
913 | } | |
914 | ||
915 | ||
916 | static wifi_error wifi_stop_rssi_monitoring(wifi_request_id id, wifi_interface_handle iface) | |
917 | { | |
918 | ALOGD("Stopping RSSI monitor"); | |
919 | ||
920 | if(id == -1) { | |
921 | wifi_rssi_event_handler handler; | |
922 | wifi_handle handle = getWifiHandle(iface); | |
923 | memset(&handler, 0, sizeof(handler)); | |
924 | SetRSSIMonitorCommand *cmd = new SetRSSIMonitorCommand(id, iface, | |
925 | 0, 0, handler); | |
926 | cmd->cancel(); | |
927 | cmd->releaseRef(); | |
928 | return WIFI_SUCCESS; | |
929 | } | |
930 | return wifi_cancel_cmd(id, iface); | |
931 | } | |
932 | ||
0318783f JPS |
933 | wifi_error wifi_get_supported_feature_set(wifi_interface_handle handle, feature_set *set) |
934 | { | |
935 | GetFeatureSetCommand command(handle, set); | |
936 | return (wifi_error) command.requestResponse(); | |
937 | } | |
938 | ||
7753f181 | 939 | ///////////////////////////////////////////////////////////////////////////// |