Implement a new method returning the can device
[apps/agl-service-can-low-level.git] / src / can-bus.cpp
index 84cbaaa..355997f 100644 (file)
@@ -18,6 +18,7 @@
 #include "can-bus.hpp"
 
 #include <map>
+#include <cerrno>
 #include <vector>
 #include <string>
 #include <fcntl.h>
@@ -28,6 +29,9 @@
 #include <json-c/json.h>
 #include <linux/can/raw.h>
 
+#include "can-decoder.hpp"
+#include "openxc-utils.hpp"
+
 extern "C"
 {
        #include <afb/afb-binding.h>
@@ -39,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}
 {
 }
@@ -55,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<std::mutex> can_message_lock(can_message_mutex_);
@@ -98,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<std::mutex> decoded_can_message_lock(decoded_can_message_mutex_);
@@ -124,10 +128,15 @@ 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()
@@ -138,16 +147,6 @@ void can_bus_t::stop_threads()
        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()
 {
        std::vector<std::string> devices_name;
@@ -163,14 +162,15 @@ 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())
+                       can_devices_m_[device] = std::make_shared<can_bus_dev_t>(device);
+                       if (can_devices_m_[device]->open() == 0)
                        {
                                i++;
-                               can_bus_device_handler.start_reading(std::ref(*this));
+                               DEBUG(binder_interface, "Start reading thread");
+                               can_devices_m_[device]->start_reading(*this);
                        }
                        else
-                               ERROR(binder_interface, "Can't open device %s", device);
+                               ERROR(binder_interface, "Can't open device %s", device.c_str());
                }
 
                NOTICE(binder_interface, "Initialized %d/%d can bus device(s)", i, t);
@@ -197,7 +197,7 @@ std::vector<std::string> can_bus_t::read_conf()
                std::fread(&fd_conf_content[0], 1, fd_conf_content.size(), fd);
                std::fclose(fd);
 
-               DEBUG(binder_interface, "Conf file content : %s", fd_conf_content.c_str());
+               DEBUG(binder_interface, "Configuration file content : %s", fd_conf_content.c_str());
                jo = json_tokener_parse(fd_conf_content.c_str());
 
                if (jo == NULL || !json_object_object_get_ex(jo, "canbus", &canbus))
@@ -279,6 +279,11 @@ 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();
+{
+       return can_devices_m_;
+}
+
 /********************************************************************************
 *
 *              can_bus_dev_t method implementation
@@ -389,16 +394,13 @@ canfd_frame can_bus_dev_t::read()
        return canfd_frame;
 }
 
-bool can_bus_dev_t::is_running()
-{
-       return is_running_;
-}
-
 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()
@@ -412,7 +414,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());