simplersble 0.11.0-dev36

The all-in-one Bluetooth library that makes it easy to add wireless connectivity to your projects.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
#include <simpledbus/base/Holder.h>
#include <iomanip>
#include <sstream>

#include "dbus/dbus-protocol.h"

using namespace SimpleDBus;

Holder::Holder() {}

Holder::~Holder() {}

bool Holder::operator!=(const Holder& other) const { return !(*this == other); }

bool Holder::operator==(const Holder& other) const {
    if (type() != other.type()) {
        return false;
    }

    switch (type()) {
        case NONE:
            return true;
        case BYTE:
            return get_byte() == other.get_byte();
        case BOOLEAN:
            return get_boolean() == other.get_boolean();
        case INT16:
            return get_int16() == other.get_int16();
        case UINT16:
            return get_uint16() == other.get_uint16();
        case INT32:
            return get_int32() == other.get_int32();
        case UINT32:
            return get_uint32() == other.get_uint32();
        case INT64:
            return get_int64() == other.get_int64();
        case UINT64:
            return get_uint64() == other.get_uint64();
        case DOUBLE:
            return get_double() == other.get_double();
        case STRING:
            return get_string() == other.get_string();
        case OBJ_PATH:
            return get_object_path() == other.get_object_path();
        case SIGNATURE:
            return get_signature() == other.get_signature();
        case ARRAY:
            return get_array() == other.get_array();
        case DICT:
            return (get_dict_uint8() == other.get_dict_uint8()) && (get_dict_uint16() == other.get_dict_uint16()) &&
                   (get_dict_int16() == other.get_dict_int16()) && (get_dict_uint32() == other.get_dict_uint32()) &&
                   (get_dict_int32() == other.get_dict_int32()) && (get_dict_uint64() == other.get_dict_uint64()) &&
                   (get_dict_int64() == other.get_dict_int64()) && (get_dict_string() == other.get_dict_string()) &&
                   (get_dict_object_path() == other.get_dict_object_path()) &&
                   (get_dict_signature() == other.get_dict_signature());
        default:
            return false;
    }
}

Holder::Type Holder::type() const { return _type; }

std::string Holder::_represent_simple() const {
    std::ostringstream output;
    output << std::boolalpha;

    switch (_type) {
        case BOOLEAN:
            output << get_boolean();
            break;
        case BYTE:
            output << (int)get_byte();
            break;
        case INT16:
            output << (int)get_int16();
            break;
        case UINT16:
            output << (int)get_uint16();
            break;
        case INT32:
            output << get_int32();
            break;
        case UINT32:
            output << get_uint32();
            break;
        case INT64:
            output << get_int64();
            break;
        case UINT64:
            output << get_uint64();
            break;
        case DOUBLE:
            output << get_double();
            break;
        case STRING:
        case OBJ_PATH:
        case SIGNATURE:
            output << get_string();
            break;
    }
    return output.str();
}

std::vector<std::string> Holder::_represent_container() const {
    std::vector<std::string> output_lines;
    switch (_type) {
        case BOOLEAN:
        case BYTE:
        case INT16:
        case UINT16:
        case INT32:
        case UINT32:
        case INT64:
        case UINT64:
        case DOUBLE:
        case STRING:
        case OBJ_PATH:
        case SIGNATURE:
            output_lines.push_back(_represent_simple());
            break;
        case ARRAY: {
            output_lines.push_back("Array:");
            std::vector<std::string> additional_lines;
            if (holder_array.size() > 0 && holder_array[0]._type == BYTE) {
                // Dealing with an array of bytes, use custom print functionality.
                std::string temp_line = "";
                for (int i = 0; i < holder_array.size(); i++) {
                    // Represent each byte as a hex string
                    std::stringstream stream;
                    stream << std::setfill('0') << std::setw(2) << std::hex << ((int)holder_array[i].get_byte());
                    temp_line += (stream.str() + " ");
                    if ((i + 1) % 32 == 0) {
                        additional_lines.push_back(temp_line);
                        temp_line = "";
                    }
                }
                additional_lines.push_back(temp_line);
            } else {
                for (int i = 0; i < holder_array.size(); i++) {
                    for (auto& line : holder_array[i]._represent_container()) {
                        additional_lines.push_back(line);
                    }
                }
            }
            for (auto& line : additional_lines) {
                output_lines.push_back("  " + line);
            }
            break;
        }
        case DICT:
            output_lines.push_back("Dictionary:");
            for (auto& [key_type_internal, key, value] : holder_dict) {
                output_lines.push_back(_represent_type(key_type_internal, key) + ":");
                auto additional_lines = value._represent_container();
                for (auto& line : additional_lines) {
                    output_lines.push_back("  " + line);
                }
            }
            break;
    }
    return output_lines;
}

std::string Holder::represent() const {
    std::ostringstream output;
    auto output_lines = _represent_container();
    for (auto& output_line : output_lines) {
        output << output_line << std::endl;
    }
    return output.str();
}

std::string Holder::_signature_simple() const {
    switch (_type) {
        case BOOLEAN:
            return DBUS_TYPE_BOOLEAN_AS_STRING;
        case BYTE:
            return DBUS_TYPE_BYTE_AS_STRING;
        case INT16:
            return DBUS_TYPE_INT16_AS_STRING;
        case UINT16:
            return DBUS_TYPE_UINT16_AS_STRING;
        case INT32:
            return DBUS_TYPE_INT32_AS_STRING;
        case UINT32:
            return DBUS_TYPE_UINT32_AS_STRING;
        case INT64:
            return DBUS_TYPE_INT64_AS_STRING;
        case UINT64:
            return DBUS_TYPE_UINT64_AS_STRING;
        case DOUBLE:
            return DBUS_TYPE_DOUBLE_AS_STRING;
        case STRING:
            return DBUS_TYPE_STRING_AS_STRING;
        case OBJ_PATH:
            return DBUS_TYPE_OBJECT_PATH_AS_STRING;
        case SIGNATURE:
            return DBUS_TYPE_SIGNATURE_AS_STRING;
    }
    return "";
}

std::string Holder::_signature_type(Type type) noexcept {
    switch (type) {
        case BOOLEAN:
            return DBUS_TYPE_BOOLEAN_AS_STRING;
        case BYTE:
            return DBUS_TYPE_BYTE_AS_STRING;
        case INT16:
            return DBUS_TYPE_INT16_AS_STRING;
        case UINT16:
            return DBUS_TYPE_UINT16_AS_STRING;
        case INT32:
            return DBUS_TYPE_INT32_AS_STRING;
        case UINT32:
            return DBUS_TYPE_UINT32_AS_STRING;
        case INT64:
            return DBUS_TYPE_INT64_AS_STRING;
        case UINT64:
            return DBUS_TYPE_UINT64_AS_STRING;
        case DOUBLE:
            return DBUS_TYPE_DOUBLE_AS_STRING;
        case STRING:
            return DBUS_TYPE_STRING_AS_STRING;
        case OBJ_PATH:
            return DBUS_TYPE_OBJECT_PATH_AS_STRING;
        case SIGNATURE:
            return DBUS_TYPE_SIGNATURE_AS_STRING;
    }
    return "";
}

std::string Holder::_represent_type(Type type, std::any value) noexcept {
    std::ostringstream output;
    output << std::boolalpha;

    switch (type) {
        case BOOLEAN:
            output << std::any_cast<bool>(value);
            break;
        case BYTE:
            output << std::any_cast<uint8_t>(value);
            break;
        case INT16:
            output << std::any_cast<int16_t>(value);
            break;
        case UINT16:
            output << std::any_cast<uint16_t>(value);
            break;
        case INT32:
            output << std::any_cast<int32_t>(value);
            break;
        case UINT32:
            output << std::any_cast<uint32_t>(value);
            break;
        case INT64:
            output << std::any_cast<int64_t>(value);
            break;
        case UINT64:
            output << std::any_cast<uint64_t>(value);
            break;
        case DOUBLE:
            output << std::any_cast<double>(value);
            break;
        case STRING:
        case OBJ_PATH:
        case SIGNATURE:
            output << std::any_cast<std::string>(value);
            break;
    }
    return output.str();
}

void Holder::signature_override(const std::string& signature) {
    // TODO: Check that the signature is valid for the Holder type and contents.
    _signature = signature;
}

std::string Holder::signature() const {
    if (_signature) {
        return *_signature;
    }

    std::string output;
    switch (_type) {
        case BOOLEAN:
        case BYTE:
        case INT16:
        case UINT16:
        case INT32:
        case UINT32:
        case INT64:
        case UINT64:
        case DOUBLE:
        case STRING:
        case OBJ_PATH:
        case SIGNATURE:
            output = _signature_simple();
            break;
        case ARRAY:
            output = DBUS_TYPE_ARRAY_AS_STRING;
            if (holder_array.size() == 0) {
                output += DBUS_TYPE_VARIANT_AS_STRING;
            } else {
                // Check if all elements of holder_array are the same type
                auto first_type = holder_array[0]._type;
                bool all_same_type = true;
                for (auto& element : holder_array) {
                    if (element._type != first_type) {
                        all_same_type = false;
                        break;
                    }
                }

                if (all_same_type) {
                    output += holder_array[0]._signature_simple();
                } else {
                    output += DBUS_TYPE_VARIANT_AS_STRING;
                }
            }
            break;
        case DICT:
            output = DBUS_TYPE_ARRAY_AS_STRING;
            output += DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING;

            if (holder_dict.size() == 0) {
                output += DBUS_TYPE_STRING_AS_STRING;
                output += DBUS_TYPE_VARIANT_AS_STRING;
            } else {
                // Check if all keys of holder_dict are the same type
                auto first_key_type = std::get<0>(holder_dict[0]);
                bool all_same_key_type = true;
                for (auto& [key_type_internal, key, value] : holder_dict) {
                    if (key_type_internal != first_key_type) {
                        all_same_key_type = false;
                        break;
                    }
                }
                if (all_same_key_type) {
                    output += _signature_type(first_key_type);
                } else {
                    output += DBUS_TYPE_VARIANT_AS_STRING;
                }

                // Check if all values of holder_dict are the same type
                auto first_value_type = std::get<2>(holder_dict[0])._type;
                bool all_same_value_type = true;
                for (auto& [key_type_internal, key, value] : holder_dict) {
                    if (value._type != first_value_type) {
                        all_same_value_type = false;
                        break;
                    }
                }

                if (all_same_value_type && first_value_type != ARRAY && first_value_type != DICT) {
                    output += std::get<2>(holder_dict[0])._signature_simple();
                } else {
                    output += DBUS_TYPE_VARIANT_AS_STRING;
                }
            }

            output += DBUS_DICT_ENTRY_END_CHAR_AS_STRING;
            break;
    }
    return output;
}

Holder Holder::create_byte(uint8_t value) {
    Holder h;
    h._type = BYTE;
    h.holder_integer = value;
    return h;
}
Holder Holder::create_boolean(bool value) {
    Holder h;
    h._type = BOOLEAN;
    h.holder_boolean = value;
    return h;
}
Holder Holder::create_int16(int16_t value) {
    Holder h;
    h._type = INT16;
    h.holder_integer = value;
    return h;
}
Holder Holder::create_uint16(uint16_t value) {
    Holder h;
    h._type = UINT16;
    h.holder_integer = value;
    return h;
}
Holder Holder::create_int32(int32_t value) {
    Holder h;
    h._type = INT32;
    h.holder_integer = value;
    return h;
}
Holder Holder::create_uint32(uint32_t value) {
    Holder h;
    h._type = UINT32;
    h.holder_integer = value;
    return h;
}
Holder Holder::create_int64(int64_t value) {
    Holder h;
    h._type = INT64;
    h.holder_integer = value;
    return h;
}
Holder Holder::create_uint64(uint64_t value) {
    Holder h;
    h._type = UINT64;
    h.holder_integer = value;
    return h;
}
Holder Holder::create_double(double value) {
    Holder h;
    h._type = DOUBLE;
    h.holder_double = value;
    return h;
}
Holder Holder::create_string(const std::string& str) {
    Holder h;
    h._type = STRING;
    h.holder_string = str;
    return h;
}
Holder Holder::create_object_path(const ObjectPath& path) {
    Holder h;
    h._type = OBJ_PATH;
    h.holder_string = path;
    return h;
}
Holder Holder::create_signature(const Signature& signature) {
    Holder h;
    h._type = SIGNATURE;
    h.holder_string = signature;
    return h;
}
Holder Holder::create_array() {
    Holder h;
    h._type = ARRAY;
    h.holder_array.clear();
    return h;
}
Holder Holder::create_dict() {
    Holder h;
    h._type = DICT;
    h.holder_dict.clear();
    return h;
}

template <>
Holder Holder::create(bool value) {
    return create_boolean(value);
}

template <>
Holder Holder::create(uint8_t value) {
    return create_byte(value);
}

template <>
Holder Holder::create(int16_t value) {
    return create_int16(value);
}

template <>
Holder Holder::create(uint16_t value) {
    return create_uint16(value);
}

template <>
Holder Holder::create(int32_t value) {
    return create_int32(value);
}

template <>
Holder Holder::create(uint32_t value) {
    return create_uint32(value);
}

template <>
Holder Holder::create(int64_t value) {
    return create_int64(value);
}

template <>
Holder Holder::create(uint64_t value) {
    return create_uint64(value);
}

template <>
Holder Holder::create(double value) {
    return create_double(value);
}

template <>
Holder Holder::create(std::string value) {
    return create_string(value);
}

template <>
Holder Holder::create(ObjectPath value) {
    return create_object_path(value);
}

template <>
Holder Holder::create(Signature value) {
    return create_signature(value);
}

template <>
Holder Holder::create<std::vector<Holder>>() {
    return create_array();
}

template <>
Holder Holder::create<std::map<std::string, Holder>>() {
    return create_dict();
}

std::any Holder::get_contents() const {
    // Only return the contents for simple types
    switch (_type) {
        case BOOLEAN:
            return get_boolean();
        case BYTE:
            return get_byte();
        case INT16:
            return get_int16();
        case UINT16:
            return get_uint16();
        case INT32:
            return get_int32();
        case UINT32:
            return get_uint32();
        case INT64:
            return get_int64();
        case UINT64:
            return get_uint64();
        case DOUBLE:
            return get_double();
        case STRING:
            return get_string();
        case OBJ_PATH:
            return get_object_path();
        case SIGNATURE:
            return get_signature();
        default:
            return std::any();
    }
}

bool Holder::get_boolean() const { return holder_boolean; }

uint8_t Holder::get_byte() const { return (uint8_t)(holder_integer & 0x00000000000000FFL); }

int16_t Holder::get_int16() const { return (int16_t)(holder_integer & 0x000000000000FFFFL); }

uint16_t Holder::get_uint16() const { return (uint16_t)(holder_integer & 0x000000000000FFFFL); }

int32_t Holder::get_int32() const { return (int32_t)(holder_integer & 0x00000000FFFFFFFFL); }

uint32_t Holder::get_uint32() const { return (uint32_t)(holder_integer & 0x00000000FFFFFFFFL); }

int64_t Holder::get_int64() const { return (int64_t)holder_integer; }

uint64_t Holder::get_uint64() const { return holder_integer; }

double Holder::get_double() const { return holder_double; }

std::string Holder::get_string() const { return holder_string; }

std::string Holder::get_object_path() const { return holder_string; }

std::string Holder::get_signature() const { return holder_string; }

std::vector<Holder> Holder::get_array() const { return holder_array; }

std::map<uint8_t, Holder> Holder::get_dict_uint8() const { return _get_dict<uint8_t>(BYTE); }

std::map<uint16_t, Holder> Holder::get_dict_uint16() const { return _get_dict<uint16_t>(UINT16); }

std::map<uint32_t, Holder> Holder::get_dict_uint32() const { return _get_dict<uint32_t>(UINT32); }

std::map<uint64_t, Holder> Holder::get_dict_uint64() const { return _get_dict<uint64_t>(UINT64); }

std::map<int16_t, Holder> Holder::get_dict_int16() const { return _get_dict<int16_t>(INT16); }

std::map<int32_t, Holder> Holder::get_dict_int32() const { return _get_dict<int32_t>(INT32); }

std::map<int64_t, Holder> Holder::get_dict_int64() const { return _get_dict<int64_t>(INT64); }

std::map<std::string, Holder> Holder::get_dict_string() const { return _get_dict<std::string>(STRING); }

std::map<std::string, Holder> Holder::get_dict_object_path() const { return _get_dict<std::string>(OBJ_PATH); }

std::map<std::string, Holder> Holder::get_dict_signature() const { return _get_dict<std::string>(SIGNATURE); }

template <>
bool Holder::get() const {
    return get_boolean();
}

template <>
uint8_t Holder::get() const {
    return get_byte();
}

template <>
int16_t Holder::get() const {
    return get_int16();
}

template <>
uint16_t Holder::get() const {
    return get_uint16();
}

template <>
int32_t Holder::get() const {
    return get_int32();
}

template <>
uint32_t Holder::get() const {
    return get_uint32();
}

template <>
int64_t Holder::get() const {
    return get_int64();
}

template <>
uint64_t Holder::get() const {
    return get_uint64();
}

template <>
double Holder::get() const {
    return get_double();
}

template <>
std::string Holder::get() const {
    return get_string();
}

template <>
std::vector<Holder> Holder::get() const {
    return get_array();
}

template <>
std::map<uint8_t, Holder> Holder::get() const {
    return get_dict_uint8();
}

template <>
std::map<uint16_t, Holder> Holder::get() const {
    return get_dict_uint16();
}

template <>
std::map<uint32_t, Holder> Holder::get() const {
    return get_dict_uint32();
}

template <>
std::map<uint64_t, Holder> Holder::get() const {
    return get_dict_uint64();
}

template <>
std::map<int16_t, Holder> Holder::get() const {
    return get_dict_int16();
}

template <>
std::map<int32_t, Holder> Holder::get() const {
    return get_dict_int32();
}

template <>
std::map<int64_t, Holder> Holder::get() const {
    return get_dict_int64();
}

template <>
std::map<std::string, Holder> Holder::get() const {
    return get_dict_string();
}

template <>
std::map<ObjectPath, Holder> Holder::get() const {
    std::map<ObjectPath, Holder> output;
    for (auto& [key_type_internal, key, value] : holder_dict) {
        if (key_type_internal == OBJ_PATH) {
            output[ObjectPath(std::any_cast<std::string>(key))] = value;
        }
    }
    return output;
}

template <>
std::map<Signature, Holder> Holder::get() const {
    std::map<Signature, Holder> output;
    for (auto& [key_type_internal, key, value] : holder_dict) {
        if (key_type_internal == SIGNATURE) {
            output[Signature(std::any_cast<std::string>(key))] = value;
        }
    }
    return output;
}

void Holder::array_append(Holder holder) { holder_array.push_back(holder); }

void Holder::dict_append(Type key_type, std::any key, Holder value) {
    if (key.type() == typeid(const char*)) {
        key = std::string(std::any_cast<const char*>(key));
    }

    // TODO : VALIDATE THAT THE SPECIFIED KEY TYPE IS CORRECT

    holder_dict.push_back(std::make_tuple(key_type, key, value));
}

template <typename T>
std::map<T, Holder> Holder::_get_dict(Type key_type) const {
    std::map<T, Holder> output;
    for (auto& [key_type_internal, key, value] : holder_dict) {
        if (key_type_internal == key_type) {
            output[std::any_cast<T>(key)] = value;
        }
    }
    return output;
}