From 7bea16649018450ca749921abb742c8a800d4377 Mon Sep 17 00:00:00 2001 From: Inga Stotland Date: Fri, 28 Aug 2020 09:51:12 -0700 Subject: [PATCH] mesh: Store key indices as integers in node config Store NetKey and AppKey indices as integers instead of hex strings. This removes unnecessary string manipulations while not affecting the configuration file readability. Change-Id: Ic2cf06f4a3ec8cf9fd429caa863f0893ab5afc9f Signed-off-by: anuj.bhumiya --- mesh/mesh-config-json.c | 102 ++++++++++++++++++------------------------------ 1 file changed, 38 insertions(+), 64 deletions(-) diff --git a/mesh/mesh-config-json.c b/mesh/mesh-config-json.c index 374f217..857ed2e 100644 --- a/mesh/mesh-config-json.c +++ b/mesh/mesh-config-json.c @@ -250,21 +250,31 @@ static void jarray_string_del(json_object *jarray, char *str, size_t len) } } +static bool get_key_index(json_object *jobj, const char *keyword, + uint16_t *index) +{ + int idx; + + if (!get_int(jobj, keyword, &idx)) + return false; + + if (!CHECK_KEY_IDX_RANGE(idx)) + return false; + + *index = (uint16_t) idx; + return true; +} + static json_object *get_key_object(json_object *jarray, uint16_t idx) { int i, sz = json_object_array_length(jarray); for (i = 0; i < sz; ++i) { - json_object *jentry, *jvalue; - const char *str; + json_object *jentry; uint16_t jidx; jentry = json_object_array_get_idx(jarray, i); - if (!json_object_object_get_ex(jentry, "index", &jvalue)) - return NULL; - - str = json_object_get_string(jvalue); - if (sscanf(str, "%04hx", &jidx) != 1) + if (!get_key_index(jentry, "index", &jidx)) return NULL; if (jidx == idx) @@ -274,28 +284,6 @@ static json_object *get_key_object(json_object *jarray, uint16_t idx) return NULL; } -static bool get_key_index(json_object *jobj, const char *keyword, - uint16_t *index) -{ - uint16_t idx; - json_object *jvalue; - const char *str; - - if (!json_object_object_get_ex(jobj, keyword, &jvalue)) - return false; - - str = json_object_get_string(jvalue); - - if (sscanf(str, "%04hx", &idx) != 1) - return false; - - if (!CHECK_KEY_IDX_RANGE(idx)) - return false; - - *index = (uint16_t) idx; - return true; -} - static void jarray_key_del(json_object *jarray, int16_t idx) { int i, sz = json_object_array_length(jarray); @@ -595,11 +583,23 @@ fail: return false; } +static bool write_int(json_object *jobj, const char *desc, int val) +{ + json_object *jvalue; + + jvalue = json_object_new_int(val); + if (!jvalue) + return false; + + json_object_object_del(jobj, desc); + json_object_object_add(jobj, desc, jvalue); + return true; +} + bool mesh_config_net_key_add(struct mesh_config *cfg, uint16_t idx, const uint8_t key[16]) { - json_object *jnode, *jarray = NULL, *jentry = NULL, *jstring; - char buf[5]; + json_object *jnode, *jarray = NULL, *jentry = NULL; if (!cfg) return false; @@ -619,13 +619,9 @@ bool mesh_config_net_key_add(struct mesh_config *cfg, uint16_t idx, if (!jentry) return false; - snprintf(buf, 5, "%4.4x", idx); - jstring = json_object_new_string(buf); - if (!jstring) + if (!write_int(jentry, "index", idx)) goto fail; - json_object_object_add(jentry, "index", jstring); - if (!add_key_value(jentry, "key", key)) goto fail; @@ -725,8 +721,7 @@ bool mesh_config_write_token(struct mesh_config *cfg, uint8_t *token) bool mesh_config_app_key_add(struct mesh_config *cfg, uint16_t net_idx, uint16_t app_idx, const uint8_t key[16]) { - json_object *jnode, *jarray = NULL, *jentry = NULL, *jstring = NULL; - char buf[5]; + json_object *jnode, *jarray = NULL, *jentry = NULL; if (!cfg) return false; @@ -745,20 +740,12 @@ bool mesh_config_app_key_add(struct mesh_config *cfg, uint16_t net_idx, if (!jentry) return false; - snprintf(buf, 5, "%4.4x", app_idx); - jstring = json_object_new_string(buf); - if (!jstring) + if (!write_int(jentry, "index", app_idx)) goto fail; - json_object_object_add(jentry, "index", jstring); - - snprintf(buf, 5, "%4.4x", net_idx); - jstring = json_object_new_string(buf); - if (!jstring) + if (!write_int(jentry, "boundNetKey", net_idx)) goto fail; - json_object_object_add(jentry, "boundNetKey", jstring); - if (!add_key_value(jentry, "key", key)) goto fail; @@ -996,6 +983,7 @@ static struct mesh_config_pub *parse_model_publication(json_object *jpub) case 32: if (!str2hex(str, len, pub->virt_addr, 16)) goto fail; + pub->virt = true; break; default: @@ -1469,19 +1457,6 @@ static bool write_uint32_hex(json_object *jobj, const char *desc, uint32_t val) return true; } -static bool write_int(json_object *jobj, const char *desc, int val) -{ - json_object *jvalue; - - jvalue = json_object_new_int(val); - if (!jvalue) - return false; - - json_object_object_del(jobj, desc); - json_object_object_add(jobj, desc, jvalue); - return true; -} - static const char *mode_to_string(int mode) { switch (mode) { @@ -1873,7 +1848,7 @@ bool mesh_config_model_pub_add(struct mesh_config *cfg, uint16_t ele_addr, if (!res) goto fail; - if (!write_uint16_hex(jpub, "index", pub->idx)) + if (!write_int(jpub, "index", pub->idx)) goto fail; if (!write_int(jpub, "ttl", pub->ttl)) @@ -1882,8 +1857,7 @@ bool mesh_config_model_pub_add(struct mesh_config *cfg, uint16_t ele_addr, if (!write_int(jpub, "period", pub->period)) goto fail; - if (!write_int(jpub, "credentials", - pub->credential ? 1 : 0)) + if (!write_int(jpub, "credentials", pub->credential ? 1 : 0)) goto fail; jrtx = json_object_new_object(); -- 2.7.4