Fix: read 2 times for 1 incoming CAN frame.
[apps/agl-service-can-low-level.git] / CAN-binder / low-can-binding / binding / low-can-cb.cpp
index ce829b9..bc94eed 100644 (file)
@@ -47,7 +47,7 @@ extern "C"
 ///
 ///*******************************************************************************/
 
-void on_no_clients(std::shared_ptr<low_can_subscription_t> can_subscription, uint32_t pid)
+void on_no_clients(std::shared_ptr<low_can_subscription_t> can_subscription, uint32_t pid, std::map<int, std::shared_ptr<low_can_subscription_t> >& s)
 {
        if( ! can_subscription->get_diagnostic_message().empty() && can_subscription->get_diagnostic_message(pid) != nullptr)
        {
@@ -57,14 +57,11 @@ void on_no_clients(std::shared_ptr<low_can_subscription_t> can_subscription, uin
                        application_t::instance().get_diagnostic_manager().cleanup_request(adr, true);
        }
 
-       on_no_clients(can_subscription);
+       on_no_clients(can_subscription, s);
 }
 
-void on_no_clients(std::shared_ptr<low_can_subscription_t> can_subscription)
+void on_no_clients(std::shared_ptr<low_can_subscription_t> can_subscription, std::map<int, std::shared_ptr<low_can_subscription_t> >& s)
 {
-       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::shared_ptr<low_can_subscription_t> >& s = sm.get_subscribed_signals();
        auto it = s.find(can_subscription->get_index());
        s.erase(it);
 }
@@ -88,10 +85,12 @@ int read_message(sd_event_source *event_source, int fd, uint32_t revents, void *
                utils::socketcan_bcm_t& s = can_subscription->get_socket();
                s >> cm;
 
-               push_n_notify(cm);
+               // Sure we got a valid CAN message ?
+               if(! cm.get_id() == 0 && ! cm.get_length() == 0)
+                       {push_n_notify(cm);}
        }
 
-       /* check if error or hangup */
+       // check if error or hangup
        if ((revents & (EPOLLERR|EPOLLRDHUP|EPOLLHUP)) != 0)
        {
                sd_event_source_unref(event_source);
@@ -135,7 +134,7 @@ static int create_event_handle(std::shared_ptr<low_can_subscription_t>& can_subs
 /// against the application framework using that event handle.
 static int subscribe_unsubscribe_signal(struct afb_req request, bool subscribe, std::shared_ptr<low_can_subscription_t>& can_subscription, std::map<int, std::shared_ptr<low_can_subscription_t> >& s)
 {
-       int ret;
+       int ret = -1;
        int sub_index = can_subscription->get_index();
 
        if (can_subscription && s.find(sub_index) != s.end())
@@ -145,6 +144,7 @@ static int subscribe_unsubscribe_signal(struct afb_req request, bool subscribe,
                        NOTICE(binder_interface, "%s: Event isn't valid, no need to unsubscribed.", __FUNCTION__);
                        ret = -1;
                }
+               ret = 0;
        }
        else
        {
@@ -208,8 +208,6 @@ static int subscribe_unsubscribe_diagnostic_messages(struct afb_req request, boo
                }
                else
                {
-                       diag_m.cleanup_request(
-                                       diag_m.find_recurring_request(*diag_req), true);
                        if(sig->get_supported())
                        {DEBUG(binder_interface, "%s: %s cancelled due to unsubscribe", __FUNCTION__, sig->get_name().c_str());}
                        else
@@ -244,13 +242,13 @@ static int subscribe_unsubscribe_can_signals(struct afb_req request, bool subscr
                         can_subscription = std::make_shared<low_can_subscription_t>(low_can_subscription_t(event_filter));
                        if(can_subscription->create_rx_filter(sig) < 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;}
 
-               if(add_to_event_loop(can_subscription) < 0)
-                       {return -1;}
                rets++;
                DEBUG(binder_interface, "%s: signal: %s subscribed", __FUNCTION__, sig->get_name().c_str());
        }