[
  {
    "path": ".github/FUNDING.yml",
    "content": "# These are supported funding model platforms\n\ngithub: [devcoons]\n\n"
  },
  {
    "path": ".github/workflows/build.yml",
    "content": "name: C/C++ CI\n\non:\n  push:\n    branches:    \n      # Push events on master branch\n      - master\n  pull_request:\n    branches:    \n      # Push events on master branch\n      - master\n\njobs:\n  build:\n    runs-on: ubuntu-latest\n    steps:\n    - uses: actions/checkout@v2\n    - name: make\n      run: make\n    - name: Get current date\n      id: date\n      run: echo \"::set-output name=date::$(date +'%Y-%m-%d')\"\n    - name: Create Release\n      id: create_release\n      uses: actions/create-release@v1\n      env:\n        GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}\n      with:\n        tag_name: ${{steps.date.outputs.date}}\n        release_name: ${{steps.date.outputs.date}} \n        draft: false\n        prerelease: false\n    - name: make clean\n      run: make clean\n"
  },
  {
    "path": "CMakeLists.txt",
    "content": "cmake_minimum_required(VERSION 3.10)\nproject(iso15765_canbus C)\n\nset(SRC_DIR src)\nset(LIB_DIR lib)\nset(EXM_DIR exm)\n\ninclude_directories(${SRC_DIR} ${LIB_DIR} ${EXM_DIR})\n\n# Add the dependency library (libiqueue)\nfile(GLOB LIB_FILES \"${LIB_DIR}/*.c\")\nadd_library(iqueue STATIC ${LIB_FILES})\n\n# Add the main library (libiso15765)\nfile(GLOB SRC_FILES \"${SRC_DIR}/*.c\")\nadd_library(iso15765 STATIC ${SRC_FILES})\ntarget_link_libraries(iso15765 PRIVATE iqueue)\n\n# Add the example executable\nfile(GLOB EXM_FILES \"${EXM_DIR}/*.c\")\nadd_executable(example ${EXM_FILES})\ntarget_link_libraries(example PRIVATE iso15765 iqueue)\n\nset_target_properties(iqueue iso15765 example PROPERTIES\n    ARCHIVE_OUTPUT_DIRECTORY \"${CMAKE_BINARY_DIR}/build\"\n    LIBRARY_OUTPUT_DIRECTORY \"${CMAKE_BINARY_DIR}/build\"\n    RUNTIME_OUTPUT_DIRECTORY \"${CMAKE_BINARY_DIR}/build\"\n)\n\nif(MSVC)\n    target_compile_options(iqueue PRIVATE /W4)\n    target_compile_options(iso15765 PRIVATE /W4)\n    target_compile_options(example PRIVATE /W4)\nelse()\n    target_compile_options(iqueue PRIVATE -Wall -Wextra)\n    target_compile_options(iso15765 PRIVATE -Wall -Wextra)\n    target_compile_options(example PRIVATE -Wall -Wextra)\nendif()\n"
  },
  {
    "path": "LICENSE",
    "content": "                    GNU AFFERO GENERAL PUBLIC LICENSE\n                       Version 3, 19 November 2007\n\n Copyright (C) 2007 Free Software Foundation, Inc. <https://fsf.org/>\n Everyone is permitted to copy and distribute verbatim copies\n of this license document, but changing it is not allowed.\n\n                            Preamble\n\n  The GNU Affero General Public License is a free, copyleft license for\nsoftware and other kinds of works, specifically designed to ensure\ncooperation with the community in the case of network server software.\n\n  The licenses for most software and other practical works are designed\nto take away your freedom to share and change the works.  By contrast,\nour General Public Licenses are intended to guarantee your freedom to\nshare and change all versions of a program--to make sure it remains free\nsoftware for all its users.\n\n  When we speak of free software, we are referring to freedom, not\nprice.  Our General Public Licenses are designed to make sure that you\nhave the freedom to distribute copies of free software (and charge for\nthem if you wish), that you receive source code or can get it if you\nwant it, that you can change the software or use pieces of it in new\nfree programs, and that you know you can do these things.\n\n  Developers that use our General Public Licenses protect your rights\nwith two steps: (1) assert copyright on the software, and (2) offer\nyou this License which gives you legal permission to copy, distribute\nand/or modify the software.\n\n  A secondary benefit of defending all users' freedom is that\nimprovements made in alternate versions of the program, if they\nreceive widespread use, become available for other developers to\nincorporate.  Many developers of free software are heartened and\nencouraged by the resulting cooperation.  However, in the case of\nsoftware used on network servers, this result may fail to come about.\nThe GNU General Public License permits making a modified version and\nletting the public access it on a server without ever releasing its\nsource code to the public.\n\n  The GNU Affero General Public License is designed specifically to\nensure that, in such cases, the modified source code becomes available\nto the community.  It requires the operator of a network server to\nprovide the source code of the modified version running there to the\nusers of that server.  Therefore, public use of a modified version, on\na publicly accessible server, gives the public access to the source\ncode of the modified version.\n\n  An older license, called the Affero General Public License and\npublished by Affero, was designed to accomplish similar goals.  This is\na different license, not a version of the Affero GPL, but Affero has\nreleased a new version of the Affero GPL which permits relicensing under\nthis license.\n\n  The precise terms and conditions for copying, distribution and\nmodification follow.\n\n                       TERMS AND CONDITIONS\n\n  0. Definitions.\n\n  \"This License\" refers to version 3 of the GNU Affero General Public License.\n\n  \"Copyright\" also means copyright-like laws that apply to other kinds of\nworks, such as semiconductor masks.\n\n  \"The Program\" refers to any copyrightable work licensed under this\nLicense.  Each licensee is addressed as \"you\".  \"Licensees\" and\n\"recipients\" may be individuals or organizations.\n\n  To \"modify\" a work means to copy from or adapt all or part of the work\nin a fashion requiring copyright permission, other than the making of an\nexact copy.  The resulting work is called a \"modified version\" of the\nearlier work or a work \"based on\" the earlier work.\n\n  A \"covered work\" means either the unmodified Program or a work based\non the Program.\n\n  To \"propagate\" a work means to do anything with it that, without\npermission, would make you directly or secondarily liable for\ninfringement under applicable copyright law, except executing it on a\ncomputer or modifying a private copy.  Propagation includes copying,\ndistribution (with or without modification), making available to the\npublic, and in some countries other activities as well.\n\n  To \"convey\" a work means any kind of propagation that enables other\nparties to make or receive copies.  Mere interaction with a user through\na computer network, with no transfer of a copy, is not conveying.\n\n  An interactive user interface displays \"Appropriate Legal Notices\"\nto the extent that it includes a convenient and prominently visible\nfeature that (1) displays an appropriate copyright notice, and (2)\ntells the user that there is no warranty for the work (except to the\nextent that warranties are provided), that licensees may convey the\nwork under this License, and how to view a copy of this License.  If\nthe interface presents a list of user commands or options, such as a\nmenu, a prominent item in the list meets this criterion.\n\n  1. Source Code.\n\n  The \"source code\" for a work means the preferred form of the work\nfor making modifications to it.  \"Object code\" means any non-source\nform of a work.\n\n  A \"Standard Interface\" means an interface that either is an official\nstandard defined by a recognized standards body, or, in the case of\ninterfaces specified for a particular programming language, one that\nis widely used among developers working in that language.\n\n  The \"System Libraries\" of an executable work include anything, other\nthan the work as a whole, that (a) is included in the normal form of\npackaging a Major Component, but which is not part of that Major\nComponent, and (b) serves only to enable use of the work with that\nMajor Component, or to implement a Standard Interface for which an\nimplementation is available to the public in source code form.  A\n\"Major Component\", in this context, means a major essential component\n(kernel, window system, and so on) of the specific operating system\n(if any) on which the executable work runs, or a compiler used to\nproduce the work, or an object code interpreter used to run it.\n\n  The \"Corresponding Source\" for a work in object code form means all\nthe source code needed to generate, install, and (for an executable\nwork) run the object code and to modify the work, including scripts to\ncontrol those activities.  However, it does not include the work's\nSystem Libraries, or general-purpose tools or generally available free\nprograms which are used unmodified in performing those activities but\nwhich are not part of the work.  For example, Corresponding Source\nincludes interface definition files associated with source files for\nthe work, and the source code for shared libraries and dynamically\nlinked subprograms that the work is specifically designed to require,\nsuch as by intimate data communication or control flow between those\nsubprograms and other parts of the work.\n\n  The Corresponding Source need not include anything that users\ncan regenerate automatically from other parts of the Corresponding\nSource.\n\n  The Corresponding Source for a work in source code form is that\nsame work.\n\n  2. Basic Permissions.\n\n  All rights granted under this License are granted for the term of\ncopyright on the Program, and are irrevocable provided the stated\nconditions are met.  This License explicitly affirms your unlimited\npermission to run the unmodified Program.  The output from running a\ncovered work is covered by this License only if the output, given its\ncontent, constitutes a covered work.  This License acknowledges your\nrights of fair use or other equivalent, as provided by copyright law.\n\n  You may make, run and propagate covered works that you do not\nconvey, without conditions so long as your license otherwise remains\nin force.  You may convey covered works to others for the sole purpose\nof having them make modifications exclusively for you, or provide you\nwith facilities for running those works, provided that you comply with\nthe terms of this License in conveying all material for which you do\nnot control copyright.  Those thus making or running the covered works\nfor you must do so exclusively on your behalf, under your direction\nand control, on terms that prohibit them from making any copies of\nyour copyrighted material outside their relationship with you.\n\n  Conveying under any other circumstances is permitted solely under\nthe conditions stated below.  Sublicensing is not allowed; section 10\nmakes it unnecessary.\n\n  3. Protecting Users' Legal Rights From Anti-Circumvention Law.\n\n  No covered work shall be deemed part of an effective technological\nmeasure under any applicable law fulfilling obligations under article\n11 of the WIPO copyright treaty adopted on 20 December 1996, or\nsimilar laws prohibiting or restricting circumvention of such\nmeasures.\n\n  When you convey a covered work, you waive any legal power to forbid\ncircumvention of technological measures to the extent such circumvention\nis effected by exercising rights under this License with respect to\nthe covered work, and you disclaim any intention to limit operation or\nmodification of the work as a means of enforcing, against the work's\nusers, your or third parties' legal rights to forbid circumvention of\ntechnological measures.\n\n  4. Conveying Verbatim Copies.\n\n  You may convey verbatim copies of the Program's source code as you\nreceive it, in any medium, provided that you conspicuously and\nappropriately publish on each copy an appropriate copyright notice;\nkeep intact all notices stating that this License and any\nnon-permissive terms added in accord with section 7 apply to the code;\nkeep intact all notices of the absence of any warranty; and give all\nrecipients a copy of this License along with the Program.\n\n  You may charge any price or no price for each copy that you convey,\nand you may offer support or warranty protection for a fee.\n\n  5. Conveying Modified Source Versions.\n\n  You may convey a work based on the Program, or the modifications to\nproduce it from the Program, in the form of source code under the\nterms of section 4, provided that you also meet all of these conditions:\n\n    a) The work must carry prominent notices stating that you modified\n    it, and giving a relevant date.\n\n    b) The work must carry prominent notices stating that it is\n    released under this License and any conditions added under section\n    7.  This requirement modifies the requirement in section 4 to\n    \"keep intact all notices\".\n\n    c) You must license the entire work, as a whole, under this\n    License to anyone who comes into possession of a copy.  This\n    License will therefore apply, along with any applicable section 7\n    additional terms, to the whole of the work, and all its parts,\n    regardless of how they are packaged.  This License gives no\n    permission to license the work in any other way, but it does not\n    invalidate such permission if you have separately received it.\n\n    d) If the work has interactive user interfaces, each must display\n    Appropriate Legal Notices; however, if the Program has interactive\n    interfaces that do not display Appropriate Legal Notices, your\n    work need not make them do so.\n\n  A compilation of a covered work with other separate and independent\nworks, which are not by their nature extensions of the covered work,\nand which are not combined with it such as to form a larger program,\nin or on a volume of a storage or distribution medium, is called an\n\"aggregate\" if the compilation and its resulting copyright are not\nused to limit the access or legal rights of the compilation's users\nbeyond what the individual works permit.  Inclusion of a covered work\nin an aggregate does not cause this License to apply to the other\nparts of the aggregate.\n\n  6. Conveying Non-Source Forms.\n\n  You may convey a covered work in object code form under the terms\nof sections 4 and 5, provided that you also convey the\nmachine-readable Corresponding Source under the terms of this License,\nin one of these ways:\n\n    a) Convey the object code in, or embodied in, a physical product\n    (including a physical distribution medium), accompanied by the\n    Corresponding Source fixed on a durable physical medium\n    customarily used for software interchange.\n\n    b) Convey the object code in, or embodied in, a physical product\n    (including a physical distribution medium), accompanied by a\n    written offer, valid for at least three years and valid for as\n    long as you offer spare parts or customer support for that product\n    model, to give anyone who possesses the object code either (1) a\n    copy of the Corresponding Source for all the software in the\n    product that is covered by this License, on a durable physical\n    medium customarily used for software interchange, for a price no\n    more than your reasonable cost of physically performing this\n    conveying of source, or (2) access to copy the\n    Corresponding Source from a network server at no charge.\n\n    c) Convey individual copies of the object code with a copy of the\n    written offer to provide the Corresponding Source.  This\n    alternative is allowed only occasionally and noncommercially, and\n    only if you received the object code with such an offer, in accord\n    with subsection 6b.\n\n    d) Convey the object code by offering access from a designated\n    place (gratis or for a charge), and offer equivalent access to the\n    Corresponding Source in the same way through the same place at no\n    further charge.  You need not require recipients to copy the\n    Corresponding Source along with the object code.  If the place to\n    copy the object code is a network server, the Corresponding Source\n    may be on a different server (operated by you or a third party)\n    that supports equivalent copying facilities, provided you maintain\n    clear directions next to the object code saying where to find the\n    Corresponding Source.  Regardless of what server hosts the\n    Corresponding Source, you remain obligated to ensure that it is\n    available for as long as needed to satisfy these requirements.\n\n    e) Convey the object code using peer-to-peer transmission, provided\n    you inform other peers where the object code and Corresponding\n    Source of the work are being offered to the general public at no\n    charge under subsection 6d.\n\n  A separable portion of the object code, whose source code is excluded\nfrom the Corresponding Source as a System Library, need not be\nincluded in conveying the object code work.\n\n  A \"User Product\" is either (1) a \"consumer product\", which means any\ntangible personal property which is normally used for personal, family,\nor household purposes, or (2) anything designed or sold for incorporation\ninto a dwelling.  In determining whether a product is a consumer product,\ndoubtful cases shall be resolved in favor of coverage.  For a particular\nproduct received by a particular user, \"normally used\" refers to a\ntypical or common use of that class of product, regardless of the status\nof the particular user or of the way in which the particular user\nactually uses, or expects or is expected to use, the product.  A product\nis a consumer product regardless of whether the product has substantial\ncommercial, industrial or non-consumer uses, unless such uses represent\nthe only significant mode of use of the product.\n\n  \"Installation Information\" for a User Product means any methods,\nprocedures, authorization keys, or other information required to install\nand execute modified versions of a covered work in that User Product from\na modified version of its Corresponding Source.  The information must\nsuffice to ensure that the continued functioning of the modified object\ncode is in no case prevented or interfered with solely because\nmodification has been made.\n\n  If you convey an object code work under this section in, or with, or\nspecifically for use in, a User Product, and the conveying occurs as\npart of a transaction in which the right of possession and use of the\nUser Product is transferred to the recipient in perpetuity or for a\nfixed term (regardless of how the transaction is characterized), the\nCorresponding Source conveyed under this section must be accompanied\nby the Installation Information.  But this requirement does not apply\nif neither you nor any third party retains the ability to install\nmodified object code on the User Product (for example, the work has\nbeen installed in ROM).\n\n  The requirement to provide Installation Information does not include a\nrequirement to continue to provide support service, warranty, or updates\nfor a work that has been modified or installed by the recipient, or for\nthe User Product in which it has been modified or installed.  Access to a\nnetwork may be denied when the modification itself materially and\nadversely affects the operation of the network or violates the rules and\nprotocols for communication across the network.\n\n  Corresponding Source conveyed, and Installation Information provided,\nin accord with this section must be in a format that is publicly\ndocumented (and with an implementation available to the public in\nsource code form), and must require no special password or key for\nunpacking, reading or copying.\n\n  7. Additional Terms.\n\n  \"Additional permissions\" are terms that supplement the terms of this\nLicense by making exceptions from one or more of its conditions.\nAdditional permissions that are applicable to the entire Program shall\nbe treated as though they were included in this License, to the extent\nthat they are valid under applicable law.  If additional permissions\napply only to part of the Program, that part may be used separately\nunder those permissions, but the entire Program remains governed by\nthis License without regard to the additional permissions.\n\n  When you convey a copy of a covered work, you may at your option\nremove any additional permissions from that copy, or from any part of\nit.  (Additional permissions may be written to require their own\nremoval in certain cases when you modify the work.)  You may place\nadditional permissions on material, added by you to a covered work,\nfor which you have or can give appropriate copyright permission.\n\n  Notwithstanding any other provision of this License, for material you\nadd to a covered work, you may (if authorized by the copyright holders of\nthat material) supplement the terms of this License with terms:\n\n    a) Disclaiming warranty or limiting liability differently from the\n    terms of sections 15 and 16 of this License; or\n\n    b) Requiring preservation of specified reasonable legal notices or\n    author attributions in that material or in the Appropriate Legal\n    Notices displayed by works containing it; or\n\n    c) Prohibiting misrepresentation of the origin of that material, or\n    requiring that modified versions of such material be marked in\n    reasonable ways as different from the original version; or\n\n    d) Limiting the use for publicity purposes of names of licensors or\n    authors of the material; or\n\n    e) Declining to grant rights under trademark law for use of some\n    trade names, trademarks, or service marks; or\n\n    f) Requiring indemnification of licensors and authors of that\n    material by anyone who conveys the material (or modified versions of\n    it) with contractual assumptions of liability to the recipient, for\n    any liability that these contractual assumptions directly impose on\n    those licensors and authors.\n\n  All other non-permissive additional terms are considered \"further\nrestrictions\" within the meaning of section 10.  If the Program as you\nreceived it, or any part of it, contains a notice stating that it is\ngoverned by this License along with a term that is a further\nrestriction, you may remove that term.  If a license document contains\na further restriction but permits relicensing or conveying under this\nLicense, you may add to a covered work material governed by the terms\nof that license document, provided that the further restriction does\nnot survive such relicensing or conveying.\n\n  If you add terms to a covered work in accord with this section, you\nmust place, in the relevant source files, a statement of the\nadditional terms that apply to those files, or a notice indicating\nwhere to find the applicable terms.\n\n  Additional terms, permissive or non-permissive, may be stated in the\nform of a separately written license, or stated as exceptions;\nthe above requirements apply either way.\n\n  8. Termination.\n\n  You may not propagate or modify a covered work except as expressly\nprovided under this License.  Any attempt otherwise to propagate or\nmodify it is void, and will automatically terminate your rights under\nthis License (including any patent licenses granted under the third\nparagraph of section 11).\n\n  However, if you cease all violation of this License, then your\nlicense from a particular copyright holder is reinstated (a)\nprovisionally, unless and until the copyright holder explicitly and\nfinally terminates your license, and (b) permanently, if the copyright\nholder fails to notify you of the violation by some reasonable means\nprior to 60 days after the cessation.\n\n  Moreover, your license from a particular copyright holder is\nreinstated permanently if the copyright holder notifies you of the\nviolation by some reasonable means, this is the first time you have\nreceived notice of violation of this License (for any work) from that\ncopyright holder, and you cure the violation prior to 30 days after\nyour receipt of the notice.\n\n  Termination of your rights under this section does not terminate the\nlicenses of parties who have received copies or rights from you under\nthis License.  If your rights have been terminated and not permanently\nreinstated, you do not qualify to receive new licenses for the same\nmaterial under section 10.\n\n  9. Acceptance Not Required for Having Copies.\n\n  You are not required to accept this License in order to receive or\nrun a copy of the Program.  Ancillary propagation of a covered work\noccurring solely as a consequence of using peer-to-peer transmission\nto receive a copy likewise does not require acceptance.  However,\nnothing other than this License grants you permission to propagate or\nmodify any covered work.  These actions infringe copyright if you do\nnot accept this License.  Therefore, by modifying or propagating a\ncovered work, you indicate your acceptance of this License to do so.\n\n  10. Automatic Licensing of Downstream Recipients.\n\n  Each time you convey a covered work, the recipient automatically\nreceives a license from the original licensors, to run, modify and\npropagate that work, subject to this License.  You are not responsible\nfor enforcing compliance by third parties with this License.\n\n  An \"entity transaction\" is a transaction transferring control of an\norganization, or substantially all assets of one, or subdividing an\norganization, or merging organizations.  If propagation of a covered\nwork results from an entity transaction, each party to that\ntransaction who receives a copy of the work also receives whatever\nlicenses to the work the party's predecessor in interest had or could\ngive under the previous paragraph, plus a right to possession of the\nCorresponding Source of the work from the predecessor in interest, if\nthe predecessor has it or can get it with reasonable efforts.\n\n  You may not impose any further restrictions on the exercise of the\nrights granted or affirmed under this License.  For example, you may\nnot impose a license fee, royalty, or other charge for exercise of\nrights granted under this License, and you may not initiate litigation\n(including a cross-claim or counterclaim in a lawsuit) alleging that\nany patent claim is infringed by making, using, selling, offering for\nsale, or importing the Program or any portion of it.\n\n  11. Patents.\n\n  A \"contributor\" is a copyright holder who authorizes use under this\nLicense of the Program or a work on which the Program is based.  The\nwork thus licensed is called the contributor's \"contributor version\".\n\n  A contributor's \"essential patent claims\" are all patent claims\nowned or controlled by the contributor, whether already acquired or\nhereafter acquired, that would be infringed by some manner, permitted\nby this License, of making, using, or selling its contributor version,\nbut do not include claims that would be infringed only as a\nconsequence of further modification of the contributor version.  For\npurposes of this definition, \"control\" includes the right to grant\npatent sublicenses in a manner consistent with the requirements of\nthis License.\n\n  Each contributor grants you a non-exclusive, worldwide, royalty-free\npatent license under the contributor's essential patent claims, to\nmake, use, sell, offer for sale, import and otherwise run, modify and\npropagate the contents of its contributor version.\n\n  In the following three paragraphs, a \"patent license\" is any express\nagreement or commitment, however denominated, not to enforce a patent\n(such as an express permission to practice a patent or covenant not to\nsue for patent infringement).  To \"grant\" such a patent license to a\nparty means to make such an agreement or commitment not to enforce a\npatent against the party.\n\n  If you convey a covered work, knowingly relying on a patent license,\nand the Corresponding Source of the work is not available for anyone\nto copy, free of charge and under the terms of this License, through a\npublicly available network server or other readily accessible means,\nthen you must either (1) cause the Corresponding Source to be so\navailable, or (2) arrange to deprive yourself of the benefit of the\npatent license for this particular work, or (3) arrange, in a manner\nconsistent with the requirements of this License, to extend the patent\nlicense to downstream recipients.  \"Knowingly relying\" means you have\nactual knowledge that, but for the patent license, your conveying the\ncovered work in a country, or your recipient's use of the covered work\nin a country, would infringe one or more identifiable patents in that\ncountry that you have reason to believe are valid.\n\n  If, pursuant to or in connection with a single transaction or\narrangement, you convey, or propagate by procuring conveyance of, a\ncovered work, and grant a patent license to some of the parties\nreceiving the covered work authorizing them to use, propagate, modify\nor convey a specific copy of the covered work, then the patent license\nyou grant is automatically extended to all recipients of the covered\nwork and works based on it.\n\n  A patent license is \"discriminatory\" if it does not include within\nthe scope of its coverage, prohibits the exercise of, or is\nconditioned on the non-exercise of one or more of the rights that are\nspecifically granted under this License.  You may not convey a covered\nwork if you are a party to an arrangement with a third party that is\nin the business of distributing software, under which you make payment\nto the third party based on the extent of your activity of conveying\nthe work, and under which the third party grants, to any of the\nparties who would receive the covered work from you, a discriminatory\npatent license (a) in connection with copies of the covered work\nconveyed by you (or copies made from those copies), or (b) primarily\nfor and in connection with specific products or compilations that\ncontain the covered work, unless you entered into that arrangement,\nor that patent license was granted, prior to 28 March 2007.\n\n  Nothing in this License shall be construed as excluding or limiting\nany implied license or other defenses to infringement that may\notherwise be available to you under applicable patent law.\n\n  12. No Surrender of Others' Freedom.\n\n  If conditions are imposed on you (whether by court order, agreement or\notherwise) that contradict the conditions of this License, they do not\nexcuse you from the conditions of this License.  If you cannot convey a\ncovered work so as to satisfy simultaneously your obligations under this\nLicense and any other pertinent obligations, then as a consequence you may\nnot convey it at all.  For example, if you agree to terms that obligate you\nto collect a royalty for further conveying from those to whom you convey\nthe Program, the only way you could satisfy both those terms and this\nLicense would be to refrain entirely from conveying the Program.\n\n  13. Remote Network Interaction; Use with the GNU General Public License.\n\n  Notwithstanding any other provision of this License, if you modify the\nProgram, your modified version must prominently offer all users\ninteracting with it remotely through a computer network (if your version\nsupports such interaction) an opportunity to receive the Corresponding\nSource of your version by providing access to the Corresponding Source\nfrom a network server at no charge, through some standard or customary\nmeans of facilitating copying of software.  This Corresponding Source\nshall include the Corresponding Source for any work covered by version 3\nof the GNU General Public License that is incorporated pursuant to the\nfollowing paragraph.\n\n  Notwithstanding any other provision of this License, you have\npermission to link or combine any covered work with a work licensed\nunder version 3 of the GNU General Public License into a single\ncombined work, and to convey the resulting work.  The terms of this\nLicense will continue to apply to the part which is the covered work,\nbut the work with which it is combined will remain governed by version\n3 of the GNU General Public License.\n\n  14. Revised Versions of this License.\n\n  The Free Software Foundation may publish revised and/or new versions of\nthe GNU Affero General Public License from time to time.  Such new versions\nwill be similar in spirit to the present version, but may differ in detail to\naddress new problems or concerns.\n\n  Each version is given a distinguishing version number.  If the\nProgram specifies that a certain numbered version of the GNU Affero General\nPublic License \"or any later version\" applies to it, you have the\noption of following the terms and conditions either of that numbered\nversion or of any later version published by the Free Software\nFoundation.  If the Program does not specify a version number of the\nGNU Affero General Public License, you may choose any version ever published\nby the Free Software Foundation.\n\n  If the Program specifies that a proxy can decide which future\nversions of the GNU Affero General Public License can be used, that proxy's\npublic statement of acceptance of a version permanently authorizes you\nto choose that version for the Program.\n\n  Later license versions may give you additional or different\npermissions.  However, no additional obligations are imposed on any\nauthor or copyright holder as a result of your choosing to follow a\nlater version.\n\n  15. Disclaimer of Warranty.\n\n  THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY\nAPPLICABLE LAW.  EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT\nHOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM \"AS IS\" WITHOUT WARRANTY\nOF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO,\nTHE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR\nPURPOSE.  THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM\nIS WITH YOU.  SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF\nALL NECESSARY SERVICING, REPAIR OR CORRECTION.\n\n  16. Limitation of Liability.\n\n  IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING\nWILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS\nTHE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY\nGENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE\nUSE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF\nDATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD\nPARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS),\nEVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF\nSUCH DAMAGES.\n\n  17. Interpretation of Sections 15 and 16.\n\n  If the disclaimer of warranty and limitation of liability provided\nabove cannot be given local legal effect according to their terms,\nreviewing courts shall apply local law that most closely approximates\nan absolute waiver of all civil liability in connection with the\nProgram, unless a warranty or assumption of liability accompanies a\ncopy of the Program in return for a fee.\n\n                     END OF TERMS AND CONDITIONS\n\n            How to Apply These Terms to Your New Programs\n\n  If you develop a new program, and you want it to be of the greatest\npossible use to the public, the best way to achieve this is to make it\nfree software which everyone can redistribute and change under these terms.\n\n  To do so, attach the following notices to the program.  It is safest\nto attach them to the start of each source file to most effectively\nstate the exclusion of warranty; and each file should have at least\nthe \"copyright\" line and a pointer to where the full notice is found.\n\n    <one line to give the program's name and a brief idea of what it does.>\n    Copyright (C) <year>  <name of author>\n\n    This program is free software: you can redistribute it and/or modify\n    it under the terms of the GNU Affero General Public License as published\n    by the Free Software Foundation, either version 3 of the License, or\n    (at your option) any later version.\n\n    This program is distributed in the hope that it will be useful,\n    but WITHOUT ANY WARRANTY; without even the implied warranty of\n    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n    GNU Affero General Public License for more details.\n\n    You should have received a copy of the GNU Affero General Public License\n    along with this program.  If not, see <https://www.gnu.org/licenses/>.\n\nAlso add information on how to contact you by electronic and paper mail.\n\n  If your software can interact with users remotely through a computer\nnetwork, you should also make sure that it provides a way for users to\nget its source.  For example, if your program is a web application, its\ninterface could display a \"Source\" link that leads users to an archive\nof the code.  There are many ways you could offer source, and different\nsolutions will be better for different programs; see section 13 for the\nspecific requirements.\n\n  You should also get your employer (if you work as a programmer) or school,\nif any, to sign a \"copyright disclaimer\" for the program, if necessary.\nFor more information on this, and how to apply and follow the GNU AGPL, see\n<https://www.gnu.org/licenses/>.\n"
  },
  {
    "path": "Makefile",
    "content": "CC = gcc\nCFLAGS = -Wall -Wextra -Ilib -Isrc -Iexm\n\nSRC_DIR = src\nLIB_DIR = lib\nEXM_DIR = exm\nBUILD_DIR = build\n\nLIBRARY = $(BUILD_DIR)/libiso15765.a\nLIB_DEP = $(BUILD_DIR)/libiqueue.a\nEXAMPLE = $(BUILD_DIR)/example\n\nSRC_FILES = $(wildcard $(SRC_DIR)/*.c)\nLIB_FILES = $(wildcard $(LIB_DIR)/*.c)\nEXM_FILES = $(wildcard $(EXM_DIR)/*.c)\n\nSRC_OBJS = $(patsubst $(SRC_DIR)/%.c, $(BUILD_DIR)/src_%.o, $(SRC_FILES))\nLIB_OBJS = $(patsubst $(LIB_DIR)/%.c, $(BUILD_DIR)/lib_%.o, $(LIB_FILES))\nEXM_OBJS = $(patsubst $(EXM_DIR)/%.c, $(BUILD_DIR)/exm_%.o, $(EXM_FILES))\n\n# Rules\nall: $(LIBRARY) $(EXAMPLE)\n\n$(BUILD_DIR):\n\tmkdir -p $(BUILD_DIR)\n\n# Compile dependency library\n$(LIB_DEP): $(BUILD_DIR) $(LIB_OBJS)\n\tar rcs $@ $(LIB_OBJS)\n\n$(BUILD_DIR)/lib_%.o: $(LIB_DIR)/%.c\n\t$(CC) $(CFLAGS) -c $< -o $@\n\n# Compile main library\n$(LIBRARY): $(LIB_DEP) $(SRC_OBJS)\n\tar rcs $@ $(SRC_OBJS)\n\n$(BUILD_DIR)/src_%.o: $(SRC_DIR)/%.c\n\t$(CC) $(CFLAGS) -c $< -o $@\n\n# Compile example\n$(EXAMPLE): $(LIBRARY) $(LIB_DEP) $(EXM_OBJS)\n\t$(CC) $(CFLAGS) $(EXM_OBJS) $(LIBRARY) $(LIB_DEP) -o $@\n\n$(BUILD_DIR)/exm_%.o: $(EXM_DIR)/%.c\n\t$(CC) $(CFLAGS) -c $< -o $@\n\nclean:\n\trm -rf $(BUILD_DIR)\n\nrebuild: clean all\n\n.PHONY: all clean\n"
  },
  {
    "path": "README.md",
    "content": "# ISO15765-2 CANBus TP \n![C/C++ CI](https://github.com/devcoons/iso15765-canbus/workflows/C/C++%20CI/badge.svg)  [![Codacy Badge](https://app.codacy.com/project/badge/Grade/5a80fc004df744e888729e512eec1fda)](https://app.codacy.com/gh/devcoons/iso15765-canbus/dashboard?utm_source=gh&utm_medium=referral&utm_content=&utm_campaign=Badge_grade)\n\n*Compiler flags: **-O3 -Wfatal-errors -Wall -std=c11***\n\nAn 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.\n\n>This ISO defines common requirements for vehicle diagnostic systems implemented on a Controller Area Network (CAN) communication link, as specified in ISO 11898.\n>Although primarily intended for diagnostic systems, it also meets requirements from other CAN-based systems needing a network layer protocol.\n\n## How to use\n\n-  Include the header file `iso15765_2.h`\n-  Define an `iso15765_t` handler, create and attach the required shim/callbacks (`send_frame` `on_error` `get_ms`) of the handler\n```C\n/* Required shim/callback functions */\nstatic uint8_t send_frame(cbus_id_type id_type, uint32_t id, cbus_fr_format fr_fmt, uint8_t dlc, uint8_t* dt);\nstatic void usdata_indication(n_indn_t* info);\nstatic void on_error(n_rslt err_type);\nstatic uint32_t getms();\n\n/* ISOTP handler */\nstatic iso15765_t handler =\n{\n\t.addr_md = N_ADM_FIXED,\t\t     // Selected address mode of the TP\n\t.fr_id_type = CBUS_ID_T_EXTENDED,    // CANBus frame type\n\t.config.stmin = 0x05,\t\t     // Default min. frame transmission separation\n\t.config.bs = 0x0F,\t\t     // Maximun size of the block sequence\n\t.config.n_bs = 800,\t\t     // Time until reception of the next FlowControl N_PDU\n \t.config.n_cr = 250,\t\t     // Time until reception of the next ConsecutiveFrame N_PDU\n\t.clbs.get_ms = getms,\t\t     // Time-source for the library in ms(required)\n\t.clbs.on_error = on_error,\t     // Callback which will be executed in any occured error.\n\t.clbs.send_frame = send_frame,\t     // This callback will be fired when a transmission of a canbus frame is ready.\n\t.clbs.indn = usdata_indication\t     // Indication Callback: Will be fired when a reception\n\t\t\t\t\t     // is available or an error occured during the reception.\n};\n\nstatic uint8_t send_frame(cbus_id_type id_type, uint32_t id, cbus_fr_format fr_fmt, uint8_t dlc, uint8_t* dt)\n{\n    // Here transmit the frame through CANBus\n    return 0;\n}\n\nstatic void usdata_indication(n_indn_t* info)\n{\n    // Use the incoming message. This function should be fired by the library when a new complete message arrives.\n}\n\nstatic void on_error(n_rslt err_type)\n{\n    // Check the errors etc..\n}\n\nstatic uint32_t getms()\n{\n    // return time in ms; \n    // ex. return GetTickCount();\n}\n```\n\n-  In your main, first initialize the iso15765_t handler `iso15765_init(&handler);`\n\n-  To send a message, create a `n_req_t` and use the function `iso15765_send`. For example:\n\n```C\nn_req_t frame =\n{\n    .n_ai.n_pr = 0x06,\t\t// Network Address Priority\n    .n_ai.n_sa = 0x01,\t\t// Network Source Address\n    .n_ai.n_ta = 0x02,\t\t// Network Target Address\n    .n_ai.n_ae = 0x00,\t\t// Network Address Extension\n    .n_ai.n_tt = N_TA_T_PHY,\t// Network Target Address type\n    .fr_fmt = CBUS_FR_FRM_STD,\t// CANFD or CANSTD\n    .msg = {1,2,3,4,5,6,7,8,\t// Message\n    \t    9,10,11,12,13,14,\n\t    15,16,17,18,19,20},\n    .msg_sz = 20,\t\t// Message size\n};\n...\niso15765_send(&handler, &frame);\n```\n-  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\n-  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.\n-  As described before, any new/completed incoming message should be handled in the callback `static void usdata_indication(indn_t* info)`\n\nBelow is a **complete loopback example**. The service send a message to itself by enqueing the transmitted frame in the inbound stream.\n\n```C\n#include <stdio.h>\n#include <stdlib.h>\n#include \"iso15765_2.h\"\n#include <windows.h>\n#include <sysinfoapi.h>\n\n/******************************************************************************\n* Declaration | Static Functions\n******************************************************************************/\n\nstatic uint8_t send_frame(canbus_md md, uint32_t id, uint8_t dlc, uint8_t* dt);\nstatic void on_error(n_rslt err_type);\nstatic uint32_t getms();\n\n/******************************************************************************\n* Enumerations, structures & Variables\n******************************************************************************/\n\nstatic iso15765_t handler =\n{\n        .addr_md = N_ADM_FIXED,\n        .fr_id_type = CBUS_ID_T_EXTENDED,\n        .clbs.send_frame = send_frame1,\n        .clbs.on_error = on_error,\n        .clbs.get_ms = getms,\n        .clbs.indn = indn1,\n        .config.stmin = 0x3,\n        .config.bs = 0x0f,\n        .config.n_bs = 100,\n        .config.n_cr = 3\n};\n\nn_req_t frame =\n{\n        .n_ai.n_pr = 0x07,\n        .n_ai.n_sa = 0x01,\n        .n_ai.n_ta = 0x02,\n        .n_ai.n_ae = 0x00,\n        .n_ai.n_tt = N_TA_T_PHY,\n        .fr_fmt = CBUS_FR_FRM_FD,\n        .msg = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23},\n        .msg_sz = 23,\n};\n\n/******************************************************************************\n* Definition  | Static Functions\n******************************************************************************/\n\nstatic uint32_t getms()\n{\n        return GetTickCount();\n}\n\nstatic uint8_t send_frame(cbus_id_type id_type, uint32_t id, cbus_fr_format fr_fmt, uint8_t dlc, uint8_t* dt)\n{\n        printf(\"%d  #1# - id:%x    dlc:%02x    \",GetTickCount(), id, dlc);\n        for (int i = 0; i < 8; i++) printf(\"%02x \", dt[i]);\n        printf(\"\\n\");\n        canbus_frame_t frame = { .id = id, .dlc = dlc, .id_type = id_type, .fr_format= fr_fmt };\n        memmove(frame.dt, dt, dlc);\n        iso15765_enqueue(&handler, &frame);\n        return 0;\n}\n\nstatic void on_error(n_rslt err_type)\n{\n        printf(\"ERROR OCCURED!:%d\", err_type);\n}\n\n/******************************************************************************\n* Definition  | Public Functions\n******************************************************************************/\n\nint main()\n{\n        iso15765_init(&handler);\n\n        iso15765_send(&handler, &frame);\n        while(1)\n        {\n                iso15765_process(&handler);\n                Sleep(5);\n        }\n        return 0;\n}\n```\n\nPlease check the folder **`exm`** for more examples\n\n## Development\n\nThis 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.\n\n## Contributing\n\nWe would love you to contribute to `iso15765-canbus`, pull requests are welcome!\n\n## Support\n\nSupport this project though https://paypal.me/iikem or https://github.com/sponsors/devcoons\n\n## Licensing Information\n\nThis project is licensed under the GNU Affero General Public License v3.0 (AGPL-3.0).  \nYou can find the full license text in the [LICENSE](./LICENSE) file.\n\n### Commercial Licensing\n\nFor commercial use, including proprietary or for-profit applications, you must obtain a separate license.  \nContact me for commercial licensing at:  \n- GitHub: [https://github.com/devcoons](https://github.com/devcoons)  \n- Email: i_-_-_s@outlook.com\n"
  },
  {
    "path": "doc/.gitkeep",
    "content": ""
  },
  {
    "path": "exm/ISO15765Development.c",
    "content": "#include <stdio.h>\n#include <stdlib.h>\n#include <string.h>\n#include \"lib_iso15765.h\"\n\n#ifdef _WIN32\n    #include <windows.h>\n    #include <sysinfoapi.h>\n    #include <conio.h>\n#else\n    #include <unistd.h>\n    #include <sys/time.h>\n#endif\n\n/******************************************************************************\n* Declaration | Static Functions\n******************************************************************************/\n\nstatic uint8_t send_frame1(cbus_id_type id_type, uint32_t id, cbus_fr_format fr_fmt, uint8_t dlc, uint8_t* dt);\nstatic uint8_t send_frame2(cbus_id_type id_type, uint32_t id, cbus_fr_format fr_fmt, uint8_t dlc, uint8_t* dt);\nstatic void indn1(n_indn_t* info);\nstatic void indn2(n_indn_t* info);\nstatic void on_error(n_rslt err_type);\nstatic uint32_t getms();\n\n/******************************************************************************\n* Enumerations, structures & Variables\n******************************************************************************/\n\nstatic iso15765_t handler1 = {\n    .addr_md = N_ADM_FIXED,\n    .fr_id_type = CBUS_ID_T_EXTENDED,\n    .clbs.send_frame = send_frame1,\n    .clbs.on_error = on_error,\n    .clbs.get_ms = getms,\n    .clbs.indn = indn1,\n    .config.stmin = 0x3,\n    .config.bs = 0x2a,\n    .config.n_bs = 100,\n    .config.n_cr = 3\n};\n\nstatic iso15765_t handler2 = {\n    .addr_md = N_ADM_FIXED,\n    .fr_id_type = CBUS_ID_T_EXTENDED,\n    .clbs.send_frame = send_frame2,\n    .clbs.on_error = on_error,\n    .clbs.get_ms = getms,\n    .clbs.indn = indn2,\n    .config.stmin = 0x3,\n    .config.bs = 0x0f,\n    .config.n_bs = 100,\n    .config.n_cr = 3\n};\n\nn_req_t frame1 = {\n    .n_ai.n_pr = 0x07,\n    .n_ai.n_sa = 0x01,\n    .n_ai.n_ta = 0x02,\n    .n_ai.n_ae = 0x00,\n    .n_ai.n_tt = N_TA_T_PHY,\n    .fr_fmt = CBUS_FR_FRM_STD,\n    .msg = {0},\n    .msg_sz = 0,\n};\n\nn_req_t frame2 = {\n    .n_ai.n_pr = 0x07,\n    .n_ai.n_sa = 0x02,\n    .n_ai.n_ta = 0x01,\n    .n_ai.n_ae = 0x00,\n    .n_ai.n_tt = N_TA_T_PHY,\n    .fr_fmt = CBUS_FR_FRM_STD,\n    .msg = {0},\n    .msg_sz = 0,\n};\n\n/******************************************************************************\n* Definition  | Static Functions\n******************************************************************************/\nuint16_t f_sz1 = 1;\nuint16_t f_sz2 = 1;\n\nstatic void rand_string(uint8_t* str, size_t size) {\n    const uint8_t charset[] = \"abcdefghijklmnopqrstuvwxyzABCDEFGHIJK\";\n    for (size_t n = 0; n < size; n++) {\n        int key = rand() % (sizeof(charset) - 1);\n        str[n] = charset[key];\n    }\n}\n\nstatic uint32_t getms() {\n#ifdef _WIN32\n    return GetTickCount();\n#else\n    struct timeval tv;\n    gettimeofday(&tv, NULL);\n    return (uint32_t)(tv.tv_sec * 1000 + tv.tv_usec / 1000);\n#endif\n}\n\nstatic 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) {\n    printf(\" (%d) - %d | TpMode: [%02x] | FrameType: [%2d] IdType: [%d] Id: [%8x] DLC: [%02d]\\t\\t\", instance, getms(), tp_mode, mode, ctp_ft, id, dlc);\n\n    for (int s = 0; s < dlc; s += 8) {\n        int elements = dlc - s > 8 ? 8 : dlc - s;\n        for (int i = s; i < s + 8; i++) {\n            printf((i < s + elements) ? \"%02x \" : \"   \", dt[i]);\n        }\n        printf(\"\\t\");\n        if (s + elements == dlc) {\n            printf(\"\\n\");\n        } else {\n            printf(\"\\n     |\\t\\t\");\n        }\n    }\n}\n\nstatic uint8_t send_frame1(cbus_id_type id_type, uint32_t id, cbus_fr_format fr_fmt, uint8_t dlc, uint8_t* dt) {\n    print_frame(1, handler2.addr_md, id_type, id, fr_fmt, dlc, dt);\n    canbus_frame_t frame = { .id = id, .dlc = dlc, .id_type = id_type, .fr_format= fr_fmt };\n    memmove(frame.dt, dt, dlc);\n    iso15765_enqueue(&handler2, &frame);\n    return 0;\n}\n\nstatic uint8_t send_frame2(cbus_id_type id_type, uint32_t id, cbus_fr_format fr_fmt, uint8_t dlc, uint8_t* dt) {     \n    print_frame(2, handler1.addr_md, id_type, id, fr_fmt, dlc, dt);\n    canbus_frame_t frame = { .id = id, .dlc = dlc, .id_type = id_type, .fr_format = fr_fmt };\n    memmove(frame.dt, dt, dlc);\n    iso15765_enqueue(&handler1, &frame);\n    return 0;\n}\n\nstatic void on_error(n_rslt err_type) {\n    printf(\"ERROR OCCURRED!: %04x\\n\", err_type);\n}\n\nstatic void indn1(n_indn_t* info) {\n    uint8_t v = (memcmp(info->msg, frame2.msg, info->msg_sz) == 0) ? 'V' : 'X';\n    uint8_t s = (info->msg_sz == frame2.msg_sz) ? 'V' : 'X';\n\n    printf(\"- Reception of H1. Msg_sz:[%d] | SZ_CH[%c] MSG_CH[%c]\\n\", info->msg_sz, s, v);\n\n    if (v != 'V' || s != 'V') {\n        printf(\"--------- ERROR -----------\\n\");\n#ifdef _WIN32\n        Sleep(1000);\n#else\n        usleep(1000 * 1000); // Sleep for 1 second\n#endif\n    }\n    printf(\"- END OF TRANSMISSION/RECEPTION -\\n\\n\\n\");\n\n    frame1.msg_sz = f_sz1;\n    rand_string(frame1.msg, frame1.msg_sz);\n    f_sz1 = (f_sz1 == 512) ? 0 : f_sz1 + 1;\n    iso15765_send(&handler1, &frame1);\n}\n\nstatic void indn2(n_indn_t* info) {\n    uint8_t v = (memcmp(info->msg, frame1.msg, info->msg_sz) == 0) ? 'V' : 'X';\n    uint8_t s = (info->msg_sz == frame1.msg_sz) ? 'V' : 'X';\n\n    printf(\"- Reception of H2. Msg_sz:[%d] | SZ_CH[%c] MSG_CH[%c]\\n\", info->msg_sz, s, v);\n\n    if (v != 'V' || s != 'V') {\n        printf(\"--------- ERROR -----------\\n\");\n#ifdef _WIN32\n        Sleep(1000);\n#else\n        usleep(1000 * 1000); // Sleep for 1 second\n#endif\n    }\n    printf(\"- END OF TRANSMISSION/RECEPTION -\\n\\n\\n\");\n\n    frame2.msg_sz = f_sz2;\n    rand_string(frame2.msg, frame2.msg_sz);\n    f_sz2 = (f_sz2 == 512) ? 0 : f_sz2 + 1;\n    iso15765_send(&handler2, &frame2);\n}\n\n/******************************************************************************\n* Definition  | Public Functions\n******************************************************************************/\n\nint main() {\n    iso15765_init(&handler1);\n    iso15765_init(&handler2);\n\n    frame1.msg_sz = f_sz1;\n    rand_string(frame1.msg, frame1.msg_sz);\n    f_sz1++;\n\n    iso15765_send(&handler1, &frame1);\n    while (1) {\n        iso15765_process(&handler1);\n        iso15765_process(&handler2);\n    }\n    return 0;\n}\n\n"
  },
  {
    "path": "lib/lib_iqueue.c",
    "content": "/*!\n@file   lib_iqueue.c\n@brief  <brief description here>\n@t.odo\t-\n---------------------------------------------------------------------------\n\nGNU Affero General Public License v3.0  \n\nCopyright (c) 2024 Ioannis D. (devcoons)  \n\nThis program is free software: you can redistribute it and/or modify it \nunder the terms of the GNU Affero General Public License as published by \nthe Free Software Foundation, either version 3 of the License.  \n\nThis program is distributed in the hope that it will be useful,  \nbut WITHOUT ANY WARRANTY; without even the implied warranty of  \nMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the  \nGNU Affero General Public License for more details.  \n\nYou should have received a copy of the GNU Affero General Public License  \nalong with this program. If not, see <https://www.gnu.org/licenses/>.  \n\nFor commercial use, including proprietary or for-profit applications, \na separate license is required. Contact:  \n\n- GitHub: [https://github.com/devcoons](https://github.com/devcoons)  \n- Email: i_-_-_s@outlook.com \n*/\n/******************************************************************************\n* Preprocessor Definitions & Macros\n******************************************************************************/\n\n/******************************************************************************\n* Includes\n******************************************************************************/\n\n#include \"lib_iqueue.h\"\n\n/******************************************************************************\n* Enumerations, structures & Variables\n******************************************************************************/\n\n/******************************************************************************\n* Declaration | Static Functions\n******************************************************************************/\n\n/******************************************************************************\n* Definition  | Static Functions\n******************************************************************************/\n\n\n/******************************************************************************\n* Definition  | Public Functions\n******************************************************************************/\n\ni_status iqueue_init(iqueue_t* _queue, uint32_t _max_elements, size_t _element_size, void* _storage)\n{\n    if ((_queue != NULL) && (_storage != NULL))\n    {\n        (void)memset(_storage, 0, _element_size * _max_elements);\n\t\t_queue->element_size = _element_size;\n\t\t_queue->max_elements = _max_elements;\n\t\t_queue->first = (uintptr_t)NULL;\n\t\t_queue->next = (uintptr_t)_storage;\n\t\t_queue->storage = _storage;\n        return I_OK;\n    }\n    else\n    {\n        return I_ERROR;\n    }\n}\n\nvoid* iqueue_get_next_enqueue(iqueue_t* _queue)\n{\n\treturn (_queue != NULL) ? (void *)(_queue->next) : NULL;\n}\n\ni_status iqueue_advance_next(iqueue_t* _queue)\n{\n\tif (_queue->first != _queue->next)\n\t{\n\t\tuintptr_t storage_end = (uintptr_t)(_queue->storage) + (_queue->element_size * _queue->max_elements);\n\t\t_queue->first = (_queue->first == 0U) ? _queue->next : _queue->first;\n\t\t_queue->next = ((_queue->next + _queue->element_size) == storage_end) ? (uintptr_t)_queue->storage : (_queue->next + _queue->element_size);\n\t\treturn I_OK;\n\t}\n\telse\n\t{\n\t\treturn I_FULL;\n\t}\n}\n\ni_status iqueue_enqueue(iqueue_t* _queue, void* _element)\n{\n\tif (_queue->first != _queue->next)\n\t{\n\t\t(void)memmove((void*)_queue->next, (void*)_element, _queue->element_size);\n\t\treturn iqueue_advance_next(_queue);\n\t}\n\treturn I_FULL;\n}\n\ni_status iqueue_dequeue(iqueue_t* _queue, void* _element)\n{\n\tvoid* x = iqueue_dequeue_fast(_queue);\n\tif (x != NULL)\n\t{\n\t\t(void)memmove((void*)_element, x, _queue->element_size);\n\t\treturn I_OK;\n\t}\n\telse\n\t{\n\t\treturn I_EMPTY;\n\t}\n}\n\nvoid* iqueue_dequeue_fast(iqueue_t* _queue)\n{\n\tif (_queue == NULL) {\n\t\treturn NULL;\n\t}\n\n\tif (_queue->first != 0U) {\n\t\tvoid* ret_ptr = (void*)_queue->first;\n\n\t\tuintptr_t storage_end = (uintptr_t)_queue->storage + (_queue->element_size * _queue->max_elements);\n\t\tuintptr_t next_first = _queue->first + _queue->element_size;\n\n\t\t_queue->first = (next_first == storage_end) ? (uintptr_t)_queue->storage : next_first;\n\t\t_queue->first = (_queue->first == _queue->next) ? 0U : _queue->first;\n\n\t\treturn ret_ptr;\n\t}\n\n\treturn NULL;\n}\n\n\ni_status iqueue_size(iqueue_t* _queue, size_t* _size)\n{\n\t*_size = _queue->first == 0\n\t\t? 0\n\t\t: _queue->first < _queue->next\n\t\t? (_queue->next - _queue->first) / _queue->element_size\n\t\t: _queue->max_elements - ((_queue->first - _queue->next) / _queue->element_size);\n\n\treturn I_OK;\n}\n\n/******************************************************************************\n* EOF - NO CODE AFTER THIS LINE\n******************************************************************************/"
  },
  {
    "path": "lib/lib_iqueue.h",
    "content": "#pragma once\n/*!\n\t@file   lib_iqueue.h\n\t@brief  <brief description here>\n\t@t.odo\t-\n\t---------------------------------------------------------------------------\n\n\tMIT License\n\tCopyright (c) 2018 Io. D (Devcoons)\n\n\tPermission is hereby granted, free of charge, to any person obtaining a copy\n\tof this software and associated documentation files (the \"Software\"), to deal\n\tin the Software without restriction, including without limitation the rights\n\tto use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n\tcopies of the Software, and to permit persons to whom the Software is\n\tfurnished to do so, subject to the following conditions:\n\n\tThe above copyright notice and this permission notice shall be included in all\n\tcopies or substantial portions of the Software.\n\n\tTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n\tIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n\tFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n\tAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n\tLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n\tOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\n\tSOFTWARE.\n*/\n/******************************************************************************\n* Preprocessor Definitions & Macros\n******************************************************************************/\n\n#ifndef LIBRARIES_INC_LIB_IQUEUE_H_\n#define LIBRARIES_INC_LIB_IQUEUE_H_\n\n/******************************************************************************\n* Includes\n******************************************************************************/\n\n#include <inttypes.h>\n#include <string.h>\n\n/******************************************************************************\n* Enumerations, structures & Variables\n******************************************************************************/\n\n#if !defined(ENUM_I_STATUS)\n#define ENUM_I_STATUS\ntypedef enum\n{\n\tI_OK = 0x00,\n\tI_INVALID = 0x01,\n\tI_EXISTS = 0x02,\n\tI_NOTEXISTS = 0x03,\n\tI_FAILED = 0x04,\n\tI_EXPIRED = 0x05,\n\tI_UNKNOWN = 0x06,\n\tI_INPROGRESS = 0x07,\n\tI_IDLE = 0x08,\n\tI_FULL = 0x09,\n\tI_EMPTY = 0x0A,\n\tI_YES = 0x0B,\n\tI_NO = 0x0C,\n\tI_SKIP = 0x0D,\n\tI_DEBUG_01 = 0xE0,\n\tI_DEBUG_02 = 0xE1,\n\tI_DEBUG_03 = 0xE2,\n\tI_DEBUG_04 = 0xE3,\n\tI_DEBUG_05 = 0xE4,\n\tI_DEBUG_06 = 0xE5,\n\tI_DEBUG_07 = 0xE6,\n\tI_DEBUG_08 = 0xE7,\n\tI_DEBUG_09 = 0xE8,\n\tI_DEBUG_10 = 0xE9,\n\tI_DEBUG_11 = 0xEA,\n\tI_DEBUG_12 = 0xEB,\n\tI_DEBUG_13 = 0xEC,\n\tI_DEBUG_14 = 0xED,\n\tI_DEBUG_15 = 0xEE,\n\tI_DEBUG_16 = 0xEF,\n\tI_MEMUNALIGNED = 0xFD,\n\tI_NOTIMPLEMENTED = 0xFE,\n\tI_ERROR = 0xFF\n}i_status;\n#endif\n\n\ntypedef struct\n{\n\tvoid* storage;\n\tuintptr_t first;\n\tuintptr_t next;\n\tsize_t element_size;\n\tuint32_t max_elements;\n}\niqueue_t;\n\n/******************************************************************************\n* Declaration | Public Functions\n******************************************************************************/\n\ni_status iqueue_init(iqueue_t* _queue, uint32_t _max_elements, size_t _element_size, void* _storage);\ni_status iqueue_enqueue(iqueue_t* _queue, void* _element);\ni_status iqueue_dequeue(iqueue_t* _queue, void* _element);\ni_status iqueue_size(iqueue_t* _queue, size_t* _size);\ni_status iqueue_advance_next(iqueue_t* _queue);\nvoid* iqueue_get_next_enqueue(iqueue_t* _queue);\nvoid* iqueue_dequeue_fast(iqueue_t* _queue);\n\n/******************************************************************************\n* EOF - NO CODE AFTER THIS LINE\n******************************************************************************/\n#endif\n"
  },
  {
    "path": "src/lib_iso15765.c",
    "content": "/*!\n@file   iso15765_2.c\n@brief  Source file of the ISO15765-2 library\n@t.odo\t-\n---------------------------------------------------------------------------\n\nGNU Affero General Public License v3.0  \n\nCopyright (c) 2024 Ioannis D. (devcoons)  \n\nThis program is free software: you can redistribute it and/or modify it \nunder the terms of the GNU Affero General Public License as published by \nthe Free Software Foundation, either version 3 of the License.  \n\nThis program is distributed in the hope that it will be useful,  \nbut WITHOUT ANY WARRANTY; without even the implied warranty of  \nMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the  \nGNU Affero General Public License for more details.  \n\nYou should have received a copy of the GNU Affero General Public License  \nalong with this program. If not, see <https://www.gnu.org/licenses/>.  \n\nFor commercial use, including proprietary or for-profit applications, \na separate license is required. Contact:  \n\n- GitHub: [https://github.com/devcoons](https://github.com/devcoons)  \n- Email: i_-_-_s@outlook.com \n\n*/\n/******************************************************************************\n* Preprocessor Definitions & Macros\n******************************************************************************/\n\n/******************************************************************************\n* Includes\n******************************************************************************/\n\n#include \"lib_iso15765.h\"\n\n/******************************************************************************\n* Enumerations, structures & Variables\n******************************************************************************/\n\n/* Structs used by the service to inform the user(upper layer) of an event */\nstatic n_indn_t sgn_indn;\nstatic n_ff_indn_t sgn_ff_indn;\nstatic n_cfm_t sgn_conf;\nstatic n_chg_param_cfm_t sgn_chg_cfm;\n\n/******************************************************************************\n* Declaration | Static Functions\n******************************************************************************/\n\n/******************************************************************************\n* Definition  | Static Functions\n******************************************************************************/\n\n/*\n * Default empty callback functions. The user can assing his own during the init\n * of the service.\n */\nstatic void indn(n_indn_t* info)\n{\n\tISO_15675_UNUSED(info);\n}\n\nstatic void ff_indn(n_ff_indn_t* info)\n{\n\tISO_15675_UNUSED(info);\n}\n\nstatic void cfm(n_cfm_t* info)\n{\n\tISO_15675_UNUSED(info);\n}\n\nstatic void cfg_cfm(n_chg_param_cfm_t* info)\n{\n\tISO_15675_UNUSED(info);\n}\n\n/*\n * Helper function to check if time interval has passed from a given time.\n */\ninline static n_rslt has_interval_passed(uint32_t current_time, uint32_t last_time, uint32_t interval)\n{\n    if ((interval == 0U) || (interval >= UINT32_MAX))\n    {\n        return N_ERROR;\n    }\n\n    uint32_t elapsed_time = current_time - last_time;\n\treturn (elapsed_time >= interval) ? N_OK : N_INV;\n}\n\n/*\n * Helper function to find the closest can_dl\n */\ninline static uint8_t n_get_closest_can_dl(uint8_t size, cbus_fr_format tmt)\n{\n\tuint8_t rval = 0;\n\n\tif (tmt == CBUS_FR_FRM_STD)\n\t{\n\t\trval = (size <= 0x08U) ? size : 0x08U;\n\t}\n\telse\n\t{\n\t\tif (size <= 8)\n\t\t{\n\t\t\trval = size;\n\t\t}\n\t\telse if (size <= 12)\n\t\t{\n\t\t\trval = 12;\n\t\t}\n\t\telse if (size <= 16)\n\t\t{\n\t\t\trval = 16;\n\t\t}\n\t\telse if (size <= 20)\n\t\t{\n\t\t\trval = 20;\n\t\t}\n\t\telse if (size <= 24)\n\t\t{\n\t\t\trval = 24;\n\t\t}\n\t\telse if (size <= 32)\n\t\t{\n\t\t\trval = 32;\n\t\t}\n\t\telse if (size <= 48)\n\t\t{\n\t\t\trval = 48;\n\t\t}\n\t\telse\n\t\t{\n\t\t\trval = 64;\n\t\t}\n\t}\n\n\treturn rval;\n}\n\n/*\n * Helper function to find the closest can_dl\n */\ninline static uint8_t n_get_dt_offset(addr_md address, pci_type pci, uint16_t data_size)\n{\n\tif (address\t!= N_ADM_NORMAL && address != N_ADM_FIXED \n\t&& address != N_ADM_MIXED11 && address != N_ADM_EXTENDED \n\t&& address != N_ADM_MIXED29)\n\t{\n\t\treturn 0xFF;\n\t}\n\n\tuint8_t offset = (address & 0x01);\n\n\tswitch (pci)\n\t{\n\tcase N_PCI_T_SF:\n\t\toffset += 1;\n\t\toffset += data_size <= (8 - offset) ? 0 : 1;\n\t\tbreak;\n\tcase N_PCI_T_FF:\n\t\toffset += 2;\n\t\toffset += data_size > 4095 ? 4 : 0;\n\t\tbreak;\n\tcase N_PCI_T_CF:\n\t\toffset += 1;\n\t\tbreak;\n\tcase N_PCI_T_FC:\n\t\toffset += 3;\n\t\tbreak;\n\tdefault:\n\t\toffset = 0xFF;\n\t\tbreak;\n\t}\n\treturn offset;\n}\n\n/*\n * Helper function to find which PCI_Type the outbound stream has to use\n */\ninline static pci_type n_out_frame_type(iso15765_t* instance)\n{\n\tpci_type result = N_PCI_T_CF;\n\n\tif (instance->out.cf_cnt == 0)\n\t{\n\t\tif ((instance->addr_md & 0x01) == 1)\n\t\t{\n\t\t\tif (instance->out.fr_fmt == CBUS_FR_FRM_STD)\n\t\t\t{\n\t\t\t\tresult = instance->out.msg_sz <= 6 ? N_PCI_T_SF : N_PCI_T_FF;\n\t\t\t}\n\t\t\telse\n\t\t\t{\n\t\t\t\tresult = instance->out.msg_sz <= 61 ? N_PCI_T_SF : N_PCI_T_FF;\n\t\t\t}\n\t\t}\n\t\telse\n\t\t{\n\t\t\tif (instance->out.fr_fmt == CBUS_FR_FRM_STD)\n\t\t\t{\n\t\t\t\tresult = instance->out.msg_sz <= 7 ? N_PCI_T_SF : N_PCI_T_FF;\n\t\t\t}\n\t\t\telse\n\t\t\t{\n\t\t\t\tresult = instance->out.msg_sz <= 62 ? N_PCI_T_SF : N_PCI_T_FF;\n\t\t\t}\n\t\t}\n\t}\n\treturn result;\n}\n\n/*\n * Convert partially the CANBus Frame from the PCI\n */\ninline static n_rslt n_pci_pack(addr_md mode, n_pdu_t* n_pdu, const uint8_t* dt)\n{\n\tn_rslt result = N_ERROR;\n\n\tif (n_pdu != NULL && dt != NULL)\n\t{\n\t\tuint8_t offs = (mode & 0x01);\n\n\t\tswitch (n_pdu->n_pci.pt)\n\t\t{\n\t\tcase N_PCI_T_SF:\n\t\t\tif (n_pdu->n_pci.dl <= (uint16_t)(7 - offs))\n\t\t\t{\n\t\t\t\tn_pdu->dt[0 + offs] = (n_pdu->n_pci.pt) << 4\n\t\t\t\t\t| (uint8_t)(n_pdu->n_pci.dl & 0x0F);\n\t\t\t}\n\t\t\telse\n\t\t\t{\n\t\t\t\tn_pdu->dt[0 + offs] = 0 + ((n_pdu->n_pci.pt) << 4);\n\t\t\t\tn_pdu->dt[1 + offs] = (uint8_t)n_pdu->n_pci.dl;\n\t\t\t}\n\t\t\tresult = N_OK;\n\t\t\tbreak;\n\t\tcase N_PCI_T_CF:\n\t\t\tn_pdu->dt[0 + offs] = (n_pdu->n_pci.pt) << 4\n\t\t\t\t| n_pdu->n_pci.sn;\n\t\t\tresult = N_OK;\n\t\t\tbreak;\n\t\tcase N_PCI_T_FF:\n\t\t\tn_pdu->dt[0 + offs] = (n_pdu->n_pci.pt) << 4\n\t\t\t\t| (n_pdu->n_pci.dl & 0x0F00) >> 8;\n\t\t\tn_pdu->dt[1 + offs] = n_pdu->n_pci.dl & 0x00FF;\n\t\t\tresult = N_OK;\n\t\t\tbreak;\n\t\tcase N_PCI_T_FC:\n\t\t\tn_pdu->dt[0 + offs] = (n_pdu->n_pci.pt) << 4\n\t\t\t\t| n_pdu->n_pci.fs;\n\t\t\tn_pdu->dt[1 + offs] = n_pdu->n_pci.bs;\n\t\t\tn_pdu->dt[2 + offs] = n_pdu->n_pci.st;\n\t\t\tresult = N_OK;\n\t\t\tbreak;\n\t\tdefault:\n\t\t\tresult = N_ERROR;\n\t\t\tbreak;\n\t\t}\n\t}\n\treturn result;\n}\n\n/*\n * Convert the PCI from the CANBus Frame\n */\ninline static n_rslt n_pci_unpack(addr_md mode, n_pdu_t* n_pdu, uint8_t dlc, uint8_t* dt)\n{\n    n_rslt result = N_ERROR;\n\n    if ((n_pdu != NULL) && (dt != NULL))\n    {\n        uint8_t offs = (uint8_t)(mode & 0x01U); // Make use and purpose of 'offs' explicit\n\n        // Document the operation's intent clearly to avoid being seen as 'dead code'\n        n_pdu->n_pci.pt = (uint8_t)((dt[offs] & 0xF0U) >> 4U);\n\n        switch (n_pdu->n_pci.pt)\n        {\n            case N_PCI_T_SF:\n                // Conditional operation based on 'dlc', not dead code\n                n_pdu->n_pci.dl = (dlc <= 8U) ? (uint8_t)(dt[offs] & 0x0FU) : dt[1U + offs];\n                result = n_pdu->n_pci.dl > dlc ? N_ERROR : N_OK;\n                break;\n\n            case N_PCI_T_CF:\n                // Direct assignment, the operation depends on 'dt' content\n                n_pdu->n_pci.sn = (uint8_t)(dt[offs] & 0x0FU);\n                n_pdu->sz = dlc - (1U + offs);\n                result = N_OK;\n                break;\n\n            case N_PCI_T_FF:\n                // Combine two bytes into a larger value, clearly intentional\n                n_pdu->n_pci.dl = ((uint16_t)(dt[offs] & 0x0FU) << 8U) | dt[1U + offs];\n                n_pdu->sz = dlc - (2U + offs);\n                result = N_OK;\n                break;\n\n            case N_PCI_T_FC:\n                // Sequential assignments based on protocol, clearly used\n                n_pdu->n_pci.fs = (uint8_t)(dt[offs] & 0x0FU);\n                n_pdu->n_pci.bs = dt[1U + offs];\n                n_pdu->n_pci.st = dt[2U + offs] <= 0x7F ? dt[2U + offs] :\n\t\t\t\t\t\t\t\t  ((dt[2U + offs]>= 0xF1 && dt[2U + offs]<=0xF9) ? 1 : 127);\n                n_pdu->sz = dlc - (3U + offs); // Adjust for correct data length calculation\n                result = N_OK;\n                break;\n\n            default:\n                result = N_ERROR;\n                break;\n        }\n    }\n\n    return result;\n}\n\n/*\n * Helper function of 'n_pdu_pack' to use the frame payload.\n */\ninline static n_rslt n_pdu_pack_dt(addr_md mode, n_pdu_t* n_pdu, uint8_t* dt)\n{\n\tn_rslt result = N_ERROR;\n\tuint8_t offset = 0xFF;\n\n\tif (dt != NULL)\n\t{\n\t\tswitch (n_pdu->n_pci.pt)\n\t\t{\n\t\tcase N_PCI_T_SF:\n\t\t\toffset = n_get_dt_offset(mode, N_PCI_T_SF, n_pdu->sz);\n\t\t\tbreak;\n\t\tcase N_PCI_T_FF:\n\t\t\toffset = n_get_dt_offset(mode, N_PCI_T_FF, n_pdu->sz);\n\t\t\tbreak;\n\t\tcase N_PCI_T_CF:\n\t\t\toffset = n_get_dt_offset(mode, N_PCI_T_CF, n_pdu->sz);\n\t\t\tbreak;\n\t\tcase N_PCI_T_FC:\n\t\t\toffset = n_get_dt_offset(mode, N_PCI_T_FC, n_pdu->sz);\n\t\t\tbreak;\n\t\tdefault:\n\t\t\toffset = 0xFF;\n\t\t\tbreak;\n\t\t}\n\n\t\tif (offset == 0xFF)\n\t\t{\n\t\t\tresult = N_ERROR;\n\t\t}\n\t\telse \n\t\t{\n\t\t\tmemmove(&n_pdu->dt[offset], dt, n_pdu->sz);\n\t\t\tresult = N_OK;\t\t\t\n\t\t}\n\t\t\n\t}\n\treturn result;\n}\n\n/*\n * Helper function of 'n_pdu_unpack' to use the frame payload.\n */\ninline static n_rslt n_pdu_unpack_dt(addr_md mode, n_pdu_t* n_pdu, uint8_t* dt)\n{\n\tn_rslt result = N_ERROR;\n\tuint8_t offset = 0xFF;\n\tuint16_t sz = 0;\n\n\tif ((n_pdu != NULL) && (dt != NULL))\n\t{\n\t\tswitch (n_pdu->n_pci.pt)\n\t\t{\n\t\tcase N_PCI_T_SF:\n\t\t\toffset = n_get_dt_offset(mode, N_PCI_T_SF, n_pdu->n_pci.dl);\n\t\t\tsz = n_pdu->n_pci.dl;\n\t\t\tbreak;\n\t\tcase N_PCI_T_FF:\n\t\t\toffset = n_get_dt_offset(mode, N_PCI_T_FF, n_pdu->n_pci.dl);\n\t\t\tsz = n_pdu->sz;\n\t\t\tbreak;\n\t\tcase N_PCI_T_CF:\n\t\t\toffset = n_get_dt_offset(mode, N_PCI_T_CF, n_pdu->n_pci.dl);\n\t\t\tsz = n_pdu->sz;\n\t\t\tbreak;\n\t\tcase N_PCI_T_FC:\n\t\t\toffset = n_get_dt_offset(mode, N_PCI_T_FC, n_pdu->n_pci.dl);\n\t\t\tsz = n_pdu->sz;\n\t\t\tbreak;\n\t\tdefault:\n\t\t\toffset = 0xFF;\n\t\t\tsz = 0;\n\t\t\tresult = N_ERROR;\n\t\t\tbreak;\n\t\t}\n\n\t\tif (offset == 0xFF)\n\t\t{\n\t\t\tresult = N_ERROR;\n\t\t}\n\t\telse \n\t\t{\n\t\t\tmemmove(n_pdu->dt, &dt[offset], sz);\n\t\t\tresult = N_OK;\t\t\t\n\t\t}\n\t}\n\treturn result;\n}\n\n/*\n * Convert CANBus frame from PDU\n */\ninline static n_rslt n_pdu_pack(addr_md mode, n_pdu_t* n_pdu, uint32_t* id, uint8_t* dt)\n{\n    if ((dt == NULL) || (id == NULL))\n    {\n        return N_ERROR;\n    }\n\n    switch (mode)\n    {\n        case N_ADM_EXTENDED:\n        \t*id = 0x80U\n                | (n_pdu->n_ai.n_pr << 8)\n                | (n_pdu->n_ai.n_ae << 3)\n                | (n_pdu->n_ai.n_sa) \n                | ((n_pdu->n_ai.n_tt == N_TA_T_PHY) ? 0x40U : 0x00U); \n\t\t\t\tn_pdu->dt[0] = n_pdu->n_ai.n_ta;\n\t\t\tbreak;\n        case N_ADM_NORMAL:\n            *id = 0x80U\n                | (n_pdu->n_ai.n_pr << 8)\n                | (n_pdu->n_ai.n_ta << 3)\n                | n_pdu->n_ai.n_sa\n                | ((n_pdu->n_ai.n_tt == N_TA_T_PHY) ? 0x40U : 0x00U);\n            break;\n        case N_ADM_MIXED29:\n            *id = (n_pdu->n_ai.n_pr << 26) \n                | ((n_pdu->n_ai.n_tt == N_TA_T_PHY) ? 0xCEU : 0xCDU) << 16\n                | (n_pdu->n_ai.n_ta << 8)\n                | n_pdu->n_ai.n_sa;\n            n_pdu->dt[0] = n_pdu->n_ai.n_ae;\n            break;\n        case N_ADM_FIXED:\n            *id = (n_pdu->n_ai.n_pr << 26)\n                | ((n_pdu->n_ai.n_tt == N_TA_T_PHY) ? 0xDAU : 0xDBU) << 16\n                | (n_pdu->n_ai.n_ta << 8)\n                | n_pdu->n_ai.n_sa;\n            break;\n        case N_ADM_MIXED11:\n            *id = 0x80U\n                | (n_pdu->n_ai.n_pr << 8)\n                | (n_pdu->n_ai.n_ta << 3)\n                | n_pdu->n_ai.n_sa\n                | ((n_pdu->n_ai.n_tt == N_TA_T_PHY) ? 0x40U : 0x00U);\n            n_pdu->dt[0] = n_pdu->n_ai.n_ae;\n            break;\n        default:\n            return N_ERROR;\n    }\n\n    if (n_pci_pack(mode, n_pdu, dt) == N_OK)\n\t{\n    \treturn n_pdu_pack_dt(mode, n_pdu, dt);\n\t}\n\treturn N_ERROR;\n}\n\n/*\n * Convert PDU from CANBus frame\n */\ninline static n_rslt n_pdu_unpack(addr_md mode, n_pdu_t* n_pdu, uint32_t id, uint8_t dlc, uint8_t* dt)\n{\n\tif (n_pdu == NULL || dt == NULL)\n\t{\n\t\treturn N_ERROR;\n\t}\n\n\tswitch (mode)\n\t{\n\tcase N_ADM_MIXED11:\n\t\tn_pdu->n_ai.n_ae = dt[0];\n#ifdef fallthrough\n\t\t__attribute__((fallthrough));\n#endif\n\tcase N_ADM_NORMAL:\n\t\tn_pdu->n_ai.n_pr = (uint8_t)((id & 0x700U) >> 8);\n\t\tn_pdu->n_ai.n_ta = (uint8_t)((id & 0x38U) >> 3);\n\t\tn_pdu->n_ai.n_sa = (uint8_t)(id & 0x07U);\n\t\tn_pdu->n_ai.n_tt = (uint8_t)(((id & 0x40U) >> 6) == 1 ? N_TA_T_PHY : N_TA_T_FUNC);\n\t\tbreak;\n\tcase N_ADM_MIXED29:\n\t\tn_pdu->n_ai.n_ae = dt[0];\n\t\tn_pdu->n_ai.n_pr = (uint8_t)((id & 0x1C000000) >> 26);\n\t\tn_pdu->n_ai.n_tt = (uint8_t)(((id & 0x00FF0000) >> 16) == 0xCE ? N_TA_T_PHY : N_TA_T_FUNC);\n\t\tn_pdu->n_ai.n_ta = (uint8_t)((id & 0x0000FF00) >> 8);\n\t\tn_pdu->n_ai.n_sa = (uint8_t)((id & 0x000000FF) >> 0);\n\t\tbreak;\n\tcase N_ADM_FIXED:\n\t\tn_pdu->n_ai.n_pr = (uint8_t)((id & 0x1C000000) >> 26);\n\t\tn_pdu->n_ai.n_tt = (uint8_t)(((id & 0x00FF0000) >> 16) == 0xDA ? N_TA_T_PHY : N_TA_T_FUNC);\n\t\tn_pdu->n_ai.n_ta = (uint8_t)((id & 0x0000FF00) >> 8);\n\t\tn_pdu->n_ai.n_sa = (uint8_t)((id & 0x000000FF) >> 0);\n\t\tbreak;\n\tcase N_ADM_EXTENDED:\n\t\tn_pdu->n_ai.n_pr = (uint8_t)((id & 0x700U) >> 8);\n\t\tn_pdu->n_ai.n_ae = (uint8_t)((id & 0xF8U) >> 3);\n\t\tn_pdu->n_ai.n_sa = (uint8_t)(id & 0x07U);\n\t\tn_pdu->n_ai.n_tt = (uint8_t)((id & 0x40U) >> 6 == 1 ? N_TA_T_PHY : N_TA_T_FUNC);\n\t\tn_pdu->n_ai.n_ta = dt[0];\n\t\tbreak;\n\tdefault:\n\t\treturn N_UNE_PDU;\n\t}\n\n\tif (n_pci_unpack(mode, n_pdu, dlc, dt) == N_OK)\n\t{\n\t\treturn n_pdu_unpack_dt(mode, n_pdu, dt);\n\t}\n\treturn N_ERROR;\n}\n\n/*\n * Given the correct parameters, the service informs the upper-layer/user about\n * an event by using the appropriate callbacks. The function does not support\n * the N_CHG_P_CONF signal type.\n */\ninline static void signaling(signal_tp tp, n_iostream_t* strm, void(*cb)(void*), uint16_t msg_sz, n_rslt sgn_rslt)\n{\n\tif (cb != NULL)\n\t{\n\t\tswitch (tp)\n\t\t{\n\t\tcase N_INDN:\n\t\t\tsgn_indn.rslt = sgn_rslt;\n\t\t\tsgn_indn.msg_sz = msg_sz;\n\t\t\tsgn_indn.fr_fmt = strm->fr_fmt;\n\t\t\tmemmove(&sgn_indn.n_ai, &strm->pdu.n_ai, sizeof(n_ai_t));\n\t\t\tmemmove(&sgn_indn.n_pci, &strm->pdu.n_pci, sizeof(n_pci_t));\n\t\t\tmemmove(&sgn_indn.msg, strm->msg, msg_sz);\n\t\t\tstrm->sts = N_S_IDLE;\n\t\t\tcb(&sgn_indn);\n\t\t\tbreak;\n\t\tcase N_FF_INDN:\n\t\t\tsgn_ff_indn.fr_fmt = strm->fr_fmt;\n\t\t\tsgn_ff_indn.msg_sz = msg_sz;\n\t\t\tmemmove(&sgn_ff_indn.n_ai, &strm->pdu.n_ai, sizeof(n_ai_t));\n\t\t\tmemmove(&sgn_ff_indn.n_pci, &strm->pdu.n_pci, sizeof(n_pci_t));\n\t\t\tstrm->sts = (uint8_t)((uint32_t)strm->sts | (uint32_t)N_S_RX_BUSY);\n\t\t\tcb(&sgn_ff_indn);\n\t\t\tbreak;\n\t\tcase N_CONF:\n\t\t\tsgn_conf.rslt = sgn_rslt;\n\t\t\tmemmove(&sgn_conf.n_ai, &strm->pdu.n_ai, sizeof(n_ai_t));\n\t\t\tmemmove(&sgn_conf.n_pci, &strm->pdu.n_pci, sizeof(n_pci_t));\n\t\t\tcb(&sgn_conf);\n\t\t\tbreak;\n\t\tdefault:\n\t\t\treturn;\n\t\t}\n\t}\n\treturn;\n}\n\n/*\n * Check if any timeout should be occured.\n */\ninline static n_rslt process_timeouts(iso15765_t* ih)\n{\n\tif (ih->out.sts != N_S_TX_WAIT_FC || ih->out.last_upd.n_bs == 0 || ih->config.n_bs == 0)\n\t{\n\t\treturn N_OK;\n\t}\n\n\tn_rslt timeout = has_interval_passed(ih->clbs.get_ms(),ih->out.last_upd.n_bs,ih->config.n_bs);\n\n\tif(timeout == N_INV)\n\t{\n\t\treturn N_OK;\n\t}\n\t\n\n\t/* if timeout occures then reset the counters and report to the upper layer */\n\tih->out.cf_cnt = 0x0;\n\tsignaling(N_INDN, &ih->out, (void*)ih->clbs.indn, ih->out.msg_sz, N_TIMEOUT_Bs);\n\tih->clbs.on_error(N_TIMEOUT_Bs);\n\treturn N_TIMEOUT_Bs;\n}\n\n/*\n * Sends a Flow Control Frame upon request from the reception procedure\n */\nstatic n_rslt send_N_PCI_T_FC(iso15765_t* ih)\n{\n\tuint32_t id;\n\n\tih->out.sts |= N_S_TX_BUSY;\n\tih->fl_pdu.n_pci.pt = N_PCI_T_FC;\n\tih->fl_pdu.n_pci.bs = ih->config.bs;\n\tih->fl_pdu.n_pci.st = ih->config.stmin;\n\tih->fl_pdu.n_ai.n_ae = ih->in.pdu.n_ai.n_ae;\n\tih->fl_pdu.n_ai.n_sa = ih->in.pdu.n_ai.n_ta;\n\tih->fl_pdu.n_ai.n_ta = ih->in.pdu.n_ai.n_sa;\n\tih->fl_pdu.n_ai.n_pr = ih->in.pdu.n_ai.n_pr;\n\tih->fl_pdu.n_ai.n_tt = ih->in.pdu.n_ai.n_tt;\n\tih->in.cfg_bs = ih->config.bs;\n\n\tif (n_pdu_pack(ih->addr_md, &ih->fl_pdu, &id, ih->out.msg) != N_OK)\n\t{\n\t\tih->out.sts = (ih->out.sts & (~N_S_TX_BUSY));\n\t\treturn N_ERROR;\n\t}\n\n\tih->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);\n\tih->out.sts = (ih->out.sts & (~N_S_TX_BUSY));\n\treturn N_OK;\n}\n\n/*\n * Helper function to set some basic stream parameters value\n */\ninline static void set_stream_data(n_iostream_t* ist, uint8_t cf, uint8_t wf, stream_sts sts)\n{\n\tist->cf_cnt = cf;\n\tist->wf_cnt = wf;\n\tist->sts = sts;\n}\n\n/*\n * Check if current Wait Flow status counter reached the max WFS\n */\ninline static n_rslt check_max_wf_capacity(iso15765_t* ih)\n{\n\tif (ih->out.wf_cnt < ih->config.wf)\n\t{\n\t\treturn N_OK;\n\t}\n\n\tih->clbs.on_error(N_WFT_OVRN);\n\tset_stream_data(&ih->out, 0, 0, N_S_IDLE);\n\treturn N_WFT_OVRN;\n}\n\n/*\n * Process inbound First Frame reception and report to the upper layer using the\n * indication callback function.\n */\nstatic n_rslt process_in_ff(iso15765_t* ih)\n{\n\tif (ih->in.msg_sz > I15765_MSG_SIZE)\n\t{\n\t\tih->clbs.on_error(N_INV_REQ_SZ);\n\t\treturn N_INV_REQ_SZ;\n\t}\n\t/* If reception is in progress: Terminate the current reception, report an\n\t* N_USData.indication, with <N_Result> set to N_UNEXP_PDU, to the upper layer, and\n\t* process the FF N_PDU as the start of a new reception.*/\n\tif ((ih->in.sts & N_S_RX_BUSY) != 0)\n\t{\n\t\tih->clbs.on_error(N_UNE_PDU);\n\t\tsignaling(N_INDN, &ih->in, (void*)ih->clbs.indn, ih->in.msg_sz, N_UNE_PDU);\n\t}\n\n\t/* Copy all data, init the CFrames reception parameters and send a FC */\n\tmemmove(ih->in.msg, ih->in.pdu.dt, ih->in.pdu.sz);\n\tih->in.msg_sz = ih->in.pdu.n_pci.dl;\n\tih->in.msg_pos = ih->in.pdu.sz;\n\tih->in.cf_cnt = 0;\n\tih->in.wf_cnt = 0;\n\tsignaling(N_FF_INDN, &ih->in, (void*)ih->clbs.ff_indn, ih->in.msg_sz, N_OK);\n\tsend_N_PCI_T_FC(ih);\n\treturn N_OK;\n}\n\n/*\n * Process inbound Single Frame reception and report to the upper layer using the\n * indication callback function.\n */\nstatic n_rslt process_in_sf(iso15765_t* ih)\n{\n\t/* If reception is in progress: Terminate the current reception, report an\n\t* N_USData.indication, with <N_Result> set to N_UNEXP_PDU, to the upper layer, and\n\t* process the SF N_PDU as the start of a new reception.*/\n\tif ((ih->in.sts & N_S_RX_BUSY) != 0)\n\t{\n\t\tih->clbs.on_error(N_UNE_PDU);\n\t\tsignaling(N_INDN, &ih->in, (void*)ih->clbs.indn, ih->in.msg_sz, N_UNE_PDU);\n\t}\n\tmemmove(&ih->in.msg[0], ih->in.pdu.dt, ih->in.pdu.n_pci.dl);\n\tih->in.sts = N_S_IDLE;\n\tsignaling(N_INDN, &ih->in, (void*)ih->clbs.indn, ih->in.pdu.n_pci.dl, N_OK);\n\treturn N_OK;\n}\n\n/*\n * Process inbound Consecutive Frames. Perform all the required checks according\n * to (ref: iso15765-2 p.26) and if everything is ok copy all the data to the\n * inbound stream buffer and update the reception parameters (CF_cnt,timeouts etc)\n */\nstatic n_rslt process_in_cf(iso15765_t* ih)\n{\n\tn_rslt rslt = N_OK;\n\n\t/* According to (ref: iso15765-2 p.26) if we are not in progress of\n\t* reception we should ignore it */\n\tif ((ih->in.sts & N_S_RX_BUSY) == 0)\n\t{\n\t\trslt = N_UNE_CF;\n\t\tgoto in_cf_error;\n\t}\n\n\t/* Increase the CF counter and check if the reception sequence is ok */\n\tih->in.cf_cnt = ih->in.cf_cnt + 1 > 0xFF ? 0 : ih->in.cf_cnt + 1;\n\tih->in.sn_glb = (ih->in.sn_glb + 1) & 0x0F;\n\tif (ih->in.sn_glb != ih->in.pdu.n_pci.sn)\n\t{\n\t\trslt = N_INV_SEQ_NUM;\n\t\tgoto in_cf_error;\n\t}\n\t\n\t/* As long as everything is ok the we copy the frame data to the inbound\n\t* stream buffer. Afterwards check if the message size is completed and\n\t* signal the user and afterwards reset the inboud stream */\n\tmemmove(&ih->in.msg[ih->in.msg_pos], ih->in.pdu.dt, ih->in.pdu.sz);\n\tih->in.msg_pos += ih->in.pdu.sz;\n\n\tif (ih->in.msg_pos >= ih->in.msg_sz)\n\t{\n\t\tsignaling(N_INDN, &ih->in, (void*)ih->clbs.indn, ih->in.msg_sz, N_OK);\n\t\tmemset(&ih->in, 0, sizeof(n_iostream_t));\n\t\tih->in.last_upd.n_cr = 0;\n\t\t//\tih->in.sts = N_S_IDLE;\n\t\treturn N_OK;\n\t}\n\t/* if we reach the max CF counter, then we send a FC frame */\n\tif(ih->config.bs != 0)\n\t{\n\t\tif (ih->in.cf_cnt == ih->config.bs)\n\t\t{\n\t\t\tih->in.cf_cnt = 0;\n\t\t\tsend_N_PCI_T_FC(ih);\n\t\t}\n\t}\n\t/* Update the Cr timer */\n\tih->in.last_upd.n_cr = ih->clbs.get_ms();\n\treturn rslt;\n\nin_cf_error:\n\tih->clbs.on_error(rslt);\n\tih->in.sts = N_S_IDLE;\n\treturn rslt;\n}\n\n/*\n * Process inbound Flow Control Frames. Outcome depends on the stream status\n * (if it is busy etc) as well as the Flow Control Status.\n */\nstatic n_rslt process_in_fc(iso15765_t* ih)\n{\n\tn_rslt rslt = N_UNE_PDU;\n\n\t/* According to (ref: iso15765-2 p.26) if we are not expecting FC frame\n\t* we should ignore it */\n\tif (ih->out.sts != N_S_TX_WAIT_FC)\n\t{\n\t\treturn rslt;\n\t}\n\n\tswitch (ih->in.pdu.n_pci.fs)\n\t{\n\tcase N_WAIT:\n\t\t/* Increase the WF counter, check if we reached the WF Limit to abort\n\t\t* the reception and (if not WF overflow) update the Bs time */\n\t\tih->out.wf_cnt += 1;\n\t\tif (check_max_wf_capacity(ih) != N_WFT_OVRN)\n\t\t{\n\t\t\treturn N_OK;\t\t\t\n\t\t}\n\t\tih->out.last_upd.n_bs = ih->clbs.get_ms();\n\t\trslt = N_WFT_OVRN;\n\t\tbreak;\n\tcase N_OVERFLOW:\n\t\trslt = N_BUFFER_OVFLW;\n\t\tbreak;\n\tcase N_CONTINUE:\n\t\t/* Store the requested transmission parameters (from receiver)\n\t\t* to the outbound stream, reset the counters of CFs(1) and WFs(0)\n\t\t* and change the outbound stream status to Ready */\n\t\tih->out.cfg_bs = ih->in.pdu.n_pci.bs;\n\t\tih->out.stmin = ih->in.pdu.n_pci.st;\n\t\tset_stream_data(&ih->out, 1, 0, N_S_TX_READY);\n\t\treturn N_OK;\n\tdefault:\n\t\trslt = N_UNE_FC_STS;\n\t\tbreak;\n\t}\n\n\t/* If there is an error (only way to be here) then reset the\n\t* outbound stream, set the counters of CFs(0) and WFs(0)\n\t* and change the outbound stream status to Idle. Use on_error\n\t* callback to inform the upper layer */\n\tset_stream_data(&ih->out, 0, 0, N_S_IDLE);\n\tih->clbs.on_error(rslt);\n\tih->in.sts = N_S_IDLE;\n\treturn rslt;\n}\n\n/*\n * Inbound stream process. The function receives a canbus frame dequeued by\n * the 'iso15765_process' and performs any needed operation to identify and\n * consume the underlying information.\n */\ninline static n_rslt iso15765_process_in(iso15765_t* ih, canbus_frame_t* frame)\n{\n\t/* Converting the canbus frame to PDU format and process it by its PCI Type */\n\tih->in.fr_fmt = frame->fr_format;\n\tif (n_pdu_unpack(ih->addr_md, &ih->in.pdu, frame->id, (uint8_t)frame->dlc, frame->dt) == N_OK)\n\t{\n\t\tswitch (ih->in.pdu.n_pci.pt)\n\t\t{\n\t\tcase N_PCI_T_FC:\n\t\t\treturn process_in_fc(ih);\n\t\tcase N_PCI_T_CF:\n\t\t\treturn process_in_cf(ih);\n\t\tcase N_PCI_T_SF:\n\t\t\treturn process_in_sf(ih);\n\t\tcase N_PCI_T_FF:\n\t\t\treturn process_in_ff(ih);\n\t\tdefault:\n\t\t\tbreak;\n\t\t}\n\t}\n\n\t/* According to (ref: iso15765-2 p.26) if PDU is not valid\n\t* we should ignore it */\n\tih->clbs.on_error(N_INV_PDU);\n\treturn N_INV_PDU;\n}\n\n/*\n * Procces the outbound stream.\n */\nstatic n_rslt iso15765_process_out(iso15765_t* ih)\n{\n\t/* if there is no pending action just return */\n\tif (ih->out.sts != N_S_TX_BUSY && ih->out.sts != N_S_TX_READY)\n\t{\n\t\treturn N_IDLE;\n\t}\n\n\tuint32_t id;\n\tn_rslt rslt = N_ERROR;\n\tn_rslt timeout = N_ERROR;\n\t\n\t/* Find the PCI type of the pending outbound stream */\n\tih->out.pdu.n_pci.pt = n_out_frame_type(ih);\n\n\tswitch (ih->out.pdu.n_pci.pt)\n\t{\n\tcase N_PCI_T_SF:\n\t\t/* Copy all the data of the SF to the outbound stream, pack and send the canbus frame */\n\t\tih->out.pdu.n_pci.dl = ih->out.msg_sz;\n\t\tih->out.pdu.sz = ih->out.msg_sz;\n\n\t\tif (n_pdu_pack(ih->addr_md, &ih->out.pdu, &id, ih->out.msg) != N_OK)\n\t\t{\n\t\t\tgoto iso15765_process_out_cfm;\n\t\t}\n\t\t\t\n\t\trslt = 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;\n\t\tgoto iso15765_process_out_cfm;\n\t\tbreak;\n\n\tcase N_PCI_T_FF:\n\t\t/* Copy all the data of the FF to the outbound stream for transmission and prepare the service\n\t\t* for a multi-frame reception */\n\t\tih->out.pdu.n_pci.dl = ih->out.msg_sz;\n\t\tih->out.wf_cnt = 0;\n\t\tih->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);\n\t\tih->out.msg_pos = ih->out.pdu.sz;\n\t\tif (n_pdu_pack(ih->addr_md, &ih->out.pdu, &id, ih->out.msg) != N_OK)\n\t\t{\n\t\t\tgoto iso15765_process_out_cfm;\n\t\t}\n\t\tih->out.cf_cnt = 1;\n\n\t\t/* after this frame we expect a Flow Control then assign the correct flag before the\n\t\t* transmission to avoid any issues and start the timer */\n\t\tih->out.sts = N_S_TX_WAIT_FC;\n\t\trslt = 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;\n\t\tih->out.last_upd.n_bs = ih->clbs.get_ms();\n\t\treturn (rslt == 0) ? N_OK : N_ERROR;\n\n\tcase N_PCI_T_CF:\n\t\t/* if the minimun difference between transmissions is not reached then skip */\n\t\ttimeout = has_interval_passed(ih->clbs.get_ms(), ih->out.last_upd.n_cs, ih->out.stmin);\n\t\tif (timeout == N_INV)\n\t\t{\n\t\t\treturn N_OK;\n\t\t} \n\t\telse if (timeout == N_ERROR)\n\t\t{\n\t\t\treturn N_ERROR;\n\t\t}\n\t\t\t\n\t\t/* Increase the sequence number of the frame and the CF counter of the stream\n\t\t* and then pack the PDU to a CANBus frame */\n\t\tih->out.pdu.n_pci.sn = ih->out.sn_glb;\n\t\tih->out.sn_glb = (ih->out.sn_glb + 1) & 0x0F;\n\n\t\tif (ih->out.fr_fmt == CBUS_FR_FRM_STD)\n\t\t{\n\t\t\tuint8_t max_payload = (ih->addr_md & 0x01) == 0 ? 7 : 6;\n\t\t\tih->out.pdu.sz = ih->out.msg_sz - ih->out.msg_pos;\n\t\t\tih->out.pdu.sz = ih->out.pdu.sz >= max_payload ? max_payload : ih->out.pdu.sz;\n\t\t}\n\t\telse\n\t\t{\n\t\t\tuint8_t max_payload = (ih->addr_md & 0x01) == 0 ? 63 : 62;\n\t\t\tih->out.pdu.sz = ih->out.msg_sz - ih->out.msg_pos;\n\t\t\tih->out.pdu.sz = ih->out.pdu.sz >= max_payload ? max_payload : ih->out.pdu.sz;\n\t\t}\n\n\t\tif (n_pdu_pack(ih->addr_md, &ih->out.pdu, &id, &ih->out.msg[ih->out.msg_pos]) != N_OK)\n\t\t{\n\t\t\tgoto iso15765_process_out_cfm;\n\t\t}\n\t\t\t\n\t\t/* Increase the position which indicates the remaining data in the inbound buffer */\n\t\tih->out.msg_pos += ih->out.pdu.sz;\n\n\t\t/* if after this frame we expect a Flow Control then assign the correct flag before the\n\t\t* transmission to avoid any issues and start the timer */\n\t\tif (ih->out.cf_cnt == ih->out.cfg_bs)\n\t\t{\n\t\t\tih->out.sts = N_S_TX_WAIT_FC;\n\t\t\tih->out.last_upd.n_bs = ih->clbs.get_ms();\n\t\t}\n\t\tih->out.cf_cnt = ih->out.cf_cnt == 0xFF ? 0 : ih->out.cf_cnt + 1;\n\t\t/* send the canbus frame! */\n\t\tuint8_t of1 = (ih->addr_md & 0x01) == 0 ? 1 : 2;\n\t\trslt = 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;\n\t\tih->out.last_upd.n_cs = ih->clbs.get_ms();\n\t\tif (ih->out.msg_pos >= ih->out.msg_sz)\n\t\t{\n\t\t\tgoto iso15765_process_out_cfm;\n\t\t}\n\t\treturn N_OK;\n\n\tdefault:\n\t\tbreak;\n\t}\n\n\treturn N_ERROR;\n\niso15765_process_out_cfm:\n\tih->out.sts = N_S_IDLE;\n\tih->out.cf_cnt = 0;\n\tih->out.wf_cnt = 0;\n\tsignaling(N_CONF, &ih->out, (void*)ih->clbs.cfm, 0, rslt);\n\treturn rslt;\n}\n\n/******************************************************************************\n* Definition  | Public Functions\n******************************************************************************/\n\n/*\n * Initialize and check if the configuration parameters of the ISO15765 handler are\n * correct. Not assigned callback which are not assigned will be automatically assigned\n * to the static UNUSED functions. If no error occurs, the system can start the service.\n */\nn_rslt iso15765_init(iso15765_t* instance)\n{\n\tif (instance == NULL)\n\t{\n\t\treturn N_NULL;\n\t}\n\n\t/* check if parameters have correct values */\n\tif ((instance->fr_id_type != CBUS_ID_T_STANDARD && instance->fr_id_type != CBUS_ID_T_EXTENDED)\n\t\t|| (instance->fr_id_type & instance->addr_md) == 0)\n\t{\n\t\treturn N_WRG_VALUE;\n\t}\n\n\t/* check if must-have functions are assigned */\n\tif (instance->clbs.send_frame == NULL || instance->clbs.get_ms == NULL)\n\t{\n\t\treturn N_MISSING_CLB;\n\t}\n\n\t/* if optional functions are not assigned then use the defaults */\n\tif (instance->clbs.indn == NULL)\n\t{\n\t\tinstance->clbs.indn = indn;\n\t}\n\n\tif (instance->clbs.ff_indn == NULL)\n\t{\n\t\tinstance->clbs.ff_indn = ff_indn;\n\t}\n\n\tif (instance->clbs.cfm == NULL)\n\t{\n\t\tinstance->clbs.cfm = cfm;\n\t}\n\n\tif (instance->clbs.cfg_cfm == NULL)\n\t{\n\t\tinstance->clbs.cfg_cfm = cfg_cfm;\n\t}\n\n\t/* clear the in/out streams */\n\tmemset(&instance->in, 0, sizeof(n_iostream_t));\n\tmemset(&instance->out, 0, sizeof(n_iostream_t));\n\tmemset(&instance->fl_pdu, 0, sizeof(n_pdu_t));\n\t/* init the incoming canbus frame queue(buffer) */\n\tif (iqueue_init(&instance->inqueue,\n\t\tI15765_QUEUE_ELMS,\n\t\tsizeof(canbus_frame_t),\n\t\tinstance->inq_buf) != I_OK)\n\t\t{\n\t\t\treturn N_INV;\n\t\t}\n\n\tISO_15675_UNUSED(sgn_chg_cfm);\n\n\tinstance->init_sts = N_OK;\n\treturn instance->init_sts;\n}\n\n/*\n * Enqueues an incoming frame from the lower level (CANBus) to a buffer. The service\n * will process the frames during the call of the 'iso15765_process' function. Usually\n * this function should be called when a canbus frame is received.\n */\nn_rslt iso15765_enqueue(iso15765_t* instance, canbus_frame_t* frame)\n{\n\tif (instance == NULL || frame == NULL)\n\t{\n\t\treturn N_NULL;\n\t}\n\n\tif (instance->init_sts != N_OK)\n\t{\n\t\treturn N_ERROR;\n\t}\n\n\tif (frame->fr_format == CBUS_FR_FRM_STD)\n\t{\n\t\tif (frame->dlc == 0 || frame->dlc > 8)\n\t\t{\n\t\t\treturn N_ERROR;\n\t\t}\n\t}\n\telse if (frame->fr_format == CBUS_FR_FRM_FD)\n\t{\n\t\tif (frame->dlc == 0 ||\n\t\t\t(frame->dlc > 8 && frame->dlc != 12 && frame->dlc != 16 &&\n\t\t\tframe->dlc != 20 && frame->dlc != 24 && frame->dlc != 32 &&\n\t\t\tframe->dlc != 48 && frame->dlc != 64))\n\t\t{\n\t\t\treturn N_ERROR;\n\t\t}\n\t}\n\telse \n\t{\n\t\treturn N_ERROR;\n\t}\n\n\n\treturn iqueue_enqueue(&instance->inqueue, frame) == I_OK\n\t\t? N_OK : N_BUFFER_OVFLW;\n}\n\n/*\n * Request to send a message. Depending on the message a call to 'iso15765_process'\n * may be required. The service can send one message per time as long as the\n * communication is syncronous\n */\nn_rslt iso15765_send(iso15765_t* instance, n_req_t* frame)\n{\n\tif (instance == NULL)\n\t{\n\t\treturn N_NULL;\n\t}\n\n\tif (instance->init_sts != N_OK)\n\t{\n\t\treturn N_ERROR;\n\t}\n\n\t/* Make sure that there is no transmission in progress */\n\tif (instance->out.sts != N_S_IDLE)\n\t{\n\t\treturn N_TX_BUSY;\n\t}\n\t/* and the requested size is fitting in our outbound buffer */\n\tif (frame->msg_sz > I15765_MSG_SIZE)\n\t{\n\t\treturn N_BUFFER_OVFLW;\n\t}\n\t/* or there is not actual message to be sent */\n\tif (frame->msg_sz == 0)\n\t{\n\t\treturn N_INV_REQ_SZ;\n\t}\n\t/* check if frame type is correct */\n\tif (frame->fr_fmt != CBUS_FR_FRM_STD && frame->fr_fmt != CBUS_FR_FRM_FD)\n\t{\n\t\treturn N_INV;\n\t}\n\t/* check if Target Address Type is correct */\n\tif (frame->n_ai.n_tt != N_TA_T_PHY && frame->n_ai.n_tt != N_TA_T_FUNC)\n\t{\n\t\treturn N_INV;\n\t}\n\n\t/* copy all the info and data to the outbound buffer */\n\tinstance->out.fr_fmt = frame->fr_fmt;\n\tinstance->out.msg_sz = frame->msg_sz;\n\tmemmove(instance->out.msg, frame->msg, frame->msg_sz);\n\tmemmove(&instance->out.pdu.n_ai, &frame->n_ai, sizeof(n_ai_t));\n\tinstance->out.sn_glb = 1;\n\tinstance->out.cf_cnt = 0;\n\tinstance->out.wf_cnt = 0;\n\tinstance->out.sts = N_S_TX_BUSY;\n\n\treturn N_OK;\n}\n\n/*\n * Process the inbound/outbound streams of the service. For optimal operation\n * this function should be called continiously with a minimal delay. It is\n * suggested to be used in a thread.\n */\nn_rslt iso15765_process(iso15765_t* instance)\n{\n\tif (instance == NULL)\n\t{\n\t\treturn N_NULL;\n\t}\n\n\tif (instance->init_sts != N_OK)\n\t{\n\t\treturn N_ERROR;\n\t}\n\n\t/* First check if a timeout is occured. Only for the inbound stream */\n\tn_rslt rslt = process_timeouts(instance);\n\tcanbus_frame_t frame;\n\n\t/* Dequeue all the incoming frames and process them */\n\twhile (iqueue_dequeue(&instance->inqueue, &frame) != I_EMPTY)\n\t{\n\t\trslt |= iso15765_process_in(instance, &frame);\n\t}\n\n\t/* Process the outbound stream */\n\trslt |= iso15765_process_out(instance);\n\treturn rslt;\n}\n\n/******************************************************************************\n* EOF - NO CODE AFTER THIS LINE\n******************************************************************************/\n"
  },
  {
    "path": "src/lib_iso15765.h",
    "content": "/*!\n@file   iso15765_2.h\n@brief  Header file of the ISO15765-2 library\n@t.odo\t-\n---------------------------------------------------------------------------\n\nGNU Affero General Public License v3.0  \n\nCopyright (c) 2024 Ioannis D. (devcoons)  \n\nThis program is free software: you can redistribute it and/or modify it \nunder the terms of the GNU Affero General Public License as published by \nthe Free Software Foundation, either version 3 of the License.  \n\nThis program is distributed in the hope that it will be useful,  \nbut WITHOUT ANY WARRANTY; without even the implied warranty of  \nMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the  \nGNU Affero General Public License for more details.  \n\nYou should have received a copy of the GNU Affero General Public License  \nalong with this program. If not, see <https://www.gnu.org/licenses/>.  \n\nFor commercial use, including proprietary or for-profit applications, \na separate license is required. Contact:  \n\n- GitHub: [https://github.com/devcoons](https://github.com/devcoons)  \n- Email: i_-_-_s@outlook.com \n*/\n/******************************************************************************\n* Preprocessor Definitions & Macros\n******************************************************************************/\n\n#ifndef DEVCOONS_ISO15765_2_H_\n#define DEVCOONS_ISO15765_2_H_\n\n#define I15765_MSG_SIZE\t\t516\t/* Max. size of the TP up to 4095 bytes */\n\n#define I15765_QUEUE_ELMS\t64\t/* No. of max incoming frames that the\n\t\t\t\t\t * reception buffer can hold */\n\n/* Alignment is required for Microcontrollers */\n#if defined(__clang__)\n\t#define ALIGNMENT __attribute__ ((aligned (4)))\n#elif defined(__GNUC__) || defined(__GNUG__)\n\t#define ALIGNMENT __attribute__ ((aligned (4)))\n#elif defined(_MSC_VER)\n\t#define ALIGNMENT __declspec(align(4))\n#endif\n\n#ifndef ISO_15675_UNUSED\n\t#define ISO_15675_UNUSED(x) ((void)(x))\n#endif\n\n/******************************************************************************\n * Includes\n******************************************************************************/\n\n#include <stdlib.h>\n#include <stdint.h>\n#include \"lib_iqueue.h\"\n\n/******************************************************************************\n * Enumerations, structures & Variables\n******************************************************************************/\n\n/* --- CANTP Mode - Frame Type (ref:                     ) ----------------- */\n\n#ifndef CBUS_FR_FORMAT\n#define CBUS_FR_FORMAT\ntypedef enum\n{\n\tCBUS_FR_FRM_STD = 0x01,\t\t/* Standard CANBUS */\n\tCBUS_FR_FRM_FD  = 0x02\t\t/* FD CANBUS       */\n}cbus_fr_format;\n#endif\n\n/* --- CANBus Mode [ID Type] (ref: iso15765-2 p.8) -------==---------------- */\n\n#ifndef CBUS_ID_TYPE\n#define CBUS_ID_TYPE\ntypedef enum\n{\n\tCBUS_ID_T_STANDARD = 0x04U,\t/* 11bits CAN Identifier */\n\tCBUS_ID_T_EXTENDED = 0x08U\t/* 29bits CAN Identifier */\n}cbus_id_type;\n#endif\n\n/* --- CANBus Frame (ref: iso15765-2 p.) ----------------------------------- */\n\n#ifndef CANBUS_FRAME\n#define CANBUS_FRAME\ntypedef struct\n{\n\tuint32_t id;\t\t/* CAN Frame Id */\n\tuint32_t id_type;\t/* CAN Frame Id Type `cbus_id_type` */\n\tuint16_t fr_format;\t/* CAN Frame Format `cbus_fr_format` */\n\tuint16_t dlc;\t\t/* Size of data */\n\tuint8_t dt[64];\t\t/* Actual data of the frame */\n}canbus_frame_t;\n#endif\n\n/* --- CANTP Addressing Mode (ref: iso15765-2 p.28) ------------------------ */\n\ntypedef enum\n{\n\tN_ADM_UNKN \t= 0x00,\t/* Not defined */\n\tN_ADM_NORMAL \t= 0x14,\t/* Normal Mode */\n\tN_ADM_FIXED \t= 0x28,\t/* Fixed Mode */\n\tN_ADM_MIXED11 \t= 0x35,\t/* Mixed 11bits ID Mode */\n\tN_ADM_EXTENDED \t= 0x45,\t/* Extended Mode */\n\tN_ADM_MIXED29 \t= 0x59\t/* Mixed 29bits ID Mode */\n}addr_md;\n\n/* --- Protocol Control Information Type (ref: iso15765-2 p.17) ------------ */\n\ntypedef enum\n{\n\tN_PCI_T_SF = 0x00U,\t/* Single Frame */\n\tN_PCI_T_FF = 0x01U,\t/* First Frame */\n\tN_PCI_T_CF = 0x02U,\t/* Consecutive Frame */\n\tN_PCI_T_FC = 0x03U,\t/* Flow Control Frame */\n\tN_PCI_T_UN = 0xFF\t/* Unknown */\n}pci_type;\n\n/* --- Network Target Address Type (ref: iso15765-2 p.9,29,30) ------------- */\n\ntypedef enum\n{\n\tN_TA_T_PHY  = 0x01,\t/* Physical */\n\tN_TA_T_FUNC = 0x02\t/* Functional */\n}ta_type;\n\n/* --- Message Type (ref: iso15765-2 p.8) ---------------------------------- */\n\ntypedef enum\n{\n\tN_MT_DIAG  = 0x00,\t/* Diagnostics */\n\tN_MT_RDIAG = 0x01\t/* Remote Diagnostics */\n}mtype;\n\n/* --- FlowControl Parameters (ref: iso15765-2 p.--------------------------- */\n\ntypedef enum\n{\n\tN_ST_MIN = 0x00U,\t/* SeparationTimeMin: the minimum time the sender should\n\t\t\t\t * wait between the transmissions of two CF N_PDUs */\n\tN_BS = 0x01U,\t\t/* BlockSize: the max. number of N_PDUs the receiver allows\n\t\t\t\t * the sender to send, before waiting for an authorization\n\t\t\t\t * to continue transmission of the following N_PDUs */\n}fl_param;\n\n/* --- N rslts (ref: iso15765-2 p.10-11) ----------------------------------- */\n\ntypedef enum\n{\n\tN_OK \t\t= 0x0000, /* service execution has completed successfully */\n\tN_TIMEOUT_A \t= 0x0001, /* when the timer N_Ar/N_As has passed its time-out\n\t\t\t\t   * value N_Asmax / N_Armax; */\n\tN_TIMEOUT_Bs \t= 0x0002, /* when the timer N_Bs has passed its time-out value\n\t\t\t\t   * N_Bsmax */\n\tN_TIMEOUT_Cr \t= 0x0004, /* when the timer N_Cr has passed its time-out value\n\t\t\t\t   * N_Crmax */\n\tN_WRG_SN \t= 0x0008, /* upon reception of an unexpected sequence number\n\t\t\t\t   * (PCI.SN) value */\n\tN_INV_FS \t= 0x0010, /* when an invalid or unknown FlowStatus value has\n\t\t\t\t   * been received in a flow control(FC) N_PDU */\n\tN_UNE_PDU \t= 0x0020, /* upon reception of an unexpected protocol data unit; */\n\tN_WFT_OVRN \t= 0x0040, /* upon reception of flow control WAIT frame that\n\t\t\t\t   * exceeds the maximum counter N_WFTmax */\n\tN_BUFFER_OVFLW \t= 0x0080, /* upon reception of a flow control (FC) N_PDU with\n\t\t\t\t   * FlowStatus = OVFLW */\n\tN_ERROR \t= 0x0100, /* when an error has been detected by the network layer\n\t\t\t\t   * and no other parameter value can be used to better\n\t\t\t\t   * describe the error. */\n\tN_IDLE \t\t= 0x0110, /* service execution has completed successfully and not TXRX */\n\tN_TX_BUSY \t= 0x0120, /* service execution has completed successfully and TX pending */\n\tN_RX_BUSY \t= 0x0140, /* service execution has completed successfully and RX pending */\n\tN_WRG_PARAM \t= 0x1003, /* the service did not execute due to an undefined <Parameter> */\n\tN_WRG_VALUE \t= 0x1004, /* the service did not execute due to an out of range <Parameter_Value>; */\n\tN_INV \t\t= 0x1005, /* the service did not execute due to an invalid requested operation */\n\tN_MISSING_CLB \t= 0x1006, /* the service did not execute due to missing mandatory callback functions */\n\tN_NULL \t\t= 0xFFFF, /* the service has a null pointer (this should never happen) */\n\tN_FC_TIMEOUT \t= 0x1001, /* Flow Control Timeout. @n_timeouts.n_bs */\n\tN_CF_TIMEOUT \t= 0x1002, /* ConsecutiveFrame Timeout. @n_timeouts.n_cr */\n\tN_CAN_DT_INV \t= 0x1003, /* Invalid CAN Data Error */\n\tN_UNE_FC \t= 0x1004, /* FlowControl was received without being expected. */\n\tN_UNE_CF \t= 0x1005, /* ConsecutiveFrame was received without being expected. */\n\tN_RCV_INT_SF \t= 0x1006, /* Frames sequence was not correct. Service received a\n\t\t\t\t   * single frame during a Multi-frame reception. */\n\tN_RCV_INT_FF \t= 0x1007, /* Frames sequence was not correct. Service received a\n\t\t\t\t   * first frame during a Multi-frame reception. */\n\tN_RCV_INT_FC \t= 0x1012, /* ReceptionInterruptedWithFC */\n\tN_INV_SEQ_NUM \t= 0x1008, /* ConsecutiveFrame sequence number was invalid. */\n\tN_UNS_WF \t= 0x1009, /* Unsuported WaitFrame */\n\tN_MAX_WF_REAC \t= 0x100A, /* Maximum Wait Frames Reached */\n\tN_INV_PDU \t= 0x1010, /* PDU Conversion failed */\n\tN_INV_REQ_SZ \t= 0x010E, /* Invalid Request Size */\n\tN_UNE_FC_STS \t= 0x1011, /* Flow control status was invalid and/or unexpected. */\n\tN_OVFLW \t= 0x100F, /* Buffer Overflow. */\n}n_rslt;\n\n/* --- N_PCI_T_FC Status (ref: iso15765-2 p.8) ---------------------------- */\n\ntypedef enum\n{\n\tN_CONTINUE = 0x00U,\t/* continue to send, the authorization to continue */\n\tN_WAIT \t   = 0x01U,\t/* the request to continue to wait */\n\tN_OVERFLOW = 0x02U\t/* buffer overflow, the indication that the number of bytes specified\n\t\t\t\t * in the FirstFrame of the segmented message exceeds the number of\n\t\t\t\t * bytes that can be stored in the buffer of the receiver entity */\n}flow_sts;\n\n/* --- dt I/O Stream Status (ref: iso15765-2 p.8) ------------------------ */\n\ntypedef enum\n{\n\tN_S_IDLE \t= 0x00, /* Has to pending action */\n\tN_S_RX_BUSY \t= 0x02, /* Reception is in progress */\n\tN_S_TX_BUSY \t= 0x04, /* Transmission is in progress */\n\tN_S_TX_READY \t= 0x05, /* Transmission is ready to begin */\n\tN_S_TX_WAIT_FC \t= 0x10, /* Transmission is in progress and waits\n\t\t\t\t\t\t\t * for a Flow control frame */\n}stream_sts;\n\n/* --- Network Layer Timing PARAMs (ref: iso15765-2 p.) -------------------- */\n\ntypedef struct ALIGNMENT\n{\n\tuint32_t n_bs;\t/* FlowControl N_PDU not received (lost, overwritten) on the\n\t\t\t * sender side or preceding FirstFrame N_PDU or\tConsecutiveFrame\n\t\t\t * N_PDU not received(lost, overwritten) on the receiver side.\n\t\t\t * Abort message transmission and issue N_USData.confirm with\n\t\t\t * <N_Result> = N_TIMEOUT_Bs. */\n\tuint32_t n_cr;\t/* ConsecutiveFrame N_PDU not received (lost, overwritten) on\n\t\t\t * the receiver side or preceding FC N_PDU not received(lost,\n\t\t\t * overwritten) on the sender side. Abort message reception and issue\n\t\t\t * N_USData.indication with <N_Result> = N_TIMEOUT_Cr */\n\tuint32_t n_cs;\n}n_timeouts;\n\n/* --- Address information (ref: iso15765-2 p.) ---------------------------- */\n\ntypedef struct ALIGNMENT\n{\n\tuint8_t n_pr;\t\t/* Network Address Priority */\n\tuint8_t n_sa;\t\t/* Network Source Address */\n\tuint8_t n_ta;\t\t/* Network Target Address */\n\tuint8_t n_ae;\t\t/* Network Address Extension */\n\tta_type n_tt;\t\t/* Network Target Address type */\n}n_ai_t;\n\n/* --- Protocol control information (ref: iso15765-2 p.) ------------------- */\n\ntypedef struct ALIGNMENT\n{\n\tuint8_t fs;\t/* FlowStatus: whether the sending network entity can\n\t\t\t * proceed with the message transmission 'flow_sts' */\n\tuint8_t bs;\t/* BlockSize */\n\tuint8_t sn;\t/* SequenceNumber: specify the order of the consecutive frames */\n\tuint8_t st;\t/* SeparationTime: Requested separation time */\n\tpci_type pt;\t/* Type of the received pdu 'pci_type' */\n\tuint16_t dl;\t/* PCI data length (if 0 frame must be ignored) */\n}n_pci_t;\n\n/* --- Protocol dt unit (ref: iso15765-2 p.) ------------------------------- */\n\ntypedef struct ALIGNMENT\n{\n\tmtype n_mt;\t/* Message Type */\n\tuint16_t sz;\t/* Actual data size */\n\tn_ai_t n_ai;\t/* Address information */\n\tn_pci_t n_pci;\t/* Protocol control information */\n\tuint8_t dt[64];\t/* Data Field */\n} n_pdu_t;\n\n/* --- N_USdt.cfm (ref: iso15765-2 p.6) ------------------------------------ */\n\ntypedef struct ALIGNMENT\n{\n\tn_ai_t n_ai;\t/* Address information */\n\tn_pci_t n_pci;\t/* Protocol control information */\n\tn_rslt rslt;\t/* Result of the request */\n}n_cfm_t;\n\n/* --- N_FF.indn (ref: iso15765-2 p.6) ------------------------------------ */\n\ntypedef struct ALIGNMENT\n{\n\tcbus_fr_format fr_fmt;\t/* CANBus Frame format */\n\tn_ai_t n_ai;\t\t/* Address information */\n\tn_pci_t n_pci;\t\t/* Protocol control information */\n\tuint16_t msg_sz;\t/* Size of the message that will be received */\n}n_ff_indn_t;\n\n/* --- N_USData.request (ref: iso15765-2 p6.) ------------------------------ */\n\ntypedef struct ALIGNMENT\n{\n\tcbus_fr_format fr_fmt;\t\t/* CANBus Frame format */\n\tn_ai_t n_ai;\t\t\t/* Address information */\n\tn_pci_t n_pci;\t\t\t/* Protocol control information */\n\tuint16_t msg_sz;\t\t/* Message actual size */\n\tuint8_t msg[I15765_MSG_SIZE];\t/* Message to be transmitted */\n}n_req_t;\n\n/* --- N_USdt.indn (ref: iso15765-2 p.7) ---------------------------------- */\n\ntypedef struct ALIGNMENT\n{\n\tcbus_fr_format fr_fmt;\t\t/* CANBus Frame format */\n\tn_ai_t n_ai;\t\t\t/* Address information */\n\tn_pci_t n_pci;\t\t\t/* Protocol control information */\n\tn_rslt rslt;\t\t\t/* Result of the reception */\n\tuint16_t msg_sz;\t\t/* Received message actual size */\n\tuint8_t msg[I15765_MSG_SIZE];\t/* Received message data */\n}n_indn_t;\n\ntypedef struct ALIGNMENT\n{\n\tn_ai_t n_ai;\t\t/* Address information. Not supported:\n\t\t\t\t * the lib handles only 1 stream */\n\tn_pci_t n_pci;\t\t/* Protocol control information. Not supported:\n\t\t\t\t * the lib handles only 1 stream) */\n\tfl_param param;\t\t/* Parameter to change */\n\tuint8_t pval;\t\t/* Value to set */\n}n_chg_param_req_t;\n\ntypedef struct ALIGNMENT\n{\n\tn_ai_t n_ai;\t\t/* Address information. Not supported:\n\t\t\t\t * the lib handles only 1 stream */\n\tn_pci_t n_pci;\t\t/* Protocol control information. Not\n\t\t\t\t * supported: because the lib handles only 1 stream */\n\tfl_param param;\t\t/* Requested Parameter to change */\n\tuint8_t pval;\t\t/* Requested Value to set */\n\tn_rslt rslt;\t\t/* Result of the request */\n}n_chg_param_cfm_t;\n\n\ntypedef enum\n{\n\tN_INDN = 0x01,\t\t/* N_USData.indication */\n\tN_FF_INDN = 0x02,\t/* N_USData_FF.indication */\n\tN_CONF = 0x03,\t\t/* N_USData.confirm */\n\tN_CHG_P_CONF = 0x04\t/* N_ChangeParameter.confirm */\n}signal_tp;\n\n/* --- Callbacks  ---------------------------------------------------------- */\n\ntypedef struct ALIGNMENT\n{\n\tvoid (*indn)(n_indn_t*);\t\t\t/* Indication Callback: Will be fired when a reception\n\t\t\t\t\t\t\t * is available or an error occured during the reception. */\n\tvoid (*ff_indn)(n_ff_indn_t*);\t\t\t/* First Frame Indication Callback: Will be fired when a\n\t\t\t\t\t\t\t * FF is received, giving back some useful information */\n\tvoid (*cfm)(n_cfm_t*);\t\t\t\t/* This callback confirms to the higher layers that the requested\n\t\t\t\t\t\t\t * service has been carried out */\n\tvoid (*cfg_cfm)(n_chg_param_cfm_t*);\t\t/* This service confirms to the upper layer that the request to\n\t\t\t\t\t\t\t * change a specific protocol has been carried out */\n\tvoid (*pdu_custom_pack)(n_pdu_t*, uint32_t*);\t/* Custom CAN ID packing for 11bits ID. If assinged the default\n\t\t\t\t\t\t\t * packing will be skipped */\n\tvoid (*pdu_custom_unpack)(n_pdu_t*, uint32_t*);\t/* Custom CAN ID uppacking for 11bits ID. If assinged the default\n\t\t\t\t\t\t\t * uppacking will be skipped */\n\tvoid (*on_error)(n_rslt);\t\t\t/* Will be fired in any occured error. */\n\tuint32_t(*get_ms)();\t\t\t\t/* Time-source for the library in ms(required) */\n\tuint8_t(*send_frame)\t\t\t\t/* Callback to assing the Network Layer. This callback */\n\t\t(\t\t\t\t\t/* will be fired when a transmission of a canbus frame is ready. */\n\t\tcbus_id_type,\t\t\t\t/* - CANBus Frame ID Type [Standard or Extended] */\n\t\tuint32_t,\t\t\t\t/* - Frame ID */\n\t\tcbus_fr_format,\t\t\t\t/* - Frame Type: [CLASSIC or FD]*/\n\t\tuint8_t,\t\t\t\t/* - Frame Data Length */\n\t\tuint8_t*\t\t\t\t/* - Frame Data Array */\n\t\t);\t\t\t\t\t\t\t\t\t\t\n}n_callbacks_t;\n\n/* --- PDU Stream  --------------------------------------------------------- */\n\ntypedef struct ALIGNMENT\n{\n\tcbus_fr_format fr_fmt;\t\t/* CANBus Frame format */\n\tn_pdu_t pdu;\t\t\t/* Keep information about the in/out frame in a PDU format */\n\tuint8_t cf_cnt;\t\t\t/* Current block sequence number (ConsecutiveFrame) */\n\tuint8_t wf_cnt;\t\t\t/* Current received wait flow control frames */\n\tuint8_t sn_glb;\t\t\t/* Current Sequence Number of the transmittion */\n\tuint8_t cfg_wf;\t\t\t/* Max supported Wait Flow Control frames */\n\tuint8_t stmin;\t\t\t/* Frames transmission rate */\n\tuint8_t cfg_bs;\t\t\t/* Max. supported block sequence (ConsecutiveFrame) */\n\tstream_sts sts;\t\t\t/* Stream status */\n\tuint16_t msg_sz;\t\t/* Actual message buffer size */\n\tuint16_t msg_pos;\t\t/* Transmit message buffer position */\n\tn_timeouts last_upd;\t\t/* Time keeper for timouts */\n\tuint8_t msg[I15765_MSG_SIZE];\t/* Received/Transmit message buffer */\n}n_iostream_t;\n\n/* --- iso15765 timing configuration (ref: iso15765-2 p.25)----------------- */\n\ntypedef struct ALIGNMENT\n{\n\tuint8_t stmin;\t\t\t/* Default min. frame transmission separation */\n\tuint8_t bs;\t\t\t/* Max. Block size during transmission */\n\tuint8_t wf;\t\t\t/* Max. accepted Wait Requests from the FlowControl */\n\tuint16_t n_bs;\t\t\t/* Time until reception of the next FlowControl N_PDU */\n\tuint16_t n_cr;\t\t\t/* Time until reception of the next ConsecutiveFrame N_PDU */\n}n_config_t;\n\n/* --- iso15765 Handler  --------------------------------------------------- */\n\ntypedef struct ALIGNMENT\n{\n\tn_rslt init_sts; \t\t/* Instance is initialized correctly */\n\taddr_md addr_md;\t\t/* Selected address mode of the TP */\n\tcbus_id_type fr_id_type;\t/* CANBus frame Id Type */\n\tn_iostream_t in;\t\t/* Incoming data stream (reception) */\n\tn_iostream_t out;\t\t/* Outcoming data stream (transmission) */\n\tn_pdu_t fl_pdu;\t\t\t/* Flow control pdu */\n\tn_callbacks_t clbs;\t\t/* Callbacks */\n\tn_config_t config;\t\t/* Default configuration to be used. (timing etc) */\n\tn_timeouts cfg_timeout;\t\t/* Timeouts configuration */\n\tiqueue_t inqueue;\t\t/* Queue handler for the incoming canbus frames */\n\tuint8_t inq_buf[I15765_QUEUE_ELMS * sizeof(canbus_frame_t)]; /* Queue buffer */\n}iso15765_t;\n\n/******************************************************************************\n* Declaration | Public Functions\n******************************************************************************/\n\nn_rslt iso15765_init(iso15765_t* instance);\n\nn_rslt iso15765_send(iso15765_t* instance, n_req_t* frame);\n\nn_rslt iso15765_enqueue(iso15765_t* instance, canbus_frame_t* frame);\n\nn_rslt iso15765_process(iso15765_t* instance);\n\n/******************************************************************************\n* EOF - NO CODE AFTER THIS LINE\n******************************************************************************/\n#endif\n"
  }
]