ef114077e5d5dcb0783a1287655722e714284f55
[src/app-framework-binder.git] / src / afb-subcall.c
1 /*
2  * Copyright (C) 2016, 2017 "IoT.bzh"
3  * Author: José Bollo <jose.bollo@iot.bzh>
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *   http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18 #define _GNU_SOURCE
19
20 #include <stdlib.h>
21 #include <string.h>
22
23 #include <json-c/json.h>
24 #include <afb/afb-req-itf.h>
25
26 #include "afb-subcall.h"
27 #include "afb-msg-json.h"
28 #include "afb-apiset.h"
29 #include "afb-context.h"
30 #include "afb-xreq.h"
31 #include "afb-cred.h"
32 #include "verbose.h"
33
34 struct subcall
35 {
36         struct afb_xreq xreq;
37         struct afb_xreq *caller;
38         void (*callback)(void*, int, struct json_object*);
39         void *closure;
40 };
41
42 static void subcall_destroy(struct afb_xreq *xreq)
43 {
44         struct subcall *subcall = CONTAINER_OF_XREQ(struct subcall, xreq);
45
46         json_object_put(subcall->xreq.json);
47         afb_cred_unref(subcall->xreq.cred);
48         afb_xreq_unref(subcall->caller);
49         free(subcall);
50 }
51
52 static void subcall_reply(struct afb_xreq *xreq, int iserror, struct json_object *obj)
53 {
54         struct subcall *subcall = CONTAINER_OF_XREQ(struct subcall, xreq);
55
56         subcall->callback(subcall->closure, iserror, obj);
57         json_object_put(obj);
58 }
59
60 static int subcall_subscribe(struct afb_xreq *xreq, struct afb_event event)
61 {
62         struct subcall *subcall = CONTAINER_OF_XREQ(struct subcall, xreq);
63
64         return afb_xreq_subscribe(subcall->caller, event);
65 }
66
67 static int subcall_unsubscribe(struct afb_xreq *xreq, struct afb_event event)
68 {
69         struct subcall *subcall = CONTAINER_OF_XREQ(struct subcall, xreq);
70
71         return afb_xreq_unsubscribe(subcall->caller, event);
72 }
73
74 const struct afb_xreq_query_itf afb_subcall_xreq_itf = {
75         .reply = subcall_reply,
76         .unref = subcall_destroy,
77         .subscribe = subcall_subscribe,
78         .unsubscribe = subcall_unsubscribe
79 };
80
81 void afb_subcall(
82                 struct afb_xreq *caller,
83                 const char *api,
84                 const char *verb,
85                 struct json_object *args,
86                 void (*callback)(void*, int, struct json_object*),
87                 void *closure
88 )
89 {
90         struct subcall *subcall;
91         size_t lenapi, lenverb;
92         char *copy;
93
94         lenapi = 1 + strlen(api);
95         lenverb = 1 + strlen(verb);
96         subcall = malloc(lenapi + lenverb + sizeof *subcall);
97         if (subcall == NULL)
98                 callback(closure, 1, afb_msg_json_internal_error());
99         else {
100                 afb_xreq_init(&subcall->xreq, &afb_subcall_xreq_itf);
101                 afb_context_subinit(&subcall->xreq.context, &caller->context);
102                 subcall->xreq.cred = afb_cred_addref(caller->cred);
103                 subcall->xreq.json = args;
104                 copy = (char*)&subcall[1];
105                 memcpy(copy, api, lenapi);
106                 subcall->xreq.api = copy;
107                 copy = &copy[lenapi];
108                 memcpy(copy, verb, lenverb);
109                 subcall->xreq.verb = copy;
110                 subcall->caller = caller;
111                 subcall->callback = callback;
112                 subcall->closure = closure;
113                 afb_xreq_addref(caller);
114                 json_object_get(args); /* keep args existing */
115                 afb_xreq_process(&subcall->xreq, caller->apiset);
116         }
117 }
118