-
Notifications
You must be signed in to change notification settings - Fork 79
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Gaming Audio Profile
#606
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,198 @@ | ||
# Copyright 2024 Google LLC | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# https://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
"""LE Audio - Gaming Audio Profile""" | ||
|
||
# ----------------------------------------------------------------------------- | ||
# Imports | ||
# ----------------------------------------------------------------------------- | ||
import struct | ||
from typing import Optional | ||
|
||
from bumble.gatt import ( | ||
TemplateService, | ||
DelegatedCharacteristicAdapter, | ||
Characteristic, | ||
GATT_GAMING_AUDIO_SERVICE, | ||
GATT_GMAP_ROLE_CHARACTERISTIC, | ||
GATT_UGG_FEATURES_CHARACTERISTIC, | ||
GATT_UGT_FEATURES_CHARACTERISTIC, | ||
GATT_BGS_FEATURES_CHARACTERISTIC, | ||
GATT_BGR_FEATURES_CHARACTERISTIC, | ||
InvalidServiceError, | ||
) | ||
from bumble.gatt_client import ProfileServiceProxy, ServiceProxy | ||
from enum import IntFlag | ||
|
||
|
||
# ----------------------------------------------------------------------------- | ||
# Classes | ||
# ----------------------------------------------------------------------------- | ||
class GmapRole(IntFlag): | ||
UNICAST_GAME_GATEWAY = 1 << 0 | ||
UNICAST_GAME_TERMINAL = 1 << 1 | ||
BROADCAST_GAME_SENDER = 1 << 2 | ||
BROADCAST_GAME_RECEIVER = 1 << 3 | ||
|
||
|
||
class UggFeatures(IntFlag): | ||
UGG_MULTIPLEX = 1 << 0 | ||
UGG_96_KBPS_SOURCE = 1 << 1 | ||
UGG_MULTISINK = 1 << 2 | ||
|
||
|
||
class UgtFeatures(IntFlag): | ||
UGT_SOURCE = 1 << 0 | ||
UGT_80_KBPS_SOURCE = 1 << 1 | ||
UGT_SINK = 1 << 2 | ||
UGT_64_KBPS_SINK = 1 << 3 | ||
UGT_MULTIPLEX = 1 << 4 | ||
UGT_MULTISINK = 1 << 5 | ||
UGT_MULTISOURCE = 1 << 6 | ||
|
||
|
||
class BgsFeatures(IntFlag): | ||
BGS_96_KBPS = 1 << 0 | ||
|
||
|
||
class BgrFeatures(IntFlag): | ||
BGR_MULTISINK = 1 << 0 | ||
BGR_MULTIPLEX = 1 << 1 | ||
|
||
|
||
# ----------------------------------------------------------------------------- | ||
# Server | ||
# ----------------------------------------------------------------------------- | ||
class GamingAudioService(TemplateService): | ||
UUID = GATT_GAMING_AUDIO_SERVICE | ||
|
||
gmap_role: Characteristic | ||
ugg_features: Optional[Characteristic] = None | ||
ugt_features: Optional[Characteristic] = None | ||
bgs_features: Optional[Characteristic] = None | ||
bgr_features: Optional[Characteristic] = None | ||
|
||
def __init__( | ||
self, | ||
gmap_role: GmapRole, | ||
ugg_features: Optional[UggFeatures] = None, | ||
ugt_features: Optional[UgtFeatures] = None, | ||
bgs_features: Optional[BgsFeatures] = None, | ||
bgr_features: Optional[BgrFeatures] = None, | ||
) -> None: | ||
characteristics = [] | ||
|
||
ugg_features = UggFeatures(0) if ugg_features is None else ugg_features | ||
ugt_features = UgtFeatures(0) if ugt_features is None else ugt_features | ||
bgs_features = BgsFeatures(0) if bgs_features is None else bgs_features | ||
bgr_features = BgrFeatures(0) if bgr_features is None else bgr_features | ||
|
||
self.gmap_role = Characteristic( | ||
uuid=GATT_GMAP_ROLE_CHARACTERISTIC, | ||
properties=Characteristic.Properties.READ, | ||
permissions=Characteristic.Permissions.READABLE, | ||
value=struct.pack('B', gmap_role), | ||
) | ||
characteristics.append(self.gmap_role) | ||
|
||
if gmap_role & GmapRole.UNICAST_GAME_GATEWAY: | ||
self.ugg_features = Characteristic( | ||
uuid=GATT_UGG_FEATURES_CHARACTERISTIC, | ||
properties=Characteristic.Properties.READ, | ||
permissions=Characteristic.Permissions.READABLE, | ||
value=struct.pack('B', ugg_features), | ||
) | ||
characteristics.append(self.ugg_features) | ||
|
||
if gmap_role & GmapRole.UNICAST_GAME_TERMINAL: | ||
self.ugt_features = Characteristic( | ||
uuid=GATT_UGT_FEATURES_CHARACTERISTIC, | ||
properties=Characteristic.Properties.READ, | ||
permissions=Characteristic.Permissions.READABLE, | ||
value=struct.pack('B', ugt_features), | ||
) | ||
characteristics.append(self.ugt_features) | ||
|
||
if gmap_role & GmapRole.BROADCAST_GAME_SENDER: | ||
self.bgs_features = Characteristic( | ||
uuid=GATT_BGS_FEATURES_CHARACTERISTIC, | ||
properties=Characteristic.Properties.READ, | ||
permissions=Characteristic.Permissions.READABLE, | ||
value=struct.pack('B', bgs_features), | ||
) | ||
characteristics.append(self.bgs_features) | ||
|
||
if gmap_role & GmapRole.BROADCAST_GAME_RECEIVER: | ||
self.bgr_features = Characteristic( | ||
uuid=GATT_BGR_FEATURES_CHARACTERISTIC, | ||
properties=Characteristic.Properties.READ, | ||
permissions=Characteristic.Permissions.READABLE, | ||
value=struct.pack('B', bgr_features), | ||
) | ||
characteristics.append(self.bgr_features) | ||
|
||
super().__init__(characteristics) | ||
|
||
|
||
# ----------------------------------------------------------------------------- | ||
# Client | ||
# ----------------------------------------------------------------------------- | ||
class GamingAudioServiceProxy(ProfileServiceProxy): | ||
SERVICE_CLASS = GamingAudioService | ||
|
||
def __init__(self, service_proxy: ServiceProxy) -> None: | ||
self.service_proxy = service_proxy | ||
|
||
if not ( | ||
characteristics := service_proxy.get_characteristics_by_uuid( | ||
GATT_GMAP_ROLE_CHARACTERISTIC | ||
) | ||
): | ||
raise InvalidServiceError("GMAP Role Characteristic not found") | ||
self.gmap_role = DelegatedCharacteristicAdapter( | ||
characteristic=characteristics[0], | ||
decode=lambda value: GmapRole(value[0]), | ||
) | ||
|
||
if characteristics := service_proxy.get_characteristics_by_uuid( | ||
GATT_UGG_FEATURES_CHARACTERISTIC | ||
): | ||
self.ugg_features = DelegatedCharacteristicAdapter( | ||
characteristic=characteristics[0], | ||
decode=lambda value: UggFeatures(value[0]), | ||
) | ||
|
||
if characteristics := service_proxy.get_characteristics_by_uuid( | ||
GATT_UGT_FEATURES_CHARACTERISTIC | ||
): | ||
self.ugt_features = DelegatedCharacteristicAdapter( | ||
characteristic=characteristics[0], | ||
decode=lambda value: UgtFeatures(value[0]), | ||
) | ||
|
||
if characteristics := service_proxy.get_characteristics_by_uuid( | ||
GATT_BGS_FEATURES_CHARACTERISTIC | ||
): | ||
self.bgs_features = DelegatedCharacteristicAdapter( | ||
characteristic=characteristics[0], | ||
decode=lambda value: BgsFeatures(value[0]), | ||
) | ||
|
||
if characteristics := service_proxy.get_characteristics_by_uuid( | ||
GATT_BGR_FEATURES_CHARACTERISTIC | ||
): | ||
self.bgr_features = DelegatedCharacteristicAdapter( | ||
characteristic=characteristics[0], | ||
decode=lambda value: BgrFeatures(value[0]), | ||
) |
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,84 @@ | ||
# Copyright 2024 Google LLC | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# https://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
|
||
# ----------------------------------------------------------------------------- | ||
# Imports | ||
# ----------------------------------------------------------------------------- | ||
import pytest | ||
import pytest_asyncio | ||
|
||
from bumble import device | ||
from bumble.profiles.gmap import ( | ||
GamingAudioService, | ||
GamingAudioServiceProxy, | ||
GmapRole, | ||
UggFeatures, | ||
UgtFeatures, | ||
BgrFeatures, | ||
BgsFeatures, | ||
) | ||
|
||
from .test_utils import TwoDevices | ||
|
||
# ----------------------------------------------------------------------------- | ||
# Tests | ||
# ----------------------------------------------------------------------------- | ||
gmas_service = GamingAudioService( | ||
gmap_role=GmapRole.UNICAST_GAME_GATEWAY | ||
| GmapRole.UNICAST_GAME_TERMINAL | ||
| GmapRole.BROADCAST_GAME_RECEIVER | ||
| GmapRole.BROADCAST_GAME_SENDER, | ||
ugg_features=UggFeatures.UGG_MULTISINK, | ||
ugt_features=UgtFeatures.UGT_SOURCE, | ||
bgr_features=BgrFeatures.BGR_MULTISINK, | ||
bgs_features=BgsFeatures.BGS_96_KBPS, | ||
) | ||
|
||
|
||
@pytest_asyncio.fixture | ||
async def gmap_client(): | ||
devices = TwoDevices() | ||
devices[0].add_service(gmas_service) | ||
|
||
await devices.setup_connection() | ||
|
||
assert devices.connections[0] | ||
assert devices.connections[1] | ||
|
||
devices.connections[0].encryption = 1 | ||
devices.connections[1].encryption = 1 | ||
|
||
peer = device.Peer(devices.connections[1]) | ||
|
||
gmap_client = await peer.discover_service_and_create_proxy(GamingAudioServiceProxy) | ||
|
||
assert gmap_client | ||
yield gmap_client | ||
|
||
|
||
# ----------------------------------------------------------------------------- | ||
@pytest.mark.asyncio | ||
async def test_init_service(gmap_client: GamingAudioServiceProxy): | ||
assert ( | ||
await gmap_client.gmap_role.read_value() | ||
== GmapRole.UNICAST_GAME_GATEWAY | ||
| GmapRole.UNICAST_GAME_TERMINAL | ||
| GmapRole.BROADCAST_GAME_RECEIVER | ||
| GmapRole.BROADCAST_GAME_SENDER | ||
) | ||
assert await gmap_client.ugg_features.read_value() == UggFeatures.UGG_MULTISINK | ||
assert await gmap_client.ugt_features.read_value() == UgtFeatures.UGT_SOURCE | ||
assert await gmap_client.bgr_features.read_value() == BgrFeatures.BGR_MULTISINK | ||
assert await gmap_client.bgs_features.read_value() == BgsFeatures.BGS_96_KBPS |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You still need to set a value when parameters are None, otherwise the attribute will be missing.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Even if in given configuration a characteristic should not exist in the service?
As per the GMAP specification, section 4.7 Service characteristics, Table 4.1. GMAS characteristics, depending on the
GMAP Role
the other 4 characteristics are either mandatory or excluded, thus I added the role check before adding those conditional characteristics.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Correct. It's not really related to Bluetooth, but from a readability perspective, you should always set a value for any attribute, especially when you annotate them as
Optional
, people will expect there is such an attribute even they areNone
.Consider if you are using Java, such a pattern will probably be warned that you may access a member without initialization.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @zxzxwu , that would mean that we will have characteristics exposed which are not really needed.
Also from the testing point of view, I see value in having an option to create invalid test device which is missing some characteristics to verify proper error handling on the DUT.
Does it makes sense?
We also had an idea that some of the characteristic could be created as per spec (depends on the role) but in fact as it is now gives good test flexibility.
Other options would be to extend this constructor with a bool's which would tell if given characteristic should be created but this seems to be overkill.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You don't need to create a characteristic. What is needed here is
Or set it like
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you, now it is clear. I added the latter suggestion.