Introduce apiset for grouping apis
[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 #include "jobs.h"
34
35 struct subcall;
36
37 static void subcall_destroy(struct afb_xreq *xreq);
38 static void subcall_reply(struct afb_xreq *xreq, int iserror, struct json_object *obj);
39 static int subcall_subscribe(struct afb_xreq *xreq, struct afb_event event);
40 static int subcall_unsubscribe(struct afb_xreq *xreq, struct afb_event event);
41
42 const struct afb_xreq_query_itf afb_subcall_xreq_itf = {
43         .reply = subcall_reply,
44         .unref = subcall_destroy,
45         .subscribe = subcall_subscribe,
46         .unsubscribe = subcall_unsubscribe
47 };
48
49 struct subcall
50 {
51         struct afb_xreq xreq;
52         struct afb_xreq *caller;
53         void (*callback)(void*, int, struct json_object*);
54         void *closure;
55 };
56
57 static void subcall_destroy(struct afb_xreq *xreq)
58 {
59         struct subcall *subcall = CONTAINER_OF_XREQ(struct subcall, xreq);
60
61         json_object_put(subcall->xreq.json);
62         afb_cred_unref(subcall->xreq.cred);
63         afb_xreq_unref(subcall->caller);
64         free(subcall);
65 }
66
67 static void subcall_reply(struct afb_xreq *xreq, int iserror, struct json_object *obj)
68 {
69         struct subcall *subcall = CONTAINER_OF_XREQ(struct subcall, xreq);
70
71         subcall->callback(subcall->closure, iserror, obj);
72         json_object_put(obj);
73 }
74
75 static int subcall_subscribe(struct afb_xreq *xreq, struct afb_event event)
76 {
77         struct subcall *subcall = CONTAINER_OF_XREQ(struct subcall, xreq);
78
79         return afb_xreq_subscribe(subcall->caller, event);
80 }
81
82 static int subcall_unsubscribe(struct afb_xreq *xreq, struct afb_event event)
83 {
84         struct subcall *subcall = CONTAINER_OF_XREQ(struct subcall, xreq);
85
86         return afb_xreq_unsubscribe(subcall->caller, event);
87 }
88
89 static struct subcall *create_subcall(struct afb_xreq *caller, const char *api, const char *verb, struct json_object *args, void (*callback)(void*, int, struct json_object*), void *closure)
90 {
91         struct subcall *subcall;
92         size_t lenapi, lenverb;
93         char *copy;
94
95         lenapi = 1 + strlen(api);
96         lenverb = 1 + strlen(verb);
97         subcall = malloc(lenapi + lenverb + sizeof *subcall);
98         if (subcall == NULL) {
99                 return NULL;
100         }
101         afb_xreq_init(&subcall->xreq, &afb_subcall_xreq_itf);
102         afb_context_subinit(&subcall->xreq.context, &caller->context);
103         subcall->xreq.cred = afb_cred_addref(caller->cred);
104         subcall->xreq.json = args;
105         copy = (char*)&subcall[1];
106         memcpy(copy, api, lenapi);
107         subcall->xreq.api = copy;
108         copy = &copy[lenapi];
109         memcpy(copy, verb, lenverb);
110         subcall->xreq.verb = copy;
111         subcall->caller = caller;
112         subcall->callback = callback;
113         subcall->closure = closure;
114         afb_xreq_addref(caller);
115         json_object_get(args);
116         return subcall;
117 }
118
119 void afb_subcall(
120                 struct afb_xreq *caller,
121                 const char *api,
122                 const char *verb,
123                 struct json_object *args,
124                 void (*callback)(void*, int, struct json_object*),
125                 void *closure
126 )
127 {
128         struct subcall *subcall;
129
130         subcall = create_subcall(caller, api, verb, args, callback, closure);
131         if (subcall == NULL) {
132                 callback(closure, 1, afb_msg_json_internal_error());
133                 return;
134         }
135
136         afb_xreq_process(&subcall->xreq, caller->apiset);
137 }
138
139 struct subcall_sync
140 {
141         struct afb_xreq *caller;
142         const char *api;
143         const char *verb;
144         struct json_object *args;
145         struct jobloop *jobloop;
146         struct json_object *result;
147         int iserror;
148 };
149
150 static void subcall_sync_leave(struct subcall_sync *sync)
151 {
152         struct jobloop *jobloop = sync->jobloop;
153         sync->jobloop = NULL;
154         if (jobloop)
155                 jobs_leave(jobloop);
156 }
157
158 static void subcall_sync_reply(void *closure, int iserror, struct json_object *obj)
159 {
160         struct subcall_sync *sync = closure;
161
162         sync->iserror = iserror;
163         sync->result = obj;
164         json_object_get(obj);
165         subcall_sync_leave(sync);
166 }
167
168 static void subcall_sync_enter(int signum, void *closure, struct jobloop *jobloop)
169 {
170         struct subcall_sync *sync = closure;
171
172         if (!signum) {
173                 sync->jobloop = jobloop;
174                 afb_xreq_unhooked_subcall(sync->caller, sync->api, sync->verb, sync->args, subcall_sync_reply, sync);
175         } else {
176                 sync->result = json_object_get(afb_msg_json_internal_error());
177                 sync->iserror = 1;
178                 subcall_sync_leave(sync);
179         }
180 }
181
182 int afb_subcall_sync(
183                 struct afb_xreq *caller,
184                 const char *api,
185                 const char *verb,
186                 struct json_object *args,
187                 struct json_object **result
188 )
189 {
190         int rc;
191         struct subcall_sync sync;
192
193         sync.caller = caller;
194         sync.api = api;
195         sync.verb = verb;
196         sync.args = args;
197         sync.jobloop = NULL;
198         sync.result = NULL;
199         sync.iserror = 1;
200
201         rc = jobs_enter(NULL, 0, subcall_sync_enter, &sync);
202         if (rc < 0) {
203                 sync.result = json_object_get(afb_msg_json_internal_error());
204                 sync.iserror = 1;
205         }
206         rc = !sync.iserror;
207         *result = sync.result;
208         return rc;
209 }
210
211