Skip to content

Commit

Permalink
Add pd daemon service
Browse files Browse the repository at this point in the history
  • Loading branch information
sherysheng committed Jul 4, 2024
1 parent 569bb31 commit ff2f05a
Showing 1 changed file with 56 additions and 0 deletions.
56 changes: 56 additions & 0 deletions script/reference-device/dhcp6_pd_daemon.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import dbus
from dbus.mainloop.glib import DBusGMainLoop
import gi.repository.GLib as GLib
import time
import subprocess

dbus_loop = DBusGMainLoop(set_as_default=True)
bus = dbus.SystemBus()
obj = None
iface = None

def properties_changed_handler(interface_name, changed_properties, invalidated_properties):
if "Dhcp6PdState" in changed_properties:
new_state = changed_properties["Dhcp6PdState"]
print(f"Dhcp6PdState changed to: {new_state}")
if new_state == "running": # Check for string equality
try:
subprocess.run(["sudo", "service", "dhcpcd", "restart"], check=True)
print("Successfully restarted dhcpcd service.")
except subprocess.CalledProcessError as e:
print(f"Error restarting dhcpcd service: {e}")
elif new_state == "stopped":
try:
subprocess.run(["sudo", "cp", "/etc/dhcpcd.conf.orig", "/etc/dhcpcd.conf"], check=True)
print("Successfully restarted dhcpcd service.")
except subprocess.CalledProcessError as e:
print(f"Error restarting dhcpcd service: {e}")

def connect_to_signal():
global obj, iface
try:
obj = bus.get_object('io.openthread.BorderRouter.wpan0', '/io/openthread/BorderRouter/wpan0')
iface = dbus.Interface(obj, 'org.freedesktop.DBus.Properties')
obj.connect_to_signal("PropertiesChanged", properties_changed_handler, dbus_interface=iface.dbus_interface)
print("Connected to D-Bus signal.")
except dbus.DBusException as ex:
print(f"Error connecting to D-Bus: {ex}")
obj = None
iface = None


def check_and_reconnect():
if obj is None:
connect_to_signal()

def main():
connect_to_signal()

loop = GLib.MainLoop()

GLib.timeout_add_seconds(5, check_and_reconnect) # Check every 5 seconds

loop.run()

if __name__ == '__main__':
main()

0 comments on commit ff2f05a

Please sign in to comment.