-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
MCP constants and Client implementation
- Loading branch information
Showing
3 changed files
with
590 additions
and
0 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,300 @@ | ||
# Copyright 2021-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 | ||
# ----------------------------------------------------------------------------- | ||
from __future__ import annotations | ||
import dataclasses | ||
import enum | ||
import struct | ||
|
||
from bumble import core | ||
from bumble import gatt | ||
from bumble import gatt_client | ||
from bumble import utils | ||
|
||
from typing import Type, Optional, ClassVar, Dict | ||
from typing_extensions import Self | ||
|
||
# ----------------------------------------------------------------------------- | ||
# Constants | ||
# ----------------------------------------------------------------------------- | ||
|
||
|
||
class PlayingOrder(enum.IntEnum): | ||
''' | ||
See Media Control Service 3.15. Playing Order. | ||
''' | ||
|
||
SINGLE_ONCE = 0x01 | ||
SINGLE_REPEAT = 0x02 | ||
IN_ORDER_ONCE = 0x03 | ||
IN_ORDER_REPEAT = 0x04 | ||
OLDEST_ONCE = 0x05 | ||
OLDEST_REPEAT = 0x06 | ||
NEWEST_ONCE = 0x07 | ||
NEWEST_REPEAT = 0x08 | ||
SHUFFLE_ONCE = 0x09 | ||
SHUFFLE_REPEAT = 0x0A | ||
|
||
|
||
class PlayingOrderSupported(enum.IntFlag): | ||
''' | ||
See Media Control Service 3.16. Playing Orders Supported. | ||
''' | ||
|
||
SINGLE_ONCE = 0x0001 | ||
SINGLE_REPEAT = 0x0002 | ||
IN_ORDER_ONCE = 0x0004 | ||
IN_ORDER_REPEAT = 0x0008 | ||
OLDEST_ONCE = 0x0010 | ||
OLDEST_REPEAT = 0x0020 | ||
NEWEST_ONCE = 0x0040 | ||
NEWEST_REPEAT = 0x0080 | ||
SHUFFLE_ONCE = 0x0100 | ||
SHUFFLE_REPEAT = 0x0200 | ||
|
||
|
||
class MediaState(enum.IntEnum): | ||
''' | ||
See Media Control Service 3.17. Media State. | ||
''' | ||
|
||
INACTIVE = 0x00 | ||
PLAYING = 0x01 | ||
PAUSED = 0x02 | ||
SEEKING = 0x03 | ||
|
||
|
||
class MediaControlPointOpcode(enum.IntEnum): | ||
''' | ||
See Media Control Service 3.18. Media Control Point. | ||
''' | ||
|
||
PLAY = 0x01 | ||
PAUSE = 0x02 | ||
FAST_REWIND = 0x03 | ||
FAST_FORWARD = 0x04 | ||
STOP = 0x05 | ||
MOVE_RELATIVE = 0x10 | ||
PREVIOUS_SEGMENT = 0x20 | ||
NEXT_SEGMENT = 0x21 | ||
FIRST_SEGMENT = 0x22 | ||
LAST_SEGMENT = 0x23 | ||
GOTO_SEGMENT = 0x24 | ||
PREVIOUS_TRACK = 0x30 | ||
NEXT_TRACK = 0x31 | ||
FIRST_TRACK = 0x32 | ||
LAST_TRACK = 0x33 | ||
GOTO_TRACK = 0x34 | ||
PREVIOUS_GROUP = 0x40 | ||
NEXT_GROUP = 0x41 | ||
FIRST_GROUP = 0x42 | ||
LAST_GROUP = 0x43 | ||
GOTO_GROUP = 0x44 | ||
|
||
|
||
class MediaControlPointOpcodeSupported(enum.IntFlag): | ||
''' | ||
See Media Control Service 3.19. Media Control Point Opcodes Supported. | ||
''' | ||
|
||
PLAY = 0x00000001 | ||
PAUSE = 0x00000002 | ||
FAST_REWIND = 0x00000004 | ||
FAST_FORWARD = 0x00000008 | ||
STOP = 0x00000010 | ||
MOVE_RELATIVE = 0x00000020 | ||
PREVIOUS_SEGMENT = 0x00000040 | ||
NEXT_SEGMENT = 0x00000080 | ||
FIRST_SEGMENT = 0x00000100 | ||
LAST_SEGMENT = 0x00000200 | ||
GOTO_SEGMENT = 0x00000400 | ||
PREVIOUS_TRACK = 0x00000800 | ||
NEXT_TRACK = 0x00001000 | ||
FIRST_TRACK = 0x00002000 | ||
LAST_TRACK = 0x00004000 | ||
GOTO_TRACK = 0x00008000 | ||
PREVIOUS_GROUP = 0x00010000 | ||
NEXT_GROUP = 0x00020000 | ||
FIRST_GROUP = 0x00040000 | ||
LAST_GROUP = 0x00080000 | ||
GOTO_GROUP = 0x00100000 | ||
|
||
|
||
class SearchControlPointItemType(enum.IntEnum): | ||
''' | ||
See Media Control Service 3.20. Search Control Point. | ||
''' | ||
|
||
TRACK_NAME = 0x01 | ||
ARTIST_NAME = 0x02 | ||
ALBUM_NAME = 0x03 | ||
GROUP_NAME = 0x04 | ||
EARLIEST_YEAR = 0x05 | ||
LATEST_YEAR = 0x06 | ||
GENRE = 0x07 | ||
ONLY_TRACKS = 0x08 | ||
ONLY_GROUPS = 0x09 | ||
|
||
|
||
class ObjectType(enum.IntEnum): | ||
''' | ||
See Media Control Service 4.4.1. Object Type field. | ||
''' | ||
|
||
TASK = 0 | ||
GROUP = 1 | ||
|
||
|
||
class ObjectId(int): | ||
@classmethod | ||
def parse_from(cls: Type[Self], data: bytes) -> Self: | ||
return cls(int.from_bytes(data, byteorder='little', signed=False)) | ||
|
||
def __bytes__(self) -> bytes: | ||
return self.to_bytes(6, 'little') | ||
|
||
|
||
@dataclasses.dataclass | ||
class GroupObjectType: | ||
object_type: ObjectType | ||
object_id: ObjectId | ||
|
||
@classmethod | ||
def parse_from(cls: Type[Self], data: bytes) -> Self: | ||
return cls( | ||
object_type=ObjectType(data[0]), | ||
object_id=ObjectId.parse_from(data[1:]), | ||
) | ||
|
||
def __bytes__(self) -> bytes: | ||
return bytes([self.object_type]) + bytes(self.object_id) | ||
|
||
|
||
# ----------------------------------------------------------------------------- | ||
# Server | ||
# ----------------------------------------------------------------------------- | ||
class MediaControlService(gatt.TemplateService): | ||
UUID = gatt.GATT_MEDIA_CONTROL_SERVICE | ||
|
||
def __init__(self) -> None: | ||
raise NotImplementedError() | ||
|
||
|
||
class GenericMediaControlService(MediaControlService): | ||
UUID = gatt.GATT_GENERIC_MEDIA_CONTROL_SERVICE | ||
|
||
|
||
# ----------------------------------------------------------------------------- | ||
# Client | ||
# ----------------------------------------------------------------------------- | ||
class MediaControlServiceProxy( | ||
gatt_client.ProfileServiceProxy, utils.CompositeEventEmitter | ||
): | ||
SERVICE_CLASS = MediaControlService | ||
|
||
media_player_name: Optional[gatt_client.CharacteristicProxy] = None | ||
media_player_icon_object_id: Optional[gatt_client.CharacteristicProxy] = None | ||
media_player_icon_url: Optional[gatt_client.CharacteristicProxy] = None | ||
track_changed: Optional[gatt_client.CharacteristicProxy] = None | ||
track_title: Optional[gatt_client.CharacteristicProxy] = None | ||
track_duration: Optional[gatt_client.CharacteristicProxy] = None | ||
track_position: Optional[gatt_client.CharacteristicProxy] = None | ||
playback_speed: Optional[gatt_client.CharacteristicProxy] = None | ||
seeking_speed: Optional[gatt_client.CharacteristicProxy] = None | ||
current_track_segments_object_id: Optional[gatt_client.CharacteristicProxy] = None | ||
current_track_object_id: Optional[gatt_client.CharacteristicProxy] = None | ||
next_track_object_id: Optional[gatt_client.CharacteristicProxy] = None | ||
parent_group_object_id: Optional[gatt_client.CharacteristicProxy] = None | ||
current_group_object_id: Optional[gatt_client.CharacteristicProxy] = None | ||
playing_order: Optional[gatt_client.CharacteristicProxy] = None | ||
playing_orders_supported: Optional[gatt_client.CharacteristicProxy] = None | ||
media_state: Optional[gatt_client.CharacteristicProxy] = None | ||
media_control_point: Optional[gatt_client.CharacteristicProxy] = None | ||
media_control_point_opcodes_supported: Optional[ | ||
gatt_client.CharacteristicProxy | ||
] = None | ||
search_control_point: Optional[gatt_client.CharacteristicProxy] = None | ||
search_results_object_id: Optional[gatt_client.CharacteristicProxy] = None | ||
content_control_id: Optional[gatt_client.CharacteristicProxy] = None | ||
|
||
_CHARACTERISTICS: ClassVar[Dict[str, core.UUID]] = { | ||
'media_player_name': gatt.GATT_MEDIA_PLAYER_NAME_CHARACTERISTIC, | ||
'media_player_icon_object_id': gatt.GATT_MEDIA_PLAYER_ICON_OBJECT_ID_CHARACTERISTIC, | ||
'media_player_icon_url': gatt.GATT_MEDIA_PLAYER_ICON_URL_CHARACTERISTIC, | ||
'track_changed': gatt.GATT_TRACK_CHANGED_CHARACTERISTIC, | ||
'track_title': gatt.GATT_TRACK_TITLE_CHARACTERISTIC, | ||
'track_duration': gatt.GATT_TRACK_DURATION_CHARACTERISTIC, | ||
'track_position': gatt.GATT_TRACK_POSITION_CHARACTERISTIC, | ||
'playback_speed': gatt.GATT_PLAYBACK_SPEED_CHARACTERISTIC, | ||
'seeking_speed': gatt.GATT_SEEKING_SPEED_CHARACTERISTIC, | ||
'current_track_segments_object_id': gatt.GATT_CURRENT_TRACK_SEGMENTS_OBJECT_ID_CHARACTERISTIC, | ||
'current_track_object_id': gatt.GATT_CURRENT_TRACK_OBJECT_ID_CHARACTERISTIC, | ||
'next_track_object_id': gatt.GATT_NEXT_TRACK_OBJECT_ID_CHARACTERISTIC, | ||
'parent_group_object_id': gatt.GATT_PARENT_GROUP_OBJECT_ID_CHARACTERISTIC, | ||
'current_group_object_id': gatt.GATT_CURRENT_GROUP_OBJECT_ID_CHARACTERISTIC, | ||
'playing_order': gatt.GATT_PLAYING_ORDER_CHARACTERISTIC, | ||
'playing_orders_supported': gatt.GATT_PLAYING_ORDERS_SUPPORTED_CHARACTERISTIC, | ||
'media_state': gatt.GATT_MEDIA_STATE_CHARACTERISTIC, | ||
'media_control_point': gatt.GATT_MEDIA_CONTROL_POINT_CHARACTERISTIC, | ||
'media_control_point_opcodes_supported': gatt.GATT_MEDIA_CONTROL_POINT_OPCODES_SUPPORTED_CHARACTERISTIC, | ||
'search_control_point': gatt.GATT_SEARCH_CONTROL_POINT_CHARACTERISTIC, | ||
'search_results_object_id': gatt.GATT_SEARCH_RESULTS_OBJECT_ID_CHARACTERISTIC, | ||
'content_control_id': gatt.GATT_CONTENT_CONTROL_ID_CHARACTERISTIC, | ||
} | ||
|
||
def __init__(self, service_proxy: gatt_client.ServiceProxy) -> None: | ||
utils.CompositeEventEmitter.__init__(self) | ||
self.service_proxy = service_proxy | ||
|
||
for field, uuid in self._CHARACTERISTICS.items(): | ||
if characteristics := service_proxy.get_characteristics_by_uuid(uuid): | ||
setattr(self, field, characteristics[0]) | ||
|
||
async def subscribe_async(): | ||
if self.media_state: | ||
await self.media_state.subscribe(self._on_media_state_change) | ||
if self.track_changed: | ||
await self.track_changed.subscribe(self._on_track_changed) | ||
if self.track_title: | ||
await self.track_title.subscribe(self._on_track_title) | ||
if self.track_duration: | ||
await self.track_duration.subscribe(self._on_track_duration) | ||
if self.track_position: | ||
await self.track_position.subscribe(self._on_track_position) | ||
|
||
service_proxy.client.connection.abort_on('disconnection', subscribe_async()) | ||
|
||
def _on_media_state_change(self, data: bytes) -> None: | ||
self.emit('media_state', MediaState(data[0])) | ||
|
||
def _on_track_changed(self, data: bytes) -> None: | ||
del data | ||
self.emit('track_changed') | ||
|
||
def _on_track_title(self, data: bytes) -> None: | ||
self.emit('track_title', data.decode("utf-8")) | ||
|
||
def _on_track_duration(self, data: bytes) -> None: | ||
self.emit('track_duration', struct.unpack_from('<i', data)[0]) | ||
|
||
def _on_track_position(self, data: bytes) -> None: | ||
self.emit('track_position', struct.unpack_from('<i', data)[0]) | ||
|
||
|
||
class GenericMediaControlServiceProxy(MediaControlServiceProxy): | ||
SERVICE_CLASS = GenericMediaControlService |
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,83 @@ | ||
<html data-bs-theme="dark"> | ||
|
||
<head> | ||
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet" | ||
integrity="sha384-T3c6CoIi6uLrA9TneNEoa7RxnatzjcDSCmG1MXxSR1GAsXEV/Dwwykc2MPK8M2HN" crossorigin="anonymous"> | ||
</head> | ||
|
||
<body> | ||
<nav class="navbar navbar-dark bg-primary"> | ||
<div class="container"> | ||
<span class="navbar-brand mb-0 h1">Bumble LEA Media Control Client</span> | ||
</div> | ||
</nav> | ||
<br> | ||
|
||
<div class="container"> | ||
|
||
<label class="form-label">Server Port</label> | ||
<div class="input-group mb-3"> | ||
<input type="text" class="form-control" aria-label="Port Number" value="8989" id="port"> | ||
<button class="btn btn-primary" type="button" onclick="connect()">Connect</button> | ||
</div> | ||
|
||
<button class="btn btn-primary" onclick="send_opcode(0x01)">Play</button> | ||
<button class="btn btn-primary" onclick="send_opcode(0x02)">Pause</button> | ||
<button class="btn btn-primary" onclick="send_opcode(0x03)">Fast Rewind</button> | ||
<button class="btn btn-primary" onclick="send_opcode(0x04)">Fast Forward</button> | ||
<button class="btn btn-primary" onclick="send_opcode(0x05)">Stop</button> | ||
|
||
</br></br> | ||
|
||
<button class="btn btn-primary" onclick="send_opcode(0x30)">Previous Track</button> | ||
<button class="btn btn-primary" onclick="send_opcode(0x31)">Next Track</button> | ||
|
||
<hr> | ||
|
||
<div id="socketStateContainer" class="bg-body-tertiary p-3 rounded-2"> | ||
<h3>Log</h3> | ||
<code id="log" style="white-space: pre-line;"></code> | ||
</div> | ||
</div> | ||
|
||
|
||
<script> | ||
let portInput = document.getElementById("port") | ||
let log = document.getElementById("log") | ||
let socket | ||
|
||
function connect() { | ||
socket = new WebSocket(`ws://localhost:${portInput.value}`); | ||
socket.onopen = _ => { | ||
log.textContent += 'OPEN\n' | ||
} | ||
socket.onclose = _ => { | ||
log.textContent += 'CLOSED\n' | ||
} | ||
socket.onerror = (error) => { | ||
log.textContent += 'ERROR\n' | ||
console.log(`ERROR: ${error}`) | ||
} | ||
socket.onmessage = (event) => { | ||
log.textContent += `<-- ${event.data}\n` | ||
} | ||
} | ||
|
||
function send(message) { | ||
if (socket && socket.readyState == WebSocket.OPEN) { | ||
let jsonMessage = JSON.stringify(message) | ||
log.textContent += `--> ${jsonMessage}\n` | ||
socket.send(jsonMessage) | ||
} else { | ||
log.textContent += 'NOT CONNECTED\n' | ||
} | ||
} | ||
|
||
function send_opcode(opcode) { | ||
send({ 'opcode': opcode }) | ||
} | ||
</script> | ||
</div> | ||
</body> | ||
|
||
</html> |
Oops, something went wrong.