lib_gap.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. sys.exit("Error: Only supported on Linux platform")
  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. ADV_OBJ = False
  31. DBUS_PROP_IFACE = 'org.freedesktop.DBus.Properties'
  32. LE_ADVERTISEMENT_IFACE = 'org.bluez.LEAdvertisement1'
  33. class InvalidArgsException(dbus.exceptions.DBusException):
  34. _dbus_error_name = 'org.freedesktop.DBus.Error.InvalidArgs'
  35. class Advertisement(dbus.service.Object):
  36. PATH_BASE = '/org/bluez/hci0/advertisement'
  37. def __init__(self, bus, index, advertising_type, uuid, name):
  38. self.path = self.PATH_BASE + str(index)
  39. self.bus = bus
  40. self.ad_type = advertising_type
  41. self.service_uuids = [uuid]
  42. self.local_name = dbus.String(name)
  43. dbus.service.Object.__init__(self, bus, self.path)
  44. def __del__(self):
  45. pass
  46. def get_properties(self):
  47. properties = dict()
  48. properties['Type'] = self.ad_type
  49. if self.service_uuids is not None:
  50. properties['ServiceUUIDs'] = dbus.Array(self.service_uuids,
  51. signature='s')
  52. if self.local_name is not None:
  53. properties['LocalName'] = dbus.String(self.local_name)
  54. return {LE_ADVERTISEMENT_IFACE: properties}
  55. def get_path(self):
  56. return dbus.ObjectPath(self.path)
  57. @dbus.service.method(DBUS_PROP_IFACE,
  58. in_signature='s',
  59. out_signature='a{sv}')
  60. def GetAll(self, interface):
  61. global ADV_OBJ
  62. if interface != LE_ADVERTISEMENT_IFACE:
  63. raise InvalidArgsException()
  64. ADV_OBJ = True
  65. return self.get_properties()[LE_ADVERTISEMENT_IFACE]
  66. @dbus.service.method(LE_ADVERTISEMENT_IFACE,
  67. in_signature='',
  68. out_signature='')
  69. def Release(self):
  70. pass