| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
from __future__ import print_function |
| 5 |
|
| 6 |
import argparse |
| 7 |
import dbus |
| 8 |
import dbus.exceptions |
| 9 |
import dbus.mainloop.glib |
| 10 |
import dbus.service |
| 11 |
import time |
| 12 |
import threading |
| 13 |
|
| 14 |
try: |
| 15 |
from gi.repository import GObject |
| 16 |
except ImportError: |
| 17 |
import gobject as GObject |
| 18 |
|
| 19 |
mainloop = None |
| 20 |
|
| 21 |
BLUEZ_SERVICE_NAME = 'org.bluez' |
| 22 |
LE_ADVERTISING_MANAGER_IFACE = 'org.bluez.LEAdvertisingManager1' |
| 23 |
DBUS_OM_IFACE = 'org.freedesktop.DBus.ObjectManager' |
| 24 |
DBUS_PROP_IFACE = 'org.freedesktop.DBus.Properties' |
| 25 |
|
| 26 |
LE_ADVERTISEMENT_IFACE = 'org.bluez.LEAdvertisement1' |
| 27 |
|
| 28 |
|
| 29 |
class InvalidArgsException(dbus.exceptions.DBusException): |
| 30 |
_dbus_error_name = 'org.freedesktop.DBus.Error.InvalidArgs' |
| 31 |
|
| 32 |
|
| 33 |
class NotSupportedException(dbus.exceptions.DBusException): |
| 34 |
_dbus_error_name = 'org.bluez.Error.NotSupported' |
| 35 |
|
| 36 |
|
| 37 |
class NotPermittedException(dbus.exceptions.DBusException): |
| 38 |
_dbus_error_name = 'org.bluez.Error.NotPermitted' |
| 39 |
|
| 40 |
|
| 41 |
class InvalidValueLengthException(dbus.exceptions.DBusException): |
| 42 |
_dbus_error_name = 'org.bluez.Error.InvalidValueLength' |
| 43 |
|
| 44 |
|
| 45 |
class FailedException(dbus.exceptions.DBusException): |
| 46 |
_dbus_error_name = 'org.bluez.Error.Failed' |
| 47 |
|
| 48 |
|
| 49 |
class Advertisement(dbus.service.Object): |
| 50 |
PATH_BASE = '/org/bluez/example/advertisement' |
| 51 |
|
| 52 |
def __init__(self, bus, index, advertising_type): |
| 53 |
self.path = self.PATH_BASE + str(index) |
| 54 |
self.bus = bus |
| 55 |
self.ad_type = advertising_type |
| 56 |
self.service_uuids = None |
| 57 |
self.manufacturer_data = None |
| 58 |
self.solicit_uuids = None |
| 59 |
self.service_data = None |
| 60 |
self.local_name = None |
| 61 |
self.include_tx_power = False |
| 62 |
self.data = None |
| 63 |
dbus.service.Object.__init__(self, bus, self.path) |
| 64 |
|
| 65 |
def get_properties(self): |
| 66 |
properties = dict() |
| 67 |
properties['Type'] = self.ad_type |
| 68 |
if self.service_uuids is not None: |
| 69 |
properties['ServiceUUIDs'] = dbus.Array(self.service_uuids, |
| 70 |
signature='s') |
| 71 |
if self.solicit_uuids is not None: |
| 72 |
properties['SolicitUUIDs'] = dbus.Array(self.solicit_uuids, |
| 73 |
signature='s') |
| 74 |
if self.manufacturer_data is not None: |
| 75 |
properties['ManufacturerData'] = dbus.Dictionary( |
| 76 |
self.manufacturer_data, signature='qv') |
| 77 |
if self.service_data is not None: |
| 78 |
properties['ServiceData'] = dbus.Dictionary(self.service_data, |
| 79 |
signature='sv') |
| 80 |
if self.local_name is not None: |
| 81 |
properties['LocalName'] = dbus.String(self.local_name) |
| 82 |
if self.include_tx_power: |
| 83 |
properties['Includes'] = dbus.Array(["tx-power"], signature='s') |
| 84 |
|
| 85 |
if self.data is not None: |
| 86 |
properties['Data'] = dbus.Dictionary( |
| 87 |
self.data, signature='yv') |
| 88 |
return {LE_ADVERTISEMENT_IFACE: properties} |
| 89 |
|
| 90 |
def get_path(self): |
| 91 |
return dbus.ObjectPath(self.path) |
| 92 |
|
| 93 |
def add_service_uuid(self, uuid): |
| 94 |
if not self.service_uuids: |
| 95 |
self.service_uuids = [] |
| 96 |
self.service_uuids.append(uuid) |
| 97 |
|
| 98 |
def add_solicit_uuid(self, uuid): |
| 99 |
if not self.solicit_uuids: |
| 100 |
self.solicit_uuids = [] |
| 101 |
self.solicit_uuids.append(uuid) |
| 102 |
|
| 103 |
def add_manufacturer_data(self, manuf_code, data): |
| 104 |
if not self.manufacturer_data: |
| 105 |
self.manufacturer_data = dbus.Dictionary({}, signature='qv') |
| 106 |
self.manufacturer_data[manuf_code] = dbus.Array(data, signature='y') |
| 107 |
|
| 108 |
def add_service_data(self, uuid, data): |
| 109 |
if not self.service_data: |
| 110 |
self.service_data = dbus.Dictionary({}, signature='sv') |
| 111 |
self.service_data[uuid] = dbus.Array(data, signature='y') |
| 112 |
|
| 113 |
def add_local_name(self, name): |
| 114 |
if not self.local_name: |
| 115 |
self.local_name = "" |
| 116 |
self.local_name = dbus.String(name) |
| 117 |
|
| 118 |
def add_data(self, ad_type, data): |
| 119 |
if not self.data: |
| 120 |
self.data = dbus.Dictionary({}, signature='yv') |
| 121 |
self.data[ad_type] = dbus.Array(data, signature='y') |
| 122 |
|
| 123 |
@dbus.service.method(DBUS_PROP_IFACE, |
| 124 |
in_signature='s', |
| 125 |
out_signature='a{sv}') |
| 126 |
def GetAll(self, interface): |
| 127 |
print('GetAll') |
| 128 |
if interface != LE_ADVERTISEMENT_IFACE: |
| 129 |
raise InvalidArgsException() |
| 130 |
print('returning props') |
| 131 |
return self.get_properties()[LE_ADVERTISEMENT_IFACE] |
| 132 |
|
| 133 |
@dbus.service.method(LE_ADVERTISEMENT_IFACE, |
| 134 |
in_signature='', |
| 135 |
out_signature='') |
| 136 |
def Release(self): |
| 137 |
print('%s: Released!' % self.path) |
| 138 |
|
| 139 |
|
| 140 |
class TestAdvertisement(Advertisement): |
| 141 |
|
| 142 |
def __init__(self, bus, index): |
| 143 |
Advertisement.__init__(self, bus, index, 'peripheral') |
| 144 |
self.add_service_uuid('180D') |
| 145 |
self.add_service_uuid('180F') |
| 146 |
self.add_manufacturer_data(0xffff, [0x00, 0x01, 0x02, 0x03]) |
| 147 |
self.add_service_data('9999', [0x00, 0x01, 0x02, 0x03, 0x04]) |
| 148 |
self.add_local_name('TestAdvertisement') |
| 149 |
self.include_tx_power = True |
| 150 |
self.add_data(0x26, [0x01, 0x01, 0x00]) |
| 151 |
|
| 152 |
|
| 153 |
def register_ad_cb(): |
| 154 |
print('Advertisement registered') |
| 155 |
|
| 156 |
|
| 157 |
def register_ad_error_cb(error): |
| 158 |
print('Failed to register advertisement: ' + str(error)) |
| 159 |
mainloop.quit() |
| 160 |
|
| 161 |
|
| 162 |
def find_adapter(bus): |
| 163 |
remote_om = dbus.Interface(bus.get_object(BLUEZ_SERVICE_NAME, '/'), |
| 164 |
DBUS_OM_IFACE) |
| 165 |
objects = remote_om.GetManagedObjects() |
| 166 |
|
| 167 |
for o, props in objects.items(): |
| 168 |
if LE_ADVERTISING_MANAGER_IFACE in props: |
| 169 |
return o |
| 170 |
|
| 171 |
return None |
| 172 |
|
| 173 |
|
| 174 |
def shutdown(timeout): |
| 175 |
print('Advertising for {} seconds...'.format(timeout)) |
| 176 |
time.sleep(timeout) |
| 177 |
mainloop.quit() |
| 178 |
|
| 179 |
|
| 180 |
def main(timeout=0): |
| 181 |
global mainloop |
| 182 |
|
| 183 |
dbus.mainloop.glib.DBusGMainLoop(set_as_default=True) |
| 184 |
|
| 185 |
bus = dbus.SystemBus() |
| 186 |
|
| 187 |
adapter = find_adapter(bus) |
| 188 |
if not adapter: |
| 189 |
print('LEAdvertisingManager1 interface not found') |
| 190 |
return |
| 191 |
|
| 192 |
adapter_props = dbus.Interface(bus.get_object(BLUEZ_SERVICE_NAME, adapter), |
| 193 |
"org.freedesktop.DBus.Properties") |
| 194 |
|
| 195 |
adapter_props.Set("org.bluez.Adapter1", "Powered", dbus.Boolean(1)) |
| 196 |
|
| 197 |
ad_manager = dbus.Interface(bus.get_object(BLUEZ_SERVICE_NAME, adapter), |
| 198 |
LE_ADVERTISING_MANAGER_IFACE) |
| 199 |
|
| 200 |
test_advertisement = TestAdvertisement(bus, 0) |
| 201 |
|
| 202 |
mainloop = GObject.MainLoop() |
| 203 |
|
| 204 |
ad_manager.RegisterAdvertisement(test_advertisement.get_path(), {}, |
| 205 |
reply_handler=register_ad_cb, |
| 206 |
error_handler=register_ad_error_cb) |
| 207 |
|
| 208 |
if timeout > 0: |
| 209 |
threading.Thread(target=shutdown, args=(timeout,)).start() |
| 210 |
else: |
| 211 |
print('Advertising forever...') |
| 212 |
|
| 213 |
mainloop.run() |
| 214 |
|
| 215 |
ad_manager.UnregisterAdvertisement(test_advertisement) |
| 216 |
print('Advertisement unregistered') |
| 217 |
dbus.service.Object.remove_from_connection(test_advertisement) |
| 218 |
|
| 219 |
|
| 220 |
if __name__ == '__main__': |
| 221 |
parser = argparse.ArgumentParser() |
| 222 |
parser.add_argument('--timeout', default=0, type=int, help="advertise " + |
| 223 |
"for this many seconds then stop, 0=run forever " + |
| 224 |
"(default: 0)") |
| 225 |
args = parser.parse_args() |
| 226 |
|
| 227 |
main(args.timeout) |