cansend: re-order socket handling and parsing of input
Do all the socket handling (setting address, bind, socket options) in the beginning. Only parse the canframe argument after this is done. This has as effect that wrong arguments are only reported after the device has been opened and bound. This is preparation for sending multiple CAN frames in one cansend invocation. Signed-off-by: Arnout Vandecappelle (Essensium/Mind) <arnout@mind.be>pull/93/head
parent
7a4dd732af
commit
f811b53dfe
56
cansend.c
56
cansend.c
|
|
@ -71,23 +71,6 @@ int main(int argc, char **argv)
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* parse CAN frame */
|
|
||||||
required_mtu = parse_canframe(argv[2], &frame);
|
|
||||||
if (!required_mtu){
|
|
||||||
fprintf(stderr, "\nWrong CAN-frame format! Try:\n\n");
|
|
||||||
fprintf(stderr, " <can_id>#{R|data} for CAN 2.0 frames\n");
|
|
||||||
fprintf(stderr, " <can_id>##<flags>{data} for CAN FD frames\n\n");
|
|
||||||
fprintf(stderr, "<can_id> can have 3 (SFF) or 8 (EFF) hex chars\n");
|
|
||||||
fprintf(stderr, "{data} has 0..8 (0..64 CAN FD) ASCII hex-values (optionally");
|
|
||||||
fprintf(stderr, " separated by '.')\n");
|
|
||||||
fprintf(stderr, "<flags> a single ASCII Hex value (0 .. F) which defines");
|
|
||||||
fprintf(stderr, " canfd_frame.flags\n\n");
|
|
||||||
fprintf(stderr, "e.g. 5A1#11.2233.44556677.88 / 123#DEADBEEF / 5AA# / ");
|
|
||||||
fprintf(stderr, "123##1 / 213##311\n 1F334455#1122334455667788 / 123#R ");
|
|
||||||
fprintf(stderr, "for remote transmission request.\n\n");
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* open socket */
|
/* open socket */
|
||||||
if ((s = socket(PF_CAN, SOCK_RAW, CAN_RAW)) < 0) {
|
if ((s = socket(PF_CAN, SOCK_RAW, CAN_RAW)) < 0) {
|
||||||
perror("socket");
|
perror("socket");
|
||||||
|
|
@ -106,6 +89,34 @@ int main(int argc, char **argv)
|
||||||
addr.can_family = AF_CAN;
|
addr.can_family = AF_CAN;
|
||||||
addr.can_ifindex = ifr.ifr_ifindex;
|
addr.can_ifindex = ifr.ifr_ifindex;
|
||||||
|
|
||||||
|
/* disable default receive filter on this RAW socket */
|
||||||
|
/* This is obsolete as we do not read from the socket at all, but for */
|
||||||
|
/* this reason we can remove the receive list in the Kernel to save a */
|
||||||
|
/* little (really a very little!) CPU usage. */
|
||||||
|
setsockopt(s, SOL_CAN_RAW, CAN_RAW_FILTER, NULL, 0);
|
||||||
|
|
||||||
|
if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
|
||||||
|
perror("bind");
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* parse CAN frame */
|
||||||
|
required_mtu = parse_canframe(argv[2], &frame);
|
||||||
|
if (!required_mtu){
|
||||||
|
fprintf(stderr, "\nWrong CAN-frame format! Try:\n\n");
|
||||||
|
fprintf(stderr, " <can_id>#{R|data} for CAN 2.0 frames\n");
|
||||||
|
fprintf(stderr, " <can_id>##<flags>{data} for CAN FD frames\n\n");
|
||||||
|
fprintf(stderr, "<can_id> can have 3 (SFF) or 8 (EFF) hex chars\n");
|
||||||
|
fprintf(stderr, "{data} has 0..8 (0..64 CAN FD) ASCII hex-values (optionally");
|
||||||
|
fprintf(stderr, " separated by '.')\n");
|
||||||
|
fprintf(stderr, "<flags> a single ASCII Hex value (0 .. F) which defines");
|
||||||
|
fprintf(stderr, " canfd_frame.flags\n\n");
|
||||||
|
fprintf(stderr, "e.g. 5A1#11.2233.44556677.88 / 123#DEADBEEF / 5AA# / ");
|
||||||
|
fprintf(stderr, "123##1 / 213##311\n 1F334455#1122334455667788 / 123#R ");
|
||||||
|
fprintf(stderr, "for remote transmission request.\n\n");
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
if (required_mtu > CAN_MTU) {
|
if (required_mtu > CAN_MTU) {
|
||||||
|
|
||||||
/* check if the frame fits into the CAN netdevice */
|
/* check if the frame fits into the CAN netdevice */
|
||||||
|
|
@ -131,17 +142,6 @@ int main(int argc, char **argv)
|
||||||
frame.len = can_dlc2len(can_len2dlc(frame.len));
|
frame.len = can_dlc2len(can_len2dlc(frame.len));
|
||||||
}
|
}
|
||||||
|
|
||||||
/* disable default receive filter on this RAW socket */
|
|
||||||
/* This is obsolete as we do not read from the socket at all, but for */
|
|
||||||
/* this reason we can remove the receive list in the Kernel to save a */
|
|
||||||
/* little (really a very little!) CPU usage. */
|
|
||||||
setsockopt(s, SOL_CAN_RAW, CAN_RAW_FILTER, NULL, 0);
|
|
||||||
|
|
||||||
if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
|
|
||||||
perror("bind");
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* send frame */
|
/* send frame */
|
||||||
if (write(s, &frame, required_mtu) != required_mtu) {
|
if (write(s, &frame, required_mtu) != required_mtu) {
|
||||||
perror("write");
|
perror("write");
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue