summaryrefslogtreecommitdiffstats
path: root/src/serialbus/doc/src/socketcan.qdoc
blob: d300e46e7ac9eb95f826ac1e39eb606c2d1637d8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/****************************************************************************
**
** Copyright (C) 2015 The Qt Company Ltd.
** Contact: http://www.qt.io/licensing/
**
** This file is part of the documentation of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:FDL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see http://www.qt.io/terms-conditions. For further
** information use the contact form at http://www.qt.io/contact-us.
**
** GNU Free Documentation License Usage
** Alternatively, this file may be used under the terms of the GNU Free
** Documentation License version 1.3 as published by the Free Software
** Foundation and appearing in the file included in the packaging of
** this file. Please review the following information to ensure
** the GNU Free Documentation License version 1.3 requirements
** will be met: http://www.gnu.org/copyleft/fdl.html.
** $QT_END_LICENSE$
**
****************************************************************************/
/*!
    \page qtserialbus-socketcan-overview.html
    \title Using SocketCAN Plugin

    \brief Overview of how to use the SocketCAN plugin.

    The SocketCAN plugin encapsulates the Linux sockets API for accessing the CAN devices.
    This API is a set of open source CAN drivers and a networking stack contributed by
    Volkswagen Research to the Linux kernel.

    This plugin requires a Linux Kernel with SocketCAN support and SocketCAN device
    drivers for the used CAN hardware.

    \section1 SocketCAN usage

    To list all (including unconfigured) network interfaces, the command
    \c{ifconfig -a} can be used.

    To use SocketCAN, the corresponding Linux Kernel modules must be loaded
    and the network interface must be configured.

    \section2 Setting up real CAN hardware

    This section assumes, that the device driver is already loaded
    (most likely automatically when connecting the CAN hardware).

    \section3 Default settings

    To set the device can0 to a bitrate of 250 kBit/s:
    \code
        sudo ip link set up can0 type can bitrate 250000
    \endcode

    To automatically recover from "bus off" errors after 100 milliseconds,
    the following command can be used:
    \code
        sudo ip link set up can0 type can bitrate 250000 restart-ms 100
    \endcode

    \section3 CAN FD settings

    To set the device can0 to an arbitration bitrate of 500 kBit/s and a data
    bitrate of 4 MBit/s (for frames with bitrate switch flag):
    \code
        sudo ip link set can0 up type can bitrate 500000 dbitrate 4000000 fd on
    \endcode

    \section2 Setting up a virtual CAN bus

    \note For CAN FD usage, the MTU (Maximum Transmission Unit) has to be set
    to 72 byte.

    \code
        sudo modprobe vcan
        sudo ip link add dev vcan0 type vcan
        sudo ip link set up vcan0 mtu 72
    \endcode

    The command line test programs used in the following are from
    the \l{https://github.com/linux-can/can-utils}{can-utils} package:

    \code
        # Display received CAN messages with absolute timestamps and flags
        candump -ta -x vcan0

        # Send a CAN FD message with flags BRS and EFI set
        cansend vcan0 123##3112233445566778899aabbccddeeff

        # Generate random CAN messages
        cangen vcan0
    \endcode

    \section1 Creating CAN Bus Devices

    At first it is necessary to check that QCanBus provides the desired plugin:

    \code
        if (QCanBus::instance()->plugins().contains(QStringLiteral("socketcan"))) {
            // plugin available
        }
    \endcode

    Where \e socketcan is the plugin name.

    Next, a connection to a specific interface can be established:

    \code
        QCanBusDevice *device = QCanBus::instance()->createDevice(
            QStringLiteral("socketcan"), QStringLiteral("can0"));
        device->connectDevice();
    \endcode

    Where \e can0 is the active CAN interface name. CAN interfaces act like regular
    network interfaces on Linux systems and can be discovered using \c ifconfig.

    The device is now open for writing and reading CAN frames:

    \code
        QCanBusFrame frame;
        frame.setFrameId(8);
        QByteArray payload("A36E");
        frame.setPayload(payload);
        device->writeFrame(frame);
    \endcode

    The reading can be done using the \l {QCanBusDevice::}{readFrame()} method. The
    \l {QCanBusDevice::}{framesReceived()} signal is emitted when at least one new frame
    is available for reading:

    \code
        QCanBusFrame frame = device->readFrame();
    \endcode

    SocketCAN supports the following configurations that can be controlled through
    \l {QCanBusDevice::}{setConfigurationParameter()}:

    \table
        \header
            \li Configuration parameter key
            \li Description
        \row
            \li QCanBusDevice::LoopbackKey
            \li To meet the multiple-user needs, the local loopback is enabled by default.
                This means, whenever a CAN frame is transmitted on the CAN bus, a local
                echo of this frame is sent to all applications connected to this CAN device.
                If this option is enabled, the therefore received frames are marked with
                QCanBusFrame::hasLocalEcho()
        \row
            \li QCanBusDevice::ReceiveOwnKey
            \li The reception of the CAN frames on the same socket that was sending the CAN frame
                is disabled by default. When enabling this option, all CAN frames sent to the CAN
                bus immediately appear in the receive buffer. This can be used to check if sending
                was successful. If this option is enabled, the therefore received frames are marked
                with QCanBusFrame::hasLocalEcho()
        \row
            \li QCanBusDevice::ErrorFilterKey
            \li A CAN interface driver can generate so called \e {Error Message Frames} that can
                optionally be passed to the user application in the same way as other CAN frames.
                The possible errors are divided into different error classes that may be filtered
                using the appropriate error mask. The values for the error mask are defined in
                \c {linux/can/error.h}.
        \row
            \li QCanBusDevice::RawFilterKey
            \li This configuration can contain multiple filters of type \l QCanBusDevice::Filter.
                By default, the connection is configured to accept any CAN bus message.
        \row
            \li QCanBusDevice::BitRateKey
            \li This configuration is not supported by the socketcan plugin. However
                it is possible to set the rate when configuring the CAN network interface using
                the \c {ip link} command.
        \row
            \li QCanBusDevice::CanFdKey
            \li This configuration option determines whether CANFD frames may be sent or received.
                By default, this option is disabled. It controls the CAN_RAW_FD_FRAMES
                option of the CAN socket.
        \row
            \li QCanBusDevice::DataBitRateKey
            \li This configuration is not supported by the socketcan plugin. However
                it is possible to set the data rate when configuring the CAN network interface
                using the \c {ip link} command.
    \endtable

    For example:

    \snippet snippetmain.cpp SocketCan Filter Example

    Extended frame format and flexible data-rate are supported in SocketCAN.
 */