1 ============================================================================
3 can-sockets.txt : general socketCAN API documentation
5 See can-raw.txt and can-bcm.txt for in-depth documentation
6 on RAW and BCM sockets.
8 Part of the documentation for the socketCAN subsystem
12 1 How to use Socket CAN
15 ============================================================================
17 1. How to use Socket CAN
18 ------------------------
20 Like TCP/IP, you first need to open a socket for communicating over a
21 CAN network. Since Socket CAN implements a new protocol family, you
22 need to pass PF_CAN as the first argument to the socket(2) system
23 call. Currently, there are two CAN protocols to choose from, the raw
24 socket protocol and the broadcast manager (BCM). So to open a socket,
27 s = socket(PF_CAN, SOCK_RAW, CAN_RAW);
31 s = socket(PF_CAN, SOCK_DGRAM, CAN_BCM);
33 respectively. After the successful creation of the socket, you would
34 normally use the bind(2) system call to bind the socket to a CAN
35 interface (which is different from TCP/IP due to different addressing
36 - see overview.txt, chapter 3). After binding (CAN_RAW) or connecting
37 (CAN_BCM) the socket, you can read(2) and write(2) from/to the socket
38 or use send(2), sendto(2), sendmsg(2) and the recv* counterpart operations
39 on the socket as usual. There are also CAN specific socket options
42 The basic CAN frame structure and the sockaddr structure are defined
43 in include/linux/can.h:
46 canid_t can_id; /* 32 bit CAN_ID + EFF/RTR/ERR flags */
47 __u8 can_dlc; /* data length code: 0 .. 8 */
48 __u8 data[8] __attribute__((aligned(8)));
51 The alignment of the (linear) payload data[] to a 64bit boundary
52 allows the user to define own structs and unions to easily access the
53 CAN payload. There is no given byteorder on the CAN bus by
54 default. A read(2) system call on a CAN_RAW socket transfers a
55 struct can_frame to the user space.
57 The sockaddr_can structure has an interface index like the
58 PF_PACKET socket, that also binds to a specific interface:
61 sa_family_t can_family;
64 /* transport protocol class address info (e.g. ISOTP) */
65 struct { canid_t rx_id, tx_id; } tp;
67 /* reserved for future CAN protocols address information */
71 To determine the interface index an appropriate ioctl() has to
72 be used (example for CAN_RAW sockets without error checking):
75 struct sockaddr_can addr;
78 s = socket(PF_CAN, SOCK_RAW, CAN_RAW);
80 strcpy(ifr.ifr_name, "can0" );
81 ioctl(s, SIOCGIFINDEX, &ifr);
83 addr.can_family = AF_CAN;
84 addr.can_ifindex = ifr.ifr_ifindex;
86 bind(s, (struct sockaddr *)&addr, sizeof(addr));
90 To bind a socket to all(!) CAN interfaces the interface index must
91 be 0 (zero). In this case the socket receives CAN frames from every
92 enabled CAN interface. To determine the originating CAN interface
93 the system call recvfrom(2) may be used instead of read(2). To send
94 on a socket that is bound to 'any' interface sendto(2) is needed to
95 specify the outgoing interface.
97 Reading CAN frames from a bound CAN_RAW socket (see above) consists
98 of reading a struct can_frame:
100 struct can_frame frame;
102 nbytes = read(s, &frame, sizeof(struct can_frame));
105 perror("can raw socket read");
109 /* paranoid check ... */
110 if (nbytes < sizeof(struct can_frame)) {
111 fprintf(stderr, "read: incomplete CAN frame\n");
115 /* do something with the received CAN frame */
117 Writing CAN frames can be done similarly, with the write(2) system call:
119 nbytes = write(s, &frame, sizeof(struct can_frame));
121 When the CAN interface is bound to 'any' existing CAN interface
122 (addr.can_ifindex = 0) it is recommended to use recvfrom(2) if the
123 information about the originating CAN interface is needed:
125 struct sockaddr_can addr;
127 socklen_t len = sizeof(addr);
128 struct can_frame frame;
130 nbytes = recvfrom(s, &frame, sizeof(struct can_frame),
131 0, (struct sockaddr*)&addr, &len);
133 /* get interface name of the received CAN frame */
134 ifr.ifr_ifindex = addr.can_ifindex;
135 ioctl(s, SIOCGIFNAME, &ifr);
136 printf("Received a CAN frame from interface %s", ifr.ifr_name);
138 To write CAN frames on sockets bound to 'any' CAN interface the
139 outgoing interface has to be defined certainly.
141 strcpy(ifr.ifr_name, "can0");
142 ioctl(s, SIOCGIFINDEX, &ifr);
143 addr.can_ifindex = ifr.ifr_ifindex;
144 addr.can_family = AF_CAN;
146 nbytes = sendto(s, &frame, sizeof(struct can_frame),
147 0, (struct sockaddr*)&addr, sizeof(addr));
151 For applications in the CAN environment it is often of interest an
152 accurate timestamp of the instant a message from CAN bus has been received.
153 Such a timestamp can be read with ioctl(2) after reading a message from
157 ioctl(s, SIOCGSTAMP, &tv);
159 The timestamp on Linux has a resolution of one microsecond and it is set
160 automatically at the reception of a CAN frame.
162 Alternatively the timestamp can be obtained as a control message (cmsg) using
163 the recvmsg() system call. After enabling the timestamps in the cmsg's by
165 const int timestamp = 1;
166 setsockopt(s, SOL_SOCKET, SO_TIMESTAMP, ×tamp, sizeof(timestamp));
168 the data structures filled by recvmsg() need to be parsed for
169 cmsg->cmsg_type == SO_TIMESTAMP to get the timestamp. See cmsg() manpage.