From d2d2620bbb8f0aad0181e67e9f6affc3d6cb4a51 Mon Sep 17 00:00:00 2001 From: Romain Forlot Date: Wed, 26 Jun 2019 10:34:04 +0200 Subject: Change can_message_t class usage for new j1939 This commit transforms the class can_message_t as the base class and creates two derived classes: j1939_message_t and can_message_t. Bug-AGL: SPEC-2386 Change-Id: I6d3afd8e4f5abff2cd0ec4e9910bd52a2893de76 Signed-off-by: Arthur Guyader Signed-off-by: Stephane Desneux Signed-off-by: Romain Forlot --- low-can-binding/CMakeLists.txt | 9 +- low-can-binding/binding/low-can-cb.cpp | 10 +- low-can-binding/binding/low-can-subscription.cpp | 133 ++++++++----- low-can-binding/binding/low-can-subscription.hpp | 12 +- low-can-binding/can/can-bus-device.hpp | 2 +- low-can-binding/can/can-bus.cpp | 50 ++--- low-can-binding/can/can-bus.hpp | 13 +- low-can-binding/can/can-decoder.cpp | 4 +- low-can-binding/can/can-decoder.hpp | 6 +- low-can-binding/can/can-encoder.hpp | 2 +- low-can-binding/can/can-message-definition.cpp | 14 +- low-can-binding/can/can-message-definition.hpp | 5 +- low-can-binding/can/can-message.cpp | 217 ---------------------- low-can-binding/can/can-message.hpp | 72 ------- low-can-binding/can/can-signals.cpp | 2 +- low-can-binding/can/can-signals.hpp | 2 +- low-can-binding/can/message/can-message.cpp | 194 +++++++++++++++++++ low-can-binding/can/message/can-message.hpp | 49 +++++ low-can-binding/can/message/j1939-message.cpp | 163 ++++++++++++++++ low-can-binding/can/message/j1939-message.hpp | 73 ++++++++ low-can-binding/can/message/message.cpp | 96 ++++++++++ low-can-binding/can/message/message.hpp | 86 +++++++++ low-can-binding/diagnostic/diagnostic-manager.cpp | 30 +-- low-can-binding/diagnostic/diagnostic-manager.hpp | 6 +- low-can-binding/diagnostic/diagnostic-message.hpp | 2 +- low-can-binding/utils/socketcan-bcm.cpp | 23 +-- low-can-binding/utils/socketcan-bcm.hpp | 20 +- low-can-binding/utils/socketcan-j1939.cpp | 4 +- low-can-binding/utils/socketcan.hpp | 8 +- 29 files changed, 865 insertions(+), 442 deletions(-) delete mode 100644 low-can-binding/can/can-message.cpp delete mode 100644 low-can-binding/can/can-message.hpp create mode 100644 low-can-binding/can/message/can-message.cpp create mode 100644 low-can-binding/can/message/can-message.hpp create mode 100644 low-can-binding/can/message/j1939-message.cpp create mode 100644 low-can-binding/can/message/j1939-message.hpp create mode 100644 low-can-binding/can/message/message.cpp create mode 100644 low-can-binding/can/message/message.hpp diff --git a/low-can-binding/CMakeLists.txt b/low-can-binding/CMakeLists.txt index 296fa7d4..5c0ef07e 100644 --- a/low-can-binding/CMakeLists.txt +++ b/low-can-binding/CMakeLists.txt @@ -30,7 +30,8 @@ PROJECT_TARGET_ADD(low-can) can/can-bus.cpp can/can-message-set.cpp can/can-message-definition.cpp - can/can-message.cpp + can/message/message.cpp + can/message/can-message.cpp can/can-signals.cpp can/can-decoder.cpp can/can-encoder.cpp @@ -44,11 +45,13 @@ PROJECT_TARGET_ADD(low-can) #utils/socketcan-raw.cpp utils/socketcan-bcm.cpp utils/config-parser.cpp - binding/application-generated.cpp) + binding/application-generated.cpp + ) if(WITH_FEATURE_J1939) set(SOURCES_J1939 - # Empty for the moment to not break the build until all modifications are set + can/message/j1939-message.cpp + utils/socketcan-j1939.cpp ) endif() diff --git a/low-can-binding/binding/low-can-cb.cpp b/low-can-binding/binding/low-can-cb.cpp index 6b869073..9515424a 100644 --- a/low-can-binding/binding/low-can-cb.cpp +++ b/low-can-binding/binding/low-can-cb.cpp @@ -32,7 +32,7 @@ #include "../can/can-encoder.hpp" #include "../can/can-bus.hpp" #include "../can/can-signals.hpp" -#include "../can/can-message.hpp" +#include "../can/message/message.hpp" #include "../utils/signals.hpp" #include "../diagnostic/diagnostic-message.hpp" #include "../utils/openxc-utils.hpp" @@ -70,12 +70,12 @@ void on_no_clients(std::shared_ptr can_subscription, std s.erase(it); } -static void push_n_notify(std::shared_ptr m) +static void push_n_notify(std::shared_ptr m) { can_bus_t& cbm = application_t::instance().get_can_bus_manager(); { std::lock_guard can_message_lock(cbm.get_can_message_mutex()); - cbm.push_new_can_message(*m); + cbm.push_new_can_message(m); } cbm.get_new_can_message_cv().notify_one(); } @@ -88,7 +88,7 @@ int read_message(sd_event_source *event_source, int fd, uint32_t revents, void * if ((revents & EPOLLIN) != 0) { std::shared_ptr s = can_subscription->get_socket(); - std::shared_ptr message = s->read_message(); + std::shared_ptr message = s->read_message(); // Sure we got a valid CAN message ? if (! message->get_id() == 0 && ! message->get_length() == 0) @@ -413,7 +413,7 @@ static int send_frame(struct canfd_frame& cfd, const std::string& bus_name) if( cd.count(bus_name) == 0) {cd[bus_name] = std::make_shared(low_can_subscription_t());} - return cd[bus_name]->tx_send(cfd, bus_name); + return cd[bus_name]->tx_send(*cd[bus_name], cfd, bus_name); } static void write_raw_frame(afb_req_t request, const std::string& bus_name, json_object *json_value) diff --git a/low-can-binding/binding/low-can-subscription.cpp b/low-can-binding/binding/low-can-subscription.cpp index 09e9b807..27f4ebba 100644 --- a/low-can-binding/binding/low-can-subscription.cpp +++ b/low-can-binding/binding/low-can-subscription.cpp @@ -19,6 +19,11 @@ #include "low-can-subscription.hpp" #include "application.hpp" #include "canutil/write.h" +#include "../utils/socketcan-bcm.hpp" + +#ifdef USE_FEATURE_J1939 + #include "../utils/socketcan-j1939.hpp" +#endif low_can_subscription_t::low_can_subscription_t() : index_{-1}, @@ -243,9 +248,9 @@ int low_can_subscription_t::open_socket(const std::string& bus_name) /// /// @returns a bcm_msg with the msg_head parts set and can_frame /// zeroed. -struct utils::bcm_msg low_can_subscription_t::make_bcm_head(uint32_t opcode, uint32_t can_id, uint32_t flags, const struct timeval& timeout, const struct timeval& frequency_thinning) const +struct bcm_msg low_can_subscription_t::make_bcm_head(uint32_t opcode, uint32_t can_id, uint32_t flags, const struct timeval& timeout, const struct timeval& frequency_thinning) const { - struct utils::bcm_msg bcm_msg; + struct bcm_msg bcm_msg; ::memset(&bcm_msg, 0, sizeof(bcm_msg)); bcm_msg.msg_head.opcode = opcode; @@ -262,7 +267,7 @@ struct utils::bcm_msg low_can_subscription_t::make_bcm_head(uint32_t opcode, uin /// @brief Take an existing bcm_msg struct and add a can_frame. /// Currently only 1 uniq can_frame can be added, it's not possible to build /// a multiplexed message with several can_frame. -void low_can_subscription_t::add_one_bcm_frame(struct canfd_frame& cfd, struct utils::bcm_msg& bcm_msg) const +void low_can_subscription_t::add_one_bcm_frame(struct canfd_frame& cfd, struct bcm_msg& bcm_msg) const { struct can_frame cf; @@ -278,17 +283,31 @@ void low_can_subscription_t::add_one_bcm_frame(struct canfd_frame& cfd, struct u bcm_msg.msg_head.nframes++; } +#ifdef USE_FEATURE_J1939 +int low_can_subscription_t::create_rx_filter_j1939(low_can_subscription_t &subscription, std::shared_ptr sig) +{ + subscription.can_signal_= sig; + + // Make sure that socket is opened. + if(subscription.open_socket() < 0) + { + return -1; + } + return 0; +} +#endif + /// @brief Create a RX_SETUP receive job to be used by the BCM socket for a CAN signal /// subscription /// /// @return 0 if ok else -1 -int low_can_subscription_t::create_rx_filter(std::shared_ptr sig) +int low_can_subscription_t::create_rx_filter_can(low_can_subscription_t &subscription, std::shared_ptr sig) { uint32_t flags; float val; struct timeval freq, timeout = {0, 0}; struct canfd_frame cfd; - can_signal_= sig; + subscription.can_signal_= sig; if (sig->get_message()->is_fd()) { @@ -300,45 +319,83 @@ int low_can_subscription_t::create_rx_filter(std::shared_ptr sig) flags = SETTIMER|RX_NO_AUTOTIMER; cfd.len = CAN_MAX_DLEN; } - val = (float)(1 << can_signal_->get_bit_size()) - 1; + val = (float)(1 << subscription.can_signal_->get_bit_size()) - 1; if(! bitfield_encode_float(val, - can_signal_->get_bit_position(), - can_signal_->get_bit_size(), + subscription.can_signal_->get_bit_position(), + subscription.can_signal_->get_bit_size(), 1, - can_signal_->get_offset(), + subscription.can_signal_->get_offset(), cfd.data, cfd.len)) return -1; - frequency_clock_t f = event_filter_.frequency == 0 ? can_signal_->get_frequency() : frequency_clock_t(event_filter_.frequency); + frequency_clock_t f = subscription.event_filter_.frequency == 0 ? subscription.can_signal_->get_frequency() : frequency_clock_t(subscription.event_filter_.frequency); freq = f.get_timeval_from_period(); - utils::bcm_msg bcm_msg = make_bcm_head(RX_SETUP, can_signal_->get_message()->get_id(), flags, timeout, freq); - add_one_bcm_frame(cfd, bcm_msg); + struct bcm_msg bcm_msg = subscription.make_bcm_head(RX_SETUP, subscription.can_signal_->get_message()->get_id(), flags, timeout, freq); + subscription.add_one_bcm_frame(cfd, bcm_msg); - return create_rx_filter(bcm_msg); + return create_rx_filter_bcm(subscription, bcm_msg); +} + +int low_can_subscription_t::create_rx_filter(std::shared_ptr sig) +{ + #ifdef USE_FEATURE_J1939 + if(sig->get_message()->is_j1939()) + { + return low_can_subscription_t::create_rx_filter_j1939(*this, sig); + } + else + { + #endif + return low_can_subscription_t::create_rx_filter_can(*this, sig); + #ifdef USE_FEATURE_J1939 + } + #endif +} + + +/// @brief Create a RX_SETUP receive job to be used by the BCM socket for a +/// diagnostic message subscription. +/// +/// @return 0 if ok else -1 +int low_can_subscription_t::create_rx_filter(std::shared_ptr sig) +{ + diagnostic_message_.push_back(sig); + + struct timeval freq = frequency_clock_t(event_filter_.frequency).get_timeval_from_period(); + //struct timeval timeout = frequency_clock_t(10).get_timeval_from_period(); + struct timeval timeout = {0,0}; + + struct bcm_msg bcm_msg = make_bcm_head(RX_SETUP, OBD2_FUNCTIONAL_BROADCAST_ID, SETTIMER|RX_NO_AUTOTIMER|RX_FILTER_ID, timeout, freq); + return create_rx_filter_bcm(*this, bcm_msg); } /// @brief Create a RX_SETUP receive job used by the BCM socket directly from /// a bcm_msg. The method should not be used directly but rather through the -/// two previous method with can_signal_t or diagnostic_message_t object. +/// two previous method with signal_t or diagnostic_message_t object. /// /// If the CAN arbitration ID is the OBD2 functional broadcast id the subscribed /// to the 8 classics OBD2 functional response ID /// /// @return 0 if ok else -1 -int low_can_subscription_t::create_rx_filter(utils::bcm_msg& bcm_msg) +int low_can_subscription_t::create_rx_filter_bcm(low_can_subscription_t &subscription, struct bcm_msg& bcm_msg) { // Make sure that socket is opened. - if(open_socket() < 0) + if(subscription.open_socket() < 0) {return -1;} // If it's not an OBD2 CAN ID then just add a simple RX_SETUP job // else monitor all standard 8 CAN OBD2 ID response. + + std::shared_ptr msg = std::make_shared(); + + msg->set_bcm_msg(bcm_msg); + if(bcm_msg.msg_head.can_id != OBD2_FUNCTIONAL_BROADCAST_ID) { - socket_->write_message(bcm_msg); - if(! socket_) + subscription.socket_->write_message(msg); + if(! subscription.socket_) return -1; } else @@ -346,46 +403,34 @@ int low_can_subscription_t::create_rx_filter(utils::bcm_msg& bcm_msg) for(uint8_t i = 0; i < 8; i++) { bcm_msg.msg_head.can_id = OBD2_FUNCTIONAL_RESPONSE_START + i; - - socket_->write_message(bcm_msg); - if(! socket_) + msg->set_bcm_msg(bcm_msg); + subscription.socket_->write_message(msg); + if(! subscription.socket_) return -1; } } - return 0; } -/// @brief Create a RX_SETUP receive job to be used by the BCM socket for a -/// diagnostic message subscription. -/// -/// @return 0 if ok else -1 -int low_can_subscription_t::create_rx_filter(std::shared_ptr sig) -{ - diagnostic_message_.push_back(sig); - - struct timeval freq = frequency_clock_t(event_filter_.frequency).get_timeval_from_period(); - //struct timeval timeout = frequency_clock_t(10).get_timeval_from_period(); - struct timeval timeout = {0,0}; - - utils::bcm_msg bcm_msg = make_bcm_head(RX_SETUP, OBD2_FUNCTIONAL_BROADCAST_ID, SETTIMER|RX_NO_AUTOTIMER|RX_FILTER_ID, timeout, freq); - return create_rx_filter(bcm_msg); -} - /// @brief Creates a TX_SEND job that is used by the BCM socket to /// send a message /// /// @return 0 if ok else -1 -int low_can_subscription_t::tx_send(struct canfd_frame& cfd, const std::string& bus_name) +int low_can_subscription_t::tx_send(low_can_subscription_t &subscription, struct canfd_frame& cfd, const std::string& bus_name) { - struct utils::bcm_msg bcm_msg = make_bcm_head(TX_SEND, cfd.can_id); - add_one_bcm_frame(cfd, bcm_msg); + struct bcm_msg bcm_msg = subscription.make_bcm_head(TX_SEND, cfd.can_id); + subscription.add_one_bcm_frame(cfd, bcm_msg); - if(open_socket(bus_name) < 0) + if(subscription.open_socket(bus_name) < 0) {return -1;} - socket_->write_message(bcm_msg); - if(! socket_) + + std::shared_ptr msg = std::make_shared(); + + msg->set_bcm_msg(bcm_msg); + + subscription.socket_->write_message(msg); + if(! subscription.socket_.get()) return -1; return 0; diff --git a/low-can-binding/binding/low-can-subscription.hpp b/low-can-binding/binding/low-can-subscription.hpp index ded600c5..c9cd2e3f 100644 --- a/low-can-binding/binding/low-can-subscription.hpp +++ b/low-can-binding/binding/low-can-subscription.hpp @@ -54,7 +54,7 @@ private: std::shared_ptr can_signal_; ///< can_signal_ - the CAN signal subscribed std::vector > diagnostic_message_; ///< diagnostic_message_ - diagnostic messages meant to receive OBD2 /// responses. Normal diagnostic request and response are not tested for now. - std::shared_ptr socket_; ///< socket_ - socket_ that receives CAN messages. + std::shared_ptr socket_; ///< socket_ - socket_ that receives CAN messages. int set_event(); @@ -89,14 +89,16 @@ public: void set_min(float min); void set_max(float max); - struct utils::bcm_msg make_bcm_head(uint32_t opcode, uint32_t can_id = 0, uint32_t flags = 0, const struct timeval& timeout = {0,0}, const struct timeval& frequency_thinning = {0,0}) const; - void add_one_bcm_frame(struct canfd_frame& cfd, struct utils::bcm_msg& bcm_msg) const; + struct bcm_msg make_bcm_head(uint32_t opcode, uint32_t can_id = 0, uint32_t flags = 0, const struct timeval& timeout = {0,0}, const struct timeval& frequency_thinning = {0,0}) const; + void add_one_bcm_frame(struct canfd_frame& cfd, struct bcm_msg& bcm_msg) const; int open_socket(const std::string& bus_name = ""); int create_rx_filter(std::shared_ptr sig); int create_rx_filter(std::shared_ptr sig); - int create_rx_filter(utils::bcm_msg& bcm_msg); + static int create_rx_filter_can(low_can_subscription_t &subscription, std::shared_ptr sig); + static int create_rx_filter_j1939(low_can_subscription_t &subscription, std::shared_ptr sig); + static int create_rx_filter_bcm(low_can_subscription_t &subscription, struct bcm_msg& bcm_msg); - int tx_send(struct canfd_frame& cfd, const std::string& bus_name); + static int tx_send(low_can_subscription_t &subscription, struct canfd_frame& cfd, const std::string& bus_name); }; diff --git a/low-can-binding/can/can-bus-device.hpp b/low-can-binding/can/can-bus-device.hpp index c53e5f23..a5c9493c 100644 --- a/low-can-binding/can/can-bus-device.hpp +++ b/low-can-binding/can/can-bus-device.hpp @@ -26,7 +26,7 @@ #include #include "openxc.pb.h" -#include "can-message.hpp" +#include "message/can-message.hpp" #include "../utils/config-parser.hpp" #include "../binding/low-can-subscription.hpp" diff --git a/low-can-binding/can/can-bus.cpp b/low-can-binding/can/can-bus.cpp index e798618d..739e865c 100644 --- a/low-can-binding/can/can-bus.cpp +++ b/low-can-binding/can/can-bus.cpp @@ -81,9 +81,9 @@ bool can_bus_t::apply_filter(const openxc_VehicleMessage& vehicle_message, std:: /// @param[in] can_message - a single CAN message from the CAN socket read, to be decode. /// /// @return How many signals has been decoded. -void can_bus_t::process_can_signals(const can_message_t& can_message, std::map >& s) +void can_bus_t::process_signals(const message_t& message, std::map >& s) { - int subscription_id = can_message.get_sub_id(); + int subscription_id = message.get_sub_id(); openxc_DynamicField decoded_message; openxc_VehicleMessage vehicle_message; @@ -93,9 +93,9 @@ void can_bus_t::process_can_signals(const can_message_t& can_message, std::map sig = s[subscription_id]; - decoded_message = decoder_t::translate_signal(*sig->get_can_signal(), can_message, &send); + decoded_message = decoder_t::translate_signal(*sig->get_can_signal(), message, &send); openxc_SimpleMessage s_message = build_SimpleMessage(sig->get_name(), decoded_message); - vehicle_message = build_VehicleMessage(s_message, can_message.get_timestamp()); + vehicle_message = build_VehicleMessage(s_message, message.get_timestamp()); if(send && apply_filter(vehicle_message, sig)) { @@ -114,13 +114,13 @@ void can_bus_t::process_can_signals(const can_message_t& can_message, std::map >& s) +void can_bus_t::process_diagnostic_signals(diagnostic_manager_t& manager, std::shared_ptr message, std::map >& s) { - int subscription_id = can_message.get_sub_id(); + int subscription_id = message->get_sub_id(); - openxc_VehicleMessage vehicle_message = manager.find_and_decode_adr(can_message); - if (can_message.get_timestamp()) - {vehicle_message.timestamp = can_message.get_timestamp();} + openxc_VehicleMessage vehicle_message = manager.find_and_decode_adr(message); + if (message->get_timestamp()) + {vehicle_message.timestamp = message->get_timestamp();} if( (vehicle_message.has_simple_message && vehicle_message.simple_message.has_name) && s.find(subscription_id) != s.end() && afb_event_is_valid(s[subscription_id]->get_event())) { @@ -155,16 +155,18 @@ void can_bus_t::can_decode_message() new_can_message_cv_.wait(can_message_lock); while(!can_message_q_.empty()) { - const can_message_t can_message = next_can_message(); + std::shared_ptr message = next_can_message(); can_message_lock.unlock(); { std::lock_guard subscribed_signals_lock(sm.get_subscribed_signals_mutex()); std::map >& s = sm.get_subscribed_signals(); - if(application_t::instance().get_diagnostic_manager().is_diagnostic_response(can_message)) - {process_diagnostic_signals(application_t::instance().get_diagnostic_manager(), can_message, s);} + if(application_t::instance().get_diagnostic_manager().is_diagnostic_response(message)) + { + process_diagnostic_signals(application_t::instance().get_diagnostic_manager(), message, s); + } else - {process_can_signals(can_message, s);} + {process_signals(*message, s);} } can_message_lock.lock(); } @@ -251,28 +253,28 @@ std::mutex& can_bus_t::get_can_message_mutex() /// @brief Return first can_message_t on the queue /// /// @return a can_message_t -const can_message_t can_bus_t::next_can_message() +std::shared_ptr can_bus_t::next_can_message() { - can_message_t can_msg; + std::shared_ptr msg; if(!can_message_q_.empty()) { - can_msg = can_message_q_.front(); + msg = can_message_q_.front(); can_message_q_.pop(); - AFB_DEBUG("Here is the next can message : id %X, length %X, data %02X%02X%02X%02X%02X%02X%02X%02X", can_msg.get_id(), can_msg.get_length(), - can_msg.get_data()[0], can_msg.get_data()[1], can_msg.get_data()[2], can_msg.get_data()[3], can_msg.get_data()[4], can_msg.get_data()[5], can_msg.get_data()[6], can_msg.get_data()[7]); - return can_msg; + std::string debug = msg->get_debug_message(); + AFB_DEBUG(debug.c_str()); + return msg; } - return can_msg; + return msg; } -/// @brief Push a can_message_t into the queue +/// @brief Push a message_t into the queue /// -/// @param[in] can_msg - the const reference can_message_t object to push into the queue -void can_bus_t::push_new_can_message(const can_message_t& can_msg) +/// @param[in] msg - the const reference message_t object to push into the queue +void can_bus_t::push_new_can_message(std::shared_ptr msg) { - can_message_q_.push(can_msg); + can_message_q_.push(msg); } /// @brief Return first openxc_VehicleMessage on the queue diff --git a/low-can-binding/can/can-bus.hpp b/low-can-binding/can/can-bus.hpp index f1eb690f..2a798ca6 100644 --- a/low-can-binding/can/can-bus.hpp +++ b/low-can-binding/can/can-bus.hpp @@ -24,9 +24,8 @@ #include #include #include - #include "openxc.pb.h" -#include "can-message.hpp" +#include "message/can-message.hpp" #include "../utils/config-parser.hpp" #include "../binding/low-can-subscription.hpp" @@ -47,8 +46,8 @@ private: utils::config_parser_t conf_file_; ///< configuration file handle used to initialize can_bus_dev_t objects. bool apply_filter(const openxc_VehicleMessage& vehicle_message, std::shared_ptr can_subscription); - void process_can_signals(const can_message_t& can_message, std::map >& s); - void process_diagnostic_signals(diagnostic_manager_t& manager, const can_message_t& can_message, std::map >& s); + void process_signals(const message_t& message, std::map >& s); + void process_diagnostic_signals(diagnostic_manager_t& manager, std::shared_ptr message, std::map >& s); void can_decode_message(); std::thread th_decoding_; ///< thread that will handle decoding a can frame @@ -60,7 +59,7 @@ private: 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_q_; ///< queue that will store can_message_t to be decoded + std::queue > can_message_q_; ///< queue that will store can_message_t to be 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_ @@ -80,8 +79,8 @@ public: void start_threads(); void stop_threads(); - const can_message_t next_can_message(); - void push_new_can_message(const can_message_t& can_msg); + std::shared_ptr next_can_message(); + void push_new_can_message(std::shared_ptr can_msg); std::mutex& get_can_message_mutex(); std::condition_variable& get_new_can_message_cv(); diff --git a/low-can-binding/can/can-decoder.cpp b/low-can-binding/can/can-decoder.cpp index 763bfcb0..a2bf411b 100644 --- a/low-can-binding/can/can-decoder.cpp +++ b/low-can-binding/can/can-decoder.cpp @@ -31,7 +31,7 @@ /// @return Returns the raw value of the signal parsed as a bitfield from the given byte /// array. /// -float decoder_t::parse_signal_bitfield(can_signal_t& signal, const can_message_t& message) +float decoder_t::parse_signal_bitfield(can_signal_t& signal, const message_t& message) { return bitfield_parse_float(message.get_data(), CAN_MESSAGE_SIZE, signal.get_bit_position(), signal.get_bit_size(), signal.get_factor(), @@ -144,7 +144,7 @@ openxc_DynamicField decoder_t::decode_state(can_signal_t& signal, float value, b /// The decoder returns an openxc_DynamicField, which may contain a number, /// string or boolean. /// -openxc_DynamicField decoder_t::translate_signal(can_signal_t& signal, const can_message_t& message, bool* send) +openxc_DynamicField decoder_t::translate_signal(can_signal_t& signal, const message_t& message, bool* send) { float value = decoder_t::parse_signal_bitfield(signal, message); AFB_DEBUG("Decoded message from parse_signal_bitfield: %f", value); diff --git a/low-can-binding/can/can-decoder.hpp b/low-can-binding/can/can-decoder.hpp index 7e7349db..ffc881f9 100644 --- a/low-can-binding/can/can-decoder.hpp +++ b/low-can-binding/can/can-decoder.hpp @@ -18,20 +18,20 @@ #pragma once #include "can-signals.hpp" -#include "can-message.hpp" +#include "message/can-message.hpp" #include "openxc.pb.h" class decoder_t { public: - static float parse_signal_bitfield(can_signal_t& signal, const can_message_t& message); + static float parse_signal_bitfield(can_signal_t& signal, const message_t& message); static openxc_DynamicField decode_state(can_signal_t& signal, float value, bool* send); static openxc_DynamicField decode_boolean(can_signal_t& signal, float value, bool* send); static openxc_DynamicField decode_ignore(can_signal_t& signal, float value, bool* send); static openxc_DynamicField decode_noop(can_signal_t& signal, float value, bool* send); - static openxc_DynamicField translate_signal(can_signal_t& signal, const can_message_t& messag, bool* send); + static openxc_DynamicField translate_signal(can_signal_t& signal, const message_t& messag, bool* send); static openxc_DynamicField decode_signal(can_signal_t& signal, const can_message_t& message, bool* send); diff --git a/low-can-binding/can/can-encoder.hpp b/low-can-binding/can/can-encoder.hpp index f8398836..294b1674 100644 --- a/low-can-binding/can/can-encoder.hpp +++ b/low-can-binding/can/can-encoder.hpp @@ -18,7 +18,7 @@ #pragma once #include "can-signals.hpp" -#include "can-message.hpp" +#include "message/can-message.hpp" #include "openxc.pb.h" class encoder_t diff --git a/low-can-binding/can/can-message-definition.cpp b/low-can-binding/can/can-message-definition.cpp index 483f4a36..92040c31 100644 --- a/low-can-binding/can/can-message-definition.cpp +++ b/low-can-binding/can/can-message-definition.cpp @@ -54,6 +54,18 @@ bool can_message_definition_t::is_fd() const return is_fd_; } +bool can_message_definition_t::is_j1939() const +{ + if(format_ == can_message_format_t::J1939) + { + return true; + } + else + { + return false; + } +} + std::vector >& can_message_definition_t::get_can_signals() { return can_signals_; @@ -64,7 +76,7 @@ void can_message_definition_t::set_parent(can_message_set_t* parent) parent_= parent; } -void can_message_definition_t::set_last_value(const can_message_t& cm) +void can_message_definition_t::set_last_value(const message_t& cm) { last_value_= cm.get_data_vector(); } diff --git a/low-can-binding/can/can-message-definition.hpp b/low-can-binding/can/can-message-definition.hpp index b9025cbb..689c654c 100644 --- a/low-can-binding/can/can-message-definition.hpp +++ b/low-can-binding/can/can-message-definition.hpp @@ -29,7 +29,7 @@ #include #include "can-signals.hpp" -#include "can-message.hpp" +#include "message/can-message.hpp" #include "can-message-set.hpp" #include "../utils/timer.hpp" @@ -72,8 +72,9 @@ public: const std::string get_bus_device_name() const; uint32_t get_id() const; bool is_fd() const; + bool is_j1939() const; std::vector >& get_can_signals(); void set_parent(can_message_set_t* parent); - void set_last_value(const can_message_t& cm); + void set_last_value(const message_t& cm); }; diff --git a/low-can-binding/can/can-message.cpp b/low-can-binding/can/can-message.cpp deleted file mode 100644 index 074f5990..00000000 --- a/low-can-binding/can/can-message.cpp +++ /dev/null @@ -1,217 +0,0 @@ -/* - * Copyright (C) 2015, 2016 "IoT.bzh" - * Author "Romain Forlot" - * - * 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 "can-message.hpp" - -#include - -#include "../binding/low-can-hat.hpp" - -/// -/// @brief Class constructor -/// -/// can_message_t class constructor. -/// -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}, - sub_id_{-1} -{} - -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& data, - uint64_t timestamp) - : maxdlen_{maxdlen}, - id_{id}, - length_{length}, - format_{format}, - rtr_flag_{rtr_flag}, - flags_{flags}, - data_{data}, - timestamp_{timestamp}, - sub_id_{-1} -{} - -/// -/// @brief Retrieve id_ member value. -/// -/// @return id_ class member -/// -uint32_t can_message_t::get_id() const -{ - return id_; -} - -int can_message_t::get_sub_id() const -{ - return sub_id_; -} - -/// -/// @brief Retrieve data_ member value. -/// -/// @return pointer to the first element -/// of class member data_ -/// -const uint8_t* can_message_t::get_data() const -{ - return data_.data(); -} - -/// -/// @brief Retrieve data_ member whole vector -/// -/// @return the vector as is -/// -const std::vector can_message_t::get_data_vector() const -{ - return data_; -} - -/// -/// @brief Retrieve length_ member value. -/// -/// @return length_ class member -/// -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_; -} - -/// @brief Control whether the object is correctly initialized -/// to be sent over the CAN bus -/// -/// @return True if object correctly initialized and false if not. -bool can_message_t::is_correct_to_send() -{ - if (id_ != 0 && length_ != 0 && format_ != can_message_format_t::INVALID) - { - int i; - for(i=0;i data; - - switch(nbytes) - { - case CANFD_MTU: - AFB_DEBUG("Got an CAN FD frame"); - maxdlen = CANFD_MAX_DLEN; - break; - case CAN_MTU: - AFB_DEBUG("Got a legacy CAN frame"); - maxdlen = CAN_MAX_DLEN; - break; - default: - AFB_ERROR("unsupported CAN frame"); - break; - } - - if (frame.can_id & CAN_ERR_FLAG) - { - format = can_message_format_t::INVALID; - id = frame.can_id & (CAN_ERR_MASK|CAN_ERR_FLAG); - } - else if (frame.can_id & CAN_EFF_FLAG) - { - format = can_message_format_t::EXTENDED; - id = frame.can_id & CAN_EFF_MASK; - } - else - { - format = can_message_format_t::STANDARD; - id = frame.can_id & CAN_SFF_MASK; - } - - /* Overwrite length_ if RTR flags is detected. - * standard CAN frames may have RTR enabled. There are no ERR frames with RTR */ - if (frame.can_id & CAN_RTR_FLAG) - { - rtr_flag = true; - if(frame.len && frame.len <= CAN_MAX_DLC) - { - if(rtr_flag) - length = frame.len& 0xF; - else - { - length = (frame.len > maxdlen) ? maxdlen : frame.len; - } - } - } - else - { - length = (frame.len > maxdlen) ? maxdlen : frame.len; - - /* Flags field only present for CAN FD frames*/ - if(maxdlen == CANFD_MAX_DLEN) - flags = frame.flags & 0xF; - - if (data.capacity() < maxdlen) - data.reserve(maxdlen); - int i; - - data.clear(); - /* maxdlen_ is now set at CAN_MAX_DLEN or CANFD_MAX_DLEN, respectively 8 and 64 bytes*/ - for(i=0;i - * - * 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 -#include -#include -#include - -#include "../utils/timer.hpp" - -#define CAN_MESSAGE_SIZE 8 - -/** - * @enum can_message_format_t - * @brief The ID format for a CAN message. - */ -enum class can_message_format_t { - STANDARD, ///< STANDARD - standard 11-bit CAN arbitration ID. */ - EXTENDED, ///< EXTENDED - an extended frame, with a 29-bit arbitration ID. */ - INVALID, ///< INVALID - INVALID code used at initialization to signify that it isn't usable'*/ -}; - -/// @class can_message_t -/// -/// @brief A compact representation of a single CAN message, meant to be used in in/out -/// buffers. It is a wrapper of a can_frame struct with some sugar around it for binding purposes. -class can_message_t { -private: - uint8_t maxdlen_; ///< maxdlen_ - Max data length deduce from number of bytes read from the socket.*/ - uint32_t id_; ///< id_ - The ID of the message. */ - uint8_t length_; ///< length_ - the length of the data array (max 8). */ - can_message_format_t format_; ///< format_ - the format of the message's ID.*/ - bool rtr_flag_; ///< rtr_flag_ - Telling if the frame has RTR flag positionned. Then frame hasn't data field*/ - uint8_t flags_; ///< flags_ - flags of a CAN FD frame. Needed if we catch FD frames.*/ - std::vector 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& data, uint64_t timestamp); - - uint32_t get_id() const; - int get_sub_id() const; - const uint8_t* get_data() const; - const std::vector get_data_vector() const; - uint8_t get_length() const; - uint64_t get_timestamp() const; - - void set_sub_id(int sub_id); - void set_timestamp(uint64_t timestamp); - - bool is_correct_to_send(); - - static can_message_t convert_from_frame(const canfd_frame& frame, size_t nbytes, uint64_t timestamp); -}; diff --git a/low-can-binding/can/can-signals.cpp b/low-can-binding/can/can-signals.cpp index 14881e89..76a9665f 100644 --- a/low-can-binding/can/can-signals.cpp +++ b/low-can-binding/can/can-signals.cpp @@ -22,7 +22,7 @@ #include "../binding/application.hpp" #include "../utils/signals.hpp" #include "can-decoder.hpp" -#include "can-message.hpp" +#include "message/can-message.hpp" #include "can-bus.hpp" #include "../diagnostic/diagnostic-message.hpp" #include "canutil/write.h" diff --git a/low-can-binding/can/can-signals.hpp b/low-can-binding/can/can-signals.hpp index 2fd7cecf..9ac55081 100644 --- a/low-can-binding/can/can-signals.hpp +++ b/low-can-binding/can/can-signals.hpp @@ -27,7 +27,7 @@ #include "can-message-definition.hpp" #include "../utils/timer.hpp" #include "../utils/socketcan-bcm.hpp" -#include "can-message.hpp" +#include "message/can-message.hpp" #include "../diagnostic/diagnostic-message.hpp" #define MESSAGE_SET_ID 0 diff --git a/low-can-binding/can/message/can-message.cpp b/low-can-binding/can/message/can-message.cpp new file mode 100644 index 00000000..91f66252 --- /dev/null +++ b/low-can-binding/can/message/can-message.cpp @@ -0,0 +1,194 @@ +/* + * Copyright (C) 2015, 2016 "IoT.bzh" + * Author "Romain Forlot" + * + * 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 "./can-message.hpp" + +#include + +#include "../../binding/low-can-hat.hpp" + +/// +/// @brief Class constructor +/// +/// can_message_t class constructor. +/// +can_message_t::can_message_t() + : message_t(), + maxdlen_{0}, + id_{0}, + rtr_flag_{false}, + flags_{0} +{} + +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& data, + uint64_t timestamp) + : message_t(length, format, data, timestamp), + maxdlen_{maxdlen}, + id_{id}, + rtr_flag_{rtr_flag}, + flags_{flags} +{} + +/// +/// @brief Retrieve id_ member value. +/// +/// @return id_ class member +/// +uint32_t can_message_t::get_id() const +{ + return id_; +} + + +/// @brief Control whether the object is correctly initialized +/// to be sent over the CAN bus +/// +/// @return True if object correctly initialized and false if not. +bool can_message_t::is_correct_to_send() +{ + if (id_ != 0 && length_ != 0 && format_ != can_message_format_t::INVALID) + { + int i; + for(i=0;i can_message_t::convert_from_frame(const struct canfd_frame& frame, size_t nbytes, uint64_t timestamp) +{ + uint8_t maxdlen = 0, length = 0, flags = 0; + uint32_t id; + can_message_format_t format; + bool rtr_flag; + std::vector data; + + switch(nbytes) + { + case CANFD_MTU: + AFB_DEBUG("Got an CAN FD frame"); + maxdlen = CANFD_MAX_DLEN; + break; + case CAN_MTU: + AFB_DEBUG("Got a legacy CAN frame"); + maxdlen = CAN_MAX_DLEN; + break; + default: + AFB_ERROR("unsupported CAN frame"); + break; + } + + if (frame.can_id & CAN_ERR_FLAG) + { + format = can_message_format_t::INVALID; + id = frame.can_id & (CAN_ERR_MASK|CAN_ERR_FLAG); + } + else if (frame.can_id & CAN_EFF_FLAG) + { + format = can_message_format_t::EXTENDED; + id = frame.can_id & CAN_EFF_MASK; + } + else + { + format = can_message_format_t::STANDARD; + id = frame.can_id & CAN_SFF_MASK; + } + + /* Overwrite length_ if RTR flags is detected. + * standard CAN frames may have RTR enabled. There are no ERR frames with RTR */ + if (frame.can_id & CAN_RTR_FLAG) + { + rtr_flag = true; + if(frame.len && frame.len <= CAN_MAX_DLC) + { + if(rtr_flag) + length = frame.len& 0xF; + else + { + length = (frame.len > maxdlen) ? maxdlen : frame.len; + } + } + } + else + { + length = (frame.len > maxdlen) ? maxdlen : frame.len; + + /* Flags field only present for CAN FD frames*/ + if(maxdlen == CANFD_MAX_DLEN) + flags = frame.flags & 0xF; + + if (data.capacity() < maxdlen) + data.reserve(maxdlen); + int i; + + data.clear(); + /* maxdlen_ is now set at CAN_MAX_DLEN or CANFD_MAX_DLEN, respectively 8 and 64 bytes*/ + for(i=0;i(can_message_t(maxdlen, id, length, format, rtr_flag, flags, data, timestamp)); +} + + +bool can_message_t::is_set() +{ + return (id_ != 0 && length_ != 0); +} + +std::string can_message_t::get_debug_message() +{ + std::string ret = ""; + ret = ret + "Here is the next can message : id " + std::to_string(id_) + " length " + std::to_string(length_) + ", data "; + for (size_t i = 0; i < data_.size(); i++) + { + ret = ret + std::to_string(data_[i]); + } + + return ret; +} + +struct bcm_msg can_message_t::get_bcm_msg() +{ + return bcm_msg_; +} + +void can_message_t::set_bcm_msg(struct bcm_msg bcm_msg) +{ + bcm_msg_ = bcm_msg; +} + diff --git a/low-can-binding/can/message/can-message.hpp b/low-can-binding/can/message/can-message.hpp new file mode 100644 index 00000000..5aa302cc --- /dev/null +++ b/low-can-binding/can/message/can-message.hpp @@ -0,0 +1,49 @@ +/* + * Copyright (C) 2015, 2016 "IoT.bzh" + * Author "Romain Forlot" + * + * 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 "./message.hpp" + + + +/// @class can_message_t +/// +/// @brief A compact representation of a single CAN message, meant to be used in in/out +/// buffers. It is a wrapper of a can_frame struct with some sugar around it for binding purposes. +class can_message_t : public message_t { + private: + uint8_t maxdlen_; ///< maxdlen_ - Max data length deduce from number of bytes read from the socket.*/ + uint32_t id_; ///< id_ - The ID of the message. */ + bool rtr_flag_; ///< rtr_flag_ - Telling if the frame has RTR flag positionned. Then frame hasn't data field*/ + uint8_t flags_; ///< flags_ - flags of a CAN FD frame. Needed if we catch FD frames.*/ + struct bcm_msg bcm_msg_; + + 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& data, uint64_t timestamp); + + uint32_t get_id() const; + + static std::shared_ptr convert_from_frame(const canfd_frame& frame, size_t nbytes, uint64_t timestamp); + bool is_correct_to_send(); + bool is_set(); + struct bcm_msg get_bcm_msg(); + void set_bcm_msg(struct bcm_msg bcm_msg); + + std::string get_debug_message(); + +}; diff --git a/low-can-binding/can/message/j1939-message.cpp b/low-can-binding/can/message/j1939-message.cpp new file mode 100644 index 00000000..8269cbfa --- /dev/null +++ b/low-can-binding/can/message/j1939-message.cpp @@ -0,0 +1,163 @@ +/* + * Copyright (C) 2018, 2019 "IoT.bzh" + * Author "Arthur Guyader" + * + * 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 "./j1939-message.hpp" +#include +#include "../../binding/low-can-hat.hpp" + + +/// +/// @brief Class constructor +/// +/// j1939_message_t class constructor. +/// +j1939_message_t::j1939_message_t(): + message_t(), + name_{0}, + pgn_{0}, + addr_{0} +{} + +j1939_message_t::j1939_message_t(uint8_t length, + can_message_format_t format, + std::vector& data, + uint64_t timestamp, + name_t name, + pgn_t pgn, + uint8_t addr): + message_t(length, format, data, timestamp), + name_{name}, + pgn_{pgn}, + addr_{addr} +{} + +/// +/// @brief Retrieve name_ member value. +/// +/// @return name_ class member +/// +uint64_t j1939_message_t::get_name() const { + return name_; +} + +/// +/// @brief Retrieve pgn_ member value. +/// +/// @return pgn_ class member +/// +uint32_t j1939_message_t::get_pgn() const{ + return pgn_; +} + +/// +/// @brief Retrieve addr_ member value. +/// +/// @return addr_ class member +/// +uint8_t j1939_message_t::get_addr() const{ + return addr_; +} + + +/// @brief Take a sockaddr_can struct and array of data to initialize class members +/// +/// This is the preferred way to initialize class members. +/// +/// @param[in] addr - sockaddr_can to get pgn, name and addr +/// @param[in] data - array of data get from the j1939 socket +/// @param[in] nbytes - size of the array of data +/// @param[in] timestamp - timestamp of the message +/// +/// @return A j1939_message_t object fully initialized with sockaddr_can and data values. +std::shared_ptr j1939_message_t::convert_from_addr(struct sockaddr_can& addr, uint8_t (&data)[128],size_t nbytes, uint64_t timestamp) +{ + uint8_t length = 0; + can_message_format_t format; + std::vector dataVector; + + if(nbytes > J1939_MAX_DLEN) + { + AFB_DEBUG("Unsupported j1939 frame"); + format = can_message_format_t::INVALID; + } + else + { + AFB_DEBUG("Got a j1939 frame"); + format = can_message_format_t::J1939; + } + + length = (uint8_t) nbytes; + dataVector.reserve(length); + int i; + dataVector.clear(); + for(i=0;i(j1939_message_t(length, format, dataVector, timestamp,addr.can_addr.j1939.name,addr.can_addr.j1939.pgn,addr.can_addr.j1939.addr)); +} + +/// @brief Test if members pgn_ and length are set. +/// +/// @return boolean - true = set - false = not set +bool j1939_message_t::is_set() +{ + return (pgn_ != 0 && length_ != 0); +} + +/// @brief Generate a string with informations about the message +/// +/// @return Debug message with informations about members +std::string j1939_message_t::get_debug_message() +{ + std::string ret = ""; + ret = ret + "Here is the next j1939 message : pgn " + std::to_string(pgn_) + " length " + std::to_string(length_) + ", data "; + for (size_t i = 0; i < data_.size(); i++) + { + ret = ret + std::to_string(data_[i]); + } + return ret; +} + +/// +/// @brief Retrieve pgn_ member value. +/// +/// @return pgn_ class member +/// +uint32_t j1939_message_t::get_id() const +{ + AFB_WARNING("Prefer method get_pgn() for j1939 messages"); + return get_pgn(); +} + + +struct bcm_msg j1939_message_t::get_bcm_msg() +{ + AFB_WARNING("Not implemented"); + struct bcm_msg bcm_msg; + ::memset(&bcm_msg, 0, sizeof(struct bcm_msg)); + return bcm_msg; +} + +void j1939_message_t::set_bcm_msg(struct bcm_msg bcm_msg) +{ + AFB_WARNING("Not implemented"); +} diff --git a/low-can-binding/can/message/j1939-message.hpp b/low-can-binding/can/message/j1939-message.hpp new file mode 100644 index 00000000..74b625e0 --- /dev/null +++ b/low-can-binding/can/message/j1939-message.hpp @@ -0,0 +1,73 @@ +/* + * Copyright (C) 2018, 2019 "IoT.bzh" + * Author "Arthur Guyader" + * + * 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 +#include +#include "./message.hpp" + +#define J1939_MAX_MULTIPACKETS 255 +#define J1939_MAX_DLEN J1939_MAX_MULTIPACKETS * CAN_MAX_DLEN + +class j1939_message_t : public message_t +{ + private: + /* J1939 NAME + * + * bit 0-20 : Identity Number + * bit 21-31 : Manufacturer Code + * bit 32-34 : ECU Instance + * bit 35-39 : Function Instance + * bit 40-47 : Function + * bit 48 : Reserved + * bit 49-55 : Vehicle System + * bit 56-59 : Vehicle System Instance + * bit 60-62 : Industry Group + * bit 63 : Arbitrary Address Capable + */ + name_t name_; + + /* J1939 Parameter Group Number + * + * bit 0-7 : PDU Specific (PS) + * bit 8-15 : PDU Format (PF) + * bit 16 : Data Page (DP) + * bit 17 : Reserved (R) + * bit 19-31 : set to zero + */ + pgn_t pgn_; + + + /* J1939 Address + */ + uint8_t addr_; + + public: + j1939_message_t(); + j1939_message_t(uint8_t length, can_message_format_t format, std::vector& data, uint64_t timestamp, name_t name, pgn_t pgn, uint8_t addr); + uint64_t get_name() const; + uint32_t get_pgn() const; + uint8_t get_addr() const; + static std::shared_ptr convert_from_addr(struct sockaddr_can& addr, uint8_t (&data)[128], size_t nbytes, uint64_t timestamp); + bool is_set(); + std::string get_debug_message(); + uint32_t get_id() const; + struct bcm_msg get_bcm_msg(); + void set_bcm_msg(struct bcm_msg bcm_msg); +}; + + diff --git a/low-can-binding/can/message/message.cpp b/low-can-binding/can/message/message.cpp new file mode 100644 index 00000000..fe37e7ad --- /dev/null +++ b/low-can-binding/can/message/message.cpp @@ -0,0 +1,96 @@ +/* + * Copyright (C) 2015, 2016 "IoT.bzh" + * Author "Romain Forlot" + * + * 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 "./message.hpp" + +#include + +#include "../../binding/low-can-hat.hpp" + +/// +/// @brief Class constructor +/// +/// message_t class constructor. +/// +message_t::message_t() + : length_{0}, + format_{can_message_format_t::INVALID}, + timestamp_{0}, + sub_id_{-1} +{} + +message_t::message_t(uint8_t length, + can_message_format_t format, + std::vector& data, + uint64_t timestamp) + : length_{length}, + format_{format}, + data_{data}, + timestamp_{timestamp}, + sub_id_{-1} +{} + +int message_t::get_sub_id() const +{ + return sub_id_; +} + +/// +/// @brief Retrieve data_ member value. +/// +/// @return pointer to the first element +/// of class member data_ +/// +const uint8_t* message_t::get_data() const +{ + return data_.data(); +} + +/// +/// @brief Retrieve data_ member whole vector +/// +/// @return the vector as is +/// +const std::vector message_t::get_data_vector() const +{ + return data_; +} + +/// +/// @brief Retrieve length_ member value. +/// +/// @return length_ class member +/// +uint8_t message_t::get_length() const +{ + return length_; +} + +void message_t::set_sub_id(int sub_id) +{ + sub_id_ = sub_id; +} + +uint64_t message_t::get_timestamp() const +{ + return timestamp_; +} + +can_message_format_t message_t::get_msg_format() +{ + return format_; +} diff --git a/low-can-binding/can/message/message.hpp b/low-can-binding/can/message/message.hpp new file mode 100644 index 00000000..6e0daada --- /dev/null +++ b/low-can-binding/can/message/message.hpp @@ -0,0 +1,86 @@ +/* + * Copyright (C) 2015, 2016 "IoT.bzh" + * Author "Romain Forlot" + * + * 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 +#include +#include +#include +#include +#include +#include +#include "../../utils/timer.hpp" + +#define CAN_MESSAGE_SIZE 8 + +#define MAX_BCM_CAN_FRAMES 257 + +struct bcm_msg +{ + struct bcm_msg_head msg_head; + union { + struct canfd_frame fd_frames[MAX_BCM_CAN_FRAMES]; + struct can_frame frames[MAX_BCM_CAN_FRAMES]; + }; +}; + +/** + * @enum can_message_format_t + * @brief The ID format for a CAN message. + */ +enum class can_message_format_t { + STANDARD, ///< STANDARD - standard 11-bit CAN arbitration ID. */ + EXTENDED, ///< EXTENDED - an extended frame, with a 29-bit arbitration ID. */ + J1939, ///< J1939 - Format for j1939 messages + INVALID, ///< INVALID - INVALID code used at initialization to signify that it isn't usable'*/ +}; + + +/// @class message_t +/// +/// @brief A compact representation of a single CAN message, meant to be used in in/out +/// buffers. It is a wrapper of a can_frame struct with some sugar around it for binding purposes. +class message_t { +protected: + uint8_t length_; ///< length_ - the length of the data array (max 8). */ + can_message_format_t format_; ///< format_ - the format of the message's ID.*/ + std::vector 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: + message_t(); + message_t(uint8_t length, can_message_format_t format, std::vector& data, uint64_t timestamp); + + int get_sub_id() const; + const uint8_t* get_data() const; + const std::vector get_data_vector() const; + uint8_t get_length() const; + uint64_t get_timestamp() const; + + void set_sub_id(int sub_id); + void set_timestamp(uint64_t timestamp); + can_message_format_t get_msg_format(); + virtual bool is_set() = 0; + virtual std::string get_debug_message() = 0; + virtual uint32_t get_id() const = 0; + virtual struct bcm_msg get_bcm_msg() = 0; + virtual void set_bcm_msg(struct bcm_msg bcm_msg) = 0; + +}; diff --git a/low-can-binding/diagnostic/diagnostic-manager.cpp b/low-can-binding/diagnostic/diagnostic-manager.cpp index 65afecfe..7c067c6a 100644 --- a/low-can-binding/diagnostic/diagnostic-manager.cpp +++ b/low-can-binding/diagnostic/diagnostic-manager.cpp @@ -104,7 +104,7 @@ bool diagnostic_manager_t::shims_send(const uint32_t arbitration_id, const uint8 if(! tx_socket) tx_socket.open(dm.get_bus_device_name()); - struct utils::bcm_msg bcm_msg; + struct bcm_msg bcm_msg; struct can_frame cf; struct timeval freq = current_adr->get_frequency_clock().get_timeval_from_period(); @@ -123,7 +123,12 @@ bool diagnostic_manager_t::shims_send(const uint32_t arbitration_id, const uint8 bcm_msg.frames[0] = cf; - tx_socket.write_message(bcm_msg); + + std::shared_ptr msg = std::make_shared(); + + msg->set_bcm_msg(bcm_msg); + + tx_socket.write_message(msg); if(tx_socket) return true; return false; @@ -466,13 +471,13 @@ openxc_VehicleMessage diagnostic_manager_t::relay_diagnostic_response(active_dia /// @param[in] cm - A raw CAN message. /// /// @return A pointer to a filled openxc_VehicleMessage or a nullptr if nothing has been found. -openxc_VehicleMessage diagnostic_manager_t::relay_diagnostic_handle(active_diagnostic_request_t* entry, const can_message_t& cm) +openxc_VehicleMessage diagnostic_manager_t::relay_diagnostic_handle(active_diagnostic_request_t* entry, std::shared_ptr m) { - DiagnosticResponse response = diagnostic_receive_can_frame(&shims_, entry->get_handle(), cm.get_id(), cm.get_data(), cm.get_length()); + DiagnosticResponse response = diagnostic_receive_can_frame(&shims_, entry->get_handle(), m->get_id(), m->get_data(), m->get_length()); if(response.completed && entry->get_handle()->completed) { if(entry->get_handle()->success) - return relay_diagnostic_response(entry, response, cm.get_timestamp()); + return relay_diagnostic_response(entry, response, m->get_timestamp()); } else if(!response.completed && response.multi_frame) { @@ -491,20 +496,20 @@ openxc_VehicleMessage diagnostic_manager_t::relay_diagnostic_handle(active_diagn /// @param[in] cm - Raw CAN message received /// /// @return VehicleMessage with decoded value. -openxc_VehicleMessage diagnostic_manager_t::find_and_decode_adr(const can_message_t& cm) +openxc_VehicleMessage diagnostic_manager_t::find_and_decode_adr(std::shared_ptr m) { openxc_VehicleMessage vehicle_message = build_VehicleMessage(); for ( auto entry : non_recurring_requests_) { - vehicle_message = relay_diagnostic_handle(entry, cm); + vehicle_message = relay_diagnostic_handle(entry, m); if (is_valid(vehicle_message)) return vehicle_message; } for ( auto entry : recurring_requests_) { - vehicle_message = relay_diagnostic_handle(entry, cm); + vehicle_message = relay_diagnostic_handle(entry, m); if (is_valid(vehicle_message)) return vehicle_message; } @@ -519,9 +524,12 @@ openxc_VehicleMessage diagnostic_manager_t::find_and_decode_adr(const can_messag /// @param[in] cm - CAN message received from the socket. /// /// @return True if the active diagnostic request match the response. -bool diagnostic_manager_t::is_diagnostic_response(const can_message_t& cm) +bool diagnostic_manager_t::is_diagnostic_response(std::shared_ptr m) { - if (cm.get_id() >= 0x7e8 && cm.get_id() <= 0x7ef) - return true; + if(m->get_msg_format() == can_message_format_t::STANDARD || m->get_msg_format() == can_message_format_t::EXTENDED) + { + if (m->get_id() >= 0x7e8 && m->get_id() <= 0x7ef) + return true; + } return false; } diff --git a/low-can-binding/diagnostic/diagnostic-manager.hpp b/low-can-binding/diagnostic/diagnostic-manager.hpp index 26c869e2..64a19d7c 100644 --- a/low-can-binding/diagnostic/diagnostic-manager.hpp +++ b/low-can-binding/diagnostic/diagnostic-manager.hpp @@ -84,7 +84,7 @@ public: // Decoding part openxc_VehicleMessage relay_diagnostic_response(active_diagnostic_request_t* adr, const DiagnosticResponse& response, const uint64_t timestamp); - openxc_VehicleMessage relay_diagnostic_handle(active_diagnostic_request_t* entry, const can_message_t& cm); - openxc_VehicleMessage find_and_decode_adr(const can_message_t& cm); - bool is_diagnostic_response(const can_message_t& cm); + openxc_VehicleMessage relay_diagnostic_handle(active_diagnostic_request_t* entry, std::shared_ptr m); + openxc_VehicleMessage find_and_decode_adr(std::shared_ptr m); + bool is_diagnostic_response(std::shared_ptr m); }; diff --git a/low-can-binding/diagnostic/diagnostic-message.hpp b/low-can-binding/diagnostic/diagnostic-message.hpp index 4ae84358..86149413 100644 --- a/low-can-binding/diagnostic/diagnostic-message.hpp +++ b/low-can-binding/diagnostic/diagnostic-message.hpp @@ -22,7 +22,7 @@ #include "uds/uds.h" #include "../can/can-message-set.hpp" -#include "../can/can-message.hpp" +#include "../can/message/can-message.hpp" #include "active-diagnostic-request.hpp" enum UNIT { diff --git a/low-can-binding/utils/socketcan-bcm.cpp b/low-can-binding/utils/socketcan-bcm.cpp index b9124ee3..0f6bc75c 100644 --- a/low-can-binding/utils/socketcan-bcm.cpp +++ b/low-can-binding/utils/socketcan-bcm.cpp @@ -70,7 +70,7 @@ namespace utils /// Read the socket to retrieve the associated CAN message. All the hard work is do into /// convert_from_frame method and if there isn't CAN message retrieve, only BCM head struct, /// then CAN message will be zeroed and must be handled later. - std::shared_ptr socketcan_bcm_t::read_message() + std::shared_ptr socketcan_bcm_t::read_message() { struct bcm_msg msg; std::shared_ptr cm = std::make_shared(); @@ -98,33 +98,24 @@ namespace utils struct timeval tv; ioctl(socket(), SIOCGSTAMP, &tv); uint64_t timestamp = 1000000 * tv.tv_sec + tv.tv_usec; - *cm = can_message_t::convert_from_frame(msg.fd_frames[0] , frame_size, timestamp); + cm = can_message_t::convert_from_frame(msg.fd_frames[0] , frame_size, timestamp); cm->set_sub_id((int)socket()); return cm; } - void socketcan_bcm_t::write_message(std::vector>& vobj) + void socketcan_bcm_t::write_message(std::vector>& vobj) { for(const auto& obj : vobj) write_message(obj); } - void socketcan_bcm_t::write_message(std::shared_ptr m) - { - struct can_frame obj; - obj.can_id = m->get_id(); - obj.can_dlc = m->get_length(); - ::memcpy(obj.data, m->get_data(), CAN_MAX_DLEN); - if (::sendto(socket(), &obj, sizeof(&obj), 0, (const struct sockaddr*)&get_tx_address(), sizeof(get_tx_address())) < 0) - AFB_API_ERROR(afbBindingV3root, "Error sending : %i %s", errno, ::strerror(errno)); - } - - void socketcan_bcm_t::write_message(struct bcm_msg& obj) + void socketcan_bcm_t::write_message(std::shared_ptr m) { + struct bcm_msg obj = m->get_bcm_msg(); size_t size = (obj.msg_head.flags & CAN_FD_FRAME) ? - (size_t)((char*)&obj.fd_frames[obj.msg_head.nframes] - (char*)&obj): - (size_t)((char*)&obj.frames[obj.msg_head.nframes] - (char*)&obj); + (size_t)((char*)&obj.fd_frames[obj.msg_head.nframes] - (char*)&obj): + (size_t)((char*)&obj.frames[obj.msg_head.nframes] - (char*)&obj); if (::sendto(socket(), &obj, size, 0, (const struct sockaddr*)&get_tx_address(), sizeof(get_tx_address())) < 0) AFB_API_ERROR(afbBindingV3root, "Error sending : %i %s", errno, ::strerror(errno)); } diff --git a/low-can-binding/utils/socketcan-bcm.hpp b/low-can-binding/utils/socketcan-bcm.hpp index 00028905..5e86aa4f 100644 --- a/low-can-binding/utils/socketcan-bcm.hpp +++ b/low-can-binding/utils/socketcan-bcm.hpp @@ -18,21 +18,10 @@ #pragma once #include "socketcan.hpp" -#include "../can/can-message.hpp" - -#define MAX_BCM_CAN_FRAMES 257 +#include "../can/message/can-message.hpp" namespace utils { - struct bcm_msg - { - struct bcm_msg_head msg_head; - union { - struct canfd_frame fd_frames[MAX_BCM_CAN_FRAMES]; - struct can_frame frames[MAX_BCM_CAN_FRAMES]; - }; - }; - /// @brief derivated socketcan class specialized for BCM CAN socket.make_bcm_head class socketcan_bcm_t : public socketcan_t { @@ -40,10 +29,9 @@ namespace utils using socketcan_t::socketcan_t; virtual int open(std::string device_name); - virtual std::shared_ptr read_message(); - virtual void write_message(std::vector>& vobj); - virtual void write_message(std::shared_ptr obj); - void write_message(struct bcm_msg& obj); + virtual std::shared_ptr read_message(); + virtual void write_message(std::vector>& vobj); + virtual void write_message(std::shared_ptr obj); private: int connect(const struct sockaddr* addr, socklen_t len); diff --git a/low-can-binding/utils/socketcan-j1939.cpp b/low-can-binding/utils/socketcan-j1939.cpp index f914ac27..5f977f04 100644 --- a/low-can-binding/utils/socketcan-j1939.cpp +++ b/low-can-binding/utils/socketcan-j1939.cpp @@ -181,12 +181,12 @@ namespace utils } - void socketcan_j1939_t::write_message(std::vector>& vobj) + void socketcan_j1939_t::write_message(std::vector>& vobj) { AFB_WARNING("Not implemented"); } - void socketcan_j1939_t::write_message(std::shared_ptr cm) + void socketcan_j1939_t::write_message(std::shared_ptr cm) { AFB_WARNING("Not implemented"); } diff --git a/low-can-binding/utils/socketcan.hpp b/low-can-binding/utils/socketcan.hpp index 6419175d..fdf8c949 100644 --- a/low-can-binding/utils/socketcan.hpp +++ b/low-can-binding/utils/socketcan.hpp @@ -24,7 +24,7 @@ #include #include "../binding/low-can-hat.hpp" -#include "../can/can-message.hpp" +#include "../can/message/can-message.hpp" #define INVALID_SOCKET -1 @@ -47,9 +47,9 @@ namespace utils virtual int open(std::string device_name) = 0; int setopt(int level, int optname, const void* optval, socklen_t optlen); virtual int close(); - virtual std::shared_ptr read_message() = 0; - virtual void write_message(std::shared_ptr obj) = 0; - virtual void write_message(std::vector>& vobj) = 0; + virtual std::shared_ptr read_message() = 0; + virtual void write_message(std::shared_ptr obj) = 0; + virtual void write_message(std::vector>& vobj) = 0; protected: int socket_; -- cgit 1.2.3-korg