Skip to content

Commit

Permalink
Add persistent storage test/example
Browse files Browse the repository at this point in the history
Add an example app which tests the KVS api.
  • Loading branch information
Rob Oliver committed Jan 20, 2021
1 parent dc3d90c commit 92fb5c1
Show file tree
Hide file tree
Showing 15 changed files with 1,375 additions and 0 deletions.
9 changes: 9 additions & 0 deletions examples/build_overrides/efr32_sdk.gni
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,16 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import("//build_overrides/chip.gni")
import("//build_overrides/pigweed.gni")

declare_args() {
# Root directory for erf32 SDK.
efr32_sdk_build_root = "//third_party/connectedhomeip/third_party/efr32_sdk"

dir_pw_log_chip =
get_path_info("//../../../src/lib/support/pw_log_chip", "abspath")
}

pw_log_BACKEND = "$dir_pw_log_chip"
pw_assert_BACKEND = "$dir_pw_assert_log"
164 changes: 164 additions & 0 deletions examples/persistent-storage/KeyValueStorageTest.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
/*
*
* Copyright (c) 2021 Project CHIP Authors
* All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include <cstring>
#include <string>

#include <platform/KeyValueStoreManager.h>
#include <support/ErrorStr.h>
#include <support/ReturnMacros.h>
#include <support/logging/CHIPLogging.h>

using namespace chip::DeviceLayer::PersistedStorage;

#define RUN_TEST(test_result) \
do \
{ \
const CHIP_ERROR temp_test_result = test_result; \
if (temp_test_result != CHIP_NO_ERROR) \
{ \
char error_str[255]; \
chip::FormatCHIPError(error_str, sizeof(error_str), temp_test_result); \
ChipLogError(NotSpecified, "%s: FAILED %d [%s]", #test_result, temp_test_result, chip::ErrorStr(temp_test_result)); \
} \
else \
{ \
ChipLogProgress(NotSpecified, "%s: PASSED", #test_result); \
} \
} while (0)

namespace chip {
namespace {

CHIP_ERROR TestEmptyString()
{
const char * kTestKey = "str_key";
const char kTestValue[] = "";
char read_value[sizeof(kTestValue)];
size_t read_size;
ReturnErrorOnFailure(KeyValueStoreMgr().Put(kTestKey, kTestValue));
ReturnErrorOnFailure(KeyValueStoreMgr().Get(kTestKey, read_value, sizeof(read_value), &read_size));
ReturnErrorCodeIf(strcmp(kTestValue, read_value) != 0, CHIP_ERROR_INTERNAL);
ReturnErrorCodeIf(read_size != sizeof(kTestValue), CHIP_ERROR_INTERNAL);
ReturnErrorOnFailure(KeyValueStoreMgr().Delete(kTestKey));
return CHIP_NO_ERROR;
}

CHIP_ERROR TestString()
{
const char * kTestKey = "str_key";
const char kTestValue[] = "test_value";
char read_value[sizeof(kTestValue)];
size_t read_size;
ReturnErrorOnFailure(KeyValueStoreMgr().Put(kTestKey, kTestValue));
ReturnErrorOnFailure(KeyValueStoreMgr().Get(kTestKey, read_value, sizeof(read_value), &read_size));
ReturnErrorCodeIf(strcmp(kTestValue, read_value) != 0, CHIP_ERROR_INTERNAL);
ReturnErrorCodeIf(read_size != sizeof(kTestValue), CHIP_ERROR_INTERNAL);
ReturnErrorOnFailure(KeyValueStoreMgr().Delete(kTestKey));
return CHIP_NO_ERROR;
}

CHIP_ERROR TestUint32()
{
const char * kTestKey = "uint32_key";
uint32_t kTestValue = 5;
uint32_t read_value;
ReturnErrorOnFailure(KeyValueStoreMgr().Put(kTestKey, kTestValue));
ReturnErrorOnFailure(KeyValueStoreMgr().Get(kTestKey, &read_value));
ReturnErrorCodeIf(kTestValue != read_value, CHIP_ERROR_INTERNAL);
ReturnErrorOnFailure(KeyValueStoreMgr().Delete(kTestKey));
return CHIP_NO_ERROR;
}

CHIP_ERROR TestArray()
{
const char * kTestKey = "array_key";
uint32_t kTestValue[5] = { 1, 2, 3, 4, 5 };
uint32_t read_value[5];
ReturnErrorOnFailure(KeyValueStoreMgr().Put(kTestKey, kTestValue));
ReturnErrorOnFailure(KeyValueStoreMgr().Get(kTestKey, &read_value));
ReturnErrorCodeIf(memcmp(kTestValue, read_value, sizeof(kTestValue)) != 0, CHIP_ERROR_INTERNAL);
ReturnErrorOnFailure(KeyValueStoreMgr().Delete(kTestKey));
return CHIP_NO_ERROR;
}

CHIP_ERROR TestStruct()
{
struct SomeStruct
{
uint8_t value1;
uint32_t value2;
};
const char * kTestKey = "struct_key";
SomeStruct kTestValue{ value1 : 1, value2 : 2 };
SomeStruct read_value;
ReturnErrorOnFailure(KeyValueStoreMgr().Put(kTestKey, kTestValue));
ReturnErrorOnFailure(KeyValueStoreMgr().Get(kTestKey, &read_value));
ReturnErrorCodeIf(kTestValue.value1 != read_value.value1, CHIP_ERROR_INTERNAL);
ReturnErrorCodeIf(kTestValue.value2 != read_value.value2, CHIP_ERROR_INTERNAL);
ReturnErrorOnFailure(KeyValueStoreMgr().Delete(kTestKey));
return CHIP_NO_ERROR;
}

CHIP_ERROR TestUpdateValue()
{
const char * kTestKey = "update_key";
uint32_t read_value;
for (size_t i = 0; i < 10; i++)
{
ReturnErrorOnFailure(KeyValueStoreMgr().Put(kTestKey, i));
ReturnErrorOnFailure(KeyValueStoreMgr().Get(kTestKey, &read_value));
ReturnErrorCodeIf(i != read_value, CHIP_ERROR_INTERNAL);
}
ReturnErrorOnFailure(KeyValueStoreMgr().Delete(kTestKey));
return CHIP_NO_ERROR;
}

CHIP_ERROR TestMultiRead()
{
const char * kTestKey = "multi_key";
uint32_t kTestValue[5] = { 1, 2, 3, 4, 5 };
ReturnErrorOnFailure(KeyValueStoreMgr().Put(kTestKey, kTestValue));
for (size_t i = 0; i < 5; i++)
{
uint32_t read_value;
size_t read_size;
// Returns buffer too small for all but the last read.
CHIP_ERROR error = KeyValueStoreMgr().Get(kTestKey, &read_value, sizeof(read_value), &read_size, i * sizeof(uint32_t));
ReturnErrorCodeIf(error != (i < 4 ? CHIP_ERROR_BUFFER_TOO_SMALL : CHIP_NO_ERROR), error);
ReturnErrorCodeIf(read_size != sizeof(read_value), CHIP_ERROR_INTERNAL);
ReturnErrorCodeIf(kTestValue[i] != read_value, CHIP_ERROR_INTERNAL);
}
ReturnErrorOnFailure(KeyValueStoreMgr().Delete(kTestKey));
return CHIP_NO_ERROR;
}

} // namespace

void RunKvsTest()
{
RUN_TEST(TestEmptyString());
RUN_TEST(TestString());
RUN_TEST(TestUint32());
RUN_TEST(TestArray());
RUN_TEST(TestStruct());
RUN_TEST(TestUpdateValue());
RUN_TEST(TestMultiRead());
}

} // namespace chip
25 changes: 25 additions & 0 deletions examples/persistent-storage/KeyValueStorageTest.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
*
* Copyright (c) 2021 Project CHIP Authors
* All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#pragma once

namespace chip {

void RunKvsTest();

} // namespace chip
26 changes: 26 additions & 0 deletions examples/persistent-storage/efr32/.gn
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Copyright (c) 2021 Project CHIP Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# The location of the build configuration file.
buildconfig = "//build/config/BUILDCONFIG.gn"

# CHIP uses angle bracket includes.
check_system_includes = true

default_args = {
target_cpu = "arm"
target_os = "freertos"

import("//args.gni")
}
98 changes: 98 additions & 0 deletions examples/persistent-storage/efr32/BUILD.gn
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
# Copyright (c) 2021 Project CHIP Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import("//build/config/defaults.gni")
import("//build_overrides/chip.gni")
import("//build_overrides/efr32_sdk.gni")
import("//build_overrides/pigweed.gni")
import("${efr32_sdk_build_root}/efr32_executable.gni")
import("${efr32_sdk_build_root}/efr32_sdk.gni")

assert(current_os == "freertos")

efr32_project_dir = "${chip_root}/examples/persistent-storage/efr32"
examples_plat_dir = "${chip_root}/examples/platform/efr32"

efr32_sdk("sdk") {
include_dirs = [
"${chip_root}/src/platform/EFR32",
"${efr32_project_dir}/include",
"${efr32_project_dir}/src",
"${examples_plat_dir}/${efr32_family}/${efr32_board}",
]

sources = [
"${efr32_project_dir}/include/CHIPProjectConfig.h",
"${efr32_project_dir}/include/FreeRTOSConfig.h",
"${examples_plat_dir}/${efr32_family}/${efr32_board}/hal-config.h",
]

defines = [
"CHIP_KVS_SECTOR_COUNT=4",
"CHIP_KVS_BASE_SECTOR_INDEX=((FLASH_SIZE/FLASH_PAGE_SIZE)-(CHIP_KVS_SECTOR_COUNT))",
]

if (is_debug) {
defines += [ "BUILD_RELEASE=0" ]
} else {
defines += [ "BUILD_RELEASE=1" ]
}

defines += [ "BOARD_ID=${efr32_board}" ]
}

efr32_executable("persistent_storage") {
include_dirs = [ "${efr32_project_dir}/.." ]
defines = []
output_name = "chip-efr32-persistent_storage-example.out"

deps = [ "$dir_pw_kvs:crc16" ]
public_deps = [
":sdk",
"$dir_pw_assert",
"${chip_root}/src/lib",
]

include_dirs += [
"${efr32_project_dir}/include",
"${chip_root}/src/app/util",
"${examples_plat_dir}",
]

sources = [
"${efr32_project_dir}/../KeyValueStorageTest.cpp",
"${examples_plat_dir}/${efr32_family}/${efr32_board}/hal-config.h",
"${examples_plat_dir}/${efr32_family}/${efr32_board}/init_board.c",
"${examples_plat_dir}/${efr32_family}/${efr32_board}/init_mcu.c",
"main.cpp",
]

output_dir = root_out_dir

if (efr32_family == "efr32mg12") {
ldscript = "${efr32_project_dir}/ldscripts/efr32-persistent_storage-example-MG12P.ld"
} else if (efr32_family == "efr32mg21") {
ldscript = "${efr32_project_dir}/ldscripts/efr32-persistent_storage-example-MG21.ld"
}

ldflags = [ "-T" + rebase_path(ldscript, root_build_dir) ]
}

group("efr32") {
deps = [ ":persistent_storage" ]
}

group("default") {
deps = [ ":efr32" ]
}
Loading

0 comments on commit 92fb5c1

Please sign in to comment.