-
Notifications
You must be signed in to change notification settings - Fork 79
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
243 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,151 @@ | ||
from __future__ import annotations | ||
|
||
import struct | ||
from typing import TYPE_CHECKING | ||
|
||
from bumble import att | ||
from bumble import gatt | ||
from bumble import gatt_client | ||
from bumble import crypto | ||
|
||
if TYPE_CHECKING: | ||
from bumble import device | ||
|
||
|
||
# ----------------------------------------------------------------------------- | ||
class GenericAttributeProfileService(gatt.TemplateService): | ||
'''See Vol 3, Part G - 7 - DEFINED GENERIC ATTRIBUTE PROFILE SERVICE.''' | ||
|
||
UUID = gatt.GATT_GENERIC_ATTRIBUTE_SERVICE | ||
|
||
client_supported_features_characteristic: gatt.Characteristic | None = None | ||
server_supported_features_characteristic: gatt.Characteristic | None = None | ||
database_hash_characteristic: gatt.Characteristic | None = None | ||
service_changed_characteristic: gatt.Characteristic | None = None | ||
|
||
def __init__( | ||
self, | ||
server_supported_features: int | None = None, | ||
database_hash_enabled: bool = True, | ||
service_change_enabled: bool = True, | ||
) -> None: | ||
|
||
if server_supported_features is not None: | ||
self.server_supported_features_characteristic = gatt.Characteristic( | ||
uuid=gatt.GATT_SERVER_SUPPORTED_FEATURES_CHARACTERISTIC, | ||
properties=gatt.Characteristic.Properties.READ, | ||
permissions=gatt.Characteristic.Permissions.READABLE, | ||
value=bytes([server_supported_features]), | ||
) | ||
|
||
if database_hash_enabled: | ||
self.database_hash_characteristic = gatt.Characteristic( | ||
uuid=gatt.GATT_DATABASE_HASH_CHARACTERISTIC, | ||
properties=gatt.Characteristic.Properties.READ, | ||
permissions=gatt.Characteristic.Permissions.READABLE, | ||
value=gatt.CharacteristicValue(read=self.get_database_hash), | ||
) | ||
|
||
if service_change_enabled: | ||
self.service_changed_characteristic = gatt.Characteristic( | ||
uuid=gatt.GATT_SERVICE_CHANGED_CHARACTERISTIC, | ||
properties=gatt.Characteristic.Properties.INDICATE, | ||
permissions=gatt.Characteristic.Permissions(0), | ||
value=b'', | ||
) | ||
|
||
if (database_hash_enabled and service_change_enabled) or ( | ||
server_supported_features | ||
and ( | ||
server_supported_features & gatt.ServerSupportedFeatures.EATT_SUPPORTED | ||
) | ||
): # TODO: Support Multiple Handle Value Notifications | ||
self.client_supported_features_characteristic = gatt.Characteristic( | ||
uuid=gatt.GATT_CLIENT_SUPPORTED_FEATURES_CHARACTERISTIC, | ||
properties=( | ||
gatt.Characteristic.Properties.READ | ||
| gatt.Characteristic.Properties.WRITE | ||
), | ||
permissions=( | ||
gatt.Characteristic.Permissions.READABLE | ||
| gatt.Characteristic.Permissions.WRITEABLE | ||
), | ||
value=bytes(1), | ||
) | ||
|
||
super().__init__( | ||
characteristics=[ | ||
c | ||
for c in ( | ||
self.client_supported_features_characteristic, | ||
self.server_supported_features_characteristic, | ||
self.database_hash_characteristic, | ||
self.service_changed_characteristic, | ||
) | ||
if c is not None | ||
], | ||
primary=True, | ||
) | ||
|
||
@classmethod | ||
def get_attribute_data(cls, attribute: att.Attribute) -> bytes: | ||
if attribute.type in ( | ||
gatt.GATT_PRIMARY_SERVICE_ATTRIBUTE_TYPE, | ||
gatt.GATT_SECONDARY_SERVICE_ATTRIBUTE_TYPE, | ||
gatt.GATT_INCLUDE_ATTRIBUTE_TYPE, | ||
gatt.GATT_CHARACTERISTIC_ATTRIBUTE_TYPE, | ||
gatt.GATT_CHARACTERISTIC_EXTENDED_PROPERTIES_DESCRIPTOR, | ||
): | ||
return ( | ||
struct.pack("<H", attribute.handle) | ||
+ attribute.type.to_bytes() | ||
+ attribute.value | ||
) | ||
elif attribute.type in ( | ||
gatt.GATT_CHARACTERISTIC_USER_DESCRIPTION_DESCRIPTOR, | ||
gatt.GATT_CLIENT_CHARACTERISTIC_CONFIGURATION_DESCRIPTOR, | ||
gatt.GATT_SERVER_CHARACTERISTIC_CONFIGURATION_DESCRIPTOR, | ||
gatt.GATT_CHARACTERISTIC_PRESENTATION_FORMAT_DESCRIPTOR, | ||
gatt.GATT_CHARACTERISTIC_AGGREGATE_FORMAT_DESCRIPTOR, | ||
): | ||
return struct.pack("<H", attribute.handle) + attribute.type.to_bytes() | ||
return b'' | ||
|
||
def get_database_hash(self, connection: device.Connection | None) -> bytes: | ||
assert connection | ||
|
||
m = b''.join( | ||
[ | ||
self.get_attribute_data(attribute) | ||
for attribute in connection.device.gatt_server.attributes | ||
] | ||
) | ||
|
||
return crypto.aes_cmac(m=m[::-1], k=bytes(16))[::-1] | ||
|
||
|
||
class GenericAttributeProfileServiceProxy(gatt_client.ProfileServiceProxy): | ||
SERVICE_CLASS = GenericAttributeProfileService | ||
|
||
client_supported_features_characteristic: gatt_client.CharacteristicProxy | None = ( | ||
None | ||
) | ||
server_supported_features_characteristic: gatt_client.CharacteristicProxy | None = ( | ||
None | ||
) | ||
database_hash_characteristic: gatt_client.CharacteristicProxy | None = None | ||
service_changed_characteristic: gatt_client.CharacteristicProxy | None = None | ||
|
||
_CHARACTERISTICS = { | ||
gatt.GATT_CLIENT_SUPPORTED_FEATURES_CHARACTERISTIC: 'client_supported_features_characteristic', | ||
gatt.GATT_SERVER_SUPPORTED_FEATURES_CHARACTERISTIC: 'server_supported_features_characteristic', | ||
gatt.GATT_DATABASE_HASH_CHARACTERISTIC: 'database_hash_characteristic', | ||
gatt.GATT_SERVICE_CHANGED_CHARACTERISTIC: 'service_changed_characteristic', | ||
} | ||
|
||
def __init__(self, service_proxy: gatt_client.ServiceProxy) -> None: | ||
self.service_proxy = service_proxy | ||
|
||
for uuid, attribute_name in self._CHARACTERISTICS.items(): | ||
if characteristic := self.service_proxy.get_characteristics_by_uuid(uuid): | ||
setattr(self, attribute_name, characteristic) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters