aboutsummaryrefslogtreecommitdiffstats
path: root/low-can-binding
diff options
context:
space:
mode:
authorRomain Forlot <romain.forlot@iot.bzh>2019-11-23 09:00:50 +0100
committerRomain Forlot <romain.forlot@iot.bzh>2019-12-03 19:46:26 +0100
commit453a68145bc28a8464efedc055dd43359dd8a8a7 (patch)
treeda02354b5f3085e7e628f80cb90a2dae2843abec /low-can-binding
parentd4ce219e8031b201b71df5e7b71181f7a15e3240 (diff)
encoder: use switch case to handle CAN protocols
Better using switch case than if...else. This would be more extendable and readable. Change-Id: I5fb03531ad77441bcb7d1620bdb07d82aa6a4eb4 Signed-off-by: Romain Forlot <romain.forlot@iot.bzh>
Diffstat (limited to 'low-can-binding')
-rw-r--r--low-can-binding/can/can-encoder.cpp111
1 files changed, 62 insertions, 49 deletions
diff --git a/low-can-binding/can/can-encoder.cpp b/low-can-binding/can/can-encoder.cpp
index 1a5dcdc9..36be668c 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);
}
+
}
@@ -297,31 +303,38 @@ 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,
+ 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_one_frame_message(signal, value, message);
+#ifdef USE_FEATURE_J1939
+ 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_multi_frame_message(signal, value, message);
+#endif
+ case CAN_PROTOCOL:
+ message = new can_message_t(CAN_MAX_DLEN,
signal->get_message()->get_id(),
- CANFD_MAX_DLEN,
+ CAN_MAX_DLEN,
false,
signal->get_message()->get_flags(),
data,
0);
return build_one_frame_message(signal, value, message);
- }
-#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_multi_frame_message(signal, value, message);
- }
-#endif
- else
- {
+ default:
message = new can_message_t(CAN_MAX_DLEN,
signal->get_message()->get_id(),
CAN_MAX_DLEN,