|
| 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 <catch2/catch_test_macros.hpp> |
| 21 | + |
| 22 | +#define private public |
| 23 | +#define protected public |
| 24 | + |
| 25 | +#include "FakeBLELocalDevice.h" |
| 26 | +#include "BLEAdvertisingData.h" |
| 27 | +#include "BLETypedCharacteristics.h" |
| 28 | +#include "BLELocalCharacteristic.h" |
| 29 | +#include "BLEStringCharacteristic.h" |
| 30 | +#include "BLEProperty.h" |
| 31 | +#include <memory> |
| 32 | + |
| 33 | +int property[] = { |
| 34 | + BLEBroadcast, |
| 35 | + BLERead, |
| 36 | + BLEWriteWithoutResponse, |
| 37 | + BLEWrite, |
| 38 | + BLENotify, |
| 39 | + BLEIndicate, |
| 40 | + BLEAuthSignedWrite, |
| 41 | + BLEExtProp, |
| 42 | + BLERead | BLEWrite | BLENotify |
| 43 | +}; |
| 44 | + |
| 45 | +int permission[] = { |
| 46 | + BLEEncryption, |
| 47 | + BLEAuthentication, |
| 48 | + BLEAuthorization, |
| 49 | + BLEEncryption | BLEAuthentication |
| 50 | +}; |
| 51 | + |
| 52 | +const char uuid[][31] = { |
| 53 | + "1 Bool", |
| 54 | + "2 Char", |
| 55 | + "3 UnsignedChar", |
| 56 | + "4 Byte", |
| 57 | + "5 Short", |
| 58 | + "6 UnsignedShort", |
| 59 | + "7 Word", |
| 60 | + "8 Int", |
| 61 | + "9 UnsignedInt", |
| 62 | + "A Long", |
| 63 | + "B UnsignedLong", |
| 64 | + "C Float", |
| 65 | + "D Double", |
| 66 | + "E String" |
| 67 | +}; |
| 68 | + |
| 69 | +std::unique_ptr<BLECharacteristic> createCharacteristic(const char* uuid, unsigned int properties) |
| 70 | +{ |
| 71 | + switch(uuid[0]) |
| 72 | + { |
| 73 | + case '1': |
| 74 | + return std::unique_ptr<BLECharacteristic>(new BLEBoolCharacteristic(uuid, properties)); |
| 75 | + case '2': |
| 76 | + return std::unique_ptr<BLECharacteristic>(new BLECharCharacteristic(uuid, properties)); |
| 77 | + case '3': |
| 78 | + return std::unique_ptr<BLECharacteristic>(new BLEUnsignedCharCharacteristic(uuid, properties)); |
| 79 | + case '4': |
| 80 | + return std::unique_ptr<BLECharacteristic>(new BLEByteCharacteristic(uuid, properties)); |
| 81 | + case '5': |
| 82 | + return std::unique_ptr<BLECharacteristic>(new BLEShortCharacteristic(uuid, properties)); |
| 83 | + case '6': |
| 84 | + return std::unique_ptr<BLECharacteristic>(new BLEUnsignedShortCharacteristic(uuid, properties)); |
| 85 | + case '7': |
| 86 | + return std::unique_ptr<BLECharacteristic>(new BLEWordCharacteristic(uuid, properties)); |
| 87 | + case '8': |
| 88 | + return std::unique_ptr<BLECharacteristic>(new BLEIntCharacteristic(uuid, properties)); |
| 89 | + case '9': |
| 90 | + return std::unique_ptr<BLECharacteristic>(new BLEUnsignedIntCharacteristic(uuid, properties)); |
| 91 | + case 'A': |
| 92 | + return std::unique_ptr<BLECharacteristic>(new BLELongCharacteristic(uuid, properties)); |
| 93 | + case 'B': |
| 94 | + return std::unique_ptr<BLECharacteristic>(new BLEUnsignedLongCharacteristic(uuid, properties)); |
| 95 | + case 'C': |
| 96 | + return std::unique_ptr<BLECharacteristic>(new BLEFloatCharacteristic(uuid, properties)); |
| 97 | + case 'D': |
| 98 | + return std::unique_ptr<BLECharacteristic>(new BLEDoubleCharacteristic(uuid, properties)); |
| 99 | + case 'E': |
| 100 | + return std::unique_ptr<BLECharacteristic>(new BLEStringCharacteristic(uuid, properties, 2)); |
| 101 | + default: |
| 102 | + break; |
| 103 | + } |
| 104 | + return nullptr; |
| 105 | +} |
| 106 | + |
| 107 | +TEST_CASE("Test characteristic properties and permissions", "[ArduinoBLE::BLECharacteristic]") |
| 108 | +{ |
| 109 | + WHEN("Create a characteristic") |
| 110 | + { |
| 111 | + for(int i = 0; i < sizeof(property)/sizeof(int); i++) |
| 112 | + { |
| 113 | + for(int j = 0; j < sizeof(permission)/sizeof(int); j++) |
| 114 | + { |
| 115 | + for(int k = 0; k < 14; k++) |
| 116 | + { |
| 117 | + std::unique_ptr<BLECharacteristic> ptr = createCharacteristic(uuid[k], property[i] | permission[j]); |
| 118 | + REQUIRE(ptr != nullptr); |
| 119 | + REQUIRE(ptr->properties() == (property[i])); |
| 120 | + BLELocalCharacteristic * local = ptr->local(); |
| 121 | + REQUIRE(local->permissions() == (permission[j] >> 8)); |
| 122 | + } |
| 123 | + } |
| 124 | + } |
| 125 | + } |
| 126 | +} |
0 commit comments