2 * Copyright (C) 2015-2018 "IoT.bzh"
3 * Author "Fulup Ar Foll"
4 * Author José Bollo <jose.bollo@iot.bzh>
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
28 #include <sys/types.h>
31 #include <systemd/sd-event.h>
32 #include <json-c/json.h>
35 #include "afb-ws-client.h"
36 #include "afb-proto-ws.h"
38 /* declaration of functions */
39 static void on_wsj1_hangup(void *closure, struct afb_wsj1 *wsj1);
40 static void on_wsj1_call(void *closure, const char *api, const char *verb, struct afb_wsj1_msg *msg);
41 static void on_wsj1_event(void *closure, const char *event, struct afb_wsj1_msg *msg);
43 static void on_pws_hangup(void *closure);
44 static void on_pws_reply_success(void *closure, void *request, struct json_object *result, const char *info);
45 static void on_pws_reply_fail(void *closure, void *request, const char *status, const char *info);
46 static void on_pws_event_create(void *closure, const char *event_name, int event_id);
47 static void on_pws_event_remove(void *closure, const char *event_name, int event_id);
48 static void on_pws_event_subscribe(void *closure, void *request, const char *event_name, int event_id);
49 static void on_pws_event_unsubscribe(void *closure, void *request, const char *event_name, int event_id);
50 static void on_pws_event_push(void *closure, const char *event_name, int event_id, struct json_object *data);
51 static void on_pws_event_broadcast(void *closure, const char *event_name, struct json_object *data);
52 static void on_pws_subcall(void *closure, struct afb_proto_ws_subcall *subcall, void *request, const char *api, const char *verb, struct json_object *args);
54 static int io_event_callback(sd_event_source *src, int fd, uint32_t revents, void *closure);
56 static void wsj1_emit(const char *api, const char *verb, const char *object);
57 static void pws_call(const char *verb, const char *object);
59 /* the callback interface for wsj1 */
60 static struct afb_wsj1_itf wsj1_itf = {
61 .on_hangup = on_wsj1_hangup,
62 .on_call = on_wsj1_call,
63 .on_event = on_wsj1_event
66 /* the callback interface for pws */
67 static struct afb_proto_ws_client_itf pws_itf = {
68 .on_reply_success = on_pws_reply_success,
69 .on_reply_fail = on_pws_reply_fail,
70 .on_event_create = on_pws_event_create,
71 .on_event_remove = on_pws_event_remove,
72 .on_event_subscribe = on_pws_event_subscribe,
73 .on_event_unsubscribe = on_pws_event_unsubscribe,
74 .on_event_push = on_pws_event_push,
75 .on_event_broadcast = on_pws_event_broadcast,
76 .on_subcall = on_pws_subcall,
79 /* global variables */
80 static struct afb_wsj1 *wsj1;
81 static struct afb_proto_ws *pws;
88 static sd_event_source *evsrc;
89 static char *sessionid = "afb-client-demo";
91 /* print usage of the program */
92 static void usage(int status, char *arg0)
94 char *name = strrchr(arg0, '/');
95 name = name ? name + 1 : arg0;
96 fprintf(status ? stderr : stdout, "usage: %s [-H [-r]] [-b] uri [api verb [data]]\n", name);
97 fprintf(status ? stderr : stdout, " %s -d [-H [-r]] [-b] uri [verb [data]]\n", name);
102 int main(int ac, char **av, char **env)
108 /* get the program name */
112 while (ac > 1 && av[1][0] == '-') {
113 if (av[1][1] == '-') {
116 if (!strcmp(av[1], "--human")) /* request for human output */
119 else if (!strcmp(av[1], "--raw")) /* request for raw output */
122 else if (!strcmp(av[1], "--direct")) /* request for direct api */
125 else if (!strcmp(av[1], "--break")) /* request to break connection */
128 /* emit usage and exit */
130 usage(!!strcmp(av[1], "--help"), a0);
132 /* short option(s) */
133 for (rc = 1 ; av[1][rc] ; rc++)
135 case 'H': human = 1; break;
136 case 'r': raw = 1; break;
137 case 'd': direct = 1; break;
138 case 'b': breakcon = 1; break;
139 default: usage(av[1][rc] != 'h', a0);
146 /* check the argument count */
147 if (ac != 2 && ac != 4 && ac != 5)
150 /* set raw by default */
154 /* get the default event loop */
155 rc = sd_event_default(&loop);
157 fprintf(stderr, "connection to default event loop failed: %s\n", strerror(-rc));
161 /* connect the websocket wsj1 to the uri given by the first argument */
163 pws = afb_ws_client_connect_api(loop, av[1], &pws_itf, NULL);
165 fprintf(stderr, "connection to %s failed: %m\n", av[1]);
168 afb_proto_ws_on_hangup(pws, on_pws_hangup);
170 wsj1 = afb_ws_client_connect_wsj1(loop, av[1], &wsj1_itf, NULL);
172 fprintf(stderr, "connection to %s failed: %m\n", av[1]);
177 /* test the behaviour */
179 /* get requests from stdin */
180 fcntl(0, F_SETFL, O_NONBLOCK);
181 sd_event_add_io(loop, &evsrc, 0, EPOLLIN, io_event_callback, NULL);
183 /* the request is defined by the arguments */
186 pws_call(av[2], av[3]);
188 wsj1_emit(av[2], av[3], av[4]);
193 sd_event_run(loop, 30000000);
197 /* decrement the count of calls */
198 static void dec_callcount()
201 if (exonrep && !callcount)
205 /* called when wsj1 hangsup */
206 static void on_wsj1_hangup(void *closure, struct afb_wsj1 *wsj1)
208 printf("ON-HANGUP\n");
213 /* called when wsj1 receives a method invocation */
214 static void on_wsj1_call(void *closure, const char *api, const char *verb, struct afb_wsj1_msg *msg)
218 printf("ON-CALL %s/%s(%s)\n", api, verb, afb_wsj1_msg_object_s(msg));
220 printf("ON-CALL %s/%s:\n%s\n", api, verb,
221 json_object_to_json_string_ext(afb_wsj1_msg_object_j(msg),
222 JSON_C_TO_STRING_PRETTY));
224 rc = afb_wsj1_reply_error_s(msg, "\"unimplemented\"", NULL);
226 fprintf(stderr, "replying failed: %m\n");
229 /* called when wsj1 receives an event */
230 static void on_wsj1_event(void *closure, const char *event, struct afb_wsj1_msg *msg)
233 printf("ON-EVENT %s(%s)\n", event, afb_wsj1_msg_object_s(msg));
235 printf("ON-EVENT %s:\n%s\n", event,
236 json_object_to_json_string_ext(afb_wsj1_msg_object_j(msg),
237 JSON_C_TO_STRING_PRETTY));
241 /* called when wsj1 receives a reply */
242 static void on_wsj1_reply(void *closure, struct afb_wsj1_msg *msg)
245 printf("ON-REPLY %s: %s\n", (char*)closure, afb_wsj1_msg_object_s(msg));
247 printf("ON-REPLY %s: %s\n%s\n", (char*)closure,
248 afb_wsj1_msg_is_reply_ok(msg) ? "OK" : "ERROR",
249 json_object_to_json_string_ext(afb_wsj1_msg_object_j(msg),
250 JSON_C_TO_STRING_PRETTY));
257 static void wsj1_call(const char *api, const char *verb, const char *object)
263 /* allocates an id for the request */
264 rc = asprintf(&key, "%d:%s/%s", ++num, api, verb);
266 /* send the request */
268 rc = afb_wsj1_call_s(wsj1, api, verb, object, on_wsj1_reply, key);
270 fprintf(stderr, "calling %s/%s(%s) failed: %m\n", api, verb, object);
276 static void wsj1_event(const char *event, const char *object)
280 rc = afb_wsj1_send_event_s(wsj1, event, object);
282 fprintf(stderr, "sending !%s(%s) failed: %m\n", event, object);
285 /* emits either a call (when api!='!') or an event */
286 static void wsj1_emit(const char *api, const char *verb, const char *object)
288 if (object == NULL || object[0] == 0)
290 if (api[0] == '!' && api[1] == 0)
291 wsj1_event(verb, object);
293 wsj1_call(api, verb, object);
298 /* called when something happens on stdin */
299 static int io_event_callback(sd_event_source *src, int fd, uint32_t revents, void *closure)
301 static size_t count = 0;
302 static char line[16384];
303 static char sep[] = " \t";
304 static char sepnl[] = " \t\n";
309 /* read the buffer */
310 do { rc = read(0, line + count, sizeof line - count); } while (rc < 0 && errno == EINTR);
312 fprintf(stderr, "read error: %m\n");
319 sd_event_source_unref(evsrc);
323 /* normalise the buffer content */
324 /* TODO: handle backspace \x7f ? */
326 /* process the lines */
329 size_t i, api[2], verb[2], rest[2];
331 while(i < count && strchr(sep, line[i])) i++;
332 api[0] = i; while(i < count && !strchr(sepnl, line[i])) i++; api[1] = i;
333 while(i < count && strchr(sep, line[i])) i++;
338 verb[0] = i; while(i < count && !strchr(sepnl, line[i])) i++; verb[1] = i;
339 while(i < count && strchr(sep, line[i])) i++;
341 rest[0] = i; while(i < count && line[i] != '\n') i++; rest[1] = i;
342 if (i == count) break;
344 if (api[0] == api[1]) {
346 } else if (line[api[0]] == '#') {
348 } else if (verb[0] == verb[1]) {
349 fprintf(stderr, "verb missing, bad line: %s\n", line+pos);
351 line[api[1]] = line[verb[1]] = 0;
353 pws_call(line + verb[0], line + rest[0]);
355 wsj1_emit(line + api[0], line + verb[0], line + rest[0]);
360 if (count == sizeof line) {
361 fprintf(stderr, "overflow\n");
365 memmove(line, line + pos, count);
369 static void on_pws_reply_success(void *closure, void *request, struct json_object *result, const char *info)
372 printf("ON-REPLY-SUCCESS %s: [%s] %s\n", (char*)request, info?:"", json_object_to_json_string(result));
374 printf("ON-REPLY-SUCCESS %s: %s\n%s\n", (char*)request, info?:"", json_object_to_json_string_ext(result, JSON_C_TO_STRING_PRETTY));
380 static void on_pws_reply_fail(void *closure, void *request, const char *status, const char *info)
382 printf("ON-REPLY-FAIL %s: %s [%s]\n", (char*)request, status?:"?", info?:"");
388 static void on_pws_event_create(void *closure, const char *event_name, int event_id)
390 printf("ON-EVENT-CREATE: [%d:%s]\n", event_id, event_name);
394 static void on_pws_event_remove(void *closure, const char *event_name, int event_id)
396 printf("ON-EVENT-REMOVE: [%d:%s]\n", event_id, event_name);
400 static void on_pws_event_subscribe(void *closure, void *request, const char *event_name, int event_id)
402 printf("ON-EVENT-SUBSCRIBE %s: [%d:%s]\n", (char*)request, event_id, event_name);
406 static void on_pws_event_unsubscribe(void *closure, void *request, const char *event_name, int event_id)
408 printf("ON-EVENT-UNSUBSCRIBE %s: [%d:%s]\n", (char*)request, event_id, event_name);
412 static void on_pws_event_push(void *closure, const char *event_name, int event_id, struct json_object *data)
415 printf("ON-EVENT-PUSH: [%d:%s] %s\n", event_id, event_name, json_object_to_json_string(data));
417 printf("ON-EVENT-PUSH: [%d:%s]\n%s\n", event_id, event_name, json_object_to_json_string_ext(data, JSON_C_TO_STRING_PRETTY));
421 static void on_pws_event_broadcast(void *closure, const char *event_name, struct json_object *data)
424 printf("ON-EVENT-BROADCAST: [%s] %s\n", event_name, json_object_to_json_string(data));
426 printf("ON-EVENT-BROADCAST: [%s]\n%s\n", event_name, json_object_to_json_string_ext(data, JSON_C_TO_STRING_PRETTY));
430 static void on_pws_subcall(void *closure, struct afb_proto_ws_subcall *subcall, void *request, const char *api, const char *verb, struct json_object *args)
433 printf("ON-SUBCALL %s: %s/%s %s\n", (char*)request, api, verb, json_object_to_json_string(args));
435 printf("ON-SUBCALL %s: %s/%s\n%s\n", (char*)request, api, verb, json_object_to_json_string_ext(args, JSON_C_TO_STRING_PRETTY));
436 afb_proto_ws_subcall_reply(subcall, 1, NULL);
441 static void pws_call(const char *verb, const char *object)
446 struct json_object *o;
448 /* allocates an id for the request */
449 rc = asprintf(&key, "%d:%s", ++num, verb);
451 /* send the request */
453 if (object == NULL || object[0] == 0 || !strcmp(object, "null"))
456 o = json_tokener_parse(object);
458 o = json_object_new_string(object);
460 rc = afb_proto_ws_client_call(pws, verb, o, sessionid, key);
463 fprintf(stderr, "calling %s(%s) failed: %m\n", verb, object?:"");
470 /* called when pws hangsup */
471 static void on_pws_hangup(void *closure)
473 printf("ON-HANGUP\n");