├── .gitignore ├── LICENSE ├── Makefile ├── Readme.md ├── can-j1939-kickstart.md ├── include └── uapi │ └── linux │ ├── can.h │ └── can │ ├── bcm.h │ ├── error.h │ ├── gw.h │ ├── j1939.h │ ├── netlink.h │ └── raw.h ├── page.theme ├── style.css └── testj1939.c /.gitignore: -------------------------------------------------------------------------------- 1 | # output 2 | testj1939 3 | Readme.html 4 | /can-j1939-kickstart.html 5 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 3, 29 June 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | Preamble 9 | 10 | The GNU General Public License is a free, copyleft license for 11 | software and other kinds of works. 12 | 13 | The licenses for most software and other practical works are designed 14 | to take away your freedom to share and change the works. By contrast, 15 | the GNU General Public License is intended to guarantee your freedom to 16 | share and change all versions of a program--to make sure it remains free 17 | software for all its users. We, the Free Software Foundation, use the 18 | GNU General Public License for most of our software; it applies also to 19 | any other work released this way by its authors. You can apply it to 20 | your programs, too. 21 | 22 | When we speak of free software, we are referring to freedom, not 23 | price. Our General Public Licenses are designed to make sure that you 24 | have the freedom to distribute copies of free software (and charge for 25 | them if you wish), that you receive source code or can get it if you 26 | want it, that you can change the software or use pieces of it in new 27 | free programs, and that you know you can do these things. 28 | 29 | To protect your rights, we need to prevent others from denying you 30 | these rights or asking you to surrender the rights. Therefore, you have 31 | certain responsibilities if you distribute copies of the software, or if 32 | you modify it: responsibilities to respect the freedom of others. 33 | 34 | For example, if you distribute copies of such a program, whether 35 | gratis or for a fee, you must pass on to the recipients the same 36 | freedoms that you received. You must make sure that they, too, receive 37 | or can get the source code. And you must show them these terms so they 38 | know their rights. 39 | 40 | Developers that use the GNU GPL protect your rights with two steps: 41 | (1) assert copyright on the software, and (2) offer you this License 42 | giving you legal permission to copy, distribute and/or modify it. 43 | 44 | For the developers' and authors' protection, the GPL clearly explains 45 | that there is no warranty for this free software. For both users' and 46 | authors' sake, the GPL requires that modified versions be marked as 47 | changed, so that their problems will not be attributed erroneously to 48 | authors of previous versions. 49 | 50 | Some devices are designed to deny users access to install or run 51 | modified versions of the software inside them, although the manufacturer 52 | can do so. This is fundamentally incompatible with the aim of 53 | protecting users' freedom to change the software. The systematic 54 | pattern of such abuse occurs in the area of products for individuals to 55 | use, which is precisely where it is most unacceptable. Therefore, we 56 | have designed this version of the GPL to prohibit the practice for those 57 | products. If such problems arise substantially in other domains, we 58 | stand ready to extend this provision to those domains in future versions 59 | of the GPL, as needed to protect the freedom of users. 60 | 61 | Finally, every program is threatened constantly by software patents. 62 | States should not allow patents to restrict development and use of 63 | software on general-purpose computers, but in those that do, we wish to 64 | avoid the special danger that patents applied to a free program could 65 | make it effectively proprietary. To prevent this, the GPL assures that 66 | patents cannot be used to render the program non-free. 67 | 68 | The precise terms and conditions for copying, distribution and 69 | modification follow. 70 | 71 | TERMS AND CONDITIONS 72 | 73 | 0. Definitions. 74 | 75 | "This License" refers to version 3 of the GNU General Public License. 76 | 77 | "Copyright" also means copyright-like laws that apply to other kinds of 78 | works, such as semiconductor masks. 79 | 80 | "The Program" refers to any copyrightable work licensed under this 81 | License. Each licensee is addressed as "you". "Licensees" and 82 | "recipients" may be individuals or organizations. 83 | 84 | To "modify" a work means to copy from or adapt all or part of the work 85 | in a fashion requiring copyright permission, other than the making of an 86 | exact copy. The resulting work is called a "modified version" of the 87 | earlier work or a work "based on" the earlier work. 88 | 89 | A "covered work" means either the unmodified Program or a work based 90 | on the Program. 91 | 92 | To "propagate" a work means to do anything with it that, without 93 | permission, would make you directly or secondarily liable for 94 | infringement under applicable copyright law, except executing it on a 95 | computer or modifying a private copy. Propagation includes copying, 96 | distribution (with or without modification), making available to the 97 | public, and in some countries other activities as well. 98 | 99 | To "convey" a work means any kind of propagation that enables other 100 | parties to make or receive copies. Mere interaction with a user through 101 | a computer network, with no transfer of a copy, is not conveying. 102 | 103 | An interactive user interface displays "Appropriate Legal Notices" 104 | to the extent that it includes a convenient and prominently visible 105 | feature that (1) displays an appropriate copyright notice, and (2) 106 | tells the user that there is no warranty for the work (except to the 107 | extent that warranties are provided), that licensees may convey the 108 | work under this License, and how to view a copy of this License. If 109 | the interface presents a list of user commands or options, such as a 110 | menu, a prominent item in the list meets this criterion. 111 | 112 | 1. Source Code. 113 | 114 | The "source code" for a work means the preferred form of the work 115 | for making modifications to it. "Object code" means any non-source 116 | form of a work. 117 | 118 | A "Standard Interface" means an interface that either is an official 119 | standard defined by a recognized standards body, or, in the case of 120 | interfaces specified for a particular programming language, one that 121 | is widely used among developers working in that language. 122 | 123 | The "System Libraries" of an executable work include anything, other 124 | than the work as a whole, that (a) is included in the normal form of 125 | packaging a Major Component, but which is not part of that Major 126 | Component, and (b) serves only to enable use of the work with that 127 | Major Component, or to implement a Standard Interface for which an 128 | implementation is available to the public in source code form. A 129 | "Major Component", in this context, means a major essential component 130 | (kernel, window system, and so on) of the specific operating system 131 | (if any) on which the executable work runs, or a compiler used to 132 | produce the work, or an object code interpreter used to run it. 133 | 134 | The "Corresponding Source" for a work in object code form means all 135 | the source code needed to generate, install, and (for an executable 136 | work) run the object code and to modify the work, including scripts to 137 | control those activities. However, it does not include the work's 138 | System Libraries, or general-purpose tools or generally available free 139 | programs which are used unmodified in performing those activities but 140 | which are not part of the work. For example, Corresponding Source 141 | includes interface definition files associated with source files for 142 | the work, and the source code for shared libraries and dynamically 143 | linked subprograms that the work is specifically designed to require, 144 | such as by intimate data communication or control flow between those 145 | subprograms and other parts of the work. 146 | 147 | The Corresponding Source need not include anything that users 148 | can regenerate automatically from other parts of the Corresponding 149 | Source. 150 | 151 | The Corresponding Source for a work in source code form is that 152 | same work. 153 | 154 | 2. Basic Permissions. 155 | 156 | All rights granted under this License are granted for the term of 157 | copyright on the Program, and are irrevocable provided the stated 158 | conditions are met. This License explicitly affirms your unlimited 159 | permission to run the unmodified Program. The output from running a 160 | covered work is covered by this License only if the output, given its 161 | content, constitutes a covered work. This License acknowledges your 162 | rights of fair use or other equivalent, as provided by copyright law. 163 | 164 | You may make, run and propagate covered works that you do not 165 | convey, without conditions so long as your license otherwise remains 166 | in force. You may convey covered works to others for the sole purpose 167 | of having them make modifications exclusively for you, or provide you 168 | with facilities for running those works, provided that you comply with 169 | the terms of this License in conveying all material for which you do 170 | not control copyright. Those thus making or running the covered works 171 | for you must do so exclusively on your behalf, under your direction 172 | and control, on terms that prohibit them from making any copies of 173 | your copyrighted material outside their relationship with you. 174 | 175 | Conveying under any other circumstances is permitted solely under 176 | the conditions stated below. Sublicensing is not allowed; section 10 177 | makes it unnecessary. 178 | 179 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law. 180 | 181 | No covered work shall be deemed part of an effective technological 182 | measure under any applicable law fulfilling obligations under article 183 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or 184 | similar laws prohibiting or restricting circumvention of such 185 | measures. 186 | 187 | When you convey a covered work, you waive any legal power to forbid 188 | circumvention of technological measures to the extent such circumvention 189 | is effected by exercising rights under this License with respect to 190 | the covered work, and you disclaim any intention to limit operation or 191 | modification of the work as a means of enforcing, against the work's 192 | users, your or third parties' legal rights to forbid circumvention of 193 | technological measures. 194 | 195 | 4. Conveying Verbatim Copies. 196 | 197 | You may convey verbatim copies of the Program's source code as you 198 | receive it, in any medium, provided that you conspicuously and 199 | appropriately publish on each copy an appropriate copyright notice; 200 | keep intact all notices stating that this License and any 201 | non-permissive terms added in accord with section 7 apply to the code; 202 | keep intact all notices of the absence of any warranty; and give all 203 | recipients a copy of this License along with the Program. 204 | 205 | You may charge any price or no price for each copy that you convey, 206 | and you may offer support or warranty protection for a fee. 207 | 208 | 5. Conveying Modified Source Versions. 209 | 210 | You may convey a work based on the Program, or the modifications to 211 | produce it from the Program, in the form of source code under the 212 | terms of section 4, provided that you also meet all of these conditions: 213 | 214 | a) The work must carry prominent notices stating that you modified 215 | it, and giving a relevant date. 216 | 217 | b) The work must carry prominent notices stating that it is 218 | released under this License and any conditions added under section 219 | 7. This requirement modifies the requirement in section 4 to 220 | "keep intact all notices". 221 | 222 | c) You must license the entire work, as a whole, under this 223 | License to anyone who comes into possession of a copy. This 224 | License will therefore apply, along with any applicable section 7 225 | additional terms, to the whole of the work, and all its parts, 226 | regardless of how they are packaged. This License gives no 227 | permission to license the work in any other way, but it does not 228 | invalidate such permission if you have separately received it. 229 | 230 | d) If the work has interactive user interfaces, each must display 231 | Appropriate Legal Notices; however, if the Program has interactive 232 | interfaces that do not display Appropriate Legal Notices, your 233 | work need not make them do so. 234 | 235 | A compilation of a covered work with other separate and independent 236 | works, which are not by their nature extensions of the covered work, 237 | and which are not combined with it such as to form a larger program, 238 | in or on a volume of a storage or distribution medium, is called an 239 | "aggregate" if the compilation and its resulting copyright are not 240 | used to limit the access or legal rights of the compilation's users 241 | beyond what the individual works permit. Inclusion of a covered work 242 | in an aggregate does not cause this License to apply to the other 243 | parts of the aggregate. 244 | 245 | 6. Conveying Non-Source Forms. 246 | 247 | You may convey a covered work in object code form under the terms 248 | of sections 4 and 5, provided that you also convey the 249 | machine-readable Corresponding Source under the terms of this License, 250 | in one of these ways: 251 | 252 | a) Convey the object code in, or embodied in, a physical product 253 | (including a physical distribution medium), accompanied by the 254 | Corresponding Source fixed on a durable physical medium 255 | customarily used for software interchange. 256 | 257 | b) Convey the object code in, or embodied in, a physical product 258 | (including a physical distribution medium), accompanied by a 259 | written offer, valid for at least three years and valid for as 260 | long as you offer spare parts or customer support for that product 261 | model, to give anyone who possesses the object code either (1) a 262 | copy of the Corresponding Source for all the software in the 263 | product that is covered by this License, on a durable physical 264 | medium customarily used for software interchange, for a price no 265 | more than your reasonable cost of physically performing this 266 | conveying of source, or (2) access to copy the 267 | Corresponding Source from a network server at no charge. 268 | 269 | c) Convey individual copies of the object code with a copy of the 270 | written offer to provide the Corresponding Source. This 271 | alternative is allowed only occasionally and noncommercially, and 272 | only if you received the object code with such an offer, in accord 273 | with subsection 6b. 274 | 275 | d) Convey the object code by offering access from a designated 276 | place (gratis or for a charge), and offer equivalent access to the 277 | Corresponding Source in the same way through the same place at no 278 | further charge. You need not require recipients to copy the 279 | Corresponding Source along with the object code. If the place to 280 | copy the object code is a network server, the Corresponding Source 281 | may be on a different server (operated by you or a third party) 282 | that supports equivalent copying facilities, provided you maintain 283 | clear directions next to the object code saying where to find the 284 | Corresponding Source. Regardless of what server hosts the 285 | Corresponding Source, you remain obligated to ensure that it is 286 | available for as long as needed to satisfy these requirements. 287 | 288 | e) Convey the object code using peer-to-peer transmission, provided 289 | you inform other peers where the object code and Corresponding 290 | Source of the work are being offered to the general public at no 291 | charge under subsection 6d. 292 | 293 | A separable portion of the object code, whose source code is excluded 294 | from the Corresponding Source as a System Library, need not be 295 | included in conveying the object code work. 296 | 297 | A "User Product" is either (1) a "consumer product", which means any 298 | tangible personal property which is normally used for personal, family, 299 | or household purposes, or (2) anything designed or sold for incorporation 300 | into a dwelling. In determining whether a product is a consumer product, 301 | doubtful cases shall be resolved in favor of coverage. For a particular 302 | product received by a particular user, "normally used" refers to a 303 | typical or common use of that class of product, regardless of the status 304 | of the particular user or of the way in which the particular user 305 | actually uses, or expects or is expected to use, the product. A product 306 | is a consumer product regardless of whether the product has substantial 307 | commercial, industrial or non-consumer uses, unless such uses represent 308 | the only significant mode of use of the product. 309 | 310 | "Installation Information" for a User Product means any methods, 311 | procedures, authorization keys, or other information required to install 312 | and execute modified versions of a covered work in that User Product from 313 | a modified version of its Corresponding Source. The information must 314 | suffice to ensure that the continued functioning of the modified object 315 | code is in no case prevented or interfered with solely because 316 | modification has been made. 317 | 318 | If you convey an object code work under this section in, or with, or 319 | specifically for use in, a User Product, and the conveying occurs as 320 | part of a transaction in which the right of possession and use of the 321 | User Product is transferred to the recipient in perpetuity or for a 322 | fixed term (regardless of how the transaction is characterized), the 323 | Corresponding Source conveyed under this section must be accompanied 324 | by the Installation Information. But this requirement does not apply 325 | if neither you nor any third party retains the ability to install 326 | modified object code on the User Product (for example, the work has 327 | been installed in ROM). 328 | 329 | The requirement to provide Installation Information does not include a 330 | requirement to continue to provide support service, warranty, or updates 331 | for a work that has been modified or installed by the recipient, or for 332 | the User Product in which it has been modified or installed. Access to a 333 | network may be denied when the modification itself materially and 334 | adversely affects the operation of the network or violates the rules and 335 | protocols for communication across the network. 336 | 337 | Corresponding Source conveyed, and Installation Information provided, 338 | in accord with this section must be in a format that is publicly 339 | documented (and with an implementation available to the public in 340 | source code form), and must require no special password or key for 341 | unpacking, reading or copying. 342 | 343 | 7. Additional Terms. 344 | 345 | "Additional permissions" are terms that supplement the terms of this 346 | License by making exceptions from one or more of its conditions. 347 | Additional permissions that are applicable to the entire Program shall 348 | be treated as though they were included in this License, to the extent 349 | that they are valid under applicable law. If additional permissions 350 | apply only to part of the Program, that part may be used separately 351 | under those permissions, but the entire Program remains governed by 352 | this License without regard to the additional permissions. 353 | 354 | When you convey a copy of a covered work, you may at your option 355 | remove any additional permissions from that copy, or from any part of 356 | it. (Additional permissions may be written to require their own 357 | removal in certain cases when you modify the work.) You may place 358 | additional permissions on material, added by you to a covered work, 359 | for which you have or can give appropriate copyright permission. 360 | 361 | Notwithstanding any other provision of this License, for material you 362 | add to a covered work, you may (if authorized by the copyright holders of 363 | that material) supplement the terms of this License with terms: 364 | 365 | a) Disclaiming warranty or limiting liability differently from the 366 | terms of sections 15 and 16 of this License; or 367 | 368 | b) Requiring preservation of specified reasonable legal notices or 369 | author attributions in that material or in the Appropriate Legal 370 | Notices displayed by works containing it; or 371 | 372 | c) Prohibiting misrepresentation of the origin of that material, or 373 | requiring that modified versions of such material be marked in 374 | reasonable ways as different from the original version; or 375 | 376 | d) Limiting the use for publicity purposes of names of licensors or 377 | authors of the material; or 378 | 379 | e) Declining to grant rights under trademark law for use of some 380 | trade names, trademarks, or service marks; or 381 | 382 | f) Requiring indemnification of licensors and authors of that 383 | material by anyone who conveys the material (or modified versions of 384 | it) with contractual assumptions of liability to the recipient, for 385 | any liability that these contractual assumptions directly impose on 386 | those licensors and authors. 387 | 388 | All other non-permissive additional terms are considered "further 389 | restrictions" within the meaning of section 10. If the Program as you 390 | received it, or any part of it, contains a notice stating that it is 391 | governed by this License along with a term that is a further 392 | restriction, you may remove that term. If a license document contains 393 | a further restriction but permits relicensing or conveying under this 394 | License, you may add to a covered work material governed by the terms 395 | of that license document, provided that the further restriction does 396 | not survive such relicensing or conveying. 397 | 398 | If you add terms to a covered work in accord with this section, you 399 | must place, in the relevant source files, a statement of the 400 | additional terms that apply to those files, or a notice indicating 401 | where to find the applicable terms. 402 | 403 | Additional terms, permissive or non-permissive, may be stated in the 404 | form of a separately written license, or stated as exceptions; 405 | the above requirements apply either way. 406 | 407 | 8. Termination. 408 | 409 | You may not propagate or modify a covered work except as expressly 410 | provided under this License. Any attempt otherwise to propagate or 411 | modify it is void, and will automatically terminate your rights under 412 | this License (including any patent licenses granted under the third 413 | paragraph of section 11). 414 | 415 | However, if you cease all violation of this License, then your 416 | license from a particular copyright holder is reinstated (a) 417 | provisionally, unless and until the copyright holder explicitly and 418 | finally terminates your license, and (b) permanently, if the copyright 419 | holder fails to notify you of the violation by some reasonable means 420 | prior to 60 days after the cessation. 421 | 422 | Moreover, your license from a particular copyright holder is 423 | reinstated permanently if the copyright holder notifies you of the 424 | violation by some reasonable means, this is the first time you have 425 | received notice of violation of this License (for any work) from that 426 | copyright holder, and you cure the violation prior to 30 days after 427 | your receipt of the notice. 428 | 429 | Termination of your rights under this section does not terminate the 430 | licenses of parties who have received copies or rights from you under 431 | this License. If your rights have been terminated and not permanently 432 | reinstated, you do not qualify to receive new licenses for the same 433 | material under section 10. 434 | 435 | 9. Acceptance Not Required for Having Copies. 436 | 437 | You are not required to accept this License in order to receive or 438 | run a copy of the Program. Ancillary propagation of a covered work 439 | occurring solely as a consequence of using peer-to-peer transmission 440 | to receive a copy likewise does not require acceptance. However, 441 | nothing other than this License grants you permission to propagate or 442 | modify any covered work. These actions infringe copyright if you do 443 | not accept this License. Therefore, by modifying or propagating a 444 | covered work, you indicate your acceptance of this License to do so. 445 | 446 | 10. Automatic Licensing of Downstream Recipients. 447 | 448 | Each time you convey a covered work, the recipient automatically 449 | receives a license from the original licensors, to run, modify and 450 | propagate that work, subject to this License. You are not responsible 451 | for enforcing compliance by third parties with this License. 452 | 453 | An "entity transaction" is a transaction transferring control of an 454 | organization, or substantially all assets of one, or subdividing an 455 | organization, or merging organizations. If propagation of a covered 456 | work results from an entity transaction, each party to that 457 | transaction who receives a copy of the work also receives whatever 458 | licenses to the work the party's predecessor in interest had or could 459 | give under the previous paragraph, plus a right to possession of the 460 | Corresponding Source of the work from the predecessor in interest, if 461 | the predecessor has it or can get it with reasonable efforts. 462 | 463 | You may not impose any further restrictions on the exercise of the 464 | rights granted or affirmed under this License. For example, you may 465 | not impose a license fee, royalty, or other charge for exercise of 466 | rights granted under this License, and you may not initiate litigation 467 | (including a cross-claim or counterclaim in a lawsuit) alleging that 468 | any patent claim is infringed by making, using, selling, offering for 469 | sale, or importing the Program or any portion of it. 470 | 471 | 11. Patents. 472 | 473 | A "contributor" is a copyright holder who authorizes use under this 474 | License of the Program or a work on which the Program is based. The 475 | work thus licensed is called the contributor's "contributor version". 476 | 477 | A contributor's "essential patent claims" are all patent claims 478 | owned or controlled by the contributor, whether already acquired or 479 | hereafter acquired, that would be infringed by some manner, permitted 480 | by this License, of making, using, or selling its contributor version, 481 | but do not include claims that would be infringed only as a 482 | consequence of further modification of the contributor version. For 483 | purposes of this definition, "control" includes the right to grant 484 | patent sublicenses in a manner consistent with the requirements of 485 | this License. 486 | 487 | Each contributor grants you a non-exclusive, worldwide, royalty-free 488 | patent license under the contributor's essential patent claims, to 489 | make, use, sell, offer for sale, import and otherwise run, modify and 490 | propagate the contents of its contributor version. 491 | 492 | In the following three paragraphs, a "patent license" is any express 493 | agreement or commitment, however denominated, not to enforce a patent 494 | (such as an express permission to practice a patent or covenant not to 495 | sue for patent infringement). To "grant" such a patent license to a 496 | party means to make such an agreement or commitment not to enforce a 497 | patent against the party. 498 | 499 | If you convey a covered work, knowingly relying on a patent license, 500 | and the Corresponding Source of the work is not available for anyone 501 | to copy, free of charge and under the terms of this License, through a 502 | publicly available network server or other readily accessible means, 503 | then you must either (1) cause the Corresponding Source to be so 504 | available, or (2) arrange to deprive yourself of the benefit of the 505 | patent license for this particular work, or (3) arrange, in a manner 506 | consistent with the requirements of this License, to extend the patent 507 | license to downstream recipients. "Knowingly relying" means you have 508 | actual knowledge that, but for the patent license, your conveying the 509 | covered work in a country, or your recipient's use of the covered work 510 | in a country, would infringe one or more identifiable patents in that 511 | country that you have reason to believe are valid. 512 | 513 | If, pursuant to or in connection with a single transaction or 514 | arrangement, you convey, or propagate by procuring conveyance of, a 515 | covered work, and grant a patent license to some of the parties 516 | receiving the covered work authorizing them to use, propagate, modify 517 | or convey a specific copy of the covered work, then the patent license 518 | you grant is automatically extended to all recipients of the covered 519 | work and works based on it. 520 | 521 | A patent license is "discriminatory" if it does not include within 522 | the scope of its coverage, prohibits the exercise of, or is 523 | conditioned on the non-exercise of one or more of the rights that are 524 | specifically granted under this License. You may not convey a covered 525 | work if you are a party to an arrangement with a third party that is 526 | in the business of distributing software, under which you make payment 527 | to the third party based on the extent of your activity of conveying 528 | the work, and under which the third party grants, to any of the 529 | parties who would receive the covered work from you, a discriminatory 530 | patent license (a) in connection with copies of the covered work 531 | conveyed by you (or copies made from those copies), or (b) primarily 532 | for and in connection with specific products or compilations that 533 | contain the covered work, unless you entered into that arrangement, 534 | or that patent license was granted, prior to 28 March 2007. 535 | 536 | Nothing in this License shall be construed as excluding or limiting 537 | any implied license or other defenses to infringement that may 538 | otherwise be available to you under applicable patent law. 539 | 540 | 12. No Surrender of Others' Freedom. 541 | 542 | If conditions are imposed on you (whether by court order, agreement or 543 | otherwise) that contradict the conditions of this License, they do not 544 | excuse you from the conditions of this License. If you cannot convey a 545 | covered work so as to satisfy simultaneously your obligations under this 546 | License and any other pertinent obligations, then as a consequence you may 547 | not convey it at all. For example, if you agree to terms that obligate you 548 | to collect a royalty for further conveying from those to whom you convey 549 | the Program, the only way you could satisfy both those terms and this 550 | License would be to refrain entirely from conveying the Program. 551 | 552 | 13. Use with the GNU Affero General Public License. 553 | 554 | Notwithstanding any other provision of this License, you have 555 | permission to link or combine any covered work with a work licensed 556 | under version 3 of the GNU Affero General Public License into a single 557 | combined work, and to convey the resulting work. The terms of this 558 | License will continue to apply to the part which is the covered work, 559 | but the special requirements of the GNU Affero General Public License, 560 | section 13, concerning interaction through a network will apply to the 561 | combination as such. 562 | 563 | 14. Revised Versions of this License. 564 | 565 | The Free Software Foundation may publish revised and/or new versions of 566 | the GNU General Public License from time to time. Such new versions will 567 | be similar in spirit to the present version, but may differ in detail to 568 | address new problems or concerns. 569 | 570 | Each version is given a distinguishing version number. If the 571 | Program specifies that a certain numbered version of the GNU General 572 | Public License "or any later version" applies to it, you have the 573 | option of following the terms and conditions either of that numbered 574 | version or of any later version published by the Free Software 575 | Foundation. If the Program does not specify a version number of the 576 | GNU General Public License, you may choose any version ever published 577 | by the Free Software Foundation. 578 | 579 | If the Program specifies that a proxy can decide which future 580 | versions of the GNU General Public License can be used, that proxy's 581 | public statement of acceptance of a version permanently authorizes you 582 | to choose that version for the Program. 583 | 584 | Later license versions may give you additional or different 585 | permissions. However, no additional obligations are imposed on any 586 | author or copyright holder as a result of your choosing to follow a 587 | later version. 588 | 589 | 15. Disclaimer of Warranty. 590 | 591 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY 592 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT 593 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY 594 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, 595 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 596 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM 597 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF 598 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 599 | 600 | 16. Limitation of Liability. 601 | 602 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 603 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS 604 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY 605 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE 606 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF 607 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD 608 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), 609 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF 610 | SUCH DAMAGES. 611 | 612 | 17. Interpretation of Sections 15 and 16. 613 | 614 | If the disclaimer of warranty and limitation of liability provided 615 | above cannot be given local legal effect according to their terms, 616 | reviewing courts shall apply local law that most closely approximates 617 | an absolute waiver of all civil liability in connection with the 618 | Program, unless a warranty or assumption of liability accompanies a 619 | copy of the Program in return for a fee. 620 | 621 | END OF TERMS AND CONDITIONS 622 | 623 | How to Apply These Terms to Your New Programs 624 | 625 | If you develop a new program, and you want it to be of the greatest 626 | possible use to the public, the best way to achieve this is to make it 627 | free software which everyone can redistribute and change under these terms. 628 | 629 | To do so, attach the following notices to the program. It is safest 630 | to attach them to the start of each source file to most effectively 631 | state the exclusion of warranty; and each file should have at least 632 | the "copyright" line and a pointer to where the full notice is found. 633 | 634 | {one line to give the program's name and a brief idea of what it does.} 635 | Copyright (C) {year} {name of author} 636 | 637 | This program is free software: you can redistribute it and/or modify 638 | it under the terms of the GNU General Public License as published by 639 | the Free Software Foundation, either version 3 of the License, or 640 | (at your option) any later version. 641 | 642 | This program is distributed in the hope that it will be useful, 643 | but WITHOUT ANY WARRANTY; without even the implied warranty of 644 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 645 | GNU General Public License for more details. 646 | 647 | You should have received a copy of the GNU General Public License 648 | along with this program. If not, see . 649 | 650 | Also add information on how to contact you by electronic and paper mail. 651 | 652 | If the program does terminal interaction, make it output a short 653 | notice like this when it starts in an interactive mode: 654 | 655 | {project} Copyright (C) {year} {fullname} 656 | This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 657 | This is free software, and you are welcome to redistribute it 658 | under certain conditions; type `show c' for details. 659 | 660 | The hypothetical commands `show w' and `show c' should show the appropriate 661 | parts of the General Public License. Of course, your program's commands 662 | might be different; for a GUI interface, you would use an "about box". 663 | 664 | You should also get your employer (if you work as a programmer) or school, 665 | if any, to sign a "copyright disclaimer" for the program, if necessary. 666 | For more information on this, and how to apply and follow the GNU GPL, see 667 | . 668 | 669 | The GNU General Public License does not permit incorporating your program 670 | into proprietary programs. If your program is a subroutine library, you 671 | may consider it more useful to permit linking proprietary applications with 672 | the library. If this is what you want to do, use the GNU Lesser General 673 | Public License instead of this License. But first, please read 674 | . 675 | 676 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | PREFIX := /usr/local 2 | 3 | 4 | default: testj1939 5 | all: default $(patsubst %.md, %.html, $(wildcard *.md)) 6 | 7 | %.html: %.md page.theme 8 | theme -f -o $@ $< -p "$*" 9 | 10 | CFLAGS += -Wall -g3 -O0 11 | CPPFLAGS += -Iinclude/uapi 12 | 13 | install: 14 | install -D -m 755 testj1939 ${DESTDIR}${PREFIX}/bin/testj1939 15 | 16 | clean: 17 | rm -f testj1939 $(wildcard *.html) 18 | -------------------------------------------------------------------------------- /Readme.md: -------------------------------------------------------------------------------- 1 | # CAN-J1939 on linux 2 | 3 | The [Kickstart guide is here](can-j1939-kickstart.md) 4 | 5 | ## CAN on linux 6 | 7 | See [Wikipedia:socketcan](http://en.wikipedia.org/wiki/Socketcan) 8 | 9 | ## J1939 networking in short 10 | 11 | * Add addressing on top of CAN (destination address & broadcast) 12 | 13 | * Any (max 1780) length packets. 14 | Packets of 9 or more use **Transport Protocol** (fragmentation) 15 | Such packets use different CANid for the same PGN. 16 | 17 | * only **29**bit, non-**RTR** CAN frames 18 | 19 | * CAN id is composed of 20 | * 0..8: SA (source address) 21 | * 9..26: 22 | * PDU1: PGN+DA (destionation address) 23 | * PDU2: PGN 24 | * 27..29: PRIO 25 | 26 | * SA / DA may be dynamically assigned via j1939-81 27 | Fixed rules of precedence in Specification, no master necessary 28 | 29 | ## J1939 on SocketCAN 30 | 31 | J1939 is *just another protocol* that fits 32 | in the Berkely sockets. 33 | 34 | socket(AF_CAN, SOCK_DGRAM, CAN_J1939) 35 | 36 | ## differences from CAN_RAW 37 | ### addressing 38 | 39 | SA, DA & PGN are used, not CAN id. 40 | 41 | Berkeley socket API is used to communicate these to userspace: 42 | 43 | * SA+PGN is put in sockname ([getsockname](http://man7.org/linux/man-pages/man2/getsockname.2.html)) 44 | * DA+PGN is put in peername ([getpeername](http://man7.org/linux/man-pages/man2/getpeername.2.html)) 45 | PGN is put in both structs 46 | 47 | PRIO is a datalink property, and irrelevant for interpretation 48 | Therefore, PRIO is not in *sockname* or *peername*. 49 | 50 | The *data* that is [recv][recvfrom] or [send][sendto] is the real payload. 51 | Unlike CAN_RAW, where addressing info is data. 52 | 53 | ### Packet size 54 | 55 | J1939 handles packets of 8+ bytes with **Transport Protocol** fragmentation transparently. 56 | No fixed data size is necessary. 57 | 58 | send(sock, data, 8, 0); 59 | 60 | will emit a single CAN frame. 61 | 62 | send(sock, data, 9, 0); 63 | 64 | will use fragementation, emitting 1+ CAN frames. 65 | 66 | ## Enable j1939 (obsolete!) 67 | 68 | CAN has no protocol id field. 69 | The can-j1939 stack only activates when a socket opens 70 | for a network device. 71 | 72 | The methods described here existed in earlier implementations. 73 | 74 | ### netlink 75 | 76 | ip link set can0 j1939 on 77 | 78 | This method is obsoleted in favor of _on socket connect_. 79 | 80 | ### procfs for legacy kernel (2.6.25) 81 | 82 | This API is dropped for kernels with netlink support! 83 | 84 | echo can0 > /proc/net/can-j1939/net 85 | 86 | # Using J1939 87 | 88 | ## BSD socket implementation 89 | * socket 90 | * bind / connect 91 | * recvfrom / sendto 92 | * getsockname / getpeername 93 | 94 | ## Modified *struct sockaddr_can* 95 | 96 | struct sockaddr_can { 97 | sa_family_t can_family; 98 | int can_ifindex; 99 | union { 100 | struct { 101 | __u64 name; 102 | __u32 pgn; 103 | __u8 addr; 104 | } j1939; 105 | } can_addr; 106 | } 107 | 108 | * *can_addr.j1939.pgn* is PGN 109 | 110 | * *can_addr.j1939.addr* & *can_addr.j1939.name* 111 | determine the ECU 112 | 113 | * receiving address information, 114 | *addr* is always set, 115 | *name* is set when available. 116 | 117 | * When providing address information, 118 | *name* != 0 indicates dynamic addressing 119 | 120 | ## iproute2 (obsolete!) 121 | 122 | Older versions of can-j1939 used a modified iproute2 123 | for manipulating the kernel lists of current addresses. 124 | 125 | ### Static addressing 126 | 127 | ip addr add j1939 0x80 dev can0 128 | 129 | ### Dynamic addressing 130 | 131 | ip addr add j1939 name 0x012345678abcdef dev can0 132 | 133 | -------------------------------------------------------------------------------- /can-j1939-kickstart.md: -------------------------------------------------------------------------------- 1 | # Kickstart guide to can-j1939 on linux 2 | 3 | ## Prepare using VCAN 4 | 5 | You may skip this step entirely if you have a functional 6 | **can0** bus on your system. 7 | 8 | Load module, when *vcan* is not in-kernel 9 | 10 | modprobe vcan 11 | 12 | Create a virtual can0 device and start the device 13 | 14 | ip link add can0 type vcan 15 | ip link set can0 up 16 | 17 | ## First steps with j1939 18 | 19 | Use [testj1939](testj1939.c) 20 | 21 | When *can-j1939* is compiled as module, opening a socket will load it, 22 | __or__ you can load it manually 23 | 24 | modprobe can-j1939 25 | 26 | Most of the subsequent examples will use 2 sockets programs (in 2 terminals). 27 | One will use CAN_J1939 sockets using *testj1939*, 28 | and the other will use CAN_RAW sockets using cansend+candump. 29 | 30 | testj1939 can be told to print the used API calls by adding **-v** program argument. 31 | 32 | ### receive without source address 33 | 34 | Do in terminal 1 35 | 36 | ./testj1939 -r can0: 37 | 38 | Send raw CAN in terminal 2 39 | 40 | cansend can0 1823ff40#0123 41 | 42 | You should have this output in terminal 1 43 | 44 | 40 02300: 01 23 45 | 46 | This means, from NAME 0, SA 40, PGN 02300 was received, 47 | with 2 databytes, *01* & *02*. 48 | 49 | now emit this CAN message: 50 | 51 | cansend can0 18234140#0123 52 | 53 | In J1939, this means that ECU 0x40 sends directly to ECU 0x41 54 | Since we did not bind to address 0x41, this traffic 55 | is not meant for us and *testj1939* does not receive it. 56 | 57 | ### Use source address 58 | 59 | Binding a can-j1939 socket to a source address will register 60 | allow you to send packets. 61 | 62 | ./testj1939 can0:0x80 63 | 64 | Your system had, for a small moment, source address 0x80 assigned. 65 | 66 | ### receive with source address 67 | 68 | Terminal 1: 69 | 70 | ./testj1939 -r can0:0x80 71 | 72 | Terminal 2: 73 | 74 | cansend can0 18238040#0123 75 | 76 | Will emit this output 77 | 78 | 40 02300: 01 23 79 | 80 | This is because the traffic had destination address __0x80__ . 81 | 82 | ### send 83 | 84 | Open in terminal 1: 85 | 86 | candump -L can0 87 | 88 | And to these test in another terminal 89 | 90 | ./testj1939 -s can0:0x80,0x3ffff 91 | 92 | This produces **1BFFFF80#0123456789ABCDEF** on CAN. 93 | 94 | ### Multiple source addresses on 1 CAN device 95 | 96 | ./testj1939 -s can0:0x90,0x3ffff 97 | 98 | produces **1BFFFF90#0123456789ABCDEF** , 99 | 100 | ### Use PDU1 PGN 101 | 102 | ./testj1939 -s can0:0x80,0x12345 103 | 104 | emits **1923FF80#0123456789ABCDEF** . 105 | 106 | Note that the real PGN is **0x12300**, and destination address is **0xff**. 107 | 108 | ### Use destination address info 109 | 110 | The destination field may be set during sendto(). 111 | *testj1939* implements that like this 112 | 113 | ./testj1939 -s can0:0x80,0x12345 can0:0x40 114 | 115 | emits **19234080#0123456789ABCDEF** . 116 | 117 | The destination CAN iface __must__ always match the source CAN iface. 118 | Specifing one during bind is therefore sufficient. 119 | 120 | ./testj1939 -s can0:,0x12300 :0x40 121 | 122 | emits the very same. 123 | 124 | ### Emit different PGNs using the same socket 125 | 126 | The PGN is provided in both __bind( *sockname* )__ and 127 | __sendto( *peername* )__ , and only one is used. 128 | *peername* PGN has highest precedence. 129 | 130 | For broadcasted transmissions 131 | 132 | ./testj1939 -s can0:0x80,0x12300 :,0x32100 133 | 134 | emits **1B21FF80#0123456789ABCDEF** rather than 1923FF80#012345678ABCDEF 135 | 136 | Desitination specific transmissions 137 | 138 | ./testj1939 -s can0:0x80,0x12300 :0x40,0x32100 139 | 140 | emits **1B214080#0123456789ABCDEF** . 141 | 142 | It makes sometimes sense to omit the PGN in __bind( *sockname* )__ . 143 | 144 | ### Larger packets 145 | 146 | J1939 transparently switches to *Transport Protocol* when packets 147 | do not fit into single CAN packets. 148 | 149 | ./testj1939 -s20 can0:0x80 :,0x12300 150 | 151 | emits: 152 | 153 | 18ECFF80#20140003FF002301 154 | 155 | This is the first fragment for broadcasted *Transport Protocol*. 156 | _testj1939_ returns before the subsequent packets can leave, and 157 | as the last socket on the system closes, can-j1939 effectively 158 | cleans up all resources. Real-world applications will run like forever, 159 | and will not encounter this side-effect. 160 | 161 | Try again, and instruct _testj1939_ to keep the socket open for 1 second. 162 | 163 | ./testj1939 -w1.0 -s20 can0:0x80 :,0x12300 164 | 165 | emits: 166 | 167 | 18ECFF80#20140003FF002301 168 | 18EBFF80#010123456789ABCD 169 | 18EBFF80#02EF0123456789AB 170 | 18EBFF80#03CDEF01234567 171 | 172 | The fragments for broadcasted *Transport Protocol* are seperated 173 | __50ms__ from each other. 174 | Destination specific *Transport Protocol* applies flow control 175 | and may emit CAN packets much faster. 176 | 177 | First assign 0x90 to the local system. 178 | This becomes important because the kernel must interact in the 179 | transport protocol sessions before the complete packet is delivered. 180 | 181 | ./testj1939 can0:0x90 -r & 182 | 183 | Now test: 184 | 185 | ./testj1939 -s20 can0:0x80 :0x90,0x12300 186 | 187 | emits: 188 | 189 | 18EC9080#1014000303002301 190 | 18EC8090#110301FFFF002301 191 | 18EB9080#010123456789ABCD 192 | 18EB9080#02EF0123456789AB 193 | 18EB9080#03CDEF01234567 194 | 18EC8090#13140003FF002301 195 | 196 | The flow control causes a bit overhead. 197 | This overhead scales very good for larger J1939 packets. 198 | 199 | ## Advanced topics with j1939 200 | 201 | ### Change priority of J1939 packets 202 | 203 | ./testj1939 -s can0:0x80,0x0100 204 | ./testj1939 -s -p3 can0:0x80,0x0200 205 | 206 | emits 207 | 208 | 1801FF80#0123456789ABCDEF 209 | 0C02FF80#0123456789ABCDEF 210 | 211 | ### using connect 212 | 213 | ### advanced filtering 214 | 215 | ## dynamic addressing 216 | 217 | -------------------------------------------------------------------------------- /include/uapi/linux/can.h: -------------------------------------------------------------------------------- 1 | /* 2 | * linux/can.h 3 | * 4 | * Definitions for CAN network layer (socket addr / CAN frame / CAN filter) 5 | * 6 | * Authors: Oliver Hartkopp 7 | * Urs Thuermann 8 | * Copyright (c) 2002-2007 Volkswagen Group Electronic Research 9 | * All rights reserved. 10 | * 11 | * Redistribution and use in source and binary forms, with or without 12 | * modification, are permitted provided that the following conditions 13 | * are met: 14 | * 1. Redistributions of source code must retain the above copyright 15 | * notice, this list of conditions and the following disclaimer. 16 | * 2. Redistributions in binary form must reproduce the above copyright 17 | * notice, this list of conditions and the following disclaimer in the 18 | * documentation and/or other materials provided with the distribution. 19 | * 3. Neither the name of Volkswagen nor the names of its contributors 20 | * may be used to endorse or promote products derived from this software 21 | * without specific prior written permission. 22 | * 23 | * Alternatively, provided that this notice is retained in full, this 24 | * software may be distributed under the terms of the GNU General 25 | * Public License ("GPL") version 2, in which case the provisions of the 26 | * GPL apply INSTEAD OF those given above. 27 | * 28 | * The provided data structures and external interfaces from this code 29 | * are not restricted to be used by modules with a GPL compatible license. 30 | * 31 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 32 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 33 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 34 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 35 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 36 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 37 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 38 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 39 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 40 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 41 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH 42 | * DAMAGE. 43 | */ 44 | 45 | #ifndef _UAPI_CAN_H 46 | #define _UAPI_CAN_H 47 | 48 | #include 49 | #include 50 | 51 | /* controller area network (CAN) kernel definitions */ 52 | 53 | /* special address description flags for the CAN_ID */ 54 | #define CAN_EFF_FLAG 0x80000000U /* EFF/SFF is set in the MSB */ 55 | #define CAN_RTR_FLAG 0x40000000U /* remote transmission request */ 56 | #define CAN_ERR_FLAG 0x20000000U /* error message frame */ 57 | 58 | /* valid bits in CAN ID for frame formats */ 59 | #define CAN_SFF_MASK 0x000007FFU /* standard frame format (SFF) */ 60 | #define CAN_EFF_MASK 0x1FFFFFFFU /* extended frame format (EFF) */ 61 | #define CAN_ERR_MASK 0x1FFFFFFFU /* omit EFF, RTR, ERR flags */ 62 | 63 | /* 64 | * Controller Area Network Identifier structure 65 | * 66 | * bit 0-28 : CAN identifier (11/29 bit) 67 | * bit 29 : error message frame flag (0 = data frame, 1 = error message) 68 | * bit 30 : remote transmission request flag (1 = rtr frame) 69 | * bit 31 : frame format flag (0 = standard 11 bit, 1 = extended 29 bit) 70 | */ 71 | typedef __u32 canid_t; 72 | 73 | #define CAN_SFF_ID_BITS 11 74 | #define CAN_EFF_ID_BITS 29 75 | 76 | /* 77 | * Controller Area Network Error Message Frame Mask structure 78 | * 79 | * bit 0-28 : error class mask (see include/linux/can/error.h) 80 | * bit 29-31 : set to zero 81 | */ 82 | typedef __u32 can_err_mask_t; 83 | 84 | /* CAN payload length and DLC definitions according to ISO 11898-1 */ 85 | #define CAN_MAX_DLC 8 86 | #define CAN_MAX_DLEN 8 87 | 88 | /* CAN FD payload length and DLC definitions according to ISO 11898-7 */ 89 | #define CANFD_MAX_DLC 15 90 | #define CANFD_MAX_DLEN 64 91 | 92 | /** 93 | * struct can_frame - basic CAN frame structure 94 | * @can_id: CAN ID of the frame and CAN_*_FLAG flags, see canid_t definition 95 | * @can_dlc: frame payload length in byte (0 .. 8) aka data length code 96 | * N.B. the DLC field from ISO 11898-1 Chapter 8.4.2.3 has a 1:1 97 | * mapping of the 'data length code' to the real payload length 98 | * @__pad: padding 99 | * @__res0: reserved / padding 100 | * @__res1: reserved / padding 101 | * @data: CAN frame payload (up to 8 byte) 102 | */ 103 | struct can_frame { 104 | canid_t can_id; /* 32 bit CAN_ID + EFF/RTR/ERR flags */ 105 | __u8 can_dlc; /* frame payload length in byte (0 .. CAN_MAX_DLEN) */ 106 | __u8 __pad; /* padding */ 107 | __u8 __res0; /* reserved / padding */ 108 | __u8 __res1; /* reserved / padding */ 109 | __u8 data[CAN_MAX_DLEN] __attribute__((aligned(8))); 110 | }; 111 | 112 | /* 113 | * defined bits for canfd_frame.flags 114 | * 115 | * The use of struct canfd_frame implies the Extended Data Length (EDL) bit to 116 | * be set in the CAN frame bitstream on the wire. The EDL bit switch turns 117 | * the CAN controllers bitstream processor into the CAN FD mode which creates 118 | * two new options within the CAN FD frame specification: 119 | * 120 | * Bit Rate Switch - to indicate a second bitrate is/was used for the payload 121 | * Error State Indicator - represents the error state of the transmitting node 122 | * 123 | * As the CANFD_ESI bit is internally generated by the transmitting CAN 124 | * controller only the CANFD_BRS bit is relevant for real CAN controllers when 125 | * building a CAN FD frame for transmission. Setting the CANFD_ESI bit can make 126 | * sense for virtual CAN interfaces to test applications with echoed frames. 127 | */ 128 | #define CANFD_BRS 0x01 /* bit rate switch (second bitrate for payload data) */ 129 | #define CANFD_ESI 0x02 /* error state indicator of the transmitting node */ 130 | 131 | /** 132 | * struct canfd_frame - CAN flexible data rate frame structure 133 | * @can_id: CAN ID of the frame and CAN_*_FLAG flags, see canid_t definition 134 | * @len: frame payload length in byte (0 .. CANFD_MAX_DLEN) 135 | * @flags: additional flags for CAN FD 136 | * @__res0: reserved / padding 137 | * @__res1: reserved / padding 138 | * @data: CAN FD frame payload (up to CANFD_MAX_DLEN byte) 139 | */ 140 | struct canfd_frame { 141 | canid_t can_id; /* 32 bit CAN_ID + EFF/RTR/ERR flags */ 142 | __u8 len; /* frame payload length in byte */ 143 | __u8 flags; /* additional flags for CAN FD */ 144 | __u8 __res0; /* reserved / padding */ 145 | __u8 __res1; /* reserved / padding */ 146 | __u8 data[CANFD_MAX_DLEN] __attribute__((aligned(8))); 147 | }; 148 | 149 | #define CAN_MTU (sizeof(struct can_frame)) 150 | #define CANFD_MTU (sizeof(struct canfd_frame)) 151 | 152 | /* particular protocols of the protocol family PF_CAN */ 153 | #define CAN_RAW 1 /* RAW sockets */ 154 | #define CAN_BCM 2 /* Broadcast Manager */ 155 | #define CAN_TP16 3 /* VAG Transport Protocol v1.6 */ 156 | #define CAN_TP20 4 /* VAG Transport Protocol v2.0 */ 157 | #define CAN_MCNET 5 /* Bosch MCNet */ 158 | #define CAN_ISOTP 6 /* ISO 15765-2 Transport Protocol */ 159 | #define CAN_J1939 7 /* SAE J1939 */ 160 | #define CAN_NPROTO 8 161 | 162 | #define SOL_CAN_BASE 100 163 | 164 | /** 165 | * struct sockaddr_can - the sockaddr structure for CAN sockets 166 | * @can_family: address family number AF_CAN. 167 | * @can_ifindex: CAN network interface index. 168 | * @can_addr: protocol specific address information 169 | */ 170 | struct sockaddr_can { 171 | __kernel_sa_family_t can_family; 172 | int can_ifindex; 173 | union { 174 | /* transport protocol class address information (e.g. ISOTP) */ 175 | struct { 176 | canid_t rx_id; 177 | canid_t tx_id; 178 | } tp; 179 | 180 | /* J1939 address information */ 181 | struct { 182 | /* 8 byte name when using dynamic addressing */ 183 | __u64 name; 184 | /* pgn: 185 | * 8bit: PS in PDU2 case, else 0 186 | * 8bit: PF 187 | * 1bit: DP 188 | * 1bit: reserved 189 | */ 190 | __u32 pgn; 191 | 192 | /* 1byte address */ 193 | __u8 addr; 194 | } j1939; 195 | 196 | /* reserved for future CAN protocols address information */ 197 | } can_addr; 198 | }; 199 | 200 | /** 201 | * struct can_filter - CAN ID based filter in can_register(). 202 | * @can_id: relevant bits of CAN ID which are not masked out. 203 | * @can_mask: CAN mask (see description) 204 | * 205 | * Description: 206 | * A filter matches, when 207 | * 208 | * & mask == can_id & mask 209 | * 210 | * The filter can be inverted (CAN_INV_FILTER bit set in can_id) or it can 211 | * filter for error message frames (CAN_ERR_FLAG bit set in mask). 212 | */ 213 | struct can_filter { 214 | canid_t can_id; 215 | canid_t can_mask; 216 | }; 217 | 218 | #define CAN_INV_FILTER 0x20000000U /* to be set in can_filter.can_id */ 219 | 220 | #endif /* !_UAPI_CAN_H */ 221 | -------------------------------------------------------------------------------- /include/uapi/linux/can/bcm.h: -------------------------------------------------------------------------------- 1 | /* 2 | * linux/can/bcm.h 3 | * 4 | * Definitions for CAN Broadcast Manager (BCM) 5 | * 6 | * Author: Oliver Hartkopp 7 | * Copyright (c) 2002-2007 Volkswagen Group Electronic Research 8 | * All rights reserved. 9 | * 10 | * Redistribution and use in source and binary forms, with or without 11 | * modification, are permitted provided that the following conditions 12 | * are met: 13 | * 1. Redistributions of source code must retain the above copyright 14 | * notice, this list of conditions and the following disclaimer. 15 | * 2. Redistributions in binary form must reproduce the above copyright 16 | * notice, this list of conditions and the following disclaimer in the 17 | * documentation and/or other materials provided with the distribution. 18 | * 3. Neither the name of Volkswagen nor the names of its contributors 19 | * may be used to endorse or promote products derived from this software 20 | * without specific prior written permission. 21 | * 22 | * Alternatively, provided that this notice is retained in full, this 23 | * software may be distributed under the terms of the GNU General 24 | * Public License ("GPL") version 2, in which case the provisions of the 25 | * GPL apply INSTEAD OF those given above. 26 | * 27 | * The provided data structures and external interfaces from this code 28 | * are not restricted to be used by modules with a GPL compatible license. 29 | * 30 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 31 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 32 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 33 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 34 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 35 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 36 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 37 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 38 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 39 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 40 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH 41 | * DAMAGE. 42 | */ 43 | 44 | #ifndef _UAPI_CAN_BCM_H 45 | #define _UAPI_CAN_BCM_H 46 | 47 | #include 48 | #include 49 | 50 | struct bcm_timeval { 51 | long tv_sec; 52 | long tv_usec; 53 | }; 54 | 55 | /** 56 | * struct bcm_msg_head - head of messages to/from the broadcast manager 57 | * @opcode: opcode, see enum below. 58 | * @flags: special flags, see below. 59 | * @count: number of frames to send before changing interval. 60 | * @ival1: interval for the first @count frames. 61 | * @ival2: interval for the following frames. 62 | * @can_id: CAN ID of frames to be sent or received. 63 | * @nframes: number of frames appended to the message head. 64 | * @frames: array of CAN frames. 65 | */ 66 | struct bcm_msg_head { 67 | __u32 opcode; 68 | __u32 flags; 69 | __u32 count; 70 | struct bcm_timeval ival1, ival2; 71 | canid_t can_id; 72 | __u32 nframes; 73 | struct can_frame frames[0]; 74 | }; 75 | 76 | enum { 77 | TX_SETUP = 1, /* create (cyclic) transmission task */ 78 | TX_DELETE, /* remove (cyclic) transmission task */ 79 | TX_READ, /* read properties of (cyclic) transmission task */ 80 | TX_SEND, /* send one CAN frame */ 81 | RX_SETUP, /* create RX content filter subscription */ 82 | RX_DELETE, /* remove RX content filter subscription */ 83 | RX_READ, /* read properties of RX content filter subscription */ 84 | TX_STATUS, /* reply to TX_READ request */ 85 | TX_EXPIRED, /* notification on performed transmissions (count=0) */ 86 | RX_STATUS, /* reply to RX_READ request */ 87 | RX_TIMEOUT, /* cyclic message is absent */ 88 | RX_CHANGED /* updated CAN frame (detected content change) */ 89 | }; 90 | 91 | #define SETTIMER 0x0001 92 | #define STARTTIMER 0x0002 93 | #define TX_COUNTEVT 0x0004 94 | #define TX_ANNOUNCE 0x0008 95 | #define TX_CP_CAN_ID 0x0010 96 | #define RX_FILTER_ID 0x0020 97 | #define RX_CHECK_DLC 0x0040 98 | #define RX_NO_AUTOTIMER 0x0080 99 | #define RX_ANNOUNCE_RESUME 0x0100 100 | #define TX_RESET_MULTI_IDX 0x0200 101 | #define RX_RTR_FRAME 0x0400 102 | 103 | #endif /* !_UAPI_CAN_BCM_H */ 104 | -------------------------------------------------------------------------------- /include/uapi/linux/can/error.h: -------------------------------------------------------------------------------- 1 | /* 2 | * linux/can/error.h 3 | * 4 | * Definitions of the CAN error messages to be filtered and passed to the user. 5 | * 6 | * Author: Oliver Hartkopp 7 | * Copyright (c) 2002-2007 Volkswagen Group Electronic Research 8 | * All rights reserved. 9 | * 10 | * Redistribution and use in source and binary forms, with or without 11 | * modification, are permitted provided that the following conditions 12 | * are met: 13 | * 1. Redistributions of source code must retain the above copyright 14 | * notice, this list of conditions and the following disclaimer. 15 | * 2. Redistributions in binary form must reproduce the above copyright 16 | * notice, this list of conditions and the following disclaimer in the 17 | * documentation and/or other materials provided with the distribution. 18 | * 3. Neither the name of Volkswagen nor the names of its contributors 19 | * may be used to endorse or promote products derived from this software 20 | * without specific prior written permission. 21 | * 22 | * Alternatively, provided that this notice is retained in full, this 23 | * software may be distributed under the terms of the GNU General 24 | * Public License ("GPL") version 2, in which case the provisions of the 25 | * GPL apply INSTEAD OF those given above. 26 | * 27 | * The provided data structures and external interfaces from this code 28 | * are not restricted to be used by modules with a GPL compatible license. 29 | * 30 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 31 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 32 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 33 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 34 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 35 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 36 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 37 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 38 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 39 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 40 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH 41 | * DAMAGE. 42 | */ 43 | 44 | #ifndef _UAPI_CAN_ERROR_H 45 | #define _UAPI_CAN_ERROR_H 46 | 47 | #define CAN_ERR_DLC 8 /* dlc for error message frames */ 48 | 49 | /* error class (mask) in can_id */ 50 | #define CAN_ERR_TX_TIMEOUT 0x00000001U /* TX timeout (by netdevice driver) */ 51 | #define CAN_ERR_LOSTARB 0x00000002U /* lost arbitration / data[0] */ 52 | #define CAN_ERR_CRTL 0x00000004U /* controller problems / data[1] */ 53 | #define CAN_ERR_PROT 0x00000008U /* protocol violations / data[2..3] */ 54 | #define CAN_ERR_TRX 0x00000010U /* transceiver status / data[4] */ 55 | #define CAN_ERR_ACK 0x00000020U /* received no ACK on transmission */ 56 | #define CAN_ERR_BUSOFF 0x00000040U /* bus off */ 57 | #define CAN_ERR_BUSERROR 0x00000080U /* bus error (may flood!) */ 58 | #define CAN_ERR_RESTARTED 0x00000100U /* controller restarted */ 59 | 60 | /* arbitration lost in bit ... / data[0] */ 61 | #define CAN_ERR_LOSTARB_UNSPEC 0x00 /* unspecified */ 62 | /* else bit number in bitstream */ 63 | 64 | /* error status of CAN-controller / data[1] */ 65 | #define CAN_ERR_CRTL_UNSPEC 0x00 /* unspecified */ 66 | #define CAN_ERR_CRTL_RX_OVERFLOW 0x01 /* RX buffer overflow */ 67 | #define CAN_ERR_CRTL_TX_OVERFLOW 0x02 /* TX buffer overflow */ 68 | #define CAN_ERR_CRTL_RX_WARNING 0x04 /* reached warning level for RX errors */ 69 | #define CAN_ERR_CRTL_TX_WARNING 0x08 /* reached warning level for TX errors */ 70 | #define CAN_ERR_CRTL_RX_PASSIVE 0x10 /* reached error passive status RX */ 71 | #define CAN_ERR_CRTL_TX_PASSIVE 0x20 /* reached error passive status TX */ 72 | /* (at least one error counter exceeds */ 73 | /* the protocol-defined level of 127) */ 74 | #define CAN_ERR_CRTL_ACTIVE 0x40 /* recovered to error active state */ 75 | 76 | /* error in CAN protocol (type) / data[2] */ 77 | #define CAN_ERR_PROT_UNSPEC 0x00 /* unspecified */ 78 | #define CAN_ERR_PROT_BIT 0x01 /* single bit error */ 79 | #define CAN_ERR_PROT_FORM 0x02 /* frame format error */ 80 | #define CAN_ERR_PROT_STUFF 0x04 /* bit stuffing error */ 81 | #define CAN_ERR_PROT_BIT0 0x08 /* unable to send dominant bit */ 82 | #define CAN_ERR_PROT_BIT1 0x10 /* unable to send recessive bit */ 83 | #define CAN_ERR_PROT_OVERLOAD 0x20 /* bus overload */ 84 | #define CAN_ERR_PROT_ACTIVE 0x40 /* active error announcement */ 85 | #define CAN_ERR_PROT_TX 0x80 /* error occurred on transmission */ 86 | 87 | /* error in CAN protocol (location) / data[3] */ 88 | #define CAN_ERR_PROT_LOC_UNSPEC 0x00 /* unspecified */ 89 | #define CAN_ERR_PROT_LOC_SOF 0x03 /* start of frame */ 90 | #define CAN_ERR_PROT_LOC_ID28_21 0x02 /* ID bits 28 - 21 (SFF: 10 - 3) */ 91 | #define CAN_ERR_PROT_LOC_ID20_18 0x06 /* ID bits 20 - 18 (SFF: 2 - 0 )*/ 92 | #define CAN_ERR_PROT_LOC_SRTR 0x04 /* substitute RTR (SFF: RTR) */ 93 | #define CAN_ERR_PROT_LOC_IDE 0x05 /* identifier extension */ 94 | #define CAN_ERR_PROT_LOC_ID17_13 0x07 /* ID bits 17-13 */ 95 | #define CAN_ERR_PROT_LOC_ID12_05 0x0F /* ID bits 12-5 */ 96 | #define CAN_ERR_PROT_LOC_ID04_00 0x0E /* ID bits 4-0 */ 97 | #define CAN_ERR_PROT_LOC_RTR 0x0C /* RTR */ 98 | #define CAN_ERR_PROT_LOC_RES1 0x0D /* reserved bit 1 */ 99 | #define CAN_ERR_PROT_LOC_RES0 0x09 /* reserved bit 0 */ 100 | #define CAN_ERR_PROT_LOC_DLC 0x0B /* data length code */ 101 | #define CAN_ERR_PROT_LOC_DATA 0x0A /* data section */ 102 | #define CAN_ERR_PROT_LOC_CRC_SEQ 0x08 /* CRC sequence */ 103 | #define CAN_ERR_PROT_LOC_CRC_DEL 0x18 /* CRC delimiter */ 104 | #define CAN_ERR_PROT_LOC_ACK 0x19 /* ACK slot */ 105 | #define CAN_ERR_PROT_LOC_ACK_DEL 0x1B /* ACK delimiter */ 106 | #define CAN_ERR_PROT_LOC_EOF 0x1A /* end of frame */ 107 | #define CAN_ERR_PROT_LOC_INTERM 0x12 /* intermission */ 108 | 109 | /* error status of CAN-transceiver / data[4] */ 110 | /* CANH CANL */ 111 | #define CAN_ERR_TRX_UNSPEC 0x00 /* 0000 0000 */ 112 | #define CAN_ERR_TRX_CANH_NO_WIRE 0x04 /* 0000 0100 */ 113 | #define CAN_ERR_TRX_CANH_SHORT_TO_BAT 0x05 /* 0000 0101 */ 114 | #define CAN_ERR_TRX_CANH_SHORT_TO_VCC 0x06 /* 0000 0110 */ 115 | #define CAN_ERR_TRX_CANH_SHORT_TO_GND 0x07 /* 0000 0111 */ 116 | #define CAN_ERR_TRX_CANL_NO_WIRE 0x40 /* 0100 0000 */ 117 | #define CAN_ERR_TRX_CANL_SHORT_TO_BAT 0x50 /* 0101 0000 */ 118 | #define CAN_ERR_TRX_CANL_SHORT_TO_VCC 0x60 /* 0110 0000 */ 119 | #define CAN_ERR_TRX_CANL_SHORT_TO_GND 0x70 /* 0111 0000 */ 120 | #define CAN_ERR_TRX_CANL_SHORT_TO_CANH 0x80 /* 1000 0000 */ 121 | 122 | /* controller specific additional information / data[5..7] */ 123 | 124 | #endif /* _UAPI_CAN_ERROR_H */ 125 | -------------------------------------------------------------------------------- /include/uapi/linux/can/gw.h: -------------------------------------------------------------------------------- 1 | /* 2 | * linux/can/gw.h 3 | * 4 | * Definitions for CAN frame Gateway/Router/Bridge 5 | * 6 | * Author: Oliver Hartkopp 7 | * Copyright (c) 2011 Volkswagen Group Electronic Research 8 | * All rights reserved. 9 | * 10 | * Redistribution and use in source and binary forms, with or without 11 | * modification, are permitted provided that the following conditions 12 | * are met: 13 | * 1. Redistributions of source code must retain the above copyright 14 | * notice, this list of conditions and the following disclaimer. 15 | * 2. Redistributions in binary form must reproduce the above copyright 16 | * notice, this list of conditions and the following disclaimer in the 17 | * documentation and/or other materials provided with the distribution. 18 | * 3. Neither the name of Volkswagen nor the names of its contributors 19 | * may be used to endorse or promote products derived from this software 20 | * without specific prior written permission. 21 | * 22 | * Alternatively, provided that this notice is retained in full, this 23 | * software may be distributed under the terms of the GNU General 24 | * Public License ("GPL") version 2, in which case the provisions of the 25 | * GPL apply INSTEAD OF those given above. 26 | * 27 | * The provided data structures and external interfaces from this code 28 | * are not restricted to be used by modules with a GPL compatible license. 29 | * 30 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 31 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 32 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 33 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 34 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 35 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 36 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 37 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 38 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 39 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 40 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH 41 | * DAMAGE. 42 | */ 43 | 44 | #ifndef _UAPI_CAN_GW_H 45 | #define _UAPI_CAN_GW_H 46 | 47 | #include 48 | #include 49 | 50 | struct rtcanmsg { 51 | __u8 can_family; 52 | __u8 gwtype; 53 | __u16 flags; 54 | }; 55 | 56 | /* CAN gateway types */ 57 | enum { 58 | CGW_TYPE_UNSPEC, 59 | CGW_TYPE_CAN_CAN, /* CAN->CAN routing */ 60 | __CGW_TYPE_MAX 61 | }; 62 | 63 | #define CGW_TYPE_MAX (__CGW_TYPE_MAX - 1) 64 | 65 | /* CAN rtnetlink attribute definitions */ 66 | enum { 67 | CGW_UNSPEC, 68 | CGW_MOD_AND, /* CAN frame modification binary AND */ 69 | CGW_MOD_OR, /* CAN frame modification binary OR */ 70 | CGW_MOD_XOR, /* CAN frame modification binary XOR */ 71 | CGW_MOD_SET, /* CAN frame modification set alternate values */ 72 | CGW_CS_XOR, /* set data[] XOR checksum into data[index] */ 73 | CGW_CS_CRC8, /* set data[] CRC8 checksum into data[index] */ 74 | CGW_HANDLED, /* number of handled CAN frames */ 75 | CGW_DROPPED, /* number of dropped CAN frames */ 76 | CGW_SRC_IF, /* ifindex of source network interface */ 77 | CGW_DST_IF, /* ifindex of destination network interface */ 78 | CGW_FILTER, /* specify struct can_filter on source CAN device */ 79 | CGW_DELETED, /* number of deleted CAN frames (see max_hops param) */ 80 | CGW_LIM_HOPS, /* limit the number of hops of this specific rule */ 81 | CGW_MOD_UID, /* user defined identifier for modification updates */ 82 | __CGW_MAX 83 | }; 84 | 85 | #define CGW_MAX (__CGW_MAX - 1) 86 | 87 | #define CGW_FLAGS_CAN_ECHO 0x01 88 | #define CGW_FLAGS_CAN_SRC_TSTAMP 0x02 89 | #define CGW_FLAGS_CAN_IIF_TX_OK 0x04 90 | 91 | #define CGW_MOD_FUNCS 4 /* AND OR XOR SET */ 92 | 93 | /* CAN frame elements that are affected by curr. 3 CAN frame modifications */ 94 | #define CGW_MOD_ID 0x01 95 | #define CGW_MOD_DLC 0x02 96 | #define CGW_MOD_DATA 0x04 97 | 98 | #define CGW_FRAME_MODS 3 /* ID DLC DATA */ 99 | 100 | #define MAX_MODFUNCTIONS (CGW_MOD_FUNCS * CGW_FRAME_MODS) 101 | 102 | struct cgw_frame_mod { 103 | struct can_frame cf; 104 | __u8 modtype; 105 | } __attribute__((packed)); 106 | 107 | #define CGW_MODATTR_LEN sizeof(struct cgw_frame_mod) 108 | 109 | struct cgw_csum_xor { 110 | __s8 from_idx; 111 | __s8 to_idx; 112 | __s8 result_idx; 113 | __u8 init_xor_val; 114 | } __attribute__((packed)); 115 | 116 | struct cgw_csum_crc8 { 117 | __s8 from_idx; 118 | __s8 to_idx; 119 | __s8 result_idx; 120 | __u8 init_crc_val; 121 | __u8 final_xor_val; 122 | __u8 crctab[256]; 123 | __u8 profile; 124 | __u8 profile_data[20]; 125 | } __attribute__((packed)); 126 | 127 | /* length of checksum operation parameters. idx = index in CAN frame data[] */ 128 | #define CGW_CS_XOR_LEN sizeof(struct cgw_csum_xor) 129 | #define CGW_CS_CRC8_LEN sizeof(struct cgw_csum_crc8) 130 | 131 | /* CRC8 profiles (compute CRC for additional data elements - see below) */ 132 | enum { 133 | CGW_CRC8PRF_UNSPEC, 134 | CGW_CRC8PRF_1U8, /* compute one additional u8 value */ 135 | CGW_CRC8PRF_16U8, /* u8 value table indexed by data[1] & 0xF */ 136 | CGW_CRC8PRF_SFFID_XOR, /* (can_id & 0xFF) ^ (can_id >> 8 & 0xFF) */ 137 | __CGW_CRC8PRF_MAX 138 | }; 139 | 140 | #define CGW_CRC8PRF_MAX (__CGW_CRC8PRF_MAX - 1) 141 | 142 | /* 143 | * CAN rtnetlink attribute contents in detail 144 | * 145 | * CGW_XXX_IF (length 4 bytes): 146 | * Sets an interface index for source/destination network interfaces. 147 | * For the CAN->CAN gwtype the indices of _two_ CAN interfaces are mandatory. 148 | * 149 | * CGW_FILTER (length 8 bytes): 150 | * Sets a CAN receive filter for the gateway job specified by the 151 | * struct can_filter described in include/linux/can.h 152 | * 153 | * CGW_MOD_(AND|OR|XOR|SET) (length 17 bytes): 154 | * Specifies a modification that's done to a received CAN frame before it is 155 | * send out to the destination interface. 156 | * 157 | * data used as operator 158 | * affected CAN frame elements 159 | * 160 | * CGW_LIM_HOPS (length 1 byte): 161 | * Limit the number of hops of this specific rule. Usually the received CAN 162 | * frame can be processed as much as 'max_hops' times (which is given at module 163 | * load time of the can-gw module). This value is used to reduce the number of 164 | * possible hops for this gateway rule to a value smaller then max_hops. 165 | * 166 | * CGW_MOD_UID (length 4 bytes): 167 | * Optional non-zero user defined routing job identifier to alter existing 168 | * modification settings at runtime. 169 | * 170 | * CGW_CS_XOR (length 4 bytes): 171 | * Set a simple XOR checksum starting with an initial value into 172 | * data[result-idx] using data[start-idx] .. data[end-idx] 173 | * 174 | * The XOR checksum is calculated like this: 175 | * 176 | * xor = init_xor_val 177 | * 178 | * for (i = from_idx .. to_idx) 179 | * xor ^= can_frame.data[i] 180 | * 181 | * can_frame.data[ result_idx ] = xor 182 | * 183 | * CGW_CS_CRC8 (length 282 bytes): 184 | * Set a CRC8 value into data[result-idx] using a given 256 byte CRC8 table, 185 | * a given initial value and a defined input data[start-idx] .. data[end-idx]. 186 | * Finally the result value is XOR'ed with the final_xor_val. 187 | * 188 | * The CRC8 checksum is calculated like this: 189 | * 190 | * crc = init_crc_val 191 | * 192 | * for (i = from_idx .. to_idx) 193 | * crc = crctab[ crc ^ can_frame.data[i] ] 194 | * 195 | * can_frame.data[ result_idx ] = crc ^ final_xor_val 196 | * 197 | * The calculated CRC may contain additional source data elements that can be 198 | * defined in the handling of 'checksum profiles' e.g. shown in AUTOSAR specs 199 | * like http://www.autosar.org/download/R4.0/AUTOSAR_SWS_E2ELibrary.pdf 200 | * E.g. the profile_data[] may contain additional u8 values (called DATA_IDs) 201 | * that are used depending on counter values inside the CAN frame data[]. 202 | * So far only three profiles have been implemented for illustration. 203 | * 204 | * Remark: In general the attribute data is a linear buffer. 205 | * Beware of sending unpacked or aligned structs! 206 | */ 207 | 208 | #endif /* !_UAPI_CAN_GW_H */ 209 | -------------------------------------------------------------------------------- /include/uapi/linux/can/j1939.h: -------------------------------------------------------------------------------- 1 | /* 2 | * j1939.h 3 | * 4 | * Copyright (c) 2010-2011 EIA Electronics 5 | * 6 | * This program is free software; you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License version 2 as 8 | * published by the Free Software Foundation. 9 | */ 10 | 11 | #ifndef _UAPI_CAN_J1939_H_ 12 | #define _UAPI_CAN_J1939_H_ 13 | 14 | #include 15 | #include 16 | #include 17 | 18 | #define J1939_IDLE_ADDR 0xfe 19 | #define J1939_NO_ADDR 0xff 20 | #define J1939_NO_NAME 0 21 | #define J1939_NO_PGN 0x40000 22 | 23 | /* J1939 Parameter Group Number 24 | * 25 | * bit 0-7 : PDU Specific (PS) 26 | * bit 8-15 : PDU Format (PF) 27 | * bit 16 : Data Page (DP) 28 | * bit 17 : Reserved (R) 29 | * bit 19-31 : set to zero 30 | */ 31 | typedef __u32 pgn_t; 32 | 33 | /* J1939 Priority 34 | * 35 | * bit 0-2 : Priority (P) 36 | * bit 3-7 : set to zero 37 | */ 38 | typedef __u8 priority_t; 39 | 40 | /* J1939 NAME 41 | * 42 | * bit 0-20 : Identity Number 43 | * bit 21-31 : Manufacturer Code 44 | * bit 32-34 : ECU Instance 45 | * bit 35-39 : Function Instance 46 | * bit 40-47 : Function 47 | * bit 48 : Reserved 48 | * bit 49-55 : Vehicle System 49 | * bit 56-59 : Vehicle System Instance 50 | * bit 60-62 : Industry Group 51 | * bit 63 : Arbitrary Address Capable 52 | */ 53 | typedef __u64 name_t; 54 | 55 | /* J1939 socket options */ 56 | #define SOL_CAN_J1939 (SOL_CAN_BASE + CAN_J1939) 57 | enum { 58 | SO_J1939_FILTER = 1, /* set filters */ 59 | SO_J1939_PROMISC = 2, /* set/clr promiscuous mode */ 60 | SO_J1939_RECV_OWN = 3, 61 | SO_J1939_SEND_PRIO = 4, 62 | }; 63 | 64 | enum { 65 | SCM_J1939_DEST_ADDR = 1, 66 | SCM_J1939_DEST_NAME = 2, 67 | SCM_J1939_PRIO = 3, 68 | }; 69 | 70 | struct j1939_filter { 71 | name_t name; 72 | name_t name_mask; 73 | __u8 addr; 74 | __u8 addr_mask; 75 | pgn_t pgn; 76 | pgn_t pgn_mask; 77 | }; 78 | 79 | /* RTNETLINK */ 80 | enum { 81 | IFLA_J1939_UNSPEC, 82 | IFLA_J1939_ENABLE, 83 | IFLA_J1939_MAX, 84 | }; 85 | 86 | enum { 87 | IFA_J1939_UNSPEC, 88 | IFA_J1939_ADDR, 89 | IFA_J1939_NAME, 90 | IFA_J1939_MAX, 91 | }; 92 | 93 | #endif /* !_UAPI_CAN_J1939_H_ */ 94 | -------------------------------------------------------------------------------- /include/uapi/linux/can/netlink.h: -------------------------------------------------------------------------------- 1 | /* 2 | * linux/can/netlink.h 3 | * 4 | * Definitions for the CAN netlink interface 5 | * 6 | * Copyright (c) 2009 Wolfgang Grandegger 7 | * 8 | * This program is free software; you can redistribute it and/or modify 9 | * it under the terms of the version 2 of the GNU General Public License 10 | * as published by the Free Software Foundation 11 | * 12 | * This program is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU General Public License for more details. 16 | */ 17 | 18 | #ifndef _UAPI_CAN_NETLINK_H 19 | #define _UAPI_CAN_NETLINK_H 20 | 21 | #include 22 | 23 | /* 24 | * CAN bit-timing parameters 25 | * 26 | * For further information, please read chapter "8 BIT TIMING 27 | * REQUIREMENTS" of the "Bosch CAN Specification version 2.0" 28 | * at http://www.semiconductors.bosch.de/pdf/can2spec.pdf. 29 | */ 30 | struct can_bittiming { 31 | __u32 bitrate; /* Bit-rate in bits/second */ 32 | __u32 sample_point; /* Sample point in one-tenth of a percent */ 33 | __u32 tq; /* Time quanta (TQ) in nanoseconds */ 34 | __u32 prop_seg; /* Propagation segment in TQs */ 35 | __u32 phase_seg1; /* Phase buffer segment 1 in TQs */ 36 | __u32 phase_seg2; /* Phase buffer segment 2 in TQs */ 37 | __u32 sjw; /* Synchronisation jump width in TQs */ 38 | __u32 brp; /* Bit-rate prescaler */ 39 | }; 40 | 41 | /* 42 | * CAN harware-dependent bit-timing constant 43 | * 44 | * Used for calculating and checking bit-timing parameters 45 | */ 46 | struct can_bittiming_const { 47 | char name[16]; /* Name of the CAN controller hardware */ 48 | __u32 tseg1_min; /* Time segement 1 = prop_seg + phase_seg1 */ 49 | __u32 tseg1_max; 50 | __u32 tseg2_min; /* Time segement 2 = phase_seg2 */ 51 | __u32 tseg2_max; 52 | __u32 sjw_max; /* Synchronisation jump width */ 53 | __u32 brp_min; /* Bit-rate prescaler */ 54 | __u32 brp_max; 55 | __u32 brp_inc; 56 | }; 57 | 58 | /* 59 | * CAN clock parameters 60 | */ 61 | struct can_clock { 62 | __u32 freq; /* CAN system clock frequency in Hz */ 63 | }; 64 | 65 | /* 66 | * CAN operational and error states 67 | */ 68 | enum can_state { 69 | CAN_STATE_ERROR_ACTIVE = 0, /* RX/TX error count < 96 */ 70 | CAN_STATE_ERROR_WARNING, /* RX/TX error count < 128 */ 71 | CAN_STATE_ERROR_PASSIVE, /* RX/TX error count < 256 */ 72 | CAN_STATE_BUS_OFF, /* RX/TX error count >= 256 */ 73 | CAN_STATE_STOPPED, /* Device is stopped */ 74 | CAN_STATE_SLEEPING, /* Device is sleeping */ 75 | CAN_STATE_MAX 76 | }; 77 | 78 | /* 79 | * CAN bus error counters 80 | */ 81 | struct can_berr_counter { 82 | __u16 txerr; 83 | __u16 rxerr; 84 | }; 85 | 86 | /* 87 | * CAN controller mode 88 | */ 89 | struct can_ctrlmode { 90 | __u32 mask; 91 | __u32 flags; 92 | }; 93 | 94 | #define CAN_CTRLMODE_LOOPBACK 0x01 /* Loopback mode */ 95 | #define CAN_CTRLMODE_LISTENONLY 0x02 /* Listen-only mode */ 96 | #define CAN_CTRLMODE_3_SAMPLES 0x04 /* Triple sampling mode */ 97 | #define CAN_CTRLMODE_ONE_SHOT 0x08 /* One-Shot mode */ 98 | #define CAN_CTRLMODE_BERR_REPORTING 0x10 /* Bus-error reporting */ 99 | #define CAN_CTRLMODE_FD 0x20 /* CAN FD mode */ 100 | #define CAN_CTRLMODE_PRESUME_ACK 0x40 /* Ignore missing CAN ACKs */ 101 | #define CAN_CTRLMODE_FD_NON_ISO 0x80 /* CAN FD in non-ISO mode */ 102 | 103 | /* 104 | * CAN device statistics 105 | */ 106 | struct can_device_stats { 107 | __u32 bus_error; /* Bus errors */ 108 | __u32 error_warning; /* Changes to error warning state */ 109 | __u32 error_passive; /* Changes to error passive state */ 110 | __u32 bus_off; /* Changes to bus off state */ 111 | __u32 arbitration_lost; /* Arbitration lost errors */ 112 | __u32 restarts; /* CAN controller re-starts */ 113 | }; 114 | 115 | /* 116 | * CAN netlink interface 117 | */ 118 | enum { 119 | IFLA_CAN_UNSPEC, 120 | IFLA_CAN_BITTIMING, 121 | IFLA_CAN_BITTIMING_CONST, 122 | IFLA_CAN_CLOCK, 123 | IFLA_CAN_STATE, 124 | IFLA_CAN_CTRLMODE, 125 | IFLA_CAN_RESTART_MS, 126 | IFLA_CAN_RESTART, 127 | IFLA_CAN_BERR_COUNTER, 128 | IFLA_CAN_DATA_BITTIMING, 129 | IFLA_CAN_DATA_BITTIMING_CONST, 130 | __IFLA_CAN_MAX 131 | }; 132 | 133 | #define IFLA_CAN_MAX (__IFLA_CAN_MAX - 1) 134 | 135 | #endif /* !_UAPI_CAN_NETLINK_H */ 136 | -------------------------------------------------------------------------------- /include/uapi/linux/can/raw.h: -------------------------------------------------------------------------------- 1 | /* 2 | * linux/can/raw.h 3 | * 4 | * Definitions for raw CAN sockets 5 | * 6 | * Authors: Oliver Hartkopp 7 | * Urs Thuermann 8 | * Copyright (c) 2002-2007 Volkswagen Group Electronic Research 9 | * All rights reserved. 10 | * 11 | * Redistribution and use in source and binary forms, with or without 12 | * modification, are permitted provided that the following conditions 13 | * are met: 14 | * 1. Redistributions of source code must retain the above copyright 15 | * notice, this list of conditions and the following disclaimer. 16 | * 2. Redistributions in binary form must reproduce the above copyright 17 | * notice, this list of conditions and the following disclaimer in the 18 | * documentation and/or other materials provided with the distribution. 19 | * 3. Neither the name of Volkswagen nor the names of its contributors 20 | * may be used to endorse or promote products derived from this software 21 | * without specific prior written permission. 22 | * 23 | * Alternatively, provided that this notice is retained in full, this 24 | * software may be distributed under the terms of the GNU General 25 | * Public License ("GPL") version 2, in which case the provisions of the 26 | * GPL apply INSTEAD OF those given above. 27 | * 28 | * The provided data structures and external interfaces from this code 29 | * are not restricted to be used by modules with a GPL compatible license. 30 | * 31 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 32 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 33 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 34 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 35 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 36 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 37 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 38 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 39 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 40 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 41 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH 42 | * DAMAGE. 43 | */ 44 | 45 | #ifndef _UAPI_CAN_RAW_H 46 | #define _UAPI_CAN_RAW_H 47 | 48 | #include 49 | 50 | #define SOL_CAN_RAW (SOL_CAN_BASE + CAN_RAW) 51 | 52 | /* for socket options affecting the socket (not the global system) */ 53 | 54 | enum { 55 | CAN_RAW_FILTER = 1, /* set 0 .. n can_filter(s) */ 56 | CAN_RAW_ERR_FILTER, /* set filter for error frames */ 57 | CAN_RAW_LOOPBACK, /* local loopback (default:on) */ 58 | CAN_RAW_RECV_OWN_MSGS, /* receive my own msgs (default:off) */ 59 | CAN_RAW_FD_FRAMES, /* allow CAN FD frames (default:off) */ 60 | CAN_RAW_JOIN_FILTERS, /* all filters must match to trigger */ 61 | }; 62 | 63 | #endif /* !_UAPI_CAN_RAW_H */ 64 | -------------------------------------------------------------------------------- /page.theme: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | page: <?theme title?> 5 | 6 | 12 | 13 | 14 |
15 | 16 |
17 | 18 | 19 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /style.css: -------------------------------------------------------------------------------- 1 | * { 2 | font-family: Helvetica Neue, Helvetica, Arial, sans-serif; 3 | } 4 | body { 5 | max-width: 60em; 6 | margin: 0 auto; 7 | color: #111; 8 | } 9 | 10 | pre, code { 11 | font-family: Monaco, Courier New, monospace; 12 | font-size: 11px; 13 | } 14 | 15 | h1 { 16 | color: rgb(43,105,145); 17 | font-weight: bold; 18 | font-size: 40px; 19 | letter-spacing: -1px; 20 | margin-bottom: -5px; 21 | margin: 0; 22 | } 23 | h1 code { 24 | font-size: 32px; 25 | } 26 | 27 | h2 { 28 | color: rgb(43,105,145); 29 | font-weight: bold; 30 | margin-bottom: -5px; 31 | } 32 | h2 code { 33 | font-size: 22px; 34 | } 35 | 36 | h3 { 37 | margin-bottom: -5px; 38 | } 39 | h3 code { 40 | font-size: 16px; 41 | } 42 | 43 | a { 44 | color: blue; 45 | text-decoration: none; 46 | } 47 | a:visited { 48 | color: navy; 49 | } 50 | a:hover { 51 | text-decoration: underline; 52 | } 53 | 54 | pre { 55 | border-width: 1px; 56 | border-color: #777; 57 | border-style: solid; 58 | padding: 0.5em; 59 | background-color: #ccc; 60 | overflow: auto; 61 | color: #000; 62 | font-weight: bold; 63 | } 64 | 65 | p, li { 66 | font-size: 13px; 67 | line-height: 18px; 68 | } 69 | -------------------------------------------------------------------------------- /testj1939.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2013 EIA Electronics 3 | * 4 | * Authors: 5 | * Kurt Van Dijck 6 | * 7 | * This program is free software; you can redistribute it and/or modify 8 | * it under the terms of the version 2 of the GNU General Public License 9 | * as published by the Free Software Foundation 10 | */ 11 | 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include 18 | #include 19 | 20 | #include 21 | #include 22 | #include 23 | #include 24 | #include 25 | #include 26 | #include 27 | #include 28 | 29 | static const char help_msg[] = 30 | "testj1939: demonstrate j1939 use\n" 31 | "Usage: testj1939 FROM TO\n" 32 | " FROM / TO - or [IFACE][:[SA][,[PGN][,NAME]]]\n" 33 | "Options:\n" 34 | " -v Print relevant API calls\n" 35 | " -s[=LEN] Initial send of LEN bytes dummy data\n" 36 | " -r Receive (and print) data\n" 37 | " -e Echo incoming packets back\n" 38 | " This atually receives packets\n" 39 | " -c Issue connect()\n" 40 | " -p=PRIO Set priority to PRIO\n" 41 | " -n Emit 64bit NAMEs in output\n" 42 | " -w[TIME] Return after TIME (default 1) seconds\n" 43 | "\n" 44 | "Example:\n" 45 | "testj1939 can1 20\n" 46 | "\n" 47 | ; 48 | 49 | static const char optstring[] = "?vs::rep:cnw::"; 50 | 51 | static void parse_canaddr(char *spec, struct sockaddr_can *paddr) 52 | { 53 | char *str; 54 | 55 | str = strsep(&spec, ":"); 56 | if (strlen(str)) 57 | paddr->can_ifindex = if_nametoindex(str); 58 | 59 | str = strsep(&spec, ","); 60 | if (str && strlen(str)) 61 | paddr->can_addr.j1939.addr = strtoul(str, NULL, 0); 62 | 63 | str = strsep(&spec, ","); 64 | if (str && strlen(str)) 65 | paddr->can_addr.j1939.pgn = strtoul(str, NULL, 0); 66 | 67 | str = strsep(&spec, ","); 68 | if (str && strlen(str)) 69 | paddr->can_addr.j1939.name = strtoul(str, NULL, 0); 70 | } 71 | 72 | static const char *canaddr2str(const struct sockaddr_can *paddr) 73 | { 74 | static char buf[128]; 75 | char *str = buf; 76 | char ifname[IF_NAMESIZE]; 77 | 78 | if (paddr->can_ifindex) 79 | str += sprintf(str, "%s", if_indextoname(paddr->can_ifindex, ifname)); 80 | *str++ = ':'; 81 | 82 | if (paddr->can_addr.j1939.addr != J1939_NO_ADDR) 83 | str += sprintf(str, "%02x", paddr->can_addr.j1939.addr); 84 | *str++ = ','; 85 | if (paddr->can_addr.j1939.pgn != J1939_NO_PGN) 86 | str += sprintf(str, "%05x", paddr->can_addr.j1939.pgn); 87 | *str++ = ','; 88 | if (paddr->can_addr.j1939.name != J1939_NO_NAME) 89 | str += sprintf(str, "%016llx", paddr->can_addr.j1939.name); 90 | *str++ = 0; 91 | return buf; 92 | } 93 | 94 | static void onsigalrm(int sig) 95 | { 96 | error(0, 0, "exit as requested"); 97 | exit(0); 98 | } 99 | 100 | static void schedule_oneshot_itimer(double delay) 101 | { 102 | struct itimerval it = {}; 103 | 104 | it.it_value.tv_sec = delay; 105 | it.it_value.tv_usec = (long)(delay * 1e6) % 1000000; 106 | if (setitimer(ITIMER_REAL, &it, NULL) < 0) 107 | error(1, errno, "schedule itimer %.3lfs", delay); 108 | } 109 | 110 | /* main */ 111 | int main(int argc, char *argv[]) 112 | { 113 | int ret, sock, opt, j, verbose; 114 | socklen_t peernamelen; 115 | struct sockaddr_can sockname = { 116 | .can_family = AF_CAN, 117 | .can_addr.j1939 = { 118 | .addr = J1939_NO_ADDR, 119 | .name = J1939_NO_NAME, 120 | .pgn = J1939_NO_PGN, 121 | }, 122 | }, peername = { 123 | .can_family = AF_CAN, 124 | .can_addr.j1939 = { 125 | .addr = J1939_NO_ADDR, 126 | .name = J1939_NO_NAME, 127 | .pgn = J1939_NO_PGN, 128 | }, 129 | }; 130 | uint8_t dat[128]; 131 | int valid_peername = 0; 132 | int todo_send = 0, todo_recv = 0, todo_echo = 0, todo_prio = -1; 133 | int todo_connect = 0, todo_names = 0, todo_wait = 0; 134 | 135 | /* argument parsing */ 136 | while ((opt = getopt(argc, argv, optstring)) != -1) 137 | switch (opt) { 138 | case 'v': 139 | verbose = 1; 140 | break; 141 | case 's': 142 | todo_send = strtoul(optarg ?: "8", NULL, 0); 143 | break; 144 | case 'r': 145 | todo_recv = 1; 146 | break; 147 | case 'e': 148 | todo_echo = 1; 149 | break; 150 | case 'p': 151 | todo_prio = strtoul(optarg, NULL, 0); 152 | break; 153 | case 'c': 154 | todo_connect = 1; 155 | break; 156 | case 'n': 157 | todo_names = 1; 158 | break; 159 | case 'w': 160 | schedule_oneshot_itimer(strtod(optarg ?: "1", NULL)); 161 | signal(SIGALRM, onsigalrm); 162 | todo_wait = 1; 163 | break; 164 | default: 165 | fputs(help_msg, stderr); 166 | exit(1); 167 | break; 168 | } 169 | 170 | if (argv[optind]) { 171 | if (strcmp("-", argv[optind])) 172 | parse_canaddr(argv[optind], &sockname); 173 | ++optind; 174 | } 175 | 176 | if (argv[optind]) { 177 | if (strcmp("-", argv[optind])) { 178 | parse_canaddr(argv[optind], &peername); 179 | valid_peername = 1; 180 | } 181 | ++optind; 182 | } 183 | 184 | /* open socket */ 185 | if (verbose) 186 | fprintf(stderr, "- socket(PF_CAN, SOCK_DGRAM, CAN_J1939);\n"); 187 | sock = ret = socket(PF_CAN, SOCK_DGRAM, CAN_J1939); 188 | if (ret < 0) 189 | error(1, errno, "socket(j1939)"); 190 | 191 | if (todo_prio >= 0) { 192 | if (verbose) 193 | fprintf(stderr, "- setsockopt(, SOL_CAN_J1939, SO_J1939_SEND_PRIO, &%i);\n", todo_prio); 194 | ret = setsockopt(sock, SOL_CAN_J1939, SO_J1939_SEND_PRIO, 195 | &todo_prio, sizeof(todo_prio)); 196 | if (ret < 0) 197 | error(1, errno, "set priority %i", todo_prio); 198 | } 199 | 200 | if (verbose) 201 | fprintf(stderr, "- bind(, %s, %zi);\n", canaddr2str(&sockname), sizeof(sockname)); 202 | ret = bind(sock, (void *)&sockname, sizeof(sockname)); 203 | if (ret < 0) 204 | error(1, errno, "bind()"); 205 | 206 | if (todo_connect) { 207 | if (!valid_peername) 208 | error(1, 0, "no peername supplied"); 209 | if (verbose) 210 | fprintf(stderr, "- connect(, %s, %zi);\n", canaddr2str(&peername), sizeof(peername)); 211 | ret = connect(sock, (void *)&peername, sizeof(peername)); 212 | if (ret < 0) 213 | error(1, errno, "connect()"); 214 | } 215 | 216 | if (todo_send) { 217 | /* initialize test vector */ 218 | for (j = 0; j < sizeof(dat); ++j) 219 | dat[j] = ((2*j) << 4) + ((2*j+1) & 0xf); 220 | 221 | /* send data */ 222 | /* 223 | * when using connect, do not provide additional 224 | * destination information and use send() 225 | */ 226 | if (valid_peername && !todo_connect) { 227 | if (verbose) 228 | fprintf(stderr, "- sendto(, , %i, 0, %s, %zi);\n", todo_send, canaddr2str(&peername), sizeof(peername)); 229 | ret = sendto(sock, dat, todo_send, 0, 230 | (void *)&peername, sizeof(peername)); 231 | } else { 232 | /* 233 | * we may do sendto(sock, dat, todo_send, 0, NULL, 0) 234 | * as well, but using send() demonstrates the API better 235 | */ 236 | if (verbose) 237 | fprintf(stderr, "- send(, , %i, 0);\n", todo_send); 238 | ret = send(sock, dat, todo_send, 0); 239 | } 240 | 241 | if (ret < 0) 242 | error(1, errno, "sendto"); 243 | } 244 | 245 | /* main loop */ 246 | if ((todo_echo || todo_recv) && verbose) 247 | fprintf(stderr, "- while (1)\n"); 248 | while (todo_echo || todo_recv) { 249 | /* 250 | * re-use peername for storing the sender's peername of 251 | * received packets 252 | */ 253 | if (verbose) 254 | fprintf(stderr, "- recvfrom(, , %zi, 0, &, %zi);\n", sizeof(peername), sizeof(peername)); 255 | peernamelen = sizeof(peername); 256 | ret = recvfrom(sock, dat, sizeof(dat), 0, 257 | (void *)&peername, &peernamelen); 258 | if (ret < 0) { 259 | if (EINTR == errno) { 260 | if (verbose) 261 | fprintf(stderr, "-\t\n"); 262 | continue; 263 | } 264 | error(1, errno, "recvfrom()"); 265 | } 266 | 267 | if (todo_echo) { 268 | if (verbose) 269 | fprintf(stderr, "- sendto(, , %i, 0, %s, %i);\n", ret, canaddr2str(&peername), peernamelen); 270 | ret = sendto(sock, dat, ret, 0, 271 | (void *)&peername, peernamelen); 272 | if (ret < 0) 273 | error(1, errno, "sendto"); 274 | } 275 | if (todo_recv) { 276 | if (todo_names && peername.can_addr.j1939.name) 277 | printf("%016llx ", peername.can_addr.j1939.name); 278 | printf("%02x %05x:", peername.can_addr.j1939.addr, 279 | peername.can_addr.j1939.pgn); 280 | for (j = 0; j < ret; ++j) 281 | printf(" %02x", dat[j]); 282 | printf("\n"); 283 | } 284 | } 285 | if (todo_wait) 286 | for (;;) 287 | sleep(1); 288 | return 0; 289 | } 290 | 291 | --------------------------------------------------------------------------------