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:
GetLinkStatsCommand(wifi_interface_handle iface,wifi_stats_result_handler handler)70 GetLinkStatsCommand(wifi_interface_handle iface, wifi_stats_result_handler handler)
71 : WifiCommand("GetLinkStatsCommand", iface, 0), mHandler(handler)
72 { }
73
create()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:
handleResponse(WifiEvent & reply)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 wifi_iface_stat *iface_stat =
110 (wifi_iface_stat *)((char *)&((wifi_radio_stat_internal *)data)->channels
111 + radio_stat->num_channels * sizeof(wifi_channel_stat));
112 (*mHandler.on_link_stats_results)(id, iface_stat, 1, radio_stat);
113 free(radio_stat);
114 return NL_OK;
115 }
116
117 private:
convertToExternalRadioStatStructure(wifi_radio_stat_internal * internal_stat_ptr)118 wifi_radio_stat *convertToExternalRadioStatStructure(wifi_radio_stat_internal *internal_stat_ptr) {
119 wifi_radio_stat *external_stat_ptr = NULL;
120 if (internal_stat_ptr) {
121 uint32_t channel_size = internal_stat_ptr->num_channels * sizeof(wifi_channel_stat);
122 uint32_t total_size = sizeof(wifi_radio_stat) + channel_size;
123 external_stat_ptr = (wifi_radio_stat *)malloc(total_size);
124 if (external_stat_ptr) {
125 external_stat_ptr->radio = internal_stat_ptr->radio;
126 external_stat_ptr->on_time = internal_stat_ptr->on_time;
127 external_stat_ptr->tx_time = internal_stat_ptr->tx_time;
128 external_stat_ptr->rx_time = internal_stat_ptr->rx_time;
129 external_stat_ptr->tx_time_per_levels = NULL;
130 external_stat_ptr->num_tx_levels = 0;
131 external_stat_ptr->on_time_scan = internal_stat_ptr->on_time_scan;
132 external_stat_ptr->on_time_nbd = internal_stat_ptr->on_time_nbd;
133 external_stat_ptr->on_time_gscan = internal_stat_ptr->on_time_gscan;
134 external_stat_ptr->on_time_roam_scan = internal_stat_ptr->on_time_roam_scan;
135 external_stat_ptr->on_time_pno_scan = internal_stat_ptr->on_time_pno_scan;
136 external_stat_ptr->on_time_hs20 = internal_stat_ptr->on_time_hs20;
137 external_stat_ptr->num_channels = internal_stat_ptr->num_channels;
138 if (internal_stat_ptr->num_channels) {
139 memcpy(&(external_stat_ptr->channels), &(internal_stat_ptr->channels),
140 channel_size);
141 }
142 }
143 }
144 return external_stat_ptr;
145 }
146 };
147
wifi_get_link_stats(wifi_request_id id,wifi_interface_handle iface,wifi_stats_result_handler handler)148 wifi_error wifi_get_link_stats(wifi_request_id id,
149 wifi_interface_handle iface, wifi_stats_result_handler handler)
150 {
151 GetLinkStatsCommand command(iface, handler);
152 return (wifi_error) command.requestResponse();
153 }
154
wifi_set_link_stats(wifi_interface_handle,wifi_link_layer_params)155 wifi_error wifi_set_link_stats(
156 wifi_interface_handle /* iface */, wifi_link_layer_params /* params */)
157 {
158 /* Return success here since bcom HAL does not need set link stats. */
159 return WIFI_SUCCESS;
160 }
161
wifi_clear_link_stats(wifi_interface_handle,u32,u32 *,u8,u8 *)162 wifi_error wifi_clear_link_stats(
163 wifi_interface_handle /* iface */, u32 /* stats_clear_req_mask */,
164 u32 * /* stats_clear_rsp_mask */, u8 /* stop_req */, u8 * /* stop_rsp */)
165 {
166 /* Return success here since bcom HAL does not support clear link stats. */
167 return WIFI_SUCCESS;
168 }
169