diff options
author | Romain Forlot <romain.forlot@iot.bzh> | 2019-06-26 10:34:04 +0200 |
---|---|---|
committer | Romain Forlot <romain.forlot@iot.bzh> | 2019-06-26 17:55:06 +0200 |
commit | d2d2620bbb8f0aad0181e67e9f6affc3d6cb4a51 (patch) | |
tree | 13e7c1069252dbadb2b430414f3722132d1d7013 /low-can-binding/can | |
parent | 82b45e3de8c92816c8080178224c5bd4be60a091 (diff) |
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 <arthur.guyader@iot.bzh>
Signed-off-by: Stephane Desneux <stephane.desneux@iot.bzh>
Signed-off-by: Romain Forlot <romain.forlot@iot.bzh>
Diffstat (limited to 'low-can-binding/can')
-rw-r--r-- | low-can-binding/can/can-bus-device.hpp | 2 | ||||
-rw-r--r-- | low-can-binding/can/can-bus.cpp | 50 | ||||
-rw-r--r-- | low-can-binding/can/can-bus.hpp | 13 | ||||
-rw-r--r-- | low-can-binding/can/can-decoder.cpp | 4 | ||||
-rw-r--r-- | low-can-binding/can/can-decoder.hpp | 6 | ||||
-rw-r--r-- | low-can-binding/can/can-encoder.hpp | 2 | ||||
-rw-r--r-- | low-can-binding/can/can-message-definition.cpp | 14 | ||||
-rw-r--r-- | low-can-binding/can/can-message-definition.hpp | 5 | ||||
-rw-r--r-- | low-can-binding/can/can-signals.cpp | 2 | ||||
-rw-r--r-- | low-can-binding/can/can-signals.hpp | 2 | ||||
-rw-r--r-- | low-can-binding/can/message/can-message.cpp (renamed from low-can-binding/can/can-message.cpp) | 101 | ||||
-rw-r--r-- | low-can-binding/can/message/can-message.hpp | 49 | ||||
-rw-r--r-- | low-can-binding/can/message/j1939-message.cpp | 163 | ||||
-rw-r--r-- | low-can-binding/can/message/j1939-message.hpp | 73 | ||||
-rw-r--r-- | low-can-binding/can/message/message.cpp | 96 | ||||
-rw-r--r-- | low-can-binding/can/message/message.hpp (renamed from low-can-binding/can/can-message.hpp) | 46 |
16 files changed, 507 insertions, 121 deletions
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 <condition_variable> #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<int, std::shared_ptr<low_can_subscription_t> >& s) +void can_bus_t::process_signals(const message_t& message, std::map<int, std::shared_ptr<low_can_subscription_t> >& 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<i // First we have to found which can_signal_t it is std::shared_ptr<low_can_subscription_t> 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<i /// @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_diagnostic_signals(diagnostic_manager_t& manager, const can_message_t& can_message, std::map<int, std::shared_ptr<low_can_subscription_t> >& s) +void can_bus_t::process_diagnostic_signals(diagnostic_manager_t& manager, std::shared_ptr<message_t> message, std::map<int, std::shared_ptr<low_can_subscription_t> >& 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_t> message = next_can_message(); can_message_lock.unlock(); { std::lock_guard<std::mutex> subscribed_signals_lock(sm.get_subscribed_signals_mutex()); std::map<int, std::shared_ptr<low_can_subscription_t> >& 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<message_t> can_bus_t::next_can_message() { - can_message_t can_msg; + std::shared_ptr<message_t> 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<message_t> 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 <thread> #include <linux/can.h> #include <condition_variable> - #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<low_can_subscription_t> can_subscription); - void process_can_signals(const can_message_t& can_message, std::map<int, std::shared_ptr<low_can_subscription_t> >& s); - void process_diagnostic_signals(diagnostic_manager_t& manager, const can_message_t& can_message, std::map<int, std::shared_ptr<low_can_subscription_t> >& s); + void process_signals(const message_t& message, std::map<int, std::shared_ptr<low_can_subscription_t> >& s); + void process_diagnostic_signals(diagnostic_manager_t& manager, std::shared_ptr<message_t> message, std::map<int, std::shared_ptr<low_can_subscription_t> >& 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_t> can_message_q_; ///< queue that will store can_message_t to be decoded + std::queue <std::shared_ptr<message_t>> 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<message_t> next_can_message(); + void push_new_can_message(std::shared_ptr<message_t> 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<std::shared_ptr<can_signal_t> >& 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 <memory> #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<std::shared_ptr<can_signal_t> >& 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-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/can-message.cpp b/low-can-binding/can/message/can-message.cpp index 074f5990..91f66252 100644 --- a/low-can-binding/can/can-message.cpp +++ b/low-can-binding/can/message/can-message.cpp @@ -15,11 +15,11 @@ * limitations under the License. */ -#include "can-message.hpp" +#include "./can-message.hpp" #include <cstring> -#include "../binding/low-can-hat.hpp" +#include "../../binding/low-can-hat.hpp" /// /// @brief Class constructor @@ -27,14 +27,11 @@ /// can_message_t class constructor. /// can_message_t::can_message_t() - : maxdlen_{0}, + : message_t(), + maxdlen_{0}, id_{0}, - length_{0}, - format_{can_message_format_t::INVALID}, rtr_flag_{false}, - flags_{0}, - timestamp_{0}, - sub_id_{-1} + flags_{0} {} can_message_t::can_message_t(uint8_t maxdlen, @@ -45,15 +42,11 @@ can_message_t::can_message_t(uint8_t maxdlen, uint8_t flags, std::vector<uint8_t>& data, uint64_t timestamp) - : maxdlen_{maxdlen}, + : message_t(length, format, data, timestamp), + maxdlen_{maxdlen}, id_{id}, - length_{length}, - format_{format}, rtr_flag_{rtr_flag}, - flags_{flags}, - data_{data}, - timestamp_{timestamp}, - sub_id_{-1} + flags_{flags} {} /// @@ -66,51 +59,6 @@ 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<uint8_t> 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 @@ -136,7 +84,7 @@ bool can_message_t::is_correct_to_send() /// @param[in] nbytes - bytes read from socket read operation. /// /// @return A can_message_t object fully initialized with canfd_frame values. -can_message_t can_message_t::convert_from_frame(const struct canfd_frame& frame, size_t nbytes, uint64_t timestamp) +std::shared_ptr<can_message_t> 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; @@ -213,5 +161,34 @@ can_message_t can_message_t::convert_from_frame(const struct canfd_frame& frame, id, (uint8_t)format, length, data[0], data[1], data[2], data[3], data[4], data[5], data[6], data[7]); } - return can_message_t(maxdlen, id, length, format, rtr_flag, flags, data, timestamp); + return std::make_shared<can_message_t>(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" <romain.forlot@iot.bzh> + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#pragma once +#include "./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<uint8_t>& data, uint64_t timestamp); + + uint32_t get_id() const; + + static std::shared_ptr<can_message_t> 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" <arthur.guyader@iot.bzh> + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "./j1939-message.hpp" +#include <cstring> +#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<uint8_t>& 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> 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<uint8_t> 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<length;i++) + { + dataVector.push_back(data[i]); + }; + + AFB_DEBUG("Found pgn: %X, format: %X, length: %X, data %02X%02X%02X%02X%02X%02X%02X%02X", + addr.can_addr.j1939.pgn, (uint8_t)format, length, data[0], data[1], data[2], data[3], data[4], data[5], data[6], data[7]); + + return std::make_shared<j1939_message_t>(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" <arthur.guyader@iot.bzh> + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#pragma once +#include <linux/can.h> +#include <linux/can/j1939.h> +#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<uint8_t>& 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<j1939_message_t> 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" <romain.forlot@iot.bzh> + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "./message.hpp" + +#include <cstring> + +#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<uint8_t>& 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<uint8_t> 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/can-message.hpp b/low-can-binding/can/message/message.hpp index 032ef4d3..6e0daada 100644 --- a/low-can-binding/can/can-message.hpp +++ b/low-can-binding/can/message/message.hpp @@ -20,12 +20,25 @@ #include <vector> #include <string> #include <cstdint> +#include <iostream> +#include <memory> #include <linux/can.h> - -#include "../utils/timer.hpp" +#include <linux/can/bcm.h> +#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. @@ -33,30 +46,28 @@ 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'*/ + J1939, ///< J1939 - Format for j1939 messages + INVALID, ///< INVALID - INVALID code used at initialization to signify that it isn't usable'*/ }; -/// @class can_message_t + +/// @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 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. */ +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.*/ - 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<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); + message_t(); + message_t(uint8_t length, can_message_format_t format, std::vector<uint8_t>& data, uint64_t timestamp); - uint32_t get_id() const; int get_sub_id() const; const uint8_t* get_data() const; const std::vector<uint8_t> get_data_vector() const; @@ -65,8 +76,11 @@ public: 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; - bool is_correct_to_send(); - - static can_message_t convert_from_frame(const canfd_frame& frame, size_t nbytes, uint64_t timestamp); }; |