Skip to content

Commit

Permalink
Code tidy.
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 May 6, 2024
1 parent 3b06377 commit 40b5257
Show file tree
Hide file tree
Showing 10 changed files with 349 additions and 398 deletions.
91 changes: 48 additions & 43 deletions dse/network/encoder.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,97 +30,102 @@ uint32_t simbus_generate_uid_hash(const uint8_t* key, size_t len)
}


static NetworkMessage* _find_mux_message(
Network* n, uint32_t mux_id, uint32_t frame_id)
{
if (mux_id == 0) return NULL;
for (NetworkMessage* nm = n->messages; nm && nm->name; nm++) {
if (nm->frame_id == frame_id) {
if (nm->mux_id == mux_id) {
return nm;
break;
}
}
}
return NULL;
}

static void _process_message(
Network* network, NetworkMessage* message, NCodecCanMessage* msg)
Network* n, NetworkMessage* nm, NCodecCanMessage* msg)
{
message->unpack_func(message->buffer, msg->buffer, msg->len);
if (message->mux_signal && message->mux_signal->mux_mi) {
nm->unpack_func(nm->buffer, msg->buffer, msg->len);
if (nm->mux_signal && nm->mux_signal->mux_mi) {
/* This is a container message, also process the contained message. */
MarshalItem* mi = message->mux_signal->mux_mi;
network_marshal_messages_to_signals(network, mi, true);
uint32_t mux_id = network->signal_vector[mi->signal_vector_index];
MarshalItem* mi = nm->mux_signal->mux_mi;
network_marshal_messages_to_signals(n, mi, true);
uint32_t mux_id = n->signal_vector[mi->signal_vector_index];
/* Locate the mux message. */
NetworkMessage* mux_message = NULL;
NetworkMessage* nm_p = network->messages;
while (mux_id && nm_p->frame_id) {
if (nm_p->frame_id == msg->frame_id) {
if (nm_p->mux_id == mux_id) {
mux_message = nm_p;
break;
}
}
/* Next message. */
nm_p++;
}
NetworkMessage* mux_message =
_find_mux_message(n, mux_id, msg->frame_id);
if (mux_message) {
_process_message(network, mux_message, msg);
_process_message(n, mux_message, msg);
}
}

/* Calculate the checksum for the payload. */
uint32_t payload_checksum =
simbus_generate_uid_hash(message->buffer, message->buffer_len);
message->update_signals = false;
if (payload_checksum == message->buffer_checksum) {
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);
return;
}
message->buffer_checksum = payload_checksum;
message->update_signals = true;
nm->buffer_checksum = payload_checksum;
nm->update_signals = true;
log_debug("decode path checksum %d", payload_checksum);
}

static void _process_can_frame(Network* network, NCodecCanMessage* msg)
static void _process_can_frame(Network* n, NCodecCanMessage* msg)
{
/* Find the matching message. */
NetworkMessage* message = NULL;
NetworkMessage* nm_p = network->messages;
while (nm_p->frame_id) {
if (nm_p->frame_id == msg->frame_id) {
message = nm_p;
NetworkMessage* nm = n->messages;
while (nm->frame_id) {
if (nm->frame_id == msg->frame_id) {
message = nm;
break;
}
/* Next message. */
nm_p++;
nm++;
}
if (message) {
_process_message(network, message, msg);
_process_message(n, message, msg);
} else {
log_debug("Network does not have frame_id : %d", msg->frame_id);
}
}

void network_decode_from_bus(Network* network, void* nc)
void network_decode_from_bus(Network* n, void* nc)
{
assert(network);
assert(n);
assert(nc);

while (1) {
NCodecCanMessage msg = {};
if (ncodec_read(nc, &msg) < 0) break;
_process_can_frame(network, &msg);
_process_can_frame(n, &msg);
}
ncodec_truncate(nc);
}


void network_encode_to_bus(Network* network, void* nc)
void network_encode_to_bus(Network* n, void* nc)
{
assert(network);
assert(n);
assert(nc);

for (NetworkMessage* nm_p = network->messages; nm_p->name; nm_p++) {
for (NetworkMessage* nm = n->messages; nm->name; nm++) {
/* Check if this message should be TXed? */
if (nm_p->needs_tx == false) continue;
if (nm->needs_tx == false) continue;
/* Message TX and and clear the needs_tx flag. */
int rc = ncodec_write(nc, &(struct NCodecCanMessage){
.frame_id = nm_p->frame_id,
.frame_type = nm_p->frame_type,
.buffer = (uint8_t*)nm_p->payload,
.len = nm_p->payload_len,
.frame_id = nm->frame_id,
.frame_type = nm->frame_type,
.buffer = (uint8_t*)nm->payload,
.len = nm->payload_len,
});
if (rc < 0) log_error("Unable to write CAN Frame to ncodec object!");
nm_p->needs_tx = false;
nm->needs_tx = false;
}
ncodec_flush(nc);
}
Loading

0 comments on commit 40b5257

Please sign in to comment.