1 /*
2 * Copyright (c) 2021 Bestechnic (Shanghai) Co., Ltd. All rights reserved.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15 #ifndef BWIFI_INTERFACE_H
16 #define BWIFI_INTERFACE_H
17
18 #include "plat_types.h"
19 #include "cmsis_os.h"
20 #include "wifi_def.h"
21 #include "bwifi_hal.h"
22 #include "bwifi_event.h"
23 #if LWIP_ETHERNETIF
24 #include "lwip/netif.h"
25 #endif
26
27 #ifdef __cplusplus
28 extern "C" {
29 #endif
30
31 #define BWIFI_MAX_COUNTRY_CODE_SIZE 2
32 #define BWIFI_CONFIG_VAL_LEN 100
33 #define WIFI_SCAN_DUMP
34
35 typedef enum {
36 WIFI_IF_STATION,
37 WIFI_IF_SOFTAP,
38 } BWIFI_INTF_TYPE_T;
39
40 typedef enum {
41 BWIFI_STATUS_IDLE = 0,
42 BWIFI_STATUS_DISCONNECTING = 1,
43 BWIFI_STATUS_SCANNING = 2,
44 BWIFI_STATUS_CONNECTING = 3,
45 BWIFI_STATUS_WPS_CONNECTING = 4,
46 BWIFI_STATUS_CONNECTED = 5,
47 BWIFI_STATUS_DHCPING = 6,
48 BWIFI_STATUS_GOT_IP = 7,
49 BWIFI_STATUS_ONLINE_SCANNING = 8,
50 BWIFI_STATUS_END_DEF, /* must be the last one */
51 } BWIFI_STATUS_T;
52
53 enum {
54 BWIFI_R_OK = 0,
55 BWIFI_R_COMMON_FAIL = -1,
56 BWIFI_R_INVALID_ARG = -2, //invalid argument
57 BWIFI_R_INVALID_PASSWORD = -3, //invalid password
58 BWIFI_R_MEMORY_ERROR = -4, //no memory to allocate resource
59 BWIFI_R_INIT_FAIL = -5, //init wifi fail
60 BWIFI_R_NOT_INITED = -6, //wifi is not initialized
61 BWIFI_R_STATUS_ERROR = -7, //request in error STATUS
62 BWIFI_R_SCAN_REQ_FAIL = -8, //scan fail to start
63 BWIFI_R_SCAN_NO_AP_FOUND = -9, //scan result is NULL (didn't find any SSID)
64 BWIFI_R_NO_SUITABLE_NETWORK = -10, //no suitable network to connect
65 BWIFI_R_CONN_REQ_FAIL = -11, //connect fail to start
66 BWIFI_R_CONN_FAIL = -12, //connect procedure result in fail
67 BWIFI_R_CONN_NO_SSID_CONFIG = -13, //no saved SSID config to connect
68 BWIFI_R_DISC_FAIL = -14, //disconnect procedure result in fail
69 BWIFI_R_WPS_NOT_FOUND = -15, //couldn't find WPS AP
70 BWIFI_R_WPS_REQ_FAIL = -16, //WPS fail to start
71 };
72
73 enum {
74 WIFI_MFP_DISABLE = 0, /**< disable mfp capability */
75 WIFI_MFP_ENABLE, /**< have mfp capability, but not require peer to support mfp */
76 WIFI_MFP_REQUIRED, /**< have mfp capability, and require peer to support mfp */
77 WIFI_MFP_DEFAULT, /**< mfp is not specified, setting it based on our capability and network type */
78 };
79
80 typedef enum bwifi_softap_status {
81 BWIFI_SOFTAP_STATUS_OFF = 0,
82 BWIFI_SOFTAP_STATUS_ON,
83 } BWIFI_SOFTAP_STATUS_T;
84
85 typedef enum {
86 WIFI_USER_EVT_CONN_INTER_STATE,
87 WIFI_USER_EVT_CONNECTED,
88 WIFI_USER_EVT_GOT_IP,
89 WIFI_USER_EVT_DISCONNECTED,
90 WIFI_USER_EVT_ROAM_COMPLETE,
91 WIFI_USER_EVT_AP_ENABLED,
92 WIFI_USER_EVT_AP_DISABLED,
93 WIFI_USER_EVT_AP_STA_CONNECTED,
94 WIFI_USER_EVT_AP_STA_DISCONNECTED,
95 WIFI_USER_EVT_FATAL_ERROR,
96 /* mutually exclusive events */
97 /* If user layer wants to take over the initiative of wifi reset,
98 * register this event for the timing to trigger reset.
99 */
100 WIFI_USER_EVT_MAC_RESET_REQUEST,
101 /* If user layer is only interested in the process of wifi reset,
102 * register these events to get notify when reset start/end.
103 */
104 WIFI_USER_EVT_MAC_RESET_START,
105 WIFI_USER_EVT_MAC_RESET_DONE,
106 WIFI_USER_EVT_COEX_MODE_CHANGE,
107 WIFI_USER_EVT_MAX
108 } WIFI_USER_EVT_ID;
109
110 typedef enum {
111 FATAL_ERROR = 1,
112 UMAC_CRASH = 2,
113 } BWIFI_MAC_RESET_REASON;
114
115 struct ip_info {
116 uint32_t ip; /**< IP address */
117 uint32_t netmask; /**< netmask */
118 uint32_t gw; /**< gateway */
119 };
120
121 struct bwifi_quick_connect_config {
122 struct bwifi_station_config config;
123 uint32_t channel;
124 uint32_t ip[3];//struct ip_info ip;
125 };
126
127 typedef void (*user_evt_handler_t)(WIFI_USER_EVT_ID evt_id, void *arg);
128
129 void bwifi_reg_user_evt_handler(WIFI_USER_EVT_ID evt_id, user_evt_handler_t cb);
130 void bwifi_unreg_user_evt_handler(WIFI_USER_EVT_ID evt_id);
131 void bwifi_reg_eth_input_handler(eth_input_handler cb);
132 void bwifi_unreg_eth_input_handler();
133
134 /*
135 * wifi record interface
136 */
137 int bwifi_find_record(const char *type, void *record_info);
138 int bwifi_add_record(void *record_info);
139 int bwifi_del_record(char *type);
140 /*
141 * Add network config to wpa_supplicant
142 * return the allocated network id, or negative on failure.
143 */
144 int bwifi_add_config(struct bwifi_station_config *config);
145 /*
146 * Modify network config item
147 * return the result of current operation
148 */
149 int bwifi_modify_config(struct bwifi_config_item *item);
150 /*
151 * Get the number of saved network configs from wpa_supplicant.
152 */
153 int bwifi_count_configured_ssid(void);
154 /*
155 * Get network configs saved in wpa_supplicant
156 * return the number of networks
157 */
158 int bwifi_get_current_config(struct bwifi_station_config *config, int count);
159 /*
160 * Save network config to flash (max 8)
161 */
162 int bwifi_save_config(struct bwifi_station_config *config);
163 /*
164 * Get network configs saved in flash
165 * return the number of networks
166 */
167 int bwifi_get_saved_config(struct bwifi_station_config *config, int count);
168 /*
169 * Find network config saved in flash who matches with the specified ssid
170 */
171 int bwifi_find_saved_config_by_ssid(const char *ssid, struct bwifi_station_config *config);
172 /*
173 * Delete all network configs saved in wpa and flash
174 */
175 int bwifi_del_all_config(void);
176 /*
177 * Delete network config saved in wpa and flash
178 * It will check ssid, passwd, hidden, web_keyid, bssid (if not zero),
179 * if config is NULL or config->ssid is zero length, delete all.
180 */
181 int bwifi_del_config(struct bwifi_station_config *config);
182 /*
183 * Scan for wildcard ssid and saved hidden ssids
184 * you can get bss list from bwifi_get_scan_result
185 * return bss number or error code
186 */
187 int bwifi_scan(void);
188 /*
189 * Scan for specified ssid (if not NULL) on the specified channels (if no 0)
190 * you can get bss list from bwifi_get_scan_result
191 * return bss number or error code
192 */
193 int bwifi_config_scan(struct bwifi_scan_config *scan_config);
194 /*
195 * Get scan result
196 */
197 int bwifi_get_scan_result(struct bwifi_bss_info *result, int count);
198 /*
199 * Clear saved scan list which is not in use
200 */
201 void bwifi_flush_scan_result(void);
202 /*
203 * Auto connect to an AP saved in wpa_supplicant
204 */
205 int bwifi_auto_connect(void);
206 /*
207 * Connect to an AP assigned by user config
208 *
209 * This function will add the network config to wpa_supplicant and trigger new connection.
210 * If user wants to connect to another AP after connected:
211 * 1. bwifi_disconnect()
212 * 2. bwifi_connect(config)
213 */
214 int bwifi_connect(struct bwifi_station_config *config);
215 /*
216 * Connect to an AP via WPS PBC
217 */
218 int bwifi_connect_wps_pbc(void);
219 /*
220 * Connect to specified ssid, passwd can be NULL for unencrypted AP.
221 */
222 int bwifi_connect_to_ssid(const char *ssid, const char *passwd, int8_t wep_keyid, uint8_t hidden, uint8_t *bssid);
223 /*
224 * Connect to specified ssid and mfp, passwd can't be NULL
225 */
226 int bwifi_connect_to_ssid_with_mfp(const char *ssid, const char *passwd, int8_t wep_keyid, uint8_t hidden, uint8_t *bssid, uint8_t mfp);
227 /*
228 * Connect to specified bssid, passwd can be NULL for unencrypted AP.
229 */
230 int bwifi_connect_to_bssid(uint8_t *bssid, const char *passwd);
231 /*
232 * Connect to specified network by network id allocated in wpa_supplicant.
233 */
234 int bwifi_connect_to_nid(int nid);
235 /*
236 * Disconnect to current connected AP or stop connecting to AP
237 */
238 int bwifi_disconnect(void);
239
240 /*
241 * Quick connect functions
242 */
243 int bwifi_get_quick_connect_config(struct bwifi_quick_connect_config *quick_config);
244 int bwifi_set_quick_connect_config(struct bwifi_quick_connect_config *quick_config);
245 int bwifi_del_quick_connect_config(void);
246 int bwifi_quick_connect(struct bwifi_quick_connect_config *quick_config);
247
248 /*
249 * Set whether auto reconnect after disconnection
250 * default val true
251 */
252 void bwifi_set_reconnect_policy(bool enable);
253 /*
254 * Get current reconnect policy
255 */
256 bool bwifi_get_reconnect_policy(void);
257 /*
258 * Get current connected AP info
259 */
260 int bwifi_get_current_ssid(char *ssid);
261 int bwifi_get_current_bssid(uint8_t *bssid);
262 int bwifi_get_own_mac(uint8_t *addr);
263 uint8_t bwifi_get_current_channel(void);
264 int8_t bwifi_get_current_rssi(void);
265 int bwifi_get_current_rate(void);
266
267 /**
268 * Get station linkinfo statistics which can be used to evaluate network traffic.
269 * @info: pointer to a bwifi_station_linkinfo struct to linkinfo statistics
270 * Returns: void
271 */
272 void bwifi_get_station_linkinfo(bwifi_station_linkinfo *info);
273
274 #if LWIP_SUPPORT
275 /*
276 * Enable or disable using the static IP for subsequent connection.
277 *
278 * The DHCP clent is enabled by default and collides with the static IP.
279 * If this API is callbed with a valid pointer to the ip_info struct,
280 * DHCP client will be disabled and the static IP in ip_info will be used;
281 * if this API is called with NULL, then DHCP client will be enabled.
282 * It depends on the latest configuration.
283 */
284 int bwifi_set_static_ip(struct ip_info *ip);
285 /*
286 * Get current ip addr of wifi station (dhcpc or static ip)
287 */
288 int bwifi_get_current_ip(struct ip_info *ip);
289
290 #else
291
292 #if LWIP_ETHERNETIF
293 /*
294 * Get netif struct of wifi station or softap
295 *
296 * This function should only be called when SDK inside LWIP is turned off
297 * but ethernet interface is added and initialized by us.
298 * return the netif struct to user's LWIP stack for further management.
299 */
300 struct netif *bwifi_get_netif(BWIFI_INTF_TYPE_T type);
301 #endif
302
303 /*
304 * Set netif ip addr to wifi mac layer for ARP filter feature
305 *
306 * This function should only be called when SDK inside LWIP is turned off
307 * and DHCP procedure is also taken over by user's LWIP stack,
308 * we need user to tell us the assigned local ip addr.
309 */
310 int bwifi_set_ip_addr(BWIFI_INTF_TYPE_T type, struct ip_info *ip);
311 #endif
312
313 /**
314 * Change current wifi status
315 *
316 * @new_status: The new wifi status
317 * Returns: void
318 */
319 void bwifi_change_current_status(BWIFI_STATUS_T new_status);
320
321 void airkiss_notify(uint8_t token);
322
323 /**
324 * Enable or disable the statistics of the frames sent out and received
325 * in the last interval_sec time.
326 * @en: 0 = disable, 1 = enable
327 * @interval_sec: time of the statistics in seconds
328 * Returns: void
329 *
330 * @note If enabled, the statistics information will be output via uart log.
331 */
332 void bwifi_trans_stat_en(uint8_t en, uint8_t interval_sec);
333 /**
334 * Get trans statistics which can be used to evaluate network traffic.
335 * @stat: pointer to a bwifi_trans_stat struct to retrieve trans statistics
336 * @clear: 1 = clear, 0 = keep
337 * Returns: void
338 *
339 * @note Elements in bwifi_trans_stat will be cleared every time this function
340 * is called if clear is set to 1.
341 */
342 void bwifi_trans_stat_get(bwifi_trans_stat *stat, int8_t clear);
343
344 /*
345 * Initialize wifi hardware and interface
346 */
347 int bwifi_init(void);
348
349 #ifdef WIFI_ON_CP
350 /*
351 * reset wifi stack on cp
352 */
353 int bwifi_reinit(void);
354 #endif
355
356 /*
357 * Set country code
358 * If not set, we use the default country code CN.
359 */
360 int bwifi_set_country_code(char *country);
361 /*
362 * Get current country code
363 */
364 int bwifi_get_country_code(char *country);
365
366 void bwifi_set_connecting_status(void);
367 /*
368 * Set powersave mode for legacy Wi-Fi.
369 * @ps: 0 = disable, 1 = enable
370 * Returns: 0 on success or negtive on failure
371 */
372 int bwifi_set_ps_mode(int ps);
373
374 /*
375 * swtich channel dynamically
376 * @mode: 0 softap mode, 1 sta mode
377 * @channel: channel number
378 * @snd_offset: secondary channel offset to center freq
379 * 0 - no snd ch, 1 - upper 10M, -1 - lower 10M
380 * Returns: 0 on success or negtive on failure
381 */
382 int bwifi_switch_channel(uint8_t mode, uint8_t channel, int8_t snd_offset);
383
384 #ifdef __AP_MODE__
385 /*
386 * Start softap with previous configuration
387 */
388 int bwifi_softap_start(void);
389 /*
390 * Stop softap
391 */
392 void bwifi_softap_stop(void);
393
394 /**
395 * Set softap basic configuration
396 *
397 * @ssid: AP's SSID
398 * @channel: Primary channel for AP to start on
399 * @sec_channel_offset: Secondary channel offset for HT40
400 * 0 = HT40 disabled;
401 * 1 = HT40 enabled, secondary channel above primary;
402 * -1 = HT40 enabled, secondary channel below primary
403 * @hidden: Whether to ignore broadcast SSID
404 * 0 = broadcast ssid;
405 * 1 = hide ssid with zero length;
406 * 2 = hide ssid with zero contents
407 * @security: Enum of security type
408 * @password: User passphrase
409 *
410 * Returns: 0 on success, nagtive if invalid arguments
411 */
412 int bwifi_set_softap_config(char *ssid,
413 uint8_t channel, int sec_channel_offset,
414 uint8_t hidden,
415 BWIFI_SEC_TYPE_T security,
416 char *passwd);
417 /*
418 * Set softap's maximum client number
419 * @sta_num: Maximum number of STAs
420 * Returns: void
421 */
422 void bwifi_set_softap_max_sta_num(int sta_num);
423 /*
424 * Set softap's country code
425 * @country_code: Two octets string to represent a country code
426 * Returns: void
427 */
428 void bwifi_set_softap_country_code(char *country_code);
429 /**
430 * Add vendor elements in softap's Beacon/ProbeRsp
431 * @ie: Pointer to raw data of vendor elements
432 * @ie_len: Length of vendor elements
433 * Returns: 0 on success, nagtive on failure
434 */
435 int bwifi_add_softap_vendor_elements(const uint8_t *ie, size_t ie_len);
436 /*
437 * Get softap's client list
438 * @mac_list: Pointer to buffer to store the mac address list
439 * @count: STA count to retrieve, returns the actual connected STA count
440 * Returns: 0 on success, nagtive on failure
441 */
442 int bwifi_softap_get_station_list(struct bwifi_mac_addr *mac_list, int *count);
443
444 #endif
445
446 /*
447 * Get current Wi-Fi status
448 */
449 BWIFI_STATUS_T bwifi_get_current_status(void);
450 /*
451 * Get current softap status
452 */
453 BWIFI_SOFTAP_STATUS_T bwifi_get_softap_status(void);
454
455 /*
456 * This function is used for userspace to send an encapsulated mgmt frame.
457 * @data: Pointer to the frame data
458 * @len: Length of the frame
459 * @noack: Whether need to wait for ack
460 * @chan: Which channel to send
461 */
462 int bwifi_send_mgmt_frame_on_chan(const uint8_t *data, size_t len, int noack, uint8_t chan);
463
464 /*
465 * Enable or disable Wi-Fi recovery mechanism on fatal error.
466 * which is turned off by default for debug purpose.
467 */
468 void bwifi_recovery_enable(bool en);
469
470 /**
471 * Set fix data rate for Wi-Fi transmission
472 * @fix_rate_idx
473 * - 0 ~ 3: DSSS/CCK rates: 1, 2, 5.5, 11;
474 * - 4 ~ 5: reserved;
475 * - 6 ~ 13: legacy OFDM rates: 6, 9, 12, 18, 24, 36, 48, 54;
476 * - 14 ~ 21: 1*1 MCS rates: MCS0 ~ MCS7;
477 * - 0xff: default value to disable the fix rate function.
478 */
479 void bwifi_set_fix_rate(uint32_t fix_rate_idx);
480 /*
481 * Get current set fix rate index
482 */
483 uint32_t bwifi_get_fix_rate(void);
484
485 /*
486 * Do wifi reset
487 * Returns: BWIFI_R_OK on success, others on failure
488 */
489 int bwifi_reset(void);
490
491 /*
492 * set epta parameters of wifi/bt coex
493 * wifi_dur: wifi duration of active window
494 * bt_dur: bt duration of active window
495 * mode: epta mode
496 0 - periodic mode in which the wifi/bt active time is specified by wifi_dur/bt_dur, only support 100ms period
497 1 - arbitrate mode by hw itself
498 2 - wifi active only mode
499 */
500 void bwifi_set_epta_param(int wifi_dur, int bt_dur, int mode);
501
502 /*
503 0 - bt not active, allow to alloc max duration for wlan
504 1 - bt active, allow to alloc medium duration for wlan
505 2 - bt a2dp active
506 */
507 void bwifi_bt_state_notify(int state);
508
509 /*
510 * get free buf to send
511 * Returns: address of buf pointer
512 */
bwifi_get_data_buf(void)513 static inline uint8_t ** bwifi_get_data_buf(void)
514 {
515 return (uint8_t **)BWIFI_HAL_API_CALL(get_send_buf);
516 }
517
518 /*
519 * send data buf
520 * @devnum: dev interface number
521 * @tx_buf: address of send buffer pointer
522 * @tx_len: send data length
523 * Returns: BWIFI_R_OK on success, others on failure
524 */
bwifi_send_data_buf(uint8_t devnum,uint8_t ** tx_buf,uint16_t tx_len)525 static inline int bwifi_send_data_buf(uint8_t devnum, uint8_t **tx_buf, uint16_t tx_len)
526 {
527 return BWIFI_HAL_API_CALL(data_send, devnum, tx_buf, tx_len);
528 }
529
530 /*
531 * bwifi_str_cmd - send string command
532 * @type: command type, 0 - rf test, other - reserved
533 * @cmd_buf: command string ended with '\0'
534 * @rsp_buf: response string ended with '\0'
535 * Returns: 0 - success, other - failure
536 */
537 int bwifi_str_cmd(uint8_t type, uint8_t *cmd_buf, uint8_t *rsp_buf);
538
539 #ifdef __cplusplus
540 }
541 #endif
542
543 #endif /* BWIFI_INTERFACE_H */
544