Coverage Report

Created: 2021-08-28 18:14

D:\git\skunkworks\herald-for-cpp\herald-tests\bledevice-tests.cpp
Line
Count
Source
1
//  Copyright 2020-2021 Herald Project Contributors
2
//  SPDX-License-Identifier: Apache-2.0
3
//
4
5
#include <memory>
6
#include <vector>
7
8
#include "catch.hpp"
9
10
#include "herald/herald.h"
11
12
#include "test-templates.h"
13
14
class DummyBLEDeviceDelegate : public herald::ble::BLEDeviceDelegate {
15
public:
16
10
  DummyBLEDeviceDelegate() : callbackCalled(false), dev(), attr() {};
17
10
  ~DummyBLEDeviceDelegate() = default;
18
19
  // overrides
20
5
  void device(const herald::ble::BLEDevice& device, const herald::ble::BLEDeviceAttribute didUpdate) override {
21
5
    callbackCalled = true;
22
5
    dev.emplace(std::reference_wrapper<const herald::ble::BLEDevice>(device));
23
5
    attr.emplace(didUpdate);
24
5
  }
25
26
  bool callbackCalled;
27
  std::optional<std::reference_wrapper<const herald::ble::BLEDevice>> dev;
28
  std::optional<herald::ble::BLEDeviceAttribute> attr;
29
};
30
31
1
TEST_CASE("ble-device-ctor", "[ble][device][ctor]") {
32
1
  SECTION("ble-device-ctor") {
33
1
    herald::datatype::Data d{std::byte(9),6};
34
1
    herald::datatype::TargetIdentifier id{d};
35
1
    DummyBLEDeviceDelegate delegate;
36
1
    herald::ble::BLESensorConfiguration conf;
37
1
    herald::ble::BLEDevice device(conf,id,delegate);
38
1
39
1
    REQUIRE(device.identifier() == id);
40
1
    REQUIRE(device.description().size() > 0);
41
1
    REQUIRE(((std::string)device).size() > 0);
42
1
43
1
    // REQUIRE(device.timeIntervalSinceConnected() == herald::datatype::TimeInterval::never());
44
1
    REQUIRE(device.timeIntervalSinceLastPayloadDataUpdate() == herald::datatype::TimeInterval::never());
45
1
    REQUIRE(device.timeIntervalSinceLastUpdate() == herald::datatype::TimeInterval::zero());
46
1
    // REQUIRE(device.timeIntervalSinceLastWritePayload() == herald::datatype::TimeInterval::never());
47
1
    // REQUIRE(device.timeIntervalSinceLastWritePayloadSharing() == herald::datatype::TimeInterval::never());
48
1
    // REQUIRE(device.timeIntervalSinceLastWriteRssi() == herald::datatype::TimeInterval::never());
49
1
    
50
1
    REQUIRE(device.state() == herald::ble::BLEDeviceState::uninitialised);
51
1
    REQUIRE(device.operatingSystem() == herald::ble::BLEDeviceOperatingSystem::unknown);
52
1
    REQUIRE(device.payloadData().size() == 0); // Uninitialised, to data must be empty
53
1
    // REQUIRE(!device.immediateSendData().has_value());
54
1
    REQUIRE(device.rssi() == 0); // Its not been 'seen' yet, so must be 0
55
1
  }
56
1
}
57
58
59
1
TEST_CASE("ble-device-update-state", "[ble][device][update][state]") {
60
1
  SECTION("ble-device-update-state") {
61
1
    herald::datatype::Data d{std::byte(9),6};
62
1
    herald::datatype::TargetIdentifier id{d};
63
1
    
64
1
    herald::datatype::Date now;
65
1
    herald::datatype::Date createdAt = now - herald::datatype::TimeInterval::minutes(1); // forces updated times to be greater than zero
66
1
67
1
    DummyBLEDeviceDelegate delegate;
68
1
    herald::ble::BLESensorConfiguration conf;
69
1
    herald::ble::BLEDevice device(conf,id,delegate,createdAt);
70
1
71
1
    herald::ble::BLEDeviceState s = herald::ble::BLEDeviceState::disconnected;
72
1
    device.state(s);
73
1
74
1
    REQUIRE(device.state() == s);
75
1
    REQUIRE(delegate.callbackCalled);
76
1
    const herald::ble::BLEDevice& dev = delegate.dev.value().get();
77
1
    REQUIRE(dev == device);
78
1
    REQUIRE(delegate.attr.value() == herald::ble::BLEDeviceAttribute::state);
79
1
80
1
    herald::datatype::TimeInterval lu = device.timeIntervalSinceLastUpdate();
81
1
    REQUIRE(lu >= herald::datatype::TimeInterval::seconds(0));
82
1
    REQUIRE(lu < herald::datatype::TimeInterval::never());
83
1
  }
84
1
}
85
86
1
TEST_CASE("ble-device-update-os", "[ble][device][update][os]") {
87
1
  SECTION("ble-device-update-os") {
88
1
    herald::datatype::Data d{std::byte(9),6};
89
1
    herald::datatype::TargetIdentifier id{d};
90
1
    
91
1
    herald::datatype::Date now;
92
1
    herald::datatype::Date createdAt = now - herald::datatype::TimeInterval::minutes(1); // forces updated times to be greater than zero
93
1
94
1
    DummyBLEDeviceDelegate delegate;
95
1
    herald::ble::BLESensorConfiguration conf;
96
1
    herald::ble::BLEDevice device(conf,id,delegate,createdAt);
97
1
98
1
    herald::ble::BLEDeviceOperatingSystem s = herald::ble::BLEDeviceOperatingSystem::android;
99
1
    device.operatingSystem(s);
100
1
101
1
    // actual value
102
1
    REQUIRE(device.operatingSystem() == s);
103
1
104
1
    // delegates
105
1
    REQUIRE(delegate.callbackCalled);
106
1
    const herald::ble::BLEDevice& dev = delegate.dev.value().get();
107
1
    REQUIRE(dev == device);
108
1
    REQUIRE(delegate.attr.value() == herald::ble::BLEDeviceAttribute::operatingSystem);
109
1
110
1
    herald::datatype::TimeInterval lu = device.timeIntervalSinceLastUpdate();
111
1
    REQUIRE(lu >= herald::datatype::TimeInterval::seconds(0));
112
1
    REQUIRE(lu < herald::datatype::TimeInterval::never());
113
1
  }
114
1
}
115
116
1
TEST_CASE("ble-device-update-payload", "[ble][device][update][payload]") {
117
1
  SECTION("ble-device-update-payload") {
118
1
    herald::datatype::Data d{std::byte(9),6};
119
1
    herald::datatype::TargetIdentifier id{d};
120
1
    
121
1
    herald::datatype::Date now;
122
1
    herald::datatype::Date createdAt = now - herald::datatype::TimeInterval::minutes(1); // forces updated times to be greater than zero
123
1
124
1
    DummyBLEDeviceDelegate delegate;
125
1
    herald::ble::BLESensorConfiguration conf;
126
1
    herald::ble::BLEDevice device(conf,id,delegate,createdAt);
127
1
128
1
    herald::payload::fixed::ConcreteFixedPayloadDataSupplierV1 pds(826,4,123123123);
129
1
130
1
    BlankDevice bd;
131
1
    herald::datatype::PayloadData payload = pds.payload(herald::datatype::PayloadTimestamp(),bd);
132
1
    device.payloadData(payload);
133
1
134
1
    // actual value
135
1
    REQUIRE(device.payloadData() == payload);
136
1
137
1
    // delegates
138
1
    REQUIRE(delegate.callbackCalled);
139
1
    const herald::ble::BLEDevice& dev = delegate.dev.value().get();
140
1
    REQUIRE(dev == device);
141
1
    REQUIRE(delegate.attr.value() == herald::ble::BLEDeviceAttribute::payloadData);
142
1
143
1
    herald::datatype::TimeInterval lu = device.timeIntervalSinceLastUpdate();
144
1
    REQUIRE(lu >= herald::datatype::TimeInterval::seconds(0));
145
1
    REQUIRE(lu < herald::datatype::TimeInterval::never());
146
1
    
147
1
    REQUIRE(device.timeIntervalSinceLastPayloadDataUpdate() >= herald::datatype::TimeInterval::seconds(0));
148
1
    REQUIRE(device.timeIntervalSinceLastPayloadDataUpdate() < herald::datatype::TimeInterval::never());
149
1
  }
150
1
}
151
152
// TEST_CASE("ble-device-update-immediatesenddata", "[ble][device][update][immediatesenddata]") {
153
//   SECTION("ble-device-update-immediatesenddata") {
154
//     herald::datatype::Data d{std::byte(9),6};
155
//     herald::datatype::TargetIdentifier id{d};
156
    
157
//     herald::datatype::Date now;
158
//     herald::datatype::Date createdAt = now - herald::datatype::TimeInterval::minutes(1); // forces updated times to be greater than zero
159
160
//     DummyBLEDeviceDelegate delegate;
161
//     herald::ble::BLESensorConfiguration conf;
162
//     herald::ble::BLEDevice device(conf,id,delegate,createdAt);
163
164
//     herald::datatype::Data raw{std::byte(9), 12};
165
//     herald::datatype::ImmediateSendData isd(raw);
166
//     device.immediateSendData(isd);
167
168
//     // actual value
169
//     REQUIRE(device.immediateSendData().has_value());
170
//     REQUIRE(device.immediateSendData().value() == isd);
171
172
//     // delegates
173
//     REQUIRE(delegate.callbackCalled);
174
//     const herald::ble::BLEDevice& dev = delegate.dev.value().get();
175
//     REQUIRE(dev == device);
176
//     REQUIRE(delegate.attr.value() == herald::ble::BLEDeviceAttribute::immediateSendData);
177
178
//     herald::datatype::TimeInterval lu = device.timeIntervalSinceLastUpdate();
179
//     REQUIRE(lu >= herald::datatype::TimeInterval::seconds(0));
180
//     REQUIRE(lu < herald::datatype::TimeInterval::never());
181
//   }
182
// }
183
184
1
TEST_CASE("ble-device-update-rssi", "[ble][device][update][rssi]") {
185
1
  SECTION("ble-device-update-rssi") {
186
1
    herald::datatype::Data d{std::byte(9),6};
187
1
    herald::datatype::TargetIdentifier id{d};
188
1
    
189
1
    herald::datatype::Date now;
190
1
    herald::datatype::Date createdAt = now - herald::datatype::TimeInterval::minutes(1); // forces updated times to be greater than zero
191
1
192
1
    DummyBLEDeviceDelegate delegate;
193
1
    herald::ble::BLESensorConfiguration conf;
194
1
    herald::ble::BLEDevice device(conf,id,delegate,createdAt);
195
1
196
1
    herald::datatype::RSSI rssi(12);
197
1
    device.rssi(rssi);
198
1
199
1
    // actual value
200
1
    // REQUIRE(device.rssi().has_value());
201
1
    REQUIRE(device.rssi() == rssi);
202
1
203
1
    // delegates
204
1
    REQUIRE(delegate.callbackCalled);
205
1
    const herald::ble::BLEDevice& dev = delegate.dev.value().get();
206
1
    REQUIRE(dev == device);
207
1
    REQUIRE(delegate.attr.value() == herald::ble::BLEDeviceAttribute::rssi);
208
1
209
1
    herald::datatype::TimeInterval lu = device.timeIntervalSinceLastUpdate();
210
1
    REQUIRE(lu >= herald::datatype::TimeInterval::seconds(0));
211
1
    REQUIRE(lu < herald::datatype::TimeInterval::never());
212
1
  }
213
1
}
214
215
1
TEST_CASE("ble-device-update-txpower", "[ble][device][update][txpower]") {
216
1
  SECTION("ble-device-update-txpower") {
217
1
    herald::datatype::Data d{std::byte(9),6};
218
1
    herald::datatype::TargetIdentifier id{d};
219
1
    
220
1
    herald::datatype::Date now;
221
1
    herald::datatype::Date createdAt = now - herald::datatype::TimeInterval::minutes(1); // forces updated times to be greater than zero
222
1
223
1
    DummyBLEDeviceDelegate delegate;
224
1
    herald::ble::BLESensorConfiguration conf;
225
1
    herald::ble::BLEDevice device(conf,id,delegate,createdAt);
226
1
227
1
    herald::ble::BLETxPower tx(12);
228
1
    device.txPower(tx);
229
1
230
1
    // actual value
231
1
    REQUIRE(device.txPower().has_value());
232
1
    REQUIRE(device.txPower().value() == tx);
233
1
234
1
    // delegates
235
1
    REQUIRE(delegate.callbackCalled);
236
1
    const herald::ble::BLEDevice& dev = delegate.dev.value().get();
237
1
    REQUIRE(dev == device);
238
1
    REQUIRE(delegate.attr.value() == herald::ble::BLEDeviceAttribute::txPower);
239
1
240
1
    herald::datatype::TimeInterval lu = device.timeIntervalSinceLastUpdate();
241
1
    REQUIRE(lu >= herald::datatype::TimeInterval::seconds(0));
242
1
    REQUIRE(lu < herald::datatype::TimeInterval::never());
243
1
  }
244
1
}
245
246
1
TEST_CASE("ble-device-update-pseudo", "[ble][device][update][pseudo]") {
247
1
  SECTION("ble-device-update-pseudo") {
248
1
    herald::datatype::Data d{std::byte(9),6};
249
1
    herald::datatype::TargetIdentifier id{d};
250
1
    
251
1
    herald::datatype::Date now;
252
1
    herald::datatype::Date createdAt = now - herald::datatype::TimeInterval::minutes(1); // forces updated times to be greater than zero
253
1
254
1
    DummyBLEDeviceDelegate delegate;
255
1
    herald::ble::BLESensorConfiguration conf;
256
1
    herald::ble::BLEDevice device(conf,id,delegate,createdAt);
257
1
258
1
    herald::datatype::Data pseudod{std::byte(7),6};
259
1
    herald::ble::BLEMacAddress pseudo(pseudod);
260
1
    device.pseudoDeviceAddress(pseudo);
261
1
262
1
    // actual value
263
1
    REQUIRE(device.pseudoDeviceAddress().has_value());
264
1
    REQUIRE(device.pseudoDeviceAddress().value() == pseudo);
265
1
266
1
    // delegates
267
1
    REQUIRE(!delegate.callbackCalled);
268
1
269
1
    herald::datatype::TimeInterval lu = device.timeIntervalSinceLastUpdate();
270
1
    REQUIRE(lu != herald::datatype::TimeInterval::never()); // pseudo address counts as update
271
1
  }
272
1
}
273
274
// TEST_CASE("ble-device-update-receiveOnly", "[ble][device][update][receiveOnly]") {
275
//   SECTION("ble-device-update-receiveOnly") {
276
//     herald::datatype::Data d{std::byte(9),6};
277
//     herald::datatype::TargetIdentifier id{d};
278
    
279
//     herald::datatype::Date now;
280
//     herald::datatype::Date createdAt = now - herald::datatype::TimeInterval::minutes(1); // forces updated times to be greater than zero
281
282
//     DummyBLEDeviceDelegate delegate;
283
//     herald::ble::BLESensorConfiguration conf;
284
//     herald::ble::BLEDevice device(conf,id,delegate,createdAt);
285
286
//     REQUIRE(device.receiveOnly() == false); // default - should default to try both read and write
287
//     device.receiveOnly(true);
288
289
//     // actual value
290
//     REQUIRE(device.receiveOnly() == true);
291
292
//     // delegates
293
//     REQUIRE(!delegate.callbackCalled);
294
295
//     herald::datatype::TimeInterval lu = device.timeIntervalSinceLastUpdate();
296
//     REQUIRE(lu == herald::datatype::TimeInterval::zero());
297
//   }
298
// }
299
300
1
TEST_CASE("ble-device-update-ignore", "[ble][device][update][ignore]") {
301
1
  SECTION("ble-device-update-ignore") {
302
1
    herald::datatype::Data d{std::byte(9),6};
303
1
    herald::datatype::TargetIdentifier id{d};
304
1
    
305
1
    herald::datatype::Date now;
306
1
    herald::datatype::Date createdAt = now - herald::datatype::TimeInterval::minutes(1); // forces updated times to be greater than zero
307
1
308
1
    DummyBLEDeviceDelegate delegate;
309
1
    herald::ble::BLESensorConfiguration conf;
310
1
    herald::ble::BLEDevice device(conf,id,delegate,createdAt);
311
1
312
1
    REQUIRE(device.ignore() == false); // default - should default to try both read and write
313
1
    device.ignore(true);
314
1
315
1
    // actual value
316
1
    REQUIRE(device.ignore() == true);
317
1
318
1
    // delegates
319
1
    REQUIRE(!delegate.callbackCalled);
320
1
321
1
    herald::datatype::TimeInterval lu = device.timeIntervalSinceLastUpdate();
322
1
    REQUIRE(lu == herald::datatype::TimeInterval::zero());
323
1
  }
324
1
}
325
326
1
TEST_CASE("ble-device-update-payloadchar", "[ble][device][update][payloadchar]") {
327
1
  SECTION("ble-device-update-payloadchar") {
328
1
    herald::datatype::Data d{std::byte(9),6};
329
1
    herald::datatype::TargetIdentifier id{d};
330
1
    
331
1
    herald::datatype::Date now;
332
1
    herald::datatype::Date createdAt = now - herald::datatype::TimeInterval::minutes(1); // forces updated times to be greater than zero
333
1
334
1
    DummyBLEDeviceDelegate delegate;
335
1
    herald::ble::BLESensorConfiguration conf;
336
1
    herald::ble::BLEDevice device(conf,id,delegate,createdAt);
337
1
338
1
    herald::datatype::IntegerDistributedRandomSource irds;
339
1
    herald::datatype::RandomnessGenerator<herald::datatype::IntegerDistributedRandomSource> gen(
340
1
      std::move(irds)
341
1
    );
342
1
    herald::datatype::UUID uuid = herald::datatype::UUID::random(gen);
343
1
344
1
    device.payloadCharacteristic(uuid);
345
1
346
1
    // actual value
347
1
    // REQUIRE(device.payloadCharacteristic().has_value() == true); // Only true if a supported value. Test one isnt.
348
1
    // REQUIRE(device.payloadCharacteristic().value() == uuid);
349
1
350
1
    // delegates
351
1
    REQUIRE(!delegate.callbackCalled);
352
1
353
1
    herald::datatype::TimeInterval lu = device.timeIntervalSinceLastUpdate();
354
1
    REQUIRE(lu == herald::datatype::TimeInterval::zero());
355
1
  }
356
1
}
357
358
// TODO refactor signalchar test as we now only support a subset of UUIDs not arbitrary ones
359
// TEST_CASE("ble-device-update-signalchar", "[ble][device][update][signalchar]") {
360
//   SECTION("ble-device-update-signalchar") {
361
//     herald::datatype::Data d{std::byte(9),6};
362
//     herald::datatype::TargetIdentifier id{d};
363
    
364
//     herald::datatype::Date now;
365
//     herald::datatype::Date createdAt = now - herald::datatype::TimeInterval::minutes(1); // forces updated times to be greater than zero
366
367
//     DummyBLEDeviceDelegate delegate;
368
//     herald::ble::BLESensorConfiguration conf;
369
//     herald::ble::BLEDevice device(conf,id,delegate,createdAt);
370
371
//     herald::datatype::IntegerDistributedRandomSource irds;
372
//     herald::datatype::RandomnessGenerator<herald::datatype::IntegerDistributedRandomSource> gen(
373
//       std::move(irds)
374
//     );
375
//     herald::datatype::UUID uuid = herald::datatype::UUID::random(gen);
376
377
//     device.signalCharacteristic(uuid);
378
379
//     // actual value
380
//     REQUIRE(device.signalCharacteristic().has_value() == true);
381
//     REQUIRE(device.signalCharacteristic().value() == uuid);
382
383
//     // delegates
384
//     REQUIRE(!delegate.callbackCalled);
385
386
//     herald::datatype::TimeInterval lu = device.timeIntervalSinceLastUpdate();
387
//     REQUIRE(lu == herald::datatype::TimeInterval::zero());
388
//   }
389
// }
390
391
1
TEST_CASE("ble-device-invalidate-chars", "[ble][device][update][invalidatechars]") {
392
1
  SECTION("ble-device-invalidate-chars") {
393
1
    herald::datatype::Data d{std::byte(9),6};
394
1
    herald::datatype::TargetIdentifier id{d};
395
1
    
396
1
    herald::datatype::Date now;
397
1
    herald::datatype::Date createdAt = now - herald::datatype::TimeInterval::minutes(1); // forces updated times to be greater than zero
398
1
399
1
    DummyBLEDeviceDelegate delegate;
400
1
    herald::ble::BLESensorConfiguration conf;
401
1
    herald::ble::BLEDevice device(conf,id,delegate,createdAt);
402
1
403
1
    herald::datatype::IntegerDistributedRandomSource irds;
404
1
    herald::datatype::RandomnessGenerator<herald::datatype::IntegerDistributedRandomSource> gen(
405
1
      std::move(irds)
406
1
    );
407
1
    herald::datatype::UUID uuids = herald::datatype::UUID::random(gen);
408
1
    herald::datatype::UUID uuidp = herald::datatype::UUID::random(gen);
409
1
410
1
    device.signalCharacteristic(uuids);
411
1
    device.payloadCharacteristic(uuidp);
412
1
413
1
    // actual value
414
1
    REQUIRE(device.signalCharacteristic().has_value() == true);
415
1
    // REQUIRE(device.signalCharacteristic().value() == uuids);
416
1
    // REQUIRE(device.payloadCharacteristic().has_value() == true); // only true if a supported value (test one isnt)
417
1
    // REQUIRE(device.payloadCharacteristic().value() == uuidp);
418
1
419
1
    // clear
420
1
    device.invalidateCharacteristics();
421
1
422
1
    // recheck
423
1
    // REQUIRE(device.signalCharacteristic().has_value() == false);
424
1
    REQUIRE(device.payloadCharacteristic().has_value() == false);
425
1
426
1
    // delegates
427
1
    REQUIRE(!delegate.callbackCalled);
428
1
429
1
    herald::datatype::TimeInterval lu = device.timeIntervalSinceLastUpdate();
430
1
    REQUIRE(lu == herald::datatype::TimeInterval::zero());
431
1
  }
432
1
}
433
434
435
436
// TEST_CASE("ble-device-register-rssi", "[ble][device][register][rssi]") {
437
//   SECTION("ble-device-register-rssi") {
438
//     herald::datatype::Data d{std::byte(9),6};
439
//     herald::datatype::TargetIdentifier id{d};
440
    
441
//     herald::datatype::Date now;
442
//     herald::datatype::Date createdAt = now - herald::datatype::TimeInterval::minutes(1); // forces updated times to be greater than zero
443
444
//     DummyBLEDeviceDelegate delegate;
445
//     herald::ble::BLESensorConfiguration conf;
446
//     herald::ble::BLEDevice device(conf,id,delegate,createdAt);
447
448
//     herald::datatype::Date operationAt = now - herald::datatype::TimeInterval::seconds(30);
449
450
//     device.registerWriteRssi(operationAt);
451
452
//     // actual value
453
//     REQUIRE(device.timeIntervalSinceLastWriteRssi() >= herald::datatype::TimeInterval::seconds(30));
454
//     REQUIRE(device.timeIntervalSinceLastWriteRssi() < herald::datatype::TimeInterval::never());
455
456
//     // delegates
457
//     REQUIRE(!delegate.callbackCalled);
458
459
//     herald::datatype::TimeInterval lu = device.timeIntervalSinceLastUpdate();
460
//     REQUIRE(lu >= herald::datatype::TimeInterval::seconds(0));
461
//     REQUIRE(lu < herald::datatype::TimeInterval::never());
462
//   }
463
// }