From: Jakub Witowski <jakub.witowski@xxxxxxxxxxx> This enables more code to be reused in ImportLocalNode implementation, when using 'json' import data format. --- mesh/mesh-config-json.c | 76 +++++++++++++++++++++++++++++++---------- 1 file changed, 58 insertions(+), 18 deletions(-) diff --git a/mesh/mesh-config-json.c b/mesh/mesh-config-json.c index 75015e607..6df7f7b3f 100644 --- a/mesh/mesh-config-json.c +++ b/mesh/mesh-config-json.c @@ -52,6 +52,8 @@ struct write_info { mesh_config_status_func_t cb; }; +typedef bool (*read_net_key_cb)(struct mesh_config_netkey *, void*); + static const char *cfg_name = "/node.json"; static const char *bak_ext = ".bak"; static const char *tmp_ext = ".tmp"; @@ -297,6 +299,33 @@ static json_object *jarray_key_del(json_object *jarray, int16_t idx) return jarray_new; } +static bool read_unicast_address(json_object *jobj, + uint16_t *unicast) +{ + json_object *jvalue; + char *str; + + if (!json_object_object_get_ex(jobj, "unicastAddress", &jvalue)) + return false; + + str = (char *)json_object_get_string(jvalue); + if (sscanf(str, "%04hx", unicast) != 1) + return false; + + return true; +} + +static bool read_seq_number(json_object *jobj, uint32_t *seq_number) +{ + json_object *jvalue; + + if (!json_object_object_get_ex(jobj, "sequenceNumber", &jvalue)) + return false; + + *seq_number = json_object_get_int(jvalue); + return true; +} + static bool read_iv_index(json_object *jobj, uint32_t *idx, bool *update) { int tmp; @@ -424,12 +453,35 @@ fail: return false; } -static bool read_net_keys(json_object *jobj, struct mesh_config_node *node) +static bool read_net_key(struct mesh_config_netkey *net_key, + void *user_data) +{ + struct mesh_config_node *node = user_data; + + if (!net_key) { + l_queue_destroy(node->netkeys, l_free); + node->netkeys = NULL; + return false; + } + + if (!node->netkeys) + node->netkeys = l_queue_new(); + + l_queue_push_tail(node->netkeys, net_key); + + return true; +} + +static bool read_net_keys(json_object *jobj, read_net_key_cb cb, + void *user_data) { json_object *jarray; int len; int i; + if (!cb) + return true; + /* At least one NetKey must be present for a provisioned node */ if (!json_object_object_get_ex(jobj, "netKeys", &jarray)) return false; @@ -441,8 +493,6 @@ static bool read_net_keys(json_object *jobj, struct mesh_config_node *node) if (!len) return false; - node->netkeys = l_queue_new(); - for (i = 0; i < len; ++i) { json_object *jtemp, *jvalue; char *str; @@ -480,13 +530,12 @@ static bool read_net_keys(json_object *jobj, struct mesh_config_node *node) if (!str2hex(str, strlen(str), netkey->key, 16)) goto fail; - l_queue_push_tail(node->netkeys, netkey); + cb(netkey, user_data); } return true; fail: - l_queue_destroy(node->netkeys, l_free); - node->netkeys = NULL; + cb(NULL, user_data); return false; } @@ -1294,7 +1343,6 @@ static bool read_net_transmit(json_object *jobj, struct mesh_config_node *node) static bool read_node(json_object *jnode, struct mesh_config_node *node) { json_object *jvalue; - char *str; if (!read_iv_index(jnode, &node->iv_index, &node->iv_update)) { l_info("Failed to read IV index"); @@ -1318,14 +1366,7 @@ static bool read_node(json_object *jnode, struct mesh_config_node *node) parse_features(jnode, node); - if (!json_object_object_get_ex(jnode, "unicastAddress", &jvalue)) { - l_info("Bad config: Unicast address must be present"); - return false; - } - - str = (char *)json_object_get_string(jvalue); - if (sscanf(str, "%04hx", &node->unicast) != 1) - return false; + read_unicast_address(jnode, &node->unicast); if (json_object_object_get_ex(jnode, "defaultTTL", &jvalue)) { int ttl = json_object_get_int(jvalue); @@ -1335,8 +1376,7 @@ static bool read_node(json_object *jnode, struct mesh_config_node *node) node->ttl = (uint8_t) ttl; } - if (json_object_object_get_ex(jnode, "sequenceNumber", &jvalue)) - node->seq_number = json_object_get_int(jvalue); + read_seq_number(jnode, &node->seq_number); /* Check for required "elements" property */ if (!json_object_object_get_ex(jnode, "elements", &jvalue)) @@ -1347,7 +1387,7 @@ static bool read_node(json_object *jnode, struct mesh_config_node *node) return false; } - if (!read_net_keys(jnode, node)) { + if (!read_net_keys(jnode, read_net_key, node)) { l_info("Failed to read net keys"); return false; } -- 2.19.1