|
|
@ -39,74 +39,97 @@ 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; |
|
|
|
|
|
|
|
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"); |
|
|
|
} |
|
|
|
// 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) { |
|
|
|
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))); |
|
|
|
// 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; |
|
|
|
} |
|
|
|
|
|
|
|
// 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, ðernetif_init, &tcpip_input); |
|
|
|
netif_ext_callback_t extcb; |
|
|
|
|
|
|
|
// /* Registers the default network interface */ |
|
|
|
// netif_set_default(&gnetif); |
|
|
|
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 (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 */ |
|
|
|
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); |
|
|
|
} |
|
|
|
|
|
|
|
// /* Start DHCP negotiation for a network interface (IPv4) */ |
|
|
|
// dhcp_start(&gnetif); |
|
|
|
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; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
// typedef enum { obtaining_ip_mode_type_static = 0, obtaining_ip_mode_type_dhcp = 1, obtaining_ip_mode_type_lla = 2 } obtaining_ip_mode_t; |
|
|
|
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(); |
|
|
|
|
|
|
|
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; |
|
|
|
config_cache.obtaining_ip_mode = 0; |
|
|
|
|
|
|
|
mode = 1; |
|
|
|
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_get()->mac, 6); |
|
|
|
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]); |
|
|
@ -119,10 +142,12 @@ void network_service_init() { |
|
|
|
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 if (mode == obtaining_ip_mode_type_dhcp) { |
|
|
|
memcpy(g_mac, config_get()->mac, 6); |
|
|
|
} 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); |
|
|
@ -138,6 +163,7 @@ void network_service_init() { |
|
|
|
} |
|
|
|
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); |
|
|
|
|
|
|
@ -147,6 +173,7 @@ void network_service_init() { |
|
|
|
} |
|
|
|
|
|
|
|
dhcp_start(&gnetif); |
|
|
|
} else if (mode == obtaining_ip_mode_type_lla) { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
bool network_service_network_is_ready() { return false; } |