Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
example3.cpp
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 example3.cpp Test case to demonstrate each API function works correctly. 00020 * 00021 * \par Example 3 Notes 00022 * 00023 * Example3 is a synchronous mode example for creating key-values in the persistent storage. 00024 * 00025 * The flash-journal synchronous mode example test does the following CFSTORE operations: 00026 * - initialises 00027 * - creates a key-value pair (KV). 00028 * - writes the data for the KV 00029 * - closes KV. 00030 * - flushes the KV to flash 00031 * - opens KV for reading. 00032 * - reads KV and checks the value blob was the same as previously written. 00033 * - closes KV. 00034 * - finds a KV (there is only 1 to find). 00035 * - for the KV returned, get the key name. 00036 * - for the KV returned, get the value length. 00037 * - for the KV returned, delete the KV. 00038 * - find another KV (which fails as there are no more keys to find). 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 Example3 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 example3.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_EXAMPLE3_APP` e.g. `mbed compile -v -m K64F -t GCC_ARM -DCFSTORE_EXAMPLE3_APP`. 00056 * 00057 */ 00058 #include "mbed.h" 00059 #ifndef CFSTORE_EXAMPLE3_APP 00060 /* when built as Configuration-Store example, include greentea support otherwise omit */ 00061 #include "utest/utest.h" 00062 #include "unity/unity.h" 00063 #include "greentea-client/test_env.h" 00064 #else // CFSTORE_EXAMPLE3_APP 00065 /* map utest types for building as stand alone example */ 00066 #define control_t void 00067 #define CaseNext 00068 #endif // CFSTORE_EXAMPLE3_APP 00069 00070 #include "cfstore_config.h" 00071 #include "cfstore_test.h" 00072 #include "configuration_store.h" 00073 00074 00075 #include <stdio.h> 00076 #include <stdlib.h> 00077 #include <string.h> 00078 00079 #ifndef CFSTORE_EXAMPLE3_APP 00080 using namespace utest::v1; 00081 #endif 00082 00083 00084 /// @cond CFSTORE_DOXYGEN_DISABLE 00085 #define CFSTORE_EX1_TEST_ASSERT(Expr) if (!(Expr)) { printf("%s:%u: assertion failure\r\n", __FUNCTION__, __LINE__); while (1) ;} 00086 #define CFSTORE_EX1_TEST_ASSERT_EQUAL(expected, actual) if ((expected) != (actual)) {printf("%s:%u: assertion failure\r\n", __FUNCTION__, __LINE__); while (1) ;} 00087 #define CFSTORE_EX1_TEST_ASSERT_NOT_EQUAL(expected, actual) if ((expected) == (actual)) {printf("%s:%u: assertion failure\r\n", __FUNCTION__, __LINE__); while (1) ;} 00088 00089 #define CFSTORE_EX1_TEST_ASSERT_MSG(Expr, _fmt, ...) \ 00090 do \ 00091 { \ 00092 if (!(Expr)) \ 00093 { \ 00094 printf(_fmt, __VA_ARGS__); \ 00095 while (1) ; \ 00096 } \ 00097 }while(0); 00098 00099 #define CFSTORE_EX1_LOG(_fmt, ...) \ 00100 do \ 00101 { \ 00102 printf(_fmt, __VA_ARGS__); \ 00103 }while(0); 00104 00105 00106 const char* cfstore_ex3_opcode_str[] = 00107 { 00108 "UNDEFINED", 00109 "CFSTORE_OPCODE_CLOSE", 00110 "CFSTORE_OPCODE_CREATE", 00111 "CFSTORE_OPCODE_DELETE", 00112 "CFSTORE_OPCODE_FIND", 00113 "CFSTORE_OPCODE_FLUSH", 00114 "CFSTORE_OPCODE_GET_KEY_NAME", 00115 "CFSTORE_OPCODE_GET_STATUS", 00116 "CFSTORE_OPCODE_GET_VALUE_LEN", 00117 "CFSTORE_OPCODE_INITIALIZE", 00118 "CFSTORE_OPCODE_OPEN", 00119 "CFSTORE_OPCODE_POWER_CONTROL", 00120 "CFSTORE_OPCODE_READ", 00121 "CFSTORE_OPCODE_RSEEK", 00122 "CFSTORE_OPCODE_UNINITIALIZE", 00123 "CFSTORE_OPCODE_WRITE", 00124 "CFSTORE_OPCODE_MAX" 00125 }; 00126 00127 const char* cfstore_ex3_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"; 00128 const char* cfstore_ex3_kv_value = "TheRollingStone"; 00129 #define CFSTORE_EX1_RSEEK_OFFSET 10 /* offset to S of Stone */ 00130 00131 typedef struct cfstore_example3_ctx_t 00132 { 00133 ARM_CFSTORE_CAPABILITIES caps; 00134 uint8_t hkey[CFSTORE_HANDLE_BUFSIZE]; 00135 uint8_t hkey_next_buf[CFSTORE_HANDLE_BUFSIZE]; 00136 uint8_t hkey_prev_buf[CFSTORE_HANDLE_BUFSIZE]; 00137 ARM_CFSTORE_HANDLE hkey_next; 00138 ARM_CFSTORE_HANDLE hkey_prev; 00139 ARM_CFSTORE_SIZE len; 00140 ARM_CFSTORE_KEYDESC kdesc; 00141 ARM_CFSTORE_FMODE flags; 00142 char value[CFSTORE_KEY_NAME_MAX_LENGTH+1]; 00143 } cfstore_example3_ctx_t; 00144 00145 static cfstore_example3_ctx_t cfstore_example3_ctx_g; 00146 00147 extern ARM_CFSTORE_DRIVER cfstore_driver; 00148 ARM_CFSTORE_DRIVER *cfstore_drv = &cfstore_driver; 00149 /// @endcond 00150 00151 00152 static void cfstore_ex3_test_01(cfstore_example3_ctx_t* ctx) 00153 { 00154 int32_t ret; 00155 00156 CFSTORE_EX1_LOG("INITIALIZING%s", "\r\n"); 00157 ret = cfstore_drv->Initialize(NULL, NULL); 00158 CFSTORE_EX1_TEST_ASSERT_MSG(ret >= ARM_DRIVER_OK, "%s:Error: Initialize() should return ret >= 0 for async/synch modes(ret=%ld)\r\n", __func__, ret); 00159 00160 CFSTORE_EX1_LOG("CREATING%s", "\r\n"); 00161 memset(&ctx->kdesc, 0, sizeof(ARM_CFSTORE_KEYDESC)); 00162 ctx->kdesc.drl = ARM_RETENTION_NVM; 00163 ctx->len = strlen(cfstore_ex3_kv_value); 00164 ret = cfstore_drv->Create(cfstore_ex3_kv_name, ctx->len, &ctx->kdesc, ctx->hkey); 00165 CFSTORE_EX1_TEST_ASSERT_MSG(ret >= ARM_DRIVER_OK, "%s:Error: Create() failed (ret=%ld)\r\n", __func__, ret); 00166 00167 CFSTORE_EX1_LOG("WRITING%s", "\r\n"); 00168 ctx->len = strlen(cfstore_ex3_kv_value); 00169 ret = cfstore_drv->Write(ctx->hkey, cfstore_ex3_kv_value, &ctx->len); 00170 CFSTORE_EX1_TEST_ASSERT_MSG(ret >= ARM_DRIVER_OK, "%s:Error: Write() failed (ret=%ld)\r\n", __func__, ret); 00171 00172 CFSTORE_EX1_TEST_ASSERT_MSG(ret == (int32_t) strlen(cfstore_ex3_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_ex3_kv_value)); 00173 CFSTORE_EX1_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); 00174 00175 CFSTORE_EX1_LOG("CLOSING1%s", "\r\n"); 00176 ret = cfstore_drv->Close(ctx->hkey); 00177 CFSTORE_EX1_TEST_ASSERT_MSG(ret >= ARM_DRIVER_OK, "%s:Error: Close() failed (ret=%ld)\r\n", __func__, ret); 00178 00179 CFSTORE_EX1_LOG("FLUSHING1%s", "\r\n"); 00180 ret = cfstore_drv->Flush(); 00181 CFSTORE_EX1_TEST_ASSERT_MSG(ret >= ARM_DRIVER_OK, "%s:Error: Flush() failed (ret=%ld)\r\n", __func__, ret); 00182 00183 CFSTORE_EX1_LOG("OPENING%s", "\r\n"); 00184 memset(&ctx->flags, 0, sizeof(ctx->flags)); 00185 memset(&ctx->hkey, 0, CFSTORE_HANDLE_BUFSIZE); 00186 ret = cfstore_drv->Open(cfstore_ex3_kv_name, ctx->flags, ctx->hkey); 00187 CFSTORE_EX1_TEST_ASSERT_MSG(ret >= ARM_DRIVER_OK, "%s:Error: Open() failed (ret=%ld)\r\n", __func__, ret); 00188 00189 CFSTORE_EX1_LOG("READING1%s", "\r\n"); 00190 ctx->len = CFSTORE_KEY_NAME_MAX_LENGTH; 00191 memset(ctx->value, 0, CFSTORE_KEY_NAME_MAX_LENGTH+1); 00192 ret = cfstore_drv->Read(ctx->hkey, ctx->value, &ctx->len); 00193 CFSTORE_EX1_TEST_ASSERT_MSG(ret >= ARM_DRIVER_OK, "%s:Error: Read() failed (ret=%ld)\r\n", __func__, ret); 00194 00195 CFSTORE_EX1_TEST_ASSERT_MSG(ret == (int32_t) strlen(cfstore_ex3_kv_value), "%s:Error: Read() number of octets read (i.e. completion status (%ld)) != strlen(ctx->value)(%ld)\r\n", __func__, ret, (int32_t) strlen(cfstore_ex3_kv_value)); 00196 CFSTORE_EX1_TEST_ASSERT_MSG(ret == (int32_t) ctx->len, "%s:Error: Read() number of octets read (i.e. completion status (%ld)) != updated value of len parameter (%ld)\r\n", __func__, ret, (int32_t) ctx->len); 00197 CFSTORE_EX1_TEST_ASSERT_MSG(strncmp(ctx->value, cfstore_ex3_kv_value, strlen(cfstore_ex3_kv_value)) == 0, "%s:Error: the read value (%s) is not as expected (%s)\r\n", __func__, ctx->value, cfstore_ex3_kv_value); 00198 00199 CFSTORE_EX1_LOG("RSEEKING%s", "\r\n"); 00200 ret = cfstore_drv->Rseek(ctx->hkey, CFSTORE_EX1_RSEEK_OFFSET); 00201 CFSTORE_EX1_TEST_ASSERT_MSG(ret >= ARM_DRIVER_OK, "%s:Error: Rseek() failed (ret=%ld)\r\n", __func__, ret); 00202 00203 CFSTORE_EX1_LOG("READING2%s", "\r\n"); 00204 ctx->len = CFSTORE_KEY_NAME_MAX_LENGTH; 00205 memset(ctx->value, 0, CFSTORE_KEY_NAME_MAX_LENGTH+1); 00206 ret = cfstore_drv->Read(ctx->hkey, ctx->value, &ctx->len); 00207 CFSTORE_EX1_TEST_ASSERT_MSG(ret >= ARM_DRIVER_OK, "%s:Error: Read() failed (ret=%ld)\r\n", __func__, ret); 00208 CFSTORE_EX1_TEST_ASSERT_MSG(ret == (int32_t) strlen(&cfstore_ex3_kv_value[CFSTORE_EX1_RSEEK_OFFSET]), "%s:Error: Read() number of octets read (i.e. completion status (%ld)) != strlen(ctx->value)(%ld)\r\n", __func__, ret, (int32_t) strlen(&cfstore_ex3_kv_value[CFSTORE_EX1_RSEEK_OFFSET])); 00209 CFSTORE_EX1_TEST_ASSERT_MSG(ret == (int32_t) ctx->len, "%s:Error: Read() number of octets read (i.e. completion status (%ld)) != updated value of len parameter (%ld)\r\n", __func__, ret, (int32_t) ctx->len); 00210 CFSTORE_EX1_TEST_ASSERT_MSG(strncmp(ctx->value, &cfstore_ex3_kv_value[CFSTORE_EX1_RSEEK_OFFSET], strlen(&cfstore_ex3_kv_value[CFSTORE_EX1_RSEEK_OFFSET])) == 0, "%s:Error: the read value (%s) is not as expected (%s)\r\n", __func__, ctx->value, &cfstore_ex3_kv_value[CFSTORE_EX1_RSEEK_OFFSET]); 00211 00212 CFSTORE_EX1_LOG("CLOSING2%s", "\r\n"); 00213 ret = cfstore_drv->Close(ctx->hkey); 00214 CFSTORE_EX1_TEST_ASSERT_MSG(ret >= ARM_DRIVER_OK, "%s:Error: Close() failed (ret=%ld)\r\n", __func__, ret); 00215 00216 CFSTORE_EX1_LOG("FINDING1%s", "\r\n"); 00217 ret = cfstore_drv->Find("*", ctx->hkey_next, ctx->hkey_prev); 00218 CFSTORE_EX1_TEST_ASSERT_MSG(ret >= ARM_DRIVER_OK, "%s:Error: Find() failed (ret=%ld)\r\n", __func__, ret); 00219 00220 CFSTORE_EX1_LOG("GETTING_KEY_NAME%s", "\r\n"); 00221 ctx->len = CFSTORE_KEY_NAME_MAX_LENGTH; 00222 memset(ctx->value, 0, CFSTORE_KEY_NAME_MAX_LENGTH+1); 00223 ret = cfstore_drv->GetKeyName(ctx->hkey_prev, ctx->value, (uint8_t*) &ctx->len); 00224 CFSTORE_EX1_TEST_ASSERT_MSG(ret >= ARM_DRIVER_OK, "%s:Error: GetKeyName() failed (ret=%ld)\r\n", __func__, ret); 00225 CFSTORE_EX1_TEST_ASSERT_MSG( ((int32_t) ctx->len == ((int32_t) strlen(cfstore_ex3_kv_name)+1)), "%s:Error: GetKeyName() updated value of len parameter (%ld) != strlen(cfstore_ex3_kv_name) (%ld) (\r\n", __func__, (int32_t) ctx->len, (int32_t) strlen(cfstore_ex3_kv_name)); 00226 CFSTORE_EX1_TEST_ASSERT_MSG(strncmp(ctx->value, cfstore_ex3_kv_name, strlen(cfstore_ex3_kv_name)) == 0, "%s:Error: the key name (%s) is not as expected (%s)\r\n", __func__, ctx->value, cfstore_ex3_kv_name); 00227 00228 CFSTORE_EX1_LOG("GETTING_VALUE_LEN%s", "\r\n"); 00229 ctx->len = CFSTORE_KEY_NAME_MAX_LENGTH; 00230 ret = cfstore_drv->GetValueLen(ctx->hkey_prev, &ctx->len); 00231 CFSTORE_EX1_TEST_ASSERT_MSG(ret >= ARM_DRIVER_OK, "%s:Error: GetValueLen() failed (ret=%ld)\r\n", __func__, ret); 00232 CFSTORE_EX1_TEST_ASSERT_MSG((int32_t) ctx->len == (int32_t) strlen(cfstore_ex3_kv_value), "%s:Error: GetValueLen() updated value of len parameter (%ld) != strlen(cfstore_ex3_kv_value)(%ld) \r\n", __func__, (int32_t) ctx->len, (int32_t) strlen(cfstore_ex3_kv_value)); 00233 00234 CFSTORE_EX1_LOG("DELETING%s", "\r\n"); 00235 ret = cfstore_drv->Delete(ctx->hkey_prev); 00236 CFSTORE_EX1_TEST_ASSERT_MSG(ret >= ARM_DRIVER_OK, "%s:Error: Close() failed (ret=%ld)\r\n", __func__, ret); 00237 CFSTORE_HANDLE_SWAP(ctx->hkey_prev, ctx->hkey_next); 00238 00239 CFSTORE_EX1_LOG("FINDING2%s", "\r\n"); 00240 ret = cfstore_drv->Find("*", ctx->hkey_next, ctx->hkey_prev); 00241 CFSTORE_EX1_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); 00242 00243 CFSTORE_EX1_LOG("FLUSHING2%s", "\r\n"); 00244 ret = cfstore_drv->Flush(); 00245 CFSTORE_EX1_TEST_ASSERT_MSG(ret >= ARM_DRIVER_OK, "%s:Error:2: Flush() failed (ret=%ld)\r\n", __func__, ret); 00246 00247 CFSTORE_EX1_LOG("UNINITIALIZING%s", "\r\n"); 00248 ret = cfstore_drv->Uninitialize(); 00249 CFSTORE_EX1_TEST_ASSERT_MSG(ret >= ARM_DRIVER_OK, "%s:Error: Uninitialize() should return ret >= 0 for synch mode(ret=%ld)\r\n", __func__, ret); 00250 CFSTORE_EX1_LOG("***************%s", "\r\n"); 00251 CFSTORE_EX1_LOG("*** SUCCESS ***%s", "\r\n"); 00252 CFSTORE_EX1_LOG("***************%s", "\r\n"); 00253 return; 00254 } 00255 00256 static control_t cfstore_example3_app_start(const size_t call_count) 00257 { 00258 cfstore_example3_ctx_t* ctx = &cfstore_example3_ctx_g; 00259 00260 (void) call_count; 00261 00262 /* initialise the context */ 00263 memset(ctx, 0, sizeof(cfstore_example3_ctx_t)); 00264 ctx->hkey_next = ctx->hkey_next_buf; 00265 ctx->hkey_prev = ctx->hkey_prev_buf; 00266 ctx->caps = cfstore_drv->GetCapabilities(); 00267 CFSTORE_EX1_LOG("%s:INITIALIZING: caps.asynchronous_ops=%lu\n", __func__, ctx->caps.asynchronous_ops); 00268 if(ctx->caps.asynchronous_ops == 1){ 00269 /* This is a sync mode only test. If this test is not built for sync mode, then skip testing return true 00270 * This means the test will conveniently pass when run in CI as part of async mode testing */ 00271 CFSTORE_EX1_LOG("*** Skipping test as binary built for flash journal async mode, and this test is sync-only%s", "\n"); 00272 return CaseNext; 00273 } 00274 cfstore_ex3_test_01(ctx); 00275 return CaseNext; 00276 } 00277 00278 #ifndef CFSTORE_EXAMPLE3_APP 00279 /* when built as Configuration-Store example, include greentea support otherwise omit */ 00280 00281 /* report whether built/configured for flash sync or async mode */ 00282 static control_t cfstore_example3_test_00(const size_t call_count) 00283 { 00284 int32_t ret = ARM_DRIVER_ERROR; 00285 00286 (void) call_count; 00287 ret = cfstore_test_startup(); 00288 CFSTORE_EX1_TEST_ASSERT_MSG(ret >= ARM_DRIVER_OK, "%s:Error: failed to perform test startup (ret=%d).\n", __func__, (int) ret); 00289 return CaseNext; 00290 } 00291 00292 /// @cond CFSTORE_DOXYGEN_DISABLE 00293 utest::v1::status_t greentea_setup(const size_t number_of_cases) 00294 { 00295 GREENTEA_SETUP(100, "default_auto"); 00296 return greentea_test_setup_handler(number_of_cases); 00297 } 00298 00299 Case cases[] = { 00300 /* 1 2 3 4 5 6 7 */ 00301 /* 1234567890123456789012345678901234567890123456789012345678901234567890 */ 00302 Case("EXAMPLE3_test_00", cfstore_example3_test_00), 00303 Case("EXAMPLE3_test_01_start", cfstore_example3_app_start), 00304 }; 00305 00306 00307 /* Declare your test specification with a custom setup handler */ 00308 Specification specification(greentea_setup, cases); 00309 00310 int main() 00311 { 00312 return !Harness::run(specification); 00313 } 00314 /// @endcond 00315 00316 00317 #else // CFSTORE_EXAMPLE3_APP 00318 00319 // stand alone Configuration-Store-Example 00320 void app_start(int argc __unused, char** argv __unused) 00321 { 00322 cfstore_example3_app_start(0); 00323 } 00324 00325 00326 #endif // CFSTORE_EXAMPLE3_APP
Generated on Tue Aug 9 2022 00:37:07 by
1.7.2