2 * Copyright (C) 2015, 2016 "IoT.bzh"
3 * Author "Fulup Ar Foll" <fulup@iot.bzh>
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>
33 #include <afb/afb-wsj1.h>
34 #include <afb/afb-ws-client.h>
36 /* declaration of functions */
37 static void on_hangup(void *closure, struct afb_wsj1 *wsj1);
38 static void on_call(void *closure, const char *api, const char *verb, struct afb_wsj1_msg *msg);
39 static void on_event(void *closure, const char *event, struct afb_wsj1_msg *msg);
40 static int io_event_callback(sd_event_source *src, int fd, uint32_t revents, void *closure);
41 static void emit(const char *api, const char *verb, const char *object);
43 /* the callback interface for wsj1 */
44 static struct afb_wsj1_itf itf = {
45 .on_hangup = on_hangup,
50 /* global variables */
51 static struct afb_wsj1 *wsj1;
54 static sd_event_source *evsrc;
56 /* print usage of the program */
57 static void usage(int status, char *arg0)
59 char *name = strrchr(arg0, '/');
60 name = name ? name + 1 : arg0;
61 fprintf(status ? stderr : stdout, "usage: %s uri [api verb [data]]\n", name);
66 int main(int ac, char **av, char **env)
71 /* check the argument count */
72 if (ac != 2 && ac != 4 && ac != 5)
75 /* emit error and exit if requested */
76 if (!strcmp(av[1], "-h") || !strcmp(av[1], "--help"))
79 /* get the default event loop */
80 rc = sd_event_default(&loop);
82 fprintf(stderr, "connection to default event loop failed: %s\n", strerror(-rc));
86 /* connect the websocket wsj1 to the uri given by the first argument */
87 wsj1 = afb_ws_client_connect_wsj1(loop,av[1], &itf, NULL);
89 fprintf(stderr, "connection to %s failed: %m\n", av[1]);
93 /* test the behaviour */
95 /* get requests from stdin */
96 fcntl(0, F_SETFL, O_NONBLOCK);
97 sd_event_add_io(loop, &evsrc, 0, EPOLLIN, io_event_callback, NULL);
99 /* the request is defined by the arguments */
101 emit(av[2], av[3], av[4]);
106 sd_event_run(loop, 30000000);
110 /* called when wsj1 hangsup */
111 static void on_hangup(void *closure, struct afb_wsj1 *wsj1)
113 printf("ON-HANGUP\n");
117 /* called when wsj1 receives a method invocation */
118 static void on_call(void *closure, const char *api, const char *verb, struct afb_wsj1_msg *msg)
121 printf("ON-CALL %s/%s(%s)\n", api, verb, afb_wsj1_msg_object_s(msg));
122 rc = afb_wsj1_reply_error_s(msg, "\"unimplemented\"", NULL);
124 fprintf(stderr, "replying failed: %m\n");
127 /* called when wsj1 receives an event */
128 static void on_event(void *closure, const char *event, struct afb_wsj1_msg *msg)
130 printf("ON-EVENT %s(%s)\n", event, afb_wsj1_msg_object_s(msg));
133 /* called when wsj1 receives a reply */
134 static void on_reply(void *closure, struct afb_wsj1_msg *msg)
136 printf("ON-REPLY %s: %s\n", (char*)closure, afb_wsj1_msg_object_s(msg));
139 if (exonrep && !callcount)
140 //afb_wsj1_hangup(afb_wsj1_msg_wsj1(msg));
145 static void call(const char *api, const char *verb, const char *object)
151 /* allocates an id for the request */
152 rc = asprintf(&key, "%d:%s/%s", ++num, api, verb);
154 /* send the request */
156 rc = afb_wsj1_call_s(wsj1, api, verb, object, on_reply, key);
158 fprintf(stderr, "calling %s/%s(%s) failed: %m\n", api, verb, object);
164 static void event(const char *event, const char *object)
168 rc = afb_wsj1_send_event_s(wsj1, event, object);
170 fprintf(stderr, "sending !%s(%s) failed: %m\n", event, object);
173 /* emits either a call (when api!='!') or an event */
174 static void emit(const char *api, const char *verb, const char *object)
176 if (object == NULL || object[0] == 0)
178 if (api[0] == '!' && api[1] == 0)
181 call(api, verb, object);
184 /* called when something happens on stdin */
185 static int io_event_callback(sd_event_source *src, int fd, uint32_t revents, void *closure)
187 static size_t count = 0;
188 static char line[16384];
189 static char sep[] = " \t";
190 static char sepnl[] = " \t\n";
195 /* read the buffer */
196 do { rc = read(0, line + count, sizeof line - count); } while (rc < 0 && errno == EINTR);
198 fprintf(stderr, "read error: %m\n");
205 sd_event_source_unref(evsrc);
209 /* normalise the buffer content */
210 /* TODO: handle backspace \x7f ? */
212 /* process the lines */
215 size_t i, api[2], verb[2], rest[2];
217 while(i < count && strchr(sep, line[i])) i++;
218 api[0] = i; while(i < count && !strchr(sepnl, line[i])) i++; api[1] = i;
219 while(i < count && strchr(sep, line[i])) i++;
220 verb[0] = i; while(i < count && !strchr(sepnl, line[i])) i++; verb[1] = i;
221 while(i < count && strchr(sep, line[i])) i++;
222 rest[0] = i; while(i < count && line[i] != '\n') i++; rest[1] = i;
223 if (i == count) break;
225 if (api[0] == api[1] || verb[0] == verb[1])
226 fprintf(stderr, "bad line: %s\n", line+pos);
228 line[api[1]] = line[verb[1]] = 0;
229 emit(line + api[0], line + verb[0], line + rest[0]);
234 if (count == sizeof line) {
235 fprintf(stderr, "overflow\n");
239 memmove(line, line + pos, count);