From 895e65610c78eb883b70c971c2c29e300ca063d3 Mon Sep 17 00:00:00 2001 From: ronan Date: Thu, 10 Oct 2019 11:44:25 +0200 Subject: Add timer subscription sample - Add dependency on agl-libafb-helpers, very useful on agl binding. - Add subscription button on html interface. - After starting the timer, you can subscribe and so unsubscribe to a timer event. - Update the README.md. Bug-AGL: SPEC-2778 Signed-off-by: ronan Change-Id: I8cc08a191a93972082060d267276df467b053589 Signed-off-by: ronan --- README.md | 40 ++++--- conf.d/cmake/config.cmake | 1 + helloworld-subscribe-event/CMakeLists.txt | 2 +- .../helloworld-event-service-binding.c | 107 +++++++++++++++++ .../helloworld-service-binding.c | 133 --------------------- htdocs/index.html | 16 ++- 6 files changed, 145 insertions(+), 154 deletions(-) create mode 100644 helloworld-subscribe-event/helloworld-event-service-binding.c delete mode 100644 helloworld-subscribe-event/helloworld-service-binding.c diff --git a/README.md b/README.md index 2e6bad9..fb96fec 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,8 @@ A binding example for AGL ## Pre-requisites Please follow [this guide](https://docs.automotivelinux.org/docs/en/master/devguides/reference/2-download-packages.html) -to add the AGL-Master repository to your distribution. In order to load these files into the current shell script, use the following command : +to add the AGL-Master repository to your distribution.\ +In order to load these files into the current shell script, use the following command : * **Debian/Ubuntu** @@ -13,6 +14,8 @@ to add the AGL-Master repository to your distribution. In order to load these f source /etc/profile.d/agl-app-framework-binder.sh ``` +### CMake module + Then install the cmake module using your distribution package manager. * **Debian/Ubuntu** @@ -32,24 +35,27 @@ sudo zypper install agl-cmake-apps-module ```bash sudo dnf install agl-cmake-apps-module ``` + +### Build dependency + Because the helloworld-service binding uses json, the following package has to be installed. * **Debian/Ubuntu** ```bash -sudo apt-get install libjson-c-dev +sudo apt-get install libjson-c-dev agl-libafb-helpers-dev ``` * **openSUSE** ```bash -sudo zypper install libjson-c-dev +sudo zypper install libjson-c-devel agl-libafb-helpers-devel ``` * **Fedora** ```bash -sudo dnf install libjson-c-dev +sudo dnf install libjson-c-devel ``` ## Setup @@ -62,23 +68,21 @@ cd agl-service-helloworld ## Build for AGL ```bash -#setup your build environement +#setup your build environnement . /xdt/sdk/environment-setup-aarch64-agl-linux #build your application ./autobuild/agl/autobuild package ``` -## Build for 'native' Linux distros (Fedora, openSUSE, Debian, Ubuntu, ...) +## Build for 'native' Linux distribution (Fedora, openSUSE, Debian, Ubuntu, ...) ```bash ./autobuild/linux/autobuild package ``` -You can also use binary package from OBS: [opensuse.org/LinuxAutomotive][opensuse.org/LinuxAutomotive] - ## Deploy -### AGL +### Deploy on AGL ```bash export YOUR_BOARD_IP=192.168.1.X @@ -91,7 +95,7 @@ ssh root@${YOUR_BOARD_IP} afm-util start ${APP_NAME}@${APP_VERSION} ## TEST -### AGL +### TEST on AGL ```bash export YOUR_BOARD_IP=192.168.1.X @@ -108,20 +112,23 @@ curl http://${YOUR_BOARD_IP}:${PORT}/api/helloworld/ping?token=x 2>/dev/null | p ### Native Linux -For native build you need to have tools **afb-daemon**. -You can build it by yourself [app-framework-binder][app-framework-binder], or use binary package from OBS: [opensuse.org/LinuxAutomotive][opensuse.org/LinuxAutomotive] +For native build you need to have tools **afb-daemon**.\ +You can build it by yourself [app-framework-binder][app-framework-binder], or use binary package from OBS:\(opensuse.org/LinuxAutomotive)[opensuse.org/LinuxAutomotive] ```bash -export PORT=8000 -afb-daemon --port=${PORT} --ldpaths=/opt/AGL/helloworld-service/lib/ +export PORT=1234 +cd build +afb-daemon --port=${PORT} --ldpaths=package --workdir=. --roothttp=../htdocs --token= --verbose + +xdg-open http://localhost:${PORT}/ +#Command line test curl http://localhost:${PORT}/api/helloworld/ping #For a nice display curl http://localhost:${PORT}/api/helloworld/ping 2>/dev/null | python -m json.tool - ``` -# Activate authentification security +## Activate authentication security To test auth just switch the line: @@ -139,5 +146,4 @@ To test auth just switch the line: And rebuild your application -[opensuse.org/LinuxAutomotive]:https://en.opensuse.org/LinuxAutomotive [app-framework-binder]:https://gerrit.automotivelinux.org/gerrit/#/admin/projects/src/app-framework-binder diff --git a/conf.d/cmake/config.cmake b/conf.d/cmake/config.cmake index 44690c5..3a2c333 100644 --- a/conf.d/cmake/config.cmake +++ b/conf.d/cmake/config.cmake @@ -71,6 +71,7 @@ set (gcc_minimal_version 4.9) set (PKG_REQUIRED_LIST json-c afb-daemon + afb-helpers ) diff --git a/helloworld-subscribe-event/CMakeLists.txt b/helloworld-subscribe-event/CMakeLists.txt index 131e8d4..bb877a6 100644 --- a/helloworld-subscribe-event/CMakeLists.txt +++ b/helloworld-subscribe-event/CMakeLists.txt @@ -17,7 +17,7 @@ # Add target to project dependency list PROJECT_TARGET_ADD(helloworld-subscribe-event) # Define project Targets - ADD_LIBRARY(${TARGET_NAME} MODULE helloworld-service-binding.c) + ADD_LIBRARY(${TARGET_NAME} MODULE helloworld-event-service-binding.c) # Binder exposes a unique public entry point SET_TARGET_PROPERTIES(${TARGET_NAME} PROPERTIES diff --git a/helloworld-subscribe-event/helloworld-event-service-binding.c b/helloworld-subscribe-event/helloworld-event-service-binding.c new file mode 100644 index 0000000..632142a --- /dev/null +++ b/helloworld-subscribe-event/helloworld-event-service-binding.c @@ -0,0 +1,107 @@ +/* + * Copyright (C) 2016-2019 "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 +#include +#include + +#define AFB_BINDING_VERSION 3 +#include + +#include + +static afb_event_t event; +static TimerHandleT *timer; + +static void timerCount() +{ + static int timerCount = 0; + int listeners = 0; + + if(afb_event_is_valid(event)) + listeners = afb_event_push(event, json_object_new_int(timerCount)); + + timerCount++; +} + +static void startTimer(afb_req_t request) +{ + char *timeruid; + timer = (TimerHandleT*)calloc (1, sizeof(TimerHandleT)); + timer->delay = (uint) 1000; + timer->count = -1; // run forever + timer->uid = timeruid; + TimerEvtStart(request->api, timer, timerCount, NULL); + + afb_req_success(request, NULL, "startTimer"); + + AFB_REQ_NOTICE(request, "Invoked at startTimer"); +} + +static void subscribeSample(afb_req_t request) +{ + // Event not valid ? then creating one. + if(! afb_event_is_valid(event)) + event = afb_api_make_event(request->api, "timerCount"); + + if(afb_req_subscribe(request, event) < 0) { + afb_req_fail(request, "Error", "Subscription operation failed"); + return; + } + + afb_req_success(request, NULL, "subscribeSample"); + + AFB_REQ_NOTICE(request, "Invoked at subscribe invocation"); +} + +static void unsubscribeSample(afb_req_t request) +{ + if(! afb_event_is_valid(event)) { + afb_req_fail(request, "Invalid", "Event not valid"); + return; + } + + if(afb_req_unsubscribe(request, event) < 0) { + afb_req_fail(request, "Error", "Unsubscription operation failed"); + return; + } + + afb_req_success(request, NULL, NULL); + AFB_REQ_NOTICE(request, "Invoked at unsubscribe invocation"); +} + +static const afb_verb_t verbs[] = { + /*Without security*/ + {.verb = "startTimer", .session = AFB_SESSION_NONE, .callback = startTimer, .auth = NULL}, + + {.verb = "subscribe", .session = AFB_SESSION_NONE, .callback = subscribeSample, .auth = NULL}, + {.verb = "unsubscribe", .session = AFB_SESSION_NONE, .callback = unsubscribeSample, .auth = NULL}, + {NULL} +}; + +const afb_binding_t afbBindingExport = { + .api = "helloworld-event", + .specification = NULL, + .verbs = verbs, + .preinit = NULL, + .init = NULL, + .onevent = NULL, + .userdata = NULL, + .provide_class = NULL, + .require_class = NULL, + .require_api = NULL, + .noconcurrency = 0 +}; diff --git a/helloworld-subscribe-event/helloworld-service-binding.c b/helloworld-subscribe-event/helloworld-service-binding.c deleted file mode 100644 index ce525e7..0000000 --- a/helloworld-subscribe-event/helloworld-service-binding.c +++ /dev/null @@ -1,133 +0,0 @@ -/* - * Copyright (C) 2016-2018 "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 -#include -#include - -#define AFB_BINDING_VERSION 3 -#include - -static afb_event_t event; - -static void pingSample(afb_req_t request) -{ - static int pingcount = 0; - int listeners = 0; - - if(afb_event_is_valid(event)) - listeners = afb_event_push(event, json_object_new_int(pingcount)); - - afb_req_success_f(request, json_object_new_int(pingcount), "Ping count = %d, Event listeners = %d", pingcount, listeners); - - AFB_API_NOTICE(afbBindingV3root, "Verbosity macro at level notice invoked at ping invocation, count = %d, event listeners = %d", pingcount, listeners); - - pingcount++; -} - -static void subscribeSample(afb_req_t request) -{ - // Event not valid ? then creating one. - if(! afb_event_is_valid(event)) - event = afb_api_make_event(request->api, "PingCount"); - - - if(afb_req_subscribe(request, event) < 0) { - afb_req_fail(request, "Error", "Subscription operation failed"); - return; - } - - afb_req_success(request, NULL, NULL); - - AFB_API_NOTICE(request->api, "Verbosity macro at level notice invoked at subscribe invocation"); -} - -static void unsubscribeSample(afb_req_t request) -{ - if(! afb_event_is_valid(event)) { - afb_req_fail(request, "Invalid", "Event not valid"); - return; - } - - if(afb_req_unsubscribe(request, event) < 0) { - afb_req_fail(request, "Error", "Unsubscription operation failed"); - return; - } - - afb_req_success(request, NULL, NULL); - AFB_API_NOTICE(request->api, "Verbosity macro at level notice invoked at unsubscribe invocation"); -} - -// testArgsSample - return success only if argument is set to {"cezam": "open"} -static void testArgsSample(afb_req_t request) -{ - json_object *tmpJ; - json_object *res = json_object_new_object(); - json_object *queryJ = afb_req_json(request); - - json_bool success = json_object_object_get_ex(queryJ, "cezam", &tmpJ); - if (!success) { - afb_req_fail_f(request, "ERROR", "key cezam not found in '%s'", json_object_get_string(queryJ)); - return; - } - - if (json_object_get_type(tmpJ) != json_type_string) { - afb_req_fail(request, "ERROR", "key cezam not a string"); - return; - } - - if (strncmp(json_object_get_string(tmpJ), "open", 4) == 0) { - json_object_object_add(res, "code", json_object_new_int(123456789)); - afb_req_success(request, res, NULL); - return; - } - - afb_req_fail_f(request, "ERROR", "value of cezam (%s) is not the expected one.", - json_object_get_string(queryJ)); -} - -static const struct afb_auth _afb_auths_monitor[] = { - {.type = afb_auth_Permission, .text = "urn:AGL:permission:monitor:public:set"}, - {.type = afb_auth_Permission, .text = "urn:AGL:permission:monitor:public:get"}, - {.type = afb_auth_Or, .first = &_afb_auths_monitor[1], .next = &_afb_auths_monitor[0]} -}; - -static const afb_verb_t verbs[] = { - /*Without security*/ - {.verb = "ping", .session = AFB_SESSION_NONE, .callback = pingSample, .auth = NULL}, - - /*With security "urn:AGL:permission:monitor:public:get"*/ - /*{ .verb = "ping" , .session = AFB_SESSION_NONE, .callback = pingSample , .auth = &_afb_auths_monitor[1]},*/ - - {.verb = "subscribe", .session = AFB_SESSION_NONE, .callback = subscribeSample, .auth = NULL}, - {.verb = "unsubscribe", .session = AFB_SESSION_NONE, .callback = unsubscribeSample, .auth = NULL}, - {.verb = "testargs", .session = AFB_SESSION_NONE, .callback = testArgsSample, .auth = NULL}, - {NULL} -}; - -const afb_binding_t afbBindingExport = { - .api = "helloworld-event", - .specification = NULL, - .verbs = verbs, - .preinit = NULL, - .init = NULL, - .onevent = NULL, - .userdata = NULL, - .provide_class = NULL, - .require_class = NULL, - .require_api = NULL, - .noconcurrency = 0 -}; diff --git a/htdocs/index.html b/htdocs/index.html index ffefe20..5dac015 100644 --- a/htdocs/index.html +++ b/htdocs/index.html @@ -1,5 +1,7 @@ + + Simple Helloworld binding Test @@ -15,12 +17,14 @@

Simple Helloworld binding Test

- +

- + + @@ -32,6 +36,12 @@

+
+ + + +
- + \ No newline at end of file -- cgit 1.2.3-korg