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(void *closure, void *request, struct json_object *result, const char *error, const char *info);
45 static void on_pws_event_create(void *closure, const char *event_name, int event_id);
46 static void on_pws_event_remove(void *closure, const char *event_name, int event_id);
47 static void on_pws_event_subscribe(void *closure, void *request, const char *event_name, int event_id);
48 static void on_pws_event_unsubscribe(void *closure, void *request, const char *event_name, int event_id);
49 static void on_pws_event_push(void *closure, const char *event_name, int event_id, struct json_object *data);
50 static void on_pws_event_broadcast(void *closure, const char *event_name, struct json_object *data);
52 static int io_event_callback(sd_event_source *src, int fd, uint32_t revents, void *closure);
54 static void wsj1_emit(const char *api, const char *verb, const char *object);
55 static void pws_call(const char *verb, const char *object);
57 /* the callback interface for wsj1 */
58 static struct afb_wsj1_itf wsj1_itf = {
59 .on_hangup = on_wsj1_hangup,
60 .on_call = on_wsj1_call,
61 .on_event = on_wsj1_event
64 /* the callback interface for pws */
65 static struct afb_proto_ws_client_itf pws_itf = {
66 .on_reply = on_pws_reply,
67 .on_event_create = on_pws_event_create,
68 .on_event_remove = on_pws_event_remove,
69 .on_event_subscribe = on_pws_event_subscribe,
70 .on_event_unsubscribe = on_pws_event_unsubscribe,
71 .on_event_push = on_pws_event_push,
72 .on_event_broadcast = on_pws_event_broadcast,
75 /* global variables */
76 static struct afb_wsj1 *wsj1;
77 static struct afb_proto_ws *pws;
86 static sd_event_source *evsrc;
87 static char *sessionid = "afb-client-demo";
89 /* print usage of the program */
90 static void usage(int status, char *arg0)
92 char *name = strrchr(arg0, '/');
93 name = name ? name + 1 : arg0;
94 fprintf(status ? stderr : stdout, "usage: %s [-H [-r]] [-b] [-e] uri [api verb [data]]\n", name);
95 fprintf(status ? stderr : stdout, " %s -d [-H [-r]] [-b] [-e] uri [verb [data]]\n", name);
96 fprintf(status ? stderr : stdout, "\n"
98 " --break, -b Break connection just after event/call has been emitted.\n"
99 " --direct, -d Direct api\n"
100 " --echo, -e Echo inputs\n"
101 " --help, -h Display this help\n"
102 " --human, -H Display human readable JSON\n"
103 " --raw, -r Raw output (default)\n"
104 " --keep-running, -k Keep running until disconnect, even if input closed\n"
106 " %s --human 'localhost:1234/api?token=HELLO&uuid=magic' hello ping\n"
114 int main(int ac, char **av, char **env)
120 /* get the program name */
124 while (ac > 1 && av[1][0] == '-') {
125 if (av[1][1] == '-') {
128 if (!strcmp(av[1], "--human")) /* request for human output */
131 else if (!strcmp(av[1], "--raw")) /* request for raw output */
134 else if (!strcmp(av[1], "--direct")) /* request for direct api */
137 else if (!strcmp(av[1], "--break")) /* request to break connection */
140 else if (!strcmp(av[1], "--keep-running")) /* request to break connection */
143 else if (!strcmp(av[1], "--echo")) /* request to echo inputs */
146 /* emit usage and exit */
148 usage(!!strcmp(av[1], "--help"), a0);
150 /* short option(s) */
151 for (rc = 1 ; av[1][rc] ; rc++)
153 case 'H': human = 1; break;
154 case 'r': raw = 1; break;
155 case 'd': direct = 1; break;
156 case 'b': breakcon = 1; break;
157 case 'k': keeprun = 1; break;
158 case 'e': echo = 1; break;
159 default: usage(av[1][rc] != 'h', a0);
166 /* check the argument count */
167 if (ac != 2 && ac != 4 && ac != 5)
170 /* set raw by default */
174 /* get the default event loop */
175 rc = sd_event_default(&loop);
177 fprintf(stderr, "connection to default event loop failed: %s\n", strerror(-rc));
181 /* connect the websocket wsj1 to the uri given by the first argument */
183 pws = afb_ws_client_connect_api(loop, av[1], &pws_itf, NULL);
185 fprintf(stderr, "connection to %s failed: %m\n", av[1]);
188 afb_proto_ws_on_hangup(pws, on_pws_hangup);
190 wsj1 = afb_ws_client_connect_wsj1(loop, av[1], &wsj1_itf, NULL);
192 fprintf(stderr, "connection to %s failed: %m\n", av[1]);
197 /* test the behaviour */
199 /* get requests from stdin */
200 fcntl(0, F_SETFL, O_NONBLOCK);
201 sd_event_add_io(loop, &evsrc, 0, EPOLLIN, io_event_callback, NULL);
203 /* the request is defined by the arguments */
206 pws_call(av[2], av[3]);
208 wsj1_emit(av[2], av[3], av[4]);
213 sd_event_run(loop, 30000000);
217 /* decrement the count of calls */
218 static void dec_callcount()
221 if (exonrep && !callcount)
225 /* called when wsj1 hangsup */
226 static void on_wsj1_hangup(void *closure, struct afb_wsj1 *wsj1)
228 printf("ON-HANGUP\n");
233 /* called when wsj1 receives a method invocation */
234 static void on_wsj1_call(void *closure, const char *api, const char *verb, struct afb_wsj1_msg *msg)
238 printf("%s\n", afb_wsj1_msg_object_s(msg));
240 printf("ON-CALL %s/%s:\n%s\n", api, verb,
241 json_object_to_json_string_ext(afb_wsj1_msg_object_j(msg),
242 JSON_C_TO_STRING_PRETTY));
244 rc = afb_wsj1_reply_error_s(msg, "\"unimplemented\"", NULL);
246 fprintf(stderr, "replying failed: %m\n");
249 /* called when wsj1 receives an event */
250 static void on_wsj1_event(void *closure, const char *event, struct afb_wsj1_msg *msg)
253 printf("%s\n", afb_wsj1_msg_object_s(msg));
255 printf("ON-EVENT %s:\n%s\n", event,
256 json_object_to_json_string_ext(afb_wsj1_msg_object_j(msg),
257 JSON_C_TO_STRING_PRETTY));
261 /* called when wsj1 receives a reply */
262 static void on_wsj1_reply(void *closure, struct afb_wsj1_msg *msg)
265 printf("%s\n", afb_wsj1_msg_object_s(msg));
267 printf("ON-REPLY %s: %s\n%s\n", (char*)closure,
268 afb_wsj1_msg_is_reply_ok(msg) ? "OK" : "ERROR",
269 json_object_to_json_string_ext(afb_wsj1_msg_object_j(msg),
270 JSON_C_TO_STRING_PRETTY));
277 static void wsj1_call(const char *api, const char *verb, const char *object)
283 /* allocates an id for the request */
284 rc = asprintf(&key, "%d:%s/%s", ++num, api, verb);
286 /* echo the command if asked */
288 printf("SEND-CALL %s/%s %s\n", api, verb, object?:"null");
290 /* send the request */
292 rc = afb_wsj1_call_s(wsj1, api, verb, object, on_wsj1_reply, key);
294 fprintf(stderr, "calling %s/%s(%s) failed: %m\n", api, verb, object);
300 static void wsj1_event(const char *event, const char *object)
304 /* echo the command if asked */
306 printf("SEND-EVENT: %s %s\n", event, object?:"null");
308 rc = afb_wsj1_send_event_s(wsj1, event, object);
310 fprintf(stderr, "sending !%s(%s) failed: %m\n", event, object);
313 /* emits either a call (when api!='!') or an event */
314 static void wsj1_emit(const char *api, const char *verb, const char *object)
316 if (object == NULL || object[0] == 0)
319 if (api[0] == '!' && api[1] == 0)
320 wsj1_event(verb, object);
322 wsj1_call(api, verb, object);
327 /* called when something happens on stdin */
328 static int io_event_callback(sd_event_source *src, int fd, uint32_t revents, void *closure)
330 static size_t count = 0;
331 static char line[16384];
332 static char sep[] = " \t";
333 static char sepnl[] = " \t\n";
338 /* read the buffer */
339 do { rc = read(0, line + count, sizeof line - count); } while (rc < 0 && errno == EINTR);
341 fprintf(stderr, "read error: %m\n");
350 sd_event_source_unref(evsrc);
354 /* normalise the buffer content */
355 /* TODO: handle backspace \x7f ? */
357 /* process the lines */
360 size_t i, api[2], verb[2], rest[2];
362 while(i < count && strchr(sep, line[i])) i++;
363 api[0] = i; while(i < count && !strchr(sepnl, line[i])) i++; api[1] = i;
364 while(i < count && strchr(sep, line[i])) i++;
369 verb[0] = i; while(i < count && !strchr(sepnl, line[i])) i++; verb[1] = i;
370 while(i < count && strchr(sep, line[i])) i++;
372 rest[0] = i; while(i < count && line[i] != '\n') i++; rest[1] = i;
373 if (i == count) break;
375 if (api[0] == api[1]) {
377 } else if (line[api[0]] == '#') {
379 } else if (verb[0] == verb[1]) {
380 fprintf(stderr, "verb missing, bad line: %s\n", line+pos);
382 line[api[1]] = line[verb[1]] = 0;
384 pws_call(line + verb[0], line + rest[0]);
386 wsj1_emit(line + api[0], line + verb[0], line + rest[0]);
391 if (count == sizeof line) {
392 fprintf(stderr, "overflow\n");
396 memmove(line, line + pos, count);
400 static void on_pws_reply(void *closure, void *request, struct json_object *result, const char *error, const char *info)
402 error = error ?: "success";
404 /* TODO: transitionnal: fake the structured response */
405 struct json_object *x = json_object_new_object(), *y = json_object_new_object();
406 json_object_object_add(x, "jtype", json_object_new_string("afb-reply"));
407 json_object_object_add(x, "request", y);
408 json_object_object_add(y, "status", json_object_new_string(error));
410 json_object_object_add(y, "info", json_object_new_string(info));
412 json_object_object_add(x, "response", json_object_get(result));
414 printf("%s\n", json_object_to_json_string(x));
418 printf("ON-REPLY %s: %s %s\n%s\n", (char*)request, error, info ?: "", json_object_to_json_string_ext(result, JSON_C_TO_STRING_PRETTY));
424 static void on_pws_event_create(void *closure, const char *event_name, int event_id)
426 printf("ON-EVENT-CREATE: [%d:%s]\n", event_id, event_name);
430 static void on_pws_event_remove(void *closure, const char *event_name, int event_id)
432 printf("ON-EVENT-REMOVE: [%d:%s]\n", event_id, event_name);
436 static void on_pws_event_subscribe(void *closure, void *request, const char *event_name, int event_id)
438 printf("ON-EVENT-SUBSCRIBE %s: [%d:%s]\n", (char*)request, event_id, event_name);
442 static void on_pws_event_unsubscribe(void *closure, void *request, const char *event_name, int event_id)
444 printf("ON-EVENT-UNSUBSCRIBE %s: [%d:%s]\n", (char*)request, event_id, event_name);
448 static void on_pws_event_push(void *closure, const char *event_name, int event_id, struct json_object *data)
451 printf("ON-EVENT-PUSH: [%d:%s]\n%s\n", event_id, event_name, json_object_to_json_string_ext(data, 0));
453 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));
457 static void on_pws_event_broadcast(void *closure, const char *event_name, struct json_object *data)
460 printf("ON-EVENT-BROADCAST: [%s]\n%s\n", event_name, json_object_to_json_string_ext(data, 0));
462 printf("ON-EVENT-BROADCAST: [%s]\n%s\n", event_name, json_object_to_json_string_ext(data, JSON_C_TO_STRING_PRETTY));
467 static void pws_call(const char *verb, const char *object)
472 struct json_object *o;
474 /* allocates an id for the request */
475 rc = asprintf(&key, "%d:%s", ++num, verb);
477 /* echo the command if asked */
479 printf("SEND-CALL: %s %s\n", verb, object?:"null");
481 /* send the request */
483 if (object == NULL || object[0] == 0 || !strcmp(object, "null"))
486 o = json_tokener_parse(object);
488 o = json_object_new_string(object);
490 rc = afb_proto_ws_client_call(pws, verb, o, sessionid, key, NULL);
493 fprintf(stderr, "calling %s(%s) failed: %m\n", verb, object?:"");
500 /* called when pws hangsup */
501 static void on_pws_hangup(void *closure)
503 printf("ON-HANGUP\n");