Skip to content

Commit

Permalink
Merge pull request #39 from boschglobal/fix/can_examples
Browse files Browse the repository at this point in the history
Fixed the crashing of acf-can-listener when receiving other ACF packe…
  • Loading branch information
nayakned committed Sep 4, 2024
2 parents 5a51e07 + 928ff88 commit 89cf5a6
Showing 1 changed file with 4 additions and 26 deletions.
30 changes: 4 additions & 26 deletions examples/acf-can/acf-can-listener.c
Original file line number Diff line number Diff line change
Expand Up @@ -144,31 +144,12 @@ static int is_valid_acf_packet(uint8_t* acf_pdu)
Avtp_AcfCommon_t *pdu = (Avtp_AcfCommon_t*) acf_pdu;
uint8_t acf_msg_type = Avtp_AcfCommon_GetAcfMsgType(pdu);
if (acf_msg_type != AVTP_ACF_TYPE_CAN) {
fprintf(stderr, "ACF type mismatch: expected %"PRIu8", got %"PRIu8"\n",
AVTP_ACF_TYPE_CAN, acf_msg_type);
return 0;
}

return 1;
}

void print_can_acf(uint8_t* acf_pdu)
{
Avtp_Can_t *pdu = (Avtp_Can_t*) acf_pdu;
uint16_t acf_msg_len = Avtp_Can_GetAcfMsgLength(pdu);
uint8_t can_bus_id = Avtp_Can_GetCanBusId(pdu);
uint64_t timestamp = Avtp_Can_GetMessageTimestamp(pdu);
uint32_t can_identifier = Avtp_Can_GetCanIdentifier(pdu);
uint8_t pad = Avtp_Can_GetPad(pdu);

fprintf(stderr, "------------------------------------\n");
fprintf(stderr, "Msg Length: %"PRIu16"\n", acf_msg_len);
fprintf(stderr, "Can Bus ID: %"PRIu8"\n", can_bus_id);
fprintf(stderr, "Timestamp: %"PRIu64"", timestamp);
fprintf(stderr, "Can Identifier: %"PRIu32"\n", can_identifier);
fprintf(stderr, "Pad: %"PRIu8"\n", pad);
}

static int new_packet(int sk_fd, int can_socket) {

int res = 0;
Expand All @@ -185,7 +166,7 @@ static int new_packet(int sk_fd, int can_socket) {
res = recv(sk_fd, pdu, MAX_PDU_SIZE, 0);
if (res < 0 || res > MAX_PDU_SIZE) {
perror("Failed to receive data");
return -1;
return 0;
}

if (use_udp) {
Expand All @@ -200,9 +181,7 @@ static int new_packet(int sk_fd, int can_socket) {
subtype = Avtp_CommonHeader_GetSubtype((Avtp_CommonHeader_t*)cf_pdu);
if (!((subtype == AVTP_SUBTYPE_NTSCF) ||
(subtype == AVTP_SUBTYPE_TSCF))) {
fprintf(stderr, "Subtype mismatch: expected %u or %u, got %"PRIu8". Dropping packet\n",
AVTP_SUBTYPE_NTSCF, AVTP_SUBTYPE_TSCF, subtype);
return -1;
return 0;
}

if (subtype == AVTP_SUBTYPE_TSCF){
Expand All @@ -218,8 +197,7 @@ static int new_packet(int sk_fd, int can_socket) {
acf_pdu = &pdu[proc_bytes + msg_proc_bytes];

if (!is_valid_acf_packet(acf_pdu)) {
fprintf(stderr, "Error: Invalid ACF packet.\n");
return -1;
return 0;
}

can_id = Avtp_Can_GetCanIdentifier((Avtp_Can_t*)acf_pdu);
Expand All @@ -234,7 +212,7 @@ static int new_packet(int sk_fd, int can_socket) {
can_id |= CAN_EFF_FLAG;
} else if (can_id > 0x7FF) {
fprintf(stderr, "Error: CAN ID is > 0x7FF but the EFF bit is not set.\n");
return -1;
return 0;
}

// Handle RTR Flag
Expand Down

0 comments on commit 89cf5a6

Please sign in to comment.