Simplify functions for calls
[src/app-framework-binder.git] / src / afb-api-dbus.c
index 53bc2dc..179f895 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * 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");
@@ -30,7 +30,7 @@
 
 #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"
@@ -317,11 +317,11 @@ static int api_dbus_client_on_reply(sd_bus_message *message, void *userdata, sd_
 }
 
 /* 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_req req, struct afb_context *context, const char *verb)
 {
+       struct api_dbus *api = closure;
        size_t size;
        int rc;
-       char *method = strndupa(verb, lenverb);
        struct dbus_memo *memo;
        struct sd_bus_message *msg;
 
@@ -334,13 +334,13 @@ static void api_dbus_client_call(struct api_dbus *api, struct afb_req req, struc
 
        /* 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, verb);
        if (rc < 0)
                goto error;
 
        rc = sd_bus_message_append(msg, "ssu",
                        afb_req_raw(req, &size),
-                       ctxClientGetUuid(context->session),
+                       afb_session_uuid(context->session),
                        (uint32_t)context->flags);
        if (rc < 0)
                goto error;
@@ -363,8 +363,10 @@ 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;
@@ -618,8 +620,8 @@ 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.call = api_dbus_client_call;
+       afb_api.service_start = api_dbus_service_start;
        if (afb_apis_add(api->api, afb_api) < 0)
                goto error2;
 
@@ -724,7 +726,7 @@ static void afb_api_dbus_server_listener_free(struct listener *listener)
        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;
@@ -736,7 +738,7 @@ static struct listener *afb_api_dbus_server_listerner_get(struct api_dbus *api,
                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);
@@ -751,7 +753,7 @@ static struct listener *afb_api_dbus_server_listerner_get(struct api_dbus *api,
                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);
@@ -799,7 +801,7 @@ static struct json_object *dbus_req_json(struct dbus_req *dreq)
 {
        if (dreq->json == NULL) {
                dreq->json = json_tokener_parse(dreq->request);
-               if (dreq->json == NULL) {
+               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);
                }
@@ -937,6 +939,7 @@ static void afb_api_dbus_server_event_push(void *closure, const char *event, int
 {
        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)
@@ -962,7 +965,7 @@ static int api_dbus_server_on_object_called(sd_bus_message *message, void *userd
        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 */
@@ -1002,7 +1005,7 @@ static int api_dbus_server_on_object_called(sd_bus_message *message, void *userd
        dreq->refcount = 1;
        areq.itf = &afb_api_dbus_req_itf;
        areq.closure = dreq;
-       afb_apis_call_(areq, &dreq->context, api->api, method);
+       afb_apis_call(areq, &dreq->context, api->api, method);
        dbus_req_unref(dreq);
        return 1;