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 <log/log.h>
41 
42 #include "wifi_hal.h"
43 #include "common.h"
44 #include "cpp_bindings.h"
45 
46 /* Internal radio statistics structure in the driver */
47 typedef struct {
48 	wifi_radio radio;
49 	uint32_t on_time;
50 	uint32_t tx_time;
51 	uint32_t rx_time;
52 	uint32_t on_time_scan;
53 	uint32_t on_time_nbd;
54 	uint32_t on_time_gscan;
55 	uint32_t on_time_roam_scan;
56 	uint32_t on_time_pno_scan;
57 	uint32_t on_time_hs20;
58 	uint32_t num_channels;
59 	wifi_channel_stat channels[];
60 } wifi_radio_stat_internal;
61 
62 enum {
63     LSTATS_SUBCMD_GET_INFO = ANDROID_NL80211_SUBCMD_LSTATS_RANGE_START,
64 };
65 
66 class GetLinkStatsCommand : public WifiCommand
67 {
68     wifi_stats_result_handler mHandler;
69 public:
70     GetLinkStatsCommand(wifi_interface_handle iface, wifi_stats_result_handler handler)
71         : WifiCommand("GetLinkStatsCommand", iface, 0), mHandler(handler)
72     { }
73 
74     virtual int create() {
75         // ALOGI("Creating message to get link statistics; iface = %d", mIfaceInfo->id);
76 
77         int ret = mMsg.create(GOOGLE_OUI, LSTATS_SUBCMD_GET_INFO);
78         if (ret < 0) {
79             ALOGE("Failed to create %x - %d", LSTATS_SUBCMD_GET_INFO, ret);
80             return ret;
81         }
82 
83         return ret;
84     }
85 
86 protected:
87     virtual int handleResponse(WifiEvent& reply) {
88 
89         // ALOGI("In GetLinkStatsCommand::handleResponse");
90 
91         if (reply.get_cmd() != NL80211_CMD_VENDOR) {
92             ALOGD("Ignoring reply with cmd = %d", reply.get_cmd());
93             return NL_SKIP;
94         }
95 
96         int id = reply.get_vendor_id();
97         int subcmd = reply.get_vendor_subcmd();
98 
99         // ALOGI("Id = %0x, subcmd = %d", id, subcmd);
100 
101         void *data = reply.get_vendor_data();
102         int len = reply.get_vendor_data_len();
103         wifi_radio_stat *radio_stat =
104             convertToExternalRadioStatStructure((wifi_radio_stat_internal *)data);
105         if (!radio_stat) {
106             ALOGE("Invalid stats pointer received");
107             return NL_SKIP;
108         }
109         if (radio_stat->num_channels > 11) {
110             ALOGE("Incorrect number of channels = %d", radio_stat->num_channels);
111             // dump data before num_channels
112             ALOGE("radio: = %d", radio_stat->radio);
113             ALOGE("on_time: = %d", radio_stat->on_time);
114             ALOGE("tx_time: = %d", radio_stat->tx_time);
115             ALOGE("rx_time: = %d", radio_stat->rx_time);
116             ALOGE("on_time_scan: = %d", radio_stat->on_time_scan);
117             ALOGE("on_time_nbd: = %d", radio_stat->on_time_nbd);
118             ALOGE("on_time_gscan: = %d", radio_stat->on_time_gscan);
119             ALOGE("on_time_pno_scan: = %d", radio_stat->on_time_pno_scan);
120             ALOGE("on_time_hs20: = %d", radio_stat->on_time_hs20);
121             free(radio_stat);
122             return NL_SKIP;
123         }
124         wifi_iface_stat *iface_stat =
125             (wifi_iface_stat *)((char *)&((wifi_radio_stat_internal *)data)->channels
126                 + radio_stat->num_channels * sizeof(wifi_channel_stat));
127         (*mHandler.on_link_stats_results)(id, iface_stat, 1, radio_stat);
128         free(radio_stat);
129         return NL_OK;
130     }
131 
132 private:
133     wifi_radio_stat *convertToExternalRadioStatStructure(wifi_radio_stat_internal *internal_stat_ptr) {
134         wifi_radio_stat *external_stat_ptr = NULL;
135         if (internal_stat_ptr) {
136             uint32_t channel_size = internal_stat_ptr->num_channels * sizeof(wifi_channel_stat);
137             uint32_t total_size = sizeof(wifi_radio_stat) + channel_size;
138             external_stat_ptr = (wifi_radio_stat *)malloc(total_size);
139             if (external_stat_ptr) {
140                 external_stat_ptr->radio = internal_stat_ptr->radio;
141                 external_stat_ptr->on_time = internal_stat_ptr->on_time;
142                 external_stat_ptr->tx_time = internal_stat_ptr->tx_time;
143                 external_stat_ptr->rx_time = internal_stat_ptr->rx_time;
144                 external_stat_ptr->tx_time_per_levels = NULL;
145                 external_stat_ptr->num_tx_levels = 0;
146                 external_stat_ptr->on_time_scan = internal_stat_ptr->on_time_scan;
147                 external_stat_ptr->on_time_nbd = internal_stat_ptr->on_time_nbd;
148                 external_stat_ptr->on_time_gscan = internal_stat_ptr->on_time_gscan;
149                 external_stat_ptr->on_time_roam_scan = internal_stat_ptr->on_time_roam_scan;
150                 external_stat_ptr->on_time_pno_scan = internal_stat_ptr->on_time_pno_scan;
151                 external_stat_ptr->on_time_hs20 = internal_stat_ptr->on_time_hs20;
152                 external_stat_ptr->num_channels = internal_stat_ptr->num_channels;
153                 if (internal_stat_ptr->num_channels) {
154                     memcpy(&(external_stat_ptr->channels), &(internal_stat_ptr->channels),
155                         channel_size);
156                 }
157             }
158         }
159         return external_stat_ptr;
160     }
161 };
162 
163 wifi_error wifi_get_link_stats(wifi_request_id id,
164         wifi_interface_handle iface, wifi_stats_result_handler handler)
165 {
166     GetLinkStatsCommand command(iface, handler);
167     return (wifi_error) command.requestResponse();
168 }
169 
170 wifi_error wifi_set_link_stats(
171         wifi_interface_handle /* iface */, wifi_link_layer_params /* params */)
172 {
173     /* Return success here since bcom HAL does not need set link stats. */
174     return WIFI_SUCCESS;
175 }
176 
177 wifi_error wifi_clear_link_stats(
178         wifi_interface_handle /* iface */, u32 /* stats_clear_req_mask */,
179         u32 * /* stats_clear_rsp_mask */, u8 /* stop_req */, u8 * /* stop_rsp */)
180 {
181     /* Return success here since bcom HAL does not support clear link stats. */
182     return WIFI_SUCCESS;
183 }
184