X-Git-Url: http://git.linex4red.de/pub/USBasp.git/blobdiff_plain/41ef05a6e559a10134967e8a899aab78c556b645..aca7863350509a1f390eda93ac0150378d8cd16c:/Projects/Webserver/Lib/DHCPClientApp.c diff --git a/Projects/Webserver/Lib/DHCPClientApp.c b/Projects/Webserver/Lib/DHCPClientApp.c index 0e51d5707..1af90ad19 100644 --- a/Projects/Webserver/Lib/DHCPClientApp.c +++ b/Projects/Webserver/Lib/DHCPClientApp.c @@ -28,39 +28,35 @@ this software. */ +#if defined(ENABLE_DHCP_CLIENT) || defined(__DOXYGEN__) + /** \file * * DHCP Client Application. When connected to the uIP stack, this will retrieve IP configuration settings from the * DHCP server on the network. */ - -#include "DHCPClientApp.h" -#if defined(ENABLE_DHCP_CLIENT) || defined(__DOXYGEN__) -/** Timer for managing the timeout period for a DHCP server to respond */ -struct timer DHCPTimer; +#define INCLUDE_FROM_DHCPCLIENTAPP_C +#include "DHCPClientApp.h" /** Initialization function for the DHCP client. */ void DHCPClientApp_Init(void) { - /* Create an IP address to the broadcast network address */ - uip_ipaddr_t DHCPServerIPAddress; - uip_ipaddr(&DHCPServerIPAddress, 255, 255, 255, 255); - /* Create a new UDP connection to the DHCP server port for the DHCP solicitation */ - struct uip_udp_conn* Connection = uip_udp_new(&DHCPServerIPAddress, HTONS(DHCPC_SERVER_PORT)); + struct uip_udp_conn* Connection = uip_udp_new(&uip_broadcast_addr, HTONS(DHCPC_SERVER_PORT)); /* If the connection was successfully created, bind it to the local DHCP client port */ if (Connection != NULL) { uip_udp_appstate_t* const AppState = &Connection->appstate; - uip_udp_bind(Connection, HTONS(DHCPC_CLIENT_PORT)); + + /* Set the initial client state */ AppState->DHCPClient.CurrentState = DHCP_STATE_SendDiscover; - } - /* Set timeout period to half a second for a DHCP server to respond */ - timer_set(&DHCPTimer, CLOCK_SECOND / 2); + /* Set timeout period to half a second for a DHCP server to respond */ + timer_set(&AppState->DHCPClient.Timeout, CLOCK_SECOND / 2); + } } /** uIP stack application callback for the DHCP client. This function must be called each time the TCP/IP stack @@ -91,7 +87,7 @@ void DHCPClientApp_Callback(void) uip_udp_send(AppDataSize); /* Reset the timeout timer, progress to next state */ - timer_reset(&DHCPTimer); + timer_reset(&AppState->DHCPClient.Timeout); AppState->DHCPClient.CurrentState = DHCP_STATE_WaitForOffer; break; @@ -99,7 +95,7 @@ void DHCPClientApp_Callback(void) if (!(uip_newdata())) { /* Check if the DHCP timeout period has expired while waiting for a response */ - if (timer_expired(&DHCPTimer)) + if (timer_expired(&AppState->DHCPClient.Timeout)) AppState->DHCPClient.CurrentState = DHCP_STATE_SendDiscover; break; @@ -116,7 +112,7 @@ void DHCPClientApp_Callback(void) DHCPClientApp_GetOption(AppData->Options, DHCP_OPTION_ROUTER, &AppState->DHCPClient.DHCPOffer_Data.GatewayIP); DHCPClientApp_GetOption(AppData->Options, DHCP_OPTION_SERVER_ID, &AppState->DHCPClient.DHCPOffer_Data.ServerIP); - timer_reset(&DHCPTimer); + timer_reset(&AppState->DHCPClient.Timeout); AppState->DHCPClient.CurrentState = DHCP_STATE_SendRequest; } @@ -137,7 +133,7 @@ void DHCPClientApp_Callback(void) uip_udp_send(AppDataSize); /* Reset the timeout timer, progress to next state */ - timer_reset(&DHCPTimer); + timer_reset(&AppState->DHCPClient.Timeout); AppState->DHCPClient.CurrentState = DHCP_STATE_WaitForACK; break; @@ -145,7 +141,7 @@ void DHCPClientApp_Callback(void) if (!(uip_newdata())) { /* Check if the DHCP timeout period has expired while waiting for a response */ - if (timer_expired(&DHCPTimer)) + if (timer_expired(&AppState->DHCPClient.Timeout)) AppState->DHCPClient.CurrentState = DHCP_STATE_SendDiscover; break; @@ -161,7 +157,10 @@ void DHCPClientApp_Callback(void) uip_setnetmask((uip_ipaddr_t*)&AppState->DHCPClient.DHCPOffer_Data.Netmask); uip_setdraddr((uip_ipaddr_t*)&AppState->DHCPClient.DHCPOffer_Data.GatewayIP); - AppState->DHCPClient.CurrentState = DHCP_STATE_AddressLeased; + /* Indicate to the user that we now have a valid IP configuration */ + HaveIPConfiguration = true; + + AppState->DHCPClient.CurrentState = DHCP_STATE_AddressLeased; } break; @@ -177,7 +176,7 @@ void DHCPClientApp_Callback(void) * * \return Size in bytes of the created DHCP packet */ -uint16_t DHCPClientApp_FillDHCPHeader(DHCP_Header_t* DHCPHeader, uint8_t DHCPMessageType, uip_udp_appstate_t* AppState) +static uint16_t DHCPClientApp_FillDHCPHeader(DHCP_Header_t* DHCPHeader, uint8_t DHCPMessageType, uip_udp_appstate_t* AppState) { /* Erase existing packet data so that we start will all 0x00 DHCP header data */ memset(DHCPHeader, 0, sizeof(DHCP_Header_t)); @@ -216,7 +215,7 @@ uint16_t DHCPClientApp_FillDHCPHeader(DHCP_Header_t* DHCPHeader, uint8_t DHCPMes * * \return Number of bytes added to the DHCP packet */ -uint8_t DHCPClientApp_SetOption(uint8_t* DHCPOptionList, uint8_t Option, uint8_t DataLen, void* OptionData) +static uint8_t DHCPClientApp_SetOption(uint8_t* DHCPOptionList, uint8_t Option, uint8_t DataLen, void* OptionData) { /* Skip through the DHCP options list until the terminator option is found */ while (*DHCPOptionList != DHCP_OPTION_END) @@ -240,7 +239,7 @@ uint8_t DHCPClientApp_SetOption(uint8_t* DHCPOptionList, uint8_t Option, uint8_t * * \return Boolean true if the option was found in the DHCP packet's options list, false otherwise */ -bool DHCPClientApp_GetOption(uint8_t* DHCPOptionList, uint8_t Option, void* Destination) +static bool DHCPClientApp_GetOption(uint8_t* DHCPOptionList, uint8_t Option, void* Destination) { /* Look through the incoming DHCP packet's options list for the requested option */ while (*DHCPOptionList != DHCP_OPTION_END)