Handle object without signal, only a bus
authorRomain Forlot <romain.forlot@iot.bzh>
Fri, 14 Jul 2017 15:30:43 +0000 (17:30 +0200)
committerRomain Forlot <romain.forlot@iot.bzh>
Fri, 21 Jul 2017 10:02:11 +0000 (12:02 +0200)
To be able to send or receive message on bus without depending
upon on a signals, this is adds opening socket just with a bus_name
if provided.
Also handle send a frame on bus depending on a signal or not.

Change-Id: I3a60a03a2d36db4db14b9ae90eefc4bbc8f21e32
Signed-off-by: Romain Forlot <romain.forlot@iot.bzh>
low-can-binding/binding/low-can-socket.cpp
low-can-binding/binding/low-can-socket.hpp

index 716b75b..fdd506b 100644 (file)
@@ -160,7 +160,7 @@ void low_can_socket_t::set_max(float max)
 /// this will open the socket with the required CAN bus device name.
 ///
 /// @return INVALID_SOCKET on failure else positive integer
-int low_can_socket_t::open_socket()
+int low_can_socket_t::open_socket(const std::string& bus_name)
 {
        int ret = 0;
        if(! socket_)
@@ -169,6 +169,8 @@ int low_can_socket_t::open_socket()
                        {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());}
+               else if ( ! bus_name.empty())
+                       { ret = socket_.open(bus_name);}
                index_ = (int)socket_.socket();
        }
        return ret;
@@ -181,6 +183,7 @@ int low_can_socket_t::open_socket()
 struct utils::simple_bcm_msg low_can_socket_t::make_bcm_head(uint32_t opcode, uint32_t can_id, uint32_t flags, const struct timeval& timeout, const struct timeval& frequency_thinning) const
 {
        struct utils::simple_bcm_msg bcm_msg;
+       ::memset(&bcm_msg, 0, sizeof(bcm_msg));
 
        bcm_msg.msg_head.opcode  = opcode;
        bcm_msg.msg_head.can_id  = can_id;
@@ -312,3 +315,24 @@ int low_can_socket_t::tx_send(const struct can_frame& cf, std::shared_ptr<can_si
 
        return 0;
 }
+
+/// @brief Create a TX_SEND job used by the BCM socket to
+/// simply send message
+///
+/// @return 0 if ok else -1
+int low_can_socket_t::tx_send(const struct can_frame& cf, const std::string& bus_name)
+{
+       can_signal_ = nullptr;
+
+       utils::simple_bcm_msg bcm_msg =  make_bcm_head(TX_SEND);
+       add_bcm_frame(cf, bcm_msg);
+
+       if(open_socket(bus_name) < 0)
+               {return -1;}
+
+       socket_ << bcm_msg;
+       if(! socket_)
+               return -1;
+
+       return 0;
+}
index 3621a3d..018b307 100644 (file)
@@ -80,11 +80,12 @@ public:
        struct utils::simple_bcm_msg make_bcm_head(uint32_t opcode, uint32_t can_id = 0, uint32_t flags = 0, const struct timeval& timeout = {0,0}, const struct timeval& frequency_thinning = {0,0}) const;
        void add_bcm_frame(const struct can_frame& cfd, struct utils::simple_bcm_msg& bcm_msg) const;
 
-       int open_socket();
+       int open_socket(const std::string& bus_name = "");
 
        int create_rx_filter(std::shared_ptr<can_signal_t> sig);
        int create_rx_filter(std::shared_ptr<diagnostic_message_t> sig);
        int create_rx_filter(utils::simple_bcm_msg& bcm_msg);
 
-       int tx_send(const struct can_frame& cf, std::shared_ptr<can_signal_t> sig = nullptr);
+       int tx_send(const struct can_frame& cf, std::shared_ptr<can_signal_t> sig);
+       int tx_send(const struct can_frame& cf, const std::string& bus_name);
 };