X-Git-Url: https://gerrit.automotivelinux.org/gerrit/gitweb?a=blobdiff_plain;f=src%2Fcan-bus.cpp;h=52c81cf188268360c8ebe842dffb31ac4c6921a7;hb=0c9926d385f357e4a0a8bb2932da0aa6e2503b9b;hp=f4a6d55c24a152a344658c46e3a3f87141590792;hpb=5b91b2101f4fb83f622a360b4ab42bdb2bfb4e3f;p=apps%2Fagl-service-can-low-level.git diff --git a/src/can-bus.cpp b/src/can-bus.cpp index f4a6d55c..52c81cf1 100644 --- a/src/can-bus.cpp +++ b/src/can-bus.cpp @@ -43,7 +43,7 @@ extern "C" * *********************************************************************************/ -can_bus_t::can_bus_t(int& conf_file) +can_bus_t::can_bus_t(int conf_file) : conf_file_{conf_file} { } @@ -59,7 +59,7 @@ void can_bus_t::can_decode_message() decoder_t decoder; DEBUG(binder_interface, "Beginning of decoding thread."); - while(is_decoding()) + while(is_decoding_) { { std::unique_lock can_message_lock(can_message_mutex_); @@ -102,7 +102,7 @@ void can_bus_t::can_event_push() json_object* jo; DEBUG(binder_interface, "Beginning of the pushing thread"); - while(is_pushing()) + while(is_pushing_) { { std::unique_lock decoded_can_message_lock(decoded_can_message_mutex_); @@ -128,28 +128,21 @@ void can_bus_t::can_event_push() void can_bus_t::start_threads() { - th_decoding_ = std::thread(&can_bus_t::can_decode_message, this); is_decoding_ = true; - th_pushing_ = std::thread(&can_bus_t::can_event_push, this); + th_decoding_ = std::thread(&can_bus_t::can_decode_message, this); + if(!th_decoding_.joinable()) + is_decoding_ = false; + is_pushing_ = true; + th_pushing_ = std::thread(&can_bus_t::can_event_push, this); + if(!th_pushing_.joinable()) + is_pushing_ = false; } void can_bus_t::stop_threads() { is_decoding_ = false; is_pushing_ = false; - th_decoding_.join(); - th_pushing_.join(); -} - -bool can_bus_t::is_decoding() -{ - return is_decoding_; -} - -bool can_bus_t::is_pushing() -{ - return is_pushing_; } int can_bus_t::init_can_dev() @@ -167,12 +160,12 @@ int can_bus_t::init_can_dev() for(const auto& device : devices_name) { - can_bus_dev_t can_bus_device_handler(device); - if (can_bus_device_handler.open() == 0) + can_devices_m_[device] = std::make_shared(device); + if (can_devices_m_[device]->open() == 0) { i++; DEBUG(binder_interface, "Start reading thread"); - can_bus_device_handler.start_reading(std::ref(*this)); + can_devices_m_[device]->start_reading(*this); } else ERROR(binder_interface, "Can't open device %s", device.c_str()); @@ -284,6 +277,11 @@ void can_bus_t::push_new_vehicle_message(const openxc_VehicleMessage& v_msg) has_vehicle_message_ = true; } +std::map> can_bus_t::get_can_devices() +{ + return can_devices_m_; +} + /******************************************************************************** * * can_bus_dev_t method implementation @@ -361,7 +359,7 @@ canfd_frame 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,46 +368,32 @@ 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; -} -bool can_bus_dev_t::is_running() -{ - return is_running_; + return cfd; } void can_bus_dev_t::start_reading(can_bus_t& can_bus) { DEBUG(binder_interface, "Launching reading thread"); - th_reading_ = std::thread(&can_bus_dev_t::can_reader, this, std::ref(can_bus)); is_running_ = true; + th_reading_ = std::thread(&can_bus_dev_t::can_reader, this, std::ref(can_bus)); + if(!th_reading_.joinable()) + is_running_ = false; } 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) @@ -417,7 +401,7 @@ void can_bus_dev_t::can_reader(can_bus_t& can_bus) can_message_t can_message; DEBUG(binder_interface, "Beginning of reading thread"); - while(is_running()) + while(is_running_) { can_message.convert_from_canfd_frame(read());