aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRomain Forlot <romain.forlot@iot.bzh>2017-05-11 17:35:58 +0200
committerRomain Forlot <romain.forlot@iot.bzh>2017-05-19 11:36:42 +0200
commitd040702e75b9c49a4192e557ffceb33351dfc12e (patch)
tree06a26676a20c0fbad7e667114650d8136b373757
parentf04542587423e3b26d76c25d1c84acd5dea180b8 (diff)
Implement unsubscribe like subscribe.
New redundant function creation to mutualize code between two operations. Change-Id: I8516c2c6d2f0c1d5cf89244ea23b831530e84a5f Signed-off-by: Romain Forlot <romain.forlot@iot.bzh>
-rw-r--r--CAN-binder/low-can-binding/binding/low-can-cb.cpp49
1 files changed, 31 insertions, 18 deletions
diff --git a/CAN-binder/low-can-binding/binding/low-can-cb.cpp b/CAN-binder/low-can-binding/binding/low-can-cb.cpp
index eaa4fabc..df6795f8 100644
--- a/CAN-binder/low-can-binding/binding/low-can-cb.cpp
+++ b/CAN-binder/low-can-binding/binding/low-can-cb.cpp
@@ -196,6 +196,26 @@ static int subscribe_unsubscribe_signals(struct afb_req request, bool subscribe,
return rets;
}
+static int process_args(struct afb_req request, std::vector<std::string> args, bool subscribe)
+{
+ struct utils::signals_found sf;
+ int ok = 0, total = 0;
+
+ for(const auto& sig: args)
+ {
+ openxc_DynamicField search_key = build_DynamicField(sig);
+ sf = utils::signals_manager_t::instance().find_signals(search_key);
+ total = (int)sf.can_signals.size() + (int)sf.diagnostic_messages.size();
+
+ if (sf.can_signals.empty() && sf.diagnostic_messages.empty())
+ NOTICE(binder_interface, "%s: No signal(s) found for %s.", __FUNCTION__, sig.c_str());
+ else
+ ok = subscribe_unsubscribe_signals(request, subscribe, sf);
+ }
+ NOTICE(binder_interface, "%s: Subscribed/unsubscribe correctly to %d/%d signal(s).", __FUNCTION__, ok, total);
+ return ok;
+}
+
static const std::vector<std::string> parse_args_from_request(struct afb_req request, bool subscribe)
{
int i, n;
@@ -228,32 +248,25 @@ static const std::vector<std::string> parse_args_from_request(struct afb_req req
void subscribe(struct afb_req request)
{
std::vector<std::string> args;
- struct utils::signals_found sf;
- int ok = 0, total = 0;
bool subscribe = true;
args = parse_args_from_request(request, subscribe);
- for(const auto& sig: args)
- {
- openxc_DynamicField search_key = build_DynamicField(sig);
- sf = utils::signals_manager_t::instance().find_signals(search_key);
- total = (int)sf.can_signals.size() + (int)sf.diagnostic_messages.size();
-
- if (sf.can_signals.empty() && sf.diagnostic_messages.empty())
- NOTICE(binder_interface, "%s: No signal(s) found for %s.", __FUNCTION__, sig.c_str());
- else
- ok = subscribe_unsubscribe_signals(request, subscribe, sf);
- }
-
- NOTICE(binder_interface, "%s: Subscribed/unsubscribe correctly to %d/%d signal(s).", __FUNCTION__, ok, total);
- if (ok > 0)
+ if (process_args(request, args, subscribe) > 0)
afb_req_success(request, NULL, NULL);
else
afb_req_fail(request, "error", NULL);
}
- void unsubscribe(struct afb_req request)
+void unsubscribe(struct afb_req request)
{
- parse_args_from_request(request, false);
+ std::vector<std::string> args;
+ bool subscribe = false;
+
+ args = parse_args_from_request(request, subscribe);
+
+ if (process_args(request, args, subscribe) > 0)
+ afb_req_success(request, NULL, NULL);
+ else
+ afb_req_fail(request, "error", NULL);
} \ No newline at end of file