|
| 1 | +/* Copyright (C) 2024 |
| 2 | +
|
| 3 | + This file is part of InfiniTime. |
| 4 | +
|
| 5 | + InfiniTime is free software: you can redistribute it and/or modify |
| 6 | + it under the terms of the GNU General Public License as published |
| 7 | + by the Free Software Foundation, either version 3 of the License, or |
| 8 | + (at your option) any later version. |
| 9 | +
|
| 10 | + InfiniTime is distributed in the hope that it will be useful, |
| 11 | + but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | + GNU General Public License for more details. |
| 14 | +
|
| 15 | + You should have received a copy of the GNU General Public License |
| 16 | + along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 17 | +*/ |
| 18 | +#include "components/ble/ReminderService.h" |
| 19 | +#include "systemtask/SystemTask.h" |
| 20 | +#include <cstring> |
| 21 | +#include <nrf_log.h> |
| 22 | + |
| 23 | +using namespace Pinetime::Controllers; |
| 24 | + |
| 25 | +int ReminderCallback(uint16_t /*connHandle*/, uint16_t /*attrHandle*/, struct ble_gatt_access_ctxt* ctxt, void* arg) { |
| 26 | + return static_cast<ReminderService*>(arg)->OnCommand(ctxt); |
| 27 | +} |
| 28 | + |
| 29 | +ReminderService::ReminderService(System::SystemTask& systemTask, ReminderController& reminderController) |
| 30 | + : characteristicDefinition { |
| 31 | + {.uuid = &uploadCharUuid.u, .access_cb = ReminderCallback, .arg = this, .flags = BLE_GATT_CHR_F_WRITE}, |
| 32 | + {.uuid = &deleteCharUuid.u, .access_cb = ReminderCallback, .arg = this, .flags = BLE_GATT_CHR_F_WRITE}, |
| 33 | + {.uuid = &listCharUuid.u, .access_cb = ReminderCallback, .arg = this, .flags = BLE_GATT_CHR_F_READ}, |
| 34 | + {.uuid = &ackCharUuid.u, |
| 35 | + .access_cb = ReminderCallback, |
| 36 | + .arg = this, |
| 37 | + .flags = BLE_GATT_CHR_F_NOTIFY, |
| 38 | + .val_handle = &ackHandle}, |
| 39 | + {.uuid = &syncCharUuid.u, .access_cb = ReminderCallback, .arg = this, .flags = BLE_GATT_CHR_F_WRITE}, |
| 40 | + {0}}, |
| 41 | + serviceDefinition { |
| 42 | + {.type = BLE_GATT_SVC_TYPE_PRIMARY, .uuid = &serviceUuid.u, .characteristics = characteristicDefinition}, |
| 43 | + {0}}, |
| 44 | + systemTask {systemTask}, |
| 45 | + reminderController {reminderController} { |
| 46 | +} |
| 47 | + |
| 48 | +void ReminderService::Init() { |
| 49 | + int res; |
| 50 | + res = ble_gatts_count_cfg(serviceDefinition); |
| 51 | + ASSERT(res == 0); |
| 52 | + |
| 53 | + res = ble_gatts_add_svcs(serviceDefinition); |
| 54 | + ASSERT(res == 0); |
| 55 | +} |
| 56 | + |
| 57 | +int ReminderService::OnCommand(struct ble_gatt_access_ctxt* ctxt) { |
| 58 | + if (ctxt->op == BLE_GATT_ACCESS_OP_WRITE_CHR) { |
| 59 | + size_t packetLen = OS_MBUF_PKTLEN(ctxt->om); |
| 60 | + |
| 61 | + if (ble_uuid_cmp(ctxt->chr->uuid, &uploadCharUuid.u) == 0) { |
| 62 | + // Upload/modify a single reminder (expects sizeof(Reminder) bytes) |
| 63 | + if (packetLen < sizeof(Reminder)) { |
| 64 | + NRF_LOG_WARNING("[ReminderService] Upload packet too small: %u", packetLen); |
| 65 | + return BLE_ATT_ERR_INVALID_ATTR_VALUE_LEN; |
| 66 | + } |
| 67 | + |
| 68 | + Reminder reminder {}; |
| 69 | + os_mbuf_copydata(ctxt->om, 0, sizeof(Reminder), &reminder); |
| 70 | + if (reminder.id >= Reminder::MaxReminders) { |
| 71 | + NRF_LOG_WARNING("[ReminderService] Invalid reminder ID: %u", reminder.id); |
| 72 | + return BLE_ATT_ERR_INVALID_ATTR_VALUE_LEN; |
| 73 | + } |
| 74 | + |
| 75 | + if (!reminderController.AddOrUpdate(reminder)) { |
| 76 | + return BLE_ATT_ERR_INSUFFICIENT_RES; |
| 77 | + } |
| 78 | + NRF_LOG_INFO("[ReminderService] Uploaded reminder %u: %02d:%02d", reminder.id, reminder.hours, reminder.minutes); |
| 79 | + |
| 80 | + } else if (ble_uuid_cmp(ctxt->chr->uuid, &deleteCharUuid.u) == 0) { |
| 81 | + // Delete by ID (1 byte) |
| 82 | + if (packetLen < 1) { |
| 83 | + return BLE_ATT_ERR_INVALID_ATTR_VALUE_LEN; |
| 84 | + } |
| 85 | + |
| 86 | + uint8_t reminderId = 0; |
| 87 | + os_mbuf_copydata(ctxt->om, 0, 1, &reminderId); |
| 88 | + if (reminderId == 0xFF) { |
| 89 | + reminderController.ClearAll(); |
| 90 | + NRF_LOG_INFO("[ReminderService] Cleared all reminders"); |
| 91 | + } else { |
| 92 | + reminderController.Delete(reminderId); |
| 93 | + NRF_LOG_INFO("[ReminderService] Deleted reminder %u", reminderId); |
| 94 | + } |
| 95 | + |
| 96 | + } else if (ble_uuid_cmp(ctxt->chr->uuid, &syncCharUuid.u) == 0) { |
| 97 | + // Sync: 1 byte count + N * sizeof(Reminder) bytes |
| 98 | + if (packetLen < 1) { |
| 99 | + return BLE_ATT_ERR_INVALID_ATTR_VALUE_LEN; |
| 100 | + } |
| 101 | + |
| 102 | + uint8_t syncCount = 0; |
| 103 | + os_mbuf_copydata(ctxt->om, 0, 1, &syncCount); |
| 104 | + |
| 105 | + if (syncCount > Reminder::MaxReminders) { |
| 106 | + NRF_LOG_WARNING("[ReminderService] Sync count %u exceeds max", syncCount); |
| 107 | + return BLE_ATT_ERR_INVALID_ATTR_VALUE_LEN; |
| 108 | + } |
| 109 | + |
| 110 | + size_t expectedSize = 1 + (syncCount * sizeof(Reminder)); |
| 111 | + if (packetLen < expectedSize) { |
| 112 | + NRF_LOG_WARNING("[ReminderService] Sync packet too small: %u < %u", packetLen, expectedSize); |
| 113 | + return BLE_ATT_ERR_INVALID_ATTR_VALUE_LEN; |
| 114 | + } |
| 115 | + |
| 116 | + reminderController.ClearAll(); |
| 117 | + for (uint8_t i = 0; i < syncCount; i++) { |
| 118 | + Reminder reminder {}; |
| 119 | + os_mbuf_copydata(ctxt->om, 1 + (i * sizeof(Reminder)), sizeof(Reminder), &reminder); |
| 120 | + if (reminder.id < Reminder::MaxReminders) { |
| 121 | + reminderController.AddOrUpdate(reminder); |
| 122 | + } |
| 123 | + } |
| 124 | + NRF_LOG_INFO("[ReminderService] Synced %u reminders", syncCount); |
| 125 | + } |
| 126 | + |
| 127 | + } else if (ctxt->op == BLE_GATT_ACCESS_OP_READ_CHR) { |
| 128 | + if (ble_uuid_cmp(ctxt->chr->uuid, &listCharUuid.u) == 0) { |
| 129 | + // List all reminders: 1 byte count + N * sizeof(Reminder) structs |
| 130 | + uint8_t activeCount = reminderController.ActiveCount(); |
| 131 | + int rc = os_mbuf_append(ctxt->om, &activeCount, sizeof(activeCount)); |
| 132 | + if (rc != 0) { |
| 133 | + return BLE_ATT_ERR_INSUFFICIENT_RES; |
| 134 | + } |
| 135 | + |
| 136 | + const auto& all = reminderController.GetAll(); |
| 137 | + for (uint8_t i = 0; i < activeCount; i++) { |
| 138 | + rc = os_mbuf_append(ctxt->om, &all[i], sizeof(Reminder)); |
| 139 | + if (rc != 0) { |
| 140 | + return BLE_ATT_ERR_INSUFFICIENT_RES; |
| 141 | + } |
| 142 | + } |
| 143 | + NRF_LOG_INFO("[ReminderService] Listed %u reminders", activeCount); |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + return 0; |
| 148 | +} |
| 149 | + |
| 150 | +void ReminderService::SendAckNotification(uint8_t reminderId, uint32_t timestamp) { |
| 151 | + uint16_t connectionHandle = systemTask.nimble().connHandle(); |
| 152 | + if (connectionHandle == 0 || connectionHandle == BLE_HS_CONN_HANDLE_NONE) { |
| 153 | + return; |
| 154 | + } |
| 155 | + |
| 156 | + // 5 bytes: 1 byte ID + 4 bytes timestamp (little-endian) |
| 157 | + uint8_t data[5]; |
| 158 | + data[0] = reminderId; |
| 159 | + data[1] = static_cast<uint8_t>(timestamp); |
| 160 | + data[2] = static_cast<uint8_t>(timestamp >> 8); |
| 161 | + data[3] = static_cast<uint8_t>(timestamp >> 16); |
| 162 | + data[4] = static_cast<uint8_t>(timestamp >> 24); |
| 163 | + |
| 164 | + auto* om = ble_hs_mbuf_from_flat(data, sizeof(data)); |
| 165 | + ble_gattc_notify_custom(connectionHandle, ackHandle, om); |
| 166 | + NRF_LOG_INFO("[ReminderService] Sent ack for reminder %u", reminderId); |
| 167 | +} |
0 commit comments