1 // Copyright (C) 2022 Beken Corporation 2 // 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 #pragma once 16 17 #include <common/bk_include.h> 18 #include <common/bk_err.h> 19 #include "wlan_defs_pub.h" 20 #include <modules/wifi.h> 21 #include "bk_rw_msg.h" 22 #include "bk_compiler.h" 23 24 #ifdef __cplusplus 25 extern "C" { 26 #endif 27 28 /** 29 * @brief WiFi APIs Version 1.0 30 * @defgroup bk_api_wifi_v1 Old WiFi API group 31 * @{ 32 */ 33 34 /* TODO add general description about legacy WiFi API 35 * 36 */ 37 38 /** 39 * @brief configure country info 40 * 41 * @attention 1. The default country is {.cc="CN", .schan=1, .nchan=13, policy=WIFI_COUNTRY_POLICY_AUTO} 42 * @attention 2. When the country policy is WIFI_COUNTRY_POLICY_AUTO, the country info of the AP to which 43 * the station is connected is used. E.g. if the configured country info is {.cc="USA", .schan=1, .nchan=11} 44 * and the country info of the AP to which the station is connected is {.cc="JP", .schan=1, .nchan=14} 45 * then the country info that will be used is {.cc="JP", .schan=1, .nchan=14}. If the station disconnected 46 * from the AP the country info is set back back to the country info of the station automatically, 47 * {.cc="USA", .schan=1, .nchan=11} in the example. 48 * @attention 3. When the country policy is WIFI_COUNTRY_POLICY_MANUAL, always use the configured country info. 49 * @attention 4. When the country info is changed because of configuration or because the station connects to a different 50 * external AP, the country IE in probe response/beacon of the soft-AP is changed also. 51 * @attention 5. The country configuration is not stored into flash 52 * @attention 6. This API doesn't validate the per-country rules, it's up to the user to fill in all fields according to 53 * local regulations. 54 * 55 * @param country the configured country info 56 * 57 * @return 58 * - kNoErr: succeed 59 * - kNotInitializedErr: WiFi is not initialized 60 * - kParamErr: invalid argument 61 */ 62 __bk_deprecated 63 bk_err_t bk_wlan_set_country(const wifi_country_t *country); 64 65 /** 66 * @brief get the current country info 67 * 68 * @param country country info 69 * 70 * @return 71 * - kNoErr: succeed 72 * - kNotInitializedErr: WiFi is not initialized 73 * - kParamErr: invalid argument 74 */ 75 __bk_deprecated 76 bk_err_t bk_wlan_get_country(wifi_country_t *country); 77 78 /** @brief Connect or establish a Wi-Fi network in normal mode (station or soft ap mode). 79 * 80 * This function can establish a Wi-Fi connection as a station or create 81 * a soft AP that other staions can connect (4 stations Max). In station mode, 82 * _BK_ first scan all of the supported Wi-Fi channels to find a wlan that 83 * matchs the input SSID, and read the security mode. Then try to connect 84 * to the target wlan. If any error occurs in the connection procedure or 85 * disconnected after a successful connection, _BK_ start the reconnection 86 * procedure in backgound after a time interval defined in inNetworkInitPara. 87 * Call this function twice when setup coexistence mode (staion + soft ap). 88 * This function retruns immediately in station mode, and the connection will 89 * be executed in background. 90 * 91 * @param inNetworkInitPara: Specifies wlan parameters. 92 * 93 * @return In station mode, allways retrurn kWlanNoErr. 94 * In soft ap mode, return kWlanXXXErr 95 */ 96 __bk_deprecated 97 bk_err_t bk_wlan_start(network_InitTypeDef_st* inNetworkInitPara); 98 99 /** 100 * @brief stop STA or AP 101 * 102 * @param mode mode can be BK_SOFT_AP or BK_STATION 103 * 104 * @return 105 * - kNoErr: succeed 106 * - otherwise: fail 107 */ 108 __bk_deprecated 109 int bk_wlan_stop(char mode); 110 111 /** @brief Connect to a Wi-Fi network with advantage settings (station mode only) 112 * 113 * This function can connect to an access point with precise settings, 114 * that greatly speed up the connection if the input settings are correct 115 * and fixed. If this fast connection is failed for some reason, _BK_ 116 * change back to normal: scan + connect mode refer to @ref bk_wlan_start. 117 * This function returns after the fast connection try. 118 * 119 * @note This function cannot establish a soft ap, use StartNetwork() for this 120 * purpose. 121 * If input SSID length is 0, _BK_ use BSSID to connect the target wlan. 122 * If both SSID and BSSID are all wrong, the connection will be failed. 123 * 124 * @param inNetworkInitParaAdv: Specifies the precise wlan parameters. 125 * 126 * @return Allways return kWlanNoErr although error occurs in first fast try 127 * kWlanTimeoutErr: DHCP client timeout 128 */ 129 __bk_deprecated 130 bk_err_t bk_wlan_start_sta_adv(network_InitTypeDef_adv_st* inNetworkInitParaAdv); 131 132 /** @brief Read current wireless link status on station interface. 133 * 134 * @param outStatus: Point to the buffer to store the link status. 135 * 136 * @return 137 * - kNoErr : on success. 138 * - kGeneralErr : fail 139 */ 140 __bk_deprecated 141 bk_err_t bk_wlan_get_link_status(LinkStatusTypeDef *outStatus); 142 143 /** 144 * @brief Start scan to scan all APs in all channels 145 * 146 * When scan is completed, the register scan callback is called and the application 147 * can handle the scan result in the callback. 148 * 149 * TODO add more about how to use scan, backgroud scan, passive or active scan etc? 150 */ 151 __bk_deprecated 152 void bk_wlan_start_scan(void); 153 154 /** 155 * @brief Start scan to scan specified SSIDs in all channels 156 * 157 * - When scan is completed, the register scan callback is called and the application 158 * can handle the scan result in the callback. 159 * - Compare to /ref bk_wlan_start_scan, this API only scan specified SSIDs. 160 * 161 * TODO add more about how to use assigned scan 162 * 163 * @param ssid_array SSIDs to be scaned 164 * @param ssid_num SSID number in ssid_ary 165 */ 166 __bk_deprecated 167 void bk_wlan_start_assign_scan(UINT8 **ssid_array, UINT8 ssid_num); 168 169 /** 170 * @brief Register scan completion callback 171 * 172 * When scan is completed, the register scan callback will be called, the application 173 * can handle the result in the callback. 174 * 175 * @param ind_cb scan callback 176 */ 177 __bk_deprecated 178 void bk_wlan_scan_ap_reg_cb(FUNC_2PARAM_PTR ind_cb); 179 180 /** 181 * @brief Get the number of found APs 182 * 183 * In scan callback, the application can get the number of found APs by calling this API. 184 * 185 * @return return the number of found APs 186 */ 187 __bk_deprecated 188 unsigned char bk_wlan_get_scan_ap_result_numbers(void); 189 190 /** 191 * @brief Get all APs found by this scan 192 * 193 * In scan callback, the application can get all found by calling this API. 194 * 195 * @param scan_result_table store the found APs 196 * @param ap_num the maxiumu AP number that scan_result_table can store 197 * 198 * @return return the actual number of returned APs 199 */ 200 __bk_deprecated 201 int bk_wlan_get_scan_ap_result(SCAN_RST_ITEM_PTR scan_result_table,unsigned char ap_num); 202 203 /** 204 * @brief Send raw 802.11 frame 205 * 206 * @attention 1. This API can be used in WiFi station, softap, or monitor mode. 207 * @attention 2. Only support to send non-QoS frame. 208 * @attention 3. The frame sequence will be overwritten by WiFi driver. 209 * @attention 4. The API doesn't check the correctness of the raw frame, the 210 * caller need to guarantee the correctness of the frame. 211 * 212 * @param buffer raw 802.11 frame 213 * @param len the length of the 802.11 frame 214 * 215 * @return 216 * - kNoErr: success 217 * - otherwise: fail 218 */ 219 __bk_deprecated 220 int bk_wlan_send_80211_raw_frame(uint8_t *buffer, int len); 221 222 /** 223 * @brief set filter bitmap 224 * 225 * - If bit MONITOR_FILTER_MUL_BRD_CAST is set, the multicast data frame is not 226 * filtered to the monitor callback; 227 * - If bit MONITOR_FILTER_DUPL_FRM is set, the duplicated data frame is not 228 * filtered to the monitor callback. 229 * 230 * @param filter filter bitmap 231 * 232 * @return 233 * - kNoErr : on success. 234 * - kGeneralErr : fail 235 */ 236 __bk_deprecated 237 void bk_wlan_set_monitor_filter(unsigned char filter); 238 239 /** 240 * @brief check whether the filter_type is set or not 241 * 242 * This function is used internally by SDK, it's used to check whether the 243 * specified filter_type is set or not. 244 * 245 * @param filter_type filter type 246 * 247 * @return 248 * - true : if filter_type is set 249 * - false : if filter_type is not set 250 */ 251 __bk_deprecated 252 int bk_wlan_monitor_filter(unsigned char filter_type); 253 254 /** @brief Start wifi monitor mode 255 * 256 * @attention This function disconnects wifi station and softAP before entering monitor mode. 257 * 258 * @return 259 * - kNoErr : on success. 260 * - kGeneralErr : fail 261 */ 262 __bk_deprecated 263 int bk_wlan_start_monitor(void); 264 265 /** @brief Stop wifi monitor mode 266 * 267 * @return 268 * - kNoErr : on success. 269 * - kGeneralErr : fail 270 */ 271 __bk_deprecated 272 int bk_wlan_stop_monitor(void); 273 274 /** @brief Set the monitor channel synchronous 275 * 276 * This function is for monitor mode only, it can change the channel dynamically 277 * without restarting the monitor mode. 278 * 279 * @param channel the monitor channel, range is from 1 to 13 280 * 281 * @return 282 * - kNoErr : on success. 283 * - kGeneralErr : fail 284 */ 285 __bk_deprecated 286 int bk_wlan_set_channel_sync(int channel); 287 288 /** 289 * @brief Set the monitor channel asynchronous 290 * 291 * @param channel the monitor channel, range is from 1 to 13 292 * 293 * @return 294 * - kNoErr : on success. 295 * - kGeneralErr : fail 296 */ 297 __bk_deprecated 298 int bk_wlan_set_channel(int channel); 299 300 /** 301 * @brief start the station 302 * 303 * This function disconnect the station first and the connect the station 304 * with the new configuration. 305 * 306 * @param inNetworkInitPara the configuration of the station 307 * 308 * @return 309 * - kNoErr : on success. 310 * - kGeneralErr : fail 311 */ 312 __bk_deprecated 313 bk_err_t bk_wlan_start_sta(network_InitTypeDef_st *inNetworkInitPara); 314 315 /** 316 * @brief start the AP 317 * 318 * This function stop the AP first and then start the AP with new configuration. 319 * 320 * @param inNetworkInitPara the configuration of the AP 321 * 322 * @return 323 * - kNoErr : on success. 324 * - kGeneralErr : fail 325 */ 326 __bk_deprecated 327 bk_err_t bk_wlan_start_ap(network_InitTypeDef_st *inNetworkInitPara); 328 329 /** 330 * @brief start the AP 331 * 332 * This function stop the AP first and then start the AP with new configuration. 333 * It can set more configurations than /ref bk_wlan_start_ap. 334 * 335 * @param inNetworkInitParaAP the configuration of the AP 336 * 337 * @return 338 * - kNoErr : on success. 339 * - kGeneralErr : fail 340 */ 341 __bk_deprecated 342 bk_err_t bk_wlan_start_ap_adv(network_InitTypeDef_ap_st *inNetworkInitParaAP); 343 344 /** 345 * @brief set AP default channel 346 * 347 * TODO - precondition to use this API? 348 * 349 * @param channel AP channel 350 */ 351 __bk_deprecated 352 void bk_wlan_ap_set_default_channel(uint8_t channel); 353 354 /** 355 * @brief The callback function of 802.11 frame receiving 356 * 357 * When WiFi driver receives 802.11 frame that should be filtered to application, 358 * the registered callback function will be called. For the filter condition, 359 * refer to specific monitor register API, such as bk_wlan_register_monitor_cb etc. 360 * 361 * @param data the 802.11 frame received by WiFi driver 362 * @param len the length of data 363 * @param info link info, such as rssi etc 364 * 365 */ 366 typedef void (*monitor_cb_t)(uint8_t *data, int len, wifi_link_info_t *info); 367 368 /** 369 * @brief Register the monitor callback function 370 * 371 * On receiving a 802.11 packet, the WiFi driver calls the registered function to handle the packets. 372 * TODO - add more description which frame should be filtered! 373 * 374 * @param fn callback function to receive the WiFi packets 375 * 376 * @return 377 * - kNoErr : on success. 378 * - kGeneralErr : fail 379 */ 380 __bk_deprecated 381 void bk_wlan_register_monitor_cb(monitor_cb_t fn); 382 383 384 #if CONFIG_DEEP_PS 385 386 /** 387 * @brief Request deep sleep, and wakeup by gpio. 388 * 389 * TODO - add detail? 390 * TODO - why this API is so complex? can we simplify it? 391 * 392 * @param gpio_index_map The gpio bitmap which set 1 enable wakeup deep sleep. 393 * gpio_index_map is hex and every bits is map to gpio0-gpio31. 394 * gpio_edge_map The gpio edge bitmap for wakeup gpios, 395 * gpio_edge_map is hex and every bits is map to gpio0-gpio31. 396 * 0:rising,1:falling. 397 */ 398 void bk_enter_deep_sleep(UINT32 gpio_index_map, 399 UINT32 gpio_edge_map, 400 UINT32 gpio_last_index_map, 401 UINT32 gpio_last_edge_map, 402 UINT32 sleep_time, 403 UINT32 wake_up_way, 404 UINT32 gpio_stay_lo_map, 405 UINT32 gpio_stay_hi_map); 406 407 #endif 408 409 /** 410 * @brief Enable DTIM power save 411 * 412 * DTIM power save will enable/disable WiFi RF dynamically according to 413 * WiFi power management protocol. 414 * 415 * TODO add more detail about DTIM power save, such as: 416 * - precondition to use it 417 * - how about AP/STA coexist? 418 * - how about WIFI/BLE coexist? 419 * - how about Monitor mode? 420 * 421 * @return 422 * - kNoErr : on success. 423 * - otherwise: fail 424 */ 425 __bk_deprecated 426 int bk_wlan_ps_enable(void ); 427 428 /** 429 * @brief Disable DTIM power save 430 * 431 * @return 432 * - kNoErr : on success. 433 * - otherwise: fail 434 */ 435 __bk_deprecated 436 int bk_wlan_ps_disable(void); 437 438 /** 439 * @brief Enable DTIM power save timer 440 * 441 * TODO why we need this API? for keepalive? 442 * 443 * @return 444 * - kNoErr : on success. 445 * - oterwise: fail 446 */ 447 __bk_deprecated 448 int bk_wlan_ps_timer_start(void); 449 450 /** 451 * @brief Disable DTIM power save timer 452 * 453 * TODO add details 454 * 455 * @return 456 * - kNoErr : on success. 457 * - otherwise: fail 458 */ 459 __bk_deprecated 460 int bk_wlan_ps_timer_pause(void); 461 462 /** 463 * @brief Enable MCU power save 464 * 465 * When MCU power save is enabled, the power management model low the MCU 466 * frequency to save power if it detects there is no activity; and resume 467 * the MCU if specific activities happen, such as interrupt, timer etc. 468 * 469 * TODO add details. 470 * 471 * @return 472 * - kNoErr : on success. 473 * - otherwise: fail 474 */ 475 __bk_deprecated 476 int bk_wlan_mcu_ps_mode_enable(void); 477 478 /** 479 * @brief Disable MCU power save 480 * 481 * TODO add details. 482 * 483 * @return 484 * - kNoErr : on success. 485 * - otherwise: fail 486 */ 487 __bk_deprecated 488 int bk_wlan_mcu_ps_mode_disable(void); 489 490 /** 491 * @brief Register WiFi status change callback 492 * 493 * If WiFi status is changed, the callback is called by WiFi driver. 494 * 495 * TODO add details. 496 * 497 * @param cb status change callback function 498 */ 499 __bk_deprecated 500 void bk_wlan_status_register_cb(FUNC_1PARAM_PTR cb); 501 502 /** 503 * @brief Get WiFi status change callback 504 * 505 * @return return status callback 506 */ 507 __bk_deprecated 508 FUNC_1PARAM_PTR bk_wlan_get_status_cb(void); 509 510 /** 511 * @brief AP no password callback 512 */ 513 typedef void (*bk_ap_no_password_cb_t)(void); 514 515 /** 516 * @brief Register AP no password callback 517 * 518 * @attention Current this API has no effect at all, in other words the 519 * WiFi driver never calls the callback. 520 * 521 * @return return status callback 522 */ 523 __bk_deprecated 524 void bk_ap_no_password_connected_register_cb(bk_ap_no_password_cb_t func); 525 526 /** 527 * @brief get the system MAC 528 * 529 * @param mac store the system MAC 530 */ 531 __bk_deprecated 532 void bk_wifi_get_mac_address(char *mac); 533 534 /** 535 * @brief Get maximum power level 536 * 537 * TODO add more description about the power level 538 * 539 */ 540 __bk_deprecated 541 uint32_t bk_wlan_max_power_level_get(void); 542 543 /** 544 * @brief Get AP configurations 545 * 546 * @param ap store ap basic configurations 547 * @param key store the pointer of the global key of AP 548 * @param key_len store the key length 549 * 550 * @return 551 * - kNoErr: success 552 * - others: otherwise 553 */ 554 __bk_deprecated 555 bk_err_t bk_wlan_get_bssid_info(apinfo_adv_t *ap, uint8_t **key, int *key_len); 556 557 #ifdef CONFIG_AOS_MESH 558 559 /** 560 * @brief Register mesh monitor callback 561 * 562 * If mesh monitor callback is registered, the WiFi driver filters all received 563 * 802.11 data frame to the callback. 564 * 565 * @param fn mesh monitor callback 566 */ 567 void wlan_register_mesh_monitor_cb(monitor_cb_t fn); 568 569 /** 570 * @brief Set mesh BSSID 571 * 572 * @param bssid the BSSID of mesh 573 * 574 * @return 575 * - kNoErr: success 576 * - others: otherwise 577 */ 578 int wlan_set_mesh_bssid(uint8_t *bssid); 579 580 /** 581 * @brief Get mesh BSSID 582 * 583 * This function return the global mesh BSSID 584 */ 585 uint8_t *wlan_get_mesh_bssid(void); 586 #endif 587 588 /** 589 * @brief Set Wi-Fi state 590 * 591 * This function set Wi-Fi state to stop active scan, to ensure 592 * softap video_transfer 593 */ 594 void bk_wifi_set_video_transfer_state(bool enable); 595 596 /** 597 * @brief Prepare Deepsleep 598 * 599 * This function set Wi-Fi prepare deep sleep 600 * 601 */ 602 void bk_wifi_prepare_deepsleep(void); 603 604 typedef struct { 605 uint32_t sta_start_tick; /**< sta start connect tick */ 606 uint32_t sta_assoc_tick; /**< sta complete assoc tick */ 607 uint32_t sta_eapol_tick; /**< sta complete eapol tick */ 608 uint32_t sta_ip_tick; /**< sta got ip tick */ 609 }wifi_connect_tick_t; 610 extern wifi_connect_tick_t sta_tick; 611 612 /** 613 * @} 614 */ 615 616 #ifdef __cplusplus 617 } 618 #endif 619