takashi kadono / Mbed OS Nucleo_446

Dependencies:   ssd1331

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers example4.cpp Source File

example4.cpp

Go to the documentation of this file.
00001 /*
00002  * mbed Microcontroller Library
00003  * Copyright (c) 2006-2016 ARM Limited
00004  *
00005  * Licensed under the Apache License, Version 2.0 (the "License");
00006  * you may not use this file except in compliance with the License.
00007  * You may obtain a copy of the License at
00008  *
00009  *     http://www.apache.org/licenses/LICENSE-2.0
00010  *
00011  * Unless required by applicable law or agreed to in writing, software
00012  * distributed under the License is distributed on an "AS IS" BASIS,
00013  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014  * See the License for the specific language governing permissions and
00015  * limitations under the License.
00016  *
00017  */
00018 
00019 /** @file example4.cpp Test case to demonstrate a subset of the API functions each work correctly.
00020  *
00021  * Test case created from Issue 10 code supplied by Motti Gondabi. The code:
00022  * - creates a KV
00023  * - writes the KV
00024  * - closes the KV
00025  * - flushes the KV
00026  * - opens the KV.
00027  * - deletes the KV.
00028  * - flushes empty configuration store.
00029  *
00030  * The test case makes sure that the implementation can flush an empty configuration store
00031  * without causing errors. This has only been possible since flash-journal-strategy-sequential
00032  * v0.4.0.
00033  */
00034 
00035 #include "mbed.h"
00036 #include "cfstore_config.h"
00037 #include "cfstore_test.h"
00038 #include "cfstore_debug.h"
00039 #include "Driver_Common.h"
00040 #include "configuration_store.h"
00041 #include "utest/utest.h"
00042 #include "unity/unity.h"
00043 #include "greentea-client/test_env.h"
00044 
00045 #include <stdio.h>
00046 #include <stdlib.h>
00047 #include <string.h>
00048 #include <assert.h>
00049 #include <inttypes.h>
00050 
00051 using namespace utest::v1;
00052 
00053 static char cfstore_example4_utest_msg_g[CFSTORE_UTEST_MSG_BUF_SIZE];
00054 
00055 /* defines */
00056 /// @cond CFSTORE_DOXYGEN_DISABLE
00057 #define PvMemSet memset
00058 #define PvStrLen strlen
00059 #define PvKeyValue_t cfstore_kv_data_t
00060 
00061 ARM_CFSTORE_DRIVER *gCfStoreDriver = &cfstore_driver;
00062 /// @endcond
00063 
00064 static control_t cfstore_example4_test_00(const size_t call_count)
00065 {
00066     int32_t ret = ARM_DRIVER_ERROR;
00067     ARM_CFSTORE_CAPABILITIES caps;;
00068 
00069     (void) call_count;
00070 
00071     /* initialise the context */
00072     caps = gCfStoreDriver->GetCapabilities();
00073     CFSTORE_LOG("%s:INITIALIZING: caps.asynchronous_ops=%lu\n", __func__, caps.asynchronous_ops);
00074     if(caps.asynchronous_ops == 1){
00075         /* This is a sync mode only test. If this test is not built for sync mode, then skip testing return true
00076          * This means the test will conveniently pass when run in CI as part of async mode testing */
00077         CFSTORE_LOG("*** Skipping test as binary built for flash journal async mode, and this test is sync-only%s", "\n");
00078         return CaseNext;
00079     }
00080     ret = cfstore_test_startup();
00081     CFSTORE_TEST_UTEST_MESSAGE(cfstore_example4_utest_msg_g, CFSTORE_UTEST_MSG_BUF_SIZE, "%s:Error: failed to perform test startup (ret=%d).\n", __func__, (int) ret);
00082     TEST_ASSERT_MESSAGE(ret >= ARM_DRIVER_OK, cfstore_example4_utest_msg_g);
00083     return CaseNext;
00084 }
00085 
00086 
00087 /* used for sync mode build only */
00088 #if defined STORAGE_DRIVER_CONFIG_HARDWARE_MTD_ASYNC_OPS && STORAGE_DRIVER_CONFIG_HARDWARE_MTD_ASYNC_OPS==0
00089 
00090 static const PvKeyValue_t testDataKeyValue[] = {
00091     { "com.arm.mbed.spv.assets.dtls", "This Is my DTLS Secret" },
00092     { "com.arm.mbed.spv.assets.asset1.payload", "The Rolling Stone" },
00093     { "com.arm.mbed.spv.assets.asset2.payload", "Grumpy old man" },
00094     { "com.arm.mbed.spv.assets.asset3.payload", "Delete this asset payload" },
00095 };
00096 
00097 
00098 static int32_t CreateKeyValueStore(
00099     const char *keyName,
00100     const char *data,
00101     ARM_CFSTORE_SIZE *dataLength,
00102     ARM_CFSTORE_KEYDESC *keyDesc)
00103 {
00104     int32_t cfsStatus = ARM_DRIVER_ERROR;
00105     ARM_CFSTORE_SIZE valueLength = 0;
00106     ARM_CFSTORE_HANDLE_INIT(hkey);
00107 
00108     valueLength = *dataLength;
00109     cfsStatus = gCfStoreDriver->Create(keyName, valueLength, keyDesc, hkey);
00110     CFSTORE_TEST_UTEST_MESSAGE(cfstore_example4_utest_msg_g, CFSTORE_UTEST_MSG_BUF_SIZE, "%s:Error: Create() failed (cfsStatus=%d)\n", __func__, (int) cfsStatus);
00111     TEST_ASSERT_MESSAGE(cfsStatus >= ARM_DRIVER_OK, cfstore_example4_utest_msg_g);
00112 
00113     valueLength = *dataLength;
00114     cfsStatus = gCfStoreDriver->Write(hkey, data, &valueLength);
00115     CFSTORE_TEST_UTEST_MESSAGE(cfstore_example4_utest_msg_g, CFSTORE_UTEST_MSG_BUF_SIZE, "%s:Error: Write() failed (cfsStatus=%d)\n", __func__, (int) cfsStatus);
00116     TEST_ASSERT_MESSAGE(cfsStatus >= ARM_DRIVER_OK, cfstore_example4_utest_msg_g);
00117     CFSTORE_TEST_UTEST_MESSAGE(cfstore_example4_utest_msg_g, CFSTORE_UTEST_MSG_BUF_SIZE, "%s:Error: valueLength != *dataLength\n", __func__);
00118     TEST_ASSERT_MESSAGE(valueLength == *dataLength, cfstore_example4_utest_msg_g);
00119 
00120     cfsStatus = gCfStoreDriver->Close(hkey);
00121     CFSTORE_TEST_UTEST_MESSAGE(cfstore_example4_utest_msg_g, CFSTORE_UTEST_MSG_BUF_SIZE, "%s:Error: Close() failed (cfsStatus=%d)\n", __func__, (int) cfsStatus);
00122     TEST_ASSERT_MESSAGE(cfsStatus >= ARM_DRIVER_OK, cfstore_example4_utest_msg_g);
00123 
00124     cfsStatus = gCfStoreDriver->Flush();
00125     CFSTORE_TEST_UTEST_MESSAGE(cfstore_example4_utest_msg_g, CFSTORE_UTEST_MSG_BUF_SIZE, "%s:Error: Flush() failed (cfsStatus=%d)\n", __func__, (int) cfsStatus);
00126     TEST_ASSERT_MESSAGE(cfsStatus >= ARM_DRIVER_OK, cfstore_example4_utest_msg_g);
00127 
00128     return ARM_DRIVER_OK;
00129 }
00130 
00131 
00132 static control_t cfstore_example4_test_01(const size_t call_count)
00133 {
00134     int32_t cfsStatus = ARM_DRIVER_ERROR;
00135     ARM_CFSTORE_KEYDESC kdesc;
00136     ARM_CFSTORE_SIZE valueLen;
00137     ARM_CFSTORE_FMODE flags;
00138     ARM_CFSTORE_HANDLE_INIT(hkey);
00139 
00140     (void) call_count;
00141     PvMemSet(&kdesc, 0, sizeof(kdesc));
00142 
00143     kdesc.drl = ARM_RETENTION_NVM;
00144     valueLen = PvStrLen(testDataKeyValue[0].value);
00145 
00146     cfsStatus = gCfStoreDriver->Initialize(NULL, NULL);
00147     CFSTORE_TEST_UTEST_MESSAGE(cfstore_example4_utest_msg_g, CFSTORE_UTEST_MSG_BUF_SIZE, "%s:Error: Initialize() failed (cfsStatus=%d)\n", __func__, (int) cfsStatus);
00148     TEST_ASSERT_MESSAGE(cfsStatus >= ARM_DRIVER_OK, cfstore_example4_utest_msg_g);
00149 
00150     cfsStatus = CreateKeyValueStore(testDataKeyValue[0].key_name, testDataKeyValue[0].value, &valueLen, &kdesc);
00151     CFSTORE_TEST_UTEST_MESSAGE(cfstore_example4_utest_msg_g, CFSTORE_UTEST_MSG_BUF_SIZE, "%s:Error: CreateKeyValueStore() failed (cfsStatus=%d)\n", __func__, (int) cfsStatus);
00152     TEST_ASSERT_MESSAGE(cfsStatus >= ARM_DRIVER_OK, cfstore_example4_utest_msg_g);
00153 
00154     PvMemSet(&flags, 0, sizeof(flags));
00155 
00156     cfsStatus = gCfStoreDriver->Open(testDataKeyValue[0].key_name, flags, hkey);
00157     CFSTORE_TEST_UTEST_MESSAGE(cfstore_example4_utest_msg_g, CFSTORE_UTEST_MSG_BUF_SIZE, "%s:Error: Open() failed (cfsStatus=%d)\n", __func__, (int) cfsStatus);
00158     TEST_ASSERT_MESSAGE(cfsStatus >= ARM_DRIVER_OK, cfstore_example4_utest_msg_g);
00159 
00160     cfsStatus = gCfStoreDriver->Delete(hkey);
00161     CFSTORE_TEST_UTEST_MESSAGE(cfstore_example4_utest_msg_g, CFSTORE_UTEST_MSG_BUF_SIZE, "%s:Error: Delete() failed (cfsStatus=%d)\n", __func__, (int) cfsStatus);
00162     TEST_ASSERT_MESSAGE(cfsStatus >= ARM_DRIVER_OK, cfstore_example4_utest_msg_g);
00163 
00164     cfsStatus = gCfStoreDriver->Close(hkey);
00165     CFSTORE_TEST_UTEST_MESSAGE(cfstore_example4_utest_msg_g, CFSTORE_UTEST_MSG_BUF_SIZE, "%s:Error: Close() failed (cfsStatus=%d)\n", __func__, (int) cfsStatus);
00166     TEST_ASSERT_MESSAGE(cfsStatus >= ARM_DRIVER_OK, cfstore_example4_utest_msg_g);
00167 
00168     cfsStatus = gCfStoreDriver->Flush();
00169     CFSTORE_TEST_UTEST_MESSAGE(cfstore_example4_utest_msg_g, CFSTORE_UTEST_MSG_BUF_SIZE, "%s:Error: Flush() failed (cfsStatus=%d)\n", __func__, (int) cfsStatus);
00170     TEST_ASSERT_MESSAGE(cfsStatus >= ARM_DRIVER_OK, cfstore_example4_utest_msg_g);
00171 
00172     cfsStatus = gCfStoreDriver->Uninitialize();
00173     CFSTORE_TEST_UTEST_MESSAGE(cfstore_example4_utest_msg_g, CFSTORE_UTEST_MSG_BUF_SIZE, "%s:Error: Uninitialize() failed (cfsStatus=%d)\n", __func__, (int) cfsStatus);
00174     TEST_ASSERT_MESSAGE(cfsStatus >= ARM_DRIVER_OK, cfstore_example4_utest_msg_g);
00175 
00176     return CaseNext;
00177 }
00178 #endif // STORAGE_DRIVER_CONFIG_HARDWARE_MTD_ASYNC_OPS
00179 
00180 
00181 /// @cond CFSTORE_DOXYGEN_DISABLE
00182 utest::v1::status_t greentea_setup(const size_t number_of_cases)
00183 {
00184     GREENTEA_SETUP(400, "default_auto");
00185     return greentea_test_setup_handler(number_of_cases);
00186 }
00187 
00188 Case cases[] = {
00189            /*          1         2         3         4         5         6        7  */
00190            /* 1234567890123456789012345678901234567890123456789012345678901234567890 */
00191         Case("EXAMPLE4_test_00", cfstore_example4_test_00),
00192 #if defined STORAGE_DRIVER_CONFIG_HARDWARE_MTD_ASYNC_OPS && STORAGE_DRIVER_CONFIG_HARDWARE_MTD_ASYNC_OPS==0
00193         Case("EXAMPLE4_test_01", cfstore_example4_test_01),
00194 #endif // STORAGE_DRIVER_CONFIG_HARDWARE_MTD_ASYNC_OPS
00195 };
00196 
00197 
00198 /* Declare your test specification with a custom setup handler */
00199 Specification specification(greentea_setup, cases);
00200 
00201 int main()
00202 {
00203     return !Harness::run(specification);
00204 }
00205 /// @endcond