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"
42 /* predeclaration of structures */
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);
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);
58 /* declaration of websocket structure */
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;
72 /* declaration of wsreq structure */
76 struct afb_ws_json1 *aws;
77 struct afb_wsreq *next;
78 struct afb_wsj1_msg *msgj1;
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
87 /* interface for xreq */
88 const struct afb_xreq_query_itf afb_ws_json1_xreq_itf = {
90 .subscribe = wsreq_subscribe,
91 .unsubscribe = wsreq_unsubscribe,
92 .unref = wsreq_destroy
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
101 /***************************************************************
102 ****************************************************************
104 ** functions of afb_ws_json1 / afb_wsj1
106 ****************************************************************
107 ***************************************************************/
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)
111 struct afb_ws_json1 *result;
114 assert(context != NULL);
116 result = malloc(sizeof * result);
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)
128 result->wsj1 = afb_wsj1_create(fdev, &wsj1_itf, result);
129 if (result->wsj1 == NULL)
132 result->listener = afb_evt_listener_create(&evt_itf, result);
133 if (result->listener == NULL)
136 result->cred = afb_cred_create_for_socket(fdev_fd(fdev));
137 result->apiset = afb_apiset_addref(apiset);
141 afb_wsj1_unref(result->wsj1);
143 afb_session_unref(result->session);
151 struct afb_ws_json1 *afb_ws_json1_addref(struct afb_ws_json1 *ws)
153 __atomic_add_fetch(&ws->refcount, 1, __ATOMIC_RELAXED);
157 void afb_ws_json1_unref(struct afb_ws_json1 *ws)
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);
171 static void aws_on_hangup_cb(void *closure, struct afb_wsj1 *wsj1)
173 struct afb_ws_json1 *ws = closure;
174 afb_ws_json1_unref(ws);
177 static void aws_on_call_cb(void *closure, const char *api, const char *verb, struct afb_wsj1_msg *msg)
179 struct afb_ws_json1 *ws = closure;
180 struct afb_wsreq *wsreq;
182 DEBUG("received websocket request for %s/%s: %s", api, verb, afb_wsj1_msg_object_s(msg));
185 wsreq = calloc(1, sizeof *wsreq);
187 afb_wsj1_close(ws->wsj1, 1008, NULL);
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;
201 /* fill and record the request */
202 afb_wsj1_msg_addref(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);
211 afb_xreq_process(&wsreq->xreq, ws->apiset);
214 static void aws_on_event(struct afb_ws_json1 *aws, const char *event, struct json_object *object)
216 afb_wsj1_send_event_j(aws->wsj1, event, afb_msg_json_event(event, object));
219 static void aws_on_push_cb(void *closure, const char *event, int eventid, struct json_object *object)
221 aws_on_event(closure, event, object);
224 static void aws_on_broadcast_cb(void *closure, const char *event, struct json_object *object, const uuid_binary_t uuid, uint8_t hop)
226 aws_on_event(closure, event, afb_msg_json_event(event, object));
229 /***************************************************************
230 ****************************************************************
232 ** functions of wsreq / afb_req
234 ****************************************************************
235 ***************************************************************/
237 static void wsreq_destroy(struct afb_xreq *xreq)
239 struct afb_wsreq *wsreq = CONTAINER_OF_XREQ(struct afb_wsreq, xreq);
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);
248 static void wsreq_reply(struct afb_xreq *xreq, struct json_object *object, const char *error, const char *info)
250 struct afb_wsreq *wsreq = CONTAINER_OF_XREQ(struct afb_wsreq, xreq);
252 struct json_object *reply;
254 /* create the reply */
255 reply = afb_msg_json_reply(object, error, info, &xreq->context);
257 rc = (error ? afb_wsj1_reply_error_j : afb_wsj1_reply_ok_j)(
258 wsreq->msgj1, reply, afb_context_sent_token(&wsreq->xreq.context));
260 ERROR("Can't send reply: %m");
263 static int wsreq_subscribe(struct afb_xreq *xreq, struct afb_event_x2 *event)
265 struct afb_wsreq *wsreq = CONTAINER_OF_XREQ(struct afb_wsreq, xreq);
267 return afb_evt_event_x2_add_watch(wsreq->aws->listener, event);
270 static int wsreq_unsubscribe(struct afb_xreq *xreq, struct afb_event_x2 *event)
272 struct afb_wsreq *wsreq = CONTAINER_OF_XREQ(struct afb_wsreq, xreq);
274 return afb_evt_event_x2_remove_watch(wsreq->aws->listener, event);