Skip to content

Commit

Permalink
Use characteristic handle to identify characteristic instead of chara…
Browse files Browse the repository at this point in the history
…cteristic UUID. Fix ssue hbldh#139.
  • Loading branch information
Plinio Andrade authored and hbldh committed Jun 30, 2020
1 parent e5ec779 commit 125175a
Show file tree
Hide file tree
Showing 12 changed files with 57 additions and 12 deletions.
5 changes: 5 additions & 0 deletions bleak/backends/bluezdbus/characteristic.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ def service_uuid(self) -> str:
"""The uuid of the Service containing this characteristic"""
return self.__service_uuid

@property
def handle(self) -> int:
"""The handle of this characteristic"""
return int(self.obj.get("Handle"))

@property
def uuid(self) -> str:
"""The uuid of this characteristic"""
Expand Down
2 changes: 1 addition & 1 deletion bleak/backends/bluezdbus/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ async def get_services(self) -> BleakGATTServiceCollection:
)
)
self.services.add_descriptor(
BleakGATTDescriptorBlueZDBus(desc, object_path, _characteristic[0].uuid)
BleakGATTDescriptorBlueZDBus(desc, object_path, _characteristic[0].uuid, int(_characteristic[0].handle))
)

self._services_resolved = True
Expand Down
8 changes: 7 additions & 1 deletion bleak/backends/bluezdbus/descriptor.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,18 @@
class BleakGATTDescriptorBlueZDBus(BleakGATTDescriptor):
"""GATT Descriptor implementation for BlueZ DBus backend"""

def __init__(self, obj: dict, object_path: str, characteristic_uuid: str):
def __init__(self, obj: dict, object_path: str, characteristic_uuid: str, characteristic_handle: int):
super(BleakGATTDescriptorBlueZDBus, self).__init__(obj)
self.__path = object_path
self.__characteristic_uuid = characteristic_uuid
self.__characteristic_handle = characteristic_handle
self.__handle = int(self.path.split("/")[-1].replace("desc", ""), 16)

@property
def characteristic_handle(self) -> int:
"""handle for the characteristic that this descriptor belongs to"""
return self.__characteristic_handle

@property
def characteristic_uuid(self) -> str:
"""UUID for the characteristic that this descriptor belongs to"""
Expand Down
7 changes: 7 additions & 0 deletions bleak/backends/characteristic.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,19 @@ def service_uuid(self) -> str:
"""The UUID of the Service containing this characteristic"""
raise NotImplementedError()

@property
@abc.abstractmethod
def handle(self) -> int:
"""The handle for this characteristic"""
raise NotImplementedError()

@property
@abc.abstractmethod
def uuid(self) -> str:
"""The UUID for this characteristic"""
raise NotImplementedError()


@property
@abc.abstractmethod
def description(self) -> str:
Expand Down
5 changes: 5 additions & 0 deletions bleak/backends/corebluetooth/characteristic.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,11 @@ def service_uuid(self) -> str:
"""The uuid of the Service containing this characteristic"""
return self.obj.service().UUID().UUIDString()

@property
def handle(self) -> int:
"""Integer handle for this characteristic"""
return int(self.obj.handle())

@property
def uuid(self) -> str:
"""The uuid of this characteristic"""
Expand Down
2 changes: 1 addition & 1 deletion bleak/backends/corebluetooth/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ async def get_services(self) -> BleakGATTServiceCollection:
for descriptor in descriptors:
self.services.add_descriptor(
BleakGATTDescriptorCoreBluetooth(
descriptor, characteristic.UUID().UUIDString()
descriptor, characteristic.UUID().UUIDString(), int(characteristic.handle())
)
)
self._services_resolved = True
Expand Down
9 changes: 7 additions & 2 deletions bleak/backends/corebluetooth/descriptor.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,20 @@
class BleakGATTDescriptorCoreBluetooth(BleakGATTDescriptor):
"""GATT Descriptor implementation for CoreBluetooth backend"""

def __init__(self, obj: CBDescriptor, characteristic_uuid: str):
def __init__(self, obj: CBDescriptor, characteristic_uuid: str, characteristic_handle: int):
super(BleakGATTDescriptorCoreBluetooth, self).__init__(obj)

self.obj = obj
self.__characteristic_uuid = characteristic_uuid
self.__characteristic_handle = characteristic_handle

def __str__(self):
return "{0}: (Handle: {1})".format(self.uuid, self.handle)

@property
def characteristic_handle(self) -> int:
"""handle for the characteristic that this descriptor belongs to"""
return self.__characteristic_handle

@property
def characteristic_uuid(self) -> str:
"""UUID for the characteristic that this descriptor belongs to"""
Expand Down
6 changes: 6 additions & 0 deletions bleak/backends/descriptor.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@ def characteristic_uuid(self) -> str:
"""UUID for the characteristic that this descriptor belongs to"""
raise NotImplementedError()

@property
@abc.abstractmethod
def characteristic_handle(self) -> int:
"""handle for the characteristic that this descriptor belongs to"""
raise NotImplementedError()

@property
@abc.abstractmethod
def uuid(self) -> str:
Expand Down
7 changes: 6 additions & 1 deletion bleak/backends/dotnet/characteristic.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,18 @@ def __init__(self, obj: GattCharacteristic):
]

def __str__(self):
return "{0}: {1}".format(self.uuid, self.description)
return "[{0}] {1}: {2}".format(self.handle, self.uuid, self.description)

@property
def service_uuid(self) -> str:
"""The uuid of the Service containing this characteristic"""
return self.obj.Service.Uuid.ToString()

@property
def handle(self) -> int:
"""The handle of this characteristic"""
return int(self.obj.AttributeHandle)

@property
def uuid(self) -> str:
"""The uuid of this characteristic"""
Expand Down
2 changes: 1 addition & 1 deletion bleak/backends/dotnet/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ async def get_services(self) -> BleakGATTServiceCollection:
for descriptor in list(descriptors_result.Descriptors):
self.services.add_descriptor(
BleakGATTDescriptorDotNet(
descriptor, characteristic.Uuid.ToString()
descriptor, characteristic.Uuid.ToString(), int(characteristic.AttributeHandle)
)
)

Expand Down
8 changes: 7 additions & 1 deletion bleak/backends/dotnet/descriptor.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,20 @@
class BleakGATTDescriptorDotNet(BleakGATTDescriptor):
"""GATT Descriptor implementation for .NET backend"""

def __init__(self, obj: GattDescriptor, characteristic_uuid: str):
def __init__(self, obj: GattDescriptor, characteristic_uuid: str, characteristic_handle: int):
super(BleakGATTDescriptorDotNet, self).__init__(obj)
self.obj = obj
self.__characteristic_uuid = characteristic_uuid
self.__characteristic_handle = characteristic_handle

def __str__(self):
return "{0}: (Handle: {1})".format(self.uuid, self.handle)

@property
def characteristic_handle(self) -> int:
"""handle for the characteristic that this descriptor belongs to"""
return self.__characteristic_handle

@property
def characteristic_uuid(self) -> str:
"""UUID for the characteristic that this descriptor belongs to"""
Expand Down
8 changes: 4 additions & 4 deletions bleak/backends/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ def services(self) -> dict:

@property
def characteristics(self) -> dict:
"""Returns dictionary of UUID strings to BleakGATTCharacteristic"""
"""Returns dictionary of handle strings to BleakGATTCharacteristic"""
return self.__characteristics

@property
Expand Down Expand Up @@ -111,8 +111,8 @@ def add_characteristic(self, characteristic: BleakGATTCharacteristic):
Should not be used by end user, but rather by `bleak` itself.
"""
if characteristic.uuid not in self.__characteristics:
self.__characteristics[characteristic.uuid] = characteristic
if characteristic.handle not in self.__characteristics:
self.__characteristics[characteristic.handle] = characteristic
self.__services[characteristic.service_uuid].add_characteristic(
characteristic
)
Expand All @@ -132,7 +132,7 @@ def add_descriptor(self, descriptor: BleakGATTDescriptor):
"""
if descriptor.handle not in self.__descriptors:
self.__descriptors[descriptor.handle] = descriptor
self.__characteristics[descriptor.characteristic_uuid].add_descriptor(
self.__characteristics[descriptor.characteristic_handle].add_descriptor(
descriptor
)
else:
Expand Down

0 comments on commit 125175a

Please sign in to comment.