Skip to content

Commit

Permalink
Merge pull request #36 from kevincar/35-characteristic-protection-level
Browse files Browse the repository at this point in the history
Characteristic protection level
  • Loading branch information
kevincar authored May 8, 2021
2 parents cdf8b28 + ca743b6 commit 5116de9
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 11 deletions.
38 changes: 31 additions & 7 deletions bless/backends/dotnet/characteristic.py
Original file line number Diff line number Diff line change
@@ -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):
Expand All @@ -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"""
Expand Down
17 changes: 13 additions & 4 deletions bless/backends/dotnet/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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 = (
Expand Down

0 comments on commit 5116de9

Please sign in to comment.