From 118c1d1edead3dabf0867125027281ccb776b9a3 Mon Sep 17 00:00:00 2001 From: Romain Forlot Date: Sat, 22 Apr 2017 17:39:27 +0200 Subject: Rework subscription and unsubscriptions operations. More readable. Change-Id: I53101eed16b1faf1e5014826d91e4d64665f2f9f Signed-off-by: Romain Forlot --- CAN-binder/low-can-binding/low-can-binding.cpp | 92 ++++++++++++++------------ 1 file changed, 50 insertions(+), 42 deletions(-) (limited to 'CAN-binder') diff --git a/CAN-binder/low-can-binding/low-can-binding.cpp b/CAN-binder/low-can-binding/low-can-binding.cpp index 0244beb..d6e1944 100644 --- a/CAN-binder/low-can-binding/low-can-binding.cpp +++ b/CAN-binder/low-can-binding/low-can-binding.cpp @@ -84,6 +84,9 @@ static int create_event_handle(const std::string& sig_name, std::map parse_signals_from_request(struct afb_req request, bool subscribe) { - int ok, i, n; + int i, n; + std::vector ret; struct json_object *args, *a, *x; - /* makes the subscription/unsubscription */ + /* retrieve signals to subscribe */ args = afb_req_json(request); - if (args == NULL || !json_object_object_get_ex(args, "event", &a)) { - ok = subscribe_unsubscribe_name(request, subscribe, "*"); - } else if (json_object_get_type(a) != json_type_array) { - ok = subscribe_unsubscribe_name(request, subscribe, json_object_get_string(a)); - } else { + if (args == NULL || !json_object_object_get_ex(args, "event", &a)) + { + ret.push_back("*"); + } + else if (json_object_get_type(a) != json_type_array) + { + ret.push_back(json_object_get_string(a)); + } + else + { n = json_object_array_length(a); - ok = 0; - for (i = 0 ; i < n ; i++) { + for (i = 0 ; i < n ; i++) + { x = json_object_array_get_idx(a, i); - if (subscribe_unsubscribe_name(request, subscribe, json_object_get_string(x))) - ok++; + ret.push_back(json_object_get_string(x)); } - ok = (ok == n); } - /* send the report */ - if (ok) - afb_req_success(request, NULL, NULL); - else - afb_req_fail(request, "error", NULL); + return ret; } extern "C" { static void subscribe(struct afb_req request) { - subscribe_unsubscribe(request, true); + std::vector signals; + struct utils::signals_found sf; + int ok = 0, total = 0; + + signals = parse_signals_from_request(request, true); + + for(const auto& sig: signals) + { + 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) + afb_req_success(request, NULL, NULL); + else + afb_req_fail(request, "error", NULL); } static void unsubscribe(struct afb_req request) { - subscribe_unsubscribe(request, false); + parse_signals_from_request(request, false); } static const struct afb_verb_desc_v1 verbs[]= -- cgit 1.2.3-korg