Socketcan
Encyclopedia
SocketCAN is a set of open source
Open source
The term open source describes practices in production and development that promote access to the end product's source materials. Some consider open source a philosophy, others consider it a pragmatic methodology...

 CAN
Controller Area Network
Controller–area network is a vehicle bus standard designed to allow microcontrollers and devices to communicate with each other within a vehicle without a host computer....

 drivers and a networking stack contributed by Volkswagen Research
Volkswagen
Volkswagen is a German automobile manufacturer and is the original and biggest-selling marque of the Volkswagen Group, which now also owns the Audi, Bentley, Bugatti, Lamborghini, SEAT, and Škoda marques and the truck manufacturer Scania.Volkswagen means "people's car" in German, where it is...

 to the Linux kernel
Linux kernel
The Linux kernel is an operating system kernel used by the Linux family of Unix-like operating systems. It is one of the most prominent examples of free and open source software....

. Formerly known as Low Level CAN Framework (LLCF).

Established CAN drivers are based on the model of character devices. Typically they only allow sending to and receiving from the CAN controller. Conventional implementations for this device class only allow a single process on the device which means that all other processes are blocked in the meantime as known from accessing a device via the serial interface. In addition, these drivers typically all differ slightly in the interface presented to the application, stifling portability. The SocketCAN concept on the other hand uses the model of network devices, which allows multiple applications to access one CAN device simultaneously. Also, single application are able to access multiple CAN networks in parallel.

The SocketCAN concept extends the Berkeley sockets
Berkeley sockets
The Berkeley sockets application programming interface comprises a library for developing applications in the C programming language that perform inter-process communication, most commonly for communications across a computer network....

 API in Linux by introducing a new protocol family PF_CAN that coexists with other protocol families like PF_INET for the Internet Protocol
Internet Protocol
The Internet Protocol is the principal communications protocol used for relaying datagrams across an internetwork using the Internet Protocol Suite...

. The communication with the CAN bus is done analogue to the use of the Internet Protocol via Sockets. Fundamental components of SocketCAN are the network device drivers for different CAN controllers and the implementation of the CAN protocol family. The protocol family PF_CAN provide the structures to enable different protocols on the bus: Raw sockets for direct CAN communication and transport protocols for point-to-point connections. Moreover the broadcast manager which is part of the CAN protocol family provides functions e.g. for sending CAN messages periodically or realize complex message filters.

Patches about CAN were added in the 2.6.25 Linux kernel
Linux kernel
The Linux kernel is an operating system kernel used by the Linux family of Unix-like operating systems. It is one of the most prominent examples of free and open source software....

. Meanwhile some controller drivers were added and work is going on to add drivers for a variety of controllers.

Usage

The application first sets up its access to the CAN interface by initialising a socket (much like in TCP/IP communications), then binding that socket to an interface (or all interfaces, if the application so desires). Once bound, the socket can then be used like a UDP
User Datagram Protocol
The User Datagram Protocol is one of the core members of the Internet Protocol Suite, the set of network protocols used for the Internet. With UDP, computer applications can send messages, in this case referred to as datagrams, to other hosts on an Internet Protocol network without requiring...

 socket via read, write, etc...

The following is a simple (incomplete) example, that sends a packet, then reads back a packet using the raw interface. It is based on the notes documented in the Linux Kernel
Linux kernel
The Linux kernel is an operating system kernel used by the Linux family of Unix-like operating systems. It is one of the most prominent examples of free and open source software....

.

  1. include
  2. include
  3. include
  4. include

  1. include
  2. include
  3. include


/* At time of writing, these constants are not defined in the headers */
  1. ifndef PF_CAN
  2. define PF_CAN 29
  3. endif

  1. ifndef AF_CAN
  2. define AF_CAN PF_CAN
  3. endif


/* ... */

/* Somewhere in your app */

/* Create the socket */
int skt = socket( PF_CAN, SOCK_RAW, CAN_RAW );

/* Locate the interface you wish to use */
struct ifreq ifr;
strcpy(ifr.ifr_name, "can0");
ioctl(skt, SIOCGIFINDEX, &ifr); /* ifr.ifr_ifindex gets filled
* with that device's index */

/* Select that CAN interface, and bind the socket to it. */
struct sockaddr_can addr;
addr.can_family = AF_CAN;
addr.can_ifindex = ifr.ifr_ifindex;
bind( skt, (struct sockaddr*)&addr, sizeof(addr) );

/* Send a message to the CAN bus */
struct can_frame frame;
frame.can_id = 0x123;
strcpy( frame.data, "foo" );
frame.can_dlc = strlen( frame.data );
int bytes_sent = write( skt, &frame, sizeof(frame) );

/* Read a message back from the CAN bus */
int bytes_read = read( skt, &frame, sizeof(frame) );

External links

The source of this article is wikipedia, the free encyclopedia.  The text of this article is licensed under the GFDL.
 
x
OK