From b3c087e74484afffe15ba2c9da49e9aeedeac7fc Mon Sep 17 00:00:00 2001 From: Nat Ersoz Date: Sat, 9 Feb 2019 12:19:59 -0800 Subject: [PATCH] Add unit tests for service and characteristics classes Issue #5 --- ble/gatt_attribute.cc | 12 ++++--- unit_tests/Makefile | 7 ++++ unit_tests/test_service_container.cc | 53 ++++++++++++++++++++++++++++ 3 files changed, 67 insertions(+), 5 deletions(-) create mode 100644 unit_tests/test_service_container.cc diff --git a/ble/gatt_attribute.cc b/ble/gatt_attribute.cc index 5a5b406..b5dea53 100644 --- a/ble/gatt_attribute.cc +++ b/ble/gatt_attribute.cc @@ -33,6 +33,8 @@ ble::att::length_t attribute::write(ble::att::op_code write_type, ble::att::length_t length, void const* data) { + (void) write_type; + uint8_t *data_dst = reinterpret_cast(this->data_pointer()); ASSERT(data_dst); @@ -45,15 +47,15 @@ ble::att::length_t attribute::write(ble::att::op_code write_type, data_end = std::min(data_end, data_max); - std::ptrdiff_t const length = data_end - data_dst; - if (length > 0) + std::ptrdiff_t const dest_length = data_end - data_dst; + if (dest_length > 0) { - memcpy(data_dst, data, length); - return static_cast(length); + memcpy(data_dst, data, dest_length); + return static_cast(dest_length); } else { - logger::instance().warn("attribute::write: %d", length); + logger::instance().warn("attribute::write: %d", dest_length); return 0u; } } diff --git a/unit_tests/Makefile b/unit_tests/Makefile index b71fe84..071f3ee 100644 --- a/unit_tests/Makefile +++ b/unit_tests/Makefile @@ -23,6 +23,7 @@ INCLUDE_PATH += -I $(GTEST_DIR)/include/gtest/internal vpath %.cc . vpath %.cc ../utilities vpath %.cc ../ble +vpath %.cc ../ble/service vpath %.cc $(GTEST_DIR)/src/ vpath %.c $(GTEST_DIR)/src/ @@ -51,6 +52,11 @@ CPPFLAGS += -isystem $(GTEST_DIR)/include TARGET_NAME = all_tests SRC = +SRC += battery_service.cc +SRC += gatt_attribute.cc +SRC += gatt_characteristic.cc +SRC += gatt_service.cc +SRC += gatt_service_container.cc SRC += uuid.cc SRC += gregorian.cc @@ -64,6 +70,7 @@ SRC += test_gregorian.cc SRC += test_int_to_string.cc SRC += test_make_array.cc SRC += test_observer.cc +SRC += test_service_container.cc SRC += test_uuid.cc # Helpers, stubs, fakes. diff --git a/unit_tests/test_service_container.cc b/unit_tests/test_service_container.cc new file mode 100644 index 0000000..ff0a87d --- /dev/null +++ b/unit_tests/test_service_container.cc @@ -0,0 +1,53 @@ +/** + * @file test_service_container.cc + * @copyright (c) 2018, natersoz. Distributed under the Apache 2.0 license. + */ + +#include "gtest/gtest.h" + +#include "ble/gatt_service_container.h" +#include "ble/service/gap_service.h" +#include "ble/service/battery_service.h" +#include "ble/service/current_time_service.h" +#include "ble/gap_connection_parameters.h" + +TEST(ServiceContainerTest, GAP_Battery_Time) +{ + // --- GAP service + constexpr char const device_name[] = "periph"; + constexpr size_t const device_name_length = std::size(device_name) - 1u; + + ble::gap::connection_parameters const gap_connection_parameters( + ble::gap::connection_interval_msec(100), + ble::gap::connection_interval_msec(200), + 0u, + ble::gap::supervision_timeout_msec(4000u)); + + ble::service::device_name device_name_characteristic(device_name, device_name_length); + ble::service::appearance appearance_characteristic(ble::gatt::appearance::unknown); + ble::service::ppcp ppcp_characteristic(gap_connection_parameters); + ble::service::gap_service gap_service; + + gap_service.characteristic_add(device_name_characteristic); + gap_service.characteristic_add(appearance_characteristic); + gap_service.characteristic_add(ppcp_characteristic); + + // --- Battery Service + ble::service::battery_level battery_level_characteristic; + ble::service::battery_power_state battery_power_characteristic; + ble::service::battery_service battery_service; + + battery_service.characteristic_add(battery_level_characteristic); + battery_service.characteristic_add(battery_power_characteristic); + + // --- Current Time Service + ble::service::current_time_service current_time_service; + + ble::gatt::service_container service_container; + service_container.push_back(gap_service); + service_container.push_back(battery_service); + service_container.push_back(current_time_service); + + +} +