Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers example5.cpp Source File

example5.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  * Test cases to add and delete KVs in the CFSTORE.
00018  */
00019 
00020 
00021 /** @file example5.cpp
00022  *
00023  * Test case to demonstrate each API function works correctly.
00024  *
00025  * \par Example 5 Notes
00026  *
00027  * This flash-journal synchronous mode example test does the following CFSTORE operations:
00028  * - initialises
00029  * - creates a key-value pair (KV).
00030  * - writes the data for the KV
00031  * - closes KV.
00032  * - flushes the KV to flash
00033  * - flushes the KV to flash again
00034  * - uninitialises
00035  * - initialises
00036  * - opens KV for reading.
00037  * - deletes KV
00038  * - closes KV.
00039  * - flushes the updated state to flash to store the removal of the deleted KV.
00040  * - uninitialises
00041  * - stops
00042  *
00043  * This test is coded so as to work only in flash journal sync mode
00044  * i.e. with caps.asynchronous_ops == false
00045  *
00046  * The test leaves the flash in the same state as at the beginning of the test so
00047  * it can be run a second time on the device without flashing, and the test should
00048  * still work.
00049  *
00050  * \par How to Build Example5 as a Stand-alone Application
00051  *
00052  * This example can be build as a stand-alone application as follows:
00053  * - Create a new mbed application using the `mbed new .` command.
00054  * - Copy this file example5.cpp from the to the top level application directory and rename the file to main.cpp.
00055  * - Build the application with `mbed compile -v -m <target> -t <toolchain> -DCFSTORE_EXAMPLE5_APP` e.g. `mbed compile -v -m K64F -t GCC_ARM -DCFSTORE_EXAMPLE5_APP`.
00056  */
00057 
00058 #include "mbed.h"
00059 #include "Driver_Common.h"
00060 
00061 #ifndef CFSTORE_EXAMPLE5_APP
00062 /* when built as Configuration-Store example, include greentea support otherwise omit */
00063 #include "utest/utest.h"
00064 #include "unity/unity.h"
00065 #include "greentea-client/test_env.h"
00066 #else   // CFSTORE_EXAMPLE5_APP
00067 /* map utest types for building as stand alone example */
00068 #define control_t   void
00069 #define CaseNext
00070 #endif  // CFSTORE_EXAMPLE5_APP
00071 
00072 #include "cfstore_config.h"
00073 #include "configuration_store.h"
00074 
00075 #ifdef CFSTORE_CONFIG_BACKEND_FLASH_ENABLED
00076 #include "flash_journal_strategy_sequential.h"
00077 #include "flash_journal.h"
00078 #include "Driver_Common.h"
00079 #endif /* CFSTORE_CONFIG_BACKEND_FLASH_ENABLED */
00080 
00081 #include <stdio.h>
00082 #include <stdlib.h>
00083 #include <string.h>
00084 
00085 
00086 #ifndef CFSTORE_EXAMPLE5_APP
00087 using namespace utest::v1;
00088 #endif
00089 
00090 
00091 /// @cond CFSTORE_DOXYGEN_DISABLE
00092 #define CFSTORE_EX5_TEST_ASSERT(Expr)                       if (!(Expr)) { printf("%s:%u: assertion failure\r\n", __FUNCTION__, __LINE__); while (1) ;}
00093 #define CFSTORE_EX5_TEST_ASSERT_EQUAL(expected, actual)     if ((expected) != (actual)) {printf("%s:%u: assertion failure\r\n", __FUNCTION__, __LINE__); while (1) ;}
00094 #define CFSTORE_EX5_TEST_ASSERT_NOT_EQUAL(expected, actual) if ((expected) == (actual)) {printf("%s:%u: assertion failure\r\n", __FUNCTION__, __LINE__); while (1) ;}
00095 
00096 #define CFSTORE_EX5_TEST_ASSERT_MSG(Expr, _fmt, ...)      \
00097     do                                                    \
00098     {                                                     \
00099         if (!(Expr))                                      \
00100         {                                                 \
00101             printf(_fmt, __VA_ARGS__);                    \
00102             while (1) ;                                   \
00103         }                                                 \
00104     }while(0);
00105 
00106 #define CFSTORE_EX5_LOG(_fmt, ...)                        \
00107   do                                                      \
00108   {                                                       \
00109         printf(_fmt, __VA_ARGS__);                        \
00110   }while(0);
00111 
00112 
00113 const char* cfstore_ex5_opcode_str[] =
00114 {
00115     "UNDEFINED",
00116     "CFSTORE_OPCODE_CLOSE",
00117     "CFSTORE_OPCODE_CREATE",
00118     "CFSTORE_OPCODE_DELETE",
00119     "CFSTORE_OPCODE_FIND",
00120     "CFSTORE_OPCODE_FLUSH",
00121     "CFSTORE_OPCODE_GET_KEY_NAME",
00122     "CFSTORE_OPCODE_GET_STATUS",
00123     "CFSTORE_OPCODE_GET_VALUE_LEN",
00124     "CFSTORE_OPCODE_INITIALIZE",
00125     "CFSTORE_OPCODE_OPEN",
00126     "CFSTORE_OPCODE_POWER_CONTROL",
00127     "CFSTORE_OPCODE_READ",
00128     "CFSTORE_OPCODE_RSEEK",
00129     "CFSTORE_OPCODE_UNINITIALIZE",
00130     "CFSTORE_OPCODE_WRITE",
00131     "CFSTORE_OPCODE_MAX"
00132 };
00133 
00134 const char* cfstore_ex5_kv_name = "basement.medicine.pavement.government.trenchcoat.off.cough.off.kid.did.when.again.alleyway.friend.cap.pen.dollarbills.ten.foot.soot.put.but.anyway.say.May.DA.kid.did.toes.bows.those.hose.nose.clothes.man.blows.well.well";
00135 const char* cfstore_ex5_kv_value = "TheRollingStone";
00136 #define CFSTORE_EX5_RSEEK_OFFSET    10   /* offset to S of Stone */
00137 
00138 /// @cond CFSTORE_DOXYGEN_DISABLE
00139 typedef struct cfstore_EXAMPLE5_ctx_t
00140 {
00141     ARM_CFSTORE_CAPABILITIES caps;
00142     uint8_t hkey[CFSTORE_HANDLE_BUFSIZE];
00143     uint8_t hkey_next_buf[CFSTORE_HANDLE_BUFSIZE];
00144     uint8_t hkey_prev_buf[CFSTORE_HANDLE_BUFSIZE];
00145     ARM_CFSTORE_HANDLE  hkey_next;
00146     ARM_CFSTORE_HANDLE  hkey_prev;
00147     ARM_CFSTORE_SIZE len;
00148     ARM_CFSTORE_KEYDESC kdesc;
00149     ARM_CFSTORE_FMODE flags;
00150     char value[CFSTORE_KEY_NAME_MAX_LENGTH+1];
00151 } cfstore_EXAMPLE5_ctx_t;
00152 
00153 static cfstore_EXAMPLE5_ctx_t cfstore_EXAMPLE5_ctx_g;
00154 
00155 extern ARM_CFSTORE_DRIVER cfstore_driver;
00156 ARM_CFSTORE_DRIVER *cfstore_drv = &cfstore_driver;
00157 /// @endcond
00158 
00159 
00160 /* @brief   test startup code to reset flash
00161  */
00162 int32_t cfstore_test_startup(void)
00163 {
00164     ARM_CFSTORE_CAPABILITIES caps = cfstore_driver.GetCapabilities();
00165     CFSTORE_EX5_LOG("INITIALIZING: caps.asynchronous_ops=%d\n", (int) caps.asynchronous_ops);
00166 
00167 #ifdef CFSTORE_CONFIG_BACKEND_FLASH_ENABLED
00168     int32_t ret = ARM_DRIVER_ERROR;
00169     static FlashJournal_t jrnl;
00170     extern ARM_DRIVER_STORAGE ARM_Driver_Storage_MTD_K64F;
00171     const ARM_DRIVER_STORAGE *drv = &ARM_Driver_Storage_MTD_K64F;
00172 
00173     ret = FlashJournal_initialize(&jrnl, drv, &FLASH_JOURNAL_STRATEGY_SEQUENTIAL, NULL);
00174     CFSTORE_EX5_TEST_ASSERT_MSG(ret >= JOURNAL_STATUS_OK, "%s:Error: FlashJournal_initialize() failed (ret=%d)\r\n", __func__, (int) ret);
00175 
00176     ret = FlashJournal_reset(&jrnl);
00177     CFSTORE_EX5_TEST_ASSERT_MSG(ret >= JOURNAL_STATUS_OK, "%s:Error: FlashJournal_reset() failed (ret=%d)\r\n", __func__, (int) ret);
00178 #endif /*  CFSTORE_CONFIG_BACKEND_FLASH_ENABLED */
00179 
00180     return ARM_DRIVER_OK;
00181 }
00182 
00183 
00184 static void cfstore_ex5_test_01(cfstore_EXAMPLE5_ctx_t* ctx)
00185 {
00186     int32_t ret;
00187 
00188     CFSTORE_EX5_LOG("INITIALIZING1%s", "\r\n");
00189     ret = cfstore_drv->Initialize(NULL, NULL);
00190     CFSTORE_EX5_TEST_ASSERT_MSG(ret >= ARM_DRIVER_OK, "%s:Error: Initialize() should return ret >= 0 for async/synch modes(ret=%ld)\r\n", __func__, ret);
00191 
00192     CFSTORE_EX5_LOG("CREATING%s", "\r\n");
00193     memset(&ctx->kdesc, 0, sizeof(ARM_CFSTORE_KEYDESC));
00194     ctx->kdesc.drl = ARM_RETENTION_NVM;
00195     ctx->len = strlen(cfstore_ex5_kv_value);
00196     ret = cfstore_drv->Create(cfstore_ex5_kv_name, ctx->len, &ctx->kdesc, ctx->hkey);
00197     CFSTORE_EX5_TEST_ASSERT_MSG(ret >= ARM_DRIVER_OK, "%s:Error: Create() failed (ret=%ld)\r\n", __func__, ret);
00198 
00199     CFSTORE_EX5_LOG("WRITING%s", "\r\n");
00200     ctx->len = strlen(cfstore_ex5_kv_value);
00201     ret = cfstore_drv->Write(ctx->hkey, cfstore_ex5_kv_value, &ctx->len);
00202     CFSTORE_EX5_TEST_ASSERT_MSG(ret >= ARM_DRIVER_OK, "%s:Error: Write() failed (ret=%ld)\r\n", __func__, ret);
00203 
00204     CFSTORE_EX5_TEST_ASSERT_MSG(ret == (int32_t) strlen(cfstore_ex5_kv_value), "%s:Error: Write() number of octets written (i.e. completion status (%ld)) != strlen(ctx->value)(%ld)\r\n", __func__, ret, (int32_t) strlen(cfstore_ex5_kv_value));
00205     CFSTORE_EX5_TEST_ASSERT_MSG(ret == (int32_t) ctx->len, "%s:Error: Write() number of octets written (i.e. completion status (%ld)) != updated value of len parameter (%ld)\r\n", __func__, ret, (int32_t) ctx->len);
00206 
00207     CFSTORE_EX5_LOG("CLOSING1%s", "\r\n");
00208     ret = cfstore_drv->Close(ctx->hkey);
00209     CFSTORE_EX5_TEST_ASSERT_MSG(ret >= ARM_DRIVER_OK, "%s:Error: Close() failed (ret=%ld)\r\n", __func__, ret);
00210 
00211     CFSTORE_EX5_LOG("FLUSHING1%s", "\r\n");
00212     ret = cfstore_drv->Flush();
00213     CFSTORE_EX5_TEST_ASSERT_MSG(ret >= ARM_DRIVER_OK, "%s:Error: Flush() failed (ret=%ld)\r\n", __func__, ret);
00214 
00215 #ifdef CFSTORE_CONFIG_BACKEND_FLASH_ENABLED
00216     /* CFSTORE_CONFIG_BACKEND_FLASH_ENABLED => flash storage support present.
00217      * if this was not the case (i.e. cfstore running SRAM in memory mode) then
00218      * we dont compile in the  Uninitialize()/Initialize() as the
00219      * Uninitialize() clears the sram */
00220     CFSTORE_EX5_LOG("UNINITIALIZING1%s", "\r\n");
00221     ret = cfstore_drv->Uninitialize();
00222     CFSTORE_EX5_TEST_ASSERT_MSG(ret >= ARM_DRIVER_OK, "%s:Error: Uninitialize() should return ret >= 0 for synch mode(ret=%ld)\r\n", __func__, ret);
00223 
00224     CFSTORE_EX5_LOG("INITIALIZING2%s", "\r\n");
00225     ret = cfstore_drv->Initialize(NULL, NULL);
00226     CFSTORE_EX5_TEST_ASSERT_MSG(ret >= ARM_DRIVER_OK, "%s:Error: Initialize() should return ret >= 0 for async/synch modes(ret=%ld)\r\n", __func__, ret);
00227 #endif
00228 
00229     CFSTORE_EX5_LOG("OPENING%s", "\r\n");
00230     memset(&ctx->flags, 0, sizeof(ctx->flags));
00231     memset(&ctx->hkey, 0, CFSTORE_HANDLE_BUFSIZE);
00232     ret = cfstore_drv->Open(cfstore_ex5_kv_name, ctx->flags, ctx->hkey);
00233     CFSTORE_EX5_TEST_ASSERT_MSG(ret >= ARM_DRIVER_OK, "%s:Error: Open() failed (ret=%ld)\r\n", __func__, ret);
00234 
00235     CFSTORE_EX5_LOG("DELETE1%s", "\r\n");
00236     ctx->len = CFSTORE_KEY_NAME_MAX_LENGTH;
00237     memset(ctx->value, 0, CFSTORE_KEY_NAME_MAX_LENGTH + 1);
00238     ret = cfstore_drv->Delete(ctx->hkey);
00239     CFSTORE_EX5_TEST_ASSERT_MSG(ret >= ARM_DRIVER_OK, "%s:Error: Delete() failed (ret=%ld)\r\n", __func__, ret);
00240 
00241     CFSTORE_EX5_LOG("CLOSING2%s", "\r\n");
00242     ret = cfstore_drv->Close(ctx->hkey);
00243     CFSTORE_EX5_TEST_ASSERT_MSG(ret >= ARM_DRIVER_OK, "%s:Error: Close() failed (ret=%ld)\r\n", __func__, ret);
00244 
00245     CFSTORE_EX5_LOG("FLUSHING3%s", "\r\n");
00246     ret = cfstore_drv->Flush();
00247     CFSTORE_EX5_TEST_ASSERT_MSG(ret >= ARM_DRIVER_OK, "%s:Error: Flush() failed (ret=%ld)\r\n", __func__, ret);
00248 
00249     CFSTORE_EX5_LOG("OPEN2 %s", "\r\n");
00250     memset(&ctx->flags, 0, sizeof(ctx->flags));
00251     memset(&ctx->hkey, 0, CFSTORE_HANDLE_BUFSIZE);
00252     ret = cfstore_drv->Open(cfstore_ex5_kv_name, ctx->flags, ctx->hkey);
00253     CFSTORE_EX5_TEST_ASSERT_MSG(ret == ARM_CFSTORE_DRIVER_ERROR_KEY_NOT_FOUND, "%s:Error: Find() failed to return expected value of ARM_CFSTORE_DRIVER_ERROR_KEY_NOT_FOUND (ret=%ld)\r\n", __func__, ret);
00254 
00255     CFSTORE_EX5_LOG("FLUSHING2%s", "\r\n");
00256     ret = cfstore_drv->Flush();
00257     CFSTORE_EX5_TEST_ASSERT_MSG(ret >= ARM_DRIVER_OK, "%s:Error:2: Flush() failed (ret=%ld)\r\n", __func__, ret);
00258 
00259     CFSTORE_EX5_LOG("UNINITIALIZING3%s", "\r\n");
00260     ret = cfstore_drv->Uninitialize();
00261     CFSTORE_EX5_TEST_ASSERT_MSG(ret >= ARM_DRIVER_OK, "%s:Error: Uninitialize() should return ret >= 0 for synch mode(ret=%ld)\r\n", __func__, ret);
00262     CFSTORE_EX5_LOG("***************%s", "\r\n");
00263     CFSTORE_EX5_LOG("*** SUCCESS ***%s", "\r\n");
00264     CFSTORE_EX5_LOG("***************%s", "\r\n");
00265     return;
00266 }
00267 
00268 static control_t cfstore_EXAMPLE5_app_start(const size_t call_count)
00269 {
00270     int32_t ret = ARM_DRIVER_ERROR;
00271     cfstore_EXAMPLE5_ctx_t* ctx = &cfstore_EXAMPLE5_ctx_g;
00272 
00273     (void) call_count;
00274 
00275     /* initialise the context */
00276     memset(ctx, 0, sizeof(cfstore_EXAMPLE5_ctx_t));
00277     ctx->hkey_next = ctx->hkey_next_buf;
00278     ctx->hkey_prev = ctx->hkey_prev_buf;
00279     ctx->caps = cfstore_drv->GetCapabilities();
00280     CFSTORE_EX5_LOG("%s:INITIALIZING: caps.asynchronous_ops=%lu\n", __func__, ctx->caps.asynchronous_ops);
00281     if(ctx->caps.asynchronous_ops){
00282         /* This is a sync mode only test. If this test is not built for sync mode, then skip testing return true
00283          * This means the test will conveniently pass when run in CI as part of async mode testing */
00284         CFSTORE_EX5_LOG("*** Skipping test as binary built for flash journal async mode, and this test is sync-only%s", "\n");
00285         return CaseNext;
00286     }
00287     ret = cfstore_test_startup();
00288     CFSTORE_EX5_TEST_ASSERT_MSG(ret >= ARM_DRIVER_OK, "%s:Error: failed to perform test startup (ret=%d).\n", __func__, (int) ret);
00289     cfstore_ex5_test_01(ctx);
00290     return CaseNext;
00291 }
00292 
00293 #ifndef CFSTORE_EXAMPLE5_APP
00294 /* when built as Configuration-Store example, include greentea support otherwise omit */
00295 
00296 /// @cond CFSTORE_DOXYGEN_DISABLE
00297 utest::v1::status_t greentea_setup(const size_t number_of_cases)
00298 {
00299     GREENTEA_SETUP(100, "default_auto");
00300     return greentea_test_setup_handler(number_of_cases);
00301 }
00302 
00303 Case cases[] = {
00304            /*          1         2         3         4         5         6        7  */
00305            /* 1234567890123456789012345678901234567890123456789012345678901234567890 */
00306         Case("EXAMPLE5_test_01_start", cfstore_EXAMPLE5_app_start),
00307 };
00308 
00309 
00310 /* Declare your test specification with a custom setup handler */
00311 Specification specification(greentea_setup, cases);
00312 
00313 
00314 int main()
00315 {
00316     return !Harness::run(specification);
00317 }
00318 /// @endcond
00319 
00320 
00321 #else   // CFSTORE_EXAMPLE5_APP
00322 
00323 // stand alone Configuration-Store-Example
00324 void app_start(int argc __unused, char** argv __unused)
00325 {
00326     cfstore_EXAMPLE5_app_start(0);
00327 }
00328 
00329 
00330 #endif // CFSTORE_EXAMPLE5_APP