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>
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>
27 #define LOG_TAG "WifiHAL"
29 #include <utils/Log.h>
32 #include "cpp_bindings.h"
36 #define WIFI_HAL_CMD_SOCK_PORT 644
37 #define WIFI_HAL_EVENT_SOCK_PORT 645
40 #define FEATURE_SET_MATRIX 1
41 #define ATTR_NODFS_VALUE 3
42 #define ATTR_COUNTRY_CODE 4
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
);
51 typedef enum wifi_attr
{
52 ANDR_WIFI_ATTRIBUTE_ND_OFFLOAD_CONFIG
,
53 ANDR_WIFI_ATTRIBUTE_PNO_RANDOM_MAC_OUI
56 enum wifi_rssi_monitor_attr
{
57 RSSI_MONITOR_ATTRIBUTE_MAX_RSSI
,
58 RSSI_MONITOR_ATTRIBUTE_MIN_RSSI
,
59 RSSI_MONITOR_ATTRIBUTE_START
,
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
);
65 wifi_error
wifi_get_wake_reason_stats(wifi_interface_handle iface
, WLAN_DRIVER_WAKE_REASON_CNT
*wifi_wake_reason_cnt
);
67 /* Initialize/Cleanup */
69 void wifi_socket_set_local_port(struct nl_sock
*sock
, uint32_t port
)
71 uint32_t pid
= getpid() & 0x3FFFFF;
72 nl_socket_set_local_port(sock
, pid
+ (port
<< 22));
75 class SetNdoffloadCommand
: public WifiCommand
{
80 SetNdoffloadCommand(wifi_interface_handle handle
, u8 enable
)
81 : WifiCommand(handle
, 0) {
84 virtual int create() {
87 ret
= mMsg
.create(GOOGLE_OUI
, SLSI_NL80211_VENDOR_SUBCMD_CONFIGURE_ND_OFFLOAD
);
89 ALOGE("Can't create message to send to driver - %d", ret
);
90 return WIFI_ERROR_NOT_AVAILABLE
;
93 nlattr
*data
= mMsg
.attr_start(NL80211_ATTR_VENDOR_DATA
);
94 ret
= mMsg
.put_u8(ANDR_WIFI_ATTRIBUTE_ND_OFFLOAD_CONFIG
, mEnable
);
98 ALOGD("Driver message has been created successfully--> %d", mEnable
);
104 static nl_sock
* wifi_create_nl_socket(int port
)
106 struct nl_sock
*sock
= nl_socket_alloc();
108 ALOGE("Could not create handle");
112 wifi_socket_set_local_port(sock
, port
);
114 if (nl_connect(sock
, NETLINK_GENERIC
)) {
115 ALOGE("Could not connect handle");
116 nl_socket_free(sock
);
124 wifi_error
wifi_configure_nd_offload(wifi_interface_handle handle
, u8 enable
)
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
);
134 return (wifi_error
)ret
;
137 wifi_error
wifi_get_packet_filter_capabilities(wifi_interface_handle handle
,
138 u32
*version
, u32
*max_len
)
140 /*Return success to pass VTS test.*/
141 ALOGD("Packet filter not supported");
149 /* Initialize HAL function pointer table */
150 wifi_error
init_wifi_vendor_hal_func_table(wifi_hal_fn
*fn
)
153 return WIFI_ERROR_UNKNOWN
;
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
;
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
;
176 fn
->wifi_start_sending_offloaded_packet
= wifi_start_sending_offloaded_packet
;
177 fn
->wifi_stop_sending_offloaded_packet
= wifi_stop_sending_offloaded_packet
;
178 fn
->wifi_set_epno_list
= wifi_set_epno_list
;
179 fn
->wifi_reset_epno_list
= wifi_reset_epno_list
;
180 fn
->wifi_set_passpoint_list
= wifi_set_passpoint_list
;
181 fn
->wifi_reset_passpoint_list
= wifi_reset_passpoint_list
;
182 fn
->wifi_start_rssi_monitoring
= wifi_start_rssi_monitoring
;
183 fn
->wifi_stop_rssi_monitoring
= wifi_stop_rssi_monitoring
;
184 fn
->wifi_set_link_stats
= wifi_set_link_stats
;
185 fn
->wifi_get_link_stats
= wifi_get_link_stats
;
186 fn
->wifi_clear_link_stats
= wifi_clear_link_stats
;
187 fn
->wifi_set_country_code
= wifi_set_country_code
;
188 fn
->wifi_configure_roaming
= wifi_configure_roaming
;
189 fn
->wifi_configure_nd_offload
= wifi_configure_nd_offload
;
190 fn
->wifi_get_packet_filter_capabilities
= wifi_get_packet_filter_capabilities
;
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
;
209 wifi_error
wifi_initialize(wifi_handle
*handle
)
213 ALOGI("Initializing wifi");
214 hal_info
*info
= (hal_info
*)malloc(sizeof(hal_info
));
216 ALOGE("Could not allocate hal_info");
217 return WIFI_ERROR_UNKNOWN
;
220 memset(info
, 0, sizeof(*info
));
222 if (socketpair(AF_UNIX
, SOCK_STREAM
, 0, info
->cleanup_socks
) == -1) {
223 ALOGE("Could not create cleanup sockets");
225 return WIFI_ERROR_UNKNOWN
;
228 struct nl_sock
*cmd_sock
= wifi_create_nl_socket(WIFI_HAL_CMD_SOCK_PORT
);
229 if (cmd_sock
== NULL
) {
230 ALOGE("Could not create handle");
232 return WIFI_ERROR_UNKNOWN
;
235 struct nl_sock
*event_sock
= wifi_create_nl_socket(WIFI_HAL_EVENT_SOCK_PORT
);
236 if (event_sock
== NULL
) {
237 ALOGE("Could not create handle");
238 nl_socket_free(cmd_sock
);
240 return WIFI_ERROR_UNKNOWN
;
243 struct nl_cb
*cb
= nl_socket_get_cb(event_sock
);
245 ALOGE("Could not create handle");
246 nl_socket_free(cmd_sock
);
247 nl_socket_free(event_sock
);
249 return WIFI_ERROR_UNKNOWN
;
252 // ALOGI("cb->refcnt = %d", cb->cb_refcnt);
253 nl_cb_set(cb
, NL_CB_SEQ_CHECK
, NL_CB_CUSTOM
, internal_no_seq_check
, info
);
254 nl_cb_set(cb
, NL_CB_VALID
, NL_CB_CUSTOM
, internal_valid_message_handler
, info
);
257 info
->cmd_sock
= cmd_sock
;
258 info
->event_sock
= event_sock
;
259 info
->clean_up
= false;
260 info
->in_event_loop
= false;
262 info
->event_cb
= (cb_info
*)malloc(sizeof(cb_info
) * DEFAULT_EVENT_CB_SIZE
);
263 info
->alloc_event_cb
= DEFAULT_EVENT_CB_SIZE
;
264 info
->num_event_cb
= 0;
266 info
->cmd
= (cmd_info
*)malloc(sizeof(cmd_info
) * DEFAULT_CMD_SIZE
);
267 info
->alloc_cmd
= DEFAULT_CMD_SIZE
;
270 info
->nl80211_family_id
= genl_ctrl_resolve(cmd_sock
, "nl80211");
271 if (info
->nl80211_family_id
< 0) {
272 ALOGE("Could not resolve nl80211 familty id");
273 nl_socket_free(cmd_sock
);
274 nl_socket_free(event_sock
);
276 return WIFI_ERROR_UNKNOWN
;
279 pthread_mutex_init(&info
->cb_lock
, NULL
);
281 *handle
= (wifi_handle
) info
;
283 wifi_add_membership(*handle
, "scan");
284 wifi_add_membership(*handle
, "mlme");
285 wifi_add_membership(*handle
, "regulatory");
286 wifi_add_membership(*handle
, "vendor");
288 wifi_init_interfaces(*handle
);
289 char intf_name_buff
[10 * 10 + 4]; /* Randomly choosen max interface 10. each interface name max 9 + 1(for space) */
290 char *pos
= intf_name_buff
;
291 for (int i
= 0; i
< (info
->num_interfaces
< 10 ? info
->num_interfaces
: 10); i
++) {
292 strncpy(pos
, info
->interfaces
[i
]->name
, sizeof(intf_name_buff
) - (pos
- intf_name_buff
));
295 if (info
->num_interfaces
> 10) {
296 strncpy(pos
, "...", 3);
299 ALOGD("Found %d interfaces[%s]. Initialized Wifi HAL Successfully", info
->num_interfaces
, intf_name_buff
);
304 static int wifi_add_membership(wifi_handle handle
, const char *group
)
306 hal_info
*info
= getHalInfo(handle
);
308 int id
= wifi_get_multicast_id(handle
, "nl80211", group
);
310 ALOGE("Could not find group %s", group
);
314 int ret
= nl_socket_add_membership(info
->event_sock
, id
);
316 ALOGE("Could not add membership to group %s", group
);
321 static void internal_cleaned_up_handler(wifi_handle handle
)
323 hal_info
*info
= getHalInfo(handle
);
324 wifi_cleaned_up_handler cleaned_up_handler
= info
->cleaned_up_handler
;
326 if (info
->cmd_sock
!= 0) {
327 close(info
->cleanup_socks
[0]);
328 close(info
->cleanup_socks
[1]);
329 nl_socket_free(info
->cmd_sock
);
330 nl_socket_free(info
->event_sock
);
331 info
->cmd_sock
= NULL
;
332 info
->event_sock
= NULL
;
335 (*cleaned_up_handler
)(handle
);
336 pthread_mutex_destroy(&info
->cb_lock
);
340 void wifi_cleanup(wifi_handle handle
, wifi_cleaned_up_handler handler
)
342 hal_info
*info
= getHalInfo(handle
);
345 info
->cleaned_up_handler
= handler
;
346 if (write(info
->cleanup_socks
[0], "Exit", 4) < 1) {
347 ALOGE("could not write to the cleanup socket");
349 // Listen to the response
350 // Hopefully we dont get errors or get hung up
351 // Not much can be done in that case, but assume that
352 // it has rx'ed the Exit message to exit the thread.
353 // As a fallback set the cleanup flag to TRUE
354 memset(buf
, 0, sizeof(buf
));
355 int result
= read(info
->cleanup_socks
[0], buf
, sizeof(buf
));
356 ALOGE("%s: Read after POLL returned %d, error no = %d", __FUNCTION__
, result
, errno
);
357 if (strncmp(buf
, "Done", 4) != 0) {
358 ALOGD("Rx'ed %s", buf
);
361 info
->clean_up
= true;
362 pthread_mutex_lock(&info
->cb_lock
);
364 int bad_commands
= 0;
366 while (info
->num_cmd
> bad_commands
) {
367 int num_cmd
= info
->num_cmd
;
368 cmd_info
*cmdi
= &(info
->cmd
[bad_commands
]);
369 WifiCommand
*cmd
= cmdi
->cmd
;
371 pthread_mutex_unlock(&info
->cb_lock
);
373 pthread_mutex_lock(&info
->cb_lock
);
374 /* release reference added when command is saved */
376 if (num_cmd
== info
->num_cmd
) {
382 for (int i
= 0; i
< info
->num_event_cb
; i
++) {
383 cb_info
*cbi
= &(info
->event_cb
[i
]);
384 WifiCommand
*cmd
= (WifiCommand
*)cbi
->cb_arg
;
385 ALOGE("Leaked command %p", cmd
);
387 pthread_mutex_unlock(&info
->cb_lock
);
388 internal_cleaned_up_handler(handle
);
391 static int internal_pollin_handler(wifi_handle handle
)
393 hal_info
*info
= getHalInfo(handle
);
394 struct nl_cb
*cb
= nl_socket_get_cb(info
->event_sock
);
395 int res
= nl_recvmsgs(info
->event_sock
, cb
);
400 /* Run event handler */
401 void wifi_event_loop(wifi_handle handle
)
403 hal_info
*info
= getHalInfo(handle
);
404 if (info
->in_event_loop
) {
407 info
->in_event_loop
= true;
411 memset(&pfd
[0], 0, sizeof(pollfd
) * 2);
413 pfd
[0].fd
= nl_socket_get_fd(info
->event_sock
);
414 pfd
[0].events
= POLLIN
;
415 pfd
[1].fd
= info
->cleanup_socks
[1];
416 pfd
[1].events
= POLLIN
;
421 int timeout
= -1; /* Infinite timeout */
424 int result
= poll(pfd
, 2, timeout
);
426 } else if (pfd
[0].revents
& POLLERR
) {
427 int prev_err
= (int)errno
;
428 int result2
= read(pfd
[0].fd
, buf
, sizeof(buf
));
429 ALOGE("Poll err:%d | Read after POLL returned %d, error no = %d", prev_err
, result2
, errno
);
430 } else if (pfd
[0].revents
& POLLHUP
) {
431 ALOGE("Remote side hung up");
433 } else if (pfd
[0].revents
& POLLIN
) {
434 internal_pollin_handler(handle
);
435 } else if (pfd
[1].revents
& POLLIN
) {
436 memset(buf
, 0, sizeof(buf
));
437 int result2
= read(pfd
[1].fd
, buf
, sizeof(buf
));
438 ALOGE("%s: Read after POLL returned %d, error no = %d", __FUNCTION__
, result2
, errno
);
439 if (strncmp(buf
, "Exit", 4) == 0) {
440 ALOGD("Got a signal to exit!!!");
441 if (write(pfd
[1].fd
, "Done", 4) < 1) {
442 ALOGE("could not write to the cleanup socket");
446 ALOGD("Rx'ed %s on the cleanup socket\n", buf
);
449 ALOGE("Unknown event - %0x, %0x", pfd
[0].revents
, pfd
[1].revents
);
451 } while (!info
->clean_up
);
454 ///////////////////////////////////////////////////////////////////////////////////////
456 static int internal_no_seq_check(struct nl_msg
*msg
, void *arg
)
461 static int internal_valid_message_handler(nl_msg
*msg
, void *arg
)
463 wifi_handle handle
= (wifi_handle
)arg
;
464 hal_info
*info
= getHalInfo(handle
);
466 WifiEvent
event(msg
);
467 int res
= event
.parse();
469 ALOGE("Failed to parse event: %d", res
);
473 int cmd
= event
.get_cmd();
474 uint32_t vendor_id
= 0;
477 if (cmd
== NL80211_CMD_VENDOR
) {
478 vendor_id
= event
.get_u32(NL80211_ATTR_VENDOR_ID
);
479 subcmd
= event
.get_u32(NL80211_ATTR_VENDOR_SUBCMD
);
481 ALOGI("event received %s, vendor_id = 0x%0x, subcmd = 0x%0x",
482 event.get_cmdString(), vendor_id, subcmd);*/
485 //ALOGI("event received %s, vendor_id = 0x%0x", event.get_cmdString(), vendor_id);
488 pthread_mutex_lock(&info
->cb_lock
);
490 for (int i
= 0; i
< info
->num_event_cb
; i
++) {
491 if (cmd
== info
->event_cb
[i
].nl_cmd
) {
492 if (cmd
== NL80211_CMD_VENDOR
493 && ((vendor_id
!= info
->event_cb
[i
].vendor_id
)
494 || (subcmd
!= info
->event_cb
[i
].vendor_subcmd
)))
496 /* event for a different vendor, ignore it */
500 cb_info
*cbi
= &(info
->event_cb
[i
]);
501 nl_recvmsg_msg_cb_t cb_func
= cbi
->cb_func
;
502 void *cb_arg
= cbi
->cb_arg
;
503 WifiCommand
*cmd
= (WifiCommand
*)cbi
->cb_arg
;
508 pthread_mutex_unlock(&info
->cb_lock
);
510 (*cb_func
)(msg
, cb_arg
);
519 pthread_mutex_unlock(&info
->cb_lock
);
523 ///////////////////////////////////////////////////////////////////////////////////////
525 class GetMulticastIdCommand
: public WifiCommand
532 GetMulticastIdCommand(wifi_handle handle
, const char *name
, const char *group
)
533 : WifiCommand(handle
, 0)
544 virtual int create() {
545 int nlctrlFamily
= genl_ctrl_resolve(mInfo
->cmd_sock
, "nlctrl");
546 int ret
= mMsg
.create(nlctrlFamily
, CTRL_CMD_GETFAMILY
, 0, 0);
550 ret
= mMsg
.put_string(CTRL_ATTR_FAMILY_NAME
, mName
);
554 virtual int handleResponse(WifiEvent
& reply
) {
555 struct nlattr
**tb
= reply
.attributes();
556 struct nlattr
*mcgrp
= NULL
;
559 if (!tb
[CTRL_ATTR_MCAST_GROUPS
]) {
560 ALOGE("No multicast groups found");
564 for_each_attr(mcgrp
, tb
[CTRL_ATTR_MCAST_GROUPS
], i
) {
566 struct nlattr
*tb2
[CTRL_ATTR_MCAST_GRP_MAX
+ 1];
567 nla_parse(tb2
, CTRL_ATTR_MCAST_GRP_MAX
, (nlattr
*)nla_data(mcgrp
),
568 nla_len(mcgrp
), NULL
);
569 if (!tb2
[CTRL_ATTR_MCAST_GRP_NAME
] || !tb2
[CTRL_ATTR_MCAST_GRP_ID
]) {
573 char *grpName
= (char *)nla_data(tb2
[CTRL_ATTR_MCAST_GRP_NAME
]);
574 int grpNameLen
= nla_len(tb2
[CTRL_ATTR_MCAST_GRP_NAME
]);
576 if (strncmp(grpName
, mGroup
, grpNameLen
) != 0)
579 mId
= nla_get_u32(tb2
[CTRL_ATTR_MCAST_GRP_ID
]);
588 class SetPnoMacAddrOuiCommand
: public WifiCommand
{
593 feature_set
*feature_matrix
;
597 SetPnoMacAddrOuiCommand(wifi_interface_handle handle
, oui scan_oui
)
598 : WifiCommand(handle
, 0)
602 feature_matrix
= NULL
;
607 int createRequest(WifiRequest
& request
, int subcmd
, byte
*scan_oui
) {
608 int result
= request
.create(GOOGLE_OUI
, subcmd
);
613 nlattr
*data
= request
.attr_start(NL80211_ATTR_VENDOR_DATA
);
614 result
= request
.put(ANDR_WIFI_ATTRIBUTE_PNO_RANDOM_MAC_OUI
, scan_oui
, DOT11_OUI_LEN
);
619 request
.attr_end(data
);
625 WifiRequest
request(familyId(), ifaceId());
626 int result
= createRequest(request
, SLSI_NL80211_VENDOR_SUBCMD_SET_GSCAN_OUI
, mOui
);
627 if (result
!= WIFI_SUCCESS
) {
628 ALOGE("failed to create request; result = %d", result
);
632 result
= requestResponse(request
);
633 if (result
!= WIFI_SUCCESS
) {
634 ALOGE("failed to set scanning mac OUI; result = %d", result
);
640 virtual int handleResponse(WifiEvent
& reply
) {
641 /* Nothing to do on response! */
646 class SetNodfsCommand
: public WifiCommand
{
651 SetNodfsCommand(wifi_interface_handle handle
, u32 nodfs
)
652 : WifiCommand(handle
, 0) {
655 virtual int create() {
658 ret
= mMsg
.create(GOOGLE_OUI
, SLSI_NL80211_VENDOR_SUBCMD_SET_NODFS
);
660 ALOGE("Can't create message to send to driver - %d", ret
);
664 nlattr
*data
= mMsg
.attr_start(NL80211_ATTR_VENDOR_DATA
);
665 ret
= mMsg
.put_u32(ATTR_NODFS_VALUE
, mNoDfs
);
675 class SetRSSIMonitorCommand
: public WifiCommand
{
679 wifi_rssi_event_handler mHandler
;
681 SetRSSIMonitorCommand(wifi_request_id id
, wifi_interface_handle handle
,
682 s8 max_rssi
, s8 min_rssi
, wifi_rssi_event_handler eh
)
683 : WifiCommand(handle
, id
), mMax_rssi(max_rssi
), mMin_rssi
684 (min_rssi
), mHandler(eh
)
687 int createRequest(WifiRequest
& request
, int enable
) {
688 int result
= request
.create(GOOGLE_OUI
, SLSI_NL80211_VENDOR_SUBCMD_SET_RSSI_MONITOR
);
693 nlattr
*data
= request
.attr_start(NL80211_ATTR_VENDOR_DATA
);
694 result
= request
.put_u8(RSSI_MONITOR_ATTRIBUTE_MAX_RSSI
, (enable
? mMax_rssi
: 0));
699 result
= request
.put_u8(RSSI_MONITOR_ATTRIBUTE_MIN_RSSI
, (enable
? mMin_rssi
: 0));
703 result
= request
.put_u8(RSSI_MONITOR_ATTRIBUTE_START
, enable
);
707 request
.attr_end(data
);
712 WifiRequest
request(familyId(), ifaceId());
713 int result
= createRequest(request
, 1);
717 result
= requestResponse(request
);
719 ALOGI("Failed to set RSSI Monitor, result = %d", result
);
722 ALOGI("Successfully set RSSI monitoring");
723 registerVendorHandler(GOOGLE_OUI
, WIFI_RSSI_REPORT_EVENT
);
728 virtual int cancel() {
730 WifiRequest
request(familyId(), ifaceId());
731 int result
= createRequest(request
, 0);
732 if (result
!= WIFI_SUCCESS
) {
733 ALOGE("failed to create request; result = %d", result
);
735 result
= requestResponse(request
);
736 if (result
!= WIFI_SUCCESS
) {
737 ALOGE("failed to stop RSSI monitoring = %d", result
);
740 unregisterVendorHandler(GOOGLE_OUI
, WIFI_RSSI_REPORT_EVENT
);
744 virtual int handleResponse(WifiEvent
& reply
) {
745 /* Nothing to do on response! */
749 virtual int handleEvent(WifiEvent
& event
) {
751 nlattr
*vendor_data
= event
.get_attribute(NL80211_ATTR_VENDOR_DATA
);
752 int len
= event
.get_vendor_data_len();
754 if (vendor_data
== NULL
|| len
== 0) {
755 ALOGI("RSSI monitor: No data");
764 rssi_monitor_evt
*data
= (rssi_monitor_evt
*)event
.get_vendor_data();
766 if (*mHandler
.on_rssi_threshold_breached
) {
767 (*mHandler
.on_rssi_threshold_breached
)(id(), data
->BSSID
, data
->cur_rssi
);
769 ALOGW("No RSSI monitor handler registered");
777 class SetCountryCodeCommand
: public WifiCommand
{
779 const char *mCountryCode
;
781 SetCountryCodeCommand(wifi_interface_handle handle
, const char *country_code
)
782 : WifiCommand(handle
, 0) {
783 mCountryCode
= country_code
;
785 virtual int create() {
788 ret
= mMsg
.create(GOOGLE_OUI
, SLSI_NL80211_VENDOR_SUBCMD_SET_COUNTRY_CODE
);
790 ALOGE("Can't create message to send to driver - %d", ret
);
794 nlattr
*data
= mMsg
.attr_start(NL80211_ATTR_VENDOR_DATA
);
795 ret
= mMsg
.put_string(ATTR_COUNTRY_CODE
, mCountryCode
);
806 class GetFeatureSetCommand
: public WifiCommand
{
813 GetFeatureSetCommand(wifi_interface_handle handle
, feature_set
*set
)
814 : WifiCommand(handle
, 0)
819 virtual int create() {
822 ret
= mMsg
.create(GOOGLE_OUI
, SLSI_NL80211_VENDOR_SUBCMD_GET_FEATURE_SET
);
824 ALOGE("create failed - %d", ret
);
831 virtual int handleResponse(WifiEvent
& reply
) {
833 int id
= reply
.get_vendor_id();
834 int subcmd
= reply
.get_vendor_subcmd();
836 if (reply
.get_cmd() != NL80211_CMD_VENDOR
) {
837 ALOGD("Ignore reply; cmd = %d", reply
.get_cmd());
841 nlattr
*vendor_data
= reply
.get_attribute(NL80211_ATTR_VENDOR_DATA
);
842 int len
= reply
.get_vendor_data_len();
844 if (vendor_data
== NULL
|| len
== 0) {
845 ALOGE("vendor data in GetFeatureSetCommand missing!!");
849 void *data
= reply
.get_vendor_data();
851 ALOGE("feature_set Pointer not set");
854 memcpy(fset
, data
, min(len
, (int) sizeof(*fset
)));
862 static int wifi_get_multicast_id(wifi_handle handle
, const char *name
, const char *group
)
864 GetMulticastIdCommand
cmd(handle
, name
, group
);
865 int res
= cmd
.requestResponse();
872 /////////////////////////////////////////////////////////////////////////
874 static bool is_wifi_interface(const char *name
)
876 if (strncmp(name
, "wlan", 4) != 0 && strncmp(name
, "p2p", 3) != 0) {
877 /* not a wifi interface; ignore it */
884 static int get_interface(const char *name
, interface_info
*info
)
886 strcpy(info
->name
, name
);
887 info
->id
= if_nametoindex(name
);
891 wifi_error
wifi_init_interfaces(wifi_handle handle
)
893 hal_info
*info
= (hal_info
*)handle
;
896 DIR *d
= opendir("/sys/class/net");
898 return WIFI_ERROR_UNKNOWN
;
901 while ((de
= readdir(d
))) {
902 if (de
->d_name
[0] == '.')
904 if (is_wifi_interface(de
->d_name
) ) {
911 d
= opendir("/sys/class/net");
913 return WIFI_ERROR_UNKNOWN
;
915 info
->interfaces
= (interface_info
**)malloc(sizeof(interface_info
*) * n
);
918 while ((de
= readdir(d
))) {
919 if (de
->d_name
[0] == '.')
921 if (is_wifi_interface(de
->d_name
)) {
922 interface_info
*ifinfo
= (interface_info
*)malloc(sizeof(interface_info
));
923 if (get_interface(de
->d_name
, ifinfo
) != WIFI_SUCCESS
) {
927 ifinfo
->handle
= handle
;
928 info
->interfaces
[i
] = ifinfo
;
935 info
->num_interfaces
= n
;
939 wifi_error
wifi_get_ifaces(wifi_handle handle
, int *num
, wifi_interface_handle
**interfaces
)
941 hal_info
*info
= (hal_info
*)handle
;
943 *interfaces
= (wifi_interface_handle
*)info
->interfaces
;
944 *num
= info
->num_interfaces
;
949 wifi_error
wifi_get_iface_name(wifi_interface_handle handle
, char *name
, size_t size
)
951 interface_info
*info
= (interface_info
*)handle
;
952 strcpy(name
, info
->name
);
956 wifi_error
wifi_get_concurrency_matrix(wifi_interface_handle handle
, int set_size_max
,
957 feature_set set
[], int *set_size
)
959 return WIFI_ERROR_NOT_SUPPORTED
;
962 wifi_error
wifi_set_scanning_mac_oui(wifi_interface_handle handle
, oui scan_oui
)
964 SetPnoMacAddrOuiCommand
command(handle
, scan_oui
);
965 return (wifi_error
)command
.start();
969 wifi_error
wifi_set_nodfs_flag(wifi_interface_handle handle
, u32 nodfs
)
971 SetNodfsCommand
command(handle
, nodfs
);
972 return (wifi_error
) command
.requestResponse();
975 static wifi_error
wifi_start_rssi_monitoring(wifi_request_id id
, wifi_interface_handle
976 iface
, s8 max_rssi
, s8 min_rssi
, wifi_rssi_event_handler eh
)
978 ALOGD("Start RSSI monitor %d", id
);
979 wifi_handle handle
= getWifiHandle(iface
);
980 SetRSSIMonitorCommand
*cmd
= new SetRSSIMonitorCommand(id
, iface
, max_rssi
, min_rssi
, eh
);
981 wifi_register_cmd(handle
, id
, cmd
);
983 wifi_error result
= (wifi_error
)cmd
->start();
984 if (result
!= WIFI_SUCCESS
) {
985 wifi_unregister_cmd(handle
, id
);
991 static wifi_error
wifi_stop_rssi_monitoring(wifi_request_id id
, wifi_interface_handle iface
)
993 ALOGD("Stopping RSSI monitor");
996 wifi_rssi_event_handler handler
;
997 wifi_handle handle
= getWifiHandle(iface
);
998 memset(&handler
, 0, sizeof(handler
));
999 SetRSSIMonitorCommand
*cmd
= new SetRSSIMonitorCommand(id
, iface
,
1003 return WIFI_SUCCESS
;
1005 return wifi_cancel_cmd(id
, iface
);
1008 wifi_error
wifi_get_supported_feature_set(wifi_interface_handle handle
, feature_set
*set
)
1010 GetFeatureSetCommand
command(handle
, set
);
1011 return (wifi_error
) command
.requestResponse();
1014 wifi_error
wifi_set_country_code(wifi_interface_handle handle
, const char *country_code
)
1016 SetCountryCodeCommand
command(handle
, country_code
);
1017 return (wifi_error
) command
.requestResponse();
1021 /////////////////////////////////////////////////////////////////////////////