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.
27 * Interface for callback functions.
28 * The received closure is the closure passed when creating the afb_wsj1
29 * socket using afb_wsj1_create.
33 * This function is called on hangup.
34 * Receives the 'closure' and the handle 'wsj1'
36 void (*on_hangup)(void *closure, struct afb_wsj1 *wsj1);
39 * This function is called on incoming call.
40 * Receives the 'closure'
42 void (*on_call)(void *closure, const char *api, const char *verb, struct afb_wsj1_msg *msg);
45 * This function is called on incoming event
47 void (*on_event)(void *closure, const char *event, struct afb_wsj1_msg *msg);
51 * Creates the afb_wsj1 socket connected to the file descriptor 'fdev'
52 * and having the callback interface defined by 'itf' for the 'closure'.
53 * Returns the created wsj1 websocket or NULL in case of error.
55 extern struct afb_wsj1 *afb_wsj1_create(struct fdev *fdev, struct afb_wsj1_itf *itf, void *closure);
58 * Increases by one the count of reference to 'wsj1'
60 extern void afb_wsj1_addref(struct afb_wsj1 *wsj1);
63 * Decreases by one the count of reference to 'wsj1'
64 * and if it falls to zero releases the used resources
67 extern void afb_wsj1_unref(struct afb_wsj1 *wsj1);
70 * Sends a close message to the websocket of 'wsj1'.
71 * The close message is sent with the 'code' and 'text'.
73 * Return 0 in case of success. Otherwise, returns -1 and set errno.
75 extern int afb_wsj1_close(struct afb_wsj1 *wsj1, uint16_t code, const char *text);
78 * Sends on 'wsj1' the event of name 'event' with the
79 * data 'object'. If not NULL, 'object' should be a valid
81 * Return 0 in case of success. Otherwise, returns -1 and set errno.
83 extern int afb_wsj1_send_event_s(struct afb_wsj1 *wsj1, const char *event, const char *object);
86 * Sends on 'wsj1' the event of name 'event' with the
87 * data 'object'. 'object' can be NULL.
88 * 'object' is dereferenced using 'json_object_put'. Use 'json_object_get' to keep it.
89 * Return 0 in case of success. Otherwise, returns -1 and set errno.
91 extern int afb_wsj1_send_event_j(struct afb_wsj1 *wsj1, const char *event, struct json_object *object);
94 * Sends on 'wsj1' a call to the method of 'api'/'verb' with arguments
95 * given by 'object'. If not NULL, 'object' should be a valid JSON string.
96 * On receiving the reply, the function 'on_reply' is called with 'closure'
97 * as its first argument and the message of the reply.
98 * Return 0 in case of success. Otherwise, returns -1 and set errno.
100 extern int afb_wsj1_call_s(struct afb_wsj1 *wsj1, const char *api, const char *verb, const char *object, void (*on_reply)(void *closure, struct afb_wsj1_msg *msg), void *closure);
103 * Sends on 'wsj1' a call to the method of 'api'/'verb' with arguments
104 * given by 'object'. 'object' can be NULL.
105 * 'object' is dereferenced using 'json_object_put'. Use 'json_object_get' to keep it.
106 * On receiving the reply, the function 'on_reply' is called with 'closure'
107 * as its first argument and the message of the reply.
108 * Return 0 in case of success. Otherwise, returns -1 and set errno.
110 extern int afb_wsj1_call_j(struct afb_wsj1 *wsj1, const char *api, const char *verb, struct json_object *object, void (*on_reply)(void *closure, struct afb_wsj1_msg *msg), void *closure);
113 * Sends for message 'msg' the reply with the 'object' and, if not NULL, the token.
114 * When 'iserror' is zero a OK reply is send, otherwise an ERROR reply is sent.
115 * If not NULL, 'object' should be a valid JSON string.
116 * Return 0 in case of success. Otherwise, returns -1 and set errno.
118 extern int afb_wsj1_reply_s(struct afb_wsj1_msg *msg, const char *object, const char *token, int iserror);
121 * Sends for message 'msg' the reply with the 'object' and, if not NULL, the token.
122 * When 'iserror' is zero a OK reply is send, otherwise an ERROR reply is sent.
123 * 'object' can be NULL.
124 * 'object' is dereferenced using 'json_object_put'. Use 'json_object_get' to keep it.
125 * Return 0 in case of success. Otherwise, returns -1 and set errno.
127 extern int afb_wsj1_reply_j(struct afb_wsj1_msg *msg, struct json_object *object, const char *token, int iserror);
130 * Sends for message 'msg' the OK reply with the 'object' and, if not NULL, the token.
131 * If not NULL, 'object' should be a valid JSON string.
132 * Return 0 in case of success. Otherwise, returns -1 and set errno.
134 static inline int afb_wsj1_reply_ok_s(struct afb_wsj1_msg *msg, const char *object, const char *token)
136 return afb_wsj1_reply_s(msg, object, token, 0);
140 * Sends for message 'msg' the OK reply with the 'object' and, if not NULL, the token.
141 * 'object' can be NULL.
142 * 'object' is dereferenced using 'json_object_put'. Use 'json_object_get' to keep it.
143 * Return 0 in case of success. Otherwise, returns -1 and set errno.
145 static inline int afb_wsj1_reply_ok_j(struct afb_wsj1_msg *msg, struct json_object *object, const char *token)
147 return afb_wsj1_reply_j(msg, object, token, 0);
151 * Sends for message 'msg' the ERROR reply with the 'object' and, if not NULL, the token.
152 * If not NULL, 'object' should be a valid JSON string.
153 * Return 0 in case of success. Otherwise, returns -1 and set errno.
155 static inline int afb_wsj1_reply_error_s(struct afb_wsj1_msg *msg, const char *object, const char *token)
157 return afb_wsj1_reply_s(msg, object, token, 1);
161 * Sends for message 'msg' the ERROR reply with the 'object' and, if not NULL, the token.
162 * 'object' can be NULL.
163 * 'object' is dereferenced using 'json_object_put'. Use 'json_object_get' to keep it.
164 * Return 0 in case of success. Otherwise, returns -1 and set errno.
166 static inline int afb_wsj1_reply_error_j(struct afb_wsj1_msg *msg, struct json_object *object, const char *token)
168 return afb_wsj1_reply_j(msg, object, token, 1);
172 * Increases by one the count of reference to 'msg'.
173 * Should be called if callbacks stores the message.
175 extern void afb_wsj1_msg_addref(struct afb_wsj1_msg *msg);
178 * Decreases by one the count of reference to 'msg'.
179 * and if it falls to zero releases the used resources
180 * and free the memory.
181 * Should be called if 'afb_wsj1_msg_addref' was called.
183 extern void afb_wsj1_msg_unref(struct afb_wsj1_msg *msg);
186 * Returns 1 if 'msg' is for a CALL
187 * Otherwise returns 0.
189 extern int afb_wsj1_msg_is_call(struct afb_wsj1_msg *msg);
192 * Returns 1 if 'msg' is for a REPLY of any kind
193 * Otherwise returns 0.
195 extern int afb_wsj1_msg_is_reply(struct afb_wsj1_msg *msg);
198 * Returns 1 if 'msg' is for a REPLY OK
199 * Otherwise returns 0.
201 extern int afb_wsj1_msg_is_reply_ok(struct afb_wsj1_msg *msg);
204 * Returns 1 if 'msg' is for a REPLY ERROR
205 * Otherwise returns 0.
207 extern int afb_wsj1_msg_is_reply_error(struct afb_wsj1_msg *msg);
210 * Returns 1 if 'msg' is for an EVENT
211 * Otherwise returns 0.
213 extern int afb_wsj1_msg_is_event(struct afb_wsj1_msg *msg);
216 * Returns the api of the call for 'msg'
217 * Returns NULL if 'msg' is not for a CALL
219 extern const char *afb_wsj1_msg_api(struct afb_wsj1_msg *msg);
222 * Returns the verb call for 'msg'
223 * Returns NULL if 'msg' is not for a CALL
225 extern const char *afb_wsj1_msg_verb(struct afb_wsj1_msg *msg);
228 * Returns the event name for 'msg'
229 * Returns NULL if 'msg' is not for an EVENT
231 extern const char *afb_wsj1_msg_event(struct afb_wsj1_msg *msg);
234 * Returns the token sent with 'msg' or NULL when no token was sent.
236 extern const char *afb_wsj1_msg_token(struct afb_wsj1_msg *msg);
239 * Returns the wsj1 of 'msg'
241 extern struct afb_wsj1 *afb_wsj1_msg_wsj1(struct afb_wsj1_msg *msg);
244 * Returns the string representation of the object received with 'msg'
246 extern const char *afb_wsj1_msg_object_s(struct afb_wsj1_msg *msg);
249 * Returns the object received with 'msg'
251 extern struct json_object *afb_wsj1_msg_object_j(struct afb_wsj1_msg *msg);