TinyCTS
Bluetooth Current Time Service client and server for Arduino
Loading...
Searching...
No Matches
TinyCTSServerPico.h
Go to the documentation of this file.
1
7
8#pragma once
9#include <BluetoothLock.h>
10
11#include <cstdlib>
12#include <cstring>
13
14#include "TinyCTSPicoBLE.h"
16
17namespace tiny_cts {
18
46class TinyCTSServer : public TinyCTSServerBase, public BLECharacteristicCallbacks {
47 public:
48 TinyCTSServer() = default;
49
57 bool begin(TinyCTSTimeSource& source, const char* deviceName = "TinyCTS") {
58 _source = &source;
59 bool pairing = _settable && _encrypted;
60 if (!ctsPicoBegin(deviceName, pairing)) return false;
61
62 // writing needs an encrypted connection (ATT_SECURITY_ENCRYPTED = 1)
63 uint8_t writePerm = pairing ? 1 : 0;
64 uint16_t write = _settable ? BLEWrite : 0;
65 _service = new BLEService(BLEUUID(CTS_SERVICE_UUID));
66 _current = new Value(BLEUUID(CTS_CURRENT_TIME_UUID), BLERead | BLENotify | write, writePerm);
67 _local = new Value(BLEUUID(CTS_LOCAL_TIME_INFO_UUID), BLERead | write, writePerm);
68 _reference = new Value(BLEUUID(CTS_REFERENCE_TIME_INFO_UUID), BLERead, 0);
69 _current->setCallbacks(this);
70 _local->setCallbacks(this);
71 _reference->setCallbacks(this);
72 updateCurrent(0, false);
73 updateLocal();
74 updateReference();
75 _service->addCharacteristic(_current);
76 _service->addCharacteristic(_local);
77 _service->addCharacteristic(_reference);
78 BLE.server()->addService(_service);
79 BLE.startAdvertising();
80 return true;
81 }
82
84 void end() {
85 BLE.stopAdvertising();
86 _source = nullptr;
87 }
88
93 bool process() {
94 if (_current == nullptr || !notifyDue()) return false;
95 return notifyTime(0);
96 }
97
99 bool notifyTime(uint8_t adjustReason = 0) {
100 if (_current == nullptr) return false;
101 BluetoothLock lock; // the callbacks of the stack use the time source too
102 updateCurrent(adjustReason, true);
103 return true;
104 }
105
106 protected:
110 class Value : public BLECharacteristic {
111 public:
112 Value(BLEUUID uuid, uint16_t flags, uint8_t writePerm)
113 : BLECharacteristic(uuid, flags, nullptr, 0, writePerm) {}
114
115 void set(const uint8_t* data, size_t len, bool notify) {
116 _charData = realloc(_charData, len);
117 memcpy(_charData, data, len);
118 _charLen = len;
119 if (notify) requestNotify();
120 }
121 };
122
123 void onTimeZoneChanged() override {
124 if (_local == nullptr) return;
125 BluetoothLock lock;
126 updateLocal();
127 updateCurrent(TinyCTSCurrentTime::TimeZoneChange, true);
128 }
129
130 void onReferenceChanged() override {
131 if (_reference == nullptr) return;
132 BluetoothLock lock;
133 updateReference();
134 }
135
136 // (runs in the context of the Bluetooth stack)
137 void onRead(BLECharacteristic* chr) override {
138 if (chr == _current) updateCurrent(0, false);
139 }
140
141 void onWrite(BLECharacteristic* chr) override {
142 const uint8_t* data = static_cast<const uint8_t*>(chr->valueData());
143 size_t len = chr->valueLen();
144 if (chr == _current) {
145 if (setTimeFromWrite(data, len)) {
146 updateCurrent(TinyCTSCurrentTime::ManualUpdate, true);
147 } else {
148 updateCurrent(0, false); // refused: show the real time again
149 }
150 } else if (chr == _local) {
151 setTimeZoneFromWrite(data, len);
152 updateLocal();
153 updateCurrent(TinyCTSCurrentTime::TimeZoneChange, true);
154 }
155 }
156
157 void updateCurrent(uint8_t reason, bool notify) {
158 uint8_t data[TinyCTSCurrentTime::Size];
159 _current->set(data, currentTimeValue(reason, data), notify);
160 }
161
162 void updateLocal() {
163 uint8_t data[TinyCTSLocalTimeInfo::Size];
164 _local->set(data, localTimeValue(data), false);
165 }
166
167 void updateReference() {
168 uint8_t data[TinyCTSReferenceTimeInfo::Size];
169 _reference->set(data, referenceTimeValue(data), false);
170 }
171
172 BLEService* _service = nullptr;
173 Value* _current = nullptr;
174 Value* _local = nullptr;
175 Value* _reference = nullptr;
176};
177
178} // namespace tiny_cts
Starts the BLE stack of the Raspberry Pi Pico (W) core once, for the client and the server.
bool ctsPicoBegin(const char *name, bool pairing)
Definition TinyCTSPicoBLE.h:20
The logic of the Current Time Service server, independent of the Bluetooth stack: the values of the c...
constexpr uint16_t CTS_CURRENT_TIME_UUID
16 bit UUID of the Current Time characteristic (read, notify, write)
Definition TinyCTSTypes.h:19
constexpr uint16_t CTS_SERVICE_UUID
16 bit UUID of the Current Time Service
Definition TinyCTSTypes.h:17
constexpr uint16_t CTS_REFERENCE_TIME_INFO_UUID
16 bit UUID of the Reference Time Information characteristic (read)
Definition TinyCTSTypes.h:23
constexpr uint16_t CTS_LOCAL_TIME_INFO_UUID
16 bit UUID of the Local Time Information characteristic (read, write)
Definition TinyCTSTypes.h:21
Definition TinyCTSServerPico.h:110
Settings and values of a TinyCTSServer. Use TinyCTSServer, which adds the Bluetooth part.
Definition TinyCTSServerBase.h:22
bool setTimeZoneFromWrite(const uint8_t *data, size_t len)
Definition TinyCTSServerBase.h:117
bool setTimeFromWrite(const uint8_t *data, size_t len)
Definition TinyCTSServerBase.h:102
bool notifyDue()
Definition TinyCTSServerBase.h:125
size_t currentTimeValue(uint8_t reason, uint8_t *out)
Definition TinyCTSServerBase.h:84
A BLE peripheral that offers the Current Time Service (0x1805) and serves the time of a time source t...
Definition TinyCTSServerDesktop.h:40
void end()
Stop advertising.
Definition TinyCTSServerPico.h:84
bool begin(TinyCTSTimeSource &source, const char *deviceName="TinyCTS")
Start the BLE hardware, the service and the advertising.
Definition TinyCTSServerPico.h:57
void onTimeZoneChanged() override
Definition TinyCTSServerPico.h:123
bool process()
Call this regularly in loop(): it sends the notifications.
Definition TinyCTSServerPico.h:93
void onReferenceChanged() override
Definition TinyCTSServerPico.h:130
bool notifyTime(uint8_t adjustReason=0)
Notify the subscribed clients now.
Definition TinyCTSServerDesktop.h:95
bool onRead(uint16_t uuid, std::vector< uint8_t > &value) override
Definition TinyCTSServerDesktop.h:113
bool onWrite(uint16_t uuid, const uint8_t *data, size_t len) override
Definition TinyCTSServerDesktop.h:129
Abstract base class of the time sources: an object that knows the current UTC time.
Definition TinyCTSTimeSource.h:51
static constexpr size_t Size
Size of the encoded characteristic.
Definition TinyCTSTypes.h:70