Skip to content

Commit

Permalink
j1939cat: add broadcast support
Browse files Browse the repository at this point in the history
Allow to use j1939cat to send and receive broadcast (TP.BAM) messages.

Signed-off-by: Oleksij Rempel <o.rempel@pengutronix.de>
  • Loading branch information
olerem committed Aug 7, 2020
1 parent bad9051 commit 327587b
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion j1939cat.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ struct j1939cat_priv {
bool todo_recv;
bool todo_filesize;
bool todo_connect;
int todo_broadcast;

unsigned long polltimeout;

Expand All @@ -77,14 +78,15 @@ static const char help_msg[] =
" -P <timeout> poll timeout in milliseconds before sending data.\n"
" With this option send() will be used with MSG_DONTWAIT flag.\n"
" -R <count> Set send repeat count. Default: 1\n"
" -B Allow to send and receive broadcast packets.\n"
"\n"
"Example:\n"
"j1939cat -i some_file_to_send can0:0x80 :0x90,0x12300\n"
"j1939cat can0:0x90 -r > /tmp/some_file_to_receive\n"
"\n"
;

static const char optstring[] = "?hi:vs:rp:P:R:";
static const char optstring[] = "?hi:vs:rp:P:R:B";


static void j1939cat_init_sockaddr_can(struct sockaddr_can *sac)
Expand Down Expand Up @@ -560,6 +562,16 @@ static int j1939cat_sock_prepare(struct j1939cat_priv *priv)
(char *) &sock_opt, sizeof(sock_opt)))
err(1, "setsockopt timestamping");

if (priv->todo_broadcast) {
ret = setsockopt(priv->sock, SOL_SOCKET, SO_BROADCAST,
&priv->todo_broadcast,
sizeof(priv->todo_broadcast));
if (ret < 0) {
warn("setsockopt: filed to set broadcast");
return EXIT_FAILURE;
}
}

ret = bind(priv->sock, (void *)&priv->sockname, sizeof(priv->sockname));
if (ret < 0) {
warn("bind()");
Expand Down Expand Up @@ -619,6 +631,9 @@ static int j1939cat_parse_args(struct j1939cat_priv *priv, int argc, char *argv[
if (priv->repeat < 1)
err(EXIT_FAILURE, "send/repeat count can't be less then 1\n");
break;
case 'B':
priv->todo_broadcast = 1;
break;
case 'h': /*fallthrough*/
default:
fputs(help_msg, stderr);
Expand Down

0 comments on commit 327587b

Please sign in to comment.