linux-bluetooth.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH BlueZ 0/3] Json storage refactor
@ 2019-06-05 13:01 Jakub Witowski
  2019-06-05 13:01 ` [PATCH BlueZ 1/3] mesh: Refactor elements related object in json file Jakub Witowski
                   ` (3 more replies)
  0 siblings, 4 replies; 6+ messages in thread
From: Jakub Witowski @ 2019-06-05 13:01 UTC (permalink / raw)
  To: linux-bluetooth

Netwrk keys, application keys and elements with models have been simplified in json storage file. Arrays objects are no longer required.

Jakub Witowski (3):
  mesh: Refactor elements related object in json file
  mesh: Refactor net keys related object in json file
  mesh: Refactor app key related object in json file

 mesh/mesh-db.c | 469 ++++++++++++++++++++++++++-----------------------
 1 file changed, 248 insertions(+), 221 deletions(-)

-- 
2.20.1


^ permalink raw reply	[flat|nested] 6+ messages in thread

* [PATCH BlueZ 1/3] mesh: Refactor elements related object in json file
  2019-06-05 13:01 [PATCH BlueZ 0/3] Json storage refactor Jakub Witowski
@ 2019-06-05 13:01 ` Jakub Witowski
  2019-06-05 13:01 ` [PATCH BlueZ 2/3] mesh: Refactor net keys " Jakub Witowski
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 6+ messages in thread
From: Jakub Witowski @ 2019-06-05 13:01 UTC (permalink / raw)
  To: linux-bluetooth; +Cc: Inga Stotland, Brian Gix

Elements and models have been simplified in json storage file.
The array object is no longer used.
---
 mesh/mesh-db.c | 134 ++++++++++++++++++++++++++++++-------------------
 1 file changed, 81 insertions(+), 53 deletions(-)

diff --git a/mesh/mesh-db.c b/mesh/mesh-db.c
index 5b2868fdb..7bc1ba272 100644
--- a/mesh/mesh-db.c
+++ b/mesh/mesh-db.c
@@ -35,6 +35,7 @@
 #include "mesh/mesh-db.h"
 
 #define CHECK_KEY_IDX_RANGE(x) (((x) >= 0) && ((x) <= 4095))
+#define NUMBER_BASE_DECIMAL    (10)
 
 static bool get_int(json_object *jobj, const char *keyword, int *value)
 {
@@ -994,53 +995,69 @@ fail:
 
 static bool parse_elements(json_object *jelements, struct mesh_db_node *node)
 {
-	int i, num_ele;
-
-	num_ele = json_object_array_length(jelements);
-	if (!num_ele)
-		/* Allow "empty" nodes */
-		return true;
+	struct json_object_iterator iter, end;
+	json_object *jtemp;
+	const char *jidx;
 
 	node->elements = l_queue_new();
+
 	if (!node->elements)
 		return false;
 
-	for (i = 0; i < num_ele; ++i) {
-		json_object *jelement;
-		json_object *jmodels;
-		json_object *jvalue;
+	if (json_object_get_type(jelements) != json_type_object)
+		return false;
+
+	iter = json_object_iter_begin(jelements);
+	end = json_object_iter_end(jelements);
+
+	while (!json_object_iter_equal(&iter, &end)) {
+
+		json_object *jmodels, *jlocation;
 		struct mesh_db_element *ele;
-		int index;
-		char *str;
+		char *str_end, *str;
+		uint8_t idx;
 
-		jelement = json_object_array_get_idx(jelements, i);
-		if (!jelement)
-			goto fail;
+		jidx = json_object_iter_peek_name(&iter);
+		jtemp = json_object_iter_peek_value(&iter);
 
-		if (!get_int(jelement, "elementIndex", &index) ||
-								index > num_ele)
-			goto fail;
+		if (!*jidx)
+			return false;
+
+		idx = (uint8_t)strtol(jidx, &str_end, NUMBER_BASE_DECIMAL);
+
+		if (*str_end)
+			return false;
+
+		if (json_object_get_type(jtemp) != json_type_object)
+			return false;
 
 		ele = l_new(struct mesh_db_element, 1);
-		ele->index = index;
+		if (!ele)
+			goto fail;
+
+		ele->index = idx;
 		ele->models = l_queue_new();
 		if (!ele->models)
 			goto fail;
 
-		if (!json_object_object_get_ex(jelement, "location", &jvalue))
+		if (!json_object_object_get_ex(jtemp, "location", &jlocation))
 			goto fail;
 
-		str = (char *)json_object_get_string(jvalue);
+		str = (char *)json_object_get_string(jlocation);
 		if (sscanf(str, "%04hx", &(ele->location)) != 1)
 			goto fail;
 
-		if (json_object_object_get_ex(jelement, "models", &jmodels)) {
-			if (json_object_get_type(jmodels) != json_type_array ||
-						!parse_models(jmodels, ele))
+		if (json_object_object_get_ex(jtemp, "models", &jmodels)) {
+
+			if (json_object_get_type(jmodels) != json_type_object)
+				goto fail;
+
+			if (!parse_models(jmodels, ele))
 				goto fail;
 		}
 
 		l_queue_push_tail(node->elements, ele);
+		json_object_iter_next(&iter);
 	}
 
 	return true;
@@ -1209,12 +1226,16 @@ bool mesh_db_read_node(json_object *jnode, mesh_db_node_cb cb, void *user_data)
 	if (json_object_object_get_ex(jnode, "sequenceNumber", &jvalue))
 		node.seq_number = json_object_get_int(jvalue);
 
-	if (json_object_object_get_ex(jnode, "elements", &jvalue)) {
-		if (json_object_get_type(jvalue) == json_type_array) {
-			if (!parse_elements(jvalue, &node))
-				return false;
+	json_object_object_get_ex(jnode, "elements", &jvalue);
+
+	if (jvalue && json_object_get_type(jvalue) == json_type_object) {
+
+		if (!parse_elements(jvalue, &node)) {
+			l_info("Failed to parse elements");
+			return false;
 		}
-	}
+	} else
+		l_info("Failed to parse elements: wrong JSON object type");
 
 	return cb(&node, user_data);
 }
@@ -1427,24 +1448,28 @@ void mesh_db_remove_property(json_object *jobj, const char *desc)
 static void add_model(void *a, void *b)
 {
 	struct mesh_db_model *mod = a;
-	json_object *jmodels = b, *jmodel;
-
-	jmodel = json_object_new_object();
-	if (!jmodel)
-		return;
+	json_object *jmodels = b;
+	json_object *jmodel;
+	char buf[9];
 
 	if (!mod->vendor)
-		mesh_db_write_uint16_hex(jmodel, "modelId",
-						(uint16_t) mod->id);
+		snprintf(buf, 5, "%4.4x", (uint16_t)mod->id);
 	else
-		mesh_db_write_uint32_hex(jmodel, "modelId", mod->id);
+		snprintf(buf, 9, "%8.8x", mod->id);
 
-	json_object_array_add(jmodels, jmodel);
+	jmodel = json_object_new_object();
+
+	if (!jmodel) {
+		json_object_put(jmodels);
+		return;
+	}
+
+	json_object_object_add(jmodels, buf, jmodel);
 }
 
 /* Add unprovisioned node (local) */
-bool mesh_db_add_node(json_object *jnode, struct mesh_db_node *node) {
-
+bool mesh_db_add_node(json_object *jnode, struct mesh_db_node *node)
+{
 	struct mesh_db_modes *modes = &node->modes;
 	const struct l_queue_entry *entry;
 	json_object *jelements;
@@ -1493,38 +1518,41 @@ bool mesh_db_add_node(json_object *jnode, struct mesh_db_node *node) {
 						json_object_new_int(node->ttl));
 
 	/* Elements */
-	jelements = json_object_new_array();
+	jelements = json_object_new_object();
 	if (!jelements)
 		return false;
 
 	entry = l_queue_get_entries(node->elements);
 
-	for (; entry; entry = entry->next) {
+	for (int idx = 0; entry; entry = entry->next, idx++) {
+
+		char int_as_str[11];
 		struct mesh_db_element *ele = entry->data;
-		json_object *jelement, *jmodels;
+		json_object *jmodels;
+		json_object *jsub_elements;
 
-		jelement = json_object_new_object();
+		/* Convert index to string value */
+		sprintf(int_as_str, "%d", ele->index);
+		jsub_elements = json_object_new_object();
 
-		if (!jelement) {
-			json_object_put(jelements);
-			return false;
-		}
+		mesh_db_write_uint16_hex(jsub_elements,
+				"location", ele->location);
 
-		mesh_db_write_int(jelement, "elementIndex", ele->index);
-		mesh_db_write_uint16_hex(jelement, "location", ele->location);
-		json_object_array_add(jelements, jelement);
+		json_object_object_add(jelements,
+				&int_as_str[0], jsub_elements);
 
 		/* Models */
 		if (l_queue_isempty(ele->models))
 			continue;
 
-		jmodels = json_object_new_array();
+		jmodels = json_object_new_object();
+
 		if (!jmodels) {
 			json_object_put(jelements);
 			return false;
 		}
 
-		json_object_object_add(jelement, "models", jmodels);
+		json_object_object_add(jsub_elements, "models", jmodels);
 		l_queue_foreach(ele->models, add_model, jmodels);
 	}
 
-- 
2.20.1


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [PATCH BlueZ 2/3] mesh: Refactor net keys related object in json file
  2019-06-05 13:01 [PATCH BlueZ 0/3] Json storage refactor Jakub Witowski
  2019-06-05 13:01 ` [PATCH BlueZ 1/3] mesh: Refactor elements related object in json file Jakub Witowski
@ 2019-06-05 13:01 ` Jakub Witowski
  2019-06-05 13:01 ` [PATCH BlueZ 3/3] mesh: Refactor app key " Jakub Witowski
  2019-06-05 19:49 ` [PATCH BlueZ 0/3] Json storage refactor Stotland, Inga
  3 siblings, 0 replies; 6+ messages in thread
From: Jakub Witowski @ 2019-06-05 13:01 UTC (permalink / raw)
  To: linux-bluetooth; +Cc: Inga Stotland, Brian Gix

Network keys have been simplified in json storage file.
The array object is no longer used.
---
 mesh/mesh-db.c | 113 +++++++++++++++++++++++--------------------------
 1 file changed, 53 insertions(+), 60 deletions(-)

diff --git a/mesh/mesh-db.c b/mesh/mesh-db.c
index 7bc1ba272..e8a9a1227 100644
--- a/mesh/mesh-db.c
+++ b/mesh/mesh-db.c
@@ -373,39 +373,48 @@ bool mesh_db_read_app_keys(json_object *jobj, mesh_db_app_key_cb cb,
 bool mesh_db_read_net_keys(json_object *jobj, mesh_db_net_key_cb cb,
 								void *user_data)
 {
-	json_object *jarray;
-	int len;
-	int i;
+	json_object *jobject;
+	struct json_object_iterator iter, end;
+	const char *jidx;
+	json_object *jtemp, *jvalue;
 
 	if (!cb)
 		return true;
 
-	if (!json_object_object_get_ex(jobj, "netKeys", &jarray))
-		return false;
+	if (!json_object_object_get_ex(jobj, "netKeys", &jobject))
+		return true;
 
-	if (json_object_get_type(jarray) != json_type_array)
+	if (json_object_get_type(jobject) != json_type_object)
 		return false;
 
-	len = json_object_array_length(jarray);
+	iter = json_object_iter_begin(jobject);
+	end = json_object_iter_end(jobject);
+
+	while (!json_object_iter_equal(&iter, &end)) {
 
-	for (i = 0; i < len; ++i) {
-		json_object *jtemp, *jvalue;
 		int idx;
-		char *str;
+		char *str, *end;
 		bool key_refresh = false;
 		int phase;
 		uint8_t key[16];
 		uint8_t new_key[16];
 
-		jtemp = json_object_array_get_idx(jarray, i);
+		jidx = json_object_iter_peek_name(&iter);
+		jtemp = json_object_iter_peek_value(&iter);
 
-		if (!get_int(jtemp, "index", &idx))
+		if (!*jidx)
 			return false;
 
-		if (!CHECK_KEY_IDX_RANGE(idx))
+		idx = (int)strtol(jidx, &end, 10);
+		if (*end || !CHECK_KEY_IDX_RANGE(idx))
 			return false;
 
-		if (json_object_object_get_ex(jtemp, "oldKey", &jvalue)) {
+		if (json_object_get_type(jtemp) != json_type_object)
+			return false;
+
+		json_object_object_get_ex(jtemp, "oldKey", &jvalue);
+
+		if (jvalue) {
 			str = (char *)json_object_get_string(jvalue);
 			if (!str2hex(str, strlen(str), key, 16))
 				return false;
@@ -419,7 +428,9 @@ bool mesh_db_read_net_keys(json_object *jobj, mesh_db_net_key_cb cb,
 		if (!str2hex(str, strlen(str), key_refresh ? new_key : key, 16))
 			return false;
 
-		if (!json_object_object_get_ex(jtemp, "keyRefresh", &jvalue))
+		json_object_object_get_ex(jtemp, "keyRefresh", &jvalue);
+
+		if (!jvalue)
 			phase = KEY_REFRESH_PHASE_NONE;
 		else
 			phase = json_object_get_int(jvalue);
@@ -428,51 +439,45 @@ bool mesh_db_read_net_keys(json_object *jobj, mesh_db_net_key_cb cb,
 		if (!cb((uint16_t)idx, key, key_refresh ? new_key : NULL, phase,
 								user_data))
 			return false;
+
+		json_object_iter_next(&iter);
 	}
 
 	return true;
 }
 
 bool mesh_db_net_key_add(json_object *jobj, uint16_t idx,
-							const uint8_t key[16])
+		const uint8_t key[16])
 {
-	json_object *jarray = NULL, *jentry = NULL, *jstring;
-	char buf[5];
+	json_object *jobject, *jentry = NULL, *jvalue = NULL;
+	char int_as_str[6];
 
-	json_object_object_get_ex(jobj, "netKeys", &jarray);
-	if (jarray)
-		jentry = get_key_object(jarray, idx);
+	json_object_object_get_ex(jobj, "netKeys", &jobject);
 
-	/* Do not allow direct overwrite */
-	if (jentry)
-		return false;
+	if (!jobject) {
+		jobject = json_object_new_object();
+
+		if (!jobject)
+			return false;
+
+		json_object_object_add(jobj, "netKeys", jobject);
+	}
 
 	jentry = json_object_new_object();
 	if (!jentry)
-		return false;
+		goto fail;
 
-	snprintf(buf, 5, "%4.4x", idx);
-	jstring = json_object_new_string(buf);
-	if (!jstring)
+	jvalue = json_object_new_int(KEY_REFRESH_PHASE_NONE);
+	if (!jvalue)
 		goto fail;
 
-	json_object_object_add(jentry, "index", jstring);
+	json_object_object_add(jentry, "keyRefresh", jvalue);
 
 	if (!add_key_value(jentry, "key", key))
 		goto fail;
 
-	json_object_object_add(jentry, "keyRefresh",
-				json_object_new_int(KEY_REFRESH_PHASE_NONE));
-
-	if (!jarray) {
-		jarray = json_object_new_array();
-		if (!jarray)
-			goto fail;
-		json_object_object_add(jobj, "netKeys", jarray);
-	}
-
-	json_object_array_add(jarray, jentry);
-
+	snprintf(int_as_str, sizeof(int_as_str), "%hd", idx);
+	json_object_object_add(jobject, int_as_str, jentry);
 	return true;
 fail:
 	if (jentry)
@@ -514,32 +519,20 @@ bool mesh_db_net_key_update(json_object *jobj, uint16_t idx,
 
 bool mesh_db_net_key_del(json_object *jobj, uint16_t idx)
 {
-	json_object *jarray, *jarray_new;
+	json_object *jobject;
+	char int_as_str[6];
 
-	if (!json_object_object_get_ex(jobj, "netKeys", &jarray))
+	json_object_object_get_ex(jobj, "netKeys", &jobject);
+	if (!jobject)
 		return true;
 
-	/* Check if matching entry exists */
-	if (!get_key_object(jarray, idx))
-		return true;
-
-	if (json_object_array_length(jarray) == 1) {
+	if (json_object_get_object(jobject)->size == 1) {
 		json_object_object_del(jobj, "netKeys");
 		return true;
 	}
 
-	/*
-	 * There is no easy way to delete a value from json array.
-	 * Create a new copy without specified element and
-	 * then remove old array.
-	 */
-	jarray_new = jarray_key_del(jarray, idx);
-	if (!jarray_new)
-		return false;
-
-	json_object_object_del(jobj, "netKeys");
-	json_object_object_add(jobj, "netKeys", jarray_new);
-
+	snprintf(int_as_str, sizeof(int_as_str), "%hd", idx);
+	json_object_object_del(jobject, int_as_str);
 	return true;
 }
 
-- 
2.20.1


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [PATCH BlueZ 3/3] mesh: Refactor app key related object in json file
  2019-06-05 13:01 [PATCH BlueZ 0/3] Json storage refactor Jakub Witowski
  2019-06-05 13:01 ` [PATCH BlueZ 1/3] mesh: Refactor elements related object in json file Jakub Witowski
  2019-06-05 13:01 ` [PATCH BlueZ 2/3] mesh: Refactor net keys " Jakub Witowski
@ 2019-06-05 13:01 ` Jakub Witowski
  2019-06-05 19:49 ` [PATCH BlueZ 0/3] Json storage refactor Stotland, Inga
  3 siblings, 0 replies; 6+ messages in thread
From: Jakub Witowski @ 2019-06-05 13:01 UTC (permalink / raw)
  To: linux-bluetooth; +Cc: Inga Stotland

Application keys have been simplified in json storage file.
The array object is no longer used.
---
 mesh/mesh-db.c | 222 +++++++++++++++++++++++++------------------------
 1 file changed, 114 insertions(+), 108 deletions(-)

diff --git a/mesh/mesh-db.c b/mesh/mesh-db.c
index e8a9a1227..30e8f7738 100644
--- a/mesh/mesh-db.c
+++ b/mesh/mesh-db.c
@@ -205,25 +205,17 @@ static json_object *jarray_string_del(json_object *jarray, char *str,
 	return jarray_new;
 }
 
-static json_object *get_key_object(json_object *jarray, uint16_t idx)
+static json_object *get_key_object(json_object *jobject, uint16_t idx)
 {
-	int i, sz = json_object_array_length(jarray);
-
-	for (i = 0; i < sz; ++i) {
-		json_object *jentry, *jvalue;
-		uint32_t jidx;
-
-		jentry = json_object_array_get_idx(jarray, i);
-		if (!json_object_object_get_ex(jentry, "index", &jvalue))
-			return NULL;
+	char int_as_str[6];
 
-		jidx = json_object_get_int(jvalue);
+	/* Convert index to string */
+	sprintf(int_as_str, "%u", idx);
 
-		if (jidx == idx)
-			return jentry;
-	}
+	if (!json_object_object_get_ex(jobject, int_as_str, &jobject))
+		return NULL;
 
-	return NULL;
+	return jobject;
 }
 
 static json_object *jarray_key_del(json_object *jarray, int16_t idx)
@@ -311,62 +303,77 @@ bool mesh_db_read_device_key(json_object *jobj, uint8_t key_buf[16])
 bool mesh_db_read_app_keys(json_object *jobj, mesh_db_app_key_cb cb,
 							void *user_data)
 {
-	json_object *jarray;
-	int len;
-	int i;
+	struct json_object_iterator current;
+	struct json_object_iterator end;
+	struct json_object *jvalue, *jnet_k_idx, *jnet_key, *jold_key;
+
+	char *key_val;
+	const char *idx;
+	uint8_t key[16];
+	uint8_t new_key[16];
+	int app_idx, net_idx;
+	bool key_refresh = false;
+
+	l_info("");
 
 	if (!cb)
 		return true;
 
-	if (!json_object_object_get_ex(jobj, "appKeys", &jarray))
-		return false;
+	if (!json_object_object_get_ex(jobj, "appKeys", &jvalue))
+		return true;
 
-	if (json_object_get_type(jarray) != json_type_array)
-		return false;
+	current = json_object_iter_begin(jvalue);
+	end = json_object_iter_end(jvalue);
 
-	len = json_object_array_length(jarray);
+	while (!json_object_iter_equal(&current, &end)) {
 
-	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];
+		idx = json_object_iter_peek_name(&current);
 
-		jtemp = json_object_array_get_idx(jarray, i);
+		/* Get app key index */
+		app_idx = atoi(idx);
 
-		if (!get_int(jtemp, "index", &app_idx))
+		if (!CHECK_KEY_IDX_RANGE(app_idx))
 			return false;
 
-		if (!CHECK_KEY_IDX_RANGE(app_idx))
+		/* Enter to the 'idx' values */
+		if (!json_object_object_get_ex(jvalue, idx, &jnet_k_idx))
 			return false;
 
-		if (!get_int(jtemp, "boundNetKey", &net_idx))
+		/* Get net key index */
+		if (!get_int(jnet_k_idx, "boundNetKey", &net_idx))
 			return false;
 
 		if (!CHECK_KEY_IDX_RANGE(net_idx))
 			return false;
 
-		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))
+		/* Get app key */
+		if (!json_object_object_get_ex(jnet_k_idx, "key", &jnet_key))
 			return false;
 
-		str = (char *)json_object_get_string(jvalue);
-		if (!str2hex(str, strlen(str), key_refresh ? new_key : key, 16))
+		key_val = (char *)json_object_get_string(jnet_key);
+
+		if (!str2hex(key_val, strlen(key_val),
+					 key_refresh ? new_key : key, 16))
 			return false;
 
+		/* Get old key if exists */
+		json_object_object_get_ex(jnet_key, "oldKey", &jold_key);
+
+		if (jold_key) {
+			key_val = (char *)json_object_get_string(jvalue);
+
+			if (!str2hex(key_val, strlen(key_val), key, 16))
+				return false;
+
+			key_refresh = true;
+		}
+
 		if (!cb((uint16_t)net_idx, (uint16_t) app_idx, key,
 				key_refresh ? new_key : NULL, user_data))
 			return false;
-	}
 
+		json_object_iter_next(&current);
+	}
 	return true;
 }
 
@@ -377,6 +384,7 @@ bool mesh_db_read_net_keys(json_object *jobj, mesh_db_net_key_cb cb,
 	struct json_object_iterator iter, end;
 	const char *jidx;
 	json_object *jtemp, *jvalue;
+	l_info("");
 
 	if (!cb)
 		return true;
@@ -451,6 +459,7 @@ bool mesh_db_net_key_add(json_object *jobj, uint16_t idx,
 {
 	json_object *jobject, *jentry = NULL, *jvalue = NULL;
 	char int_as_str[6];
+	l_info("");
 
 	json_object_object_get_ex(jobj, "netKeys", &jobject);
 
@@ -489,13 +498,14 @@ fail:
 bool mesh_db_net_key_update(json_object *jobj, uint16_t idx,
 							const uint8_t key[16])
 {
-	json_object *jarray, *jentry, *jstring;
+	json_object *jobject, *jentry, *jstring;
 	const char *str;
+	l_info("");
 
-	if (!json_object_object_get_ex(jobj, "netKeys", &jarray))
+	if (!json_object_object_get_ex(jobj, "netKeys", &jobject))
 		return false;
 
-	jentry = get_key_object(jarray, idx);
+	jentry = get_key_object(jobject, idx);
 	/* Net key must be already recorded */
 	if (!jentry)
 		return false;
@@ -521,6 +531,7 @@ bool mesh_db_net_key_del(json_object *jobj, uint16_t idx)
 {
 	json_object *jobject;
 	char int_as_str[6];
+	l_info("");
 
 	json_object_object_get_ex(jobj, "netKeys", &jobject);
 	if (!jobject)
@@ -549,50 +560,37 @@ bool mesh_db_write_token(json_object *jnode, uint8_t *token)
 bool mesh_db_app_key_add(json_object *jobj, 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 *jobject, *jentry = NULL;
+	char int_as_str[6];
 
-	json_object_object_get_ex(jobj, "appKeys", &jarray);
-	if (jarray)
-		jentry = get_key_object(jarray, app_idx);
+	/* Create appKey object if doesn't exists */
+	if (!json_object_object_get_ex(jobj, "appKeys", &jobject)) {
+		jobject = json_object_new_object();
+		json_object_object_add(jobj, "appKeys", jobject);
+	}
 
-	/* Do not allow direct overrwrite */
-	if (jentry)
-		return false;
+	/* Check if entry (index) is known */
+	if (!jentry) {
+		jentry = json_object_new_object();
 
-	jentry = json_object_new_object();
-	if (!jentry)
-		return false;
-
-	snprintf(buf, 5, "%4.4x", app_idx);
-	jstring = json_object_new_string(buf);
-	if (!jstring)
-		goto fail;
+		if (!jentry)
+			goto fail;
+	}
 
-	json_object_object_add(jentry, "index", jstring);
+	/* Convert idx to string value */
+	sprintf(int_as_str, "%hd", app_idx);
 
-	snprintf(buf, 5, "%4.4x", net_idx);
-	jstring = json_object_new_string(buf);
-	if (!jstring)
+	/* Add net index value */
+	if (!mesh_db_write_int(jentry, "boundNetKey", net_idx))
 		goto fail;
 
-	json_object_object_add(jentry, "boundNetKey", jstring);
-
 	if (!add_key_value(jentry, "key", key))
 		goto fail;
 
-	if (!jarray) {
-		jarray = json_object_new_array();
-		if (!jarray)
-			goto fail;
-		json_object_object_add(jobj, "appKeys", jarray);
-	}
-
-	json_object_array_add(jarray, jentry);
-
+	json_object_object_add(jobject, &int_as_str[0], jentry);
 	return true;
-fail:
 
+fail:
 	if (jentry)
 		json_object_put(jentry);
 
@@ -602,55 +600,60 @@ fail:
 bool mesh_db_app_key_update(json_object *jobj, uint16_t app_idx,
 							const uint8_t key[16])
 {
-	json_object *jarray, *jentry = NULL, *jstring = NULL;
+	json_object *jobject, *jentry = NULL, *jstring = NULL;
 	const char *str;
 
-	if (!json_object_object_get_ex(jobj, "appKeys", &jarray))
-		return false;
+	/* Check if update has been requested */
+	if (!json_object_object_get_ex(jobj, "appKeys", &jobject))
+		goto fail;
+
+	jentry = get_key_object(jobject, app_idx);
 
 	/* The key entry should exist if the key is updated */
-	jentry = get_key_object(jarray, app_idx);
 	if (!jentry)
-		return false;
+		goto fail;
 
 	if (!json_object_object_get_ex(jentry, "key", &jstring))
-		return false;
+		goto fail;
 
 	str = json_object_get_string(jstring);
 	jstring = json_object_new_string(str);
 	json_object_object_add(jentry, "oldKey", jstring);
 	json_object_object_del(jentry, "key");
 
-	return add_key_value(jentry, "key", key);
+	if (!add_key_value(jentry, "key", key))
+		goto fail;
+
+	return true;
+
+fail:
+	if (jentry)
+		json_object_put(jentry);
+
+	return false;
 }
 
 bool mesh_db_app_key_del(json_object *jobj, uint16_t net_idx, uint16_t idx)
 {
-	json_object *jarray, *jarray_new;
-
-	if (!json_object_object_get_ex(jobj, "appKeys", &jarray))
-		return true;
+	char int_as_str[6];
+	json_object *jobject;
 
-	/* Check if matching entry exists */
-	if (!get_key_object(jarray, idx))
+	/* Check if appKeys object exists */
+	json_object_object_get_ex(jobj, "appKeys", &jobject);
+	if (!jobject)
 		return true;
 
-	if (json_object_array_length(jarray) == 1) {
+	if (json_object_get_object(jobject)->size == 1) {
+		/* Only one appKey is in Json file.
+		 * Remove the entire appKey field
+		 */
 		json_object_object_del(jobj, "appKeys");
 		return true;
 	}
 
-	/*
-	 * There is no easy way to delete a value from json array.
-	 * Create a new copy without specified element and
-	 * then remove old array.
-	 */
-	jarray_new = jarray_key_del(jarray, idx);
-	if (!jarray_new)
-		return false;
-
-	json_object_object_del(jobj, "appKeys");
-	json_object_object_add(jobj, "appKeys", jarray_new);
+	/* Convert index to string */
+	sprintf(int_as_str, "%u", idx);
+	json_object_object_del(jobject, int_as_str);
 
 	return true;
 }
@@ -991,6 +994,7 @@ static bool parse_elements(json_object *jelements, struct mesh_db_node *node)
 	struct json_object_iterator iter, end;
 	json_object *jtemp;
 	const char *jidx;
+	l_info("");
 
 	node->elements = l_queue_new();
 
@@ -1444,6 +1448,7 @@ static void add_model(void *a, void *b)
 	json_object *jmodels = b;
 	json_object *jmodel;
 	char buf[9];
+	l_info("");
 
 	if (!mod->vendor)
 		snprintf(buf, 5, "%4.4x", (uint16_t)mod->id);
@@ -1466,6 +1471,7 @@ bool mesh_db_add_node(json_object *jnode, struct mesh_db_node *node)
 	struct mesh_db_modes *modes = &node->modes;
 	const struct l_queue_entry *entry;
 	json_object *jelements;
+	l_info("");
 
 	if (!jnode)
 		return false;
@@ -1587,13 +1593,13 @@ static void finish_key_refresh(json_object *jobj, uint16_t net_idx)
 
 bool mesh_db_net_key_set_phase(json_object *jobj, uint16_t idx, uint8_t phase)
 {
-	json_object *jarray, *jentry = NULL;
+	json_object *jobject, *jentry = NULL;
 
 	if (!jobj)
 		return false;
 
-	if (json_object_object_get_ex(jobj, "netKeys", &jarray))
-		jentry = get_key_object(jarray, idx);
+	if (json_object_object_get_ex(jobj, "netKeys", &jobject))
+		jentry = get_key_object(jobject, idx);
 
 	if (!jentry)
 		return false;
-- 
2.20.1


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* Re: [PATCH BlueZ 0/3] Json storage refactor
  2019-06-05 13:01 [PATCH BlueZ 0/3] Json storage refactor Jakub Witowski
                   ` (2 preceding siblings ...)
  2019-06-05 13:01 ` [PATCH BlueZ 3/3] mesh: Refactor app key " Jakub Witowski
@ 2019-06-05 19:49 ` Stotland, Inga
  2019-06-06  7:05   ` Michał Lowas-Rzechonek
  3 siblings, 1 reply; 6+ messages in thread
From: Stotland, Inga @ 2019-06-05 19:49 UTC (permalink / raw)
  To: jakub.witowski, linux-bluetooth; +Cc: Gix, Brian

[-- Attachment #1: Type: text/plain, Size: 1113 bytes --]

Hi Jakub,

On Wed, 2019-06-05 at 15:01 +0200, Jakub Witowski wrote:
> Netwrk keys, application keys and elements with models have been
> simplified in json storage file. Arrays objects are no longer
> required.
> 
> Jakub Witowski (3):
>   mesh: Refactor elements related object in json file
>   mesh: Refactor net keys related object in json file
>   mesh: Refactor app key related object in json file
> 
>  mesh/mesh-db.c | 469 ++++++++++++++++++++++++++---------------------
> --
>  1 file changed, 248 insertions(+), 221 deletions(-)
> 

The proposed change would be okay if not for the fact that we plan to
re-use the same code for storing the information database for
provisioner/configuration client. That puts constraints on the format
in which the json data is stored.

However, since mesh-db.h describes self-contained API, it should be
easy to create a custom bluetooth-meshd solution, i.e., keeping the
mesh-db APIs unchanged, but use customized routines to implement them.
That should allow you to keep local node configuration in your own
format.

Best regards,
Inga


[-- Attachment #2: smime.p7s --]
[-- Type: application/x-pkcs7-signature, Size: 3265 bytes --]

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH BlueZ 0/3] Json storage refactor
  2019-06-05 19:49 ` [PATCH BlueZ 0/3] Json storage refactor Stotland, Inga
@ 2019-06-06  7:05   ` Michał Lowas-Rzechonek
  0 siblings, 0 replies; 6+ messages in thread
From: Michał Lowas-Rzechonek @ 2019-06-06  7:05 UTC (permalink / raw)
  To: Stotland, Inga; +Cc: jakub.witowski, linux-bluetooth, Gix, Brian

Hi Inga,

On 06/05, Stotland, Inga wrote:
> On Wed, 2019-06-05 at 15:01 +0200, Jakub Witowski wrote:
> > Netwrk keys, application keys and elements with models have been
> > simplified in json storage file. Arrays objects are no longer
> > required.
> The proposed change would be okay if not for the fact that we plan to
> re-use the same code for storing the information database for
> provisioner/configuration client. That puts constraints on the format
> in which the json data is stored.
Could you please elaborate?

The patchset doesn't change much in terms of JSON format, it just
replaces thing like:

    "netKeys": [
        { "index": "0000", <params...> },
        { "index": "0001", <params...> }
    ],
    "elements": [
        { "elementIndex": 0, <params...> }
    ]

with:

    "netKeys": {
        "0000": { <params> },
        "0001": { <params> }
    },
    "elements": {
        "0": { <params...> }
    }

The latter format should allow storing exactly the same information, but in a
slightly more straightforward manner.

> (...) use customized routines to implement them.
> That should allow you to keep local node configuration in your own
> format.
We would very much like to avoid vendor patches. From our POV the whole
point of open source collaboration is to make the *mainline* usable for
as many people as possible.

regards
-- 
Michał Lowas-Rzechonek <michal.lowas-rzechonek@silvair.com>
Silvair http://silvair.com
Jasnogórska 44, 31-358 Krakow, POLAND

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2019-06-06  7:05 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-06-05 13:01 [PATCH BlueZ 0/3] Json storage refactor Jakub Witowski
2019-06-05 13:01 ` [PATCH BlueZ 1/3] mesh: Refactor elements related object in json file Jakub Witowski
2019-06-05 13:01 ` [PATCH BlueZ 2/3] mesh: Refactor net keys " Jakub Witowski
2019-06-05 13:01 ` [PATCH BlueZ 3/3] mesh: Refactor app key " Jakub Witowski
2019-06-05 19:49 ` [PATCH BlueZ 0/3] Json storage refactor Stotland, Inga
2019-06-06  7:05   ` Michał Lowas-Rzechonek

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).