Skip to content
Draft
8 changes: 8 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,7 @@ list(APPEND SOURCE_FILES
displayapp/screens/Error.cpp
displayapp/screens/Alarm.cpp
displayapp/screens/Styles.cpp
displayapp/screens/Timeline.cpp
displayapp/Colors.cpp

## Settings
Expand Down Expand Up @@ -495,6 +496,8 @@ list(APPEND SOURCE_FILES
components/ble/ServiceDiscovery.cpp
components/ble/HeartRateService.cpp
components/ble/MotionService.cpp
components/ble/CalendarService.cpp
components/calendar/CalendarManager.cpp
components/firmwarevalidator/FirmwareValidator.cpp
components/motor/MotorController.cpp
components/settings/Settings.cpp
Expand Down Expand Up @@ -568,6 +571,8 @@ list(APPEND RECOVERY_SOURCE_FILES
components/ble/NavigationService.cpp
components/ble/HeartRateService.cpp
components/ble/MotionService.cpp
components/ble/CalendarService.cpp
components/calendar/CalendarManager.cpp
components/firmwarevalidator/FirmwareValidator.cpp
components/settings/Settings.cpp
components/timer/TimerController.cpp
Expand Down Expand Up @@ -647,6 +652,7 @@ set(INCLUDE_FILES
displayapp/screens/Motion.h
displayapp/screens/Timer.h
displayapp/screens/Alarm.h
displayapp/screens/Timeline.h
displayapp/Colors.h
drivers/St7789.h
drivers/SpiNorFlash.h
Expand Down Expand Up @@ -683,6 +689,8 @@ set(INCLUDE_FILES
components/ble/HeartRateService.h
components/ble/MotionService.h
components/ble/weather/WeatherService.h
components/ble/CalendarService.h
components/calendar/CalendarManager.h
components/settings/Settings.h
components/timer/TimerController.h
components/alarm/AlarmController.h
Expand Down
80 changes: 80 additions & 0 deletions src/components/ble/CalendarService.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
#include <nrf_log.h>
#include "CalendarService.h"

using namespace Pinetime::Controllers;

int CalCallback(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt* ctxt, void* arg) {
auto pCalendarEventService = static_cast<Pinetime::Controllers::CalendarService*>(arg);
return pCalendarEventService->OnCommand(conn_handle, attr_handle, ctxt);
}

uint32_t bytesToInt(const char* bytes) {
return (bytes[0] << 24) | (bytes[1] << 16) | (bytes[2] << 8) | bytes[3];
}

CalendarService::CalendarService(Pinetime::System::SystemTask& system, CalendarManager& calendarManager)
: m_system(system), m_calendarManager(calendarManager) {
calUuid.value[14] = calId[0];
calUuid.value[15] = calId[1];

calAddEventUuid.value[12] = calAddEventCharId[0];
calAddEventUuid.value[13] = calAddEventCharId[1];
calAddEventUuid.value[14] = calId[0];
calAddEventUuid.value[15] = calId[1];

calDeleteEventUuid.value[12] = calDeleteEventCharId[0];
calDeleteEventUuid.value[13] = calDeleteEventCharId[1];
calDeleteEventUuid.value[14] = calId[0];
calDeleteEventUuid.value[15] = calId[1];

characteristicDefinition[0] = {
.uuid = (ble_uuid_t*) (&calAddEventUuid),
.access_cb = CalCallback,
.arg = this,
.flags = BLE_GATT_CHR_F_WRITE,
};
characteristicDefinition[1] = {
.uuid = (ble_uuid_t*) (&calDeleteEventUuid),
.access_cb = CalCallback,
.arg = this,
.flags = BLE_GATT_CHR_F_WRITE,
};
characteristicDefinition[2] = {0};

serviceDefinition[0] = {
.type = BLE_GATT_SVC_TYPE_PRIMARY,
.uuid = (ble_uuid_t*) &calUuid,
.characteristics = characteristicDefinition,
};
serviceDefinition[1] = {0};
}
void CalendarService::Init() {
int res = 0;
res = ble_gatts_count_cfg(serviceDefinition);
ASSERT(res == 0);

res = ble_gatts_add_svcs(serviceDefinition);
ASSERT(res == 0);
}
int CalendarService::OnCommand(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt* ctxt) {
if (ctxt->op == BLE_GATT_ACCESS_OP_WRITE_CHR) {
size_t notifSize = OS_MBUF_PKTLEN(ctxt->om);
uint8_t data[notifSize + 1];
data[notifSize] = '\0';
os_mbuf_copydata(ctxt->om, 0, notifSize, data);
char* s = (char*) &data[0];
NRF_LOG_INFO("DATA : %s", s);
if (ble_uuid_cmp(ctxt->chr->uuid, (ble_uuid_t*) &calAddEventUuid) == 0) {
CalendarManager::CalendarEvent event {
.id = s[0],
.title = &s[9],
.timestamp = bytesToInt(&s[1]),
.duration = bytesToInt(&s[5]),
};
m_calendarManager.addEvent(event);
} else if (ble_uuid_cmp(ctxt->chr->uuid, (ble_uuid_t*) &calDeleteEventUuid) == 0) {
m_calendarManager.deleteEvent(s[0]);
}
}
return 0;
}
55 changes: 55 additions & 0 deletions src/components/ble/CalendarService.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#pragma once

// 00000000-78fc-48fe-8e23-433b3a1942d0
#include <host/ble_gap.h>
#include <host/ble_uuid.h>
#include <array>
#include "components/calendar/CalendarManager.h"

#define CALENDAR_SERVICE_UUID_BASE \
{ 0xd0, 0x42, 0x19, 0x3a, 0x3b, 0x43, 0x23, 0x8e, 0xfe, 0x48, 0xfc, 0x78, 0x00, 0x00, 0x04, 0x00 }

namespace Pinetime {
namespace System {
class SystemTask;
}
namespace Controllers {

class CalendarService {
public:
explicit CalendarService(Pinetime::System::SystemTask& system, CalendarManager& calendarManager);

void Init();

int OnCommand(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt* ctxt);

void event(char event);

static const char EVENT_CALENDAR_ADD = 0x01;
static const char EVENT_CALENDAR_DELETE = 0x02;

private:
static constexpr uint8_t calId[2] = {0x04, 0x00};
static constexpr uint8_t calAddEventCharId[2] = {0x01, 0x00};
static constexpr uint8_t calDeleteEventCharId[2] = {0x02, 0x00};
ble_uuid128_t calUuid {
.u = {.type = BLE_UUID_TYPE_128},
.value = CALENDAR_SERVICE_UUID_BASE,
};
ble_uuid128_t calAddEventUuid {
.u = {.type = BLE_UUID_TYPE_128},
.value = CALENDAR_SERVICE_UUID_BASE,
};
ble_uuid128_t calDeleteEventUuid {
.u = {.type = BLE_UUID_TYPE_128},
.value = CALENDAR_SERVICE_UUID_BASE,
};

struct ble_gatt_chr_def characteristicDefinition[3];
struct ble_gatt_svc_def serviceDefinition[2];

Pinetime::System::SystemTask& m_system;
CalendarManager& m_calendarManager;
};
}
};
4 changes: 4 additions & 0 deletions src/components/ble/NimbleController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#undef max
#undef min
#include "components/ble/BleController.h"
#include "components/calendar/CalendarManager.h"
#include "components/ble/NotificationManager.h"
#include "components/datetime/DateTimeController.h"
#include "components/fs/FS.h"
Expand All @@ -25,6 +26,7 @@ using namespace Pinetime::Controllers;

NimbleController::NimbleController(Pinetime::System::SystemTask& systemTask,
Ble& bleController,
CalendarManager& calendarManager,
DateTime& dateTimeController,
NotificationManager& notificationManager,
Battery& batteryController,
Expand All @@ -43,6 +45,7 @@ NimbleController::NimbleController(Pinetime::System::SystemTask& systemTask,
currentTimeClient {dateTimeController},
anService {systemTask, notificationManager},
alertNotificationClient {systemTask, notificationManager},
calendarService{systemTask, calendarManager},
currentTimeService {dateTimeController},
musicService {systemTask},
weatherService {systemTask, dateTimeController},
Expand Down Expand Up @@ -88,6 +91,7 @@ void NimbleController::Init() {
ble_svc_gatt_init();

deviceInformationService.Init();
calendarService.Init();
currentTimeClient.Init();
currentTimeService.Init();
musicService.Init();
Expand Down
3 changes: 3 additions & 0 deletions src/components/ble/NimbleController.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "components/ble/AlertNotificationClient.h"
#include "components/ble/AlertNotificationService.h"
#include "components/ble/BatteryInformationService.h"
#include "components/ble/CalendarService.h"
#include "components/ble/CurrentTimeClient.h"
#include "components/ble/CurrentTimeService.h"
#include "components/ble/DeviceInformationService.h"
Expand Down Expand Up @@ -43,6 +44,7 @@ namespace Pinetime {
public:
NimbleController(Pinetime::System::SystemTask& systemTask,
Ble& bleController,
CalendarManager& calendarManager,
DateTime& dateTimeController,
NotificationManager& notificationManager,
Battery& batteryController,
Expand Down Expand Up @@ -95,6 +97,7 @@ namespace Pinetime {
CurrentTimeClient currentTimeClient;
AlertNotificationService anService;
AlertNotificationClient alertNotificationClient;
CalendarService calendarService;
CurrentTimeService currentTimeService;
MusicService musicService;
WeatherService weatherService;
Expand Down
63 changes: 63 additions & 0 deletions src/components/calendar/CalendarManager.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#include "CalendarManager.h"
#include <algorithm>

using namespace Pinetime::Controllers;

/**
* Determines if the event `event1` should be placed before event `event2`
* @param event1 The event to be inserted
* @param event2 The comparison event
* @return
*/
bool CalendarManager::isBefore(CalendarEvent& event1, CalendarEvent& event2) {
return event1.timestamp < event2.timestamp;
}


/**
* Inserts a new event if enough space
* @param event The event to insert
* @return true if the operation succeeded (enough space), false otherwise
*/
bool CalendarManager::addEvent(CalendarEvent& event) {
auto it = calendarEvents.begin();

while (it != calendarEvents.end() && isBefore(*it, event)) {
++it;
}
calendarEvents.insert(it, event);
return true;
}

/**
* Removes the event corresponding to the given id if found
* @param id The id of the event to remove
* @return true if the event is found, false otherwise
*/
bool CalendarManager::deleteEvent(CalendarManager::CalendarEvent::Id id) {
auto matchId = [id](const CalendarEvent& event) { return event.id == id; };
auto it = std::find_if(calendarEvents.begin(), calendarEvents.end(), matchId);

if (it != calendarEvents.end()) {
calendarEvents.erase(it);
return true;
}

return false;
}

CalendarManager::CalendarEventIterator CalendarManager::begin() {
return calendarEvents.begin();
}

CalendarManager::CalendarEventIterator CalendarManager::end() {
return calendarEvents.end();
}

bool CalendarManager::empty() const {
return calendarEvents.empty();
}

unsigned int CalendarManager::getCount() const {
return calendarEvents.size();
}
37 changes: 37 additions & 0 deletions src/components/calendar/CalendarManager.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#pragma once

#include <array>
#include <atomic>
#include <cstddef>
#include <cstdint>
#include <string>
#include <list>

namespace Pinetime {
namespace Controllers {
class CalendarManager {
public:
struct CalendarEvent {
using Id = uint8_t;
Id id;
std::string title;
uint32_t timestamp;
uint32_t duration;
};
using CalendarEventIterator = std::list<CalendarEvent>::iterator;

private:
static bool isBefore(CalendarEvent& event1, CalendarEvent& event2);

std::list<CalendarEvent> calendarEvents;

public:
bool addEvent(CalendarEvent& event);
bool deleteEvent(CalendarEvent::Id id);
CalendarEventIterator begin();
CalendarEventIterator end();
bool empty() const;
unsigned int getCount() const;
};
}
}
1 change: 1 addition & 0 deletions src/displayapp/Apps.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ namespace Pinetime {
Metronome,
Motion,
Steps,
Timeline,
Weather,
PassKey,
QuickSettings,
Expand Down
7 changes: 7 additions & 0 deletions src/displayapp/DisplayApp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include "displayapp/screens/Notifications.h"
#include "displayapp/screens/SystemInfo.h"
#include "displayapp/screens/Tile.h"
#include "displayapp/screens/Timeline.h"
#include "displayapp/screens/Twos.h"
#include "displayapp/screens/FlashLight.h"
#include "displayapp/screens/BatteryInfo.h"
Expand Down Expand Up @@ -89,6 +90,7 @@ DisplayApp::DisplayApp(Drivers::St7789& lcd,
Drivers::Cst816S& touchPanel,
Controllers::Battery& batteryController,
Controllers::Ble& bleController,
Controllers::CalendarManager& calendarManager,
Controllers::DateTime& dateTimeController,
Drivers::WatchdogView& watchdog,
Pinetime::Controllers::NotificationManager& notificationManager,
Expand All @@ -105,6 +107,7 @@ DisplayApp::DisplayApp(Drivers::St7789& lcd,
touchPanel {touchPanel},
batteryController {batteryController},
bleController {bleController},
calendarManager {calendarManager},
dateTimeController {dateTimeController},
watchdog {watchdog},
notificationManager {notificationManager},
Expand Down Expand Up @@ -482,6 +485,10 @@ void DisplayApp::LoadApp(Apps app, DisplayApp::FullRefreshDirections direction)
case Apps::Steps:
currentScreen = std::make_unique<Screens::Steps>(this, motionController, settingsController);
break;

case Apps::Timeline:
currentScreen = std::make_unique<Screens::Timeline>(this, dateTimeController, calendarManager, lvgl);
break;
}
currentApp = app;
}
Expand Down
Loading