this software.\r
*/\r
\r
+#if defined(ENABLE_DHCP_CLIENT) || defined(__DOXYGEN__)\r
+\r
/** \file\r
*\r
* DHCP Client Application. When connected to the uIP stack, this will retrieve IP configuration settings from the\r
* DHCP server on the network.\r
*/\r
- \r
-#include "DHCPClientApp.h"\r
\r
-#if defined(ENABLE_DHCP) || defined(__DOXYGEN__)\r
-/** Timer for managing the timeout period for a DHCP server to respond */\r
-struct timer DHCPTimer;\r
+#define INCLUDE_FROM_DHCPCLIENTAPP_C\r
+#include "DHCPClientApp.h"\r
\r
/** Initialization function for the DHCP client. */\r
void DHCPClientApp_Init(void)\r
{\r
- uip_udp_appstate_t* const AppState = &uip_udp_conn->appstate;\r
- \r
/* Create a new UDP connection to the DHCP server port for the DHCP solicitation */\r
- uip_ipaddr_t DHCPServerIPAddress;\r
- uip_ipaddr(&DHCPServerIPAddress, 255, 255, 255, 255);\r
- AppState->DHCPClient.Connection = uip_udp_new(&DHCPServerIPAddress, HTONS(DHCPC_SERVER_PORT));\r
+ struct uip_udp_conn* Connection = uip_udp_new(&uip_broadcast_addr, HTONS(DHCPC_SERVER_PORT));\r
\r
- /* If the connection was sucessfully created, bind it to the local DHCP client port */\r
- if(AppState->DHCPClient.Connection != NULL)\r
+ /* If the connection was successfully created, bind it to the local DHCP client port */\r
+ if (Connection != NULL)\r
{\r
- uip_udp_bind(AppState->DHCPClient.Connection, HTONS(DHCPC_CLIENT_PORT));\r
+ uip_udp_appstate_t* const AppState = &Connection->appstate;\r
+ uip_udp_bind(Connection, HTONS(DHCPC_CLIENT_PORT));\r
+ \r
+ /* Set the initial client state */\r
AppState->DHCPClient.CurrentState = DHCP_STATE_SendDiscover;\r
- }\r
\r
- /* Set timeout period to half a second for a DHCP server to respond */\r
- timer_set(&DHCPTimer, CLOCK_SECOND / 2);\r
+ /* Set timeout period to half a second for a DHCP server to respond */\r
+ timer_set(&AppState->DHCPClient.Timeout, CLOCK_SECOND / 2);\r
+ }\r
}\r
\r
/** uIP stack application callback for the DHCP client. This function must be called each time the TCP/IP stack \r
uip_udp_send(AppDataSize);\r
\r
/* Reset the timeout timer, progress to next state */\r
- timer_reset(&DHCPTimer);\r
+ timer_reset(&AppState->DHCPClient.Timeout);\r
AppState->DHCPClient.CurrentState = DHCP_STATE_WaitForOffer; \r
\r
break;\r
if (!(uip_newdata()))\r
{\r
/* Check if the DHCP timeout period has expired while waiting for a response */\r
- if (timer_expired(&DHCPTimer))\r
+ if (timer_expired(&AppState->DHCPClient.Timeout))\r
AppState->DHCPClient.CurrentState = DHCP_STATE_SendDiscover;\r
\r
break;\r
DHCPClientApp_GetOption(AppData->Options, DHCP_OPTION_ROUTER, &AppState->DHCPClient.DHCPOffer_Data.GatewayIP);\r
DHCPClientApp_GetOption(AppData->Options, DHCP_OPTION_SERVER_ID, &AppState->DHCPClient.DHCPOffer_Data.ServerIP);\r
\r
- timer_reset(&DHCPTimer);\r
+ timer_reset(&AppState->DHCPClient.Timeout);\r
AppState->DHCPClient.CurrentState = DHCP_STATE_SendRequest;\r
}\r
\r
uip_udp_send(AppDataSize);\r
\r
/* Reset the timeout timer, progress to next state */\r
- timer_reset(&DHCPTimer);\r
+ timer_reset(&AppState->DHCPClient.Timeout);\r
AppState->DHCPClient.CurrentState = DHCP_STATE_WaitForACK;\r
\r
break;\r
if (!(uip_newdata()))\r
{\r
/* Check if the DHCP timeout period has expired while waiting for a response */\r
- if (timer_expired(&DHCPTimer))\r
+ if (timer_expired(&AppState->DHCPClient.Timeout))\r
AppState->DHCPClient.CurrentState = DHCP_STATE_SendDiscover;\r
\r
break;\r
uip_setnetmask((uip_ipaddr_t*)&AppState->DHCPClient.DHCPOffer_Data.Netmask);\r
uip_setdraddr((uip_ipaddr_t*)&AppState->DHCPClient.DHCPOffer_Data.GatewayIP);\r
\r
- AppState->DHCPClient.CurrentState = DHCP_STATE_AddressLeased;\r
+ /* Indicate to the user that we now have a valid IP configuration */\r
+ HaveIPConfiguration = true;\r
+\r
+ AppState->DHCPClient.CurrentState = DHCP_STATE_AddressLeased; \r
}\r
\r
break;\r
*\r
* \return Size in bytes of the created DHCP packet\r
*/\r
-uint16_t DHCPClientApp_FillDHCPHeader(DHCP_Header_t* DHCPHeader, uint8_t DHCPMessageType, uip_udp_appstate_t* AppState)\r
+static uint16_t DHCPClientApp_FillDHCPHeader(DHCP_Header_t* DHCPHeader, uint8_t DHCPMessageType, uip_udp_appstate_t* AppState)\r
{\r
/* Erase existing packet data so that we start will all 0x00 DHCP header data */\r
memset(DHCPHeader, 0, sizeof(DHCP_Header_t));\r
DHCPHeader->TransactionID = DHCP_TRANSACTION_ID;\r
DHCPHeader->ElapsedSeconds = 0;\r
DHCPHeader->Flags = HTONS(BOOTP_BROADCAST);\r
- memcpy(&DHCPHeader->ClientIP, &uip_hostaddr, sizeof(uip_ipaddr_t));\r
- memcpy(&DHCPHeader->YourIP, &AppState->DHCPClient.DHCPOffer_Data.AllocatedIP, sizeof(uip_ipaddr_t));\r
- memcpy(&DHCPHeader->NextServerIP, &AppState->DHCPClient.DHCPOffer_Data.ServerIP, sizeof(uip_ipaddr_t));\r
+ memcpy(&DHCPHeader->ClientIP, &uip_hostaddr, sizeof(uip_ipaddr_t));\r
+ memcpy(&DHCPHeader->YourIP, &AppState->DHCPClient.DHCPOffer_Data.AllocatedIP, sizeof(uip_ipaddr_t));\r
+ memcpy(&DHCPHeader->NextServerIP, &AppState->DHCPClient.DHCPOffer_Data.ServerIP, sizeof(uip_ipaddr_t));\r
memcpy(&DHCPHeader->ClientHardwareAddress, &MACAddress, sizeof(struct uip_eth_addr));\r
DHCPHeader->Cookie = DHCP_MAGIC_COOKIE;\r
\r
- /* Add a DHCP type and terminator options to the start of the DHCP options field */\r
+ /* Add a DHCP message type and terminator options to the start of the DHCP options field */\r
DHCPHeader->Options[0] = DHCP_OPTION_MSG_TYPE;\r
DHCPHeader->Options[1] = 1;\r
DHCPHeader->Options[2] = DHCPMessageType;\r
*\r
* \return Number of bytes added to the DHCP packet\r
*/\r
-uint8_t DHCPClientApp_SetOption(uint8_t* DHCPOptionList, uint8_t Option, uint8_t DataLen, void* OptionData)\r
+static uint8_t DHCPClientApp_SetOption(uint8_t* DHCPOptionList, uint8_t Option, uint8_t DataLen, void* OptionData)\r
{\r
/* Skip through the DHCP options list until the terminator option is found */\r
while (*DHCPOptionList != DHCP_OPTION_END)\r
*\r
* \return Boolean true if the option was found in the DHCP packet's options list, false otherwise\r
*/\r
-bool DHCPClientApp_GetOption(uint8_t* DHCPOptionList, uint8_t Option, void* Destination)\r
+static bool DHCPClientApp_GetOption(uint8_t* DHCPOptionList, uint8_t Option, void* Destination)\r
{\r
- /* Look through the incomming DHCP packet's options list for the requested option */\r
+ /* Look through the incoming DHCP packet's options list for the requested option */\r
while (*DHCPOptionList != DHCP_OPTION_END)\r
{\r
/* Check if the current DHCP option in the packet is the one requested */\r
/* Copy request option's data to the destination buffer */\r
memcpy(Destination, &DHCPOptionList[2], DHCPOptionList[1]);\r
\r
- /* Indicate that the requested option data was sucessfully retrieved */\r
+ /* Indicate that the requested option data was successfully retrieved */\r
return true;\r
}\r
\r
DHCPOptionList += (DHCPOptionList[1] + 2);\r
}\r
\r
- /* Requested option not found in the incomming packet's DHCP options list */\r
+ /* Requested option not found in the incoming packet's DHCP options list */\r
return false;\r
}\r
#endif\r