Author: JF <jf@codingfield.com>
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 +
diff --git a/CMakeLists.txt b/CMakeLists.txt index 5346ccfb19d74ebc951fbbe9672f0a931415f4a4..1e340f74352056e705bb4d55b391b409fe063789 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 ddc7296715876be6ccef974750d1c444264df739..8fbbafb62004bc71a1441b87ec8afe6327704451 100644 --- a/src/components/ble/AlertNotificationClient.cpp +++ b/src/components/ble/AlertNotificationClient.cpp @@ -105,25 +105,18 @@ } 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)); - - for (uint i = 0; i < messageSize-1; i++) { - if (s[i] == 0x00) { - s[i] = 0x0A; - } - } - s[messageSize-1] = '\0'; + message[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 9a9b535df0ba6aeceffbd09fa9abe34a617c44ef..7e432b829d41f5891d8f6e9b68861c0f6c0098ff 100644 --- a/src/components/ble/AlertNotificationService.cpp +++ b/src/components/ble/AlertNotificationService.cpp @@ -38,7 +38,7 @@ { 0 } }, - serviceDefinition{ + serviceDefinition{ { /* Device Information Service */ .type = BLE_GATT_SVC_TYPE_PRIMARY, @@ -48,33 +48,25 @@ }, { 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 53cb44cc59fb7bf1444d9737831112b90a2a3102..1b8c4989622853262066299429ff2d3633fd4ead 100644 --- a/src/components/ble/AlertNotificationService.h +++ b/src/components/ble/AlertNotificationService.h @@ -32,8 +32,8 @@ 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 4297cde9f6d76932240fe68d5b43ec3355a4d787..6ed3f8356ec73ca424742b5da5ab17eb2fd68ae5 100644 --- a/src/components/ble/NotificationManager.cpp +++ b/src/components/ble/NotificationManager.cpp @@ -4,10 +4,13 @@ #include "NotificationManager.h" 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 6bf689a874805423e8f2c7e621080b3b8f3c8ad0..e4e2b4d420b3fec752e726ec63d452b6b52d613b 100644 --- a/src/components/ble/NotificationManager.h +++ b/src/components/ble/NotificationManager.h @@ -8,7 +8,7 @@ namespace Controllers { 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 @@ Notification GetPrevious(Notification::Id id); bool ClearNewNotificationFlag(); bool AreNewNotificationsAvailable(); + static constexpr uint8_t MaximumMessageSize() { return MessageSize; }; private: Notification::Id GetNextId();