summaryrefslogtreecommitdiff
path: root/src/Components/Ble/AlertNotificationClient.cpp
diff options
context:
space:
mode:
authorJF002 <JF002@users.noreply.github.com>2020-05-16 16:15:38 +0200
committerGitHub <noreply@github.com>2020-05-16 16:15:38 +0200
commitd58f57b1b5a616debf893f209f1d96cac101489e (patch)
tree9df19606a2615586bb533d39fb42c84be8774092 /src/Components/Ble/AlertNotificationClient.cpp
parent24a1f87a78584d3b67f07ea7972ea0d8e1f8167c (diff)
parentd6c6ac4cf5801e17caf7bfc0878423703ed0413b (diff)
Merge pull request #30 from JF002/nimble
Nimble
Diffstat (limited to 'src/Components/Ble/AlertNotificationClient.cpp')
-rw-r--r--src/Components/Ble/AlertNotificationClient.cpp138
1 files changed, 138 insertions, 0 deletions
diff --git a/src/Components/Ble/AlertNotificationClient.cpp b/src/Components/Ble/AlertNotificationClient.cpp
new file mode 100644
index 00000000..6e096353
--- /dev/null
+++ b/src/Components/Ble/AlertNotificationClient.cpp
@@ -0,0 +1,138 @@
+#include <SystemTask/SystemTask.h>
+#include "NotificationManager.h"
+
+#include "AlertNotificationClient.h"
+
+
+using namespace Pinetime::Controllers;
+constexpr ble_uuid16_t AlertNotificationClient::ansServiceUuid;
+
+constexpr ble_uuid16_t AlertNotificationClient::supportedNewAlertCategoryUuid;
+constexpr ble_uuid16_t AlertNotificationClient::supportedUnreadAlertCategoryUuid ;
+constexpr ble_uuid16_t AlertNotificationClient::newAlertUuid;
+constexpr ble_uuid16_t AlertNotificationClient::unreadAlertStatusUuid;
+constexpr ble_uuid16_t AlertNotificationClient::controlPointUuid;
+
+int Pinetime::Controllers::NewAlertSubcribeCallback(uint16_t conn_handle,
+ const struct ble_gatt_error *error,
+ struct ble_gatt_attr *attr,
+ void *arg) {
+ auto client = static_cast<AlertNotificationClient*>(arg);
+ return client->OnNewAlertSubcribe(conn_handle, error, attr);
+}
+
+AlertNotificationClient::AlertNotificationClient(Pinetime::System::SystemTask& systemTask,
+ Pinetime::Controllers::NotificationManager& notificationManager) :
+ systemTask{systemTask}, notificationManager{notificationManager}{
+
+}
+
+bool AlertNotificationClient::OnDiscoveryEvent(uint16_t connectionHandle, const ble_gatt_error *error, const ble_gatt_svc *service) {
+ if(service == nullptr && error->status == BLE_HS_EDONE) {
+ NRF_LOG_INFO("ANS Discovery complete");
+ return true;
+ }
+
+ if(service != nullptr && ble_uuid_cmp(((ble_uuid_t*)&ansServiceUuid), &service->uuid.u) == 0) {
+ NRF_LOG_INFO("ANS discovered : 0x%x", service->start_handle);
+ ansStartHandle = service->start_handle;
+ ansEndHandle = service->end_handle;
+ isDiscovered = true;
+ }
+ return false;
+}
+
+void AlertNotificationClient::Init() {
+
+}
+
+int AlertNotificationClient::OnCharacteristicsDiscoveryEvent(uint16_t connectionHandle, const ble_gatt_error *error,
+ const ble_gatt_chr *characteristic) {
+ if(error->status != 0 && error->status != BLE_HS_EDONE) {
+ NRF_LOG_INFO("ANS Characteristic discovery ERROR");
+ return 0;
+ }
+
+ if(characteristic == nullptr && error->status == BLE_HS_EDONE) {
+ NRF_LOG_INFO("ANS Characteristic discovery complete");
+ } else {
+ if(characteristic != nullptr && ble_uuid_cmp(((ble_uuid_t*)&supportedNewAlertCategoryUuid), &characteristic->uuid.u) == 0) {
+ NRF_LOG_INFO("ANS Characteristic discovered : supportedNewAlertCategoryUuid");
+ supportedNewAlertCategoryHandle = characteristic->val_handle;
+ } else if(characteristic != nullptr && ble_uuid_cmp(((ble_uuid_t*)&supportedUnreadAlertCategoryUuid), &characteristic->uuid.u) == 0) {
+ NRF_LOG_INFO("ANS Characteristic discovered : supportedUnreadAlertCategoryUuid");
+ supportedUnreadAlertCategoryHandle = characteristic->val_handle;
+ } else if(characteristic != nullptr && ble_uuid_cmp(((ble_uuid_t*)&newAlertUuid), &characteristic->uuid.u) == 0) {
+ NRF_LOG_INFO("ANS Characteristic discovered : newAlertUuid");
+ newAlertHandle = characteristic->val_handle;
+ newAlertDefHandle = characteristic->def_handle;
+ } else if(characteristic != nullptr && ble_uuid_cmp(((ble_uuid_t*)&unreadAlertStatusUuid), &characteristic->uuid.u) == 0) {
+ NRF_LOG_INFO("ANS Characteristic discovered : unreadAlertStatusUuid");
+ unreadAlertStatusHandle = characteristic->val_handle;
+ } else if(characteristic != nullptr && ble_uuid_cmp(((ble_uuid_t*)&controlPointUuid), &characteristic->uuid.u) == 0) {
+ NRF_LOG_INFO("ANS Characteristic discovered : controlPointUuid");
+ controlPointHandle = characteristic->val_handle;
+ }else
+ NRF_LOG_INFO("ANS Characteristic discovered : 0x%x", characteristic->val_handle);
+ }
+ return 0;
+}
+
+int AlertNotificationClient::OnNewAlertSubcribe(uint16_t connectionHandle, const ble_gatt_error *error,
+ ble_gatt_attr *attribute) {
+ if(error->status == 0) {
+ NRF_LOG_INFO("ANS New alert subscribe OK");
+ } else {
+ NRF_LOG_INFO("ANS New alert subscribe ERROR");
+ }
+
+ return 0;
+}
+
+int AlertNotificationClient::OnDescriptorDiscoveryEventCallback(uint16_t connectionHandle, const ble_gatt_error *error,
+ uint16_t characteristicValueHandle,
+ const ble_gatt_dsc *descriptor) {
+ if(error->status == 0) {
+ if(characteristicValueHandle == newAlertHandle && ble_uuid_cmp(((ble_uuid_t*)&newAlertUuid), &descriptor->uuid.u)) {
+ if(newAlertDescriptorHandle == 0) {
+ NRF_LOG_INFO("ANS Descriptor discovered : %d", descriptor->handle);
+ newAlertDescriptorHandle = descriptor->handle;
+ uint8_t value[2];
+ value[0] = 1;
+ value[1] = 0;
+ ble_gattc_write_flat(connectionHandle, newAlertDescriptorHandle, value, sizeof(value), NewAlertSubcribeCallback, this);
+ }
+ }
+ }
+ return 0;
+}
+
+void AlertNotificationClient::OnNotification(ble_gap_event *event) {
+ if(event->notify_rx.attr_handle == newAlertHandle) {
+ size_t notifSize = OS_MBUF_PKTLEN(event->notify_rx.om);
+ uint8_t data[notifSize + 1];
+ data[notifSize] = '\0';
+ os_mbuf_copydata(event->notify_rx.om, 0, notifSize, data);
+ char *s = (char *) &data[2];
+ NRF_LOG_INFO("DATA : %s", s);
+
+ notificationManager.Push(Pinetime::Controllers::NotificationManager::Categories::SimpleAlert, s, notifSize + 1);
+ systemTask.PushMessage(Pinetime::System::SystemTask::Messages::OnNewNotification);
+ }
+}
+
+bool AlertNotificationClient::IsDiscovered() const {
+ return isDiscovered;
+}
+
+uint16_t AlertNotificationClient::StartHandle() const {
+ return ansStartHandle;
+}
+
+uint16_t AlertNotificationClient::EndHandle() const {
+ return ansEndHandle;
+}
+
+uint16_t AlertNotificationClient::NewAlerthandle() const {
+ return newAlertHandle;
+}