X-Git-Url: http://git.linex4red.de/pub/USBasp.git/blobdiff_plain/16ea5aa7a2e5f326f8ff129e740a19bb3fb7829f..ce8d0424b1a59bb2b0bd3ab8f69f4e4cf8c9930b:/Projects/Webserver/Lib/DHCPClientApp.c diff --git a/Projects/Webserver/Lib/DHCPClientApp.c b/Projects/Webserver/Lib/DHCPClientApp.c index 79170fabe..cf512734e 100644 --- a/Projects/Webserver/Lib/DHCPClientApp.c +++ b/Projects/Webserver/Lib/DHCPClientApp.c @@ -33,32 +33,29 @@ * 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) || defined(__DOXYGEN__) -/** Timer for managing the timeout period for a DHCP server to respond */ -struct timer DHCPTimer; +#if defined(ENABLE_DHCP_CLIENT) || defined(__DOXYGEN__) /** Initialization function for the DHCP client. */ void DHCPClientApp_Init(void) { - uip_udp_appstate_t* const AppState = &uip_udp_conn->appstate; - /* Create a new UDP connection to the DHCP server port for the DHCP solicitation */ - uip_ipaddr_t DHCPServerIPAddress; - uip_ipaddr(&DHCPServerIPAddress, 255, 255, 255, 255); - AppState->DHCPClient.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 sucessfully created, bind it to the local DHCP client port */ - if(AppState->DHCPClient.Connection != NULL) + /* If the connection was successfully created, bind it to the local DHCP client port */ + if (Connection != NULL) { - uip_udp_bind(AppState->DHCPClient.Connection, HTONS(DHCPC_CLIENT_PORT)); + 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 @@ -89,7 +86,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; @@ -97,7 +94,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; @@ -114,7 +111,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; } @@ -135,7 +132,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; @@ -143,7 +140,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; @@ -188,13 +185,13 @@ uint16_t DHCPClientApp_FillDHCPHeader(DHCP_Header_t* DHCPHeader, uint8_t DHCPMes DHCPHeader->TransactionID = DHCP_TRANSACTION_ID; DHCPHeader->ElapsedSeconds = 0; DHCPHeader->Flags = HTONS(BOOTP_BROADCAST); - memcpy(&DHCPHeader->ClientIP, &uip_hostaddr, sizeof(uip_ipaddr_t)); - memcpy(&DHCPHeader->YourIP, &AppState->DHCPClient.DHCPOffer_Data.AllocatedIP, sizeof(uip_ipaddr_t)); - memcpy(&DHCPHeader->NextServerIP, &AppState->DHCPClient.DHCPOffer_Data.ServerIP, sizeof(uip_ipaddr_t)); + memcpy(&DHCPHeader->ClientIP, &uip_hostaddr, sizeof(uip_ipaddr_t)); + memcpy(&DHCPHeader->YourIP, &AppState->DHCPClient.DHCPOffer_Data.AllocatedIP, sizeof(uip_ipaddr_t)); + memcpy(&DHCPHeader->NextServerIP, &AppState->DHCPClient.DHCPOffer_Data.ServerIP, sizeof(uip_ipaddr_t)); memcpy(&DHCPHeader->ClientHardwareAddress, &MACAddress, sizeof(struct uip_eth_addr)); DHCPHeader->Cookie = DHCP_MAGIC_COOKIE; - /* Add a DHCP type and terminator options to the start of the DHCP options field */ + /* Add a DHCP message type and terminator options to the start of the DHCP options field */ DHCPHeader->Options[0] = DHCP_OPTION_MSG_TYPE; DHCPHeader->Options[1] = 1; DHCPHeader->Options[2] = DHCPMessageType; @@ -240,7 +237,7 @@ uint8_t DHCPClientApp_SetOption(uint8_t* DHCPOptionList, uint8_t Option, uint8_t */ bool DHCPClientApp_GetOption(uint8_t* DHCPOptionList, uint8_t Option, void* Destination) { - /* Look through the incomming DHCP packet's options list for the requested option */ + /* Look through the incoming DHCP packet's options list for the requested option */ while (*DHCPOptionList != DHCP_OPTION_END) { /* Check if the current DHCP option in the packet is the one requested */ @@ -249,7 +246,7 @@ bool DHCPClientApp_GetOption(uint8_t* DHCPOptionList, uint8_t Option, void* Dest /* Copy request option's data to the destination buffer */ memcpy(Destination, &DHCPOptionList[2], DHCPOptionList[1]); - /* Indicate that the requested option data was sucessfully retrieved */ + /* Indicate that the requested option data was successfully retrieved */ return true; } @@ -257,7 +254,7 @@ bool DHCPClientApp_GetOption(uint8_t* DHCPOptionList, uint8_t Option, void* Dest DHCPOptionList += (DHCPOptionList[1] + 2); } - /* Requested option not found in the incomming packet's DHCP options list */ + /* Requested option not found in the incoming packet's DHCP options list */ return false; } #endif