Skip to main content

TAL Network API Reference

Overview

tal_network.h wraps TCP and UDP sockets, select, and common socket options behind one portable API. It follows BSD-style flows: create a socket, connect or bind, listen, accept, then send and recv. UDP uses send_to and recvfrom. Helpers convert between strings and TUYA_IP_ADDR_T host-order IPv4.

Source: TuyaOpen/src/tal_network/include/tal_network.h

Audience: Developers implementing HTTP clients, custom TCP or UDP protocols, or debugging connectivity with tal_wifi.

IP address constants

MacroMeaning
TY_IPADDR_LOOPBACK127.0.0.1
TY_IPADDR_ANY0.0.0.0
TY_IPADDR_BROADCAST255.255.255.255

Errors and multiplexing

FunctionDescription
tal_net_get_errnoLast network error as TUYA_ERRNO.
tal_net_selectWait on read, write, except sets. Timeout in ms.
tal_net_fd_set, tal_net_fd_clear, tal_net_fd_isset, tal_net_fd_zeroFD set helpers. Macros: TAL_FD_SET, TAL_FD_CLR, TAL_FD_ISSET, TAL_FD_ZERO.
tal_net_get_nonblockQuery non-blocking mode.
tal_net_set_blockSet blocking or non-blocking.
tal_net_closeClose socket fd.

Socket lifecycle

FunctionDescription
tal_net_socket_createCreate TCP or UDP (TUYA_PROTOCOL_TYPE_E). Returns fd.
tal_net_connectTCP connect to address and port.
tal_net_connect_rawConnect using raw socket address buffer.
tal_net_bindBind local address and port.
tal_net_listenListen with backlog.
tal_net_acceptAccept; optional peer address and port out.

Data transfer

FunctionDescription
tal_net_send, tal_net_recvStream I/O.
tal_net_send_to, tal_net_recvfromUDP with peer address.
tal_net_recv_nd_sizeRecv until nd_size bytes or error.

Options and DNS

FunctionDescription
tal_net_set_timeoutSend or recv timeout (TUYA_TRANS_TYPE_E).
tal_net_set_bufsizeBuffer sizes.
tal_net_set_reuseSO_REUSEADDR style reuse.
tal_net_disable_nagleTCP_NODELAY.
tal_net_set_broadcastUDP broadcast.
tal_net_set_keepaliveTCP keepalive.
tal_net_gethostbynameResolve name to TUYA_IP_ADDR_T.
tal_net_get_socket_ipLocal bound address.
tal_net_str2addr, tal_net_addr2strString and address conversion.
tal_net_setsockopt, tal_net_getsockoptRaw options (TUYA_OPT_LEVEL, TUYA_OPT_NAME).

Typical TCP client

  1. Bring Wi-Fi up (Wi-Fi Station tutorial).
  2. Create socket with tal_net_socket_create using the TCP enum from tuya_cloud_types.h.
  3. Optionally use non-blocking mode and tal_net_select for timeouts.
  4. Resolve host with tal_net_gethostbyname, then tal_net_connect.
  5. Loop on tal_net_send and tal_net_recv.
  6. tal_net_close.

Platform notes

Return types mix OPERATE_RET, TUYA_ERRNO, and byte counts. Read each function comment in the header. The adapter maps to the port stack (for example lwIP).

References