mbed-os for GR-LYCHEE

Dependents:   mbed-os-example-blinky-gr-lychee GR-Boads_Camera_sample GR-Boards_Audio_Recoder GR-Boads_Camera_DisplayApp ... more

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers cfstore_test.c Source File

cfstore_test.c

00001 /* @file cfstore_test.c
00002  *
00003  * mbed Microcontroller Library
00004  * Copyright (c) 2006-2016 ARM Limited
00005  *
00006  * Licensed under the Apache License, Version 2.0 (the "License");
00007  * you may not use this file except in compliance with the License.
00008  * You may obtain a copy of the License at
00009  *
00010  *     http://www.apache.org/licenses/LICENSE-2.0
00011  *
00012  * Unless required by applicable law or agreed to in writing, software
00013  * distributed under the License is distributed on an "AS IS" BASIS,
00014  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00015  * See the License for the specific language governing permissions and
00016  * limitations under the License.
00017  *
00018  * test support code implementation file.
00019  */
00020 
00021 #include "cfstore_config.h"
00022 #include "cfstore_debug.h"
00023 #include "cfstore_test.h"
00024 #include "configuration_store.h"
00025 
00026 #ifdef CFSTORE_CONFIG_BACKEND_FLASH_ENABLED
00027 #include "flash_journal_strategy_sequential.h"
00028 #include "flash_journal.h"
00029 #include "Driver_Common.h"
00030 #endif /* CFSTORE_CONFIG_BACKEND_FLASH_ENABLED */
00031 
00032 #include <stdio.h>
00033 #include <stdlib.h>
00034 #include <string.h>
00035 #include <assert.h>
00036 #include <inttypes.h>
00037 #include <ctype.h>
00038 
00039 
00040 /* ruler for measuring text strings */
00041 /*                                                                                                    1         1         1         1         1         1         1         1         1         1         2         2         2 */
00042 /* 0        1         2         3         4         5         6         7         8         9         0         1         2         3         4         5         6         7         8         9         0         1         2 */
00043 /* 1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890 */
00044 
00045 const uint8_t cfstore_test_byte_data_table[CFSTORE_TEST_BYTE_DATA_TABLE_SIZE] = {
00046     0x2d, 0xf3, 0x31, 0x4c, 0x11, 0x4f, 0xde, 0x0d, 0xbd, 0xbc, 0xa6, 0x78, 0x36, 0x5c, 0x1d, 0x28,
00047     0x5f, 0xa9, 0x10, 0x65, 0x54, 0x45, 0x21, 0x1a, 0x88, 0xfe, 0x76, 0x45, 0xb9, 0xac, 0x65, 0x9a,
00048     0x34, 0x9d, 0x73, 0x10, 0xb4, 0xa9, 0x2e, 0x90, 0x95, 0x68, 0xac, 0xfe, 0xc5, 0x2d, 0x15, 0x03,
00049     0x34, 0x70, 0xf1, 0x1d, 0x48, 0xa1, 0xa0, 0xed, 0x5c, 0x2f, 0xf5, 0x2b, 0xb9, 0x84, 0xbb, 0x45,
00050     0x32, 0xdd, 0xb1, 0x33, 0x95, 0x2a, 0xbc, 0x26, 0xf0, 0x89, 0xba, 0xf4, 0xbd, 0xf9, 0x5d, 0x2e,
00051     0x6e, 0x11, 0xc6, 0xa7, 0x78, 0xfc, 0xc9, 0x0e, 0x6b, 0x38, 0xba, 0x14, 0x1b, 0xab, 0x4c, 0x20,
00052     0x91, 0xe4, 0xb0, 0xf1, 0x2b, 0x14, 0x07, 0x6b, 0xb5, 0xcd, 0xe3, 0x49, 0x75, 0xac, 0xe8, 0x98,
00053     0xf1, 0x58, 0x8f, 0xd9, 0xc4, 0x8f, 0x00, 0x17, 0xb5, 0x06, 0x6a, 0x33, 0xbd, 0xa7, 0x40, 0x5a,
00054     0xbf, 0x49, 0xf7, 0x27, 0x1b, 0x4c, 0x3e, 0x6f, 0xe3, 0x08, 0x1f, 0xfd, 0xa6, 0xd4, 0xc7, 0x5f,
00055     0xa4, 0xa6, 0x82, 0xad, 0x19, 0xd5, 0x5c, 0xd8, 0x3a, 0x49, 0x85, 0xc9, 0x21, 0x83, 0xf6, 0xc6,
00056     0x84, 0xf9, 0x76, 0x89, 0xf3, 0x2d, 0x17, 0x50, 0x97, 0x38, 0x48, 0x9a, 0xe1, 0x82, 0xcd, 0xac,
00057     0xa8, 0x1d, 0xd7, 0x96, 0x5e, 0xb3, 0x08, 0xa8, 0x3a, 0xc7, 0x2b, 0x05, 0xaf, 0xdc, 0x16, 0xdf,
00058     0x48, 0x0f, 0x2a, 0x7e, 0x3a, 0x82, 0xd7, 0x80, 0xd6, 0x49, 0x27, 0x5d, 0xe3, 0x07, 0x62, 0xb3,
00059     0xc3, 0x6c, 0xba, 0xb2, 0xaa, 0x9f, 0xd9, 0x03, 0x0d, 0x27, 0xa8, 0xe0, 0xd6, 0xee, 0x79, 0x4b,
00060     0xd6, 0x97, 0x99, 0xb7, 0x11, 0xd6, 0x0d, 0x34, 0xae, 0x99, 0x4a, 0x93, 0x95, 0xd0, 0x5a, 0x34,
00061     0x19, 0xa2, 0x69, 0x57, 0xcf, 0x7c, 0x3d, 0x98, 0x88, 0x5d, 0x04, 0xf2, 0xd7, 0xac, 0xa5, 0x63
00062 };
00063 
00064 
00065 /* @brief   set of test data for sequential write tests */
00066 cfstore_test_rw_data_entry_t cfstore_test_rw_data_table[] =
00067 {
00068         { 25, 'z' },
00069         { 00, 'a' },
00070         { 24, 'y' },
00071         { 01, 'b' },
00072         { 23, 'x' },
00073         { 02, 'c' },
00074         { 22, 'w' },
00075         { 03, 'd' },
00076         { 21, 'v' },
00077         { 04, 'e' },
00078         { 20, 'u' },
00079         { 05, 'f' },
00080         { 19, 't' },
00081         { 06, 'g' },
00082         { 18, 's' },
00083         { 07, 'h' },
00084         { 17, 'r' },
00085         {  8, 'i' },
00086         { 16, 'q' },
00087         {  9, 'j' },
00088         { 15, 'p' },
00089         { 10, 'k' },
00090         { 14, 'o' },
00091         { 11, 'l' },
00092         { 13, 'n' },
00093         { 12, 'm' },
00094         { CFSTORE_TEST_RW_TABLE_SENTINEL, '@' },
00095 };
00096 
00097 const char* cfstore_test_opcode_str[] =
00098 {
00099     "UNDEFINED",
00100     "CFSTORE_OPCODE_CLOSE",
00101     "CFSTORE_OPCODE_CREATE",
00102     "CFSTORE_OPCODE_DELETE",
00103     "CFSTORE_OPCODE_FIND",
00104     "CFSTORE_OPCODE_FLUSH",
00105     "CFSTORE_OPCODE_GET_KEY_NAME",
00106     "CFSTORE_OPCODE_GET_STATUS",
00107     "CFSTORE_OPCODE_GET_VALUE_LEN",
00108     "CFSTORE_OPCODE_INITIALIZE",
00109     "CFSTORE_OPCODE_OPEN",
00110     "CFSTORE_OPCODE_POWER_CONTROL",
00111     "CFSTORE_OPCODE_READ",
00112     "CFSTORE_OPCODE_RSEEK",
00113     "CFSTORE_OPCODE_UNINITIALIZE",
00114     "CFSTORE_OPCODE_WRITE",
00115     "CFSTORE_OPCODE_MAX"
00116 };
00117 
00118 
00119 static int32_t cfstore_test_dump_print_array(const char* data, ARM_CFSTORE_SIZE len)
00120 {
00121     int i;
00122     char buf[80];
00123     char sbuf[80];
00124     char* outbuf = buf;
00125     char* soutbuf = sbuf;
00126 
00127     memset(outbuf, 0, 80);
00128     memset(soutbuf, 0, 80);
00129     outbuf += sprintf(outbuf, "      ");
00130     soutbuf += sprintf(soutbuf, "      ");
00131     for (i = 0; i < (int) len; i++){
00132         outbuf += sprintf(outbuf, "%02X ", data[i]);
00133 
00134         if( !(isalnum( (int) data[i]) || ispunct( (int) data[i])) ){
00135             *soutbuf++ =  '*';
00136         } else {
00137             *soutbuf++ = data[i];
00138         }
00139 
00140         if( (i % 16 == 0) && i > 0){
00141             CFSTORE_LOG("%s", buf);
00142             CFSTORE_LOG("%s\n", sbuf);
00143             outbuf = buf;
00144             soutbuf = sbuf;
00145             memset(outbuf, 0, 80);
00146             memset(soutbuf, 0, 80);
00147             outbuf += sprintf(outbuf, "      ");
00148             soutbuf += sprintf(soutbuf, "      ");
00149         }
00150     }
00151     if(i % 16){
00152         /* Pad the end of the string to align string data. */
00153         while(i % 16){
00154             outbuf += sprintf(outbuf, "   ");
00155             i++;
00156         }
00157         CFSTORE_LOG("%s", buf);
00158         CFSTORE_LOG("      %s", sbuf);
00159     }
00160     CFSTORE_LOG("%s", "\n");
00161     return ARM_DRIVER_OK;
00162 }
00163 
00164 
00165 /* @brief   function to dump contents of cfstore
00166  */
00167 int32_t cfstore_test_dump(void)
00168 {
00169     const char* key_name_query = "*";
00170     char* read_buf = NULL;
00171     char key_name[CFSTORE_KEY_NAME_MAX_LENGTH+1];
00172     uint8_t len = CFSTORE_KEY_NAME_MAX_LENGTH+1;
00173     ARM_CFSTORE_SIZE vlen = 0;
00174     int32_t ret = ARM_DRIVER_ERROR;
00175     ARM_CFSTORE_DRIVER* drv = &cfstore_driver;
00176     ARM_CFSTORE_HANDLE_INIT(next);
00177     ARM_CFSTORE_HANDLE_INIT(prev);
00178     ARM_CFSTORE_CAPABILITIES caps = cfstore_driver.GetCapabilities();
00179 
00180     CFSTORE_FENTRYLOG("%s:entered\n", __func__);
00181 
00182     CFSTORE_LOG("CFSTORE Flash Entries%s", "\n");
00183     CFSTORE_LOG("=====================%s", "\n\n");
00184     while((ret = drv->Find(key_name_query, prev, next)) == ARM_DRIVER_OK)
00185     {
00186         len = CFSTORE_KEY_NAME_MAX_LENGTH+1;
00187         ret = drv->GetKeyName(next, key_name, &len);
00188         if(ret < ARM_DRIVER_OK){
00189             CFSTORE_ERRLOG("Error: failed to get key name%s", "\n");
00190             break;
00191         }
00192         ret = drv->GetValueLen(next, &vlen);
00193         if(ret < ARM_DRIVER_OK){
00194             CFSTORE_ERRLOG("Error: failed to get value length%s", "\n");
00195             break;
00196         }
00197         read_buf = (char*) malloc(vlen+1);
00198         if(read_buf == NULL){
00199             CFSTORE_ERRLOG("Error: failed to malloc() read buffer%s", "\n");
00200             break;
00201         }
00202         ret = drv->Read(next, read_buf, &vlen);
00203         if(ret < ARM_DRIVER_OK){
00204             CFSTORE_ERRLOG("Error: failed to read key value%s", "\n");
00205             free(read_buf);
00206             break;
00207         }
00208         CFSTORE_LOG("  keyname : %s\n", key_name);
00209         CFSTORE_LOG("    name len : %d\n", (int) len);
00210         CFSTORE_LOG("    value len : %d\n", (int) vlen);
00211         CFSTORE_LOG("    data :%s", "\n");
00212         cfstore_test_dump_print_array((const char*) read_buf, vlen);
00213         CFSTORE_LOG("%s", ".\n");
00214         free(read_buf);
00215         CFSTORE_HANDLE_SWAP(prev, next);
00216     }
00217     CFSTORE_LOG("%s", ".\n");
00218     CFSTORE_LOG("  caps.asynchronous_ops : %d\n", (int) caps.asynchronous_ops);
00219     CFSTORE_LOG("%s", ".\n");
00220     CFSTORE_LOG("== End ==============%s", "\n\n");
00221 
00222     if(ret == ARM_CFSTORE_DRIVER_ERROR_KEY_NOT_FOUND) {
00223         /* As expected, no more keys have been found by the Find(). */
00224         ret = ARM_DRIVER_OK;
00225     }
00226     return ret;
00227 }
00228 
00229 
00230 /* @brief   test startup code to reset flash
00231  */
00232 int32_t cfstore_test_startup(void)
00233 {
00234     int32_t ret = ARM_DRIVER_ERROR;
00235     ARM_CFSTORE_DRIVER* cfstore_drv = &cfstore_driver;
00236     ARM_CFSTORE_CAPABILITIES caps = cfstore_driver.GetCapabilities();
00237 
00238     CFSTORE_LOG("INITIALIZING: caps.asynchronous_ops=%d\n", (int) caps.asynchronous_ops);
00239 
00240     /* Dump contents of CFSTORE */
00241     ret = cfstore_drv->Initialize(NULL, NULL);
00242     if(ret < ARM_DRIVER_OK){
00243         CFSTORE_ERRLOG("%s:Error: failed to initialize CFSTORE (ret=%d)\n", __func__, (int) ret);
00244         return ARM_DRIVER_ERROR;
00245     }
00246     ret = cfstore_test_dump();
00247     if(ret < ARM_DRIVER_OK){
00248         CFSTORE_ERRLOG("%s:Error: failed to dump CFSTORE (ret=%d)\n", __func__, (int) ret);
00249         return ARM_DRIVER_ERROR;
00250     }
00251     ret = cfstore_drv->Uninitialize();
00252     if(ret < ARM_DRIVER_OK){
00253         CFSTORE_ERRLOG("%s:Error: failed to uninitialize CFSTORE (ret=%d)\n", __func__, (int) ret);
00254         return ARM_DRIVER_ERROR;
00255     }
00256 
00257 #ifdef CFSTORE_CONFIG_BACKEND_FLASH_ENABLED
00258 
00259     static FlashJournal_t jrnl;
00260     extern ARM_DRIVER_STORAGE ARM_Driver_Storage_MTD_K64F;
00261     const ARM_DRIVER_STORAGE *drv = &ARM_Driver_Storage_MTD_K64F;
00262 
00263     ret = FlashJournal_initialize(&jrnl, drv, &FLASH_JOURNAL_STRATEGY_SEQUENTIAL, NULL);
00264     if(ret < JOURNAL_STATUS_OK){
00265         CFSTORE_ERRLOG("%s:Error: failed to initialize flash journaling layer (ret=%d)\n", __func__, (int) ret);
00266         return ARM_DRIVER_ERROR;
00267     }
00268     ret = FlashJournal_reset(&jrnl);
00269     if(ret < JOURNAL_STATUS_OK){
00270         CFSTORE_ERRLOG("%s:Error: failed to reset flash journal (ret=%d)\n", __func__, (int) ret);
00271         return ARM_DRIVER_ERROR;
00272     }
00273 
00274 #endif /*  CFSTORE_CONFIG_BACKEND_FLASH_ENABLED */
00275 
00276     return ARM_DRIVER_OK;
00277 }
00278 
00279 
00280 /* @brief   test utility function to check a node appears correctly in the cfstore
00281  * @note    this function expects cfstore to have been initialised with
00282  *          a call to ARM_CFSTORE_DRIVER::Initialize()
00283  */
00284 int32_t cfstore_test_check_node_correct(const cfstore_kv_data_t* node)
00285 {
00286     char* read_buf;
00287     int32_t ret = ARM_DRIVER_ERROR;
00288     ARM_CFSTORE_SIZE len = 0;
00289     ARM_CFSTORE_DRIVER* drv = &cfstore_driver;
00290     ARM_CFSTORE_HANDLE_INIT(hkey);
00291     ARM_CFSTORE_FMODE flags;
00292 
00293     CFSTORE_FENTRYLOG("%s:entered\r\n", __func__);
00294     memset(&flags, 0, sizeof(flags));
00295 
00296     ret = drv->Open(node->key_name, flags, hkey);
00297     if(ret < ARM_DRIVER_OK){
00298         CFSTORE_ERRLOG("%s:Error: failed to open node (key_name=\"%s\", value=\"%s\")(ret=%d)\r\n", __func__, node->key_name, node->value, (int) ret);
00299         goto out0;
00300     }
00301     len = strlen(node->value) + 1;
00302     read_buf = (char*) malloc(len);
00303     if(read_buf == NULL) {
00304         CFSTORE_ERRLOG("%s:Error: failed to allocated read buffer \r\n", __func__);
00305         goto out1;
00306     }
00307     memset(read_buf, 0, len);
00308     ret = drv->Read(hkey, read_buf, &len);
00309     if(ret < ARM_DRIVER_OK){
00310         CFSTORE_ERRLOG("%s:Error: failed to write key (key_name=\"%s\", value=\"%s\")\r\n", __func__, node->key_name, node->value);
00311         goto out2;
00312     }
00313     /* check read data is as expected */
00314     if(strncmp(read_buf, node->value, strlen(node->value)) != 0){
00315         CFSTORE_ERRLOG("%s:Error: read value data (%s) != KV value data (key_name=\"%s\", value=\"%s\")\r\n", __func__, read_buf, node->key_name, node->value);
00316         ret = ARM_DRIVER_ERROR;
00317     }
00318 out2:
00319     if(read_buf) free(read_buf);
00320 out1:
00321     drv->Close(hkey);
00322     hkey = NULL;
00323 out0:
00324     return ret;
00325 }
00326 
00327 
00328 /* @brief  test utility function to delete the cfstore key identified by key_name
00329  * @note    this function expects cfstore to have been initialised with
00330  *          a call to ARM_CFSTORE_DRIVER::Initialize()
00331  */
00332 int32_t cfstore_test_delete(const char* key_name)
00333 {
00334     int32_t ret = ARM_DRIVER_ERROR;
00335     ARM_CFSTORE_DRIVER* drv = &cfstore_driver;
00336     ARM_CFSTORE_HANDLE_INIT(hkey);
00337     ARM_CFSTORE_FMODE flags;
00338 
00339     CFSTORE_FENTRYLOG("%s:entered.\r\n", __func__);
00340     memset(&flags, 0, sizeof(flags));
00341     ret = drv->Open(key_name, flags, hkey);
00342     if(ret < ARM_DRIVER_OK){
00343         return ret;
00344     }
00345     if(hkey != NULL){
00346         ret = drv->Delete(hkey);
00347         drv->Close(hkey);
00348     }
00349     return ret;
00350 }
00351 
00352 /* @brief   test utility function to delete all of the KVs in the cfstore
00353  * @note    this function expects cfstore to have been initialised with
00354  *          a call to ARM_CFSTORE_DRIVER::Initialize()
00355  */
00356 int32_t cfstore_test_delete_all(void)
00357 {
00358     const char* key_name_query = "*";
00359     char key_name[CFSTORE_KEY_NAME_MAX_LENGTH+1];
00360     uint8_t len = CFSTORE_KEY_NAME_MAX_LENGTH+1;
00361     int32_t ret = ARM_DRIVER_ERROR;
00362     ARM_CFSTORE_DRIVER* drv = &cfstore_driver;
00363     ARM_CFSTORE_HANDLE_INIT(next);
00364     ARM_CFSTORE_HANDLE_INIT(prev);
00365 
00366     CFSTORE_FENTRYLOG("%s:entered.\r\n", __func__);
00367     while((ret = drv->Find(key_name_query, prev, next)) == ARM_DRIVER_OK)
00368     {
00369         len = CFSTORE_KEY_NAME_MAX_LENGTH+1;
00370         drv->GetKeyName(next, key_name, &len);
00371         CFSTORE_TP(CFSTORE_TP_DELETE, "%s:deleting key_name=%s, len=%d\r\n", __func__, key_name, (int) len);
00372         ret = drv->Delete(next);
00373         if(ret < ARM_DRIVER_OK){
00374             CFSTORE_ERRLOG("%s:Error: failed to delete key_name=%s, len=%d\r\n", __func__, key_name, (int) len);
00375             return ret;
00376         }
00377         CFSTORE_HANDLE_SWAP(prev, next);
00378     }
00379     if(ret == ARM_CFSTORE_DRIVER_ERROR_KEY_NOT_FOUND) {
00380         /* as expected, no more keys have been found by the Find()*/
00381         ret = ARM_DRIVER_OK;
00382     }
00383     CFSTORE_FENTRYLOG("%s:exiting (ret=%d).\r\n", __func__, (int) ret);
00384     return ret;
00385 }
00386 
00387 
00388 /* @brief   test utility function to create a KV in the cfstore
00389  * @note    this function expects cfstore to have been initialised with
00390  *          a call to ARM_CFSTORE_DRIVER::Initialize()
00391  */
00392 int32_t cfstore_test_create(const char* key_name, const char* data, ARM_CFSTORE_SIZE* len, ARM_CFSTORE_KEYDESC* kdesc)
00393 {
00394     int32_t ret = ARM_DRIVER_ERROR;
00395     ARM_CFSTORE_SIZE value_len = 0;
00396     ARM_CFSTORE_DRIVER* drv = &cfstore_driver;
00397     ARM_CFSTORE_HANDLE_INIT(hkey);
00398 
00399     CFSTORE_FENTRYLOG("%s:entered.\r\n", __func__);
00400     value_len = *len;
00401     kdesc->drl = ARM_RETENTION_WHILE_DEVICE_ACTIVE;
00402     ret = drv->Create(key_name, value_len, kdesc, hkey);
00403     if(ret < ARM_DRIVER_OK){
00404         return ret;
00405     }
00406     value_len = *len;
00407     ret = drv->Write(hkey, data, &value_len);
00408     if(ret < ARM_DRIVER_OK){
00409         drv->Close(hkey);
00410         return ret;
00411     }
00412     if(value_len != *len){
00413         drv->Close(hkey);
00414         return ARM_DRIVER_ERROR;
00415     }
00416     drv->Close(hkey);
00417     return ret;
00418 }
00419 
00420 /* @brief   test utility function to create KVs from the supplied table
00421  * @note    this function expects cfstore to have been initialised with
00422  *          a call to ARM_CFSTORE_DRIVER::Initialize()
00423  */
00424 int32_t cfstore_test_create_table(const cfstore_kv_data_t* table)
00425 {
00426     int32_t ret = ARM_DRIVER_ERROR;
00427     ARM_CFSTORE_SIZE len = 0;
00428     cfstore_kv_data_t* node = NULL;
00429     ARM_CFSTORE_KEYDESC kdesc;
00430 
00431     (void) node; /* suppresses warning when building release */
00432     CFSTORE_FENTRYLOG("%s:entered.\r\n", __func__);
00433     memset(&kdesc, 0, sizeof(kdesc));
00434     kdesc.drl = ARM_RETENTION_WHILE_DEVICE_ACTIVE;
00435     while(table->key_name != NULL)
00436     {
00437         len = strlen(table->value);
00438         ret = cfstore_test_create(table->key_name, table->value, &len, &kdesc);
00439         if(ret < ARM_DRIVER_OK){
00440             CFSTORE_ERRLOG("%s:Error: failed to create node (key_name=\"%s\", value=\"%s\")\r\n", __func__, node->key_name, node->value);
00441             return ret;
00442         }
00443         table++;
00444     }
00445     return ret;
00446 }
00447 
00448 cfstore_kv_data_t cfstore_test_init_1_data[] = {
00449         CFSTORE_INIT_1_TABLE_HEAD,
00450         { "b", "1"},
00451         { "c", "12"},
00452         { "d", "123"},
00453         { "e", "1234"},
00454         { "g", "12345"},
00455         { "h", "123456"},
00456         { "i", "1234567"},
00457         { "j", "12345678"},
00458         { "k", "123456789"},
00459         { "l", "1234567890"},
00460         { "m", "12345678901"},
00461         { "n", "123456789012"},
00462         { "o", "1234567890123"},
00463         { "p", "12345678901234"},
00464         { "q", "123456789012345"},
00465         { "r", "1234567890123456"},
00466         { "0", "a"},
00467         { "01", "ab"},
00468         { "012", "abc"},
00469         { "0123", "abcd"},
00470         { "01234", "abcde"},
00471         { "012345", "abcdef"},
00472         { "0123456", "abcdefg"},
00473         { "01234567", "abcdefgh"},
00474         { "012345678", "abcdefghi"},
00475         { "0123456789", "abcdefghj"},
00476         { "0123456789a", "abcdefghjk"},
00477         { "0123456789ab", "abcdefghjkl"},
00478         { "0123456789abc", "abcdefghjklm"},
00479         { "0123456789abcd", "abcdefghjklmn"},
00480         { "0123456789abcde", "abcdefghjklmno"},
00481         { "0123456789abcdef", "abcdefghjklmnop"},
00482         { "0123456789abcdef0", "abcdefghjklmnopq"},
00483         { "0123456789abcdef01", "abcdefghjklmnopqr"},
00484         { "0123456789abcdef012", "abcdefghjklmnopqrs"},
00485         { "0123456789abcdef0123", "abcdefghjklmnopqrst"},
00486         { "0123456789abcdef01234", "abcdefghjklmnopqrstu"},
00487         { "0123456789abcdef012345", "abcdefghjklmnopqrstuv"},
00488         CFSTORE_INIT_1_TABLE_MID_NODE,
00489         { "0123456789abcdef01234567", "abcdefghjklmnopqrstuvwx"},
00490         { "0123456789abcdef012345678", "abcdefghjklmnopqrstuvwxy"},
00491         { "0123456789abcdef0123456789", "abcdefghjklmnopqrstuvwxyz"},
00492         { "0123456789abcdef0123456789a", "b"},
00493         { "0123456789abcdef0123456789ab", "c"},
00494         { "0123456789abcdef0123456789abc", "d"},
00495         { "0123456789abcdef0123456789abcd", "e"},
00496         { "0123456789abcdef0123456789abcde", "f"},
00497         { "0123456789abcdef0123456789abcdef", "g"},
00498         { "com.arm.mbed.wifi.accesspoint.essid", ""},
00499         { "com.arm.mbed.wifi.accesspoint.essid2", ""},
00500         { "yotta.your-yotta-registry-module-name.module1", ""},
00501         { "yotta.hello-world.animal{wobbly-dog}{foot}frontLeft", "missing"},
00502         { "yotta.hello-world.animal{wobbly-dog}{foot}frontRight", "present"},
00503         { "yotta.hello-world.animal{wobbly-dog}{foot}backLeft", "half present"},
00504         { "piety.demands.us.to.honour.truth.above.our.friends", "Aristotle"},
00505         { "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", "TheRollingStone" },
00506         CFSTORE_INIT_1_TABLE_TAIL,
00507         { NULL, NULL},
00508 };
00509 
00510 
00511 /* @brief   utility test function to initialise cfstore sram area with some
00512  *          KV's to manipulate
00513  * @note    this function expects cfstore to have been initialised with
00514  *          a call to ARM_CFSTORE_DRIVER::Initialize()
00515  */
00516 int32_t cfstore_test_init_1(void)
00517 {
00518     char* read_buf = NULL;
00519     const uint8_t key_name_max_len = CFSTORE_KEY_NAME_MAX_LENGTH+1;
00520     uint8_t key_name_len = 0;
00521     char key_name_buf[CFSTORE_KEY_NAME_MAX_LENGTH+1];
00522     int32_t ret = ARM_DRIVER_ERROR;
00523     ARM_CFSTORE_SIZE len = 0;
00524     ARM_CFSTORE_SIZE max_len = 0;
00525     ARM_CFSTORE_DRIVER* drv = &cfstore_driver;
00526     cfstore_kv_data_t* node = NULL;
00527     ARM_CFSTORE_KEYDESC kdesc;
00528     ARM_CFSTORE_HANDLE_INIT(hkey);
00529 
00530     CFSTORE_FENTRYLOG("%s:entered\r\n", __func__);
00531     memset(&kdesc, 0, sizeof(kdesc));
00532     memset(key_name_buf, 0, CFSTORE_KEY_NAME_MAX_LENGTH+1);
00533 
00534     /*scan for max length of value blob*/
00535     node = cfstore_test_init_1_data;
00536     while(node->key_name != NULL)
00537     {
00538         len = strlen(node->value);
00539         if(len > max_len){
00540             max_len = len;
00541             max_len++;
00542         }
00543         node++;
00544     }
00545     read_buf = (char*) malloc(max_len);
00546     if(read_buf == NULL) {
00547         CFSTORE_ERRLOG("%s:Error: failed to allocated read buffer \r\n", __func__);
00548         return ret;
00549     }
00550     kdesc.drl = ARM_RETENTION_WHILE_DEVICE_ACTIVE;
00551     node = cfstore_test_init_1_data;
00552     while(node->key_name != NULL)
00553     {
00554         CFSTORE_DBGLOG("%s:About to create new node (key_name=\"%s\", value=\"%s\")\r\n", __func__, node->key_name, node->value);
00555         ret = drv->Create(node->key_name, strlen(node->value), &kdesc, hkey);
00556         if(ret < ARM_DRIVER_OK){
00557             CFSTORE_ERRLOG("%s:Error: failed to create node (key_name=\"%s\", value=\"%s\")\r\n", __func__, node->key_name, node->value);
00558             return ret;
00559         }
00560 
00561         CFSTORE_DBGLOG("%s:length of KV=%d (key_name=\"%s\", value=\"%s\")\r\n", __func__, (int) len, node->key_name, node->value);
00562         len = strlen(node->value);
00563         ret = drv->Write(hkey, (char*) node->value, &len);
00564         if(ret < ARM_DRIVER_OK){
00565             CFSTORE_ERRLOG("%s:Error: failed to write key (key_name=\"%s\", value=\"%s\")\r\n", __func__, node->key_name, node->value);
00566             drv->Close(hkey);
00567             return ret;
00568         }
00569         if(len != strlen(node->value)){
00570             CFSTORE_ERRLOG("%s:Error: failed to write full value data (key_name=\"%s\", value=\"%s\"), len=%d\r\n", __func__, node->key_name, node->value, (int) len);
00571             drv->Close(hkey);
00572             return ARM_DRIVER_ERROR;
00573         }
00574         /* read the data back*/
00575         len = strlen(node->value);
00576         memset(read_buf, 0, max_len);
00577         ret = drv->Read(hkey, read_buf, &len);
00578         if(ret < ARM_DRIVER_OK){
00579             CFSTORE_ERRLOG("%s:Error: failed to read key (key_name=\"%s\", value=\"%s\")\r\n", __func__, node->key_name, node->value);
00580             drv->Close(hkey);
00581             return ret;
00582         }
00583         if(len != strlen(node->value)){
00584             CFSTORE_ERRLOG("%s:Error: failed to read full value data (key_name=\"%s\", value=\"%s\"), len=%d, ret=%d\r\n", __func__, node->key_name, node->value, (int) len, (int) ret);
00585             drv->Close(hkey);
00586             return ARM_DRIVER_ERROR;
00587         }
00588         key_name_len = key_name_max_len;
00589         memset(key_name_buf, 0, key_name_len);
00590         drv->GetKeyName(hkey, key_name_buf, &key_name_len);
00591         if(len != strlen(node->value)){
00592             CFSTORE_ERRLOG("%s:Error: failed to GetKeyName() (key_name=\"%s\", value=\"%s\"), len=%d\r\n", __func__, node->key_name, node->value, (int) len);
00593             drv->Close(hkey);
00594             return ARM_DRIVER_ERROR;
00595         }
00596         /* revert CFSTORE_LOG for more trace */
00597         CFSTORE_DBGLOG("Created KV successfully (key_name=\"%s\", value=\"%s\")\r\n", key_name_buf, read_buf);
00598         drv->Close(hkey);
00599         node++;
00600     }
00601     free(read_buf);
00602     return ret;
00603 }
00604 
00605 /* @brief   test utility function to check a particular KV exists in the
00606  *          cfstore using Find() interface
00607  * @note    this function expects cfstore to have been initialised with
00608  *          a call to ARM_CFSTORE_DRIVER::Initialize()
00609  */
00610 int32_t cfstore_test_kv_is_found(const char* key_name, bool* bfound)
00611 {
00612     CFSTORE_FENTRYLOG("%s:entered.\r\n", __func__);
00613     int32_t ret = ARM_DRIVER_ERROR;
00614     ARM_CFSTORE_HANDLE_INIT(prev);
00615     ARM_CFSTORE_HANDLE_INIT(next);
00616     ARM_CFSTORE_DRIVER* drv = &cfstore_driver;
00617 
00618     CFSTORE_ASSERT(bfound != NULL);
00619     CFSTORE_ASSERT(key_name != NULL);
00620     *bfound = 0;
00621 
00622     ret = drv->Find(key_name, prev, next);
00623     if(ret == ARM_DRIVER_OK){
00624         *bfound = 1;
00625         CFSTORE_DBGLOG("%s:Found key_name=\"%s\", about to call close.\r\n", __func__, key_name);
00626         drv->Close(next);
00627     }
00628     return ret;
00629 }
00630 
00631 
00632 /* @brief   support function for generating a kv_name
00633  * @param   name    buffer to hold kv name
00634  * @param   len     length of kv name to generate
00635  * @note    braces are not included in the generated names as the names are
00636  *          of varible length and theyre may be unmatched
00637  *
00638  */
00639 #define CFSTORE_TEST_KV_NAME_BUF_MAX_DATA (10+26+26+4)
00640 int32_t cfstore_test_kv_name_gen(char* name, const size_t len)
00641 {
00642     size_t i;
00643     const char buf[CFSTORE_TEST_KV_NAME_BUF_MAX_DATA+1] = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ.-_@";
00644     CFSTORE_FENTRYLOG("%s:entered\n", __func__);
00645     for(i = 0; i < len; i++)
00646     {
00647         name[i] = buf[i % CFSTORE_TEST_KV_NAME_BUF_MAX_DATA];
00648     }
00649     return ARM_DRIVER_OK;
00650 }
00651 
00652 /* @brief   test utility function to read the value blob of a specified KV
00653  * @note    this function expects cfstore to have been initialised with
00654  *          a call to ARM_CFSTORE_DRIVER::Initialize()
00655  */
00656 int32_t cfstore_test_read(const char* key_name, char* data, ARM_CFSTORE_SIZE* len)
00657 {
00658     int32_t ret = ARM_DRIVER_ERROR;
00659     ARM_CFSTORE_DRIVER* drv = &cfstore_driver;
00660     ARM_CFSTORE_HANDLE_INIT(hkey);
00661     ARM_CFSTORE_FMODE flags;
00662 
00663     CFSTORE_FENTRYLOG("%s:entered\r\n", __func__);
00664     memset(&flags, 0, sizeof(flags));
00665     if(key_name == NULL) {
00666         CFSTORE_ERRLOG("%s:invalid key_name argument \r\n", __func__);
00667         goto out0;
00668     }
00669     if(data == NULL) {
00670         CFSTORE_ERRLOG("%s:invalid data argument \r\n", __func__);
00671         goto out0;
00672     }
00673     if(len == NULL) {
00674         CFSTORE_ERRLOG("%s:invalid len argument \r\n", __func__);
00675         goto out0;
00676     }
00677     ret = drv->Open(key_name, flags, hkey);
00678     if(ret < ARM_DRIVER_OK){
00679         CFSTORE_ERRLOG("%s:Error: failed to open node (key_name=\"%s\")(ret=%d)\r\n", __func__, key_name, (int) ret);
00680         goto out1;
00681     }
00682     ret = drv->Read(hkey, data, len);
00683     if(ret < ARM_DRIVER_OK){
00684         CFSTORE_ERRLOG("%s:Error: failed to read key (key_name=\"%s\"\r\n", __func__, key_name);
00685         goto out2;
00686     }
00687 out2:
00688     drv->Close(hkey);
00689 out1:
00690 out0:
00691     return ret;
00692 }
00693 
00694 /* @brief   write the value blob of a specified KV
00695  * @note    this function expects cfstore to have been initialised with
00696  *          a call to ARM_CFSTORE_DRIVER::Initialize()
00697  */
00698 int32_t cfstore_test_write(const char* key_name, const char* data, ARM_CFSTORE_SIZE* len)
00699 {
00700     int32_t ret = ARM_DRIVER_ERROR;
00701     ARM_CFSTORE_DRIVER* drv = &cfstore_driver;
00702     ARM_CFSTORE_HANDLE_INIT(hkey);
00703     ARM_CFSTORE_FMODE flags;
00704 
00705     CFSTORE_FENTRYLOG("%s:entered\r\n", __func__);
00706     memset(&flags, 0, sizeof(flags));
00707     if(key_name == NULL) {
00708         CFSTORE_ERRLOG("%s:Error: invalid key_name argument \r\n", __func__);
00709         goto out0;
00710     }
00711     if(data == NULL) {
00712         CFSTORE_ERRLOG("%s:Error: invalid data argument \r\n", __func__);
00713         goto out0;
00714     }
00715     if(len == NULL) {
00716         CFSTORE_ERRLOG("%s:Error: invalid len argument \r\n", __func__);
00717         goto out0;
00718     }
00719     flags.write = 1;
00720     ret = drv->Open(key_name, flags, hkey);
00721     if(ret < ARM_DRIVER_OK){
00722         CFSTORE_ERRLOG("%s:Error: failed to open node (key_name=\"%s\")(ret=%d)\r\n", __func__, key_name, (int) ret);
00723         goto out1;
00724     }
00725     ret = drv->Write(hkey, data, len);
00726     if(ret < ARM_DRIVER_OK){
00727         CFSTORE_ERRLOG("%s:Error: failed to write key (key_name=\"%s\")\r\n", __func__, key_name);
00728         goto out2;
00729     }
00730 out2:
00731     drv->Close(hkey);
00732 out1:
00733 out0:
00734     return ret;
00735 }
00736 
00737