Fix name to fit rename of git repository.
[apps/agl-service-can-low-level.git] / low-can-binding / binding / low-can-socket.cpp
index 716b75b..8a8efbf 100644 (file)
@@ -16,8 +16,6 @@
  * limitations under the License.
  */
 
-#include "low-can-hat.hpp"
-
 #include "low-can-subscription.hpp"
 #include "application.hpp"
 #include "canutil/write.h"
@@ -64,6 +62,11 @@ const std::shared_ptr<can_signal_t> low_can_socket_t::get_can_signal() const
        return can_signal_;
 }
 
+bool low_can_socket_t::is_signal_subscription_corresponding(const std::shared_ptr<can_signal_t> can_signal, const struct event_filter_t& event_filter) const
+{
+       return can_signal_ == can_signal && event_filter_ == event_filter;
+}
+
 const std::vector<std::shared_ptr<diagnostic_message_t> > low_can_socket_t::get_diagnostic_message() const
 {
        return diagnostic_message_;
@@ -107,7 +110,10 @@ const std::string low_can_socket_t::get_name() const
 {
        if (can_signal_ != nullptr)
                return can_signal_->get_name();
+       else if (!diagnostic_message_.empty())
+               return "diagnostic_messages";
 
+       AFB_WARNING("No diagnostics messages nor CAN signals registered in that subscription. Name empty ! It's a bug to be reported.");
        return "";
 }
 
@@ -119,6 +125,7 @@ const std::string low_can_socket_t::get_name(uint32_t pid) const
        if (!diagnostic_message_.empty())
                return get_diagnostic_message(pid)->get_name() ;
 
+       AFB_WARNING("No diagnostics messages nor CAN signals registered in that subscription. Name empty ! It's a bug to be reported.");
        return "";
 }
 
@@ -160,7 +167,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 +176,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 +190,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;
@@ -230,7 +240,7 @@ int low_can_socket_t::create_rx_filter(std::shared_ptr<can_signal_t> sig)
                                                        CAN_MAX_DLEN);
 
        struct timeval freq, timeout = {0, 0};
-       frequency_clock_t f = std::isnan(event_filter_.frequency) ? can_signal_->get_frequency() : frequency_clock_t(event_filter_.frequency);
+       frequency_clock_t f = event_filter_.frequency == 0 ? can_signal_->get_frequency() : frequency_clock_t(event_filter_.frequency);
        freq = f.get_timeval_from_period();
 
        utils::simple_bcm_msg bcm_msg = make_bcm_head(RX_SETUP, can_signal_->get_message()->get_id(), SETTIMER|RX_NO_AUTOTIMER, timeout, freq);
@@ -312,3 +322,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;
+}