diff options
Diffstat (limited to 'bindings')
-rw-r--r-- | bindings/CMakeLists.txt | 3 | ||||
-rw-r--r-- | bindings/tutorial/CMakeLists.txt | 32 | ||||
-rw-r--r-- | bindings/tutorial/export.map | 1 | ||||
-rw-r--r-- | bindings/tutorial/tuto-1.c | 19 | ||||
-rw-r--r-- | bindings/tutorial/tuto-2.c | 100 |
5 files changed, 154 insertions, 1 deletions
diff --git a/bindings/CMakeLists.txt b/bindings/CMakeLists.txt index 490e29aa..1a1e9016 100644 --- a/bindings/CMakeLists.txt +++ b/bindings/CMakeLists.txt @@ -16,5 +16,6 @@ # limitations under the License. ########################################################################### -ADD_SUBDIRECTORY(samples) ADD_SUBDIRECTORY(intrinsics) +ADD_SUBDIRECTORY(samples) +ADD_SUBDIRECTORY(tutorial) diff --git a/bindings/tutorial/CMakeLists.txt b/bindings/tutorial/CMakeLists.txt new file mode 100644 index 00000000..844e2dfb --- /dev/null +++ b/bindings/tutorial/CMakeLists.txt @@ -0,0 +1,32 @@ +########################################################################### +# Copyright 2015, 2016, 2017 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. +########################################################################### + + +#INCLUDE_DIRECTORIES(${include_dirs}) + +MACRO(tuto num) + ADD_LIBRARY(tuto-${num} MODULE tuto-${num}.c) + SET_TARGET_PROPERTIES(tuto-${num} PROPERTIES + PREFIX "" + LINK_FLAGS "-Wl,--version-script=${CMAKE_CURRENT_SOURCE_DIR}/export.map" + ) + TARGET_LINK_LIBRARIES(tuto-${num} ${link_libraries}) +ENDMACRO(tuto) + +tuto(1) +tuto(2) diff --git a/bindings/tutorial/export.map b/bindings/tutorial/export.map new file mode 100644 index 00000000..ee2f4133 --- /dev/null +++ b/bindings/tutorial/export.map @@ -0,0 +1 @@ +{ global: afbBindingV*; local: *; }; diff --git a/bindings/tutorial/tuto-1.c b/bindings/tutorial/tuto-1.c new file mode 100644 index 00000000..433a4ebb --- /dev/null +++ b/bindings/tutorial/tuto-1.c @@ -0,0 +1,19 @@ +#define AFB_BINDING_VERSION 2 +#include <afb/afb-binding.h> + +void hello(afb_req req) +{ + AFB_REQ_DEBUG(req, "hello world"); + afb_req_success(req, NULL, "hello world"); +} + +const afb_verb_v2 verbs[] = { + { .verb="hello", .callback=hello }, + { .verb=NULL } +}; + +const afb_binding_v2 afbBindingV2 = { + .api = "tuto-1", + .verbs = verbs +}; + diff --git a/bindings/tutorial/tuto-2.c b/bindings/tutorial/tuto-2.c new file mode 100644 index 00000000..dc2d55ab --- /dev/null +++ b/bindings/tutorial/tuto-2.c @@ -0,0 +1,100 @@ +#include <string.h> +#include <json-c/json.h> + +#define AFB_BINDING_VERSION 2 +#include <afb/afb-binding.h> + +afb_event event_login, event_logout; + +void login(afb_req req) +{ + json_object *args, *user, *passwd; + char *usr; + + args = afb_req_json(req); + if (!json_object_object_get_ex(args, "user", &user) + || !json_object_object_get_ex(args, "password", &passwd)) { + AFB_REQ_ERROR(req, "login, bad request: %s", json_object_get_string(args)); + afb_req_fail(req, "bad-request", NULL); + } else if (afb_req_context_get(req)) { + AFB_REQ_ERROR(req, "login, bad state, logout first"); + afb_req_fail(req, "bad-state", NULL); + } else if (strcmp(json_object_get_string(passwd), "please")) { + AFB_REQ_ERROR(req, "login, unauthorized: %s", json_object_get_string(args)); + afb_req_fail(req, "unauthorized", NULL); + } else { + usr = strdup(json_object_get_string(user)); + AFB_REQ_NOTICE(req, "login user: %s", usr); + afb_req_session_set_LOA(req, 1); + afb_req_context_set(req, usr, free); + afb_req_success(req, NULL, NULL); + afb_event_push(event_login, json_object_new_string(usr)); + } +} + +void action(afb_req req) +{ + json_object *args, *val; + char *usr; + + args = afb_req_json(req); + usr = afb_req_context_get(req); + AFB_REQ_NOTICE(req, "action for user %s: %s", usr, json_object_get_string(args)); + if (json_object_object_get_ex(args, "subscribe", &val)) { + if (json_object_get_boolean(val)) { + AFB_REQ_NOTICE(req, "user %s subscribes to events", usr); + afb_req_subscribe(req, event_login); + afb_req_subscribe(req, event_logout); + } else { + AFB_REQ_NOTICE(req, "user %s unsubscribes to events", usr); + afb_req_unsubscribe(req, event_login); + afb_req_unsubscribe(req, event_logout); + } + } + afb_req_success(req, json_object_get(args), NULL); +} + +void logout(afb_req req) +{ + char *usr; + + usr = afb_req_context_get(req); + AFB_REQ_NOTICE(req, "login user %s out", usr); + afb_event_push(event_logout, json_object_new_string(usr)); + afb_req_session_set_LOA(req, 0); + afb_req_context_clear(req); + afb_req_success(req, NULL, NULL); +} + +int preinit() +{ + AFB_NOTICE("preinit"); + return 0; +} + +int init() +{ + AFB_NOTICE("init"); + event_login = afb_daemon_make_event("login"); + event_logout = afb_daemon_make_event("logout"); + if (afb_event_is_valid(event_login) && afb_event_is_valid(event_logout)) + return 0; + AFB_ERROR("Can't create events"); + return -1; +} + +const afb_verb_v2 verbs[] = { + { .verb="login", .callback=login }, + { .verb="action", .callback=action, .session=AFB_SESSION_LOA_1 }, + { .verb="logout", .callback=logout, .session=AFB_SESSION_LOA_1 }, + { .verb=NULL } +}; + +const afb_binding_v2 afbBindingV2 = { + .api = "tuto-2", + .specification = NULL, + .verbs = verbs, + .preinit = preinit, + .init = init, + .noconcurrency = 0 +};
\ No newline at end of file |