LwIP雖然使用超時鏈表進行管理所有的超時事件,那么它首先需要知道有哪些超時事件才能去管理,而這些超時事件就是通過注冊的方式被掛載在鏈表上,簡單來說就是這些超時事件要在內核中登記一下,內核才會去處理,LwIP中注冊超時事件的函數是sys\_timeout(),但是實際上是調用sys\_timeout\_abs()函數,具體見代碼清單 9?2。
```
1 void
2 sys_timeout(u32_t msecs, sys_timeout_handler handler, void *arg)
3 {
4 u32_t next_timeout_time;
5 LWIP_ASSERT_CORE_LOCKED();
6
7 /* overflow handled by TIME_LESS_THAN macro */
8 next_timeout_time = (u32_t)(sys_now() + msecs); (1)
9
10 sys_timeout_abs(next_timeout_time, handler, arg);
11 }
12
13 static void
14 sys_timeout_abs(u32_t abs_time, sys_timeout_handler handler, void *arg)
15 {
16 struct sys_timeo *timeout, *t;
17
18 timeout = (struct sys_timeo *)memp_malloc(MEMP_SYS_TIMEOUT); (2)
19 if (timeout == NULL)
20 {
21 return;
22 }
23
24 timeout->next = NULL;
25 timeout->h = handler;
26 timeout->arg = arg;
27 timeout->time = abs_time; (3)
28
29 if (next_timeout == NULL)
30 {
31 next_timeout = timeout; (4)
32 return;
33 }
34 if (TIME_LESS_THAN(timeout->time, next_timeout->time))
35 {
36 timeout->next = next_timeout;
37 next_timeout = timeout; (5)
38 }
39 else
40 {
41 for (t = next_timeout; t != NULL; t = t->next)
42 {
43 if ((t->next == NULL) ||
44 TIME_LESS_THAN(timeout->time, t->next->time))
45 {
46 timeout->next = t->next;
47 t->next = timeout; (6)
48 break;
49 }
50 }
51 }
52 }
```
(1):根據當前時間計算出超時的時間,然后調用sys_timeout_abs()函數將當前事件插入超時鏈表。
(2):從內存池中申請一個MEMP_SYS_TIMEOUT類型內存,保存對應超時事件的相關信息。
(3):填寫對應的超時事件信息,超時回調函數、函數參數、超時的 時間。
(4):如果超時鏈表中沒有超時事件,那么新添加的事件就是鏈表的第一個。
(5):如果新插入的超時事件比鏈表上第一個事件的時間短,則將新插入的超時事件設置成鏈表的第一個。
(6):遍歷鏈表,尋找合適的插入節點,超時鏈表根據超時事件的時間升序排列。
在timeouts.c中,有一個名字為lwip_cyclic_timer的結構,LwIP使用該結構存放了其內部使用的循環超時事件。這些超時事件在LwIP初始化時通過函數sys_timeouts_init()調用定時器注冊函數sys_timeout()注冊進入超時鏈表中,lwip_cyclic_timer的結構具體見:
```
1 #define TCP_TMR_INTERVAL 250
2 #define IP_TMR_INTERVAL 1000
3 #define ARP_TMR_INTERVAL 1000
4
5 struct lwip_cyclic_timer
6 {
7 u32_t interval_ms;
8 lwip_cyclic_timer_handler handler;
9 };
10
11 const struct lwip_cyclic_timer lwip_cyclic_timers[] =
12 {
13 {TCP_TMR_INTERVAL, HANDLER(tcp_tmr)},
14
15 {IP_TMR_INTERVAL, HANDLER(ip_reass_tmr)},
16
17 {ARP_TMR_INTERVAL, HANDLER(etharp_tmr)},
18 };
```
lwip\_cyclic\_timers數組中存放了每個周期性的超時事件回調函數及超時時間,在LwIP初始化的時候就將這些事件一個個插入超時鏈表中,具體見代碼清單 9?4。
```
1 void sys_timeouts_init(void)
2 {
3 size_t i;
4
5 for(i =(LWIP_TCP ? 1 : 0);i < LWIP_ARRAYSIZE(lwip_cyclic_timers); i++)
6 {
7 sys_timeout(lwip_cyclic_timers[i].interval_ms,lwip_cyclic_timer,
8 LWIP_CONST_CAST(void *, &lwip_cyclic_timers[i]));
9 }
10 }
```
插入超時鏈表后的示意圖具體見圖 9?2。

每個sys\_timeo結構體中的h成員變量記錄著對應的超時回調函數,對于周期性的回調函數,LwIP是這樣子處理的:在初始化的時候將他們注冊到 lwip\_cyclic\_timer()函數中,每次在處理回調函數之后,就調用sys\_timeout\_abs()函數將其重新注冊到超時鏈表中,具體見代碼清單 9?5。
```
1 lwip_cyclic_timer(void *arg)
2 {
3 u32_t now;
4 u32_t next_timeout_time;
5 const struct lwip_cyclic_timer *cyclic = (const struct lwip_cyclic_timer *)arg;
6
7 cyclic->handler();
8
9 now = sys_now();
10 next_timeout_time = (u32_t)(current_timeout_due_time + cyclic->interval_ms);
11
12 if (TIME_LESS_THAN(next_timeout_time, now))
13 {
14 sys_timeout_abs((u32_t)(now + cyclic->interval_ms), lwip_cyclic_timer, arg);
15 }
16 else
17 {
18 sys_timeout_abs(next_timeout_time, lwip_cyclic_timer, arg);
19 }
20 }
```
- 說明
- 第1章:網絡協議簡介
- 1.1:常用網絡協議
- 1.2:網絡協議的分層模型
- 1.3:協議層報文間的封裝與拆封
- 第2章:LwIP簡介
- 2.1:LwIP的優缺點
- 2.2:LwIP的文件說明
- 2.2.1:如何獲取LwIP源碼文件
- 2.2.2:LwIP文件說明
- 2.3:查看LwIP的說明文檔
- 2.4:使用vscode查看源碼
- 2.4.1:查看文件中的符號列表(函數列表)
- 2.4.2:函數定義跳轉
- 2.5:LwIP源碼里的example
- 2.6:LwIP的三種編程接口
- 2.6.1:RAW/Callback API
- 2.6.2:NETCONN API
- 2.6.3:SOCKET API
- 第3章:開發平臺介紹
- 3.1:以太網簡介
- 3.1.1:PHY層
- 3.1.2:MAC子層
- 3.2:STM32的ETH外設
- 3.3:MII 和 RMII 接口
- 3.4:PHY:LAN8720A
- 3.5:硬件設計
- 3.6:軟件設計
- 3.6.1:獲取STM32的裸機工程模板
- 3.6.2:添加bsp_eth.c與bsp_eth.h
- 3.6.3:修改stm32f4xx_hal_conf.h文件
- 第4章:LwIP的網絡接口管理
- 4.1:netif結構體
- 4.2:netif使用
- 4.3:與netif相關的底層函數
- 4.4:ethernetif.c文件內容
- 4.4.1:ethernetif數據結構
- 4.4.2:ethernetif_init()
- 4.4.3:low_level_init()
- 第5章:LwIP的內存管理
- 5.1:幾種內存分配策略
- 5.1.1:固定大小的內存塊
- 5.1.2:可變長度分配
- 5.2:動態內存池(POOL)
- 5.2.1:內存池的預處理
- 5.2.2:內存池的初始化
- 5.2.3:內存分配
- 5.2.4:內存釋放
- 5.3:動態內存堆
- 5.3.1:內存堆的組織結構
- 5.3.2:內存堆初始化
- 5.3.3:內存分配
- 5.3.4:內存釋放
- 5.4:使用C庫的malloc和free來管理內存
- 5.5:LwIP中的配置
- 第6章:網絡數據包
- 6.1:TCP/IP協議的分層思想
- 6.2:LwIP的線程模型
- 6.3:pbuf結構體說明
- 6.4:pbuf的類型
- 6.4.1:PBUF_RAM類型的pbuf
- 6.4.2:PBUF_POOL類型的pbuf
- 6.4.3:PBUF_ROM和PBUF_REF類型pbuf
- 6.5:pbuf_alloc()
- 6.6:pbuf_free()
- 6.7:其它pbuf操作函數
- 6.7.1:pbuf_realloc()
- 6.7.2:pbuf_header()
- 6.7.3:pbuf_take()
- 6.8:網卡中使用的pbuf
- 6.8.1:low_level_output()
- 6.8.2:low_level_input()
- 6.8.3:ethernetif_input()
- 第7章:無操作系統移植LwIP
- 7.1:將LwIP添加到裸機工程
- 7.2:移植頭文件
- 7.3:移植網卡驅動
- 7.4:LwIP時基
- 7.5:協議棧初始化
- 7.6:獲取數據包
- 7.6.1:查詢方式
- 7.6.2:ping命令詳解
- 7.6.3:中斷方式
- 第8章:有操作系統移植LwIP
- 8.1:LwIP中添加操作系統
- 8.1.1:拷貝FreeRTOS源碼到工程文件夾
- 8.1.2:添加FreeRTOS源碼到工程組文件夾
- 8.1.3:指定FreeRTOS頭文件的路徑
- 8.1.4:修改stm32f10x_it.c
- 8.2:lwipopts.h文件需要加入的配置
- 8.3:sys_arch.c/h文件的編寫
- 8.4:網卡底層的編寫
- 8.5:協議棧初始化
- 8.6:移植后使用ping測試基本響應
- 第9章:LwIP一探究竟
- 9.1:網卡接收數據的流程
- 9.2:內核超時處理
- 9.2.1:sys_timeo結構體與超時鏈表
- 9.2.2:注冊超時事件
- 9.2.3:超時檢查
- 9.3:tcpip_thread線程
- 9.4:LwIP中的消息
- 9.4.1:消息結構
- 9.4.2:數據包消息
- 9.4.3:API消息
- 9.5:揭開LwIP神秘的面紗
- 第10章:ARP協議
- 10.1:鏈路層概述
- 10.2:MAC地址的基本概念
- 10.3:初識ARP
- 10.4:以太網幀結構
- 10.5:IP地址映射為物理地址
- 10.6:ARP緩存表
- 10.7:ARP緩存表的超時處理
- 10.8:ARP報文
- 10.9:發送ARP請求包
- 10.10:數據包接收流程
- 10.10.1:以太網之數據包接收
- 10.10.2:ARP數據包處理
- 10.10.3:更新ARP緩存表
- 10.11:數據包發送流程
- 10.11.1:etharp_output()函數
- 10.11.2:etharp_output_to_arp_index()函數
- 10.11.3:etharp_query()函數
- 第11章:IP協議
- 11.1:IP地址.md
- 11.1.1:概述
- 11.1.2:IP地址編址
- 11.1.3:特殊IP地址