Wrong error handling when fire diagnostic request.
[apps/agl-service-can-low-level.git] / src / low-can-binding.cpp
index 7a19c62..2b04fa8 100644 (file)
@@ -18,6 +18,7 @@
 
 #include "low-can-binding.hpp"
 
+#include <map>
 #include <queue>
 #include <mutex>
 #include <vector>
@@ -25,7 +26,6 @@
 #include <time.h>
 #include <linux/can.h>
 #include <json-c/json.h>
-#include <systemd/sd-event.h>
 
 #include "openxc.pb.h"
 #include "configuration.hpp"
@@ -34,7 +34,7 @@
 #include "can/can-message.hpp"
 #include "utils/timer.hpp"
 #include "utils/signals.hpp"
-#include "obd2/obd2-signals.hpp"
+#include "diagnostic/diagnostic-message.hpp"
 #include "utils/openxc-utils.hpp"
 
 extern "C"
@@ -42,8 +42,6 @@ extern "C"
        #include <afb/afb-service-itf.h>
 };
 
-#define MICRO 1000000
-
 // Interface between the daemon and the binding
 const struct afb_binding_interface *binder_interface;
 
@@ -56,9 +54,9 @@ const struct afb_binding_interface *binder_interface;
 static int make_subscription_unsubscription(struct afb_req request, const std::string& sig_name, std::map<std::string, struct afb_event>& s, bool subscribe)
 {
        /* Make the subscription or unsubscription to the event */
-       if (((subscribe ? afb_req_subscribe : afb_req_unsubscribe)(request, s[sig_name.c_str()])) < 0)
+       if (((subscribe ? afb_req_subscribe : afb_req_unsubscribe)(request, s[sig_name])) < 0)
        {
-               ERROR(binder_interface, "Operation goes wrong for signal: %s", sig_name);
+               ERROR(binder_interface, "make_subscription_unsubscription: Operation goes wrong for signal: %s", sig_name.c_str());
                return 0;
        }
        return 1;
@@ -70,7 +68,7 @@ static int create_event_handle(const std::string& sig_name, std::map<std::string
        s[sig_name] = afb_daemon_make_event(binder_interface->daemon, sig_name.c_str());
        if (!afb_event_is_valid(s[sig_name]))
        {
-               ERROR(binder_interface, "Can't create an event, something goes wrong.");
+               ERROR(binder_interface, "create_event_handle: Can't create an event for %s, something goes wrong.", sig_name.c_str());
                return 0;
        }
        return 1;
@@ -82,18 +80,18 @@ static int subscribe_unsubscribe_signal(struct afb_req request, bool subscribe,
 
        std::lock_guard<std::mutex> subscribed_signals_lock(get_subscribed_signals_mutex());
        std::map<std::string, struct afb_event>& s = get_subscribed_signals();
-       if (s.find(sig) != s.end() && !afb_event_is_valid(s[sig]))
+       if (s.find(sig) != s.end())
        {
-               if(!subscribe)
+               if (!afb_event_is_valid(s[sig]) && !subscribe)
                {
-                       NOTICE(binder_interface, "Event isn't valid, it can't be unsubscribed.");
+                       NOTICE(binder_interface, "subscribe_unsubscribe_signal: Event isn't valid, it can't be unsubscribed.");
                        ret = -1;
                }
-               else
+               /*else
                {
-                       /* Event it isn't valid annymore, recreate it */
+                       // Event it isn't valid annymore, recreate it
                        ret = create_event_handle(sig, s);
-               }
+               }*/
        }
        else
        {
@@ -124,9 +122,8 @@ static int subscribe_unsubscribe_signal(struct afb_req request, bool subscribe,
 static int subscribe_unsubscribe_signals(struct afb_req request, bool subscribe, const std::vector<std::string>& signals)
 {
        int rets = 0;
-       sd_event_source *source;
 
-       //TODO: Implement way to dynamically call the right function no matter 
+       //TODO: Implement way to dynamically call the right function no matter
        // how much signals types we have.
 
        for(const std::string& sig : signals)
@@ -134,15 +131,27 @@ static int subscribe_unsubscribe_signals(struct afb_req request, bool subscribe,
                int ret;
                if (active_diagnostic_request_t::is_diagnostic_signal(sig))
                {
-                       std::vector<obd2_signal_t*> found;
-                       configuration_t::instance().find_obd2_signals(build_DynamicField(sig), found);
-                       int frequency = found.front()->get_frequency();
+                       std::vector<diagnostic_message_t*> found;
+                       configuration_t::instance().find_diagnostic_messages(build_DynamicField(sig), found);
                        DiagnosticRequest* diag_req = new DiagnosticRequest(found.front()->build_diagnostic_request());
-                       configuration_t::instance().get_diagnostic_manager().add_recurring_request(
-                               diag_req, sig.c_str(), false, obd2_signal_t::decode_obd2_response, nullptr, (float)frequency);
-                               //TODO: Adding callback requesting ignition status:     diag_req, sig.c_str(), false, obd2_signal_t::decode_obd2_response, obd2_signal_t::check_ignition_status, frequency);
-                       sd_event_add_time(afb_daemon_get_event_loop(binder_interface->daemon), &source, CLOCK_MONOTONIC, frequency*MICRO, 0,
-                                                               configuration_t::instance().get_diagnostic_manager().send_request, diag_req);
+
+                       // If the requested diagnostic message isn't supported by the car then unssubcribe.
+                       // no matter what we want, worse case will be a fail unsubscription but at least we don't
+                       // poll a PID for nothing.
+                       if(found.front()->get_supported())
+                               subscribe = false;
+                       if(subscribe)
+                       {
+                               float frequency = found.front()->get_frequency();
+                               configuration_t::instance().get_diagnostic_manager().add_recurring_request(
+                                       diag_req, sig.c_str(), false, found.front()->get_decoder(), found.front()->get_callback(), (float)frequency);
+                                       //TODO: Adding callback requesting ignition status:     diag_req, sig.c_str(), false, diagnostic_message_t::decode_obd2_response, diagnostic_message_t::check_ignition_status, frequency);
+                       }
+                       else
+                       {
+                               configuration_t::instance().get_diagnostic_manager().cleanup_request(
+                                       configuration_t::instance().get_diagnostic_manager().find_recurring_request(diag_req), true);
+                       }
                }
 
                ret = subscribe_unsubscribe_signal(request, subscribe, sig);
@@ -166,7 +175,7 @@ static int subscribe_unsubscribe_name(struct afb_req request, bool subscribe, co
                ret = 0;
 
        ret = subscribe_unsubscribe_signals(request, subscribe, signals);
-       NOTICE(binder_interface, "Subscribed correctly to %d/%d signal(s).", ret, (int)signals.size());
+       NOTICE(binder_interface, "Subscribed/unsubscribe correctly to %d/%d signal(s).", ret, (int)signals.size());
 
        return ret;
 }
@@ -236,9 +245,9 @@ extern "C"
 
        /**
        * @brief Initialize the binding.
-       * 
+       *
        * @param[in] service Structure which represent the Application Framework Binder.
-       * 
+       *
        * @return Exit code, zero if success.
        */
        int afbBindingV1ServiceInit(struct afb_service service)
@@ -252,7 +261,7 @@ extern "C"
                /// Initialize Diagnostic manager that will handle obd2 requests.
                /// 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(configuration_t::instance().get_diagnostic_manager().initialize(can_bus_manager.get_can_devices().front()))
+               if(configuration_t::instance().get_diagnostic_manager().initialize())
                        return 0;
 
                ERROR(binder_interface, "There was something wrong with CAN device Initialization. Check your config file maybe");