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