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