2021-05-16 23:16:50 +00:00
|
|
|
/* Copyright (C) 2020-2021 JF, Adam Pigg, Avamander
|
2020-10-11 00:11:55 +00:00
|
|
|
|
|
|
|
This file is part of InfiniTime.
|
|
|
|
|
|
|
|
InfiniTime is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published
|
|
|
|
by the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
InfiniTime is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
2021-10-13 20:08:35 +00:00
|
|
|
#include "components/ble/MusicService.h"
|
2020-11-15 14:05:51 +00:00
|
|
|
#include "systemtask/SystemTask.h"
|
2020-07-13 11:40:39 +00:00
|
|
|
|
2021-06-12 22:58:53 +00:00
|
|
|
namespace {
|
|
|
|
// 0000yyxx-78fc-48fe-8e23-433b3a1942d0
|
|
|
|
constexpr ble_uuid128_t CharUuid(uint8_t x, uint8_t y) {
|
|
|
|
return ble_uuid128_t{
|
|
|
|
.u = {.type = BLE_UUID_TYPE_128},
|
|
|
|
.value = { 0xd0, 0x42, 0x19, 0x3a, 0x3b, 0x43, 0x23, 0x8e, 0xfe, 0x48, 0xfc, 0x78, x, y, 0x00, 0x00 }
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
// 00000000-78fc-48fe-8e23-433b3a1942d0
|
|
|
|
constexpr ble_uuid128_t BaseUuid() {
|
|
|
|
return CharUuid(0x00, 0x00);
|
|
|
|
}
|
|
|
|
|
|
|
|
constexpr ble_uuid128_t msUuid {BaseUuid()};
|
|
|
|
|
|
|
|
constexpr ble_uuid128_t msEventCharUuid {CharUuid(0x01, 0x00)};
|
|
|
|
constexpr ble_uuid128_t msStatusCharUuid {CharUuid(0x02, 0x00)};
|
|
|
|
constexpr ble_uuid128_t msArtistCharUuid {CharUuid(0x03, 0x00)};
|
|
|
|
constexpr ble_uuid128_t msTrackCharUuid {CharUuid(0x04, 0x00)};
|
|
|
|
constexpr ble_uuid128_t msAlbumCharUuid {CharUuid(0x05, 0x00)};
|
|
|
|
constexpr ble_uuid128_t msPositionCharUuid {CharUuid(0x06, 0x00)};
|
|
|
|
constexpr ble_uuid128_t msTotalLengthCharUuid {CharUuid(0x07, 0x00)};
|
|
|
|
constexpr ble_uuid128_t msTrackNumberCharUuid {CharUuid(0x08, 0x00)};
|
|
|
|
constexpr ble_uuid128_t msTrackTotalCharUuid {CharUuid(0x09, 0x00)};
|
|
|
|
constexpr ble_uuid128_t msPlaybackSpeedCharUuid {CharUuid(0x0a, 0x00)};
|
|
|
|
constexpr ble_uuid128_t msRepeatCharUuid {CharUuid(0x0b, 0x00)};
|
|
|
|
constexpr ble_uuid128_t msShuffleCharUuid {CharUuid(0x0c, 0x00)};
|
|
|
|
|
2021-06-12 22:59:14 +00:00
|
|
|
int MusicCallback(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt* ctxt, void* arg) {
|
|
|
|
return static_cast<Pinetime::Controllers::MusicService*>(arg)->OnCommand(conn_handle, attr_handle, ctxt);
|
|
|
|
}
|
2020-07-13 11:40:39 +00:00
|
|
|
}
|
|
|
|
|
2021-04-18 17:28:14 +00:00
|
|
|
Pinetime::Controllers::MusicService::MusicService(Pinetime::System::SystemTask& system) : m_system(system) {
|
2021-06-07 22:52:27 +00:00
|
|
|
characteristicDefinition[0] = {.uuid = &msEventCharUuid.u,
|
2021-05-16 23:08:12 +00:00
|
|
|
.access_cb = MusicCallback,
|
2021-04-18 17:28:14 +00:00
|
|
|
.arg = this,
|
|
|
|
.flags = BLE_GATT_CHR_F_NOTIFY,
|
|
|
|
.val_handle = &eventHandle};
|
2021-06-07 22:52:27 +00:00
|
|
|
characteristicDefinition[1] = {.uuid = &msStatusCharUuid.u,
|
2021-05-16 23:08:12 +00:00
|
|
|
.access_cb = MusicCallback,
|
2021-04-18 17:28:14 +00:00
|
|
|
.arg = this,
|
|
|
|
.flags = BLE_GATT_CHR_F_WRITE | BLE_GATT_CHR_F_READ};
|
2021-06-07 22:52:27 +00:00
|
|
|
characteristicDefinition[2] = {.uuid = &msTrackCharUuid.u,
|
2021-05-16 23:08:12 +00:00
|
|
|
.access_cb = MusicCallback,
|
2021-04-18 17:28:14 +00:00
|
|
|
.arg = this,
|
|
|
|
.flags = BLE_GATT_CHR_F_WRITE | BLE_GATT_CHR_F_READ};
|
2021-06-07 22:52:27 +00:00
|
|
|
characteristicDefinition[3] = {.uuid = &msArtistCharUuid.u,
|
2021-05-16 23:08:12 +00:00
|
|
|
.access_cb = MusicCallback,
|
2021-04-18 17:28:14 +00:00
|
|
|
.arg = this,
|
|
|
|
.flags = BLE_GATT_CHR_F_WRITE | BLE_GATT_CHR_F_READ};
|
2021-06-07 22:52:27 +00:00
|
|
|
characteristicDefinition[4] = {.uuid = &msAlbumCharUuid.u,
|
2021-05-16 23:08:12 +00:00
|
|
|
.access_cb = MusicCallback,
|
2021-04-18 17:28:14 +00:00
|
|
|
.arg = this,
|
|
|
|
.flags = BLE_GATT_CHR_F_WRITE | BLE_GATT_CHR_F_READ};
|
2021-06-07 22:52:27 +00:00
|
|
|
characteristicDefinition[5] = {.uuid = &msPositionCharUuid.u,
|
2021-05-16 23:08:12 +00:00
|
|
|
.access_cb = MusicCallback,
|
|
|
|
.arg = this,
|
|
|
|
.flags = BLE_GATT_CHR_F_WRITE | BLE_GATT_CHR_F_READ};
|
2021-06-07 22:52:27 +00:00
|
|
|
characteristicDefinition[6] = {.uuid = &msTotalLengthCharUuid.u,
|
2021-05-16 23:08:12 +00:00
|
|
|
.access_cb = MusicCallback,
|
|
|
|
.arg = this,
|
|
|
|
.flags = BLE_GATT_CHR_F_WRITE | BLE_GATT_CHR_F_READ};
|
2021-06-07 22:52:27 +00:00
|
|
|
characteristicDefinition[7] = {.uuid = &msTotalLengthCharUuid.u,
|
2021-05-16 23:08:12 +00:00
|
|
|
.access_cb = MusicCallback,
|
|
|
|
.arg = this,
|
|
|
|
.flags = BLE_GATT_CHR_F_WRITE | BLE_GATT_CHR_F_READ};
|
2021-06-07 22:52:27 +00:00
|
|
|
characteristicDefinition[8] = {.uuid = &msTrackNumberCharUuid.u,
|
2021-05-16 23:08:12 +00:00
|
|
|
.access_cb = MusicCallback,
|
|
|
|
.arg = this,
|
|
|
|
.flags = BLE_GATT_CHR_F_WRITE | BLE_GATT_CHR_F_READ};
|
2021-06-07 22:52:27 +00:00
|
|
|
characteristicDefinition[9] = {.uuid = &msTrackTotalCharUuid.u,
|
2021-05-16 23:08:12 +00:00
|
|
|
.access_cb = MusicCallback,
|
|
|
|
.arg = this,
|
|
|
|
.flags = BLE_GATT_CHR_F_WRITE | BLE_GATT_CHR_F_READ};
|
2021-06-07 22:52:27 +00:00
|
|
|
characteristicDefinition[10] = {.uuid = &msPlaybackSpeedCharUuid.u,
|
2021-05-16 23:08:12 +00:00
|
|
|
.access_cb = MusicCallback,
|
|
|
|
.arg = this,
|
|
|
|
.flags = BLE_GATT_CHR_F_WRITE | BLE_GATT_CHR_F_READ};
|
2021-06-07 22:52:27 +00:00
|
|
|
characteristicDefinition[11] = {.uuid = &msRepeatCharUuid.u,
|
2021-05-16 23:08:12 +00:00
|
|
|
.access_cb = MusicCallback,
|
|
|
|
.arg = this,
|
|
|
|
.flags = BLE_GATT_CHR_F_WRITE | BLE_GATT_CHR_F_READ};
|
2021-06-07 22:52:27 +00:00
|
|
|
characteristicDefinition[12] = {.uuid = &msShuffleCharUuid.u,
|
2021-05-16 23:08:12 +00:00
|
|
|
.access_cb = MusicCallback,
|
2021-04-18 17:28:14 +00:00
|
|
|
.arg = this,
|
|
|
|
.flags = BLE_GATT_CHR_F_WRITE | BLE_GATT_CHR_F_READ};
|
2020-10-11 00:11:55 +00:00
|
|
|
characteristicDefinition[13] = {0};
|
2021-04-18 17:28:14 +00:00
|
|
|
|
2021-05-16 23:08:12 +00:00
|
|
|
serviceDefinition[0] = {
|
2021-06-07 22:52:27 +00:00
|
|
|
.type = BLE_GATT_SVC_TYPE_PRIMARY, .uuid = &msUuid.u, .characteristics = characteristicDefinition};
|
2020-10-11 00:11:55 +00:00
|
|
|
serviceDefinition[1] = {0};
|
2020-07-13 11:40:39 +00:00
|
|
|
}
|
|
|
|
|
2020-10-11 00:11:55 +00:00
|
|
|
void Pinetime::Controllers::MusicService::Init() {
|
2021-05-16 23:08:12 +00:00
|
|
|
uint8_t res = 0;
|
2020-07-13 11:40:39 +00:00
|
|
|
res = ble_gatts_count_cfg(serviceDefinition);
|
|
|
|
ASSERT(res == 0);
|
2021-04-18 17:28:14 +00:00
|
|
|
|
2020-07-13 11:40:39 +00:00
|
|
|
res = ble_gatts_add_svcs(serviceDefinition);
|
|
|
|
ASSERT(res == 0);
|
|
|
|
}
|
|
|
|
|
2021-04-18 17:28:14 +00:00
|
|
|
int Pinetime::Controllers::MusicService::OnCommand(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt* ctxt) {
|
2020-07-13 11:40:39 +00:00
|
|
|
if (ctxt->op == BLE_GATT_ACCESS_OP_WRITE_CHR) {
|
2020-10-11 00:11:55 +00:00
|
|
|
size_t notifSize = OS_MBUF_PKTLEN(ctxt->om);
|
2021-05-16 23:08:12 +00:00
|
|
|
char data[notifSize + 1];
|
2020-10-11 00:11:55 +00:00
|
|
|
data[notifSize] = '\0';
|
|
|
|
os_mbuf_copydata(ctxt->om, 0, notifSize, data);
|
2021-05-16 23:08:12 +00:00
|
|
|
char* s = &data[0];
|
2021-06-07 22:52:27 +00:00
|
|
|
if (ble_uuid_cmp(ctxt->chr->uuid, &msArtistCharUuid.u) == 0) {
|
2020-10-11 00:11:55 +00:00
|
|
|
artistName = s;
|
2021-06-07 22:52:27 +00:00
|
|
|
} else if (ble_uuid_cmp(ctxt->chr->uuid, &msTrackCharUuid.u) == 0) {
|
2020-10-11 00:11:55 +00:00
|
|
|
trackName = s;
|
2021-06-07 22:52:27 +00:00
|
|
|
} else if (ble_uuid_cmp(ctxt->chr->uuid, &msAlbumCharUuid.u) == 0) {
|
2020-10-11 00:11:55 +00:00
|
|
|
albumName = s;
|
2021-06-07 22:52:27 +00:00
|
|
|
} else if (ble_uuid_cmp(ctxt->chr->uuid, &msStatusCharUuid.u) == 0) {
|
2020-10-11 00:11:55 +00:00
|
|
|
playing = s[0];
|
2021-06-07 22:52:27 +00:00
|
|
|
} else if (ble_uuid_cmp(ctxt->chr->uuid, &msRepeatCharUuid.u) == 0) {
|
2020-10-11 00:11:55 +00:00
|
|
|
repeat = s[0];
|
2021-06-07 22:52:27 +00:00
|
|
|
} else if (ble_uuid_cmp(ctxt->chr->uuid, &msShuffleCharUuid.u) == 0) {
|
2020-10-11 00:11:55 +00:00
|
|
|
shuffle = s[0];
|
2021-06-07 22:52:27 +00:00
|
|
|
} else if (ble_uuid_cmp(ctxt->chr->uuid, &msPositionCharUuid.u) == 0) {
|
2020-10-11 00:11:55 +00:00
|
|
|
trackProgress = (s[0] << 24) | (s[1] << 16) | (s[2] << 8) | s[3];
|
2021-06-07 22:52:27 +00:00
|
|
|
} else if (ble_uuid_cmp(ctxt->chr->uuid, &msTotalLengthCharUuid.u) == 0) {
|
2020-10-11 00:11:55 +00:00
|
|
|
trackLength = (s[0] << 24) | (s[1] << 16) | (s[2] << 8) | s[3];
|
2021-06-07 22:52:27 +00:00
|
|
|
} else if (ble_uuid_cmp(ctxt->chr->uuid, &msTrackNumberCharUuid.u) == 0) {
|
2020-10-11 00:11:55 +00:00
|
|
|
trackNumber = (s[0] << 24) | (s[1] << 16) | (s[2] << 8) | s[3];
|
2021-06-07 22:52:27 +00:00
|
|
|
} else if (ble_uuid_cmp(ctxt->chr->uuid, &msTrackTotalCharUuid.u) == 0) {
|
2020-10-11 00:11:55 +00:00
|
|
|
tracksTotal = (s[0] << 24) | (s[1] << 16) | (s[2] << 8) | s[3];
|
2021-06-07 22:52:27 +00:00
|
|
|
} else if (ble_uuid_cmp(ctxt->chr->uuid, &msPlaybackSpeedCharUuid.u) == 0) {
|
2020-10-11 00:11:55 +00:00
|
|
|
playbackSpeed = static_cast<float>(((s[0] << 24) | (s[1] << 16) | (s[2] << 8) | s[3])) / 100.0f;
|
|
|
|
}
|
2020-07-13 11:40:39 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-05-16 23:08:12 +00:00
|
|
|
std::string Pinetime::Controllers::MusicService::getAlbum() const {
|
2020-10-11 00:11:55 +00:00
|
|
|
return albumName;
|
2020-07-13 11:40:39 +00:00
|
|
|
}
|
|
|
|
|
2021-05-16 23:08:12 +00:00
|
|
|
std::string Pinetime::Controllers::MusicService::getArtist() const {
|
2020-10-11 00:11:55 +00:00
|
|
|
return artistName;
|
2020-07-13 11:40:39 +00:00
|
|
|
}
|
|
|
|
|
2021-05-16 23:08:12 +00:00
|
|
|
std::string Pinetime::Controllers::MusicService::getTrack() const {
|
2020-10-11 00:11:55 +00:00
|
|
|
return trackName;
|
2020-07-13 11:40:39 +00:00
|
|
|
}
|
|
|
|
|
2021-05-16 23:08:12 +00:00
|
|
|
bool Pinetime::Controllers::MusicService::isPlaying() const {
|
2020-10-11 00:11:55 +00:00
|
|
|
return playing;
|
2020-07-20 20:28:21 +00:00
|
|
|
}
|
|
|
|
|
2021-05-16 23:08:12 +00:00
|
|
|
float Pinetime::Controllers::MusicService::getPlaybackSpeed() const {
|
2020-10-11 00:11:55 +00:00
|
|
|
return playbackSpeed;
|
|
|
|
}
|
2020-07-13 11:40:39 +00:00
|
|
|
|
2021-05-16 23:08:12 +00:00
|
|
|
int Pinetime::Controllers::MusicService::getProgress() const {
|
|
|
|
return trackProgress;
|
|
|
|
}
|
|
|
|
|
|
|
|
int Pinetime::Controllers::MusicService::getTrackLength() const {
|
|
|
|
return trackLength;
|
|
|
|
}
|
|
|
|
|
2020-10-11 00:11:55 +00:00
|
|
|
void Pinetime::Controllers::MusicService::event(char event) {
|
2021-04-18 17:28:14 +00:00
|
|
|
auto* om = ble_hs_mbuf_from_flat(&event, 1);
|
|
|
|
|
2020-10-11 00:11:55 +00:00
|
|
|
uint16_t connectionHandle = m_system.nimble().connHandle();
|
2021-04-18 17:28:14 +00:00
|
|
|
|
2020-10-11 00:11:55 +00:00
|
|
|
if (connectionHandle == 0 || connectionHandle == BLE_HS_CONN_HANDLE_NONE) {
|
|
|
|
return;
|
|
|
|
}
|
2021-04-18 17:28:14 +00:00
|
|
|
|
2020-10-11 00:11:55 +00:00
|
|
|
ble_gattc_notify_custom(connectionHandle, eventHandle, om);
|
2021-06-07 22:52:27 +00:00
|
|
|
}
|