summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/can/can-bus-dev.cpp92
-rw-r--r--src/can/can-bus-dev.hpp60
-rw-r--r--src/can/can-bus.cpp75
-rw-r--r--src/can/can-bus.hpp140
4 files changed, 208 insertions, 159 deletions
diff --git a/src/can/can-bus-dev.cpp b/src/can/can-bus-dev.cpp
new file mode 100644
index 00000000..5166550e
--- /dev/null
+++ b/src/can/can-bus-dev.cpp
@@ -0,0 +1,92 @@
+/*
+ * Copyright (C) 2015, 2016, 2017 "IoT.bzh"
+ * Author "Romain Forlot" <romain.forlot@iot.bzh>
+ * Author "Loïc Collignon" <loic.collignon@iot.bzh>
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <string.h>
+#include <linux/can/raw.h>
+#include <net/if.h>
+#include <sys/ioctl.h>
+
+#include "can-bus-dev.hpp"
+#include "low-can-binding.hpp"
+
+/// @brief Class constructor
+/// @param dev_name String representing the device name into the linux /dev tree
+can_bus_dev_t::can_bus_dev_t(const std::string& dev_name)
+ : device_name_{dev_name}
+{
+}
+
+/// @brief Open the can socket and returning it
+/// @return -1
+int can_bus_dev_t::open()
+{
+ const int canfd_on = 1;
+ const int timestamp_on = 1;
+ struct ifreq ifr;
+ struct timeval timeout;
+
+ DEBUG(binder_interface, "CAN Handler socket : %d", can_socket_.socket());
+ if (can_socket_) return 0;
+
+ can_socket_.open(PF_CAN, SOCK_RAW, CAN_RAW);
+ if (can_socket_)
+ {
+ DEBUG(binder_interface, "CAN Handler socket correctly initialized : %d", can_socket_.socket());
+
+ // Set timeout for read
+ can_socket_.setopt(SOL_SOCKET, SO_RCVTIMEO, (char *)&timeout, sizeof(timeout));
+
+ // Set timestamp for receveid frame
+ if (can_socket_.setopt(SOL_SOCKET, SO_TIMESTAMP, &timestamp_on, sizeof(timestamp_on)) < 0)
+ WARNING(binder_interface, "setsockopt SO_TIMESTAMP error: %s", ::strerror(errno));
+ DEBUG(binder_interface, "Switch CAN Handler socket to use fd mode");
+
+ // try to switch the socket into CAN_FD mode
+ if (can_socket_.setopt(SOL_CAN_RAW, CAN_RAW_FD_FRAMES, &canfd_on, sizeof(canfd_on)) < 0)
+ {
+ NOTICE(binder_interface, "Can not switch into CAN Extended frame format.");
+ is_fdmode_on_ = false;
+ }
+ else
+ {
+ DEBUG(binder_interface, "Correctly set up CAN socket to use FD frames.");
+ is_fdmode_on_ = true;
+ }
+
+ // Attempts to open a socket to CAN bus
+ ::strcpy(ifr.ifr_name, device_name_.c_str());
+ DEBUG(binder_interface, "ifr_name is : %s", ifr.ifr_name);
+ if(::ioctl(can_socket_.socket(), SIOCGIFINDEX, &ifr) < 0)
+ ERROR(binder_interface, "ioctl failed. Error was : %s", strerror(errno));
+ else
+ {
+ txAddress_.can_family = AF_CAN;
+ txAddress_.can_ifindex = ifr.ifr_ifindex;
+
+ // And bind it to txAddress
+ DEBUG(binder_interface, "Bind the socket");
+ if (::bind(can_socket_.socket(), (struct sockaddr *)&txAddress_, sizeof(txAddress_)) < 0)
+ ERROR(binder_interface, "Bind failed. %s", strerror(errno));
+ else
+ return 0;
+ }
+ close();
+ }
+ else ERROR(binder_interface, "socket could not be created. Error was : %s", ::strerror(errno));
+ return -1;
+}
diff --git a/src/can/can-bus-dev.hpp b/src/can/can-bus-dev.hpp
new file mode 100644
index 00000000..83a1b30e
--- /dev/null
+++ b/src/can/can-bus-dev.hpp
@@ -0,0 +1,60 @@
+/*
+ * Copyright (C) 2015, 2016, 2017 "IoT.bzh"
+ * Author "Romain Forlot" <romain.forlot@iot.bzh>
+ * Author "Loïc Collignon" <loic.collignon@iot.bzh>
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#pragma once
+
+#include <string>
+#include <thread>
+#include <linux/can.h>
+
+#include "../utils/socket.hpp"
+
+class can_bus_t;
+class can_message_t;
+
+/// @brief Object representing a can device. Handle opening, closing and reading on the
+/// socket. This is the low level object to be use by can_bus_t.
+class can_bus_dev_t
+{
+private:
+ std::string device_name_;
+ utils::socket_t can_socket_;
+
+ int32_t id_; /// < an identifier used through binding that refer to that device
+
+ bool is_fdmode_on_; /// < boolean telling if whether or not the can socket use fdmode.
+ struct sockaddr_can txAddress_; /// < internal member using to bind to the socket
+
+ std::thread th_reading_; /// < Thread handling read the socket can device filling can_message_q_ queue of can_bus_t
+ bool is_running_; /// < boolean telling whether or not reading is running or not
+ void can_reader(can_bus_t& can_bus);
+
+public:
+ can_bus_dev_t(const std::string& dev_name);
+
+ int open();
+ int close();
+
+ void start_reading(can_bus_t& can_bus);
+
+ void stop_reading();
+
+ std::pair<struct canfd_frame&, size_t> read();
+
+ int send_can_message(can_message_t& can_msg);
+};
diff --git a/src/can/can-bus.cpp b/src/can/can-bus.cpp
index 699cb8c3..e92e2cbc 100644
--- a/src/can/can-bus.cpp
+++ b/src/can/can-bus.cpp
@@ -15,8 +15,6 @@
* limitations under the License.
*/
-#include "can/can-bus.hpp"
-
#include <map>
#include <cerrno>
#include <vector>
@@ -37,6 +35,8 @@ extern "C"
#include <afb/afb-binding.h>
}
+#include "can/can-bus.hpp"
+
/********************************************************************************
*
* can_bus_t method implementation
@@ -53,6 +53,7 @@ can_bus_t::can_bus_t(int conf_file)
{
}
+
/**
* @brief thread to decoding raw CAN messages.
*
@@ -358,74 +359,6 @@ std::map<std::string, std::shared_ptr<can_bus_dev_t>> can_bus_t::get_can_devices
* can_bus_dev_t method implementation
*
*********************************************************************************/
-/**
-* @brief Class constructor
-*
-* @param const string representing the device name into the linux /dev tree
-*/
-can_bus_dev_t::can_bus_dev_t(const std::string& dev_name)
- : device_name_{dev_name}, can_socket_{-1}
-{}
-
-/**
-* @brief Open the can socket and returning it
-*
-* @return
-*/
-int can_bus_dev_t::open()
-{
- const int canfd_on = 1;
- const int timestamp_on = 1;
- struct ifreq ifr;
- struct timeval timeout;
-
- DEBUG(binder_interface, "CAN Handler socket : %d", can_socket_);
- if (can_socket_ >= 0)
- return 0;
-
- can_socket_ = ::socket(PF_CAN, SOCK_RAW, CAN_RAW);
- DEBUG(binder_interface, "CAN Handler socket correctly initialized : %d", can_socket_);
- if (can_socket_ < 0)
- ERROR(binder_interface, "socket could not be created. Error was : %s", ::strerror(errno));
- else
- {
- /* Set timeout for read */
- ::setsockopt(can_socket_, SOL_SOCKET, SO_RCVTIMEO, (char *)&timeout, sizeof(timeout));
- /* Set timestamp for receveid frame */
- if (::setsockopt(can_socket_, SOL_SOCKET, SO_TIMESTAMP, &timestamp_on, sizeof(timestamp_on)) < 0)
- WARNING(binder_interface, "setsockopt SO_TIMESTAMP error: %s", ::strerror(errno));
- DEBUG(binder_interface, "Switch CAN Handler socket to use fd mode");
- /* try to switch the socket into CAN_FD mode */
- if (::setsockopt(can_socket_, SOL_CAN_RAW, CAN_RAW_FD_FRAMES, &canfd_on, sizeof(canfd_on)) < 0)
- {
- NOTICE(binder_interface, "Can not switch into CAN Extended frame format.");
- is_fdmode_on_ = false;
- } else {
- DEBUG(binder_interface, "Correctly set up CAN socket to use FD frames.");
- is_fdmode_on_ = true;
- }
-
- /* Attempts to open a socket to CAN bus */
- ::strcpy(ifr.ifr_name, device_name_.c_str());
- DEBUG(binder_interface, "ifr_name is : %s", ifr.ifr_name);
- if(::ioctl(can_socket_, SIOCGIFINDEX, &ifr) < 0)
- ERROR(binder_interface, "ioctl failed. Error was : %s", strerror(errno));
- else
- {
- txAddress_.can_family = AF_CAN;
- txAddress_.can_ifindex = ifr.ifr_ifindex;
-
- /* And bind it to txAddress */
- DEBUG(binder_interface, "Bind the socket");
- if (::bind(can_socket_, (struct sockaddr *)&txAddress_, sizeof(txAddress_)) < 0)
- ERROR(binder_interface, "Bind failed. %s", strerror(errno));
- else
- return 0;
- }
- close();
- }
- return -1;
-}
/**
* @brief Open the can socket and returning it
@@ -552,4 +485,4 @@ int can_bus_dev_t::send_can_message(can_message_t& can_msg)
open();
}
return 0;
-} \ No newline at end of file
+}
diff --git a/src/can/can-bus.hpp b/src/can/can-bus.hpp
index 4c265286..4443bf2a 100644
--- a/src/can/can-bus.hpp
+++ b/src/can/can-bus.hpp
@@ -1,6 +1,7 @@
/*
- * Copyright (C) 2015, 2016 "IoT.bzh"
+ * Copyright (C) 2015, 2016, 2017 "IoT.bzh"
* Author "Romain Forlot" <romain.forlot@iot.bzh>
+ * Author "Loïc Collignon" <loic.collignon@iot.bzh>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -30,6 +31,8 @@
#include "low-can-binding.hpp"
+#include "can-bus-dev.hpp"
+
// TODO actual max is 32 but dropped to 24 for memory considerations
#define MAX_ACCEPTANCE_FILTERS 24
// TODO this takes up a ton of memory
@@ -37,93 +40,54 @@
#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.
- *
- * This object is also used to initialize can_bus_dev_t object after reading
- * json conf file describing the CAN devices to use. Thus, those object will read
- * on the device the CAN frame and push them into the can_bus_t can_message_q_ queue.
- *
- * That queue will be later used to be decoded and pushed to subscribers.
- */
-class can_bus_t {
- private:
- int conf_file_; /*!< conf_file_ - configuration file handle used to initialize can_bus_dev_t objects.*/
-
- void can_decode_message();
- std::thread th_decoding_; /*!< thread that'll handle decoding a can frame */
- bool is_decoding_; /*!< boolean member controling thread while loop*/
-
- void can_event_push();
- std::thread th_pushing_; /*!< thread that'll handle pushing decoded can frame to subscribers */
- bool is_pushing_; /*!< boolean member controling thread while loop*/
-
- std::condition_variable new_can_message_cv_; /*!< condition_variable use to wait until there is a new CAN message to read*/
- std::mutex can_message_mutex_; /*!< mutex protecting the can_message_q_ queue.*/
- std::queue <can_message_t> can_message_q_; /*!< queue that'll store can_message_t to decoded */
-
- std::condition_variable new_decoded_can_message_; /*!< condition_variable use to wait until there is a new vehicle message to read from the queue vehicle_message_q_*/
- std::mutex decoded_can_message_mutex_; /*!< mutex protecting the vehicle_message_q_ 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:
- can_bus_t(int conf_file);
-
- int init_can_dev();
- std::vector<std::string> read_conf();
-
-
- void start_threads();
- void stop_threads();
-
- can_message_t next_can_message();
- void push_new_can_message(const can_message_t& can_msg);
- std::mutex& get_can_message_mutex();
- std::condition_variable& get_new_can_message_cv();
-
- openxc_VehicleMessage next_vehicle_message();
- void push_new_vehicle_message(const openxc_VehicleMessage& v_msg);
-
- std::map<std::string, std::shared_ptr<can_bus_dev_t>> get_can_devices();
-};
-
-
-/**
- * @class can_bus_dev_t
- *
- * @brief Object representing a can device. Handle opening, closing and reading on the
- * socket. This is the low level object to be use by can_bus_t.
- */
-class can_bus_dev_t {
- private:
- int32_t id_; /*!< int32_t id_ - an identifier used through binding that refer to that device*/
- std::string device_name_; /*!< std::string device_name_ - name of the linux device handling the can bus. Generally vcan0, can0, etc. */
- int can_socket_; /*!< socket handler for the can device */
- bool is_fdmode_on_; /*!< boolean telling if whether or not the can socket use fdmode. */
- struct sockaddr_can txAddress_; /*!< internal member using to bind to the socket */
-
- std::thread th_reading_; /*!< Thread handling read the socket can device filling can_message_q_ queue of can_bus_t */
- bool is_running_; /*!< boolean telling whether or not reading is running or not */
- void can_reader(can_bus_t& can_bus);
-
- public:
- can_bus_dev_t(const std::string& dev_name);
-
- int open();
- int close();
-
- void start_reading(can_bus_t& can_bus);
-
- void stop_reading();
-
- std::pair<struct canfd_frame&, size_t> read();
-
- int send_can_message(can_message_t& can_msg);
+/// @brief Object used to handle decoding and manage event queue to be pushed.
+///
+/// This object is also used to initialize can_bus_dev_t object after reading
+/// json conf file describing the CAN devices to use. Thus, those object will read
+/// on the device the CAN frame and push them into the can_bus_t can_message_q_ queue.
+///
+/// That queue will be later used to be decoded and pushed to subscribers.
+class can_bus_t
+{
+private:
+ int conf_file_; /// < configuration file handle used to initialize can_bus_dev_t objects.
+
+ void can_decode_message();
+ std::thread th_decoding_; /// < thread that'll handle decoding a can frame
+ bool is_decoding_; /// < boolean member controling thread while loop
+
+ void can_event_push();
+ std::thread th_pushing_; /// < thread that'll handle pushing decoded can frame to subscribers
+ bool is_pushing_; /// < boolean member controling thread while loop
+
+ std::condition_variable new_can_message_cv_; /// < condition_variable use to wait until there is a new CAN message to read
+ std::mutex can_message_mutex_; /// < mutex protecting the can_message_q_ queue.
+ std::queue <can_message_t> can_message_q_; /// < queue that'll store can_message_t to decoded
+
+ std::condition_variable new_decoded_can_message_; /// < condition_variable use to wait until there is a new vehicle message to read from the queue vehicle_message_q_
+ std::mutex decoded_can_message_mutex_; /// < mutex protecting the vehicle_message_q_ 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:
+ can_bus_t(int conf_file);
+
+ int init_can_dev();
+ std::vector<std::string> read_conf();
+
+ void start_threads();
+ void stop_threads();
+
+ can_message_t next_can_message();
+ void push_new_can_message(const can_message_t& can_msg);
+ std::mutex& get_can_message_mutex();
+ std::condition_variable& get_new_can_message_cv();
+
+ openxc_VehicleMessage next_vehicle_message();
+ void push_new_vehicle_message(const openxc_VehicleMessage& v_msg);
+
+ std::map<std::string, std::shared_ptr<can_bus_dev_t>> get_can_devices();
};
/// TODO: implement this function as method into can_bus class