class BLEGattCharacteristic(object): char_uuid = BLEUUID(BLEUUID.Standard.characteristic) def __init__(self, uuid, handle_decl, handle_value, data_decl=None, data_value=None, char_props=None): self.uuid = uuid self.handle_decl = handle_decl self.handle_value = handle_value self.data_decl = data_decl self.data_value = data_value self.char_props = char_props # TODO: if None, parse first byte of data_decl? self.end_handle = None self.descs = list() @classmethod def from_c(cls, gattc_char): return cls(uuid=BLEUUID.from_c(gattc_char.uuid), handle_decl=gattc_char.handle_decl, handle_value=gattc_char.handle_value, char_props=BLEGattCharacteristicProperties.from_c( gattc_char.char_props)) def __repr__(self): return "{}(uuid={}, handle_decl={}, handle_value={}, data_decl={}, data_value={}, end_handle={})".format( self.__class__.__name__, self.uuid, self.handle_decl, self.handle_value, self.data_decl, self.data_value, self.end_handle)
class BLEGattService(object): srvc_uuid = BLEUUID(BLEUUID.Standard.service_primary) def __init__(self, uuid, start_handle, end_handle): self.uuid = uuid self.start_handle = start_handle self.end_handle = end_handle self.chars = [] @classmethod def from_c(cls, gattc_service): return cls(uuid=BLEUUID.from_c(gattc_service.uuid), start_handle=gattc_service.handle_range.start_handle, end_handle=gattc_service.handle_range.end_handle) def char_add(self, char): char.end_handle = self.end_handle self.chars.append(char) if len(self.chars) > 1: self.chars[-2].end_handle = char.handle_decl - 1 def __repr__(self): return "{}(uuid={}, start_handle={}, end_handle={})".format( self.__class__.__name__, self.uuid, self.start_handle, self.end_handle)
class BLEGattCharacteristic(object): char_uuid = BLEUUID(BLEUUID.Standard.characteristic) def __init__(self, uuid, handle_decl, handle_value, data_decl=None, data_value=None, char_props=None): self.uuid = uuid self.handle_decl = handle_decl self.handle_value = handle_value self.data_decl = data_decl self.data_value = data_value self.char_props = char_props # TODO: if None, parse first byte of data_decl? self.end_handle = None self.descs = list() def discovered_handles(self): return sorted([self.handle_decl, self.handle_value] + [d.handle for d in self.descs]) def missing_handles(self): all_handles = set(range(self.handle_decl, self.end_handle+1)) return sorted(all_handles - set(self.discovered_handles())) @classmethod def from_c(cls, gattc_char): return cls(uuid=BLEUUID.from_c(gattc_char.uuid), handle_decl=gattc_char.handle_decl, handle_value=gattc_char.handle_value, char_props=BLEGattCharacteristicProperties.from_c(gattc_char.char_props)) def __repr__(self): return "{}(uuid={}, handle_decl={}, handle_value={}, data_decl={}, data_value={}, end_handle={})".format( self.__class__.__name__, self.uuid, self.handle_decl, self.handle_value, self.data_decl, self.data_value, self.end_handle )
class BLEGattService(object): srvc_uuid = BLEUUID(BLEUUID.Standard.service_primary) def __init__(self, uuid, start_handle, end_handle): self.uuid = uuid self.start_handle = start_handle self.end_handle = end_handle self.chars = list() @classmethod def from_c(cls, gattc_service): return cls(uuid=BLEUUID.from_c(gattc_service.uuid), start_handle=gattc_service.handle_range.start_handle, end_handle=gattc_service.handle_range.end_handle) def char_add(self, char): char.end_handle = self.end_handle self.chars.append(char) if len(self.chars) > 1: self.chars[-2].end_handle = char.handle_decl - 1
def from_c(cls, gattc_char): return cls(uuid=BLEUUID.from_c(gattc_char.uuid), handle_decl=gattc_char.handle_decl, handle_value=gattc_char.handle_value, char_props=BLEGattCharacteristicProperties.from_c(gattc_char.char_props))
def from_c(cls, gattc_service): return cls(uuid=BLEUUID.from_c(gattc_service.uuid), start_handle=gattc_service.handle_range.start_handle, end_handle=gattc_service.handle_range.end_handle)
def from_c(cls, attr_info128): uuid = BLEUUID.from_uuid128(attr_info128.uuid) attr_handle = attr_info128.handle return cls(attr_handle, uuid)
def from_c(cls, attr_info16): handle = attr_info16.handle uuid = BLEUUID.from_c(attr_info16.uuid) return cls(handle, uuid)
def from_c(cls, gattc_desc): return cls(uuid=BLEUUID.from_c(gattc_desc.uuid), handle=gattc_desc.handle)