2 * Copyright (C) 2016, 2017 "IoT.bzh"
3 * Author: José Bollo <jose.bollo@iot.bzh>
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
23 #include <json-c/json.h>
24 #include <afb/afb-req-itf.h>
26 #include "afb-subcall.h"
27 #include "afb-msg-json.h"
28 #include "afb-apiset.h"
29 #include "afb-context.h"
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);
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
52 struct afb_xreq *caller;
53 void (*callback)(void*, int, struct json_object*);
57 static void subcall_destroy(struct afb_xreq *xreq)
59 struct subcall *subcall = CONTAINER_OF_XREQ(struct subcall, xreq);
61 json_object_put(subcall->xreq.json);
62 afb_cred_unref(subcall->xreq.cred);
63 afb_xreq_unref(subcall->caller);
67 static void subcall_reply(struct afb_xreq *xreq, int iserror, struct json_object *obj)
69 struct subcall *subcall = CONTAINER_OF_XREQ(struct subcall, xreq);
71 subcall->callback(subcall->closure, iserror, obj);
75 static int subcall_subscribe(struct afb_xreq *xreq, struct afb_event event)
77 struct subcall *subcall = CONTAINER_OF_XREQ(struct subcall, xreq);
79 return afb_xreq_subscribe(subcall->caller, event);
82 static int subcall_unsubscribe(struct afb_xreq *xreq, struct afb_event event)
84 struct subcall *subcall = CONTAINER_OF_XREQ(struct subcall, xreq);
86 return afb_xreq_unsubscribe(subcall->caller, event);
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)
91 struct subcall *subcall;
92 size_t lenapi, lenverb;
95 lenapi = 1 + strlen(api);
96 lenverb = 1 + strlen(verb);
97 subcall = malloc(lenapi + lenverb + sizeof *subcall);
98 if (subcall == NULL) {
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 = ©[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);
120 struct afb_xreq *caller,
123 struct json_object *args,
124 void (*callback)(void*, int, struct json_object*),
128 struct subcall *subcall;
130 subcall = create_subcall(caller, api, verb, args, callback, closure);
131 if (subcall == NULL) {
132 callback(closure, 1, afb_msg_json_internal_error());
136 afb_xreq_process(&subcall->xreq, caller->apiset);
141 struct afb_xreq *caller;
144 struct json_object *args;
145 struct jobloop *jobloop;
146 struct json_object *result;
150 static void subcall_sync_leave(struct subcall_sync *sync)
152 struct jobloop *jobloop = sync->jobloop;
153 sync->jobloop = NULL;
158 static void subcall_sync_reply(void *closure, int iserror, struct json_object *obj)
160 struct subcall_sync *sync = closure;
162 sync->iserror = iserror;
164 json_object_get(obj);
165 subcall_sync_leave(sync);
168 static void subcall_sync_enter(int signum, void *closure, struct jobloop *jobloop)
170 struct subcall_sync *sync = closure;
173 sync->jobloop = jobloop;
174 afb_xreq_unhooked_subcall(sync->caller, sync->api, sync->verb, sync->args, subcall_sync_reply, sync);
176 sync->result = json_object_get(afb_msg_json_internal_error());
178 subcall_sync_leave(sync);
182 int afb_subcall_sync(
183 struct afb_xreq *caller,
186 struct json_object *args,
187 struct json_object **result
191 struct subcall_sync sync;
193 sync.caller = caller;
201 rc = jobs_enter(NULL, 0, subcall_sync_enter, &sync);
203 sync.result = json_object_get(afb_msg_json_internal_error());
207 *result = sync.result;