2020-07-20 20:28:21 +00:00
|
|
|
#include <SystemTask/SystemTask.h>
|
2020-07-13 11:40:39 +00:00
|
|
|
#include "MusicService.h"
|
|
|
|
|
|
|
|
int MSCallback(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt *ctxt, void *arg) {
|
|
|
|
auto musicService = static_cast<Pinetime::Controllers::MusicService*>(arg);
|
|
|
|
return musicService->OnCommand(conn_handle, attr_handle, ctxt);
|
|
|
|
}
|
|
|
|
|
2020-07-20 20:28:21 +00:00
|
|
|
Pinetime::Controllers::MusicService::MusicService(Pinetime::System::SystemTask &system) : m_system(system)
|
2020-07-13 11:40:39 +00:00
|
|
|
{
|
|
|
|
msUuid.value[11] = msId[0];
|
|
|
|
msUuid.value[12] = msId[1];
|
|
|
|
msEventCharUuid.value[11] = msEventCharId[0];
|
|
|
|
msEventCharUuid.value[12] = msEventCharId[1];
|
|
|
|
msStatusCharUuid.value[11] = msStatusCharId[0];
|
|
|
|
msStatusCharUuid.value[12] = msStatusCharId[1];
|
|
|
|
msTrackCharUuid.value[11] = msTrackCharId[0];
|
|
|
|
msTrackCharUuid.value[12] = msTrackCharId[1];
|
|
|
|
msArtistCharUuid.value[11] = msArtistCharId[0];
|
|
|
|
msArtistCharUuid.value[12] = msArtistCharId[1];
|
|
|
|
msAlbumCharUuid.value[11] = msAlbumCharId[0];
|
|
|
|
msAlbumCharUuid.value[12] = msAlbumCharId[1];
|
|
|
|
|
2020-07-15 09:02:01 +00:00
|
|
|
characteristicDefinition[0] = { .uuid = (ble_uuid_t*)(&msEventCharUuid),
|
2020-07-13 11:40:39 +00:00
|
|
|
.access_cb = MSCallback,
|
|
|
|
.arg = this,
|
2020-07-15 09:02:01 +00:00
|
|
|
.flags = BLE_GATT_CHR_F_NOTIFY,
|
|
|
|
.val_handle = &m_eventHandle
|
2020-07-13 11:40:39 +00:00
|
|
|
};
|
2020-07-15 09:02:01 +00:00
|
|
|
characteristicDefinition[1] = { .uuid = (ble_uuid_t*)(&msStatusCharUuid),
|
2020-07-13 11:40:39 +00:00
|
|
|
.access_cb = MSCallback,
|
|
|
|
.arg = this,
|
|
|
|
.flags = BLE_GATT_CHR_F_WRITE | BLE_GATT_CHR_F_READ
|
|
|
|
};
|
2020-07-15 09:02:01 +00:00
|
|
|
characteristicDefinition[2] = { .uuid = (ble_uuid_t*)(&msTrackCharUuid),
|
2020-07-13 11:40:39 +00:00
|
|
|
.access_cb = MSCallback,
|
|
|
|
.arg = this,
|
|
|
|
.flags = BLE_GATT_CHR_F_WRITE | BLE_GATT_CHR_F_READ
|
|
|
|
};
|
2020-07-15 09:02:01 +00:00
|
|
|
characteristicDefinition[3] = { .uuid = (ble_uuid_t*)(&msArtistCharUuid),
|
2020-07-13 11:40:39 +00:00
|
|
|
.access_cb = MSCallback,
|
|
|
|
.arg = this,
|
|
|
|
.flags = BLE_GATT_CHR_F_WRITE | BLE_GATT_CHR_F_READ
|
|
|
|
};
|
2020-07-15 09:02:01 +00:00
|
|
|
characteristicDefinition[4] = { .uuid = (ble_uuid_t*)(&msAlbumCharUuid),
|
2020-07-13 11:40:39 +00:00
|
|
|
.access_cb = MSCallback,
|
|
|
|
.arg = this,
|
|
|
|
.flags = BLE_GATT_CHR_F_WRITE | BLE_GATT_CHR_F_READ
|
|
|
|
};
|
|
|
|
characteristicDefinition[5] = {0};
|
|
|
|
|
|
|
|
serviceDefinition[0] = {
|
|
|
|
.type = BLE_GATT_SVC_TYPE_PRIMARY,
|
|
|
|
.uuid = (ble_uuid_t *) &msUuid,
|
|
|
|
.characteristics = characteristicDefinition
|
|
|
|
};
|
|
|
|
serviceDefinition[1] = {0};
|
|
|
|
|
|
|
|
m_artist = "Waiting for";
|
|
|
|
m_album = "";
|
|
|
|
m_track = "track information...";
|
|
|
|
}
|
|
|
|
|
|
|
|
void Pinetime::Controllers::MusicService::Init()
|
|
|
|
{
|
|
|
|
int res = 0;
|
|
|
|
res = ble_gatts_count_cfg(serviceDefinition);
|
|
|
|
ASSERT(res == 0);
|
|
|
|
|
|
|
|
res = ble_gatts_add_svcs(serviceDefinition);
|
|
|
|
ASSERT(res == 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
int Pinetime::Controllers::MusicService::OnCommand(uint16_t conn_handle, uint16_t attr_handle,
|
|
|
|
struct ble_gatt_access_ctxt *ctxt) {
|
|
|
|
|
|
|
|
if (ctxt->op == BLE_GATT_ACCESS_OP_WRITE_CHR) {
|
|
|
|
size_t notifSize = OS_MBUF_PKTLEN(ctxt->om);
|
|
|
|
uint8_t data[notifSize + 1];
|
|
|
|
data[notifSize] = '\0';
|
|
|
|
os_mbuf_copydata(ctxt->om, 0, notifSize, data);
|
|
|
|
char *s = (char *) &data[0];
|
|
|
|
NRF_LOG_INFO("DATA : %s", s);
|
|
|
|
if (ble_uuid_cmp(ctxt->chr->uuid, (ble_uuid_t *)&msArtistCharUuid) == 0) {
|
|
|
|
m_artist = s;
|
|
|
|
} else if (ble_uuid_cmp(ctxt->chr->uuid, (ble_uuid_t *)&msTrackCharUuid) == 0) {
|
|
|
|
m_track = s;
|
|
|
|
} else if (ble_uuid_cmp(ctxt->chr->uuid, (ble_uuid_t *)&msAlbumCharUuid) == 0) {
|
|
|
|
m_album = s;
|
2020-07-20 20:28:21 +00:00
|
|
|
} else if (ble_uuid_cmp(ctxt->chr->uuid, (ble_uuid_t *)&msStatusCharUuid) == 0) {
|
|
|
|
m_status = s[0];
|
2020-07-13 11:40:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string Pinetime::Controllers::MusicService::album()
|
|
|
|
{
|
|
|
|
return m_album;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string Pinetime::Controllers::MusicService::artist()
|
|
|
|
{
|
|
|
|
return m_artist;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string Pinetime::Controllers::MusicService::track()
|
|
|
|
{
|
|
|
|
return m_track;
|
|
|
|
}
|
|
|
|
|
2020-07-20 20:28:21 +00:00
|
|
|
unsigned char Pinetime::Controllers::MusicService::status()
|
|
|
|
{
|
|
|
|
return m_status;
|
|
|
|
}
|
|
|
|
|
2020-07-13 11:40:39 +00:00
|
|
|
void Pinetime::Controllers::MusicService::event(char event)
|
|
|
|
{
|
2020-07-20 20:28:21 +00:00
|
|
|
auto *om = ble_hs_mbuf_from_flat(&event, 1);
|
2020-07-13 11:40:39 +00:00
|
|
|
int ret;
|
|
|
|
|
2020-07-20 20:28:21 +00:00
|
|
|
uint16_t connectionHandle = m_system.nimble().connHandle();
|
2020-07-13 11:40:39 +00:00
|
|
|
|
2020-07-20 20:28:21 +00:00
|
|
|
if (connectionHandle == 0 || connectionHandle == BLE_HS_CONN_HANDLE_NONE) {
|
2020-07-15 09:02:01 +00:00
|
|
|
return;
|
|
|
|
}
|
2020-07-13 11:40:39 +00:00
|
|
|
|
2020-07-15 09:02:01 +00:00
|
|
|
ret = ble_gattc_notify_custom(connectionHandle, m_eventHandle, om);
|
2020-07-13 11:40:39 +00:00
|
|
|
}
|
|
|
|
|