2 * Copyright (C) 2016 "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 * Interface for callback functions.
27 * The received closure is the closure passed when creating the afb_wsj1
28 * socket using afb_wsj1_create.
32 * This function is called on hangup.
33 * Receives the 'closure' and the handle 'wsj1'
35 void (*on_hangup)(void *closure, struct afb_wsj1 *wsj1);
38 * This function is called on incoming call.
39 * Receives the 'closure'
41 void (*on_call)(void *closure, const char *api, const char *verb, struct afb_wsj1_msg *msg);
44 * This function is called on incoming event
46 void (*on_event)(void *closure, const char *event, struct afb_wsj1_msg *msg);
50 * Creates the afb_wsj1 socket connected to the file descriptor 'fd'
51 * and having the callback interface defined by 'itf' for the 'closure'.
52 * Returns the created wsj1 websocket or NULL in case of error.
54 extern struct afb_wsj1 *afb_wsj1_create(int fd, struct afb_wsj1_itf *itf, void *closure);
57 * Increases by one the count of reference to 'wsj1'
59 extern void afb_wsj1_addref(struct afb_wsj1 *wsj1);
62 * Decreases by one the count of reference to 'wsj1'
63 * and if it falls to zero releases the used resources
66 extern void afb_wsj1_unref(struct afb_wsj1 *wsj1);
69 * Sends on 'wsj1' the event of name 'event' with the
70 * data 'object'. If not NULL, 'object' should be a valid
72 * Return 0 in case of success. Otherwise, returns -1 and set errno.
74 extern int afb_wsj1_send_event_s(struct afb_wsj1 *wsj1, const char *event, const char *object);
77 * Sends on 'wsj1' the event of name 'event' with the
78 * data 'object'. 'object' can be NULL.
79 * Return 0 in case of success. Otherwise, returns -1 and set errno.
81 extern int afb_wsj1_send_event_j(struct afb_wsj1 *wsj1, const char *event, struct json_object *object);
84 * Sends on 'wsj1' a call to the method of 'api'/'verb' with arguments
85 * given by 'object'. If not NULL, 'object' should be a valid JSON string.
86 * On receiving the reply, the function 'on_reply' is called with 'closure'
87 * as its first argument and the message of the reply.
88 * Return 0 in case of success. Otherwise, returns -1 and set errno.
90 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);
93 * Sends on 'wsj1' a call to the method of 'api'/'verb' with arguments
94 * given by 'object'. 'object' can be NULL.
95 * On receiving the reply, the function 'on_reply' is called with 'closure'
96 * as its first argument and the message of the reply.
97 * Return 0 in case of success. Otherwise, returns -1 and set errno.
99 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);
102 * Sends for message 'msg' the reply with the 'object' and, if not NULL, the token.
103 * When 'iserror' is zero a OK reply is send, otherwise an ERROR reply is sent.
104 * If not NULL, 'object' should be a valid JSON string.
105 * Return 0 in case of success. Otherwise, returns -1 and set errno.
107 extern int afb_wsj1_reply_s(struct afb_wsj1_msg *msg, const char *object, const char *token, int iserror);
110 * Sends for message 'msg' the reply with the 'object' and, if not NULL, the token.
111 * When 'iserror' is zero a OK reply is send, otherwise an ERROR reply is sent.
112 * 'object' can be NULL.
113 * Return 0 in case of success. Otherwise, returns -1 and set errno.
115 extern int afb_wsj1_reply_j(struct afb_wsj1_msg *msg, struct json_object *object, const char *token, int iserror);
121 * Sends for message 'msg' the OK reply with the 'object' and, if not NULL, the token.
122 * If not NULL, 'object' should be a valid JSON string.
123 * Return 0 in case of success. Otherwise, returns -1 and set errno.
125 static inline int afb_wsj1_reply_ok_s(struct afb_wsj1_msg *msg, const char *object, const char *token)
127 return afb_wsj1_reply_s(msg, object, token, 0);
131 * Sends for message 'msg' the OK reply with the 'object' and, if not NULL, the token.
132 * 'object' can be NULL.
133 * Return 0 in case of success. Otherwise, returns -1 and set errno.
135 static inline int afb_wsj1_reply_ok_j(struct afb_wsj1_msg *msg, struct json_object *object, const char *token)
137 return afb_wsj1_reply_j(msg, object, token, 0);
141 * Sends for message 'msg' the ERROR reply with the 'object' and, if not NULL, the token.
142 * If not NULL, 'object' should be a valid JSON string.
143 * Return 0 in case of success. Otherwise, returns -1 and set errno.
145 static inline int afb_wsj1_reply_error_s(struct afb_wsj1_msg *msg, const char *object, const char *token)
147 return afb_wsj1_reply_s(msg, object, token, 1);
151 * Sends for message 'msg' the ERROR reply with the 'object' and, if not NULL, the token.
152 * 'object' can be NULL.
153 * Return 0 in case of success. Otherwise, returns -1 and set errno.
155 static inline int afb_wsj1_reply_error_j(struct afb_wsj1_msg *msg, struct json_object *object, const char *token)
157 return afb_wsj1_reply_j(msg, object, token, 1);
161 * Increases by one the count of reference to 'msg'.
162 * Should be called if callbacks stores the message.
164 extern void afb_wsj1_msg_addref(struct afb_wsj1_msg *msg);
167 * Decreases by one the count of reference to 'msg'.
168 * and if it falls to zero releases the used resources
169 * and free the memory.
170 * Should be called if 'afb_wsj1_msg_addref' was called.
172 extern void afb_wsj1_msg_unref(struct afb_wsj1_msg *msg);
175 * Returns 1 if 'msg' is for a CALL
176 * Otherwise returns 0.
178 extern int afb_wsj1_msg_is_call(struct afb_wsj1_msg *msg);
181 * Returns 1 if 'msg' is for a REPLY of any kind
182 * Otherwise returns 0.
184 extern int afb_wsj1_msg_is_reply(struct afb_wsj1_msg *msg);
187 * Returns 1 if 'msg' is for a REPLY OK
188 * Otherwise returns 0.
190 extern int afb_wsj1_msg_is_reply_ok(struct afb_wsj1_msg *msg);
193 * Returns 1 if 'msg' is for a REPLY ERROR
194 * Otherwise returns 0.
196 extern int afb_wsj1_msg_is_reply_error(struct afb_wsj1_msg *msg);
199 * Returns 1 if 'msg' is for an EVENT
200 * Otherwise returns 0.
202 extern int afb_wsj1_msg_is_event(struct afb_wsj1_msg *msg);
205 * Returns the api of the call for 'msg'
206 * Returns NULL if 'msg' is not for a CALL
208 extern const char *afb_wsj1_msg_api(struct afb_wsj1_msg *msg);
211 * Returns the verb call for 'msg'
212 * Returns NULL if 'msg' is not for a CALL
214 extern const char *afb_wsj1_msg_verb(struct afb_wsj1_msg *msg);
217 * Returns the event name for 'msg'
218 * Returns NULL if 'msg' is not for an EVENT
220 extern const char *afb_wsj1_msg_event(struct afb_wsj1_msg *msg);
223 * Returns the token sent with 'msg' or NULL when no token was sent.
225 extern const char *afb_wsj1_msg_token(struct afb_wsj1_msg *msg);
228 * Returns the wsj1 of 'msg'
230 extern struct afb_wsj1 *afb_wsj1_msg_wsj1(struct afb_wsj1_msg *msg);
233 * Returns the string representation of the object received with 'msg'
235 extern const char *afb_wsj1_msg_object_s(struct afb_wsj1_msg *msg);
238 * Returns the object received with 'msg'
240 extern struct json_object *afb_wsj1_msg_object_j(struct afb_wsj1_msg *msg);