tkl_wifi | Wi-Fi Driver
The tkl_wifi.c
file provides a series of Wi-Fi related APIs to adapt to different network interfaces. These APIs include setting/reading mode, channel, MAC address, connection, and other related operations. The file is also automatically generated by the Tuya Operating System (TuyaOS) and reserves areas for developers to implement code.
Index
- tkl_wifi_init
- tkl_wifi_scan_ap
- tkl_wifi_release_ap
- tkl_wifi_start_ap
- tkl_wifi_stop_ap
- tkl_wifi_set_cur_channel
- tkl_wifi_get_cur_channel
- tkl_wifi_set_sniffer
- tkl_wifi_get_ip
- tkl_wifi_set_ip
- tkl_wifi_set_mac
- tkl_wifi_get_mac
- tkl_wifi_set_work_mode
- tkl_wifi_get_work_mode
- tkl_wifi_get_connected_ap_info
- tkl_wifi_get_bssid
- tkl_wifi_set_country_code
- tkl_wifi_set_rf_calibrated
- tkl_wifi_set_lp_mode
- tkl_wifi_station_fast_connect
- tkl_wifi_station_connect
- tkl_wifi_station_disconnect
- tkl_wifi_station_get_conn_ap_rssi
- tkl_wifi_station_get_status
- tkl_wifi_send_mgnt
- tkl_wifi_register_recv_mgnt_callback
API Description
tkl_wifi_init
OPERATE_RET tkl_wifi_init(WIFI_EVENT_CB cb)
-
Function Description:
Set the Wi-Fi working status.
-
Parameters:
Input/Output Parameter Name Description [in] cb Wi-Fi working status callback -
Return Value:
A return value of
OPRT_OK
indicates success, other return values indicate an error, please refer totuya_error_code.h
for error information.
tkl_wifi_scan_ap
OPERATE_RET tkl_wifi_scan_ap(const int8_t *ssid, AP_IF_S **ap_ary, uint32_t *num)
-
Function Description:
Scan the current environment to obtain AP information in the current environment.
-
Parameters:
Input/Output Parameter Name Description [in] ssid If ssid = NULL, scan all APs; otherwise, scan the specified AP. [out] ap_ary Scanning result information. [out] num Number of scanned APs. -
Return Value:
A return value of
OPRT_OK
indicates success, other return values indicate an error, please refer totuya_error_code.h
for error information. -
Remarks:
- Blocking function.
- Can only scan APs in the current country code channel list.
- When there is an AP with the specified SSID in the environment, the success rate of scanning the SSID should exceed 98%.
- Memory needs to be allocated within this API (for scanning multiple APs, continuous memory space needs to be allocated).
- The memory is released by the Tuya SDK by actively calling
tkl_wifi_release_ap
.
tkl_wifi_release_ap
OPERATE_RET tkl_wifi_release_ap(AP_IF_S *ap)
-
Function Description:
Release the AP information resources.
-
Parameters:
Input/Output Parameter Name Description [in] ap The AP information to be released. -
Return Value:
A return value of
OPRT_OK
indicates success, other return values indicate an error, please refer totuya_error_code.h
for error information.
tkl_wifi_start_ap
OPERATE_RET tkl_wifi_start_ap(const WF_AP_CFG_IF_S *cfg)
-
Function Description:
Start AP mode.
-
Parameters:
Input/Output Parameter Name Description [in] cfg AP configuration parameters. -
Return Value:
A return value of
OPRT_OK
indicates success, other return values indicate an error, please refer totuya_error_code.h
for error information. -
Remarks:
- It is necessary to set the maximum number of STAs that can successfully connect to the AP mode according to
max_conn
. - The IP information for the AP mode needs to be set according to the IP information.
- In stationap mode, if the AP port is connected, when the station port connects to the router, the AP port cannot be disconnected during the connection process, and it can receive and send broadcast packets even if the connection fails.
- Coexistence of EZ networking and AP: 4.1. In softap or stationap mode, the sniffer function can be enabled and the channel can be switched. 4.2. After enabling the sniffer function, the data given to the callback must include the device connection request packets of the AP port.
- It is necessary to set the maximum number of STAs that can successfully connect to the AP mode according to
tkl_wifi_stop_ap
OPERATE_RET tkl_wifi_stop_ap(void)
-
Function Description:
Stop AP mode.
-
Parameters:
None
-
Return Value:
A return value of
OPRT_OK
indicates success, other return values indicate an error, please refer totuya_error_code.h
for error information. -
Remarks:
- Different shutdown logic is required depending on whether the device is in softap or stationap mode.
- In stationap mode, if there is a connection on the station port, there should be no disconnection during the closure of the AP port.
tkl_wifi_set_cur_channel
OPERATE_RET tkl_wifi_set_cur_channel(const uint8_t chan)
-
Function Description:
Set the channel.
-
Parameters:
Input/Output Parameter Name Description [in] chan Channel. -
Return Value:
A return value of
OPRT_OK
indicates success, other return values indicate an error, please refer totuya_error_code.h
for error information. -
Remarks:
- Setting a channel outside the current country code channel range will fail.
- Setting the channel in the sniffer callback is supported.
tkl_wifi_get_cur_channel
OPERATE_RET tkl_wifi_get_cur_channel(uint8_t *chan);
-
Function Description:
Get the current channel.
-
Parameters:
Input/Output Parameter Name Description [out] chan Channel. -
Return Value:
A return value of
OPRT_OK
indicates success, other return values indicate an error, please refer totuya_error_code.h
for error information.
tkl_wifi_set_sniffer
OPERATE_RET tkl_wifi_set_sniffer(const BOOL_T en, const SNIFFER_CALLBACK cb)
-
Function Description:
Set the sniffer function switch.
-
Parameters:
Input/Output Parameter Name Description [in] en Enable or disable the sniffer function. [in] cb Packet capture data callback. -
Return Value:
A return value of
OPRT_OK
indicates success, other return values indicate an error, please refer totuya_error_code.h
for error information. -
Remarks:
The data returned to the application must include management packet data.
tkl_wifi_get_ip
OPERATE_RET tkl_wifi_get_ip(const WF_IF_E wf, NW_IP_S *ip)
-
Function Description:
Get Wi-Fi IP information (IP address, gateway address, subnet mask).
-
Parameters:
Input/Output Parameter Name Description [in] wf Wi-Fi mode. [out] ip IP information. -------- -------- WF_STATION station type WF_AP ap type -
Return Value:
A return value of
OPRT_OK
indicates success, other return values indicate an error, please refer totuya_error_code.h
for error information. -
Remarks:
It is necessary to distinguish between IP information under different modes (based on the passed parameter
wf
).
tkl_wifi_set_ip
OPERATE_RET tkl_wifi_set_ip(const WF_IF_E wf, NW_IP_S *ip)
-
Function Description:
Set Wi-Fi static IP information (IP address, gateway address, subnet mask).
-
Parameters:
Input/Output Parameter Name Description [in] wf Wi-Fi mode. [in] ip IP information. -------- -------- WF_STATION station type WF_AP ap type -
Return Value:
A return value of
OPRT_OK
indicates success, other return values indicate an error, please refer totuya_error_code.h
for error information. -
Remarks:
It is necessary to distinguish between IP information under different modes (based on the passed parameter
wf
).
tkl_wifi_set_mac
OPERATE_RET tkl_wifi_set_mac(const WF_IF_E wf, const NW_MAC_S *mac)
-
Function Description:
Set MAC address
-
Parameters:
Input/Output Parameter Name Description [in] wf WiFi mode [in] mac MAC address -------- -------- WF_STATION station type WF_AP AP type -
Return Value:
A return value of
OPRT_OK
indicates success, other return values indicate an error. Please refer totuya_error_code.h
for error information. -
Remarks:
Permanent effect, not lost after power off
tkl_wifi_get_mac
OPERATE_RET tkl_wifi_get_mac(const WF_IF_E wf, NW_MAC_S *mac);
-
Function Description:
Get MAC address
-
Parameters:
Input/Output Parameter Name Description [in] wf WiFi mode [out] mac MAC address -------- -------- WF_STATION station type WF_AP AP type -
Return Value:
A return value of
OPRT_OK
indicates success, other return values indicate an error. Please refer totuya_error_code.h
for error information. -
Remarks:
The MAC address under station and AP modes needs to be distinguished based on the working mode (according to the passed parameter wf)
tkl_wifi_set_work_mode
OPERATE_RET tkl_wifi_set_work_mode(const WF_WK_MD_E mode)
-
Function Description:
Set WiFi working mode
-
Parameters:
Input/Output Parameter Name Description [in] mode WiFi working mode -------- -------- WWM_LOWPOWER Low power (refers to turning off the WiFi module) WWM_SNIFFER monitor WWM_STATION station WWM_SOFTAP softap WWM_STATIONAP stationap -
Return Value:
A return value of
OPRT_OK
indicates success, other return values indicate an error. Please refer totuya_error_code.h
for error information. -
Remarks:
- From device power-on to calling tuya entrance, it is required to be within 200ms.
- If the device initialization time exceeds 200ms, the underlying layer can do some necessary initialization and then call tuya entrance, placing the time-consuming WiFi initialization in another thread.
- tkl_wifi_set_work_mode is the first API used by tuya business to call WiFi, in this API it is necessary to determine whether the WiFi initialization is complete, if not, wait here until it is complete before proceeding (only needs to be checked once).
- If the device power-on to tuya entrance is less than 200ms, there is no need to consider this point.
tkl_wifi_get_work_mode
OPERATE_RET tkl_wifi_get_work_mode(WF_WK_MD_E *mode)
-
Function Description:
Get WiFi working mode
-
Parameters:
Input/Output Parameter Name Description [out] mode WiFi working mode -------- -------- WWM_LOWPOWER Low power (refers to turning off the WiFi module) WWM_SNIFFER monitor WWM_STATION station WWM_SOFTAP softap WWM_STATIONAP stationap -
Return Value:
A return value of
OPRT_OK
indicates success, other return values indicate an error. Please refer totuya_error_code.h
for error information.
tkl_wifi_get_connected_ap_info
OPERATE_RET tkl_wifi_get_connected_ap_info(FAST_WF_CONNECTED_AP_INFO_T **fast_ap_info)
-
Function Description:
Get the connected AP information, used for fast connection feature
-
Parameters:
Input/Output Parameter Name Description [out] fast_ap_info Connected AP information -
Return Value:
A return value of
OPRT_OK
indicates success, other return values indicate an error. Please refer totuya_error_code.h
for error information. -
Remarks:
- fast_ap_info needs to dynamically allocate memory within this API
- Every time the router is successfully reconnected, the router information is obtained once, and if there is an update, it will be synchronized to flash
tkl_wifi_get_bssid
OPERATE_RET tkl_wifi_get_bssid(uint8_t *mac)
-
Function Description:
Get the MAC address of the connected AP
-
Parameters:
Input/Output Parameter Name Description [out] mac MAC address -
Return Value:
A return value of
OPRT_OK
indicates success, other return values indicate an error. Please refer totuya_error_code.h
for error information.
tkl_wifi_set_country_code
OPERATE_RET tkl_wifi_set_country_code(const COUNTRY_CODE_E ccode)
-
Function Description:
Set country code
-
Parameters:
Input/Output Parameter Name Description [in] ccode Country code -------- -------- COUNTRY_CODE_CN China region 1-13 COUNTRY_CODE_US US region 1-11 COUNTRY_CODE_JP Japan region 1-14 COUNTRY_CODE_EU Europe region 1-13, need to consider European adaptive -
Return Value:
A return value of
OPRT_OK
indicates success, other return values indicate an error. Please refer totuya_error_code.h
for error information. -
Remarks:
- Country code currently only needs to support China region (CN 1-13), US region (US 1-11), Japan region (JP 1-14), Europe region (EU 1-13)
- Requirements for different country codes are as follows: 2.1 When scanning routers, only APs corresponding to the channel list of the country code can be scanned. 2.2 The European region needs to implement European adaptive function 2.3 When setting the channel, if the set channel is not within the channel list range of the current country code, the channel setting returns an error
tkl_wifi_set_rf_calibrated
BOOL_T tkl_wifi_set_rf_calibrated(void);
-
Function Description:
Check if RF has been calibrated
-
Parameters:
None
-
Return Value:
- TRUE: Calibrated
- FALSE: Not calibrated
tkl_wifi_set_lp_mode
OPERATE_RET tkl_wifi_set_lp_mode(const BOOL_T enable, const uint8_t dtim)
-
Function Description:
WiFi low power mode setting
-
Parameters:
Input/Output Parameter Name Description [in] enable Whether to enable WiFi low power mode [in] dtim dtim parameter -
Return Value:
A return value of
OPRT_OK
indicates success, other return values indicate an error. Please refer totuya_error_code.h
for error information.
tkl_wifi_station_fast_connect
OPERATE_RET tkl_wifi_station_fast_connect(const FAST_WF_CONNECTED_AP_INFO_T *fast_ap_info)
-
Function Description:
Fast connect to router
-
Parameters:
Input/Output Parameter Name Description [in] fast_ap_info AP information required for fast connection -
Return Value:
A return value of
OPRT_OK
indicates success, other return values indicate an error. Please refer totuya_error_code.h
for error information. -
Remarks:
This function is only called for the first connection after the network is configured and the device is restarted.
tkl_wifi_station_connect
OPERATE_RET tkl_wifi_station_connect(const int8_t *ssid, const int8_t *passwd)
-
Function Description:
Connect to router
-
Parameters:
Input/Output Parameter Name Description [in] ssid ssid [in] passwd passwd -
Return Value: A return value of
OPRT_OK
indicates success, other return values indicate an error. Please refer totuya_error_code.h
for error information. -
Remarks:
- Non-blocking, after successfully starting the connection process, the upper layer will call tkl_wifi_station_get_status every 1 second to query the Wi-Fi connection status.
- The auto-reconnect feature needs to be enabled, with a reconnect time within 1 minute. The upper layer will initiate a reconnect every 1 minute in the case of disconnection.
tkl_wifi_station_disconnect
OPERATE_RET tkl_wifi_station_disconnect(void)
-
Function Description: Disconnect from router
-
Parameters: None
-
Return Value: A return value of
OPRT_OK
indicates success, other return values indicate an error. Please refer totuya_error_code.h
for error information.
tkl_wifi_station_get_conn_ap_rssi
OPERATE_RET tkl_wifi_station_get_conn_ap_rssi(int8_t *rssi)
-
Function Description:
Get the signal strength of the connected AP
-
Parameters:
Input/Output Parameter Name Description [out] rssi Signal strength -
Return Value:
A return value of
OPRT_OK
indicates success, other return values indicate an error. Please refer totuya_error_code.h
for error information. -
Remarks:
- Without moving the device and the router, the rssi obtained multiple times can only fluctuate slightly.
- Moving the device and the router will result in corresponding changes in the obtained rssi.
tkl_wifi_station_get_status
OPERATE_RET tkl_wifi_station_get_status(WF_STATION_STAT_E *stat)
-
Function Description:
Get the current connection status of Wi-Fi
-
Parameters:
Input/Output Parameter Name Description [out] stat Connection status -------- -------- WSS_IDLE WSS_CONNECTING Connecting WSS_PASSWD_WRONG Wrong password WSS_CONN_FAIL Connection failed WSS_CONN_SUCCESS Connection successful WSS_GOT_IP DHCP successful -
Return Value:
A return value of
OPRT_OK
indicates success, other return values indicate an error. Please refer totuya_error_code.h
for error information.
tkl_wifi_send_mgnt
OPERATE_RET tkl_wifi_send_mgnt(const uint8_t *buf, const uint32_t len)
-
Function Description:
Send management packet data
-
Parameters:
Input/Output Parameter Name Description [in] buf Management packet data buffer [in] len Length of management packet data -
Return Value:
A return value of
OPRT_OK
indicates success, other return values indicate an error. Please refer totuya_error_code.h
for error information.
tkl_wifi_register_recv_mgnt_callback
OPERATE_RET tkl_wifi_register_recv_mgnt_callback(const BOOL_T enable, const WIFI_REV_MGNT_CB recv_cb)
-
Function Description:
Set whether the application layer should receive management packet data
-
Parameters:
Input/Output Parameter Name Description [in] enable Whether to enable receiving management packet data [in] recv_cb Callback for receiving management packet data -
Return Value:
A return value of
OPRT_OK
indicates success, other return values indicate an error. Please refer totuya_error_code.h
for error information.