Skip to content

build(cmake): Add lint rule using clang-format #136

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ if(BUILD_TESTING)
include(cmake/include/unittest.cmake)
endif()

include(cmake/include/clang-format.cmake)
include(cmake/include/doxygen.cmake)

# ##############################################################################
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
#include "attribute.hpp"
#include <vector>

namespace zpc_database_updater {
namespace zpc_database_updater
{

/**
* @brief Update the datastore to the target version
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,17 @@ std::vector<attribute> endpoint_id_list()
return endpoint_list;
}

std::vector<attribute> get_attribute_list(attribute_store_type_t attribute_type) {
std::vector<attribute> get_attribute_list(attribute_store_type_t attribute_type)
{
std::vector<attribute> attribute_list;

auto endpoint_list = endpoint_id_list();

for(auto endpoint: endpoint_list) {
for (auto endpoint: endpoint_list) {
auto attributes = endpoint.children(attribute_type);
attribute_list.insert(attribute_list.end(), attributes.begin(), attributes.end());
attribute_list.insert(attribute_list.end(),
attributes.begin(),
attributes.end());
}

return attribute_list;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
#include "zpc_datastore_fixt.h"
#include "attribute_store_defined_attribute_types.h"

// Interface
// Interface
#include "zpc_database_helper.hpp"

// Unify components
Expand All @@ -25,13 +25,14 @@
#include "attribute_store_type_registration.h"
#include "sl_log.h"

// Datastore
// Datastore
#include "datastore.h"

using namespace attribute_store;
constexpr const char *LOG_TAG = "zpc_database_updater";

namespace zpc_database_updater {
namespace zpc_database_updater
{
///////////////////////////////////////////////////////////////////////////////
// Helper function
///////////////////////////////////////////////////////////////////////////////
Expand All @@ -49,17 +50,16 @@ bool update_bitmask_attribute(attribute attribute_to_update)
std::vector<uint8_t> current_value
= attribute_to_update.reported<std::vector<uint8_t>>();

auto bitmask_length = current_value.size();
auto bitmask_length = current_value.size();
uint32_t new_bitmask = 0;

// Since we are using uint32_t we can't have more that 4 bit mask
if (bitmask_length > 4 || bitmask_length == 0) {
if (bitmask_length > 4 || bitmask_length == 0) {
return false;
}

for (int i = bitmask_length - 1; i >= 0; i--) {
new_bitmask
= (new_bitmask << 8) | current_value[i];
new_bitmask = (new_bitmask << 8) | current_value[i];
}

attribute_to_update.set_reported<uint32_t>(new_bitmask);
Expand All @@ -76,7 +76,8 @@ bool update_bitmask_attribute(attribute attribute_to_update)
void helper_convert_bitmask_to_new_format(attribute_store_type_t attribute_type)
{
auto attribute_list = get_attribute_list(attribute_type);
const std::string attribute_name = attribute_store_get_type_name(attribute_type);
const std::string attribute_name
= attribute_store_get_type_name(attribute_type);

if (attribute_list.size() == 0) {
sl_log_info(LOG_TAG,
Expand All @@ -103,9 +104,6 @@ void helper_convert_bitmask_to_new_format(attribute_store_type_t attribute_type)
}
}




///////////////////////////////////////////////////////////////////////////////
// Conversion functions
///////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -210,7 +208,8 @@ sl_status_t convert_v1_datastore_to_v2()
}

// See zpc_datastore_version for details on the versioning
sl_status_t convert_v2_datastore_to_v3() {
sl_status_t convert_v2_datastore_to_v3()
{
// Update ATTRIBUTE_COMMAND_CLASS_THERMOSTAT_SUPPORTED_MODES
helper_convert_bitmask_to_new_format(
ATTRIBUTE_COMMAND_CLASS_THERMOSTAT_SUPPORTED_MODES);
Expand All @@ -234,8 +233,7 @@ sl_status_t convert_v2_datastore_to_v3() {
"Updating attribute(s) ID under %s...",
attribute_name.c_str());


// WARNING : Do not use defined type value here. We need those specific ID's from
// WARNING : Do not use defined type value here. We need those specific ID's from
// the v2 database and form the v3 database.
// Specify ID's for the setpoint types since they have changed since v2
constexpr attribute_store_type_t OLD_MIN_VALUE_TYPE = 0x4306;
Expand All @@ -253,18 +251,20 @@ sl_status_t convert_v2_datastore_to_v3() {
// Default precision (keep same behavior as before)
constexpr uint8_t DEFAULT_PRECISION = 3;

for(auto current_setpoint: setpoint_types_list) {
for (auto current_setpoint: setpoint_types_list) {
// Update the scale for value (same ID)
uint32_t old_value_scale
= current_setpoint
.child_by_type(ATTRIBUTE_COMMAND_CLASS_THERMOSTAT_SETPOINT_VALUE_SCALE)
.child_by_type(
ATTRIBUTE_COMMAND_CLASS_THERMOSTAT_SETPOINT_VALUE_SCALE)
.reported<uint32_t>();
// Those cases should NOT happen
if (old_value_scale > 255) {
sl_log_warning(LOG_TAG, "Value scale is too high. Setting it to 255.");
old_value_scale = 255;
}
current_setpoint.child_by_type(ATTRIBUTE_COMMAND_CLASS_THERMOSTAT_SETPOINT_VALUE_SCALE)
current_setpoint
.child_by_type(ATTRIBUTE_COMMAND_CLASS_THERMOSTAT_SETPOINT_VALUE_SCALE)
.set_reported<uint8_t>(old_value_scale);

// First get old values
Expand All @@ -283,12 +283,12 @@ sl_status_t convert_v2_datastore_to_v3() {
if (old_min_value_scale > 255) {
sl_log_warning(LOG_TAG,
"Min value scale is too high. Setting it to 255.");
old_min_value_scale = 255;
old_min_value_scale = 255;
}
if (old_max_value_scale > 255) {
sl_log_warning(LOG_TAG,
"Max value scale is too high. Setting it to 255.");
old_max_value_scale = 255;
old_max_value_scale = 255;
}

// Then update current attribute tree
Expand All @@ -304,23 +304,21 @@ sl_status_t convert_v2_datastore_to_v3() {
// Create the missing ones
current_setpoint.emplace_node<int32_t>(NEW_MAX_VALUE_TYPE, old_max_value);
current_setpoint.emplace_node<uint8_t>(NEW_MAX_VALUE_SCALE_TYPE,
old_max_value_scale);
old_max_value_scale);
current_setpoint.emplace_node<uint8_t>(NEW_MAX_VALUE_PRECISION_TYPE, 3);
}

sl_log_info(LOG_TAG, "Done.");
}

sl_log_info(LOG_TAG, "Successfully converted from version 2 to version 3.\n");
return datastore_store_int("version", DATASTORE_VERSION_V3);
}


///////////////////////////////////////////////////////////////////////////////
// Exposed functions
///////////////////////////////////////////////////////////////////////////////
sl_status_t update_datastore(int64_t datastore_version,
int64_t target_version)
sl_status_t update_datastore(int64_t datastore_version, int64_t target_version)
{
while (datastore_version < target_version) {
switch (datastore_version) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,30 +79,26 @@ void suiteSetUp()
/// Teardown the test suite (called once after all test_xxx functions are called)
int suiteTearDown(int num_failures)
{
return num_failures;
return num_failures;
}

/// Called before each and every test
void setUp()
{

}
void setUp() {}

/// Called after each and every test
void tearDown() {
}
void tearDown() {}

// WARNING : This test is here to cover the v1 to v2 function.
// No test was done by the time, so we don't have a v1 database to test.
// We simulate a v1 empty database and check if the migration is successful.
void test_migration_v1_v2_test()
{
// Current database version
constexpr int64_t BASE_VERSION = 1;
constexpr int64_t BASE_VERSION = 1;
// New version
constexpr int64_t DESTINATION_VERSION = 2;

// This test is here to
// This test is here to
helper_setup_database(":memory:", 1);

// Migrate the datastore
Expand All @@ -116,13 +112,12 @@ void test_migration_v1_v2_test()
void test_migration_v2_v3()
{
// Current database version
constexpr int64_t BASE_VERSION = 2;
constexpr int64_t BASE_VERSION = 2;
// New version
constexpr int64_t DESTINATION_VERSION = 3;

helper_setup_database(TEST_DB_V2_BASE_NAME, BASE_VERSION);


attribute_store_log();

// Migrate the datastore
Expand All @@ -142,40 +137,41 @@ void test_migration_v2_v3()

// Test new attributes

// First of all get the correct endpoint
// attribute_store_log() extract :
// First of all get the correct endpoint
// attribute_store_log() extract :
//
// 59: 2024-Jul-09 14:20:25.685584 <d> [attribute_store] ID: 1 - Root node - Reported (hex): []
// 59: 2024-Jul-09 14:20:25.685690 <d> [attribute_store] ID: 55 - HomeID - Reported (hex): [DF AF 03 F7]
// 59: 2024-Jul-09 14:20:25.685750 <d> [attribute_store] ID: 56 - NodeID - Reported (hex): [01 00]
// 59: 2024-Jul-09 14:20:25.687162 <d> [attribute_store] ID: 73 - NodeID - Reported (hex): [02 00]
// 59: 2024-Jul-09 14:20:25.687233 <d> [attribute_store] ID: 74 - Endpoint ID - Reported (hex): [00]
// 59: 2024-Jul-09 14:29:39.847599 <d> [attribute_store] ID: 75 - Unknown 0xFD020001 - Reported (hex): [00 00 00 00]
// 59: 2024-Jul-09 14:29:39.847676 <d> [attribute_store] ID: 76 - Inclusion protocol - Reported (hex): [00 00 00 00]
// 59: 2024-Jul-09 14:29:39.847755 <d> [attribute_store] ID: 79 - Granted keys - Reported (hex): [00]
// 59: 2024-Jul-09 14:29:39.847828 <d> [attribute_store] ID: 80 - KEX Fail type - Reported (hex): [00 00 00 00]
// 59: 2024-Jul-09 14:29:39.847905 <d> [attribute_store] ID: 81 - NIF: Protocol Listening byte - Reported (hex): [53]
// 59: 2024-Jul-09 14:29:39.847978 <d> [attribute_store] ID: 82 - NIF: Optional Protocol byte - Reported (hex): [DC]
// 59: 2024-Jul-09 14:29:39.848061 <d> [attribute_store] ID: 85 - Version CC is supported - Reported (hex): [01]
// 59: 2024-Jul-09 14:29:39.848135 <d> [attribute_store] ID: 111 - Last Rx/Tx timestamp - Reported (hex): [AF 56 00 00 00 00 00 00]
// 59: 2024-Jul-09 14:29:39.847599 <d> [attribute_store] ID: 75 - Unknown 0xFD020001 - Reported (hex): [00 00 00 00]
// 59: 2024-Jul-09 14:29:39.847676 <d> [attribute_store] ID: 76 - Inclusion protocol - Reported (hex): [00 00 00 00]
// 59: 2024-Jul-09 14:29:39.847755 <d> [attribute_store] ID: 79 - Granted keys - Reported (hex): [00]
// 59: 2024-Jul-09 14:29:39.847828 <d> [attribute_store] ID: 80 - KEX Fail type - Reported (hex): [00 00 00 00]
// 59: 2024-Jul-09 14:29:39.847905 <d> [attribute_store] ID: 81 - NIF: Protocol Listening byte - Reported (hex): [53]
// 59: 2024-Jul-09 14:29:39.847978 <d> [attribute_store] ID: 82 - NIF: Optional Protocol byte - Reported (hex): [DC]
// 59: 2024-Jul-09 14:29:39.848061 <d> [attribute_store] ID: 85 - Version CC is supported - Reported (hex): [01]
// 59: 2024-Jul-09 14:29:39.848135 <d> [attribute_store] ID: 111 - Last Rx/Tx timestamp - Reported (hex): [AF 56 00 00 00 00 00 00]
// 59: 2024-Jul-09 14:20:25.696159 <d> [attribute_store] ID: 175 - Endpoint ID - Reported (hex): [01] << We want him
auto root_node = attribute_store_get_root();
auto root_node = attribute_store_get_root();
auto home_id_node = attribute_store_get_node_child(root_node, 0);
auto node_id_node = attribute_store_get_node_child(home_id_node, 1);
const zwave_endpoint_id_t endpoint_id = 1;
// Need to specify the endpoint ID here
auto endpoint_id_node = attribute_store_get_node_child_by_value(node_id_node,
ATTRIBUTE_ENDPOINT_ID,
REPORTED_ATTRIBUTE,
&endpoint_id,
sizeof(endpoint_id),
0);
auto endpoint_id_node
= attribute_store_get_node_child_by_value(node_id_node,
ATTRIBUTE_ENDPOINT_ID,
REPORTED_ATTRIBUTE,
&endpoint_id,
sizeof(endpoint_id),
0);

// Test new bitmask values
const std::map<attribute_store_type_t, uint32_t> bitmask_attributes = {
{ATTRIBUTE_COMMAND_CLASS_THERMOSTAT_SUPPORTED_MODES, 0x180F },
{ATTRIBUTE_COMMAND_CLASS_THERMOSTAT_SETPOINT_SUPPORTED_SETPOINT_TYPES, 0x0186 }
};
const std::map<attribute_store_type_t, uint32_t> bitmask_attributes
= {{ATTRIBUTE_COMMAND_CLASS_THERMOSTAT_SUPPORTED_MODES, 0x180F},
{ATTRIBUTE_COMMAND_CLASS_THERMOSTAT_SETPOINT_SUPPORTED_SETPOINT_TYPES,
0x0186}};

for (const auto &[attribute_type, expected_value]: bitmask_attributes) {
printf("Testing attribute %s\n",
Expand All @@ -196,8 +192,6 @@ void test_migration_v2_v3()
"New bitmask value mismatch.");
}



// Test new attribute ID values
struct setpoint_value {
int32_t value;
Expand All @@ -206,7 +200,8 @@ void test_migration_v2_v3()
};

// Expected value in each fields
const std::map<uint8_t, std::vector<setpoint_value>> expected_setpoint_attributes
const std::map<uint8_t, std::vector<setpoint_value>>
expected_setpoint_attributes
= {{0x01, {{73000, 1, 3}, {-10000, 0, 3}, {100000, 0, 3}}},
{0x02, {{73000, 1, 3}, {-10000, 0, 3}, {100000, 0, 3}}},
{0x0B, {{62000, 1, 3}, {-10000, 0, 3}, {100000, 0, 3}}},
Expand All @@ -225,7 +220,8 @@ void test_migration_v2_v3()
// MAX_VALUE_PRECISION_TYPE : 0x430C
const attribute_store_type_t START_VALUE_TYPE = 0x4304;

for (const auto &[setpoint_type, expected_values]: expected_setpoint_attributes) {
for (const auto &[setpoint_type, expected_values]:
expected_setpoint_attributes) {
printf("Testing setpoint type %d\n", setpoint_type);

auto setpoint_type_node = attribute_store_get_node_child_by_value(
Expand Down Expand Up @@ -262,14 +258,13 @@ void test_migration_v2_v3()
reported_values.value,
"New setpoint value mismatch.");


// Get scale
// TODO : use defined offset in thermostat setpoint header when available
status
= attribute_store_get_child_reported(setpoint_type_node,
START_VALUE_TYPE + CURRENT_OFFSET + 1,
&reported_values.scale,
sizeof(reported_values.scale));
status = attribute_store_get_child_reported(
setpoint_type_node,
START_VALUE_TYPE + CURRENT_OFFSET + 1,
&reported_values.scale,
sizeof(reported_values.scale));
TEST_ASSERT_EQUAL_MESSAGE(SL_STATUS_OK,
status,
"Failed to get new setpoint scale.");
Expand All @@ -279,11 +274,11 @@ void test_migration_v2_v3()
"New setpoint scale mismatch.");

// Get precision
status
= attribute_store_get_child_reported(setpoint_type_node,
START_VALUE_TYPE + CURRENT_OFFSET + 2,
&reported_values.precision,
sizeof(reported_values.precision));
status = attribute_store_get_child_reported(
setpoint_type_node,
START_VALUE_TYPE + CURRENT_OFFSET + 2,
&reported_values.precision,
sizeof(reported_values.precision));
TEST_ASSERT_EQUAL_MESSAGE(SL_STATUS_OK,
status,
"Failed to get new setpoint precision.");
Expand All @@ -295,6 +290,4 @@ void test_migration_v2_v3()
}
}



} // extern "C"
} // extern "C"
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,9 @@ static void import_data_store(attribute &parent, nlohmann::json &jsn)
sl_log_error("zpc_database_tool", "Missing type attribute in json");
return;
}
std::string type_str = jsn["type"].get<std::string>();
attribute_store_type_t type = attribute_store_get_type_by_name(type_str.c_str());
std::string type_str = jsn["type"].get<std::string>();
attribute_store_type_t type
= attribute_store_get_type_by_name(type_str.c_str());
attribute node;

if (type == ATTRIBUTE_TREE_ROOT) {
Expand Down
Loading
Loading