Cleaning
[apps/agl-service-can-low-level.git] / CAN-binder / low-can-binding / binding / low-can-cb.cpp
index 297c377..712c83c 100644 (file)
@@ -59,9 +59,11 @@ low_can_subscription_t::low_can_subscription_t(struct event_filter_t event_filte
        : event_filter_{event_filter}
 {}
 
-low_can_subscription_t::low_can_subscription_t(struct event_filter_t event_filter, std::shared_ptr<active_diagnostic_request_t> active_diagnostic_request)
-       : active_diagnostic_request_{active_diagnostic_request}, event_filter_{event_filter}
-{}
+low_can_subscription_t::low_can_subscription_t(struct event_filter_t event_filter, std::shared_ptr<diagnostic_message_t> diagnostic_message)
+       : diagnostic_message_{diagnostic_message}, event_filter_{event_filter}
+{
+       index_ = diagnostic_message->get_pid();
+}
 
 low_can_subscription_t::low_can_subscription_t( low_can_subscription_t&& s)
        : index_{s.index_},
@@ -80,6 +82,11 @@ low_can_subscription_t::operator bool() const
        return socket_.socket() != INVALID_SOCKET;
 }
 
+struct afb_event& low_can_subscription_t::get_event()
+{
+       return event_;
+}
+
 int low_can_subscription_t::get_index() const
 {
        return index_;
@@ -94,8 +101,8 @@ const std::string low_can_subscription_t::get_name() const
 {
        if (can_signal_ != nullptr)
                return can_signal_->get_name();
-       if (active_diagnostic_request_ != nullptr)
-               return active_diagnostic_request_->get_name() ;
+       if (diagnostic_message_ != nullptr)
+               return diagnostic_message_->get_name() ;
 
        return "";
 }
@@ -120,6 +127,11 @@ utils::socketcan_bcm_t& low_can_subscription_t::get_socket()
        return socket_;
 }
 
+void low_can_subscription_t::set_event(struct afb_event event)
+{
+       event_ = event;
+}
+
 void low_can_subscription_t::set_frequency(float freq)
 {
        event_filter_.frequency = freq;
@@ -266,10 +278,10 @@ int read_message(sd_event_source *s, int fd, uint32_t revents, void *userdata)
 ///
 ///*******************************************************************************/
 
-static int make_subscription_unsubscription(struct afb_req request, std::shared_ptr<low_can_subscription_t>& can_subscription, std::map<int, std::pair<std::shared_ptr<low_can_subscription_t>, struct afb_event> >& s, bool subscribe)
+static int make_subscription_unsubscription(struct afb_req request, std::shared_ptr<low_can_subscription_t>& can_subscription, std::map<int, std::shared_ptr<low_can_subscription_t> >& s, bool subscribe)
 {
        /* Make the subscription or unsubscription to the event */
-       if (((subscribe ? afb_req_subscribe : afb_req_unsubscribe)(request, s[can_subscription->get_index()].second)) < 0)
+       if (((subscribe ? afb_req_subscribe : afb_req_unsubscribe)(request, s[can_subscription->get_index()]->get_event())) < 0)
        {
                ERROR(binder_interface, "%s: Operation goes wrong for signal: %s", __FUNCTION__, can_subscription->get_name().c_str());
                return -1;
@@ -277,12 +289,12 @@ static int make_subscription_unsubscription(struct afb_req request, std::shared_
        return 0;
 }
 
-static int create_event_handle(std::shared_ptr<low_can_subscription_t>& can_subscription, std::map<int, std::pair<std::shared_ptr<low_can_subscription_t>, struct afb_event> >& s)
+static int create_event_handle(std::shared_ptr<low_can_subscription_t>& can_subscription, std::map<int, std::shared_ptr<low_can_subscription_t> >& s)
 {
        int sub_index = can_subscription->get_index();
-       struct afb_event event = afb_daemon_make_event(binder_interface->daemon, can_subscription->get_name().c_str());
-       s[sub_index] = std::make_pair(can_subscription, event);
-       if (!afb_event_is_valid(s[sub_index].second))
+       can_subscription->set_event(afb_daemon_make_event(binder_interface->daemon, can_subscription->get_name().c_str()));
+       s[sub_index] = can_subscription;
+       if (!afb_event_is_valid(s[sub_index]->get_event()))
        {
                ERROR(binder_interface, "%s: Can't create an event for %s, something goes wrong.", __FUNCTION__, can_subscription->get_name().c_str());
                return -1;
@@ -300,10 +312,10 @@ static int subscribe_unsubscribe_signal(struct afb_req request, bool subscribe,
        utils::signals_manager_t& sm = utils::signals_manager_t::instance();
 
        std::lock_guard<std::mutex> subscribed_signals_lock(sm.get_subscribed_signals_mutex());
-       std::map<int, std::pair<std::shared_ptr<low_can_subscription_t>, struct afb_event> >& s = sm.get_subscribed_signals();
+       std::map<int, std::shared_ptr<low_can_subscription_t> >& s = sm.get_subscribed_signals();
        if (can_subscription && s.find(sub_index) != s.end())
        {
-               if (!afb_event_is_valid(s[sub_index].second) && !subscribe)
+               if (!afb_event_is_valid(s[sub_index]->get_event()) && !subscribe)
                {
                        NOTICE(binder_interface, "%s: Event isn't valid, no need to unsubscribed.", __FUNCTION__);
                        ret = -1;
@@ -317,8 +329,8 @@ static int subscribe_unsubscribe_signal(struct afb_req request, bool subscribe,
        else
        {
                /* Event doesn't exist , so let's create it */
-               struct afb_event empty_event = {nullptr, nullptr};
-               s[sub_index] = std::make_pair(can_subscription, empty_event);
+               can_subscription->set_event({nullptr, nullptr});
+               s[sub_index] = can_subscription;
                ret = create_event_handle(can_subscription, s);
        }
 
@@ -337,18 +349,22 @@ int subscribe_unsubscribe_diagnostic_messages(struct afb_req request, bool subsc
 
        for(const auto& sig : diagnostic_messages)
        {
-               active_diagnostic_request_t* adr;
                DiagnosticRequest* diag_req = conf.get_request_from_diagnostic_message(sig->get_name());
+               float frequency = std::isnan(event_filter.frequency) ? sig->get_frequency() : event_filter.frequency;
 
                // 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
                // poll a PID for nothing.
-               if(sig->get_supported() && subscribe)
+               //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);
+               if(sig->get_supported() && subscribe &&
+                       diag_m.add_recurring_request(diag_req, sig->get_name().c_str(), false, sig->get_decoder(), sig->get_callback(), frequency) != nullptr)
                {
-                       float frequency = std::isnan(event_filter.frequency) ? sig->get_frequency() : event_filter.frequency;
-
-                       adr = diag_m.add_recurring_request(diag_req, sig->get_name().c_str(), false, sig->get_decoder(), sig->get_callback(), 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);
+               std::shared_ptr<low_can_subscription_t> can_subscription(new low_can_subscription_t(event_filter, sig));
+               int ret = subscribe_unsubscribe_signal(request, subscribe, can_subscription);
+               if(ret < 0)
+                       return ret;
+               rets++;
+               DEBUG(binder_interface, "%s: Signal: %s subscribed", __FUNCTION__, sig->get_name().c_str());
                }
                else
                {
@@ -359,13 +375,6 @@ int subscribe_unsubscribe_diagnostic_messages(struct afb_req request, bool subsc
                        diag_req = nullptr;
                        return -1;
                }
-
-               std::shared_ptr<low_can_subscription_t> can_subscription(new low_can_subscription_t(event_filter, std::shared_ptr<active_diagnostic_request_t>(adr)));
-               int ret = subscribe_unsubscribe_signal(request, subscribe, can_subscription);
-               if(ret < 0)
-                       return ret;
-               rets++;
-               DEBUG(binder_interface, "%s: Signal: %s subscribed", __FUNCTION__, sig->get_name().c_str());
        }
 
        return rets;