fsfw-example-common/stm32h7/networking/app_ethernet.cpp

87 lines
2.4 KiB
C++
Raw Normal View History

2021-07-12 21:21:03 +02:00
/* Includes ------------------------------------------------------------------*/
#include "app_ethernet.h"
2022-05-05 20:55:28 +02:00
2021-07-12 21:21:03 +02:00
#include "ethernetif.h"
2021-07-16 13:39:50 +02:00
#include "networking.h"
2022-05-05 20:55:28 +02:00
#include "udp_config.h"
2021-07-12 21:21:03 +02:00
#if LWIP_DHCP
#include "app_dhcp.h"
#endif
2022-05-05 20:55:28 +02:00
#include <OBSWConfig.h>
2021-07-12 21:21:03 +02:00
#include <lwip/netif.h>
2022-05-05 20:55:28 +02:00
#include <lwipopts.h>
2021-07-12 21:21:03 +02:00
#include <stm32h7xx_nucleo.h>
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
uint32_t ethernetLinkTimer = 0;
/* Private function prototypes -----------------------------------------------*/
2022-05-22 15:30:08 +02:00
void handle_status_change(struct netif *netif, bool link_up);
2021-07-12 21:21:03 +02:00
/* Private functions ---------------------------------------------------------*/
/**
* @brief Notify the User about the network interface config status
* @param netif: the network interface
* @retval None
*/
2022-05-22 15:30:08 +02:00
void networking::ethernetLinkStatusUpdated(struct netif *netif) {
2022-05-05 20:55:28 +02:00
if (netif_is_link_up(netif)) {
networking::setEthCableConnected(true);
handle_status_change(netif, true);
} else {
networking::setEthCableConnected(false);
handle_status_change(netif, false);
}
2021-07-12 21:21:03 +02:00
}
2022-05-22 15:30:08 +02:00
void handle_status_change(struct netif *netif, bool link_up) {
2022-05-05 20:55:28 +02:00
if (link_up) {
2021-07-12 21:21:03 +02:00
#if LWIP_DHCP
2022-05-05 20:55:28 +02:00
/* Update DHCP state machine */
set_dhcp_state(DHCP_START);
2021-07-12 21:21:03 +02:00
#else
2022-05-05 20:55:28 +02:00
uint8_t iptxt[20];
2022-05-22 15:30:08 +02:00
sprintf((char *)iptxt, "%s", ip4addr_ntoa(netif_ip4_addr(netif)));
printf("\rNetwork cable connected. Static IP address: %s | Port: %d\n\r",
iptxt, UDP_SERVER_PORT);
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_On(LED1);
BSP_LED_Off(LED2);
2021-07-12 21:21:03 +02:00
#endif
#endif /* LWIP_DHCP */
2022-05-05 20:55:28 +02:00
} else {
printf("Network cable disconnected\n\r");
2021-07-12 21:21:03 +02:00
#if LWIP_DHCP
2022-05-05 20:55:28 +02:00
/* Update DHCP state machine */
set_dhcp_state(DHCP_LINK_DOWN);
2021-07-12 21:21:03 +02:00
#else
#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 /* LWIP_DHCP */
2022-05-05 20:55:28 +02:00
}
2021-07-12 21:21:03 +02:00
}
#if LWIP_NETIF_LINK_CALLBACK
/**
* @brief Ethernet Link periodic check
* @param netif
* @retval None
*/
2022-05-22 15:30:08 +02:00
void networking::ethernetLinkPeriodicHandle(struct netif *netif) {
2022-05-05 20:55:28 +02:00
/* Ethernet Link every 100ms */
if (HAL_GetTick() - ethernetLinkTimer >= 100) {
ethernetLinkTimer = HAL_GetTick();
ethernet_link_check_state(netif);
}
2021-07-12 21:21:03 +02:00
}
#endif /* LWIP_NETIF_LINK_CALLBACK */