1 // Copyright 2017-2019 Espressif Systems (Shanghai) PTE LTD
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef _BLE_MESH_SETTINGS_NVS_H_
16 #define _BLE_MESH_SETTINGS_NVS_H_
17 
18 #include "nvs_flash.h"
19 #include "mesh_buf.h"
20 
21 #ifdef __cplusplus
22 extern "C" {
23 #endif
24 
25 typedef nvs_handle_t    bt_mesh_nvs_handle_t;
26 
27 #define SETTINGS_ITEM_SIZE              sizeof(uint16_t)
28 
29 #define BLE_MESH_GET_ELEM_IDX(x)        ((uint8_t)((x) >> 8))
30 #define BLE_MESH_GET_MODEL_IDX(x)       ((uint8_t)(x))
31 #define BLE_MESH_GET_MODEL_KEY(a, b)    ((uint16_t)(((uint16_t)((a) << 8)) | (b)))
32 
33 int bt_mesh_settings_nvs_open(const char* name, bt_mesh_nvs_handle_t *handle);
34 void bt_mesh_settings_nvs_close(bt_mesh_nvs_handle_t handle);
35 
36 void bt_mesh_settings_init_foreach(void);
37 void bt_mesh_settings_deinit_foreach(bool erase);
38 
39 int bt_mesh_settings_direct_open(bt_mesh_nvs_handle_t *handle);
40 void bt_mesh_settings_direct_close(void);
41 
42 int bt_mesh_save_settings(bt_mesh_nvs_handle_t handle, const char *key,
43                           const uint8_t *val, size_t len);
44 int bt_mesh_save_core_settings(const char *key, const uint8_t *val, size_t len);
45 int bt_mesh_save_uid_settings(const char *key, const uint8_t *val, size_t len);
46 
47 int bt_mesh_erase_settings(bt_mesh_nvs_handle_t handle, const char *key);
48 int bt_mesh_erase_core_settings(const char *key);
49 int bt_mesh_erase_uid_settings(const char *name);
50 
51 int bt_mesh_load_settings(bt_mesh_nvs_handle_t handle, const char *key,
52                           uint8_t *buf, size_t buf_len, bool *exist);
53 int bt_mesh_load_core_settings(const char *key, uint8_t *buf, size_t buf_len, bool *exist);
54 int bt_mesh_load_uid_settings(const char *key, uint8_t *buf, size_t buf_len, bool *exist);
55 
56 struct net_buf_simple *bt_mesh_get_settings_item(bt_mesh_nvs_handle_t handle, const char *key);
57 struct net_buf_simple *bt_mesh_get_core_settings_item(const char *key);
58 struct net_buf_simple *bt_mesh_get_uid_settings_item(const char *key);
59 
60 int bt_mesh_add_settings_item(bt_mesh_nvs_handle_t handle, const char *key, const uint16_t val);
61 int bt_mesh_add_core_settings_item(const char *key, const uint16_t val);
62 int bt_mesh_add_uid_settings_item(const char *key, const uint16_t val);
63 
64 int bt_mesh_remove_settings_item(bt_mesh_nvs_handle_t handle, const char *key, const uint16_t val);
65 int bt_mesh_remove_core_settings_item(const char *key, const uint16_t val);
66 int bt_mesh_remove_uid_settings_item(const char *key, const uint16_t val);
67 
68 int bt_mesh_settings_erase_key(bt_mesh_nvs_handle_t handle, const char *key);
69 int bt_mesh_settings_erase_all(bt_mesh_nvs_handle_t handle);
70 
71 #ifdef __cplusplus
72 }
73 #endif
74 
75 #endif /* _BLE_MESH_SETTINGS_NVS_H_ */
76