summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorRomain Forlot <romain.forlot@iot.bzh>2017-02-28 21:09:26 +0100
committerRomain Forlot <romain.forlot@iot.bzh>2017-02-28 21:45:49 +0100
commitca998f6733e3de11886ac6c3ee6525dbaaf3b525 (patch)
tree6298de2bd40417297129680a994135282ac5a3b5 /src
parentabded1a649deb14f7e7fbc67d34d02ff4a9b5fa6 (diff)
Fix: can bus object life is now expanded and don't
die prematuraly. can_bus_handler object is now a pointer living forever. can_bus_dev_t object is now stored as a shared pointer into a variable member into can_bus_t. So everyone survive now. Change-Id: I52768dd7fe3c203a5f679f59afd1bcf330f3af35 Signed-off-by: Romain Forlot <romain.forlot@iot.bzh>
Diffstat (limited to 'src')
-rw-r--r--src/can-bus.cpp42
-rw-r--r--src/can-bus.hpp17
-rw-r--r--src/low-can-binding.cpp8
3 files changed, 36 insertions, 31 deletions
diff --git a/src/can-bus.cpp b/src/can-bus.cpp
index 8511bb1e..e15a3043 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<std::mutex> 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<std::mutex> decoded_can_message_lock(decoded_can_message_mutex_);
@@ -128,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()
@@ -142,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;
@@ -167,12 +162,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<can_bus_dev_t>(device);
+ if (can_devices_m_[device]->open() == 0)
{
i++;
DEBUG(binder_interface, "Start reading thread");
- can_bus_device_handler.start_reading(*this);
+ can_devices_m_[device]->start_reading(*this);
}
else
ERROR(binder_interface, "Can't open device %s", device.c_str());
@@ -394,16 +389,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()
@@ -417,7 +409,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());
diff --git a/src/can-bus.hpp b/src/can-bus.hpp
index 302514d8..83b104b6 100644
--- a/src/can-bus.hpp
+++ b/src/can-bus.hpp
@@ -36,6 +36,8 @@
#define CAN_ACTIVE_TIMEOUT_S 30
+class can_bus_dev_t;
+
/**
* @class can_bus_t
* @brief Object used to handle decoding and manage event queue to be pushed.
@@ -81,6 +83,8 @@ class can_bus_t {
bool has_vehicle_message_; /*!< boolean members that control whether or not there is openxc_VehicleMessage into the queue */
std::queue <openxc_VehicleMessage> vehicle_message_q_; /*!< queue that'll store openxc_VehicleMessage to pushed */
+ std::map<std::string, std::shared_ptr<can_bus_dev_t>> can_devices_m_; /*!< Can device map containing all can_bus_dev_t objects initialized during init_can_dev function*/
+
public:
/**
* @brief Class constructor
@@ -88,7 +92,7 @@ class can_bus_t {
* @param struct afb_binding_interface *interface between daemon and binding
* @param int file handle to the json configuration file.
*/
- can_bus_t(int& conf_file);
+ can_bus_t(int conf_file);
/**
* @brief Will initialize can_bus_dev_t objects after reading
@@ -152,11 +156,17 @@ class can_bus_t {
void push_new_can_message(const can_message_t& can_msg);
/**
- * @brief Return a boolean telling if there is any can_message into the queue
+ * @brief return can_message_mutex_ member
*
- * @return true if there is at least a can_message_t, false if not.
+ * @return return can_message_mutex_ member
*/
std::mutex& get_can_message_mutex();
+
+ /**
+ * @brief return new_can_message_ member
+ *
+ * @return return new_can_message_ member
+ */
std::condition_variable& get_new_can_message();
@@ -175,6 +185,7 @@ class can_bus_t {
void push_new_vehicle_message(const openxc_VehicleMessage& v_msg);
};
+
/**
* @class can_bus_dev_t
*
diff --git a/src/low-can-binding.cpp b/src/low-can-binding.cpp
index 0ffef5e9..24d14575 100644
--- a/src/low-can-binding.cpp
+++ b/src/low-can-binding.cpp
@@ -44,6 +44,8 @@ extern "C"
*/
const struct afb_binding_interface *binder_interface;
+can_bus_t *can_bus_handler;
+
/********************************************************************************
*
* Subscription and unsubscription
@@ -235,12 +237,12 @@ extern "C"
fd_conf = afb_daemon_rootdir_open_locale(binder_interface->daemon, "can_buses.json", O_RDONLY, NULL);
/* Initialize the CAN bus handler */
- can_bus_t can_bus_handler(fd_conf);
+ can_bus_handler = new can_bus_t(fd_conf);
/* Open CAN socket */
- if(can_bus_handler.init_can_dev() == 0)
+ if(can_bus_handler->init_can_dev() == 0)
{
- can_bus_handler.start_threads();
+ can_bus_handler->start_threads();
return 0;
}
ERROR(binder_interface, "There was something wrong with CAN device Initialization. Check your config file maybe");