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-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;
|
|
|
|
|
2022-05-22 15:30:08 +02:00
|
|
|
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);
|
2021-07-12 21:21:03 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief DHCP_Process_Handle
|
|
|
|
* @param None
|
|
|
|
* @retval None
|
|
|
|
*/
|
2022-05-22 15:30:08 +02:00
|
|
|
void DHCP_Process(struct netif *netif) {
|
2022-05-29 17:34:43 +02:00
|
|
|
struct dhcp *dhcp = nullptr;
|
2022-05-05 20:55:28 +02:00
|
|
|
switch (DHCP_state) {
|
2022-08-08 12:32:06 +02:00
|
|
|
case DHCP_START: {
|
|
|
|
handle_dhcp_start(netif);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case DHCP_WAIT_ADDRESS: {
|
|
|
|
handle_dhcp_wait(netif, &dhcp);
|
|
|
|
break;
|
|
|
|
}
|
2021-07-12 21:21:03 +02:00
|
|
|
|
2022-08-08 12:32:06 +02:00
|
|
|
case DHCP_LINK_DOWN: {
|
|
|
|
handle_dhcp_down(netif);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default: {
|
|
|
|
break;
|
|
|
|
}
|
2022-05-05 20:55:28 +02:00
|
|
|
}
|
2021-07-12 21:21:03 +02:00
|
|
|
}
|
|
|
|
|
2022-05-22 15:30:08 +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];
|
2022-05-22 15:30:08 +02:00
|
|
|
sprintf((char *)iptxt, "%s", ip4addr_ntoa(netif_ip4_addr(netif)));
|
2022-05-05 20:55:28 +02:00
|
|
|
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-22 15:30:08 +02:00
|
|
|
ETH_HandleTypeDef *handle = getEthernetHandle();
|
2022-05-05 20:55:28 +02:00
|
|
|
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-22 15:30:08 +02:00
|
|
|
void DHCP_Periodic_Handle(struct netif *netif) {
|
2022-05-05 20:55:28 +02:00
|
|
|
/* 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
|
|
|
}
|
|
|
|
|
2022-05-22 15:30:08 +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
|
|
|
}
|
|
|
|
|
2022-05-22 15:30:08 +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;
|
2022-08-08 12:32:06 +02:00
|
|
|
printf("IP address assigned by a DHCP server: %s\n\r", ip4addr_ntoa(netif_ip4_addr(netif)));
|
2022-05-05 20:55:28 +02:00
|
|
|
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 {
|
2022-08-08 12:32:06 +02:00
|
|
|
*dhcp =
|
|
|
|
static_cast<struct dhcp *>(netif_get_client_data(netif, LWIP_NETIF_CLIENT_DATA_INDEX_DHCP));
|
2022-05-05 20:55:28 +02:00
|
|
|
|
|
|
|
/* 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
|
|
|
}
|
|
|
|
|
2022-05-22 15:30:08 +02:00
|
|
|
void handle_dhcp_down(struct netif *netif) {
|
2022-05-29 17:34:43 +02:00
|
|
|
static_cast<void>(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 */
|