2 * Copyright (C) 2016-2019 "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.
26 #include <json-c/json.h>
29 #include "afb-ws-json1.h"
30 #include "afb-msg-json.h"
31 #include "afb-session.h"
33 #include "afb-apiset.h"
35 #include "afb-context.h"
37 #include "afb-token.h"
43 /* predeclaration of structures */
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, int 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);
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);
59 /* declaration of websocket structure */
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;
74 /* declaration of wsreq structure */
78 struct afb_ws_json1 *aws;
79 struct afb_wsreq *next;
80 struct afb_wsj1_msg *msgj1;
83 /* interface for afb_ws_json1 / afb_wsj1 */
84 static struct afb_wsj1_itf wsj1_itf = {
85 .on_hangup = aws_on_hangup_cb,
86 .on_call = aws_on_call_cb
89 /* interface for xreq */
90 const struct afb_xreq_query_itf afb_ws_json1_xreq_itf = {
92 .subscribe = wsreq_subscribe,
93 .unsubscribe = wsreq_unsubscribe,
94 .unref = wsreq_destroy
97 /* the interface for events */
98 static const struct afb_evt_itf evt_itf = {
99 .broadcast = aws_on_broadcast_cb,
100 .push = aws_on_push_cb
103 /***************************************************************
104 ****************************************************************
106 ** functions of afb_ws_json1 / afb_wsj1
108 ****************************************************************
109 ***************************************************************/
111 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)
113 struct afb_ws_json1 *result;
116 assert(context != NULL);
118 result = malloc(sizeof * result);
122 result->refcount = 1;
123 result->cleanup = cleanup;
124 result->cleanup_closure = cleanup_closure;
125 result->session = afb_session_addref(context->session);
126 result->token = afb_token_addref(context->token);
127 result->new_session = context->created != 0;
128 if (result->session == NULL)
131 result->wsj1 = afb_wsj1_create(fdev, &wsj1_itf, result);
132 if (result->wsj1 == NULL)
135 result->listener = afb_evt_listener_create(&evt_itf, result);
136 if (result->listener == NULL)
139 result->cred = afb_cred_create_for_socket(fdev_fd(fdev));
140 result->apiset = afb_apiset_addref(apiset);
144 afb_wsj1_unref(result->wsj1);
146 afb_session_unref(result->session);
147 afb_token_unref(result->token);
155 struct afb_ws_json1 *afb_ws_json1_addref(struct afb_ws_json1 *ws)
157 __atomic_add_fetch(&ws->refcount, 1, __ATOMIC_RELAXED);
161 void afb_ws_json1_unref(struct afb_ws_json1 *ws)
163 if (!__atomic_sub_fetch(&ws->refcount, 1, __ATOMIC_RELAXED)) {
164 afb_evt_listener_unref(ws->listener);
165 afb_wsj1_unref(ws->wsj1);
166 if (ws->cleanup != NULL)
167 ws->cleanup(ws->cleanup_closure);
168 afb_token_unref(ws->token);
169 afb_session_unref(ws->session);
170 afb_cred_unref(ws->cred);
171 afb_apiset_unref(ws->apiset);
176 static void aws_on_hangup_cb(void *closure, struct afb_wsj1 *wsj1)
178 struct afb_ws_json1 *ws = closure;
179 afb_ws_json1_unref(ws);
182 static void aws_on_call_cb(void *closure, const char *api, const char *verb, struct afb_wsj1_msg *msg)
184 struct afb_ws_json1 *ws = closure;
185 struct afb_wsreq *wsreq;
187 DEBUG("received websocket request for %s/%s: %s", api, verb, afb_wsj1_msg_object_s(msg));
190 wsreq = calloc(1, sizeof *wsreq);
192 afb_wsj1_close(ws->wsj1, 1008, NULL);
196 /* init the context */
197 afb_xreq_init(&wsreq->xreq, &afb_ws_json1_xreq_itf);
198 afb_context_init(&wsreq->xreq.context, ws->session, afb_wsj1_msg_token(msg));
199 if (!wsreq->xreq.context.invalidated)
200 wsreq->xreq.context.validated = 1;
201 if (ws->new_session != 0) {
202 wsreq->xreq.context.created = 1;
206 /* fill and record the request */
207 afb_wsj1_msg_addref(msg);
209 wsreq->xreq.cred = afb_cred_addref(ws->cred);
210 wsreq->xreq.request.called_api = api;
211 wsreq->xreq.request.called_verb = verb;
212 wsreq->xreq.json = afb_wsj1_msg_object_j(wsreq->msgj1);
213 wsreq->aws = afb_ws_json1_addref(ws);
216 afb_xreq_process(&wsreq->xreq, ws->apiset);
219 static void aws_on_event(struct afb_ws_json1 *aws, const char *event, struct json_object *object)
221 afb_wsj1_send_event_j(aws->wsj1, event, afb_msg_json_event(event, object));
224 static void aws_on_push_cb(void *closure, const char *event, int eventid, struct json_object *object)
226 aws_on_event(closure, event, object);
229 static void aws_on_broadcast_cb(void *closure, const char *event, struct json_object *object, const uuid_binary_t uuid, uint8_t hop)
231 aws_on_event(closure, event, afb_msg_json_event(event, object));
234 /***************************************************************
235 ****************************************************************
237 ** functions of wsreq / afb_req
239 ****************************************************************
240 ***************************************************************/
242 static void wsreq_destroy(struct afb_xreq *xreq)
244 struct afb_wsreq *wsreq = CONTAINER_OF_XREQ(struct afb_wsreq, xreq);
246 afb_context_disconnect(&wsreq->xreq.context);
247 afb_wsj1_msg_unref(wsreq->msgj1);
248 afb_cred_unref(wsreq->xreq.cred);
249 afb_ws_json1_unref(wsreq->aws);
253 static void wsreq_reply(struct afb_xreq *xreq, struct json_object *object, const char *error, const char *info)
255 struct afb_wsreq *wsreq = CONTAINER_OF_XREQ(struct afb_wsreq, xreq);
257 struct json_object *reply;
259 /* create the reply */
260 reply = afb_msg_json_reply(object, error, info, &xreq->context);
262 rc = (error ? afb_wsj1_reply_error_j : afb_wsj1_reply_ok_j)(
263 wsreq->msgj1, reply, NULL);
265 ERROR("Can't send reply: %m");
268 static int wsreq_subscribe(struct afb_xreq *xreq, struct afb_event_x2 *event)
270 struct afb_wsreq *wsreq = CONTAINER_OF_XREQ(struct afb_wsreq, xreq);
272 return afb_evt_event_x2_add_watch(wsreq->aws->listener, event);
275 static int wsreq_unsubscribe(struct afb_xreq *xreq, struct afb_event_x2 *event)
277 struct afb_wsreq *wsreq = CONTAINER_OF_XREQ(struct afb_wsreq, xreq);
279 return afb_evt_event_x2_remove_watch(wsreq->aws->listener, event);