From 732219677fc18ead1452200c1ce35d1b0b12ff90 Mon Sep 17 00:00:00 2001 From: porubcinj <188404015+porubcinj@users.noreply.github.com> Date: Tue, 4 Feb 2025 15:06:25 -0500 Subject: [PATCH 1/2] Add BLEPermission Support to BLEStringCharacteristic 5 years ago, support for encryption was added by #156. Currently, the BLEEncryption tag needs to be added to require proper encrypted bluetooth pairing. It is possible to add this tag to all other kinds of characteristics except String characteristics. BLEStringCharacteristic's constructor calls BLECharacteristic's constructor, and BLECharacteristic uses a uint16_t instead of a uint8_t. This PR simply changes the properties attribute of BLEStringCharacteristic to match the width of the permissions attribute of BLECharacteristic (from 8 to 16 bits) to support the BLEPermission tags in BLEProperty.h. --- src/BLEStringCharacteristic.cpp | 2 +- src/BLEStringCharacteristic.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/BLEStringCharacteristic.cpp b/src/BLEStringCharacteristic.cpp index 76f0812c..a12e722d 100644 --- a/src/BLEStringCharacteristic.cpp +++ b/src/BLEStringCharacteristic.cpp @@ -19,7 +19,7 @@ #include "BLEStringCharacteristic.h" -BLEStringCharacteristic::BLEStringCharacteristic(const char* uuid, unsigned char properties, int valueSize) : +BLEStringCharacteristic::BLEStringCharacteristic(const char* uuid, unsigned int properties, int valueSize) : BLECharacteristic(uuid, properties, valueSize) { } diff --git a/src/BLEStringCharacteristic.h b/src/BLEStringCharacteristic.h index c9f28fca..65604846 100644 --- a/src/BLEStringCharacteristic.h +++ b/src/BLEStringCharacteristic.h @@ -27,7 +27,7 @@ class BLEStringCharacteristic : public BLECharacteristic { public: - BLEStringCharacteristic(const char* uuid, unsigned char properties, int valueSize); + BLEStringCharacteristic(const char* uuid, unsigned int properties, int valueSize); int writeValue(const String& value); int setValue(const String& value) { return writeValue(value); } From e2bf0c08e3eae28c5a0d92bc4390bb3ae16005b5 Mon Sep 17 00:00:00 2001 From: pennam Date: Thu, 10 Apr 2025 11:32:20 +0200 Subject: [PATCH 2/2] Add basic test case on characteristic permissions --- extras/test/CMakeLists.txt | 21 +++ .../test_characteristic/test_permissions.cpp | 126 ++++++++++++++++++ 2 files changed, 147 insertions(+) create mode 100644 extras/test/src/test_characteristic/test_permissions.cpp diff --git a/extras/test/CMakeLists.txt b/extras/test/CMakeLists.txt index 84603cd3..a90fc5aa 100644 --- a/extras/test/CMakeLists.txt +++ b/extras/test/CMakeLists.txt @@ -96,6 +96,17 @@ set(TEST_TARGET_ADVERTISING_DATA_SRCS src/test_advertising_data/FakeBLELocalDevice.cpp ) +set(TEST_TARGET_CHARACTERISTIC_SRCS + # Test files + ${COMMON_TEST_SRCS} + src/test_characteristic/test_permissions.cpp + # DUT files + ${DUT_SRCS} + # Fake classes files + src/util/HCIFakeTransport.cpp + src/test_advertising_data/FakeBLELocalDevice.cpp +) + ########################################################################## set(CMAKE_C_FLAGS ${CMAKE_C_FLAGS} "--coverage") @@ -106,6 +117,7 @@ set(CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS} "--coverage") add_executable(TEST_TARGET_UUID ${TEST_TARGET_UUID_SRCS}) add_executable(TEST_TARGET_DISC_DEVICE ${TEST_TARGET_DISC_DEVICE_SRCS}) add_executable(TEST_TARGET_ADVERTISING_DATA ${TEST_TARGET_ADVERTISING_DATA_SRCS}) +add_executable(TEST_TARGET_CHARACTERISTIC_DATA ${TEST_TARGET_CHARACTERISTIC_SRCS}) ########################################################################## @@ -118,11 +130,13 @@ include_directories(../../src/utility) target_include_directories(TEST_TARGET_DISC_DEVICE PUBLIC include/test_discovered_device) target_include_directories(TEST_TARGET_ADVERTISING_DATA PUBLIC include/test_advertising_data) +target_include_directories(TEST_TARGET_CHARACTERISTIC_DATA PUBLIC include/test_advertising_data) ########################################################################## target_compile_definitions(TEST_TARGET_DISC_DEVICE PUBLIC FAKE_GAP) target_compile_definitions(TEST_TARGET_ADVERTISING_DATA PUBLIC FAKE_BLELOCALDEVICE) +target_compile_definitions(TEST_TARGET_CHARACTERISTIC_DATA PUBLIC FAKE_BLELOCALDEVICE) ########################################################################## @@ -137,6 +151,13 @@ add_custom_command(TARGET TEST_TARGET_ADVERTISING_DATA POST_BUILD COMMAND ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/TEST_TARGET_ADVERTISING_DATA ) +add_custom_command(TARGET TEST_TARGET_CHARACTERISTIC_DATA POST_BUILD + COMMAND ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/TEST_TARGET_CHARACTERISTIC_DATA +) + +########################################################################## + target_link_libraries( TEST_TARGET_UUID Catch2WithMain ) target_link_libraries( TEST_TARGET_DISC_DEVICE Catch2WithMain ) target_link_libraries( TEST_TARGET_ADVERTISING_DATA Catch2WithMain ) +target_link_libraries( TEST_TARGET_CHARACTERISTIC_DATA Catch2WithMain ) diff --git a/extras/test/src/test_characteristic/test_permissions.cpp b/extras/test/src/test_characteristic/test_permissions.cpp new file mode 100644 index 00000000..cd658580 --- /dev/null +++ b/extras/test/src/test_characteristic/test_permissions.cpp @@ -0,0 +1,126 @@ +/* + This file is part of the ArduinoBLE library. + Copyright (c) 2018 Arduino SA. All rights reserved. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA +*/ + +#include + +#define private public +#define protected public + +#include "FakeBLELocalDevice.h" +#include "BLEAdvertisingData.h" +#include "BLETypedCharacteristics.h" +#include "BLELocalCharacteristic.h" +#include "BLEStringCharacteristic.h" +#include "BLEProperty.h" +#include + +int property[] = { + BLEBroadcast, + BLERead, + BLEWriteWithoutResponse, + BLEWrite, + BLENotify, + BLEIndicate, + BLEAuthSignedWrite, + BLEExtProp, + BLERead | BLEWrite | BLENotify +}; + +int permission[] = { + BLEEncryption, + BLEAuthentication, + BLEAuthorization, + BLEEncryption | BLEAuthentication +}; + +const char uuid[][31] = { + "1 Bool", + "2 Char", + "3 UnsignedChar", + "4 Byte", + "5 Short", + "6 UnsignedShort", + "7 Word", + "8 Int", + "9 UnsignedInt", + "A Long", + "B UnsignedLong", + "C Float", + "D Double", + "E String" +}; + +std::unique_ptr createCharacteristic(const char* uuid, unsigned int properties) +{ + switch(uuid[0]) + { + case '1': + return std::unique_ptr(new BLEBoolCharacteristic(uuid, properties)); + case '2': + return std::unique_ptr(new BLECharCharacteristic(uuid, properties)); + case '3': + return std::unique_ptr(new BLEUnsignedCharCharacteristic(uuid, properties)); + case '4': + return std::unique_ptr(new BLEByteCharacteristic(uuid, properties)); + case '5': + return std::unique_ptr(new BLEShortCharacteristic(uuid, properties)); + case '6': + return std::unique_ptr(new BLEUnsignedShortCharacteristic(uuid, properties)); + case '7': + return std::unique_ptr(new BLEWordCharacteristic(uuid, properties)); + case '8': + return std::unique_ptr(new BLEIntCharacteristic(uuid, properties)); + case '9': + return std::unique_ptr(new BLEUnsignedIntCharacteristic(uuid, properties)); + case 'A': + return std::unique_ptr(new BLELongCharacteristic(uuid, properties)); + case 'B': + return std::unique_ptr(new BLEUnsignedLongCharacteristic(uuid, properties)); + case 'C': + return std::unique_ptr(new BLEFloatCharacteristic(uuid, properties)); + case 'D': + return std::unique_ptr(new BLEDoubleCharacteristic(uuid, properties)); + case 'E': + return std::unique_ptr(new BLEStringCharacteristic(uuid, properties, 2)); + default: + break; + } + return nullptr; +} + +TEST_CASE("Test characteristic properties and permissions", "[ArduinoBLE::BLECharacteristic]") +{ + WHEN("Create a characteristic") + { + for(int i = 0; i < sizeof(property)/sizeof(int); i++) + { + for(int j = 0; j < sizeof(permission)/sizeof(int); j++) + { + for(int k = 0; k < 14; k++) + { + std::unique_ptr ptr = createCharacteristic(uuid[k], property[i] | permission[j]); + REQUIRE(ptr != nullptr); + REQUIRE(ptr->properties() == (property[i])); + BLELocalCharacteristic * local = ptr->local(); + REQUIRE(local->permissions() == (permission[j] >> 8)); + } + } + } + } +}