TinyCTS
Bluetooth Current Time Service client and server for Arduino
Loading...
Searching...
No Matches
TinyCTSServerArduinoBLE.h
Go to the documentation of this file.
1
7
8#pragma once
9#include "TinyCTSArduinoBLE.h"
11
12namespace tiny_cts {
13
42class TinyCTSServer : public TinyCTSServerBase {
43 public:
44 TinyCTSServer() = default;
45
53 bool begin(TinyCTSTimeSource& source, const char* deviceName = "TinyCTS") {
54 _source = &source;
55 if (!ctsArduinoBleBegin()) return false;
56 instance() = this;
57 if (_service == nullptr) {
58 uint16_t encryption = _settable && _encrypted ? BLEEncryption : 0;
59 uint16_t write = _settable ? BLEWrite : 0;
60 _service = new BLEService("1805");
61 _current = new BLECharacteristic("2A2B", BLERead | BLENotify | write | encryption,
63 _local = new BLECharacteristic("2A0F", BLERead | write | encryption,
64 TinyCTSLocalTimeInfo::Size);
65 _reference = new BLECharacteristic("2A14", BLERead, TinyCTSReferenceTimeInfo::Size);
66 _current->setEventHandler(BLERead, onReadCurrent);
67 if (_settable) {
68 _current->setEventHandler(BLEWritten, onWriteCurrent);
69 _local->setEventHandler(BLEWritten, onWriteLocal);
70 }
71 _service->addCharacteristic(*_current);
72 _service->addCharacteristic(*_local);
73 _service->addCharacteristic(*_reference);
74 BLE.addService(*_service);
75 }
76 updateCurrent(0);
77 updateLocal();
78 updateReference();
79 BLE.setLocalName(deviceName);
80 BLE.setDeviceName(deviceName);
81 BLE.setAdvertisedService(*_service);
82 return BLE.advertise() != 0;
83 }
84
86 void end() {
87 BLE.stopAdvertise();
88 _source = nullptr;
89 instance() = nullptr;
90 }
91
97 bool process() {
98 if (_service == nullptr) return false;
99 BLE.poll();
100 if (!notifyDue()) return false;
101 return notifyTime(0);
102 }
103
105 bool notifyTime(uint8_t adjustReason = 0) {
106 if (_current == nullptr) return false;
107 updateCurrent(adjustReason);
108 return true;
109 }
110
111 protected:
112 static TinyCTSServer*& instance() {
113 static TinyCTSServer* server = nullptr;
114 return server;
115 }
116
117 void onTimeZoneChanged() override {
118 if (_local == nullptr) return;
119 updateLocal();
120 notifyTime(TinyCTSCurrentTime::TimeZoneChange);
121 }
122
123 void onReferenceChanged() override {
124 if (_reference != nullptr) updateReference();
125 }
126
127 // the events of ArduinoBLE: plain functions without a context
128 static void onReadCurrent(BLEDevice, BLECharacteristic) {
129 if (instance() != nullptr) instance()->updateCurrent(0);
130 }
131
132 static void onWriteCurrent(BLEDevice, BLECharacteristic chr) {
133 TinyCTSServer* s = instance();
134 if (s == nullptr) return;
135 if (s->setTimeFromWrite(chr.value(), chr.valueLength())) {
136 s->updateCurrent(TinyCTSCurrentTime::ManualUpdate);
137 } else {
138 s->updateCurrent(0); // refused: show the real time again
139 }
140 }
141
142 static void onWriteLocal(BLEDevice, BLECharacteristic chr) {
143 TinyCTSServer* s = instance();
144 if (s == nullptr) return;
145 s->setTimeZoneFromWrite(chr.value(), chr.valueLength());
146 s->updateLocal();
147 s->updateCurrent(TinyCTSCurrentTime::TimeZoneChange);
148 }
149
150 // (writing the value notifies the subscribed clients)
151 void updateCurrent(uint8_t reason) {
152 uint8_t data[TinyCTSCurrentTime::Size];
153 _current->writeValue(data, static_cast<int>(currentTimeValue(reason, data)));
154 }
155
156 void updateLocal() {
157 uint8_t data[TinyCTSLocalTimeInfo::Size];
158 _local->writeValue(data, static_cast<int>(localTimeValue(data)));
159 }
160
161 void updateReference() {
162 uint8_t data[TinyCTSReferenceTimeInfo::Size];
163 _reference->writeValue(data, static_cast<int>(referenceTimeValue(data)));
164 }
165
166 BLEService* _service = nullptr;
167 BLECharacteristic* _current = nullptr;
168 BLECharacteristic* _local = nullptr;
169 BLECharacteristic* _reference = nullptr;
170};
171
172} // namespace tiny_cts
Starts the ArduinoBLE stack once, for the client and the server.
bool ctsArduinoBleBegin()
Definition TinyCTSArduinoBLE.h:15
The logic of the Current Time Service server, independent of the Bluetooth stack: the values of the c...
Settings and values of a TinyCTSServer. Use TinyCTSServer, which adds the Bluetooth part.
Definition TinyCTSServerBase.h:22
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 TinyCTSServerArduinoBLE.h:86
bool begin(TinyCTSTimeSource &source, const char *deviceName="TinyCTS")
Start the BLE hardware, the service and the advertising.
Definition TinyCTSServerArduinoBLE.h:53
void onTimeZoneChanged() override
Definition TinyCTSServerArduinoBLE.h:117
bool process()
Call this regularly in loop(): it handles the requests of the clients and sends the notifications.
Definition TinyCTSServerArduinoBLE.h:97
void onReferenceChanged() override
Definition TinyCTSServerArduinoBLE.h:123
bool notifyTime(uint8_t adjustReason=0)
Notify the subscribed clients now.
Definition TinyCTSServerDesktop.h:95
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