2021-10-13 16:08:35 -04:00
|
|
|
#include "components/ble/ImmediateAlertService.h"
|
2020-11-15 09:05:51 -05:00
|
|
|
#include <cstring>
|
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-09-27 14:59:06 -04:00
|
|
|
|
|
|
|
using namespace Pinetime::Controllers;
|
|
|
|
|
|
|
|
constexpr ble_uuid16_t ImmediateAlertService::immediateAlertServiceUuid;
|
|
|
|
constexpr ble_uuid16_t ImmediateAlertService::alertLevelUuid;
|
|
|
|
|
|
|
|
namespace {
|
2022-12-30 16:31:31 -05:00
|
|
|
int AlertLevelCallback(uint16_t /*conn_handle*/, uint16_t attr_handle, struct ble_gatt_access_ctxt* ctxt, void* arg) {
|
2021-04-18 13:28:14 -04:00
|
|
|
auto* immediateAlertService = static_cast<ImmediateAlertService*>(arg);
|
2022-12-30 16:31:31 -05:00
|
|
|
return immediateAlertService->OnAlertLevelChanged(attr_handle, ctxt);
|
2020-09-27 14:59:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
const char* ToString(ImmediateAlertService::Levels level) {
|
|
|
|
switch (level) {
|
2021-04-18 13:28:14 -04: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 14:59:06 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-18 13:28:14 -04:00
|
|
|
ImmediateAlertService::ImmediateAlertService(Pinetime::System::SystemTask& systemTask,
|
|
|
|
Pinetime::Controllers::NotificationManager& notificationManager)
|
|
|
|
: systemTask {systemTask},
|
|
|
|
notificationManager {notificationManager},
|
2021-07-24 11:40:06 -04:00
|
|
|
characteristicDefinition {{.uuid = &alertLevelUuid.u,
|
2021-04-18 13:28:14 -04:00
|
|
|
.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,
|
2021-07-24 11:40:06 -04:00
|
|
|
.uuid = &immediateAlertServiceUuid.u,
|
2021-04-18 13:28:14 -04:00
|
|
|
.characteristics = characteristicDefinition},
|
|
|
|
{0},
|
|
|
|
} {
|
2020-09-27 14:59:06 -04: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);
|
|
|
|
}
|
|
|
|
|
2022-12-30 16:31:31 -05:00
|
|
|
int ImmediateAlertService::OnAlertLevelChanged(uint16_t attributeHandle, ble_gatt_access_ctxt* context) {
|
2021-04-18 13:28:14 -04:00
|
|
|
if (attributeHandle == alertLevelHandle) {
|
|
|
|
if (context->op == BLE_GATT_ACCESS_OP_WRITE_CHR) {
|
2020-09-27 14:59:06 -04:00
|
|
|
auto alertLevel = static_cast<Levels>(context->om->om_data[0]);
|
|
|
|
auto* alertString = ToString(alertLevel);
|
2020-10-22 04:43:42 -04: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 09:56:03 -04:00
|
|
|
systemTask.PushMessage(Pinetime::System::Messages::OnNewNotification);
|
2020-09-27 14:59:06 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2021-07-24 11:40:06 -04:00
|
|
|
}
|