Skip to contentSkip to conversation

mesh: Configuration storage re-org

PATCH Superseded Inga Stotland 3 participants 19 messages Jul 10, 2019

Subsystems (2)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

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. The only assumption is that the configuration is stored in <mesh_storage_directory>/<node_uuid> directory.

Currently, the daemon supports only JSON-based configuration format. It is expected that other configuration formats may be added in future.

Inga Stotland (9):
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: Generalize mesh-config APIs
mesh: Change variable prefix "jconfig" to "config"
mesh: Define storage format specific read/write routines
mesh: Implement config read/write for mesh json format
mesh: Switch to using mesh-config routines for storage
mesh: Make storage.c json-c agnostic

 Makefile.mesh                          |   2 +-
 mesh/cfgmod-server.c                   |  27 +-
 mesh/{mesh-db.c => mesh-config-json.c} | 515 ++++++++++++++++++-------
 mesh/mesh-config.h                     | 161 ++++++++
 mesh/mesh-db.h                         | 157 --------
 mesh/mesh.c                            |  11 +-
 mesh/model.c                           |   7 +-
 mesh/node.c                            |  83 ++--
 mesh/node.h                            |   4 +-
 mesh/storage.c                         | 385 +++++++-----------
 10 files changed, 745 insertions(+), 607 deletions(-)
 rename mesh/{mesh-db.c => mesh-config-json.c} (73%)
 create mode 100644 mesh/mesh-config.h
 delete mode 100644 mesh/mesh-db.h
Signature · 1 line
2.21.0
#1 lore raw

PATCH 1/9 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
index 1f781cfe9..a2d95dacf 100644
@@ -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
index 0f2b77fde..2b7804242 100644
@@ -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
#2 lore raw

PATCH 2/9 mesh: Rename mesh-db.c to mesh-config-json.c

In reply to Inga Stotland

This 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
index 1ace507af..502ba2a47 100644
@@ -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
index a19ddc72e..486c87307 100644
@@ -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
similarity index 99%
rename from mesh/mesh-db.c
rename to mesh/mesh-config-json.c
index e0a000261..35d0bd27e 100644
@@ -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
similarity index 100%
rename from mesh/mesh-db.h
rename to mesh/mesh-config.h
mesh/model.c +1 -1
index e08f95b71..0474762e0 100644
@@ -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
index a2d95dacf..06e809706 100644
@@ -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
index 2b7804242..bba2ef348 100644
@@ -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
#3 lore raw

PATCH 3/9 mesh: Change mesh_db prefix to mesh_config

In reply to Inga Stotland

This 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
index 486c87307..0479a9185 100644
@@ -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
index 0474762e0..9331e1044 100644
@@ -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
index 06e809706..6ebdcf588 100644
@@ -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
#4 lore raw

PATCH 4/9 mesh: Generalize mesh-config APIs

In reply to Inga Stotland

This changes argument for each mesh-config API to use void* as a pointer to a node configuration object. This makes usage of JSON opaque for the rest of the code and allows to plug in a non-JSON configuration storage implementation.

---
 mesh/mesh-config-json.c | 228 +++++++++++++++++++++++-----------------
 mesh/mesh-config.h      | 107 +++++++++----------
 2 files changed, 180 insertions(+), 155 deletions(-)
mesh/mesh-config-json.c +132 -96
index 8fcb8afe3..5ca086ad0 100644
@@ -252,10 +252,14 @@ 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)
+bool mesh_config_read_iv_index(void *cfg, uint32_t *idx, bool *update)
 {
+	json_object *jobj = cfg;
 	int tmp;
 
+	if (!jobj)
+		return false;
+
 	/* IV index */
 	if (!get_int(jobj, "IVindex", &tmp))
 		return false;
@@ -270,12 +274,12 @@ 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])
+bool mesh_config_read_token(void *cfg, uint8_t token[8])
 {
-	json_object *jvalue;
+	json_object *jobj = cfg, *jvalue;
 	char *str;
 
-	if (!token)
+	if (!token || !jobj)
 		return false;
 
 	if (!json_object_object_get_ex(jobj, "token", &jvalue))
@@ -288,12 +292,12 @@ 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])
+bool mesh_config_read_device_key(void *cfg, uint8_t key_buf[16])
 {
-	json_object *jvalue;
+	json_object *jvalue, *jobj = cfg;
 	char *str;
 
-	if (!key_buf)
+	if (!key_buf || !jobj)
 		return false;
 
 	if (!json_object_object_get_ex(jobj, "deviceKey", &jvalue))
@@ -306,15 +310,15 @@ 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,
+bool mesh_config_read_app_keys(void *cfg, mesh_config_app_key_cb cb,
 							void *user_data)
 {
-	json_object *jarray;
+	json_object *jarray, *jobj = cfg;
 	int len;
 	int i;
 
-	if (!cb)
-		return true;
+	if (!jobj || !cb)
+		return false;
 
 	if (!json_object_object_get_ex(jobj, "appKeys", &jarray))
 		return false;
@@ -368,15 +372,15 @@ bool mesh_config_read_app_keys(json_object *jobj, mesh_config_app_key_cb cb,
 	return true;
 }
 
-bool mesh_config_read_net_keys(json_object *jobj, mesh_config_net_key_cb cb,
+bool mesh_config_read_net_keys(void *cfg, mesh_config_net_key_cb cb,
 								void *user_data)
 {
-	json_object *jarray;
+	json_object *jarray, *jobj = cfg;
 	int len;
 	int i;
 
-	if (!cb)
-		return true;
+	if (!cb || !jobj)
+		return false;
 
 	if (!json_object_object_get_ex(jobj, "netKeys", &jarray))
 		return false;
@@ -431,12 +435,14 @@ bool mesh_config_read_net_keys(json_object *jobj, mesh_config_net_key_cb cb,
 	return true;
 }
 
-bool mesh_config_net_key_add(json_object *jobj, uint16_t idx,
-							const uint8_t key[16])
+bool mesh_config_net_key_add(void *cfg, uint16_t idx, const uint8_t key[16])
 {
-	json_object *jarray = NULL, *jentry = NULL, *jstring;
+	json_object *jarray = NULL, *jentry = NULL, *jstring, *jobj = cfg;
 	char buf[5];
 
+	if (!jobj)
+		return false;
+
 	json_object_object_get_ex(jobj, "netKeys", &jarray);
 	if (jarray)
 		jentry = get_key_object(jarray, idx);
@@ -479,12 +485,14 @@ fail:
 	return false;
 }
 
-bool mesh_config_net_key_update(json_object *jobj, uint16_t idx,
-							const uint8_t key[16])
+bool mesh_config_net_key_update(void *cfg, uint16_t idx, const uint8_t key[16])
 {
-	json_object *jarray, *jentry, *jstring;
+	json_object *jarray, *jentry, *jstring, *jobj = cfg;
 	const char *str;
 
+	if (!jobj)
+		return false;
+
 	if (!json_object_object_get_ex(jobj, "netKeys", &jarray))
 		return false;
 
@@ -510,9 +518,12 @@ bool mesh_config_net_key_update(json_object *jobj, uint16_t idx,
 	return true;
 }
 
-bool mesh_config_net_key_del(json_object *jobj, uint16_t idx)
+bool mesh_config_net_key_del(void *cfg, uint16_t idx)
 {
-	json_object *jarray, *jarray_new;
+	json_object *jarray, *jarray_new, *jobj = cfg;
+
+	if (!jobj)
+		return false;
 
 	if (!json_object_object_get_ex(jobj, "netKeys", &jarray))
 		return true;
@@ -541,21 +552,35 @@ bool mesh_config_net_key_del(json_object *jobj, uint16_t idx)
 	return true;
 }
 
-bool mesh_config_write_device_key(json_object *jnode, uint8_t *key)
+bool mesh_config_write_device_key(void *cfg, uint8_t *key)
 {
-	return add_key_value(jnode, "deviceKey", key);
+	json_object *jobj = cfg;
+
+	if (!jobj)
+		return false;
+
+	return add_key_value(jobj, "deviceKey", key);
 }
 
-bool mesh_config_write_token(json_object *jnode, uint8_t *token)
+bool mesh_config_write_token(void *cfg, uint8_t *token)
 {
-	return add_u64_value(jnode, "token", token);
+	json_object *jobj = cfg;
+
+	if (!jobj)
+		return false;
+
+	return add_u64_value(jobj, "token", token);
 }
 
-bool mesh_config_app_key_add(json_object *jobj, uint16_t net_idx,
+bool mesh_config_app_key_add(void *cfg, uint16_t net_idx,
 					uint16_t app_idx, const uint8_t key[16])
 {
 	json_object *jarray = NULL, *jentry = NULL, *jstring = NULL;
 	char buf[5];
+	json_object *jobj = cfg;
+
+	if (!jobj)
+		return false;
 
 	json_object_object_get_ex(jobj, "appKeys", &jarray);
 	if (jarray)
@@ -604,11 +629,15 @@ fail:
 	return false;
 }
 
-bool mesh_config_app_key_update(json_object *jobj, uint16_t app_idx,
+bool mesh_config_app_key_update(void *cfg, uint16_t app_idx,
 							const uint8_t key[16])
 {
 	json_object *jarray, *jentry = NULL, *jstring = NULL;
 	const char *str;
+	json_object *jobj = cfg;
+
+	if (!jobj)
+		return false;
 
 	if (!json_object_object_get_ex(jobj, "appKeys", &jarray))
 		return false;
@@ -629,9 +658,13 @@ bool mesh_config_app_key_update(json_object *jobj, uint16_t app_idx,
 	return add_key_value(jentry, "key", key);
 }
 
-bool mesh_config_app_key_del(json_object *jobj, uint16_t net_idx, uint16_t idx)
+bool mesh_config_app_key_del(void *cfg, uint16_t net_idx, uint16_t idx)
 {
 	json_object *jarray, *jarray_new;
+	json_object *jobj = cfg;
+
+	if (!jobj)
+		return false;
 
 	if (!json_object_object_get_ex(jobj, "appKeys", &jarray))
 		return true;
@@ -660,14 +693,17 @@ bool mesh_config_app_key_del(json_object *jobj, uint16_t net_idx, uint16_t idx)
 	return true;
 }
 
-bool mesh_config_model_binding_add(json_object *jnode, uint8_t ele_idx,
-					bool vendor, uint32_t mod_id,
-							uint16_t app_idx)
+bool mesh_config_model_binding_add(void *cfg, uint8_t ele_idx, bool vendor,
+					uint32_t mod_id, uint16_t app_idx)
 {
 	json_object *jmodel, *jstring, *jarray = NULL;
 	char buf[5];
+	json_object *jobj = cfg;
 
-	jmodel = get_element_model(jnode, ele_idx, mod_id, vendor);
+	if (!jobj)
+		return false;
+
+	jmodel = get_element_model(jobj, ele_idx, mod_id, vendor);
 	if (!jmodel)
 		return false;
 
@@ -695,14 +731,16 @@ bool mesh_config_model_binding_add(json_object *jnode, uint8_t ele_idx,
 	return true;
 }
 
-bool mesh_config_model_binding_del(json_object *jnode, uint8_t ele_idx,
-					bool vendor, uint32_t mod_id,
-							uint16_t app_idx)
+bool mesh_config_model_binding_del(void *cfg, uint8_t ele_idx, bool vendor,
+					uint32_t mod_id, uint16_t app_idx)
 {
-	json_object *jmodel, *jarray, *jarray_new;
+	json_object *jmodel, *jarray, *jarray_new, *jobj = cfg;
 	char buf[5];
 
-	jmodel = get_element_model(jnode, ele_idx, mod_id, vendor);
+	if (!jobj)
+		return false;
+
+	jmodel = get_element_model(jobj, ele_idx, mod_id, vendor);
 	if (!jmodel)
 		return false;
 
@@ -1162,14 +1200,13 @@ static bool parse_composition(json_object *jcomp, struct mesh_config_node *node)
 	return true;
 }
 
-bool mesh_config_read_node(json_object *jnode, mesh_config_node_cb cb,
-							void *user_data)
+bool mesh_config_read_node(void *cfg, mesh_config_node_cb cb, void *user_data)
 {
 	struct mesh_config_node node;
-	json_object *jvalue;
+	json_object *jvalue, *jobj = cfg;
 	char *str;
 
-	if (!jnode)
+	if (!jobj)
 		return false;
 
 	if (!cb) {
@@ -1179,14 +1216,14 @@ bool mesh_config_read_node(json_object *jnode, mesh_config_node_cb cb,
 
 	memset(&node, 0, sizeof(node));
 
-	if (!parse_composition(jnode, &node)) {
+	if (!parse_composition(jobj, &node)) {
 		l_info("Failed to parse local node composition");
 		return false;
 	}
 
-	parse_features(jnode, &node);
+	parse_features(jobj, &node);
 
-	if (!json_object_object_get_ex(jnode, "unicastAddress", &jvalue)) {
+	if (!json_object_object_get_ex(jobj, "unicastAddress", &jvalue)) {
 		l_info("Bad config: Unicast address must be present");
 		return false;
 	}
@@ -1195,7 +1232,7 @@ bool mesh_config_read_node(json_object *jnode, mesh_config_node_cb cb,
 	if (sscanf(str, "%04hx", &node.unicast) != 1)
 		return false;
 
-	if (json_object_object_get_ex(jnode, "defaultTTL", &jvalue)) {
+	if (json_object_object_get_ex(jobj, "defaultTTL", &jvalue)) {
 		int ttl = json_object_get_int(jvalue);
 
 		if (ttl < 0 || ttl == 1 || ttl > DEFAULT_TTL)
@@ -1203,11 +1240,11 @@ bool mesh_config_read_node(json_object *jnode, mesh_config_node_cb cb,
 		node.ttl = (uint8_t) ttl;
 	}
 
-	if (json_object_object_get_ex(jnode, "sequenceNumber", &jvalue))
+	if (json_object_object_get_ex(jobj, "sequenceNumber", &jvalue))
 		node.seq_number = json_object_get_int(jvalue);
 
 	/* Check for required "elements" property */
-	if (!json_object_object_get_ex(jnode, "elements", &jvalue))
+	if (!json_object_object_get_ex(jobj, "elements", &jvalue))
 		return false;
 
 	if (!parse_elements(jvalue, &node))
@@ -1216,10 +1253,9 @@ bool mesh_config_read_node(json_object *jnode, mesh_config_node_cb cb,
 	return cb(&node, user_data);
 }
 
-bool mesh_config_write_uint16_hex(json_object *jobj, const char *desc,
-								uint16_t value)
+bool mesh_config_write_uint16_hex(void *cfg, const char *desc, uint16_t value)
 {
-	json_object *jstring;
+	json_object *jstring, *jobj = cfg;
 	char buf[5];
 
 	if (!jobj)
@@ -1234,10 +1270,9 @@ bool mesh_config_write_uint16_hex(json_object *jobj, const char *desc,
 	return true;
 }
 
-bool mesh_config_write_uint32_hex(json_object *jobj, const char *desc,
-								uint32_t value)
+bool mesh_config_write_uint32_hex(void *cfg, const char *desc, uint32_t value)
 {
-	json_object *jstring;
+	json_object *jstring, *jobj = cfg;
 	char buf[9];
 
 	if (!jobj)
@@ -1252,9 +1287,9 @@ bool mesh_config_write_uint32_hex(json_object *jobj, const char *desc,
 	return true;
 }
 
-bool mesh_config_write_int(json_object *jobj, const char *keyword, int value)
+bool mesh_config_write_int(void *cfg, const char *keyword, int value)
 {
-	json_object *jvalue;
+	json_object *jvalue, *jobj = cfg;
 
 	if (!jobj)
 		return false;
@@ -1269,9 +1304,9 @@ bool mesh_config_write_int(json_object *jobj, const char *keyword, int value)
 	return true;
 }
 
-bool mesh_config_write_bool(json_object *jobj, const char *keyword, bool value)
+bool mesh_config_write_bool(void *cfg, const char *keyword, bool value)
 {
-	json_object *jvalue;
+	json_object *jvalue, *jobj = cfg;
 
 	if (!jobj)
 		return false;
@@ -1298,9 +1333,9 @@ static const char *mode_to_string(int mode)
 	}
 }
 
-bool mesh_config_write_mode(json_object *jobj, const char *keyword, int value)
+bool mesh_config_write_mode(void *cfg, const char *keyword, int value)
 {
-	json_object *jstring;
+	json_object *jstring, *jobj = cfg;
 
 	if (!jobj)
 		return false;
@@ -1315,15 +1350,15 @@ bool mesh_config_write_mode(json_object *jobj, const char *keyword, int value)
 	return true;
 }
 
-bool mesh_config_write_relay_mode(json_object *jnode, uint8_t mode,
-					uint8_t count, uint16_t interval)
+bool mesh_config_write_relay_mode(void *cfg, uint8_t mode, uint8_t count,
+							uint16_t interval)
 {
-	json_object *jrelay;
+	json_object *jrelay, *jobj = cfg;
 
-	if (!jnode)
+	if (!jobj)
 		return false;
 
-	json_object_object_del(jnode, "relay");
+	json_object_object_del(jobj, "relay");
 
 	jrelay = json_object_new_object();
 	if (!jrelay)
@@ -1338,7 +1373,7 @@ bool mesh_config_write_relay_mode(json_object *jnode, uint8_t mode,
 	if (!mesh_config_write_int(jrelay, "interval", interval))
 		goto fail;
 
-	json_object_object_add(jnode, "relay", jrelay);
+	json_object_object_add(jobj, "relay", jrelay);
 
 	return true;
 fail:
@@ -1346,10 +1381,9 @@ fail:
 	return false;
 }
 
-bool mesh_config_read_net_transmit(json_object *jobj, uint8_t *cnt,
-							uint16_t *interval)
+bool mesh_config_read_net_transmit(void *cfg, uint8_t *cnt, uint16_t *interval)
 {
-	json_object *jretransmit, *jvalue;
+	json_object *jretransmit, *jvalue, *jobj = cfg;
 
 	if (!jobj)
 		return false;
@@ -1370,10 +1404,9 @@ bool mesh_config_read_net_transmit(json_object *jobj, uint8_t *cnt,
 	return true;
 }
 
-bool mesh_config_write_net_transmit(json_object *jobj, uint8_t cnt,
-							uint16_t interval)
+bool mesh_config_write_net_transmit(void *cfg, uint8_t cnt, uint16_t interval)
 {
-	json_object *jretransmit;
+	json_object *jretransmit, *jobj = cfg;
 
 	if (!jobj)
 		return false;
@@ -1399,8 +1432,9 @@ fail:
 
 }
 
-bool mesh_config_write_iv_index(json_object *jobj, uint32_t idx, bool update)
+bool mesh_config_write_iv_index(void *cfg, uint32_t idx, bool update)
 {
+	json_object *jobj = cfg;
 	int tmp = update ? 1 : 0;
 
 	if (!jobj)
@@ -1415,8 +1449,10 @@ bool mesh_config_write_iv_index(json_object *jobj, uint32_t idx, bool update)
 	return true;
 }
 
-void mesh_config_remove_property(json_object *jobj, const char *desc)
+void mesh_config_remove_property(void *cfg, const char *desc)
 {
+	json_object *jobj = cfg;
+
 	if (jobj)
 		json_object_object_del(jobj, desc);
 }
@@ -1440,11 +1476,11 @@ static void add_model(void *a, void *b)
 }
 
 /* Add unprovisioned node (local) */
-bool mesh_config_add_node(json_object *jnode, struct mesh_config_node *node)
+bool mesh_config_add_node(void *cfg, struct mesh_config_node *node)
 {
 	struct mesh_config_modes *modes = &node->modes;
 	const struct l_queue_entry *entry;
-	json_object *jelems;
+	json_object *jelems, *jnode = cfg;
 
 	if (!jnode)
 		return false;
@@ -1562,10 +1598,9 @@ static void finish_key_refresh(json_object *jobj, uint16_t net_idx)
 
 }
 
-bool mesh_config_net_key_set_phase(json_object *jobj, uint16_t idx,
-								uint8_t phase)
+bool mesh_config_net_key_set_phase(void *cfg, uint16_t idx, uint8_t phase)
 {
-	json_object *jarray, *jentry = NULL;
+	json_object *jobj = cfg, *jarray, *jentry = NULL;
 
 	if (!jobj)
 		return false;
@@ -1588,11 +1623,10 @@ bool mesh_config_net_key_set_phase(json_object *jobj, uint16_t idx,
 	return true;
 }
 
-bool mesh_config_model_pub_add(json_object *jnode, uint16_t addr,
-					uint32_t mod_id, bool vendor,
-					struct mesh_config_pub *pub)
+bool mesh_config_model_pub_add(void *cfg, uint16_t addr, uint32_t mod_id,
+				bool vendor, struct mesh_config_pub *pub)
 {
-	json_object *jmodel, *jpub, *jretransmit;
+	json_object *jnode = cfg, *jmodel, *jpub, *jretransmit;
 	bool res;
 	int ele_idx;
 
@@ -1672,20 +1706,21 @@ static bool delete_model_property(json_object *jnode, uint16_t addr,
 	return true;
 }
 
-bool mesh_config_model_pub_del(json_object *jnode, uint16_t addr,
-						uint32_t mod_id, bool vendor)
+bool mesh_config_model_pub_del(void *cfg, uint16_t addr, uint32_t mod_id,
+								bool vendor)
 {
+	json_object *jnode = cfg;
+
 	if (!jnode)
 		return false;
 
 	return delete_model_property(jnode, addr, mod_id, vendor, "publish");
 }
 
-bool mesh_config_model_sub_add(json_object *jnode, uint16_t addr,
-						uint32_t mod_id, bool vendor,
-						struct mesh_config_sub *sub)
+bool mesh_config_model_sub_add(void *cfg, uint16_t addr, uint32_t mod_id,
+				bool vendor, struct mesh_config_sub *sub)
 {
-	json_object *jmodel, *jstring, *jarray = NULL;
+	json_object *jnode = cfg, *jmodel, *jstring, *jarray = NULL;
 	int ele_idx, len;
 	char buf[33];
 
@@ -1730,11 +1765,10 @@ bool mesh_config_model_sub_add(json_object *jnode, uint16_t addr,
 	return true;
 }
 
-bool mesh_config_model_sub_del(json_object *jnode, uint16_t addr,
-						uint32_t mod_id, bool vendor,
-						struct mesh_config_sub *sub)
+bool mesh_config_model_sub_del(void *cfg, uint16_t addr, uint32_t mod_id,
+				bool vendor, struct mesh_config_sub *sub)
 {
-	json_object *jmodel, *jarray, *jarray_new;
+	json_object *jnode = cfg, *jmodel, *jarray, *jarray_new;
 	char buf[33];
 	int len, ele_idx;
 
@@ -1783,9 +1817,11 @@ bool mesh_config_model_sub_del(json_object *jnode, uint16_t addr,
 	return true;
 }
 
-bool mesh_config_model_sub_del_all(json_object *jnode, uint16_t addr,
-						uint32_t mod_id, bool vendor)
+bool mesh_config_model_sub_del_all(void *cfg, uint16_t addr, uint32_t mod_id,
+								bool vendor)
 {
+	json_object *jnode = cfg;
+
 	if (!jnode)
 		return false;
 
mesh/mesh-config.h +48 -59

121 lines not on this page · raw message

Signature · 1 line
2.21.0
#5 lore raw

Re: [PATCH BlueZ 4/9] mesh: Generalize mesh-config APIs

Hi Inga,

Quoted 14 lines · Inga Stotland

This changes argument for each mesh-config API to use void* as a pointer to a node configuration object. This makes usage of JSON opaque for the rest of the code and allows to plug in a non-JSON configuration storage implementation.

---
 mesh/mesh-config-json.c | 228 +++++++++++++++++++++++-----------------
 mesh/mesh-config.h      | 107 +++++++++----------
 2 files changed, 180 insertions(+), 155 deletions(-)
mesh/mesh-config-json.c +0 -0
index 8fcb8afe3..5ca086ad0 100644

mesh/mesh-config-json.c static json_object *jarray_key_del(json_object *jarray, int16_t idx) · line 255

Quoted 5 lines
mesh/mesh-config-json.c +1 -1
@@ -252,10 +252,14 @@ 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)
+bool mesh_config_read_iv_index(void *cfg, uint32_t *idx, bool *update)

I'm not a fan of using void* for polymorphism. While there are places this makes sense (e.g. user_data pointers in callbacks), I think we should try to avoid this in our APIs.

Since the proposed patch assumes that we would switch backends during the build, and you can't have two different config formats compiled in at the same time, how about this:

mesh-config.h:

    union mesh_config;

    union mesh_config *mesh_config_create_config(void);
    void mesh_config_release_config(union mesh_config *config);

mesh-config-json.c:

    union mesh_config {
        json_object *json
    };

    union mesh_config *mesh_config_create_config(void)
    {
        return (union mesh_config*)json_object_new_object();
    }

    void mesh_config_release_config(union mesh_config *cfg)
    {
        json_object *jnode = (json_object *)cfg;

        if (!cfg)
            return;

        json_object_put(jnode);
    }

etc.

Signature · 3 lines
Michał Lowas-Rzechonek <[email protected]>
Silvair http://silvair.com
Jasnogórska 44, 31-358 Krakow, POLAND
#6 lore raw

Re: [PATCH BlueZ 4/9] mesh: Generalize mesh-config APIs

Quoted 17 lines · Michał Lowas-Rzechonek

mesh-config.h:

    union mesh_config;

    union mesh_config *mesh_config_create_config(void);
    void mesh_config_release_config(union mesh_config *config);

mesh-config-json.c:

    union mesh_config {
        json_object *json
    };

    union mesh_config *mesh_config_create_config(void)
    {
        return (union mesh_config*)json_object_new_object();
    }

    void mesh_config_release_config(union mesh_config *cfg)
Quoted 8 lines
    {
        json_object *jnode = (json_object *)cfg;

        if (!cfg)
            return;

        json_object_put(jnode);
    }

Or even simpler, since we don't even need to define the mesh_config:

mesh-config.h

    struct mesh_config;

    struct mesh_config *mesh_config_create_config(void);
    void mesh_config_release_config(struct mesh_config *config);

    struct mesh_config *mesh_config_create_config(void)
    {
        return (struct mesh_config*)json_object_new_object();
    }

    void mesh_config_release_config(struct mesh_config *cfg)
    {
        json_object *jnode = (json_object *)cfg;
    }
Signature · 3 lines
Michał Lowas-Rzechonek <[email protected]>
Silvair http://silvair.com
Jasnogórska 44, 31-358 Krakow, POLAND
#7 lore raw

Re: [PATCH BlueZ 4/9] mesh: Generalize mesh-config APIs

Hi Michal,

Quoted 30 lines · Michał Lowas-Rzechonek
Quoted 17 lines · Michał Lowas-Rzechonek

mesh-config.h:

    union mesh_config;

    union mesh_config *mesh_config_create_config(void);
    void mesh_config_release_config(union mesh_config *config);

mesh-config-json.c:

    union mesh_config {
        json_object *json
    };

    union mesh_config *mesh_config_create_config(void)
    {
        return (union mesh_config*)json_object_new_object();
    }

    void mesh_config_release_config(union mesh_config *cfg)
Quoted 8 lines
    {
        json_object *jnode = (json_object *)cfg;

        if (!cfg)
            return;

        json_object_put(jnode);
    }

Or even simpler, since we don't even need to define the mesh_config:

mesh-config.h

Quoted 15 lines
    struct mesh_config;

    struct mesh_config *mesh_config_create_config(void);
    void mesh_config_release_config(struct mesh_config *config);

    struct mesh_config *mesh_config_create_config(void)
    {
        return (struct mesh_config*)json_object_new_object();
    }

    void mesh_config_release_config(struct mesh_config *cfg)
    {
        json_object *jnode = (json_object *)cfg;
    }

I agree. Defining structure forward is better.
Inga

#8 lore raw

PATCH 5/9 mesh: Change variable prefix "jconfig" to "config"

In reply to Inga Stotland

This adjust variable naming by changing "jconfig" prefix to more generic "config. This also removes #include json-c/json.h for model.c, cfg-server.c and node.c as they no longer rely on the specifics of node configuration storage implementation.

---
 mesh/cfgmod-server.c | 16 +++++++---------
 mesh/model.c         |  1 -
 mesh/node.c          | 25 ++++++++++++-------------
 mesh/node.h          |  4 ++--
 mesh/storage.c       | 34 +++++++++++++++++-----------------
 5 files changed, 38 insertions(+), 42 deletions(-)
mesh/cfgmod-server.c +7 -9
index 0479a9185..1aa7c85da 100644
@@ -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,7 +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,
+		if (storage_set_transmit_params(node_config_get(node), count,
 								interval))
 			mesh_net_transmit_params_set(net, count, interval);
 		/* Fall Through */
mesh/model.c +0 -1
index 9331e1044..0f10727b4 100644
@@ -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 +12 -13
index 6ebdcf588..82d917337 100644
@@ -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"
@@ -81,7 +80,7 @@ struct mesh_node {
 	char *app_path;
 	char *owner;
 	char *path;
-	void *jconfig;
+	void *config;
 	char *node_path;
 	uint32_t disc_watch;
 	time_t upd_sec;
@@ -719,7 +718,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->config, proxy, "proxy");
 
 	if (res) {
 		node->proxy = proxy;
@@ -746,7 +745,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->config, beacon, "beacon");
 
 	if (res) {
 		node->beacon = beacon;
@@ -773,7 +772,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->config, friend, "friend");
 
 	if (res) {
 		node->friend = friend;
@@ -1431,16 +1430,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->config, "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->config, 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->config, dev_key))
 		return false;
 
 	node->primary = unicast;
@@ -1456,7 +1455,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->config, net_key_idx,
 							KEY_REFRESH_PHASE_TWO))
 			return false;
 	}
@@ -2016,14 +2015,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, void *config)
 {
-	node->jconfig = jconfig;
+	node->config = config;
 }
 
-void *node_jconfig_get(struct mesh_node *node)
+void *node_config_get(struct mesh_node *node)
 {
-	return node->jconfig;
+	return node->config;
 }
 
 void node_path_set(struct mesh_node *node, char *path)
mesh/node.h +2 -2
index 142527b30..055862a7f 100644
@@ -95,7 +95,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, void *config);
+void *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 +17 -17
index 601669791..e87b58c59 100644
@@ -169,7 +169,7 @@ static bool parse_config(char *in_file, char *out_dir, const uint8_t uuid[16])
 		node_remove(node);
 	}
 
-	node_jconfig_set(node, jnode);
+	node_config_set(node, jnode);
 	node_path_set(node, out_dir);
 
 done:
@@ -182,7 +182,7 @@ done:
 
 bool storage_set_ttl(struct mesh_node *node, uint8_t ttl)
 {
-	json_object *jnode = node_jconfig_get(node);
+	json_object *jnode = node_config_get(node);
 
 	if (!mesh_config_write_int(jnode, "defaultTTL", ttl))
 		return false;
@@ -194,7 +194,7 @@ 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)
 {
-	json_object *jnode = node_jconfig_get(node);
+	json_object *jnode = node_config_get(node);
 
 	if (!mesh_config_write_relay_mode(jnode, enable, count, interval))
 		return false;
@@ -206,7 +206,7 @@ bool storage_set_relay(struct mesh_node *node, bool enable,
 bool storage_set_transmit_params(struct mesh_node *node, uint8_t count,
 							uint8_t interval)
 {
-	json_object *jnode = node_jconfig_get(node);
+	json_object *jnode = node_config_get(node);
 
 	if (!mesh_config_write_net_transmit(jnode, count, interval))
 		return false;
@@ -218,7 +218,7 @@ bool storage_set_transmit_params(struct mesh_node *node, uint8_t count,
 bool storage_set_mode(struct mesh_node *node, uint8_t mode,
 						const char *mode_name)
 {
-	json_object *jnode = node_jconfig_get(node);
+	json_object *jnode = node_config_get(node);
 
 	if (!mesh_config_write_mode(jnode, mode_name, mode))
 		return false;
@@ -238,7 +238,7 @@ bool storage_model_bind(struct mesh_node *node, uint16_t addr, uint32_t mod_id,
 	if (ele_idx < 0)
 		return false;
 
-	jnode = node_jconfig_get(node);
+	jnode = node_config_get(node);
 
 	if (unbind)
 		stored = mesh_config_model_binding_del(jnode, ele_idx,
@@ -260,7 +260,7 @@ bool storage_app_key_add(struct mesh_net *net, uint16_t net_idx,
 	struct mesh_node *node = mesh_net_node_get(net);
 	bool stored;
 
-	jnode = node_jconfig_get(node);
+	jnode = node_config_get(node);
 	if (!jnode)
 		return false;
 
@@ -281,7 +281,7 @@ bool storage_app_key_del(struct mesh_net *net, uint16_t net_idx,
 	json_object *jnode;
 	struct mesh_node *node = mesh_net_node_get(net);
 
-	jnode = node_jconfig_get(node);
+	jnode = node_config_get(node);
 	if (!jnode)
 		return false;
 
@@ -296,7 +296,7 @@ bool storage_net_key_add(struct mesh_net *net, uint16_t net_idx,
 					const uint8_t key[16], bool update)
 {
 	struct mesh_node *node = mesh_net_node_get(net);
-	json_object *jnode = node_jconfig_get(node);
+	json_object *jnode = node_config_get(node);
 	bool stored;
 
 	if (!update)
@@ -313,7 +313,7 @@ bool storage_net_key_add(struct mesh_net *net, uint16_t net_idx,
 bool storage_net_key_del(struct mesh_net *net, uint16_t net_idx)
 {
 	struct mesh_node *node = mesh_net_node_get(net);
-	json_object *jnode = node_jconfig_get(node);
+	json_object *jnode = node_config_get(node);
 
 	if (!mesh_config_net_key_del(jnode, net_idx))
 		return false;
@@ -326,7 +326,7 @@ bool storage_set_iv_index(struct mesh_net *net, uint32_t iv_index,
 								bool update)
 {
 	struct mesh_node *node = mesh_net_node_get(net);
-	json_object *jnode = node_jconfig_get(node);
+	json_object *jnode = node_config_get(node);
 
 	if (!mesh_config_write_iv_index(jnode, iv_index, update))
 		return false;
@@ -339,7 +339,7 @@ bool storage_set_key_refresh_phase(struct mesh_net *net, uint16_t net_idx,
 								uint8_t phase)
 {
 	struct mesh_node *node = mesh_net_node_get(net);
-	json_object *jnode = node_jconfig_get(node);
+	json_object *jnode = node_config_get(node);
 
 	if (!mesh_config_net_key_set_phase(jnode, net_idx, phase))
 		return false;
@@ -351,7 +351,7 @@ bool storage_set_key_refresh_phase(struct mesh_net *net, uint16_t net_idx,
 bool storage_write_sequence_number(struct mesh_net *net, uint32_t seq)
 {
 	struct mesh_node *node = mesh_net_node_get(net);
-	json_object *jnode = node_jconfig_get(node);
+	json_object *jnode = node_config_get(node);
 
 	if (!mesh_config_write_int(jnode, "sequenceNumber", seq))
 		return false;
@@ -421,7 +421,7 @@ void storage_save_config(struct mesh_node *node, bool no_wait,
 	struct write_info *info;
 
 	info = l_new(struct write_info, 1);
-	info->jnode = node_jconfig_get(node);
+	info->jnode = node_config_get(node);
 	info->node_path = node_path_get(node);
 	info->cb = cb;
 	info->user_data = user_data;
@@ -557,7 +557,7 @@ bool storage_create_node_config(struct mesh_node *node, void *data)
 	if (!save_config(jnode, name_buf))
 		goto fail;
 
-	node_jconfig_set(node, jnode);
+	node_config_set(node, jnode);
 
 	return true;
 fail:
@@ -594,11 +594,11 @@ void storage_remove_node_config(struct mesh_node *node)
 		return;
 
 	/* Free the node config json object */
-	jnode = node_jconfig_get(node);
+	jnode = node_config_get(node);
 	if (jnode)
 		json_object_put(jnode);
 
-	node_jconfig_set(node, NULL);
+	node_config_set(node, NULL);
 
 	node_path = node_path_get(node);
 	l_debug("Delete node config %s", node_path);
Signature · 1 line
2.21.0
#9 lore raw

Re: [PATCH BlueZ 5/9] mesh: Change variable prefix "jconfig" to "config"

Inga,

Quoted 75 lines · Inga Stotland

This adjust variable naming by changing "jconfig" prefix to more generic "config. This also removes #include json-c/json.h for model.c, cfg-server.c and node.c as they no longer rely on the specifics of node configuration storage implementation.

---
 mesh/cfgmod-server.c | 16 +++++++---------
 mesh/model.c         |  1 -
 mesh/node.c          | 25 ++++++++++++-------------
 mesh/node.h          |  4 ++--
 mesh/storage.c       | 34 +++++++++++++++++-----------------
 5 files changed, 38 insertions(+), 42 deletions(-)
mesh/cfgmod-server.c +6 -8
index 0479a9185..1aa7c85da 100644
@@ -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;

mesh/cfgmod-server.c static bool cfg_srv_pkt(uint16_t src, uint32_t dst, · line 880

Quoted 6 lines
mesh/cfgmod-server.c +1 -1
@@ -878,7 +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,
+		if (storage_set_transmit_params(node_config_get(node), count,
 								interval))

Given the signature:

bool storage_set_transmit_params(struct mesh_node *node, uint8_t count,
uint8_t interval)

shouldn't that be:

if (storage_set_transmit_params(node, count, interval))

and in other places as well?

Signature · 3 lines
Michał Lowas-Rzechonek <[email protected]>
Silvair http://silvair.com
Jasnogórska 44, 31-358 Krakow, POLAND
#10 lore raw

PATCH 6/9 mesh: Define storage format specific read/write routines

In reply to Inga Stotland

This adds the following generic APIs to mesh-config.h

    void *mesh_config_create_config(void);
    void mesh_config_release_config(void *config);
    void *mesh_config_get_config(const char *dir);
    void *mesh_config_get_config_backup(const char *dir);
    bool mesh_config_restore_backup(const char *dir);
    bool mesh_config_save_config(const char *dir, void *cfg);

The implementation of these API routines depends on the underlying storage directory structure and can be specific to a chosen configuration file format.

---
 mesh/mesh-config.h | 7 +++++++
 1 file changed, 7 insertions(+)
mesh/mesh-config.h +7 -0
index ab101a331..077c59bbb 100644
@@ -100,6 +100,13 @@ typedef bool (*mesh_config_app_key_cb)(uint16_t idx, uint16_t net_idx,
 typedef bool (*mesh_config_node_cb)(struct mesh_config_node *node,
 							void *user_data);
 
+void *mesh_config_create_config(void);
+void mesh_config_release_config(void *config);
+void *mesh_config_get_config(const char *dir);
+void *mesh_config_get_config_backup(const char *dir);
+bool mesh_config_restore_backup(const char *dir);
+bool mesh_config_save_config(const char *dir, void *cfg);
+
 bool mesh_config_read_node(void *cfg, mesh_config_node_cb cb, void *user_data);
 bool mesh_config_add_node(void *cfg, struct mesh_config_node *node);
 bool mesh_config_read_iv_index(void *cfg, uint32_t *idx, bool *update);
Signature · 1 line
2.21.0
#11 lore raw

Re: [PATCH BlueZ 6/9] mesh: Define storage format specific read/write routines

Quoted 11 lines · Inga Stotland

This adds the following generic APIs to mesh-config.h

    void *mesh_config_create_config(void);
    void mesh_config_release_config(void *config);
    void *mesh_config_get_config(const char *dir);
    void *mesh_config_get_config_backup(const char *dir);
    bool mesh_config_restore_backup(const char *dir);
    bool mesh_config_save_config(const char *dir, void *cfg);

The implementation of these API routines depends on the underlying storage directory structure and can be specific to a chosen configuration file format.

I don't like the assumption that each node is stored in a separate file, and there needs to be a backup file.

One of the storage formats I had in mind is a single transactional database (some flavor of berkeley db, or maybe even sqlite) that would hold all the nodes.

With this in mind, how about:

    union mesh_config *mesh_config_create_config(const uint8_t uuid[16]);
    bool mesh_config_save(union mesh_config *cfg);

and to iterate over saved nodes:

    typedef struct mesh_node *(*mesh_node_load_cb)(union mesh_config *cfg,
                                                    const uint8_t uuid[16],
                                                    void *user_data);

    void mesh_config_load_nodes(mesh_node_load_cb cb, void *user_data);

and move file/directory handling from storage.c to mesh-config-json.c?

regards

Signature · 3 lines
Michał Lowas-Rzechonek <[email protected]>
Silvair http://silvair.com
Jasnogórska 44, 31-358 Krakow, POLAND
#12 lore raw

Re: [PATCH BlueZ 6/9] mesh: Define storage format specific read/write routines

Hi Michal,

Quoted 35 lines · Michał Lowas-Rzechonek
Quoted 11 lines · Inga Stotland

This adds the following generic APIs to mesh-config.h

    void *mesh_config_create_config(void);
    void mesh_config_release_config(void *config);
    void *mesh_config_get_config(const char *dir);
    void *mesh_config_get_config_backup(const char *dir);
    bool mesh_config_restore_backup(const char *dir);
    bool mesh_config_save_config(const char *dir, void *cfg);

The implementation of these API routines depends on the underlying storage directory structure and can be specific to a chosen configuration file format.

I don't like the assumption that each node is stored in a separate file,
and there needs to be a backup file.

One of the storage formats I had in mind is a single transactional database (some flavor of berkeley db, or maybe even sqlite) that would
hold all the nodes.

With this in mind, how about:

union mesh_config *mesh_config_create_config(const uint8_t

uuid[16]);
    bool mesh_config_save(union mesh_config *cfg);

and to iterate over saved nodes:
typedef struct mesh_node *(*mesh_node_load_cb)(union mesh_config
*cfg,
const uint8_t
uuid[16],
void *user_data);

Quoted 8 lines

void mesh_config_load_nodes(mesh_node_load_cb cb, void
*user_data);

and move file/directory handling from storage.c to mesh-config- json.c?

regards

If we do away with the notion that each node has its own directory, this means that entire node parsing procedure needs to be pushed into the corresponding mesh-config-<specific> implementation and storage.c becomes obsolete.

#13 lore raw

Re: [PATCH BlueZ 6/9] mesh: Define storage format specific read/write routines

Hi Inga,

Quoted 4 lines · Inga

If we do away with the notion that each node has its own directory, this means that entire node parsing procedure needs to be pushed into the corresponding mesh-config-<specific> implementation and storage.c becomes obsolete.

I know. Isn't that neat? ;)

Signature · 3 lines
Michał Lowas-Rzechonek <[email protected]>
Silvair http://silvair.com
Jasnogórska 44, 31-358 Krakow, POLAND
#14 lore raw

RE: [PATCH BlueZ 6/9] mesh: Define storage format specific read/write routines

In reply to Michał Lowas-Rzechonek

Hi Michał,

Quoted 18 lines

-----Original Message-----
From: Michał Lowas-Rzechonek [mailto:[email protected]]
Sent: Wednesday, July 10, 2019 12:53 AM
To: Stotland, Inga <[email protected]>
Cc: [email protected]; Gix, Brian <[email protected]>; [email protected]
Subject: Re: [PATCH BlueZ 6/9] mesh: Define storage format specific read/write routines

Quoted 8 lines · Inga Stotland

This adds the following generic APIs to mesh-config.h

    void *mesh_config_create_config(void);
    void mesh_config_release_config(void *config);
    void *mesh_config_get_config(const char *dir);
    void *mesh_config_get_config_backup(const char *dir);
    bool mesh_config_restore_backup(const char *dir);
    bool mesh_config_save_config(const char *dir, void *cfg);
Quoted 6 lines
Quoted 3 lines

The implementation of these API routines depends on the underlying storage directory structure and can be specific to a chosen configuration file format.

I don't like the assumption that each node is stored in a separate file, and there needs to be a backup file.

I think I understand what you are saying about *How* the node data is persevered, and that the underlying storage might not be a typical file system.

We *do* need a mechanism to get to an earlier version of a Node if there is corruption detected in the most resent version (perhaps an unexpected loss of power during a write operation). That is the purpose of the current backup system. Whether this is accomplished by reading a backup file or "Rolling Back" the history on a journal (or git repo, or whatever) we do need that backup.

But we could certainly hide the mechanism of backing up (and of reverting to a backed up version).

Quoted 4 lines

One of the storage formats I had in mind is a single transactional database (some flavor of berkeley db, or maybe even sqlite) that would hold all the nodes.

#15 lore raw

Re: [PATCH BlueZ 6/9] mesh: Define storage format specific read/write routines

Hi Brian,

Quoted 6 lines · Brian
Quoted 2 lines

I don't like the assumption that each node is stored in a separate file, and there needs to be a backup file.

I think I understand what you are saying about *How* the node data is persevered, and that the underlying storage might not be a typical file system.

Precisely.

Quoted 6 lines

We *do* need a mechanism to get to an earlier version of a Node if there is corruption detected in the most resent version (perhaps an unexpected loss of power during a write operation). That is the purpose of the current backup system. Whether this is accomplished by reading a backup file or "Rolling Back" the history on a journal (or git repo, or whatever) we do need that backup.

Yes, I understand that. But, as you noticed, the rollback mechanism doesn't need to be a backup *file*. This is an implementation detail of a filesystem-backed JSON storage.

I would even go as far as saying that we don't need a backup - we need atomicity and durability from ACID. Achieving that is non-trivial, but is still an implementation detail.

Quoted 2 lines

But we could certainly hide the mechanism of backing up (and of reverting to a backed up version).

Yes, this.

Signature · 3 lines
Michał Lowas-Rzechonek <[email protected]>
Silvair http://silvair.com
Jasnogórska 44, 31-358 Krakow, POLAND
#16 lore raw

PATCH 7/9 mesh: Implement config read/write for mesh json format

In reply to Inga Stotland

This adds implementation of mesh_config_create_config(), mesh_config_release_config(), mesh_config_get_config(), mesh_config_get_config_backup(), mesh_config_restore_backup() and mesh_config_save_config() for the JSON based node configuration storage.

---
 mesh/mesh-config-json.c | 171 ++++++++++++++++++++++++++++++++++++++++
 1 file changed, 171 insertions(+)
mesh/mesh-config-json.c +171 -0
index 5ca086ad0..3c65b3846 100644
@@ -22,9 +22,14 @@
 #endif
 
 #define _GNU_SOURCE
+
+#include <dirent.h>
 #include <errno.h>
+#include <fcntl.h>
+#include <ftw.h>
 #include <stdio.h>
 #include <string.h>
+#include <unistd.h>
 
 #include <ell/ell.h>
 #include <json-c/json.h>
@@ -35,6 +40,10 @@
 
 #define CHECK_KEY_IDX_RANGE(x) (((x) >= 0) && ((x) <= 4095))
 
+static const char *cfg_name = "/node.json";
+static const char *bak_name = "/node.json.bak";
+static const char *tmp_name = "/node.json.tmp";
+
 static bool get_int(json_object *jobj, const char *keyword, int *value)
 {
 	json_object *jvalue;
@@ -1827,3 +1836,165 @@ bool mesh_config_model_sub_del_all(void *cfg, uint16_t addr, uint32_t mod_id,
 
 	return delete_model_property(jnode, addr, mod_id, vendor, "subscribe");
 }
+
+static char *create_filename(const char *dir, const char *fname)
+{
+	size_t path_len = strlen(dir) + strlen(fname);
+
+	if (path_len + 1 >= PATH_MAX)
+		return NULL;
+
+	return l_strdup_printf("%s%s", dir, fname);
+}
+
+static void *get_node_config(const char *dir, const char *fname)
+{
+	int fd;
+	char *str = NULL, *fname_full;
+	struct stat st;
+	ssize_t sz;
+	json_object *jconfig = NULL;
+
+	fname_full = create_filename(dir, fname);
+	if (!fname)
+		return NULL;
+
+	l_info("Loading configuration from %s", fname_full);
+
+	fd = open(fname_full, O_RDONLY);
+	if (fd < 0)
+		goto done;
+
+	if (fstat(fd, &st) == -1)
+		goto done;
+
+	str = (char *) l_new(char, st.st_size + 1);
+	if (!str)
+		goto done;
+
+	sz = read(fd, str, st.st_size);
+	if (sz != st.st_size) {
+		l_error("Failed to read configuration file %s", fname_full);
+		goto done;
+	}
+
+	jconfig = json_tokener_parse(str);
+
+done:
+	if (fd >= 0)
+		close(fd);
+
+	l_free(str);
+	l_free(fname_full);
+
+	return jconfig;
+}
+
+void *mesh_config_get_config(const char *dir)
+{
+	if (!dir)
+		return NULL;
+
+	return get_node_config(dir, cfg_name);
+}
+
+void *mesh_config_get_config_backup(const char *dir)
+{
+	if (!dir)
+		return NULL;
+
+	return get_node_config(cfg_name, bak_name);
+}
+
+bool mesh_config_restore_backup(const char *dir)
+{
+	char *fname_cfg = NULL, *fname_bak = NULL;
+	bool res = false;
+
+	fname_cfg = create_filename(dir, cfg_name);
+	fname_bak = create_filename(dir, bak_name);
+
+	if (fname_cfg && fname_bak) {
+		remove(fname_cfg);
+		rename(fname_bak, fname_cfg);
+		res = true;
+	}
+
+	l_free(fname_bak);
+	l_free(fname_cfg);
+
+	return res;
+}
+
+static bool save_config(void *cfg, const char *fname)
+{
+	FILE *outfile;
+	const char *str;
+	bool result = false;
+	json_object *jnode = cfg;
+
+	outfile = fopen(fname, "w");
+
+	if (!outfile)
+		return false;
+
+	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");
+	else
+		result = true;
+
+	fclose(outfile);
+
+	return result;
+}
+
+void *mesh_config_create_config(void)
+{
+	return json_object_new_object();
+}
+
+void mesh_config_release_config(void *cfg)
+{
+	json_object *jnode = cfg;
+
+	if (!cfg)
+		return;
+
+	json_object_put(jnode);
+}
+
+bool mesh_config_save_config(const char *dir, void *cfg)
+{
+	char *fname_cfg;
+	char *fname_bak;
+	char *fname_tmp;
+	bool result = false;
+
+	fname_cfg = create_filename(dir, cfg_name);
+	fname_bak = create_filename(dir, bak_name);
+	fname_tmp = create_filename(dir, tmp_name);
+
+	if (!fname_cfg || !fname_bak || !fname_tmp)
+		goto done;
+
+	result = save_config(cfg, fname_tmp);
+
+	if (result) {
+		remove(fname_bak);
+		rename(fname_cfg, fname_bak);
+		rename(fname_tmp, fname_cfg);
+	} else {
+		l_error("Failed to save configuration to %s", fname_tmp);
+	}
+
+	remove(fname_tmp);
+
+done:
+	l_free(fname_cfg);
+	l_free(fname_bak);
+	l_free(fname_tmp);
+
+	return result;
+}
Signature · 1 line
2.21.0
#17 lore raw

PATCH 8/9 mesh: Switch to using mesh-config routines for storage

In reply to Inga Stotland

This removes the assumptions of the layout of the node configuration directory from storage.c to allow read/write of persistent node configuration to be format agnostic.

---
 mesh/mesh.c    |  11 ++-
 mesh/storage.c | 229 +++++++++++++++++--------------------------------
 2 files changed, 87 insertions(+), 153 deletions(-)
mesh/mesh.c +8 -3
index 26acfd4dc..bdd79e6bb 100644
@@ -22,6 +22,8 @@
 #endif
 
 #define _GNU_SOURCE
+#include <dirent.h>
+
 #include <ell/ell.h>
 
 #include "mesh/mesh-io.h"
@@ -143,6 +145,12 @@ bool mesh_init(const char *config_dir, enum mesh_io_type type, void *opts)
 	if (mesh.io)
 		return true;
 
+	if (!config_dir)
+		config_dir = MESH_STORAGEDIR;
+
+	if (strlen(config_dir) >= PATH_MAX)
+		return false;
+
 	mesh_model_init();
 	mesh_agent_init();
 
@@ -150,9 +158,6 @@ 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;
-
 	l_info("Loading node configuration from %s", config_dir);
 
 	if (!storage_load_nodes(config_dir))
mesh/storage.c +79 -150

304 lines not on this page · raw message

Signature · 1 line
2.21.0
#18 lore raw

PATCH 9/9 mesh: Make storage.c json-c agnostic

In reply to Inga Stotland

This removes all the references to json-c constructs from storage.c All the storage operations are now interfacing through mesh-config APIs.

---
 mesh/storage.c | 122 ++++++++++++++++++++++++-------------------------
 1 file changed, 61 insertions(+), 61 deletions(-)
mesh/storage.c +61 -61
index 721b65904..a4131404e 100644
@@ -29,7 +29,6 @@
 #include <libgen.h>
 #include <ftw.h>
 
-#include <json-c/json.h>
 #include <ell/ell.h>
 
 #include "mesh/mesh-defs.h"
@@ -41,8 +40,8 @@
 #include "mesh/storage.h"
 
 struct write_info {
-	json_object *jnode;
-	const char *node_path;
+	void *node_cfg;
+	const char *node_dir;
 	void *user_data;
 	mesh_status_func_t cb;
 };
@@ -83,7 +82,7 @@ static bool read_app_keys_cb(uint16_t net_idx, uint16_t app_idx, uint8_t *key,
 	return appkey_key_init(net, net_idx, app_idx, key, new_key);
 }
 
-static bool parse_node(struct mesh_node *node, json_object *jnode)
+static bool parse_node(struct mesh_node *node, void *node_cfg)
 {
 	bool bvalue;
 	uint32_t iv_index;
@@ -92,30 +91,30 @@ static bool parse_node(struct mesh_node *node, json_object *jnode)
 	uint16_t interval;
 	struct mesh_net *net = node_get_net(node);
 
-	if (mesh_config_read_iv_index(jnode, &iv_index, &bvalue))
+	if (mesh_config_read_iv_index(node_cfg, &iv_index, &bvalue))
 		mesh_net_set_iv_index(net, iv_index, bvalue);
 
-	if (mesh_config_read_net_transmit(jnode, &cnt, &interval))
+	if (mesh_config_read_net_transmit(node_cfg, &cnt, &interval))
 		mesh_net_transmit_params_set(net, cnt, interval);
 
 	/* Node composition/configuration info */
-	if (!mesh_config_read_node(jnode, read_node_cb, node))
+	if (!mesh_config_read_node(node_cfg, read_node_cb, node))
 		return false;
 
-	if (!mesh_config_read_net_keys(jnode, read_net_keys_cb, net))
+	if (!mesh_config_read_net_keys(node_cfg, read_net_keys_cb, net))
 		return false;
 
-	if (!mesh_config_read_token(jnode, key_buf))
+	if (!mesh_config_read_token(node_cfg, key_buf))
 		return false;
 
 	node_set_token(node, key_buf);
 
-	if (!mesh_config_read_device_key(jnode, key_buf))
+	if (!mesh_config_read_device_key(node_cfg, key_buf))
 		return false;
 
 	node_set_device_key(node, key_buf);
 
-	mesh_config_read_app_keys(jnode, read_app_keys_cb, net);
+	mesh_config_read_app_keys(node_cfg, read_app_keys_cb, net);
 
 	return true;
 }
@@ -139,9 +138,9 @@ static struct mesh_node *parse_config(void *node_cfg,  const uint8_t uuid[16])
 
 bool storage_set_ttl(struct mesh_node *node, uint8_t ttl)
 {
-	json_object *jnode = node_config_get(node);
+	void *node_cfg = node_config_get(node);
 
-	if (!mesh_config_write_int(jnode, "defaultTTL", ttl))
+	if (!mesh_config_write_int(node_cfg, "defaultTTL", ttl))
 		return false;
 
 	storage_save_config(node, true, NULL, NULL);
@@ -151,9 +150,9 @@ 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)
 {
-	json_object *jnode = node_config_get(node);
+	void *node_cfg = node_config_get(node);
 
-	if (!mesh_config_write_relay_mode(jnode, enable, count, interval))
+	if (!mesh_config_write_relay_mode(node_cfg, enable, count, interval))
 		return false;
 
 	storage_save_config(node, true, NULL, NULL);
@@ -163,9 +162,9 @@ bool storage_set_relay(struct mesh_node *node, bool enable,
 bool storage_set_transmit_params(struct mesh_node *node, uint8_t count,
 							uint8_t interval)
 {
-	json_object *jnode = node_config_get(node);
+	void *node_cfg = node_config_get(node);
 
-	if (!mesh_config_write_net_transmit(jnode, count, interval))
+	if (!mesh_config_write_net_transmit(node_cfg, count, interval))
 		return false;
 
 	storage_save_config(node, true, NULL, NULL);
@@ -175,9 +174,9 @@ bool storage_set_transmit_params(struct mesh_node *node, uint8_t count,
 bool storage_set_mode(struct mesh_node *node, uint8_t mode,
 						const char *mode_name)
 {
-	json_object *jnode = node_config_get(node);
+	void *node_cfg = node_config_get(node);
 
-	if (!mesh_config_write_mode(jnode, mode_name, mode))
+	if (!mesh_config_write_mode(node_cfg, mode_name, mode))
 		return false;
 
 	storage_save_config(node, true, NULL, NULL);
@@ -187,7 +186,7 @@ bool storage_set_mode(struct mesh_node *node, uint8_t mode,
 bool storage_model_bind(struct mesh_node *node, uint16_t addr, uint32_t mod_id,
 				uint16_t app_idx, bool unbind)
 {
-	json_object *jnode;
+	void *node_cfg;
 	int ele_idx;
 	bool stored, is_vendor = (mod_id > 0xffff);
 
@@ -195,13 +194,13 @@ bool storage_model_bind(struct mesh_node *node, uint16_t addr, uint32_t mod_id,
 	if (ele_idx < 0)
 		return false;
 
-	jnode = node_config_get(node);
+	node_cfg = node_config_get(node);
 
 	if (unbind)
-		stored = mesh_config_model_binding_del(jnode, ele_idx,
+		stored = mesh_config_model_binding_del(node_cfg, ele_idx,
 						is_vendor, mod_id, app_idx);
 	else
-		stored = mesh_config_model_binding_add(jnode, ele_idx,
+		stored = mesh_config_model_binding_add(node_cfg, ele_idx,
 						is_vendor, mod_id, app_idx);
 
 	if (stored)
@@ -213,18 +212,19 @@ bool storage_model_bind(struct mesh_node *node, uint16_t addr, uint32_t mod_id,
 bool storage_app_key_add(struct mesh_net *net, uint16_t net_idx,
 			uint16_t app_idx, const uint8_t key[16], bool update)
 {
-	json_object *jnode;
+	void *node_cfg;
 	struct mesh_node *node = mesh_net_node_get(net);
 	bool stored;
 
-	jnode = node_config_get(node);
-	if (!jnode)
+	node_cfg = node_config_get(node);
+	if (!node_cfg)
 		return false;
 
 	if (update)
-		stored = mesh_config_app_key_update(jnode, app_idx, key);
+		stored = mesh_config_app_key_update(node_cfg, app_idx, key);
 	else
-		stored = mesh_config_app_key_add(jnode, net_idx, app_idx, key);
+		stored = mesh_config_app_key_add(node_cfg, net_idx, app_idx,
+									key);
 
 	if (stored)
 		storage_save_config(node, true, NULL, NULL);
@@ -235,14 +235,14 @@ bool storage_app_key_add(struct mesh_net *net, uint16_t net_idx,
 bool storage_app_key_del(struct mesh_net *net, uint16_t net_idx,
 					uint16_t app_idx)
 {
-	json_object *jnode;
+	void *node_cfg;
 	struct mesh_node *node = mesh_net_node_get(net);
 
-	jnode = node_config_get(node);
-	if (!jnode)
+	node_cfg = node_config_get(node);
+	if (!node_cfg)
 		return false;
 
-	if (!mesh_config_app_key_del(jnode, net_idx, app_idx))
+	if (!mesh_config_app_key_del(node_cfg, net_idx, app_idx))
 		return false;
 
 	storage_save_config(node, true, NULL, NULL);
@@ -253,13 +253,13 @@ bool storage_net_key_add(struct mesh_net *net, uint16_t net_idx,
 					const uint8_t key[16], bool update)
 {
 	struct mesh_node *node = mesh_net_node_get(net);
-	json_object *jnode = node_config_get(node);
+	void *node_cfg = node_config_get(node);
 	bool stored;
 
 	if (!update)
-		stored = mesh_config_net_key_add(jnode, net_idx, key);
+		stored = mesh_config_net_key_add(node_cfg, net_idx, key);
 	else
-		stored = mesh_config_net_key_update(jnode, net_idx, key);
+		stored = mesh_config_net_key_update(node_cfg, net_idx, key);
 
 	if (stored)
 		storage_save_config(node, true, NULL, NULL);
@@ -270,9 +270,9 @@ bool storage_net_key_add(struct mesh_net *net, uint16_t net_idx,
 bool storage_net_key_del(struct mesh_net *net, uint16_t net_idx)
 {
 	struct mesh_node *node = mesh_net_node_get(net);
-	json_object *jnode = node_config_get(node);
+	void *node_cfg = node_config_get(node);
 
-	if (!mesh_config_net_key_del(jnode, net_idx))
+	if (!mesh_config_net_key_del(node_cfg, net_idx))
 		return false;
 
 	storage_save_config(node, true, NULL, NULL);
@@ -283,9 +283,9 @@ bool storage_set_iv_index(struct mesh_net *net, uint32_t iv_index,
 								bool update)
 {
 	struct mesh_node *node = mesh_net_node_get(net);
-	json_object *jnode = node_config_get(node);
+	void *node_cfg = node_config_get(node);
 
-	if (!mesh_config_write_iv_index(jnode, iv_index, update))
+	if (!mesh_config_write_iv_index(node_cfg, iv_index, update))
 		return false;
 
 	storage_save_config(node, true, NULL, NULL);
@@ -296,9 +296,9 @@ bool storage_set_key_refresh_phase(struct mesh_net *net, uint16_t net_idx,
 								uint8_t phase)
 {
 	struct mesh_node *node = mesh_net_node_get(net);
-	json_object *jnode = node_config_get(node);
+	void *node_cfg = node_config_get(node);
 
-	if (!mesh_config_net_key_set_phase(jnode, net_idx, phase))
+	if (!mesh_config_net_key_set_phase(node_cfg, net_idx, phase))
 		return false;
 
 	storage_save_config(node, true, NULL, NULL);
@@ -308,9 +308,9 @@ bool storage_set_key_refresh_phase(struct mesh_net *net, uint16_t net_idx,
 bool storage_write_sequence_number(struct mesh_net *net, uint32_t seq)
 {
 	struct mesh_node *node = mesh_net_node_get(net);
-	json_object *jnode = node_config_get(node);
+	void *node_cfg = node_config_get(node);
 
-	if (!mesh_config_write_int(jnode, "sequenceNumber", seq))
+	if (!mesh_config_write_int(node_cfg, "sequenceNumber", seq))
 		return false;
 
 	storage_save_config(node, false, NULL, NULL);
@@ -323,7 +323,7 @@ static void idle_save_config(void *user_data)
 	bool result;
 
 	l_debug("Storage-Wrote");
-	result = mesh_config_save_config(info->node_path, info->jnode);
+	result = mesh_config_save_config(info->node_dir, info->node_cfg);
 
 	if (info->cb)
 		info->cb(info->user_data, result);
@@ -337,8 +337,8 @@ void storage_save_config(struct mesh_node *node, bool no_wait,
 	struct write_info *info;
 
 	info = l_new(struct write_info, 1);
-	info->jnode = node_config_get(node);
-	info->node_path = node_path_get(node);
+	info->node_cfg = node_config_get(node);
+	info->node_dir = node_path_get(node);
 	info->cb = cb;
 	info->user_data = user_data;
 
@@ -471,33 +471,33 @@ static int del_fobject(const char *fpath, const struct stat *sb, int typeflag,
 /* Permanently remove node configuration */
 void storage_remove_node_config(struct mesh_node *node)
 {
-	char *node_path, *node_name;
+	char *node_dir, *node_name;
 	char uuid[33];
-	struct json_object *jnode;
+	void *node_cfg;
 
 	if (!node)
 		return;
 
 	/* Free the node config json object */
-	jnode = node_config_get(node);
-	if (jnode)
-		mesh_config_release_config(jnode);
+	node_cfg = node_config_get(node);
+	if (node_cfg)
+		mesh_config_release_config(node_cfg);
 
 	node_config_set(node, NULL);
 
-	node_path = node_path_get(node);
-	l_debug("Delete node config %s", node_path);
+	node_dir = node_path_get(node);
+	l_debug("Delete node config %s", node_dir);
 
 	/* Make sure path name of node follows expected guidelines */
 	if (!hex2str(node_uuid_get(node), 16, uuid, sizeof(uuid)))
 		return;
 
-	node_name = basename(node_path);
+	node_name = basename(node_dir);
 
 	if (strcmp(node_name, uuid))
 		return;
 
-	nftw(node_path, del_fobject, 5, FTW_DEPTH | FTW_PHYS);
+	nftw(node_dir, del_fobject, 5, FTW_DEPTH | FTW_PHYS);
 }
 
 bool storage_create_node_config(struct mesh_node *node, void *data)
@@ -505,14 +505,14 @@ bool storage_create_node_config(struct mesh_node *node, void *data)
 	struct mesh_config_node *db_node = data;
 	char uuid[33];
 	char node_dir[PATH_MAX];
-	json_object *jnode;
+	void *node_cfg;
 
 	if (!storage_dir)
 		return false;
 
-	jnode = mesh_config_create_config();
+	node_cfg = mesh_config_create_config();
 
-	if (!jnode || !mesh_config_add_node(jnode, db_node))
+	if (!node_cfg || !mesh_config_add_node(node_cfg, db_node))
 		return false;
 
 	if (!hex2str(node_uuid_get(node), 16, uuid, sizeof(uuid)))
@@ -527,18 +527,18 @@ bool storage_create_node_config(struct mesh_node *node, void *data)
 	if (mkdir(node_dir, 0755) != 0)
 		goto fail;
 
-	if (!mesh_config_save_config(node_dir, jnode)) {
+	if (!mesh_config_save_config(node_dir, node_cfg)) {
 		nftw(node_dir, del_fobject, 5, FTW_DEPTH | FTW_PHYS);
 		goto fail;
 	}
 
 	l_debug("New node config %s", node_dir);
 	node_path_set(node, node_dir);
-	node_config_set(node, jnode);
+	node_config_set(node, node_cfg);
 
 	return true;
 fail:
-	mesh_config_release_config(jnode);
+	mesh_config_release_config(node_cfg);
 	node_config_set(node, NULL);
 
 	return false;
Signature · 1 line
2.21.0