19d71dd0cSThe j1939 authors.. SPDX-License-Identifier: (GPL-2.0 OR MIT) 29d71dd0cSThe j1939 authors 39d71dd0cSThe j1939 authors=================== 49d71dd0cSThe j1939 authorsJ1939 Documentation 59d71dd0cSThe j1939 authors=================== 69d71dd0cSThe j1939 authors 79d71dd0cSThe j1939 authorsOverview / What Is J1939 89d71dd0cSThe j1939 authors======================== 99d71dd0cSThe j1939 authors 109d71dd0cSThe j1939 authorsSAE J1939 defines a higher layer protocol on CAN. It implements a more 119d71dd0cSThe j1939 authorssophisticated addressing scheme and extends the maximum packet size above 8 129d71dd0cSThe j1939 authorsbytes. Several derived specifications exist, which differ from the original 13864a2756SYegor YefremovJ1939 on the application level, like MilCAN A, NMEA2000, and especially 149d71dd0cSThe j1939 authorsISO-11783 (ISOBUS). This last one specifies the so-called ETP (Extended 15864a2756SYegor YefremovTransport Protocol), which has been included in this implementation. This 169d71dd0cSThe j1939 authorsresults in a maximum packet size of ((2 ^ 24) - 1) * 7 bytes == 111 MiB. 179d71dd0cSThe j1939 authors 189d71dd0cSThe j1939 authorsSpecifications used 199d71dd0cSThe j1939 authors------------------- 209d71dd0cSThe j1939 authors 219d71dd0cSThe j1939 authors* SAE J1939-21 : data link layer 229d71dd0cSThe j1939 authors* SAE J1939-81 : network management 239d71dd0cSThe j1939 authors* ISO 11783-6 : Virtual Terminal (Extended Transport Protocol) 249d71dd0cSThe j1939 authors 259d71dd0cSThe j1939 authors.. _j1939-motivation: 269d71dd0cSThe j1939 authors 279d71dd0cSThe j1939 authorsMotivation 289d71dd0cSThe j1939 authors========== 299d71dd0cSThe j1939 authors 309d71dd0cSThe j1939 authorsGiven the fact there's something like SocketCAN with an API similar to BSD 319d71dd0cSThe j1939 authorssockets, we found some reasons to justify a kernel implementation for the 329d71dd0cSThe j1939 authorsaddressing and transport methods used by J1939. 339d71dd0cSThe j1939 authors 349d71dd0cSThe j1939 authors* **Addressing:** when a process on an ECU communicates via J1939, it should 35864a2756SYegor Yefremov not necessarily know its source address. Although, at least one process per 369d71dd0cSThe j1939 authors ECU should know the source address. Other processes should be able to reuse 379d71dd0cSThe j1939 authors that address. This way, address parameters for different processes 389d71dd0cSThe j1939 authors cooperating for the same ECU, are not duplicated. This way of working is 39864a2756SYegor Yefremov closely related to the UNIX concept, where programs do just one thing and do 409d71dd0cSThe j1939 authors it well. 419d71dd0cSThe j1939 authors 429d71dd0cSThe j1939 authors* **Dynamic addressing:** Address Claiming in J1939 is time critical. 43864a2756SYegor Yefremov Furthermore, data transport should be handled properly during the address 449d71dd0cSThe j1939 authors negotiation. Putting this functionality in the kernel eliminates it as a 459d71dd0cSThe j1939 authors requirement for _every_ user space process that communicates via J1939. This 469d71dd0cSThe j1939 authors results in a consistent J1939 bus with proper addressing. 479d71dd0cSThe j1939 authors 489d71dd0cSThe j1939 authors* **Transport:** both TP & ETP reuse some PGNs to relay big packets over them. 499d71dd0cSThe j1939 authors Different processes may thus use the same TP & ETP PGNs without actually 509d71dd0cSThe j1939 authors knowing it. The individual TP & ETP sessions _must_ be serialized 519d71dd0cSThe j1939 authors (synchronized) between different processes. The kernel solves this problem 529d71dd0cSThe j1939 authors properly and eliminates the serialization (synchronization) as a requirement 539d71dd0cSThe j1939 authors for _every_ user space process that communicates via J1939. 549d71dd0cSThe j1939 authors 559d71dd0cSThe j1939 authorsJ1939 defines some other features (relaying, gateway, fast packet transport, 569d71dd0cSThe j1939 authors...). In-kernel code for these would not contribute to protocol stability. 579d71dd0cSThe j1939 authorsTherefore, these parts are left to user space. 589d71dd0cSThe j1939 authors 599d71dd0cSThe j1939 authorsThe J1939 sockets operate on CAN network devices (see SocketCAN). Any J1939 609d71dd0cSThe j1939 authorsuser space library operating on CAN raw sockets will still operate properly. 61864a2756SYegor YefremovSince such a library does not communicate with the in-kernel implementation, care 629d71dd0cSThe j1939 authorsmust be taken that these two do not interfere. In practice, this means they 639d71dd0cSThe j1939 authorscannot share ECU addresses. A single ECU (or virtual ECU) address is used by 649d71dd0cSThe j1939 authorsthe library exclusively, or by the in-kernel system exclusively. 659d71dd0cSThe j1939 authors 669d71dd0cSThe j1939 authorsJ1939 concepts 679d71dd0cSThe j1939 authors============== 689d71dd0cSThe j1939 authors 699d71dd0cSThe j1939 authorsPGN 709d71dd0cSThe j1939 authors--- 719d71dd0cSThe j1939 authors 72b7d3c0e5SYegor YefremovThe J1939 protocol uses the 29-bit CAN identifier with the following structure: 73b7d3c0e5SYegor Yefremov 74b7d3c0e5SYegor Yefremov ============ ============== ==================== 75b7d3c0e5SYegor Yefremov 29 bit CAN-ID 76b7d3c0e5SYegor Yefremov -------------------------------------------------- 77b7d3c0e5SYegor Yefremov Bit positions within the CAN-ID 78b7d3c0e5SYegor Yefremov -------------------------------------------------- 79b7d3c0e5SYegor Yefremov 28 ... 26 25 ... 8 7 ... 0 80b7d3c0e5SYegor Yefremov ============ ============== ==================== 81b7d3c0e5SYegor Yefremov Priority PGN SA (Source Address) 82b7d3c0e5SYegor Yefremov ============ ============== ==================== 83b7d3c0e5SYegor Yefremov 849d71dd0cSThe j1939 authorsThe PGN (Parameter Group Number) is a number to identify a packet. The PGN 859d71dd0cSThe j1939 authorsis composed as follows: 86b7d3c0e5SYegor Yefremov 87b7d3c0e5SYegor Yefremov ============ ============== ================= ================= 88b7d3c0e5SYegor Yefremov PGN 89b7d3c0e5SYegor Yefremov ------------------------------------------------------------------ 90b7d3c0e5SYegor Yefremov Bit positions within the CAN-ID 91b7d3c0e5SYegor Yefremov ------------------------------------------------------------------ 92b7d3c0e5SYegor Yefremov 25 24 23 ... 16 15 ... 8 93b7d3c0e5SYegor Yefremov ============ ============== ================= ================= 94b7d3c0e5SYegor Yefremov R (Reserved) DP (Data Page) PF (PDU Format) PS (PDU Specific) 95b7d3c0e5SYegor Yefremov ============ ============== ================= ================= 969d71dd0cSThe j1939 authors 979d71dd0cSThe j1939 authorsIn J1939-21 distinction is made between PDU1 format (where PF < 240) and PDU2 98864a2756SYegor Yefremovformat (where PF >= 240). Furthermore, when using the PDU2 format, the PS-field 999d71dd0cSThe j1939 authorscontains a so-called Group Extension, which is part of the PGN. When using PDU2 1009d71dd0cSThe j1939 authorsformat, the Group Extension is set in the PS-field. 1019d71dd0cSThe j1939 authors 102b7d3c0e5SYegor Yefremov ============== ======================== 103b7d3c0e5SYegor Yefremov PDU1 Format (specific) (peer to peer) 104b7d3c0e5SYegor Yefremov ---------------------------------------- 105b7d3c0e5SYegor Yefremov Bit positions within the CAN-ID 106b7d3c0e5SYegor Yefremov ---------------------------------------- 107b7d3c0e5SYegor Yefremov 23 ... 16 15 ... 8 108b7d3c0e5SYegor Yefremov ============== ======================== 109b7d3c0e5SYegor Yefremov 00h ... EFh DA (Destination address) 110b7d3c0e5SYegor Yefremov ============== ======================== 111b7d3c0e5SYegor Yefremov 112b7d3c0e5SYegor Yefremov ============== ======================== 113b7d3c0e5SYegor Yefremov PDU2 Format (global) (broadcast) 114b7d3c0e5SYegor Yefremov ---------------------------------------- 115b7d3c0e5SYegor Yefremov Bit positions within the CAN-ID 116b7d3c0e5SYegor Yefremov ---------------------------------------- 117b7d3c0e5SYegor Yefremov 23 ... 16 15 ... 8 118b7d3c0e5SYegor Yefremov ============== ======================== 119a266ef69SRandy Dunlap F0h ... FFh GE (Group Extension) 120b7d3c0e5SYegor Yefremov ============== ======================== 121b7d3c0e5SYegor Yefremov 1229d71dd0cSThe j1939 authorsOn the other hand, when using PDU1 format, the PS-field contains a so-called 1239d71dd0cSThe j1939 authorsDestination Address, which is _not_ part of the PGN. When communicating a PGN 124*769aa742SAlexander Hölzlfrom user space to kernel (or vice versa) and PDU1 format is used, the PS-field 1259d71dd0cSThe j1939 authorsof the PGN shall be set to zero. The Destination Address shall be set 1269d71dd0cSThe j1939 authorselsewhere. 1279d71dd0cSThe j1939 authors 1289d71dd0cSThe j1939 authorsRegarding PGN mapping to 29-bit CAN identifier, the Destination Address shall 1299d71dd0cSThe j1939 authorsbe get/set from/to the appropriate bits of the identifier by the kernel. 1309d71dd0cSThe j1939 authors 1319d71dd0cSThe j1939 authors 1329d71dd0cSThe j1939 authorsAddressing 1339d71dd0cSThe j1939 authors---------- 1349d71dd0cSThe j1939 authors 1359d71dd0cSThe j1939 authorsBoth static and dynamic addressing methods can be used. 1369d71dd0cSThe j1939 authors 137864a2756SYegor YefremovFor static addresses, no extra checks are made by the kernel and provided 1389d71dd0cSThe j1939 authorsaddresses are considered right. This responsibility is for the OEM or system 1399d71dd0cSThe j1939 authorsintegrator. 1409d71dd0cSThe j1939 authors 1419d71dd0cSThe j1939 authorsFor dynamic addressing, so-called Address Claiming, extra support is foreseen 142864a2756SYegor Yefremovin the kernel. In J1939 any ECU is known by its 64-bit NAME. At the moment of 1439d71dd0cSThe j1939 authorsa successful address claim, the kernel keeps track of both NAME and source 1449d71dd0cSThe j1939 authorsaddress being claimed. This serves as a base for filter schemes. By default, 145864a2756SYegor Yefremovpackets with a destination that is not locally will be rejected. 1469d71dd0cSThe j1939 authors 1479d71dd0cSThe j1939 authorsMixed mode packets (from a static to a dynamic address or vice versa) are 1489d71dd0cSThe j1939 authorsallowed. The BSD sockets define separate API calls for getting/setting the 1499d71dd0cSThe j1939 authorslocal & remote address and are applicable for J1939 sockets. 1509d71dd0cSThe j1939 authors 1519d71dd0cSThe j1939 authorsFiltering 1529d71dd0cSThe j1939 authors--------- 1539d71dd0cSThe j1939 authors 1549d71dd0cSThe j1939 authorsJ1939 defines white list filters per socket that a user can set in order to 1559d71dd0cSThe j1939 authorsreceive a subset of the J1939 traffic. Filtering can be based on: 1569d71dd0cSThe j1939 authors 1579d71dd0cSThe j1939 authors* SA 1589d71dd0cSThe j1939 authors* SOURCE_NAME 1599d71dd0cSThe j1939 authors* PGN 1609d71dd0cSThe j1939 authors 1619d71dd0cSThe j1939 authorsWhen multiple filters are in place for a single socket, and a packet comes in 1629d71dd0cSThe j1939 authorsthat matches several of those filters, the packet is only received once for 1639d71dd0cSThe j1939 authorsthat socket. 1649d71dd0cSThe j1939 authors 1659d71dd0cSThe j1939 authorsHow to Use J1939 1669d71dd0cSThe j1939 authors================ 1679d71dd0cSThe j1939 authors 1689d71dd0cSThe j1939 authorsAPI Calls 1699d71dd0cSThe j1939 authors--------- 1709d71dd0cSThe j1939 authors 1719d71dd0cSThe j1939 authorsOn CAN, you first need to open a socket for communicating over a CAN network. 172a39372c2SYegor YefremovTo use J1939, ``#include <linux/can/j1939.h>``. From there, ``<linux/can.h>`` will be 1739d71dd0cSThe j1939 authorsincluded too. To open a socket, use: 1749d71dd0cSThe j1939 authors 1759d71dd0cSThe j1939 authors.. code-block:: C 1769d71dd0cSThe j1939 authors 1779d71dd0cSThe j1939 authors s = socket(PF_CAN, SOCK_DGRAM, CAN_J1939); 1789d71dd0cSThe j1939 authors 179a39372c2SYegor YefremovJ1939 does use ``SOCK_DGRAM`` sockets. In the J1939 specification, connections are 1809d71dd0cSThe j1939 authorsmentioned in the context of transport protocol sessions. These still deliver 181a39372c2SYegor Yefremovpackets to the other end (using several CAN packets). ``SOCK_STREAM`` is not 1829d71dd0cSThe j1939 authorssupported. 1839d71dd0cSThe j1939 authors 184a39372c2SYegor YefremovAfter the successful creation of the socket, you would normally use the ``bind(2)`` 185a39372c2SYegor Yefremovand/or ``connect(2)`` system call to bind the socket to a CAN interface. After 186a39372c2SYegor Yefremovbinding and/or connecting the socket, you can ``read(2)`` and ``write(2)`` from/to the 187a39372c2SYegor Yefremovsocket or use ``send(2)``, ``sendto(2)``, ``sendmsg(2)`` and the ``recv*()`` counterpart 1889d71dd0cSThe j1939 authorsoperations on the socket as usual. There are also J1939 specific socket options 1899d71dd0cSThe j1939 authorsdescribed below. 1909d71dd0cSThe j1939 authors 191a39372c2SYegor YefremovIn order to send data, a ``bind(2)`` must have been successful. ``bind(2)`` assigns a 1929d71dd0cSThe j1939 authorslocal address to a socket. 1939d71dd0cSThe j1939 authors 194864a2756SYegor YefremovDifferent from CAN is that the payload data is just the data that get sends, 195864a2756SYegor Yefremovwithout its header info. The header info is derived from the sockaddr supplied 196a39372c2SYegor Yefremovto ``bind(2)``, ``connect(2)``, ``sendto(2)`` and ``recvfrom(2)``. A ``write(2)`` with size 4 will 1979d71dd0cSThe j1939 authorsresult in a packet with 4 bytes. 1989d71dd0cSThe j1939 authors 1999d71dd0cSThe j1939 authorsThe sockaddr structure has extensions for use with J1939 as specified below: 2009d71dd0cSThe j1939 authors 2019d71dd0cSThe j1939 authors.. code-block:: C 2029d71dd0cSThe j1939 authors 2039d71dd0cSThe j1939 authors struct sockaddr_can { 2049d71dd0cSThe j1939 authors sa_family_t can_family; 2059d71dd0cSThe j1939 authors int can_ifindex; 2069d71dd0cSThe j1939 authors union { 2079d71dd0cSThe j1939 authors struct { 2089d71dd0cSThe j1939 authors __u64 name; 2099d71dd0cSThe j1939 authors /* pgn: 2109d71dd0cSThe j1939 authors * 8 bit: PS in PDU2 case, else 0 2119d71dd0cSThe j1939 authors * 8 bit: PF 2129d71dd0cSThe j1939 authors * 1 bit: DP 2139d71dd0cSThe j1939 authors * 1 bit: reserved 2149d71dd0cSThe j1939 authors */ 2159d71dd0cSThe j1939 authors __u32 pgn; 2169d71dd0cSThe j1939 authors __u8 addr; 2179d71dd0cSThe j1939 authors } j1939; 2189d71dd0cSThe j1939 authors } can_addr; 2199d71dd0cSThe j1939 authors } 2209d71dd0cSThe j1939 authors 221a39372c2SYegor Yefremov``can_family`` & ``can_ifindex`` serve the same purpose as for other SocketCAN sockets. 2229d71dd0cSThe j1939 authors 223a39372c2SYegor Yefremov``can_addr.j1939.pgn`` specifies the PGN (max 0x3ffff). Individual bits are 2249d71dd0cSThe j1939 authorsspecified above. 2259d71dd0cSThe j1939 authors 226a39372c2SYegor Yefremov``can_addr.j1939.name`` contains the 64-bit J1939 NAME. 2279d71dd0cSThe j1939 authors 228a39372c2SYegor Yefremov``can_addr.j1939.addr`` contains the address. 2299d71dd0cSThe j1939 authors 230a39372c2SYegor YefremovThe ``bind(2)`` system call assigns the local address, i.e. the source address when 231a39372c2SYegor Yefremovsending packages. If a PGN during ``bind(2)`` is set, it's used as a RX filter. 2329d71dd0cSThe j1939 authorsI.e. only packets with a matching PGN are received. If an ADDR or NAME is set 2339d71dd0cSThe j1939 authorsit is used as a receive filter, too. It will match the destination NAME or ADDR 2349d71dd0cSThe j1939 authorsof the incoming packet. The NAME filter will work only if appropriate Address 2359d71dd0cSThe j1939 authorsClaiming for this name was done on the CAN bus and registered/cached by the 2369d71dd0cSThe j1939 authorskernel. 2379d71dd0cSThe j1939 authors 238a39372c2SYegor YefremovOn the other hand ``connect(2)`` assigns the remote address, i.e. the destination 239a39372c2SYegor Yefremovaddress. The PGN from ``connect(2)`` is used as the default PGN when sending 2409d71dd0cSThe j1939 authorspackets. If ADDR or NAME is set it will be used as the default destination ADDR 241a39372c2SYegor Yefremovor NAME. Further a set ADDR or NAME during ``connect(2)`` is used as a receive 2429d71dd0cSThe j1939 authorsfilter. It will match the source NAME or ADDR of the incoming packet. 2439d71dd0cSThe j1939 authors 244a39372c2SYegor YefremovBoth ``write(2)`` and ``send(2)`` will send a packet with local address from ``bind(2)`` and the 245a39372c2SYegor Yefremovremote address from ``connect(2)``. Use ``sendto(2)`` to overwrite the destination 2469d71dd0cSThe j1939 authorsaddress. 2479d71dd0cSThe j1939 authors 248a39372c2SYegor YefremovIf ``can_addr.j1939.name`` is set (!= 0) the NAME is looked up by the kernel and 249a39372c2SYegor Yefremovthe corresponding ADDR is used. If ``can_addr.j1939.name`` is not set (== 0), 250a39372c2SYegor Yefremov``can_addr.j1939.addr`` is used. 2519d71dd0cSThe j1939 authors 2529d71dd0cSThe j1939 authorsWhen creating a socket, reasonable defaults are set. Some options can be 253a39372c2SYegor Yefremovmodified with ``setsockopt(2)`` & ``getsockopt(2)``. 2549d71dd0cSThe j1939 authors 2559d71dd0cSThe j1939 authorsRX path related options: 2569d71dd0cSThe j1939 authors 257a39372c2SYegor Yefremov- ``SO_J1939_FILTER`` - configure array of filters 258a39372c2SYegor Yefremov- ``SO_J1939_PROMISC`` - disable filters set by ``bind(2)`` and ``connect(2)`` 2599d71dd0cSThe j1939 authors 2609d71dd0cSThe j1939 authorsBy default no broadcast packets can be send or received. To enable sending or 261a39372c2SYegor Yefremovreceiving broadcast packets use the socket option ``SO_BROADCAST``: 2629d71dd0cSThe j1939 authors 2639d71dd0cSThe j1939 authors.. code-block:: C 2649d71dd0cSThe j1939 authors 2659d71dd0cSThe j1939 authors int value = 1; 2669d71dd0cSThe j1939 authors setsockopt(sock, SOL_SOCKET, SO_BROADCAST, &value, sizeof(value)); 2679d71dd0cSThe j1939 authors 2689d71dd0cSThe j1939 authorsThe following diagram illustrates the RX path: 2699d71dd0cSThe j1939 authors 2709d71dd0cSThe j1939 authors.. code:: 2719d71dd0cSThe j1939 authors 2729d71dd0cSThe j1939 authors +--------------------+ 2739d71dd0cSThe j1939 authors | incoming packet | 2749d71dd0cSThe j1939 authors +--------------------+ 2759d71dd0cSThe j1939 authors | 2769d71dd0cSThe j1939 authors V 2779d71dd0cSThe j1939 authors +--------------------+ 2789d71dd0cSThe j1939 authors | SO_J1939_PROMISC? | 2799d71dd0cSThe j1939 authors +--------------------+ 2809d71dd0cSThe j1939 authors | | 2819d71dd0cSThe j1939 authors no | | yes 2829d71dd0cSThe j1939 authors | | 2839d71dd0cSThe j1939 authors .---------' `---------. 2849d71dd0cSThe j1939 authors | | 2859d71dd0cSThe j1939 authors +---------------------------+ | 2869d71dd0cSThe j1939 authors | bind() + connect() + | | 2879d71dd0cSThe j1939 authors | SOCK_BROADCAST filter | | 2889d71dd0cSThe j1939 authors +---------------------------+ | 2899d71dd0cSThe j1939 authors | | 2909d71dd0cSThe j1939 authors |<---------------------' 2919d71dd0cSThe j1939 authors V 2929d71dd0cSThe j1939 authors +---------------------------+ 2939d71dd0cSThe j1939 authors | SO_J1939_FILTER | 2949d71dd0cSThe j1939 authors +---------------------------+ 2959d71dd0cSThe j1939 authors | 2969d71dd0cSThe j1939 authors V 2979d71dd0cSThe j1939 authors +---------------------------+ 2989d71dd0cSThe j1939 authors | socket recv() | 2999d71dd0cSThe j1939 authors +---------------------------+ 3009d71dd0cSThe j1939 authors 3019d71dd0cSThe j1939 authorsTX path related options: 302a39372c2SYegor Yefremov``SO_J1939_SEND_PRIO`` - change default send priority for the socket 3039d71dd0cSThe j1939 authors 3049d71dd0cSThe j1939 authorsMessage Flags during send() and Related System Calls 3059d71dd0cSThe j1939 authors^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 3069d71dd0cSThe j1939 authors 307a39372c2SYegor Yefremov``send(2)``, ``sendto(2)`` and ``sendmsg(2)`` take a 'flags' argument. Currently 3089d71dd0cSThe j1939 authorssupported flags are: 3099d71dd0cSThe j1939 authors 310a39372c2SYegor Yefremov* ``MSG_DONTWAIT``, i.e. non-blocking operation. 3119d71dd0cSThe j1939 authors 3129d71dd0cSThe j1939 authorsrecvmsg(2) 313c5f75a14SAdam Zerella^^^^^^^^^^ 3149d71dd0cSThe j1939 authors 315a39372c2SYegor YefremovIn most cases ``recvmsg(2)`` is needed if you want to extract more information than 316a39372c2SYegor Yefremov``recvfrom(2)`` can provide. For example package priority and timestamp. The 3179d71dd0cSThe j1939 authorsDestination Address, name and packet priority (if applicable) are attached to 318a39372c2SYegor Yefremovthe msghdr in the ``recvmsg(2)`` call. They can be extracted using ``cmsg(3)`` macros, 319a39372c2SYegor Yefremovwith ``cmsg_level == SOL_J1939 && cmsg_type == SCM_J1939_DEST_ADDR``, 320a39372c2SYegor Yefremov``SCM_J1939_DEST_NAME`` or ``SCM_J1939_PRIO``. The returned data is a ``uint8_t`` for 321a39372c2SYegor Yefremov``priority`` and ``dst_addr``, and ``uint64_t`` for ``dst_name``. 3229d71dd0cSThe j1939 authors 3239d71dd0cSThe j1939 authors.. code-block:: C 3249d71dd0cSThe j1939 authors 3259d71dd0cSThe j1939 authors uint8_t priority, dst_addr; 3269d71dd0cSThe j1939 authors uint64_t dst_name; 3279d71dd0cSThe j1939 authors 3289d71dd0cSThe j1939 authors for (cmsg = CMSG_FIRSTHDR(&msg); cmsg; cmsg = CMSG_NXTHDR(&msg, cmsg)) { 3299d71dd0cSThe j1939 authors switch (cmsg->cmsg_level) { 3309d71dd0cSThe j1939 authors case SOL_CAN_J1939: 3319d71dd0cSThe j1939 authors if (cmsg->cmsg_type == SCM_J1939_DEST_ADDR) 3329d71dd0cSThe j1939 authors dst_addr = *CMSG_DATA(cmsg); 3339d71dd0cSThe j1939 authors else if (cmsg->cmsg_type == SCM_J1939_DEST_NAME) 3349d71dd0cSThe j1939 authors memcpy(&dst_name, CMSG_DATA(cmsg), cmsg->cmsg_len - CMSG_LEN(0)); 3359d71dd0cSThe j1939 authors else if (cmsg->cmsg_type == SCM_J1939_PRIO) 3369d71dd0cSThe j1939 authors priority = *CMSG_DATA(cmsg); 3379d71dd0cSThe j1939 authors break; 3389d71dd0cSThe j1939 authors } 3399d71dd0cSThe j1939 authors } 3409d71dd0cSThe j1939 authors 3419d71dd0cSThe j1939 authorsDynamic Addressing 3429d71dd0cSThe j1939 authors------------------ 3439d71dd0cSThe j1939 authors 3449d71dd0cSThe j1939 authorsDistinction has to be made between using the claimed address and doing an 3459d71dd0cSThe j1939 authorsaddress claim. To use an already claimed address, one has to fill in the 346a39372c2SYegor Yefremov``j1939.name`` member and provide it to ``bind(2)``. If the name had claimed an address 3479d71dd0cSThe j1939 authorsearlier, all further messages being sent will use that address. And the 348a39372c2SYegor Yefremov``j1939.addr`` member will be ignored. 3499d71dd0cSThe j1939 authors 3509d71dd0cSThe j1939 authorsAn exception on this is PGN 0x0ee00. This is the "Address Claim/Cannot Claim 351a39372c2SYegor YefremovAddress" message and the kernel will use the ``j1939.addr`` member for that PGN if 3529d71dd0cSThe j1939 authorsnecessary. 3539d71dd0cSThe j1939 authors 3549d71dd0cSThe j1939 authorsTo claim an address following code example can be used: 3559d71dd0cSThe j1939 authors 3569d71dd0cSThe j1939 authors.. code-block:: C 3579d71dd0cSThe j1939 authors 3589d71dd0cSThe j1939 authors struct sockaddr_can baddr = { 3599d71dd0cSThe j1939 authors .can_family = AF_CAN, 3609d71dd0cSThe j1939 authors .can_addr.j1939 = { 3619d71dd0cSThe j1939 authors .name = name, 3629d71dd0cSThe j1939 authors .addr = J1939_IDLE_ADDR, 3639d71dd0cSThe j1939 authors .pgn = J1939_NO_PGN, /* to disable bind() rx filter for PGN */ 3649d71dd0cSThe j1939 authors }, 3659d71dd0cSThe j1939 authors .can_ifindex = if_nametoindex("can0"), 3669d71dd0cSThe j1939 authors }; 3679d71dd0cSThe j1939 authors 3689d71dd0cSThe j1939 authors bind(sock, (struct sockaddr *)&baddr, sizeof(baddr)); 3699d71dd0cSThe j1939 authors 3709d71dd0cSThe j1939 authors /* for Address Claiming broadcast must be allowed */ 3719d71dd0cSThe j1939 authors int value = 1; 3729d71dd0cSThe j1939 authors setsockopt(sock, SOL_SOCKET, SO_BROADCAST, &value, sizeof(value)); 3739d71dd0cSThe j1939 authors 3749d71dd0cSThe j1939 authors /* configured advanced RX filter with PGN needed for Address Claiming */ 3759d71dd0cSThe j1939 authors const struct j1939_filter filt[] = { 3769d71dd0cSThe j1939 authors { 3779d71dd0cSThe j1939 authors .pgn = J1939_PGN_ADDRESS_CLAIMED, 3789d71dd0cSThe j1939 authors .pgn_mask = J1939_PGN_PDU1_MAX, 3799d71dd0cSThe j1939 authors }, { 3808ac9d71dSMarc Kleine-Budde .pgn = J1939_PGN_REQUEST, 3819d71dd0cSThe j1939 authors .pgn_mask = J1939_PGN_PDU1_MAX, 3829d71dd0cSThe j1939 authors }, { 3839d71dd0cSThe j1939 authors .pgn = J1939_PGN_ADDRESS_COMMANDED, 3849d71dd0cSThe j1939 authors .pgn_mask = J1939_PGN_MAX, 3859d71dd0cSThe j1939 authors }, 3869d71dd0cSThe j1939 authors }; 3879d71dd0cSThe j1939 authors 3889d71dd0cSThe j1939 authors setsockopt(sock, SOL_CAN_J1939, SO_J1939_FILTER, &filt, sizeof(filt)); 3899d71dd0cSThe j1939 authors 3909d71dd0cSThe j1939 authors uint64_t dat = htole64(name); 3919d71dd0cSThe j1939 authors const struct sockaddr_can saddr = { 3929d71dd0cSThe j1939 authors .can_family = AF_CAN, 3939d71dd0cSThe j1939 authors .can_addr.j1939 = { 3949d71dd0cSThe j1939 authors .pgn = J1939_PGN_ADDRESS_CLAIMED, 3959d71dd0cSThe j1939 authors .addr = J1939_NO_ADDR, 3969d71dd0cSThe j1939 authors }, 3979d71dd0cSThe j1939 authors }; 3989d71dd0cSThe j1939 authors 3999d71dd0cSThe j1939 authors /* Afterwards do a sendto(2) with data set to the NAME (Little Endian). If the 4009d71dd0cSThe j1939 authors * NAME provided, does not match the j1939.name provided to bind(2), EPROTO 4019d71dd0cSThe j1939 authors * will be returned. 4029d71dd0cSThe j1939 authors */ 4039d71dd0cSThe j1939 authors sendto(sock, dat, sizeof(dat), 0, (const struct sockaddr *)&saddr, sizeof(saddr)); 4049d71dd0cSThe j1939 authors 4059d71dd0cSThe j1939 authorsIf no-one else contests the address claim within 250ms after transmission, the 4069d71dd0cSThe j1939 authorskernel marks the NAME-SA assignment as valid. The valid assignment will be kept 4079d71dd0cSThe j1939 authorsamong other valid NAME-SA assignments. From that point, any socket bound to the 4089d71dd0cSThe j1939 authorsNAME can send packets. 4099d71dd0cSThe j1939 authors 4109d71dd0cSThe j1939 authorsIf another ECU claims the address, the kernel will mark the NAME-SA expired. 4119d71dd0cSThe j1939 authorsNo socket bound to the NAME can send packets (other than address claims). To 412a39372c2SYegor Yefremovclaim another address, some socket bound to NAME, must ``bind(2)`` again, but with 413a39372c2SYegor Yefremovonly ``j1939.addr`` changed to the new SA, and must then send a valid address claim 4149d71dd0cSThe j1939 authorspacket. This restarts the state machine in the kernel (and any other 4159d71dd0cSThe j1939 authorsparticipant on the bus) for this NAME. 4169d71dd0cSThe j1939 authors 417a39372c2SYegor Yefremov``can-utils`` also include the ``j1939acd`` tool, so it can be used as code example or as 4189d71dd0cSThe j1939 authorsdefault Address Claiming daemon. 4199d71dd0cSThe j1939 authors 4209d71dd0cSThe j1939 authorsSend Examples 4219d71dd0cSThe j1939 authors------------- 4229d71dd0cSThe j1939 authors 4239d71dd0cSThe j1939 authorsStatic Addressing 4249d71dd0cSThe j1939 authors^^^^^^^^^^^^^^^^^ 4259d71dd0cSThe j1939 authors 4269d71dd0cSThe j1939 authorsThis example will send a PGN (0x12300) from SA 0x20 to DA 0x30. 4279d71dd0cSThe j1939 authors 4289d71dd0cSThe j1939 authorsBind: 4299d71dd0cSThe j1939 authors 4309d71dd0cSThe j1939 authors.. code-block:: C 4319d71dd0cSThe j1939 authors 4329d71dd0cSThe j1939 authors struct sockaddr_can baddr = { 4339d71dd0cSThe j1939 authors .can_family = AF_CAN, 4349d71dd0cSThe j1939 authors .can_addr.j1939 = { 4359d71dd0cSThe j1939 authors .name = J1939_NO_NAME, 4369d71dd0cSThe j1939 authors .addr = 0x20, 4379d71dd0cSThe j1939 authors .pgn = J1939_NO_PGN, 4389d71dd0cSThe j1939 authors }, 4399d71dd0cSThe j1939 authors .can_ifindex = if_nametoindex("can0"), 4409d71dd0cSThe j1939 authors }; 4419d71dd0cSThe j1939 authors 4429d71dd0cSThe j1939 authors bind(sock, (struct sockaddr *)&baddr, sizeof(baddr)); 4439d71dd0cSThe j1939 authors 444a39372c2SYegor YefremovNow, the socket 'sock' is bound to the SA 0x20. Since no ``connect(2)`` was called, 445a39372c2SYegor Yefremovat this point we can use only ``sendto(2)`` or ``sendmsg(2)``. 4469d71dd0cSThe j1939 authors 4479d71dd0cSThe j1939 authorsSend: 4489d71dd0cSThe j1939 authors 4499d71dd0cSThe j1939 authors.. code-block:: C 4509d71dd0cSThe j1939 authors 4519d71dd0cSThe j1939 authors const struct sockaddr_can saddr = { 4529d71dd0cSThe j1939 authors .can_family = AF_CAN, 4539d71dd0cSThe j1939 authors .can_addr.j1939 = { 4549d71dd0cSThe j1939 authors .name = J1939_NO_NAME; 455ea780d39SYegor Yefremov .addr = 0x30, 456ea780d39SYegor Yefremov .pgn = 0x12300, 4579d71dd0cSThe j1939 authors }, 4589d71dd0cSThe j1939 authors }; 4599d71dd0cSThe j1939 authors 4609d71dd0cSThe j1939 authors sendto(sock, dat, sizeof(dat), 0, (const struct sockaddr *)&saddr, sizeof(saddr)); 461