+rebased after mesh-config refactoring
+updated documentation
+removed import_req
+added patch to remove void pointers from managed_obj_request
Jakub Witowski (2):
mesh: Add ImportLocalNode API documentation
mesh: Extract read_* functions in mesh-config-json
Michał Lowas-Rzechonek (2):
mesh: Implement ImportLocalNode() method
mesh: Convert void pointers to anonymous unions in
managed_obj_request:wq
doc/mesh-api.txt | 52 ++++++++++---
mesh/mesh-config-json.c | 161 +++++++++++++++++++++++++++++++++++-----
mesh/mesh-config.h | 10 +++
mesh/mesh-defs.h | 3 +
mesh/mesh.c | 90 +++++++++++++++++++++-
mesh/net.h | 2 -
mesh/node.c | 151 ++++++++++++++++++++++++++-----------
mesh/node.h | 5 ++
8 files changed, 399 insertions(+), 75 deletions(-)
--
2.19.1
From: Jakub Witowski <[email protected]>
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
---
mesh/node.c | 69 +++++++++++++++++++++++------------------------------
1 file changed, 30 insertions(+), 39 deletions(-)
diff --git a/mesh/node.c b/mesh/node.c
index d1b37a5da..d631b9324 100644
--- a/mesh/node.c
+++ b/mesh/node.c
@@ -112,8 +112,14 @@ struct mesh_node {
};
struct managed_obj_request {
- void *data;
- void *cb;
+ union {
+ const uint8_t *uuid;
+ struct mesh_node *node;
+ };
+ union {
+ node_ready_func_t ready_cb;
+ node_join_ready_func_t join_ready_cb;
+ };
struct l_dbus_message *pending_msg;
enum request_type type;
struct mesh_config_import *import;
@@ -1571,10 +1577,10 @@ static void get_managed_objects_cb(struct l_dbus_message *msg, void *user_data)
}
if (is_new) {
- node = node_new(req->data);
+ node = node_new(req->uuid);
node->elements = l_queue_new();
} else {
- node = req->data;
+ node = req->node;
}
num_ele = 0;
@@ -1644,8 +1650,6 @@ static void get_managed_objects_cb(struct l_dbus_message *msg, void *user_data)
}
if (req->type == REQUEST_TYPE_ATTACH) {
- node_ready_func_t cb = req->cb;
-
if (num_ele != node->num_ele)
goto fail;
@@ -1654,13 +1658,11 @@ static void get_managed_objects_cb(struct l_dbus_message *msg, void *user_data)
node->disc_watch = l_dbus_add_disconnect_watch(bus,
node->owner, app_disc_cb, node, NULL);
- cb(req->pending_msg, MESH_ERROR_NONE, node);
+ req->ready_cb(req->pending_msg, MESH_ERROR_NONE, node);
} else
goto fail;
} else if (req->type == REQUEST_TYPE_JOIN) {
- node_join_ready_func_t cb = req->cb;
-
if (!agent) {
l_error("Interface %s not found",
MESH_PROVISION_AGENT_INTERFACE);
@@ -1669,16 +1671,14 @@ static void get_managed_objects_cb(struct l_dbus_message *msg, void *user_data)
node->num_ele = num_ele;
set_defaults(node);
- memcpy(node->uuid, req->data, 16);
+ memcpy(node->uuid, req->uuid, 16);
if (!create_node_config(node, node->uuid))
goto fail;
- cb(node, agent);
+ req->join_ready_cb(node, agent);
} else if (req->type == REQUEST_TYPE_IMPORT) {
-
- node_ready_func_t cb = req->cb;
struct mesh_config_import *import = req->import;
struct keyring_net_key net_key;
@@ -1694,7 +1694,7 @@ static void get_managed_objects_cb(struct l_dbus_message *msg, void *user_data)
if (node->seq_number != import->node->seq_number)
node->seq_number = import->node->seq_number;
- memcpy(node->uuid, req->data, 16);
+ memcpy(node->uuid, req->uuid, 16);
if (!create_node_config(node, node->uuid))
goto fail;
@@ -1719,17 +1719,16 @@ static void get_managed_objects_cb(struct l_dbus_message *msg, void *user_data)
if (!keyring_put_net_key(node, import->net_key.idx, &net_key))
goto fail;
- cb(req->pending_msg, MESH_ERROR_NONE, node);
+ req->ready_cb(req->pending_msg, MESH_ERROR_NONE, node);
} else {
/* Callback for create node request */
- node_ready_func_t cb = req->cb;
struct keyring_net_key net_key;
uint8_t dev_key[16];
node->num_ele = num_ele;
set_defaults(node);
- memcpy(node->uuid, req->data, 16);
+ memcpy(node->uuid, req->uuid, 16);
if (!create_node_config(node, node->uuid))
goto fail;
@@ -1756,7 +1755,7 @@ static void get_managed_objects_cb(struct l_dbus_message *msg, void *user_data)
if (!keyring_put_net_key(node, PRIMARY_NET_IDX, &net_key))
goto fail;
- cb(req->pending_msg, MESH_ERROR_NONE, node);
+ req->ready_cb(req->pending_msg, MESH_ERROR_NONE, node);
}
return;
@@ -1765,26 +1764,18 @@ fail:
mesh_agent_remove(agent);
if (!is_new) {
- /* Handle failed Attach request */
- node_ready_func_t cb = req->cb;
-
free_node_dbus_resources(node);
- cb(req->pending_msg, MESH_ERROR_FAILED, node);
+ req->ready_cb(req->pending_msg, MESH_ERROR_FAILED, node);
} else {
/* Handle failed Join, Create and Import requests */
if (node)
node_remove(node);
- if (req->type == REQUEST_TYPE_JOIN) {
- node_join_ready_func_t cb = req->cb;
-
- cb(NULL, NULL);
- } else {
- node_ready_func_t cb = req->cb;
-
- cb(req->pending_msg, MESH_ERROR_FAILED, NULL);
- }
+ if (req->type == REQUEST_TYPE_JOIN)
+ req->join_ready_cb(NULL, NULL);
+ else
+ req->ready_cb(req->pending_msg, MESH_ERROR_FAILED, NULL);
}
}
@@ -1809,8 +1800,8 @@ int node_attach(const char *app_path, const char *sender, uint64_t token,
node->owner = l_strdup(sender);
req = l_new(struct managed_obj_request, 1);
- req->data = node;
- req->cb = cb;
+ req->node = node;
+ req->ready_cb = cb;
req->pending_msg = user_data;
req->type = REQUEST_TYPE_ATTACH;
@@ -1833,8 +1824,8 @@ void node_join(const char *app_path, const char *sender, const uint8_t *uuid,
l_debug("");
req = l_new(struct managed_obj_request, 1);
- req->data = (void *) uuid;
- req->cb = cb;
+ req->uuid = uuid;
+ req->join_ready_cb = cb;
req->type = REQUEST_TYPE_JOIN;
l_dbus_method_call(dbus_get_bus(), sender, app_path,
@@ -1859,8 +1850,8 @@ bool node_import(const char *app_path, const char *sender,
req = l_new(struct managed_obj_request, 1);
- req->data = (void *) uuid;
- req->cb = cb;
+ req->uuid = uuid;
+ req->ready_cb = cb;
req->pending_msg = user_data;
req->import = import;
req->type = REQUEST_TYPE_IMPORT;
@@ -1881,8 +1872,8 @@ void node_create(const char *app_path, const char *sender, const uint8_t *uuid,
l_debug("");
req = l_new(struct managed_obj_request, 1);
- req->data = (void *) uuid;
- req->cb = cb;
+ req->uuid = uuid;
+ req->ready_cb = cb;
req->pending_msg = user_data;
req->type = REQUEST_TYPE_CREATE;
--
2.19.1