mirror of
https://github.com/google/bumble.git
synced 2026-05-08 03:58:01 +00:00
Implement GATT server included service declaration
This commit is contained in:
@@ -205,8 +205,16 @@ class Service(Attribute):
|
|||||||
'''
|
'''
|
||||||
|
|
||||||
uuid: UUID
|
uuid: UUID
|
||||||
|
characteristics: List[Characteristic]
|
||||||
|
included_services: List[Service]
|
||||||
|
|
||||||
def __init__(self, uuid, characteristics: List[Characteristic], primary=True):
|
def __init__(
|
||||||
|
self,
|
||||||
|
uuid,
|
||||||
|
characteristics: List[Characteristic],
|
||||||
|
included_services: List[Service] = [],
|
||||||
|
primary=True,
|
||||||
|
):
|
||||||
# Convert the uuid to a UUID object if it isn't already
|
# Convert the uuid to a UUID object if it isn't already
|
||||||
if isinstance(uuid, str):
|
if isinstance(uuid, str):
|
||||||
uuid = UUID(uuid)
|
uuid = UUID(uuid)
|
||||||
@@ -219,7 +227,7 @@ class Service(Attribute):
|
|||||||
uuid.to_pdu_bytes(),
|
uuid.to_pdu_bytes(),
|
||||||
)
|
)
|
||||||
self.uuid = uuid
|
self.uuid = uuid
|
||||||
# self.included_services = []
|
self.included_services = included_services[:]
|
||||||
self.characteristics = characteristics[:]
|
self.characteristics = characteristics[:]
|
||||||
self.primary = primary
|
self.primary = primary
|
||||||
|
|
||||||
@@ -253,6 +261,33 @@ class TemplateService(Service):
|
|||||||
super().__init__(self.UUID, characteristics, primary)
|
super().__init__(self.UUID, characteristics, primary)
|
||||||
|
|
||||||
|
|
||||||
|
# -----------------------------------------------------------------------------
|
||||||
|
class IncludedServiceDeclaration(Attribute):
|
||||||
|
'''
|
||||||
|
See Vol 3, Part G - 3.2 INCLUDE DEFINITION
|
||||||
|
'''
|
||||||
|
|
||||||
|
service: Service
|
||||||
|
|
||||||
|
def __init__(self, service):
|
||||||
|
declaration_bytes = struct.pack(
|
||||||
|
'<HH2s', service.handle, service.end_group_handle, service.uuid.to_bytes()
|
||||||
|
)
|
||||||
|
super().__init__(
|
||||||
|
GATT_INCLUDE_ATTRIBUTE_TYPE, Attribute.READABLE, declaration_bytes
|
||||||
|
)
|
||||||
|
self.service = service
|
||||||
|
|
||||||
|
def __str__(self):
|
||||||
|
return (
|
||||||
|
f'IncludedServiceDefinition(handle=0x{self.handle:04X}, '
|
||||||
|
f'group_starting_handle=0x{self.service.handle:04X}, '
|
||||||
|
f'group_ending_handle=0x{self.service.end_group_handle:04X}, '
|
||||||
|
f'uuid={self.service.uuid}, '
|
||||||
|
f'{self.service.properties!s})'
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
# -----------------------------------------------------------------------------
|
# -----------------------------------------------------------------------------
|
||||||
class Characteristic(Attribute):
|
class Characteristic(Attribute):
|
||||||
'''
|
'''
|
||||||
|
|||||||
@@ -68,6 +68,7 @@ from .gatt import (
|
|||||||
Characteristic,
|
Characteristic,
|
||||||
CharacteristicDeclaration,
|
CharacteristicDeclaration,
|
||||||
CharacteristicValue,
|
CharacteristicValue,
|
||||||
|
IncludedServiceDeclaration,
|
||||||
Descriptor,
|
Descriptor,
|
||||||
Service,
|
Service,
|
||||||
)
|
)
|
||||||
@@ -94,6 +95,7 @@ class Server(EventEmitter):
|
|||||||
def __init__(self, device):
|
def __init__(self, device):
|
||||||
super().__init__()
|
super().__init__()
|
||||||
self.device = device
|
self.device = device
|
||||||
|
self.services = []
|
||||||
self.attributes = [] # Attributes, ordered by increasing handle values
|
self.attributes = [] # Attributes, ordered by increasing handle values
|
||||||
self.attributes_by_handle = {} # Map for fast attribute access by handle
|
self.attributes_by_handle = {} # Map for fast attribute access by handle
|
||||||
self.max_mtu = (
|
self.max_mtu = (
|
||||||
@@ -222,7 +224,14 @@ class Server(EventEmitter):
|
|||||||
# Add the service attribute to the DB
|
# Add the service attribute to the DB
|
||||||
self.add_attribute(service)
|
self.add_attribute(service)
|
||||||
|
|
||||||
# TODO: add included services
|
# Add all included service
|
||||||
|
for included_service in service.included_services:
|
||||||
|
# Not registered yet, register the included service first.
|
||||||
|
if included_service not in self.services:
|
||||||
|
self.add_service(included_service)
|
||||||
|
# TODO: Handle circular service reference
|
||||||
|
include_declaration = IncludedServiceDeclaration(included_service)
|
||||||
|
self.add_attribute(include_declaration)
|
||||||
|
|
||||||
# Add all characteristics
|
# Add all characteristics
|
||||||
for characteristic in service.characteristics:
|
for characteristic in service.characteristics:
|
||||||
@@ -274,6 +283,7 @@ class Server(EventEmitter):
|
|||||||
|
|
||||||
# Update the service group end
|
# Update the service group end
|
||||||
service.end_group_handle = self.attributes[-1].handle
|
service.end_group_handle = self.attributes[-1].handle
|
||||||
|
self.services.append(service)
|
||||||
|
|
||||||
def add_services(self, services):
|
def add_services(self, services):
|
||||||
for service in services:
|
for service in services:
|
||||||
|
|||||||
Reference in New Issue
Block a user