When an App is creating or attaching to a node, it first evaluates the validity of the Apps composition. Invalid compositions are not allowed to continue. Valid compositions need to be saved, which means that create_node_config() must be called as part of Composition validation. --- mesh/node.c | 147 +++++++++++++++++++++++++++------------------------- 1 file changed, 75 insertions(+), 72 deletions(-) diff --git a/mesh/node.c b/mesh/node.c index 76d8fd6e9..4170fad58 100644 --- a/mesh/node.c +++ b/mesh/node.c @@ -857,6 +857,75 @@ static bool match_page(const void *a, const void *b) return page->page_num == page_num; } +static void convert_node_to_storage(struct mesh_node *node, + struct mesh_config_node *db_node) +{ + const struct l_queue_entry *entry; + + db_node->cid = node->comp.cid; + db_node->pid = node->comp.pid; + db_node->vid = node->comp.vid; + db_node->crpl = node->comp.crpl; + db_node->modes.lpn = node->lpn; + db_node->modes.proxy = node->proxy; + + db_node->modes.friend = node->friend; + db_node->modes.relay.state = node->relay.mode; + db_node->modes.relay.cnt = node->relay.cnt; + db_node->modes.relay.interval = node->relay.interval; + db_node->modes.beacon = node->beacon; + + db_node->ttl = node->ttl; + db_node->seq_number = node->seq_number; + + db_node->elements = l_queue_new(); + + entry = l_queue_get_entries(node->elements); + + for (; entry; entry = entry->next) { + struct node_element *ele = entry->data; + struct mesh_config_element *db_ele; + + db_ele = l_new(struct mesh_config_element, 1); + + db_ele->index = ele->idx; + db_ele->location = ele->location; + db_ele->models = l_queue_new(); + + mesh_model_convert_to_storage(db_ele->models, ele->models); + + l_queue_push_tail(db_node->elements, db_ele); + } + +} + +static bool create_node_config(struct mesh_node *node, const uint8_t uuid[16]) +{ + struct mesh_config_node db_node; + const struct l_queue_entry *entry; + const char *storage_dir; + + convert_node_to_storage(node, &db_node); + storage_dir = mesh_get_storage_dir(); + node->cfg = mesh_config_create(storage_dir, uuid, &db_node); + + if (node->cfg) + init_storage_dir(node); + + /* Free temporarily allocated resources */ + entry = l_queue_get_entries(db_node.elements); + + for (; entry; entry = entry->next) { + struct mesh_config_element *db_ele = entry->data; + + l_queue_destroy(db_ele->models, l_free); + } + + l_queue_destroy(db_node.elements, l_free); + + return node->cfg != NULL; +} + bool node_set_comp(struct mesh_node *node, uint8_t page_num, const uint8_t *data, uint16_t len) { @@ -865,6 +934,9 @@ bool node_set_comp(struct mesh_node *node, uint8_t page_num, if (!node || len < MIN_COMP_SIZE) return false; + if (!node->cfg && !create_node_config(node, node->uuid)) + return false; + page = l_queue_remove_if(node->pages, match_page, L_UINT_TO_PTR(page_num)); @@ -1107,75 +1179,6 @@ fail: return false; } -static void convert_node_to_storage(struct mesh_node *node, - struct mesh_config_node *db_node) -{ - const struct l_queue_entry *entry; - - db_node->cid = node->comp.cid; - db_node->pid = node->comp.pid; - db_node->vid = node->comp.vid; - db_node->crpl = node->comp.crpl; - db_node->modes.lpn = node->lpn; - db_node->modes.proxy = node->proxy; - - db_node->modes.friend = node->friend; - db_node->modes.relay.state = node->relay.mode; - db_node->modes.relay.cnt = node->relay.cnt; - db_node->modes.relay.interval = node->relay.interval; - db_node->modes.beacon = node->beacon; - - db_node->ttl = node->ttl; - db_node->seq_number = node->seq_number; - - db_node->elements = l_queue_new(); - - entry = l_queue_get_entries(node->elements); - - for (; entry; entry = entry->next) { - struct node_element *ele = entry->data; - struct mesh_config_element *db_ele; - - db_ele = l_new(struct mesh_config_element, 1); - - db_ele->index = ele->idx; - db_ele->location = ele->location; - db_ele->models = l_queue_new(); - - mesh_model_convert_to_storage(db_ele->models, ele->models); - - l_queue_push_tail(db_node->elements, db_ele); - } - -} - -static bool create_node_config(struct mesh_node *node, const uint8_t uuid[16]) -{ - struct mesh_config_node db_node; - const struct l_queue_entry *entry; - const char *storage_dir; - - convert_node_to_storage(node, &db_node); - storage_dir = mesh_get_storage_dir(); - node->cfg = mesh_config_create(storage_dir, uuid, &db_node); - - if (node->cfg) - init_storage_dir(node); - - /* Free temporarily allocated resources */ - entry = l_queue_get_entries(db_node.elements); - - for (; entry; entry = entry->next) { - struct mesh_config_element *db_ele = entry->data; - - l_queue_destroy(db_ele->models, l_free); - } - - l_queue_destroy(db_node.elements, l_free); - - return node->cfg != NULL; -} - static bool get_app_properties(struct mesh_node *node, const char *path, struct l_dbus_message_iter *properties) { @@ -1516,7 +1519,7 @@ static void get_managed_objects_cb(struct l_dbus_message *msg, void *user_data) goto fail; } - if (!create_node_config(node, node->uuid)) + if (!node->cfg && !create_node_config(node, node->uuid)) goto fail; req->join_ready_cb(node, node->agent); @@ -1524,7 +1527,7 @@ static void get_managed_objects_cb(struct l_dbus_message *msg, void *user_data) return; case REQUEST_TYPE_IMPORT: - if (!create_node_config(node, node->uuid)) + if (!node->cfg && !create_node_config(node, node->uuid)) goto fail; import = req->import; @@ -1540,7 +1543,7 @@ static void get_managed_objects_cb(struct l_dbus_message *msg, void *user_data) return; case REQUEST_TYPE_CREATE: - if (!create_node_config(node, node->uuid)) + if (!node->cfg && !create_node_config(node, node->uuid)) goto fail; /* Generate device and primary network keys */ -- 2.25.4