4 #include <sys/socket.h>
5 #include <netlink/genl/genl.h>
6 #include <netlink/genl/family.h>
7 #include <netlink/genl/ctrl.h>
8 #include <linux/rtnetlink.h>
9 #include <netpacket/packet.h>
10 #include <linux/filter.h>
11 #include <linux/errqueue.h>
13 #include <linux/pkt_sched.h>
14 #include <netlink/object-api.h>
15 #include <netlink/netlink.h>
16 #include <netlink/socket.h>
17 #include <netlink/attr.h>
18 #include <netlink/handlers.h>
19 #include <netlink/msg.h>
26 #define LOG_TAG "WifiHAL"
28 #include <utils/Log.h>
31 #include "cpp_bindings.h"
34 #define WIFI_HAL_CMD_SOCK_PORT 644
35 #define WIFI_HAL_EVENT_SOCK_PORT 645
38 #define FEATURE_SET_MATRIX 1
39 #define ATTR_NODFS_VALUE 3
40 #define ATTR_COUNTRY_CODE 4
42 static void internal_event_handler(wifi_handle handle
, int events
);
43 static int internal_no_seq_check(nl_msg
*msg
, void *arg
);
44 static int internal_valid_message_handler(nl_msg
*msg
, void *arg
);
45 static int wifi_get_multicast_id(wifi_handle handle
, const char *name
, const char *group
);
46 static int wifi_add_membership(wifi_handle handle
, const char *group
);
47 static wifi_error
wifi_init_interfaces(wifi_handle handle
);
49 typedef enum wifi_attr
{
50 ANDR_WIFI_ATTRIBUTE_NUM_FEATURE_SET
,
51 ANDR_WIFI_ATTRIBUTE_FEATURE_SET
,
52 ANDR_WIFI_ATTRIBUTE_PNO_RANDOM_MAC_OUI
55 enum wifi_rssi_monitor_attr
{
56 RSSI_MONITOR_ATTRIBUTE_MAX_RSSI
,
57 RSSI_MONITOR_ATTRIBUTE_MIN_RSSI
,
58 RSSI_MONITOR_ATTRIBUTE_START
,
61 static wifi_error
wifi_start_rssi_monitoring(wifi_request_id id
, wifi_interface_handle
62 iface
, s8 max_rssi
, s8 min_rssi
, wifi_rssi_event_handler eh
);
63 static wifi_error
wifi_stop_rssi_monitoring(wifi_request_id id
, wifi_interface_handle iface
);
65 /* Initialize/Cleanup */
67 void wifi_socket_set_local_port(struct nl_sock
*sock
, uint32_t port
)
69 uint32_t pid
= getpid() & 0x3FFFFF;
70 nl_socket_set_local_port(sock
, pid
+ (port
<< 22));
73 static nl_sock
* wifi_create_nl_socket(int port
)
75 ALOGI("Creating socket");
76 struct nl_sock
*sock
= nl_socket_alloc();
78 ALOGE("Could not create handle");
82 wifi_socket_set_local_port(sock
, port
);
84 ALOGI("Connecting socket");
85 if (nl_connect(sock
, NETLINK_GENERIC
)) {
86 ALOGE("Could not connect handle");
94 /* Initialize HAL function pointer table */
95 wifi_error
init_wifi_vendor_hal_func_table(wifi_hal_fn
*fn
)
98 return WIFI_ERROR_UNKNOWN
;
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
;
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
;
121 fn
->wifi_start_sending_offloaded_packet
= wifi_start_sending_offloaded_packet
;
122 fn
->wifi_stop_sending_offloaded_packet
= wifi_stop_sending_offloaded_packet
;
123 fn
->wifi_set_epno_list
= wifi_set_epno_list
;
124 fn
->wifi_reset_epno_list
= wifi_reset_epno_list
;
125 fn
->wifi_set_passpoint_list
= wifi_set_passpoint_list
;
126 fn
->wifi_reset_passpoint_list
= wifi_reset_passpoint_list
;
127 fn
->wifi_set_bssid_blacklist
= wifi_set_bssid_blacklist
;
128 fn
->wifi_start_rssi_monitoring
= wifi_start_rssi_monitoring
;
129 fn
->wifi_stop_rssi_monitoring
= wifi_stop_rssi_monitoring
;
130 fn
->wifi_set_link_stats
= wifi_set_link_stats
;
131 fn
->wifi_get_link_stats
= wifi_get_link_stats
;
132 fn
->wifi_clear_link_stats
= wifi_clear_link_stats
;
133 fn
->wifi_set_country_code
= wifi_set_country_code
;
138 wifi_error
wifi_initialize(wifi_handle
*handle
)
142 ALOGI("Initializing wifi");
143 hal_info
*info
= (hal_info
*)malloc(sizeof(hal_info
));
145 ALOGE("Could not allocate hal_info");
146 return WIFI_ERROR_UNKNOWN
;
149 memset(info
, 0, sizeof(*info
));
151 ALOGI("Creating socket");
152 if (socketpair(AF_UNIX
, SOCK_STREAM
, 0, info
->cleanup_socks
) == -1) {
153 ALOGE("Could not create cleanup sockets");
155 return WIFI_ERROR_UNKNOWN
;
158 struct nl_sock
*cmd_sock
= wifi_create_nl_socket(WIFI_HAL_CMD_SOCK_PORT
);
159 if (cmd_sock
== NULL
) {
160 ALOGE("Could not create handle");
162 return WIFI_ERROR_UNKNOWN
;
165 struct nl_sock
*event_sock
= wifi_create_nl_socket(WIFI_HAL_EVENT_SOCK_PORT
);
166 if (event_sock
== NULL
) {
167 ALOGE("Could not create handle");
168 nl_socket_free(cmd_sock
);
170 return WIFI_ERROR_UNKNOWN
;
173 struct nl_cb
*cb
= nl_socket_get_cb(event_sock
);
175 ALOGE("Could not create handle");
176 nl_socket_free(cmd_sock
);
177 nl_socket_free(event_sock
);
179 return WIFI_ERROR_UNKNOWN
;
182 // ALOGI("cb->refcnt = %d", cb->cb_refcnt);
183 nl_cb_set(cb
, NL_CB_SEQ_CHECK
, NL_CB_CUSTOM
, internal_no_seq_check
, info
);
184 nl_cb_set(cb
, NL_CB_VALID
, NL_CB_CUSTOM
, internal_valid_message_handler
, info
);
187 info
->cmd_sock
= cmd_sock
;
188 info
->event_sock
= event_sock
;
189 info
->clean_up
= false;
190 info
->in_event_loop
= false;
192 info
->event_cb
= (cb_info
*)malloc(sizeof(cb_info
) * DEFAULT_EVENT_CB_SIZE
);
193 info
->alloc_event_cb
= DEFAULT_EVENT_CB_SIZE
;
194 info
->num_event_cb
= 0;
196 info
->cmd
= (cmd_info
*)malloc(sizeof(cmd_info
) * DEFAULT_CMD_SIZE
);
197 info
->alloc_cmd
= DEFAULT_CMD_SIZE
;
200 info
->nl80211_family_id
= genl_ctrl_resolve(cmd_sock
, "nl80211");
201 if (info
->nl80211_family_id
< 0) {
202 ALOGE("Could not resolve nl80211 familty id");
203 nl_socket_free(cmd_sock
);
204 nl_socket_free(event_sock
);
206 return WIFI_ERROR_UNKNOWN
;
209 pthread_mutex_init(&info
->cb_lock
, NULL
);
211 *handle
= (wifi_handle
) info
;
212 ALOGD("wifi_initialize, handle = %p\n", handle
);
213 ALOGD("wifi_initialize, *handle = %p\n", *handle
);
214 ALOGD("wifi_initialize, info = %p\n", info
);
215 ALOGD("wifi_initialize, *info = %pn", *info
);
216 wifi_add_membership(*handle
, "scan");
217 wifi_add_membership(*handle
, "mlme");
218 wifi_add_membership(*handle
, "regulatory");
219 wifi_add_membership(*handle
, "vendor");
221 wifi_init_interfaces(*handle
);
222 ALOGD("Found %d interfaces", info
->num_interfaces
);
225 ALOGI("Initialized Wifi HAL Successfully; vendor cmd = %d", NL80211_CMD_VENDOR
);
229 static int wifi_add_membership(wifi_handle handle
, const char *group
)
231 hal_info
*info
= getHalInfo(handle
);
233 int id
= wifi_get_multicast_id(handle
, "nl80211", group
);
235 ALOGE("Could not find group %s", group
);
239 int ret
= nl_socket_add_membership(info
->event_sock
, id
);
241 ALOGE("Could not add membership to group %s", group
);
244 ALOGI("Successfully added membership for group %s", group
);
248 static void internal_cleaned_up_handler(wifi_handle handle
)
250 hal_info
*info
= getHalInfo(handle
);
251 wifi_cleaned_up_handler cleaned_up_handler
= info
->cleaned_up_handler
;
253 if (info
->cmd_sock
!= 0) {
254 close(info
->cleanup_socks
[0]);
255 close(info
->cleanup_socks
[1]);
256 nl_socket_free(info
->cmd_sock
);
257 nl_socket_free(info
->event_sock
);
258 info
->cmd_sock
= NULL
;
259 info
->event_sock
= NULL
;
262 (*cleaned_up_handler
)(handle
);
263 pthread_mutex_destroy(&info
->cb_lock
);
266 ALOGI("Internal cleanup completed");
269 void wifi_cleanup(wifi_handle handle
, wifi_cleaned_up_handler handler
)
271 hal_info
*info
= getHalInfo(handle
);
274 info
->cleaned_up_handler
= handler
;
275 if (write(info
->cleanup_socks
[0], "Exit", 4) < 1) {
276 ALOGE("could not write to the cleanup socket");
278 // Listen to the response
279 // Hopefully we dont get errors or get hung up
280 // Not much can be done in that case, but assume that
281 // it has rx'ed the Exit message to exit the thread.
282 // As a fallback set the cleanup flag to TRUE
283 memset(buf
, 0, sizeof(buf
));
284 int result
= read(info
->cleanup_socks
[0], buf
, sizeof(buf
));
285 ALOGE("%s: Read after POLL returned %d, error no = %d", __FUNCTION__
, result
, errno
);
286 if (strncmp(buf
, "Done", 4) == 0) {
287 ALOGE("Event processing terminated");
289 ALOGD("Rx'ed %s", buf
);
292 info
->clean_up
= true;
293 pthread_mutex_lock(&info
->cb_lock
);
295 int bad_commands
= 0;
297 while (info
->num_cmd
> bad_commands
) {
298 int num_cmd
= info
->num_cmd
;
299 cmd_info
*cmdi
= &(info
->cmd
[bad_commands
]);
300 WifiCommand
*cmd
= cmdi
->cmd
;
302 pthread_mutex_unlock(&info
->cb_lock
);
304 pthread_mutex_lock(&info
->cb_lock
);
305 /* release reference added when command is saved */
307 if (num_cmd
== info
->num_cmd
) {
313 for (int i
= 0; i
< info
->num_event_cb
; i
++) {
314 cb_info
*cbi
= &(info
->event_cb
[i
]);
315 WifiCommand
*cmd
= (WifiCommand
*)cbi
->cb_arg
;
316 ALOGE("Leaked command %p", cmd
);
318 pthread_mutex_unlock(&info
->cb_lock
);
319 internal_cleaned_up_handler(handle
);
322 static int internal_pollin_handler(wifi_handle handle
)
324 hal_info
*info
= getHalInfo(handle
);
325 ALOGI("even_loop info = %p", info
);
326 struct nl_cb
*cb
= nl_socket_get_cb(info
->event_sock
);
327 int res
= nl_recvmsgs(info
->event_sock
, cb
);
328 ALOGD("nl_recvmsgs returned %d", res
);
333 /* Run event handler */
334 void wifi_event_loop(wifi_handle handle
)
336 hal_info
*info
= getHalInfo(handle
);
337 ALOGI("even_loop info = %p", info
);
338 ALOGI("even_loop info = %p", handle
);
339 if (info
->in_event_loop
) {
342 info
->in_event_loop
= true;
346 memset(&pfd
[0], 0, sizeof(pollfd
) * 2);
348 pfd
[0].fd
= nl_socket_get_fd(info
->event_sock
);
349 pfd
[0].events
= POLLIN
;
350 pfd
[1].fd
= info
->cleanup_socks
[1];
351 pfd
[1].events
= POLLIN
;
356 int timeout
= -1; /* Infinite timeout */
359 int result
= poll(pfd
, 2, timeout
);
361 } else if (pfd
[0].revents
& POLLERR
) {
362 ALOGE("POLL Error; error no = %d", errno
);
363 int result2
= read(pfd
[0].fd
, buf
, sizeof(buf
));
364 ALOGE("Read after POLL returned %d, error no = %d", result2
, errno
);
365 } else if (pfd
[0].revents
& POLLHUP
) {
366 ALOGE("Remote side hung up");
368 } else if (pfd
[0].revents
& POLLIN
) {
369 internal_pollin_handler(handle
);
370 } else if (pfd
[1].revents
& POLLIN
) {
371 memset(buf
, 0, sizeof(buf
));
372 int result2
= read(pfd
[1].fd
, buf
, sizeof(buf
));
373 ALOGE("%s: Read after POLL returned %d, error no = %d", __FUNCTION__
, result2
, errno
);
374 if (strncmp(buf
, "Exit", 4) == 0) {
375 ALOGD("Got a signal to exit!!!");
376 if (write(pfd
[1].fd
, "Done", 4) < 1) {
377 ALOGE("could not write to the cleanup socket");
381 ALOGD("Rx'ed %s on the cleanup socket\n", buf
);
384 ALOGE("Unknown event - %0x, %0x", pfd
[0].revents
, pfd
[1].revents
);
386 } while (!info
->clean_up
);
387 ALOGI("Exit %s", __FUNCTION__
);
390 ///////////////////////////////////////////////////////////////////////////////////////
392 static int internal_no_seq_check(struct nl_msg
*msg
, void *arg
)
397 static int internal_valid_message_handler(nl_msg
*msg
, void *arg
)
399 wifi_handle handle
= (wifi_handle
)arg
;
400 hal_info
*info
= getHalInfo(handle
);
401 ALOGI("even_loop info = %p", handle
);
402 ALOGD("internal_valid_message_handler, info = %p", info
);
404 WifiEvent
event(msg
);
405 int res
= event
.parse();
407 ALOGE("Failed to parse event: %d", res
);
411 int cmd
= event
.get_cmd();
412 uint32_t vendor_id
= 0;
415 if (cmd
== NL80211_CMD_VENDOR
) {
416 vendor_id
= event
.get_u32(NL80211_ATTR_VENDOR_ID
);
417 subcmd
= event
.get_u32(NL80211_ATTR_VENDOR_SUBCMD
);
418 ALOGI("event received %s, vendor_id = 0x%0x, subcmd = 0x%0x",
419 event
.get_cmdString(), vendor_id
, subcmd
);
421 ALOGI("event received %s", event
.get_cmdString());
424 //ALOGI("event received %s, vendor_id = 0x%0x", event.get_cmdString(), vendor_id);
427 pthread_mutex_lock(&info
->cb_lock
);
429 ALOGI("Number of events %d", info
->num_event_cb
);
431 for (int i
= 0; i
< info
->num_event_cb
; i
++) {
432 if (cmd
== info
->event_cb
[i
].nl_cmd
) {
433 if (cmd
== NL80211_CMD_VENDOR
434 && ((vendor_id
!= info
->event_cb
[i
].vendor_id
)
435 || (subcmd
!= info
->event_cb
[i
].vendor_subcmd
)))
437 /* event for a different vendor, ignore it */
441 cb_info
*cbi
= &(info
->event_cb
[i
]);
442 nl_recvmsg_msg_cb_t cb_func
= cbi
->cb_func
;
443 void *cb_arg
= cbi
->cb_arg
;
444 WifiCommand
*cmd
= (WifiCommand
*)cbi
->cb_arg
;
449 pthread_mutex_unlock(&info
->cb_lock
);
451 (*cb_func
)(msg
, cb_arg
);
460 pthread_mutex_unlock(&info
->cb_lock
);
464 ///////////////////////////////////////////////////////////////////////////////////////
466 class GetMulticastIdCommand
: public WifiCommand
473 GetMulticastIdCommand(wifi_handle handle
, const char *name
, const char *group
)
474 : WifiCommand(handle
, 0)
485 virtual int create() {
486 int nlctrlFamily
= genl_ctrl_resolve(mInfo
->cmd_sock
, "nlctrl");
487 ALOGI("ctrl family = %d", nlctrlFamily
);
488 int ret
= mMsg
.create(nlctrlFamily
, CTRL_CMD_GETFAMILY
, 0, 0);
492 ret
= mMsg
.put_string(CTRL_ATTR_FAMILY_NAME
, mName
);
496 virtual int handleResponse(WifiEvent
& reply
) {
498 ALOGE("handling reponse in %s", __func__
);
500 struct nlattr
**tb
= reply
.attributes();
501 struct nlattr
*mcgrp
= NULL
;
504 if (!tb
[CTRL_ATTR_MCAST_GROUPS
]) {
505 ALOGE("No multicast groups found");
508 ALOGE("Multicast groups attr size = %d", nla_len(tb
[CTRL_ATTR_MCAST_GROUPS
]));
511 for_each_attr(mcgrp
, tb
[CTRL_ATTR_MCAST_GROUPS
], i
) {
513 ALOGE("Processing group");
514 struct nlattr
*tb2
[CTRL_ATTR_MCAST_GRP_MAX
+ 1];
515 nla_parse(tb2
, CTRL_ATTR_MCAST_GRP_MAX
, (nlattr
*)nla_data(mcgrp
),
516 nla_len(mcgrp
), NULL
);
517 if (!tb2
[CTRL_ATTR_MCAST_GRP_NAME
] || !tb2
[CTRL_ATTR_MCAST_GRP_ID
]) {
521 char *grpName
= (char *)nla_data(tb2
[CTRL_ATTR_MCAST_GRP_NAME
]);
522 int grpNameLen
= nla_len(tb2
[CTRL_ATTR_MCAST_GRP_NAME
]);
524 ALOGE("Found group name %s", grpName
);
526 if (strncmp(grpName
, mGroup
, grpNameLen
) != 0)
529 mId
= nla_get_u32(tb2
[CTRL_ATTR_MCAST_GRP_ID
]);
538 class SetPnoMacAddrOuiCommand
: public WifiCommand
{
543 feature_set
*feature_matrix
;
547 SetPnoMacAddrOuiCommand(wifi_interface_handle handle
, oui scan_oui
)
548 : WifiCommand(handle
, 0)
553 int createRequest(WifiRequest
& request
, int subcmd
, byte
*scan_oui
) {
554 int result
= request
.create(GOOGLE_OUI
, subcmd
);
559 nlattr
*data
= request
.attr_start(NL80211_ATTR_VENDOR_DATA
);
560 result
= request
.put(ANDR_WIFI_ATTRIBUTE_PNO_RANDOM_MAC_OUI
, scan_oui
, DOT11_OUI_LEN
);
565 request
.attr_end(data
);
571 ALOGD("Sending mac address OUI");
572 WifiRequest
request(familyId(), ifaceId());
573 int result
= createRequest(request
, SLSI_NL80211_VENDOR_SUBCMD_SET_GSCAN_OUI
, mOui
);
574 if (result
!= WIFI_SUCCESS
) {
575 ALOGE("failed to create request; result = %d", result
);
579 result
= requestResponse(request
);
580 if (result
!= WIFI_SUCCESS
) {
581 ALOGE("failed to set scanning mac OUI; result = %d", result
);
587 virtual int handleResponse(WifiEvent
& reply
) {
588 ALOGD("Request complete!");
589 /* Nothing to do on response! */
594 class SetNodfsCommand
: public WifiCommand
{
599 SetNodfsCommand(wifi_interface_handle handle
, u32 nodfs
)
600 : WifiCommand(handle
, 0) {
603 virtual int create() {
606 ret
= mMsg
.create(GOOGLE_OUI
, SLSI_NL80211_VENDOR_SUBCMD_SET_NODFS
);
608 ALOGE("Can't create message to send to driver - %d", ret
);
612 nlattr
*data
= mMsg
.attr_start(NL80211_ATTR_VENDOR_DATA
);
613 ret
= mMsg
.put_u32(ATTR_NODFS_VALUE
, mNoDfs
);
623 class SetRSSIMonitorCommand
: public WifiCommand
{
627 wifi_rssi_event_handler mHandler
;
629 SetRSSIMonitorCommand(wifi_request_id id
, wifi_interface_handle handle
,
630 s8 max_rssi
, s8 min_rssi
, wifi_rssi_event_handler eh
)
631 : WifiCommand(handle
, id
), mMax_rssi(max_rssi
), mMin_rssi
632 (min_rssi
), mHandler(eh
)
635 int createRequest(WifiRequest
& request
, int enable
) {
636 int result
= request
.create(GOOGLE_OUI
, SLSI_NL80211_VENDOR_SUBCMD_SET_RSSI_MONITOR
);
641 nlattr
*data
= request
.attr_start(NL80211_ATTR_VENDOR_DATA
);
642 result
= request
.put_u8(RSSI_MONITOR_ATTRIBUTE_MAX_RSSI
, (enable
? mMax_rssi
: 0));
646 ALOGD("create request");
647 result
= request
.put_u8(RSSI_MONITOR_ATTRIBUTE_MIN_RSSI
, (enable
? mMin_rssi
: 0));
651 result
= request
.put_u8(RSSI_MONITOR_ATTRIBUTE_START
, enable
);
655 request
.attr_end(data
);
660 WifiRequest
request(familyId(), ifaceId());
661 int result
= createRequest(request
, 1);
665 result
= requestResponse(request
);
667 ALOGI("Failed to set RSSI Monitor, result = %d", result
);
670 ALOGI("Successfully set RSSI monitoring");
671 registerVendorHandler(GOOGLE_OUI
, WIFI_RSSI_REPORT_EVENT
);
675 unregisterVendorHandler(GOOGLE_OUI
, WIFI_RSSI_REPORT_EVENT
);
682 virtual int cancel() {
684 WifiRequest
request(familyId(), ifaceId());
685 int result
= createRequest(request
, 0);
686 if (result
!= WIFI_SUCCESS
) {
687 ALOGE("failed to create request; result = %d", result
);
689 result
= requestResponse(request
);
690 if (result
!= WIFI_SUCCESS
) {
691 ALOGE("failed to stop RSSI monitoring = %d", result
);
694 unregisterVendorHandler(GOOGLE_OUI
, WIFI_RSSI_REPORT_EVENT
);
698 virtual int handleResponse(WifiEvent
& reply
) {
699 /* Nothing to do on response! */
703 virtual int handleEvent(WifiEvent
& event
) {
704 ALOGI("Got a RSSI monitor event");
706 nlattr
*vendor_data
= event
.get_attribute(NL80211_ATTR_VENDOR_DATA
);
707 int len
= event
.get_vendor_data_len();
709 if (vendor_data
== NULL
|| len
== 0) {
710 ALOGI("RSSI monitor: No data");
719 rssi_monitor_evt
*data
= (rssi_monitor_evt
*)event
.get_vendor_data();
721 if (*mHandler
.on_rssi_threshold_breached
) {
722 (*mHandler
.on_rssi_threshold_breached
)(id(), data
->BSSID
, data
->cur_rssi
);
724 ALOGW("No RSSI monitor handler registered");
732 class SetCountryCodeCommand
: public WifiCommand
{
734 const char *mCountryCode
;
736 SetCountryCodeCommand(wifi_interface_handle handle
, const char *country_code
)
737 : WifiCommand(handle
, 0) {
738 mCountryCode
= country_code
;
740 virtual int create() {
743 ret
= mMsg
.create(GOOGLE_OUI
, SLSI_NL80211_VENDOR_SUBCMD_SET_COUNTRY_CODE
);
745 ALOGE("Can't create message to send to driver - %d", ret
);
749 nlattr
*data
= mMsg
.attr_start(NL80211_ATTR_VENDOR_DATA
);
750 ret
= mMsg
.put_string(ATTR_COUNTRY_CODE
, mCountryCode
);
761 class GetFeatureSetCommand
: public WifiCommand
{
769 GetFeatureSetCommand(wifi_interface_handle handle
, feature_set
*set
)
770 : WifiCommand(handle
, 0)
775 virtual int create() {
778 ret
= mMsg
.create(GOOGLE_OUI
, SLSI_NL80211_VENDOR_SUBCMD_GET_FEATURE_SET
);
780 ALOGE("create failed - %d", ret
);
787 virtual int handleResponse(WifiEvent
& reply
) {
789 int id
= reply
.get_vendor_id();
790 int subcmd
= reply
.get_vendor_subcmd();
792 if (reply
.get_cmd() != NL80211_CMD_VENDOR
) {
793 ALOGD("Ignore reply; cmd = %d", reply
.get_cmd());
797 nlattr
*vendor_data
= reply
.get_attribute(NL80211_ATTR_VENDOR_DATA
);
798 int len
= reply
.get_vendor_data_len();
800 if (vendor_data
== NULL
|| len
== 0) {
801 ALOGE("vendor data in GetFeatureSetCommand missing!!");
805 void *data
= reply
.get_vendor_data();
807 ALOGE("feature_set Pointer not set");
810 memcpy(fset
, data
, min(len
, (int) sizeof(*fset
)));
816 static int wifi_get_multicast_id(wifi_handle handle
, const char *name
, const char *group
)
818 GetMulticastIdCommand
cmd(handle
, name
, group
);
819 int res
= cmd
.requestResponse();
826 /////////////////////////////////////////////////////////////////////////
828 static bool is_wifi_interface(const char *name
)
830 if (strncmp(name
, "wlan", 4) != 0 && strncmp(name
, "p2p", 3) != 0) {
831 /* not a wifi interface; ignore it */
838 static int get_interface(const char *name
, interface_info
*info
)
840 strcpy(info
->name
, name
);
841 info
->id
= if_nametoindex(name
);
842 ALOGI("found an interface : %s, id = %d", name
, info
->id
);
846 wifi_error
wifi_init_interfaces(wifi_handle handle
)
848 hal_info
*info
= (hal_info
*)handle
;
849 ALOGD("wifi_init_interfaces, info = %p", info
);
853 DIR *d
= opendir("/sys/class/net");
855 return WIFI_ERROR_UNKNOWN
;
858 while ((de
= readdir(d
))) {
859 if (de
->d_name
[0] == '.')
861 if (is_wifi_interface(de
->d_name
) ) {
868 d
= opendir("/sys/class/net");
870 return WIFI_ERROR_UNKNOWN
;
872 info
->interfaces
= (interface_info
**)malloc(sizeof(interface_info
*) * n
);
875 while ((de
= readdir(d
))) {
876 if (de
->d_name
[0] == '.')
878 if (is_wifi_interface(de
->d_name
)) {
879 interface_info
*ifinfo
= (interface_info
*)malloc(sizeof(interface_info
));
880 if (get_interface(de
->d_name
, ifinfo
) != WIFI_SUCCESS
) {
884 ifinfo
->handle
= handle
;
885 info
->interfaces
[i
] = ifinfo
;
892 info
->num_interfaces
= n
;
896 wifi_error
wifi_get_ifaces(wifi_handle handle
, int *num
, wifi_interface_handle
**interfaces
)
898 hal_info
*info
= (hal_info
*)handle
;
900 *interfaces
= (wifi_interface_handle
*)info
->interfaces
;
901 *num
= info
->num_interfaces
;
906 wifi_error
wifi_get_iface_name(wifi_interface_handle handle
, char *name
, size_t size
)
908 interface_info
*info
= (interface_info
*)handle
;
909 strcpy(name
, info
->name
);
913 wifi_error
wifi_get_concurrency_matrix(wifi_interface_handle handle
, int set_size_max
,
914 feature_set set
[], int *set_size
)
916 return WIFI_ERROR_NOT_SUPPORTED
;
919 wifi_error
wifi_set_scanning_mac_oui(wifi_interface_handle handle
, oui scan_oui
)
921 SetPnoMacAddrOuiCommand
command(handle
, scan_oui
);
922 return (wifi_error
)command
.start();
926 wifi_error
wifi_set_nodfs_flag(wifi_interface_handle handle
, u32 nodfs
)
928 SetNodfsCommand
command(handle
, nodfs
);
929 return (wifi_error
) command
.requestResponse();
932 static wifi_error
wifi_start_rssi_monitoring(wifi_request_id id
, wifi_interface_handle
933 iface
, s8 max_rssi
, s8 min_rssi
, wifi_rssi_event_handler eh
)
935 ALOGD("Start RSSI monitor %d", id
);
936 wifi_handle handle
= getWifiHandle(iface
);
937 SetRSSIMonitorCommand
*cmd
= new SetRSSIMonitorCommand(id
, iface
, max_rssi
, min_rssi
, eh
);
938 wifi_register_cmd(handle
, id
, cmd
);
940 wifi_error result
= (wifi_error
)cmd
->start();
941 if (result
!= WIFI_SUCCESS
) {
942 wifi_unregister_cmd(handle
, id
);
948 static wifi_error
wifi_stop_rssi_monitoring(wifi_request_id id
, wifi_interface_handle iface
)
950 ALOGD("Stopping RSSI monitor");
953 wifi_rssi_event_handler handler
;
954 wifi_handle handle
= getWifiHandle(iface
);
955 memset(&handler
, 0, sizeof(handler
));
956 SetRSSIMonitorCommand
*cmd
= new SetRSSIMonitorCommand(id
, iface
,
962 return wifi_cancel_cmd(id
, iface
);
965 wifi_error
wifi_get_supported_feature_set(wifi_interface_handle handle
, feature_set
*set
)
967 GetFeatureSetCommand
command(handle
, set
);
968 return (wifi_error
) command
.requestResponse();
971 wifi_error
wifi_set_country_code(wifi_interface_handle handle
, const char *country_code
)
973 SetCountryCodeCommand
command(handle
, country_code
);
974 return (wifi_error
) command
.requestResponse();
977 /////////////////////////////////////////////////////////////////////////////