TinyCTS
Bluetooth Current Time Service client and server for Arduino
Loading...
Searching...
No Matches
TinyCTSServerBase.h
Go to the documentation of this file.
1
8
9#pragma once
10#include <cstdint>
11
12#include "TinyCTSTimeSource.h"
13#include "TinyCTSTypes.h"
14
15namespace tiny_cts {
16
23 public:
24 virtual ~TinyCTSServerBase() = default;
25
32 void setNotifyInterval(uint32_t ms) { _notifyMs = ms; }
33
39 void setTimeZone(int utcOffsetMinutes, int dstMinutes = 0) {
40 _info = TinyCTSLocalTimeInfo::fromMinutes(utcOffsetMinutes, dstMinutes);
42 }
43
45 void setReferenceInfo(uint8_t source, uint8_t accuracy = 255) {
46 _ref.source = source;
47 _ref.accuracy = accuracy;
49 }
50
56 void setTimeIsLocal(bool local) { _timeIsLocal = local; }
57
66 void enableTimeSetting(bool requireEncryption = true) {
67 _settable = true;
68 _encrypted = requireEncryption;
69 }
70
72 uint32_t getTimeSetCount() const { return _setCount; }
73
74 protected:
76 virtual void onTimeZoneChanged() {}
78 virtual void onReferenceChanged() {}
79
84 size_t currentTimeValue(uint8_t reason, uint8_t* out) {
85 uint64_t ms = _source != nullptr ? _source->getTimeMs() : 0;
87 if (ms != 0) {
88 if (_timeIsLocal) ms += static_cast<int64_t>(_info.utcOffsetMinutes()) * 60000;
89 t = TinyCTSCurrentTime::fromUnixMs(ms, reason);
90 }
91 return t.encode(out);
92 }
93
94 size_t localTimeValue(uint8_t* out) const { return _info.encode(out); }
95
96 size_t referenceTimeValue(uint8_t* out) const { return _ref.encode(out); }
97
102 bool setTimeFromWrite(const uint8_t* data, size_t len) {
104 uint64_t ms;
105 if (_source == nullptr || !_settable) return false;
106 if (!TinyCTSCurrentTime::decode(data, len, t) || !t.toUnixMs(ms)) return false;
107 if (_timeIsLocal) ms -= static_cast<int64_t>(_info.utcOffsetMinutes()) * 60000;
108 if (!_source->setTimeMs(ms)) return false;
109 _setCount++;
110 return true;
111 }
112
117 bool setTimeZoneFromWrite(const uint8_t* data, size_t len) {
119 if (!_settable || !TinyCTSLocalTimeInfo::decode(data, len, info)) return false;
120 _info = info;
121 return true;
122 }
123
125 bool notifyDue() {
126 if (_source == nullptr || _notifyMs == 0) return false;
127 uint32_t now = TINY_CTS_MILLIS();
128 if (static_cast<uint32_t>(now - _lastNotify) < _notifyMs) return false;
129 _lastNotify = now;
130 return true;
131 }
132
133 TinyCTSTimeSource* _source = nullptr;
136 uint32_t _notifyMs = 0;
137 uint32_t _lastNotify = 0;
138 uint32_t _setCount = 0;
139 bool _timeIsLocal = false;
140 bool _settable = false;
141 bool _encrypted = true;
142};
143
144} // 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...
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
uint32_t getTimeSetCount() const
The number of times that a client has set the time.
Definition TinyCTSServerBase.h:72
bool setTimeFromWrite(const uint8_t *data, size_t len)
Definition TinyCTSServerBase.h:102
void setTimeZone(int utcOffsetMinutes, int dstMinutes=0)
The time zone that is offered in the Local Time Information.
Definition TinyCTSServerBase.h:39
void setTimeIsLocal(bool local)
The Current Time contains the local time (UTC + the offsets of the Local Time Information) instead of...
Definition TinyCTSServerBase.h:56
virtual void onReferenceChanged()
Definition TinyCTSServerBase.h:78
bool notifyDue()
Definition TinyCTSServerBase.h:125
virtual void onTimeZoneChanged()
Definition TinyCTSServerBase.h:76
void enableTimeSetting(bool requireEncryption=true)
Allow the clients to set the time of the time source by writing the Current Time (and the time zone b...
Definition TinyCTSServerBase.h:66
void setReferenceInfo(uint8_t source, uint8_t accuracy=255)
Where the time comes from and how exact it is (125 ms steps).
Definition TinyCTSServerBase.h:45
size_t currentTimeValue(uint8_t reason, uint8_t *out)
Definition TinyCTSServerBase.h:84
void setNotifyInterval(uint32_t ms)
Notify the subscribed clients regularly in process(). 0 (default): only when the time was set or the ...
Definition TinyCTSServerBase.h:32
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
static TinyCTSLocalTimeInfo fromMinutes(int utcOffsetMinutes, int dstMinutes=0)
From the offsets in minutes.
Definition TinyCTSTypes.h:186
The Reference Time Information characteristic (0x2A14): where the time of the server comes from,...
Definition TinyCTSTypes.h:227