Skip to content

Commit

Permalink
Add unit tests for service and characteristics classes
Browse files Browse the repository at this point in the history
Issue #5
  • Loading branch information
natersoz committed Feb 18, 2019
1 parent 6a57e1c commit b3c087e
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 5 deletions.
12 changes: 7 additions & 5 deletions ble/gatt_attribute.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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<uint8_t*>(this->data_pointer());
ASSERT(data_dst);

Expand All @@ -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<att::length_t>(length);
memcpy(data_dst, data, dest_length);
return static_cast<att::length_t>(dest_length);
}
else
{
logger::instance().warn("attribute::write: %d", length);
logger::instance().warn("attribute::write: %d", dest_length);
return 0u;
}
}
Expand Down
7 changes: 7 additions & 0 deletions unit_tests/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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/

Expand Down Expand Up @@ -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
Expand All @@ -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.
Expand Down
53 changes: 53 additions & 0 deletions unit_tests/test_service_container.cc
Original file line number Diff line number Diff line change
@@ -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);


}

0 comments on commit b3c087e

Please sign in to comment.