2020-09-27 18:59:06 +00:00
|
|
|
#include "ImmediateAlertService.h"
|
2020-11-15 14:05:51 +00:00
|
|
|
#include <cstring>
|
|
|
|
#include "NotificationManager.h"
|
|
|
|
#include "systemtask/SystemTask.h"
|
2020-09-27 18:59:06 +00:00
|
|
|
|
|
|
|
using namespace Pinetime::Controllers;
|
|
|
|
|
|
|
|
constexpr ble_uuid16_t ImmediateAlertService::immediateAlertServiceUuid;
|
|
|
|
constexpr ble_uuid16_t ImmediateAlertService::alertLevelUuid;
|
|
|
|
|
|
|
|
namespace {
|
2021-04-18 17:28:14 +00:00
|
|
|
int AlertLevelCallback(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt* ctxt, void* arg) {
|
|
|
|
auto* immediateAlertService = static_cast<ImmediateAlertService*>(arg);
|
2020-09-27 18:59:06 +00:00
|
|
|
return immediateAlertService->OnAlertLevelChanged(conn_handle, attr_handle, ctxt);
|
|
|
|
}
|
|
|
|
|
|
|
|
const char* ToString(ImmediateAlertService::Levels level) {
|
|
|
|
switch (level) {
|
2021-04-18 17:28:14 +00:00
|
|
|
case ImmediateAlertService::Levels::NoAlert:
|
|
|
|
return "Alert : None";
|
|
|
|
case ImmediateAlertService::Levels::HighAlert:
|
|
|
|
return "Alert : High";
|
|
|
|
case ImmediateAlertService::Levels::MildAlert:
|
|
|
|
return "Alert : Mild";
|
|
|
|
default:
|
|
|
|
return "";
|
2020-09-27 18:59:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-18 17:28:14 +00:00
|
|
|
ImmediateAlertService::ImmediateAlertService(Pinetime::System::SystemTask& systemTask,
|
|
|
|
Pinetime::Controllers::NotificationManager& notificationManager)
|
|
|
|
: systemTask {systemTask},
|
|
|
|
notificationManager {notificationManager},
|
|
|
|
characteristicDefinition {{.uuid = (ble_uuid_t*) &alertLevelUuid,
|
|
|
|
.access_cb = AlertLevelCallback,
|
|
|
|
.arg = this,
|
|
|
|
.flags = BLE_GATT_CHR_F_WRITE_NO_RSP,
|
|
|
|
.val_handle = &alertLevelHandle},
|
|
|
|
{0}},
|
|
|
|
serviceDefinition {
|
|
|
|
{/* Device Information Service */
|
|
|
|
.type = BLE_GATT_SVC_TYPE_PRIMARY,
|
|
|
|
.uuid = (ble_uuid_t*) &immediateAlertServiceUuid,
|
|
|
|
.characteristics = characteristicDefinition},
|
|
|
|
{0},
|
|
|
|
} {
|
2020-09-27 18:59:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ImmediateAlertService::Init() {
|
|
|
|
int res = 0;
|
|
|
|
res = ble_gatts_count_cfg(serviceDefinition);
|
|
|
|
ASSERT(res == 0);
|
|
|
|
|
|
|
|
res = ble_gatts_add_svcs(serviceDefinition);
|
|
|
|
ASSERT(res == 0);
|
|
|
|
}
|
|
|
|
|
2021-04-18 17:28:14 +00:00
|
|
|
int ImmediateAlertService::OnAlertLevelChanged(uint16_t connectionHandle, uint16_t attributeHandle, ble_gatt_access_ctxt* context) {
|
|
|
|
if (attributeHandle == alertLevelHandle) {
|
|
|
|
if (context->op == BLE_GATT_ACCESS_OP_WRITE_CHR) {
|
2020-09-27 18:59:06 +00:00
|
|
|
auto alertLevel = static_cast<Levels>(context->om->om_data[0]);
|
|
|
|
auto* alertString = ToString(alertLevel);
|
2020-10-22 08:43:42 +00:00
|
|
|
|
|
|
|
NotificationManager::Notification notif;
|
|
|
|
std::memcpy(notif.message.data(), alertString, strlen(alertString));
|
|
|
|
notif.category = Pinetime::Controllers::NotificationManager::Categories::SimpleAlert;
|
|
|
|
notificationManager.Push(std::move(notif));
|
|
|
|
|
2021-06-06 13:56:03 +00:00
|
|
|
systemTask.PushMessage(Pinetime::System::Messages::OnNewNotification);
|
2020-09-27 18:59:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|