j1939.rst 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135
  1. .. SPDX-License-Identifier: (GPL-2.0 OR MIT)
  2. ===================
  3. J1939 Documentation
  4. ===================
  5. Overview / What Is J1939
  6. ========================
  7. SAE J1939 defines a higher layer protocol on CAN. It implements a more
  8. sophisticated addressing scheme and extends the maximum packet size above 8
  9. bytes. Several derived specifications exist, which differ from the original
  10. J1939 on the application level, like MilCAN A, NMEA2000, and especially
  11. ISO-11783 (ISOBUS). This last one specifies the so-called ETP (Extended
  12. Transport Protocol), which has been included in this implementation. This
  13. results in a maximum packet size of ((2 ^ 24) - 1) * 7 bytes == 111 MiB.
  14. Specifications used
  15. -------------------
  16. * SAE J1939-21 : data link layer
  17. * SAE J1939-81 : network management
  18. * ISO 11783-6 : Virtual Terminal (Extended Transport Protocol)
  19. .. _j1939-motivation:
  20. Motivation
  21. ==========
  22. Given the fact there's something like SocketCAN with an API similar to BSD
  23. sockets, we found some reasons to justify a kernel implementation for the
  24. addressing and transport methods used by J1939.
  25. * **Addressing:** when a process on an ECU communicates via J1939, it should
  26. not necessarily know its source address. Although, at least one process per
  27. ECU should know the source address. Other processes should be able to reuse
  28. that address. This way, address parameters for different processes
  29. cooperating for the same ECU, are not duplicated. This way of working is
  30. closely related to the UNIX concept, where programs do just one thing and do
  31. it well.
  32. * **Dynamic addressing:** Address Claiming in J1939 is time critical.
  33. Furthermore, data transport should be handled properly during the address
  34. negotiation. Putting this functionality in the kernel eliminates it as a
  35. requirement for _every_ user space process that communicates via J1939. This
  36. results in a consistent J1939 bus with proper addressing.
  37. * **Transport:** both TP & ETP reuse some PGNs to relay big packets over them.
  38. Different processes may thus use the same TP & ETP PGNs without actually
  39. knowing it. The individual TP & ETP sessions _must_ be serialized
  40. (synchronized) between different processes. The kernel solves this problem
  41. properly and eliminates the serialization (synchronization) as a requirement
  42. for _every_ user space process that communicates via J1939.
  43. J1939 defines some other features (relaying, gateway, fast packet transport,
  44. ...). In-kernel code for these would not contribute to protocol stability.
  45. Therefore, these parts are left to user space.
  46. The J1939 sockets operate on CAN network devices (see SocketCAN). Any J1939
  47. user space library operating on CAN raw sockets will still operate properly.
  48. Since such a library does not communicate with the in-kernel implementation, care
  49. must be taken that these two do not interfere. In practice, this means they
  50. cannot share ECU addresses. A single ECU (or virtual ECU) address is used by
  51. the library exclusively, or by the in-kernel system exclusively.
  52. J1939 concepts
  53. ==============
  54. Data Sent to the J1939 Stack
  55. ----------------------------
  56. The data buffers sent to the J1939 stack from user space are not CAN frames
  57. themselves. Instead, they are payloads that the J1939 stack converts into
  58. proper CAN frames based on the size of the buffer and the type of transfer. The
  59. size of the buffer influences how the stack processes the data and determines
  60. the internal code path used for the transfer.
  61. **Handling of Different Buffer Sizes:**
  62. - **Buffers with a size of 8 bytes or less:**
  63. - These are handled as simple sessions internally within the stack.
  64. - The stack converts the buffer directly into a single CAN frame without
  65. fragmentation.
  66. - This type of transfer does not require an actual client (receiver) on the
  67. receiving side.
  68. - **Buffers up to 1785 bytes:**
  69. - These are automatically handled as J1939 Transport Protocol (TP) transfers.
  70. - Internally, the stack splits the buffer into multiple 8-byte CAN frames.
  71. - TP transfers can be unicast or broadcast.
  72. - **Broadcast TP:** Does not require a receiver on the other side and can be
  73. used in broadcast scenarios.
  74. - **Unicast TP:** Requires an active receiver (client) on the other side to
  75. acknowledge the transfer.
  76. - **Buffers from 1786 bytes up to 111 MiB:**
  77. - These are handled as ISO 11783 Extended Transport Protocol (ETP) transfers.
  78. - ETP transfers are used for larger payloads and are split into multiple CAN
  79. frames internally.
  80. - **ETP transfers (unicast):** Require a receiver on the other side to
  81. process the incoming data and acknowledge each step of the transfer.
  82. - ETP transfers cannot be broadcast like TP transfers, and always require a
  83. receiver for operation.
  84. **Non-Blocking Operation with `MSG_DONTWAIT`:**
  85. The J1939 stack supports non-blocking operation when used in combination with
  86. the `MSG_DONTWAIT` flag. In this mode, the stack attempts to take as much data
  87. as the available memory for the socket allows. It returns the amount of data
  88. that was successfully taken, and it is the responsibility of user space to
  89. monitor this value and handle partial transfers.
  90. - If the stack cannot take the entire buffer, it returns the number of bytes
  91. successfully taken, and user space should handle the remainder.
  92. - **Error handling:** When using `MSG_DONTWAIT`, the user must rely on the
  93. error queue to detect transfer errors. See the **SO_J1939_ERRQUEUE** section
  94. for details on how to subscribe to error notifications. Without the error
  95. queue, there is no other way for user space to be notified of transfer errors
  96. during non-blocking operations.
  97. **Behavior and Requirements:**
  98. - **Simple transfers (<= 8 bytes):** Do not require a receiver on the other
  99. side, making them easy to send without needing address claiming or
  100. coordination with a destination.
  101. - **Unicast TP/ETP:** Requires a receiver on the other side to complete the
  102. transfer. The receiver must acknowledge the transfer for the session to
  103. proceed successfully.
  104. - **Broadcast TP:** Allows sending data without a receiver, but only works for
  105. TP transfers. ETP cannot be broadcast and always needs a receiving client.
  106. These different behaviors depend heavily on the size of the buffer provided to
  107. the stack, and the appropriate transport mechanism (TP or ETP) is selected
  108. based on the payload size. The stack automatically manages the fragmentation
  109. and reassembly of large payloads and ensures that the correct CAN frames are
  110. generated and transmitted for each session.
  111. PGN
  112. ---
  113. The J1939 protocol uses the 29-bit CAN identifier with the following structure:
  114. ============ ============== ====================
  115. 29 bit CAN-ID
  116. --------------------------------------------------
  117. Bit positions within the CAN-ID
  118. --------------------------------------------------
  119. 28 ... 26 25 ... 8 7 ... 0
  120. ============ ============== ====================
  121. Priority PGN SA (Source Address)
  122. ============ ============== ====================
  123. The PGN (Parameter Group Number) is a number to identify a packet. The PGN
  124. is composed as follows:
  125. ============ ============== ================= =================
  126. PGN
  127. ------------------------------------------------------------------
  128. Bit positions within the CAN-ID
  129. ------------------------------------------------------------------
  130. 25 24 23 ... 16 15 ... 8
  131. ============ ============== ================= =================
  132. R (Reserved) DP (Data Page) PF (PDU Format) PS (PDU Specific)
  133. ============ ============== ================= =================
  134. In J1939-21 distinction is made between PDU1 format (where PF < 240) and PDU2
  135. format (where PF >= 240). Furthermore, when using the PDU2 format, the PS-field
  136. contains a so-called Group Extension, which is part of the PGN. When using PDU2
  137. format, the Group Extension is set in the PS-field.
  138. ============== ========================
  139. PDU1 Format (specific) (peer to peer)
  140. ----------------------------------------
  141. Bit positions within the CAN-ID
  142. ----------------------------------------
  143. 23 ... 16 15 ... 8
  144. ============== ========================
  145. 00h ... EFh DA (Destination address)
  146. ============== ========================
  147. ============== ========================
  148. PDU2 Format (global) (broadcast)
  149. ----------------------------------------
  150. Bit positions within the CAN-ID
  151. ----------------------------------------
  152. 23 ... 16 15 ... 8
  153. ============== ========================
  154. F0h ... FFh GE (Group Extension)
  155. ============== ========================
  156. On the other hand, when using PDU1 format, the PS-field contains a so-called
  157. Destination Address, which is _not_ part of the PGN. When communicating a PGN
  158. from user space to kernel (or vice versa) and PDU1 format is used, the PS-field
  159. of the PGN shall be set to zero. The Destination Address shall be set
  160. elsewhere.
  161. Regarding PGN mapping to 29-bit CAN identifier, the Destination Address shall
  162. be get/set from/to the appropriate bits of the identifier by the kernel.
  163. Addressing
  164. ----------
  165. Both static and dynamic addressing methods can be used.
  166. For static addresses, no extra checks are made by the kernel and provided
  167. addresses are considered right. This responsibility is for the OEM or system
  168. integrator.
  169. For dynamic addressing, so-called Address Claiming, extra support is foreseen
  170. in the kernel. In J1939 any ECU is known by its 64-bit NAME. At the moment of
  171. a successful address claim, the kernel keeps track of both NAME and source
  172. address being claimed. This serves as a base for filter schemes. By default,
  173. packets with a destination that is not locally will be rejected.
  174. Mixed mode packets (from a static to a dynamic address or vice versa) are
  175. allowed. The BSD sockets define separate API calls for getting/setting the
  176. local & remote address and are applicable for J1939 sockets.
  177. Filtering
  178. ---------
  179. J1939 defines white list filters per socket that a user can set in order to
  180. receive a subset of the J1939 traffic. Filtering can be based on:
  181. * SA
  182. * SOURCE_NAME
  183. * PGN
  184. When multiple filters are in place for a single socket, and a packet comes in
  185. that matches several of those filters, the packet is only received once for
  186. that socket.
  187. How to Use J1939
  188. ================
  189. API Calls
  190. ---------
  191. On CAN, you first need to open a socket for communicating over a CAN network.
  192. To use J1939, ``#include <linux/can/j1939.h>``. From there, ``<linux/can.h>`` will be
  193. included too. To open a socket, use:
  194. .. code-block:: C
  195. s = socket(PF_CAN, SOCK_DGRAM, CAN_J1939);
  196. J1939 does use ``SOCK_DGRAM`` sockets. In the J1939 specification, connections are
  197. mentioned in the context of transport protocol sessions. These still deliver
  198. packets to the other end (using several CAN packets). ``SOCK_STREAM`` is not
  199. supported.
  200. After the successful creation of the socket, you would normally use the ``bind(2)``
  201. and/or ``connect(2)`` system call to bind the socket to a CAN interface. After
  202. binding and/or connecting the socket, you can ``read(2)`` and ``write(2)`` from/to the
  203. socket or use ``send(2)``, ``sendto(2)``, ``sendmsg(2)`` and the ``recv*()`` counterpart
  204. operations on the socket as usual. There are also J1939 specific socket options
  205. described below.
  206. In order to send data, a ``bind(2)`` must have been successful. ``bind(2)`` assigns a
  207. local address to a socket.
  208. Different from CAN is that the payload data is just the data that get sends,
  209. without its header info. The header info is derived from the sockaddr supplied
  210. to ``bind(2)``, ``connect(2)``, ``sendto(2)`` and ``recvfrom(2)``. A ``write(2)`` with size 4 will
  211. result in a packet with 4 bytes.
  212. The sockaddr structure has extensions for use with J1939 as specified below:
  213. .. code-block:: C
  214. struct sockaddr_can {
  215. sa_family_t can_family;
  216. int can_ifindex;
  217. union {
  218. struct {
  219. __u64 name;
  220. /* pgn:
  221. * 8 bit: PS in PDU2 case, else 0
  222. * 8 bit: PF
  223. * 1 bit: DP
  224. * 1 bit: reserved
  225. */
  226. __u32 pgn;
  227. __u8 addr;
  228. } j1939;
  229. } can_addr;
  230. }
  231. ``can_family`` & ``can_ifindex`` serve the same purpose as for other SocketCAN sockets.
  232. ``can_addr.j1939.pgn`` specifies the PGN (max 0x3ffff). Individual bits are
  233. specified above.
  234. ``can_addr.j1939.name`` contains the 64-bit J1939 NAME.
  235. ``can_addr.j1939.addr`` contains the address.
  236. The ``bind(2)`` system call assigns the local address, i.e. the source address when
  237. sending packages. If a PGN during ``bind(2)`` is set, it's used as a RX filter.
  238. I.e. only packets with a matching PGN are received. If an ADDR or NAME is set
  239. it is used as a receive filter, too. It will match the destination NAME or ADDR
  240. of the incoming packet. The NAME filter will work only if appropriate Address
  241. Claiming for this name was done on the CAN bus and registered/cached by the
  242. kernel.
  243. On the other hand ``connect(2)`` assigns the remote address, i.e. the destination
  244. address. The PGN from ``connect(2)`` is used as the default PGN when sending
  245. packets. If ADDR or NAME is set it will be used as the default destination ADDR
  246. or NAME. Further a set ADDR or NAME during ``connect(2)`` is used as a receive
  247. filter. It will match the source NAME or ADDR of the incoming packet.
  248. Both ``write(2)`` and ``send(2)`` will send a packet with local address from ``bind(2)`` and the
  249. remote address from ``connect(2)``. Use ``sendto(2)`` to overwrite the destination
  250. address.
  251. If ``can_addr.j1939.name`` is set (!= 0) the NAME is looked up by the kernel and
  252. the corresponding ADDR is used. If ``can_addr.j1939.name`` is not set (== 0),
  253. ``can_addr.j1939.addr`` is used.
  254. When creating a socket, reasonable defaults are set. Some options can be
  255. modified with ``setsockopt(2)`` & ``getsockopt(2)``.
  256. RX path related options:
  257. - ``SO_J1939_FILTER`` - configure array of filters
  258. - ``SO_J1939_PROMISC`` - disable filters set by ``bind(2)`` and ``connect(2)``
  259. By default no broadcast packets can be send or received. To enable sending or
  260. receiving broadcast packets use the socket option ``SO_BROADCAST``:
  261. .. code-block:: C
  262. int value = 1;
  263. setsockopt(sock, SOL_SOCKET, SO_BROADCAST, &value, sizeof(value));
  264. The following diagram illustrates the RX path:
  265. .. code::
  266. +--------------------+
  267. | incoming packet |
  268. +--------------------+
  269. |
  270. V
  271. +--------------------+
  272. | SO_J1939_PROMISC? |
  273. +--------------------+
  274. | |
  275. no | | yes
  276. | |
  277. .---------' `---------.
  278. | |
  279. +---------------------------+ |
  280. | bind() + connect() + | |
  281. | SOCK_BROADCAST filter | |
  282. +---------------------------+ |
  283. | |
  284. |<---------------------'
  285. V
  286. +---------------------------+
  287. | SO_J1939_FILTER |
  288. +---------------------------+
  289. |
  290. V
  291. +---------------------------+
  292. | socket recv() |
  293. +---------------------------+
  294. TX path related options:
  295. ``SO_J1939_SEND_PRIO`` - change default send priority for the socket
  296. Message Flags during send() and Related System Calls
  297. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  298. ``send(2)``, ``sendto(2)`` and ``sendmsg(2)`` take a 'flags' argument. Currently
  299. supported flags are:
  300. * ``MSG_DONTWAIT``, i.e. non-blocking operation.
  301. recvmsg(2)
  302. ^^^^^^^^^^
  303. In most cases ``recvmsg(2)`` is needed if you want to extract more information than
  304. ``recvfrom(2)`` can provide. For example package priority and timestamp. The
  305. Destination Address, name and packet priority (if applicable) are attached to
  306. the msghdr in the ``recvmsg(2)`` call. They can be extracted using ``cmsg(3)`` macros,
  307. with ``cmsg_level == SOL_J1939 && cmsg_type == SCM_J1939_DEST_ADDR``,
  308. ``SCM_J1939_DEST_NAME`` or ``SCM_J1939_PRIO``. The returned data is a ``uint8_t`` for
  309. ``priority`` and ``dst_addr``, and ``uint64_t`` for ``dst_name``.
  310. .. code-block:: C
  311. uint8_t priority, dst_addr;
  312. uint64_t dst_name;
  313. for (cmsg = CMSG_FIRSTHDR(&msg); cmsg; cmsg = CMSG_NXTHDR(&msg, cmsg)) {
  314. switch (cmsg->cmsg_level) {
  315. case SOL_CAN_J1939:
  316. if (cmsg->cmsg_type == SCM_J1939_DEST_ADDR)
  317. dst_addr = *CMSG_DATA(cmsg);
  318. else if (cmsg->cmsg_type == SCM_J1939_DEST_NAME)
  319. memcpy(&dst_name, CMSG_DATA(cmsg), cmsg->cmsg_len - CMSG_LEN(0));
  320. else if (cmsg->cmsg_type == SCM_J1939_PRIO)
  321. priority = *CMSG_DATA(cmsg);
  322. break;
  323. }
  324. }
  325. setsockopt(2)
  326. ^^^^^^^^^^^^^
  327. The ``setsockopt(2)`` function is used to configure various socket-level
  328. options for J1939 communication. The following options are supported:
  329. ``SO_J1939_FILTER``
  330. ~~~~~~~~~~~~~~~~~~~
  331. The ``SO_J1939_FILTER`` option is essential when the default behavior of
  332. ``bind(2)`` and ``connect(2)`` is insufficient for specific use cases. By
  333. default, ``bind(2)`` and ``connect(2)`` allow a socket to be associated with a
  334. single unicast or broadcast address. However, there are scenarios where finer
  335. control over the incoming messages is required, such as filtering by Parameter
  336. Group Number (PGN) rather than by addresses.
  337. For example, in a system where multiple types of J1939 messages are being
  338. transmitted, a process might only be interested in a subset of those messages,
  339. such as specific PGNs, and not want to receive all messages destined for its
  340. address or broadcast to the bus.
  341. By applying the ``SO_J1939_FILTER`` option, you can filter messages based on:
  342. - **Source Address (SA)**: Filter messages coming from specific source
  343. addresses.
  344. - **Source Name**: Filter messages coming from ECUs with specific NAME
  345. identifiers.
  346. - **Parameter Group Number (PGN)**: Focus on receiving messages with specific
  347. PGNs, filtering out irrelevant ones.
  348. This filtering mechanism is particularly useful when:
  349. - You want to receive a subset of messages based on their PGNs, even if the
  350. address is the same.
  351. - You need to handle both broadcast and unicast messages but only care about
  352. certain message types or parameters.
  353. - The ``bind(2)`` and ``connect(2)`` functions only allow binding to a single
  354. address, which might not be sufficient if the process needs to handle multiple
  355. PGNs but does not want to open multiple sockets.
  356. To remove existing filters, you can pass ``optval == NULL`` or ``optlen == 0``
  357. to ``setsockopt(2)``. This will clear all currently set filters. If you want to
  358. **update** the set of filters, you must pass the updated filter set to
  359. ``setsockopt(2)``, as the new filter set will **replace** the old one entirely.
  360. This behavior ensures that any previous filter configuration is discarded and
  361. only the new set is applied.
  362. Example of removing all filters:
  363. .. code-block:: c
  364. setsockopt(sock, SOL_CAN_J1939, SO_J1939_FILTER, NULL, 0);
  365. **Maximum number of filters:** The maximum amount of filters that can be
  366. applied using ``SO_J1939_FILTER`` is defined by ``J1939_FILTER_MAX``, which is
  367. set to 512. This means you can configure up to 512 individual filters to match
  368. your specific filtering needs.
  369. Practical use case: **Monitoring Address Claiming**
  370. One practical use case is monitoring the J1939 address claiming process by
  371. filtering for specific PGNs related to address claiming. This allows a process
  372. to monitor and handle address claims without processing unrelated messages.
  373. Example:
  374. .. code-block:: c
  375. struct j1939_filter filt[] = {
  376. {
  377. .pgn = J1939_PGN_ADDRESS_CLAIMED,
  378. .pgn_mask = J1939_PGN_PDU1_MAX,
  379. }, {
  380. .pgn = J1939_PGN_REQUEST,
  381. .pgn_mask = J1939_PGN_PDU1_MAX,
  382. }, {
  383. .pgn = J1939_PGN_ADDRESS_COMMANDED,
  384. .pgn_mask = J1939_PGN_MAX,
  385. },
  386. };
  387. setsockopt(sock, SOL_CAN_J1939, SO_J1939_FILTER, &filt, sizeof(filt));
  388. In this example, the socket will only receive messages with the PGNs related to
  389. address claiming: ``J1939_PGN_ADDRESS_CLAIMED``, ``J1939_PGN_REQUEST``, and
  390. ``J1939_PGN_ADDRESS_COMMANDED``. This is particularly useful in scenarios where
  391. you want to monitor and process address claims without being overwhelmed by
  392. other traffic on the J1939 network.
  393. ``SO_J1939_PROMISC``
  394. ~~~~~~~~~~~~~~~~~~~~
  395. The ``SO_J1939_PROMISC`` option enables socket-level promiscuous mode. When
  396. this option is enabled, the socket will receive all J1939 traffic, regardless
  397. of any filters set by ``bind()`` or ``connect()``. This is analogous to
  398. enabling promiscuous mode for an Ethernet interface, where all traffic on the
  399. network segment is captured.
  400. However, **`SO_J1939_FILTER` has a higher priority** compared to
  401. ``SO_J1939_PROMISC``. This means that even in promiscuous mode, you can reduce
  402. the number of packets received by applying specific filters with
  403. `SO_J1939_FILTER`. The filters will limit which packets are passed to the
  404. socket, allowing for more refined traffic selection while promiscuous mode is
  405. active.
  406. The acceptable value size for this option is ``sizeof(int)``, and the value is
  407. only differentiated between `0` and non-zero. A value of `0` disables
  408. promiscuous mode, while any non-zero value enables it.
  409. This combination can be useful for debugging or monitoring specific types of
  410. traffic while still capturing a broad set of messages.
  411. Example:
  412. .. code-block:: c
  413. int value = 1;
  414. setsockopt(sock, SOL_CAN_J1939, SO_J1939_PROMISC, &value, sizeof(value));
  415. In this example, setting ``value`` to any non-zero value (e.g., `1`) enables
  416. promiscuous mode, allowing the socket to receive all J1939 traffic on the
  417. network.
  418. ``SO_BROADCAST``
  419. ~~~~~~~~~~~~~~~~
  420. The ``SO_BROADCAST`` option enables the sending and receiving of broadcast
  421. messages. By default, broadcast messages are disabled for J1939 sockets. When
  422. this option is enabled, the socket will be allowed to send and receive
  423. broadcast packets on the J1939 network.
  424. Due to the nature of the CAN bus as a shared medium, all messages transmitted
  425. on the bus are visible to all participants. In the context of J1939,
  426. broadcasting refers to using a specific destination address field, where the
  427. destination address is set to a value that indicates the message is intended
  428. for all participants (usually a global address such as 0xFF). Enabling the
  429. broadcast option allows the socket to send and receive such broadcast messages.
  430. The acceptable value size for this option is ``sizeof(int)``, and the value is
  431. only differentiated between `0` and non-zero. A value of `0` disables the
  432. ability to send and receive broadcast messages, while any non-zero value
  433. enables it.
  434. Example:
  435. .. code-block:: c
  436. int value = 1;
  437. setsockopt(sock, SOL_SOCKET, SO_BROADCAST, &value, sizeof(value));
  438. In this example, setting ``value`` to any non-zero value (e.g., `1`) enables
  439. the socket to send and receive broadcast messages.
  440. ``SO_J1939_SEND_PRIO``
  441. ~~~~~~~~~~~~~~~~~~~~~~
  442. The ``SO_J1939_SEND_PRIO`` option sets the priority of outgoing J1939 messages
  443. for the socket. In J1939, messages can have different priorities, and lower
  444. numerical values indicate higher priority. This option allows the user to
  445. control the priority of messages sent from the socket by adjusting the priority
  446. bits in the CAN identifier.
  447. The acceptable value **size** for this option is ``sizeof(int)``, and the value
  448. is expected to be in the range of 0 to 7, where `0` is the highest priority,
  449. and `7` is the lowest. By default, the priority is set to `6` if this option is
  450. not explicitly configured.
  451. Note that the priority values `0` and `1` can only be set if the process has
  452. the `CAP_NET_ADMIN` capability. These are reserved for high-priority traffic
  453. and require administrative privileges.
  454. Example:
  455. .. code-block:: c
  456. int prio = 3; // Priority value between 0 (highest) and 7 (lowest)
  457. setsockopt(sock, SOL_CAN_J1939, SO_J1939_SEND_PRIO, &prio, sizeof(prio));
  458. In this example, the priority is set to `3`, meaning the outgoing messages will
  459. be sent with a moderate priority level.
  460. ``SO_J1939_ERRQUEUE``
  461. ~~~~~~~~~~~~~~~~~~~~~
  462. The ``SO_J1939_ERRQUEUE`` option enables the socket to receive error messages
  463. from the error queue, providing diagnostic information about transmission
  464. failures, protocol violations, or other issues that occur during J1939
  465. communication. Once this option is set, user space is required to handle
  466. ``MSG_ERRQUEUE`` messages.
  467. Setting ``SO_J1939_ERRQUEUE`` to ``0`` will purge any currently present error
  468. messages in the error queue. When enabled, error messages can be retrieved
  469. using the ``recvmsg(2)`` system call.
  470. When subscribing to the error queue, the following error events can be
  471. accessed:
  472. - **``J1939_EE_INFO_TX_ABORT``**: Transmission abort errors.
  473. - **``J1939_EE_INFO_RX_RTS``**: Reception of RTS (Request to Send) control
  474. frames.
  475. - **``J1939_EE_INFO_RX_DPO``**: Reception of data packets with Data Page Offset
  476. (DPO).
  477. - **``J1939_EE_INFO_RX_ABORT``**: Reception abort errors.
  478. The error queue can be used to correlate errors with specific message transfer
  479. sessions using the session ID (``tskey``). The session ID is assigned via the
  480. ``SOF_TIMESTAMPING_OPT_ID`` flag, which is set by enabling the
  481. ``SO_TIMESTAMPING`` option.
  482. If ``SO_J1939_ERRQUEUE`` is activated, the user is required to pull messages
  483. from the error queue, meaning that using plain ``recv(2)`` is not sufficient
  484. anymore. The user must use ``recvmsg(2)`` with appropriate flags to handle
  485. error messages. Failure to do so can result in the socket becoming blocked with
  486. unprocessed error messages in the queue.
  487. It is **recommended** that ``SO_J1939_ERRQUEUE`` be used in combination with
  488. ``SO_TIMESTAMPING`` in most cases. This enables proper error handling along
  489. with session tracking and timestamping, providing a more detailed analysis of
  490. message transfers and errors.
  491. The acceptable value **size** for this option is ``sizeof(int)``, and the value
  492. is only differentiated between ``0`` and non-zero. A value of ``0`` disables
  493. error queue reception and purges any existing error messages, while any
  494. non-zero value enables it.
  495. Example:
  496. .. code-block:: c
  497. int enable = 1; // Enable error queue reception
  498. setsockopt(sock, SOL_CAN_J1939, SO_J1939_ERRQUEUE, &enable, sizeof(enable));
  499. // Enable timestamping with session tracking via tskey
  500. int timestamping = SOF_TIMESTAMPING_OPT_ID | SOF_TIMESTAMPING_TX_ACK |
  501. SOF_TIMESTAMPING_TX_SCHED |
  502. SOF_TIMESTAMPING_RX_SOFTWARE | SOF_TIMESTAMPING_OPT_CMSG;
  503. setsockopt(sock, SOL_SOCKET, SO_TIMESTAMPING, &timestamping,
  504. sizeof(timestamping));
  505. When enabled, error messages can be retrieved using ``recvmsg(2)``. By
  506. combining ``SO_J1939_ERRQUEUE`` with ``SO_TIMESTAMPING`` (with
  507. ``SOF_TIMESTAMPING_OPT_ID`` and ``SOF_TIMESTAMPING_OPT_CMSG`` enabled), the
  508. user can track message transfers, retrieve precise timestamps, and correlate
  509. errors with specific sessions.
  510. For more information on enabling timestamps and session tracking, refer to the
  511. `SO_TIMESTAMPING` section.
  512. ``SO_TIMESTAMPING``
  513. ~~~~~~~~~~~~~~~~~~~
  514. The ``SO_TIMESTAMPING`` option allows the socket to receive timestamps for
  515. various events related to message transmissions and receptions in J1939. This
  516. option is often used in combination with ``SO_J1939_ERRQUEUE`` to provide
  517. detailed diagnostic information, session tracking, and precise timing data for
  518. message transfers.
  519. In J1939, all payloads provided by user space, regardless of size, are
  520. processed by the kernel as **sessions**. This includes both single-frame
  521. messages (up to 8 bytes) and multi-frame protocols such as the Transport
  522. Protocol (TP) and Extended Transport Protocol (ETP). Even for small,
  523. single-frame messages, the kernel creates a session to manage the transmission
  524. and reception. The concept of sessions allows the kernel to manage various
  525. aspects of the protocol, such as reassembling multi-frame messages and tracking
  526. the status of transmissions.
  527. When receiving extended error messages from the error queue, the error
  528. information is delivered through a `struct sock_extended_err`, accessible via
  529. the control message (``cmsg``) retrieved using the ``recvmsg(2)`` system call.
  530. There are two typical origins for the extended error messages in J1939:
  531. 1. ``serr->ee_origin == SO_EE_ORIGIN_TIMESTAMPING``:
  532. In this case, the `serr->ee_info` field will contain one of the following
  533. timestamp types:
  534. - ``SCM_TSTAMP_SCHED``: This timestamp is valid for Extended Transport
  535. Protocol (ETP) transfers and simple transfers (8 bytes or less). It
  536. indicates when a message or set of frames has been scheduled for
  537. transmission.
  538. - For simple transfers (8 bytes or less), it marks the point when the
  539. message is queued and ready to be sent onto the CAN bus.
  540. - For ETP transfers, it is sent after receiving a CTS (Clear to Send)
  541. frame on the sender side, indicating that a new set of frames has been
  542. scheduled for transmission.
  543. - The Transport Protocol (TP) case is currently not implemented for this
  544. timestamp.
  545. - On the receiver side, the counterpart to this event for ETP is
  546. represented by the ``J1939_EE_INFO_RX_DPO`` message, which indicates the
  547. reception of a Data Page Offset (DPO) control frame.
  548. - ``SCM_TSTAMP_ACK``: This timestamp indicates the acknowledgment of the
  549. message or session.
  550. - For simple transfers (8 bytes or less), it marks when the message has
  551. been sent and an echo confirmation has been received from the CAN
  552. controller, indicating that the frame was transmitted onto the bus.
  553. - For multi-frame transfers (TP or ETP), it signifies that the entire
  554. session has been acknowledged, typically after receiving the End of
  555. Message Acknowledgment (EOMA) packet.
  556. 2. ``serr->ee_origin == SO_EE_ORIGIN_LOCAL``:
  557. In this case, the `serr->ee_info` field will contain one of the following
  558. J1939 stack-specific message types:
  559. - ``J1939_EE_INFO_TX_ABORT``: This message indicates that the transmission
  560. of a message or session was aborted. The cause of the abort can come from
  561. various sources:
  562. - **CAN stack failure**: The J1939 stack was unable to pass the frame to
  563. the CAN framework for transmission.
  564. - **Echo failure**: The J1939 stack did not receive an echo confirmation
  565. from the CAN controller, meaning the frame may not have been successfully
  566. transmitted to the CAN bus.
  567. - **Protocol-level issues**: For multi-frame transfers (TP/ETP), this
  568. could include protocol-related errors, such as an abort signaled by the
  569. receiver or a timeout at the protocol level, which causes the session to
  570. terminate prematurely.
  571. - The corresponding error code is stored in ``serr->ee_data``
  572. (``session->err`` on kernel side), providing additional details about
  573. the specific reason for the abort.
  574. - ``J1939_EE_INFO_RX_RTS``: This message indicates that the J1939 stack has
  575. received a Request to Send (RTS) control frame, signaling the start of a
  576. multi-frame transfer using the Transport Protocol (TP) or Extended
  577. Transport Protocol (ETP).
  578. - It informs the receiver that the sender is ready to transmit a
  579. multi-frame message and includes details about the total message size
  580. and the number of frames to be sent.
  581. - Statistics such as ``J1939_NLA_TOTAL_SIZE``, ``J1939_NLA_PGN``,
  582. ``J1939_NLA_SRC_NAME``, and ``J1939_NLA_DEST_NAME`` are provided along
  583. with the ``J1939_EE_INFO_RX_RTS`` message, giving detailed information
  584. about the incoming transfer.
  585. - ``J1939_EE_INFO_RX_DPO``: This message indicates that the J1939 stack has
  586. received a Data Page Offset (DPO) control frame, which is part of the
  587. Extended Transport Protocol (ETP).
  588. - The DPO frame signals the continuation of an ETP multi-frame message by
  589. indicating the offset position in the data being transferred. It helps
  590. the receiver manage large data sets by identifying which portion of the
  591. message is being received.
  592. - It is typically paired with a corresponding ``SCM_TSTAMP_SCHED`` event
  593. on the sender side, which indicates when the next set of frames is
  594. scheduled for transmission.
  595. - This event includes statistics such as ``J1939_NLA_BYTES_ACKED``, which
  596. tracks the number of bytes acknowledged up to that point in the session.
  597. - ``J1939_EE_INFO_RX_ABORT``: This message indicates that the reception of a
  598. multi-frame message (Transport Protocol or Extended Transport Protocol) has
  599. been aborted.
  600. - The abort can be triggered by protocol-level errors such as timeouts, an
  601. unexpected frame, or a specific abort request from the sender.
  602. - This message signals that the receiver cannot continue processing the
  603. transfer, and the session is terminated.
  604. - The corresponding error code is stored in ``serr->ee_data``
  605. (``session->err`` on kernel side ), providing further details about the
  606. reason for the abort, such as protocol violations or timeouts.
  607. - After receiving this message, the receiver discards the partially received
  608. frames, and the multi-frame session is considered incomplete.
  609. In both cases, if ``SOF_TIMESTAMPING_OPT_ID`` is enabled, ``serr->ee_data``
  610. will be set to the session’s unique identifier (``session->tskey``). This
  611. allows user space to track message transfers by their session identifier across
  612. multiple frames or stages.
  613. In all other cases, ``serr->ee_errno`` will be set to ``ENOMSG``, except for
  614. the ``J1939_EE_INFO_TX_ABORT`` and ``J1939_EE_INFO_RX_ABORT`` cases, where the
  615. kernel sets ``serr->ee_data`` to the error stored in ``session->err``. All
  616. protocol-specific errors are converted to standard kernel error values and
  617. stored in ``session->err``. These error values are unified across system calls
  618. and ``serr->ee_errno``. Some of the known error values are described in the
  619. `Error Codes in the J1939 Stack` section.
  620. When the `J1939_EE_INFO_RX_RTS` message is provided, it will include the
  621. following statistics for multi-frame messages (TP and ETP):
  622. - ``J1939_NLA_TOTAL_SIZE``: Total size of the message in the session.
  623. - ``J1939_NLA_PGN``: Parameter Group Number (PGN) identifying the message type.
  624. - ``J1939_NLA_SRC_NAME``: 64-bit name of the source ECU.
  625. - ``J1939_NLA_DEST_NAME``: 64-bit name of the destination ECU.
  626. - ``J1939_NLA_SRC_ADDR``: 8-bit source address of the sending ECU.
  627. - ``J1939_NLA_DEST_ADDR``: 8-bit destination address of the receiving ECU.
  628. - For other messages (including single-frame messages), only the following
  629. statistic is included:
  630. - ``J1939_NLA_BYTES_ACKED``: Number of bytes successfully acknowledged in the
  631. session.
  632. The key flags for ``SO_TIMESTAMPING`` include:
  633. - ``SOF_TIMESTAMPING_OPT_ID``: Enables the use of a unique session identifier
  634. (``tskey``) for each transfer. This identifier helps track message transfers
  635. and errors as distinct sessions in user space. When this option is enabled,
  636. ``serr->ee_data`` will be set to ``session->tskey``.
  637. - ``SOF_TIMESTAMPING_OPT_CMSG``: Sends timestamp information through control
  638. messages (``struct scm_timestamping``), allowing the application to retrieve
  639. timestamps alongside the data.
  640. - ``SOF_TIMESTAMPING_TX_SCHED``: Provides the timestamp for when a message is
  641. scheduled for transmission (``SCM_TSTAMP_SCHED``).
  642. - ``SOF_TIMESTAMPING_TX_ACK``: Provides the timestamp for when a message
  643. transmission is fully acknowledged (``SCM_TSTAMP_ACK``).
  644. - ``SOF_TIMESTAMPING_RX_SOFTWARE``: Provides timestamps for reception-related
  645. events (e.g., ``J1939_EE_INFO_RX_RTS``, ``J1939_EE_INFO_RX_DPO``,
  646. ``J1939_EE_INFO_RX_ABORT``).
  647. These flags enable detailed monitoring of message lifecycles, including
  648. transmission scheduling, acknowledgments, reception timestamps, and gathering
  649. detailed statistics about the communication session, especially for multi-frame
  650. payloads like TP and ETP.
  651. Example:
  652. .. code-block:: c
  653. // Enable timestamping with various options, including session tracking and
  654. // statistics
  655. int sock_opt = SOF_TIMESTAMPING_OPT_CMSG |
  656. SOF_TIMESTAMPING_TX_ACK |
  657. SOF_TIMESTAMPING_TX_SCHED |
  658. SOF_TIMESTAMPING_OPT_ID |
  659. SOF_TIMESTAMPING_RX_SOFTWARE;
  660. setsockopt(sock, SOL_SOCKET, SO_TIMESTAMPING, &sock_opt, sizeof(sock_opt));
  661. Dynamic Addressing
  662. ------------------
  663. Distinction has to be made between using the claimed address and doing an
  664. address claim. To use an already claimed address, one has to fill in the
  665. ``j1939.name`` member and provide it to ``bind(2)``. If the name had claimed an address
  666. earlier, all further messages being sent will use that address. And the
  667. ``j1939.addr`` member will be ignored.
  668. An exception on this is PGN 0x0ee00. This is the "Address Claim/Cannot Claim
  669. Address" message and the kernel will use the ``j1939.addr`` member for that PGN if
  670. necessary.
  671. To claim an address following code example can be used:
  672. .. code-block:: C
  673. struct sockaddr_can baddr = {
  674. .can_family = AF_CAN,
  675. .can_addr.j1939 = {
  676. .name = name,
  677. .addr = J1939_IDLE_ADDR,
  678. .pgn = J1939_NO_PGN, /* to disable bind() rx filter for PGN */
  679. },
  680. .can_ifindex = if_nametoindex("can0"),
  681. };
  682. bind(sock, (struct sockaddr *)&baddr, sizeof(baddr));
  683. /* for Address Claiming broadcast must be allowed */
  684. int value = 1;
  685. setsockopt(sock, SOL_SOCKET, SO_BROADCAST, &value, sizeof(value));
  686. /* configured advanced RX filter with PGN needed for Address Claiming */
  687. const struct j1939_filter filt[] = {
  688. {
  689. .pgn = J1939_PGN_ADDRESS_CLAIMED,
  690. .pgn_mask = J1939_PGN_PDU1_MAX,
  691. }, {
  692. .pgn = J1939_PGN_REQUEST,
  693. .pgn_mask = J1939_PGN_PDU1_MAX,
  694. }, {
  695. .pgn = J1939_PGN_ADDRESS_COMMANDED,
  696. .pgn_mask = J1939_PGN_MAX,
  697. },
  698. };
  699. setsockopt(sock, SOL_CAN_J1939, SO_J1939_FILTER, &filt, sizeof(filt));
  700. uint64_t dat = htole64(name);
  701. const struct sockaddr_can saddr = {
  702. .can_family = AF_CAN,
  703. .can_addr.j1939 = {
  704. .pgn = J1939_PGN_ADDRESS_CLAIMED,
  705. .addr = J1939_NO_ADDR,
  706. },
  707. };
  708. /* Afterwards do a sendto(2) with data set to the NAME (Little Endian). If the
  709. * NAME provided, does not match the j1939.name provided to bind(2), EPROTO
  710. * will be returned.
  711. */
  712. sendto(sock, dat, sizeof(dat), 0, (const struct sockaddr *)&saddr, sizeof(saddr));
  713. If no-one else contests the address claim within 250ms after transmission, the
  714. kernel marks the NAME-SA assignment as valid. The valid assignment will be kept
  715. among other valid NAME-SA assignments. From that point, any socket bound to the
  716. NAME can send packets.
  717. If another ECU claims the address, the kernel will mark the NAME-SA expired.
  718. No socket bound to the NAME can send packets (other than address claims). To
  719. claim another address, some socket bound to NAME, must ``bind(2)`` again, but with
  720. only ``j1939.addr`` changed to the new SA, and must then send a valid address claim
  721. packet. This restarts the state machine in the kernel (and any other
  722. participant on the bus) for this NAME.
  723. ``can-utils`` also include the ``j1939acd`` tool, so it can be used as code example or as
  724. default Address Claiming daemon.
  725. Send Examples
  726. -------------
  727. Static Addressing
  728. ^^^^^^^^^^^^^^^^^
  729. This example will send a PGN (0x12300) from SA 0x20 to DA 0x30.
  730. Bind:
  731. .. code-block:: C
  732. struct sockaddr_can baddr = {
  733. .can_family = AF_CAN,
  734. .can_addr.j1939 = {
  735. .name = J1939_NO_NAME,
  736. .addr = 0x20,
  737. .pgn = J1939_NO_PGN,
  738. },
  739. .can_ifindex = if_nametoindex("can0"),
  740. };
  741. bind(sock, (struct sockaddr *)&baddr, sizeof(baddr));
  742. Now, the socket 'sock' is bound to the SA 0x20. Since no ``connect(2)`` was called,
  743. at this point we can use only ``sendto(2)`` or ``sendmsg(2)``.
  744. Send:
  745. .. code-block:: C
  746. const struct sockaddr_can saddr = {
  747. .can_family = AF_CAN,
  748. .can_addr.j1939 = {
  749. .name = J1939_NO_NAME;
  750. .addr = 0x30,
  751. .pgn = 0x12300,
  752. },
  753. };
  754. sendto(sock, dat, sizeof(dat), 0, (const struct sockaddr *)&saddr, sizeof(saddr));
  755. Error Codes in the J1939 Stack
  756. ------------------------------
  757. This section lists all potential kernel error codes that can be exposed to user
  758. space when interacting with the J1939 stack. It includes both standard error
  759. codes and those derived from protocol-specific abort codes.
  760. - ``EAGAIN``: Operation would block; retry may succeed. One common reason is
  761. that an active TP or ETP session exists, and an attempt was made to start a
  762. new overlapping TP or ETP session between the same peers.
  763. - ``ENETDOWN``: Network is down. This occurs when the CAN interface is switched
  764. to the "down" state.
  765. - ``ENOBUFS``: No buffer space available. This error occurs when the CAN
  766. interface's transmit (TX) queue is full, and no more messages can be queued.
  767. - ``EOVERFLOW``: Value too large for defined data type. In J1939, this can
  768. happen if the requested data lies outside of the queued buffer. For example,
  769. if a CTS (Clear to Send) requests an offset not available in the kernel buffer
  770. because user space did not provide enough data.
  771. - ``EBUSY``: Device or resource is busy. For example, this occurs if an
  772. identical session is already active and the stack is unable to recover from
  773. the condition.
  774. - ``EACCES``: Permission denied. This error can occur, for example, when
  775. attempting to send broadcast messages, but the socket is not configured with
  776. ``SO_BROADCAST``.
  777. - ``EADDRNOTAVAIL``: Address not available. This error occurs in cases such as:
  778. - When attempting to use ``getsockname(2)`` to retrieve the peer's address,
  779. but the socket is not connected.
  780. - When trying to send data to or from a NAME, but address claiming for the
  781. NAME was not performed or detected by the stack.
  782. - ``EBADFD``: File descriptor in bad state. This error can occur if:
  783. - Attempting to send data to an unbound socket.
  784. - The socket is bound but has no source name, and the source address is
  785. ``J1939_NO_ADDR``.
  786. - The ``can_ifindex`` is incorrect.
  787. - ``EFAULT``: Bad address. Occurs mostly when the stack can't copy from or to a
  788. sockptr, when there is insufficient data from user space, or when the buffer
  789. provided by user space is not large enough for the requested data.
  790. - ``EINTR``: A signal occurred before any data was transmitted; see ``signal(7)``.
  791. - ``EINVAL``: Invalid argument passed. For example:
  792. - ``msg->msg_namelen`` is less than ``J1939_MIN_NAMELEN``.
  793. - ``addr->can_family`` is not equal to ``AF_CAN``.
  794. - An incorrect PGN was provided.
  795. - ``ENODEV``: No such device. This happens when the CAN network device cannot
  796. be found for the provided ``can_ifindex`` or if ``can_ifindex`` is 0.
  797. - ``ENOMEM``: Out of memory. Typically related to issues with memory allocation
  798. in the stack.
  799. - ``ENOPROTOOPT``: Protocol not available. This can occur when using
  800. ``getsockopt(2)`` or ``setsockopt(2)`` if the requested socket option is not
  801. available.
  802. - ``EDESTADDRREQ``: Destination address required. This error occurs:
  803. - In the case of ``connect(2)``, if the ``struct sockaddr *uaddr`` is ``NULL``.
  804. - In the case of ``send*(2)``, if there is an attempt to send an ETP message
  805. to a broadcast address.
  806. - ``EDOM``: Argument out of domain. This error may happen if attempting to send
  807. a TP or ETP message to a PGN that is reserved for control PGNs for TP or ETP
  808. operations.
  809. - ``EIO``: I/O error. This can occur if the amount of data provided to the
  810. socket for a TP or ETP session does not match the announced amount of data for
  811. the session.
  812. - ``ENOENT``: No such file or directory. This can happen when the stack
  813. attempts to transfer CTS or EOMA but cannot find a matching receiving socket
  814. anymore.
  815. - ``ENOIOCTLCMD``: No ioctls are available for the socket layer.
  816. - ``EPERM``: Operation not permitted. For example, this can occur if a
  817. requested action requires ``CAP_NET_ADMIN`` privileges.
  818. - ``ENETUNREACH``: Network unreachable. Most likely, this occurs when frames
  819. cannot be transmitted to the CAN bus.
  820. - ``ETIME``: Timer expired. This can happen if a timeout occurs while
  821. attempting to send a simple message, for example, when an echo message from
  822. the controller is not received.
  823. - ``EPROTO``: Protocol error.
  824. - Used for various protocol-level errors in J1939, including:
  825. - Duplicate sequence number.
  826. - Unexpected EDPO or ECTS packet.
  827. - Invalid PGN or offset in EDPO/ECTS.
  828. - Number of EDPO packets exceeded CTS allowance.
  829. - Any other protocol-level error.
  830. - ``EMSGSIZE``: Message too long.
  831. - ``ENOMSG``: No message available.
  832. - ``EALREADY``: The ECU is already engaged in one or more connection-managed
  833. sessions and cannot support another.
  834. - ``EHOSTUNREACH``: A timeout occurred, and the session was aborted.
  835. - ``EBADMSG``: CTS (Clear to Send) messages were received during an active data
  836. transfer, causing an abort.
  837. - ``ENOTRECOVERABLE``: The maximum retransmission request limit was reached,
  838. and the session cannot recover.
  839. - ``ENOTCONN``: An unexpected data transfer packet was received.
  840. - ``EILSEQ``: A bad sequence number was received, and the software could not
  841. recover.