c7991934ce4c379208652e396d221175fbf9f78d
[src/app-framework-binder.git] / src / afb-ws-json1.c
1 /*
2  * Copyright (C) 2016-2019 "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 <unistd.h>
22 #include <assert.h>
23 #include <errno.h>
24 #include <string.h>
25
26 #include <json-c/json.h>
27
28 #include "afb-wsj1.h"
29 #include "afb-ws-json1.h"
30 #include "afb-msg-json.h"
31 #include "afb-session.h"
32 #include "afb-cred.h"
33 #include "afb-apiset.h"
34 #include "afb-xreq.h"
35 #include "afb-context.h"
36 #include "afb-evt.h"
37
38 #include "systemd.h"
39 #include "verbose.h"
40 #include "fdev.h"
41
42 /* predeclaration of structures */
43 struct afb_ws_json1;
44 struct afb_wsreq;
45
46 /* predeclaration of websocket callbacks */
47 static void aws_on_hangup_cb(void *closure, struct afb_wsj1 *wsj1);
48 static void aws_on_call_cb(void *closure, const char *api, const char *verb, struct afb_wsj1_msg *msg);
49 static void aws_on_push_cb(void *closure, const char *event, int eventid, struct json_object *object);
50 static void aws_on_broadcast_cb(void *closure, const char *event, struct json_object *object, const uuid_binary_t uuid, uint8_t hop);
51
52 /* predeclaration of wsreq callbacks */
53 static void wsreq_destroy(struct afb_xreq *xreq);
54 static void wsreq_reply(struct afb_xreq *xreq, struct json_object *object, const char *error, const char *info);
55 static int wsreq_subscribe(struct afb_xreq *xreq, struct afb_event_x2 *event);
56 static int wsreq_unsubscribe(struct afb_xreq *xreq, struct afb_event_x2 *event);
57
58 /* declaration of websocket structure */
59 struct afb_ws_json1
60 {
61         int refcount;
62         void (*cleanup)(void*);
63         void *cleanup_closure;
64         struct afb_session *session;
65         struct afb_evt_listener *listener;
66         struct afb_wsj1 *wsj1;
67         struct afb_cred *cred;
68         struct afb_apiset *apiset;
69         int new_session;
70 };
71
72 /* declaration of wsreq structure */
73 struct afb_wsreq
74 {
75         struct afb_xreq xreq;
76         struct afb_ws_json1 *aws;
77         struct afb_wsreq *next;
78         struct afb_wsj1_msg *msgj1;
79 };
80
81 /* interface for afb_ws_json1 / afb_wsj1 */
82 static struct afb_wsj1_itf wsj1_itf = {
83         .on_hangup = aws_on_hangup_cb,
84         .on_call = aws_on_call_cb
85 };
86
87 /* interface for xreq */
88 const struct afb_xreq_query_itf afb_ws_json1_xreq_itf = {
89         .reply = wsreq_reply,
90         .subscribe = wsreq_subscribe,
91         .unsubscribe = wsreq_unsubscribe,
92         .unref = wsreq_destroy
93 };
94
95 /* the interface for events */
96 static const struct afb_evt_itf evt_itf = {
97         .broadcast = aws_on_broadcast_cb,
98         .push = aws_on_push_cb
99 };
100
101 /***************************************************************
102 ****************************************************************
103 **
104 **  functions of afb_ws_json1 / afb_wsj1
105 **
106 ****************************************************************
107 ***************************************************************/
108
109 struct afb_ws_json1 *afb_ws_json1_create(struct fdev *fdev, struct afb_apiset *apiset, struct afb_context *context, void (*cleanup)(void*), void *cleanup_closure)
110 {
111         struct afb_ws_json1 *result;
112
113         assert(fdev);
114         assert(context != NULL);
115
116         result = malloc(sizeof * result);
117         if (result == NULL)
118                 goto error;
119
120         result->refcount = 1;
121         result->cleanup = cleanup;
122         result->cleanup_closure = cleanup_closure;
123         result->session = afb_session_addref(context->session);
124         result->new_session = context->created != 0;
125         if (result->session == NULL)
126                 goto error2;
127
128         result->wsj1 = afb_wsj1_create(fdev, &wsj1_itf, result);
129         if (result->wsj1 == NULL)
130                 goto error3;
131
132         result->listener = afb_evt_listener_create(&evt_itf, result);
133         if (result->listener == NULL)
134                 goto error4;
135
136         result->cred = afb_cred_create_for_socket(fdev_fd(fdev));
137         result->apiset = afb_apiset_addref(apiset);
138         return result;
139
140 error4:
141         afb_wsj1_unref(result->wsj1);
142 error3:
143         afb_session_unref(result->session);
144 error2:
145         free(result);
146 error:
147         fdev_unref(fdev);
148         return NULL;
149 }
150
151 struct afb_ws_json1 *afb_ws_json1_addref(struct afb_ws_json1 *ws)
152 {
153         __atomic_add_fetch(&ws->refcount, 1, __ATOMIC_RELAXED);
154         return ws;
155 }
156
157 void afb_ws_json1_unref(struct afb_ws_json1 *ws)
158 {
159         if (!__atomic_sub_fetch(&ws->refcount, 1, __ATOMIC_RELAXED)) {
160                 afb_evt_listener_unref(ws->listener);
161                 afb_wsj1_unref(ws->wsj1);
162                 if (ws->cleanup != NULL)
163                         ws->cleanup(ws->cleanup_closure);
164                 afb_session_unref(ws->session);
165                 afb_cred_unref(ws->cred);
166                 afb_apiset_unref(ws->apiset);
167                 free(ws);
168         }
169 }
170
171 static void aws_on_hangup_cb(void *closure, struct afb_wsj1 *wsj1)
172 {
173         struct afb_ws_json1 *ws = closure;
174         afb_ws_json1_unref(ws);
175 }
176
177 static void aws_on_call_cb(void *closure, const char *api, const char *verb, struct afb_wsj1_msg *msg)
178 {
179         struct afb_ws_json1 *ws = closure;
180         struct afb_wsreq *wsreq;
181
182         DEBUG("received websocket request for %s/%s: %s", api, verb, afb_wsj1_msg_object_s(msg));
183
184         /* allocate */
185         wsreq = calloc(1, sizeof *wsreq);
186         if (wsreq == NULL) {
187                 afb_wsj1_close(ws->wsj1, 1008, NULL);
188                 return;
189         }
190
191         /* init the context */
192         afb_xreq_init(&wsreq->xreq, &afb_ws_json1_xreq_itf);
193         afb_context_init(&wsreq->xreq.context, ws->session, afb_wsj1_msg_token(msg));
194         if (!wsreq->xreq.context.invalidated)
195                 wsreq->xreq.context.validated = 1;
196         if (ws->new_session != 0) {
197                 wsreq->xreq.context.created = 1;
198                 ws->new_session = 0;
199         }
200
201         /* fill and record the request */
202         afb_wsj1_msg_addref(msg);
203         wsreq->msgj1 = msg;
204         wsreq->xreq.cred = afb_cred_addref(ws->cred);
205         wsreq->xreq.request.called_api = api;
206         wsreq->xreq.request.called_verb = verb;
207         wsreq->xreq.json = afb_wsj1_msg_object_j(wsreq->msgj1);
208         wsreq->aws = afb_ws_json1_addref(ws);
209
210         /* emits the call */
211         afb_xreq_process(&wsreq->xreq, ws->apiset);
212 }
213
214 static void aws_on_event(struct afb_ws_json1 *aws, const char *event, struct json_object *object)
215 {
216         afb_wsj1_send_event_j(aws->wsj1, event, afb_msg_json_event(event, object));
217 }
218
219 static void aws_on_push_cb(void *closure, const char *event, int eventid, struct json_object *object)
220 {
221         aws_on_event(closure, event, object);
222 }
223
224 static void aws_on_broadcast_cb(void *closure, const char *event, struct json_object *object, const uuid_binary_t uuid, uint8_t hop)
225 {
226         aws_on_event(closure, event, afb_msg_json_event(event, object));
227 }
228
229 /***************************************************************
230 ****************************************************************
231 **
232 **  functions of wsreq / afb_req
233 **
234 ****************************************************************
235 ***************************************************************/
236
237 static void wsreq_destroy(struct afb_xreq *xreq)
238 {
239         struct afb_wsreq *wsreq = CONTAINER_OF_XREQ(struct afb_wsreq, xreq);
240
241         afb_context_disconnect(&wsreq->xreq.context);
242         afb_wsj1_msg_unref(wsreq->msgj1);
243         afb_cred_unref(wsreq->xreq.cred);
244         afb_ws_json1_unref(wsreq->aws);
245         free(wsreq);
246 }
247
248 static void wsreq_reply(struct afb_xreq *xreq, struct json_object *object, const char *error, const char *info)
249 {
250         struct afb_wsreq *wsreq = CONTAINER_OF_XREQ(struct afb_wsreq, xreq);
251         int rc;
252         struct json_object *reply;
253
254         /* create the reply */
255         reply = afb_msg_json_reply(object, error, info, &xreq->context);
256
257         rc = (error ? afb_wsj1_reply_error_j : afb_wsj1_reply_ok_j)(
258                         wsreq->msgj1, reply, afb_context_sent_token(&wsreq->xreq.context));
259         if (rc)
260                 ERROR("Can't send reply: %m");
261 }
262
263 static int wsreq_subscribe(struct afb_xreq *xreq, struct afb_event_x2 *event)
264 {
265         struct afb_wsreq *wsreq = CONTAINER_OF_XREQ(struct afb_wsreq, xreq);
266
267         return afb_evt_event_x2_add_watch(wsreq->aws->listener, event);
268 }
269
270 static int wsreq_unsubscribe(struct afb_xreq *xreq, struct afb_event_x2 *event)
271 {
272         struct afb_wsreq *wsreq = CONTAINER_OF_XREQ(struct afb_wsreq, xreq);
273
274         return afb_evt_event_x2_remove_watch(wsreq->aws->listener, event);
275 }
276