1*4882a593Smuzhiyun.. SPDX-License-Identifier: (GPL-2.0 OR MIT) 2*4882a593Smuzhiyun 3*4882a593Smuzhiyun=================== 4*4882a593SmuzhiyunJ1939 Documentation 5*4882a593Smuzhiyun=================== 6*4882a593Smuzhiyun 7*4882a593SmuzhiyunOverview / What Is J1939 8*4882a593Smuzhiyun======================== 9*4882a593Smuzhiyun 10*4882a593SmuzhiyunSAE J1939 defines a higher layer protocol on CAN. It implements a more 11*4882a593Smuzhiyunsophisticated addressing scheme and extends the maximum packet size above 8 12*4882a593Smuzhiyunbytes. Several derived specifications exist, which differ from the original 13*4882a593SmuzhiyunJ1939 on the application level, like MilCAN A, NMEA2000, and especially 14*4882a593SmuzhiyunISO-11783 (ISOBUS). This last one specifies the so-called ETP (Extended 15*4882a593SmuzhiyunTransport Protocol), which has been included in this implementation. This 16*4882a593Smuzhiyunresults in a maximum packet size of ((2 ^ 24) - 1) * 7 bytes == 111 MiB. 17*4882a593Smuzhiyun 18*4882a593SmuzhiyunSpecifications used 19*4882a593Smuzhiyun------------------- 20*4882a593Smuzhiyun 21*4882a593Smuzhiyun* SAE J1939-21 : data link layer 22*4882a593Smuzhiyun* SAE J1939-81 : network management 23*4882a593Smuzhiyun* ISO 11783-6 : Virtual Terminal (Extended Transport Protocol) 24*4882a593Smuzhiyun 25*4882a593Smuzhiyun.. _j1939-motivation: 26*4882a593Smuzhiyun 27*4882a593SmuzhiyunMotivation 28*4882a593Smuzhiyun========== 29*4882a593Smuzhiyun 30*4882a593SmuzhiyunGiven the fact there's something like SocketCAN with an API similar to BSD 31*4882a593Smuzhiyunsockets, we found some reasons to justify a kernel implementation for the 32*4882a593Smuzhiyunaddressing and transport methods used by J1939. 33*4882a593Smuzhiyun 34*4882a593Smuzhiyun* **Addressing:** when a process on an ECU communicates via J1939, it should 35*4882a593Smuzhiyun not necessarily know its source address. Although, at least one process per 36*4882a593Smuzhiyun ECU should know the source address. Other processes should be able to reuse 37*4882a593Smuzhiyun that address. This way, address parameters for different processes 38*4882a593Smuzhiyun cooperating for the same ECU, are not duplicated. This way of working is 39*4882a593Smuzhiyun closely related to the UNIX concept, where programs do just one thing and do 40*4882a593Smuzhiyun it well. 41*4882a593Smuzhiyun 42*4882a593Smuzhiyun* **Dynamic addressing:** Address Claiming in J1939 is time critical. 43*4882a593Smuzhiyun Furthermore, data transport should be handled properly during the address 44*4882a593Smuzhiyun negotiation. Putting this functionality in the kernel eliminates it as a 45*4882a593Smuzhiyun requirement for _every_ user space process that communicates via J1939. This 46*4882a593Smuzhiyun results in a consistent J1939 bus with proper addressing. 47*4882a593Smuzhiyun 48*4882a593Smuzhiyun* **Transport:** both TP & ETP reuse some PGNs to relay big packets over them. 49*4882a593Smuzhiyun Different processes may thus use the same TP & ETP PGNs without actually 50*4882a593Smuzhiyun knowing it. The individual TP & ETP sessions _must_ be serialized 51*4882a593Smuzhiyun (synchronized) between different processes. The kernel solves this problem 52*4882a593Smuzhiyun properly and eliminates the serialization (synchronization) as a requirement 53*4882a593Smuzhiyun for _every_ user space process that communicates via J1939. 54*4882a593Smuzhiyun 55*4882a593SmuzhiyunJ1939 defines some other features (relaying, gateway, fast packet transport, 56*4882a593Smuzhiyun...). In-kernel code for these would not contribute to protocol stability. 57*4882a593SmuzhiyunTherefore, these parts are left to user space. 58*4882a593Smuzhiyun 59*4882a593SmuzhiyunThe J1939 sockets operate on CAN network devices (see SocketCAN). Any J1939 60*4882a593Smuzhiyunuser space library operating on CAN raw sockets will still operate properly. 61*4882a593SmuzhiyunSince such a library does not communicate with the in-kernel implementation, care 62*4882a593Smuzhiyunmust be taken that these two do not interfere. In practice, this means they 63*4882a593Smuzhiyuncannot share ECU addresses. A single ECU (or virtual ECU) address is used by 64*4882a593Smuzhiyunthe library exclusively, or by the in-kernel system exclusively. 65*4882a593Smuzhiyun 66*4882a593SmuzhiyunJ1939 concepts 67*4882a593Smuzhiyun============== 68*4882a593Smuzhiyun 69*4882a593SmuzhiyunPGN 70*4882a593Smuzhiyun--- 71*4882a593Smuzhiyun 72*4882a593SmuzhiyunThe PGN (Parameter Group Number) is a number to identify a packet. The PGN 73*4882a593Smuzhiyunis composed as follows: 74*4882a593Smuzhiyun1 bit : Reserved Bit 75*4882a593Smuzhiyun1 bit : Data Page 76*4882a593Smuzhiyun8 bits : PF (PDU Format) 77*4882a593Smuzhiyun8 bits : PS (PDU Specific) 78*4882a593Smuzhiyun 79*4882a593SmuzhiyunIn J1939-21 distinction is made between PDU1 format (where PF < 240) and PDU2 80*4882a593Smuzhiyunformat (where PF >= 240). Furthermore, when using the PDU2 format, the PS-field 81*4882a593Smuzhiyuncontains a so-called Group Extension, which is part of the PGN. When using PDU2 82*4882a593Smuzhiyunformat, the Group Extension is set in the PS-field. 83*4882a593Smuzhiyun 84*4882a593SmuzhiyunOn the other hand, when using PDU1 format, the PS-field contains a so-called 85*4882a593SmuzhiyunDestination Address, which is _not_ part of the PGN. When communicating a PGN 86*4882a593Smuzhiyunfrom user space to kernel (or vice versa) and PDU2 format is used, the PS-field 87*4882a593Smuzhiyunof the PGN shall be set to zero. The Destination Address shall be set 88*4882a593Smuzhiyunelsewhere. 89*4882a593Smuzhiyun 90*4882a593SmuzhiyunRegarding PGN mapping to 29-bit CAN identifier, the Destination Address shall 91*4882a593Smuzhiyunbe get/set from/to the appropriate bits of the identifier by the kernel. 92*4882a593Smuzhiyun 93*4882a593Smuzhiyun 94*4882a593SmuzhiyunAddressing 95*4882a593Smuzhiyun---------- 96*4882a593Smuzhiyun 97*4882a593SmuzhiyunBoth static and dynamic addressing methods can be used. 98*4882a593Smuzhiyun 99*4882a593SmuzhiyunFor static addresses, no extra checks are made by the kernel and provided 100*4882a593Smuzhiyunaddresses are considered right. This responsibility is for the OEM or system 101*4882a593Smuzhiyunintegrator. 102*4882a593Smuzhiyun 103*4882a593SmuzhiyunFor dynamic addressing, so-called Address Claiming, extra support is foreseen 104*4882a593Smuzhiyunin the kernel. In J1939 any ECU is known by its 64-bit NAME. At the moment of 105*4882a593Smuzhiyuna successful address claim, the kernel keeps track of both NAME and source 106*4882a593Smuzhiyunaddress being claimed. This serves as a base for filter schemes. By default, 107*4882a593Smuzhiyunpackets with a destination that is not locally will be rejected. 108*4882a593Smuzhiyun 109*4882a593SmuzhiyunMixed mode packets (from a static to a dynamic address or vice versa) are 110*4882a593Smuzhiyunallowed. The BSD sockets define separate API calls for getting/setting the 111*4882a593Smuzhiyunlocal & remote address and are applicable for J1939 sockets. 112*4882a593Smuzhiyun 113*4882a593SmuzhiyunFiltering 114*4882a593Smuzhiyun--------- 115*4882a593Smuzhiyun 116*4882a593SmuzhiyunJ1939 defines white list filters per socket that a user can set in order to 117*4882a593Smuzhiyunreceive a subset of the J1939 traffic. Filtering can be based on: 118*4882a593Smuzhiyun 119*4882a593Smuzhiyun* SA 120*4882a593Smuzhiyun* SOURCE_NAME 121*4882a593Smuzhiyun* PGN 122*4882a593Smuzhiyun 123*4882a593SmuzhiyunWhen multiple filters are in place for a single socket, and a packet comes in 124*4882a593Smuzhiyunthat matches several of those filters, the packet is only received once for 125*4882a593Smuzhiyunthat socket. 126*4882a593Smuzhiyun 127*4882a593SmuzhiyunHow to Use J1939 128*4882a593Smuzhiyun================ 129*4882a593Smuzhiyun 130*4882a593SmuzhiyunAPI Calls 131*4882a593Smuzhiyun--------- 132*4882a593Smuzhiyun 133*4882a593SmuzhiyunOn CAN, you first need to open a socket for communicating over a CAN network. 134*4882a593SmuzhiyunTo use J1939, ``#include <linux/can/j1939.h>``. From there, ``<linux/can.h>`` will be 135*4882a593Smuzhiyunincluded too. To open a socket, use: 136*4882a593Smuzhiyun 137*4882a593Smuzhiyun.. code-block:: C 138*4882a593Smuzhiyun 139*4882a593Smuzhiyun s = socket(PF_CAN, SOCK_DGRAM, CAN_J1939); 140*4882a593Smuzhiyun 141*4882a593SmuzhiyunJ1939 does use ``SOCK_DGRAM`` sockets. In the J1939 specification, connections are 142*4882a593Smuzhiyunmentioned in the context of transport protocol sessions. These still deliver 143*4882a593Smuzhiyunpackets to the other end (using several CAN packets). ``SOCK_STREAM`` is not 144*4882a593Smuzhiyunsupported. 145*4882a593Smuzhiyun 146*4882a593SmuzhiyunAfter the successful creation of the socket, you would normally use the ``bind(2)`` 147*4882a593Smuzhiyunand/or ``connect(2)`` system call to bind the socket to a CAN interface. After 148*4882a593Smuzhiyunbinding and/or connecting the socket, you can ``read(2)`` and ``write(2)`` from/to the 149*4882a593Smuzhiyunsocket or use ``send(2)``, ``sendto(2)``, ``sendmsg(2)`` and the ``recv*()`` counterpart 150*4882a593Smuzhiyunoperations on the socket as usual. There are also J1939 specific socket options 151*4882a593Smuzhiyundescribed below. 152*4882a593Smuzhiyun 153*4882a593SmuzhiyunIn order to send data, a ``bind(2)`` must have been successful. ``bind(2)`` assigns a 154*4882a593Smuzhiyunlocal address to a socket. 155*4882a593Smuzhiyun 156*4882a593SmuzhiyunDifferent from CAN is that the payload data is just the data that get sends, 157*4882a593Smuzhiyunwithout its header info. The header info is derived from the sockaddr supplied 158*4882a593Smuzhiyunto ``bind(2)``, ``connect(2)``, ``sendto(2)`` and ``recvfrom(2)``. A ``write(2)`` with size 4 will 159*4882a593Smuzhiyunresult in a packet with 4 bytes. 160*4882a593Smuzhiyun 161*4882a593SmuzhiyunThe sockaddr structure has extensions for use with J1939 as specified below: 162*4882a593Smuzhiyun 163*4882a593Smuzhiyun.. code-block:: C 164*4882a593Smuzhiyun 165*4882a593Smuzhiyun struct sockaddr_can { 166*4882a593Smuzhiyun sa_family_t can_family; 167*4882a593Smuzhiyun int can_ifindex; 168*4882a593Smuzhiyun union { 169*4882a593Smuzhiyun struct { 170*4882a593Smuzhiyun __u64 name; 171*4882a593Smuzhiyun /* pgn: 172*4882a593Smuzhiyun * 8 bit: PS in PDU2 case, else 0 173*4882a593Smuzhiyun * 8 bit: PF 174*4882a593Smuzhiyun * 1 bit: DP 175*4882a593Smuzhiyun * 1 bit: reserved 176*4882a593Smuzhiyun */ 177*4882a593Smuzhiyun __u32 pgn; 178*4882a593Smuzhiyun __u8 addr; 179*4882a593Smuzhiyun } j1939; 180*4882a593Smuzhiyun } can_addr; 181*4882a593Smuzhiyun } 182*4882a593Smuzhiyun 183*4882a593Smuzhiyun``can_family`` & ``can_ifindex`` serve the same purpose as for other SocketCAN sockets. 184*4882a593Smuzhiyun 185*4882a593Smuzhiyun``can_addr.j1939.pgn`` specifies the PGN (max 0x3ffff). Individual bits are 186*4882a593Smuzhiyunspecified above. 187*4882a593Smuzhiyun 188*4882a593Smuzhiyun``can_addr.j1939.name`` contains the 64-bit J1939 NAME. 189*4882a593Smuzhiyun 190*4882a593Smuzhiyun``can_addr.j1939.addr`` contains the address. 191*4882a593Smuzhiyun 192*4882a593SmuzhiyunThe ``bind(2)`` system call assigns the local address, i.e. the source address when 193*4882a593Smuzhiyunsending packages. If a PGN during ``bind(2)`` is set, it's used as a RX filter. 194*4882a593SmuzhiyunI.e. only packets with a matching PGN are received. If an ADDR or NAME is set 195*4882a593Smuzhiyunit is used as a receive filter, too. It will match the destination NAME or ADDR 196*4882a593Smuzhiyunof the incoming packet. The NAME filter will work only if appropriate Address 197*4882a593SmuzhiyunClaiming for this name was done on the CAN bus and registered/cached by the 198*4882a593Smuzhiyunkernel. 199*4882a593Smuzhiyun 200*4882a593SmuzhiyunOn the other hand ``connect(2)`` assigns the remote address, i.e. the destination 201*4882a593Smuzhiyunaddress. The PGN from ``connect(2)`` is used as the default PGN when sending 202*4882a593Smuzhiyunpackets. If ADDR or NAME is set it will be used as the default destination ADDR 203*4882a593Smuzhiyunor NAME. Further a set ADDR or NAME during ``connect(2)`` is used as a receive 204*4882a593Smuzhiyunfilter. It will match the source NAME or ADDR of the incoming packet. 205*4882a593Smuzhiyun 206*4882a593SmuzhiyunBoth ``write(2)`` and ``send(2)`` will send a packet with local address from ``bind(2)`` and the 207*4882a593Smuzhiyunremote address from ``connect(2)``. Use ``sendto(2)`` to overwrite the destination 208*4882a593Smuzhiyunaddress. 209*4882a593Smuzhiyun 210*4882a593SmuzhiyunIf ``can_addr.j1939.name`` is set (!= 0) the NAME is looked up by the kernel and 211*4882a593Smuzhiyunthe corresponding ADDR is used. If ``can_addr.j1939.name`` is not set (== 0), 212*4882a593Smuzhiyun``can_addr.j1939.addr`` is used. 213*4882a593Smuzhiyun 214*4882a593SmuzhiyunWhen creating a socket, reasonable defaults are set. Some options can be 215*4882a593Smuzhiyunmodified with ``setsockopt(2)`` & ``getsockopt(2)``. 216*4882a593Smuzhiyun 217*4882a593SmuzhiyunRX path related options: 218*4882a593Smuzhiyun 219*4882a593Smuzhiyun- ``SO_J1939_FILTER`` - configure array of filters 220*4882a593Smuzhiyun- ``SO_J1939_PROMISC`` - disable filters set by ``bind(2)`` and ``connect(2)`` 221*4882a593Smuzhiyun 222*4882a593SmuzhiyunBy default no broadcast packets can be send or received. To enable sending or 223*4882a593Smuzhiyunreceiving broadcast packets use the socket option ``SO_BROADCAST``: 224*4882a593Smuzhiyun 225*4882a593Smuzhiyun.. code-block:: C 226*4882a593Smuzhiyun 227*4882a593Smuzhiyun int value = 1; 228*4882a593Smuzhiyun setsockopt(sock, SOL_SOCKET, SO_BROADCAST, &value, sizeof(value)); 229*4882a593Smuzhiyun 230*4882a593SmuzhiyunThe following diagram illustrates the RX path: 231*4882a593Smuzhiyun 232*4882a593Smuzhiyun.. code:: 233*4882a593Smuzhiyun 234*4882a593Smuzhiyun +--------------------+ 235*4882a593Smuzhiyun | incoming packet | 236*4882a593Smuzhiyun +--------------------+ 237*4882a593Smuzhiyun | 238*4882a593Smuzhiyun V 239*4882a593Smuzhiyun +--------------------+ 240*4882a593Smuzhiyun | SO_J1939_PROMISC? | 241*4882a593Smuzhiyun +--------------------+ 242*4882a593Smuzhiyun | | 243*4882a593Smuzhiyun no | | yes 244*4882a593Smuzhiyun | | 245*4882a593Smuzhiyun .---------' `---------. 246*4882a593Smuzhiyun | | 247*4882a593Smuzhiyun +---------------------------+ | 248*4882a593Smuzhiyun | bind() + connect() + | | 249*4882a593Smuzhiyun | SOCK_BROADCAST filter | | 250*4882a593Smuzhiyun +---------------------------+ | 251*4882a593Smuzhiyun | | 252*4882a593Smuzhiyun |<---------------------' 253*4882a593Smuzhiyun V 254*4882a593Smuzhiyun +---------------------------+ 255*4882a593Smuzhiyun | SO_J1939_FILTER | 256*4882a593Smuzhiyun +---------------------------+ 257*4882a593Smuzhiyun | 258*4882a593Smuzhiyun V 259*4882a593Smuzhiyun +---------------------------+ 260*4882a593Smuzhiyun | socket recv() | 261*4882a593Smuzhiyun +---------------------------+ 262*4882a593Smuzhiyun 263*4882a593SmuzhiyunTX path related options: 264*4882a593Smuzhiyun``SO_J1939_SEND_PRIO`` - change default send priority for the socket 265*4882a593Smuzhiyun 266*4882a593SmuzhiyunMessage Flags during send() and Related System Calls 267*4882a593Smuzhiyun^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 268*4882a593Smuzhiyun 269*4882a593Smuzhiyun``send(2)``, ``sendto(2)`` and ``sendmsg(2)`` take a 'flags' argument. Currently 270*4882a593Smuzhiyunsupported flags are: 271*4882a593Smuzhiyun 272*4882a593Smuzhiyun* ``MSG_DONTWAIT``, i.e. non-blocking operation. 273*4882a593Smuzhiyun 274*4882a593Smuzhiyunrecvmsg(2) 275*4882a593Smuzhiyun^^^^^^^^^^ 276*4882a593Smuzhiyun 277*4882a593SmuzhiyunIn most cases ``recvmsg(2)`` is needed if you want to extract more information than 278*4882a593Smuzhiyun``recvfrom(2)`` can provide. For example package priority and timestamp. The 279*4882a593SmuzhiyunDestination Address, name and packet priority (if applicable) are attached to 280*4882a593Smuzhiyunthe msghdr in the ``recvmsg(2)`` call. They can be extracted using ``cmsg(3)`` macros, 281*4882a593Smuzhiyunwith ``cmsg_level == SOL_J1939 && cmsg_type == SCM_J1939_DEST_ADDR``, 282*4882a593Smuzhiyun``SCM_J1939_DEST_NAME`` or ``SCM_J1939_PRIO``. The returned data is a ``uint8_t`` for 283*4882a593Smuzhiyun``priority`` and ``dst_addr``, and ``uint64_t`` for ``dst_name``. 284*4882a593Smuzhiyun 285*4882a593Smuzhiyun.. code-block:: C 286*4882a593Smuzhiyun 287*4882a593Smuzhiyun uint8_t priority, dst_addr; 288*4882a593Smuzhiyun uint64_t dst_name; 289*4882a593Smuzhiyun 290*4882a593Smuzhiyun for (cmsg = CMSG_FIRSTHDR(&msg); cmsg; cmsg = CMSG_NXTHDR(&msg, cmsg)) { 291*4882a593Smuzhiyun switch (cmsg->cmsg_level) { 292*4882a593Smuzhiyun case SOL_CAN_J1939: 293*4882a593Smuzhiyun if (cmsg->cmsg_type == SCM_J1939_DEST_ADDR) 294*4882a593Smuzhiyun dst_addr = *CMSG_DATA(cmsg); 295*4882a593Smuzhiyun else if (cmsg->cmsg_type == SCM_J1939_DEST_NAME) 296*4882a593Smuzhiyun memcpy(&dst_name, CMSG_DATA(cmsg), cmsg->cmsg_len - CMSG_LEN(0)); 297*4882a593Smuzhiyun else if (cmsg->cmsg_type == SCM_J1939_PRIO) 298*4882a593Smuzhiyun priority = *CMSG_DATA(cmsg); 299*4882a593Smuzhiyun break; 300*4882a593Smuzhiyun } 301*4882a593Smuzhiyun } 302*4882a593Smuzhiyun 303*4882a593SmuzhiyunDynamic Addressing 304*4882a593Smuzhiyun------------------ 305*4882a593Smuzhiyun 306*4882a593SmuzhiyunDistinction has to be made between using the claimed address and doing an 307*4882a593Smuzhiyunaddress claim. To use an already claimed address, one has to fill in the 308*4882a593Smuzhiyun``j1939.name`` member and provide it to ``bind(2)``. If the name had claimed an address 309*4882a593Smuzhiyunearlier, all further messages being sent will use that address. And the 310*4882a593Smuzhiyun``j1939.addr`` member will be ignored. 311*4882a593Smuzhiyun 312*4882a593SmuzhiyunAn exception on this is PGN 0x0ee00. This is the "Address Claim/Cannot Claim 313*4882a593SmuzhiyunAddress" message and the kernel will use the ``j1939.addr`` member for that PGN if 314*4882a593Smuzhiyunnecessary. 315*4882a593Smuzhiyun 316*4882a593SmuzhiyunTo claim an address following code example can be used: 317*4882a593Smuzhiyun 318*4882a593Smuzhiyun.. code-block:: C 319*4882a593Smuzhiyun 320*4882a593Smuzhiyun struct sockaddr_can baddr = { 321*4882a593Smuzhiyun .can_family = AF_CAN, 322*4882a593Smuzhiyun .can_addr.j1939 = { 323*4882a593Smuzhiyun .name = name, 324*4882a593Smuzhiyun .addr = J1939_IDLE_ADDR, 325*4882a593Smuzhiyun .pgn = J1939_NO_PGN, /* to disable bind() rx filter for PGN */ 326*4882a593Smuzhiyun }, 327*4882a593Smuzhiyun .can_ifindex = if_nametoindex("can0"), 328*4882a593Smuzhiyun }; 329*4882a593Smuzhiyun 330*4882a593Smuzhiyun bind(sock, (struct sockaddr *)&baddr, sizeof(baddr)); 331*4882a593Smuzhiyun 332*4882a593Smuzhiyun /* for Address Claiming broadcast must be allowed */ 333*4882a593Smuzhiyun int value = 1; 334*4882a593Smuzhiyun setsockopt(sock, SOL_SOCKET, SO_BROADCAST, &value, sizeof(value)); 335*4882a593Smuzhiyun 336*4882a593Smuzhiyun /* configured advanced RX filter with PGN needed for Address Claiming */ 337*4882a593Smuzhiyun const struct j1939_filter filt[] = { 338*4882a593Smuzhiyun { 339*4882a593Smuzhiyun .pgn = J1939_PGN_ADDRESS_CLAIMED, 340*4882a593Smuzhiyun .pgn_mask = J1939_PGN_PDU1_MAX, 341*4882a593Smuzhiyun }, { 342*4882a593Smuzhiyun .pgn = J1939_PGN_REQUEST, 343*4882a593Smuzhiyun .pgn_mask = J1939_PGN_PDU1_MAX, 344*4882a593Smuzhiyun }, { 345*4882a593Smuzhiyun .pgn = J1939_PGN_ADDRESS_COMMANDED, 346*4882a593Smuzhiyun .pgn_mask = J1939_PGN_MAX, 347*4882a593Smuzhiyun }, 348*4882a593Smuzhiyun }; 349*4882a593Smuzhiyun 350*4882a593Smuzhiyun setsockopt(sock, SOL_CAN_J1939, SO_J1939_FILTER, &filt, sizeof(filt)); 351*4882a593Smuzhiyun 352*4882a593Smuzhiyun uint64_t dat = htole64(name); 353*4882a593Smuzhiyun const struct sockaddr_can saddr = { 354*4882a593Smuzhiyun .can_family = AF_CAN, 355*4882a593Smuzhiyun .can_addr.j1939 = { 356*4882a593Smuzhiyun .pgn = J1939_PGN_ADDRESS_CLAIMED, 357*4882a593Smuzhiyun .addr = J1939_NO_ADDR, 358*4882a593Smuzhiyun }, 359*4882a593Smuzhiyun }; 360*4882a593Smuzhiyun 361*4882a593Smuzhiyun /* Afterwards do a sendto(2) with data set to the NAME (Little Endian). If the 362*4882a593Smuzhiyun * NAME provided, does not match the j1939.name provided to bind(2), EPROTO 363*4882a593Smuzhiyun * will be returned. 364*4882a593Smuzhiyun */ 365*4882a593Smuzhiyun sendto(sock, dat, sizeof(dat), 0, (const struct sockaddr *)&saddr, sizeof(saddr)); 366*4882a593Smuzhiyun 367*4882a593SmuzhiyunIf no-one else contests the address claim within 250ms after transmission, the 368*4882a593Smuzhiyunkernel marks the NAME-SA assignment as valid. The valid assignment will be kept 369*4882a593Smuzhiyunamong other valid NAME-SA assignments. From that point, any socket bound to the 370*4882a593SmuzhiyunNAME can send packets. 371*4882a593Smuzhiyun 372*4882a593SmuzhiyunIf another ECU claims the address, the kernel will mark the NAME-SA expired. 373*4882a593SmuzhiyunNo socket bound to the NAME can send packets (other than address claims). To 374*4882a593Smuzhiyunclaim another address, some socket bound to NAME, must ``bind(2)`` again, but with 375*4882a593Smuzhiyunonly ``j1939.addr`` changed to the new SA, and must then send a valid address claim 376*4882a593Smuzhiyunpacket. This restarts the state machine in the kernel (and any other 377*4882a593Smuzhiyunparticipant on the bus) for this NAME. 378*4882a593Smuzhiyun 379*4882a593Smuzhiyun``can-utils`` also include the ``j1939acd`` tool, so it can be used as code example or as 380*4882a593Smuzhiyundefault Address Claiming daemon. 381*4882a593Smuzhiyun 382*4882a593SmuzhiyunSend Examples 383*4882a593Smuzhiyun------------- 384*4882a593Smuzhiyun 385*4882a593SmuzhiyunStatic Addressing 386*4882a593Smuzhiyun^^^^^^^^^^^^^^^^^ 387*4882a593Smuzhiyun 388*4882a593SmuzhiyunThis example will send a PGN (0x12300) from SA 0x20 to DA 0x30. 389*4882a593Smuzhiyun 390*4882a593SmuzhiyunBind: 391*4882a593Smuzhiyun 392*4882a593Smuzhiyun.. code-block:: C 393*4882a593Smuzhiyun 394*4882a593Smuzhiyun struct sockaddr_can baddr = { 395*4882a593Smuzhiyun .can_family = AF_CAN, 396*4882a593Smuzhiyun .can_addr.j1939 = { 397*4882a593Smuzhiyun .name = J1939_NO_NAME, 398*4882a593Smuzhiyun .addr = 0x20, 399*4882a593Smuzhiyun .pgn = J1939_NO_PGN, 400*4882a593Smuzhiyun }, 401*4882a593Smuzhiyun .can_ifindex = if_nametoindex("can0"), 402*4882a593Smuzhiyun }; 403*4882a593Smuzhiyun 404*4882a593Smuzhiyun bind(sock, (struct sockaddr *)&baddr, sizeof(baddr)); 405*4882a593Smuzhiyun 406*4882a593SmuzhiyunNow, the socket 'sock' is bound to the SA 0x20. Since no ``connect(2)`` was called, 407*4882a593Smuzhiyunat this point we can use only ``sendto(2)`` or ``sendmsg(2)``. 408*4882a593Smuzhiyun 409*4882a593SmuzhiyunSend: 410*4882a593Smuzhiyun 411*4882a593Smuzhiyun.. code-block:: C 412*4882a593Smuzhiyun 413*4882a593Smuzhiyun const struct sockaddr_can saddr = { 414*4882a593Smuzhiyun .can_family = AF_CAN, 415*4882a593Smuzhiyun .can_addr.j1939 = { 416*4882a593Smuzhiyun .name = J1939_NO_NAME; 417*4882a593Smuzhiyun .addr = 0x30, 418*4882a593Smuzhiyun .pgn = 0x12300, 419*4882a593Smuzhiyun }, 420*4882a593Smuzhiyun }; 421*4882a593Smuzhiyun 422*4882a593Smuzhiyun sendto(sock, dat, sizeof(dat), 0, (const struct sockaddr *)&saddr, sizeof(saddr)); 423