/* USER CODE BEGIN Header */ /** ****************************************************************************** * File Name : LWIP.c * Description : This file provides initialization code for LWIP * middleWare. ****************************************************************************** * @attention * * Copyright (c) 2024 STMicroelectronics. * All rights reserved. * * This software is licensed under terms that can be found in the LICENSE file * in the root directory of this software component. * If no LICENSE file comes with this software, it is provided AS-IS. * ****************************************************************************** */ /* USER CODE END Header */ /* Includes ------------------------------------------------------------------*/ #include "lwip.h" #include "lwip/init.h" #include "lwip/netif.h" #if defined(__CC_ARM) #include "lwip/sio.h" #endif #include "ethernetif.h" // #include "base_service/base_service.h" #include "base_service/task_level_config.h" #define TAG "network" static struct netif gnetif; static ip4_addr_t ipaddr; static ip4_addr_t netmask; static ip4_addr_t gw; static uint8_t IP_ADDRESS[4]; static uint8_t NETMASK_ADDRESS[4]; static uint8_t GATEWAY_ADDRESS[4]; static bool m_networkisready; static config_t config_cache; static bool dhcp_is_start; uint8_t g_mac[6]; static void ethernet_link_status_updated(struct netif *netif) { // uint32_t mode = config_cache.obtaining_ip_mode; // if (netif_is_up(netif)) { // ZLOGI(TAG, "ethernet_link_status_updated: netif_is_up"); // if (mode == obtaining_ip_mode_type_static) { // m_getip = true; // } // } else { // ZLOGI(TAG, "ethernet_link_status_updated: netif_is_down"); // m_getip = false; // } } static void ethernet_status_updated(struct netif *netif) { // if (netif->ip_addr.addr == 0) { // return; // } // ZLOGI(TAG, "---------------------get ip-----------------------"); // ZLOGI(TAG, " IP address: %s", ip4addr_ntoa(netif_ip4_addr(netif))); // ZLOGI(TAG, " netmask : %s", ip4addr_ntoa(netif_ip4_netmask(netif))); // ZLOGI(TAG, " gateway : %s", ip4addr_ntoa(netif_ip4_gw(netif))); // m_getip = true; } netif_ext_callback_t extcb; void netif_ext_callback(struct netif *netif, netif_nsc_reason_t reason, const netif_ext_callback_args_t *args) { uint32_t mode = config_cache.obtaining_ip_mode; if (mode == obtaining_ip_mode_type_static) { if (reason & LWIP_NSC_STATUS_CHANGED) { ZLOGI(TAG, "STATIC_IP: LWIP_NSC_STATUS_CHANGED %d", args->status_changed.state); } if (reason & LWIP_NSC_LINK_CHANGED) { ZLOGI(TAG, "STATIC_IP: LWIP_NSC_LINK_CHANGED %d", args->link_changed.state); if (args->link_changed.state) { m_networkisready = true; } else { m_networkisready = false; } } } else if (mode == obtaining_ip_mode_type_dhcp) { if (reason & LWIP_NSC_STATUS_CHANGED) { ZLOGI(TAG, "DHCP_MODE: LWIP_NSC_STATUS_CHANGED %d", args->status_changed.state); } if (reason & LWIP_NSC_LINK_CHANGED) { ZLOGI(TAG, "DHCP_MODE: LWIP_NSC_LINK_CHANGED %d", args->link_changed.state); // if (!args->link_changed.state) { // IP4_ADDR(&ipaddr, 0, 0, 0, 0); // IP4_ADDR(&netmask, 0, 0, 0, 0); // IP4_ADDR(&gw, 0, 0, 0, 0); // netif_set_addr(netif, &ipaddr, &netmask, &gw); // m_networkisready = false; // } if (!dhcp_is_start) { dhcp_start(&gnetif); dhcp_is_start = true; } if (args->link_changed.state) { if (netif->ip_addr.addr != 0) { m_networkisready = true; } } else { m_networkisready = false; } } if (reason & LWIP_NSC_IPV4_ADDRESS_CHANGED || reason & LWIP_NSC_IPV4_GATEWAY_CHANGED || reason & LWIP_NSC_IPV4_NETMASK_CHANGED || reason & LWIP_NSC_IPV4_SETTINGS_CHANGED) { if (netif->ip_addr.addr == 0) { ZLOGI(TAG, "clear ip"); return; } ZLOGI(TAG, "DHCP_MODE: LWIP_NSC_IPV4_ADDRESS_CHANGED"); ZLOGI(TAG, " IP address: %s", ip4addr_ntoa(netif_ip4_addr(netif))); ZLOGI(TAG, " netmask : %s", ip4addr_ntoa(netif_ip4_netmask(netif))); ZLOGI(TAG, " gateway : %s", ip4addr_ntoa(netif_ip4_gw(netif))); m_networkisready = true; } } } void network_service_init() { ZLOGI(TAG, "network_service_init"); config_cache = *config_get(); // config_cache.obtaining_ip_mode = 0; uint32_t mode = config_cache.obtaining_ip_mode; *(uint32_t *)(IP_ADDRESS) = config_cache.ip; *(uint32_t *)(NETMASK_ADDRESS) = config_cache.netmask; *(uint32_t *)(GATEWAY_ADDRESS) = config_cache.gw; if (mode == obtaining_ip_mode_type_static) { memcpy(g_mac, config_cache.mac, 6); tcpip_init(NULL, NULL); IP4_ADDR(&ipaddr, IP_ADDRESS[0], IP_ADDRESS[1], IP_ADDRESS[2], IP_ADDRESS[3]); IP4_ADDR(&netmask, NETMASK_ADDRESS[0], NETMASK_ADDRESS[1], NETMASK_ADDRESS[2], NETMASK_ADDRESS[3]); IP4_ADDR(&gw, GATEWAY_ADDRESS[0], GATEWAY_ADDRESS[1], GATEWAY_ADDRESS[2], GATEWAY_ADDRESS[3]); netif_add(&gnetif, &ipaddr, &netmask, &gw, NULL, ðernetif_init, &tcpip_input); netif_set_default(&gnetif); if (netif_is_link_up(&gnetif)) { netif_set_up(&gnetif); } else { netif_set_down(&gnetif); } netif_set_link_callback(&gnetif, ethernet_link_status_updated); netif_set_status_callback(&gnetif, ethernet_status_updated); netif_add_ext_callback(&extcb, netif_ext_callback); osThreadDef(EthLink, ethernet_link_thread, NETWORK_REPORT_TASK_LEVEL, 0, 512); osThreadCreate(osThread(EthLink), &gnetif); } else { memcpy(g_mac, config_cache.mac, 6); tcpip_init(NULL, NULL); IP4_ADDR(&ipaddr, 0, 0, 0, 0); IP4_ADDR(&netmask, 0, 0, 0, 0); IP4_ADDR(&gw, 0, 0, 0, 0); netif_set_hostname(&gnetif, PC_DEVICE_NAME); netif_add(&gnetif, &ipaddr, &netmask, &gw, NULL, ðernetif_init, &tcpip_input); netif_set_default(&gnetif); if (netif_is_link_up(&gnetif)) { netif_set_up(&gnetif); } else { netif_set_down(&gnetif); } netif_set_link_callback(&gnetif, ethernet_link_status_updated); netif_set_status_callback(&gnetif, ethernet_status_updated); netif_add_ext_callback(&extcb, netif_ext_callback); osThreadDef(EthLink, ethernet_link_thread, NETWORK_REPORT_TASK_LEVEL, 0, 1024); osThreadCreate(osThread(EthLink), &gnetif); // while (!netif_is_up(&gnetif)) { // ZLOGI(TAG, "waiting for dhcp"); // osDelay(100); // } } } bool network_service_network_is_ready() { return m_networkisready; }