diff options
author | Romain Forlot <romain.forlot@iot.bzh> | 2017-12-05 16:51:25 +0100 |
---|---|---|
committer | Romain Forlot <romain.forlot@iot.bzh> | 2017-12-14 11:00:49 +0100 |
commit | fb487caec38c0da7a43bab850af6aa79b07befde (patch) | |
tree | da3c46e3e8dcd4627fee54f784b391b284d373b5 | |
parent | 2c3a3ca76a57efa1cd825d140fb26fdec3482ee8 (diff) |
Hold all contexts (plugin & source) in signalCtx_
Keeping persistence between call data are kept in that member
also subscription Action now use getSignals_ context and no more
the signal one, this is more accurate and simples
Change-Id: Idd7c56ba30f1daa9eaf9b99a7261d58189ef0bb2
Signed-off-by: Romain Forlot <romain.forlot@iot.bzh>
-rw-r--r-- | plugins/low-can.cpp | 74 | ||||
-rw-r--r-- | signal-composer-binding/signal-composer.cpp | 1 | ||||
-rw-r--r-- | signal-composer-binding/signal.cpp | 41 | ||||
-rw-r--r-- | signal-composer-binding/signal.hpp | 2 | ||||
-rw-r--r-- | signal-composer-binding/source.cpp | 6 |
5 files changed, 77 insertions, 47 deletions
diff --git a/plugins/low-can.cpp b/plugins/low-can.cpp index b8b84eb..57c389e 100644 --- a/plugins/low-can.cpp +++ b/plugins/low-can.cpp @@ -50,11 +50,6 @@ struct pluginCtxT { allDoorsCtxT allDoorsCtx; }; -struct pluginCtxT pluginCtx = { - json_object_new_array(), - {false, false, false, false, false, false, false, false} -}; - void setDoor(doorT* aDoor, const char* eventName, int eventStatus) { if(strcasestr(eventName, "door")) {aDoor->door = eventStatus;} @@ -63,21 +58,17 @@ void setDoor(doorT* aDoor, const char* eventName, int eventStatus) } // Call at initialisation time -/* CTLP_ONLOAD(plugin, composerHandle) { - lowCANCtxT *pluginCtx = (lowCANCtxT*)calloc (1, sizeof(lowCANCtxT)); - - pluginCtx->pluginHandle = (struct signalCBT*)composerHandle; + struct pluginCtxT* pluginCtx = new struct pluginCtxT; pluginCtx->subscriptionBatch = json_object_new_array(); + memset(&pluginCtx->allDoorsCtx, 0, sizeof(allDoorsCtxT)); - AFB_NOTICE ("Low-can plugin: label='%s' info='%s'", - plugin->uid, - plugin->info); + struct signalCBT* handle = (struct signalCBT*)composerHandle; + handle->pluginCtx = pluginCtx; - return (void*)pluginCtx; + return (void*)handle; } -*/ CTLP_CAPI (subscribeToLow, source, argsJ, eventJ) { json_object* dependsArrayJ = nullptr, *subscribeArgsJ = nullptr, *subscribeFilterJ = nullptr, *responseJ = nullptr, *filterJ = nullptr; @@ -85,6 +76,9 @@ CTLP_CAPI (subscribeToLow, source, argsJ, eventJ) { double frequency = 0; int err = 0; + struct signalCBT* context = reinterpret_cast<struct signalCBT*>(source->context); + struct pluginCtxT* pluginCtx = reinterpret_cast<struct pluginCtxT*>(context->pluginCtx); + if(eventJ) { err = wrap_json_unpack(eventJ, "{ss,s?s,s?o,s?s,s?F,s?o !}", @@ -104,7 +98,10 @@ CTLP_CAPI (subscribeToLow, source, argsJ, eventJ) { if(frequency > 0 && !filterJ) {wrap_json_pack(&subscribeFilterJ, "{sf}", "frequency", frequency);} else - {subscribeFilterJ = filterJ;} + { + json_object_get(filterJ); + subscribeFilterJ = filterJ; + } std::string eventStr = std::string(event); std::string lowEvent = eventStr.substr(eventStr.find("/")+1); @@ -116,14 +113,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; @@ -133,7 +130,8 @@ CTLP_CAPI (isOpen, source, argsJ, eventJ) { const char *eventName = nullptr; int eventStatus; uint64_t timestamp; - signalCBT *sourceCtx=(signalCBT*)source->context; + struct signalCBT* context = reinterpret_cast<struct signalCBT*>(source->context); + struct pluginCtxT* pluginCtx = reinterpret_cast<struct pluginCtxT*>(context->pluginCtx); int err = wrap_json_unpack(eventJ, "{ss,sb,s?F}", "name", &eventName, @@ -147,23 +145,23 @@ CTLP_CAPI (isOpen, source, argsJ, eventJ) { if(strcasestr(eventName, "front_left")) { - sourceCtx->searchNsetSignalValue(eventName,(uint64_t)timestamp, eventStatus); - setDoor(&pluginCtx.allDoorsCtx.front_left, eventName, eventStatus); + context->searchNsetSignalValue(eventName,(uint64_t)timestamp, eventStatus); + setDoor(&pluginCtx->allDoorsCtx.front_left, eventName, eventStatus); } else if(strcasestr(eventName, "front_right")) { - sourceCtx->searchNsetSignalValue(eventName,(uint64_t)timestamp, eventStatus); - setDoor(&pluginCtx.allDoorsCtx.front_right, eventName, eventStatus); + context->searchNsetSignalValue(eventName,(uint64_t)timestamp, eventStatus); + setDoor(&pluginCtx->allDoorsCtx.front_right, eventName, eventStatus); } else if(strcasestr(eventName, "rear_left")) { - sourceCtx->searchNsetSignalValue(eventName,(uint64_t)timestamp, eventStatus); - setDoor(&pluginCtx.allDoorsCtx.rear_left, eventName, eventStatus); + context->searchNsetSignalValue(eventName,(uint64_t)timestamp, eventStatus); + setDoor(&pluginCtx->allDoorsCtx.rear_left, eventName, eventStatus); } else if(strcasestr(eventName, "rear_right")) { - sourceCtx->searchNsetSignalValue(eventName,(uint64_t)timestamp, eventStatus); - setDoor(&pluginCtx.allDoorsCtx.rear_right, eventName, eventStatus); + context->searchNsetSignalValue(eventName,(uint64_t)timestamp, eventStatus); + setDoor(&pluginCtx->allDoorsCtx.rear_right, eventName, eventStatus); } else { @@ -173,16 +171,16 @@ CTLP_CAPI (isOpen, source, argsJ, eventJ) { AFB_DEBUG("This is the situation: source:%s, args:%s, event:%s,\n fld: %s, flw: %s, frd: %s, frw: %s, rld: %s, rlw: %s, rrd: %s, rrw: %s", 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" + argsJ ? json_object_to_json_string(argsJ):"", + eventJ ? 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" ); return 0; diff --git a/signal-composer-binding/signal-composer.cpp b/signal-composer-binding/signal-composer.cpp index dedd4b3..3ad2d4e 100644 --- a/signal-composer-binding/signal-composer.cpp +++ b/signal-composer-binding/signal-composer.cpp @@ -58,6 +58,7 @@ static struct signalCBT pluginHandle = { .searchNsetSignalValue = searchNsetSignalValueHandle, .setSignalValue = setSignalValueHandle, .aSignal = nullptr, + .pluginCtx = nullptr, }; CtlSectionT Composer::ctlSections_[] = { diff --git a/signal-composer-binding/signal.cpp b/signal-composer-binding/signal.cpp index e7156dc..c09e059 100644 --- a/signal-composer-binding/signal.cpp +++ b/signal-composer-binding/signal.cpp @@ -34,6 +34,7 @@ Signal::Signal() unit_(""), onReceived_(nullptr), getSignalsArgs_(nullptr), + signalCtx_(new struct signalCBT), subscribed_(false) {} @@ -48,6 +49,7 @@ Signal::Signal(const std::string& id, const std::string& event, std::vector<std: unit_(unit), onReceived_(onReceived), getSignalsArgs_(getSignalsArgs), + signalCtx_(new struct signalCBT), subscribed_(false) {} @@ -67,11 +69,13 @@ Signal::Signal(const std::string& id, unit_(unit), onReceived_(onReceived), getSignalsArgs_(), + signalCtx_(new struct signalCBT), subscribed_(false) {} Signal::~Signal() { + delete(signalCtx_); delete(onReceived_); } @@ -126,9 +130,16 @@ json_object* Signal::toJSON() const "uid", id_.c_str(), "getSignalsArgs", getSignalsArgs_); - if (!event_.empty()) {json_object_object_add(queryJ, "event", json_object_new_string(event_.c_str()));} - if (json_object_array_length(nameArrayJ)) {json_object_object_add(queryJ, "depends", nameArrayJ);} + if (!event_.empty()) + {json_object_object_add(queryJ, "event", json_object_new_string(event_.c_str()));} + + if (json_object_array_length(nameArrayJ)) + {json_object_object_add(queryJ, "depends", nameArrayJ);} + else + {json_object_put(nameArrayJ);} + if (!unit_.empty()) {json_object_object_add(queryJ, "unit", json_object_new_string(unit_.c_str()));} + if (frequency_) {json_object_object_add(queryJ, "frequency", json_object_new_double(frequency_));} if(timestamp_) {json_object_object_add(queryJ, "timestamp", json_object_new_int64(timestamp_));} @@ -140,15 +151,31 @@ json_object* Signal::toJSON() const return queryJ; } +/// @brief Initialize signal context if not already done and return it. +/// Signal context is a handle to be use by plugins then they can call +/// some signal object method setting signal values. +/// Also if plugin set a context it retrieve it and initiaze the pluginCtx +/// member then plugin can find a persistent memory area where to hold its +/// value. +/// +/// @return a pointer to the signalCtx_ member initialized. struct signalCBT* Signal::get_context() { - struct signalCBT* ctx = new struct signalCBT; + if(!signalCtx_->aSignal || + !signalCtx_->searchNsetSignalValue || + !signalCtx_->setSignalValue) + { + signalCtx_->searchNsetSignalValue = searchNsetSignalValueHandle; + signalCtx_->setSignalValue = setSignalValueHandle; + + signalCtx_->aSignal = (void*)this; - ctx->searchNsetSignalValue = searchNsetSignalValueHandle; - ctx->setSignalValue = setSignalValueHandle; + signalCtx_->pluginCtx = onReceived_ && onReceived_->type == CTL_TYPE_CB ? + onReceived_->exec.cb.plugin->context: + nullptr; + } - ctx->aSignal = (void*)this; - return ctx; + return signalCtx_; } /// @brief Set Signal timestamp and value property when an incoming diff --git a/signal-composer-binding/signal.hpp b/signal-composer-binding/signal.hpp index 97ab4c7..0fbb0a6 100644 --- a/signal-composer-binding/signal.hpp +++ b/signal-composer-binding/signal.hpp @@ -73,6 +73,7 @@ private: std::string unit_; CtlActionT* onReceived_; json_object* getSignalsArgs_; + struct signalCBT* signalCtx_; public: bool subscribed_; ///< subscribed_ - boolean value telling if yes or no the signal has been subcribed to the low level binding. @@ -114,4 +115,5 @@ struct signalCBT void (*searchNsetSignalValue)(const char* aName, uint64_t timestamp, struct signalValue value); void (*setSignalValue)(void* aSignal, uint64_t timestamp, struct signalValue value); void* aSignal; + void* pluginCtx; }; diff --git a/signal-composer-binding/source.cpp b/signal-composer-binding/source.cpp index 6ad9511..3180513 100644 --- a/signal-composer-binding/source.cpp +++ b/signal-composer-binding/source.cpp @@ -113,11 +113,13 @@ void SourceAPI::makeSubscription() break; } source.uid = sig.first.c_str(); - source.context = (void*)sig.second->get_context(); + source.context = getSignals_->type == CTL_TYPE_CB ? + getSignals_->exec.cb.plugin->context: + nullptr; ActionExecOne(&source, getSignals_, signalJ); // Considerate signal subscribed no matter what sig.second->subscribed_ = true; - delete((struct signalCBT*)source.context); + json_object_put(signalJ); } source.uid = ""; ActionExecOne(&source, getSignals_, nullptr); |