From a55645f3af6e4aabe9e38884f1e42fe38372fd06 Mon Sep 17 00:00:00 2001 From: yzd <2804710251@qq.com> Date: Thu, 5 Oct 2023 09:45:14 -0700 Subject: [PATCH] change 1 --- APP_Framework/Applications/app_test/Makefile | 2 +- .../app_test/test_webserver/README.md | 0 .../app_test/test_webserver/test_webserver.c | 144 ++++++++++++++++++ .../app_test/test_webserver/test_webserver.h | 19 +++ 4 files changed, 164 insertions(+), 1 deletion(-) create mode 100644 APP_Framework/Applications/app_test/test_webserver/README.md create mode 100644 APP_Framework/Applications/app_test/test_webserver/test_webserver.c create mode 100644 APP_Framework/Applications/app_test/test_webserver/test_webserver.h diff --git a/APP_Framework/Applications/app_test/Makefile b/APP_Framework/Applications/app_test/Makefile index 6016dcb7a..10f8129d6 100644 --- a/APP_Framework/Applications/app_test/Makefile +++ b/APP_Framework/Applications/app_test/Makefile @@ -114,7 +114,7 @@ ifeq ($(CONFIG_ADD_XIZI_FEATURES),y) endif ifeq ($(CONFIG_USER_TEST_WEBSERVER),y) - SRC_FILES += + SRC_FILES += test_webserver/test_webserver.c endif ifeq ($(CONFIG_USER_TEST_MQTTCLIENT),y) diff --git a/APP_Framework/Applications/app_test/test_webserver/README.md b/APP_Framework/Applications/app_test/test_webserver/README.md new file mode 100644 index 000000000..e69de29bb diff --git a/APP_Framework/Applications/app_test/test_webserver/test_webserver.c b/APP_Framework/Applications/app_test/test_webserver/test_webserver.c new file mode 100644 index 000000000..1edf4f1f7 --- /dev/null +++ b/APP_Framework/Applications/app_test/test_webserver/test_webserver.c @@ -0,0 +1,144 @@ +#include "lwip/opt.h" +#include "lwip/arch.h" +#include "lwip/api.h" +#include "lwip/sockets.h" + +#undef ioctl // 解除ioctl的宏定义 + +#include "test_webserver.h" +#include +#include +#include + + + + +static int server_sock = -1; // 用于跟踪服务器的socket + +const char *html_form = +"" +"
" +"IP:
" +"Gateway:
" +"DNS:
" +"Subnet Mask:
" +"" +"
" +""; + +NetworkConfig config; + +void handle_client(int client_fd) { + char request[1024]; + int bytes = recv(client_fd, request, sizeof(request) - 1, 0); + + if (bytes > 0) { + request[bytes] = '\0'; + + // 简单解析POST数据 + if (strstr(request, "POST")) { + char *start, *end; + + start = strstr(request, "ip="); + if (start) { + start += 3; + end = strstr(start, "&"); + strncpy(config.ip, start, end - start); + } + + start = strstr(request, "gateway="); + if (start) { + start += 8; + end = strstr(start, "&"); + strncpy(config.gateway, start, end - start); + } + + start = strstr(request, "dns="); + if (start) { + start += 4; + end = strstr(start, "&"); + strncpy(config.dns, start, end - start); + } + + start = strstr(request, "subnet_mask="); + if (start) { + start += 12; + end = strchr(start, ' '); + if (!end) end = start + strlen(start); + strncpy(config.subnet_mask, start, end - start); + } + + printf("Configured IP: %s\n", config.ip); + printf("Configured Gateway: %s\n", config.gateway); + printf("Configured DNS: %s\n", config.dns); + printf("Configured Subnet Mask: %s\n", config.subnet_mask); + } + + send(client_fd, html_form, strlen(html_form), 0); + } + lwip_close(client_fd); +} + +void start_webserver(void) { + if (server_sock >= 0) { + printf("Web server is already running.\n"); + return; + } + + server_sock = lwip_socket(AF_INET, SOCK_STREAM, 0); + if (server_sock < 0) { + printf("Failed to create socket.\n"); + return; + } + + struct sockaddr_in addr; + memset(&addr, 0, sizeof(addr)); + addr.sin_family = AF_INET; + addr.sin_port = lwip_htons(80); + addr.sin_addr.s_addr = lwip_htonl(INADDR_ANY); + + if (lwip_bind(server_sock, (struct sockaddr*)&addr, sizeof(addr)) < 0) { + printf("Failed to bind to port 80.\n"); + lwip_close(server_sock); + server_sock = -1; + return; + } + + lwip_listen(server_sock, 5); + + struct sockaddr_in client_addr; + socklen_t addr_len = sizeof(client_addr); + while (1) { + int client_fd = lwip_accept(server_sock, (struct sockaddr*)&client_addr, &addr_len); + if (client_fd >= 0) { + handle_client(client_fd); + } + } +} + +void stop_webserver(void) { + if (server_sock < 0) { + printf("Web server is not running.\n"); + return; + } + + lwip_close(server_sock); + server_sock = -1; + + printf("Web server stopped.\n"); +} + +void testWebServer(void) { + printf("Web server starting...\n"); + start_webserver(); +} + +void cmdPrintConfig(int argc, char** argv) { + printf("IP: %s\n", config.ip); + printf("Gateway: %s\n", config.gateway); + printf("DNS: %s\n", config.dns); + printf("Subnet Mask: %s\n", config.subnet_mask); +} + +PRIV_SHELL_CMD_FUNCTION(testWebServer, web server-start, PRIV_SHELL_CMD_MAIN_ATTR); +PRIV_SHELL_CMD_FUNCTION(cmdPrintConfig, print webserver config on cmd, PRIV_SHELL_CMD_MAIN_ATTR); diff --git a/APP_Framework/Applications/app_test/test_webserver/test_webserver.h b/APP_Framework/Applications/app_test/test_webserver/test_webserver.h new file mode 100644 index 000000000..5e627907a --- /dev/null +++ b/APP_Framework/Applications/app_test/test_webserver/test_webserver.h @@ -0,0 +1,19 @@ +// test_webserver.h +#ifndef TEST_WEBSERVER_H +#define TEST_WEBSERVER_H + +// 数据结构存储网络配置 +typedef struct { + char ip[16]; + char gateway[16]; + char dns[16]; + char subnet_mask[16]; +} NetworkConfig; + +// 函数声明 +void start_webserver(void); +void stop_webserver(void); +void testWebServer(void); // 注意:这里我根据.c文件修正了函数名,您的原始函数名是`test_web_server`,而.c文件中是`testWebServer` +void cmdPrintConfig(int argc, char** argv); // 同上,注意函数名的大小写 + +#endif