2021-07-16 13:20:38 +02:00
|
|
|
#include "app_dhcp.h"
|
2022-05-05 20:55:28 +02:00
|
|
|
|
|
|
|
#include "OBSWConfig.h"
|
2021-07-16 13:20:38 +02:00
|
|
|
#include "app_ethernet.h"
|
|
|
|
#include "ethernetif.h"
|
2021-07-13 09:15:36 +02:00
|
|
|
#include "lwip/dhcp.h"
|
2022-05-05 20:55:28 +02:00
|
|
|
#include "networking.h"
|
2021-07-13 09:15:36 +02:00
|
|
|
#include "stm32h7xx_nucleo.h"
|
2022-05-05 20:55:28 +02:00
|
|
|
#include "udp_config.h"
|
2021-07-12 21:21:03 +02:00
|
|
|
|
|
|
|
#if LWIP_DHCP == 1
|
|
|
|
|
|
|
|
uint8_t DHCP_state = DHCP_OFF;
|
|
|
|
uint32_t DHCPfineTimer = 0;
|
|
|
|
|
|
|
|
void handle_dhcp_timeout(struct netif* netif);
|
|
|
|
void handle_dhcp_start(struct netif* netif);
|
|
|
|
void handle_dhcp_wait(struct netif* netif, struct dhcp** dhcp);
|
|
|
|
void handle_dhcp_down(struct netif* netif);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief DHCP_Process_Handle
|
|
|
|
* @param None
|
|
|
|
* @retval None
|
|
|
|
*/
|
2022-05-05 20:55:28 +02:00
|
|
|
void DHCP_Process(struct netif* netif) {
|
|
|
|
struct dhcp* dhcp = NULL;
|
|
|
|
switch (DHCP_state) {
|
2021-07-12 21:21:03 +02:00
|
|
|
case DHCP_START: {
|
2022-05-05 20:55:28 +02:00
|
|
|
handle_dhcp_start(netif);
|
|
|
|
break;
|
2021-07-12 21:21:03 +02:00
|
|
|
}
|
|
|
|
case DHCP_WAIT_ADDRESS: {
|
2022-05-05 20:55:28 +02:00
|
|
|
handle_dhcp_wait(netif, &dhcp);
|
|
|
|
break;
|
2021-07-12 21:21:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
case DHCP_LINK_DOWN: {
|
2022-05-05 20:55:28 +02:00
|
|
|
handle_dhcp_down(netif);
|
|
|
|
break;
|
2021-07-12 21:21:03 +02:00
|
|
|
}
|
|
|
|
default: {
|
2022-05-05 20:55:28 +02:00
|
|
|
break;
|
2021-07-12 21:21:03 +02:00
|
|
|
}
|
2022-05-05 20:55:28 +02:00
|
|
|
}
|
2021-07-12 21:21:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void handle_dhcp_timeout(struct netif* netif) {
|
2022-05-05 20:55:28 +02:00
|
|
|
ip_addr_t ipaddr;
|
|
|
|
ip_addr_t netmask;
|
|
|
|
ip_addr_t gw;
|
2021-07-12 21:21:03 +02:00
|
|
|
|
2022-05-05 20:55:28 +02:00
|
|
|
DHCP_state = DHCP_TIMEOUT;
|
2021-07-12 21:21:03 +02:00
|
|
|
|
2022-05-05 20:55:28 +02:00
|
|
|
/* Stop DHCP */
|
|
|
|
dhcp_stop(netif);
|
2021-07-12 21:21:03 +02:00
|
|
|
|
2022-05-05 20:55:28 +02:00
|
|
|
/* Static address used */
|
|
|
|
networking::setLwipAddresses(&ipaddr, &netmask, &gw);
|
|
|
|
netif_set_addr(netif, &ipaddr, &netmask, &gw);
|
2021-07-12 21:21:03 +02:00
|
|
|
|
2022-05-05 20:55:28 +02:00
|
|
|
printf("DHCP Timeout\n\r");
|
|
|
|
uint8_t iptxt[20];
|
|
|
|
sprintf((char*)iptxt, "%s", ip4addr_ntoa(netif_ip4_addr(netif)));
|
|
|
|
printf("Assigning static IP address: %s\n", iptxt);
|
2021-07-12 21:21:03 +02:00
|
|
|
|
2021-07-16 13:20:38 +02:00
|
|
|
#if defined FSFW_OSAL_FREERTOS
|
2022-05-05 20:55:28 +02:00
|
|
|
ETH_HandleTypeDef* handle = getEthernetHandle();
|
|
|
|
handle->gState = HAL_ETH_STATE_READY;
|
2021-07-16 13:20:38 +02:00
|
|
|
#endif
|
2021-07-12 21:21:03 +02:00
|
|
|
|
|
|
|
#if OBSW_ETHERNET_TMTC_COMMANDING == 1
|
|
|
|
#if OBSW_ETHERNET_USE_LED1_LED2 == 1
|
2022-05-05 20:55:28 +02:00
|
|
|
BSP_LED_On(LED1);
|
|
|
|
BSP_LED_Off(LED2);
|
2021-07-12 21:21:03 +02:00
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief DHCP periodic check
|
|
|
|
* @param netif
|
|
|
|
* @retval None
|
|
|
|
*/
|
2022-05-05 20:55:28 +02:00
|
|
|
void DHCP_Periodic_Handle(struct netif* netif) {
|
|
|
|
/* Fine DHCP periodic process every 500ms */
|
|
|
|
if (HAL_GetTick() - DHCPfineTimer >= DHCP_FINE_TIMER_MSECS) {
|
|
|
|
DHCPfineTimer = HAL_GetTick();
|
|
|
|
/* process DHCP state machine */
|
|
|
|
DHCP_Process(netif);
|
|
|
|
}
|
2021-07-12 21:21:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void handle_dhcp_start(struct netif* netif) {
|
2022-05-05 20:55:28 +02:00
|
|
|
printf("handle_dhcp_start: Looking for DHCP server ...\n\r");
|
2021-07-12 21:21:03 +02:00
|
|
|
#if OBSW_ETHERNET_TMTC_COMMANDING == 1
|
|
|
|
#if OBSW_ETHERNET_USE_LED1_LED2 == 1
|
2022-05-05 20:55:28 +02:00
|
|
|
BSP_LED_Off(LED1);
|
|
|
|
BSP_LED_Off(LED2);
|
2021-07-12 21:21:03 +02:00
|
|
|
#endif
|
|
|
|
#endif
|
2022-05-05 20:55:28 +02:00
|
|
|
ip_addr_set_zero_ip4(&netif->ip_addr);
|
|
|
|
ip_addr_set_zero_ip4(&netif->netmask);
|
|
|
|
ip_addr_set_zero_ip4(&netif->gw);
|
|
|
|
dhcp_start(netif);
|
|
|
|
DHCP_state = DHCP_WAIT_ADDRESS;
|
2021-07-12 21:21:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void handle_dhcp_wait(struct netif* netif, struct dhcp** dhcp) {
|
2022-05-05 20:55:28 +02:00
|
|
|
if (dhcp_supplied_address(netif)) {
|
|
|
|
DHCP_state = DHCP_ADDRESS_ASSIGNED;
|
|
|
|
printf("IP address assigned by a DHCP server: %s\n\r", ip4addr_ntoa(netif_ip4_addr(netif)));
|
|
|
|
printf("Listener port: %d\n\r", UDP_SERVER_PORT);
|
2021-07-12 21:21:03 +02:00
|
|
|
#if OBSW_ETHERNET_TMTC_COMMANDING == 1
|
|
|
|
#if OBSW_ETHERNET_USE_LED1_LED2 == 1
|
2022-05-05 20:55:28 +02:00
|
|
|
BSP_LED_On(LED1);
|
|
|
|
BSP_LED_Off(LED2);
|
2021-07-12 21:21:03 +02:00
|
|
|
#endif
|
|
|
|
#endif
|
2022-05-05 20:55:28 +02:00
|
|
|
} else {
|
|
|
|
*dhcp = (struct dhcp*)netif_get_client_data(netif, LWIP_NETIF_CLIENT_DATA_INDEX_DHCP);
|
|
|
|
|
|
|
|
/* DHCP timeout */
|
|
|
|
if ((*dhcp)->tries > MAX_DHCP_TRIES) {
|
|
|
|
handle_dhcp_timeout(netif);
|
2021-07-12 21:21:03 +02:00
|
|
|
}
|
2022-05-05 20:55:28 +02:00
|
|
|
}
|
2021-07-12 21:21:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void handle_dhcp_down(struct netif* netif) {
|
2022-05-05 20:55:28 +02:00
|
|
|
DHCP_state = DHCP_OFF;
|
2021-07-12 21:21:03 +02:00
|
|
|
#if OBSW_ETHERNET_TMTC_COMMANDING == 1
|
2022-05-05 20:55:28 +02:00
|
|
|
printf("DHCP_Process: The network cable is not connected.\n\r");
|
2021-07-12 21:21:03 +02:00
|
|
|
#if OBSW_ETHERNET_USE_LED1_LED2 == 1
|
2022-05-05 20:55:28 +02:00
|
|
|
BSP_LED_Off(LED1);
|
|
|
|
BSP_LED_On(LED2);
|
2021-07-12 21:21:03 +02:00
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
2022-05-05 20:55:28 +02:00
|
|
|
/* Global boolean to track ethernet connection */
|
|
|
|
networking::setEthCableConnected(false);
|
2021-07-12 21:21:03 +02:00
|
|
|
}
|
|
|
|
|
2022-05-05 20:55:28 +02:00
|
|
|
uint8_t get_dhcp_state() { return DHCP_state; }
|
2021-07-12 21:21:03 +02:00
|
|
|
|
2022-05-05 20:55:28 +02:00
|
|
|
void set_dhcp_state(uint8_t new_state) { DHCP_state = new_state; }
|
2021-07-12 21:21:03 +02:00
|
|
|
|
|
|
|
#endif /* LWIP_DHCP == 1 */
|