162306a36Sopenharmony_ci.. SPDX-License-Identifier: (GPL-2.0 OR MIT) 262306a36Sopenharmony_ci 362306a36Sopenharmony_ci=================== 462306a36Sopenharmony_ciJ1939 Documentation 562306a36Sopenharmony_ci=================== 662306a36Sopenharmony_ci 762306a36Sopenharmony_ciOverview / What Is J1939 862306a36Sopenharmony_ci======================== 962306a36Sopenharmony_ci 1062306a36Sopenharmony_ciSAE J1939 defines a higher layer protocol on CAN. It implements a more 1162306a36Sopenharmony_cisophisticated addressing scheme and extends the maximum packet size above 8 1262306a36Sopenharmony_cibytes. Several derived specifications exist, which differ from the original 1362306a36Sopenharmony_ciJ1939 on the application level, like MilCAN A, NMEA2000, and especially 1462306a36Sopenharmony_ciISO-11783 (ISOBUS). This last one specifies the so-called ETP (Extended 1562306a36Sopenharmony_ciTransport Protocol), which has been included in this implementation. This 1662306a36Sopenharmony_ciresults in a maximum packet size of ((2 ^ 24) - 1) * 7 bytes == 111 MiB. 1762306a36Sopenharmony_ci 1862306a36Sopenharmony_ciSpecifications used 1962306a36Sopenharmony_ci------------------- 2062306a36Sopenharmony_ci 2162306a36Sopenharmony_ci* SAE J1939-21 : data link layer 2262306a36Sopenharmony_ci* SAE J1939-81 : network management 2362306a36Sopenharmony_ci* ISO 11783-6 : Virtual Terminal (Extended Transport Protocol) 2462306a36Sopenharmony_ci 2562306a36Sopenharmony_ci.. _j1939-motivation: 2662306a36Sopenharmony_ci 2762306a36Sopenharmony_ciMotivation 2862306a36Sopenharmony_ci========== 2962306a36Sopenharmony_ci 3062306a36Sopenharmony_ciGiven the fact there's something like SocketCAN with an API similar to BSD 3162306a36Sopenharmony_cisockets, we found some reasons to justify a kernel implementation for the 3262306a36Sopenharmony_ciaddressing and transport methods used by J1939. 3362306a36Sopenharmony_ci 3462306a36Sopenharmony_ci* **Addressing:** when a process on an ECU communicates via J1939, it should 3562306a36Sopenharmony_ci not necessarily know its source address. Although, at least one process per 3662306a36Sopenharmony_ci ECU should know the source address. Other processes should be able to reuse 3762306a36Sopenharmony_ci that address. This way, address parameters for different processes 3862306a36Sopenharmony_ci cooperating for the same ECU, are not duplicated. This way of working is 3962306a36Sopenharmony_ci closely related to the UNIX concept, where programs do just one thing and do 4062306a36Sopenharmony_ci it well. 4162306a36Sopenharmony_ci 4262306a36Sopenharmony_ci* **Dynamic addressing:** Address Claiming in J1939 is time critical. 4362306a36Sopenharmony_ci Furthermore, data transport should be handled properly during the address 4462306a36Sopenharmony_ci negotiation. Putting this functionality in the kernel eliminates it as a 4562306a36Sopenharmony_ci requirement for _every_ user space process that communicates via J1939. This 4662306a36Sopenharmony_ci results in a consistent J1939 bus with proper addressing. 4762306a36Sopenharmony_ci 4862306a36Sopenharmony_ci* **Transport:** both TP & ETP reuse some PGNs to relay big packets over them. 4962306a36Sopenharmony_ci Different processes may thus use the same TP & ETP PGNs without actually 5062306a36Sopenharmony_ci knowing it. The individual TP & ETP sessions _must_ be serialized 5162306a36Sopenharmony_ci (synchronized) between different processes. The kernel solves this problem 5262306a36Sopenharmony_ci properly and eliminates the serialization (synchronization) as a requirement 5362306a36Sopenharmony_ci for _every_ user space process that communicates via J1939. 5462306a36Sopenharmony_ci 5562306a36Sopenharmony_ciJ1939 defines some other features (relaying, gateway, fast packet transport, 5662306a36Sopenharmony_ci...). In-kernel code for these would not contribute to protocol stability. 5762306a36Sopenharmony_ciTherefore, these parts are left to user space. 5862306a36Sopenharmony_ci 5962306a36Sopenharmony_ciThe J1939 sockets operate on CAN network devices (see SocketCAN). Any J1939 6062306a36Sopenharmony_ciuser space library operating on CAN raw sockets will still operate properly. 6162306a36Sopenharmony_ciSince such a library does not communicate with the in-kernel implementation, care 6262306a36Sopenharmony_cimust be taken that these two do not interfere. In practice, this means they 6362306a36Sopenharmony_cicannot share ECU addresses. A single ECU (or virtual ECU) address is used by 6462306a36Sopenharmony_cithe library exclusively, or by the in-kernel system exclusively. 6562306a36Sopenharmony_ci 6662306a36Sopenharmony_ciJ1939 concepts 6762306a36Sopenharmony_ci============== 6862306a36Sopenharmony_ci 6962306a36Sopenharmony_ciPGN 7062306a36Sopenharmony_ci--- 7162306a36Sopenharmony_ci 7262306a36Sopenharmony_ciThe J1939 protocol uses the 29-bit CAN identifier with the following structure: 7362306a36Sopenharmony_ci 7462306a36Sopenharmony_ci ============ ============== ==================== 7562306a36Sopenharmony_ci 29 bit CAN-ID 7662306a36Sopenharmony_ci -------------------------------------------------- 7762306a36Sopenharmony_ci Bit positions within the CAN-ID 7862306a36Sopenharmony_ci -------------------------------------------------- 7962306a36Sopenharmony_ci 28 ... 26 25 ... 8 7 ... 0 8062306a36Sopenharmony_ci ============ ============== ==================== 8162306a36Sopenharmony_ci Priority PGN SA (Source Address) 8262306a36Sopenharmony_ci ============ ============== ==================== 8362306a36Sopenharmony_ci 8462306a36Sopenharmony_ciThe PGN (Parameter Group Number) is a number to identify a packet. The PGN 8562306a36Sopenharmony_ciis composed as follows: 8662306a36Sopenharmony_ci 8762306a36Sopenharmony_ci ============ ============== ================= ================= 8862306a36Sopenharmony_ci PGN 8962306a36Sopenharmony_ci ------------------------------------------------------------------ 9062306a36Sopenharmony_ci Bit positions within the CAN-ID 9162306a36Sopenharmony_ci ------------------------------------------------------------------ 9262306a36Sopenharmony_ci 25 24 23 ... 16 15 ... 8 9362306a36Sopenharmony_ci ============ ============== ================= ================= 9462306a36Sopenharmony_ci R (Reserved) DP (Data Page) PF (PDU Format) PS (PDU Specific) 9562306a36Sopenharmony_ci ============ ============== ================= ================= 9662306a36Sopenharmony_ci 9762306a36Sopenharmony_ciIn J1939-21 distinction is made between PDU1 format (where PF < 240) and PDU2 9862306a36Sopenharmony_ciformat (where PF >= 240). Furthermore, when using the PDU2 format, the PS-field 9962306a36Sopenharmony_cicontains a so-called Group Extension, which is part of the PGN. When using PDU2 10062306a36Sopenharmony_ciformat, the Group Extension is set in the PS-field. 10162306a36Sopenharmony_ci 10262306a36Sopenharmony_ci ============== ======================== 10362306a36Sopenharmony_ci PDU1 Format (specific) (peer to peer) 10462306a36Sopenharmony_ci ---------------------------------------- 10562306a36Sopenharmony_ci Bit positions within the CAN-ID 10662306a36Sopenharmony_ci ---------------------------------------- 10762306a36Sopenharmony_ci 23 ... 16 15 ... 8 10862306a36Sopenharmony_ci ============== ======================== 10962306a36Sopenharmony_ci 00h ... EFh DA (Destination address) 11062306a36Sopenharmony_ci ============== ======================== 11162306a36Sopenharmony_ci 11262306a36Sopenharmony_ci ============== ======================== 11362306a36Sopenharmony_ci PDU2 Format (global) (broadcast) 11462306a36Sopenharmony_ci ---------------------------------------- 11562306a36Sopenharmony_ci Bit positions within the CAN-ID 11662306a36Sopenharmony_ci ---------------------------------------- 11762306a36Sopenharmony_ci 23 ... 16 15 ... 8 11862306a36Sopenharmony_ci ============== ======================== 11962306a36Sopenharmony_ci F0h ... FFh GE (Group Extension) 12062306a36Sopenharmony_ci ============== ======================== 12162306a36Sopenharmony_ci 12262306a36Sopenharmony_ciOn the other hand, when using PDU1 format, the PS-field contains a so-called 12362306a36Sopenharmony_ciDestination Address, which is _not_ part of the PGN. When communicating a PGN 12462306a36Sopenharmony_cifrom user space to kernel (or vice versa) and PDU2 format is used, the PS-field 12562306a36Sopenharmony_ciof the PGN shall be set to zero. The Destination Address shall be set 12662306a36Sopenharmony_cielsewhere. 12762306a36Sopenharmony_ci 12862306a36Sopenharmony_ciRegarding PGN mapping to 29-bit CAN identifier, the Destination Address shall 12962306a36Sopenharmony_cibe get/set from/to the appropriate bits of the identifier by the kernel. 13062306a36Sopenharmony_ci 13162306a36Sopenharmony_ci 13262306a36Sopenharmony_ciAddressing 13362306a36Sopenharmony_ci---------- 13462306a36Sopenharmony_ci 13562306a36Sopenharmony_ciBoth static and dynamic addressing methods can be used. 13662306a36Sopenharmony_ci 13762306a36Sopenharmony_ciFor static addresses, no extra checks are made by the kernel and provided 13862306a36Sopenharmony_ciaddresses are considered right. This responsibility is for the OEM or system 13962306a36Sopenharmony_ciintegrator. 14062306a36Sopenharmony_ci 14162306a36Sopenharmony_ciFor dynamic addressing, so-called Address Claiming, extra support is foreseen 14262306a36Sopenharmony_ciin the kernel. In J1939 any ECU is known by its 64-bit NAME. At the moment of 14362306a36Sopenharmony_cia successful address claim, the kernel keeps track of both NAME and source 14462306a36Sopenharmony_ciaddress being claimed. This serves as a base for filter schemes. By default, 14562306a36Sopenharmony_cipackets with a destination that is not locally will be rejected. 14662306a36Sopenharmony_ci 14762306a36Sopenharmony_ciMixed mode packets (from a static to a dynamic address or vice versa) are 14862306a36Sopenharmony_ciallowed. The BSD sockets define separate API calls for getting/setting the 14962306a36Sopenharmony_cilocal & remote address and are applicable for J1939 sockets. 15062306a36Sopenharmony_ci 15162306a36Sopenharmony_ciFiltering 15262306a36Sopenharmony_ci--------- 15362306a36Sopenharmony_ci 15462306a36Sopenharmony_ciJ1939 defines white list filters per socket that a user can set in order to 15562306a36Sopenharmony_cireceive a subset of the J1939 traffic. Filtering can be based on: 15662306a36Sopenharmony_ci 15762306a36Sopenharmony_ci* SA 15862306a36Sopenharmony_ci* SOURCE_NAME 15962306a36Sopenharmony_ci* PGN 16062306a36Sopenharmony_ci 16162306a36Sopenharmony_ciWhen multiple filters are in place for a single socket, and a packet comes in 16262306a36Sopenharmony_cithat matches several of those filters, the packet is only received once for 16362306a36Sopenharmony_cithat socket. 16462306a36Sopenharmony_ci 16562306a36Sopenharmony_ciHow to Use J1939 16662306a36Sopenharmony_ci================ 16762306a36Sopenharmony_ci 16862306a36Sopenharmony_ciAPI Calls 16962306a36Sopenharmony_ci--------- 17062306a36Sopenharmony_ci 17162306a36Sopenharmony_ciOn CAN, you first need to open a socket for communicating over a CAN network. 17262306a36Sopenharmony_ciTo use J1939, ``#include <linux/can/j1939.h>``. From there, ``<linux/can.h>`` will be 17362306a36Sopenharmony_ciincluded too. To open a socket, use: 17462306a36Sopenharmony_ci 17562306a36Sopenharmony_ci.. code-block:: C 17662306a36Sopenharmony_ci 17762306a36Sopenharmony_ci s = socket(PF_CAN, SOCK_DGRAM, CAN_J1939); 17862306a36Sopenharmony_ci 17962306a36Sopenharmony_ciJ1939 does use ``SOCK_DGRAM`` sockets. In the J1939 specification, connections are 18062306a36Sopenharmony_cimentioned in the context of transport protocol sessions. These still deliver 18162306a36Sopenharmony_cipackets to the other end (using several CAN packets). ``SOCK_STREAM`` is not 18262306a36Sopenharmony_cisupported. 18362306a36Sopenharmony_ci 18462306a36Sopenharmony_ciAfter the successful creation of the socket, you would normally use the ``bind(2)`` 18562306a36Sopenharmony_ciand/or ``connect(2)`` system call to bind the socket to a CAN interface. After 18662306a36Sopenharmony_cibinding and/or connecting the socket, you can ``read(2)`` and ``write(2)`` from/to the 18762306a36Sopenharmony_cisocket or use ``send(2)``, ``sendto(2)``, ``sendmsg(2)`` and the ``recv*()`` counterpart 18862306a36Sopenharmony_cioperations on the socket as usual. There are also J1939 specific socket options 18962306a36Sopenharmony_cidescribed below. 19062306a36Sopenharmony_ci 19162306a36Sopenharmony_ciIn order to send data, a ``bind(2)`` must have been successful. ``bind(2)`` assigns a 19262306a36Sopenharmony_cilocal address to a socket. 19362306a36Sopenharmony_ci 19462306a36Sopenharmony_ciDifferent from CAN is that the payload data is just the data that get sends, 19562306a36Sopenharmony_ciwithout its header info. The header info is derived from the sockaddr supplied 19662306a36Sopenharmony_cito ``bind(2)``, ``connect(2)``, ``sendto(2)`` and ``recvfrom(2)``. A ``write(2)`` with size 4 will 19762306a36Sopenharmony_ciresult in a packet with 4 bytes. 19862306a36Sopenharmony_ci 19962306a36Sopenharmony_ciThe sockaddr structure has extensions for use with J1939 as specified below: 20062306a36Sopenharmony_ci 20162306a36Sopenharmony_ci.. code-block:: C 20262306a36Sopenharmony_ci 20362306a36Sopenharmony_ci struct sockaddr_can { 20462306a36Sopenharmony_ci sa_family_t can_family; 20562306a36Sopenharmony_ci int can_ifindex; 20662306a36Sopenharmony_ci union { 20762306a36Sopenharmony_ci struct { 20862306a36Sopenharmony_ci __u64 name; 20962306a36Sopenharmony_ci /* pgn: 21062306a36Sopenharmony_ci * 8 bit: PS in PDU2 case, else 0 21162306a36Sopenharmony_ci * 8 bit: PF 21262306a36Sopenharmony_ci * 1 bit: DP 21362306a36Sopenharmony_ci * 1 bit: reserved 21462306a36Sopenharmony_ci */ 21562306a36Sopenharmony_ci __u32 pgn; 21662306a36Sopenharmony_ci __u8 addr; 21762306a36Sopenharmony_ci } j1939; 21862306a36Sopenharmony_ci } can_addr; 21962306a36Sopenharmony_ci } 22062306a36Sopenharmony_ci 22162306a36Sopenharmony_ci``can_family`` & ``can_ifindex`` serve the same purpose as for other SocketCAN sockets. 22262306a36Sopenharmony_ci 22362306a36Sopenharmony_ci``can_addr.j1939.pgn`` specifies the PGN (max 0x3ffff). Individual bits are 22462306a36Sopenharmony_cispecified above. 22562306a36Sopenharmony_ci 22662306a36Sopenharmony_ci``can_addr.j1939.name`` contains the 64-bit J1939 NAME. 22762306a36Sopenharmony_ci 22862306a36Sopenharmony_ci``can_addr.j1939.addr`` contains the address. 22962306a36Sopenharmony_ci 23062306a36Sopenharmony_ciThe ``bind(2)`` system call assigns the local address, i.e. the source address when 23162306a36Sopenharmony_cisending packages. If a PGN during ``bind(2)`` is set, it's used as a RX filter. 23262306a36Sopenharmony_ciI.e. only packets with a matching PGN are received. If an ADDR or NAME is set 23362306a36Sopenharmony_ciit is used as a receive filter, too. It will match the destination NAME or ADDR 23462306a36Sopenharmony_ciof the incoming packet. The NAME filter will work only if appropriate Address 23562306a36Sopenharmony_ciClaiming for this name was done on the CAN bus and registered/cached by the 23662306a36Sopenharmony_cikernel. 23762306a36Sopenharmony_ci 23862306a36Sopenharmony_ciOn the other hand ``connect(2)`` assigns the remote address, i.e. the destination 23962306a36Sopenharmony_ciaddress. The PGN from ``connect(2)`` is used as the default PGN when sending 24062306a36Sopenharmony_cipackets. If ADDR or NAME is set it will be used as the default destination ADDR 24162306a36Sopenharmony_cior NAME. Further a set ADDR or NAME during ``connect(2)`` is used as a receive 24262306a36Sopenharmony_cifilter. It will match the source NAME or ADDR of the incoming packet. 24362306a36Sopenharmony_ci 24462306a36Sopenharmony_ciBoth ``write(2)`` and ``send(2)`` will send a packet with local address from ``bind(2)`` and the 24562306a36Sopenharmony_ciremote address from ``connect(2)``. Use ``sendto(2)`` to overwrite the destination 24662306a36Sopenharmony_ciaddress. 24762306a36Sopenharmony_ci 24862306a36Sopenharmony_ciIf ``can_addr.j1939.name`` is set (!= 0) the NAME is looked up by the kernel and 24962306a36Sopenharmony_cithe corresponding ADDR is used. If ``can_addr.j1939.name`` is not set (== 0), 25062306a36Sopenharmony_ci``can_addr.j1939.addr`` is used. 25162306a36Sopenharmony_ci 25262306a36Sopenharmony_ciWhen creating a socket, reasonable defaults are set. Some options can be 25362306a36Sopenharmony_cimodified with ``setsockopt(2)`` & ``getsockopt(2)``. 25462306a36Sopenharmony_ci 25562306a36Sopenharmony_ciRX path related options: 25662306a36Sopenharmony_ci 25762306a36Sopenharmony_ci- ``SO_J1939_FILTER`` - configure array of filters 25862306a36Sopenharmony_ci- ``SO_J1939_PROMISC`` - disable filters set by ``bind(2)`` and ``connect(2)`` 25962306a36Sopenharmony_ci 26062306a36Sopenharmony_ciBy default no broadcast packets can be send or received. To enable sending or 26162306a36Sopenharmony_cireceiving broadcast packets use the socket option ``SO_BROADCAST``: 26262306a36Sopenharmony_ci 26362306a36Sopenharmony_ci.. code-block:: C 26462306a36Sopenharmony_ci 26562306a36Sopenharmony_ci int value = 1; 26662306a36Sopenharmony_ci setsockopt(sock, SOL_SOCKET, SO_BROADCAST, &value, sizeof(value)); 26762306a36Sopenharmony_ci 26862306a36Sopenharmony_ciThe following diagram illustrates the RX path: 26962306a36Sopenharmony_ci 27062306a36Sopenharmony_ci.. code:: 27162306a36Sopenharmony_ci 27262306a36Sopenharmony_ci +--------------------+ 27362306a36Sopenharmony_ci | incoming packet | 27462306a36Sopenharmony_ci +--------------------+ 27562306a36Sopenharmony_ci | 27662306a36Sopenharmony_ci V 27762306a36Sopenharmony_ci +--------------------+ 27862306a36Sopenharmony_ci | SO_J1939_PROMISC? | 27962306a36Sopenharmony_ci +--------------------+ 28062306a36Sopenharmony_ci | | 28162306a36Sopenharmony_ci no | | yes 28262306a36Sopenharmony_ci | | 28362306a36Sopenharmony_ci .---------' `---------. 28462306a36Sopenharmony_ci | | 28562306a36Sopenharmony_ci +---------------------------+ | 28662306a36Sopenharmony_ci | bind() + connect() + | | 28762306a36Sopenharmony_ci | SOCK_BROADCAST filter | | 28862306a36Sopenharmony_ci +---------------------------+ | 28962306a36Sopenharmony_ci | | 29062306a36Sopenharmony_ci |<---------------------' 29162306a36Sopenharmony_ci V 29262306a36Sopenharmony_ci +---------------------------+ 29362306a36Sopenharmony_ci | SO_J1939_FILTER | 29462306a36Sopenharmony_ci +---------------------------+ 29562306a36Sopenharmony_ci | 29662306a36Sopenharmony_ci V 29762306a36Sopenharmony_ci +---------------------------+ 29862306a36Sopenharmony_ci | socket recv() | 29962306a36Sopenharmony_ci +---------------------------+ 30062306a36Sopenharmony_ci 30162306a36Sopenharmony_ciTX path related options: 30262306a36Sopenharmony_ci``SO_J1939_SEND_PRIO`` - change default send priority for the socket 30362306a36Sopenharmony_ci 30462306a36Sopenharmony_ciMessage Flags during send() and Related System Calls 30562306a36Sopenharmony_ci^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 30662306a36Sopenharmony_ci 30762306a36Sopenharmony_ci``send(2)``, ``sendto(2)`` and ``sendmsg(2)`` take a 'flags' argument. Currently 30862306a36Sopenharmony_cisupported flags are: 30962306a36Sopenharmony_ci 31062306a36Sopenharmony_ci* ``MSG_DONTWAIT``, i.e. non-blocking operation. 31162306a36Sopenharmony_ci 31262306a36Sopenharmony_cirecvmsg(2) 31362306a36Sopenharmony_ci^^^^^^^^^^ 31462306a36Sopenharmony_ci 31562306a36Sopenharmony_ciIn most cases ``recvmsg(2)`` is needed if you want to extract more information than 31662306a36Sopenharmony_ci``recvfrom(2)`` can provide. For example package priority and timestamp. The 31762306a36Sopenharmony_ciDestination Address, name and packet priority (if applicable) are attached to 31862306a36Sopenharmony_cithe msghdr in the ``recvmsg(2)`` call. They can be extracted using ``cmsg(3)`` macros, 31962306a36Sopenharmony_ciwith ``cmsg_level == SOL_J1939 && cmsg_type == SCM_J1939_DEST_ADDR``, 32062306a36Sopenharmony_ci``SCM_J1939_DEST_NAME`` or ``SCM_J1939_PRIO``. The returned data is a ``uint8_t`` for 32162306a36Sopenharmony_ci``priority`` and ``dst_addr``, and ``uint64_t`` for ``dst_name``. 32262306a36Sopenharmony_ci 32362306a36Sopenharmony_ci.. code-block:: C 32462306a36Sopenharmony_ci 32562306a36Sopenharmony_ci uint8_t priority, dst_addr; 32662306a36Sopenharmony_ci uint64_t dst_name; 32762306a36Sopenharmony_ci 32862306a36Sopenharmony_ci for (cmsg = CMSG_FIRSTHDR(&msg); cmsg; cmsg = CMSG_NXTHDR(&msg, cmsg)) { 32962306a36Sopenharmony_ci switch (cmsg->cmsg_level) { 33062306a36Sopenharmony_ci case SOL_CAN_J1939: 33162306a36Sopenharmony_ci if (cmsg->cmsg_type == SCM_J1939_DEST_ADDR) 33262306a36Sopenharmony_ci dst_addr = *CMSG_DATA(cmsg); 33362306a36Sopenharmony_ci else if (cmsg->cmsg_type == SCM_J1939_DEST_NAME) 33462306a36Sopenharmony_ci memcpy(&dst_name, CMSG_DATA(cmsg), cmsg->cmsg_len - CMSG_LEN(0)); 33562306a36Sopenharmony_ci else if (cmsg->cmsg_type == SCM_J1939_PRIO) 33662306a36Sopenharmony_ci priority = *CMSG_DATA(cmsg); 33762306a36Sopenharmony_ci break; 33862306a36Sopenharmony_ci } 33962306a36Sopenharmony_ci } 34062306a36Sopenharmony_ci 34162306a36Sopenharmony_ciDynamic Addressing 34262306a36Sopenharmony_ci------------------ 34362306a36Sopenharmony_ci 34462306a36Sopenharmony_ciDistinction has to be made between using the claimed address and doing an 34562306a36Sopenharmony_ciaddress claim. To use an already claimed address, one has to fill in the 34662306a36Sopenharmony_ci``j1939.name`` member and provide it to ``bind(2)``. If the name had claimed an address 34762306a36Sopenharmony_ciearlier, all further messages being sent will use that address. And the 34862306a36Sopenharmony_ci``j1939.addr`` member will be ignored. 34962306a36Sopenharmony_ci 35062306a36Sopenharmony_ciAn exception on this is PGN 0x0ee00. This is the "Address Claim/Cannot Claim 35162306a36Sopenharmony_ciAddress" message and the kernel will use the ``j1939.addr`` member for that PGN if 35262306a36Sopenharmony_cinecessary. 35362306a36Sopenharmony_ci 35462306a36Sopenharmony_ciTo claim an address following code example can be used: 35562306a36Sopenharmony_ci 35662306a36Sopenharmony_ci.. code-block:: C 35762306a36Sopenharmony_ci 35862306a36Sopenharmony_ci struct sockaddr_can baddr = { 35962306a36Sopenharmony_ci .can_family = AF_CAN, 36062306a36Sopenharmony_ci .can_addr.j1939 = { 36162306a36Sopenharmony_ci .name = name, 36262306a36Sopenharmony_ci .addr = J1939_IDLE_ADDR, 36362306a36Sopenharmony_ci .pgn = J1939_NO_PGN, /* to disable bind() rx filter for PGN */ 36462306a36Sopenharmony_ci }, 36562306a36Sopenharmony_ci .can_ifindex = if_nametoindex("can0"), 36662306a36Sopenharmony_ci }; 36762306a36Sopenharmony_ci 36862306a36Sopenharmony_ci bind(sock, (struct sockaddr *)&baddr, sizeof(baddr)); 36962306a36Sopenharmony_ci 37062306a36Sopenharmony_ci /* for Address Claiming broadcast must be allowed */ 37162306a36Sopenharmony_ci int value = 1; 37262306a36Sopenharmony_ci setsockopt(sock, SOL_SOCKET, SO_BROADCAST, &value, sizeof(value)); 37362306a36Sopenharmony_ci 37462306a36Sopenharmony_ci /* configured advanced RX filter with PGN needed for Address Claiming */ 37562306a36Sopenharmony_ci const struct j1939_filter filt[] = { 37662306a36Sopenharmony_ci { 37762306a36Sopenharmony_ci .pgn = J1939_PGN_ADDRESS_CLAIMED, 37862306a36Sopenharmony_ci .pgn_mask = J1939_PGN_PDU1_MAX, 37962306a36Sopenharmony_ci }, { 38062306a36Sopenharmony_ci .pgn = J1939_PGN_REQUEST, 38162306a36Sopenharmony_ci .pgn_mask = J1939_PGN_PDU1_MAX, 38262306a36Sopenharmony_ci }, { 38362306a36Sopenharmony_ci .pgn = J1939_PGN_ADDRESS_COMMANDED, 38462306a36Sopenharmony_ci .pgn_mask = J1939_PGN_MAX, 38562306a36Sopenharmony_ci }, 38662306a36Sopenharmony_ci }; 38762306a36Sopenharmony_ci 38862306a36Sopenharmony_ci setsockopt(sock, SOL_CAN_J1939, SO_J1939_FILTER, &filt, sizeof(filt)); 38962306a36Sopenharmony_ci 39062306a36Sopenharmony_ci uint64_t dat = htole64(name); 39162306a36Sopenharmony_ci const struct sockaddr_can saddr = { 39262306a36Sopenharmony_ci .can_family = AF_CAN, 39362306a36Sopenharmony_ci .can_addr.j1939 = { 39462306a36Sopenharmony_ci .pgn = J1939_PGN_ADDRESS_CLAIMED, 39562306a36Sopenharmony_ci .addr = J1939_NO_ADDR, 39662306a36Sopenharmony_ci }, 39762306a36Sopenharmony_ci }; 39862306a36Sopenharmony_ci 39962306a36Sopenharmony_ci /* Afterwards do a sendto(2) with data set to the NAME (Little Endian). If the 40062306a36Sopenharmony_ci * NAME provided, does not match the j1939.name provided to bind(2), EPROTO 40162306a36Sopenharmony_ci * will be returned. 40262306a36Sopenharmony_ci */ 40362306a36Sopenharmony_ci sendto(sock, dat, sizeof(dat), 0, (const struct sockaddr *)&saddr, sizeof(saddr)); 40462306a36Sopenharmony_ci 40562306a36Sopenharmony_ciIf no-one else contests the address claim within 250ms after transmission, the 40662306a36Sopenharmony_cikernel marks the NAME-SA assignment as valid. The valid assignment will be kept 40762306a36Sopenharmony_ciamong other valid NAME-SA assignments. From that point, any socket bound to the 40862306a36Sopenharmony_ciNAME can send packets. 40962306a36Sopenharmony_ci 41062306a36Sopenharmony_ciIf another ECU claims the address, the kernel will mark the NAME-SA expired. 41162306a36Sopenharmony_ciNo socket bound to the NAME can send packets (other than address claims). To 41262306a36Sopenharmony_ciclaim another address, some socket bound to NAME, must ``bind(2)`` again, but with 41362306a36Sopenharmony_cionly ``j1939.addr`` changed to the new SA, and must then send a valid address claim 41462306a36Sopenharmony_cipacket. This restarts the state machine in the kernel (and any other 41562306a36Sopenharmony_ciparticipant on the bus) for this NAME. 41662306a36Sopenharmony_ci 41762306a36Sopenharmony_ci``can-utils`` also include the ``j1939acd`` tool, so it can be used as code example or as 41862306a36Sopenharmony_cidefault Address Claiming daemon. 41962306a36Sopenharmony_ci 42062306a36Sopenharmony_ciSend Examples 42162306a36Sopenharmony_ci------------- 42262306a36Sopenharmony_ci 42362306a36Sopenharmony_ciStatic Addressing 42462306a36Sopenharmony_ci^^^^^^^^^^^^^^^^^ 42562306a36Sopenharmony_ci 42662306a36Sopenharmony_ciThis example will send a PGN (0x12300) from SA 0x20 to DA 0x30. 42762306a36Sopenharmony_ci 42862306a36Sopenharmony_ciBind: 42962306a36Sopenharmony_ci 43062306a36Sopenharmony_ci.. code-block:: C 43162306a36Sopenharmony_ci 43262306a36Sopenharmony_ci struct sockaddr_can baddr = { 43362306a36Sopenharmony_ci .can_family = AF_CAN, 43462306a36Sopenharmony_ci .can_addr.j1939 = { 43562306a36Sopenharmony_ci .name = J1939_NO_NAME, 43662306a36Sopenharmony_ci .addr = 0x20, 43762306a36Sopenharmony_ci .pgn = J1939_NO_PGN, 43862306a36Sopenharmony_ci }, 43962306a36Sopenharmony_ci .can_ifindex = if_nametoindex("can0"), 44062306a36Sopenharmony_ci }; 44162306a36Sopenharmony_ci 44262306a36Sopenharmony_ci bind(sock, (struct sockaddr *)&baddr, sizeof(baddr)); 44362306a36Sopenharmony_ci 44462306a36Sopenharmony_ciNow, the socket 'sock' is bound to the SA 0x20. Since no ``connect(2)`` was called, 44562306a36Sopenharmony_ciat this point we can use only ``sendto(2)`` or ``sendmsg(2)``. 44662306a36Sopenharmony_ci 44762306a36Sopenharmony_ciSend: 44862306a36Sopenharmony_ci 44962306a36Sopenharmony_ci.. code-block:: C 45062306a36Sopenharmony_ci 45162306a36Sopenharmony_ci const struct sockaddr_can saddr = { 45262306a36Sopenharmony_ci .can_family = AF_CAN, 45362306a36Sopenharmony_ci .can_addr.j1939 = { 45462306a36Sopenharmony_ci .name = J1939_NO_NAME; 45562306a36Sopenharmony_ci .addr = 0x30, 45662306a36Sopenharmony_ci .pgn = 0x12300, 45762306a36Sopenharmony_ci }, 45862306a36Sopenharmony_ci }; 45962306a36Sopenharmony_ci 46062306a36Sopenharmony_ci sendto(sock, dat, sizeof(dat), 0, (const struct sockaddr *)&saddr, sizeof(saddr)); 461