2020-04-25 13:09:47 +02:00
|
|
|
#include "AlertNotificationClient.h"
|
2020-11-15 15:05:51 +01:00
|
|
|
#include <algorithm>
|
|
|
|
#include "NotificationManager.h"
|
|
|
|
#include "systemtask/SystemTask.h"
|
2020-04-25 13:09:47 +02:00
|
|
|
|
|
|
|
using namespace Pinetime::Controllers;
|
|
|
|
constexpr ble_uuid16_t AlertNotificationClient::ansServiceUuid;
|
|
|
|
constexpr ble_uuid16_t AlertNotificationClient::supportedNewAlertCategoryUuid;
|
2020-10-29 16:06:01 +01:00
|
|
|
constexpr ble_uuid16_t AlertNotificationClient::supportedUnreadAlertCategoryUuid;
|
2020-04-25 13:09:47 +02:00
|
|
|
constexpr ble_uuid16_t AlertNotificationClient::newAlertUuid;
|
|
|
|
constexpr ble_uuid16_t AlertNotificationClient::unreadAlertStatusUuid;
|
|
|
|
constexpr ble_uuid16_t AlertNotificationClient::controlPointUuid;
|
|
|
|
|
2020-10-29 16:06:01 +01:00
|
|
|
namespace {
|
2021-04-18 19:28:14 +02:00
|
|
|
int OnDiscoveryEventCallback(uint16_t conn_handle, const struct ble_gatt_error* error, const struct ble_gatt_svc* service, void* arg) {
|
|
|
|
auto client = static_cast<AlertNotificationClient*>(arg);
|
2020-10-29 16:06:01 +01:00
|
|
|
return client->OnDiscoveryEvent(conn_handle, error, service);
|
|
|
|
}
|
|
|
|
|
2021-04-18 19:28:14 +02:00
|
|
|
int OnAlertNotificationCharacteristicDiscoveredCallback(uint16_t conn_handle,
|
|
|
|
const struct ble_gatt_error* error,
|
|
|
|
const struct ble_gatt_chr* chr,
|
|
|
|
void* arg) {
|
|
|
|
auto client = static_cast<AlertNotificationClient*>(arg);
|
2020-10-29 16:06:01 +01:00
|
|
|
return client->OnCharacteristicsDiscoveryEvent(conn_handle, error, chr);
|
|
|
|
}
|
2020-04-25 13:09:47 +02:00
|
|
|
|
2021-04-18 19:28:14 +02:00
|
|
|
int OnAlertNotificationDescriptorDiscoveryEventCallback(
|
|
|
|
uint16_t conn_handle, const struct ble_gatt_error* error, uint16_t chr_val_handle, const struct ble_gatt_dsc* dsc, void* arg) {
|
|
|
|
auto client = static_cast<AlertNotificationClient*>(arg);
|
2020-10-29 16:06:01 +01:00
|
|
|
return client->OnDescriptorDiscoveryEventCallback(conn_handle, error, chr_val_handle, dsc);
|
|
|
|
}
|
|
|
|
|
2021-04-18 19:28:14 +02:00
|
|
|
int NewAlertSubcribeCallback(uint16_t conn_handle, const struct ble_gatt_error* error, struct ble_gatt_attr* attr, void* arg) {
|
|
|
|
auto client = static_cast<AlertNotificationClient*>(arg);
|
2020-10-29 16:06:01 +01:00
|
|
|
return client->OnNewAlertSubcribe(conn_handle, error, attr);
|
|
|
|
}
|
|
|
|
}
|
2020-04-25 13:09:47 +02:00
|
|
|
|
2021-04-18 19:28:14 +02:00
|
|
|
AlertNotificationClient::AlertNotificationClient(Pinetime::System::SystemTask& systemTask,
|
|
|
|
Pinetime::Controllers::NotificationManager& notificationManager)
|
|
|
|
: systemTask {systemTask}, notificationManager {notificationManager} {
|
2020-04-25 13:09:47 +02:00
|
|
|
}
|
|
|
|
|
2021-04-18 19:28:14 +02:00
|
|
|
bool AlertNotificationClient::OnDiscoveryEvent(uint16_t connectionHandle, const ble_gatt_error* error, const ble_gatt_svc* service) {
|
2020-10-29 16:06:01 +01:00
|
|
|
if (service == nullptr && error->status == BLE_HS_EDONE) {
|
|
|
|
if (isDiscovered) {
|
|
|
|
NRF_LOG_INFO("ANS Discovery found, starting characteristics discovery");
|
|
|
|
|
2021-04-18 19:28:14 +02:00
|
|
|
ble_gattc_disc_all_chrs(connectionHandle, ansStartHandle, ansEndHandle, OnAlertNotificationCharacteristicDiscoveredCallback, this);
|
2020-10-29 16:06:01 +01:00
|
|
|
} else {
|
|
|
|
NRF_LOG_INFO("ANS not found");
|
|
|
|
onServiceDiscovered(connectionHandle);
|
|
|
|
}
|
2020-04-25 13:09:47 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-04-18 19:28:14 +02:00
|
|
|
if (service != nullptr && ble_uuid_cmp(((ble_uuid_t*) &ansServiceUuid), &service->uuid.u) == 0) {
|
2020-10-29 16:06:01 +01:00
|
|
|
NRF_LOG_INFO("ANS discovered : 0x%x - 0x%x", service->start_handle, service->end_handle);
|
|
|
|
ansStartHandle = service->start_handle;
|
|
|
|
ansEndHandle = service->end_handle;
|
|
|
|
isDiscovered = true;
|
2020-04-25 13:09:47 +02:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-04-18 19:28:14 +02:00
|
|
|
int AlertNotificationClient::OnCharacteristicsDiscoveryEvent(uint16_t connectionHandle,
|
|
|
|
const ble_gatt_error* error,
|
|
|
|
const ble_gatt_chr* characteristic) {
|
2020-10-29 16:06:01 +01:00
|
|
|
if (error->status != 0 && error->status != BLE_HS_EDONE) {
|
2020-04-25 13:09:47 +02:00
|
|
|
NRF_LOG_INFO("ANS Characteristic discovery ERROR");
|
2020-10-29 16:06:01 +01:00
|
|
|
onServiceDiscovered(connectionHandle);
|
2020-04-25 13:09:47 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-10-29 16:06:01 +01:00
|
|
|
if (characteristic == nullptr && error->status == BLE_HS_EDONE) {
|
2020-04-25 13:09:47 +02:00
|
|
|
NRF_LOG_INFO("ANS Characteristic discovery complete");
|
2020-10-29 16:06:01 +01:00
|
|
|
if (isCharacteristicDiscovered) {
|
2021-04-18 19:28:14 +02:00
|
|
|
ble_gattc_disc_all_dscs(connectionHandle, newAlertHandle, ansEndHandle, OnAlertNotificationDescriptorDiscoveryEventCallback, this);
|
2020-10-29 16:06:01 +01:00
|
|
|
} else
|
|
|
|
onServiceDiscovered(connectionHandle);
|
2020-04-25 13:09:47 +02:00
|
|
|
} else {
|
2021-04-18 19:28:14 +02:00
|
|
|
if (characteristic != nullptr && ble_uuid_cmp(((ble_uuid_t*) &supportedNewAlertCategoryUuid), &characteristic->uuid.u) == 0) {
|
2020-04-25 13:09:47 +02:00
|
|
|
NRF_LOG_INFO("ANS Characteristic discovered : supportedNewAlertCategoryUuid");
|
|
|
|
supportedNewAlertCategoryHandle = characteristic->val_handle;
|
2021-04-18 19:28:14 +02:00
|
|
|
} else if (characteristic != nullptr && ble_uuid_cmp(((ble_uuid_t*) &supportedUnreadAlertCategoryUuid), &characteristic->uuid.u) == 0) {
|
2020-04-25 13:09:47 +02:00
|
|
|
NRF_LOG_INFO("ANS Characteristic discovered : supportedUnreadAlertCategoryUuid");
|
|
|
|
supportedUnreadAlertCategoryHandle = characteristic->val_handle;
|
2021-04-18 19:28:14 +02:00
|
|
|
} else if (characteristic != nullptr && ble_uuid_cmp(((ble_uuid_t*) &newAlertUuid), &characteristic->uuid.u) == 0) {
|
2020-04-25 13:09:47 +02:00
|
|
|
NRF_LOG_INFO("ANS Characteristic discovered : newAlertUuid");
|
|
|
|
newAlertHandle = characteristic->val_handle;
|
|
|
|
newAlertDefHandle = characteristic->def_handle;
|
2020-10-29 16:06:01 +01:00
|
|
|
isCharacteristicDiscovered = true;
|
2021-04-18 19:28:14 +02:00
|
|
|
} else if (characteristic != nullptr && ble_uuid_cmp(((ble_uuid_t*) &unreadAlertStatusUuid), &characteristic->uuid.u) == 0) {
|
2020-04-25 13:09:47 +02:00
|
|
|
NRF_LOG_INFO("ANS Characteristic discovered : unreadAlertStatusUuid");
|
|
|
|
unreadAlertStatusHandle = characteristic->val_handle;
|
2021-04-18 19:28:14 +02:00
|
|
|
} else if (characteristic != nullptr && ble_uuid_cmp(((ble_uuid_t*) &controlPointUuid), &characteristic->uuid.u) == 0) {
|
2020-04-25 13:09:47 +02:00
|
|
|
NRF_LOG_INFO("ANS Characteristic discovered : controlPointUuid");
|
|
|
|
controlPointHandle = characteristic->val_handle;
|
2021-04-18 19:28:14 +02:00
|
|
|
} else
|
|
|
|
NRF_LOG_INFO("ANS Characteristic discovered : 0x%x", characteristic->val_handle);
|
2020-10-29 16:06:01 +01:00
|
|
|
}
|
2020-04-25 13:09:47 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-04-18 19:28:14 +02:00
|
|
|
int AlertNotificationClient::OnNewAlertSubcribe(uint16_t connectionHandle, const ble_gatt_error* error, ble_gatt_attr* attribute) {
|
2020-10-29 16:06:01 +01:00
|
|
|
if (error->status == 0) {
|
2020-04-25 13:09:47 +02:00
|
|
|
NRF_LOG_INFO("ANS New alert subscribe OK");
|
|
|
|
} else {
|
|
|
|
NRF_LOG_INFO("ANS New alert subscribe ERROR");
|
|
|
|
}
|
2020-10-29 16:06:01 +01:00
|
|
|
onServiceDiscovered(connectionHandle);
|
2020-04-25 13:09:47 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-04-18 19:28:14 +02:00
|
|
|
int AlertNotificationClient::OnDescriptorDiscoveryEventCallback(uint16_t connectionHandle,
|
|
|
|
const ble_gatt_error* error,
|
2020-04-25 13:09:47 +02:00
|
|
|
uint16_t characteristicValueHandle,
|
2021-04-18 19:28:14 +02:00
|
|
|
const ble_gatt_dsc* descriptor) {
|
2020-10-29 16:06:01 +01:00
|
|
|
if (error->status == 0) {
|
2021-04-18 19:28:14 +02:00
|
|
|
if (characteristicValueHandle == newAlertHandle && ble_uuid_cmp(((ble_uuid_t*) &newAlertUuid), &descriptor->uuid.u)) {
|
2020-10-29 16:06:01 +01:00
|
|
|
if (newAlertDescriptorHandle == 0) {
|
2020-04-25 13:09:47 +02:00
|
|
|
NRF_LOG_INFO("ANS Descriptor discovered : %d", descriptor->handle);
|
|
|
|
newAlertDescriptorHandle = descriptor->handle;
|
2020-10-29 16:06:01 +01:00
|
|
|
isDescriptorFound = true;
|
2020-04-25 13:09:47 +02:00
|
|
|
uint8_t value[2];
|
|
|
|
value[0] = 1;
|
|
|
|
value[1] = 0;
|
|
|
|
ble_gattc_write_flat(connectionHandle, newAlertDescriptorHandle, value, sizeof(value), NewAlertSubcribeCallback, this);
|
|
|
|
}
|
|
|
|
}
|
2020-10-29 16:06:01 +01:00
|
|
|
} else {
|
|
|
|
if (!isDescriptorFound)
|
|
|
|
onServiceDiscovered(connectionHandle);
|
2020-04-25 13:09:47 +02:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-04-18 19:28:14 +02:00
|
|
|
void AlertNotificationClient::OnNotification(ble_gap_event* event) {
|
2020-10-29 16:06:01 +01:00
|
|
|
if (event->notify_rx.attr_handle == newAlertHandle) {
|
2020-10-22 10:43:42 +02:00
|
|
|
constexpr size_t stringTerminatorSize = 1; // end of string '\0'
|
|
|
|
constexpr size_t headerSize = 3;
|
2021-04-18 19:28:14 +02:00
|
|
|
const auto maxMessageSize {NotificationManager::MaximumMessageSize()};
|
|
|
|
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(event->notify_rx.om);
|
2021-04-18 19:28:14 +02:00
|
|
|
if (packetLen <= headerSize)
|
|
|
|
return;
|
2021-04-04 15:19:37 +02:00
|
|
|
|
|
|
|
size_t bufferSize = std::min(packetLen + stringTerminatorSize, maxBufferSize);
|
2020-11-15 15:05:51 +01:00
|
|
|
auto messageSize = std::min(maxMessageSize, (bufferSize - headerSize));
|
2020-06-28 11:59:14 +02:00
|
|
|
|
2020-10-22 10:43:42 +02:00
|
|
|
NotificationManager::Notification notif;
|
2020-10-29 16:06:01 +01:00
|
|
|
os_mbuf_copydata(event->notify_rx.om, headerSize, messageSize - 1, notif.message.data());
|
|
|
|
notif.message[messageSize - 1] = '\0';
|
2021-04-04 12:10:47 +02:00
|
|
|
notif.size = messageSize;
|
2020-10-22 10:43:42 +02:00
|
|
|
notif.category = Pinetime::Controllers::NotificationManager::Categories::SimpleAlert;
|
|
|
|
notificationManager.Push(std::move(notif));
|
2020-06-28 11:59:14 +02:00
|
|
|
|
2021-06-06 15:56:03 +02:00
|
|
|
systemTask.PushMessage(Pinetime::System::Messages::OnNewNotification);
|
2020-04-25 13:09:47 +02:00
|
|
|
}
|
|
|
|
}
|
2020-04-25 15:52:00 +02:00
|
|
|
|
2020-10-28 18:23:09 +01:00
|
|
|
void AlertNotificationClient::Reset() {
|
|
|
|
ansStartHandle = 0;
|
|
|
|
ansEndHandle = 0;
|
|
|
|
supportedNewAlertCategoryHandle = 0;
|
|
|
|
supportedUnreadAlertCategoryHandle = 0;
|
|
|
|
newAlertHandle = 0;
|
|
|
|
newAlertDescriptorHandle = 0;
|
|
|
|
newAlertDefHandle = 0;
|
|
|
|
unreadAlertStatusHandle = 0;
|
|
|
|
controlPointHandle = 0;
|
|
|
|
isDiscovered = false;
|
2020-10-29 16:06:01 +01:00
|
|
|
isCharacteristicDiscovered = false;
|
|
|
|
isDescriptorFound = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void AlertNotificationClient::Discover(uint16_t connectionHandle, std::function<void(uint16_t)> onServiceDiscovered) {
|
|
|
|
NRF_LOG_INFO("[ANS] Starting discovery");
|
|
|
|
this->onServiceDiscovered = onServiceDiscovered;
|
|
|
|
ble_gattc_disc_svc_by_uuid(connectionHandle, &ansServiceUuid.u, OnDiscoveryEventCallback, this);
|
2020-10-28 18:23:09 +01:00
|
|
|
}
|