diff --git a/.eide/eide.json b/.eide/eide.json index db7618e..1ec9c95 100644 --- a/.eide/eide.json +++ b/.eide/eide.json @@ -72,7 +72,9 @@ "Drivers/CMSIS/Include", "Core/ioLibrary_Driver/Ethernet/W5500", "Core/ioLibrary_Driver/Ethernet", - "Core/ioLibrary_Driver/Internet/DNS" + "Core/ioLibrary_Driver/Internet/DNS", + "Core/motor", + "Core/Html" ], "libList": [], "defineList": [ @@ -88,18 +90,15 @@ "afterBuildTasks": [], "global": { "$float-abi-type": "softfp", - "output-debug-info": "enable", - "misc-control": [] + "output-debug-info": "enable" }, "c/cpp-compiler": { - "language-c": "c11", + "language-c": "gnu17", "language-cpp": "c++11", "optimization": "level-debug", "warnings": "all-warnings", "one-elf-section-per-function": true, - "one-elf-section-per-data": true, - "C_FLAGS": "", - "CXX_FLAGS": "" + "one-elf-section-per-data": true }, "asm-compiler": { "ASM_FLAGS": "-DDEBUG" @@ -108,7 +107,7 @@ "output-format": "elf", "remove-unused-input-sections": true, "LD_FLAGS": "-TSTM32F103VCTX_FLASH.ld", - "LIB_FLAGS": "" + "$toolName": "auto" } } } diff --git a/Core/Html/index.html b/Core/Html/index.html new file mode 100644 index 0000000..c69d5f5 --- /dev/null +++ b/Core/Html/index.html @@ -0,0 +1,189 @@ +R"( + +W5500 Configuration + + + + +

W5500 Network Configuration

+
+
+ + +
+
+ + +
+
+ + +
+
+ + +
+ +
+
+ Network Status: %s +
+)" \ No newline at end of file diff --git a/Core/Inc/html_files.h b/Core/Inc/html_files.h new file mode 100644 index 0000000..1802fa1 --- /dev/null +++ b/Core/Inc/html_files.h @@ -0,0 +1,15 @@ +#ifndef __HTML_FILES_H +#define __HTML_FILES_H + +#ifdef __cplusplus +extern "C" { +#endif + +// 声明HTML页面内容 +extern const char index_html[]; + +#ifdef __cplusplus +} +#endif + +#endif /* __HTML_FILES_H */ \ No newline at end of file diff --git a/Core/Src/html_files.c b/Core/Src/html_files.c new file mode 100644 index 0000000..79e2133 --- /dev/null +++ b/Core/Src/html_files.c @@ -0,0 +1,6 @@ +#include "html_files.h" + +// 主页HTML内容 +const char index_html[] = +#include "index.html" +; \ No newline at end of file diff --git a/Core/Src/http_server.c b/Core/Src/http_server.c index f393bab..feb2628 100644 --- a/Core/Src/http_server.c +++ b/Core/Src/http_server.c @@ -1,100 +1,7 @@ #include "http_server.h" // HTML页面模板 -const char* html_page = "" -"" -"W5500 Configuration" -"" -"" -"

W5500 Network Configuration

" -"
" -"
" -" " -" " -"
" -"
" -" " -" " -"
" -"
" -" " -" " -"
" -"
" -" " -" " -"
" -" " -"
" -"
" -" Network Status: %s" -"
" -""; +#include "html_files.h" // 解析HTTP请求 static void parse_http_request(uint8_t* buffer, uint16_t length, http_request* req) @@ -124,28 +31,57 @@ static void parse_http_request(uint8_t* buffer, uint16_t length, http_request* r } // 发送HTTP响应 -static void send_response(uint8_t sn, const char* content, uint16_t length) +static int send_all(uint8_t sn, const uint8_t* data, uint16_t length) { - char header[128]; - sprintf(header, - "HTTP/1.1 200 OK\r\n" - "Content-Type: text/html\r\n" - "Content-Length: %d\r\n" - "\r\n", length); - - send(sn, (uint8_t*)header, strlen(header)); - send(sn, (uint8_t*)content, length); + int total_sent = 0; + while (total_sent < length) { + int sent = send(sn, data + total_sent, length - total_sent); + if (sent <= 0) { + return -1; // 发送错误 + } + total_sent += sent; + } + return total_sent; } +static void send_chunked_response_header(uint8_t sn) +{ + const char* header = + "HTTP/1.1 200 OK\r\n" + "Content-Type: text/html\r\n" + "Transfer-Encoding: chunked\r\n" + "\r\n"; + + send_all(sn, (uint8_t*)header, strlen(header)); +} + +static void send_chunk(uint8_t sn, const char* data, uint16_t length) +{ + char chunk_header[10]; + sprintf(chunk_header, "%X\r\n", length); + send_all(sn, (uint8_t*)chunk_header, strlen(chunk_header)); + send_all(sn, (uint8_t*)data, length); + send_all(sn, (uint8_t*)"\r\n", 2); +} + +static void send_chunk_end(uint8_t sn) +{ + send_all(sn, (uint8_t*)"0\r\n\r\n", 5); +} + +#define CHUNK_SIZE 512 + // 处理配置页面请求 static void handle_config_page(uint8_t sn) { - char page[4096]; // 增加缓冲区大小以适应新的HTML内容 + char buffer[CHUNK_SIZE]; bool is_connected = (wizphy_getphylink() == PHY_LINK_ON); char* link_status = is_connected ? "Connected" : "Disconnected"; char* status_class = is_connected ? "connected" : "disconnected"; - sprintf(page, html_page, + // 先将完整的HTML内容格式化到一个临时缓冲区 + char full_page[8192]; // 确保足够大 + int total_len = sprintf(full_page, index_html, gWIZNETINFO.mac[0], gWIZNETINFO.mac[1], gWIZNETINFO.mac[2], gWIZNETINFO.mac[3], gWIZNETINFO.mac[4], gWIZNETINFO.mac[5], gWIZNETINFO.ip[0], gWIZNETINFO.ip[1], gWIZNETINFO.ip[2], gWIZNETINFO.ip[3], @@ -153,7 +89,20 @@ static void handle_config_page(uint8_t sn) gWIZNETINFO.gw[0], gWIZNETINFO.gw[1], gWIZNETINFO.gw[2], gWIZNETINFO.gw[3], status_class, link_status); - send_response(sn, page, strlen(page)); + // 发送响应头 + send_chunked_response_header(sn); + + // 分块发送内容 + int sent = 0; + while (sent < total_len) { + int chunk_len = (total_len - sent) > CHUNK_SIZE ? CHUNK_SIZE : (total_len - sent); + memcpy(buffer, full_page + sent, chunk_len); + send_chunk(sn, buffer, chunk_len); + sent += chunk_len; + } + + // 发送结束块 + send_chunk_end(sn); } // 解析并更新网络配置 @@ -225,9 +174,11 @@ void http_server_task(void) else if(req.method == HTTP_POST && strcmp(req.uri, "/config") == 0) { update_network_config((char*)req.body); // 重定向回配置页面 - send_response(HTTP_SERVER_SOCKET, + send_chunked_response_header(HTTP_SERVER_SOCKET); + send_chunk(HTTP_SERVER_SOCKET, "", strlen("")); + send_chunk_end(HTTP_SERVER_SOCKET); } } } diff --git a/Core/Src/main.c b/Core/Src/main.c index 1025eb5..35393ea 100644 --- a/Core/Src/main.c +++ b/Core/Src/main.c @@ -26,7 +26,7 @@ /* Private includes ----------------------------------------------------------*/ /* USER CODE BEGIN Includes */ #include "w5500_init.h" -#include "http_test.h" +// #include "http_test.h" #include "http_server.h" /* USER CODE END Includes */ @@ -108,7 +108,8 @@ int main(void) http_server_init(); // 执行一次HTTP客户端测试 - http_test_request(); + // http_test_request(); + HAL_GPIO_WritePin(LED_GPIO_Port, LED_Pin, GPIO_PIN_RESET); /* USER CODE END 2 */ /* Infinite loop */ @@ -119,8 +120,8 @@ int main(void) /* USER CODE BEGIN 3 */ http_server_task(); - HAL_GPIO_TogglePin(LED_GPIO_Port, LED_Pin); - HAL_Delay(500); + // HAL_GPIO_TogglePin(LED_GPIO_Port, LED_Pin); + // HAL_Delay(100); } /* USER CODE END 3 */ } diff --git a/f1CTL.code-workspace b/f1CTL.code-workspace index 5310c11..9a2aace 100644 --- a/f1CTL.code-workspace +++ b/f1CTL.code-workspace @@ -18,7 +18,8 @@ "*.c++": "cpp", "*.cpp": "cpp", "*.cxx": "cpp", - "*.cc": "cpp" + "*.cc": "cpp", + "*.html": "c" }, "[yaml]": { "editor.insertSpaces": true,