diff options
author | Romain Forlot <romain.forlot@iot.bzh> | 2019-11-23 09:00:50 +0100 |
---|---|---|
committer | Romain Forlot <romain.forlot@iot.bzh> | 2020-01-09 15:55:03 +0100 |
commit | 28340674704f385c916e528f66e368b10fad31d8 (patch) | |
tree | 2fdbd5395d9c50202a02c858c09feb3a9398bbed | |
parent | 31996413bb7a4cddc94067568117b7ff23ee60ff (diff) |
encoder: use switch case to handle CAN protocols
Better using switch case than if...else. This would be more extendable
and readable.
Bug-AGL: SPEC-2988
Change-Id: I5fb03531ad77441bcb7d1620bdb07d82aa6a4eb4
Signed-off-by: Romain Forlot <romain.forlot@iot.bzh>
-rw-r--r-- | low-can-binding/can/can-encoder.cpp | 68 |
1 files changed, 37 insertions, 31 deletions
diff --git a/low-can-binding/can/can-encoder.cpp b/low-can-binding/can/can-encoder.cpp index 113428b9..f0ea0921 100644 --- a/low-can-binding/can/can-encoder.cpp +++ b/low-can-binding/can/can-encoder.cpp @@ -167,41 +167,47 @@ message_t* encoder_t::build_message(const std::shared_ptr<signal_t>& signal, uin { message_t *message; std::vector<uint8_t> data; - if(signal->get_message()->is_fd()) + switch(signal->get_message()->get_flags()) { - 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); - } + case CAN_PROTOCOL_WITH_FD_FRAME: + 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( 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); - } + case J1939_PROTOCOL: + 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, - false, - signal->get_message()->get_flags(), - data, - 0); - return build_frame(signal,value,message, factor, offset); + case CAN_PROTOCOL: + 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); + default: + 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); } + } /// @brief Encode a boolean into an integer, fit for a CAN signal bitfield. |