In class mutex and condition variable except for subscribed_signals map
[apps/agl-service-can-low-level.git] / src / can-utils.cpp
index 5a845b3..977d344 100644 (file)
@@ -28,7 +28,6 @@
 #include <sys/socket.h>
 #include <json-c/json.h>
 #include <linux/can/raw.h>
-#include <systemd/sd-event.h>
 
 extern "C"
 {
@@ -163,16 +162,33 @@ canfd_frame can_message_t::convert_to_canfd_frame()
 *********************************************************************************/
 
 can_bus_t::can_bus_t(int& conf_file)
-       :  conf_file_{conf_file}
+       : conf_file_{conf_file}
 {
 }
 
 void can_bus_t::start_threads()
 {
        th_decoding_ = std::thread(can_decode_message, std::ref(*this));
+       is_decoding_ = true;
        th_pushing_ = std::thread(can_event_push, std::ref(*this));
+       is_pushing_ = true;
 }
 
+void can_bus_t::stop_threads()
+{
+       is_decoding_ = false;
+       is_pushing_ = false;
+}
+
+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()
 {
@@ -190,15 +206,17 @@ int can_bus_t::init_can_dev()
                for(const auto& device : devices_name)
                {
                        can_bus_dev_t can_bus_device_handler(device);
-                       can_bus_device_handler.open();
+                       if (can_bus_device_handler.open())
+                               i++;
+                       else
+                               ERROR(binder_interface, "Can't open device %s", device);
                        can_bus_device_handler.start_reading(std::ref(*this));
-                       i++;
                }
 
                NOTICE(binder_interface, "Initialized %d/%d can bus device(s)", i, t);
                return 0;
        }
-       ERROR(binder_interface, "init_can_dev: Error at CAN device initialization. No devices read into configuration file. Did you specify canbus JSON object ?");
+       ERROR(binder_interface, "init_can_dev: Error at CAN device initialization. No devices read from configuration file. Did you specify canbus JSON object ?");
        return 1;
 }
 
@@ -240,6 +258,26 @@ std::vector<std::string> can_bus_t::read_conf()
        return ret;
 }
 
+std::condition_variable& can_bus_t::get_new_can_message()
+{
+       return new_can_message_;
+}
+
+std::mutex& can_bus_t::get_can_message_mutex()
+{
+       return can_message_mutex_;
+}
+
+std::condition_variable& can_bus_t::get_new_decoded_can_message()
+{
+       return new_decoded_can_message_;
+}
+
+std::mutex& can_bus_t::get_decoded_can_message_mutex()
+{
+       return decoded_can_message_mutex_;
+}
+
 can_message_t can_bus_t::next_can_message()
 {
        can_message_t can_msg;
@@ -295,15 +333,6 @@ bool can_bus_t::has_vehicle_message() const
        return has_vehicle_message_;
 }
 
-/********************************************************************************
-*
-*              This is the sd_event_add_io callback function declaration. 
-*              Its implementation can be found into low-can-binding.cpp.
-*
-*********************************************************************************/
-
-int can_frame_received(sd_event_source *s, int fd, uint32_t revents, void *userdata);
-
 /********************************************************************************
 *
 *              can_bus_dev_t method implementation
@@ -315,22 +344,6 @@ can_bus_dev_t::can_bus_dev_t(const std::string &dev_name)
 {
 }
 
-int can_bus_dev_t::event_loop_connection()
-{
-       sd_event_source *source;
-       int rc;
-
-       /* adds to the event loop */
-       rc = sd_event_add_io(afb_daemon_get_event_loop(binder_interface->daemon), &source, can_socket_, EPOLLIN, can_frame_received, this);
-       if (rc < 0) {
-               close();
-               ERROR(binder_interface, "Can't coonect CAN device %s to the event loop", device_name_);
-       } else {
-               NOTICE(binder_interface, "Connected to %s", device_name_);
-       }
-       return rc;
-}
-
 int can_bus_dev_t::open()
 {
        const int canfd_on = 1;
@@ -370,14 +383,9 @@ int can_bus_dev_t::open()
 
                        /* And bind it to txAddress */
                        if (::bind(can_socket_, (struct sockaddr *)&txAddress_, sizeof(txAddress_)) < 0)
-                       {
                                ERROR(binder_interface, "Bind failed");
-                       }
                        else
-                       {
-                               ::fcntl(can_socket_, F_SETFL, O_NONBLOCK);
                                return 0;
-                       }
                }
                close();
        }