Пример #1
0
def register_agent(capability):
    bus = SystemBus()
    manager = bus.get('org.bluez')['.AgentManager1']
    manager.RegisterAgent(AGENT_PATH, capability)
    logging.info(
        'register_agent:manager.RegisterAgent(path=%s, capability=%s)' %
        (AGENT_PATH, capability))
    manager.RequestDefaultAgent(AGENT_PATH)
    logging.info('register_agent:manager.RequestDefaultAgent(path=%s)' %
                 (AGENT_PATH))
    bus.register_object(AGENT_PATH, Agent(), node_info=None)
    logging.info('register_agent:bus.register_object(path=%s)' % (AGENT_PATH))
Пример #2
0
class Service:
    """
      <node>
        <interface name='soundcraft.utils.notepad'>
          <property name='version' type='s' access='read' />
          <property name='devices' type='ao' access='read'>
            <annotation name="org.freedesktop.DBus.Property.EmitsChangedSignal" value="true"/>
          </property>
          <signal name='Added'>
            <arg name='path' type='o'/>
          </signal>
          <signal name='Removed'>
            <arg name='path' type='o'/>
          </signal>
          <method name='Shutdown'/>
        </interface>
      </node>
    """

    InterfaceName = "soundcraft.utils.notepad"
    PropertiesChanged = signal()
    Added = signal()
    Removed = signal()

    def __init__(self):
        self.object = None
        self.bus = SystemBus()
        self.udev = GUdev.Client(subsystems=["usb/usb_device"])
        self.udev.connect("uevent", self.uevent)
        self.loop = GLib.MainLoop()
        self.busname = self.bus.publish(BUSNAME, self)

    def run(self):
        self.tryRegister()
        if not self.hasDevice():
            print(f"Waiting for one to arrive...")
        self.loop.run()

    @property
    def version(self):
        return soundcraft.__version__

    @property
    def devices(self):
        if self.hasDevice():
            return [self.object._path]
        return []

    def Shutdown(self):
        print("Shutting down")
        self.unregister()
        self.loop.quit()

    def objPath(self, idx):
        return f"/soundcraft/utils/notepad/{idx}"

    def tryRegister(self):
        if self.hasDevice():
            print(
                f"There is already a {self.object._wrapped._dev.name} on the bus at {self.object._path}"
            )
            return
        dev = soundcraft.notepad.autodetect()
        if dev is None:
            print(f"No recognised device was found")
            return
        # Reset any stored state
        dev.resetState()
        path = self.objPath(0)
        wrapped = NotepadDbus(dev)
        self.object = self.bus.register_object(path, wrapped, None)
        self.object._wrapped = wrapped
        self.object._path = path
        print(
            f"Presenting {self.object._wrapped._dev.name} on the system bus as {path}"
        )
        self.Added(path)
        self.PropertiesChanged(self.InterfaceName, {"devices": self.devices},
                               [])

    def hasDevice(self):
        return self.object is not None

    def unregister(self):
        if not self.hasDevice():
            return
        path = self.object._path
        print(
            f"Removed {self.object._wrapped._dev.name} AKA {path} from the system bus"
        )
        self.object.unregister()
        self.object = None
        self.PropertiesChanged(self.InterfaceName, {"devices": self.devices},
                               [])
        self.Removed(path)

    def uevent(self, observer, action, device):
        if action == "add":
            idVendor = device.get_property("ID_VENDOR_ID")
            idProduct = device.get_property("ID_PRODUCT_ID")
            if idVendor == "05fc":
                print(
                    f"Checking new Soundcraft device ({idVendor}:{idProduct})..."
                )
                self.tryRegister()
                if not self.hasDevice():
                    print(
                        f"Contact the developer for help adding support for your advice"
                    )
        elif action == "remove" and self.hasDevice():
            # UDEV adds leading 0s to decimal numbers.  They're not octal.  Why??
            busnum = int(device.get_property("BUSNUM").lstrip("0"))
            devnum = int(device.get_property("DEVNUM").lstrip("0"))
            objectdev = self.object._wrapped._dev.dev
            if busnum == objectdev.bus and devnum == objectdev.address:
                self.unregister()
    def Appearance(self):
        return 0x00C1

    @property
    @log
    def Duration(self):
        return 2

    @property
    @log
    def Timeout(self):
        return 120

 
if __name__ == '__main__':
    bus = SystemBus()
    loop = GLib.MainLoop()

    obj_path = '/org/bluez/example/advertisement0'
    bus.register_object(obj_path, LEAdvertisement1(), None)
    mgr = bus.get('org.bluez','/org/bluez/hci0')

    try:
        print('Registering advertisement')
        mgr.RegisterAdvertisement(obj_path, {})
        loop.run()
    except KeyboardInterrupt:
        print('Unregistering advertisement')
        mgr.UnregisterAdvertisement(obj_path)