improve code style

This commit is contained in:
Yuyang Huang
2022-12-06 07:38:05 -08:00
parent 29f9a79502
commit 52db1cfcc1
2 changed files with 12 additions and 18 deletions

View File

@@ -67,13 +67,10 @@ class Server(EventEmitter):
return 1 + len(self.attributes) return 1 + len(self.attributes)
def get_advertising_service_data(self): def get_advertising_service_data(self):
service_advertising_data_map = dict[Service, bytes]() return {
for attribute in self.attributes: attribute: data for attribute in self.attributes
if isinstance(attribute, Service): if isinstance(attribute, Service) and (data := attribute.get_advertising_data())
if not attribute.get_advertising_data(): }
continue
service_advertising_data_map[attribute] = attribute.get_advertising_data()
return service_advertising_data_map
def get_attribute(self, handle): def get_attribute(self, handle):
attribute = self.attributes_by_handle.get(handle) attribute = self.attributes_by_handle.get(handle)

View File

@@ -47,16 +47,12 @@ class AshaService(TemplateService):
PROTOCOL_VERSION = 0x01 PROTOCOL_VERSION = 0x01
RESERVED_FOR_FUTURE_USE = [00, 00] RESERVED_FOR_FUTURE_USE = [00, 00]
FEATURE_MAP = [0x01] # [LE CoC audio output streaming supported] FEATURE_MAP = [0x01] # [LE CoC audio output streaming supported]
SUPPORTED_CODEDC_ID = [0x02, 0x01] # Codec IDs [G.722 at 16 kHz] SUPPORTED_CODEC_ID = [0x02, 0x01] # Codec IDs [G.722 at 16 kHz]
RENDER_DELAY = [00, 00]
def __init__(self, capability: int, hisyncid: [int]): def __init__(self, capability: int, hisyncid: [int]):
self.hisyncid = hisyncid self.hisyncid = hisyncid
self.capability = capability # Device Capabilities [Left, Monaural] self.capability = capability # Device Capabilities [Left, Monaural]
self.render_delay = [00, 00]
self.reserved_for_future_use = [00, 00]
# Four least significant bytes of the HiSyncId.
self.truncated_hisyncid = self.hisyncid[:4]
# Handler for volume control # Handler for volume control
def on_volume_write(connection, value): def on_volume_write(connection, value):
@@ -88,10 +84,10 @@ class AshaService(TemplateService):
self.capability, self.capability,
]) + ]) +
bytes(self.hisyncid) + bytes(self.hisyncid) +
bytes(AshaService.FEATURE_MAP)+ bytes(AshaService.FEATURE_MAP) +
bytes(self.render_delay)+ bytes(AshaService.RENDER_DELAY) +
bytes(AshaService.RESERVED_FOR_FUTURE_USE)+ bytes(AshaService.RESERVED_FOR_FUTURE_USE) +
bytes(AshaService.SUPPORTED_CODEDC_ID) bytes(AshaService.SUPPORTED_CODEC_ID)
) )
self.audio_control_point_characteristic = Characteristic( self.audio_control_point_characteristic = Characteristic(
@@ -132,13 +128,14 @@ class AshaService(TemplateService):
super().__init__(characteristics) super().__init__(characteristics)
def get_advertising_data(self): def get_advertising_data(self):
# Advertisement only uses 4 least significant bytes of the HiSyncId.
return bytes( return bytes(
AdvertisingData([ AdvertisingData([
(AdvertisingData.INCOMPLETE_LIST_OF_16_BIT_SERVICE_CLASS_UUIDS, bytes(GATT_ASHA_SERVICE)), (AdvertisingData.INCOMPLETE_LIST_OF_16_BIT_SERVICE_CLASS_UUIDS, bytes(GATT_ASHA_SERVICE)),
(AdvertisingData.SERVICE_DATA_16_BIT_UUID, bytes(GATT_ASHA_SERVICE) + bytes([ (AdvertisingData.SERVICE_DATA_16_BIT_UUID, bytes(GATT_ASHA_SERVICE) + bytes([
AshaService.PROTOCOL_VERSION, AshaService.PROTOCOL_VERSION,
self.capability, self.capability,
]) + bytes(self.truncated_hisyncid)) ]) + bytes(self.hisyncid[:4]))
]) ])
) )