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"
42 #define HOOK(x,...) if((svc)->hookflags & afb_hook_flag_svc_##x) afb_hook_svc_##x(__VA_ARGS__)
45 * Structure for requests initiated by the service
54 void (*callback)(void*, int, struct json_object*);
58 struct jobloop *jobloop;
59 struct json_object *result;
64 /* functions for services */
65 static void svc_on_event(void *closure, const char *event, int eventid, struct json_object *object);
66 static void svc_call(void *closure, const char *api, const char *verb, struct json_object *args,
67 void (*callback)(void*, int, struct json_object*), void *cbclosure);
68 static int svc_call_sync(void *closure, const char *api, const char *verb, struct json_object *args,
69 struct json_object **result);
71 /* the interface for services */
72 static const struct afb_service_itf service_itf = {
74 .call_sync = svc_call_sync
77 /* the interface for events */
78 static const struct afb_evt_itf evt_itf = {
79 .broadcast = svc_on_event,
83 /* functions for requests of services */
84 static void svcreq_destroy(struct afb_xreq *xreq);
85 static void svcreq_reply(struct afb_xreq *xreq, int status, json_object *obj);
87 /* interface for requests of services */
88 const struct afb_xreq_query_itf afb_svc_xreq_itf = {
89 .unref = svcreq_destroy,
93 /* the common session for services sharing their session */
94 static struct afb_session *common_session;
96 static inline struct afb_service to_afb_service(struct afb_svc *svc)
98 return (struct afb_service){ .itf = &service_itf, .closure = svc };
104 static void svc_free(struct afb_svc *svc)
106 if (svc->listener != NULL)
107 afb_evt_listener_unref(svc->listener);
109 afb_session_unref(svc->session);
110 afb_apiset_unref(svc->apiset);
115 * Allocates a new service
117 static struct afb_svc *afb_svc_alloc(
119 struct afb_apiset *apiset,
125 /* allocates the svc handler */
126 svc = calloc(1, sizeof * svc);
132 /* instanciate the apiset */
134 svc->apiset = afb_apiset_addref(apiset);
136 /* instanciate the session */
138 /* session shared with other svcs */
139 if (common_session == NULL) {
140 common_session = afb_session_create (NULL, 0);
141 if (common_session == NULL)
144 svc->session = afb_session_addref(common_session);
146 /* session dedicated to the svc */
147 svc->session = afb_session_create (NULL, 0);
148 if (svc->session == NULL)
152 svc->hookflags = afb_hook_flags_svc(svc->api);
161 * Creates a new service
163 struct afb_svc *afb_svc_create_v1(
165 struct afb_apiset *apiset,
167 int (*start)(struct afb_service service),
168 void (*on_event)(const char *event, struct json_object *object)
174 /* allocates the svc handler */
175 svc = afb_svc_alloc(api, apiset, share_session);
179 /* initialises the listener if needed */
181 svc->on_event = on_event;
182 svc->listener = afb_evt_listener_create(&evt_itf, svc);
183 if (svc->listener == NULL)
187 /* initialises the svc now */
189 HOOK(start_before, svc);
190 rc = start(to_afb_service(svc));
191 HOOK(start_after, svc, rc);
204 * Creates a new service
206 struct afb_svc *afb_svc_create_v2(
208 struct afb_apiset *apiset,
211 void (*on_event)(const char *event, struct json_object *object),
212 struct afb_binding_data_v2 *data
218 /* allocates the svc handler */
219 svc = afb_svc_alloc(api, apiset, share_session);
222 data->service = to_afb_service(svc);
224 /* initialises the listener if needed */
226 svc->on_event = on_event;
227 svc->listener = afb_evt_listener_create(&evt_itf, svc);
228 if (svc->listener == NULL)
232 /* starts the svc if needed */
234 HOOK(start_before, svc);
236 HOOK(start_after, svc, rc);
248 void afb_svc_update_hook(struct afb_svc *svc)
250 svc->hookflags = afb_hook_flags_svc(svc->api);
254 * Propagates the event to the service
256 static void svc_on_event(void *closure, const char *event, int eventid, struct json_object *object)
258 struct afb_svc *svc = closure;
260 HOOK(on_event_before, svc, event, eventid, object);
261 svc->on_event(event, object);
262 HOOK(on_event_after, svc, event, eventid, object);
263 json_object_put(object);
269 static struct svc_req *svcreq_create(struct afb_svc *svc, const char *api, const char *verb, struct json_object *args)
271 struct svc_req *svcreq;
272 size_t lenapi, lenverb;
275 /* allocates the request */
276 lenapi = 1 + strlen(api);
277 lenverb = 1 + strlen(verb);
278 svcreq = malloc(lenapi + lenverb + sizeof *svcreq);
279 if (svcreq != NULL) {
280 /* initialises the request */
281 afb_xreq_init(&svcreq->xreq, &afb_svc_xreq_itf);
282 afb_context_init(&svcreq->xreq.context, svc->session, NULL);
283 svcreq->xreq.context.validated = 1;
284 copy = (char*)&svcreq[1];
285 memcpy(copy, api, lenapi);
286 svcreq->xreq.api = copy;
287 copy = ©[lenapi];
288 memcpy(copy, verb, lenverb);
289 svcreq->xreq.verb = copy;
290 svcreq->xreq.listener = svc->listener;
291 svcreq->xreq.json = args;
298 * destroys the svc_req
300 static void svcreq_destroy(struct afb_xreq *xreq)
302 struct svc_req *svcreq = CONTAINER_OF_XREQ(struct svc_req, xreq);
304 afb_context_disconnect(&svcreq->xreq.context);
305 json_object_put(svcreq->xreq.json);
306 afb_cred_unref(svcreq->xreq.cred);
310 static void svcreq_sync_leave(struct svc_req *svcreq)
312 struct jobloop *jobloop = svcreq->jobloop;
315 svcreq->jobloop = NULL;
320 static void svcreq_reply(struct afb_xreq *xreq, int status, json_object *obj)
322 struct svc_req *svcreq = CONTAINER_OF_XREQ(struct svc_req, xreq);
324 struct afb_svc *svc = svcreq->svc;
325 if (svcreq->callback)
326 svcreq->callback(svcreq->closure, status, obj);
327 HOOK(call_result, svc, status, obj);
328 json_object_put(obj);
330 svcreq->status = status;
331 svcreq->result = obj;
332 svcreq_sync_leave(svcreq);
336 static void svcreq_sync_enter(int signum, void *closure, struct jobloop *jobloop)
338 struct svc_req *svcreq = closure;
341 svcreq->jobloop = jobloop;
342 afb_xreq_process(&svcreq->xreq, svcreq->svc->apiset);
344 svcreq->result = afb_msg_json_internal_error();
346 svcreq_sync_leave(svcreq);
351 * Initiates a call for the service
353 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)
355 struct afb_svc *svc = closure;
356 struct svc_req *svcreq;
357 struct json_object *ierr;
359 HOOK(call, svc, api, verb, args);
361 /* allocates the request */
362 svcreq = svcreq_create(svc, api, verb, args);
363 if (svcreq == NULL) {
364 ERROR("out of memory");
365 json_object_put(args);
366 ierr = afb_msg_json_internal_error();
368 callback(cbclosure, -1, ierr);
369 HOOK(call_result, svc, -1, ierr);
370 json_object_put(ierr);
374 /* initialises the request */
375 svcreq->jobloop = NULL;
376 svcreq->callback = callback;
377 svcreq->closure = cbclosure;
380 /* terminates and frees ressources if needed */
381 afb_xreq_process(&svcreq->xreq, svc->apiset);
384 static int svc_call_sync(void *closure, const char *api, const char *verb, struct json_object *args,
385 struct json_object **result)
387 struct afb_svc *svc = closure;
388 struct svc_req *svcreq;
389 struct json_object *resu;
392 HOOK(callsync, svc, api, verb, args);
394 /* allocates the request */
395 svcreq = svcreq_create(svc, api, verb, args);
396 if (svcreq == NULL) {
397 ERROR("out of memory");
399 json_object_put(args);
400 resu = afb_msg_json_internal_error();
403 /* initialises the request */
404 svcreq->jobloop = NULL;
405 svcreq->callback = NULL;
406 svcreq->result = NULL;
409 afb_xreq_addref(&svcreq->xreq);
410 rc = jobs_enter(NULL, 0, svcreq_sync_enter, svcreq);
413 resu = (rc >= 0 || svcreq->result) ? svcreq->result : afb_msg_json_internal_error();
414 afb_xreq_unref(&svcreq->xreq);
416 HOOK(callsync_result, svc, rc, resu);
420 json_object_put(resu);