Files
STM32F4-Base/Drivers/BSP/NET/net_select.h
T
wandering1 bb76cfd779 修复规范矛盾并定案5项规则,保护宏统一XXX_H
规则定案:常量在右(a>5)、保护宏XXX_H(禁双下划线)、
头文件常量用#define、存储类前缀(g_/s_)优先、允许void返回值
全库32个.h头文件保护宏 __XXX_H → XXX_H 改名(字节级替换)
S1检查规则同步更新(拒绝双下划线),Inc/gpio.h 反向验证被正确拦截
规范示例矛盾修复:K&R括号、标准类型、变量初始化、命名常量、注释字段补全
2026-09-01 16:07:00 +08:00

115 lines
3.1 KiB
C
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/*
* 模块名称:Network Select API
* 模块功能:I/O 多路复用接口,支持 select 和 poll 模式,
* 用于同时监控多个 Socket 的读写事件
* 适用平台:STM32F4 系列(CH395F 以太网芯片)
* 作者:王建锋
* 创建日期:2026-07-18
* 修改记录:
*/
#ifndef NET_SELECT_H
#define NET_SELECT_H
#ifdef __cplusplus
extern "C" {
#endif
#include "net_types.h"
#include "net_config.h"
/*
* 文件描述符集合
* 每个位对应一个 socket 描述符 (0~7)
*/
typedef struct {
uint32_t fd_bits; /* 位掩码 */
} net_fd_set_t;
/*
* 时间结构体
*/
typedef struct {
long tv_sec; /* 秒 */
long tv_usec; /* 微秒 */
} net_timeval_t;
/*
* poll 事件结构体
*/
typedef struct {
int fd; /* socket 描述符 */
short events; /* 请求的事件 */
short revents; /* 实际发生的事件 */
} net_pollfd_t;
/*
* poll 事件掩码
*/
#define NET_POLLIN 0x0001 /* 可读 */
#define NET_POLLOUT 0x0004 /* 可写 */
#define NET_POLLERR 0x0008 /* 错误 */
#define NET_POLLHUP 0x0010 /* 挂起 */
#define NET_POLLNVAL 0x0020 /* 无效请求 */
/*
* fd_set 操作宏
*/
/*
* 函数功能:清空 fd_set
*/
#define NET_FD_ZERO(fdset) ((fdset)->fd_bits = 0)
/*
* 函数功能:将 fd 加入 fd_set
*/
#define NET_FD_SET(fd, fdset) ((fdset)->fd_bits |= (1U << (fd)))
/*
* 函数功能:将 fd 从 fd_set 移除
*/
#define NET_FD_CLR(fd, fdset) ((fdset)->fd_bits &= ~(1U << (fd)))
/*
* 函数功能:检查 fd 是否在 fd_set 中
*/
#define NET_FD_ISSET(fd, fdset) (((fdset)->fd_bits & (1U << (fd))) != 0)
/*
* I/O 多路复用 API
*/
/*
* 函数功能:I/O 多路复用(select 模式)
* 入口参数:nfds - 最大 fd + 1(通常为 NET_MAX_SOCKETS)
* readfds - 可读事件集合,NULL 表示不关心
* writefds - 可写事件集合,NULL 表示不关心
* exceptfds - 异常事件集合,NULL 表示不关心
* timeout - 超时时间,NULL 表示无限等待
* 返回值:就绪的 fd 数量,0=超时,-1=错误
* 限定条件:net_init() 已调用
* 函数说明:此函数会调用 net_poll() 轮询所有 Socket,
* 并检查哪些 Socket 有指定的事件发生。
*/
int net_select(int nfds, net_fd_set_t *readfds, net_fd_set_t *writefds,
net_fd_set_t *exceptfds, net_timeval_t *timeout);
/*
* 函数功能:I/O 多路复用(poll 模式)
* 入口参数:fds - net_pollfd_t 数组
* nfds - 数组元素数量
* timeout - 超时时间(毫秒),-1 表示无限等待
* 返回值:就绪的 fd 数量,0=超时,-1=错误
* 限定条件:net_init() 已调用
* 函数说明:与 select 类似,但使用 pollfd 结构体数组,
* 更灵活,支持更多事件类型。
*/
int net_poll_events(net_pollfd_t *fds, int nfds, int timeout);
#ifdef __cplusplus
}
#endif
#endif /* NET_SELECT_H */