Skip to content

Commit

Permalink
Merge pull request #93 from AlanRosenthal/alan/add_default_services
Browse files Browse the repository at this point in the history
Add Device::add_default_services()
  • Loading branch information
barbibulle authored Dec 1, 2022
2 parents 4417eb6 + a8eff73 commit c86125d
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 4 deletions.
9 changes: 6 additions & 3 deletions bumble/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -797,9 +797,7 @@ def __init__(self, name = None, address = None, config = None, host = None, gene
# Register the SDP server with the L2CAP Channel Manager
self.sdp_server.register(self.l2cap_channel_manager)

# Add a GAP Service if requested
if generic_access_service:
self.gatt_server.add_service(GenericAccessService(self.name))
self.add_default_services(generic_access_service)
self.l2cap_channel_manager.register_fixed_channel(ATT_CID, self.on_gatt_pdu)

# Forward some events
Expand Down Expand Up @@ -1886,6 +1884,11 @@ def add_service(self, service):
def add_services(self, services):
self.gatt_server.add_services(services)

def add_default_services(self, generic_access_service=True):
# Add a GAP Service if requested
if generic_access_service:
self.gatt_server.add_service(GenericAccessService(self.name))

async def notify_subscriber(self, connection, attribute, value=None, force=False):
await self.gatt_server.notify_subscriber(connection, attribute, value, force)

Expand Down
23 changes: 22 additions & 1 deletion tests/device_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
HCI_ACCEPT_CONNECTION_REQUEST_COMMAND, HCI_COMMAND_STATUS_PENDING, HCI_CREATE_CONNECTION_COMMAND, HCI_SUCCESS,
Address, HCI_Command_Complete_Event, HCI_Command_Status_Event, HCI_Connection_Complete_Event, HCI_Connection_Request_Event, HCI_Packet
)

from bumble.gatt import GATT_GENERIC_ACCESS_SERVICE, GATT_CHARACTERISTIC_ATTRIBUTE_TYPE, GATT_DEVICE_NAME_CHARACTERISTIC, GATT_APPEARANCE_CHARACTERISTIC

# -----------------------------------------------------------------------------
# Logging
Expand Down Expand Up @@ -182,6 +182,27 @@ async def run_test_device():
await test_device_connect_parallel()


# -----------------------------------------------------------------------------
def test_gatt_services_with_gas():
device = Device(host=Host(None, None))

# there should be one service and two chars, therefore 5 attributes
assert len(device.gatt_server.attributes) == 5
assert device.gatt_server.attributes[0].uuid == GATT_GENERIC_ACCESS_SERVICE
assert device.gatt_server.attributes[1].type == GATT_CHARACTERISTIC_ATTRIBUTE_TYPE
assert device.gatt_server.attributes[2].uuid == GATT_DEVICE_NAME_CHARACTERISTIC
assert device.gatt_server.attributes[3].type == GATT_CHARACTERISTIC_ATTRIBUTE_TYPE
assert device.gatt_server.attributes[4].uuid == GATT_APPEARANCE_CHARACTERISTIC


# -----------------------------------------------------------------------------
def test_gatt_services_without_gas():
device = Device(host=Host(None, None), generic_access_service=False)

# there should be no services
assert len(device.gatt_server.attributes) == 0


# -----------------------------------------------------------------------------
if __name__ == '__main__':
logging.basicConfig(level = os.environ.get('BUMBLE_LOGLEVEL', 'INFO').upper())
Expand Down

0 comments on commit c86125d

Please sign in to comment.