2 * Copyright (C) 2015-2020 "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, 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);
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;
73 /* declaration of wsreq structure */
77 struct afb_ws_json1 *aws;
78 struct afb_wsreq *next;
79 struct afb_wsj1_msg *msgj1;
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
88 /* interface for xreq */
89 const struct afb_xreq_query_itf afb_ws_json1_xreq_itf = {
91 .subscribe = wsreq_subscribe,
92 .unsubscribe = wsreq_unsubscribe,
93 .unref = wsreq_destroy
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
102 /***************************************************************
103 ****************************************************************
105 ** functions of afb_ws_json1 / afb_wsj1
107 ****************************************************************
108 ***************************************************************/
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)
112 struct afb_ws_json1 *result;
115 assert(context != NULL);
117 result = malloc(sizeof * result);
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)
129 result->wsj1 = afb_wsj1_create(fdev, &wsj1_itf, result);
130 if (result->wsj1 == NULL)
133 result->listener = afb_evt_listener_create(&evt_itf, result);
134 if (result->listener == NULL)
137 result->cred = afb_cred_create_for_socket(fdev_fd(fdev));
138 result->apiset = afb_apiset_addref(apiset);
142 afb_wsj1_unref(result->wsj1);
144 afb_session_unref(result->session);
145 afb_token_unref(result->token);
153 struct afb_ws_json1 *afb_ws_json1_addref(struct afb_ws_json1 *ws)
155 __atomic_add_fetch(&ws->refcount, 1, __ATOMIC_RELAXED);
159 void afb_ws_json1_unref(struct afb_ws_json1 *ws)
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);
174 static void aws_on_hangup_cb(void *closure, struct afb_wsj1 *wsj1)
176 struct afb_ws_json1 *ws = closure;
177 afb_ws_json1_unref(ws);
180 static int aws_new_token(struct afb_ws_json1 *ws, const char *new_token_string)
183 struct afb_token *newtok, *oldtok;
185 rc = afb_token_get(&newtok, new_token_string);
189 afb_token_unref(oldtok);
194 static void aws_on_call_cb(void *closure, const char *api, const char *verb, struct afb_wsj1_msg *msg)
196 struct afb_ws_json1 *ws = closure;
197 struct afb_wsreq *wsreq;
200 DEBUG("received websocket request for %s/%s: %s", api, verb, afb_wsj1_msg_object_s(msg));
202 /* handle new tokens */
203 tok = afb_wsj1_msg_token(msg);
205 aws_new_token(ws, tok);
208 wsreq = calloc(1, sizeof *wsreq);
210 afb_wsj1_close(ws->wsj1, 1008, NULL);
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, ws->cred);
218 /* fill and record the request */
219 afb_wsj1_msg_addref(msg);
221 wsreq->xreq.request.called_api = api;
222 wsreq->xreq.request.called_verb = verb;
223 wsreq->xreq.json = afb_wsj1_msg_object_j(wsreq->msgj1);
224 wsreq->aws = afb_ws_json1_addref(ws);
227 afb_xreq_process(&wsreq->xreq, ws->apiset);
230 static void aws_on_event(struct afb_ws_json1 *aws, const char *event, struct json_object *object)
232 afb_wsj1_send_event_j(aws->wsj1, event, afb_msg_json_event(event, object));
235 static void aws_on_push_cb(void *closure, const char *event, uint16_t eventid, struct json_object *object)
237 aws_on_event(closure, event, object);
240 static void aws_on_broadcast_cb(void *closure, const char *event, struct json_object *object, const uuid_binary_t uuid, uint8_t hop)
242 aws_on_event(closure, event, afb_msg_json_event(event, object));
245 /***************************************************************
246 ****************************************************************
248 ** functions of wsreq / afb_req
250 ****************************************************************
251 ***************************************************************/
253 static void wsreq_destroy(struct afb_xreq *xreq)
255 struct afb_wsreq *wsreq = CONTAINER_OF_XREQ(struct afb_wsreq, xreq);
257 afb_context_disconnect(&wsreq->xreq.context);
258 afb_wsj1_msg_unref(wsreq->msgj1);
259 afb_ws_json1_unref(wsreq->aws);
263 static void wsreq_reply(struct afb_xreq *xreq, struct json_object *object, const char *error, const char *info)
265 struct afb_wsreq *wsreq = CONTAINER_OF_XREQ(struct afb_wsreq, xreq);
267 struct json_object *reply;
269 /* create the reply */
270 reply = afb_msg_json_reply(object, error, info, &xreq->context);
272 rc = (error ? afb_wsj1_reply_error_j : afb_wsj1_reply_ok_j)(
273 wsreq->msgj1, reply, NULL);
275 ERROR("Can't send reply: %m");
278 static int wsreq_subscribe(struct afb_xreq *xreq, struct afb_event_x2 *event)
280 struct afb_wsreq *wsreq = CONTAINER_OF_XREQ(struct afb_wsreq, xreq);
282 return afb_evt_listener_watch_x2(wsreq->aws->listener, event);
285 static int wsreq_unsubscribe(struct afb_xreq *xreq, struct afb_event_x2 *event)
287 struct afb_wsreq *wsreq = CONTAINER_OF_XREQ(struct afb_wsreq, xreq);
289 return afb_evt_listener_unwatch_x2(wsreq->aws->listener, event);