lib_gap.py 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. #!/usr/bin/env python
  2. #
  3. # Copyright 2019 Espressif Systems (Shanghai) PTE LTD
  4. #
  5. # Licensed under the Apache License, Version 2.0 (the "License");
  6. # you may not use this file except in compliance with the License.
  7. # You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing, software
  12. # distributed under the License is distributed on an "AS IS" BASIS,
  13. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. # See the License for the specific language governing permissions and
  15. # limitations under the License.
  16. #
  17. # Register Advertisement
  18. from __future__ import print_function
  19. import sys
  20. try:
  21. import dbus
  22. import dbus.service
  23. except ImportError as e:
  24. if 'linux' not in sys.platform:
  25. raise e
  26. print(e)
  27. print('Install packages `libgirepository1.0-dev gir1.2-gtk-3.0 libcairo2-dev libdbus-1-dev libdbus-glib-1-dev` for resolving the issue')
  28. print('Run `pip install -r $IDF_PATH/tools/ble/requirements.txt` for resolving the issue')
  29. raise
  30. DBUS_PROP_IFACE = 'org.freedesktop.DBus.Properties'
  31. LE_ADVERTISEMENT_IFACE = 'org.bluez.LEAdvertisement1'
  32. class InvalidArgsException(dbus.exceptions.DBusException):
  33. _dbus_error_name = 'org.freedesktop.DBus.Error.InvalidArgs'
  34. class Advertisement(dbus.service.Object):
  35. PATH_BASE = '/org/bluez/hci0/advertisement'
  36. def __init__(self, bus, index, advertising_type, uuid, name):
  37. self.path = self.PATH_BASE + str(index)
  38. self.bus = bus
  39. self.ad_type = advertising_type
  40. self.service_uuids = [uuid]
  41. self.local_name = dbus.String(name)
  42. dbus.service.Object.__init__(self, bus, self.path)
  43. def __del__(self):
  44. pass
  45. def get_properties(self):
  46. properties = dict()
  47. properties['Type'] = self.ad_type
  48. if self.service_uuids is not None:
  49. properties['ServiceUUIDs'] = dbus.Array(self.service_uuids,
  50. signature='s')
  51. if self.local_name is not None:
  52. properties['LocalName'] = dbus.String(self.local_name)
  53. return {LE_ADVERTISEMENT_IFACE: properties}
  54. def get_path(self):
  55. return dbus.ObjectPath(self.path)
  56. @dbus.service.method(DBUS_PROP_IFACE,
  57. in_signature='s',
  58. out_signature='a{sv}')
  59. def GetAll(self, interface):
  60. if interface != LE_ADVERTISEMENT_IFACE:
  61. raise InvalidArgsException()
  62. return self.get_properties()[LE_ADVERTISEMENT_IFACE]
  63. @dbus.service.method(LE_ADVERTISEMENT_IFACE,
  64. in_signature='',
  65. out_signature='')
  66. def Release(self):
  67. pass