From 28340674704f385c916e528f66e368b10fad31d8 Mon Sep 17 00:00:00 2001 From: Romain Forlot Date: Sat, 23 Nov 2019 09:00:50 +0100 Subject: 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 --- low-can-binding/can/can-encoder.cpp | 68 ++++++++++++++++++++----------------- 1 file 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, uin { message_t *message; std::vector 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. -- cgit 1.2.3-korg