TinyCTS
Bluetooth Current Time Service client and server for Arduino
Loading...
Searching...
No Matches
TinyCTSClientPico.h
Go to the documentation of this file.
1
7
8#pragma once
10#include "TinyCTSPicoBLE.h"
11
12namespace tiny_cts {
13
34class TinyCTSClient : public TinyCTSClientBase {
35 public:
41 explicit TinyCTSClient(const char* device = nullptr, uint32_t scanMs = 5000)
42 : _scanMs(scanMs) {
43 if (device != nullptr) _device = device;
44 }
45
50 bool begin() {
51 if (!ctsPicoBegin("", false)) return false;
52 return update();
53 }
54
60 bool update() {
61 BLEClient* client = connect();
62 if (client == nullptr) return false;
63 bool ok = false;
64 BLERemoteService* service = client->service(BLEUUID(CTS_SERVICE_UUID));
65 if (service != nullptr) {
66 uint8_t data[16];
67 size_t len;
68 if (read(service->characteristic(BLEUUID(CTS_LOCAL_TIME_INFO_UUID)), data, len)) {
69 storeLocalInfo(data, len);
70 }
71 if (read(service->characteristic(BLEUUID(CTS_REFERENCE_TIME_INFO_UUID)), data, len)) {
72 storeReferenceInfo(data, len);
73 }
74 BLERemoteCharacteristic* chr = service->characteristic(BLEUUID(CTS_CURRENT_TIME_UUID));
75 uint32_t t0 = TINY_CTS_MILLIS();
76 if (read(chr, data, len)) {
77 ok = storeCurrentTime(data, len, t0, TINY_CTS_MILLIS());
78 }
79 }
80 client->disconnect();
81 return ok;
82 }
83
89 bool setTimeMs(uint64_t unixMs) override {
90 BLEClient* client = connect();
91 if (client == nullptr) return false;
92 bool ok = false;
93 BLERemoteService* service = client->service(BLEUUID(CTS_SERVICE_UUID));
94 BLERemoteCharacteristic* chr =
95 service != nullptr ? service->characteristic(BLEUUID(CTS_CURRENT_TIME_UUID)) : nullptr;
96 if (chr != nullptr && chr->canWrite()) {
97 uint8_t data[TinyCTSCurrentTime::Size];
98 ok = chr->setValue(data, timeToWrite(unixMs, data));
99 }
100 client->disconnect();
101 return ok;
102 }
103
104 protected:
105 BLEClient* connect() {
106 uint32_t seconds = (_scanMs + 999) / 1000;
107 if (seconds == 0) seconds = 1;
108 // the scan with the UUID keeps the devices that advertise the service
109 BLEScanReport* report =
110 _device.empty() ? BLE.scan(BLEUUID(CTS_SERVICE_UUID), seconds) : BLE.scan(seconds);
111 BLEClient* client = BLE.client();
112 bool connected = false;
113 for (auto& item : *report) {
114 std::string address = item.getAddress().toString().c_str();
115 std::string name = item.getName();
116 if (!_device.empty() && !isWanted(address, name)) continue;
117 connected = client->connect(item, 10);
118 if (connected) {
119 _address = address;
120 _name = name;
121 }
122 break;
123 }
124 BLE.clearScan();
125 return connected ? client : nullptr;
126 }
127
130 static bool read(BLERemoteCharacteristic* chr, uint8_t* out, size_t& len) {
131 if (chr == nullptr || !chr->canRead()) return false;
132 chr->getUInt8();
133 const void* value = chr->valueData();
134 len = chr->valueLen();
135 if (value == nullptr || len == 0) return false;
136 if (len > 16) len = 16;
137 memcpy(out, value, len);
138 return true;
139 }
140
141 uint32_t _scanMs;
142};
143
144} // namespace tiny_cts
The logic of the Current Time Service client, independent of the Bluetooth stack: decoding of the cha...
Starts the BLE stack of the Raspberry Pi Pico (W) core once, for the client and the server.
bool ctsPicoBegin(const char *name, bool pairing)
Definition TinyCTSPicoBLE.h:20
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_SERVICE_UUID
16 bit UUID of the Current Time Service
Definition TinyCTSTypes.h:17
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 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 isWanted(const std::string &address, const std::string &name) const
Definition TinyCTSClientBase.h:117
bool storeCurrentTime(const uint8_t *data, size_t len, uint32_t startMs, uint32_t endMs)
Definition TinyCTSClientBase.h:91
Reads the time from a BLE device that offers the Current Time Service (a phone, a watch,...
Definition TinyCTSClientDesktop.h:30
bool begin()
Start the BLE hardware, find the device and read the time.
Definition TinyCTSClientPico.h:50
TinyCTSClient(const char *device=nullptr, uint32_t scanMs=5000)
Definition TinyCTSClientPico.h:41
bool setTimeMs(uint64_t unixMs) override
Set the time of the device by writing the Current Time. It works if the device allows it (and the con...
Definition TinyCTSClientPico.h:89
bool update()
Read the time (and the other characteristics) again: the device is connected, read and disconnected.
Definition TinyCTSClientDesktop.h:58
static bool read(BLERemoteCharacteristic *chr, uint8_t *out, size_t &len)
Definition TinyCTSClientPico.h:130
static constexpr size_t Size
Size of the encoded characteristic.
Definition TinyCTSTypes.h:70