change 1
This commit is contained in:
parent
d4a216157f
commit
a55645f3af
|
@ -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)
|
||||
|
|
|
@ -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 <string.h>
|
||||
#include <stdio.h>
|
||||
#include <transform.h>
|
||||
|
||||
|
||||
|
||||
|
||||
static int server_sock = -1; // 用于跟踪服务器的socket
|
||||
|
||||
const char *html_form =
|
||||
"<html><body>"
|
||||
"<form method='post'>"
|
||||
"IP: <input type='text' name='ip'><br>"
|
||||
"Gateway: <input type='text' name='gateway'><br>"
|
||||
"DNS: <input type='text' name='dns'><br>"
|
||||
"Subnet Mask: <input type='text' name='subnet_mask'><br>"
|
||||
"<input type='submit' value='Set'>"
|
||||
"</form>"
|
||||
"</body></html>";
|
||||
|
||||
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);
|
|
@ -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
|
Loading…
Reference in New Issue