Encapsulate Notification management in NotificationManager. It implement a static array of notifications to avoid dynamic allocation.
This commit is contained in:
@@ -4,10 +4,6 @@
|
||||
|
||||
using namespace Pinetime::Controllers;
|
||||
|
||||
Ble::Ble() {
|
||||
notificationQueue = xQueueCreate(10, sizeof(NotificationMessage));
|
||||
}
|
||||
|
||||
void Ble::Connect() {
|
||||
isConnected = true;
|
||||
}
|
||||
@@ -16,24 +12,4 @@ void Ble::Disconnect() {
|
||||
isConnected = false;
|
||||
}
|
||||
|
||||
void Ble::PushNotification(const char *message, uint8_t size) {
|
||||
char* messageCopy = static_cast<char *>(malloc(sizeof(char) * size));
|
||||
std::memcpy(messageCopy, message, size);
|
||||
NotificationMessage msg;
|
||||
msg.size = size;
|
||||
msg.message = messageCopy;
|
||||
|
||||
BaseType_t xHigherPriorityTaskWoken;
|
||||
xHigherPriorityTaskWoken = pdFALSE;
|
||||
xQueueSendFromISR(notificationQueue, &msg, &xHigherPriorityTaskWoken);
|
||||
if (xHigherPriorityTaskWoken) {
|
||||
/* Actual macro used here is port specific. */
|
||||
// TODO : should I do something here?
|
||||
}
|
||||
}
|
||||
|
||||
bool Ble::PopNotification(Ble::NotificationMessage& msg) {
|
||||
return xQueueReceive(notificationQueue, &msg, 0) != 0;
|
||||
}
|
||||
|
||||
|
||||
|
@@ -7,22 +7,13 @@ namespace Pinetime {
|
||||
namespace Controllers {
|
||||
class Ble {
|
||||
public:
|
||||
struct NotificationMessage {
|
||||
uint8_t size = 0;
|
||||
const char* message = nullptr;
|
||||
};
|
||||
|
||||
Ble();
|
||||
Ble() = default;
|
||||
bool IsConnected() const {return isConnected;}
|
||||
void Connect();
|
||||
void Disconnect();
|
||||
|
||||
void PushNotification(const char* message, uint8_t size);
|
||||
bool PopNotification(NotificationMessage& msg);
|
||||
|
||||
private:
|
||||
bool isConnected = false;
|
||||
QueueHandle_t notificationQueue;
|
||||
|
||||
};
|
||||
}
|
||||
|
29
src/Components/Ble/NotificationManager.cpp
Normal file
29
src/Components/Ble/NotificationManager.cpp
Normal file
@@ -0,0 +1,29 @@
|
||||
#include <cstring>
|
||||
#include "NotificationManager.h"
|
||||
|
||||
using namespace Pinetime::Controllers;
|
||||
|
||||
void NotificationManager::Push(Pinetime::Controllers::NotificationManager::Categories category,
|
||||
const char *message, uint8_t messageSize) {
|
||||
// TODO handle edge cases on read/write index
|
||||
auto& notif = notifications[writeIndex];
|
||||
std::memcpy(notif.message.data(), message, messageSize);
|
||||
notif.message[messageSize] = '\0';
|
||||
notif.category = category;
|
||||
|
||||
writeIndex = (writeIndex + 1 < TotalNbNotifications) ? writeIndex + 1 : 0;
|
||||
if(!empty && writeIndex == readIndex)
|
||||
readIndex = writeIndex + 1;
|
||||
}
|
||||
|
||||
NotificationManager::Notification Pinetime::Controllers::NotificationManager::Pop() {
|
||||
// TODO handle edge cases on read/write index
|
||||
NotificationManager::Notification notification = notifications[readIndex];
|
||||
|
||||
if(readIndex != writeIndex) {
|
||||
readIndex = (readIndex + 1 < TotalNbNotifications) ? readIndex + 1 : 0;
|
||||
}
|
||||
|
||||
// TODO Check move optimization on return
|
||||
return notification;
|
||||
}
|
29
src/Components/Ble/NotificationManager.h
Normal file
29
src/Components/Ble/NotificationManager.h
Normal file
@@ -0,0 +1,29 @@
|
||||
#pragma once
|
||||
|
||||
#include <array>
|
||||
|
||||
namespace Pinetime {
|
||||
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;
|
||||
|
||||
struct Notification {
|
||||
std::array<char, MessageSize> message;
|
||||
Categories category = Categories::Unknown;
|
||||
};
|
||||
|
||||
void Push(Categories category, const char* message, uint8_t messageSize);
|
||||
Notification Pop();
|
||||
|
||||
|
||||
private:
|
||||
static constexpr uint8_t TotalNbNotifications = 5;
|
||||
std::array<Notification, TotalNbNotifications> notifications;
|
||||
uint8_t readIndex = 0;
|
||||
uint8_t writeIndex = 0;
|
||||
bool empty = true;
|
||||
};
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user