diff options
author | Romain Forlot <romain.forlot@iot.bzh> | 2017-11-29 19:32:01 +0100 |
---|---|---|
committer | Romain Forlot <romain.forlot@iot.bzh> | 2017-12-14 11:00:26 +0100 |
commit | 01bf5e6e309c12475e1bf2d58f3b72e64f7a5add (patch) | |
tree | 84467dd9b5b4a7b7cfc71a8e96101514182766b7 | |
parent | 9a2620f02ce35edd6420f77643c7f64f132d62c1 (diff) |
Update and use latest ctl-utilities.
Controller will now handle loading additional files
from section.
Context retrieving change a bit, now we have source context
and plugin context.
Update json unpacking and packing.
Cleaning, format.
Change-Id: Ie7a4721f6b703796a0dac1a236922427f4755dc5
Signed-off-by: Romain Forlot <romain.forlot@iot.bzh>
m--------- | ctl-utilities | 0 | ||||
-rw-r--r-- | plugins/low-can.cpp | 59 | ||||
-rw-r--r-- | signal-composer-binding/signal-composer.cpp | 49 | ||||
-rw-r--r-- | signal-composer-binding/signal-composer.hpp | 1 | ||||
-rw-r--r-- | signal-composer-binding/signal.cpp | 2 | ||||
-rw-r--r-- | signal-composer-binding/source.cpp | 2 |
6 files changed, 44 insertions, 69 deletions
diff --git a/ctl-utilities b/ctl-utilities -Subproject 1f9d09659ef682a2ed9915a4c952272c1c73962 +Subproject 5a591bb47bdc9d6e2eceda127a749b8bdb92b1a diff --git a/plugins/low-can.cpp b/plugins/low-can.cpp index 8d09eda..b8b84eb 100644 --- a/plugins/low-can.cpp +++ b/plugins/low-can.cpp @@ -45,11 +45,15 @@ typedef struct { doorT rear_right; } allDoorsCtxT; -typedef struct { - struct signalCBT* pluginHandle; +struct pluginCtxT { json_object *subscriptionBatch; allDoorsCtxT allDoorsCtx; -} lowCANCtxT; +}; + +struct pluginCtxT pluginCtx = { + json_object_new_array(), + {false, false, false, false, false, false, false, false} +}; void setDoor(doorT* aDoor, const char* eventName, int eventStatus) { @@ -59,9 +63,10 @@ void setDoor(doorT* aDoor, const char* eventName, int eventStatus) } // Call at initialisation time +/* CTLP_ONLOAD(plugin, composerHandle) { - lowCANCtxT *pluginCtx= (lowCANCtxT*)calloc (1, sizeof(lowCANCtxT)); + lowCANCtxT *pluginCtx = (lowCANCtxT*)calloc (1, sizeof(lowCANCtxT)); pluginCtx->pluginHandle = (struct signalCBT*)composerHandle; pluginCtx->subscriptionBatch = json_object_new_array(); @@ -72,9 +77,9 @@ CTLP_ONLOAD(plugin, composerHandle) return (void*)pluginCtx; } +*/ CTLP_CAPI (subscribeToLow, source, argsJ, eventJ) { - lowCANCtxT *pluginCtx = (lowCANCtxT*)source->context; json_object* dependsArrayJ = nullptr, *subscribeArgsJ = nullptr, *subscribeFilterJ = nullptr, *responseJ = nullptr, *filterJ = nullptr; const char *id = nullptr, *event = nullptr, *unit = nullptr; double frequency = 0; @@ -83,7 +88,7 @@ CTLP_CAPI (subscribeToLow, source, argsJ, eventJ) { if(eventJ) { err = wrap_json_unpack(eventJ, "{ss,s?s,s?o,s?s,s?F,s?o !}", - "id", &id, + "uid", &id, "event", &event, "depends", &dependsArrayJ, "unit", &unit, @@ -111,14 +116,14 @@ CTLP_CAPI (subscribeToLow, source, argsJ, eventJ) { AFB_ERROR("Error building subscription query object"); return err; } - json_object_array_add(pluginCtx->subscriptionBatch, subscribeArgsJ); + json_object_array_add(pluginCtx.subscriptionBatch, subscribeArgsJ); } else { - AFB_DEBUG("Calling subscribe with %s", json_object_to_json_string_ext(pluginCtx->subscriptionBatch, JSON_C_TO_STRING_PRETTY)); - err = afb_service_call_sync("low-can", "subscribe", pluginCtx->subscriptionBatch, &responseJ); + AFB_DEBUG("Calling subscribe with %s", json_object_to_json_string_ext(pluginCtx.subscriptionBatch, JSON_C_TO_STRING_PRETTY)); + err = afb_service_call_sync("low-can", "subscribe", pluginCtx.subscriptionBatch, &responseJ); if(err) - {AFB_ERROR("Subscribe to '%s' responseJ:%s", json_object_to_json_string_ext(pluginCtx->subscriptionBatch, JSON_C_TO_STRING_PRETTY), json_object_to_json_string(responseJ));} + {AFB_ERROR("Subscribe to '%s' responseJ:%s", json_object_to_json_string_ext(pluginCtx.subscriptionBatch, JSON_C_TO_STRING_PRETTY), json_object_to_json_string(responseJ));} } return err; @@ -128,7 +133,7 @@ CTLP_CAPI (isOpen, source, argsJ, eventJ) { const char *eventName = nullptr; int eventStatus; uint64_t timestamp; - lowCANCtxT *pluginCtx=(lowCANCtxT*)source->context; + signalCBT *sourceCtx=(signalCBT*)source->context; int err = wrap_json_unpack(eventJ, "{ss,sb,s?F}", "name", &eventName, @@ -142,23 +147,23 @@ CTLP_CAPI (isOpen, source, argsJ, eventJ) { if(strcasestr(eventName, "front_left")) { - pluginCtx->pluginHandle->searchNsetSignalValue(eventName,(uint64_t)timestamp, eventStatus); - setDoor(&pluginCtx->allDoorsCtx.front_left, eventName, eventStatus); + sourceCtx->searchNsetSignalValue(eventName,(uint64_t)timestamp, eventStatus); + setDoor(&pluginCtx.allDoorsCtx.front_left, eventName, eventStatus); } else if(strcasestr(eventName, "front_right")) { - pluginCtx->pluginHandle->searchNsetSignalValue(eventName,(uint64_t)timestamp, eventStatus); - setDoor(&pluginCtx->allDoorsCtx.front_right, eventName, eventStatus); + sourceCtx->searchNsetSignalValue(eventName,(uint64_t)timestamp, eventStatus); + setDoor(&pluginCtx.allDoorsCtx.front_right, eventName, eventStatus); } else if(strcasestr(eventName, "rear_left")) { - pluginCtx->pluginHandle->searchNsetSignalValue(eventName,(uint64_t)timestamp, eventStatus); - setDoor(&pluginCtx->allDoorsCtx.rear_left, eventName, eventStatus); + sourceCtx->searchNsetSignalValue(eventName,(uint64_t)timestamp, eventStatus); + setDoor(&pluginCtx.allDoorsCtx.rear_left, eventName, eventStatus); } else if(strcasestr(eventName, "rear_right")) { - pluginCtx->pluginHandle->searchNsetSignalValue(eventName,(uint64_t)timestamp, eventStatus); - setDoor(&pluginCtx->allDoorsCtx.rear_right, eventName, eventStatus); + sourceCtx->searchNsetSignalValue(eventName,(uint64_t)timestamp, eventStatus); + setDoor(&pluginCtx.allDoorsCtx.rear_right, eventName, eventStatus); } else { @@ -170,14 +175,14 @@ CTLP_CAPI (isOpen, source, argsJ, eventJ) { source->uid, json_object_to_json_string(argsJ), json_object_to_json_string(eventJ), - pluginCtx->allDoorsCtx.front_left.door ? "true":"false", - pluginCtx->allDoorsCtx.front_left.window ? "true":"false", - pluginCtx->allDoorsCtx.front_right.door ? "true":"false", - pluginCtx->allDoorsCtx.front_right.window ? "true":"false", - pluginCtx->allDoorsCtx.rear_left.door ? "true":"false", - pluginCtx->allDoorsCtx.rear_left.window ? "true":"false", - pluginCtx->allDoorsCtx.rear_right.door ? "true":"false", - pluginCtx->allDoorsCtx.rear_right.window ? "true":"false" + pluginCtx.allDoorsCtx.front_left.door ? "true":"false", + pluginCtx.allDoorsCtx.front_left.window ? "true":"false", + pluginCtx.allDoorsCtx.front_right.door ? "true":"false", + pluginCtx.allDoorsCtx.front_right.window ? "true":"false", + pluginCtx.allDoorsCtx.rear_left.door ? "true":"false", + pluginCtx.allDoorsCtx.rear_left.window ? "true":"false", + pluginCtx.allDoorsCtx.rear_right.door ? "true":"false", + pluginCtx.allDoorsCtx.rear_right.window ? "true":"false" ); return 0; diff --git a/signal-composer-binding/signal-composer.cpp b/signal-composer-binding/signal-composer.cpp index e46c57f..b8d5504 100644 --- a/signal-composer-binding/signal-composer.cpp +++ b/signal-composer-binding/signal-composer.cpp @@ -96,7 +96,7 @@ CtlActionT* Composer::convert2Action(const std::string& name, json_object* actio json_object *functionArgsJ = nullptr, *ctlActionJ = nullptr; char *function; const char *plugin; - CtlActionT *ctlAction = nullptr; + CtlActionT *ctlAction = new CtlActionT; if(actionJ && !wrap_json_unpack(actionJ, "{ss,s?s,s?o !}", "function", &function, @@ -173,25 +173,6 @@ CtlActionT* Composer::convert2Action(const std::string& name, json_object* actio return ctlAction; } -void Composer::loadAdditionnalFiles(json_object* filesJ) -{ - std::vector<std::string> files; - if(filesJ) - { - wrap_json_optarray_for_all(filesJ, extractString, (void*)&files); - if(! files.empty()) - { - for(const auto& oneFile: files) - { - std::string filepath = CtlConfigSearch(nullptr, CONTROL_CONFIG_PATH, oneFile.c_str()); - json_object* oneFileJ = json_object_from_file(filepath.c_str()); - if(oneFileJ) - {loadSourcesAPI(nullptr, nullptr, oneFileJ);} - } - } - } -} - /// @brief Add the builtin plugin in the default plugins section definition /// /// @param[in] section - Control Section structure @@ -232,24 +213,22 @@ int Composer::loadOneSourceAPI(json_object* sourceJ) { json_object *initJ = nullptr, *getSignalsJ = nullptr, - *onReceivedJ = nullptr, - *filesJ = nullptr; + *onReceivedJ = nullptr; CtlActionT *initCtl = nullptr, *getSignalsCtl = nullptr, *onReceivedCtl = nullptr; - const char *api, *info; + const char *uid, *api, *info; int retention = 0; int err = wrap_json_unpack(sourceJ, "{ss,s?s,s?o,s?o,s?o,s?i,s?o !}", + "uid", &uid, "api", &api, "info", &info, "init", &initJ, "getSignals", &getSignalsJ, // Signals field to make signals conf by sources "onReceived", &onReceivedJ, - "retention", &retention, - // External files to load where lies others sources obj - "files", &filesJ); + "retention", &retention); if (err) { AFB_ERROR("Missing something api|[info]|[init]|[getSignals] in %s", json_object_get_string(sourceJ)); @@ -277,9 +256,6 @@ int Composer::loadOneSourceAPI(json_object* sourceJ) onReceivedCtl = onReceivedJ ? convert2Action("onReceived", onReceivedJ) : nullptr; sourcesListV_.push_back(std::make_shared<SourceAPI>(api, info, initCtl, getSignalsCtl, onReceivedCtl, retention)); - - loadAdditionnalFiles(filesJ); - return err; } @@ -290,14 +266,14 @@ int Composer::loadSourcesAPI(AFB_ApiT apihandle, CtlSectionT* section, json_obje if(sourcesJ) { - AFB_ApiNotice("Sources JSON: %s", json_object_to_json_string(sourcesJ)); json_object *sigCompJ = nullptr; // add the signal composer itself as source wrap_json_pack(&sigCompJ, "{ss,ss}", "api", afbBindingV2.api, "info", "Api on behalf the virtual signals are sent"); - json_object_array_add(sourcesJ, sigCompJ); + if(json_object_is_type(sourcesJ, json_type_array)) + json_object_array_add(sourcesJ, sigCompJ); if (json_object_get_type(sourcesJ) == json_type_array) { @@ -326,8 +302,7 @@ int Composer::loadOneSignal(json_object* signalJ) { json_object *onReceivedJ = nullptr, *dependsJ = nullptr, - *getSignalsArgs = nullptr, - *filesJ = nullptr; + *getSignalsArgs = nullptr; CtlActionT* onReceivedCtl; const char *id = nullptr, *event = nullptr, @@ -339,16 +314,14 @@ int Composer::loadOneSignal(json_object* signalJ) std::shared_ptr<SourceAPI> src = nullptr; int err = wrap_json_unpack(signalJ, "{ss,s?s,s?o,s?o,s?F,s?s,s?F,s?o !}", - "id", &id, + "uid", &id, "event", &event, "depends", &dependsJ, "getSignalsArgs", &getSignalsArgs, "retention", &retention, "unit", &unit, "frequency", &frequency, - "onReceived", &onReceivedJ, - // External files to load where lies others sources obj - "files", &filesJ); + "onReceived", &onReceivedJ); if (err) { AFB_ERROR("Missing something id|[event|depends]|[getSignalsArgs]|[retention]|[unit]|[frequency]|[onReceived] in %s", json_object_get_string(signalJ)); @@ -436,8 +409,6 @@ int Composer::loadOneSignal(json_object* signalJ) else {err = -1;} - loadAdditionnalFiles(filesJ); - return err; } diff --git a/signal-composer-binding/signal-composer.hpp b/signal-composer-binding/signal-composer.hpp index cd9c28f..2782288 100644 --- a/signal-composer-binding/signal-composer.hpp +++ b/signal-composer-binding/signal-composer.hpp @@ -33,7 +33,6 @@ private: ~Composer(); CtlActionT* convert2Action(const std::string& name, json_object* action); - void loadAdditionnalFiles(json_object* filesJ); static int pluginsLoad(AFB_ApiT apiHandle, CtlSectionT *section, json_object *pluginsJ); int loadOneSourceAPI(json_object* sourcesJ); diff --git a/signal-composer-binding/signal.cpp b/signal-composer-binding/signal.cpp index e099c43..2af440c 100644 --- a/signal-composer-binding/signal.cpp +++ b/signal-composer-binding/signal.cpp @@ -123,7 +123,7 @@ json_object* Signal::toJSON() const } wrap_json_pack(&queryJ, "{ss,so*}", - "id", id_.c_str(), + "uid", id_.c_str(), "getSignalsArgs", getSignalsArgs_); if (!event_.empty()) {json_object_object_add(queryJ, "event", json_object_new_string(event_.c_str()));} diff --git a/signal-composer-binding/source.cpp b/signal-composer-binding/source.cpp index 3e8153a..9e10e65 100644 --- a/signal-composer-binding/source.cpp +++ b/signal-composer-binding/source.cpp @@ -101,7 +101,7 @@ void SourceAPI::makeSubscription() { CtlSourceT source; source.uid = api_.c_str(); - source.api = nullptr; // We use binding v2, no dynamic API. + source.api = nullptr; // We use binding v2, no dynamic API. source.request = {nullptr, nullptr}; for(auto& sig: signalsMap_) |