xref: /aosp_15_r20/external/autotest/client/cros/bluetooth/advertisement.py (revision 9c5db1993ded3edbeafc8092d69fe5de2ee02df7)
1# Lint as: python2, python3
2# Copyright 2016 The Chromium OS Authors. All rights reserved.
3# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
5"""Construction of an Advertisement object from an advertisement data
6dictionary.
7
8Much of this module refers to the code of test/example-advertisement in
9bluez project.
10"""
11
12from __future__ import absolute_import
13from __future__ import division
14from __future__ import print_function
15from gi.repository import GLib
16
17# TODO(b/215715213) - Wait until ebuild runs as python3 to remove this try
18try:
19    import pydbus
20except:
21    pydbus = {}
22
23import logging
24
25LE_ADVERTISEMENT_IFACE = 'org.bluez.LEAdvertisement1'
26
27
28def InvalidArgsException():
29    return GLib.gerror_new_literal(0, 'org.freedesktop.DBus.Error.InvalidArgs',
30                                   0)
31
32
33class Advertisement:
34    """An advertisement object."""
35    def __init__(self, bus, advertisement_data):
36        """Construction of an Advertisement object.
37
38        @param bus: a dbus system bus.
39        @param advertisement_data: advertisement data dictionary.
40        """
41        self._get_advertising_data(advertisement_data)
42
43        # Register self on bus and hold object for unregister
44        self.obj = bus.register_object(self.path, self, None)
45
46    # D-Bus service definition (required by pydbus).
47    dbus = """
48    <node>
49        <interface name="org.bluez.LEAdvertisement1">
50            <method name="Release" />
51        </interface>
52        <interface name="org.freedesktop.DBus.Properties">
53            <method name="Set">
54                <arg type="s" name="interface" direction="in" />
55                <arg type="s" name="prop" direction="in" />
56                <arg type="v" name="value" direction="in" />
57            </method>
58            <method name="GetAll">
59                <arg type="s" name="interface" direction="in" />
60                <arg type="a{sv}" name="properties" direction="out" />
61            </method>
62        </interface>
63    </node>
64    """
65
66    def unregister(self):
67        """Unregister self from bus."""
68        self.obj.unregister()
69
70    def _get_advertising_data(self, advertisement_data):
71        """Get advertising data from the advertisement_data dictionary.
72
73        @param bus: a dbus system bus.
74
75        """
76        self.path = advertisement_data.get('Path')
77        self.type = advertisement_data.get('Type')
78        self.service_uuids = advertisement_data.get('ServiceUUIDs', [])
79        self.solicit_uuids = advertisement_data.get('SolicitUUIDs', [])
80
81        # The xmlrpclib library requires that only string keys are allowed in
82        # python dictionary. Hence, we need to define the manufacturer data
83        # in an advertisement dictionary like
84        #    'ManufacturerData': {'0xff00': [0xa1, 0xa2, 0xa3, 0xa4, 0xa5]},
85        # in order to let autotest server transmit the advertisement to
86        # a client DUT for testing.
87        # On the other hand, the dbus method of advertising requires that
88        # the signature of the manufacturer data to be 'qv' where 'q' stands
89        # for unsigned 16-bit integer. Hence, we need to convert the key
90        # from a string, e.g., '0xff00', to its hex value, 0xff00.
91        # For signatures of the advertising properties, refer to
92        #     device_properties in src/third_party/bluez/src/device.c
93        # For explanation about signature types, refer to
94        #     https://dbus.freedesktop.org/doc/dbus-specification.html
95        self.manufacturer_data = {}  # Signature = a{qv}
96        manufacturer_data = advertisement_data.get('ManufacturerData', {})
97        for key, value in manufacturer_data.items():
98            self.manufacturer_data[int(key, 16)] = GLib.Variant('ay', value)
99
100        self.service_data = {}  # Signature = a{sv}
101        service_data = advertisement_data.get('ServiceData', {})
102        for uuid, data in service_data.items():
103            self.service_data[uuid] = GLib.Variant('ay', data)
104
105        self.include_tx_power = advertisement_data.get('IncludeTxPower')
106
107        self.discoverable = advertisement_data.get('Discoverable')
108
109        self.scan_response = advertisement_data.get('ScanResponseData')
110
111        self.min_interval = advertisement_data.get('MinInterval')
112        self.max_interval = advertisement_data.get('MaxInterval')
113
114        self.tx_power = advertisement_data.get('TxPower')
115
116    def get_path(self):
117        """Get the dbus object path of the advertisement.
118
119        @returns: the advertisement object path.
120
121        """
122        return self.path
123
124    def Set(self, interface, prop, value):
125        """Called when bluetoothd Sets a property on our advertising object
126
127        @param interface: String interface, i.e. org.bluez.LEAdvertisement1
128        @param prop: String name of the property being set
129        @param value: Value of the property being set
130        """
131        logging.info('Setting prop {} value to {}'.format(prop, value))
132
133        if prop == 'TxPower':
134            self.tx_power = value
135
136    def GetAll(self, interface):
137        """Get the properties dictionary of the advertisement.
138
139        @param interface: the bluetooth dbus interface.
140
141        @returns: the advertisement properties dictionary.
142
143        """
144        if interface != LE_ADVERTISEMENT_IFACE:
145            raise InvalidArgsException()
146
147        properties = dict()
148        properties['Type'] = GLib.Variant('s', self.type)
149
150        if self.service_uuids is not None:
151            properties['ServiceUUIDs'] = GLib.Variant('as', self.service_uuids)
152        if self.solicit_uuids is not None:
153            properties['SolicitUUIDs'] = GLib.Variant('as', self.solicit_uuids)
154        if self.manufacturer_data is not None:
155            properties['ManufacturerData'] = GLib.Variant(
156                    'a{qv}', self.manufacturer_data)
157
158        if self.service_data is not None:
159            properties['ServiceData'] = GLib.Variant('a{sv}',
160                                                     self.service_data)
161        if self.discoverable is not None:
162            properties['Discoverable'] = GLib.Variant('b', self.discoverable)
163
164        if self.include_tx_power is not None:
165            properties['IncludeTxPower'] = GLib.Variant(
166                    'b', self.include_tx_power)
167
168        # Note here: Scan response data is an int (tag) -> array (value) mapping
169        # but autotest's xmlrpc server can only accept string keys. For this
170        # reason, the scan response key is encoded as a hex string, and then
171        # re-mapped here before the advertisement is registered.
172        if self.scan_response is not None:
173            scan_rsp = {}
174            for key, value in self.scan_response.items():
175                scan_rsp[int(key, 16)] = GLib.Variant('ay', value)
176
177            properties['ScanResponseData'] = GLib.Variant('a{yv}', scan_rsp)
178
179        if self.min_interval is not None:
180            properties['MinInterval'] = GLib.Variant('u', self.min_interval)
181
182        if self.max_interval is not None:
183            properties['MaxInterval'] = GLib.Variant('u', self.max_interval)
184
185        if self.tx_power is not None:
186            properties['TxPower'] = GLib.Variant('n', self.tx_power)
187
188        return properties
189
190    def Release(self):
191        """The method callback at release."""
192        logging.info('%s: Advertisement Release() called.', self.path)
193
194
195def example_advertisement(bus):
196    """A demo example of creating an Advertisement object.
197
198    @param bus: a dbus system bus.
199    @returns: the Advertisement object.
200
201    """
202    ADVERTISEMENT_DATA = {
203            'Path': '/org/bluez/test/advertisement1',
204
205            # Could be 'central' or 'peripheral'.
206            'Type': 'peripheral',
207
208            # Refer to the specification for a list of service assigned numbers:
209            # https://www.bluetooth.com/specifications/gatt/services
210            # e.g., 180D represents "Heart Reate" service, and
211            #       180F "Battery Service".
212            'ServiceUUIDs': ['180D', '180F'],
213
214            # Service solicitation UUIDs.
215            'SolicitUUIDs': [],
216
217            # Two bytes of manufacturer id followed by manufacturer specific data.
218            'ManufacturerData': {
219                    '0xff00': [0xa1, 0xa2, 0xa3, 0xa4, 0xa5]
220            },
221
222            # service UUID followed by additional service data.
223            'ServiceData': {
224                    '9999': [0x10, 0x20, 0x30, 0x40, 0x50]
225            },
226
227            # Does it include transmit power level?
228            'IncludeTxPower': True
229    }
230
231    return Advertisement(bus, ADVERTISEMENT_DATA)
232
233
234if __name__ == '__main__':
235    bus = pydbus.SystemBus()
236    adv = example_advertisement(bus)
237    print(adv.GetAll(LE_ADVERTISEMENT_IFACE))
238