Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using with Bluez #1

Closed
ukBaz opened this issue Jul 16, 2019 · 2 comments
Closed

Using with Bluez #1

ukBaz opened this issue Jul 16, 2019 · 2 comments

Comments

@ukBaz
Copy link

ukBaz commented Jul 16, 2019

Thanks for creating this library.

I am trying to apply it to apply it to accessing the BlueZ Advertising API and I am having a few problems.
The API is:
https://git.kernel.org/pub/scm/bluetooth/bluez.git/tree/doc/advertising-api.txt

Below is the code I am trying to run and also the errors I'm getting. I am struggling to understand how to correct the errors. Do you have any insight?

(process:4118): GLib-GIO-CRITICAL **: 07:47:54.587: g_dbus_connection_register_object: assertion 'object_path != NULL && g_variant_is_object_path (object_path)' failed

(process:4118): GLib-GIO-CRITICAL **: 07:47:54.587: g_dbus_connection_register_object: assertion 'object_path != NULL && g_variant_is_object_path (object_path)' failed

(process:4118): GLib-GIO-CRITICAL **: 07:47:54.587: g_dbus_connection_register_object: assertion 'object_path != NULL && g_variant_is_object_path (object_path)' failed
Traceback (most recent call last):
  File "/home/thinkabit1/PycharmProjects/new_bluez/pydbus_seez.py", line 97, in <module>
    None)
  File "/home/thinkabit1/PycharmProjects/new_bluez/pydbus_manager/registration.py", line 59, in register_object
    manager._add_object(path, object_)
  File "/home/thinkabit1/PycharmProjects/new_bluez/pydbus_manager/registration.py", line 18, in _add_object
    interfaces_and_properties = self._get_interfaces_and_properties(path)
  File "/home/thinkabit1/PycharmProjects/new_bluez/pydbus_manager/registration.py", line 30, in _get_interfaces_and_properties
    proxy = self.get(path)
  File "/home/thinkabit1/PycharmProjects/new_bluez/pydbus_manager/proxy.py", line 11, in get
    composite_object = bus.get(bus_name, object_path, **kwargs)
  File "/home/thinkabit1/PycharmProjects/new_bluez/venv/lib/python3.7/site-packages/pydbus/proxy.py", line 59, in get
    return CompositeInterface(introspection)(self, bus_name, object_path)
  File "/home/thinkabit1/PycharmProjects/new_bluez/venv/lib/python3.7/site-packages/pydbus/proxy.py", line 118, in CompositeInterface
    raise KeyError("object does not export any interfaces; you might need to pass object path as the 2nd argument for get()")
KeyError: 'object does not export any interfaces; you might need to pass object path as the 2nd argument for get()'

from pydbus import SystemBus
from pydbus.generic import signal
from pydbus_manager import Manager
from gi.repository import GLib


class Advertisement:
    """
    <!DOCTYPE node PUBLIC '-//freedesktop//DTD D-BUS Object Introspection 1.0//EN'
    'http://www.freedesktop.org/standards/dbus/1.0/introspect.dtd'>
    <node>
        <interface name='org.freedesktop.DBus.Introspectable'>
            <method name='Introspect'>
                <arg name='data' direction='out' type='s'/>
            </method>
        </interface>
        <interface name='org.freedesktop.DBus.Properties'>
            <method name='Get'>
                <arg name='interface' direction='in' type='s'/>
                <arg name='property' direction='in' type='s'/>
                <arg name='value' direction='out' type='v'/>
            </method>
            <method name="Set">
                <arg name="interface_name" direction="in" type="s"/>
                <arg name="property_name" direction="in" type="s"/>
                <arg name="value" direction="in" type="v"/>
            </method>
            <method name='GetAll'>
                <arg name='interface' direction='in' type='s'/>
                <arg name='properties' direction='out' type='a{sv}'/>
            </method>
        </interface>
        <interface name='org.bluez.LEAdvertisement1'>
            <method name='Release'>
                  <annotation name="org.freedesktop.DBus.Method.NoReply" value="true"/>
            </method>
            <annotation name="org.freedesktop.DBus.Properties.PropertiesChanged" value="const"/>
            <property name="Type" type="s" access="read"/>
            <property name="ServiceUUIDs" type="as" access="read"/>
            <property name="ManufacturerData" type="a{sv}" access="read"/>
            <property name="SolicitUUIDs" type="as" access="read"/>
            <property name="ServiceData" type="a{sv}" access="read"/>
            <property name="IncludeTxPower" type="b" access="read"/>
        </interface>
    </node>
    """
    LE_ADVERTISEMENT_IFACE = 'org.bluez.LEAdvertisement1'

    def Introspect(self):
        return self.__doc__

    def Get(self, interface_name, property_name):
        return self.GetAll(interface_name)[property_name]

    def GetAll(self, interface_name):
        if interface_name == Advertisement.LE_ADVERTISEMENT_IFACE:
            return {'Type': pydbus.Variant('s', self.Type),
                    # 'ServiceUUIDs': pydbus.Variant('as', self.ServiceUUIDs),
                    # 'ServiceData': pydbus.Variant('a{sv}', self.ServiceData),
                    # 'IncludeTxPower': pydbus.Variant('b', self.IncludeTxPower),
                    'SolicitUUIDs': pydbus.Variant('as', self.SolicitUUIDs)}

    def Release(self):
        pass

    @property
    def Type(self):
        return 'peripheral'

    @property
    def SolicitUUIDs(self):
        return ['180F']
    @property
    def ServiceUUIDs(self):
        return ['FEAA']

    @property
    def ServiceData(self):
        return {'FEAA', [0x10, 0x08, 0x03, 0x75, 0x6B, 0x42, 0x61,
                         0x7A, 0x2e, 0x67, 0x69, 0x74, 0x68, 0x75,
                         0x62, 0x2E, 0x69, 0x6F]}

    @property
    def IncludeTxPower(self):
        return False

    @property
    def ManufacturerData(self):
        return []


if __name__ == '__main__':
    bus = SystemBus()
    obj_manager = Manager(bus, 'ukBaz.bluezero')
    reg1 = obj_manager.register_object('ukBaz/bluezero/advertisement1',
                                       Advertisement(),
                                       None)

    ad_manager = bus.get('org.bluez', '/org/bluez/hci0')['org.bluez.LEAdvertisingManager1']
    ad_manager.RegisterAdvertisement('/ukBaz/bluezero/advertisement1', {})

    loop = GLib.MainLoop()
    try:
        loop.run()
    except KeyboardInterrupt:
        print("\nStopping ...")
        loop.quit()
@seebz
Copy link
Owner

seebz commented Jul 16, 2019

Hi @ukBaz,
I'll try to help the better I can do.

First, I think its important to warn about the state of this "project".
As I said when presenting it on the pydbus repo, this code was a personnal introduction in python development...
It needs to be improved/reviewed for usage in production.


Now, about your code, I identified 3 "things" :

The errors your provided was caused by a missing "/" on the path of your reg1 object:
'ukBaz/bluezero/advertisement1' must be '/ukBaz/bluezero/advertisement1'.

Interfaces org.freedesktop.DBus.Introspectable and org.freedesktop.DBus.Properties can be handled by pydbus (link). You can remove related stuff from your code.

The return of ServiceData is not valid and causes a GDBus.Error:unknown.TypeError. I cant help about but probably the guys from pydbus.

from pydbus import SystemBus
from pydbus.generic import signal
from pydbus_manager import Manager
from gi.repository import GLib


class Advertisement:
    """
    <node>
        <interface name='org.bluez.LEAdvertisement1'>
            <method name='Release'>
                <annotation name="org.freedesktop.DBus.Method.NoReply" value="true"/>
            </method>
            <annotation name="org.freedesktop.DBus.Properties.PropertiesChanged" value="const"/>
            <property name="Type" type="s" access="read"/>
            <property name="ServiceUUIDs" type="as" access="read"/>
            <property name="ManufacturerData" type="a{sv}" access="read"/>
            <property name="SolicitUUIDs" type="as" access="read"/>
            <property name="ServiceData" type="a{sv}" access="read"/>
            <property name="IncludeTxPower" type="b" access="read"/>
        </interface>
    </node>
    """

    def Release(self):
        pass

    @property
    def Type(self):
        return 'peripheral'

    @property
    def SolicitUUIDs(self):
        return ['180F']

    @property
    def ServiceUUIDs(self):
        return ['FEAA']

    @property
    def ServiceData(self):
        return {}

        # code above produce the following error:
        # "GDBus.Error:unknown.TypeError: unhashable type: 'list'"

        # return {'FEAA', [0x10, 0x08, 0x03, 0x75, 0x6B, 0x42, 0x61,
        #                  0x7A, 0x2e, 0x67, 0x69, 0x74, 0x68, 0x75,
        #                  0x62, 0x2E, 0x69, 0x6F]}

    @property
    def IncludeTxPower(self):
        return False

    @property
    def ManufacturerData(self):
        return []


if __name__ == '__main__':
    bus = SystemBus()
    obj_manager = Manager(bus, 'ukBaz.bluezero')
    reg1 = obj_manager.register_object('/ukBaz/bluezero/advertisement1',
                                       Advertisement(),
                                       None)

    ad_manager = bus.get('org.bluez', '/org/bluez/hci0')['org.bluez.LEAdvertisingManager1']
    ad_manager.RegisterAdvertisement('/ukBaz/bluezero/advertisement1', {})

    loop = GLib.MainLoop()
    try:
        loop.run()
    except KeyboardInterrupt:
        print("\nStopping ...")
        loop.quit()

Note I tested using SessionBus because of the AccessDenied error.


Hope I helped.
Good luck :)

@ukBaz
Copy link
Author

ukBaz commented Jul 16, 2019

Thanks for the quick and helpful reply. Well spotted on me missing the leading slash and ServiceData not being a correct dictionary.
For your reference, I have to put the dbus configuration file into /etc/dbus-1/system.d to change the permissions.
I can now get an org.freedesktop.DBus.ObjectManager interface to appear. Sadly it still doesn't play nicely with BlueZ.
Thanks again for your help.

@ukBaz ukBaz closed this as completed Jul 16, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants