嵌入式 lwip http server makefsdata

背景:

基于君正X2000 MCU Freertos+lwip架构 实现HTTP server服务,MCU作为HTTP服务器通过网口进行数据包的传输,提供网页服务。其中设计到LWIP提供的工具makefsdata,常用于将文件或目录结构转换为适合嵌入到固件中的二进制格式。

一、 lwip 源码和资源

1. 1 源码资源下载链接

lwip官方网站:https://savannah.nongnu.org/projects/lwip/

lwip官方网站下载地址:https://download.savannah.nongnu.org/releases/lwip/

下载解压打开后文件目录大致是这个样子

在这里插入图片描述

1.2 资源目录介绍:

fs用于存放网页资源诸如HTML、CSS、JavaScript等文件。makefsdata是官网提供的工具用于将fs中网页资源转换为适合嵌入到固件中的二进制格式,使用makefsdata生成fsdata.c方便集成到嵌入式系统当中。其他是一些http实现的接口及头文件。

二 、makefsdata工具使用

2.1 makefsdata转换工具有俩种实现方式:

2. 1. 1使用makefsdata脚本

使用源码makefsdata文件夹下的makefsdata脚本直接解释运行,使用脚本只需要添加运行权限,直接运行即可快捷方便的生成fsdata.c集成到嵌入式系统中。但是缺乏一些扩展功能,使用不够灵活且无法使用压缩,这对于资源有限的嵌入式设备来说是不可接受的。

lwip源码资源下载后存放网页资源的fs脚本在makefsdata脚本的上层目录,而makefsdata脚本默认将当前目录下的文件资源转换生成fsdata.c。故对脚本做一些修改。将脚本中的chdir(“fs”); ——> chdir(“…/fs”);

添加文件复制移动命令将生成的fsdata.c自动替换成上层旧文件方便生成后编译

在这里插入图片描述

下面是修改后的完整makefsdata脚本

#!/usr/bin/perluse File::Copy;open(OUTPUT, "> fsdata.c");chdir("../fs");
open(FILES, "find . -type f |");while($file = <FILES>) {# Do not include files in CVS directories nor backup files.if($file =~ /(CVS|~)/) {next;}chop($file);open(HEADER, "> /tmp/header") || die $!;if($file =~ /404/) {print(HEADER "HTTP/1.0 404 File not found\r\n");} else {print(HEADER "HTTP/1.0 200 OK\r\n");}print(HEADER "Server: lwIP/pre-0.6 (http://www.sics.se/~adam/lwip/)\r\n");if($file =~ /\.html$/) {print(HEADER "Content-type: text/html\r\n");} elsif($file =~ /\.gif$/) {print(HEADER "Content-type: image/gif\r\n");} elsif($file =~ /\.png$/) {print(HEADER "Content-type: image/png\r\n");} elsif($file =~ /\.jpg$/) {print(HEADER "Content-type: image/jpeg\r\n");} elsif($file =~ /\.class$/) {print(HEADER "Content-type: application/octet-stream\r\n");} elsif($file =~ /\.ram$/) {print(HEADER "Content-type: audio/x-pn-realaudio\r\n");    } else {print(HEADER "Content-type: text/plain\r\n");}print(HEADER "\r\n");close(HEADER);unless($file =~ /\.plain$/ || $file =~ /cgi/) {system("cat /tmp/header $file > /tmp/file");} else {system("cp $file /tmp/file");}open(FILE, "/tmp/file");unlink("/tmp/file");unlink("/tmp/header");$file =~ s/\.//;$fvar = $file;$fvar =~ s-/-_-g;$fvar =~ s-\.-_-g;print(OUTPUT "static const unsigned char data".$fvar."[] = {\n");print(OUTPUT "\t/* $file */\n\t");for($j = 0; $j < length($file); $j++) {printf(OUTPUT "%#02x, ", unpack("C", substr($file, $j, 1)));}printf(OUTPUT "0,\n");$i = 0;while(read(FILE, $data, 1)) {if($i == 0) {print(OUTPUT "\t");}printf(OUTPUT "%#02x, ", unpack("C", $data));$i++;if($i == 10) {print(OUTPUT "\n");$i = 0;}}print(OUTPUT "};\n\n");close(FILE);push(@fvars, $fvar);push(@files, $file);
}for($i = 0; $i < @fvars; $i++) {$file = $files[$i];$fvar = $fvars[$i];if($i == 0) {$prevfile = "NULL";} else {$prevfile = "file" . $fvars[$i - 1];}print(OUTPUT "const struct fsdata_file file".$fvar."[] = {{$prevfile, data$fvar, ");print(OUTPUT "data$fvar + ". (length($file) + 1) .", ");print(OUTPUT "sizeof(data$fvar) - ". (length($file) + 1) .", FS_FILE_FLAGS_HEADER_INCLUDED | FS_FILE_FLAGS_HEADER_PERSISTENT}};\n\n");
}chdir("../makefsdata");
move("fsdata.c", "../fsdata.c");print(OUTPUT "#define FS_ROOT file$fvars[$i - 1]\n\n");
print(OUTPUT "#define FS_NUMFILES $i\n");

2.1.2 编译makefsdata.c后使用可执行文件生成fsdata.c

博主在windows下使用vscode+mingw编译生成makefsdata.exe,进而使用该程序生成fsdata.c。相较于脚本使用多了一步编译生成的步骤,但是多了许多扩展功能且可以进行zlib压缩网页资源文件。对于资源有限的嵌入式设备压缩功能就十分必要。编译好的makefsdata.exe放在置顶,可以直接在windows下命令行运行。

编译参考博客 使用vscode编译makefsdata 如果需要压缩功能需要额外下载第三方库如zlip。压缩转换命令如下,可配合-XC排除不需要压缩的文件。官网提供的网页资源不经压缩转换后fsdata.c大小为21KB,经过-defl:5参数压缩转换后为14KB。压缩比例约为66KB,不同压缩等级,网页资源经过压缩后压缩比例不尽相同。

makefsdata.exe -defl:5   //  Windows下命令行运行,-defl表示使用压缩,5表示压缩等级(1-10)默认10

扩展功能参考资源文件下的readme.txt,更多功能可以参考源码或者makefsdata -help查看user page。

This directory contains a script ('makefsdata') to create C code suitable for
httpd for given html pages (or other files) in a directory.There is also a plain C console application doing the same and extended a bit.Usage: htmlgen [targetdir] [-s] [-i]stargetdir: relative or absolute path to files to convertswitch -s: toggle processing of subdirectories (default is on)switch -e: exclude HTTP header from file (header is created at runtime, default is on)switch -11: include HTTP 1.1 header (1.0 is default)if targetdir not specified, makefsdata will attempt toprocess files in subdirectory 'fs'.The C version of this program can optionally store the none-SSI files in
a compressed form in which they are also sent to the web client (which
must support the Deflate content encoding). Files that grow during compression
(due to being not compressible well), will stored umcompressed automatically.
In order to do so, compile the program with MAKEFS_SUPPORT_DEFLATE set to 1. You must
manually download minizip.c for this to work. As an alternative, you can additionally
define MAKEFS_SUPPORT_DEFLATE_ZLIB to use your system's zlib instead.
Compression of .html, .js, .css and .svg files usually yields very good compression
rates and is a great way of reducing your program's size.

makefsdata.c实现的扩展功能。

static void print_usage(void)
{printf(" Usage: htmlgen [targetdir] [-s] [-e] [-11] [-nossi] [-ssi:<filename>] [-c] [-f:<filename>] [-m] [-svr:<name>] [-x:<ext_list>] [-xc:<ext_list>" USAGE_ARG_DEFLATE NEWLINE NEWLINE);printf("   targetdir: relative or absolute path to files to convert" NEWLINE);printf("   switch -s: toggle processing of subdirectories (default is on)" NEWLINE);printf("   switch -e: exclude HTTP header from file (header is created at runtime, default is off)" NEWLINE);printf("   switch -11: include HTTP 1.1 header (1.0 is default)" NEWLINE);printf("   switch -nossi: no support for SSI (cannot calculate Content-Length for SSI)" NEWLINE);printf("   switch -ssi: ssi filename (ssi support controlled by file list, not by extension)" NEWLINE);printf("   switch -c: precalculate checksums for all pages (default is off)" NEWLINE);printf("   switch -f: target filename (default is \"fsdata.c\")" NEWLINE);printf("   switch -m: include \"Last-Modified\" header based on file time" NEWLINE);printf("   switch -svr: server identifier sent in HTTP response header ('Server' field)" NEWLINE);printf("   switch -x: comma separated list of extensions of files to exclude (e.g., -x:json,txt)" NEWLINE);printf("   switch -xc: comma separated list of extensions of files to not compress (e.g., -xc:mp3,jpg)" NEWLINE);
#if MAKEFS_SUPPORT_DEFLATEprintf("   switch -defl: deflate-compress all non-SSI files (with opt. compr.-level, default=10)" NEWLINE);printf("                 ATTENTION: browser has to support \"Content-Encoding: deflate\"!" NEWLINE);
#endifprintf("   if targetdir not specified, htmlgen will attempt to" NEWLINE);printf("   process files in subdirectory 'fs'" NEWLINE);
}

三、集成fsdata.c到嵌入式设备

3.1 概述:君正X2000芯片提供2个网口驱动,不同芯片架构平台网口实现大同小异不是本文核心内容不做赘述。本次实验将设备默认静态IP设置为192.168.3.120。将生成的fsdata.c替换后编译生成固件烧录至开发板后即可展示网页资源。

3.2 调用LWIP实现HTTP server

调用LWIP实现最基础的网页展示十分简单,只需要将生成fsdata.c替换后在主程序头部包含LWIP头文件

#include "lwip/apps/httpd.h"

在主程序中调用httpd_init即可启动http server。

httpd_init();		// 初始化 HTTPD SERVER

嵌入式设备通过网线连接到PC,打开浏览器输入192.168.3.120(端口号会默认80)即可展示网页资源左上角图片为替换验证后的图片资源(与LWIP官方提供的默认图片资源不一样)。

在这里插入图片描述

3.3 http server实际应用功能待更新。。。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.rhkb.cn/news/21584.html

如若内容造成侵权/违法违规/事实不符,请联系长河编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

论文笔记-WSDM2025-ColdLLM

论文笔记-WSDM2025-Large Language Model Simulator for Cold-Start Recommendation ColdLLM&#xff1a;用于冷启动推荐的大语言模型模拟器摘要1.引言2.前言3.方法3.1整体框架3.1.1行为模拟3.1.2嵌入优化 3.2耦合漏斗ColdLLM3.2.1过滤模拟3.2.2精炼模拟 3.3模拟器训练3.3.1LLM…

《DeepSeek-V3:人工智能大语言模型》

《DeepSeek-V3:人工智能大语言模型》 1. 引言 我们介绍了 DeepSeek-V3,这是一个强大的专家混合 (MoE) 语言模型,总共有 671B 个参数,每个令牌激活了 37B。 为了实现高效的推理和具有成本效益的训练,DeepSeek-V3 采用了多头潜在注意力 (MLA) 和 DeepSeekMoE 架构,这些…

手机控制电脑远程关机

远程看看软件兼容iOS和Android设备&#xff0c;该软件除了能通过电脑远程关闭另一台电脑外&#xff0c;您还可以通过它在手机上远程关闭公司的电脑。您可以按照以下步骤进行操作以实现电脑远程关机&#xff1a; 步骤1.在手机应用商店搜索“远程看看”进行软件安装&#xff0c;…

Aseprite绘画流程案例(1)——画相机图标

原图&#xff1a; 步骤一&#xff1a;打开需要参照的图标 步骤二&#xff1a;将参照的图片拖放到右边&#xff0c;作为参考 步骤三&#xff1a;新建24x24的画布&#xff0c;背景为白色的画布 步骤四&#xff1a;点击菜单栏——视图——显示——像素网格&#xff08;如果画布已经…

The Heliosphere 日球层

转自 The Heliosphere - NASA This is an artists concept of our Heliosphere as it travels through our galaxy with the major features labeled. Termination Shock: Blowing outward billions of kilometers from the Sun is the solar wind, a thin stream of electrica…

使用API有效率地管理Dynadot域名,为域名部署DNS安全拓展(DNSSEC)

关于Dynadot Dynadot是通过ICANN认证的域名注册商&#xff0c;自2002年成立以来&#xff0c;服务于全球108个国家和地区的客户&#xff0c;为数以万计的客户提供简洁&#xff0c;优惠&#xff0c;安全的域名注册以及管理服务。 Dynadot平台操作教程索引&#xff08;包括域名邮…

vsan数据恢复—vsan缓存盘故障导致虚拟磁盘文件丢失的数据恢复案例

vsan数据恢复环境&故障&#xff1a; VMware vsan架构采用21模式。每台设备只有一个磁盘组&#xff08;71&#xff09;&#xff0c;缓存盘的大小为240GB&#xff0c;容量盘的大小为1.2TB。 由于其中一台主机&#xff08;0号组设备&#xff09;的缓存盘出现故障&#xff0c;导…

匹配算法:向下就近原则,向下没有就向上

匹配算法&#xff1a;向下就近原则&#xff0c;向下没有就向上 实现方式一实现方式二总结 实现方式一 private static List<Integer> findMatches(List<Integer> sourceList, List<Integer> searchValues) {List<Integer> sortedList sourceList.stre…

AI客服-接入deepseek大模型到微信(本地部署deepseek集成微信自动收发消息)

1.本地部署 1.1 ollama Ollama软件通过其高度优化的推理引擎和先进的内存管理机制&#xff0c;显著提升了大型语言模型在本地设备上的运行效率。其核心采用了量化技术&#xff08;Quantization&#xff09;以降低模型的计算复杂度和存储需求&#xff0c;同时结合张量并行计算&…

Python VsCode DeepSeek接入

Python VsCode DeepSeek接入 创建API key 首先进入DeepSeek官网&#xff0c;https://www.deepseek.com/ 点击左侧“API Keys”&#xff0c;创建API key&#xff0c;输出名称为“AI” 点击“创建"&#xff0c;将API key保存&#xff0c;复制在其它地方。 在VsCode中下载…

【python】网页批量转PDF

安装wkhtmltopdf 网站&#xff1a;wkhtmltopdf wkhtmltopdf http://www.baidu.com/ D:website1.pdf 安装pdfkit库 pip install pdfkit 批量转换代码 import os import pdfkit path_wkthmltopdf rE:\Program Files\wkhtmltopdf\bin\wkhtmltopdf.exe config pdfkit.configu…

架构师面试(三):订阅模型

问题 对【注册中心】【配置中心】【消息队列】和【IM】进行分析和抽象&#xff0c;可归纳出一个完整的业务模型单元&#xff0c;即【订阅系统】&#xff0c;下面关于实现订阅系统的几种模型的相关描述中&#xff0c;说法正确的有哪几项&#xff1f; A. 信箱模型&#xff0c;即…

数据结构:算法的时间复杂度和空间复杂度

1.算法效率 1.1 如何衡量一个算法的好坏 如何衡量一个算法的好坏呢&#xff1f; 比如对于以下斐波那契数列&#xff1a; long long Fib(int N) {if(N < 3)return 1;return Fib(N-1) Fib(N-2); }斐波那契数列的递归实现方式非常简洁&#xff0c;但简洁一定好吗&#xff…

linux下pip下载项目失败

想下载CLIP的项目复现代码的时候&#xff0c;出现问题如下&#xff1a; 于是手动使用 Git 克隆仓库&#xff0c; git clone https://github.com/openai/CLIP.git cd CLIP pip install .ls查看文件如下&#xff1a;(手动克隆git项目成功)

Redis文档总结

文档&#xff1a;https://redis.com.cn/topics/why-use-redis.html 1.我们为什么一定要用 Redis 呢&#xff1f; 速度快&#xff0c;完全基于内存&#xff0c;使用 C 语言实现&#xff0c;网络层使用 epoll 解决高并发问题&#xff0c;单线程模型避免了不必要的上下文切换及竞争…

【前端框架】Vue3 面试题深度解析

本文详细讲解了VUE3相关的面试题&#xff0c;从基础到进阶到高级&#xff0c;分别都有涉及&#xff0c;希望对你有所帮助&#xff01; 基础题目 1. 简述 Vue3 与 Vue2 相比有哪些主要变化&#xff1f; 答案&#xff1a; 响应式系统&#xff1a;Vue2 使用 Object.definePrope…

Django+Vue3全栈开发实战:从零搭建博客系统

文章目录 1. 开发环境准备2. 创建Django项目与配置3. 设计数据模型与API4. 使用DRF创建RESTful API5. 创建Vue3项目与配置6. 前端页面开发与组件设计7. 前后端交互与Axios集成8. 项目优化与调试9. 部署上线10. 总结与扩展10.1 项目总结10.1.1 技术栈回顾10.1.2 项目亮点 10.2 扩…

【论文笔记】MambaGlue: Fast and Robust Local Feature Matching With Mamba

【引用格式】&#xff1a;Ryoo K, Lim H, Myung H. MambaGlue: Fast and Robust Local Feature Matching With Mamba[J]. arXiv preprint arXiv:2502.00462, 2025. 【网址】&#xff1a;https://arxiv.org/pdf/2502.00462 【开源代码】&#xff1a;https://github.com/uri-Ka…

Office word打开加载比较慢处理方法

1.添加safe参数 ,找到word启动项,右击word,选择属性 , 添加/safe , 应用并确定 2.取消加载项,点击文件,点击选项 ,点击加载项,点击转到,取消所有勾选,确定。

Denoising Diffusion Restoration Models论文解读

论文要点 恢复的线性逆问题可以使用预训练的DDPM完成&#xff1a;1. 将降质矩阵使用SVD&#xff0c;得到分解矩阵&#xff1b;2. 使用分解矩阵将图像投影到降质类型间共享的谱空间&#xff1b;3. 谱空间中执行DDPM。 评价 同Track的方法同样很多&#xff0c;比如后续的DDNM、…