aboutsummaryrefslogtreecommitdiffstats
path: root/signal-composer-binding
diff options
context:
space:
mode:
authorRomain Forlot <romain.forlot@iot.bzh>2017-09-10 19:46:02 +0200
committerRomain Forlot <romain.forlot@iot.bzh>2017-12-14 11:00:25 +0100
commit75cfbf3d0206f12422091e6479c508c69445bf4a (patch)
tree200085512507e7ee5adc43b377d34ff5953db8de /signal-composer-binding
parentbdf2d19cf8a3d6005456508ca52dba74cd9c103f (diff)
Controller src integration
Change-Id: I0b18cd55057a784d183a5ba02c332810a34d1fca Signed-off-by: Romain Forlot <romain.forlot@iot.bzh>
Diffstat (limited to 'signal-composer-binding')
-rw-r--r--signal-composer-binding/CMakeLists.txt2
-rw-r--r--signal-composer-binding/ctl-dispatch.c705
-rw-r--r--signal-composer-binding/ctl-lua.c64
-rw-r--r--signal-composer-binding/ctl-lua.h61
-rw-r--r--signal-composer-binding/signal-composer-apidef.h76
-rw-r--r--signal-composer-binding/signal-composer-apidef.json5
-rw-r--r--signal-composer-binding/signal-composer-binding.cpp30
-rw-r--r--signal-composer-binding/signal-composer-binding.hpp7
-rw-r--r--signal-composer-binding/signal-conf.cpp16
-rw-r--r--signal-composer-binding/signal-conf.hpp27
-rw-r--r--signal-composer-binding/signal.hpp46
11 files changed, 894 insertions, 145 deletions
diff --git a/signal-composer-binding/CMakeLists.txt b/signal-composer-binding/CMakeLists.txt
index 52f8418..78db103 100644
--- a/signal-composer-binding/CMakeLists.txt
+++ b/signal-composer-binding/CMakeLists.txt
@@ -21,7 +21,7 @@
PROJECT_TARGET_ADD(signal-composer)
# Define project Targets
- add_library(${TARGET_NAME} MODULE ${TARGET_NAME}-binding.cpp ${TARGET_NAME}.cpp ctl-lua.c)
+ add_library(${TARGET_NAME} MODULE ${TARGET_NAME}-binding.cpp ${TARGET_NAME}.cpp ctl-lua.c ctl-dispatch.c)
# Binder exposes a unique public entry point
SET_TARGET_PROPERTIES(${TARGET_NAME} PROPERTIES
diff --git a/signal-composer-binding/ctl-dispatch.c b/signal-composer-binding/ctl-dispatch.c
new file mode 100644
index 0000000..b84c86e
--- /dev/null
+++ b/signal-composer-binding/ctl-dispatch.c
@@ -0,0 +1,705 @@
+/*
+ * 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, something express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * Reference:
+ * Json load using json_unpack https://jansson.readthedocs.io/en/2.9/apiref.html#parsing-and-validating-values
+ */
+
+#define _GNU_SOURCE
+#include <stdio.h>
+#include <string.h>
+#include <dlfcn.h>
+#include <sys/prctl.h>
+#include <filescan-utils.h>
+#include <wrap-json.h>
+
+#include "ctl-lua.h"
+#include "signal-composer-binding.hpp"
+
+typedef void*(*DispatchPluginInstallCbT)(const char* label, const char*version, const char*info);
+
+static afb_req NULL_AFBREQ = {};
+
+typedef struct {
+ const char* id;
+ const char* source;
+ const char* class;
+ DispatchActionT* onReceived;
+} DispatchSignalT;
+
+typedef struct {
+ const char* label;
+ const char* info;
+ const char* ssource;
+ const char* sclass;
+ DispatchActionT* actions;
+} DispatchHandleT;
+
+typedef struct {
+ const char *label;
+ const char *info;
+ void *context;
+ char *sharelib;
+ void *dlHandle;
+ luaL_Reg *l2cFunc;
+ int l2cCount;
+} DispatchPluginT;
+
+typedef struct {
+ const char* label;
+ const char *info;
+ const char *version;
+ DispatchPluginT *plugin;
+ DispatchHandleT **sources;
+ DispatchHandleT **signals;
+} DispatchConfigT;
+
+// global config handle
+static DispatchConfigT *configHandle = NULL;
+
+static int DispatchSignalToIndex(DispatchHandleT **signals, const char* controlLabel) {
+
+ for (int idx = 0; signals[idx]; idx++) {
+ if (!strcasecmp(controlLabel, signals[idx]->label)) return idx;
+ }
+ return -1;
+}
+
+static int DispatchOneSignal(DispatchSourceT source, DispatchHandleT **signals, const char* controlLabel, json_object *queryJ, afb_req request) {
+ int err;
+
+ if (!configHandle) {
+ AFB_ERROR("DISPATCH-CTL-API: (Hoops/Bug!!!) No Config Loaded");
+ return -1;
+ }
+
+ if (!configHandle->signals) {
+ AFB_ERROR("DISPATCH-CTL-API: No Signal Action in Json config label=%s version=%s", configHandle->label, configHandle->version);
+ return -1;
+ }
+
+ int index = DispatchSignalToIndex(signals, controlLabel);
+ if (index < 0 || !signals[index]->actions) {
+ AFB_ERROR("DISPATCH-CTL-API:NotFound/Error label=%s in Json Signal Config File", controlLabel);
+ return -1;
+ }
+
+ // Fulup (Bug/Feature) in current version is unique to every onload profile
+ if (configHandle->plugin && configHandle->plugin->l2cCount) {
+ LuaL2cNewLib (configHandle->plugin->label, configHandle->plugin->l2cFunc, configHandle->plugin->l2cCount);
+ }
+
+ // loop on action for this control
+ DispatchActionT *actions = signals[index]->actions;
+ for (int idx = 0; actions[idx].label; idx++) {
+
+ switch (actions[idx].mode) {
+ case CTL_MODE_API:
+ {
+ json_object *returnJ;
+
+ // if query is empty increment usage count and pass args
+ if (!queryJ || json_object_get_type(queryJ) != json_type_object) {
+ json_object_get(actions[idx].argsJ);
+ queryJ= actions[idx].argsJ;
+ } else if (actions[idx].argsJ) {
+
+ // Merge queryJ and argsJ before sending request
+ if (json_object_get_type(actions[idx].argsJ) == json_type_object) {
+ json_object_object_foreach(actions[idx].argsJ, key, val) {
+ json_object_object_add(queryJ, key, val);
+ }
+ } else {
+ json_object_object_add(queryJ, "args", actions[idx].argsJ);
+ }
+ }
+
+ int err = afb_service_call_sync(actions[idx].api, actions[idx].call, queryJ, &returnJ);
+ if (err) {
+ static const char*format = "DispatchOneSignal(Api) api=%s verb=%s args=%s";
+ if (afb_req_is_valid(request))afb_req_fail_f(request, "DISPATCH-CTL-MODE:API", format, actions[idx].label, actions[idx].api, actions[idx].call);
+ else AFB_ERROR(format, actions[idx].api, actions[idx].call, actions[idx].label);
+ return -1;
+ }
+ break;
+ }
+
+#ifdef CONTROL_SUPPORT_LUA
+ case CTL_MODE_LUA:
+ err = LuaCallFunc(source, &actions[idx], queryJ);
+ if (err) {
+ static const char*format = "DispatchOneSignal(Lua) label=%s func=%s args=%s";
+ if (afb_req_is_valid(request)) afb_req_fail_f(request, "DISPATCH-CTL-MODE:Lua", format, actions[idx].label, actions[idx].call, json_object_get_string(actions[idx].argsJ));
+ else AFB_ERROR(format, actions[idx].label, actions[idx].call, json_object_get_string(actions[idx].argsJ));
+ return -1;
+ }
+ break;
+#endif
+
+ case CTL_MODE_CB:
+ err = (*actions[idx].actionCB) (source, actions[idx].label, actions[idx].argsJ, queryJ, configHandle->plugin->context);
+ if (err) {
+ static const char*format = "DispatchOneSignal(Callback) label%s func=%s args=%s";
+ if (afb_req_is_valid(request)) afb_req_fail_f(request, "DISPATCH-CTL-MODE:Cb", format, actions[idx].label, actions[idx].call, json_object_get_string(actions[idx].argsJ));
+ else AFB_ERROR(format, actions[idx].label, actions[idx].call, json_object_get_string(actions[idx].argsJ));
+ return -1;
+ }
+ break;
+
+ default:
+ {
+ static const char*format = "DispatchOneSignal(unknown) mode control=%s action=%s";
+ AFB_ERROR(format, signals[index]->label);
+ if (afb_req_is_valid(request))afb_req_fail_f(request, "DISPATCH-CTL-MODE:Unknown", format, signals[index]->label);
+ }
+ }
+ }
+
+ // everything when fine
+ if (afb_req_is_valid(request)) afb_req_success(request, NULL, signals[index]->label);
+ return 0;
+}
+
+// Event name is mapped on control label and executed as a standard control
+/*
+void DispatchOneEvent(const char *evtLabel, json_object *eventJ) {
+ DispatchHandleT **events = configHandle->events;
+
+ (void) DispatchOneSignal(CTL_SOURCE_EVENT, events, evtLabel, eventJ, NULL_AFBREQ);
+}
+*/
+// Event name is mapped on control label and executed as a standard control
+
+int DispatchSources() {
+ if (!configHandle) return 1;
+
+ int err = 0;
+ DispatchHandleT **sources = configHandle->sources;
+ ssize_t i = 0, nSources = sizeof(*sources) / sizeof(DispatchHandleT);
+
+ while(i < nSources) {
+ const char* sourceLabel = sources[i++]->label;
+ err = DispatchOneSignal(CTL_SOURCE_ONLOAD, sources, sourceLabel, NULL, NULL_AFBREQ);
+ }
+
+ return err;
+}
+
+void ctlapi_dispatch(afb_req request) {
+ DispatchHandleT **signals = configHandle->signals;
+ json_object *queryJ, *argsJ=NULL;
+ const char *target;
+ DispatchSourceT source= CTL_SOURCE_UNKNOWN;
+
+ queryJ = afb_req_json(request);
+ int err = wrap_json_unpack(queryJ, "{s:s, s?i s?o !}", "target", &target, "source", &source, "args", &argsJ);
+ if (err) {
+ afb_req_fail_f(request, "CTL-DISPTACH-INVALID", "missing target or args not a valid json object query=%s", json_object_get_string(queryJ));
+ return;
+ }
+
+ (void) DispatchOneSignal(source, signals, target, argsJ, request);
+}
+
+// Wrapper to Lua2c plugin command add context dans delegate to LuaWrapper
+int DispatchOneL2c(lua_State* luaState, char *funcname, Lua2cFunctionT callback) {
+#ifndef CONTROL_SUPPORT_LUA
+ AFB_ERROR("DISPATCH-ONE-L2C: LUA support not selected (cf:CONTROL_SUPPORT_LUA) in config.cmake");
+ return 1;
+#else
+ int err=Lua2cWrapper(luaState, funcname, callback, configHandle->plugin->context);
+ return err;
+#endif
+}
+
+
+// List Avaliable Configuration Files
+
+void ctlapi_config(struct afb_req request) {
+ json_object*tmpJ;
+ char *dirList;
+ // Compile some default directories to browse
+ char defaultConfPath[CONTROL_MAXPATH_LEN];
+ strncpy(defaultConfPath, GetBindingDirPath(), sizeof(GetBindingDirPath()));
+ strncat(defaultConfPath, "/etc:", sizeof(defaultConfPath) - strlen(defaultConfPath) - 1);
+ strncat(defaultConfPath, GetBindingDirPath(), sizeof(defaultConfPath) - strlen(defaultConfPath) - 1);
+ strncat(defaultConfPath, "/data", sizeof(defaultConfPath) - strlen(defaultConfPath) - 1);
+
+
+ json_object* queryJ = afb_req_json(request);
+ if (queryJ && json_object_object_get_ex(queryJ, "cfgpath", &tmpJ)) {
+ dirList = strdup(json_object_get_string(tmpJ));
+ } else {
+
+ dirList = getenv("CONTROL_CONFIG_PATH");
+ if (!dirList) dirList = defaultConfPath;
+ AFB_NOTICE("CONFIG-MISSING: use default CONTROL_CONFIG_PATH=%s", defaultConfPath);
+ }
+
+ // get list of config file
+ struct json_object *responseJ = ScanForConfig(dirList, CTL_SCAN_RECURSIVE, "onload", "json");
+
+ if (json_object_array_length(responseJ) == 0) {
+ afb_req_fail(request, "CONFIGPATH:EMPTY", "No Config Found in CONTROL_CONFIG_PATH");
+ } else {
+ afb_req_success(request, responseJ, NULL);
+ }
+
+ return;
+}
+
+// unpack individual action object
+
+static int DispatchLoadOneAction(DispatchConfigT *controlConfig, json_object *actionJ, DispatchActionT *action) {
+ char *api = NULL, *verb = NULL, *callback = NULL, *lua = NULL, *function = NULL;
+ int err, modeCount = 0;
+
+ err = wrap_json_unpack(actionJ, "{s?s,s?s,s?s,s?s,s?s,s?s,s?s,s?o !}"
+ , "label", &action->label
+ , "info", &action->info
+ , "function", &function
+ , "callback", &callback
+ , "lua", &lua
+ , "api", &api, "verb", &verb
+ , "args", &action->argsJ);
+ if (err) {
+ AFB_ERROR("DISPATCH-LOAD-ACTION Missing something label|info|callback|lua|(api+verb)|args in %s", json_object_get_string(actionJ));
+ return -1;
+ }
+
+ // Generic way to specify a C or LUA actions
+ if(function) {
+ if(strcasestr(function, "lua")) {
+ action->mode = CTL_MODE_LUA;
+ action->call = function;
+ modeCount++;
+ }
+ else if(controlConfig->plugin) {
+ action->mode = CTL_MODE_CB;
+ action->call = callback;
+ modeCount++;
+
+ action->actionCB = dlsym(controlConfig->plugin->dlHandle, callback);
+ if (!action->actionCB) {
+ AFB_ERROR("DISPATCH-LOAD-ACTION fail to find calbback=%s in %s", callback, controlConfig->plugin->sharelib);
+ return -1;
+ }
+ }
+ // If label not already set then use function name
+ if(! &action->label) action->label = function;
+ }
+
+ if (lua) {
+ action->mode = CTL_MODE_LUA;
+ action->call = lua;
+ modeCount++;
+ // If label not already set then use function name
+ if(! &action->label) action->label = lua;
+ }
+
+ if (api && verb) {
+ action->mode = CTL_MODE_API;
+ action->api = api;
+ action->call = verb;
+ modeCount++;
+ char* apiVerb = strdup(api);
+ apiVerb = strncat(apiVerb, "/", sizeof(*apiVerb) - strlen(apiVerb) - 1);
+ apiVerb = strncat(apiVerb, verb, sizeof(*apiVerb) - strlen(apiVerb) - 1);
+ // If label not already set then use function name
+ if(! &action->label) action->label = apiVerb;
+ free(apiVerb);
+ }
+
+ if (callback && controlConfig->plugin) {
+ action->mode = CTL_MODE_CB;
+ action->call = callback;
+ modeCount++;
+
+ action->actionCB = dlsym(controlConfig->plugin->dlHandle, callback);
+ if (!action->actionCB) {
+ AFB_ERROR("DISPATCH-LOAD-ACTION fail to find calbback=%s in %s", callback, controlConfig->plugin->sharelib);
+ return -1;
+ }
+ }
+
+ // make sure at least one mode is selected
+ if (modeCount == 0) {
+ AFB_ERROR("DISPATCH-LOAD-ACTION No Action Selected lua|callback|(api+verb) in %s", json_object_get_string(actionJ));
+ return -1;
+ }
+
+ if (modeCount > 1) {
+ AFB_ERROR("DISPATCH-LOAD-ACTION:Too Many arguments lua|callback|(api+verb) in %s", json_object_get_string(actionJ));
+ return -1;
+ }
+ return 0;
+};
+
+static DispatchActionT *DispatchLoadActions(DispatchConfigT *controlConfig, json_object *actionsJ) {
+ int err;
+ DispatchActionT *actions;
+
+ // action array is close with a nullvalue;
+ if (json_object_get_type(actionsJ) == json_type_array) {
+ int count = json_object_array_length(actionsJ);
+ actions = calloc(count + 1, sizeof (DispatchActionT));
+
+ for (int idx = 0; idx < count; idx++) {
+ json_object *actionJ = json_object_array_get_idx(actionsJ, idx);
+ err = DispatchLoadOneAction(controlConfig, actionJ, &actions[idx]);
+ if (err) return NULL;
+ }
+
+ } else {
+ actions = calloc(2, sizeof (DispatchActionT));
+ err = DispatchLoadOneAction(controlConfig, actionsJ, &actions[0]);
+ if (err) return NULL;
+ }
+
+ return actions;
+}
+
+static void DispatchLoadPlugin(DispatchConfigT *controlConfig, json_object* sourcesJ, json_object* pluginJ)
+{
+ int err = 0;
+ json_object *lua2csJ = NULL;
+ DispatchPluginT *dPlugin= calloc(1, sizeof(DispatchPluginT));
+ controlConfig->plugin = dPlugin;
+ const char*ldSearchPath=NULL;
+
+ err = wrap_json_unpack(pluginJ, "{ss,s?s,s?s,ss,s?o!}",
+ "label", &dPlugin->label, "info", &dPlugin->info, "ldpath", &ldSearchPath, "sharelib", &dPlugin->sharelib, "lua2c", &lua2csJ);
+ if (err) {
+ AFB_ERROR("DISPATCH-LOAD-CONFIG:ONLOAD Plugin missing label|[info]|sharelib|[lua2c] in %s", json_object_get_string(sourcesJ));
+ return;
+ }
+
+ // if search path not in Json config file, then try default
+ if (!ldSearchPath) ldSearchPath= strncat(GetBindingDirPath(), "/data", sizeof(GetBindingDirPath()) - strlen(GetBindingDirPath()) - 1);
+
+ // search for default policy config file
+ json_object *pluginPathJ = ScanForConfig(ldSearchPath, CTL_SCAN_RECURSIVE, dPlugin->sharelib, NULL);
+ if (!pluginPathJ || json_object_array_length(pluginPathJ) == 0) {
+ AFB_ERROR("DISPATCH-LOAD-CONFIG:PLUGIN Missing plugin=%s in path=%s", dPlugin->sharelib, ldSearchPath);
+ return;
+ }
+
+ char *filename;
+ char*fullpath;
+ err = wrap_json_unpack(json_object_array_get_idx(pluginPathJ, 0), "{s:s, s:s !}", "fullpath", &fullpath, "filename", &filename);
+ if (err) {
+ AFB_ERROR("DISPATCH-LOAD-CONFIG:PLUGIN HOOPs invalid plugin file path = %s", json_object_get_string(pluginPathJ));
+ return;
+ }
+
+ if (json_object_array_length(pluginPathJ) > 1) {
+ AFB_WARNING("DISPATCH-LOAD-CONFIG:PLUGIN plugin multiple instances in searchpath will use %s/%s", fullpath, filename);
+ }
+
+ char pluginpath[CONTROL_MAXPATH_LEN];
+ strncpy(pluginpath, fullpath, sizeof (pluginpath));
+ strncat(pluginpath, "/", sizeof (pluginpath)-strlen(pluginpath)-1);
+ strncat(pluginpath, filename, sizeof (pluginpath)-strlen(pluginpath)-1);
+ dPlugin->dlHandle = dlopen(pluginpath, RTLD_NOW);
+ if (!dPlugin->dlHandle) {
+ AFB_ERROR("DISPATCH-LOAD-CONFIG:PLUGIN Fail to load pluginpath=%s err= %s", pluginpath, dlerror());
+ return;
+ }
+
+ CtlPluginMagicT *ctlPluginMagic = (CtlPluginMagicT*) dlsym(dPlugin->dlHandle, "CtlPluginMagic");
+ if (!ctlPluginMagic || ctlPluginMagic->magic != CTL_PLUGIN_MAGIC) {
+ AFB_ERROR("DISPATCH-LOAD-CONFIG:Plugin symbol'CtlPluginMagic' missing or != CTL_PLUGIN_MAGIC plugin=%s", pluginpath);
+ return;
+ } else {
+ AFB_NOTICE("DISPATCH-LOAD-CONFIG:Plugin %s successfully registered", ctlPluginMagic->label);
+ }
+
+ // Jose hack to make verbosity visible from sharelib
+ struct afb_binding_data_v2 *afbHidenData = dlsym(dPlugin->dlHandle, "afbBindingV2data");
+ if (afbHidenData) *afbHidenData = afbBindingV2data;
+
+ // Push lua2cWrapper @ into plugin
+ Lua2cWrapperT *lua2cInPlug = dlsym(dPlugin->dlHandle, "Lua2cWrap");
+#ifndef CONTROL_SUPPORT_LUA
+ if (lua2cInPlug) *lua2cInPlug = NULL;
+#else
+ // Lua2cWrapper is part of binder and not expose to dynamic link
+ if (lua2cInPlug) *lua2cInPlug = DispatchOneL2c;
+
+ {
+ int Lua2cAddOne(luaL_Reg *l2cFunc, const char* l2cName, int index) {
+ char funcName[CONTROL_MAXPATH_LEN];
+ strncpy(funcName, "lua2c_", sizeof(funcName));
+ strncat(funcName, l2cName, sizeof(funcName)-strlen(funcName)-1);
+
+ Lua2cFunctionT l2cFunction= (Lua2cFunctionT)dlsym(dPlugin->dlHandle, funcName);
+ if (!l2cFunction) {
+ AFB_ERROR("DISPATCH-LOAD-CONFIG:Plugin symbol'%s' missing err=%s", funcName, dlerror());
+ return 1;
+ }
+ l2cFunc[index].func=(void*)l2cFunction;
+ l2cFunc[index].name=strdup(l2cName);
+
+ return 0;
+ }
+
+ int errCount = 0;
+ luaL_Reg *l2cFunc=NULL;
+ int count=0;
+
+ // look on l2c command and push them to LUA
+ if (json_object_get_type(lua2csJ) == json_type_array) {
+ int length = json_object_array_length(lua2csJ);
+ l2cFunc = calloc(length + 1, sizeof (luaL_Reg));
+ for (count=0; count < length; count++) {
+ int err;
+ const char *l2cName = json_object_get_string(json_object_array_get_idx(lua2csJ, count));
+ err = Lua2cAddOne(l2cFunc, l2cName, count);
+ if (err) errCount++;
+ }
+ } else {
+ l2cFunc = calloc(2, sizeof (luaL_Reg));
+ const char *l2cName = json_object_get_string(lua2csJ);
+ errCount = Lua2cAddOne(l2cFunc, l2cName, 0);
+ count=1;
+ }
+ if (errCount) {
+ AFB_ERROR("DISPATCH-LOAD-CONFIG:Plugin %d symbols not found in plugin='%s'", errCount, pluginpath);
+ return;
+ } else {
+ dPlugin->l2cFunc= l2cFunc;
+ dPlugin->l2cCount= count;
+ }
+ }
+#endif
+ DispatchPluginInstallCbT ctlPluginOnload = dlsym(dPlugin->dlHandle, "CtlPluginOnload");
+ if (ctlPluginOnload) {
+ dPlugin->context = (*ctlPluginOnload) (controlConfig->label, controlConfig->version, controlConfig->info);
+ }
+}
+
+static DispatchHandleT *DispatchLoadSignal(DispatchConfigT *controlConfig, json_object *controlJ) {
+ json_object *actionsJ, *permissionsJ;
+ int err;
+
+ DispatchHandleT *dispatchHandle = calloc(1, sizeof (DispatchHandleT));
+ err = wrap_json_unpack(controlJ, "{ss,s?s,ss,ss,s?o,so !}"
+ , "id", &dispatchHandle->label
+ , "info", &dispatchHandle->info
+ , "source", &dispatchHandle->ssource
+ , "class", &dispatchHandle->sclass
+ , "permissions", &permissionsJ
+ , "onReceived", &actionsJ);
+ if (err) {
+ AFB_ERROR("DISPATCH-LOAD-CONFIG:CONTROL Missing something label|[info]|actions in %s", json_object_get_string(controlJ));
+ return NULL;
+ }
+
+ dispatchHandle->actions = DispatchLoadActions(controlConfig, actionsJ);
+ if (!dispatchHandle->actions) {
+ AFB_ERROR("DISPATCH-LOAD-CONFIG:CONTROL Error when parsing actions %s", dispatchHandle->label);
+ return NULL;
+ }
+ return dispatchHandle;
+}
+
+static DispatchHandleT *DispatchLoadSource(DispatchConfigT *controlConfig, json_object *sourcesJ) {
+ json_object *actionsJ = NULL, *pluginJ = NULL;
+ int err;
+
+ DispatchHandleT *dispatchSources = calloc(1, sizeof (DispatchHandleT));
+ err = wrap_json_unpack(sourcesJ, "{ss,s?s,s?o,s?o,s?o !}",
+ "api", &dispatchSources->label, "info", &dispatchSources->info, "plugin", &pluginJ, "actions", &actionsJ);
+ if (err) {
+ AFB_ERROR("DISPATCH-LOAD-CONFIG:ONLOAD Missing something label|[info]|[plugin]|[actions] in %s", json_object_get_string(sourcesJ));
+ return NULL;
+ }
+
+ // best effort to initialise everything before starting
+ err = afb_daemon_require_api(dispatchSources->label, 1);
+ if (err) {
+ AFB_WARNING("DISPATCH-LOAD-CONFIG:REQUIRE Fail to get=%s", dispatchSources->label);
+ }
+
+ if (pluginJ) {
+ DispatchLoadPlugin(controlConfig, sourcesJ, pluginJ);
+ }
+
+ dispatchSources->actions = DispatchLoadActions(controlConfig, actionsJ);
+ if (!dispatchSources->actions) {
+ AFB_ERROR("DISPATCH-LOAD-CONFIG:ONLOAD Error when parsing actions %s", dispatchSources->label);
+ return NULL;
+ }
+ return dispatchSources;
+}
+
+static DispatchConfigT *DispatchLoadConfig(const char* filepath) {
+ json_object *controlConfigJ, *ignoreJ;
+ int err;
+
+ // Load JSON file
+ controlConfigJ = json_object_from_file(filepath);
+ if (!controlConfigJ) {
+ AFB_ERROR("DISPATCH-LOAD-CONFIG:JsonLoad invalid JSON %s ", filepath);
+ return NULL;
+ }
+
+ AFB_INFO("DISPATCH-LOAD-CONFIG: loading config filepath=%s", filepath);
+
+ json_object *metadataJ = NULL, *sourcesJ = NULL, *signalsJ = NULL;
+ err = wrap_json_unpack(controlConfigJ, "{s?s,s?o,s?o,s?o !}", "$schema", &ignoreJ, "metadata", &metadataJ, "sources", &sourcesJ, "signals", &signalsJ);
+ if (err) {
+ AFB_ERROR("DISPATCH-LOAD-CONFIG Missing something metadata|[sources]|[signals] in %s", json_object_get_string(controlConfigJ));
+ return NULL;
+ }
+
+ DispatchConfigT *controlConfig = calloc(1, sizeof (DispatchConfigT));
+ if (metadataJ) {
+ const char*ctlname=NULL;
+ err = wrap_json_unpack(metadataJ, "{ss,s?s,s?s,ss !}", "label", &controlConfig->label, "version", &controlConfig->version, "name", &ctlname, "info", &controlConfig->info);
+ if (err) {
+ AFB_ERROR("DISPATCH-LOAD-CONFIG:METADATA Missing something label|version|[label] in %s", json_object_get_string(metadataJ));
+ return NULL;
+ }
+
+ // if ctlname is provided change process name now
+ if (ctlname) {
+ err= prctl(PR_SET_NAME, ctlname,NULL,NULL,NULL);
+ if (err) AFB_WARNING("Fail to set Process Name to:%s",ctlname);
+ }
+ }
+
+ if (sourcesJ) {
+ DispatchHandleT *dispatchHandle;
+
+ if (json_object_get_type(sourcesJ) != json_type_array) {
+ controlConfig->sources = (DispatchHandleT**) calloc(2, sizeof (void*));
+ dispatchHandle = DispatchLoadSource(controlConfig, sourcesJ);
+ controlConfig->sources[0] = dispatchHandle;
+ } else {
+ int length = json_object_array_length(sourcesJ);
+ controlConfig->sources = (DispatchHandleT**) calloc(length + 1, sizeof (void*));
+
+ for (int jdx = 0; jdx < length; jdx++) {
+ json_object *sourcesJ = json_object_array_get_idx(sourcesJ, jdx);
+ dispatchHandle = DispatchLoadSource(controlConfig, sourcesJ);
+ controlConfig->sources[jdx] = dispatchHandle;
+ }
+ }
+ }
+
+ if (signalsJ) {
+ DispatchHandleT* dispatchHandle;
+
+ if (json_object_get_type(signalsJ) != json_type_array) {
+ controlConfig->signals = (DispatchHandleT**) calloc(2, sizeof (void*));
+ dispatchHandle = DispatchLoadSignal(controlConfig, signalsJ);
+ controlConfig->signals[0] = dispatchHandle;
+ } else {
+ int length = json_object_array_length(signalsJ);
+ controlConfig->signals = (DispatchHandleT**) calloc(length + 1, sizeof (void*));
+
+ for (int jdx = 0; jdx < length; jdx++) {
+ json_object *controlJ = json_object_array_get_idx(signalsJ, jdx);
+ dispatchHandle = DispatchLoadSignal(controlConfig, controlJ);
+ controlConfig->signals[jdx] = dispatchHandle;
+ }
+ }
+ }
+/*
+ if (eventsJ) {
+ DispatchHandleT *dispatchHandle;
+
+ if (json_object_get_type(eventsJ) != json_type_array) {
+ controlConfig->events = (DispatchHandleT**) calloc(2, sizeof (void*));
+ dispatchHandle = DispatchLoadSignal(controlConfig, eventsJ);
+ controlConfig->events[0] = dispatchHandle;
+ } else {
+ int length = json_object_array_length(eventsJ);
+ controlConfig->events = (DispatchHandleT**) calloc(length + 1, sizeof (void*));
+
+ for (int jdx = 0; jdx < length; jdx++) {
+ json_object *eventJ = json_object_array_get_idx(eventsJ, jdx);
+ dispatchHandle = DispatchLoadSignal(controlConfig, eventJ);
+ controlConfig->events[jdx] = dispatchHandle;
+ }
+ }
+ }
+*/
+ return controlConfig;
+}
+
+
+// Load default config file at init
+
+int DispatchInit() {
+ int index, luaLoaded = 0;
+ char controlFile [CONTROL_MAXPATH_LEN];
+ // Compile some default directories to browse
+ char defaultConfPath[CONTROL_MAXPATH_LEN];
+ strncpy(defaultConfPath, GetBindingDirPath(), sizeof(GetBindingDirPath()));
+ strncat(defaultConfPath, "/etc:", sizeof(defaultConfPath) - strlen(defaultConfPath) - 1);
+ strncat(defaultConfPath, GetBindingDirPath(), sizeof(defaultConfPath) - strlen(defaultConfPath) - 1);
+ strncat(defaultConfPath, "/data", sizeof(defaultConfPath) - strlen(defaultConfPath) - 1);
+
+ const char *dirList = getenv("CONTROL_CONFIG_PATH");
+ if (!dirList) dirList = defaultConfPath;
+
+ strncpy(controlFile, CONTROL_CONFIG_PRE "-", CONTROL_MAXPATH_LEN);
+ strncat(controlFile, GetBinderName(), CONTROL_MAXPATH_LEN-strlen(controlFile)-1);
+
+ // search for default dispatch config file
+ json_object* responseJ = ScanForConfig(dirList, CTL_SCAN_RECURSIVE, controlFile, "json");
+
+ // We load 1st file others are just warnings
+ for (index = 0; index < json_object_array_length(responseJ); index++) {
+ json_object *entryJ = json_object_array_get_idx(responseJ, index);
+
+ char *filename;
+ char*fullpath;
+ int err = wrap_json_unpack(entryJ, "{s:s, s:s !}", "fullpath", &fullpath, "filename", &filename);
+ if (err) {
+ AFB_ERROR("DISPATCH-INIT HOOPs invalid JSON entry= %s", json_object_get_string(entryJ));
+ return -1;
+ }
+
+ if (index == 0) {
+ if (strcasestr(filename, controlFile)) {
+ char filepath[CONTROL_MAXPATH_LEN];
+ strncpy(filepath, fullpath, sizeof (filepath));
+ strncat(filepath, "/", sizeof (filepath)-strlen(filepath)-1);
+ strncat(filepath, filename, sizeof (filepath)-strlen(filepath)-1);
+ configHandle = DispatchLoadConfig(filepath);
+ if (!configHandle) {
+ AFB_ERROR("DISPATCH-INIT:ERROR Fail loading [%s]", filepath);
+ return -1;
+ }
+ luaLoaded = 1;
+ break;
+ }
+ } else {
+ AFB_WARNING("DISPATCH-INIT:WARNING Secondary Signal Config Ignored %s/%s", fullpath, filename);
+ }
+ }
+
+ // no dispatch config found remove control API from binder
+ if (!luaLoaded) {
+ AFB_WARNING("DISPATCH-INIT:WARNING (setenv CONTROL_CONFIG_PATH) No Config '%s-*.json' in '%s'", controlFile, dirList);
+ }
+
+ AFB_NOTICE("DISPATCH-INIT:SUCCES: Signal Dispatch Init");
+ return 0;
+}
diff --git a/signal-composer-binding/ctl-lua.c b/signal-composer-binding/ctl-lua.c
index e8e48fa..2773696 100644
--- a/signal-composer-binding/ctl-lua.c
+++ b/signal-composer-binding/ctl-lua.c
@@ -80,17 +80,17 @@ typedef enum {
*/
static LuaAfbContextT *LuaCtxCheck (lua_State *luaState, int index) {
- LuaAfbContextT *afbContext;
- //luaL_checktype(luaState, index, LUA_TUSERDATA);
- //afbContext = (LuaAfbContextT *)luaL_checkudata(luaState, index, CTX_TOKEN);
- luaL_checktype(luaState, index, LUA_TLIGHTUSERDATA);
- afbContext = (LuaAfbContextT *) lua_touserdata(luaState, index);
- if (afbContext == NULL && afbContext->ctxMagic != CTX_MAGIC) {
- luaL_error(luaState, "Fail to retrieve user data context=%s", CTX_TOKEN);
- AFB_ERROR ("afbContextCheck error retrieving afbContext");
- return NULL;
- }
- return afbContext;
+ LuaAfbContextT *afbContext;
+ //luaL_checktype(luaState, index, LUA_TUSERDATA);
+ //afbContext = (LuaAfbContextT *)luaL_checkudata(luaState, index, CTX_TOKEN);
+ luaL_checktype(luaState, index, LUA_TLIGHTUSERDATA);
+ afbContext = (LuaAfbContextT *) lua_touserdata(luaState, index);
+ if (afbContext == NULL && afbContext->ctxMagic != CTX_MAGIC) {
+ luaL_error(luaState, "Fail to retrieve user data context=%s", CTX_TOKEN);
+ AFB_ERROR ("afbContextCheck error retrieving afbContext");
+ return NULL;
+ }
+ return afbContext;
}
static LuaAfbContextT *LuaCtxPush (lua_State *luaState, afb_req request, void *handle, const char* info) {
@@ -265,7 +265,7 @@ static json_object *LuaPopArgs (lua_State* luaState, int start) {
json_object *responseJ;
int stop = lua_gettop(luaState);
- if(stop-start <0) return NULL;
+ if(stop-start <0) goto OnErrorExit;
// start at 2 because we are using a function array lib
if (start == stop) {
@@ -277,13 +277,13 @@ static json_object *LuaPopArgs (lua_State* luaState, int start) {
json_object *argJ=LuaPopOneArg (luaState, idx);
if (!argJ) goto OnErrorExit;
json_object_array_add(responseJ, argJ);
- }
+ }
}
return responseJ;
- OnErrorExit:
- return NULL;
+ OnErrorExit:
+ return NULL;
}
@@ -660,18 +660,14 @@ int LuaCallFunc (DispatchSourceT source, DispatchActionT *action, json_object *q
err=lua_pcall(luaState, count, 1, 0);
if (err) {
AFB_ERROR("LuaCallFunc Fail calling %s error=%s", func, lua_tostring(luaState,-1));
- goto OnErrorExit;
+ return -1;
}
// return LUA script value
int rc= (int)lua_tointeger(luaState, -1);
return rc;
-
- OnErrorExit:
- return -1;
}
-
// Execute LUA code from received API request
static void LuaDoAction (LuaDoActionT action, afb_req request) {
@@ -727,7 +723,6 @@ static void LuaDoAction (LuaDoActionT action, afb_req request) {
char *filename; char*fullpath;
char luaScriptPath[CONTROL_MAXPATH_LEN];
int index;
- BPaths BindingPaths = GetBindingDirsPath();
// scan luascript search path once
static json_object *luaScriptPathJ =NULL;
@@ -743,10 +738,10 @@ static void LuaDoAction (LuaDoActionT action, afb_req request) {
// search for filename=script in CONTROL_LUA_PATH
if (!luaScriptPathJ) {
- strncpy(luaScriptPath,CONTROL_DOSCRIPT_PRE, sizeof(luaScriptPath));
+ strncpy(luaScriptPath, CONTROL_DOSCRIPT_PRE, sizeof(luaScriptPath));
strncat(luaScriptPath,"-", sizeof(luaScriptPath)-strlen(luaScriptPath)-1);
strncat(luaScriptPath,target, sizeof(luaScriptPath)-strlen(luaScriptPath)-1);
- luaScriptPathJ= ScanForConfig(BindingPaths.etcdir, CTL_SCAN_RECURSIVE,luaScriptPath,".lua");
+ luaScriptPathJ= ScanForConfig(strncat(GetBindingDirPath(), "/etc", sizeof(GetBindingDirPath()) - strlen(GetBindingDirPath()) - 1), CTL_SCAN_RECURSIVE,luaScriptPath,".lua");
}
for (index=0; index < json_object_array_length(luaScriptPathJ); index++) {
json_object *entryJ=json_object_array_get_idx(luaScriptPathJ, index);
@@ -841,7 +836,7 @@ static int LuaTimerClear (lua_State* luaState) {
// Get Timer Handle
LuaAfbContextT *afbContext= LuaCtxCheck(luaState, LUA_FIST_ARG);
- if (!afbContext) goto OnErrorExit;
+ if (!afbContext) return -1;
// retrieve useful information opaque handle
TimerHandleT *timerHandle = (TimerHandleT*)afbContext->handle;
@@ -850,15 +845,12 @@ static int LuaTimerClear (lua_State* luaState) {
TimerEvtStop(timerHandle);
return 0; //happy end
-
-OnErrorExit:
- return 1;
}
static int LuaTimerGet (lua_State* luaState) {
// Get Timer Handle
LuaAfbContextT *afbContext= LuaCtxCheck(luaState, LUA_FIST_ARG);
- if (!afbContext) goto OnErrorExit;
+ if (!afbContext) return 0;
// retrieve useful information opaque handle
TimerHandleT *timerHandle = (TimerHandleT*)afbContext->handle;
@@ -876,9 +868,6 @@ static int LuaTimerGet (lua_State* luaState) {
json_object_put(responseJ);
return count; // return argument
-
-OnErrorExit:
- return 0;
}
// Timer Callback
@@ -894,7 +883,7 @@ static int LuaTimerSetCB (void *handle) {
// Push timer handle
LuaAfbContextT *afbContext= LuaCtxPush(luaState, NULL_AFBREQ, contextCB->handle, timerHandle->label);
- if (!afbContext) goto OnErrorExit;
+ if (!afbContext) return 1;
count=1;
// Push user Context
@@ -903,7 +892,7 @@ static int LuaTimerSetCB (void *handle) {
int err=lua_pcall(luaState, count, LUA_MULTRET, 0);
if (err) {
AFB_ERROR ("LUA-TIMER-CB:FAIL response=%s err=%s", json_object_get_string(contextCB->context), lua_tostring(luaState,-1));
- goto OnErrorExit;
+ return 1;
}
// get return parameter
@@ -916,9 +905,6 @@ static int LuaTimerSetCB (void *handle) {
LuaCtxFree(afbContext);
}
return 0; // By default we are happy
-
- OnErrorExit:
- return 1; // stop timer
}
static int LuaTimerSet(lua_State* luaState) {
@@ -997,12 +983,12 @@ int LuaLibInit () {
// search for default policy config file
char fullprefix[CONTROL_MAXPATH_LEN];
- strncpy (fullprefix, CONTROL_CONFIG_PRE "-", sizeof(fullprefix));
+ strncpy (fullprefix, CONTROL_CONFIG_PRE, sizeof(fullprefix));
strncat (fullprefix, GetBinderName(), sizeof(fullprefix)-strlen(fullprefix)-1);
strncat (fullprefix, "-", sizeof(fullprefix)-strlen(fullprefix)-1);
const char *dirList= getenv("CONTROL_LUA_PATH");
- if (!dirList) dirList= GetBindingDirsPath().etcdir;
+ if (!dirList) dirList= strncat(GetBindingDirPath(), "/etc", sizeof(GetBindingDirPath()) - strlen(GetBindingDirPath()) - 1);
json_object *luaScriptPathJ = ScanForConfig(dirList , CTL_SCAN_RECURSIVE, fullprefix, "lua");
@@ -1026,7 +1012,7 @@ int LuaLibInit () {
luaDefaultEvt->event = afb_daemon_make_event(CONTROL_LUA_EVENT);
if (!afb_event_is_valid(luaDefaultEvt->event)) {
AFB_ERROR ("POLCTL_INIT: Cannot register lua-events=%s ", CONTROL_LUA_EVENT);
- goto OnErrorExit;;
+ goto OnErrorExit;
}
// load+exec any file found in LUA search path
diff --git a/signal-composer-binding/ctl-lua.h b/signal-composer-binding/ctl-lua.h
index df2379f..c5f472d 100644
--- a/signal-composer-binding/ctl-lua.h
+++ b/signal-composer-binding/ctl-lua.h
@@ -12,60 +12,6 @@
#include "signal-composer-binding.hpp"
-#ifndef CONTROL_DOSCRIPT_PRE
-#define CONTROL_DOSCRIPT_PRE "doscript"
-#endif
-
-#ifndef CONTROL_CONFIG_PRE
-#define CONTROL_CONFIG_PRE "onload"
-#endif
-
-#ifndef CONTROL_LUA_EVENT
-#define CONTROL_LUA_EVENT "luaevt"
-#endif
-
-typedef int (*timerCallbackT)(void *context);
-
-typedef struct TimerHandleS {
- int count;
- int delay;
- const char*label;
- void *context;
- timerCallbackT callback;
- sd_event_source *evtSource;
-} TimerHandleT;
-
-int TimerEvtInit (void);
-afb_event TimerEvtGet(void);
-void TimerEvtStart(TimerHandleT *timerHandle, timerCallbackT callback, void *context);
-void TimerEvtStop(TimerHandleT *timerHandle);
-
-typedef enum {
- CTL_MODE_NONE=0,
- CTL_MODE_API,
- CTL_MODE_CB,
- CTL_MODE_LUA,
-} CtlRequestModeT;
-
-typedef enum {
- CTL_SOURCE_CLOSE=-1,
- CTL_SOURCE_UNKNOWN=0,
- CTL_SOURCE_ONLOAD=1,
- CTL_SOURCE_OPEN=2,
- CTL_SOURCE_EVENT=3,
-} DispatchSourceT;
-
-typedef struct DispatchActionS{
- const char *info;
- const char* label;
- CtlRequestModeT mode;
- const char* api;
- const char* call;
- json_object *argsJ;
- int timeout;
- int (*actionCB)(DispatchSourceT source, const char*label, json_object *argsJ, json_object *queryJ, void *context);
-} DispatchActionT;
-
typedef enum {
LUA_DOCALL,
LUA_DOSTRING,
@@ -73,3 +19,10 @@ typedef enum {
} LuaDoActionT;
typedef int (*Lua2cFunctionT)(char *funcname, json_object *argsJ, void*context);
+
+typedef int (*Lua2cWrapperT) (lua_State* luaState, char *funcname, Lua2cFunctionT callback);
+
+int LuaLibInit ();
+void LuaL2cNewLib(const char *label, luaL_Reg *l2cFunc, int count);
+int Lua2cWrapper(lua_State* luaState, char *funcname, Lua2cFunctionT callback, void *context);
+int LuaCallFunc (DispatchSourceT source, DispatchActionT *action, json_object *queryJ) ;
diff --git a/signal-composer-binding/signal-composer-apidef.h b/signal-composer-binding/signal-composer-apidef.h
index f986692..a652de5 100644
--- a/signal-composer-binding/signal-composer-apidef.h
+++ b/signal-composer-binding/signal-composer-apidef.h
@@ -4,45 +4,45 @@ static const char _afb_description_v2_signals_composer[] =
"a-3.0/default-schema.json\",\"info\":{\"description\":\"\",\"title\":\"s"
"ignals-composer-service\",\"version\":\"4.0\",\"x-binding-c-generator\":"
"{\"api\":\"signals-composer\",\"version\":2,\"prefix\":\"\",\"postfix\":"
- "\"\",\"start\":null,\"onevent\":\"onEvent\",\"init\":\"init_service\",\""
- "scope\":\"\",\"private\":false}},\"servers\":[{\"url\":\"ws://{host}:{po"
- "rt}/api/monitor\",\"description\":\"Signals composer API connected to lo"
- "w level AGL services\",\"variables\":{\"host\":{\"default\":\"localhost\""
- "},\"port\":{\"default\":\"1234\"}},\"x-afb-events\":[{\"$ref\":\"#/compo"
- "nents/schemas/afb-event\"}]}],\"components\":{\"schemas\":{\"afb-reply\""
- ":{\"$ref\":\"#/components/schemas/afb-reply-v2\"},\"afb-event\":{\"$ref\""
- ":\"#/components/schemas/afb-event-v2\"},\"afb-reply-v2\":{\"title\":\"Ge"
- "neric response.\",\"type\":\"object\",\"required\":[\"jtype\",\"request\""
- "],\"properties\":{\"jtype\":{\"type\":\"string\",\"const\":\"afb-reply\""
- "},\"request\":{\"type\":\"object\",\"required\":[\"status\"],\"propertie"
- "s\":{\"status\":{\"type\":\"string\"},\"info\":{\"type\":\"string\"},\"t"
- "oken\":{\"type\":\"string\"},\"uuid\":{\"type\":\"string\"},\"reqid\":{\""
- "type\":\"string\"}}},\"response\":{\"type\":\"object\"}}},\"afb-event-v2"
- "\":{\"type\":\"object\",\"required\":[\"jtype\",\"event\"],\"properties\""
- ":{\"jtype\":{\"type\":\"string\",\"const\":\"afb-event\"},\"event\":{\"t"
- "ype\":\"string\"},\"data\":{\"type\":\"object\"}}}},\"x-permissions\":{}"
- ",\"responses\":{\"200\":{\"description\":\"A complex object array respon"
- "se\",\"content\":{\"application/json\":{\"schema\":{\"$ref\":\"#/compone"
- "nts/schemas/afb-reply\"}}}}}},\"paths\":{\"/subscribe\":{\"description\""
- ":\"Subscribe to a signal object\",\"parameters\":[{\"in\":\"query\",\"na"
- "me\":\"event\",\"required\":false,\"schema\":{\"type\":\"string\"}}],\"r"
- "esponses\":{\"200\":{\"$ref\":\"#/components/responses/200\"}}},\"/unsub"
- "scribe\":{\"description\":\"Unsubscribe previously suscribed signal obje"
- "cts.\",\"parameters\":[{\"in\":\"query\",\"name\":\"event\",\"required\""
- ":false,\"schema\":{\"type\":\"string\"}}],\"responses\":{\"200\":{\"$ref"
- "\":\"#/components/responses/200\"}}},\"/get\":{\"description\":\"Get inf"
- "ormations about a resource or element\",\"responses\":{\"200\":{\"$ref\""
- ":\"#/components/responses/200\"}}},\"/load\":{\"description\":\"Load con"
- "fig file in directory passed as argument searching for pattern 'sig' in "
- "filename\",\"parameters\":[{\"in\":\"query\",\"name\":\"path\",\"require"
- "d\":true,\"schema\":{\"type\":\"string\"}}],\"responses\":{\"200\":{\"$r"
- "ef\":\"#/components/responses/200\"}}}}}"
+ "\"\",\"start\":null,\"onevent\":\"onEvent\",\"preinit\":\"loadConf\",\"i"
+ "nit\":\"execConf\",\"scope\":\"\",\"private\":false}},\"servers\":[{\"ur"
+ "l\":\"ws://{host}:{port}/api/monitor\",\"description\":\"Signals compose"
+ "r API connected to low level AGL services\",\"variables\":{\"host\":{\"d"
+ "efault\":\"localhost\"},\"port\":{\"default\":\"1234\"}},\"x-afb-events\""
+ ":[{\"$ref\":\"#/components/schemas/afb-event\"}]}],\"components\":{\"sch"
+ "emas\":{\"afb-reply\":{\"$ref\":\"#/components/schemas/afb-reply-v2\"},\""
+ "afb-event\":{\"$ref\":\"#/components/schemas/afb-event-v2\"},\"afb-reply"
+ "-v2\":{\"title\":\"Generic response.\",\"type\":\"object\",\"required\":"
+ "[\"jtype\",\"request\"],\"properties\":{\"jtype\":{\"type\":\"string\",\""
+ "const\":\"afb-reply\"},\"request\":{\"type\":\"object\",\"required\":[\""
+ "status\"],\"properties\":{\"status\":{\"type\":\"string\"},\"info\":{\"t"
+ "ype\":\"string\"},\"token\":{\"type\":\"string\"},\"uuid\":{\"type\":\"s"
+ "tring\"},\"reqid\":{\"type\":\"string\"}}},\"response\":{\"type\":\"obje"
+ "ct\"}}},\"afb-event-v2\":{\"type\":\"object\",\"required\":[\"jtype\",\""
+ "event\"],\"properties\":{\"jtype\":{\"type\":\"string\",\"const\":\"afb-"
+ "event\"},\"event\":{\"type\":\"string\"},\"data\":{\"type\":\"object\"}}"
+ "}},\"x-permissions\":{},\"responses\":{\"200\":{\"description\":\"A comp"
+ "lex object array response\",\"content\":{\"application/json\":{\"schema\""
+ ":{\"$ref\":\"#/components/schemas/afb-reply\"}}}}}},\"paths\":{\"/subscr"
+ "ibe\":{\"description\":\"Subscribe to a signal object\",\"parameters\":["
+ "{\"in\":\"query\",\"name\":\"event\",\"required\":false,\"schema\":{\"ty"
+ "pe\":\"string\"}}],\"responses\":{\"200\":{\"$ref\":\"#/components/respo"
+ "nses/200\"}}},\"/unsubscribe\":{\"description\":\"Unsubscribe previously"
+ " suscribed signal objects.\",\"parameters\":[{\"in\":\"query\",\"name\":"
+ "\"event\",\"required\":false,\"schema\":{\"type\":\"string\"}}],\"respon"
+ "ses\":{\"200\":{\"$ref\":\"#/components/responses/200\"}}},\"/get\":{\"d"
+ "escription\":\"Get informations about a resource or element\",\"response"
+ "s\":{\"200\":{\"$ref\":\"#/components/responses/200\"}}},\"/loadConf\":{"
+ "\"description\":\"Load config file in directory passed as argument searc"
+ "hing for pattern 'sig' in filename\",\"parameters\":[{\"in\":\"query\",\""
+ "name\":\"path\",\"required\":true,\"schema\":{\"type\":\"string\"}}],\"r"
+ "esponses\":{\"200\":{\"$ref\":\"#/components/responses/200\"}}}}}"
;
void subscribe(struct afb_req req);
void unsubscribe(struct afb_req req);
void get(struct afb_req req);
- void load(struct afb_req req);
+ void loadConf(struct afb_req req);
static const struct afb_verb_v2 _afb_verbs_v2_signals_composer[] = {
{
@@ -67,8 +67,8 @@ static const struct afb_verb_v2 _afb_verbs_v2_signals_composer[] = {
.session = AFB_SESSION_NONE_V2
},
{
- .verb = "load",
- .callback = load,
+ .verb = "loadConf",
+ .callback = loadConf,
.auth = NULL,
.info = "Load config file in directory passed as argument searching for pattern 'sig' in filename",
.session = AFB_SESSION_NONE_V2
@@ -87,8 +87,8 @@ const struct afb_binding_v2 afbBindingV2 = {
.specification = _afb_description_v2_signals_composer,
.info = "",
.verbs = _afb_verbs_v2_signals_composer,
- .preinit = NULL,
- .init = init_service,
+ .preinit = loadConf,
+ .init = execConf,
.onevent = onEvent,
.noconcurrency = 0
};
diff --git a/signal-composer-binding/signal-composer-apidef.json b/signal-composer-binding/signal-composer-apidef.json
index 78080f1..7085dc2 100644
--- a/signal-composer-binding/signal-composer-apidef.json
+++ b/signal-composer-binding/signal-composer-apidef.json
@@ -12,7 +12,8 @@
"postfix": "",
"start": null ,
"onevent": "onEvent",
- "init": "init_service",
+ "preinit": "loadConf",
+ "init": "execConf",
"scope": "",
"private": false
}
@@ -130,7 +131,7 @@
"200": {"$ref": "#/components/responses/200"}
}
},
- "/load": {
+ "/loadConf": {
"description": "Load config file in directory passed as argument searching for pattern 'sig' in filename",
"parameters": [
{
diff --git a/signal-composer-binding/signal-composer-binding.cpp b/signal-composer-binding/signal-composer-binding.cpp
index 24b81f6..2ca1dec 100644
--- a/signal-composer-binding/signal-composer-binding.cpp
+++ b/signal-composer-binding/signal-composer-binding.cpp
@@ -15,12 +15,15 @@
* limitations under the License.
*/
+#include <ctl-config.h>
+
#include "signal-composer-binding.hpp"
#include "signal-composer-apidef.h"
#include "wrap-json.h"
#include "signal-composer.hpp"
SignalComposer SigComp;
+static CtlConfigT *ctlConfig=NULL;
/// @brief callback for receiving message from low binding. Treatment itself is made in SigComp class.
void onEvent(const char *event, json_object *object)
@@ -46,7 +49,7 @@ void unsubscribe(afb_req request)
}
/// @brief verb that loads JSON configuration (old SigComp.json file now)
-void load(afb_req request)
+void loadConf(afb_req request)
{
json_object* args = afb_req_json(request);
const char* confd;
@@ -79,12 +82,23 @@ int ticked(sd_event_source *source, uint64_t t, void* data)
return 0;
}
-/// @brief Initialize the binding.
-///
-/// @return Exit code, zero if success.
-int init_service()
+int loadConf()
{
- AFB_DEBUG("SigComp level binding is initializing");
- AFB_NOTICE("SigComp level binding is initialized and running");
- return 0;
+ int errcount=0;
+
+ ctlConfig = CtlConfigLoad();
+
+ #ifdef CONTROL_SUPPORT_LUA
+ errcount += LuaLibInit();
+ #endif
+
+ return errcount;
+}
+
+int execConf()
+{
+ int err = CtlConfigExec();
+
+ AFB_DEBUG ("Signal Composer Control configuration Done errcount=%d", errcount);
+ return errcount;
}
diff --git a/signal-composer-binding/signal-composer-binding.hpp b/signal-composer-binding/signal-composer-binding.hpp
index 3b512f3..7bf1df1 100644
--- a/signal-composer-binding/signal-composer-binding.hpp
+++ b/signal-composer-binding/signal-composer-binding.hpp
@@ -13,6 +13,7 @@ extern "C"
};
#endif
- void onEvent(const char *event, struct json_object *object);
- int init_service();
- int ticked(sd_event_source *source, uint64_t t, void *data);
+void onEvent(const char *event, struct json_object *object);
+int loadConf();
+int execConf();
+int ticked(sd_event_source *source, uint64_t t, void *data);
diff --git a/signal-composer-binding/signal-conf.cpp b/signal-composer-binding/signal-conf.cpp
new file mode 100644
index 0000000..73f2450
--- /dev/null
+++ b/signal-composer-binding/signal-conf.cpp
@@ -0,0 +1,16 @@
+/*
+ * Copyright (C) 2015, 2016 "IoT.bzh"
+ * Author "Romain Forlot" <romain.forlot@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.
+ */
diff --git a/signal-composer-binding/signal-conf.hpp b/signal-composer-binding/signal-conf.hpp
new file mode 100644
index 0000000..3698135
--- /dev/null
+++ b/signal-composer-binding/signal-conf.hpp
@@ -0,0 +1,27 @@
+/*
+ * Copyright (C) 2015, 2016 "IoT.bzh"
+ * Author "Romain Forlot" <romain.forlot@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 <json.hpp>
+
+ class signalConfiguration
+{
+private:
+ nlohmann::json config_;
+
+public:
+ void parseConfig(std::string configPath);
+}
diff --git a/signal-composer-binding/signal.hpp b/signal-composer-binding/signal.hpp
new file mode 100644
index 0000000..94878c9
--- /dev/null
+++ b/signal-composer-binding/signal.hpp
@@ -0,0 +1,46 @@
+/*
+ * Copyright (C) 2015, 2016 "IoT.bzh"
+ * Author "Romain Forlot" <romain.forlot@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
+
+#include <string>
+#include <memory>
+#include <vector>
+
+class Signal;
+
+class Signal
+{
+private:
+ std::string api_;
+ std::string name_;
+ std::vector<> history_;
+ float frequency_;
+ st::string unit_;
+ float min_;
+ float max_;
+ float last_;
+
+ std::vector<std::shared_ptr<Signal>> Observers_;
+
+public:
+ void notify();
+ void infiniteRecursionCheck();
+ void attach();
+ void detach();
+ void notify();
+}