Movatterモバイル変換


[0]ホーム

URL:


Jump to content
WikipediaThe Free Encyclopedia
Search

SocketCAN

From Wikipedia, the free encyclopedia
(Redirected fromSocketcan)
Open source controller area network drivers and networking stack for the Linux kernel

SocketCAN is a set ofopen sourceCAN drivers and a networking stack contributed byVolkswagen Research to theLinux kernel. SocketCAN was formerly known asLow Level CAN Framework (LLCF).

Typical CAN communication layers. With SocketCAN (left) or conventional (right).

Traditional CAN drivers for Linux are based on the model of character devices. Typically they only allow sending to and receiving from the CAN controller. Conventional implementations of this class ofdevice driver only allow a single process to access the device, which means that all other processes are blocked in the meantime. 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, a single application is able to access multiple CAN networks in parallel.

The SocketCAN concept extends theBerkeley socketsAPI in Linux by introducing a new protocol family, PF_CAN, that coexists with other protocol families, such as PF_INET for theInternet Protocol. The communication with the CAN bus is therefore done analogously 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, provides 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. SinceLinux kernel Version 5.10 the protocol family also includes anISO-TP implementation, CAN_ISOTP.[1]

Patches for CAN were added in the 2.6.25Linux kernel. Meanwhile some controller drivers were added and work is going on to add drivers for a variety of controllers.[citation needed]

Usage

[edit]

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 aUDP socket viaread,write, etc...

Python added support for SocketCAN in version 3.3.[2] An open source librarypython-can provides SocketCAN support for Python 2 and Python 3[3][circular reference].

Installing a CAN device requires loading the can_dev module and configuring the IP link to specify the CAN bus bitrate, for example:

$modprobecan_dev$modprobecan$modprobecan_raw$sudoiplinksetcan0typecanbitrate500000$sudoiplinksetupcan0

There is also a virtual CAN driver for testing purposes which can be loaded and created in Linux with the commands below.

$modprobecan$modprobecan_raw$modprobevcan$sudoiplinkadddevvcan0typevcan$sudoiplinksetupvcan0$iplinkshowvcan03: vcan0: <NOARP,UP,LOWER_UP> mtu 16 qdisc noqueue state UNKNOWN    link/can

The following code snippet is a working example of the SocketCAN API, that sends a packet using the raw interface. It is based on the notes documented in theLinux Kernel.[4]

#include<stdio.h>#include<stdlib.h>#include<string.h>#include<net/if.h>#include<sys/ioctl.h>#include<sys/socket.h>#include<sys/types.h>#include<unistd.h>#include<linux/can.h>#include<linux/can/raw.h>intmain(void){ints;intnbytes;structsockaddr_canaddr;structcan_frameframe;structifreqifr;constchar*ifname="vcan0";if((s=socket(PF_CAN,SOCK_RAW,CAN_RAW))==-1){perror("Error while opening socket");return-1;}strcpy(ifr.ifr_name,ifname);ioctl(s,SIOCGIFINDEX,&ifr);addr.can_family=AF_CAN;addr.can_ifindex=ifr.ifr_ifindex;printf("%s at index %d\n",ifname,ifr.ifr_ifindex);if(bind(s,(structsockaddr*)&addr,sizeof(addr))==-1){perror("Error in socket bind");return-2;}frame.can_id=0x123;frame.can_dlc=2;frame.data[0]=0x11;frame.data[1]=0x22;nbytes=write(s,&frame,sizeof(structcan_frame));printf("Wrote %d bytes\n",nbytes);return0;}

The packet can be analyzed on the vcan0 interface using the candump utility which is part of the SocketCAN can-utils[5] package.

user@server:~/can-utils $./candumpvcan0  vcan0  123  [2] 11 22

References

[edit]
  1. ^"hartkopp/can-isotp". May 14, 2021 – via GitHub.
  2. ^"Issue 10141: SocketCan support - Python tracker".bugs.python.org.
  3. ^SocketCAN
  4. ^Viewable online fromLinux Kernel Documentation or inlinux/Documentation/networking/can.txt in most recent source trees
  5. ^can-utilshttps://github.com/linux-can/can-utils/

External links

[edit]
Retrieved from "https://en.wikipedia.org/w/index.php?title=SocketCAN&oldid=1320540622"
Categories:
Hidden categories:

[8]ページ先頭

©2009-2026 Movatter.jp