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 "wifi_hal.h"
20 
21 #ifndef __WIFI_HAL_COMMON_H__
22 #define __WIFI_HAL_COMMON_H__
23 
24 #define LOG_TAG  "WifiHAL"
25 
26 #include <log/log.h>
27 #include "nl80211_copy.h"
28 #include "sync.h"
29 
30 #define SOCKET_BUFFER_SIZE      (32768U)
31 #define RECV_BUF_SIZE           (4096)
32 #define DEFAULT_EVENT_CB_SIZE   (64)
33 #define DEFAULT_CMD_SIZE        (64)
34 #define DOT11_OUI_LEN             3
35 #define DOT11_MAX_SSID_LEN        32
36 
37 #define MAX_PROBE_RESP_IE_LEN      2048
38 /*
39  Vendor OUI - This is a unique identifier that identifies organization. Lets
40  code Android specific functions with Google OUI; although vendors can do more
41  with their own OUI's as well.
42  */
43 
44 const uint32_t GOOGLE_OUI = 0x001A11;
45 /* TODO: define vendor OUI here */
46 
47 
48 /*
49  This enum defines ranges for various commands; commands themselves
50  can be defined in respective feature headers; i.e. find gscan command
51  definitions in gscan.cpp
52  */
53 
54 typedef enum {
55     /* don't use 0 as a valid subcommand */
56     VENDOR_NL80211_SUBCMD_UNSPECIFIED,
57 
58     /* define all vendor startup commands between 0x0 and 0x0FFF */
59     VENDOR_NL80211_SUBCMD_RANGE_START = 0x0001,
60     VENDOR_NL80211_SUBCMD_RANGE_END   = 0x0FFF,
61 
62     /* define all GScan related commands between 0x1000 and 0x10FF */
63     ANDROID_NL80211_SUBCMD_GSCAN_RANGE_START = 0x1000,
64     ANDROID_NL80211_SUBCMD_GSCAN_RANGE_END   = 0x10FF,
65 
66     /* define all NearbyDiscovery related commands between 0x1100 and 0x11FF */
67     ANDROID_NL80211_SUBCMD_NBD_RANGE_START = 0x1100,
68     ANDROID_NL80211_SUBCMD_NBD_RANGE_END   = 0x11FF,
69 
70     /* define all RTT related commands between 0x1100 and 0x11FF */
71     ANDROID_NL80211_SUBCMD_RTT_RANGE_START = 0x1100,
72     ANDROID_NL80211_SUBCMD_RTT_RANGE_END   = 0x11FF,
73 
74     ANDROID_NL80211_SUBCMD_LSTATS_RANGE_START = 0x1200,
75     ANDROID_NL80211_SUBCMD_LSTATS_RANGE_END   = 0x12FF,
76 
77     /* define all Logger related commands between 0x1400 and 0x14FF */
78     ANDROID_NL80211_SUBCMD_DEBUG_RANGE_START = 0x1400,
79     ANDROID_NL80211_SUBCMD_DEBUG_RANGE_END   = 0x14FF,
80 
81     /* define all wifi offload related commands between 0x1600 and 0x16FF */
82     ANDROID_NL80211_SUBCMD_WIFI_OFFLOAD_RANGE_START = 0x1600,
83     ANDROID_NL80211_SUBCMD_WIFI_OFFLOAD_RANGE_END   = 0x16FF,
84 
85     /* define all NAN related commands between 0x1700 and 0x17FF */
86     ANDROID_NL80211_SUBCMD_NAN_RANGE_START = 0x1700,
87     ANDROID_NL80211_SUBCMD_NAN_RANGE_END   = 0x17FF,
88 
89     /* define all Android Packet Filter related commands between 0x1800 and 0x18FF */
90     ANDROID_NL80211_SUBCMD_PKT_FILTER_RANGE_START = 0x1800,
91     ANDROID_NL80211_SUBCMD_PKT_FILTER_RANGE_END   = 0x18FF,
92 
93     /* This is reserved for future usage */
94 
95 } ANDROID_VENDOR_SUB_COMMAND;
96 
97 typedef enum {
98 
99     GSCAN_SUBCMD_GET_CAPABILITIES = ANDROID_NL80211_SUBCMD_GSCAN_RANGE_START,
100 
101     GSCAN_SUBCMD_SET_CONFIG,                            /* 0x1001 */
102 
103     GSCAN_SUBCMD_SET_SCAN_CONFIG,                       /* 0x1002 */
104     GSCAN_SUBCMD_ENABLE_GSCAN,                          /* 0x1003 */
105     GSCAN_SUBCMD_GET_SCAN_RESULTS,                      /* 0x1004 */
106     GSCAN_SUBCMD_SCAN_RESULTS,                          /* 0x1005 */
107 
108     GSCAN_SUBCMD_SET_HOTLIST,                           /* 0x1006 */
109 
110     GSCAN_SUBCMD_SET_SIGNIFICANT_CHANGE_CONFIG,         /* 0x1007 */
111     GSCAN_SUBCMD_ENABLE_FULL_SCAN_RESULTS,              /* 0x1008 */
112     GSCAN_SUBCMD_GET_CHANNEL_LIST,                       /* 0x1009 */
113 
114     WIFI_SUBCMD_GET_FEATURE_SET,                         /* 0x100A */
115     WIFI_SUBCMD_GET_FEATURE_SET_MATRIX,                  /* 0x100B */
116     WIFI_SUBCMD_SET_PNO_RANDOM_MAC_OUI,                  /* 0x100C */
117     WIFI_SUBCMD_NODFS_SET,                               /* 0x100D */
118     WIFI_SUBCMD_SET_COUNTRY_CODE,                             /* 0x100E */
119     /* Add more sub commands here */
120     GSCAN_SUBCMD_SET_EPNO_SSID,                          /* 0x100F */
121 
122     WIFI_SUBCMD_SET_SSID_WHITE_LIST,                    /* 0x1010 */
123     WIFI_SUBCMD_SET_ROAM_PARAMS,                        /* 0x1011 */
124     WIFI_SUBCMD_ENABLE_LAZY_ROAM,                       /* 0x1012 */
125     WIFI_SUBCMD_SET_BSSID_PREF,                         /* 0x1013 */
126     WIFI_SUBCMD_SET_BSSID_BLACKLIST,                     /* 0x1014 */
127 
128     GSCAN_SUBCMD_ANQPO_CONFIG,                          /* 0x1015 */
129     WIFI_SUBCMD_SET_RSSI_MONITOR,                       /* 0x1016 */
130     WIFI_SUBCMD_CONFIG_ND_OFFLOAD,                      /* 0x1017 */
131     /* Add more sub commands here */
132 
133     GSCAN_SUBCMD_MAX,
134 
135     APF_SUBCMD_GET_CAPABILITIES = ANDROID_NL80211_SUBCMD_PKT_FILTER_RANGE_START,
136     APF_SUBCMD_SET_FILTER,
137 } WIFI_SUB_COMMAND;
138 
139 typedef enum {
140     BRCM_RESERVED1,
141     BRCM_RESERVED2,
142     GSCAN_EVENT_SIGNIFICANT_CHANGE_RESULTS ,
143     GSCAN_EVENT_HOTLIST_RESULTS_FOUND,
144     GSCAN_EVENT_SCAN_RESULTS_AVAILABLE,
145     GSCAN_EVENT_FULL_SCAN_RESULTS,
146     RTT_EVENT_COMPLETE,
147     GSCAN_EVENT_COMPLETE_SCAN,
148     GSCAN_EVENT_HOTLIST_RESULTS_LOST,
149     GSCAN_EVENT_EPNO_EVENT,
150     GOOGLE_DEBUG_RING_EVENT,
151     GOOGLE_DEBUG_MEM_DUMP_EVENT,
152     GSCAN_EVENT_ANQPO_HOTSPOT_MATCH,
153     GOOGLE_RSSI_MONITOR_EVENT
154 } WIFI_EVENT;
155 
156 typedef void (*wifi_internal_event_handler) (wifi_handle handle, int events);
157 
158 class WifiCommand;
159 
160 typedef struct {
161     int nl_cmd;
162     uint32_t vendor_id;
163     int vendor_subcmd;
164     nl_recvmsg_msg_cb_t cb_func;
165     void *cb_arg;
166 } cb_info;
167 
168 typedef struct {
169     wifi_request_id id;
170     WifiCommand *cmd;
171 } cmd_info;
172 
173 typedef struct {
174     wifi_handle handle;                             // handle to wifi data
175     char name[IFNAMSIZ+1];                          // interface name + trailing null
176     int  id;                                        // id to use when talking to driver
177 } interface_info;
178 
179 typedef struct {
180 
181     struct nl_sock *cmd_sock;                       // command socket object
182     struct nl_sock *event_sock;                     // event socket object
183     int nl80211_family_id;                          // family id for 80211 driver
184     int cleanup_socks[2];                           // sockets used to implement wifi_cleanup
185 
186     bool in_event_loop;                             // Indicates that event loop is active
187     bool clean_up;                                  // Indication to exit since cleanup has started
188 
189     wifi_internal_event_handler event_handler;      // default event handler
190     wifi_cleaned_up_handler cleaned_up_handler;     // socket cleaned up handler
191 
192     cb_info *event_cb;                              // event callbacks
193     int num_event_cb;                               // number of event callbacks
194     int alloc_event_cb;                             // number of allocated callback objects
195     pthread_mutex_t cb_lock;                        // mutex for the event_cb access
196 
197     cmd_info *cmd;                                  // Outstanding commands
198     int num_cmd;                                    // number of commands
199     int alloc_cmd;                                  // number of commands allocated
200 
201     interface_info **interfaces;                    // array of interfaces
202     int num_interfaces;                             // number of interfaces
203 
204 
205     // add other details
206 } hal_info;
207 
208 #define PNO_SSID_FOUND  0x1
209 #define PNO_SSID_LOST    0x2
210 
211 typedef struct wifi_pno_result {
212     unsigned char ssid[DOT11_MAX_SSID_LEN];
213     unsigned char ssid_len;
214     signed char rssi;
215     u16 channel;
216     u16 flags;
217     mac_addr  bssid;
218 } wifi_pno_result_t;
219 
220 typedef struct wifi_gscan_result {
221     u64 ts;                           // Time of discovery
222     u8 ssid[DOT11_MAX_SSID_LEN+1];    // null terminated
223     mac_addr bssid;                   // BSSID
224     u32 channel;                      // channel frequency in MHz
225     s32 rssi;                         // in db
226     u64 rtt;                          // in nanoseconds
227     u64 rtt_sd;                       // standard deviation in rtt
228     u16 beacon_period;                // units are Kusec
229     u16 capability;                   // Capability information
230     u32 pad;
231 } wifi_gscan_result_t;
232 
233 typedef struct wifi_gscan_full_result {
234     wifi_gscan_result_t fixed;
235     u32 scan_ch_bucket;              // scan chbucket bitmask
236     u32 ie_length;                   // byte length of Information Elements
237     u8  ie_data[1];                  // IE data to follow
238 } wifi_gscan_full_result_t;
239 
240 wifi_error wifi_register_handler(wifi_handle handle, int cmd, nl_recvmsg_msg_cb_t func, void *arg);
241 wifi_error wifi_register_vendor_handler(wifi_handle handle,
242             uint32_t id, int subcmd, nl_recvmsg_msg_cb_t func, void *arg);
243 
244 void wifi_unregister_handler(wifi_handle handle, int cmd);
245 void wifi_unregister_vendor_handler(wifi_handle handle, uint32_t id, int subcmd);
246 
247 wifi_error wifi_register_cmd(wifi_handle handle, int id, WifiCommand *cmd);
248 WifiCommand *wifi_unregister_cmd(wifi_handle handle, int id);
249 WifiCommand *wifi_get_cmd(wifi_handle handle, int id);
250 void wifi_unregister_cmd(wifi_handle handle, WifiCommand *cmd);
251 
252 interface_info *getIfaceInfo(wifi_interface_handle);
253 wifi_handle getWifiHandle(wifi_interface_handle handle);
254 hal_info *getHalInfo(wifi_handle handle);
255 hal_info *getHalInfo(wifi_interface_handle handle);
256 wifi_handle getWifiHandle(hal_info *info);
257 wifi_interface_handle getIfaceHandle(interface_info *info);
258 wifi_error wifi_cancel_cmd(wifi_request_id id, wifi_interface_handle iface);
259 
260 // some common macros
261 
262 #define min(x, y)       ((x) < (y) ? (x) : (y))
263 #define max(x, y)       ((x) > (y) ? (x) : (y))
264 
265 #define NULL_CHECK_RETURN(ptr, str, ret) \
266     do { \
267         if (!(ptr)) { \
268             ALOGE("%s(): null pointer - #ptr (%s)\n", __FUNCTION__, str); \
269             return ret; \
270         } \
271     } while (0)
272 
273 #endif
274 
275