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