summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRomain Forlot <romain.forlot@iot.bzh>2017-02-23 14:09:30 +0000
committerRomain Forlot <romain.forlot@iot.bzh>2017-02-23 14:09:30 +0000
commitd59970dce571c1753174124326406ec1ff8e2ce8 (patch)
tree9d58203f97dc15353502f49a401fca383b00dd35
parentb0975e4bfa534153657a7ee8acaee0f950056e97 (diff)
Make binder_interface global variable rather than
pass it over and over through class and functions Change-Id: Ie32d44126a41125c6c45d18663b668328df44e2d Signed-off-by: Romain Forlot <romain.forlot@iot.bzh>
-rw-r--r--src/CMakeLists.txt2
-rw-r--r--src/can-signals.cpp4
-rw-r--r--src/can-signals.hpp2
-rw-r--r--src/can-utils.cpp71
-rw-r--r--src/can-utils.hpp26
-rw-r--r--src/can_decode_message.cpp4
-rw-r--r--src/can_reader.cpp4
-rw-r--r--src/low-can-binding.cpp4
8 files changed, 54 insertions, 63 deletions
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index e1a82ffc..91de0b3d 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -40,7 +40,7 @@ add_compile_options(-Werror=implicit-function-declaration)
add_compile_options(-ffunction-sections -fdata-sections)
add_compile_options(-Wl,--as-needed -Wl,--gc-sections)
add_compile_options(-fPIC)
-add_compile_options(-std=c++11)
+add_compile_options(-std=c++11 -D_REENTRANT)
set(CMAKE_C_FLAGS_PROFILING "-g -O0 -pg -Wp,-U_FORTIFY_SOURCE")
set(CMAKE_C_FLAGS_DEBUG "-g -O0 -ggdb -Wp,-U_FORTIFY_SOURCE")
diff --git a/src/can-signals.cpp b/src/can-signals.cpp
index ee682f9f..2cf4b038 100644
--- a/src/can-signals.cpp
+++ b/src/can-signals.cpp
@@ -35,7 +35,7 @@ size_t getSignalCount()
return SIGNALS[MESSAGE_SET_ID].size();
}
-std::vector<CanSignal> find_can_signals(const struct afb_binding_interface* interface, openxc_DynamicField& key)
+std::vector<CanSignal> find_can_signals(openxc_DynamicField& key)
{
std::vector<CanSignal> signals;
@@ -57,7 +57,7 @@ std::vector<CanSignal> find_can_signals(const struct afb_binding_interface* inte
}
break;
default:
- ERROR(interface, "find_can_signals: wrong openxc_DynamicField specified. Use openxc_DynamicField_Type_NUM or openxc_DynamicField_Type_STRING type only.");
+ ERROR(binder_interface, "find_can_signals: wrong openxc_DynamicField specified. Use openxc_DynamicField_Type_NUM or openxc_DynamicField_Type_STRING type only.");
CanSignal cs;
::memset(&cs, 0, sizeof(CanSignal));
signals.push_back(cs);
diff --git a/src/can-signals.hpp b/src/can-signals.hpp
index 374afeea..d4ada1de 100644
--- a/src/can-signals.hpp
+++ b/src/can-signals.hpp
@@ -101,7 +101,7 @@ CanBus* getCanBuses();
*
* @return std::vector<std::string> return found CanSignal generic name vector.
*/
-std::vector<CanSignal> find_can_signals(const struct afb_binding_interface* interface, const openxc_DynamicField &key);
+std::vector<CanSignal> find_can_signals(const openxc_DynamicField &key);
/**
* @brief Retrieve can arbitration id of a given CanSignal
diff --git a/src/can-utils.cpp b/src/can-utils.cpp
index de9e8ce1..ba4060ea 100644
--- a/src/can-utils.cpp
+++ b/src/can-utils.cpp
@@ -23,8 +23,8 @@
*
*********************************************************************************/
-can_message_t::can_message_t(const struct afb_binding_interface* interface)
- : interface_{interface}, id_{0}, length_{0}, format_{CanMessageFormat::ERROR}, data_{0,0,0,0,0,0,0,0}
+can_message_t::can_message_t()
+ : id_{0}, length_{0}, format_{CanMessageFormat::ERROR}, data_{0,0,0,0,0,0,0,0}
{}
uint32_t can_message_t::get_id() const
@@ -71,7 +71,7 @@ void can_message_t::set_id(const uint32_t new_id)
id_ = new_id & CAN_EFF_MASK;
break;
default:
- ERROR(interface_, "ERROR: Can set id, not a compatible format or format not set prior to set id.");
+ ERROR(binder_interface, "ERROR: Can set id, not a compatible format or format not set prior to set id.");
break;
}
}
@@ -81,13 +81,13 @@ void can_message_t::set_format(const CanMessageFormat new_format)
if(new_format == CanMessageFormat::STANDARD || new_format == CanMessageFormat::EXTENDED)
format_ = new_format;
else
- ERROR(interface_, "ERROR: Can set format, wrong format chosen");
+ ERROR(binder_interface, "ERROR: Can set format, wrong format chosen");
}
void can_message_t::set_data(const uint8_t new_data)
{
if ((sizeof(new_data) / sizeof(uint8_t) > CAN_MESSAGE_SIZE))
- ERROR(interface_, "Can set data, your data array is too big");
+ ERROR(binder_interface, "Can set data, your data array is too big");
else
{
::memcpy(&data_, &new_data, sizeof(new_data));
@@ -119,7 +119,7 @@ void can_message_t::convert_from_canfd_frame(const canfd_frame& frame)
if (sizeof(frame.data) <= sizeof(data_))
::memcpy(&data_, frame.data, length_);
else if (sizeof(frame.data) >= CAN_MAX_DLEN)
- ERROR(interface_, "can_message_t: canfd_frame data too long to be stored into CanMessage object");
+ ERROR(binder_interface, "can_message_t: canfd_frame data too long to be stored into CanMessage object");
}
canfd_frame can_message_t::convert_to_canfd_frame()
@@ -133,7 +133,7 @@ canfd_frame can_message_t::convert_to_canfd_frame()
::memcpy(frame.data, get_data(), length_);
}
else
- ERROR(interface_, "can_message_t not correctly initialized to be sent");
+ ERROR(binder_interface, "can_message_t not correctly initialized to be sent");
return frame;
}
@@ -149,20 +149,20 @@ can_bus_dev_t::can_bus_dev_t(const std::string &dev_name)
{
}
-int can_bus_dev_t::open(const struct afb_binding_interface* interface)
+int can_bus_dev_t::open()
{
const int canfd_on = 1;
struct ifreq ifr;
struct timeval timeout = {1, 0};
- DEBUG(interface, "open_can_dev: CAN Handler socket : %d", can_socket_);
+ DEBUG(binder_interface, "CAN Handler socket : %d", can_socket_);
if (can_socket_ >= 0)
return 0;
can_socket_ = ::socket(PF_CAN, SOCK_RAW, CAN_RAW);
if (can_socket_ < 0)
{
- ERROR(interface, "open_can_dev: socket could not be created");
+ ERROR(binder_interface, "socket could not be created");
}
else
{
@@ -171,7 +171,7 @@ int can_bus_dev_t::open(const struct afb_binding_interface* interface)
/* 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(interface, "open_can_dev: Can not switch into CAN Extended frame format.");
+ NOTICE(binder_interface, "Can not switch into CAN Extended frame format.");
is_fdmode_on_ = false;
} else {
is_fdmode_on_ = true;
@@ -180,7 +180,7 @@ int can_bus_dev_t::open(const struct afb_binding_interface* interface)
/* Attempts to open a socket to CAN bus */
::strcpy(ifr.ifr_name, device_name_.c_str());
if(::ioctl(can_socket_, SIOCGIFINDEX, &ifr) < 0)
- ERROR(interface, "open_can_dev: ioctl failed");
+ ERROR(binder_interface, "ioctl failed");
else
{
txAddress_.can_family = AF_CAN;
@@ -189,7 +189,7 @@ int can_bus_dev_t::open(const struct afb_binding_interface* interface)
/* And bind it to txAddress */
if (::bind(can_socket_, (struct sockaddr *)&txAddress_, sizeof(txAddress_)) < 0)
{
- ERROR(interface, "open_can_dev: bind failed");
+ ERROR(binder_interface, "Bind failed");
}
else
{
@@ -209,7 +209,7 @@ int can_bus_dev_t::close()
return can_socket_;
}
-canfd_frame can_bus_dev_t::read(const struct afb_binding_interface* interface)
+canfd_frame can_bus_dev_t::read()
{
ssize_t nbytes;
//int maxdlen;
@@ -218,7 +218,7 @@ canfd_frame can_bus_dev_t::read(const struct afb_binding_interface* interface)
/* Test that socket is really opened */
if (can_socket_ < 0)
{
- ERROR(interface, "read_can: Socket unavailable. Closing thread.");
+ ERROR(binder_interface, "read_can: Socket unavailable. Closing thread.");
is_running_ = false;
}
@@ -227,17 +227,17 @@ canfd_frame can_bus_dev_t::read(const struct afb_binding_interface* interface)
switch(nbytes)
{
case CANFD_MTU:
- DEBUG(interface, "read_can: Got an CAN FD frame with length %d", canfd_frame.len);
+ DEBUG(binder_interface, "read_can: Got an CAN FD frame with length %d", canfd_frame.len);
//maxdlen = CANFD_MAX_DLEN;
break;
case CAN_MTU:
- DEBUG(interface, "read_can: Got a legacy CAN frame with length %d", canfd_frame.len);
+ DEBUG(binder_interface, "read_can: Got a legacy CAN frame with length %d", canfd_frame.len);
//maxdlen = CAN_MAX_DLEN;
break;
default:
if (errno == ENETDOWN)
- ERROR(interface, "read_can: %s interface down", device_name_);
- ERROR(interface, "read_can: Error reading CAN bus");
+ ERROR(binder_interface, "read_can: %s binder_interface down", device_name_);
+ ERROR(binder_interface, "read_can: Error reading CAN bus");
::memset(&canfd_frame, 0, sizeof(canfd_frame));
is_running_ = false;
break;
@@ -249,6 +249,7 @@ canfd_frame can_bus_dev_t::read(const struct afb_binding_interface* interface)
void can_bus_dev_t::start_reading(can_bus_t& can_bus)
{
th_reading_ = std::thread(can_reader, std::ref(*this), std::ref(can_bus));
+
is_running_ = true;
}
@@ -260,7 +261,7 @@ bool can_bus_dev_t::is_running()
return is_running_;
}
-int can_bus_dev_t::send_can_message(can_message_t& can_msg, const struct afb_binding_interface* interface)
+int can_bus_dev_t::send_can_message(can_message_t& can_msg)
{
ssize_t nbytes;
canfd_frame f;
@@ -273,15 +274,15 @@ int can_bus_dev_t::send_can_message(can_message_t& can_msg, const struct afb_bin
(struct sockaddr*)&txAddress_, sizeof(txAddress_));
if (nbytes == -1)
{
- ERROR(interface, "send_can_message: Sending CAN frame failed.");
+ ERROR(binder_interface, "send_can_message: Sending CAN frame failed.");
return -1;
}
return (int)nbytes;
}
else
{
- ERROR(interface, "send_can_message: socket not initialized. Attempt to reopen can device socket.");
- open(interface);
+ ERROR(binder_interface, "send_can_message: socket not initialized. Attempt to reopen can device socket.");
+ open();
}
return 0;
}
@@ -291,8 +292,8 @@ int can_bus_dev_t::send_can_message(can_message_t& can_msg, const struct afb_bin
*
*********************************************************************************/
-can_bus_t::can_bus_t(const struct afb_binding_interface *interface, int& conf_file)
- : conf_file_{conf_file}, interface_{interface}
+can_bus_t::can_bus_t(int& conf_file)
+ : conf_file_{conf_file}
{
}
@@ -319,15 +320,15 @@ 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(interface_);
+ can_bus_device_handler.open();
can_bus_device_handler.start_reading(std::ref(*this));
i++;
}
- NOTICE(interface_, "Initialized %d/%d can bus device(s)", i, t);
+ NOTICE(binder_interface, "Initialized %d/%d can bus device(s)", i, t);
return 0;
}
- ERROR(interface_, "init_can_dev: Error at CAN device initialization.");
+ ERROR(binder_interface, "init_can_dev: Error at CAN device initialization.");
return 1;
}
@@ -351,7 +352,7 @@ std::vector<std::string> can_bus_t::read_conf()
if (jo == NULL || !json_object_object_get_ex(jo, "canbus", &canbus))
{
- ERROR(interface_, "Can't find canbus node in the configuration file. Please review it.");
+ ERROR(binder_interface, "Can't find canbus node in the configuration file. Please review it.");
ret.clear();
}
else if (json_object_get_type(canbus) != json_type_array)
@@ -364,24 +365,24 @@ std::vector<std::string> can_bus_t::read_conf()
}
return ret;
}
- ERROR(interface_, "Problem at reading the conf file");
+ ERROR(binder_interface, "Problem at reading the conf file");
ret.clear();
return ret;
}
can_message_t can_bus_t::next_can_message()
{
- can_message_t can_msg(interface_);
+ can_message_t can_msg;
if(!can_message_q_.empty())
{
can_msg = can_message_q_.front();
can_message_q_.pop();
- DEBUG(interface_, "next_can_message: Here is the next can message : id %d, length %d", can_msg.get_id(), can_msg.get_length());
+ DEBUG(binder_interface, "next_can_message: Here is the next can message : id %d, length %d", can_msg.get_id(), can_msg.get_length());
return can_msg;
}
- NOTICE(interface_, "next_can_message: End of can message queue");
+ NOTICE(binder_interface, "next_can_message: End of can message queue");
has_can_message_ = false;
return can_msg;
}
@@ -404,11 +405,11 @@ openxc_VehicleMessage can_bus_t::next_vehicle_message()
{
v_msg = vehicle_message_q_.front();
vehicle_message_q_.pop();
- DEBUG(interface_, "next_vehicle_message: next vehicle message poped");
+ DEBUG(binder_interface, "next_vehicle_message: next vehicle message poped");
return v_msg;
}
- NOTICE(interface_, "next_vehicle_message: End of vehicle message queue");
+ NOTICE(binder_interface, "next_vehicle_message: End of vehicle message queue");
has_vehicle_message_ = false;
return v_msg;
}
diff --git a/src/can-utils.hpp b/src/can-utils.hpp
index 4d41f211..218452d9 100644
--- a/src/can-utils.hpp
+++ b/src/can-utils.hpp
@@ -22,10 +22,10 @@
#include <vector>
#include <cstdio>
#include <string>
-#include <thread>
#include <fcntl.h>
#include <unistd.h>
#include <net/if.h>
+#include <thread>
#include <sys/ioctl.h>
#include <linux/can.h>
#include <sys/socket.h>
@@ -34,6 +34,7 @@
#include "timer.hpp"
#include "openxc.pb.h"
+#include "low-can-binding.hpp"
extern "C"
{
@@ -98,11 +99,6 @@ typedef enum CanMessageFormat CanMessageFormat;
*
* @brief A compact representation of a single CAN message, meant to be used in in/out
* buffers.
- *
- * param[in]
- * param[in]
- * param[in]
- * @param[in]
*/
/*************************
@@ -118,8 +114,6 @@ typedef struct CanMessage CanMessage;
*/
class can_message_t {
private:
- const struct afb_binding_interface* interface_; /*!< afb_binding_interface interface between daemon and binding */
-
uint32_t id_; /*!< uint32_t id - The ID of the message. */
uint8_t length_; /*!< uint8_t length - the length of the data array (max 8). */
CanMessageFormat format_; /*!< CanMessageFormat format - the format of the message's ID.*/
@@ -130,10 +124,8 @@ class can_message_t {
* @brief Class constructor
*
* Constructor about can_message_t class.
- *
- * @param interface - const structafb_binding_interface pointer
*/
- can_message_t(const struct afb_binding_interface* interface);
+ can_message_t();
/**
* @brief Retrieve id_ member value.
@@ -254,15 +246,13 @@ class can_bus_t {
std::queue <openxc_VehicleMessage> vehicle_message_q_; /*!< queue that'll store openxc_VehicleMessage to pushed */
public:
- const struct afb_binding_interface *interface_; /*!< interface_ - afb_binding_interface pointer to the binder. Used to log messages */
-
/**
* @brief Class constructor
*
* @param struct afb_binding_interface *interface between daemon and binding
* @param int file handle to the json configuration file.
*/
- can_bus_t(const struct afb_binding_interface *interface, int& conf_file);
+ can_bus_t(int& conf_file);
/**
* @brief Will initialize can_bus_dev_t objects after reading
@@ -356,7 +346,7 @@ class can_bus_dev_t {
*
* @return
*/
- int open(const struct afb_binding_interface* interface);
+ int open();
int close();
bool is_running();
@@ -364,7 +354,7 @@ class can_bus_dev_t {
* @brief start reading threads and set flag is_running_
*
* @param can_bus_t reference can_bus_t. it will be passed to the thread
- * to allow using afb_binding_interface and can_bus_t queue.
+ * to allow using can_bus_t queue.
*/
void start_reading(can_bus_t& can_bus);
@@ -374,7 +364,7 @@ class can_bus_dev_t {
* @param const struct afb_binding_interface* interface pointer. Used to be able to log
* using application framework logger.
*/
- canfd_frame read(const struct afb_binding_interface *interface);
+ canfd_frame read();
/**
* @brief Send a can message from a can_message_t object.
@@ -383,7 +373,7 @@ class can_bus_dev_t {
* @param const struct afb_binding_interface* interface pointer. Used to be able to log
* using application framework logger.
*/
- int send_can_message(can_message_t& can_msg, const struct afb_binding_interface* interface);
+ int send_can_message(can_message_t& can_msg);
};
/**
diff --git a/src/can_decode_message.cpp b/src/can_decode_message.cpp
index 8b087cfc..8fadbdb6 100644
--- a/src/can_decode_message.cpp
+++ b/src/can_decode_message.cpp
@@ -20,7 +20,7 @@
void can_decode_message(can_bus_t &can_bus)
{
- can_message_t can_message(can_bus.interface_) ;
+ can_message_t can_message;
std::vector <CanSignal> signals;
std::vector <CanSignal>::iterator signals_i;
openxc_VehicleMessage vehicle_message;
@@ -35,7 +35,7 @@ void can_decode_message(can_bus_t &can_bus)
/* First we have to found which CanSignal is */
search_key = build_DynamicField((double)can_message.get_id());
- signals = find_can_signals(can_bus.interface_, search_key);
+ signals = find_can_signals(search_key);
/* Decoding the message ! Don't kill the messenger ! */
for(const auto& sig : signals)
diff --git a/src/can_reader.cpp b/src/can_reader.cpp
index 24bc6004..b836570a 100644
--- a/src/can_reader.cpp
+++ b/src/can_reader.cpp
@@ -20,11 +20,11 @@
void can_reader(can_bus_dev_t &can_bus_dev, can_bus_t& can_bus)
{
- can_message_t can_message(can_bus.interface_);
+ can_message_t can_message;
while(can_bus_dev.is_running())
{
- can_message.convert_from_canfd_frame(can_bus_dev.read(can_bus.interface_));
+ can_message.convert_from_canfd_frame(can_bus_dev.read());
can_bus.push_new_can_message(can_message);
}
} \ No newline at end of file
diff --git a/src/low-can-binding.cpp b/src/low-can-binding.cpp
index 87c94d54..9e344018 100644
--- a/src/low-can-binding.cpp
+++ b/src/low-can-binding.cpp
@@ -136,7 +136,7 @@ static int subscribe_unsubscribe_name(struct afb_req request, bool subscribe, co
else
{
openxc_DynamicField search_key = build_DynamicField(name);
- sig = find_can_signals(binder_interface, search_key);
+ sig = find_can_signals(search_key);
if (sig.empty())
ret = 0;
}
@@ -221,7 +221,7 @@ extern "C"
fd_conf = afb_daemon_rootdir_open_locale(binder_interface->daemon, "can_bus.json", O_RDONLY, NULL);
/* Open CAN socket */
- can_bus_t can_bus_handler(binder_interface, fd_conf);
+ can_bus_t can_bus_handler(fd_conf);
if(can_bus_handler.init_can_dev() == 0)
{
can_bus_handler.start_threads();