test_util.cpp 9.33 KB
Newer Older
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
#include "test_util.h"

#include "ble.h"

#include <algorithm>
#include <cstring>
#include <iterator>

#include <cctype>
#include <random>
#include <vector>

namespace testutil {

/**
 * @brief Function that find advertising type data based on specified specified advertising type
 *
 * @param[in]  advType Advertisment type to search for
 * @param[in]  advData Advertising data to parse
 * @param[in,out] advTypeData Advertising data found for provided advertising type
 *
 * @return true if advertising type was found, false if not.
 */
bool advReportParse(const uint8_t advType, const std::vector<uint8_t> &advData,
                    std::vector<uint8_t> &advTypeData)
{
    auto typeDataBegin = advData.begin();
    while (typeDataBegin != advData.end())
    {
        auto fieldLength = *typeDataBegin++;
        if (typeDataBegin == advData.end())
        {
            return false;
        }

        auto fieldType = *typeDataBegin++;
        if (typeDataBegin == advData.end())
        {
            return false;
        }

        const auto advTypeDataLength = fieldLength - 1;
        const auto distance          = std::distance(typeDataBegin, advData.end());

        if (distance < advTypeDataLength)
        {
            return false;
        }

        if (fieldType == advType)
        {
            const auto typeDataEnd = typeDataBegin + advTypeDataLength;
            advTypeData.assign(typeDataBegin, typeDataEnd);
            return true;
        }

        std::advance(typeDataBegin, fieldLength - 1);
    }

    return false;
}

bool findManufacturerSpecificData(const ble_gap_evt_adv_report_t &p_adv_report,
                                  std::vector<uint8_t> &manufacturer_specific_data)
{
    std::vector<uint8_t> advData;

    uint8_t *data;
    uint16_t data_len;

#if NRF_SD_BLE_API >= 6
    data     = (uint8_t *)p_adv_report.data.p_data;
    data_len = p_adv_report.data.len;
#else
    data     = (uint8_t *)p_adv_report.data;
    data_len = p_adv_report.dlen;
#endif
    advData.assign(data, data + data_len);

    std::vector<uint8_t> manufacturerSpecific;
    return advReportParse(BLE_GAP_AD_TYPE_MANUFACTURER_SPECIFIC_DATA, advData,
                          manufacturer_specific_data);
}

/**
 * @brief Function that search for advertising name in BLE_GAP_AD_TYPE_COMPLETE_LOCAL_NAME or
 * BLE_GAP_AD_TYPE_SHORT_LOCAL_NAME
 *
 * @param[in] p_adv_report Pointer to advertising report to search in
 * @param[in] name_to_find Advertising name to search for
 *
 * @return true if name is found, false if not
 */
bool findAdvName(const ble_gap_evt_adv_report_t p_adv_report, const std::string &name_to_find)
{
    std::vector<uint8_t> advData;

    uint8_t *data;
    uint16_t data_len;

#if NRF_SD_BLE_API >= 6
    data     = (uint8_t *)p_adv_report.data.p_data;
    data_len = p_adv_report.data.len;
#else
    data     = (uint8_t *)p_adv_report.data;
    data_len = p_adv_report.dlen;
#endif
    advData.assign(data, data + data_len);

    std::vector<uint8_t> advTypeData;
    std::vector<uint8_t> nameToFind;
    std::copy(name_to_find.begin(), name_to_find.end(), std::back_inserter(nameToFind));

    auto found = [&](const uint8_t advType) {
        if (advReportParse(advType, advData, advTypeData))
        {
            if (nameToFind.size() > advTypeData.size())
            {
                return false;
            }

            if (std::equal(nameToFind.begin(), nameToFind.end(), advTypeData.begin()))
            {
                return true;
            }
        }

        return false;
    };

    return found(BLE_GAP_AD_TYPE_COMPLETE_LOCAL_NAME) || found(BLE_GAP_AD_TYPE_SHORT_LOCAL_NAME);
}

void assertAdvertisingPacketSizeIsValid(const std::vector<uint8_t> existingPacket,
                                          const size_t additionalBytes, const bool extended,
                                          const bool connectable)
{
    auto valid            = true;
    const auto packetSize = existingPacket.size() + additionalBytes;

    if (packetSize > testutil::ADV_DATA_BUFFER_SIZE)
    {
        if (extended)
        {
#if NRF_SD_BLE_API == 6
            valid = packetSize >
                    static_cast<size_t>(
                        connectable ? BLE_GAP_ADV_SET_DATA_SIZE_EXTENDED_CONNECTABLE_MAX_SUPPORTED
                                    : BLE_GAP_ADV_SET_DATA_SIZE_EXTENDED_MAX_SUPPORTED);
#else
            throw std::invalid_argument(
                "Only SoftDevice API version 6 or newer support extended advertising.");
#endif
        }
        else
        {
#if NRF_SD_BLE_API == 6
            valid = packetSize > BLE_GAP_ADV_SET_DATA_SIZE_MAX;
#else
            valid = packetSize > BLE_GAP_ADV_MAX_SIZE;
#endif
        }
    }

    if (!valid)
    {
        throw std::range_error("Packet size is larger than allowed for this type of advertising.");
    }
}

/**
 * @brief Function that append name to advertise to advertising type
 * BLE_GAP_AD_TYPE_COMPLETE_LOCAL_NAME
 *
 * @param[in,out] advertisingData std::vector to append advertising data to
 * @param[in] name Name to append to advertisingData
 */
void appendAdvertisingName(std::vector<uint8_t> &advertisingData, const std::string &name,
                           const bool extended)
{
    assertAdvertisingPacketSizeIsValid(advertisingData, 2 /* size + type */ + name.length(),
                                         extended);

    advertisingData.push_back(1 /* type */ + static_cast<uint8_t>(name.length()));
    advertisingData.push_back(BLE_GAP_AD_TYPE_COMPLETE_LOCAL_NAME);
    std::copy(name.begin(), name.end(), std::back_inserter(advertisingData));
}

/**
 * @brief Function that append flags to advertising type
 *
 * @param[in,out] advertisingData std::vector to append advertising flags to
 * @param[in] flags Flags to append to advertisingData
 */
void appendAdvertisingFlags(std::vector<uint8_t> &advertisingData, const uint8_t flags,
                              const bool extended)
{
    assertAdvertisingPacketSizeIsValid(advertisingData, 3 /* size + type + flags */, extended);

    advertisingData.push_back(2); // Flags field size + flags data size
    advertisingData.push_back(BLE_GAP_AD_TYPE_FLAGS);
    advertisingData.push_back(flags);
}

/**
 * @brief Function that append manufacturer specifid data to advertising
 *
 * @param[in,out] advertisingData std::vector to append manufacturer specific data to
 * @param[in] manufacturerSpecificData Manufacturer specific data to append to advertisingData
 */
void appendManufacturerSpecificData(std::vector<uint8_t> &advertisingData,
                                    const std::vector<uint8_t> manufacturerSpecificData,
                                    const bool extended)
{
    assertAdvertisingPacketSizeIsValid(
        advertisingData, 2 /* size + type */ + manufacturerSpecificData.size(), extended);

    advertisingData.push_back(1 /* type */ + static_cast<uint8_t>(manufacturerSpecificData.size()));
    advertisingData.push_back(BLE_GAP_AD_TYPE_MANUFACTURER_SPECIFIC_DATA);
    std::copy(manufacturerSpecificData.begin(), manufacturerSpecificData.end(),
              std::back_inserter(advertisingData));
}

/*
 * @brief Function that fills a std::vector<uint8_t> with random values
 *
 * @param[in,out] data vector to populate with random values
 * @param[in] size number of random values to fill the vector with
 */
void appendRandomData(std::vector<uint8_t> &data, const size_t size)
{
    data.resize(size);
    std::generate(data.begin(), data.end(), std::rand);
}

/*
 * @brief Function that fills a std::vector<uint8_t> with random ASCII values
 *
 * @param[in,out] data vector to populate with random ASCII values
 * @param[in] size number of random values to fill the vector with
 */

void appendRandomAlphaNumeric(std::vector<uint8_t> &data, const size_t size)
{
    data.resize(size);
    std::generate(data.begin(), data.end(), [] {
        uint8_t c;
        while (!std::isalnum(c = static_cast<uint8_t>(std::rand())))
            ;
        return c;
    });
}

std::string createRandomAdvertisingName(const std::string &prefix, const uint8_t length)
{
    std::vector<uint8_t> data;
    data.reserve(length);

    if (prefix.length() > length)
    {
        return std::string(prefix.begin(), prefix.begin() + length);
    }

    std::copy(prefix.begin(), prefix.end(), std::back_inserter(data));
    const auto randomLength = length - prefix.length();
    std::generate_n(std::back_inserter(data), randomLength, [] {
267
268
        uint8_t c;
        while (!std::isalnum(c = static_cast<uint8_t>(std::rand())))
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
            ;
        return c;
    });

    return std::string(data.begin(), data.end());
}

bool operator==(const ble_gap_addr_t &lhs, const ble_gap_addr_t &rhs)
{
    const auto addressIsSame = std::memcmp(lhs.addr, rhs.addr, BLE_GAP_ADDR_LEN) == 0;

    if (addressIsSame && (lhs.addr_type == rhs.addr_type))
    {
#if NRF_SD_BLE_API == 6
        if (lhs.addr_id_peer != rhs.addr_id_peer)
        {
            return false;
        }
#endif // NRF_SD_BLE_API

        return true;
    }
    else
    {
        return false;
    }
}

bool operator!=(const ble_gap_addr_t &lhs, const ble_gap_addr_t &rhs)
{
    return !(lhs == rhs);
}

#if NRF_SD_BLE_API == 6
bool operator==(const ble_gap_phys_t &lhs, const ble_gap_phys_t &rhs)
{
    return ((lhs.tx_phys == rhs.tx_phys) && (lhs.rx_phys == rhs.rx_phys));
}

bool operator!=(const ble_gap_phys_t &lhs, const ble_gap_phys_t &rhs)
{
    return !(lhs == rhs);
}
#endif // NRF_SD_BLE_API == 6

} // namespace testutil