New way to return canfd_frame with now number of read bytes.
[apps/agl-service-can-low-level.git] / src / can-bus.cpp
index 355997f..631f609 100644 (file)
@@ -143,8 +143,6 @@ void can_bus_t::stop_threads()
 {
        is_decoding_ = false;
        is_pushing_ = false;
-       th_decoding_.join();
-       th_pushing_.join();
 }
 
 int can_bus_t::init_can_dev()
@@ -242,7 +240,7 @@ can_message_t can_bus_t::next_can_message()
        {
                can_msg = can_message_q_.front();
                can_message_q_.pop();
-               DEBUG(binder_interface, "next_can_message: Here is the next can message : id %d, length %d", can_msg.get_id(), can_msg.get_length());
+               DEBUG(binder_interface, "next_can_message: Here is the next can message : id %X, length %X", can_msg.get_id(), can_msg.get_length());
                return can_msg;
        }
        
@@ -279,7 +277,7 @@ void can_bus_t::push_new_vehicle_message(const openxc_VehicleMessage& v_msg)
        has_vehicle_message_ = true;
 }
 
-std::map<std::string, std::shared_ptr<can_bus_dev_t>> can_bus_t::get_can_devices();
+std::map<std::string, std::shared_ptr<can_bus_dev_t>> can_bus_t::get_can_devices()
 {
        return can_devices_m_;
 }
@@ -357,11 +355,11 @@ int can_bus_dev_t::close()
        return can_socket_;
 }
 
-canfd_frame can_bus_dev_t::read()
+std::pair<struct canfd_frame&, size_t> can_bus_dev_t::read()
 {
        ssize_t nbytes;
        //int maxdlen;
-       canfd_frame canfd_frame;
+       struct canfd_frame cfd;
 
        /* Test that socket is really opened */
        if (can_socket_ < 0)
@@ -370,28 +368,18 @@ canfd_frame can_bus_dev_t::read()
                is_running_ = false;
        }
 
-       nbytes = ::read(can_socket_, &canfd_frame, CANFD_MTU);
+       nbytes = ::read(can_socket_, &cfd, CANFD_MTU);
 
-       switch(nbytes)
+       /* if we did not fit into CAN sized messages then stop_reading. */
+       if (nbytes != CANFD_MTU && nbytes != CAN_MTU)
        {
-               case CANFD_MTU:
-                       DEBUG(binder_interface, "read_can: Got an CAN FD frame with length %d", canfd_frame.len);
-                       //maxdlen = CANFD_MAX_DLEN;
-                       break;
-               case CAN_MTU:
-                       DEBUG(binder_interface, "read_can: Got a legacy CAN frame with length %d", canfd_frame.len);
-                       //maxdlen = CAN_MAX_DLEN;
-                       break;
-               default:
-                       if (errno == ENETDOWN)
-                                       ERROR(binder_interface, "read_can: %s binder_interface down", device_name_);
-                       ERROR(binder_interface, "read_can: Error reading CAN bus");
-                       ::memset(&canfd_frame, 0, sizeof(canfd_frame));
-                       is_running_ = false;
-                       break;
+               if (errno == ENETDOWN)
+                       ERROR(binder_interface, "read: %s CAN device down", device_name_);
+               ERROR(binder_interface, "read: Incomplete CAN(FD) frame");
+               ::memset(&cfd, 0, sizeof(cfd));
        }
-       
-       return canfd_frame;
+
+       return std::pair<struct canfd_frame&, size_t>(cfd, nbytes);
 }
 
 void can_bus_dev_t::start_reading(can_bus_t& can_bus)
@@ -406,7 +394,6 @@ void can_bus_dev_t::start_reading(can_bus_t& can_bus)
 void can_bus_dev_t::stop_reading()
 {
        is_running_ = false;
-       th_reading_.join();
 }
 
 void can_bus_dev_t::can_reader(can_bus_t& can_bus)