1 /* 2 * Copyright (c) 2020 HiSilicon (Shanghai) Technologies CO., LIMITED. 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 * Description: wifi APIs 15 */ 16 17 #ifndef _WIFI_API_H_ 18 #define _WIFI_API_H_ 19 20 #include "td_type.h" 21 #include "td_base.h" 22 #include "soc_wifi_api.h" 23 #include "service_wifi_api.h" 24 #include "config_ssid.h" 25 #include "soc_osal.h" 26 #ifdef LOS_CONFIG_MESH 27 #include "soc_wifi_mesh_api.h" 28 #endif /* LOS_CONFIG_MESH */ 29 #ifdef LOS_CONFIG_P2P 30 #include "soc_wifi_p2p_api.h" 31 #endif 32 #include <stdbool.h> 33 34 #define WPA_MIN_KEY_LEN 8 35 #define WPA_MAX_KEY_LEN 64 36 #define WAPI_MAX_KEY_LEN 65 37 #define WPA_HT_CAPA_LEN 20 38 #define WPA_MAX_SSID_LEN 32 39 #define WPA_MAX_ESSID_LEN WPA_MAX_SSID_LEN 40 #define WPA_MIN_FREQ_LEN 6 41 #define WPA_MAX_FREQ_LEN 10 42 #define WPA_AP_MIN_BEACON 25 43 #define WPA_AP_MAX_BEACON 1000 44 #define WPA_24G_FREQ_TXT_LEN 4 45 #define WPA_WEP40_KEY_LEN 5 46 #define WPA_WEP104_KEY_LEN 13 47 #define WPA_WEP40_HEX_KEY_LEN 10 48 #define WPA_WEP104_HEX_KEY_LEN 26 49 #define WPA_AP_MAX_DTIM 30 50 #define WPA_AP_MIN_DTIM 1 51 #define WPA_MAX_REKEY_TIME 86400 52 #define WPA_MIN_REKEY_TIME 30 53 #define WPA_DOUBLE_IFACE_WIFI_DEV_NUM 2 54 #define WPA_BASE_WIFI_DEV_NUM 1 55 #define WPA_MAX_WIFI_DEV_NUM 2 56 #define WPA_NETWORK_ID_TXT_LEN 5 57 #define WPA_CTRL_CMD_LEN 256 58 #define WPA_CMD_BUF_SIZE 64 59 #define WPA_MIN(x, y) (((x) < (y)) ? (x) : (y)) 60 #define WPA_STR_LEN(x) (strlen(x) + 1) 61 #define WPA_EXTERNED_SSID_LEN (WPA_MAX_SSID_LEN * 5) 62 #define WPA_CMD_BSSID_LEN 32 63 #define WPA_CMD_MIN_SIZE 16 64 #define WPA_MAX_NUM_STA 8 65 #define WPA_SSID_SCAN_PREFEX_ENABLE 1 66 #define WPA_SSID_SCAN_PREFEX_DISABEL 0 67 #define WPA_P2P_INTENT_LEN 13 68 #define WPA_P2P_JOIN_LEN 5 69 #define WPA_P2P_WPS_LEN 3 70 #define WPA_INT_TO_CHAR_LEN 11 71 #define WPA_P2P_LISTEN_LEN 20 72 #define WPA_P2P_PEER_CMD_LEN 27 73 #define WPA_P2P_MIN_INTENT 0 74 #define WPA_P2P_MAX_INTENT 15 75 #define WIFI_EVENT_DELAY_5S 5000 76 #define WPA_BIT0 (1U << 0) 77 #define WPA_BIT1 (1U << 1) 78 #define WPA_BIT2 (1U << 2) 79 #define WPA_BIT3 (1U << 3) 80 #define WPA_BIT4 (1U << 4) 81 #define WPA_BIT5 (1U << 5) 82 #define WPA_BIT6 (1U << 6) 83 #define WPA_BIT7 (1U << 7) 84 #define WPA_BIT8 (1U << 8) 85 #define WPA_BIT9 (1U << 9) 86 #define WPA_BIT10 (1U << 10) 87 #define WPA_BIT11 (1U << 11) 88 #define WPA_BIT12 (1U << 12) 89 #define WPA_BIT13 (1U << 13) 90 #define WPA_BIT14 (1U << 14) 91 #define WPA_BIT15 (1U << 15) 92 #define WPA_BIT16 (1U << 16) 93 #define WPA_BIT17 (1U << 17) 94 #define WPA_BIT18 (1U << 18) 95 #define WPA_BIT19 (1U << 19) 96 #define WPA_BIT20 (1U << 20) 97 #define WPA_BIT21 (1U << 21) 98 #define WPA_BIT22 (1U << 22) 99 #define WPA_BIT23 (1U << 23) 100 #define WPA_BIT24 (1U << 24) 101 #define WPA_BIT26 (1U << 26) 102 #define WPA_BIT27 (1U << 27) 103 #define WPA_BIT28 (1U << 28) 104 #define WPA_BIT29 (1U << 29) 105 #define WPA_BIT30 (1U << 30) 106 107 #define WPA_P2P_SCAN_MAX_CMD 32 108 #define WPA_P2P_IFNAME_MAX_LEN 10 109 #define WPA_P2P_DEFAULT_PERSISTENT 0 110 #define WPA_P2P_GROUP_MAX_LEN 24 111 112 #define MESH_AP 1 113 #define MESH_STA 0 114 115 #ifndef MAX_SSID_LEN 116 #define MAX_SSID_LEN 32 117 #endif 118 #define MAX_DRIVER_NAME_LEN 16 119 #define WPA_MAX_SSID_KEY_INPUT_LEN 128 120 #define WPA_TXT_ADDR_LEN 17 121 #define WPA_INVITE_ADDR_LEN 23 122 #define WPA_INVITE_PERSISTENT_ID 13 123 #define WPA_STA_PMK_LEN 32 124 #define WPA_STA_ITERA 4096 125 #define WPA_MAX_TRY_FREQ_SCAN_CNT 3 126 #define WPA_24G_CHANNEL_NUMS 14 127 128 #define WPA_ALIGN_SIZE 4 129 130 #define WPA_COUNTRY_CODE_LEN 3 131 #define WPA_COUNTRY_CODE_USA "US" 132 #define WPA_COUNTRY_CODE_ZZ "ZZ" // ZZ : support channel 14 133 #define WPA_COUNTRY_CODE_JAPAN "JP" 134 135 #define WPA_CHANNEL_MAX_USA 11 136 #define WPA_CHANNEL_MAX_JAPAN 14 137 #define WPA_CHANNEL_MAX_OTHERS 13 138 #define SAE_GROUPS_MAX_NUM 4 139 140 // LiteOS : BIT25 and BIT0 should not be used. 141 // FreeRTOS: BIT31 - BIT24 should not be used. 142 #define WPA_EVENT_AP_DEAUTH_FAIL WPA_BIT1 143 #define WPA_EVENT_AP_DEAUTH_OK WPA_BIT2 144 #define WPA_EVENT_AP_DEAUTH_FLAG (WPA_BIT1 | WPA_BIT2) 145 #define WPA_EVENT_AP_SHOW_STA_OK WPA_BIT3 146 #define WPA_EVENT_AP_SHOW_STA_ERROR WPA_BIT4 147 #define WPA_EVENT_AP_SHOW_STA_FLAG (WPA_BIT3 | WPA_BIT4) 148 #define WPA_EVENT_AP_STOP_OK WPA_BIT5 149 150 #define WPA_EVENT_GET_SCAN_RESULT_OK WPA_BIT1 151 #define WPA_EVENT_GET_SCAN_RESULT_ERROR WPA_BIT2 152 #define WPA_EVENT_GET_SCAN_RESULT_FLAG (WPA_BIT1 | WPA_BIT2) 153 #define WPA_EVENT_SCAN_RESULT_FREE_OK WPA_BIT3 154 #define WPA_EVENT_STA_STOP_OK WPA_BIT4 155 #define WPA_EVENT_STA_STOP_ERROR WPA_BIT5 156 #define WPA_EVENT_STA_STOP_FLAG (WPA_BIT4 | WPA_BIT5) 157 #define WPA_EVENT_STA_RM_NETWORK_OK WPA_BIT6 158 #define WPA_EVENT_STA_STATUS_OK WPA_BIT7 159 #define WPA_EVENT_STA_STATUS_FAIL WPA_BIT8 160 #define WPA_EVENT_STA_STATUS_FLAG (WPA_BIT7 | WPA_BIT8) 161 #define WPA_EVENT_QUICK_CONNECT_OK WPA_BIT9 162 #define WPA_EVENT_QUICK_CONNECT_ERROR WPA_BIT10 163 #define WPA_EVENT_QUICK_CONNECT_FLAG (WPA_BIT9 | WPA_BIT10) 164 #define WPA_EVENT_SCAN_OK WPA_BIT11 165 #define WPA_EVENT_WPA_START_OK WPA_BIT12 166 #define WPA_EVENT_WPA_START_ERROR WPA_BIT13 167 #define WPA_EVENT_WPA_START_FLAG (WPA_BIT12 | WPA_BIT13) 168 #define WPA_EVENT_ADD_IFACE_OK WPA_BIT14 169 #define WPA_EVENT_ADD_IFACE_ERROR WPA_BIT15 170 #define WPA_EVENT_ADD_IFACE_FLAG (WPA_BIT14 | WPA_BIT15) 171 #define WPA_EVENT_REMOVE_IFACE_FLAG WPA_BIT16 172 #define WPA_EVENT_SCAN_RESULT_CLEAR_OK WPA_BIT17 173 #define WPA_EVENT_STA_WNM_BSS_QUERY WPA_BIT18 174 #define WPA_EVENT_STA_WNM_NOTIFY WPA_BIT19 175 176 #define WPA_EVENT_P2P_GET_STATUS_OK WPA_BIT1 177 #define WPA_EVENT_P2P_GET_STATUS_ERROR WPA_BIT2 178 #define WPA_EVENT_P2P_GET_STATUS_FLAG (WPA_BIT1 | WPA_BIT2) 179 #define WPA_EVENT_P2P_GET_PEER_OK WPA_BIT3 180 #define WPA_EVENT_P2P_GET_PEER_ERROR WPA_BIT4 181 #define WPA_EVENT_P2P_GET_PEER_FLAG (WPA_BIT3 | WPA_BIT4) 182 #define WPA_EVENT_P2P_GET_PERSISTENT_GROUP_OK WPA_BIT5 183 #define WPA_EVENT_P2P_GET_PERSISTENT_GROUP_ERROR WPA_BIT6 184 #define WPA_EVENT_P2P_GET_PERSISTENT_GROUP_FLAG (WPA_BIT5 | WPA_BIT6) 185 186 enum ext_scan_record_flag { 187 EXT_SCAN_UNSPECIFIED, 188 EXT_SCAN, 189 EXT_CHANNEL_SCAN, 190 EXT_SSID_SCAN, 191 EXT_PREFIX_SSID_SCAN, 192 EXT_BSSID_SCAN, 193 }; 194 195 typedef enum { 196 SOC_WPA_RM_NETWORK_END = 0, 197 SOC_WPA_RM_NETWORK_START, 198 SOC_WPA_RM_NETWORK_WORKING 199 } wpa_rm_network; 200 201 typedef enum { 202 SOC_WPA_BIT_SCAN_UNKNOW = 0, 203 SOC_WPA_BIT_SCAN_SSID = WPA_BIT0, 204 SOC_WPA_BIT_SCAN_BSSID = WPA_BIT1, 205 } wpa_scan_type; 206 207 struct wpa_assoc_request { 208 /* ssid input before ssid txt parsing */ 209 char ssid[EXT_WIFI_MAX_SSID_LEN + 1]; 210 char key[WPA_MAX_SSID_KEY_INPUT_LEN + 1]; 211 unsigned char bssid[EXT_WIFI_MAC_LEN]; 212 ext_wifi_auth_mode auth; 213 u8 channel; 214 ext_wifi_pairwise wpa_pairwise; 215 unsigned char scan_type; 216 unsigned char hex_flag; 217 ext_wifi_wpa_psk_usage_type psk_flag; 218 unsigned char ft_flag; 219 unsigned char resv[3]; 220 }; 221 222 struct ext_scan_record { 223 char ssid[EXT_WIFI_MAX_SSID_LEN + 1]; 224 unsigned char bssid[EXT_WIFI_MAC_LEN]; 225 unsigned char chan_list[WIFI_24G_CHANNEL_NUMS]; 226 unsigned char chan_num; 227 enum ext_scan_record_flag flag; 228 }; 229 230 struct wpa_scan_params { 231 char ssid[EXT_WIFI_MAX_SSID_LEN + 1]; 232 int ssid_len; 233 unsigned char chan_list[WIFI_24G_CHANNEL_NUMS]; 234 unsigned char chan_num; 235 enum ext_scan_record_flag flag; 236 unsigned char bssid[EXT_WIFI_MAC_LEN]; 237 }; 238 typedef enum ext_iftype { 239 EXT_IFTYPE_UNSPECIFIED, 240 EXT_IFTYPE_ADHOC, 241 EXT_IFTYPE_STATION, 242 EXT_IFTYPE_AP, 243 EXT_IFTYPE_AP_VLAN, 244 EXT_IFTYPE_WDS, 245 EXT_IFTYPE_MONITOR, 246 EXT_IFTYPE_MESH_POINT, 247 EXT_IFTYPE_P2P_CLIENT, 248 EXT_IFTYPE_P2P_GO, 249 EXT_IFTYPE_P2P_DEVICE, 250 251 /* keep last */ 252 NUM_EXT_IFTYPES, 253 } wifi_iftype; 254 255 enum ext_mesh_enable_flag_type { 256 EXT_MESH_ENABLE_ACCEPT_PEER, 257 EXT_MESH_ENABLE_ACCEPT_STA, 258 EXT_MESH_ENABLE_FLAG_BUTT 259 }; 260 261 struct wifi_ap_opt_set { 262 protocol_mode_enum hw_mode; 263 int short_gi_off; 264 int beacon_period; 265 int dtim_period; 266 int wpa_group_rekey; 267 enum mfp_options ieee80211w; 268 #ifdef CONFIG_HOSTAPD_WPA3 269 int sae_pwe; 270 int transition; 271 int clog_threshold; 272 int sae_groups[SAE_GROUPS_MAX_NUM]; 273 #endif /* CONFIG_HOSTAPD_WPA3 */ 274 }; 275 276 struct wifi_mesh_opt_set { 277 int beacon_period; 278 int dtim_period; 279 int wpa_group_rekey; 280 }; 281 282 struct wifi_sta_opt_set { 283 protocol_mode_enum hw_mode; 284 wifi_pmf_option_enum pmf; 285 int usr_pmf_set_flag; 286 #ifdef CONFIG_WPA3 287 int sae_pwe; 288 int sae_groups[SAE_GROUPS_MAX_NUM]; 289 #endif 290 }; 291 292 struct wifi_reconnect_set { 293 int enable; 294 unsigned int timeout; 295 unsigned int period; 296 unsigned int max_try_count; 297 unsigned int try_count; 298 unsigned int try_freq_scan_count; 299 int pending_flag; 300 struct wpa_ssid *current_ssid; 301 }; 302 303 struct ext_wifi_dev { 304 ext_wifi_iftype iftype; 305 void *priv; 306 int network_id; 307 int ifname_len; 308 char ifname[WIFI_IFNAME_MAX_SIZE + 1]; 309 char reserve[1]; 310 }; 311 312 struct hostapd_conf { 313 unsigned char bssid[ETH_ALEN]; 314 char resv1[WPA_ALIGN_SIZE - ETH_ALEN % WPA_ALIGN_SIZE]; 315 char ssid[EXT_WIFI_MAX_SSID_LEN + 1]; 316 char resv2[WPA_ALIGN_SIZE - 1]; 317 unsigned int channel_num; 318 int wpa_key_mgmt; 319 int wpa_pairwise; 320 ext_wifi_auth_mode authmode; 321 unsigned char key[WPA_MAX_KEY_LEN + 1]; 322 char resv3[WPA_ALIGN_SIZE - 1]; 323 int auth_algs; 324 unsigned char wep_idx; 325 char resv4[WPA_ALIGN_SIZE - 1]; 326 int wpa; 327 char driver[MAX_DRIVER_NAME_LEN]; 328 int ignore_broadcast_ssid; 329 }; 330 331 typedef enum { 332 WIFI_EXTEND_IE1 = 7, // same wifi driver 333 WIFI_EXTEND_IE2, 334 WIFI_EXTEND_IE3, 335 WIFI_EXTEND_IE4, 336 WIFI_EXTEND_IE_BUTT 337 } wifi_extend_ie_index; 338 339 typedef struct hostapd_conf wifi_softap_config; 340 341 typedef enum { 342 WIFI_CSI_SWITCH_OFF, 343 WIFI_CSI_SWITCH_ON, 344 WIFI_CSI_SWITCH_BUTT 345 } wifi_csi_switch; 346 347 typedef struct { 348 u8 mac[ETH_ALEN]; 349 } ext_sta_info; 350 351 #ifdef LOS_CONFIG_P2P 352 typedef struct { 353 char intent_param[WPA_P2P_INTENT_LEN + 1]; 354 char addr_txt[EXT_WIFI_TXT_ADDR_LEN + 1]; 355 char join_txt[WPA_P2P_JOIN_LEN + 1]; 356 char wps_txt[WPS_PINCODE_MAX_LEN]; 357 } p2p_connect_param; 358 359 typedef struct { 360 char mac_set[WPA_INVITE_ADDR_LEN]; 361 char per_id_set[WPA_INVITE_PERSISTENT_ID]; 362 char group[WPA_P2P_GROUP_MAX_LEN]; 363 } p2p_invite_param; 364 #endif 365 366 typedef enum { 367 WIFI_STASTUS_OK, 368 WIFI_STASTUS_PTR_NULL, 369 WIFI_STASTUS_CHAN_FAIL, 370 WIFI_STASTUS_SET_FAIL 371 } ext_wifi_status_val; 372 373 typedef struct { 374 ext_wifi_status *conn_status; 375 osal_semaphore sta_status_sem; 376 ext_wifi_status_val status; 377 } ext_wifi_status_sem; 378 379 extern int g_fast_connect_flag; 380 extern int g_fast_connect_scan_flag; 381 extern int g_connecting_flag; 382 extern wpa_rm_network g_wpa_rm_network; 383 extern int g_usr_scanning_flag; 384 extern size_t g_result_len; 385 extern char *g_scan_result_buf; 386 extern struct wifi_reconnect_set g_reconnect_set; 387 extern struct wifi_sta_opt_set g_sta_opt_set; 388 extern struct ext_scan_record g_scan_record; 389 extern struct wpa_ssid *g_connecting_ssid; 390 extern uapi_wifi_event_cb g_wpa_event_cb; 391 extern ext_wifi_scan_no_save_cb g_raw_scan_cb; 392 extern unsigned int g_wpa_event; 393 extern unsigned int g_softap_event; 394 #ifdef LOS_CONFIG_P2P 395 extern unsigned int g_p2p_event; 396 #endif /* LOS_CONFIG_P2P */ 397 extern unsigned int g_sta_num; 398 extern ext_wifi_status *g_sta_status; 399 extern unsigned int g_mesh_get_sta_flag; 400 extern unsigned char g_quick_conn_psk[EXT_WIFI_STA_PSK_LEN]; 401 extern unsigned int g_quick_conn_psk_flag; 402 extern struct wifi_ap_opt_set g_ap_opt_set; 403 extern struct hostapd_data *g_hapd; 404 extern ext_wifi_ap_sta_info *g_ap_sta_info; 405 extern int g_mesh_flag; 406 extern int g_mesh_sta_flag; 407 extern int g_scan_flag; 408 extern struct hostapd_conf g_global_conf; 409 extern unsigned char g_ssid_prefix_flag; 410 extern struct ext_wifi_dev *g_wifi_dev[WPA_MAX_WIFI_DEV_NUM]; 411 412 #ifdef LOS_CONFIG_MESH 413 extern struct wifi_mesh_opt_set g_mesh_opt_set; 414 extern ext_wifi_mesh_peer_info *g_mesh_sta_info; 415 #endif /* LOS_CONFIG_MESH */ 416 417 #ifdef LOS_CONFIG_P2P 418 extern ext_wifi_p2p_status_info *g_p2p_status_buf; 419 extern ext_wifi_p2p_peer_info *g_p2p_peers; 420 extern unsigned int g_p2p_peers_discoverd; 421 extern ext_wifi_p2p_client_info *g_p2p_client_info; 422 extern ext_p2p_persistent_group *g_p2p_persistent_group; 423 extern unsigned int g_persistent_group_num; 424 int los_set_p2p_wifi_dev_role(wifi_iftype role); 425 #endif 426 void wifi_new_task_event_cb(const ext_wifi_event *event_cb); 427 void los_free_wifi_dev(struct ext_wifi_dev *wifi_dev); 428 #if defined(LOS_CONFIG_P2P) && defined(LOS_CONFIG_P2P_TWO_IFACE) 429 void los_free_p2p_wifi_dev(void); 430 int los_set_p2p_go_gc_wifi_dev(struct wpa_supplicant *wpa_s, int role); 431 #endif 432 int los_wpa_ssid_config_set(struct wpa_ssid *ssid, const char *name, const char *value); 433 int los_freq_to_channel(int freq, unsigned int *channel); 434 struct ext_wifi_dev * los_get_wifi_dev_by_name(const char *ifname); 435 struct ext_wifi_dev * los_get_wifi_dev_by_iftype(ext_wifi_iftype iftype); 436 struct ext_wifi_dev * los_get_wifi_dev_by_priv(const void *ctx); 437 int los_count_wifi_dev_in_use(void); 438 struct ext_wifi_dev * wpa_get_other_existed_wpa_wifi_dev(const void *priv); 439 int los_wpa_scan(struct wpa_scan_params *params, ext_wifi_iftype iftype); 440 int wifi_scan(ext_wifi_iftype iftype, bool is_mesh, const ext_wifi_scan_params *sp); 441 int wifi_scan_result_bssid_parse(char **starttmp, void *buf, size_t *reply_len); 442 int wifi_scan_result_freq_parse(char **starttmp, void *buf, size_t *reply_len); 443 int wifi_scan_result_rssi_parse(char **starttmp, void *buf, size_t *reply_len); 444 void wifi_scan_result_base_flag_parse(const char *starttmp, void *buf); 445 int wifi_scan_result_filter_parse(const void *buf); 446 int wifi_scan_result_ssid_parse(char **starttmp, void *buf, size_t *reply_len); 447 int wifi_scan_result(ext_wifi_iftype iftype); 448 int chan_to_freq(unsigned char chan); 449 int addr_precheck(const unsigned char *addr); 450 int try_set_lock_flag(void); 451 void clr_lock_flag(void); 452 int is_lock_flag_off(void); 453 int is_ap_mesh_or_p2p_on(void); 454 int is_hex_string(const char *data, size_t len); 455 int wifi_add_iface(const struct ext_wifi_dev *wifi_dev); 456 int wifi_wpa_start(const struct ext_wifi_dev *wifi_dev); 457 int wifi_remove_iface(struct ext_wifi_dev *wifi_dev); 458 int wifi_wpa_stop(ext_wifi_iftype iftype); 459 int los_set_wifi_dev(struct ext_wifi_dev *wifi_dev); 460 struct ext_wifi_dev * wifi_dev_get(ext_wifi_iftype iftype); 461 struct ext_wifi_dev * wifi_dev_creat(ext_wifi_iftype iftype, protocol_mode_enum mode); 462 int wifi_channel_check(unsigned char chan); 463 void wpa_event_task_free(void); 464 #endif 465