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