3 #include <sys/socket.h>
4 #include <netlink/genl/genl.h>
5 #include <netlink/genl/family.h>
6 #include <netlink/genl/ctrl.h>
7 #include <linux/rtnetlink.h>
8 #include <netpacket/packet.h>
9 #include <linux/filter.h>
10 #include <linux/errqueue.h>
12 #include <linux/pkt_sched.h>
13 #include <netlink/object-api.h>
14 #include <netlink/netlink.h>
15 #include <netlink/socket.h>
16 #include <netlink/handlers.h>
20 #define LOG_TAG "WifiHAL"
22 #include <utils/Log.h>
26 #include "cpp_bindings.h"
30 LSTATS_ATTRIBUTE_SET_MPDU_SIZE_THRESHOLD
= 1,
31 LSTATS_ATTRIBUTE_SET_AGGR_STATISTICS_GATHERING
,
32 LSTATS_ATTRIBUTE_CLEAR_STOP_REQUEST_MASK
,
33 LSTATS_ATTRIBUTE_CLEAR_STOP_REQUEST
,
39 class LinkLayerStatsCommand
: public WifiCommand
41 wifi_link_layer_params mParams
;
42 u32 mStatsClearReqMask
;
43 u32
*mStatsClearRspMask
;
47 LinkLayerStatsCommand(wifi_interface_handle handle
, wifi_link_layer_params params
)
48 : WifiCommand(handle
, 0), mParams(params
)
51 LinkLayerStatsCommand(wifi_interface_handle handle
,
52 u32 stats_clear_req_mask
, u32
*stats_clear_rsp_mask
, u8 stop_req
, u8
*stop_rsp
)
53 : WifiCommand(handle
, 0), mStatsClearReqMask(stats_clear_req_mask
), mStatsClearRspMask(stats_clear_rsp_mask
),
54 mStopReq(stop_req
), mStopRsp(stop_rsp
)
57 int createSetRequest(WifiRequest
& request
) {
58 int result
= request
.create(GOOGLE_OUI
, SLSI_NL80211_VENDOR_SUBCMD_LLS_SET_INFO
);
63 nlattr
*data
= request
.attr_start(NL80211_ATTR_VENDOR_DATA
);
65 result
= request
.put_u32(LSTATS_ATTRIBUTE_SET_MPDU_SIZE_THRESHOLD
, mParams
.mpdu_size_threshold
);
70 result
= request
.put_u32(LSTATS_ATTRIBUTE_SET_AGGR_STATISTICS_GATHERING
, mParams
.aggressive_statistics_gathering
);
74 request
.attr_end(data
);
78 int createClearRequest(WifiRequest
& request
) {
79 int result
= request
.create(GOOGLE_OUI
, SLSI_NL80211_VENDOR_SUBCMD_LLS_CLEAR_INFO
);
83 nlattr
*data
= request
.attr_start(NL80211_ATTR_VENDOR_DATA
);
85 result
= request
.put_u32(LSTATS_ATTRIBUTE_CLEAR_STOP_REQUEST_MASK
, mStatsClearReqMask
);
90 result
= request
.put_u32(LSTATS_ATTRIBUTE_CLEAR_STOP_REQUEST
, mStopReq
);
94 request
.attr_end(data
);
99 ALOGD("Starting Link Layer Statistics measurement (%d, %d)", mParams
.mpdu_size_threshold
, mParams
.aggressive_statistics_gathering
);
100 WifiRequest
request(familyId(), ifaceId());
101 int result
= createSetRequest(request
);
103 ALOGE("failed to set Link Layer Statistics (result:%d)", result
);
107 result
= requestResponse(request
);
109 ALOGE("failed to Set Link Layer Statistics (result:%d)", result
);
113 ALOGD("Successfully set Link Layer Statistics measurement");
117 virtual int clear() {
118 ALOGD("Clearing Link Layer Statistics (%d, %d)", mStatsClearReqMask
, mStopReq
);
119 WifiRequest
request(familyId(), ifaceId());
120 int result
= createClearRequest(request
);
122 ALOGE("failed to clear Link Layer Statistics (result:%d)", result
);
126 result
= requestResponse(request
);
128 ALOGE("failed to clear Link Layer Statistics (result:%d)", result
);
132 ALOGD("Successfully clear Link Layer Statistics measurement");
136 virtual int handleResponse(WifiEvent
& reply
) {
137 /* Nothing to do on response! */
143 wifi_error
wifi_set_link_stats(wifi_interface_handle iface
, wifi_link_layer_params params
)
145 LinkLayerStatsCommand
*command
= new LinkLayerStatsCommand(iface
, params
);
146 wifi_error result
= (wifi_error
)command
->start();
147 if (result
!= WIFI_SUCCESS
) {
148 ALOGE("failed to Set link layer stats", result
);
153 wifi_error
wifi_clear_link_stats(wifi_interface_handle iface
,
154 u32 stats_clear_req_mask
, u32
*stats_clear_rsp_mask
, u8 stop_req
, u8
*stop_rsp
)
156 LinkLayerStatsCommand
*command
= new LinkLayerStatsCommand(iface
, stats_clear_req_mask
, stats_clear_rsp_mask
, stop_req
, stop_rsp
);
157 wifi_error result
= (wifi_error
)command
->clear();
158 if (result
!= WIFI_SUCCESS
) {
159 ALOGE("failed to Clear link layer stats", result
);
160 *stats_clear_rsp_mask
= 0;
163 *stats_clear_rsp_mask
= stats_clear_req_mask
;
164 *stop_rsp
= stop_req
;
170 class GetLinkStatsCommand
: public WifiCommand
172 wifi_stats_result_handler mHandler
;
173 wifi_interface_handle iface
;
175 GetLinkStatsCommand(wifi_interface_handle iface
, wifi_stats_result_handler handler
)
176 : WifiCommand(iface
, 0), mHandler(handler
)
181 virtual int create() {
182 ALOGI("Creating message to get link statistics");
184 int ret
= mMsg
.create(GOOGLE_OUI
, SLSI_NL80211_VENDOR_SUBCMD_LLS_GET_INFO
);
186 ALOGE("Failed to create %x - %d", SLSI_NL80211_VENDOR_SUBCMD_LLS_GET_INFO
, ret
);
194 virtual int handleResponse(WifiEvent
& reply
) {
196 ALOGI("In GetLinkStatsCommand::handleResponse");
198 if (reply
.get_cmd() != NL80211_CMD_VENDOR
) {
199 ALOGD("Ignoring reply with cmd = %d", reply
.get_cmd());
203 int id
= reply
.get_vendor_id();
204 int subcmd
= reply
.get_vendor_subcmd();
206 ALOGI("Id = %0x, subcmd = %d", id
, subcmd
);
208 u8
*data
= (u8
*)reply
.get_vendor_data();
209 int len
= reply
.get_vendor_data_len();
211 // assuming max peers is 16
212 wifi_iface_stat
*iface_stat
= (wifi_iface_stat
*) malloc(sizeof(wifi_iface_stat
) + sizeof(wifi_peer_info
) * 16);
213 // max channel is 38 (14 2.4GHz and 24 5GHz)
214 wifi_radio_stat
*radio_stat
= (wifi_radio_stat
*) malloc(sizeof(wifi_radio_stat
) + sizeof(wifi_channel_stat
) * 38);
216 if (!iface_stat
|| !radio_stat
) {
217 ALOGE("Memory alloc failed in response handler!!!");
221 //kernel is 64 bit. if userspace is 64 bit, typecastting buffer works else, make corrections
222 if (sizeof(iface_stat
->iface
) == 8) {
223 memcpy(iface_stat
, data
, sizeof(wifi_iface_stat
) + sizeof(wifi_peer_info
) * ((wifi_iface_stat
*)data
)->num_peers
);
224 data
+= sizeof(wifi_iface_stat
) + sizeof(wifi_peer_info
) * ((wifi_iface_stat
*)data
)->num_peers
;
225 memcpy(radio_stat
, data
, sizeof(wifi_radio_stat
) + sizeof(wifi_channel_stat
)* ((wifi_radio_stat
*)data
)->num_channels
);
227 /* for 64 bit kernel ad 32 bit user space, there is 4 byte extra at the beging and another 4 byte pad after 80 bytes
228 * so remove first 4 and 81-84 bytes from NL buffer.*/
230 memcpy(iface_stat
, data
, 80);
231 data
+= 80 + 4; //for allignment skip 4 bytes
232 memcpy(((u8
*)iface_stat
) + 80, data
, sizeof(wifi_iface_stat
) - 80);
233 data
+= sizeof(wifi_iface_stat
) - 80;
234 memcpy(iface_stat
->peer_info
, data
, sizeof(wifi_peer_info
) * iface_stat
->num_peers
);
235 data
+= sizeof(wifi_peer_info
) * iface_stat
->num_peers
;
236 memcpy(radio_stat
, data
, sizeof(wifi_radio_stat
));
237 data
+= sizeof(wifi_radio_stat
);
238 memcpy(radio_stat
->channels
, data
, sizeof(wifi_channel_stat
) * radio_stat
->num_channels
);
240 iface_stat
->iface
= iface
;
241 (*mHandler
.on_link_stats_results
)(id
, iface_stat
, 1, radio_stat
);
248 wifi_error
wifi_get_link_stats(wifi_request_id id
,
249 wifi_interface_handle iface
, wifi_stats_result_handler handler
)
251 GetLinkStatsCommand
command(iface
, handler
);
252 return (wifi_error
) command
.requestResponse();