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> | |
29 | ||
30 | #include "wifi_hal.h" | |
31 | #include "common.h" | |
32 | #include "cpp_bindings.h" | |
33 | ||
34 | ||
35 | #define WIFI_HAL_CMD_SOCK_PORT 644 | |
36 | #define WIFI_HAL_EVENT_SOCK_PORT 645 | |
37 | ||
38 | #define FEATURE_SET 0 | |
39 | #define FEATURE_SET_MATRIX 1 | |
40 | #define ATTR_NODFS_VALUE 3 | |
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 | ||
55 | /* Initialize/Cleanup */ | |
56 | ||
57 | void wifi_socket_set_local_port(struct nl_sock *sock, uint32_t port) | |
58 | { | |
59 | uint32_t pid = getpid() & 0x3FFFFF; | |
60 | nl_socket_set_local_port(sock, pid + (port << 22)); | |
61 | } | |
62 | ||
63 | static nl_sock * wifi_create_nl_socket(int port) | |
64 | { | |
65 | ALOGI("Creating socket"); | |
66 | struct nl_sock *sock = nl_socket_alloc(); | |
67 | if (sock == NULL) { | |
68 | ALOGE("Could not create handle"); | |
69 | return NULL; | |
70 | } | |
71 | ||
72 | wifi_socket_set_local_port(sock, port); | |
73 | ||
de2a8779 | 74 | ALOGI("Connecting socket"); |
7753f181 DD |
75 | if (nl_connect(sock, NETLINK_GENERIC)) { |
76 | ALOGE("Could not connect handle"); | |
77 | nl_socket_free(sock); | |
78 | return NULL; | |
79 | } | |
80 | ||
7753f181 DD |
81 | return sock; |
82 | } | |
83 | ||
d59e4b54 | 84 | /* Initialize HAL function pointer table */ |
7753f181 DD |
85 | wifi_error init_wifi_vendor_hal_func_table(wifi_hal_fn *fn) |
86 | { | |
87 | if (fn == NULL) { | |
88 | return WIFI_ERROR_UNKNOWN; | |
89 | } | |
90 | fn->wifi_initialize = wifi_initialize; | |
91 | fn->wifi_cleanup = wifi_cleanup; | |
92 | fn->wifi_event_loop = wifi_event_loop; | |
93 | fn->wifi_get_supported_feature_set = wifi_get_supported_feature_set; | |
94 | fn->wifi_get_concurrency_matrix = wifi_get_concurrency_matrix; | |
95 | fn->wifi_set_scanning_mac_oui = wifi_set_scanning_mac_oui; | |
96 | fn->wifi_get_ifaces = wifi_get_ifaces; | |
97 | fn->wifi_get_iface_name = wifi_get_iface_name; | |
98 | fn->wifi_start_gscan = wifi_start_gscan; | |
99 | fn->wifi_stop_gscan = wifi_stop_gscan; | |
100 | fn->wifi_get_cached_gscan_results = wifi_get_cached_gscan_results; | |
101 | fn->wifi_set_bssid_hotlist = wifi_set_bssid_hotlist; | |
102 | fn->wifi_reset_bssid_hotlist = wifi_reset_bssid_hotlist; | |
103 | fn->wifi_set_significant_change_handler = wifi_set_significant_change_handler; | |
104 | fn->wifi_reset_significant_change_handler = wifi_reset_significant_change_handler; | |
105 | fn->wifi_get_gscan_capabilities = wifi_get_gscan_capabilities; | |
106 | fn->wifi_get_link_stats = wifi_get_link_stats; | |
107 | fn->wifi_get_valid_channels = wifi_get_valid_channels; | |
108 | fn->wifi_rtt_range_request = wifi_rtt_range_request; | |
109 | fn->wifi_rtt_range_cancel = wifi_rtt_range_cancel; | |
110 | fn->wifi_get_rtt_capabilities = wifi_get_rtt_capabilities; | |
111 | fn->wifi_set_nodfs_flag = wifi_set_nodfs_flag; | |
3c0c6ab1 PG |
112 | fn->wifi_start_sending_offloaded_packet = wifi_start_sending_offloaded_packet; |
113 | fn->wifi_stop_sending_offloaded_packet = wifi_stop_sending_offloaded_packet; | |
6ff2d683 JPS |
114 | #if 0 |
115 | fn->wifi_set_epno_list = wifi_set_epno_list; | |
116 | fn->wifi_set_passpoint_list = wifi_set_passpoint_list; | |
117 | fn->wifi_reset_passpoint_list = wifi_reset_passpoint_list; | |
118 | #endif | |
7753f181 DD |
119 | return WIFI_SUCCESS; |
120 | } | |
121 | ||
122 | wifi_error wifi_initialize(wifi_handle *handle) | |
123 | { | |
124 | srand(getpid()); | |
125 | ||
126 | ALOGI("Initializing wifi"); | |
127 | hal_info *info = (hal_info *)malloc(sizeof(hal_info)); | |
128 | if (info == NULL) { | |
129 | ALOGE("Could not allocate hal_info"); | |
130 | return WIFI_ERROR_UNKNOWN; | |
131 | } | |
132 | ||
133 | memset(info, 0, sizeof(*info)); | |
134 | ||
135 | ALOGI("Creating socket"); | |
136 | if (socketpair(AF_UNIX, SOCK_STREAM, 0, info->cleanup_socks) == -1) { | |
137 | ALOGE("Could not create cleanup sockets"); | |
138 | free(info); | |
139 | return WIFI_ERROR_UNKNOWN; | |
140 | } | |
141 | ||
142 | struct nl_sock *cmd_sock = wifi_create_nl_socket(WIFI_HAL_CMD_SOCK_PORT); | |
143 | if (cmd_sock == NULL) { | |
144 | ALOGE("Could not create handle"); | |
145 | free(info); | |
146 | return WIFI_ERROR_UNKNOWN; | |
147 | } | |
148 | ||
149 | struct nl_sock *event_sock = wifi_create_nl_socket(WIFI_HAL_EVENT_SOCK_PORT); | |
150 | if (event_sock == NULL) { | |
151 | ALOGE("Could not create handle"); | |
152 | nl_socket_free(cmd_sock); | |
153 | free(info); | |
154 | return WIFI_ERROR_UNKNOWN; | |
155 | } | |
156 | ||
157 | struct nl_cb *cb = nl_socket_get_cb(event_sock); | |
158 | if (cb == NULL) { | |
159 | ALOGE("Could not create handle"); | |
160 | nl_socket_free(cmd_sock); | |
161 | nl_socket_free(event_sock); | |
162 | free(info); | |
163 | return WIFI_ERROR_UNKNOWN; | |
164 | } | |
165 | ||
166 | // ALOGI("cb->refcnt = %d", cb->cb_refcnt); | |
167 | nl_cb_set(cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM, internal_no_seq_check, info); | |
168 | nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, internal_valid_message_handler, info); | |
169 | nl_cb_put(cb); | |
170 | ||
171 | info->cmd_sock = cmd_sock; | |
172 | info->event_sock = event_sock; | |
173 | info->clean_up = false; | |
174 | info->in_event_loop = false; | |
175 | ||
176 | info->event_cb = (cb_info *)malloc(sizeof(cb_info) * DEFAULT_EVENT_CB_SIZE); | |
177 | info->alloc_event_cb = DEFAULT_EVENT_CB_SIZE; | |
178 | info->num_event_cb = 0; | |
179 | ||
180 | info->cmd = (cmd_info *)malloc(sizeof(cmd_info) * DEFAULT_CMD_SIZE); | |
181 | info->alloc_cmd = DEFAULT_CMD_SIZE; | |
182 | info->num_cmd = 0; | |
183 | ||
184 | info->nl80211_family_id = genl_ctrl_resolve(cmd_sock, "nl80211"); | |
185 | if (info->nl80211_family_id < 0) { | |
186 | ALOGE("Could not resolve nl80211 familty id"); | |
187 | nl_socket_free(cmd_sock); | |
188 | nl_socket_free(event_sock); | |
189 | free(info); | |
190 | return WIFI_ERROR_UNKNOWN; | |
191 | } | |
192 | ||
193 | pthread_mutex_init(&info->cb_lock, NULL); | |
194 | ||
195 | *handle = (wifi_handle) info; | |
196 | ALOGD("wifi_initialize, handle = %p\n", handle); | |
197 | ALOGD("wifi_initialize, *handle = %p\n", *handle); | |
198 | ALOGD("wifi_initialize, info = %p\n", info); | |
199 | ALOGD("wifi_initialize, *info = %pn", *info); | |
200 | wifi_add_membership(*handle, "scan"); | |
201 | wifi_add_membership(*handle, "mlme"); | |
202 | wifi_add_membership(*handle, "regulatory"); | |
203 | wifi_add_membership(*handle, "vendor"); | |
204 | ||
205 | wifi_init_interfaces(*handle); | |
206 | ALOGD("Found %d interfaces", info->num_interfaces); | |
207 | ||
208 | ||
209 | ALOGI("Initialized Wifi HAL Successfully; vendor cmd = %d", NL80211_CMD_VENDOR); | |
210 | return WIFI_SUCCESS; | |
211 | } | |
212 | ||
213 | static int wifi_add_membership(wifi_handle handle, const char *group) | |
214 | { | |
215 | hal_info *info = getHalInfo(handle); | |
216 | ||
217 | int id = wifi_get_multicast_id(handle, "nl80211", group); | |
218 | if (id < 0) { | |
219 | ALOGE("Could not find group %s", group); | |
220 | return id; | |
221 | } | |
222 | ||
223 | int ret = nl_socket_add_membership(info->event_sock, id); | |
224 | if (ret < 0) { | |
225 | ALOGE("Could not add membership to group %s", group); | |
226 | } | |
227 | ||
228 | ALOGI("Successfully added membership for group %s", group); | |
229 | return ret; | |
230 | } | |
231 | ||
232 | static void internal_cleaned_up_handler(wifi_handle handle) | |
233 | { | |
234 | hal_info *info = getHalInfo(handle); | |
235 | wifi_cleaned_up_handler cleaned_up_handler = info->cleaned_up_handler; | |
236 | ||
237 | if (info->cmd_sock != 0) { | |
238 | close(info->cleanup_socks[0]); | |
239 | close(info->cleanup_socks[1]); | |
240 | nl_socket_free(info->cmd_sock); | |
241 | nl_socket_free(info->event_sock); | |
242 | info->cmd_sock = NULL; | |
243 | info->event_sock = NULL; | |
244 | } | |
245 | ||
246 | (*cleaned_up_handler)(handle); | |
247 | pthread_mutex_destroy(&info->cb_lock); | |
248 | free(info); | |
249 | ||
250 | ALOGI("Internal cleanup completed"); | |
251 | } | |
252 | ||
253 | void wifi_cleanup(wifi_handle handle, wifi_cleaned_up_handler handler) | |
254 | { | |
255 | hal_info *info = getHalInfo(handle); | |
256 | char buf[64]; | |
257 | ||
258 | info->cleaned_up_handler = handler; | |
259 | if (write(info->cleanup_socks[0], "Exit", 4) < 1) { | |
260 | ALOGE("could not write to the cleanup socket"); | |
261 | } else { | |
262 | // Listen to the response | |
263 | // Hopefully we dont get errors or get hung up | |
264 | // Not much can be done in that case, but assume that | |
265 | // it has rx'ed the Exit message to exit the thread. | |
266 | // As a fallback set the cleanup flag to TRUE | |
267 | memset(buf, 0, sizeof(buf)); | |
268 | int result = read(info->cleanup_socks[0], buf, sizeof(buf)); | |
269 | ALOGE("%s: Read after POLL returned %d, error no = %d", __FUNCTION__, result, errno); | |
270 | if (strncmp(buf, "Done", 4) == 0) { | |
271 | ALOGE("Event processing terminated"); | |
272 | } else { | |
273 | ALOGD("Rx'ed %s", buf); | |
274 | } | |
275 | } | |
276 | info->clean_up = true; | |
277 | pthread_mutex_lock(&info->cb_lock); | |
278 | ||
279 | int bad_commands = 0; | |
280 | ||
7753f181 DD |
281 | while (info->num_cmd > bad_commands) { |
282 | int num_cmd = info->num_cmd; | |
283 | cmd_info *cmdi = &(info->cmd[bad_commands]); | |
284 | WifiCommand *cmd = cmdi->cmd; | |
285 | if (cmd != NULL) { | |
286 | pthread_mutex_unlock(&info->cb_lock); | |
287 | cmd->cancel(); | |
288 | pthread_mutex_lock(&info->cb_lock); | |
289 | /* release reference added when command is saved */ | |
290 | cmd->releaseRef(); | |
291 | if (num_cmd == info->num_cmd) { | |
292 | bad_commands++; | |
293 | } | |
294 | } | |
295 | } | |
296 | ||
297 | for (int i = 0; i < info->num_event_cb; i++) { | |
298 | cb_info *cbi = &(info->event_cb[i]); | |
299 | WifiCommand *cmd = (WifiCommand *)cbi->cb_arg; | |
300 | ALOGE("Leaked command %p", cmd); | |
301 | } | |
302 | pthread_mutex_unlock(&info->cb_lock); | |
303 | internal_cleaned_up_handler(handle); | |
304 | } | |
305 | ||
306 | static int internal_pollin_handler(wifi_handle handle) | |
307 | { | |
308 | hal_info *info = getHalInfo(handle); | |
309 | ALOGI("even_loop info = %p", info); | |
310 | struct nl_cb *cb = nl_socket_get_cb(info->event_sock); | |
311 | int res = nl_recvmsgs(info->event_sock, cb); | |
312 | ALOGD("nl_recvmsgs returned %d", res); | |
313 | nl_cb_put(cb); | |
314 | return res; | |
315 | } | |
316 | ||
317 | /* Run event handler */ | |
318 | void wifi_event_loop(wifi_handle handle) | |
319 | { | |
320 | hal_info *info = getHalInfo(handle); | |
321 | ALOGI("even_loop info = %p", info); | |
322 | ALOGI("even_loop info = %p", handle); | |
323 | if (info->in_event_loop) { | |
324 | return; | |
325 | } else { | |
326 | info->in_event_loop = true; | |
327 | } | |
328 | ||
329 | pollfd pfd[2]; | |
330 | memset(&pfd[0], 0, sizeof(pollfd) * 2); | |
331 | ||
332 | pfd[0].fd = nl_socket_get_fd(info->event_sock); | |
333 | pfd[0].events = POLLIN; | |
334 | pfd[1].fd = info->cleanup_socks[1]; | |
335 | pfd[1].events = POLLIN; | |
336 | ||
337 | char buf[2048]; | |
338 | ||
339 | do { | |
340 | int timeout = -1; /* Infinite timeout */ | |
341 | pfd[0].revents = 0; | |
342 | pfd[1].revents = 0; | |
343 | int result = poll(pfd, 2, timeout); | |
344 | if (result < 0) { | |
345 | } else if (pfd[0].revents & POLLERR) { | |
346 | ALOGE("POLL Error; error no = %d", errno); | |
347 | int result2 = read(pfd[0].fd, buf, sizeof(buf)); | |
348 | ALOGE("Read after POLL returned %d, error no = %d", result2, errno); | |
349 | } else if (pfd[0].revents & POLLHUP) { | |
350 | ALOGE("Remote side hung up"); | |
351 | break; | |
352 | } else if (pfd[0].revents & POLLIN) { | |
353 | internal_pollin_handler(handle); | |
354 | } else if (pfd[1].revents & POLLIN) { | |
355 | memset(buf, 0, sizeof(buf)); | |
356 | int result2 = read(pfd[1].fd, buf, sizeof(buf)); | |
357 | ALOGE("%s: Read after POLL returned %d, error no = %d", __FUNCTION__, result2, errno); | |
358 | if (strncmp(buf, "Exit", 4) == 0) { | |
359 | ALOGD("Got a signal to exit!!!"); | |
360 | if (write(pfd[1].fd, "Done", 4) < 1) { | |
361 | ALOGE("could not write to the cleanup socket"); | |
362 | } | |
363 | break; | |
364 | } else { | |
365 | ALOGD("Rx'ed %s on the cleanup socket\n", buf); | |
366 | } | |
367 | } else { | |
368 | ALOGE("Unknown event - %0x, %0x", pfd[0].revents, pfd[1].revents); | |
369 | } | |
370 | } while (!info->clean_up); | |
371 | ALOGI("Exit %s", __FUNCTION__); | |
372 | } | |
373 | ||
374 | /////////////////////////////////////////////////////////////////////////////////////// | |
375 | ||
376 | static int internal_no_seq_check(struct nl_msg *msg, void *arg) | |
377 | { | |
378 | return NL_OK; | |
379 | } | |
380 | ||
381 | static int internal_valid_message_handler(nl_msg *msg, void *arg) | |
382 | { | |
383 | wifi_handle handle = (wifi_handle)arg; | |
384 | hal_info *info = getHalInfo(handle); | |
385 | ALOGI("even_loop info = %p", handle); | |
386 | ALOGD("internal_valid_message_handler, info = %p", info); | |
387 | ||
388 | WifiEvent event(msg); | |
389 | int res = event.parse(); | |
390 | if (res < 0) { | |
391 | ALOGE("Failed to parse event: %d", res); | |
392 | return NL_SKIP; | |
393 | } | |
394 | ||
395 | int cmd = event.get_cmd(); | |
396 | uint32_t vendor_id = 0; | |
397 | int subcmd = 0; | |
398 | ||
399 | if (cmd == NL80211_CMD_VENDOR) { | |
400 | vendor_id = event.get_u32(NL80211_ATTR_VENDOR_ID); | |
401 | subcmd = event.get_u32(NL80211_ATTR_VENDOR_SUBCMD); | |
402 | ALOGI("event received %s, vendor_id = 0x%0x, subcmd = 0x%0x", | |
403 | event.get_cmdString(), vendor_id, subcmd); | |
404 | } else { | |
405 | ALOGI("event received %s", event.get_cmdString()); | |
406 | } | |
407 | ||
408 | //ALOGI("event received %s, vendor_id = 0x%0x", event.get_cmdString(), vendor_id); | |
409 | //event.log(); | |
410 | ||
7753f181 | 411 | pthread_mutex_lock(&info->cb_lock); |
3c0c6ab1 | 412 | |
7753f181 DD |
413 | ALOGI("Number of events %d", info->num_event_cb); |
414 | ||
415 | for (int i = 0; i < info->num_event_cb; i++) { | |
416 | if (cmd == info->event_cb[i].nl_cmd) { | |
417 | if (cmd == NL80211_CMD_VENDOR | |
418 | && ((vendor_id != info->event_cb[i].vendor_id) | |
419 | || (subcmd != info->event_cb[i].vendor_subcmd))) | |
420 | { | |
421 | /* event for a different vendor, ignore it */ | |
422 | continue; | |
423 | } | |
424 | ||
425 | cb_info *cbi = &(info->event_cb[i]); | |
426 | nl_recvmsg_msg_cb_t cb_func = cbi->cb_func; | |
427 | void *cb_arg = cbi->cb_arg; | |
428 | WifiCommand *cmd = (WifiCommand *)cbi->cb_arg; | |
429 | if (cmd != NULL) { | |
430 | cmd->addRef(); | |
431 | } | |
432 | ||
433 | pthread_mutex_unlock(&info->cb_lock); | |
434 | if (cb_func) | |
435 | (*cb_func)(msg, cb_arg); | |
436 | if (cmd != NULL) { | |
437 | cmd->releaseRef(); | |
438 | } | |
439 | ||
440 | return NL_OK; | |
441 | } | |
442 | } | |
443 | ||
444 | pthread_mutex_unlock(&info->cb_lock); | |
445 | return NL_OK; | |
446 | } | |
447 | ||
448 | /////////////////////////////////////////////////////////////////////////////////////// | |
449 | ||
450 | class GetMulticastIdCommand : public WifiCommand | |
451 | { | |
452 | private: | |
453 | const char *mName; | |
454 | const char *mGroup; | |
455 | int mId; | |
456 | public: | |
457 | GetMulticastIdCommand(wifi_handle handle, const char *name, const char *group) | |
458 | : WifiCommand(handle, 0) | |
459 | { | |
460 | mName = name; | |
461 | mGroup = group; | |
462 | mId = -1; | |
463 | } | |
464 | ||
465 | int getId() { | |
466 | return mId; | |
467 | } | |
468 | ||
469 | virtual int create() { | |
470 | int nlctrlFamily = genl_ctrl_resolve(mInfo->cmd_sock, "nlctrl"); | |
471 | ALOGI("ctrl family = %d", nlctrlFamily); | |
472 | int ret = mMsg.create(nlctrlFamily, CTRL_CMD_GETFAMILY, 0, 0); | |
473 | if (ret < 0) { | |
474 | return ret; | |
475 | } | |
476 | ret = mMsg.put_string(CTRL_ATTR_FAMILY_NAME, mName); | |
477 | return ret; | |
478 | } | |
479 | ||
480 | virtual int handleResponse(WifiEvent& reply) { | |
481 | ||
482 | ALOGE("handling reponse in %s", __func__); | |
483 | ||
484 | struct nlattr **tb = reply.attributes(); | |
7753f181 DD |
485 | struct nlattr *mcgrp = NULL; |
486 | int i; | |
487 | ||
488 | if (!tb[CTRL_ATTR_MCAST_GROUPS]) { | |
489 | ALOGE("No multicast groups found"); | |
490 | return NL_SKIP; | |
491 | } else { | |
492 | ALOGE("Multicast groups attr size = %d", nla_len(tb[CTRL_ATTR_MCAST_GROUPS])); | |
493 | } | |
494 | ||
495 | for_each_attr(mcgrp, tb[CTRL_ATTR_MCAST_GROUPS], i) { | |
496 | ||
497 | ALOGE("Processing group"); | |
498 | struct nlattr *tb2[CTRL_ATTR_MCAST_GRP_MAX + 1]; | |
499 | nla_parse(tb2, CTRL_ATTR_MCAST_GRP_MAX, (nlattr *)nla_data(mcgrp), | |
500 | nla_len(mcgrp), NULL); | |
501 | if (!tb2[CTRL_ATTR_MCAST_GRP_NAME] || !tb2[CTRL_ATTR_MCAST_GRP_ID]) { | |
502 | continue; | |
503 | } | |
504 | ||
505 | char *grpName = (char *)nla_data(tb2[CTRL_ATTR_MCAST_GRP_NAME]); | |
506 | int grpNameLen = nla_len(tb2[CTRL_ATTR_MCAST_GRP_NAME]); | |
507 | ||
508 | ALOGE("Found group name %s", grpName); | |
509 | ||
510 | if (strncmp(grpName, mGroup, grpNameLen) != 0) | |
511 | continue; | |
512 | ||
513 | mId = nla_get_u32(tb2[CTRL_ATTR_MCAST_GRP_ID]); | |
514 | break; | |
515 | } | |
516 | ||
517 | return NL_SKIP; | |
518 | } | |
519 | ||
520 | }; | |
521 | ||
522 | class SetPnoMacAddrOuiCommand : public WifiCommand { | |
523 | ||
524 | private: | |
525 | byte *mOui; | |
526 | feature_set *fset; | |
527 | feature_set *feature_matrix; | |
528 | int *fm_size; | |
529 | int set_size_max; | |
530 | public: | |
531 | SetPnoMacAddrOuiCommand(wifi_interface_handle handle, oui scan_oui) | |
532 | : WifiCommand(handle, 0) | |
533 | { | |
534 | mOui = scan_oui; | |
535 | } | |
536 | ||
537 | int createRequest(WifiRequest& request, int subcmd, byte *scan_oui) { | |
538 | int result = request.create(GOOGLE_OUI, subcmd); | |
539 | if (result < 0) { | |
540 | return result; | |
541 | } | |
542 | ||
543 | nlattr *data = request.attr_start(NL80211_ATTR_VENDOR_DATA); | |
544 | result = request.put(ANDR_WIFI_ATTRIBUTE_PNO_RANDOM_MAC_OUI, scan_oui, DOT11_OUI_LEN); | |
545 | if (result < 0) { | |
546 | return result; | |
547 | } | |
548 | ||
549 | request.attr_end(data); | |
550 | return WIFI_SUCCESS; | |
551 | ||
552 | } | |
553 | ||
554 | int start() { | |
555 | ALOGD("Sending mac address OUI"); | |
556 | WifiRequest request(familyId(), ifaceId()); | |
557 | int result = createRequest(request, SLSI_NL80211_VENDOR_SUBCMD_SET_GSCAN_OUI, mOui); | |
558 | if (result != WIFI_SUCCESS) { | |
559 | ALOGE("failed to create request; result = %d", result); | |
560 | return result; | |
561 | } | |
562 | ||
563 | result = requestResponse(request); | |
564 | if (result != WIFI_SUCCESS) { | |
565 | ALOGE("failed to set scanning mac OUI; result = %d", result); | |
566 | } | |
567 | ||
568 | return result; | |
569 | } | |
570 | protected: | |
571 | virtual int handleResponse(WifiEvent& reply) { | |
572 | ALOGD("Request complete!"); | |
573 | /* Nothing to do on response! */ | |
574 | return NL_SKIP; | |
575 | } | |
576 | }; | |
577 | ||
578 | class SetNodfsCommand : public WifiCommand { | |
579 | ||
580 | private: | |
581 | u32 mNoDfs; | |
582 | public: | |
583 | SetNodfsCommand(wifi_interface_handle handle, u32 nodfs) | |
584 | : WifiCommand(handle, 0) { | |
585 | mNoDfs = nodfs; | |
586 | } | |
587 | virtual int create() { | |
588 | int ret; | |
589 | ||
590 | ret = mMsg.create(GOOGLE_OUI, SLSI_NL80211_VENDOR_SUBCMD_SET_NODFS); | |
591 | if (ret < 0) { | |
592 | ALOGE("Can't create message to send to driver - %d", ret); | |
593 | return ret; | |
594 | } | |
595 | ||
596 | nlattr *data = mMsg.attr_start(NL80211_ATTR_VENDOR_DATA); | |
597 | ret = mMsg.put_u32(ATTR_NODFS_VALUE, mNoDfs); | |
598 | if (ret < 0) { | |
599 | return ret; | |
600 | } | |
601 | ||
602 | mMsg.attr_end(data); | |
603 | return WIFI_SUCCESS; | |
604 | } | |
605 | }; | |
606 | ||
607 | static int wifi_get_multicast_id(wifi_handle handle, const char *name, const char *group) | |
608 | { | |
609 | GetMulticastIdCommand cmd(handle, name, group); | |
610 | int res = cmd.requestResponse(); | |
611 | if (res < 0) | |
612 | return res; | |
613 | else | |
614 | return cmd.getId(); | |
615 | } | |
616 | ||
617 | ///////////////////////////////////////////////////////////////////////// | |
618 | ||
619 | static bool is_wifi_interface(const char *name) | |
620 | { | |
621 | if (strncmp(name, "wlan", 4) != 0 && strncmp(name, "p2p", 3) != 0) { | |
622 | /* not a wifi interface; ignore it */ | |
623 | return false; | |
624 | } else { | |
625 | return true; | |
626 | } | |
627 | } | |
628 | ||
629 | static int get_interface(const char *name, interface_info *info) | |
630 | { | |
631 | strcpy(info->name, name); | |
632 | info->id = if_nametoindex(name); | |
633 | ALOGI("found an interface : %s, id = %d", name, info->id); | |
634 | return WIFI_SUCCESS; | |
635 | } | |
636 | ||
637 | wifi_error wifi_init_interfaces(wifi_handle handle) | |
638 | { | |
639 | hal_info *info = (hal_info *)handle; | |
640 | ALOGD("wifi_init_interfaces, info = %p", info); | |
641 | ||
642 | struct dirent *de; | |
643 | ||
644 | DIR *d = opendir("/sys/class/net"); | |
645 | if (d == 0) | |
646 | return WIFI_ERROR_UNKNOWN; | |
647 | ||
648 | int n = 0; | |
649 | while ((de = readdir(d))) { | |
650 | if (de->d_name[0] == '.') | |
651 | continue; | |
652 | if (is_wifi_interface(de->d_name) ) { | |
653 | n++; | |
654 | } | |
655 | } | |
656 | ||
657 | closedir(d); | |
658 | ||
659 | d = opendir("/sys/class/net"); | |
660 | if (d == 0) | |
661 | return WIFI_ERROR_UNKNOWN; | |
662 | ||
663 | info->interfaces = (interface_info **)malloc(sizeof(interface_info *) * n); | |
664 | ||
665 | int i = 0; | |
666 | while ((de = readdir(d))) { | |
667 | if (de->d_name[0] == '.') | |
668 | continue; | |
669 | if (is_wifi_interface(de->d_name)) { | |
670 | interface_info *ifinfo = (interface_info *)malloc(sizeof(interface_info)); | |
671 | if (get_interface(de->d_name, ifinfo) != WIFI_SUCCESS) { | |
672 | free(ifinfo); | |
673 | continue; | |
674 | } | |
675 | ifinfo->handle = handle; | |
676 | info->interfaces[i] = ifinfo; | |
677 | i++; | |
678 | } | |
679 | } | |
680 | ||
681 | closedir(d); | |
682 | ||
683 | info->num_interfaces = n; | |
684 | return WIFI_SUCCESS; | |
685 | } | |
686 | ||
687 | wifi_error wifi_get_ifaces(wifi_handle handle, int *num, wifi_interface_handle **interfaces) | |
688 | { | |
689 | hal_info *info = (hal_info *)handle; | |
690 | ||
691 | *interfaces = (wifi_interface_handle *)info->interfaces; | |
692 | *num = info->num_interfaces; | |
693 | ||
694 | return WIFI_SUCCESS; | |
695 | } | |
696 | ||
697 | wifi_error wifi_get_iface_name(wifi_interface_handle handle, char *name, size_t size) | |
698 | { | |
699 | interface_info *info = (interface_info *)handle; | |
700 | strcpy(name, info->name); | |
701 | return WIFI_SUCCESS; | |
702 | } | |
703 | ||
704 | wifi_error wifi_get_supported_feature_set(wifi_interface_handle handle, feature_set *set) | |
705 | { | |
706 | return WIFI_ERROR_NOT_SUPPORTED; | |
707 | } | |
708 | ||
709 | wifi_error wifi_get_concurrency_matrix(wifi_interface_handle handle, int set_size_max, | |
710 | feature_set set[], int *set_size) | |
711 | { | |
712 | return WIFI_ERROR_NOT_SUPPORTED; | |
713 | } | |
714 | ||
715 | wifi_error wifi_set_scanning_mac_oui(wifi_interface_handle handle, oui scan_oui) | |
716 | { | |
717 | SetPnoMacAddrOuiCommand command(handle, scan_oui); | |
718 | return (wifi_error)command.start(); | |
719 | ||
720 | } | |
721 | ||
722 | wifi_error wifi_set_nodfs_flag(wifi_interface_handle handle, u32 nodfs) | |
723 | { | |
724 | SetNodfsCommand command(handle, nodfs); | |
725 | return (wifi_error) command.requestResponse(); | |
726 | } | |
727 | ||
728 | ///////////////////////////////////////////////////////////////////////////// |