diff --git a/mesh/mesh.c b/mesh/mesh.c
index e7eef04..a084f92 100644
--- a/mesh/mesh.c
+++ b/mesh/mesh.c
uint8_t *uuid;
};
-struct attach_data {
- uint64_t token;
- struct l_dbus_message *msg;
- const char *app;
-};
-
static struct bt_mesh mesh;
static struct l_queue *controllers;
static struct mgmt *mgmt_mesh;
/* We allow only one outstanding Join request */
static struct join_data *join_pending;
-/* Pending Attach requests */
-static struct l_queue *attach_queue;
+/* Pending method requests */
+static struct l_queue *pending_queue;
static bool simple_match(const void *a, const void *b)
{
return true;
}
-static void attach_exit(void *data)
+static void pending_request_exit(void *data)
{
struct l_dbus_message *reply;
- struct attach_data *pending = data;
+ struct l_dbus_message *msg = data;
- reply = dbus_error(pending->msg, MESH_ERROR_FAILED, "Failed. Exiting");
+ reply = dbus_error(msg, MESH_ERROR_FAILED, "Failed. Exiting");
l_dbus_send(dbus_get_bus(), reply);
- l_free(pending);
}
static void free_pending_join_call(bool failed)
free_pending_join_call(true);
}
- l_queue_destroy(attach_queue, attach_exit);
+ l_queue_destroy(pending_queue, pending_request_exit);
node_cleanup_all();
mesh_model_cleanup();
return reply;
}
-static bool match_attach_request(const void *a, const void *b)
-{
- const struct attach_data *pending = a;
- const uint64_t *token = b;
-
- return *token == pending->token;
-}
-
-static void attach_ready_cb(int status, char *node_path, uint64_t token)
+static void attach_ready_cb(void *user_data, int status, struct mesh_node *node)
{
struct l_dbus_message *reply;
- struct attach_data *pending;
+ struct l_dbus_message *pending_msg;
- pending = l_queue_find(attach_queue, match_attach_request, &token);
- if (!pending)
+ pending_msg = l_queue_find(pending_queue, simple_match, user_data);
+ if (!pending_msg)
return;
if (status != MESH_ERROR_NONE) {
const char *desc = (status == MESH_ERROR_NOT_FOUND) ?
"Node match not found" : "Attach failed";
- reply = dbus_error(pending->msg, status, desc);
+ reply = dbus_error(pending_msg, status, desc);
goto done;
}
- reply = l_dbus_message_new_method_return(pending->msg);
+ reply = l_dbus_message_new_method_return(pending_msg);
- node_build_attach_reply(reply, token);
+ node_build_attach_reply(node, reply);
done:
l_dbus_send(dbus_get_bus(), reply);
- l_queue_remove(attach_queue, pending);
- l_free(pending);
+ l_queue_remove(pending_queue, pending_msg);
}
static struct l_dbus_message *attach_call(struct l_dbus *dbus,
{
uint64_t token;
const char *app_path, *sender;
- struct attach_data *pending;
+ struct l_dbus_message *pending_msg;
+ int status;
l_debug("Attach");
sender = l_dbus_message_get_sender(msg);
- if (node_attach(app_path, sender, token, attach_ready_cb) !=
- MESH_ERROR_NONE)
- return dbus_error(msg, MESH_ERROR_NOT_FOUND,
- "Matching node not found");
+ pending_msg = l_dbus_message_ref(msg);
+ if (!pending_queue)
+ pending_queue = l_queue_new();
- pending = l_new(struct attach_data, 1);
+ l_queue_push_tail(pending_queue, pending_msg);
- pending->token = token;
- pending->msg = l_dbus_message_ref(msg);
+ status = node_attach(app_path, sender, token, attach_ready_cb,
+ pending_msg);
+ if (status == MESH_ERROR_NONE)
+ return NULL;
- if (!attach_queue)
- attach_queue = l_queue_new();
+ l_queue_remove(pending_queue, pending_msg);
- l_queue_push_tail(attach_queue, pending);
-
- return NULL;
+ return dbus_error(msg, status, NULL);
}
static struct l_dbus_message *leave_call(struct l_dbus *dbus,
diff --git a/mesh/node.c b/mesh/node.c
index a6c9332..a9eb41e 100644
--- a/mesh/node.c
+++ b/mesh/node.c
};
struct attach_obj_request {
- node_attach_ready_func_t cb;
+ node_ready_func_t cb;
struct mesh_node *node;
+ void *user_data;
};
struct join_obj_request {
struct attach_obj_request *req = user_data;
struct mesh_node *node = req->node;
const char *path;
- uint64_t token = l_get_be64(node->token);
uint8_t num_ele;
if (l_dbus_message_is_error(msg)) {
node->disc_watch = l_dbus_add_disconnect_watch(bus, node->owner,
app_disc_cb, node, NULL);
- req->cb(MESH_ERROR_NONE, node->path, token);
+ req->cb(req->user_data, MESH_ERROR_NONE, node);
return;
}
fail:
- req->cb(MESH_ERROR_FAILED, NULL, token);
+ req->cb(req->user_data, MESH_ERROR_FAILED, NULL);
l_queue_foreach(node->elements, free_element_path, NULL);
l_free(node->app_path);
/* Establish relationship between application and mesh node */
int node_attach(const char *app_path, const char *sender, uint64_t token,
- node_attach_ready_func_t cb)
+ node_ready_func_t cb, void *user_data)
{
struct attach_obj_request *req;
struct mesh_node *node;
req = l_new(struct attach_obj_request, 1);
req->node = node;
req->cb = cb;
+ req->user_data = user_data;
l_dbus_method_call(dbus_get_bus(), sender, app_path,
L_DBUS_INTERFACE_OBJECT_MANAGER,
l_dbus_message_builder_leave_struct(builder);
}
-void node_build_attach_reply(struct l_dbus_message *reply, uint64_t token)
+void node_build_attach_reply(struct mesh_node *node,
+ struct l_dbus_message *reply)
{
- struct mesh_node *node;
struct l_dbus_message_builder *builder;
- node = l_queue_find(nodes, match_token, &token);
- if (!node)
- return;
-
builder = l_dbus_message_builder_new(reply);
/* Node object path */
diff --git a/mesh/node.h b/mesh/node.h
index ebc82ff..20b6009 100644
--- a/mesh/node.h
+++ b/mesh/node.h
#define MIN_SEQ_CACHE (2*MIN_SEQ_TRIGGER)
#define MIN_SEQ_CACHE_TIME (5*60)
-typedef void (*node_attach_ready_func_t) (int status, char *node_path,
- uint64_t token);
+typedef void (*node_ready_func_t) (void *user_data, int status,
+ struct mesh_node *node);
typedef void (*node_join_ready_func_t) (struct mesh_node *node,
struct mesh_agent *agent);
struct mesh_io *io);
void node_attach_io(struct mesh_io *io);
int node_attach(const char *app_path, const char *sender, uint64_t token,
- node_attach_ready_func_t cb);
-void node_build_attach_reply(struct l_dbus_message *reply, uint64_t token);
+ node_ready_func_t cb, void *user_data);
+void node_build_attach_reply(struct mesh_node *node,
+ struct l_dbus_message *reply);
void node_id_set(struct mesh_node *node, uint16_t node_id);
uint16_t node_id_get(struct mesh_node *node);
bool node_dbus_init(struct l_dbus *bus);