Repository: devcoons/iso15765-canbus
Branch: master
Commit: 18c2fe0c1489
Files: 12
Total size: 106.3 KB
Directory structure:
gitextract_hntsohq7/
├── .github/
│ ├── FUNDING.yml
│ └── workflows/
│ └── build.yml
├── CMakeLists.txt
├── LICENSE
├── Makefile
├── README.md
├── doc/
│ └── .gitkeep
├── exm/
│ └── ISO15765Development.c
├── lib/
│ ├── lib_iqueue.c
│ └── lib_iqueue.h
└── src/
├── lib_iso15765.c
└── lib_iso15765.h
================================================
FILE CONTENTS
================================================
================================================
FILE: .github/FUNDING.yml
================================================
# These are supported funding model platforms
github: [devcoons]
================================================
FILE: .github/workflows/build.yml
================================================
name: C/C++ CI
on:
push:
branches:
# Push events on master branch
- master
pull_request:
branches:
# Push events on master branch
- master
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: make
run: make
- name: Get current date
id: date
run: echo "::set-output name=date::$(date +'%Y-%m-%d')"
- name: Create Release
id: create_release
uses: actions/create-release@v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
tag_name: ${{steps.date.outputs.date}}
release_name: ${{steps.date.outputs.date}}
draft: false
prerelease: false
- name: make clean
run: make clean
================================================
FILE: CMakeLists.txt
================================================
cmake_minimum_required(VERSION 3.10)
project(iso15765_canbus C)
set(SRC_DIR src)
set(LIB_DIR lib)
set(EXM_DIR exm)
include_directories(${SRC_DIR} ${LIB_DIR} ${EXM_DIR})
# Add the dependency library (libiqueue)
file(GLOB LIB_FILES "${LIB_DIR}/*.c")
add_library(iqueue STATIC ${LIB_FILES})
# Add the main library (libiso15765)
file(GLOB SRC_FILES "${SRC_DIR}/*.c")
add_library(iso15765 STATIC ${SRC_FILES})
target_link_libraries(iso15765 PRIVATE iqueue)
# Add the example executable
file(GLOB EXM_FILES "${EXM_DIR}/*.c")
add_executable(example ${EXM_FILES})
target_link_libraries(example PRIVATE iso15765 iqueue)
set_target_properties(iqueue iso15765 example PROPERTIES
ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/build"
LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/build"
RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/build"
)
if(MSVC)
target_compile_options(iqueue PRIVATE /W4)
target_compile_options(iso15765 PRIVATE /W4)
target_compile_options(example PRIVATE /W4)
else()
target_compile_options(iqueue PRIVATE -Wall -Wextra)
target_compile_options(iso15765 PRIVATE -Wall -Wextra)
target_compile_options(example PRIVATE -Wall -Wextra)
endif()
================================================
FILE: LICENSE
================================================
GNU AFFERO GENERAL PUBLIC LICENSE
Version 3, 19 November 2007
Copyright (C) 2007 Free Software Foundation, Inc. <https://fsf.org/>
Everyone is permitted to copy and distribute verbatim copies
of this license document, but changing it is not allowed.
Preamble
The GNU Affero General Public License is a free, copyleft license for
software and other kinds of works, specifically designed to ensure
cooperation with the community in the case of network server software.
The licenses for most software and other practical works are designed
to take away your freedom to share and change the works. By contrast,
our General Public Licenses are intended to guarantee your freedom to
share and change all versions of a program--to make sure it remains free
software for all its users.
When we speak of free software, we are referring to freedom, not
price. Our General Public Licenses are designed to make sure that you
have the freedom to distribute copies of free software (and charge for
them if you wish), that you receive source code or can get it if you
want it, that you can change the software or use pieces of it in new
free programs, and that you know you can do these things.
Developers that use our General Public Licenses protect your rights
with two steps: (1) assert copyright on the software, and (2) offer
you this License which gives you legal permission to copy, distribute
and/or modify the software.
A secondary benefit of defending all users' freedom is that
improvements made in alternate versions of the program, if they
receive widespread use, become available for other developers to
incorporate. Many developers of free software are heartened and
encouraged by the resulting cooperation. However, in the case of
software used on network servers, this result may fail to come about.
The GNU General Public License permits making a modified version and
letting the public access it on a server without ever releasing its
source code to the public.
The GNU Affero General Public License is designed specifically to
ensure that, in such cases, the modified source code becomes available
to the community. It requires the operator of a network server to
provide the source code of the modified version running there to the
users of that server. Therefore, public use of a modified version, on
a publicly accessible server, gives the public access to the source
code of the modified version.
An older license, called the Affero General Public License and
published by Affero, was designed to accomplish similar goals. This is
a different license, not a version of the Affero GPL, but Affero has
released a new version of the Affero GPL which permits relicensing under
this license.
The precise terms and conditions for copying, distribution and
modification follow.
TERMS AND CONDITIONS
0. Definitions.
"This License" refers to version 3 of the GNU Affero General Public License.
"Copyright" also means copyright-like laws that apply to other kinds of
works, such as semiconductor masks.
"The Program" refers to any copyrightable work licensed under this
License. Each licensee is addressed as "you". "Licensees" and
"recipients" may be individuals or organizations.
To "modify" a work means to copy from or adapt all or part of the work
in a fashion requiring copyright permission, other than the making of an
exact copy. The resulting work is called a "modified version" of the
earlier work or a work "based on" the earlier work.
A "covered work" means either the unmodified Program or a work based
on the Program.
To "propagate" a work means to do anything with it that, without
permission, would make you directly or secondarily liable for
infringement under applicable copyright law, except executing it on a
computer or modifying a private copy. Propagation includes copying,
distribution (with or without modification), making available to the
public, and in some countries other activities as well.
To "convey" a work means any kind of propagation that enables other
parties to make or receive copies. Mere interaction with a user through
a computer network, with no transfer of a copy, is not conveying.
An interactive user interface displays "Appropriate Legal Notices"
to the extent that it includes a convenient and prominently visible
feature that (1) displays an appropriate copyright notice, and (2)
tells the user that there is no warranty for the work (except to the
extent that warranties are provided), that licensees may convey the
work under this License, and how to view a copy of this License. If
the interface presents a list of user commands or options, such as a
menu, a prominent item in the list meets this criterion.
1. Source Code.
The "source code" for a work means the preferred form of the work
for making modifications to it. "Object code" means any non-source
form of a work.
A "Standard Interface" means an interface that either is an official
standard defined by a recognized standards body, or, in the case of
interfaces specified for a particular programming language, one that
is widely used among developers working in that language.
The "System Libraries" of an executable work include anything, other
than the work as a whole, that (a) is included in the normal form of
packaging a Major Component, but which is not part of that Major
Component, and (b) serves only to enable use of the work with that
Major Component, or to implement a Standard Interface for which an
implementation is available to the public in source code form. A
"Major Component", in this context, means a major essential component
(kernel, window system, and so on) of the specific operating system
(if any) on which the executable work runs, or a compiler used to
produce the work, or an object code interpreter used to run it.
The "Corresponding Source" for a work in object code form means all
the source code needed to generate, install, and (for an executable
work) run the object code and to modify the work, including scripts to
control those activities. However, it does not include the work's
System Libraries, or general-purpose tools or generally available free
programs which are used unmodified in performing those activities but
which are not part of the work. For example, Corresponding Source
includes interface definition files associated with source files for
the work, and the source code for shared libraries and dynamically
linked subprograms that the work is specifically designed to require,
such as by intimate data communication or control flow between those
subprograms and other parts of the work.
The Corresponding Source need not include anything that users
can regenerate automatically from other parts of the Corresponding
Source.
The Corresponding Source for a work in source code form is that
same work.
2. Basic Permissions.
All rights granted under this License are granted for the term of
copyright on the Program, and are irrevocable provided the stated
conditions are met. This License explicitly affirms your unlimited
permission to run the unmodified Program. The output from running a
covered work is covered by this License only if the output, given its
content, constitutes a covered work. This License acknowledges your
rights of fair use or other equivalent, as provided by copyright law.
You may make, run and propagate covered works that you do not
convey, without conditions so long as your license otherwise remains
in force. You may convey covered works to others for the sole purpose
of having them make modifications exclusively for you, or provide you
with facilities for running those works, provided that you comply with
the terms of this License in conveying all material for which you do
not control copyright. Those thus making or running the covered works
for you must do so exclusively on your behalf, under your direction
and control, on terms that prohibit them from making any copies of
your copyrighted material outside their relationship with you.
Conveying under any other circumstances is permitted solely under
the conditions stated below. Sublicensing is not allowed; section 10
makes it unnecessary.
3. Protecting Users' Legal Rights From Anti-Circumvention Law.
No covered work shall be deemed part of an effective technological
measure under any applicable law fulfilling obligations under article
11 of the WIPO copyright treaty adopted on 20 December 1996, or
similar laws prohibiting or restricting circumvention of such
measures.
When you convey a covered work, you waive any legal power to forbid
circumvention of technological measures to the extent such circumvention
is effected by exercising rights under this License with respect to
the covered work, and you disclaim any intention to limit operation or
modification of the work as a means of enforcing, against the work's
users, your or third parties' legal rights to forbid circumvention of
technological measures.
4. Conveying Verbatim Copies.
You may convey verbatim copies of the Program's source code as you
receive it, in any medium, provided that you conspicuously and
appropriately publish on each copy an appropriate copyright notice;
keep intact all notices stating that this License and any
non-permissive terms added in accord with section 7 apply to the code;
keep intact all notices of the absence of any warranty; and give all
recipients a copy of this License along with the Program.
You may charge any price or no price for each copy that you convey,
and you may offer support or warranty protection for a fee.
5. Conveying Modified Source Versions.
You may convey a work based on the Program, or the modifications to
produce it from the Program, in the form of source code under the
terms of section 4, provided that you also meet all of these conditions:
a) The work must carry prominent notices stating that you modified
it, and giving a relevant date.
b) The work must carry prominent notices stating that it is
released under this License and any conditions added under section
7. This requirement modifies the requirement in section 4 to
"keep intact all notices".
c) You must license the entire work, as a whole, under this
License to anyone who comes into possession of a copy. This
License will therefore apply, along with any applicable section 7
additional terms, to the whole of the work, and all its parts,
regardless of how they are packaged. This License gives no
permission to license the work in any other way, but it does not
invalidate such permission if you have separately received it.
d) If the work has interactive user interfaces, each must display
Appropriate Legal Notices; however, if the Program has interactive
interfaces that do not display Appropriate Legal Notices, your
work need not make them do so.
A compilation of a covered work with other separate and independent
works, which are not by their nature extensions of the covered work,
and which are not combined with it such as to form a larger program,
in or on a volume of a storage or distribution medium, is called an
"aggregate" if the compilation and its resulting copyright are not
used to limit the access or legal rights of the compilation's users
beyond what the individual works permit. Inclusion of a covered work
in an aggregate does not cause this License to apply to the other
parts of the aggregate.
6. Conveying Non-Source Forms.
You may convey a covered work in object code form under the terms
of sections 4 and 5, provided that you also convey the
machine-readable Corresponding Source under the terms of this License,
in one of these ways:
a) Convey the object code in, or embodied in, a physical product
(including a physical distribution medium), accompanied by the
Corresponding Source fixed on a durable physical medium
customarily used for software interchange.
b) Convey the object code in, or embodied in, a physical product
(including a physical distribution medium), accompanied by a
written offer, valid for at least three years and valid for as
long as you offer spare parts or customer support for that product
model, to give anyone who possesses the object code either (1) a
copy of the Corresponding Source for all the software in the
product that is covered by this License, on a durable physical
medium customarily used for software interchange, for a price no
more than your reasonable cost of physically performing this
conveying of source, or (2) access to copy the
Corresponding Source from a network server at no charge.
c) Convey individual copies of the object code with a copy of the
written offer to provide the Corresponding Source. This
alternative is allowed only occasionally and noncommercially, and
only if you received the object code with such an offer, in accord
with subsection 6b.
d) Convey the object code by offering access from a designated
place (gratis or for a charge), and offer equivalent access to the
Corresponding Source in the same way through the same place at no
further charge. You need not require recipients to copy the
Corresponding Source along with the object code. If the place to
copy the object code is a network server, the Corresponding Source
may be on a different server (operated by you or a third party)
that supports equivalent copying facilities, provided you maintain
clear directions next to the object code saying where to find the
Corresponding Source. Regardless of what server hosts the
Corresponding Source, you remain obligated to ensure that it is
available for as long as needed to satisfy these requirements.
e) Convey the object code using peer-to-peer transmission, provided
you inform other peers where the object code and Corresponding
Source of the work are being offered to the general public at no
charge under subsection 6d.
A separable portion of the object code, whose source code is excluded
from the Corresponding Source as a System Library, need not be
included in conveying the object code work.
A "User Product" is either (1) a "consumer product", which means any
tangible personal property which is normally used for personal, family,
or household purposes, or (2) anything designed or sold for incorporation
into a dwelling. In determining whether a product is a consumer product,
doubtful cases shall be resolved in favor of coverage. For a particular
product received by a particular user, "normally used" refers to a
typical or common use of that class of product, regardless of the status
of the particular user or of the way in which the particular user
actually uses, or expects or is expected to use, the product. A product
is a consumer product regardless of whether the product has substantial
commercial, industrial or non-consumer uses, unless such uses represent
the only significant mode of use of the product.
"Installation Information" for a User Product means any methods,
procedures, authorization keys, or other information required to install
and execute modified versions of a covered work in that User Product from
a modified version of its Corresponding Source. The information must
suffice to ensure that the continued functioning of the modified object
code is in no case prevented or interfered with solely because
modification has been made.
If you convey an object code work under this section in, or with, or
specifically for use in, a User Product, and the conveying occurs as
part of a transaction in which the right of possession and use of the
User Product is transferred to the recipient in perpetuity or for a
fixed term (regardless of how the transaction is characterized), the
Corresponding Source conveyed under this section must be accompanied
by the Installation Information. But this requirement does not apply
if neither you nor any third party retains the ability to install
modified object code on the User Product (for example, the work has
been installed in ROM).
The requirement to provide Installation Information does not include a
requirement to continue to provide support service, warranty, or updates
for a work that has been modified or installed by the recipient, or for
the User Product in which it has been modified or installed. Access to a
network may be denied when the modification itself materially and
adversely affects the operation of the network or violates the rules and
protocols for communication across the network.
Corresponding Source conveyed, and Installation Information provided,
in accord with this section must be in a format that is publicly
documented (and with an implementation available to the public in
source code form), and must require no special password or key for
unpacking, reading or copying.
7. Additional Terms.
"Additional permissions" are terms that supplement the terms of this
License by making exceptions from one or more of its conditions.
Additional permissions that are applicable to the entire Program shall
be treated as though they were included in this License, to the extent
that they are valid under applicable law. If additional permissions
apply only to part of the Program, that part may be used separately
under those permissions, but the entire Program remains governed by
this License without regard to the additional permissions.
When you convey a copy of a covered work, you may at your option
remove any additional permissions from that copy, or from any part of
it. (Additional permissions may be written to require their own
removal in certain cases when you modify the work.) You may place
additional permissions on material, added by you to a covered work,
for which you have or can give appropriate copyright permission.
Notwithstanding any other provision of this License, for material you
add to a covered work, you may (if authorized by the copyright holders of
that material) supplement the terms of this License with terms:
a) Disclaiming warranty or limiting liability differently from the
terms of sections 15 and 16 of this License; or
b) Requiring preservation of specified reasonable legal notices or
author attributions in that material or in the Appropriate Legal
Notices displayed by works containing it; or
c) Prohibiting misrepresentation of the origin of that material, or
requiring that modified versions of such material be marked in
reasonable ways as different from the original version; or
d) Limiting the use for publicity purposes of names of licensors or
authors of the material; or
e) Declining to grant rights under trademark law for use of some
trade names, trademarks, or service marks; or
f) Requiring indemnification of licensors and authors of that
material by anyone who conveys the material (or modified versions of
it) with contractual assumptions of liability to the recipient, for
any liability that these contractual assumptions directly impose on
those licensors and authors.
All other non-permissive additional terms are considered "further
restrictions" within the meaning of section 10. If the Program as you
received it, or any part of it, contains a notice stating that it is
governed by this License along with a term that is a further
restriction, you may remove that term. If a license document contains
a further restriction but permits relicensing or conveying under this
License, you may add to a covered work material governed by the terms
of that license document, provided that the further restriction does
not survive such relicensing or conveying.
If you add terms to a covered work in accord with this section, you
must place, in the relevant source files, a statement of the
additional terms that apply to those files, or a notice indicating
where to find the applicable terms.
Additional terms, permissive or non-permissive, may be stated in the
form of a separately written license, or stated as exceptions;
the above requirements apply either way.
8. Termination.
You may not propagate or modify a covered work except as expressly
provided under this License. Any attempt otherwise to propagate or
modify it is void, and will automatically terminate your rights under
this License (including any patent licenses granted under the third
paragraph of section 11).
However, if you cease all violation of this License, then your
license from a particular copyright holder is reinstated (a)
provisionally, unless and until the copyright holder explicitly and
finally terminates your license, and (b) permanently, if the copyright
holder fails to notify you of the violation by some reasonable means
prior to 60 days after the cessation.
Moreover, your license from a particular copyright holder is
reinstated permanently if the copyright holder notifies you of the
violation by some reasonable means, this is the first time you have
received notice of violation of this License (for any work) from that
copyright holder, and you cure the violation prior to 30 days after
your receipt of the notice.
Termination of your rights under this section does not terminate the
licenses of parties who have received copies or rights from you under
this License. If your rights have been terminated and not permanently
reinstated, you do not qualify to receive new licenses for the same
material under section 10.
9. Acceptance Not Required for Having Copies.
You are not required to accept this License in order to receive or
run a copy of the Program. Ancillary propagation of a covered work
occurring solely as a consequence of using peer-to-peer transmission
to receive a copy likewise does not require acceptance. However,
nothing other than this License grants you permission to propagate or
modify any covered work. These actions infringe copyright if you do
not accept this License. Therefore, by modifying or propagating a
covered work, you indicate your acceptance of this License to do so.
10. Automatic Licensing of Downstream Recipients.
Each time you convey a covered work, the recipient automatically
receives a license from the original licensors, to run, modify and
propagate that work, subject to this License. You are not responsible
for enforcing compliance by third parties with this License.
An "entity transaction" is a transaction transferring control of an
organization, or substantially all assets of one, or subdividing an
organization, or merging organizations. If propagation of a covered
work results from an entity transaction, each party to that
transaction who receives a copy of the work also receives whatever
licenses to the work the party's predecessor in interest had or could
give under the previous paragraph, plus a right to possession of the
Corresponding Source of the work from the predecessor in interest, if
the predecessor has it or can get it with reasonable efforts.
You may not impose any further restrictions on the exercise of the
rights granted or affirmed under this License. For example, you may
not impose a license fee, royalty, or other charge for exercise of
rights granted under this License, and you may not initiate litigation
(including a cross-claim or counterclaim in a lawsuit) alleging that
any patent claim is infringed by making, using, selling, offering for
sale, or importing the Program or any portion of it.
11. Patents.
A "contributor" is a copyright holder who authorizes use under this
License of the Program or a work on which the Program is based. The
work thus licensed is called the contributor's "contributor version".
A contributor's "essential patent claims" are all patent claims
owned or controlled by the contributor, whether already acquired or
hereafter acquired, that would be infringed by some manner, permitted
by this License, of making, using, or selling its contributor version,
but do not include claims that would be infringed only as a
consequence of further modification of the contributor version. For
purposes of this definition, "control" includes the right to grant
patent sublicenses in a manner consistent with the requirements of
this License.
Each contributor grants you a non-exclusive, worldwide, royalty-free
patent license under the contributor's essential patent claims, to
make, use, sell, offer for sale, import and otherwise run, modify and
propagate the contents of its contributor version.
In the following three paragraphs, a "patent license" is any express
agreement or commitment, however denominated, not to enforce a patent
(such as an express permission to practice a patent or covenant not to
sue for patent infringement). To "grant" such a patent license to a
party means to make such an agreement or commitment not to enforce a
patent against the party.
If you convey a covered work, knowingly relying on a patent license,
and the Corresponding Source of the work is not available for anyone
to copy, free of charge and under the terms of this License, through a
publicly available network server or other readily accessible means,
then you must either (1) cause the Corresponding Source to be so
available, or (2) arrange to deprive yourself of the benefit of the
patent license for this particular work, or (3) arrange, in a manner
consistent with the requirements of this License, to extend the patent
license to downstream recipients. "Knowingly relying" means you have
actual knowledge that, but for the patent license, your conveying the
covered work in a country, or your recipient's use of the covered work
in a country, would infringe one or more identifiable patents in that
country that you have reason to believe are valid.
If, pursuant to or in connection with a single transaction or
arrangement, you convey, or propagate by procuring conveyance of, a
covered work, and grant a patent license to some of the parties
receiving the covered work authorizing them to use, propagate, modify
or convey a specific copy of the covered work, then the patent license
you grant is automatically extended to all recipients of the covered
work and works based on it.
A patent license is "discriminatory" if it does not include within
the scope of its coverage, prohibits the exercise of, or is
conditioned on the non-exercise of one or more of the rights that are
specifically granted under this License. You may not convey a covered
work if you are a party to an arrangement with a third party that is
in the business of distributing software, under which you make payment
to the third party based on the extent of your activity of conveying
the work, and under which the third party grants, to any of the
parties who would receive the covered work from you, a discriminatory
patent license (a) in connection with copies of the covered work
conveyed by you (or copies made from those copies), or (b) primarily
for and in connection with specific products or compilations that
contain the covered work, unless you entered into that arrangement,
or that patent license was granted, prior to 28 March 2007.
Nothing in this License shall be construed as excluding or limiting
any implied license or other defenses to infringement that may
otherwise be available to you under applicable patent law.
12. No Surrender of Others' Freedom.
If conditions are imposed on you (whether by court order, agreement or
otherwise) that contradict the conditions of this License, they do not
excuse you from the conditions of this License. If you cannot convey a
covered work so as to satisfy simultaneously your obligations under this
License and any other pertinent obligations, then as a consequence you may
not convey it at all. For example, if you agree to terms that obligate you
to collect a royalty for further conveying from those to whom you convey
the Program, the only way you could satisfy both those terms and this
License would be to refrain entirely from conveying the Program.
13. Remote Network Interaction; Use with the GNU General Public License.
Notwithstanding any other provision of this License, if you modify the
Program, your modified version must prominently offer all users
interacting with it remotely through a computer network (if your version
supports such interaction) an opportunity to receive the Corresponding
Source of your version by providing access to the Corresponding Source
from a network server at no charge, through some standard or customary
means of facilitating copying of software. This Corresponding Source
shall include the Corresponding Source for any work covered by version 3
of the GNU General Public License that is incorporated pursuant to the
following paragraph.
Notwithstanding any other provision of this License, you have
permission to link or combine any covered work with a work licensed
under version 3 of the GNU General Public License into a single
combined work, and to convey the resulting work. The terms of this
License will continue to apply to the part which is the covered work,
but the work with which it is combined will remain governed by version
3 of the GNU General Public License.
14. Revised Versions of this License.
The Free Software Foundation may publish revised and/or new versions of
the GNU Affero General Public License from time to time. Such new versions
will be similar in spirit to the present version, but may differ in detail to
address new problems or concerns.
Each version is given a distinguishing version number. If the
Program specifies that a certain numbered version of the GNU Affero General
Public License "or any later version" applies to it, you have the
option of following the terms and conditions either of that numbered
version or of any later version published by the Free Software
Foundation. If the Program does not specify a version number of the
GNU Affero General Public License, you may choose any version ever published
by the Free Software Foundation.
If the Program specifies that a proxy can decide which future
versions of the GNU Affero General Public License can be used, that proxy's
public statement of acceptance of a version permanently authorizes you
to choose that version for the Program.
Later license versions may give you additional or different
permissions. However, no additional obligations are imposed on any
author or copyright holder as a result of your choosing to follow a
later version.
15. Disclaimer of Warranty.
THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY
APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT
HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY
OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO,
THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM
IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF
ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
16. Limitation of Liability.
IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS
THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY
GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF
DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD
PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS),
EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
SUCH DAMAGES.
17. Interpretation of Sections 15 and 16.
If the disclaimer of warranty and limitation of liability provided
above cannot be given local legal effect according to their terms,
reviewing courts shall apply local law that most closely approximates
an absolute waiver of all civil liability in connection with the
Program, unless a warranty or assumption of liability accompanies a
copy of the Program in return for a fee.
END OF TERMS AND CONDITIONS
How to Apply These Terms to Your New Programs
If you develop a new program, and you want it to be of the greatest
possible use to the public, the best way to achieve this is to make it
free software which everyone can redistribute and change under these terms.
To do so, attach the following notices to the program. It is safest
to attach them to the start of each source file to most effectively
state the exclusion of warranty; and each file should have at least
the "copyright" line and a pointer to where the full notice is found.
<one line to give the program's name and a brief idea of what it does.>
Copyright (C) <year> <name of author>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
Also add information on how to contact you by electronic and paper mail.
If your software can interact with users remotely through a computer
network, you should also make sure that it provides a way for users to
get its source. For example, if your program is a web application, its
interface could display a "Source" link that leads users to an archive
of the code. There are many ways you could offer source, and different
solutions will be better for different programs; see section 13 for the
specific requirements.
You should also get your employer (if you work as a programmer) or school,
if any, to sign a "copyright disclaimer" for the program, if necessary.
For more information on this, and how to apply and follow the GNU AGPL, see
<https://www.gnu.org/licenses/>.
================================================
FILE: Makefile
================================================
CC = gcc
CFLAGS = -Wall -Wextra -Ilib -Isrc -Iexm
SRC_DIR = src
LIB_DIR = lib
EXM_DIR = exm
BUILD_DIR = build
LIBRARY = $(BUILD_DIR)/libiso15765.a
LIB_DEP = $(BUILD_DIR)/libiqueue.a
EXAMPLE = $(BUILD_DIR)/example
SRC_FILES = $(wildcard $(SRC_DIR)/*.c)
LIB_FILES = $(wildcard $(LIB_DIR)/*.c)
EXM_FILES = $(wildcard $(EXM_DIR)/*.c)
SRC_OBJS = $(patsubst $(SRC_DIR)/%.c, $(BUILD_DIR)/src_%.o, $(SRC_FILES))
LIB_OBJS = $(patsubst $(LIB_DIR)/%.c, $(BUILD_DIR)/lib_%.o, $(LIB_FILES))
EXM_OBJS = $(patsubst $(EXM_DIR)/%.c, $(BUILD_DIR)/exm_%.o, $(EXM_FILES))
# Rules
all: $(LIBRARY) $(EXAMPLE)
$(BUILD_DIR):
mkdir -p $(BUILD_DIR)
# Compile dependency library
$(LIB_DEP): $(BUILD_DIR) $(LIB_OBJS)
ar rcs $@ $(LIB_OBJS)
$(BUILD_DIR)/lib_%.o: $(LIB_DIR)/%.c
$(CC) $(CFLAGS) -c $< -o $@
# Compile main library
$(LIBRARY): $(LIB_DEP) $(SRC_OBJS)
ar rcs $@ $(SRC_OBJS)
$(BUILD_DIR)/src_%.o: $(SRC_DIR)/%.c
$(CC) $(CFLAGS) -c $< -o $@
# Compile example
$(EXAMPLE): $(LIBRARY) $(LIB_DEP) $(EXM_OBJS)
$(CC) $(CFLAGS) $(EXM_OBJS) $(LIBRARY) $(LIB_DEP) -o $@
$(BUILD_DIR)/exm_%.o: $(EXM_DIR)/%.c
$(CC) $(CFLAGS) -c $< -o $@
clean:
rm -rf $(BUILD_DIR)
rebuild: clean all
.PHONY: all clean
================================================
FILE: README.md
================================================
# ISO15765-2 CANBus TP
 [](https://app.codacy.com/gh/devcoons/iso15765-canbus/dashboard?utm_source=gh&utm_medium=referral&utm_content=&utm_campaign=Badge_grade)
*Compiler flags: **-O3 -Wfatal-errors -Wall -std=c11***
An implementation of the **ISO15765-2 (ISO-TP)** protocol in a platform agnostic C library. The library interacts in a transparent way with the lower ISO layers which means that the user must define the connection for the reception and the transmission of the CANBus frames. In this way you have a complete control and reusability of this library in different platforms. This library can support **UDS**, **OBDII** and any other application layer protocol that requires ISO15765-2 TP.
>This ISO defines common requirements for vehicle diagnostic systems implemented on a Controller Area Network (CAN) communication link, as specified in ISO 11898.
>Although primarily intended for diagnostic systems, it also meets requirements from other CAN-based systems needing a network layer protocol.
## How to use
- Include the header file `iso15765_2.h`
- Define an `iso15765_t` handler, create and attach the required shim/callbacks (`send_frame` `on_error` `get_ms`) of the handler
```C
/* Required shim/callback functions */
static uint8_t send_frame(cbus_id_type id_type, uint32_t id, cbus_fr_format fr_fmt, uint8_t dlc, uint8_t* dt);
static void usdata_indication(n_indn_t* info);
static void on_error(n_rslt err_type);
static uint32_t getms();
/* ISOTP handler */
static iso15765_t handler =
{
.addr_md = N_ADM_FIXED, // Selected address mode of the TP
.fr_id_type = CBUS_ID_T_EXTENDED, // CANBus frame type
.config.stmin = 0x05, // Default min. frame transmission separation
.config.bs = 0x0F, // Maximun size of the block sequence
.config.n_bs = 800, // Time until reception of the next FlowControl N_PDU
.config.n_cr = 250, // Time until reception of the next ConsecutiveFrame N_PDU
.clbs.get_ms = getms, // Time-source for the library in ms(required)
.clbs.on_error = on_error, // Callback which will be executed in any occured error.
.clbs.send_frame = send_frame, // This callback will be fired when a transmission of a canbus frame is ready.
.clbs.indn = usdata_indication // Indication Callback: Will be fired when a reception
// is available or an error occured during the reception.
};
static uint8_t send_frame(cbus_id_type id_type, uint32_t id, cbus_fr_format fr_fmt, uint8_t dlc, uint8_t* dt)
{
// Here transmit the frame through CANBus
return 0;
}
static void usdata_indication(n_indn_t* info)
{
// Use the incoming message. This function should be fired by the library when a new complete message arrives.
}
static void on_error(n_rslt err_type)
{
// Check the errors etc..
}
static uint32_t getms()
{
// return time in ms;
// ex. return GetTickCount();
}
```
- In your main, first initialize the iso15765_t handler `iso15765_init(&handler);`
- To send a message, create a `n_req_t` and use the function `iso15765_send`. For example:
```C
n_req_t frame =
{
.n_ai.n_pr = 0x06, // Network Address Priority
.n_ai.n_sa = 0x01, // Network Source Address
.n_ai.n_ta = 0x02, // Network Target Address
.n_ai.n_ae = 0x00, // Network Address Extension
.n_ai.n_tt = N_TA_T_PHY, // Network Target Address type
.fr_fmt = CBUS_FR_FRM_STD, // CANFD or CANSTD
.msg = {1,2,3,4,5,6,7,8, // Message
9,10,11,12,13,14,
15,16,17,18,19,20},
.msg_sz = 20, // Message size
};
...
iso15765_send(&handler, &frame);
```
- To push an incoming frame to the library use the function `iso15765_enqueue(&handler, &frame);`. It is suggested to put this function inside the frame reception callback of your interface
- Use the `iso15765_process(&handler);` to allow the library to process the in/out streams of data. Normally you could put this function in a thread to run continuously.
- As described before, any new/completed incoming message should be handled in the callback `static void usdata_indication(indn_t* info)`
Below is a **complete loopback example**. The service send a message to itself by enqueing the transmitted frame in the inbound stream.
```C
#include <stdio.h>
#include <stdlib.h>
#include "iso15765_2.h"
#include <windows.h>
#include <sysinfoapi.h>
/******************************************************************************
* Declaration | Static Functions
******************************************************************************/
static uint8_t send_frame(canbus_md md, uint32_t id, uint8_t dlc, uint8_t* dt);
static void on_error(n_rslt err_type);
static uint32_t getms();
/******************************************************************************
* Enumerations, structures & Variables
******************************************************************************/
static iso15765_t handler =
{
.addr_md = N_ADM_FIXED,
.fr_id_type = CBUS_ID_T_EXTENDED,
.clbs.send_frame = send_frame1,
.clbs.on_error = on_error,
.clbs.get_ms = getms,
.clbs.indn = indn1,
.config.stmin = 0x3,
.config.bs = 0x0f,
.config.n_bs = 100,
.config.n_cr = 3
};
n_req_t frame =
{
.n_ai.n_pr = 0x07,
.n_ai.n_sa = 0x01,
.n_ai.n_ta = 0x02,
.n_ai.n_ae = 0x00,
.n_ai.n_tt = N_TA_T_PHY,
.fr_fmt = CBUS_FR_FRM_FD,
.msg = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23},
.msg_sz = 23,
};
/******************************************************************************
* Definition | Static Functions
******************************************************************************/
static uint32_t getms()
{
return GetTickCount();
}
static uint8_t send_frame(cbus_id_type id_type, uint32_t id, cbus_fr_format fr_fmt, uint8_t dlc, uint8_t* dt)
{
printf("%d #1# - id:%x dlc:%02x ",GetTickCount(), id, dlc);
for (int i = 0; i < 8; i++) printf("%02x ", dt[i]);
printf("\n");
canbus_frame_t frame = { .id = id, .dlc = dlc, .id_type = id_type, .fr_format= fr_fmt };
memmove(frame.dt, dt, dlc);
iso15765_enqueue(&handler, &frame);
return 0;
}
static void on_error(n_rslt err_type)
{
printf("ERROR OCCURED!:%d", err_type);
}
/******************************************************************************
* Definition | Public Functions
******************************************************************************/
int main()
{
iso15765_init(&handler);
iso15765_send(&handler, &frame);
while(1)
{
iso15765_process(&handler);
Sleep(5);
}
return 0;
}
```
Please check the folder **`exm`** for more examples
## Development
This library is experimental and is still under development. The purpose is to create a complete ISO15765 library with all the described features. Feel free to suggest anything. If you use this library please ref.
## Contributing
We would love you to contribute to `iso15765-canbus`, pull requests are welcome!
## Support
Support this project though https://paypal.me/iikem or https://github.com/sponsors/devcoons
## Licensing Information
This project is licensed under the GNU Affero General Public License v3.0 (AGPL-3.0).
You can find the full license text in the [LICENSE](./LICENSE) file.
### Commercial Licensing
For commercial use, including proprietary or for-profit applications, you must obtain a separate license.
Contact me for commercial licensing at:
- GitHub: [https://github.com/devcoons](https://github.com/devcoons)
- Email: i_-_-_s@outlook.com
================================================
FILE: doc/.gitkeep
================================================
================================================
FILE: exm/ISO15765Development.c
================================================
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "lib_iso15765.h"
#ifdef _WIN32
#include <windows.h>
#include <sysinfoapi.h>
#include <conio.h>
#else
#include <unistd.h>
#include <sys/time.h>
#endif
/******************************************************************************
* Declaration | Static Functions
******************************************************************************/
static uint8_t send_frame1(cbus_id_type id_type, uint32_t id, cbus_fr_format fr_fmt, uint8_t dlc, uint8_t* dt);
static uint8_t send_frame2(cbus_id_type id_type, uint32_t id, cbus_fr_format fr_fmt, uint8_t dlc, uint8_t* dt);
static void indn1(n_indn_t* info);
static void indn2(n_indn_t* info);
static void on_error(n_rslt err_type);
static uint32_t getms();
/******************************************************************************
* Enumerations, structures & Variables
******************************************************************************/
static iso15765_t handler1 = {
.addr_md = N_ADM_FIXED,
.fr_id_type = CBUS_ID_T_EXTENDED,
.clbs.send_frame = send_frame1,
.clbs.on_error = on_error,
.clbs.get_ms = getms,
.clbs.indn = indn1,
.config.stmin = 0x3,
.config.bs = 0x2a,
.config.n_bs = 100,
.config.n_cr = 3
};
static iso15765_t handler2 = {
.addr_md = N_ADM_FIXED,
.fr_id_type = CBUS_ID_T_EXTENDED,
.clbs.send_frame = send_frame2,
.clbs.on_error = on_error,
.clbs.get_ms = getms,
.clbs.indn = indn2,
.config.stmin = 0x3,
.config.bs = 0x0f,
.config.n_bs = 100,
.config.n_cr = 3
};
n_req_t frame1 = {
.n_ai.n_pr = 0x07,
.n_ai.n_sa = 0x01,
.n_ai.n_ta = 0x02,
.n_ai.n_ae = 0x00,
.n_ai.n_tt = N_TA_T_PHY,
.fr_fmt = CBUS_FR_FRM_STD,
.msg = {0},
.msg_sz = 0,
};
n_req_t frame2 = {
.n_ai.n_pr = 0x07,
.n_ai.n_sa = 0x02,
.n_ai.n_ta = 0x01,
.n_ai.n_ae = 0x00,
.n_ai.n_tt = N_TA_T_PHY,
.fr_fmt = CBUS_FR_FRM_STD,
.msg = {0},
.msg_sz = 0,
};
/******************************************************************************
* Definition | Static Functions
******************************************************************************/
uint16_t f_sz1 = 1;
uint16_t f_sz2 = 1;
static void rand_string(uint8_t* str, size_t size) {
const uint8_t charset[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJK";
for (size_t n = 0; n < size; n++) {
int key = rand() % (sizeof(charset) - 1);
str[n] = charset[key];
}
}
static uint32_t getms() {
#ifdef _WIN32
return GetTickCount();
#else
struct timeval tv;
gettimeofday(&tv, NULL);
return (uint32_t)(tv.tv_sec * 1000 + tv.tv_usec / 1000);
#endif
}
static void print_frame(uint8_t instance, uint8_t tp_mode, cbus_id_type mode, uint32_t id, uint8_t ctp_ft, uint8_t dlc, uint8_t* dt) {
printf(" (%d) - %d | TpMode: [%02x] | FrameType: [%2d] IdType: [%d] Id: [%8x] DLC: [%02d]\t\t", instance, getms(), tp_mode, mode, ctp_ft, id, dlc);
for (int s = 0; s < dlc; s += 8) {
int elements = dlc - s > 8 ? 8 : dlc - s;
for (int i = s; i < s + 8; i++) {
printf((i < s + elements) ? "%02x " : " ", dt[i]);
}
printf("\t");
if (s + elements == dlc) {
printf("\n");
} else {
printf("\n |\t\t");
}
}
}
static uint8_t send_frame1(cbus_id_type id_type, uint32_t id, cbus_fr_format fr_fmt, uint8_t dlc, uint8_t* dt) {
print_frame(1, handler2.addr_md, id_type, id, fr_fmt, dlc, dt);
canbus_frame_t frame = { .id = id, .dlc = dlc, .id_type = id_type, .fr_format= fr_fmt };
memmove(frame.dt, dt, dlc);
iso15765_enqueue(&handler2, &frame);
return 0;
}
static uint8_t send_frame2(cbus_id_type id_type, uint32_t id, cbus_fr_format fr_fmt, uint8_t dlc, uint8_t* dt) {
print_frame(2, handler1.addr_md, id_type, id, fr_fmt, dlc, dt);
canbus_frame_t frame = { .id = id, .dlc = dlc, .id_type = id_type, .fr_format = fr_fmt };
memmove(frame.dt, dt, dlc);
iso15765_enqueue(&handler1, &frame);
return 0;
}
static void on_error(n_rslt err_type) {
printf("ERROR OCCURRED!: %04x\n", err_type);
}
static void indn1(n_indn_t* info) {
uint8_t v = (memcmp(info->msg, frame2.msg, info->msg_sz) == 0) ? 'V' : 'X';
uint8_t s = (info->msg_sz == frame2.msg_sz) ? 'V' : 'X';
printf("- Reception of H1. Msg_sz:[%d] | SZ_CH[%c] MSG_CH[%c]\n", info->msg_sz, s, v);
if (v != 'V' || s != 'V') {
printf("--------- ERROR -----------\n");
#ifdef _WIN32
Sleep(1000);
#else
usleep(1000 * 1000); // Sleep for 1 second
#endif
}
printf("- END OF TRANSMISSION/RECEPTION -\n\n\n");
frame1.msg_sz = f_sz1;
rand_string(frame1.msg, frame1.msg_sz);
f_sz1 = (f_sz1 == 512) ? 0 : f_sz1 + 1;
iso15765_send(&handler1, &frame1);
}
static void indn2(n_indn_t* info) {
uint8_t v = (memcmp(info->msg, frame1.msg, info->msg_sz) == 0) ? 'V' : 'X';
uint8_t s = (info->msg_sz == frame1.msg_sz) ? 'V' : 'X';
printf("- Reception of H2. Msg_sz:[%d] | SZ_CH[%c] MSG_CH[%c]\n", info->msg_sz, s, v);
if (v != 'V' || s != 'V') {
printf("--------- ERROR -----------\n");
#ifdef _WIN32
Sleep(1000);
#else
usleep(1000 * 1000); // Sleep for 1 second
#endif
}
printf("- END OF TRANSMISSION/RECEPTION -\n\n\n");
frame2.msg_sz = f_sz2;
rand_string(frame2.msg, frame2.msg_sz);
f_sz2 = (f_sz2 == 512) ? 0 : f_sz2 + 1;
iso15765_send(&handler2, &frame2);
}
/******************************************************************************
* Definition | Public Functions
******************************************************************************/
int main() {
iso15765_init(&handler1);
iso15765_init(&handler2);
frame1.msg_sz = f_sz1;
rand_string(frame1.msg, frame1.msg_sz);
f_sz1++;
iso15765_send(&handler1, &frame1);
while (1) {
iso15765_process(&handler1);
iso15765_process(&handler2);
}
return 0;
}
================================================
FILE: lib/lib_iqueue.c
================================================
/*!
@file lib_iqueue.c
@brief <brief description here>
@t.odo -
---------------------------------------------------------------------------
GNU Affero General Public License v3.0
Copyright (c) 2024 Ioannis D. (devcoons)
This program is free software: you can redistribute it and/or modify it
under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
For commercial use, including proprietary or for-profit applications,
a separate license is required. Contact:
- GitHub: [https://github.com/devcoons](https://github.com/devcoons)
- Email: i_-_-_s@outlook.com
*/
/******************************************************************************
* Preprocessor Definitions & Macros
******************************************************************************/
/******************************************************************************
* Includes
******************************************************************************/
#include "lib_iqueue.h"
/******************************************************************************
* Enumerations, structures & Variables
******************************************************************************/
/******************************************************************************
* Declaration | Static Functions
******************************************************************************/
/******************************************************************************
* Definition | Static Functions
******************************************************************************/
/******************************************************************************
* Definition | Public Functions
******************************************************************************/
i_status iqueue_init(iqueue_t* _queue, uint32_t _max_elements, size_t _element_size, void* _storage)
{
if ((_queue != NULL) && (_storage != NULL))
{
(void)memset(_storage, 0, _element_size * _max_elements);
_queue->element_size = _element_size;
_queue->max_elements = _max_elements;
_queue->first = (uintptr_t)NULL;
_queue->next = (uintptr_t)_storage;
_queue->storage = _storage;
return I_OK;
}
else
{
return I_ERROR;
}
}
void* iqueue_get_next_enqueue(iqueue_t* _queue)
{
return (_queue != NULL) ? (void *)(_queue->next) : NULL;
}
i_status iqueue_advance_next(iqueue_t* _queue)
{
if (_queue->first != _queue->next)
{
uintptr_t storage_end = (uintptr_t)(_queue->storage) + (_queue->element_size * _queue->max_elements);
_queue->first = (_queue->first == 0U) ? _queue->next : _queue->first;
_queue->next = ((_queue->next + _queue->element_size) == storage_end) ? (uintptr_t)_queue->storage : (_queue->next + _queue->element_size);
return I_OK;
}
else
{
return I_FULL;
}
}
i_status iqueue_enqueue(iqueue_t* _queue, void* _element)
{
if (_queue->first != _queue->next)
{
(void)memmove((void*)_queue->next, (void*)_element, _queue->element_size);
return iqueue_advance_next(_queue);
}
return I_FULL;
}
i_status iqueue_dequeue(iqueue_t* _queue, void* _element)
{
void* x = iqueue_dequeue_fast(_queue);
if (x != NULL)
{
(void)memmove((void*)_element, x, _queue->element_size);
return I_OK;
}
else
{
return I_EMPTY;
}
}
void* iqueue_dequeue_fast(iqueue_t* _queue)
{
if (_queue == NULL) {
return NULL;
}
if (_queue->first != 0U) {
void* ret_ptr = (void*)_queue->first;
uintptr_t storage_end = (uintptr_t)_queue->storage + (_queue->element_size * _queue->max_elements);
uintptr_t next_first = _queue->first + _queue->element_size;
_queue->first = (next_first == storage_end) ? (uintptr_t)_queue->storage : next_first;
_queue->first = (_queue->first == _queue->next) ? 0U : _queue->first;
return ret_ptr;
}
return NULL;
}
i_status iqueue_size(iqueue_t* _queue, size_t* _size)
{
*_size = _queue->first == 0
? 0
: _queue->first < _queue->next
? (_queue->next - _queue->first) / _queue->element_size
: _queue->max_elements - ((_queue->first - _queue->next) / _queue->element_size);
return I_OK;
}
/******************************************************************************
* EOF - NO CODE AFTER THIS LINE
******************************************************************************/
================================================
FILE: lib/lib_iqueue.h
================================================
#pragma once
/*!
@file lib_iqueue.h
@brief <brief description here>
@t.odo -
---------------------------------------------------------------------------
MIT License
Copyright (c) 2018 Io. D (Devcoons)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
/******************************************************************************
* Preprocessor Definitions & Macros
******************************************************************************/
#ifndef LIBRARIES_INC_LIB_IQUEUE_H_
#define LIBRARIES_INC_LIB_IQUEUE_H_
/******************************************************************************
* Includes
******************************************************************************/
#include <inttypes.h>
#include <string.h>
/******************************************************************************
* Enumerations, structures & Variables
******************************************************************************/
#if !defined(ENUM_I_STATUS)
#define ENUM_I_STATUS
typedef enum
{
I_OK = 0x00,
I_INVALID = 0x01,
I_EXISTS = 0x02,
I_NOTEXISTS = 0x03,
I_FAILED = 0x04,
I_EXPIRED = 0x05,
I_UNKNOWN = 0x06,
I_INPROGRESS = 0x07,
I_IDLE = 0x08,
I_FULL = 0x09,
I_EMPTY = 0x0A,
I_YES = 0x0B,
I_NO = 0x0C,
I_SKIP = 0x0D,
I_DEBUG_01 = 0xE0,
I_DEBUG_02 = 0xE1,
I_DEBUG_03 = 0xE2,
I_DEBUG_04 = 0xE3,
I_DEBUG_05 = 0xE4,
I_DEBUG_06 = 0xE5,
I_DEBUG_07 = 0xE6,
I_DEBUG_08 = 0xE7,
I_DEBUG_09 = 0xE8,
I_DEBUG_10 = 0xE9,
I_DEBUG_11 = 0xEA,
I_DEBUG_12 = 0xEB,
I_DEBUG_13 = 0xEC,
I_DEBUG_14 = 0xED,
I_DEBUG_15 = 0xEE,
I_DEBUG_16 = 0xEF,
I_MEMUNALIGNED = 0xFD,
I_NOTIMPLEMENTED = 0xFE,
I_ERROR = 0xFF
}i_status;
#endif
typedef struct
{
void* storage;
uintptr_t first;
uintptr_t next;
size_t element_size;
uint32_t max_elements;
}
iqueue_t;
/******************************************************************************
* Declaration | Public Functions
******************************************************************************/
i_status iqueue_init(iqueue_t* _queue, uint32_t _max_elements, size_t _element_size, void* _storage);
i_status iqueue_enqueue(iqueue_t* _queue, void* _element);
i_status iqueue_dequeue(iqueue_t* _queue, void* _element);
i_status iqueue_size(iqueue_t* _queue, size_t* _size);
i_status iqueue_advance_next(iqueue_t* _queue);
void* iqueue_get_next_enqueue(iqueue_t* _queue);
void* iqueue_dequeue_fast(iqueue_t* _queue);
/******************************************************************************
* EOF - NO CODE AFTER THIS LINE
******************************************************************************/
#endif
================================================
FILE: src/lib_iso15765.c
================================================
/*!
@file iso15765_2.c
@brief Source file of the ISO15765-2 library
@t.odo -
---------------------------------------------------------------------------
GNU Affero General Public License v3.0
Copyright (c) 2024 Ioannis D. (devcoons)
This program is free software: you can redistribute it and/or modify it
under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
For commercial use, including proprietary or for-profit applications,
a separate license is required. Contact:
- GitHub: [https://github.com/devcoons](https://github.com/devcoons)
- Email: i_-_-_s@outlook.com
*/
/******************************************************************************
* Preprocessor Definitions & Macros
******************************************************************************/
/******************************************************************************
* Includes
******************************************************************************/
#include "lib_iso15765.h"
/******************************************************************************
* Enumerations, structures & Variables
******************************************************************************/
/* Structs used by the service to inform the user(upper layer) of an event */
static n_indn_t sgn_indn;
static n_ff_indn_t sgn_ff_indn;
static n_cfm_t sgn_conf;
static n_chg_param_cfm_t sgn_chg_cfm;
/******************************************************************************
* Declaration | Static Functions
******************************************************************************/
/******************************************************************************
* Definition | Static Functions
******************************************************************************/
/*
* Default empty callback functions. The user can assing his own during the init
* of the service.
*/
static void indn(n_indn_t* info)
{
ISO_15675_UNUSED(info);
}
static void ff_indn(n_ff_indn_t* info)
{
ISO_15675_UNUSED(info);
}
static void cfm(n_cfm_t* info)
{
ISO_15675_UNUSED(info);
}
static void cfg_cfm(n_chg_param_cfm_t* info)
{
ISO_15675_UNUSED(info);
}
/*
* Helper function to check if time interval has passed from a given time.
*/
inline static n_rslt has_interval_passed(uint32_t current_time, uint32_t last_time, uint32_t interval)
{
if ((interval == 0U) || (interval >= UINT32_MAX))
{
return N_ERROR;
}
uint32_t elapsed_time = current_time - last_time;
return (elapsed_time >= interval) ? N_OK : N_INV;
}
/*
* Helper function to find the closest can_dl
*/
inline static uint8_t n_get_closest_can_dl(uint8_t size, cbus_fr_format tmt)
{
uint8_t rval = 0;
if (tmt == CBUS_FR_FRM_STD)
{
rval = (size <= 0x08U) ? size : 0x08U;
}
else
{
if (size <= 8)
{
rval = size;
}
else if (size <= 12)
{
rval = 12;
}
else if (size <= 16)
{
rval = 16;
}
else if (size <= 20)
{
rval = 20;
}
else if (size <= 24)
{
rval = 24;
}
else if (size <= 32)
{
rval = 32;
}
else if (size <= 48)
{
rval = 48;
}
else
{
rval = 64;
}
}
return rval;
}
/*
* Helper function to find the closest can_dl
*/
inline static uint8_t n_get_dt_offset(addr_md address, pci_type pci, uint16_t data_size)
{
if (address != N_ADM_NORMAL && address != N_ADM_FIXED
&& address != N_ADM_MIXED11 && address != N_ADM_EXTENDED
&& address != N_ADM_MIXED29)
{
return 0xFF;
}
uint8_t offset = (address & 0x01);
switch (pci)
{
case N_PCI_T_SF:
offset += 1;
offset += data_size <= (8 - offset) ? 0 : 1;
break;
case N_PCI_T_FF:
offset += 2;
offset += data_size > 4095 ? 4 : 0;
break;
case N_PCI_T_CF:
offset += 1;
break;
case N_PCI_T_FC:
offset += 3;
break;
default:
offset = 0xFF;
break;
}
return offset;
}
/*
* Helper function to find which PCI_Type the outbound stream has to use
*/
inline static pci_type n_out_frame_type(iso15765_t* instance)
{
pci_type result = N_PCI_T_CF;
if (instance->out.cf_cnt == 0)
{
if ((instance->addr_md & 0x01) == 1)
{
if (instance->out.fr_fmt == CBUS_FR_FRM_STD)
{
result = instance->out.msg_sz <= 6 ? N_PCI_T_SF : N_PCI_T_FF;
}
else
{
result = instance->out.msg_sz <= 61 ? N_PCI_T_SF : N_PCI_T_FF;
}
}
else
{
if (instance->out.fr_fmt == CBUS_FR_FRM_STD)
{
result = instance->out.msg_sz <= 7 ? N_PCI_T_SF : N_PCI_T_FF;
}
else
{
result = instance->out.msg_sz <= 62 ? N_PCI_T_SF : N_PCI_T_FF;
}
}
}
return result;
}
/*
* Convert partially the CANBus Frame from the PCI
*/
inline static n_rslt n_pci_pack(addr_md mode, n_pdu_t* n_pdu, const uint8_t* dt)
{
n_rslt result = N_ERROR;
if (n_pdu != NULL && dt != NULL)
{
uint8_t offs = (mode & 0x01);
switch (n_pdu->n_pci.pt)
{
case N_PCI_T_SF:
if (n_pdu->n_pci.dl <= (uint16_t)(7 - offs))
{
n_pdu->dt[0 + offs] = (n_pdu->n_pci.pt) << 4
| (uint8_t)(n_pdu->n_pci.dl & 0x0F);
}
else
{
n_pdu->dt[0 + offs] = 0 + ((n_pdu->n_pci.pt) << 4);
n_pdu->dt[1 + offs] = (uint8_t)n_pdu->n_pci.dl;
}
result = N_OK;
break;
case N_PCI_T_CF:
n_pdu->dt[0 + offs] = (n_pdu->n_pci.pt) << 4
| n_pdu->n_pci.sn;
result = N_OK;
break;
case N_PCI_T_FF:
n_pdu->dt[0 + offs] = (n_pdu->n_pci.pt) << 4
| (n_pdu->n_pci.dl & 0x0F00) >> 8;
n_pdu->dt[1 + offs] = n_pdu->n_pci.dl & 0x00FF;
result = N_OK;
break;
case N_PCI_T_FC:
n_pdu->dt[0 + offs] = (n_pdu->n_pci.pt) << 4
| n_pdu->n_pci.fs;
n_pdu->dt[1 + offs] = n_pdu->n_pci.bs;
n_pdu->dt[2 + offs] = n_pdu->n_pci.st;
result = N_OK;
break;
default:
result = N_ERROR;
break;
}
}
return result;
}
/*
* Convert the PCI from the CANBus Frame
*/
inline static n_rslt n_pci_unpack(addr_md mode, n_pdu_t* n_pdu, uint8_t dlc, uint8_t* dt)
{
n_rslt result = N_ERROR;
if ((n_pdu != NULL) && (dt != NULL))
{
uint8_t offs = (uint8_t)(mode & 0x01U); // Make use and purpose of 'offs' explicit
// Document the operation's intent clearly to avoid being seen as 'dead code'
n_pdu->n_pci.pt = (uint8_t)((dt[offs] & 0xF0U) >> 4U);
switch (n_pdu->n_pci.pt)
{
case N_PCI_T_SF:
// Conditional operation based on 'dlc', not dead code
n_pdu->n_pci.dl = (dlc <= 8U) ? (uint8_t)(dt[offs] & 0x0FU) : dt[1U + offs];
result = n_pdu->n_pci.dl > dlc ? N_ERROR : N_OK;
break;
case N_PCI_T_CF:
// Direct assignment, the operation depends on 'dt' content
n_pdu->n_pci.sn = (uint8_t)(dt[offs] & 0x0FU);
n_pdu->sz = dlc - (1U + offs);
result = N_OK;
break;
case N_PCI_T_FF:
// Combine two bytes into a larger value, clearly intentional
n_pdu->n_pci.dl = ((uint16_t)(dt[offs] & 0x0FU) << 8U) | dt[1U + offs];
n_pdu->sz = dlc - (2U + offs);
result = N_OK;
break;
case N_PCI_T_FC:
// Sequential assignments based on protocol, clearly used
n_pdu->n_pci.fs = (uint8_t)(dt[offs] & 0x0FU);
n_pdu->n_pci.bs = dt[1U + offs];
n_pdu->n_pci.st = dt[2U + offs] <= 0x7F ? dt[2U + offs] :
((dt[2U + offs]>= 0xF1 && dt[2U + offs]<=0xF9) ? 1 : 127);
n_pdu->sz = dlc - (3U + offs); // Adjust for correct data length calculation
result = N_OK;
break;
default:
result = N_ERROR;
break;
}
}
return result;
}
/*
* Helper function of 'n_pdu_pack' to use the frame payload.
*/
inline static n_rslt n_pdu_pack_dt(addr_md mode, n_pdu_t* n_pdu, uint8_t* dt)
{
n_rslt result = N_ERROR;
uint8_t offset = 0xFF;
if (dt != NULL)
{
switch (n_pdu->n_pci.pt)
{
case N_PCI_T_SF:
offset = n_get_dt_offset(mode, N_PCI_T_SF, n_pdu->sz);
break;
case N_PCI_T_FF:
offset = n_get_dt_offset(mode, N_PCI_T_FF, n_pdu->sz);
break;
case N_PCI_T_CF:
offset = n_get_dt_offset(mode, N_PCI_T_CF, n_pdu->sz);
break;
case N_PCI_T_FC:
offset = n_get_dt_offset(mode, N_PCI_T_FC, n_pdu->sz);
break;
default:
offset = 0xFF;
break;
}
if (offset == 0xFF)
{
result = N_ERROR;
}
else
{
memmove(&n_pdu->dt[offset], dt, n_pdu->sz);
result = N_OK;
}
}
return result;
}
/*
* Helper function of 'n_pdu_unpack' to use the frame payload.
*/
inline static n_rslt n_pdu_unpack_dt(addr_md mode, n_pdu_t* n_pdu, uint8_t* dt)
{
n_rslt result = N_ERROR;
uint8_t offset = 0xFF;
uint16_t sz = 0;
if ((n_pdu != NULL) && (dt != NULL))
{
switch (n_pdu->n_pci.pt)
{
case N_PCI_T_SF:
offset = n_get_dt_offset(mode, N_PCI_T_SF, n_pdu->n_pci.dl);
sz = n_pdu->n_pci.dl;
break;
case N_PCI_T_FF:
offset = n_get_dt_offset(mode, N_PCI_T_FF, n_pdu->n_pci.dl);
sz = n_pdu->sz;
break;
case N_PCI_T_CF:
offset = n_get_dt_offset(mode, N_PCI_T_CF, n_pdu->n_pci.dl);
sz = n_pdu->sz;
break;
case N_PCI_T_FC:
offset = n_get_dt_offset(mode, N_PCI_T_FC, n_pdu->n_pci.dl);
sz = n_pdu->sz;
break;
default:
offset = 0xFF;
sz = 0;
result = N_ERROR;
break;
}
if (offset == 0xFF)
{
result = N_ERROR;
}
else
{
memmove(n_pdu->dt, &dt[offset], sz);
result = N_OK;
}
}
return result;
}
/*
* Convert CANBus frame from PDU
*/
inline static n_rslt n_pdu_pack(addr_md mode, n_pdu_t* n_pdu, uint32_t* id, uint8_t* dt)
{
if ((dt == NULL) || (id == NULL))
{
return N_ERROR;
}
switch (mode)
{
case N_ADM_EXTENDED:
*id = 0x80U
| (n_pdu->n_ai.n_pr << 8)
| (n_pdu->n_ai.n_ae << 3)
| (n_pdu->n_ai.n_sa)
| ((n_pdu->n_ai.n_tt == N_TA_T_PHY) ? 0x40U : 0x00U);
n_pdu->dt[0] = n_pdu->n_ai.n_ta;
break;
case N_ADM_NORMAL:
*id = 0x80U
| (n_pdu->n_ai.n_pr << 8)
| (n_pdu->n_ai.n_ta << 3)
| n_pdu->n_ai.n_sa
| ((n_pdu->n_ai.n_tt == N_TA_T_PHY) ? 0x40U : 0x00U);
break;
case N_ADM_MIXED29:
*id = (n_pdu->n_ai.n_pr << 26)
| ((n_pdu->n_ai.n_tt == N_TA_T_PHY) ? 0xCEU : 0xCDU) << 16
| (n_pdu->n_ai.n_ta << 8)
| n_pdu->n_ai.n_sa;
n_pdu->dt[0] = n_pdu->n_ai.n_ae;
break;
case N_ADM_FIXED:
*id = (n_pdu->n_ai.n_pr << 26)
| ((n_pdu->n_ai.n_tt == N_TA_T_PHY) ? 0xDAU : 0xDBU) << 16
| (n_pdu->n_ai.n_ta << 8)
| n_pdu->n_ai.n_sa;
break;
case N_ADM_MIXED11:
*id = 0x80U
| (n_pdu->n_ai.n_pr << 8)
| (n_pdu->n_ai.n_ta << 3)
| n_pdu->n_ai.n_sa
| ((n_pdu->n_ai.n_tt == N_TA_T_PHY) ? 0x40U : 0x00U);
n_pdu->dt[0] = n_pdu->n_ai.n_ae;
break;
default:
return N_ERROR;
}
if (n_pci_pack(mode, n_pdu, dt) == N_OK)
{
return n_pdu_pack_dt(mode, n_pdu, dt);
}
return N_ERROR;
}
/*
* Convert PDU from CANBus frame
*/
inline static n_rslt n_pdu_unpack(addr_md mode, n_pdu_t* n_pdu, uint32_t id, uint8_t dlc, uint8_t* dt)
{
if (n_pdu == NULL || dt == NULL)
{
return N_ERROR;
}
switch (mode)
{
case N_ADM_MIXED11:
n_pdu->n_ai.n_ae = dt[0];
#ifdef fallthrough
__attribute__((fallthrough));
#endif
case N_ADM_NORMAL:
n_pdu->n_ai.n_pr = (uint8_t)((id & 0x700U) >> 8);
n_pdu->n_ai.n_ta = (uint8_t)((id & 0x38U) >> 3);
n_pdu->n_ai.n_sa = (uint8_t)(id & 0x07U);
n_pdu->n_ai.n_tt = (uint8_t)(((id & 0x40U) >> 6) == 1 ? N_TA_T_PHY : N_TA_T_FUNC);
break;
case N_ADM_MIXED29:
n_pdu->n_ai.n_ae = dt[0];
n_pdu->n_ai.n_pr = (uint8_t)((id & 0x1C000000) >> 26);
n_pdu->n_ai.n_tt = (uint8_t)(((id & 0x00FF0000) >> 16) == 0xCE ? N_TA_T_PHY : N_TA_T_FUNC);
n_pdu->n_ai.n_ta = (uint8_t)((id & 0x0000FF00) >> 8);
n_pdu->n_ai.n_sa = (uint8_t)((id & 0x000000FF) >> 0);
break;
case N_ADM_FIXED:
n_pdu->n_ai.n_pr = (uint8_t)((id & 0x1C000000) >> 26);
n_pdu->n_ai.n_tt = (uint8_t)(((id & 0x00FF0000) >> 16) == 0xDA ? N_TA_T_PHY : N_TA_T_FUNC);
n_pdu->n_ai.n_ta = (uint8_t)((id & 0x0000FF00) >> 8);
n_pdu->n_ai.n_sa = (uint8_t)((id & 0x000000FF) >> 0);
break;
case N_ADM_EXTENDED:
n_pdu->n_ai.n_pr = (uint8_t)((id & 0x700U) >> 8);
n_pdu->n_ai.n_ae = (uint8_t)((id & 0xF8U) >> 3);
n_pdu->n_ai.n_sa = (uint8_t)(id & 0x07U);
n_pdu->n_ai.n_tt = (uint8_t)((id & 0x40U) >> 6 == 1 ? N_TA_T_PHY : N_TA_T_FUNC);
n_pdu->n_ai.n_ta = dt[0];
break;
default:
return N_UNE_PDU;
}
if (n_pci_unpack(mode, n_pdu, dlc, dt) == N_OK)
{
return n_pdu_unpack_dt(mode, n_pdu, dt);
}
return N_ERROR;
}
/*
* Given the correct parameters, the service informs the upper-layer/user about
* an event by using the appropriate callbacks. The function does not support
* the N_CHG_P_CONF signal type.
*/
inline static void signaling(signal_tp tp, n_iostream_t* strm, void(*cb)(void*), uint16_t msg_sz, n_rslt sgn_rslt)
{
if (cb != NULL)
{
switch (tp)
{
case N_INDN:
sgn_indn.rslt = sgn_rslt;
sgn_indn.msg_sz = msg_sz;
sgn_indn.fr_fmt = strm->fr_fmt;
memmove(&sgn_indn.n_ai, &strm->pdu.n_ai, sizeof(n_ai_t));
memmove(&sgn_indn.n_pci, &strm->pdu.n_pci, sizeof(n_pci_t));
memmove(&sgn_indn.msg, strm->msg, msg_sz);
strm->sts = N_S_IDLE;
cb(&sgn_indn);
break;
case N_FF_INDN:
sgn_ff_indn.fr_fmt = strm->fr_fmt;
sgn_ff_indn.msg_sz = msg_sz;
memmove(&sgn_ff_indn.n_ai, &strm->pdu.n_ai, sizeof(n_ai_t));
memmove(&sgn_ff_indn.n_pci, &strm->pdu.n_pci, sizeof(n_pci_t));
strm->sts = (uint8_t)((uint32_t)strm->sts | (uint32_t)N_S_RX_BUSY);
cb(&sgn_ff_indn);
break;
case N_CONF:
sgn_conf.rslt = sgn_rslt;
memmove(&sgn_conf.n_ai, &strm->pdu.n_ai, sizeof(n_ai_t));
memmove(&sgn_conf.n_pci, &strm->pdu.n_pci, sizeof(n_pci_t));
cb(&sgn_conf);
break;
default:
return;
}
}
return;
}
/*
* Check if any timeout should be occured.
*/
inline static n_rslt process_timeouts(iso15765_t* ih)
{
if (ih->out.sts != N_S_TX_WAIT_FC || ih->out.last_upd.n_bs == 0 || ih->config.n_bs == 0)
{
return N_OK;
}
n_rslt timeout = has_interval_passed(ih->clbs.get_ms(),ih->out.last_upd.n_bs,ih->config.n_bs);
if(timeout == N_INV)
{
return N_OK;
}
/* if timeout occures then reset the counters and report to the upper layer */
ih->out.cf_cnt = 0x0;
signaling(N_INDN, &ih->out, (void*)ih->clbs.indn, ih->out.msg_sz, N_TIMEOUT_Bs);
ih->clbs.on_error(N_TIMEOUT_Bs);
return N_TIMEOUT_Bs;
}
/*
* Sends a Flow Control Frame upon request from the reception procedure
*/
static n_rslt send_N_PCI_T_FC(iso15765_t* ih)
{
uint32_t id;
ih->out.sts |= N_S_TX_BUSY;
ih->fl_pdu.n_pci.pt = N_PCI_T_FC;
ih->fl_pdu.n_pci.bs = ih->config.bs;
ih->fl_pdu.n_pci.st = ih->config.stmin;
ih->fl_pdu.n_ai.n_ae = ih->in.pdu.n_ai.n_ae;
ih->fl_pdu.n_ai.n_sa = ih->in.pdu.n_ai.n_ta;
ih->fl_pdu.n_ai.n_ta = ih->in.pdu.n_ai.n_sa;
ih->fl_pdu.n_ai.n_pr = ih->in.pdu.n_ai.n_pr;
ih->fl_pdu.n_ai.n_tt = ih->in.pdu.n_ai.n_tt;
ih->in.cfg_bs = ih->config.bs;
if (n_pdu_pack(ih->addr_md, &ih->fl_pdu, &id, ih->out.msg) != N_OK)
{
ih->out.sts = (ih->out.sts & (~N_S_TX_BUSY));
return N_ERROR;
}
ih->clbs.send_frame(ih->fr_id_type, id, ih->in.fr_fmt, n_get_dt_offset(ih->addr_md, N_PCI_T_FC, ih->fl_pdu.sz), ih->fl_pdu.dt);
ih->out.sts = (ih->out.sts & (~N_S_TX_BUSY));
return N_OK;
}
/*
* Helper function to set some basic stream parameters value
*/
inline static void set_stream_data(n_iostream_t* ist, uint8_t cf, uint8_t wf, stream_sts sts)
{
ist->cf_cnt = cf;
ist->wf_cnt = wf;
ist->sts = sts;
}
/*
* Check if current Wait Flow status counter reached the max WFS
*/
inline static n_rslt check_max_wf_capacity(iso15765_t* ih)
{
if (ih->out.wf_cnt < ih->config.wf)
{
return N_OK;
}
ih->clbs.on_error(N_WFT_OVRN);
set_stream_data(&ih->out, 0, 0, N_S_IDLE);
return N_WFT_OVRN;
}
/*
* Process inbound First Frame reception and report to the upper layer using the
* indication callback function.
*/
static n_rslt process_in_ff(iso15765_t* ih)
{
if (ih->in.msg_sz > I15765_MSG_SIZE)
{
ih->clbs.on_error(N_INV_REQ_SZ);
return N_INV_REQ_SZ;
}
/* If reception is in progress: Terminate the current reception, report an
* N_USData.indication, with <N_Result> set to N_UNEXP_PDU, to the upper layer, and
* process the FF N_PDU as the start of a new reception.*/
if ((ih->in.sts & N_S_RX_BUSY) != 0)
{
ih->clbs.on_error(N_UNE_PDU);
signaling(N_INDN, &ih->in, (void*)ih->clbs.indn, ih->in.msg_sz, N_UNE_PDU);
}
/* Copy all data, init the CFrames reception parameters and send a FC */
memmove(ih->in.msg, ih->in.pdu.dt, ih->in.pdu.sz);
ih->in.msg_sz = ih->in.pdu.n_pci.dl;
ih->in.msg_pos = ih->in.pdu.sz;
ih->in.cf_cnt = 0;
ih->in.wf_cnt = 0;
signaling(N_FF_INDN, &ih->in, (void*)ih->clbs.ff_indn, ih->in.msg_sz, N_OK);
send_N_PCI_T_FC(ih);
return N_OK;
}
/*
* Process inbound Single Frame reception and report to the upper layer using the
* indication callback function.
*/
static n_rslt process_in_sf(iso15765_t* ih)
{
/* If reception is in progress: Terminate the current reception, report an
* N_USData.indication, with <N_Result> set to N_UNEXP_PDU, to the upper layer, and
* process the SF N_PDU as the start of a new reception.*/
if ((ih->in.sts & N_S_RX_BUSY) != 0)
{
ih->clbs.on_error(N_UNE_PDU);
signaling(N_INDN, &ih->in, (void*)ih->clbs.indn, ih->in.msg_sz, N_UNE_PDU);
}
memmove(&ih->in.msg[0], ih->in.pdu.dt, ih->in.pdu.n_pci.dl);
ih->in.sts = N_S_IDLE;
signaling(N_INDN, &ih->in, (void*)ih->clbs.indn, ih->in.pdu.n_pci.dl, N_OK);
return N_OK;
}
/*
* Process inbound Consecutive Frames. Perform all the required checks according
* to (ref: iso15765-2 p.26) and if everything is ok copy all the data to the
* inbound stream buffer and update the reception parameters (CF_cnt,timeouts etc)
*/
static n_rslt process_in_cf(iso15765_t* ih)
{
n_rslt rslt = N_OK;
/* According to (ref: iso15765-2 p.26) if we are not in progress of
* reception we should ignore it */
if ((ih->in.sts & N_S_RX_BUSY) == 0)
{
rslt = N_UNE_CF;
goto in_cf_error;
}
/* Increase the CF counter and check if the reception sequence is ok */
ih->in.cf_cnt = ih->in.cf_cnt + 1 > 0xFF ? 0 : ih->in.cf_cnt + 1;
ih->in.sn_glb = (ih->in.sn_glb + 1) & 0x0F;
if (ih->in.sn_glb != ih->in.pdu.n_pci.sn)
{
rslt = N_INV_SEQ_NUM;
goto in_cf_error;
}
/* As long as everything is ok the we copy the frame data to the inbound
* stream buffer. Afterwards check if the message size is completed and
* signal the user and afterwards reset the inboud stream */
memmove(&ih->in.msg[ih->in.msg_pos], ih->in.pdu.dt, ih->in.pdu.sz);
ih->in.msg_pos += ih->in.pdu.sz;
if (ih->in.msg_pos >= ih->in.msg_sz)
{
signaling(N_INDN, &ih->in, (void*)ih->clbs.indn, ih->in.msg_sz, N_OK);
memset(&ih->in, 0, sizeof(n_iostream_t));
ih->in.last_upd.n_cr = 0;
// ih->in.sts = N_S_IDLE;
return N_OK;
}
/* if we reach the max CF counter, then we send a FC frame */
if(ih->config.bs != 0)
{
if (ih->in.cf_cnt == ih->config.bs)
{
ih->in.cf_cnt = 0;
send_N_PCI_T_FC(ih);
}
}
/* Update the Cr timer */
ih->in.last_upd.n_cr = ih->clbs.get_ms();
return rslt;
in_cf_error:
ih->clbs.on_error(rslt);
ih->in.sts = N_S_IDLE;
return rslt;
}
/*
* Process inbound Flow Control Frames. Outcome depends on the stream status
* (if it is busy etc) as well as the Flow Control Status.
*/
static n_rslt process_in_fc(iso15765_t* ih)
{
n_rslt rslt = N_UNE_PDU;
/* According to (ref: iso15765-2 p.26) if we are not expecting FC frame
* we should ignore it */
if (ih->out.sts != N_S_TX_WAIT_FC)
{
return rslt;
}
switch (ih->in.pdu.n_pci.fs)
{
case N_WAIT:
/* Increase the WF counter, check if we reached the WF Limit to abort
* the reception and (if not WF overflow) update the Bs time */
ih->out.wf_cnt += 1;
if (check_max_wf_capacity(ih) != N_WFT_OVRN)
{
return N_OK;
}
ih->out.last_upd.n_bs = ih->clbs.get_ms();
rslt = N_WFT_OVRN;
break;
case N_OVERFLOW:
rslt = N_BUFFER_OVFLW;
break;
case N_CONTINUE:
/* Store the requested transmission parameters (from receiver)
* to the outbound stream, reset the counters of CFs(1) and WFs(0)
* and change the outbound stream status to Ready */
ih->out.cfg_bs = ih->in.pdu.n_pci.bs;
ih->out.stmin = ih->in.pdu.n_pci.st;
set_stream_data(&ih->out, 1, 0, N_S_TX_READY);
return N_OK;
default:
rslt = N_UNE_FC_STS;
break;
}
/* If there is an error (only way to be here) then reset the
* outbound stream, set the counters of CFs(0) and WFs(0)
* and change the outbound stream status to Idle. Use on_error
* callback to inform the upper layer */
set_stream_data(&ih->out, 0, 0, N_S_IDLE);
ih->clbs.on_error(rslt);
ih->in.sts = N_S_IDLE;
return rslt;
}
/*
* Inbound stream process. The function receives a canbus frame dequeued by
* the 'iso15765_process' and performs any needed operation to identify and
* consume the underlying information.
*/
inline static n_rslt iso15765_process_in(iso15765_t* ih, canbus_frame_t* frame)
{
/* Converting the canbus frame to PDU format and process it by its PCI Type */
ih->in.fr_fmt = frame->fr_format;
if (n_pdu_unpack(ih->addr_md, &ih->in.pdu, frame->id, (uint8_t)frame->dlc, frame->dt) == N_OK)
{
switch (ih->in.pdu.n_pci.pt)
{
case N_PCI_T_FC:
return process_in_fc(ih);
case N_PCI_T_CF:
return process_in_cf(ih);
case N_PCI_T_SF:
return process_in_sf(ih);
case N_PCI_T_FF:
return process_in_ff(ih);
default:
break;
}
}
/* According to (ref: iso15765-2 p.26) if PDU is not valid
* we should ignore it */
ih->clbs.on_error(N_INV_PDU);
return N_INV_PDU;
}
/*
* Procces the outbound stream.
*/
static n_rslt iso15765_process_out(iso15765_t* ih)
{
/* if there is no pending action just return */
if (ih->out.sts != N_S_TX_BUSY && ih->out.sts != N_S_TX_READY)
{
return N_IDLE;
}
uint32_t id;
n_rslt rslt = N_ERROR;
n_rslt timeout = N_ERROR;
/* Find the PCI type of the pending outbound stream */
ih->out.pdu.n_pci.pt = n_out_frame_type(ih);
switch (ih->out.pdu.n_pci.pt)
{
case N_PCI_T_SF:
/* Copy all the data of the SF to the outbound stream, pack and send the canbus frame */
ih->out.pdu.n_pci.dl = ih->out.msg_sz;
ih->out.pdu.sz = ih->out.msg_sz;
if (n_pdu_pack(ih->addr_md, &ih->out.pdu, &id, ih->out.msg) != N_OK)
{
goto iso15765_process_out_cfm;
}
rslt = ih->clbs.send_frame(ih->fr_id_type, id, ih->out.fr_fmt, n_get_closest_can_dl(ih->out.pdu.sz + n_get_dt_offset(ih->addr_md, N_PCI_T_SF, ih->out.pdu.sz), ih->out.fr_fmt), ih->out.pdu.dt) == 0 ? N_OK : N_ERROR;
goto iso15765_process_out_cfm;
break;
case N_PCI_T_FF:
/* Copy all the data of the FF to the outbound stream for transmission and prepare the service
* for a multi-frame reception */
ih->out.pdu.n_pci.dl = ih->out.msg_sz;
ih->out.wf_cnt = 0;
ih->out.pdu.sz = ih->out.fr_fmt == CBUS_FR_FRM_STD ? ((ih->addr_md & 0x01) == 0 ? 6 : 5) : ((ih->addr_md & 0x01) == 0 ? 62 : 61);
ih->out.msg_pos = ih->out.pdu.sz;
if (n_pdu_pack(ih->addr_md, &ih->out.pdu, &id, ih->out.msg) != N_OK)
{
goto iso15765_process_out_cfm;
}
ih->out.cf_cnt = 1;
/* after this frame we expect a Flow Control then assign the correct flag before the
* transmission to avoid any issues and start the timer */
ih->out.sts = N_S_TX_WAIT_FC;
rslt = ih->clbs.send_frame(ih->fr_id_type, id, ih->out.fr_fmt, ih->out.fr_fmt == CBUS_FR_FRM_STD ? 8 : 64, ih->out.pdu.dt) == 0 ? N_OK : N_ERROR;
ih->out.last_upd.n_bs = ih->clbs.get_ms();
return (rslt == 0) ? N_OK : N_ERROR;
case N_PCI_T_CF:
/* if the minimun difference between transmissions is not reached then skip */
timeout = has_interval_passed(ih->clbs.get_ms(), ih->out.last_upd.n_cs, ih->out.stmin);
if (timeout == N_INV)
{
return N_OK;
}
else if (timeout == N_ERROR)
{
return N_ERROR;
}
/* Increase the sequence number of the frame and the CF counter of the stream
* and then pack the PDU to a CANBus frame */
ih->out.pdu.n_pci.sn = ih->out.sn_glb;
ih->out.sn_glb = (ih->out.sn_glb + 1) & 0x0F;
if (ih->out.fr_fmt == CBUS_FR_FRM_STD)
{
uint8_t max_payload = (ih->addr_md & 0x01) == 0 ? 7 : 6;
ih->out.pdu.sz = ih->out.msg_sz - ih->out.msg_pos;
ih->out.pdu.sz = ih->out.pdu.sz >= max_payload ? max_payload : ih->out.pdu.sz;
}
else
{
uint8_t max_payload = (ih->addr_md & 0x01) == 0 ? 63 : 62;
ih->out.pdu.sz = ih->out.msg_sz - ih->out.msg_pos;
ih->out.pdu.sz = ih->out.pdu.sz >= max_payload ? max_payload : ih->out.pdu.sz;
}
if (n_pdu_pack(ih->addr_md, &ih->out.pdu, &id, &ih->out.msg[ih->out.msg_pos]) != N_OK)
{
goto iso15765_process_out_cfm;
}
/* Increase the position which indicates the remaining data in the inbound buffer */
ih->out.msg_pos += ih->out.pdu.sz;
/* if after this frame we expect a Flow Control then assign the correct flag before the
* transmission to avoid any issues and start the timer */
if (ih->out.cf_cnt == ih->out.cfg_bs)
{
ih->out.sts = N_S_TX_WAIT_FC;
ih->out.last_upd.n_bs = ih->clbs.get_ms();
}
ih->out.cf_cnt = ih->out.cf_cnt == 0xFF ? 0 : ih->out.cf_cnt + 1;
/* send the canbus frame! */
uint8_t of1 = (ih->addr_md & 0x01) == 0 ? 1 : 2;
rslt = ih->clbs.send_frame(ih->fr_id_type, id, ih->out.fr_fmt, n_get_closest_can_dl(ih->out.pdu.sz + of1, ih->out.fr_fmt), ih->out.pdu.dt) == 0 ? N_OK : N_ERROR;
ih->out.last_upd.n_cs = ih->clbs.get_ms();
if (ih->out.msg_pos >= ih->out.msg_sz)
{
goto iso15765_process_out_cfm;
}
return N_OK;
default:
break;
}
return N_ERROR;
iso15765_process_out_cfm:
ih->out.sts = N_S_IDLE;
ih->out.cf_cnt = 0;
ih->out.wf_cnt = 0;
signaling(N_CONF, &ih->out, (void*)ih->clbs.cfm, 0, rslt);
return rslt;
}
/******************************************************************************
* Definition | Public Functions
******************************************************************************/
/*
* Initialize and check if the configuration parameters of the ISO15765 handler are
* correct. Not assigned callback which are not assigned will be automatically assigned
* to the static UNUSED functions. If no error occurs, the system can start the service.
*/
n_rslt iso15765_init(iso15765_t* instance)
{
if (instance == NULL)
{
return N_NULL;
}
/* check if parameters have correct values */
if ((instance->fr_id_type != CBUS_ID_T_STANDARD && instance->fr_id_type != CBUS_ID_T_EXTENDED)
|| (instance->fr_id_type & instance->addr_md) == 0)
{
return N_WRG_VALUE;
}
/* check if must-have functions are assigned */
if (instance->clbs.send_frame == NULL || instance->clbs.get_ms == NULL)
{
return N_MISSING_CLB;
}
/* if optional functions are not assigned then use the defaults */
if (instance->clbs.indn == NULL)
{
instance->clbs.indn = indn;
}
if (instance->clbs.ff_indn == NULL)
{
instance->clbs.ff_indn = ff_indn;
}
if (instance->clbs.cfm == NULL)
{
instance->clbs.cfm = cfm;
}
if (instance->clbs.cfg_cfm == NULL)
{
instance->clbs.cfg_cfm = cfg_cfm;
}
/* clear the in/out streams */
memset(&instance->in, 0, sizeof(n_iostream_t));
memset(&instance->out, 0, sizeof(n_iostream_t));
memset(&instance->fl_pdu, 0, sizeof(n_pdu_t));
/* init the incoming canbus frame queue(buffer) */
if (iqueue_init(&instance->inqueue,
I15765_QUEUE_ELMS,
sizeof(canbus_frame_t),
instance->inq_buf) != I_OK)
{
return N_INV;
}
ISO_15675_UNUSED(sgn_chg_cfm);
instance->init_sts = N_OK;
return instance->init_sts;
}
/*
* Enqueues an incoming frame from the lower level (CANBus) to a buffer. The service
* will process the frames during the call of the 'iso15765_process' function. Usually
* this function should be called when a canbus frame is received.
*/
n_rslt iso15765_enqueue(iso15765_t* instance, canbus_frame_t* frame)
{
if (instance == NULL || frame == NULL)
{
return N_NULL;
}
if (instance->init_sts != N_OK)
{
return N_ERROR;
}
if (frame->fr_format == CBUS_FR_FRM_STD)
{
if (frame->dlc == 0 || frame->dlc > 8)
{
return N_ERROR;
}
}
else if (frame->fr_format == CBUS_FR_FRM_FD)
{
if (frame->dlc == 0 ||
(frame->dlc > 8 && frame->dlc != 12 && frame->dlc != 16 &&
frame->dlc != 20 && frame->dlc != 24 && frame->dlc != 32 &&
frame->dlc != 48 && frame->dlc != 64))
{
return N_ERROR;
}
}
else
{
return N_ERROR;
}
return iqueue_enqueue(&instance->inqueue, frame) == I_OK
? N_OK : N_BUFFER_OVFLW;
}
/*
* Request to send a message. Depending on the message a call to 'iso15765_process'
* may be required. The service can send one message per time as long as the
* communication is syncronous
*/
n_rslt iso15765_send(iso15765_t* instance, n_req_t* frame)
{
if (instance == NULL)
{
return N_NULL;
}
if (instance->init_sts != N_OK)
{
return N_ERROR;
}
/* Make sure that there is no transmission in progress */
if (instance->out.sts != N_S_IDLE)
{
return N_TX_BUSY;
}
/* and the requested size is fitting in our outbound buffer */
if (frame->msg_sz > I15765_MSG_SIZE)
{
return N_BUFFER_OVFLW;
}
/* or there is not actual message to be sent */
if (frame->msg_sz == 0)
{
return N_INV_REQ_SZ;
}
/* check if frame type is correct */
if (frame->fr_fmt != CBUS_FR_FRM_STD && frame->fr_fmt != CBUS_FR_FRM_FD)
{
return N_INV;
}
/* check if Target Address Type is correct */
if (frame->n_ai.n_tt != N_TA_T_PHY && frame->n_ai.n_tt != N_TA_T_FUNC)
{
return N_INV;
}
/* copy all the info and data to the outbound buffer */
instance->out.fr_fmt = frame->fr_fmt;
instance->out.msg_sz = frame->msg_sz;
memmove(instance->out.msg, frame->msg, frame->msg_sz);
memmove(&instance->out.pdu.n_ai, &frame->n_ai, sizeof(n_ai_t));
instance->out.sn_glb = 1;
instance->out.cf_cnt = 0;
instance->out.wf_cnt = 0;
instance->out.sts = N_S_TX_BUSY;
return N_OK;
}
/*
* Process the inbound/outbound streams of the service. For optimal operation
* this function should be called continiously with a minimal delay. It is
* suggested to be used in a thread.
*/
n_rslt iso15765_process(iso15765_t* instance)
{
if (instance == NULL)
{
return N_NULL;
}
if (instance->init_sts != N_OK)
{
return N_ERROR;
}
/* First check if a timeout is occured. Only for the inbound stream */
n_rslt rslt = process_timeouts(instance);
canbus_frame_t frame;
/* Dequeue all the incoming frames and process them */
while (iqueue_dequeue(&instance->inqueue, &frame) != I_EMPTY)
{
rslt |= iso15765_process_in(instance, &frame);
}
/* Process the outbound stream */
rslt |= iso15765_process_out(instance);
return rslt;
}
/******************************************************************************
* EOF - NO CODE AFTER THIS LINE
******************************************************************************/
================================================
FILE: src/lib_iso15765.h
================================================
/*!
@file iso15765_2.h
@brief Header file of the ISO15765-2 library
@t.odo -
---------------------------------------------------------------------------
GNU Affero General Public License v3.0
Copyright (c) 2024 Ioannis D. (devcoons)
This program is free software: you can redistribute it and/or modify it
under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
For commercial use, including proprietary or for-profit applications,
a separate license is required. Contact:
- GitHub: [https://github.com/devcoons](https://github.com/devcoons)
- Email: i_-_-_s@outlook.com
*/
/******************************************************************************
* Preprocessor Definitions & Macros
******************************************************************************/
#ifndef DEVCOONS_ISO15765_2_H_
#define DEVCOONS_ISO15765_2_H_
#define I15765_MSG_SIZE 516 /* Max. size of the TP up to 4095 bytes */
#define I15765_QUEUE_ELMS 64 /* No. of max incoming frames that the
* reception buffer can hold */
/* Alignment is required for Microcontrollers */
#if defined(__clang__)
#define ALIGNMENT __attribute__ ((aligned (4)))
#elif defined(__GNUC__) || defined(__GNUG__)
#define ALIGNMENT __attribute__ ((aligned (4)))
#elif defined(_MSC_VER)
#define ALIGNMENT __declspec(align(4))
#endif
#ifndef ISO_15675_UNUSED
#define ISO_15675_UNUSED(x) ((void)(x))
#endif
/******************************************************************************
* Includes
******************************************************************************/
#include <stdlib.h>
#include <stdint.h>
#include "lib_iqueue.h"
/******************************************************************************
* Enumerations, structures & Variables
******************************************************************************/
/* --- CANTP Mode - Frame Type (ref: ) ----------------- */
#ifndef CBUS_FR_FORMAT
#define CBUS_FR_FORMAT
typedef enum
{
CBUS_FR_FRM_STD = 0x01, /* Standard CANBUS */
CBUS_FR_FRM_FD = 0x02 /* FD CANBUS */
}cbus_fr_format;
#endif
/* --- CANBus Mode [ID Type] (ref: iso15765-2 p.8) -------==---------------- */
#ifndef CBUS_ID_TYPE
#define CBUS_ID_TYPE
typedef enum
{
CBUS_ID_T_STANDARD = 0x04U, /* 11bits CAN Identifier */
CBUS_ID_T_EXTENDED = 0x08U /* 29bits CAN Identifier */
}cbus_id_type;
#endif
/* --- CANBus Frame (ref: iso15765-2 p.) ----------------------------------- */
#ifndef CANBUS_FRAME
#define CANBUS_FRAME
typedef struct
{
uint32_t id; /* CAN Frame Id */
uint32_t id_type; /* CAN Frame Id Type `cbus_id_type` */
uint16_t fr_format; /* CAN Frame Format `cbus_fr_format` */
uint16_t dlc; /* Size of data */
uint8_t dt[64]; /* Actual data of the frame */
}canbus_frame_t;
#endif
/* --- CANTP Addressing Mode (ref: iso15765-2 p.28) ------------------------ */
typedef enum
{
N_ADM_UNKN = 0x00, /* Not defined */
N_ADM_NORMAL = 0x14, /* Normal Mode */
N_ADM_FIXED = 0x28, /* Fixed Mode */
N_ADM_MIXED11 = 0x35, /* Mixed 11bits ID Mode */
N_ADM_EXTENDED = 0x45, /* Extended Mode */
N_ADM_MIXED29 = 0x59 /* Mixed 29bits ID Mode */
}addr_md;
/* --- Protocol Control Information Type (ref: iso15765-2 p.17) ------------ */
typedef enum
{
N_PCI_T_SF = 0x00U, /* Single Frame */
N_PCI_T_FF = 0x01U, /* First Frame */
N_PCI_T_CF = 0x02U, /* Consecutive Frame */
N_PCI_T_FC = 0x03U, /* Flow Control Frame */
N_PCI_T_UN = 0xFF /* Unknown */
}pci_type;
/* --- Network Target Address Type (ref: iso15765-2 p.9,29,30) ------------- */
typedef enum
{
N_TA_T_PHY = 0x01, /* Physical */
N_TA_T_FUNC = 0x02 /* Functional */
}ta_type;
/* --- Message Type (ref: iso15765-2 p.8) ---------------------------------- */
typedef enum
{
N_MT_DIAG = 0x00, /* Diagnostics */
N_MT_RDIAG = 0x01 /* Remote Diagnostics */
}mtype;
/* --- FlowControl Parameters (ref: iso15765-2 p.--------------------------- */
typedef enum
{
N_ST_MIN = 0x00U, /* SeparationTimeMin: the minimum time the sender should
* wait between the transmissions of two CF N_PDUs */
N_BS = 0x01U, /* BlockSize: the max. number of N_PDUs the receiver allows
* the sender to send, before waiting for an authorization
* to continue transmission of the following N_PDUs */
}fl_param;
/* --- N rslts (ref: iso15765-2 p.10-11) ----------------------------------- */
typedef enum
{
N_OK = 0x0000, /* service execution has completed successfully */
N_TIMEOUT_A = 0x0001, /* when the timer N_Ar/N_As has passed its time-out
* value N_Asmax / N_Armax; */
N_TIMEOUT_Bs = 0x0002, /* when the timer N_Bs has passed its time-out value
* N_Bsmax */
N_TIMEOUT_Cr = 0x0004, /* when the timer N_Cr has passed its time-out value
* N_Crmax */
N_WRG_SN = 0x0008, /* upon reception of an unexpected sequence number
* (PCI.SN) value */
N_INV_FS = 0x0010, /* when an invalid or unknown FlowStatus value has
* been received in a flow control(FC) N_PDU */
N_UNE_PDU = 0x0020, /* upon reception of an unexpected protocol data unit; */
N_WFT_OVRN = 0x0040, /* upon reception of flow control WAIT frame that
* exceeds the maximum counter N_WFTmax */
N_BUFFER_OVFLW = 0x0080, /* upon reception of a flow control (FC) N_PDU with
* FlowStatus = OVFLW */
N_ERROR = 0x0100, /* when an error has been detected by the network layer
* and no other parameter value can be used to better
* describe the error. */
N_IDLE = 0x0110, /* service execution has completed successfully and not TXRX */
N_TX_BUSY = 0x0120, /* service execution has completed successfully and TX pending */
N_RX_BUSY = 0x0140, /* service execution has completed successfully and RX pending */
N_WRG_PARAM = 0x1003, /* the service did not execute due to an undefined <Parameter> */
N_WRG_VALUE = 0x1004, /* the service did not execute due to an out of range <Parameter_Value>; */
N_INV = 0x1005, /* the service did not execute due to an invalid requested operation */
N_MISSING_CLB = 0x1006, /* the service did not execute due to missing mandatory callback functions */
N_NULL = 0xFFFF, /* the service has a null pointer (this should never happen) */
N_FC_TIMEOUT = 0x1001, /* Flow Control Timeout. @n_timeouts.n_bs */
N_CF_TIMEOUT = 0x1002, /* ConsecutiveFrame Timeout. @n_timeouts.n_cr */
N_CAN_DT_INV = 0x1003, /* Invalid CAN Data Error */
N_UNE_FC = 0x1004, /* FlowControl was received without being expected. */
N_UNE_CF = 0x1005, /* ConsecutiveFrame was received without being expected. */
N_RCV_INT_SF = 0x1006, /* Frames sequence was not correct. Service received a
* single frame during a Multi-frame reception. */
N_RCV_INT_FF = 0x1007, /* Frames sequence was not correct. Service received a
* first frame during a Multi-frame reception. */
N_RCV_INT_FC = 0x1012, /* ReceptionInterruptedWithFC */
N_INV_SEQ_NUM = 0x1008, /* ConsecutiveFrame sequence number was invalid. */
N_UNS_WF = 0x1009, /* Unsuported WaitFrame */
N_MAX_WF_REAC = 0x100A, /* Maximum Wait Frames Reached */
N_INV_PDU = 0x1010, /* PDU Conversion failed */
N_INV_REQ_SZ = 0x010E, /* Invalid Request Size */
N_UNE_FC_STS = 0x1011, /* Flow control status was invalid and/or unexpected. */
N_OVFLW = 0x100F, /* Buffer Overflow. */
}n_rslt;
/* --- N_PCI_T_FC Status (ref: iso15765-2 p.8) ---------------------------- */
typedef enum
{
N_CONTINUE = 0x00U, /* continue to send, the authorization to continue */
N_WAIT = 0x01U, /* the request to continue to wait */
N_OVERFLOW = 0x02U /* buffer overflow, the indication that the number of bytes specified
* in the FirstFrame of the segmented message exceeds the number of
* bytes that can be stored in the buffer of the receiver entity */
}flow_sts;
/* --- dt I/O Stream Status (ref: iso15765-2 p.8) ------------------------ */
typedef enum
{
N_S_IDLE = 0x00, /* Has to pending action */
N_S_RX_BUSY = 0x02, /* Reception is in progress */
N_S_TX_BUSY = 0x04, /* Transmission is in progress */
N_S_TX_READY = 0x05, /* Transmission is ready to begin */
N_S_TX_WAIT_FC = 0x10, /* Transmission is in progress and waits
* for a Flow control frame */
}stream_sts;
/* --- Network Layer Timing PARAMs (ref: iso15765-2 p.) -------------------- */
typedef struct ALIGNMENT
{
uint32_t n_bs; /* FlowControl N_PDU not received (lost, overwritten) on the
* sender side or preceding FirstFrame N_PDU or ConsecutiveFrame
* N_PDU not received(lost, overwritten) on the receiver side.
* Abort message transmission and issue N_USData.confirm with
* <N_Result> = N_TIMEOUT_Bs. */
uint32_t n_cr; /* ConsecutiveFrame N_PDU not received (lost, overwritten) on
* the receiver side or preceding FC N_PDU not received(lost,
* overwritten) on the sender side. Abort message reception and issue
* N_USData.indication with <N_Result> = N_TIMEOUT_Cr */
uint32_t n_cs;
}n_timeouts;
/* --- Address information (ref: iso15765-2 p.) ---------------------------- */
typedef struct ALIGNMENT
{
uint8_t n_pr; /* Network Address Priority */
uint8_t n_sa; /* Network Source Address */
uint8_t n_ta; /* Network Target Address */
uint8_t n_ae; /* Network Address Extension */
ta_type n_tt; /* Network Target Address type */
}n_ai_t;
/* --- Protocol control information (ref: iso15765-2 p.) ------------------- */
typedef struct ALIGNMENT
{
uint8_t fs; /* FlowStatus: whether the sending network entity can
* proceed with the message transmission 'flow_sts' */
uint8_t bs; /* BlockSize */
uint8_t sn; /* SequenceNumber: specify the order of the consecutive frames */
uint8_t st; /* SeparationTime: Requested separation time */
pci_type pt; /* Type of the received pdu 'pci_type' */
uint16_t dl; /* PCI data length (if 0 frame must be ignored) */
}n_pci_t;
/* --- Protocol dt unit (ref: iso15765-2 p.) ------------------------------- */
typedef struct ALIGNMENT
{
mtype n_mt; /* Message Type */
uint16_t sz; /* Actual data size */
n_ai_t n_ai; /* Address information */
n_pci_t n_pci; /* Protocol control information */
uint8_t dt[64]; /* Data Field */
} n_pdu_t;
/* --- N_USdt.cfm (ref: iso15765-2 p.6) ------------------------------------ */
typedef struct ALIGNMENT
{
n_ai_t n_ai; /* Address information */
n_pci_t n_pci; /* Protocol control information */
n_rslt rslt; /* Result of the request */
}n_cfm_t;
/* --- N_FF.indn (ref: iso15765-2 p.6) ------------------------------------ */
typedef struct ALIGNMENT
{
cbus_fr_format fr_fmt; /* CANBus Frame format */
n_ai_t n_ai; /* Address information */
n_pci_t n_pci; /* Protocol control information */
uint16_t msg_sz; /* Size of the message that will be received */
}n_ff_indn_t;
/* --- N_USData.request (ref: iso15765-2 p6.) ------------------------------ */
typedef struct ALIGNMENT
{
cbus_fr_format fr_fmt; /* CANBus Frame format */
n_ai_t n_ai; /* Address information */
n_pci_t n_pci; /* Protocol control information */
uint16_t msg_sz; /* Message actual size */
uint8_t msg[I15765_MSG_SIZE]; /* Message to be transmitted */
}n_req_t;
/* --- N_USdt.indn (ref: iso15765-2 p.7) ---------------------------------- */
typedef struct ALIGNMENT
{
cbus_fr_format fr_fmt; /* CANBus Frame format */
n_ai_t n_ai; /* Address information */
n_pci_t n_pci; /* Protocol control information */
n_rslt rslt; /* Result of the reception */
uint16_t msg_sz; /* Received message actual size */
uint8_t msg[I15765_MSG_SIZE]; /* Received message data */
}n_indn_t;
typedef struct ALIGNMENT
{
n_ai_t n_ai; /* Address information. Not supported:
* the lib handles only 1 stream */
n_pci_t n_pci; /* Protocol control information. Not supported:
* the lib handles only 1 stream) */
fl_param param; /* Parameter to change */
uint8_t pval; /* Value to set */
}n_chg_param_req_t;
typedef struct ALIGNMENT
{
n_ai_t n_ai; /* Address information. Not supported:
* the lib handles only 1 stream */
n_pci_t n_pci; /* Protocol control information. Not
* supported: because the lib handles only 1 stream */
fl_param param; /* Requested Parameter to change */
uint8_t pval; /* Requested Value to set */
n_rslt rslt; /* Result of the request */
}n_chg_param_cfm_t;
typedef enum
{
N_INDN = 0x01, /* N_USData.indication */
N_FF_INDN = 0x02, /* N_USData_FF.indication */
N_CONF = 0x03, /* N_USData.confirm */
N_CHG_P_CONF = 0x04 /* N_ChangeParameter.confirm */
}signal_tp;
/* --- Callbacks ---------------------------------------------------------- */
typedef struct ALIGNMENT
{
void (*indn)(n_indn_t*); /* Indication Callback: Will be fired when a reception
* is available or an error occured during the reception. */
void (*ff_indn)(n_ff_indn_t*); /* First Frame Indication Callback: Will be fired when a
* FF is received, giving back some useful information */
void (*cfm)(n_cfm_t*); /* This callback confirms to the higher layers that the requested
* service has been carried out */
void (*cfg_cfm)(n_chg_param_cfm_t*); /* This service confirms to the upper layer that the request to
* change a specific protocol has been carried out */
void (*pdu_custom_pack)(n_pdu_t*, uint32_t*); /* Custom CAN ID packing for 11bits ID. If assinged the default
* packing will be skipped */
void (*pdu_custom_unpack)(n_pdu_t*, uint32_t*); /* Custom CAN ID uppacking for 11bits ID. If assinged the default
* uppacking will be skipped */
void (*on_error)(n_rslt); /* Will be fired in any occured error. */
uint32_t(*get_ms)(); /* Time-source for the library in ms(required) */
uint8_t(*send_frame) /* Callback to assing the Network Layer. This callback */
( /* will be fired when a transmission of a canbus frame is ready. */
cbus_id_type, /* - CANBus Frame ID Type [Standard or Extended] */
uint32_t, /* - Frame ID */
cbus_fr_format, /* - Frame Type: [CLASSIC or FD]*/
uint8_t, /* - Frame Data Length */
uint8_t* /* - Frame Data Array */
);
}n_callbacks_t;
/* --- PDU Stream --------------------------------------------------------- */
typedef struct ALIGNMENT
{
cbus_fr_format fr_fmt; /* CANBus Frame format */
n_pdu_t pdu; /* Keep information about the in/out frame in a PDU format */
uint8_t cf_cnt; /* Current block sequence number (ConsecutiveFrame) */
uint8_t wf_cnt; /* Current received wait flow control frames */
uint8_t sn_glb; /* Current Sequence Number of the transmittion */
uint8_t cfg_wf; /* Max supported Wait Flow Control frames */
uint8_t stmin; /* Frames transmission rate */
uint8_t cfg_bs; /* Max. supported block sequence (ConsecutiveFrame) */
stream_sts sts; /* Stream status */
uint16_t msg_sz; /* Actual message buffer size */
uint16_t msg_pos; /* Transmit message buffer position */
n_timeouts last_upd; /* Time keeper for timouts */
uint8_t msg[I15765_MSG_SIZE]; /* Received/Transmit message buffer */
}n_iostream_t;
/* --- iso15765 timing configuration (ref: iso15765-2 p.25)----------------- */
typedef struct ALIGNMENT
{
uint8_t stmin; /* Default min. frame transmission separation */
uint8_t bs; /* Max. Block size during transmission */
uint8_t wf; /* Max. accepted Wait Requests from the FlowControl */
uint16_t n_bs; /* Time until reception of the next FlowControl N_PDU */
uint16_t n_cr; /* Time until reception of the next ConsecutiveFrame N_PDU */
}n_config_t;
/* --- iso15765 Handler --------------------------------------------------- */
typedef struct ALIGNMENT
{
n_rslt init_sts; /* Instance is initialized correctly */
addr_md addr_md; /* Selected address mode of the TP */
cbus_id_type fr_id_type; /* CANBus frame Id Type */
n_iostream_t in; /* Incoming data stream (reception) */
n_iostream_t out; /* Outcoming data stream (transmission) */
n_pdu_t fl_pdu; /* Flow control pdu */
n_callbacks_t clbs; /* Callbacks */
n_config_t config; /* Default configuration to be used. (timing etc) */
n_timeouts cfg_timeout; /* Timeouts configuration */
iqueue_t inqueue; /* Queue handler for the incoming canbus frames */
uint8_t inq_buf[I15765_QUEUE_ELMS * sizeof(canbus_frame_t)]; /* Queue buffer */
}iso15765_t;
/******************************************************************************
* Declaration | Public Functions
******************************************************************************/
n_rslt iso15765_init(iso15765_t* instance);
n_rslt iso15765_send(iso15765_t* instance, n_req_t* frame);
n_rslt iso15765_enqueue(iso15765_t* instance, canbus_frame_t* frame);
n_rslt iso15765_process(iso15765_t* instance);
/******************************************************************************
* EOF - NO CODE AFTER THIS LINE
******************************************************************************/
#endif
gitextract_hntsohq7/
├── .github/
│ ├── FUNDING.yml
│ └── workflows/
│ └── build.yml
├── CMakeLists.txt
├── LICENSE
├── Makefile
├── README.md
├── doc/
│ └── .gitkeep
├── exm/
│ └── ISO15765Development.c
├── lib/
│ ├── lib_iqueue.c
│ └── lib_iqueue.h
└── src/
├── lib_iso15765.c
└── lib_iso15765.h
SYMBOL INDEX (71 symbols across 5 files)
FILE: exm/ISO15765Development.c
function rand_string (line 84) | static void rand_string(uint8_t* str, size_t size) {
function getms (line 92) | static uint32_t getms() {
function print_frame (line 102) | static void print_frame(uint8_t instance, uint8_t tp_mode, cbus_id_type ...
function send_frame1 (line 119) | static uint8_t send_frame1(cbus_id_type id_type, uint32_t id, cbus_fr_fo...
function send_frame2 (line 127) | static uint8_t send_frame2(cbus_id_type id_type, uint32_t id, cbus_fr_fo...
function on_error (line 135) | static void on_error(n_rslt err_type) {
function indn1 (line 139) | static void indn1(n_indn_t* info) {
function indn2 (line 161) | static void indn2(n_indn_t* info) {
function main (line 187) | int main() {
FILE: lib/lib_iqueue.c
function i_status (line 56) | i_status iqueue_init(iqueue_t* _queue, uint32_t _max_elements, size_t _e...
function i_status (line 79) | i_status iqueue_advance_next(iqueue_t* _queue)
function i_status (line 94) | i_status iqueue_enqueue(iqueue_t* _queue, void* _element)
function i_status (line 104) | i_status iqueue_dequeue(iqueue_t* _queue, void* _element)
function i_status (line 140) | i_status iqueue_size(iqueue_t* _queue, size_t* _size)
FILE: lib/lib_iqueue.h
type i_status (line 49) | typedef enum
type iqueue_t (line 88) | typedef struct
FILE: src/lib_iso15765.c
function indn (line 62) | static void indn(n_indn_t* info)
function ff_indn (line 67) | static void ff_indn(n_ff_indn_t* info)
function cfm (line 72) | static void cfm(n_cfm_t* info)
function cfg_cfm (line 77) | static void cfg_cfm(n_chg_param_cfm_t* info)
function n_rslt (line 85) | inline static n_rslt has_interval_passed(uint32_t current_time, uint32_t...
function n_get_closest_can_dl (line 99) | inline static uint8_t n_get_closest_can_dl(uint8_t size, cbus_fr_format ...
function n_get_dt_offset (line 149) | inline static uint8_t n_get_dt_offset(addr_md address, pci_type pci, uin...
function pci_type (line 186) | inline static pci_type n_out_frame_type(iso15765_t* instance)
function n_rslt (line 221) | inline static n_rslt n_pci_pack(addr_md mode, n_pdu_t* n_pdu, const uint...
function n_rslt (line 273) | inline static n_rslt n_pci_unpack(addr_md mode, n_pdu_t* n_pdu, uint8_t ...
function n_rslt (line 328) | inline static n_rslt n_pdu_pack_dt(addr_md mode, n_pdu_t* n_pdu, uint8_t...
function n_rslt (line 371) | inline static n_rslt n_pdu_unpack_dt(addr_md mode, n_pdu_t* n_pdu, uint8...
function n_rslt (line 420) | inline static n_rslt n_pdu_pack(addr_md mode, n_pdu_t* n_pdu, uint32_t* ...
function n_rslt (line 479) | inline static n_rslt n_pdu_unpack(addr_md mode, n_pdu_t* n_pdu, uint32_t...
function signaling (line 535) | inline static void signaling(signal_tp tp, n_iostream_t* strm, void(*cb)...
function n_rslt (line 575) | inline static n_rslt process_timeouts(iso15765_t* ih)
function n_rslt (line 600) | static n_rslt send_N_PCI_T_FC(iso15765_t* ih)
function set_stream_data (line 629) | inline static void set_stream_data(n_iostream_t* ist, uint8_t cf, uint8_...
function n_rslt (line 639) | inline static n_rslt check_max_wf_capacity(iso15765_t* ih)
function n_rslt (line 655) | static n_rslt process_in_ff(iso15765_t* ih)
function n_rslt (line 686) | static n_rslt process_in_sf(iso15765_t* ih)
function n_rslt (line 707) | static n_rslt process_in_cf(iso15765_t* ih)
function n_rslt (line 765) | static n_rslt process_in_fc(iso15765_t* ih)
function n_rslt (line 820) | inline static n_rslt iso15765_process_in(iso15765_t* ih, canbus_frame_t*...
function n_rslt (line 850) | static n_rslt iso15765_process_out(iso15765_t* ih)
function n_rslt (line 980) | n_rslt iso15765_init(iso15765_t* instance)
function n_rslt (line 1045) | n_rslt iso15765_enqueue(iso15765_t* instance, canbus_frame_t* frame)
function n_rslt (line 1089) | n_rslt iso15765_send(iso15765_t* instance, n_req_t* frame)
function n_rslt (line 1145) | n_rslt iso15765_process(iso15765_t* instance)
FILE: src/lib_iso15765.h
type cbus_fr_format (line 70) | typedef enum
type cbus_id_type (line 81) | typedef enum
type canbus_frame_t (line 92) | typedef struct
type addr_md (line 104) | typedef enum
type pci_type (line 116) | typedef enum
type ta_type (line 127) | typedef enum
type mtype (line 135) | typedef enum
type fl_param (line 143) | typedef enum
type n_rslt (line 154) | typedef enum
type flow_sts (line 204) | typedef enum
type stream_sts (line 215) | typedef enum
type n_timeouts (line 227) | typedef struct ALIGNMENT
type n_ai_t (line 243) | typedef struct ALIGNMENT
type n_pci_t (line 254) | typedef struct ALIGNMENT
type n_pdu_t (line 267) | typedef struct ALIGNMENT
type n_cfm_t (line 278) | typedef struct ALIGNMENT
type n_ff_indn_t (line 287) | typedef struct ALIGNMENT
type n_req_t (line 297) | typedef struct ALIGNMENT
type n_indn_t (line 308) | typedef struct ALIGNMENT
type n_chg_param_req_t (line 318) | typedef struct ALIGNMENT
type n_chg_param_cfm_t (line 328) | typedef struct ALIGNMENT
type signal_tp (line 340) | typedef enum
type n_callbacks_t (line 350) | typedef struct ALIGNMENT
type n_iostream_t (line 378) | typedef struct ALIGNMENT
type n_config_t (line 397) | typedef struct ALIGNMENT
type iso15765_t (line 408) | typedef struct ALIGNMENT
Condensed preview — 12 files, each showing path, character count, and a content snippet. Download the .json file or copy for the full structured content (115K chars).
[
{
"path": ".github/FUNDING.yml",
"chars": 67,
"preview": "# These are supported funding model platforms\n\ngithub: [devcoons]\n\n"
},
{
"path": ".github/workflows/build.yml",
"chars": 763,
"preview": "name: C/C++ CI\n\non:\n push:\n branches: \n # Push events on master branch\n - master\n pull_request:\n br"
},
{
"path": "CMakeLists.txt",
"chars": 1190,
"preview": "cmake_minimum_required(VERSION 3.10)\nproject(iso15765_canbus C)\n\nset(SRC_DIR src)\nset(LIB_DIR lib)\nset(EXM_DIR exm)\n\ninc"
},
{
"path": "LICENSE",
"chars": 34523,
"preview": " GNU AFFERO GENERAL PUBLIC LICENSE\n Version 3, 19 November 2007\n\n Copyright (C)"
},
{
"path": "Makefile",
"chars": 1193,
"preview": "CC = gcc\nCFLAGS = -Wall -Wextra -Ilib -Isrc -Iexm\n\nSRC_DIR = src\nLIB_DIR = lib\nEXM_DIR = exm\nBUILD_DIR = build\n\nLIBRARY "
},
{
"path": "README.md",
"chars": 7883,
"preview": "# ISO15765-2 CANBus TP \n [![Coda"
},
{
"path": "doc/.gitkeep",
"chars": 0,
"preview": ""
},
{
"path": "exm/ISO15765Development.c",
"chars": 6033,
"preview": "#include <stdio.h>\n#include <stdlib.h>\n#include <string.h>\n#include \"lib_iso15765.h\"\n\n#ifdef _WIN32\n #include <window"
},
{
"path": "lib/lib_iqueue.c",
"chars": 4749,
"preview": "/*!\n@file lib_iqueue.c\n@brief <brief description here>\n@t.odo\t-\n-----------------------------------------------------"
},
{
"path": "lib/lib_iqueue.h",
"chars": 3591,
"preview": "#pragma once\n/*!\n\t@file lib_iqueue.h\n\t@brief <brief description here>\n\t@t.odo\t-\n\t------------------------------------"
},
{
"path": "src/lib_iso15765.c",
"chars": 31537,
"preview": "/*!\n@file iso15765_2.c\n@brief Source file of the ISO15765-2 library\n@t.odo\t-\n----------------------------------------"
},
{
"path": "src/lib_iso15765.h",
"chars": 17280,
"preview": "/*!\n@file iso15765_2.h\n@brief Header file of the ISO15765-2 library\n@t.odo\t-\n----------------------------------------"
}
]
About this extraction
This page contains the full source code of the devcoons/iso15765-canbus GitHub repository, extracted and formatted as plain text for AI agents and large language models (LLMs). The extraction includes 12 files (106.3 KB), approximately 29.8k tokens, and a symbol index with 71 extracted functions, classes, methods, constants, and types. Use this with OpenClaw, Claude, ChatGPT, Cursor, Windsurf, or any other AI tool that accepts text input. You can copy the full output to your clipboard or download it as a .txt file.
Extracted by GitExtract — free GitHub repo to text converter for AI. Built by Nikandr Surkov.