|
|
@ -50,52 +50,103 @@ static void ethernet_link_status_updated(struct netif *netif) { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
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, ðernetif_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; |
|
|
|
|
|
|
|
// IP_ADDRESS[0] = 192; |
|
|
|
// IP_ADDRESS[1] = 168; |
|
|
|
// IP_ADDRESS[2] = 8; |
|
|
|
// IP_ADDRESS[3] = 10; |
|
|
|
// NETMASK_ADDRESS[0] = 255; |
|
|
|
// NETMASK_ADDRESS[1] = 255; |
|
|
|
// NETMASK_ADDRESS[2] = 255; |
|
|
|
// NETMASK_ADDRESS[3] = 255; |
|
|
|
// GATEWAY_ADDRESS[0] = 192; |
|
|
|
// GATEWAY_ADDRESS[1] = 168; |
|
|
|
// GATEWAY_ADDRESS[2] = 8; |
|
|
|
// GATEWAY_ADDRESS[3] = 1; |
|
|
|
|
|
|
|
memcpy(g_mac, config_get()->mac, 6); |
|
|
|
|
|
|
|
/* Initilialize the LwIP stack with RTOS */ |
|
|
|
tcpip_init(NULL, NULL); |
|
|
|
|
|
|
|
/* IP addresses initialization without DHCP (IPv4) */ |
|
|
|
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]); |
|
|
|
|
|
|
|
/* add the network interface (IPv4/IPv6) with RTOS */ |
|
|
|
netif_add(&gnetif, &ipaddr, &netmask, &gw, NULL, ðernetif_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); |
|
|
|
} |
|
|
|
mode = 1; |
|
|
|
|
|
|
|
/* Set the link callback function, this function is called on change of link status*/ |
|
|
|
netif_set_link_callback(&gnetif, ethernet_link_status_updated); |
|
|
|
osThreadDef(EthLink, ethernet_link_thread, NETWORK_REPORT_TASK_LEVEL, 0, 512); |
|
|
|
osThreadCreate(osThread(EthLink), &gnetif); |
|
|
|
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, ð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); |
|
|
|
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, ð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); |
|
|
|
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) { |
|
|
|
} |
|
|
|
} |