/*
Copyright 2015 IoT.bzh
+ author: José Bollo <jose.bollo@iot.bzh>
+
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
static const char reply_invalid[] = "{\"status\":\"invalid request\"}";
static const char interface_jbus[] = "org.jbus";
-static int send_reply(struct jreq *jreq, const char *reply)
-{
- int rc = -1;
- if (dbus_message_append_args(jreq->reply, DBUS_TYPE_STRING, &reply, DBUS_TYPE_INVALID)) {
- if (dbus_connection_send(jreq->connection, jreq->reply, NULL))
- rc = 0;
- }
- dbus_message_unref(jreq->reply);
- dbus_connection_unref(jreq->connection);
- free(jreq);
- return rc;
-}
-
static DBusHandlerResult incoming_resp(DBusConnection *connection, DBusMessage *message, struct jbus *jbus)
{
int status;
dbus_uint32_t serial;
/* search for the waiter */
- serial = dbus_message_get_serial(message);
+ serial = dbus_message_get_reply_serial(message);
prv = &jbus->waiters;
while ((jrw = *prv) != NULL && jrw->serial != serial)
prv = &jrw->next;
/* retrieve the json value */
if (!dbus_message_get_args(message, NULL, DBUS_TYPE_STRING, &str, DBUS_TYPE_INVALID)) {
- send_reply(jreq, reply_invalid);
+ jbus_replyj(jreq, reply_invalid);
return DBUS_HANDLER_RESULT_HANDLED;
}
query = json_tokener_parse(str);
if (query == NULL) {
- send_reply(jreq, reply_invalid);
+ jbus_replyj(jreq, reply_invalid);
return DBUS_HANDLER_RESULT_HANDLED;
}
}
}
+int jbus_replyj(struct jreq *jreq, const char *reply)
+{
+ int rc = -1;
+ if (dbus_message_append_args(jreq->reply, DBUS_TYPE_STRING, &reply, DBUS_TYPE_INVALID)) {
+ if (dbus_connection_send(jreq->connection, jreq->reply, NULL))
+ rc = 0;
+ }
+ dbus_message_unref(jreq->reply);
+ dbus_connection_unref(jreq->connection);
+ free(jreq);
+ return rc;
+}
+
int jbus_reply(struct jreq *jreq, struct json_object *reply)
{
const char *str = json_object_to_json_string(reply);
- return send_reply(jreq, str ? str : reply_out_of_memory);
+ return jbus_replyj(jreq, str ? str : reply_out_of_memory);
}
int jbus_add_service(struct jbus *jbus, const char *method, void (*oncall)(struct jreq *jreq, struct json_object *request))
return 0;
-error3:
- free(srv->method);
error2:
free(srv);
error:
return 0;
case DBUS_REQUEST_NAME_REPLY_EXISTS:
case DBUS_REQUEST_NAME_REPLY_IN_QUEUE:
+ default:
errno = EADDRINUSE;
return -1;
}
}
+int jbus_read_write_dispatch(struct jbus *jbus, int toms)
+{
+ if (dbus_connection_read_write_dispatch(jbus->connection, toms));
+ return 0;
+ errno = EPIPE;
+ return -1;
+}
+
int jbus_callj(struct jbus *jbus, const char *method, const char *query, void (*onresp)(int status, struct json_object *response, void *data), void *data)
{
- int rc;
DBusMessage *msg;
struct jrespw *resp;
int s2 = jbus_add_service(jbus, "incr", incr);
int s3 = jbus_start_serving(jbus);
printf("started %d %d %d\n", s1, s2, s3);
- while (dbus_connection_read_write_dispatch (jbus->connection, -1))
+ while (!jbus_read_write_dispatch (jbus, -1))
;
}
#endif
while(i--) {
jbus_callj(jbus, "ping", "{\"toto\":[1,2,3,4,true,\"toto\"]}", onresp, "ping");
jbus_callj(jbus, "incr", "{\"doit\":\"for-me\"}", onresp, "incr");
- dbus_connection_read_write_dispatch (jbus->connection, 1);
+ jbus_read_write_dispatch (jbus, 1);
}
- while (dbus_connection_read_write_dispatch (jbus->connection, -1))
+ while (!jbus_read_write_dispatch (jbus, -1))
;
}
#endif