1# Copyright 2016 The Chromium OS Authors. All rights reserved. 2# Use of this source code is governed by a BSD-style license that can be 3# found in the LICENSE file. 4 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 12import dbus 13import dbus.mainloop.glib 14import dbus.service 15import gobject 16import logging 17 18 19DBUS_PROP_IFACE = 'org.freedesktop.DBus.Properties' 20LE_ADVERTISEMENT_IFACE = 'org.bluez.LEAdvertisement1' 21 22 23class Advertisement(dbus.service.Object): 24 """An advertisement object.""" 25 26 def __init__(self, bus, advertisement_data): 27 """Construction of an Advertisement object. 28 29 @param bus: a dbus system bus. 30 @param advertisement_data: advertisement data dictionary. 31 32 """ 33 self.bus = bus 34 self._get_advertising_data(advertisement_data) 35 super(Advertisement, self).__init__(self.bus, self.path) 36 37 38 def _get_advertising_data(self, advertisement_data): 39 """Get advertising data from the advertisement_data dictionary. 40 41 @param bus: a dbus system bus. 42 43 """ 44 self.path = advertisement_data.get('Path') 45 self.type = advertisement_data.get('Type') 46 self.service_uuids = advertisement_data.get('ServiceUUIDs', []) 47 self.solicit_uuids = advertisement_data.get('SolicitUUIDs', []) 48 49 # Should convert the key of manufacturer_data from string to hex value. 50 # It is due to xmlrpclib limitation which only allows string key. 51 self.manufacturer_data = {} 52 manufacturer_data = advertisement_data.get('ManufacturerData', {}) 53 for key, value in manufacturer_data.items(): 54 self.manufacturer_data[int(key, 16)] = value 55 56 self.service_data = advertisement_data.get('ServiceData') 57 self.include_tx_power = advertisement_data.get('IncludeTxPower') 58 59 60 def get_path(self): 61 """Get the dbus object path of the advertisement. 62 63 @returns: the advertisement object path. 64 65 """ 66 return dbus.ObjectPath(self.path) 67 68 69 @dbus.service.method(DBUS_PROP_IFACE, in_signature='s', 70 out_signature='a{sv}') 71 def GetAll(self, interface): 72 """Get the properties dictionary of the advertisement. 73 74 @param interface: the bluetooth dbus interface. 75 76 @returns: the advertisement properties dictionary. 77 78 """ 79 if interface != LE_ADVERTISEMENT_IFACE: 80 raise InvalidArgsException() 81 82 properties = dict() 83 properties['Type'] = dbus.String(self.type) 84 85 if self.service_uuids is not None: 86 properties['ServiceUUIDs'] = dbus.Array(self.service_uuids, 87 signature='s') 88 if self.solicit_uuids is not None: 89 properties['SolicitUUIDs'] = dbus.Array(self.solicit_uuids, 90 signature='s') 91 if self.manufacturer_data is not None: 92 properties['ManufacturerData'] = dbus.Dictionary( 93 self.manufacturer_data, signature='qay') 94 95 if self.service_data is not None: 96 properties['ServiceData'] = dbus.Dictionary(self.service_data, 97 signature='say') 98 if self.include_tx_power is not None: 99 properties['IncludeTxPower'] = dbus.Boolean(self.include_tx_power) 100 101 return properties 102 103 104 @dbus.service.method(LE_ADVERTISEMENT_IFACE, in_signature='', 105 out_signature='') 106 def Release(self): 107 """The method callback at release.""" 108 logging.info('%s: Advertisement Release() called.', self.path) 109 110 111def example_advertisement(): 112 """A demo example of creating an Advertisement object. 113 114 @returns: the Advertisement object. 115 116 """ 117 ADVERTISEMENT_DATA = { 118 'Path': '/org/bluez/test/advertisement1', 119 120 # Could be 'central' or 'peripheral'. 121 'Type': 'peripheral', 122 123 # Refer to the specification for a list of service assgined numbers: 124 # https://www.bluetooth.com/specifications/gatt/services 125 # e.g., 180D represents "Heart Reate" service, and 126 # 180F "Battery Service". 127 'ServiceUUIDs': ['180D', '180F'], 128 129 # Service solicitation UUIDs. 130 'SolicitUUIDs': [], 131 132 # Two bytes of manufacturer id followed by manufacturer specific data. 133 'ManufacturerData': {'0xff00': [0xa1, 0xa2, 0xa3, 0xa4, 0xa5]}, 134 135 # service UUID followed by additional service data. 136 'ServiceData': {'9999': [0x10, 0x20, 0x30, 0x40, 0x50]}, 137 138 # Does it include transmit power level? 139 'IncludeTxPower': True} 140 141 return Advertisement(bus, ADVERTISEMENT_DATA) 142 143 144if __name__ == '__main__': 145 # It is required to set the mainloop before creating the system bus object. 146 dbus.mainloop.glib.DBusGMainLoop(set_as_default=True) 147 bus = dbus.SystemBus() 148 149 adv = example_advertisement() 150 print adv.GetAll(LE_ADVERTISEMENT_IFACE) 151