1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
39
40
41
46
47
48
49
50
51
53
54
57
58
60
61
62
63
64
65
67
68
69
70
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
108
109
110
111
112
113
114
115
116
117
118
119
121
122
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
145
146
148
149
150
151
152
153
154
155
156
157
158
159
161
162
163
164
165
166
167
168
169
170
171
/* ... */
#include "lwip/opt.h"
#include "main.h"
#if LWIP_DHCP
#include "lwip/dhcp.h"
#endif
#include "app_ethernet.h"
#include "ethernetif.h"
#ifdef USE_LCD
#include "lcd_log.h"
#endif
Includes
#if LWIP_DHCP
#define MAX_DHCP_TRIES 4
__IO uint8_t DHCP_state = DHCP_OFF;/* ... */
#endif
Private variables
/* ... */
void ethernet_link_status_updated(struct netif *netif)
{
if (netif_is_up(netif))
{
#if LWIP_DHCP
DHCP_state = DHCP_START;/* ... */
#elif defined(USE_LCD)
uint8_t iptxt[20];
sprintf((char *)iptxt, "%s", ip4addr_ntoa(netif_ip4_addr(netif)));
LCD_UsrLog ("Static IP address: %s\n", iptxt);/* ... */
#else
BSP_LED_On(LED1);
BSP_LED_Off(LED2);/* ... */
#endif
}if (netif_is_up(netif)) { ... }
else
{
#if LWIP_DHCP
DHCP_state = DHCP_LINK_DOWN;/* ... */
#elif defined(USE_LCD)
LCD_UsrLog ("The network cable is not connected \n");
#else
BSP_LED_Off(LED1);
BSP_LED_On(LED2);/* ... */
#endif
}else { ... }
}{ ... }
#if LWIP_DHCP
/* ... */
void DHCP_Thread(void const * argument)
{
struct netif *netif = (struct netif *) argument;
ip_addr_t ipaddr;
ip_addr_t netmask;
ip_addr_t gw;
struct dhcp *dhcp;
#ifdef USE_LCD
uint8_t iptxt[20];
#endif
for (;;)
{
switch (DHCP_state)
{
case DHCP_START:
{
ip_addr_set_zero_ip4(&netif->ip_addr);
ip_addr_set_zero_ip4(&netif->netmask);
ip_addr_set_zero_ip4(&netif->gw);
DHCP_state = DHCP_WAIT_ADDRESS;
#ifdef USE_LCD
LCD_UsrLog (" State: Looking for DHCP server ...\n");
#else
BSP_LED_Off(LED1);
BSP_LED_Off(LED2);/* ... */
#endif
dhcp_start(netif);
...}
break;case DHCP_START:
case DHCP_WAIT_ADDRESS:
{
if (dhcp_supplied_address(netif))
{
DHCP_state = DHCP_ADDRESS_ASSIGNED;
#ifdef USE_LCD
sprintf((char *)iptxt, "%s", ip4addr_ntoa(netif_ip4_addr(netif)));
LCD_UsrLog ("IP address assigned by a DHCP server: %s\n", iptxt);/* ... */
#else
BSP_LED_On(LED1);
BSP_LED_Off(LED2);/* ... */
#endif
}if (dhcp_supplied_address(netif)) { ... }
else
{
dhcp = (struct dhcp *)netif_get_client_data(netif, LWIP_NETIF_CLIENT_DATA_INDEX_DHCP);
if (dhcp->tries > MAX_DHCP_TRIES)
{
DHCP_state = DHCP_TIMEOUT;
IP_ADDR4(&ipaddr, IP_ADDR0 ,IP_ADDR1 , IP_ADDR2 , IP_ADDR3 );
IP_ADDR4(&netmask, NETMASK_ADDR0, NETMASK_ADDR1, NETMASK_ADDR2, NETMASK_ADDR3);
IP_ADDR4(&gw, GW_ADDR0, GW_ADDR1, GW_ADDR2, GW_ADDR3);
netif_set_addr(netif, ip_2_ip4(&ipaddr), ip_2_ip4(&netmask), ip_2_ip4(&gw));
#ifdef USE_LCD
sprintf((char *)iptxt, "%s", ip4addr_ntoa(netif_ip4_addr(netif)));
LCD_UsrLog ("DHCP Timeout !! \n");
LCD_UsrLog ("Static IP address: %s\n", iptxt);/* ... */
#else
BSP_LED_On(LED1);
BSP_LED_Off(LED2);/* ... */
#endif
}if (dhcp->tries > MAX_DHCP_TRIES) { ... }
}else { ... }
...}
break;case DHCP_WAIT_ADDRESS:
case DHCP_LINK_DOWN:
{
DHCP_state = DHCP_OFF;
#ifdef USE_LCD
LCD_UsrLog ("The network cable is not connected \n");
#else
BSP_LED_Off(LED1);
BSP_LED_On(LED2);/* ... */
#endif
...}
break;case DHCP_LINK_DOWN:
default: break;
}switch (DHCP_state) { ... }
osDelay(500);
}for (;;) { ... }
}{ ... }
#endif/* ... */ Private functions