can-bus: process signal rework to subscribe message 61/23461/3
authorArthur Guyader <arthur.guyader@iot.bzh>
Wed, 18 Dec 2019 15:57:22 +0000 (16:57 +0100)
committerRomain Forlot <romain.forlot@iot.bzh>
Thu, 9 Jan 2020 15:25:36 +0000 (16:25 +0100)
This commits allows to subscribe message and have all signals
in one frame.

Change-Id: I95de0e46b30be09a47a04754266cb55650eeec31
Signed-off-by: Arthur Guyader <arthur.guyader@iot.bzh>
Signed-off-by: Romain Forlot <romain.forlot@iot.bzh>
low-can-binding/binding/low-can-cb.cpp
low-can-binding/can/can-bus.cpp

index 4be7261..42e0265 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;
 }
 
 
index f6b5add..40e080f 100644 (file)
@@ -110,17 +110,39 @@ void can_bus_t::process_signals(std::shared_ptr<message_t> message, map_subscrip
        {
                bool send = true;
                // First we have to found which signal_t it is
-               std::shared_ptr<low_can_subscription_t> sig = s[subscription_id];
+               std::shared_ptr<low_can_subscription_t> subscription = s[subscription_id];
+               openxc_SimpleMessage s_message;
 
-               decoded_message = decoder_t::translate_signal(*sig->get_signal(), message, &send);
-               openxc_SimpleMessage s_message = build_SimpleMessage(sig->get_name(), decoded_message);
+               // messages
+               if(subscription->get_message_definition() != nullptr)
+               {
+                       openxc_DynamicField dynamicField_tmp;
+                       json_object *signal_json_tmp;
+                       decoded_message = build_DynamicField_json(json_object_new_array());
+                       for(std::shared_ptr<signal_t> sig : subscription->get_message_definition()->get_signals())
+                       {
+                               signal_json_tmp = json_object_new_object();
+                               dynamicField_tmp = decoder_t::translate_signal(*sig, message, &send);
+                               json_object_object_add(signal_json_tmp,"name", json_object_new_string(sig->get_name().c_str()));
+                               jsonify_DynamicField(dynamicField_tmp,signal_json_tmp);
+                               if(sig != nullptr && sig->get_unit() != "")
+                                       json_object_object_add(signal_json_tmp, "unit", json_object_new_string(sig->get_unit().c_str()));
+                               json_object_array_add(decoded_message.json_value,signal_json_tmp);
+                       }
+               }
+               else // signal
+               {
+                       decoded_message = decoder_t::translate_signal(*subscription->get_signal(), message, &send);
+               }
+
+               s_message = build_SimpleMessage(subscription->get_name(), decoded_message);
                vehicle_message = build_VehicleMessage(s_message, message->get_timestamp());
 
-               if(send && apply_filter(vehicle_message, sig))
+               if(send && apply_filter(vehicle_message, subscription))
                {
                        std::lock_guard<std::mutex> decoded_can_message_lock(decoded_can_message_mutex_);
                        push_new_vehicle_message(subscription_id, vehicle_message);
-                       AFB_DEBUG("%s CAN signals processed.", sig->get_name().c_str());
+                       AFB_DEBUG("%s CAN signals processed.", subscription->get_name().c_str());
                }
        }
 }