From ffdb10f2885321e6c2d355f4c945e22a4932ef22 Mon Sep 17 00:00:00 2001 From: Neil Chen Date: Wed, 3 Aug 2022 00:38:53 +0800 Subject: [PATCH] add kv_store sample for bearpi_hm_nano Signed-off-by: Neil Chen --- LICENSE | 0 OAT.xml | 0 bearpi_hm_nano/app/BUILD.gn | 1 + bearpi_hm_nano/app/README.md | 0 bearpi_hm_nano/app/Z2_basic_kv_store/BUILD.gn | 21 +++++ .../app/Z2_basic_kv_store/README.md | 82 +++++++++++++++++++ .../app/Z2_basic_kv_store/kv_store_example.c | 67 +++++++++++++++ 7 files changed, 171 insertions(+) mode change 100755 => 100644 LICENSE mode change 100755 => 100644 OAT.xml mode change 100755 => 100644 bearpi_hm_nano/app/BUILD.gn mode change 100755 => 100644 bearpi_hm_nano/app/README.md create mode 100644 bearpi_hm_nano/app/Z2_basic_kv_store/BUILD.gn create mode 100644 bearpi_hm_nano/app/Z2_basic_kv_store/README.md create mode 100644 bearpi_hm_nano/app/Z2_basic_kv_store/kv_store_example.c diff --git a/LICENSE b/LICENSE old mode 100755 new mode 100644 diff --git a/OAT.xml b/OAT.xml old mode 100755 new mode 100644 diff --git a/bearpi_hm_nano/app/BUILD.gn b/bearpi_hm_nano/app/BUILD.gn old mode 100755 new mode 100644 index 8fe883e..4acdb83 --- a/bearpi_hm_nano/app/BUILD.gn +++ b/bearpi_hm_nano/app/BUILD.gn @@ -50,5 +50,6 @@ lite_component("app") { # "D12_iot_cloud_oc_gps:cloud_oc_gps", # "Z1_basic_flash_ylc:flash_example", + # "Z2_basic_kv_store:kv_store_example", ] } diff --git a/bearpi_hm_nano/app/README.md b/bearpi_hm_nano/app/README.md old mode 100755 new mode 100644 diff --git a/bearpi_hm_nano/app/Z2_basic_kv_store/BUILD.gn b/bearpi_hm_nano/app/Z2_basic_kv_store/BUILD.gn new file mode 100644 index 0000000..d042dd9 --- /dev/null +++ b/bearpi_hm_nano/app/Z2_basic_kv_store/BUILD.gn @@ -0,0 +1,21 @@ +# Copyright (c) 2020 Nanjing Xiaoxiongpai Intelligent Technology Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +static_library("kv_store_example") { + sources = [ "kv_store_example.c" ] + + include_dirs = [ + "//base/iothardware/peripheral/interfaces/inner_api", + "//utils/native/lite/include", + ] +} diff --git a/bearpi_hm_nano/app/Z2_basic_kv_store/README.md b/bearpi_hm_nano/app/Z2_basic_kv_store/README.md new file mode 100644 index 0000000..16d59a3 --- /dev/null +++ b/bearpi_hm_nano/app/Z2_basic_kv_store/README.md @@ -0,0 +1,82 @@ +# 公共基础子系统开发 —— kv存储(基于BearPi-HM_Nano开发板) +本示例将演示如何使用kv存储相关的API + +## kv_store API分析 +本示例主要使用了以下API完成kv存储相关的验证。 + +### UtilsGetValue() +```c +int UtilsGetValue(const char* key, char* value, unsigned int len); +``` +**描述:** + +从文件系统或cache中获取键 key 对应的值 + +**参数:** + +|名字|描述| +|:--|:------| +| key | 键 | +| value | 指向存储键所对应的值所在的地址 | +| len | 存放value的存储区大小 | + +### UtilsSetValue() +```c +int UtilsSetValue(const char* key, const char* value); +``` +**描述:** + +新增或更新存储在文件系统或cache中的键 key 所对应的值 + +**参数:** + +|名字|描述| +|:--|:------| +| key | 键 | +| value | 待新增或更新的值 | + +### UtilsDeleteValue() +```c +int UtilsDeleteValue(const char* key); +``` +**描述:** + +删除存储在文件系统或cache中的键 key 所对应的值 + +**参数:** + +|名字|描述| +|:--|:------| +| key | 键 | + + +## 编译调试 + +### 修改 BUILD.gn 文件 + +修改 `device\board\bearpi\bearpi_hm_nano\app` 路径下的 BUILD.gn 文件,指定 `Z2_basic_kv_store` 参与编译。 + +```r + # "D11_iot_cloud_oc_agriculture:cloud_oc_agriculture", + # "D12_iot_cloud_oc_gps:cloud_oc_gps", + + # "Z1_basic_flash_ylc:flash_example", + "Z2_basic_kv_store:kv_store_example", +``` + +### 运行结果 + +示例代码编译烧录后,按下开发板的RESET按键,通过串口助手可以看到kv操作过程。 + +```sh +hiview init success. +[PARAM][param_service.c:125]ParamServiceTask start +get value failed! +===== key: user, value: ===== +===== key: user, value: Zhang San ===== +delete key-value for user success. +get value failed! +===== key: user, value: ===== + +``` + diff --git a/bearpi_hm_nano/app/Z2_basic_kv_store/kv_store_example.c b/bearpi_hm_nano/app/Z2_basic_kv_store/kv_store_example.c new file mode 100644 index 0000000..c1b9d81 --- /dev/null +++ b/bearpi_hm_nano/app/Z2_basic_kv_store/kv_store_example.c @@ -0,0 +1,67 @@ +/* + * Copyright (c) 2020 Nanjing Xiaoxiongpai Intelligent Technology Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include + +#include "ohos_init.h" +#include "cmsis_os2.h" +#include "kv_store.h" + +#define BOOT_WAIT_TIME 200 + +static void ShowValue(const char* key) +{ + if (!key) { + return; + } + + char buf[512] = {0}; + + if (UtilsGetValue(key, buf, sizeof(buf)-1) < 0) { + printf("get value failed!\n"); + } + printf("===== key: %s, value: %s =====\n", key, buf); +} + +static void KvStoreExampleEntry(void) +{ + const char* usernameKey = "user"; + const char* usernameValue = "Zhang San"; + + // wait a while for checking log easily + osDelay(BOOT_WAIT_TIME); + + // 1. get value failed if no key-value exist + ShowValue(usernameKey); + + // 2. save key-value + if (UtilsSetValue(usernameKey, usernameValue) < 0) { + printf("set username value failed!\n"); + } + + // 3. get value success if key-value exist + ShowValue(usernameKey); + + // 4. get value failed after key-value deleted + if (UtilsDeleteValue(usernameKey) < 0) { + printf("delete key-value for %s failed!\n", usernameKey); + } else { + printf("delete key-value for %s success.\n", usernameKey); + } + + ShowValue(usernameKey); +} + +APP_FEATURE_INIT(KvStoreExampleEntry); \ No newline at end of file