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