Add credential data to xreq
[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-apis.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
93         subcall = calloc(1, sizeof *subcall);
94         if (subcall == NULL) {
95                 return NULL;
96         }
97
98         afb_xreq_init(&subcall->xreq, &afb_subcall_xreq_itf);
99         afb_context_subinit(&subcall->xreq.context, &caller->context);
100         subcall->xreq.cred = afb_cred_addref(caller->cred);
101         subcall->xreq.json = args;
102         subcall->xreq.api = api; /* TODO: alloc ? */
103         subcall->xreq.verb = verb; /* TODO: alloc ? */
104         subcall->caller = caller;
105         subcall->callback = callback;
106         subcall->closure = closure;
107         afb_xreq_addref(caller);
108         json_object_get(args);
109         return subcall;
110 }
111
112 void afb_subcall(
113                 struct afb_xreq *caller,
114                 const char *api,
115                 const char *verb,
116                 struct json_object *args,
117                 void (*callback)(void*, int, struct json_object*),
118                 void *closure
119 )
120 {
121         struct subcall *subcall;
122
123         subcall = create_subcall(caller, api, verb, args, callback, closure);
124         if (subcall == NULL) {
125                 callback(closure, 1, afb_msg_json_internal_error());
126                 return;
127         }
128
129         afb_apis_call(&subcall->xreq);
130         afb_xreq_unref(&subcall->xreq);
131 }
132
133 struct subcall_sync
134 {
135         struct afb_xreq *caller;
136         const char *api;
137         const char *verb;
138         struct json_object *args;
139         struct jobloop *jobloop;
140         struct json_object *result;
141         int iserror;
142 };
143
144 static void subcall_sync_leave(struct subcall_sync *sync)
145 {
146         struct jobloop *jobloop = sync->jobloop;
147         sync->jobloop = NULL;
148         if (jobloop)
149                 jobs_leave(jobloop);
150 }
151
152 static void subcall_sync_reply(void *closure, int iserror, struct json_object *obj)
153 {
154         struct subcall_sync *sync = closure;
155
156         sync->iserror = iserror;
157         sync->result = obj;
158         json_object_get(obj);
159         subcall_sync_leave(sync);
160 }
161
162 static void subcall_sync_enter(int signum, void *closure, struct jobloop *jobloop)
163 {
164         struct subcall_sync *sync = closure;
165
166         if (!signum) {
167                 sync->jobloop = jobloop;
168                 afb_xreq_unhooked_subcall(sync->caller, sync->api, sync->verb, sync->args, subcall_sync_reply, sync);
169         } else {
170                 sync->result = json_object_get(afb_msg_json_internal_error());
171                 sync->iserror = 1;
172                 subcall_sync_leave(sync);
173         }
174 }
175
176 int afb_subcall_sync(
177                 struct afb_xreq *caller,
178                 const char *api,
179                 const char *verb,
180                 struct json_object *args,
181                 struct json_object **result
182 )
183 {
184         int rc;
185         struct subcall_sync sync;
186
187         sync.caller = caller;
188         sync.api = api;
189         sync.verb = verb;
190         sync.args = args;
191         sync.jobloop = NULL;
192         sync.result = NULL;
193         sync.iserror = 1;
194
195         rc = jobs_enter(NULL, 0, subcall_sync_enter, &sync);
196         if (rc < 0) {
197                 sync.result = json_object_get(afb_msg_json_internal_error());
198                 sync.iserror = 1;
199         }
200         rc = !sync.iserror;
201         *result = sync.result;
202         return rc;
203 }
204
205