2 * Copyright (C) 2016, 2017, 2018 "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-systemd.h"
31 #include "afb-msg-json.h"
32 #include "afb-session.h"
34 #include "afb-apiset.h"
36 #include "afb-context.h"
41 /* predeclaration of structures */
45 /* predeclaration of websocket callbacks */
46 static void aws_on_hangup(struct afb_ws_json1 *ws, struct afb_wsj1 *wsj1);
47 static void aws_on_call(struct afb_ws_json1 *ws, const char *api, const char *verb, struct afb_wsj1_msg *msg);
48 static void aws_on_event(struct afb_ws_json1 *ws, const char *event, int eventid, struct json_object *object);
50 /* predeclaration of wsreq callbacks */
51 static void wsreq_destroy(struct afb_xreq *xreq);
52 static void wsreq_reply(struct afb_xreq *xreq, struct json_object *object, const char *error, const char *info);
54 /* declaration of websocket structure */
58 void (*cleanup)(void*);
59 void *cleanup_closure;
60 struct afb_session *session;
61 struct afb_evt_listener *listener;
62 struct afb_wsj1 *wsj1;
63 struct afb_cred *cred;
64 struct afb_apiset *apiset;
68 /* declaration of wsreq structure */
72 struct afb_ws_json1 *aws;
73 struct afb_wsreq *next;
74 struct afb_wsj1_msg *msgj1;
77 /* interface for afb_ws_json1 / afb_wsj1 */
78 static struct afb_wsj1_itf wsj1_itf = {
79 .on_hangup = (void*)aws_on_hangup,
80 .on_call = (void*)aws_on_call
83 /* interface for xreq */
84 const struct afb_xreq_query_itf afb_ws_json1_xreq_itf = {
86 .unref = wsreq_destroy
89 /* the interface for events */
90 static const struct afb_evt_itf evt_itf = {
91 .broadcast = (void*)aws_on_event,
92 .push = (void*)aws_on_event
95 /***************************************************************
96 ****************************************************************
98 ** functions of afb_ws_json1 / afb_wsj1
100 ****************************************************************
101 ***************************************************************/
103 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)
105 struct afb_ws_json1 *result;
108 assert(context != NULL);
110 result = malloc(sizeof * result);
114 result->refcount = 1;
115 result->cleanup = cleanup;
116 result->cleanup_closure = cleanup_closure;
117 result->session = afb_session_addref(context->session);
118 result->new_session = context->created != 0;
119 if (result->session == NULL)
122 result->wsj1 = afb_wsj1_create(fdev, &wsj1_itf, result);
123 if (result->wsj1 == NULL)
126 result->listener = afb_evt_listener_create(&evt_itf, result);
127 if (result->listener == NULL)
130 result->cred = afb_cred_create_for_socket(fdev_fd(fdev));
131 result->apiset = afb_apiset_addref(apiset);
135 afb_wsj1_unref(result->wsj1);
137 afb_session_unref(result->session);
145 struct afb_ws_json1 *afb_ws_json1_addref(struct afb_ws_json1 *ws)
147 __atomic_add_fetch(&ws->refcount, 1, __ATOMIC_RELAXED);
151 void afb_ws_json1_unref(struct afb_ws_json1 *ws)
153 if (!__atomic_sub_fetch(&ws->refcount, 1, __ATOMIC_RELAXED)) {
154 afb_evt_listener_unref(ws->listener);
155 afb_wsj1_unref(ws->wsj1);
156 if (ws->cleanup != NULL)
157 ws->cleanup(ws->cleanup_closure);
158 afb_session_unref(ws->session);
159 afb_cred_unref(ws->cred);
160 afb_apiset_unref(ws->apiset);
165 static void aws_on_hangup(struct afb_ws_json1 *ws, struct afb_wsj1 *wsj1)
167 afb_ws_json1_unref(ws);
170 static void aws_on_call(struct afb_ws_json1 *ws, const char *api, const char *verb, struct afb_wsj1_msg *msg)
172 struct afb_wsreq *wsreq;
174 DEBUG("received websocket request for %s/%s: %s", api, verb, afb_wsj1_msg_object_s(msg));
177 wsreq = calloc(1, sizeof *wsreq);
179 afb_wsj1_close(ws->wsj1, 1008, NULL);
183 /* init the context */
184 afb_xreq_init(&wsreq->xreq, &afb_ws_json1_xreq_itf);
185 afb_context_init(&wsreq->xreq.context, ws->session, afb_wsj1_msg_token(msg));
186 if (!wsreq->xreq.context.invalidated)
187 wsreq->xreq.context.validated = 1;
188 if (ws->new_session != 0) {
189 wsreq->xreq.context.created = 1;
193 /* fill and record the request */
194 afb_wsj1_msg_addref(msg);
196 wsreq->xreq.cred = afb_cred_addref(ws->cred);
197 wsreq->xreq.request.called_api = api;
198 wsreq->xreq.request.called_verb = verb;
199 wsreq->xreq.json = afb_wsj1_msg_object_j(wsreq->msgj1);
200 wsreq->aws = afb_ws_json1_addref(ws);
201 wsreq->xreq.listener = wsreq->aws->listener;
204 afb_xreq_process(&wsreq->xreq, ws->apiset);
207 static void aws_on_event(struct afb_ws_json1 *aws, const char *event, int eventid, struct json_object *object)
209 afb_wsj1_send_event_j(aws->wsj1, event, afb_msg_json_event(event, object));
212 /***************************************************************
213 ****************************************************************
215 ** functions of wsreq / afb_req
217 ****************************************************************
218 ***************************************************************/
220 static void wsreq_destroy(struct afb_xreq *xreq)
222 struct afb_wsreq *wsreq = CONTAINER_OF_XREQ(struct afb_wsreq, xreq);
224 afb_context_disconnect(&wsreq->xreq.context);
225 afb_wsj1_msg_unref(wsreq->msgj1);
226 afb_cred_unref(wsreq->xreq.cred);
227 afb_ws_json1_unref(wsreq->aws);
231 static void wsreq_reply(struct afb_xreq *xreq, struct json_object *object, const char *error, const char *info)
233 struct afb_wsreq *wsreq = CONTAINER_OF_XREQ(struct afb_wsreq, xreq);
235 struct json_object *reply;
237 /* create the reply */
238 reply = afb_msg_json_reply(object, error, info, &xreq->context);
240 rc = (error ? afb_wsj1_reply_error_j : afb_wsj1_reply_ok_j)(
241 wsreq->msgj1, reply, afb_context_sent_token(&wsreq->xreq.context));
243 ERROR("Can't send reply: %m");