From cabd3b9e7627644817df1e0a3a08ae4827b3a214 Mon Sep 17 00:00:00 2001 From: lly Date: Mon, 1 Jun 2020 09:34:13 +0800 Subject: [PATCH] ble_mesh: Add mesh example common nvs operations --- .../components/example_nvs/CMakeLists.txt | 7 + .../example_nvs/ble_mesh_example_nvs.c | 151 ++++++++++++++++++ .../example_nvs/ble_mesh_example_nvs.h | 26 +++ .../components/example_nvs/component.mk | 6 + 4 files changed, 190 insertions(+) create mode 100644 examples/bluetooth/esp_ble_mesh/components/example_nvs/CMakeLists.txt create mode 100644 examples/bluetooth/esp_ble_mesh/components/example_nvs/ble_mesh_example_nvs.c create mode 100644 examples/bluetooth/esp_ble_mesh/components/example_nvs/ble_mesh_example_nvs.h create mode 100644 examples/bluetooth/esp_ble_mesh/components/example_nvs/component.mk diff --git a/examples/bluetooth/esp_ble_mesh/components/example_nvs/CMakeLists.txt b/examples/bluetooth/esp_ble_mesh/components/example_nvs/CMakeLists.txt new file mode 100644 index 000000000..75b640c6b --- /dev/null +++ b/examples/bluetooth/esp_ble_mesh/components/example_nvs/CMakeLists.txt @@ -0,0 +1,7 @@ +set(COMPONENT_SRCS "ble_mesh_example_nvs.c") + +set(COMPONENT_ADD_INCLUDEDIRS ".") + +set(COMPONENT_REQUIRES nvs_flash) + +register_component() diff --git a/examples/bluetooth/esp_ble_mesh/components/example_nvs/ble_mesh_example_nvs.c b/examples/bluetooth/esp_ble_mesh/components/example_nvs/ble_mesh_example_nvs.c new file mode 100644 index 000000000..f173f3d17 --- /dev/null +++ b/examples/bluetooth/esp_ble_mesh/components/example_nvs/ble_mesh_example_nvs.c @@ -0,0 +1,151 @@ +/* + This example code is in the Public Domain (or CC0 licensed, at your option.) + + Unless required by applicable law or agreed to in writing, this + software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + CONDITIONS OF ANY KIND, either express or implied. +*/ + +#include +#include +#include "sdkconfig.h" +#include "esp_log.h" +#include "nvs_flash.h" + +#define TAG "EXAMPLE_NVS" + +#define NVS_NAME "mesh_example" + +esp_err_t ble_mesh_nvs_open(nvs_handle *handle) +{ + esp_err_t err = ESP_OK; + + if (handle == NULL) { + ESP_LOGE(TAG, "Open, invalid nvs handle"); + return ESP_ERR_INVALID_ARG; + } + + err = nvs_open(NVS_NAME, NVS_READWRITE, handle); + if (err != ESP_OK) { + ESP_LOGE(TAG, "Open, nvs_open failed, err %d", err); + return err; + } + + ESP_LOGI(TAG, "Open namespace done, name \"%s\"", NVS_NAME); + return err; +} + +esp_err_t ble_mesh_nvs_store(nvs_handle handle, const char *key, const void *data, size_t length) +{ + esp_err_t err = ESP_OK; + + if (key == NULL || data == NULL || length == 0) { + ESP_LOGE(TAG, "Store, invalid parameter"); + return ESP_ERR_INVALID_ARG; + } + + err = nvs_set_blob(handle, key, data, length); + if (err != ESP_OK) { + ESP_LOGE(TAG, "Store, nvs_set_blob failed, err %d", err); + return err; + } + + err = nvs_commit(handle); + if (err != ESP_OK) { + ESP_LOGE(TAG, "Store, nvs_commit failed, err %d", err); + return err; + } + + ESP_LOGI(TAG, "Store, key \"%s\", length %u", key, length); + ESP_LOG_BUFFER_HEX("EXAMPLE_NVS: Store, data", data, length); + return err; +} + +esp_err_t ble_mesh_nvs_get_length(nvs_handle handle, const char *key, size_t *length) +{ + esp_err_t err = ESP_OK; + + if (key == NULL || length == NULL) { + ESP_LOGE(TAG, "Get length, invalid parameter"); + return ESP_ERR_INVALID_ARG; + } + + err = nvs_get_blob(handle, key, NULL, length); + if (err == ESP_ERR_NVS_NOT_FOUND) { + ESP_LOGI(TAG, "Get length, key \"%s\" not exists", key); + *length = 0; + return ESP_OK; + } + + if (err != ESP_OK) { + ESP_LOGE(TAG, "Get length, nvs_get_blob failed, err %d", err); + } else { + ESP_LOGI(TAG, "Get length, key \"%s\", length %u", key, *length); + } + + return err; +} + +esp_err_t ble_mesh_nvs_restore(nvs_handle handle, const char *key, void *data, size_t length, bool *exist) +{ + esp_err_t err = ESP_OK; + + if (key == NULL || data == NULL || length == 0) { + ESP_LOGE(TAG, "Restore, invalid parameter"); + return ESP_ERR_INVALID_ARG; + } + + err = nvs_get_blob(handle, key, data, &length); + if (err == ESP_ERR_NVS_NOT_FOUND) { + ESP_LOGI(TAG, "Restore, key \"%s\" not exists", key); + if (exist) { + *exist = false; + } + return ESP_OK; + } + + if (exist) { + *exist = true; + } + + if (err != ESP_OK) { + ESP_LOGE(TAG, "Restore, nvs_get_blob failed, err %d", err); + } else { + ESP_LOGI(TAG, "Restore, key \"%s\", length %u", key, length); + ESP_LOG_BUFFER_HEX("EXAMPLE_NVS: Restore, data", data, length); + } + + return err; +} + +esp_err_t ble_mesh_nvs_erase(nvs_handle handle, const char *key) +{ + esp_err_t err = ESP_OK; + + if (key) { + err = nvs_erase_key(handle, key); + if (err == ESP_ERR_NVS_NOT_FOUND) { + ESP_LOGI(TAG, "Erase, key \"%s\" not exists", key); + return ESP_OK; + } + } else { + err = nvs_erase_all(handle); + } + if (err != ESP_OK) { + ESP_LOGE(TAG, "Erase, nvs_erase_%s failed, err %d", key ? "key" : "all", err); + return err; + } + + err = nvs_commit(handle); + if (err != ESP_OK) { + ESP_LOGE(TAG, "Erase, nvs_commit failed, err %d", err); + return err; + } + + if (key) { + ESP_LOGI(TAG, "Erase done, key \"%s\"", key); + } else { + ESP_LOGI(TAG, "Erase namespace done, name \"%s\"", NVS_NAME); + } + return err; +} diff --git a/examples/bluetooth/esp_ble_mesh/components/example_nvs/ble_mesh_example_nvs.h b/examples/bluetooth/esp_ble_mesh/components/example_nvs/ble_mesh_example_nvs.h new file mode 100644 index 000000000..6183e5fda --- /dev/null +++ b/examples/bluetooth/esp_ble_mesh/components/example_nvs/ble_mesh_example_nvs.h @@ -0,0 +1,26 @@ +/* + This example code is in the Public Domain (or CC0 licensed, at your option.) + + Unless required by applicable law or agreed to in writing, this + software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + CONDITIONS OF ANY KIND, either express or implied. +*/ + +#ifndef _BLE_MESH_EXAMPLE_NVS_H_ +#define _BLE_MESH_EXAMPLE_NVS_H_ + +#include +#include "esp_err.h" +#include "nvs_flash.h" + +esp_err_t ble_mesh_nvs_open(nvs_handle *handle); + +esp_err_t ble_mesh_nvs_store(nvs_handle handle, const char *key, const void *data, size_t length); + +esp_err_t ble_mesh_nvs_get_length(nvs_handle handle, const char *key, size_t *length); + +esp_err_t ble_mesh_nvs_restore(nvs_handle handle, const char *key, void *data, size_t length, bool *exist); + +esp_err_t ble_mesh_nvs_erase(nvs_handle handle, const char *key); + +#endif /* _BLE_MESH_EXAMPLE_NVS_H_ */ diff --git a/examples/bluetooth/esp_ble_mesh/components/example_nvs/component.mk b/examples/bluetooth/esp_ble_mesh/components/example_nvs/component.mk new file mode 100644 index 000000000..646c890db --- /dev/null +++ b/examples/bluetooth/esp_ble_mesh/components/example_nvs/component.mk @@ -0,0 +1,6 @@ +# +# "main" pseudo-component makefile. +# +# (Uses default behaviour of compiling all source files in directory, adding 'include' to include path.) +# +COMPONENT_ADD_INCLUDEDIRS := . \ No newline at end of file