Sfoglia il codice sorgente

增加CAN数据解析工具

chenxiaowei 1 anno fa
parent
commit
bb78e2523a
100 ha cambiato i file con 11489 aggiunte e 0 eliminazioni
  1. 340 0
      src/tool/adc_cantool/LICENSE
  2. 77 0
      src/tool/adc_cantool/adc_cantool.pro
  3. BIN
      src/tool/adc_cantool/assets/adc.png
  4. BIN
      src/tool/adc_cantool/assets/cangaroo.png
  5. 129 0
      src/tool/adc_cantool/assets/cangaroo.svg
  6. BIN
      src/tool/adc_cantool/assets/mdibg.png
  7. 73 0
      src/tool/adc_cantool/assets/mdibg.svg
  8. 10 0
      src/tool/adc_cantool/canifconfig/canifconfig.pro
  9. 382 0
      src/tool/adc_cantool/canifconfig/main.c
  10. 5 0
      src/tool/adc_cantool/cantool.qrc
  11. 292 0
      src/tool/adc_cantool/core/Backend.cpp
  12. 114 0
      src/tool/adc_cantool/core/Backend.h
  13. 89 0
      src/tool/adc_cantool/core/CanDb.cpp
  14. 73 0
      src/tool/adc_cantool/core/CanDb.h
  15. 112 0
      src/tool/adc_cantool/core/CanDbMessage.cpp
  16. 73 0
      src/tool/adc_cantool/core/CanDbMessage.h
  17. 49 0
      src/tool/adc_cantool/core/CanDbNode.cpp
  18. 43 0
      src/tool/adc_cantool/core/CanDbNode.h
  19. 229 0
      src/tool/adc_cantool/core/CanDbSignal.cpp
  20. 106 0
      src/tool/adc_cantool/core/CanDbSignal.h
  21. 349 0
      src/tool/adc_cantool/core/CanMessage.cpp
  22. 104 0
      src/tool/adc_cantool/core/CanMessage.h
  23. 215 0
      src/tool/adc_cantool/core/CanTrace.cpp
  24. 87 0
      src/tool/adc_cantool/core/CanTrace.h
  25. 44 0
      src/tool/adc_cantool/core/ConfigurableWidget.cpp
  26. 43 0
      src/tool/adc_cantool/core/ConfigurableWidget.h
  27. 63 0
      src/tool/adc_cantool/core/Log.cpp
  28. 43 0
      src/tool/adc_cantool/core/Log.h
  29. 170 0
      src/tool/adc_cantool/core/LogModel.cpp
  30. 71 0
      src/tool/adc_cantool/core/LogModel.h
  31. 215 0
      src/tool/adc_cantool/core/MeasurementInterface.cpp
  32. 92 0
      src/tool/adc_cantool/core/MeasurementInterface.h
  33. 159 0
      src/tool/adc_cantool/core/MeasurementNetwork.cpp
  34. 61 0
      src/tool/adc_cantool/core/MeasurementNetwork.h
  35. 145 0
      src/tool/adc_cantool/core/MeasurementSetup.cpp
  36. 63 0
      src/tool/adc_cantool/core/MeasurementSetup.h
  37. 30 0
      src/tool/adc_cantool/core/core.pri
  38. 73 0
      src/tool/adc_cantool/core/portable_endian.h
  39. 4 0
      src/tool/adc_cantool/debian/cangaroo.install
  40. 39 0
      src/tool/adc_cantool/debian/changelog
  41. 1 0
      src/tool/adc_cantool/debian/compat
  42. 17 0
      src/tool/adc_cantool/debian/control
  43. 29 0
      src/tool/adc_cantool/debian/copyright
  44. 2 0
      src/tool/adc_cantool/debian/dirs
  45. 6 0
      src/tool/adc_cantool/debian/rules
  46. 1 0
      src/tool/adc_cantool/debian/source/format
  47. BIN
      src/tool/adc_cantool/docs/can_signal_byteorder.ods
  48. 13 0
      src/tool/adc_cantool/driver/CANBlastDriver/CANBlastDriver.pri
  49. 140 0
      src/tool/adc_cantool/driver/CANBlastDriver/CANBlasterDriver.cpp
  50. 43 0
      src/tool/adc_cantool/driver/CANBlastDriver/CANBlasterDriver.h
  51. 314 0
      src/tool/adc_cantool/driver/CANBlastDriver/CANBlasterInterface.cpp
  52. 237 0
      src/tool/adc_cantool/driver/CANBlastDriver/CANBlasterInterface.h
  53. 107 0
      src/tool/adc_cantool/driver/CanDriver.cpp
  54. 63 0
      src/tool/adc_cantool/driver/CanDriver.h
  55. 101 0
      src/tool/adc_cantool/driver/CanInterface.cpp
  56. 92 0
      src/tool/adc_cantool/driver/CanInterface.h
  57. 92 0
      src/tool/adc_cantool/driver/CanListener.cpp
  58. 60 0
      src/tool/adc_cantool/driver/CanListener.h
  59. 49 0
      src/tool/adc_cantool/driver/CanTiming.cpp
  60. 37 0
      src/tool/adc_cantool/driver/CanTiming.h
  61. 80 0
      src/tool/adc_cantool/driver/CandleApiDriver/CandleApiDriver.cpp
  62. 44 0
      src/tool/adc_cantool/driver/CandleApiDriver/CandleApiDriver.h
  63. 24 0
      src/tool/adc_cantool/driver/CandleApiDriver/CandleApiDriver.pri
  64. 390 0
      src/tool/adc_cantool/driver/CandleApiDriver/CandleApiInterface.cpp
  65. 70 0
      src/tool/adc_cantool/driver/CandleApiDriver/CandleApiInterface.h
  66. 33 0
      src/tool/adc_cantool/driver/CandleApiDriver/CandleApiTiming.cpp
  67. 31 0
      src/tool/adc_cantool/driver/CandleApiDriver/CandleApiTiming.h
  68. 66 0
      src/tool/adc_cantool/driver/CandleApiDriver/api/LICENSE
  69. 589 0
      src/tool/adc_cantool/driver/CandleApiDriver/api/candle.c
  70. 166 0
      src/tool/adc_cantool/driver/CandleApiDriver/api/candle.h
  71. 152 0
      src/tool/adc_cantool/driver/CandleApiDriver/api/candle_ctrl_req.c
  72. 37 0
      src/tool/adc_cantool/driver/CandleApiDriver/api/candle_ctrl_req.h
  73. 90 0
      src/tool/adc_cantool/driver/CandleApiDriver/api/candle_defs.h
  74. 37 0
      src/tool/adc_cantool/driver/CandleApiDriver/api/ch_9.h
  75. 176 0
      src/tool/adc_cantool/driver/GenericCanSetupPage.cpp
  76. 43 0
      src/tool/adc_cantool/driver/GenericCanSetupPage.h
  77. 264 0
      src/tool/adc_cantool/driver/GenericCanSetupPage.ui
  78. 107 0
      src/tool/adc_cantool/driver/SLCANDriver/SLCANDriver.cpp
  79. 43 0
      src/tool/adc_cantool/driver/SLCANDriver/SLCANDriver.h
  80. 12 0
      src/tool/adc_cantool/driver/SLCANDriver/SLCANDriver.pri
  81. 736 0
      src/tool/adc_cantool/driver/SLCANDriver/SLCANInterface.cpp
  82. 133 0
      src/tool/adc_cantool/driver/SLCANDriver/SLCANInterface.h
  83. 99 0
      src/tool/adc_cantool/driver/SocketCanDriver/SocketCanDriver.cpp
  84. 43 0
      src/tool/adc_cantool/driver/SocketCanDriver/SocketCanDriver.h
  85. 12 0
      src/tool/adc_cantool/driver/SocketCanDriver/SocketCanDriver.pri
  86. 473 0
      src/tool/adc_cantool/driver/SocketCanDriver/SocketCanInterface.cpp
  87. 115 0
      src/tool/adc_cantool/driver/SocketCanDriver/SocketCanInterface.h
  88. 16 0
      src/tool/adc_cantool/driver/driver.pri
  89. 21 0
      src/tool/adc_cantool/gpl-header.txt
  90. 11 0
      src/tool/adc_cantool/main.cpp
  91. 370 0
      src/tool/adc_cantool/mainwindow.cpp
  92. 99 0
      src/tool/adc_cantool/mainwindow.h
  93. 77 0
      src/tool/adc_cantool/mainwindow.ui
  94. 688 0
      src/tool/adc_cantool/parser/dbc/DbcParser.cpp
  95. 92 0
      src/tool/adc_cantool/parser/dbc/DbcParser.h
  96. 137 0
      src/tool/adc_cantool/parser/dbc/DbcTokens.cpp
  97. 102 0
      src/tool/adc_cantool/parser/dbc/DbcTokens.h
  98. 7 0
      src/tool/adc_cantool/parser/dbc/dbc.pri
  99. 25 0
      src/tool/adc_cantool/scripts/setup_vcan.sh
  100. 109 0
      src/tool/adc_cantool/window/CanStatusWindow/CanStatusWindow.cpp

+ 340 - 0
src/tool/adc_cantool/LICENSE

@@ -0,0 +1,340 @@
+                    GNU GENERAL PUBLIC LICENSE
+                       Version 2, June 1991
+
+ Copyright (C) 1989, 1991 Free Software Foundation, Inc., <http://fsf.org/>
+ 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.
+
+                            Preamble
+
+  The licenses for most software are designed to take away your
+freedom to share and change it.  By contrast, the GNU General Public
+License is intended to guarantee your freedom to share and change free
+software--to make sure the software is free for all its users.  This
+General Public License applies to most of the Free Software
+Foundation's software and to any other program whose authors commit to
+using it.  (Some other Free Software Foundation software is covered by
+the GNU Lesser General Public License instead.)  You can apply it to
+your programs, too.
+
+  When we speak of free software, we are referring to freedom, not
+price.  Our General Public Licenses are designed to make sure that you
+have the freedom to distribute copies of free software (and charge for
+this service if you wish), that you receive source code or can get it
+if you want it, that you can change the software or use pieces of it
+in new free programs; and that you know you can do these things.
+
+  To protect your rights, we need to make restrictions that forbid
+anyone to deny you these rights or to ask you to surrender the rights.
+These restrictions translate to certain responsibilities for you if you
+distribute copies of the software, or if you modify it.
+
+  For example, if you distribute copies of such a program, whether
+gratis or for a fee, you must give the recipients all the rights that
+you have.  You must make sure that they, too, receive or can get the
+source code.  And you must show them these terms so they know their
+rights.
+
+  We protect your rights with two steps: (1) copyright the software, and
+(2) offer you this license which gives you legal permission to copy,
+distribute and/or modify the software.
+
+  Also, for each author's protection and ours, we want to make certain
+that everyone understands that there is no warranty for this free
+software.  If the software is modified by someone else and passed on, we
+want its recipients to know that what they have is not the original, so
+that any problems introduced by others will not reflect on the original
+authors' reputations.
+
+  Finally, any free program is threatened constantly by software
+patents.  We wish to avoid the danger that redistributors of a free
+program will individually obtain patent licenses, in effect making the
+program proprietary.  To prevent this, we have made it clear that any
+patent must be licensed for everyone's free use or not licensed at all.
+
+  The precise terms and conditions for copying, distribution and
+modification follow.
+
+                    GNU GENERAL PUBLIC LICENSE
+   TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
+
+  0. This License applies to any program or other work which contains
+a notice placed by the copyright holder saying it may be distributed
+under the terms of this General Public License.  The "Program", below,
+refers to any such program or work, and a "work based on the Program"
+means either the Program or any derivative work under copyright law:
+that is to say, a work containing the Program or a portion of it,
+either verbatim or with modifications and/or translated into another
+language.  (Hereinafter, translation is included without limitation in
+the term "modification".)  Each licensee is addressed as "you".
+
+Activities other than copying, distribution and modification are not
+covered by this License; they are outside its scope.  The act of
+running the Program is not restricted, and the output from the Program
+is covered only if its contents constitute a work based on the
+Program (independent of having been made by running the Program).
+Whether that is true depends on what the Program does.
+
+  1. You may copy and distribute verbatim copies of the Program's
+source code as you receive it, in any medium, provided that you
+conspicuously and appropriately publish on each copy an appropriate
+copyright notice and disclaimer of warranty; keep intact all the
+notices that refer to this License and to the absence of any warranty;
+and give any other recipients of the Program a copy of this License
+along with the Program.
+
+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 Program or any portion
+of it, thus forming a work based on the Program, 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) You must cause the modified files to carry prominent notices
+    stating that you changed the files and the date of any change.
+
+    b) You must cause any work that you distribute or publish, that in
+    whole or in part contains or is derived from the Program or any
+    part thereof, to be licensed as a whole at no charge to all third
+    parties under the terms of this License.
+
+    c) If the modified program normally reads commands interactively
+    when run, you must cause it, when started running for such
+    interactive use in the most ordinary way, to print or display an
+    announcement including an appropriate copyright notice and a
+    notice that there is no warranty (or else, saying that you provide
+    a warranty) and that users may redistribute the program under
+    these conditions, and telling the user how to view a copy of this
+    License.  (Exception: if the Program itself is interactive but
+    does not normally print such an announcement, your work based on
+    the Program is not required to print an announcement.)
+
+These requirements apply to the modified work as a whole.  If
+identifiable sections of that work are not derived from the Program,
+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 Program, 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 Program.
+
+In addition, mere aggregation of another work not based on the Program
+with the Program (or with a work based on the Program) on a volume of
+a storage or distribution medium does not bring the other work under
+the scope of this License.
+
+  3. You may copy and distribute the Program (or a work based on it,
+under Section 2) in object code or executable form under the terms of
+Sections 1 and 2 above provided that you also do one of the following:
+
+    a) 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; or,
+
+    b) Accompany it with a written offer, valid for at least three
+    years, to give any third party, for a charge no more than your
+    cost of physically performing source distribution, a complete
+    machine-readable copy of the corresponding source code, to be
+    distributed under the terms of Sections 1 and 2 above on a medium
+    customarily used for software interchange; or,
+
+    c) Accompany it with the information you received as to the offer
+    to distribute corresponding source code.  (This alternative is
+    allowed only for noncommercial distribution and only if you
+    received the program in object code or executable form with such
+    an offer, in accord with Subsection b above.)
+
+The source code for a work means the preferred form of the work for
+making modifications to it.  For an executable work, 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 executable.  However, as a
+special exception, the source code 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.
+
+If distribution of executable or 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 counts as
+distribution of the source code, even though third parties are not
+compelled to copy the source along with the object code.
+
+  4. You may not copy, modify, sublicense, or distribute the Program
+except as expressly provided under this License.  Any attempt
+otherwise to copy, modify, sublicense or distribute the Program 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.
+
+  5. 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 Program or its derivative works.  These actions are
+prohibited by law if you do not accept this License.  Therefore, by
+modifying or distributing the Program (or any work based on the
+Program), you indicate your acceptance of this License to do so, and
+all its terms and conditions for copying, distributing or modifying
+the Program or works based on it.
+
+  6. Each time you redistribute the Program (or any work based on the
+Program), the recipient automatically receives a license from the
+original licensor to copy, distribute or modify the Program 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 to
+this License.
+
+  7. 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 Program at all.  For example, if a patent
+license would not permit royalty-free redistribution of the Program 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 Program.
+
+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.
+
+  8. If the distribution and/or use of the Program is restricted in
+certain countries either by patents or by copyrighted interfaces, the
+original copyright holder who places the Program 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.
+
+  9. The Free Software Foundation may publish revised and/or new versions
+of the General Public License from time to time.  Such new versions will
+be similar in spirit to the present version, but may differ in detail to
+address new problems or concerns.
+
+Each version is given a distinguishing version number.  If the Program
+specifies 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 Program does not specify a version number of
+this License, you may choose any version ever published by the Free Software
+Foundation.
+
+  10. If you wish to incorporate parts of the Program into other free
+programs whose distribution conditions are different, 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
+
+  11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
+FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW.  EXCEPT WHEN
+OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
+PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED
+OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.  THE ENTIRE RISK AS
+TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU.  SHOULD THE
+PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING,
+REPAIR OR CORRECTION.
+
+  12. 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 PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES,
+INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING
+OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED
+TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY
+YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER
+PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE
+POSSIBILITY OF SUCH DAMAGES.
+
+                     END OF TERMS AND CONDITIONS
+
+            How to Apply These Terms to Your New Programs
+
+  If you develop a new program, and you want it to be of the greatest
+possible use to the public, the best way to achieve this is to make it
+free software which everyone can redistribute and change under these terms.
+
+  To do so, attach the following notices to the program.  It is safest
+to attach them to the start of each source file to most effectively
+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.
+
+    {description}
+    Copyright (C) {year}  {fullname}
+
+    This program is free software; you can redistribute it and/or modify
+    it under the terms of the GNU General Public License as published by
+    the Free Software Foundation; either version 2 of the License, or
+    (at your option) any later version.
+
+    This program is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+    GNU General Public License for more details.
+
+    You should have received a copy of the GNU General Public License along
+    with this program; 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.
+
+If the program is interactive, make it output a short notice like this
+when it starts in an interactive mode:
+
+    Gnomovision version 69, Copyright (C) year name of author
+    Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
+    This is free software, and you are welcome to redistribute it
+    under certain conditions; type `show c' for details.
+
+The hypothetical commands `show w' and `show c' should show the appropriate
+parts of the General Public License.  Of course, the commands you use may
+be called something other than `show w' and `show c'; they could even be
+mouse-clicks or menu items--whatever suits your program.
+
+You should also get your employer (if you work as a programmer) or your
+school, if any, to sign a "copyright disclaimer" for the program, if
+necessary.  Here is a sample; alter the names:
+
+  Yoyodyne, Inc., hereby disclaims all copyright interest in the program
+  `Gnomovision' (which makes passes at compilers) written by James Hacker.
+
+  {signature of Ty Coon}, 1 April 1989
+  Ty Coon, President of Vice
+
+This General Public License does not permit incorporating your program into
+proprietary programs.  If your program is a subroutine library, you may
+consider it more useful to permit linking proprietary applications with the
+library.  If this is what you want to do, use the GNU Lesser General
+Public License instead of this License.
+

+ 77 - 0
src/tool/adc_cantool/adc_cantool.pro

@@ -0,0 +1,77 @@
+#-------------------------------------------------
+#
+# Project created by QtCreator 2023-01-12T09:44:18
+#
+#-------------------------------------------------
+lessThan(QT_MAJOR_VERSION, 5): error("requires Qt 5")
+QT       += core gui
+
+greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
+
+QT += charts
+QT += network
+QT += xml
+QT += charts
+QT += serialport
+
+CONFIG += ordered warn_on qt debug_and_release
+CONFIG += c++11
+
+TARGET = adc_cantool
+TEMPLATE = app
+
+CONFIG += warn_on
+CONFIG += link_pkgconfig
+
+#DESTDIR = ./bin
+#MOC_DIR = ./build/moc
+#RCC_DIR = ./build/rcc
+#UI_DIR = ./build/ui
+unix:OBJECTS_DIR = ./build/o/unix
+win32:OBJECTS_DIR = ./build/o/win32
+macx:OBJECTS_DIR = ./build/o/mac
+# The following define makes your compiler emit warnings if you use
+# any feature of Qt which has been marked as deprecated (the exact warnings
+# depend on your compiler). Please consult the documentation of the
+# deprecated API in order to know how to port your code away from it.
+DEFINES += QT_DEPRECATED_WARNINGS
+
+# You can also make your code fail to compile if you use deprecated APIs.
+# In order to do so, uncomment the following line.
+# You can also select to disable deprecated APIs only up to a certain version of Qt.
+#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0
+
+
+SOURCES += \
+        main.cpp \
+        mainwindow.cpp
+
+HEADERS += \
+        mainwindow.h
+
+FORMS += \
+        mainwindow.ui
+
+include($$PWD/core/core.pri)
+include($$PWD/driver/driver.pri)
+include($$PWD/parser/dbc/dbc.pri)
+include($$PWD/window/TraceWindow/TraceWindow.pri)
+include($$PWD/window/SetupDialog/SetupDialog.pri)
+include($$PWD/window/LogWindow/LogWindow.pri)
+include($$PWD/window/GraphWindow/GraphWindow.pri)
+include($$PWD/window/CanStatusWindow/CanStatusWindow.pri)
+include($$PWD/window/RawTxWindow/RawTxWindow.pri)
+include($$PWD/window/ChassisAnalysisWindow/ChassisAnalysisWindow.pri)
+#include($$PWD/window/ShenLanChassisAnalysisWindow/ShenLanChassisAnalysisWindow.pri)
+
+unix:PKGCONFIG += libnl-3.0
+unix:PKGCONFIG += libnl-route-3.0
+unix:include($$PWD/driver/SocketCanDriver/SocketCanDriver.pri)
+
+include($$PWD/driver/CANBlastDriver/CANBlastDriver.pri)
+include($$PWD/driver/SLCANDriver/SLCANDriver.pri)
+
+win32:include($$PWD/driver/CandleApiDriver/CandleApiDriver.pri)
+
+RESOURCES += \
+    cantool.qrc

BIN
src/tool/adc_cantool/assets/adc.png


BIN
src/tool/adc_cantool/assets/cangaroo.png


+ 129 - 0
src/tool/adc_cantool/assets/cangaroo.svg

@@ -0,0 +1,129 @@
+<?xml version="1.0" encoding="UTF-8" standalone="no"?>
+<!-- Created with Inkscape (http://www.inkscape.org/) -->
+
+<svg
+   xmlns:dc="http://purl.org/dc/elements/1.1/"
+   xmlns:cc="http://creativecommons.org/ns#"
+   xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
+   xmlns:svg="http://www.w3.org/2000/svg"
+   xmlns="http://www.w3.org/2000/svg"
+   xmlns:xlink="http://www.w3.org/1999/xlink"
+   xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd"
+   xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape"
+   width="200mm"
+   height="200mm"
+   viewBox="0 0 708.66142 708.66141"
+   id="svg2"
+   version="1.1"
+   inkscape:version="0.91 r13725"
+   sodipodi:docname="sign.svg">
+  <defs
+     id="defs4">
+    <linearGradient
+       inkscape:collect="always"
+       id="linearGradient4828">
+      <stop
+         style="stop-color:#ffe2a0;stop-opacity:1;"
+         offset="0"
+         id="stop4830" />
+      <stop
+         style="stop-color:#ffe2a0;stop-opacity:0;"
+         offset="1"
+         id="stop4832" />
+    </linearGradient>
+    <radialGradient
+       inkscape:collect="always"
+       xlink:href="#linearGradient4828"
+       id="radialGradient4834"
+       cx="-213.18985"
+       cy="735.4455"
+       fx="-213.18985"
+       fy="735.4455"
+       r="209.90335"
+       gradientUnits="userSpaceOnUse" />
+  </defs>
+  <sodipodi:namedview
+     id="base"
+     pagecolor="#ffffff"
+     bordercolor="#666666"
+     borderopacity="1.0"
+     inkscape:pageopacity="0.0"
+     inkscape:pageshadow="2"
+     inkscape:zoom="0.98994949"
+     inkscape:cx="386.90375"
+     inkscape:cy="377.91498"
+     inkscape:document-units="px"
+     inkscape:current-layer="layer1"
+     showgrid="false"
+     showguides="true"
+     inkscape:guide-bbox="true"
+     inkscape:window-width="1920"
+     inkscape:window-height="1020"
+     inkscape:window-x="0"
+     inkscape:window-y="0"
+     inkscape:window-maximized="1" />
+  <metadata
+     id="metadata7">
+    <rdf:RDF>
+      <cc:Work
+         rdf:about="">
+        <dc:format>image/svg+xml</dc:format>
+        <dc:type
+           rdf:resource="http://purl.org/dc/dcmitype/StillImage" />
+        <dc:title />
+      </cc:Work>
+    </rdf:RDF>
+  </metadata>
+  <g
+     inkscape:label="Ebene 1"
+     inkscape:groupmode="layer"
+     id="layer1"
+     transform="translate(0,-343.70079)">
+    <rect
+       style="fill:none;fill-opacity:1;stroke:none;stroke-width:8.30000019;stroke-linecap:round;stroke-linejoin:round;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1"
+       id="rect4234"
+       width="2006.163"
+       height="1337.442"
+       x="-719.22864"
+       y="-0.21672098"
+       ry="16.6278" />
+    <g
+       id="g4836"
+       inkscape:export-xdpi="40.310001"
+       inkscape:export-ydpi="40.310001">
+      <rect
+         transform="matrix(0.70710678,-0.70710678,0.70710678,0.70710678,0,0)"
+         style="fill:#eda800;fill-opacity:1;stroke:none;stroke-width:8.32127285;stroke-linecap:round;stroke-linejoin:round;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1"
+         id="rect4192"
+         width="419.8067"
+         height="419.8067"
+         x="-443.45035"
+         y="545.185"
+         ry="26.729334" />
+      <rect
+         transform="matrix(0.70710678,-0.70710678,0.70710678,0.70710678,0,0)"
+         ry="15.630058"
+         y="557.78033"
+         x="-430.85504"
+         height="394.61609"
+         width="394.61609"
+         id="rect4190"
+         style="fill:none;fill-opacity:1;stroke:#000000;stroke-width:8.61946106;stroke-linecap:round;stroke-linejoin:round;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1" />
+      <rect
+         ry="26.729334"
+         y="545.185"
+         x="-443.45035"
+         height="419.8067"
+         width="419.8067"
+         id="rect4802"
+         style="fill:url(#radialGradient4834);fill-opacity:1;stroke:none;stroke-width:8.32127285;stroke-linecap:round;stroke-linejoin:round;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1"
+         transform="matrix(0.70710678,-0.70710678,0.70710678,0.70710678,0,0)" />
+      <path
+         sodipodi:nodetypes="cccccccccccccccccccccccccccccccccc"
+         inkscape:connector-curvature="0"
+         id="path4147"
+         d="m 149.37797,666.06055 c 36.85202,68.27225 98.31598,9.21863 133.45962,-34.1469 53.33162,-69.83408 100.80365,-71.32307 143.01904,-20.61564 10.87924,12.85652 19.30216,26.25099 26.12101,28.11203 8.87164,0.39217 17.64483,10.43161 24.89623,1.61237 1.83635,-1.92185 4.31807,-4.73544 12.52974,-8.50183 5.52361,-1.27938 -19.63483,-5.89752 -24.59044,-19.08144 -1.12994,-3.53833 -0.11983,-6.185 6.58541,-2.6688 14.34879,1.96546 20.95743,7.42176 28.41231,16.97406 5.04585,-3.69521 16.76491,5.70699 18.97359,8.13619 14.75199,6.79698 24.03482,14.12345 26.40937,18.02486 4.01044,5.23554 -0.89719,9.98884 -10.17097,7.32428 -12.26094,0.096 -12.0494,4.0172 -33.13885,3.449 -3.45976,5.64441 -4.23485,10.65196 -22.89809,22.23524 -3.14976,5.5128 -4.58537,17.95946 -27.94172,24.09806 l -26.0148,37.41813 c -9.19812,12.57436 -25.78056,-3.7517 -12.11947,-13.99947 -2.36604,8.63596 3.75645,16.93693 6.88137,7.53009 -0.24723,-18.16238 3.80973,-21.32505 5.02217,-37.9579 -11.44974,-0.95836 -9.2316,-2.61478 -14.02838,-4.46019 -2.96842,1.50841 -12.72011,15.18724 -15.0803,26.34009 7.49746,52.38941 -1.11195,57.51121 23.04091,61.65535 21.05587,5.39221 52.17215,14.12286 66.23052,20.37982 6.38897,-1.65469 11.18169,-0.78625 14.00807,2.71867 5.90335,0.80925 9.47167,3.0768 9.72272,6.22069 -4.88251,-2.946 -9.20347,-1.09013 -12.21099,2.02749 -13.90963,0.88066 -20.05007,-1.48264 -21.42356,-4.53159 -26.0187,-3.31013 -63.37293,-9.98546 -76.66616,-11.77218 -13.92805,0.17623 -13.35645,-11.68119 -14.69203,-17.00635 -8.44769,-29.03516 -17.45108,-48.6236 -26.58765,-65.94782 -0.95035,-1.81134 -1.41819,-3.62267 -1.63294,-5.43401 -28.26207,-3.09944 -38.91315,-27.2472 -58.24412,-43.13013 l -58.78012,32.78603 c -47.9789,29.96939 -93.14922,-9.54483 -89.09149,-37.7882 z"
+         style="fill:#000000;fill-opacity:1;fill-rule:evenodd;stroke:none;stroke-width:1px;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1" />
+    </g>
+  </g>
+</svg>

BIN
src/tool/adc_cantool/assets/mdibg.png


File diff suppressed because it is too large
+ 73 - 0
src/tool/adc_cantool/assets/mdibg.svg


+ 10 - 0
src/tool/adc_cantool/canifconfig/canifconfig.pro

@@ -0,0 +1,10 @@
+TEMPLATE = app
+CONFIG += console c++11
+CONFIG -= app_bundle
+CONFIG -= qt
+CONFIG += link_pkgconfig
+
+SOURCES += main.c
+
+PKGCONFIG += libnl-3.0
+PKGCONFIG += libnl-route-3.0

+ 382 - 0
src/tool/adc_cantool/canifconfig/main.c

@@ -0,0 +1,382 @@
+#include <unistd.h>
+#include <stdio.h>
+#include <stdbool.h>
+#include <errno.h>
+
+#include <netinet/in.h>
+#include <linux/if.h>
+#include <linux/if_arp.h>
+#include <linux/can.h>
+#include <linux/can/raw.h>
+#include <linux/can/netlink.h>
+#include <netlink/version.h>
+#include <netlink/route/link.h>
+#include <netlink/route/link/can.h>
+
+struct opts {
+
+    char    *ifname;
+    bool     do_bring_up;
+    bool     do_bring_down;
+
+    bool     do_set_bitrate;
+    unsigned bitrate;
+
+    bool     do_set_sample_point;
+    unsigned sample_point;
+
+    bool     do_set_fd_enable;
+    bool     fd_enable;
+
+    bool     do_set_fd_bitrate;
+    unsigned fd_bitrate;
+
+    bool     do_set_fd_sample_point;
+    unsigned fd_sample_point;
+
+    bool     do_set_restart_ms;
+    unsigned restart_ms;
+
+    bool     do_set_mode_listen_only;
+    bool     mode_listen_only;
+
+    bool     do_set_mode_oneshot;
+    bool     mode_oneshot;
+
+    bool     do_set_mode_triple_sampling;
+    bool     mode_triple_sampling;
+
+};
+
+struct nldata {
+    struct nl_sock *sock;
+    struct nl_cache *cache;
+    struct rtnl_link *link;
+};
+
+
+void print_usage(char *program_name)
+{
+    fprintf(
+        stderr,
+        "Usage: %s -i <socketcan interface>\n"
+        "  -d          bring interface down before configuration\n"
+        "  -u          bring interface up after configuration\n"
+        "  -b <n>      set bitrate to n bits per second\n"
+        "  -p <n>      set sample point to n/1000, e.g. 875 for 87.5%%\n"
+        "  -f <on|off> enable/disable CanFD\n"
+        "  -B <n>      set fd bitrate to n bits per second\n"
+        "  -P <n>      set fd sample point to n/1000, e.g. 875 for 87.5%%\n"
+        "  -r <n>      set auto restart time to n milliseconds, 0 for disable\n"
+        "  -l <on|off> enable/disable listen-only mode\n"
+        "  -o <on|off> enable/disable one-shot mode\n"
+        "  -3 <on|off> enable/disable triple-sampling mode\n"
+        "\n",
+         program_name
+    );
+}
+
+bool atobool(char *name, char *arg) {
+    if (strcmp(arg, "on")==0) {
+        return true;
+    } else if (strcmp(arg, "off")==0) {
+        return false;
+    } else {
+        fprintf(stderr, "Error: Argument of %s must be \"on\" or \"off\", not \"%s\"\n", name, arg);
+        exit(EXIT_FAILURE);
+    }
+}
+
+int parse_opts(int argc, char *argv[], struct opts *opts)
+{
+    int opt;
+    memset(opts, 0, sizeof(*opts));
+
+    while ((opt = getopt(argc, argv, "i:udb:p:f:B:P:r:l:o:3:h")) != -1) {
+
+        switch (opt) {
+
+        case 'i':
+            opts->ifname = optarg;
+            break;
+
+        case 'u':
+            opts->do_bring_up = true;
+            break;
+
+        case 'd':
+            opts->do_bring_down = true;
+            break;
+
+        case 'b':
+            opts->do_set_bitrate = true;
+            opts->bitrate = atoi(optarg);
+            break;
+
+        case 'p':
+            opts->do_set_sample_point = true;
+            opts->sample_point = atoi(optarg);
+            break;
+
+        case 'f':
+            opts->do_set_fd_enable = true;
+            opts->fd_enable = atobool("-f", optarg);
+            break;
+
+        case 'B':
+            opts->do_set_fd_bitrate = true;
+            opts->fd_bitrate = atoi(optarg);
+            break;
+
+        case 'P':
+            opts->do_set_fd_sample_point = true;
+            opts->fd_sample_point = atoi(optarg);
+            break;
+
+        case 'r':
+            opts->do_set_restart_ms = true;
+            opts->restart_ms = atoi(optarg);
+            break;
+
+        case 'l':
+            opts->do_set_mode_listen_only = true;
+            opts->mode_listen_only = atobool("-l", optarg);
+            break;
+
+        case 'o':
+            opts->do_set_mode_oneshot = true;
+            opts->mode_oneshot = atobool("-o", optarg);
+            break;
+
+        case '3':
+            opts->do_set_mode_triple_sampling = true;
+            opts->mode_triple_sampling = atobool("-3", optarg);
+            break;
+
+        case 'h':
+        default:
+            print_usage(argv[0]);
+            return EXIT_FAILURE;
+        }
+    }
+
+    if (opts->ifname == 0) {
+        print_usage(argv[0]);
+        return EXIT_FAILURE;
+    }
+
+
+    return EXIT_SUCCESS;
+}
+
+
+int netlink_connect(struct nldata *nldata, char *ifname)
+{
+
+    memset(nldata, 0, sizeof(*nldata));
+
+    nldata->sock = nl_socket_alloc();
+    if ( nldata->sock == NULL ) {
+        perror("cannot allocate netlink socket");
+        return EXIT_FAILURE;
+    }
+
+    if ( nl_connect(nldata->sock, NETLINK_ROUTE) != 0 ) {
+        perror("cannot connect to kernel netlink interface");
+    } else {
+
+        if ( rtnl_link_alloc_cache(nldata->sock, AF_UNSPEC, &nldata->cache) != 0 ) {
+            perror("cannot allocate link cache");
+        } else {
+
+            nldata->link = rtnl_link_get_by_name(nldata->cache, ifname);
+            if (nldata->link == 0) {
+                fprintf(stderr, "cannot find interface %s\n", ifname);
+            } else {
+                if (rtnl_link_is_can(nldata->link)) {
+
+                    return EXIT_SUCCESS;
+
+                } else {
+                    fprintf(stderr, "not a can interface: %s\n", ifname);
+                }
+
+                rtnl_link_put(nldata->link);
+                nldata->link = NULL;
+
+            }
+
+            nl_cache_free(nldata->cache);
+            nldata->cache = NULL;
+
+        }
+        nl_close(nldata->sock);
+    }
+
+    nl_socket_free(nldata->sock);
+    nldata->sock = NULL;
+    return EXIT_FAILURE;
+}
+
+int netlink_close(struct nldata *nldata)
+{
+    if (nldata->link) {
+        rtnl_link_put(nldata->link);
+        nldata->link = NULL;
+    }
+
+    if (nldata->cache) {
+        nl_cache_free(nldata->cache);
+        nldata->cache = NULL;
+    }
+
+    if (nldata->sock) {
+        nl_close(nldata->sock);
+        nl_socket_free(nldata->sock);
+        nldata->sock = NULL;
+    }
+
+    return EXIT_SUCCESS;
+}
+
+
+bool netlink_new_can_request(struct rtnl_link **req)
+{
+    if (*req != NULL) {
+        rtnl_link_put(*req);
+    }
+
+    *req = rtnl_link_alloc();
+    if (*req != NULL) {
+        if (rtnl_link_set_type(*req, "can") != 0) {
+            rtnl_link_put(*req);
+            *req = NULL;
+        }
+    }
+
+    if (*req == NULL) {
+        fprintf(stderr, "cannot create netlink request object\n");
+    }
+
+    return *req != NULL;
+}
+
+
+int main(int argc, char *argv[])
+{
+
+    struct opts opts;
+    struct nldata nldata;
+    struct rtnl_link *req = NULL;
+    int retval = EXIT_FAILURE;
+
+    if (parse_opts(argc, argv, &opts) != 0) {
+        exit(EXIT_FAILURE);
+    }
+
+    if (netlink_connect(&nldata, opts.ifname) != 0) {
+        exit(EXIT_FAILURE);
+    }
+
+    do {
+
+        if (opts.do_bring_down) {
+
+            if (!netlink_new_can_request(&req)) {
+                break;
+            }
+
+            rtnl_link_unset_flags(req, IFF_UP);
+            if (rtnl_link_change(nldata.sock, nldata.link, req, 0) != 0) {
+                perror("cannot bring interface down");
+                break;
+            }
+
+        }
+
+        if (!netlink_new_can_request(&req)) {
+            exit(EXIT_FAILURE);
+        }
+
+        if (opts.do_set_bitrate) {
+            if (rtnl_link_can_set_bitrate(req, opts.bitrate) != 0) {
+                fprintf(stderr, "cannot set bitrate to %d\n", opts.bitrate);
+                break;
+            }
+        }
+
+        if (opts.do_set_sample_point) {
+            if (rtnl_link_can_set_sample_point(req, opts.sample_point) != 0) {
+                fprintf(stderr, "cannot set sample point to %.3f\n", opts.sample_point/1000.0);
+                break;
+            }
+        }
+
+        if (opts.do_set_fd_enable || opts.do_set_fd_bitrate || opts.do_set_fd_sample_point) {
+            fprintf(stderr, "warning: canfd functions not implemented yet.\n");
+        }
+
+        if (opts.do_set_restart_ms) {
+            if (rtnl_link_can_set_restart_ms(req, opts.restart_ms) != 0) {
+                fprintf(stderr, "cannot set restart_ms to %d\n", opts.restart_ms);
+            }
+        }
+
+        if (opts.do_set_mode_listen_only) {
+            if (opts.mode_listen_only) {
+                printf("setting listen-only flag to true\n");
+                rtnl_link_can_set_ctrlmode(nldata.link, CAN_CTRLMODE_LISTENONLY);
+            } else {
+                printf("setting listen-only flag to false\n");
+                rtnl_link_can_unset_ctrlmode(nldata.link, CAN_CTRLMODE_LISTENONLY);
+            }
+        }
+
+        if (opts.do_set_mode_triple_sampling) {
+            if (opts.mode_triple_sampling) {
+                rtnl_link_can_set_ctrlmode(req, CAN_CTRLMODE_3_SAMPLES);
+            } else {
+                rtnl_link_can_unset_ctrlmode(req, CAN_CTRLMODE_3_SAMPLES);
+            }
+        }
+
+        if (opts.do_set_mode_oneshot) {
+            if (opts.mode_oneshot) {
+                rtnl_link_can_set_ctrlmode(req, CAN_CTRLMODE_ONE_SHOT);
+            } else {
+                rtnl_link_can_unset_ctrlmode(req, CAN_CTRLMODE_ONE_SHOT);
+            }
+        }
+
+        if (rtnl_link_change(nldata.sock, nldata.link, req, 0) != 0) {
+            perror("cannot set link configuration");
+            break;
+        }
+
+        if (opts.do_bring_up) {
+
+            if (!netlink_new_can_request(&req)) {
+                break;
+            }
+
+            rtnl_link_set_flags(req, IFF_UP);
+            if (rtnl_link_change(nldata.sock, nldata.link, req, 0) != 0) {
+                perror("cannot bring interface up");
+                break;
+            }
+
+        }
+
+        retval = EXIT_SUCCESS;
+
+    } while (false);
+
+
+    if (req != NULL) {
+        rtnl_link_put(req);
+    }
+
+    netlink_close(&nldata);
+    exit(retval);
+
+}

+ 5 - 0
src/tool/adc_cantool/cantool.qrc

@@ -0,0 +1,5 @@
+<RCC>
+    <qresource prefix="/">
+        <file>assets/adc.png</file>
+    </qresource>
+</RCC>

+ 292 - 0
src/tool/adc_cantool/core/Backend.cpp

@@ -0,0 +1,292 @@
+/*
+
+  Copyright (c) 2015, 2016 Hubert Denkmair <hubert@denkmair.de>
+
+  This file is part of cangaroo.
+
+  cangaroo is free software: you can redistribute it and/or modify
+  it under the terms of the GNU General Public License as published by
+  the Free Software Foundation, either version 2 of the License, or
+  (at your option) any later version.
+
+  cangaroo 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 General Public License for more details.
+
+  You should have received a copy of the GNU General Public License
+  along with cangaroo.  If not, see <http://www.gnu.org/licenses/>.
+
+*/
+
+#include "Backend.h"
+#include "LogModel.h"
+
+#include <QDateTime>
+
+#include <core/CanTrace.h>
+#include <core/MeasurementSetup.h>
+#include <core/MeasurementNetwork.h>
+#include <core/MeasurementInterface.h>
+#include <driver/CanDriver.h>
+#include <driver/CanInterface.h>
+#include <driver/CanListener.h>
+#include <parser/dbc/DbcParser.h>
+
+Backend *Backend::_instance = 0;
+
+Backend::Backend()
+  : QObject(0),
+    _measurementRunning(false),
+    _measurementStartTime(0),
+    _setup(this)
+{
+    _logModel = new LogModel(*this);
+
+    setDefaultSetup();
+    _trace = new CanTrace(*this, this, 100);
+
+    connect(&_setup, SIGNAL(onSetupChanged()), this, SIGNAL(onSetupChanged()));
+}
+
+Backend &Backend::instance()
+{
+    if (!_instance) {
+        _instance = new Backend;
+    }
+    return *_instance;
+}
+
+Backend::~Backend()
+{
+    delete _trace;
+}
+
+void Backend::addCanDriver(CanDriver &driver)
+{
+    driver.init(_drivers.size());
+    _drivers.append(&driver);
+}
+
+bool Backend::startMeasurement()
+{
+    log_info("Starting measurement");
+
+    _measurementStartTime = QDateTime::currentMSecsSinceEpoch();
+    _timerSinceStart.start();
+
+    int i=0;
+    foreach (MeasurementNetwork *network, _setup.getNetworks()) {
+        i++;
+        foreach (MeasurementInterface *mi, network->interfaces()) {
+
+            CanInterface *intf = getInterfaceById(mi->canInterface());
+            if (intf) {
+                intf->applyConfig(*mi);
+
+                log_info(QString("Listening on interface: %1").arg(intf->getName()));
+                CanListener *listener = new CanListener(0, *this, *intf);
+                listener->startThread();
+                _listeners.append(listener);
+            }
+        }
+    }
+
+    _measurementRunning = true;
+    emit beginMeasurement();
+    return true;
+}
+
+bool Backend::stopMeasurement()
+{
+    if (_measurementRunning) {
+        foreach (CanListener *listener, _listeners) {
+            listener->requestStop();
+        }
+
+        foreach (CanListener *listener, _listeners) {
+            log_info(QString("Closing interface: %1").arg(getInterfaceName(listener->getInterfaceId())));
+            listener->waitFinish();
+        }
+
+        qDeleteAll(_listeners);
+        _listeners.clear();
+
+        log_info("Measurement stopped");
+
+        _measurementRunning = false;
+
+        emit endMeasurement();
+    }
+    return true;
+}
+
+bool Backend::isMeasurementRunning() const
+{
+    return _measurementRunning;
+}
+
+void Backend::loadDefaultSetup(MeasurementSetup &setup)
+{
+    setup.clear();
+    int i = 1;
+
+    foreach (CanDriver *driver, _drivers) {
+        driver->update();
+        foreach (CanInterfaceId intf, driver->getInterfaceIds()) {
+            MeasurementNetwork *network = setup.createNetwork();
+//            network->setName(QString().sprintf("Network %d", i++));
+            network->setName(QString().sprintf("网络 %d", i++));
+            MeasurementInterface *mi = new MeasurementInterface();
+            mi->setCanInterface(intf);
+            mi->setBitrate(500000);
+            network->addInterface(mi);
+        }
+    }
+}
+
+void Backend::setDefaultSetup()
+{
+    loadDefaultSetup(_setup);
+}
+
+MeasurementSetup &Backend::getSetup()
+{
+    return _setup;
+}
+
+void Backend::setSetup(MeasurementSetup &new_setup)
+{
+    _setup.cloneFrom(new_setup);
+}
+
+double Backend::currentTimeStamp() const
+{
+    return ((double)QDateTime::currentMSecsSinceEpoch()) / 1000;
+}
+
+CanTrace *Backend::getTrace()
+{
+    return _trace;
+}
+
+void Backend::clearTrace()
+{
+    _trace->clear();
+}
+
+CanDbMessage *Backend::findDbMessage(const CanMessage &msg) const
+{
+    return _setup.findDbMessage(msg);
+}
+
+CanInterfaceIdList Backend::getInterfaceList()
+{
+    CanInterfaceIdList result;
+    foreach (CanDriver *driver, _drivers) {
+        foreach (CanInterfaceId id, driver->getInterfaceIds()) {
+            result.append(id);
+        }
+    }
+    return result;
+}
+
+CanDriver *Backend::getDriverById(CanInterfaceId id)
+{
+    CanDriver *driver = _drivers.value((id>>8) & 0xFF);
+    if (!driver) {
+        log_critical(QString("Unable to get driver for interface id: %1. This should never happen.").arg(QString().number(id)));
+    }
+    return driver;
+}
+
+CanInterface *Backend::getInterfaceById(CanInterfaceId id)
+{
+    CanDriver *driver = getDriverById(id);
+    return driver ? driver->getInterfaceById(id) : 0;
+}
+
+QString Backend::getInterfaceName(CanInterfaceId id)
+{
+    CanInterface *intf = getInterfaceById(id);
+    if (intf) {
+        return intf->getName();
+    } else {
+        log_critical(QString("Trying to get name from unknown interface id: %1. This should never happen.").arg(QString().number(id)));
+        return "";
+    }
+}
+
+QString Backend::getDriverName(CanInterfaceId id)
+{
+    CanDriver *driver = getDriverById(id);
+    return driver ? driver->getName() : "";
+}
+
+CanDriver *Backend::getDriverByName(QString driverName)
+{
+    foreach (CanDriver *driver, _drivers) {
+        if (driver->getName()==driverName) {
+            return driver;
+        }
+    }
+    return 0;
+}
+
+CanInterface *Backend::getInterfaceByDriverAndName(QString driverName, QString deviceName)
+{
+    CanDriver *driver = getDriverByName(driverName);
+    if (driver) {
+        return driver->getInterfaceByName(deviceName);
+    } else {
+        return 0;
+    }
+
+}
+
+pCanDb Backend::loadDbc(QString filename)
+{
+    DbcParser parser;
+
+    QFile *dbc = new QFile(filename);
+    pCanDb candb(new CanDb());
+    parser.parseFile(dbc, *candb);
+    delete dbc;
+
+    return candb;
+}
+
+void Backend::clearLog()
+{
+    _logModel->clear();
+}
+
+LogModel &Backend::getLogModel() const
+{
+    return *_logModel;
+}
+
+double Backend::getTimestampAtMeasurementStart() const
+{
+    return (double)_measurementStartTime / 1000.0;
+}
+
+uint64_t Backend::getUsecsAtMeasurementStart() const
+{
+    return _measurementStartTime * 1000;
+}
+
+uint64_t Backend::getNsecsSinceMeasurementStart() const
+{
+    return _timerSinceStart.nsecsElapsed();
+}
+
+uint64_t Backend::getUsecsSinceMeasurementStart() const
+{
+    return getNsecsSinceMeasurementStart() / 1000;
+}
+
+void Backend::logMessage(const QDateTime dt, const log_level_t level, const QString msg)
+{
+    emit onLogMessage(dt, level, msg);
+}

+ 114 - 0
src/tool/adc_cantool/core/Backend.h

@@ -0,0 +1,114 @@
+/*
+
+  Copyright (c) 2015, 2016 Hubert Denkmair <hubert@denkmair.de>
+
+  This file is part of cangaroo.
+
+  cangaroo is free software: you can redistribute it and/or modify
+  it under the terms of the GNU General Public License as published by
+  the Free Software Foundation, either version 2 of the License, or
+  (at your option) any later version.
+
+  cangaroo 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 General Public License for more details.
+
+  You should have received a copy of the GNU General Public License
+  along with cangaroo.  If not, see <http://www.gnu.org/licenses/>.
+
+*/
+
+#pragma once
+
+#include <stdint.h>
+#include <QObject>
+#include <QList>
+#include <QMutex>
+#include <QDateTime>
+#include <QElapsedTimer>
+#include <driver/CanDriver.h>
+#include <core/CanDb.h>
+#include <core/MeasurementSetup.h>
+#include <core/Log.h>
+
+class MeasurementNetwork;
+class CanTrace;
+class CanListener;
+class CanDbMessage;
+class SetupDialog;
+class LogModel;
+
+class Backend : public QObject
+{
+    Q_OBJECT
+public:
+    static Backend &instance();
+
+    explicit Backend();
+    virtual ~Backend();
+
+    void addCanDriver(CanDriver &driver);
+
+    bool startMeasurement();
+    bool stopMeasurement();
+    bool isMeasurementRunning() const;
+    double getTimestampAtMeasurementStart() const;
+    uint64_t getUsecsAtMeasurementStart() const;
+    uint64_t getNsecsSinceMeasurementStart() const;
+    uint64_t getUsecsSinceMeasurementStart() const;
+
+
+    void logMessage(const QDateTime dt, const log_level_t level, const QString msg);
+
+    MeasurementSetup &getSetup();
+    void loadDefaultSetup(MeasurementSetup &setup);
+    void setDefaultSetup();
+    void setSetup(MeasurementSetup &new_setup);
+
+    double currentTimeStamp() const;
+
+    CanTrace *getTrace();
+    void clearTrace();
+
+    CanDbMessage *findDbMessage(const CanMessage &msg) const;
+
+    CanInterfaceIdList getInterfaceList();
+    CanDriver *getDriverById(CanInterfaceId id);
+    CanInterface *getInterfaceById(CanInterfaceId id);
+    QString getInterfaceName(CanInterfaceId id);
+    QString getDriverName(CanInterfaceId id);
+
+    CanDriver *getDriverByName(QString driverName);
+    CanInterface *getInterfaceByDriverAndName(QString driverName, QString deviceName);
+
+    pCanDb loadDbc(QString filename);
+
+    void clearLog();
+    LogModel &getLogModel() const;
+
+signals:
+    void beginMeasurement();
+    void endMeasurement();
+
+    void onSetupChanged();
+
+    void onLogMessage(const QDateTime dt, const log_level_t level, const QString msg);
+
+    void onSetupDialogCreated(SetupDialog &dlg);
+
+public slots:
+
+private:
+    static Backend *_instance;
+
+    bool _measurementRunning;
+    uint64_t _measurementStartTime;
+    QElapsedTimer _timerSinceStart;
+    QList<CanDriver*> _drivers;
+    MeasurementSetup _setup;
+    CanTrace *_trace;
+    QList<CanListener*> _listeners;
+
+    LogModel *_logModel;
+};

+ 89 - 0
src/tool/adc_cantool/core/CanDb.cpp

@@ -0,0 +1,89 @@
+/*
+
+  Copyright (c) 2015, 2016 Hubert Denkmair <hubert@denkmair.de>
+
+  This file is part of cangaroo.
+
+  cangaroo is free software: you can redistribute it and/or modify
+  it under the terms of the GNU General Public License as published by
+  the Free Software Foundation, either version 2 of the License, or
+  (at your option) any later version.
+
+  cangaroo 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 General Public License for more details.
+
+  You should have received a copy of the GNU General Public License
+  along with cangaroo.  If not, see <http://www.gnu.org/licenses/>.
+
+*/
+
+#include "CanDb.h"
+#include <QFileInfo>
+#include <QDomDocument>
+
+#include <core/Backend.h>
+
+CanDb::CanDb()
+{
+
+}
+
+QString CanDb::getFileName()
+{
+    QFileInfo fi(getPath());
+    return fi.fileName();
+}
+
+QString CanDb::getDirectory()
+{
+    QFileInfo fi(getPath());
+    return fi.absolutePath();
+}
+
+CanDbNode *CanDb::getOrCreateNode(QString node_name)
+{
+    if (!_nodes.contains(node_name)) {
+        CanDbNode *node = new CanDbNode(this);
+        node->setName(node_name);
+        _nodes[node_name] = node;
+        return node;
+    } else {
+        return _nodes[node_name];
+    }
+}
+
+CanDbMessage *CanDb::getMessageById(uint32_t raw_id)
+{
+    if (_messages.contains(raw_id)) {
+        return _messages[raw_id];
+    } else {
+        return 0;
+    }
+}
+
+void CanDb::addMessage(CanDbMessage *msg)
+{
+    _messages[msg->getRaw_id()] = msg;
+}
+
+QString CanDb::getComment() const
+{
+    return _comment;
+}
+
+void CanDb::setComment(const QString &comment)
+{
+    _comment = comment;
+}
+
+bool CanDb::saveXML(Backend &backend, QDomDocument &xml, QDomElement &root)
+{
+    (void) backend;
+    (void) xml;
+    root.setAttribute("type", "dbc");
+    root.setAttribute("filename", _path);
+    return true;
+}
+

+ 73 - 0
src/tool/adc_cantool/core/CanDb.h

@@ -0,0 +1,73 @@
+/*
+
+  Copyright (c) 2015, 2016 Hubert Denkmair <hubert@denkmair.de>
+
+  This file is part of cangaroo.
+
+  cangaroo is free software: you can redistribute it and/or modify
+  it under the terms of the GNU General Public License as published by
+  the Free Software Foundation, either version 2 of the License, or
+  (at your option) any later version.
+
+  cangaroo 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 General Public License for more details.
+
+  You should have received a copy of the GNU General Public License
+  along with cangaroo.  If not, see <http://www.gnu.org/licenses/>.
+
+*/
+
+#pragma once
+
+#include <QString>
+#include <QList>
+#include <QMap>
+#include <QSharedPointer>
+
+#include "CanDbNode.h"
+#include "CanDbMessage.h"
+
+class QDomDocument;
+class QDomElement;
+
+class Backend;
+class CanDb;
+class CanDbMessage;
+
+typedef QMap<QString,CanDbNode*> CanDbNodeMap;
+typedef QMap<uint32_t, CanDbMessage*> CanDbMessageList;
+typedef QSharedPointer<CanDb> pCanDb;
+
+class CanDb
+{
+    public:
+        CanDb();
+
+        void setPath(QString path) { _path = path; }
+        QString getPath() { return _path; }
+        QString getFileName();
+        QString getDirectory();
+
+        void setVersion(QString version) { _version = version; }
+        QString getVersion() { return _version; }
+
+        CanDbNode *getOrCreateNode(QString node_name);
+
+        CanDbMessage *getMessageById(uint32_t raw_id);
+        void addMessage(CanDbMessage *msg);
+
+        QString getComment() const;
+        void setComment(const QString &comment);
+
+        bool saveXML(Backend &backend, QDomDocument &xml, QDomElement &root);
+
+private:
+        QString _path;
+        QString _version;
+        QString _comment;
+        CanDbNodeMap _nodes;
+        CanDbMessageList _messages;
+
+};

+ 112 - 0
src/tool/adc_cantool/core/CanDbMessage.cpp

@@ -0,0 +1,112 @@
+/*
+
+  Copyright (c) 2015, 2016 Hubert Denkmair <hubert@denkmair.de>
+
+  This file is part of cangaroo.
+
+  cangaroo is free software: you can redistribute it and/or modify
+  it under the terms of the GNU General Public License as published by
+  the Free Software Foundation, either version 2 of the License, or
+  (at your option) any later version.
+
+  cangaroo 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 General Public License for more details.
+
+  You should have received a copy of the GNU General Public License
+  along with cangaroo.  If not, see <http://www.gnu.org/licenses/>.
+
+*/
+
+#include "CanDbMessage.h"
+
+CanDbMessage::CanDbMessage(CanDb *parent)
+  : _parent(parent), _raw_id(0), _dlc(0), _sender(0), _muxer(0)
+{
+}
+
+QString CanDbMessage::getName() const
+{
+    return _name;
+}
+
+void CanDbMessage::setName(const QString &name)
+{
+    _name = name;
+}
+
+uint32_t CanDbMessage::getRaw_id() const
+{
+    return _raw_id;
+}
+
+void CanDbMessage::setRaw_id(const uint32_t &raw_id)
+{
+    _raw_id = raw_id;
+}
+
+uint8_t CanDbMessage::getDlc() const
+{
+    return _dlc;
+}
+
+void CanDbMessage::setDlc(const uint8_t &dlc)
+{
+    _dlc = dlc;
+}
+
+CanDbNode *CanDbMessage::getSender() const
+{
+    return _sender;
+}
+
+void CanDbMessage::setSender(CanDbNode *sender)
+{
+    _sender = sender;
+}
+
+void CanDbMessage::addSignal(CanDbSignal *signal)
+{
+    _signals.append(signal);
+}
+
+CanDbSignal *CanDbMessage::getSignal(int num)
+{
+    if (_signals.size()>num) {
+        return _signals[num];
+    } else {
+        return 0;
+    }
+}
+
+CanDbSignal *CanDbMessage::getSignalByName(QString signal_name)
+{
+    CanDbSignal *signal;
+    foreach (signal, _signals) {
+        if (signal->name() == signal_name) {
+            return signal;
+        }
+    }
+    return 0;
+}
+
+QString CanDbMessage::getComment() const
+{
+    return _comment;
+}
+
+void CanDbMessage::setComment(const QString &comment)
+{
+    _comment = comment;
+}
+
+CanDbSignal *CanDbMessage::getMuxer() const
+{
+    return _muxer;
+}
+
+void CanDbMessage::setMuxer(CanDbSignal *muxer)
+{
+    _muxer = muxer;
+}

+ 73 - 0
src/tool/adc_cantool/core/CanDbMessage.h

@@ -0,0 +1,73 @@
+/*
+
+  Copyright (c) 2015, 2016 Hubert Denkmair <hubert@denkmair.de>
+
+  This file is part of cangaroo.
+
+  cangaroo is free software: you can redistribute it and/or modify
+  it under the terms of the GNU General Public License as published by
+  the Free Software Foundation, either version 2 of the License, or
+  (at your option) any later version.
+
+  cangaroo 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 General Public License for more details.
+
+  You should have received a copy of the GNU General Public License
+  along with cangaroo.  If not, see <http://www.gnu.org/licenses/>.
+
+*/
+
+#pragma once
+
+#include <stdint.h>
+#include <QString>
+#include "CanDb.h"
+#include "CanDbSignal.h"
+
+class CanDbNode;
+class CanDbSignal;
+
+typedef QList<CanDbSignal*> CanDbSignalList;
+
+class CanDbMessage
+{
+    public:
+        CanDbMessage(CanDb *parent);
+
+        QString getName() const;
+        void setName(const QString &name);
+
+        uint32_t getRaw_id() const;
+        void setRaw_id(const uint32_t &raw_id);
+
+        uint8_t getDlc() const;
+        void setDlc(const uint8_t &dlc);
+
+        CanDbNode *getSender() const;
+        void setSender(CanDbNode *sender);
+
+        void addSignal(CanDbSignal *signal);
+        CanDbSignal *getSignal(int num);
+        CanDbSignal *getSignalByName(QString signal_name);
+
+        CanDbSignalList getSignals() { return _signals; }
+
+        QString getComment() const;
+        void setComment(const QString &comment);
+
+        CanDbSignal *getMuxer() const;
+        void setMuxer(CanDbSignal *muxer);
+
+private:
+        CanDb *_parent;
+        QString _name;
+        uint32_t _raw_id;
+        uint8_t _dlc;
+        CanDbNode *_sender;
+        CanDbSignalList _signals;
+        QString _comment;
+        CanDbSignal *_muxer;
+
+};

+ 49 - 0
src/tool/adc_cantool/core/CanDbNode.cpp

@@ -0,0 +1,49 @@
+/*
+
+  Copyright (c) 2015, 2016 Hubert Denkmair <hubert@denkmair.de>
+
+  This file is part of cangaroo.
+
+  cangaroo is free software: you can redistribute it and/or modify
+  it under the terms of the GNU General Public License as published by
+  the Free Software Foundation, either version 2 of the License, or
+  (at your option) any later version.
+
+  cangaroo 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 General Public License for more details.
+
+  You should have received a copy of the GNU General Public License
+  along with cangaroo.  If not, see <http://www.gnu.org/licenses/>.
+
+*/
+
+#include "CanDbNode.h"
+
+CanDbNode::CanDbNode(CanDb *parent)
+  : _parent(parent)
+{
+
+}
+
+QString CanDbNode::name() const
+{
+    return _name;
+}
+
+void CanDbNode::setName(const QString &name)
+{
+    _name = name;
+}
+
+QString CanDbNode::comment() const
+{
+    return _comment;
+}
+
+void CanDbNode::setComment(const QString &comment)
+{
+    _comment = comment;
+}
+

+ 43 - 0
src/tool/adc_cantool/core/CanDbNode.h

@@ -0,0 +1,43 @@
+/*
+
+  Copyright (c) 2015, 2016 Hubert Denkmair <hubert@denkmair.de>
+
+  This file is part of cangaroo.
+
+  cangaroo is free software: you can redistribute it and/or modify
+  it under the terms of the GNU General Public License as published by
+  the Free Software Foundation, either version 2 of the License, or
+  (at your option) any later version.
+
+  cangaroo 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 General Public License for more details.
+
+  You should have received a copy of the GNU General Public License
+  along with cangaroo.  If not, see <http://www.gnu.org/licenses/>.
+
+*/
+
+#pragma once
+
+#include <QString>
+
+class CanDb;
+
+class CanDbNode
+{
+public:
+    CanDbNode(CanDb *parent);
+
+    QString name() const;
+    void setName(const QString &name);
+
+    QString comment() const;
+    void setComment(const QString &comment);
+
+private:
+    CanDb *_parent;
+    QString _name;
+    QString _comment;
+};

+ 229 - 0
src/tool/adc_cantool/core/CanDbSignal.cpp

@@ -0,0 +1,229 @@
+/*
+
+  Copyright (c) 2015, 2016 Hubert Denkmair <hubert@denkmair.de>
+
+  This file is part of cangaroo.
+
+  cangaroo is free software: you can redistribute it and/or modify
+  it under the terms of the GNU General Public License as published by
+  the Free Software Foundation, either version 2 of the License, or
+  (at your option) any later version.
+
+  cangaroo 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 General Public License for more details.
+
+  You should have received a copy of the GNU General Public License
+  along with cangaroo.  If not, see <http://www.gnu.org/licenses/>.
+
+*/
+
+#include "CanDbSignal.h"
+
+CanDbSignal::CanDbSignal(CanDbMessage *parent)
+  : _parent(parent),
+    _isUnsigned(false),
+    _isBigEndian(false),
+    _factor(1),
+    _offset(0),
+    _min(0),
+    _max(0),
+    _isMuxer(false),
+    _isMuxed(false),
+    _muxValue(0)
+{
+}
+
+QString CanDbSignal::name() const
+{
+    return _name;
+}
+
+void CanDbSignal::setName(const QString &name)
+{
+    _name = name;
+}
+
+uint8_t CanDbSignal::startBit() const
+{
+    return _startBit;
+}
+
+void CanDbSignal::setStartBit(uint8_t startBit)
+{
+    _startBit = startBit;
+}
+
+uint8_t CanDbSignal::length() const
+{
+    return _length;
+}
+
+void CanDbSignal::setLength(uint8_t length)
+{
+    _length = length;
+}
+
+QString CanDbSignal::comment() const
+{
+    return _comment;
+}
+
+void CanDbSignal::setComment(const QString &comment)
+{
+    _comment = comment;
+}
+
+QString CanDbSignal::getValueName(const uint64_t value) const
+{
+    if (_valueTable.contains(value)) {
+        return _valueTable[value];
+    } else {
+        return QString();
+    }
+}
+
+void CanDbSignal::setValueName(const uint64_t value, const QString &name)
+{
+    _valueTable[value] = name;
+}
+
+double CanDbSignal::convertRawValueToPhysical(const uint64_t rawValue)
+{
+    if (isUnsigned()) {
+        uint64_t v = rawValue;
+        return v * _factor + _offset;
+    } else {
+        // TODO check with DBC that actually contains signed values?!
+        int64_t v = (int64_t)(rawValue<<(64-_length));
+        v>>=(64-_length);
+        return v * _factor + _offset;
+
+    }
+}
+
+double CanDbSignal::extractPhysicalFromMessage(const CanMessage &msg)
+{
+    return convertRawValueToPhysical(extractRawDataFromMessage(msg));
+}
+
+double CanDbSignal::getFactor() const
+{
+    return _factor;
+}
+
+void CanDbSignal::setFactor(double factor)
+{
+    _factor = factor;
+}
+
+double CanDbSignal::getOffset() const
+{
+    return _offset;
+}
+
+void CanDbSignal::setOffset(double offset)
+{
+    _offset = offset;
+}
+
+double CanDbSignal::getMinimumValue() const
+{
+    return _min;
+}
+
+void CanDbSignal::setMinimumValue(double min)
+{
+    _min = min;
+}
+
+double CanDbSignal::getMaximumValue() const
+{
+    return _max;
+}
+
+void CanDbSignal::setMaximumValue(double max)
+{
+    _max = max;
+}
+
+QString CanDbSignal::getUnit() const
+{
+    return _unit;
+}
+
+void CanDbSignal::setUnit(const QString &unit)
+{
+    _unit = unit;
+}
+
+bool CanDbSignal::isUnsigned() const
+{
+    return _isUnsigned;
+}
+
+void CanDbSignal::setUnsigned(bool isUnsigned)
+{
+    _isUnsigned = isUnsigned;
+}
+bool CanDbSignal::isBigEndian() const
+{
+    return _isBigEndian;
+}
+
+void CanDbSignal::setIsBigEndian(bool isBigEndian)
+{
+    _isBigEndian = isBigEndian;
+}
+
+bool CanDbSignal::isMuxer() const
+{
+    return _isMuxer;
+}
+
+void CanDbSignal::setIsMuxer(bool isMuxer)
+{
+    _isMuxer = isMuxer;
+}
+
+bool CanDbSignal::isMuxed() const
+{
+    return _isMuxed;
+}
+
+void CanDbSignal::setIsMuxed(bool isMuxed)
+{
+    _isMuxed = isMuxed;
+}
+
+uint32_t CanDbSignal::getMuxValue() const
+{
+    return _muxValue;
+}
+
+void CanDbSignal::setMuxValue(const uint32_t &muxValue)
+{
+    _muxValue = muxValue;
+}
+
+bool CanDbSignal::isPresentInMessage(const CanMessage &msg)
+{
+    if ((_startBit + _length)>(8*msg.getLength())) {
+        return false;
+    }
+
+    if (!_isMuxed) { return true; }
+
+    CanDbSignal *muxer = _parent->getMuxer();
+    if (!muxer) { return false; }
+
+    return _muxValue == muxer->extractRawDataFromMessage(msg);
+}
+
+uint64_t CanDbSignal::extractRawDataFromMessage(const CanMessage &msg)
+{
+    return msg.extractRawSignal(startBit(), length(), isBigEndian());
+}
+
+

+ 106 - 0
src/tool/adc_cantool/core/CanDbSignal.h

@@ -0,0 +1,106 @@
+/*
+
+  Copyright (c) 2015, 2016 Hubert Denkmair <hubert@denkmair.de>
+
+  This file is part of cangaroo.
+
+  cangaroo is free software: you can redistribute it and/or modify
+  it under the terms of the GNU General Public License as published by
+  the Free Software Foundation, either version 2 of the License, or
+  (at your option) any later version.
+
+  cangaroo 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 General Public License for more details.
+
+  You should have received a copy of the GNU General Public License
+  along with cangaroo.  If not, see <http://www.gnu.org/licenses/>.
+
+*/
+
+#pragma once
+
+#include "CanMessage.h"
+#include "CanDbMessage.h"
+#include <QString>
+#include <QMap>
+
+class CanDbMessage;
+
+typedef QMap<uint64_t,QString> CanDbValueTable;
+
+class CanDbSignal
+{
+public:
+    CanDbSignal(CanDbMessage *parent);
+    QString name() const;
+    void setName(const QString &name);
+
+    uint8_t startBit() const;
+    void setStartBit(uint8_t startBit);
+
+    uint8_t length() const;
+    void setLength(uint8_t length);
+
+    QString comment() const;
+    void setComment(const QString &comment);
+
+    QString getValueName(const uint64_t value) const;
+    void setValueName(const uint64_t value, const QString &name);
+
+    double getFactor() const;
+    void setFactor(double factor);
+
+    double getOffset() const;
+    void setOffset(double offset);
+
+    double getMinimumValue() const;
+    void setMinimumValue(double min);
+
+    double getMaximumValue() const;
+    void setMaximumValue(double max);
+
+    QString getUnit() const;
+    void setUnit(const QString &unit);
+
+    bool isUnsigned() const;
+    void setUnsigned(bool isUnsigned);
+
+    bool isBigEndian() const;
+    void setIsBigEndian(bool isBigEndian);
+
+    bool isMuxer() const;
+    void setIsMuxer(bool isMuxer);
+
+    bool isMuxed() const;
+    void setIsMuxed(bool isMuxed);
+
+    uint32_t getMuxValue() const;
+    void setMuxValue(const uint32_t &muxValue);
+
+    bool isPresentInMessage(const CanMessage &msg);
+    uint64_t extractRawDataFromMessage(const CanMessage &msg);
+
+    double convertRawValueToPhysical(const uint64_t rawValue);
+    double extractPhysicalFromMessage(const CanMessage &msg);
+
+
+private:
+    CanDbMessage *_parent;
+    QString _name;
+    uint8_t _startBit;
+    uint8_t _length;
+    bool _isUnsigned;
+    bool _isBigEndian;
+    double _factor;
+    double _offset;
+    double _min;
+    double _max;
+    QString _unit;
+    bool _isMuxer;
+    bool _isMuxed;
+    uint32_t _muxValue;
+    QString _comment;
+    CanDbValueTable _valueTable;
+};

+ 349 - 0
src/tool/adc_cantool/core/CanMessage.cpp

@@ -0,0 +1,349 @@
+/*
+
+  Copyright (c) 2015, 2016 Hubert Denkmair <hubert@denkmair.de>
+
+  This file is part of cangaroo.
+
+  cangaroo is free software: you can redistribute it and/or modify
+  it under the terms of the GNU General Public License as published by
+  the Free Software Foundation, either version 2 of the License, or
+  (at your option) any later version.
+
+  cangaroo 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 General Public License for more details.
+
+  You should have received a copy of the GNU General Public License
+  along with cangaroo.  If not, see <http://www.gnu.org/licenses/>.
+
+*/
+
+
+
+#include "CanMessage.h"
+#include <core/portable_endian.h>
+
+enum {
+	id_flag_extended = 0x80000000,
+	id_flag_rtr      = 0x40000000,
+	id_flag_error    = 0x20000000,
+	id_mask_extended = 0x1FFFFFFF,
+	id_mask_standard = 0x7FF
+};
+
+CanMessage::CanMessage()
+  : _raw_id(0), _dlc(0), _isFD(false), _interface(0), _u8()
+{
+    _timestamp.tv_sec = 0;
+    _timestamp.tv_usec = 0;
+
+}
+
+CanMessage::CanMessage(uint32_t can_id)
+  : _dlc(0), _interface(0), _u8()
+{
+    _timestamp.tv_sec = 0;
+    _timestamp.tv_usec = 0;
+    setId(can_id);
+}
+
+CanMessage::CanMessage(const CanMessage &msg)
+{
+    cloneFrom(msg);
+}
+
+void CanMessage::cloneFrom(const CanMessage &msg)
+{
+    _raw_id = msg._raw_id;
+    _dlc = msg._dlc;
+
+    // Copy data
+    for(int i=0; i<64; i++)
+    {
+        _u8[i] = msg._u8[i];
+    }
+
+    _interface = msg._interface;
+    _timestamp = msg._timestamp;
+    //从文件发送数据pushback 某些变量值不对的问题
+    _isFD=msg._isFD;
+    _isBRS=msg._isBRS;
+}
+
+
+uint32_t CanMessage::getRawId() const {
+	return _raw_id;
+}
+
+void CanMessage::setRawId(const uint32_t raw_id) {
+	_raw_id = raw_id;
+}
+
+uint32_t CanMessage::getId() const {
+	if (isExtended()) {
+		return _raw_id & id_mask_extended;
+	} else {
+		return _raw_id & id_mask_standard;
+	}
+}
+
+void CanMessage::setId(const uint32_t id) {
+    _raw_id &= ~ id_mask_extended;
+    _raw_id = id;
+    if (id>0x7FF) {
+		setExtended(true);
+	}
+}
+
+bool CanMessage::isExtended() const {
+	return (_raw_id & id_flag_extended) != 0;
+}
+
+void CanMessage::setExtended(const bool isExtended) {
+	if (isExtended) {
+		_raw_id |= id_flag_extended;
+	} else {
+		_raw_id &= ~id_flag_extended;
+	}
+}
+
+bool CanMessage::isRTR() const {
+    return (_raw_id & id_flag_rtr) != 0;
+}
+
+void CanMessage::setRTR(const bool isRTR) {
+    if (isRTR) {
+        _raw_id |= id_flag_rtr;
+    } else {
+        _raw_id &= ~id_flag_rtr;
+    }
+}
+
+bool CanMessage::isFD() const {
+    return _isFD;
+}
+
+void CanMessage::setFD(const bool isFD) {
+    _isFD = isFD;
+}
+
+bool CanMessage::isBRS() const {
+    return _isBRS;
+}
+
+void CanMessage::setBRS(const bool isBRS) {
+    _isBRS = isBRS;
+}
+
+bool CanMessage::isErrorFrame() const {
+	return (_raw_id & id_flag_error) != 0;
+}
+
+void CanMessage::setErrorFrame(const bool isErrorFrame) {
+	if (isErrorFrame) {
+		_raw_id |= id_flag_error;
+	} else {
+		_raw_id &= ~id_flag_error;
+    }
+}
+
+CanInterfaceId CanMessage::getInterfaceId() const
+{
+    return _interface;
+}
+
+void CanMessage::setInterfaceId(CanInterfaceId interface)
+{
+    _interface = interface;
+}
+
+uint8_t CanMessage::getLength() const {
+	return _dlc;
+}
+
+void CanMessage::setLength(const uint8_t dlc) {
+    // Limit to CANFD max length
+    if (dlc<=64) {
+		_dlc = dlc;
+	} else {
+		_dlc = 8;
+	}
+}
+
+uint8_t CanMessage::getByte(const uint8_t index) const {
+	if (index<sizeof(_u8)) {
+		return _u8[index];
+	} else {
+		return 0;
+	}
+}
+
+void CanMessage::setByte(const uint8_t index, const uint8_t value) {
+	if (index<sizeof(_u8)) {
+		_u8[index] = value;
+    }
+}
+
+uint64_t CanMessage::extractRawSignal(uint8_t start_bit, const uint8_t length, const bool isBigEndian) const
+{
+//    if ((start_bit+length) > (getLength()*8)) {
+//        return 0;
+//    }
+
+    // FIXME: This only gives access to data bytes 0-8. Need to rework for CANFD.
+    uint64_t data = le64toh(_u64[0]);
+
+    data >>= start_bit;
+
+    uint64_t mask =  0xFFFFFFFFFFFFFFFF;
+    mask <<= length;
+    mask = ~mask;
+
+    data &= mask;
+
+    // If the length is greater than 8, we need to byteswap to preserve endianness
+    if (isBigEndian && (length > 8))
+    {
+
+        // Swap bytes
+        data = __builtin_bswap64(data);
+
+        // Shift out unused bits
+        data >>= 64 - length;
+    }
+
+    return data;
+}
+
+void CanMessage::setDataAt(uint8_t position, uint8_t data)
+{
+    if(position < 64)
+        _u8[position] = data;
+    else
+        return;
+}
+
+void CanMessage::setData(const uint8_t d0) {
+	_dlc = 1;
+	_u8[0] = d0;
+}
+
+void CanMessage::setData(const uint8_t d0, const uint8_t d1) {
+	_dlc = 2;
+	_u8[0] = d0;
+	_u8[1] = d1;
+}
+
+void CanMessage::setData(const uint8_t d0, const uint8_t d1, const uint8_t d2) {
+	_dlc = 3;
+	_u8[0] = d0;
+	_u8[1] = d1;
+	_u8[2] = d2;
+}
+
+void CanMessage::setData(const uint8_t d0, const uint8_t d1, const uint8_t d2,
+		const uint8_t d3) {
+	_dlc = 4;
+	_u8[0] = d0;
+	_u8[1] = d1;
+	_u8[2] = d2;
+	_u8[3] = d3;
+}
+
+void CanMessage::setData(const uint8_t d0, const uint8_t d1, const uint8_t d2,
+		const uint8_t d3, const uint8_t d4) {
+	_dlc = 5;
+	_u8[0] = d0;
+	_u8[1] = d1;
+	_u8[2] = d2;
+	_u8[3] = d3;
+	_u8[4] = d4;
+}
+
+void CanMessage::setData(const uint8_t d0, const uint8_t d1, const uint8_t d2,
+		const uint8_t d3, const uint8_t d4, const uint8_t d5) {
+	_dlc = 6;
+	_u8[0] = d0;
+	_u8[1] = d1;
+	_u8[2] = d2;
+	_u8[3] = d3;
+	_u8[4] = d4;
+	_u8[5] = d5;
+}
+
+void CanMessage::setData(const uint8_t d0, const uint8_t d1, const uint8_t d2,
+		const uint8_t d3, const uint8_t d4, const uint8_t d5,
+		const uint8_t d6) {
+	_dlc = 7;
+	_u8[0] = d0;
+	_u8[1] = d1;
+	_u8[2] = d2;
+	_u8[3] = d3;
+	_u8[4] = d4;
+	_u8[5] = d5;
+	_u8[6] = d6;
+}
+
+void CanMessage::setData(const uint8_t d0, const uint8_t d1, const uint8_t d2,
+		const uint8_t d3, const uint8_t d4, const uint8_t d5, const uint8_t d6,
+		const uint8_t d7) {
+	_dlc = 8;
+	_u8[0] = d0;
+	_u8[1] = d1;
+	_u8[2] = d2;
+	_u8[3] = d3;
+	_u8[4] = d4;
+	_u8[5] = d5;
+	_u8[6] = d6;
+    _u8[7] = d7;
+}
+
+timeval CanMessage::getTimestamp() const
+{
+    return _timestamp;
+}
+
+void CanMessage::setTimestamp(const timeval timestamp)
+{
+    _timestamp = timestamp;
+}
+
+void CanMessage::setTimestamp(const uint64_t seconds, const uint32_t micro_seconds)
+{
+    _timestamp.tv_sec = seconds;
+    _timestamp.tv_usec = micro_seconds;
+}
+
+double CanMessage::getFloatTimestamp() const
+{
+    return (double)_timestamp.tv_sec + ((double)_timestamp.tv_usec/1000000);
+}
+
+QDateTime CanMessage::getDateTime() const
+{
+    return QDateTime::fromMSecsSinceEpoch((qint64)(1000*getFloatTimestamp()));
+}
+
+QString CanMessage::getIdString() const
+{
+    if (isExtended()) {
+        return QString().sprintf("0x%08X", getId());
+    } else {
+        return QString().sprintf("0x%03X", getId());
+    }
+}
+
+QString CanMessage::getDataHexString() const
+{
+    if(getLength() == 0)
+        return "";
+
+    QString outstr = "";
+    for(int i=0; i<getLength(); i++)
+    {
+        outstr += QString().sprintf("%02X ", getByte(i));
+    }
+
+    return outstr;
+}

+ 104 - 0
src/tool/adc_cantool/core/CanMessage.h

@@ -0,0 +1,104 @@
+/*
+
+  Copyright (c) 2015, 2016 Hubert Denkmair <hubert@denkmair.de>
+
+  This file is part of cangaroo.
+
+  cangaroo is free software: you can redistribute it and/or modify
+  it under the terms of the GNU General Public License as published by
+  the Free Software Foundation, either version 2 of the License, or
+  (at your option) any later version.
+
+  cangaroo 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 General Public License for more details.
+
+  You should have received a copy of the GNU General Public License
+  along with cangaroo.  If not, see <http://www.gnu.org/licenses/>.
+
+*/
+
+#pragma once
+
+#include <stdint.h>
+#include <sys/time.h>
+
+#include <QString>
+#include <QDateTime>
+#include <driver/CanDriver.h>
+
+class CanMessage {
+public:
+	CanMessage();
+	CanMessage(uint32_t can_id);
+    CanMessage(const CanMessage &msg);
+    void cloneFrom(const CanMessage &msg);
+
+	uint32_t getRawId() const;
+	void setRawId(const uint32_t raw_id);
+
+	uint32_t getId() const;
+	void setId(const uint32_t id);
+
+	bool isExtended() const;
+	void setExtended(const bool isExtended);
+
+	bool isRTR() const;
+	void setRTR(const bool isRTR);
+
+    bool isFD() const;
+    void setFD(const bool isFD);
+
+    bool isBRS() const;
+    void setBRS(const bool isFD);
+
+    bool isErrorFrame() const;
+	void setErrorFrame(const bool isErrorFrame);
+
+    CanInterfaceId getInterfaceId() const;
+    void setInterfaceId(CanInterfaceId interface);
+
+	uint8_t getLength() const;
+	void setLength(const uint8_t dlc);
+
+	uint8_t getByte(const uint8_t index) const;
+	void setByte(const uint8_t index, const uint8_t value);
+
+    uint64_t extractRawSignal(uint8_t start_bit, const uint8_t length, const bool isBigEndian) const;
+
+    void setDataAt(uint8_t position, uint8_t data);
+	void setData(const uint8_t d0);
+	void setData(const uint8_t d0, const uint8_t d1);
+	void setData(const uint8_t d0, const uint8_t d1, const uint8_t d2);
+	void setData(const uint8_t d0, const uint8_t d1, const uint8_t d2, const uint8_t d3);
+	void setData(const uint8_t d0, const uint8_t d1, const uint8_t d2, const uint8_t d3, const uint8_t d4);
+	void setData(const uint8_t d0, const uint8_t d1, const uint8_t d2, const uint8_t d3, const uint8_t d4, const uint8_t d5);
+	void setData(const uint8_t d0, const uint8_t d1, const uint8_t d2, const uint8_t d3, const uint8_t d4, const uint8_t d5, const uint8_t d6);
+	void setData(const uint8_t d0, const uint8_t d1, const uint8_t d2, const uint8_t d3, const uint8_t d4, const uint8_t d5, const uint8_t d6, const uint8_t d7);
+
+    struct timeval getTimestamp() const;
+    void setTimestamp(const struct timeval timestamp);
+    void setTimestamp(const uint64_t seconds, const uint32_t micro_seconds);
+
+    double getFloatTimestamp() const;
+    QDateTime getDateTime() const;
+
+    QString getIdString() const;
+    QString getDataHexString() const;
+
+private:
+	uint32_t _raw_id;
+    uint8_t _dlc;
+    bool _isFD;
+    bool _isBRS;
+    CanInterfaceId _interface;
+    union {
+        uint8_t _u8[8*8];
+        uint16_t _u16[4*8];
+        uint32_t _u32[2*8];
+        uint64_t _u64[8];
+	};
+    struct timeval _timestamp;
+
+};

+ 215 - 0
src/tool/adc_cantool/core/CanTrace.cpp

@@ -0,0 +1,215 @@
+/*
+
+  Copyright (c) 2015, 2016 Hubert Denkmair <hubert@denkmair.de>
+
+  This file is part of cangaroo.
+
+  cangaroo is free software: you can redistribute it and/or modify
+  it under the terms of the GNU General Public License as published by
+  the Free Software Foundation, either version 2 of the License, or
+  (at your option) any later version.
+
+  cangaroo 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 General Public License for more details.
+
+  You should have received a copy of the GNU General Public License
+  along with cangaroo.  If not, see <http://www.gnu.org/licenses/>.
+
+*/
+
+#include "CanTrace.h"
+#include <QMutexLocker>
+#include <QFile>
+#include <QTextStream>
+
+#include <core/Backend.h>
+#include <core/CanMessage.h>
+#include <core/CanDbMessage.h>
+#include <core/CanDbSignal.h>
+#include <driver/CanInterface.h>
+
+CanTrace::CanTrace(Backend &backend, QObject *parent, int flushInterval)
+  : QObject(parent),
+    _backend(backend),
+    _isTimerRunning(false),
+    _mutex(QMutex::Recursive),
+    _timerMutex(),
+    _flushTimer(this)
+{
+    clear();
+    _flushTimer.setSingleShot(true);
+    _flushTimer.setInterval(flushInterval);
+    connect(&_flushTimer, SIGNAL(timeout()), this, SLOT(flushQueue()));
+}
+
+unsigned long CanTrace::size()
+{
+    QMutexLocker locker(&_mutex);
+    return _dataRowsUsed;
+}
+
+void CanTrace::clear()
+{
+    QMutexLocker locker(&_mutex);
+    emit beforeClear();
+    _data.resize(pool_chunk_size);
+    _dataRowsUsed = 0;
+    _newRows = 0;
+    emit afterClear();
+}
+
+const CanMessage *CanTrace::getMessage(int idx)
+{
+    QMutexLocker locker(&_mutex);
+    if (idx >= (_dataRowsUsed + _newRows)) {
+        return 0;
+    } else {
+        return &_data[idx];
+    }
+}
+
+void CanTrace::enqueueMessage(const CanMessage &msg, bool more_to_follow)
+{
+    QMutexLocker locker(&_mutex);
+
+    int idx = size() + _newRows;
+    if (idx>=_data.size()) {
+        _data.resize(_data.size() + pool_chunk_size);
+    }
+
+    _data[idx].cloneFrom(msg);
+    _newRows++;
+
+    if (!more_to_follow) {
+        startTimer();
+    }
+
+    emit messageEnqueued(idx);
+}
+
+void CanTrace::flushQueue()
+{
+    {
+        QMutexLocker locker(&_timerMutex);
+        _isTimerRunning = false;
+    }
+
+    QMutexLocker locker(&_mutex);
+    if (_newRows) {
+        emit beforeAppend(_newRows);
+
+        // see if we have muxed messages. cache muxed values, if any.
+        MeasurementSetup &setup = _backend.getSetup();
+        for (int i=_dataRowsUsed; i<_dataRowsUsed + _newRows; i++) {
+            CanMessage &msg = _data[i];
+            CanDbMessage *dbmsg = setup.findDbMessage(msg);
+            if (dbmsg && dbmsg->getMuxer()) {
+                foreach (CanDbSignal *signal, dbmsg->getSignals()) {
+                    if (signal->isMuxed() && signal->isPresentInMessage(msg)) {
+                        _muxCache[signal] = signal->extractRawDataFromMessage(msg);
+                    }
+                }
+            }
+        }
+
+        _dataRowsUsed += _newRows;
+        _newRows = 0;
+        emit afterAppend();
+    }
+
+}
+
+void CanTrace::startTimer()
+{
+    QMutexLocker locker(&_timerMutex);
+    if (!_isTimerRunning) {
+        _isTimerRunning = true;
+        QMetaObject::invokeMethod(&_flushTimer, "start", Qt::QueuedConnection);
+    }
+}
+
+void CanTrace::saveCanDump(QFile &file)
+{
+    QMutexLocker locker(&_mutex);
+    QTextStream stream(&file);
+    for (unsigned int i=0; i<size(); i++) {
+        CanMessage *msg = &_data[i];
+        QString line;
+        line.append(QString().sprintf("(%.6f) ", msg->getFloatTimestamp()));
+        line.append(_backend.getInterfaceName(msg->getInterfaceId()));
+        if (msg->isExtended()) {
+            line.append(QString().sprintf(" %08X#", msg->getId()));
+        } else {
+            line.append(QString().sprintf(" %03X#", msg->getId()));
+        }
+        for (int i=0; i<msg->getLength(); i++) {
+            line.append(QString().sprintf("%02X", msg->getByte(i)));
+        }
+        stream << line << endl;
+    }
+}
+
+void CanTrace::saveVectorAsc(QFile &file)
+{
+    QMutexLocker locker(&_mutex);
+    QTextStream stream(&file);
+
+    if (_data.length()<1) {
+        return;
+    }
+
+
+    auto firstMessage = _data.first();
+    double t_start = firstMessage.getFloatTimestamp();
+
+    QLocale locale_c(QLocale::C);
+    QString dt_start = locale_c.toString(firstMessage.getDateTime(), "ddd MMM dd hh:mm:ss.zzz ap yyyy");
+
+    stream << "date " << dt_start << endl;
+    stream << "base hex  timestamps absolute" << endl;
+    stream << "internal events logged" << endl;
+    stream << "// version 8.5.0" << endl;
+    stream << "Begin Triggerblock " << dt_start << endl;
+    stream << "   0.000000 Start of measurement" << endl;
+
+    for (unsigned int i=0; i<size(); i++) {
+        CanMessage &msg = _data[i];
+
+        double t_current = msg.getFloatTimestamp();
+        QString id_hex_str = QString().sprintf("%x", msg.getId());
+        QString id_dec_str = QString().sprintf("%d", msg.getId());
+        if (msg.isExtended()) {
+            id_hex_str.append("x");
+            id_dec_str.append("x");
+        }
+
+        // TODO how to handle RTR flag?
+        QString line = QString().sprintf(
+            "%11.6lf 1  %-15s %s   d %d %s  Length = %d BitCount = %d ID = %s",
+            t_current-t_start,
+            id_hex_str.toStdString().c_str(),
+            "Rx", // TODO handle Rx/Tx
+            msg.getLength(),
+            msg.getDataHexString().toStdString().c_str(),
+            0, // TODO Length (transfer time in ns)
+            0, // TODO BitCount (overall frame length, including stuff bits)
+            id_dec_str.toStdString().c_str()
+        );
+
+        stream << line << endl;
+    }
+
+    stream << "End TriggerBlock" << endl;
+}
+
+bool CanTrace::getMuxedSignalFromCache(const CanDbSignal *signal, uint64_t *raw_value)
+{
+    if (_muxCache.contains(signal)) {
+        *raw_value = _muxCache[signal];
+        return true;
+    } else {
+        return false;
+    }
+}

+ 87 - 0
src/tool/adc_cantool/core/CanTrace.h

@@ -0,0 +1,87 @@
+/*
+
+  Copyright (c) 2015, 2016 Hubert Denkmair <hubert@denkmair.de>
+
+  This file is part of cangaroo.
+
+  cangaroo is free software: you can redistribute it and/or modify
+  it under the terms of the GNU General Public License as published by
+  the Free Software Foundation, either version 2 of the License, or
+  (at your option) any later version.
+
+  cangaroo 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 General Public License for more details.
+
+  You should have received a copy of the GNU General Public License
+  along with cangaroo.  If not, see <http://www.gnu.org/licenses/>.
+
+*/
+
+#pragma once
+
+#include <QObject>
+#include <QMutex>
+#include <QTimer>
+#include <QVector>
+#include <QMap>
+#include <QFile>
+
+#include "CanMessage.h"
+
+class CanInterface;
+class CanDbMessage;
+class CanDbSignal;
+class MeasurementSetup;
+class Backend;
+
+class CanTrace : public QObject
+{
+    Q_OBJECT
+
+public:
+    explicit CanTrace(Backend &backend, QObject *parent, int flushInterval);
+
+    unsigned long size();
+    void clear();
+    const CanMessage *getMessage(int idx);
+    void enqueueMessage(const CanMessage &msg, bool more_to_follow=false);
+
+    void saveCanDump(QFile &file);
+    void saveVectorAsc(QFile &file);
+
+    bool getMuxedSignalFromCache(const CanDbSignal *signal, uint64_t *raw_value);
+
+signals:
+    void messageEnqueued(int idx);
+    void beforeAppend(int num_messages);
+    void afterAppend();
+    void beforeClear();
+    void afterClear();
+
+private slots:
+    void flushQueue();
+
+private:
+    enum {
+        pool_chunk_size = 1024
+    };
+
+    Backend &_backend;
+
+    QVector<CanMessage> _data;
+    int _dataRowsUsed;
+    int _newRows;
+    bool _isTimerRunning;
+
+    QMap<const CanDbSignal*,uint64_t> _muxCache;
+
+    QMutex _mutex;
+    QMutex _timerMutex;
+    QTimer _flushTimer;
+
+    void startTimer();
+
+
+};

+ 44 - 0
src/tool/adc_cantool/core/ConfigurableWidget.cpp

@@ -0,0 +1,44 @@
+/*
+
+  Copyright (c) 2015, 2016 Hubert Denkmair <hubert@denkmair.de>
+
+  This file is part of cangaroo.
+
+  cangaroo is free software: you can redistribute it and/or modify
+  it under the terms of the GNU General Public License as published by
+  the Free Software Foundation, either version 2 of the License, or
+  (at your option) any later version.
+
+  cangaroo 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 General Public License for more details.
+
+  You should have received a copy of the GNU General Public License
+  along with cangaroo.  If not, see <http://www.gnu.org/licenses/>.
+
+*/
+
+#include "ConfigurableWidget.h"
+#include <QDomDocument>
+#include <core/Backend.h>
+
+ConfigurableWidget::ConfigurableWidget(QWidget *parent) : QWidget(parent)
+{
+
+}
+
+bool ConfigurableWidget::saveXML(Backend &backend, QDomDocument &xml, QDomElement &root)
+{
+    (void) backend;
+    (void) xml;
+    (void) root;
+    return true;
+}
+
+bool ConfigurableWidget::loadXML(Backend &backend, QDomElement &el)
+{
+    (void) backend;
+    (void) el;
+    return true;
+}

+ 43 - 0
src/tool/adc_cantool/core/ConfigurableWidget.h

@@ -0,0 +1,43 @@
+/*
+
+  Copyright (c) 2015, 2016 Hubert Denkmair <hubert@denkmair.de>
+
+  This file is part of cangaroo.
+
+  cangaroo is free software: you can redistribute it and/or modify
+  it under the terms of the GNU General Public License as published by
+  the Free Software Foundation, either version 2 of the License, or
+  (at your option) any later version.
+
+  cangaroo 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 General Public License for more details.
+
+  You should have received a copy of the GNU General Public License
+  along with cangaroo.  If not, see <http://www.gnu.org/licenses/>.
+
+*/
+
+#pragma once
+
+#include <QObject>
+#include <QWidget>
+
+class Backend;
+class QDomDocument;
+class QDomElement;
+
+class ConfigurableWidget : public QWidget
+{
+    Q_OBJECT
+public:
+    explicit ConfigurableWidget(QWidget *parent = 0);
+    virtual bool saveXML(Backend &backend, QDomDocument &xml, QDomElement &root);
+    virtual bool loadXML(Backend &backend, QDomElement &el);
+
+signals:
+    void settingsChanged(ConfigurableWidget *sender);
+
+public slots:
+};

+ 63 - 0
src/tool/adc_cantool/core/Log.cpp

@@ -0,0 +1,63 @@
+/*
+
+  Copyright (c) 2016 Hubert Denkmair <hubert@denkmair.de>
+
+  This file is part of cangaroo.
+
+  cangaroo is free software: you can redistribute it and/or modify
+  it under the terms of the GNU General Public License as published by
+  the Free Software Foundation, either version 2 of the License, or
+  (at your option) any later version.
+
+  cangaroo 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 General Public License for more details.
+
+  You should have received a copy of the GNU General Public License
+  along with cangaroo.  If not, see <http://www.gnu.org/licenses/>.
+
+*/
+
+#include "Log.h"
+#include <core/Backend.h>
+
+void log_msg(const QDateTime dt, const log_level_t level, const QString msg)
+{
+    Backend::instance().logMessage(dt, level, msg);
+}
+
+void log_msg(const log_level_t level, const QString msg)
+{
+    Backend::instance().logMessage(QDateTime::currentDateTime(), level, msg);
+}
+
+void log_debug(const QString msg)
+{
+    log_msg(log_level_debug, msg);
+}
+
+void log_info(const QString msg)
+{
+    log_msg(log_level_info, msg);
+}
+
+void log_warning(const QString msg)
+{
+    log_msg(log_level_warning, msg);
+}
+
+void log_error(const QString msg)
+{
+    log_msg(log_level_error, msg);
+}
+
+void log_critical(const QString msg)
+{
+    log_msg(log_level_critical, msg);
+}
+
+void log_fatal(const QString msg)
+{
+    log_msg(log_level_fatal, msg);
+}

+ 43 - 0
src/tool/adc_cantool/core/Log.h

@@ -0,0 +1,43 @@
+/*
+
+  Copyright (c) 2016 Hubert Denkmair <hubert@denkmair.de>
+
+  This file is part of cangaroo.
+
+  cangaroo is free software: you can redistribute it and/or modify
+  it under the terms of the GNU General Public License as published by
+  the Free Software Foundation, either version 2 of the License, or
+  (at your option) any later version.
+
+  cangaroo 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 General Public License for more details.
+
+  You should have received a copy of the GNU General Public License
+  along with cangaroo.  If not, see <http://www.gnu.org/licenses/>.
+
+*/
+
+#pragma once
+
+#include <QDateTime>
+
+typedef enum log_level {
+    log_level_debug,
+    log_level_info,
+    log_level_warning,
+    log_level_error,
+    log_level_critical,
+    log_level_fatal
+} log_level_t;
+
+void log_msg(const QDateTime dt, const log_level_t level, const QString msg);
+void log_msg(const log_level_t level, const QString msg);
+
+void log_debug(const QString msg);
+void log_info(const QString msg);
+void log_warning(const QString msg);
+void log_error(const QString msg);
+void log_critical(const QString msg);
+void log_fatal(const QString msg);

+ 170 - 0
src/tool/adc_cantool/core/LogModel.cpp

@@ -0,0 +1,170 @@
+/*
+
+  Copyright (c) 2016 Hubert Denkmair <hubert@denkmair.de>
+
+  This file is part of cangaroo.
+
+  cangaroo is free software: you can redistribute it and/or modify
+  it under the terms of the GNU General Public License as published by
+  the Free Software Foundation, either version 2 of the License, or
+  (at your option) any later version.
+
+  cangaroo 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 General Public License for more details.
+
+  You should have received a copy of the GNU General Public License
+  along with cangaroo.  If not, see <http://www.gnu.org/licenses/>.
+
+*/
+
+
+#include "LogModel.h"
+
+LogModel::LogModel(Backend &backend)
+{
+    connect(&backend, SIGNAL(onLogMessage(QDateTime,log_level_t,QString)), this, SLOT(onLogMessage(QDateTime,log_level_t,QString)));
+}
+
+LogModel::~LogModel()
+{
+    qDeleteAll(_items);
+    _items.clear();
+}
+
+void LogModel::clear()
+{
+    beginResetModel();
+    _items.clear();
+    endResetModel();
+}
+
+QModelIndex LogModel::index(int row, int column, const QModelIndex &parent) const
+{
+    if (parent.isValid()) {
+        return QModelIndex();
+    } else {
+        return createIndex(row, column, (quintptr)0);
+    }
+}
+
+QModelIndex LogModel::parent(const QModelIndex &child) const
+{
+    (void) child;
+    return QModelIndex();
+}
+
+int LogModel::rowCount(const QModelIndex &parent) const
+{
+    (void) parent;
+    return _items.size();
+}
+
+int LogModel::columnCount(const QModelIndex &parent) const
+{
+    (void) parent;
+    return column_count;
+}
+
+bool LogModel::hasChildren(const QModelIndex &parent) const
+{
+    return !parent.isValid();
+}
+
+QVariant LogModel::headerData(int section, Qt::Orientation orientation, int role) const
+{
+    if (role == Qt::DisplayRole) {
+
+//        if (orientation == Qt::Horizontal) {
+//            switch (section) {
+//                case column_time:
+//                    return QString("Time");
+//                case column_level:
+//                    return QString("Level");
+//                case column_text:
+//                    return QString("Message");
+//            }
+//        }
+        if (orientation == Qt::Horizontal) {
+            switch (section) {
+                case column_time:
+                    return QString("时间");
+                case column_level:
+                    return QString("消息类型");
+                case column_text:
+                    return QString("具体消息");
+            }
+        }
+
+    }
+    return QVariant();
+}
+
+QVariant LogModel::data(const QModelIndex &index, int role) const
+{
+
+    if (role == Qt::TextAlignmentRole) {
+        switch (index.column()) {
+            case column_time:
+                return Qt::AlignRight + Qt::AlignVCenter;
+            case column_level:
+                return Qt::AlignCenter + Qt::AlignVCenter;
+            case column_text:
+                return Qt::AlignLeft + Qt::AlignVCenter;
+            default:
+                return QVariant();
+        }
+    }
+
+    if (role != Qt::DisplayRole) {
+        return QVariant();
+    }
+
+    if (!index.isValid()) {
+        return QVariant();
+    }
+
+    LogItem *item = _items.value(index.row(), 0);
+    if (item) {
+
+        switch (index.column()) {
+            case column_time:
+                return item->dt.toString("hh:mm:ss");
+            case column_level:
+                return logLevelText(item->level);
+            case column_text:
+                return item->text;
+            default:
+                return QVariant();
+        }
+
+    }
+    return QVariant();
+}
+
+void LogModel::onLogMessage(const QDateTime dt, const log_level_t level, const QString msg)
+{
+    LogItem *item = new LogItem();
+    item->dt = dt;
+    item->level = level;
+    item->text = msg;
+
+    beginInsertRows(QModelIndex(), _items.size(), _items.size());
+    _items.append(item);
+    endInsertRows();
+}
+
+QString LogModel::logLevelText(log_level_t level)
+{
+    switch (level) {
+        case log_level_debug: return "debug";
+        case log_level_info: return "info";
+        case log_level_warning: return "warning";
+        case log_level_error: return "error";
+        case log_level_critical: return "critical";
+        case log_level_fatal: return "fatal";
+        default: return "";
+    }
+}
+

+ 71 - 0
src/tool/adc_cantool/core/LogModel.h

@@ -0,0 +1,71 @@
+/*
+
+  Copyright (c) 2016 Hubert Denkmair <hubert@denkmair.de>
+
+  This file is part of cangaroo.
+
+  cangaroo is free software: you can redistribute it and/or modify
+  it under the terms of the GNU General Public License as published by
+  the Free Software Foundation, either version 2 of the License, or
+  (at your option) any later version.
+
+  cangaroo 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 General Public License for more details.
+
+  You should have received a copy of the GNU General Public License
+  along with cangaroo.  If not, see <http://www.gnu.org/licenses/>.
+
+*/
+
+#pragma once
+
+#include <QAbstractItemModel>
+#include <QDateTime>
+#include <QList>
+#include <core/Backend.h>
+
+class LogItem {
+public:
+    QDateTime dt;
+    log_level_t level;
+    QString text;
+};
+
+class LogModel : public QAbstractItemModel
+{
+    Q_OBJECT
+
+public:
+    enum {
+        column_time,
+        column_level,
+        column_text,
+        column_count
+    };
+
+public:
+    LogModel(Backend &backend);
+    virtual ~LogModel();
+
+    void clear();
+
+    virtual QModelIndex index(int row, int column, const QModelIndex &parent) const;
+    virtual QModelIndex parent(const QModelIndex &child) const;
+
+    virtual int rowCount(const QModelIndex &parent) const;
+    virtual int columnCount(const QModelIndex &parent) const;
+    virtual bool hasChildren(const QModelIndex &parent) const;
+
+    virtual QVariant headerData(int section, Qt::Orientation orientation, int role) const;
+    virtual QVariant data(const QModelIndex &index, int role) const;
+
+public slots:
+    void onLogMessage(const QDateTime dt, const log_level_t level, const QString msg);
+
+private:
+    QList<LogItem*> _items;
+
+    static QString logLevelText(log_level_t level);
+};

+ 215 - 0
src/tool/adc_cantool/core/MeasurementInterface.cpp

@@ -0,0 +1,215 @@
+/*
+
+  Copyright (c) 2015, 2016 Hubert Denkmair <hubert@denkmair.de>
+
+  This file is part of cangaroo.
+
+  cangaroo is free software: you can redistribute it and/or modify
+  it under the terms of the GNU General Public License as published by
+  the Free Software Foundation, either version 2 of the License, or
+  (at your option) any later version.
+
+  cangaroo 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 General Public License for more details.
+
+  You should have received a copy of the GNU General Public License
+  along with cangaroo.  If not, see <http://www.gnu.org/licenses/>.
+
+*/
+
+#include "MeasurementInterface.h"
+
+#include <core/Backend.h>
+#include <driver/CanDriver.h>
+#include <driver/CanInterface.h>
+
+
+MeasurementInterface::MeasurementInterface()
+  : _doConfigure(true),
+    _bitrate(500000),
+    _samplePoint(875),
+    _isCanFD(false),
+    _fdBitrate(4000000),
+    _fdSamplePoint(875),
+
+    _isListenOnlyMode(false),
+    _isOneShotMode(false),
+    _isTripleSampling(false),
+    _doAutoRestart(false),
+    _autoRestartMs(100)
+{
+
+}
+
+bool MeasurementInterface::loadXML(Backend &backend, QDomElement &el)
+{
+    (void) backend;
+
+    _doConfigure = el.attribute("configure", "0").toInt() != 0;
+
+    _bitrate = el.attribute("bitrate", "500000").toInt();
+    _samplePoint = el.attribute("sample-point", "875").toInt();
+    _isCanFD = el.attribute("can-fd", "0").toInt() != 0;
+    _fdBitrate = el.attribute("bitrate-fd", "500000").toInt();
+    _fdSamplePoint = el.attribute("sample-point-fd", "875").toInt();
+
+    _isListenOnlyMode = el.attribute("listen-only", "0").toInt() != 0;
+    _isOneShotMode = el.attribute("one-shot", "0").toInt() != 0;
+    _isTripleSampling = el.attribute("triple-sampling", "0").toInt() != 0;
+    _doAutoRestart = el.attribute("auto-restart", "0").toInt() != 0;
+    _autoRestartMs = el.attribute("auto-restart-time", "100").toInt();
+
+    return true;
+}
+
+bool MeasurementInterface::saveXML(Backend &backend, QDomDocument &xml, QDomElement &root)
+{
+    (void) xml;
+
+    root.setAttribute("type", "can");
+    root.setAttribute("driver", backend.getDriverName(_canif));
+    root.setAttribute("name", backend.getInterfaceName(_canif));
+
+    root.setAttribute("configure", _doConfigure ? 1 : 0);
+
+    root.setAttribute("bitrate", _bitrate);
+    root.setAttribute("sample-point", _samplePoint);
+    root.setAttribute("can-fd", _isCanFD ? 1 : 0);
+    root.setAttribute("bitrate-fd", _fdBitrate);
+    root.setAttribute("sample-point-fd", _fdSamplePoint);
+
+    root.setAttribute("listen-only", _isListenOnlyMode ? 1 : 0);
+    root.setAttribute("one-shot", _isOneShotMode ? 1 : 0);
+    root.setAttribute("triple-sampling", _isTripleSampling ? 1 : 0);
+    root.setAttribute("auto-restart", _doAutoRestart ? 1 : 0);
+    root.setAttribute("auto-restart-time", _autoRestartMs);
+
+    return true;
+}
+
+unsigned MeasurementInterface::bitrate() const
+{
+    return _bitrate;
+}
+
+void MeasurementInterface::setBitrate(unsigned bitrate)
+{
+    _bitrate = bitrate;
+}
+
+CanInterfaceId MeasurementInterface::canInterface() const
+{
+    return _canif;
+}
+
+void MeasurementInterface::setCanInterface(CanInterfaceId canif)
+{
+    _canif = canif;
+}
+
+void MeasurementInterface::cloneFrom(MeasurementInterface &origin)
+{
+    *this = origin;
+}
+
+bool MeasurementInterface::doConfigure() const
+{
+    return _doConfigure;
+}
+
+void MeasurementInterface::setDoConfigure(bool doConfigure)
+{
+    _doConfigure = doConfigure;
+}
+
+bool MeasurementInterface::isListenOnlyMode() const
+{
+    return _isListenOnlyMode;
+}
+
+void MeasurementInterface::setListenOnlyMode(bool isListenOnlyMode)
+{
+    _isListenOnlyMode = isListenOnlyMode;
+}
+
+bool MeasurementInterface::isOneShotMode() const
+{
+    return _isOneShotMode;
+}
+
+void MeasurementInterface::setOneShotMode(bool isOneShotMode)
+{
+    _isOneShotMode = isOneShotMode;
+}
+
+bool MeasurementInterface::isTripleSampling() const
+{
+    return _isTripleSampling;
+}
+
+void MeasurementInterface::setTripleSampling(bool isTripleSampling)
+{
+    _isTripleSampling = isTripleSampling;
+}
+
+bool MeasurementInterface::isCanFD() const
+{
+    return _isCanFD;
+}
+
+void MeasurementInterface::setCanFD(bool isCanFD)
+{
+    _isCanFD = isCanFD;
+}
+
+int MeasurementInterface::samplePoint() const
+{
+    return _samplePoint;
+}
+
+void MeasurementInterface::setSamplePoint(int samplePoint)
+{
+    _samplePoint = samplePoint;
+}
+
+unsigned MeasurementInterface::fdBitrate() const
+{
+    return _fdBitrate;
+}
+
+void MeasurementInterface::setFdBitrate(unsigned fdBitrate)
+{
+    _fdBitrate = fdBitrate;
+}
+
+unsigned MeasurementInterface::fdSamplePoint() const
+{
+    return _fdSamplePoint;
+}
+
+void MeasurementInterface::setFdSamplePoint(unsigned fdSamplePoint)
+{
+    _fdSamplePoint = fdSamplePoint;
+}
+
+bool MeasurementInterface::doAutoRestart() const
+{
+    return _doAutoRestart;
+}
+
+void MeasurementInterface::setAutoRestart(bool doAutoRestart)
+{
+    _doAutoRestart = doAutoRestart;
+}
+
+int MeasurementInterface::autoRestartMs() const
+{
+    return _autoRestartMs;
+}
+
+void MeasurementInterface::setAutoRestartMs(int autoRestartMs)
+{
+    _autoRestartMs = autoRestartMs;
+}

+ 92 - 0
src/tool/adc_cantool/core/MeasurementInterface.h

@@ -0,0 +1,92 @@
+/*
+
+  Copyright (c) 2015, 2016 Hubert Denkmair <hubert@denkmair.de>
+
+  This file is part of cangaroo.
+
+  cangaroo is free software: you can redistribute it and/or modify
+  it under the terms of the GNU General Public License as published by
+  the Free Software Foundation, either version 2 of the License, or
+  (at your option) any later version.
+
+  cangaroo 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 General Public License for more details.
+
+  You should have received a copy of the GNU General Public License
+  along with cangaroo.  If not, see <http://www.gnu.org/licenses/>.
+
+*/
+
+#pragma once
+
+#include <QDomDocument>
+#include <driver/CanDriver.h>
+#include <driver/CanInterface.h>
+
+class Backend;
+
+class MeasurementInterface
+{
+public:
+    MeasurementInterface();
+
+    CanInterfaceId canInterface() const;
+    void setCanInterface(CanInterfaceId canif);
+
+    void cloneFrom(MeasurementInterface &origin);
+    bool loadXML(Backend &backend, QDomElement &el);
+    bool saveXML(Backend &backend, QDomDocument &xml, QDomElement &root);
+
+    bool doConfigure() const;
+    void setDoConfigure(bool doConfigure);
+
+    bool isListenOnlyMode() const;
+    void setListenOnlyMode(bool isListenOnlyMode);
+
+    bool isOneShotMode() const;
+    void setOneShotMode(bool isOneShotMode);
+
+    bool isTripleSampling() const;
+    void setTripleSampling(bool isTripleSampling);
+
+    bool isCanFD() const;
+    void setCanFD(bool isCanFD);
+
+    unsigned bitrate() const;
+    void setBitrate(unsigned bitrate);
+
+    int samplePoint() const;
+    void setSamplePoint(int samplePoint);
+
+    unsigned fdBitrate() const;
+    void setFdBitrate(unsigned fdBitrate);
+
+    unsigned fdSamplePoint() const;
+    void setFdSamplePoint(unsigned fdSamplePoint);
+
+    bool doAutoRestart() const;
+    void setAutoRestart(bool doAutoRestart);
+
+    int autoRestartMs() const;
+    void setAutoRestartMs(int autoRestartMs);
+
+private:
+    CanInterfaceId _canif;
+
+    bool _doConfigure;
+
+    unsigned _bitrate;
+    unsigned _samplePoint;
+
+    bool _isCanFD;
+    unsigned _fdBitrate;
+    unsigned _fdSamplePoint;
+
+    bool _isListenOnlyMode;
+    bool _isOneShotMode;
+    bool _isTripleSampling;
+    bool _doAutoRestart;
+    int _autoRestartMs;
+};

+ 159 - 0
src/tool/adc_cantool/core/MeasurementNetwork.cpp

@@ -0,0 +1,159 @@
+/*
+
+  Copyright (c) 2015, 2016 Hubert Denkmair <hubert@denkmair.de>
+
+  This file is part of cangaroo.
+
+  cangaroo is free software: you can redistribute it and/or modify
+  it under the terms of the GNU General Public License as published by
+  the Free Software Foundation, either version 2 of the License, or
+  (at your option) any later version.
+
+  cangaroo 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 General Public License for more details.
+
+  You should have received a copy of the GNU General Public License
+  along with cangaroo.  If not, see <http://www.gnu.org/licenses/>.
+
+*/
+
+#include "MeasurementNetwork.h"
+#include "MeasurementInterface.h"
+
+#include <core/Backend.h>
+
+
+MeasurementNetwork::MeasurementNetwork()
+{
+}
+
+void MeasurementNetwork::cloneFrom(MeasurementNetwork &origin)
+{
+    _name = origin._name;
+    foreach (MeasurementInterface *omi, origin._interfaces) {
+        MeasurementInterface *mi = new MeasurementInterface();
+        mi->cloneFrom(*omi);
+        _interfaces.append(mi);
+    }
+    _canDbs = origin._canDbs;
+}
+
+void MeasurementNetwork::addInterface(MeasurementInterface *intf)
+{
+    _interfaces.append(intf);
+}
+
+void MeasurementNetwork::removeInterface(MeasurementInterface *intf)
+{
+    _interfaces.removeAll(intf);
+}
+
+QList<MeasurementInterface *> MeasurementNetwork::interfaces()
+{
+    return _interfaces;
+}
+
+MeasurementInterface *MeasurementNetwork::addCanInterface(CanInterfaceId canif)
+{
+    MeasurementInterface *mi = new MeasurementInterface();
+    mi->setCanInterface(canif);
+    addInterface(mi);
+    return mi;
+}
+
+CanInterfaceIdList MeasurementNetwork::getReferencedCanInterfaces()
+{
+    CanInterfaceIdList list;
+    foreach (MeasurementInterface *mi, _interfaces) {
+        list << mi->canInterface();
+    }
+    return list;
+}
+
+void MeasurementNetwork::addCanDb(QSharedPointer<CanDb> candb)
+{
+    _canDbs.append(candb);
+}
+
+void MeasurementNetwork::reloadCanDbs(Backend *backend)
+{
+    foreach(pCanDb db, _canDbs)
+    {
+        db = backend->loadDbc(db->getPath());
+    }
+}
+
+
+QString MeasurementNetwork::name() const
+{
+    return _name;
+}
+
+void MeasurementNetwork::setName(const QString &name)
+{
+    _name = name;
+}
+
+bool MeasurementNetwork::saveXML(Backend &backend, QDomDocument &xml, QDomElement &root)
+{
+    root.setAttribute("name", _name);
+
+    QDomElement interfacesNode = xml.createElement("interfaces");
+    foreach (MeasurementInterface *intf, _interfaces) {
+        QDomElement intfNode = xml.createElement("interface");
+        if (!intf->saveXML(backend, xml, intfNode)) {
+            return false;
+        }
+        interfacesNode.appendChild(intfNode);
+    }
+    root.appendChild(interfacesNode);
+
+    QDomElement candbsNode = xml.createElement("databases");
+    foreach (pCanDb candb, _canDbs) {
+        QDomElement dbNode = xml.createElement("database");
+        if (!candb->saveXML(backend, xml, dbNode)) {
+            return false;
+        }
+        candbsNode.appendChild(dbNode);
+    }
+    root.appendChild(candbsNode);
+
+
+    return true;
+}
+
+bool MeasurementNetwork::loadXML(Backend &backend, QDomElement el)
+{
+    setName(el.attribute("name", "unnamed network"));
+
+    QDomNodeList ifList = el.firstChildElement("interfaces").elementsByTagName("interface");
+    for (int i=0; i<ifList.length(); i++) {
+        QDomElement elIntf = ifList.item(i).toElement();
+        QString driverName = elIntf.attribute("driver");
+        QString deviceName = elIntf.attribute("name");
+        CanInterface *intf = backend.getInterfaceByDriverAndName(driverName, deviceName);
+        if (intf) {
+            MeasurementInterface *mi = addCanInterface(intf->getId());
+            mi->loadXML(backend, elIntf);
+        } else {
+            log_error(QString("Could not find interface %1/%2, which is referenced in the workspace config file.").arg(driverName, deviceName));
+        }
+    }
+
+
+    QDomNodeList dbList = el.firstChildElement("databases").elementsByTagName("database");
+    for (int i=0; i<dbList.length(); i++) {
+        QDomElement elDb = dbList.item(i).toElement();
+        QString filename = elDb.attribute("filename", QString());
+        if (!filename.isEmpty()) {
+            addCanDb(backend.loadDbc(filename));
+        } else {
+            log_error(QString("Unable to load CanDB: %1").arg(filename));
+        }
+    }
+
+    return true;
+}
+

+ 61 - 0
src/tool/adc_cantool/core/MeasurementNetwork.h

@@ -0,0 +1,61 @@
+/*
+
+  Copyright (c) 2015, 2016 Hubert Denkmair <hubert@denkmair.de>
+
+  This file is part of cangaroo.
+
+  cangaroo is free software: you can redistribute it and/or modify
+  it under the terms of the GNU General Public License as published by
+  the Free Software Foundation, either version 2 of the License, or
+  (at your option) any later version.
+
+  cangaroo 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 General Public License for more details.
+
+  You should have received a copy of the GNU General Public License
+  along with cangaroo.  If not, see <http://www.gnu.org/licenses/>.
+
+*/
+
+#pragma once
+
+#include <QString>
+#include <QList>
+#include <QDomDocument>
+
+#include <driver/CanDriver.h>
+#include <driver/CanInterface.h>
+#include <core/CanDb.h>
+
+class Backend;
+class MeasurementInterface;
+
+class MeasurementNetwork
+{
+public:
+    MeasurementNetwork();
+    void cloneFrom(MeasurementNetwork &origin);
+
+    void addInterface(MeasurementInterface *intf);
+    void removeInterface(MeasurementInterface *intf);
+    QList<MeasurementInterface*> interfaces();
+
+    MeasurementInterface *addCanInterface(CanInterfaceId canif);
+    CanInterfaceIdList getReferencedCanInterfaces();
+
+    void addCanDb(pCanDb candb);
+    void reloadCanDbs(Backend *backend);
+    QList<pCanDb> _canDbs;
+
+    QString name() const;
+    void setName(const QString &name);
+
+    bool saveXML(Backend &backend, QDomDocument &xml, QDomElement &root);
+    bool loadXML(Backend &backend, QDomElement el);
+
+private:
+    QString _name;
+    QList<MeasurementInterface*> _interfaces;
+};

+ 145 - 0
src/tool/adc_cantool/core/MeasurementSetup.cpp

@@ -0,0 +1,145 @@
+/*
+
+  Copyright (c) 2015, 2016 Hubert Denkmair <hubert@denkmair.de>
+
+  This file is part of cangaroo.
+
+  cangaroo is free software: you can redistribute it and/or modify
+  it under the terms of the GNU General Public License as published by
+  the Free Software Foundation, either version 2 of the License, or
+  (at your option) any later version.
+
+  cangaroo 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 General Public License for more details.
+
+  You should have received a copy of the GNU General Public License
+  along with cangaroo.  If not, see <http://www.gnu.org/licenses/>.
+
+*/
+
+#include "MeasurementSetup.h"
+
+#include <QThread>
+#include <QMetaType>
+
+#include <core/CanTrace.h>
+#include <core/CanMessage.h>
+#include <core/MeasurementNetwork.h>
+
+MeasurementSetup::MeasurementSetup(QObject *parent)
+  : QObject(parent)
+{
+}
+
+MeasurementSetup::~MeasurementSetup()
+{
+    qDeleteAll(_networks);
+}
+
+void MeasurementSetup::clear()
+{
+    qDeleteAll(_networks);
+    _networks.clear();
+    emit onSetupChanged();
+}
+
+void MeasurementSetup::cloneFrom(MeasurementSetup &origin)
+{
+    clear();
+    foreach (MeasurementNetwork *network, origin._networks) {
+        MeasurementNetwork *network_copy = new MeasurementNetwork();
+        network_copy->cloneFrom(*network);
+        _networks.append(network_copy);
+    }
+    emit onSetupChanged();
+}
+
+bool MeasurementSetup::saveXML(Backend &backend, QDomDocument &xml, QDomElement &root)
+{
+    foreach (MeasurementNetwork *network, _networks) {
+        QDomElement networkNode = xml.createElement("network");
+        if (!network->saveXML(backend, xml, networkNode)) {
+            return false;
+        }
+        root.appendChild(networkNode);
+    }
+    return true;
+}
+
+bool MeasurementSetup::loadXML(Backend &backend, QDomElement &el)
+{
+    clear();
+
+    QDomNodeList networks = el.elementsByTagName("network");
+    for (int i=0; i<networks.length(); i++) {
+        MeasurementNetwork *network = createNetwork();
+        if (!network->loadXML(backend, networks.item(i).toElement())) {
+            return false;
+        }
+    }
+
+    emit onSetupChanged();
+    return true;
+}
+
+
+MeasurementNetwork *MeasurementSetup::createNetwork()
+{
+    MeasurementNetwork *network = new MeasurementNetwork();
+    _networks.append(network);
+    return network;
+}
+
+void MeasurementSetup::removeNetwork(MeasurementNetwork *network)
+{
+    _networks.removeAll(network);
+}
+
+
+CanDbMessage *MeasurementSetup::findDbMessage(const CanMessage &msg) const
+{
+    CanDbMessage *result = 0;
+
+    foreach (MeasurementNetwork *network, _networks) {
+        foreach (pCanDb db, network->_canDbs) {
+            result = db->getMessageById(msg.getRawId());
+            if (result != 0) {
+                return result;
+            }
+        }
+    }
+    return result;
+}
+
+QString MeasurementSetup::getInterfaceName(const CanInterface &interface) const
+{
+    return interface.getName();
+}
+
+int MeasurementSetup::countNetworks() const
+{
+    return _networks.length();
+}
+
+MeasurementNetwork *MeasurementSetup::getNetwork(int index) const
+{
+    return _networks.value(index);
+}
+
+MeasurementNetwork *MeasurementSetup::getNetworkByName(QString name) const
+{
+    foreach (MeasurementNetwork *network, _networks) {
+        if (network->name() == name) {
+            return network;
+        }
+    }
+    return 0;
+}
+
+QList<MeasurementNetwork *> MeasurementSetup::getNetworks()
+{
+    return _networks;
+}
+

+ 63 - 0
src/tool/adc_cantool/core/MeasurementSetup.h

@@ -0,0 +1,63 @@
+/*
+
+  Copyright (c) 2015, 2016 Hubert Denkmair <hubert@denkmair.de>
+
+  This file is part of cangaroo.
+
+  cangaroo is free software: you can redistribute it and/or modify
+  it under the terms of the GNU General Public License as published by
+  the Free Software Foundation, either version 2 of the License, or
+  (at your option) any later version.
+
+  cangaroo 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 General Public License for more details.
+
+  You should have received a copy of the GNU General Public License
+  along with cangaroo.  If not, see <http://www.gnu.org/licenses/>.
+
+*/
+
+#pragma once
+
+#include <QObject>
+#include <QList>
+#include <QDomDocument>
+
+class Backend;
+class MeasurementNetwork;
+class CanTrace;
+class CanMessage;
+class CanInterface;
+class CanDbMessage;
+
+class MeasurementSetup : public QObject
+{
+    Q_OBJECT
+
+public:
+    explicit MeasurementSetup(QObject *parent);
+    virtual ~MeasurementSetup();
+    void clear();
+
+    CanDbMessage *findDbMessage(const CanMessage &msg) const;
+    QString getInterfaceName(const CanInterface &interface) const;
+
+    int countNetworks() const;
+    MeasurementNetwork *getNetwork(int index) const;
+    MeasurementNetwork *getNetworkByName(QString name) const;
+    QList<MeasurementNetwork*> getNetworks();
+    MeasurementNetwork *createNetwork();
+    void removeNetwork(MeasurementNetwork *network);
+
+    void cloneFrom(MeasurementSetup &origin);
+    bool saveXML(Backend &backend, QDomDocument &xml, QDomElement &root);
+    bool loadXML(Backend &backend, QDomElement &el);
+
+signals:
+    void onSetupChanged();
+
+private:
+    QList<MeasurementNetwork*> _networks;
+};

+ 30 - 0
src/tool/adc_cantool/core/core.pri

@@ -0,0 +1,30 @@
+SOURCES += \
+    $$PWD/Backend.cpp \
+    $$PWD/CanMessage.cpp \
+    $$PWD/CanTrace.cpp \
+    $$PWD/CanDbMessage.cpp \
+    $$PWD/CanDb.cpp \
+    $$PWD/CanDbNode.cpp \
+    $$PWD/CanDbSignal.cpp \
+    $$PWD/MeasurementSetup.cpp \
+    $$PWD/MeasurementNetwork.cpp \
+    $$PWD/MeasurementInterface.cpp \
+    $$PWD/LogModel.cpp \
+    $$PWD/ConfigurableWidget.cpp \
+    $$PWD/Log.cpp
+
+HEADERS += \
+    $$PWD/portable_endian.h \
+    $$PWD/Backend.h \
+    $$PWD/CanMessage.h \
+    $$PWD/CanTrace.h \
+    $$PWD/CanDbMessage.h \
+    $$PWD/CanDb.h \
+    $$PWD/CanDbNode.h \
+    $$PWD/CanDbSignal.h \
+    $$PWD/MeasurementSetup.h \
+    $$PWD/MeasurementNetwork.h \
+    $$PWD/MeasurementInterface.h \
+    $$PWD/LogModel.h \
+    $$PWD/ConfigurableWidget.h \
+    $$PWD/Log.h

+ 73 - 0
src/tool/adc_cantool/core/portable_endian.h

@@ -0,0 +1,73 @@
+// "License": Public Domain
+// I, Mathias Panzenböck, place this file hereby into the public domain. Use it at your own risk for whatever you like.
+// In case there are jurisdictions that don't support putting things in the public domain you can also consider it to
+// be "dual licensed" under the BSD, MIT and Apache licenses, if you want to. This code is trivial anyway. Consider it
+// an example on how to get the endian conversion functions on different platforms.
+
+#pragma once
+
+#if (defined(_WIN16) || defined(_WIN32) || defined(_WIN64)) && !defined(__WINDOWS__)
+
+#	define __WINDOWS__
+
+#endif
+
+#if defined(__linux__) || defined(__CYGWIN__)
+
+#	include <endian.h>
+
+#elif defined(__WINDOWS__)
+
+#	include <sys/param.h>
+
+#	if BYTE_ORDER == LITTLE_ENDIAN
+
+#		define htobe16(x) htons(x)
+#		define htole16(x) (x)
+#		define be16toh(x) ntohs(x)
+#		define le16toh(x) (x)
+ 
+#		define htobe32(x) htonl(x)
+#		define htole32(x) (x)
+#		define be32toh(x) ntohl(x)
+#		define le32toh(x) (x)
+ 
+#		define htobe64(x) htonll(x)
+#		define htole64(x) (x)
+#		define be64toh(x) ntohll(x)
+#		define le64toh(x) (x)
+
+#	elif BYTE_ORDER == BIG_ENDIAN
+
+		/* that would be xbox 360 */
+#		define htobe16(x) (x)
+#		define htole16(x) __builtin_bswap16(x)
+#		define be16toh(x) (x)
+#		define le16toh(x) __builtin_bswap16(x)
+ 
+#		define htobe32(x) (x)
+#		define htole32(x) __builtin_bswap32(x)
+#		define be32toh(x) (x)
+#		define le32toh(x) __builtin_bswap32(x)
+ 
+#		define htobe64(x) (x)
+#		define htole64(x) __builtin_bswap64(x)
+#		define be64toh(x) (x)
+#		define le64toh(x) __builtin_bswap64(x)
+
+#	else
+
+#		error byte order not supported
+
+#	endif
+
+#	define __BYTE_ORDER    BYTE_ORDER
+#	define __BIG_ENDIAN    BIG_ENDIAN
+#	define __LITTLE_ENDIAN LITTLE_ENDIAN
+#	define __PDP_ENDIAN    PDP_ENDIAN
+
+#else
+
+#	error platform not supported
+
+#endif

+ 4 - 0
src/tool/adc_cantool/debian/cangaroo.install

@@ -0,0 +1,4 @@
+bin/cangaroo usr/bin
+cangaroo.desktop usr/share/applications
+src/assets/cangaroo.png usr/share/pixmaps
+src/assets/cangaroo.svg usr/share/pixmaps

+ 39 - 0
src/tool/adc_cantool/debian/changelog

@@ -0,0 +1,39 @@
+cangaroo (0.2.0-1) experimental; urgency=low
+  * handle muxed signals in backend and trace window
+  * can status window
+  * bugfixes in setup dialog
+  * do not try to extract signals from messages when DLC too short
+  * fix windows build
+  * windows PCAN-basic driver
+  * show timestamps, log level etc. in log window
+
+ -- Hubert Denkmair <hubert@denkmair.de>  Tue, 12 Jan 2016 21:35:00 +0200
+
+cangaroo (0.1.3-1) experimental; urgency=low
+  * new can interface configuration GUI (missing a suid binary to actually set the config)
+  * use libnl-route-3 for socketcan device config read
+  * query socketcan interfaces for supported config options
+  * new logging subsystem, do not use QDebug any more
+  * some performance improvements when receiving lots of messages 
+  * bugfixs with time-delta view
+
+ -- Hubert Denkmair <hubert@denkmair.de>  Tue, 12 Jan 2016 21:35:00 +0200
+
+cangaroo (0.1.2-1) experimental; urgency=low
+  * fix device re-scan ("could not bind" console message)
+  * fix some dbc parsing issues (signed signals, ...)
+  * implement big endian signals
+
+ -- Hubert Denkmair <hubert@denkmair.de>  Tue, 12 Jan 2016 21:35:00 +0200
+
+cangaroo (0.1.1-1) experimental; urgency=low
+  * change source structure to better fit debian packaging
+  * add debian packaging info
+
+ -- Hubert Denkmair <hubert@denkmair.de>  Mon, 11 Jan 2016 00:30:00 +0200
+
+cangaroo (0.1.0-1) experimental; urgency=low
+  * Initial Release
+
+ -- Hubert Denkmair <hubert@denkmair.de>  Sun, 10 Jan 2016 21:00:00 +0200
+

+ 1 - 0
src/tool/adc_cantool/debian/compat

@@ -0,0 +1 @@
+9

+ 17 - 0
src/tool/adc_cantool/debian/control

@@ -0,0 +1,17 @@
+Source: cangaroo
+Section: unknown
+Priority: optional
+Maintainer: Hubert Denkmair <hubert@denkmair.de>
+Build-Depends: debhelper (>= 9), qtbase5-dev, qtbase5-dev-tools, qt5-qmake
+Standards-Version: 3.9.6
+Homepage: https://cangaroo.org/
+
+Package: cangaroo
+Architecture: any
+Depends: libqt5gui5, libqt5widgets5, libqt5core5a, libqt5xml5, ${misc:Depends}, ${shlibs:Depends}
+Description: Qt-based CAN Bus Analyzer
+ cangaroo is a open source CAN Bus Analyzer based on Qt5 and C++.
+ It allows for monitoring multiple SocketCAN interfaces, load DBC files,
+ analyze the CAN Traffic in different views and much more.
+ It is licensed under GPLv2. See README.md file for more information.
+

+ 29 - 0
src/tool/adc_cantool/debian/copyright

@@ -0,0 +1,29 @@
+Format: http://www.debian.org/doc/packaging-manuals/copyright-format/1.0/
+Upstream-Name: cangaroo
+Source: https://github.com/HubertD/cangaroo
+
+Files: *
+Copyright: 2015-2016 Hubert Denkmair <hubert@denkmair.de>
+License: GPL-2.0+
+
+Files: debian/*
+Copyright: 2015-2016 Hubert Denkmair <hubert@denkmair.de>
+License: GPL-2.0+
+
+License: GPL-2.0+
+
+ This program is free software: you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation, either version 2 of the License, or
+ (at your option) any later version.
+ .
+ This package 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 General Public License for more details.
+ .
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see <http://www.gnu.org/licenses/>.
+ .
+ On Debian systems, the complete text of the GNU General
+ Public License version 2 can be found in "/usr/share/common-licenses/GPL-2".

+ 2 - 0
src/tool/adc_cantool/debian/dirs

@@ -0,0 +1,2 @@
+usr/bin
+usr/share/applications

+ 6 - 0
src/tool/adc_cantool/debian/rules

@@ -0,0 +1,6 @@
+#!/usr/bin/make -f
+export QT_SELECT := qt5
+
+%:
+	dh $@ 
+

+ 1 - 0
src/tool/adc_cantool/debian/source/format

@@ -0,0 +1 @@
+3.0 (quilt)

BIN
src/tool/adc_cantool/docs/can_signal_byteorder.ods


+ 13 - 0
src/tool/adc_cantool/driver/CANBlastDriver/CANBlastDriver.pri

@@ -0,0 +1,13 @@
+CONFIG += c++11
+QT += network
+
+SOURCES += \
+    $$PWD/CANBlasterDriver.cpp \
+    $$PWD/CANBlasterInterface.cpp
+
+HEADERS  += \
+    $$PWD/CANBlasterDriver.h \
+    $$PWD/CANBlasterInterface.h
+
+FORMS +=
+

+ 140 - 0
src/tool/adc_cantool/driver/CANBlastDriver/CANBlasterDriver.cpp

@@ -0,0 +1,140 @@
+/*
+
+  Copyright (c) 2022 Ethan Zonca
+
+  This file is part of cangaroo.
+
+  cangaroo is free software: you can redistribute it and/or modify
+  it under the terms of the GNU General Public License as published by
+  the Free Software Foundation, either version 2 of the License, or
+  (at your option) any later version.
+
+  cangaroo 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 General Public License for more details.
+
+  You should have received a copy of the GNU General Public License
+  along with cangaroo.  If not, see <http://www.gnu.org/licenses/>.
+
+*/
+
+
+#include "CANBlasterDriver.h"
+#include "CANBlasterInterface.h"
+#include <core/Backend.h>
+#include <driver/GenericCanSetupPage.h>
+
+#include <errno.h>
+#include <cstring>
+#include <stdio.h>
+#include <unistd.h>
+#include <string.h>
+
+#include <QCoreApplication>
+#include <QDebug>
+#include <QtNetwork/QUdpSocket>
+#include <QNetworkDatagram>
+#include <QJsonDocument>
+#include <QJsonObject>
+#include <QList>
+
+CANBlasterDriver::CANBlasterDriver(Backend &backend)
+  : CanDriver(backend),
+    setupPage(new GenericCanSetupPage())
+{
+    QObject::connect(&backend, SIGNAL(onSetupDialogCreated(SetupDialog&)), setupPage, SLOT(onSetupDialogCreated(SetupDialog&)));
+}
+
+CANBlasterDriver::~CANBlasterDriver() {
+}
+
+bool CANBlasterDriver::update() {
+
+    deleteAllInterfaces();
+
+    // TODO: Listen for multicast packets for discovery of canblaster servers
+    QUdpSocket udpSocket;
+    QHostAddress groupAddress;
+    groupAddress.setAddress("239.255.43.21");
+    udpSocket.bind(QHostAddress::AnyIPv4, 20000, QUdpSocket::ShareAddress);
+    udpSocket.joinMulticastGroup(groupAddress);
+
+    // Record start time
+    struct timeval start_time;
+    gettimeofday(&start_time,NULL);
+
+    fprintf(stderr, "CANblaster: start listen\r\n");
+
+    QList<QString> detected_servers;
+
+    while(1)
+    {
+        while (udpSocket.hasPendingDatagrams())
+        {
+            QNetworkDatagram res = udpSocket.receiveDatagram(1024);
+            if(res.isValid())
+            {
+                QByteArray asd = res.data();
+                qDebug() << asd;
+                QJsonDocument document = QJsonDocument::fromJson(res.data());
+                QJsonObject rootObj = document.object();
+
+
+                if(rootObj.length() == 2 &&
+                   rootObj["protocol"].toString() == "CANblaster" &&
+                   rootObj["version"].toInt() == 1)
+                {
+                    if(!detected_servers.contains(res.senderAddress().toString()))
+                        detected_servers.append(res.senderAddress().toString());
+                }
+                else
+                {
+                    fprintf(stderr, "Invalid CANblaster server. Protocol: %s  Version: %d \r\n", rootObj["protocol"].toString().toStdString().c_str(), rootObj["version"].toInt());
+                }
+            }
+        }
+
+        struct timeval tv;
+        gettimeofday(&tv,NULL);
+
+        // Iterate until timer expires
+        if(tv.tv_sec - start_time.tv_sec > 2)
+            break;
+    }
+    fprintf(stderr, "CANblaster: stop listen\r\n");
+
+    fprintf(stderr, "Found %d servers: \r\n", detected_servers.length());
+    int interface_cnt = 0;
+
+    for(QString server: detected_servers)
+    {
+        fprintf(stderr, "  - %s\r\n", server.toStdString().c_str());
+        createOrUpdateInterface(interface_cnt++, server, false);
+    }
+
+
+    return true;
+}
+
+QString CANBlasterDriver::getName() {
+    return "CANblaster";
+}
+
+
+
+CANBlasterInterface *CANBlasterDriver::createOrUpdateInterface(int index, QString name, bool fd_support) {
+
+    foreach (CanInterface *intf, getInterfaces()) {
+        CANBlasterInterface *scif = dynamic_cast<CANBlasterInterface*>(intf);
+		if (scif->getIfIndex() == index) {
+			scif->setName(name);
+            return scif;
+		}
+	}
+
+
+    CANBlasterInterface *scif = new CANBlasterInterface(this, index, name, fd_support);
+    addInterface(scif);
+    return scif;
+}

+ 43 - 0
src/tool/adc_cantool/driver/CANBlastDriver/CANBlasterDriver.h

@@ -0,0 +1,43 @@
+/*
+
+  Copyright (c) 2015, 2016 Hubert Denkmair
+
+  This file is part of cangaroo.
+
+  cangaroo is free software: you can redistribute it and/or modify
+  it under the terms of the GNU General Public License as published by
+  the Free Software Foundation, either version 2 of the License, or
+  (at your option) any later version.
+
+  cangaroo 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 General Public License for more details.
+
+  You should have received a copy of the GNU General Public License
+  along with cangaroo.  If not, see <http://www.gnu.org/licenses/>.
+
+*/
+
+#pragma once
+
+#include <QString>
+#include <core/Backend.h>
+#include <driver/CanDriver.h>
+
+class CANBlasterInterface;
+class SetupDialogInterfacePage;
+class GenericCanSetupPage;
+
+class CANBlasterDriver: public CanDriver {
+public:
+    CANBlasterDriver(Backend &backend);
+    virtual ~CANBlasterDriver();
+
+    virtual QString getName();
+    virtual bool update();
+
+private:
+    CANBlasterInterface *createOrUpdateInterface(int index, QString name, bool fd_support);
+    GenericCanSetupPage *setupPage;
+};

+ 314 - 0
src/tool/adc_cantool/driver/CANBlastDriver/CANBlasterInterface.cpp

@@ -0,0 +1,314 @@
+/*
+
+  Copyright (c) 2022 Ethan Zonca
+
+  This file is part of cangaroo.
+
+  cangaroo is free software: you can redistribute it and/or modify
+  it under the terms of the GNU General Public License as published by
+  the Free Software Foundation, either version 2 of the License, or
+  (at your option) any later version.
+
+  cangaroo 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 General Public License for more details.
+
+  You should have received a copy of the GNU General Public License
+  along with cangaroo.  If not, see <http://www.gnu.org/licenses/>.
+
+*/
+
+#include "CANBlasterInterface.h"
+
+#include <core/Backend.h>
+#include <core/MeasurementInterface.h>
+#include <core/CanMessage.h>
+
+#include <stdio.h>
+#include <unistd.h>
+#include <time.h>
+#include <fcntl.h>
+#include <QString>
+#include <QStringList>
+#include <QProcess>
+#include <QThread>
+#include <QTimer>
+#include <QNetworkDatagram>
+
+
+CANBlasterInterface::CANBlasterInterface(CANBlasterDriver *driver, int index, QString name, bool fd_support)
+  : CanInterface((CanDriver *)driver),
+	_idx(index),
+    _isOpen(false),
+    _name(name),
+    _ts_mode(ts_mode_SIOCSHWTSTAMP),
+    _socket(NULL)
+{
+    // Set defaults
+    _settings.setBitrate(500000);
+    _settings.setSamplePoint(875);
+
+    _config.supports_canfd = fd_support;
+
+    // Record start time
+    gettimeofday(&_heartbeat_time,NULL);
+}
+
+CANBlasterInterface::~CANBlasterInterface() {
+}
+
+QString CANBlasterInterface::getDetailsStr() const {
+    if(_config.supports_canfd)
+    {
+        return "CANBlaster client with CANFD support";
+    }
+    else
+    {
+        return "CANBlaster client with standard CAN support";
+    }
+}
+
+QString CANBlasterInterface::getName() const {
+	return _name;
+}
+
+void CANBlasterInterface::setName(QString name) {
+    _name = name;
+}
+
+QList<CanTiming> CANBlasterInterface::getAvailableBitrates()
+{
+    QList<CanTiming> retval;
+    QList<unsigned> bitrates({10000, 20000, 50000, 83333, 100000, 125000, 250000, 500000, 800000, 1000000});
+    QList<unsigned> bitrates_fd({0, 2000000, 5000000});
+
+    QList<unsigned> samplePoints({875});
+
+    unsigned i=0;
+    foreach (unsigned br, bitrates) {
+        foreach(unsigned br_fd, bitrates_fd) {
+            foreach (unsigned sp, samplePoints) {
+                retval << CanTiming(i++, br, br_fd, sp);
+            }
+        }
+    }
+
+    return retval;
+}
+
+
+void CANBlasterInterface::applyConfig(const MeasurementInterface &mi)
+{
+    // Save settings for port configuration
+    _settings = mi;
+}
+
+
+
+bool CANBlasterInterface::supportsTimingConfiguration()
+{
+    return _config.supports_timing;
+}
+
+bool CANBlasterInterface::supportsCanFD()
+{
+    return _config.supports_canfd;
+}
+
+bool CANBlasterInterface::supportsTripleSampling()
+{
+    return false;
+}
+
+unsigned CANBlasterInterface::getBitrate()
+{
+    return 0;
+}
+
+uint32_t CANBlasterInterface::getCapabilities()
+{
+    uint32_t retval =
+        CanInterface::capability_config_os |
+        CanInterface::capability_listen_only |
+        CanInterface::capability_auto_restart;
+
+    if (supportsCanFD()) {
+        retval |= CanInterface::capability_canfd;
+    }
+
+    if (supportsTripleSampling()) {
+        retval |= CanInterface::capability_triple_sampling;
+    }
+
+    return retval;
+}
+
+bool CANBlasterInterface::updateStatistics()
+{
+    return false;
+}
+
+uint32_t CANBlasterInterface::getState()
+{
+    if(_isOpen)
+        return state_ok;
+    else
+        return state_bus_off;
+}
+
+int CANBlasterInterface::getNumRxFrames()
+{
+    return _status.rx_count;
+}
+
+int CANBlasterInterface::getNumRxErrors()
+{
+    return _status.rx_errors;
+}
+
+int CANBlasterInterface::getNumTxFrames()
+{
+    return _status.tx_count;
+}
+
+int CANBlasterInterface::getNumTxErrors()
+{
+    return _status.tx_errors;
+}
+
+int CANBlasterInterface::getNumRxOverruns()
+{
+    return _status.rx_overruns;
+}
+
+int CANBlasterInterface::getNumTxDropped()
+{
+    return _status.tx_dropped;
+}
+
+int CANBlasterInterface::getIfIndex() {
+    return _idx;
+}
+
+const char *CANBlasterInterface::cname()
+{
+    return _name.toStdString().c_str();
+}
+
+void CANBlasterInterface::open()
+{
+
+    // Start off with a fresh socket
+    if(_socket != NULL)
+    {
+        delete _socket;
+    }
+    _socket = new QUdpSocket();
+
+    if(_socket->bind(QHostAddress::AnyIPv4, 20001))
+    {
+        _isOpen = true;
+    }
+    else
+    {
+        perror("CANBlaster Bind Failed!");
+        _isOpen = false;
+    }
+}
+
+void CANBlasterInterface::close()
+{
+    if(_socket != NULL && _socket->isOpen())
+    {
+        _socket->close();
+    }
+    _isOpen = false;
+}
+
+bool CANBlasterInterface::isOpen()
+{
+    return _isOpen;
+}
+
+void CANBlasterInterface::sendMessage(const CanMessage &msg) {
+
+}
+
+bool CANBlasterInterface::readMessage(CanMessage &msg, unsigned int timeout_ms)
+{
+    // Don't saturate the thread
+    QThread().usleep(250);
+
+    // Record start time
+    struct timeval now;
+    gettimeofday(&now,NULL);
+
+    if(now.tv_sec - _heartbeat_time.tv_sec > 1)
+    {
+
+        _heartbeat_time.tv_sec = now.tv_sec;
+
+        if(_isOpen)
+        {
+            QByteArray Data;
+            Data.append("Heartbeat");
+            _socket->writeDatagram(Data, QHostAddress(getName()), 20002);
+        }
+    }
+
+    // NOTE: This only works with standard CAN frames right now!
+
+    // Process all pending datagrams
+    // TODO: Could switch to if from while but the caller expects 1 can frame
+    if (_isOpen && _socket->hasPendingDatagrams())
+    {
+        can_frame frame;
+        QHostAddress address;
+        quint16 port;
+        int res = _socket->readDatagram((char*)&frame, sizeof(can_frame), &address, &port);
+
+        // TODO: Read all bytes... to CANFD_MTU max
+//        if (nbytes == CANFD_MTU) {
+//                printf("got CAN FD frame with length %d\n", cfd.len);
+//                /* cfd.flags contains valid data */
+//        } else if (nbytes == CAN_MTU) {
+//                printf("got Classical CAN frame with length %d\n", cfd.len);
+//                /* cfd.flags is undefined */
+//        } else {
+//                fprintf(stderr, "read: invalid CAN(FD) frame\n");
+//                return 1;
+//        }
+
+        if(res > 0)
+        {
+            // Set timestamp to current time
+            struct timeval tv;
+            gettimeofday(&tv,NULL);
+            msg.setTimestamp(tv);
+
+            msg.setInterfaceId(getId());
+            msg.setId(frame.can_id & CAN_ERR_MASK);
+            msg.setBRS(false);
+            msg.setErrorFrame(frame.can_id & CAN_ERR_FLAG);
+            msg.setExtended(frame.can_id & CAN_EFF_FLAG);
+            msg.setRTR(frame.can_id & CAN_RTR_FLAG);
+            msg.setLength(frame.len);
+
+
+            for(int i=0; i<frame.len && i<CAN_MAX_DLEN; i++)
+            {
+                msg.setDataAt(i, frame.data[i]);
+            }
+
+            return true;
+        }
+        else
+        {
+            return false;
+        }
+    }
+    return false;
+
+}
+

+ 237 - 0
src/tool/adc_cantool/driver/CANBlastDriver/CANBlasterInterface.h

@@ -0,0 +1,237 @@
+/*
+
+  Copyright (c) 2015, 2016 Hubert Denkmair
+
+  This file is part of cangaroo.
+
+  cangaroo is free software: you can redistribute it and/or modify
+  it under the terms of the GNU General Public License as published by
+  the Free Software Foundation, either version 2 of the License, or
+  (at your option) any later version.
+
+  cangaroo 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 General Public License for more details.
+
+  You should have received a copy of the GNU General Public License
+  along with cangaroo.  If not, see <http://www.gnu.org/licenses/>.
+
+*/
+
+#pragma once
+
+#include "../CanInterface.h"
+#include <core/MeasurementInterface.h>
+#include <QtNetwork/QUdpSocket>
+#include <QTimer>
+
+class CANBlasterDriver;
+
+typedef struct {
+    bool supports_canfd;
+    bool supports_timing;
+    uint32_t state;
+    uint32_t base_freq;
+    uint32_t sample_point;
+    uint32_t ctrl_mode;
+    uint32_t restart_ms;
+} can_config_t;
+
+typedef struct {
+    uint32_t can_state;
+
+    uint64_t rx_count;
+    int rx_errors;
+    uint64_t rx_overruns;
+
+    uint64_t tx_count;
+    int tx_errors;
+    uint64_t tx_dropped;
+} can_status_t;
+
+class CANBlasterInterface: public CanInterface {
+public:
+    CANBlasterInterface(CANBlasterDriver *driver, int index, QString name, bool fd_support);
+    virtual ~CANBlasterInterface();
+
+    QString getDetailsStr() const;
+    virtual QString getName() const;
+    void setName(QString name);
+
+    virtual QList<CanTiming> getAvailableBitrates();
+
+    virtual void applyConfig(const MeasurementInterface &mi);
+
+    bool supportsTimingConfiguration();
+    bool supportsCanFD();
+    bool supportsTripleSampling();
+
+    virtual unsigned getBitrate();
+    virtual uint32_t getCapabilities();
+
+
+	virtual void open();
+    virtual void close();
+    virtual bool isOpen();
+
+    virtual void sendMessage(const CanMessage &msg);
+    virtual bool readMessage(CanMessage &msg, unsigned int timeout_ms);
+
+    virtual bool updateStatistics();
+    virtual uint32_t getState();
+    virtual int getNumRxFrames();
+    virtual int getNumRxErrors();
+    virtual int getNumRxOverruns();
+
+    virtual int getNumTxFrames();
+    virtual int getNumTxErrors();
+    virtual int getNumTxDropped();
+
+
+    int getIfIndex();
+
+private:
+    typedef enum {
+        ts_mode_SIOCSHWTSTAMP,
+        ts_mode_SIOCGSTAMPNS,
+        ts_mode_SIOCGSTAMP
+    } ts_mode_t;
+
+    int _idx;
+    bool _isOpen;
+    QString _name;
+
+    MeasurementInterface _settings;
+
+    can_config_t _config;
+    can_status_t _status;
+    ts_mode_t _ts_mode;
+
+    struct timeval _heartbeat_time;
+    QUdpSocket* _socket;
+    const char *cname();
+
+};
+
+
+/* Duplicated from Linux can.h for compilation on Windows. Not a great solution. */
+
+
+/* special address description flags for the CAN_ID */
+#define CAN_EFF_FLAG 0x80000000U /* EFF/SFF is set in the MSB */
+#define CAN_RTR_FLAG 0x40000000U /* remote transmission request */
+#define CAN_ERR_FLAG 0x20000000U /* error message frame */
+
+/* valid bits in CAN ID for frame formats */
+#define CAN_SFF_MASK 0x000007FFU /* standard frame format (SFF) */
+#define CAN_EFF_MASK 0x1FFFFFFFU /* extended frame format (EFF) */
+#define CAN_ERR_MASK 0x1FFFFFFFU /* omit EFF, RTR, ERR flags */
+
+/*
+ * Controller Area Network Identifier structure
+ *
+ * bit 0-28	: CAN identifier (11/29 bit)
+ * bit 29	: error message frame flag (0 = data frame, 1 = error message)
+ * bit 30	: remote transmission request flag (1 = rtr frame)
+ * bit 31	: frame format flag (0 = standard 11 bit, 1 = extended 29 bit)
+ */
+typedef uint32_t canid_t;
+
+#define CAN_SFF_ID_BITS		11
+#define CAN_EFF_ID_BITS		29
+
+/*
+ * Controller Area Network Error Message Frame Mask structure
+ *
+ * bit 0-28	: error class mask (see include/uapi/linux/can/error.h)
+ * bit 29-31	: set to zero
+ */
+typedef uint32_t can_err_mask_t;
+
+/* CAN payload length and DLC definitions according to ISO 11898-1 */
+#define CAN_MAX_DLC 8
+#define CAN_MAX_RAW_DLC 15
+#define CAN_MAX_DLEN 8
+
+/* CAN FD payload length and DLC definitions according to ISO 11898-7 */
+#define CANFD_MAX_DLC 15
+#define CANFD_MAX_DLEN 64
+
+/**
+ * struct can_frame - Classical CAN frame structure (aka CAN 2.0B)
+ * @can_id:   CAN ID of the frame and CAN_*_FLAG flags, see canid_t definition
+ * @len:      CAN frame payload length in byte (0 .. 8)
+ * @can_dlc:  deprecated name for CAN frame payload length in byte (0 .. 8)
+ * @__pad:    padding
+ * @__res0:   reserved / padding
+ * @len8_dlc: optional DLC value (9 .. 15) at 8 byte payload length
+ *            len8_dlc contains values from 9 .. 15 when the payload length is
+ *            8 bytes but the DLC value (see ISO 11898-1) is greater then 8.
+ *            CAN_CTRLMODE_CC_LEN8_DLC flag has to be enabled in CAN driver.
+ * @data:     CAN frame payload (up to 8 byte)
+ */
+struct can_frame {
+    canid_t can_id;  /* 32 bit CAN_ID + EFF/RTR/ERR flags */
+    union {
+        /* CAN frame payload length in byte (0 .. CAN_MAX_DLEN)
+         * was previously named can_dlc so we need to carry that
+         * name for legacy support
+         */
+        uint8_t len;
+        uint8_t can_dlc; /* deprecated */
+    } __attribute__((packed)); /* disable padding added in some ABIs */
+    uint8_t __pad; /* padding */
+    uint8_t __res0; /* reserved / padding */
+    uint8_t len8_dlc; /* optional DLC for 8 byte payload length (9 .. 15) */
+    uint8_t data[CAN_MAX_DLEN] __attribute__((aligned(8)));
+};
+
+/*
+ * defined bits for canfd_frame.flags
+ *
+ * The use of struct canfd_frame implies the FD Frame (FDF) bit to
+ * be set in the CAN frame bitstream on the wire. The FDF bit switch turns
+ * the CAN controllers bitstream processor into the CAN FD mode which creates
+ * two new options within the CAN FD frame specification:
+ *
+ * Bit Rate Switch - to indicate a second bitrate is/was used for the payload
+ * Error State Indicator - represents the error state of the transmitting node
+ *
+ * As the CANFD_ESI bit is internally generated by the transmitting CAN
+ * controller only the CANFD_BRS bit is relevant for real CAN controllers when
+ * building a CAN FD frame for transmission. Setting the CANFD_ESI bit can make
+ * sense for virtual CAN interfaces to test applications with echoed frames.
+ *
+ * The struct can_frame and struct canfd_frame intentionally share the same
+ * layout to be able to write CAN frame content into a CAN FD frame structure.
+ * When this is done the former differentiation via CAN_MTU / CANFD_MTU gets
+ * lost. CANFD_FDF allows programmers to mark CAN FD frames in the case of
+ * using struct canfd_frame for mixed CAN / CAN FD content (dual use).
+ * N.B. the Kernel APIs do NOT provide mixed CAN / CAN FD content inside of
+ * struct canfd_frame therefore the CANFD_FDF flag is disregarded by Linux.
+ */
+#define CANFD_BRS 0x01 /* bit rate switch (second bitrate for payload data) */
+#define CANFD_ESI 0x02 /* error state indicator of the transmitting node */
+#define CANFD_FDF 0x04 /* mark CAN FD for dual use of struct canfd_frame */
+
+/**
+ * struct canfd_frame - CAN flexible data rate frame structure
+ * @can_id: CAN ID of the frame and CAN_*_FLAG flags, see canid_t definition
+ * @len:    frame payload length in byte (0 .. CANFD_MAX_DLEN)
+ * @flags:  additional flags for CAN FD
+ * @__res0: reserved / padding
+ * @__res1: reserved / padding
+ * @data:   CAN FD frame payload (up to CANFD_MAX_DLEN byte)
+ */
+struct canfd_frame {
+    canid_t can_id;  /* 32 bit CAN_ID + EFF/RTR/ERR flags */
+    uint8_t    len;     /* frame payload length in byte */
+    uint8_t    flags;   /* additional flags for CAN FD */
+    uint8_t    __res0;  /* reserved / padding */
+    uint8_t    __res1;  /* reserved / padding */
+    uint8_t    data[CANFD_MAX_DLEN] __attribute__((aligned(8)));
+};
+
+#define CAN_MTU		(sizeof(struct can_frame))
+#define CANFD_MTU	(sizeof(struct canfd_frame))

+ 107 - 0
src/tool/adc_cantool/driver/CanDriver.cpp

@@ -0,0 +1,107 @@
+/*
+
+  Copyright (c) 2015, 2016 Hubert Denkmair <hubert@denkmair.de>
+
+  This file is part of cangaroo.
+
+  cangaroo is free software: you can redistribute it and/or modify
+  it under the terms of the GNU General Public License as published by
+  the Free Software Foundation, either version 2 of the License, or
+  (at your option) any later version.
+
+  cangaroo 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 General Public License for more details.
+
+  You should have received a copy of the GNU General Public License
+  along with cangaroo.  If not, see <http://www.gnu.org/licenses/>.
+
+*/
+
+#include "CanDriver.h"
+#include <core/Backend.h>
+#include <driver/CanInterface.h>
+
+CanDriver::CanDriver(Backend &backend)
+  : _backend(backend)
+{
+    setId(0);
+}
+
+CanDriver::~CanDriver() {
+    // TODO Auto-generated destructor stub
+}
+
+Backend &CanDriver::backend() const
+{
+    return _backend;
+}
+
+void CanDriver::init(int id)
+{
+    setId(id);
+}
+
+void CanDriver::setId(int id)
+{
+    _id = id;
+}
+
+int CanDriver::id()
+{
+    return _id;
+}
+
+QList<CanInterface *> CanDriver::getInterfaces() const
+{
+    return _interfaces;
+}
+
+CanInterfaceIdList CanDriver::getInterfaceIds() const
+{
+    CanInterfaceIdList retval;
+    foreach (CanInterface *intf, _interfaces) {
+        retval.push_back(intf->getId());
+    }
+    return retval;
+}
+
+CanInterface *CanDriver::getInterfaceById(CanInterfaceId id)
+{
+    return _interfaces.value(id & 0xFF);
+}
+
+CanInterfaceId CanDriver::addInterface(CanInterface *intf)
+{
+    intf->setId((id()<<8) | _interfaces.size());
+    _interfaces.push_back(intf);
+    return intf->getId();
+}
+
+void CanDriver::deleteInterface(CanInterface *intf)
+{
+    delete intf;
+    _interfaces.removeOne(intf);
+}
+
+void CanDriver::deleteAllInterfaces()
+{
+    for(CanInterface* interface: _interfaces)
+    {
+        delete interface;
+        _interfaces.removeOne(interface);
+    }
+}
+
+
+
+CanInterface *CanDriver::getInterfaceByName(QString ifName)
+{
+    foreach (CanInterface *intf, _interfaces) {
+        if (intf->getName() == ifName) {
+            return intf;
+        }
+    }
+    return 0;
+}

+ 63 - 0
src/tool/adc_cantool/driver/CanDriver.h

@@ -0,0 +1,63 @@
+/*
+
+  Copyright (c) 2015, 2016 Hubert Denkmair <hubert@denkmair.de>
+
+  This file is part of cangaroo.
+
+  cangaroo is free software: you can redistribute it and/or modify
+  it under the terms of the GNU General Public License as published by
+  the Free Software Foundation, either version 2 of the License, or
+  (at your option) any later version.
+
+  cangaroo 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 General Public License for more details.
+
+  You should have received a copy of the GNU General Public License
+  along with cangaroo.  If not, see <http://www.gnu.org/licenses/>.
+
+*/
+
+#pragma once
+
+#include <stdint.h>
+#include <QList>
+#include <QString>
+
+class Backend;
+class CanInterface;
+
+typedef uint16_t CanInterfaceId;
+typedef QList<uint16_t> CanInterfaceIdList;
+
+class CanDriver {
+public:
+    CanDriver(Backend &backend);
+    virtual ~CanDriver();
+
+    Backend &backend() const;
+
+    void init(int id);
+
+    int id();
+
+    virtual QString getName() = 0;
+    virtual bool update() = 0;
+
+    CanInterfaceIdList getInterfaceIds() const;
+    QList<CanInterface*> getInterfaces() const;
+    CanInterface *getInterfaceById(CanInterfaceId id);
+    CanInterfaceId addInterface(CanInterface *intf);
+    void deleteInterface(CanInterface *intf);
+    void deleteAllInterfaces();
+
+    CanInterface *getInterfaceByName(QString ifName);
+
+private:
+    Backend &_backend;
+    int _id;
+    QList<CanInterface*> _interfaces;
+
+    void setId(int id);
+};

+ 101 - 0
src/tool/adc_cantool/driver/CanInterface.cpp

@@ -0,0 +1,101 @@
+/*
+
+  Copyright (c) 2015, 2016 Hubert Denkmair <hubert@denkmair.de>
+
+  This file is part of cangaroo.
+
+  cangaroo is free software: you can redistribute it and/or modify
+  it under the terms of the GNU General Public License as published by
+  the Free Software Foundation, either version 2 of the License, or
+  (at your option) any later version.
+
+  cangaroo 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 General Public License for more details.
+
+  You should have received a copy of the GNU General Public License
+  along with cangaroo.  If not, see <http://www.gnu.org/licenses/>.
+
+*/
+
+#include "CanInterface.h"
+
+#include <QList>
+
+CanInterface::CanInterface(CanDriver *driver)
+  : _id(-1), _driver(driver)
+{
+}
+
+CanInterface::~CanInterface() {
+}
+
+CanDriver* CanInterface::getDriver() {
+    return _driver;
+}
+
+QString CanInterface::getDetailsStr() const
+{
+    return "";
+}
+
+uint32_t CanInterface::getCapabilities()
+{
+    return 0;
+}
+
+QList<CanTiming> CanInterface::getAvailableBitrates()
+{
+    QList<CanTiming> retval;
+    retval << CanTiming(0,   10000, 0, 875) \
+           << CanTiming(1,   20000, 0, 875) \
+           << CanTiming(2,   50000, 0, 875) \
+           << CanTiming(3,   83333, 0, 875) \
+           << CanTiming(4,  100000, 0, 875) \
+           << CanTiming(5,  125000, 0, 875) \
+           << CanTiming(6,  250000, 0, 875) \
+           << CanTiming(7,  500000, 0, 875) \
+           << CanTiming(8,  800000, 0, 875) \
+           << CanTiming(9, 1000000, 0, 875);
+    return retval;
+}
+
+void CanInterface::open() {
+}
+
+void CanInterface::close() {
+}
+
+bool CanInterface::isOpen()
+{
+    return false;
+}
+
+bool CanInterface::updateStatistics()
+{
+    return false;
+}
+
+QString CanInterface::getStateText()
+{
+    switch (getState()) {
+        case state_ok: return "ok";
+        case state_warning: return "warning";
+        case state_passive: return "error passive";
+        case state_bus_off: return "bus off";
+        case state_stopped: return "stopped";
+        case state_unknown: return "unknown";
+        default: return "";
+    }
+}
+
+CanInterfaceId CanInterface::getId() const
+{
+    return _id;
+}
+
+void CanInterface::setId(CanInterfaceId id)
+{
+    _id = id;
+}

+ 92 - 0
src/tool/adc_cantool/driver/CanInterface.h

@@ -0,0 +1,92 @@
+/*
+
+  Copyright (c) 2015, 2016 Hubert Denkmair <hubert@denkmair.de>
+
+  This file is part of cangaroo.
+
+  cangaroo is free software: you can redistribute it and/or modify
+  it under the terms of the GNU General Public License as published by
+  the Free Software Foundation, either version 2 of the License, or
+  (at your option) any later version.
+
+  cangaroo 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 General Public License for more details.
+
+  You should have received a copy of the GNU General Public License
+  along with cangaroo.  If not, see <http://www.gnu.org/licenses/>.
+
+*/
+
+#pragma once
+
+#include <QString>
+#include <stdint.h>
+#include "CanDriver.h"
+#include "CanTiming.h"
+#include <QObject>
+
+class CanMessage;
+class MeasurementInterface;
+
+class CanInterface: public QObject  {
+public:
+    enum {
+        state_ok,
+        state_warning,
+        state_passive,
+        state_bus_off,
+        state_stopped,
+        state_unknown
+    };
+
+    enum {
+        capability_canfd           = 0x01,
+        capability_listen_only     = 0x02,
+        capability_triple_sampling = 0x04,
+        capability_one_shot        = 0x08,
+        capability_auto_restart    = 0x10,
+        capability_config_os       = 0x20
+    };
+
+public:
+    CanInterface(CanDriver *driver);
+	virtual ~CanInterface();
+    virtual CanDriver *getDriver();
+    virtual QString getName() const = 0;
+    virtual QString getDetailsStr() const;
+
+    virtual void applyConfig(const MeasurementInterface &mi) = 0;
+
+    virtual unsigned getBitrate() = 0;
+
+    virtual uint32_t getCapabilities();
+    virtual QList<CanTiming> getAvailableBitrates();
+
+	virtual void open();
+	virtual void close();
+
+    virtual bool isOpen();
+
+    virtual void sendMessage(const CanMessage &msg) = 0;
+    virtual bool readMessage(CanMessage &msg, unsigned int timeout_ms) = 0;
+
+    virtual bool updateStatistics();
+    virtual uint32_t getState() = 0;
+    virtual int getNumRxFrames() = 0;
+    virtual int getNumRxErrors() = 0;
+    virtual int getNumTxFrames() = 0;
+    virtual int getNumTxErrors() = 0;
+    virtual int getNumRxOverruns() = 0;
+    virtual int getNumTxDropped() = 0;
+
+    QString getStateText();
+
+    CanInterfaceId getId() const;
+    void setId(CanInterfaceId id);
+
+private:
+    CanInterfaceId _id;
+    CanDriver *_driver;
+};

+ 92 - 0
src/tool/adc_cantool/driver/CanListener.cpp

@@ -0,0 +1,92 @@
+/*
+
+  Copyright (c) 2015, 2016 Hubert Denkmair <hubert@denkmair.de>
+
+  This file is part of cangaroo.
+
+  cangaroo is free software: you can redistribute it and/or modify
+  it under the terms of the GNU General Public License as published by
+  the Free Software Foundation, either version 2 of the License, or
+  (at your option) any later version.
+
+  cangaroo 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 General Public License for more details.
+
+  You should have received a copy of the GNU General Public License
+  along with cangaroo.  If not, see <http://www.gnu.org/licenses/>.
+
+*/
+
+#include "CanListener.h"
+
+#include <QThread>
+
+#include <core/Backend.h>
+#include <core/CanTrace.h>
+#include <core/CanMessage.h>
+#include "CanInterface.h"
+
+CanListener::CanListener(QObject *parent, Backend &backend, CanInterface &intf)
+  : QObject(parent),
+    _backend(backend),
+    _intf(intf),
+    _shouldBeRunning(true),
+    _openComplete(false)
+{
+    _thread = new QThread();
+}
+
+CanListener::~CanListener()
+{
+    delete _thread;
+}
+
+CanInterfaceId CanListener::getInterfaceId()
+{
+    return _intf.getId();
+}
+
+CanInterface &CanListener::getInterface()
+{
+    return _intf;
+}
+
+void CanListener::run()
+{
+    // Note: open and close done from run() so all operations take place in the same thread
+    CanMessage msg;
+    CanTrace *trace = _backend.getTrace();
+    _intf.open();
+    _openComplete = true;
+    while (_shouldBeRunning) {
+        if (_intf.readMessage(msg, 1000)) {
+            trace->enqueueMessage(msg, false);
+        }
+    }
+    _intf.close();
+    _thread->quit();
+}
+
+void CanListener::startThread()
+{
+    moveToThread(_thread);
+    connect(_thread, SIGNAL(started()), this, SLOT(run()));
+    _thread->start();
+
+    // Wait for interface to be open before returning so that beginMeasurement is emitted after interface open
+    while(!_openComplete)
+      QThread().usleep(250);
+}
+
+void CanListener::requestStop()
+{
+    _shouldBeRunning = false;
+}
+
+void CanListener::waitFinish()
+{
+    requestStop();
+    _thread->wait();
+}

+ 60 - 0
src/tool/adc_cantool/driver/CanListener.h

@@ -0,0 +1,60 @@
+/*
+
+  Copyright (c) 2015, 2016 Hubert Denkmair <hubert@denkmair.de>
+
+  This file is part of cangaroo.
+
+  cangaroo is free software: you can redistribute it and/or modify
+  it under the terms of the GNU General Public License as published by
+  the Free Software Foundation, either version 2 of the License, or
+  (at your option) any later version.
+
+  cangaroo 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 General Public License for more details.
+
+  You should have received a copy of the GNU General Public License
+  along with cangaroo.  If not, see <http://www.gnu.org/licenses/>.
+
+*/
+
+#pragma once
+
+#include <QObject>
+#include <driver/CanDriver.h>
+#include <driver/CanInterface.h>
+
+class QThread;
+class CanMessage;
+class Backend;
+
+class CanListener : public QObject
+{
+    Q_OBJECT
+
+public:
+    explicit CanListener(QObject *parent, Backend &backend, CanInterface &intf);
+    virtual ~CanListener();
+
+    CanInterfaceId getInterfaceId();
+    CanInterface &getInterface();
+
+signals:
+    void messageReceived(const CanMessage &msg);
+
+public slots:
+    void run();
+
+    void startThread();
+    void requestStop();
+    void waitFinish();
+
+private:
+    Backend &_backend;
+    CanInterface &_intf;
+    bool _shouldBeRunning;
+    bool _openComplete;
+    QThread *_thread;
+
+};

+ 49 - 0
src/tool/adc_cantool/driver/CanTiming.cpp

@@ -0,0 +1,49 @@
+#include "CanTiming.h"
+
+CanTiming::CanTiming(unsigned id, unsigned bitrate, unsigned bitrate_fd, unsigned samplePoint)
+  : _id(id),
+    _bitrate(bitrate),
+    _bitrate_fd(bitrate_fd),
+    _samplePoint(samplePoint)
+{
+}
+
+unsigned CanTiming::getId()
+{
+    return _id;
+}
+
+unsigned CanTiming::getBitrate()
+{
+    return _bitrate;
+}
+
+unsigned CanTiming::getBitrateFD()
+{
+    return _bitrate_fd;
+}
+
+bool CanTiming::isCanFD()
+{
+    return _bitrate_fd != 0;
+}
+
+unsigned CanTiming::getSamplePoint()
+{
+    return _samplePoint;
+}
+
+QString CanTiming::getSamplePointStr(unsigned samplePoint)
+{
+    return QString::number(samplePoint/10.0, 'f', 1).append("%");
+}
+
+QString CanTiming::getTitle()
+{
+    QString format = isCanFD() ? "%1/%3(FD) kBit/s, sample point %2" : "%1 kBit/s, sample point %2";
+    return format.arg(
+        QString::number(getBitrate()/1000, 'f', 2),
+        getSamplePointStr(getSamplePoint()),
+        QString::number(getBitrateFD()/1000, 'f', 2)
+    );
+}

+ 37 - 0
src/tool/adc_cantool/driver/CanTiming.h

@@ -0,0 +1,37 @@
+#ifndef CANTIMING_H
+#define CANTIMING_H
+
+#include <QString>
+
+class CanTiming
+{
+public:
+    /*!
+     * \brief CanTiming generic CAN timing description
+     * \param id internal id to identify this timing for the corresponding CAN driver
+     * \param bitrate bitrate in bits per second, e.g. 500000
+     * \param bitrate_fd bitrate for the FD part, or zero this is not a CanFD timing
+     * \param samplePoint sample point, e.g. 0.875
+     */
+    CanTiming(unsigned id, unsigned bitrate, unsigned bitrate_fd, unsigned samplePoint);
+    unsigned getId();
+    unsigned getBitrate();
+    unsigned getBitrateFD();
+    bool isCanFD();
+    unsigned getSamplePoint();
+    static QString getSamplePointStr(unsigned samplePoint);
+
+    /*!
+     * \brief getTitle get a brief one-line description of this timing
+     * \return description, e.g. "500kBit/s, sample point 87.5%"
+     */
+    QString getTitle();
+
+private:
+    unsigned _id;
+    unsigned _bitrate;
+    unsigned _bitrate_fd;
+    unsigned _samplePoint;
+};
+
+#endif // CANTIMING_H

+ 80 - 0
src/tool/adc_cantool/driver/CandleApiDriver/CandleApiDriver.cpp

@@ -0,0 +1,80 @@
+/*
+
+  Copyright (c) 2016 Hubert Denkmair <hubert@denkmair.de>
+
+  This file is part of cangaroo.
+
+  cangaroo is free software: you can redistribute it and/or modify
+  it under the terms of the GNU General Public License as published by
+  the Free Software Foundation, either version 2 of the License, or
+  (at your option) any later version.
+
+  cangaroo 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 General Public License for more details.
+
+  You should have received a copy of the GNU General Public License
+  along with cangaroo.  If not, see <http://www.gnu.org/licenses/>.
+
+*/
+
+#include "CandleApiDriver.h"
+#include "api/candle.h"
+
+#include "CandleApiInterface.h"
+#include <driver/GenericCanSetupPage.h>
+
+CandleApiDriver::CandleApiDriver(Backend &backend)
+  : CanDriver(backend),
+    setupPage(new GenericCanSetupPage(0))
+{
+    QObject::connect(&backend, SIGNAL(onSetupDialogCreated(SetupDialog&)), setupPage, SLOT(onSetupDialogCreated(SetupDialog&)));
+}
+
+QString CandleApiDriver::getName()
+{
+    return "CandleAPI";
+}
+
+bool CandleApiDriver::update()
+{
+    deleteAllInterfaces();
+
+    candle_list_handle clist;
+    uint8_t num_interfaces;
+    candle_handle dev;
+
+    if (candle_list_scan(&clist)) {
+        if (candle_list_length(clist, &num_interfaces)) {
+            for (uint8_t i=0; i<num_interfaces; i++) {
+                if (candle_dev_get(clist, i, &dev)) {
+                    CandleApiInterface *cif = findInterface(dev);
+
+                    if (cif == NULL) {
+                        cif = new CandleApiInterface(this, dev);
+                        addInterface(cif);
+                    } else {
+                        cif->update(dev);
+                    }
+
+                }
+
+            }
+        }
+        candle_list_free(clist);
+    }
+
+    return true;
+}
+
+CandleApiInterface *CandleApiDriver::findInterface(candle_handle dev)
+{
+    foreach (CanInterface *intf, getInterfaces()) {
+        CandleApiInterface *cif = dynamic_cast<CandleApiInterface*>(intf);
+        if (cif->getPath() == std::wstring(candle_dev_get_path(dev))) {
+            return cif;
+        }
+    }
+    return NULL;
+}

+ 44 - 0
src/tool/adc_cantool/driver/CandleApiDriver/CandleApiDriver.h

@@ -0,0 +1,44 @@
+/*
+
+  Copyright (c) 2016 Hubert Denkmair <hubert@denkmair.de>
+
+  This file is part of cangaroo.
+
+  cangaroo is free software: you can redistribute it and/or modify
+  it under the terms of the GNU General Public License as published by
+  the Free Software Foundation, either version 2 of the License, or
+  (at your option) any later version.
+
+  cangaroo 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 General Public License for more details.
+
+  You should have received a copy of the GNU General Public License
+  along with cangaroo.  If not, see <http://www.gnu.org/licenses/>.
+
+*/
+
+#pragma once
+
+#include <core/Backend.h>
+#include <driver/CanDriver.h>
+#include "api/candle.h"
+
+class CandleApiInterface;
+class GenericCanSetupPage;
+
+class CandleApiDriver : public CanDriver
+{
+public:
+    CandleApiDriver(Backend &backend);
+
+    virtual QString getName();
+    virtual bool update();
+
+private:
+    GenericCanSetupPage *setupPage;
+    CandleApiInterface *findInterface(candle_handle dev);
+
+};
+

+ 24 - 0
src/tool/adc_cantool/driver/CandleApiDriver/CandleApiDriver.pri

@@ -0,0 +1,24 @@
+QMAKE_CFLAGS += -std=gnu11
+
+SOURCES += \
+    $$PWD/CandleApiDriver.cpp \
+    $$PWD/CandleApiInterface.cpp \
+    $$PWD/api/candle.c \
+    $$PWD/api/candle_ctrl_req.c \
+    $$PWD/CandleApiTiming.cpp
+
+#win32:LIBS += -L$$PWD/api/ -lcandle_api
+win32: LIBS += -lSetupApi
+win32: LIBS += -lOle32
+win32: LIBS += -lwinusb
+
+HEADERS += \
+    $$PWD/api/candle.h \
+    $$PWD/CandleApiDriver.h \
+    $$PWD/CandleApiInterface.h \
+    $$PWD/api/candle_ctrl_req.h \
+    $$PWD/api/candle_defs.h \
+    $$PWD/api/ch_9.h \
+    $$PWD/CandleApiTiming.h
+
+FORMS +=

+ 390 - 0
src/tool/adc_cantool/driver/CandleApiDriver/CandleApiInterface.cpp

@@ -0,0 +1,390 @@
+#include "CandleApiDriver.h"
+#include "CandleApiInterface.h"
+
+CandleApiInterface::CandleApiInterface(CandleApiDriver *driver, candle_handle handle)
+  : CanInterface(driver),
+    _hostOffsetStart(0),
+    _deviceTicksStart(0),
+    _handle(handle),
+    _backend(driver->backend()),
+    _numRx(0),
+    _numTx(0),
+    _numTxErr(0)
+{
+    _settings.setBitrate(500000);
+    _settings.setSamplePoint(875);
+
+
+
+    // Timings for 170MHz processors (CANable 2.0)
+    // Tseg1: 2..256 Tseg2: 2..128 sjw: 1..128 brp: 1..512
+    // Note: as expressed below, Tseg1 does not include 1 count for prop phase
+    _timings
+        << CandleApiTiming(170000000,   10000, 875, 68, 217, 31)
+        << CandleApiTiming(170000000,   20000, 875, 34, 217, 31)
+        << CandleApiTiming(170000000,   50000, 875, 17, 173, 25)
+        << CandleApiTiming(170000000,   83333, 875,  8, 221, 32)
+        << CandleApiTiming(170000000,  100000, 875, 10, 147, 21)
+        << CandleApiTiming(170000000,  125000, 875, 8,  147, 21)
+        << CandleApiTiming(170000000,  250000, 875, 4,  147, 21)
+        << CandleApiTiming(170000000,  500000, 875, 2,  147, 21)
+        << CandleApiTiming(170000000, 1000000, 875, 1,  147, 21);
+
+
+    // Timings for 48MHz processors (CANable 0.X)
+    _timings
+        // sample point: 50.0%
+        << CandleApiTiming(48000000,   10000, 500, 300, 6, 8)
+        << CandleApiTiming(48000000,   20000, 500, 150, 6, 8)
+        << CandleApiTiming(48000000,   50000, 500,  60, 6, 8)
+        << CandleApiTiming(48000000,   83333, 500,  36, 6, 8)
+        << CandleApiTiming(48000000,  100000, 500,  30, 6, 8)
+        << CandleApiTiming(48000000,  125000, 500,  24, 6, 8)
+        << CandleApiTiming(48000000,  250000, 500,  12, 6, 8)
+        << CandleApiTiming(48000000,  500000, 500,   6, 6, 8)
+        << CandleApiTiming(48000000,  800000, 500,   3, 8, 9)
+        << CandleApiTiming(48000000, 1000000, 500,   3, 6, 8)
+
+        // sample point: 62.5%
+        << CandleApiTiming(48000000,   10000, 625, 300, 8, 6)
+        << CandleApiTiming(48000000,   20000, 625, 150, 8, 6)
+        << CandleApiTiming(48000000,   50000, 625,  60, 8, 6)
+        << CandleApiTiming(48000000,   83333, 625,  36, 8, 6)
+        << CandleApiTiming(48000000,  100000, 625,  30, 8, 6)
+        << CandleApiTiming(48000000,  125000, 625,  24, 8, 6)
+        << CandleApiTiming(48000000,  250000, 625,  12, 8, 6)
+        << CandleApiTiming(48000000,  500000, 625,   6, 8, 6)
+        << CandleApiTiming(48000000,  800000, 600,   4, 7, 6)
+        << CandleApiTiming(48000000, 1000000, 625,   3, 8, 6)
+
+        // sample point: 75.0%
+        << CandleApiTiming(48000000,   10000, 750, 300, 10, 4)
+        << CandleApiTiming(48000000,   20000, 750, 150, 10, 4)
+        << CandleApiTiming(48000000,   50000, 750,  60, 10, 4)
+        << CandleApiTiming(48000000,   83333, 750,  36, 10, 4)
+        << CandleApiTiming(48000000,  100000, 750,  30, 10, 4)
+        << CandleApiTiming(48000000,  125000, 750,  24, 10, 4)
+        << CandleApiTiming(48000000,  250000, 750,  12, 10, 4)
+        << CandleApiTiming(48000000,  500000, 750,   6, 10, 4)
+        << CandleApiTiming(48000000,  800000, 750,   3, 13, 5)
+        << CandleApiTiming(48000000, 1000000, 750,   3, 10, 4)
+
+        // sample point: 87.5%
+        << CandleApiTiming(48000000,   10000, 875, 300, 12, 2)
+        << CandleApiTiming(48000000,   20000, 875, 150, 12, 2)
+        << CandleApiTiming(48000000,   50000, 875,  60, 12, 2)
+        << CandleApiTiming(48000000,   83333, 875,  36, 12, 2)
+        << CandleApiTiming(48000000,  100000, 875,  30, 12, 2)
+        << CandleApiTiming(48000000,  125000, 875,  24, 12, 2)
+        << CandleApiTiming(48000000,  250000, 875,  12, 12, 2)
+        << CandleApiTiming(48000000,  500000, 875,   6, 12, 2)
+        << CandleApiTiming(48000000,  800000, 867,   4, 11, 2)
+        << CandleApiTiming(48000000, 1000000, 875,   3, 12, 2);
+
+
+    _timings
+        // sample point: 50.0%
+        << CandleApiTiming(16000000,   10000, 520, 64, 11, 12)
+        << CandleApiTiming(16000000,   20000, 500, 50,  6,  8)
+        << CandleApiTiming(16000000,   50000, 500, 20,  6,  8)
+        << CandleApiTiming(16000000,   83333, 500, 12,  6,  8)
+        << CandleApiTiming(16000000,  100000, 500, 10,  6,  8)
+        << CandleApiTiming(16000000,  125000, 500,  8,  6,  8)
+        << CandleApiTiming(16000000,  250000, 500,  4,  6,  8)
+        << CandleApiTiming(16000000,  500000, 500,  2,  6,  8)
+        << CandleApiTiming(16000000,  800000, 500,  1,  8, 10)
+        << CandleApiTiming(16000000, 1000000, 500,  1,  6,  8)
+
+        // sample point: 62.5%
+        << CandleApiTiming(16000000,   10000, 625, 64, 14,  9)
+        << CandleApiTiming(16000000,   20000, 625, 50,  8,  6)
+        << CandleApiTiming(16000000,   50000, 625, 20,  8,  6)
+        << CandleApiTiming(16000000,   83333, 625, 12,  8,  6)
+        << CandleApiTiming(16000000,  100000, 625, 10,  8,  6)
+        << CandleApiTiming(16000000,  125000, 625,  8,  8,  6)
+        << CandleApiTiming(16000000,  250000, 625,  4,  8,  6)
+        << CandleApiTiming(16000000,  500000, 625,  2,  8,  6)
+        << CandleApiTiming(16000000,  800000, 625,  1, 11,  7)
+        << CandleApiTiming(16000000, 1000000, 625,  1,  8,  6)
+
+        // sample point: 75.0%
+        << CandleApiTiming(16000000,   20000, 750, 50, 10,  4)
+        << CandleApiTiming(16000000,   50000, 750, 20, 10,  4)
+        << CandleApiTiming(16000000,   83333, 750, 12, 10,  4)
+        << CandleApiTiming(16000000,  100000, 750, 10, 10,  4)
+        << CandleApiTiming(16000000,  125000, 750,  8, 10,  4)
+        << CandleApiTiming(16000000,  250000, 750,  4, 10,  4)
+        << CandleApiTiming(16000000,  500000, 750,  2, 10,  4)
+        << CandleApiTiming(16000000,  800000, 750,  1, 13,  5)
+        << CandleApiTiming(16000000, 1000000, 750,  1, 10,  4)
+
+        // sample point: 87.5%
+        << CandleApiTiming(16000000,   20000, 875, 50, 12,  2)
+        << CandleApiTiming(16000000,   50000, 875, 20, 12,  2)
+        << CandleApiTiming(16000000,   83333, 875, 12, 12,  2)
+        << CandleApiTiming(16000000,  100000, 875, 10, 12,  2)
+        << CandleApiTiming(16000000,  125000, 875,  8, 12,  2)
+        << CandleApiTiming(16000000,  250000, 875,  4, 12,  2)
+        << CandleApiTiming(16000000,  500000, 875,  2, 12,  2)
+        << CandleApiTiming(16000000,  800000, 900,  2,  7,  1)
+        << CandleApiTiming(16000000, 1000000, 875,  1, 12,  2);
+}
+
+CandleApiInterface::~CandleApiInterface()
+{
+
+}
+
+QString CandleApiInterface::getName() const
+{
+    return "candle" + QString::number(getId() & 0xFF);
+}
+
+QString CandleApiInterface::getDetailsStr() const
+{
+    return QString::fromStdWString(getPath());
+}
+
+void CandleApiInterface::applyConfig(const MeasurementInterface &mi)
+{
+    _settings = mi;
+}
+
+unsigned CandleApiInterface::getBitrate()
+{
+    return _settings.bitrate();
+}
+
+uint32_t CandleApiInterface::getCapabilities()
+{
+    candle_capability_t caps;
+
+    if (candle_channel_get_capabilities(_handle, 0, &caps)) {
+
+        uint32_t retval = 0;
+
+        if (caps.feature & CANDLE_MODE_LISTEN_ONLY) {
+            retval |= CanInterface::capability_listen_only;
+        }
+
+        if (caps.feature & CANDLE_MODE_ONE_SHOT) {
+            retval |= CanInterface::capability_one_shot;
+        }
+
+        if (caps.feature & CANDLE_MODE_TRIPLE_SAMPLE) {
+            retval |= CanInterface::capability_triple_sampling;
+        }
+
+        return retval;
+
+    } else {
+        return 0;
+    }
+}
+
+QList<CanTiming> CandleApiInterface::getAvailableBitrates()
+{
+    QList<CanTiming> retval;
+
+    candle_capability_t caps;
+    if (candle_channel_get_capabilities(_handle, 0, &caps)) {
+        int i = 0;
+        foreach (const CandleApiTiming t, _timings) {
+            if (t.getBaseClk() == caps.fclk_can) {
+                retval << CanTiming(i++, t.getBitrate(), 0, t.getSamplePoint());
+            }
+        }
+    }
+
+    return retval;
+}
+
+bool CandleApiInterface::setBitTiming(uint32_t bitrate, uint32_t samplePoint)
+{
+    candle_capability_t caps;
+    if (!candle_channel_get_capabilities(_handle, 0, &caps)) {
+        return false;
+    }
+
+    foreach (const CandleApiTiming t, _timings) {
+        if ( (t.getBaseClk() == caps.fclk_can)
+          && (t.getBitrate()==bitrate)
+          && (t.getSamplePoint()==samplePoint) )
+        {
+            candle_bittiming_t timing = t.getTiming();
+            return candle_channel_set_timing(_handle, 0, &timing);
+        }
+    }
+
+    // no valid timing found
+    return false;
+}
+
+void CandleApiInterface::open()
+{
+    if (!candle_dev_open(_handle)) {
+        // TODO what?
+        _isOpen = false;
+        return;
+    }
+
+    if (!setBitTiming(_settings.bitrate(), _settings.samplePoint())) {
+        // TODO what?
+        _isOpen = false;
+        return;
+    }
+
+    uint32_t flags = 0;
+    if (_settings.isListenOnlyMode()) {
+        flags |= CANDLE_MODE_LISTEN_ONLY;
+    }
+    if (_settings.isOneShotMode()) {
+        flags |= CANDLE_MODE_ONE_SHOT;
+    }
+    if (_settings.isTripleSampling()) {
+        flags |= CANDLE_MODE_TRIPLE_SAMPLE;
+    }
+
+    _numRx = 0;
+    _numTx = 0;
+    _numTxErr = 0;
+
+    uint32_t t_dev;
+    if (candle_dev_get_timestamp_us(_handle, &t_dev)) {
+        _hostOffsetStart =
+                _backend.getUsecsAtMeasurementStart() +
+                _backend.getUsecsSinceMeasurementStart();
+        _deviceTicksStart = t_dev;
+    }
+
+    candle_channel_start(_handle, 0, flags);
+    _isOpen = true;
+}
+
+bool CandleApiInterface::isOpen()
+{
+    return _isOpen;
+}
+
+void CandleApiInterface::close()
+{
+    candle_channel_stop(_handle, 0);
+    candle_dev_close(_handle);
+    _isOpen = false;
+}
+
+void CandleApiInterface::sendMessage(const CanMessage &msg)
+{
+    candle_frame_t frame;
+
+    frame.can_id = msg.getId();
+    if (msg.isExtended()) {
+        frame.can_id |= CANDLE_ID_EXTENDED;
+    }
+    if (msg.isRTR()) {
+        frame.can_id |= CANDLE_ID_RTR;
+    }
+
+    frame.can_dlc = msg.getLength();
+    for (int i=0; i<8; i++) {
+        frame.data[i] = msg.getByte(i);
+    }
+
+    if (candle_frame_send(_handle, 0, &frame)) {
+        _numTx++;
+    } else {
+        _numTxErr++;
+    }
+}
+
+bool CandleApiInterface::readMessage(CanMessage &msg, unsigned int timeout_ms)
+{
+    candle_frame_t frame;
+
+    if (candle_frame_read(_handle, &frame, timeout_ms)) {
+
+        if (candle_frame_type(&frame)==CANDLE_FRAMETYPE_RECEIVE) {
+            _numRx++;
+
+            msg.setInterfaceId(getId());
+            msg.setErrorFrame(false);
+            msg.setId(candle_frame_id(&frame));
+            msg.setExtended(candle_frame_is_extended_id(&frame));
+            msg.setRTR(candle_frame_is_rtr(&frame));
+
+            uint8_t dlc = candle_frame_dlc(&frame);
+            uint8_t *data = candle_frame_data(&frame);
+            msg.setLength(dlc);
+            for (int i=0; i<dlc; i++) {
+                msg.setByte(i, data[i]);
+            }
+
+            uint32_t dev_ts = candle_frame_timestamp_us(&frame) - _deviceTicksStart;
+            uint64_t ts_us = _hostOffsetStart + dev_ts;
+
+            uint64_t us_since_start = _backend.getUsecsSinceMeasurementStart();
+            if (us_since_start > 0x180000000) { // device timestamp overflow must have happend at least once
+                ts_us += us_since_start & 0xFFFFFFFF00000000;
+            }
+
+            msg.setTimestamp(ts_us/1000000, ts_us % 1000000);
+
+            return true;
+        }
+
+    }
+
+    return false;
+}
+
+bool CandleApiInterface::updateStatistics()
+{
+    return true;
+}
+
+uint32_t CandleApiInterface::getState()
+{
+    return CanInterface::state_ok;
+}
+
+int CandleApiInterface::getNumRxFrames()
+{
+    return _numRx;
+}
+
+int CandleApiInterface::getNumRxErrors()
+{
+    return 0;
+}
+
+int CandleApiInterface::getNumTxFrames()
+{
+    return _numTx;
+}
+
+int CandleApiInterface::getNumTxErrors()
+{
+    return _numTxErr;
+}
+
+int CandleApiInterface::getNumRxOverruns()
+{
+    return 0;
+}
+
+int CandleApiInterface::getNumTxDropped()
+{
+    return 0;
+}
+
+wstring CandleApiInterface::getPath() const
+{
+    return wstring(candle_dev_get_path(_handle));
+}
+
+void CandleApiInterface::update(candle_handle dev)
+{
+    candle_dev_free(_handle);
+    _handle = dev;
+}
+

+ 70 - 0
src/tool/adc_cantool/driver/CandleApiDriver/CandleApiInterface.h

@@ -0,0 +1,70 @@
+#ifndef CANDLEAPIINTERFACE_H
+#define CANDLEAPIINTERFACE_H
+
+#include <driver/CanInterface.h>
+#include <core/MeasurementInterface.h>
+#include "api/candle.h"
+#include "CandleApiTiming.h"
+#include <windows.h>
+#include <QList>
+
+class CandleApiDriver;
+
+using namespace std;
+
+class CandleApiInterface : public CanInterface
+{
+public:
+    CandleApiInterface(CandleApiDriver *driver, candle_handle handle);
+    virtual ~CandleApiInterface();
+
+    virtual QString getName() const;
+    virtual QString getDetailsStr() const;
+
+    virtual void applyConfig(const MeasurementInterface &mi);
+
+    virtual unsigned getBitrate();
+
+    virtual uint32_t getCapabilities();
+    virtual QList<CanTiming> getAvailableBitrates();
+
+    virtual void open();
+    virtual bool isOpen();
+    virtual void close();
+
+    virtual void sendMessage(const CanMessage &msg);
+    virtual bool readMessage(CanMessage &msg, unsigned int timeout_ms);
+
+    virtual bool updateStatistics();
+    virtual uint32_t getState();
+    virtual int getNumRxFrames();
+    virtual int getNumRxErrors();
+    virtual int getNumTxFrames();
+    virtual int getNumTxErrors();
+    virtual int getNumRxOverruns();
+    virtual int getNumTxDropped();
+
+    wstring getPath() const;
+
+    void update(candle_handle dev);
+
+private:
+
+    uint64_t _hostOffsetStart;
+    uint32_t _deviceTicksStart;
+    bool _isOpen;
+
+    candle_handle _handle;
+    MeasurementInterface _settings;
+    Backend &_backend;
+
+    uint64_t _numRx;
+    uint64_t _numTx;
+    uint64_t _numTxErr;
+
+    QList<CandleApiTiming> _timings;
+
+    bool setBitTiming(uint32_t bitrate, uint32_t samplePoint);
+};
+
+#endif // CANDLEAPIINTERFACE_H

+ 33 - 0
src/tool/adc_cantool/driver/CandleApiDriver/CandleApiTiming.cpp

@@ -0,0 +1,33 @@
+#include "CandleApiTiming.h"
+
+CandleApiTiming::CandleApiTiming(uint32_t baseClk, uint32_t bitrate, uint32_t samplePoint, uint32_t brp, uint32_t phase_seg1, uint32_t phase_seg2)
+  : _baseClk(baseClk),
+    _bitrate(bitrate),
+    _samplePoint(samplePoint)
+{
+    _timing.brp = brp;
+    _timing.phase_seg1 = phase_seg1;
+    _timing.phase_seg2 = phase_seg2;
+    _timing.sjw = 1;
+    _timing.prop_seg = 1;
+}
+
+uint32_t CandleApiTiming::getBaseClk() const
+{
+    return _baseClk;
+}
+
+uint32_t CandleApiTiming::getBitrate() const
+{
+    return _bitrate;
+}
+
+uint32_t CandleApiTiming::getSamplePoint() const
+{
+    return _samplePoint;
+}
+
+candle_bittiming_t CandleApiTiming::getTiming() const
+{
+    return _timing;
+}

+ 31 - 0
src/tool/adc_cantool/driver/CandleApiDriver/CandleApiTiming.h

@@ -0,0 +1,31 @@
+#ifndef CANDLEAPITIMING_H
+#define CANDLEAPITIMING_H
+
+#include "api/candle.h"
+
+class CandleApiTiming
+{
+public:
+    CandleApiTiming(
+        uint32_t baseClk,
+        uint32_t bitrate,
+        uint32_t samplePoint,
+        uint32_t brp,
+        uint32_t phase_seg1,
+        uint32_t phase_seg2
+    );
+
+    uint32_t getBaseClk() const;
+    uint32_t getBitrate() const;
+    uint32_t getSamplePoint() const;
+    candle_bittiming_t getTiming() const;
+
+private:
+    uint32_t _baseClk;
+    uint32_t _bitrate;
+    uint32_t _samplePoint;
+    candle_bittiming_t _timing;
+
+};
+
+#endif // CANDLEAPITIMING_H

+ 66 - 0
src/tool/adc_cantool/driver/CandleApiDriver/api/LICENSE

@@ -0,0 +1,66 @@
+GNU LESSER GENERAL PUBLIC LICENSE
+
+Version 3, 29 June 2007
+
+Copyright © 2007 Free Software Foundation, Inc. <http://fsf.org/>
+
+Everyone is permitted to copy and distribute verbatim copies of this license document, but changing it is not allowed.
+
+This version of the GNU Lesser General Public License incorporates the terms and conditions of version 3 of the GNU General Public License, supplemented by the additional permissions listed below.
+0. Additional Definitions.
+
+As used herein, “this License” refers to version 3 of the GNU Lesser General Public License, and the “GNU GPL” refers to version 3 of the GNU General Public License.
+
+“The Library” refers to a covered work governed by this License, other than an Application or a Combined Work as defined below.
+
+An “Application” is any work that makes use of an interface provided by the Library, but which is not otherwise based on the Library. Defining a subclass of a class defined by the Library is deemed a mode of using an interface provided by the Library.
+
+A “Combined Work” is a work produced by combining or linking an Application with the Library. The particular version of the Library with which the Combined Work was made is also called the “Linked Version”.
+
+The “Minimal Corresponding Source” for a Combined Work means the Corresponding Source for the Combined Work, excluding any source code for portions of the Combined Work that, considered in isolation, are based on the Application, and not on the Linked Version.
+
+The “Corresponding Application Code” for a Combined Work means the object code and/or source code for the Application, including any data and utility programs needed for reproducing the Combined Work from the Application, but excluding the System Libraries of the Combined Work.
+1. Exception to Section 3 of the GNU GPL.
+
+You may convey a covered work under sections 3 and 4 of this License without being bound by section 3 of the GNU GPL.
+2. Conveying Modified Versions.
+
+If you modify a copy of the Library, and, in your modifications, a facility refers to a function or data to be supplied by an Application that uses the facility (other than as an argument passed when the facility is invoked), then you may convey a copy of the modified version:
+
+    a) under this License, provided that you make a good faith effort to ensure that, in the event an Application does not supply the function or data, the facility still operates, and performs whatever part of its purpose remains meaningful, or
+    b) under the GNU GPL, with none of the additional permissions of this License applicable to that copy.
+
+3. Object Code Incorporating Material from Library Header Files.
+
+The object code form of an Application may incorporate material from a header file that is part of the Library. You may convey such object code under terms of your choice, provided that, if the incorporated material is not limited to numerical parameters, data structure layouts and accessors, or small macros, inline functions and templates (ten or fewer lines in length), you do both of the following:
+
+    a) Give prominent notice with each copy of the object code that the Library is used in it and that the Library and its use are covered by this License.
+    b) Accompany the object code with a copy of the GNU GPL and this license document.
+
+4. Combined Works.
+
+You may convey a Combined Work under terms of your choice that, taken together, effectively do not restrict modification of the portions of the Library contained in the Combined Work and reverse engineering for debugging such modifications, if you also do each of the following:
+
+    a) Give prominent notice with each copy of the Combined Work that the Library is used in it and that the Library and its use are covered by this License.
+    b) Accompany the Combined Work with a copy of the GNU GPL and this license document.
+    c) For a Combined Work that displays copyright notices during execution, include the copyright notice for the Library among these notices, as well as a reference directing the user to the copies of the GNU GPL and this license document.
+    d) Do one of the following:
+        0) Convey the Minimal Corresponding Source under the terms of this License, and the Corresponding Application Code in a form suitable for, and under terms that permit, the user to recombine or relink the Application with a modified version of the Linked Version to produce a modified Combined Work, in the manner specified by section 6 of the GNU GPL for conveying Corresponding Source.
+        1) Use a suitable shared library mechanism for linking with the Library. A suitable mechanism is one that (a) uses at run time a copy of the Library already present on the user's computer system, and (b) will operate properly with a modified version of the Library that is interface-compatible with the Linked Version.
+    e) Provide Installation Information, but only if you would otherwise be required to provide such information under section 6 of the GNU GPL, and only to the extent that such information is necessary to install and execute a modified version of the Combined Work produced by recombining or relinking the Application with a modified version of the Linked Version. (If you use option 4d0, the Installation Information must accompany the Minimal Corresponding Source and Corresponding Application Code. If you use option 4d1, you must provide the Installation Information in the manner specified by section 6 of the GNU GPL for conveying Corresponding Source.)
+
+5. Combined Libraries.
+
+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 that are not Applications and are not covered by this License, and convey such a combined library under terms of your choice, if you do both of the following:
+
+    a) Accompany the combined library with a copy of the same work based on the Library, uncombined with any other library facilities, conveyed under the terms of this License.
+    b) Give prominent notice with the combined library that part of it is a work based on the Library, and explaining where to find the accompanying uncombined form of the same work.
+
+6. Revised Versions of the GNU Lesser General Public License.
+
+The Free Software Foundation may publish revised and/or new versions of the GNU 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 as you received it specifies that a certain numbered version of the GNU Lesser General Public License “or any later version” applies to it, you have the option of following the terms and conditions either of that published version or of any later version published by the Free Software Foundation. If the Library as you received it does not specify a version number of the GNU Lesser General Public License, you may choose any version of the GNU Lesser General Public License ever published by the Free Software Foundation.
+
+If the Library as you received it specifies that a proxy can decide whether future versions of the GNU Lesser General Public License shall apply, that proxy's public statement of acceptance of any version is permanent authorization for you to choose that version for the Library.
+

+ 589 - 0
src/tool/adc_cantool/driver/CandleApiDriver/api/candle.c

@@ -0,0 +1,589 @@
+/*
+
+  Copyright (c) 2016 Hubert Denkmair <hubert@denkmair.de>
+
+  This file is part of the candle windows API.
+  
+  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 3 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, see <http://www.gnu.org/licenses/>.
+
+*/
+
+#include "candle.h"
+#include <stdlib.h>
+
+#include "candle_defs.h"
+#include "candle_ctrl_req.h"
+#include "ch_9.h"
+
+static bool candle_dev_interal_open(candle_handle hdev);
+
+static bool candle_read_di(HDEVINFO hdi, SP_DEVICE_INTERFACE_DATA interfaceData, candle_device_t *dev)
+{
+    /* get required length first (this call always fails with an error) */
+    ULONG requiredLength=0;
+    SetupDiGetDeviceInterfaceDetail(hdi, &interfaceData, NULL, 0, &requiredLength, NULL);
+    if (GetLastError() != ERROR_INSUFFICIENT_BUFFER) {
+        dev->last_error = CANDLE_ERR_SETUPDI_IF_DETAILS;
+        return false;
+    }
+
+    PSP_DEVICE_INTERFACE_DETAIL_DATA detail_data =
+        (PSP_DEVICE_INTERFACE_DETAIL_DATA) LocalAlloc(LMEM_FIXED, requiredLength);
+
+    if (detail_data != NULL) {
+        detail_data->cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA);
+    } else {
+        dev->last_error = CANDLE_ERR_MALLOC;
+        return false;
+    }
+
+    bool retval = true;
+    ULONG length = requiredLength;
+    if (!SetupDiGetDeviceInterfaceDetail(hdi, &interfaceData, detail_data, length, &requiredLength, NULL) ) {
+        dev->last_error = CANDLE_ERR_SETUPDI_IF_DETAILS2;
+        retval = false;
+    } else if (FAILED(StringCchCopy(dev->path, sizeof(dev->path), detail_data->DevicePath))) {
+        dev->last_error = CANDLE_ERR_PATH_LEN;
+        retval = false;
+    }
+
+    LocalFree(detail_data);
+
+    if (!retval) {
+        return false;
+    }
+
+    /* try to open to read device infos and see if it is avail */
+    if (candle_dev_interal_open(dev)) {
+        dev->state = CANDLE_DEVSTATE_AVAIL;
+        candle_dev_close(dev);
+    } else {
+        dev->state = CANDLE_DEVSTATE_INUSE;
+    }
+
+    dev->last_error = CANDLE_ERR_OK;
+    return true;
+}
+
+bool __stdcall candle_list_scan(candle_list_handle *list)
+{
+    if (list==NULL) {
+        return false;
+    }
+
+    candle_list_t *l = (candle_list_t *)calloc(1, sizeof(candle_list_t));
+    *list = l;
+    if (l==NULL) {
+        return false;
+    }
+
+    GUID guid;
+    if (CLSIDFromString(L"{c15b4308-04d3-11e6-b3ea-6057189e6443}", &guid) != NOERROR) {
+        l->last_error = CANDLE_ERR_CLSID;
+        return false;
+    }
+
+    HDEVINFO hdi = SetupDiGetClassDevs(&guid, NULL, NULL, DIGCF_PRESENT | DIGCF_DEVICEINTERFACE);
+    if (hdi == INVALID_HANDLE_VALUE) {
+        l->last_error = CANDLE_ERR_GET_DEVICES;
+        return false;
+    }
+
+    bool rv = false;
+    for (unsigned i=0; i<CANDLE_MAX_DEVICES; i++) {
+
+        SP_DEVICE_INTERFACE_DATA interfaceData;
+        interfaceData.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA);
+
+        if (SetupDiEnumDeviceInterfaces(hdi, NULL, &guid, i, &interfaceData)) {
+
+            if (!candle_read_di(hdi, interfaceData, &l->dev[i])) {
+                l->last_error = l->dev[i].last_error;
+                rv = false;
+                break;
+            }
+
+        } else {
+
+            DWORD err = GetLastError();
+            if (err==ERROR_NO_MORE_ITEMS) {
+                l->num_devices = i;
+                l->last_error = CANDLE_ERR_OK;
+                rv = true;
+            } else {
+                l->last_error = CANDLE_ERR_SETUPDI_IF_ENUM;
+                rv = false;
+            }
+            break;
+
+        }
+
+    }
+
+    SetupDiDestroyDeviceInfoList(hdi);
+
+    return rv;
+
+}
+
+bool __stdcall DLL candle_list_free(candle_list_handle list)
+{
+    free(list);
+    return true;
+}
+
+bool __stdcall DLL candle_list_length(candle_list_handle list, uint8_t *len)
+{
+    candle_list_t *l = (candle_list_t *)list;
+    *len = l->num_devices;
+    return true;
+}
+
+bool __stdcall DLL candle_dev_get(candle_list_handle list, uint8_t dev_num, candle_handle *hdev)
+{
+    candle_list_t *l = (candle_list_t *)list;
+    if (l==NULL) {
+        return false;
+    }
+
+    if (dev_num >= CANDLE_MAX_DEVICES) {
+        l->last_error = CANDLE_ERR_DEV_OUT_OF_RANGE;
+        return false;
+    }
+
+    candle_device_t *dev = calloc(1, sizeof(candle_device_t));
+    *hdev = dev;
+    if (dev==NULL) {
+        l->last_error = CANDLE_ERR_MALLOC;
+        return false;
+    }
+
+    memcpy(dev, &l->dev[dev_num], sizeof(candle_device_t));
+    l->last_error = CANDLE_ERR_OK;
+    dev->last_error = CANDLE_ERR_OK;
+    return true;
+}
+
+
+bool __stdcall DLL candle_dev_get_state(candle_handle hdev, candle_devstate_t *state)
+{
+    if (hdev==NULL) {
+        return false;
+    } else {
+        candle_device_t *dev = (candle_device_t*)hdev;
+        *state = dev->state;
+        return true;
+    }
+}
+
+wchar_t __stdcall DLL *candle_dev_get_path(candle_handle hdev)
+{
+    if (hdev==NULL) {
+        return NULL;
+    } else {
+        candle_device_t *dev = (candle_device_t*)hdev;
+        return dev->path;
+    }
+}
+
+static bool candle_dev_interal_open(candle_handle hdev)
+{
+    candle_device_t *dev = (candle_device_t*)hdev;
+
+    memset(dev->rxevents, 0, sizeof(dev->rxevents));
+    memset(dev->rxurbs, 0, sizeof(dev->rxurbs));
+
+    dev->deviceHandle = CreateFile(
+        dev->path,
+        GENERIC_WRITE | GENERIC_READ,
+        FILE_SHARE_WRITE | FILE_SHARE_READ,
+        NULL,
+        OPEN_EXISTING,
+        FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED,
+        NULL
+    );
+
+    if (dev->deviceHandle == INVALID_HANDLE_VALUE) {
+        dev->last_error = CANDLE_ERR_CREATE_FILE;
+        return false;
+    }
+
+    if (!WinUsb_Initialize(dev->deviceHandle, &dev->winUSBHandle)) {
+        dev->last_error = CANDLE_ERR_WINUSB_INITIALIZE;
+        goto close_handle;
+    }
+
+    USB_INTERFACE_DESCRIPTOR ifaceDescriptor;
+    if (!WinUsb_QueryInterfaceSettings(dev->winUSBHandle, 0, &ifaceDescriptor)) {
+        dev->last_error = CANDLE_ERR_QUERY_INTERFACE;
+        goto winusb_free;
+    }
+
+    dev->interfaceNumber = ifaceDescriptor.bInterfaceNumber;
+    unsigned pipes_found = 0;
+
+    for (uint8_t i=0; i<ifaceDescriptor.bNumEndpoints; i++) {
+
+        WINUSB_PIPE_INFORMATION pipeInfo;
+        if (!WinUsb_QueryPipe(dev->winUSBHandle, 0, i, &pipeInfo)) {
+            dev->last_error = CANDLE_ERR_QUERY_PIPE;
+            goto winusb_free;
+        }
+
+        if (pipeInfo.PipeType == UsbdPipeTypeBulk && USB_ENDPOINT_DIRECTION_IN(pipeInfo.PipeId)) {
+            dev->bulkInPipe = pipeInfo.PipeId;
+            pipes_found++;
+        } else if (pipeInfo.PipeType == UsbdPipeTypeBulk && USB_ENDPOINT_DIRECTION_OUT(pipeInfo.PipeId)) {
+            dev->bulkOutPipe = pipeInfo.PipeId;
+            pipes_found++;
+        } else {
+            dev->last_error = CANDLE_ERR_PARSE_IF_DESCR;
+            goto winusb_free;
+        }
+
+    }
+
+    if (pipes_found != 2) {
+        dev->last_error = CANDLE_ERR_PARSE_IF_DESCR;
+        goto winusb_free;
+    }
+
+    char use_raw_io = 1;
+    if (!WinUsb_SetPipePolicy(dev->winUSBHandle, dev->bulkInPipe, RAW_IO, sizeof(use_raw_io), &use_raw_io)) {
+        dev->last_error = CANDLE_ERR_SET_PIPE_RAW_IO;
+        goto winusb_free;
+    } 
+
+    if (!candle_ctrl_set_host_format(dev)) {
+        goto winusb_free;
+    }
+
+    if (!candle_ctrl_get_config(dev, &dev->dconf)) {
+        goto winusb_free;
+    }
+
+    if (!candle_ctrl_get_capability(dev, 0, &dev->bt_const)) {
+        dev->last_error = CANDLE_ERR_GET_BITTIMING_CONST;
+        goto winusb_free;
+    }
+
+    dev->last_error = CANDLE_ERR_OK;
+    return true;
+
+winusb_free:
+    WinUsb_Free(dev->winUSBHandle);
+
+close_handle:
+    CloseHandle(dev->deviceHandle);
+    return false;
+
+}
+
+static bool candle_prepare_read(candle_device_t *dev, unsigned urb_num)
+{
+    bool rc = WinUsb_ReadPipe(
+        dev->winUSBHandle,
+        dev->bulkInPipe,
+        dev->rxurbs[urb_num].buf,
+        sizeof(dev->rxurbs[urb_num].buf),
+        NULL,
+        &dev->rxurbs[urb_num].ovl
+    );
+
+    if (rc || (GetLastError()!=ERROR_IO_PENDING)) {
+        dev->last_error = CANDLE_ERR_PREPARE_READ;
+        return false;
+    } else {
+        dev->last_error = CANDLE_ERR_OK;
+        return true;
+    }
+}
+
+static bool candle_close_rxurbs(candle_device_t *dev)
+{
+    for (unsigned i=0; i<CANDLE_URB_COUNT; i++) {
+        if (dev->rxevents[i] != NULL) {
+            CloseHandle(dev->rxevents[i]);
+        }
+    }
+    return true;
+}
+
+
+bool __stdcall DLL candle_dev_open(candle_handle hdev)
+{
+    candle_device_t *dev = (candle_device_t*)hdev;
+
+    if (candle_dev_interal_open(dev)) {
+        for (unsigned i=0; i<CANDLE_URB_COUNT; i++) {
+            HANDLE ev = CreateEvent(NULL, true, false, NULL);
+            dev->rxevents[i] = ev;
+            dev->rxurbs[i].ovl.hEvent = ev;
+            if (!candle_prepare_read(dev, i)) {
+                candle_close_rxurbs(dev);
+                return false; // keep last_error from prepare_read call
+            }
+        }
+        dev->last_error = CANDLE_ERR_OK;
+        return true;
+    } else {
+        return false; // keep last_error from open_device call
+    }
+
+}
+
+bool __stdcall DLL candle_dev_get_timestamp_us(candle_handle hdev, uint32_t *timestamp_us)
+{
+	return candle_ctrl_get_timestamp(hdev, timestamp_us);
+}
+
+bool __stdcall DLL candle_dev_close(candle_handle hdev)
+{
+    candle_device_t *dev = (candle_device_t*)hdev;
+
+    candle_close_rxurbs(dev);
+
+    WinUsb_Free(dev->winUSBHandle);
+    dev->winUSBHandle = NULL;
+    CloseHandle(dev->deviceHandle);
+    dev->deviceHandle = NULL;
+
+    dev->last_error = CANDLE_ERR_OK;
+    return true;
+}
+
+bool __stdcall DLL candle_dev_free(candle_handle hdev)
+{
+    free(hdev);
+    return true;
+}
+
+candle_err_t __stdcall DLL candle_dev_last_error(candle_handle hdev)
+{
+    candle_device_t *dev = (candle_device_t*)hdev;
+    return dev->last_error;
+}
+
+bool __stdcall DLL candle_channel_count(candle_handle hdev, uint8_t *num_channels)
+{
+    // TODO check if info was already read from device; try to do so; throw error...
+    candle_device_t *dev = (candle_device_t*)hdev;
+    *num_channels = dev->dconf.icount+1;
+    return true;
+}
+
+bool __stdcall DLL candle_channel_get_capabilities(candle_handle hdev, uint8_t ch, candle_capability_t *cap)
+{
+    // TODO check if info was already read from device; try to do so; throw error...
+    candle_device_t *dev = (candle_device_t*)hdev;
+    memcpy(cap, &dev->bt_const.feature, sizeof(candle_capability_t));
+    return true;
+}
+
+bool __stdcall DLL candle_channel_set_timing(candle_handle hdev, uint8_t ch, candle_bittiming_t *data)
+{
+    // TODO ensure device is open, check channel count..
+    candle_device_t *dev = (candle_device_t*)hdev;
+    return candle_ctrl_set_bittiming(dev, ch, data);
+}
+
+bool __stdcall DLL candle_channel_set_bitrate(candle_handle hdev, uint8_t ch, uint32_t bitrate)
+{
+    // TODO ensure device is open, check channel count..
+    candle_device_t *dev = (candle_device_t*)hdev;
+
+    if (dev->bt_const.fclk_can != 48000000) {
+        /* this function only works for the candleLight base clock of 48MHz */
+        dev->last_error = CANDLE_ERR_BITRATE_FCLK;
+        return false;
+    }
+
+    candle_bittiming_t t;
+    t.prop_seg = 1;
+    t.sjw = 1;
+    t.phase_seg1 = 13 - t.prop_seg;
+    t.phase_seg2 = 2;
+
+    switch (bitrate) {
+        case 10000:
+            t.brp = 300;
+            break;
+
+        case 20000:
+            t.brp = 150;
+            break;
+
+        case 50000:
+            t.brp = 60;
+            break;
+
+        case 83333:
+            t.brp = 36;
+            break;
+
+        case 100000:
+            t.brp = 30;
+            break;
+
+        case 125000:
+            t.brp = 24;
+            break;
+
+        case 250000:
+            t.brp = 12;
+            break;
+
+        case 500000:
+            t.brp = 6;
+            break;
+
+        case 800000:
+            t.brp = 4;
+            t.phase_seg1 = 12 - t.prop_seg;
+            t.phase_seg2 = 2;
+            break;
+
+        case 1000000:
+            t.brp = 3;
+            break;
+
+        default:
+            dev->last_error = CANDLE_ERR_BITRATE_UNSUPPORTED;
+            return false;
+    }
+
+    return candle_ctrl_set_bittiming(dev, ch, &t);
+}
+
+bool __stdcall DLL candle_channel_start(candle_handle hdev, uint8_t ch, uint32_t flags)
+{
+    // TODO ensure device is open, check channel count..
+    candle_device_t *dev = (candle_device_t*)hdev;
+    flags |= CANDLE_MODE_HW_TIMESTAMP;
+    return candle_ctrl_set_device_mode(dev, ch, CANDLE_DEVMODE_START, flags);
+}
+
+bool __stdcall DLL candle_channel_stop(candle_handle hdev, uint8_t ch)
+{
+    // TODO ensure device is open, check channel count..
+    candle_device_t *dev = (candle_device_t*)hdev;
+    return candle_ctrl_set_device_mode(dev, ch, CANDLE_DEVMODE_RESET, 0);
+}
+
+bool __stdcall DLL candle_frame_send(candle_handle hdev, uint8_t ch, candle_frame_t *frame)
+{
+    // TODO ensure device is open, check channel count..
+    candle_device_t *dev = (candle_device_t*)hdev;
+
+    unsigned long bytes_sent = 0;
+
+    frame->echo_id = 0;
+    frame->channel = ch;
+
+    bool rc = WinUsb_WritePipe(
+        dev->winUSBHandle,
+        dev->bulkOutPipe,
+        (uint8_t*)frame,
+        sizeof(*frame),
+        &bytes_sent,
+        0
+    );
+
+    dev->last_error = rc ? CANDLE_ERR_OK : CANDLE_ERR_SEND_FRAME;
+    return rc;
+
+}
+
+bool __stdcall DLL candle_frame_read(candle_handle hdev, candle_frame_t *frame, uint32_t timeout_ms)
+{
+    // TODO ensure device is open..
+    candle_device_t *dev = (candle_device_t*)hdev;
+
+    DWORD wait_result = WaitForMultipleObjects(CANDLE_URB_COUNT, dev->rxevents, false, timeout_ms);
+    if (wait_result == WAIT_TIMEOUT) {
+        dev->last_error = CANDLE_ERR_READ_TIMEOUT;
+        return false;
+    }
+
+    if ( (wait_result < WAIT_OBJECT_0) || (wait_result >= WAIT_OBJECT_0 + CANDLE_URB_COUNT) ) {
+        dev->last_error = CANDLE_ERR_READ_WAIT;
+        return false;
+    }
+
+    DWORD urb_num = wait_result - WAIT_OBJECT_0;
+    DWORD bytes_transfered;
+
+    if (!WinUsb_GetOverlappedResult(dev->winUSBHandle, &dev->rxurbs[urb_num].ovl, &bytes_transfered, false)) {
+        candle_prepare_read(dev, urb_num);
+        dev->last_error = CANDLE_ERR_READ_RESULT;
+        return false;
+    }
+
+    if (bytes_transfered < sizeof(*frame)-4) {
+        candle_prepare_read(dev, urb_num);
+        dev->last_error = CANDLE_ERR_READ_SIZE;
+        return false;
+    }
+
+    if (bytes_transfered < sizeof(*frame)) {
+        frame->timestamp_us = 0;
+    }
+
+    memcpy(frame, dev->rxurbs[urb_num].buf, sizeof(*frame));
+
+    return candle_prepare_read(dev, urb_num);
+}
+
+candle_frametype_t __stdcall DLL candle_frame_type(candle_frame_t *frame)
+{
+    if (frame->echo_id != 0xFFFFFFFF) {
+        return CANDLE_FRAMETYPE_ECHO;
+    };
+
+    if (frame->can_id & CANDLE_ID_ERR) {
+        return CANDLE_FRAMETYPE_ERROR;
+    }
+
+    return CANDLE_FRAMETYPE_RECEIVE;
+}
+
+uint32_t __stdcall DLL candle_frame_id(candle_frame_t *frame)
+{
+    return frame->can_id & 0x1FFFFFFF;
+}
+
+bool __stdcall DLL candle_frame_is_extended_id(candle_frame_t *frame)
+{
+    return (frame->can_id & CANDLE_ID_EXTENDED) != 0;
+}
+
+bool __stdcall DLL candle_frame_is_rtr(candle_frame_t *frame)
+{
+    return (frame->can_id & CANDLE_ID_RTR) != 0;
+}
+
+uint8_t __stdcall DLL candle_frame_dlc(candle_frame_t *frame)
+{
+    return frame->can_dlc;
+}
+
+uint8_t __stdcall DLL *candle_frame_data(candle_frame_t *frame)
+{
+    return frame->data;
+}
+
+uint32_t __stdcall DLL candle_frame_timestamp_us(candle_frame_t *frame)
+{
+    return frame->timestamp_us;
+}

+ 166 - 0
src/tool/adc_cantool/driver/CandleApiDriver/api/candle.h

@@ -0,0 +1,166 @@
+/*
+
+  Copyright (c) 2016 Hubert Denkmair <hubert@denkmair.de>
+
+  This file is part of the candle windows API.
+  
+  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 3 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, see <http://www.gnu.org/licenses/>.
+
+*/
+
+#pragma once
+#include <stdint.h>
+#include <stdbool.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+typedef void* candle_list_handle;
+typedef void* candle_handle;
+
+typedef enum {
+    CANDLE_DEVSTATE_AVAIL,
+    CANDLE_DEVSTATE_INUSE
+} candle_devstate_t;
+
+typedef enum {
+    CANDLE_FRAMETYPE_UNKNOWN,
+    CANDLE_FRAMETYPE_RECEIVE,
+    CANDLE_FRAMETYPE_ECHO,
+    CANDLE_FRAMETYPE_ERROR,
+    CANDLE_FRAMETYPE_TIMESTAMP_OVFL
+} candle_frametype_t;
+
+enum {
+    CANDLE_ID_EXTENDED = 0x80000000,
+    CANDLE_ID_RTR      = 0x40000000,
+    CANDLE_ID_ERR      = 0x20000000
+};
+
+typedef enum {
+    CANDLE_MODE_NORMAL        = 0x00,
+    CANDLE_MODE_LISTEN_ONLY   = 0x01,
+    CANDLE_MODE_LOOP_BACK     = 0x02,
+    CANDLE_MODE_TRIPLE_SAMPLE = 0x04,
+    CANDLE_MODE_ONE_SHOT      = 0x08,
+    CANDLE_MODE_HW_TIMESTAMP  = 0x10,
+} candle_mode_t;
+
+typedef enum {
+    CANDLE_ERR_OK                  =  0,
+    CANDLE_ERR_CREATE_FILE         =  1,
+    CANDLE_ERR_WINUSB_INITIALIZE   =  2,
+    CANDLE_ERR_QUERY_INTERFACE     =  3,
+    CANDLE_ERR_QUERY_PIPE          =  4,
+    CANDLE_ERR_PARSE_IF_DESCR      =  5,
+    CANDLE_ERR_SET_HOST_FORMAT     =  6,
+    CANDLE_ERR_GET_DEVICE_INFO     =  7,
+    CANDLE_ERR_GET_BITTIMING_CONST =  8,
+    CANDLE_ERR_PREPARE_READ        =  9,
+    CANDLE_ERR_SET_DEVICE_MODE     = 10,
+    CANDLE_ERR_SET_BITTIMING       = 11,
+    CANDLE_ERR_BITRATE_FCLK        = 12,
+    CANDLE_ERR_BITRATE_UNSUPPORTED = 13,
+    CANDLE_ERR_SEND_FRAME          = 14,
+    CANDLE_ERR_READ_TIMEOUT        = 15,
+    CANDLE_ERR_READ_WAIT           = 16,
+    CANDLE_ERR_READ_RESULT         = 17,
+    CANDLE_ERR_READ_SIZE           = 18,
+    CANDLE_ERR_SETUPDI_IF_DETAILS  = 19,
+    CANDLE_ERR_SETUPDI_IF_DETAILS2 = 20,
+    CANDLE_ERR_MALLOC              = 21,
+    CANDLE_ERR_PATH_LEN            = 22,
+    CANDLE_ERR_CLSID               = 23,
+    CANDLE_ERR_GET_DEVICES         = 24,
+    CANDLE_ERR_SETUPDI_IF_ENUM     = 25,
+    CANDLE_ERR_SET_TIMESTAMP_MODE  = 26,
+    CANDLE_ERR_DEV_OUT_OF_RANGE    = 27,
+	CANDLE_ERR_GET_TIMESTAMP       = 28,
+    CANDLE_ERR_SET_PIPE_RAW_IO     = 29
+} candle_err_t;
+
+#pragma pack(push,1)
+
+typedef struct {
+    uint32_t echo_id;
+    uint32_t can_id;
+    uint8_t can_dlc;
+    uint8_t channel;
+    uint8_t flags;
+    uint8_t reserved;
+    uint8_t data[8];
+    uint32_t timestamp_us;
+} candle_frame_t;
+
+typedef struct {
+    uint32_t feature;
+    uint32_t fclk_can;
+    uint32_t tseg1_min;
+    uint32_t tseg1_max;
+    uint32_t tseg2_min;
+    uint32_t tseg2_max;
+    uint32_t sjw_max;
+    uint32_t brp_min;
+    uint32_t brp_max;
+    uint32_t brp_inc;
+} candle_capability_t;
+
+typedef struct {
+    uint32_t prop_seg;
+    uint32_t phase_seg1;
+    uint32_t phase_seg2;
+    uint32_t sjw;
+    uint32_t brp;
+} candle_bittiming_t;
+
+#pragma pack(pop)
+
+#define DLL
+
+bool __stdcall DLL candle_list_scan(candle_list_handle *list);
+bool __stdcall DLL candle_list_free(candle_list_handle list);
+bool __stdcall DLL candle_list_length(candle_list_handle list, uint8_t *len);
+
+bool __stdcall DLL candle_dev_get(candle_list_handle list, uint8_t dev_num, candle_handle *hdev);
+bool __stdcall DLL candle_dev_get_state(candle_handle hdev, candle_devstate_t *state);
+wchar_t __stdcall DLL *candle_dev_get_path(candle_handle hdev);
+bool __stdcall DLL candle_dev_open(candle_handle hdev);
+bool __stdcall DLL candle_dev_get_timestamp_us(candle_handle hdev, uint32_t *timestamp_us);
+bool __stdcall DLL candle_dev_close(candle_handle hdev);
+bool __stdcall DLL candle_dev_free(candle_handle hdev);
+
+bool __stdcall DLL candle_channel_count(candle_handle hdev, uint8_t *num_channels);
+bool __stdcall DLL candle_channel_get_capabilities(candle_handle hdev, uint8_t ch, candle_capability_t *cap);
+bool __stdcall DLL candle_channel_set_timing(candle_handle hdev, uint8_t ch, candle_bittiming_t *data);
+bool __stdcall DLL candle_channel_set_bitrate(candle_handle hdev, uint8_t ch, uint32_t bitrate);
+bool __stdcall DLL candle_channel_start(candle_handle hdev, uint8_t ch, uint32_t flags);
+bool __stdcall DLL candle_channel_stop(candle_handle hdev, uint8_t ch);
+
+bool __stdcall DLL candle_frame_send(candle_handle hdev, uint8_t ch, candle_frame_t *frame);
+bool __stdcall DLL candle_frame_read(candle_handle hdev, candle_frame_t *frame, uint32_t timeout_ms);
+
+candle_frametype_t __stdcall DLL candle_frame_type(candle_frame_t *frame);
+uint32_t __stdcall DLL candle_frame_id(candle_frame_t *frame);
+bool __stdcall DLL candle_frame_is_extended_id(candle_frame_t *frame);
+bool __stdcall DLL candle_frame_is_rtr(candle_frame_t *frame);
+uint8_t __stdcall DLL candle_frame_dlc(candle_frame_t *frame);
+uint8_t __stdcall DLL *candle_frame_data(candle_frame_t *frame);
+uint32_t __stdcall DLL candle_frame_timestamp_us(candle_frame_t *frame);
+
+candle_err_t __stdcall DLL candle_dev_last_error(candle_handle hdev);
+
+#ifdef __cplusplus
+}
+#endif

+ 152 - 0
src/tool/adc_cantool/driver/CandleApiDriver/api/candle_ctrl_req.c

@@ -0,0 +1,152 @@
+/*
+
+  Copyright (c) 2016 Hubert Denkmair <hubert@denkmair.de>
+
+  This file is part of the candle windows API.
+  
+  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 3 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, see <http://www.gnu.org/licenses/>.
+
+*/
+
+#include "candle_ctrl_req.h"
+#include "ch_9.h"
+
+enum {
+    CANDLE_BREQ_HOST_FORMAT = 0,
+    CANDLE_BREQ_BITTIMING,
+    CANDLE_BREQ_MODE,
+    CANDLE_BREQ_BERR,
+    CANDLE_BREQ_BT_CONST,
+    CANDLE_BREQ_DEVICE_CONFIG,
+    CANDLE_TIMESTAMP_GET,
+};
+
+static bool usb_control_msg(WINUSB_INTERFACE_HANDLE hnd, uint8_t request, uint8_t requesttype, uint16_t value, uint16_t index, void *data, uint16_t size)
+{
+    WINUSB_SETUP_PACKET packet;
+    memset(&packet, 0, sizeof(packet));
+
+    packet.Request = request;
+    packet.RequestType = requesttype;
+    packet.Value = value;
+    packet.Index = index;
+    packet.Length = size;
+
+    unsigned long bytes_sent = 0;
+    return WinUsb_ControlTransfer(hnd, packet, (uint8_t*)data, size, &bytes_sent, 0);
+}
+
+bool candle_ctrl_set_host_format(candle_device_t *dev)
+{
+    candle_host_config_t hconf;
+    hconf.byte_order = 0x0000beef;
+
+    bool rc = usb_control_msg(
+        dev->winUSBHandle,
+        CANDLE_BREQ_HOST_FORMAT,
+        USB_DIR_OUT|USB_TYPE_VENDOR|USB_RECIP_INTERFACE,
+        1,
+        dev->interfaceNumber,
+        &hconf,
+        sizeof(hconf)
+    );
+
+    dev->last_error = rc ? CANDLE_ERR_OK : CANDLE_ERR_SET_HOST_FORMAT;
+    return rc;
+}
+
+bool candle_ctrl_set_device_mode(candle_device_t *dev, uint8_t channel, uint32_t mode, uint32_t flags)
+{
+    candle_device_mode_t dm;
+    dm.mode = mode;
+    dm.flags = flags;
+
+    bool rc = usb_control_msg(
+        dev->winUSBHandle,
+        CANDLE_BREQ_MODE,
+        USB_DIR_OUT|USB_TYPE_VENDOR|USB_RECIP_INTERFACE,
+        channel,
+        dev->interfaceNumber,
+        &dm,
+        sizeof(dm)
+    );
+
+    dev->last_error = rc ? CANDLE_ERR_OK : CANDLE_ERR_SET_DEVICE_MODE;
+    return rc;
+}
+
+
+bool candle_ctrl_get_config(candle_device_t *dev, candle_device_config_t *dconf)
+{
+    bool rc = usb_control_msg(
+        dev->winUSBHandle,
+        CANDLE_BREQ_DEVICE_CONFIG,
+        USB_DIR_IN|USB_TYPE_VENDOR|USB_RECIP_INTERFACE,
+        1,
+        dev->interfaceNumber,
+        dconf,
+        sizeof(*dconf)
+    );
+
+    dev->last_error = rc ? CANDLE_ERR_OK : CANDLE_ERR_GET_DEVICE_INFO;
+    return rc;
+}
+
+bool candle_ctrl_get_timestamp(candle_device_t *dev, uint32_t *current_timestamp)
+{
+    bool rc = usb_control_msg(
+        dev->winUSBHandle,
+        CANDLE_TIMESTAMP_GET,
+        USB_DIR_IN|USB_TYPE_VENDOR|USB_RECIP_INTERFACE,
+        1,
+        dev->interfaceNumber,
+        current_timestamp,
+        sizeof(*current_timestamp)
+    );
+
+    dev->last_error = rc ? CANDLE_ERR_OK : CANDLE_ERR_GET_TIMESTAMP;
+    return rc;
+}
+
+bool candle_ctrl_get_capability(candle_device_t *dev, uint8_t channel, candle_capability_t *data)
+{
+    bool rc = usb_control_msg(
+        dev->winUSBHandle,
+        CANDLE_BREQ_BT_CONST,
+        USB_DIR_IN|USB_TYPE_VENDOR|USB_RECIP_INTERFACE,
+        channel,
+        0,
+        data,
+        sizeof(*data)
+    );
+
+    dev->last_error = rc ? CANDLE_ERR_OK : CANDLE_ERR_GET_BITTIMING_CONST;
+    return rc;
+}
+
+bool candle_ctrl_set_bittiming(candle_device_t *dev, uint8_t channel, candle_bittiming_t *data)
+{
+    bool rc = usb_control_msg(
+        dev->winUSBHandle,
+        CANDLE_BREQ_BITTIMING,
+        USB_DIR_OUT|USB_TYPE_VENDOR|USB_RECIP_INTERFACE,
+        channel,
+        0,
+        data,
+        sizeof(*data)
+    );
+
+    dev->last_error = rc ? CANDLE_ERR_OK : CANDLE_ERR_SET_BITTIMING;
+    return rc;
+}

+ 37 - 0
src/tool/adc_cantool/driver/CandleApiDriver/api/candle_ctrl_req.h

@@ -0,0 +1,37 @@
+/*
+
+  Copyright (c) 2016 Hubert Denkmair <hubert@denkmair.de>
+
+  This file is part of the candle windows API.
+  
+  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 3 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, see <http://www.gnu.org/licenses/>.
+
+*/
+
+#pragma once
+
+#include "candle_defs.h"
+
+enum {
+    CANDLE_DEVMODE_RESET = 0,
+    CANDLE_DEVMODE_START = 1
+};
+
+bool candle_ctrl_set_host_format(candle_device_t *dev);
+bool candle_ctrl_set_device_mode(candle_device_t *dev, uint8_t channel, uint32_t mode, uint32_t flags);
+bool candle_ctrl_get_config(candle_device_t *dev, candle_device_config_t *dconf);
+bool candle_ctrl_get_capability(candle_device_t *dev, uint8_t channel, candle_capability_t *data);
+bool candle_ctrl_set_bittiming(candle_device_t *dev, uint8_t channel, candle_bittiming_t *data);
+bool candle_ctrl_get_timestamp(candle_device_t *dev, uint32_t *current_timestamp);
+

+ 90 - 0
src/tool/adc_cantool/driver/CandleApiDriver/api/candle_defs.h

@@ -0,0 +1,90 @@
+/*
+
+  Copyright (c) 2016 Hubert Denkmair <hubert@denkmair.de>
+
+  This file is part of the candle windows API.
+  
+  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 3 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, see <http://www.gnu.org/licenses/>.
+
+*/
+
+#pragma once
+
+#include <stdint.h>
+#include <windows.h>
+#include <winbase.h>
+#include <winusb.h>
+#include <setupapi.h>
+#include <devguid.h>
+#include <regstr.h>
+
+#undef __CRT__NO_INLINE
+#include <strsafe.h>
+#define __CRT__NO_INLINE
+
+#include "candle.h"
+
+#define CANDLE_MAX_DEVICES 32
+#define CANDLE_URB_COUNT 30
+
+#pragma pack(push,1)
+
+typedef struct {
+    uint32_t byte_order;
+} candle_host_config_t;
+
+typedef struct {
+    uint8_t reserved1;
+    uint8_t reserved2;
+    uint8_t reserved3;
+    uint8_t icount;
+    uint32_t sw_version;
+    uint32_t hw_version;
+} candle_device_config_t;
+
+typedef struct {
+    uint32_t mode;
+    uint32_t flags;
+} candle_device_mode_t;
+
+#pragma pack(pop)
+
+
+typedef struct {
+    OVERLAPPED ovl;
+    uint8_t buf[128];
+} canlde_rx_urb;
+
+typedef struct {
+    wchar_t path[256];
+    candle_devstate_t state;
+    candle_err_t last_error;
+
+    HANDLE deviceHandle;
+    WINUSB_INTERFACE_HANDLE winUSBHandle;
+    UCHAR interfaceNumber;
+    UCHAR bulkInPipe;
+    UCHAR bulkOutPipe;
+
+    candle_device_config_t dconf;
+    candle_capability_t bt_const;
+    canlde_rx_urb rxurbs[CANDLE_URB_COUNT];
+    HANDLE rxevents[CANDLE_URB_COUNT];
+} candle_device_t;
+
+typedef struct {
+    uint8_t num_devices;
+    candle_err_t last_error;
+    candle_device_t dev[CANDLE_MAX_DEVICES];
+} candle_list_t;

+ 37 - 0
src/tool/adc_cantool/driver/CandleApiDriver/api/ch_9.h

@@ -0,0 +1,37 @@
+/*
+
+  Copyright (c) 2016 Hubert Denkmair <hubert@denkmair.de>
+
+  This file is part of the candle windows API.
+  
+  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 3 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, see <http://www.gnu.org/licenses/>.
+
+*/
+
+#pragma once
+
+#define USB_DIR_OUT                     0               /* to device */
+#define USB_DIR_IN                      0x80            /* to host */
+
+#define USB_TYPE_MASK                   (0x03 << 5)
+#define USB_TYPE_STANDARD               (0x00 << 5)
+#define USB_TYPE_CLASS                  (0x01 << 5)
+#define USB_TYPE_VENDOR                 (0x02 << 5)
+#define USB_TYPE_RESERVED               (0x03 << 5)
+
+#define USB_RECIP_MASK                  0x1f
+#define USB_RECIP_DEVICE                0x00
+#define USB_RECIP_INTERFACE             0x01
+#define USB_RECIP_ENDPOINT              0x02
+#define USB_RECIP_OTHER                 0x03

+ 176 - 0
src/tool/adc_cantool/driver/GenericCanSetupPage.cpp

@@ -0,0 +1,176 @@
+#include "GenericCanSetupPage.h"
+#include "ui_GenericCanSetupPage.h"
+#include <core/Backend.h>
+#include <driver/CanInterface.h>
+#include <core/MeasurementInterface.h>
+#include <window/SetupDialog/SetupDialog.h>
+#include <QList>
+#include <QtAlgorithms>
+
+GenericCanSetupPage::GenericCanSetupPage(QWidget *parent) :
+    QWidget(parent),
+    ui(new Ui::GenericCanSetupPage),
+    _mi(0),
+    _enable_ui_updates(false)
+{
+    ui->setupUi(this);
+    connect(ui->cbBitrate, SIGNAL(currentIndexChanged(int)), this, SLOT(updateUI()));
+    connect(ui->cbSamplePoint, SIGNAL(currentIndexChanged(int)), this, SLOT(updateUI()));
+    connect(ui->cbBitrateFD, SIGNAL(currentIndexChanged(int)), this, SLOT(updateUI()));
+    connect(ui->cbSamplePointFD, SIGNAL(currentIndexChanged(int)), this, SLOT(updateUI()));
+
+    connect(ui->cbConfigOS, SIGNAL(stateChanged(int)), this, SLOT(updateUI()));
+    connect(ui->cbListenOnly, SIGNAL(stateChanged(int)), this, SLOT(updateUI()));
+    connect(ui->cbOneShot, SIGNAL(stateChanged(int)), this, SLOT(updateUI()));
+    connect(ui->cbTripleSampling, SIGNAL(stateChanged(int)), this, SLOT(updateUI()));
+    connect(ui->cbAutoRestart, SIGNAL(stateChanged(int)), this, SLOT(updateUI()));
+}
+
+GenericCanSetupPage::~GenericCanSetupPage()
+{
+    delete ui;
+}
+
+void GenericCanSetupPage::onSetupDialogCreated(SetupDialog &dlg)
+{
+    dlg.addPage(this);
+    connect(&dlg, SIGNAL(onShowInterfacePage(SetupDialog&,MeasurementInterface*)), this, SLOT(onShowInterfacePage(SetupDialog&,MeasurementInterface*)));
+}
+
+void GenericCanSetupPage::onShowInterfacePage(SetupDialog &dlg, MeasurementInterface *mi)
+{
+    _mi = mi;
+    CanInterface *intf = backend().getInterfaceById(_mi->canInterface());
+
+    _enable_ui_updates = false;
+
+    ui->laDriver->setText(intf->getDriver()->getName());
+    ui->laInterface->setText(intf->getName());
+    ui->laInterfaceDetails->setText(intf->getDetailsStr());
+
+    fillBitratesList(intf, _mi->bitrate());
+    fillFdBitrate(intf, _mi->fdBitrate());//(intf, _mi->bitrate());//cxw,
+    fillSamplePointsForBitrate(intf, _mi->bitrate(), _mi->samplePoint());
+
+
+    ui->cbConfigOS->setChecked(!_mi->doConfigure());
+    ui->cbListenOnly->setChecked(_mi->isListenOnlyMode());
+    ui->cbOneShot->setChecked(_mi->isOneShotMode());
+    ui->cbTripleSampling->setChecked(_mi->isTripleSampling());
+    ui->cbAutoRestart->setChecked(_mi->doAutoRestart());
+
+    disenableUI(_mi->doConfigure());
+    dlg.displayPage(this);
+
+    _enable_ui_updates = true;
+}
+
+void GenericCanSetupPage::updateUI()
+{
+    if (_enable_ui_updates && (_mi!=0)) {
+        CanInterface *intf = backend().getInterfaceById(_mi->canInterface());
+
+        _mi->setDoConfigure(!ui->cbConfigOS->isChecked());
+        _mi->setListenOnlyMode(ui->cbListenOnly->isChecked());
+        _mi->setOneShotMode(ui->cbOneShot->isChecked());
+        _mi->setTripleSampling(ui->cbTripleSampling->isChecked());
+        _mi->setAutoRestart(ui->cbAutoRestart->isChecked());
+        _mi->setBitrate(ui->cbBitrate->currentData().toUInt());
+        _mi->setSamplePoint(ui->cbSamplePoint->currentData().toUInt());
+        _mi->setFdBitrate(ui->cbBitrateFD->currentData().toUInt());  //cxw,fd
+
+        _enable_ui_updates = false;
+
+        disenableUI(_mi->doConfigure());
+        fillSamplePointsForBitrate(
+            intf,
+            ui->cbBitrate->currentData().toUInt(),
+            ui->cbSamplePoint->currentData().toUInt()
+        );
+        _enable_ui_updates = true;
+
+    }
+
+
+}
+
+void GenericCanSetupPage::fillBitratesList(CanInterface *intf, unsigned selectedBitrate)
+{
+    QList<uint32_t> bitrates;
+    foreach (CanTiming t, intf->getAvailableBitrates()) {
+        if (!bitrates.contains(t.getBitrate())) {
+            bitrates.append(t.getBitrate());
+        }
+    }
+    qSort(bitrates);
+
+    ui->cbBitrate->clear();
+    foreach (uint32_t br, bitrates) {
+        ui->cbBitrate->addItem(QString::number(br), br);
+    }
+    ui->cbBitrate->setCurrentText(QString::number(selectedBitrate));
+}
+
+void GenericCanSetupPage::fillSamplePointsForBitrate(CanInterface *intf, unsigned selectedBitrate, unsigned selectedSamplePoint)
+{
+    QList<uint32_t> samplePoints;
+    foreach(CanTiming t, intf->getAvailableBitrates()) {
+        if (t.getBitrate() == selectedBitrate) {
+            if (!samplePoints.contains(t.getSamplePoint())) {
+                samplePoints.append(t.getSamplePoint());
+            }
+        }
+    }
+    qSort(samplePoints);
+
+    ui->cbSamplePoint->clear();
+    foreach (uint32_t sp, samplePoints) {
+        ui->cbSamplePoint->addItem(CanTiming::getSamplePointStr(sp), sp);
+    }
+    ui->cbSamplePoint->setCurrentText(CanTiming::getSamplePointStr(selectedSamplePoint));
+}
+
+
+void GenericCanSetupPage::fillFdBitrate(CanInterface *intf, unsigned selectedBitrate)
+{
+    QList<uint32_t> fdBitrates;
+    foreach(CanTiming t, intf->getAvailableBitrates()) {
+//        if (t.getBitrate() == selectedBitrate) {
+            if (t.isCanFD() && !fdBitrates.contains(t.getBitrateFD())) {  //cxw,canfd 波特率可选
+                fdBitrates.append(t.getBitrateFD());
+            }
+//        }
+    }
+
+
+    qSort(fdBitrates);
+
+    ui->cbBitrateFD->clear();
+    foreach (uint32_t fd_br, fdBitrates) {
+        ui->cbBitrateFD->addItem(QString::number(fd_br), fd_br);
+    }
+    ui->cbBitrateFD->setCurrentText(QString::number(selectedBitrate)); //cxw,canfd botelv
+}
+
+void GenericCanSetupPage::disenableUI(bool enabled)
+{
+
+    CanInterface *intf = backend().getInterfaceById(_mi->canInterface());
+    uint32_t caps = intf->getCapabilities();
+
+    ui->cbBitrate->setEnabled(enabled);
+    ui->cbSamplePoint->setEnabled(enabled);
+    ui->cbConfigOS->setEnabled(caps & CanInterface::capability_config_os);
+
+    ui->cbBitrateFD->setEnabled(enabled && (caps & CanInterface::capability_canfd));
+    ui->cbSamplePointFD->setEnabled(enabled && (caps & CanInterface::capability_canfd));
+    ui->cbListenOnly->setEnabled(enabled && (caps & CanInterface::capability_listen_only));
+    ui->cbOneShot->setEnabled(enabled && (caps & CanInterface::capability_one_shot));
+    ui->cbTripleSampling->setEnabled(enabled && (caps & CanInterface::capability_triple_sampling));
+    ui->cbAutoRestart->setEnabled(enabled && (caps & CanInterface::capability_auto_restart));
+}
+
+Backend &GenericCanSetupPage::backend()
+{
+    return Backend::instance();
+}

+ 43 - 0
src/tool/adc_cantool/driver/GenericCanSetupPage.h

@@ -0,0 +1,43 @@
+#ifndef GENERICCANSETUPPAGE_H
+#define GENERICCANSETUPPAGE_H
+
+#include <QWidget>
+
+namespace Ui {
+class GenericCanSetupPage;
+}
+
+class CanInterface;
+class SetupDialog;
+class MeasurementInterface;
+class Backend;
+
+class GenericCanSetupPage : public QWidget
+{
+    Q_OBJECT
+
+public:
+    explicit GenericCanSetupPage(QWidget *parent = 0);
+    ~GenericCanSetupPage();
+
+public slots:
+    void onSetupDialogCreated(SetupDialog &dlg);
+    void onShowInterfacePage(SetupDialog &dlg, MeasurementInterface *mi);
+
+private slots:
+    void updateUI();
+
+private:
+    Ui::GenericCanSetupPage *ui;
+    MeasurementInterface *_mi;
+    bool _enable_ui_updates;
+
+    void fillBitratesList(CanInterface *intf, unsigned selectedBitrate);
+    void fillSamplePointsForBitrate(CanInterface *intf, unsigned selectedBitrate, unsigned selectedSamplePoint);
+    void fillFdBitrate(CanInterface *intf, unsigned selectedBitrate);
+    void disenableUI(bool enabled);
+
+    Backend &backend();
+};
+
+#endif // GENERICCANSETUPPAGE_H

+ 264 - 0
src/tool/adc_cantool/driver/GenericCanSetupPage.ui

@@ -0,0 +1,264 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<ui version="4.0">
+ <class>GenericCanSetupPage</class>
+ <widget class="QWidget" name="GenericCanSetupPage">
+  <property name="geometry">
+   <rect>
+    <x>0</x>
+    <y>0</y>
+    <width>795</width>
+    <height>633</height>
+   </rect>
+  </property>
+  <property name="windowTitle">
+   <string>Form</string>
+  </property>
+  <widget class="QLabel" name="label">
+   <property name="geometry">
+    <rect>
+     <x>9</x>
+     <y>9</y>
+     <width>66</width>
+     <height>16</height>
+    </rect>
+   </property>
+   <property name="text">
+    <string>驱动:</string>
+   </property>
+  </widget>
+  <widget class="QLabel" name="laDriver">
+   <property name="geometry">
+    <rect>
+     <x>140</x>
+     <y>9</y>
+     <width>610</width>
+     <height>16</height>
+    </rect>
+   </property>
+   <property name="text">
+    <string>TextLabel</string>
+   </property>
+  </widget>
+  <widget class="QLabel" name="label_3">
+   <property name="geometry">
+    <rect>
+     <x>9</x>
+     <y>28</y>
+     <width>82</width>
+     <height>16</height>
+    </rect>
+   </property>
+   <property name="text">
+    <string>接口:</string>
+   </property>
+  </widget>
+  <widget class="QLabel" name="laInterface">
+   <property name="geometry">
+    <rect>
+     <x>140</x>
+     <y>28</y>
+     <width>610</width>
+     <height>16</height>
+    </rect>
+   </property>
+   <property name="text">
+    <string>TextLabel</string>
+   </property>
+  </widget>
+  <widget class="QLabel" name="label_5">
+   <property name="geometry">
+    <rect>
+     <x>9</x>
+     <y>47</y>
+     <width>117</width>
+     <height>16</height>
+    </rect>
+   </property>
+   <property name="text">
+    <string>接口详细信息:</string>
+   </property>
+  </widget>
+  <widget class="QLabel" name="laInterfaceDetails">
+   <property name="geometry">
+    <rect>
+     <x>140</x>
+     <y>47</y>
+     <width>610</width>
+     <height>16</height>
+    </rect>
+   </property>
+   <property name="text">
+    <string>TextLabel</string>
+   </property>
+  </widget>
+  <widget class="QLabel" name="label_7">
+   <property name="geometry">
+    <rect>
+     <x>9</x>
+     <y>84</y>
+     <width>69</width>
+     <height>16</height>
+    </rect>
+   </property>
+   <property name="text">
+    <string>波特率:</string>
+   </property>
+  </widget>
+  <widget class="QComboBox" name="cbBitrate">
+   <property name="geometry">
+    <rect>
+     <x>140</x>
+     <y>84</y>
+     <width>170</width>
+     <height>20</height>
+    </rect>
+   </property>
+  </widget>
+  <widget class="QLabel" name="label_8">
+   <property name="geometry">
+    <rect>
+     <x>9</x>
+     <y>110</y>
+     <width>98</width>
+     <height>16</height>
+    </rect>
+   </property>
+   <property name="text">
+    <string>采样点:</string>
+   </property>
+  </widget>
+  <widget class="QComboBox" name="cbSamplePoint">
+   <property name="geometry">
+    <rect>
+     <x>140</x>
+     <y>110</y>
+     <width>170</width>
+     <height>20</height>
+    </rect>
+   </property>
+  </widget>
+  <widget class="QLabel" name="label_9">
+   <property name="geometry">
+    <rect>
+     <x>9</x>
+     <y>150</y>
+     <width>104</width>
+     <height>16</height>
+    </rect>
+   </property>
+   <property name="text">
+    <string>CanFD 波特率:</string>
+   </property>
+  </widget>
+  <widget class="QComboBox" name="cbBitrateFD">
+   <property name="geometry">
+    <rect>
+     <x>140</x>
+     <y>150</y>
+     <width>170</width>
+     <height>20</height>
+    </rect>
+   </property>
+  </widget>
+  <widget class="QLabel" name="label_10">
+   <property name="geometry">
+    <rect>
+     <x>9</x>
+     <y>176</y>
+     <width>130</width>
+     <height>16</height>
+    </rect>
+   </property>
+   <property name="text">
+    <string>CanFD 采样点:</string>
+   </property>
+  </widget>
+  <widget class="QComboBox" name="cbSamplePointFD">
+   <property name="geometry">
+    <rect>
+     <x>140</x>
+     <y>176</y>
+     <width>170</width>
+     <height>20</height>
+    </rect>
+   </property>
+  </widget>
+  <widget class="QLabel" name="label_11">
+   <property name="geometry">
+    <rect>
+     <x>9</x>
+     <y>220</y>
+     <width>61</width>
+     <height>16</height>
+    </rect>
+   </property>
+   <property name="text">
+    <string>其他:</string>
+   </property>
+  </widget>
+  <widget class="QWidget" name="verticalLayoutWidget">
+   <property name="geometry">
+    <rect>
+     <x>140</x>
+     <y>220</y>
+     <width>411</width>
+     <height>159</height>
+    </rect>
+   </property>
+   <layout class="QVBoxLayout" name="vbOptions">
+    <item>
+     <widget class="QCheckBox" name="cbConfigOS">
+      <property name="enabled">
+       <bool>true</bool>
+      </property>
+      <property name="text">
+       <string>从操作系统配置</string>
+      </property>
+     </widget>
+    </item>
+    <item>
+     <widget class="QCheckBox" name="cbListenOnly">
+      <property name="enabled">
+       <bool>true</bool>
+      </property>
+      <property name="text">
+       <string>监听模式</string>
+      </property>
+     </widget>
+    </item>
+    <item>
+     <widget class="QCheckBox" name="cbOneShot">
+      <property name="enabled">
+       <bool>true</bool>
+      </property>
+      <property name="text">
+       <string>One-Shot mode</string>
+      </property>
+     </widget>
+    </item>
+    <item>
+     <widget class="QCheckBox" name="cbTripleSampling">
+      <property name="enabled">
+       <bool>true</bool>
+      </property>
+      <property name="text">
+       <string>Triple Sampling</string>
+      </property>
+     </widget>
+    </item>
+    <item>
+     <widget class="QCheckBox" name="cbAutoRestart">
+      <property name="enabled">
+       <bool>true</bool>
+      </property>
+      <property name="text">
+       <string>掉线重新启动</string>
+      </property>
+     </widget>
+    </item>
+   </layout>
+  </widget>
+ </widget>
+ <resources/>
+ <connections/>
+</ui>

+ 107 - 0
src/tool/adc_cantool/driver/SLCANDriver/SLCANDriver.cpp

@@ -0,0 +1,107 @@
+/*
+
+  Copyright (c) 2022 Ethan Zonca
+
+  This file is part of cangaroo.
+
+  cangaroo is free software: you can redistribute it and/or modify
+  it under the terms of the GNU General Public License as published by
+  the Free Software Foundation, either version 2 of the License, or
+  (at your option) any later version.
+
+  cangaroo 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 General Public License for more details.
+
+  You should have received a copy of the GNU General Public License
+  along with cangaroo.  If not, see <http://www.gnu.org/licenses/>.
+
+*/
+
+
+#include "SLCANDriver.h"
+#include "SLCANInterface.h"
+#include <core/Backend.h>
+#include <driver/GenericCanSetupPage.h>
+
+#include <errno.h>
+#include <cstring>
+#include <stdio.h>
+#include <unistd.h>
+#include <string.h>
+
+//
+#include <QCoreApplication>
+#include <QDebug>
+#include <QtSerialPort/QSerialPort>
+#include <QtSerialPort/QSerialPortInfo>
+
+SLCANDriver::SLCANDriver(Backend &backend)
+  : CanDriver(backend),
+    setupPage(new GenericCanSetupPage())
+{
+    QObject::connect(&backend, SIGNAL(onSetupDialogCreated(SetupDialog&)), setupPage, SLOT(onSetupDialogCreated(SetupDialog&)));
+}
+
+SLCANDriver::~SLCANDriver() {
+}
+
+bool SLCANDriver::update() {
+
+    deleteAllInterfaces();
+
+    int interface_cnt = 0;
+
+    foreach (const QSerialPortInfo &info, QSerialPortInfo::availablePorts()) {
+        fprintf(stderr, "Name : %s \r\n",  info.portName().toStdString().c_str());
+        fprintf(stderr, "   Description : %s \r\n", info.description().toStdString().c_str());
+        fprintf(stderr, "   Manufacturer: %s \r\n", info.manufacturer().toStdString().c_str());
+
+        if(info.vendorIdentifier() == 0xad50 && info.productIdentifier() == 0x60C4)
+        {
+
+            perror("   ++ CANable 1.0 or similar ST USB CDC device detected");
+
+            // Create new slcan interface without FD support
+            SLCANInterface *intf = createOrUpdateInterface(interface_cnt, info.portName(), false);
+            interface_cnt++;
+        }
+        else if(info.vendorIdentifier() == 0x16D0 && info.productIdentifier() == 0x117E)
+        {
+            perror("   ++ CANable 2.0 detected");
+
+            // Create new slcan interface without FD support
+            SLCANInterface *intf = createOrUpdateInterface(interface_cnt, info.portName(), true);
+            interface_cnt++;
+        }
+        else
+        {
+            perror("   !! This is not a CANable device!");
+        }
+    }
+
+    return true;
+}
+
+QString SLCANDriver::getName() {
+    return "CANable SLCAN";
+}
+
+
+
+SLCANInterface *SLCANDriver::createOrUpdateInterface(int index, QString name, bool fd_support) {
+
+    foreach (CanInterface *intf, getInterfaces()) {
+        SLCANInterface *scif = dynamic_cast<SLCANInterface*>(intf);
+		if (scif->getIfIndex() == index) {
+			scif->setName(name);
+            return scif;
+		}
+	}
+
+
+    SLCANInterface *scif = new SLCANInterface(this, index, name, fd_support);
+    addInterface(scif);
+    return scif;
+}

+ 43 - 0
src/tool/adc_cantool/driver/SLCANDriver/SLCANDriver.h

@@ -0,0 +1,43 @@
+/*
+
+  Copyright (c) 2015, 2016 Hubert Denkmair
+
+  This file is part of cangaroo.
+
+  cangaroo is free software: you can redistribute it and/or modify
+  it under the terms of the GNU General Public License as published by
+  the Free Software Foundation, either version 2 of the License, or
+  (at your option) any later version.
+
+  cangaroo 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 General Public License for more details.
+
+  You should have received a copy of the GNU General Public License
+  along with cangaroo.  If not, see <http://www.gnu.org/licenses/>.
+
+*/
+
+#pragma once
+
+#include <QString>
+#include <core/Backend.h>
+#include <driver/CanDriver.h>
+
+class SLCANInterface;
+class SetupDialogInterfacePage;
+class GenericCanSetupPage;
+
+class SLCANDriver: public CanDriver {
+public:
+    SLCANDriver(Backend &backend);
+    virtual ~SLCANDriver();
+
+    virtual QString getName();
+    virtual bool update();
+
+private:
+    SLCANInterface *createOrUpdateInterface(int index, QString name, bool fd_support);
+    GenericCanSetupPage *setupPage;
+};

+ 12 - 0
src/tool/adc_cantool/driver/SLCANDriver/SLCANDriver.pri

@@ -0,0 +1,12 @@
+CONFIG += c++11
+
+SOURCES += \
+    $$PWD/SLCANInterface.cpp \
+    $$PWD/SLCANDriver.cpp
+
+HEADERS  += \
+    $$PWD/SLCANInterface.h \
+    $$PWD/SLCANDriver.h
+
+FORMS +=
+

+ 736 - 0
src/tool/adc_cantool/driver/SLCANDriver/SLCANInterface.cpp

@@ -0,0 +1,736 @@
+/*
+
+  Copyright (c) 2022 Ethan Zonca
+
+  This file is part of cangaroo.
+
+  cangaroo is free software: you can redistribute it and/or modify
+  it under the terms of the GNU General Public License as published by
+  the Free Software Foundation, either version 2 of the License, or
+  (at your option) any later version.
+
+  cangaroo 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 General Public License for more details.
+
+  You should have received a copy of the GNU General Public License
+  along with cangaroo.  If not, see <http://www.gnu.org/licenses/>.
+
+*/
+
+#include "SLCANInterface.h"
+
+#include <core/Backend.h>
+#include <core/MeasurementInterface.h>
+#include <core/CanMessage.h>
+
+#include <stdio.h>
+#include <unistd.h>
+#include <time.h>
+#include <fcntl.h>
+#include <QString>
+#include <QStringList>
+#include <QProcess>
+#include <QtSerialPort/QSerialPort>
+#include <QtSerialPort/QSerialPortInfo>
+#include <QThread>
+
+SLCANInterface::SLCANInterface(SLCANDriver *driver, int index, QString name, bool fd_support)
+  : CanInterface((CanDriver *)driver),
+	_idx(index),
+    _isOpen(false),
+    _serport(NULL),
+    _msg_queue(),
+    _name(name),
+    _rx_linbuf_ctr(0),
+    _rxbuf_head(0),
+    _rxbuf_tail(0),
+    _ts_mode(ts_mode_SIOCSHWTSTAMP)
+{
+    // Set defaults
+    _settings.setBitrate(500000);
+    _settings.setSamplePoint(875);
+
+    _config.supports_canfd = fd_support;
+}
+
+SLCANInterface::~SLCANInterface() {
+}
+
+QString SLCANInterface::getDetailsStr() const {
+    if(_config.supports_canfd)
+    {
+        return "CANable with CANFD support";
+    }
+    else
+    {
+        return "CANable with standard CAN support";
+    }
+}
+
+QString SLCANInterface::getName() const {
+	return _name;
+}
+
+void SLCANInterface::setName(QString name) {
+    _name = name;
+}
+
+QList<CanTiming> SLCANInterface::getAvailableBitrates()
+{
+    QList<CanTiming> retval;
+    QList<unsigned> bitrates({10000, 20000, 50000, 83333, 100000, 125000, 250000, 500000, 800000, 1000000});
+    QList<unsigned> bitrates_fd({0, 2000000, 5000000});
+
+    QList<unsigned> samplePoints({875});
+
+    unsigned i=0;
+    foreach (unsigned br, bitrates) {
+        foreach(unsigned br_fd, bitrates_fd) {
+            foreach (unsigned sp, samplePoints) {
+                retval << CanTiming(i++, br, br_fd, sp);
+            }
+        }
+    }
+
+    return retval;
+}
+
+
+void SLCANInterface::applyConfig(const MeasurementInterface &mi)
+{
+    // Save settings for port configuration
+    _settings = mi;
+}
+
+bool SLCANInterface::updateStatus()
+{
+    return false;
+}
+
+bool SLCANInterface::readConfig()
+{
+    return false;
+}
+
+bool SLCANInterface::readConfigFromLink(rtnl_link *link)
+{
+    return false;
+}
+
+bool SLCANInterface::supportsTimingConfiguration()
+{
+    return _config.supports_timing;
+}
+
+bool SLCANInterface::supportsCanFD()
+{
+    return _config.supports_canfd;
+}
+
+bool SLCANInterface::supportsTripleSampling()
+{
+    return false;
+}
+
+unsigned SLCANInterface::getBitrate()
+{
+    return _settings.bitrate();
+}
+
+uint32_t SLCANInterface::getCapabilities()
+{
+    uint32_t retval =
+        CanInterface::capability_config_os |
+        CanInterface::capability_listen_only |
+        CanInterface::capability_auto_restart;
+
+    if (supportsCanFD()) {
+        retval |= CanInterface::capability_canfd;
+    }
+
+    if (supportsTripleSampling()) {
+        retval |= CanInterface::capability_triple_sampling;
+    }
+
+    return retval;
+}
+
+bool SLCANInterface::updateStatistics()
+{
+    return updateStatus();
+}
+
+uint32_t SLCANInterface::getState()
+{
+    if(_isOpen)
+        return state_ok;
+    else
+        return state_bus_off;
+}
+
+int SLCANInterface::getNumRxFrames()
+{
+    return _status.rx_count;
+}
+
+int SLCANInterface::getNumRxErrors()
+{
+    return _status.rx_errors;
+}
+
+int SLCANInterface::getNumTxFrames()
+{
+    return _status.tx_count;
+}
+
+int SLCANInterface::getNumTxErrors()
+{
+    return _status.tx_errors;
+}
+
+int SLCANInterface::getNumRxOverruns()
+{
+    return _status.rx_overruns;
+}
+
+int SLCANInterface::getNumTxDropped()
+{
+    return _status.tx_dropped;
+}
+
+int SLCANInterface::getIfIndex() {
+    return _idx;
+}
+
+void SLCANInterface::open()
+{
+    if(_serport != NULL)
+    {
+        delete _serport;
+    }
+
+    _serport = new QSerialPort();
+
+    _serport_mutex.lock();
+    _serport->setPortName(_name);
+    _serport->setBaudRate(1000000);
+    _serport->setDataBits(QSerialPort::Data8);
+    _serport->setParity(QSerialPort::NoParity);
+    _serport->setStopBits(QSerialPort::OneStop);
+    _serport->setFlowControl(QSerialPort::NoFlowControl);
+    _serport->setReadBufferSize(2048);
+    if (_serport->open(QIODevice::ReadWrite)) {
+        //perror("Serport connected!");
+    } else {
+        perror("Serport connect failed!");
+        _serport_mutex.unlock();
+        _isOpen = false;
+        return;
+    }
+    _serport->flush();
+    _serport->clear();
+
+    // Set the classic CAN bitrate
+    switch(_settings.bitrate())
+    {
+        case 1000000:
+            _serport->write("S8\r", 3);
+            _serport->flush();
+            break;
+        case 750000:
+            _serport->write("S7\r", 3);
+            _serport->flush();
+            break;
+        case 500000:
+            _serport->write("S6\r", 3);
+            _serport->flush();
+            break;
+        case 250000:
+            _serport->write("S5\r", 3);
+            _serport->flush();
+            break;
+        case 125000:
+            _serport->write("S4\r", 3);
+            _serport->flush();
+            break;
+        case 100000:
+            _serport->write("S3\r", 3);
+            _serport->flush();
+            break;
+        case 83333:
+            _serport->write("S9\r", 3);
+            _serport->flush();
+            break;
+        case 50000:
+            _serport->write("S2\r", 3);
+            _serport->flush();
+            break;
+        case 20000:
+            _serport->write("S1\r", 3);
+            _serport->flush();
+            break;
+        case 10000:
+            _serport->write("S0\r", 3);
+            _serport->flush();
+            break;
+        default:
+            // Default to 10k
+            _serport->write("S0\r", 3);
+            _serport->flush();
+            break;
+    }
+
+    _serport->waitForBytesWritten(300);
+
+
+
+    // Set configured BRS rate
+    if(_config.supports_canfd)
+    {
+        switch(_settings.fdBitrate())
+        {
+            case 2000000:
+                _serport->write("Y2\r", 3);
+                _serport->flush();
+                break;
+            case 5000000:
+                _serport->write("Y5\r", 3);
+                _serport->flush();
+                break;
+        }
+    }
+
+    _serport->waitForBytesWritten(300);
+
+
+    // Open the port
+    _serport->write("O\r\n", 3);
+    _serport->flush();
+
+    _isOpen = true;
+
+    // Release port mutex
+    _serport_mutex.unlock();
+}
+
+void SLCANInterface::close()
+{
+    _serport_mutex.lock();
+
+    if (_serport->isOpen())
+    {
+        // Close CAN port
+        _serport->write("C\r", 2);        
+        _serport->flush();
+        _serport->waitForBytesWritten(300);
+        _serport->clear();
+        _serport->close();
+    }
+
+    _isOpen = false;
+    _serport_mutex.unlock();
+}
+
+bool SLCANInterface::isOpen()
+{
+    return _isOpen;
+}
+
+void SLCANInterface::sendMessage(const CanMessage &msg) {
+
+    // SLCAN_MTU plus null terminator
+    char buf[SLCAN_MTU+1] = {0};
+
+    uint8_t msg_idx = 0;
+
+    // Message is FD
+    // Add character for frame type
+    if(msg.isFD())
+    {
+        if(msg.isBRS())
+        {
+            buf[msg_idx] = 'b';
+
+        }
+        else
+        {
+            buf[msg_idx] = 'd';
+        }
+    }
+    // Message is not FD
+    // Add character for frame type
+    else
+    {
+        if (msg.isRTR()) {
+            buf[msg_idx] = 'r';
+        }
+        else
+        {
+            buf[msg_idx] = 't';
+        }
+    }
+
+    // Assume standard identifier
+    uint8_t id_len = SLCAN_STD_ID_LEN;
+    uint32_t tmp = msg.getId();
+
+    // Check if extended
+    if (msg.isExtended())
+    {
+        // Convert first char to upper case for extended frame
+        buf[msg_idx] -= 32;
+        id_len = SLCAN_EXT_ID_LEN;
+    }
+    msg_idx++;
+
+    // Add identifier to buffer
+    for(uint8_t j = id_len; j > 0; j--)
+    {
+        // Add nibble to buffer
+        buf[j] = (tmp & 0xF);
+        tmp = tmp >> 4;
+        msg_idx++;
+    }
+
+    // Sanity check length
+    int8_t bytes = msg.getLength();
+
+
+    if(bytes < 0)
+        return;
+    if(bytes > 64)
+        return;
+
+    // If canfd
+    if(bytes > 8)
+    {
+        switch(bytes)
+        {
+        case 12:
+            bytes = 0x9;
+            break;
+        case 16:
+            bytes = 0xA;
+            break;
+        case 20:
+            bytes = 0xB;
+            break;
+        case 24:
+            bytes = 0xC;
+            break;
+        case 32:
+            bytes = 0xD;
+            break;
+        case 48:
+            bytes = 0xE;
+            break;
+        case 64:
+            bytes = 0xF;
+            break;
+        }
+    }
+
+    // Add DLC to buffer
+    buf[msg_idx++] = bytes;
+
+    // Add data bytes
+    for (uint8_t j = 0; j < msg.getLength(); j++)
+    {
+        buf[msg_idx++] = (msg.getByte(j) >> 4);
+        buf[msg_idx++] = (msg.getByte(j) & 0x0F);
+    }
+
+    // Convert to ASCII (2nd character to end)
+    for (uint8_t j = 1; j < msg_idx; j++)
+    {
+        if (buf[j] < 0xA) {
+            buf[j] += 0x30;
+        } else {
+            buf[j] += 0x37;
+        }
+    }
+
+    // Add CR for slcan EOL
+    buf[msg_idx++] = '\r';
+
+    // Ensure null termination
+    buf[msg_idx] = '\0';
+
+    _msg_queue.append(QString(buf));
+
+}
+
+bool SLCANInterface::readMessage(CanMessage &msg, unsigned int timeout_ms)
+{
+    // Don't saturate the thread. Read the buffer every 1ms.
+    QThread().msleep(1);
+
+    // Transmit all items that are queued
+    while(!_msg_queue.empty())
+    {
+        // Consume first item
+        QString tmp = _msg_queue.front();
+        _msg_queue.pop_front();
+
+        _serport_mutex.lock();
+        // Write string to serial device
+        _serport->write(tmp.toStdString().c_str(), tmp.length());
+        _serport->flush();
+        _serport->waitForBytesWritten(300);
+        _serport_mutex.unlock();
+    }
+
+    // RX doesn't work on windows unless we call this for some reason
+    _serport->waitForReadyRead(1);
+
+    if(_serport->bytesAvailable())
+    {
+        // This is called when readyRead() is emitted
+        QByteArray datas = _serport->readAll();
+        _rxbuf_mutex.lock();
+        for(int i=0; i<datas.count(); i++)
+        {
+            // If incrementing the head will hit the tail, we've filled the buffer. Reset and discard all data.
+            if(((_rxbuf_head + 1) % RXCIRBUF_LEN) == _rxbuf_tail)
+            {
+                _rxbuf_head = 0;
+                _rxbuf_tail = 0;
+            }
+            else
+            {
+                // Put inbound data at the head locatoin
+                _rxbuf[_rxbuf_head] = datas.at(i);
+                _rxbuf_head = (_rxbuf_head + 1) % RXCIRBUF_LEN; // Wrap at MTU
+            }
+        }
+        _rxbuf_mutex.unlock();
+    }
+
+
+
+    //////////////////////////
+
+    bool ret = false;
+    _rxbuf_mutex.lock();
+    while(_rxbuf_tail != _rxbuf_head)
+    {
+        // Save data if room
+        if(_rx_linbuf_ctr < SLCAN_MTU)
+        {
+            _rx_linbuf[_rx_linbuf_ctr++] = _rxbuf[_rxbuf_tail];
+
+            // If we have a newline, then we just finished parsing a CAN message.
+            if(_rxbuf[_rxbuf_tail] == '\r')
+            {
+                ret = parseMessage(msg);
+                _rx_linbuf_ctr = 0;
+            }
+        }
+        // Discard data if not
+        else
+        {
+            perror("Linbuf full");
+            _rx_linbuf_ctr = 0;
+        }
+
+        _rxbuf_tail = (_rxbuf_tail + 1) % RXCIRBUF_LEN;
+    }
+    _rxbuf_mutex.unlock();
+
+    return ret;
+}
+
+bool SLCANInterface::parseMessage(CanMessage &msg)
+{
+    // Set timestamp to current time
+    struct timeval tv;
+    gettimeofday(&tv,NULL);
+    msg.setTimestamp(tv);
+
+    // Defaults
+    msg.setErrorFrame(0);
+    msg.setInterfaceId(getId());
+    msg.setId(0);
+
+    bool msg_is_fd = false;
+
+    // Convert from ASCII (2nd character to end)
+    for (int i = 1; i < _rx_linbuf_ctr; i++)
+    {
+        // Lowercase letters
+        if(_rx_linbuf[i] >= 'a')
+            _rx_linbuf[i] = _rx_linbuf[i] - 'a' + 10;
+        // Uppercase letters
+        else if(_rx_linbuf[i] >= 'A')
+            _rx_linbuf[i] = _rx_linbuf[i] - 'A' + 10;
+        // Numbers
+        else
+            _rx_linbuf[i] = _rx_linbuf[i] - '0';
+    }
+
+
+    // Handle each incoming command
+    switch(_rx_linbuf[0])
+    {
+
+        // Transmit data frame command
+        case 'T':
+            msg.setExtended(1);
+            break;
+        case 't':
+            msg.setExtended(0);
+            break;
+
+        // Transmit remote frame command
+        case 'r':
+            msg.setExtended(0);
+            msg.setRTR(1);
+            break;
+        case 'R':
+            msg.setExtended(1);
+            msg.setRTR(1);
+            break;
+
+        // CANFD transmit - no BRS
+        case 'd':
+            msg.setExtended(0);
+            msg_is_fd = true;
+            break;
+        case 'D':
+            msg.setExtended(1);
+            msg_is_fd = true;
+            break;
+
+        // CANFD transmit - with BRS
+        case 'b':
+            msg.setExtended(0);
+            msg_is_fd = true;
+            break;
+        case 'B':
+            msg.setExtended(1);
+            msg_is_fd = true;
+            break;
+
+
+
+        // Invalid command
+        default:
+            return false;
+    }
+
+    // Start parsing at second byte (skip command byte)
+    uint8_t parse_loc = 1;
+
+    // Default to standard id len
+    uint8_t id_len = SLCAN_STD_ID_LEN;
+
+    // Update length if message is extended ID
+    if(msg.isExtended())
+        id_len = SLCAN_EXT_ID_LEN;
+
+    uint32_t id_tmp = 0;
+
+    // Iterate through ID bytes
+    while(parse_loc <= id_len)
+    {
+        id_tmp *= 16;
+        id_tmp += _rx_linbuf[parse_loc++];
+    }
+
+
+    msg.setId(id_tmp);
+
+    // Attempt to parse DLC and check sanity
+    uint8_t dlc_code_raw = _rx_linbuf[parse_loc++];
+
+    // If dlc is too long for an FD frame
+    if(msg_is_fd && dlc_code_raw > 0xF)
+    {
+        return false;
+    }
+    if(!msg_is_fd && dlc_code_raw > 0x8)
+    {
+        return false;
+    }
+
+    if(dlc_code_raw > 0x8)
+    {
+        switch(dlc_code_raw)
+        {
+        case 0x9:
+            dlc_code_raw = 12;
+            break;
+        case 0xA:
+            dlc_code_raw = 16;
+            break;
+        case 0xB:
+            dlc_code_raw = 20;
+            break;
+        case 0xC:
+            dlc_code_raw = 24;
+            break;
+        case 0xD:
+            dlc_code_raw = 32;
+            break;
+        case 0xE:
+            dlc_code_raw = 48;
+            break;
+        case 0xF:
+            dlc_code_raw = 64;
+            break;
+        default:
+            dlc_code_raw = 0;
+            perror("Invalid length");
+            break;
+        }
+    }
+
+    msg.setLength(dlc_code_raw);
+
+    // Calculate number of bytes we expect in the message
+    int8_t bytes_in_msg = dlc_code_raw;
+
+    if(bytes_in_msg < 0)
+        return false;
+    if(bytes_in_msg > 64)
+        return false;
+
+    // Parse data
+    // TODO: Guard against walking off the end of the string!
+    for (uint8_t i = 0; i < bytes_in_msg; i++)
+    {
+        msg.setByte(i,  (_rx_linbuf[parse_loc] << 4) + _rx_linbuf[parse_loc+1]);
+        parse_loc += 2;
+    }
+
+    // Reset buffer
+    _rx_linbuf_ctr = 0;
+    _rx_linbuf[0] = '\0';
+    return true;
+
+
+/*
+
+    // FIXME
+    if (_ts_mode == ts_mode_SIOCSHWTSTAMP) {
+        // TODO implement me
+        _ts_mode = ts_mode_SIOCGSTAMPNS;
+    }
+
+    if (_ts_mode==ts_mode_SIOCGSTAMPNS) {
+        if (ioctl(_fd, SIOCGSTAMPNS, &ts_rcv) == 0) {
+            msg.setTimestamp(ts_rcv.tv_sec, ts_rcv.tv_nsec/1000);
+        } else {
+            _ts_mode = ts_mode_SIOCGSTAMP;
+        }
+    }
+
+    if (_ts_mode==ts_mode_SIOCGSTAMP) {
+        ioctl(_fd, SIOCGSTAMP, &tv_rcv);
+        msg.setTimestamp(tv_rcv.tv_sec, tv_rcv.tv_usec);
+    }*/
+
+
+}

+ 133 - 0
src/tool/adc_cantool/driver/SLCANDriver/SLCANInterface.h

@@ -0,0 +1,133 @@
+/*
+
+  Copyright (c) 2015, 2016 Hubert Denkmair
+
+  This file is part of cangaroo.
+
+  cangaroo is free software: you can redistribute it and/or modify
+  it under the terms of the GNU General Public License as published by
+  the Free Software Foundation, either version 2 of the License, or
+  (at your option) any later version.
+
+  cangaroo 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 General Public License for more details.
+
+  You should have received a copy of the GNU General Public License
+  along with cangaroo.  If not, see <http://www.gnu.org/licenses/>.
+
+*/
+
+#pragma once
+
+#include "../CanInterface.h"
+#include <core/MeasurementInterface.h>
+#include <QtSerialPort/QSerialPort>
+#include <QtSerialPort/QSerialPortInfo>
+#include <QMutex>
+
+// Maximum rx buffer len
+#define SLCAN_MTU 138 + 1 + 16 // canfd 64 frame plus \r plus some padding
+#define SLCAN_STD_ID_LEN 3
+#define SLCAN_EXT_ID_LEN 8
+
+#define RXCIRBUF_LEN 8192 // Buffer for received serial data, serviced at 1ms intervals
+
+class SLCANDriver;
+
+typedef struct {
+    bool supports_canfd;
+    bool supports_timing;
+    uint32_t state;
+    uint32_t base_freq;
+    uint32_t sample_point;
+    uint32_t ctrl_mode;
+    uint32_t restart_ms;
+} can_config_t;
+
+typedef struct {
+    uint32_t can_state;
+
+    uint64_t rx_count;
+    int rx_errors;
+    uint64_t rx_overruns;
+
+    uint64_t tx_count;
+    int tx_errors;
+    uint64_t tx_dropped;
+} can_status_t;
+
+class SLCANInterface: public CanInterface {
+public:
+    SLCANInterface(SLCANDriver *driver, int index, QString name, bool fd_support);
+    virtual ~SLCANInterface();
+
+    QString getDetailsStr() const;
+    virtual QString getName() const;
+    void setName(QString name);
+
+    virtual QList<CanTiming> getAvailableBitrates();
+
+    virtual void applyConfig(const MeasurementInterface &mi);
+    virtual bool readConfig();
+    virtual bool readConfigFromLink(struct rtnl_link *link);
+
+    bool supportsTimingConfiguration();
+    bool supportsCanFD();
+    bool supportsTripleSampling();
+
+    virtual unsigned getBitrate();
+    virtual uint32_t getCapabilities();
+
+
+	virtual void open();
+    virtual void close();
+    virtual bool isOpen();
+
+    virtual void sendMessage(const CanMessage &msg);
+    virtual bool readMessage(CanMessage &msg, unsigned int timeout_ms);
+
+    virtual bool updateStatistics();
+    virtual uint32_t getState();
+    virtual int getNumRxFrames();
+    virtual int getNumRxErrors();
+    virtual int getNumRxOverruns();
+
+    virtual int getNumTxFrames();
+    virtual int getNumTxErrors();
+    virtual int getNumTxDropped();
+
+    int getIfIndex();
+
+private:
+    typedef enum {
+        ts_mode_SIOCSHWTSTAMP,
+        ts_mode_SIOCGSTAMPNS,
+        ts_mode_SIOCGSTAMP
+    } ts_mode_t;
+
+    int _idx;
+    bool _isOpen;
+    QSerialPort* _serport;
+    QStringList _msg_queue;
+    QMutex _serport_mutex;
+    QString _name;
+    char _rx_linbuf[SLCAN_MTU];
+    int _rx_linbuf_ctr;
+
+    char _rxbuf[RXCIRBUF_LEN];
+    uint32_t _rxbuf_head;
+    uint32_t _rxbuf_tail;
+
+    QMutex _rxbuf_mutex;
+    MeasurementInterface _settings;
+
+    can_config_t _config;
+    can_status_t _status;
+    ts_mode_t _ts_mode;
+
+    bool updateStatus();
+    bool parseMessage(CanMessage &msg);
+
+};

+ 99 - 0
src/tool/adc_cantool/driver/SocketCanDriver/SocketCanDriver.cpp

@@ -0,0 +1,99 @@
+/*
+
+  Copyright (c) 2015, 2016 Hubert Denkmair
+
+  This file is part of cangaroo.
+
+  cangaroo is free software: you can redistribute it and/or modify
+  it under the terms of the GNU General Public License as published by
+  the Free Software Foundation, either version 2 of the License, or
+  (at your option) any later version.
+
+  cangaroo 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 General Public License for more details.
+
+  You should have received a copy of the GNU General Public License
+  along with cangaroo.  If not, see <http://www.gnu.org/licenses/>.
+
+*/
+
+
+#include "SocketCanDriver.h"
+#include "SocketCanInterface.h"
+#include <core/Backend.h>
+#include <driver/GenericCanSetupPage.h>
+
+#include <sys/socket.h>
+#include <linux/if.h>
+#include <linux/if_arp.h>
+#include <linux/can/netlink.h>
+#include <netlink/route/link.h>
+#include <netlink/route/link/can.h>
+#include <errno.h>
+#include <cstring>
+#include <stdio.h>
+#include <unistd.h>
+#include <string.h>
+
+SocketCanDriver::SocketCanDriver(Backend &backend)
+  : CanDriver(backend),
+    setupPage(new GenericCanSetupPage())
+{
+    QObject::connect(&backend, SIGNAL(onSetupDialogCreated(SetupDialog&)), setupPage, SLOT(onSetupDialogCreated(SetupDialog&)));
+}
+
+SocketCanDriver::~SocketCanDriver() {
+}
+
+bool SocketCanDriver::update() {
+
+    deleteAllInterfaces();
+
+    struct nl_sock *sock = nl_socket_alloc();
+    struct nl_cache *cache;
+
+    nl_connect(sock, NETLINK_ROUTE);
+    int result = rtnl_link_alloc_cache(sock, AF_UNSPEC, &cache);
+
+    if (result < 0) {
+        log_error(QString("Could not access netlink device list: %1").arg(result));
+    } else {
+
+        for (struct nl_object *obj = nl_cache_get_first(cache); obj!=0; obj=nl_cache_get_next(obj)) {
+            struct rtnl_link *link = (struct rtnl_link *)obj;
+
+            if (rtnl_link_get_arptype(link)==ARPHRD_CAN) {
+                SocketCanInterface *intf = createOrUpdateInterface(rtnl_link_get_ifindex(link), QString(rtnl_link_get_name(link)));
+                intf->readConfigFromLink(link);
+            }
+        }
+    }
+
+    nl_cache_free(cache);
+    nl_close(sock);
+    nl_socket_free(sock);
+
+    return true;
+}
+
+QString SocketCanDriver::getName() {
+	return "SocketCAN";
+}
+
+SocketCanInterface *SocketCanDriver::createOrUpdateInterface(int index, QString name) {
+
+    foreach (CanInterface *intf, getInterfaces()) {
+        SocketCanInterface *scif = dynamic_cast<SocketCanInterface*>(intf);
+		if (scif->getIfIndex() == index) {
+			scif->setName(name);
+            return scif;
+		}
+	}
+
+
+    SocketCanInterface *scif = new SocketCanInterface(this, index, name);
+    addInterface(scif);
+    return scif;
+}

+ 43 - 0
src/tool/adc_cantool/driver/SocketCanDriver/SocketCanDriver.h

@@ -0,0 +1,43 @@
+/*
+
+  Copyright (c) 2015, 2016 Hubert Denkmair
+
+  This file is part of cangaroo.
+
+  cangaroo is free software: you can redistribute it and/or modify
+  it under the terms of the GNU General Public License as published by
+  the Free Software Foundation, either version 2 of the License, or
+  (at your option) any later version.
+
+  cangaroo 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 General Public License for more details.
+
+  You should have received a copy of the GNU General Public License
+  along with cangaroo.  If not, see <http://www.gnu.org/licenses/>.
+
+*/
+
+#pragma once
+
+#include <QString>
+#include <core/Backend.h>
+#include <driver/CanDriver.h>
+
+class SocketCanInterface;
+class SetupDialogInterfacePage;
+class GenericCanSetupPage;
+
+class SocketCanDriver: public CanDriver {
+public:
+    SocketCanDriver(Backend &backend);
+    virtual ~SocketCanDriver();
+
+    virtual QString getName();
+    virtual bool update();
+
+private:
+    SocketCanInterface *createOrUpdateInterface(int index, QString name);
+    GenericCanSetupPage *setupPage;
+};

+ 12 - 0
src/tool/adc_cantool/driver/SocketCanDriver/SocketCanDriver.pri

@@ -0,0 +1,12 @@
+CONFIG += c++11
+
+SOURCES += \
+    $$PWD/SocketCanInterface.cpp \
+    $$PWD/SocketCanDriver.cpp
+
+HEADERS  += \
+    $$PWD/SocketCanInterface.h \
+    $$PWD/SocketCanDriver.h
+
+FORMS +=
+

+ 473 - 0
src/tool/adc_cantool/driver/SocketCanDriver/SocketCanInterface.cpp

@@ -0,0 +1,473 @@
+/*
+
+  Copyright (c) 2015, 2016 Hubert Denkmair
+
+  This file is part of cangaroo.
+
+  cangaroo is free software: you can redistribute it and/or modify
+  it under the terms of the GNU General Public License as published by
+  the Free Software Foundation, either version 2 of the License, or
+  (at your option) any later version.
+
+  cangaroo 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 General Public License for more details.
+
+  You should have received a copy of the GNU General Public License
+  along with cangaroo.  If not, see <http://www.gnu.org/licenses/>.
+
+*/
+
+#include "SocketCanInterface.h"
+
+#include <core/Backend.h>
+#include <core/MeasurementInterface.h>
+#include <core/CanMessage.h>
+
+#include <stdio.h>
+#include <unistd.h>
+#include <time.h>
+#include <QString>
+#include <QStringList>
+#include <QProcess>
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <sys/ioctl.h>
+#include <sys/time.h>
+
+#include <linux/if.h>
+#include <linux/can.h>
+#include <linux/can/raw.h>
+#include <linux/can/netlink.h>
+#include <linux/sockios.h>
+#include <netlink/version.h>
+#include <netlink/route/link.h>
+#include <netlink/route/link/can.h>
+
+SocketCanInterface::SocketCanInterface(SocketCanDriver *driver, int index, QString name)
+  : CanInterface((CanDriver *)driver),
+	_idx(index),
+    _isOpen(false),
+	_fd(0),
+    _name(name),
+    _ts_mode(ts_mode_SIOCSHWTSTAMP)
+{
+}
+
+SocketCanInterface::~SocketCanInterface() {
+}
+
+QString SocketCanInterface::getName() const {
+	return _name;
+}
+
+void SocketCanInterface::setName(QString name) {
+    _name = name;
+}
+
+QList<CanTiming> SocketCanInterface::getAvailableBitrates()
+{
+    QList<CanTiming> retval;
+    QList<unsigned> bitrates({10000, 20000, 50000, 83333, 100000, 125000, 250000, 500000, 800000, 1000000,2000000,4000000});//cxw,增加2个波特率
+    QList<unsigned> samplePoints({500, 625, 750,  875});
+
+    unsigned i=0;
+    foreach (unsigned br, bitrates) {
+        foreach (unsigned sp, samplePoints) {
+            if(this->_config.supports_canfd)
+                retval << CanTiming(i++, br, br, sp);//CanTiming(i++, br, 0, sp);//将fd的加入否则不支持FD
+            else
+               retval << CanTiming(i++, br, 0, sp);
+        }
+    }
+
+    return retval;
+}
+
+QString SocketCanInterface::buildIpRouteCmd(const MeasurementInterface &mi)
+{
+    QStringList cmd;
+    cmd.append("ip");
+    cmd.append("link");
+    cmd.append("set");
+    cmd.append(getName());
+    cmd.append("up");
+    cmd.append("type");
+    cmd.append("can");
+
+    cmd.append("bitrate");
+    cmd.append(QString().number(mi.bitrate()));
+    cmd.append("sample-point");
+    cmd.append(QString().number((float)mi.samplePoint()/1000.0, 'f', 3));
+
+    if (mi.isCanFD()) {
+        cmd.append("dbitrate");
+        cmd.append(QString().number(mi.fdBitrate()));
+        cmd.append("dsample-point");
+        cmd.append(QString().number((float)mi.fdSamplePoint()/1000.0, 'f', 3));
+        cmd.append("fd");
+        cmd.append("on");
+    }
+
+    cmd.append("restart-ms");
+    if (mi.doAutoRestart()) {
+        cmd.append(QString().number(mi.autoRestartMs()));
+    } else {
+        cmd.append("0");
+    }
+
+    return cmd.join(' ');
+}
+
+QStringList SocketCanInterface::buildCanIfConfigArgs(const MeasurementInterface &mi)
+{
+    QStringList args;
+    args << "-d";
+    args << "-i" << getName();
+    args << "-b" << QString::number(mi.bitrate());
+    args << "-p" << QString::number(mi.samplePoint());
+    args << "-u";
+    return args;
+}
+
+
+void SocketCanInterface::applyConfig(const MeasurementInterface &mi)
+{
+    if (!mi.doConfigure()) {
+        log_info(QString("interface %1 not managed by cangaroo, not touching configuration").arg(getName()));
+        return;
+    }
+
+    log_info(QString("calling canifconfig to reconfigure interface %1").arg(getName()));
+    QStringList sl = buildCanIfConfigArgs(mi);
+    sl.prepend("canifconfig");
+    log_info(sl.join(" "));
+
+    QProcess canIfConfig;
+    canIfConfig.start("canifconfig", buildCanIfConfigArgs(mi));
+    if (!canIfConfig.waitForFinished()) {
+        log_error(QString("timeout waiting for canifconfig"));
+        return;
+    }
+
+    if (canIfConfig.exitStatus()!=QProcess::NormalExit) {
+        log_error(QString("canifconfig crashed"));
+        return;
+    }
+
+    if (canIfConfig.exitCode() != 0) {
+        log_error(QString("canifconfig failed: ") + QString(canIfConfig.readAllStandardError()).trimmed());
+        return;
+    }
+
+}
+
+#if (LIBNL_CURRENT<=216)
+#warning we need at least libnl3 version 3.2.22 to be able to get link status via netlink
+int rtnl_link_can_state(struct rtnl_link *link, uint32_t *state) {
+    (void) link;
+    (void) state;
+    return -1;
+}
+#endif
+
+bool SocketCanInterface::updateStatus()
+{
+    bool retval = false;
+
+    struct nl_sock *sock = nl_socket_alloc();
+    struct nl_cache *cache;
+    struct rtnl_link *link;
+    uint32_t state;
+
+    _status.can_state = state_unknown;
+
+    nl_connect(sock, NETLINK_ROUTE);
+    if (rtnl_link_alloc_cache(sock, AF_UNSPEC, &cache) >= 0) {
+        if (rtnl_link_get_kernel(sock, _idx, 0, &link) == 0) {
+
+            _status.rx_count = rtnl_link_get_stat(link, RTNL_LINK_RX_PACKETS);
+            _status.rx_overruns = rtnl_link_get_stat(link, RTNL_LINK_RX_OVER_ERR);
+            _status.tx_count = rtnl_link_get_stat(link, RTNL_LINK_TX_PACKETS);
+            _status.tx_dropped = rtnl_link_get_stat(link, RTNL_LINK_TX_DROPPED);
+
+            if (rtnl_link_is_can(link)) {
+                if (rtnl_link_can_state(link, &state)==0) {
+                    _status.can_state = state;
+                }
+                _status.rx_errors = rtnl_link_can_berr_rx(link);
+                _status.tx_errors = rtnl_link_can_berr_tx(link);
+            } else {
+                _status.rx_errors = 0;
+                _status.tx_errors = 0;
+            }
+            retval = true;
+        }
+    }
+
+    nl_cache_free(cache);
+    nl_close(sock);
+    nl_socket_free(sock);
+
+    return retval;
+}
+
+bool SocketCanInterface::readConfig()
+{
+    bool retval = false;
+
+    struct nl_sock *sock = nl_socket_alloc();
+    struct nl_cache *cache;
+    struct rtnl_link *link;
+
+    nl_connect(sock, NETLINK_ROUTE);
+    int result = rtnl_link_alloc_cache(sock, AF_UNSPEC, &cache);
+
+    if (result>=0) {
+        if (rtnl_link_get_kernel(sock, _idx, 0, &link) == 0) {
+            retval = readConfigFromLink(link);
+        }
+    }
+
+    nl_cache_free(cache);
+    nl_close(sock);
+    nl_socket_free(sock);
+
+    return retval;
+}
+
+bool SocketCanInterface::readConfigFromLink(rtnl_link *link)
+{
+    _config.state = state_unknown;
+    _config.supports_canfd = (rtnl_link_get_mtu(link)==72);
+    _config.supports_timing = rtnl_link_is_can(link);
+    if (_config.supports_timing) {
+        rtnl_link_can_freq(link, &_config.base_freq);
+        rtnl_link_can_get_ctrlmode(link, &_config.ctrl_mode);
+        rtnl_link_can_get_bittiming(link, &_config.bit_timing);
+        rtnl_link_can_get_sample_point(link, &_config.sample_point);
+        rtnl_link_can_get_restart_ms(link, &_config.restart_ms);
+    } else {
+        // maybe a vcan interface?
+    }
+    return true;
+}
+
+bool SocketCanInterface::supportsTimingConfiguration()
+{
+    return _config.supports_timing;
+}
+
+bool SocketCanInterface::supportsCanFD()
+{
+    return _config.supports_canfd;
+}
+
+bool SocketCanInterface::supportsTripleSampling()
+{
+    return false;
+}
+
+unsigned SocketCanInterface::getBitrate() {
+    if (readConfig()) {
+        return _config.bit_timing.bitrate;
+    } else {
+        return 0;
+    }
+}
+
+uint32_t SocketCanInterface::getCapabilities()
+{
+    uint32_t retval =
+        CanInterface::capability_config_os |
+        CanInterface::capability_listen_only |
+        CanInterface::capability_auto_restart;
+
+    if (supportsCanFD()) {
+        retval |= CanInterface::capability_canfd;
+    }
+
+    if (supportsTripleSampling()) {
+        retval |= CanInterface::capability_triple_sampling;
+    }
+
+    return retval;
+}
+
+bool SocketCanInterface::updateStatistics()
+{
+    return updateStatus();
+}
+
+uint32_t SocketCanInterface::getState()
+{
+    switch (_status.can_state) {
+        case CAN_STATE_ERROR_ACTIVE: return state_ok;
+        case CAN_STATE_ERROR_WARNING: return state_warning;
+        case CAN_STATE_ERROR_PASSIVE: return state_passive;
+        case CAN_STATE_BUS_OFF: return state_bus_off;
+        case CAN_STATE_STOPPED: return state_stopped;
+        default: return state_unknown;
+    }
+}
+
+int SocketCanInterface::getNumRxFrames()
+{
+    return _status.rx_count;
+}
+
+int SocketCanInterface::getNumRxErrors()
+{
+    return _status.rx_errors;
+}
+
+int SocketCanInterface::getNumTxFrames()
+{
+    return _status.tx_count;
+}
+
+int SocketCanInterface::getNumTxErrors()
+{
+    return _status.tx_errors;
+}
+
+int SocketCanInterface::getNumRxOverruns()
+{
+    return _status.rx_overruns;
+}
+
+int SocketCanInterface::getNumTxDropped()
+{
+    return _status.tx_dropped;
+}
+
+int SocketCanInterface::getIfIndex() {
+    return _idx;
+}
+
+const char *SocketCanInterface::cname()
+{
+    return _name.toStdString().c_str();
+}
+
+void SocketCanInterface::open() {
+	if((_fd = socket(PF_CAN, SOCK_RAW, CAN_RAW)) < 0) {
+		perror("Error while opening socket");
+        _isOpen = false;
+	}
+
+	struct ifreq ifr;
+    struct sockaddr_can addr;
+    strcpy(ifr.ifr_name, _name.toStdString().c_str());
+	ioctl(_fd, SIOCGIFINDEX, &ifr);
+
+	addr.can_family  = AF_CAN;
+	addr.can_ifindex = ifr.ifr_ifindex;
+
+	if(bind(_fd, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
+		perror("Error in socket bind");
+        _isOpen = false;
+	}
+
+    _isOpen = true;
+}
+
+bool SocketCanInterface::isOpen()
+{
+    return _isOpen;
+}
+
+void SocketCanInterface::close() {
+	::close(_fd);
+    _isOpen = false;
+}
+
+void SocketCanInterface::sendMessage(const CanMessage &msg) {
+	struct can_frame frame;
+
+	frame.can_id = msg.getId();
+
+	if (msg.isExtended()) {
+		frame.can_id |= CAN_EFF_FLAG;
+	}
+
+	if (msg.isRTR()) {
+		frame.can_id |= CAN_RTR_FLAG;
+	}
+
+	if (msg.isErrorFrame()) {
+		frame.can_id |= CAN_ERR_FLAG;
+	}
+
+	uint8_t len = msg.getLength();
+	if (len>8) { len = 8; }
+
+	frame.can_dlc = len;
+	for (int i=0; i<len; i++) {
+		frame.data[i] = msg.getByte(i);
+	}
+
+	::write(_fd, &frame, sizeof(struct can_frame));
+}
+
+bool SocketCanInterface::readMessage(CanMessage &msg, unsigned int timeout_ms) {
+
+    struct can_frame frame;
+    struct timespec ts_rcv;
+    struct timeval tv_rcv;
+    struct timeval timeout;
+    //struct ifreq hwtstamp;
+    fd_set fdset;
+
+    timeout.tv_sec = timeout_ms / 1000;
+    timeout.tv_usec = 1000 * (timeout_ms % 1000);
+
+    FD_ZERO(&fdset);
+    FD_SET(_fd, &fdset);
+
+
+    int rv = select(_fd+1, &fdset, NULL, NULL, &timeout);
+    if (rv>0) {
+
+        if (read(_fd, &frame, sizeof(struct can_frame)) < 0) {
+            return false;
+        }
+
+        if (_ts_mode == ts_mode_SIOCSHWTSTAMP) {
+            // TODO implement me
+            _ts_mode = ts_mode_SIOCGSTAMPNS;
+        }
+
+        if (_ts_mode==ts_mode_SIOCGSTAMPNS) {
+            if (ioctl(_fd, SIOCGSTAMPNS, &ts_rcv) == 0) {
+                msg.setTimestamp(ts_rcv.tv_sec, ts_rcv.tv_nsec/1000);
+            } else {
+                _ts_mode = ts_mode_SIOCGSTAMP;
+            }
+        }
+
+        if (_ts_mode==ts_mode_SIOCGSTAMP) {
+            ioctl(_fd, SIOCGSTAMP, &tv_rcv);
+            msg.setTimestamp(tv_rcv.tv_sec, tv_rcv.tv_usec);
+        }
+
+        msg.setId(frame.can_id);
+        msg.setExtended((frame.can_id & CAN_EFF_FLAG)!=0);
+        msg.setRTR((frame.can_id & CAN_RTR_FLAG)!=0);
+        msg.setErrorFrame((frame.can_id & CAN_ERR_FLAG)!=0);
+        msg.setInterfaceId(getId());
+
+        uint8_t len = frame.can_dlc;
+        if (len>8) { len = 8; }
+
+        msg.setLength(len);
+        for (int i=0; i<len; i++) {
+            msg.setByte(i, frame.data[i]);
+        }
+
+        return true;
+    } else {
+        return false;
+    }
+}

+ 115 - 0
src/tool/adc_cantool/driver/SocketCanDriver/SocketCanInterface.h

@@ -0,0 +1,115 @@
+/*
+
+  Copyright (c) 2015, 2016 Hubert Denkmair
+
+  This file is part of cangaroo.
+
+  cangaroo is free software: you can redistribute it and/or modify
+  it under the terms of the GNU General Public License as published by
+  the Free Software Foundation, either version 2 of the License, or
+  (at your option) any later version.
+
+  cangaroo 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 General Public License for more details.
+
+  You should have received a copy of the GNU General Public License
+  along with cangaroo.  If not, see <http://www.gnu.org/licenses/>.
+
+*/
+
+#pragma once
+
+#include "../CanInterface.h"
+#include <linux/can/netlink.h>
+
+class SocketCanDriver;
+
+typedef struct {
+    bool supports_canfd;
+    bool supports_timing;
+    uint32_t state;
+    uint32_t base_freq;
+    uint32_t sample_point;
+    uint32_t ctrl_mode;
+    uint32_t restart_ms;
+    struct can_bittiming bit_timing;
+} can_config_t;
+
+typedef struct {
+    uint32_t can_state;
+
+    uint64_t rx_count;
+    int rx_errors;
+    uint64_t rx_overruns;
+
+    uint64_t tx_count;
+    int tx_errors;
+    uint64_t tx_dropped;
+} can_status_t;
+
+class SocketCanInterface: public CanInterface {
+public:
+    SocketCanInterface(SocketCanDriver *driver, int index, QString name);
+	virtual ~SocketCanInterface();
+
+    virtual QString getName() const;
+    void setName(QString name);
+
+    virtual QList<CanTiming> getAvailableBitrates();
+
+    virtual void applyConfig(const MeasurementInterface &mi);
+    virtual bool readConfig();
+    virtual bool readConfigFromLink(struct rtnl_link *link);
+
+    bool supportsTimingConfiguration();
+    bool supportsCanFD();
+    bool supportsTripleSampling();
+
+    virtual unsigned getBitrate();
+    virtual uint32_t getCapabilities();
+
+
+	virtual void open();
+    virtual bool isOpen();
+	virtual void close();
+
+    virtual void sendMessage(const CanMessage &msg);
+    virtual bool readMessage(CanMessage &msg, unsigned int timeout_ms);
+
+    virtual bool updateStatistics();
+    virtual uint32_t getState();
+    virtual int getNumRxFrames();
+    virtual int getNumRxErrors();
+    virtual int getNumRxOverruns();
+
+    virtual int getNumTxFrames();
+    virtual int getNumTxErrors();
+    virtual int getNumTxDropped();
+
+
+    int getIfIndex();
+
+private:
+    typedef enum {
+        ts_mode_SIOCSHWTSTAMP,
+        ts_mode_SIOCGSTAMPNS,
+        ts_mode_SIOCGSTAMP
+    } ts_mode_t;
+
+    int _idx;
+    bool _isOpen;
+	int _fd;
+    QString _name;
+
+    can_config_t _config;
+    can_status_t _status;
+    ts_mode_t _ts_mode;
+
+    const char *cname();
+    bool updateStatus();
+
+    QString buildIpRouteCmd(const MeasurementInterface &mi);
+    QStringList buildCanIfConfigArgs(const MeasurementInterface &mi);
+};

+ 16 - 0
src/tool/adc_cantool/driver/driver.pri

@@ -0,0 +1,16 @@
+SOURCES += \
+    $$PWD/CanInterface.cpp \
+    $$PWD/CanListener.cpp \
+    $$PWD/CanDriver.cpp \
+    $$PWD/CanTiming.cpp \
+    $$PWD/GenericCanSetupPage.cpp
+
+HEADERS  += \
+    $$PWD/CanInterface.h \
+    $$PWD/CanListener.h \
+    $$PWD/CanDriver.h \
+    $$PWD/CanTiming.h \
+    $$PWD/GenericCanSetupPage.h
+
+FORMS += \
+    $$PWD/GenericCanSetupPage.ui

+ 21 - 0
src/tool/adc_cantool/gpl-header.txt

@@ -0,0 +1,21 @@
+/*
+
+  Copyright (c) 2016 Hubert Denkmair <hubert@denkmair.de>
+
+  This file is part of cangaroo.
+
+  cangaroo is free software: you can redistribute it and/or modify
+  it under the terms of the GNU General Public License as published by
+  the Free Software Foundation, either version 2 of the License, or
+  (at your option) any later version.
+
+  cangaroo 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 General Public License for more details.
+
+  You should have received a copy of the GNU General Public License
+  along with cangaroo.  If not, see <http://www.gnu.org/licenses/>.
+
+*/
+

+ 11 - 0
src/tool/adc_cantool/main.cpp

@@ -0,0 +1,11 @@
+#include "mainwindow.h"
+#include <QApplication>
+
+int main(int argc, char *argv[])
+{
+    QApplication a(argc, argv);
+    MainWindow w;
+    w.show();
+
+    return a.exec();
+}

+ 370 - 0
src/tool/adc_cantool/mainwindow.cpp

@@ -0,0 +1,370 @@
+#include "mainwindow.h"
+#include "ui_mainwindow.h"
+
+#include <QtWidgets>
+#include <QMdiArea>
+#include <QSignalMapper>
+#include <QCloseEvent>
+#include <QDomDocument>
+
+#include <core/MeasurementSetup.h>
+#include <core/CanTrace.h>
+#include <window/TraceWindow/TraceWindow.h>
+#include <window/SetupDialog/SetupDialog.h>
+#include <window/LogWindow/LogWindow.h>
+#include <window/GraphWindow/GraphWindow.h>
+#include <window/CanStatusWindow/CanStatusWindow.h>
+#include <window/RawTxWindow/RawTxWindow.h>
+#include <window/ChassisAnalysisWindow/ChassisAnalysisWindow.h>
+
+#include <driver/SLCANDriver/SLCANDriver.h>
+#include <driver/CANBlastDriver/CANBlasterDriver.h>
+
+#if defined(__linux__)
+#include <driver/SocketCanDriver/SocketCanDriver.h>
+#else
+#include <driver/CandleApiDriver/CandleApiDriver.h>
+#endif
+
+MainWindow::MainWindow(QWidget *parent) :
+    QMainWindow(parent),
+    ui(new Ui::MainWindow)
+{
+
+
+
+    ui->setupUi(this);
+    _baseWindowTitle = windowTitle();
+
+    QIcon icon(":/assets/adc.png");
+    setWindowIcon(icon);
+    setWindowState(Qt::WindowMaximized);
+//    setWindowState(Qt::WindowFullScreen);
+//    showFullScreen();
+
+//       QAction *act=new QAction(QStringLiteral("新菜单"),this);
+//    ui->menuBar->addAction(ui->menuDevice->menuAction(),act);
+//    connect(act, SIGNAL(triggered()), this, SLOT(startMeasurement()));
+
+      actionStart_Measurement=new QAction("&开始采集",this);
+      ui->mainToolBar->addAction(actionStart_Measurement);
+      ui->mainToolBar->widgetForAction(actionStart_Measurement)->setObjectName(actionStart_Measurement->objectName());
+
+      actionStop_Measurement=new QAction("&停止采集",this);
+      ui->mainToolBar->addAction(actionStop_Measurement);
+      ui->mainToolBar->widgetForAction(actionStop_Measurement)->setObjectName(actionStop_Measurement->objectName());
+
+      actionTrace_clear=new QAction("&清除采集数据",this);
+      ui->mainToolBar->addAction(actionTrace_clear);
+      ui->mainToolBar->widgetForAction(actionTrace_clear)->setObjectName(actionTrace_clear->objectName());
+
+
+
+//    connect(ui->action_Trace_View, SIGNAL(triggered()), this, SLOT(createTraceWindow()));
+//    connect(ui->actionLog_View, SIGNAL(triggered()), this, SLOT(addLogWidget()));
+//    connect(ui->actionGraph_View, SIGNAL(triggered()), this, SLOT(createGraphWindow()));
+//    connect(ui->actionGraph_View_2, SIGNAL(triggered()), this, SLOT(addGraphWidget()));
+    connect(ui->actionSetup, SIGNAL(triggered()), this, SLOT(showSetupDialog()));
+//    connect(ui->actionTransmit_View, SIGNAL(triggered()), this, SLOT(addRawTxWidget()));
+
+    connect(actionStart_Measurement, SIGNAL(triggered()), this, SLOT(startMeasurement()));
+    connect(actionStop_Measurement, SIGNAL(triggered()), this, SLOT(stopMeasurement()));
+    connect(actionTrace_clear, SIGNAL(triggered()), this, SLOT(on_action_TraceClear_triggered()));
+
+    connect(&backend(), SIGNAL(beginMeasurement()), this, SLOT(updateMeasurementActions()));
+    connect(&backend(), SIGNAL(endMeasurement()), this, SLOT(updateMeasurementActions()));
+    updateMeasurementActions();
+
+//    connect(ui->actionSave_Trace_to_file, SIGNAL(triggered(bool)), this, SLOT(saveTraceToFile()));
+//    connect(ui->actionAbout, SIGNAL(triggered()), this, SLOT(showAboutDialog()));
+
+
+#if defined(__linux__)
+    Backend::instance().addCanDriver(*(new SocketCanDriver(Backend::instance())));
+#else
+    Backend::instance().addCanDriver(*(new CandleApiDriver(Backend::instance())));
+#endif
+    Backend::instance().addCanDriver(*(new SLCANDriver(Backend::instance())));
+    Backend::instance().addCanDriver(*(new CANBlasterDriver(Backend::instance())));
+
+    setWorkspaceModified(false);
+    newWorkspace();
+    backend().setDefaultSetup();//加载已经检测到的CAN接口
+
+    _setupDlg = new SetupDialog(Backend::instance(), 0);
+}
+
+MainWindow::~MainWindow()
+{
+    delete ui;
+}
+
+Backend &MainWindow::backend()
+{
+    return Backend::instance();
+}
+
+QMainWindow *MainWindow::createTab(QString title)
+{
+    QMainWindow *mm = new QMainWindow(this);
+    QPalette pal(palette());
+    pal.setColor(QPalette::Background, QColor(0xeb, 0xeb, 0xeb));
+    mm->setAutoFillBackground(true);
+    mm->setPalette(pal);
+    ui->mainTabs->addTab(mm, title);
+    return mm;
+}
+
+QMainWindow *MainWindow::currentTab()
+{
+    return (QMainWindow*)ui->mainTabs->currentWidget();
+}
+void MainWindow::stopAndClearMeasurement()
+{
+    backend().stopMeasurement();
+    QCoreApplication::processEvents();
+    backend().clearTrace();
+    backend().clearLog();
+}
+
+bool MainWindow::saveWorkspace()
+{
+    if (_workspaceFileName.isEmpty()) {
+        return saveWorkspaceAs();
+    } else {
+        return saveWorkspaceToFile(_workspaceFileName);
+    }
+}
+
+bool MainWindow::saveWorkspaceAs()
+{
+    QString filename = QFileDialog::getSaveFileName(this, "Save workspace configuration", "", "Workspace config files (*.cangaroo)");
+    if (!filename.isNull()) {
+        return saveWorkspaceToFile(filename);
+    } else {
+        return false;
+    }
+}
+
+
+
+void MainWindow::setWorkspaceModified(bool modified)
+{
+    _workspaceModified = modified;
+
+    QString title = _baseWindowTitle;
+    if (!_workspaceFileName.isEmpty()) {
+        QFileInfo fi(_workspaceFileName);
+        title += " - " + fi.fileName();
+    }
+    if (_workspaceModified) {
+        title += '*';
+    }
+    setWindowTitle(title);
+}
+int MainWindow::askSaveBecauseWorkspaceModified()
+{
+    if (_workspaceModified) {
+        QMessageBox msgBox;
+        msgBox.setText("The current workspace has been modified.");
+        msgBox.setInformativeText("Do you want to save your changes?");
+        msgBox.setStandardButtons(QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel);
+        msgBox.setDefaultButton(QMessageBox::Save);
+        int result = msgBox.exec();
+
+        if (result == QMessageBox::Save) {
+            if (saveWorkspace()) {
+                return QMessageBox::Save;
+            } else {
+                return QMessageBox::Cancel;
+            }
+        }
+
+        return result;
+    } else {
+        return QMessageBox::Discard;
+    }
+}
+
+void MainWindow::addLogWidget(QMainWindow *parent)
+{
+    if (!parent) {
+        parent = currentTab();
+    }
+    QDockWidget *dock = new QDockWidget("Log", parent);
+    dock->setWidget(new LogWindow(dock, backend()));
+    parent->addDockWidget(Qt::BottomDockWidgetArea, dock);
+}
+
+void MainWindow::addStatusWidget(QMainWindow *parent)
+{
+    if (!parent) {
+        parent = currentTab();
+    }
+
+    QDockWidget *dock = new QDockWidget("CAN Status", parent);
+    dock->setWidget(new CanStatusWindow(dock, backend()));
+    parent->addDockWidget(Qt::BottomDockWidgetArea, dock);
+}
+bool MainWindow::showSetupDialog()
+{
+    MeasurementSetup new_setup(&backend());
+    new_setup.cloneFrom(backend().getSetup());
+
+    if (_setupDlg->showSetupDialog(new_setup)) {
+        backend().setSetup(new_setup);
+        setWorkspaceModified(true);
+        return true;
+    } else {
+        return false;
+    }
+}
+
+
+void MainWindow::showAboutDialog()
+{
+    QMessageBox::about(this,
+       "About adc_cantool",
+       "adc_cantool\n"
+       "open source can bus analyzer\n"
+       "\n"
+       "version 0.0.0\n"
+       "\n"
+       "(c)2022\n"
+       "(c)2022"
+    );
+}
+
+bool MainWindow::saveWorkspaceToFile(QString filename)
+{
+    QDomDocument doc;
+    QDomElement root = doc.createElement("cangaroo-workspace");
+    doc.appendChild(root);
+
+    QDomElement tabsRoot = doc.createElement("tabs");
+    root.appendChild(tabsRoot);
+
+    for (int i=0; i < ui->mainTabs->count(); i++) {
+        QMainWindow *w = (QMainWindow*)ui->mainTabs->widget(i);
+
+        QDomElement tabEl = doc.createElement("tab");
+        tabEl.setAttribute("title", ui->mainTabs->tabText(i));
+
+        ConfigurableWidget *mdi = dynamic_cast<ConfigurableWidget*>(w->centralWidget());
+        if (!mdi->saveXML(backend(), doc, tabEl)) {
+            log_error(QString("Cannot save window settings to file: %1").arg(filename));
+            return false;
+        }
+
+        tabsRoot.appendChild(tabEl);
+    }
+
+    QDomElement setupRoot = doc.createElement("setup");
+    if (!backend().getSetup().saveXML(backend(), doc, setupRoot)) {
+        log_error(QString("Cannot save measurement setup to file: %1").arg(filename));
+        return false;
+    }
+    root.appendChild(setupRoot);
+
+    QFile outFile(filename);
+    if(outFile.open(QIODevice::WriteOnly|QIODevice::Text) ) {
+        QTextStream stream( &outFile );
+        stream << doc.toString();
+        outFile.close();
+        _workspaceFileName = filename;
+        setWorkspaceModified(false);
+        log_info(QString("Saved workspace settings to file: %1").arg(filename));
+        return true;
+    } else {
+        log_error(QString("Cannot open workspace file for writing: %1").arg(filename));
+        return false;
+    }
+
+}
+void MainWindow::addChassisAnalysisWindow(QMainWindow *parent)
+{
+    if (!parent) {
+        parent = currentTab();
+    }
+//    QDockWidget *dock = new QDockWidget("Chassis data Analysis", parent);
+     QDockWidget *dock = new QDockWidget("底盘数据解析", parent);
+    dock->setWidget(new ChassisAnalysisWindow(dock, backend()));
+    parent->addDockWidget(Qt::BottomDockWidgetArea, dock);
+
+}
+void MainWindow::newWorkspace()
+{
+    if (askSaveBecauseWorkspaceModified() != QMessageBox::Cancel) {
+        stopAndClearMeasurement();
+        clearWorkspace();
+        createTraceWindow();
+        addRawTxWidget();
+        addChassisAnalysisWindow();
+        backend().setDefaultSetup();
+    }
+}
+
+void MainWindow::startMeasurement()
+{
+//    perror("   button is push!");
+    if (showSetupDialog()) {
+        backend().clearTrace();
+        backend().startMeasurement();
+    }
+}
+
+void MainWindow::updateMeasurementActions()
+{
+    bool running = backend().isMeasurementRunning();
+    actionStart_Measurement->setEnabled(!running);
+    actionStop_Measurement->setEnabled(running);
+}
+
+
+void MainWindow::stopMeasurement()
+{
+    backend().stopMeasurement();
+}
+
+
+QMainWindow *MainWindow::createTraceWindow(QString title)
+{
+    if (title.isNull()) {
+//        title = "Trace";
+        title = "采集数据";
+    }
+    QMainWindow *mm = createTab(title);
+    mm->setCentralWidget(new TraceWindow(mm, backend()));
+
+    addLogWidget(mm);
+
+    ui->mainTabs->setCurrentWidget(mm);
+    return mm;
+}
+void MainWindow::clearWorkspace()
+{
+    ui->mainTabs->clear();
+    _workspaceFileName.clear();
+    setWorkspaceModified(false);
+}
+
+
+
+
+
+void MainWindow::addRawTxWidget(QMainWindow *parent)
+{
+    if (!parent) {
+        parent = currentTab();
+    }
+    QDockWidget *dock = new QDockWidget("数据发送窗口", parent);
+    dock->setWidget(new RawTxWindow(dock, backend()));
+    parent->addDockWidget(Qt::BottomDockWidgetArea, dock);
+
+}
+
+void MainWindow::on_action_TraceClear_triggered()
+{
+    backend().clearTrace();
+    backend().clearLog();
+}

+ 99 - 0
src/tool/adc_cantool/mainwindow.h

@@ -0,0 +1,99 @@
+#ifndef MAINWINDOW_H
+#define MAINWINDOW_H
+
+#pragma once
+
+#include <QMainWindow>
+#include <QList>
+#include <core/Backend.h>
+
+QT_BEGIN_NAMESPACE
+class QAction;
+class QMenu;
+class QMdiArea;
+class QMdiSubWindow;
+class QWidget;
+class QSignalMapper;
+class QDomElement;
+QT_END_NAMESPACE
+
+namespace Ui {
+class MainWindow;
+}
+
+class ConfigurableWidget;
+class SetupDialog;
+
+class MainWindow : public QMainWindow
+{
+    Q_OBJECT
+
+public:
+    explicit MainWindow(QWidget *parent = 0);
+    ~MainWindow();
+protected:
+//    void closeEvent(QCloseEvent *event) Q_DECL_OVERRIDE;
+
+public slots:
+    QMainWindow *createTraceWindow(QString title=QString());
+//    QMainWindow *createGraphWindow(QString title=QString());
+//    void addGraphWidget(QMainWindow *parent=0);
+    void addRawTxWidget(QMainWindow *parent=0);
+    void addLogWidget(QMainWindow *parent=0);
+    void addStatusWidget(QMainWindow *parent=0);
+
+    bool showSetupDialog();
+    void showAboutDialog();
+
+    void startMeasurement();
+    void stopMeasurement();
+//    void saveTraceToFile();
+
+    void updateMeasurementActions();
+
+private slots:
+//    void on_action_WorkspaceNew_triggered();
+//    void on_action_WorkspaceOpen_triggered();
+//    void on_action_WorkspaceSave_triggered();
+//    void on_action_WorkspaceSaveAs_triggered();
+    void on_action_TraceClear_triggered();
+
+//    void on_actionCan_Status_View_triggered();
+
+private:
+    Ui::MainWindow *ui;
+    SetupDialog *_setupDlg;
+
+    bool _workspaceModified;
+    QString _workspaceFileName;
+    QString _baseWindowTitle;
+
+    QAction *actionStart_Measurement;
+    QAction *actionStop_Measurement;
+    QAction *actionTrace_clear;
+
+    Backend &backend();
+
+    QMainWindow *createTab(QString title);
+    QMainWindow *currentTab();
+
+    void stopAndClearMeasurement();
+
+    void clearWorkspace();
+//    void traceClear_triggered();
+//    bool loadWorkspaceTab(QDomElement el);
+//    bool loadWorkspaceSetup(QDomElement el);
+//    void loadWorkspaceFromFile(QString filename);
+    bool saveWorkspaceToFile(QString filename);
+    void addChassisAnalysisWindow(QMainWindow *parent=0);
+
+    void newWorkspace();
+//    void loadWorkspace();
+    bool saveWorkspace();
+    bool saveWorkspaceAs();
+
+    void setWorkspaceModified(bool modified);
+    int askSaveBecauseWorkspaceModified();
+};
+
+#endif // MAINWINDOW_H

+ 77 - 0
src/tool/adc_cantool/mainwindow.ui

@@ -0,0 +1,77 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<ui version="4.0">
+ <class>MainWindow</class>
+ <widget class="QMainWindow" name="MainWindow">
+  <property name="geometry">
+   <rect>
+    <x>0</x>
+    <y>0</y>
+    <width>880</width>
+    <height>786</height>
+   </rect>
+  </property>
+  <property name="windowTitle">
+   <string>adcCanTool</string>
+  </property>
+  <widget class="QWidget" name="centralWidget">
+   <layout class="QVBoxLayout" name="verticalLayout">
+    <item>
+     <widget class="QTabWidget" name="mainTabs">
+      <property name="currentIndex">
+       <number>-1</number>
+      </property>
+      <property name="usesScrollButtons">
+       <bool>true</bool>
+      </property>
+      <property name="movable">
+       <bool>true</bool>
+      </property>
+     </widget>
+    </item>
+   </layout>
+  </widget>
+  <widget class="QMenuBar" name="menuBar">
+   <property name="geometry">
+    <rect>
+     <x>0</x>
+     <y>0</y>
+     <width>880</width>
+     <height>28</height>
+    </rect>
+   </property>
+   <widget class="QMenu" name="menuFile">
+    <property name="title">
+     <string>文件</string>
+    </property>
+   </widget>
+   <widget class="QMenu" name="menuDevice">
+    <property name="title">
+     <string>设备操作</string>
+    </property>
+    <addaction name="actionSetup"/>
+   </widget>
+   <addaction name="menuFile"/>
+   <addaction name="menuDevice"/>
+  </widget>
+  <widget class="QToolBar" name="mainToolBar">
+   <attribute name="toolBarArea">
+    <enum>TopToolBarArea</enum>
+   </attribute>
+   <attribute name="toolBarBreak">
+    <bool>false</bool>
+   </attribute>
+  </widget>
+  <widget class="QStatusBar" name="statusBar"/>
+  <action name="actionSetup">
+   <property name="text">
+    <string>&amp;设置</string>
+   </property>
+   <property name="iconText">
+    <string>设置</string>
+   </property>
+  </action>
+ </widget>
+ <layoutdefault spacing="6" margin="11"/>
+ <resources/>
+ <connections/>
+</ui>

+ 688 - 0
src/tool/adc_cantool/parser/dbc/DbcParser.cpp

@@ -0,0 +1,688 @@
+/*
+
+  Copyright (c) 2015, 2016 Hubert Denkmair <hubert@denkmair.de>
+
+  This file is part of cangaroo.
+
+  cangaroo is free software: you can redistribute it and/or modify
+  it under the terms of the GNU General Public License as published by
+  the Free Software Foundation, either version 2 of the License, or
+  (at your option) any later version.
+
+  cangaroo 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 General Public License for more details.
+
+  You should have received a copy of the GNU General Public License
+  along with cangaroo.  If not, see <http://www.gnu.org/licenses/>.
+
+*/
+
+#include "DbcParser.h"
+#include <QTextStream>
+#include <stdint.h>
+#include <iostream>
+#include <core/Backend.h>
+#include <core/CanDb.h>
+
+#include "DbcTokens.h"
+
+DbcParser::DbcParser()
+  : _errorLine(0), _errorColumn(0)
+{
+}
+
+bool DbcParser::parseFile(QFile *file, CanDb &candb)
+{
+    DbcTokenList tokens;
+    if (tokenize(file, tokens) != err_ok) {
+        QString msg = QString("error parsing dbc file %1").arg(file->fileName());
+        if (_errorLine) {
+            msg += QString(" at line %1, column %2").arg(_errorLine).arg(_errorColumn);
+        }
+        log_error(msg);
+        return false;
+    }
+
+    candb.setPath(file->fileName());
+    return parse(candb, tokens);
+}
+
+DbcToken *DbcParser::createNewToken(QChar ch, int line, int column)
+{
+    static const QString acceptableIdStartChars("ABCDEFGHIKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_");
+    static const QRegExp numberRegExp("^(\\d+(\\.\\d*)?(E[-+]?\\d*)?)$");
+
+    if (ch.isSpace()) {
+        return new DbcWhitespaceToken(line, column);
+    } else if (ch.isDigit()) {
+        return new DbcRegExpToken(line, column, dbc_tok_number, numberRegExp);
+    } else if (ch == '"') {
+        return new DbcStringToken(line, column);
+    } else if (ch == ':') {
+        return new DbcSingleCharToken(line, column, dbc_tok_colon, ':');
+    } else if (ch == '|') {
+        return new DbcSingleCharToken(line, column, dbc_tok_pipe, '|');
+    } else if (ch == '@') {
+        return new DbcSingleCharToken(line, column, dbc_tok_at, '@');
+    } else if (ch == '+') {
+        return new DbcSingleCharToken(line, column, dbc_tok_plus, '+');
+    } else if (ch == '-') {
+        return new DbcSingleCharToken(line, column, dbc_tok_minus, '-');
+    } else if (ch == '(') {
+        return new DbcSingleCharToken(line, column, dbc_tok_parenth_open, '(');
+    } else if (ch == ')') {
+        return new DbcSingleCharToken(line, column, dbc_tok_parenth_close, ')');
+    } else if (ch == '[') {
+        return new DbcSingleCharToken(line, column, dbc_tok_bracket_open, '[');
+    } else if (ch == ']') {
+        return new DbcSingleCharToken(line, column, dbc_tok_bracket_close, ']');
+    } else if (ch == ',') {
+        return new DbcSingleCharToken(line, column, dbc_tok_comma, ',');
+    } else if (ch == ';') {
+        return new DbcSingleCharToken(line, column, dbc_tok_semicolon, ';');
+    } else if (acceptableIdStartChars.contains(ch)) {
+        return new DbcIdentifierToken(line, column);
+    } else {
+        return 0;
+    }
+}
+
+DbcParser::error_t DbcParser::tokenize(QFile *file, DbcParser::DbcTokenList &tokens)
+{
+
+    if (!file->open(QIODevice::ReadOnly)) {
+        // TODO raise cannot open file exception
+        return err_cannot_open_file;
+    }
+
+    DbcToken *currentToken = 0;
+    int line = 1;
+    int column = 0;
+
+    error_t retval = err_ok;
+
+    QTextStream in(file);
+    in.setCodec("ISO 8859-1");
+
+    while (true) {
+        QString s = in.read(1);
+        if (s.isEmpty()) { break; }
+
+        QChar ch = s[0];
+
+        if (ch=='\n') {
+            line++;
+            column=1;
+        } else {
+            column++;
+        }
+
+        if (currentToken) {
+            if (!currentToken->appendChar(ch)) {
+                tokens.append(currentToken);
+                currentToken = 0;
+            }
+        }
+
+        if (!currentToken) {
+            currentToken = createNewToken(ch, line, column);
+            if (currentToken) {
+                currentToken->appendChar(ch);
+            } else {
+                retval = err_tokenize_error;
+                _errorColumn = column;
+                _errorLine = line;
+                break;
+            }
+        }
+
+    }
+
+    file->close();
+    return retval;
+}
+
+bool DbcParser::isSectionEnding(DbcToken *token, bool newLineIsSectionEnding)
+{
+    if (!token) {
+        return true;
+    } else {
+        int numNewLinesForEnding = newLineIsSectionEnding ? 1 : 2;
+        dbc_token_type_t type = token->getType();
+        return ( (type==dbc_tok_semicolon) || ( (type==dbc_tok_whitespace) && (token->countLineBreaks()>=numNewLinesForEnding)));
+    }
+}
+
+DbcToken *DbcParser::readToken(DbcParser::DbcTokenList &tokens, int typeMask, bool skipWhitespace, bool skipSectionEnding, bool newLineIsSectionEnding)
+{
+    while (true) {
+        if (tokens.isEmpty()) { return 0; }
+
+        DbcToken *token = tokens.first();
+        dbc_token_type_t type = token->getType();
+
+        if (type & typeMask) {
+
+            tokens.pop_front();
+            return token;
+
+        } else if (isSectionEnding(token, newLineIsSectionEnding)) {
+
+            if (skipSectionEnding) {
+                tokens.pop_front();
+                free(token);
+                continue;
+            } else {
+                return 0;
+            }
+
+        } else if (skipWhitespace && (type==dbc_tok_whitespace)) {
+
+            tokens.pop_front();
+            free(token);
+            continue;
+
+        } else {
+
+            return 0;
+
+        }
+    }
+
+    return 0;
+}
+
+bool DbcParser::expectSectionEnding(DbcTokenList &tokens, bool newLineIsSectionEnding)
+{
+    if (tokens.isEmpty()) {
+        return true;
+    }
+
+    DbcToken *token = readToken(tokens, dbc_tok_whitespace|dbc_tok_semicolon);
+    if (!token) {
+        return false;
+    }
+
+    if (!isSectionEnding(token, newLineIsSectionEnding)) {
+        free(token);
+        return false;
+    } else {
+        free(token);
+        return true;
+    }
+
+}
+
+bool DbcParser::expectLineBreak(DbcParser::DbcTokenList &tokens)
+{
+    bool found_line_break;
+
+    DbcToken *token = readToken(tokens, dbc_tok_whitespace);
+    if (token) {
+        found_line_break = token->countLineBreaks()>0;
+        free(token);
+    } else {
+        found_line_break = false;
+    }
+
+    return found_line_break;
+}
+
+bool DbcParser::expectAndSkipToken(DbcTokenList &tokens, dbc_token_type_t type, bool skipWhitespace, bool skipSectionEnding)
+{
+    DbcToken *token = readToken(tokens, type, skipWhitespace, skipSectionEnding);
+    if (!token) {
+        return false;
+    } else {
+        free(token);
+        return true;
+    }
+}
+
+bool DbcParser::expectData(DbcParser::DbcTokenList &tokens, dbc_token_type_t type, QString *data, bool skipWhitespace, bool skipSectionEnding, bool newLineIsSectionEnding)
+{
+    DbcToken *token;
+    if (!(token = readToken(tokens, type, skipWhitespace, skipSectionEnding, newLineIsSectionEnding))) {
+        return false;
+    }
+
+    if (data) {
+        data->clear();
+        data->append(token->getData());
+    }
+
+    free(token);
+    return true;
+}
+
+bool DbcParser::expectIdentifier(DbcParser::DbcTokenList &tokens, QString *id, bool skipWhitespace, bool skipSectionEnding, bool newLineIsSectionEnding)
+{
+    return expectData(tokens, dbc_tok_identifier, id, skipWhitespace, skipSectionEnding, newLineIsSectionEnding);
+}
+
+bool DbcParser::expectString(DbcParser::DbcTokenList &tokens, QString *str, bool skipWhitespace)
+{
+    QString quotedStr;
+    bool ok = expectData(tokens, dbc_tok_string, &quotedStr, skipWhitespace);
+    if (ok && quotedStr.length()>=2) {
+        *str = quotedStr.mid(1, quotedStr.length()-2);
+        return true;
+    } else {
+        return false;
+    }
+}
+
+bool DbcParser::expectNumber(DbcParser::DbcTokenList &tokens, QString *str, bool skipWhitespace)
+{
+    QString data;
+    if  (expectAndSkipToken(tokens, dbc_tok_minus, skipWhitespace)) {
+        *str = "-";
+    } else if (expectAndSkipToken(tokens, dbc_tok_plus, skipWhitespace)) {
+        *str = "+";
+    }
+
+    if (expectData(tokens, dbc_tok_number, &data, skipWhitespace)) {
+        *str += data;
+        return true;
+    } else {
+        return false;
+    }
+
+}
+
+bool DbcParser::expectInt(DbcParser::DbcTokenList &tokens, int *i, int base, bool skipWhitespace)
+{
+    QString data;
+    if (!expectNumber(tokens, &data, skipWhitespace)) {
+        return false;
+    }
+
+    bool convert_ok;
+    *i = data.toInt(&convert_ok, base);
+    return convert_ok;
+}
+
+bool DbcParser::expectLongLong(DbcTokenList &tokens, long long *i, int base, bool skipWhitespace)
+{
+    QString data;
+    if (!expectNumber(tokens, &data, skipWhitespace)) {
+        return false;
+    }
+
+    bool convert_ok;
+    *i = data.toLongLong(&convert_ok, base);
+    return convert_ok;
+}
+
+bool DbcParser::expectDouble(DbcTokenList &tokens, double *df, bool skipWhitespace)
+{
+    QString data;
+    if (!expectNumber(tokens, &data, skipWhitespace)) {
+        return false;
+    }
+
+    bool convert_ok;
+    *df = data.toDouble(&convert_ok);
+    return convert_ok;
+}
+
+void DbcParser::skipUntilSectionEnding(DbcTokenList &tokens)
+{
+    while (!tokens.isEmpty()) {
+        DbcToken *token = readToken(tokens, dbc_tok_ALL, false, false);
+        if (!token) { return; }
+        if (isSectionEnding(token)) {
+            free(token);
+            return;
+        } else {
+            free(token);
+        }
+    }
+}
+
+bool DbcParser::parseIdentifierList(DbcTokenList &tokens, QStringList *list, bool newLineIsSectionEnding)
+{
+    if (!expectAndSkipToken(tokens, dbc_tok_colon)) {
+        return false;
+    }
+
+    QString id;
+    while (expectIdentifier(tokens, &id, true, false, newLineIsSectionEnding)) {
+        if (list) {
+            list->append(id);
+        }
+    }
+
+    return expectSectionEnding(tokens, newLineIsSectionEnding);
+}
+
+bool DbcParser::parse(CanDb &candb, DbcTokenList &tokens)
+{
+    _dbcVersion.clear();
+    _nsEntries.clear();
+    _buEntries.clear();
+
+    while (!tokens.isEmpty()) {
+        if (!parseSection(candb, tokens)) {
+            return false;
+        }
+    }
+
+    return true;
+}
+
+bool DbcParser::parseSection(CanDb &candb, DbcTokenList &tokens) {
+    bool retval = true;
+
+    QString sectionName;
+    QStringList strings;
+
+    while (retval) {
+
+        if (tokens.isEmpty()) {
+            break;
+        }
+
+        if (expectIdentifier(tokens, &sectionName, true, true)) {
+            if (sectionName == "VERSION") {
+                retval &= parseSectionVersion(candb, tokens);
+            } else if (sectionName == "NS_") {
+                strings.clear();
+                retval &= parseIdentifierList(tokens, &strings);
+            } else if (sectionName == "BS_") {
+                retval &= parseSectionBs(tokens);
+            } else if (sectionName == "BU_") {
+                retval &= parseSectionBu(candb, tokens);
+            } else if (sectionName == "BO_") {
+                retval &= parseSectionBo(candb, tokens);
+            } else if (sectionName == "CM_") {
+                retval &= parseSectionCm(candb, tokens);
+            } else if (sectionName == "VAL_") {
+                retval &= parseSectionVal(candb, tokens);
+            } else {
+                skipUntilSectionEnding(tokens);
+            }
+
+        } else {
+            retval = false;
+        }
+
+    }
+
+    if (!retval) {
+        log_error("dbc parse error");
+    }
+    return retval;
+
+    /*
+    if (sectionName == "BA_")             { return tokSectionBa; }
+    if (sectionName == "BA_REL_")         { return tokSectionBaRel; }
+    if (sectionName == "BA_DEF_")         { return tokSectionBaDef; }
+    if (sectionName == "BA_DEF_DEF_")     { return tokSectionBaDefDef; }
+    if (sectionName == "BA_DEF_DEF_REL_") { return tokSectionBaDefDefRel; }
+*/
+}
+
+bool DbcParser::parseSectionVersion(CanDb &candb, DbcTokenList &tokens)
+{
+    QString version;
+    if (!expectString(tokens, &version)) { return false; }
+    candb.setVersion(version);
+    return expectSectionEnding(tokens);
+}
+
+bool DbcParser::parseSectionBs(DbcParser::DbcTokenList &tokens)
+{
+    if (!expectAndSkipToken(tokens, dbc_tok_colon)) {
+        return false;
+    }
+
+    return expectSectionEnding(tokens);
+}
+
+bool DbcParser::parseSectionBu(CanDb &candb, DbcParser::DbcTokenList &tokens)
+{
+    QStringList strings;
+    QString s;
+
+    if (!parseIdentifierList(tokens, &strings, true)) {
+        return false;
+    }
+
+    foreach(s, strings) {
+        candb.getOrCreateNode(s);
+    }
+
+    return true;
+}
+
+
+bool DbcParser::parseSectionBo(CanDb &candb, DbcTokenList &tokens)
+{
+    long long can_id;
+    int dlc;
+    QString msg_name;
+    QString sender;
+
+    if (!expectLongLong(tokens, &can_id)) { return false; }
+    if (!expectIdentifier(tokens, &msg_name)) { return false; }
+    if (!expectAndSkipToken(tokens, dbc_tok_colon)) { return false; }
+    if (!expectInt(tokens, &dlc)) { return false; }
+    if (!expectIdentifier(tokens, &sender)) { return false; }
+
+    CanDbMessage *msg = new CanDbMessage(&candb);
+    msg->setRaw_id(can_id);
+    msg->setName(msg_name);
+    msg->setDlc(dlc);
+    msg->setSender(candb.getOrCreateNode(sender));
+    candb.addMessage(msg);
+
+    QString subsect;
+    while (true) {
+        if (expectSectionEnding(tokens)) {
+            return true;
+         } else {
+            if (!expectIdentifier(tokens, &subsect)) {
+                return false;
+            }
+
+            if (subsect!="SG_") {
+                return false;
+            }
+
+            if (!parseSectionBoSg(candb, msg, tokens)) {
+                return false;
+            }
+        }
+    }
+
+
+}
+
+bool DbcParser::parseSectionBoSg(CanDb &candb, CanDbMessage *msg, DbcTokenList &tokens)
+{
+    (void)candb;
+
+    QString signal_name;
+    QString mux_indicator;
+    int start_bit = 0;
+    int length = 0;
+    int byte_order = 0;
+    double factor = 1;
+    double offset = 0;
+    double minimum = 0;
+    double maximum = 0;
+    QString unit;
+    QString receiver;
+    QStringList receivers;
+
+    CanDbSignal *signal = new CanDbSignal(msg);
+    msg->addSignal(signal);
+
+    if (!expectIdentifier(tokens, &signal_name)) { return false; }
+    signal->setName(signal_name);
+
+
+    if (expectIdentifier(tokens, &mux_indicator)) {
+        if (mux_indicator=="M") {
+            signal->setIsMuxer(true);
+            msg->setMuxer(signal);
+        } else if (mux_indicator.startsWith('m')) {
+            signal->setIsMuxed(true);
+            bool ok;
+            signal->setMuxValue(mux_indicator.mid(1).toUInt(&ok));
+            if (!ok) { return false; }
+        } else {
+            return false;
+        }
+    }
+
+    if (!expectAndSkipToken(tokens, dbc_tok_colon)) { return false; }
+    if (!expectInt(tokens, &start_bit)) { return false; }
+
+    signal->setStartBit(start_bit);
+
+    if (!expectAndSkipToken(tokens, dbc_tok_pipe)) { return false; }
+    if (!expectInt(tokens, &length)) { return false; }
+    signal->setLength(length);
+
+    if (!expectAndSkipToken(tokens, dbc_tok_at)) { return false; }
+    if (!expectInt(tokens, &byte_order)) { return false; }
+    signal->setIsBigEndian(byte_order==0);
+
+    // If the signal is big endian, convert the start bit to the Intel-style start bit for further parsing
+    if(signal->isBigEndian())
+    {
+        // This will be the number of 8-bit rows above the message
+        uint8_t row_position = signal->startBit() >> 3;
+
+        // Bit position in current row (0-7)
+        uint8_t column_position = signal->startBit() & 0b111;
+
+        // Calcualte the normalized start bit position (bit index starting at 0)
+        uint8_t normalized_position = (row_position * 8) + (7 - column_position);
+
+        signal->setStartBit(normalized_position);
+    }
+
+    if (expectAndSkipToken(tokens, dbc_tok_plus)) {
+        signal->setUnsigned(true);
+    } else {
+        if (expectAndSkipToken(tokens, dbc_tok_minus)) {
+            signal->setUnsigned(false);
+        } else {
+            return false;
+        }
+    }
+
+    if (!expectAndSkipToken(tokens, dbc_tok_parenth_open)) { return false; }
+    if (!expectDouble(tokens, &factor)) { return false; }
+    signal->setFactor(factor);
+    if (!expectAndSkipToken(tokens, dbc_tok_comma)) { return false; }
+    if (!expectDouble(tokens, &offset)) { return false; }
+    signal->setOffset(offset);
+    if (!expectAndSkipToken(tokens, dbc_tok_parenth_close)) { return false; }
+
+    if (!expectAndSkipToken(tokens, dbc_tok_bracket_open)) { return false; }
+    if (!expectDouble(tokens, &minimum)) { return false; }
+    signal->setMinimumValue(minimum);
+    if (!expectAndSkipToken(tokens, dbc_tok_pipe)) { return false; }
+    if (!expectDouble(tokens, &maximum)) { return false; }
+    signal->setMaximumValue(maximum);
+    if (!expectAndSkipToken(tokens, dbc_tok_bracket_close)) { return false; }
+
+    if (!expectString(tokens, &unit)) { return false; }
+    signal->setUnit(unit);
+
+    if (!expectIdentifier(tokens, &receiver)) { return false; }
+    receivers.append(receiver);
+
+    while (expectAndSkipToken(tokens, dbc_tok_comma, false, false)) {
+        if (!expectIdentifier(tokens, &receiver)) { return false; }
+        receivers.append(receiver);
+    }
+
+
+    return true;
+}
+
+bool DbcParser::parseSectionCm(CanDb &candb, DbcParser::DbcTokenList &tokens)
+{
+    QString s;
+    QString idtype;
+    QString id;
+    long long ll;
+
+    if (expectString(tokens, &s)) { // DBC file comment
+        candb.setComment(s);
+        return true;
+    }
+
+    if (!expectIdentifier(tokens, &idtype)) { return false; }
+
+    if (idtype=="BU_") {
+
+        if (!expectIdentifier(tokens, &id)) { return false; }
+        if (!expectString(tokens, &s)) { return false; }
+        candb.getOrCreateNode(id)->setComment(s);
+        return expectSectionEnding(tokens);
+
+    } else if (idtype=="BO_") {
+
+        if (!expectLongLong(tokens, &ll)) { return false; }
+        if (!expectString(tokens, &s)) { return false; }
+        CanDbMessage *msg = candb.getMessageById(ll);
+        if (!msg) { return false; }
+        msg->setComment(s);
+        return expectSectionEnding(tokens);
+
+    } else if (idtype=="SG_") {
+
+        if (!expectLongLong(tokens, &ll)) { return false; }
+        CanDbMessage *msg = candb.getMessageById(ll);
+        if (!msg) { return false; }
+
+        if (!expectIdentifier(tokens, &id)) { return false; }
+        CanDbSignal *signal = msg->getSignalByName(id);
+        if (!signal) { return false; }
+
+        if (!expectString(tokens, &s)) { return false; }
+        signal->setComment(s);
+
+        return expectSectionEnding(tokens);
+
+    } else {
+
+        return false;
+
+    }
+
+
+}
+
+bool DbcParser::parseSectionVal(CanDb &candb, DbcParser::DbcTokenList &tokens)
+{
+    long long can_id;
+    QString signal_id;
+    long long value;
+    QString name;
+
+    if (!expectLongLong(tokens, &can_id)) { return false; }
+    CanDbMessage *msg = candb.getMessageById(can_id);
+    if (!msg) { return false; }
+
+    if (!expectIdentifier(tokens, &signal_id)) { return false; }
+    CanDbSignal *signal = msg->getSignalByName(signal_id);
+    if (!signal) { return false; }
+
+    while (!expectAndSkipToken(tokens, dbc_tok_semicolon)) {
+        if (!expectLongLong(tokens, &value)) { return false; }
+        if (!expectString(tokens, &name)) { return false; }
+        signal->setValueName(value, name);
+    }
+
+    return true;
+}
+

+ 92 - 0
src/tool/adc_cantool/parser/dbc/DbcParser.h

@@ -0,0 +1,92 @@
+/*
+
+  Copyright (c) 2015, 2016 Hubert Denkmair <hubert@denkmair.de>
+
+  This file is part of cangaroo.
+
+  cangaroo is free software: you can redistribute it and/or modify
+  it under the terms of the GNU General Public License as published by
+  the Free Software Foundation, either version 2 of the License, or
+  (at your option) any later version.
+
+  cangaroo 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 General Public License for more details.
+
+  You should have received a copy of the GNU General Public License
+  along with cangaroo.  If not, see <http://www.gnu.org/licenses/>.
+
+*/
+
+#pragma once
+
+#include <QFile>
+#include <QRegExp>
+#include <QList>
+#include <qstringlist.h>
+
+#include <core/CanDb.h>
+
+#include "DbcTokens.h"
+
+class CanDbMessage;
+
+class DbcParser
+{
+
+public:
+    typedef QList<DbcToken *> DbcTokenList;
+
+    typedef enum {
+        err_ok,
+        err_cannot_open_file,
+        err_tokenize_error,
+    } error_t;
+
+public:
+    DbcParser();
+    bool parseFile(QFile *file, CanDb &candb);
+
+private:
+    int _errorLine;
+    int _errorColumn;
+
+    QString _dbcVersion;
+    QStringList _nsEntries;
+    QStringList _buEntries;
+
+    DbcToken *createNewToken(QChar ch, int line, int column);
+    error_t tokenize(QFile *file, DbcTokenList &tokens);
+
+    bool isSectionEnding(DbcToken *token, bool newLineIsSectionEnding=false);
+    bool expectSectionEnding(DbcTokenList &tokens, bool newLineIsSectionEnding=false);
+    bool expectLineBreak(DbcTokenList &tokens);
+    bool expectAndSkipToken(DbcTokenList &tokens, dbc_token_type_t type, bool skipWhitespace=true, bool skipSectionEnding=false);
+
+    bool expectData(DbcTokenList &tokens, dbc_token_type_t type, QString *data, bool skipWhitespace=true, bool skipSectionEnding=false, bool newLineIsSectionEnding=false);
+    bool expectIdentifier(DbcTokenList &tokens, QString *id, bool skipWhitespace=true, bool skipSectionEnding=false, bool newLineIsSectionEnding=false);
+    bool expectString(DbcTokenList &tokens, QString *str, bool skipWhitespace=true);
+
+    bool expectNumber(DbcTokenList &tokens, QString *str, bool skipWhitespace=true);
+
+    bool expectInt(DbcTokenList &tokens, int *i, int base=10, bool skipWhitespace=true);
+    bool expectLongLong(DbcTokenList &tokens, long long *i, int base=10, bool skipWhitespace=true);
+    bool expectDouble(DbcTokenList &tokens, double *df, bool skipWhitespace=true);
+    void skipUntilSectionEnding(DbcTokenList &tokens);
+
+    DbcToken *readToken(DbcTokenList &tokens, int typeMask, bool skipWhitespace=true, bool skipSectionEnding=false, bool newLineIsSectionEnding=false);
+
+    bool parse(CanDb &candb, DbcTokenList &tokens);
+    bool parseIdentifierList(DbcTokenList &tokens, QStringList *list, bool newLineIsSectionEnding=false);
+
+    bool parseSection(CanDb &candb, DbcTokenList &tokens);
+    bool parseSectionVersion(CanDb &candb, DbcTokenList &tokens);
+    bool parseSectionBs(DbcTokenList &tokens);
+    bool parseSectionBu(CanDb &candb, DbcTokenList &tokens);
+    bool parseSectionBo(CanDb &candb, DbcTokenList &tokens);
+    bool parseSectionBoSg(CanDb &candb, CanDbMessage *msg, DbcTokenList &tokens);
+    bool parseSectionCm(CanDb &candb, DbcTokenList &tokens);
+    bool parseSectionVal(CanDb &candb, DbcTokenList &tokens);
+
+};

+ 137 - 0
src/tool/adc_cantool/parser/dbc/DbcTokens.cpp

@@ -0,0 +1,137 @@
+/*
+
+  Copyright (c) 2015, 2016 Hubert Denkmair <hubert@denkmair.de>
+
+  This file is part of cangaroo.
+
+  cangaroo is free software: you can redistribute it and/or modify
+  it under the terms of the GNU General Public License as published by
+  the Free Software Foundation, either version 2 of the License, or
+  (at your option) any later version.
+
+  cangaroo 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 General Public License for more details.
+
+  You should have received a copy of the GNU General Public License
+  along with cangaroo.  If not, see <http://www.gnu.org/licenses/>.
+
+*/
+
+#include "DbcTokens.h"
+
+DbcToken::DbcToken(int line, int column, dbc_token_type_t type)
+  : _line(line), _col(column), _type(type), _numLineBreaks(0)
+{
+
+}
+
+bool DbcToken::appendChar(QChar ch)
+{
+    if (acceptsChar(ch)) {
+        if (ch=='\n') {
+            _numLineBreaks++;
+        }
+        _data.append(ch);
+        return true;
+    } else {
+        return false;
+    }
+}
+
+dbc_token_type_t DbcToken::getType()
+{
+    return _type;
+}
+
+QString DbcToken::getData()
+{
+    return _data;
+}
+
+int DbcToken::countLineBreaks()
+{
+    return _numLineBreaks;
+}
+
+int DbcToken::getLine()
+{
+    return _line;
+}
+
+int DbcToken::getColumn()
+{
+    return _col;
+}
+
+
+DbcIdentifierToken::DbcIdentifierToken(int line, int column)
+  : DbcToken(line, column, dbc_tok_identifier)
+{
+}
+
+bool DbcIdentifierToken::acceptsChar(QChar ch)
+{
+    static const QString acceptableStartChars("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_");
+    static const QString acceptableChars("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_");
+    if (_data.isEmpty()) {
+        return acceptableStartChars.contains(ch);
+    } else {
+        return acceptableChars.contains(ch);
+    }
+}
+
+
+DbcStringToken::DbcStringToken(int line, int column)
+  : DbcToken(line, column, dbc_tok_string)
+{
+}
+
+bool DbcStringToken::acceptsChar(QChar ch)
+{
+   if (_data.isEmpty()) {
+       return (ch=='"');
+   } else if (_data.length()<2) {
+       return true;
+   } else {
+       return !_data.endsWith('"');
+   }
+}
+
+
+
+
+DbcWhitespaceToken::DbcWhitespaceToken(int line, int column)
+  : DbcToken(line, column, dbc_tok_whitespace)
+{
+}
+
+bool DbcWhitespaceToken::acceptsChar(QChar ch)
+{
+    return ch.isSpace();
+}
+
+DbcRegExpToken::DbcRegExpToken(int line, int column, dbc_token_type_t type, QRegExp re)
+  : DbcToken(line, column, type),
+    _re(re)
+{
+}
+
+bool DbcRegExpToken::acceptsChar(QChar ch)
+{
+    QString s = _data + ch;
+    return (_re.indexIn(s)==0);
+}
+
+DbcSingleCharToken::DbcSingleCharToken(int line, int column, dbc_token_type_t type, QChar ch)
+  : DbcToken(line, column, type),
+    _ch(ch)
+{
+}
+
+bool DbcSingleCharToken::acceptsChar(QChar ch)
+{
+    return (ch==_ch) && _data.isEmpty();
+}
+

+ 102 - 0
src/tool/adc_cantool/parser/dbc/DbcTokens.h

@@ -0,0 +1,102 @@
+/*
+
+  Copyright (c) 2015, 2016 Hubert Denkmair <hubert@denkmair.de>
+
+  This file is part of cangaroo.
+
+  cangaroo is free software: you can redistribute it and/or modify
+  it under the terms of the GNU General Public License as published by
+  the Free Software Foundation, either version 2 of the License, or
+  (at your option) any later version.
+
+  cangaroo 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 General Public License for more details.
+
+  You should have received a copy of the GNU General Public License
+  along with cangaroo.  If not, see <http://www.gnu.org/licenses/>.
+
+*/
+
+#pragma once
+
+#include <QFile>
+#include <QChar>
+#include <QRegExp>
+
+typedef enum {
+    dbc_tok_whitespace = 1,
+    dbc_tok_identifier = 2,
+    dbc_tok_string = 4,
+    dbc_tok_number = 8,
+    dbc_tok_colon = 16,
+    dbc_tok_pipe = 32,
+    dbc_tok_at = 64,
+    dbc_tok_plus = 128,
+    dbc_tok_parenth_open = 256,
+    dbc_tok_parenth_close = 512,
+    dbc_tok_bracket_open = 1024,
+    dbc_tok_bracket_close = 2048,
+    dbc_tok_comma = 4096,
+    dbc_tok_semicolon = 8192,
+    dbc_tok_minus = 16384,
+
+    dbc_tok_ALL = 0xFFFFFFFF
+} dbc_token_type_t;
+
+class DbcToken {
+public:
+    DbcToken(int line, int column, dbc_token_type_t type);
+    virtual bool acceptsChar(QChar ch) = 0;
+
+    bool appendChar(QChar ch);
+    dbc_token_type_t getType();
+    QString getData();
+    int countLineBreaks();
+    int getLine();
+    int getColumn();
+
+protected:
+    QString _data;
+private:
+    int _line;
+    int _col;
+    dbc_token_type_t _type;
+    int _numLineBreaks;
+};
+
+class DbcWhitespaceToken : public DbcToken {
+public:
+    DbcWhitespaceToken(int line, int column);
+    virtual bool acceptsChar(QChar ch);
+
+};
+
+class DbcIdentifierToken : public DbcToken {
+public:
+    DbcIdentifierToken(int line, int column);
+    virtual bool acceptsChar(QChar ch);
+};
+
+class DbcStringToken : public DbcToken {
+public:
+    DbcStringToken(int line, int column);
+    virtual bool acceptsChar(QChar ch);
+};
+
+class DbcRegExpToken : public DbcToken {
+public:
+    DbcRegExpToken(int line, int column, dbc_token_type_t type, QRegExp re);
+    virtual bool acceptsChar(QChar ch);
+private:
+    QRegExp _re;
+};
+
+class DbcSingleCharToken : public DbcToken {
+public:
+    DbcSingleCharToken(int line, int column, dbc_token_type_t type, QChar ch);
+    virtual bool acceptsChar(QChar ch);
+private:
+    QChar _ch;
+};

+ 7 - 0
src/tool/adc_cantool/parser/dbc/dbc.pri

@@ -0,0 +1,7 @@
+HEADERS += \
+    $$PWD/DbcParser.h \
+    $$PWD/DbcTokens.h
+
+SOURCES += \
+    $$PWD/DbcParser.cpp \
+    $$PWD/DbcTokens.cpp

+ 25 - 0
src/tool/adc_cantool/scripts/setup_vcan.sh

@@ -0,0 +1,25 @@
+#!/bin/bash
+sudo modprobe can
+sudo modprobe can_raw
+sudo modprobe vcan
+
+#sudo modprobe can-dev
+sudo modprobe mttcan
+
+
+#sudo ip link set can0 type can bitrate 500000 dbitrate 2000000 berr-reporting on fd on
+#sudo ip link set can0 type can bitrate 500000 berr-reporting on 
+#sudo ip link set can0 type can bitrate 500000 dbitrate 2000000 berr-reporting on fd on
+#sudo ip link set can1 type can bitrate 500000 dbitrate 2000000 berr-reporting on fd on
+#sudo ip link set can0 up
+#sudo ip link set can1 up
+
+#sudo ip link add dev vcan0 type vcan
+#sudo ip link set up vcan0
+
+#sudo ip link add dev vcan1 type vcan
+#sudo ip link set up vcan1
+
+#sudo ip link add dev vcan2 type vcan
+#sudo ip link set up vcan2
+

+ 109 - 0
src/tool/adc_cantool/window/CanStatusWindow/CanStatusWindow.cpp

@@ -0,0 +1,109 @@
+/*
+
+  Copyright (c) 2016 Hubert Denkmair <hubert@denkmair.de>
+
+  This file is part of cangaroo.
+
+  cangaroo is free software: you can redistribute it and/or modify
+  it under the terms of the GNU General Public License as published by
+  the Free Software Foundation, either version 2 of the License, or
+  (at your option) any later version.
+
+  cangaroo 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 General Public License for more details.
+
+  You should have received a copy of the GNU General Public License
+  along with cangaroo.  If not, see <http://www.gnu.org/licenses/>.
+
+*/
+
+
+#include "CanStatusWindow.h"
+#include "ui_CanStatusWindow.h"
+
+#include <QStringList>
+#include <QTimer>
+#include <core/Backend.h>
+#include <core/MeasurementSetup.h>
+#include <core/MeasurementNetwork.h>
+#include <core/MeasurementInterface.h>
+
+CanStatusWindow::CanStatusWindow(QWidget *parent, Backend &backend) :
+    ConfigurableWidget(parent),
+    ui(new Ui::CanStatusWindow),
+    _backend(backend),
+    _timer(new QTimer(this))
+{
+    ui->setupUi(this);
+    ui->treeWidget->setHeaderLabels(QStringList()
+        << "Driver" << "Interface" << "State"
+        << "Rx Frames" << "Rx Errors" << "Rx Overrun"
+        << "Tx Frames" << "Tx Errors" << "Tx Dropped"
+        << "# Warning" << "# Passive" << "# Bus Off" << " #Restarts"
+    );
+    ui->treeWidget->setColumnWidth(0, 80);
+    ui->treeWidget->setColumnWidth(1, 70);
+
+    connect(&backend, SIGNAL(beginMeasurement()), this, SLOT(beginMeasurement()));
+    connect(&backend, SIGNAL(endMeasurement()), this, SLOT(endMeasurement()));
+    connect(_timer, SIGNAL(timeout()), this, SLOT(update()));
+}
+
+CanStatusWindow::~CanStatusWindow()
+{
+    delete ui;
+}
+
+void CanStatusWindow::beginMeasurement()
+{
+    ui->treeWidget->clear();
+    foreach (CanInterfaceId ifid, backend().getInterfaceList()) {
+        CanInterface *intf = backend().getInterfaceById(ifid);
+        QTreeWidgetItem *item = new QTreeWidgetItem(ui->treeWidget);
+        item->setData(0, Qt::UserRole, QVariant::fromValue((void*)intf));
+        item->setText(column_driver, intf->getDriver()->getName());
+        item->setText(column_interface, intf->getName());
+
+        item->setTextAlignment(column_driver, Qt::AlignLeft);
+        item->setTextAlignment(column_interface, Qt::AlignLeft);
+        item->setTextAlignment(column_state, Qt::AlignCenter);
+        for (int i=column_rx_frames; i<column_count; i++) {
+            item->setTextAlignment(i, Qt::AlignRight);
+        }
+
+        ui->treeWidget->addTopLevelItem(item);
+    }
+    update();
+    _timer->start(100);
+}
+
+void CanStatusWindow::endMeasurement()
+{
+    _timer->stop();
+}
+
+void CanStatusWindow::update()
+{
+    for (QTreeWidgetItemIterator it(ui->treeWidget); *it; ++it) {
+        QTreeWidgetItem *item = *it;
+        CanInterface *intf = (CanInterface *)item->data(0, Qt::UserRole).value<void *>();
+        if (intf) {
+            intf->updateStatistics();
+            item->setText(column_state, intf->getStateText());
+            item->setText(column_rx_frames, QString().number(intf->getNumRxFrames()));
+            item->setText(column_rx_errors, QString().number(intf->getNumRxErrors()));
+            item->setText(column_rx_overrun, QString().number(intf->getNumRxOverruns()));
+            item->setText(column_tx_frames, QString().number(intf->getNumTxFrames()));
+            item->setText(column_tx_errors, QString().number(intf->getNumTxErrors()));
+            item->setText(column_tx_dropped, QString().number(intf->getNumTxDropped()));
+        }
+    }
+
+}
+
+Backend &CanStatusWindow::backend()
+{
+    return _backend;
+}

Some files were not shown because too many files changed in this diff