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.
24 #include <json-c/json.h>
26 #include <afb/afb-binding-v1.h>
27 #include <afb/afb-binding-v2.h>
29 #include "afb-session.h"
30 #include "afb-context.h"
32 #include "afb-msg-json.h"
36 #include "afb-apiset.h"
41 * Structure for recording service
45 /* session of the service */
46 struct afb_session *session;
48 /* the apiset for the service */
49 struct afb_apiset *apiset;
51 /* event listener of the service or NULL */
52 struct afb_evt_listener *listener;
54 /* on event callback for the service */
55 void (*on_event)(const char *event, struct json_object *object);
59 * Structure for requests initiated by the service
68 void (*callback)(void*, int, struct json_object*);
72 struct jobloop *jobloop;
73 struct json_object *result;
77 /* functions for services */
78 static void svc_on_event(void *closure, const char *event, int eventid, struct json_object *object);
79 static void svc_call(void *closure, const char *api, const char *verb, struct json_object *args,
80 void (*callback)(void*, int, struct json_object*), void *cbclosure);
81 static int svc_call_sync(void *closure, const char *api, const char *verb, struct json_object *args,
82 struct json_object **result);
84 /* the interface for services */
85 static const struct afb_service_itf service_itf = {
87 .call_sync = svc_call_sync
90 /* the interface for events */
91 static const struct afb_evt_itf evt_itf = {
92 .broadcast = svc_on_event,
96 /* functions for requests of services */
97 static void svcreq_destroy(struct afb_xreq *xreq);
98 static void svcreq_reply(struct afb_xreq *xreq, int iserror, json_object *obj);
100 /* interface for requests of services */
101 const struct afb_xreq_query_itf afb_svc_xreq_itf = {
102 .unref = svcreq_destroy,
103 .reply = svcreq_reply
106 /* the common session for services sharing their session */
107 static struct afb_session *common_session;
109 static inline struct afb_service to_afb_service(struct afb_svc *svc)
111 return (struct afb_service){ .itf = &service_itf, .closure = svc };
117 static void svc_free(struct afb_svc *svc)
119 if (svc->listener != NULL)
120 afb_evt_listener_unref(svc->listener);
122 afb_session_unref(svc->session);
123 afb_apiset_unref(svc->apiset);
128 * Allocates a new service
130 static struct afb_svc *afb_svc_alloc(
131 struct afb_apiset *apiset,
137 /* allocates the svc handler */
138 svc = calloc(1, sizeof * svc);
144 /* instanciate the apiset */
145 svc->apiset = afb_apiset_addref(apiset);
147 /* instanciate the session */
149 /* session shared with other svcs */
150 if (common_session == NULL) {
151 common_session = afb_session_create (NULL, 0);
152 if (common_session == NULL)
155 svc->session = afb_session_addref(common_session);
157 /* session dedicated to the svc */
158 svc->session = afb_session_create (NULL, 0);
159 if (svc->session == NULL)
171 * Creates a new service
173 struct afb_svc *afb_svc_create_v1(
174 struct afb_apiset *apiset,
176 int (*start)(struct afb_service service),
177 void (*on_event)(const char *event, struct json_object *object)
183 /* allocates the svc handler */
184 svc = afb_svc_alloc(apiset, share_session);
188 /* initialises the listener if needed */
190 svc->on_event = on_event;
191 svc->listener = afb_evt_listener_create(&evt_itf, svc);
192 if (svc->listener == NULL)
196 /* initialises the svc now */
198 rc = start(to_afb_service(svc));
211 * Creates a new service
213 struct afb_svc *afb_svc_create_v2(
214 struct afb_apiset *apiset,
217 void (*on_event)(const char *event, struct json_object *object),
218 struct afb_binding_data_v2 *data
224 /* allocates the svc handler */
225 svc = afb_svc_alloc(apiset, share_session);
228 data->service = to_afb_service(svc);
230 /* initialises the listener if needed */
232 svc->on_event = on_event;
233 svc->listener = afb_evt_listener_create(&evt_itf, svc);
234 if (svc->listener == NULL)
238 /* starts the svc if needed */
253 * Propagates the event to the service
255 static void svc_on_event(void *closure, const char *event, int eventid, struct json_object *object)
257 struct afb_svc *svc = closure;
258 svc->on_event(event, object);
259 json_object_put(object);
265 static struct svc_req *svcreq_create(struct afb_svc *svc, const char *api, const char *verb, struct json_object *args)
267 struct svc_req *svcreq;
268 size_t lenapi, lenverb;
271 /* allocates the request */
272 lenapi = 1 + strlen(api);
273 lenverb = 1 + strlen(verb);
274 svcreq = malloc(lenapi + lenverb + sizeof *svcreq);
275 if (svcreq != NULL) {
276 /* initialises the request */
277 afb_xreq_init(&svcreq->xreq, &afb_svc_xreq_itf);
278 afb_context_init(&svcreq->xreq.context, svc->session, NULL);
279 svcreq->xreq.context.validated = 1;
280 copy = (char*)&svcreq[1];
281 memcpy(copy, api, lenapi);
282 svcreq->xreq.api = copy;
283 copy = ©[lenapi];
284 memcpy(copy, verb, lenverb);
285 svcreq->xreq.verb = copy;
286 svcreq->xreq.listener = svc->listener;
287 svcreq->xreq.json = args;
294 * destroys the svc_req
296 static void svcreq_destroy(struct afb_xreq *xreq)
298 struct svc_req *svcreq = CONTAINER_OF_XREQ(struct svc_req, xreq);
300 afb_context_disconnect(&svcreq->xreq.context);
301 json_object_put(svcreq->xreq.json);
302 afb_cred_unref(svcreq->xreq.cred);
306 static void svcreq_sync_leave(struct svc_req *svcreq)
308 struct jobloop *jobloop = svcreq->jobloop;
311 svcreq->jobloop = NULL;
316 static void svcreq_reply(struct afb_xreq *xreq, int iserror, json_object *obj)
318 struct svc_req *svcreq = CONTAINER_OF_XREQ(struct svc_req, xreq);
319 if (svcreq->callback) {
320 svcreq->callback(svcreq->closure, iserror, obj);
321 json_object_put(obj);
323 svcreq->iserror = iserror;
324 svcreq->result = obj;
325 svcreq_sync_leave(svcreq);
329 static void svcreq_sync_enter(int signum, void *closure, struct jobloop *jobloop)
331 struct svc_req *svcreq = closure;
334 svcreq->jobloop = jobloop;
335 afb_xreq_process(&svcreq->xreq, svcreq->svc->apiset);
337 svcreq->result = afb_msg_json_internal_error();
339 svcreq_sync_leave(svcreq);
344 * Initiates a call for the service
346 static void svc_call(void *closure, const char *api, const char *verb, struct json_object *args, void (*callback)(void*, int, struct json_object*), void *cbclosure)
348 struct afb_svc *svc = closure;
349 struct svc_req *svcreq;
350 struct json_object *ierr;
352 /* allocates the request */
353 svcreq = svcreq_create(svc, api, verb, args);
354 if (svcreq == NULL) {
355 ERROR("out of memory");
356 json_object_put(args);
357 ierr = afb_msg_json_internal_error();
358 callback(cbclosure, 1, ierr);
359 json_object_put(ierr);
363 /* initialises the request */
364 svcreq->jobloop = NULL;
365 svcreq->callback = callback;
366 svcreq->closure = cbclosure;
368 /* terminates and frees ressources if needed */
369 afb_xreq_process(&svcreq->xreq, svc->apiset);
372 static int svc_call_sync(void *closure, const char *api, const char *verb, struct json_object *args,
373 struct json_object **result)
375 struct afb_svc *svc = closure;
376 struct svc_req *svcreq;
379 /* allocates the request */
380 svcreq = svcreq_create(svc, api, verb, args);
381 if (svcreq == NULL) {
382 ERROR("out of memory");
384 json_object_put(args);
385 *result = afb_msg_json_internal_error();
389 /* initialises the request */
390 svcreq->jobloop = NULL;
391 svcreq->callback = NULL;
392 svcreq->result = NULL;
394 afb_xreq_addref(&svcreq->xreq);
395 rc = jobs_enter(NULL, 0, svcreq_sync_enter, svcreq);
396 rc = rc >= 0 && !svcreq->iserror;
397 *result = (rc || svcreq->result) ? svcreq->result : afb_msg_json_internal_error();
398 afb_xreq_unref(&svcreq->xreq);