• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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                 return NL_SKIP;
130             }
131         }
132 
133         if (num_radios) {
134             rem_len = MAX_CMD_RESP_BUF_LEN;
135             radioStatsBuf = (u8 *)malloc(MAX_CMD_RESP_BUF_LEN);
136             if (!radioStatsBuf) {
137                 ALOGE("No memory\n");
138                 return NL_SKIP;
139             }
140             memset(radioStatsBuf, 0, MAX_CMD_RESP_BUF_LEN);
141             output = radioStatsBuf;
142 
143             if (!data || !data_len) {
144                 ALOGE("%s: null data\n", __func__);
145                 return NL_SKIP;
146             }
147 
148             data_ptr = (u8*)data;
149             for (int i = 0; i < num_radios; i++) {
150                 rem_len -= per_radio_size;
151                 if (rem_len < per_radio_size) {
152                     ALOGE("No data left for radio %d\n", i);
153                     goto exit;
154                 }
155                 data_ptr = (u8*)data + total_size;
156                 if (!data_ptr) {
157                     ALOGE("Invalid data for radio index = %d\n", i);
158                     goto exit;
159                 }
160                 radio_stat_ptr =
161                     convertToExternalRadioStatStructure((wifi_radio_stat*)data_ptr,
162                         &per_radio_size);
163                 if (!radio_stat_ptr || !per_radio_size) {
164                     ALOGE("No data for radio %d\n", i);
165                     continue;
166                 }
167                 memcpy(output, radio_stat_ptr, per_radio_size);
168                 output += per_radio_size;
169                 total_size += per_radio_size;
170             }
171 
172             iface_stat = ((u8*)data + total_size);
173             if (!iface_stat || data_len < total_size) {
174                 ALOGE("No data for iface stats!!, data_len = %d, total_size = %d\n",
175                     data_len, total_size);
176                 goto exit;
177             }
178             (*mHandler.on_link_stats_results)(id, (wifi_iface_stat *)iface_stat,
179             num_radios, (wifi_radio_stat *)radioStatsBuf);
180         }
181 exit:
182         if (radio_stat_ptr) {
183             free(radio_stat_ptr);
184             radio_stat_ptr = NULL;
185         }
186         if (radioStatsBuf) {
187             free(radioStatsBuf);
188             radioStatsBuf = NULL;
189         }
190         return NL_OK;
191     }
192 
193 private:
convertToExternalRadioStatStructure(wifi_radio_stat * internal_stat_ptr,uint32_t * per_radio_size)194     wifi_radio_stat *convertToExternalRadioStatStructure(wifi_radio_stat *internal_stat_ptr,
195         uint32_t *per_radio_size) {
196         wifi_radio_stat *external_stat_ptr = NULL;
197         if (!internal_stat_ptr) {
198             ALOGE("Incoming data is null\n");
199         } else {
200             uint32_t channel_size = internal_stat_ptr->num_channels * sizeof(wifi_channel_stat);
201             *per_radio_size = offsetof(wifi_radio_stat, channels) + channel_size;
202             external_stat_ptr = (wifi_radio_stat *)malloc(*per_radio_size);
203             if (external_stat_ptr) {
204                 external_stat_ptr->radio = internal_stat_ptr->radio;
205                 external_stat_ptr->on_time = internal_stat_ptr->on_time;
206                 external_stat_ptr->tx_time = internal_stat_ptr->tx_time;
207                 external_stat_ptr->num_tx_levels = internal_stat_ptr->num_tx_levels;
208                 external_stat_ptr->tx_time_per_levels = NULL;
209                 external_stat_ptr->rx_time = internal_stat_ptr->rx_time;
210                 external_stat_ptr->on_time_scan = internal_stat_ptr->on_time_scan;
211                 external_stat_ptr->on_time_nbd = internal_stat_ptr->on_time_nbd;
212                 external_stat_ptr->on_time_gscan = internal_stat_ptr->on_time_gscan;
213                 external_stat_ptr->on_time_roam_scan = internal_stat_ptr->on_time_roam_scan;
214                 external_stat_ptr->on_time_pno_scan = internal_stat_ptr->on_time_pno_scan;
215                 external_stat_ptr->on_time_hs20 = internal_stat_ptr->on_time_hs20;
216                 external_stat_ptr->num_channels = internal_stat_ptr->num_channels;
217                 if (internal_stat_ptr->num_channels) {
218                     memcpy(&(external_stat_ptr->channels), &(internal_stat_ptr->channels),
219                         channel_size);
220                 }
221             }
222         }
223         return external_stat_ptr;
224     }
225 };
226 
wifi_get_link_stats(wifi_request_id id,wifi_interface_handle iface,wifi_stats_result_handler handler)227 wifi_error wifi_get_link_stats(wifi_request_id id,
228         wifi_interface_handle iface, wifi_stats_result_handler handler)
229 {
230     GetLinkStatsCommand command(iface, handler);
231     return (wifi_error) command.requestResponse();
232 }
233 
wifi_set_link_stats(wifi_interface_handle,wifi_link_layer_params)234 wifi_error wifi_set_link_stats(
235         wifi_interface_handle /* iface */, wifi_link_layer_params /* params */)
236 {
237     /* Return success here since bcom HAL does not need set link stats. */
238     return WIFI_SUCCESS;
239 }
240 
wifi_clear_link_stats(wifi_interface_handle,u32,u32 *,u8,u8 *)241 wifi_error wifi_clear_link_stats(
242         wifi_interface_handle /* iface */, u32 /* stats_clear_req_mask */,
243         u32 * /* stats_clear_rsp_mask */, u8 /* stop_req */, u8 * /* stop_rsp */)
244 {
245     /* Return success here since bcom HAL does not support clear link stats. */
246     return WIFI_SUCCESS;
247 }
248