From ef5670c7e09a1a63fc5df4a066472ed7fe7550ff Mon Sep 17 00:00:00 2001 From: JF Date: Tue, 20 Oct 2020 20:57:39 +0200 Subject: Integrate new notification UI with notifications coming from BLE --- src/components/ble/NotificationManager.cpp | 68 ++++++++++++++++++++++++++---- src/components/ble/NotificationManager.h | 15 ++++++- 2 files changed, 73 insertions(+), 10 deletions(-) (limited to 'src/components') diff --git a/src/components/ble/NotificationManager.cpp b/src/components/ble/NotificationManager.cpp index 0aea0697..4297cde9 100644 --- a/src/components/ble/NotificationManager.cpp +++ b/src/components/ble/NotificationManager.cpp @@ -1,4 +1,5 @@ #include +#include #include "NotificationManager.h" using namespace Pinetime::Controllers; @@ -11,20 +12,69 @@ void NotificationManager::Push(Pinetime::Controllers::NotificationManager::Categ std::memcpy(notif.message.data(), message, checkedSize); notif.message[checkedSize] = '\0'; notif.category = category; + notif.id = GetNextId(); + notif.valid = true; writeIndex = (writeIndex + 1 < TotalNbNotifications) ? writeIndex + 1 : 0; - if(!empty && writeIndex == readIndex) - readIndex = writeIndex + 1; + if(!empty) + readIndex = (readIndex + 1 < TotalNbNotifications) ? readIndex + 1 : 0; + else empty = false; + + newNotification = true; } -NotificationManager::Notification Pinetime::Controllers::NotificationManager::Pop() { -// TODO handle edge cases on read/write index +NotificationManager::Notification NotificationManager::GetLastNotification() { NotificationManager::Notification notification = notifications[readIndex]; + return notification; +} - if(readIndex != writeIndex) { - readIndex = (readIndex + 1 < TotalNbNotifications) ? readIndex + 1 : 0; - } +NotificationManager::Notification::Id NotificationManager::GetNextId() { + return nextId++; +} - // TODO Check move optimization on return - return notification; +NotificationManager::Notification NotificationManager::GetNext(NotificationManager::Notification::Id id) { + auto currentIterator = std::find_if(notifications.begin(), notifications.end(), [id](const Notification& n){return n.valid && n.id == id;}); + if(currentIterator == notifications.end() || currentIterator->id != id) return Notification{}; + + auto& lastNotification = notifications[readIndex]; + + NotificationManager::Notification result; + + if(currentIterator == (notifications.end()-1)) + result = *(notifications.begin()); + else + result = *(currentIterator+1); + + if(result.id <= id) return {}; + + result.index = (lastNotification.id - result.id)+1; + return result; } + +NotificationManager::Notification NotificationManager::GetPrevious(NotificationManager::Notification::Id id) { + auto currentIterator = std::find_if(notifications.begin(), notifications.end(), [id](const Notification& n){return n.valid && n.id == id;}); + if(currentIterator == notifications.end() || currentIterator->id != id) return Notification{}; + + auto& lastNotification = notifications[readIndex]; + + NotificationManager::Notification result; + + if(currentIterator == notifications.begin()) + result = *(notifications.end()-1); + else + result = *(currentIterator-1); + + if(result.id >= id) return {}; + + result.index = (lastNotification.id - result.id)+1; + return result; +} + +bool NotificationManager::AreNewNotificationsAvailable() { + return newNotification; +} + +bool NotificationManager::ClearNewNotificationFlag() { + return newNotification.exchange(false); +} + diff --git a/src/components/ble/NotificationManager.h b/src/components/ble/NotificationManager.h index daa1571b..6bf689a8 100644 --- a/src/components/ble/NotificationManager.h +++ b/src/components/ble/NotificationManager.h @@ -1,6 +1,7 @@ #pragma once #include +#include namespace Pinetime { namespace Controllers { @@ -10,20 +11,32 @@ namespace Pinetime { static constexpr uint8_t MessageSize{18}; struct Notification { + using Id = uint8_t; + Id id; + bool valid = false; + uint8_t index; + uint8_t number = TotalNbNotifications; std::array message; Categories category = Categories::Unknown; }; + Notification::Id nextId {0}; void Push(Categories category, const char* message, uint8_t messageSize); - Notification Pop(); + Notification GetLastNotification(); + Notification GetNext(Notification::Id id); + Notification GetPrevious(Notification::Id id); + bool ClearNewNotificationFlag(); + bool AreNewNotificationsAvailable(); private: + Notification::Id GetNextId(); static constexpr uint8_t TotalNbNotifications = 5; std::array notifications; uint8_t readIndex = 0; uint8_t writeIndex = 0; bool empty = true; + std::atomic newNotification{false}; }; } } \ No newline at end of file -- cgit v1.2.3 From 440ae412b9ce9c868aa8b98e6da537bd0ec62de7 Mon Sep 17 00:00:00 2001 From: JF Date: Wed, 21 Oct 2020 17:31:56 +0200 Subject: Increase max size of notification message to 100 char. Fix bug in message handling that would ignore the last character of the notification. --- CMakeLists.txt | 2 +- src/components/ble/AlertNotificationClient.cpp | 25 +++++++------------ src/components/ble/AlertNotificationService.cpp | 32 ++++++++++--------------- src/components/ble/AlertNotificationService.h | 4 ++-- src/components/ble/NotificationManager.cpp | 5 +++- src/components/ble/NotificationManager.h | 3 ++- 6 files changed, 30 insertions(+), 41 deletions(-) (limited to 'src/components') diff --git a/CMakeLists.txt b/CMakeLists.txt index 5346ccfb..1e340f74 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,5 +1,5 @@ cmake_minimum_required(VERSION 3.10) -project(pinetime VERSION 0.8.2 LANGUAGES C CXX ASM) +project(pinetime VERSION 0.9.0 LANGUAGES C CXX ASM) set(NRF_TARGET "nrf52") diff --git a/src/components/ble/AlertNotificationClient.cpp b/src/components/ble/AlertNotificationClient.cpp index ddc72967..8fbbafb6 100644 --- a/src/components/ble/AlertNotificationClient.cpp +++ b/src/components/ble/AlertNotificationClient.cpp @@ -105,25 +105,18 @@ int AlertNotificationClient::OnDescriptorDiscoveryEventCallback(uint16_t connect void AlertNotificationClient::OnNotification(ble_gap_event *event) { if(event->notify_rx.attr_handle == newAlertHandle) { - // TODO implement this with more memory safety (and constexpr) - static const size_t maxBufferSize{21}; - static const size_t maxMessageSize{18}; - size_t bufferSize = min(OS_MBUF_PKTLEN(event->notify_rx.om), maxBufferSize); + static constexpr size_t stringTerminatorSize{1}; // end of string '\0' + static constexpr size_t headerSize{3}; + const auto maxMessageSize {NotificationManager::MaximumMessageSize()}; + const auto maxBufferSize{maxMessageSize + headerSize}; - uint8_t data[bufferSize]; - os_mbuf_copydata(event->notify_rx.om, 0, bufferSize, data); + size_t bufferSize = min(OS_MBUF_PKTLEN(event->notify_rx.om) + stringTerminatorSize, maxBufferSize); + char *message = (char *)(&event->notify_rx.om[headerSize]); + auto messageSize = min(maxMessageSize, (bufferSize-headerSize)); - char *s = (char *) &data[3]; - auto messageSize = min(maxMessageSize, (bufferSize-3)); + message[messageSize-1] = '\0'; - for (uint i = 0; i < messageSize-1; i++) { - if (s[i] == 0x00) { - s[i] = 0x0A; - } - } - s[messageSize-1] = '\0'; - - notificationManager.Push(Pinetime::Controllers::NotificationManager::Categories::SimpleAlert, s, messageSize); + notificationManager.Push(Pinetime::Controllers::NotificationManager::Categories::SimpleAlert, message, messageSize); systemTask.PushMessage(Pinetime::System::SystemTask::Messages::OnNewNotification); } } diff --git a/src/components/ble/AlertNotificationService.cpp b/src/components/ble/AlertNotificationService.cpp index 9a9b535d..7e432b82 100644 --- a/src/components/ble/AlertNotificationService.cpp +++ b/src/components/ble/AlertNotificationService.cpp @@ -38,7 +38,7 @@ AlertNotificationService::AlertNotificationService ( System::SystemTask& systemT 0 } }, - serviceDefinition{ + serviceDefinition{ { /* Device Information Service */ .type = BLE_GATT_SVC_TYPE_PRIMARY, @@ -48,33 +48,25 @@ AlertNotificationService::AlertNotificationService ( System::SystemTask& systemT { 0 }, - }, m_systemTask{systemTask}, m_notificationManager{notificationManager} { + }, systemTask{systemTask}, notificationManager{notificationManager} { } 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) { - // TODO implement this with more memory safety (and constexpr) - static const size_t maxBufferSize{21}; - static const size_t maxMessageSize{18}; - size_t bufferSize = min(OS_MBUF_PKTLEN(ctxt->om), maxBufferSize); - - uint8_t data[bufferSize]; - os_mbuf_copydata(ctxt->om, 0, bufferSize, data); + static constexpr size_t stringTerminatorSize{1}; // end of string '\0' + static constexpr size_t headerSize{3}; + const auto maxMessageSize {NotificationManager::MaximumMessageSize()}; + const auto maxBufferSize{maxMessageSize + headerSize}; - char *s = (char *) &data[3]; - auto messageSize = min(maxMessageSize, (bufferSize-3)); + size_t bufferSize = min(OS_MBUF_PKTLEN(ctxt->om) + stringTerminatorSize, maxBufferSize); + char *message = (char *)(&ctxt->om->om_data[headerSize]); + auto messageSize = min(maxMessageSize, (bufferSize-headerSize)); - for (uint i = 0; i < messageSize-1; i++) { - if (s[i] == 0x00) { - s[i] = 0x0A; - } - } - s[messageSize-1] = '\0'; + message[messageSize-1] = '\0'; - m_notificationManager.Push(Pinetime::Controllers::NotificationManager::Categories::SimpleAlert, s, messageSize); - m_systemTask.PushMessage(Pinetime::System::SystemTask::Messages::OnNewNotification); + notificationManager.Push(Pinetime::Controllers::NotificationManager::Categories::SimpleAlert, message, messageSize); + systemTask.PushMessage(Pinetime::System::SystemTask::Messages::OnNewNotification); } return 0; } diff --git a/src/components/ble/AlertNotificationService.h b/src/components/ble/AlertNotificationService.h index 53cb44cc..1b8c4989 100644 --- a/src/components/ble/AlertNotificationService.h +++ b/src/components/ble/AlertNotificationService.h @@ -32,8 +32,8 @@ namespace Pinetime { struct ble_gatt_chr_def characteristicDefinition[2]; struct ble_gatt_svc_def serviceDefinition[2]; - Pinetime::System::SystemTask &m_systemTask; - NotificationManager &m_notificationManager; + Pinetime::System::SystemTask &systemTask; + NotificationManager ¬ificationManager; }; } } diff --git a/src/components/ble/NotificationManager.cpp b/src/components/ble/NotificationManager.cpp index 4297cde9..6ed3f835 100644 --- a/src/components/ble/NotificationManager.cpp +++ b/src/components/ble/NotificationManager.cpp @@ -4,10 +4,13 @@ using namespace Pinetime::Controllers; +constexpr uint8_t NotificationManager::MessageSize; + + void NotificationManager::Push(Pinetime::Controllers::NotificationManager::Categories category, const char *message, uint8_t currentMessageSize) { // TODO handle edge cases on read/write index - auto checkedSize = std::min(currentMessageSize, uint8_t{18}); + auto checkedSize = std::min(currentMessageSize, MessageSize); auto& notif = notifications[writeIndex]; std::memcpy(notif.message.data(), message, checkedSize); notif.message[checkedSize] = '\0'; diff --git a/src/components/ble/NotificationManager.h b/src/components/ble/NotificationManager.h index 6bf689a8..e4e2b4d4 100644 --- a/src/components/ble/NotificationManager.h +++ b/src/components/ble/NotificationManager.h @@ -8,7 +8,7 @@ namespace Pinetime { class NotificationManager { public: enum class Categories {Unknown, SimpleAlert, Email, News, IncomingCall, MissedCall, Sms, VoiceMail, Schedule, HighProriotyAlert, InstantMessage }; - static constexpr uint8_t MessageSize{18}; + static constexpr uint8_t MessageSize{100}; struct Notification { using Id = uint8_t; @@ -28,6 +28,7 @@ namespace Pinetime { bool ClearNewNotificationFlag(); bool AreNewNotificationsAvailable(); + static constexpr uint8_t MaximumMessageSize() { return MessageSize; }; private: Notification::Id GetNextId(); -- cgit v1.2.3 From cabf1168d429db84b0985e25881ead86d80dde3c Mon Sep 17 00:00:00 2001 From: JF Date: Wed, 21 Oct 2020 22:15:02 +0200 Subject: Notifications : Fix display of notification index/number. --- src/components/ble/NotificationManager.cpp | 5 +++++ src/components/ble/NotificationManager.h | 2 +- src/displayapp/screens/Notifications.cpp | 8 ++++---- 3 files changed, 10 insertions(+), 5 deletions(-) (limited to 'src/components') diff --git a/src/components/ble/NotificationManager.cpp b/src/components/ble/NotificationManager.cpp index 6ed3f835..11555bee 100644 --- a/src/components/ble/NotificationManager.cpp +++ b/src/components/ble/NotificationManager.cpp @@ -28,6 +28,7 @@ void NotificationManager::Push(Pinetime::Controllers::NotificationManager::Categ NotificationManager::Notification NotificationManager::GetLastNotification() { NotificationManager::Notification notification = notifications[readIndex]; + notification.index = 1; return notification; } @@ -81,3 +82,7 @@ bool NotificationManager::ClearNewNotificationFlag() { return newNotification.exchange(false); } +size_t NotificationManager::NbNotifications() const { + return std::count_if(notifications.begin(), notifications.end(), [](const Notification& n){ return n.valid;}); +} + diff --git a/src/components/ble/NotificationManager.h b/src/components/ble/NotificationManager.h index e4e2b4d4..64c6df6d 100644 --- a/src/components/ble/NotificationManager.h +++ b/src/components/ble/NotificationManager.h @@ -15,7 +15,6 @@ namespace Pinetime { Id id; bool valid = false; uint8_t index; - uint8_t number = TotalNbNotifications; std::array message; Categories category = Categories::Unknown; }; @@ -29,6 +28,7 @@ namespace Pinetime { bool AreNewNotificationsAvailable(); static constexpr uint8_t MaximumMessageSize() { return MessageSize; }; + size_t NbNotifications() const; private: Notification::Id GetNextId(); diff --git a/src/displayapp/screens/Notifications.cpp b/src/displayapp/screens/Notifications.cpp index 09365f2e..38a28e12 100644 --- a/src/displayapp/screens/Notifications.cpp +++ b/src/displayapp/screens/Notifications.cpp @@ -11,10 +11,10 @@ Notifications::Notifications(DisplayApp *app, Pinetime::Controllers::Notificatio auto notification = notificationManager.GetLastNotification(); if(notification.valid) { currentId = notification.id; - currentItem.reset(new NotificationItem("Notification", notification.message.data(), notification.index, notification.number, mode)); + currentItem.reset(new NotificationItem("Notification", notification.message.data(), notification.index, notificationManager.NbNotifications(), mode)); validDisplay = true; } else { - currentItem.reset(new NotificationItem("Notification", "No notification to display", 0, 0, mode)); + currentItem.reset(new NotificationItem("Notification", "No notification to display", 0, notificationManager.NbNotifications(), Modes::Preview)); } if(mode == Modes::Preview) { @@ -71,7 +71,7 @@ bool Notifications::OnTouchEvent(Pinetime::Applications::TouchEvents event) { currentId = previousNotification.id; currentItem.reset(nullptr); app->SetFullRefresh(DisplayApp::FullRefreshDirections::Up); - currentItem.reset(new NotificationItem("Notification", previousNotification.message.data(), previousNotification.index, previousNotification.number, mode)); + currentItem.reset(new NotificationItem("Notification", previousNotification.message.data(), previousNotification.index, notificationManager.NbNotifications(), mode)); } return true; case Pinetime::Applications::TouchEvents::SwipeDown: { @@ -87,7 +87,7 @@ bool Notifications::OnTouchEvent(Pinetime::Applications::TouchEvents event) { currentId = nextNotification.id; currentItem.reset(nullptr); app->SetFullRefresh(DisplayApp::FullRefreshDirections::Down); - currentItem.reset(new NotificationItem("Notification", nextNotification.message.data(), nextNotification.index, nextNotification.number, mode)); + currentItem.reset(new NotificationItem("Notification", nextNotification.message.data(), nextNotification.index, notificationManager.NbNotifications(), mode)); } return true; default: -- cgit v1.2.3 From 07b6812f61cf5b7726fbf6015c2c60caa12c7f20 Mon Sep 17 00:00:00 2001 From: JF Date: Thu, 22 Oct 2020 10:43:42 +0200 Subject: Notifications : Fix copy when the messages is spread across multiple os_mbuf. --- src/components/ble/AlertNotificationClient.cpp | 15 +++++++++------ src/components/ble/AlertNotificationService.cpp | 15 +++++++++------ src/components/ble/ImmediateAlertService.cpp | 8 +++++++- src/components/ble/NotificationManager.cpp | 11 ++--------- src/components/ble/NotificationManager.h | 2 +- 5 files changed, 28 insertions(+), 23 deletions(-) (limited to 'src/components') diff --git a/src/components/ble/AlertNotificationClient.cpp b/src/components/ble/AlertNotificationClient.cpp index 8fbbafb6..40970e0b 100644 --- a/src/components/ble/AlertNotificationClient.cpp +++ b/src/components/ble/AlertNotificationClient.cpp @@ -105,18 +105,21 @@ int AlertNotificationClient::OnDescriptorDiscoveryEventCallback(uint16_t connect void AlertNotificationClient::OnNotification(ble_gap_event *event) { if(event->notify_rx.attr_handle == newAlertHandle) { - static constexpr size_t stringTerminatorSize{1}; // end of string '\0' - static constexpr size_t headerSize{3}; + constexpr size_t stringTerminatorSize = 1; // end of string '\0' + constexpr size_t headerSize = 3; const auto maxMessageSize {NotificationManager::MaximumMessageSize()}; const auto maxBufferSize{maxMessageSize + headerSize}; - size_t bufferSize = min(OS_MBUF_PKTLEN(event->notify_rx.om) + stringTerminatorSize, maxBufferSize); - char *message = (char *)(&event->notify_rx.om[headerSize]); + const auto dbgPacketLen = OS_MBUF_PKTLEN(event->notify_rx.om); + size_t bufferSize = min(dbgPacketLen + stringTerminatorSize, maxBufferSize); auto messageSize = min(maxMessageSize, (bufferSize-headerSize)); - message[messageSize-1] = '\0'; + NotificationManager::Notification notif; + os_mbuf_copydata(event->notify_rx.om, headerSize, messageSize-1, notif.message.data()); + notif.message[messageSize-1] = '\0'; + notif.category = Pinetime::Controllers::NotificationManager::Categories::SimpleAlert; + notificationManager.Push(std::move(notif)); - notificationManager.Push(Pinetime::Controllers::NotificationManager::Categories::SimpleAlert, message, messageSize); systemTask.PushMessage(Pinetime::System::SystemTask::Messages::OnNewNotification); } } diff --git a/src/components/ble/AlertNotificationService.cpp b/src/components/ble/AlertNotificationService.cpp index 7e432b82..32711b92 100644 --- a/src/components/ble/AlertNotificationService.cpp +++ b/src/components/ble/AlertNotificationService.cpp @@ -54,18 +54,21 @@ AlertNotificationService::AlertNotificationService ( System::SystemTask& systemT 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) { - static constexpr size_t stringTerminatorSize{1}; // end of string '\0' - static constexpr size_t headerSize{3}; + constexpr size_t stringTerminatorSize = 1; // end of string '\0' + constexpr size_t headerSize = 3; const auto maxMessageSize {NotificationManager::MaximumMessageSize()}; const auto maxBufferSize{maxMessageSize + headerSize}; - size_t bufferSize = min(OS_MBUF_PKTLEN(ctxt->om) + stringTerminatorSize, maxBufferSize); - char *message = (char *)(&ctxt->om->om_data[headerSize]); + const auto dbgPacketLen = OS_MBUF_PKTLEN(ctxt->om); + size_t bufferSize = min(dbgPacketLen + stringTerminatorSize, maxBufferSize); auto messageSize = min(maxMessageSize, (bufferSize-headerSize)); - message[messageSize-1] = '\0'; + NotificationManager::Notification notif; + os_mbuf_copydata(ctxt->om, headerSize, messageSize-1, notif.message.data()); + notif.message[messageSize-1] = '\0'; + notif.category = Pinetime::Controllers::NotificationManager::Categories::SimpleAlert; + notificationManager.Push(std::move(notif)); - notificationManager.Push(Pinetime::Controllers::NotificationManager::Categories::SimpleAlert, message, messageSize); systemTask.PushMessage(Pinetime::System::SystemTask::Messages::OnNewNotification); } return 0; diff --git a/src/components/ble/ImmediateAlertService.cpp b/src/components/ble/ImmediateAlertService.cpp index 3b7f47bf..e2cee308 100644 --- a/src/components/ble/ImmediateAlertService.cpp +++ b/src/components/ble/ImmediateAlertService.cpp @@ -1,4 +1,5 @@ #include +#include #include "ImmediateAlertService.h" #include "AlertNotificationService.h" @@ -67,7 +68,12 @@ int ImmediateAlertService::OnAlertLevelChanged(uint16_t connectionHandle, uint16 if(context->op == BLE_GATT_ACCESS_OP_WRITE_CHR) { auto alertLevel = static_cast(context->om->om_data[0]); auto* alertString = ToString(alertLevel); - notificationManager.Push(Pinetime::Controllers::NotificationManager::Categories::SimpleAlert, alertString, strlen(alertString)); + + NotificationManager::Notification notif; + std::memcpy(notif.message.data(), alertString, strlen(alertString)); + notif.category = Pinetime::Controllers::NotificationManager::Categories::SimpleAlert; + notificationManager.Push(std::move(notif)); + systemTask.PushMessage(Pinetime::System::SystemTask::Messages::OnNewNotification); } } diff --git a/src/components/ble/NotificationManager.cpp b/src/components/ble/NotificationManager.cpp index 11555bee..67711723 100644 --- a/src/components/ble/NotificationManager.cpp +++ b/src/components/ble/NotificationManager.cpp @@ -7,17 +7,10 @@ using namespace Pinetime::Controllers; constexpr uint8_t NotificationManager::MessageSize; -void NotificationManager::Push(Pinetime::Controllers::NotificationManager::Categories category, - const char *message, uint8_t currentMessageSize) { - // TODO handle edge cases on read/write index - auto checkedSize = std::min(currentMessageSize, MessageSize); - auto& notif = notifications[writeIndex]; - std::memcpy(notif.message.data(), message, checkedSize); - notif.message[checkedSize] = '\0'; - notif.category = category; +void NotificationManager::Push(NotificationManager::Notification &¬if) { notif.id = GetNextId(); notif.valid = true; - + notifications[writeIndex] = std::move(notif); writeIndex = (writeIndex + 1 < TotalNbNotifications) ? writeIndex + 1 : 0; if(!empty) readIndex = (readIndex + 1 < TotalNbNotifications) ? readIndex + 1 : 0; diff --git a/src/components/ble/NotificationManager.h b/src/components/ble/NotificationManager.h index 64c6df6d..49fe8306 100644 --- a/src/components/ble/NotificationManager.h +++ b/src/components/ble/NotificationManager.h @@ -20,7 +20,7 @@ namespace Pinetime { }; Notification::Id nextId {0}; - void Push(Categories category, const char* message, uint8_t messageSize); + void Push(Notification&& notif); Notification GetLastNotification(); Notification GetNext(Notification::Id id); Notification GetPrevious(Notification::Id id); -- cgit v1.2.3