1 /*
2 * Copyright (C) 2017 The Android Open Source Project
3 *
4 * Portions copyright (C) 2017 Broadcom Limited
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18
19 #include <stdint.h>
20 #include <fcntl.h>
21 #include <sys/socket.h>
22 #include <netlink/genl/genl.h>
23 #include <netlink/genl/family.h>
24 #include <netlink/genl/ctrl.h>
25 #include <linux/rtnetlink.h>
26 #include <netpacket/packet.h>
27 #include <linux/filter.h>
28 #include <linux/errqueue.h>
29
30 #include <linux/pkt_sched.h>
31 #include <netlink/object-api.h>
32 #include <netlink/netlink.h>
33 #include <netlink/socket.h>
34 #include <netlink/handlers.h>
35
36 #include "sync.h"
37
38 #define LOG_TAG "WifiHAL"
39
40 #include <utils/Log.h>
41
42 #include "wifi_hal.h"
43 #include "common.h"
44 #include "cpp_bindings.h"
45
46 typedef enum {
47 ANDR_WIFI_ATTRIBUTE_INVALID = 0,
48 ANDR_WIFI_ATTRIBUTE_NUM_RADIO = 1,
49 ANDR_WIFI_ATTRIBUTE_STATS_INFO = 2,
50 ANDR_WIFI_ATTRIBUTE_STATS_MAX = 3
51 } LINK_STAT_ATTRIBUTE;
52
53 /* Internal radio statistics structure in the driver */
54 typedef struct {
55 wifi_radio radio;
56 uint32_t on_time;
57 uint32_t tx_time;
58 uint32_t rx_time;
59 uint32_t on_time_scan;
60 uint32_t on_time_nbd;
61 uint32_t on_time_gscan;
62 uint32_t on_time_roam_scan;
63 uint32_t on_time_pno_scan;
64 uint32_t on_time_hs20;
65 uint32_t num_channels;
66 wifi_channel_stat channels[];
67 } wifi_radio_stat_internal;
68
69 enum {
70 LSTATS_SUBCMD_GET_INFO = ANDROID_NL80211_SUBCMD_LSTATS_RANGE_START,
71 };
72
73 class GetLinkStatsCommand : public WifiCommand
74 {
75 wifi_stats_result_handler mHandler;
76 public:
GetLinkStatsCommand(wifi_interface_handle iface,wifi_stats_result_handler handler)77 GetLinkStatsCommand(wifi_interface_handle iface, wifi_stats_result_handler handler)
78 : WifiCommand("GetLinkStatsCommand", iface, 0), mHandler(handler)
79 { }
80
create()81 virtual int create() {
82 // ALOGI("Creating message to get link statistics; iface = %d", mIfaceInfo->id);
83
84 int ret = mMsg.create(GOOGLE_OUI, LSTATS_SUBCMD_GET_INFO);
85 if (ret < 0) {
86 ALOGE("Failed to create %x - %d", LSTATS_SUBCMD_GET_INFO, ret);
87 return ret;
88 }
89
90 return ret;
91 }
92
93 protected:
handleResponse(WifiEvent & reply)94 virtual int handleResponse(WifiEvent& reply) {
95 void *data = NULL;
96 wifi_radio_stat *radio_stat_ptr = NULL;
97 u8 *iface_stat = NULL;
98 u8 *radioStatsBuf = NULL, *output = NULL, *data_ptr = NULL;
99 uint32_t total_size = 0, per_radio_size = 0, data_len = 0, rem_len = 0;
100 int num_radios = 0, id = 0, subcmd = 0, len = 0;
101
102 // ALOGI("In GetLinkStatsCommand::handleResponse");
103
104 if (reply.get_cmd() != NL80211_CMD_VENDOR) {
105 ALOGD("Ignoring reply with cmd = %d", reply.get_cmd());
106 return NL_SKIP;
107 }
108
109 id = reply.get_vendor_id();
110 subcmd = reply.get_vendor_subcmd();
111 nlattr *vendor_data = reply.get_attribute(NL80211_ATTR_VENDOR_DATA);
112 len = reply.get_vendor_data_len();
113
114 ALOGV("Id = %0x, subcmd = %d, len = %d\n", id, subcmd, len);
115 if (vendor_data == NULL || len == 0) {
116 ALOGE("no vendor data in GetLinkStatCommand response; ignoring it");
117 return NL_SKIP;
118 }
119
120 for (nl_iterator it(vendor_data); it.has_next(); it.next()) {
121 if (it.get_type() == ANDR_WIFI_ATTRIBUTE_NUM_RADIO) {
122 num_radios = it.get_u32();
123 } else if (it.get_type() == ANDR_WIFI_ATTRIBUTE_STATS_INFO) {
124 data = it.get_data();
125 data_len = it.get_len();
126 } else {
127 ALOGW("Ignoring invalid attribute type = %d, size = %d\n",
128 it.get_type(), it.get_len());
129 }
130 }
131
132 if (num_radios) {
133 rem_len = MAX_CMD_RESP_BUF_LEN;
134 radioStatsBuf = (u8 *)malloc(MAX_CMD_RESP_BUF_LEN);
135 if (!radioStatsBuf) {
136 ALOGE("No memory\n");
137 return NL_SKIP;
138 }
139 memset(radioStatsBuf, 0, MAX_CMD_RESP_BUF_LEN);
140 output = radioStatsBuf;
141
142 if (!data || !data_len) {
143 ALOGE("%s: null data\n", __func__);
144 return NL_SKIP;
145 }
146
147 data_ptr = (u8*)data;
148 for (int i = 0; i < num_radios; i++) {
149 rem_len -= per_radio_size;
150 if (rem_len < per_radio_size) {
151 ALOGE("No data left for radio %d\n", i);
152 goto exit;
153 }
154 data_ptr = (u8*)data + total_size;
155 if (!data_ptr) {
156 ALOGE("Invalid data for radio index = %d\n", i);
157 goto exit;
158 }
159 radio_stat_ptr =
160 convertToExternalRadioStatStructure((wifi_radio_stat*)data_ptr,
161 &per_radio_size);
162 if (!radio_stat_ptr || !per_radio_size) {
163 ALOGE("No data for radio %d\n", i);
164 continue;
165 }
166 memcpy(output, radio_stat_ptr, per_radio_size);
167 output += per_radio_size;
168 total_size += per_radio_size;
169 }
170
171 iface_stat = ((u8*)data + total_size);
172 if (!iface_stat || data_len < total_size) {
173 ALOGE("No data for iface stats!!, data_len = %d, total_size = %d\n",
174 data_len, total_size);
175 goto exit;
176 }
177 (*mHandler.on_link_stats_results)(id, (wifi_iface_stat *)iface_stat,
178 num_radios, (wifi_radio_stat *)radioStatsBuf);
179 } else {
180 /* To be deprecated, adding it to keep it backward compatible */
181 // ALOGD("GetLinkStatCommand: zero radio case\n");
182 data = reply.get_vendor_data();
183 if (!data) {
184 ALOGE("Invalid vendor data received\n");
185 return NL_SKIP;
186 }
187
188 num_radios = 1;
189 data = reply.get_vendor_data();
190 len = reply.get_vendor_data_len();
191 if (!data || !len) {
192 ALOGE("Invalid vendor data received\n");
193 return NL_SKIP;
194 }
195 radio_stat_ptr =
196 convertToExternalRadioStatStructureLegacy((wifi_radio_stat_internal *)data);
197 if (!radio_stat_ptr) {
198 ALOGE("Invalid stats pointer received\n");
199 return NL_SKIP;
200 }
201 wifi_iface_stat *iface_stat =
202 (wifi_iface_stat *)((char *)&((wifi_radio_stat_internal *)data)->channels
203 + radio_stat_ptr->num_channels * sizeof(wifi_channel_stat));
204 (*mHandler.on_link_stats_results)(id, iface_stat, num_radios, radio_stat_ptr);
205 }
206 exit:
207 if (radio_stat_ptr) {
208 free(radio_stat_ptr);
209 radio_stat_ptr = NULL;
210 }
211 if (radioStatsBuf) {
212 free(radioStatsBuf);
213 radioStatsBuf = NULL;
214 }
215 return NL_OK;
216 }
217
218 private:
convertToExternalRadioStatStructure(wifi_radio_stat * internal_stat_ptr,uint32_t * per_radio_size)219 wifi_radio_stat *convertToExternalRadioStatStructure(wifi_radio_stat *internal_stat_ptr,
220 uint32_t *per_radio_size) {
221 wifi_radio_stat *external_stat_ptr = NULL;
222 if (!internal_stat_ptr) {
223 ALOGE("Incoming data is null\n");
224 } else {
225 uint32_t channel_size = internal_stat_ptr->num_channels * sizeof(wifi_channel_stat);
226 *per_radio_size = offsetof(wifi_radio_stat, channels) + channel_size;
227 external_stat_ptr = (wifi_radio_stat *)malloc(*per_radio_size);
228 if (external_stat_ptr) {
229 external_stat_ptr->radio = internal_stat_ptr->radio;
230 external_stat_ptr->on_time = internal_stat_ptr->on_time;
231 external_stat_ptr->tx_time = internal_stat_ptr->tx_time;
232 external_stat_ptr->num_tx_levels = internal_stat_ptr->num_tx_levels;
233 external_stat_ptr->tx_time_per_levels = NULL;
234 external_stat_ptr->rx_time = internal_stat_ptr->rx_time;
235 external_stat_ptr->on_time_scan = internal_stat_ptr->on_time_scan;
236 external_stat_ptr->on_time_nbd = internal_stat_ptr->on_time_nbd;
237 external_stat_ptr->on_time_gscan = internal_stat_ptr->on_time_gscan;
238 external_stat_ptr->on_time_roam_scan = internal_stat_ptr->on_time_roam_scan;
239 external_stat_ptr->on_time_pno_scan = internal_stat_ptr->on_time_pno_scan;
240 external_stat_ptr->on_time_hs20 = internal_stat_ptr->on_time_hs20;
241 external_stat_ptr->num_channels = internal_stat_ptr->num_channels;
242 if (internal_stat_ptr->num_channels) {
243 memcpy(&(external_stat_ptr->channels), &(internal_stat_ptr->channels),
244 channel_size);
245 }
246 }
247 }
248 return external_stat_ptr;
249 }
250
convertToExternalRadioStatStructureLegacy(wifi_radio_stat_internal * internal_stat_ptr)251 wifi_radio_stat *convertToExternalRadioStatStructureLegacy(wifi_radio_stat_internal *internal_stat_ptr) {
252 wifi_radio_stat *external_stat_ptr = NULL;
253 if (!internal_stat_ptr) {
254 ALOGE("Sta_ptr is null\n");
255 } else {
256 uint32_t channel_size = internal_stat_ptr->num_channels * sizeof(wifi_channel_stat);
257 uint32_t total_size = sizeof(wifi_radio_stat) + channel_size;
258 external_stat_ptr = (wifi_radio_stat *)malloc(total_size);
259 if (external_stat_ptr) {
260 external_stat_ptr->radio = internal_stat_ptr->radio;
261 external_stat_ptr->on_time = internal_stat_ptr->on_time;
262 external_stat_ptr->tx_time = internal_stat_ptr->tx_time;
263 external_stat_ptr->rx_time = internal_stat_ptr->rx_time;
264 external_stat_ptr->tx_time_per_levels = NULL;
265 external_stat_ptr->num_tx_levels = 0;
266 external_stat_ptr->on_time_scan = internal_stat_ptr->on_time_scan;
267 external_stat_ptr->on_time_nbd = internal_stat_ptr->on_time_nbd;
268 external_stat_ptr->on_time_gscan = internal_stat_ptr->on_time_gscan;
269 external_stat_ptr->on_time_roam_scan = internal_stat_ptr->on_time_roam_scan;
270 external_stat_ptr->on_time_pno_scan = internal_stat_ptr->on_time_pno_scan;
271 external_stat_ptr->on_time_hs20 = internal_stat_ptr->on_time_hs20;
272 external_stat_ptr->num_channels = internal_stat_ptr->num_channels;
273 if (internal_stat_ptr->num_channels) {
274 memcpy(&(external_stat_ptr->channels), &(internal_stat_ptr->channels),
275 channel_size);
276 }
277 }
278 }
279 return external_stat_ptr;
280 }
281 };
282
wifi_get_link_stats(wifi_request_id id,wifi_interface_handle iface,wifi_stats_result_handler handler)283 wifi_error wifi_get_link_stats(wifi_request_id id,
284 wifi_interface_handle iface, wifi_stats_result_handler handler)
285 {
286 GetLinkStatsCommand command(iface, handler);
287 return (wifi_error) command.requestResponse();
288 }
289
wifi_set_link_stats(wifi_interface_handle,wifi_link_layer_params)290 wifi_error wifi_set_link_stats(
291 wifi_interface_handle /* iface */, wifi_link_layer_params /* params */)
292 {
293 /* Return success here since bcom HAL does not need set link stats. */
294 return WIFI_SUCCESS;
295 }
296
wifi_clear_link_stats(wifi_interface_handle,u32,u32 *,u8,u8 *)297 wifi_error wifi_clear_link_stats(
298 wifi_interface_handle /* iface */, u32 /* stats_clear_req_mask */,
299 u32 * /* stats_clear_rsp_mask */, u8 /* stop_req */, u8 * /* stop_rsp */)
300 {
301 /* Return success here since bcom HAL does not support clear link stats. */
302 return WIFI_SUCCESS;
303 }
304