Skip to content

Commit

Permalink
get message length from pdu intead of recv
Browse files Browse the repository at this point in the history
  • Loading branch information
kamelfakihh committed Jun 13, 2024
1 parent 42c05af commit 30e9598
Showing 1 changed file with 31 additions and 40 deletions.
71 changes: 31 additions & 40 deletions examples/acf-can/acf-can-listener.c
Original file line number Diff line number Diff line change
Expand Up @@ -168,42 +168,12 @@ void print_can_acf(uint8_t* acf_pdu)
fprintf(stderr, "Pad: %"PRIu64"\n", pad);
}

static int is_valid_cf_packet(uint8_t* pdu,
ssize_t *proc_bytes) {

uint64_t subtype;
uint64_t pdu_length;
int res;

res = Avtp_CommonHeader_GetField((Avtp_CommonHeader_t*)pdu, AVTP_COMMON_HEADER_FIELD_SUBTYPE, &subtype);
if (res < 0) {
fprintf(stderr, "Failed to get subtype field: %d\n", res);
return 0;
}

if (!((subtype == AVTP_SUBTYPE_NTSCF) ||
(subtype == AVTP_SUBTYPE_TSCF))) {
fprintf(stderr, "Subtype mismatch: expected %u or %u, got %"PRIu64"\n",
AVTP_SUBTYPE_NTSCF, AVTP_SUBTYPE_TSCF, subtype);
return 0;
}

if (subtype == AVTP_SUBTYPE_NTSCF) {
pdu_length = AVTP_NTSCF_HEADER_LEN;
} else if (subtype == AVTP_SUBTYPE_TSCF) {
pdu_length = AVTP_TSCF_HEADER_LEN;
}
*proc_bytes += pdu_length;
return 1;

}

static int new_packet(int sk_fd, int can_socket) {

int res;
ssize_t recd_bytes, proc_bytes = 0;
int res;
uint64_t msg_length, proc_bytes = 0;
uint64_t can_frame_id, udp_seq_num = 0, subtype;
uint16_t payload_length, pdu_length;
uint64_t can_frame_id, udp_seq_num = 0;
uint8_t *can_payload, i;
uint8_t pdu[MAX_PDU_SIZE];
uint8_t* cf_pdu;
Expand All @@ -212,27 +182,48 @@ static int new_packet(int sk_fd, int can_socket) {
char stdout_string[1000] = "\0";
struct can_frame frame;

recd_bytes = recv(sk_fd, pdu, MAX_PDU_SIZE, 0);
if (recd_bytes < 0 || recd_bytes > MAX_PDU_SIZE) {
res = recv(sk_fd, pdu, MAX_PDU_SIZE, 0);

if (res < 0 || res > MAX_PDU_SIZE) {
perror("Failed to receive data");
return -1;
}

if (use_udp) {
udp_pdu = (Avtp_UDP_t *) pdu;
Avtp_UDP_GetField(udp_pdu, AVTP_UDP_FIELD_ENCAPSULATION_SEQ_NO, &udp_seq_num);
cf_pdu = pdu + sizeof(Avtp_UDP_t);
proc_bytes += sizeof(Avtp_UDP_t);
cf_pdu = pdu + sizeof(Avtp_UDP_t);
} else {
cf_pdu = pdu;
}

if (!is_valid_cf_packet(cf_pdu, &proc_bytes)) {
fprintf(stderr, "Dropping packet\n");
res = Avtp_CommonHeader_GetField((Avtp_CommonHeader_t*)pdu, AVTP_COMMON_HEADER_FIELD_SUBTYPE, &subtype);
if (res < 0) {
fprintf(stderr, "Failed to get subtype field: %d\n", res);
return 0;
}

if (!((subtype == AVTP_SUBTYPE_NTSCF) ||
(subtype == AVTP_SUBTYPE_TSCF))) {
fprintf(stderr, "Subtype mismatch: expected %u or %u, got %"PRIu64". Dropping packet\n",
AVTP_SUBTYPE_NTSCF, AVTP_SUBTYPE_TSCF, subtype);
return -1;
}

if(subtype == AVTP_SUBTYPE_TSCF){
proc_bytes += AVTP_TSCF_HEADER_LEN;
res = Avtp_Tscf_GetField((Avtp_Tscf_t*)pdu, AVTP_TSCF_FIELD_STREAM_DATA_LENGTH, (uint64_t *) &msg_length);
}else{
proc_bytes += AVTP_NTSCF_HEADER_LEN;
res = Avtp_Ntscf_GetField((Avtp_Ntscf_t*)pdu, AVTP_NTSCF_FIELD_NTSCF_DATA_LENGTH, (uint64_t *) &msg_length);
}

if (res < 0) {
fprintf(stderr, "Failed to get message length: %d\n", res);
return 0;
}

while (proc_bytes < recd_bytes) {
while (proc_bytes < msg_length) {

acf_pdu = &pdu[proc_bytes];

Expand Down

0 comments on commit 30e9598

Please sign in to comment.