-static DBusHandlerResult incoming_resp(DBusConnection *connection, DBusMessage *message, struct jbus *jbus, int iserror)
-{
- int status;
- const char *str;
- struct jrespw *jrw, **prv;
- struct json_object *reply;
- dbus_uint32_t serial;
-
- /* search for the waiter */
- serial = dbus_message_get_reply_serial(message);
- prv = &jbus->waiters;
- while ((jrw = *prv) != NULL && jrw->serial != serial)
- prv = &jrw->next;
- if (jrw == NULL)
- return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
- *prv = jrw->next;
-
- /* retrieve the string value */
- if (!dbus_message_get_args(message, NULL, DBUS_TYPE_STRING, &str, DBUS_TYPE_INVALID)) {
- status = -1;
- str = NULL;
- reply = NULL;
- }
-
- /* treat it */
- if (jrw->onresp_s)
- jrw->onresp_s(iserror ? -1 : status, str, jrw->data);
- else {
- reply = json_tokener_parse(str);
- status = reply ? 0 : -1;
- jrw->onresp_j(iserror ? -1 : status, reply, jrw->data);
- json_object_put(reply);
- }
-
- free(jrw);
- return DBUS_HANDLER_RESULT_HANDLED;
-}
-
-static DBusHandlerResult incoming_call(DBusConnection *connection, DBusMessage *message, struct jbus *jbus)
-{
- struct jservice *srv;
- struct jreq *jreq;
- const char *str;
- const char *method;
- struct json_object *query;
-
- /* search for the service */
- if (!matchitf(jbus, message))
- return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
- method = dbus_message_get_member(message);
- if (method == NULL)
- return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
- srv = jbus->services;
- while(srv != NULL && strcmp(method, srv->method))
- srv = srv->next;
- if (srv == NULL)
- return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
-
- /* handle the message */
- jreq = malloc(sizeof * jreq);
- if (jreq == NULL)
- return DBUS_HANDLER_RESULT_NEED_MEMORY;
- jreq->request = dbus_message_ref(message);
- jreq->connection = dbus_connection_ref(jbus->connection);
-
- /* retrieve the string value */
- if (!dbus_message_get_args(message, NULL, DBUS_TYPE_STRING, &str, DBUS_TYPE_INVALID))
- return reply_invalid_request(jreq);
- if (srv->oncall_s) {
- /* handling strings only */
- srv->oncall_s(jreq, str);
- }
- else {
- /* handling json only */
- query = json_tokener_parse(str);
- if (query == NULL)
- return reply_invalid_request(jreq);
- srv->oncall_j(jreq, query);
- json_object_put(query);
- }
- return DBUS_HANDLER_RESULT_HANDLED;
-}
-
-static DBusHandlerResult incoming_signal(DBusConnection *connection, DBusMessage *message, struct jbus *jbus)
-{
- struct jsignal *sig;
- const char *str;
- const char *name;
- struct json_object *obj;
-
- /* search for the service */
- if (!matchitf(jbus, message))
- return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
- name = dbus_message_get_member(message);
- if (name == NULL)
- return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
- sig = jbus->signals;
- while(sig != NULL && strcmp(name, sig->name))
- sig = sig->next;
- if (sig == NULL)
- return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
-
- /* retrieve the string value */
- 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);
- }
- else {
- /* handling json only */
- obj = json_tokener_parse(str);
- if (obj != NULL) {
- sig->onsignal_j(obj);
- json_object_put(obj);
- }
- }
- }
- return DBUS_HANDLER_RESULT_HANDLED;
-}
-
-static DBusHandlerResult incoming(DBusConnection *connection, DBusMessage *message, void *data)
-{
- switch(dbus_message_get_type(message)) {
- case DBUS_MESSAGE_TYPE_METHOD_CALL:
- return incoming_call(connection, message, (struct jbus*)data);
- case DBUS_MESSAGE_TYPE_METHOD_RETURN:
- return incoming_resp(connection, message, (struct jbus*)data, 0);
- case DBUS_MESSAGE_TYPE_ERROR:
- return incoming_resp(connection, message, (struct jbus*)data, 1);
- case DBUS_MESSAGE_TYPE_SIGNAL:
- return incoming_signal(connection, message, (struct jbus*)data);
- }
- return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
-}
-
-/************************** MAIN FUNCTIONS *****************************************/
-
-struct jbus *create_jbus(int session, const char *path)
+/*
+ * Creates a 'jbus' bound the 'path' and it derived names and linked
+ * either to the DBUS SYSTEM when 'session' is nul or to the DBUS SESSION
+ * if 'session' is not nul.
+ *
+ * The parameter 'path' is intended to be the path of a DBUS single object.
+ * Single means that it exists only one instance of the object on the
+ * given bus. That path implies 2 derived DBUS names:
+ * 1. the destination name of the program that handles the object
+ * 2. the interface name of the object
+ * These names are derived by removing the heading slash (/) and
+ * by replacing all occurences of slashes by dots.
+ * For example, passing path = /a/b/c means that the object /a/b/c is
+ * handled by the destination a.b.c and replies to the interface a.b.c
+ *
+ * Returns the created jbus or NULL in case of error.
+ */
+struct jbus *create_jbus(struct sbus *sbus, const char *path)