新增代码规范检查钩子(规范V1.0 可机械校验项)并修复存量违规

新增 scripts/check_coding_standard.py + coding-standard 钩子(S1~S9:
头文件保护宏、文件头注释、宏大写、_t 后缀、switch default、
禁 strcpy/sprintf/goto/Tab、函数注释)
修复存量违规:NET_* 字节序宏大写化、net_fd_set 等类型加 _t 后缀、
net_inet_ntoa 的 sprintf 改 snprintf、补 4 处函数注释与 ui_task.c 文件头
docs/代码规范/pre-commit使用说明.md:新增 2.8 节,修正失效文档路径
This commit is contained in:
2026-09-01 15:41:25 +08:00
parent 07afb7b468
commit 2b66f96d01
12 changed files with 278 additions and 72 deletions
+12 -12
View File
@@ -35,11 +35,11 @@ static int check_socket_error(int sockfd);
/*
* 函数功能:I/O 多路复用(select 模式),等待多个 Socket 的读写事件就绪
* 入口参数:nfds - 最大文件描述符数 +1 int > 0
* readfds - 读事件集合指针 net_fd_set *(可为 NULL)
* writefds - 写事件集合指针 net_fd_set *(可为 NULL)
* exceptfds- 异常事件集合指针 net_fd_set *(可为 NULL)
* timeout - 超时时间结构体指针 net_timeval *(NULL=无限等待)
* 出口参数:readfds/writefds/exceptfds - 仅保留就绪的 Socket 位 net_fd_set *
* readfds - 读事件集合指针 net_fd_set_t *(可为 NULL)
* writefds - 写事件集合指针 net_fd_set_t *(可为 NULL)
* exceptfds- 异常事件集合指针 net_fd_set_t *(可为 NULL)
* timeout - 超时时间结构体指针 net_timeval_t *(NULL=无限等待)
* 出口参数:readfds/writefds/exceptfds - 仅保留就绪的 Socket 位 net_fd_set_t *
* 返回值:就绪的 Socket 数量(>= 0);-1 表示参数无效 int
* 限定条件:net_init() + MX_FREERTOS_Init() 已执行;可在任意任务上下文调用
* 函数说明:1. 轮询 Socket 控制块(RAM)中的读/写/异常状态,判断事件是否就绪
@@ -49,13 +49,13 @@ static int check_socket_error(int sockfd);
* 3. 阻塞等待直到有事件就绪或超时
* 4. 通过 osDelay(1) 让出 CPU,平衡实时性与 CPU 利用率(状态刷新延迟 <=10ms)
*/
int net_select(int nfds, net_fd_set *readfds, net_fd_set *writefds,
net_fd_set *exceptfds, net_timeval *timeout) {
int net_select(int nfds, net_fd_set_t *readfds, net_fd_set_t *writefds,
net_fd_set_t *exceptfds, net_timeval_t *timeout) {
uint32_t start_tick;
uint32_t timeout_ms = 0;
net_fd_set result_read = {0};
net_fd_set result_write = {0};
net_fd_set result_except = {0};
net_fd_set_t result_read = {0};
net_fd_set_t result_write = {0};
net_fd_set_t result_except = {0};
int ready_count = 0;
int i;
@@ -142,7 +142,7 @@ int net_select(int nfds, net_fd_set *readfds, net_fd_set *writefds,
/*
* 函数功能:I/O 多路复用(poll 模式),等待多个 Socket 的读写事件就绪
* 入口参数:fds - pollfd 数组指针 net_pollfd *
* 入口参数:fds - pollfd 数组指针 net_pollfd_t *
* nfds - 数组元素数量 int > 0
* timeout - 超时时间(毫秒) int >= 0(-1=无限等待,但本实现按 >=0 处理)
* 出口参数:fds[i].revents - 填充实际发生的事件掩码 short
@@ -153,7 +153,7 @@ int net_select(int nfds, net_fd_set *readfds, net_fd_set *writefds,
* 3. 阻塞等待直到有事件就绪或超时
* 4. 通过 osDelay(1) 让出 CPU,平衡实时性与 CPU 利用率(状态刷新延迟 <=10ms)
*/
int net_poll_events(net_pollfd *fds, int nfds, int timeout) {
int net_poll_events(net_pollfd_t *fds, int nfds, int timeout) {
uint32_t start_tick;
int ready = 0;
int i;
+7 -7
View File
@@ -24,7 +24,7 @@ extern "C" {
*/
typedef struct {
uint32_t fd_bits; /* 位掩码 */
} net_fd_set;
} net_fd_set_t;
/*
* 时间结构体
@@ -32,7 +32,7 @@ typedef struct {
typedef struct {
long tv_sec; /* 秒 */
long tv_usec; /* 微秒 */
} net_timeval;
} net_timeval_t;
/*
* poll 事件结构体
@@ -41,7 +41,7 @@ typedef struct {
int fd; /* socket 描述符 */
short events; /* 请求的事件 */
short revents; /* 实际发生的事件 */
} net_pollfd;
} net_pollfd_t;
/*
* poll 事件掩码
@@ -92,12 +92,12 @@ typedef struct {
* 函数说明:此函数会调用 net_poll() 轮询所有 Socket,
* 并检查哪些 Socket 有指定的事件发生。
*/
int net_select(int nfds, net_fd_set *readfds, net_fd_set *writefds,
net_fd_set *exceptfds, net_timeval *timeout);
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 数组
* 入口参数:fds - net_pollfd_t 数组
* nfds - 数组元素数量
* timeout - 超时时间(毫秒),-1 表示无限等待
* 返回值:就绪的 fd 数量,0=超时,-1=错误
@@ -105,7 +105,7 @@ int net_select(int nfds, net_fd_set *readfds, net_fd_set *writefds,
* 函数说明:与 select 类似,但使用 pollfd 结构体数组,
* 更灵活,支持更多事件类型。
*/
int net_poll_events(net_pollfd *fds, int nfds, int timeout);
int net_poll_events(net_pollfd_t *fds, int nfds, int timeout);
#ifdef __cplusplus
}
+23 -13
View File
@@ -1049,6 +1049,15 @@ static int net_relisten_locked(net_sock_t *p_sock) {
return 0;
}
/*
* 函数功能:关闭 Socket(内部版本,不经过消息队列)
* 入口参数:p_sock - 要关闭的 Socket 控制块指针 net_sock_t *
* 出口参数:无
* 返回值:0 - 关闭成功 int
* 限定条件:必须在 netTask 上下文中调用;p_sock 有效
* 函数说明:1. 关闭 CH395F Socket,排空上一条连接残留的 DISCONNECT 中断
* 2. 等待芯片回到 CLOSED 状态(超时 500ms),再清零控制块
*/
static int net_close_locked(net_sock_t *p_sock) {
int sockfd = (int)(p_sock - s_net_socks);
uint32_t tick = HAL_GetTick();
@@ -1515,7 +1524,7 @@ int net_get_errno(void) {
* 返回值:网络字节序的 32 位 IP 地址(0 表示参数无效) uint32_t
* 限定条件:无
* 函数说明:1. 逐段解析十进制数与并小端组装为 32 位值
* 2. 转换结果经 NET_htonl 转为网络字节序
* 2. 转换结果经 NET_HTONL 转为网络字节序
*/
uint32_t net_inet_addr(const char *cp) {
uint32_t addr = 0;
@@ -1537,7 +1546,7 @@ uint32_t net_inet_addr(const char *cp) {
}
}
return NET_htonl(addr);
return NET_HTONL(addr);
}
/*
@@ -1547,16 +1556,16 @@ uint32_t net_inet_addr(const char *cp) {
* 出口参数:buf - 填充点分十进制字符串
* 返回值:成功返回 buf 指针;buf 为 NULL 时返回 NULL char *
* 限定条件:buf 非空且长度足够(>= 16 字节)
* 函数说明:1. 先经 NET_ntohl 转为主机字节序再格式化输出
* 函数说明:1. 先经 NET_NTOHL 转为主机字节序再格式化输出
*/
char *net_inet_ntoa(uint32_t addr, char *buf) {
uint32_t naddr = NET_ntohl(addr);
uint32_t naddr = NET_NTOHL(addr);
if (NULL == buf) {
return NULL;
}
sprintf(buf, "%lu.%lu.%lu.%lu",
snprintf(buf, 16, "%lu.%lu.%lu.%lu",
(unsigned long)(naddr & 0xFF),
(unsigned long)((naddr >> 8) & 0xFF),
(unsigned long)((naddr >> 16) & 0xFF),
@@ -1572,10 +1581,10 @@ char *net_inet_ntoa(uint32_t addr, char *buf) {
* 出口参数:无
* 返回值:网络字节序的 16 位端口 uint16_t
* 限定条件:无
* 函数说明:直接调用 NET_htons 完成转换
* 函数说明:直接调用 NET_HTONS 完成转换
*/
uint16_t net_htons(uint16_t hostshort) {
return NET_htons(hostshort);
return NET_HTONS(hostshort);
}
@@ -1585,10 +1594,10 @@ uint16_t net_htons(uint16_t hostshort) {
* 出口参数:无
* 返回值:主机字节序的 16 位端口 uint16_t
* 限定条件:无
* 函数说明:直接调用 NET_ntohs 完成转换
* 函数说明:直接调用 NET_NTOHS 完成转换
*/
uint16_t net_ntohs(uint16_t netshort) {
return NET_ntohs(netshort);
return NET_NTOHS(netshort);
}
/*
@@ -1597,10 +1606,10 @@ uint16_t net_ntohs(uint16_t netshort) {
* 出口参数:无
* 返回值:网络字节序的 32 位 IP 地址 uint32_t
* 限定条件:无
* 函数说明:直接调用 NET_htonl 完成转换
* 函数说明:直接调用 NET_HTONL 完成转换
*/
uint32_t net_htonl(uint32_t hostlong) {
return NET_htonl(hostlong);
return NET_HTONL(hostlong);
}
/*
@@ -1609,10 +1618,10 @@ uint32_t net_htonl(uint32_t hostlong) {
* 出口参数:无
* 返回值:主机字节序的 32 位 IP 地址 uint32_t
* 限定条件:无
* 函数说明:直接调用 NET_ntohl 完成转换
* 函数说明:直接调用 NET_NTOHL 完成转换
*/
uint32_t net_ntohl(uint32_t netlong) {
return NET_ntohl(netlong);
return NET_NTOHL(netlong);
}
/*
@@ -1637,6 +1646,7 @@ static int alloc_socket(void) {
}
/*
* 函数功能:处理 CH395F CONNECT 中断事件(单连接到达 / Client 连接成功)
* 入口参数:p_sock - 发生连接事件的 Socket 控制块指针 net_sock_t *
* 出口参数:无
* 返回值:无
+8 -8
View File
@@ -53,18 +53,18 @@ extern "C" {
* 网络字节序转换宏
*/
#ifdef __BIG_ENDIAN
#define NET_htonl(x) (x)
#define NET_htons(x) (x)
#define NET_ntohl(x) (x)
#define NET_ntohs(x) (x)
#define NET_HTONL(x) (x)
#define NET_HTONS(x) (x)
#define NET_NTOHL(x) (x)
#define NET_NTOHS(x) (x)
#else
#define NET_htonl(x) (((uint32_t)(x) << 24) | \
#define NET_HTONL(x) (((uint32_t)(x) << 24) | \
(((uint32_t)(x) & 0xFF00) << 8) | \
(((uint32_t)(x) & 0xFF0000) >> 8) | \
((uint32_t)(x) >> 24))
#define NET_htons(x) (((uint16_t)(x) << 8) | ((uint16_t)(x) >> 8))
#define NET_ntohl(x) NET_htonl(x)
#define NET_ntohs(x) NET_htons(x)
#define NET_HTONS(x) (((uint16_t)(x) << 8) | ((uint16_t)(x) >> 8))
#define NET_NTOHL(x) NET_HTONL(x)
#define NET_NTOHS(x) NET_HTONS(x)
#endif
/*