TinyCTS
Bluetooth Current Time Service client and server for Arduino
Loading...
Searching...
No Matches
TinyCTSServerDesktop.h
Go to the documentation of this file.
1
7
8#pragma once
9#include <mutex>
10
11#include "TinyCTSBackend.h"
13
14namespace tiny_cts {
15
40class TinyCTSServer : public TinyCTSServerBase, private TinyCTSPeripheralHandler {
41 public:
42 TinyCTSServer() = default;
43 ~TinyCTSServer() override { end(); }
44
46 void setAdapter(const char* name) { _adapterName = name; }
47
55 bool begin(TinyCTSTimeSource& source, const char* deviceName = "TinyCTS") {
56 std::lock_guard<std::recursive_mutex> lock(_mutex);
57 _source = &source;
58 _error.clear();
59 _peripheral = ctsCreatePeripheral(_adapterName);
60 if (!_peripheral) {
61 _error = "no Bluetooth stack";
62 return false;
63 }
64 if (!_peripheral->start(deviceName, _settable, _encrypted, this, _error)) {
65 _peripheral.reset();
66 return false;
67 }
68 return true;
69 }
70
72 void end() {
73 std::lock_guard<std::recursive_mutex> lock(_mutex);
74 if (_peripheral) _peripheral->stop();
75 _peripheral.reset();
76 _source = nullptr;
77 }
78
84 bool process() {
85 {
86 std::lock_guard<std::recursive_mutex> lock(_mutex);
87 if (!_peripheral) return false;
88 _peripheral->process();
89 if (!notifyDue()) return false;
90 }
91 return notifyTime(0);
92 }
93
95 bool notifyTime(uint8_t adjustReason = 0) {
96 std::lock_guard<std::recursive_mutex> lock(_mutex);
97 if (!_peripheral) return false;
98 uint8_t data[TinyCTSCurrentTime::Size];
99 size_t len = currentTimeValue(adjustReason, data);
100 _peripheral->notify(CTS_CURRENT_TIME_UUID, std::vector<uint8_t>(data, data + len));
101 return true;
102 }
103
105 const std::string& getLastError() const { return _error; }
106
108 uint32_t getRequestCount() const { return _requests; }
109
110 protected:
111 void onTimeZoneChanged() override { notifyTime(TinyCTSCurrentTime::TimeZoneChange); }
112
113 bool onRead(uint16_t uuid, std::vector<uint8_t>& value) override {
114 std::lock_guard<std::recursive_mutex> lock(_mutex);
115 value.resize(TinyCTSCurrentTime::Size);
116 if (uuid == CTS_CURRENT_TIME_UUID) {
117 value.resize(currentTimeValue(0, value.data()));
118 } else if (uuid == CTS_LOCAL_TIME_INFO_UUID) {
119 value.resize(localTimeValue(value.data()));
120 } else if (uuid == CTS_REFERENCE_TIME_INFO_UUID) {
121 value.resize(referenceTimeValue(value.data()));
122 } else {
123 return false;
124 }
125 _requests++;
126 return true;
127 }
128
129 bool onWrite(uint16_t uuid, const uint8_t* data, size_t len) override {
130 std::lock_guard<std::recursive_mutex> lock(_mutex);
131 if (uuid == CTS_CURRENT_TIME_UUID && setTimeFromWrite(data, len)) {
132 notifyTime(TinyCTSCurrentTime::ManualUpdate);
133 return true;
134 }
135 if (uuid == CTS_LOCAL_TIME_INFO_UUID && setTimeZoneFromWrite(data, len)) {
136 notifyTime(TinyCTSCurrentTime::TimeZoneChange);
137 return true;
138 }
139 return false;
140 }
141
142 std::string _adapterName = "hci0", _error;
143 std::unique_ptr<TinyCTSPeripheral> _peripheral;
144 std::recursive_mutex _mutex;
145 uint32_t _requests = 0;
146};
147
148} // namespace tiny_cts
Includes the Bluetooth stack of the desktop: BlueZ (Linux) is header only. CoreBluetooth (macOS) and ...
std::unique_ptr< TinyCTSPeripheral > ctsCreatePeripheral(const std::string &adapter)
Definition TinyCTSBlueZBackend.h:442
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_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
The callbacks of a TinyCTSPeripheral: it asks the handler for the value when a client reads a charact...
Definition TinyCTSDesktop.h:74
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
uint32_t getRequestCount() const
The number of values that the clients have read.
Definition TinyCTSServerDesktop.h:108
void end()
Unregister the service and stop the advertising.
Definition TinyCTSServerDesktop.h:72
bool begin(TinyCTSTimeSource &source, const char *deviceName="TinyCTS")
Register the service and start the advertising.
Definition TinyCTSServerDesktop.h:55
void onTimeZoneChanged() override
Definition TinyCTSServerDesktop.h:111
const std::string & getLastError() const
The reason why begin() failed.
Definition TinyCTSServerDesktop.h:105
bool process()
Handle the requests of the clients and send the notifications. Call this regularly in loop().
Definition TinyCTSServerDesktop.h:84
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
void setAdapter(const char *name)
The Bluetooth adapter to use (Linux only, default: hci0). Call it before begin().
Definition TinyCTSServerDesktop.h:46
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