-static int call(
- struct jbus *jbus,
- const char *method,
- const char *query,
- void (*onresp_s)(int status, const char *response, void *data),
- void (*onresp_j)(int status, struct json_object *response, void *data),
- void *data
-)
-{
- DBusMessage *msg;
- struct jrespw *resp;
-
- resp = malloc(sizeof * resp);
- if (resp == NULL) {
- errno = ENOMEM;
- goto error;
- }
-
- msg = dbus_message_new_method_call(jbus->name, jbus->path, jbus->name, method);
- if (msg == NULL) {
- errno = ENOMEM;
- goto error2;
- }
-
- if (!dbus_message_append_args(msg, DBUS_TYPE_STRING, &query, DBUS_TYPE_INVALID)) {
- errno = ENOMEM;
- goto error3;
- }
-
- if (!dbus_connection_send(jbus->connection, msg, &resp->serial)) {
- goto error3;
- }
-
- dbus_message_unref(msg);
- resp->data = data;
- resp->onresp_s = onresp_s;
- resp->onresp_j = onresp_j;
- resp->next = jbus->waiters;
- jbus->waiters = resp;
- return 0;
-
-error3:
- dbus_message_unref(msg);
-error2:
- free(resp);
-error:
- return -1;
-}
-
-static void sync_of_replies(int status, const char *value, void *data)
-{
- struct respsync *s = data;
- s->value = status ? NULL : strdup(value ? value : "");
- s->replied = 1;
-}
-
-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)