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