增加网络配置界面

This commit is contained in:
2026-09-10 11:36:33 +08:00
parent 856b6f6426
commit 2b80d34e74
44 changed files with 2488 additions and 332 deletions
+10 -8
View File
@@ -6,6 +6,8 @@
#include <stdbool.h>
#include <ctype.h>
#include "FreeRTOS.h"
#include "lftpd.h"
#include "private/lftpd_status.h"
#include "private/lftpd_inet.h"
@@ -222,14 +224,14 @@ static int cmd_cwd(lftpd_client_t *client, const char *arg)
int is_dir;
if (lftpd_io_stat(path, &size, &is_dir) != 0 || !is_dir) {
send_simple_response(client->socket, 550, STATUS_550);
free(path);
vPortFree(path);
return -1;
}
}
strncpy(client->directory, path, sizeof(client->directory) - 1);
client->directory[sizeof(client->directory) - 1] = '\0';
free(path);
vPortFree(path);
send_simple_response(client->socket, 250, STATUS_250);
return 0;
@@ -248,18 +250,18 @@ static int cmd_dele(lftpd_client_t *client, const char *arg)
int is_dir;
if (lftpd_io_stat(path, &size, &is_dir) != 0) {
send_simple_response(client->socket, 550, STATUS_550);
free(path);
vPortFree(path);
return -1;
}
if (is_dir) {
send_simple_response(client->socket, 550, STATUS_550);
free(path);
vPortFree(path);
return -1;
}
lftpd_io_unlink(path);
free(path);
vPortFree(path);
send_simple_response(client->socket, 250, STATUS_250);
return 0;
}
@@ -459,7 +461,7 @@ static int cmd_retr(lftpd_client_t *client, const char *arg)
lftpd_log_error("DOWNLOAD '%s' FAILED", path);
send_simple_response(client->socket, 450, STATUS_450);
}
free(path);
vPortFree(path);
return 0;
}
@@ -481,7 +483,7 @@ static int cmd_size(lftpd_client_t *client, const char *arg)
send_simple_response(client->socket, 550, STATUS_550);
}
free(path);
vPortFree(path);
return 0;
}
@@ -518,7 +520,7 @@ static int cmd_stor(lftpd_client_t *client, const char *arg)
lftpd_log_error("UPLOAD '%s' FAILED", path);
send_simple_response(client->socket, 450, STATUS_450);
}
free(path);
vPortFree(path);
return 0;
}
+10 -8
View File
@@ -3,6 +3,8 @@
#include <string.h>
#include <ctype.h>
#include "FreeRTOS.h"
#include "ff.h"
#include "private/lftpd_io.h"
@@ -41,19 +43,19 @@ char *lftpd_io_canonicalize_path(const char *base, const char *name)
char *path;
if (name[0] == '/') {
path = (char *)malloc(strlen(name) + 1);
path = (char *)pvPortMalloc(strlen(name) + 1);
if (path == NULL) return NULL;
strcpy(path, name);
} else {
size_t len = strlen(base) + 1 + strlen(name) + 1;
path = (char *)malloc(len);
path = (char *)pvPortMalloc(len);
if (path == NULL) return NULL;
snprintf(path, len, "%s/%s", base, name);
}
char *abs_path = (char *)malloc(strlen(path) + 2);
char *abs_path = (char *)pvPortMalloc(strlen(path) + 2);
if (abs_path == NULL) {
free(path);
vPortFree(path);
return NULL;
}
abs_path[0] = '\0';
@@ -83,7 +85,7 @@ char *lftpd_io_canonicalize_path(const char *base, const char *name)
*p = saved;
}
free(path);
vPortFree(path);
if (strlen(abs_path) == 0) {
strcpy(abs_path, "/");
@@ -186,13 +188,13 @@ int lftpd_io_unlink(const char *path)
void *lftpd_io_opendir(const char *path)
{
DIR *dp = (DIR *)malloc(sizeof(DIR));
DIR *dp = (DIR *)pvPortMalloc(sizeof(DIR));
if (dp == NULL) {
return NULL;
}
const char *fpath = to_fatfs_path(path);
if (f_opendir(dp, fpath) != FR_OK) {
free(dp);
vPortFree(dp);
return NULL;
}
return (void *)dp;
@@ -230,6 +232,6 @@ void lftpd_io_closedir(void *dp)
{
if (dp != NULL) {
f_closedir((DIR *)dp);
free(dp);
vPortFree(dp);
}
}
+30 -4
View File
@@ -11,6 +11,7 @@
#include <string.h>
#include <stdio.h>
#include <stdbool.h>
#include "FreeRTOS.h"
#include "task.h"
@@ -69,11 +70,27 @@ static int net_relisten_locked(net_sock_t *p_sock);
static int net_accept_locked(int sockfd, struct net_sockaddr *addr, int *addrlen);
static int net_connect_locked(int sockfd, const struct net_sockaddr *addr, int addrlen);
/*
* 函数功能:判断 6 字节 MAC 是否全零
* 入口参数:p_mac - MAC 指针(不可为 NULL) const uint8_t *
* 返回值:true - 全零(= 使用芯片内置 MAC);false - 非全零
* 限定条件:p_mac 非空
*/
static bool s_mac_is_zero(const uint8_t *p_mac) {
for (int i = 0; i < 6; i++) {
if (p_mac[i] != 0u) {
return false;
}
}
return true;
}
/*
* 函数功能:网络子系统初始化(配置 CH395F + 启用多连接模式)
* 入口参数:ip - IP 地址字符串 "192.168.1.100",NULL 使用 DHCP 自动获取 const char *
* mask - 子网掩码字符串 "255.255.255.0",NULL 使用默认值 const char *
* gateway - 网关地址字符串 "192.168.1.1",NULL 使用默认值 const char *
* mac - MAC 地址(6 字节,低字节在前),NULL/全零使用芯片内置 const uint8_t *
* 返回值:0 - 初始化成功 int
* -1 - 初始化失败(芯片检测/复位/协议栈初始化失败) int
* 限定条件:SPI2 已正确初始化;netTask 尚未启动
@@ -81,8 +98,10 @@ static int net_connect_locked(int sockfd, const struct net_sockaddr *addr, int a
* 2. 启用 TCP Server 多连接模式(bit1=1),设置 TCP MSS、重传参数、KeepAlive 参数
* 3. 配置 IP/网关/子网掩码,或启用 DHCP
* 4. 初始化 CH395F 协议栈并使能 PING 响应
* 5. mac 非空且非全零:初始化成功后设置 MAC(写芯片 EEPROM,
* 每上电重放同值幂等)
*/
int net_init(const char *ip, const char *mask, const char *gateway) {
int net_init(const char *ip, const char *mask, const char *gateway, const uint8_t *mac) {
ch395f_status_t ret;
DBG_INFO("net_init: start");
@@ -155,6 +174,13 @@ int net_init(const char *ip, const char *mask, const char *gateway) {
return -1;
}
/* 设置 MAC(须在协议栈初始化之后,限定条件"芯片已初始化";写芯片 EEPROM,
* 每上电重放同值幂等);NULL 或全零 = 保持芯片内置 MAC */
if (mac != NULL && s_mac_is_zero(mac) == false) {
ch395f_set_mac_addr((uint8_t *)mac);
DBG_INFO("net_init: set MAC");
}
/* 多 Socket 模式:为 Socket 0~7 各分配独立收发缓冲(必须在 OPEN_SOCKET 之前)
* 手册(§5.45):CH395F 内部缓冲共 48 块 × 512B = 24KB;芯片上电默认仅给 Socket 0~3
* 分配(每 Socket 接收 8 块/发送 4 块),Socket 4~7 为 NULL,故用到 8 个 Socket 必须重分配。
@@ -520,7 +546,7 @@ int net_listen_locked(int sockfd) {
break; /* 打开成功,退出重试 */
}
ch395f_close_socket(sockfd); /* 打开失败先关闭,再延时后重试 */
HAL_Delay(500);
osDelay(500);
}
/* 3 次仍失败,返回错误 */
if (status != CH395F_ERR_SUCCESS) {
@@ -795,7 +821,7 @@ static int net_connect_locked(int sockfd, const struct net_sockaddr *addr, int a
break; /* 打开成功,退出重试 */
}
ch395f_close_socket(sockfd); /* 打开失败先关闭,再延时后重试 */
HAL_Delay(500);
osDelay(500);
}
if (status != CH395F_ERR_SUCCESS) {
@@ -1020,7 +1046,7 @@ static int net_relisten_locked(net_sock_t *p_sock) {
break;
}
ch395f_close_socket((uint8_t)sockfd); /* 打开失败先关闭,再延时后重试 */
HAL_Delay(500);
osDelay(500);
}
if (status != CH395F_ERR_SUCCESS) {
s_net_errno = NET_ERR_BUSY;
+5 -2
View File
@@ -53,11 +53,14 @@ typedef struct {
* 入口参数:ip - 本地 IP 地址字符串 "192.168.1.100",NULL 使用 DHCP
* mask - 子网掩码字符串 "255.255.255.0",NULL 使用默认
* gateway - 网关地址字符串 "192.168.1.1",NULL 使用默认
* mac - MAC 地址(6 字节,低字节在前),NULL 或全零使用芯片内置
* 返回值:0 成功,-1 失败
* 限定条件:SPI2 已正确初始化
* 函数说明:必须在所有网络操作之前调用
* 函数说明:必须在所有网络操作之前调用;mac 非空且非全零时于协议栈
* 初始化成功后设置(ch395f_set_mac_addr 写芯片 EEPROM,
* 每上电重放同值幂等)
*/
int net_init(const char *ip, const char *mask, const char *gateway);
int net_init(const char *ip, const char *mask, const char *gateway, const uint8_t *mac);
/*
* 函数功能:轮询所有 Socket 状态(必须在主循环中调用)