tkl_network | Networking APIs
The tkl_network.c
file provides a series of APIs required for network communication processes to adapt to different network interfaces. These APIs include creating sockets, connecting, binding, listening, sending data, receiving data, setting and getting socket options, and other network operations. The file defines common handling of network APIs and allows for cross-platform network communication through these encapsulated functions.
API Descriptionâ
tkl_net_get_errnoâ
TUYA_ERRNO tkl_net_get_errno(void);
Functionâ
Retrieves the network error code.
Parametersâ
No parameters.
Return Valueâ
Returns 0 on success, otherwise returns an error code specific to the target system.
tkl_net_fd_setâ
OPERATE_RET tkl_net_fd_set(const int fd, TUYA_FD_SET_T* fds);
Functionâ
Adds a file descriptor to the set.
Parametersâ
fd
: The file descriptor to be added.fds
: Pointer to the file descriptor set.
Return Valueâ
Returns OPRT_OK
on success, otherwise refer to error codes in tuya_error_code.h
.
tkl_net_fd_clearâ
OPERATE_RET tkl_net_fd_clear(const int fd, TUYA_FD_SET_T* fds);
Functionâ
Removes a file descriptor from the set.
Parametersâ
fd
: The file descriptor to be cleared.fds
: Pointer to the file descriptor set.
Return Valueâ
Returns OPRT_OK
on success, otherwise refer to error codes in tuya_error_code.h
.
tkl_net_fd_issetâ
OPERATE_RET tkl_net_fd_isset(const int fd, TUYA_FD_SET_T* fds);
Functionâ
Checks if a file descriptor is in the set.
Parametersâ
fd
: The file descriptor to be checked.fds
: Pointer to the file descriptor set.
Return Valueâ
Returns TRUE
if the file descriptor is in the set, otherwise returns FALSE
.
tkl_net_fd_zeroâ
OPERATE_RET tkl_net_fd_zero(TUYA_FD_SET_T* fds);
Functionâ
Clears all file descriptors in the file descriptor set.
Parametersâ
fds
: Pointer to the file descriptor set.
Return Valueâ
Returns OPRT_OK
on success, otherwise refer to error codes in tuya_error_code.h
.
tkl_net_selectâ
int tkl_net_select(const int maxfd, TUYA_FD_SET_T *readfds, TUYA_FD_SET_T *writefds, TUYA_FD_SET_T *errorfds, const uint32_t ms_timeout);
Functionâ
Retrieves the set of available file descriptors.
Parametersâ
maxfd
: One more than the maximum number of file descriptors to be checked.readfds
: Pointer to the set of readable file descriptors.writefds
: Pointer to the set of writable file descriptors.errorfds
: Pointer to the set of erroneous file descriptors.ms_timeout
: Timeout period in milliseconds.
Return Valueâ
Returns the number of available file descriptors; returns a value less than or equal to 0 on error.
tkl_net_get_nonblockâ
int tkl_net_get_nonblock(const int fd);
Functionâ
Retrieves a non-blocking file descriptor.
Parametersâ
fd
: The file descriptor.
Return Valueâ
Returns the number of non-blocking file descriptors on success; returns a value less than or equal to 0 on error.
tkl_net_set_blockâ
OPERATE_RET tkl_net_set_block(const int fd, const BOOL_T block);
Functionâ
Sets the blocking flag for a file descriptor.
Parametersâ
fd
: The file descriptor.block
: IfTRUE
, sets to blocking mode; ifFALSE
, sets to non-blocking mode.
Return Valueâ
Returns OPRT_OK
on success, otherwise refer to error codes in tuya_error_code.h
.
tkl_net_closeâ
TUYA_ERRNO tkl_net_close(const int fd);
Functionâ
Closes a file descriptor.
Parametersâ
fd
: The file descriptor.
Return Valueâ
Returns 0 on success, otherwise returns an error code specific to the target system.
tkl_net_shutdownâ
TUYA_ERRNO tkl_net_shutdown(const int fd, const int how);
Functionâ
Shuts down read/write operations on a file descriptor.
Parametersâ
fd
: The file descriptor.how
: The type of shutdown.
Return Valueâ
Returns 0 on success, otherwise returns an error code specific to the target system.
tkl_net_socket_createâ
int tkl_net_socket_create(const TUYA_PROTOCOL_TYPE_E type);
Functionâ
Creates a TCP or UDP socket.
Parametersâ
type
: The protocol type, either TCP or UDP.
Return Valueâ
Returns the file descriptor.
tkl_net_connectâ
TUYA_ERRNO tkl_net_connect(const int fd, const TUYA_IP_ADDR_T addr, const uint16_t port);
Functionâ
Connects to a network.
Parametersâ
fd
: The file descriptor.addr
: Server address information.port
: Server port information.
Return Valueâ
Returns 0 on success, otherwise returns an error code specific to the target system.
tkl_net_connect_rawâ
TUYA_ERRNO tkl_net_connect_raw(const int fd, void *p_socket_addr, const int len);
Functionâ
Connects to a network using raw data.
Parametersâ
fd
: The file descriptor.p_socket_addr
: Raw socket data.len
: Length of the data.
Return Valueâ
Returns 0 on success, otherwise returns an error code specific to the target system.
tkl_net_bindâ
TUYA_ERRNO tkl_net_bind(const int fd, const TUYA_IP_ADDR_T addr, const uint16_t port);
Functionâ
Binds a socket to a network.
Parametersâ
fd
: The file descriptor.addr
: Server address information.port
: Server port information.
Return Valueâ
Returns 0 on success, otherwise returns an error code specific to the target system.
tkl_net_listenâ
TUYA_ERRNO tkl_net_listen(const int fd, const int backlog);
Functionâ
Listens for network connection requests.
Parametersâ
fd
: File descriptor.backlog
: The maximum number of pending connections the queue can hold.
Return Valueâ
Returns 0 on success, otherwise returns an error code specific to the target system.
tkl_net_acceptâ
TUYA_ERRNO tkl_net_accept(const int fd, TUYA_IP_ADDR_T *addr, uint16_t *port);
Functionâ
Accepts a network connection request.
Parametersâ
fd
: File descriptor.addr
: The IP address of the receiver.port
: The port number of the receiver.
Return Valueâ
Returns 0 on success, otherwise returns an error code specific to the target system.
tkl_net_sendâ
TUYA_ERRNO tkl_net_send(const int fd, const void *buf, const uint32_t nbytes);
Functionâ
Sends data over the network.
Parametersâ
fd
: File descriptor.buf
: The buffer containing the data to be sent.nbytes
: The length of the buffer.
Return Valueâ
The number of bytes sent; returns a negative value on error.
tkl_net_send_toâ
TUYA_ERRNO tkl_net_send_to(const int fd, const void *buf, const uint32_t nbytes, const TUYA_IP_ADDR_T addr, const uint16_t port);
Functionâ
Sends data to a specified server.
Parametersâ
fd
: File descriptor.buf
: The buffer containing the data to be sent.nbytes
: The length of the buffer.addr
: Server address information.port
: Server port information.
Return Valueâ
The number of bytes sent; returns a negative value on error.
tkl_net_recvâ
TUYA_ERRNO tkl_net_recv(const int fd, void *buf, const uint32_t nbytes);
Functionâ
Receives data from the network.
Parametersâ
fd
: File descriptor.buf
: The buffer to store the received data.nbytes
: The length of the buffer.
Return Valueâ
The number of bytes received; returns a negative value on error.
tkl_net_recv_nd_sizeâ
int tkl_net_recv_nd_size(const int fd, void *buf, const uint32_t buf_size, const uint32_t nd_size);
Functionâ
Receives data of a specified size from the network.
Parametersâ
fd
: File descriptor.buf
: The buffer to store the received data.buf_size
: The size of the buffer.nd_size
: The expected size of the data to be received.
Return Valueâ
The actual amount of data received on success, or a negative value on error.
tkl_net_recvfromâ
TUYA_ERRNO tkl_net_recvfrom(const int fd, void *buf, const uint32_t nbytes, TUYA_IP_ADDR_T *addr, uint16_t *port);
Functionâ
Receives data from a specified server.
Parametersâ
fd
: File descriptor.buf
: The buffer to store the received data.nbytes
: The length of the buffer.addr
[OUT]: Server address information.port
[OUT]: Server port information.
Return Valueâ
The number of bytes received; returns a negative value on error.
tkl_net_gethostbynameâ
OPERATE_RET tkl_net_gethostbyname(const char *domain, TUYA_IP_ADDR_T *addr);
Functionâ
Obtains address information by domain name.
Parametersâ
domain
: Domain name information.addr
: Pointer to store the address information.
Return Valueâ
Returns OPRT_OK
on success, otherwise refer to error codes in tuya_error_code.h
.
tkl_net_socket_bindâ
OPERATE_RET tkl_net_socket_bind(const int fd, const char *ip);
Functionâ
Binds the socket to a network with the specified IP.
Parametersâ
fd
: File descriptor.ip
: IP address.
Return Valueâ
Returns OPRT_OK
on success, otherwise refer to error codes in tuya_error_code.h
.
tkl_net_set_cloexecâ
OPERATE_RET tkl_net_set_cloexec(const int fd);
Functionâ
Sets the socket to remain open in child processes after a fork
call.
Parametersâ
fd
: File descriptor.
Return Valueâ
Returns OPRT_OK
on success, otherwise refer to error codes in tuya_error_code.h
.
tkl_net_get_socket_ipâ
OPERATE_RET tkl_net_get_socket_ip(const int fd, TUYA_IP_ADDR_T *addr);
Functionâ
Obtains the IP address via the socket descriptor.
Parametersâ
fd
: File descriptor.addr
: Pointer to store the IP address.
Return Valueâ
Returns OPRT_OK
on success, otherwise refer to error codes in tuya_error_code.h
.
tkl_net_str2addrâ
TUYA_IP_ADDR_T tkl_net_str2addr(const char *ip_str);
Functionâ
Converts an IP string to an address.
Parametersâ
ip_str
: IP string.
Return Valueâ
Returns the converted IP address.
tkl_net_setsockoptâ
OPERATE_RET tkl_net_setsockopt(const int fd, const TUYA_OPT_LEVEL level, const TUYA_OPT_NAME optname, const void *optval, const int optlen);
Functionâ
Sets socket options.
Parametersâ
fd
: File descriptor.level
: The level at which the option is defined.optname
: The option name.optval
: A pointer to the option value.optlen
: The length of the option value.
Return Valueâ
Returns OPRT_OK
on success, otherwise refer to error codes in tuya_error_code.h
.
tkl_net_getsockoptâ
OPERATE_RET tkl_net_getsockopt(const int fd, const TUYA_OPT_LEVEL level, const TUYA_OPT_NAME optname, void *optval, int *optlen);
Functionâ
Gets the socket option value.
Parametersâ
fd
: File descriptor.level
: The level at which the option is defined.optname
: The option name.optval
: A pointer to where the option value will be stored.optlen
: A pointer to where the length of the option value will be stored.
Return Valueâ
Returns OPRT_OK
on success, otherwise refer to error codes in tuya_error_code.h
.
tkl_net_set_timeoutâ
OPERATE_RET tkl_net_set_timeout(const int fd, const int ms_timeout, const TUYA_TRANS_TYPE_E type);
Functionâ
Sets the socket timeout option.
Parametersâ
fd
: File descriptor.ms_timeout
: Timeout period in milliseconds.type
: Type of transmission, either receiving or sending.
Return Valueâ
Returns OPRT_OK
on success, otherwise refer to error codes in tuya_error_code.h
.
tkl_net_set_bufsizeâ
OPERATE_RET tkl_net_set_bufsize(const int fd, const int buf_size, const TUYA_TRANS_TYPE_E type);
Functionâ
Sets the socket buffer size option.
Parametersâ
fd
: File descriptor.buf_size
: Buffer size in bytes.type
: Type of transmission, either receiving or sending.
Return Valueâ
Returns OPRT_OK
on success, otherwise refer to error codes in tuya_error_code.h
.
tkl_net_set_reuseâ
OPERATE_RET tkl_net_set_reuse(const int fd);
Functionâ
Enables the socket reuse option.
Parametersâ
fd
: File descriptor.
Return Valueâ
Returns OPRT_OK
on success, otherwise refer to error codes in tuya_error_code.h
.
tkl_net_disable_nagleâ
OPERATE_RET tkl_net_disable_nagle(const int fd);
Functionâ
Disables the Nagle algorithm for the socket.
Parametersâ
fd
: File descriptor.
Return Valueâ
Returns OPRT_OK
on success, otherwise refer to error codes in tuya_error_code.h
.
tkl_net_set_broadcastâ
OPERATE_RET tkl_net_set_broadcast(const int fd);
Functionâ
Enables the socket broadcast option.
Parametersâ
fd
: File descriptor.
Return Valueâ
Returns OPRT_OK
on success, otherwise refer to error codes in tuya_error_code.h
.
tkl_net_set_keepaliveâ
OPERATE_RET tkl_net_set_keepalive(int fd, const BOOL_T alive, const uint32_t idle, const uint32_t intr, const uint32_t cnt);
Functionâ
Sets the keepalive
option for the socket to monitor the connection.
Parametersâ
fd
: File descriptor.alive
:keepalive
option, enable or disable.idle
:keep idle
option, idle time (seconds); if there is no data exchange in this time, the probe starts.intr
:keep interval
option, probe interval.cnt
:keep count
option, number of probes.
Return Valueâ
Returns OPRT_OK
on success, otherwise refer to error codes in tuya_error_code.h
.
tkl_net_getsocknameâ
OPERATE_RET tkl_net_getsockname(int fd, TUYA_IP_ADDR_T *addr, uint16_t *port);
Functionâ
Gets information about a specific socket.
Parametersâ
fd
: File descriptor.addr
: Source IP address.port
: Source port.
Return Valueâ
Returns OPRT_OK
on success, otherwise refer to error codes in tuya_error_code.h
.
tkl_net_getpeernameâ
OPERATE_RET tkl_net_getpeername(int fd, TUYA_IP_ADDR_T *addr, uint16_t *port);
Functionâ
Gets the destination information of a specific socket.
Parametersâ
fd
: File descriptor.addr
: Destination IP address.port
: Destination port.
Return Valueâ
Returns OPRT_OK
on success, otherwise refer to error codes in tuya_error_code.h
.
tkl_net_sethostnameâ
OPERATE_RET tkl_net_sethostname(const char *hostname);
Functionâ
Sets the system hostname, which will be displayed on the router.
Parametersâ
hostname
: Hostname.
Return Valueâ
Returns OPRT_OK
on success, otherwise refer to error codes in tuya_error_code.h
.