204bdb8379121307ebe437aba307429063193aab
[src/app-framework-binder.git] / src / afb-proto-ws.h
1 /*
2  * Copyright (C) 2016-2019 "IoT.bzh"
3  * Author: José Bollo <jose.bollo@iot.bzh>
4  *
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
8  *
9  *   http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  */
17
18
19 #pragma once
20
21 /*
22  * Defined since version 3, the value AFB_PROTO_WS_VERSION can be used to
23  * track versions of afb-proto-ws. History:
24  *
25  * date          version      comment
26  * 2018/04/09       3         introduced for bindings v3
27  * 2019/11/20       4         introduced for tokens
28  */
29 #define AFB_PROTO_WS_VERSION    4
30
31 struct fdev;
32 struct afb_proto_ws;
33 struct afb_proto_ws_call;
34 struct afb_proto_ws_describe;
35
36 typedef unsigned char afb_proto_ws_uuid_t[16];
37
38 struct afb_proto_ws_client_itf
39 {
40         /* can't be NULL */
41         void (*on_reply)(void *closure, void *request, struct json_object *obj, const char *error, const char *info);
42
43         /* can be NULL */
44         void (*on_event_create)(void *closure, uint16_t event_id, const char *event_name);
45         void (*on_event_remove)(void *closure, uint16_t event_id);
46         void (*on_event_subscribe)(void *closure, void *request, uint16_t event_id);
47         void (*on_event_unsubscribe)(void *closure, void *request, uint16_t event_id);
48         void (*on_event_push)(void *closure, uint16_t event_id, struct json_object *data);
49         void (*on_event_broadcast)(void *closure, const char *event_name, struct json_object *data, const afb_proto_ws_uuid_t uuid, uint8_t hop);
50 };
51
52 struct afb_proto_ws_server_itf
53 {
54         void (*on_session_create)(void *closure, uint16_t sessionid, const char *sessionstr);
55         void (*on_session_remove)(void *closure, uint16_t sessionid);
56         void (*on_token_create)(void *closure, uint16_t tokenid, const char *tokenstr);
57         void (*on_token_remove)(void *closure, uint16_t tokenid);
58         void (*on_call)(void *closure, struct afb_proto_ws_call *call, const char *verb, struct json_object *args, uint16_t sessionid, uint16_t tokenid, const char *user_creds);
59         void (*on_describe)(void *closure, struct afb_proto_ws_describe *describe);
60         void (*on_event_unexpected)(void *closure, uint16_t eventid);
61 };
62
63 extern struct afb_proto_ws *afb_proto_ws_create_client(struct fdev *fdev, const struct afb_proto_ws_client_itf *itf, void *closure);
64 extern struct afb_proto_ws *afb_proto_ws_create_server(struct fdev *fdev, const struct afb_proto_ws_server_itf *itf, void *closure);
65
66 extern void afb_proto_ws_unref(struct afb_proto_ws *protows);
67 extern void afb_proto_ws_addref(struct afb_proto_ws *protows);
68
69 extern int afb_proto_ws_is_client(struct afb_proto_ws *protows);
70 extern int afb_proto_ws_is_server(struct afb_proto_ws *protows);
71
72 extern void afb_proto_ws_hangup(struct afb_proto_ws *protows);
73
74 extern void afb_proto_ws_on_hangup(struct afb_proto_ws *protows, void (*on_hangup)(void *closure));
75 extern void afb_proto_ws_set_queuing(struct afb_proto_ws *protows, int (*queuing)(struct afb_proto_ws *, void (*)(int,void*), void*));
76
77
78 extern int afb_proto_ws_client_session_create(struct afb_proto_ws *protows, uint16_t sessionid, const char *sessionstr);
79 extern int afb_proto_ws_client_session_remove(struct afb_proto_ws *protows, uint16_t sessionid);
80 extern int afb_proto_ws_client_token_create(struct afb_proto_ws *protows, uint16_t tokenid, const char *tokenstr);
81 extern int afb_proto_ws_client_token_remove(struct afb_proto_ws *protows, uint16_t tokenid);
82 extern int afb_proto_ws_client_call(struct afb_proto_ws *protows, const char *verb, struct json_object *args, uint16_t sessionid, uint16_t tokenid, void *request, const char *user_creds);
83 extern int afb_proto_ws_client_describe(struct afb_proto_ws *protows, void (*callback)(void*, struct json_object*), void *closure);
84 extern int afb_proto_ws_client_event_unexpected(struct afb_proto_ws *protows, uint16_t eventid);
85
86 extern int afb_proto_ws_server_event_create(struct afb_proto_ws *protows, uint16_t event_id, const char *event_name);
87 extern int afb_proto_ws_server_event_remove(struct afb_proto_ws *protows, uint16_t event_id);
88 extern int afb_proto_ws_server_event_push(struct afb_proto_ws *protows, uint16_t event_id, struct json_object *data);
89 extern int afb_proto_ws_server_event_broadcast(struct afb_proto_ws *protows, const char *event_name, struct json_object *data, const afb_proto_ws_uuid_t uuid, uint8_t hop);
90
91 extern void afb_proto_ws_call_addref(struct afb_proto_ws_call *call);
92 extern void afb_proto_ws_call_unref(struct afb_proto_ws_call *call);
93
94 extern int afb_proto_ws_call_reply(struct afb_proto_ws_call *call, struct json_object *obj, const char *error, const char *info);
95
96 extern int afb_proto_ws_call_subscribe(struct afb_proto_ws_call *call, uint16_t event_id);
97 extern int afb_proto_ws_call_unsubscribe(struct afb_proto_ws_call *call, uint16_t event_id);
98
99 extern int afb_proto_ws_describe_put(struct afb_proto_ws_describe *describe, struct json_object *description);