Add function remove last bcm frame
[apps/agl-service-can-low-level.git] / low-can-binding / binding / low-can-subscription.cpp
index bb48888..bec3ad6 100644 (file)
@@ -95,8 +95,13 @@ int low_can_subscription_t::set_event()
  */
 int low_can_subscription_t::subscribe(afb_req_t request)
 {
-       if(set_event() < 0)
-               return -1;
+       if(! afb_event_is_valid(event_))
+       {
+               if(set_event() < 0)
+               {
+                       return -1;
+               }
+       }
        return afb_req_subscribe(request, event_);
 }
 
@@ -348,6 +353,17 @@ void low_can_subscription_t::add_one_bcm_frame(struct canfd_frame& cfd, struct b
        bcm_msg.msg_head.nframes++;
 }
 
+/// @brief Take an existing bcm_msg struct and add a can_frame.
+/// Currently only 1 uniq can_frame can be added, it's not possible to build
+/// a multiplexed message with several can_frame.
+void low_can_subscription_t::remove_last_bcm_frame(struct bcm_msg& bcm_msg)
+{
+       struct canfd_frame cf;
+       memset(&cf,0,sizeof(cf));
+       bcm_msg.fd_frames[bcm_msg.msg_head.nframes] = cf;
+       bcm_msg.msg_head.nframes--;
+}
+
 #ifdef USE_FEATURE_J1939
 int low_can_subscription_t::create_rx_filter_j1939(low_can_subscription_t &subscription, std::shared_ptr<signal_t> sig)
 {
@@ -485,9 +501,12 @@ int low_can_subscription_t::tx_send(low_can_subscription_t &subscription, messag
 {
        can_message_t *cm = static_cast<can_message_t*>(message);
 
-       struct bcm_msg bcm_msg = subscription.make_bcm_head(TX_SEND, cm->get_id(),cm->get_flags());
-       canfd_frame cfd = cm->convert_to_canfd_frame();
-       subscription.add_one_bcm_frame(cfd, bcm_msg);
+       struct bcm_msg bcm_msg = subscription.make_bcm_head(TX_SEND, cm->get_id(),cm->get_flags() | TX_CP_CAN_ID); // TX_CP_CAN_ID -> copy in cfd the id of bcm
+       std::vector<canfd_frame> cfd_vect = cm->convert_to_canfd_frame_vector();
+       for(auto cfd: cfd_vect)
+       {
+               subscription.add_one_bcm_frame(cfd, bcm_msg);
+       }
 
        if(subscription.open_socket(subscription, bus_name,socket_type::BCM) < 0)
        {