Skip to main content

tkl_wifi | Wi-Fi Driver

tkl_wifi adapts a platform's Wi-Fi driver to TuyaOS. It covers station and soft-AP operation: initialization, scanning, connecting, channel and country-code control, MAC and IP management, sniffer and management-frame capture, low-power mode, and fast connect. The implementation lives in tkl_wifi.c, which TuyaOS generates with marked regions for your platform code.

Unless noted otherwise, every function returns OPRT_OK on success and another value on error; see tuya_error_code.h for the error codes.

Key typesโ€‹

The interface mode selects which logical Wi-Fi interface an API acts on:

WF_IF_EDescription
WF_STATIONStation (client) interface.
WF_APSoft-AP interface.

The work mode selects how the Wi-Fi subsystem runs:

WF_WK_MD_EDescription
WWM_POWERDOWNPowered down (Wi-Fi module off).
WWM_SNIFFERSniffer (monitor) mode.
WWM_STATIONStation mode.
WWM_SOFTAPSoft-AP mode.
WWM_STATIONAPConcurrent station and soft-AP.
WWM_UNKNOWNUnknown mode.

The station status reports the connection progress:

WF_STATION_STAT_EDescription
WSS_IDLENot connected.
WSS_CONNECTINGConnecting.
WSS_PASSWD_WRONGPassword does not match.
WSS_NO_AP_FOUNDAP not found.
WSS_CONN_FAILConnection failed.
WSS_CONN_SUCCESSConnected to the AP.
WSS_GOT_IPIP address acquired.
WSS_DHCP_FAILDHCP failed.

tkl_wifi_initโ€‹

OPERATE_RET tkl_wifi_init(WIFI_EVENT_CB cb);

Initializes the Wi-Fi subsystem and registers the event callback.

Parameters:

Input/OutputNameDescription
[in]cbThe Wi-Fi event callback, WIFI_EVENT_CB.

The callback has the following signature, where event is a WF_EVENT_E (WFE_CONNECTED, WFE_CONNECT_FAILED, WFE_DISCONNECTED):

typedef void (*WIFI_EVENT_CB)(WF_EVENT_E event, void *arg);

tkl_wifi_scan_apโ€‹

OPERATE_RET tkl_wifi_scan_ap(const int8_t *ssid, AP_IF_S **ap_ary, uint32_t *num);

Scans the environment and returns the APs found.

Parameters:

Input/OutputNameDescription
[in]ssidThe SSID to scan for. If NULL, scans all APs; otherwise scans the specified SSID.
[out]ap_aryThe array of scanned AP information, AP_IF_S.
[out]numThe number of APs in ap_ary.
note

This function allocates the result memory and is blocking. Release the result with tkl_wifi_release_ap when you no longer need it. It scans only the channels of the current country code.

tkl_wifi_release_apโ€‹

OPERATE_RET tkl_wifi_release_ap(AP_IF_S *ap);

Releases the AP information allocated by tkl_wifi_scan_ap.

Parameters:

Input/OutputNameDescription
[in]apThe AP information to release.

tkl_wifi_start_apโ€‹

OPERATE_RET tkl_wifi_start_ap(const WF_AP_CFG_IF_S *cfg);

Starts soft-AP mode with the given configuration.

Parameters:

Input/OutputNameDescription
[in]cfgThe soft-AP configuration, WF_AP_CFG_IF_S.

WF_AP_CFG_IF_S carries the SSID, password, channel, encryption mode (md, a WF_AP_AUTH_MODE_E), hidden flag, maximum connections (max_conn), broadcast interval, and the AP-mode IP information:

typedef struct {
uint8_t ssid[WIFI_SSID_LEN + 1]; // ssid
uint8_t s_len; // len of ssid
uint8_t passwd[WIFI_PASSWD_LEN + 1]; // passwd
uint8_t p_len; // len of passwd
uint8_t chan; // channel, default: 6
WF_AP_AUTH_MODE_E md; // encryption type
uint8_t ssid_hidden; // ssid hidden, default: 0
uint8_t max_conn; // max sta connect nums, default: 1
uint16_t ms_interval; // broadcast interval, default: 100
NW_IP_S ip; // ip info for ap mode
} WF_AP_CFG_IF_S;

tkl_wifi_stop_apโ€‹

OPERATE_RET tkl_wifi_stop_ap(void);

Stops soft-AP mode.

tkl_wifi_set_cur_channelโ€‹

OPERATE_RET tkl_wifi_set_cur_channel(const uint8_t chan);

Sets the working channel of the Wi-Fi interface.

Parameters:

Input/OutputNameDescription
[in]chanThe channel to set.
note

Setting a channel outside the current country-code channel range fails. Setting the channel from inside the sniffer callback is supported.

tkl_wifi_get_cur_channelโ€‹

OPERATE_RET tkl_wifi_get_cur_channel(uint8_t *chan);

Gets the current working channel.

Parameters:

Input/OutputNameDescription
[out]chanThe current channel.

tkl_wifi_set_snifferโ€‹

OPERATE_RET tkl_wifi_set_sniffer(const BOOL_T en, const SNIFFER_CALLBACK cb);

Enables or disables sniffer mode. While enabled, the driver delivers each captured frame to cb.

Parameters:

Input/OutputNameDescription
[in]enTRUE to enable sniffer mode, FALSE to disable it.
[in]cbThe capture callback, SNIFFER_CALLBACK.

The callback has the following signature:

typedef void (*SNIFFER_CALLBACK)(const uint8_t *buf, const uint16_t len, const int8_t rssi);

tkl_wifi_get_ipโ€‹

OPERATE_RET tkl_wifi_get_ip(const WF_IF_E wf, NW_IP_S *ip);

Gets the IPv4 address, gateway, and subnet mask of the given interface.

Parameters:

Input/OutputNameDescription
[in]wfThe Wi-Fi interface, WF_IF_E.
[out]ipThe IP information, NW_IP_S.
note

In station+AP mode the device has two IPs; pass wf to select which interface to read.

tkl_wifi_get_ipv6โ€‹

OPERATE_RET tkl_wifi_get_ipv6(const WF_IF_E wf, NW_IP_TYPE type, NW_IP_S *ip);

Gets an IPv6 address of the given interface.

Parameters:

Input/OutputNameDescription
[in]wfThe Wi-Fi interface, WF_IF_E.
[in]typeThe IPv6 address type to read, NW_IP_TYPE.
[out]ipThe IP information, NW_IP_S.

tkl_wifi_set_ipโ€‹

OPERATE_RET tkl_wifi_set_ip(const WF_IF_E wf, NW_IP_S *ip);

Sets a static IPv4 address, gateway, and subnet mask on the given interface.

Parameters:

Input/OutputNameDescription
[in]wfThe Wi-Fi interface, WF_IF_E.
[in]ipThe IP information to set, NW_IP_S.

tkl_wifi_set_macโ€‹

OPERATE_RET tkl_wifi_set_mac(const WF_IF_E wf, const NW_MAC_S *mac);

Sets the MAC address of the given interface.

Parameters:

Input/OutputNameDescription
[in]wfThe Wi-Fi interface, WF_IF_E.
[in]macThe MAC address to set, NW_MAC_S.

tkl_wifi_get_macโ€‹

OPERATE_RET tkl_wifi_get_mac(const WF_IF_E wf, NW_MAC_S *mac);

Gets the MAC address of the given interface.

Parameters:

Input/OutputNameDescription
[in]wfThe Wi-Fi interface, WF_IF_E.
[out]macThe MAC address, NW_MAC_S.

tkl_wifi_set_work_modeโ€‹

OPERATE_RET tkl_wifi_set_work_mode(const WF_WK_MD_E mode);

Sets the Wi-Fi work mode.

Parameters:

Input/OutputNameDescription
[in]modeThe work mode, WF_WK_MD_E.

tkl_wifi_get_work_modeโ€‹

OPERATE_RET tkl_wifi_get_work_mode(WF_WK_MD_E *mode);

Gets the current Wi-Fi work mode.

Parameters:

Input/OutputNameDescription
[out]modeThe work mode, WF_WK_MD_E.

tkl_wifi_get_connected_ap_infoโ€‹

OPERATE_RET tkl_wifi_get_connected_ap_info(FAST_WF_CONNECTED_AP_INFO_T **fast_ap_info);

Gets the information of the currently connected AP, for use with the fast-connect feature.

Parameters:

Input/OutputNameDescription
[out]fast_ap_infoThe connected-AP information, FAST_WF_CONNECTED_AP_INFO_T.
note

This function allocates fast_ap_info. Pair it with tkl_wifi_station_fast_connect to speed up reconnection after a restart.

tkl_wifi_get_bssidโ€‹

OPERATE_RET tkl_wifi_get_bssid(uint8_t *mac);

Gets the BSSID (MAC address) of the connected AP.

Parameters:

Input/OutputNameDescription
[out]macA 6-byte buffer receiving the BSSID.

tkl_wifi_set_country_codeโ€‹

OPERATE_RET tkl_wifi_set_country_code(const COUNTRY_CODE_E ccode);

Sets the regulatory country code, which determines the allowed channel list.

Parameters:

Input/OutputNameDescription
[in]ccodeThe country code, COUNTRY_CODE_E.
COUNTRY_CODE_ERegionChannels
COUNTRY_CODE_CNChina1โ€“13
COUNTRY_CODE_USUnited States1โ€“11
COUNTRY_CODE_JPJapan1โ€“14
COUNTRY_CODE_EUEurope1โ€“13

tkl_wifi_set_rf_calibratedโ€‹

OPERATE_RET tkl_wifi_set_rf_calibrated(void);

Performs Wi-Fi RF calibration. Called during Wi-Fi production testing.

Return value:

OPRT_OK on success. Other values indicate an error; see tuya_error_code.h.

tkl_wifi_set_lp_modeโ€‹

OPERATE_RET tkl_wifi_set_lp_mode(const BOOL_T enable, const uint8_t dtim);

Enables or disables Wi-Fi low-power mode.

Parameters:

Input/OutputNameDescription
[in]enableTRUE to enable low-power mode, FALSE to disable it.
[in]dtimThe DTIM interval.

tkl_wifi_station_fast_connectโ€‹

OPERATE_RET tkl_wifi_station_fast_connect(const FAST_WF_CONNECTED_AP_INFO_T *fast_ap_info);

Connects to a router using cached AP information for a faster connection.

Parameters:

Input/OutputNameDescription
[in]fast_ap_infoThe cached AP information from tkl_wifi_get_connected_ap_info.
note

Used for the first connection after pairing and a device restart.

tkl_wifi_station_connectโ€‹

OPERATE_RET tkl_wifi_station_connect(const int8_t *ssid, const int8_t *passwd);

Connects the station to a router.

Parameters:

Input/OutputNameDescription
[in]ssidThe SSID to connect to.
[in]passwdThe password.
note

Non-blocking. After the connection starts, poll tkl_wifi_station_get_status to follow progress.

tkl_wifi_station_disconnectโ€‹

OPERATE_RET tkl_wifi_station_disconnect(void);

Disconnects the station from the router.

tkl_wifi_station_get_conn_ap_rssiโ€‹

OPERATE_RET tkl_wifi_station_get_conn_ap_rssi(int8_t *rssi);

Gets the signal strength (RSSI) of the connected AP.

Parameters:

Input/OutputNameDescription
[out]rssiThe RSSI value.

tkl_wifi_station_get_statusโ€‹

OPERATE_RET tkl_wifi_station_get_status(WF_STATION_STAT_E *stat);

Gets the current station connection status.

Parameters:

Input/OutputNameDescription
[out]statThe station status, WF_STATION_STAT_E.

tkl_wifi_send_mgntโ€‹

OPERATE_RET tkl_wifi_send_mgnt(const uint8_t *buf, const uint32_t len);

Sends a Wi-Fi management frame.

Parameters:

Input/OutputNameDescription
[in]bufThe management-frame buffer.
[in]lenThe buffer length.

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);

Enables or disables delivery of received management frames to a callback.

Parameters:

Input/OutputNameDescription
[in]enableTRUE to enable management-frame reception, FALSE to disable it.
[in]recv_cbThe receive callback, WIFI_REV_MGNT_CB.

The callback has the following signature:

typedef void (*WIFI_REV_MGNT_CB)(uint8_t *buf, uint32_t len);

tkl_wifi_ioctlโ€‹

OPERATE_RET tkl_wifi_ioctl(WF_IOCTL_CMD_E cmd, void *args);

Issues a driver-specific control command.

Parameters:

Input/OutputNameDescription
[in]cmdThe command, WF_IOCTL_CMD_E.
[in]argsThe arguments associated with the command.
WF_IOCTL_CMD_EDescription
WFI_BEACON_CMDConfigure beacon vendor-specific information element.
WFI_GET_LAST_DISCONN_REASONGet the last disconnect reason, WF_DISCONN_REASON_E.
WFI_AP_GET_STALIST_CMDGet the soft-AP station list, WF_STA_LIST_S.
WFI_CONNECT_CMDConnect using WF_IOCTL_CONN_T.