takashi kadono / Mbed OS Nucleo_446

Dependencies:   ssd1331

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers read.cpp Source File

read.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 
00018 /** @file read.cpp Test cases to read KVs in the CFSTORE using the drv->Read() API call.
00019  *
00020  * Please consult the documentation under the test-case functions for
00021  * a description of the individual test case.
00022  */
00023 
00024 #include "mbed.h"
00025 #include "cfstore_config.h"
00026 #include "cfstore_test.h"
00027 #include "cfstore_debug.h"
00028 #include "Driver_Common.h"
00029 #include "configuration_store.h"
00030 #include "utest/utest.h"
00031 #include "unity/unity.h"
00032 #include "greentea-client/test_env.h"
00033 #include "cfstore_utest.h"
00034 
00035 #include <stdio.h>
00036 #include <string.h>
00037 #include <inttypes.h>
00038 
00039 using namespace utest::v1;
00040 
00041 static char cfstore_read_utest_msg_g[CFSTORE_UTEST_MSG_BUF_SIZE];
00042 
00043 
00044 
00045 /* KV data for test_01 */
00046 static cfstore_kv_data_t cfstore_read_test_01_kv_data[] = {
00047         CFSTORE_INIT_1_TABLE_MID_NODE,
00048         { NULL, NULL},
00049 };
00050 
00051 /* report whether built/configured for flash sync or async mode */
00052 static control_t cfstore_read_test_00(const size_t call_count)
00053 {
00054     int32_t ret = ARM_DRIVER_ERROR;
00055 
00056     (void) call_count;
00057     ret = cfstore_test_startup();
00058     CFSTORE_TEST_UTEST_MESSAGE(cfstore_read_utest_msg_g, CFSTORE_UTEST_MSG_BUF_SIZE, "%s:Error: failed to perform test startup (ret=%d).\n", __func__, (int) ret);
00059     TEST_ASSERT_MESSAGE(ret >= ARM_DRIVER_OK, cfstore_read_utest_msg_g);
00060     return CaseNext;
00061 }
00062 
00063 
00064 /* @brief check char at offset is as expected */
00065 static int32_t cfstore_read_seek_test(ARM_CFSTORE_HANDLE  hkey, uint32_t offset, const char expected)
00066 {
00067     ARM_CFSTORE_SIZE len = 1;
00068     char read_buf[1];
00069     int32_t ret = ARM_DRIVER_ERROR;
00070     ARM_CFSTORE_DRIVER* drv = &cfstore_driver;
00071 
00072     ret = drv->Rseek(hkey, offset);
00073     if(ret < ARM_DRIVER_OK){
00074         CFSTORE_ERRLOG("%s:failed to Rseek() to desired offset (offset=%d) (ret=%d).\n", __func__, (int) offset, (int) ret);
00075         goto out0;
00076     }
00077     ret = drv->Read(hkey, read_buf, &len);
00078     if(ret < ARM_DRIVER_OK){
00079         CFSTORE_ERRLOG("%s:failed to Read() (offset=%d)(ret=%d).\n", __func__, (int) offset, (int) ret);
00080         goto out0;
00081     }
00082     if(read_buf[0] != expected){
00083         ret = ARM_DRIVER_ERROR;
00084         goto out0;
00085     }
00086 out0:
00087     return ret;
00088 }
00089 
00090 /** @brief
00091  *
00092  * @return on success returns CaseNext to continue to next test case, otherwise will assert on errors.
00093  */
00094 control_t cfstore_read_test_01_end (const size_t call_count)
00095 {
00096     int32_t ret = ARM_DRIVER_ERROR;
00097     ARM_CFSTORE_SIZE len = 0;
00098     ARM_CFSTORE_DRIVER* drv = &cfstore_driver;
00099     ARM_CFSTORE_KEYDESC kdesc;
00100     ARM_CFSTORE_HANDLE_INIT(hkey);
00101     cfstore_test_rw_data_entry_t *node;
00102     ARM_CFSTORE_FMODE flags;
00103 
00104     CFSTORE_DBGLOG("%s:entered\n", __func__);
00105     (void) call_count;
00106     memset(&kdesc, 0, sizeof(kdesc));
00107     memset(&flags, 0, sizeof(flags));
00108 
00109     /* create a key for reading */
00110     kdesc.drl = ARM_RETENTION_WHILE_DEVICE_ACTIVE;
00111     len = strlen(cfstore_read_test_01_kv_data[0].value);
00112     ret = cfstore_test_create(cfstore_read_test_01_kv_data[0].key_name, (char*) cfstore_read_test_01_kv_data[0].value, &len, &kdesc);
00113     CFSTORE_TEST_UTEST_MESSAGE(cfstore_read_utest_msg_g, CFSTORE_UTEST_MSG_BUF_SIZE, "%s:Error: failed to create KV in store (ret=%d).\n", __func__, (int) ret);
00114     TEST_ASSERT_MESSAGE(ret >= ARM_DRIVER_OK, cfstore_read_utest_msg_g);
00115 
00116     /* now open the newly created key */
00117     ret = drv->Open(cfstore_read_test_01_kv_data[0].key_name, flags, hkey);
00118     CFSTORE_TEST_UTEST_MESSAGE(cfstore_read_utest_msg_g, CFSTORE_UTEST_MSG_BUF_SIZE, "%s:Error: failed to open node (key_name=\"%s\", value=\"%s\")(ret=%d)\n", __func__, cfstore_read_test_01_kv_data[0].key_name, cfstore_read_test_01_kv_data[0].value, (int) ret);
00119     TEST_ASSERT_MESSAGE(ret >= ARM_DRIVER_OK, cfstore_read_utest_msg_g);
00120 
00121     /* seek back and forth in key and check the characters are as expected */
00122     node = cfstore_test_rw_data_table;
00123     while(node->offset != CFSTORE_TEST_RW_TABLE_SENTINEL)
00124     {
00125         ret = cfstore_read_seek_test(hkey, node->offset, node->rw_char);
00126         CFSTORE_TEST_UTEST_MESSAGE(cfstore_read_utest_msg_g, CFSTORE_UTEST_MSG_BUF_SIZE, "%s:Error: Rseek() to offset (%d) didn't read expected char (\'%c\') (ret=%d)\n", __func__, (int) node->offset, node->rw_char, (int) ret);
00127         TEST_ASSERT_MESSAGE(ret >= ARM_DRIVER_OK, cfstore_read_utest_msg_g);
00128         node++;
00129     }
00130     CFSTORE_TEST_UTEST_MESSAGE(cfstore_read_utest_msg_g, CFSTORE_UTEST_MSG_BUF_SIZE, "%s:Error: Close() call failed.\n", __func__);
00131     TEST_ASSERT_MESSAGE(drv->Close(hkey) >= ARM_DRIVER_OK, cfstore_read_utest_msg_g);
00132 
00133     ret = drv->Uninitialize();
00134     CFSTORE_TEST_UTEST_MESSAGE(cfstore_read_utest_msg_g, CFSTORE_UTEST_MSG_BUF_SIZE, "%s:Error: Uninitialize() call failed.\n", __func__);
00135     TEST_ASSERT_MESSAGE(ret >= ARM_DRIVER_OK, cfstore_read_utest_msg_g);
00136     return CaseNext;
00137 }
00138 
00139 
00140 /** @brief
00141  *
00142  * @return on success returns CaseNext to continue to next test case, otherwise will assert on errors.
00143  */
00144 control_t cfstore_read_test_02_end (const size_t call_count)
00145 {
00146     (void) call_count;
00147     /*todo: implement test */
00148     CFSTORE_TEST_UTEST_MESSAGE(cfstore_read_utest_msg_g, CFSTORE_UTEST_MSG_BUF_SIZE, "%s:Warn: Not implemented\n", __func__);
00149     CFSTORE_DBGLOG("%s: WARN: requires implementation\n", __func__);
00150     TEST_ASSERT_MESSAGE(true, cfstore_read_utest_msg_g);
00151     return CaseNext;
00152 }
00153 
00154 
00155 /// @cond CFSTORE_DOXYGEN_DISABLE
00156 utest::v1::status_t greentea_setup(const size_t number_of_cases)
00157 {
00158     GREENTEA_SETUP(200, "default_auto");
00159     return greentea_test_setup_handler(number_of_cases);
00160 }
00161 
00162 Case cases[] = {
00163            /*          1         2         3         4         5         6        7  */
00164            /* 1234567890123456789012345678901234567890123456789012345678901234567890 */
00165         Case("READ_test_00", cfstore_read_test_00),
00166         Case("READ_test_01_start", cfstore_utest_default_start),
00167         Case("READ_test_01_end", cfstore_read_test_01_end ),
00168         Case("READ_test_02_start", cfstore_utest_default_start),
00169         Case("READ_test_02_end", cfstore_read_test_02_end ),
00170 };
00171 
00172 
00173 /* Declare your test specification with a custom setup handler */
00174 Specification specification(greentea_setup, cases);
00175 
00176 int main()
00177 {
00178     return !Harness::run(specification);
00179 }
00180 /// @endcond