From 6b156d67257ef6a5e095a035ecdd6f32163a4135 Mon Sep 17 00:00:00 2001 From: Yang Song Date: Thu, 14 Nov 2024 04:44:51 +0000 Subject: [PATCH 1/4] pd daemon service: ensure dhcp6_pd_daemon starts after otbr-agent --- script/_dhcpv6_pd_ref | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/script/_dhcpv6_pd_ref b/script/_dhcpv6_pd_ref index 946c22c7400..e0b950617eb 100644 --- a/script/_dhcpv6_pd_ref +++ b/script/_dhcpv6_pd_ref @@ -78,8 +78,9 @@ create_dhcp6_pd_daemon_service() sudo tee ${PD_DAEMON_SERVICE_PATH} < Date: Fri, 15 Nov 2024 11:40:25 +0000 Subject: [PATCH 2/4] pd daemon service: reconnect to dbus after otbr-agent reset it --- script/reference-device/dhcp6_pd_daemon.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/script/reference-device/dhcp6_pd_daemon.py b/script/reference-device/dhcp6_pd_daemon.py index b0f04352a25..c0541a71697 100755 --- a/script/reference-device/dhcp6_pd_daemon.py +++ b/script/reference-device/dhcp6_pd_daemon.py @@ -108,8 +108,17 @@ def connect_to_signal(): def check_and_reconnect(dbus_obj): + global bus if dbus_obj is None: connect_to_signal() + bus.watch_name_owner(bus_name='io.openthread.BorderRouter.wpan0', + callback=handle_name_owner_changed) + + +def handle_name_owner_changed(new_owner): + if not new_owner: + logging.warning("Otbr-agent likely reloaded, reconnecting to D-Bus") + connect_to_signal() def main(): From 88e1433a62318bfb38c4bddc17ac34e7a0b4f0bd Mon Sep 17 00:00:00 2001 From: Yang Song Date: Mon, 18 Nov 2024 07:47:03 +0000 Subject: [PATCH 3/4] pd daemon service: reconnect to dbus after otbr-agent reset it --- script/reference-device/dhcp6_pd_daemon.py | 34 ++++++++++++---------- 1 file changed, 18 insertions(+), 16 deletions(-) diff --git a/script/reference-device/dhcp6_pd_daemon.py b/script/reference-device/dhcp6_pd_daemon.py index 7354f5e9562..e23d73d55d7 100755 --- a/script/reference-device/dhcp6_pd_daemon.py +++ b/script/reference-device/dhcp6_pd_daemon.py @@ -32,6 +32,7 @@ import gi.repository.GLib as GLib import subprocess import threading +import time import os from dbus.mainloop.glib import DBusGMainLoop @@ -41,7 +42,8 @@ logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s') -bus = dbus.SystemBus() +dbus_obj = None +dbus_owner = None intended_dhcp6pd_state = None DHCP_CONFIG_PATH = "/etc/dhcpcd.conf" @@ -89,7 +91,8 @@ def properties_changed_handler(interface_name, changed_properties, restart_dhcpcd_with_no_pd_config() -def connect_to_signal(): +def connect_to_signal(bus): + global dbus_obj try: dbus_obj = bus.get_object('io.openthread.BorderRouter.wpan0', '/io/openthread/BorderRouter/wpan0') @@ -106,18 +109,17 @@ def connect_to_signal(): return None -def check_and_reconnect(dbus_obj): - global bus - if dbus_obj is None: - connect_to_signal() - bus.watch_name_owner(bus_name='io.openthread.BorderRouter.wpan0', - callback=handle_name_owner_changed) +def check_and_reconnect(bus): + global dbus_obj, dbus_owner + current_owner = bus.get_name_owner('io.openthread.BorderRouter.wpan0') + if dbus_obj is None or current_owner != dbus_owner: + if dbus_obj is not None: + logging.warning("D-Bus owner changed, reconnecting...") - -def handle_name_owner_changed(new_owner): - if not new_owner: - logging.warning("Otbr-agent likely reloaded, reconnecting to D-Bus") - connect_to_signal() + time.sleep(2) # Add a small delay to avoid aggressive reconnects + dbus_obj = connect_to_signal(bus) + dbus_owner = current_owner + return True def main(): @@ -137,11 +139,11 @@ def main(): except subprocess.CalledProcessError as e: logging.error(f"Error restarting dhcpcd service: {e}") - loop = GLib.MainLoop() + bus = dbus.SystemBus() - thread_dbus_obj = connect_to_signal() + loop = GLib.MainLoop() - GLib.timeout_add_seconds(5, check_and_reconnect, thread_dbus_obj) + GLib.timeout_add_seconds(5, check_and_reconnect, bus) loop.run() From 4b03dee6477cd96be468064388395a2c5b9d04a3 Mon Sep 17 00:00:00 2001 From: Yang Song Date: Wed, 20 Nov 2024 06:33:02 +0000 Subject: [PATCH 4/4] [PD] pd daemon service: reconnect to dbus on otbr-agent reload --- script/reference-device/dhcp6_pd_daemon.py | 29 ++++++---------------- 1 file changed, 8 insertions(+), 21 deletions(-) diff --git a/script/reference-device/dhcp6_pd_daemon.py b/script/reference-device/dhcp6_pd_daemon.py index e23d73d55d7..fa386ed14a4 100755 --- a/script/reference-device/dhcp6_pd_daemon.py +++ b/script/reference-device/dhcp6_pd_daemon.py @@ -32,7 +32,6 @@ import gi.repository.GLib as GLib import subprocess import threading -import time import os from dbus.mainloop.glib import DBusGMainLoop @@ -42,8 +41,7 @@ logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s') -dbus_obj = None -dbus_owner = None +bus = dbus.SystemBus() intended_dhcp6pd_state = None DHCP_CONFIG_PATH = "/etc/dhcpcd.conf" @@ -91,8 +89,7 @@ def properties_changed_handler(interface_name, changed_properties, restart_dhcpcd_with_no_pd_config() -def connect_to_signal(bus): - global dbus_obj +def connect_to_signal(): try: dbus_obj = bus.get_object('io.openthread.BorderRouter.wpan0', '/io/openthread/BorderRouter/wpan0') @@ -103,23 +100,14 @@ def connect_to_signal(bus): properties_changed_handler, dbus_interface=properties_dbus_iface.dbus_interface) logging.info("Connected to D-Bus signal.") - return dbus_obj except dbus.DBusException as e: logging.error(f"Error connecting to D-Bus: {e}") - return None -def check_and_reconnect(bus): - global dbus_obj, dbus_owner - current_owner = bus.get_name_owner('io.openthread.BorderRouter.wpan0') - if dbus_obj is None or current_owner != dbus_owner: - if dbus_obj is not None: - logging.warning("D-Bus owner changed, reconnecting...") - - time.sleep(2) # Add a small delay to avoid aggressive reconnects - dbus_obj = connect_to_signal(bus) - dbus_owner = current_owner - return True +def handle_name_owner_changed(new_owner): + if new_owner: + logging.info(f"New D-Bus owner({new_owner}) assigned, connecting...") + connect_to_signal() def main(): @@ -139,11 +127,10 @@ def main(): except subprocess.CalledProcessError as e: logging.error(f"Error restarting dhcpcd service: {e}") - bus = dbus.SystemBus() - loop = GLib.MainLoop() - GLib.timeout_add_seconds(5, check_and_reconnect, bus) + bus.watch_name_owner(bus_name='io.openthread.BorderRouter.wpan0', + callback=handle_name_owner_changed) loop.run()