aboutsummaryrefslogtreecommitdiffstats
path: root/CAN-binder/low-can-binding/can
diff options
context:
space:
mode:
authorRomain Forlot <romain.forlot@iot.bzh>2017-05-31 12:46:02 +0200
committerRomain Forlot <romain.forlot@iot.bzh>2017-06-01 18:22:49 +0200
commit1f4eb8c5562bf5c0bc6ebd1b35977cd71d9b3a83 (patch)
tree0373a64035f894a7b18139306cbf879989d28268 /CAN-binder/low-can-binding/can
parentc28f479a597de89f38b2bbd155afc26c7f2de822 (diff)
Reworked subscription to integrate filtering.
- Creation of an object which old the subscription context (filter, signal) : low_can_subscription_t - Move socket from can_signal_t to this new object. - Adding a member to can_message_t to transport subscription_id to be able to retrieve it with all the context through running. Change-Id: I87be8cd6c3c93a81040357920d8c081a316800c3 Signed-off-by: Romain Forlot <romain.forlot@iot.bzh>
Diffstat (limited to 'CAN-binder/low-can-binding/can')
-rw-r--r--CAN-binder/low-can-binding/can/can-bus.cpp40
-rw-r--r--CAN-binder/low-can-binding/can/can-bus.hpp9
-rw-r--r--CAN-binder/low-can-binding/can/can-message.cpp22
-rw-r--r--CAN-binder/low-can-binding/can/can-message.hpp3
-rw-r--r--CAN-binder/low-can-binding/can/can-signals.cpp46
-rw-r--r--CAN-binder/low-can-binding/can/can-signals.hpp4
6 files changed, 47 insertions, 77 deletions
diff --git a/CAN-binder/low-can-binding/can/can-bus.cpp b/CAN-binder/low-can-binding/can/can-bus.cpp
index a35eecdf..0ce6dc6a 100644
--- a/CAN-binder/low-can-binding/can/can-bus.cpp
+++ b/CAN-binder/low-can-binding/can/can-bus.cpp
@@ -72,9 +72,9 @@ int can_bus_t::process_can_signals(const can_message_t& can_message)
for(const auto& sig : signals.can_signals)
{
std::lock_guard<std::mutex> subscribed_signals_lock(sm.get_subscribed_signals_mutex());
- std::map<std::string, struct afb_event>& s = sm.get_subscribed_signals();
+ std::map<int, std::pair<std::shared_ptr<low_can_subscription_t>, struct afb_event> >& s = sm.get_subscribed_signals();
- if( s.find(sig->get_name()) != s.end() && afb_event_is_valid(s[sig->get_name()]))
+ if( s.find(can_message.get_sub_id()) != s.end() && afb_event_is_valid(s[can_message.get_sub_id()].second))
{
bool send = true;
decoded_message = decoder_t::translateSignal(*sig, can_message, conf.get_all_can_signals(), &send);
@@ -82,10 +82,10 @@ int can_bus_t::process_can_signals(const can_message_t& can_message)
if(send)
{
openxc_SimpleMessage s_message = build_SimpleMessage(sig->get_name(), decoded_message);
- vehicle_message = build_VehicleMessage(s_message);
+ vehicle_message = build_VehicleMessage(s_message, can_message.get_timestamp());
std::lock_guard<std::mutex> decoded_can_message_lock(decoded_can_message_mutex_);
- push_new_vehicle_message(vehicle_message);
+ push_new_vehicle_message(std::make_pair(can_message.get_sub_id(), vehicle_message));
}
processed_signals++;
}
@@ -110,14 +110,14 @@ int can_bus_t::process_diagnostic_signals(diagnostic_manager_t& manager, const c
utils::signals_manager_t& sm = utils::signals_manager_t::instance();
std::lock_guard<std::mutex> subscribed_signals_lock(sm.get_subscribed_signals_mutex());
- std::map<std::string, struct afb_event>& s = sm.get_subscribed_signals();
+ std::map<int, std::pair<std::shared_ptr<low_can_subscription_t>, struct afb_event> >& s = sm.get_subscribed_signals();
openxc_VehicleMessage vehicle_message = manager.find_and_decode_adr(can_message);
if( (vehicle_message.has_simple_message && vehicle_message.simple_message.has_name) &&
- (s.find(vehicle_message.simple_message.name) != s.end() && afb_event_is_valid(s[vehicle_message.simple_message.name])))
+ (s.find(can_message.get_sub_id()) != s.end() && afb_event_is_valid(s[can_message.get_sub_id()].second)))
{
std::lock_guard<std::mutex> decoded_can_message_lock(decoded_can_message_mutex_);
- push_new_vehicle_message(vehicle_message);
+ push_new_vehicle_message(std::make_pair(can_message.get_sub_id(), vehicle_message));
processed_signals++;
}
@@ -138,8 +138,6 @@ int can_bus_t::process_diagnostic_signals(diagnostic_manager_t& manager, const c
/// TODO: make diagnostic messages parsing optionnal.
void can_bus_t::can_decode_message()
{
- can_message_t can_message;
-
while(is_decoding_)
{
{
@@ -147,10 +145,10 @@ void can_bus_t::can_decode_message()
new_can_message_cv_.wait(can_message_lock);
while(!can_message_q_.empty())
{
- can_message = next_can_message();
+ const can_message_t can_message = next_can_message();
- if(configuration_t::instance().get_diagnostic_manager().is_diagnostic_response(can_message))
- process_diagnostic_signals(configuration_t::instance().get_diagnostic_manager(), can_message);
+ if(application_t::instance().get_diagnostic_manager().is_diagnostic_response(can_message))
+ process_diagnostic_signals(application_t::instance().get_diagnostic_manager(), can_message);
else
process_can_signals(can_message);
}
@@ -163,7 +161,7 @@ void can_bus_t::can_decode_message()
/// which are events that has to be pushed.
void can_bus_t::can_event_push()
{
- openxc_VehicleMessage v_message;
+ std::pair<int, openxc_VehicleMessage> v_message;
openxc_SimpleMessage s_message;
json_object* jo;
utils::signals_manager_t& sm = utils::signals_manager_t::instance();
@@ -176,15 +174,15 @@ void can_bus_t::can_event_push()
{
v_message = next_vehicle_message();
- s_message = get_simple_message(v_message);
+ s_message = get_simple_message(v_message.second);
{
std::lock_guard<std::mutex> subscribed_signals_lock(sm.get_subscribed_signals_mutex());
- std::map<std::string, struct afb_event>& s = sm.get_subscribed_signals();
- if(s.find(std::string(s_message.name)) != s.end() && afb_event_is_valid(s[std::string(s_message.name)]))
+ std::map<int, std::pair<std::shared_ptr<low_can_subscription_t>, struct afb_event> >& s = sm.get_subscribed_signals();
+ if(s.find(v_message.first) != s.end() && afb_event_is_valid(s[v_message.first].second))
{
jo = json_object_new_object();
jsonify_simple(s_message, jo);
- if(afb_event_push(s[std::string(s_message.name)], jo) == 0)
+ if(afb_event_push(s[v_message.first].second, jo) == 0)
on_no_clients(std::string(s_message.name));
}
}
@@ -235,7 +233,7 @@ std::mutex& can_bus_t::get_can_message_mutex()
/// @brief Return first can_message_t on the queue
///
/// @return a can_message_t
-can_message_t can_bus_t::next_can_message()
+const can_message_t can_bus_t::next_can_message()
{
can_message_t can_msg;
@@ -262,9 +260,9 @@ void can_bus_t::push_new_can_message(const can_message_t& can_msg)
/// @brief Return first openxc_VehicleMessage on the queue
///
/// @return a openxc_VehicleMessage containing a decoded can message
-openxc_VehicleMessage can_bus_t::next_vehicle_message()
+std::pair<int, openxc_VehicleMessage> can_bus_t::next_vehicle_message()
{
- openxc_VehicleMessage v_msg;
+ std::pair<int, openxc_VehicleMessage> v_msg;
if(! vehicle_message_q_.empty())
{
@@ -280,7 +278,7 @@ openxc_VehicleMessage can_bus_t::next_vehicle_message()
/// @brief Push a openxc_VehicleMessage into the queue
///
/// @param[in] v_msg - const reference openxc_VehicleMessage object to push into the queue
-void can_bus_t::push_new_vehicle_message(const openxc_VehicleMessage& v_msg)
+void can_bus_t::push_new_vehicle_message(const std::pair<int, openxc_VehicleMessage>& v_msg)
{
vehicle_message_q_.push(v_msg);
}
diff --git a/CAN-binder/low-can-binding/can/can-bus.hpp b/CAN-binder/low-can-binding/can/can-bus.hpp
index 16d8cf6a..934b45d5 100644
--- a/CAN-binder/low-can-binding/can/can-bus.hpp
+++ b/CAN-binder/low-can-binding/can/can-bus.hpp
@@ -18,6 +18,7 @@
#pragma once
+#include <utility>
#include <mutex>
#include <queue>
#include <thread>
@@ -67,7 +68,7 @@ private:
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::queue <std::pair<int, openxc_VehicleMessage> > vehicle_message_q_; ///< queue that'll store openxc_VehicleMessage to pushed
std::vector<std::pair<std::string, std::string> > can_devices_;
public:
@@ -81,11 +82,11 @@ public:
void start_threads();
void stop_threads();
- can_message_t next_can_message();
+ const 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::pair<int, openxc_VehicleMessage> next_vehicle_message();
+ void push_new_vehicle_message(const std::pair<int, openxc_VehicleMessage>& v_msg);
};
diff --git a/CAN-binder/low-can-binding/can/can-message.cpp b/CAN-binder/low-can-binding/can/can-message.cpp
index abfbfdb0..93894947 100644
--- a/CAN-binder/low-can-binding/can/can-message.cpp
+++ b/CAN-binder/low-can-binding/can/can-message.cpp
@@ -27,7 +27,14 @@
/// Constructor about can_message_t class.
///
can_message_t::can_message_t()
- : maxdlen_{0}, id_{0}, length_{0}, format_{can_message_format_t::INVALID}, rtr_flag_{false}, flags_{0}, timestamp_{0}
+ : maxdlen_{0},
+ id_{0},
+ length_{0},
+ format_{can_message_format_t::INVALID},
+ rtr_flag_{false},
+ flags_{0},
+ timestamp_{0},
+ sub_id_{-1}
{}
can_message_t::can_message_t(uint8_t maxdlen,
@@ -45,7 +52,8 @@ can_message_t::can_message_t(uint8_t maxdlen,
rtr_flag_{rtr_flag},
flags_{flags},
data_{data},
- timestamp_{timestamp}
+ timestamp_{timestamp},
+ sub_id_{-1}
{}
///
@@ -58,6 +66,11 @@ uint32_t can_message_t::get_id() const
return id_;
}
+int can_message_t::get_sub_id() const
+{
+ return sub_id_;
+}
+
///
/// @brief Retrieve RTR flag member.
///
@@ -121,6 +134,11 @@ uint8_t can_message_t::get_length() const
return length_;
}
+void can_message_t::set_sub_id(int sub_id)
+{
+ sub_id_ = sub_id;
+}
+
uint64_t can_message_t::get_timestamp() const
{
return timestamp_;
diff --git a/CAN-binder/low-can-binding/can/can-message.hpp b/CAN-binder/low-can-binding/can/can-message.hpp
index 1ce1c7a5..ced2602a 100644
--- a/CAN-binder/low-can-binding/can/can-message.hpp
+++ b/CAN-binder/low-can-binding/can/can-message.hpp
@@ -52,12 +52,14 @@ private:
uint8_t flags_; /*!< flags_ - flags of a CAN FD frame. Needed if we catch FD frames.*/
std::vector<uint8_t> data_; /*!< data_ - The message's data field with a size of 8 which is the standard about CAN bus messages.*/
uint64_t timestamp_; /*!< timestamp_ - timestamp of the received message*/
+ int sub_id_; /*!< sub_id_ - Subscription index. */
public:
can_message_t();
can_message_t(uint8_t maxdlen, uint32_t id, uint8_t length, can_message_format_t format, bool rtr_flag_, uint8_t flags, std::vector<uint8_t>& data, uint64_t timestamp);
uint32_t get_id() const;
+ int get_sub_id() const;
bool get_rtr_flag_() const;
can_message_format_t get_format() const;
uint8_t get_flags() const;
@@ -66,6 +68,7 @@ public:
uint8_t get_length() const;
uint64_t get_timestamp() const;
+ void set_sub_id(int sub_id);
void set_timestamp(uint64_t timestamp);
void set_format(const can_message_format_t new_format);
diff --git a/CAN-binder/low-can-binding/can/can-signals.cpp b/CAN-binder/low-can-binding/can/can-signals.cpp
index 324cecc7..362e3582 100644
--- a/CAN-binder/low-can-binding/can/can-signals.cpp
+++ b/CAN-binder/low-can-binding/can/can-signals.cpp
@@ -83,12 +83,6 @@ can_signal_t::can_signal_t(
, received_{b.received_}
, last_value_{b.last_value_}
{}*/
-
-utils::socketcan_bcm_t can_signal_t::get_socket() const
-{
- return socket_;
-}
-
can_message_definition_t* can_signal_t::get_message() const
{
return parent_;
@@ -220,43 +214,3 @@ void can_signal_t::set_timestamp(uint64_t timestamp)
frequency_.tick(timestamp);
}
-/// @brief Create a RX_SETUP receive job used by the BCM socket.
-///
-/// @return 0 if ok else -1
-int can_signal_t::create_rx_filter()
-{
- // Make sure that socket has been opened.
- if(! socket_)
- socket_.open(
- get_message()->get_bus_device_name());
-
- uint32_t can_id = get_message()->get_id();
-
- struct utils::simple_bcm_msg bcm_msg;
- struct can_frame cfd;
-
- memset(&cfd, 0, sizeof(cfd));
- memset(&bcm_msg.msg_head, 0, sizeof(bcm_msg.msg_head));
- float val = (float)(1 << bit_size_)-1;
- struct timeval freq = frequency_.get_timeval_from_period();
-
- bcm_msg.msg_head.opcode = RX_SETUP;
- bcm_msg.msg_head.can_id = can_id;
- bcm_msg.msg_head.flags = SETTIMER|RX_NO_AUTOTIMER;
- bcm_msg.msg_head.ival2.tv_sec = freq.tv_sec ;
- bcm_msg.msg_head.ival2.tv_usec = freq.tv_usec;
- bcm_msg.msg_head.nframes = 1;
- bitfield_encode_float(val,
- bit_position_,
- bit_size_,
- factor_,
- offset_,
- cfd.data,
- CAN_MAX_DLEN);
-
- bcm_msg.frames = cfd;
-
- if(socket_ << bcm_msg)
- return 0;
- return -1;
-}
diff --git a/CAN-binder/low-can-binding/can/can-signals.hpp b/CAN-binder/low-can-binding/can/can-signals.hpp
index b8fbe4ef..2d67f273 100644
--- a/CAN-binder/low-can-binding/can/can-signals.hpp
+++ b/CAN-binder/low-can-binding/can/can-signals.hpp
@@ -76,7 +76,6 @@ class can_signal_t
{
private:
can_message_definition_t* parent_; /*!< parent_ - pointer to the parent message definition holding this signal*/
- utils::socketcan_bcm_t socket_; /*!< socket_ - Specific BCM socket that filter the signal read from CAN device */
std::string generic_name_; /*!< generic_name_ - The name of the signal to be output.*/
static std::string prefix_; /*!< prefix_ - generic_name_ will be prefixed with it. It has to reflect the used protocol.
* which make easier to sort message when the come in.*/
@@ -127,7 +126,6 @@ public:
SignalEncoder encoder,
bool received);
- utils::socketcan_bcm_t get_socket() const;
can_message_definition_t* get_message() const;
const std::string get_generic_name() const;
const std::string get_name() const;
@@ -155,6 +153,4 @@ public:
void set_received(bool r);
void set_last_value(float val);
void set_timestamp(uint64_t timestamp);
-
- int create_rx_filter();
};