get verb: add option id
[apps/agl-service-can-low-level.git] / low-can-binding / binding / low-can-cb.cpp
index 4be7261..5e4c16a 100644 (file)
@@ -364,7 +364,6 @@ static int one_subscribe_unsubscribe_events(afb_req_t request, bool subscribe, c
 
 static int one_subscribe_unsubscribe_id(afb_req_t request, bool subscribe, const uint32_t& id, json_object *args)
 {
-       int ret = 0;
        std::shared_ptr<message_definition_t> message_definition = application_t::instance().get_message_definition(id);
        struct utils::signals_found sf;
 
@@ -374,15 +373,26 @@ static int one_subscribe_unsubscribe_id(afb_req_t request, bool subscribe, const
        if(sf.signals.empty())
        {
                AFB_NOTICE("No signal(s) found for %d.", id);
-               ret = -1;
-       }
-       else
-       {
-               event_filter_t event_filter = generate_filter(args);
-               ret = subscribe_unsubscribe_signals(request, subscribe, sf, event_filter);
+               return -1;
        }
 
-       return ret;
+       event_filter_t event_filter = generate_filter(args);
+       std::shared_ptr<low_can_subscription_t> can_subscription = std::make_shared<low_can_subscription_t>(low_can_subscription_t(event_filter));
+       can_subscription->set_message_definition(message_definition);
+
+       utils::signals_manager_t& sm = utils::signals_manager_t::instance();
+       std::lock_guard<std::mutex> subscribed_signals_lock(sm.get_subscribed_signals_mutex());
+       map_subscription& s = sm.get_subscribed_signals();
+
+       if(can_subscription->create_rx_filter(message_definition) < 0)
+               return -1;
+       if(add_to_event_loop(can_subscription) < 0)
+               return -1;
+
+       if(subscribe_unsubscribe_signal(request, subscribe, can_subscription, s) < 0)
+               return -1;
+
+       return 0;
 }
 
 
@@ -736,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_t> 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;
@@ -753,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:");