mesh: Configuration storage re-org
Subsystems (2)
v2 cover mesh: Configuration storage re-org
This set of patches introduces the notion of generic mesh-config API that allows the daemon to be agnostic of the underlying node configuration directory layout and the format of the file(s) in which node configuration is saved.
Currently, the daemon supports only JSON-based configuration format. It is expected that other configuration formats may be added in future.
As a result of these changes, storage.c and storage.h are obsolete and are removed.
Inga Stotland (10):
mesh: Move network config setup from storage.c to node.c
mesh: Rename mesh-db.c to mesh-config-json.c
mesh: Change mesh_db prefix to mesh_config
mesh: Move load from storage functionality into node.c
mesh: Confine dependency on json-c to mesh-config-json.c
mesh: Replace storage_save_config with mesh_config_save_config
mesh: Use mesh_config APIs to store node configuration
mesh: Manage node config directory in mesh-config
mesh: Create or re-use a node storage directory for keyring
mesh: Rename mesh_config_srv_init() to cfgmod_server_init()
Makefile.mesh | 3 +-
mesh/appkey.c | 19 +-
mesh/cfgmod-server.c | 31 +-
mesh/cfgmod.h | 2 +-
mesh/keyring.c | 36 +-
mesh/{mesh-db.c => mesh-config-json.c} | 1071 +++++++++++++++++-------
mesh/mesh-config.h | 172 ++++
mesh/mesh-db.h | 157 ----
mesh/mesh.c | 15 +-
mesh/mesh.h | 1 +
mesh/model.c | 23 +-
mesh/net.c | 26 +-
mesh/node.c | 221 +++--
mesh/node.h | 14 +-
mesh/storage.c | 656 ---------------
mesh/storage.h | 51 --
mesh/util.c | 39 +-
mesh/util.h | 1 +
18 files changed, 1218 insertions(+), 1320 deletions(-)
rename mesh/{mesh-db.c => mesh-config-json.c} (58%)
create mode 100644 mesh/mesh-config.h
delete mode 100644 mesh/mesh-db.h
delete mode 100644 mesh/storage.c
delete mode 100644 mesh/storage.hSignature · 1 line
2.21.0
PATCH v2 1/10 mesh: Move network config setup from storage.c to node.c
This commit moves initialization of configuration parameters for node->net when the local node state is restored from stored configuration. Old location: storage.c, new locaiton node.c.
--- mesh/node.c | 28 ++++++++++++++++++++++++++++ mesh/storage.c | 39 --------------------------------------- 2 files changed, 28 insertions(+), 39 deletions(-)
mesh/node.c +28 -0
@@ -375,6 +375,7 @@ bool node_init_from_storage(struct mesh_node *node, void *data) {struct mesh_db_node *db_node = data;unsigned int num_ele; +uint8_t mode; node->comp = l_new(struct node_composition, 1); node->comp->cid = db_node->cid;
@@ -407,6 +408,33 @@ bool node_init_from_storage(struct mesh_node *node, void *data) node->primary = db_node->unicast; +mesh_net_set_seq_num (node->net, node->seq_number); +mesh_net_set_default_ttl (node->net, node->ttl); + + mode = node->proxy; +if (mode ==MESH_MODE_ENABLED || mode ==MESH_MODE_DISABLED ) +mesh_net_set_proxy_mode (node->net, mode ==MESH_MODE_ENABLED ); + + mode = node->lpn; +if (mode ==MESH_MODE_ENABLED || mode ==MESH_MODE_DISABLED ) +mesh_net_set_friend_mode (node->net, mode ==MESH_MODE_ENABLED ); + + mode = node->relay.mode; +if (mode ==MESH_MODE_ENABLED || mode ==MESH_MODE_DISABLED ) +mesh_net_set_relay_mode (node->net, mode ==MESH_MODE_ENABLED , + node->relay.cnt, node->relay.interval); + + mode = node->beacon; +if (mode ==MESH_MODE_ENABLED || mode ==MESH_MODE_DISABLED ) +mesh_net_set_beacon_mode (node->net, mode ==MESH_MODE_ENABLED ); + +if (!IS_UNASSIGNED (node->primary) && + !mesh_net_register_unicast (node->net, node->primary, num_ele)) +return false; + +if (node->uuid) +mesh_net_id_uuid_set (node->net, node->uuid); +/* Initialize configuration server model */ mesh_config_srv_init (node,PRIMARY_ELE_IDX );
mesh/storage.c +0 -39
@@ -56,51 +56,12 @@ static const char *storage_dir;static bool read_node_cb (struct mesh_db_node *db_node,void *user_data) {struct mesh_node *node = user_data; -struct mesh_net *net; -uint32_t seq_number; -uint8_t ttl, mode, cnt, num_ele; -uint16_t unicast, interval; -uint8_t *uuid;if (!node_init_from_storage (node, db_node)) {node_remove (node);return false; } - net =node_get_net (node); - seq_number =node_get_sequence_number (node); -mesh_net_set_seq_num (net, seq_number); - ttl =node_default_ttl_get (node); -mesh_net_set_default_ttl (net, ttl); - - mode =node_proxy_mode_get (node); -if (mode ==MESH_MODE_ENABLED || mode ==MESH_MODE_DISABLED ) -mesh_net_set_proxy_mode (net, mode ==MESH_MODE_ENABLED ); - - mode =node_friend_mode_get (node); -if (mode ==MESH_MODE_ENABLED || mode ==MESH_MODE_DISABLED ) -mesh_net_set_friend_mode (net, mode ==MESH_MODE_ENABLED ); - - mode =node_relay_mode_get (node, &cnt, &interval); -if (mode ==MESH_MODE_ENABLED || mode ==MESH_MODE_DISABLED ) -mesh_net_set_relay_mode (net, mode ==MESH_MODE_ENABLED , cnt, - interval); - - mode =node_beacon_mode_get (node); -if (mode ==MESH_MODE_ENABLED || mode ==MESH_MODE_DISABLED ) -mesh_net_set_beacon_mode (net, mode ==MESH_MODE_ENABLED ); - - unicast = db_node->unicast; - num_ele =node_get_num_elements (node); - -if (!IS_UNASSIGNED (unicast) && - !mesh_net_register_unicast (net, unicast, num_ele)) -return false; - - uuid =node_uuid_get (node); -if (uuid) -mesh_net_id_uuid_set (net, uuid); -return true; }
Signature · 1 line
2.21.0
PATCH v2 2/10 mesh: Rename mesh-db.c to mesh-config-json.c
In reply to Inga StotlandThis moves mesh-db.h to mesh-config.h and mesh-db.c to mesh-config-json.c. mesh-config.h declares common APIs for storing mesh node configuration. mesh-config-json.c defines the APIs for JSOn specific storage. This allows for future parallel implementation a different (not JSON-based) mechanism of storing node configuration.
---
Makefile.mesh | 2 +-
mesh/cfgmod-server.c | 3 +--
mesh/{mesh-db.c => mesh-config-json.c} | 3 +--
mesh/{mesh-db.h => mesh-config.h} | 0
mesh/model.c | 2 +-
mesh/node.c | 2 +-
mesh/storage.c | 3 +--
7 files changed, 6 insertions(+), 9 deletions(-)
rename mesh/{mesh-db.c => mesh-config-json.c} (99%)
rename mesh/{mesh-db.h => mesh-config.h} (100%)Makefile.mesh +1 -1
@@ -25,7 +25,7 @@ mesh_sources = mesh/mesh.h mesh/mesh.c \ mesh/provision.h mesh/prov.h \ mesh/model.h mesh/model.c \ mesh/cfgmod.h mesh/cfgmod-server.c \ - mesh/mesh-db.h mesh/mesh-db.c \ + mesh/mesh-config.h mesh/mesh-config-json.c \ mesh/util.h mesh/util.c \ mesh/dbus.h mesh/dbus.c \ mesh/agent.h mesh/agent.c \
mesh/cfgmod-server.c +1 -2
@@ -32,8 +32,7 @@#include "mesh/appkey.h" #include "mesh/model.h" #include "mesh/storage.h" -#include "mesh/mesh-db.h" - +#include "mesh/mesh-config.h" #include "mesh/cfgmod.h" #define CFG_MAX_MSG_LEN 380
mesh/mesh-config-json.c renamed from mesh/mesh-db.c +1 -2
@@ -31,8 +31,7 @@#include "mesh/mesh-defs.h" #include "mesh/util.h" - -#include "mesh/mesh-db.h" +#include "mesh/mesh-config.h" #define CHECK_KEY_IDX_RANGE (x) (((x) >= 0) && ((x) <= 4095))
mesh/mesh-config.h renamed from mesh/mesh-db.h +0 -0
mesh/model.c +1 -1
@@ -30,7 +30,7 @@#include "mesh/mesh.h" #include "mesh/crypto.h" #include "mesh/node.h" -#include "mesh/mesh-db.h" +#include "mesh/mesh-config.h" #include "mesh/net.h" #include "mesh/appkey.h" #include "mesh/cfgmod.h"
mesh/node.c +1 -1
@@ -31,7 +31,7 @@#include "mesh/mesh-defs.h" #include "mesh/mesh.h" #include "mesh/net.h" -#include "mesh/mesh-db.h" +#include "mesh/mesh-config.h" #include "mesh/provision.h" #include "mesh/storage.h" #include "mesh/keyring.h"
mesh/storage.c +1 -2
@@ -22,7 +22,6 @@#endif #define _GNU_SOURCE -//#include <errno.h> #include <fcntl.h> #include <stdio.h> #include <unistd.h>
@@ -37,7 +36,7 @@#include "mesh/node.h" #include "mesh/net.h" #include "mesh/appkey.h" -#include "mesh/mesh-db.h" +#include "mesh/mesh-config.h" #include "mesh/util.h" #include "mesh/storage.h"
Signature · 1 line
2.21.0
PATCH v2 3/10 mesh: Change mesh_db prefix to mesh_config
In reply to Inga StotlandThis changes the naming the API declarations in mesh-config.h:mesh_db_... ->mesh_config_...
--- mesh/cfgmod-server.c | 20 ++-- mesh/mesh-config-json.c | 213 +++++++++++++++++++++------------------- mesh/mesh-config.h | 122 ++++++++++++----------- mesh/model.c | 4 +- mesh/node.c | 36 +++---- mesh/storage.c | 54 +++++----- 6 files changed, 234 insertions(+), 215 deletions(-)
mesh/cfgmod-server.c +10 -10
@@ -197,14 +197,14 @@ static bool config_pub_set(struct mesh_node *node, uint16_t src, uint16_t dst,/* Remove model publication from config file */ if (status ==MESH_STATUS_SUCCESS ) -mesh_db_model_pub_del (node_jconfig_get (node), ele_addr, - vendor ? mod_id : mod_id &0x0000ffff , - vendor); +mesh_config_model_pub_del (node_jconfig_get (node), + ele_addr, vendor ? mod_id : mod_id &0x0000ffff , + vendor); goto done; }if (status ==MESH_STATUS_SUCCESS ) { -struct mesh_db_pub db_pub = { +struct mesh_config_pub db_pub = { .virt = b_virt, .addr = ota, .idx = idx,
@@ -219,7 +219,7 @@ static bool config_pub_set(struct mesh_node *node, uint16_t src, uint16_t dst,memcpy (db_pub.virt_addr, pub_addr,16 );/* Save model publication to config file */ -if (!mesh_db_model_pub_add (node_jconfig_get (node), ele_addr, +if (!mesh_config_model_pub_add (node_jconfig_get (node), ele_addr, vendor ? mod_id : mod_id &0x0000ffff , vendor, &db_pub)) status =MESH_STATUS_STORAGE_FAIL ;
@@ -321,7 +321,7 @@ static bool save_config_sub(struct mesh_node *node, uint16_t ele_addr, const uint8_t *addr, bool virt, uint16_t grp, uint32_t opcode) { - struct mesh_db_sub db_sub = { + struct mesh_config_sub db_sub = { .virt = virt, .src.addr = grp };
@@ -331,18 +331,18 @@ static bool save_config_sub(struct mesh_node *node, uint16_t ele_addr,if (opcode ==OP_CONFIG_MODEL_SUB_VIRT_OVERWRITE || opcode ==OP_CONFIG_MODEL_SUB_OVERWRITE ) -mesh_db_model_sub_del_all (node_jconfig_get (node), +mesh_config_model_sub_del_all (node_jconfig_get (node), ele_addr, vendor ? mod_id : mod_id &0x0000ffff , vendor);if (opcode !=OP_CONFIG_MODEL_SUB_VIRT_DELETE && opcode !=OP_CONFIG_MODEL_SUB_DELETE ) -return mesh_db_model_sub_add (node_jconfig_get (node), +return mesh_config_model_sub_add (node_jconfig_get (node), ele_addr, vendor ? mod_id : mod_id &0x0000ffff , vendor, &db_sub);else -return mesh_db_model_sub_del (node_jconfig_get (node), +return mesh_config_model_sub_del (node_jconfig_get (node), ele_addr, vendor ? mod_id : mod_id &0x0000ffff , vendor, &db_sub);
@@ -419,7 +419,7 @@ static void config_sub_set(struct mesh_node *node, uint16_t src, uint16_t dst, status =mesh_model_sub_del_all (node, ele_addr, mod_id);if (status ==MESH_STATUS_SUCCESS ) -mesh_db_model_sub_del_all (node_jconfig_get (node), +mesh_config_model_sub_del_all (node_jconfig_get (node), ele_addr, vendor ? mod_id : mod_id &0x0000ffff , vendor);break ;
mesh/mesh-config-json.c +111 -102
637 lines not on this page · raw message
mesh/mesh-config.h +65 -57
183 lines not on this page · raw message
mesh/model.c +2 -2
@@ -1406,10 +1406,10 @@ int mesh_model_sub_del_all(struct mesh_node *node, uint16_t addr, uint32_t id)struct mesh_model *mesh_model_setup (struct mesh_node *node,uint8_t ele_idx,void *data) { -struct mesh_db_model *db_mod = data; +struct mesh_config_model *db_mod = data;struct mesh_model *mod;struct mesh_net *net; -struct mesh_db_pub *pub = db_mod->pub; +struct mesh_config_pub *pub = db_mod->pub;uint32_t i;if (db_mod->num_bindings >MAX_BINDINGS ) {
mesh/node.c +19 -17
@@ -289,7 +289,7 @@ void node_remove(struct mesh_node *node) }static bool add_models (struct mesh_node *node,struct node_element *ele, -struct mesh_db_element *db_ele) +struct mesh_config_element *db_ele) {const struct l_queue_entry *entry;
@@ -299,7 +299,7 @@ static bool add_models(struct mesh_node *node, struct node_element *ele, entry =l_queue_get_entries (db_ele->models);for (; entry; entry = entry->next) {struct mesh_model *mod; -struct mesh_db_model *db_mod; +struct mesh_config_model *db_mod; db_mod = entry->data; mod =mesh_model_setup (node, ele->idx, db_mod);
@@ -317,7 +317,7 @@ static void add_internal_model(struct mesh_node *node, uint32_t mod_id, {struct node_element *ele;struct mesh_model *mod; -struct mesh_db_model db_mod; +struct mesh_config_model db_mod; ele =l_queue_find (node->elements, match_element_idx,L_UINT_TO_PTR (ele_idx));
@@ -338,7 +338,8 @@ static void add_internal_model(struct mesh_node *node, uint32_t mod_id,l_queue_push_tail (ele->models, mod); } -static bool add_element (struct mesh_node *node,struct mesh_db_element *db_ele) +static bool add_element (struct mesh_node *node, +struct mesh_config_element *db_ele) {struct node_element *ele;
@@ -356,7 +357,8 @@ static bool add_element(struct mesh_node *node, struct mesh_db_element *db_ele)return true; } -static bool add_elements (struct mesh_node *node,struct mesh_db_node *db_node) +static bool add_elements (struct mesh_node *node, +struct mesh_config_node *db_node) {const struct l_queue_entry *entry;
@@ -373,7 +375,7 @@ static bool add_elements(struct mesh_node *node, struct mesh_db_node *db_node)bool node_init_from_storage (struct mesh_node *node,void *data) { -struct mesh_db_node *db_node = data; +struct mesh_config_node *db_node = data;unsigned int num_ele;uint8_t mode;
@@ -1260,7 +1262,7 @@ static bool get_element_properties(struct mesh_node *node, const char *path, }static void convert_node_to_storage (struct mesh_node *node, -struct mesh_db_node *db_node) +struct mesh_config_node *db_node) {const struct l_queue_entry *entry;
@@ -1286,10 +1288,10 @@ static void convert_node_to_storage(struct mesh_node *node,for (; entry; entry = entry->next) {struct node_element *ele = entry->data; -struct mesh_db_element *db_ele; +struct mesh_config_element *db_ele;const struct l_queue_entry *mod_entry; - db_ele = l_new(struct mesh_db_element, 1); + db_ele = l_new(struct mesh_config_element, 1); db_ele->index = ele->idx; db_ele->location = ele->location;
@@ -1299,10 +1301,10 @@ static void convert_node_to_storage(struct mesh_node *node,for (; mod_entry; mod_entry = mod_entry->next) {struct mesh_model *mod = mod_entry->data; -struct mesh_db_model *db_mod; +struct mesh_config_model *db_mod;uint32_t mod_id =mesh_model_get_model_id (mod); - db_mod = l_new(struct mesh_db_model, 1); + db_mod = l_new(struct mesh_config_model, 1); db_mod->id = mod_id; db_mod->vendor = ((mod_id &VENDOR_ID_MASK ) !=VENDOR_ID_MASK );
@@ -1316,7 +1318,7 @@ static void convert_node_to_storage(struct mesh_node *node,static bool create_node_config (struct mesh_node *node) { -struct mesh_db_node db_node; +struct mesh_config_node db_node;const struct l_queue_entry *entry;bool res;
@@ -1326,7 +1328,7 @@ static bool create_node_config(struct mesh_node *node)/* Free temporarily allocated resources */ entry =l_queue_get_entries (db_node.elements);for (; entry; entry = entry->next) { -struct mesh_db_element *db_ele = entry->data; +struct mesh_config_element *db_ele = entry->data;l_queue_destroy (db_ele->models, l_free); }
@@ -1429,16 +1431,16 @@ static bool add_local_node(struct mesh_node *node, uint16_t unicast, bool kr,mesh_net_set_iv_index (node->net, iv_idx, ivu); -if (!mesh_db_write_uint16_hex (node->jconfig,"unicastAddress" , +if (!mesh_config_write_uint16_hex (node->jconfig,"unicastAddress" , unicast))return false;l_getrandom (node->token,sizeof (node->token)); -if (!mesh_db_write_token (node->jconfig, node->token)) +if (!mesh_config_write_token (node->jconfig, node->token))return false;memcpy (node->dev_key, dev_key,16 ); -if (!mesh_db_write_device_key (node->jconfig, dev_key)) +if (!mesh_config_write_device_key (node->jconfig, dev_key))return false; node->primary = unicast;
@@ -1454,7 +1456,7 @@ static bool add_local_node(struct mesh_node *node, uint16_t unicast, bool kr, MESH_STATUS_SUCCESS)return false; -if (!mesh_db_net_key_set_phase (node->jconfig, net_key_idx, +if (!mesh_config_net_key_set_phase (node->jconfig, net_key_idx,KEY_REFRESH_PHASE_TWO ))return false; }
mesh/storage.c +27 -27
170 lines not on this page · raw message
Signature · 1 line
2.21.0
PATCH v2 4/10 mesh: Move load from storage functionality into node.c
In reply to Inga StotlandThis moves the initialization of a mesh node from stored configuration from storage.c to node.c
--- mesh/mesh-config-json.c | 255 +++++++++++++++++++++++----------------- mesh/mesh-config.h | 59 +++++----- mesh/node.c | 40 ++++++- mesh/storage.c | 61 +--------- 4 files changed, 215 insertions(+), 200 deletions(-)
mesh/mesh-config-json.c +149 -106
@@ -252,7 +252,7 @@ static json_object *jarray_key_del(json_object *jarray, int16_t idx)return jarray_new; } -bool mesh_config_read_iv_index (json_object *jobj,uint32_t *idx,bool *update) +static bool read_iv_index (json_object *jobj,uint32_t *idx,bool *update) {int tmp;
@@ -270,7 +270,7 @@ bool mesh_config_read_iv_index(json_object *jobj, uint32_t *idx, bool *update)return true; } -bool mesh_config_read_token (json_object *jobj,uint8_t token[8 ]) +static bool read_token (json_object *jobj,uint8_t token[8 ]) {json_object *jvalue;char *str;
@@ -288,7 +288,7 @@ bool mesh_config_read_token(json_object *jobj, uint8_t token[8])return true; } -bool mesh_config_read_device_key (json_object *jobj,uint8_t key_buf[16 ]) +static bool read_device_key (json_object *jobj,uint8_t key_buf[16 ]) {json_object *jvalue;char *str;
@@ -306,78 +306,86 @@ bool mesh_config_read_device_key(json_object *jobj, uint8_t key_buf[16])return true; } -bool mesh_config_read_app_keys (json_object *jobj,mesh_config_app_key_cb cb, -void *user_data) +static bool get_key_index (json_object *jobj,const char *keyword, +uint16_t *index) +{ +int idx; + +if (!get_int (jobj, keyword, &idx)) +return false; + +if (!CHECK_KEY_IDX_RANGE (idx)) +return false; + + *index = (uint16_t ) idx; +return true; +} + +static bool read_app_keys (json_object *jobj,struct mesh_config_node *node) {json_object *jarray;int len;int i; -if (!cb) -return true; -if (!json_object_object_get_ex (jobj,"appKeys" , &jarray)) -return false; +return true;if (json_object_get_type (jarray) != json_type_array)return false; +/* Allow empty AppKey array */ len =json_object_array_length (jarray); +if (!len) +return true; + + node->appkeys =l_queue_new ();for (i =0 ; i < len; ++i) {json_object *jtemp, *jvalue; -int app_idx, net_idx; -bool key_refresh = false;char *str; -uint8_t key[16 ]; -uint8_t new_key[16 ]; +struct mesh_config_appkey *appkey; + + appkey = l_new(struct mesh_config_appkey, 1); jtemp =json_object_array_get_idx (jarray, i); -if (!get_int (jtemp,"index" , &app_idx)) -return false; +if (!get_key_index (jtemp,"index" , &appkey->app_idx)) + goto fail; -if (!CHECK_KEY_IDX_RANGE (app_idx)) -return false; +if (!get_key_index (jtemp,"boundNetKey" , &appkey->net_idx)) + goto fail; -if (!get_int (jtemp,"boundNetKey" , &net_idx)) -return false; +if (!json_object_object_get_ex (jtemp,"key" , &jvalue)) + goto fail; -if (!CHECK_KEY_IDX_RANGE (net_idx)) -return false; + str = (char *)json_object_get_string (jvalue); +if (!str2hex (str,strlen (str), appkey->new_key,16 )) + goto fail; -if (json_object_object_get_ex (jtemp,"oldKey" , &jvalue)) { +if (json_object_object_get_ex (jtemp,"oldKey" , &jvalue)) str = (char *)json_object_get_string (jvalue); -if (!str2hex (str,strlen (str), key,16 )) -return false; - key_refresh = true; - } - -if (!json_object_object_get_ex (jtemp,"key" , &jvalue)) -return false; - str = (char *)json_object_get_string (jvalue); -if (!str2hex (str,strlen (str), key_refresh ? new_key : key,16 )) -return false; +if (!str2hex (str,strlen (str), appkey->key,16 )) + goto fail; -if (!cb ((uint16_t )net_idx, (uint16_t ) app_idx, key, - key_refresh ? new_key :NULL , user_data)) -return false; +l_queue_push_tail (node->appkeys, appkey); }return true; +fail: +l_queue_destroy (node->appkeys, l_free); + node->appkeys =NULL ; + +return false; } -bool mesh_config_read_net_keys (json_object *jobj,mesh_config_net_key_cb cb, -void *user_data) +static bool read_net_keys (json_object *jobj,struct mesh_config_node *node) {json_object *jarray;int len;int i; -if (!cb) -return true; - +/* At least one NetKey must be present for a provisioned node */ if (!json_object_object_get_ex (jobj,"netKeys" , &jarray))return false;
@@ -385,50 +393,57 @@ bool mesh_config_read_net_keys(json_object *jobj, mesh_config_net_key_cb cb,return false; len =json_object_array_length (jarray); +if (!len) +return false; + + node->netkeys =l_queue_new ();for (i =0 ; i < len; ++i) {json_object *jtemp, *jvalue; -int idx;char *str; -bool key_refresh = false; -int phase; -uint8_t key[16 ]; -uint8_t new_key[16 ]; +struct mesh_config_netkey *netkey; - jtemp =json_object_array_get_idx (jarray, i); - -if (!get_int (jtemp,"index" , &idx)) -return false; + netkey = l_new(struct mesh_config_netkey, 1); -if (!CHECK_KEY_IDX_RANGE (idx)) -return false; + jtemp =json_object_array_get_idx (jarray, i); -if (json_object_object_get_ex (jtemp,"oldKey" , &jvalue)) { - str = (char *)json_object_get_string (jvalue); -if (!str2hex (str,strlen (str), key,16 )) -return false; - key_refresh = true; - } +if (!get_key_index (jtemp,"index" , &netkey->idx)) + goto fail;if (!json_object_object_get_ex (jtemp,"key" , &jvalue)) -return false; + goto fail; str = (char *)json_object_get_string (jvalue); -if (!str2hex (str,strlen (str), key_refresh ? new_key : key,16 )) -return false; +if (!str2hex (str,strlen (str), netkey->new_key,16 )) + goto fail;if (!json_object_object_get_ex (jtemp,"keyRefresh" , &jvalue)) - phase =KEY_REFRESH_PHASE_NONE ; + netkey->phase =KEY_REFRESH_PHASE_NONE ;else - phase =json_object_get_int (jvalue); + netkey->phase = (uint8_t )json_object_get_int (jvalue); +if (netkey->phase >KEY_REFRESH_PHASE_TWO ) + goto fail; -if (!cb ((uint16_t )idx, key, key_refresh ? new_key :NULL , phase, - user_data)) -return false; +if (json_object_object_get_ex (jtemp,"oldKey" , &jvalue)) { +if (netkey->phase ==KEY_REFRESH_PHASE_NONE ) + goto fail; + + str = (char *)json_object_get_string (jvalue); + } + +if (!str2hex (str,strlen (str), netkey->key,16 )) + goto fail; + +l_queue_push_tail (node->netkeys, netkey); }return true; +fail: +l_queue_destroy (node->netkeys, l_free); + node->netkeys =NULL ; + +return false; } bool mesh_config_net_key_add(json_object *jobj, uint16_t idx,
@@ -787,21 +802,6 @@ static bool parse_bindings(json_object *jarray, struct mesh_config_model *mod)return true; } -static bool get_key_index (json_object *jobj,const char *keyword, -uint16_t *index) -{ -int idx; - -if (!get_int (jobj, keyword, &idx)) -return false; - -if (!CHECK_KEY_IDX_RANGE (idx)) -return false; - - *index = (uint16_t ) idx; -return true; -} -static struct mesh_config_pub *parse_model_publication (json_object *jpub) {json_object *jvalue;
@@ -1162,12 +1162,40 @@ static bool parse_composition(json_object *jcomp, struct mesh_config_node *node)return true; } +static bool read_net_transmit (json_object *jobj,struct mesh_config_node *node) +{ +json_object *jretransmit, *jvalue; +uint16_t interval; +uint8_t cnt; + +if (!json_object_object_get_ex (jobj,"retransmit" , &jretransmit)) +return true; + +if (!json_object_object_get_ex (jretransmit,"count" , &jvalue)) +return false; + +/* TODO: add range checking */ + cnt = (uint8_t )json_object_get_int (jvalue); + +if (!json_object_object_get_ex (jretransmit,"interval" , &jvalue)) +return false; + + interval = (uint16_t )json_object_get_int (jvalue); + + node->net_transmit = l_new(struct mesh_config_transmit, 1); + node->net_transmit->count = cnt; + node->net_transmit->interval = interval; + +return true; +} +bool mesh_config_read_node (json_object *jnode,mesh_config_node_cb cb,void *user_data) {struct mesh_config_node node;json_object *jvalue;char *str; +bool result = false;if (!jnode)return false;
@@ -1179,6 +1207,21 @@ bool mesh_config_read_node(json_object *jnode, mesh_config_node_cb cb,memset (&node,0 ,sizeof (node)); +if (!read_iv_index (jnode, &node.iv_index, &node.iv_update)) { +l_info ("Failed to read IV index" ); +return false; + } + +if (!read_token (jnode, node.token)) { +l_info ("Failed to read node token" ); +return false; + } + +if (!read_device_key (jnode, node.dev_key)) { +l_info ("Failed to read node device key" ); +return false; + } +if (!parse_composition (jnode, &node)) {l_info ("Failed to parse local node composition" );return false;
@@ -1210,10 +1253,34 @@ bool mesh_config_read_node(json_object *jnode, mesh_config_node_cb cb,if (!json_object_object_get_ex (jnode,"elements" , &jvalue))return false; -if (!parse_elements (jvalue, &node)) +if (!read_net_transmit (jnode, &node)) { +l_info ("Failed to read node net transmit parameters" );return false; + } -return cb (&node, user_data); +if (!read_net_keys (jnode, &node)) { +l_info ("Failed to read net keys" ); + goto done; + } + +if (!read_app_keys (jnode, &node)) { +l_info ("Failed to read app keys" ); + goto done; + } + +if (!parse_elements (jvalue, &node)) { +l_info ("Failed to parse elements" ); + goto done; + } + + result =cb (&node, user_data); + +done: +l_free (node.net_transmit); +l_queue_destroy (node.netkeys, l_free); +l_queue_destroy (node.netkeys, l_free); + +return result; } bool mesh_config_write_uint16_hex(json_object *jobj, const char *desc,
@@ -1346,30 +1413,6 @@ fail:return false; } -bool mesh_config_read_net_transmit (json_object *jobj,uint8_t *cnt, -uint16_t *interval) -{ -json_object *jretransmit, *jvalue; - -if (!jobj) -return false; - -if (!json_object_object_get_ex (jobj,"retransmit" , &jretransmit)) -return false; - -if (!json_object_object_get_ex (jretransmit,"count" , &jvalue)) -return false; - - *cnt = (uint8_t )json_object_get_int (jvalue); - -if (!json_object_object_get_ex (jretransmit,"interval" , &jvalue)) -return false; - - *interval = (uint16_t )json_object_get_int (jvalue); - -return true; -} -bool mesh_config_write_net_transmit (json_object *jobj,uint8_t cnt,uint16_t interval) {
mesh/mesh-config.h +28 -31
83 lines not on this page · raw message
mesh/node.c +37 -3
@@ -31,6 +31,7 @@#include "mesh/mesh-defs.h" #include "mesh/mesh.h" #include "mesh/net.h" +#include "mesh/appkey.h" #include "mesh/mesh-config.h" #include "mesh/provision.h" #include "mesh/storage.h"
@@ -373,6 +374,24 @@ static bool add_elements(struct mesh_node *node,return true; } +static void set_net_key (void *a,void *b) +{ +struct mesh_config_netkey *netkey = a; +struct mesh_node *node = b; + +mesh_net_set_key (node->net, netkey->idx, netkey->key, netkey->new_key, + netkey->phase); +} + +static void set_app_key (void *a,void *b) +{ +struct mesh_config_appkey *appkey = a; +struct mesh_node *node = b; + +appkey_key_init (node->net, appkey->net_idx, appkey->app_idx, + appkey->key, appkey->new_key); +} +bool node_init_from_storage (struct mesh_node *node,void *data) {struct mesh_config_node *db_node = data;
@@ -399,6 +418,9 @@ bool node_init_from_storage(struct mesh_node *node, void *data) node->ttl = db_node->ttl; node->seq_number = db_node->seq_number; +memcpy (node->dev_key, db_node->dev_key,16 ); +memcpy (node->token, db_node->token,8 ); + num_ele =l_queue_length (db_node->elements);if (num_ele >0xff )return false;
@@ -410,6 +432,21 @@ bool node_init_from_storage(struct mesh_node *node, void *data) node->primary = db_node->unicast; +if (!db_node->netkeys) +return false; + +mesh_net_set_iv_index (node->net, db_node->iv_index, db_node->iv_update); + +if (db_node->net_transmit) +mesh_net_transmit_params_set (node->net, + db_node->net_transmit->count, + db_node->net_transmit->interval); + +l_queue_foreach (db_node->netkeys, set_net_key, node); + +if (db_node->appkeys) +l_queue_foreach (db_node->appkeys, set_app_key, node); +mesh_net_set_seq_num (node->net, node->seq_number);mesh_net_set_default_ttl (node->net, node->ttl);
@@ -434,9 +471,6 @@ bool node_init_from_storage(struct mesh_node *node, void *data) !mesh_net_register_unicast(node->net, node->primary, num_ele))return false; -if (node->uuid) -mesh_net_id_uuid_set (node->net, node->uuid); -/* Initialize configuration server model */ mesh_config_srv_init (node,PRIMARY_ELE_IDX );
mesh/storage.c +1 -60
73 lines not on this page · raw message
Signature · 1 line
2.21.0
PATCH v2 5/10 mesh: Confine dependency on json-c to mesh-config-json.c
In reply to Inga StotlandThis removes dependency on json-s/json.h header from everywhere in the code except for mesh-config-json.c. The details about node configuration storage format are obfuscated: a pointer to an opaque mesh_config structure is stored with mesh_node and is passed around to mae all the storage related operations.
--- mesh/cfgmod-server.c | 17 +- mesh/mesh-config-json.c | 558 +++++++++++++++++++++++++++------------- mesh/mesh-config.h | 77 +++--- mesh/model.c | 1 - mesh/node.c | 37 ++- mesh/node.h | 9 +- mesh/storage.c | 205 +++++---------- mesh/storage.h | 4 +- 8 files changed, 524 insertions(+), 384 deletions(-)
mesh/cfgmod-server.c +7 -10
@@ -24,8 +24,6 @@#include <sys/time.h> #include <ell/ell.h> -#include "json-c/json.h" -#include "mesh/mesh-defs.h" #include "mesh/node.h" #include "mesh/net.h"
@@ -197,7 +195,7 @@ static bool config_pub_set(struct mesh_node *node, uint16_t src, uint16_t dst,/* Remove model publication from config file */ if (status ==MESH_STATUS_SUCCESS ) -mesh_config_model_pub_del (node_jconfig_get (node), +mesh_config_model_pub_del (node_config_get (node), ele_addr, vendor ? mod_id : mod_id &0x0000ffff , vendor); goto done;
@@ -219,7 +217,7 @@ static bool config_pub_set(struct mesh_node *node, uint16_t src, uint16_t dst,memcpy (db_pub.virt_addr, pub_addr,16 );/* Save model publication to config file */ -if (!mesh_config_model_pub_add (node_jconfig_get (node), ele_addr, +if (!mesh_config_model_pub_add (node_config_get (node), ele_addr, vendor ? mod_id : mod_id &0x0000ffff , vendor, &db_pub)) status =MESH_STATUS_STORAGE_FAIL ;
@@ -331,18 +329,18 @@ static bool save_config_sub(struct mesh_node *node, uint16_t ele_addr,if (opcode ==OP_CONFIG_MODEL_SUB_VIRT_OVERWRITE || opcode ==OP_CONFIG_MODEL_SUB_OVERWRITE ) -mesh_config_model_sub_del_all (node_jconfig_get (node), +mesh_config_model_sub_del_all (node_config_get (node), ele_addr, vendor ? mod_id : mod_id &0x0000ffff , vendor);if (opcode !=OP_CONFIG_MODEL_SUB_VIRT_DELETE && opcode !=OP_CONFIG_MODEL_SUB_DELETE ) -return mesh_config_model_sub_add (node_jconfig_get (node), +return mesh_config_model_sub_add (node_config_get (node), ele_addr, vendor ? mod_id : mod_id &0x0000ffff , vendor, &db_sub);else -return mesh_config_model_sub_del (node_jconfig_get (node), +return mesh_config_model_sub_del (node_config_get (node), ele_addr, vendor ? mod_id : mod_id &0x0000ffff , vendor, &db_sub);
@@ -419,7 +417,7 @@ static void config_sub_set(struct mesh_node *node, uint16_t src, uint16_t dst, status =mesh_model_sub_del_all (node, ele_addr, mod_id);if (status ==MESH_STATUS_SUCCESS ) -mesh_config_model_sub_del_all (node_jconfig_get (node), +mesh_config_model_sub_del_all (node_config_get (node), ele_addr, vendor ? mod_id : mod_id &0x0000ffff , vendor);break ;
@@ -878,8 +876,7 @@ static bool cfg_srv_pkt(uint16_t src, uint32_t dst, count = (pkt[0 ] >>5 ) +1 ; interval = ((pkt[0 ] &0x1f ) +1 ) *10 ; -if (storage_set_transmit_params (node_jconfig_get (node), count, - interval)) +if (storage_set_transmit_params (node, count, interval))mesh_net_transmit_params_set (net, count, interval);/* Fall Through */
mesh/mesh-config-json.c +381 -177
1,010 lines not on this page · raw message
mesh/mesh-config.h +45 -32
114 lines not on this page · raw message
mesh/model.c +0 -1
@@ -23,7 +23,6 @@#include <sys/time.h> #include <ell/ell.h> -#include <json-c/json.h> #include "mesh/mesh-defs.h"
mesh/node.c +18 -19
@@ -26,7 +26,6 @@#include <sys/time.h> #include <ell/ell.h> -#include <json-c/json.h> #include "mesh/mesh-defs.h" #include "mesh/mesh.h"
@@ -82,7 +81,7 @@ struct mesh_node {char *app_path;char *owner;char *path; -void *jconfig; +struct mesh_config *cfg;char *node_path;uint32_t disc_watch;time_t upd_sec;
@@ -392,9 +391,9 @@ static void set_app_key(void *a, void *b) appkey->key, appkey->new_key); } -bool node_init_from_storage (struct mesh_node *node,void *data) +bool node_init_from_storage (struct mesh_node *node,const uint8_t uuid[16 ], +struct mesh_config_node *db_node) { -struct mesh_config_node *db_node = data;unsigned int num_ele;uint8_t mode;
@@ -753,7 +752,7 @@ bool node_proxy_mode_set(struct mesh_node *node, bool enable)return false; proxy = enable ?MESH_MODE_ENABLED :MESH_MODE_DISABLED ; - res =storage_set_mode (node->jconfig, proxy,"proxy" ); + res =storage_set_mode (node, proxy,"proxy" );if (res) { node->proxy = proxy;
@@ -780,7 +779,7 @@ bool node_beacon_mode_set(struct mesh_node *node, bool enable)return false; beacon = enable ?MESH_MODE_ENABLED :MESH_MODE_DISABLED ; - res =storage_set_mode (node->jconfig, beacon,"beacon" ); + res =storage_set_mode (node, beacon,"beacon" );if (res) { node->beacon = beacon;
@@ -807,7 +806,7 @@ bool node_friend_mode_set(struct mesh_node *node, bool enable)return false; friend = enable ?MESH_MODE_ENABLED :MESH_MODE_DISABLED ; - res =storage_set_mode (node->jconfig, friend,"friend" ); + res =storage_set_mode (node, friend,"friend" );if (res) { node->friend = friend;
@@ -1350,14 +1349,14 @@ static void convert_node_to_storage(struct mesh_node *node, } -static bool create_node_config (struct mesh_node *node) +static bool create_node_config (struct mesh_node *node,const uint8_t uuid[16 ]) {struct mesh_config_node db_node;const struct l_queue_entry *entry;bool res;convert_node_to_storage (node, &db_node); - res =storage_create_node_config (node, &db_node); + res =storage_create_node_config (node, uuid, &db_node);/* Free temporarily allocated resources */ entry =l_queue_get_entries (db_node.elements);
@@ -1465,16 +1464,16 @@ static bool add_local_node(struct mesh_node *node, uint16_t unicast, bool kr,mesh_net_set_iv_index (node->net, iv_idx, ivu); -if (!mesh_config_write_uint16_hex (node->jconfig,"unicastAddress" , +if (!mesh_config_write_uint16_hex (node->cfg,"unicastAddress" , unicast))return false;l_getrandom (node->token,sizeof (node->token)); -if (!mesh_config_write_token (node->jconfig, node->token)) +if (!mesh_config_write_token (node->cfg, node->token))return false;memcpy (node->dev_key, dev_key,16 ); -if (!mesh_config_write_device_key (node->jconfig, dev_key)) +if (!mesh_config_write_device_key (node->cfg, dev_key))return false; node->primary = unicast;
@@ -1490,7 +1489,7 @@ static bool add_local_node(struct mesh_node *node, uint16_t unicast, bool kr, MESH_STATUS_SUCCESS)return false; -if (!mesh_config_net_key_set_phase (node->jconfig, net_key_idx, +if (!mesh_config_net_key_set_phase (node->cfg, net_key_idx,KEY_REFRESH_PHASE_TWO ))return false; }
@@ -1625,7 +1624,7 @@ static void get_managed_objects_cb(struct l_dbus_message *msg, void *user_data)set_defaults (node);memcpy (node->uuid, req->data,16 ); -if (!create_node_config (node)) +if (!create_node_config (node, node->uuid)) goto fail;cb (node, agent);
@@ -1640,7 +1639,7 @@ static void get_managed_objects_cb(struct l_dbus_message *msg, void *user_data)set_defaults (node);memcpy (node->uuid, req->data,16 ); -if (!create_node_config (node)) +if (!create_node_config (node, node->uuid)) goto fail;/* Generate device and primary network keys */
@@ -2050,14 +2049,14 @@ bool node_add_pending_local(struct mesh_node *node, void *prov_node_info) info->device_key, info->net_index, info->net_key); } -void node_jconfig_set (struct mesh_node *node,void *jconfig) +void node_config_set (struct mesh_node *node,struct mesh_config *cfg) { - node->jconfig = jconfig; + node->cfg = cfg; } -void *node_jconfig_get (struct mesh_node *node) +struct mesh_config *node_config_get (struct mesh_node *node) { -return node->jconfig; +return node->cfg; }void node_path_set (struct mesh_node *node,char *path)
mesh/node.h +6 -3
@@ -21,6 +21,8 @@ struct mesh_net;struct mesh_node ;struct mesh_io ;struct mesh_agent ; +struct mesh_config ; +struct mesh_config_node ;/* To prevent local node JSON cache thrashing, minimum update times */ #define MIN_SEQ_TRIGGER 32
@@ -66,7 +68,8 @@ int node_get_element_idx(struct mesh_node *node, uint16_t ele_addr);struct l_queue *node_get_element_models (struct mesh_node *node,uint8_t ele_idx,int *status);uint16_t node_get_crpl (struct mesh_node *node); -bool node_init_from_storage (struct mesh_node *node,void *data); +bool node_init_from_storage (struct mesh_node *node,const uint8_t uuid[16 ], +struct mesh_config_node *db_node);uint16_t node_generate_comp (struct mesh_node *node,uint8_t *buf,uint16_t sz);uint8_t node_lpn_mode_get (struct mesh_node *node); bool node_relay_mode_set(struct mesh_node *node, bool enable, uint8_t cnt,
@@ -95,7 +98,7 @@ void node_id_set(struct mesh_node *node, uint16_t node_id);uint16_t node_id_get (struct mesh_node *node);bool node_dbus_init (struct l_dbus *bus);void node_cleanup_all (void ); -void node_jconfig_set (struct mesh_node *node,void *jconfig); -void *node_jconfig_get (struct mesh_node *node); +void node_config_set (struct mesh_node *node,struct mesh_config *cfg); +struct mesh_config *node_config_get (struct mesh_node *node);void node_path_set (struct mesh_node *node,char *path);char *node_path_get (struct mesh_node *node);
mesh/storage.c +64 -141
396 lines not on this page · raw message
mesh/storage.h +3 -1
@@ -19,9 +19,11 @@struct mesh_net ;struct mesh_node ; +struct mesh_config_node ;bool storage_load_nodes (const char *dir); -bool storage_create_node_config (struct mesh_node *node,void *db_node); +bool storage_create_node_config (struct mesh_node *node,const uint8_t uuid[16 ], +struct mesh_config_node *db_node);void storage_remove_node_config (struct mesh_node *node);void storage_save_config (struct mesh_node *node,bool no_wait,mesh_status_func_t cb,void *user_data);
Signature · 1 line
2.21.0
PATCH v2 6/10 mesh: Replace storage_save_config with mesh_config_save_config
In reply to Inga StotlandThis moves writing out of node configuration from storage.c down to mesh-config-<format-specific>.c to allow for more generic storage layout.
New generalized API in mesh-config.h:
mesh_config_save_config(struct mesh_config *cfg, bool no_wait,
mesh_config_status_func_t cb, void *user_data)
replaces the old one in storage.h:
storage_save_config(struct mesh_node *node, bool no_wait,
mesh_status_func_t cb, void *user_data)
Currently, only JSON format is supported for storing node configuration:mesh_config_save_config is implemented in mesh-config-json.c
--- mesh/mesh-config-json.c | 70 +++++++++++++++++++++++++++++++---- mesh/mesh-config.h | 9 +++-- mesh/node.c | 6 +-- mesh/storage.c | 82 +++++++---------------------------------- mesh/storage.h | 2 - 5 files changed, 84 insertions(+), 85 deletions(-)
mesh/mesh-config-json.c +63 -7
@@ -46,6 +46,15 @@ struct mesh_config {uint8_t uuid[16 ]; }; +struct write_info { +struct mesh_config *cfg; +void *user_data; +mesh_config_status_func_t cb; +}; + +static const char *bak_ext =".bak" ; +static const char *tmp_ext =".tmp" ; +static bool get_int (json_object *jobj,const char *keyword,int *value) {json_object *jvalue;
@@ -1926,7 +1935,7 @@ bool mesh_config_model_sub_del_all(struct mesh_config *cfg, uint16_t addr, }bool mesh_config_load_node (const char *cfg_path,const uint8_t uuid[16 ], -mesh_config_node_cb cb,void *user_data) +mesh_config_node_func_t cb,void *user_data) {int fd;char *str;
@@ -2011,23 +2020,19 @@ void mesh_config_release(struct mesh_config *cfg)l_free (cfg); } -bool mesh_config_save_config (struct mesh_config *cfg,const char *fname) +static bool save_config (json_object *jnode,const char *fname) {FILE *outfile;const char *str;bool result = false; -if (!cfg) -return false; - outfile =fopen (fname,"w" );if (!outfile) {l_error ("Failed to save configuration to %s" , fname);return false; } - str =json_object_to_json_string_ext (cfg->jnode, -JSON_C_TO_STRING_PRETTY ); + str =json_object_to_json_string_ext (jnode,JSON_C_TO_STRING_PRETTY );if (fwrite (str,sizeof (char ),strlen (str), outfile) <strlen (str))l_warn ("Incomplete write of mesh configuration" );
@@ -2038,3 +2043,54 @@ bool mesh_config_save_config(struct mesh_config *cfg, const char *fname)return result; } + +static void idle_save_config (void *user_data) +{ +struct write_info *info = user_data; +char *fname_tmp, *fname_bak, *fname_cfg; +bool result = false; + + fname_cfg = info->cfg->node_path; + fname_tmp =l_strdup_printf ("%s%s" , fname_cfg, tmp_ext); + fname_bak =l_strdup_printf ("%s%s" , fname_cfg, bak_ext); +remove (fname_tmp); + + result =save_config (info->cfg->jnode, fname_tmp); + +if (result) { +remove (fname_bak); +rename (fname_cfg, fname_bak); +rename (fname_tmp, fname_cfg); + } + +remove (fname_tmp); + +l_free (fname_tmp); +l_free (fname_bak); + +if (info->cb) + info->cb (info->user_data, result); + +l_free (info); +} + +bool mesh_config_save_config (struct mesh_config *cfg,bool no_wait, +mesh_config_status_func_t cb,void *user_data) +{ +struct write_info *info; + +if (!cfg) +return false; + + info = l_new(struct write_info, 1); + info->cfg = cfg; + info->cb = cb; + info->user_data = user_data; + +if (no_wait) +idle_save_config (info); +else +l_idle_oneshot (idle_save_config, info,NULL ); + +return true; +}
mesh/mesh-config.h +5 -4
@@ -104,16 +104,17 @@ struct mesh_config_node {uint8_t dev_key[16 ];uint8_t token[8 ]; }; - -typedef bool (*mesh_config_node_cb )(struct mesh_config_node *node, +typedef void (*mesh_config_status_func_t )(void *user_data,bool result); +typedef bool (*mesh_config_node_func_t )(struct mesh_config_node *node,const uint8_t uuid[16 ],struct mesh_config *cfg,void *user_data);bool mesh_config_load_node (const char *cfg_path,const uint8_t uuid[16 ], -mesh_config_node_cb cb,void *user_data); +mesh_config_node_func_t cb,void *user_data);void mesh_config_release (struct mesh_config *cfg); -bool mesh_config_save_config (struct mesh_config *cfg,const char *fname); +bool mesh_config_save_config (struct mesh_config *cfg,bool no_wait, +mesh_config_status_func_t cb,void *user_data);struct mesh_config *mesh_config_create (const char *cfg_path,const uint8_t uuid[16 ],struct mesh_config_node *node);
mesh/node.c +3 -3
@@ -482,12 +482,12 @@ static void cleanup_node(void *data)struct mesh_net *net = node->net;/* Save local node configuration */ -if (node->node_path) { +if (node->cfg) {/* Preserve the last sequence number */ storage_write_sequence_number (net,mesh_net_get_seq_num (net)); -storage_save_config (node, true,NULL ,NULL ); +mesh_config_save_config (node->cfg, true,NULL ,NULL ); }free_node_resources (node);
@@ -1494,7 +1494,7 @@ static bool add_local_node(struct mesh_node *node, uint16_t unicast, bool kr,return false; } -storage_save_config (node, true,NULL ,NULL ); +mesh_config_save_config (node->cfg, true,NULL ,NULL );/* Initialize configuration server model */ mesh_config_srv_init (node,PRIMARY_ELE_IDX );
mesh/storage.c +13 -69
171 lines not on this page · raw message
mesh/storage.h +0 -2
@@ -25,8 +25,6 @@ bool storage_load_nodes(const char *dir);bool storage_create_node_config (struct mesh_node *node,const uint8_t uuid[16 ],struct mesh_config_node *db_node);void storage_remove_node_config (struct mesh_node *node); -void storage_save_config (struct mesh_node *node,bool no_wait, -mesh_status_func_t cb,void *user_data);bool storage_model_bind (struct mesh_node *node,uint16_t addr,uint32_t id,uint16_t app_idx,bool unbind);
Signature · 1 line
2.21.0
PATCH v2 7/10 mesh: Use mesh_config APIs to store node configuration
In reply to Inga StotlandThis eliminates storage_set...() routines as an intermediate layer between mesh_config layer and the rest of the daemon when storing updated node configuration values.
For the JSON based implementation, each call to mesh_config_write...() routines results in writing to the node configuration file.
--- mesh/appkey.c | 19 +++- mesh/cfgmod-server.c | 4 +- mesh/mesh-config-json.c | 203 +++++++++++++++++++--------------------- mesh/mesh-config.h | 11 +-- mesh/model.c | 15 ++- mesh/net.c | 26 +++-- mesh/node.c | 23 ++--- mesh/storage.c | 170 --------------------------------- mesh/storage.h | 24 ----- 9 files changed, 155 insertions(+), 340 deletions(-)
mesh/appkey.c +15 -4
@@ -31,7 +31,7 @@#include "mesh/crypto.h" #include "mesh/util.h" #include "mesh/model.h" -#include "mesh/storage.h" +#include "mesh/mesh-config.h" #include "mesh/appkey.h" struct mesh_app_key {
@@ -368,6 +368,7 @@ int appkey_key_update(struct mesh_net *net, uint16_t net_idx, uint16_t app_idx,struct mesh_app_key *key;struct l_queue *app_keys;uint8_t phase =KEY_REFRESH_PHASE_NONE ; +struct mesh_node *node; app_keys =mesh_net_get_app_keys (net);if (!app_keys)
@@ -395,7 +396,10 @@ int appkey_key_update(struct mesh_net *net, uint16_t net_idx, uint16_t app_idx,if (!set_key (key, app_idx, new_key, true))return MESH_STATUS_INSUFF_RESOURCES ; -if (!storage_app_key_add (net, net_idx, app_idx, new_key, true)) + node =mesh_net_node_get (net); + +if (!mesh_config_app_key_update (node_config_get (node), app_idx, + new_key))return MESH_STATUS_STORAGE_FAIL ;return MESH_STATUS_SUCCESS ;
@@ -406,6 +410,7 @@ int appkey_key_add(struct mesh_net *net, uint16_t net_idx, uint16_t app_idx, {struct mesh_app_key *key;struct l_queue *app_keys; +struct mesh_node *node; app_keys =mesh_net_get_app_keys (net);if (!app_keys)
@@ -432,7 +437,10 @@ int appkey_key_add(struct mesh_net *net, uint16_t net_idx, uint16_t app_idx,return MESH_STATUS_INSUFF_RESOURCES ; } -if (!storage_app_key_add (net, net_idx, app_idx, new_key, false)) { + node =mesh_net_node_get (net); + +if (!mesh_config_app_key_add (node_config_get (node), net_idx, app_idx, + new_key)) {appkey_key_free (key);return MESH_STATUS_STORAGE_FAIL ; }
@@ -451,6 +459,7 @@ int appkey_key_delete(struct mesh_net *net, uint16_t net_idx, {struct mesh_app_key *key;struct l_queue *app_keys; +struct mesh_node *node; app_keys =mesh_net_get_app_keys (net);if (!app_keys)
@@ -469,7 +478,9 @@ int appkey_key_delete(struct mesh_net *net, uint16_t net_idx,l_queue_remove (app_keys, key);appkey_key_free (key); -if (!storage_app_key_del (net, net_idx, app_idx)) + node =mesh_net_node_get (net); + +if (!mesh_config_app_key_del (node_config_get (node), net_idx, app_idx))return MESH_STATUS_STORAGE_FAIL ;return MESH_STATUS_SUCCESS ;
mesh/cfgmod-server.c +3 -1
@@ -876,7 +876,9 @@ static bool cfg_srv_pkt(uint16_t src, uint32_t dst, count = (pkt[0 ] >>5 ) +1 ; interval = ((pkt[0 ] &0x1f ) +1 ) *10 ; -if (storage_set_transmit_params (node, count, interval)) + +if (mesh_config_write_net_transmit (node_config_get (node), count, + interval))mesh_net_transmit_params_set (net, count, interval);/* Fall Through */
mesh/mesh-config-json.c +96 -107
396 lines not on this page · raw message
mesh/mesh-config.h +3 -8
@@ -127,16 +127,11 @@ bool mesh_config_write_network_key(struct mesh_config *cfg, uint16_t idx, uint8_t *key, uint8_t *new_key, int phase);bool mesh_config_write_app_key (struct mesh_config *cfg,uint16_t net_idx,uint16_t app_idx,uint8_t *key,uint8_t *new_key); -bool mesh_config_write_int (struct mesh_config *cfg, -const char *keyword,int value); -bool mesh_config_write_uint16_hex (struct mesh_config *cfg,const char *desc, -uint16_t value); -bool mesh_config_write_uint32_hex (struct mesh_config *cfg,const char *desc, -uint32_t value); -bool mesh_config_write_bool (struct mesh_config *cfg,const char *keyword, -bool value); +bool mesh_config_write_seq_number (struct mesh_config *cfg,uint32_t seq); +bool mesh_config_write_unicast (struct mesh_config *cfg,uint16_t unicast);bool mesh_config_write_relay_mode (struct mesh_config *cfg,uint8_t mode,uint8_t count,uint16_t interval); +bool mesh_config_write_ttl (struct mesh_config *cfg,uint8_t ttl);bool mesh_config_write_mode (struct mesh_config *cfg,const char *keyword,int value); bool mesh_config_model_binding_add(struct mesh_config *cfg, uint8_t ele_idx,
mesh/model.c +10 -5
@@ -532,7 +532,8 @@ static int update_binding(struct mesh_node *node, uint16_t addr, uint32_t id, {int status;struct mesh_model *mod; -bool is_present; +bool is_present, is_vendor; +uint8_t ele_idx; mod =find_model (node, addr, id, &status);if (!mod) {
@@ -540,7 +541,8 @@ static int update_binding(struct mesh_node *node, uint16_t addr, uint32_t id,return status; } - id = (id >=VENDOR_ID_MASK ) ? (id &0xffff ) : id; + is_vendor = id <VENDOR_ID_MASK && id >0xffff ; + id = !is_vendor ? (id &0xffff ) : id;if (id ==CONFIG_SRV_MODEL || id ==CONFIG_CLI_MODEL )return MESH_STATUS_INVALID_MODEL ;
@@ -556,10 +558,12 @@ static int update_binding(struct mesh_node *node, uint16_t addr, uint32_t id,if (is_present && !unbind)return MESH_STATUS_SUCCESS ; + ele_idx = (uint8_t )node_get_element_idx (node, addr); +if (unbind) {model_unbind_idx (node, mod, app_idx); - -if (!storage_model_bind (node, addr, id, app_idx, true)) +if (!mesh_config_model_binding_del (node_config_get (node), + ele_idx, is_vendor, id, app_idx))return MESH_STATUS_STORAGE_FAIL ;return MESH_STATUS_SUCCESS ;
@@ -568,7 +572,8 @@ static int update_binding(struct mesh_node *node, uint16_t addr, uint32_t id,if (l_queue_length (mod->bindings) >=MAX_BINDINGS )return MESH_STATUS_INSUFF_RESOURCES ; -if (!storage_model_bind (node, addr, id, app_idx, false)) +if (!mesh_config_model_binding_add (node_config_get (node), + ele_idx, is_vendor, id, app_idx))return MESH_STATUS_STORAGE_FAIL ;model_bind_idx (node, mod, app_idx);
mesh/net.c +16 -10
@@ -33,7 +33,7 @@#include "mesh/net.h" #include "mesh/mesh-io.h" #include "mesh/friend.h" -#include "mesh/storage.h" +#include "mesh/mesh-config.h" #include "mesh/model.h" #include "mesh/appkey.h"
@@ -971,7 +971,7 @@ int mesh_net_del_key(struct mesh_net *net, uint16_t idx)l_queue_remove (net->subnets, subnet);subnet_free (subnet); -if (!storage_net_key_del (net, idx)) +if (!mesh_config_net_key_del (node_config_get (net->node), idx))return MESH_STATUS_STORAGE_FAIL ;return MESH_STATUS_SUCCESS ;
@@ -1024,7 +1024,7 @@ int mesh_net_add_key(struct mesh_net *net, uint16_t idx, const uint8_t *value)if (!subnet)return MESH_STATUS_INSUFF_RESOURCES ; -if (!storage_net_key_add (net, idx, value, false)) { +if (!mesh_config_net_key_add (node_config_get (net->node), idx, value)) {l_queue_remove (net->subnets, subnet);subnet_free (subnet);return MESH_STATUS_STORAGE_FAIL ;
@@ -2662,7 +2662,8 @@ static int key_refresh_phase_two(struct mesh_net *net, uint16_t idx)else l_queue_foreach (net->friends, frnd_kr_phase2, net); -storage_set_key_refresh_phase (net, idx,KEY_REFRESH_PHASE_TWO ); +mesh_config_net_key_set_phase (node_config_get (net->node), idx, +KEY_REFRESH_PHASE_TWO );return MESH_STATUS_SUCCESS ; }
@@ -2697,7 +2698,8 @@ static int key_refresh_finish(struct mesh_net *net, uint16_t idx)else l_queue_foreach (net->friends, frnd_kr_phase3, net); -storage_set_key_refresh_phase (net, idx,KEY_REFRESH_PHASE_NONE ); +mesh_config_net_key_set_phase (node_config_get (net->node), idx, +KEY_REFRESH_PHASE_NONE );return MESH_STATUS_SUCCESS ; }
@@ -2749,7 +2751,8 @@ static void update_iv_kr_state(struct mesh_subnet *subnet, uint32_t iv_index, net->iv_upd_state =IV_UPD_NORMAL ; } -storage_set_iv_index (net, iv_index, net->iv_upd_state); +mesh_config_write_iv_index (node_config_get (net->node), iv_index, + net->iv_upd_state);/* Figure out the key refresh phase */ if (kr_transition) {
@@ -2771,7 +2774,8 @@ static void update_iv_kr_state(struct mesh_subnet *subnet, uint32_t iv_index, net->iv_upd_state =IV_UPD_UPDATING ; net->iv_update_timeout =l_timeout_create (IV_IDX_UPD_MIN , iv_upd_to, net,NULL ); -storage_set_iv_index (net, iv_index, net->iv_upd_state); +mesh_config_write_iv_index (node_config_get (net->node), iv_index, + net->iv_upd_state); }else if (iv_update && iv_index != net->iv_index) {l_error ("Update attempted too soon (iv idx already updated)" );return ;
@@ -2784,7 +2788,8 @@ static void update_iv_kr_state(struct mesh_subnet *subnet, uint32_t iv_index,if (iv_index > net->iv_index) {l_queue_clear (net->msg_cache, mesh_msg_free); net->iv_index = iv_index; -storage_set_iv_index (net, iv_index, net->iv_upd_state); +mesh_config_write_iv_index (node_config_get (net->node), iv_index, + net->iv_upd_state); }/* Figure out the key refresh phase */
@@ -3122,7 +3127,8 @@ bool mesh_net_iv_index_update(struct mesh_net *net)mesh_net_flush_msg_queues (net); net->iv_upd_state =IV_UPD_UPDATING ; net->iv_index++; -if (!storage_set_iv_index (net, net->iv_index,IV_UPD_UPDATING )) +if (!mesh_config_write_iv_index (node_config_get (net->node), + net->iv_index,IV_UPD_UPDATING ))return false;l_queue_foreach (net->subnets, set_network_beacon, net);
@@ -3739,7 +3745,7 @@ int mesh_net_update_key(struct mesh_net *net, uint16_t idx,l_info ("key refresh phase 1: Key ID %d" , subnet->net_key_upd); -if (!storage_net_key_add (net, idx, value, true)) +if (!mesh_config_net_key_update (node_config_get (net->node), idx, value))return MESH_STATUS_STORAGE_FAIL ; subnet->kr_phase =KEY_REFRESH_PHASE_ONE ;
mesh/node.c +12 -11
@@ -485,7 +485,8 @@ static void cleanup_node(void *data)if (node->cfg) {/* Preserve the last sequence number */ -storage_write_sequence_number (net,mesh_net_get_seq_num (net)); +mesh_config_write_seq_number (node->cfg, +mesh_net_get_seq_num (net));mesh_config_save_config (node->cfg, true,NULL ,NULL ); }
@@ -525,7 +526,8 @@ bool node_app_key_delete(struct mesh_net *net, uint16_t addr,mesh_model_app_key_delete (node, ele->models, app_idx); } -return true; + +return mesh_config_app_key_del (node->cfg, net_idx, app_idx); }uint16_t node_get_primary (struct mesh_node *node)
@@ -606,7 +608,7 @@ bool node_default_ttl_set(struct mesh_node *node, uint8_t ttl)if (!node)return false; - res =storage_set_ttl (node, ttl); + res =mesh_config_write_ttl (node->cfg, ttl);if (res) { node->ttl = ttl;
@@ -653,7 +655,7 @@ bool node_set_sequence_number(struct mesh_node *node, uint32_t seq) node->upd_sec = write_time.tv_sec; -return storage_write_sequence_number (node->net, seq); +return mesh_config_write_seq_number (node->cfg, seq); }uint32_t node_get_sequence_number (struct mesh_node *node)
@@ -730,7 +732,7 @@ bool node_relay_mode_set(struct mesh_node *node, bool enable, uint8_t cnt,if (!node || node->relay.mode ==MESH_MODE_UNSUPPORTED )return false; - res =storage_set_relay (node, enable, cnt, interval); + res =mesh_config_write_relay_mode (node->cfg, enable, cnt, interval);if (res) { node->relay.mode = enable ? MESH_MODE_ENABLED :
@@ -752,7 +754,7 @@ bool node_proxy_mode_set(struct mesh_node *node, bool enable)return false; proxy = enable ?MESH_MODE_ENABLED :MESH_MODE_DISABLED ; - res =storage_set_mode (node, proxy,"proxy" ); + res =mesh_config_write_mode (node->cfg,"proxy" , proxy);if (res) { node->proxy = proxy;
@@ -779,7 +781,7 @@ bool node_beacon_mode_set(struct mesh_node *node, bool enable)return false; beacon = enable ?MESH_MODE_ENABLED :MESH_MODE_DISABLED ; - res =storage_set_mode (node, beacon,"beacon" ); + res =mesh_config_write_mode (node->cfg,"beacon" , beacon);if (res) { node->beacon = beacon;
@@ -806,7 +808,7 @@ bool node_friend_mode_set(struct mesh_node *node, bool enable)return false; friend = enable ?MESH_MODE_ENABLED :MESH_MODE_DISABLED ; - res =storage_set_mode (node, friend,"friend" ); + res =mesh_config_write_mode (node->cfg,"friend" , friend);if (res) { node->friend = friend;
@@ -1459,13 +1461,12 @@ static bool add_local_node(struct mesh_node *node, uint16_t unicast, bool kr,l_queue_push_tail (nodes, node); -if (!storage_set_iv_index (node->net, iv_idx, ivu)) +if (!mesh_config_write_iv_index (node->cfg, iv_idx, ivu))return false;mesh_net_set_iv_index (node->net, iv_idx, ivu); -if (!mesh_config_write_uint16_hex (node->cfg,"unicastAddress" , - unicast)) +if (!mesh_config_write_unicast (node->cfg, unicast))return false;l_getrandom (node->token,sizeof (node->token));
mesh/storage.c +0 -170
176 lines not on this page · raw message
mesh/storage.h +0 -24
@@ -25,27 +25,3 @@ bool storage_load_nodes(const char *dir);bool storage_create_node_config (struct mesh_node *node,const uint8_t uuid[16 ],struct mesh_config_node *db_node);void storage_remove_node_config (struct mesh_node *node); -bool storage_model_bind (struct mesh_node *node,uint16_t addr,uint32_t id, -uint16_t app_idx,bool unbind); - -bool storage_set_ttl (struct mesh_node *node,uint8_t ttl); -bool storage_set_relay (struct mesh_node *node,bool enable,uint8_t count, -uint8_t interval); -bool storage_set_transmit_params (struct mesh_node *node,uint8_t count, -uint8_t interval); -bool storage_set_mode (struct mesh_node *node,uint8_t mode, -const char *mode_name); -bool storage_net_key_add (struct mesh_net *net,uint16_t net_idx, -const uint8_t key[16 ],bool update); -bool storage_net_key_del (struct mesh_net *net,uint16_t net_idx); -bool storage_app_key_add (struct mesh_net *net,uint16_t net_idx, -uint16_t app_idx,const uint8_t key[16 ],bool update); -bool storage_app_key_del (struct mesh_net *net,uint16_t net_idx, -uint16_t app_idx); -bool storage_write_sequence_number (struct mesh_net *net,uint32_t seq); -bool storage_set_iv_index (struct mesh_net *net,uint32_t iv_index, -bool update); -bool storage_set_device_key (struct mesh_node *node,uint8_t dev_key[16 ]); -bool storage_set_unicast (struct mesh_node *node,uint16_t unicast); -bool storage_set_key_refresh_phase (struct mesh_net *net,uint16_t net_idx, -uint8_t phase);
Signature · 1 line
2.21.0
PATCH v2 8/10 mesh: Manage node config directory in mesh-config
In reply to Inga StotlandThis completely remove storage.c file. Instead, the handling of mesh node configuration storage is done completely in mesh-config layer by calling the following API functions:
bool mesh_config_load_nodes(const char *cfg_dir,
mesh_config_node_func_t cb, void *user_data)
void mesh_config_release(struct mesh_config *cfg)
void mesh_config_destroy(struct mesh_config *cfg)
bool mesh_config_save(struct mesh_config *cfg, bool no_wait,
mesh_config_status_func_t cb, void *user_data)
struct mesh_config *mesh_config_create(const char *cfg_path,
const uint8_t uuid[16], struct mesh_config_node *node)
--- Makefile.mesh | 1 - mesh/cfgmod-server.c | 1 - mesh/mesh-config-json.c | 211 +++++++++++++++++++++++++++------ mesh/mesh-config.h | 7 +- mesh/mesh.c | 15 ++- mesh/mesh.h | 1 + mesh/model.c | 1 - mesh/node.c | 48 ++++---- mesh/node.h | 3 +- mesh/storage.c | 254 ---------------------------------------- mesh/storage.h | 27 ----- mesh/util.c | 39 +++++- mesh/util.h | 1 + 13 files changed, 255 insertions(+), 354 deletions(-) delete mode 100644 mesh/storage.c delete mode 100644 mesh/storage.h
Makefile.mesh +0 -1
@@ -16,7 +16,6 @@ mesh_sources = mesh/mesh.h mesh/mesh.c \ mesh/error.h mesh/mesh-io-api.h \ mesh/mesh-io-generic.h \ mesh/mesh-io-generic.c \ - mesh/storage.h mesh/storage.c \ mesh/net.h mesh/net.c \ mesh/crypto.h mesh/crypto.c \ mesh/friend.h mesh/friend.c \
mesh/cfgmod-server.c +0 -1
@@ -29,7 +29,6 @@#include "mesh/net.h" #include "mesh/appkey.h" #include "mesh/model.h" -#include "mesh/storage.h" #include "mesh/mesh-config.h" #include "mesh/cfgmod.h"
mesh/mesh-config-json.c +172 -39
430 lines not on this page · raw message
mesh/mesh-config.h +4 -3
@@ -110,10 +110,11 @@ typedef bool (*mesh_config_node_func_t)(struct mesh_config_node *node,struct mesh_config *cfg, void *user_data); -bool mesh_config_load_node (const char *cfg_path,const uint8_t uuid[16 ], -mesh_config_node_func_t cb,void *user_data); +bool mesh_config_load_nodes (const char *cfg_dir,mesh_config_node_func_t cb, +void *user_data);void mesh_config_release (struct mesh_config *cfg); -bool mesh_config_save_config (struct mesh_config *cfg,bool no_wait, +void mesh_config_destroy (struct mesh_config *cfg); +bool mesh_config_save (struct mesh_config *cfg,bool no_wait,mesh_config_status_func_t cb,void *user_data); struct mesh_config *mesh_config_create(const char *cfg_path, const uint8_t uuid[16],
mesh/mesh.c +10 -5
@@ -27,7 +27,6 @@#include "mesh/mesh-io.h" #include "mesh/node.h" #include "mesh/net.h" -#include "mesh/storage.h" #include "mesh/provision.h" #include "mesh/model.h" #include "mesh/dbus.h"
@@ -78,6 +77,8 @@ static struct join_data *join_pending;/* Pending method requests */ static struct l_queue *pending_queue; +static const char *storage_dir; +static bool simple_match (const void *a,const void *b) {return a == b;
@@ -150,12 +151,11 @@ bool mesh_init(const char *config_dir, enum mesh_io_type type, void *opts) mesh.prov_timeout =DEFAULT_PROV_TIMEOUT ; mesh.algorithms =DEFAULT_ALGORITHMS ; -if (!config_dir) - config_dir =MESH_STORAGEDIR ; + storage_dir = config_dir ? config_dir :MESH_STORAGEDIR ; -l_info ("Loading node configuration from %s" , config_dir); +l_info ("Loading node configuration from %s" , storage_dir); -if (!storage_load_nodes (config_dir)) +if (!node_load_from_storage (storage_dir))return false; mesh.io =mesh_io_new (type, opts);
@@ -638,3 +638,8 @@ bool mesh_dbus_init(struct l_dbus *dbus)return true; } + +const char *mesh_get_storage_dir (void ) +{ +return storage_dir; +}
mesh/mesh.h +1 -0
@@ -42,3 +42,4 @@ bool mesh_send_pkt(uint8_t count, uint16_t interval, uint8_t *data,bool mesh_send_cancel (const uint8_t *filter,uint8_t len);bool mesh_reg_prov_rx (prov_rx_cb_t cb,void *user_data);void mesh_unreg_prov_rx (prov_rx_cb_t cb); +const char *mesh_get_storage_dir (void );
mesh/model.c +0 -1
@@ -33,7 +33,6 @@#include "mesh/net.h" #include "mesh/appkey.h" #include "mesh/cfgmod.h" -#include "mesh/storage.h" #include "mesh/error.h" #include "mesh/dbus.h" #include "mesh/util.h"
mesh/node.c +28 -20
136 lines not on this page · raw message
mesh/node.h +1 -2
@@ -35,7 +35,6 @@ typedef void (*node_ready_func_t) (void *user_data, int status,typedef void (*node_join_ready_func_t ) (struct mesh_node *node,struct mesh_agent *agent); -struct mesh_node *node_new (const uint8_t uuid[16 ]);void node_remove (struct mesh_node *node);void node_join (const char *app_path,const char *sender,const uint8_t *uuid,node_join_ready_func_t cb);
@@ -98,7 +97,7 @@ void node_id_set(struct mesh_node *node, uint16_t node_id);uint16_t node_id_get (struct mesh_node *node);bool node_dbus_init (struct l_dbus *bus);void node_cleanup_all (void ); -void node_config_set (struct mesh_node *node,struct mesh_config *cfg);struct mesh_config *node_config_get (struct mesh_node *node);void node_path_set (struct mesh_node *node,char *path);char *node_path_get (struct mesh_node *node); +bool node_load_from_storage (const char *storage_dir);
mesh/storage.c deleted +0 -254
254 lines not on this page · raw message
mesh/storage.h deleted +0 -27
@@ -1,27 +0,0 @@ -/* -* -* BlueZ - Bluetooth protocol stack for Linux -* -* Copyright (C) 2017-2019 Intel Corporation. All rights reserved. -* -* -* This library is free software; you can redistribute it and/or -* modify it under the terms of the GNU Lesser General Public -* License as published by the Free Software Foundation; either -* version 2.1 of the License, or (at your option) any later version. -* -* This library is distributed in the hope that it will be useful, -* but WITHOUT ANY WARRANTY; without even the implied warranty of -* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU -* Lesser General Public License for more details. -* -*/ - -struct mesh_net ; -struct mesh_node ; -struct mesh_config_node ; - -bool storage_load_nodes (const char *dir); -bool storage_create_node_config (struct mesh_node *node,const uint8_t uuid[16 ], -struct mesh_config_node *db_node); -void storage_remove_node_config (struct mesh_node *node);
mesh/util.c +38 -1
@@ -22,7 +22,8 @@#endif #define _GNU_SOURCE - +#include <dirent.h> +#include <ftw.h> #include <stdio.h> #include <time.h> #include <sys/time.h>
@@ -91,3 +92,39 @@ size_t hex2str(uint8_t *in, size_t in_len, char *out, size_t out_len) out[in_len *2 ] ='\0' ;return i; } + +int create_dir (const char *dir_name) +{ +struct stat st; +char dir[PATH_MAX +1 ], *prev, *next; +int err; + + err =stat (dir_name, &st); +if (!err &&S_ISREG (st.st_mode)) +return 0 ; + +memset (dir,0 ,PATH_MAX +1 ); +strcat (dir,"/" ); + + prev =strchr (dir_name,'/' ); + +while (prev) { + next =strchr (prev +1 ,'/' ); +if (!next) +break ; + +if (next - prev ==1 ) { + prev = next; +continue ; + } + +strncat (dir, prev +1 , next - prev); +mkdir (dir,0755 ); + + prev = next; + } + +mkdir (dir_name,0755 ); + +return 0 ; +}
mesh/util.h +1 -0
@@ -22,3 +22,4 @@ bool str2hex(const char *str, uint16_t in_len, uint8_t *out, uint16_t out_len);size_t hex2str (uint8_t *in,size_t in_len,char *out,size_t out_len);void print_packet (const char *label,const void *data,uint16_t size); +int create_dir (const char *dir_name);
Signature · 1 line
2.21.0
PATCH v2 9/10 mesh: Create or re-use a node storage directory for keyring
In reply to Inga StotlandThis adds APIs to set a directory for storing node's key ring info. The directory is named MESH_STORAGE_DIR/<node uuid>. This directory may contain additional node info plus node configuration, if node configuration storage follows the same layout (as it does when JSON based config file format is used).
--- mesh/keyring.c | 36 ++++++++++++++++++------------------ mesh/node.c | 41 ++++++++++++++++++++++++++++++++--------- mesh/node.h | 4 ++-- 3 files changed, 52 insertions(+), 29 deletions(-)
mesh/keyring.c +18 -18
@@ -43,7 +43,7 @@ const char *net_key_dir = "/net_keys";bool keyring_put_net_key (struct mesh_node *node,uint16_t net_idx,struct keyring_net_key *key) { -char *node_path; +const char *node_path;char key_file[PATH_MAX ];bool result = false;int fd;
@@ -51,7 +51,7 @@ bool keyring_put_net_key(struct mesh_node *node, uint16_t net_idx,if (!node || !key)return false; - node_path =node_path_get (node); + node_path =node_get_storage_dir (node);if (strlen (node_path) +strlen (net_key_dir) +1 +3 >=PATH_MAX )return false;
@@ -76,7 +76,7 @@ bool keyring_put_net_key(struct mesh_node *node, uint16_t net_idx,bool keyring_put_app_key (struct mesh_node *node,uint16_t app_idx,uint16_t net_idx,struct keyring_app_key *key) { -char *node_path; +const char *node_path;char key_file[PATH_MAX ];bool result = false;int fd;
@@ -84,7 +84,7 @@ bool keyring_put_app_key(struct mesh_node *node, uint16_t app_idx,if (!node || !key)return false; - node_path =node_path_get (node); + node_path =node_get_storage_dir (node);if (strlen (node_path) +strlen (app_key_dir) +1 +3 >=PATH_MAX )return false;
@@ -123,7 +123,7 @@ bool keyring_put_app_key(struct mesh_node *node, uint16_t app_idx,bool keyring_put_remote_dev_key (struct mesh_node *node,uint16_t unicast,uint8_t count,uint8_t dev_key[16 ]) { -char *node_path; +const char *node_path;char key_file[PATH_MAX ];bool result = true;int fd, i;
@@ -131,7 +131,7 @@ bool keyring_put_remote_dev_key(struct mesh_node *node, uint16_t unicast,if (!node)return false; - node_path =node_path_get (node); + node_path =node_get_storage_dir (node);if (strlen (node_path) +strlen (dev_key_dir) +1 +4 >=PATH_MAX )return false;
@@ -161,7 +161,7 @@ bool keyring_put_remote_dev_key(struct mesh_node *node, uint16_t unicast,bool keyring_get_net_key (struct mesh_node *node,uint16_t net_idx,struct keyring_net_key *key) { -char *node_path; +const char *node_path;char key_file[PATH_MAX ];bool result = false;int fd;
@@ -169,7 +169,7 @@ bool keyring_get_net_key(struct mesh_node *node, uint16_t net_idx,if (!node || !key)return false; - node_path =node_path_get (node); + node_path =node_get_storage_dir (node);snprintf (key_file,PATH_MAX ,"%s%s/%3.3x" , node_path, net_key_dir, net_idx);
@@ -187,7 +187,7 @@ bool keyring_get_net_key(struct mesh_node *node, uint16_t net_idx,bool keyring_get_app_key (struct mesh_node *node,uint16_t app_idx,struct keyring_app_key *key) { -char *node_path; +const char *node_path;char key_file[PATH_MAX ];bool result = false;int fd;
@@ -195,7 +195,7 @@ bool keyring_get_app_key(struct mesh_node *node, uint16_t app_idx,if (!node || !key)return false; - node_path =node_path_get (node); + node_path =node_get_storage_dir (node);snprintf (key_file,PATH_MAX ,"%s%s/%3.3x" , node_path, app_key_dir, app_idx);
@@ -213,7 +213,7 @@ bool keyring_get_app_key(struct mesh_node *node, uint16_t app_idx,bool keyring_get_remote_dev_key (struct mesh_node *node,uint16_t unicast,uint8_t dev_key[16 ]) { -char *node_path; +const char *node_path;char key_file[PATH_MAX ];bool result = false;int fd;
@@ -221,7 +221,7 @@ bool keyring_get_remote_dev_key(struct mesh_node *node, uint16_t unicast,if (!node)return false; - node_path =node_path_get (node); + node_path =node_get_storage_dir (node);snprintf (key_file,PATH_MAX ,"%s%s/%4.4x" , node_path, dev_key_dir, unicast);
@@ -238,13 +238,13 @@ bool keyring_get_remote_dev_key(struct mesh_node *node, uint16_t unicast,bool keyring_del_net_key (struct mesh_node *node,uint16_t net_idx) { -char *node_path; +const char *node_path;char key_file[PATH_MAX ];if (!node)return false; - node_path =node_path_get (node); + node_path =node_get_storage_dir (node);snprintf (key_file,PATH_MAX ,"%s%s/%3.3x" , node_path, net_key_dir, net_idx);l_debug ("RM Net Key %s" , key_file);
@@ -258,13 +258,13 @@ bool keyring_del_net_key(struct mesh_node *node, uint16_t net_idx)bool keyring_del_app_key (struct mesh_node *node,uint16_t app_idx) { -char *node_path; +const char *node_path;char key_file[PATH_MAX ];if (!node)return false; - node_path =node_path_get (node); + node_path =node_get_storage_dir (node);snprintf (key_file,PATH_MAX ,"%s%s/%3.3x" , node_path, app_key_dir, app_idx);l_debug ("RM App Key %s" , key_file);
@@ -276,14 +276,14 @@ bool keyring_del_app_key(struct mesh_node *node, uint16_t app_idx)bool keyring_del_remote_dev_key (struct mesh_node *node,uint16_t unicast,uint8_t count) { -char *node_path; +const char *node_path;char key_file[PATH_MAX ];int i;if (!node)return false; - node_path =node_path_get (node); + node_path =node_get_storage_dir (node);for (i =0 ; i < count; i++) {snprintf (key_file,PATH_MAX ,"%s%s/%4.4x" , node_path, dev_key_dir, unicast + i);
mesh/node.c +32 -9
78 lines not on this page · raw message
mesh/node.h +2 -2
@@ -98,6 +98,6 @@ uint16_t node_id_get(struct mesh_node *node);bool node_dbus_init (struct l_dbus *bus);void node_cleanup_all (void );struct mesh_config *node_config_get (struct mesh_node *node); -void node_path_set (struct mesh_node *node,char *path); -char *node_path_get (struct mesh_node *node); +const char *node_get_storage_dir (struct mesh_node *node); +bool node_init_storage_dir (struct mesh_node *node);bool node_load_from_storage (const char *storage_dir);
Signature · 1 line
2.21.0
PATCH v2 10/10 mesh: Rename mesh_config_srv_init() to cfgmod_server_init()
In reply to Inga StotlandThis renames mesh_config_srv_init() to cfgmod_server_init() to avoid confusion with mesh_config_..> API and to reflect that the call pertains to configuration server model.
--- mesh/cfgmod-server.c | 2 +- mesh/cfgmod.h | 2 +- mesh/node.c | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-)
mesh/cfgmod-server.c +1 -1
@@ -1284,7 +1284,7 @@ static const struct mesh_model_ops ops = { .pub = NULL }; -void mesh_config_srv_init (struct mesh_node *node,uint8_t ele_idx) +void cfgmod_server_init (struct mesh_node *node,uint8_t ele_idx) {l_debug ("%2.2x" , ele_idx);mesh_model_register (node, ele_idx,CONFIG_SRV_MODEL , &ops, node);
mesh/cfgmod.h +1 -1
@@ -94,4 +94,4 @@#define OP_VEND_MODEL_APP_GET 0x804C#define OP_VEND_MODEL_APP_LIST 0x804E -void mesh_config_srv_init (struct mesh_node *node,uint8_t ele_idx); +void cfgmod_server_init (struct mesh_node *node,uint8_t ele_idx);
mesh/node.c +2 -2
@@ -476,7 +476,7 @@ static bool init_from_storage(struct mesh_config_node *db_node, goto fail;/* Initialize configuration server model */ -mesh_config_srv_init (node,PRIMARY_ELE_IDX ); +cfgmod_server_init (node,PRIMARY_ELE_IDX ); node->cfg = cfg;
@@ -1509,7 +1509,7 @@ static bool add_local_node(struct mesh_node *node, uint16_t unicast, bool kr,mesh_config_save (node->cfg, true,NULL ,NULL );/* Initialize configuration server model */ -mesh_config_srv_init (node,PRIMARY_ELE_IDX ); +cfgmod_server_init (node,PRIMARY_ELE_IDX );return true; }
Signature · 1 line
2.21.0