diff options
59 files changed, 347 insertions, 174 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index cee5a07f..12e4be95 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -123,9 +123,3 @@ ADD_DEFINITIONS(-DPLUGIN_INSTALL_DIR="${plugin_install_dir}") ADD_SUBDIRECTORY(src) ADD_SUBDIRECTORY(plugins) -ADD_EXECUTABLE(afb-daemon $<TARGET_OBJECTS:src>) -INCLUDE_DIRECTORIES(${include_dirs}) -TARGET_LINK_LIBRARIES(afb-daemon ${link_libraries}) - -INSTALL(TARGETS afb-daemon - RUNTIME DESTINATION ${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_BINDIR}) diff --git a/include/afb-evmgr-itf.h b/include/afb-evmgr-itf.h index 2268ec90..90b1e0a9 100644 --- a/include/afb-evmgr-itf.h +++ b/include/afb-evmgr-itf.h @@ -1,5 +1,5 @@ /* - * Copyright 2016 IoT.bzh + * Copyright (C) 2016 "IoT.bzh" * Author: José Bollo <jose.bollo@iot.bzh> * * Licensed under the Apache License, Version 2.0 (the "License"); diff --git a/include/afb-plugin.h b/include/afb-plugin.h index 63617cbe..40b26cfa 100644 --- a/include/afb-plugin.h +++ b/include/afb-plugin.h @@ -1,5 +1,5 @@ /* - * Copyright 2016 IoT.bzh + * Copyright (C) 2016 "IoT.bzh" * Author: José Bollo <jose.bollo@iot.bzh> * * Licensed under the Apache License, Version 2.0 (the "License"); diff --git a/include/afb-req-itf.h b/include/afb-req-itf.h index a975503d..a8980ffc 100644 --- a/include/afb-req-itf.h +++ b/include/afb-req-itf.h @@ -1,5 +1,5 @@ /* - * Copyright 2016 IoT.bzh + * Copyright (C) 2016 "IoT.bzh" * Author: José Bollo <jose.bollo@iot.bzh> * * Licensed under the Apache License, Version 2.0 (the "License"); diff --git a/plugins/afm-main-plugin/afm-main-plugin.c b/plugins/afm-main-plugin/afm-main-plugin.c index 60db5b40..95767307 100644 --- a/plugins/afm-main-plugin/afm-main-plugin.c +++ b/plugins/afm-main-plugin/afm-main-plugin.c @@ -1,5 +1,5 @@ /* - * Copyright (C) 2015 "IoT.bzh" + * Copyright (C) 2015, 2016 "IoT.bzh" * Author "Fulup Ar Foll" * * Licensed under the Apache License, Version 2.0 (the "License"); diff --git a/plugins/afm-main-plugin/utils-jbus.c b/plugins/afm-main-plugin/utils-jbus.c index c38919d5..f339e0e6 100644 --- a/plugins/afm-main-plugin/utils-jbus.c +++ b/plugins/afm-main-plugin/utils-jbus.c @@ -1,5 +1,5 @@ /* - Copyright 2015 IoT.bzh + Copyright (C) 2015, 2016 "IoT.bzh" author: José Bollo <jose.bollo@iot.bzh> diff --git a/plugins/afm-main-plugin/utils-jbus.h b/plugins/afm-main-plugin/utils-jbus.h index ff4c8fab..44b84c18 100644 --- a/plugins/afm-main-plugin/utils-jbus.h +++ b/plugins/afm-main-plugin/utils-jbus.h @@ -1,5 +1,5 @@ /* - Copyright 2015 IoT.bzh + Copyright (C) 2015, 2016 "IoT.bzh" author: José Bollo <jose.bollo@iot.bzh> diff --git a/plugins/audio/audio-alsa.c b/plugins/audio/audio-alsa.c index dde1ba25..28fe3258 100644 --- a/plugins/audio/audio-alsa.c +++ b/plugins/audio/audio-alsa.c @@ -1,5 +1,5 @@ /* - * Copyright (C) 2015 "IoT.bzh" + * Copyright (C) 2015, 2016 "IoT.bzh" * Author "Manuel Bachmann" * * Licensed under the Apache License, Version 2.0 (the "License"); diff --git a/plugins/audio/audio-alsa.h b/plugins/audio/audio-alsa.h index 4a384605..679d4eee 100644 --- a/plugins/audio/audio-alsa.h +++ b/plugins/audio/audio-alsa.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 2015 "IoT.bzh" + * Copyright (C) 2015, 2016 "IoT.bzh" * Author "Manuel Bachmann" * * Licensed under the Apache License, Version 2.0 (the "License"); diff --git a/plugins/audio/audio-api.c b/plugins/audio/audio-api.c index a043dce9..00f81001 100644 --- a/plugins/audio/audio-api.c +++ b/plugins/audio/audio-api.c @@ -1,5 +1,5 @@ /* - * Copyright (C) 2015 "IoT.bzh" + * Copyright (C) 2015, 2016 "IoT.bzh" * Author "Manuel Bachmann" * * Licensed under the Apache License, Version 2.0 (the "License"); diff --git a/plugins/audio/audio-api.h b/plugins/audio/audio-api.h index 9056e63d..f5c77c2e 100644 --- a/plugins/audio/audio-api.h +++ b/plugins/audio/audio-api.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 2015 "IoT.bzh" + * Copyright (C) 2015, 2016 "IoT.bzh" * Author "Manuel Bachmann" * * Licensed under the Apache License, Version 2.0 (the "License"); diff --git a/plugins/radio/radio-api.c b/plugins/radio/radio-api.c index d3d04ae8..edeb9786 100644 --- a/plugins/radio/radio-api.c +++ b/plugins/radio/radio-api.c @@ -1,5 +1,5 @@ /* - * Copyright (C) 2015 "IoT.bzh" + * Copyright (C) 2015, 2016 "IoT.bzh" * Author "Manuel Bachmann" * * Licensed under the Apache License, Version 2.0 (the "License"); diff --git a/plugins/radio/radio-api.h b/plugins/radio/radio-api.h index 162bd5df..9b91ec69 100644 --- a/plugins/radio/radio-api.h +++ b/plugins/radio/radio-api.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 2015 "IoT.bzh" + * Copyright (C) 2015, 2016 "IoT.bzh" * Author "Manuel Bachmann" * * Licensed under the Apache License, Version 2.0 (the "License"); diff --git a/plugins/radio/radio-rtlsdr.c b/plugins/radio/radio-rtlsdr.c index 49efd2b9..626ecbdd 100644 --- a/plugins/radio/radio-rtlsdr.c +++ b/plugins/radio/radio-rtlsdr.c @@ -1,5 +1,5 @@ /* - * Copyright (C) 2015 "IoT.bzh" + * Copyright (C) 2015, 2016 "IoT.bzh" * Author "Manuel Bachmann" * * Licensed under the Apache License, Version 2.0 (the "License"); diff --git a/plugins/radio/radio-rtlsdr.h b/plugins/radio/radio-rtlsdr.h index ba39211e..ad22908d 100644 --- a/plugins/radio/radio-rtlsdr.h +++ b/plugins/radio/radio-rtlsdr.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 2015 "IoT.bzh" + * Copyright (C) 2015, 2016 "IoT.bzh" * Author "Manuel Bachmann" * * Licensed under the Apache License, Version 2.0 (the "License"); diff --git a/plugins/samples/ClientCtx.c b/plugins/samples/ClientCtx.c index cc2c0221..4d8e7205 100644 --- a/plugins/samples/ClientCtx.c +++ b/plugins/samples/ClientCtx.c @@ -1,5 +1,5 @@ /* - * Copyright (C) 2015 "IoT.bzh" + * Copyright (C) 2015, 2016 "IoT.bzh" * Author "Fulup Ar Foll" * * Licensed under the Apache License, Version 2.0 (the "License"); diff --git a/plugins/samples/HelloWorld.c b/plugins/samples/HelloWorld.c index 83a48114..6f9d01d8 100644 --- a/plugins/samples/HelloWorld.c +++ b/plugins/samples/HelloWorld.c @@ -1,5 +1,5 @@ /* - * Copyright (C) 2015 "IoT.bzh" + * Copyright (C) 2015, 2016 "IoT.bzh" * Author "Fulup Ar Foll" * * Licensed under the Apache License, Version 2.0 (the "License"); diff --git a/plugins/samples/SamplePost.c b/plugins/samples/SamplePost.c index 2dc09661..25e73f93 100644 --- a/plugins/samples/SamplePost.c +++ b/plugins/samples/SamplePost.c @@ -1,5 +1,5 @@ /* - * Copyright (C) 2015 "IoT.bzh" + * Copyright (C) 2015, 2016 "IoT.bzh" * Author "Fulup Ar Foll" * * Licensed under the Apache License, Version 2.0 (the "License"); diff --git a/plugins/session/token-api.c b/plugins/session/token-api.c index 14deb12b..80289486 100644 --- a/plugins/session/token-api.c +++ b/plugins/session/token-api.c @@ -1,5 +1,5 @@ /* - * Copyright (C) 2015 "IoT.bzh" + * Copyright (C) 2015, 2016 "IoT.bzh" * Author "Fulup Ar Foll" * * Licensed under the Apache License, Version 2.0 (the "License"); diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 68beea00..67e3abce 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -1,5 +1,5 @@ -ADD_LIBRARY(src OBJECT +ADD_LIBRARY(afb-lib OBJECT afb-api-dbus.c afb-api-so.c afb-apis.c @@ -16,11 +16,21 @@ ADD_LIBRARY(src OBJECT afb-ws-json1.c afb-ws.c afb-wsj1.c - main.c session.c verbose.c websock.c ) + +INCLUDE_DIRECTORIES(${include_dirs}) + +ADD_EXECUTABLE(afb-daemon $<TARGET_OBJECTS:afb-lib> main.c) +INCLUDE_DIRECTORIES(${include_dirs}) +TARGET_LINK_LIBRARIES(afb-daemon ${link_libraries}) + +ADD_EXECUTABLE(afb-client-demo $<TARGET_OBJECTS:afb-lib> afb-client-demo.c) INCLUDE_DIRECTORIES(${include_dirs}) +TARGET_LINK_LIBRARIES(afb-client-demo ${link_libraries}) +INSTALL(TARGETS afb-daemon + RUNTIME DESTINATION ${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_BINDIR}) diff --git a/src/afb-api-dbus.c b/src/afb-api-dbus.c index 5b75bfa0..3a882a56 100644 --- a/src/afb-api-dbus.c +++ b/src/afb-api-dbus.c @@ -1,5 +1,5 @@ /* - * Copyright (C) 2015 "IoT.bzh" + * Copyright (C) 2015, 2016 "IoT.bzh" * Author José Bollo <jose.bollo@iot.bzh> * * Licensed under the Apache License, Version 2.0 (the "License"); diff --git a/src/afb-api-dbus.h b/src/afb-api-dbus.h index 8ea1909b..c8a7bc3f 100644 --- a/src/afb-api-dbus.h +++ b/src/afb-api-dbus.h @@ -1,5 +1,5 @@ /* - * Copyright 2016 IoT.bzh + * Copyright (C) 2016 "IoT.bzh" * Author: José Bollo <jose.bollo@iot.bzh> * * Licensed under the Apache License, Version 2.0 (the "License"); diff --git a/src/afb-api-so.h b/src/afb-api-so.h index fe4d76b0..8ef58a53 100644 --- a/src/afb-api-so.h +++ b/src/afb-api-so.h @@ -1,5 +1,5 @@ /* - * Copyright 2016 IoT.bzh + * Copyright (C) 2016 "IoT.bzh" * Author: José Bollo <jose.bollo@iot.bzh> * * Licensed under the Apache License, Version 2.0 (the "License"); diff --git a/src/afb-apis.h b/src/afb-apis.h index 3e75130e..76969782 100644 --- a/src/afb-apis.h +++ b/src/afb-apis.h @@ -1,5 +1,5 @@ /* - * Copyright 2016 IoT.bzh + * Copyright (C) 2016 "IoT.bzh" * Author: José Bollo <jose.bollo@iot.bzh> * * Licensed under the Apache License, Version 2.0 (the "License"); diff --git a/src/afb-client-demo.c b/src/afb-client-demo.c new file mode 100644 index 00000000..349a0274 --- /dev/null +++ b/src/afb-client-demo.c @@ -0,0 +1,206 @@ +/* + * Copyright (C) 2015, 2016 "IoT.bzh" + * Author "Fulup Ar Foll" + * Author José Bollo <jose.bollo@iot.bzh> + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#define _GNU_SOURCE + +#include <stdlib.h> +#include <stdio.h> +#include <stdint.h> +#include <string.h> +#include <unistd.h> +#include <fcntl.h> +#include <sys/stat.h> +#include <sys/types.h> +#include <errno.h> + +#include <systemd/sd-event.h> + +#include "afb-common.h" +#include "afb-wsj1.h" +#include "afb-ws-client.h" + +static void on_hangup(void *closure, struct afb_wsj1 *wsj1); +static void on_call(void *closure, const char *api, const char *verb, struct afb_wsj1_msg *msg); +static void on_event(void *closure, const char *event, struct afb_wsj1_msg *msg); +static int io_event_callback(sd_event_source *src, int fd, uint32_t revents, void *closure); +static void emit(const char *api, const char *verb, const char *object); + +static struct afb_wsj1_itf itf = { + .on_hangup = on_hangup, + .on_call = on_call, + .on_event = on_event +}; + +static struct afb_wsj1 *wsj1; +static int exonrep; +static int callcount; +static sd_event_source *evsrc; + +static void usage(int status, char *arg0) +{ + char *name = strrchr(arg0, '/'); + name = name ? name + 1 : arg0; + fprintf(status ? stderr : stdin, "usage: %s uri [api verb data]\n", name); + exit(status); +} + +int main(int ac, char **av, char **env) +{ + if (ac != 2 && ac != 5) + usage(1, av[0]); + if (!strcmp(av[1], "-h") || !strcmp(av[1], "--help")) + usage(0, av[0]); + + wsj1 = afb_ws_client_connect_wsj1(av[1], &itf, NULL); + if (wsj1 == NULL) { + fprintf(stderr, "connection to %s failed: %m\n", av[1]); + return 1; + } + + if (ac == 2) { + fcntl(0, F_SETFL, O_NONBLOCK); + sd_event_add_io(afb_common_get_event_loop(), &evsrc, 0, EPOLLIN, io_event_callback, NULL); + } else { + exonrep = 1; + emit(av[2], av[3], av[4]); + } + for(;;) + sd_event_run(afb_common_get_event_loop(), 30000000); + return 0; +} + +static void on_hangup(void *closure, struct afb_wsj1 *wsj1) +{ + printf("ON-HANGUP\n"); + exit(0); +} + +static void on_call(void *closure, const char *api, const char *verb, struct afb_wsj1_msg *msg) +{ + int rc; + printf("ON-CALL %s/%s(%s)\n", api, verb, afb_wsj1_msg_object_s(msg)); + rc = afb_wsj1_reply_error_s(msg, "\"unimplemented\"", NULL); + if (rc < 0) + fprintf(stderr, "replying failed: %m\n"); +} + +static void on_event(void *closure, const char *event, struct afb_wsj1_msg *msg) +{ + printf("ON-EVENT %s(%s)\n", event, afb_wsj1_msg_object_s(msg)); +} + +static void event(const char *event, const char *object) +{ + int rc; + + rc = afb_wsj1_send_event_s(wsj1, event, object); + if (rc < 0) + fprintf(stderr, "sending !%s(%s) failed: %m\n", event, object); +} + +static void on_reply(void *closure, struct afb_wsj1_msg *msg) +{ + printf("ON-REPLY %s: %s\n", (char*)closure, afb_wsj1_msg_object_s(msg)); + free(closure); + callcount--; + if (exonrep && !callcount) + //afb_wsj1_hangup(afb_wsj1_msg_wsj1(msg)); + exit(0); +} + +static void call(const char *api, const char *verb, const char *object) +{ + static int num = 0; + char *key; + int rc; + + rc = asprintf(&key, "%d:%s/%s", ++num, api, verb); + callcount++; + rc = afb_wsj1_call_s(wsj1, api, verb, object, on_reply, key); + if (rc < 0) { + fprintf(stderr, "calling %s/%s(%s) failed: %m\n", api, verb, object); + callcount--; + } +} + +static void emit(const char *api, const char *verb, const char *object) +{ + if (api[0] == '!' && api[1] == 0) + event(verb, object); + else + call(api, verb, object); +} + +static int io_event_callback(sd_event_source *src, int fd, uint32_t revents, void *closure) +{ + static size_t count = 0; + static char line[16384]; + static char sep[] = " \t"; + static char sepnl[] = " \t\n"; + + ssize_t rc; + size_t pos; + + /* read the buffer */ + do { rc = read(0, line + count, sizeof line - count); } while (rc < 0 && errno == EINTR); + if (rc < 0) { + fprintf(stderr, "read error: %m\n"); + exit(1); + } + if (rc == 0) { + if (!callcount) + exit(0); + exonrep = 1; + sd_event_source_unref(evsrc); + } + count += (size_t)rc; + + /* normalise the buffer content */ + /* TODO: handle backspace \x7f */ + + /* process the lines */ + pos = 0; + for(;;) { + size_t i, api[2], verb[2], rest[2]; + i = pos; + while(i < count && strchr(sep, line[i])) i++; + api[0] = i; while(i < count && !strchr(sepnl, line[i])) i++; api[1] = i; + while(i < count && strchr(sep, line[i])) i++; + verb[0] = i; while(i < count && !strchr(sepnl, line[i])) i++; verb[1] = i; + while(i < count && strchr(sep, line[i])) i++; + rest[0] = i; while(i < count && line[i] != '\n') i++; rest[1] = i; + if (i == count) break; + line[i++] = 0; + if (api[0] == api[1] || verb[0] == verb[1] || rest[0] == rest[1]) + fprintf(stderr, "bad line: %s\n", line+pos); + else { + line[api[1]] = line[verb[1]] = 0; + emit(line + api[0], line + verb[0], line + rest[0]); + } + pos = i; + } + count -= pos; + if (count == sizeof line) { + fprintf(stderr, "overflow\n"); + exit(1); + } + if (count) + memmove(line, line + pos, count); + return 1; +} + diff --git a/src/afb-common.c b/src/afb-common.c index e3d48d51..3bd5514e 100644 --- a/src/afb-common.c +++ b/src/afb-common.c @@ -1,5 +1,5 @@ /* - * Copyright (C) 2015 "IoT.bzh" + * Copyright (C) 2015, 2016 "IoT.bzh" * Author José Bollo <jose.bollo@iot.bzh> * * Licensed under the Apache License, Version 2.0 (the "License"); diff --git a/src/afb-common.h b/src/afb-common.h index fe17bc93..588ef700 100644 --- a/src/afb-common.h +++ b/src/afb-common.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 2015 "IoT.bzh" + * Copyright (C) 2015, 2016 "IoT.bzh" * Author José Bollo <jose.bollo@iot.bzh> * * Licensed under the Apache License, Version 2.0 (the "License"); diff --git a/src/afb-config.h b/src/afb-config.h index bd98baa3..c0def7c8 100644 --- a/src/afb-config.h +++ b/src/afb-config.h @@ -1,7 +1,5 @@ /* - * local-def.h -- provide a REST/HTTP interface - * - * Copyright (C) 2015, Fulup Ar Foll + * Copyright (C) 2015, 2016 "IoT.bzh" * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/src/afb-context.c b/src/afb-context.c index 2f391dec..b00224e4 100644 --- a/src/afb-context.c +++ b/src/afb-context.c @@ -1,5 +1,5 @@ /* - * Copyright (C) 2015 "IoT.bzh" + * Copyright (C) 2015, 2016 "IoT.bzh" * Author "Fulup Ar Foll" * * Licensed under the Apache License, Version 2.0 (the "License"); @@ -23,8 +23,7 @@ #include "session.h" #include "afb-context.h" - -void afb_context_init(struct afb_context *context, struct AFB_clientCtx *session, const char *token) +static void init_context(struct afb_context *context, struct AFB_clientCtx *session, const char *token) { assert(session != NULL); @@ -42,6 +41,11 @@ void afb_context_init(struct afb_context *context, struct AFB_clientCtx *session } } +void afb_context_init(struct afb_context *context, struct AFB_clientCtx *session, const char *token) +{ + init_context(context, ctxClientAddRef(session), token); +} + int afb_context_connect(struct afb_context *context, const char *uuid, const char *token) { int created; @@ -50,7 +54,7 @@ int afb_context_connect(struct afb_context *context, const char *uuid, const cha session = ctxClientGetSession (uuid, &created); if (session == NULL) return -1; - afb_context_init(context, session, token); + init_context(context, session, token); if (created) context->created = 1; return 0; diff --git a/src/afb-context.h b/src/afb-context.h index eeb4def3..2f5ecc10 100644 --- a/src/afb-context.h +++ b/src/afb-context.h @@ -1,5 +1,5 @@ /* - * Copyright 2016 IoT.bzh + * Copyright (C) 2016 "IoT.bzh" * Author: José Bollo <jose.bollo@iot.bzh> * * Licensed under the Apache License, Version 2.0 (the "License"); diff --git a/src/afb-hreq.c b/src/afb-hreq.c index 81e630bb..0f2f0ffb 100644 --- a/src/afb-hreq.c +++ b/src/afb-hreq.c @@ -1,5 +1,5 @@ /* - * Copyright 2016 IoT.bzh + * Copyright (C) 2016 "IoT.bzh" * Author: José Bollo <jose.bollo@iot.bzh> * * Licensed under the Apache License, Version 2.0 (the "License"); diff --git a/src/afb-hreq.h b/src/afb-hreq.h index 3d197788..d4041d76 100644 --- a/src/afb-hreq.h +++ b/src/afb-hreq.h @@ -1,5 +1,5 @@ /* - * Copyright 2016 IoT.bzh + * Copyright (C) 2016 "IoT.bzh" * Author: José Bollo <jose.bollo@iot.bzh> * * Licensed under the Apache License, Version 2.0 (the "License"); diff --git a/src/afb-hsrv.c b/src/afb-hsrv.c index 5b0153c2..743315cb 100644 --- a/src/afb-hsrv.c +++ b/src/afb-hsrv.c @@ -1,5 +1,5 @@ /* - * Copyright 2016 IoT.bzh + * Copyright (C) 2016 "IoT.bzh" * Author: José Bollo <jose.bollo@iot.bzh> * * Licensed under the Apache License, Version 2.0 (the "License"); diff --git a/src/afb-hsrv.h b/src/afb-hsrv.h index b328df15..cd9ba019 100644 --- a/src/afb-hsrv.h +++ b/src/afb-hsrv.h @@ -1,5 +1,5 @@ /* - Copyright 2016 IoT.bzh + Copyright (C) 2016 "IoT.bzh" author: José Bollo <jose.bollo@iot.bzh> diff --git a/src/afb-hswitch.c b/src/afb-hswitch.c index f0308455..bdded7fb 100644 --- a/src/afb-hswitch.c +++ b/src/afb-hswitch.c @@ -1,5 +1,5 @@ /* - * Copyright (C) 2015 "IoT.bzh" + * Copyright (C) 2015, 2016 "IoT.bzh" * Author "Fulup Ar Foll" * Author José Bollo <jose.bollo@iot.bzh> * diff --git a/src/afb-hswitch.h b/src/afb-hswitch.h index f70a0bf8..ece5608b 100644 --- a/src/afb-hswitch.h +++ b/src/afb-hswitch.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 2015 "IoT.bzh" + * Copyright (C) 2015, 2016 "IoT.bzh" * Author "Fulup Ar Foll" * Author José Bollo <jose.bollo@iot.bzh> * diff --git a/src/afb-method.c b/src/afb-method.c index 488ba3e3..da3b6271 100644 --- a/src/afb-method.c +++ b/src/afb-method.c @@ -1,5 +1,5 @@ /* - * Copyright 2016 IoT.bzh + * Copyright (C) 2016 "IoT.bzh" * Author: José Bollo <jose.bollo@iot.bzh> * * Licensed under the Apache License, Version 2.0 (the "License"); diff --git a/src/afb-method.h b/src/afb-method.h index 1411d29f..53fc5b4e 100644 --- a/src/afb-method.h +++ b/src/afb-method.h @@ -1,5 +1,5 @@ /* - * Copyright 2016 IoT.bzh + * Copyright (C) 2016 "IoT.bzh" * Author: José Bollo <jose.bollo@iot.bzh> * * Licensed under the Apache License, Version 2.0 (the "License"); diff --git a/src/afb-msg-json.c b/src/afb-msg-json.c index 55b3ed31..164a6e8f 100644 --- a/src/afb-msg-json.c +++ b/src/afb-msg-json.c @@ -1,5 +1,5 @@ /* - * Copyright 2016 IoT.bzh + * Copyright (C) 2016 "IoT.bzh" * Author: José Bollo <jose.bollo@iot.bzh> * * Licensed under the Apache License, Version 2.0 (the "License"); diff --git a/src/afb-msg-json.h b/src/afb-msg-json.h index f1d20be5..a90fbb08 100644 --- a/src/afb-msg-json.h +++ b/src/afb-msg-json.h @@ -1,5 +1,5 @@ /* - * Copyright 2016 IoT.bzh + * Copyright (C) 2016 "IoT.bzh" * Author: José Bollo <jose.bollo@iot.bzh> * * Licensed under the Apache License, Version 2.0 (the "License"); diff --git a/src/afb-sig-handler.c b/src/afb-sig-handler.c index 127bf0c9..5fc5bb5e 100644 --- a/src/afb-sig-handler.c +++ b/src/afb-sig-handler.c @@ -1,5 +1,5 @@ /* - * Copyright (C) 2015 "IoT.bzh" + * Copyright (C) 2015, 2016 "IoT.bzh" * Author "Fulup Ar Foll" * Author José Bollo <jose.bollo@iot.bzh> * diff --git a/src/afb-sig-handler.h b/src/afb-sig-handler.h index 3d0249a2..7ef28c30 100644 --- a/src/afb-sig-handler.h +++ b/src/afb-sig-handler.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 2015 "IoT.bzh" + * Copyright (C) 2015, 2016 "IoT.bzh" * Author "Fulup Ar Foll" * Author José Bollo <jose.bollo@iot.bzh> * diff --git a/src/afb-websock.c b/src/afb-websock.c index f9c9dd01..a4237cfb 100644 --- a/src/afb-websock.c +++ b/src/afb-websock.c @@ -1,5 +1,5 @@ /* - * Copyright 2016 IoT.bzh + * Copyright (C) 2016 "IoT.bzh" * Author: José Bollo <jose.bollo@iot.bzh> * * Licensed under the Apache License, Version 2.0 (the "License"); diff --git a/src/afb-websock.h b/src/afb-websock.h index 646e98f8..7aac0d0d 100644 --- a/src/afb-websock.h +++ b/src/afb-websock.h @@ -1,5 +1,5 @@ /* - * Copyright 2016 IoT.bzh + * Copyright (C) 2016 "IoT.bzh" * Author: José Bollo <jose.bollo@iot.bzh> * * Licensed under the Apache License, Version 2.0 (the "License"); diff --git a/src/afb-ws-client.c b/src/afb-ws-client.c index 6ed3f50f..2569d135 100644 --- a/src/afb-ws-client.c +++ b/src/afb-ws-client.c @@ -1,5 +1,5 @@ /* - * Copyright 2016 IoT.bzh + * Copyright (C) 2016 "IoT.bzh" * Author: José Bollo <jose.bollo@iot.bzh> * * Licensed under the Apache License, Version 2.0 (the "License"); @@ -82,8 +82,12 @@ static char *strjoin(int count, const char **strings, const char *separ) for(count = 0 ; strings[count] != NULL ; count++); /* compute the length of the result */ - length = 0; - if (count != 0) { + if (count == 0) + length = 0; + else { + length = (unsigned)(count - 1) * strlen(separ); + for (idx = 0 ; idx < count ; idx ++) + length += strlen(strings[idx]); } /* allocates the result */ @@ -104,10 +108,11 @@ static char *strjoin(int count, const char **strings, const char *separ) } /* creates the http message for the request */ -static int make_request(char **request, const char *path, const char *key, const char *protocols) +static int make_request(char **request, const char *path, const char *host, const char *key, const char *protocols) { int rc = asprintf(request, - "GET %s HTTP1.1\r\n" + "GET %s HTTP/1.1\r\n" + "Host: %s\r\n" "Upgrade: websocket\r\n" "Connection: Upgrade\r\n" "Sec-WebSocket-Version: 13\r\n" @@ -116,6 +121,7 @@ static int make_request(char **request, const char *path, const char *key, const "Content-Length: 0\r\n" "\r\n" , path + , host , key , protocols ); @@ -128,7 +134,7 @@ static int make_request(char **request, const char *path, const char *key, const } /* create the request and send it to fd, returns the expected accept string */ -static const char *send_request(int fd, const char **protocols, const char *path) +static const char *send_request(int fd, const char **protocols, const char *path, const char *host) { const char *key, *ack; char *protolist, *request; @@ -141,7 +147,7 @@ static const char *send_request(int fd, const char **protocols, const char *path /* create the request */ getkeypair(&key, &ack); - length = make_request(&request, path, key, protolist); + length = make_request(&request, path, host, key, protolist); free(protolist); if (length < 0) return NULL; @@ -196,12 +202,12 @@ static int receive_response(int fd, const char **protocols, const char *ack) if (len != 8 || 0 != strncmp(line, "HTTP/1.1", 8)) goto error; it = line + len; - len = strspn(line, " "); + len = strspn(it, " "); if (len == 0) goto error; it += len; - len = strcspn(line, " "); - if (len != 3 || 0 != strncmp(line, "101", 3)) + len = strcspn(it, " "); + if (len != 3 || 0 != strncmp(it, "101", 3)) goto error; /* reads the rest of the response until empty line */ @@ -254,9 +260,9 @@ error: return result; } -static int negociate(int fd, const char **protocols, const char *path) +static int negociate(int fd, const char **protocols, const char *path, const char *host) { - const char *ack = send_request(fd, protocols, path); + const char *ack = send_request(fd, protocols, path, host); return ack == NULL ? -1 : receive_response(fd, protocols, ack); } @@ -296,8 +302,7 @@ static int parse_uri(const char *uri, char **host, char **service, const char ** /* make the result */ *host = strndup(h, hlen); if (*host != NULL) { - return -1; - *service = plen ? strndup(h, hlen) : strdup("http"); + *service = plen ? strndup(p, plen) : strdup("http"); if (*service != NULL) { *path = uri; return 0; @@ -321,7 +326,7 @@ static const char *proto_json1[2] = { "x-afb-ws-json1", NULL }; struct afb_wsj1 *afb_ws_client_connect_wsj1(const char *uri, struct afb_wsj1_itf *itf, void *closure) { int rc, fd; - char *host, *service; + char *host, *service, xhost[32]; const char *path; struct addrinfo hint, *rai, *iai; struct afb_wsj1 *result; @@ -347,11 +352,17 @@ struct afb_wsj1 *afb_ws_client_connect_wsj1(const char *uri, struct afb_wsj1_itf result = NULL; iai = rai; while (iai != NULL) { + struct sockaddr_in *a = (struct sockaddr_in*)(iai->ai_addr); + unsigned char *ipv4 = (unsigned char*)&(a->sin_addr.s_addr); + unsigned char *port = (unsigned char*)&(a->sin_port); + sprintf(xhost, "%d.%d.%d.%d:%d", + (int)ipv4[0], (int)ipv4[1], (int)ipv4[2], (int)ipv4[3], + (((int)port[0]) << 8)|(int)port[1]); fd = socket(iai->ai_family, iai->ai_socktype, iai->ai_protocol); if (fd >= 0) { rc = connect(fd, iai->ai_addr, iai->ai_addrlen); if (rc == 0) { - rc = negociate(fd, proto_json1, path); + rc = negociate(fd, proto_json1, path, xhost); if (rc == 0) { result = afb_wsj1_create(fd, itf, closure); if (result != NULL) diff --git a/src/afb-ws-client.h b/src/afb-ws-client.h new file mode 100644 index 00000000..125bd6f3 --- /dev/null +++ b/src/afb-ws-client.h @@ -0,0 +1,23 @@ +/* + * Copyright (C) 2016 "IoT.bzh" + * Author: José Bollo <jose.bollo@iot.bzh> + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#pragma once + +struct afb_wsj1; +struct afb_wsj1_itf; + +extern struct afb_wsj1 *afb_ws_client_connect_wsj1(const char *uri, struct afb_wsj1_itf *itf, void *closure); diff --git a/src/afb-ws-json1.c b/src/afb-ws-json1.c index b6397af6..fe7514d9 100644 --- a/src/afb-ws-json1.c +++ b/src/afb-ws-json1.c @@ -1,5 +1,5 @@ /* - * Copyright 2016 IoT.bzh + * Copyright (C) 2016 "IoT.bzh" * Author: José Bollo <jose.bollo@iot.bzh> * * Licensed under the Apache License, Version 2.0 (the "License"); diff --git a/src/afb-ws-json1.h b/src/afb-ws-json1.h index 014b9aa2..964835bd 100644 --- a/src/afb-ws-json1.h +++ b/src/afb-ws-json1.h @@ -1,5 +1,5 @@ /* - * Copyright 2016 IoT.bzh + * Copyright (C) 2016 "IoT.bzh" * Author: José Bollo <jose.bollo@iot.bzh> * * Licensed under the Apache License, Version 2.0 (the "License"); diff --git a/src/afb-ws.c b/src/afb-ws.c index 5e8732db..34796818 100644 --- a/src/afb-ws.c +++ b/src/afb-ws.c @@ -1,5 +1,5 @@ /* - * Copyright 2016 IoT.bzh + * Copyright (C) 2016 "IoT.bzh" * Author: José Bollo <jose.bollo@iot.bzh> * * Licensed under the Apache License, Version 2.0 (the "License"); @@ -272,6 +272,7 @@ int afb_ws_texts(struct afb_ws *ws, ...) } ios[count].iov_base = (void*)s; ios[count].iov_len = strlen(s); + count++; s = va_arg(args, const char *); } va_end(args); diff --git a/src/afb-ws.h b/src/afb-ws.h index 48df9e4f..49deaacd 100644 --- a/src/afb-ws.h +++ b/src/afb-ws.h @@ -1,5 +1,5 @@ /* - * Copyright 2016 IoT.bzh + * Copyright (C) 2016 "IoT.bzh" * Author: José Bollo <jose.bollo@iot.bzh> * * Licensed under the Apache License, Version 2.0 (the "License"); diff --git a/src/afb-wsj1.c b/src/afb-wsj1.c index 0a7dfd80..cf31a57a 100644 --- a/src/afb-wsj1.c +++ b/src/afb-wsj1.c @@ -1,5 +1,5 @@ /* - * Copyright 2016 IoT.bzh + * Copyright (C) 2016 "IoT.bzh" * Author: José Bollo <jose.bollo@iot.bzh> * * Licensed under the Apache License, Version 2.0 (the "License"); @@ -34,8 +34,8 @@ #define RETERR 4 #define EVENT 5 -static void wsj1_on_hangup(struct afb_wsj1 *ws); -static void wsj1_on_text(struct afb_wsj1 *ws, char *text, size_t size); +static void wsj1_on_hangup(struct afb_wsj1 *wsj1); +static void wsj1_on_text(struct afb_wsj1 *wsj1, char *text, size_t size); static struct afb_ws_itf wsj1_itf = { .on_hangup = (void*)wsj1_on_hangup, @@ -127,10 +127,10 @@ void afb_wsj1_unref(struct afb_wsj1 *wsj1) } } -static void wsj1_on_hangup(struct afb_wsj1 *ws) +static void wsj1_on_hangup(struct afb_wsj1 *wsj1) { - if (ws->itf->on_hangup != NULL) - ws->itf->on_hangup(ws->closure); + if (wsj1->itf->on_hangup != NULL) + wsj1->itf->on_hangup(wsj1->closure, wsj1); } @@ -302,7 +302,8 @@ static void wsj1_on_text(struct afb_wsj1 *wsj1, char *text, size_t size) afb_wsj1_addref(wsj1); msg->wsj1 = wsj1; msg->next = wsj1->messages; - msg->next->previous = msg; + if (msg->next != NULL) + msg->next->previous = msg; wsj1->messages = msg; /* incoke the handler */ @@ -418,90 +419,11 @@ const char *afb_wsj1_msg_token(struct afb_wsj1_msg *msg) return msg->token; } - - - - - - - - - -#if 0 - - - - - -static void wsj1_emit(struct afb_wsj1 *wsj1, int code, const char *id, size_t idlen, struct json_object *data, const char *token) +struct afb_wsj1 *afb_wsj1_msg_wsj1(struct afb_wsj1_msg *msg) { - json_object *msg; - const char *txt; - - /* pack the message */ - msg = json_object_new_array(); - json_object_array_add(msg, json_object_new_int(code)); - json_object_array_add(msg, json_object_new_string_len(id, (int)idlen)); - json_object_array_add(msg, data); - if (token) - json_object_array_add(msg, json_object_new_string(token)); - - /* emits the reply */ - txt = json_object_to_json_string(msg); - afb_ws_text(wsj1->ws, txt, strlen(txt)); - json_object_put(msg); + return msg->wsj1; } -static void wsj1_msg_reply(struct afb_wsj1_msg *msg, int retcode, const char *status, const char *info, json_object *resp) -{ - const char *token = afb_context_sent_token(&msg->context); - wsj1_emit(msg->wsj1, retcode, msg->id, msg->idlen, afb_msg_json_reply(status, info, resp, token, NULL), token); -} - -static void wsj1_msg_fail(struct afb_wsj1_msg *msg, const char *status, const char *info) -{ - wsj1_msg_reply(msg, RETERR, status, info, NULL); -} - -static void wsj1_msg_success(struct afb_wsj1_msg *msg, json_object *obj, const char *info) -{ - wsj1_msg_reply(msg, RETOK, "success", info, obj); -} - -static const char *wsj1_msg_raw(struct afb_wsj1_msg *msg, size_t *size) -{ - *size = msg->objlen; - return msg->obj; -} - -static void wsj1_msg_send(struct afb_wsj1_msg *msg, const char *buffer, size_t size) -{ - afb_ws_text(msg->wsj1->ws, buffer, size); -} - -static void wsj1_send_event(struct afb_wsj1 *wsj1, const char *event, struct json_object *object) -{ - wsj1_emit(wsj1, EVENT, event, strlen(event), afb_msg_json_event(event, object), NULL); -} - - - - - - - - - - - - - - - - -#endif - - static int wsj1_send_isot(struct afb_wsj1 *wsj1, int i1, const char *s1, const char *o1, const char *t1) { char code[2] = { (char)('0' + i1), 0 }; diff --git a/src/afb-wsj1.h b/src/afb-wsj1.h index d96367f9..08bf67f7 100644 --- a/src/afb-wsj1.h +++ b/src/afb-wsj1.h @@ -1,5 +1,5 @@ /* - * Copyright 2016 IoT.bzh + * Copyright (C) 2016 "IoT.bzh" * Author: José Bollo <jose.bollo@iot.bzh> * * Licensed under the Apache License, Version 2.0 (the "License"); @@ -23,7 +23,7 @@ struct afb_wsj1_msg; struct json_object; struct afb_wsj1_itf { - void (*on_hangup)(void *closure); + void (*on_hangup)(void *closure, struct afb_wsj1 *wsj1); void (*on_call)(void *closure, const char *api, const char *verb, struct afb_wsj1_msg *msg); void (*on_event)(void *closure, const char *event, struct afb_wsj1_msg *msg); }; @@ -58,6 +58,7 @@ extern const char *afb_wsj1_msg_api(struct afb_wsj1_msg *msg); extern const char *afb_wsj1_msg_verb(struct afb_wsj1_msg *msg); extern const char *afb_wsj1_msg_event(struct afb_wsj1_msg *msg); extern const char *afb_wsj1_msg_token(struct afb_wsj1_msg *msg); +extern struct afb_wsj1 *afb_wsj1_msg_wsj1(struct afb_wsj1_msg *msg); extern const char *afb_wsj1_msg_object_s(struct afb_wsj1_msg *msg); extern struct json_object *afb_wsj1_msg_object_j(struct afb_wsj1_msg *msg); @@ -1,5 +1,5 @@ /* - * Copyright (C) 2015 "IoT.bzh" + * Copyright (C) 2015, 2016 "IoT.bzh" * Author "Fulup Ar Foll" * Author José Bollo <jose.bollo@iot.bzh> * @@ -131,7 +131,7 @@ static void printVersion (FILE *file) fprintf(file, "\n----------------------------------------- \n"); fprintf(file, " AFB [Application Framework Binder] version=%s |\n", AFB_VERSION); fprintf(file, " \n"); - fprintf(file, " Copyright(C) 2016 /IoT.bzh [fulup -at- iot.bzh]\n"); + fprintf(file, " Copyright (C) 2015, 2016 \"IoT.bzh\" [fulup -at- iot.bzh]\n"); fprintf(file, " AFB comes with ABSOLUTELY NO WARRANTY.\n"); fprintf(file, " Licence Apache 2\n\n"); exit (0); diff --git a/src/session.c b/src/session.c index 22e50cfc..320eab9b 100644 --- a/src/session.c +++ b/src/session.c @@ -1,5 +1,5 @@ /* - * Copyright (C) 2015 "IoT.bzh" + * Copyright (C) 2015, 2016 "IoT.bzh" * Author "Fulup Ar Foll" * * Licensed under the Apache License, Version 2.0 (the "License"); @@ -197,11 +197,10 @@ static void ctxStoreCleanUp (time_t now) // Loop on Sessions Table and remove anything that is older than timeout for (idx=0; idx < sessions.max; idx++) { - ctx = ctxClientAddRef(sessions.store[idx]); + ctx = sessions.store[idx]; if (ctx != NULL && ctxStoreTooOld(ctx, now)) { ctxClientClose (ctx); } - ctxClientUnref(ctx); } } @@ -291,6 +290,10 @@ void ctxClientClose (struct AFB_clientCtx *clientCtx) ctxUuidFreeCB (clientCtx); while(clientCtx->listeners != NULL) ctxClientEventListenerRemove(clientCtx, clientCtx->listeners->listener); + if (clientCtx->refcount == 0) { + ctxStoreDel (clientCtx); + free(clientCtx); + } } } diff --git a/src/session.h b/src/session.h index a4c05222..bb37a652 100644 --- a/src/session.h +++ b/src/session.h @@ -1,5 +1,5 @@ /* - * Copyright 2016 IoT.bzh + * Copyright (C) 2016 "IoT.bzh" * Author: José Bollo <jose.bollo@iot.bzh> * * Licensed under the Apache License, Version 2.0 (the "License"); diff --git a/src/verbose.c b/src/verbose.c index 82de1779..892aa9a3 100644 --- a/src/verbose.c +++ b/src/verbose.c @@ -1,5 +1,5 @@ /* - Copyright 2016 IoT.bzh + Copyright (C) 2016 "IoT.bzh" author: José Bollo <jose.bollo@iot.bzh> diff --git a/src/verbose.h b/src/verbose.h index ccd8eaf6..5c9f8864 100644 --- a/src/verbose.h +++ b/src/verbose.h @@ -1,5 +1,5 @@ /* - Copyright 2016 IoT.bzh + Copyright (C) 2016 "IoT.bzh" author: José Bollo <jose.bollo@iot.bzh> diff --git a/src/websock.c b/src/websock.c index 9e79559b..1b886c1b 100644 --- a/src/websock.c +++ b/src/websock.c @@ -1,5 +1,5 @@ /* - * Copyright 2016 iot.bzh + * Copyright (C) 2016 "IoT.bzh" * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/src/websock.h b/src/websock.h index 82e251c1..e024182e 100644 --- a/src/websock.h +++ b/src/websock.h @@ -1,5 +1,5 @@ /* - * Copyright 2016 iot.bzh + * Copyright (C) 2016 "IoT.bzh" * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. |