#include <sys/socket.h>
#include <json-c/json.h>
#include <linux/can/raw.h>
-#include <systemd/sd-event.h>
extern "C"
{
*********************************************************************************/
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()
{
t = devices_name.size();
i=0;
- std::lock_guard<std::mutex> can_frame_lock(can_frame_mutex);
for(const auto& device : devices_name)
{
can_bus_dev_t can_bus_device_handler(device);
- (can_bus_device_handler.open()) ? i++ : ERROR(binder_interface, "Can't open device %s", device);
+ 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));
}
- can_frame_mutex.unlock();
NOTICE(binder_interface, "Initialized %d/%d can bus device(s)", i, t);
return 0;
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;
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
{
}
-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;
/* 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();
}