TinyCTS
Bluetooth Current Time Service client and server for Arduino
Loading...
Searching...
No Matches
TinyCTSClientDesktop.h
Go to the documentation of this file.
1
7
8#pragma once
9#include "TinyCTSBackend.h"
11
12namespace tiny_cts {
13
31 public:
39 explicit TinyCTSClient(const char* device = nullptr, uint32_t scanMs = 5000)
40 : _scanMs(scanMs) {
41 if (device != nullptr) _device = device;
42 }
43
45 void setAdapter(const char* name) {
46 _adapterName = name;
47 _central.reset();
48 }
49
51 bool begin() { return update(); }
52
58 bool update() {
59 if (!connect()) return false;
60 bool ok = false;
61 std::vector<uint8_t> data;
62 std::string ignored;
63 if (_central->read(CTS_LOCAL_TIME_INFO_UUID, data, ignored)) {
64 storeLocalInfo(data.data(), data.size());
65 }
66 if (_central->read(CTS_REFERENCE_TIME_INFO_UUID, data, ignored)) {
67 storeReferenceInfo(data.data(), data.size());
68 }
69 uint32_t t0 = TINY_CTS_MILLIS();
70 if (!_central->read(CTS_CURRENT_TIME_UUID, data, _error)) {
71 _error = "cannot read the time: " + _error;
72 } else {
73 _roundTripMs = static_cast<uint32_t>(TINY_CTS_MILLIS() - t0);
74 ok = storeCurrentTime(data.data(), data.size(), t0, t0 + _roundTripMs);
75 if (!ok) _error = "the device does not know the time";
76 }
77 _central->disconnect();
78 return ok;
79 }
80
90 bool setTimeMs(uint64_t unixMs) override {
91 uint32_t start = TINY_CTS_MILLIS();
92 if (!connect()) return false;
93 // the round trip time of a read: the write needs about half of it
94 std::vector<uint8_t> data;
95 std::string ignored;
96 uint32_t t0 = TINY_CTS_MILLIS();
97 _central->read(CTS_CURRENT_TIME_UUID, data, ignored);
98 _roundTripMs = static_cast<uint32_t>(TINY_CTS_MILLIS() - t0);
99 uint8_t out[TinyCTSCurrentTime::Size];
100 size_t len = timeToWrite(
101 unixMs + static_cast<uint32_t>(TINY_CTS_MILLIS() - start) + _roundTripMs / 2, out);
102 bool ok = _central->write(CTS_CURRENT_TIME_UUID, out, len, _error);
103 if (!ok) _error = "rejected: " + _error;
104 _central->disconnect();
105 return ok;
106 }
107
113 if (!connect()) return false;
114 uint8_t out[TinyCTSLocalTimeInfo::Size];
115 info.encode(out);
116 bool ok = _central->write(CTS_LOCAL_TIME_INFO_UUID, out, sizeof out, _error);
117 if (!ok) _error = "rejected: " + _error;
118 _central->disconnect();
119 return ok;
120 }
121
126 std::vector<TinyCTSScanEntry> scan(uint32_t timeoutMs) {
127 std::vector<TinyCTSScanEntry> result;
128 _error.clear();
129 if (central()) _central->scan(timeoutMs, result, _error);
130 return result;
131 }
132
134 const std::string& getLastError() const { return _error; }
135
137 uint32_t getRoundTripMs() const { return _roundTripMs; }
138
139 protected:
140 TinyCTSCentral* central() {
141 if (!_central) _central = ctsCreateCentral(_adapterName);
142 if (!_central) _error = "no Bluetooth stack";
143 return _central.get();
144 }
145
146 bool connect() {
147 _error.clear();
148 if (central() == nullptr) return false;
149 TinyCTSScanEntry found;
150 if (!_central->connect(_device, _scanMs, found, _error)) return false;
151 _name = found.name;
152 _address = found.address;
153 return true;
154 }
155
156 uint32_t _scanMs;
157 uint32_t _roundTripMs = 0;
158 std::string _adapterName = "hci0";
159 std::string _error;
160 std::unique_ptr<TinyCTSCentral> _central;
161};
162
163} // namespace tiny_cts
Includes the Bluetooth stack of the desktop: BlueZ (Linux) is header only. CoreBluetooth (macOS) and ...
std::unique_ptr< TinyCTSCentral > ctsCreateCentral(const std::string &adapter)
Definition TinyCTSBlueZBackend.h:438
The logic of the Current Time Service client, independent of the Bluetooth stack: decoding of the cha...
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 client side (central role) of the Bluetooth stack of the operating system: finds a device,...
Definition TinyCTSDesktop.h:38
The data of a TinyCTSClient. Use TinyCTSClient, which adds the Bluetooth part.
Definition TinyCTSClientBase.h:25
size_t timeToWrite(uint64_t unixMs, uint8_t *out) const
Definition TinyCTSClientBase.h:111
bool storeCurrentTime(const uint8_t *data, size_t len, uint32_t startMs, uint32_t endMs)
Definition TinyCTSClientBase.h:91
bool begin()
Find the device and read the time.
Definition TinyCTSClientDesktop.h:51
TinyCTSClient(const char *device=nullptr, uint32_t scanMs=5000)
Definition TinyCTSClientDesktop.h:39
bool setTimeZone(const TinyCTSLocalTimeInfo &info)
Set the time zone of the device by writing its Local Time Information (if the device allows it).
Definition TinyCTSClientDesktop.h:112
uint32_t getRoundTripMs() const
The round trip time of the last read or write in ms (the connection interval).
Definition TinyCTSClientDesktop.h:137
bool setTimeMs(uint64_t unixMs) override
Set the time of the device by writing the Current Time. It works if the device allows it; if it requi...
Definition TinyCTSClientDesktop.h:90
void setAdapter(const char *name)
The Bluetooth adapter to use (Linux only, default: hci0).
Definition TinyCTSClientDesktop.h:45
std::vector< TinyCTSScanEntry > scan(uint32_t timeoutMs)
List the devices that are in range.
Definition TinyCTSClientDesktop.h:126
const std::string & getLastError() const
The reason why the last call failed.
Definition TinyCTSClientDesktop.h:134
bool update()
Read the time (and the other characteristics) again: the device is connected, read and disconnected.
Definition TinyCTSClientDesktop.h:58
static constexpr size_t Size
Size of the encoded characteristic.
Definition TinyCTSTypes.h:70
The Local Time Information characteristic (0x2A0F): the time zone and the daylight saving time (DST) ...
Definition TinyCTSTypes.h:173