diff options
author | 2019-11-26 16:19:35 +0100 | |
---|---|---|
committer | 2019-11-28 16:11:47 +0100 | |
commit | fee3b49099fb39731e15f9e26ad5a873bc109f59 (patch) | |
tree | f925b8b2a744d9e94cedbd460e47ba533717c36c /low-can-binding/can | |
parent | a8d5f5d3018da0b051dad4c143c6257b2fba9fac (diff) |
Replace all enum types with masks
This commit allows to change all enum types by masks.
Now to implement new protocol you don't need to add
more attribute in class. All can be contained in
the flags field.
Bug-AGL : SPEC-2779
Bug-AGL: SPEC-2976
Change-Id: I814d0052139be5d5efefc9ff1b4b558f46b85e90
Signed-off-by: Arthur Guyader <arthur.guyader@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-encoder.cpp | 23 | ||||
-rw-r--r-- | low-can-binding/can/message-definition.cpp | 27 | ||||
-rw-r--r-- | low-can-binding/can/message-definition.hpp | 13 | ||||
-rw-r--r-- | low-can-binding/can/message/can-message.cpp | 22 | ||||
-rw-r--r-- | low-can-binding/can/message/can-message.hpp | 3 | ||||
-rw-r--r-- | low-can-binding/can/message/j1939-message.cpp | 18 | ||||
-rw-r--r-- | low-can-binding/can/message/j1939-message.hpp | 4 | ||||
-rw-r--r-- | low-can-binding/can/message/message.cpp | 26 | ||||
-rw-r--r-- | low-can-binding/can/message/message.hpp | 23 |
9 files changed, 69 insertions, 90 deletions
diff --git a/low-can-binding/can/can-encoder.cpp b/low-can-binding/can/can-encoder.cpp index 44b5d9b2..37a7b246 100644 --- a/low-can-binding/can/can-encoder.cpp +++ b/low-can-binding/can/can-encoder.cpp @@ -174,20 +174,37 @@ message_t* encoder_t::build_message(const std::shared_ptr<signal_t>& signal, uin std::vector<uint8_t> data; if(signal->get_message()->is_fd()) { - message = new can_message_t(CANFD_MAX_DLEN,signal->get_message()->get_id(),CANFD_MAX_DLEN,signal->get_message()->get_format(),false,CAN_FD_FRAME,data,0); + message = new can_message_t( CANFD_MAX_DLEN, + signal->get_message()->get_id(), + CANFD_MAX_DLEN, + false, + signal->get_message()->get_flags(), + data, + 0); return build_frame(signal,value,message, factor, offset); } #ifdef USE_FEATURE_J1939 else if(signal->get_message()->is_j1939()) { - message = new j1939_message_t(J1939_MAX_DLEN,signal->get_message()->get_length(),signal->get_message()->get_format(),data,0,J1939_NO_NAME,signal->get_message()->get_id(),J1939_NO_ADDR); + message = new j1939_message_t( signal->get_message()->get_length(), + data, + 0, + J1939_NO_NAME, + signal->get_message()->get_id(), + J1939_NO_ADDR); return build_frame(signal,value,message, factor, offset); } #endif else { - message = new can_message_t(CAN_MAX_DLEN,signal->get_message()->get_id(),CAN_MAX_DLEN,signal->get_message()->get_format(),false,0,data,0); + message = new can_message_t(CAN_MAX_DLEN, + signal->get_message()->get_id(), + CAN_MAX_DLEN, + false, + signal->get_message()->get_flags(), + data, + 0); return build_frame(signal,value,message, factor, offset); } } diff --git a/low-can-binding/can/message-definition.cpp b/low-can-binding/can/message-definition.cpp index 064b2c17..407ad750 100644 --- a/low-can-binding/can/message-definition.cpp +++ b/low-can-binding/can/message-definition.cpp @@ -22,16 +22,14 @@ message_definition_t::message_definition_t( const std::string bus, uint32_t id, - bool is_fd, - message_format_t format, + uint32_t flags, frequency_clock_t frequency_clock, bool force_send_changed, const std::vector<std::shared_ptr<signal_t> >& signals) : parent_{nullptr}, bus_{bus}, id_{id}, - is_fd_(is_fd), - format_{format}, + flags_{flags}, frequency_clock_{frequency_clock}, force_send_changed_{force_send_changed}, last_value_{CAN_MESSAGE_SIZE}, @@ -42,8 +40,7 @@ message_definition_t::message_definition_t(const std::string bus, uint32_t id, const std::string name, uint32_t length, - bool is_fd, - message_format_t format, + uint32_t flags, frequency_clock_t frequency_clock, bool force_send_changed, const std::vector<std::shared_ptr<signal_t> >& signals) @@ -52,8 +49,7 @@ message_definition_t::message_definition_t(const std::string bus, id_{id}, name_{name}, length_{length}, - is_fd_(is_fd), - format_{format}, + flags_{flags}, frequency_clock_{frequency_clock}, force_send_changed_{force_send_changed}, last_value_{CAN_MESSAGE_SIZE}, @@ -73,19 +69,12 @@ uint32_t message_definition_t::get_id() const bool message_definition_t::is_fd() const { - return is_fd_; + return (flags_&FD_FRAME); } bool message_definition_t::is_j1939() const { - if(format_ == message_format_t::J1939) - { - return true; - } - else - { - return false; - } + return (flags_&J1939_PROTOCOL); } std::vector<std::shared_ptr<signal_t>>& message_definition_t::get_signals() @@ -108,7 +97,7 @@ uint32_t message_definition_t::get_length() const return length_; } -message_format_t message_definition_t::get_format() const +uint32_t message_definition_t::get_flags() const { - return format_; + return flags_; }
\ No newline at end of file diff --git a/low-can-binding/can/message-definition.hpp b/low-can-binding/can/message-definition.hpp index f39bb71e..13ae2289 100644 --- a/low-can-binding/can/message-definition.hpp +++ b/low-can-binding/can/message-definition.hpp @@ -45,8 +45,7 @@ private: uint32_t id_; ///< id_ - The ID or the PGN (if j1939) of the message.*/ std::string name_; ///< name_ - J1939 PGN name uint32_t length_; ///< length_ - Message data length in bytes. For J1939 message, this is the expected data size - bool is_fd_; /*!< uses_fd_ - Flags to enable an FD CAN message communication*/ - message_format_t format_; ///< format_ - the format of the message's ID.*/ + uint32_t flags_; ///< format_ - the format of the message's ID.*/ frequency_clock_t frequency_clock_; ///< clock_ - an optional frequency clock to control the output of this /// message, if sent raw, or simply to mark the max frequency for custom /// handlers to retrieve.*/ @@ -61,11 +60,10 @@ public: //message_definition_t(const message_definition_t& b); message_definition_t(const std::string bus); message_definition_t(const std::string bus, uint32_t id, frequency_clock_t frequency_clock, bool force_send_changed); - message_definition_t(const std::string bus, uint32_t id, message_format_t format, frequency_clock_t frequency_clock, bool force_send_changed); + message_definition_t(const std::string bus, uint32_t id, uint32_t flags, frequency_clock_t frequency_clock, bool force_send_changed); message_definition_t(const std::string bus, uint32_t id, - bool is_fd, - message_format_t format, + uint32_t flags, frequency_clock_t frequency_clock, bool force_send_changed, const std::vector<std::shared_ptr<signal_t> >& signals); @@ -73,8 +71,7 @@ public: uint32_t id, std::string name, uint32_t length, - bool is_fd, - message_format_t format, + uint32_t flags, frequency_clock_t frequency_clock, bool force_send_changed, const std::vector<std::shared_ptr<signal_t> >& signals); @@ -87,7 +84,7 @@ public: bool is_j1939() const; std::vector<std::shared_ptr<signal_t>>& get_signals(); uint32_t get_length() const; - message_format_t get_format() const; + uint32_t get_flags() const; void set_parent(std::shared_ptr<message_set_t> parent); void set_last_value(std::shared_ptr<message_t> m); diff --git a/low-can-binding/can/message/can-message.cpp b/low-can-binding/can/message/can-message.cpp index 8a04992e..0ef693c7 100644 --- a/low-can-binding/can/message/can-message.cpp +++ b/low-can-binding/can/message/can-message.cpp @@ -35,12 +35,11 @@ can_message_t::can_message_t() can_message_t::can_message_t(uint32_t maxdlen, uint32_t id, uint32_t length, - message_format_t format, bool rtr_flag, uint32_t flags, std::vector<uint8_t>& data, uint64_t timestamp) - : message_t(maxdlen, length, format, data, timestamp, flags), + : message_t(maxdlen, length, flags, data, timestamp), id_{id}, rtr_flag_{rtr_flag} {} @@ -62,7 +61,7 @@ uint32_t can_message_t::get_id() const /// @return True if object correctly initialized and false if not. bool can_message_t::is_correct_to_send() { - if (id_ != 0 && length_ != 0 && format_ != message_format_t::INVALID) + if (id_ != 0 && length_ != 0 && !(flags_&INVALID_FLAG)) { int i; for(i=0;i<length_;i++) @@ -83,9 +82,8 @@ bool can_message_t::is_correct_to_send() std::shared_ptr<can_message_t> can_message_t::convert_from_frame(const struct canfd_frame& frame, size_t nbytes, uint64_t timestamp) { uint32_t maxdlen = 0, length = 0; - uint8_t flags = 0; + uint32_t flags = 0; uint32_t id; - message_format_t format; bool rtr_flag; std::vector<uint8_t> data; @@ -106,17 +104,17 @@ std::shared_ptr<can_message_t> can_message_t::convert_from_frame(const struct ca if (frame.can_id & CAN_ERR_FLAG) { - format = message_format_t::INVALID; + flags = flags|INVALID_FLAG; id = frame.can_id & (CAN_ERR_MASK|CAN_ERR_FLAG); } else if (frame.can_id & CAN_EFF_FLAG) { - format = message_format_t::EXTENDED; + flags = flags|EXTENDED_ID; id = frame.can_id & CAN_EFF_MASK; } else { - format = message_format_t::STANDARD; + flags = flags|STANDARD_ID; id = frame.can_id & CAN_SFF_MASK; } @@ -140,8 +138,8 @@ std::shared_ptr<can_message_t> can_message_t::convert_from_frame(const struct ca 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(maxdlen == CANFD_MAX_DLEN) + flags = frame.flags & 0xF;*/ if (data.capacity() < maxdlen) data.reserve(maxdlen); @@ -155,10 +153,10 @@ std::shared_ptr<can_message_t> can_message_t::convert_from_frame(const struct ca }; AFB_DEBUG("Found id: %X, format: %X, length: %X, data %02X%02X%02X%02X%02X%02X%02X%02X", - id, (uint8_t)format, length, data[0], data[1], data[2], data[3], data[4], data[5], data[6], data[7]); + id, (uint32_t)flags, length, data[0], data[1], data[2], data[3], data[4], data[5], data[6], data[7]); } - return std::make_shared<can_message_t>(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, rtr_flag, flags, data, timestamp)); } /// @brief Take all initialized class members and build a diff --git a/low-can-binding/can/message/can-message.hpp b/low-can-binding/can/message/can-message.hpp index 75e65c67..769899ea 100644 --- a/low-can-binding/can/message/can-message.hpp +++ b/low-can-binding/can/message/can-message.hpp @@ -39,8 +39,7 @@ class can_message_t : public message_t { public: can_message_t(); - can_message_t(uint32_t maxdlen, uint32_t id, uint32_t length, message_format_t format, bool rtr_flag_, uint32_t flags, std::vector<uint8_t>& data, uint64_t timestamp); - + can_message_t(uint32_t maxdlen, uint32_t id, uint32_t length, bool rtr_flag_, uint32_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); diff --git a/low-can-binding/can/message/j1939-message.cpp b/low-can-binding/can/message/j1939-message.cpp index 2d600a1e..ec65cba4 100644 --- a/low-can-binding/can/message/j1939-message.cpp +++ b/low-can-binding/can/message/j1939-message.cpp @@ -45,13 +45,12 @@ j1939_message_t::j1939_message_t(): * @param addr The address of the message */ j1939_message_t::j1939_message_t(uint32_t length, - message_format_t format, std::vector<uint8_t>& data, uint64_t timestamp, name_t name, pgn_t pgn, uint8_t addr): - message_t(J1939_MAX_DLEN,length, format, data, timestamp, 0), + message_t(J1939_MAX_DLEN, length, J1939_PROTOCOL, data, timestamp), name_{name}, pgn_{pgn}, addr_{addr} @@ -99,18 +98,13 @@ std::shared_ptr<j1939_message_t> j1939_message_t::convert_from_addr(struct socka { int i; uint32_t length = 0; - message_format_t format; + uint32_t flags; std::vector<uint8_t> data_vector; if(nbytes > J1939_MAX_DLEN) { AFB_DEBUG("Unsupported j1939 frame"); - format = message_format_t::INVALID; - } - else - { - //AFB_DEBUG("Got a j1939 frame"); - format = message_format_t::J1939; + return std::make_shared<j1939_message_t>(j1939_message_t()); } length = (uint32_t) nbytes; @@ -126,10 +120,10 @@ std::shared_ptr<j1939_message_t> j1939_message_t::convert_from_addr(struct socka data_vector.push_back(data[i]); }; - AFB_DEBUG("Found pgn: %X, format: %X, length: %X, data %s", - addr.can_addr.j1939.pgn, (uint8_t)format, length, data_string.c_str()); + AFB_DEBUG("Found pgn: %X, length: %X, data %s", + addr.can_addr.j1939.pgn, length, data_string.c_str()); - return std::make_shared<j1939_message_t>(j1939_message_t(J1939_MAX_DLEN,length, format, data_vector, timestamp,addr.can_addr.j1939.name,addr.can_addr.j1939.pgn,addr.can_addr.j1939.addr)); + return std::make_shared<j1939_message_t>(j1939_message_t(length, data_vector, 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. diff --git a/low-can-binding/can/message/j1939-message.hpp b/low-can-binding/can/message/j1939-message.hpp index 0d3736f1..9e9cea4a 100644 --- a/low-can-binding/can/message/j1939-message.hpp +++ b/low-can-binding/can/message/j1939-message.hpp @@ -67,7 +67,7 @@ class j1939_message_t : public message_t public: j1939_message_t(); - j1939_message_t(uint32_t maxdlen, uint32_t length, message_format_t format, std::vector<uint8_t>& data, uint64_t timestamp, name_t name, pgn_t pgn, uint8_t addr); + j1939_message_t(uint32_t length, 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; @@ -78,4 +78,4 @@ class j1939_message_t : public message_t struct sockaddr_can get_sockname(); void set_sockname(struct sockaddr_can sockname); void set_sockname(pgn_t pgn, name_t name, uint8_t addr); -}; +};
\ No newline at end of file diff --git a/low-can-binding/can/message/message.cpp b/low-can-binding/can/message/message.cpp index 497304de..aaab99f5 100644 --- a/low-can-binding/can/message/message.cpp +++ b/low-can-binding/can/message/message.cpp @@ -28,10 +28,9 @@ message_t::message_t() : maxdlen_{0}, length_{0}, - format_{message_format_t::INVALID}, + flags_{INVALID_FLAG}, timestamp_{0}, - sub_id_{-1}, - flags_{0} + sub_id_{-1} {} /** @@ -45,17 +44,15 @@ message_t::message_t() */ message_t::message_t(uint32_t maxdlen, uint32_t length, - message_format_t format, + uint32_t flags, std::vector<uint8_t>& data, - uint64_t timestamp, - uint32_t flags) + uint64_t timestamp) : maxdlen_{maxdlen}, length_{length}, - format_{format}, + flags_{flags}, data_{data}, timestamp_{timestamp}, - sub_id_{-1}, - flags_{flags} + sub_id_{-1} {} /** @@ -163,17 +160,6 @@ uint64_t message_t::get_timestamp() const return timestamp_; } -/** - * @brief Return the format of the message - * - * @return message_format_t The message format - */ -message_format_t message_t::get_msg_format() -{ - return format_; -} - - uint32_t message_t::get_flags() { return flags_; diff --git a/low-can-binding/can/message/message.hpp b/low-can-binding/can/message/message.hpp index 2182fe5b..6cef0185 100644 --- a/low-can-binding/can/message/message.hpp +++ b/low-can-binding/can/message/message.hpp @@ -32,16 +32,17 @@ /** - * @enum message_format_t - * @brief The ID format for a CAN message. + * FLAGS */ -enum class 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'*/ -}; +#define INVALID_FLAG 0x0001 +#define STANDARD_ID 0x0002 +#define EXTENDED_ID 0x0004 +#define BCM_PROTOCOL 0x0008 +#define J1939_PROTOCOL 0x0010 +#define J1939_ADDR_CLAIM_PROTOCOL 0x0020 +#define ISOTP_PROTOCOL 0x0040 +#define FD_FRAME 0x0800 /// @class message_t /// @@ -51,15 +52,14 @@ class message_t { protected: uint32_t maxdlen_; ///< maxdlen_ - Max data length deduce from number of bytes read from the socket.*/ uint32_t length_; ///< length_ - the length of the data array. */ - message_format_t format_; ///< format_ - the format of the message's ID.*/ + uint32_t flags_; ///< format_ - the format mask of the message*/ 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. */ - uint32_t flags_; ///< flags_ - flags of a CAN FD frame. Needed if we catch FD frames.*/ public: message_t(); - message_t(uint32_t maxdlen, uint32_t length, message_format_t format, std::vector<uint8_t>& data, uint64_t timestamp, uint32_t flags); + message_t(uint32_t maxdlen, uint32_t length, uint32_t flags, std::vector<uint8_t>& data, uint64_t timestamp); virtual ~message_t() = default; int get_sub_id() const; @@ -72,7 +72,6 @@ public: void set_data(std::vector<uint8_t> &data); void set_sub_id(int sub_id); void set_timestamp(uint64_t timestamp); - 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; |