Exemple #1
0
/* @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;
}
Exemple #2
0
/* @brief   utility test function to initialise cfstore sram area with some
 *          KV's to manipulate
 * @note    this function expects cfstore to have been initialised with
 *          a call to ARM_CFSTORE_DRIVER::Initialize()
 */
int32_t cfstore_test_init_1(void)
{
    char* read_buf = NULL;
    const uint8_t key_name_max_len = CFSTORE_KEY_NAME_MAX_LENGTH+1;
    uint8_t key_name_len = 0;
    char key_name_buf[CFSTORE_KEY_NAME_MAX_LENGTH+1];
    int32_t ret = ARM_DRIVER_ERROR;
    ARM_CFSTORE_SIZE len = 0;
    ARM_CFSTORE_SIZE max_len = 0;
    ARM_CFSTORE_DRIVER* drv = &cfstore_driver;
    cfstore_kv_data_t* node = NULL;
    ARM_CFSTORE_KEYDESC kdesc;
    ARM_CFSTORE_HANDLE_INIT(hkey);

    CFSTORE_FENTRYLOG("%s:entered\r\n", __func__);
    memset(&kdesc, 0, sizeof(kdesc));
    memset(key_name_buf, 0, CFSTORE_KEY_NAME_MAX_LENGTH+1);

    /*scan for max length of value blob*/
    node = cfstore_test_init_1_data;
    while(node->key_name != NULL)
    {
        len = strlen(node->value);
        if(len > max_len){
            max_len = len;
            max_len++;
        }
        node++;
    }
    read_buf = (char*) malloc(max_len);
    if(read_buf == NULL) {
        CFSTORE_ERRLOG("%s:Error: failed to allocated read buffer \r\n", __func__);
        return ret;
    }
    kdesc.drl = ARM_RETENTION_WHILE_DEVICE_ACTIVE;
    node = cfstore_test_init_1_data;
    while(node->key_name != NULL)
    {
        CFSTORE_DBGLOG("%s:About to create new node (key_name=\"%s\", value=\"%s\")\r\n", __func__, node->key_name, node->value);
        ret = drv->Create(node->key_name, strlen(node->value), &kdesc, hkey);
        if(ret < ARM_DRIVER_OK){
            CFSTORE_ERRLOG("%s:Error: failed to create node (key_name=\"%s\", value=\"%s\")\r\n", __func__, node->key_name, node->value);
            return ret;
        }

        CFSTORE_DBGLOG("%s:length of KV=%d (key_name=\"%s\", value=\"%s\")\r\n", __func__, (int) len, node->key_name, node->value);
        len = strlen(node->value);
        ret = drv->Write(hkey, (char*) node->value, &len);
        if(ret < ARM_DRIVER_OK){
            CFSTORE_ERRLOG("%s:Error: failed to write key (key_name=\"%s\", value=\"%s\")\r\n", __func__, node->key_name, node->value);
            drv->Close(hkey);
            return ret;
        }
        if(len != strlen(node->value)){
            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);
            drv->Close(hkey);
            return ARM_DRIVER_ERROR;
        }
        /* read the data back*/
        len = strlen(node->value);
        memset(read_buf, 0, max_len);
        ret = drv->Read(hkey, read_buf, &len);
        if(ret < ARM_DRIVER_OK){
            CFSTORE_ERRLOG("%s:Error: failed to read key (key_name=\"%s\", value=\"%s\")\r\n", __func__, node->key_name, node->value);
            drv->Close(hkey);
            return ret;
        }
        if(len != strlen(node->value)){
            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);
            drv->Close(hkey);
            return ARM_DRIVER_ERROR;
        }
        key_name_len = key_name_max_len;
        memset(key_name_buf, 0, key_name_len);
        drv->GetKeyName(hkey, key_name_buf, &key_name_len);
        if(len != strlen(node->value)){
            CFSTORE_ERRLOG("%s:Error: failed to GetKeyName() (key_name=\"%s\", value=\"%s\"), len=%d\r\n", __func__, node->key_name, node->value, (int) len);
            drv->Close(hkey);
            return ARM_DRIVER_ERROR;
        }
        /* revert CFSTORE_LOG for more trace */
        CFSTORE_DBGLOG("Created KV successfully (key_name=\"%s\", value=\"%s\")\r\n", key_name_buf, read_buf);
        drv->Close(hkey);
        node++;
    }
    free(read_buf);
    return ret;
}
Exemple #3
0
/* @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;
}