alertnotificationclient: Make use of extra char in message array for \0

This commit is contained in:
Finlay Davidson 2023-03-17 13:38:52 +01:00
parent 11ade64166
commit d1b16a7dcb

View file

@ -143,24 +143,22 @@ int AlertNotificationClient::OnDescriptorDiscoveryEventCallback(uint16_t connect
void AlertNotificationClient::OnNotification(ble_gap_event* event) { void AlertNotificationClient::OnNotification(ble_gap_event* event) {
if (event->notify_rx.attr_handle == newAlertHandle) { if (event->notify_rx.attr_handle == newAlertHandle) {
constexpr size_t stringTerminatorSize = 1; // end of string '\0' constexpr uint32_t headerSize = 3;
constexpr size_t headerSize = 3; constexpr uint32_t maxMessageSize = NotificationManager::MaximumMessageSize();
const auto maxMessageSize {NotificationManager::MaximumMessageSize()};
const auto maxBufferSize {maxMessageSize + headerSize};
// Ignore notifications with empty message // Ignore notifications with empty message
const auto packetLen = OS_MBUF_PKTLEN(event->notify_rx.om); const uint32_t packetLen = OS_MBUF_PKTLEN(event->notify_rx.om);
if (packetLen <= headerSize) if (packetLen <= headerSize) {
return; return;
}
size_t bufferSize = std::min(packetLen + stringTerminatorSize, maxBufferSize); const uint32_t messageSize = std::min(maxMessageSize, packetLen - headerSize);
auto messageSize = std::min(maxMessageSize, (bufferSize - headerSize));
NotificationManager::Notification notif; NotificationManager::Notification notif;
os_mbuf_copydata(event->notify_rx.om, headerSize, messageSize - 1, notif.message.data()); os_mbuf_copydata(event->notify_rx.om, headerSize, messageSize, notif.message.data());
notif.message[messageSize - 1] = '\0'; notif.message[messageSize] = '\0';
notif.size = messageSize; notif.size = messageSize;
notif.category = Pinetime::Controllers::NotificationManager::Categories::SimpleAlert; notif.category = NotificationManager::Categories::SimpleAlert;
notificationManager.Push(std::move(notif)); notificationManager.Push(std::move(notif));
systemTask.PushMessage(Pinetime::System::Messages::OnNewNotification); systemTask.PushMessage(Pinetime::System::Messages::OnNewNotification);