From: Clément Bénier Date: Mon, 23 Dec 2019 10:31:45 +0000 (+0100) Subject: get verb: add option id X-Git-Tag: 8.99.5~4 X-Git-Url: https://gerrit.automotivelinux.org/gerrit/gitweb?a=commitdiff_plain;h=refs%2Fchanges%2F72%2F23472%2F2;p=apps%2Fagl-service-can-low-level.git get verb: add option id can be called with json value: - a string matching can id - an array matching can ids Change-Id: Ia38728e065348e265613643fc934eb985564b722 Signed-off-by: Clément Bénier Signed-off-by: Romain Forlot --- diff --git a/low-can-binding/binding/low-can-cb.cpp b/low-can-binding/binding/low-can-cb.cpp index 42e02653..5e4c16a5 100644 --- a/low-can-binding/binding/low-can-cb.cpp +++ b/low-can-binding/binding/low-can-cb.cpp @@ -746,6 +746,36 @@ static struct json_object *get_signals_value(const std::string& name) return ans; } + +static struct json_object *get_id_value(const uint32_t& id) +{ + std::shared_ptr message_definition = application_t::instance().get_message_definition(id); + struct utils::signals_found sf; + struct json_object *ans = nullptr; + + if(message_definition) + sf.signals = list_ptr_signal_t(message_definition->get_signals().begin(), message_definition->get_signals().end()); + + if(sf.signals.empty()) + { + AFB_WARNING("no signal(s) found for %d.", id); + return NULL; + } + + ans = json_object_new_object(); + struct json_object *jsignals = json_object_new_array(); + json_object_object_add(ans, "signals", jsignals); + for(const auto& sig: sf.signals) + { + struct json_object *jobj = json_object_new_object(); + json_object_object_add(jobj, "name", json_object_new_string(sig->get_name().c_str())); + json_object_object_add(jobj, "value", json_object_new_double(sig->get_last_value())); + json_object_array_add(jsignals, jobj); + } + + return ans; +} + void get(afb_req_t request) { int rc = 0; @@ -763,6 +793,37 @@ void get(afb_req_t request) if (!ans) rc = -1; } + else if (args != nullptr && + (json_object_object_get_ex(args, "id", &json_name))) + { + if (json_object_get_type(json_name) == json_type_string) // id is set before and check if it's an array + { + ans = get_id_value(json_object_get_int(json_name)); + } + else if(json_object_get_type(json_name) == json_type_array) + { + ans = json_object_new_array(); + for (int i = 0 ; i < json_object_array_length(json_name); i++) + { + json_object *sub_ans = nullptr; + json_object *x = json_object_array_get_idx(json_name, i); + sub_ans = get_id_value(json_object_get_int(x)); + if(!sub_ans) + rc = -1; + else { + struct json_object *jobj = json_object_new_object(); + struct json_object *jid = json_object_new_string(json_object_get_string(x)); + json_object_object_add(jobj, "id", jid); + json_object_object_add(jobj, "data", sub_ans); + json_object_array_add(ans, jobj); + } + } + } + else + rc = -1; + if (!ans) + rc = -1; + } else { AFB_ERROR("Request argument malformed. Please use the following syntax:");