From 148f5f9441311123b2f8fae84faf7bb42e45b840 Mon Sep 17 00:00:00 2001 From: zhaohe Date: Mon, 25 Mar 2024 21:52:39 +0800 Subject: [PATCH] V3 --- LWIP/App/lwip.c | 31 ++------ LWIP/Target/lwipopts.h | 10 +++ README.md | 2 + iflytop_xsync_protocol | 2 +- usrc/base_service/config_service.c | 2 +- usrc/project_configs.h | 1 + usrc/service/network_service.c | 131 ++++++++++++++++++++++---------- usrc/service/report_generator_service.c | 32 +++++++- xsync_stm32.ioc | 8 +- 9 files changed, 147 insertions(+), 72 deletions(-) diff --git a/LWIP/App/lwip.c b/LWIP/App/lwip.c index f5aa6e5..01b431e 100644 --- a/LWIP/App/lwip.c +++ b/LWIP/App/lwip.c @@ -44,9 +44,6 @@ struct netif gnetif; ip4_addr_t ipaddr; ip4_addr_t netmask; ip4_addr_t gw; -uint8_t IP_ADDRESS[4]; -uint8_t NETMASK_ADDRESS[4]; -uint8_t GATEWAY_ADDRESS[4]; /* USER CODE BEGIN 2 */ @@ -57,30 +54,13 @@ uint8_t GATEWAY_ADDRESS[4]; */ void MX_LWIP_Init(void) { - /* IP addresses initialization */ - 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; - -/* USER CODE BEGIN IP_ADDRESSES */ -/* USER CODE END IP_ADDRESSES */ - /* 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]); + /* 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); @@ -108,6 +88,9 @@ void MX_LWIP_Init(void) 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); + /* USER CODE BEGIN 3 */ /* USER CODE END 3 */ diff --git a/LWIP/Target/lwipopts.h b/LWIP/Target/lwipopts.h index 6f49472..dcc71c6 100644 --- a/LWIP/Target/lwipopts.h +++ b/LWIP/Target/lwipopts.h @@ -49,12 +49,20 @@ /* LwIP Stack Parameters (modified compared to initialization value in opt.h) -*/ /* Parameters set in STM32CubeMX LwIP Configuration GUI -*/ +/*----- Value in opt.h for LWIP_DHCP: 0 -----*/ +#define LWIP_DHCP 1 /*----- Default Value for LWIP_DNS: 0 ---*/ #define LWIP_DNS 1 /*----- Value in opt.h for MEM_ALIGNMENT: 1 -----*/ #define MEM_ALIGNMENT 4 /*----- Value in opt.h for LWIP_ETHERNET: LWIP_ARP || PPPOE_SUPPORT -*/ #define LWIP_ETHERNET 1 +/*----- Default Value for LWIP_AUTOIP: 0 ---*/ +#define LWIP_AUTOIP 1 +/*----- 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 /*----- 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 -----*/ @@ -67,6 +75,8 @@ #define TCP_WND_UPDATE_THRESHOLD 536 /*----- Default Value for LWIP_NETIF_HOSTNAME: 0 ---*/ #define LWIP_NETIF_HOSTNAME 1 +/*----- Default Value for LWIP_NETIF_STATUS_CALLBACK: 0 ---*/ +#define LWIP_NETIF_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/README.md b/README.md index 1cada81..7504b65 100644 --- a/README.md +++ b/README.md @@ -10,6 +10,8 @@ V3 1.添加SN码,并规范化 2.修改xsync相机同步消息上报格式 3.修改xsynctimecode消息上报格式 + 4.添加dhcp支持 + 5.dhcp失败自动触发LLA,生成随机本地IP ``` diff --git a/iflytop_xsync_protocol b/iflytop_xsync_protocol index 6f8c0e9..e6aef8e 160000 --- a/iflytop_xsync_protocol +++ b/iflytop_xsync_protocol @@ -1 +1 @@ -Subproject commit 6f8c0e94e9d830196ae42bfa6ea7dc447fe230ad +Subproject commit e6aef8efe9806029eb776f851af4c4d043f22a48 diff --git a/usrc/base_service/config_service.c b/usrc/base_service/config_service.c index be57c24..a5aab09 100644 --- a/usrc/base_service/config_service.c +++ b/usrc/base_service/config_service.c @@ -20,7 +20,7 @@ static void create_default_config(config_t *now_cfg, bool cfg_is_error, config_t IP4_ADDR((ip4_addr_t *)&default_cfg->ip, 192, 168, 8, 10); IP4_ADDR((ip4_addr_t *)&default_cfg->gw, 192, 168, 8, 1); IP4_ADDR((ip4_addr_t *)&default_cfg->netmask, 255, 255, 255, 0); - default_cfg->obtaining_ip_mode = obtaining_ip_mode_type_static; // dhcp + default_cfg->obtaining_ip_mode = obtaining_ip_mode_type_dhcp; // dhcp default_cfg->config0 = KXSYNC_REG_STM32_CONFIG0_MASK_TIMECODE_REPORT_ENABLE | // KXSYNC_REG_STM32_CONFIG0_MASK_CAMERA_SYNC_REPORT_ENABLE; diff --git a/usrc/project_configs.h b/usrc/project_configs.h index 1761df2..2088da5 100644 --- a/usrc/project_configs.h +++ b/usrc/project_configs.h @@ -4,6 +4,7 @@ #define PC_MANUFACTURER0 ('i' | 'f' << 8 | 'l' << 16 | 'y' << 24) #define PC_MANUFACTURER1 ('t' | 'o' << 8 | 'p' << 16 | '\0' << 24) #define PC_PROJECT_NAME "xsync" +#define PC_DEVICE_NAME "xsync" #define PC_IFLYTOP_ENABLE_OS 1 #define PC_DEBUG_UART huart1 diff --git a/usrc/service/network_service.c b/usrc/service/network_service.c index 395d5cb..5808d7b 100644 --- a/usrc/service/network_service.c +++ b/usrc/service/network_service.c @@ -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) { + } } diff --git a/usrc/service/report_generator_service.c b/usrc/service/report_generator_service.c index f294064..8c1a0a4 100644 --- a/usrc/service/report_generator_service.c +++ b/usrc/service/report_generator_service.c @@ -37,6 +37,15 @@ static void create_and_send_timecode(uint32_t timecode0, uint32_t timecode1, uin txpacket->data[2] = frameNum; xs_udp_broadcast(&m_udp_camera_timecode_sender, IFLYTOP_XSYNC_EVENT_REPORT_PC_PORT, txbuf, sizeof(iflytop_xsync_event_report_packet_t) + 12); } + +static void create_and_send_device_online_packet() { + static uint8_t txbuf[256]; + iflytop_xsync_event_report_packet_t *txpacket = (iflytop_xsync_event_report_packet_t *)txbuf; + txpacket->eventid = kdevice_online_report_event; + txpacket->data[0] = kxsync_device_type_xsync; + xs_udp_broadcast(&m_udp_camera_timecode_sender, IFLYTOP_XSYNC_EVENT_REPORT_PC_PORT, txbuf, sizeof(iflytop_xsync_event_report_packet_t) + 4); +} + static void create_and_send_sync_record_state_packet(uint32_t workstate, uint32_t timecode0, uint32_t timecode1) { static uint8_t txbuf[256]; uint32_t packetdatalen = 3; @@ -111,9 +120,18 @@ static uint32_t dtimes(timecode_parse_result_t *a, timecode_parse_result_t *b, b } } +static uint32_t last_report_packet_time; + +static void try_report_device_info_packet() { + if (xs_has_passedms(last_report_packet_time) >= 3000) { + last_report_packet_time = xs_get_ticket(); + create_and_send_device_online_packet(); + } +} + static void xync_signal_report_thread(void const *argument) { while (true) { - osEvent signal = osSignalWait(0x07, osWaitForever); + osEvent signal = osSignalWait(0x07, 100); if (signal.value.signals == 0x01) { uint32_t tc0; uint32_t tc1; @@ -133,7 +151,9 @@ static void xync_signal_report_thread(void const *argument) { daycnt = 0; } - create_and_send_timecode(tc0, tc1, cnt); + create_and_send_timecode(tc0, tc1, cnt); // timecode广播包 + last_report_packet_time = xs_get_ticket(); + if (dtime >= 1) { second = daycnt * 24 * 3600 + result.timeinall; create_and_send_camera_sync_msg(second); @@ -145,7 +165,8 @@ static void xync_signal_report_thread(void const *argument) { uint32_t timecode1 = 0; fpga_if_spi_read_data_02(record_sig_gen_timecode_snapshot0, &timecode0); fpga_if_spi_read_data_02(record_sig_gen_timecode_snapshot1, &timecode1); - create_and_send_sync_record_state_packet(1, timecode0, timecode1); + create_and_send_sync_record_state_packet(1, timecode0, timecode1); // 相机同步包 + last_report_packet_time = xs_get_ticket(); } if (signal.value.signals & 0x04) { // 结束录制 @@ -153,8 +174,11 @@ static void xync_signal_report_thread(void const *argument) { uint32_t timecode1 = 0; fpga_if_spi_read_data_02(record_sig_gen_timecode_snapshot0, &timecode0); fpga_if_spi_read_data_02(record_sig_gen_timecode_snapshot1, &timecode1); - create_and_send_sync_record_state_packet(0, timecode0, timecode1); + create_and_send_sync_record_state_packet(0, timecode0, timecode1); // 相机同步包 + last_report_packet_time = xs_get_ticket(); } + + try_report_device_info_packet(); } } diff --git a/xsync_stm32.ioc b/xsync_stm32.ioc index 50b02bc..e0a0ac3 100644 --- a/xsync_stm32.ioc +++ b/xsync_stm32.ioc @@ -82,11 +82,15 @@ 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.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.IP_ADDRESS=192.168.008.010 -LWIP.LWIP_DHCP=0 +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_HOSTNAME=1 +LWIP.LWIP_NETIF_STATUS_CALLBACK=1 LWIP.LWIP_SO_RCVTIMEO=1 LWIP.LWIP_SO_SNDTIMEO=1 LWIP.NETMASK_ADDRESS=255.255.255.255