summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJosé Bollo <jose.bollo@iot.bzh>2016-03-15 14:17:14 +0100
committerJosé Bollo <jose.bollo@iot.bzh>2016-03-15 14:17:14 +0100
commitc36ac6398097e09284f5d889ac2f994038a7d260 (patch)
treeb49fc0a8c33ae85bfdeeb76739c2e26e28b05b72
parent0beff6e84858ffc4468276865d3709d7ee0b1852 (diff)
utils-jbus: adds closure
Change-Id: I14466dee08429c404de400868967d8d97b2ba359 Signed-off-by: José Bollo <jose.bollo@iot.bzh>
-rw-r--r--src/afm-system-daemon.c8
-rw-r--r--src/afm-user-daemon.c44
-rw-r--r--src/utils-jbus.c54
-rw-r--r--src/utils-jbus.h8
4 files changed, 60 insertions, 54 deletions
diff --git a/src/afm-system-daemon.c b/src/afm-system-daemon.c
index 1152281..25d9456 100644
--- a/src/afm-system-daemon.c
+++ b/src/afm-system-daemon.c
@@ -66,7 +66,7 @@ const char error_bad_request[] = "\"bad request\"";
const char error_not_found[] = "\"not found\"";
const char error_cant_start[] = "\"can't start\"";
-static void on_install(struct jreq *jreq, struct json_object *req)
+static void on_install(struct jreq *jreq, struct json_object *req, void *unused)
{
const char *wgtfile;
const char *root;
@@ -113,7 +113,7 @@ static void on_install(struct jreq *jreq, struct json_object *req)
}
}
-static void on_uninstall(struct jreq *jreq, struct json_object *req)
+static void on_uninstall(struct jreq *jreq, struct json_object *req, void *unused)
{
const char *idaver;
const char *root;
@@ -222,8 +222,8 @@ int main(int ac, char **av)
ERROR("create_jbus failed");
return 1;
}
- if(jbus_add_service_j(jbus, "install", on_install)
- || jbus_add_service_j(jbus, "uninstall", on_uninstall)) {
+ if(jbus_add_service_j(jbus, "install", on_install, NULL)
+ || jbus_add_service_j(jbus, "uninstall", on_uninstall, NULL)) {
ERROR("adding services failed");
return 1;
}
diff --git a/src/afm-user-daemon.c b/src/afm-user-daemon.c
index 2615cf5..cbb51e5 100644
--- a/src/afm-user-daemon.c
+++ b/src/afm-user-daemon.c
@@ -144,7 +144,7 @@ static void reply_status(struct jreq *jreq, int status, const char *errstr)
*
* Nothing is expected in 'obj' that can be anything.
*/
-static void on_runnables(struct jreq *jreq, struct json_object *obj)
+static void on_runnables(struct jreq *jreq, struct json_object *obj, void *unused)
{
struct json_object *resp;
INFO("method runnables called");
@@ -156,7 +156,7 @@ static void on_runnables(struct jreq *jreq, struct json_object *obj)
/*
* On query "detail" from 'jreq' with parameters of 'obj'.
*/
-static void on_detail(struct jreq *jreq, struct json_object *obj)
+static void on_detail(struct jreq *jreq, struct json_object *obj, void *unused)
{
const char *appid;
struct json_object *resp;
@@ -183,7 +183,7 @@ static void on_detail(struct jreq *jreq, struct json_object *obj)
/*
* On query "start" from 'jreq' with parameters of 'obj'.
*/
-static void on_start(struct jreq *jreq, struct json_object *obj)
+static void on_start(struct jreq *jreq, struct json_object *obj, void *unused)
{
const char *appid, *modestr;
char *uri;
@@ -250,7 +250,7 @@ static void on_start(struct jreq *jreq, struct json_object *obj)
/*
* On query "stop" from 'jreq' with parameters of 'obj'.
*/
-static void on_stop(struct jreq *jreq, struct json_object *obj)
+static void on_stop(struct jreq *jreq, struct json_object *obj, void *unused)
{
int runid, status;
if (onrunid(jreq, obj, "stop", &runid)) {
@@ -262,7 +262,7 @@ static void on_stop(struct jreq *jreq, struct json_object *obj)
/*
* On query "continue" from 'jreq' with parameters of 'obj'.
*/
-static void on_continue(struct jreq *jreq, struct json_object *obj)
+static void on_continue(struct jreq *jreq, struct json_object *obj, void *unused)
{
int runid, status;
if (onrunid(jreq, obj, "continue", &runid)) {
@@ -274,7 +274,7 @@ static void on_continue(struct jreq *jreq, struct json_object *obj)
/*
* On query "terminate" from 'jreq' with parameters of 'obj'.
*/
-static void on_terminate(struct jreq *jreq, struct json_object *obj)
+static void on_terminate(struct jreq *jreq, struct json_object *obj, void *unused)
{
int runid, status;
if (onrunid(jreq, obj, "terminate", &runid)) {
@@ -286,7 +286,7 @@ static void on_terminate(struct jreq *jreq, struct json_object *obj)
/*
* On query "runners" from 'jreq' with parameters of 'obj'.
*/
-static void on_runners(struct jreq *jreq, struct json_object *obj)
+static void on_runners(struct jreq *jreq, struct json_object *obj, void *unused)
{
struct json_object *resp;
INFO("method runners called");
@@ -298,7 +298,7 @@ static void on_runners(struct jreq *jreq, struct json_object *obj)
/*
* On query "state" from 'jreq' with parameters of 'obj'.
*/
-static void on_state(struct jreq *jreq, struct json_object *obj)
+static void on_state(struct jreq *jreq, struct json_object *obj, void *unused)
{
int runid;
struct json_object *resp;
@@ -337,7 +337,7 @@ static void propagate(struct jreq *jreq, const char *msg, const char *method)
/*
* On query "install" from 'jreq' with parameters of 'msg'.
*/
-static void on_install(struct jreq *jreq, const char *msg)
+static void on_install(struct jreq *jreq, const char *msg, void *unused)
{
return propagate(jreq, msg, "install");
}
@@ -345,7 +345,7 @@ static void on_install(struct jreq *jreq, const char *msg)
/*
* On query "uninstall" from 'jreq' with parameters of 'msg'.
*/
-static void on_uninstall(struct jreq *jreq, const char *msg)
+static void on_uninstall(struct jreq *jreq, const char *msg, void *unused)
{
return propagate(jreq, msg, "uninstall");
}
@@ -353,7 +353,7 @@ static void on_uninstall(struct jreq *jreq, const char *msg)
/*
* On system signaling that applications list changed
*/
-static void on_signal_changed(struct json_object *obj)
+static void on_signal_changed(struct json_object *obj, void *unused)
{
/* update the database */
afm_db_update_applications(afdb);
@@ -481,7 +481,7 @@ int main(int ac, char **av)
}
/* observe signals of system */
- if(jbus_on_signal_j(system_bus, "changed", on_signal_changed)) {
+ if(jbus_on_signal_j(system_bus, "changed", on_signal_changed, NULL)) {
ERROR("adding signal observer failed");
return 1;
}
@@ -494,16 +494,16 @@ int main(int ac, char **av)
}
/* init services */
- if (jbus_add_service_j(user_bus, "runnables", on_runnables)
- || jbus_add_service_j(user_bus, "detail", on_detail)
- || jbus_add_service_j(user_bus, "start", on_start)
- || jbus_add_service_j(user_bus, "terminate", on_terminate)
- || jbus_add_service_j(user_bus, "stop", on_stop)
- || jbus_add_service_j(user_bus, "continue", on_continue)
- || jbus_add_service_j(user_bus, "runners", on_runners)
- || jbus_add_service_j(user_bus, "state", on_state)
- || jbus_add_service_s(user_bus, "install", on_install)
- || jbus_add_service_s(user_bus, "uninstall", on_uninstall)) {
+ if (jbus_add_service_j(user_bus, "runnables", on_runnables, NULL)
+ || jbus_add_service_j(user_bus, "detail", on_detail, NULL)
+ || jbus_add_service_j(user_bus, "start", on_start, NULL)
+ || jbus_add_service_j(user_bus, "terminate", on_terminate, NULL)
+ || jbus_add_service_j(user_bus, "stop", on_stop, NULL)
+ || jbus_add_service_j(user_bus, "continue", on_continue, NULL)
+ || jbus_add_service_j(user_bus, "runners", on_runners, NULL)
+ || jbus_add_service_j(user_bus, "state", on_state, NULL)
+ || jbus_add_service_s(user_bus, "install", on_install, NULL)
+ || jbus_add_service_s(user_bus, "uninstall", on_uninstall, NULL)) {
ERROR("adding services failed");
return 1;
}
diff --git a/src/utils-jbus.c b/src/utils-jbus.c
index ab219c7..6d01b1e 100644
--- a/src/utils-jbus.c
+++ b/src/utils-jbus.c
@@ -45,16 +45,18 @@ struct jreq {
struct jservice {
struct jservice *next;
char *method;
- void (*oncall_s)(struct jreq *, const char *);
- void (*oncall_j)(struct jreq *, struct json_object *);
+ void (*oncall_s)(struct jreq *, const char *, void *);
+ void (*oncall_j)(struct jreq *, struct json_object *, void *);
+ void *data;
};
/* structure for signal handlers */
struct jsignal {
struct jsignal *next;
char *name;
- void (*onsignal_s)(const char *);
- void (*onsignal_j)(struct json_object *);
+ void (*onsignal_s)(const char *, void *);
+ void (*onsignal_j)(struct json_object *, void *);
+ void *data;
};
/* structure for recording asynchronous requests */
@@ -120,8 +122,9 @@ static int matchitf(struct jbus *jbus, DBusMessage *message)
static int add_service(
struct jbus *jbus,
const char *method,
- void (*oncall_s)(struct jreq*, const char*),
- void (*oncall_j)(struct jreq*, struct json_object*)
+ void (*oncall_s)(struct jreq*, const char*, void*),
+ void (*oncall_j)(struct jreq*, struct json_object*, void*),
+ void *data
)
{
struct jservice *srv;
@@ -141,6 +144,7 @@ static int add_service(
/* record the service */
srv->oncall_s = oncall_s;
srv->oncall_j = oncall_j;
+ srv->data = data;
srv->next = jbus->services;
jbus->services = srv;
@@ -155,8 +159,9 @@ error:
static int add_signal(
struct jbus *jbus,
const char *name,
- void (*onsignal_s)(const char*),
- void (*onsignal_j)(struct json_object*)
+ void (*onsignal_s)(const char*, void*),
+ void (*onsignal_j)(struct json_object*, void*),
+ void *data
)
{
char *rule;
@@ -185,6 +190,7 @@ static int add_signal(
/* record the signal */
sig->onsignal_s = onsignal_s;
sig->onsignal_j = onsignal_j;
+ sig->data = data;
sig->next = jbus->signals;
jbus->signals = sig;
@@ -335,13 +341,13 @@ static DBusHandlerResult incoming_call(DBusConnection *connection, DBusMessage *
return reply_invalid_request(jreq);
if (srv->oncall_s) {
/* handling strings only */
- srv->oncall_s(jreq, str);
+ srv->oncall_s(jreq, str, srv->data);
}
else {
/* handling json only */
if (!parse(jbus, str, &query))
return reply_invalid_request(jreq);
- srv->oncall_j(jreq, query);
+ srv->oncall_j(jreq, query, srv->data);
json_object_put(query);
}
return DBUS_HANDLER_RESULT_HANDLED;
@@ -370,12 +376,12 @@ static DBusHandlerResult incoming_signal(DBusConnection *connection, DBusMessage
if (dbus_message_get_args(message, NULL, DBUS_TYPE_STRING, &str, DBUS_TYPE_INVALID)) {
if (sig->onsignal_s) {
/* handling strings only */
- sig->onsignal_s(str);
+ sig->onsignal_s(str, sig->data);
}
else {
/* handling json only */
if (parse(jbus, str, &obj)) {
- sig->onsignal_j(obj);
+ sig->onsignal_j(obj, sig->data);
json_object_put(obj);
}
}
@@ -630,14 +636,14 @@ int jbus_send_signal_j(struct jbus *jbus, const char *name, struct json_object *
return jbus_send_signal_s(jbus, name, str);
}
-int jbus_add_service_s(struct jbus *jbus, const char *method, void (*oncall)(struct jreq *, const char *))
+int jbus_add_service_s(struct jbus *jbus, const char *method, void (*oncall)(struct jreq *, const char *, void *), void *data)
{
- return add_service(jbus, method, oncall, NULL);
+ return add_service(jbus, method, oncall, NULL, data);
}
-int jbus_add_service_j(struct jbus *jbus, const char *method, void (*oncall)(struct jreq *, struct json_object *))
+int jbus_add_service_j(struct jbus *jbus, const char *method, void (*oncall)(struct jreq *, struct json_object *, void *), void *data)
{
- return add_service(jbus, method, NULL, oncall);
+ return add_service(jbus, method, NULL, oncall, data);
}
int jbus_start_serving(struct jbus *jbus)
@@ -812,14 +818,14 @@ struct json_object *jbus_call_jj_sync(struct jbus *jbus, const char *method, str
return jbus_call_sj_sync(jbus, method, str);
}
-int jbus_on_signal_s(struct jbus *jbus, const char *name, void (*onsig)(const char *))
+int jbus_on_signal_s(struct jbus *jbus, const char *name, void (*onsig)(const char *, void *), void *data)
{
- return add_signal(jbus, name, onsig, NULL);
+ return add_signal(jbus, name, onsig, NULL, data);
}
-int jbus_on_signal_j(struct jbus *jbus, const char *name, void (*onsig)(struct json_object *))
+int jbus_on_signal_j(struct jbus *jbus, const char *name, void (*onsig)(struct json_object *, void *), void *data)
{
- return add_signal(jbus, name, NULL, onsig);
+ return add_signal(jbus, name, NULL, onsig, data);
}
/************************** FEW LITTLE TESTS *****************************************/
@@ -828,13 +834,13 @@ int jbus_on_signal_j(struct jbus *jbus, const char *name, void (*onsig)(struct j
#include <stdio.h>
#include <unistd.h>
struct jbus *jbus;
-void ping(struct jreq *jreq, struct json_object *request)
+void ping(struct jreq *jreq, struct json_object *request, void *unused)
{
printf("ping(%s) -> %s\n",json_object_to_json_string(request),json_object_to_json_string(request));
jbus_reply_j(jreq, request);
json_object_put(request);
}
-void incr(struct jreq *jreq, struct json_object *request)
+void incr(struct jreq *jreq, struct json_object *request, void *unused)
{
static int counter = 0;
struct json_object *res = json_object_new_int(++counter);
@@ -848,8 +854,8 @@ int main()
{
int s1, s2, s3;
jbus = create_jbus(1, "/bzh/iot/jdbus");
- s1 = jbus_add_service_j(jbus, "ping", ping);
- s2 = jbus_add_service_j(jbus, "incr", incr);
+ s1 = jbus_add_service_j(jbus, "ping", ping, NULL);
+ s2 = jbus_add_service_j(jbus, "incr", incr, NULL);
s3 = jbus_start_serving(jbus);
printf("started %d %d %d\n", s1, s2, s3);
while (!jbus_read_write_dispatch (jbus, -1));
diff --git a/src/utils-jbus.h b/src/utils-jbus.h
index 973c158..11cb8ed 100644
--- a/src/utils-jbus.h
+++ b/src/utils-jbus.h
@@ -46,8 +46,8 @@ extern char *jbus_call_js_sync(struct jbus *jbus, const char *method, struct jso
extern struct json_object *jbus_call_sj_sync(struct jbus *jbus, const char *method, const char *query);
extern struct json_object *jbus_call_jj_sync(struct jbus *jbus, const char *method, struct json_object *query);
-extern int jbus_on_signal_s(struct jbus *jbus, const char *name, void (*onsignal)(const char *));
-extern int jbus_on_signal_j(struct jbus *jbus, const char *name, void (*onsignal)(struct json_object *));
+extern int jbus_on_signal_s(struct jbus *jbus, const char *name, void (*onsignal)(const char *, void *), void *data);
+extern int jbus_on_signal_j(struct jbus *jbus, const char *name, void (*onsignal)(struct json_object *, void *), void *data);
/* verbs for servers */
extern int jbus_reply_s(struct jreq *jreq, const char *reply);
@@ -55,8 +55,8 @@ extern int jbus_reply_j(struct jreq *jreq, struct json_object *reply);
extern int jbus_reply_error_s(struct jreq *jreq, const char *reply);
extern int jbus_reply_error_j(struct jreq *jreq, struct json_object *reply);
-extern int jbus_add_service_s(struct jbus *jbus, const char *method, void (*oncall)(struct jreq *, const char *));
-extern int jbus_add_service_j(struct jbus *jbus, const char *method, void (*oncall)(struct jreq *, struct json_object *));
+extern int jbus_add_service_s(struct jbus *jbus, const char *method, void (*oncall)(struct jreq *, const char *, void *), void *data);
+extern int jbus_add_service_j(struct jbus *jbus, const char *method, void (*oncall)(struct jreq *, struct json_object *, void *), void *data);
extern int jbus_start_serving(struct jbus *jbus);
extern int jbus_send_signal_s(struct jbus *jbus, const char *name, const char *content);
extern int jbus_send_signal_j(struct jbus *jbus, const char *name, struct json_object *content);