diff --git a/bless/backends/dotnet/characteristic.py b/bless/backends/dotnet/characteristic.py index 54ed4e6..4f8f7c3 100644 --- a/bless/backends/dotnet/characteristic.py +++ b/bless/backends/dotnet/characteristic.py @@ -1,15 +1,12 @@ -from enum import Flag - from bleak.backends.dotnet.characteristic import ( # type: ignore BleakGATTCharacteristicDotNet, ) +from Windows.Devices.Bluetooth.GenericAttributeProfile import ( # type: ignore + GattProtectionLevel +) -class CBAttributePermissions(Flag): - readable = 0x1 - writeable = 0x2 - read_encryption_required = 0x4 - write_encryption_required = 0x8 +from bless.backends.characteristic import GATTAttributePermissions class BlessGATTCharacteristicDotNet(BleakGATTCharacteristicDotNet): @@ -21,6 +18,33 @@ def __init__(self, obj): super().__init__(obj) self._value: bytearray = bytearray(b"") + @staticmethod + def permissions_to_protection_level( + permissions: GATTAttributePermissions, read: bool + ) -> GattProtectionLevel: + """ + Convert the GATTAttributePermissions into a GattProtectionLevel + GATTAttributePermissions currently only consider Encryption or Plain + + Parameters + ---------- + permissions : GATTAttributePermissions + The permission flags for the characteristic + read : bool + If True, processes the permissions for Reading, else process for Writing + + Returns + ------- + GattProtectionLevel + The protection level equivalent + """ + result: GattProtectionLevel = GattProtectionLevel.Plain + shift_value: int = 3 if read else 4 + permission_value: int = permissions.value >> shift_value + if permission_value & 1: + result |= GattProtectionLevel.EncryptionRequired + return result + @property def value(self) -> bytearray: """Get the value of the characteristic""" diff --git a/bless/backends/dotnet/server.py b/bless/backends/dotnet/server.py index ae1ebf5..0a1a1c0 100644 --- a/bless/backends/dotnet/server.py +++ b/bless/backends/dotnet/server.py @@ -12,9 +12,9 @@ from bless.exceptions import BlessError from bless.backends.server import BaseBlessServer # type: ignore from bless.backends.characteristic import ( # type: ignore - GATTCharacteristicProperties, - GATTAttributePermissions - ) + GATTCharacteristicProperties, + GATTAttributePermissions, +) from bless.backends.dotnet.service import BlessGATTServiceDotNet from bless.backends.dotnet.characteristic import ( # type: ignore BlessGATTCharacteristicDotNet, @@ -220,7 +220,16 @@ async def add_new_characteristic( GattLocalCharacteristicParameters() ) ReadParameters.CharacteristicProperties = properties.value - ReadParameters.ReadProtectionLevel = permissions.value + ReadParameters.ReadProtectionLevel = ( + BlessGATTCharacteristicDotNet.permissions_to_protection_level( + permissions, True + ) + ) + ReadParameters.WriteProtectionLevel = ( + BlessGATTCharacteristicDotNet.permissions_to_protection_level( + permissions, False + ) + ) service: GattLocalService = self.services[str(serverguid)] characteristic_result: GattLocalCharacteristicResult = (