Direct-BT v3.3.0-1-gc2d430c
Direct-BT - Direct Bluetooth Programming.
test_adeir01.cpp
Go to the documentation of this file.
1#include <iostream>
2#include <cassert>
3#include <cinttypes>
4#include <cstring>
5
7
8#include <jau/uuid.hpp>
9// #include <direct_bt/BTAddress.hpp>
10// #include <direct_bt/BTTypes1.hpp>
12// #include <direct_bt/GATTHandler.hpp>
13// #include <direct_bt/GATTIoctl.hpp>
14
15using namespace direct_bt;
16
17/**
18 * EIR AD Test: Squeezing all-at-once .. fits in 31 bytes
19 */
20TEST_CASE( "AD EIR PDU Test 01", "[datatype][AD][EIR]" ) {
21 const std::vector<uint8_t> msd_data = { 0x01, 0x02 };
22 ManufactureSpecificData msd(0x0001, msd_data.data(), msd_data.size());
23
24 const jau::uuid16_t uuid_01 = jau::uuid16_t(0x1234);
25 const jau::uuid16_t uuid_02 = jau::uuid16_t(0x0a0b);
26 {
27 std::shared_ptr<const jau::uuid_t> const p1 = std::make_shared<jau::uuid16_t>( uuid_01 );
28 std::shared_ptr<const jau::uuid_t> const p2 = uuid_02.clone();
29 std::cout << "uuid_01: " << uuid_01.toString() << ", [" << p1->toString() << "]" << std::endl;
30 std::cout << "uuid_02: " << uuid_02.toString() << ", [" << p2->toString() << "]" << std::endl;
31 }
32
33 EInfoReport eir0;
34 eir0.setFlags(GAPFlags::LE_Gen_Disc);
35 eir0.setName("TestTempDev01"); // 13
36 eir0.setManufactureSpecificData(msd); // 2 + 4
37 eir0.addService(uuid_01);
38 eir0.addService(uuid_02);
39
40 std::cout << "eir0.0: " << eir0.toString(true) << std::endl;
41
42 std::vector<uint8_t> buffer;
43 {
44 buffer.resize(31); // max EIR size
45 const jau::nsize_t eir_sz = eir0.write_data(EIRDataType::ALL, buffer.data(), buffer.capacity());
46 buffer.resize(eir_sz);
47 std::cout << "eir0.0: bytes-out " << eir_sz << ", " << buffer.size() << std::endl;
48 std::cout << "eir0.0: " << jau::bytesHexString(buffer.data(), 0, buffer.size(), true /* lsb */) << std::endl;
49 }
50 std::cout << std::endl;
51
52 EInfoReport eir1;
53 const int eir_segments = eir1.read_data(buffer.data(), buffer.size());
54 std::cout << "eir1.0: segments " << eir_segments << std::endl;
55 std::cout << "eir1.0: " << eir1.toString(true) << std::endl;
56
57 REQUIRE(eir0 == eir1);
58}
59
60/**
61 * EIR AD Test: Exceeding 31 bytes -> Using two advertising EIR chunks (init + scan_rsp)
62 */
63TEST_CASE( "AD EIR PDU Test 02", "[datatype][AD][EIR]" ) {
64 const std::vector<uint8_t> msd_data = { 0x01, 0x02, 0x03, 0x04, 0x05 };
65 ManufactureSpecificData msd(0x0001, msd_data.data(), msd_data.size());
66
67 const jau::uuid16_t uuid_01(0x1234);
68 const jau::uuid16_t uuid_02(0x0a0b);
69 const jau::uuid32_t uuid_11(0xabcd1234);
70 const jau::uuid128_t uuid_21("00001234-5678-100a-8000-00805F9B34FB");
71 {
72 std::shared_ptr<const jau::uuid_t> const p1 = uuid_21.clone();
73 std::cout << "uuid_21: " << uuid_21.toString() << ", [" << p1->toString() << "]" << std::endl;
74 }
75
76 // Complete version
77 EInfoReport eir0a;
78 eir0a.setFlags(GAPFlags::LE_Gen_Disc);
79 eir0a.setName("TestTempDev02"); // 13
80 eir0a.setManufactureSpecificData(msd); // 2 + 4
81 eir0a.addService(uuid_01);
82 eir0a.addService(uuid_02);
83 eir0a.addService(uuid_11);
84 eir0a.addService(uuid_21);
85
86 // Less services (initial adv)
87 const EIRDataType mask_0b = EIRDataType::FLAGS | EIRDataType::NAME | EIRDataType::MANUF_DATA;
88 EInfoReport eir0b;
89 eir0b.setFlags(GAPFlags::LE_Gen_Disc);
90 eir0b.setName("TestTempDev02"); // 13
91 eir0b.setManufactureSpecificData(msd); // 2 + 4
92
93 // Only services (scan resp)
94 const EIRDataType mask_0c = EIRDataType::SERVICE_UUID;
95 EInfoReport eir0c;
96 eir0c.addService(uuid_01);
97 eir0c.addService(uuid_02);
98 eir0c.addService(uuid_11);
99 eir0c.addService(uuid_21);
100
101 std::cout << "eir0a: " << eir0a.toString(true) << std::endl;
102 std::cout << "eir0b: " << eir0b.toString(true) << std::endl;
103 std::cout << "eir0c: " << eir0c.toString(true) << std::endl;
104
105 // Test: Less services (initial adv)
106 {
107 std::vector<uint8_t> buffer;
108 buffer.resize(31); // max EIR size
109 const jau::nsize_t eir_sz = eir0a.write_data(mask_0b, buffer.data(), buffer.capacity());
110 buffer.resize(eir_sz);
111 std::cout << "eir0a.1: bytes-out " << eir_sz << ", " << buffer.size() << std::endl;
112 std::cout << "eir0a.1: " << jau::bytesHexString(buffer.data(), 0, buffer.size(), true /* lsb */) << std::endl;
113 std::cout << std::endl;
114
115 EInfoReport eir1;
116 const int eir_segments = eir1.read_data(buffer.data(), buffer.size());
117 std::cout << "eir1.0: segments " << eir_segments << std::endl;
118 std::cout << "eir1.0: " << eir1.toString(true) << std::endl;
119
120 REQUIRE(eir0b == eir1);
121 }
122
123 // Test: Only Services (scan resp)
124 {
125 std::vector<uint8_t> buffer;
126 buffer.resize(31); // max EIR size
127 const jau::nsize_t eir_sz = eir0a.write_data(mask_0c, buffer.data(), buffer.capacity());
128 buffer.resize(eir_sz);
129 std::cout << "eir0a.2: bytes-out " << eir_sz << ", " << buffer.size() << std::endl;
130 std::cout << "eir0a.2: " << jau::bytesHexString(buffer.data(), 0, buffer.size(), true /* lsb */) << std::endl;
131 std::cout << std::endl;
132
133 EInfoReport eir1;
134 const int eir_segments = eir1.read_data(buffer.data(), buffer.size());
135 std::cout << "eir2.0: segments " << eir_segments << std::endl;
136 std::cout << "eir2.0: " << eir1.toString(true) << std::endl;
137
138 REQUIRE(eir0c == eir1);
139 }
140
141 // Test: Both
142 {
143 std::vector<uint8_t> buffer1;
144 buffer1.resize(31); // max EIR size
145
146 const jau::nsize_t eir_sz1 = eir0a.write_data(mask_0b, buffer1.data(), buffer1.capacity());
147 buffer1.resize(eir_sz1);
148 std::cout << "eir0a.3: bytes-out " << eir_sz1 << ", " << buffer1.size() << std::endl;
149 std::cout << "eir0a.3: " << jau::bytesHexString(buffer1.data(), 0, buffer1.size(), true /* lsb */) << std::endl;
150 std::cout << std::endl;
151
152 std::vector<uint8_t> buffer2;
153 buffer2.resize(31); // max EIR size
154 const jau::nsize_t eir_sz2 = eir0a.write_data(mask_0c, buffer2.data(), buffer2.capacity());
155 buffer2.resize(eir_sz2);
156 std::cout << "eir0a.4: bytes-out " << eir_sz2 << ", " << buffer2.size() << std::endl;
157 std::cout << "eir0a.4: " << jau::bytesHexString(buffer2.data(), 0, buffer2.size(), true /* lsb */) << std::endl;
158 std::cout << std::endl;
159
160 EInfoReport eir1;
161 const int eir_segments1 = eir1.read_data(buffer1.data(), buffer1.size());
162 std::cout << "eir3.1: segments " << eir_segments1 << std::endl;
163 std::cout << "eir3.1: " << eir1.toString(true) << std::endl;
164
165 const int eir_segments2 = eir1.read_data(buffer2.data(), buffer2.size());
166 std::cout << "eir3.2: segments " << eir_segments2 << std::endl;
167 std::cout << "eir3.2: " << eir1.toString(true) << std::endl;
168
169 REQUIRE(eir0a == eir1);
170 }
171}
Collection of 'Extended Advertising Data' (EAD), 'Advertising Data' (AD) or 'Extended Inquiry Respons...
Definition: BTTypes0.hpp:898
std::string toString(const bool includeServices=true) const noexcept
Definition: BTTypes0.cpp:854
jau::nsize_t write_data(EIRDataType write_mask, uint8_t *data, jau::nsize_t const data_length) const noexcept
Writes the Extended Inquiry Response (EIR) or (Extended) Advertising Data (EAD or AD) segments of exi...
Definition: BTTypes0.cpp:1149
void setFlags(GAPFlags f) noexcept
Definition: BTTypes0.hpp:991
bool addService(const std::shared_ptr< const jau::uuid_t > &uuid) noexcept
Definition: BTTypes0.cpp:834
int read_data(uint8_t const *data, uint8_t const data_length) noexcept
Reads the Extended Inquiry Response (EIR) or (Extended) Advertising Data (EAD or AD) segments and ret...
Definition: BTTypes0.cpp:1003
std::string toString() const noexcept override
Returns the string representation in BE network order, i.e.
Definition: uuid.cpp:177
std::string toString() const noexcept override
Returns the string representation in BE network order, i.e.
Definition: uuid.cpp:135
std::unique_ptr< uuid_t > clone() const noexcept
Definition: uuid.cpp:84
EIRDataType
Bit mask of 'Extended Inquiry Response' (EIR) data fields, indicating a set of related data.
Definition: BTTypes0.hpp:838
uint_fast32_t nsize_t
Natural 'size_t' alternative using uint_fast32_t as its natural sized type.
Definition: int_types.hpp:53
std::string bytesHexString(const void *data, const nsize_t offset, const nsize_t length, const bool lsbFirst, const bool lowerCase=true) noexcept
Produce a hexadecimal string representation of the given byte values.
TEST_CASE("AD EIR PDU Test 01", "[datatype][AD][EIR]")
EIR AD Test: Squeezing all-at-once .