You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

152 lines
5.2 KiB

/* 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];
uint8_t g_mac[6];
static void ethernet_link_status_updated(struct netif *netif) {
if (netif_is_up(netif)) {
ZLOGI(TAG, "ethernet_link_status_updated: netif_is_up");
} else {
ZLOGI(TAG, "ethernet_link_status_updated: netif_is_down");
}
}
static void ethernet_status_updated(struct netif *netif) {
ZLOGI(TAG, "dhcp success----");
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)));
}
// tcpip_init( NULL, NULL );
// /* IP addresses initialization with DHCP (IPv4) */
// ipaddr.addr = 0;
// netmask.addr = 0;
// gw.addr = 0;
// /* add the network interface (IPv4/IPv6) with RTOS */
// netif_add(&gnetif, &ipaddr, &netmask, &gw, NULL, &ethernetif_init, &tcpip_input);
// /* Registers the default network interface */
// netif_set_default(&gnetif);
// if (netif_is_link_up(&gnetif))
// {
// /* When the netif is fully configured this function must be called */
// netif_set_up(&gnetif);
// }
// else
// {
// /* When the netif link is down this function must be called */
// netif_set_down(&gnetif);
// }
// /* Set the link callback function, this function is called on change of link status*/
// netif_set_link_callback(&gnetif, ethernet_link_status_updated);
// /* Create the Ethernet link handler thread */
// /* USER CODE BEGIN H7_OS_THREAD_DEF_CREATE_CMSIS_RTOS_V1 */
// osThreadDef(EthLink, ethernet_link_thread, osPriorityBelowNormal, 0, configMINIMAL_STACK_SIZE *2);
// osThreadCreate (osThread(EthLink), &gnetif);
// /* USER CODE END H7_OS_THREAD_DEF_CREATE_CMSIS_RTOS_V1 */
// /* Start DHCP negotiation for a network interface (IPv4) */
// dhcp_start(&gnetif);
// typedef enum { obtaining_ip_mode_type_static = 0, obtaining_ip_mode_type_dhcp = 1, obtaining_ip_mode_type_lla = 2 } obtaining_ip_mode_t;
void network_service_init() {
ZLOGI(TAG, "network_service_init");
uint32_t mode = config_get()->obtaining_ip_mode;
*(uint32_t *)(IP_ADDRESS) = config_get()->ip;
*(uint32_t *)(NETMASK_ADDRESS) = config_get()->netmask;
*(uint32_t *)(GATEWAY_ADDRESS) = config_get()->gw;
mode = 1;
if (mode == obtaining_ip_mode_type_static) {
memcpy(g_mac, config_get()->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, &ethernetif_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);
osThreadDef(EthLink, ethernet_link_thread, NETWORK_REPORT_TASK_LEVEL, 0, 512);
osThreadCreate(osThread(EthLink), &gnetif);
} else if (mode == obtaining_ip_mode_type_dhcp) {
memcpy(g_mac, config_get()->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, &ethernetif_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);
osThreadDef(EthLink, ethernet_link_thread, NETWORK_REPORT_TASK_LEVEL, 0, 512);
osThreadCreate(osThread(EthLink), &gnetif);
while (!netif_is_up(&gnetif)) {
ZLOGI(TAG, "waiting for dhcp");
osDelay(100);
}
dhcp_start(&gnetif);
} else if (mode == obtaining_ip_mode_type_lla) {
}
}