LWIP官方DEMO使用之httpd服务

前言

LWIP的移植可以参考上篇文章 《HC32F4A0 lwip移植 - USTHzhanglu - 博客园 (cnblogs.com)》

此博文为学习笔记,仅介绍如何使用官方demo,无更深入分析。

关键词:LWIP, HTTP, HTTPD, HC32

参数

MCU型号 HC32F4A0PIHB
库版本 HC32F4A0_DDL_Rev2.0.0
LWIP版本 lwip-STABLE-2_2_0_RC1
PHY型号 YT8512
MDK版本 5.37
PHY接口 RMII
IP IPv4

导入库

在Lwip 2.2.x版本,apps文件夹中,已经预置了httpd服务。结构如下:

$ tree apps/http/ -aAC
apps/http/
├── altcp_proxyconnect.c
├── fs
│   ├── 404.html
│   ├── img
│   │   └── sics.gif
│   └── index.html
├── fs.c
├── fsdata.c
├── fsdata.h
├── http_client.c
├── httpd.c
├── httpd_structs.h
└── makefsdata
    ├── makefsdata
    ├── makefsdata.c
    ├── readme.txt
    └── tinydir.h

其中需要包含到工程中的文件有fs.chttpd.c,其他相关依赖会自动包含。

使用

httpd依赖于tcp,在lwip_lwipopts.h中使能tcp

/* ---------- TCP options ---------- */ 
#define LWIP_TCP                        1
#define TCP_TTL                         255
#define LWIP_TCP_KEEPALIVE              1

在初始化lwip完成后初始化httpd

  lwip_init();
  netif_init();
  httpd_init();

为了便于测试,在lwip_lwipopts.h中开启debug功能

#define LWIP_DEBUG
//#define ETHARP_DEBUG                  LWIP_DBG_ON
//#define PBUF_DEBUG                    LWIP_DBG_ON
//#define NETIF_DEBUG                   LWIP_DBG_ON
#define HTTPD_DEBUG                   LWIP_DBG_ON

然后编译烧录即可

运行测试

开启了mdns后,可以直接以域名的方式进行访问,在浏览器中输入lwip.local,访问成功会出现如下页面:

log如下:

httpd_init
mdns status[netif 0][service 0]: 1
http_accept 1ffead78 / 00000000
http_recv: pcb=1ffead78 pbuf=1ffedd84 err=Ok.
Received 487 bytes
First pbuf
CRLF received, parsing request
Received GET request"
Received "GET" request for URI: "/"
Looking for /index.shtml...
Looking for /index.ssi...
Looking for /index.shtm...
Looking for /index.html...
Opened.
http_recv: data 000504b3 len 1881
http_send: pcb=1ffead78 hs=1ffe698c left=1881
Trying to send 1881 bytes
Sent 1881 bytes
End of file.
Closing connection 1ffead78
http_accept 1ffeacd8 / 00000000
http_recv: pcb=1ffeacd8 pbuf=1ffedd84 err=Ok.
Received 401 bytes
First pbuf
CRLF received, parsing request
Received GET request"
Received "GET" request for URI: "/img/sics.gif"
Opening /img/sics.gif
http_recv: data 0004fe84 len 853
http_send: pcb=1ffeacd8 hs=1ffe69e0 left=853
Trying to send 853 bytes
Sent 853 bytes
End of file.
Closing connection 1ffeacd8
http_accept 1ffeac38 / 00000000
http_poll: pcb=1ffeac38 hs=1ffe69e0 pcb_state=ESTABLISHED

占坑待续

posted @ 2023-12-04 11:07  USTHzhanglu  阅读(743)  评论(0编辑  收藏  举报