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
本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!
