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 <sys/un.h>
31 #include <netlink/genl/genl.h>
32 #include <netlink/genl/family.h>
33 #include <netlink/genl/ctrl.h>
34 #include <linux/rtnetlink.h>
35 #include <netpacket/packet.h>
36 #include <linux/filter.h>
37 #include <linux/errqueue.h>
38 
39 #include <linux/pkt_sched.h>
40 #include <netlink/object-api.h>
41 #include <netlink/netlink.h>
42 #include <netlink/socket.h>
43 
44 #include "nl80211_copy.h"
45 
46 #include <utils/Log.h>
47 #include "rb_wrapper.h"
48 #include "pkt_stats.h"
49 #include "wifihal_internal.h"
50 #include "qca-vendor_copy.h"
51 
52 #define SOCKET_BUFFER_SIZE      (32768U)
53 #define RECV_BUF_SIZE           (4096)
54 #define DEFAULT_EVENT_CB_SIZE   (64)
55 #define NUM_RING_BUFS           5
56 
57 #define WIFI_HAL_CTRL_IFACE     "/dev/socket/wifihal/wifihal_ctrlsock"
58 
59 #define MAC_ADDR_ARRAY(a) (a)[0], (a)[1], (a)[2], (a)[3], (a)[4], (a)[5]
60 #define MAC_ADDR_STR "%02x:%02x:%02x:%02x:%02x:%02x"
61 #ifndef BIT
62 #define BIT(x) (1 << (x))
63 #endif
64 
65 typedef int16_t s16;
66 typedef int32_t s32;
67 typedef int64_t s64;
68 
69 typedef void (*wifi_internal_event_handler) (wifi_handle handle, int events);
70 
71 class WifiCommand;
72 
73 typedef struct {
74     int nl_cmd;
75     uint32_t vendor_id;
76     int vendor_subcmd;
77     nl_recvmsg_msg_cb_t cb_func;
78     void *cb_arg;
79 } cb_info;
80 
81 typedef struct {
82     wifi_request_id id;
83     WifiCommand *cmd;
84 } cmd_info;
85 
86 typedef struct {
87     wifi_handle handle;                             // handle to wifi data
88     char name[IFNAMSIZ+1];                          // interface name + trailing null
89     int  id;                                        // id to use when talking to driver
90 } interface_info;
91 
92 typedef struct {
93     wifi_gscan_capabilities gscan_capa;
94     wifi_roaming_capabilities roaming_capa;
95 } wifi_capa;
96 
97 typedef struct {
98     u8 *flags;
99     size_t flags_len;
100 } features_info;
101 
102 enum pkt_log_version {
103     PKT_LOG_V0          = 0,     // UNSPECIFIED Target
104     PKT_LOG_V1          = 1,     // ROME Base Target
105     PKT_LOG_V2          = 2,     // HELIUM Base Target
106     PKT_LOG_V3          = 3,     // LETHIUM Base target
107 };
108 
109 struct gscan_event_handlers_s;
110 struct rssi_monitor_event_handler_s;
111 struct cld80211_ctx;
112 
113 struct ctrl_sock {
114     int s;
115     struct sockaddr_un local;
116 };
117 
118 typedef struct hal_info_s {
119 
120     struct nl_sock *cmd_sock;                       // command socket object
121     struct nl_sock *event_sock;                     // event socket object
122     struct nl_sock *user_sock;                      // user socket object
123     struct ctrl_sock wifihal_ctrl_sock;             // ctrl sock object
124     struct list_head monitor_sockets;               // list of monitor sockets
125     int nl80211_family_id;                          // family id for 80211 driver
126 
127     bool in_event_loop;                             // Indicates that event loop is active
128     bool clean_up;                                  // Indication to clean up the socket
129 
130     wifi_internal_event_handler event_handler;      // default event handler
131     wifi_cleaned_up_handler cleaned_up_handler;     // socket cleaned up handler
132 
133     cb_info *event_cb;                              // event callbacks
134     int num_event_cb;                               // number of event callbacks
135     int alloc_event_cb;                             // number of allocated callback objects
136     pthread_mutex_t cb_lock;                        // mutex for the event_cb access
137 
138     interface_info **interfaces;                    // array of interfaces
139     int num_interfaces;                             // number of interfaces
140 
141     feature_set supported_feature_set;
142     /* driver supported features defined by enum qca_wlan_vendor_features that
143        can be queried by vendor command QCA_NL80211_VENDOR_SUBCMD_GET_FEATURES */
144     features_info driver_supported_features;
145     u32 supported_logger_feature_set;
146     // add other details
147     int user_sock_arg;
148     int event_sock_arg;
149     struct rb_info rb_infos[NUM_RING_BUFS];
150     void (*on_ring_buffer_data) (char *ring_name, char *buffer, int buffer_size,
151           wifi_ring_buffer_status *status);
152     void (*on_alert) (wifi_request_id id, char *buffer, int buffer_size, int err_code);
153     struct pkt_stats_s *pkt_stats;
154 
155     /* socket pair used to exit from blocking poll*/
156     int exit_sockets[2];
157     u32 rx_buf_size_allocated;
158     u32 rx_buf_size_occupied;
159     wifi_ring_buffer_entry *rx_aggr_pkts;
160     rx_aggr_stats aggr_stats;
161     u32 prev_seq_no;
162     // pointer to structure having various gscan_event_handlers
163     struct gscan_event_handlers_s *gscan_handlers;
164     /* mutex for the log_handler access*/
165     pthread_mutex_t lh_lock;
166     /* mutex for the alert_handler access*/
167     pthread_mutex_t ah_lock;
168     u32 firmware_bus_max_size;
169     bool fate_monitoring_enabled;
170     packet_fate_monitor_info *pkt_fate_stats;
171     /* mutex for the packet fate stats shared resource protection */
172     pthread_mutex_t pkt_fate_stats_lock;
173     struct rssi_monitor_event_handler_s *rssi_handlers;
174     wifi_capa capa;
175     struct cld80211_ctx *cldctx;
176     bool apf_enabled;
177     pkt_log_version  pkt_log_ver;
178 } hal_info;
179 
180 wifi_error wifi_register_handler(wifi_handle handle, int cmd, nl_recvmsg_msg_cb_t func, void *arg);
181 wifi_error wifi_register_vendor_handler(wifi_handle handle,
182             uint32_t id, int subcmd, nl_recvmsg_msg_cb_t func, void *arg);
183 
184 void wifi_unregister_handler(wifi_handle handle, int cmd);
185 void wifi_unregister_vendor_handler(wifi_handle handle, uint32_t id, int subcmd);
186 
187 interface_info *getIfaceInfo(wifi_interface_handle);
188 wifi_handle getWifiHandle(wifi_interface_handle handle);
189 hal_info *getHalInfo(wifi_handle handle);
190 hal_info *getHalInfo(wifi_interface_handle handle);
191 wifi_handle getWifiHandle(hal_info *info);
192 wifi_interface_handle getIfaceHandle(interface_info *info);
193 wifi_error initializeGscanHandlers(hal_info *info);
194 wifi_error cleanupGscanHandlers(hal_info *info);
195 wifi_error initializeRSSIMonitorHandler(hal_info *info);
196 wifi_error cleanupRSSIMonitorHandler(hal_info *info);
197 
198 lowi_cb_table_t *getLowiCallbackTable(u32 requested_lowi_capabilities);
199 
200 wifi_error wifi_start_sending_offloaded_packet(wifi_request_id id,
201         wifi_interface_handle iface, u16 ether_type, u8 *ip_packet,
202         u16 ip_packet_len, u8 *src_mac_addr, u8 *dst_mac_addr, u32 period_msec);
203 wifi_error wifi_stop_sending_offloaded_packet(wifi_request_id id,
204         wifi_interface_handle iface);
205 wifi_error wifi_start_rssi_monitoring(wifi_request_id id, wifi_interface_handle
206         iface, s8 max_rssi, s8 min_rssi, wifi_rssi_event_handler eh);
207 wifi_error wifi_stop_rssi_monitoring(wifi_request_id id, wifi_interface_handle iface);
208 wifi_error wifi_set_radio_mode_change_handler(wifi_request_id id, wifi_interface_handle
209         iface, wifi_radio_mode_change_handler eh);
210 wifi_error mapKernelErrortoWifiHalError(int kern_err);
211 void wifi_cleanup_dynamic_ifaces(wifi_handle handle);
212 wifi_error wifi_virtual_interface_create(wifi_handle handle, const char* ifname,
213                                          wifi_interface_type iface_type);
214 wifi_error wifi_virtual_interface_delete(wifi_handle handle, const char* ifname);
215 // some common macros
216 
217 #define min(x, y)       ((x) < (y) ? (x) : (y))
218 #define max(x, y)       ((x) > (y) ? (x) : (y))
219 
220 #define REQUEST_ID_MAX 1000
221 #define get_requestid() ((arc4random()%REQUEST_ID_MAX) + 1)
222 #define WAIT_TIME_FOR_SET_REG_DOMAIN 50000
223 
224 #ifndef UNUSED
225 #define UNUSED(x)    (void)(x)
226 #endif
227 
228 #ifdef __cplusplus
229 extern "C"
230 {
231 #endif /* __cplusplus */
232 void hexdump(void *bytes, u16 len);
233 u8 get_rssi(u8 rssi_wo_noise_floor);
234 #ifdef __cplusplus
235 }
236 #endif /* __cplusplus */
237 
238 #endif
239 
240