/*
- * Copyright (C) 2015, 2016 "IoT.bzh"
+ * Copyright (C) 2015, 2016, 2017 "IoT.bzh"
* Author José Bollo <jose.bollo@iot.bzh>
*
* Licensed under the Apache License, Version 2.0 (the "License");
#include "afb-common.h"
-#include "session.h"
+#include "afb-session.h"
#include "afb-msg-json.h"
#include "afb-apis.h"
#include "afb-api-so.h"
#include "afb-context.h"
#include "afb-evt.h"
-#include "afb-subcall.h"
+#include "afb-xreq.h"
#include "verbose.h"
static const char DEFAULT_PATH_PREFIX[] = "/org/agl/afb/api/";
#define RETOK 1
#define RETERR 2
-#define RETRAW 3
/******************* common part **********************************/
struct dbus_memo {
struct dbus_memo *next; /* the next memo */
struct api_dbus *api; /* the dbus api */
- struct afb_req req; /* the request handle */
- struct afb_context *context; /* the context of the query */
+ struct afb_xreq *xreq; /* the request */
uint64_t msgid; /* the message identifier */
};
};
/* allocates and init the memorizing data */
-static struct dbus_memo *api_dbus_client_memo_make(struct api_dbus *api, struct afb_req req, struct afb_context *context)
+static struct dbus_memo *api_dbus_client_memo_make(struct api_dbus *api, struct afb_xreq *xreq)
{
struct dbus_memo *memo;
memo = malloc(sizeof *memo);
if (memo != NULL) {
- afb_req_addref(req);
- memo->req = req;
- memo->context = context;
+ afb_xreq_addref(xreq);
+ memo->xreq = xreq;
memo->msgid = 0;
memo->api = api;
memo->next = api->client.memos;
prv = &(*prv)->next;
}
- afb_req_unref(memo->req);
+ afb_xreq_unref(memo->xreq);
free(memo);
}
rc = sd_bus_message_read(message, "yssu", &type, &first, &second, &flags);
if (rc < 0) {
/* failing to have the answer */
- afb_req_fail(memo->req, "error", "dbus error");
+ afb_xreq_fail(memo->xreq, "error", "dbus error");
} else {
/* report the answer */
- memo->context->flags = (unsigned)flags;
+ memo->xreq->context.flags = (unsigned)flags;
switch(type) {
case RETOK:
- afb_req_success(memo->req, json_tokener_parse(first), *second ? second : NULL);
+ afb_xreq_success(memo->xreq, json_tokener_parse(first), *second ? second : NULL);
break;
case RETERR:
- afb_req_fail(memo->req, first, *second ? second : NULL);
- break;
- case RETRAW:
- afb_req_send(memo->req, first, strlen(first));
+ afb_xreq_fail(memo->xreq, first, *second ? second : NULL);
break;
default:
- afb_req_fail(memo->req, "error", "dbus link broken");
+ afb_xreq_fail(memo->xreq, "error", "dbus link broken");
break;
}
}
}
/* on call, propagate it to the dbus service */
-static void api_dbus_client_call(struct api_dbus *api, struct afb_req req, struct afb_context *context, const char *verb, size_t lenverb)
+static void api_dbus_client_call(void *closure, struct afb_xreq *xreq)
{
+ struct api_dbus *api = closure;
size_t size;
int rc;
- char *method = strndupa(verb, lenverb);
struct dbus_memo *memo;
struct sd_bus_message *msg;
/* create the recording data */
- memo = api_dbus_client_memo_make(api, req, context);
+ memo = api_dbus_client_memo_make(api, xreq);
if (memo == NULL) {
- afb_req_fail(req, "error", "out of memory");
+ afb_xreq_fail(xreq, "error", "out of memory");
return;
}
/* creates the message */
msg = NULL;
- rc = sd_bus_message_new_method_call(api->sdbus, &msg, api->name, api->path, api->name, method);
+ rc = sd_bus_message_new_method_call(api->sdbus, &msg, api->name, api->path, api->name, xreq->verb);
if (rc < 0)
goto error;
rc = sd_bus_message_append(msg, "ssu",
- afb_req_raw(req, &size),
- ctxClientGetUuid(context->session),
- (uint32_t)context->flags);
+ afb_xreq_raw(xreq, &size),
+ afb_session_uuid(xreq->context.session),
+ (uint32_t)xreq->context.flags);
if (rc < 0)
goto error;
error:
/* if there was an error report it directly */
errno = -rc;
- afb_req_fail(req, "error", "dbus error");
+ afb_xreq_fail(xreq, "error", "dbus error");
api_dbus_client_memo_destroy(memo);
end:
sd_bus_message_unref(msg);
}
-static int api_dbus_service_start(struct api_dbus *api, int share_session, int onneed)
+static int api_dbus_service_start(void *closure, int share_session, int onneed)
{
+ struct api_dbus *api = closure;
+
/* not an error when onneed */
if (onneed != 0)
return 0;
}
/* subscribe the request to the event */
- rc = afb_req_subscribe(memo->req, ev->event);
+ rc = afb_xreq_subscribe(memo->xreq, ev->event);
if (rc < 0)
ERROR("can't subscribe: %m");
}
}
/* unsubscribe the request from the event */
- rc = afb_req_unsubscribe(memo->req, ev->event);
+ rc = afb_xreq_unsubscribe(memo->xreq, ev->event);
if (rc < 0)
ERROR("can't unsubscribe: %m");
}
return 1;
}
+static struct afb_api_itf dbus_api_itf = {
+ .call = api_dbus_client_call,
+ .service_start = api_dbus_service_start
+};
+
/* adds a afb-dbus-service client api */
int afb_api_dbus_add_client(const char *path)
{
/* record it as an API */
afb_api.closure = api;
- afb_api.call = (void*)api_dbus_client_call;
- afb_api.service_start = (void*)api_dbus_service_start;
+ afb_api.itf = &dbus_api_itf;
if (afb_apis_add(api->api, afb_api) < 0)
goto error2;
free(listener);
}
-static struct listener *afb_api_dbus_server_listerner_get(struct api_dbus *api, const char *sender, struct AFB_clientCtx *session)
+static struct listener *afb_api_dbus_server_listerner_get(struct api_dbus *api, const char *sender, struct afb_session *session)
{
int rc;
struct listener *listener;
return NULL;
/* retrieves the stored listener */
- listener = ctxClientCookieGet(session, destination);
+ listener = afb_session_get_cookie(session, destination);
if (listener != NULL) {
/* found */
afb_api_dbus_server_destination_unref(destination);
listener->destination = destination;
listener->listener = afb_evt_listener_create(&evt_push_itf, destination);
if (listener->listener != NULL) {
- rc = ctxClientCookieSet(session, destination, listener, (void*)afb_api_dbus_server_listener_free);
+ rc = afb_session_set_cookie(session, destination, listener, (void*)afb_api_dbus_server_listener_free);
if (rc == 0)
return listener;
afb_evt_listener_unref(listener->listener);
/******************* dbus request part for server *****************/
-/*
- * structure for a dbus request
+/**
+ * Structure for a dbus request
*/
struct dbus_req {
- struct afb_context context; /* the context, should be THE FIRST */
- sd_bus_message *message; /* the incoming request message */
- const char *request; /* the readen request as string */
- struct json_object *json; /* the readen request as object */
- struct listener *listener; /* the listener for events */
- int refcount; /* reference count of the request */
+ struct afb_xreq xreq; /**< the xreq of the request */
+ sd_bus_message *message; /**< the incoming request message */
+ const char *request; /**< the readen request as string */
+ struct json_object *json; /**< the readen request as object */
+ struct listener *listener; /**< the listener for events */
};
-/* increment the reference count of the request */
-static void dbus_req_addref(struct dbus_req *dreq)
-{
- dreq->refcount++;
-}
-
/* decrement the reference count of the request and free/release it on falling to null */
-static void dbus_req_unref(struct dbus_req *dreq)
+static void dbus_req_destroy(void *closure)
{
- if (dreq == NULL || --dreq->refcount)
- return;
+ struct dbus_req *dreq = closure;
- afb_context_disconnect(&dreq->context);
+ afb_context_disconnect(&dreq->xreq.context);
json_object_put(dreq->json);
sd_bus_message_unref(dreq->message);
free(dreq);
}
/* get the object of the request */
-static struct json_object *dbus_req_json(struct dbus_req *dreq)
+static struct json_object *dbus_req_json(void *closure)
{
- if (dreq->json == NULL) {
- dreq->json = json_tokener_parse(dreq->request);
- if (dreq->json == NULL) {
- /* lazy error detection of json request. Is it to improve? */
- dreq->json = json_object_new_string(dreq->request);
- }
- }
+ struct dbus_req *dreq = closure;
+
return dreq->json;
}
/* get the argument of the request of 'name' */
-static struct afb_arg dbus_req_get(struct dbus_req *dreq, const char *name)
-{
- return afb_msg_json_get_arg(dbus_req_json(dreq), name);
-}
-
static void dbus_req_reply(struct dbus_req *dreq, uint8_t type, const char *first, const char *second)
{
int rc;
rc = sd_bus_reply_method_return(dreq->message,
- "yssu", type, first ? : "", second ? : "", (uint32_t)dreq->context.flags);
+ "yssu", type, first ? : "", second ? : "", (uint32_t)dreq->xreq.context.flags);
if (rc < 0)
ERROR("sending the reply failed");
}
-static void dbus_req_success(struct dbus_req *dreq, struct json_object *obj, const char *info)
+static void dbus_req_success(void *closure, struct json_object *obj, const char *info)
{
- dbus_req_reply(dreq, RETOK, json_object_to_json_string_ext(obj, JSON_C_TO_STRING_PLAIN), info);
-}
+ struct dbus_req *dreq = closure;
-static void dbus_req_fail(struct dbus_req *dreq, const char *status, const char *info)
-{
- dbus_req_reply(dreq, RETERR, status, info);
+ dbus_req_reply(dreq, RETOK, json_object_to_json_string_ext(obj, JSON_C_TO_STRING_PLAIN), info);
}
-static const char *dbus_req_raw(struct dbus_req *dreq, size_t *size)
+static void dbus_req_fail(void *closure, const char *status, const char *info)
{
- if (size != NULL)
- *size = strlen(dreq->request);
- return dreq->request;
-}
+ struct dbus_req *dreq = closure;
-static void dbus_req_send(struct dbus_req *dreq, const char *buffer, size_t size)
-{
- /* TODO: how to put sized buffer as strings? things aren't clear here!!! */
- dbus_req_reply(dreq, RETRAW, buffer, "");
+ dbus_req_reply(dreq, RETERR, status, info);
}
static void afb_api_dbus_server_event_send(struct destination *destination, char order, const char *event, int eventid, const char *data, uint64_t msgid);
-static int dbus_req_subscribe(struct dbus_req *dreq, struct afb_event event)
+static int dbus_req_subscribe(void *closure, struct afb_event event)
{
+ struct dbus_req *dreq = closure;
uint64_t msgid;
int rc;
return rc;
}
-static int dbus_req_unsubscribe(struct dbus_req *dreq, struct afb_event event)
+static int dbus_req_unsubscribe(void *closure, struct afb_event event)
{
+ struct dbus_req *dreq = closure;
uint64_t msgid;
int rc;
return rc;
}
-static void dbus_req_subcall(struct dbus_req *dreq, const char *api, const char *verb, struct json_object *args, void (*callback)(void*, int, struct json_object*), void *closure);
-
-const struct afb_req_itf afb_api_dbus_req_itf = {
- .json = (void*)dbus_req_json,
- .get = (void*)dbus_req_get,
- .success = (void*)dbus_req_success,
- .fail = (void*)dbus_req_fail,
- .raw = (void*)dbus_req_raw,
- .send = (void*)dbus_req_send,
- .context_get = (void*)afb_context_get,
- .context_set = (void*)afb_context_set,
- .addref = (void*)dbus_req_addref,
- .unref = (void*)dbus_req_unref,
- .session_close = (void*)afb_context_close,
- .session_set_LOA = (void*)afb_context_change_loa,
- .subscribe = (void*)dbus_req_subscribe,
- .unsubscribe = (void*)dbus_req_unsubscribe,
- .subcall = (void*)dbus_req_subcall
+const struct afb_xreq_query_itf afb_api_dbus_xreq_itf = {
+ .json = dbus_req_json,
+ .success = dbus_req_success,
+ .fail = dbus_req_fail,
+ .unref = dbus_req_destroy,
+ .subscribe = dbus_req_subscribe,
+ .unsubscribe = dbus_req_unsubscribe
};
-static void dbus_req_subcall(struct dbus_req *dreq, const char *api, const char *verb, struct json_object *args, void (*callback)(void*, int, struct json_object*), void *closure)
-{
- afb_subcall(&dreq->context, api, verb, args, callback, closure, (struct afb_req){ .itf = &afb_api_dbus_req_itf, .closure = dreq });
-}
-
/******************* server part **********************************/
static void afb_api_dbus_server_event_send(struct destination *destination, char order, const char *event, int eventid, const char *data, uint64_t msgid)
{
const char *data = json_object_to_json_string_ext(object, JSON_C_TO_STRING_PLAIN);
afb_api_dbus_server_event_send(closure, '!', event, eventid, data, 0);
+ json_object_put(object);
}
static void afb_api_dbus_server_event_broadcast(void *closure, const char *event, int eventid, struct json_object *object)
const char *uuid;
struct dbus_req *dreq;
struct api_dbus *api = userdata;
- struct afb_req areq;
uint32_t flags;
- struct AFB_clientCtx *session;
+ struct afb_session *session;
struct listener *listener;
/* check the interface */
}
/* connect to the context */
- if (afb_context_connect(&dreq->context, uuid, NULL) < 0)
+ if (afb_context_connect(&dreq->xreq.context, uuid, NULL) < 0)
goto out_of_memory;
- session = dreq->context.session;
+ session = dreq->xreq.context.session;
/* get the listener */
listener = afb_api_dbus_server_listerner_get(api, sd_bus_message_get_sender(message), session);
goto out_of_memory;
/* fulfill the request and emit it */
- dreq->context.flags = flags;
+ dreq->xreq.context.flags = flags;
dreq->message = sd_bus_message_ref(message);
- dreq->json = NULL;
+ dreq->json = json_tokener_parse(dreq->request);
+ if (dreq->json == NULL && strcmp(dreq->request, "null")) {
+ /* lazy error detection of json request. Is it to improve? */
+ dreq->json = json_object_new_string(dreq->request);
+ }
dreq->listener = listener;
- dreq->refcount = 1;
- areq.itf = &afb_api_dbus_req_itf;
- areq.closure = dreq;
- afb_apis_call_(areq, &dreq->context, api->api, method);
- dbus_req_unref(dreq);
+ dreq->xreq.refcount = 1;
+ dreq->xreq.query = dreq;
+ dreq->xreq.queryitf = &afb_api_dbus_xreq_itf;
+ dreq->xreq.api = api->api;
+ dreq->xreq.verb = method;
+ afb_apis_call(&dreq->xreq);
+ afb_xreq_unref(&dreq->xreq);
return 1;
out_of_memory: