39 explicit TinyCTSClient(
const char* device =
nullptr, uint32_t scanMs = 5000)
41 if (device !=
nullptr) _device = device;
59 if (!connect())
return false;
61 std::vector<uint8_t> data;
64 storeLocalInfo(data.data(), data.size());
67 storeReferenceInfo(data.data(), data.size());
69 uint32_t t0 = TINY_CTS_MILLIS();
71 _error =
"cannot read the time: " + _error;
73 _roundTripMs =
static_cast<uint32_t
>(TINY_CTS_MILLIS() - t0);
75 if (!ok) _error =
"the device does not know the time";
77 _central->disconnect();
91 uint32_t start = TINY_CTS_MILLIS();
92 if (!connect())
return false;
94 std::vector<uint8_t> data;
96 uint32_t t0 = TINY_CTS_MILLIS();
98 _roundTripMs =
static_cast<uint32_t
>(TINY_CTS_MILLIS() - t0);
101 unixMs +
static_cast<uint32_t
>(TINY_CTS_MILLIS() - start) + _roundTripMs / 2, out);
103 if (!ok) _error =
"rejected: " + _error;
104 _central->disconnect();
113 if (!connect())
return false;
114 uint8_t out[TinyCTSLocalTimeInfo::Size];
117 if (!ok) _error =
"rejected: " + _error;
118 _central->disconnect();
126 std::vector<TinyCTSScanEntry>
scan(uint32_t timeoutMs) {
127 std::vector<TinyCTSScanEntry> result;
129 if (central()) _central->scan(timeoutMs, result, _error);
142 if (!_central) _error =
"no Bluetooth stack";
143 return _central.get();
148 if (central() ==
nullptr)
return false;
149 TinyCTSScanEntry found;
150 if (!_central->connect(_device, _scanMs, found, _error))
return false;
152 _address = found.address;
157 uint32_t _roundTripMs = 0;
158 std::string _adapterName =
"hci0";
160 std::unique_ptr<TinyCTSCentral> _central;
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