2 * Copyright (C) 2015-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.
28 #include <sys/types.h>
29 #include <sys/socket.h>
33 #include <json-c/json.h>
36 #include "afb-msg-json.h"
37 #include "afb-proto-ws.h"
43 /******** implementation of internal binder protocol per api **************/
46 This protocol is asymmetric: there is a client and a server
48 The client can require the following actions:
54 The server must reply to the previous actions by
56 - answering success or failure of the call
58 - answering the required description
60 The server can also within the context of a call
62 - subscribe or unsubscribe an event
64 For the purpose of handling events the server can:
66 - create/destroy an event
68 - push or broadcast data as an event
71 /************** constants for protocol definition *************************/
73 #define CHAR_FOR_CALL 'K'
74 #define CHAR_FOR_REPLY 'k'
75 #define CHAR_FOR_EVT_BROADCAST 'B'
76 #define CHAR_FOR_EVT_ADD 'E'
77 #define CHAR_FOR_EVT_DEL 'e'
78 #define CHAR_FOR_EVT_PUSH 'P'
79 #define CHAR_FOR_EVT_SUBSCRIBE 'X'
80 #define CHAR_FOR_EVT_UNSUBSCRIBE 'x'
81 #define CHAR_FOR_DESCRIBE 'D'
82 #define CHAR_FOR_DESCRIPTION 'd'
83 #define CHAR_FOR_TOKEN_ADD 'T'
84 #define CHAR_FOR_TOKEN_DROP 't'
85 #define CHAR_FOR_SESSION_ADD 'S'
86 #define CHAR_FOR_SESSION_DROP 's'
87 #define CHAR_FOR_VERSION_OFFER 'V'
88 #define CHAR_FOR_VERSION_SET 'v'
90 /******************* manage versions *****************************/
92 #define WSAPI_IDENTIFIER 02723012011 /* wsapi: 23.19.1.16.9 */
94 #define WSAPI_VERSION_UNSET 0
95 #define WSAPI_VERSION_1 1
97 #define WSAPI_VERSION_MIN WSAPI_VERSION_1
98 #define WSAPI_VERSION_MAX WSAPI_VERSION_1
100 /******************* maximum count of ids ***********************/
102 #define ACTIVE_ID_MAX 4095
104 /******************* handling calls *****************************/
107 * structure for recording calls on client side
110 struct client_call *next; /* the next call */
111 void *request; /* the request closure */
112 uint16_t callid; /* the message identifier */
116 * structure for a ws request
118 struct afb_proto_ws_call {
119 struct afb_proto_ws *protows; /* the client of the request */
120 char *buffer; /* the incoming buffer */
121 uint16_t refcount; /* reference count */
122 uint16_t callid; /* the incoming request callid */
126 * structure for recording describe requests
128 struct client_describe
130 struct client_describe *next;
131 void (*callback)(void*, struct json_object*);
137 * structure for jobs of describing
139 struct afb_proto_ws_describe
141 struct afb_proto_ws *protows;
145 /******************* proto description for client or servers ******************/
149 /* count of references */
155 /* count actives ids */
161 /* resource control */
162 pthread_mutex_t mutex;
167 /* the client closure */
170 /* the client side interface */
171 const struct afb_proto_ws_client_itf *client_itf;
173 /* the server side interface */
174 const struct afb_proto_ws_server_itf *server_itf;
176 /* emitted calls (client side) */
177 struct client_call *calls;
179 /* pending description (client side) */
180 struct client_describe *describes;
182 /* on hangup callback */
183 void (*on_hangup)(void *closure);
185 /* queuing facility for processing messages */
186 int (*queuing)(struct afb_proto_ws *proto, void (*process)(int s, void *c), void *closure);
189 /******************* streaming objects **********************************/
191 #define WRITEBUF_COUNT_MAX 32
192 #define WRITEBUF_BUFSZ (WRITEBUF_COUNT_MAX * sizeof(uint32_t))
196 int iovcount, bufcount;
197 struct iovec iovec[WRITEBUF_COUNT_MAX];
198 char buf[WRITEBUF_BUFSZ];
203 char *base, *head, *end;
208 struct afb_proto_ws *protows;
212 /******************* serialization part **********************************/
214 static char *readbuf_get(struct readbuf *rb, uint32_t length)
216 char *before = rb->head;
217 char *after = before + length;
224 static int readbuf_getat(struct readbuf *rb, void *to, uint32_t length)
226 char *head = readbuf_get(rb, length);
229 memcpy(to, head, length);
233 __attribute__((unused))
234 static int readbuf_char(struct readbuf *rb, char *value)
236 return readbuf_getat(rb, value, sizeof *value);
239 static int readbuf_uint32(struct readbuf *rb, uint32_t *value)
241 int r = readbuf_getat(rb, value, sizeof *value);
243 *value = le32toh(*value);
247 static int readbuf_uint16(struct readbuf *rb, uint16_t *value)
249 int r = readbuf_getat(rb, value, sizeof *value);
251 *value = le16toh(*value);
255 static int readbuf_uint8(struct readbuf *rb, uint8_t *value)
257 return readbuf_getat(rb, value, sizeof *value);
260 static int _readbuf_string_(struct readbuf *rb, const char **value, size_t *length, int nulok)
263 if (!readbuf_uint32(rb, &len))
274 *length = (size_t)(len - 1);
275 return (*value = readbuf_get(rb, len)) != NULL && rb->head[-1] == 0;
279 static int readbuf_string(struct readbuf *rb, const char **value, size_t *length)
281 return _readbuf_string_(rb, value, length, 0);
284 static int readbuf_nullstring(struct readbuf *rb, const char **value, size_t *length)
286 return _readbuf_string_(rb, value, length, 1);
289 static int readbuf_object(struct readbuf *rb, struct json_object **object)
292 struct json_object *o;
293 enum json_tokener_error jerr;
294 int rc = readbuf_string(rb, &string, NULL);
296 o = json_tokener_parse_verbose(string, &jerr);
297 if (jerr != json_tokener_success)
298 o = json_object_new_string(string);
304 static int writebuf_put(struct writebuf *wb, const void *value, size_t length)
306 int i = wb->iovcount;
307 if (i == WRITEBUF_COUNT_MAX)
309 wb->iovec[i].iov_base = (void*)value;
310 wb->iovec[i].iov_len = length;
311 wb->iovcount = i + 1;
315 static int writebuf_putbuf(struct writebuf *wb, const void *value, int length)
318 int i = wb->iovcount, n = wb->bufcount, nafter;
320 /* check enough length */
322 if (nafter > WRITEBUF_BUFSZ)
325 /* get where to store */
327 if (i && p == (((char*)wb->iovec[i - 1].iov_base) + wb->iovec[i - 1].iov_len))
328 /* increase previous iovec */
329 wb->iovec[i - 1].iov_len += (size_t)length;
330 else if (i == WRITEBUF_COUNT_MAX)
335 wb->iovec[i].iov_base = p;
336 wb->iovec[i].iov_len = (size_t)length;
337 wb->iovcount = i + 1;
340 memcpy(p, value, (size_t)length);
341 wb->bufcount = nafter;
345 __attribute__((unused))
346 static int writebuf_char(struct writebuf *wb, char value)
348 return writebuf_putbuf(wb, &value, 1);
351 static int writebuf_uint32(struct writebuf *wb, uint32_t value)
353 value = htole32(value);
354 return writebuf_putbuf(wb, &value, (int)sizeof value);
357 static int writebuf_uint16(struct writebuf *wb, uint16_t value)
359 value = htole16(value);
360 return writebuf_putbuf(wb, &value, (int)sizeof value);
363 static int writebuf_uint8(struct writebuf *wb, uint8_t value)
365 return writebuf_putbuf(wb, &value, (int)sizeof value);
368 static int writebuf_string_length(struct writebuf *wb, const char *value, size_t length)
370 uint32_t len = (uint32_t)++length;
371 return (size_t)len == length && len && writebuf_uint32(wb, len) && writebuf_put(wb, value, length);
374 static int writebuf_string(struct writebuf *wb, const char *value)
376 return writebuf_string_length(wb, value, strlen(value));
379 static int writebuf_nullstring(struct writebuf *wb, const char *value)
381 return value ? writebuf_string_length(wb, value, strlen(value)) : writebuf_uint32(wb, 0);
384 static int writebuf_object(struct writebuf *wb, struct json_object *object)
386 const char *string = json_object_to_json_string_ext(object, JSON_C_TO_STRING_PLAIN);
387 return string != NULL && writebuf_string(wb, string);
390 /******************* queuing of messages *****************/
392 /* queue the processing of the received message (except if size=0 cause it's not a valid message) */
393 static void queue_message_processing(struct afb_proto_ws *protows, char *data, size_t size, void (*processing)(int,void*))
395 struct binary *binary;
398 binary = malloc(sizeof *binary);
400 /* TODO process the problem */
403 binary->protows = protows;
404 binary->rb.base = data;
405 binary->rb.head = data;
406 binary->rb.end = data + size;
407 if (!protows->queuing
408 || protows->queuing(protows, processing, binary) < 0)
409 processing(0, binary);
416 /******************* sending messages *****************/
418 static int proto_write(struct afb_proto_ws *protows, struct writebuf *wb)
423 pthread_mutex_lock(&protows->mutex);
429 rc = afb_ws_binary_v(ws, wb->iovec, wb->iovcount);
433 pthread_mutex_unlock(&protows->mutex);
437 static int send_version_offer_1(struct afb_proto_ws *protows, uint8_t version)
440 struct writebuf wb = { .iovcount = 0, .bufcount = 0 };
442 if (writebuf_char(&wb, CHAR_FOR_VERSION_OFFER)
443 && writebuf_uint32(&wb, WSAPI_IDENTIFIER)
444 && writebuf_uint8(&wb, 1) /* offer one version */
445 && writebuf_uint8(&wb, version))
446 rc = proto_write(protows, &wb);
450 static int send_version_set(struct afb_proto_ws *protows, uint8_t version)
453 struct writebuf wb = { .iovcount = 0, .bufcount = 0 };
455 if (writebuf_char(&wb, CHAR_FOR_VERSION_SET)
456 && writebuf_uint8(&wb, version))
457 rc = proto_write(protows, &wb);
461 /******************* ws request part for server *****************/
463 void afb_proto_ws_call_addref(struct afb_proto_ws_call *call)
465 __atomic_add_fetch(&call->refcount, 1, __ATOMIC_RELAXED);
468 void afb_proto_ws_call_unref(struct afb_proto_ws_call *call)
470 if (__atomic_sub_fetch(&call->refcount, 1, __ATOMIC_RELAXED))
473 afb_proto_ws_unref(call->protows);
478 int afb_proto_ws_call_reply(struct afb_proto_ws_call *call, struct json_object *obj, const char *error, const char *info)
481 struct writebuf wb = { .iovcount = 0, .bufcount = 0 };
482 struct afb_proto_ws *protows = call->protows;
484 if (writebuf_char(&wb, CHAR_FOR_REPLY)
485 && writebuf_uint16(&wb, call->callid)
486 && writebuf_nullstring(&wb, error)
487 && writebuf_nullstring(&wb, info)
488 && writebuf_object(&wb, obj))
489 rc = proto_write(protows, &wb);
493 int afb_proto_ws_call_subscribe(struct afb_proto_ws_call *call, uint16_t event_id)
496 struct writebuf wb = { .iovcount = 0, .bufcount = 0 };
497 struct afb_proto_ws *protows = call->protows;
499 if (writebuf_char(&wb, CHAR_FOR_EVT_SUBSCRIBE)
500 && writebuf_uint16(&wb, call->callid)
501 && writebuf_uint16(&wb, event_id))
502 rc = proto_write(protows, &wb);
506 int afb_proto_ws_call_unsubscribe(struct afb_proto_ws_call *call, uint16_t event_id)
509 struct writebuf wb = { .iovcount = 0, .bufcount = 0 };
510 struct afb_proto_ws *protows = call->protows;
512 if (writebuf_char(&wb, CHAR_FOR_EVT_UNSUBSCRIBE)
513 && writebuf_uint16(&wb, call->callid)
514 && writebuf_uint16(&wb, event_id))
515 rc = proto_write(protows, &wb);
519 /******************* client part **********************************/
521 /* search a memorized call */
522 static struct client_call *client_call_search_locked(struct afb_proto_ws *protows, uint16_t callid)
524 struct client_call *call;
526 call = protows->calls;
527 while (call != NULL && call->callid != callid)
533 static struct client_call *client_call_search_unlocked(struct afb_proto_ws *protows, uint16_t callid)
535 struct client_call *result;
537 pthread_mutex_lock(&protows->mutex);
538 result = client_call_search_locked(protows, callid);
539 pthread_mutex_unlock(&protows->mutex);
543 /* free and release the memorizing call */
544 static void client_call_destroy(struct afb_proto_ws *protows, struct client_call *call)
546 struct client_call **prv;
548 pthread_mutex_lock(&protows->mutex);
549 prv = &protows->calls;
550 while (*prv != NULL) {
554 pthread_mutex_unlock(&protows->mutex);
560 pthread_mutex_unlock(&protows->mutex);
563 /* get event from the message */
564 static int client_msg_call_get(struct afb_proto_ws *protows, struct readbuf *rb, struct client_call **call)
568 /* get event data from the message */
569 if (!readbuf_uint16(rb, &callid))
573 *call = client_call_search_unlocked(protows, callid);
574 return *call != NULL;
578 static void client_on_event_create(struct afb_proto_ws *protows, struct readbuf *rb)
580 const char *event_name;
583 if (protows->client_itf->on_event_create
584 && readbuf_uint16(rb, &event_id)
585 && readbuf_string(rb, &event_name, NULL))
586 protows->client_itf->on_event_create(protows->closure, event_id, event_name);
588 ERROR("Ignoring creation of event");
591 /* removes an event */
592 static void client_on_event_remove(struct afb_proto_ws *protows, struct readbuf *rb)
596 if (protows->client_itf->on_event_remove && readbuf_uint16(rb, &event_id))
597 protows->client_itf->on_event_remove(protows->closure, event_id);
599 ERROR("Ignoring deletion of event");
602 /* subscribes an event */
603 static void client_on_event_subscribe(struct afb_proto_ws *protows, struct readbuf *rb)
606 struct client_call *call;
608 if (protows->client_itf->on_event_subscribe && client_msg_call_get(protows, rb, &call) && readbuf_uint16(rb, &event_id))
609 protows->client_itf->on_event_subscribe(protows->closure, call->request, event_id);
611 ERROR("Ignoring subscription to event");
614 /* unsubscribes an event */
615 static void client_on_event_unsubscribe(struct afb_proto_ws *protows, struct readbuf *rb)
618 struct client_call *call;
620 if (protows->client_itf->on_event_unsubscribe && client_msg_call_get(protows, rb, &call) && readbuf_uint16(rb, &event_id))
621 protows->client_itf->on_event_unsubscribe(protows->closure, call->request, event_id);
623 ERROR("Ignoring unsubscription to event");
626 /* receives broadcasted events */
627 static void client_on_event_broadcast(struct afb_proto_ws *protows, struct readbuf *rb)
629 const char *event_name, *uuid;
631 struct json_object *object;
633 if (protows->client_itf->on_event_broadcast && readbuf_string(rb, &event_name, NULL) && readbuf_object(rb, &object) && (uuid = readbuf_get(rb, 16)) && readbuf_uint8(rb, &hop))
634 protows->client_itf->on_event_broadcast(protows->closure, event_name, object, (unsigned char*)uuid, hop);
636 ERROR("Ignoring broadcast of event");
640 static void client_on_event_push(struct afb_proto_ws *protows, struct readbuf *rb)
643 struct json_object *object;
645 if (protows->client_itf->on_event_push && readbuf_uint16(rb, &event_id) && readbuf_object(rb, &object))
646 protows->client_itf->on_event_push(protows->closure, event_id, object);
648 ERROR("Ignoring push of event");
651 static void client_on_reply(struct afb_proto_ws *protows, struct readbuf *rb)
653 struct client_call *call;
654 struct json_object *object;
655 const char *error, *info;
657 if (!client_msg_call_get(protows, rb, &call))
660 if (readbuf_nullstring(rb, &error, NULL) && readbuf_nullstring(rb, &info, NULL) && readbuf_object(rb, &object)) {
661 protows->client_itf->on_reply(protows->closure, call->request, object, error, info);
663 protows->client_itf->on_reply(protows->closure, call->request, NULL, "proto-error", "can't process success");
665 client_call_destroy(protows, call);
668 static void client_on_description(struct afb_proto_ws *protows, struct readbuf *rb)
671 struct client_describe *desc, **prv;
672 struct json_object *object;
674 if (readbuf_uint32(rb, &descid)) {
675 pthread_mutex_lock(&protows->mutex);
676 prv = &protows->describes;
677 while ((desc = *prv) && desc->descid != descid)
680 pthread_mutex_unlock(&protows->mutex);
684 pthread_mutex_unlock(&protows->mutex);
685 if (!readbuf_object(rb, &object))
687 desc->callback(desc->closure, object);
693 /* received a version set */
694 static void client_on_version_set(struct afb_proto_ws *protows, struct readbuf *rb)
698 /* reads the descid */
699 if (readbuf_uint8(rb, &version)
700 && WSAPI_VERSION_MIN <= version
701 && version <= WSAPI_VERSION_MAX) {
702 protows->version = version;
705 afb_proto_ws_hangup(protows);
709 /* callback when receiving binary data */
710 static void client_on_binary_job(int sig, void *closure)
712 struct binary *binary = closure;
715 switch (*binary->rb.head++) {
716 case CHAR_FOR_REPLY: /* reply */
717 client_on_reply(binary->protows, &binary->rb);
719 case CHAR_FOR_EVT_BROADCAST: /* broadcast */
720 client_on_event_broadcast(binary->protows, &binary->rb);
722 case CHAR_FOR_EVT_ADD: /* creates the event */
723 client_on_event_create(binary->protows, &binary->rb);
725 case CHAR_FOR_EVT_DEL: /* removes the event */
726 client_on_event_remove(binary->protows, &binary->rb);
728 case CHAR_FOR_EVT_PUSH: /* pushs the event */
729 client_on_event_push(binary->protows, &binary->rb);
731 case CHAR_FOR_EVT_SUBSCRIBE: /* subscribe event for a request */
732 client_on_event_subscribe(binary->protows, &binary->rb);
734 case CHAR_FOR_EVT_UNSUBSCRIBE: /* unsubscribe event for a request */
735 client_on_event_unsubscribe(binary->protows, &binary->rb);
737 case CHAR_FOR_DESCRIPTION: /* description */
738 client_on_description(binary->protows, &binary->rb);
740 case CHAR_FOR_VERSION_SET: /* set the protocol version */
741 client_on_version_set(binary->protows, &binary->rb);
743 default: /* unexpected message */
744 /* TODO: close the connection */
748 free(binary->rb.base);
752 /* callback when receiving binary data */
753 static void client_on_binary(void *closure, char *data, size_t size)
755 struct afb_proto_ws *protows = closure;
757 queue_message_processing(protows, data, size, client_on_binary_job);
760 static int client_send_idstr_add_drop(struct afb_proto_ws *protows, char order, uint16_t id, const char *value)
762 struct writebuf wb = { .iovcount = 0, .bufcount = 0 };
765 if (writebuf_char(&wb, order)
766 && writebuf_uint16(&wb, id)
767 && (!value || writebuf_string(&wb, value)))
768 rc = proto_write(protows, &wb);
772 int afb_proto_ws_client_session_create(struct afb_proto_ws *protows, uint16_t sessionid, const char *sessionstr)
774 return client_send_idstr_add_drop(protows, CHAR_FOR_SESSION_ADD, sessionid, sessionstr);
777 int afb_proto_ws_client_session_remove(struct afb_proto_ws *protows, uint16_t sessionid)
779 return client_send_idstr_add_drop(protows, CHAR_FOR_SESSION_DROP, sessionid, NULL);
782 int afb_proto_ws_client_token_create(struct afb_proto_ws *protows, uint16_t tokenid, const char *tokenstr)
784 return client_send_idstr_add_drop(protows, CHAR_FOR_TOKEN_ADD, tokenid, tokenstr);
788 int afb_proto_ws_client_token_remove(struct afb_proto_ws *protows, uint16_t tokenid)
790 return client_send_idstr_add_drop(protows, CHAR_FOR_TOKEN_DROP, tokenid, NULL);
793 int afb_proto_ws_client_call(
794 struct afb_proto_ws *protows,
796 struct json_object *args,
800 const char *user_creds
804 struct client_call *call;
805 struct writebuf wb = { .iovcount = 0, .bufcount = 0 };
808 /* allocate call data */
809 call = malloc(sizeof *call);
814 call->request = request;
817 pthread_mutex_lock(&protows->mutex);
818 if (protows->idcount >= ACTIVE_ID_MAX) {
819 pthread_mutex_unlock(&protows->mutex);
824 id = ++protows->genid;
825 while(!id || client_call_search_locked(protows, id) != NULL)
827 call->callid = protows->genid = id;
828 call->next = protows->calls;
829 protows->calls = call;
830 pthread_mutex_unlock(&protows->mutex);
832 /* creates the call message */
833 if (!writebuf_char(&wb, CHAR_FOR_CALL)
834 || !writebuf_uint16(&wb, call->callid)
835 || !writebuf_string(&wb, verb)
836 || !writebuf_uint16(&wb, sessionid)
837 || !writebuf_uint16(&wb, tokenid)
838 || !writebuf_object(&wb, args)
839 || !writebuf_nullstring(&wb, user_creds)) {
845 rc = proto_write(protows, &wb);
850 client_call_destroy(protows, call);
855 /* get the description */
856 int afb_proto_ws_client_describe(struct afb_proto_ws *protows, void (*callback)(void*, struct json_object*), void *closure)
858 struct client_describe *desc, *d;
859 struct writebuf wb = { .iovcount = 0, .bufcount = 0 };
862 desc = malloc(sizeof *desc);
868 /* fill in stack the description of the task */
869 pthread_mutex_lock(&protows->mutex);
870 if (protows->idcount >= ACTIVE_ID_MAX) {
875 id = ++protows->genid;
876 d = protows->describes;
878 if (id && d->descid != id)
882 d = protows->describes;
885 desc->descid = protows->genid = id;
886 desc->callback = callback;
887 desc->closure = closure;
888 desc->next = protows->describes;
889 protows->describes = desc;
890 pthread_mutex_unlock(&protows->mutex);
893 if (!writebuf_char(&wb, CHAR_FOR_DESCRIBE)
894 || !writebuf_uint16(&wb, desc->descid)) {
899 if (proto_write(protows, &wb) == 0)
903 pthread_mutex_lock(&protows->mutex);
904 d = protows->describes;
906 protows->describes = desc->next;
908 while(d && d->next != desc)
911 d->next = desc->next;
915 pthread_mutex_unlock(&protows->mutex);
918 /* TODO? callback(closure, NULL); */
922 /******************* client description part for server *****************************/
924 /* on call, propagate it to the ws service */
925 static void server_on_call(struct afb_proto_ws *protows, struct readbuf *rb)
927 struct afb_proto_ws_call *call;
928 const char *verb, *user_creds;
929 uint16_t callid, sessionid, tokenid;
931 struct json_object *object;
933 afb_proto_ws_addref(protows);
935 /* reads the call message data */
936 if (!readbuf_uint16(rb, &callid)
937 || !readbuf_string(rb, &verb, &lenverb)
938 || !readbuf_uint16(rb, &sessionid)
939 || !readbuf_uint16(rb, &tokenid)
940 || !readbuf_object(rb, &object)
941 || !readbuf_nullstring(rb, &user_creds, NULL))
944 /* create the request */
945 call = malloc(sizeof *call);
949 call->protows = protows;
950 call->callid = callid;
952 call->buffer = rb->base;
953 rb->base = NULL; /* don't free the buffer */
955 protows->server_itf->on_call(protows->closure, call, verb, object, sessionid, tokenid, user_creds);
959 json_object_put(object);
962 afb_proto_ws_unref(protows);
965 static int server_send_description(struct afb_proto_ws *protows, uint32_t descid, struct json_object *descobj)
968 struct writebuf wb = { .iovcount = 0, .bufcount = 0 };
970 if (writebuf_char(&wb, CHAR_FOR_DESCRIPTION)
971 && writebuf_uint32(&wb, descid)
972 && writebuf_object(&wb, descobj))
973 rc = proto_write(protows, &wb);
977 int afb_proto_ws_describe_put(struct afb_proto_ws_describe *describe, struct json_object *description)
979 int rc = server_send_description(describe->protows, describe->descid, description);
980 afb_proto_ws_unref(describe->protows);
985 /* on describe, propagate it to the ws service */
986 static void server_on_describe(struct afb_proto_ws *protows, struct readbuf *rb)
989 struct afb_proto_ws_describe *desc;
991 /* reads the descid */
992 if (readbuf_uint16(rb, &descid)) {
993 if (protows->server_itf->on_describe) {
994 /* create asynchronous job */
995 desc = malloc(sizeof *desc);
997 desc->descid = descid;
998 desc->protows = protows;
999 afb_proto_ws_addref(protows);
1000 protows->server_itf->on_describe(protows->closure, desc);
1004 server_send_description(protows, descid, NULL);
1008 static void server_on_session_add(struct afb_proto_ws *protows, struct readbuf *rb)
1011 const char *sessionstr;
1013 if (readbuf_uint16(rb, &sessionid) && readbuf_string(rb, &sessionstr, NULL))
1014 protows->server_itf->on_session_create(protows->closure, sessionid, sessionstr);
1017 static void server_on_session_drop(struct afb_proto_ws *protows, struct readbuf *rb)
1021 if (readbuf_uint16(rb, &sessionid))
1022 protows->server_itf->on_session_remove(protows->closure, sessionid);
1025 static void server_on_token_add(struct afb_proto_ws *protows, struct readbuf *rb)
1028 const char *tokenstr;
1030 if (readbuf_uint16(rb, &tokenid) && readbuf_string(rb, &tokenstr, NULL))
1031 protows->server_itf->on_token_create(protows->closure, tokenid, tokenstr);
1034 static void server_on_token_drop(struct afb_proto_ws *protows, struct readbuf *rb)
1038 if (readbuf_uint16(rb, &tokenid))
1039 protows->server_itf->on_token_remove(protows->closure, tokenid);
1042 /* on version offer */
1043 static void server_on_version_offer(struct afb_proto_ws *protows, struct readbuf *rb)
1051 /* reads the descid */
1052 if (readbuf_uint32(rb, &id)
1053 && id == WSAPI_IDENTIFIER
1054 && readbuf_uint8(rb, &count)
1056 && (versions = (uint8_t*)readbuf_get(rb, (uint32_t)count))) {
1057 version = WSAPI_VERSION_UNSET;
1059 v = versions[--count];
1060 if (v >= WSAPI_VERSION_MIN
1061 && v <= WSAPI_VERSION_MAX
1062 && (version == WSAPI_VERSION_UNSET || version < v))
1065 if (version != WSAPI_VERSION_UNSET) {
1066 if (send_version_set(protows, version) >= 0) {
1067 protows->version = version;
1072 afb_proto_ws_hangup(protows);
1075 /* callback when receiving binary data */
1076 static void server_on_binary_job(int sig, void *closure)
1078 struct binary *binary = closure;
1081 switch (*binary->rb.head++) {
1083 server_on_call(binary->protows, &binary->rb);
1085 case CHAR_FOR_DESCRIBE:
1086 server_on_describe(binary->protows, &binary->rb);
1088 case CHAR_FOR_SESSION_ADD:
1089 server_on_session_add(binary->protows, &binary->rb);
1091 case CHAR_FOR_SESSION_DROP:
1092 server_on_session_drop(binary->protows, &binary->rb);
1094 case CHAR_FOR_TOKEN_ADD:
1095 server_on_token_add(binary->protows, &binary->rb);
1097 case CHAR_FOR_TOKEN_DROP:
1098 server_on_token_drop(binary->protows, &binary->rb);
1100 case CHAR_FOR_VERSION_OFFER:
1101 server_on_version_offer(binary->protows, &binary->rb);
1103 default: /* unexpected message */
1104 /* TODO: close the connection */
1108 free(binary->rb.base);
1112 static void server_on_binary(void *closure, char *data, size_t size)
1114 struct afb_proto_ws *protows = closure;
1116 queue_message_processing(protows, data, size, server_on_binary_job);
1119 /******************* server part: manage events **********************************/
1121 static int server_event_send(struct afb_proto_ws *protows, char order, uint16_t event_id, const char *event_name, struct json_object *data)
1123 struct writebuf wb = { .iovcount = 0, .bufcount = 0 };
1126 if (writebuf_char(&wb, order)
1127 && writebuf_uint16(&wb, event_id)
1128 && (order != CHAR_FOR_EVT_ADD || writebuf_string(&wb, event_name))
1129 && (order != CHAR_FOR_EVT_PUSH || writebuf_object(&wb, data)))
1130 rc = proto_write(protows, &wb);
1134 int afb_proto_ws_server_event_create(struct afb_proto_ws *protows, uint16_t event_id, const char *event_name)
1136 return server_event_send(protows, CHAR_FOR_EVT_ADD, event_id, event_name, NULL);
1139 int afb_proto_ws_server_event_remove(struct afb_proto_ws *protows, uint16_t event_id)
1141 return server_event_send(protows, CHAR_FOR_EVT_DEL, event_id, NULL, NULL);
1144 int afb_proto_ws_server_event_push(struct afb_proto_ws *protows, uint16_t event_id, struct json_object *data)
1146 return server_event_send(protows, CHAR_FOR_EVT_PUSH, event_id, NULL, data);
1149 int afb_proto_ws_server_event_broadcast(struct afb_proto_ws *protows, const char *event_name, struct json_object *data, const unsigned char uuid[16], uint8_t hop)
1151 struct writebuf wb = { .iovcount = 0, .bufcount = 0 };
1157 if (writebuf_char(&wb, CHAR_FOR_EVT_BROADCAST)
1158 && writebuf_string(&wb, event_name)
1159 && writebuf_object(&wb, data)
1160 && writebuf_put(&wb, uuid, 16)
1161 && writebuf_uint8(&wb, (uint8_t)(hop - 1)))
1162 rc = proto_write(protows, &wb);
1166 /*****************************************************/
1168 /* callback when receiving a hangup */
1169 static void on_hangup(void *closure)
1171 struct afb_proto_ws *protows = closure;
1172 struct client_describe *cd, *ncd;
1173 struct client_call *call, *ncall;
1176 pthread_mutex_lock(&protows->mutex);
1177 ncd = protows->describes;
1178 protows->describes = NULL;
1179 ncall = protows->calls;
1180 protows->calls = NULL;
1183 protows->idcount = 0;
1184 pthread_mutex_unlock(&protows->mutex);
1189 protows->client_itf->on_reply(protows->closure, call->request, NULL, "disconnected", "server hung up");
1196 cd->callback(cd->closure, NULL);
1202 if (protows->on_hangup)
1203 protows->on_hangup(protows->closure);
1207 /*****************************************************/
1209 static const struct afb_ws_itf proto_ws_client_ws_itf =
1213 .on_binary = client_on_binary,
1215 .on_hangup = on_hangup
1218 static const struct afb_ws_itf server_ws_itf =
1222 .on_binary = server_on_binary,
1224 .on_hangup = on_hangup
1227 /*****************************************************/
1229 static struct afb_proto_ws *afb_proto_ws_create(struct fdev *fdev, const struct afb_proto_ws_server_itf *itfs, const struct afb_proto_ws_client_itf *itfc, void *closure, const struct afb_ws_itf *itf)
1231 struct afb_proto_ws *protows;
1233 protows = calloc(1, sizeof *protows);
1234 if (protows == NULL)
1237 fcntl(fdev_fd(fdev), F_SETFD, FD_CLOEXEC);
1238 fcntl(fdev_fd(fdev), F_SETFL, O_NONBLOCK);
1239 protows->ws = afb_ws_create(fdev, itf, protows);
1240 if (protows->ws != NULL) {
1241 protows->refcount = 1;
1242 protows->version = WSAPI_VERSION_UNSET;
1243 protows->closure = closure;
1244 protows->server_itf = itfs;
1245 protows->client_itf = itfc;
1246 pthread_mutex_init(&protows->mutex, NULL);
1254 struct afb_proto_ws *afb_proto_ws_create_client(struct fdev *fdev, const struct afb_proto_ws_client_itf *itf, void *closure)
1256 struct afb_proto_ws *protows;
1258 protows = afb_proto_ws_create(fdev, NULL, itf, closure, &proto_ws_client_ws_itf);
1260 if (send_version_offer_1(protows, WSAPI_VERSION_1) != 0) {
1261 afb_proto_ws_unref(protows);
1268 struct afb_proto_ws *afb_proto_ws_create_server(struct fdev *fdev, const struct afb_proto_ws_server_itf *itf, void *closure)
1270 return afb_proto_ws_create(fdev, itf, NULL, closure, &server_ws_itf);
1273 void afb_proto_ws_unref(struct afb_proto_ws *protows)
1275 if (protows && !__atomic_sub_fetch(&protows->refcount, 1, __ATOMIC_RELAXED)) {
1276 afb_proto_ws_hangup(protows);
1277 pthread_mutex_destroy(&protows->mutex);
1282 void afb_proto_ws_addref(struct afb_proto_ws *protows)
1284 __atomic_add_fetch(&protows->refcount, 1, __ATOMIC_RELAXED);
1287 int afb_proto_ws_is_client(struct afb_proto_ws *protows)
1289 return !!protows->client_itf;
1292 int afb_proto_ws_is_server(struct afb_proto_ws *protows)
1294 return !!protows->server_itf;
1297 void afb_proto_ws_hangup(struct afb_proto_ws *protows)
1300 afb_ws_hangup(protows->ws);
1303 void afb_proto_ws_on_hangup(struct afb_proto_ws *protows, void (*on_hangup)(void *closure))
1305 protows->on_hangup = on_hangup;
1308 void afb_proto_ws_set_queuing(struct afb_proto_ws *protows, int (*queuing)(struct afb_proto_ws*, void (*)(int,void*), void*))
1310 protows->queuing = queuing;