44 TinyCTSServer() =
default;
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);
68 _current->setEventHandler(BLEWritten, onWriteCurrent);
69 _local->setEventHandler(BLEWritten, onWriteLocal);
71 _service->addCharacteristic(*_current);
72 _service->addCharacteristic(*_local);
73 _service->addCharacteristic(*_reference);
74 BLE.addService(*_service);
79 BLE.setLocalName(deviceName);
80 BLE.setDeviceName(deviceName);
81 BLE.setAdvertisedService(*_service);
82 return BLE.advertise() != 0;
98 if (_service ==
nullptr)
return false;
106 if (_current ==
nullptr)
return false;
107 updateCurrent(adjustReason);
118 if (_local ==
nullptr)
return;
120 notifyTime(TinyCTSCurrentTime::TimeZoneChange);
124 if (_reference !=
nullptr) updateReference();
128 static void onReadCurrent(BLEDevice, BLECharacteristic) {
129 if (instance() !=
nullptr) instance()->updateCurrent(0);
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);
142 static void onWriteLocal(BLEDevice, BLECharacteristic chr) {
143 TinyCTSServer* s = instance();
144 if (s ==
nullptr)
return;
145 s->setTimeZoneFromWrite(chr.value(), chr.valueLength());
147 s->updateCurrent(TinyCTSCurrentTime::TimeZoneChange);
151 void updateCurrent(uint8_t reason) {
153 _current->writeValue(data,
static_cast<int>(
currentTimeValue(reason, data)));
157 uint8_t data[TinyCTSLocalTimeInfo::Size];
158 _local->writeValue(data,
static_cast<int>(localTimeValue(data)));
161 void updateReference() {
162 uint8_t data[TinyCTSReferenceTimeInfo::Size];
163 _reference->writeValue(data,
static_cast<int>(referenceTimeValue(data)));
166 BLEService* _service =
nullptr;
167 BLECharacteristic* _current =
nullptr;
168 BLECharacteristic* _local =
nullptr;
169 BLECharacteristic* _reference =
nullptr;
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