TinyCTS
Bluetooth Current Time Service client and server for Arduino
Loading...
Searching...
No Matches
TinyCTSClientBase.h
Go to the documentation of this file.
1
8
9#pragma once
10#include <cctype>
11#include <cstdint>
12#include <ctime>
13#include <string>
14
15#include "TinyCTSTimeSource.h"
16#include "TinyCTSTypes.h"
17
18namespace tiny_cts {
19
26 public:
32 uint64_t getTimeMs() override {
33 if (_timeMs == 0) return 0;
34 uint32_t now = TINY_CTS_MILLIS();
35 _timeMs += static_cast<uint32_t>(now - _lastMillis);
36 _lastMillis = now;
37 return _timeMs;
38 }
39
41 uint32_t getTime() { return static_cast<uint32_t>(getTimeMs() / 1000); }
42
44 std::tm getTm() {
45 time_t t = static_cast<time_t>(getTime());
46 std::tm tm = {};
47 ctsGmtime(t, tm);
48 return tm;
49 }
50
52 std::tm getLocalTm() {
53 time_t t = static_cast<time_t>(getTime()) + _info.utcOffsetMinutes() * 60;
54 std::tm tm = {};
55 ctsGmtime(t, tm);
56 return tm;
57 }
58
60 const TinyCTSLocalTimeInfo& getLocalTimeInfo() const { return _info; }
61
63 const TinyCTSReferenceTimeInfo& getReferenceTimeInfo() const { return _ref; }
64
66 const TinyCTSCurrentTime& getCurrentTime() const { return _current; }
67
69 const std::string& getDeviceName() const { return _name; }
70
72 const std::string& getDeviceAddress() const { return _address; }
73
79 void setLocalTimeInfo(const TinyCTSLocalTimeInfo& info) { _info = info; }
80
82 void setTimeIsLocal(bool local) { _timeIsLocal = local; }
83
84 protected:
91 bool storeCurrentTime(const uint8_t* data, size_t len, uint32_t startMs, uint32_t endMs) {
93 uint64_t ms;
94 if (!TinyCTSCurrentTime::decode(data, len, t) || !t.toUnixMs(ms)) return false;
95 if (_timeIsLocal) ms -= static_cast<int64_t>(_info.utcOffsetMinutes()) * 60000;
96 _current = t;
97 _timeMs = ms + static_cast<uint32_t>(endMs - startMs) / 2;
98 _lastMillis = endMs;
99 return true;
100 }
101
102 void storeLocalInfo(const uint8_t* data, size_t len) {
103 TinyCTSLocalTimeInfo::decode(data, len, _info);
104 }
105
106 void storeReferenceInfo(const uint8_t* data, size_t len) {
107 TinyCTSReferenceTimeInfo::decode(data, len, _ref);
108 }
109
111 size_t timeToWrite(uint64_t unixMs, uint8_t* out) const {
112 if (_timeIsLocal) unixMs += static_cast<int64_t>(_info.utcOffsetMinutes()) * 60000;
113 return TinyCTSCurrentTime::fromUnixMs(unixMs, TinyCTSCurrentTime::ManualUpdate).encode(out);
114 }
115
117 bool isWanted(const std::string& address, const std::string& name) const {
118 return equalsIgnoreCase(address, _device) || name == _device;
119 }
120
121 static bool equalsIgnoreCase(const std::string& a, const std::string& b) {
122 if (a.size() != b.size()) return false;
123 for (size_t i = 0; i < a.size(); i++) {
124 if (tolower(a[i]) != tolower(b[i])) return false;
125 }
126 return true;
127 }
128
129 std::string _device, _name, _address;
130 uint64_t _timeMs = 0;
131 uint32_t _lastMillis = 0;
132 bool _timeIsLocal = false;
133 TinyCTSCurrentTime _current;
134 TinyCTSLocalTimeInfo _info;
135 TinyCTSReferenceTimeInfo _ref;
136};
137
138} // namespace tiny_cts
Time sources of the TinyCTSServer: the interface, a software clock, the system clock and an adapter f...
The data of the Bluetooth Current Time Service (CTS, 0x1805): UUIDs and the encoding of the character...
void ctsGmtime(time_t t, std::tm &tm)
gmtime_r() where it exists, gmtime_s() on Windows
Definition TinyCTSTypes.h:49
The data of a TinyCTSClient. Use TinyCTSClient, which adds the Bluetooth part.
Definition TinyCTSClientBase.h:25
std::tm getTm()
The UTC time as std::tm.
Definition TinyCTSClientBase.h:44
std::tm getLocalTm()
The local time as std::tm, with the offsets of the device.
Definition TinyCTSClientBase.h:52
const std::string & getDeviceAddress() const
The address of the device that was used.
Definition TinyCTSClientBase.h:72
uint64_t getTimeMs() override
The UTC time in ms since 1970-01-01, extrapolated since the last update.
Definition TinyCTSClientBase.h:32
size_t timeToWrite(uint64_t unixMs, uint8_t *out) const
Definition TinyCTSClientBase.h:111
const TinyCTSCurrentTime & getCurrentTime() const
The Current Time as it was sent by the device.
Definition TinyCTSClientBase.h:66
bool isWanted(const std::string &address, const std::string &name) const
Definition TinyCTSClientBase.h:117
void setTimeIsLocal(bool local)
The Current Time of the device is the local time, not UTC.
Definition TinyCTSClientBase.h:82
uint32_t getTime()
The UTC time in seconds since 1970-01-01 (0 if not known).
Definition TinyCTSClientBase.h:41
const TinyCTSReferenceTimeInfo & getReferenceTimeInfo() const
Where the time of the device comes from.
Definition TinyCTSClientBase.h:63
const std::string & getDeviceName() const
The name of the device that was used (empty before begin()).
Definition TinyCTSClientBase.h:69
const TinyCTSLocalTimeInfo & getLocalTimeInfo() const
The time zone of the device (valid after update() if it has it).
Definition TinyCTSClientBase.h:60
bool storeCurrentTime(const uint8_t *data, size_t len, uint32_t startMs, uint32_t endMs)
Definition TinyCTSClientBase.h:91
void setLocalTimeInfo(const TinyCTSLocalTimeInfo &info)
Tell the client the time zone of the device, if it does not have the Local Time Information....
Definition TinyCTSClientBase.h:79
Abstract base class of the time sources: an object that knows the current UTC time.
Definition TinyCTSTimeSource.h:51
The Current Time characteristic (0x2A2B): 10 bytes with the date and time (Exact Time 256),...
Definition TinyCTSTypes.h:68
static TinyCTSCurrentTime fromUnixMs(uint64_t unixMs, uint8_t reason=0)
The characteristic for a time in ms since 1970-01-01.
Definition TinyCTSTypes.h:90
bool toUnixMs(uint64_t &unixMs) const
The time in ms since 1970-01-01.
Definition TinyCTSTypes.h:115
static bool decode(const uint8_t *data, size_t len, TinyCTSCurrentTime &t)
Read the characteristic. Some devices send the 9 bytes of the Exact Time 256 without the adjust reaso...
Definition TinyCTSTypes.h:153
size_t encode(uint8_t *out) const
Write the 10 bytes.
Definition TinyCTSTypes.h:134
The Local Time Information characteristic (0x2A0F): the time zone and the daylight saving time (DST) ...
Definition TinyCTSTypes.h:173
The Reference Time Information characteristic (0x2A14): where the time of the server comes from,...
Definition TinyCTSTypes.h:227