TinyCTS
Bluetooth Current Time Service client and server for Arduino
Loading...
Searching...
No Matches
tiny_cts::TinyCTSServer Class Reference

A BLE peripheral that offers the Current Time Service (0x1805) and serves the time of a time source to the BLE clients (phones, watches, boards with a TinyCTSClient). More...

#include <TinyCTSServerDesktop.h>

Inheritance diagram for tiny_cts::TinyCTSServer:
tiny_cts::TinyCTSServerBase tiny_cts::TinyCTSPeripheralHandler tiny_cts::TinyCTSServerBase tiny_cts::TinyCTSServerBase tiny_cts::TinyCTSServerBase

Classes

class  Value

Public Member Functions

bool begin (TinyCTSTimeSource &source, const char *deviceName="TinyCTS")
 Register the service and start the advertising.
bool begin (TinyCTSTimeSource &source, const char *deviceName="TinyCTS")
 Start the BLE hardware, the service and the advertising.
bool begin (TinyCTSTimeSource &source, const char *deviceName="TinyCTS")
 Start the service and the advertising. NimBLEDevice::init() is called with the name, if the NimBLE stack is not initialized yet.
bool begin (TinyCTSTimeSource &source, const char *deviceName="TinyCTS")
 Start the BLE hardware, the service and the advertising.
void end ()
 Unregister the service and stop the advertising.
void end ()
 Stop advertising.
void end ()
 Stop advertising.
void end ()
 Stop advertising.
const std::string & getLastError () const
 The reason why begin() failed.
uint32_t getRequestCount () const
 The number of values that the clients have read.
bool notifyTime (uint8_t adjustReason=0)
 Notify the subscribed clients now.
bool notifyTime (uint8_t adjustReason=0)
 Notify the subscribed clients now.
bool notifyTime (uint8_t adjustReason=0)
 Notify the subscribed clients now.
bool notifyTime (uint8_t adjustReason=0)
 Notify the subscribed clients now.
bool process ()
 Handle the requests of the clients and send the notifications. Call this regularly in loop().
bool process ()
 Call this regularly in loop(): it handles the requests of the clients and sends the notifications.
bool process ()
 Call this regularly in loop(): it sends the notifications.
bool process ()
 Call this regularly in loop(): it sends the notifications.
void setAdapter (const char *name)
 The Bluetooth adapter to use (Linux only, default: hci0). Call it before begin().
Public Member Functions inherited from tiny_cts::TinyCTSServerBase
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 by writing the Local Time Information). Needs a time source that implements setTimeMs(). Call it before begin().
uint32_t getTimeSetCount () const
 The number of times that a client has set the time.
void setNotifyInterval (uint32_t ms)
 Notify the subscribed clients regularly in process(). 0 (default): only when the time was set or the time zone changed. The specification asks for a notification when the time changes by a larger step: use a long interval, e.g. 60000 ms.
void setReferenceInfo (uint8_t source, uint8_t accuracy=255)
 Where the time comes from and how exact it is (125 ms steps).
void setTimeIsLocal (bool local)
 The Current Time contains the local time (UTC + the offsets of the Local Time Information) instead of UTC (default). Some clients expect the local time. Call it before begin().
void setTimeZone (int utcOffsetMinutes, int dstMinutes=0)
 The time zone that is offered in the Local Time Information.

Protected Member Functions

void onRead (BLECharacteristic *chr) override
void onRead (NimBLECharacteristic *chr, NimBLEConnInfo &) override
bool onRead (uint16_t uuid, std::vector< uint8_t > &value) override
void onReferenceChanged () override
void onReferenceChanged () override
void onReferenceChanged () override
void onTimeZoneChanged () override
void onTimeZoneChanged () override
void onTimeZoneChanged () override
void onTimeZoneChanged () override
void onWrite (BLECharacteristic *chr) override
void onWrite (NimBLECharacteristic *chr, NimBLEConnInfo &) override
bool onWrite (uint16_t uuid, const uint8_t *data, size_t len) override
void updateCurrent (uint8_t reason)
void updateCurrent (uint8_t reason)
void updateCurrent (uint8_t reason, bool notify)
void updateLocal ()
void updateLocal ()
void updateLocal ()
void updateReference ()
void updateReference ()
void updateReference ()
Protected Member Functions inherited from tiny_cts::TinyCTSServerBase
size_t currentTimeValue (uint8_t reason, uint8_t *out)
size_t localTimeValue (uint8_t *out) const
bool notifyDue ()
size_t referenceTimeValue (uint8_t *out) const
bool setTimeFromWrite (const uint8_t *data, size_t len)
bool setTimeZoneFromWrite (const uint8_t *data, size_t len)

Static Protected Member Functions

static TinyCTSServer *& instance ()
static void onReadCurrent (BLEDevice, BLECharacteristic)
static void onWriteCurrent (BLEDevice, BLECharacteristic chr)
static void onWriteLocal (BLEDevice, BLECharacteristic chr)

Protected Attributes

std::string _adapterName = "hci0"
BLECharacteristic * _current = nullptr
NimBLECharacteristic * _current = nullptr
Value * _current = nullptr
std::string _error
BLECharacteristic * _local = nullptr
NimBLECharacteristic * _local = nullptr
Value * _local = nullptr
std::recursive_mutex _mutex
std::unique_ptr< TinyCTSPeripheral > _peripheral
BLECharacteristic * _reference = nullptr
NimBLECharacteristic * _reference = nullptr
Value * _reference = nullptr
uint32_t _requests = 0
BLEService * _service = nullptr
Protected Attributes inherited from tiny_cts::TinyCTSServerBase
bool _encrypted = true
TinyCTSLocalTimeInfo _info
uint32_t _lastNotify = 0
uint32_t _notifyMs = 0
TinyCTSReferenceTimeInfo _ref
uint32_t _setCount = 0
bool _settable = false
TinyCTSTimeSource * _source = nullptr
bool _timeIsLocal = false

Detailed Description

A BLE peripheral that offers the Current Time Service (0x1805) and serves the time of a time source to the BLE clients (phones, watches, boards with a TinyCTSClient).

A BLE peripheral that offers the Current Time Service (0x1805) and serves the time of a time source to the BLE clients (phones, watches, other boards running a TinyCTSClient, tinycts on the desktop).

This is the version for the desktop. The computer must have a Bluetooth adapter that can be a peripheral. Call process() in your loop: on Linux it handles the requests of BlueZ, on the other systems they are answered by a thread of the system, which also calls the time source. The settings are in TinyCTSServerBase. Only one server per process is possible.

The name of the device is only used on Linux and macOS: Windows advertises the name of the computer.

TinyCTSServer server;
server.setTimeZone(60, 0);
if (!server.begin(source, "my-computer")) fprintf(stderr, "%s\n", server.getLastError().c_str());
while (running) { server.process(); sleep_ms(10); }
server.end();
void setTimeZone(int utcOffsetMinutes, int dstMinutes=0)
The time zone that is offered in the Local Time Information.
Definition TinyCTSServerBase.h:39
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
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
The UTC time of the system clock (gettimeofday()). Not available on the boards without gettimeofday()...
Definition TinyCTSTimeSource.h:114

It provides the characteristics Current Time (read, notify), Local Time Information and Reference Time Information (read). While the time source returns 0 (time not known) the Current Time is reported with an unknown date.

begin() starts the BLE hardware (BLE.begin()). Call process() in loop(): it calls BLE.poll(), which handles the requests of the clients. Only one server per program is possible. The settings are in TinyCTSServerBase.

With enableTimeSetting() the writable characteristics need an encrypted connection: ArduinoBLE then asks for it also for reading them.

TinyCTSServer server;
void setup() {
clock.setTime(1767225600);
server.setTimeZone(60, 0); // CET
server.begin(clock, "my-clock");
}
void loop() { server.process(); }
A software clock: you set the UTC time (from an RTC, a GPS, ...) and it keeps counting with millis()....
Definition TinyCTSTimeSource.h:79

It provides the characteristics Current Time (read, notify), Local Time Information and Reference Time Information (read). While the time source returns 0 (time not known) the Current Time is reported with an unknown date.

Call begin() once and process() in loop() if you want periodic notifications (setNotifyInterval()). The settings are in TinyCTSServerBase.

TinyCTSServer server;
void setup() {
clock.setTime(rtc.now().unixtime());
server.setTimeZone(60, 60); // CET, summer time
server.begin(clock, "my-clock");
}
void loop() { server.process(); }

It provides the characteristics Current Time (read, notify), Local Time Information and Reference Time Information (read). While the time source returns 0 (time not known) the Current Time is reported with an unknown date.

Select "Tools -> IP/Bluetooth Stack -> IPv4 + Bluetooth". begin() starts the BLE hardware: do not call BLE.begin() yourself. The Bluetooth callbacks read the time source: it must be fast and not block. Call process() in loop() if you want periodic notifications (setNotifyInterval()). The settings are in TinyCTSServerBase.

TinyCTSServer server;
void setup() {
clock.setTime(1767225600);
server.setTimeZone(60, 0); // CET
server.begin(clock, "my-clock");
}
void loop() { server.process(); }

Member Function Documentation

◆ begin() [1/4]

bool tiny_cts::TinyCTSServer::begin ( TinyCTSTimeSource & source,
const char * deviceName = "TinyCTS" )
inline

Register the service and start the advertising.

Parameters
sourceThe time source. It must stay valid as long as the server is used.
deviceNameThe name of the BLE device.
Returns
true on success. The reason is in getLastError().

◆ begin() [2/4]

bool tiny_cts::TinyCTSServer::begin ( TinyCTSTimeSource & source,
const char * deviceName = "TinyCTS" )
inline

Start the BLE hardware, the service and the advertising.

Parameters
sourceThe time source. It must stay valid as long as the server is used.
deviceNameThe name of the BLE device.
Returns
true on success.

◆ begin() [3/4]

bool tiny_cts::TinyCTSServer::begin ( TinyCTSTimeSource & source,
const char * deviceName = "TinyCTS" )
inline

Start the service and the advertising. NimBLEDevice::init() is called with the name, if the NimBLE stack is not initialized yet.

Parameters
sourceThe time source. It must stay valid as long as the server is used.
deviceNameThe name of the BLE device.
Returns
true on success.

◆ begin() [4/4]

bool tiny_cts::TinyCTSServer::begin ( TinyCTSTimeSource & source,
const char * deviceName = "TinyCTS" )
inline

Start the BLE hardware, the service and the advertising.

Parameters
sourceThe time source. It must stay valid as long as the server is used.
deviceNameThe name of the BLE device.
Returns
true on success.

◆ onRead()

bool tiny_cts::TinyCTSServer::onRead ( uint16_t uuid,
std::vector< uint8_t > & value )
inlineoverrideprotectedvirtual

A client reads a characteristic: return its value.

Implements tiny_cts::TinyCTSPeripheralHandler.

◆ onReferenceChanged() [1/3]

void tiny_cts::TinyCTSServer::onReferenceChanged ( )
inlineoverrideprotectedvirtual

The Reference Time Information has changed.

Reimplemented from tiny_cts::TinyCTSServerBase.

◆ onReferenceChanged() [2/3]

void tiny_cts::TinyCTSServer::onReferenceChanged ( )
inlineoverrideprotectedvirtual

The Reference Time Information has changed.

Reimplemented from tiny_cts::TinyCTSServerBase.

◆ onReferenceChanged() [3/3]

void tiny_cts::TinyCTSServer::onReferenceChanged ( )
inlineoverrideprotectedvirtual

The Reference Time Information has changed.

Reimplemented from tiny_cts::TinyCTSServerBase.

◆ onTimeZoneChanged() [1/4]

void tiny_cts::TinyCTSServer::onTimeZoneChanged ( )
inlineoverrideprotectedvirtual

The Local Time Information has changed: update the characteristic.

Reimplemented from tiny_cts::TinyCTSServerBase.

◆ onTimeZoneChanged() [2/4]

void tiny_cts::TinyCTSServer::onTimeZoneChanged ( )
inlineoverrideprotectedvirtual

The Local Time Information has changed: update the characteristic.

Reimplemented from tiny_cts::TinyCTSServerBase.

◆ onTimeZoneChanged() [3/4]

void tiny_cts::TinyCTSServer::onTimeZoneChanged ( )
inlineoverrideprotectedvirtual

The Local Time Information has changed: update the characteristic.

Reimplemented from tiny_cts::TinyCTSServerBase.

◆ onTimeZoneChanged() [4/4]

void tiny_cts::TinyCTSServer::onTimeZoneChanged ( )
inlineoverrideprotectedvirtual

The Local Time Information has changed: update the characteristic.

Reimplemented from tiny_cts::TinyCTSServerBase.

◆ onWrite()

bool tiny_cts::TinyCTSServer::onWrite ( uint16_t uuid,
const uint8_t * data,
size_t len )
inlineoverrideprotectedvirtual

A client writes a characteristic: return false to refuse.

Implements tiny_cts::TinyCTSPeripheralHandler.

◆ process() [1/4]

bool tiny_cts::TinyCTSServer::process ( )
inline

Handle the requests of the clients and send the notifications. Call this regularly in loop().

Returns
true if the Current Time was notified.

◆ process() [2/4]

bool tiny_cts::TinyCTSServer::process ( )
inline

Call this regularly in loop(): it handles the requests of the clients and sends the notifications.

Returns
true if the Current Time was notified.

◆ process() [3/4]

bool tiny_cts::TinyCTSServer::process ( )
inline

Call this regularly in loop(): it sends the notifications.

Returns
true if the Current Time was notified.

◆ process() [4/4]

bool tiny_cts::TinyCTSServer::process ( )
inline

Call this regularly in loop(): it sends the notifications.

Returns
true if the Current Time was notified.

The documentation for this class was generated from the following files: