LWIP配置主机名称和设置获取IP回调函数

一、设置主机名称

主机名称指的是当我们的设备连接上路由器之后在路由
器中显示的名字。比如当我们的华为手机连接上路由器之后,路由器中会显示我们的手机名称为HUAWEI_MATE30。

在lwip中我们可以通过netif->hostname字段设置这个值,但是设置前需要打开两个宏定义:

#define CONFIG_NETIF_HOSTNAME

#define LWIP_NETIF_HOSTNAME 1

然后才能通过LWIP_NETIF_HOSTNAME_DEFAULT 这个宏正常设置我们的hostname,否则设置无效。

#define CONFIG_NETIF_HOSTNAME 
#define LWIP_NETIF_HOSTNAME 1
#define LWIP_NETIF_HOSTNAME_DEFAULT "设备名"

我们可以通过netif_set_hostname函数修改hostname,我们也可以直接修改netif结构体本身修改hostname

#define netif_set_hostname(netif, name) do { if ((netif) != NULL) { (netif)->hostname = name; } } while (0)

二、设置获取IP的回调函数

当我们的设备连接上路由器之后,我们需要能够及时获取设备的IP信息,LWIP提供了一个回调函数用来通知获取到IP事件:

netif_status_callback_fn link_callback;
typedef void (*netif_status_callback_fn)(struct netif *netif);

示例:

 static void netif_status_callback_func(struct netif *netif)
 {
    printf("Got Ip\r\n");
    char *ip_str = NULL;
	char *netmask_str =NULL;
	char *gw_str = NULL;
	
    ip4_addr_t ipaddr;
    ip4_addr_t netmask;
    ip4_addr_t gw;
    netifapi_netif_get_addr(g_staNetif,&ipaddr,&netmask,&gw);

	ip_str = ip4addr_ntoa(&ipaddr);
	printf("ip:%s\r\n",ip_str);
	netmask_str = ip4addr_ntoa(&netmask);
	printf("netmask:%s\r\n",netmask_str);
	gw_str = ip4addr_ntoa(&gw);
	printf("gw_str:%s\r\n",gw_str);
 }
g_Netif->status_callback = netif_status_callback_func;

netif结构体的说明请参考:https://blog.csdn.net/weixin_39270987/article/details/109210417

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值