add kv_store sample for bearpi_hm_nano

Signed-off-by: Neil Chen <jingsi.chen@petalmail.com>
This commit is contained in:
Neil Chen
2022-08-03 00:38:53 +08:00
parent de4f996220
commit ffdb10f288
7 changed files with 171 additions and 0 deletions
Executable → Regular
View File
Executable → Regular
View File
Executable → Regular
+1
View File
@@ -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",
]
}
Executable → Regular
View File
@@ -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",
]
}
@@ -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: =====
```
@@ -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 <stdio.h>
#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);