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;
85 static sd_event_source *evsrc;
86 static char *sessionid = "afb-client-demo";
88 /* print usage of the program */
89 static void usage(int status, char *arg0)
91 char *name = strrchr(arg0, '/');
92 name = name ? name + 1 : arg0;
93 fprintf(status ? stderr : stdout, "usage: %s [-H [-r]] [-b] [-e] uri [api verb [data]]\n", name);
94 fprintf(status ? stderr : stdout, " %s -d [-H [-r]] [-b] [-e] uri [verb [data]]\n", name);
95 fprintf(status ? stderr : stdout, "\n"
97 " --break, -b Break connection just after event/call has been emitted.\n"
98 " --direct, -d Direct api\n"
99 " --echo, -e Echo inputs\n"
100 " --help, -h Display this help\n"
101 " --human, -H Display human readable JSON\n"
102 " --raw, -r Raw output (default)\n"
104 " %s --human 'localhost:1234/api?token=HELLO&uuid=magic' hello ping\n"
112 int main(int ac, char **av, char **env)
118 /* get the program name */
122 while (ac > 1 && av[1][0] == '-') {
123 if (av[1][1] == '-') {
126 if (!strcmp(av[1], "--human")) /* request for human output */
129 else if (!strcmp(av[1], "--raw")) /* request for raw output */
132 else if (!strcmp(av[1], "--direct")) /* request for direct api */
135 else if (!strcmp(av[1], "--break")) /* request to break connection */
138 else if (!strcmp(av[1], "--echo")) /* request to echo inputs */
141 /* emit usage and exit */
143 usage(!!strcmp(av[1], "--help"), a0);
145 /* short option(s) */
146 for (rc = 1 ; av[1][rc] ; rc++)
148 case 'H': human = 1; break;
149 case 'r': raw = 1; break;
150 case 'd': direct = 1; break;
151 case 'b': breakcon = 1; break;
152 case 'e': echo = 1; break;
153 default: usage(av[1][rc] != 'h', a0);
160 /* check the argument count */
161 if (ac != 2 && ac != 4 && ac != 5)
164 /* set raw by default */
168 /* get the default event loop */
169 rc = sd_event_default(&loop);
171 fprintf(stderr, "connection to default event loop failed: %s\n", strerror(-rc));
175 /* connect the websocket wsj1 to the uri given by the first argument */
177 pws = afb_ws_client_connect_api(loop, av[1], &pws_itf, NULL);
179 fprintf(stderr, "connection to %s failed: %m\n", av[1]);
182 afb_proto_ws_on_hangup(pws, on_pws_hangup);
184 wsj1 = afb_ws_client_connect_wsj1(loop, av[1], &wsj1_itf, NULL);
186 fprintf(stderr, "connection to %s failed: %m\n", av[1]);
191 /* test the behaviour */
193 /* get requests from stdin */
194 fcntl(0, F_SETFL, O_NONBLOCK);
195 sd_event_add_io(loop, &evsrc, 0, EPOLLIN, io_event_callback, NULL);
197 /* the request is defined by the arguments */
200 pws_call(av[2], av[3]);
202 wsj1_emit(av[2], av[3], av[4]);
207 sd_event_run(loop, 30000000);
211 /* decrement the count of calls */
212 static void dec_callcount()
215 if (exonrep && !callcount)
219 /* called when wsj1 hangsup */
220 static void on_wsj1_hangup(void *closure, struct afb_wsj1 *wsj1)
222 printf("ON-HANGUP\n");
227 /* called when wsj1 receives a method invocation */
228 static void on_wsj1_call(void *closure, const char *api, const char *verb, struct afb_wsj1_msg *msg)
232 printf("%s\n", afb_wsj1_msg_object_s(msg));
234 printf("ON-CALL %s/%s:\n%s\n", api, verb,
235 json_object_to_json_string_ext(afb_wsj1_msg_object_j(msg),
236 JSON_C_TO_STRING_PRETTY));
238 rc = afb_wsj1_reply_error_s(msg, "\"unimplemented\"", NULL);
240 fprintf(stderr, "replying failed: %m\n");
243 /* called when wsj1 receives an event */
244 static void on_wsj1_event(void *closure, const char *event, struct afb_wsj1_msg *msg)
247 printf("%s\n", afb_wsj1_msg_object_s(msg));
249 printf("ON-EVENT %s:\n%s\n", event,
250 json_object_to_json_string_ext(afb_wsj1_msg_object_j(msg),
251 JSON_C_TO_STRING_PRETTY));
255 /* called when wsj1 receives a reply */
256 static void on_wsj1_reply(void *closure, struct afb_wsj1_msg *msg)
259 printf("%s\n", afb_wsj1_msg_object_s(msg));
261 printf("ON-REPLY %s: %s\n%s\n", (char*)closure,
262 afb_wsj1_msg_is_reply_ok(msg) ? "OK" : "ERROR",
263 json_object_to_json_string_ext(afb_wsj1_msg_object_j(msg),
264 JSON_C_TO_STRING_PRETTY));
271 static void wsj1_call(const char *api, const char *verb, const char *object)
277 /* allocates an id for the request */
278 rc = asprintf(&key, "%d:%s/%s", ++num, api, verb);
280 /* echo the command if asked */
282 printf("SEND-CALL %s/%s %s\n", api, verb, object?:"null");
284 /* send the request */
286 rc = afb_wsj1_call_s(wsj1, api, verb, object, on_wsj1_reply, key);
288 fprintf(stderr, "calling %s/%s(%s) failed: %m\n", api, verb, object);
294 static void wsj1_event(const char *event, const char *object)
298 /* echo the command if asked */
300 printf("SEND-EVENT: %s %s\n", event, object?:"null");
302 rc = afb_wsj1_send_event_s(wsj1, event, object);
304 fprintf(stderr, "sending !%s(%s) failed: %m\n", event, object);
307 /* emits either a call (when api!='!') or an event */
308 static void wsj1_emit(const char *api, const char *verb, const char *object)
310 if (object == NULL || object[0] == 0)
313 if (api[0] == '!' && api[1] == 0)
314 wsj1_event(verb, object);
316 wsj1_call(api, verb, object);
321 /* called when something happens on stdin */
322 static int io_event_callback(sd_event_source *src, int fd, uint32_t revents, void *closure)
324 static size_t count = 0;
325 static char line[16384];
326 static char sep[] = " \t";
327 static char sepnl[] = " \t\n";
332 /* read the buffer */
333 do { rc = read(0, line + count, sizeof line - count); } while (rc < 0 && errno == EINTR);
335 fprintf(stderr, "read error: %m\n");
342 sd_event_source_unref(evsrc);
346 /* normalise the buffer content */
347 /* TODO: handle backspace \x7f ? */
349 /* process the lines */
352 size_t i, api[2], verb[2], rest[2];
354 while(i < count && strchr(sep, line[i])) i++;
355 api[0] = i; while(i < count && !strchr(sepnl, line[i])) i++; api[1] = i;
356 while(i < count && strchr(sep, line[i])) i++;
361 verb[0] = i; while(i < count && !strchr(sepnl, line[i])) i++; verb[1] = i;
362 while(i < count && strchr(sep, line[i])) i++;
364 rest[0] = i; while(i < count && line[i] != '\n') i++; rest[1] = i;
365 if (i == count) break;
367 if (api[0] == api[1]) {
369 } else if (line[api[0]] == '#') {
371 } else if (verb[0] == verb[1]) {
372 fprintf(stderr, "verb missing, bad line: %s\n", line+pos);
374 line[api[1]] = line[verb[1]] = 0;
376 pws_call(line + verb[0], line + rest[0]);
378 wsj1_emit(line + api[0], line + verb[0], line + rest[0]);
383 if (count == sizeof line) {
384 fprintf(stderr, "overflow\n");
388 memmove(line, line + pos, count);
392 static void on_pws_reply(void *closure, void *request, struct json_object *result, const char *error, const char *info)
394 error = error ?: "success";
396 /* TODO: transitionnal: fake the structured response */
397 struct json_object *x = json_object_new_object(), *y = json_object_new_object();
398 json_object_object_add(x, "jtype", json_object_new_string("afb-reply"));
399 json_object_object_add(x, "request", y);
400 json_object_object_add(y, "status", json_object_new_string(error));
402 json_object_object_add(y, "info", json_object_new_string(info));
404 json_object_object_add(x, "response", json_object_get(result));
406 printf("%s\n", json_object_to_json_string(x));
410 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));
416 static void on_pws_event_create(void *closure, const char *event_name, int event_id)
418 printf("ON-EVENT-CREATE: [%d:%s]\n", event_id, event_name);
422 static void on_pws_event_remove(void *closure, const char *event_name, int event_id)
424 printf("ON-EVENT-REMOVE: [%d:%s]\n", event_id, event_name);
428 static void on_pws_event_subscribe(void *closure, void *request, const char *event_name, int event_id)
430 printf("ON-EVENT-SUBSCRIBE %s: [%d:%s]\n", (char*)request, event_id, event_name);
434 static void on_pws_event_unsubscribe(void *closure, void *request, const char *event_name, int event_id)
436 printf("ON-EVENT-UNSUBSCRIBE %s: [%d:%s]\n", (char*)request, event_id, event_name);
440 static void on_pws_event_push(void *closure, const char *event_name, int event_id, struct json_object *data)
443 printf("ON-EVENT-PUSH: [%d:%s]\n%s\n", event_id, event_name, json_object_to_json_string_ext(data, 0));
445 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));
449 static void on_pws_event_broadcast(void *closure, const char *event_name, struct json_object *data)
452 printf("ON-EVENT-BROADCAST: [%s]\n%s\n", event_name, json_object_to_json_string_ext(data, 0));
454 printf("ON-EVENT-BROADCAST: [%s]\n%s\n", event_name, json_object_to_json_string_ext(data, JSON_C_TO_STRING_PRETTY));
459 static void pws_call(const char *verb, const char *object)
464 struct json_object *o;
466 /* allocates an id for the request */
467 rc = asprintf(&key, "%d:%s", ++num, verb);
469 /* echo the command if asked */
471 printf("SEND-CALL: %s %s\n", verb, object?:"null");
473 /* send the request */
475 if (object == NULL || object[0] == 0 || !strcmp(object, "null"))
478 o = json_tokener_parse(object);
480 o = json_object_new_string(object);
482 rc = afb_proto_ws_client_call(pws, verb, o, sessionid, key, NULL);
485 fprintf(stderr, "calling %s(%s) failed: %m\n", verb, object?:"");
492 /* called when pws hangsup */
493 static void on_pws_hangup(void *closure)
495 printf("ON-HANGUP\n");