2020-04-28 13:31:58 -04:00
|
|
|
#include "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>
|
|
|
|
#include "NotificationManager.h"
|
|
|
|
#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
|
|
|
|
|
|
|
|
|
|
|
int AlertNotificationCallback(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt *ctxt, void *arg) {
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-08-17 10:31:00 -04:00
|
|
|
AlertNotificationService::AlertNotificationService ( System::SystemTask& systemTask, NotificationManager& notificationManager )
|
|
|
|
: characteristicDefinition{
|
2020-04-28 13:31:58 -04:00
|
|
|
{
|
|
|
|
.uuid = (ble_uuid_t *) &ansCharUuid,
|
|
|
|
.access_cb = AlertNotificationCallback,
|
|
|
|
.arg = this,
|
|
|
|
.flags = BLE_GATT_CHR_F_WRITE
|
|
|
|
},
|
2020-10-27 14:51:06 -04:00
|
|
|
{
|
2021-01-08 08:21:52 -05:00
|
|
|
.uuid = (ble_uuid_t *) ¬ificationEventUuid,
|
2020-10-27 14:51:06 -04:00
|
|
|
.access_cb = AlertNotificationCallback,
|
|
|
|
.arg = this,
|
|
|
|
.flags = BLE_GATT_CHR_F_NOTIFY,
|
|
|
|
.val_handle = &eventHandle
|
|
|
|
},
|
2020-04-28 13:31:58 -04:00
|
|
|
{
|
|
|
|
0
|
|
|
|
}
|
|
|
|
},
|
2020-10-21 11:31:56 -04:00
|
|
|
serviceDefinition{
|
2020-04-28 13:31:58 -04:00
|
|
|
{
|
|
|
|
/* Device Information Service */
|
|
|
|
.type = BLE_GATT_SVC_TYPE_PRIMARY,
|
|
|
|
.uuid = (ble_uuid_t *) &ansUuid,
|
|
|
|
.characteristics = characteristicDefinition
|
|
|
|
},
|
|
|
|
{
|
|
|
|
0
|
|
|
|
},
|
2020-10-21 11:31:56 -04:00
|
|
|
}, systemTask{systemTask}, notificationManager{notificationManager} {
|
2020-04-28 13:31:58 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
int AlertNotificationService::OnAlert(uint16_t conn_handle, uint16_t attr_handle,
|
|
|
|
struct ble_gatt_access_ctxt *ctxt) {
|
|
|
|
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()};
|
|
|
|
const auto maxBufferSize{maxMessageSize + headerSize};
|
2020-06-28 05:59:14 -04:00
|
|
|
|
2020-10-22 04:43:42 -04:00
|
|
|
const auto dbgPacketLen = OS_MBUF_PKTLEN(ctxt->om);
|
2020-11-15 09:05:51 -05:00
|
|
|
size_t bufferSize = std::min(dbgPacketLen + stringTerminatorSize, maxBufferSize);
|
|
|
|
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;
|
|
|
|
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);
|
2020-10-22 04:43:42 -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
|
|
|
|
switch(category) {
|
|
|
|
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-01-24 11:22:39 -05:00
|
|
|
auto event = Pinetime::System::SystemTask::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;
|
|
|
|
auto *om = ble_hs_mbuf_from_flat(&response, 1);
|
|
|
|
|
|
|
|
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;
|
|
|
|
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;
|
|
|
|
auto *om = ble_hs_mbuf_from_flat(&response, 1);
|
|
|
|
|
|
|
|
uint16_t connectionHandle = systemTask.nimble().connHandle();
|
|
|
|
|
|
|
|
if (connectionHandle == 0 || connectionHandle == BLE_HS_CONN_HANDLE_NONE) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
ble_gattc_notify_custom(connectionHandle, eventHandle, om);
|
|
|
|
}
|