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:keepaliveoption, enable or disable.idle:keep idleoption, idle time (seconds); if there is no data exchange in this time, the probe starts.intr:keep intervaloption, probe interval.cnt:keep countoption, 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.