Skip to content

Commit

Permalink
Fix container PDU signal handling.
Browse files Browse the repository at this point in the history
Signed-off-by: Rule Timothy (VM/EMT3) <[email protected]>
  • Loading branch information
timrulebosch committed Jun 6, 2024
1 parent b3ff013 commit 5069c08
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 62 deletions.
20 changes: 16 additions & 4 deletions dse/network/encoder.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ static NetworkMessage* _find_mux_message(
if (nm->frame_id == frame_id) {
if (nm->mux_id == mux_id) {
return nm;
break;
}
}
}
Expand All @@ -48,7 +47,13 @@ static NetworkMessage* _find_mux_message(
static void _process_message(
Network* n, NetworkMessage* nm, NCodecCanMessage* msg)
{
nm->unpack_func(nm->buffer, msg->buffer, msg->len);
int rc = nm->unpack_func(nm->buffer, msg->buffer, msg->len);
if (rc) {
log_error("Failed message RX, unpack_func() failed with error %d "
"(frame_id=%d)",
-rc, nm->frame_id);
return;
}
if (nm->mux_signal && nm->mux_signal->mux_mi) {
/* This is a container message, also process the contained message. */
MarshalItem* mi = nm->mux_signal->mux_mi;
Expand All @@ -59,6 +64,10 @@ static void _process_message(
_find_mux_message(n, mux_id, msg->frame_id);
if (mux_message) {
_process_message(n, mux_message, msg);
} else {
log_debug("Mux message not found (frame_id=%d, mux_id=%d, "
"msg->frame_id=%d)",
nm->frame_id, mux_id, msg->frame_id);
}
}

Expand All @@ -67,12 +76,15 @@ static void _process_message(
simbus_generate_uid_hash(nm->buffer, nm->buffer_len);
nm->update_signals = false;
if (payload_checksum == nm->buffer_checksum) {
log_debug("filtered on checksum %d", payload_checksum);
log_debug("Filtered message RX, no change detected in checksum %d, "
"(frame_id=%d, checksum %d)",
payload_checksum, nm->frame_id, nm->buffer_checksum);
return;
}
nm->buffer_checksum = payload_checksum;
nm->update_signals = true;
log_debug("decode path checksum %d", payload_checksum);
log_debug("New message RX, updated checksum %d (frame_id=%d)",
payload_checksum, nm->frame_id);
}

static void _process_can_frame(Network* n, NCodecCanMessage* msg)
Expand Down
95 changes: 37 additions & 58 deletions dse/network/engine.c
Original file line number Diff line number Diff line change
Expand Up @@ -111,11 +111,10 @@ int network_get_signal_names(
}


int network_marshal_signals_to_messages(Network* n, MarshalItem* mi)
int network_marshal_signals_to_messages(Network* n, MarshalItem* marshal_list)
{
if (n == NULL || mi == NULL) return 1;

while (mi->signal) {
if (n == NULL || marshal_list == NULL) return 1;
for (MarshalItem* mi = marshal_list; mi && mi->signal; mi++) {
double _signal_value = n->signal_vector[mi->signal_vector_index];
if (mi->signal->internal && mi->message->container) {
/* Internal signals on container messages take a constant value. */
Expand All @@ -135,8 +134,7 @@ int network_marshal_signals_to_messages(Network* n, MarshalItem* mi)
sizeof(uint8_t)],
mi->signal->name);
}
}
if (strcmp(mi->signal->member_type, "uint16_t") == 0) {
} else if (strcmp(mi->signal->member_type, "uint16_t") == 0) {
uint16_t _value = 0;
_value = mi->signal->encode_func_int16(_signal_value);
if (mi->signal->range_func_int16(_value)) {
Expand All @@ -149,9 +147,7 @@ int network_marshal_signals_to_messages(Network* n, MarshalItem* mi)
sizeof(uint16_t)],
mi->signal->name);
}
}

if (strcmp(mi->signal->member_type, "uint32_t") == 0) {
} else if (strcmp(mi->signal->member_type, "uint32_t") == 0) {
uint32_t _value = 0;
_value = mi->signal->encode_func_int32(_signal_value);
if (mi->signal->range_func_int32(_value)) {
Expand All @@ -164,9 +160,7 @@ int network_marshal_signals_to_messages(Network* n, MarshalItem* mi)
sizeof(uint32_t)],
mi->signal->name);
}
}

if (strcmp(mi->signal->member_type, "uint64_t") == 0) {
} else if (strcmp(mi->signal->member_type, "uint64_t") == 0) {
uint64_t _value = 0;
_value = mi->signal->encode_func_int64(_signal_value);
if (mi->signal->range_func_int64(_value)) {
Expand All @@ -179,9 +173,7 @@ int network_marshal_signals_to_messages(Network* n, MarshalItem* mi)
sizeof(uint64_t)],
mi->signal->name);
}
}

if (strcmp(mi->signal->member_type, "int8_t") == 0) {
} else if (strcmp(mi->signal->member_type, "int8_t") == 0) {
int8_t _value = 0;
_value = mi->signal->encode_func_int8(_signal_value);
if (mi->signal->range_func_int8(_value)) {
Expand All @@ -193,9 +185,7 @@ int network_marshal_signals_to_messages(Network* n, MarshalItem* mi)
sizeof(int8_t)],
mi->signal->name);
}
}

if (strcmp(mi->signal->member_type, "int16_t") == 0) {
} else if (strcmp(mi->signal->member_type, "int16_t") == 0) {
int16_t _value = 0;
_value = mi->signal->encode_func_int16(_signal_value);
if (mi->signal->range_func_int16(_value)) {
Expand All @@ -207,9 +197,7 @@ int network_marshal_signals_to_messages(Network* n, MarshalItem* mi)
sizeof(int16_t)],
mi->signal->name);
}
}

if (strcmp(mi->signal->member_type, "int32_t") == 0) {
} else if (strcmp(mi->signal->member_type, "int32_t") == 0) {
int32_t _value = 0;
_value = mi->signal->encode_func_int32(_signal_value);
if (mi->signal->range_func_int32(_value)) {
Expand All @@ -221,8 +209,7 @@ int network_marshal_signals_to_messages(Network* n, MarshalItem* mi)
sizeof(int32_t)],
mi->signal->name);
}
}
if (strcmp(mi->signal->member_type, "int64_t") == 0) {
} else if (strcmp(mi->signal->member_type, "int64_t") == 0) {
int16_t _value = 0;
_value = mi->signal->encode_func_int64(_signal_value);
if (mi->signal->range_func_int64(_value)) {
Expand All @@ -234,8 +221,7 @@ int network_marshal_signals_to_messages(Network* n, MarshalItem* mi)
sizeof(int64_t)],
mi->signal->name);
}
}
if (strcmp(mi->signal->member_type, "float") == 0) {
} else if (strcmp(mi->signal->member_type, "float") == 0) {
float _value = 0;
_value = mi->signal->encode_func_float(_signal_value);
if (mi->signal->range_func_float(_value)) {
Expand All @@ -248,8 +234,7 @@ int network_marshal_signals_to_messages(Network* n, MarshalItem* mi)
sizeof(float)],
mi->signal->name);
}
}
if (strcmp(mi->signal->member_type, "double") == 0) {
} else if (strcmp(mi->signal->member_type, "double") == 0) {
double _value = 0;
_value = mi->signal->encode_func_double(_signal_value);
if (mi->signal->range_func_double(_value)) {
Expand All @@ -261,28 +246,27 @@ int network_marshal_signals_to_messages(Network* n, MarshalItem* mi)
sizeof(double)],
mi->signal->name);
}
} else {
log_error("Unknown type: %s (frame_id=%d, message=%s, signal=%s)",
mi->signal->member_type, mi->message->frame_id,
mi->message->name, mi->signal->name);
}


/* Next item. */
mi++;
}
return 0;
}


int network_marshal_messages_to_signals(
Network* n, MarshalItem* mi, bool single)
Network* n, MarshalItem* marshal_list, bool single)
{
if (n == NULL || mi == NULL) return 1;
while (mi->signal) {
log_trace(
if (n == NULL || marshal_list == NULL) return 1;
for (MarshalItem* mi = marshal_list; mi && mi->signal; mi++) {
log_debug(
"MI Signal: frame_id=%d, update_signals=%d, index=%d, type=%s",
mi->message->frame_id, mi->message->update_signals,
mi->signal_vector_index, mi->signal->member_type);
if (mi->message->update_signals == false && single == false) {
/* Next item (force if single == true). */
mi++;
/* Next item (will be forced if single == true). */
continue;
}
if (strcmp(mi->signal->member_type, "uint8_t") == 0) {
Expand All @@ -301,8 +285,7 @@ int network_marshal_messages_to_signals(
_v, n->signal_vector[mi->signal_vector_index],
mi->signal->name);
}
}
if (strcmp(mi->signal->member_type, "uint16_t") == 0) {
} else if (strcmp(mi->signal->member_type, "uint16_t") == 0) {
if (mi->signal->range_func_int16((
(int16_t*)mi->message->buffer)[(mi->signal->buffer_offset) /
sizeof(int16_t)])) {
Expand All @@ -318,8 +301,7 @@ int network_marshal_messages_to_signals(
_v, n->signal_vector[mi->signal_vector_index],
mi->signal->name);
}
}
if (strcmp(mi->signal->member_type, "uint32_t") == 0) {
} else if (strcmp(mi->signal->member_type, "uint32_t") == 0) {
if (mi->signal->range_func_int32((
(int32_t*)mi->message->buffer)[(mi->signal->buffer_offset) /
sizeof(int32_t)])) {
Expand All @@ -335,8 +317,7 @@ int network_marshal_messages_to_signals(
_v, n->signal_vector[mi->signal_vector_index],
mi->signal->name);
}
}
if (strcmp(mi->signal->member_type, "uint64_t") == 0) {
} else if (strcmp(mi->signal->member_type, "uint64_t") == 0) {
if (mi->signal->range_func_int64((
(int64_t*)mi->message->buffer)[(mi->signal->buffer_offset) /
sizeof(int64_t)])) {
Expand All @@ -352,8 +333,7 @@ int network_marshal_messages_to_signals(
_v, n->signal_vector[mi->signal_vector_index],
mi->signal->name);
}
}
if (strcmp(mi->signal->member_type, "int8_t") == 0) {
} else if (strcmp(mi->signal->member_type, "int8_t") == 0) {
if (mi->signal->range_func_int8(
((int8_t*)mi->message->buffer)[(mi->signal->buffer_offset) /
sizeof(int8_t)])) {
Expand All @@ -368,8 +348,7 @@ int network_marshal_messages_to_signals(
_v, n->signal_vector[mi->signal_vector_index],
mi->signal->name);
}
}
if (strcmp(mi->signal->member_type, "int16_t") == 0) {
} else if (strcmp(mi->signal->member_type, "int16_t") == 0) {
if (mi->signal->range_func_int16((
(int16_t*)mi->message->buffer)[(mi->signal->buffer_offset) /
sizeof(int16_t)])) {
Expand All @@ -385,8 +364,7 @@ int network_marshal_messages_to_signals(
_v, n->signal_vector[mi->signal_vector_index],
mi->signal->name);
}
}
if (strcmp(mi->signal->member_type, "int32_t") == 0) {
} else if (strcmp(mi->signal->member_type, "int32_t") == 0) {
if (mi->signal->range_func_int32((
(int32_t*)mi->message->buffer)[(mi->signal->buffer_offset) /
sizeof(int32_t)])) {
Expand All @@ -402,8 +380,7 @@ int network_marshal_messages_to_signals(
_v, n->signal_vector[mi->signal_vector_index],
mi->signal->name);
}
}
if (strcmp(mi->signal->member_type, "int64_t") == 0) {
} else if (strcmp(mi->signal->member_type, "int64_t") == 0) {
if (mi->signal->range_func_int64((
(int64_t*)mi->message->buffer)[(mi->signal->buffer_offset) /
sizeof(int64_t)])) {
Expand All @@ -419,8 +396,7 @@ int network_marshal_messages_to_signals(
_v, n->signal_vector[mi->signal_vector_index],
mi->signal->name);
}
}
if (strcmp(mi->signal->member_type, "float") == 0) {
} else if (strcmp(mi->signal->member_type, "float") == 0) {
if (mi->signal->range_func_float(
((float*)mi->message->buffer)[(mi->signal->buffer_offset) /
sizeof(float)])) {
Expand All @@ -435,8 +411,7 @@ int network_marshal_messages_to_signals(
_v, n->signal_vector[mi->signal_vector_index],
mi->signal->name);
}
}
if (strcmp(mi->signal->member_type, "double") == 0) {
} else if (strcmp(mi->signal->member_type, "double") == 0) {
if (mi->signal->range_func_double(
((double*)mi->message->buffer)[(mi->signal->buffer_offset) /
sizeof(double)])) {
Expand All @@ -451,10 +426,14 @@ int network_marshal_messages_to_signals(
_v, n->signal_vector[mi->signal_vector_index],
mi->signal->name);
}
} else {
log_error("Unknown type: %s (frame_id=%d, message=%s, signal=%s)",
mi->signal->member_type, mi->message->frame_id,
mi->message->name, mi->signal->name);
}
/* Next item. */
if (single) break; /* Only process the single MI. */
mi++;

/* When single is true, process the single MI _ONLY_. */
if (single) return 0;
}

/* Reset the message processing flags. */
Expand Down
51 changes: 51 additions & 0 deletions tests/cmocka/network/test_engine.c
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,55 @@ void test_engine_marshal_container_mux_signal(void** state)
}


void test_engine_marshal_to_single_signal(void** state)
{
UNUSED(state);

/* Get the Mock objects. */
NetworkMock* mock = *state;
Network* n = mock->network;

/* Call load. */
network_load(mock->network, mock->model_instance);
assert_non_null(n);
assert_non_null(n->messages);
assert_non_null(n->signal_vector);
assert_non_null(n->signal_count);
assert_non_null(n->marshal_list);

/* Locate a mux MI. */
int32_t m600_idx = _find_message_idx(n, "mux_message");
assert_in_range(m600_idx, 0, 10);
NetworkMessage* m600 = &n->messages[m600_idx];
assert_non_null(m600);
assert_non_null(m600->mux_signal);
NetworkSignal* s600 = m600->mux_signal;
assert_string_equal(s600->name, "header_id");
assert_non_null(s600->mux_mi);
MarshalItem* mi600 = s600->mux_mi;
assert_ptr_equal(mi600->signal, s600);

/* Test that single=true does not modify update_signals. */
set_update_signals(n->marshal_list, false);
network_marshal_messages_to_signals(n, mi600, true);
for (MarshalItem* mi = n->marshal_list; mi && mi->signal; mi++) {
if (mi->message) {
assert_false(mi->message->update_signals);
}
}
set_update_signals(n->marshal_list, true);
network_marshal_messages_to_signals(n, mi600, true);
for (MarshalItem* mi = n->marshal_list; mi && mi->signal; mi++) {
if (mi->message) {
assert_true(mi->message->update_signals);
}
}


network_unload(mock->network);
}


extern int test_network_setup(void** state);
extern int test_network_teardown(void** state);

Expand All @@ -414,6 +463,8 @@ int run_engine_tests(void)
test_engine_marshal_container_message, s, t),
cmocka_unit_test_setup_teardown(
test_engine_marshal_container_mux_signal, s, t),
cmocka_unit_test_setup_teardown(
test_engine_marshal_to_single_signal, s, t),
};

return cmocka_run_group_tests_name("ENGINE", tests, NULL, NULL);
Expand Down

0 comments on commit 5069c08

Please sign in to comment.