/* @brief test utility function to delete all of the KVs in the cfstore * @note this function expects cfstore to have been initialised with * a call to ARM_CFSTORE_DRIVER::Initialize() */ int32_t cfstore_test_delete_all(void) { const char* key_name_query = "*"; char key_name[CFSTORE_KEY_NAME_MAX_LENGTH+1]; uint8_t len = CFSTORE_KEY_NAME_MAX_LENGTH+1; int32_t ret = ARM_DRIVER_ERROR; ARM_CFSTORE_DRIVER* drv = &cfstore_driver; ARM_CFSTORE_HANDLE_INIT(next); ARM_CFSTORE_HANDLE_INIT(prev); CFSTORE_FENTRYLOG("%s:entered.\r\n", __func__); while((ret = drv->Find(key_name_query, prev, next)) == ARM_DRIVER_OK) { len = CFSTORE_KEY_NAME_MAX_LENGTH+1; drv->GetKeyName(next, key_name, &len); CFSTORE_TP(CFSTORE_TP_DELETE, "%s:deleting key_name=%s, len=%d\r\n", __func__, key_name, (int) len); ret = drv->Delete(next); if(ret < ARM_DRIVER_OK){ CFSTORE_ERRLOG("%s:Error: failed to delete key_name=%s, len=%d\r\n", __func__, key_name, (int) len); return ret; } CFSTORE_HANDLE_SWAP(prev, next); } if(ret == ARM_CFSTORE_DRIVER_ERROR_KEY_NOT_FOUND) { /* as expected, no more keys have been found by the Find()*/ ret = ARM_DRIVER_OK; } CFSTORE_FENTRYLOG("%s:exiting (ret=%d).\r\n", __func__, (int) ret); return ret; }
/* @brief test utility function to check a particular KV exists in the * cfstore using Find() interface * @note this function expects cfstore to have been initialised with * a call to ARM_CFSTORE_DRIVER::Initialize() */ int32_t cfstore_test_kv_is_found(const char* key_name, bool* bfound) { CFSTORE_FENTRYLOG("%s:entered.\r\n", __func__); int32_t ret = ARM_DRIVER_ERROR; ARM_CFSTORE_HANDLE_INIT(prev); ARM_CFSTORE_HANDLE_INIT(next); ARM_CFSTORE_DRIVER* drv = &cfstore_driver; CFSTORE_ASSERT(bfound != NULL); CFSTORE_ASSERT(key_name != NULL); *bfound = 0; ret = drv->Find(key_name, prev, next); if(ret == ARM_DRIVER_OK){ *bfound = 1; CFSTORE_DBGLOG("%s:Found key_name=\"%s\", about to call close.\r\n", __func__, key_name); drv->Close(next); } return ret; }
/* @brief fsm handler called on entry to flushing state */ static void cfstore_flush_fsm_flush_on_entry(void* context) { bool bfound = false; int32_t ivalue = 0; int32_t ret = ARM_DRIVER_ERROR; ARM_CFSTORE_DRIVER* drv = &cfstore_driver; const char* key_name_query = "*"; char value[CFSTORE_KEY_NAME_MAX_LENGTH+1]; ARM_CFSTORE_SIZE len = CFSTORE_KEY_NAME_MAX_LENGTH+1; ARM_CFSTORE_HANDLE_INIT(next); ARM_CFSTORE_HANDLE_INIT(prev); ARM_CFSTORE_KEYDESC kdesc; cfstore_flush_ctx_t* ctx = (cfstore_flush_ctx_t*) context; /* check that the mtd is in synchronous mode */ CFSTORE_FENTRYLOG("%s:entered: \r\n", __func__); memset(&kdesc, 0, sizeof(kdesc)); CFSTORE_TEST_UTEST_MESSAGE(cfstore_flush_utest_msg_g, CFSTORE_FLUSH_UTEST_MSG_BUF_SIZE, "%s:Error: entered handler(%s) but not in flushing state (fsm->state=%d)\r\n", __func__, __func__, (int) ctx->fsm.state); TEST_ASSERT_MESSAGE(ctx->fsm.state == cfstore_flush_fsm_state_flushing, cfstore_flush_utest_msg_g); /* try to read key; should not be found */ ret = cfstore_test_kv_is_found(cfstore_flush_test_02_kv_data->key_name, &bfound); if(ret != ARM_DRIVER_OK && ret != ARM_CFSTORE_DRIVER_ERROR_KEY_NOT_FOUND){ CFSTORE_TEST_UTEST_MESSAGE(cfstore_flush_utest_msg_g, CFSTORE_FLUSH_UTEST_MSG_BUF_SIZE, "%s:Error: cfstore_test_kv_is_found() call failed (ret=%d).\r\n", __func__, (int) ret); TEST_ASSERT_MESSAGE(false, cfstore_flush_utest_msg_g); } if(!bfound) { /* first time start up. nothing is stored in cfstore flash. check this is the case */ while(drv->Find(key_name_query, prev, next) == ARM_DRIVER_OK) { CFSTORE_TEST_UTEST_MESSAGE(cfstore_flush_utest_msg_g, CFSTORE_FLUSH_UTEST_MSG_BUF_SIZE, "%s:Error: have found an entry in cfstore when none should be present", __func__); TEST_ASSERT_MESSAGE(false, cfstore_flush_utest_msg_g); } /* no entries found, which is correct. * store a value */ len = strlen(cfstore_flush_test_02_kv_data->value); ret = cfstore_test_create(cfstore_flush_test_02_kv_data->key_name, cfstore_flush_test_02_kv_data->value, &len, &kdesc); CFSTORE_TEST_UTEST_MESSAGE(cfstore_flush_utest_msg_g, CFSTORE_FLUSH_UTEST_MSG_BUF_SIZE, "%s:Error:1: failed to write kv data (ret=%d).\r\n", __func__, (int) ret); TEST_ASSERT_MESSAGE(ret >= ARM_DRIVER_OK, cfstore_flush_utest_msg_g); /* flush to flash */ ret = drv->Flush(); CFSTORE_TEST_UTEST_MESSAGE(cfstore_flush_utest_msg_g, CFSTORE_FLUSH_UTEST_MSG_BUF_SIZE, "%s:Error: failed to flush data to cfstore flash (ret=%d).\r\n", __func__, (int) ret); TEST_ASSERT_MESSAGE(ret >= ARM_DRIVER_OK, cfstore_flush_utest_msg_g); /* revert to CFSTORE_LOG if more trace required */ CFSTORE_DBGLOG("FLUSH: Success pending for new KV creation (name=%s, value=%s)\n", cfstore_flush_test_02_kv_data->key_name, cfstore_flush_test_02_kv_data->value); } else { /*read the value, increment by 1 and write value back */ len = CFSTORE_KEY_NAME_MAX_LENGTH+1; ret = cfstore_test_read(cfstore_flush_test_02_kv_data->key_name, value, &len); CFSTORE_TEST_UTEST_MESSAGE(cfstore_flush_utest_msg_g, CFSTORE_FLUSH_UTEST_MSG_BUF_SIZE, "%s:Error: failed to read kv data (ret=%d).\r\n", __func__, (int) ret); TEST_ASSERT_MESSAGE(ret >= ARM_DRIVER_OK, cfstore_flush_utest_msg_g); ivalue = atoi(value); /* revert to CFSTORE_LOG if more trace required */ CFSTORE_DBGLOG("FLUSH: Read KV from flash (name=%s, value=%d)\n", cfstore_flush_test_02_kv_data->key_name, (int) ivalue); /* increment value */ ++ivalue; snprintf(value, CFSTORE_KEY_NAME_MAX_LENGTH+1, "%d", (int) ivalue); len = strlen(value); ret = cfstore_test_write(cfstore_flush_test_02_kv_data->key_name, value, &len); CFSTORE_TEST_UTEST_MESSAGE(cfstore_flush_utest_msg_g, CFSTORE_FLUSH_UTEST_MSG_BUF_SIZE, "%s:Error: failed to write kv data (ret=%d).\r\n", __func__, (int) ret); TEST_ASSERT_MESSAGE(ret >= ARM_DRIVER_OK, cfstore_flush_utest_msg_g); /* flush to flash */ ret = drv->Flush(); CFSTORE_TEST_UTEST_MESSAGE(cfstore_flush_utest_msg_g, CFSTORE_FLUSH_UTEST_MSG_BUF_SIZE, "%s:Error: failed to flush data to cfstore flash (ret=%d).\r\n", __func__, (int) ret); TEST_ASSERT_MESSAGE(ret >= ARM_DRIVER_OK, cfstore_flush_utest_msg_g); /* revert to CFSTORE_LOG if more trace required */ CFSTORE_DBGLOG("FLUSH: Success pending for new KV value to flash (name=%s, value=%d)\n", cfstore_flush_test_02_kv_data->key_name, (int) ivalue); } return; }
/* @brief function to dump contents of cfstore */ int32_t cfstore_test_dump(void) { const char* key_name_query = "*"; char* read_buf = NULL; char key_name[CFSTORE_KEY_NAME_MAX_LENGTH+1]; uint8_t len = CFSTORE_KEY_NAME_MAX_LENGTH+1; ARM_CFSTORE_SIZE vlen = 0; int32_t ret = ARM_DRIVER_ERROR; ARM_CFSTORE_DRIVER* drv = &cfstore_driver; ARM_CFSTORE_HANDLE_INIT(next); ARM_CFSTORE_HANDLE_INIT(prev); ARM_CFSTORE_CAPABILITIES caps = cfstore_driver.GetCapabilities(); CFSTORE_FENTRYLOG("%s:entered\n", __func__); CFSTORE_LOG("CFSTORE Flash Entries%s", "\n"); CFSTORE_LOG("=====================%s", "\n\n"); while((ret = drv->Find(key_name_query, prev, next)) == ARM_DRIVER_OK) { len = CFSTORE_KEY_NAME_MAX_LENGTH+1; ret = drv->GetKeyName(next, key_name, &len); if(ret < ARM_DRIVER_OK){ CFSTORE_ERRLOG("Error: failed to get key name%s", "\n"); break; } ret = drv->GetValueLen(next, &vlen); if(ret < ARM_DRIVER_OK){ CFSTORE_ERRLOG("Error: failed to get value length%s", "\n"); break; } read_buf = (char*) malloc(vlen+1); if(read_buf == NULL){ CFSTORE_ERRLOG("Error: failed to malloc() read buffer%s", "\n"); break; } ret = drv->Read(next, read_buf, &vlen); if(ret < ARM_DRIVER_OK){ CFSTORE_ERRLOG("Error: failed to read key value%s", "\n"); free(read_buf); break; } CFSTORE_LOG(" keyname : %s\n", key_name); CFSTORE_LOG(" name len : %d\n", (int) len); CFSTORE_LOG(" value len : %d\n", (int) vlen); CFSTORE_LOG(" data :%s", "\n"); cfstore_test_dump_print_array((const char*) read_buf, vlen); CFSTORE_LOG("%s", ".\n"); free(read_buf); CFSTORE_HANDLE_SWAP(prev, next); } CFSTORE_LOG("%s", ".\n"); CFSTORE_LOG(" caps.asynchronous_ops : %d\n", (int) caps.asynchronous_ops); CFSTORE_LOG("%s", ".\n"); CFSTORE_LOG("== End ==============%s", "\n\n"); if(ret == ARM_CFSTORE_DRIVER_ERROR_KEY_NOT_FOUND) { /* As expected, no more keys have been found by the Find(). */ ret = ARM_DRIVER_OK; } return ret; }