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 ETHERTYPE_IP            0x0800          /* IP */
38 #define ETHERTYPE_IPV6          0x86dd          /* IP protocol version 6 */
39 #define MAX_PROBE_RESP_IE_LEN      2048
40 /*
41  Vendor OUI - This is a unique identifier that identifies organization. Lets
42  code Android specific functions with Google OUI; although vendors can do more
43  with their own OUI's as well.
44  */
45 
46 const uint32_t GOOGLE_OUI = 0x001A11;
47 const uint32_t BRCM_OUI =  0x001018;
48 /* TODO: define vendor OUI here */
49 
50 
51 #define MAC2STR(a) (a)[0], (a)[1], (a)[2], (a)[3], (a)[4], (a)[5]
52 #define MACSTR "%02x:%02x:%02x:%02x:%02x:%02x"
53 
54 #define NMR2STR(a) (a)[0], (a)[1], (a)[2], (a)[3], (a)[4], (a)[5], (a)[6], (a)[7]
55 #define NMRSTR "%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x"
56 #define NAN_MASTER_RANK_LEN 8
57 
58 #define SAR_CONFIG_SCENARIO_COUNT      100
59 
60 /*
61  This enum defines ranges for various commands; commands themselves
62  can be defined in respective feature headers; i.e. find gscan command
63  definitions in gscan.cpp
64  */
65 
66 typedef int (*nl_recvmsg_msg_cb_t)(struct nl_msg *msg, void *arg);
67 typedef enum {
68     /* don't use 0 as a valid subcommand */
69     VENDOR_NL80211_SUBCMD_UNSPECIFIED,
70 
71     /* define all vendor startup commands between 0x0 and 0x0FFF */
72     VENDOR_NL80211_SUBCMD_RANGE_START = 0x0001,
73     VENDOR_NL80211_SUBCMD_RANGE_END   = 0x0FFF,
74 
75     /* define all GScan related commands between 0x1000 and 0x10FF */
76     ANDROID_NL80211_SUBCMD_GSCAN_RANGE_START = 0x1000,
77     ANDROID_NL80211_SUBCMD_GSCAN_RANGE_END   = 0x10FF,
78 
79     /* define all NearbyDiscovery related commands between 0x1100 and 0x11FF */
80     ANDROID_NL80211_SUBCMD_NBD_RANGE_START = 0x1100,
81     ANDROID_NL80211_SUBCMD_NBD_RANGE_END   = 0x11FF,
82 
83     /* define all RTT related commands between 0x1100 and 0x11FF */
84     ANDROID_NL80211_SUBCMD_RTT_RANGE_START = 0x1100,
85     ANDROID_NL80211_SUBCMD_RTT_RANGE_END   = 0x11FF,
86 
87     ANDROID_NL80211_SUBCMD_LSTATS_RANGE_START = 0x1200,
88     ANDROID_NL80211_SUBCMD_LSTATS_RANGE_END   = 0x12FF,
89 
90     /* define all Logger related commands between 0x1400 and 0x14FF */
91     ANDROID_NL80211_SUBCMD_DEBUG_RANGE_START = 0x1400,
92     ANDROID_NL80211_SUBCMD_DEBUG_RANGE_END   = 0x14FF,
93 
94     /* define all wifi offload related commands between 0x1600 and 0x16FF */
95     ANDROID_NL80211_SUBCMD_WIFI_OFFLOAD_RANGE_START = 0x1600,
96     ANDROID_NL80211_SUBCMD_WIFI_OFFLOAD_RANGE_END   = 0x16FF,
97 
98     /* define all NAN related commands between 0x1700 and 0x17FF */
99     ANDROID_NL80211_SUBCMD_NAN_RANGE_START = 0x1700,
100     ANDROID_NL80211_SUBCMD_NAN_RANGE_END   = 0x17FF,
101 
102     /* define all Android Packet Filter related commands between 0x1800 and 0x18FF */
103     ANDROID_NL80211_SUBCMD_PKT_FILTER_RANGE_START = 0x1800,
104     ANDROID_NL80211_SUBCMD_PKT_FILTER_RANGE_END   = 0x18FF,
105 
106     /* define all tx power related commands between 0x1900 and 0x1910 */
107     ANDROID_NL80211_SUBCMD_TX_POWER_RANGE_START = 0x1900,
108     ANDROID_NL80211_SUBCMD_TX_POWER_RANGE_END	 = 0x1910,
109 
110     /* define all thermal mode related commands between 0x1920 and 0x192F */
111     ANDROID_NL80211_SUBCMD_MITIGATION_RANGE_START = 0x1920,
112     ANDROID_NL80211_SUBCMD_MITIGATION_RANGE_END   = 0x192F,
113 
114     /* define all DSCP related commands between 0x2000 and 0x20FF */
115     ANDROID_NL80211_SUBCMD_DSCP_RANGE_START =	0x2000,
116     ANDROID_NL80211_SUBCMD_DSCP_RANGE_END   =	0x20FF,
117 
118     /* define all Channel Avoidance related commands between 0x2100 and 0x211F */
119     ANDROID_NL80211_SUBCMD_CHAVOID_RANGE_START =    0x2100,
120     ANDROID_NL80211_SUBCMD_CHAVOID_RANGE_END   =    0x211F,
121 
122     /* define all OTA Download related commands between 0x2120 and 0x212F */
123     ANDROID_NL80211_SUBCMD_OTA_DOWNLOAD_START	= 0x2120,
124     ANDROID_NL80211_SUBCMD_OTA_DOWNLOAD_END	= 0x212F,
125 
126     /* define all VOIP mode config related commands between 0x2130 and 0x213F */
127     ANDROID_NL80211_SUBCMD_VIOP_MODE_START =	    0x2130,
128     ANDROID_NL80211_SUBCMD_VIOP_MODE_END =	    0x213F,
129 
130     /* define all TWT related commands between 0x2140 and 0x214F */
131     ANDROID_NL80211_SUBCMD_TWT_START =              0x2140,
132     ANDROID_NL80211_SUBCMD_TWT_END =                0x214F,
133 
134     /* define all Usable Channel related commands between 0x2150 and 0x215F */
135     ANDROID_NL80211_SUBCMD_USABLE_CHANNEL_START =   0x2150,
136     ANDROID_NL80211_SUBCMD_USABLE_CHANNEL_END =     0x215F,
137 
138     /* define all init/deinit related commands between 0x2160 and 0x216F */
139     ANDROID_NL80211_SUBCMD_INIT_DEINIT_RANGE_START = 0x2160,
140     ANDROID_NL80211_SUBCMD_INIT_DEINIT_RANGE_END   = 0x216F,
141 
142     /* This is reserved for future usage */
143 
144 } ANDROID_VENDOR_SUB_COMMAND;
145 
146 typedef enum {
147 
148     GSCAN_SUBCMD_GET_CAPABILITIES = ANDROID_NL80211_SUBCMD_GSCAN_RANGE_START,
149 
150     GSCAN_SUBCMD_SET_CONFIG,                            /* 0x1001 */
151 
152     GSCAN_SUBCMD_SET_SCAN_CONFIG,                       /* 0x1002 */
153     GSCAN_SUBCMD_ENABLE_GSCAN,                          /* 0x1003 */
154     GSCAN_SUBCMD_GET_SCAN_RESULTS,                      /* 0x1004 */
155     GSCAN_SUBCMD_SCAN_RESULTS,                          /* 0x1005 */
156 
157     GSCAN_SUBCMD_SET_HOTLIST,                           /* 0x1006 */
158 
159     GSCAN_SUBCMD_SET_SIGNIFICANT_CHANGE_CONFIG,         /* 0x1007 */
160     GSCAN_SUBCMD_ENABLE_FULL_SCAN_RESULTS,              /* 0x1008 */
161     GSCAN_SUBCMD_GET_CHANNEL_LIST,                       /* 0x1009 */
162 
163     WIFI_SUBCMD_GET_FEATURE_SET,                         /* 0x100A */
164     WIFI_SUBCMD_GET_FEATURE_SET_MATRIX,                  /* 0x100B */
165     WIFI_SUBCMD_SET_PNO_RANDOM_MAC_OUI,                  /* 0x100C */
166     WIFI_SUBCMD_NODFS_SET,                               /* 0x100D */
167     WIFI_SUBCMD_SET_COUNTRY_CODE,                        /* 0x100E */
168     /* Add more sub commands here */
169     GSCAN_SUBCMD_SET_EPNO_SSID,                          /* 0x100F */
170 
171     WIFI_SUBCMD_SET_SSID_WHITE_LIST,                     /* 0x1010 */
172     WIFI_SUBCMD_SET_ROAM_PARAMS,                         /* 0x1011 */
173     WIFI_SUBCMD_ENABLE_LAZY_ROAM,                        /* 0x1012 */
174     WIFI_SUBCMD_SET_BSSID_PREF,                          /* 0x1013 */
175     WIFI_SUBCMD_SET_BSSID_BLACKLIST,                     /* 0x1014 */
176 
177     GSCAN_SUBCMD_ANQPO_CONFIG,                           /* 0x1015 */
178     WIFI_SUBCMD_SET_RSSI_MONITOR,                        /* 0x1016 */
179     WIFI_SUBCMD_CONFIG_ND_OFFLOAD,                       /* 0x1017 */
180     WIFI_SUBCMD_CONFIG_TCPACK_SUP,                       /* 0x1018 */
181     WIFI_SUBCMD_FW_ROAM_POLICY,                          /* 0x1019 */
182     WIFI_SUBCMD_ROAM_CAPABILITY,                         /* 0x101a */
183     WIFI_SUBCMD_SET_LATENCY_MODE,                        /* 0x101b */
184     WIFI_SUBCMD_SET_MULTISTA_PRIMARY_CONNECTION,         /* 0x101c */
185     WIFI_SUBCMD_SET_MULTISTA_USE_CASE,                   /* 0x101d */
186     WIFI_SUBCMD_SET_DTIM_CONFIG,                         /* 0x101e */
187 
188     GSCAN_SUBCMD_MAX,
189 
190     /* NAN related */
191     NAN_SUBCMD_ENABLE = ANDROID_NL80211_SUBCMD_NAN_RANGE_START,
192     NAN_SUBCMD_DISABLE,                                 /* 0x1701 */
193     NAN_SUBCMD_PUBLISH,                                 /* 0x1702 */
194     NAN_SUBCMD_SUBSCRIBE,                               /* 0x1703 */
195     NAN_SUBCMD_PUBLISH_CANCEL,                          /* 0x1704 */
196     NAN_SUBCMD_SUBSCRIBE_CANCEL,                        /* 0x1705 */
197     NAN_SUBCMD_TRANSMIT_FOLLOWUP,                       /* 0x1706 */
198     NAN_SUBCMD_CONFIG,                                  /* 0x1707 */
199     NAN_SUBCMD_TCA,                                     /* 0x1708 */
200     NAN_SUBCMD_STATS,                                   /* 0x1709 */
201     NAN_SUBCMD_GET_CAPABILITIES,                        /* 0x170A */
202     NAN_SUBCMD_DATA_PATH_IFACE_CREATE,                  /* 0x170B */
203     NAN_SUBCMD_DATA_PATH_IFACE_DELETE,                  /* 0x170C */
204     NAN_SUBCMD_DATA_PATH_REQUEST,                       /* 0x170D */
205     NAN_SUBCMD_DATA_PATH_RESPONSE,                      /* 0x170E */
206     NAN_SUBCMD_DATA_PATH_END,                           /* 0x170F */
207     NAN_SUBCMD_DATA_PATH_SEC_INFO,                      /* 0x1710 */
208     NAN_SUBCMD_VERSION_INFO,                            /* 0x1711 */
209     NAN_SUBCMD_ENABLE_MERGE,                            /* 0x1712 */
210     APF_SUBCMD_GET_CAPABILITIES = ANDROID_NL80211_SUBCMD_PKT_FILTER_RANGE_START,
211     APF_SUBCMD_SET_FILTER,
212     APF_SUBCMD_READ_FILTER,
213     WIFI_SUBCMD_TX_POWER_SCENARIO = ANDROID_NL80211_SUBCMD_TX_POWER_RANGE_START,
214     WIFI_SUBCMD_THERMAL_MITIGATION = ANDROID_NL80211_SUBCMD_MITIGATION_RANGE_START,
215     DSCP_SUBCMD_SET_TABLE = ANDROID_NL80211_SUBCMD_DSCP_RANGE_START,
216     DSCP_SUBCMD_RESET_TABLE,			    	/* 0x2001 */
217     CHAVOID_SUBCMD_SET_CONFIG = ANDROID_NL80211_SUBCMD_CHAVOID_RANGE_START,
218 
219     TWT_SUBCMD_GETCAPABILITY	= ANDROID_NL80211_SUBCMD_TWT_START,
220     TWT_SUBCMD_SETUP_REQUEST,
221     TWT_SUBCMD_TEAR_DOWN_REQUEST,
222     TWT_SUBCMD_INFO_FRAME_REQUEST,
223     TWT_SUBCMD_GETSTATS,
224     TWT_SUBCMD_CLR_STATS,
225 
226     WIFI_SUBCMD_CONFIG_VOIP_MODE = ANDROID_NL80211_SUBCMD_VIOP_MODE_START,
227 
228     WIFI_SUBCMD_GET_OTA_CURRUNT_INFO = ANDROID_NL80211_SUBCMD_OTA_DOWNLOAD_START,
229     WIFI_SUBCMD_OTA_UPDATE,
230     WIFI_SUBCMD_USABLE_CHANNEL = ANDROID_NL80211_SUBCMD_USABLE_CHANNEL_START,
231     WIFI_SUBCMD_TRIGGER_SSR = ANDROID_NL80211_SUBCMD_INIT_DEINIT_RANGE_START,
232 
233 } WIFI_SUB_COMMAND;
234 
235 typedef enum {
236     BRCM_RESERVED1				= 0,
237     BRCM_RESERVED2				= 1,
238     GSCAN_EVENT_SIGNIFICANT_CHANGE_RESULTS	= 2,
239     GSCAN_EVENT_HOTLIST_RESULTS_FOUND		= 3,
240     GSCAN_EVENT_SCAN_RESULTS_AVAILABLE		= 4,
241     GSCAN_EVENT_FULL_SCAN_RESULTS		= 5,
242     RTT_EVENT_COMPLETE				= 6,
243     GSCAN_EVENT_COMPLETE_SCAN			= 7,
244     GSCAN_EVENT_HOTLIST_RESULTS_LOST		= 8,
245     GSCAN_EVENT_EPNO_EVENT			= 9,
246     GOOGLE_DEBUG_RING_EVENT			= 10,
247     GOOGLE_DEBUG_MEM_DUMP_EVENT			= 11,
248     GSCAN_EVENT_ANQPO_HOTSPOT_MATCH		= 12,
249     GOOGLE_RSSI_MONITOR_EVENT			= 13,
250     GOOGLE_MKEEP_ALIVE				= 14,
251 
252     /*
253      * BRCM specific events should be placed after the Generic events
254      * in order to match between the DHD and HAL
255      */
256     NAN_EVENT_ENABLED				= 15,
257     NAN_EVENT_DISABLED				= 16,
258     NAN_EVENT_SUBSCRIBE_MATCH			= 17,
259     NAN_EVENT_PUBLISH_REPLIED_IND		= 18,
260     NAN_EVENT_PUBLISH_TERMINATED		= 19,
261     NAN_EVENT_SUBSCRIBE_TERMINATED		= 20,
262     NAN_EVENT_DE_EVENT				= 21,
263     NAN_EVENT_FOLLOWUP				= 22,
264     NAN_EVENT_TRANSMIT_FOLLOWUP_IND		= 23,
265     NAN_EVENT_DATA_REQUEST			= 24,
266     NAN_EVENT_DATA_CONFIRMATION			= 25,
267     NAN_EVENT_DATA_END				= 26,
268     NAN_EVENT_BEACON				= 27,
269     NAN_EVENT_SDF				= 28,
270     NAN_EVENT_TCA				= 29,
271     NAN_EVENT_SUBSCRIBE_UNMATCH			= 30,
272     NAN_EVENT_UNKNOWN				= 31,
273     BRCM_VENDOR_EVENT_HANGED			= 33,
274     ROAM_EVENT_START,
275     GOOGLE_FILE_DUMP_EVENT			= 37,
276     NAN_ASYNC_RESPONSE_DISABLED			= 40,
277     BRCM_VENDOR_EVENT_TWT			= 43,
278     BRCM_TPUT_DUMP_EVENT			= 44,
279     NAN_EVENT_MATCH_EXPIRY			= 45
280 } WIFI_EVENT;
281 
282 typedef void (*wifi_internal_event_handler) (wifi_handle handle, int events);
283 
284 class WifiCommand;
285 
286 typedef struct {
287     int nl_cmd;
288     uint32_t vendor_id;
289     int vendor_subcmd;
290     nl_recvmsg_msg_cb_t cb_func;
291     void *cb_arg;
292 } cb_info;
293 
294 typedef struct {
295     wifi_request_id id;
296     WifiCommand *cmd;
297 } cmd_info;
298 
299 typedef struct {
300     wifi_handle handle;                             // handle to wifi data
301     char name[IFNAMSIZ+1];                          // interface name + trailing null
302     int  id;                                        // id to use when talking to driver
303     bool is_virtual;                                // mark each iface as virtual or static
304 } interface_info;
305 
306 typedef struct {
307 
308     struct nl_sock *cmd_sock;                       // command socket object
309     struct nl_sock *event_sock;                     // event socket object
310     int nl80211_family_id;                          // family id for 80211 driver
311     int cleanup_socks[2];                           // sockets used to implement wifi_cleanup
312 
313     bool in_event_loop;                             // Indicates that event loop is active
314     bool clean_up;                                  // Indication to exit since cleanup has started
315 
316     wifi_internal_event_handler event_handler;      // default event handler
317     wifi_cleaned_up_handler cleaned_up_handler;     // socket cleaned up handler
318 
319     cb_info *event_cb;                              // event callbacks
320     int num_event_cb;                               // number of event callbacks
321     int alloc_event_cb;                             // number of allocated callback objects
322     pthread_mutex_t cb_lock;                        // mutex for the event_cb access
323 
324     cmd_info *cmd;                                  // Outstanding commands
325     int num_cmd;                                    // number of commands
326     int alloc_cmd;                                  // number of commands allocated
327 
328     interface_info **interfaces;                    // array of interfaces
329     int num_interfaces;                             // number of interfaces
330     int max_num_interfaces;                         // max number of interfaces
331     wifi_subsystem_restart_handler restart_handler; // trigger sub system handler
332 
333 
334     // add other details
335 } hal_info;
336 
337 #define PNO_SSID_FOUND  0x1
338 #define PNO_SSID_LOST    0x2
339 
340 typedef struct wifi_pno_result {
341     unsigned char ssid[DOT11_MAX_SSID_LEN];
342     unsigned char ssid_len;
343     signed char rssi;
344     u16 channel;
345     u16 flags;
346     mac_addr  bssid;
347 } wifi_pno_result_t;
348 
349 typedef struct wifi_gscan_result {
350     u64 ts;                           // Time of discovery
351     u8 ssid[DOT11_MAX_SSID_LEN+1];    // null terminated
352     mac_addr bssid;                   // BSSID
353     u32 channel;                      // channel frequency in MHz
354     s32 rssi;                         // in db
355     u64 rtt;                          // in nanoseconds
356     u64 rtt_sd;                       // standard deviation in rtt
357     u16 beacon_period;                // units are Kusec
358     u16 capability;                   // Capability information
359     u32 pad;
360 } wifi_gscan_result_t;
361 
362 typedef struct wifi_gscan_full_result {
363     wifi_gscan_result_t fixed;
364     u32 scan_ch_bucket;              // scan chbucket bitmask
365     u32 ie_length;                   // byte length of Information Elements
366     u8  ie_data[1];                  // IE data to follow
367 } wifi_gscan_full_result_t;
368 
369 void twt_deinit_handler();
370 
371 typedef enum {
372     TWT_EVENT_INVALID          = 0,
373     TWT_SETUP_RESPONSE         = 1,
374     TWT_TEARDOWN_COMPLETION    = 2,
375     TWT_INFORM_FRAME           = 3,
376     TWT_NOTIFY                 = 4,
377     TWT_EVENT_LAST
378 } TwtEventType;
379 
380 typedef enum {
381     TWT_INVALID			= 0,
382     TWT_SETUP_REQUEST		= 1,
383     TWT_INFO_FRAME_REQUEST	= 2,
384     TWT_TEAR_DOWN_REQUEST	= 3,
385     TWT_LAST
386 } TwtRequestType;
387 
388 typedef enum {
389     TWT_ATTRIBUTE_INVALID		= 0,
390     TWT_ATTRIBUTE_CONFIG_ID		= 1,
391     TWT_ATTRIBUTE_NEG_TYPE		= 2,
392     TWT_ATTRIBUTE_TRIGGER_TYPE		= 3,
393     TWT_ATTRIBUTE_WAKE_DUR_US		= 4,
394     TWT_ATTRIBUTE_WAKE_INT_US		= 5,
395     TWT_ATTRIBUTE_WAKE_INT_MIN_US	= 6,
396     TWT_ATTRIBUTE_WAKE_INT_MAX_US	= 7,
397     TWT_ATTRIBUTE_WAKE_DUR_MIN_US	= 8,
398     TWT_ATTRIBUTE_WAKE_DUR_MAX_US	= 9,
399     TWT_ATTRIBUTE_AVG_PKT_SIZE		= 10,
400     TWT_ATTRIBUTE_AVG_PKT_NUM		= 11,
401     TWT_ATTRIBUTE_WAKE_TIME_OFF_US	= 12,
402     TWT_ATTRIBUTE_ALL_TWT		= 13,
403     TWT_ATTRIBUTE_RESUME_TIME_US	= 14,
404     TWT_ATTRIBUTE_AVG_EOSP_DUR		= 15,
405     TWT_ATTRIBUTE_EOSP_COUNT		= 16,
406     TWT_ATTRIBUTE_NUM_SP		= 17,
407     TWT_ATTRIBUTE_DEVICE_CAP		= 18,
408     TWT_ATTRIBUTE_PEER_CAP		= 19,
409     TWT_ATTRIBUTE_STATUS		= 20,
410     TWT_ATTRIBUTE_REASON_CODE		= 21,
411     TWT_ATTRIBUTE_RESUMED		= 22,
412     TWT_ATTRIBUTE_NOTIFICATION		= 23,
413     TWT_ATTRIBUTE_SUB_EVENT		= 24,
414     TWT_ATTRIBUTE_NUM_PEER_STATS	= 25,
415     TWT_ATTRIBUTE_AVG_PKT_NUM_TX	= 26,
416     TWT_ATTRIBUTE_AVG_PKT_SIZE_TX	= 27,
417     TWT_ATTRIBUTE_AVG_PKT_NUM_RX	= 28,
418     TWT_ATTRIBUTE_AVG_PKT_SIZE_RX	= 29,
419     TWT_ATTRIBUTE_MAX
420 } TWT_ATTRIBUTE;
421 
422 wifi_error wifi_register_handler(wifi_handle handle, int cmd, nl_recvmsg_msg_cb_t func, void *arg);
423 wifi_error wifi_register_vendor_handler(wifi_handle handle,
424             uint32_t id, int subcmd, nl_recvmsg_msg_cb_t func, void *arg);
425 
426 void wifi_unregister_handler(wifi_handle handle, int cmd);
427 void wifi_unregister_vendor_handler_without_lock(wifi_handle handle, uint32_t id, int subcmd);
428 void wifi_unregister_vendor_handler(wifi_handle handle, uint32_t id, int subcmd);
429 
430 wifi_error wifi_register_cmd(wifi_handle handle, int id, WifiCommand *cmd);
431 WifiCommand *wifi_unregister_cmd(wifi_handle handle, int id);
432 WifiCommand *wifi_get_cmd(wifi_handle handle, int id);
433 void wifi_unregister_cmd(wifi_handle handle, WifiCommand *cmd);
434 
435 interface_info *getIfaceInfo(wifi_interface_handle);
436 wifi_handle getWifiHandle(wifi_interface_handle handle);
437 hal_info *getHalInfo(wifi_handle handle);
438 hal_info *getHalInfo(wifi_interface_handle handle);
439 wifi_handle getWifiHandle(hal_info *info);
440 wifi_interface_handle getIfaceHandle(interface_info *info);
441 wifi_error wifi_cancel_cmd(wifi_request_id id, wifi_interface_handle iface);
442 wifi_error wifi_get_cancel_cmd(wifi_request_id id, wifi_interface_handle iface);
443 wifi_error nan_deinit_handler();
444 wifi_error wifi_start_hal(wifi_interface_handle iface);
445 wifi_error wifi_stop_hal(wifi_interface_handle iface);
446 wifi_interface_handle wifi_get_wlan_interface(wifi_handle info,
447 	    wifi_interface_handle *ifaceHandles, int numIfaceHandles);
448 wifi_error wifi_hal_ota_update(wifi_interface_handle iface, uint32_t ota_version);
449 wifi_error wifi_hal_preInit(wifi_interface_handle iface);
450 /* API to get wake reason statistics */
451 wifi_error wifi_get_wake_reason_stats(wifi_interface_handle handle,
452         WLAN_DRIVER_WAKE_REASON_CNT *wifi_wake_reason_cnt);
453 wifi_error wifi_virtual_interface_create(wifi_handle handle, const char* ifname,
454         wifi_interface_type iface_type);
455 wifi_error wifi_virtual_interface_delete(wifi_handle handle, const char* ifname);
456 wifi_error wifi_set_coex_unsafe_channels(wifi_handle handle, u32 num_channels,
457                                          wifi_coex_unsafe_channel channels[], u32 restrictions);
458 wifi_error wifi_set_voip_mode(wifi_interface_handle handle, wifi_voip_mode mode);
459 wifi_error wifi_set_dtim_config(wifi_interface_handle handle, u32 multiplier);
460 void set_hautil_mode(bool halutil_mode);
461 bool get_halutil_mode();
462 
463 /* API's to support TWT */
464 
465 /**@brief twt_get_capability
466  *        Request TWT capability
467  * @param wifi_interface_handle:
468  * @return Synchronous wifi_error and TwtCapabilitySet
469  */
470 wifi_error twt_get_capability(wifi_interface_handle iface, TwtCapabilitySet* twt_cap_set);
471 
472 /**@brief twt_register_handler
473  *        Request to register TWT callback
474  * @param wifi_interface_handle:
475  * @param TwtCallbackHandler:
476  * @return Synchronous wifi_error
477  */
478 wifi_error twt_register_handler(wifi_interface_handle iface, TwtCallbackHandler handler);
479 
480 /**@brief twt_setup_request
481  *        Request to send TWT setup frame
482  * @param wifi_interface_handle:
483  * @param TwtSetupRequest:
484  * @return Synchronous wifi_error
485  * @return Asynchronous EventTwtSetupResponse CB return TwtSetupResponse
486  */
487 wifi_error twt_setup_request(wifi_interface_handle iface, TwtSetupRequest* msg);
488 
489 /**@brief twt_teardown_request
490  *        Request to send TWT teardown frame
491  * @param wifi_interface_handle:
492  * @param TwtTeardownRequest:
493  * @return Synchronous wifi_error
494  * @return Asynchronous EventTwtTeardownCompletion CB return TwtTeardownCompletion
495  * TwtTeardownCompletion may also be received due to other events
496  * like CSA, BTCX, TWT scheduler, MultiConnection, peer-initiated teardown, etc.
497  */
498 wifi_error twt_teardown_request(wifi_interface_handle iface, TwtTeardownRequest* msg);
499 
500 /**@brief twt_info_frame_request
501  *        Request to send TWT info frame
502  * @param wifi_interface_handle:
503  * @param TwtInfoFrameRequest:
504  * @return Synchronous wifi_error
505  * @return Asynchronous EventTwtInfoFrameReceived CB return TwtInfoFrameReceived
506  * Driver may also receive Peer-initiated TwtInfoFrame
507  */
508 wifi_error twt_info_frame_request(wifi_interface_handle iface, TwtInfoFrameRequest* msg);
509 
510 /**@brief twt_get_stats
511  *        Request to get TWT stats
512  * @param wifi_interface_handle:
513  * @param config_id:
514  * @return Synchronous wifi_error and TwtStats
515  */
516 wifi_error twt_get_stats(wifi_interface_handle iface, u8 config_id, TwtStats* stats);
517 
518 /**@brief twt_clear_stats
519  *        Request to clear TWT stats
520  * @param wifi_interface_handle:
521  * @param config_id:
522  * @return Synchronous wifi_error
523  */
524 wifi_error twt_clear_stats(wifi_interface_handle iface, u8 config_id);
525 
526 wifi_error wifi_trigger_subsystem_restart(wifi_handle handle);
527 // some common macros
528 
529 #define min(x, y)       ((x) < (y) ? (x) : (y))
530 #define max(x, y)       ((x) > (y) ? (x) : (y))
531 
532 #define NULL_CHECK_RETURN(ptr, str, ret) \
533     do { \
534         if (!(ptr)) { \
535             ALOGE("%s(): null pointer - #ptr (%s)\n", __FUNCTION__, str); \
536             return ret; \
537         } \
538     } while (0)
539 
540 #endif
541 
542