2021-10-13 16:08:35 -04:00
|
|
|
#include "components/ble/AlertNotificationService.h"
|
2020-11-15 09:05:51 -05:00
|
|
|
#include <hal/nrf_rtc.h>
|
2020-06-28 05:59:14 -04:00
|
|
|
#include <cstring>
|
2020-11-15 09:05:51 -05:00
|
|
|
#include <algorithm>
|
2021-10-13 16:08:35 -04:00
|
|
|
#include "components/ble/NotificationManager.h"
|
2020-11-15 09:05:51 -05:00
|
|
|
#include "systemtask/SystemTask.h"
|
2020-04-28 13:31:58 -04:00
|
|
|
|
|
|
|
using namespace Pinetime::Controllers;
|
|
|
|
|
|
|
|
constexpr ble_uuid16_t AlertNotificationService::ansUuid;
|
|
|
|
constexpr ble_uuid16_t AlertNotificationService::ansCharUuid;
|
2021-01-08 08:21:52 -05:00
|
|
|
constexpr ble_uuid128_t AlertNotificationService::notificationEventUuid;
|
2020-04-28 13:31:58 -04:00
|
|
|
|
2021-04-18 13:28:14 -04:00
|
|
|
int AlertNotificationCallback(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt* ctxt, void* arg) {
|
2020-04-28 13:31:58 -04:00
|
|
|
auto anService = static_cast<AlertNotificationService*>(arg);
|
|
|
|
return anService->OnAlert(conn_handle, attr_handle, ctxt);
|
|
|
|
}
|
|
|
|
|
|
|
|
void AlertNotificationService::Init() {
|
2020-06-16 14:36:24 -04:00
|
|
|
int res;
|
|
|
|
res = ble_gatts_count_cfg(serviceDefinition);
|
|
|
|
ASSERT(res == 0);
|
|
|
|
|
|
|
|
res = ble_gatts_add_svcs(serviceDefinition);
|
|
|
|
ASSERT(res == 0);
|
2020-04-28 13:31:58 -04:00
|
|
|
}
|
|
|
|
|
2021-04-18 13:28:14 -04:00
|
|
|
AlertNotificationService::AlertNotificationService(System::SystemTask& systemTask, NotificationManager& notificationManager)
|
2021-07-24 11:40:06 -04:00
|
|
|
: characteristicDefinition {{.uuid = &ansCharUuid.u, .access_cb = AlertNotificationCallback, .arg = this, .flags = BLE_GATT_CHR_F_WRITE},
|
|
|
|
{.uuid = ¬ificationEventUuid.u,
|
2021-04-18 13:28:14 -04:00
|
|
|
.access_cb = AlertNotificationCallback,
|
|
|
|
.arg = this,
|
|
|
|
.flags = BLE_GATT_CHR_F_NOTIFY,
|
|
|
|
.val_handle = &eventHandle},
|
|
|
|
{0}},
|
|
|
|
serviceDefinition {
|
|
|
|
{/* Device Information Service */
|
|
|
|
.type = BLE_GATT_SVC_TYPE_PRIMARY,
|
2021-07-24 11:40:06 -04:00
|
|
|
.uuid = &ansUuid.u,
|
2021-04-18 13:28:14 -04:00
|
|
|
.characteristics = characteristicDefinition},
|
|
|
|
{0},
|
|
|
|
},
|
|
|
|
systemTask {systemTask},
|
|
|
|
notificationManager {notificationManager} {
|
2020-04-28 13:31:58 -04:00
|
|
|
}
|
|
|
|
|
2021-04-18 13:28:14 -04:00
|
|
|
int AlertNotificationService::OnAlert(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt* ctxt) {
|
2020-04-28 13:31:58 -04:00
|
|
|
if (ctxt->op == BLE_GATT_ACCESS_OP_WRITE_CHR) {
|
2020-10-22 04:43:42 -04:00
|
|
|
constexpr size_t stringTerminatorSize = 1; // end of string '\0'
|
|
|
|
constexpr size_t headerSize = 3;
|
2020-10-21 11:31:56 -04:00
|
|
|
const auto maxMessageSize {NotificationManager::MaximumMessageSize()};
|
2021-04-18 13:28:14 -04:00
|
|
|
const auto maxBufferSize {maxMessageSize + headerSize};
|
2020-06-28 05:59:14 -04:00
|
|
|
|
2021-04-04 09:19:37 -04:00
|
|
|
// Ignore notifications with empty message
|
|
|
|
const auto packetLen = OS_MBUF_PKTLEN(ctxt->om);
|
2021-04-18 13:28:14 -04:00
|
|
|
if (packetLen <= headerSize)
|
|
|
|
return 0;
|
2021-04-04 09:19:37 -04:00
|
|
|
|
|
|
|
size_t bufferSize = std::min(packetLen + stringTerminatorSize, maxBufferSize);
|
2021-04-18 13:28:14 -04:00
|
|
|
auto messageSize = std::min(maxMessageSize, (bufferSize - headerSize));
|
2021-01-24 11:22:39 -05:00
|
|
|
Categories category;
|
2020-06-28 05:59:14 -04:00
|
|
|
|
2020-10-22 04:43:42 -04:00
|
|
|
NotificationManager::Notification notif;
|
2021-04-18 13:28:14 -04:00
|
|
|
os_mbuf_copydata(ctxt->om, headerSize, messageSize - 1, notif.message.data());
|
2021-01-24 11:22:39 -05:00
|
|
|
os_mbuf_copydata(ctxt->om, 0, 1, &category);
|
2021-04-18 13:28:14 -04:00
|
|
|
notif.message[messageSize - 1] = '\0';
|
2021-04-04 06:10:47 -04:00
|
|
|
notif.size = messageSize;
|
2020-10-27 14:51:06 -04:00
|
|
|
|
2021-01-24 11:22:39 -05:00
|
|
|
// TODO convert all ANS categories to NotificationController categories
|
2021-04-18 13:28:14 -04:00
|
|
|
switch (category) {
|
2021-01-24 11:22:39 -05:00
|
|
|
case Categories::Call:
|
2020-10-27 14:51:06 -04:00
|
|
|
notif.category = Pinetime::Controllers::NotificationManager::Categories::IncomingCall;
|
2021-01-24 11:22:39 -05:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
notif.category = Pinetime::Controllers::NotificationManager::Categories::SimpleAlert;
|
2020-10-27 14:51:06 -04:00
|
|
|
break;
|
|
|
|
}
|
2020-04-28 13:31:58 -04:00
|
|
|
|
2021-06-06 09:56:03 -04:00
|
|
|
auto event = Pinetime::System::Messages::OnNewNotification;
|
2020-10-27 14:51:06 -04:00
|
|
|
notificationManager.Push(std::move(notif));
|
|
|
|
systemTask.PushMessage(event);
|
2020-04-28 13:31:58 -04:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
2020-10-27 14:51:06 -04:00
|
|
|
|
2021-01-24 11:22:39 -05:00
|
|
|
void AlertNotificationService::AcceptIncomingCall() {
|
|
|
|
auto response = IncomingCallResponses::Answer;
|
2021-04-18 13:28:14 -04:00
|
|
|
auto* om = ble_hs_mbuf_from_flat(&response, 1);
|
2021-01-24 11:22:39 -05:00
|
|
|
|
|
|
|
uint16_t connectionHandle = systemTask.nimble().connHandle();
|
|
|
|
|
|
|
|
if (connectionHandle == 0 || connectionHandle == BLE_HS_CONN_HANDLE_NONE) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
ble_gattc_notify_custom(connectionHandle, eventHandle, om);
|
|
|
|
}
|
|
|
|
|
|
|
|
void AlertNotificationService::RejectIncomingCall() {
|
|
|
|
auto response = IncomingCallResponses::Reject;
|
2021-04-18 13:28:14 -04:00
|
|
|
auto* om = ble_hs_mbuf_from_flat(&response, 1);
|
2020-10-27 14:51:06 -04:00
|
|
|
|
|
|
|
uint16_t connectionHandle = systemTask.nimble().connHandle();
|
|
|
|
|
|
|
|
if (connectionHandle == 0 || connectionHandle == BLE_HS_CONN_HANDLE_NONE) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
ble_gattc_notify_custom(connectionHandle, eventHandle, om);
|
|
|
|
}
|
2021-01-27 07:45:06 -05:00
|
|
|
|
|
|
|
void AlertNotificationService::MuteIncomingCall() {
|
|
|
|
auto response = IncomingCallResponses::Mute;
|
2021-04-18 13:28:14 -04:00
|
|
|
auto* om = ble_hs_mbuf_from_flat(&response, 1);
|
2021-01-27 07:45:06 -05:00
|
|
|
|
|
|
|
uint16_t connectionHandle = systemTask.nimble().connHandle();
|
|
|
|
|
|
|
|
if (connectionHandle == 0 || connectionHandle == BLE_HS_CONN_HANDLE_NONE) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
ble_gattc_notify_custom(connectionHandle, eventHandle, om);
|
2021-07-24 11:40:06 -04:00
|
|
|
}
|