STM32 H7 上配置 LwIP 的 mDNS

STM32 H7 上配置 LwIP 的 mDNS

  • HAL库版本:STM32Cube_FW_H7_V1.11.0
  • LwIP版本:V2.1.2(HAL库中自带的LwIP)

1 使能 IGMP

mDNS 依赖 IGMP 协议,这里先使能 IGMP 协议。

  • lwipopts.h 文件中添加
#define LWIP_ICMP                    1
  • ethernetif.clow_level_init() 函数中打开网卡的 IGMP 功能
netif->flags |= NETIF_FLAG_IGMP;
  • STM32 H7 中的 MAC 默认会打开广播、屏蔽组播,需要配置 MAC 过滤器。同样在 ethernetif.clow_level_init() 函数中添加
/* 从 ETH 寄存器中获取 Mac 过滤器参数 */
ETH_MACConfigTypeDef MACConf = {0};
ETH_MACFilterConfigTypeDef MACFilterConfig;

EthHandle.Instance = ETH;
/* 此处省略一部分 */

/* 从 ETH 寄存器中获取默认的 Mac 过滤器参数 */
HAL_ETH_GetMACFilterConfig(&EthHandle, &MACFilterConfig);
  
/* 使能组播过滤器, 接收组播消息 */
MACFilterConfig.PassAllMulticast =  ENABLE;

/* 设置 Mac 过滤器参数 */
HAL_ETH_SetMACFilterConfig(&EthHandle, &MACFilterConfig);
  • 如果需要屏蔽广播, 可以将 netif->flags 中的 NETIF_FLAG_BROADCAST 标志位取消掉,或者添加下面代码从 MAC 层屏蔽掉广播
/* 从 ETH 寄存器中获取默认的 Mac 过滤器参数 */
HAL_ETH_GetMACFilterConfig(&EthHandle, &MACFilterConfig);
  
/* 关闭广播过滤器, 拒绝所有广播消息 */
MACFilterConfig.BroadcastFilter = DISABLE;
  
/* 设置 Mac 过滤器参数 */
HAL_ETH_SetMACFilterConfig(&EthHandle, &MACFilterConfig);

2 使能 mDNS

  • LwIP/src/apps/mdns/mdns.c 文件添加到工程中
    在这里插入图片描述
  • lwipopts.h 文件中添加
/* 使能 mDNS */
#define LWIP_MDNS_RESPONDER           1

/* 每个 Netif 最大的服务数量, 如果不设置, 则默认为 1 */
#define MDNS_MAX_SERVICES             1

/* mDNS 需要 Netif 上的 1 个条目, 如果之前没有, 则设为 1, 如果存在则增加 1 */
#define LWIP_NUM_NETIF_CLIENT_DATA    1

/* mDNS 需要 1 个udp pcb 块, 在之前的基础上增加 1 */
#define MEMP_NUM_UDP_PCB              5
  • 添加下列函数, 并在网络初始化的时候调用 mdns_init() 函数
static void srv_txt(struct mdns_service *service, void *txt_userdata)
{
  err_t res;
  LWIP_UNUSED_ARG(txt_userdata);
  
  res = mdns_resp_add_service_txtitem(service, "path=/", 6);
  LWIP_ERROR("mdns add service txt failed\n", (res == ERR_OK), return);
}

static void mdns_report(struct netif* netif, u8_t result)
{
  if (result == MDNS_PROBING_SUCCESSFUL) {
    LWIP_PLATFORM_DIAG(("mdns status[netif %d]: MDNS_PROBING_SUCCESSFUL\n", netif->num));
  } else if (result == MDNS_PROBING_SUCCESSFUL) {
    LWIP_PLATFORM_DIAG(("mdns status[netif %d]: MDNS_PROBING_CONFLICT \n", netif->num));
  } else {
    LWIP_PLATFORM_DIAG(("mdns status[netif %d]: %d \n", netif->num, result));
  }
}

void mdns_init(void)
{
  mdns_resp_register_name_result_cb(mdns_report);
  mdns_resp_init();
  /* 添加 mDNS 主机, 在支持 mDNS 的电脑上, 可以直接 ping lwip.local 得到 IP 地址 */
  mdns_resp_add_netif(&gnetif, "lwip", 3600);
  /* 在 mDNS 主机上注册服务, 表示可以通过访问 lwip.local:80 端口访问开发板上的 web 服务器 */
  mdns_resp_add_service(&gnetif, "myweb", "_http", DNSSD_PROTO_TCP, 80, 3600, srv_txt, NULL);
}
  • 手动在 IP 会发生变化的地方调用 mdns_resp_announcer() 函数,在每次网络接口端口断开重连的时候调用 mdns_resp_restart() 函数。或者,在 lwipopts.h 中添加
#define LWIP_NETIF_EXT_STATUS_CALLBACK    1 
#define MDNS_RESP_USENETIF_EXTCALLBACK    1

mDNS 会在网络接口断开重连或者 IP 发生变化的时候,自动调用 mdns_resp_restart() 函数和 mdns_resp_announce() 函数。

3 测试

  • ping lwip.local

在这里插入图片描述

  • 在游览器中访问 lwip.local

在这里插入图片描述

4 实验代码

完整的实验代码

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
INTRODUCTION lwIP is a small independent implementation of the TCP/IP protocol suite. The focus of the lwIP TCP/IP implementation is to reduce the RAM usage while still having a full scale TCP. This making lwIP suitable for use in embedded systems with tens of kilobytes of free RAM and room for around 40 kilobytes of code ROM. lwIP was originally developed by Adam Dunkels at the Computer and Networks Architectures (CNA) lab at the Swedish Institute of Computer Science (SICS) and is now developed and maintained by a worldwide network of developers. FEATURES * IP (Internet Protocol, IPv4 and IPv6) including packet forwarding over multiple network interfaces * ICMP (Internet Control Message Protocol) for network maintenance and debugging * IGMP (Internet Group Management Protocol) for multicast traffic management * MLD (Multicast listener discovery for IPv6). Aims to be compliant with RFC 2710. No support for MLDv2 * ND (Neighbor discovery and stateless address autoconfiguration for IPv6). Aims to be compliant with RFC 4861 (Neighbor discovery) and RFC 4862 (Address autoconfiguration) * DHCP, AutoIP/APIPA (Zeroconf), ACD (Address Conflict Detection) and (stateless) DHCPv6 * UDP (User Datagram Protocol) including experimental UDP-lite extensions * TCP (Transmission Control Protocol) with congestion control, RTT estimation fast recovery/fast retransmit and sending SACKs * raw/native API for enhanced performance * Optional Berkeley-like socket API * TLS: optional layered TCP ("altcp") for nearly transparent TLS for any TCP-based protocol (ported to mbedTLS) (see changelog for more info) * PPPoS and PPPoE (Point-to-point protocol over Serial/Ethernet) * DNS (Domain name resolver incl. mDNS) * 6LoWPAN (via IEEE 802.15.4, BLE or ZEP) APPLICATIONS * HTTP server with SSI and CGI (HTTPS via altcp) * SNMPv2c agent with MIB compiler (Simple Network Management Protocol), v3 via altcp * SNTP (Simple network time protocol) * NetBIOS name service responder * MDNS (Multicast DNS) responder * iPerf server implementation * MQTT client (TLS support via altcp) LICENSE lwIP is freely available under a BSD license. DEVELOPMENT lwIP has grown into an excellent TCP/IP stack for embedded devices, and developers using the stack often submit bug fixes, improvements, and additions to the stack to further increase its usefulness. Development of lwIP is hosted on Savannah, a central point for software development, maintenance and distribution. Everyone can help improve lwIP by use of Savannah's interface, Git and the mailing list. A core team of developers will commit changes to the Git source tree. The lwIP TCP/IP stack is maintained in the 'lwip' Git module and contributions (such as platform ports) are in the 'contrib' Git module. See doc/savannah.txt for details on Git server access for users and developers. The current Git trees are web-browsable: http://git.savannah.gnu.org/cgit/lwip.git http://git.savannah.gnu.org/cgit/lwip/lwip-contrib.git Submit patches and bugs via the lwIP project page: http://savannah.nongnu.org/projects/lwip/ Continuous integration builds (GCC, clang): https://travis-ci.org/lwip-tcpip/lwip DOCUMENTATION Self documentation of the source code is regularly extracted from the current Git sources and is available from this web page: http://www.nongnu.org/lwip/ Also, there are mailing lists you can subscribe at http://savannah.nongnu.org/mail/?group=lwip plus searchable archives: http://lists.nongnu.org/archive/html/lwip-users/ http://lists.nongnu.org/archive/html/lwip-devel/ There is a wiki about lwIP at http://lwip.wikia.com/wiki/LwIP_Wiki You might get questions answered there, but unfortunately, it is not as well maintained as it should be. lwIP was originally written by Adam Dunkels: http://dunkels.com/adam/ Reading Adam's papers, the files in docs/, browsing the source code documentation and browsing the mailing list archives is a good way to become familiar with the design of lwIP. Adam Dunkels Leon Woestenberg

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值