diff --git a/lib/sml_parser/.clang-format b/lib/sml_parser/.clang-format new file mode 100644 index 0000000..1ef3556 --- /dev/null +++ b/lib/sml_parser/.clang-format @@ -0,0 +1 @@ +BreakBeforeBraces: Stroustrup diff --git a/lib/sml_parser/.github/workflows/native.yml b/lib/sml_parser/.github/workflows/native.yml new file mode 100644 index 0000000..2485628 --- /dev/null +++ b/lib/sml_parser/.github/workflows/native.yml @@ -0,0 +1,59 @@ +name: Test + +on: [push, pull_request] + +jobs: + test: + runs-on: ubuntu-latest + name: Test library + steps: + - uses: actions/checkout@v2 + - name: Cache pip + uses: actions/cache@v2 + with: + path: ~/.cache/pip + key: ${{ runner.os }}-pip-${{ hashFiles('**/requirements.txt') }} + restore-keys: ${{ runner.os }}-pip- + - name: Cache PlatformIO + uses: actions/cache@v2 + with: + path: ~/.platformio + key: ${{ runner.os }}-${{ hashFiles('**/lockfiles') }} + - name: Set up Python + uses: actions/setup-python@v2 + - name: Install PlatformIO + run: | + python -m pip install --upgrade pip + pip install platformio + - name: Run tests on the native platform + run: | + cd test + platformio test -e native + publish: + needs: test + if: github.ref == 'refs/heads/main' && github.repository_owner == 'olliiiver' + name: Publish Package + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - name: Cache pip + uses: actions/cache@v2 + with: + path: ~/.cache/pip + key: ${{ runner.os }}-pip-${{ hashFiles('**/requirements.txt') }} + restore-keys: ${{ runner.os }}-pip- + - name: Cache PlatformIO + uses: actions/cache@v2 + with: + path: ~/.platformio + key: ${{ runner.os }}-${{ hashFiles('**/lockfiles') }} + - name: Set up Python + uses: actions/setup-python@v2 + - name: Install PlatformIO + run: | + python -m pip install --upgrade pip + pip install --upgrade platformio + - name: Publish package + env: + PLATFORMIO_AUTH_TOKEN: ${{ secrets.PIO_TOKEN }} + run: pio package publish --non-interactive diff --git a/lib/sml_parser/.gitignore b/lib/sml_parser/.gitignore new file mode 100644 index 0000000..d9b86c1 --- /dev/null +++ b/lib/sml_parser/.gitignore @@ -0,0 +1,7 @@ +.pio/ +lib/ +.vscode/.browse.c_cpp.db* +.vscode/c_cpp_properties.json +.vscode/launch.json +.vscode/ipch +.DS_Store diff --git a/lib/sml_parser/.vscode/settings.json b/lib/sml_parser/.vscode/settings.json new file mode 100644 index 0000000..f89ed5f --- /dev/null +++ b/lib/sml_parser/.vscode/settings.json @@ -0,0 +1,3 @@ +{ + "editor.formatOnSave": true +} diff --git a/lib/sml_parser/LICENSE b/lib/sml_parser/LICENSE new file mode 100644 index 0000000..8000a6f --- /dev/null +++ b/lib/sml_parser/LICENSE @@ -0,0 +1,504 @@ + GNU LESSER GENERAL PUBLIC LICENSE + Version 2.1, February 1999 + + Copyright (C) 1991, 1999 Free Software Foundation, Inc. + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + +[This is the first released version of the Lesser GPL. It also counts + as the successor of the GNU Library Public License, version 2, hence + the version number 2.1.] + + Preamble + + The licenses for most software are designed to take away your +freedom to share and change it. By contrast, the GNU General Public +Licenses are intended to guarantee your freedom to share and change +free software--to make sure the software is free for all its users. + + This license, the Lesser General Public License, applies to some +specially designated software packages--typically libraries--of the +Free Software Foundation and other authors who decide to use it. You +can use it too, but we suggest you first think carefully about whether +this license or the ordinary General Public License is the better +strategy to use in any particular case, based on the explanations below. + + When we speak of free software, we are referring to freedom of use, +not price. Our General Public Licenses are designed to make sure that +you have the freedom to distribute copies of free software (and charge +for this service if you wish); that you receive source code or can get +it if you want it; that you can change the software and use pieces of +it in new free programs; and that you are informed that you can do +these things. + + To protect your rights, we need to make restrictions that forbid +distributors to deny you these rights or to ask you to surrender these +rights. These restrictions translate to certain responsibilities for +you if you distribute copies of the library or if you modify it. + + For example, if you distribute copies of the library, whether gratis +or for a fee, you must give the recipients all the rights that we gave +you. You must make sure that they, too, receive or can get the source +code. If you link other code with the library, you must provide +complete object files to the recipients, so that they can relink them +with the library after making changes to the library and recompiling +it. And you must show them these terms so they know their rights. + + We protect your rights with a two-step method: (1) we copyright the +library, and (2) we offer you this license, which gives you legal +permission to copy, distribute and/or modify the library. + + To protect each distributor, we want to make it very clear that +there is no warranty for the free library. Also, if the library is +modified by someone else and passed on, the recipients should know +that what they have is not the original version, so that the original +author's reputation will not be affected by problems that might be +introduced by others. + + Finally, software patents pose a constant threat to the existence of +any free program. We wish to make sure that a company cannot +effectively restrict the users of a free program by obtaining a +restrictive license from a patent holder. Therefore, we insist that +any patent license obtained for a version of the library must be +consistent with the full freedom of use specified in this license. + + Most GNU software, including some libraries, is covered by the +ordinary GNU General Public License. This license, the GNU Lesser +General Public License, applies to certain designated libraries, and +is quite different from the ordinary General Public License. We use +this license for certain libraries in order to permit linking those +libraries into non-free programs. + + When a program is linked with a library, whether statically or using +a shared library, the combination of the two is legally speaking a +combined work, a derivative of the original library. The ordinary +General Public License therefore permits such linking only if the +entire combination fits its criteria of freedom. The Lesser General +Public License permits more lax criteria for linking other code with +the library. + + We call this license the "Lesser" General Public License because it +does Less to protect the user's freedom than the ordinary General +Public License. It also provides other free software developers Less +of an advantage over competing non-free programs. These disadvantages +are the reason we use the ordinary General Public License for many +libraries. However, the Lesser license provides advantages in certain +special circumstances. + + For example, on rare occasions, there may be a special need to +encourage the widest possible use of a certain library, so that it becomes +a de-facto standard. To achieve this, non-free programs must be +allowed to use the library. A more frequent case is that a free +library does the same job as widely used non-free libraries. In this +case, there is little to gain by limiting the free library to free +software only, so we use the Lesser General Public License. + + In other cases, permission to use a particular library in non-free +programs enables a greater number of people to use a large body of +free software. For example, permission to use the GNU C Library in +non-free programs enables many more people to use the whole GNU +operating system, as well as its variant, the GNU/Linux operating +system. + + Although the Lesser General Public License is Less protective of the +users' freedom, it does ensure that the user of a program that is +linked with the Library has the freedom and the wherewithal to run +that program using a modified version of the Library. + + The precise terms and conditions for copying, distribution and +modification follow. Pay close attention to the difference between a +"work based on the library" and a "work that uses the library". The +former contains code derived from the library, whereas the latter must +be combined with the library in order to run. + + GNU LESSER GENERAL PUBLIC LICENSE + TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION + + 0. This License Agreement applies to any software library or other +program which contains a notice placed by the copyright holder or +other authorized party saying it may be distributed under the terms of +this Lesser General Public License (also called "this License"). +Each licensee is addressed as "you". + + A "library" means a collection of software functions and/or data +prepared so as to be conveniently linked with application programs +(which use some of those functions and data) to form executables. + + The "Library", below, refers to any such software library or work +which has been distributed under these terms. A "work based on the +Library" means either the Library or any derivative work under +copyright law: that is to say, a work containing the Library or a +portion of it, either verbatim or with modifications and/or translated +straightforwardly into another language. (Hereinafter, translation is +included without limitation in the term "modification".) + + "Source code" for a work means the preferred form of the work for +making modifications to it. For a library, complete source code means +all the source code for all modules it contains, plus any associated +interface definition files, plus the scripts used to control compilation +and installation of the library. + + Activities other than copying, distribution and modification are not +covered by this License; they are outside its scope. The act of +running a program using the Library is not restricted, and output from +such a program is covered only if its contents constitute a work based +on the Library (independent of the use of the Library in a tool for +writing it). Whether that is true depends on what the Library does +and what the program that uses the Library does. + + 1. You may copy and distribute verbatim copies of the Library's +complete source code as you receive it, in any medium, provided that +you conspicuously and appropriately publish on each copy an +appropriate copyright notice and disclaimer of warranty; keep intact +all the notices that refer to this License and to the absence of any +warranty; and distribute a copy of this License along with the +Library. + + You may charge a fee for the physical act of transferring a copy, +and you may at your option offer warranty protection in exchange for a +fee. + + 2. You may modify your copy or copies of the Library or any portion +of it, thus forming a work based on the Library, and copy and +distribute such modifications or work under the terms of Section 1 +above, provided that you also meet all of these conditions: + + a) The modified work must itself be a software library. + + b) You must cause the files modified to carry prominent notices + stating that you changed the files and the date of any change. + + c) You must cause the whole of the work to be licensed at no + charge to all third parties under the terms of this License. + + d) If a facility in the modified Library refers to a function or a + table of data to be supplied by an application program that uses + the facility, other than as an argument passed when the facility + is invoked, then you must make a good faith effort to ensure that, + in the event an application does not supply such function or + table, the facility still operates, and performs whatever part of + its purpose remains meaningful. + + (For example, a function in a library to compute square roots has + a purpose that is entirely well-defined independent of the + application. Therefore, Subsection 2d requires that any + application-supplied function or table used by this function must + be optional: if the application does not supply it, the square + root function must still compute square roots.) + +These requirements apply to the modified work as a whole. If +identifiable sections of that work are not derived from the Library, +and can be reasonably considered independent and separate works in +themselves, then this License, and its terms, do not apply to those +sections when you distribute them as separate works. But when you +distribute the same sections as part of a whole which is a work based +on the Library, the distribution of the whole must be on the terms of +this License, whose permissions for other licensees extend to the +entire whole, and thus to each and every part regardless of who wrote +it. + +Thus, it is not the intent of this section to claim rights or contest +your rights to work written entirely by you; rather, the intent is to +exercise the right to control the distribution of derivative or +collective works based on the Library. + +In addition, mere aggregation of another work not based on the Library +with the Library (or with a work based on the Library) on a volume of +a storage or distribution medium does not bring the other work under +the scope of this License. + + 3. You may opt to apply the terms of the ordinary GNU General Public +License instead of this License to a given copy of the Library. To do +this, you must alter all the notices that refer to this License, so +that they refer to the ordinary GNU General Public License, version 2, +instead of to this License. (If a newer version than version 2 of the +ordinary GNU General Public License has appeared, then you can specify +that version instead if you wish.) Do not make any other change in +these notices. + + Once this change is made in a given copy, it is irreversible for +that copy, so the ordinary GNU General Public License applies to all +subsequent copies and derivative works made from that copy. + + This option is useful when you wish to copy part of the code of +the Library into a program that is not a library. + + 4. You may copy and distribute the Library (or a portion or +derivative of it, under Section 2) in object code or executable form +under the terms of Sections 1 and 2 above provided that you accompany +it with the complete corresponding machine-readable source code, which +must be distributed under the terms of Sections 1 and 2 above on a +medium customarily used for software interchange. + + If distribution of object code is made by offering access to copy +from a designated place, then offering equivalent access to copy the +source code from the same place satisfies the requirement to +distribute the source code, even though third parties are not +compelled to copy the source along with the object code. + + 5. A program that contains no derivative of any portion of the +Library, but is designed to work with the Library by being compiled or +linked with it, is called a "work that uses the Library". Such a +work, in isolation, is not a derivative work of the Library, and +therefore falls outside the scope of this License. + + However, linking a "work that uses the Library" with the Library +creates an executable that is a derivative of the Library (because it +contains portions of the Library), rather than a "work that uses the +library". The executable is therefore covered by this License. +Section 6 states terms for distribution of such executables. + + When a "work that uses the Library" uses material from a header file +that is part of the Library, the object code for the work may be a +derivative work of the Library even though the source code is not. +Whether this is true is especially significant if the work can be +linked without the Library, or if the work is itself a library. The +threshold for this to be true is not precisely defined by law. + + If such an object file uses only numerical parameters, data +structure layouts and accessors, and small macros and small inline +functions (ten lines or less in length), then the use of the object +file is unrestricted, regardless of whether it is legally a derivative +work. (Executables containing this object code plus portions of the +Library will still fall under Section 6.) + + Otherwise, if the work is a derivative of the Library, you may +distribute the object code for the work under the terms of Section 6. +Any executables containing that work also fall under Section 6, +whether or not they are linked directly with the Library itself. + + 6. As an exception to the Sections above, you may also combine or +link a "work that uses the Library" with the Library to produce a +work containing portions of the Library, and distribute that work +under terms of your choice, provided that the terms permit +modification of the work for the customer's own use and reverse +engineering for debugging such modifications. + + You must give prominent notice with each copy of the work that the +Library is used in it and that the Library and its use are covered by +this License. You must supply a copy of this License. If the work +during execution displays copyright notices, you must include the +copyright notice for the Library among them, as well as a reference +directing the user to the copy of this License. Also, you must do one +of these things: + + a) Accompany the work with the complete corresponding + machine-readable source code for the Library including whatever + changes were used in the work (which must be distributed under + Sections 1 and 2 above); and, if the work is an executable linked + with the Library, with the complete machine-readable "work that + uses the Library", as object code and/or source code, so that the + user can modify the Library and then relink to produce a modified + executable containing the modified Library. (It is understood + that the user who changes the contents of definitions files in the + Library will not necessarily be able to recompile the application + to use the modified definitions.) + + b) Use a suitable shared library mechanism for linking with the + Library. A suitable mechanism is one that (1) uses at run time a + copy of the library already present on the user's computer system, + rather than copying library functions into the executable, and (2) + will operate properly with a modified version of the library, if + the user installs one, as long as the modified version is + interface-compatible with the version that the work was made with. + + c) Accompany the work with a written offer, valid for at + least three years, to give the same user the materials + specified in Subsection 6a, above, for a charge no more + than the cost of performing this distribution. + + d) If distribution of the work is made by offering access to copy + from a designated place, offer equivalent access to copy the above + specified materials from the same place. + + e) Verify that the user has already received a copy of these + materials or that you have already sent this user a copy. + + For an executable, the required form of the "work that uses the +Library" must include any data and utility programs needed for +reproducing the executable from it. However, as a special exception, +the materials to be distributed need not include anything that is +normally distributed (in either source or binary form) with the major +components (compiler, kernel, and so on) of the operating system on +which the executable runs, unless that component itself accompanies +the executable. + + It may happen that this requirement contradicts the license +restrictions of other proprietary libraries that do not normally +accompany the operating system. Such a contradiction means you cannot +use both them and the Library together in an executable that you +distribute. + + 7. You may place library facilities that are a work based on the +Library side-by-side in a single library together with other library +facilities not covered by this License, and distribute such a combined +library, provided that the separate distribution of the work based on +the Library and of the other library facilities is otherwise +permitted, and provided that you do these two things: + + a) Accompany the combined library with a copy of the same work + based on the Library, uncombined with any other library + facilities. This must be distributed under the terms of the + Sections above. + + b) Give prominent notice with the combined library of the fact + that part of it is a work based on the Library, and explaining + where to find the accompanying uncombined form of the same work. + + 8. You may not copy, modify, sublicense, link with, or distribute +the Library except as expressly provided under this License. Any +attempt otherwise to copy, modify, sublicense, link with, or +distribute the Library is void, and will automatically terminate your +rights under this License. However, parties who have received copies, +or rights, from you under this License will not have their licenses +terminated so long as such parties remain in full compliance. + + 9. You are not required to accept this License, since you have not +signed it. However, nothing else grants you permission to modify or +distribute the Library or its derivative works. These actions are +prohibited by law if you do not accept this License. Therefore, by +modifying or distributing the Library (or any work based on the +Library), you indicate your acceptance of this License to do so, and +all its terms and conditions for copying, distributing or modifying +the Library or works based on it. + + 10. Each time you redistribute the Library (or any work based on the +Library), the recipient automatically receives a license from the +original licensor to copy, distribute, link with or modify the Library +subject to these terms and conditions. You may not impose any further +restrictions on the recipients' exercise of the rights granted herein. +You are not responsible for enforcing compliance by third parties with +this License. + + 11. If, as a consequence of a court judgment or allegation of patent +infringement or for any other reason (not limited to patent issues), +conditions are imposed on you (whether by court order, agreement or +otherwise) that contradict the conditions of this License, they do not +excuse you from the conditions of this License. If you cannot +distribute so as to satisfy simultaneously your obligations under this +License and any other pertinent obligations, then as a consequence you +may not distribute the Library at all. For example, if a patent +license would not permit royalty-free redistribution of the Library by +all those who receive copies directly or indirectly through you, then +the only way you could satisfy both it and this License would be to +refrain entirely from distribution of the Library. + +If any portion of this section is held invalid or unenforceable under any +particular circumstance, the balance of the section is intended to apply, +and the section as a whole is intended to apply in other circumstances. + +It is not the purpose of this section to induce you to infringe any +patents or other property right claims or to contest validity of any +such claims; this section has the sole purpose of protecting the +integrity of the free software distribution system which is +implemented by public license practices. Many people have made +generous contributions to the wide range of software distributed +through that system in reliance on consistent application of that +system; it is up to the author/donor to decide if he or she is willing +to distribute software through any other system and a licensee cannot +impose that choice. + +This section is intended to make thoroughly clear what is believed to +be a consequence of the rest of this License. + + 12. If the distribution and/or use of the Library is restricted in +certain countries either by patents or by copyrighted interfaces, the +original copyright holder who places the Library under this License may add +an explicit geographical distribution limitation excluding those countries, +so that distribution is permitted only in or among countries not thus +excluded. In such case, this License incorporates the limitation as if +written in the body of this License. + + 13. The Free Software Foundation may publish revised and/or new +versions of the Lesser General Public License from time to time. +Such new versions will be similar in spirit to the present version, +but may differ in detail to address new problems or concerns. + +Each version is given a distinguishing version number. If the Library +specifies a version number of this License which applies to it and +"any later version", you have the option of following the terms and +conditions either of that version or of any later version published by +the Free Software Foundation. If the Library does not specify a +license version number, you may choose any version ever published by +the Free Software Foundation. + + 14. If you wish to incorporate parts of the Library into other free +programs whose distribution conditions are incompatible with these, +write to the author to ask for permission. For software which is +copyrighted by the Free Software Foundation, write to the Free +Software Foundation; we sometimes make exceptions for this. Our +decision will be guided by the two goals of preserving the free status +of all derivatives of our free software and of promoting the sharing +and reuse of software generally. + + NO WARRANTY + + 15. BECAUSE THE LIBRARY IS LICENSED FREE OF CHARGE, THERE IS NO +WARRANTY FOR THE LIBRARY, TO THE EXTENT PERMITTED BY APPLICABLE LAW. +EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR +OTHER PARTIES PROVIDE THE LIBRARY "AS IS" WITHOUT WARRANTY OF ANY +KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR +PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE +LIBRARY IS WITH YOU. SHOULD THE LIBRARY PROVE DEFECTIVE, YOU ASSUME +THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. + + 16. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN +WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY +AND/OR REDISTRIBUTE THE LIBRARY AS PERMITTED ABOVE, BE LIABLE TO YOU +FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR +CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE +LIBRARY (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING +RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A +FAILURE OF THE LIBRARY TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF +SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH +DAMAGES. + + END OF TERMS AND CONDITIONS + + How to Apply These Terms to Your New Libraries + + If you develop a new library, and you want it to be of the greatest +possible use to the public, we recommend making it free software that +everyone can redistribute and change. You can do so by permitting +redistribution under these terms (or, alternatively, under the terms of the +ordinary General Public License). + + To apply these terms, attach the following notices to the library. It is +safest to attach them to the start of each source file to most effectively +convey the exclusion of warranty; and each file should have at least the +"copyright" line and a pointer to where the full notice is found. + + + Copyright (C) + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 + USA + +Also add information on how to contact you by electronic and paper mail. + +You should also get your employer (if you work as a programmer) or your +school, if any, to sign a "copyright disclaimer" for the library, if +necessary. Here is a sample; alter the names: + + Yoyodyne, Inc., hereby disclaims all copyright interest in the + library `Frob' (a library for tweaking knobs) written by James Random + Hacker. + + , 1 April 1990 + Ty Coon, President of Vice + +That's all there is to it! diff --git a/lib/sml_parser/README.md b/lib/sml_parser/README.md new file mode 100644 index 0000000..00454fa --- /dev/null +++ b/lib/sml_parser/README.md @@ -0,0 +1,232 @@ +[![PlatformIO Registry](https://badges.registry.platformio.org/packages/olliiiver/library/SML%20Parser.svg)](https://registry.platformio.org/libraries/olliiiver/SML%20Parser) [![Test](https://github.com/olliiiver/sml_parser/workflows/Test/badge.svg)](https://github.com/olliiiver/sml_parser/actions) + +# Smart Message Language (SML) parser + +Easy to use C library with a low memory footprint to parse SML messages (based on BSI TR-03109-1) byte by byte from smart meters. It's designed to be lightweight and efficient and has a small memory footprint, making it suitable for use on embedded systems or other devices with limited memory resources. + +The library will control the last CRC value to check if the received data is correct. On any error, the parser will reset and wait for valid data. This allows to start parsing at any time. For example, a half received message is discarded. The library allows you to register handlers to process the received information. This feature allows you to easily process the data in your application without having to handle the low-level details of parsing the SML messages. + +It's actively maintained and has already been used in various projects with meters from EMH, EFR, EasyMeter, etc. + +## Examples + +| Directory | Description | +| ------------------------------------------------------ | -------------------------------------------------------------- | +| [arduino](examples/arduino/) | Loops through static data and outputs debug messages to serial | +| [arduino_serial](examples/arduino_serial/) | Reads data from Arduino Pin 8 and outputs debug to serial | +| [esp32_lora](examples/esp32_lora/) | Forward energy usage to LoraWAN (The Things Network) | +| [esp32_m5stack_sender](examples/esp32_m5stack_sender/) | Use m5stack to produce a message for testing | +| [esp32_receiver](examples/esp32_receiver/) | Receive messages and show infos on a display | +| [native](examples/native/) | Test library locally | + +The easiest way to test the library would be over the [native](examples/native/) example. + +## Example usage + +```cpp +void Manufacturer() { + smlOBISManufacturer(manuf, MAX_STR_MANUF); +} + +void PowerT1() { + smlOBISWh(T1Wh); +} + +void PowerSum() { + smlOBISWh(SumWh); +} + +SMLHandler SMLHandlers[] = { + {{ 0x81, 0x81, 0xc7, 0x82, 0x03, 0xff }, &Manufacturer}, /* 129-129:199.130.3*255 */ + {{ 0x01, 0x00, 0x01, 0x08, 0x01, 0xff }, &PowerT1}, /* 1- 0: 1. 8.1*255 (T1) */ + {{ 0x01, 0x00, 0x01, 0x08, 0x00, 0xff }, &PowerSum}, /* 1- 0: 1. 8.0*255 (T1 + T2) */ + {{ 0, 0 }} +}; + +int main () { + int i = 0, iHandler = 0; + unsigned char c; + sml_states_t s; + for (i = 0; i < ehz_bin_len; ++i) { + c = ehz_bin[i]; + s = smlState(c); + if (s == SML_START) { + /* reset local vars */ + manuf[0] = 0; T1Wh = -3; SumWh = -3; + } + if (s == SML_LISTEND) { + /* check handlers on last received list */ + for (iHandler=0; SMLHandlers[iHandler].Handler != 0 && + !(smlOBISCheck(SMLHandlers[iHandler].OBIS)); iHandler++); + if (SMLHandlers[iHandler].Handler != 0) { + SMLHandlers[iHandler].Handler(); + } + } + if (s == SML_UNEXPECTED) { + printf(">>> Unexpected byte! <<<\n"); + } + if (s == SML_FINAL) { + printf(">>> FINAL! Checksum OK\n"); + printf(">>> Manufacturer.............: %s\n", manuf); + printf(">>> Power T1 (1-0:1.8.1)..: %.3f kWh\n", T1Wh); + printf(">>> Power T1+T2 (1-0:1.8.0)..: %.3f kWh\n\n", SumWh); + } + } +} +``` + +## Debug mode + +If debug mode via `SML_DEBUG` (see examples/native/platformio.ini) is enabled, the SML data is displayed in a tree like structure. + +``` +START + LISTSTART on level 1 with 6 nodes + Data 6 (length = 6, octet string): 00 0C 04 08 87 2D + Data 5 (length = 1, unsigned int): 00 + Data 4 (length = 1, unsigned int): 00 + LISTSTART on level 2 with 2 nodes + Data 2 (length = 2, unsigned int): 01 01 + LISTSTART on level 3 with 6 nodes + Data 6 (empty) + Data 5 (empty) + Data 4 (length = 6, octet string): 00 0C 06 9E 2D 0F + Data 3 (length = 10, octet string): 06 45 4D 48 01 00 1D 46 15 CA + Data 2 (empty) + Data 1 (empty) + LISTEND + back to previous list + back to previous list + Data 2 (length = 2, unsigned int): 2B 8E + End of block at level 1 + back to previous list + LISTSTART on level 1 with 6 nodes + Data 6 (length = 6, octet string): 00 0C 04 08 87 2E + Data 5 (length = 1, unsigned int): 00 + Data 4 (length = 1, unsigned int): 00 + LISTSTART on level 2 with 2 nodes + Data 2 (length = 2, unsigned int): 07 01 + LISTSTART on level 3 with 7 nodes + Data 7 (empty) + Data 6 (length = 10, octet string): 06 45 4D 48 01 00 1D 46 15 CA + Data 5 (empty) + LISTSTART on level 4 with 2 nodes + Data 2 (length = 1, unsigned int): 01 + Data 1 (length = 4, unsigned int): 06 9E FA 83 + LISTEND on level 4 + back to previous list + LISTSTART on level 4 with 7 nodes + LISTSTART on level 5 with 7 nodes + Data 7 (length = 6, octet string): 81 81 C7 82 03 FF + Data 6 (empty) + Data 5 (empty) + Data 4 (empty) + Data 3 (empty) + Data 2 (length = 3, octet string): 45 4D 48 + Data 1 (empty) + LISTEND + back to previous list + LISTSTART on level 5 with 7 nodes + Data 7 (length = 6, octet string): 01 00 00 00 09 FF + Data 6 (empty) + Data 5 (empty) + Data 4 (empty) + Data 3 (empty) + Data 2 (length = 10, octet string): 06 45 4D 48 01 00 1D 46 15 CA + Data 1 (empty) + LISTEND + back to previous list + LISTSTART on level 5 with 7 nodes + Data 7 (length = 6, octet string): 01 00 01 08 00 FF + Data 6 (length = 2, unsigned int): 01 82 + Data 5 (empty) + Data 4 (length = 1, unsigned int): 1E + Data 3 (length = 1, signed int): 03 + Data 2 (length = 5, signed int): 00 00 00 1C 46 + Data 1 (empty) + LISTEND + back to previous list + LISTSTART on level 5 with 7 nodes + Data 7 (length = 6, octet string): 01 00 01 08 01 FF + Data 6 (empty) + Data 5 (empty) + Data 4 (length = 1, unsigned int): 1E + Data 3 (length = 1, signed int): FF + Data 2 (length = 8, signed int): 00 00 00 00 07 5B CD 15 + Data 1 (empty) + LISTEND + back to previous list + LISTSTART on level 5 with 7 nodes + Data 7 (length = 6, octet string): 01 00 01 08 02 FF + Data 6 (empty) + Data 5 (empty) + Data 4 (length = 1, unsigned int): 1E + Data 3 (length = 1, signed int): 03 + Data 2 (length = 5, signed int): 00 00 00 1C 46 + Data 1 (empty) + LISTEND + back to previous list + LISTSTART on level 5 with 7 nodes + Data 7 (length = 6, octet string): 01 00 0F 07 00 FF + Data 6 (empty) + Data 5 (empty) + Data 4 (length = 1, unsigned int): 1B + Data 3 (length = 1, signed int): FF + Data 2 (length = 4, signed int): 00 00 2F 65 + Data 1 (empty) + LISTEND + back to previous list + LISTSTART on level 5 with 7 nodes + Data 7 (length = 6, octet string): 81 81 C7 82 05 FF + Data 6 (empty) + Data 5 (empty) + Data 4 (empty) + Data 3 (empty) + Data (length = 48): FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF + Data 2 (empty) + Data 1 (empty) + LISTEND + back to previous list + back to previous list + Data 2 (empty) + Data 1 (length = 2, unsigned int): B9 3F + LISTEND on level 3 + back to previous list + back to previous list + End of block at level 1 + LISTSTART on level 2 with 6 nodes + Data 6 (length = 6, octet string): 00 0C 04 08 87 31 + Data 5 (length = 1, unsigned int): 00 + Data 4 (length = 1, unsigned int): 00 + LISTSTART on level 3 with 2 nodes + Data 2 (length = 2, unsigned int): 02 01 + LISTSTART on level 4 with 1 nodes + Data 1 (empty) + LISTEND + back to previous list + back to previous list + Data 2 (length = 2, unsigned int): 6A 53 + End of block at level 2 + back to previous list + back to previous list +End of block at level 0 +Received checksum: C6E8 +Calculated checksum: C6E8 +>>> FINAL! Checksum OK +>>> Manufacturer.............: EMH +>>> Power T1 (1-0:1.8.1)..: 12345678.900 Wh +>>> Power T1+T2 (1-0:1.8.0)..: 7238000.000 Wh +``` + +## Links + +The following sites provided a lot of helpful information to me. + +- https://wiki.volkszaehler.org/software/obis +- https://wiki.volkszaehler.org/software/sml +- https://www.stefan-weigert.de/php_loader/sml.php +- https://github.com/devZer0/libsml-testing + +## License + +GNU LGPL v2.1 diff --git a/lib/sml_parser/examples/arduino/.gitignore b/lib/sml_parser/examples/arduino/.gitignore new file mode 100644 index 0000000..b9f3806 --- /dev/null +++ b/lib/sml_parser/examples/arduino/.gitignore @@ -0,0 +1,2 @@ +.pio +.vscode diff --git a/lib/sml_parser/examples/arduino/README.md b/lib/sml_parser/examples/arduino/README.md new file mode 100644 index 0000000..f2495b3 --- /dev/null +++ b/lib/sml_parser/examples/arduino/README.md @@ -0,0 +1 @@ +# Continuously loops through a static message from RAM and outputs information to serial diff --git a/lib/sml_parser/examples/arduino/platformio.ini b/lib/sml_parser/examples/arduino/platformio.ini new file mode 100644 index 0000000..511f7bc --- /dev/null +++ b/lib/sml_parser/examples/arduino/platformio.ini @@ -0,0 +1,18 @@ +; PlatformIO Project Configuration File +; +; Build options: build flags, source filter +; Upload options: custom upload port, speed and extra flags +; Library options: dependencies, extra library storages +; Advanced options: extra scripting +; +; Please visit documentation for the other options and examples +; https://docs.platformio.org/page/projectconf.html + +[env:uno] +platform = atmelavr +board = uno +framework = arduino +board_build.mcu = atmega328p +board_build.f_cpu = 16000000L +build_flags = -D SML_DEBUG +lib_deps = ../../src diff --git a/lib/sml_parser/examples/arduino/src/ehz_bin.h b/lib/sml_parser/examples/arduino/src/ehz_bin.h new file mode 100644 index 0000000..894a549 --- /dev/null +++ b/lib/sml_parser/examples/arduino/src/ehz_bin.h @@ -0,0 +1,119 @@ +// clang-format off + +// Formatted example data from https://github.com/devZer0/libsml-testing + +const unsigned char ehz_bin[] = { + /* Start */ 0x1b, 0x1b, 0x1b, 0x1b, + /* Version */ 0x01, 0x01, 0x01, 0x01, + + /* List (6 entries) */ 0x76, + /* 1. Transaktion ID */ 0x07, 0x00, 0x0c, 0x04, 0x08, 0x87, 0x2d, + /* 2. Group No */ 0x62, 0x00, + /* 3. Abort On Error */ 0x62, 0x00, + /* 4. List (2 entries) */ 0x72, + /* 1. getOpenResponse */ 0x63, 0x01, 0x01, + /* 2. List (6 entries) */ 0x76, + /* 1. code page */ 0x01, + /* 2. clientid */ 0x01, + /* 3. Transaction ID */ 0x07, 0x00, 0x0c, 0x06, 0x9e, 0x2d, 0x0f, + /* 4. ServerID */ 0x0b, 0x06, 0x45, 0x4d, 0x48, 0x01, 0x00, 0x1d, 0x46, 0x15, 0xca, + /* 5. List name */ 0x01, + /* 6. Time */ 0x01, + /* 5. CRC */ 0x63, 0x2b, 0x8e, + /* 6. END */ 0x00, + + /* List (6 entries) */ 0x76, + /* 1. Transaktion ID */ 0x07, 0x00, 0x0c, 0x04, 0x08, 0x87, 0x2e, + /* 2. Group No */ 0x62, 0x00, + /* 3. Abort On Error */ 0x62, 0x00, + /* 4. Message Body (2) */ 0x72, + /* 1. getListResponse */ 0x63, 0x07, 0x01, + /* 2. List (7) */ 0x77, + /* 1. ClientId */ 0x01, + /* 2. ServerID */ 0x0b, 0x06, 0x45, 0x4d, 0x48, 0x01, 0x00, 0x1d, 0x46, 0x15, 0xca, + /* 3. ?? */ 0x01, + /* 4. List (2) */ 0x72, + /* 1. ?? */ 0x62, 0x01, + /* 2. ?? */ 0x65, 0x06, 0x9e, 0xfa, 0x83, + /* 5. List (7) */ 0x77, + /* 1. List (7) */ 0x77, + /* 1. OBIS */ 0x07, 0x81, 0x81, 0xc7, 0x82, 0x03, 0xff, + /* 2. */ 0x01, + /* 3. */ 0x01, + /* 4. */ 0x01, + /* 5. */ 0x01, + /* 6. */ 0x04, 0x45, 0x4d, 0x48, + /* 7. */ 0x01, + /* 2. List (7) */ 0x77, + /* 1. OBIS */ 0x07, 0x01, 0x00, 0x00, 0x00, 0x09, 0xff, + /* 2. */ 0x01, + /* 3. */ 0x01, + /* 4. */ 0x01, + /* 5. */ 0x01, + /* 6. */ 0x0b, 0x06, 0x45, 0x4d, 0x48, 0x01, 0x00, 0x1d, 0x46, 0x15, 0xca, + /* 7. */ 0x01, + /* 3. List (7) */ 0x77, + /* 1. OBIS */ 0x07, 0x01, 0x00, 0x01, 0x08, 0x00, 0xff, + /* 2. */ 0x63, 0x01, 0x82, + /* 3. */ 0x01, + /* 4. */ 0x62, 0x1e, + /* 5. */ 0x52, 0xff, + /* 6. */ 0x56, 0x00, 0x00, 0x00, 0x74, 0xea, + /* 7. */ 0x01, + /* 4. List (7) */ 0x77, + /* 1. OBIS */ 0x07, 0x01, 0x00, 0x01, 0x08, 0x01, 0xff, + /* 2. */ 0x01, + /* 3. */ 0x01, + /* 4. Unit */ 0x62, 0x1e, + /* 5. Scaler */ 0x52, 0xff, + /* 6 Value */ 0x59, 0x00, 0x00, 0x00, 0x00, 0x00, 0x12, 0xd6, 0x87, + /* 7 */ 0x01, + /* 5. List (7) */ 0x77, + /* 1. OBIS */ 0x07, 0x01, 0x00, 0x01, 0x08, 0x02, 0xff, + /* 2. */ 0x01, + /* 3. */ 0x01, + /* 4. */ 0x62, 0x1e, + /* 5. */ 0x52, 0xff, + /* 6. */ 0x56, 0x00, 0x00, 0x00, 0x00, 0x00, + /* 7. */ 0x01, + /* 6. List (7) */ 0x77, + /* 1. OBIS */ 0x07, 0x01, 0x00, 0x0f, 0x07, 0x00, 0xff, + /* 2. */ 0x01, + /* 3. */ 0x01, + /* 4. */ 0x62, 0x1b, + /* 5. */ 0x52, 0xff, + /* 6. */ 0x55, 0x00, 0x00, 0x2f, 0x65, + /* 7. */ 0x01, + /* 7. List (7) */ 0x77, + /* 1. OBIS */ 0x07, 0x81, 0x81, 0xc7, 0x82, 0x05, 0xff, + /* 2. */ 0x01, + /* 3. */ 0x01, + /* 4. */ 0x01, + /* 5. */ 0x01, + /* 6. Public Key */ 0x83, 0x02, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + /* 7. */ 0x01, + 0x01, + 0x01, + 0x63, 0xb9, 0x3f, + 0x00, + /* List (6) */ 0x76, + /* 1. */ 0x07, 0x00, 0x0c, 0x04, 0x08, 0x87, 0x31, + /* 2. */ 0x62, 0x00, + /* 3. */ 0x62, 0x00, + /* 4. List (2) */ 0x72, + /* 1. */ 0x63, 0x02, 0x01, + /* List (1) */ 0x71, + /* 1. */ 0x01, + /* 5. CRC */ 0x63, 0x6a, 0x53, + /* 6. */ 0x00, + + 0x00, + 0x1b, 0x1b, 0x1b, 0x1b, + 0x1a, 0x01, 0xb6, 0x9d +}; +const unsigned int ehz_bin_len = 319; + diff --git a/lib/sml_parser/examples/arduino/src/main.cpp b/lib/sml_parser/examples/arduino/src/main.cpp new file mode 100644 index 0000000..41ea2d7 --- /dev/null +++ b/lib/sml_parser/examples/arduino/src/main.cpp @@ -0,0 +1,75 @@ +#include "ehz_bin.h" +#include "sml.h" +#include +#include + +// Continuously loops through a static message from RAM and outputs information +// to serial + +double T1Wh = -2, SumWh = -2; + +typedef struct { + const unsigned char OBIS[6]; + void (*Handler)(); +} OBISHandler; + +void PowerT1() { smlOBISWh(T1Wh); } + +void PowerSum() { smlOBISWh(SumWh); } + +// clang-format off +OBISHandler OBISHandlers[] = { + {{ 0x01, 0x00, 0x01, 0x08, 0x01, 0xff }, &PowerT1}, /* 1- 0: 1. 8.1*255 (T1) */ + {{ 0x01, 0x00, 0x01, 0x08, 0x00, 0xff }, &PowerSum}, /* 1- 0: 1. 8.0*255 (T1 + T2) */ + {{ 0, 0 }} +}; +// clang-format on + +void setup() +{ + Serial.begin(9600); + Serial.println(F("Starting")); +} + +void loop() +{ + char floatBuffer[20]; + unsigned int i = 0, iHandler = 0; + unsigned char c; + sml_states_t s; + while (true) { + for (i = 0; i < ehz_bin_len; ++i) { + c = ehz_bin[i]; + s = smlState(c); + if (s == SML_START) { + /* reset local vars */ + T1Wh = -3; + SumWh = -3; + } + if (s == SML_LISTEND) { + /* check handlers on last received list */ + for (iHandler = 0; OBISHandlers[iHandler].Handler != 0 && + !(smlOBISCheck(OBISHandlers[iHandler].OBIS)); + iHandler++) + ; + if (OBISHandlers[iHandler].Handler != 0) { + OBISHandlers[iHandler].Handler(); + } + } + if (s == SML_UNEXPECTED) { + Serial.print(F(">>> Unexpected byte! <<<\n")); + } + if (s == SML_FINAL) { + Serial.print(F("Power T1 (1-0:1.8.1)..: ")); + dtostrf(T1Wh, 10, 3, floatBuffer); + Serial.print(floatBuffer); + Serial.print(F("\n")); + + Serial.print(F("Power T1+T2 (1-0:1.8.0)..: ")); + dtostrf(SumWh, 10, 3, floatBuffer); + Serial.print(floatBuffer); + Serial.print(F("\n\n\n\n")); + } + } + } +} diff --git a/lib/sml_parser/examples/arduino_serial/.clang-format b/lib/sml_parser/examples/arduino_serial/.clang-format new file mode 100644 index 0000000..1ef3556 --- /dev/null +++ b/lib/sml_parser/examples/arduino_serial/.clang-format @@ -0,0 +1 @@ +BreakBeforeBraces: Stroustrup diff --git a/lib/sml_parser/examples/arduino_serial/.gitignore b/lib/sml_parser/examples/arduino_serial/.gitignore new file mode 100644 index 0000000..95034cf --- /dev/null +++ b/lib/sml_parser/examples/arduino_serial/.gitignore @@ -0,0 +1,6 @@ +.pio +.vscode/.browse.c_cpp.db* +.vscode/c_cpp_properties.json +.vscode/launch.json +.vscode/ipch +lib diff --git a/lib/sml_parser/examples/arduino_serial/.vscode/extensions.json b/lib/sml_parser/examples/arduino_serial/.vscode/extensions.json new file mode 100644 index 0000000..080e70d --- /dev/null +++ b/lib/sml_parser/examples/arduino_serial/.vscode/extensions.json @@ -0,0 +1,10 @@ +{ + // See http://go.microsoft.com/fwlink/?LinkId=827846 + // for the documentation about the extensions.json format + "recommendations": [ + "platformio.platformio-ide" + ], + "unwantedRecommendations": [ + "ms-vscode.cpptools-extension-pack" + ] +} diff --git a/lib/sml_parser/examples/arduino_serial/.vscode/settings.json b/lib/sml_parser/examples/arduino_serial/.vscode/settings.json new file mode 100644 index 0000000..f89ed5f --- /dev/null +++ b/lib/sml_parser/examples/arduino_serial/.vscode/settings.json @@ -0,0 +1,3 @@ +{ + "editor.formatOnSave": true +} diff --git a/lib/sml_parser/examples/arduino_serial/README.md b/lib/sml_parser/examples/arduino_serial/README.md new file mode 100644 index 0000000..9d59cea --- /dev/null +++ b/lib/sml_parser/examples/arduino_serial/README.md @@ -0,0 +1,170 @@ +# Arduino Serial + +Reads data from Arduino Pin 8. + +Outputs some more information from the message if `SML_DEBUG` is enabled (see platformio.ini). + +Dumps a successfully received message as hex values which can be inserted into a .h file for testing. + +# Example output + +``` +START +LISTSTART on level 1 with 6 nodes + Data 6 (length = 6, octet string): 00 0C 04 08 87 2D + Data 5 (length = 1, unsigned int): 00 + Data 4 (length = 1, unsigned int): 00 +LISTSTART on level 2 with 2 nodes + Data 2 (length = 2, unsigned int): 01 01 +LISTSTART on level 3 with 6 nodes + Data 6 (empty) + Data 5 (empty) + Data 4 (length = 6, octet string): 00 0C 06 9E 2D 0F + Data 3 (length = 10, octet string): 06 45 4D 48 01 00 1D 46 15 CA + Data 2 (empty) + Data 1 (empty) +LISTEND +back to previous list +back to previous list + Data 2 (length = 2, unsigned int): 2B 8E +End of block at level 1 +back to previous list +LISTSTART on level 1 with 6 nodes + Data 6 (length = 6, octet string): 00 0C 04 08 87 2E + Data 5 (length = 1, unsigned int): 00 + Data 4 (length = 1, unsigned int): 00 +LISTSTART on level 2 with 2 nodes + Data 2 (length = 2, unsigned int): 07 01 +LISTSTART on level 3 with 7 nodes + Data 7 (empty) + Data 6 (length = 10, octet string): 06 45 4D 48 01 00 1D 46 15 CA + Data 5 (empty) +LISTSTART on level 4 with 2 nodes + Data 2 (length = 1, unsigned int): 01 + Data 1 (length = 4, unsigned int): 06 9E FA 83 +LISTEND on level 4 +back to previous list +LISTSTART on level 4 with 7 nodes +LISTSTART on level 5 with 7 nodes + Data 7 (length = 6, octet string): 81 81 C7 82 03 FF + Data 6 (empty) + Data 5 (empty) + Data 4 (empty) + Data 3 (empty) + Data 2 (length = 3, octet string): 45 4D 48 + Data 1 (empty) +LISTEND +back to previous list +LISTSTART on level 5 with 7 nodes + Data 7 (length = 6, octet string): 01 00 00 00 09 FF + Data 6 (empty) + Data 5 (empty) + Data 4 (empty) + Data 3 (empty) + Data 2 (length = 10, octet string): 06 45 4D 48 01 00 1D 46 15 CA + Data 1 (empty) +LISTEND +back to previous list +LISTSTART on level 5 with 7 nodes + Data 7 (length = 6, octet string): 01 00 01 08 00 FF + Data 6 (length = 2, unsigned int): 01 82 + Data 5 (empty) + Data 4 (length = 1, unsigned int): 1E + Data 3 (length = 1, signed int): FF + Data 2 (length = 5, signed int): 00 09 06 74 EA + Data 1 (empty) +LISTEND +back to previous list +LISTSTART on level 5 with 7 nodes + Data 7 (length = 6, octet string): 01 00 01 08 01 FF + Data 6 (empty) + Data 5 (empty) + Data 4 (length = 1, unsigned int): 1E + Data 3 (length = 1, signed int): FF + Data 2 (length = 8, signed int): 00 00 00 00 07 5B CD 15 + Data 1 (empty) +LISTEND +back to previous list +LISTSTART on level 5 with 7 nodes + Data 7 (length = 6, octet string): 01 00 01 08 02 FF + Data 6 (empty) + Data 5 (empty) + Data 4 (length = 1, unsigned int): 1E + Data 3 (length = 1, signed int): FF + Data 2 (length = 5, signed int): 00 00 00 00 00 + Data 1 (empty) +LISTEND +back to previous list +LISTSTART on level 5 with 7 nodes + Data 7 (length = 6, octet string): 01 00 0F 07 00 FF + Data 6 (empty) + Data 5 (empty) + Data 4 (length = 1, unsigned int): 1B + Data 3 (length = 1, signed int): FF + Data 2 (length = 4, signed int): 00 00 2F 65 + Data 1 (empty) +LISTEND +back to previous list +LISTSTART on level 5 with 7 nodes + Data 7 (length = 6, octet string): 81 81 C7 82 05 FF + Data 6 (empty) + Data 5 (empty) + Data 4 (empty) + Data 3 (empty) + Data (length = 48): FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF + Data 2 (empty) + Data 1 (empty) +LISTEND +back to previous list +back to previous list + Data 2 (empty) + Data 1 (length = 2, unsigned int): B9 3F +LISTEND on level 3 +back to previous list +back to previous list +End of block at level 1 +LISTSTART on level 2 with 6 nodes + Data 6 (length = 6, octet string): 00 0C 04 08 87 31 + Data 5 (length = 1, unsigned int): 00 + Data 4 (length = 1, unsigned int): 00 +LISTSTART on level 3 with 2 nodes + Data 2 (length = 2, unsigned int): 02 01 +LISTSTART on level 4 with 1 nodes + Data 1 (empty) +LISTEND +back to previous list +back to previous list + Data 2 (length = 2, unsigned int): 6A 53 +End of block at level 2 +back to previous list +back to previous list +End of block at level 0 +Received checksum: 1B70 +Calculated checksum: 1B70 +>>> Successfully received a complete message! +Size: 319 +--- +0x1B, 0x1B, 0x1B, 0x1B, 0x01, 0x01, 0x01, 0x01, 0x76, 0x07, 0x00, 0x0C, 0x04, 0x08, 0x87, +0x2D, 0x62, 0x00, 0x62, 0x00, 0x72, 0x63, 0x01, 0x01, 0x76, 0x01, 0x01, 0x07, 0x00, 0x0C, +0x06, 0x9E, 0x2D, 0x0F, 0x0B, 0x06, 0x45, 0x4D, 0x48, 0x01, 0x00, 0x1D, 0x46, 0x15, 0xCA, +0x01, 0x01, 0x63, 0x2B, 0x8E, 0x00, 0x76, 0x07, 0x00, 0x0C, 0x04, 0x08, 0x87, 0x2E, 0x62, +0x00, 0x62, 0x00, 0x72, 0x63, 0x07, 0x01, 0x77, 0x01, 0x0B, 0x06, 0x45, 0x4D, 0x48, 0x01, +0x00, 0x1D, 0x46, 0x15, 0xCA, 0x01, 0x72, 0x62, 0x01, 0x65, 0x06, 0x9E, 0xFA, 0x83, 0x77, +0x77, 0x07, 0x81, 0x81, 0xC7, 0x82, 0x03, 0xFF, 0x01, 0x01, 0x01, 0x01, 0x04, 0x45, 0x4D, +0x48, 0x01, 0x77, 0x07, 0x01, 0x00, 0x00, 0x00, 0x09, 0xFF, 0x01, 0x01, 0x01, 0x01, 0x0B, +0x06, 0x45, 0x4D, 0x48, 0x01, 0x00, 0x1D, 0x46, 0x15, 0xCA, 0x01, 0x77, 0x07, 0x01, 0x00, +0x01, 0x08, 0x00, 0xFF, 0x63, 0x01, 0x82, 0x01, 0x62, 0x1E, 0x52, 0xFF, 0x56, 0x00, 0x09, +0x06, 0x74, 0xEA, 0x01, 0x77, 0x07, 0x01, 0x00, 0x01, 0x08, 0x01, 0xFF, 0x01, 0x01, 0x62, +0x1E, 0x52, 0xFF, 0x59, 0x00, 0x00, 0x00, 0x00, 0x07, 0x5B, 0xCD, 0x15, 0x01, 0x77, 0x07, +0x01, 0x00, 0x01, 0x08, 0x02, 0xFF, 0x01, 0x01, 0x62, 0x1E, 0x52, 0xFF, 0x56, 0x00, 0x00, +0x00, 0x00, 0x00, 0x01, 0x77, 0x07, 0x01, 0x00, 0x0F, 0x07, 0x00, 0xFF, 0x01, 0x01, 0x62, +0x1B, 0x52, 0xFF, 0x55, 0x00, 0x00, 0x2F, 0x65, 0x01, 0x77, 0x07, 0x81, 0x81, 0xC7, 0x82, +0x05, 0xFF, 0x01, 0x01, 0x01, 0x01, 0x83, 0x02, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, +0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, +0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, +0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x01, 0x01, 0x01, 0x63, +0xB9, 0x3F, 0x00, 0x76, 0x07, 0x00, 0x0C, 0x04, 0x08, 0x87, 0x31, 0x62, 0x00, 0x62, 0x00, +0x72, 0x63, 0x02, 0x01, 0x71, 0x01, 0x63, 0x6A, 0x53, 0x00, 0x00, 0x1B, 0x1B, 0x1B, 0x1B, +0x1A, 0x01, 0x70, 0x1B +--- +``` \ No newline at end of file diff --git a/lib/sml_parser/examples/arduino_serial/main.cpp b/lib/sml_parser/examples/arduino_serial/main.cpp new file mode 100644 index 0000000..7be0590 --- /dev/null +++ b/lib/sml_parser/examples/arduino_serial/main.cpp @@ -0,0 +1,88 @@ +#include "sml.h" +#include +#include +#include +#include + +// Reads data from Arduino Pin 8 (only this pin is possible, see AltSoftSerial) +// SoftwareSerial will be too slow to read data even on 9600 baud. +// Dump successfully received message as hex values which +// can be inserted into a .h file. + +#define MAX_BUF_SIZE 700 + +AltSoftSerial inputSerial; + +sml_states_t currentState; + +RingBuf myBuffer; + +void print_buffer() +{ + unsigned int i = 0; + unsigned int j = 0; + char b[5]; + Serial.print(F("Size: ")); + Serial.print(myBuffer.size()); + Serial.println(""); + Serial.println(F("--- ")); + for (j = 0; j < myBuffer.size(); j++) { + i++; + sprintf(b, "0x%02X", myBuffer[j]); + Serial.print(b); + if (j < myBuffer.size() - 1) { + Serial.print(", "); + } + else { + Serial.println(""); + Serial.println(F("--- ")); + } + if ((i % 15) == 0) { + Serial.println(""); + i = 0; + } + } +} + +void readByte(unsigned char currentChar) +{ + currentState = smlState(currentChar); + if (currentState == SML_START) { + myBuffer.clear(); + myBuffer.push(0x1B); + myBuffer.push(0x1B); + myBuffer.push(0x1B); + } + else { + if (myBuffer.size() < MAX_BUF_SIZE) { + myBuffer.push(currentChar); + } + else { + Serial.print(F(">>> Message larger than MAX_BUF_SIZE\n")); + } + } + if (currentState == SML_UNEXPECTED) { + Serial.print(F(">>> Unexpected byte\n")); + } + if (currentState == SML_FINAL) { + Serial.print(F(">>> Successfully received a complete message!\n")); + print_buffer(); + } + if (currentState == SML_CHECKSUM_ERROR) { + Serial.print(F(">>> Checksum error.\n")); + } +} + +void setup() +{ + inputSerial.begin(9600); + Serial.begin(115200); + Serial.println(F("Starting")); +} + +void loop() +{ + while (inputSerial.available() > 0) { + readByte(inputSerial.read()); + } +} diff --git a/lib/sml_parser/examples/arduino_serial/platformio.ini b/lib/sml_parser/examples/arduino_serial/platformio.ini new file mode 100644 index 0000000..2e3ca64 --- /dev/null +++ b/lib/sml_parser/examples/arduino_serial/platformio.ini @@ -0,0 +1,25 @@ +; PlatformIO Project Configuration File +; +; Build options: build flags, source filter +; Upload options: custom upload port, speed and extra flags +; Library options: dependencies, extra library storages +; Advanced options: extra scripting +; +; Please visit documentation for the other options and examples +; https://docs.platformio.org/page/projectconf.html + +[platformio] +src_dir = ./ + +[env:uno] +platform = atmelavr +board = uno +framework = arduino +board_build.mcu = atmega328p +board_build.f_cpu = 16000000L +build_flags = -D SML_DEBUG +lib_deps = + ../../src + paulstoffregen/AltSoftSerial@^1.4 + locoduino/RingBuffer@^1.0.3 +monitor_speed = 115200 diff --git a/lib/sml_parser/examples/esp32_lora/.gitignore b/lib/sml_parser/examples/esp32_lora/.gitignore new file mode 100644 index 0000000..7324924 --- /dev/null +++ b/lib/sml_parser/examples/esp32_lora/.gitignore @@ -0,0 +1,3 @@ +.pio +src/keys.h + diff --git a/lib/sml_parser/examples/esp32_lora/README.md b/lib/sml_parser/examples/esp32_lora/README.md new file mode 100644 index 0000000..34df8b8 --- /dev/null +++ b/lib/sml_parser/examples/esp32_lora/README.md @@ -0,0 +1,20 @@ +# Forward energy usage to LoraWAN (The Things Network) + +- Rename keys.h.example to keys.h and insert your own DEVEUI and APPKEY +- Use the following uplink payload formatter: + +```JavaScript +function decodeUplink(input) { + var data = {}; + var str = String.fromCharCode.apply(null, input.bytes).trim(); + data.raw = str; + if (str > 0) { + data.kwh = str; + } + return { + data: data, + warnings: [], + errors: [] + }; +} +``` diff --git a/lib/sml_parser/examples/esp32_lora/platformio.ini b/lib/sml_parser/examples/esp32_lora/platformio.ini new file mode 100644 index 0000000..de4efdf --- /dev/null +++ b/lib/sml_parser/examples/esp32_lora/platformio.ini @@ -0,0 +1,26 @@ +; PlatformIO Project Configuration File +; +; Build options: build flags, source filter +; Upload options: custom upload port, speed and extra flags +; Library options: dependencies, extra library storages +; Advanced options: extra scripting +; +; Please visit documentation for the other options and examples +; https://docs.platformio.org/page/projectconf.html + +[env:esp32] +platform = espressif32 +board = heltec_wifi_lora_32_V2 +framework = arduino +monitor_speed = 115200 +build_flags = + -D SML_DEBUG + -D ARDUINO_LMIC_PROJECT_CONFIG_H_SUPPRESS + -D CFG_eu868=1 + -D CFG_sx1276_radio=1 + -D LMIC_ENABLE_arbitrary_clock_error=1 +lib_deps = + mcci-catena/MCCI LoRaWAN LMIC library@^4.1.1 + olikraus/U8g2@^2.32.10 + plerup/EspSoftwareSerial@^6.15.2 + ../../src diff --git a/lib/sml_parser/examples/esp32_lora/src/keys.h.example b/lib/sml_parser/examples/esp32_lora/src/keys.h.example new file mode 100644 index 0000000..73bb145 --- /dev/null +++ b/lib/sml_parser/examples/esp32_lora/src/keys.h.example @@ -0,0 +1,16 @@ +#include "Arduino.h" +#include + +// This EUI must be in little-endian format, so least-significant-byte +// first. When copying an EUI from ttnctl output, this means to reverse +// the bytes. For TTN issued EUIs the last bytes should be 0xD5, 0xB3, +// 0x70. +static const u1_t PROGMEM APPEUI[8]={ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; + +// This should also be in little endian format, see above. +static const u1_t PROGMEM DEVEUI[8]={ 0xFF, 0xFF, 0xFF, 0xFF, 0x7E, 0xD5, 0xB3, 0x70 }; + +// This key should be in big endian format (or, since it is not really a +// number but a block of memory, endianness does not really apply). In +// practice, a key taken from ttnctl can be copied as-is. +static const u1_t PROGMEM APPKEY[16] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF }; diff --git a/lib/sml_parser/examples/esp32_lora/src/main.cpp b/lib/sml_parser/examples/esp32_lora/src/main.cpp new file mode 100644 index 0000000..06b315f --- /dev/null +++ b/lib/sml_parser/examples/esp32_lora/src/main.cpp @@ -0,0 +1,307 @@ +#include "Arduino.h" +#include +#include +#include +#include + +#include "keys.h" +#include "sml.h" +#include +#include + +#define rxPin 36 + +SoftwareSerial inputSerial; +U8X8_SSD1306_128X64_NONAME_SW_I2C u8x8(/* clock=*/15, /* data=*/4, + /* reset=*/16); + +sml_states_t currentState; +unsigned char currentChar = 0; +unsigned long counter = 0, transmitted = 0; +char buffer[50]; +char statusMsg[30] = "Unknown"; +char floatBuffer[20]; +double T1Wh = -2, SumWh = -2, T1WhSend = -2, SumWhSend = -2; + +static osjob_t sendjob; + +// Schedule TX every this many seconds +const unsigned TX_INTERVAL = 500; + +void os_getArtEui(u1_t *buf) { memcpy_P(buf, APPEUI, 8); } +void os_getDevEui(u1_t *buf) { memcpy_P(buf, DEVEUI, 8); } +void os_getDevKey(u1_t *buf) { memcpy_P(buf, APPKEY, 16); } + +// Pin mapping +const lmic_pinmap lmic_pins = { + .nss = 18, + .rxtx = LMIC_UNUSED_PIN, + .rst = 14, + .dio = {26, 34, 35}, +}; + +typedef struct { + const unsigned char OBIS[6]; + void (*Handler)(); +} OBISHandler; + +void PowerT1() { smlOBISWh(T1Wh); } + +void PowerSum() { smlOBISWh(SumWh); } + +// clang-format off +OBISHandler OBISHandlers[] = { + {{0x01, 0x00, 0x01, 0x08, 0x01, 0xff}, &PowerT1}, /* 1- 0: 1. 8.1*255 (T1) */ + {{0x01, 0x00, 0x01, 0x08, 0x00, 0xff}, &PowerSum}, /* 1- 0: 1. 8.0*255 (T1 + T2) */ + {{0}, 0} +}; +// clang-format on + +void printHex2(unsigned v) +{ + v &= 0xff; + if (v < 16) + Serial.print('0'); + Serial.print(v, HEX); +} + +void updateDisplay() +{ + u8x8.drawString(0, 2, statusMsg); + + sprintf(buffer, "Msg: %lu", counter); + u8x8.drawString(0, 3, buffer); + + dtostrf(T1WhSend, 10, 1, floatBuffer); + sprintf(buffer, "T1.: %s", floatBuffer); + u8x8.drawString(0, 4, buffer); + + dtostrf(SumWhSend, 10, 1, floatBuffer); + sprintf(buffer, "Sum: %s", floatBuffer); + u8x8.drawString(0, 5, buffer); + + sprintf(buffer, "Tns: %lu", transmitted); + u8x8.drawString(0, 6, buffer); +} + +void do_send(osjob_t *j) +{ + unsigned char floatBuffer[20]; + // Check if there is not a current TX/RX job running + if (LMIC.opmode & OP_TXRXPEND) { + Serial.println(F("OP_TXRXPEND, not sending")); + } + else { + // @todo should be optimized + dtostrf(SumWhSend, 10, 1, (char *)floatBuffer); + LMIC_setTxData2(1, floatBuffer, strlen((char *)floatBuffer), 0); + Serial.printf("---> Packet queued %s", floatBuffer); + Serial.println(""); + } + // Next TX is scheduled after TX_COMPLETE event. +} + +void onEvent(ev_t ev) +{ + Serial.print(os_getTime()); + Serial.print(": "); + switch (ev) { + case EV_SCAN_TIMEOUT: + Serial.println(F("EV_SCAN_TIMEOUT")); + break; + case EV_BEACON_FOUND: + Serial.println(F("EV_BEACON_FOUND")); + break; + case EV_BEACON_MISSED: + Serial.println(F("EV_BEACON_MISSED")); + break; + case EV_BEACON_TRACKED: + Serial.println(F("EV_BEACON_TRACKED")); + break; + case EV_JOINING: + Serial.println(F("EV_JOINING")); + break; + case EV_JOINED: + Serial.println(F("EV_JOINED")); + { + u4_t netid = 0; + devaddr_t devaddr = 0; + u1_t nwkKey[16]; + u1_t artKey[16]; + LMIC_getSessionKeys(&netid, &devaddr, nwkKey, artKey); + Serial.print("netid: "); + Serial.println(netid, DEC); + Serial.print("devaddr: "); + Serial.println(devaddr, HEX); + Serial.print("AppSKey: "); + for (size_t i = 0; i < sizeof(artKey); ++i) { + if (i != 0) + Serial.print("-"); + printHex2(artKey[i]); + } + Serial.println(""); + Serial.print("NwkSKey: "); + for (size_t i = 0; i < sizeof(nwkKey); ++i) { + if (i != 0) + Serial.print("-"); + printHex2(nwkKey[i]); + } + Serial.println(); + sprintf(statusMsg, "Joined"); + updateDisplay(); + } + // Disable link check validation (automatically enabled + // during join, but because slow data rates change max TX + // size, we don't use it in this example. + LMIC_setLinkCheckMode(0); + break; + /* + || This event is defined but not used in the code. No + || point in wasting codespace on it. + || + || case EV_RFU1: + || Serial.println(F("EV_RFU1")); + || break; + */ + case EV_JOIN_FAILED: + Serial.println(F("EV_JOIN_FAILED")); + sprintf(statusMsg, "Join failed"); + break; + case EV_REJOIN_FAILED: + Serial.println(F("EV_REJOIN_FAILED")); + sprintf(statusMsg, "rejoin failed"); + break; + case EV_TXCOMPLETE: + Serial.println(F("---> EV_TXCOMPLETE (includes waiting for RX windows)")); + if (LMIC.txrxFlags & TXRX_ACK) + Serial.println(F("Received ack")); + if (LMIC.dataLen) { + Serial.print(F("Received ")); + Serial.print(LMIC.dataLen); + Serial.println(F(" bytes of payload")); + } + // Schedule next transmission + os_setTimedCallback(&sendjob, os_getTime() + sec2osticks(TX_INTERVAL), + do_send); + transmitted++; + sprintf(statusMsg, "TX Compl"); + updateDisplay(); + break; + case EV_LOST_TSYNC: + sprintf(statusMsg, "Lost sync"); + updateDisplay(); + Serial.println(F("EV_LOST_TSYNC")); + break; + case EV_RESET: + sprintf(statusMsg, "Reset sync"); + updateDisplay(); + Serial.println(F("EV_RESET")); + break; + case EV_RXCOMPLETE: + // data received in ping slot + Serial.println(F("EV_RXCOMPLETE")); + break; + case EV_LINK_DEAD: + sprintf(statusMsg, "Link dead"); + updateDisplay(); + Serial.println(F("EV_LINK_DEAD")); + break; + case EV_LINK_ALIVE: + sprintf(statusMsg, "Link alive"); + updateDisplay(); + Serial.println(F("EV_LINK_ALIVE")); + break; + /* + || This event is defined but not used in the code. No + || point in wasting codespace on it. + || + || case EV_SCAN_FOUND: + || Serial.println(F("EV_SCAN_FOUND")); + || break; + */ + case EV_TXSTART: + sprintf(statusMsg, "tx start"); + updateDisplay(); + Serial.println(F("EV_TXSTART")); + break; + case EV_TXCANCELED: + Serial.println(F("EV_TXCANCELED")); + break; + case EV_RXSTART: + /* do not print anything -- it wrecks timing */ + break; + case EV_JOIN_TXCOMPLETE: + Serial.println(F("EV_JOIN_TXCOMPLETE: no JoinAccept")); + break; + + default: + Serial.print(F("Unknown event: ")); + Serial.println((unsigned)ev); + break; + } +} + +void setup() +{ + Serial.begin(115200); + Serial.println("Starting"); + + u8x8.begin(); + u8x8.setFont(u8x8_font_chroma48medium8_r); + u8x8.drawString(0, 1, "SML to LoraWAN"); + u8x8.drawString(0, 2, "Waiting"); + + // LMIC init + os_init(); + + // Reset the MAC state. Session and pending data transfers will be discarded. + LMIC_reset(); + + pinMode(rxPin, INPUT); + + inputSerial.begin(9600, SWSERIAL_8N1, rxPin, -1, false, 0, 95); + inputSerial.enableRx(true); + inputSerial.enableTx(false); + + do_send(&sendjob); +} + +void readByte() +{ + unsigned int iHandler = 0; + currentState = smlState(currentChar); + if (currentState == SML_START) { + /* reset local vars */ + T1Wh = -3; + SumWh = -3; + } + if (currentState == SML_LISTEND) { + /* check handlers on last received list */ + for (iHandler = 0; OBISHandlers[iHandler].Handler != 0 && + !(smlOBISCheck(OBISHandlers[iHandler].OBIS)); + iHandler++) + ; + if (OBISHandlers[iHandler].Handler != 0) { + OBISHandlers[iHandler].Handler(); + } + } + if (currentState == SML_UNEXPECTED) { + Serial.print(F(">>> Unexpected byte!\n")); + } + if (currentState == SML_FINAL) { + SumWhSend = SumWh; + T1WhSend = T1Wh; + updateDisplay(); + counter++; + Serial.print(F(">>> Successfully received a complete message!\n")); + } +} + +void loop() +{ + os_runloop_once(); + if (inputSerial.available()) { + currentChar = inputSerial.read(); + readByte(); + } +} diff --git a/lib/sml_parser/examples/esp32_m5stack_sender/.gitignore b/lib/sml_parser/examples/esp32_m5stack_sender/.gitignore new file mode 100644 index 0000000..97a07b1 --- /dev/null +++ b/lib/sml_parser/examples/esp32_m5stack_sender/.gitignore @@ -0,0 +1,4 @@ +.pio +.vscode +.pioenvs +.piolibdeps diff --git a/lib/sml_parser/examples/esp32_m5stack_sender/README.md b/lib/sml_parser/examples/esp32_m5stack_sender/README.md new file mode 100644 index 0000000..f71cf0c --- /dev/null +++ b/lib/sml_parser/examples/esp32_m5stack_sender/README.md @@ -0,0 +1,9 @@ +# M5Stack sender + +M5Stack example to produce some test data on TXD2 pin + +## Usage + +```bash +pio run +``` diff --git a/lib/sml_parser/examples/esp32_m5stack_sender/platformio.ini b/lib/sml_parser/examples/esp32_m5stack_sender/platformio.ini new file mode 100644 index 0000000..1e5112a --- /dev/null +++ b/lib/sml_parser/examples/esp32_m5stack_sender/platformio.ini @@ -0,0 +1,23 @@ +; PlatformIO Project Configuration File +; +; Build options: build flags, source filter +; Upload options: custom upload port, speed and extra flags +; Library options: dependencies, extra library storages +; Advanced options: extra scripting +; +; Please visit documentation for the other options and examples +; https://docs.platformio.org/page/projectconf.html + +[env:m5stack-core-esp32] +platform = espressif32 +board = m5stack-core-esp32 +framework = arduino +upload_speed = 921600 +targets = upload +; build_flags = -D DEBUG_ARDUINO +lib_deps = + M5Stack + plerup/EspSoftwareSerial@^7.0.0 + ../../src +monitor_speed = 115200 + diff --git a/lib/sml_parser/examples/esp32_m5stack_sender/src/ehz_bin.h b/lib/sml_parser/examples/esp32_m5stack_sender/src/ehz_bin.h new file mode 100644 index 0000000..05ff2d8 --- /dev/null +++ b/lib/sml_parser/examples/esp32_m5stack_sender/src/ehz_bin.h @@ -0,0 +1,118 @@ +// clang-format off +// Formatted example data from https://github.com/devZer0/libsml-testing + +const unsigned char ehz_bin[] = { + /* Start */ 0x1b, 0x1b, 0x1b, 0x1b, + /* Version */ 0x01, 0x01, 0x01, 0x01, + + /* List (6 entries) */ 0x76, + /* 1. Transaktion ID */ 0x07, 0x00, 0x0c, 0x04, 0x08, 0x87, 0x2d, + /* 2. Group No */ 0x62, 0x00, + /* 3. Abort On Error */ 0x62, 0x00, + /* 4. List (2 entries) */ 0x72, + /* 1. getOpenResponse */ 0x63, 0x01, 0x01, + /* 2. List (6 entries) */ 0x76, + /* 1. code page */ 0x01, + /* 2. clientid */ 0x01, + /* 3. Transaction ID */ 0x07, 0x00, 0x0c, 0x06, 0x9e, 0x2d, 0x0f, + /* 4. ServerID */ 0x0b, 0x06, 0x45, 0x4d, 0x48, 0x01, 0x00, 0x1d, 0x46, 0x15, 0xca, + /* 5. List name */ 0x01, + /* 6. Time */ 0x01, + /* 5. CRC */ 0x63, 0x2b, 0x8e, + /* 6. END */ 0x00, + + /* List (6 entries) */ 0x76, + /* 1. Transaktion ID */ 0x07, 0x00, 0x0c, 0x04, 0x08, 0x87, 0x2e, + /* 2. Group No */ 0x62, 0x00, + /* 3. Abort On Error */ 0x62, 0x00, + /* 4. Message Body (2) */ 0x72, + /* 1. getListResponse */ 0x63, 0x07, 0x01, + /* 2. List (7) */ 0x77, + /* 1. ClientId */ 0x01, + /* 2. ServerID */ 0x0b, 0x06, 0x45, 0x4d, 0x48, 0x01, 0x00, 0x1d, 0x46, 0x15, 0xca, + /* 3. ?? */ 0x01, + /* 4. List (2) */ 0x72, + /* 1. ?? */ 0x62, 0x01, + /* 2. ?? */ 0x65, 0x06, 0x9e, 0xfa, 0x83, + /* 5. List (7) */ 0x77, + /* 1. List (7) */ 0x77, + /* 1. OBIS */ 0x07, 0x81, 0x81, 0xc7, 0x82, 0x03, 0xff, + /* 2. */ 0x01, + /* 3. */ 0x01, + /* 4. */ 0x01, + /* 5. */ 0x01, + /* 6. Value=Manuf */ 0x04, 0x45, 0x4d, 0x48, + /* 7. */ 0x01, + /* 2. List (7) */ 0x77, + /* 1. OBIS */ 0x07, 0x01, 0x00, 0x00, 0x00, 0x09, 0xff, + /* 2. */ 0x01, + /* 3. */ 0x01, + /* 4. */ 0x01, + /* 5. */ 0x01, + /* 6. Value */ 0x0b, 0x06, 0x45, 0x4d, 0x48, 0x01, 0x00, 0x1d, 0x46, 0x15, 0xca, + /* 7. */ 0x01, + /* 3. List (7) */ 0x77, + /* 1. OBIS */ 0x07, 0x01, 0x00, 0x01, 0x08, 0x00, 0xff, + /* 2. */ 0x63, 0x01, 0x82, + /* 3. */ 0x01, + /* 4. Unit=wh */ 0x62, 0x1e, + /* 5. Scaler */ 0x52, 0xff, + /* 6. Value */ 0x56, 0x00, 0x09, 0x06, 0x74, 0xea, + /* 7. */ 0x01, + /* 4. List (7) */ 0x77, + /* 1. OBIS */ 0x07, 0x01, 0x00, 0x01, 0x08, 0x01, 0xff, + /* 2. */ 0x01, + /* 3. */ 0x01, + /* 4. Unit=wh */ 0x62, 0x1e, + /* 5. Scaler */ 0x52, 0xff, + /* 6 Value */ 0x59, 0x00, 0x00, 0x00, 0x00, 0x07, 0x5b, 0xcd, 0x15, + /* 7 */ 0x01, + /* 5. List (7) */ 0x77, + /* 1. OBIS */ 0x07, 0x01, 0x00, 0x01, 0x08, 0x02, 0xff, + /* 2. */ 0x01, + /* 3. */ 0x01, + /* 4. */ 0x62, 0x1e, + /* 5. */ 0x52, 0xff, + /* 6. */ 0x56, 0x00, 0x00, 0x00, 0x00, 0x00, + /* 7. */ 0x01, + /* 6. List (7) */ 0x77, + /* 1. OBIS */ 0x07, 0x01, 0x00, 0x0f, 0x07, 0x00, 0xff, + /* 2. */ 0x01, + /* 3. */ 0x01, + /* 4. */ 0x62, 0x1b, + /* 5. */ 0x52, 0xff, + /* 6. */ 0x55, 0x00, 0x00, 0x2f, 0x65, + /* 7. */ 0x01, + /* 7. List (7) */ 0x77, + /* 1. OBIS */ 0x07, 0x81, 0x81, 0xc7, 0x82, 0x05, 0xff, + /* 2. */ 0x01, + /* 3. */ 0x01, + /* 4. */ 0x01, + /* 5. */ 0x01, + /* 6. Public Key */ 0x83, 0x02, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + /* 7. */ 0x01, + 0x01, + 0x01, + 0x63, 0xb9, 0x3f, + 0x00, + /* List (6) */ 0x76, + /* 1. */ 0x07, 0x00, 0x0c, 0x04, 0x08, 0x87, 0x31, + /* 2. */ 0x62, 0x00, + /* 3. */ 0x62, 0x00, + /* 4. List (2) */ 0x72, + /* 1. */ 0x63, 0x02, 0x01, + /* List (1) */ 0x71, + /* 1. */ 0x01, + /* 5. CRC */ 0x63, 0x6a, 0x53, + /* 6. */ 0x00, + + 0x00, + 0x1b, 0x1b, 0x1b, 0x1b, + 0x1a, 0x01, 0x70, 0x1b +}; +const unsigned int ehz_bin_len = 319; + diff --git a/lib/sml_parser/examples/esp32_m5stack_sender/src/main.cpp b/lib/sml_parser/examples/esp32_m5stack_sender/src/main.cpp new file mode 100644 index 0000000..6dd4023 --- /dev/null +++ b/lib/sml_parser/examples/esp32_m5stack_sender/src/main.cpp @@ -0,0 +1,67 @@ +#include "ehz_bin.h" +#include "sml.h" +#include +#include + +// M5Stack to produce some example data on TXD2 pin (blue grove connector) +// Press button A to toggle between inverted/uninverted signal + +SoftwareSerial outSerial; + +int i = 0, counter = 0; +unsigned char c; +sml_states_t s; +bool invert = true; + +unsigned long previousMillis = 0; +const long interval = 2000; + +void setup() +{ + outSerial.begin(9600, SWSERIAL_8N1, -1, TXD2, invert, 0, 95); + outSerial.enableRx(false); + Serial.begin(115200); + M5.begin(); + + M5.Lcd.setCursor(1, 10); + M5.Lcd.fillScreen(WHITE); + M5.Lcd.setTextColor(BLACK); + M5.Lcd.setTextSize(5); + M5.Lcd.print("SML Sender"); +} + +void updateDisplay() +{ + M5.Lcd.fillRect(0, 50, M5.Lcd.width(), 50, WHITE); + M5.Lcd.setCursor(1, 50); + M5.Lcd.setTextSize(2); + M5.Lcd.printf("Send: %i", counter); + M5.Lcd.setCursor(1, 70); + M5.Lcd.printf("Invert: %s", (invert) ? "Yes" : "No"); + Serial.print("Message sent\n"); +} + +void loop() +{ + unsigned long currentMillis = millis(); + M5.update(); + if (M5.BtnA.wasReleased()) { + invert = (invert) ? false : true; + outSerial.begin(9600, SWSERIAL_8N1, -1, TXD2, invert, 0, 95); + Serial.print("Button pressed.\n"); + updateDisplay(); + } + if (currentMillis - previousMillis >= interval) { + previousMillis = currentMillis; + for (i = 0; i < ehz_bin_len; ++i) { + c = ehz_bin[i]; + s = smlState(c); + outSerial.write(c); + outSerial.flush(); + if (s == SML_FINAL) { + counter++; + updateDisplay(); + } + } + } +} diff --git a/lib/sml_parser/examples/esp32_receiver/.gitignore b/lib/sml_parser/examples/esp32_receiver/.gitignore new file mode 100644 index 0000000..bd997cc --- /dev/null +++ b/lib/sml_parser/examples/esp32_receiver/.gitignore @@ -0,0 +1,3 @@ +.pio +.vscode + diff --git a/lib/sml_parser/examples/esp32_receiver/README.md b/lib/sml_parser/examples/esp32_receiver/README.md new file mode 100644 index 0000000..a4702ee --- /dev/null +++ b/lib/sml_parser/examples/esp32_receiver/README.md @@ -0,0 +1,11 @@ +# Receive messages and show infos on a display + +Receive messages on ESP32 pin 36. + +![Example](image.jpg) + +# Upload + +```bash +pio run --target upload +``` diff --git a/lib/sml_parser/examples/esp32_receiver/image.jpg b/lib/sml_parser/examples/esp32_receiver/image.jpg new file mode 100644 index 0000000..f87dd5b Binary files /dev/null and b/lib/sml_parser/examples/esp32_receiver/image.jpg differ diff --git a/lib/sml_parser/examples/esp32_receiver/platformio.ini b/lib/sml_parser/examples/esp32_receiver/platformio.ini new file mode 100644 index 0000000..ec22a4d --- /dev/null +++ b/lib/sml_parser/examples/esp32_receiver/platformio.ini @@ -0,0 +1,21 @@ +; PlatformIO Project Configuration File +; +; Build options: build flags, source filter +; Upload options: custom upload port, speed and extra flags +; Library options: dependencies, extra library storages +; Advanced options: extra scripting +; +; Please visit documentation for the other options and examples +; https://docs.platformio.org/page/projectconf.html + +[env:esp32] +platform = espressif32 +board = heltec_wifi_lora_32_V2 +framework = arduino +monitor_speed = 115200 +build_flags = -D SML_DEBUG +lib_deps = + olikraus/U8g2@^2.32.10 + plerup/EspSoftwareSerial@^6.15.2 + olliiiver/SML Parser + diff --git a/lib/sml_parser/examples/esp32_receiver/src/main.cpp b/lib/sml_parser/examples/esp32_receiver/src/main.cpp new file mode 100644 index 0000000..29731d0 --- /dev/null +++ b/lib/sml_parser/examples/esp32_receiver/src/main.cpp @@ -0,0 +1,113 @@ +#include "Arduino.h" +#include +#include + +#include "sml.h" +#include + +#define rxPin 36 + +#define MAX_STR_MANUF 5 +unsigned char manuf[MAX_STR_MANUF]; +double T1Wh = -2, SumWh = -2; + +typedef struct { + const unsigned char OBIS[6]; + void (*Handler)(); +} OBISHandler; + +void Manufacturer() { smlOBISManufacturer(manuf, MAX_STR_MANUF); } + +void PowerT1() { smlOBISWh(T1Wh); } + +void PowerSum() { smlOBISWh(SumWh); } + +// clang-format off +OBISHandler OBISHandlers[] = { + {{0x81, 0x81, 0xc7, 0x82, 0x03, 0xff}, &Manufacturer}, /* 129-129:199.130.3*255 */ + {{0x01, 0x00, 0x01, 0x08, 0x01, 0xff}, &PowerT1}, /* 1- 0: 1. 8.1*255 (T1) */ + {{0x01, 0x00, 0x01, 0x08, 0x00, 0xff}, &PowerSum}, /* 1- 0: 1. 8.0*255 (T1 + T2) */ + {{0}, 0} +}; +// clang-format on + +SoftwareSerial inputSerial; + +U8X8_SSD1306_128X64_NONAME_SW_I2C u8x8(/* clock=*/15, /* data=*/4, + /* reset=*/16); + +sml_states_t currentState; +unsigned char currentChar = 0; +unsigned long counter = 0; +char buffer[50]; +char floatBuffer[20]; + +void updateDisplay() +{ + sprintf(buffer, "Msg..: %lu", counter); + u8x8.drawString(0, 2, buffer); + sprintf(buffer, "Manuf: %s", manuf); + u8x8.drawString(0, 3, buffer); + + dtostrf(T1Wh, 10, 1, floatBuffer); + sprintf(buffer, "T1.: %s", floatBuffer); + u8x8.drawString(0, 4, buffer); + + dtostrf(SumWh, 10, 1, floatBuffer); + sprintf(buffer, "Sum: %s", floatBuffer); + u8x8.drawString(0, 5, buffer); +} + +void setup() +{ + Serial.begin(115200); + Serial.println("Starting"); + + u8x8.begin(); + u8x8.setFont(u8x8_font_chroma48medium8_r); + u8x8.drawString(0, 1, "SML Receiver"); + + pinMode(rxPin, INPUT); + inputSerial.begin(9600, SWSERIAL_8N1, rxPin, -1, false, 0, 95); + inputSerial.enableRx(true); + inputSerial.enableTx(false); + updateDisplay(); +} + +void readByte() +{ + unsigned int iHandler = 0; + currentState = smlState(currentChar); + if (currentState == SML_START) { + /* reset local vars */ + manuf[0] = 0; + T1Wh = -3; + SumWh = -3; + } + if (currentState == SML_LISTEND) { + /* check handlers on last received list */ + for (iHandler = 0; OBISHandlers[iHandler].Handler != 0 && + !(smlOBISCheck(OBISHandlers[iHandler].OBIS)); + iHandler++) + ; + if (OBISHandlers[iHandler].Handler != 0) { + OBISHandlers[iHandler].Handler(); + } + } + if (currentState == SML_UNEXPECTED) { + Serial.print(F(">>> Unexpected byte!\n")); + } + if (currentState == SML_FINAL) { + updateDisplay(); + counter++; + Serial.print(F(">>> Successfully received a complete message!\n")); + } +} + +void loop() +{ + if (inputSerial.available()) { + currentChar = inputSerial.read(); + readByte(); + } +} diff --git a/lib/sml_parser/examples/native/.gitignore b/lib/sml_parser/examples/native/.gitignore new file mode 100644 index 0000000..b9f3806 --- /dev/null +++ b/lib/sml_parser/examples/native/.gitignore @@ -0,0 +1,2 @@ +.pio +.vscode diff --git a/lib/sml_parser/examples/native/README.md b/lib/sml_parser/examples/native/README.md new file mode 100644 index 0000000..5f26072 --- /dev/null +++ b/lib/sml_parser/examples/native/README.md @@ -0,0 +1,13 @@ +# Native example + +Test the library on our local machine. + +Should simply work on OS X and Linux via `run.sh` + +## Convert binary file + +If you have a binary output from a meter available, you can convert it by using `xxd`: + +``` +xxd --include ehz_bin.bin >ehz_bin.h +``` diff --git a/lib/sml_parser/examples/native/ehz_bin.h b/lib/sml_parser/examples/native/ehz_bin.h new file mode 100644 index 0000000..7c15840 --- /dev/null +++ b/lib/sml_parser/examples/native/ehz_bin.h @@ -0,0 +1,118 @@ +// clang-format off +// Formatted example data from https://github.com/devZer0/libsml-testing + +const unsigned char ehz_bin[] = { + /* Start */ 0x1b, 0x1b, 0x1b, 0x1b, + /* Version */ 0x01, 0x01, 0x01, 0x01, + + /* List (6 entries) */ 0x76, + /* 1. Transaktion ID */ 0x07, 0x00, 0x0c, 0x04, 0x08, 0x87, 0x2d, + /* 2. Group No */ 0x62, 0x00, + /* 3. Abort On Error */ 0x62, 0x00, + /* 4. List (2 entries) */ 0x72, + /* 1. getOpenResponse */ 0x63, 0x01, 0x01, + /* 2. List (6 entries) */ 0x76, + /* 1. code page */ 0x01, + /* 2. clientid */ 0x01, + /* 3. Transaction ID */ 0x07, 0x00, 0x0c, 0x06, 0x9e, 0x2d, 0x0f, + /* 4. ServerID */ 0x0b, 0x06, 0x45, 0x4d, 0x48, 0x01, 0x00, 0x1d, 0x46, 0x15, 0xca, + /* 5. List name */ 0x01, + /* 6. Time */ 0x01, + /* 5. CRC */ 0x63, 0x2b, 0x8e, + /* 6. END */ 0x00, + + /* List (6 entries) */ 0x76, + /* 1. Transaktion ID */ 0x07, 0x00, 0x0c, 0x04, 0x08, 0x87, 0x2e, + /* 2. Group No */ 0x62, 0x00, + /* 3. Abort On Error */ 0x62, 0x00, + /* 4. Message Body (2) */ 0x72, + /* 1. getListResponse */ 0x63, 0x07, 0x01, + /* 2. List (7) */ 0x77, + /* 1. ClientId */ 0x01, + /* 2. ServerID */ 0x0b, 0x06, 0x45, 0x4d, 0x48, 0x01, 0x00, 0x1d, 0x46, 0x15, 0xca, + /* 3. ?? */ 0x01, + /* 4. List (2) */ 0x72, + /* 1. ?? */ 0x62, 0x01, + /* 2. ?? */ 0x65, 0x06, 0x9e, 0xfa, 0x83, + /* 5. List (7) */ 0x77, + /* 1. List (7) */ 0x77, + /* 1. OBIS */ 0x07, 0x81, 0x81, 0xc7, 0x82, 0x03, 0xff, + /* 2. */ 0x01, + /* 3. */ 0x01, + /* 4. */ 0x01, + /* 5. */ 0x01, + /* 6. */ 0x04, 0x45, 0x4d, 0x48, + /* 7. */ 0x01, + /* 2. List (7) */ 0x77, + /* 1. OBIS */ 0x07, 0x01, 0x00, 0x00, 0x00, 0x09, 0xff, + /* 2. */ 0x01, + /* 3. */ 0x01, + /* 4. */ 0x01, + /* 5. */ 0x01, + /* 6. */ 0x0b, 0x06, 0x45, 0x4d, 0x48, 0x01, 0x00, 0x1d, 0x46, 0x15, 0xca, + /* 7. */ 0x01, + /* 3. List (7) */ 0x77, + /* 1. OBIS */ 0x07, 0x01, 0x00, 0x01, 0x08, 0x00, 0xff, + /* 2. */ 0x63, 0x01, 0x82, + /* 3. */ 0x01, + /* 4. */ 0x62, 0x1e, + /* 5. */ 0x52, 0x03, + /* 6. */ 0x56, 0x00, 0x00, 0x00, 0x1C, 0x46, + /* 7. */ 0x01, + /* 4. List (7) */ 0x77, + /* 1. OBIS */ 0x07, 0x01, 0x00, 0x01, 0x08, 0x01, 0xff, + /* 2. */ 0x01, + /* 3. */ 0x01, + /* 4. Unit */ 0x62, 0x1e, + /* 5. Scaler */ 0x52, 0xff, + /* 6 Value */ 0x59, 0x00, 0x00, 0x00, 0x00, 0x07, 0x5b, 0xcd, 0x15, + /* 7 */ 0x01, + /* 5. List (7) */ 0x77, + /* 1. OBIS */ 0x07, 0x01, 0x00, 0x01, 0x08, 0x02, 0xff, + /* 2. */ 0x01, + /* 3. */ 0x01, + /* 4. */ 0x62, 0x1e, + /* 5. */ 0x52, 0x03, + /* 6. */ 0x56, 0x00, 0x00, 0x00, 0x1C, 0x46, + /* 7. */ 0x01, + /* 6. List (7) */ 0x77, + /* 1. OBIS */ 0x07, 0x01, 0x00, 0x0f, 0x07, 0x00, 0xff, + /* 2. */ 0x01, + /* 3. */ 0x01, + /* 4. */ 0x62, 0x1b, + /* 5. */ 0x52, 0xff, + /* 6. */ 0x55, 0x00, 0x00, 0x2f, 0x65, + /* 7. */ 0x01, + /* 7. List (7) */ 0x77, + /* 1. OBIS */ 0x07, 0x81, 0x81, 0xc7, 0x82, 0x05, 0xff, + /* 2. */ 0x01, + /* 3. */ 0x01, + /* 4. */ 0x01, + /* 5. */ 0x01, + /* 6. Public Key */ 0x83, 0x02, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + /* 7. */ 0x01, + 0x01, + 0x01, + 0x63, 0xb9, 0x3f, + 0x00, + /* List (6) */ 0x76, + /* 1. */ 0x07, 0x00, 0x0c, 0x04, 0x08, 0x87, 0x31, + /* 2. */ 0x62, 0x00, + /* 3. */ 0x62, 0x00, + /* 4. List (2) */ 0x72, + /* 1. */ 0x63, 0x02, 0x01, + /* List (1) */ 0x71, + /* 1. */ 0x01, + /* 5. CRC */ 0x63, 0x6a, 0x53, + /* 6. */ 0x00, + + 0x00, + 0x1b, 0x1b, 0x1b, 0x1b, + 0x1a, 0x01, 0xe8, 0xc6 +}; +const unsigned int ehz_bin_len = 319; + diff --git a/lib/sml_parser/examples/native/main.cpp b/lib/sml_parser/examples/native/main.cpp new file mode 100644 index 0000000..7dee038 --- /dev/null +++ b/lib/sml_parser/examples/native/main.cpp @@ -0,0 +1,67 @@ +#include "ehz_bin.h" +#include "sml.h" +#include + +#define MAX_STR_MANUF 5 +unsigned char manuf[MAX_STR_MANUF]; +double T1Wh = -2, SumWh = -2, Watt = -2; + +typedef struct { + const unsigned char OBIS[6]; + void (*Handler)(); +} OBISHandler; + +void Manufacturer() { smlOBISManufacturer(manuf, MAX_STR_MANUF); } + +void PowerT1() { smlOBISWh(T1Wh); } + +void PowerSum() { smlOBISWh(SumWh); } + +void PowerW() { smlOBISW(Watt); } + +// clang-format off +OBISHandler OBISHandlers[] = { + {{ 0x81, 0x81, 0xc7, 0x82, 0x03, 0xff }, &Manufacturer}, /* 129-129:199.130.3*255 */ + {{ 0x01, 0x00, 0x01, 0x08, 0x01, 0xff }, &PowerT1}, /* 1- 0: 1. 8.1*255 (T1) */ + {{ 0x01, 0x00, 0x01, 0x08, 0x00, 0xff }, &PowerSum}, /* 1- 0: 1. 8.0*255 (T1 + T2) */ + {{ 0x01, 0x00, 0x0F, 0x07, 0x00, 0xff }, &PowerW}, /* 1- 0: 15. 7.0*255 (Watt) */ + {{ 0, 0 }} +}; +// clang-format on + +int main() +{ + int i = 0, iHandler = 0; + unsigned char c; + sml_states_t s; + for (i = 0; i < ehz_bin_len; ++i) { + c = ehz_bin[i]; + s = smlState(c); + if (s == SML_START) { + /* reset local vars */ + manuf[0] = 0; + T1Wh = -3; + SumWh = -3; + } + if (s == SML_LISTEND) { + /* check handlers on last received list */ + for (iHandler = 0; OBISHandlers[iHandler].Handler != 0 && + !(smlOBISCheck(OBISHandlers[iHandler].OBIS)); + iHandler++) + ; + if (OBISHandlers[iHandler].Handler != 0) { + OBISHandlers[iHandler].Handler(); + } + } + if (s == SML_UNEXPECTED) { + printf(">>> Unexpected byte >%02X>> FINAL! Checksum OK\n"); + printf(">>> Manufacturer.............: %s\n", manuf); + printf(">>> Power T1 (1-0:1.8.1)..: %.3f Wh\n", T1Wh); + printf(">>> Power T1+T2 (1-0:1.8.0)..: %.3f Wh\n", SumWh); + printf(">>> Watt (1-0:15.7.0).: %.3f W\n\n", Watt); + } + } +} diff --git a/lib/sml_parser/examples/native/platformio.ini b/lib/sml_parser/examples/native/platformio.ini new file mode 100644 index 0000000..fc6b121 --- /dev/null +++ b/lib/sml_parser/examples/native/platformio.ini @@ -0,0 +1,7 @@ +[platformio] +src_dir = ./ + +[env:native] +platform = native +lib_deps = ../../src +build_flags = -D SML_DEBUG -D SML_NATIVE -Wall diff --git a/lib/sml_parser/examples/native/run.sh b/lib/sml_parser/examples/native/run.sh new file mode 100644 index 0000000..500f2a5 --- /dev/null +++ b/lib/sml_parser/examples/native/run.sh @@ -0,0 +1,5 @@ +#/bin/sh + +rm -rf .pio/libdeps/native/src +pio run +./.pio/build/native/program \ No newline at end of file diff --git a/lib/sml_parser/library.json b/lib/sml_parser/library.json new file mode 100644 index 0000000..4c0898e --- /dev/null +++ b/lib/sml_parser/library.json @@ -0,0 +1,20 @@ +{ + "name": "SML Parser", + "keywords": "SML, Smart Message Language, Parser, Smart Meter, OBIS", + "description": "Universal library that can parse SML (Smart Message Language) messages from smart meters. It's designed to be lightweight and efficient and has a small memory footprint, making it suitable for use on embedded systems or other devices with limited memory resources.", + "repository": { + "type": "git", + "url": "https://github.com/olliiiver/sml_parser.git" + }, + "authors": [ + { + "name": "Olliiiver", + "url": "https://github.com/olliiiver/", + "maintainer": true + } + ], + "license": "LGPL-2.1-only", + "version": "0.28", + "frameworks": "*", + "platforms": "*" +} diff --git a/lib/sml_parser/library.properties b/lib/sml_parser/library.properties new file mode 100644 index 0000000..2842624 --- /dev/null +++ b/lib/sml_parser/library.properties @@ -0,0 +1,11 @@ +name=SML-Parser +version=0.28 +author=Olliiiver +maintainer=Olliiiver +sentence=C++ library to parse Smart Message Language (SML) data from smart meters. +paragraph=SML smart meter parser +category=Data Processing +url=https://github.com/olliiiver/sml_parser +architectures=* +repository=https://github.com/olliiiver/sml_parser.git +license=LGPL-2.1-only diff --git a/lib/sml_parser/src/sml.cpp b/lib/sml_parser/src/sml.cpp new file mode 100644 index 0000000..17dbd63 --- /dev/null +++ b/lib/sml_parser/src/sml.cpp @@ -0,0 +1,421 @@ +#include +#include + +#include "sml.h" +#include "smlCrcTable.h" + +#ifdef SML_DEBUG +char logBuff[200]; + +#ifdef SML_NATIVE +#define SML_LOG(...) \ + do { \ + printf(__VA_ARGS__); \ + } while (0) +#define SML_TREELOG(level, ...) \ + do { \ + printf("%.*s", level, " "); \ + printf(__VA_ARGS__); \ + } while (0) +#elif ARDUINO +#include +#define SML_LOG(...) \ + do { \ + sprintf(logBuff, __VA_ARGS__); \ + Serial.print(logBuff); \ + } while (0) +#define SML_TREELOG(level, ...) \ + do { \ + sprintf(logBuff, __VA_ARGS__); \ + Serial.print(logBuff); \ + } while (0) +#endif + +#else +#define SML_LOG(...) \ + do { \ + } while (0) +#define SML_TREELOG(level, ...) \ + do { \ + } while (0) +#endif + +#define MAX_LIST_SIZE 80 +#define MAX_TREE_SIZE 10 + +static sml_states_t currentState = SML_START; +static char nodes[MAX_TREE_SIZE]; +static unsigned char currentLevel = 0; +static unsigned short crc = 0xFFFF; +static signed char sc; +static unsigned short crcMine = 0xFFFF; +static unsigned short crcReceived = 0x0000; +static unsigned char len = 4; +static unsigned char listBuffer[MAX_LIST_SIZE]; /* keeps a list + as length + state + data */ +static unsigned char listPos = 0; + +void crc16(unsigned char &byte) +{ +#ifdef ARDUINO + crc = + pgm_read_word_near(&smlCrcTable[(byte ^ crc) & 0xff]) ^ (crc >> 8 & 0xff); +#else + crc = smlCrcTable[(byte ^ crc) & 0xff] ^ (crc >> 8 & 0xff); +#endif +} + +void setState(sml_states_t state, int byteLen) +{ + currentState = state; + len = byteLen; +} + +void pushListBuffer(unsigned char byte) +{ + if (listPos < MAX_LIST_SIZE) { + listBuffer[listPos++] = byte; + } +} + +void reduceList() +{ + if (currentLevel >= 0 && nodes[currentLevel] > 0) + nodes[currentLevel]--; +} + +void smlNewList(unsigned char size) +{ + reduceList(); + if (currentLevel < MAX_TREE_SIZE) + currentLevel++; + nodes[currentLevel] = size; + SML_TREELOG(currentLevel, "LISTSTART on level %i with %i nodes\n", + currentLevel, size); + setState(SML_LISTSTART, size); + // @todo workaround for lists inside obis lists + if (size > 5) { + listPos = 0; + memset(listBuffer, '\0', MAX_LIST_SIZE); + } + else { + pushListBuffer(size); + pushListBuffer(currentState); + } +} + +void checkMagicByte(unsigned char &byte) +{ + unsigned int size = 0; + while (currentLevel > 0 && nodes[currentLevel] == 0) { + /* go back in tree if no nodes remaining */ + SML_TREELOG(currentLevel, "back to previous list\n"); + currentLevel--; + } + if (byte > 0x70 && byte <= 0x7F) { + /* new list */ + size = byte & 0x0F; + smlNewList(size); + } + else if (byte >= 0x01 && byte <= 0x6F && nodes[currentLevel] > 0) { + if (byte == 0x01) { + /* no data, get next */ + SML_TREELOG(currentLevel, " Data %i (empty)\n", nodes[currentLevel]); + pushListBuffer(0); + pushListBuffer(currentState); + if (nodes[currentLevel] == 1) { + setState(SML_LISTEND, 1); + SML_TREELOG(currentLevel, "LISTEND\n"); + } + else { + setState(SML_NEXT, 1); + } + } + else { + size = (byte & 0x0F) - 1; + setState(SML_DATA, size); + if ((byte & 0xF0) == 0x50) { + setState(SML_DATA_SIGNED_INT, size); + } + else if ((byte & 0xF0) == 0x60) { + setState(SML_DATA_UNSIGNED_INT, size); + } + else if ((byte & 0xF0) == 0x00) { + setState(SML_DATA_OCTET_STRING, size); + } + SML_TREELOG(currentLevel, + " Data %i (length = %i%s): ", nodes[currentLevel], size, + (currentState == SML_DATA_SIGNED_INT) ? ", signed int" + : (currentState == SML_DATA_UNSIGNED_INT) ? ", unsigned int" + : (currentState == SML_DATA_OCTET_STRING) ? ", octet string" + : ""); + pushListBuffer(size); + pushListBuffer(currentState); + } + reduceList(); + } + else if (byte == 0x00) { + /* end of block */ + reduceList(); + SML_TREELOG(currentLevel, "End of block at level %i\n", currentLevel); + if (currentLevel == 0) { + setState(SML_NEXT, 1); + } + else { + setState(SML_BLOCKEND, 1); + } + } + else if (byte & 0x80) { + // MSB bit is set, another TL byte will follow + if (byte >= 0x80 && byte <= 0x8F) { + // Datatype Octet String + setState(SML_HDATA, (byte & 0x0F) << 4); + } + else if (byte >= 0xF0 && byte <= 0xFF) { + /* Datatype List of ...*/ + setState(SML_LISTEXTENDED, (byte & 0x0F) << 4); + } + } + else if (byte == 0x1B && currentLevel == 0) { + /* end sequence */ + setState(SML_END, 3); + } + else { + /* Unexpected Byte */ + SML_TREELOG(currentLevel, + "UNEXPECTED magicbyte >%02X< at currentLevel %i\n", byte, + currentLevel); + setState(SML_UNEXPECTED, 4); + } +} + +sml_states_t smlState(unsigned char ¤tByte) +{ + unsigned char size; + if (len > 0) + len--; + crc16(currentByte); + switch (currentState) { + case SML_UNEXPECTED: + case SML_CHECKSUM_ERROR: + case SML_FINAL: + case SML_START: + currentState = SML_START; + currentLevel = 0; // Reset current level at the begin of a new transmission + // to prevent problems + if (currentByte != 0x1b) + setState(SML_UNEXPECTED, 4); + if (len == 0) { + SML_TREELOG(0, "START\n"); + /* completely clean any garbage from crc checksum */ + crc = 0xFFFF; + currentByte = 0x1b; + crc16(currentByte); + crc16(currentByte); + crc16(currentByte); + crc16(currentByte); + setState(SML_VERSION, 4); + } + break; + case SML_VERSION: + if (currentByte != 0x01) + setState(SML_UNEXPECTED, 4); + if (len == 0) { + setState(SML_BLOCKSTART, 1); + } + break; + case SML_END: + if (currentByte != 0x1b) { + SML_LOG("UNEXPECTED char >%02X< at SML_END\n", currentByte); + setState(SML_UNEXPECTED, 4); + } + if (len == 0) { + setState(SML_CHECKSUM, 4); + } + break; + case SML_CHECKSUM: + // SML_LOG("CHECK: %02X\n", currentByte); + if (len == 2) { + crcMine = crc ^ 0xFFFF; + } + if (len == 1) { + crcReceived += currentByte; + } + if (len == 0) { + crcReceived = crcReceived | (currentByte << 8); + SML_LOG("Received checksum: %02X\n", crcReceived); + SML_LOG("Calculated checksum: %02X\n", crcMine); + if (crcMine == crcReceived) { + setState(SML_FINAL, 4); + } + else { + setState(SML_CHECKSUM_ERROR, 4); + } + crc = 0xFFFF; + crcReceived = 0x000; /* reset CRC */ + } + break; + case SML_HDATA: + size = len + currentByte - 1; + setState(SML_DATA, size); + pushListBuffer(size); + pushListBuffer(currentState); + SML_TREELOG(currentLevel, " Data (length = %i): ", size); + break; + case SML_LISTEXTENDED: + size = len + (currentByte & 0x0F); + SML_TREELOG(currentLevel, "Extended List with Size=%i\n", size); + smlNewList(size); + break; + case SML_DATA: + case SML_DATA_SIGNED_INT: + case SML_DATA_UNSIGNED_INT: + case SML_DATA_OCTET_STRING: + SML_LOG("%02X ", currentByte); + pushListBuffer(currentByte); + if (nodes[currentLevel] == 0 && len == 0) { + SML_LOG("\n"); + SML_TREELOG(currentLevel, "LISTEND on level %i\n", currentLevel); + currentState = SML_LISTEND; + } + else if (len == 0) { + currentState = SML_DATAEND; + SML_LOG("\n"); + } + break; + case SML_DATAEND: + case SML_NEXT: + case SML_LISTSTART: + case SML_LISTEND: + case SML_BLOCKSTART: + case SML_BLOCKEND: + checkMagicByte(currentByte); + break; + } + return currentState; +} + +bool smlOBISCheck(const unsigned char *obis) +{ + return (memcmp(obis, &listBuffer[2], 6) == 0); +} + +void smlOBISManufacturer(unsigned char *str, int maxSize) +{ + int i = 0, pos = 0, size = 0; + while (i < listPos) { + size = (int)listBuffer[i]; + i++; + pos++; + if (pos == 6) { + /* get manufacturer at position 6 in list */ + size = (size > maxSize - 1) ? maxSize : size; + memcpy(str, &listBuffer[i + 1], size); + str[size + 1] = 0; + } + i += size + 1; + } +} + +void smlPow(double &val, signed char &scaler) +{ + if (scaler < 0) { + while (scaler++) { + val /= 10; + } + } + else { + while (scaler--) { + val *= 10; + } + } +} + +void smlOBISByUnit(long long int &val, signed char &scaler, sml_units_t unit) +{ + unsigned char i = 0, pos = 0, size = 0, y = 0, skip = 0; + sml_states_t type; + val = -1; /* unknown or error */ + while (i < listPos) { + pos++; + size = (int)listBuffer[i++]; + type = (sml_states_t)listBuffer[i++]; + if (type == SML_LISTSTART && size > 0) { + // skip a list inside an obis list + skip = size; + while (skip > 0) { + size = (int)listBuffer[i++]; + type = (sml_states_t)listBuffer[i++]; + i += size; + skip--; + } + size = 0; + } + if (pos == 4 && listBuffer[i] != unit) { + /* return unknown (-1) if unit does not match */ + return; + } + if (pos == 5) { + scaler = listBuffer[i]; + } + if (pos == 6) { + y = size; + // initialize 64bit signed integer based on MSB from received value + val = + (type == SML_DATA_SIGNED_INT && (listBuffer[i] & (1 << 7))) ? ~0 : 0; + for (y = 0; y < size; y++) { + // left shift received bytes to 64 bit signed integer + val = (val << 8) | listBuffer[i + y]; + } + } + i += size; + } +} + +void smlOBISWh(double &wh) +{ + long long int val; + smlOBISByUnit(val, sc, SML_WATT_HOUR); + wh = val; + smlPow(wh, sc); +} + +void smlOBISW(double &w) +{ + long long int val; + smlOBISByUnit(val, sc, SML_WATT); + w = val; + smlPow(w, sc); +} + +void smlOBISVolt(double &v) +{ + long long int val; + smlOBISByUnit(val, sc, SML_VOLT); + v = val; + smlPow(v, sc); +} + +void smlOBISAmpere(double &a) +{ + long long int val; + smlOBISByUnit(val, sc, SML_AMPERE); + a = val; + smlPow(a, sc); +} + +void smlOBISHertz(double &h) +{ + long long int val; + smlOBISByUnit(val, sc, SML_HERTZ); + h = val; + smlPow(h, sc); +} + +void smlOBISDegree(double &d) +{ + long long int val; + smlOBISByUnit(val, sc, SML_DEGREE); + d = val; + smlPow(d, sc); +} diff --git a/lib/sml_parser/src/sml.h b/lib/sml_parser/src/sml.h new file mode 100644 index 0000000..3f774dc --- /dev/null +++ b/lib/sml_parser/src/sml.h @@ -0,0 +1,109 @@ +#ifndef SML_H +#define SML_H + +#include + +typedef enum { + SML_START, + SML_END, + SML_VERSION, + SML_NEXT, + SML_LISTSTART, + SML_LISTEND, + SML_LISTEXTENDED, + SML_DATA, + SML_HDATA, + SML_DATAEND, + SML_BLOCKSTART, + SML_BLOCKEND, + SML_CHECKSUM, + SML_CHECKSUM_ERROR, /* calculated checksum does not match */ + SML_UNEXPECTED, /* unexpected byte received */ + SML_FINAL, /* final state, checksum OK */ + SML_DATA_SIGNED_INT, + SML_DATA_UNSIGNED_INT, + SML_DATA_OCTET_STRING, +} sml_states_t; + +typedef enum { + SML_YEAR = 1, + SML_MONTH = 2, + SML_WEEK = 3, + SML_DAY = 4, + SML_HOUR = 5, + SML_MIN = 6, + SML_SECOND = 7, + SML_DEGREE = 8, + SML_DEGREE_CELSIUS = 9, + SML_CURRENCY = 10, + SML_METRE = 11, + SML_METRE_PER_SECOND = 12, + SML_CUBIC_METRE = 13, + SML_CUBIC_METRE_CORRECTED = 14, + SML_CUBIC_METRE_PER_HOUR = 15, + SML_CUBIC_METRE_PER_HOUR_CORRECTED = 16, + SML_CUBIC_METRE_PER_DAY = 17, + SML_CUBIC_METRE_PER_DAY_CORRECTED = 18, + SML_LITRE = 19, + SML_KILOGRAM = 20, + SML_NEWTON = 21, + SML_NEWTONMETER = 22, + SML_PASCAL = 23, + SML_BAR = 24, + SML_JOULE = 25, + SML_JOULE_PER_HOUR = 26, + SML_WATT = 27, + SML_VOLT_AMPERE = 28, + SML_VAR = 29, + SML_WATT_HOUR = 30, + SML_VOLT_AMPERE_HOUR = 31, + SML_VAR_HOUR = 32, + SML_AMPERE = 33, + SML_COULOMB = 34, + SML_VOLT = 35, + SML_VOLT_PER_METRE = 36, + SML_FARAD = 37, + SML_OHM = 38, + SML_OHM_METRE = 39, + SML_WEBER = 40, + SML_TESLA = 41, + SML_AMPERE_PER_METRE = 42, + SML_HENRY = 43, + SML_HERTZ = 44, + SML_ACTIVE_ENERGY_METER_CONSTANT_OR_PULSE_VALUE = 45, + SML_REACTIVE_ENERGY_METER_CONSTANT_OR_PULSE_VALUE = 46, + SML_APPARENT_ENERGY_METER_CONSTANT_OR_PULSE_VALUE = 47, + SML_VOLT_SQUARED_HOURS = 48, + SML_AMPERE_SQUARED_HOURS = 49, + SML_KILOGRAM_PER_SECOND = 50, + SML_KELVIN = 52, + SML_VOLT_SQUARED_HOUR_METER_CONSTANT_OR_PULSE_VALUE = 53, + SML_AMPERE_SQUARED_HOUR_METER_CONSTANT_OR_PULSE_VALUE = 54, + SML_METER_CONSTANT_OR_PULSE_VALUE = 55, + SML_PERCENTAGE = 56, + SML_AMPERE_HOUR = 57, + SML_ENERGY_PER_VOLUME = 60, + SML_CALORIFIC_VALUE = 61, + SML_MOLE_PERCENT = 62, + SML_MASS_DENSITY = 63, + SML_PASCAL_SECOND = 64, + SML_RESERVED = 253, + SML_OTHER_UNIT = 254, + SML_COUNT = 255 +} sml_units_t; + +sml_states_t smlState(unsigned char &byte); +bool smlOBISCheck(const unsigned char *obis); +void smlOBISManufacturer(unsigned char *str, int maxSize); +void smlOBISByUnit(long long int &wh, signed char &scaler, sml_units_t unit); + +// Be aware that double on Arduino UNO is just 32 bit +void smlOBISWh(double &wh); +void smlOBISW(double &w); +void smlOBISVolt(double &v); +void smlOBISAmpere(double &a); +void smlOBISHertz(double &h); +void smlOBISDegree(double &d); + + +#endif diff --git a/lib/sml_parser/src/smlCrcTable.h b/lib/sml_parser/src/smlCrcTable.h new file mode 100644 index 0000000..8f6c1c1 --- /dev/null +++ b/lib/sml_parser/src/smlCrcTable.h @@ -0,0 +1,42 @@ +#ifndef SML_CRC_TABLE_H +#define SML_CRC_TABLE_H + +#include + +#ifdef ARDUINO +#include +static const uint16_t smlCrcTable[256] PROGMEM = +#else +static const uint16_t smlCrcTable[256] = +#endif + {0x0000, 0x1189, 0x2312, 0x329B, 0x4624, 0x57AD, 0x6536, 0x74BF, 0x8C48, + 0x9DC1, 0xAF5A, 0xBED3, 0xCA6C, 0xDBE5, 0xE97E, 0xF8F7, 0x1081, 0x0108, + 0x3393, 0x221A, 0x56A5, 0x472C, 0x75B7, 0x643E, 0x9CC9, 0x8D40, 0xBFDB, + 0xAE52, 0xDAED, 0xCB64, 0xF9FF, 0xE876, 0x2102, 0x308B, 0x0210, 0x1399, + 0x6726, 0x76AF, 0x4434, 0x55BD, 0xAD4A, 0xBCC3, 0x8E58, 0x9FD1, 0xEB6E, + 0xFAE7, 0xC87C, 0xD9F5, 0x3183, 0x200A, 0x1291, 0x0318, 0x77A7, 0x662E, + 0x54B5, 0x453C, 0xBDCB, 0xAC42, 0x9ED9, 0x8F50, 0xFBEF, 0xEA66, 0xD8FD, + 0xC974, 0x4204, 0x538D, 0x6116, 0x709F, 0x0420, 0x15A9, 0x2732, 0x36BB, + 0xCE4C, 0xDFC5, 0xED5E, 0xFCD7, 0x8868, 0x99E1, 0xAB7A, 0xBAF3, 0x5285, + 0x430C, 0x7197, 0x601E, 0x14A1, 0x0528, 0x37B3, 0x263A, 0xDECD, 0xCF44, + 0xFDDF, 0xEC56, 0x98E9, 0x8960, 0xBBFB, 0xAA72, 0x6306, 0x728F, 0x4014, + 0x519D, 0x2522, 0x34AB, 0x0630, 0x17B9, 0xEF4E, 0xFEC7, 0xCC5C, 0xDDD5, + 0xA96A, 0xB8E3, 0x8A78, 0x9BF1, 0x7387, 0x620E, 0x5095, 0x411C, 0x35A3, + 0x242A, 0x16B1, 0x0738, 0xFFCF, 0xEE46, 0xDCDD, 0xCD54, 0xB9EB, 0xA862, + 0x9AF9, 0x8B70, 0x8408, 0x9581, 0xA71A, 0xB693, 0xC22C, 0xD3A5, 0xE13E, + 0xF0B7, 0x0840, 0x19C9, 0x2B52, 0x3ADB, 0x4E64, 0x5FED, 0x6D76, 0x7CFF, + 0x9489, 0x8500, 0xB79B, 0xA612, 0xD2AD, 0xC324, 0xF1BF, 0xE036, 0x18C1, + 0x0948, 0x3BD3, 0x2A5A, 0x5EE5, 0x4F6C, 0x7DF7, 0x6C7E, 0xA50A, 0xB483, + 0x8618, 0x9791, 0xE32E, 0xF2A7, 0xC03C, 0xD1B5, 0x2942, 0x38CB, 0x0A50, + 0x1BD9, 0x6F66, 0x7EEF, 0x4C74, 0x5DFD, 0xB58B, 0xA402, 0x9699, 0x8710, + 0xF3AF, 0xE226, 0xD0BD, 0xC134, 0x39C3, 0x284A, 0x1AD1, 0x0B58, 0x7FE7, + 0x6E6E, 0x5CF5, 0x4D7C, 0xC60C, 0xD785, 0xE51E, 0xF497, 0x8028, 0x91A1, + 0xA33A, 0xB2B3, 0x4A44, 0x5BCD, 0x6956, 0x78DF, 0x0C60, 0x1DE9, 0x2F72, + 0x3EFB, 0xD68D, 0xC704, 0xF59F, 0xE416, 0x90A9, 0x8120, 0xB3BB, 0xA232, + 0x5AC5, 0x4B4C, 0x79D7, 0x685E, 0x1CE1, 0x0D68, 0x3FF3, 0x2E7A, 0xE70E, + 0xF687, 0xC41C, 0xD595, 0xA12A, 0xB0A3, 0x8238, 0x93B1, 0x6B46, 0x7ACF, + 0x4854, 0x59DD, 0x2D62, 0x3CEB, 0x0E70, 0x1FF9, 0xF78F, 0xE606, 0xD49D, + 0xC514, 0xB1AB, 0xA022, 0x92B9, 0x8330, 0x7BC7, 0x6A4E, 0x58D5, 0x495C, + 0x3DE3, 0x2C6A, 0x1EF1, 0x0F78}; + +#endif \ No newline at end of file diff --git a/lib/sml_parser/test/README.md b/lib/sml_parser/test/README.md new file mode 100644 index 0000000..026e13c --- /dev/null +++ b/lib/sml_parser/test/README.md @@ -0,0 +1,9 @@ +# Unity tests + +Test the library on our local machine (native) or on MCUs. + +Execute only local tests: + +``` +pio test -e native +``` diff --git a/lib/sml_parser/test/platformio.ini b/lib/sml_parser/test/platformio.ini new file mode 100644 index 0000000..5f5da48 --- /dev/null +++ b/lib/sml_parser/test/platformio.ini @@ -0,0 +1,16 @@ +[platformio] +src_dir = ./ + +[env:native] +platform = native +lib_deps = ../src +build_flags = -D SML_DEBUG -D SML_NATIVE -Wall -D UNITY_INCLUDE_DOUBLE + +[env:uno] +platform = atmelavr +board = uno +framework = arduino +;board_build.mcu = atmega328p +;board_build.f_cpu = 16000000L +build_flags = -D SML_DEBUG -D UNITY_INCLUDE_DOUBLE +lib_deps = ../src diff --git a/lib/sml_parser/test/test/test_56bit/ehz_bin.h b/lib/sml_parser/test/test/test_56bit/ehz_bin.h new file mode 100644 index 0000000..c5f39b3 --- /dev/null +++ b/lib/sml_parser/test/test/test_56bit/ehz_bin.h @@ -0,0 +1,118 @@ +// clang-format off +// Formatted example data from https://github.com/devZer0/libsml-testing + +const unsigned char ehz_bin[] = { + /* Start */ 0x1b, 0x1b, 0x1b, 0x1b, + /* Version */ 0x01, 0x01, 0x01, 0x01, + + /* List (6 entries) */ 0x76, + /* 1. Transaktion ID */ 0x07, 0x00, 0x0c, 0x04, 0x08, 0x87, 0x2d, + /* 2. Group No */ 0x62, 0x00, + /* 3. Abort On Error */ 0x62, 0x00, + /* 4. List (2 entries) */ 0x72, + /* 1. getOpenResponse */ 0x63, 0x01, 0x01, + /* 2. List (6 entries) */ 0x76, + /* 1. code page */ 0x01, + /* 2. clientid */ 0x01, + /* 3. Transaction ID */ 0x07, 0x00, 0x0c, 0x06, 0x9e, 0x2d, 0x0f, + /* 4. ServerID */ 0x0b, 0x06, 0x45, 0x4d, 0x48, 0x01, 0x00, 0x1d, 0x46, 0x15, 0xca, + /* 5. List name */ 0x01, + /* 6. Time */ 0x01, + /* 5. CRC */ 0x63, 0x2b, 0x8e, + /* 6. END */ 0x00, + + /* List (6 entries) */ 0x76, + /* 1. Transaktion ID */ 0x07, 0x00, 0x0c, 0x04, 0x08, 0x87, 0x2e, + /* 2. Group No */ 0x62, 0x00, + /* 3. Abort On Error */ 0x62, 0x00, + /* 4. Message Body (2) */ 0x72, + /* 1. getListResponse */ 0x63, 0x07, 0x01, + /* 2. List (7) */ 0x77, + /* 1. ClientId */ 0x01, + /* 2. ServerID */ 0x0b, 0x06, 0x45, 0x4d, 0x48, 0x01, 0x00, 0x1d, 0x46, 0x15, 0xca, + /* 3. ?? */ 0x01, + /* 4. List (2) */ 0x72, + /* 1. ?? */ 0x62, 0x01, + /* 2. ?? */ 0x65, 0x06, 0x9e, 0xfa, 0x83, + /* 5. List (7) */ 0x77, + /* 1. List (7) */ 0x77, + /* 1. OBIS */ 0x07, 0x81, 0x81, 0xc7, 0x82, 0x03, 0xff, + /* 2. */ 0x01, + /* 3. */ 0x01, + /* 4. */ 0x01, + /* 5. */ 0x01, + /* 6. */ 0x04, 0x45, 0x4d, 0x48, + /* 7. */ 0x01, + /* 2. List (7) */ 0x77, + /* 1. OBIS */ 0x07, 0x01, 0x00, 0x00, 0x00, 0x09, 0xff, + /* 2. */ 0x01, + /* 3. */ 0x01, + /* 4. */ 0x01, + /* 5. */ 0x01, + /* 6. */ 0x0b, 0x06, 0x45, 0x4d, 0x48, 0x01, 0x00, 0x1d, 0x46, 0x15, 0xca, + /* 7. */ 0x01, + /* 3. List (7) */ 0x77, + /* 1. OBIS */ 0x07, 0x01, 0x00, 0x01, 0x08, 0x00, 0xff, + /* 2. */ 0x63, 0x01, 0x82, + /* 3. */ 0x01, + /* 4. */ 0x62, 0x1e, + /* 5. */ 0x52, 0x03, + /* 6. */ 0x56, 0x00, 0x00, 0x00, 0x1C, 0x46, + /* 7. */ 0x01, + /* 4. List (7) */ 0x77, + /* 1. OBIS */ 0x07, 0x01, 0x00, 0x01, 0x08, 0x01, 0xff, + /* 2. */ 0x01, + /* 3. */ 0x01, + /* 4. Unit */ 0x62, 0x1e, + /* 5. Scaler */ 0x52, 0xff, + /* 6 Value */ 0x59, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, + /* 7 */ 0x01, + /* 5. List (7) */ 0x77, + /* 1. OBIS */ 0x07, 0x01, 0x00, 0x01, 0x08, 0x02, 0xff, + /* 2. */ 0x01, + /* 3. */ 0x01, + /* 4. */ 0x62, 0x1e, + /* 5. */ 0x52, 0x03, + /* 6. */ 0x56, 0x00, 0x00, 0x00, 0x1C, 0x46, + /* 7. */ 0x01, + /* 6. List (7) */ 0x77, + /* 1. OBIS */ 0x07, 0x01, 0x00, 0x0f, 0x07, 0x00, 0xff, + /* 2. */ 0x01, + /* 3. */ 0x01, + /* 4. */ 0x62, 0x1b, + /* 5. */ 0x52, 0xff, + /* 6. */ 0x55, 0x00, 0x00, 0x2f, 0x65, + /* 7. */ 0x01, + /* 7. List (7) */ 0x77, + /* 1. OBIS */ 0x07, 0x81, 0x81, 0xc7, 0x82, 0x05, 0xff, + /* 2. */ 0x01, + /* 3. */ 0x01, + /* 4. */ 0x01, + /* 5. */ 0x01, + /* 6. Public Key */ 0x83, 0x02, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + /* 7. */ 0x01, + 0x01, + 0x01, + 0x63, 0xb9, 0x3f, + 0x00, + /* List (6) */ 0x76, + /* 1. */ 0x07, 0x00, 0x0c, 0x04, 0x08, 0x87, 0x31, + /* 2. */ 0x62, 0x00, + /* 3. */ 0x62, 0x00, + /* 4. List (2) */ 0x72, + /* 1. */ 0x63, 0x02, 0x01, + /* List (1) */ 0x71, + /* 1. */ 0x01, + /* 5. CRC */ 0x63, 0x6a, 0x53, + /* 6. */ 0x00, + + 0x00, + 0x1b, 0x1b, 0x1b, 0x1b, + 0x1a, 0x01, 0xba, 0x1a +}; +const unsigned int ehz_bin_len = 319; + diff --git a/lib/sml_parser/test/test/test_56bit/test.cpp b/lib/sml_parser/test/test/test_56bit/test.cpp new file mode 100644 index 0000000..acc6775 --- /dev/null +++ b/lib/sml_parser/test/test/test_56bit/test.cpp @@ -0,0 +1,113 @@ +#include "ehz_bin.h" +#include "sml.h" +#include "unity.h" +#ifdef ARDUINO +#include "arduino.h" +#endif + +typedef struct { + const unsigned char OBIS[6]; + void (*Handler)(); +} OBISHandler; + +#define MAX_STR_MANUF 5 +unsigned char manuf[MAX_STR_MANUF]; +double SumWh = -2; +long long int T1Wh = -2; +bool isFinal = false; + +void Manufacturer() { smlOBISManufacturer(manuf, MAX_STR_MANUF); } +void PowerT1() +{ + signed char sc = 0; + smlOBISByUnit(T1Wh, sc, SML_WATT_HOUR); +} + +void PowerSum() { smlOBISWh(SumWh); } + +// clang-format off +OBISHandler OBISHandlers[] = { + {{ 0x81, 0x81, 0xc7, 0x82, 0x03, 0xff }, &Manufacturer}, /* 129-129:199.130.3*255 */ + {{ 0x01, 0x00, 0x01, 0x08, 0x01, 0xff }, &PowerT1}, /* 1- 0: 1. 8.1*255 (T1) */ + {{ 0x01, 0x00, 0x01, 0x08, 0x00, 0xff }, &PowerSum}, /* 1- 0: 1. 8.0*255 (T1 + T2) */ + {{ 0, 0 }} +}; +// clang-format on + +void setUp(void) +{ + unsigned int i = 0; + int iHandler = 0; + unsigned char c; + sml_states_t s; + manuf[0] = 0; + + for (i = 0; i < ehz_bin_len; ++i) { + c = ehz_bin[i]; + s = smlState(c); + if (s == SML_LISTEND) { + /* check handlers on last received list */ + for (iHandler = 0; OBISHandlers[iHandler].Handler != 0 && + !(smlOBISCheck(OBISHandlers[iHandler].OBIS)); + iHandler++) + ; + if (OBISHandlers[iHandler].Handler != 0) { + OBISHandlers[iHandler].Handler(); + } + } + if (s == SML_FINAL) { + isFinal = true; + } + } +} + +void test_should_return_manufacturer(void) +{ + TEST_ASSERT_EQUAL_STRING("EMH", manuf); +} + +void test_should_return_t1_56bit(void) +{ + TEST_ASSERT_EQUAL_INT(72057594037927935, T1Wh); +} + +void test_should_return_SumWh(void) +{ + TEST_ASSERT_EQUAL_DOUBLE(7238000, SumWh); +} + +void test_should_be_final(void) { TEST_ASSERT_EQUAL_INT(1, isFinal); } + +int runUnityTests(void) +{ + UNITY_BEGIN(); + RUN_TEST(test_should_return_manufacturer); + RUN_TEST(test_should_return_t1_56bit); + RUN_TEST(test_should_return_SumWh); + RUN_TEST(test_should_be_final); + return UNITY_END(); +} + +/** + * For native dev-platform or for some embedded frameworks + */ +int main(void) { return runUnityTests(); } + +/** + * For Arduino framework + */ +void setup() +{ +// Wait ~2 seconds before the Unity test runner +// establishes connection with a board Serial interface +#ifdef ARDUINO + delay(2000); +#endif + runUnityTests(); +} +void loop() {} + +/** + * For ESP-IDF framework + */ +void app_main() { runUnityTests(); } diff --git a/lib/sml_parser/test/test/test_EMH/ehz_bin.h b/lib/sml_parser/test/test/test_EMH/ehz_bin.h new file mode 100644 index 0000000..7c15840 --- /dev/null +++ b/lib/sml_parser/test/test/test_EMH/ehz_bin.h @@ -0,0 +1,118 @@ +// clang-format off +// Formatted example data from https://github.com/devZer0/libsml-testing + +const unsigned char ehz_bin[] = { + /* Start */ 0x1b, 0x1b, 0x1b, 0x1b, + /* Version */ 0x01, 0x01, 0x01, 0x01, + + /* List (6 entries) */ 0x76, + /* 1. Transaktion ID */ 0x07, 0x00, 0x0c, 0x04, 0x08, 0x87, 0x2d, + /* 2. Group No */ 0x62, 0x00, + /* 3. Abort On Error */ 0x62, 0x00, + /* 4. List (2 entries) */ 0x72, + /* 1. getOpenResponse */ 0x63, 0x01, 0x01, + /* 2. List (6 entries) */ 0x76, + /* 1. code page */ 0x01, + /* 2. clientid */ 0x01, + /* 3. Transaction ID */ 0x07, 0x00, 0x0c, 0x06, 0x9e, 0x2d, 0x0f, + /* 4. ServerID */ 0x0b, 0x06, 0x45, 0x4d, 0x48, 0x01, 0x00, 0x1d, 0x46, 0x15, 0xca, + /* 5. List name */ 0x01, + /* 6. Time */ 0x01, + /* 5. CRC */ 0x63, 0x2b, 0x8e, + /* 6. END */ 0x00, + + /* List (6 entries) */ 0x76, + /* 1. Transaktion ID */ 0x07, 0x00, 0x0c, 0x04, 0x08, 0x87, 0x2e, + /* 2. Group No */ 0x62, 0x00, + /* 3. Abort On Error */ 0x62, 0x00, + /* 4. Message Body (2) */ 0x72, + /* 1. getListResponse */ 0x63, 0x07, 0x01, + /* 2. List (7) */ 0x77, + /* 1. ClientId */ 0x01, + /* 2. ServerID */ 0x0b, 0x06, 0x45, 0x4d, 0x48, 0x01, 0x00, 0x1d, 0x46, 0x15, 0xca, + /* 3. ?? */ 0x01, + /* 4. List (2) */ 0x72, + /* 1. ?? */ 0x62, 0x01, + /* 2. ?? */ 0x65, 0x06, 0x9e, 0xfa, 0x83, + /* 5. List (7) */ 0x77, + /* 1. List (7) */ 0x77, + /* 1. OBIS */ 0x07, 0x81, 0x81, 0xc7, 0x82, 0x03, 0xff, + /* 2. */ 0x01, + /* 3. */ 0x01, + /* 4. */ 0x01, + /* 5. */ 0x01, + /* 6. */ 0x04, 0x45, 0x4d, 0x48, + /* 7. */ 0x01, + /* 2. List (7) */ 0x77, + /* 1. OBIS */ 0x07, 0x01, 0x00, 0x00, 0x00, 0x09, 0xff, + /* 2. */ 0x01, + /* 3. */ 0x01, + /* 4. */ 0x01, + /* 5. */ 0x01, + /* 6. */ 0x0b, 0x06, 0x45, 0x4d, 0x48, 0x01, 0x00, 0x1d, 0x46, 0x15, 0xca, + /* 7. */ 0x01, + /* 3. List (7) */ 0x77, + /* 1. OBIS */ 0x07, 0x01, 0x00, 0x01, 0x08, 0x00, 0xff, + /* 2. */ 0x63, 0x01, 0x82, + /* 3. */ 0x01, + /* 4. */ 0x62, 0x1e, + /* 5. */ 0x52, 0x03, + /* 6. */ 0x56, 0x00, 0x00, 0x00, 0x1C, 0x46, + /* 7. */ 0x01, + /* 4. List (7) */ 0x77, + /* 1. OBIS */ 0x07, 0x01, 0x00, 0x01, 0x08, 0x01, 0xff, + /* 2. */ 0x01, + /* 3. */ 0x01, + /* 4. Unit */ 0x62, 0x1e, + /* 5. Scaler */ 0x52, 0xff, + /* 6 Value */ 0x59, 0x00, 0x00, 0x00, 0x00, 0x07, 0x5b, 0xcd, 0x15, + /* 7 */ 0x01, + /* 5. List (7) */ 0x77, + /* 1. OBIS */ 0x07, 0x01, 0x00, 0x01, 0x08, 0x02, 0xff, + /* 2. */ 0x01, + /* 3. */ 0x01, + /* 4. */ 0x62, 0x1e, + /* 5. */ 0x52, 0x03, + /* 6. */ 0x56, 0x00, 0x00, 0x00, 0x1C, 0x46, + /* 7. */ 0x01, + /* 6. List (7) */ 0x77, + /* 1. OBIS */ 0x07, 0x01, 0x00, 0x0f, 0x07, 0x00, 0xff, + /* 2. */ 0x01, + /* 3. */ 0x01, + /* 4. */ 0x62, 0x1b, + /* 5. */ 0x52, 0xff, + /* 6. */ 0x55, 0x00, 0x00, 0x2f, 0x65, + /* 7. */ 0x01, + /* 7. List (7) */ 0x77, + /* 1. OBIS */ 0x07, 0x81, 0x81, 0xc7, 0x82, 0x05, 0xff, + /* 2. */ 0x01, + /* 3. */ 0x01, + /* 4. */ 0x01, + /* 5. */ 0x01, + /* 6. Public Key */ 0x83, 0x02, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + /* 7. */ 0x01, + 0x01, + 0x01, + 0x63, 0xb9, 0x3f, + 0x00, + /* List (6) */ 0x76, + /* 1. */ 0x07, 0x00, 0x0c, 0x04, 0x08, 0x87, 0x31, + /* 2. */ 0x62, 0x00, + /* 3. */ 0x62, 0x00, + /* 4. List (2) */ 0x72, + /* 1. */ 0x63, 0x02, 0x01, + /* List (1) */ 0x71, + /* 1. */ 0x01, + /* 5. CRC */ 0x63, 0x6a, 0x53, + /* 6. */ 0x00, + + 0x00, + 0x1b, 0x1b, 0x1b, 0x1b, + 0x1a, 0x01, 0xe8, 0xc6 +}; +const unsigned int ehz_bin_len = 319; + diff --git a/lib/sml_parser/test/test/test_EMH/test.cpp b/lib/sml_parser/test/test/test_EMH/test.cpp new file mode 100644 index 0000000..6f28352 --- /dev/null +++ b/lib/sml_parser/test/test/test_EMH/test.cpp @@ -0,0 +1,104 @@ +#include "ehz_bin.h" +#include "sml.h" +#include "unity.h" +#ifdef ARDUINO +#include "arduino.h" +#endif + +typedef struct { + const unsigned char OBIS[6]; + void (*Handler)(); +} OBISHandler; + +#define MAX_STR_MANUF 5 +unsigned char manuf[MAX_STR_MANUF]; +double T1Wh = -2, SumWh = -2; +bool isFinal = false; + +void Manufacturer() { smlOBISManufacturer(manuf, MAX_STR_MANUF); } +void PowerT1() { smlOBISWh(T1Wh); } +void PowerSum() { smlOBISWh(SumWh); } + +// clang-format off +OBISHandler OBISHandlers[] = { + {{ 0x81, 0x81, 0xc7, 0x82, 0x03, 0xff }, &Manufacturer}, /* 129-129:199.130.3*255 */ + {{ 0x01, 0x00, 0x01, 0x08, 0x01, 0xff }, &PowerT1}, /* 1- 0: 1. 8.1*255 (T1) */ + {{ 0x01, 0x00, 0x01, 0x08, 0x00, 0xff }, &PowerSum}, /* 1- 0: 1. 8.0*255 (T1 + T2) */ + {{ 0, 0 }} +}; +// clang-format on + +void setUp(void) +{ + unsigned int i = 0; + int iHandler = 0; + unsigned char c; + sml_states_t s; + manuf[0] = 0; + + for (i = 0; i < ehz_bin_len; ++i) { + c = ehz_bin[i]; + s = smlState(c); + if (s == SML_LISTEND) { + /* check handlers on last received list */ + for (iHandler = 0; OBISHandlers[iHandler].Handler != 0 && + !(smlOBISCheck(OBISHandlers[iHandler].OBIS)); + iHandler++) + ; + if (OBISHandlers[iHandler].Handler != 0) { + OBISHandlers[iHandler].Handler(); + } + } + if (s == SML_FINAL) { + isFinal = true; + } + } +} + +void test_should_return_manufacturer(void) +{ + TEST_ASSERT_EQUAL_STRING("EMH", manuf); +} + +void test_should_return_t1(void) { TEST_ASSERT_EQUAL_DOUBLE(12345678.9, T1Wh); } + +void test_should_return_SumWh(void) +{ + TEST_ASSERT_EQUAL_DOUBLE(7238000, SumWh); +} + +void test_should_be_final(void) { TEST_ASSERT_EQUAL_INT(1, isFinal); } + +int runUnityTests(void) +{ + UNITY_BEGIN(); + RUN_TEST(test_should_return_manufacturer); + RUN_TEST(test_should_return_t1); + RUN_TEST(test_should_return_SumWh); + RUN_TEST(test_should_be_final); + return UNITY_END(); +} + +/** + * For native dev-platform or for some embedded frameworks + */ +int main(void) { return runUnityTests(); } + +/** + * For Arduino framework + */ +void setup() +{ +// Wait ~2 seconds before the Unity test runner +// establishes connection with a board Serial interface +#ifdef ARDUINO + delay(2000); +#endif + runUnityTests(); +} +void loop() {} + +/** + * For ESP-IDF framework + */ +void app_main() { runUnityTests(); } diff --git a/lib/sml_parser/test/test/test_ESY/data_bin.h b/lib/sml_parser/test/test/test_ESY/data_bin.h new file mode 100644 index 0000000..cc2a62f --- /dev/null +++ b/lib/sml_parser/test/test/test_ESY/data_bin.h @@ -0,0 +1,350 @@ + +#ifdef ARDUINO +#include +static const unsigned char data_bin[] PROGMEM = +#else +static const unsigned char data_bin[] = +#endif + {0x76, 0x0b, 0x45, 0x53, 0x59, 0x41, 0x99, 0xa5, 0x02, 0xea, 0x9a, 0xf2, + 0x62, 0x00, 0x62, 0x00, 0x72, 0x63, 0x07, 0x01, 0x77, 0x01, 0x0b, 0x09, + 0x01, 0x45, 0x53, 0x59, 0x11, 0x03, 0xb5, 0x99, 0xa5, 0x07, 0x01, 0x00, + 0x62, 0x0a, 0xff, 0xff, 0x72, 0x62, 0x01, 0x65, 0x00, 0xf8, 0xdf, 0x6d, + 0x7e, 0x77, 0x07, 0x81, 0x81, 0xc7, 0x82, 0x03, 0xff, 0x01, 0x01, 0x01, + 0x01, 0x04, 0x45, 0x53, 0x59, 0x01, 0x77, 0x07, 0x01, 0x00, 0x00, 0x00, + 0x09, 0xff, 0x01, 0x01, 0x01, 0x01, 0x0b, 0x09, 0x01, 0x45, 0x53, 0x59, + 0x11, 0x03, 0xb5, 0x99, 0xa5, 0x01, 0x77, 0x07, 0x01, 0x00, 0x01, 0x08, + 0x00, 0xff, 0x64, 0x00, 0x00, 0x80, 0x01, 0x62, 0x1e, 0x52, 0xfc, 0x59, + 0x00, 0x00, 0x00, 0x06, 0xd9, 0x5b, 0x83, 0x57, 0x01, 0x77, 0x07, 0x01, + 0x00, 0x02, 0x08, 0x00, 0xff, 0x64, 0x00, 0x00, 0x80, 0x01, 0x62, 0x1e, + 0x52, 0xfc, 0x59, 0x00, 0x00, 0x00, 0x00, 0x41, 0x9b, 0xd4, 0xd3, 0x01, + 0x77, 0x07, 0x01, 0x00, 0x10, 0x07, 0x00, 0xff, 0x01, 0x01, 0x62, 0x1b, + 0x52, 0xfe, 0x59, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x2a, 0x16, 0x01, + 0x77, 0x07, 0x01, 0x00, 0x24, 0x07, 0x00, 0xff, 0x01, 0x01, 0x62, 0x1b, + 0x52, 0xfe, 0x59, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xbc, 0x0a, 0x01, + 0x77, 0x07, 0x01, 0x00, 0x38, 0x07, 0x00, 0xff, 0x01, 0x01, 0x62, 0x1b, + 0x52, 0xfe, 0x59, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x16, 0xf4, 0x01, + 0x77, 0x07, 0x01, 0x00, 0x4c, 0x07, 0x00, 0xff, 0x01, 0x01, 0x62, 0x1b, + 0x52, 0xfe, 0x59, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x57, 0x18, 0x01, + 0x77, 0x07, 0x81, 0x81, 0xc7, 0x82, 0x05, 0xff, 0x01, 0x01, 0x01, 0x01, + 0x83, 0x02, 0x1a, 0x24, 0x68, 0x7a, 0x27, 0x7e, 0x98, 0x56, 0x5e, 0x10, + 0x93, 0x05, 0x5b, 0xee, 0x0f, 0x70, 0x4e, 0x58, 0xfd, 0xaa, 0x3d, 0xd1, + 0x9d, 0x4f, 0xaf, 0x3e, 0xe0, 0x67, 0xc1, 0x64, 0xc3, 0x04, 0x94, 0xda, + 0xe9, 0xea, 0x15, 0x66, 0xed, 0x72, 0x7d, 0x23, 0x6a, 0xaf, 0x5a, 0xb0, + 0x9a, 0x5b, 0x01, 0x77, 0x07, 0x01, 0x00, 0x00, 0x00, 0x00, 0xff, 0x01, + 0x01, 0x01, 0x01, 0x0f, 0x31, 0x45, 0x53, 0x59, 0x31, 0x31, 0x36, 0x32, + 0x32, 0x33, 0x32, 0x39, 0x39, 0x37, 0x01, 0x77, 0x07, 0x01, 0x00, 0x20, + 0x07, 0x00, 0xff, 0x01, 0x01, 0x62, 0x23, 0x52, 0xff, 0x63, 0x09, 0x15, + 0x01, 0x77, 0x07, 0x01, 0x00, 0x34, 0x07, 0x00, 0xff, 0x01, 0x01, 0x62, + 0x23, 0x52, 0xff, 0x63, 0x09, 0x06, 0x01, 0x77, 0x07, 0x01, 0x00, 0x48, + 0x07, 0x00, 0xff, 0x01, 0x01, 0x62, 0x23, 0x52, 0xff, 0x63, 0x09, 0x15, + 0x01, 0x77, 0x07, 0x81, 0x81, 0xc7, 0xf0, 0x06, 0xff, 0x01, 0x01, 0x01, + 0x01, 0x04, 0x01, 0x07, 0x1e, 0x01, 0x01, 0x01, 0x63, 0x87, 0xc1, 0x00, + 0x76, 0x0b, 0x45, 0x53, 0x59, 0x41, 0x99, 0xa5, 0x02, 0xea, 0x9a, 0xf3, + 0x62, 0x00, 0x62, 0x00, 0x72, 0x63, 0x02, 0x01, 0x71, 0x01, 0x63, 0xd3, + 0xec, 0x00, 0x00, 0x00, 0x00, 0x1b, 0x1b, 0x1b, 0x1b, 0x1a, 0x03, 0x3a, + 0x23, 0x1b, 0x1b, 0x1b, 0x1b, 0x01, 0x01, 0x01, 0x01, 0x76, 0x0b, 0x45, + 0x53, 0x59, 0x41, 0x99, 0xa5, 0x02, 0xea, 0x9a, 0xf4, 0x62, 0x00, 0x62, + 0x00, 0x72, 0x63, 0x01, 0x01, 0x76, 0x01, 0x04, 0x45, 0x53, 0x59, 0x08, + 0x45, 0x53, 0x59, 0xdf, 0x6e, 0x9a, 0xf4, 0x0b, 0x09, 0x01, 0x45, 0x53, + 0x59, 0x11, 0x03, 0xb5, 0x99, 0xa5, 0x01, 0x01, 0x63, 0x8c, 0xff, 0x00, + 0x76, 0x0b, 0x45, 0x53, 0x59, 0x41, 0x99, 0xa5, 0x02, 0xea, 0x9a, 0xf5, + 0x62, 0x00, 0x62, 0x00, 0x72, 0x63, 0x07, 0x01, 0x77, 0x01, 0x0b, 0x09, + 0x01, 0x45, 0x53, 0x59, 0x11, 0x03, 0xb5, 0x99, 0xa5, 0x07, 0x01, 0x00, + 0x62, 0x0a, 0xff, 0xff, 0x72, 0x62, 0x01, 0x65, 0x00, 0xf8, 0xdf, 0x6e, + 0x7e, 0x77, 0x07, 0x81, 0x81, 0xc7, 0x82, 0x03, 0xff, 0x01, 0x01, 0x01, + 0x01, 0x04, 0x45, 0x53, 0x59, 0x01, 0x77, 0x07, 0x01, 0x00, 0x00, 0x00, + 0x09, 0xff, 0x01, 0x01, 0x01, 0x01, 0x0b, 0x09, 0x01, 0x45, 0x53, 0x59, + 0x11, 0x03, 0xb5, 0x99, 0xa5, 0x01, 0x77, 0x07, 0x01, 0x00, 0x01, 0x08, + 0x00, 0xff, 0x64, 0x00, 0x00, 0x80, 0x01, 0x62, 0x1e, 0x52, 0xfc, 0x59, + 0x00, 0x00, 0x00, 0x06, 0xd9, 0x5b, 0x8c, 0x64, 0x01, 0x77, 0x07, 0x01, + 0x00, 0x02, 0x08, 0x00, 0xff, 0x64, 0x00, 0x00, 0x80, 0x01, 0x62, 0x1e, + 0x52, 0xfc, 0x59, 0x00, 0x00, 0x00, 0x00, 0x41, 0x9b, 0xd4, 0xd3, 0x01, + 0x77, 0x07, 0x01, 0x00, 0x10, 0x07, 0x00, 0xff, 0x01, 0x01, 0x62, 0x1b, + 0x52, 0xfe, 0x59, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x45, 0xbd, 0x01, + 0x77, 0x07, 0x01, 0x00, 0x24, 0x07, 0x00, 0xff, 0x01, 0x01, 0x62, 0x1b, + 0x52, 0xfe, 0x59, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xc7, 0x58, 0x01, + 0x77, 0x07, 0x01, 0x00, 0x38, 0x07, 0x00, 0xff, 0x01, 0x01, 0x62, 0x1b, + 0x52, 0xfe, 0x59, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1d, 0x3e, 0x01, + 0x77, 0x07, 0x01, 0x00, 0x4c, 0x07, 0x00, 0xff, 0x01, 0x01, 0x62, 0x1b, + 0x52, 0xfe, 0x59, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x61, 0x25, 0x01, + 0x77, 0x07, 0x81, 0x81, 0xc7, 0x82, 0x05, 0xff, 0x01, 0x01, 0x01, 0x01, + 0x83, 0x02, 0x1a, 0x24, 0x68, 0x7a, 0x27, 0x7e, 0x98, 0x56, 0x5e, 0x10, + 0x93, 0x05, 0x5b, 0xee, 0x0f, 0x70, 0x4e, 0x58, 0xfd, 0xaa, 0x3d, 0xd1, + 0x9d, 0x4f, 0xaf, 0x3e, 0xe0, 0x67, 0xc1, 0x64, 0xc3, 0x04, 0x94, 0xda, + 0xe9, 0xea, 0x15, 0x66, 0xed, 0x72, 0x7d, 0x23, 0x6a, 0xaf, 0x5a, 0xb0, + 0x9a, 0x5b, 0x01, 0x77, 0x07, 0x01, 0x00, 0x00, 0x00, 0x00, 0xff, 0x01, + 0x01, 0x01, 0x01, 0x0f, 0x31, 0x45, 0x53, 0x59, 0x31, 0x31, 0x36, 0x32, + 0x32, 0x33, 0x32, 0x39, 0x39, 0x37, 0x01, 0x77, 0x07, 0x01, 0x00, 0x20, + 0x07, 0x00, 0xff, 0x01, 0x01, 0x62, 0x23, 0x52, 0xff, 0x63, 0x09, 0x16, + 0x01, 0x77, 0x07, 0x01, 0x00, 0x34, 0x07, 0x00, 0xff, 0x52, 0xff, 0x63, + 0x09, 0x07, 0x01, 0x77, 0x07, 0x01, 0x00, 0x48, 0x07, 0x00, 0xff, 0x01, + 0x01, 0x62, 0x23, 0x52, 0xff, 0x63, 0x09, 0x16, 0x01, 0x77, 0x07, 0x81, + 0x81, 0xc7, 0xf0, 0x06, 0xff, 0x01, 0x01, 0x01, 0x01, 0x04, 0x01, 0x07, + 0x1e, 0x01, 0x01, 0x01, 0x63, 0x8d, 0xa4, 0x00, 0x76, 0x0b, 0x45, 0x53, + 0x59, 0x41, 0x99, 0xa5, 0x02, 0xea, 0x9a, 0xf6, 0x62, 0x00, 0x62, 0x00, + 0x72, 0x63, 0x02, 0x01, 0x71, 0x01, 0x63, 0x17, 0xe7, 0x00, 0x00, 0x00, + 0x00, 0x1b, 0x1b, 0x1b, 0x1b, 0x1a, 0x03, 0x7b, 0xd3, 0x1b, 0x1b, 0x1b, + 0x1b, 0x01, 0x01, 0x01, 0x01, 0x76, 0x0b, 0x45, 0x53, 0x59, 0x41, 0x99, + 0xa5, 0x02, 0xea, 0x9a, 0xf7, 0x62, 0x00, 0x62, 0x00, 0x72, 0x63, 0x01, + 0x01, 0x76, 0x01, 0x04, 0x45, 0x53, 0x59, 0x08, 0x45, 0x53, 0x59, 0xdf, + 0x6f, 0x9a, 0xf7, 0x0b, 0x09, 0x01, 0x45, 0x53, 0x59, 0x11, 0x03, 0xb5, + 0x99, 0xa5, 0x01, 0x01, 0x63, 0x62, 0x0e, 0x00, 0x76, 0x0b, 0x45, 0x53, + 0x59, 0x41, 0x99, 0xa5, 0x02, 0xea, 0x9a, 0xf8, 0x62, 0x00, 0x62, 0x00, + 0x72, 0x63, 0x07, 0x01, 0x77, 0x01, 0x0b, 0x09, 0x01, 0x45, 0x53, 0x59, + 0x11, 0x03, 0xb5, 0x99, 0xa5, 0x07, 0x01, 0x00, 0x62, 0x0a, 0xff, 0xff, + 0x72, 0x62, 0x01, 0x65, 0x00, 0xf8, 0xdf, 0x6f, 0x7e, 0x77, 0x07, 0x81, + 0x81, 0xc7, 0x82, 0x03, 0xff, 0x01, 0x01, 0x01, 0x01, 0x04, 0x45, 0x53, + 0x59, 0x01, 0x77, 0x07, 0x01, 0x00, 0x00, 0x00, 0x09, 0xff, 0x01, 0x01, + 0x01, 0x01, 0x0b, 0x09, 0x01, 0x45, 0x53, 0x59, 0x11, 0x03, 0xb5, 0x99, + 0xa5, 0x01, 0x77, 0x07, 0x01, 0x00, 0x01, 0x08, 0x00, 0xff, 0x64, 0x00, + 0x00, 0x80, 0x01, 0x62, 0x1e, 0x52, 0xfc, 0x59, 0x00, 0x00, 0x00, 0x06, + 0xd9, 0x5b, 0x95, 0x2e, 0x01, 0x77, 0x07, 0x01, 0x00, 0x02, 0x08, 0x00, + 0xff, 0x64, 0x00, 0x00, 0x80, 0x01, 0x62, 0x1e, 0x52, 0xfc, 0x59, 0x00, + 0x00, 0x00, 0x00, 0x41, 0x9b, 0xd4, 0xd3, 0x01, 0x77, 0x07, 0x01, 0x00, + 0x10, 0x07, 0x00, 0xff, 0x01, 0x01, 0x62, 0x1b, 0x52, 0xfe, 0x59, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x01, 0x3c, 0x82, 0x01, 0x77, 0x07, 0x01, 0x00, + 0x24, 0x07, 0x00, 0xff, 0x01, 0x01, 0x62, 0x1b, 0x52, 0xfe, 0x59, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0xc5, 0x5b, 0x01, 0x77, 0x07, 0x01, 0x00, + 0x38, 0x07, 0x00, 0xff, 0x01, 0x01, 0x62, 0x1b, 0x52, 0xfe, 0x59, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x18, 0xaf, 0x01, 0x77, 0x07, 0x01, 0x00, + 0x4c, 0x07, 0x00, 0xff, 0x01, 0x01, 0x62, 0x1b, 0x52, 0xfe, 0x59, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x5e, 0x77, 0x01, 0x77, 0x07, 0x81, 0x81, + 0xc7, 0x82, 0x05, 0xff, 0x01, 0x01, 0x01, 0x01, 0x83, 0x02, 0x1a, 0x24, + 0x68, 0x7a, 0x27, 0x7e, 0x98, 0x56, 0x5e, 0x10, 0x93, 0x05, 0x5b, 0xee, + 0x0f, 0x70, 0x4e, 0x58, 0xfd, 0xaa, 0x3d, 0xd1, 0x9d, 0x4f, 0xaf, 0x3e, + 0xe0, 0x67, 0xc1, 0x64, 0xc3, 0x04, 0x94, 0xda, 0xe9, 0xea, 0x15, 0x66, + 0xed, 0x72, 0x7d, 0x23, 0x6a, 0xaf, 0x5a, 0xb0, 0x9a, 0x5b, 0x01, 0x77, + 0x07, 0x01, 0x00, 0x00, 0x00, 0x00, 0xff, 0x01, 0x01, 0x01, 0x01, 0x0f, + 0x31, 0x45, 0x53, 0x59, 0x31, 0x31, 0x36, 0x32, 0x32, 0x33, 0x32, 0x39, + 0x39, 0x37, 0x01, 0x77, 0x07, 0x01, 0x00, 0x20, 0x07, 0x00, 0xff, 0x01, + 0x01, 0x62, 0x23, 0x52, 0xff, 0x63, 0x09, 0x15, 0x01, 0x77, 0x07, 0x01, + 0x00, 0x34, 0x07, 0x00, 0xff, 0x01, 0x01, 0x62, 0x23, 0x52, 0xff, 0x63, + 0x09, 0x03, 0x01, 0x77, 0x07, 0x01, 0x00, 0x48, 0x07, 0x00, 0xff, 0x01, + 0x01, 0x62, 0x23, 0x52, 0xff, 0x63, 0x09, 0x15, 0x01, 0x77, 0x07, 0x81, + 0x81, 0xc7, 0xf0, 0x06, 0xff, 0x01, 0x01, 0x01, 0x01, 0x04, 0x01, 0x07, + 0x1e, 0x01, 0x01, 0x01, 0x63, 0x62, 0xbc, 0x00, 0x76, 0x0b, 0x45, 0x53, + 0x59, 0x41, 0x99, 0xa5, 0x02, 0xea, 0x9a, 0xf9, 0x62, 0x00, 0x62, 0x00, + 0x72, 0x63, 0x02, 0x01, 0x71, 0x01, 0x63, 0x5b, 0xfb, 0x00, 0x00, 0x00, + 0x00, 0x1b, 0x1b, 0x1b, 0x1b, 0x1a, 0x03, 0xf8, 0xb1, 0x1b, 0x1b, 0x1b, + 0x1b, 0x01, 0x01, 0x01, 0x01, 0x76, 0x0b, 0x45, 0x53, 0x59, 0x41, 0x99, + 0xa5, 0x02, 0xea, 0x9a, 0xfa, 0x62, 0x00, 0x62, 0x00, 0x72, 0x63, 0x01, + 0x01, 0x76, 0x01, 0x04, 0x45, 0x53, 0x59, 0x08, 0x45, 0x53, 0x59, 0xdf, + 0x70, 0x9a, 0xfa, 0x0b, 0x09, 0x01, 0x45, 0x53, 0x59, 0x11, 0x03, 0xb5, + 0x99, 0xa5, 0x01, 0x01, 0x63, 0x21, 0x68, 0x00, 0x76, 0x0b, 0x45, 0x53, + 0x59, 0x41, 0x99, 0xa5, 0x02, 0xea, 0x9a, 0xfb, 0x62, 0x00, 0x62, 0x00, + 0x72, 0x63, 0x07, 0x01, 0x77, 0x01, 0x0b, 0x09, 0x01, 0x45, 0x53, 0x59, + 0x11, 0x03, 0xb5, 0x99, 0xa5, 0x07, 0x01, 0x00, 0x62, 0x0a, 0xff, 0xff, + 0x72, 0x62, 0x01, 0x65, 0x00, 0xf8, 0xdf, 0x70, 0x7e, 0x77, 0x07, 0x81, + 0x81, 0xc7, 0x82, 0x03, 0xff, 0x01, 0x01, 0x01, 0x01, 0x04, 0x45, 0x53, + 0x59, 0x01, 0x77, 0x07, 0x01, 0x00, 0x00, 0x00, 0x09, 0xff, 0x01, 0x01, + 0x01, 0x01, 0x0b, 0x09, 0x01, 0x45, 0x53, 0x59, 0x11, 0x03, 0xb5, 0x99, + 0xa5, 0x01, 0x77, 0x07, 0x01, 0x00, 0x01, 0x08, 0x00, 0xff, 0x64, 0x00, + 0x00, 0x80, 0x01, 0x62, 0x1e, 0x52, 0xfc, 0x59, 0x00, 0x00, 0x00, 0x06, + 0xd9, 0x5b, 0x9d, 0x76, 0x01, 0x77, 0x07, 0x01, 0x00, 0x02, 0x08, 0x00, + 0xff, 0x64, 0x00, 0x00, 0x80, 0x01, 0x62, 0x1e, 0x52, 0xfc, 0x59, 0x00, + 0x00, 0x00, 0x00, 0x41, 0x9b, 0xd4, 0xd3, 0x01, 0x77, 0x07, 0x01, 0x00, + 0x10, 0x07, 0x00, 0xff, 0x01, 0x01, 0x62, 0x1b, 0x52, 0xfe, 0x59, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x01, 0x2a, 0x14, 0x01, 0x77, 0x07, 0x01, 0x00, + 0x24, 0x07, 0x00, 0xff, 0x01, 0x01, 0x62, 0x1b, 0x52, 0xfe, 0x59, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0xbd, 0xee, 0x01, 0x77, 0x07, 0x01, 0x00, + 0x38, 0x07, 0x00, 0xff, 0x01, 0x01, 0x62, 0x1b, 0x52, 0xfe, 0x59, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x14, 0x2f, 0x01, 0x77, 0x07, 0x01, 0x00, + 0x4c, 0x07, 0x00, 0xff, 0x01, 0x01, 0x62, 0x1b, 0x52, 0xfe, 0x59, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x57, 0xf6, 0x01, 0x77, 0x07, 0x81, 0x81, + 0xc7, 0x82, 0x05, 0xff, 0x01, 0x01, 0x01, 0x01, 0x83, 0x02, 0x1a, 0x24, + 0x68, 0x7a, 0x27, 0x7e, 0x98, 0x56, 0x5e, 0x10, 0x93, 0x05, 0x5b, 0xee, + 0x0f, 0x70, 0x4e, 0x58, 0xfd, 0xaa, 0x3d, 0xd1, 0x9d, 0x4f, 0xaf, 0x3e, + 0xe0, 0x67, 0xc1, 0x64, 0xc3, 0x04, 0x94, 0xda, 0xe9, 0xea, 0x15, 0x66, + 0xed, 0x72, 0x7d, 0x23, 0x6a, 0xaf, 0x5a, 0xb0, 0x9a, 0x5b, 0x01, 0x77, + 0x07, 0x01, 0x00, 0x00, 0x00, 0x00, 0xff, 0x01, 0x01, 0x01, 0x01, 0x0f, + 0x31, 0x45, 0x53, 0x59, 0x31, 0x31, 0x36, 0x32, 0x32, 0x33, 0x32, 0x39, + 0x39, 0x37, 0x01, 0x77, 0x07, 0x01, 0x00, 0x20, 0x07, 0x00, 0xff, 0x01, + 0x01, 0x62, 0x23, 0x52, 0xff, 0x63, 0x09, 0x14, 0x01, 0x77, 0x07, 0x01, + 0x00, 0x34, 0x07, 0x00, 0xff, 0x01, 0x01, 0x62, 0x23, 0x52, 0xff, 0x63, + 0x09, 0x02, 0x01, 0x77, 0x07, 0x01, 0x00, 0x48, 0x07, 0x00, 0xff, 0x01, + 0x01, 0x62, 0x23, 0x52, 0xff, 0x63, 0x09, 0x12, 0x01, 0x77, 0x07, 0x81, + 0x81, 0xc7, 0xf0, 0x06, 0xff, 0x01, 0x01, 0x01, 0x01, 0x04, 0x01, 0x07, + 0x1e, 0x01, 0x01, 0x01, 0x63, 0x25, 0x0c, 0x00, 0x76, 0x0b, 0x45, 0x53, + 0x59, 0x41, 0x99, 0xa5, 0x02, 0xea, 0x9a, 0xfc, 0x62, 0x00, 0x62, 0x00, + 0x72, 0x63, 0x02, 0x01, 0x71, 0x01, 0x63, 0x9f, 0xf0, 0x00, 0x00, 0x00, + 0x00, 0x1b, 0x1b, 0x1b, 0x1b, 0x1a, 0x03, 0x34, 0x5c, 0x1b, 0x1b, 0x1b, + 0x1b, 0x01, 0x01, 0x01, 0x01, 0x76, 0x0b, 0x45, 0x53, 0x59, 0x41, 0x99, + 0xa5, 0x02, 0xea, 0x9a, 0xfd, 0x62, 0x00, 0x62, 0x00, 0x72, 0x63, 0x01, + 0x01, 0x76, 0x01, 0x04, 0x45, 0x53, 0x59, 0x08, 0x45, 0x53, 0x59, 0xdf, + 0x71, 0x9a, 0xfd, 0x0b, 0x09, 0x01, 0x45, 0x53, 0x59, 0x11, 0x03, 0xb5, + 0x99, 0xa5, 0x01, 0x01, 0x63, 0x68, 0xcb, 0x00, 0x76, 0x0b, 0x45, 0x53, + 0x59, 0x41, 0x99, 0xa5, 0x02, 0xea, 0x9a, 0xfe, 0x62, 0x00, 0x62, 0x00, + 0x72, 0x63, 0x07, 0x01, 0x77, 0x01, 0x0b, 0x09, 0x01, 0x45, 0x53, 0x59, + 0x11, 0x03, 0xb5, 0x99, 0xa5, 0x07, 0x01, 0x00, 0x62, 0x0a, 0xff, 0xff, + 0x72, 0x62, 0x01, 0x65, 0x00, 0xf8, 0xdf, 0x71, 0x7e, 0x77, 0x07, 0x81, + 0x81, 0xc7, 0x82, 0x03, 0xff, 0x01, 0x01, 0x01, 0x01, 0x04, 0x45, 0x53, + 0x59, 0x01, 0x77, 0x07, 0x01, 0x00, 0x00, 0x00, 0x09, 0xff, 0x01, 0x01, + 0x01, 0x01, 0x0b, 0x09, 0x01, 0x45, 0x53, 0x59, 0x11, 0x03, 0xb5, 0x99, + 0xa5, 0x01, 0x77, 0x07, 0x01, 0x00, 0x01, 0x08, 0x00, 0xff, 0x64, 0x00, + 0x00, 0x80, 0x01, 0x62, 0x1e, 0x52, 0xfc, 0x59, 0x00, 0x00, 0x00, 0x06, + 0xd9, 0x5b, 0xa5, 0x6c, 0x01, 0x77, 0x07, 0x01, 0x00, 0x02, 0x08, 0x00, + 0xff, 0x64, 0x00, 0x00, 0x80, 0x01, 0x62, 0x1e, 0x52, 0xfc, 0x59, 0x00, + 0x00, 0x00, 0x00, 0x41, 0x9b, 0xd4, 0xd3, 0x01, 0x77, 0x07, 0x01, 0x00, + 0x10, 0x07, 0x00, 0xff, 0x01, 0x01, 0x62, 0x1b, 0x52, 0xfe, 0x59, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x01, 0x1e, 0x96, 0x01, 0x77, 0x07, 0x01, 0x00, + 0x24, 0x07, 0x00, 0xff, 0x01, 0x01, 0x62, 0x1b, 0x52, 0xfe, 0x59, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0xb9, 0x88, 0x01, 0x77, 0x07, 0x01, 0x00, + 0x38, 0x07, 0x00, 0xff, 0x01, 0x01, 0x62, 0x1b, 0x52, 0xfe, 0x59, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x11, 0x45, 0x01, 0x77, 0x07, 0x01, 0x00, + 0x4c, 0x07, 0x00, 0xff, 0x01, 0x01, 0x62, 0x1b, 0x52, 0xfe, 0x59, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x53, 0xc8, 0x01, 0x77, 0x07, 0x81, 0x81, + 0xc7, 0x82, 0x05, 0xff, 0x01, 0x01, 0x01, 0x01, 0x83, 0x02, 0x1a, 0x24, + 0x68, 0x7a, 0x27, 0x7e, 0x98, 0x56, 0x5e, 0x10, 0x93, 0x05, 0x5b, 0xee, + 0x0f, 0x70, 0x4e, 0x58, 0xfd, 0xaa, 0x3d, 0xd1, 0x9d, 0x4f, 0xaf, 0x3e, + 0xe0, 0x67, 0xc1, 0x64, 0xc3, 0x04, 0x94, 0xed, 0x72, 0x7d, 0x23, 0x6a, + 0xaf, 0x5a, 0xb0, 0x9a, 0x5b, 0x01, 0x77, 0x07, 0x01, 0x00, 0x00, 0x00, + 0x00, 0xff, 0x01, 0x01, 0x01, 0x01, 0x0f, 0x31, 0x45, 0x53, 0x59, 0x31, + 0x31, 0x36, 0x32, 0x32, 0x33, 0x32, 0x39, 0x39, 0x37, 0x01, 0x77, 0x07, + 0x01, 0x00, 0x20, 0x07, 0x00, 0xff, 0x01, 0x01, 0x62, 0x23, 0x52, 0xff, + 0x63, 0x09, 0x14, 0x01, 0x77, 0x07, 0x01, 0x00, 0x34, 0x07, 0x00, 0xff, + 0x01, 0x01, 0x62, 0x23, 0x52, 0xff, 0x63, 0x09, 0x05, 0x01, 0x77, 0x07, + 0x01, 0x00, 0x48, 0x07, 0x00, 0xff, 0x01, 0x01, 0x62, 0x23, 0x52, 0xff, + 0x63, 0x09, 0x13, 0x01, 0x77, 0x07, 0x81, 0x81, 0xc7, 0xf0, 0x06, 0xff, + 0x01, 0x01, 0x01, 0x01, 0x04, 0x01, 0x07, 0x1e, 0x01, 0x01, 0x01, 0x63, + 0xdc, 0x32, 0x00, 0x76, 0x0b, 0x45, 0x53, 0x59, 0x41, 0x99, 0xa5, 0x02, + 0xea, 0x9a, 0xff, 0x62, 0x00, 0x62, 0x00, 0x72, 0x63, 0x02, 0x01, 0x71, + 0x01, 0x63, 0x2c, 0x0e, 0x00, 0x00, 0x00, 0x00, 0x1b, 0x1b, 0x1b, 0x1b, + 0x1a, 0x03, 0x02, 0xf5, 0x1b, 0x1b, 0x1b, 0x1b, 0x01, 0x01, 0x01, 0x01, + 0x76, 0x0b, 0x45, 0x53, 0x59, 0x41, 0x99, 0xa5, 0x02, 0xea, 0x9b, 0x00, + 0x62, 0x00, 0x62, 0x00, 0x72, 0x63, 0x01, 0x01, 0x76, 0x01, 0x04, 0x45, + 0x53, 0x59, 0x08, 0x45, 0x53, 0x59, 0xa5, 0x01, 0x01, 0x63, 0x88, 0x47, + 0x00, 0x76, 0x0b, 0x45, 0x53, 0x59, 0x41, 0x99, 0xa5, 0x02, 0xea, 0x9b, + 0x01, 0x62, 0x00, 0x62, 0x00, 0x72, 0x63, 0x07, 0x01, 0x77, 0x01, 0x0b, + 0x09, 0x01, 0x45, 0x53, 0x59, 0x11, 0x03, 0xb5, 0x99, 0xa5, 0x07, 0x01, + 0x00, 0x62, 0x0a, 0xff, 0xff, 0x72, 0x62, 0x01, 0x65, 0x00, 0xf8, 0xdf, + 0x72, 0x7e, 0x77, 0x07, 0x81, 0x81, 0xc7, 0x82, 0x03, 0xff, 0x01, 0x01, + 0x01, 0x01, 0x04, 0x45, 0x53, 0x59, 0x01, 0x77, 0x07, 0x01, 0x00, 0x00, + 0x00, 0x09, 0xff, 0x01, 0x01, 0x01, 0x01, 0x0b, 0x09, 0x01, 0x45, 0x53, + 0x59, 0x11, 0x03, 0xb5, 0x99, 0xa5, 0x01, 0x77, 0x07, 0x01, 0x00, 0x01, + 0x08, 0x00, 0xff, 0x64, 0x00, 0x00, 0x80, 0x01, 0x62, 0x1e, 0x52, 0xfc, + 0x59, 0x00, 0x00, 0x00, 0x06, 0xd9, 0x5b, 0xad, 0x32, 0x01, 0x77, 0x07, + 0x01, 0x00, 0x02, 0x08, 0x00, 0xff, 0x64, 0x00, 0x00, 0x80, 0x01, 0x62, + 0x1e, 0x52, 0xfc, 0x59, 0x00, 0x00, 0x00, 0x00, 0x41, 0x9b, 0xd4, 0xd3, + 0x01, 0x77, 0x07, 0x01, 0x00, 0x10, 0x07, 0x00, 0xff, 0x01, 0x01, 0x62, + 0x1b, 0x52, 0xfe, 0x59, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x17, 0xdf, + 0x01, 0x77, 0x07, 0x01, 0x00, 0x24, 0x07, 0x00, 0xff, 0x01, 0x01, 0x62, + 0x1b, 0x52, 0xfe, 0x59, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xb7, 0x96, + 0x01, 0x77, 0x07, 0x01, 0x00, 0x38, 0x07, 0x00, 0xff, 0x01, 0x01, 0x62, + 0x1b, 0x52, 0xfe, 0x59, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0e, 0xf9, + 0x01, 0x77, 0x07, 0x01, 0x00, 0x4c, 0x07, 0x00, 0xff, 0x01, 0x01, 0x62, + 0x1b, 0x52, 0xfe, 0x59, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x51, 0x50, + 0x01, 0x77, 0x07, 0x81, 0x81, 0xc7, 0x82, 0x05, 0xff, 0x01, 0x01, 0x01, + 0x01, 0x83, 0x02, 0x1a, 0x24, 0x68, 0x7a, 0x27, 0x7e, 0x98, 0x56, 0x5e, + 0x10, 0x93, 0x05, 0x5b, 0xee, 0x0f, 0x70, 0x4e, 0x58, 0xfd, 0xaa, 0x3d, + 0xd1, 0x9d, 0x4f, 0xaf, 0x3e, 0xe0, 0x67, 0xc1, 0x64, 0xc3, 0x04, 0x94, + 0xda, 0xe9, 0xea, 0x15, 0x66, 0xed, 0x72, 0x7d, 0x23, 0x6a, 0xaf, 0x5a, + 0xb0, 0x9a, 0x5b, 0x01, 0x77, 0x07, 0x01, 0x00, 0x00, 0x00, 0x00, 0xff, + 0x01, 0x01, 0x01, 0x01, 0x0f, 0x31, 0x45, 0x53, 0x59, 0x31, 0x31, 0x36, + 0x32, 0x32, 0x33, 0x32, 0x39, 0x39, 0x37, 0x01, 0x77, 0x07, 0x01, 0x00, + 0x20, 0x07, 0x00, 0xff, 0x01, 0x01, 0x62, 0x23, 0x52, 0xff, 0x63, 0x09, + 0x13, 0x01, 0x77, 0x07, 0x01, 0x00, 0x34, 0x07, 0x00, 0xff, 0x01, 0x01, + 0x62, 0x23, 0x52, 0xff, 0x63, 0x09, 0x05, 0x01, 0x77, 0x07, 0x01, 0x00, + 0x48, 0x07, 0x00, 0xff, 0x01, 0x01, 0x62, 0x23, 0x52, 0xff, 0x63, 0x09, + 0x15, 0x01, 0x77, 0x07, 0x81, 0x81, 0xc7, 0xf0, 0x06, 0xff, 0x01, 0x01, + 0x01, 0x01, 0x04, 0x01, 0x07, 0x1e, 0x01, 0x01, 0x01, 0x63, 0x06, 0x0a, + 0x00, 0x76, 0x0b, 0x45, 0x53, 0x59, 0x41, 0x99, 0xa5, 0x02, 0xea, 0x9b, + 0x02, 0x62, 0x00, 0x62, 0x00, 0x72, 0x63, 0x02, 0x01, 0x71, 0x01, 0x63, + 0xc6, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x1b, 0x1b, 0x1b, 0x1b, 0x1a, 0x03, + 0xf9, 0x08, 0x1b, 0x1b, 0x1b, 0x1b, 0x01, 0x01, 0x01, 0x01, 0x76, 0x0b, + 0x45, 0x53, 0x59, 0x41, 0x99, 0xa5, 0x02, 0xea, 0x9b, 0x03, 0x62, 0x00, + 0x62, 0x00, 0x72, 0x63, 0x01, 0x01, 0x76, 0x01, 0x04, 0x45, 0x53, 0x59, + 0x08, 0x45, 0x53, 0x59, 0xdf, 0x73, 0x9b, 0x03, 0x0b, 0x09, 0x01, 0x45, + 0x53, 0x59, 0x11, 0x03, 0xb5, 0x99, 0xa5, 0x01, 0x01, 0x63, 0x66, 0xb6, + 0x00, 0x76, 0x0b, 0x45, 0x53, 0x59, 0x41, 0x99, 0xa5, 0x02, 0xea, 0x9b, + 0x04, 0x62, 0x00, 0x62, 0x00, 0x72, 0x63, 0x07, 0x01, 0x77, 0x01, 0x0b, + 0x09, 0x01, 0x45, 0x53, 0x59, 0x11, 0x03, 0xb5, 0x99, 0xa5, 0x07, 0x01, + 0x00, 0x62, 0x0a, 0xff, 0xff, 0x72, 0x62, 0x01, 0x65, 0x00, 0xf8, 0xdf, + 0x73, 0x7e, 0x77, 0x07, 0x81, 0x81, 0xc7, 0x82, 0x03, 0xff, 0x01, 0x01, + 0x01, 0x01, 0x04, 0x45, 0x53, 0x59, 0x01, 0x77, 0x07, 0x01, 0x00, 0x00, + 0x00, 0x09, 0xff, 0x01, 0x01, 0x01, 0x01, 0x0b, 0x09, 0x01, 0x45, 0x53, + 0x59, 0x11, 0x03, 0xb5, 0x99, 0xa5, 0x01, 0x77, 0x07, 0x01, 0x00, 0x01, + 0x08, 0x00, 0xff, 0x64, 0x00, 0x00, 0x80, 0x01, 0x62, 0x1e, 0x52, 0xfc, + 0x59, 0x00, 0x00, 0x00, 0x06, 0xd9, 0x5b, 0xb4, 0xd3, 0x01, 0x77, 0x07, + 0x01, 0x00, 0x02, 0x08, 0x00, 0xff, 0x64, 0x00, 0x00, 0x80, 0x01, 0x62, + 0x1e, 0x52, 0xfc, 0x59, 0x00, 0x00, 0x00, 0x00, 0x41, 0x9b, 0xd4, 0xd3, + 0x01, 0x77, 0x07, 0x01, 0x00, 0x10, 0x07, 0x00, 0xff, 0x01, 0x01, 0x62, + 0x1b, 0x52, 0xfe, 0x59, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x12, 0xa4, + 0x01, 0x77, 0x07, 0x01, 0x00, 0x24, 0x07, 0x00, 0xff, 0x01, 0x01, 0x62, + 0x1b, 0x52, 0xfe, 0x59, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xb5, 0x96, + 0x01, 0x77, 0x07, 0x01, 0x00, 0x38, 0x07, 0x00, 0xff, 0x01, 0x01, 0x62, + 0x1b, 0x52, 0xfe, 0x59, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0d, 0x95, + 0x01, 0x77, 0x07, 0x01, 0x00, 0x4c, 0x07, 0x00, 0xff, 0x01, 0x01, 0x62, + 0x1b, 0x52, 0xfe, 0x59, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x4f, 0x78, + 0x01, 0x77, 0x07, 0x81, 0x81, 0xc7, 0x82, 0x05, 0xff, 0x01, 0x01, 0x01, + 0x01, 0x83, 0x02, 0x1a, 0x24, 0x68, 0x7a, 0x27, 0x7e, 0x98, 0x56, 0x5e, + 0x10, 0x93, 0x05, 0x5b, 0xee, 0x0f, 0x70, 0x4e, 0x58, 0xfd, 0xaa, 0x3d, + 0xd1, 0x9d, 0x4f, 0xaf, 0x3e, 0xe0, 0x67, 0xc1, 0x64, 0xc3, 0x04, 0x94, + 0xda, 0xe9, 0xea, 0x15, 0x66, 0xed, 0x72, 0x7d, 0x23, 0x6a, 0xaf, 0x5a, + 0xb0, 0x9a, 0x5b, 0x01, 0x77, 0x07, 0x01, 0x00, 0x00, 0x00, 0x00, 0xff, + 0x01, 0x01, 0x01, 0x01, 0x0f, 0x31, 0x45, 0x53, 0x59, 0x31, 0x31, 0x36, + 0x32, 0x32, 0x33, 0x32, 0x39, 0x39, 0x37, 0x01, 0x77, 0x07, 0x01, 0x00, + 0x20, 0x07, 0x00, 0xff, 0x01, 0x01, 0x62, 0x23, 0x52, 0xff, 0x63, 0x09, + 0x12, 0x01, 0x77, 0x07, 0x01, 0x00, 0x34, 0x07, 0x00, 0xff, 0x01, 0x01, + 0x62, 0x23, 0x52, 0xff, 0x63, 0x09, 0x02, 0x01, 0x77, 0x07, 0x01, 0x00, + 0x48, 0x07, 0x00, 0xff, 0x01, 0x01, 0x62, 0x23, 0x52, 0xff, 0x63, 0x09, + 0x13, 0x01, 0x77, 0x07, 0x81, 0x81, 0xc7, 0xf0, 0x06, 0xff, 0x01, 0x01, + 0x01, 0x01, 0x04, 0x01, 0x07, 0x1e, 0x01, 0x01, 0x01, 0x63, 0xb5, 0xf6, + 0x00, 0x76, 0x0b, 0x45, 0x53, 0x59, 0x41, 0x99, 0xa5, 0x02, 0xea, 0x9b, + 0x05, 0x62, 0x00, 0x62, 0x00, 0x72, 0x63, 0x02, 0x01, 0x71, 0x01, 0x63, + 0x20, 0x50, 0x00, 0x00, 0x00, 0x00, 0x1b, 0x1b, 0x1b, 0x1b, 0x1a, 0x03, + 0x2d, 0x91, 0x1b, 0x1b, 0x1b, 0x1b, 0x01, 0x01, 0x01, 0x01, 0x76, 0x0b, + 0x45, 0x53, 0x59, 0x41, 0x99, 0xa5, 0x02, 0xea, 0x9b, 0x06, 0x62, 0x00, + 0x62, 0x00, 0x72, 0x63, 0x01, 0x01, 0x76, 0x01, 0x04, 0x45, 0x53, 0x59, + 0x08, 0x45, 0x53, 0x59, 0xdf, 0x74, 0x9b, 0x06, 0x0b, 0x09, 0x01, 0x45, + 0x53, 0x59, 0x11, 0x03, 0xb5, 0x99, 0xa5, 0x01, 0x01, 0x63, 0x27, 0x84, + 0x00, 0x76, 0x0b, 0x45, 0x53, 0x59, 0x41, 0x99, 0xa5, 0x02, 0xea, 0x9b, + 0x07, 0x62, 0x00, 0x62, 0x00, 0x72, 0x63, 0x07, 0x01, 0x77, 0x01, 0x0b, + 0x09, 0x01, 0x45, 0x53, 0x59, 0x11, 0x03, 0xb5, 0x99, 0xa5, 0x07, 0x01, + 0x00, 0x62, 0x0a, 0xff, 0xff, 0x72, 0x62, 0x01, 0x65, 0x00, 0xf8, 0xdf, + 0x74, 0x7e, 0x77, 0x07, 0x81, 0x81, 0xc7, 0x82, 0x03, 0xff, 0x01, 0x01, + 0x01, 0x01, 0x04, 0x45, 0x53, 0x59, 0x01, 0x77, 0x07, 0x01, 0x00, 0x00, + 0x00, 0x09, 0xff, 0x01, 0x01, 0x01, 0x01, 0x0b, 0x09, 0x01, 0x45, 0x53, + 0x59, 0x11, 0x03, 0xb5, 0x99, 0xa5, 0x01, 0x77, 0x07, 0x01, 0x00, 0x01, + 0x08, 0x00, 0xff, 0x64, 0x00, 0x00, 0x80, 0x01, 0x62, 0x1e, 0x52, 0xfc, + 0x59, 0x00, 0x00, 0x00, 0x06, 0xd9, 0x5b, 0xbc, 0x4a, 0x01, 0x77, 0x07, + 0x01, 0x00, 0x02, 0x08, 0x00, 0xff, 0x64, 0x00, 0x00, 0x80, 0x01, 0x62, + 0x1e, 0x52, 0xfc, 0x59, 0x00, 0x00, 0x00, 0x00, 0x41, 0x9b, 0xd4, 0xd3, + 0x01, 0x77, 0x07, 0x01, 0x00, 0x10, 0x07, 0x00, 0xff, 0x01, 0x01, 0x62, + 0x1b, 0x52, 0xfe, 0x59, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x0c, 0xb2, + 0x01, 0x77, 0x07, 0x01, 0x00, 0x24, 0x07, 0x00, 0xff, 0x01, 0x01, 0x62, + 0x1b, 0x52, 0xfe, 0x59, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xb3, 0x05, + 0x01, 0x77, 0x07, 0x01, 0x00, 0x38, 0x07, 0x00, 0xff, 0x01, 0x01, 0x62, + 0x1b, 0x52, 0xfe, 0x59, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0c, 0xa8, + 0x01, 0x77, 0x07, 0x01, 0x00, 0x4c, 0x07, 0x00, 0xff, 0x01, 0x01, 0x62, + 0x1b, 0x52, 0xfe, 0x59, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x4d, 0x04, + 0x01, 0x77, 0x07, 0x81, 0x81, 0xc7, 0x82, 0x05, 0xff, 0x01, 0x01, 0x01, + 0x01, 0x83, 0x02, 0x1a, 0x24, 0x68, 0x7a, 0x27, 0x7e, 0x98, 0x56, 0x5e, + 0x10, 0x93, 0x05, 0x5b, 0xee, 0x0f, 0x70, 0x4e, 0x58, 0xfd, 0xaa, 0x3d, + 0xd1, 0x9d, 0x4f, 0xaf, 0x3e, 0xe0, 0x67, 0xc1, 0x64, 0xc3, 0x04, 0x94, + 0xda, 0xe9, 0xea, 0x15, 0x66, 0xed, 0x72, 0x7d, 0x23, 0x6a, 0xaf, 0x5a, + 0xb0, 0x9a, 0x5b, 0x01, 0x77, 0x07, 0x01, 0x00, 0x00, 0x00, 0x00, 0xff, + 0x01, 0x01, 0x01, 0x01, 0x0f, 0x31, 0x45, 0x53, 0x59, 0x31, 0x31, 0x36, + 0x32, 0x32, 0x33, 0x32, 0x39, 0x39, 0x37, 0x01, 0x77, 0x07, 0x01, 0x00, + 0x20, 0x07, 0x00, 0xff, 0x01, 0x01, 0x62, 0x23, 0x52, 0xff, 0x63, 0x09, + 0x12, 0x01, 0x77, 0x07, 0x01, 0x00, 0x34, 0x07, 0x00, 0xff, 0x01, 0x01, + 0x62, 0x23, 0x52, 0xff, 0x63, 0x09, 0x01, 0x01, 0x77, 0x07, 0x01, 0x00, + 0x48, 0x07, 0x00, 0xff, 0x01, 0x01, 0x62, 0x23, 0x52, 0xff, 0x63, 0x09, + 0x10, 0x01, 0x77, 0x07, 0x81, 0x81, 0xc7, 0xf0, 0x06, 0xff, 0x01, 0x01, + 0x01, 0x01, 0x04, 0x01, 0x07, 0x1e, 0x01, 0x01, 0x01, 0x63, 0x16, 0x0d, + 0x00, 0x76, 0x0b, 0x45, 0x53, 0x59, 0x41, 0x99, 0xa5, 0x02, 0xea, 0x9b, + 0x08, 0x62, 0x00, 0x62, 0x00, 0x72, 0x63, 0x02, 0x01, 0x71, 0x01, 0x63, + 0x4e, 0xe7, 0x00, 0x00, 0x00, 0x00, 0x1b, 0x1b, 0x1b, 0x1b, 0x1a, 0x03, + 0xa2, 0x22, 0x1b, 0x1b, 0x1b, 0x1b, 0x01, 0x01, 0x01, 0x01, 0x76, 0x0b, + 0x45, 0x53, 0x59, 0x41, 0x99, 0xa5, 0x02, 0xea, 0x9b, 0x09, 0x62, 0x00, + 0x62, 0x00, 0x72, 0x63, 0x01, 0x01, 0x76, 0x01, 0x04, 0x45, 0x53, 0x59, + 0x08, 0x45, 0x53, 0x59, 0xdf, 0x75, 0x9b, 0x09, 0x0b, 0x09, 0x01, 0x45, + 0x53, 0x59, 0x11, 0x03, 0xb5, 0x99, 0xa5, 0x01, 0x01, 0x63, 0x20, 0x82, + 0x00, 0x76, 0x0b, 0x45, 0x53, 0x59, 0x41, 0x99, 0xa5, 0x02, 0xea, 0x9b, + 0x0a, 0x62, 0x00, 0x62, 0x00, 0x72, 0x63, 0x07, 0x01, 0x77, 0x01, 0x0b, + 0x09, 0x01, 0x45, 0x53, 0x59, 0x11, 0x03, 0xb5, 0x99, 0xa5, 0x07, 0x01, + 0x00, 0x62, 0x0a, 0xff, 0xff, 0x72, 0x62, 0x01, 0x65, 0x00, 0xf8, 0xdf, + 0x75, 0x7e, 0x77, 0x07, 0x81, 0x81, 0xc7, 0x82, 0x03, 0xff, 0x01, 0x01, + 0x01, 0x01, 0x04, 0x45, 0x53, 0x59, 0x01, 0x77, 0x07, 0x01, 0x00, 0x00, + 0x00, 0x09, 0xff, 0x01, 0x01, 0x01, 0x01, 0x0b, 0x09, 0x01, 0x45, 0x53, + 0x59, 0x11, 0x03, 0xb5}; +unsigned int data_bin_len = 4096; diff --git a/lib/sml_parser/test/test/test_ESY/test.cpp b/lib/sml_parser/test/test/test_ESY/test.cpp new file mode 100644 index 0000000..5eec376 --- /dev/null +++ b/lib/sml_parser/test/test/test_ESY/test.cpp @@ -0,0 +1,111 @@ +#include "data_bin.h" +#include "sml.h" +#include "unity.h" +#ifdef ARDUINO +#include "arduino.h" +#endif + +typedef struct { + const unsigned char OBIS[6]; + void (*Handler)(); +} OBISHandler; + +#define MAX_STR_MANUF 5 +unsigned char manuf[MAX_STR_MANUF]; +long long int SumWh = -2; +bool isFinal = false; + +void Manufacturer() { smlOBISManufacturer(manuf, MAX_STR_MANUF); } + +void PowerSum() +{ + // Double on Arduino UNO is just 32 bit, so we must test against long int + signed char sc = 0; + smlOBISByUnit(SumWh, sc, SML_WATT_HOUR); +} + +// clang-format off +OBISHandler OBISHandlers[] = { + {{ 0x81, 0x81, 0xc7, 0x82, 0x03, 0xff }, &Manufacturer}, /* 129-129:199.130.3*255 */ + {{ 0x01, 0x00, 0x01, 0x08, 0x00, 0xff }, &PowerSum}, /* 1- 0: 1. 8.0*255 (T1 + T2) */ + {{ 0, 0 }} +}; +// clang-format on + +void setUp(void) +{ + unsigned int i = 0; + int iHandler = 0; + unsigned char c; + sml_states_t s; + manuf[0] = 0; + + for (i = 0; i < data_bin_len; ++i) { +#ifdef ARDUINO + c = pgm_read_word_near(data_bin + i); +#else + c = data_bin[i]; +#endif + + s = smlState(c); + if (s == SML_LISTEND) { + /* check handlers on last received list */ + for (iHandler = 0; OBISHandlers[iHandler].Handler != 0 && + !(smlOBISCheck(OBISHandlers[iHandler].OBIS)); + iHandler++) + ; + if (OBISHandlers[iHandler].Handler != 0) { + OBISHandlers[iHandler].Handler(); + } + } + if (s == SML_FINAL) { + isFinal = true; + } + } +} + +void test_should_return_manufacturer(void) +{ + TEST_ASSERT_EQUAL_STRING("ESY", manuf); +} + +void test_should_return_SumWh(void) +{ + TEST_ASSERT_EQUAL_INT(29416471626, SumWh); +} + +void test_should_be_final(void) { TEST_ASSERT_EQUAL_INT(1, isFinal); } + +int runUnityTests(void) +{ + UNITY_BEGIN(); + RUN_TEST(test_should_return_manufacturer); + // RUN_TEST(test_should_return_t1); + RUN_TEST(test_should_return_SumWh); + RUN_TEST(test_should_be_final); + return UNITY_END(); +} + +/** + * For native dev-platform or for some embedded frameworks + */ +int main(void) { return runUnityTests(); } + +/** + * For Arduino framework + */ +void setup() +{ +// Wait ~2 seconds before the Unity test runner +// establishes connection with a board Serial interface +#ifdef ARDUINO + delay(2000); +#endif + runUnityTests(); +} +void loop() {} + +/** + * For ESP-IDF framework + */ +void app_main() { runUnityTests(); } diff --git a/lib/sml_parser/test/test/test_efr_sgm_c2/ehz_bin.h b/lib/sml_parser/test/test/test_efr_sgm_c2/ehz_bin.h new file mode 100644 index 0000000..44ad510 --- /dev/null +++ b/lib/sml_parser/test/test/test_efr_sgm_c2/ehz_bin.h @@ -0,0 +1,2 @@ +const unsigned char ehz_bin[] = {0x1B,0x1B,0x1B,0x1B,0x01,0x01,0x01,0x01,0x76,0x05,0x01,0x1F,0xF2,0x51,0x62,0x00,0x62,0x00,0x72,0x63,0x01,0x01,0x76,0x01,0x07,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0x05,0x00,0x5F,0xFB,0x71,0x0B,0x0A,0x01,0x45,0x46,0x52,0x21,0x01,0xE8,0xB2,0xF0,0x72,0x62,0x01,0x65,0x01,0x08,0xC9,0x7F,0x01,0x63,0x13,0x33,0x00,0x76,0x05,0x01,0x1F,0xF2,0x52,0x62,0x00,0x62,0x00,0x72,0x63,0x07,0x01,0x77,0x07,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0x0B,0x0A,0x01,0x45,0x46,0x52,0x21,0x01,0xE8,0xB2,0xF0,0x07,0x01,0x00,0x62,0x0A,0xFF,0xFF,0x72,0x62,0x01,0x65,0x01,0x08,0xC9,0x7F,0x7E,0x77,0x07,0x01,0x00,0x60,0x32,0x01,0x01,0x01,0x01,0x01,0x01,0x04,0x45,0x46,0x52,0x01,0x77,0x07,0x01,0x00,0x60,0x01,0x00,0xFF,0x01,0x01,0x01,0x01,0x0B,0x0A,0x01,0x45,0x46,0x52,0x21,0x01,0xE8,0xB2,0xF0,0x01,0x77,0x07,0x01,0x00,0x01,0x08,0x00,0xFF,0x64,0x04,0x01,0x04,0x72,0x62,0x01,0x65,0x01,0x08,0xC9,0x7F,0x62,0x1E,0x52,0xFF,0x64,0xF5,0x75,0xFA,0x01,0x77,0x07,0x01,0x00,0x02,0x08,0x00,0xFF,0x01,0x72,0x62,0x01,0x65,0x01,0x08,0xC9,0x7F,0x62,0x1E,0x52,0xFF,0x62,0x00,0x01,0x77,0x07,0x01,0x00,0x10,0x07,0x00,0xFF,0x01,0x01,0x62,0x1B,0x52,0x00,0x53,0x00,0x91,0x01,0x77,0x07,0x01,0x00,0x20,0x07,0x00,0xFF,0x01,0x01,0x62,0x23,0x52,0xFF,0x63,0x09,0x05,0x01,0x77,0x07,0x01,0x00,0x1F,0x07,0x00,0xFF,0x01,0x01,0x62,0x21,0x52,0xFE,0x62,0x49,0x01,0x77,0x07,0x01,0x00,0x51,0x07,0x04,0xFF,0x01,0x01,0x62,0x08,0x52,0x00,0x53,0x01,0x4A,0x01,0x77,0x07,0x01,0x00,0x0E,0x07,0x00,0xFF,0x01,0x01,0x62,0x2C,0x52,0xFF,0x63,0x01,0xF4,0x01,0x77,0x07,0x01,0x00,0x00,0x02,0x00,0x00,0x01,0x01,0x01,0x01,0x06,0x30,0x31,0x2E,0x30,0x37,0x01,0x77,0x07,0x01,0x00,0x60,0x5A,0x02,0x01,0x01,0x01,0x01,0x01,0x03,0xA9,0x0A,0x01,0x77,0x07,0x01,0x00,0x61,0x61,0x00,0x00,0x01,0x01,0x01,0x01,0x03,0x00,0x00,0x01,0x77,0x07,0x01,0x00,0x60,0x32,0x01,0x04,0x01,0x01,0x01,0x01,0x09,0x50,0x31,0x2E,0x30,0x33,0x2E,0x30,0x36,0x01,0x77,0x07,0x01,0x00,0x60,0x32,0x04,0x04,0x01,0x01,0x01,0x01,0x03,0x04,0x23,0x01,0x01,0x01,0x63,0x75,0x61,0x00,0x76,0x05,0x01,0x1F,0xF2,0x53,0x62,0x00,0x62,0x00,0x72,0x63,0x02,0x01,0x71,0x01,0x63,0x8A,0x8E,0x00,0x00,0x1B,0x1B,0x1B,0x1B,0x1A,0x01,0x70,0x98}; +const unsigned int ehz_bin_len = 416; diff --git a/lib/sml_parser/test/test/test_efr_sgm_c2/test.cpp b/lib/sml_parser/test/test/test_efr_sgm_c2/test.cpp new file mode 100644 index 0000000..3531b7e --- /dev/null +++ b/lib/sml_parser/test/test/test_efr_sgm_c2/test.cpp @@ -0,0 +1,92 @@ +#include "ehz_bin.h" +#include "sml.h" +#include "unity.h" +#ifdef ARDUINO +#include "arduino.h" +#endif + +typedef struct { + const unsigned char OBIS[6]; + void (*Handler)(); +} OBISHandler; + +#define MAX_STR_MANUF 5 +unsigned char manuf[MAX_STR_MANUF]; +double T1Wh = -2, SumWh = -2; +bool isFinal = false; + +void PowerSum() { smlOBISWh(SumWh); } + +// clang-format off +OBISHandler OBISHandlers[] = { + {{ 0x01, 0x00, 0x01, 0x08, 0x00, 0xff }, &PowerSum}, /* 1- 0: 1. 8.0*255 (T1 + T2) */ + {{ 0, 0 }} +}; +// clang-format on + +void setUp(void) +{ + unsigned int i = 0; + int iHandler = 0; + unsigned char c; + sml_states_t s; + manuf[0] = 0; + + for (i = 0; i < ehz_bin_len; ++i) { + c = ehz_bin[i]; + s = smlState(c); + if (s == SML_LISTEND) { + /* check handlers on last received list */ + for (iHandler = 0; OBISHandlers[iHandler].Handler != 0 && + !(smlOBISCheck(OBISHandlers[iHandler].OBIS)); + iHandler++) + ; + if (OBISHandlers[iHandler].Handler != 0) { + OBISHandlers[iHandler].Handler(); + } + } + if (s == SML_FINAL) { + isFinal = true; + } + } +} + + +void test_should_return_SumWh(void) +{ + TEST_ASSERT_EQUAL_DOUBLE(1608652.2, SumWh); +} + +void test_should_be_final(void) { TEST_ASSERT_EQUAL_INT(1, isFinal); } + +int runUnityTests(void) +{ + UNITY_BEGIN(); + RUN_TEST(test_should_return_SumWh); + RUN_TEST(test_should_be_final); + return UNITY_END(); +} + +/** + * For native dev-platform or for some embedded frameworks + */ +int main(void) { return runUnityTests(); } + +/** + * For Arduino framework + */ +void setup() +{ +// Wait ~2 seconds before the Unity test runner +// establishes connection with a board Serial interface +#ifdef ARDUINO + delay(2000); +#endif + runUnityTests(); +} +void loop() {} + +/** + * For ESP-IDF framework + */ +void app_main() { runUnityTests(); } diff --git a/lib/sml_parser/test/test/test_negative/ehz_bin.h b/lib/sml_parser/test/test/test_negative/ehz_bin.h new file mode 100644 index 0000000..be2851a --- /dev/null +++ b/lib/sml_parser/test/test/test_negative/ehz_bin.h @@ -0,0 +1,121 @@ +// clang-format off +// Formatted example data from https://github.com/devZer0/libsml-testing + +const unsigned char ehz_bin[] = { + /* Start */ 0x1b, 0x1b, 0x1b, 0x1b, + /* Version */ 0x01, 0x01, 0x01, 0x01, + + /* List (6 entries) */ 0x76, + /* 1. Transaktion ID */ 0x07, 0x00, 0x0c, 0x04, 0x08, 0x87, 0x2d, + /* 2. Group No */ 0x62, 0x00, + /* 3. Abort On Error */ 0x62, 0x00, + /* 4. List (2 entries) */ 0x72, + /* 1. getOpenResponse */ 0x63, 0x01, 0x01, + /* 2. List (6 entries) */ 0x76, + /* 1. code page */ 0x01, + /* 2. clientid */ 0x01, + /* 3. Transaction ID */ 0x07, 0x00, 0x0c, 0x06, 0x9e, 0x2d, 0x0f, + /* 4. ServerID */ 0x0b, 0x06, 0x45, 0x4d, 0x48, 0x01, 0x00, 0x1d, 0x46, 0x15, 0xca, + /* 5. List name */ 0x01, + /* 6. Time */ 0x01, + /* 5. CRC */ 0x63, 0x2b, 0x8e, + /* 6. END */ 0x00, + + /* List (6 entries) */ 0x76, + /* 1. Transaktion ID */ 0x07, 0x00, 0x0c, 0x04, 0x08, 0x87, 0x2e, + /* 2. Group No */ 0x62, 0x00, + /* 3. Abort On Error */ 0x62, 0x00, + /* 4. Message Body (2) */ 0x72, + /* 1. getListResponse */ 0x63, 0x07, 0x01, + /* 2. List (7) */ 0x77, + /* 1. ClientId */ 0x01, + /* 2. ServerID */ 0x0b, 0x06, 0x45, 0x4d, 0x48, 0x01, 0x00, 0x1d, 0x46, 0x15, 0xca, + /* 3. ?? */ 0x01, + /* 4. List (2) */ 0x72, + /* 1. ?? */ 0x62, 0x01, + /* 2. ?? */ 0x65, 0x06, 0x9e, 0xfa, 0x83, + /* 5. List (7) */ 0x77, + /* 1. List (7) */ 0x77, + /* 1. OBIS */ 0x07, 0x81, 0x81, 0xc7, 0x82, 0x03, 0xff, + /* 2. */ 0x01, + /* 3. */ 0x01, + /* 4. */ 0x01, + /* 5. */ 0x01, + /* 6. */ 0x04, 0x45, 0x4d, 0x48, + /* 7. */ 0x01, + /* 2. List (7) */ 0x77, + /* 1. OBIS */ 0x07, 0x01, 0x00, 0x00, 0x00, 0x09, 0xff, + /* 2. */ 0x01, + /* 3. */ 0x01, + /* 4. */ 0x01, + /* 5. */ 0x01, + /* 6. */ 0x0b, 0x06, 0x45, 0x4d, 0x48, 0x01, 0x00, 0x1d, 0x46, 0x15, 0xca, + /* 7. */ 0x01, + /* 3. List (7) */ 0x77, + /* 1. OBIS */ 0x07, 0x01, 0x00, 0x01, 0x08, 0x00, 0xff, + /* 2. */ 0x63, 0x01, 0x82, + /* 3. */ 0x01, + /* 4. */ 0x62, 0x1e, + /* 5. */ 0x52, 0x00, + /* 6. */ 0x53, 0xfe, 0xfb, + /* 7. */ 0x01, + /* 4. List (7) */ 0x77, + /* 1. OBIS */ 0x07, 0x01, 0x00, 0x01, 0x08, 0x01, 0xff, + /* 2. */ 0x01, + /* 3. */ 0x01, + /* 4. Unit */ 0x62, 0x1e, + /* 5. Scaler */ 0x52, 0x00, + /* 6 Value */ 0x62, 0xf4, + /* 7 */ 0x01, + /* 5. List (7) */ 0x77, + /* 1. OBIS */ 0x07, 0x01, 0x00, 0x01, 0x08, 0x02, 0xff, + /* 2. */ 0x01, + /* 3. */ 0x01, + /* 4. */ 0x62, 0x1e, + /* 5. */ 0x52, 0x00, + /* 6. */ 0x63, 0xfe, 0xfb, + /* 7. */ 0x01, + /* 6. List (7) */ 0x77, + /* 1. OBIS */ 0x07, 0x01, 0x00, 0x0f, 0x07, 0x00, 0xff, + /* 2. */ 0x01, + /* 3. */ 0x01, + /* 4. */ 0x62, 0x1b, + /* 5. */ 0x52, 0xff, + /* 6. */ 0x55, 0x00, 0x00, 0x2f, 0x65, + /* 7. */ 0x01, + /* 7. List (7) */ 0x77, + /* 1. OBIS */ 0x07, 0x81, 0x81, 0xc7, 0x82, 0x05, 0xff, + /* 2. */ 0x01, + /* 3. */ 0x01, + /* 4. */ 0x01, + /* 5. */ 0x01, + /* 6. Public Key */ 0x83, 0x02, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + /* 7. */ 0x01, + 0x01, + 0x01, + 0x63, 0xb9, 0x3f, + 0x00, + /* List (6) */ 0x76, + /* 1. */ 0x07, 0x00, 0x0c, 0x04, 0x08, 0x87, 0x31, + /* 2. */ 0x62, 0x00, + /* 3. */ 0x62, 0x00, + /* 4. List (2) */ 0x72, + /* 1. */ 0x63, 0x02, 0x01, + /* List (1) */ 0x71, + /* 1. */ 0x01, + /* 5. CRC */ 0x63, 0x6a, 0x53, + /* 6. */ 0x00, + + 0x00, + 0x1b, 0x1b, 0x1b, 0x1b, + 0x1a, 0x01, 0x61, 0x6b + +}; + + +const unsigned int ehz_bin_len = 306; + diff --git a/lib/sml_parser/test/test/test_negative/test.cpp b/lib/sml_parser/test/test/test_negative/test.cpp new file mode 100644 index 0000000..8e785a5 --- /dev/null +++ b/lib/sml_parser/test/test/test_negative/test.cpp @@ -0,0 +1,101 @@ +#include "ehz_bin.h" +#include "sml.h" +#include "unity.h" +#ifdef ARDUINO +#include "arduino.h" +#endif + +typedef struct { + const unsigned char OBIS[6]; + void (*Handler)(); +} OBISHandler; + +#define MAX_STR_MANUF 5 +unsigned char manuf[MAX_STR_MANUF]; +double T1Wh = -2, SumWh = -2; +bool isFinal = false; + +void Manufacturer() { smlOBISManufacturer(manuf, MAX_STR_MANUF); } +void PowerT1() { smlOBISWh(T1Wh); } +void PowerSum() { smlOBISWh(SumWh); } + +// clang-format off +OBISHandler OBISHandlers[] = { + {{ 0x81, 0x81, 0xc7, 0x82, 0x03, 0xff }, &Manufacturer}, /* 129-129:199.130.3*255 */ + {{ 0x01, 0x00, 0x01, 0x08, 0x01, 0xff }, &PowerT1}, /* 1- 0: 1. 8.1*255 (T1) */ + {{ 0x01, 0x00, 0x01, 0x08, 0x00, 0xff }, &PowerSum}, /* 1- 0: 1. 8.0*255 (T1 + T2) */ + {{ 0, 0 }} +}; +// clang-format on + +void setUp(void) +{ + unsigned int i = 0; + int iHandler = 0; + unsigned char c; + sml_states_t s; + manuf[0] = 0; + + for (i = 0; i < ehz_bin_len; ++i) { + c = ehz_bin[i]; + s = smlState(c); + if (s == SML_LISTEND) { + /* check handlers on last received list */ + for (iHandler = 0; OBISHandlers[iHandler].Handler != 0 && + !(smlOBISCheck(OBISHandlers[iHandler].OBIS)); + iHandler++) + ; + if (OBISHandlers[iHandler].Handler != 0) { + OBISHandlers[iHandler].Handler(); + } + } + if (s == SML_FINAL) { + isFinal = true; + } + } +} + +void test_should_return_manufacturer(void) +{ + TEST_ASSERT_EQUAL_STRING("EMH", manuf); +} + +void test_should_return_t1(void) { TEST_ASSERT_EQUAL_DOUBLE(244, T1Wh); } + +void test_should_return_SumWh(void) { TEST_ASSERT_EQUAL_DOUBLE(-261, SumWh); } + +void test_should_be_final(void) { TEST_ASSERT_EQUAL_INT(1, isFinal); } + +int runUnityTests(void) +{ + UNITY_BEGIN(); + RUN_TEST(test_should_return_manufacturer); + RUN_TEST(test_should_return_t1); + RUN_TEST(test_should_return_SumWh); + RUN_TEST(test_should_be_final); + return UNITY_END(); +} + +/** + * For native dev-platform or for some embedded frameworks + */ +int main(void) { return runUnityTests(); } + +/** + * For Arduino framework + */ +void setup() +{ +// Wait ~2 seconds before the Unity test runner +// establishes connection with a board Serial interface +#ifdef ARDUINO + delay(2000); +#endif + runUnityTests(); +} +void loop() {} + +/** + * For ESP-IDF framework + */ +void app_main() { runUnityTests(); } diff --git a/platformio.ini b/platformio.ini index 59fae7e..ebdc268 100644 --- a/platformio.ini +++ b/platformio.ini @@ -8,7 +8,11 @@ ; Please visit documentation for the other options and examples ; https://docs.platformio.org/page/projectconf.html -[env:minipill_l051c8_lora] +[env:minipill_l051c8_lora_custom] platform = ststm32 -board = minipill_l051c8_lora +board = minipill_l051c8_lora_custom framework = arduino +lib_deps = + locoduino/RingBuffer@^1.0.3 +monitor_port = COM7 +build_flags= -D SERIAL_RX_BUFFER_SIZE=512 diff --git a/src/main.cpp b/src/main.cpp index 2e3a128..c910b95 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -40,14 +40,76 @@ #include "STM32LowPower.h" #include "STM32IntRef.h" +#include "sml.h" +#include + void do_send(osjob_t* j); -// for debugging redirect to hardware Serial2 -// Tx on PA2 -#define Serial Serial2 +// #define Serial Serial2 HardwareSerial Serial2(USART2); // or HardWareSerial Serial2 (PA3, PA2); +#define MAX_BUF_SIZE 700 +sml_states_t currentState; + +RingBuf myBuffer; + +void print_buffer(){ + + unsigned int i = 0; + unsigned int j = 0; + char b[5]; + Serial.print(F("Size: ")); + Serial.print(myBuffer.size()); + Serial.println(""); + Serial.println(F("--- ")); + for (j = 0; j < myBuffer.size(); j++) { + i++; + sprintf(b, "0x%02X", myBuffer[j]); + Serial.print(b); + if (j < myBuffer.size() - 1) { + Serial.print(", "); + } + else { + Serial.println(""); + Serial.println(F("--- ")); + } + if ((i % 15) == 0) { + Serial.println(""); + i = 0; + } + } +} + +void readByte(unsigned char currentChar) +{ + Serial.printf("%02x",currentChar); + currentState = smlState(currentChar); + if (currentState == SML_START) { + myBuffer.clear(); + myBuffer.push(0x1B); + myBuffer.push(0x1B); + myBuffer.push(0x1B); + } + else { + if (myBuffer.size() < MAX_BUF_SIZE) { + myBuffer.push(currentChar); + } + else { + Serial.print(F(">>> Message larger than MAX_BUF_SIZE\n")); + } + } + if (currentState == SML_UNEXPECTED) { + Serial.print(F(">>> Unexpected byte\n")); + } + if (currentState == SML_FINAL) { + Serial.print(F(">>> Successfully received a complete message!\n")); + print_buffer(); + } + if (currentState == SML_CHECKSUM_ERROR) { + Serial.print(F(">>> Checksum error.\n")); + } +} // include security credentials OTAA, check secconfig_example.h for more information #include "secconfig.h" @@ -72,106 +134,107 @@ static osjob_t sendjob; // Sleep this many microseconds. Notice that the sending and waiting for downlink // will extend the time between send packets. You have to extract this time -#define SLEEP_INTERVAL 300000 +// #define SLEEP_INTERVAL 300000 +#define SLEEP_INTERVAL 30000 // Pin mapping for the MiniPill LoRa with the RFM95 LoRa chip const lmic_pinmap lmic_pins = -{ - .nss = PA4, - .rxtx = LMIC_UNUSED_PIN, - .rst = PA9, - .dio = {PA10, PB4, PB5}, + { + .nss = PA4, + .rxtx = LMIC_UNUSED_PIN, + .rst = PA9, + .dio = {PA10, PB4, PB5}, }; // event called by os on events -void onEvent (ev_t ev) +void onEvent(ev_t ev) { - // Serial.print(os_getTime()); - // Serial.print(": "); - switch(ev) { - case EV_SCAN_TIMEOUT: - // Serial.println(F("EV_SCAN_TIMEOUT")); - break; - case EV_BEACON_FOUND: - // Serial.println(F("EV_BEACON_FOUND")); - break; - case EV_BEACON_MISSED: - // Serial.println(F("EV_BEACON_MISSED")); - break; - case EV_BEACON_TRACKED: - // Serial.println(F("EV_BEACON_TRACKED")); - break; - case EV_JOINING: - // Serial.println(F("EV_JOINING")); - break; - case EV_JOINED: - // Serial.println(F("EV_JOINED")); + Serial.print(os_getTime()); + Serial.print(": "); + switch (ev) + { + case EV_SCAN_TIMEOUT: + Serial.println(F("EV_SCAN_TIMEOUT")); + break; + case EV_BEACON_FOUND: + Serial.println(F("EV_BEACON_FOUND")); + break; + case EV_BEACON_MISSED: + Serial.println(F("EV_BEACON_MISSED")); + break; + case EV_BEACON_TRACKED: + Serial.println(F("EV_BEACON_TRACKED")); + break; + case EV_JOINING: + Serial.println(F("EV_JOINING")); + break; + case EV_JOINED: + Serial.println(F("EV_JOINED")); - // Disable link check validation (automatically enabled - // during join, but not supported by TTN at this time). - LMIC_setLinkCheckMode(0); - break; - case EV_RFU1: - // Serial.println(F("EV_RFU1")); - break; - case EV_JOIN_FAILED: - // Serial.println(F("EV_JOIN_FAILED")); - break; - case EV_REJOIN_FAILED: - // Serial.println(F("EV_REJOIN_FAILED")); - break; - break; - case EV_TXCOMPLETE: - // Serial.println(F("EV_TXCOMPLETE (includes waiting for RX windows)")); - if (LMIC.txrxFlags & TXRX_ACK) - // Serial.println(F("Received ack")); - if (LMIC.dataLen) - { - // Serial.print(F("Received ")); - // Serial.print(LMIC.dataLen); - // Serial.println(F(" bytes of payload")); - // print data - for (int i = 0; i < LMIC.dataLen; i++) - { - // Serial.print(LMIC.frame[LMIC.dataBeg + i], HEX); - } - // Serial.println(); - } - // Serial.println("going to sleep"); - // reset low power parameters - LowPower.begin(); - delay(100); - // set PA6 to analog to reduce power due to currect flow on DIO on BME280 - pinMode(PA6, INPUT_ANALOG); - // take SLEEP_INTERVAL time to sleep - LowPower.deepSleep(SLEEP_INTERVAL); + // Disable link check validation (automatically enabled + // during join, but not supported by TTN at this time). + LMIC_setLinkCheckMode(0); + break; + case EV_RFU1: + Serial.println(F("EV_RFU1")); + break; + case EV_JOIN_FAILED: + Serial.println(F("EV_JOIN_FAILED")); + break; + case EV_REJOIN_FAILED: + Serial.println(F("EV_REJOIN_FAILED")); + break; + break; + case EV_TXCOMPLETE: + Serial.println(F("EV_TXCOMPLETE (includes waiting for RX windows)")); + if (LMIC.txrxFlags & TXRX_ACK) + Serial.println(F("Received ack")); + if (LMIC.dataLen) + { + Serial.print(F("Received ")); + Serial.print(LMIC.dataLen); + Serial.println(F(" bytes of payload")); + // print data + for (int i = 0; i < LMIC.dataLen; i++) + { + Serial.print(LMIC.frame[LMIC.dataBeg + i], HEX); + } + Serial.println(); + } + Serial.println("going to sleep"); + // reset low power parameters + LowPower.begin(); + delay(100); + // set PA6 to analog to reduce power due to currect flow on DIO on BME280 + pinMode(PA6, INPUT_ANALOG); + // take SLEEP_INTERVAL time to sleep + LowPower.deepSleep(SLEEP_INTERVAL); - // Serial.println("queing next job"); - // next transmission - do_send(&sendjob); + // Serial.println("queing next job"); + // next transmission + do_send(&sendjob); - - break; - case EV_LOST_TSYNC: - // Serial.println(F("EV_LOST_TSYNC")); - break; - case EV_RESET: - // Serial.println(F("EV_RESET")); - break; - case EV_RXCOMPLETE: - // data received in ping slot - // Serial.println(F("EV_RXCOMPLETE")); - break; - case EV_LINK_DEAD: - // Serial.println(F("EV_LINK_DEAD")); - break; - case EV_LINK_ALIVE: - // Serial.println(F("EV_LINK_ALIVE")); - break; - default: - // Serial.println(F("Unknown event")); - break; - } + break; + case EV_LOST_TSYNC: + Serial.println(F("EV_LOST_TSYNC")); + break; + case EV_RESET: + Serial.println(F("EV_RESET")); + break; + case EV_RXCOMPLETE: + // data received in ping slot + Serial.println(F("EV_RXCOMPLETE")); + break; + case EV_LINK_DEAD: + Serial.println(F("EV_LINK_DEAD")); + break; + case EV_LINK_ALIVE: + Serial.println(F("EV_LINK_ALIVE")); + break; + default: + Serial.println(F("Unknown event")); + break; + } } // @@ -180,9 +243,19 @@ void do_send(osjob_t* j) // Check if there is not a current TX/RX job running if (LMIC.opmode & OP_TXRXPEND) { -// Serial.println(F("OP_TXRXPEND, not sending")); + Serial.println(F("OP_TXRXPEND, not sending")); } else { + Serial.println(F("beginning to read SML ...")); + while( Serial2.available() <= 0 ) { + __asm__("nop\n\t"); + } + + while (Serial2.available() > 0) { + readByte(Serial2.read()); + } + + Serial.println(F("end of reading SML!!")); // Prepare upstream data transmission at the next possible time. uint8_t dataLength = 2; uint8_t data[dataLength]; @@ -193,7 +266,7 @@ void do_send(osjob_t* j) data[1] = (vcc & 0xff); LMIC_setTxData2(1, data, sizeof(data), 0); - // Serial.println(F("Packet queued")); + Serial.println(F("Packet queued")); // signal with LED that data is queued digitalWrite(SIGNAL_LED, LOW); delay(1000); @@ -204,10 +277,11 @@ void do_send(osjob_t* j) void setup() { - // Serial.begin(9600); + Serial.begin(9600); + Serial2.begin(9600); // delay at startup for debugging reasons delay(8000); - // Serial.println(F("Starting")); + Serial.println(F("Starting")); // set pin as OUPUT for LED pinMode(SIGNAL_LED, OUTPUT); @@ -264,6 +338,9 @@ void loop() PB4 // DIO1 - RFM95W PB5 // DIO2 - RFM95W + PB6 // USART1_TX + PB7 // USART1_RX + PA9 // USART1_TX RST - RFM95W VCC - 3V3