]> rtime.felk.cvut.cz Git - can-utils.git/blobdiff - cansend.c
candump: Enable HW timestamping before using it
[can-utils.git] / cansend.c
index dd9d73edbb0a7befc0a1ba6837e3ad618eac8ec9..b7f800339b521eddd5729860b792a072345ed00a 100644 (file)
--- a/cansend.c
+++ b/cansend.c
@@ -1,19 +1,14 @@
-/*
- *  $Id$
- */
-
 /*
  * cansend.c - simple command line tool to send CAN-frames via CAN_RAW sockets
  *
- * Copyright (c) 2002-2005 Volkswagen Group Electronic Research
+ * Copyright (c) 2002-2007 Volkswagen Group Electronic Research
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions
  * are met:
  * 1. Redistributions of source code must retain the above copyright
- *    notice, this list of conditions, the following disclaimer and
- *    the referenced file 'COPYING'.
+ *    notice, this list of conditions and the following disclaimer.
  * 2. Redistributions in binary form must reproduce the above copyright
  *    notice, this list of conditions and the following disclaimer in the
  *    documentation and/or other materials provided with the distribution.
@@ -23,8 +18,8 @@
  *
  * Alternatively, provided that this notice is retained in full, this
  * software may be distributed under the terms of the GNU General
- * Public License ("GPL") version 2 as distributed in the 'COPYING'
- * file from the main directory of the linux kernel source.
+ * Public License ("GPL") version 2, in which case the provisions of the
+ * GPL apply INSTEAD OF those given above.
  *
  * The provided data structures and external interfaces from this code
  * are not restricted to be used by modules with a GPL compatible license.
@@ -42,7 +37,7 @@
  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  * DAMAGE.
  *
- * Send feedback to <socketcan-users@lists.berlios.de>
+ * Send feedback to <linux-can@vger.kernel.org>
  *
  */
 
@@ -53,6 +48,7 @@
 
 #include <net/if.h>
 #include <sys/ioctl.h>
+#include <sys/socket.h>
 
 #include <linux/can.h>
 #include <linux/can/raw.h>
 
 int main(int argc, char **argv)
 {
-    int s; /* can raw socket */ 
-    int nbytes;
-    struct sockaddr_can addr;
-    struct can_frame frame;
-    struct ifreq ifr;
-
-    /* check command line options */
-    if (argc != 3) {
-        fprintf(stderr, "Usage: %s <device> <can_frame>.\n", argv[0]);
-        return 1;
-    }
-
-    /* parse CAN frame */
-    if (parse_canframe(argv[2], &frame)){
-       fprintf(stderr, "\nWrong CAN-frame format!\n\n");
-       fprintf(stderr, "Try: <can_id>#{R|data}\n");
-       fprintf(stderr, "can_id can have 3 (SFF) or 8 (EFF) hex chars\n");
-       fprintf(stderr, "data has 0 to 8 hex-values that can (optionally)");
-       fprintf(stderr, " be seperated by '.'\n\n");
-       fprintf(stderr, "e.g. 5A1#11.2233.44556677.88 / 123#DEADBEEF / ");
-       fprintf(stderr, "5AA# /\n     1F334455#1122334455667788 / 123#R ");
-       fprintf(stderr, "for remote transmission request.\n\n");
-       return 1;
-    }
-
-    /* open socket */
-    if ((s = socket(PF_CAN, SOCK_RAW, CAN_RAW)) < 0) {
-      perror("socket");
-      return 1;
-    }
-
-    strcpy(ifr.ifr_name, argv[1]);
-    ioctl(s, SIOCGIFINDEX, &ifr);
-
-    addr.can_family = AF_CAN;
-    addr.can_ifindex = ifr.ifr_ifindex;
-
-    if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
-      perror("bind");
-      return 1;
-    }
-
-    /* send frame */
-    if ((nbytes = write(s, &frame, sizeof(frame))) != sizeof(frame)) {
-      perror("write");
-      return 1;
-    }
-
-    //fprint_long_canframe(stdout, &frame, "\n", 0);
-
-    close(s);
-
-    return 0;
+       int s; /* can raw socket */ 
+       int required_mtu;
+       int mtu;
+       int enable_canfd = 1;
+       struct sockaddr_can addr;
+       struct canfd_frame frame;
+       struct ifreq ifr;
+
+       /* check command line options */
+       if (argc != 3) {
+               fprintf(stderr, "Usage: %s <device> <can_frame>.\n", argv[0]);
+               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 */
+       if ((s = socket(PF_CAN, SOCK_RAW, CAN_RAW)) < 0) {
+               perror("socket");
+               return 1;
+       }
+
+       strncpy(ifr.ifr_name, argv[1], IFNAMSIZ - 1);
+       ifr.ifr_name[IFNAMSIZ - 1] = '\0';
+       ifr.ifr_ifindex = if_nametoindex(ifr.ifr_name);
+       if (!ifr.ifr_ifindex) {
+               perror("if_nametoindex");
+               return 1;
+       }
+
+       addr.can_family = AF_CAN;
+       addr.can_ifindex = ifr.ifr_ifindex;
+
+       if (required_mtu > CAN_MTU) {
+
+               /* check if the frame fits into the CAN netdevice */
+               if (ioctl(s, SIOCGIFMTU, &ifr) < 0) {
+                       perror("SIOCGIFMTU");
+                       return 1;
+               }
+               mtu = ifr.ifr_mtu;
+
+               if (mtu != CANFD_MTU) {
+                       printf("CAN interface ist not CAN FD capable - sorry.\n");
+                       return 1;
+               }
+
+               /* interface is ok - try to switch the socket into CAN FD mode */
+               if (setsockopt(s, SOL_CAN_RAW, CAN_RAW_FD_FRAMES,
+                              &enable_canfd, sizeof(enable_canfd))){
+                       printf("error when enabling CAN FD support\n");
+                       return 1;
+               }
+
+               /* ensure discrete CAN FD length values 0..8, 12, 16, 20, 24, 32, 64 */
+               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 */
+       if (write(s, &frame, required_mtu) != required_mtu) {
+               perror("write");
+               return 1;
+       }
+
+       close(s);
+
+       return 0;
 }