Skip to content

Commit

Permalink
extend talker/listener to handle multiple CAN frames per Ethernet frame
Browse files Browse the repository at this point in the history
  • Loading branch information
JLPLabs committed Jul 24, 2024
1 parent 1be9db1 commit 5fd5bcf
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 9 deletions.
2 changes: 1 addition & 1 deletion examples/acf-can/acf-can-listener.c
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ static int new_packet(int sk_fd, int can_socket) {

while (msg_proc_bytes < msg_length) {

acf_pdu = &pdu[proc_bytes];
acf_pdu = &pdu[proc_bytes + msg_proc_bytes];

if (!is_valid_acf_packet(acf_pdu)) {
fprintf(stderr, "Error: Invalid ACF packet.\n");
Expand Down
29 changes: 21 additions & 8 deletions examples/acf-can/acf-can-talker.c
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,16 @@ static int priority = -1;
static uint8_t seq_num = 0;
static uint8_t use_tscf;
static uint8_t use_udp;
static uint8_t multi_can_frames = 1;
static char can_ifname[IFNAMSIZ] = "STDIN\0";

static char doc[] = "\nacf-can-talker -- a program designed to send CAN messages to \
a remote CAN bus over Ethernet using Open1722 \
\vEXAMPLES\
\n\n acf-can-talker eth0 aa:bb:cc:ee:dd:ff\
\n\n (tunnel transactions from STDIN to a remote CAN bus over Ethernet)\
\n\n acf-can-talker --count 10 eth0 aa:bb:cc:ee:dd:ff\
\n\n (as above, but pack 10 CAN frames in one Ethernet frame)\
\n\n acf-can-talker -u 10.0.0.2:17220 vcan1\
\n\n (tunnel transactions from can1 interface to a remote CAN bus over IP)\
\n\n candump can1 | acf-can-talker -u 10.0.0.2:17220\
Expand All @@ -79,6 +82,7 @@ static char args_doc[] = "[ifname] dst-mac-address/dst-nw-address:port [can ifna
static struct argp_option options[] = {
{"tscf", 't', 0, 0, "Use TSCF"},
{"udp", 'u', 0, 0, "Use UDP" },
{"count", 'c', "COUNT", 0, "Set count of CAN messages per Ethernet frame"},
{"can ifname", 0, 0, OPTION_DOC, "CAN interface (set to STDIN by default)"},
{"ifname", 0, 0, OPTION_DOC, "Network interface (If Ethernet)"},
{"dst-mac-address", 0, 0, OPTION_DOC, "Stream destination MAC address (If Ethernet)"},
Expand All @@ -98,6 +102,9 @@ static error_t parser(int key, char *arg, struct argp_state *state)
case 'u':
use_udp = 1;
break;
case 'c':
multi_can_frames = atoi(arg);
break;

case ARGP_KEY_NO_ARGS:
argp_usage(state);
Expand Down Expand Up @@ -276,6 +283,8 @@ int main(int argc, char *argv[])
if (fd < 0)
return 1;

num_acf_msgs = multi_can_frames;

// Open a CAN socket for reading frames if required
if (strcmp(can_ifname, "STDIN\0")) {
can_socket = socket(PF_CAN, SOCK_RAW, CAN_RAW);
Expand Down Expand Up @@ -307,12 +316,6 @@ int main(int argc, char *argv[])
// Sending loop
for(;;) {

// Get payload
res = get_payload(can_socket, payload, &frame_id, &payload_length);
if (!res) {
continue;
}

// Pack into control formats
uint8_t *cf_pdu;
pdu_length = 0;
Expand All @@ -331,12 +334,22 @@ int main(int argc, char *argv[])
goto err;
pdu_length += res;

for (int i = 0; i < num_acf_msgs; i++) {
int i = 0;
while (i < num_acf_msgs) {
// Get payload -- will 'spin' here until we get the requested number
// of CAN frames.
res = get_payload(can_socket, payload, &frame_id, &payload_length);
if (!res) {
continue;
}

uint8_t* acf_pdu = cf_pdu + pdu_length;
res = prepare_acf_packet(acf_pdu, payload, payload_length, frame_id);
if (res < 0)
goto err;
pdu_length += res;

i++;
}

res = update_pdu_length(cf_pdu, pdu_length);
Expand Down Expand Up @@ -365,4 +378,4 @@ int main(int argc, char *argv[])
close(fd);
return 1;

}
}

0 comments on commit 5fd5bcf

Please sign in to comment.