|
| 1 | +/* |
| 2 | + This file is part of the ArduinoBLE library. |
| 3 | + Copyright (c) 2018 Arduino SA. All rights reserved. |
| 4 | +
|
| 5 | + This library is free software; you can redistribute it and/or |
| 6 | + modify it under the terms of the GNU Lesser General Public |
| 7 | + License as published by the Free Software Foundation; either |
| 8 | + version 2.1 of the License, or (at your option) any later version. |
| 9 | +
|
| 10 | + This library is distributed in the hope that it will be useful, |
| 11 | + but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | + Lesser General Public License for more details. |
| 14 | +
|
| 15 | + You should have received a copy of the GNU Lesser General Public |
| 16 | + License along with this library; if not, write to the Free Software |
| 17 | + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 18 | +*/ |
| 19 | + |
| 20 | +#include <catch.hpp> |
| 21 | + |
| 22 | +#define private public |
| 23 | +#define protected public |
| 24 | + |
| 25 | +#include "FakeBLELocalDevice.h" |
| 26 | +#include "BLEAdvertisingData.h" |
| 27 | + |
| 28 | +TEST_CASE("Test flags override", "[ArduinoBLE::BLEAdvertisingData]") |
| 29 | +{ |
| 30 | + // Mocking advertisement packet |
| 31 | + BLEAdvertisingData advData; |
| 32 | + // Expected results |
| 33 | + uint8_t defaultData[] = {0x02, BLEFieldFlags, 0x06}; |
| 34 | + uint8_t goldenFlags[] = {0x02, 0x01, BLEFlagsBREDRNotSupported}; |
| 35 | + |
| 36 | + WHEN("Default options for flags") |
| 37 | + { |
| 38 | + BLE.advertise(); |
| 39 | + REQUIRE( 0 == (memcmp(defaultData, BLE.getAdvertisingData().data(), sizeof(defaultData))) ); |
| 40 | + REQUIRE( BLE.getScanResponseData().dataLength() == 0 ); |
| 41 | + } |
| 42 | + |
| 43 | + WHEN("Setting external advertising data which has flags") |
| 44 | + { |
| 45 | + advData.setFlags(BLEFlagsBREDRNotSupported); |
| 46 | + advData.updateData(); |
| 47 | + BLE.setAdvertisingData(advData); |
| 48 | + BLE.advertise(); |
| 49 | + REQUIRE( 3 == BLE.getAdvertisingData().dataLength()); |
| 50 | + REQUIRE( 0 == (memcmp(goldenFlags, advData.data(), sizeof(goldenFlags))) ); |
| 51 | + REQUIRE( 0 == (memcmp(goldenFlags, BLE.getAdvertisingData().data(), sizeof(goldenFlags))) ); |
| 52 | + REQUIRE( 0 != (memcmp(defaultData, BLE.getAdvertisingData().data(), sizeof(defaultData))) ); |
| 53 | + } |
| 54 | + |
| 55 | + WHEN("Setting external advertising data without flags") |
| 56 | + { |
| 57 | + advData.clear(); |
| 58 | + BLE.setAdvertisingData(advData); |
| 59 | + BLE.advertise(); |
| 60 | + REQUIRE( 0 == (memcmp(defaultData, BLE.getAdvertisingData().data(), sizeof(defaultData))) ); |
| 61 | + } |
| 62 | + |
| 63 | + // Clear BLE advertising data |
| 64 | + advData.clear(); |
| 65 | + BLE.setAdvertisingData(advData); |
| 66 | +} |
| 67 | + |
| 68 | +TEST_CASE("Set default flags in an already full advertising data packet", "[ArduinoBLE::BLEAdvertisingData]") |
| 69 | +{ |
| 70 | + BLEAdvertisingData advData; |
| 71 | + bool retVal; |
| 72 | + |
| 73 | + const char* name = "tes"; |
| 74 | + const uint8_t manufacturerData[24] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, |
| 75 | + 17, 18, 19, 20, 21, 22, 23}; |
| 76 | + |
| 77 | + uint8_t defaultFlags[3] = {0x02, 0x01, 0x06}; |
| 78 | + |
| 79 | + uint8_t goldenData[31] = { |
| 80 | + (sizeof(manufacturerData) + 1), BLEFieldManufacturerData, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, |
| 81 | + 17, 18, 19, 20, 21, 22, 23, |
| 82 | + ((uint8_t)(strlen(name) + 1)), BLEFieldCompleteLocalName, 't', 'e', 's' |
| 83 | + }; |
| 84 | + |
| 85 | + WHEN("Test flags when advertising data is full") |
| 86 | + { |
| 87 | + retVal = advData.setLocalName("tes"); |
| 88 | + retVal = advData.setManufacturerData(manufacturerData, sizeof(manufacturerData)); |
| 89 | + |
| 90 | + BLE.setAdvertisingData(advData); |
| 91 | + BLE.advertise(); |
| 92 | + REQUIRE(0 != memcmp(defaultFlags, BLE.getAdvertisingData().data(), sizeof(defaultFlags))); |
| 93 | + REQUIRE(0 == memcmp(goldenData, BLE.getAdvertisingData().data(), sizeof(goldenData))); |
| 94 | + } |
| 95 | + |
| 96 | + // Clear BLE advertising data |
| 97 | + advData.clear(); |
| 98 | + BLE.setAdvertisingData(advData); |
| 99 | +} |
| 100 | + |
| 101 | +TEST_CASE("BLE overwrite a full internal advertising data with an external built one", "[ArduinoBLE::BLEAdvertisingData]") |
| 102 | +{ |
| 103 | + bool verify; |
| 104 | + BLEAdvertisingData advData; |
| 105 | + BLEAdvertisingData internalData; |
| 106 | + |
| 107 | + WHEN("Copy empty external advertising data") |
| 108 | + { |
| 109 | + BLE.setLocalName("test"); |
| 110 | + BLE.setAdvertisedServiceUuid("1818"); |
| 111 | + BLE.advertise(); |
| 112 | + //WARN("data length: " << BLE.getAdvertisingData().dataLength()); |
| 113 | + verify = (BLE.getAdvertisingData().dataLength() == (3 + (2+2))); |
| 114 | + REQUIRE(verify); |
| 115 | + verify = (BLE.getScanResponseData().dataLength() == (4+2)); |
| 116 | + REQUIRE(verify); |
| 117 | + |
| 118 | + BLE.setAdvertisingData(advData); |
| 119 | + BLE.advertise(); |
| 120 | + //WARN(BLE.getAdvertisingData().dataLength()); |
| 121 | + verify = (BLE.getAdvertisingData().dataLength() == 3); |
| 122 | + REQUIRE(verify); |
| 123 | + |
| 124 | + BLE.setScanResponseData(advData); |
| 125 | + BLE.advertise(); |
| 126 | + verify = (BLE.getScanResponseData().dataLength() == 0); |
| 127 | + REQUIRE(verify); |
| 128 | + } |
| 129 | + |
| 130 | + // Clear BLE advertising data |
| 131 | + advData.clear(); |
| 132 | + BLE.setAdvertisingData(advData); |
| 133 | +} |
| 134 | + |
| 135 | +TEST_CASE("BLE test raw data", "[ArduinoBLE::BLEAdvertisingData]") |
| 136 | +{ |
| 137 | + BLEAdvertisingData advData; |
| 138 | + bool retVal; |
| 139 | + |
| 140 | + WHEN("Set too large raw data") |
| 141 | + { |
| 142 | + uint8_t data[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, |
| 143 | + 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, |
| 144 | + 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32 }; |
| 145 | + advData.setRawData(data, sizeof(data)); |
| 146 | + REQUIRE(!retVal); |
| 147 | + advData.clear(); |
| 148 | + } |
| 149 | + |
| 150 | + WHEN("Set correct raw data") |
| 151 | + { |
| 152 | + uint8_t data[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, |
| 153 | + 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, |
| 154 | + 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31}; |
| 155 | + retVal = advData.setRawData(data, sizeof(data)); |
| 156 | + REQUIRE(retVal); |
| 157 | + advData.updateData(); |
| 158 | + REQUIRE( 0 == memcmp(data, advData.data(), sizeof(data)) ); |
| 159 | + advData.clear(); |
| 160 | + } |
| 161 | + |
| 162 | + WHEN("Hide other parameters by setting raw data") |
| 163 | + { |
| 164 | + uint8_t data[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; |
| 165 | + advData.setLocalName("test"); |
| 166 | + advData.setRawData(data, sizeof(data)); |
| 167 | + |
| 168 | + advData.updateData(); |
| 169 | + REQUIRE( 0 == memcmp(data, advData.data(), sizeof(data)) ); |
| 170 | + |
| 171 | + advData.setLocalName("test"); |
| 172 | + advData.updateData(); |
| 173 | + REQUIRE( 0 == memcmp(data, advData.data(), sizeof(data)) ); |
| 174 | + |
| 175 | + advData.clear(); |
| 176 | + advData.setLocalName("test"); |
| 177 | + advData.updateData(); |
| 178 | + REQUIRE( 0 != memcmp(data, advData.data(), sizeof(data)) ); |
| 179 | + } |
| 180 | + |
| 181 | + // Clear BLE advertising data |
| 182 | + advData.clear(); |
| 183 | + BLE.setAdvertisingData(advData); |
| 184 | +} |
0 commit comments