iso15765-2.rst 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. .. SPDX-License-Identifier: (GPL-2.0 OR BSD-3-Clause)
  2. ====================
  3. ISO 15765-2 (ISO-TP)
  4. ====================
  5. Overview
  6. ========
  7. ISO 15765-2, also known as ISO-TP, is a transport protocol specifically defined
  8. for diagnostic communication on CAN. It is widely used in the automotive
  9. industry, for example as the transport protocol for UDSonCAN (ISO 14229-3) or
  10. emission-related diagnostic services (ISO 15031-5).
  11. ISO-TP can be used both on CAN CC (aka Classical CAN) and CAN FD (CAN with
  12. Flexible Datarate) based networks. It is also designed to be compatible with a
  13. CAN network using SAE J1939 as data link layer (however, this is not a
  14. requirement).
  15. Specifications used
  16. -------------------
  17. * ISO 15765-2:2024 : Road vehicles - Diagnostic communication over Controller
  18. Area Network (DoCAN). Part 2: Transport protocol and network layer services.
  19. Addressing
  20. ----------
  21. In its simplest form, ISO-TP is based on two kinds of addressing modes for the
  22. nodes connected to the same network:
  23. * physical addressing is implemented by two node-specific addresses and is used
  24. in 1-to-1 communication.
  25. * functional addressing is implemented by one node-specific address and is used
  26. in 1-to-N communication.
  27. Three different addressing formats can be employed:
  28. * "normal" : each address is represented simply by a CAN ID.
  29. * "extended": each address is represented by a CAN ID plus the first byte of
  30. the CAN payload; both the CAN ID and the byte inside the payload shall be
  31. different between two addresses.
  32. * "mixed": each address is represented by a CAN ID plus the first byte of
  33. the CAN payload; the CAN ID is different between two addresses, but the
  34. additional byte is the same.
  35. Transport protocol and associated frame types
  36. ---------------------------------------------
  37. When transmitting data using the ISO-TP protocol, the payload can either fit
  38. inside one single CAN message or not, also considering the overhead the protocol
  39. is generating and the optional extended addressing. In the first case, the data
  40. is transmitted at once using a so-called Single Frame (SF). In the second case,
  41. ISO-TP defines a multi-frame protocol, in which the sender provides (through a
  42. First Frame - FF) the PDU length which is to be transmitted and also asks for a
  43. Flow Control (FC) frame, which provides the maximum supported size of a macro
  44. data block (``blocksize``) and the minimum time between the single CAN messages
  45. composing such block (``stmin``). Once this information has been received, the
  46. sender starts to send frames containing fragments of the data payload (called
  47. Consecutive Frames - CF), stopping after every ``blocksize``-sized block to wait
  48. confirmation from the receiver which should then send another Flow Control
  49. frame to inform the sender about its availability to receive more data.
  50. How to Use ISO-TP
  51. =================
  52. As with others CAN protocols, the ISO-TP stack support is built into the
  53. Linux network subsystem for the CAN bus, aka. Linux-CAN or SocketCAN, and
  54. thus follows the same socket API.
  55. Creation and basic usage of an ISO-TP socket
  56. --------------------------------------------
  57. To use the ISO-TP stack, ``#include <linux/can/isotp.h>`` shall be used. A
  58. socket can then be created using the ``PF_CAN`` protocol family, the
  59. ``SOCK_DGRAM`` type (as the underlying protocol is datagram-based by design)
  60. and the ``CAN_ISOTP`` protocol:
  61. .. code-block:: C
  62. s = socket(PF_CAN, SOCK_DGRAM, CAN_ISOTP);
  63. After the socket has been successfully created, ``bind(2)`` shall be called to
  64. bind the socket to the desired CAN interface; to do so:
  65. * a TX CAN ID shall be specified as part of the sockaddr supplied to the call
  66. itself.
  67. * a RX CAN ID shall also be specified, unless broadcast flags have been set
  68. through socket option (explained below).
  69. Once bound to an interface, the socket can be read from and written to using
  70. the usual ``read(2)`` and ``write(2)`` system calls, as well as ``send(2)``,
  71. ``sendmsg(2)``, ``recv(2)`` and ``recvmsg(2)``.
  72. Unlike the CAN_RAW socket API, only the ISO-TP data field (the actual payload)
  73. is sent and received by the userspace application using these calls. The address
  74. information and the protocol information are automatically filled by the ISO-TP
  75. stack using the configuration supplied during socket creation. In the same way,
  76. the stack will use the transport mechanism when required (i.e., when the size
  77. of the data payload exceeds the MTU of the underlying CAN bus).
  78. The sockaddr structure used for SocketCAN has extensions for use with ISO-TP,
  79. as specified below:
  80. .. code-block:: C
  81. struct sockaddr_can {
  82. sa_family_t can_family;
  83. int can_ifindex;
  84. union {
  85. struct { canid_t rx_id, tx_id; } tp;
  86. ...
  87. } can_addr;
  88. }
  89. * ``can_family`` and ``can_ifindex`` serve the same purpose as for other
  90. SocketCAN sockets.
  91. * ``can_addr.tp.rx_id`` specifies the receive (RX) CAN ID and will be used as
  92. a RX filter.
  93. * ``can_addr.tp.tx_id`` specifies the transmit (TX) CAN ID
  94. ISO-TP socket options
  95. ---------------------
  96. When creating an ISO-TP socket, reasonable defaults are set. Some options can
  97. be modified with ``setsockopt(2)`` and/or read back with ``getsockopt(2)``.
  98. General options
  99. ~~~~~~~~~~~~~~~
  100. General socket options can be passed using the ``CAN_ISOTP_OPTS`` optname:
  101. .. code-block:: C
  102. struct can_isotp_options opts;
  103. ret = setsockopt(s, SOL_CAN_ISOTP, CAN_ISOTP_OPTS, &opts, sizeof(opts))
  104. where the ``can_isotp_options`` structure has the following contents:
  105. .. code-block:: C
  106. struct can_isotp_options {
  107. u32 flags;
  108. u32 frame_txtime;
  109. u8 ext_address;
  110. u8 txpad_content;
  111. u8 rxpad_content;
  112. u8 rx_ext_address;
  113. };
  114. * ``flags``: modifiers to be applied to the default behaviour of the ISO-TP
  115. stack. Following flags are available:
  116. * ``CAN_ISOTP_LISTEN_MODE``: listen only (do not send FC frames); normally
  117. used as a testing feature.
  118. * ``CAN_ISOTP_EXTEND_ADDR``: use the byte specified in ``ext_address`` as an
  119. additional address component. This enables the "mixed" addressing format if
  120. used alone, or the "extended" addressing format if used in conjunction with
  121. ``CAN_ISOTP_RX_EXT_ADDR``.
  122. * ``CAN_ISOTP_TX_PADDING``: enable padding for transmitted frames, using
  123. ``txpad_content`` as value for the padding bytes.
  124. * ``CAN_ISOTP_RX_PADDING``: enable padding for the received frames, using
  125. ``rxpad_content`` as value for the padding bytes.
  126. * ``CAN_ISOTP_CHK_PAD_LEN``: check for correct padding length on the received
  127. frames.
  128. * ``CAN_ISOTP_CHK_PAD_DATA``: check padding bytes on the received frames
  129. against ``rxpad_content``; if ``CAN_ISOTP_RX_PADDING`` is not specified,
  130. this flag is ignored.
  131. * ``CAN_ISOTP_HALF_DUPLEX``: force ISO-TP socket in half duplex mode
  132. (that is, transport mechanism can only be incoming or outgoing at the same
  133. time, not both).
  134. * ``CAN_ISOTP_FORCE_TXSTMIN``: ignore stmin from received FC; normally
  135. used as a testing feature.
  136. * ``CAN_ISOTP_FORCE_RXSTMIN``: ignore CFs depending on rx stmin; normally
  137. used as a testing feature.
  138. * ``CAN_ISOTP_RX_EXT_ADDR``: use ``rx_ext_address`` instead of ``ext_address``
  139. as extended addressing byte on the reception path. If used in conjunction
  140. with ``CAN_ISOTP_EXTEND_ADDR``, this flag effectively enables the "extended"
  141. addressing format.
  142. * ``CAN_ISOTP_WAIT_TX_DONE``: wait until the frame is sent before returning
  143. from ``write(2)`` and ``send(2)`` calls (i.e., blocking write operations).
  144. * ``CAN_ISOTP_SF_BROADCAST``: use 1-to-N functional addressing (cannot be
  145. specified alongside ``CAN_ISOTP_CF_BROADCAST``).
  146. * ``CAN_ISOTP_CF_BROADCAST``: use 1-to-N transmission without flow control
  147. (cannot be specified alongside ``CAN_ISOTP_SF_BROADCAST``).
  148. NOTE: this is not covered by the ISO 15765-2 standard.
  149. * ``CAN_ISOTP_DYN_FC_PARMS``: enable dynamic update of flow control
  150. parameters.
  151. * ``frame_txtime``: frame transmission time (defined as N_As/N_Ar inside the
  152. ISO standard); if ``0``, the default (or the last set value) is used.
  153. To set the transmission time to ``0``, the ``CAN_ISOTP_FRAME_TXTIME_ZERO``
  154. macro (equal to 0xFFFFFFFF) shall be used.
  155. * ``ext_address``: extended addressing byte, used if the
  156. ``CAN_ISOTP_EXTEND_ADDR`` flag is specified.
  157. * ``txpad_content``: byte used as padding value for transmitted frames.
  158. * ``rxpad_content``: byte used as padding value for received frames.
  159. * ``rx_ext_address``: extended addressing byte for the reception path, used if
  160. the ``CAN_ISOTP_RX_EXT_ADDR`` flag is specified.
  161. Flow Control options
  162. ~~~~~~~~~~~~~~~~~~~~
  163. Flow Control (FC) options can be passed using the ``CAN_ISOTP_RECV_FC`` optname
  164. to provide the communication parameters for receiving ISO-TP PDUs.
  165. .. code-block:: C
  166. struct can_isotp_fc_options fc_opts;
  167. ret = setsockopt(s, SOL_CAN_ISOTP, CAN_ISOTP_RECV_FC, &fc_opts, sizeof(fc_opts));
  168. where the ``can_isotp_fc_options`` structure has the following contents:
  169. .. code-block:: C
  170. struct can_isotp_options {
  171. u8 bs;
  172. u8 stmin;
  173. u8 wftmax;
  174. };
  175. * ``bs``: blocksize provided in flow control frames.
  176. * ``stmin``: minimum separation time provided in flow control frames; can
  177. have the following values (others are reserved):
  178. * 0x00 - 0x7F : 0 - 127 ms
  179. * 0xF1 - 0xF9 : 100 us - 900 us
  180. * ``wftmax``: maximum number of wait frames provided in flow control frames.
  181. Link Layer options
  182. ~~~~~~~~~~~~~~~~~~
  183. Link Layer (LL) options can be passed using the ``CAN_ISOTP_LL_OPTS`` optname:
  184. .. code-block:: C
  185. struct can_isotp_ll_options ll_opts;
  186. ret = setsockopt(s, SOL_CAN_ISOTP, CAN_ISOTP_LL_OPTS, &ll_opts, sizeof(ll_opts));
  187. where the ``can_isotp_ll_options`` structure has the following contents:
  188. .. code-block:: C
  189. struct can_isotp_ll_options {
  190. u8 mtu;
  191. u8 tx_dl;
  192. u8 tx_flags;
  193. };
  194. * ``mtu``: generated and accepted CAN frame type, can be equal to ``CAN_MTU``
  195. for classical CAN frames or ``CANFD_MTU`` for CAN FD frames.
  196. * ``tx_dl``: maximum payload length for transmitted frames, can have one value
  197. among: 8, 12, 16, 20, 24, 32, 48, 64. Values above 8 only apply to CAN FD
  198. traffic (i.e.: ``mtu = CANFD_MTU``).
  199. * ``tx_flags``: flags set into ``struct canfd_frame.flags`` at frame creation.
  200. Only applies to CAN FD traffic (i.e.: ``mtu = CANFD_MTU``).
  201. Transmission stmin
  202. ~~~~~~~~~~~~~~~~~~
  203. The transmission minimum separation time (stmin) can be forced using the
  204. ``CAN_ISOTP_TX_STMIN`` optname and providing an stmin value in microseconds as
  205. a 32bit unsigned integer; this will overwrite the value sent by the receiver in
  206. flow control frames:
  207. .. code-block:: C
  208. uint32_t stmin;
  209. ret = setsockopt(s, SOL_CAN_ISOTP, CAN_ISOTP_TX_STMIN, &stmin, sizeof(stmin));
  210. Reception stmin
  211. ~~~~~~~~~~~~~~~
  212. The reception minimum separation time (stmin) can be forced using the
  213. ``CAN_ISOTP_RX_STMIN`` optname and providing an stmin value in microseconds as
  214. a 32bit unsigned integer; received Consecutive Frames (CF) which timestamps
  215. differ less than this value will be ignored:
  216. .. code-block:: C
  217. uint32_t stmin;
  218. ret = setsockopt(s, SOL_CAN_ISOTP, CAN_ISOTP_RX_STMIN, &stmin, sizeof(stmin));
  219. Multi-frame transport support
  220. -----------------------------
  221. The ISO-TP stack contained inside the Linux kernel supports the multi-frame
  222. transport mechanism defined by the standard, with the following constraints:
  223. * the maximum size of a PDU is defined by a module parameter, with an hard
  224. limit imposed at build time.
  225. * when a transmission is in progress, subsequent calls to ``write(2)`` will
  226. block, while calls to ``send(2)`` will either block or fail depending on the
  227. presence of the ``MSG_DONTWAIT`` flag.
  228. * no support is present for sending "wait frames": whether a PDU can be fully
  229. received or not is decided when the First Frame is received.
  230. Errors
  231. ------
  232. Following errors are reported to userspace:
  233. RX path errors
  234. ~~~~~~~~~~~~~~
  235. ============ ===============================================================
  236. -ETIMEDOUT timeout of data reception
  237. -EILSEQ sequence number mismatch during a multi-frame reception
  238. -EBADMSG data reception with wrong padding
  239. ============ ===============================================================
  240. TX path errors
  241. ~~~~~~~~~~~~~~
  242. ========== =================================================================
  243. -ECOMM flow control reception timeout
  244. -EMSGSIZE flow control reception overflow
  245. -EBADMSG flow control reception with wrong layout/padding
  246. ========== =================================================================
  247. Examples
  248. ========
  249. Basic node example
  250. ------------------
  251. Following example implements a node using "normal" physical addressing, with
  252. RX ID equal to 0x18DAF142 and a TX ID equal to 0x18DA42F1. All options are left
  253. to their default.
  254. .. code-block:: C
  255. int s;
  256. struct sockaddr_can addr;
  257. int ret;
  258. s = socket(PF_CAN, SOCK_DGRAM, CAN_ISOTP);
  259. if (s < 0)
  260. exit(1);
  261. addr.can_family = AF_CAN;
  262. addr.can_ifindex = if_nametoindex("can0");
  263. addr.can_addr.tp.tx_id = 0x18DA42F1 | CAN_EFF_FLAG;
  264. addr.can_addr.tp.rx_id = 0x18DAF142 | CAN_EFF_FLAG;
  265. ret = bind(s, (struct sockaddr *)&addr, sizeof(addr));
  266. if (ret < 0)
  267. exit(1);
  268. /* Data can now be received using read(s, ...) and sent using write(s, ...) */
  269. Additional examples
  270. -------------------
  271. More complete (and complex) examples can be found inside the ``isotp*`` userland
  272. tools, distributed as part of the ``can-utils`` utilities at:
  273. https://github.com/linux-can/can-utils