Use dedicated API name for the test
[apps/agl-service-can-low-level.git] / low-can-binding / binding / low-can-cb.cpp
index d8365da..0f97404 100644 (file)
@@ -138,8 +138,8 @@ static int create_event_handle(std::shared_ptr<low_can_subscription_t>& can_subs
        return 0;
 }
 
-/// @brief Will determine if it is needed or not to create the event handle and checks it to be sure that
-/// we got a valid afb_event to get subscribe or unsubscribe. Then launch the subscription or unsubscription
+/// @brief This will determine if an event handle needs to be created and checks if
+/// we got a valid afb_event to get subscribe or unsubscribe. After that launch the subscription or unsubscription
 /// against the application framework using that event handle.
 static int subscribe_unsubscribe_signal(struct afb_req request,
                                                                                bool subscribe,
@@ -166,8 +166,8 @@ static int subscribe_unsubscribe_signal(struct afb_req request,
                ret = create_event_handle(can_subscription, s);
        }
 
-       // Check whether or not the event handler has been correctly created and
-       // make the subscription/unsubscription operation is so.
+       // Checks if the event handler is correctly created, if it is, it
+       // performs the subscription or unsubscription operations.
        if (ret < 0)
                return ret;
        return make_subscription_unsubscription(request, can_subscription, s, subscribe);
@@ -205,11 +205,14 @@ static int subscribe_unsubscribe_diagnostic_messages(struct afb_req request,
                can_subscription = it != s.end() ?
                        it->second :
                        std::make_shared<low_can_subscription_t>(low_can_subscription_t(event_filter));
-               // If the requested diagnostic message isn't supported by the car then unsubcribe it
-               // no matter what we want, worse case will be a fail unsubscription but at least we don't
+               // If the requested diagnostic message is not supported by the car then unsubcribe it
+               // no matter what we want, worst case will be a failed unsubscription but at least we won't
                // poll a PID for nothing.
                if(sig->get_supported() && subscribe)
                {
+                       if (!app.isEngineOn())
+                               AFB_WARNING("signal: Engine is off, %s won't received responses until it's on",  sig->get_name().c_str());
+
                        diag_m.add_recurring_request(diag_req, sig->get_name().c_str(), false, sig->get_decoder(), sig->get_callback(), event_filter.frequency, perm_rec_diag_req);
                        if(can_subscription->create_rx_filter(sig) < 0)
                                {return -1;}
@@ -278,8 +281,8 @@ static int subscribe_unsubscribe_can_signals(struct afb_req request,
 ///
 /// @brief subscribe to all signals in the vector signals
 ///
-/// @param[in] afb_req request : contain original request use to subscribe or unsubscribe
-/// @param[in] subscribe boolean value used to chose between a subscription operation or an unsubscription
+/// @param[in] afb_req request : contains original request use to subscribe or unsubscribe
+/// @param[in] subscribe boolean value, which chooses between a subscription operation or an unsubscription
 /// @param[in] signals -  struct containing vectors with can_signal_t and diagnostic_messages to subscribe
 ///
 /// @return Number of correctly subscribed signal
@@ -426,7 +429,7 @@ static int write_raw_frame(const std::string& bus_name, uint32_t can_id, uint8_t
        cf.can_dlc = can_dlc;
 
        struct json_object *x;
-       int n = json_object_array_length(can_data);
+       size_t n = json_object_array_length(can_data);
        if(n <= 8)
        {
                for (int i = 0 ; i < n ; i++)
@@ -651,6 +654,7 @@ void list(struct afb_req request)
 /// @return Exit code, zero if success.
 int initv2()
 {
+       uint32_t ret = 1;
        can_bus_t& can_bus_manager = application_t::instance().get_can_bus_manager();
 
        can_bus_manager.set_can_devices();
@@ -660,8 +664,29 @@ int initv2()
        /// We pass by default the first CAN bus device to its Initialization.
        /// TODO: be able to choose the CAN bus device that will be use as Diagnostic bus.
        if(application_t::instance().get_diagnostic_manager().initialize())
-               return 0;
+               ret = 0;
+
+       // Add a recurring dignostic message request to get engine speed at all times.
+       openxc_DynamicField search_key = build_DynamicField("diagnostic_messages.engine.speed");
+       struct utils::signals_found sf = utils::signals_manager_t::instance().find_signals(search_key);
+
+       if(sf.can_signals.empty() && sf.diagnostic_messages.size() == 1)
+       {
+               struct afb_req request;
+               request.itf = nullptr;
+               request.closure = nullptr;
+
+               struct event_filter_t event_filter;
+               event_filter.frequency = sf.diagnostic_messages.front()->get_frequency();
 
-       AFB_ERROR("There was something wrong with CAN device Initialization.");
-       return 1;
+               utils::signals_manager_t& sm = utils::signals_manager_t::instance();
+               std::map<int, std::shared_ptr<low_can_subscription_t> >& s = sm.get_subscribed_signals();
+
+               subscribe_unsubscribe_diagnostic_messages(request, true, sf.diagnostic_messages, event_filter, s, true);
+       }
+
+       if(ret)
+               AFB_ERROR("There was something wrong with CAN device Initialization.");
+
+       return ret;
 }