-
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
Minimal LE Audio implementation #308
Closed
Closed
Changes from all commits
Commits
Show all changes
4 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,155 @@ | ||
# Copyright 2021-2023 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 | ||
# ----------------------------------------------------------------------------- | ||
from __future__ import annotations | ||
import enum | ||
import struct | ||
from typing import Optional, List | ||
|
||
from bumble import device | ||
from bumble import gatt | ||
from bumble import gatt_client | ||
|
||
# ----------------------------------------------------------------------------- | ||
# Constants | ||
# ----------------------------------------------------------------------------- | ||
|
||
|
||
class MemberLock(enum.IntEnum): | ||
# fmt: off | ||
UNLOCKED = 0x01 | ||
LOCKED = 0x02 | ||
|
||
|
||
class AseState(enum.IntEnum): | ||
# fmt: off | ||
IDLE = 0x00 | ||
CODEC_CONFIGURED = 0x01 | ||
QOS_CONFIGURED = 0x02 | ||
ENABLING = 0x03 | ||
STREAMING = 0x04 | ||
DISABLING = 0x05 | ||
RELEASING = 0x06 | ||
|
||
|
||
# ----------------------------------------------------------------------------- | ||
# Classes | ||
# ----------------------------------------------------------------------------- | ||
class AudioStreamEndpoint: | ||
state = AseState.IDLE | ||
|
||
def __init__(self, id: int, characteristic: gatt.Characteristic) -> None: | ||
self.id = id | ||
self.characteristic = characteristic | ||
self.characteristic.value = gatt.CharacteristicValue(read=self.on_read) | ||
|
||
def on_read(self, connection: device.Connection) -> bytes: | ||
return struct.pack('BB', self.id, self.state) | ||
|
||
|
||
# ----------------------------------------------------------------------------- | ||
# Server | ||
# ----------------------------------------------------------------------------- | ||
class Service(gatt.TemplateService): | ||
UUID = gatt.GATT_COORDINATED_SET_IDENTIFICATION_SERVICE | ||
|
||
sink_ase: List[AudioStreamEndpoint] = [] | ||
source_ase: List[AudioStreamEndpoint] = [] | ||
|
||
def __init__(self, num_sink_ase: int = 0, num_source_ase: int = 0) -> None: | ||
characteristics = [] | ||
|
||
for i in range(1, num_sink_ase + 1): | ||
characteristic = gatt.Characteristic( | ||
uuid=gatt.GATT_SINK_ASE_CHARACTERISTIC, | ||
properties=gatt.Characteristic.Properties.READ | ||
| gatt.Characteristic.Properties.NOTIFY, | ||
permissions=gatt.Characteristic.Permissions.READABLE, | ||
) | ||
self.sink_ase.append( | ||
AudioStreamEndpoint( | ||
id=i, | ||
characteristic=characteristic, | ||
) | ||
) | ||
characteristics.append(characteristic) | ||
|
||
for i in range(1, num_source_ase + 1): | ||
characteristic = gatt.Characteristic( | ||
uuid=gatt.GATT_SOURCE_ASE_CHARACTERISTIC, | ||
properties=gatt.Characteristic.Properties.READ | ||
| gatt.Characteristic.Properties.NOTIFY, | ||
permissions=gatt.Characteristic.Permissions.READABLE, | ||
) | ||
self.source_ase.append( | ||
AudioStreamEndpoint( | ||
id=i, | ||
characteristic=characteristic, | ||
) | ||
) | ||
characteristics.append(characteristic) | ||
characteristics.append( | ||
gatt.Characteristic( | ||
uuid=gatt.GATT_ASE_CONTROL_POINT_CHARACTERISTIC, | ||
properties=gatt.Characteristic.Properties.WRITE | ||
| gatt.Characteristic.Properties.WRITE_WITHOUT_RESPONSE | ||
| gatt.Characteristic.Properties.NOTIFY, | ||
permissions=gatt.Characteristic.Permissions.READABLE | ||
| gatt.Characteristic.Permissions.WRITEABLE, | ||
value=gatt.CharacteristicValue(write=self.on_ase_control_point), | ||
) | ||
) | ||
|
||
super().__init__(characteristics) | ||
|
||
def on_ase_control_point(self, connection: device.Connection, data: bytes) -> None: | ||
pass | ||
|
||
|
||
# ----------------------------------------------------------------------------- | ||
# Client | ||
# ----------------------------------------------------------------------------- | ||
class ServiceProxy(gatt_client.ProfileServiceProxy): | ||
SERVICE_CLASS = Service | ||
|
||
sirk: gatt_client.CharacteristicProxy | ||
set_size: Optional[gatt_client.CharacteristicProxy] = None | ||
lock: Optional[gatt_client.CharacteristicProxy] = None | ||
rank: Optional[gatt_client.CharacteristicProxy] = None | ||
|
||
def __init__(self, service_proxy: gatt_client.ServiceProxy) -> None: | ||
self.service_proxy = service_proxy | ||
|
||
self.sirk = service_proxy.get_characteristics_by_uuid( | ||
gatt.GATT_SET_IDENTITY_RESOLVING_KEY_CHARACTERISTIC | ||
)[0] | ||
|
||
if characteristics := service_proxy.get_characteristics_by_uuid( | ||
gatt.GATT_COORDINATED_SET_SIZE_CHARACTERISTIC | ||
): | ||
self.set_size = characteristics[0] | ||
|
||
if characteristics := service_proxy.get_characteristics_by_uuid( | ||
gatt.GATT_SET_MEMBER_LOCK_CHARACTERISTIC | ||
): | ||
self.lock = characteristics[0] | ||
|
||
if characteristics := service_proxy.get_characteristics_by_uuid( | ||
gatt.GATT_SET_MEMBER_RANK_CHARACTERISTIC | ||
): | ||
self.rank = characteristics[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,127 @@ | ||
# Copyright 2021-2023 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 | ||
# ----------------------------------------------------------------------------- | ||
from __future__ import annotations | ||
import enum | ||
import struct | ||
from typing import Optional | ||
|
||
from bumble import gatt | ||
from bumble import gatt_client | ||
|
||
# ----------------------------------------------------------------------------- | ||
# Constants | ||
# ----------------------------------------------------------------------------- | ||
|
||
|
||
class MemberLock(enum.IntEnum): | ||
UNLOCKED = 0x01 | ||
LOCKED = 0x02 | ||
|
||
|
||
# ----------------------------------------------------------------------------- | ||
# Server | ||
# ----------------------------------------------------------------------------- | ||
class Service(gatt.TemplateService): | ||
UUID = gatt.GATT_COORDINATED_SET_IDENTIFICATION_SERVICE | ||
|
||
def __init__( | ||
self, | ||
set_identity_resolving_key: bytes, | ||
coordinated_set_size: Optional[int] = None, | ||
set_member_lock: Optional[MemberLock] = None, | ||
set_member_rank: Optional[int] = None, | ||
) -> None: | ||
characteristics = [] | ||
|
||
set_identity_resolving_key_characteristic = gatt.Characteristic( | ||
uuid=gatt.GATT_SET_IDENTITY_RESOLVING_KEY_CHARACTERISTIC, | ||
properties=gatt.Characteristic.Properties.READ | ||
| gatt.Characteristic.Properties.NOTIFY, | ||
permissions=gatt.Characteristic.Permissions.READABLE, | ||
value=set_identity_resolving_key, | ||
) | ||
characteristics.append(set_identity_resolving_key_characteristic) | ||
|
||
if coordinated_set_size is not None: | ||
coordinated_set_size_characteristic = gatt.Characteristic( | ||
uuid=gatt.GATT_SET_IDENTITY_RESOLVING_KEY_CHARACTERISTIC, | ||
properties=gatt.Characteristic.Properties.READ | ||
| gatt.Characteristic.Properties.NOTIFY, | ||
permissions=gatt.Characteristic.Permissions.READABLE, | ||
value=struct.pack('B', coordinated_set_size), | ||
) | ||
characteristics.append(coordinated_set_size_characteristic) | ||
|
||
if set_member_lock is not None: | ||
set_member_lock_characteristic = gatt.Characteristic( | ||
uuid=gatt.GATT_SET_IDENTITY_RESOLVING_KEY_CHARACTERISTIC, | ||
properties=gatt.Characteristic.Properties.READ | ||
| gatt.Characteristic.Properties.NOTIFY | ||
| gatt.Characteristic.Properties.WRITE, | ||
permissions=gatt.Characteristic.Permissions.READABLE | ||
| gatt.Characteristic.Permissions.WRITEABLE, | ||
value=struct.pack('B', set_member_lock), | ||
) | ||
characteristics.append(set_member_lock_characteristic) | ||
|
||
if set_member_rank is not None: | ||
set_member_rank_characteristic = gatt.Characteristic( | ||
uuid=gatt.GATT_SET_IDENTITY_RESOLVING_KEY_CHARACTERISTIC, | ||
properties=gatt.Characteristic.Properties.READ | ||
| gatt.Characteristic.Properties.NOTIFY, | ||
permissions=gatt.Characteristic.Permissions.READABLE, | ||
value=struct.pack('B', set_member_rank), | ||
) | ||
characteristics.append(set_member_rank_characteristic) | ||
|
||
super().__init__(characteristics) | ||
|
||
|
||
# ----------------------------------------------------------------------------- | ||
# Client | ||
# ----------------------------------------------------------------------------- | ||
class ServiceProxy(gatt_client.ProfileServiceProxy): | ||
SERVICE_CLASS = Service | ||
|
||
set_identity_resolving_key: gatt_client.CharacteristicProxy | ||
coordinated_set_size: Optional[gatt_client.CharacteristicProxy] = None | ||
set_member_lock: Optional[gatt_client.CharacteristicProxy] = None | ||
set_member_rank: Optional[gatt_client.CharacteristicProxy] = None | ||
|
||
def __init__(self, service_proxy: gatt_client.ServiceProxy) -> None: | ||
self.service_proxy = service_proxy | ||
|
||
self.set_identity_resolving_key = service_proxy.get_characteristics_by_uuid( | ||
gatt.GATT_SET_IDENTITY_RESOLVING_KEY_CHARACTERISTIC | ||
)[0] | ||
|
||
if characteristics := service_proxy.get_characteristics_by_uuid( | ||
gatt.GATT_COORDINATED_SET_SIZE_CHARACTERISTIC | ||
): | ||
self.coordinated_set_size = characteristics[0] | ||
|
||
if characteristics := service_proxy.get_characteristics_by_uuid( | ||
gatt.GATT_SET_MEMBER_LOCK_CHARACTERISTIC | ||
): | ||
self.set_member_lock = characteristics[0] | ||
|
||
if characteristics := service_proxy.get_characteristics_by_uuid( | ||
gatt.GATT_SET_MEMBER_RANK_CHARACTERISTIC | ||
): | ||
self.set_member_rank = characteristics[0] |
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.
I think this has been copy/pasted by mistake from another profile
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.
No, why do you think so?
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.
The characteristics in the ServiceProxy here aren't the ones from the Service above, but they look like the ones from
csis.py
.