Toggle navigation
首页
问答
文章
积分商城
专家
专区
更多专区...
文档中心
返回主站
搜索
提问
会员
中心
登录
注册
WIZnet_W5500
bug:w5500 DHCP续租逻辑问题
发布于 2022-04-22 19:14:39 浏览:2133
订阅该版
[W5500 IP 续约会重新 DHCP_init() 的问题](https://club.rt-thread.org/ask/question/434365.html) 之前就这个问题反馈过一次,当时写的有点乱,而且有那样改有问题,比如网线插拔无法重新DHCP。这次有时间准备彻底完结它 **1. DHCP步骤** ![dhcp.png](https://oss-club.rt-thread.org/uploads/20220422/16589551123d4432d4b47f480277bf9f.png.webp) DHCP的实现分为4步,分别是: 第一步:Client端在局域网内发起一个DHCP Discover包,目的是想发现能够给它提供IP的DHCP Server。 第二步:可用的DHCP Server接收到Discover包之后,通过发送DHCP Offer包给予Client端应答,意在告诉Client端它可以提供IP地址。 第三步:Client端接收到Offer包之后,发送DHCP Request包请求分配IP。 第四步:DHCP Server发送ACK数据包,确认信息。 **2. W5500 DHCP抓包** ![Snipaste_2022-04-22_17-29-38.png](https://oss-club.rt-thread.org/uploads/20220422/c29ccf066d38c2d1465960a203f00824.png) 软件包 的DHCP过程逻辑有问题,看了源码,rtthread的W5500包移植的是官方 wiznet的包, wiznet的包主要用在裸机上,他的DHCP_run主要用在单线程上,状态机实现的,但是这个软件包 将它用在了工作队列上,像一个闹钟,到时间才来处理,但是却没能很好的协调状态机里的状态,遇到需要续租的时候,直接打断了DHCP_run(), 然后强行DHCP_stop,然后在重新DHCP. **解决办法** 代码修改: wiz_dhcp_work ``` static void wiz_dhcp_work(struct rt_work *dhcp_work, void *dhcp_work_data) { #define WIZ_DHCP_WORK_RETRY 3 /* DHCP will have 3 times handshake */ #define WIZ_DHCP_WORK_RETRY_TIME (2 * RT_TICK_PER_SECOND) static int wiz_dhcp_retry_times = WIZ_DHCP_WORK_RETRY * 20; RT_ASSERT(dhcp_work_data != RT_NULL); struct netdev *netdev = (struct netdev *)dhcp_work_data; uint8_t dhcp_times = 0; uint32_t dhcp_work_times; static uint8_t data_buffer[1024]; static uint32_t dhcp_status = DHCP_FAILED; if(dhcp_status == DHCP_FAILED) { DHCP_init(WIZ_DHCP_SOCKET, data_buffer); rt_timer_start(dhcp_timer); } while (1) { /* DHCP start, return DHCP_IP_LEASED is success. */ dhcp_status = DHCP_run(); switch (dhcp_status) { case DHCP_IP_ASSIGN: case DHCP_IP_CHANGED: { /* to update netdev information */ wiz_netdev_info_update(netdev, RT_FALSE); break; } case DHCP_IP_LEASED: { int hour, min; //DHCP_stop(); //rt_timer_stop(dhcp_timer); /* to update netdev information */ wiz_netdev_info_update(netdev, RT_FALSE); /* reset the previous work configure */ rt_work_cancel(dhcp_work); dhcp_work_times = (getDHCPTick1s() > getDHCPLeasetime() / 2) ? 0 : getDHCPLeasetime() / 2 - getDHCPTick1s(); /* according to the DHCP leaset time, config next DHCP produce */ rt_work_submit(dhcp_work, (dhcp_work_times+1) * RT_TICK_PER_SECOND); hour = getDHCPLeasetime() / 3600; min = (getDHCPLeasetime() % 3600) / 60; LOG_D("DHCP countdown to lease renewal [%dH: %dMin], retry time[%04d]", hour, min, dhcp_times); //wiz_dhcp_retry_times = dhcp_times; wiz_dhcp_retry_times = WIZ_DHCP_WORK_RETRY * 20; return; } case DHCP_STOPPED: case DHCP_FAILED: { //DHCP_stop(); dhcp_times = wiz_dhcp_retry_times; LOG_E("dhcp handshake failed!"); break; } default: { dhcp_times++; /* DHCP_RUNNING status, include don't receive data */ rt_thread_mdelay(10); break; } } if (dhcp_times >= wiz_dhcp_retry_times) { LOG_D("DHCP work in %d seconds, [%03d|%03d]", WIZ_DHCP_WORK_RETRY_TIME / RT_TICK_PER_SECOND, dhcp_times, wiz_dhcp_retry_times); /* if dhcp service is too busy to manger IP, increase retry times */ wiz_dhcp_retry_times = wiz_dhcp_retry_times + WIZ_DHCP_WORK_RETRY; DHCP_stop(); dhcp_status = DHCP_FAILED; rt_timer_stop(dhcp_timer); rt_work_cancel(dhcp_work); /* according to WIZ_DHCP_WORK_RETRY_TIME, reconfigure in 2 seconds */ rt_work_submit(dhcp_work, WIZ_DHCP_WORK_RETRY_TIME); break; } } } ``` 代码修改: DHCP_run ``` uint8_t DHCP_run(void) { uint8_t type; uint8_t ret; if(dhcp_state == STATE_DHCP_STOP) return DHCP_STOPPED; if(getSn_SR(DHCP_SOCKET) != SOCK_UDP) wizchip_socket(DHCP_SOCKET, Sn_MR_UDP, DHCP_CLIENT_PORT, 0x00); ret = DHCP_RUNNING; type = parseDHCPMSG(); switch ( dhcp_state ) { case STATE_DHCP_INIT : DHCP_allocated_ip[0] = 0; DHCP_allocated_ip[1] = 0; DHCP_allocated_ip[2] = 0; DHCP_allocated_ip[3] = 0; send_DHCP_DISCOVER(); dhcp_state = STATE_DHCP_DISCOVER; break; case STATE_DHCP_DISCOVER : if (type == DHCP_OFFER){ #ifdef _DHCP_DEBUG_ printf("> Receive DHCP_OFFER\r\n"); #endif DHCP_allocated_ip[0] = pDHCPMSG->yiaddr[0]; DHCP_allocated_ip[1] = pDHCPMSG->yiaddr[1]; DHCP_allocated_ip[2] = pDHCPMSG->yiaddr[2]; DHCP_allocated_ip[3] = pDHCPMSG->yiaddr[3]; send_DHCP_REQUEST(); dhcp_state = STATE_DHCP_REQUEST; } else ret = check_DHCP_timeout(); break; case STATE_DHCP_REQUEST : if (type == DHCP_ACK) { #ifdef _DHCP_DEBUG_ printf("> Receive DHCP_ACK\r\n"); #endif if (check_DHCP_leasedIP()) { // Network info assignment from DHCP dhcp_ip_assign(); reset_DHCP_timeout(); dhcp_state = STATE_DHCP_LEASED; } else { // IP address conflict occurred reset_DHCP_timeout(); dhcp_ip_conflict(); dhcp_state = STATE_DHCP_INIT; } } else if (type == DHCP_NAK) { #ifdef _DHCP_DEBUG_ printf("> Receive DHCP_NACK\r\n"); #endif reset_DHCP_timeout(); dhcp_state = STATE_DHCP_DISCOVER; } else ret = check_DHCP_timeout(); break; case STATE_DHCP_LEASED : //ret = DHCP_IP_LEASED; if ((dhcp_lease_time != INFINITE_LEASETIME) && ((dhcp_lease_time/2) < dhcp_tick_1s)) { #ifdef _DHCP_DEBUG_ printf("> Maintains the IP address \r\n"); #endif type = 0; OLD_allocated_ip[0] = DHCP_allocated_ip[0]; OLD_allocated_ip[1] = DHCP_allocated_ip[1]; OLD_allocated_ip[2] = DHCP_allocated_ip[2]; OLD_allocated_ip[3] = DHCP_allocated_ip[3]; DHCP_XID++; send_DHCP_REQUEST(); reset_DHCP_timeout(); dhcp_state = STATE_DHCP_REREQUEST; } else { ret = DHCP_IP_LEASED; } break; case STATE_DHCP_REREQUEST : //ret = DHCP_IP_LEASED; if (type == DHCP_ACK) { dhcp_retry_count = 0; if (OLD_allocated_ip[0] != DHCP_allocated_ip[0] || OLD_allocated_ip[1] != DHCP_allocated_ip[1] || OLD_allocated_ip[2] != DHCP_allocated_ip[2] || OLD_allocated_ip[3] != DHCP_allocated_ip[3]) { ret = DHCP_IP_CHANGED; dhcp_ip_update(); #ifdef _DHCP_DEBUG_ printf(">IP changed.\r\n"); #endif } #ifdef _DHCP_DEBUG_ else printf(">IP is continued.\r\n"); #endif reset_DHCP_timeout(); dhcp_state = STATE_DHCP_LEASED; } else if (type == DHCP_NAK) { #ifdef _DHCP_DEBUG_ printf("> Receive DHCP_NACK, Failed to maintain ip\r\n"); #endif reset_DHCP_timeout(); dhcp_state = STATE_DHCP_DISCOVER; } else ret = check_DHCP_timeout(); break; default : break; } return ret; } ``` 代码修改:wiz_link_status_thread_entry ``` static void wiz_link_status_thread_entry(void *parameter) { #define WIZ_PHYCFGR_LINK_STATUS 0x01 uint8_t phycfgr = 0; struct netdev *netdev = RT_NULL; netdev = netdev_get_by_name(wiz_netdev_name); if (netdev == RT_NULL) { LOG_E("don`t find device(%s)", wiz_netdev_name); return; } while (1) { /* Get PHYCFGR data */ phycfgr = getPHYCFGR(); /* If the register contents are different from the struct contents, the struct needs to be updated */ if ((phycfgr & WIZ_PHYCFGR_LINK_STATUS) != ((netdev->flags & NETDEV_FLAG_LINK_UP) ? RT_TRUE : RT_FALSE)) { if (phycfgr & WIZ_PHYCFGR_LINK_STATUS) { wiz_socket_init(); #ifdef WIZ_USING_DHCP if(dhcp_work) { DHCP_stop(); rt_work_submit(dhcp_work, RT_WAITING_NO); } #else wiz_network_init(RT_TRUE); #endif netdev_low_level_set_link_status(netdev, phycfgr & WIZ_PHYCFGR_LINK_STATUS); wiz_netdev_info_update(netdev, RT_FALSE); LOG_I("%s netdev link status becomes link up", wiz_netdev_name); } else { netdev_low_level_set_link_status(netdev, phycfgr & WIZ_PHYCFGR_LINK_STATUS); if(dhcp_work) { rt_work_cancel(dhcp_work); } wiz_netdev_info_update(netdev, RT_TRUE); LOG_I("%s netdev link status becomes link down", wiz_netdev_name); } } rt_thread_mdelay(1000); } } ``` 在wizchip_dhcp.c 里增加函数 ``` uint32_t getDHCPTick1s(void) { return dhcp_tick_1s; } ``` **效果演示:** ![演示.gif](https://oss-club.rt-thread.org/uploads/20220422/43a6b857ddbd49851c64b908869479cb.gif)
查看更多
2
个回答
默认排序
按发布时间排序
mysterywolf
认证专家
2022-04-23
https://github.com/mysterywolf
你好 可否提一个PR?
xiangxistu
2022-04-24
这家伙很懒,什么也没写!
[该 PR 地址](https://github.com/RT-Thread-packages/wiznet/pull/68)
撰写答案
登录
注册新账号
关注者
0
被浏览
2.1k
关于作者
aingsu
这家伙很懒,什么也没写!
提问
5
回答
3
被采纳
0
关注TA
发私信
相关问题
1
W5500软件包开机无法解析link.rt-thread.org
2
W5500 如何实现断开重连?
3
W5500初始化全部都OK,电脑能ping板子,板子ping不通电脑
4
w5500 编译错误
5
求个SPI上挂W5500、Max6675两个设备的驱动例子
6
TFTP与W5500配合能传文件但无内容
7
wiznet(W5500) 设置MAC
8
SPI总线挂接2个W5500以太网芯片
9
多网卡建立链路问题,求官方解答
10
为什么W5500一直显示无法挂载
推荐文章
1
RT-Thread应用项目汇总
2
玩转RT-Thread系列教程
3
国产MCU移植系列教程汇总,欢迎查看!
4
机器人操作系统 (ROS2) 和 RT-Thread 通信
5
五分钟玩转RT-Thread新社区
6
【技术三千问】之《玩转ART-Pi》,看这篇就够了!干货汇总
7
关于STM32H7开发板上使用SDIO接口驱动SD卡挂载文件系统的问题总结
8
STM32的“GPU”——DMA2D实例详解
9
RT-Thread隐藏的宝藏之completion
10
【ART-PI】RT-Thread 开启RTC 与 Alarm组件
最新文章
1
使用百度AI助手辅助编写一个rt-thread下的ONVIF设备发现功能的功能代码
2
RT-Thread 发布 EtherKit开源以太网硬件!
3
rt-thread使用cherryusb实现虚拟串口
4
《C++20 图形界面程序:速度与渲染效率的双重优化秘籍》
5
《原子操作:程序世界里的“最小魔法单位”解析》
热门标签
RT-Thread Studio
串口
Env
LWIP
SPI
AT
Bootloader
Hardfault
CAN总线
FinSH
ART-Pi
USB
DMA
文件系统
RT-Thread
SCons
RT-Thread Nano
线程
MQTT
STM32
RTC
FAL
rt-smart
ESP8266
I2C_IIC
WIZnet_W5500
UART
ota在线升级
PWM
cubemx
freemodbus
flash
packages_软件包
BSP
潘多拉开发板_Pandora
定时器
ADC
GD32
flashDB
socket
中断
Debug
编译报错
msh
SFUD
keil_MDK
rt_mq_消息队列_msg_queue
ulog
C++_cpp
at_device
本月问答贡献
踩姑娘的小蘑菇
7
个答案
3
次被采纳
a1012112796
13
个答案
2
次被采纳
张世争
9
个答案
2
次被采纳
rv666
5
个答案
2
次被采纳
用户名由3_15位
11
个答案
1
次被采纳
本月文章贡献
程序员阿伟
8
篇文章
2
次点赞
hhart
3
篇文章
4
次点赞
大龄码农
1
篇文章
3
次点赞
ThinkCode
1
篇文章
1
次点赞
Betrayer
1
篇文章
1
次点赞
回到
顶部
发布
问题
分享
好友
手机
浏览
扫码手机浏览
投诉
建议
回到
底部