diff --git a/.vscode/settings.json b/.vscode/settings.json index 0d302ce..dfff459 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -106,7 +106,8 @@ "optional": "cpp", "span": "cpp", "xs_device_id.h": "c", - "xs_log.h": "c" + "xs_log.h": "c", + "xs_basic.h": "c" }, "files.autoGuessEncoding": false, "files.encoding": "gbk" diff --git a/LWIP/Target/lwipopts.h b/LWIP/Target/lwipopts.h index dcc71c6..aa5ac20 100644 --- a/LWIP/Target/lwipopts.h +++ b/LWIP/Target/lwipopts.h @@ -62,7 +62,7 @@ /*----- Default Value for LWIP_DHCP_AUTOIP_COOP: 0 ---*/ #define LWIP_DHCP_AUTOIP_COOP 1 /*----- Default Value for LWIP_DHCP_AUTOIP_COOP_TRIES: 9 ---*/ -#define LWIP_DHCP_AUTOIP_COOP_TRIES 3 +#define LWIP_DHCP_AUTOIP_COOP_TRIES 1 /*----- Value in opt.h for LWIP_DNS_SECURE: (LWIP_DNS_SECURE_RAND_XID | LWIP_DNS_SECURE_NO_MULTIPLE_OUTSTANDING | LWIP_DNS_SECURE_RAND_SRC_PORT) -*/ #define LWIP_DNS_SECURE 7 /*----- Value in opt.h for TCP_SND_QUEUELEN: (4*TCP_SND_BUF + (TCP_MSS - 1))/TCP_MSS -----*/ @@ -77,6 +77,8 @@ #define LWIP_NETIF_HOSTNAME 1 /*----- Default Value for LWIP_NETIF_STATUS_CALLBACK: 0 ---*/ #define LWIP_NETIF_STATUS_CALLBACK 1 +/*----- Default Value for LWIP_NETIF_EXT_STATUS_CALLBACK: 0 ---*/ +#define LWIP_NETIF_EXT_STATUS_CALLBACK 1 /*----- Value in opt.h for LWIP_NETIF_LINK_CALLBACK: 0 -----*/ #define LWIP_NETIF_LINK_CALLBACK 1 /*----- Value in opt.h for TCPIP_THREAD_STACKSIZE: 0 -----*/ diff --git a/iflytop_xsync b/iflytop_xsync index 5ae77f0..57a8cff 160000 --- a/iflytop_xsync +++ b/iflytop_xsync @@ -1 +1 @@ -Subproject commit 5ae77f0687ccf0fc7710d922deda0e32f4e73678 +Subproject commit 57a8cffbd446853b99604624091ff3204c6278c2 diff --git a/usrc/main.cpp b/usrc/main.cpp index b15f549..e50b34a 100644 --- a/usrc/main.cpp +++ b/usrc/main.cpp @@ -44,9 +44,14 @@ void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin) { ReportGeneratorService_irq_trig void debug_light_ctrl() { static uint32_t lastcall = 0; static bool light_status = false; - if (xs_has_passedms(lastcall) > 100) { + if (xs_has_passedms(lastcall) > 80) { light_status = !light_status; xs_gpio_write(&m_debug_led, light_status); + if (network_service_network_is_ready()) { + xs_gpio_write(&m_power_led, 1); + } else { + xs_gpio_write(&m_power_led, light_status); + } lastcall = xs_get_ticket(); } } @@ -72,9 +77,9 @@ void factory_reset_key_detect() { // m_power_led while (xs_gpio_read(&m_factory_reset_key)) { xs_gpio_write(&m_power_led, false); - osDelay(100); + osDelay(1000); xs_gpio_write(&m_power_led, true); - osDelay(100); + osDelay(1000); } ZLOGI(TAG, "system reset"); NVIC_SystemReset(); @@ -97,7 +102,7 @@ void umain() { */ xs_gpio_init_as_output(&m_debug_led, PC_DEBUG_LIGHT_GPIO, kxs_gpio_nopull, false, false); xs_gpio_init_as_output(&m_power_led, POWER_LED_PIN, kxs_gpio_nopull, false, true); - xs_gpio_init_as_input(&m_factory_reset_key, FACTORY_RESET_KEY, kxs_gpio_nopull, kxs_gpio_no_irq, true); + xs_gpio_init_as_input(&m_factory_reset_key, FACTORY_RESET_KEY, kxs_gpio_pullup, kxs_gpio_no_irq, true); // m_power_led /** * @brief ÅäÖóõʼ»¯ diff --git a/usrc/service/network_service.c b/usrc/service/network_service.c index 5808d7b..761e703 100644 --- a/usrc/service/network_service.c +++ b/usrc/service/network_service.c @@ -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; } diff --git a/usrc/service/network_service.h b/usrc/service/network_service.h index 70ef48e..6c63c9b 100644 --- a/usrc/service/network_service.h +++ b/usrc/service/network_service.h @@ -3,7 +3,7 @@ extern "C" { #endif void network_service_init(); - +bool network_service_network_is_ready(); #ifdef __cplusplus } #endif \ No newline at end of file diff --git a/xsync_stm32.ioc b/xsync_stm32.ioc index e0a0ac3..a23ef87 100644 --- a/xsync_stm32.ioc +++ b/xsync_stm32.ioc @@ -82,13 +82,14 @@ GPIO.groupedBy=Group By Peripherals KeepUserPlacement=false LWIP.BSP.number=1 LWIP.GATEWAY_ADDRESS=192.168.008.001 -LWIP.IPParameters=LWIP_DHCP,IP_ADDRESS,NETMASK_ADDRESS,GATEWAY_ADDRESS,LWIP_DNS,LWIP_NETIF_HOSTNAME,LWIP_SO_RCVTIMEO,LWIP_SO_SNDTIMEO,LWIP_AUTOIP,LWIP_DHCP_AUTOIP_COOP,LWIP_NETIF_STATUS_CALLBACK,LWIP_DHCP_AUTOIP_COOP_TRIES +LWIP.IPParameters=LWIP_DHCP,IP_ADDRESS,NETMASK_ADDRESS,GATEWAY_ADDRESS,LWIP_DNS,LWIP_NETIF_HOSTNAME,LWIP_SO_RCVTIMEO,LWIP_SO_SNDTIMEO,LWIP_AUTOIP,LWIP_DHCP_AUTOIP_COOP,LWIP_NETIF_STATUS_CALLBACK,LWIP_DHCP_AUTOIP_COOP_TRIES,LWIP_NETIF_EXT_STATUS_CALLBACK LWIP.IP_ADDRESS=192.168.008.010 LWIP.LWIP_AUTOIP=1 LWIP.LWIP_DHCP=1 LWIP.LWIP_DHCP_AUTOIP_COOP=1 LWIP.LWIP_DHCP_AUTOIP_COOP_TRIES=0 LWIP.LWIP_DNS=1 +LWIP.LWIP_NETIF_EXT_STATUS_CALLBACK=1 LWIP.LWIP_NETIF_HOSTNAME=1 LWIP.LWIP_NETIF_STATUS_CALLBACK=1 LWIP.LWIP_SO_RCVTIMEO=1