• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2014 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "wifi_hal.h"
18 
19 #ifndef __WIFI_HAL_COMMON_H__
20 #define __WIFI_HAL_COMMON_H__
21 
22 #ifndef LOG_TAG
23 #define LOG_TAG  "WifiHAL"
24 #endif
25 
26 #include <stdint.h>
27 #include <fcntl.h>
28 #include <inttypes.h>
29 #include <sys/socket.h>
30 #include <netlink/genl/genl.h>
31 #include <netlink/genl/family.h>
32 #include <netlink/genl/ctrl.h>
33 #include <linux/rtnetlink.h>
34 #include <netpacket/packet.h>
35 #include <linux/filter.h>
36 #include <linux/errqueue.h>
37 
38 #include <linux/pkt_sched.h>
39 #include <netlink/object-api.h>
40 #include <netlink/netlink.h>
41 #include <netlink/socket.h>
42 
43 #include "nl80211_copy.h"
44 
45 #include <utils/Log.h>
46 #include "rb_wrapper.h"
47 #include "pkt_stats.h"
48 #include "wifihal_internal.h"
49 #include "qca-vendor_copy.h"
50 
51 #define SOCKET_BUFFER_SIZE      (32768U)
52 #define RECV_BUF_SIZE           (4096)
53 #define DEFAULT_EVENT_CB_SIZE   (64)
54 #define NUM_RING_BUFS           5
55 
56 #define MAC_ADDR_ARRAY(a) (a)[0], (a)[1], (a)[2], (a)[3], (a)[4], (a)[5]
57 #define MAC_ADDR_STR "%02x:%02x:%02x:%02x:%02x:%02x"
58 #define BIT(x) (1 << (x))
59 
60 typedef int16_t s16;
61 typedef int32_t s32;
62 typedef int64_t s64;
63 
64 typedef void (*wifi_internal_event_handler) (wifi_handle handle, int events);
65 
66 class WifiCommand;
67 
68 typedef struct {
69     int nl_cmd;
70     uint32_t vendor_id;
71     int vendor_subcmd;
72     nl_recvmsg_msg_cb_t cb_func;
73     void *cb_arg;
74 } cb_info;
75 
76 typedef struct {
77     wifi_request_id id;
78     WifiCommand *cmd;
79 } cmd_info;
80 
81 typedef struct {
82     wifi_handle handle;                             // handle to wifi data
83     char name[IFNAMSIZ+1];                          // interface name + trailing null
84     int  id;                                        // id to use when talking to driver
85 } interface_info;
86 
87 typedef struct {
88     wifi_gscan_capabilities gscan_capa;
89     wifi_roaming_capabilities roaming_capa;
90 } wifi_capa;
91 
92 typedef struct {
93     u8 *flags;
94     size_t flags_len;
95 } features_info;
96 
97 enum pkt_log_version {
98     PKT_LOG_V0          = 0,     // UNSPECIFIED Target
99     PKT_LOG_V1          = 1,     // ROME Base Target
100     PKT_LOG_V2          = 2,     // HELIUM Base Target
101     PKT_LOG_V3          = 3,     // LETHIUM Base target
102 };
103 
104 struct gscan_event_handlers_s;
105 struct rssi_monitor_event_handler_s;
106 struct cld80211_ctx;
107 
108 typedef struct hal_info_s {
109 
110     struct nl_sock *cmd_sock;                       // command socket object
111     struct nl_sock *event_sock;                     // event socket object
112     struct nl_sock *user_sock;                      // user socket object
113     int nl80211_family_id;                          // family id for 80211 driver
114 
115     bool in_event_loop;                             // Indicates that event loop is active
116     bool clean_up;                                  // Indication to clean up the socket
117 
118     wifi_internal_event_handler event_handler;      // default event handler
119     wifi_cleaned_up_handler cleaned_up_handler;     // socket cleaned up handler
120 
121     cb_info *event_cb;                              // event callbacks
122     int num_event_cb;                               // number of event callbacks
123     int alloc_event_cb;                             // number of allocated callback objects
124     pthread_mutex_t cb_lock;                        // mutex for the event_cb access
125 
126     interface_info **interfaces;                    // array of interfaces
127     int num_interfaces;                             // number of interfaces
128 
129     feature_set supported_feature_set;
130     /* driver supported features defined by enum qca_wlan_vendor_features that
131        can be queried by vendor command QCA_NL80211_VENDOR_SUBCMD_GET_FEATURES */
132     features_info driver_supported_features;
133     u32 supported_logger_feature_set;
134     // add other details
135     int user_sock_arg;
136     int event_sock_arg;
137     struct rb_info rb_infos[NUM_RING_BUFS];
138     void (*on_ring_buffer_data) (char *ring_name, char *buffer, int buffer_size,
139           wifi_ring_buffer_status *status);
140     void (*on_alert) (wifi_request_id id, char *buffer, int buffer_size, int err_code);
141     struct pkt_stats_s *pkt_stats;
142 
143     /* socket pair used to exit from blocking poll*/
144     int exit_sockets[2];
145     u32 rx_buf_size_allocated;
146     u32 rx_buf_size_occupied;
147     wifi_ring_buffer_entry *rx_aggr_pkts;
148     rx_aggr_stats aggr_stats;
149     u32 prev_seq_no;
150     // pointer to structure having various gscan_event_handlers
151     struct gscan_event_handlers_s *gscan_handlers;
152     /* mutex for the log_handler access*/
153     pthread_mutex_t lh_lock;
154     /* mutex for the alert_handler access*/
155     pthread_mutex_t ah_lock;
156     u32 firmware_bus_max_size;
157     bool fate_monitoring_enabled;
158     packet_fate_monitor_info *pkt_fate_stats;
159     /* mutex for the packet fate stats shared resource protection */
160     pthread_mutex_t pkt_fate_stats_lock;
161     struct rssi_monitor_event_handler_s *rssi_handlers;
162     wifi_capa capa;
163     struct cld80211_ctx *cldctx;
164     bool apf_enabled;
165     pkt_log_version  pkt_log_ver;
166 } hal_info;
167 
168 wifi_error wifi_register_handler(wifi_handle handle, int cmd, nl_recvmsg_msg_cb_t func, void *arg);
169 wifi_error wifi_register_vendor_handler(wifi_handle handle,
170             uint32_t id, int subcmd, nl_recvmsg_msg_cb_t func, void *arg);
171 
172 void wifi_unregister_handler(wifi_handle handle, int cmd);
173 void wifi_unregister_vendor_handler(wifi_handle handle, uint32_t id, int subcmd);
174 
175 interface_info *getIfaceInfo(wifi_interface_handle);
176 wifi_handle getWifiHandle(wifi_interface_handle handle);
177 hal_info *getHalInfo(wifi_handle handle);
178 hal_info *getHalInfo(wifi_interface_handle handle);
179 wifi_handle getWifiHandle(hal_info *info);
180 wifi_interface_handle getIfaceHandle(interface_info *info);
181 wifi_error initializeGscanHandlers(hal_info *info);
182 wifi_error cleanupGscanHandlers(hal_info *info);
183 wifi_error initializeRSSIMonitorHandler(hal_info *info);
184 wifi_error cleanupRSSIMonitorHandler(hal_info *info);
185 
186 lowi_cb_table_t *getLowiCallbackTable(u32 requested_lowi_capabilities);
187 
188 wifi_error wifi_start_sending_offloaded_packet(wifi_request_id id,
189         wifi_interface_handle iface, u16 ether_type, u8 *ip_packet,
190         u16 ip_packet_len, u8 *src_mac_addr, u8 *dst_mac_addr, u32 period_msec);
191 wifi_error wifi_stop_sending_offloaded_packet(wifi_request_id id,
192         wifi_interface_handle iface);
193 wifi_error wifi_start_rssi_monitoring(wifi_request_id id, wifi_interface_handle
194         iface, s8 max_rssi, s8 min_rssi, wifi_rssi_event_handler eh);
195 wifi_error wifi_stop_rssi_monitoring(wifi_request_id id, wifi_interface_handle iface);
196 wifi_error wifi_set_radio_mode_change_handler(wifi_request_id id, wifi_interface_handle
197         iface, wifi_radio_mode_change_handler eh);
198 wifi_error mapKernelErrortoWifiHalError(int kern_err);
199 // some common macros
200 
201 #define min(x, y)       ((x) < (y) ? (x) : (y))
202 #define max(x, y)       ((x) > (y) ? (x) : (y))
203 
204 #define REQUEST_ID_MAX 1000
205 #define get_requestid() ((arc4random()%REQUEST_ID_MAX) + 1)
206 #define WAIT_TIME_FOR_SET_REG_DOMAIN 50000
207 
208 #ifndef UNUSED
209 #define UNUSED(x)    (void)(x)
210 #endif
211 
212 #ifdef __cplusplus
213 extern "C"
214 {
215 #endif /* __cplusplus */
216 void hexdump(void *bytes, u16 len);
217 u8 get_rssi(u8 rssi_wo_noise_floor);
218 #ifdef __cplusplus
219 }
220 #endif /* __cplusplus */
221 
222 #endif
223 
224