Skip to main content

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


API Description

tkl_wifi_init

OPERATE_RET tkl_wifi_init(WIFI_EVENT_CB cb)
  • Function Description:

    Set the Wi-Fi working status.

  • Parameters:

    Input/OutputParameter NameDescription
    [in]cbWi-Fi working status callback
  • Return Value:

    A return value of OPRT_OK indicates success, other return values indicate an error, please refer to tuya_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/OutputParameter NameDescription
    [in]ssidIf ssid = NULL, scan all APs; otherwise, scan the specified AP.
    [out]ap_aryScanning result information.
    [out]numNumber of scanned APs.
  • Return Value:

    A return value of OPRT_OK indicates success, other return values indicate an error, please refer to tuya_error_code.h for error information.

  • Remarks:

    1. Blocking function.
    2. Can only scan APs in the current country code channel list.
    3. When there is an AP with the specified SSID in the environment, the success rate of scanning the SSID should exceed 98%.
    4. Memory needs to be allocated within this API (for scanning multiple APs, continuous memory space needs to be allocated).
    5. 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/OutputParameter NameDescription
    [in]apThe AP information to be released.
  • Return Value:

    A return value of OPRT_OK indicates success, other return values indicate an error, please refer to tuya_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/OutputParameter NameDescription
    [in]cfgAP configuration parameters.
  • Return Value:

    A return value of OPRT_OK indicates success, other return values indicate an error, please refer to tuya_error_code.h for error information.

  • Remarks:

    1. It is necessary to set the maximum number of STAs that can successfully connect to the AP mode according to max_conn.
    2. The IP information for the AP mode needs to be set according to the IP information.
    3. 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.
    4. 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.

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 to tuya_error_code.h for error information.

  • Remarks:

    1. Different shutdown logic is required depending on whether the device is in softap or stationap mode.
    2. 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/OutputParameter NameDescription
    [in]chanChannel.
  • Return Value:

    A return value of OPRT_OK indicates success, other return values indicate an error, please refer to tuya_error_code.h for error information.

  • Remarks:

    1. Setting a channel outside the current country code channel range will fail.
    2. 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/OutputParameter NameDescription
    [out]chanChannel.
  • Return Value:

    A return value of OPRT_OK indicates success, other return values indicate an error, please refer to tuya_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/OutputParameter NameDescription
    [in]enEnable or disable the sniffer function.
    [in]cbPacket capture data callback.
  • Return Value:

    A return value of OPRT_OK indicates success, other return values indicate an error, please refer to tuya_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/OutputParameter NameDescription
    [in]wfWi-Fi mode.
    [out]ipIP information.
    ----------------
    WF_STATIONstation type
    WF_APap type
  • Return Value:

    A return value of OPRT_OK indicates success, other return values indicate an error, please refer to tuya_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/OutputParameter NameDescription
    [in]wfWi-Fi mode.
    [in]ipIP information.
    ----------------
    WF_STATIONstation type
    WF_APap type
  • Return Value:

    A return value of OPRT_OK indicates success, other return values indicate an error, please refer to tuya_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/OutputParameter NameDescription
    [in]wfWiFi mode
    [in]macMAC address
    ----------------
    WF_STATIONstation type
    WF_APAP type
  • Return Value:

    A return value of OPRT_OK indicates success, other return values indicate an error. Please refer to tuya_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/OutputParameter NameDescription
    [in]wfWiFi mode
    [out]macMAC address
    ----------------
    WF_STATIONstation type
    WF_APAP type
  • Return Value:

    A return value of OPRT_OK indicates success, other return values indicate an error. Please refer to tuya_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/OutputParameter NameDescription
    [in]modeWiFi working mode
    ----------------
    WWM_LOWPOWERLow power (refers to turning off the WiFi module)
    WWM_SNIFFERmonitor
    WWM_STATIONstation
    WWM_SOFTAPsoftap
    WWM_STATIONAPstationap
  • Return Value:

    A return value of OPRT_OK indicates success, other return values indicate an error. Please refer to tuya_error_code.h for error information.

  • Remarks:

    1. From device power-on to calling tuya entrance, it is required to be within 200ms.
    2. 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.
    3. 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).
    4. 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/OutputParameter NameDescription
    [out]modeWiFi working mode
    ----------------
    WWM_LOWPOWERLow power (refers to turning off the WiFi module)
    WWM_SNIFFERmonitor
    WWM_STATIONstation
    WWM_SOFTAPsoftap
    WWM_STATIONAPstationap
  • Return Value:

    A return value of OPRT_OK indicates success, other return values indicate an error. Please refer to tuya_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/OutputParameter NameDescription
    [out]fast_ap_infoConnected AP information
  • Return Value:

    A return value of OPRT_OK indicates success, other return values indicate an error. Please refer to tuya_error_code.h for error information.

  • Remarks:

    1. fast_ap_info needs to dynamically allocate memory within this API
    2. 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/OutputParameter NameDescription
    [out]macMAC address
  • Return Value:

    A return value of OPRT_OK indicates success, other return values indicate an error. Please refer to tuya_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/OutputParameter NameDescription
    [in]ccodeCountry code
    ----------------
    COUNTRY_CODE_CNChina region 1-13
    COUNTRY_CODE_USUS region 1-11
    COUNTRY_CODE_JPJapan region 1-14
    COUNTRY_CODE_EUEurope 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 to tuya_error_code.h for error information.

  • Remarks:

    1. 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)
    2. 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/OutputParameter NameDescription
    [in]enableWhether to enable WiFi low power mode
    [in]dtimdtim parameter
  • Return Value:

    A return value of OPRT_OK indicates success, other return values indicate an error. Please refer to tuya_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/OutputParameter NameDescription
    [in]fast_ap_infoAP information required for fast connection
  • Return Value:

    A return value of OPRT_OK indicates success, other return values indicate an error. Please refer to tuya_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/OutputParameter NameDescription
    [in]ssidssid
    [in]passwdpasswd
  • Return Value: A return value of OPRT_OK indicates success, other return values indicate an error. Please refer to tuya_error_code.h for error information.

  • Remarks:

    1. 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.
    2. 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 to tuya_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/OutputParameter NameDescription
    [out]rssiSignal strength
  • Return Value:

    A return value of OPRT_OK indicates success, other return values indicate an error. Please refer to tuya_error_code.h for error information.

  • Remarks:

    1. Without moving the device and the router, the rssi obtained multiple times can only fluctuate slightly.
    2. 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/OutputParameter NameDescription
    [out]statConnection status
    ----------------
    WSS_IDLE
    WSS_CONNECTINGConnecting
    WSS_PASSWD_WRONGWrong password
    WSS_CONN_FAILConnection failed
    WSS_CONN_SUCCESSConnection successful
    WSS_GOT_IPDHCP successful
  • Return Value:

    A return value of OPRT_OK indicates success, other return values indicate an error. Please refer to tuya_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/OutputParameter NameDescription
    [in]bufManagement packet data buffer
    [in]lenLength of management packet data
  • Return Value:

    A return value of OPRT_OK indicates success, other return values indicate an error. Please refer to tuya_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/OutputParameter NameDescription
    [in]enableWhether to enable receiving management packet data
    [in]recv_cbCallback for receiving management packet data
  • Return Value:

    A return value of OPRT_OK indicates success, other return values indicate an error. Please refer to tuya_error_code.h for error information.