forked from remi205/stm32-bsp
/
dhcp_config.c
executable file
·201 lines (173 loc) · 5.78 KB
/
dhcp_config.c
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/* Includes ------------------------------------------------------------------*/
#include "lwip/opt.h"
#include "lwip/dhcp.h"
#include "app_ethernet.h"
#include "ethernetif.h"
#include <stdio.h>
#include "dns.h"
#include "net_config.h"
//#include "Serial.h"
#include "Console.h"
#define CONSOLE 0
#ifdef USE_DHCP
#define MAX_DHCP_TRIES 4
uint32_t DHCPfineTimer = 0;
__IO uint8_t DHCP_state;
#endif
#if 1
/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/
/**
* @brief Notify the User about the nework interface config status
* @param netif: the network interface
* @retval None
*/
void user_notification(struct netif *netif)
{
if (netif_is_up(netif))
{
#ifdef USE_DHCP
/* Update DHCP state machine */
DHCP_state = DHCP_START;
#else
uint8_t iptxt[80];
sprintf((char*)iptxt, "Static IP address: %d.%d.%d.%d", IP_ADDR0, IP_ADDR1, IP_ADDR2, IP_ADDR3);
WriteConsole( iptxt);
#endif /* USE_DHCP */
}
else
{
#ifdef USE_DHCP
/* Update DHCP state machine */
DHCP_state = DHCP_LINK_DOWN;
#endif /* USE_DHCP */
WriteConsole("The network cable is not connected \n");
}
}
#endif
#if 0
/**
* @brief This function notify user about link status changement.
* @param netif: the network interface
* @retval None
*/
void ethernetif_notify_conn_changed(struct netif *netif)
{
struct ip_addr ipaddr;
struct ip_addr netmask;
struct ip_addr gw;
if(netif_is_link_up(netif))
{
#ifdef USE_DHCP
WriteConsole ("The network cable is now connected \n");
/* Update DHCP state machine */
DHCP_state = DHCP_START;
#else
IP4_ADDR(&ipaddr, IP_ADDR0, IP_ADDR1, IP_ADDR2, IP_ADDR3);
IP4_ADDR(&netmask, NETMASK_ADDR0, NETMASK_ADDR1 , NETMASK_ADDR2, NETMASK_ADDR3);
IP4_ADDR(&gw, GW_ADDR0, GW_ADDR1, GW_ADDR2, GW_ADDR3);
uint8_t iptxt[80];
sprintf((char*)iptxt, "%d.%d.%d.%d", IP_ADDR0, IP_ADDR1, IP_ADDR2, IP_ADDR3);
WriteConsole ("The network cable is now connected \n");
sprintf((char*)iptxt, "Static IP address: %d.%d.%d.%d", IP_ADDR0, IP_ADDR1, IP_ADDR2, IP_ADDR3);
WriteConsole (iptxt);
#endif /* USE_DHCP */
netif_set_addr(netif, &ipaddr , &netmask, &gw);
/* When the netif is fully configured this function must be called.*/
netif_set_up(netif);
}
else
{
#ifdef USE_DHCP
/* Update DHCP state machine */
DHCP_state = DHCP_LINK_DOWN;
#endif /* USE_DHCP */
/* When the netif link is down this function must be called.*/
netif_set_down(netif);
WriteConsole ("The network cable is not connected \n");
}
}
#endif
#ifdef USE_DHCP
/**
* @brief DHCP Process
* @param argument: network interface
* @retval None
*/
void dhcp_process(void const * argument)
{
struct netif *netif = (struct netif *) argument;
struct ip_addr ipaddr;
struct ip_addr netmask;
struct ip_addr gw;
uint32_t IPaddress = 0;
int mscnt =0;
netif->ip_addr.addr = 0;
netif->netmask.addr = 0;
netif->gw.addr = 0;
dhcp_start(netif);
DHCP_state = DHCP_WAIT_ADDRESS;
// int DHCP_state = DHCP_START;
while( 1 )
{
osDelay(DHCP_FINE_TIMER_MSECS);
dhcp_fine_tmr();
mscnt += DHCP_FINE_TIMER_MSECS;
if (mscnt >= DHCP_COARSE_TIMER_SECS*1000) {
dhcp_coarse_tmr();
mscnt = 0;
}
switch (DHCP_state)
{
case DHCP_WAIT_ADDRESS:
{
/* Read the new IP address */
IPaddress = netif->ip_addr.addr;
if (IPaddress!=0)
{
dhcp_renew(netif);
DHCP_state = DHCP_ADDRESS_ASSIGNED;
#if 1
uint8_t iptab[4];
char iptxt[80];
iptab[0] = (uint8_t)(IPaddress >> 24);
iptab[1] = (uint8_t)(IPaddress >> 16);
iptab[2] = (uint8_t)(IPaddress >> 8);
iptab[3] = (uint8_t)(IPaddress);
sprintf(iptxt, "IP address assigned by a DHCP server: %d.%d.%d.%d", iptab[3], iptab[2], iptab[1], iptab[0]);
WriteConsole ("address IP affecté\n");
#endif
return;
}
else
{
/* DHCP timeout */
if (netif->dhcp->tries > MAX_DHCP_TRIES)
{
DHCP_state = DHCP_TIMEOUT;
/* Stop DHCP */
dhcp_stop(netif);
/* Static address used */
IP4_ADDR(&ipaddr, IP_ADDR0 ,IP_ADDR1 , IP_ADDR2 , IP_ADDR3 );
IP4_ADDR(&netmask, NETMASK_ADDR0, NETMASK_ADDR1, NETMASK_ADDR2, NETMASK_ADDR3);
IP4_ADDR(&gw, GW_ADDR0, GW_ADDR1, GW_ADDR2, GW_ADDR3);
netif_set_addr(netif, &ipaddr , &netmask, &gw);
return;
#if CONSOLE
char iptxt[80];
sprintf((char*)iptxt, "%Static IP address : d.%d.%d.%d", IP_ADDR0, IP_ADDR1, IP_ADDR2, IP_ADDR3);
WriteConsole("DHCP timeout !!\n");
WriteConsole(iptxt);
#endif
}
else
netif->dhcp->tries++;
}
}
break;
default: break;
}
}
}
#endif /* USE_DHCP */
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/