9#include <BluetoothLock.h>
48 TinyCTSServer() =
default;
59 bool pairing = _settable && _encrypted;
63 uint8_t writePerm = pairing ? 1 : 0;
64 uint16_t write = _settable ? BLEWrite : 0;
69 _current->setCallbacks(
this);
70 _local->setCallbacks(
this);
71 _reference->setCallbacks(
this);
72 updateCurrent(0,
false);
75 _service->addCharacteristic(_current);
76 _service->addCharacteristic(_local);
77 _service->addCharacteristic(_reference);
78 BLE.server()->addService(_service);
79 BLE.startAdvertising();
85 BLE.stopAdvertising();
94 if (_current ==
nullptr || !
notifyDue())
return false;
100 if (_current ==
nullptr)
return false;
102 updateCurrent(adjustReason,
true);
110 class Value :
public BLECharacteristic {
112 Value(BLEUUID uuid, uint16_t flags, uint8_t writePerm)
113 : BLECharacteristic(uuid, flags,
nullptr, 0, writePerm) {}
115 void set(
const uint8_t* data,
size_t len,
bool notify) {
116 _charData = realloc(_charData, len);
117 memcpy(_charData, data, len);
119 if (notify) requestNotify();
124 if (_local ==
nullptr)
return;
127 updateCurrent(TinyCTSCurrentTime::TimeZoneChange,
true);
131 if (_reference ==
nullptr)
return;
137 void onRead(BLECharacteristic* chr)
override {
138 if (chr == _current) updateCurrent(0,
false);
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) {
146 updateCurrent(TinyCTSCurrentTime::ManualUpdate,
true);
148 updateCurrent(0,
false);
150 }
else if (chr == _local) {
153 updateCurrent(TinyCTSCurrentTime::TimeZoneChange,
true);
157 void updateCurrent(uint8_t reason,
bool notify) {
163 uint8_t data[TinyCTSLocalTimeInfo::Size];
164 _local->set(data, localTimeValue(data),
false);
167 void updateReference() {
168 uint8_t data[TinyCTSReferenceTimeInfo::Size];
169 _reference->set(data, referenceTimeValue(data),
false);
172 BLEService* _service =
nullptr;
173 Value* _current =
nullptr;
174 Value* _local =
nullptr;
175 Value* _reference =
nullptr;
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