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 15, 2021
1 parent 0da2dc3 commit 4a772a3
Show file tree
Hide file tree
Showing 15 changed files with 1,378 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"
165 changes: 165 additions & 0 deletions examples/persistent-storage/KeyValueStorageTest.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
/*
*
* Copyright (c) 2020 Project CHIP Authors
* Copyright (c) 2019 Google LLC.
* 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 <string>

#include "KeyValueStorage.h"
#include <support/ErrorStr.h>
#include <support/logging/CHIPLogging.h>

using namespace chip::Platform::PersistedStorage;

#define RETURN_CHIP_ERROR_IF(cond, chip_error) \
do \
{ \
if (cond) \
{ \
return (chip_error); \
} \
} while (0)

#define RETURN_IF_CHIP_ERROR(expr) \
do \
{ \
const CHIP_ERROR temp_chip_error = (expr); \
RETURN_CHIP_ERROR_IF(temp_chip_error != CHIP_NO_ERROR, temp_chip_error); \
} while (0)

#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 TestString()
{
const char * kTestKey = "str_key";
const char kTestValue[] = "test_value";
char read_value[sizeof(kTestValue)];
size_t read_size;
RETURN_IF_CHIP_ERROR(ChipKeyValueStoreInstance().Put(kTestKey, kTestValue));
RETURN_IF_CHIP_ERROR(ChipKeyValueStoreInstance().Get(kTestKey, read_value, sizeof(read_value), &read_size));
RETURN_CHIP_ERROR_IF(strcmp(kTestValue, read_value) != 0, CHIP_ERROR_INTERNAL);
RETURN_CHIP_ERROR_IF(read_size != sizeof(kTestValue), CHIP_ERROR_INTERNAL);
RETURN_IF_CHIP_ERROR(ChipKeyValueStoreInstance().Delete(kTestKey));
return CHIP_NO_ERROR;
}

CHIP_ERROR TestUint32()
{
const char * kTestKey = "uint32_key";
uint32_t kTestValue = 5;
uint32_t read_value;
RETURN_IF_CHIP_ERROR(ChipKeyValueStoreInstance().Put(kTestKey, kTestValue));
RETURN_IF_CHIP_ERROR(ChipKeyValueStoreInstance().Get(kTestKey, &read_value));
RETURN_CHIP_ERROR_IF(kTestValue != read_value, CHIP_ERROR_INTERNAL);
RETURN_IF_CHIP_ERROR(ChipKeyValueStoreInstance().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];
RETURN_IF_CHIP_ERROR(ChipKeyValueStoreInstance().Put(kTestKey, kTestValue));
RETURN_IF_CHIP_ERROR(ChipKeyValueStoreInstance().Get(kTestKey, &read_value));
RETURN_CHIP_ERROR_IF(memcmp(kTestValue, read_value, sizeof(kTestValue)) != 0, CHIP_ERROR_INTERNAL);
RETURN_IF_CHIP_ERROR(ChipKeyValueStoreInstance().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;
RETURN_IF_CHIP_ERROR(ChipKeyValueStoreInstance().Put(kTestKey, kTestValue));
RETURN_IF_CHIP_ERROR(ChipKeyValueStoreInstance().Get(kTestKey, &read_value));
RETURN_CHIP_ERROR_IF(kTestValue.value1 != read_value.value1, CHIP_ERROR_INTERNAL);
RETURN_CHIP_ERROR_IF(kTestValue.value2 != read_value.value2, CHIP_ERROR_INTERNAL);
RETURN_IF_CHIP_ERROR(ChipKeyValueStoreInstance().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++)
{
RETURN_IF_CHIP_ERROR(ChipKeyValueStoreInstance().Put(kTestKey, i));
RETURN_IF_CHIP_ERROR(ChipKeyValueStoreInstance().Get(kTestKey, &read_value));
RETURN_CHIP_ERROR_IF(i != read_value, CHIP_ERROR_INTERNAL);
}
RETURN_IF_CHIP_ERROR(ChipKeyValueStoreInstance().Delete(kTestKey));
return CHIP_NO_ERROR;
}

CHIP_ERROR TestMultiRead()
{
const char * kTestKey = "multi_key";
uint32_t kTestValue[5] = { 1, 2, 3, 4, 5 };
RETURN_IF_CHIP_ERROR(ChipKeyValueStoreInstance().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 =
ChipKeyValueStoreInstance().Get(kTestKey, &read_value, sizeof(read_value), &read_size, i * sizeof(uint32_t));
RETURN_CHIP_ERROR_IF(error != (i < 4 ? CHIP_ERROR_BUFFER_TOO_SMALL : CHIP_NO_ERROR), error);
RETURN_CHIP_ERROR_IF(read_size != sizeof(read_value), CHIP_ERROR_INTERNAL);
RETURN_CHIP_ERROR_IF(kTestValue[i] != read_value, CHIP_ERROR_INTERNAL);
}
RETURN_IF_CHIP_ERROR(ChipKeyValueStoreInstance().Delete(kTestKey));
return CHIP_NO_ERROR;
}

} // namespace

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

} // namespace chip
26 changes: 26 additions & 0 deletions examples/persistent-storage/KeyValueStorageTest.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
*
* Copyright (c) 2020 Project CHIP Authors
* Copyright (c) 2018 Nest Labs, Inc.
* 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
153 changes: 153 additions & 0 deletions examples/persistent-storage/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
# CHIP EFR32 Lighting Example

An example testing and demonstrating the key value storage API.

<hr>

- [CHIP EFR32 Lighting Example](#chip-efr32-lighting-example)
- [Introduction](#introduction)
- [EFR32](#efr32)
- [Building](#building)
- [Flashing the Application](#flashing-the-application)
- [Viewing Logging Output](#viewing-logging-output)

<hr>

<a name="intro"></a>

## Introduction

This example serves to both test the key value storage implementation and API as
it is brought-up on different platforms, as well as provide an example for how
to use the API.

In the future this example can be moved into a unit test when available on all
platforms.

<a name="EFR32"></a>

## EFR32

The EFR32 platform KVS is fully implemented, the KVS is enabled and configured
using theese defines:

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

<a name="building"></a>

### Building

- Download the [sdk_support](https://github.com/SiliconLabs/sdk_support) from
GitHub and export the path with :

$ export EFR32_SDK_ROOT=<Path to cloned git repo>

- Download the
[Simplicity Commander](https://www.silabs.com/mcu/programming-options)
command line tool, and ensure that `commander` is your shell search path.
(For Mac OS X, `commander` is located inside
`Commander.app/Contents/MacOS/`.)

- Download and install a suitable ARM gcc tool chain:
[GNU Arm Embedded Toolchain 9-2019-q4-major](https://developer.arm.com/tools-and-software/open-source-software/developer-tools/gnu-toolchain/gnu-rm/downloads)

- Install some additional tools(likely already present for CHIP developers):

# Linux
$ sudo apt-get install git libwebkitgtk-1.0-0 ninja-build

# Mac OS X
$ brew install ninja

- Supported hardware:

MG12 boards:

- BRD4161A / SLWSTK6000B / Wireless Starter Kit / 2.4GHz@19dBm
- BRD4166A / SLTB004A / Thunderboard Sense 2 / 2.4GHz@10dBm
- BRD4170A / SLWSTK6000B / Multiband Wireless Starter Kit / 2.4GHz@19dBm,
915MHz@19dBm
- BRD4304A / SLWSTK6000B / MGM12P Module / 2.4GHz@19dBm

MG21 boards:

- BRD4180A / SLWSTK6006A / Wireless Starter Kit / 2.4GHz@20dBm

* Build the example application:

$ cd ~/connectedhomeip/examples/persistent-storage/efr32
$ git submodule update --init
$ source third_party/connectedhomeip/scripts/activate.sh
$ export EFR32_SDK_ROOT=<path-to-silabs-sdk-v2.7>
$ export EFR32_BOARD=BRD4161A
$ gn gen out/debug --args="efr32_sdk_root=\"${EFR32_SDK_ROOT}\" efr32_board=\"${EFR32_BOARD}\""
$ ninja -C out/debug

- To delete generated executable, libraries and object files use:

$ cd ~/connectedhomeip/examples/persistent-storage/efr32
$ rm -rf out/

<a name="flashing"></a>

### Flashing the Application

- On the command line:

$ cd ~/connectedhomeip/examples/persistent-storage/efr32
$ python3 out/debug/chip-efr32-persistent_storage-example.flash.py

- Or with the Ozone debugger, just load the .out file.

<a name="view-logging"></a>

### Viewing Logging Output

The example application is built to use the SEGGER Real Time Transfer (RTT)
facility for log output. RTT is a feature built-in to the J-Link Interface MCU
on the WSTK development board. It allows bi-directional communication with an
embedded application without the need for a dedicated UART.

Using the RTT facility requires downloading and installing the _SEGGER J-Link
Software and Documentation Pack_
([web site](https://www.segger.com/downloads/jlink#J-LinkSoftwareAndDocumentationPack)).
Alternatively the _SEGGER Ozone - J-Link Debugger_ can be used to view RTT logs.

- Download the J-Link installer by navigating to the appropriate URL and
agreeing to the license agreement.

- [JLink_Linux_x86_64.deb](https://www.segger.com/downloads/jlink/JLink_Linux_x86_64.deb)
- [JLink_MacOSX.pkg](https://www.segger.com/downloads/jlink/JLink_MacOSX.pkg)

* Install the J-Link software

$ cd ~/Downloads
$ sudo dpkg -i JLink_Linux_V*_x86_64.deb

* In Linux, grant the logged in user the ability to talk to the development
hardware via the linux tty device (/dev/ttyACMx) by adding them to the
dialout group.

$ sudo usermod -a -G dialout ${USER}

Once the above is complete, log output can be viewed using the JLinkExe tool in
combination with JLinkRTTClient as follows:

- Run the JLinkExe tool with arguments to autoconnect to the WSTK board:

For MG12 use:

$ JLinkExe -device EFR32MG12PXXXF1024 -if JTAG -speed 4000 -autoconnect 1

For MG21 use:

$ JLinkExe -device EFR32MG21AXXXF1024 -if SWD -speed 4000 -autoconnect 1

- In a second terminal, run the JLinkRTTClient to view logs:

$ JLinkRTTClient
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) 2020 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")
}
Loading

0 comments on commit 4a772a3

Please sign in to comment.