shared library: pass afb-helpers to shared library sandbox/benierc/shared-library
authorClément Bénier <clement.benier@iot.bzh>
Tue, 28 Aug 2018 12:29:55 +0000 (14:29 +0200)
committerClément Bénier <clement.benier@iot.bzh>
Fri, 5 Oct 2018 12:40:04 +0000 (14:40 +0200)
- add afb-timer in helpers: rename ctl-timer from ctl-utilities
into afb-timer

Change-Id: Ic88105d140edabcc96b091fb23f215db11ccb8c6
Signed-off-by: Clément Bénier <clement.benier@iot.bzh>
CMakeLists.txt
afb-helpers.pc.in [new file with mode: 0644]
afb-timer.c [new file with mode: 0644]
afb-timer.h [new file with mode: 0644]
filescan-utils.c
filescan-utils.h

index 21c9ad0..a4a11df 100644 (file)
 # limitations under the License.
 ###########################################################################
 
-# This is a CMakeLists.txt file meant to be included as submodule into an AGL
-# app using app-templates subdmodules
+CMAKE_MINIMUM_REQUIRED(VERSION 3.3)
+set(TARGET_NAME "afb-helpers")
+set(PROJECT_PRETTY_NAME "Afb Helpers")
+set(PROJECT_DESCRIPTION "afb helpers")
+set(PROJECT_URL "https://github.com/iotbzh/4a-softmixer")
+set(PROJECT_URL "https://gerrit.automotivelinux.org:29418/apps/app-afb-helpers-submodule.git")
+set(PROJECT_AUTHOR "Ar Foll, Fulup")
+set(PROJECT_AUTHOR_MAIL "fulup@iot.bzh")
+set(PROJECT_LICENSE "APL2.0")
+set(PROJECT_LANGUAGES "C")
 
 # Add target to project dependency list
-PROJECT_TARGET_ADD(afb-helpers)
 set(CMAKE_AUTOMOC ON)
 set(CMAKE_AUTORCC ON)
 
-       set(AFB_HELPERS_SRCS wrap-json.c filescan-utils.c escape.c)
+if(NOT CMAKE_INSTALL_PREFIX)
+    set(CMAKE_INSTALL_PREFIX "/usr/local")
+endif()
+set(CMAKE_INSTALL_LIBDIR ${CMAKE_INSTALL_PREFIX}/lib)
+set(CMAKE_INSTALL_INCLUDEDIR ${CMAKE_INSTALL_PREFIX}/include)
+add_definitions(-DAFB_BINDING_VERSION=3)
 
-       option(AFB_HELPERS_QTWSCLIENT "Enable the Qt's websocket client to Application Framework Binders" OFF)
-       find_package(CURL)
+INCLUDE(FindPkgConfig)
+set (PKG_REQUIRED_LIST
+    afb-daemon
+    )
 
-       if (AFB_HELPERS_QTWSCLIENT)
-               message(STATUS "Qt's WebSocket AFB Client: Enabled!")
-               set(AFB_HELPERS_SRCS ${AFB_HELPERS_SRCS} qafbwebsocketclient.cpp qafbwebsocketclient.h)
-               find_package(Qt5WebSockets REQUIRED)
-       else()
-               message(STATUS "Qt's WebSocket AFB Client: Disabled!")
-       endif()
+# Loop on required package and add options
+foreach (PKG_CONFIG ${PKG_REQUIRED_LIST})
+    string(REGEX REPLACE "[<>]?=.*$" "" XPREFIX ${PKG_CONFIG})
+    PKG_CHECK_MODULES(${XPREFIX} REQUIRED ${PKG_CONFIG})
 
-       if (CURL_FOUND)
-               message(STATUS "CURL wrapping helpers: Enabled!")
-               set(AFB_HELPERS_SRCS ${AFB_HELPERS_SRCS} curl-wrap.c)
-       else()
-               message(STATUS "CURL wrapping helpers: Disabled!")
-       endif()
+    INCLUDE_DIRECTORIES(${${XPREFIX}_INCLUDE_DIRS})
+    list(APPEND link_libraries ${${XPREFIX}_LDFLAGS})
+    add_compile_options (${${XPREFIX}_CFLAGS})
+endforeach(PKG_CONFIG)
 
-       # Define targets
-       ADD_LIBRARY(${TARGET_NAME} STATIC ${AFB_HELPERS_SRCS})
+set(AFB_HELPERS_SRCS wrap-json.c filescan-utils.c escape.c afb-timer.c)
 
-       if (AFB_HELPERS_QTWSCLIENT)
-               target_link_libraries(${TARGET_NAME} Qt5::WebSockets)
-               #qt5_use_modules(${TARGET_NAME} WebSockets)
-       endif()
+option(AFB_HELPERS_QTWSCLIENT "Enable the Qt's websocket client to Application Framework Binders" ON)
+find_package(CURL REQUIRED)
 
-       if (CURL_FOUND)
-               target_link_libraries(${TARGET_NAME} curl)
-       endif()
+set(AFB_HELPERS_HEADERS afb-helpers-utils.h curl-wrap.h escape.h
+    filescan-utils.h qafbwebsocketclient.h wrap-json.h afb-timer.h
+    )
 
-       # Library properties
-       SET_TARGET_PROPERTIES(${TARGET_NAME} PROPERTIES
-               OUTPUT_NAME ${TARGET_NAME}
-       )
+set(AFB_HELPERS_SRCS ${AFB_HELPERS_SRCS} qafbwebsocketclient.cpp qafbwebsocketclient.h)
+find_package(Qt5WebSockets REQUIRED)
+include_directories(${Qt5WebSockets_INCLUDE_DIRS})
 
-       # Define target includes
-       TARGET_INCLUDE_DIRECTORIES(${TARGET_NAME}
-               PUBLIC  ${CMAKE_CURRENT_SOURCE_DIR}
-       )
+set(AFB_HELPERS_SRCS ${AFB_HELPERS_SRCS} curl-wrap.c)
 
+# Define targets
+ADD_LIBRARY(${TARGET_NAME} SHARED ${AFB_HELPERS_SRCS})
+
+target_link_libraries(${TARGET_NAME} Qt5::WebSockets)
+
+target_link_libraries(${TARGET_NAME} curl)
+
+# Library properties
+SET_TARGET_PROPERTIES(${TARGET_NAME} PROPERTIES
+    OUTPUT_NAME ${TARGET_NAME}
+    )
+
+# Define target includes
+TARGET_INCLUDE_DIRECTORIES(${TARGET_NAME}
+    PUBLIC  ${CMAKE_CURRENT_SOURCE_DIR}
+    )
+
+CONFIGURE_FILE(afb-helpers.pc.in afb-helpers.pc @ONLY)
+INSTALL(FILES
+    ${CMAKE_CURRENT_BINARY_DIR}/afb-helpers.pc
+    DESTINATION
+    ${CMAKE_INSTALL_LIBDIR}/pkgconfig
+    )
+
+INSTALL(FILES
+    ${CMAKE_CURRENT_BINARY_DIR}/lib${TARGET_NAME}.so
+    DESTINATION
+    ${CMAKE_INSTALL_LIBDIR}
+    )
+INSTALL(FILES
+    ${AFB_HELPERS_HEADERS}
+    DESTINATION
+    ${CMAKE_INSTALL_INCLUDEDIR}
+    )
diff --git a/afb-helpers.pc.in b/afb-helpers.pc.in
new file mode 100644 (file)
index 0000000..3ddbbb8
--- /dev/null
@@ -0,0 +1,31 @@
+##
+## Copyright (C) 2016, 2017, 2018 "IoT.bzh"
+##
+## This file is part of afb-daemon project.
+##
+## 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.
+##
+
+prefix=@CMAKE_INSTALL_PREFIX@
+exec_prefix=${prefix}
+libdir=${exec_prefix}/lib
+includedir=${prefix}/include
+
+Name: @TARGET_NAME@
+Description: @PROJECT_DESCRIPTION@
+Version: @PROJECT_VERSION@
+URL: @PROJECT_URL@
+Libs.private:
+Libs: -L@CMAKE_INSTALL_LIBDIR@ -l@TARGET_NAME@
+Cflags: -I${includedir}
+
diff --git a/afb-timer.c b/afb-timer.c
new file mode 100644 (file)
index 0000000..30ab80d
--- /dev/null
@@ -0,0 +1,103 @@
+/*
+ * Copyright (C) 2016 "IoT.bzh"
+ * Author Fulup Ar Foll <fulup@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 <stdio.h>
+#include <string.h>
+#include <time.h>
+#include <systemd/sd-event.h>
+
+#include "afb-timer.h"
+
+#define DEFAULT_PAUSE_DELAY 3000
+#define DEFAULT_TEST_COUNT 1
+typedef struct {
+    int value;
+    const char *uid;
+} AutoTestCtxT;
+
+
+static int TimerNext (sd_event_source* source, uint64_t timer, void* handle) {
+    TimerHandleT *timerHandle = (TimerHandleT*) handle;
+    int done;
+    uint64_t usec;
+
+    done= timerHandle->callback(timerHandle);
+    if (!done) {
+        AFB_API_WARNING(timerHandle->api, "TimerNext Callback Fail Tag=%s", timerHandle->uid);
+        return -1;
+    }
+
+    // Rearm timer if needed
+    timerHandle->count --;
+    if (timerHandle->count == 0) {
+        sd_event_source_unref(source);
+        if (timerHandle->freeCB) timerHandle->freeCB(timerHandle->context);
+        free (handle);
+        return 0;
+    }
+    else {
+        // otherwise validate timer for a new run
+        sd_event_now(afb_api_get_event_loop(timerHandle->api), CLOCK_MONOTONIC, &usec);
+        sd_event_source_set_enabled(source, SD_EVENT_ONESHOT);
+        sd_event_source_set_time(source, usec + timerHandle->delay*1000);
+    }
+
+    return 0;
+}
+
+void TimerEvtStop(TimerHandleT *timerHandle) {
+
+    sd_event_source_unref(timerHandle->evtSource);
+    free (timerHandle);
+}
+
+
+void TimerEvtStart(afb_api_t apiHandle, TimerHandleT *timerHandle, timerCallbackT callback, void *context) {
+    uint64_t usec;
+
+    // populate CB handle
+    timerHandle->callback=callback;
+    timerHandle->context=context;
+    timerHandle->api=apiHandle;
+
+    // set a timer with ~250us accuracy
+    sd_event_now(afb_api_get_event_loop(apiHandle), CLOCK_MONOTONIC, &usec);
+    sd_event_add_time(afb_api_get_event_loop(apiHandle), &timerHandle->evtSource, CLOCK_MONOTONIC, usec+timerHandle->delay*1000, 250, TimerNext, timerHandle);
+}
+
+
+// Create Binding Event at Init
+int TimerEvtInit (afb_api_t apiHandle) {
+
+    AFB_API_DEBUG (apiHandle, "Timer-Init Done");
+    return 0;
+}
+
+uint64_t LockWait(afb_api_t apiHandle, uint64_t utimeout) {
+    uint64_t current_usec, pre_usec;
+
+    struct sd_event *event = afb_api_get_event_loop(apiHandle);
+
+    sd_event_now(event, CLOCK_MONOTONIC, &pre_usec);
+    sd_event_run(event, utimeout);
+    sd_event_now(event, CLOCK_MONOTONIC, &current_usec);
+
+    uint64_t diff = current_usec - pre_usec;
+    utimeout = utimeout < diff ? 0 : utimeout - diff;
+    return utimeout;
+}
diff --git a/afb-timer.h b/afb-timer.h
new file mode 100644 (file)
index 0000000..5050436
--- /dev/null
@@ -0,0 +1,55 @@
+/*
+ * Copyright (C) 2016 "IoT.bzh"
+ * Author Fulup Ar Foll <fulup@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.
+ */
+
+
+#ifndef AFB_TIMER_INCLUDE
+#define AFB_TIMER_INCLUDE
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#include <systemd/sd-event.h>
+#include <afb/afb-binding.h>
+
+// ctl-timer.c
+// ----------------------
+
+typedef struct TimerHandleS {
+    int magic;
+    int count;
+    int delay;
+    const char*uid;
+    void *context;
+    sd_event_source *evtSource;
+    afb_api_t api;
+    int (*callback) (struct TimerHandleS *handle);
+    int (*freeCB) (void *context) ;
+} TimerHandleT;
+
+typedef int (*timerCallbackT)(TimerHandleT *context);
+
+extern int TimerEvtInit (afb_api_t apiHandle);
+extern void TimerEvtStart(afb_api_t apiHandle, TimerHandleT *timerHandle, timerCallbackT callback, void *context);
+extern void TimerEvtStop(TimerHandleT *timerHandle);
+
+extern uint64_t LockWait(afb_api_t apiHandle, uint64_t utimeout);
+#ifdef __cplusplus
+}
+#endif
+
+#endif // CTL_TIMER_INCLUDE
index f58a2c9..6d302e3 100644 (file)
@@ -35,7 +35,10 @@ static int ScanDir(char* searchPath, CtlScanDirModeT mode, size_t extentionLen,
     struct dirent* dirEnt;
     dirHandle = opendir(searchPath);
     if (!dirHandle) {
-        AFB_DEBUG("CONFIG-SCANNING dir=%s not readable", searchPath);
+        if(afbBindingV3root)
+            AFB_API_DEBUG_V3(afbBindingV3root, "CONFIG-SCANNING dir=%s not readable", searchPath);
+        else
+            AFB_API_DEBUG_V3(afbBindingV2data.service.closure, "CONFIG-SCANNING dir=%s not readable", searchPath);
         return 0;
     }
 
@@ -146,24 +149,14 @@ const char* GetBinderName()
     return binderName;
 }
 
-#if(AFB_BINDING_VERSION == 0 && defined(AFB_BINDING_WANT_DYNAPI))
-char *GetBindingDirPath(struct afb_dynapi *dynapi)
-#else
-char *GetBindingDirPath()
-#endif
+char *GetBindingDirPath_(int fd)
 {
     // A file description should not be greater than 999.999.999
     char fd_link[CONTROL_MAXPATH_LEN];
     char retdir[CONTROL_MAXPATH_LEN];
     ssize_t len;
 
-#if(AFB_BINDING_VERSION == 0 && defined(AFB_BINDING_WANT_DYNAPI))
-    if (!dynapi)
-        return NULL;
-    sprintf(fd_link, "/proc/self/fd/%d", afb_dynapi_rootdir_get_fd(dynapi));
-#else
-    sprintf(fd_link, "/proc/self/fd/%d", afb_daemon_rootdir_get_fd());
-#endif
+    sprintf(fd_link, "/proc/self/fd/%d", fd);
 
     if ((len = readlink(fd_link, retdir, sizeof(retdir) - 1)) == -1) {
         perror("lstat");
@@ -176,6 +169,8 @@ char *GetBindingDirPath()
 }
 
 
+
+
 /**
  * @brief Takes an input string and makes it upper case. The output buffer
  * should be able to contains the whole input string else it will be truncated
index 64ecfb9..a74dcb3 100644 (file)
@@ -86,10 +86,21 @@ extern json_object* ScanForConfig (const char* searchPath, CtlScanDirModeT mode,
  *
  * @return char* string representing the path to binding root directory.
  */
-#if(AFB_BINDING_VERSION == 0 && defined(AFB_BINDING_WANT_DYNAPI))
-extern char *GetBindingDirPath(struct afb_dynapi *dynapi);
+extern char *GetBindingDirPath_(int fd);
+#if(AFB_BINDING_VERSION == 2)
+static inline char *GetBindingDirPath_v2()
+{
+    return GetBindingDirPath_(afb_daemon_rootdir_get_fd_v2());
+}
+__attribute__((alias("GetBindingDirPath_v2")))
+static char *GetBindingDirPath();
 #else
-extern char *GetBindingDirPath();
+static char *GetBindingDirPath_v3(struct afb_api_x3* apiHandle)
+{
+    return GetBindingDirPath_(afb_api_x3_rootdir_get_fd(apiHandle));
+}
+__attribute__((alias("GetBindingDirPath_v3")))
+static char *GetBindingDirPath(struct afb_api_x3* apiHandle);
 #endif
 
 /**