Use subscription's sockets as shared_ptr 32/21732/3
authorRomain Forlot <romain.forlot@iot.bzh>
Tue, 25 Jun 2019 15:12:37 +0000 (17:12 +0200)
committerRomain Forlot <romain.forlot@iot.bzh>
Wed, 26 Jun 2019 15:55:05 +0000 (17:55 +0200)
This change is made to leverage C++ to read and write the different socket
classes depending on CAN protocol used.

Bug-AGL: SPEC-2386

Change-Id: I5e25e271fc82e9627f836aeb43b2af5ef25db83a
Signed-off-by: Stephane Desneux <stephane.desneux@iot.bzh>
Signed-off-by: Romain Forlot <romain.forlot@iot.bzh>
low-can-binding/binding/low-can-cb.cpp
low-can-binding/binding/low-can-subscription.cpp
low-can-binding/binding/low-can-subscription.hpp

index 750baef..6b86907 100644 (file)
@@ -70,12 +70,12 @@ void on_no_clients(std::shared_ptr<low_can_subscription_t> can_subscription, std
        s.erase(it);
 }
 
-static void push_n_notify(const can_message_t& cm)
+static void push_n_notify(std::shared_ptr<can_message_t> m)
 {
        can_bus_t& cbm = application_t::instance().get_can_bus_manager();
        {
                std::lock_guard<std::mutex> can_message_lock(cbm.get_can_message_mutex());
-               cbm.push_new_can_message(cm);
+               cbm.push_new_can_message(*m);
        }
        cbm.get_new_can_message_cv().notify_one();
 }
@@ -83,23 +83,27 @@ static void push_n_notify(const can_message_t& cm)
 int read_message(sd_event_source *event_source, int fd, uint32_t revents, void *userdata)
 {
        low_can_subscription_t* can_subscription = (low_can_subscription_t*)userdata;
+
+
        if ((revents & EPOLLIN) != 0)
        {
-               std::shared_ptr<can_message_t> cm;
-               utils::socketcan_bcm_t& s = can_subscription->get_socket();
-               cm = s.read_message();
+               std::shared_ptr<utils::socketcan_t> s = can_subscription->get_socket();
+               std::shared_ptr<can_message_t> message = s->read_message();
 
                // Sure we got a valid CAN message ?
-               if(! cm->get_id() == 0 && ! cm->get_length() == 0)
-                       {push_n_notify(*cm);}
+               if (! message->get_id() == 0 && ! message->get_length() == 0)
+               {
+                       push_n_notify(message);
+               }
        }
 
        // check if error or hangup
        if ((revents & (EPOLLERR|EPOLLRDHUP|EPOLLHUP)) != 0)
        {
                sd_event_source_unref(event_source);
-               can_subscription->get_socket().close();
+               can_subscription->get_socket()->close();
        }
+
        return 0;
 }
 
@@ -170,7 +174,7 @@ static int add_to_event_loop(std::shared_ptr<low_can_subscription_t>& can_subscr
                struct sd_event_source* event_source = nullptr;
                return ( sd_event_add_io(afb_daemon_get_event_loop(),
                        &event_source,
-                       can_subscription->get_socket().socket(),
+                       can_subscription->get_socket()->socket(),
                        EPOLLIN,
                        read_message,
                        can_subscription.get()));
index d8149b6..09e9b80 100644 (file)
@@ -49,7 +49,7 @@ low_can_subscription_t& low_can_subscription_t::operator=(const low_can_subscrip
 
 low_can_subscription_t::~low_can_subscription_t()
 {
-       socket_.close();
+       socket_->close();
 }
 
 low_can_subscription_t::operator bool() const
@@ -200,7 +200,7 @@ float low_can_subscription_t::get_max() const
        return event_filter_.max;
 }
 
-utils::socketcan_bcm_t& low_can_subscription_t::get_socket()
+std::shared_ptr<utils::socketcan_t> low_can_subscription_t::get_socket()
 {
        return socket_;
 }
@@ -229,12 +229,12 @@ int low_can_subscription_t::open_socket(const std::string& bus_name)
        if(! socket_)
        {
                if( can_signal_ != nullptr)
-                       {ret = socket_.open(can_signal_->get_message()->get_bus_device_name());}
+                       {ret = socket_->open(can_signal_->get_message()->get_bus_device_name());}
                else if (! diagnostic_message_ .empty())
-                       {ret = socket_.open(application_t::instance().get_diagnostic_manager().get_bus_device_name());}
+                       {ret = socket_->open(application_t::instance().get_diagnostic_manager().get_bus_device_name());}
                else if ( ! bus_name.empty())
-                       { ret = socket_.open(bus_name);}
-               index_ = (int)socket_.socket();
+                       { ret = socket_->open(bus_name);}
+               index_ = (int)socket_->socket();
        }
        return ret;
 }
@@ -337,7 +337,7 @@ int low_can_subscription_t::create_rx_filter(utils::bcm_msg& bcm_msg)
        // else monitor all standard 8 CAN OBD2 ID response.
        if(bcm_msg.msg_head.can_id != OBD2_FUNCTIONAL_BROADCAST_ID)
        {
-               socket_.write_message(bcm_msg);
+               socket_->write_message(bcm_msg);
                        if(! socket_)
                                return -1;
        }
@@ -347,7 +347,7 @@ int low_can_subscription_t::create_rx_filter(utils::bcm_msg& bcm_msg)
                {
                        bcm_msg.msg_head.can_id  =  OBD2_FUNCTIONAL_RESPONSE_START + i;
 
-                       socket_.write_message(bcm_msg);
+                       socket_->write_message(bcm_msg);
                        if(! socket_)
                                return -1;
                }
@@ -384,7 +384,7 @@ int low_can_subscription_t::tx_send(struct canfd_frame& cfd, const std::string&
        if(open_socket(bus_name) < 0)
                {return -1;}
 
-       socket_.write_message(bcm_msg);
+       socket_->write_message(bcm_msg);
        if(! socket_)
                return -1;
 
index 8af0aa1..ded600c 100644 (file)
@@ -54,7 +54,7 @@ private:
        std::shared_ptr<can_signal_t> can_signal_; ///< can_signal_ - the CAN signal subscribed
        std::vector<std::shared_ptr<diagnostic_message_t> > diagnostic_message_; ///< diagnostic_message_ - diagnostic messages meant to receive OBD2
                                                                                 /// responses. Normal diagnostic request and response are not tested for now.
-       utils::socketcan_bcm_t socket_; ///< socket_ - socket_ that receives CAN messages.
+       std::shared_ptr<utils::socketcan_bcm_t> socket_; ///< socket_ - socket_ that receives CAN messages.
 
        int set_event();
 
@@ -83,7 +83,7 @@ public:
        float get_frequency() const;
        float get_min() const;
        float get_max() const;
-       utils::socketcan_bcm_t& get_socket();
+       std::shared_ptr<utils::socketcan_t> get_socket();
 
        void set_frequency(float freq);
        void set_min(float min);