Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Read PDU to send from binary file. #419

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 29 additions & 2 deletions isotpsend.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
*
*/

#include <fcntl.h>
#include <libgen.h>
#include <stdio.h>
#include <stdlib.h>
Expand Down Expand Up @@ -78,12 +79,32 @@ void print_usage(char *prg)
fprintf(stderr, " -S (SF broadcast mode - for functional addressing)\n");
fprintf(stderr, " -C (CF broadcast mode - no wait for flow controls)\n");
fprintf(stderr, " -L <mtu>:<tx_dl>:<tx_flags> (link layer options for CAN FD)\n");
fprintf(stderr, " -i <filename> (Read PDU data from binary file instead of STDIN)\n");
fprintf(stderr, "\nCAN IDs and addresses are given and expected in hexadecimal values.\n");
fprintf(stderr, "The pdu data is expected on STDIN in space separated ASCII hex values.\n");
fprintf(stderr, "(*) = Use '-t %s' to set N_As to zero for Linux version 5.18+\n", ZERO_STRING);
fprintf(stderr, "\n");
}

ssize_t read_binary_file(void *buffer, const size_t siz_buffer, const char *filename)
{
int fd = -1;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need for initialization

ssize_t num_read = -1;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dito


if( buffer == NULL || siz_buffer < 1 || filename == NULL ) {
return 0;
}

if( (fd = open(filename, O_RDONLY)) == -1 ) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no assignments in if-statements
please adopt the coding style with parenthesis and spaces

return 0;
}

num_read = read(fd, buffer, siz_buffer);
close(fd);

return num_read > 0 ? num_read : 0;
}

int main(int argc, char **argv)
{
int s;
Expand All @@ -102,7 +123,7 @@ int main(int argc, char **argv)

addr.can_addr.tp.tx_id = addr.can_addr.tp.rx_id = NO_CAN_ID;

while ((opt = getopt(argc, argv, "s:d:x:p:P:t:f:D:l:g:bSCL:?")) != -1) {
while ((opt = getopt(argc, argv, "s:d:x:p:P:t:f:D:l:g:bSCL:i:?")) != -1) {
switch (opt) {
case 's':
addr.can_addr.tp.tx_id = strtoul(optarg, NULL, 16);
Expand Down Expand Up @@ -227,6 +248,10 @@ int main(int argc, char **argv)
}
break;

case 'i':
buflen = read_binary_file(&buf[0], sizeof(buf), optarg);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use 'buf' instead of '&buf[0]'

break;

case '?':
print_usage(basename(argv[0]));
exit(0);
Expand Down Expand Up @@ -285,13 +310,15 @@ int main(int argc, char **argv)
exit(1);
}

if( buflen < 1 ) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if (buflen > 0) { ... ??

additional the indention seems wrong

if (!datalen) {
while (buflen < BUFSIZE && scanf("%hhx", &buf[buflen]) == 1)
buflen++;
} else {
for (buflen = 0; buflen < datalen; buflen++)
buf[buflen] = ((buflen % 0xFF) + 1) & 0xFF;
}
}

loop:
if (usecs)
Expand All @@ -313,7 +340,7 @@ int main(int argc, char **argv)
goto loop;
}

/*
/*
* due to a Kernel internal wait queue the PDU is sent completely
* before close() returns.
*/
Expand Down