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 /* 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 
98         void *data = reply.get_vendor_data();
99         int len = reply.get_vendor_data_len();
100 	if (!data || !len) {
101 		ALOGE("Invalid vendor data received");
102 		return NL_SKIP;
103 	}
104         wifi_radio_stat *radio_stat =
105             convertToExternalRadioStatStructure((wifi_radio_stat_internal *)data);
106         if (!radio_stat) {
107             ALOGE("Invalid stats pointer received");
108             return NL_SKIP;
109         }
110         wifi_iface_stat *iface_stat =
111             (wifi_iface_stat *)((char *)&((wifi_radio_stat_internal *)data)->channels
112                 + radio_stat->num_channels * sizeof(wifi_channel_stat));
113         (*mHandler.on_link_stats_results)(id, iface_stat, 1, radio_stat);
114         free(radio_stat);
115         return NL_OK;
116     }
117 
118 private:
119     wifi_radio_stat *convertToExternalRadioStatStructure(wifi_radio_stat_internal *internal_stat_ptr) {
120         wifi_radio_stat *external_stat_ptr = NULL;
121 	if (!internal_stat_ptr) {
122 		ALOGE("Sta_ptr is null\n");
123 	} else {
124             uint32_t channel_size = internal_stat_ptr->num_channels * sizeof(wifi_channel_stat);
125             uint32_t total_size = sizeof(wifi_radio_stat) + channel_size;
126             external_stat_ptr = (wifi_radio_stat *)malloc(total_size);
127             if (external_stat_ptr) {
128                 external_stat_ptr->radio = internal_stat_ptr->radio;
129                 external_stat_ptr->on_time = internal_stat_ptr->on_time;
130                 external_stat_ptr->tx_time = internal_stat_ptr->tx_time;
131                 external_stat_ptr->rx_time = internal_stat_ptr->rx_time;
132                 external_stat_ptr->tx_time_per_levels = NULL;
133                 external_stat_ptr->num_tx_levels = 0;
134                 external_stat_ptr->on_time_scan = internal_stat_ptr->on_time_scan;
135                 external_stat_ptr->on_time_nbd = internal_stat_ptr->on_time_nbd;
136                 external_stat_ptr->on_time_gscan = internal_stat_ptr->on_time_gscan;
137                 external_stat_ptr->on_time_roam_scan = internal_stat_ptr->on_time_roam_scan;
138                 external_stat_ptr->on_time_pno_scan = internal_stat_ptr->on_time_pno_scan;
139                 external_stat_ptr->on_time_hs20 = internal_stat_ptr->on_time_hs20;
140                 external_stat_ptr->num_channels = internal_stat_ptr->num_channels;
141                 if (internal_stat_ptr->num_channels) {
142                     memcpy(&(external_stat_ptr->channels), &(internal_stat_ptr->channels),
143                         channel_size);
144                 }
145             }
146         }
147         return external_stat_ptr;
148     }
149 };
150 
151 wifi_error wifi_get_link_stats(wifi_request_id id,
152         wifi_interface_handle iface, wifi_stats_result_handler handler)
153 {
154     GetLinkStatsCommand command(iface, handler);
155     return (wifi_error) command.requestResponse();
156 }
157 
158 wifi_error wifi_set_link_stats(
159         wifi_interface_handle /* iface */, wifi_link_layer_params /* params */)
160 {
161     /* Return success here since bcom HAL does not need set link stats. */
162     return WIFI_SUCCESS;
163 }
164 
165 wifi_error wifi_clear_link_stats(
166         wifi_interface_handle /* iface */, u32 /* stats_clear_req_mask */,
167         u32 * /* stats_clear_rsp_mask */, u8 /* stop_req */, u8 * /* stop_rsp */)
168 {
169     /* Return success here since bcom HAL does not support clear link stats. */
170     return WIFI_SUCCESS;
171 }
172