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