From: Bert Vermeulen Date: Tue, 10 Dec 2013 16:17:38 +0000 (+0100) Subject: Add protocol decoder testing framework. X-Git-Tag: libsigrokdecode-0.3.0~205 X-Git-Url: https://sigrok.org/gitweb/?p=libsigrokdecode.git;a=commitdiff_plain;h=fbd226c3f70f20fdf3cbfd43a671bf2bcc5d23e6 Add protocol decoder testing framework. This adds a tool in the tests directory, called pdtest. It uses the "test/" directory in every PD directory, if present, to run the PD against dumps found in the sigrok-dumps repository, and compares the output against ".output" files in the "test/" directory. The file "test/test.conf" is used to configure which tests to run. A separate tool (tests/runtc.c) is used to run the actual decoding and report output. To get an overview of the options, run tests/pdtest without any options. --- diff --git a/.gitignore b/.gitignore index 2016020..99fb012 100644 --- a/.gitignore +++ b/.gitignore @@ -22,6 +22,7 @@ INSTALL tests/check_main tests/check_main.* tests/test-suite.log +tests/runtc version.h # recursive autoconf leftovers diff --git a/Makefile.am b/Makefile.am index a9460e5..f9bc14d 100644 --- a/Makefile.am +++ b/Makefile.am @@ -39,7 +39,7 @@ libsigrokdecode_la_SOURCES = \ version.c libsigrokdecode_la_CPPFLAGS = $(CPPFLAGS_PYTHON) \ - -DDECODERS_DIR='"$(DECODERS_DIR)"' + -DDECODERS_DIR='"$(DECODERS_DIR)"' libsigrokdecode_la_LDFLAGS = $(SRD_LIB_LDFLAGS) $(LDFLAGS_PYTHON) library_includedir = $(includedir)/libsigrokdecode diff --git a/configure.ac b/configure.ac index ca38710..1314dea 100644 --- a/configure.ac +++ b/configure.ac @@ -123,6 +123,11 @@ PKG_CHECK_MODULES([check], [check >= 0.9.4], LIBS="$LIBS $check_LIBS"], [have_check="no"]) AM_CONDITIONAL(HAVE_CHECK, test x"$have_check" = "xyes") +PKG_CHECK_MODULES([libsigrok], [libsigrok >= 0.2.0], + [have_libsigrok="yes"; CFLAGS="$CFLAGS $libsigrok_CFLAGS"; + LIBS="$LIBS $libsigrok_LIBS"], [have_libsigrok="no"]) +AM_CONDITIONAL(HAVE_LIBSIGROK, test x"$have_libsigrok" = "xyes") + # Checks for header files. # These are already checked: inttypes.h stdint.h stdlib.h string.h unistd.h. # AC_CHECK_HEADERS([]) @@ -178,7 +183,7 @@ echo "Detected libraries:" echo # Note: This only works for libs with pkg-config integration. -for lib in "glib-2.0 >= 2.24.0" "check >= 0.9.4"; do +for lib in "glib-2.0 >= 2.24.0" "check >= 0.9.4" "libsigrok >= 0.2.0"; do if `$PKG_CONFIG --exists $lib`; then ver=`$PKG_CONFIG --modversion $lib` answer="yes ($ver)" diff --git a/decoders/edid/config b/decoders/edid/config index 3b17972..44e1f35 100644 --- a/decoders/edid/config +++ b/decoders/edid/config @@ -1,2 +1 @@ extra-install pnpids.txt - diff --git a/decoders/edid/pd.py b/decoders/edid/pd.py index 323834b..3b3536e 100644 --- a/decoders/edid/pd.py +++ b/decoders/edid/pd.py @@ -87,8 +87,8 @@ class Decoder(srd.Decoder): optional_probes = [] options = {} annotations = [ - ['EDID fields', 'EDID structure fields'], - ['EDID sections', 'EDID structure sections'], + ['fields', 'EDID structure fields'], + ['sections', 'EDID structure sections'], ] def __init__(self, **kwargs): diff --git a/decoders/edid/test/samsung_le46b620r3p_fields.output b/decoders/edid/test/samsung_le46b620r3p_fields.output new file mode 100644 index 0000000..ec0166b --- /dev/null +++ b/decoders/edid/test/samsung_le46b620r3p_fields.output @@ -0,0 +1,49 @@ +6169-6456 edid: fields: "EDID header" +6557-7248 edid: fields: "SAM (Samsung Electric Company)" +7350-8027 edid: fields: "Product 0x0508" +8128-9579 edid: fields: "Serial 0" +9681-10459 edid: fields: "Manufactured week 48, 2008" +11005-11292 edid: fields: "EDID version: 1.3" +11393-11680 edid: fields: "Signal level standard: 01" +11393-11680 edid: fields: "Supported syncs: separate syncs" +11781-12459 edid: fields: "Physical size: 88x50cm" +12560-12847 edid: fields: "Gamma: 2.20" +12949-13237 edid: fields: "DPMS support: active off" +12949-13237 edid: fields: "Display type: RGB color" +12949-13237 edid: fields: "Generalized timing formula: not supported" +13338-17124 edid: fields: "Chromacity red: X 0.640, Y 0.330" +13338-17124 edid: fields: "Chromacity green: X 0.297, Y 0.598" +13338-17124 edid: fields: "Chromacity blue: X 0.150, Y 0.060" +13338-17124 edid: fields: "Chromacity white: X 0.312, Y 0.328" +17225-18288 edid: fields: "Supported establised modes: 720x400@70Hz, 640x480@60Hz, 640x480@67Hz, 640x480@72Hz, 640x480@75Hz, 800x600@60Hz, 800x600@72Hz, 800x600@75Hz, 832x624@75Hz, 1024x768@60Hz, 1024x768@70Hz, 1024x768@75Hz, 1280x1024@75Hz, 1152x870@75Hz" +18389-19453 edid: fields: "Supported standard modes: 1152x864@75Hz, 1280x800@60Hz, 1280x960@60Hz, 1280x1024@60Hz, 1440x900@60Hz, 1440x900@75Hz, 1680x1050@60Hz" +24617-25295 edid: fields: "Pixel clock: 148.50 MHz" +25396-26459 edid: fields: "Horizontal active: 1920" +25784-26459 edid: fields: "Horizontal blanking: 280" +26560-27632 edid: fields: "Vertical active: 1080" +26950-27632 edid: fields: "Vertical blanking: 45" +27734-29185 edid: fields: "Horizontal sync offset: 88" +28122-29185 edid: fields: "Horizontal sync pulse width: 44" +28510-29185 edid: fields: "Vertical sync offset: 4" +28510-29185 edid: fields: "Vertical sync pulse width: 5" +29286-30359 edid: fields: "Physical size: 886x498mm" +31237-31523 edid: fields: "Flags: sync type digital separate (Vsync polarity positive, Hsync polarity positive)" +31625-32305 edid: fields: "Pixel clock: 85.50 MHz" +32407-33475 edid: fields: "Horizontal active: 1360" +32795-33475 edid: fields: "Horizontal blanking: 432" +33576-34645 edid: fields: "Vertical active: 768" +33964-34645 edid: fields: "Vertical blanking: 27" +34746-36198 edid: fields: "Horizontal sync offset: 64" +35134-36198 edid: fields: "Horizontal sync pulse width: 112" +35522-36198 edid: fields: "Vertical sync offset: 3" +35522-36198 edid: fields: "Vertical sync pulse width: 6" +36299-37373 edid: fields: "Physical size: 886x498mm" +38251-38538 edid: fields: "Flags: sync type digital separate (Vsync polarity positive, Hsync polarity positive)" +40593-40880 edid: fields: "Minimum vertical rate: 60Hz" +40981-41268 edid: fields: "Maximum vertical rate: 75Hz" +41369-41656 edid: fields: "Minimum horizontal rate: 30kHz" +41757-42047 edid: fields: "Maximum horizontal rate: 81kHz" +42148-42434 edid: fields: "Maximum pixel clock: 150MHz" +45644-52565 edid: fields: "Model name: SAMSUNG" +52666-52953 edid: fields: "Extensions present: 0" +53054-53341 edid: fields: "Checksum: 155 (OK)" diff --git a/decoders/edid/test/samsung_le46b620r3p_sections.output b/decoders/edid/test/samsung_le46b620r3p_sections.output new file mode 100644 index 0000000..0d6ed8f --- /dev/null +++ b/decoders/edid/test/samsung_le46b620r3p_sections.output @@ -0,0 +1,3 @@ +24617-31911 edid: sections: "Preferred timing descriptor" +31625-38926 edid: sections: "Detailed timing descriptor" +38639-45543 edid: sections: "Monitor range limits" diff --git a/decoders/edid/test/test.conf b/decoders/edid/test/test.conf new file mode 100644 index 0000000..aff116c --- /dev/null +++ b/decoders/edid/test/test.conf @@ -0,0 +1,8 @@ +test tv + protocol-decoder i2c probe scl=0 probe sda=1 + protocol-decoder edid + stack i2c edid + input i2c/edid/samsung_le46b620r3p.sr + output edid annotation class fields match samsung_le46b620r3p_fields.output + output edid annotation class sections match samsung_le46b620r3p_sections.output + diff --git a/decoders/i2c/pd.py b/decoders/i2c/pd.py index 60e9c45..45b84e8 100644 --- a/decoders/i2c/pd.py +++ b/decoders/i2c/pd.py @@ -81,16 +81,16 @@ class Decoder(srd.Decoder): 'address_format': ['Displayed slave address format', 'shifted'], } annotations = [ - ['Start', 'Start condition'], - ['Repeat start', 'Repeat start condition'], - ['Stop', 'Stop condition'], - ['ACK', 'ACK'], - ['NACK', 'NACK'], - ['Address read', 'Address read'], - ['Address write', 'Address write'], - ['Data read', 'Data read'], - ['Data write', 'Data write'], - ['Warnings', 'Human-readable warnings'], + ['start', 'Start condition'], + ['repeat-start', 'Repeat start condition'], + ['stop', 'Stop condition'], + ['ack', 'ACK'], + ['nack', 'NACK'], + ['address-read', 'Address read'], + ['address-write', 'Address write'], + ['data-read', 'Data read'], + ['data-write', 'Data write'], + ['warnings', 'Human-readable warnings'], ] binary = ( 'Address read', diff --git a/decoders/i2c/test/gigabyte_6vle_vxl_i2c.output b/decoders/i2c/test/gigabyte_6vle_vxl_i2c.output new file mode 100644 index 0000000..2039e82 --- /dev/null +++ b/decoders/i2c/test/gigabyte_6vle_vxl_i2c.output @@ -0,0 +1,130 @@ +3670527-3670527 i2c: start: "Start" "S" +3670622-3671478 i2c: address-write: "Address write: 50" "AW: 50" "50" +3671600-3671600 i2c: ack: "ACK" "A" +3671721-3672577 i2c: data-write: "Data write: 1B" "DW: 1B" "1B" +3672699-3672699 i2c: ack: "ACK" "A" +3672881-3672881 i2c: repeat-start: "Start repeat" "Sr" +3672970-3673826 i2c: address-read: "Address read: 50" "AR: 50" "50" +3673948-3673948 i2c: ack: "ACK" "A" +3674069-3674925 i2c: data-read: "Data read: 50" "DR: 50" "50" +3675081-3675081 i2c: nack: "NACK" "N" +3675231-3675231 i2c: stop: "Stop" "P" +3675596-3675596 i2c: start: "Start" "S" +3675690-3676546 i2c: address-write: "Address write: 50" "AW: 50" "50" +3676669-3676669 i2c: ack: "ACK" "A" +3676790-3677646 i2c: data-write: "Data write: 1E" "DW: 1E" "1E" +3677768-3677768 i2c: ack: "ACK" "A" +3677950-3677950 i2c: repeat-start: "Start repeat" "Sr" +3678039-3678895 i2c: address-read: "Address read: 50" "AR: 50" "50" +3679017-3679017 i2c: ack: "ACK" "A" +3679138-3679994 i2c: data-read: "Data read: 2D" "DR: 2D" "2D" +3680149-3680149 i2c: nack: "NACK" "N" +3680299-3680299 i2c: stop: "Stop" "P" +3680665-3680665 i2c: start: "Start" "S" +3680759-3681615 i2c: address-write: "Address write: 50" "AW: 50" "50" +3681737-3681737 i2c: ack: "ACK" "A" +3681858-3682714 i2c: data-write: "Data write: 1D" "DW: 1D" "1D" +3682836-3682836 i2c: ack: "ACK" "A" +3683018-3683018 i2c: repeat-start: "Start repeat" "Sr" +3683107-3683963 i2c: address-read: "Address read: 50" "AR: 50" "50" +3684085-3684085 i2c: ack: "ACK" "A" +3684207-3685062 i2c: data-read: "Data read: 50" "DR: 50" "50" +3685218-3685218 i2c: nack: "NACK" "N" +3685368-3685368 i2c: stop: "Stop" "P" +3700267-3700267 i2c: start: "Start" "S" +3700362-3701217 i2c: address-write: "Address write: 69" "AW: 69" "69" +3701340-3701340 i2c: ack: "ACK" "A" +3701461-3702317 i2c: data-write: "Data write: 00" "DW: 00" "00" +3702439-3702439 i2c: ack: "ACK" "A" +3702621-3702621 i2c: repeat-start: "Start repeat" "Sr" +3702710-3703566 i2c: address-read: "Address read: 69" "AR: 69" "69" +3703688-3703688 i2c: ack: "ACK" "A" +3703809-3704665 i2c: data-read: "Data read: 0F" "DR: 0F" "0F" +3704787-3704787 i2c: ack: "ACK" "A" +3704908-3705764 i2c: data-read: "Data read: 06" "DR: 06" "06" +3705886-3705886 i2c: ack: "ACK" "A" +3706007-3706863 i2c: data-read: "Data read: FF" "DR: FF" "FF" +3706985-3706985 i2c: ack: "ACK" "A" +3707107-3707962 i2c: data-read: "Data read: FF" "DR: FF" "FF" +3708085-3708085 i2c: ack: "ACK" "A" +3708206-3709062 i2c: data-read: "Data read: FF" "DR: FF" "FF" +3709184-3709184 i2c: ack: "ACK" "A" +3709305-3710161 i2c: data-read: "Data read: FF" "DR: FF" "FF" +3710283-3710283 i2c: ack: "ACK" "A" +3710404-3711260 i2c: data-read: "Data read: FF" "DR: FF" "FF" +3711382-3711382 i2c: ack: "ACK" "A" +3711503-3712359 i2c: data-read: "Data read: 51" "DR: 51" "51" +3712481-3712481 i2c: ack: "ACK" "A" +3712603-3713458 i2c: data-read: "Data read: 86" "DR: 86" "86" +3713581-3713581 i2c: ack: "ACK" "A" +3713702-3714558 i2c: data-read: "Data read: 0F" "DR: 0F" "0F" +3714680-3714680 i2c: ack: "ACK" "A" +3714801-3715657 i2c: data-read: "Data read: 08" "DR: 08" "08" +3715779-3715779 i2c: ack: "ACK" "A" +3715900-3716756 i2c: data-read: "Data read: 01" "DR: 01" "01" +3716878-3716878 i2c: ack: "ACK" "A" +3716999-3717855 i2c: data-read: "Data read: 88" "DR: 88" "88" +3717977-3717977 i2c: ack: "ACK" "A" +3718099-3718954 i2c: data-read: "Data read: 0E" "DR: 0E" "0E" +3719077-3719077 i2c: ack: "ACK" "A" +3719198-3720054 i2c: data-read: "Data read: E5" "DR: E5" "E5" +3720176-3720176 i2c: ack: "ACK" "A" +3720297-3721153 i2c: data-read: "Data read: F7" "DR: F7" "F7" +3721308-3721308 i2c: nack: "NACK" "N" +3721458-3721458 i2c: stop: "Stop" "P" +3825148-3825148 i2c: start: "Start" "S" +3825243-3826099 i2c: address-write: "Address write: 69" "AW: 69" "69" +3826221-3826221 i2c: ack: "ACK" "A" +3826342-3827198 i2c: data-write: "Data write: 00" "DW: 00" "00" +3827320-3827320 i2c: ack: "ACK" "A" +3827441-3828297 i2c: data-write: "Data write: 18" "DW: 18" "18" +3828419-3828419 i2c: ack: "ACK" "A" +3828540-3829396 i2c: data-write: "Data write: AE" "DW: AE" "AE" +3829518-3829518 i2c: ack: "ACK" "A" +3829640-3830496 i2c: data-write: "Data write: FF" "DW: FF" "FF" +3830618-3830618 i2c: ack: "ACK" "A" +3830739-3831595 i2c: data-write: "Data write: EF" "DW: EF" "EF" +3831717-3831717 i2c: ack: "ACK" "A" +3831838-3832694 i2c: data-write: "Data write: FB" "DW: FB" "FB" +3832816-3832816 i2c: ack: "ACK" "A" +3832937-3833793 i2c: data-write: "Data write: 0F" "DW: 0F" "0F" +3833915-3833915 i2c: ack: "ACK" "A" +3834036-3834892 i2c: data-write: "Data write: C0" "DW: C0" "C0" +3835014-3835014 i2c: ack: "ACK" "A" +3835136-3835992 i2c: data-write: "Data write: F1" "DW: F1" "F1" +3836114-3836114 i2c: ack: "ACK" "A" +3836235-3837091 i2c: data-write: "Data write: 17" "DW: 17" "17" +3837213-3837213 i2c: ack: "ACK" "A" +3837334-3838190 i2c: data-write: "Data write: 18" "DW: 18" "18" +3838312-3838312 i2c: ack: "ACK" "A" +3838433-3839289 i2c: data-write: "Data write: 10" "DW: 10" "10" +3839411-3839411 i2c: ack: "ACK" "A" +3839532-3840388 i2c: data-write: "Data write: 7A" "DW: 7A" "7A" +3840510-3840510 i2c: ack: "ACK" "A" +3840632-3841488 i2c: data-write: "Data write: 8C" "DW: 8C" "8C" +3841610-3841610 i2c: ack: "ACK" "A" +3841731-3842587 i2c: data-write: "Data write: 81" "DW: 81" "81" +3842709-3842709 i2c: ack: "ACK" "A" +3842830-3843686 i2c: data-write: "Data write: 1F" "DW: 1F" "1F" +3843808-3843808 i2c: ack: "ACK" "A" +3843929-3844785 i2c: data-write: "Data write: 18" "DW: 18" "18" +3844907-3844907 i2c: ack: "ACK" "A" +3845028-3845884 i2c: data-write: "Data write: 00" "DW: 00" "00" +3846006-3846006 i2c: ack: "ACK" "A" +3846128-3846984 i2c: data-write: "Data write: 00" "DW: 00" "00" +3847106-3847106 i2c: ack: "ACK" "A" +3847227-3848083 i2c: data-write: "Data write: 00" "DW: 00" "00" +3848205-3848205 i2c: ack: "ACK" "A" +3848326-3849182 i2c: data-write: "Data write: 00" "DW: 00" "00" +3849304-3849304 i2c: ack: "ACK" "A" +3849425-3850281 i2c: data-write: "Data write: 00" "DW: 00" "00" +3850403-3850403 i2c: ack: "ACK" "A" +3850524-3851380 i2c: data-write: "Data write: 00" "DW: 00" "00" +3851502-3851502 i2c: ack: "ACK" "A" +3851624-3852480 i2c: data-write: "Data write: 00" "DW: 00" "00" +3852602-3852602 i2c: ack: "ACK" "A" +3852723-3853579 i2c: data-write: "Data write: 00" "DW: 00" "00" +3853701-3853701 i2c: ack: "ACK" "A" +3853822-3854678 i2c: data-write: "Data write: 00" "DW: 00" "00" +3854800-3854800 i2c: ack: "ACK" "A" +3854950-3854950 i2c: stop: "Stop" "P" diff --git a/decoders/i2c/test/rtc_ds1307_200khz.output b/decoders/i2c/test/rtc_ds1307_200khz.output new file mode 100644 index 0000000..f205406 --- /dev/null +++ b/decoders/i2c/test/rtc_ds1307_200khz.output @@ -0,0 +1,181 @@ +0-0 i2c: start: "Start" "S" +1-16 i2c: address-write: "Address write: 68" "AW: 68" "68" +18-18 i2c: ack: "ACK" "A" +24-39 i2c: data-write: "Data write: 00" "DW: 00" "00" +41-41 i2c: ack: "ACK" "A" +42-57 i2c: data-write: "Data write: 30" "DW: 30" "30" +59-59 i2c: ack: "ACK" "A" +60-75 i2c: data-write: "Data write: 35" "DW: 35" "35" +77-77 i2c: ack: "ACK" "A" +78-93 i2c: data-write: "Data write: 23" "DW: 23" "23" +95-95 i2c: ack: "ACK" "A" +96-111 i2c: data-write: "Data write: 01" "DW: 01" "01" +113-113 i2c: ack: "ACK" "A" +114-129 i2c: data-write: "Data write: 10" "DW: 10" "10" +131-131 i2c: ack: "ACK" "A" +132-147 i2c: data-write: "Data write: 03" "DW: 03" "03" +149-149 i2c: ack: "ACK" "A" +150-165 i2c: data-write: "Data write: 13" "DW: 13" "13" +167-167 i2c: ack: "ACK" "A" +171-171 i2c: stop: "Stop" "P" +253-253 i2c: start: "Start" "S" +254-269 i2c: address-write: "Address write: 68" "AW: 68" "68" +271-271 i2c: ack: "ACK" "A" +272-287 i2c: data-write: "Data write: 00" "DW: 00" "00" +289-289 i2c: ack: "ACK" "A" +323-323 i2c: repeat-start: "Start repeat" "Sr" +324-339 i2c: address-read: "Address read: 68" "AR: 68" "68" +341-341 i2c: ack: "ACK" "A" +342-357 i2c: data-read: "Data read: 30" "DR: 30" "30" +359-359 i2c: ack: "ACK" "A" +360-375 i2c: data-read: "Data read: 35" "DR: 35" "35" +377-377 i2c: ack: "ACK" "A" +378-393 i2c: data-read: "Data read: 23" "DR: 23" "23" +395-395 i2c: ack: "ACK" "A" +396-411 i2c: data-read: "Data read: 01" "DR: 01" "01" +413-413 i2c: ack: "ACK" "A" +414-429 i2c: data-read: "Data read: 10" "DR: 10" "10" +431-431 i2c: ack: "ACK" "A" +432-447 i2c: data-read: "Data read: 03" "DR: 03" "03" +449-449 i2c: ack: "ACK" "A" +450-465 i2c: data-read: "Data read: 13" "DR: 13" "13" +467-467 i2c: nack: "NACK" "N" +471-471 i2c: stop: "Stop" "P" +3548-3548 i2c: start: "Start" "S" +3549-3564 i2c: address-write: "Address write: 68" "AW: 68" "68" +3566-3566 i2c: ack: "ACK" "A" +3567-3582 i2c: data-write: "Data write: 00" "DW: 00" "00" +3584-3584 i2c: ack: "ACK" "A" +3608-3608 i2c: repeat-start: "Start repeat" "Sr" +3609-3624 i2c: address-read: "Address read: 68" "AR: 68" "68" +3626-3626 i2c: ack: "ACK" "A" +3627-3642 i2c: data-read: "Data read: 30" "DR: 30" "30" +3644-3644 i2c: ack: "ACK" "A" +3645-3660 i2c: data-read: "Data read: 35" "DR: 35" "35" +3662-3662 i2c: ack: "ACK" "A" +3663-3678 i2c: data-read: "Data read: 23" "DR: 23" "23" +3680-3680 i2c: ack: "ACK" "A" +3681-3696 i2c: data-read: "Data read: 01" "DR: 01" "01" +3698-3698 i2c: ack: "ACK" "A" +3699-3714 i2c: data-read: "Data read: 10" "DR: 10" "10" +3716-3716 i2c: ack: "ACK" "A" +3717-3732 i2c: data-read: "Data read: 03" "DR: 03" "03" +3734-3734 i2c: ack: "ACK" "A" +3735-3750 i2c: data-read: "Data read: 13" "DR: 13" "13" +3752-3752 i2c: nack: "NACK" "N" +3756-3756 i2c: stop: "Stop" "P" +7470-7470 i2c: start: "Start" "S" +7471-7486 i2c: address-write: "Address write: 68" "AW: 68" "68" +7488-7488 i2c: ack: "ACK" "A" +7489-7504 i2c: data-write: "Data write: 00" "DW: 00" "00" +7506-7506 i2c: ack: "ACK" "A" +7529-7529 i2c: repeat-start: "Start repeat" "Sr" +7530-7545 i2c: address-read: "Address read: 68" "AR: 68" "68" +7547-7547 i2c: ack: "ACK" "A" +7548-7563 i2c: data-read: "Data read: 30" "DR: 30" "30" +7565-7565 i2c: ack: "ACK" "A" +7566-7581 i2c: data-read: "Data read: 35" "DR: 35" "35" +7583-7583 i2c: ack: "ACK" "A" +7584-7599 i2c: data-read: "Data read: 23" "DR: 23" "23" +7601-7601 i2c: ack: "ACK" "A" +7602-7617 i2c: data-read: "Data read: 01" "DR: 01" "01" +7619-7619 i2c: ack: "ACK" "A" +7620-7635 i2c: data-read: "Data read: 10" "DR: 10" "10" +7637-7637 i2c: ack: "ACK" "A" +7638-7653 i2c: data-read: "Data read: 03" "DR: 03" "03" +7655-7655 i2c: ack: "ACK" "A" +7656-7671 i2c: data-read: "Data read: 13" "DR: 13" "13" +7673-7673 i2c: nack: "NACK" "N" +7677-7677 i2c: stop: "Stop" "P" +11405-11405 i2c: start: "Start" "S" +11407-11422 i2c: address-write: "Address write: 68" "AW: 68" "68" +11424-11424 i2c: ack: "ACK" "A" +11425-11440 i2c: data-write: "Data write: 00" "DW: 00" "00" +11442-11442 i2c: ack: "ACK" "A" +11466-11466 i2c: repeat-start: "Start repeat" "Sr" +11467-11482 i2c: address-read: "Address read: 68" "AR: 68" "68" +11484-11484 i2c: ack: "ACK" "A" +11485-11500 i2c: data-read: "Data read: 30" "DR: 30" "30" +11502-11502 i2c: ack: "ACK" "A" +11503-11518 i2c: data-read: "Data read: 35" "DR: 35" "35" +11520-11520 i2c: ack: "ACK" "A" +11521-11536 i2c: data-read: "Data read: 23" "DR: 23" "23" +11538-11538 i2c: ack: "ACK" "A" +11539-11554 i2c: data-read: "Data read: 01" "DR: 01" "01" +11556-11556 i2c: ack: "ACK" "A" +11557-11572 i2c: data-read: "Data read: 10" "DR: 10" "10" +11574-11574 i2c: ack: "ACK" "A" +11575-11590 i2c: data-read: "Data read: 03" "DR: 03" "03" +11592-11592 i2c: ack: "ACK" "A" +11593-11608 i2c: data-read: "Data read: 13" "DR: 13" "13" +11610-11610 i2c: nack: "NACK" "N" +11614-11614 i2c: stop: "Stop" "P" +15332-15332 i2c: start: "Start" "S" +15333-15348 i2c: address-write: "Address write: 68" "AW: 68" "68" +15350-15350 i2c: ack: "ACK" "A" +15351-15366 i2c: data-write: "Data write: 00" "DW: 00" "00" +15368-15368 i2c: ack: "ACK" "A" +15400-15400 i2c: repeat-start: "Start repeat" "Sr" +15401-15416 i2c: address-read: "Address read: 68" "AR: 68" "68" +15418-15418 i2c: ack: "ACK" "A" +15419-15434 i2c: data-read: "Data read: 30" "DR: 30" "30" +15436-15436 i2c: ack: "ACK" "A" +15437-15452 i2c: data-read: "Data read: 35" "DR: 35" "35" +15454-15454 i2c: ack: "ACK" "A" +15455-15470 i2c: data-read: "Data read: 23" "DR: 23" "23" +15472-15472 i2c: ack: "ACK" "A" +15473-15488 i2c: data-read: "Data read: 01" "DR: 01" "01" +15490-15490 i2c: ack: "ACK" "A" +15491-15506 i2c: data-read: "Data read: 10" "DR: 10" "10" +15508-15508 i2c: ack: "ACK" "A" +15509-15524 i2c: data-read: "Data read: 03" "DR: 03" "03" +15526-15526 i2c: ack: "ACK" "A" +15527-15542 i2c: data-read: "Data read: 13" "DR: 13" "13" +15544-15544 i2c: nack: "NACK" "N" +15548-15548 i2c: stop: "Stop" "P" +19253-19253 i2c: start: "Start" "S" +19254-19269 i2c: address-write: "Address write: 68" "AW: 68" "68" +19271-19271 i2c: ack: "ACK" "A" +19273-19288 i2c: data-write: "Data write: 00" "DW: 00" "00" +19290-19290 i2c: ack: "ACK" "A" +19359-19359 i2c: repeat-start: "Start repeat" "Sr" +19360-19375 i2c: address-read: "Address read: 68" "AR: 68" "68" +19377-19377 i2c: ack: "ACK" "A" +19378-19393 i2c: data-read: "Data read: 30" "DR: 30" "30" +19395-19395 i2c: ack: "ACK" "A" +19396-19411 i2c: data-read: "Data read: 35" "DR: 35" "35" +19413-19413 i2c: ack: "ACK" "A" +19414-19429 i2c: data-read: "Data read: 23" "DR: 23" "23" +19431-19431 i2c: ack: "ACK" "A" +19432-19447 i2c: data-read: "Data read: 01" "DR: 01" "01" +19449-19449 i2c: ack: "ACK" "A" +19450-19465 i2c: data-read: "Data read: 10" "DR: 10" "10" +19467-19467 i2c: ack: "ACK" "A" +19468-19483 i2c: data-read: "Data read: 03" "DR: 03" "03" +19485-19485 i2c: ack: "ACK" "A" +19486-19501 i2c: data-read: "Data read: 13" "DR: 13" "13" +19503-19503 i2c: nack: "NACK" "N" +19507-19507 i2c: stop: "Stop" "P" +23211-23211 i2c: start: "Start" "S" +23213-23228 i2c: address-write: "Address write: 68" "AW: 68" "68" +23230-23230 i2c: ack: "ACK" "A" +23264-23279 i2c: data-write: "Data write: 00" "DW: 00" "00" +23281-23281 i2c: ack: "ACK" "A" +23299-23299 i2c: repeat-start: "Start repeat" "Sr" +23300-23315 i2c: address-read: "Address read: 68" "AR: 68" "68" +23317-23317 i2c: ack: "ACK" "A" +23318-23333 i2c: data-read: "Data read: 30" "DR: 30" "30" +23335-23335 i2c: ack: "ACK" "A" +23336-23351 i2c: data-read: "Data read: 35" "DR: 35" "35" +23353-23353 i2c: ack: "ACK" "A" +23354-23369 i2c: data-read: "Data read: 23" "DR: 23" "23" +23371-23371 i2c: ack: "ACK" "A" +23372-23387 i2c: data-read: "Data read: 01" "DR: 01" "01" +23389-23389 i2c: ack: "ACK" "A" +23390-23405 i2c: data-read: "Data read: 10" "DR: 10" "10" +23407-23407 i2c: ack: "ACK" "A" +23408-23423 i2c: data-read: "Data read: 03" "DR: 03" "03" +23425-23425 i2c: ack: "ACK" "A" +23426-23441 i2c: data-read: "Data read: 13" "DR: 13" "13" +23443-23443 i2c: nack: "NACK" "N" +23447-23447 i2c: stop: "Stop" "P" diff --git a/decoders/i2c/test/rtc_ds1307_200khz_data_read.output b/decoders/i2c/test/rtc_ds1307_200khz_data_read.output new file mode 100644 index 0000000..31c3503 --- /dev/null +++ b/decoders/i2c/test/rtc_ds1307_200khz_data_read.output @@ -0,0 +1,49 @@ +342-357 i2c: data-read: "Data read: 30" "DR: 30" "30" +360-375 i2c: data-read: "Data read: 35" "DR: 35" "35" +378-393 i2c: data-read: "Data read: 23" "DR: 23" "23" +396-411 i2c: data-read: "Data read: 01" "DR: 01" "01" +414-429 i2c: data-read: "Data read: 10" "DR: 10" "10" +432-447 i2c: data-read: "Data read: 03" "DR: 03" "03" +450-465 i2c: data-read: "Data read: 13" "DR: 13" "13" +3627-3642 i2c: data-read: "Data read: 30" "DR: 30" "30" +3645-3660 i2c: data-read: "Data read: 35" "DR: 35" "35" +3663-3678 i2c: data-read: "Data read: 23" "DR: 23" "23" +3681-3696 i2c: data-read: "Data read: 01" "DR: 01" "01" +3699-3714 i2c: data-read: "Data read: 10" "DR: 10" "10" +3717-3732 i2c: data-read: "Data read: 03" "DR: 03" "03" +3735-3750 i2c: data-read: "Data read: 13" "DR: 13" "13" +7548-7563 i2c: data-read: "Data read: 30" "DR: 30" "30" +7566-7581 i2c: data-read: "Data read: 35" "DR: 35" "35" +7584-7599 i2c: data-read: "Data read: 23" "DR: 23" "23" +7602-7617 i2c: data-read: "Data read: 01" "DR: 01" "01" +7620-7635 i2c: data-read: "Data read: 10" "DR: 10" "10" +7638-7653 i2c: data-read: "Data read: 03" "DR: 03" "03" +7656-7671 i2c: data-read: "Data read: 13" "DR: 13" "13" +11485-11500 i2c: data-read: "Data read: 30" "DR: 30" "30" +11503-11518 i2c: data-read: "Data read: 35" "DR: 35" "35" +11521-11536 i2c: data-read: "Data read: 23" "DR: 23" "23" +11539-11554 i2c: data-read: "Data read: 01" "DR: 01" "01" +11557-11572 i2c: data-read: "Data read: 10" "DR: 10" "10" +11575-11590 i2c: data-read: "Data read: 03" "DR: 03" "03" +11593-11608 i2c: data-read: "Data read: 13" "DR: 13" "13" +15419-15434 i2c: data-read: "Data read: 30" "DR: 30" "30" +15437-15452 i2c: data-read: "Data read: 35" "DR: 35" "35" +15455-15470 i2c: data-read: "Data read: 23" "DR: 23" "23" +15473-15488 i2c: data-read: "Data read: 01" "DR: 01" "01" +15491-15506 i2c: data-read: "Data read: 10" "DR: 10" "10" +15509-15524 i2c: data-read: "Data read: 03" "DR: 03" "03" +15527-15542 i2c: data-read: "Data read: 13" "DR: 13" "13" +19378-19393 i2c: data-read: "Data read: 30" "DR: 30" "30" +19396-19411 i2c: data-read: "Data read: 35" "DR: 35" "35" +19414-19429 i2c: data-read: "Data read: 23" "DR: 23" "23" +19432-19447 i2c: data-read: "Data read: 01" "DR: 01" "01" +19450-19465 i2c: data-read: "Data read: 10" "DR: 10" "10" +19468-19483 i2c: data-read: "Data read: 03" "DR: 03" "03" +19486-19501 i2c: data-read: "Data read: 13" "DR: 13" "13" +23318-23333 i2c: data-read: "Data read: 30" "DR: 30" "30" +23336-23351 i2c: data-read: "Data read: 35" "DR: 35" "35" +23354-23369 i2c: data-read: "Data read: 23" "DR: 23" "23" +23372-23387 i2c: data-read: "Data read: 01" "DR: 01" "01" +23390-23405 i2c: data-read: "Data read: 10" "DR: 10" "10" +23408-23423 i2c: data-read: "Data read: 03" "DR: 03" "03" +23426-23441 i2c: data-read: "Data read: 13" "DR: 13" "13" diff --git a/decoders/i2c/test/rtc_ds1307_200khz_data_write.output b/decoders/i2c/test/rtc_ds1307_200khz_data_write.output new file mode 100644 index 0000000..1076929 --- /dev/null +++ b/decoders/i2c/test/rtc_ds1307_200khz_data_write.output @@ -0,0 +1,15 @@ +24-39 i2c: data-write: "Data write: 00" "DW: 00" "00" +42-57 i2c: data-write: "Data write: 30" "DW: 30" "30" +60-75 i2c: data-write: "Data write: 35" "DW: 35" "35" +78-93 i2c: data-write: "Data write: 23" "DW: 23" "23" +96-111 i2c: data-write: "Data write: 01" "DW: 01" "01" +114-129 i2c: data-write: "Data write: 10" "DW: 10" "10" +132-147 i2c: data-write: "Data write: 03" "DW: 03" "03" +150-165 i2c: data-write: "Data write: 13" "DW: 13" "13" +272-287 i2c: data-write: "Data write: 00" "DW: 00" "00" +3567-3582 i2c: data-write: "Data write: 00" "DW: 00" "00" +7489-7504 i2c: data-write: "Data write: 00" "DW: 00" "00" +11425-11440 i2c: data-write: "Data write: 00" "DW: 00" "00" +15351-15366 i2c: data-write: "Data write: 00" "DW: 00" "00" +19273-19288 i2c: data-write: "Data write: 00" "DW: 00" "00" +23264-23279 i2c: data-write: "Data write: 00" "DW: 00" "00" diff --git a/decoders/i2c/test/test.conf b/decoders/i2c/test/test.conf new file mode 100644 index 0000000..4e96a00 --- /dev/null +++ b/decoders/i2c/test/test.conf @@ -0,0 +1,12 @@ +test rtc + protocol-decoder i2c probe scl=0 probe sda=1 + input i2c/rtc_dallas_ds1307/rtc_ds1307_200khz.sr + output i2c annotation class data-read match rtc_ds1307_200khz_data_read.output + output i2c annotation class data-write match rtc_ds1307_200khz_data_write.output + output i2c annotation match rtc_ds1307_200khz.output + +test motherboard + protocol-decoder i2c probe scl=0 probe sda=3 + input i2c/gigabyte_6vle-vxl_i2c/gigabyte_6vle_vxl_i2c.sr + output i2c annotation match gigabyte_6vle_vxl_i2c.output + diff --git a/tests/Makefile.am b/tests/Makefile.am index b1ac2bf..8086b9d 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -19,7 +19,6 @@ ## if HAVE_CHECK - TESTS = check_main check_PROGRAMS = ${TESTS} @@ -37,5 +36,11 @@ check_main_CFLAGS = @check_CFLAGS@ check_main_LDADD = $(top_builddir)/libsigrokdecode.la @check_LIBS@ check_main_CPPFLAGS = $(CPPFLAGS_PYTHON) +endif +if HAVE_LIBSIGROK +bin_PROGRAMS = runtc +runtc_SOURCES = runtc.c +runtc_CPPFLAGS = $(CPPFLAGS_PYTHON) +runtc_LDFLAGS = -L/home/bert/sr/lib -lsigrok -lsigrokdecode endif diff --git a/tests/pdtest b/tests/pdtest new file mode 100755 index 0000000..59348d6 --- /dev/null +++ b/tests/pdtest @@ -0,0 +1,399 @@ +#!/usr/bin/env /usr/bin/python3 + +import os +import sys +from getopt import getopt +from tempfile import mkstemp +from subprocess import Popen, PIPE +from difflib import Differ + +DEBUG = False +VERBOSE = False + + +class E_syntax(Exception): + pass +class E_badline(Exception): + pass + +def INFO(msg, end='\n'): + if VERBOSE: + print(msg, end=end) + sys.stdout.flush() + + +def DBG(msg): + if DEBUG: + print(msg) + + +def ERR(msg): + print(msg, file=sys.stderr) + + +def usage(msg=None): + if msg: + print(msg.strip() + '\n') + print("""Usage: testpd [-dvarslR] [test, ...] + -d Turn on debugging + -v Verbose + -a All tests + -l List all tests + -s Show test(s) + -r Run test(s) + -R Save test reports to + Protocol decoder name ("i2c") and optionally test name ("i2c/icc")""") + sys.exit() + + +def check_testcase(tc): + if 'pdlist' not in tc or not tc['pdlist']: + return("No protocol decoders") + if 'input' not in tc or not tc['input']: + return("No input") + if 'output' not in tc or not tc['output']: + return("No output") + for op in tc['output']: + if 'match' not in op: + return("No match in output") + + return None + + +def parse_testfile(path, pd, tc, op_type, op_class): + DBG("Opening '%s'" % path) + tclist = [] + for line in open(path).read().split('\n'): + try: + line = line.strip() + if len(line) == 0 or line[0] == "#": + continue + f = line.split() + if not tclist and f[0] != "test": + # That can't be good. + raise E_badline + key = f.pop(0) + if key == 'test': + if len(f) != 1: + raise E_syntax + # new testcase + tclist.append({ + 'pd': pd, + 'name': f[0], + 'pdlist': [], + 'output': [], + }) + elif key == 'protocol-decoder': + if len(f) < 1: + raise E_syntax + pd_spec = { + 'name': f.pop(0), + 'probes': [], + 'options': [], + } + while len(f): + if len(f) == 1: + # Always needs + raise E_syntax + a, b = f[:2] + f = f[2:] + if '=' not in b: + raise E_syntax + opt, val = b.split('=') + if a == 'probe': + try: + val = int(val) + except: + raise E_syntax + pd_spec['probes'].append([opt, val]) + elif a == 'option': + pd_spec['options'].append([opt, val]) + else: + raise E_syntax + tclist[-1]['pdlist'].append(pd_spec) + elif key == 'stack': + if len(f) < 2: + raise E_syntax + tclist[-1]['stack'] = f + elif key == 'input': + if len(f) != 1: + raise E_syntax + tclist[-1]['input'] = f[0] + elif key == 'output': + op_spec = { + 'pd': f.pop(0), + 'type': f.pop(0), + } + while len(f): + if len(f) == 1: + # Always needs + raise E_syntax + a, b = f[:2] + f = f[2:] + if a == 'class': + op_spec['class'] = b + elif a == 'match': + op_spec['match'] = b + else: + raise E_syntax + tclist[-1]['output'].append(op_spec) + else: + raise E_badline + except E_badline as e: + ERR("Invalid syntax in %s: line '%s'" % (path, line)) + return [] + except E_syntax as e: + ERR("Unable to parse %s: unknown line '%s'" % (path, line)) + return [] + + # If a specific testcase was requested, keep only that one. + if tc is not None: + target_tc = None + for t in tclist: + if t['name'] == tc: + target_tc = t + break + # ...and a specific output type + if op_type is not None: + target_oplist = [] + for op in target_tc['output']: + if op['type'] == op_type: + # ...and a specific output class + if op_class is None or ('class' in op and op['class'] == op_class): + target_oplist.append(op) + DBG("match on [%s]" % str(op)) + target_tc['output'] = target_oplist + if target_tc is None: + tclist = [] + else: + tclist = [target_tc] + for t in tclist: + error = check_testcase(t) + if error: + ERR("Error in %s: %s" % (path, error)) + return [] + + return tclist + + +def get_tests(testnames): + tests = [] + for testspec in testnames: + # Optional testspec in the form i2c/rtc + tc = op_type = op_class = None + ts = testspec.strip("/").split("/") + pd = ts.pop(0) + if ts: + tc = ts.pop(0) + if ts: + op_type = ts.pop(0) + if ts: + op_class = ts.pop(0) + path = os.path.join(decoders_dir, pd) + if not os.path.isdir(path): + # User specified non-existent PD + raise Exception("%s not found." % path) + path = os.path.join(decoders_dir, pd, "test/test.conf") + if not os.path.exists(path): + # PD doesn't have any tests yet + continue + tests.append(parse_testfile(path, pd, tc, op_type, op_class)) + + return tests + + +def diff_files(f1, f2): + t1 = open(f1).readlines() + t2 = open(f2).readlines() + diff = [] + d = Differ() + for line in d.compare(t1, t2): + if line[:2] in ('- ', '+ '): + diff.append(line.strip()) + + return diff + + +def run_tests(tests): + errors = 0 + results = [] + cmd = os.path.join(tests_dir, 'runtc') + for tclist in tests: + for tc in tclist: + args = [cmd] + for pd in tc['pdlist']: + args.extend(['-P', pd['name']]) + for label, probe in pd['probes']: + args.extend(['-p', "%s=%d" % (label, probe)]) + for option, value in pd['options']: + args.extend(['-o', "%s=%s" % (option, value)]) + args.extend(['-i', os.path.join(dumps_dir, tc['input'])]) + for op in tc['output']: + name = "%s/%s/%s" % (tc['pd'], tc['name'], op['type']) + opargs = ['-O', "%s:%s" % (op['pd'], op['type'])] + if 'class' in op: + opargs[-1] += ":%s" % op['class'] + name += "/%s" % op['class'] + if VERBOSE: + dots = '.' * (60 - len(name) - 2) + INFO("%s %s " % (name, dots), end='') + results.append({ + 'testcase': name, + }) + try: + fd, outfile = mkstemp() + os.close(fd) + opargs.extend(['-f', outfile]) + DBG("Running %s %s" % (cmd, ' '.join(args + opargs))) + stdout, stderr = Popen(args + opargs, stdout=PIPE, stderr=PIPE).communicate() + if stdout: + results[-1]['statistics'] = stdout.decode('utf-8').strip() + if stderr: + results[-1]['error'] = stderr.decode('utf-8').strip() + errors += 1 + match = "%s/%s/test/%s" % (decoders_dir, op['pd'], op['match']) + diff = diff_files(match, outfile) + if diff: + results[-1]['diff'] = diff + except Exception as e: + results[-1]['error'] = str(e) + finally: + os.unlink(outfile) + if VERBOSE: + if 'diff' in results[-1]: + INFO("Output mismatch") + elif 'error' in results[-1]: + error = results[-1]['error'] + if len(error) > 20: + error = error[:17] + '...' + INFO(error) + else: + INFO("OK") + gen_report(results[-1]) + + return results, errors + + +def gen_report(result): + out = [] + if 'error' in result: + out.append("Error:") + out.append(result['error']) + out.append('') + if 'diff' in result: + out.append("Test output mismatch:") + out.extend(result['diff']) + out.append('') + if 'statistics' in result: + out.extend(["Statistics:", result['statistics']]) + out.append('') + + if out: + text = "Testcase: %s\n" % result['testcase'] + text += '\n'.join(out) + else: + return + + if report_dir: + filename = result['testcase'].replace('/', '_') + open(os.path.join(report_dir, filename), 'w').write(text) + else: + print(text) + + +def show_tests(tests): + for tclist in tests: + for tc in tclist: + print("Testcase: %s/%s" % (tc['pd'], tc['name'])) + for pd in tc['pdlist']: + print(" Protocol decoder: %s" % pd['name']) + for label, probe in pd['probes']: + print(" Probe %s=%d" % (label, probe)) + for option, value in pd['options']: + print(" Option %s=%d" % (option, value)) + if 'stack' in tc: + print(" Stack: %s" % ' '.join(tc['stack'])) + print(" Input: %s" % tc['input']) + for op in tc['output']: + print(" Output:\n Protocol decoder: %s" % op['pd']) + print(" Type: %s" % op['type']) + if 'class' in op: + print(" Class: %s" % op['class']) + print(" Match: %s" % op['match']) + print() + + +def list_tests(tests): + for tclist in tests: + for tc in tclist: + for op in tc['output']: + line = "%s/%s/%s" % (tc['pd'], tc['name'], op['type']) + if 'class' in op: + line += "/%s" % op['class'] + print(line) + + +# +# main +# + +# project root +tests_dir = os.path.abspath(os.path.dirname(sys.argv[0])) +base_dir = os.path.abspath(os.path.join(os.curdir, tests_dir, os.path.pardir)) +dumps_dir = os.path.abspath(os.path.join(base_dir, os.path.pardir, 'sigrok-dumps')) +decoders_dir = os.path.abspath(os.path.join(base_dir, 'decoders')) + +if len(sys.argv) == 1: + usage() + +opt_all = opt_run = opt_show = opt_list = False +report_dir = None +opts, args = getopt(sys.argv[1:], "dvarslR:") +for opt, arg in opts: + if opt == '-d': + DEBUG = True + if opt == '-v': + VERBOSE = True + elif opt == '-a': + opt_all = True + elif opt == '-r': + opt_run = True + elif opt == '-s': + opt_show = True + elif opt == '-l': + opt_list = True + elif opt == '-R': + report_dir = arg + +if opt_run and opt_show: + usage("Use either -s or -r, not both.") +if args and opt_all: + usage("Specify either -a or tests, not both.") +if report_dir is not None and not os.path.isdir(report_dir): + usage("%s is not a directory" % report_dir) + +ret = 0 +try: + if args: + testlist = get_tests(args) + elif opt_all: + testlist = get_tests(os.listdir(decoders_dir)) + else: + usage("Specify either -a or tests.") + + if opt_run: + results, errors = run_tests(testlist) + ret = errors + elif opt_show: + show_tests(testlist) + elif opt_list: + list_tests(testlist) + else: + usage() +except Exception as e: + print("Error: %s" % str(e)) + if DEBUG: + raise + +sys.exit(ret) + diff --git a/tests/runtc.c b/tests/runtc.c new file mode 100644 index 0000000..78dbf5e --- /dev/null +++ b/tests/runtc.c @@ -0,0 +1,482 @@ +/* + * This file is part of the libsigrokdecode project. + * + * Copyright (C) 2013 Bert Vermeulen + * + * 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 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU 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 . + */ + +#include "../libsigrokdecode.h" +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#ifdef __LINUX__ +#include +#endif +#include "../config.h" + + +int debug = FALSE; +int statistics = FALSE; + +struct probe { + char *name; + int probe; +}; + +struct option { + char *key; + char *value; +}; + +struct pd { + char *name; + GSList *probes; + GSList *options; +}; + +struct output { + char *pd; + int type; + char *class; + int class_idx; + char *outfile; + int outfd; +}; + + +void logmsg(char *prefix, FILE *out, const char *format, va_list args) +{ + if (prefix) + fprintf(out, "%s", prefix); + vfprintf(out, format, args); + fprintf(out, "\n"); +} + +void DBG(const char *format, ...) +{ + va_list args; + + if (!debug) + return; + va_start(args, format); + logmsg("DBG: ", stdout, format, args); + va_end(args); +} + +void ERR(const char *format, ...) +{ + va_list args; + + va_start(args, format); + logmsg("Error: ", stderr, format, args); + va_end(args); +} + +int srd_log(void *cb_data, int loglevel, const char *format, va_list args) +{ + (void)cb_data; + + if (loglevel == SRD_LOG_ERR || loglevel == SRD_LOG_WARN) + logmsg("Error: srd: ", stderr, format, args); + else if (loglevel >= SRD_LOG_DBG && debug) + logmsg("DBG: srd: ", stdout, format, args); + + return SRD_OK; +} + +void usage(char *msg) +{ + if (msg) + fprintf(stderr, "%s\n", msg); + + //while((c = getopt(argc, argv, "dP:p:o:i:O:f:S")) != -1) { + printf("Usage: runtc [-dPpoiOf]\n"); + printf(" -d Debug\n"); + printf(" -P \n"); + printf(" -p (optional)\n"); + printf(" -o (optional)\n"); + printf(" -i \n"); + printf(" -O \n"); + printf(" -f (optional)\n"); + exit(msg ? 1 : 0); + +} + +static void srd_cb_ann(struct srd_proto_data *pdata, void *cb_data) +{ + struct srd_decoder *dec; + struct srd_proto_data_annotation *pda; + struct output *op; + GString *line; + int i; + char **dec_ann; + + DBG("Annotation from %s", pdata->pdo->di->inst_id); + op = cb_data; + pda = pdata->data; + dec = pdata->pdo->di->decoder; + + if (strcmp(pdata->pdo->di->inst_id, op->pd)) + /* This is not the PD selected for output. */ + return; + + if (op->class_idx != -1 && op->class_idx != pda->ann_format) + /* This output takes a specific annotation class, + * but not the one that just came in. */ + return; + + dec_ann = g_slist_nth_data(dec->annotations, pda->ann_format); + line = g_string_sized_new(256); + g_string_printf(line, "%"PRIu64"-%"PRIu64" %s: %s:", + pdata->start_sample, pdata->end_sample, + pdata->pdo->di->inst_id, dec_ann[0]); + for (i = 0; pda->ann_text[i]; i++) + g_string_append_printf(line, " \"%s\"", pda->ann_text[i]); + g_string_append(line, "\n"); + if (write(op->outfd, line->str, line->len) == -1) + ERR("Oops!"); + g_string_free(line, TRUE); + +} + +static void sr_cb(const struct sr_dev_inst *sdi, + const struct sr_datafeed_packet *packet, void *cb_data) +{ + const struct sr_datafeed_logic *logic; + struct srd_session *sess; + GVariant *gvar; + uint64_t samplerate; + int num_samples; + static int samplecnt = 0; + + sess = cb_data; + + switch (packet->type) { + case SR_DF_HEADER: + DBG("Received SR_DF_HEADER"); + if (sr_config_get(sdi->driver, sdi, NULL, SR_CONF_SAMPLERATE, + &gvar) != SR_OK) { + ERR("Getting samplerate failed"); + break; + } + samplerate = g_variant_get_uint64(gvar); + g_variant_unref(gvar); + if (srd_session_metadata_set(sess, SRD_CONF_SAMPLERATE, + g_variant_new_uint64(samplerate)) != SRD_OK) { + ERR("Setting samplerate failed"); + break; + } + if (srd_session_start(sess) != SRD_OK) { + ERR("Session start failed"); + break; + } + break; + case SR_DF_LOGIC: + logic = packet->payload; + num_samples = logic->length / logic->unitsize; + DBG("Received SR_DF_LOGIC: %d samples", num_samples); + srd_session_send(sess, samplecnt, samplecnt + num_samples, + logic->data, logic->length); + samplecnt += logic->length / logic->unitsize; + break; + case SR_DF_END: + DBG("Received SR_DF_END"); + break; + } + +} + +int get_stats(int stats[2]) +{ + FILE *f; + size_t len; + int tmp; + char *buf; + + stats[0] = stats[1] = -1; + if (!(f = fopen("/proc/self/status", "r"))) + return FALSE; + len = 128; + buf = malloc(len); + while (getline(&buf, &len, f) != -1) { + if (strcasestr(buf, "vmpeak:")) { + stats[0] = strtoul(buf + 10, NULL, 10); + } else if (strcasestr(buf, "vmsize:")) { + tmp = strtoul(buf + 10, NULL, 10); + if (tmp > stats[0]) + stats[0] = tmp; + } else if (strcasestr(buf, "vmhwm:")) { + stats[1] = strtoul(buf + 6, NULL, 10); + } else if (strcasestr(buf, "vmrss:")) { + tmp = strtoul(buf + 10, NULL, 10); + if (tmp > stats[0]) + stats[0] = tmp; + } + } + free(buf); + fclose(f); + + return TRUE; +} + +static int run_testcase(char *infile, GSList *pdlist, struct output *op) +{ + struct srd_session *sess; + struct srd_decoder *dec; + struct srd_decoder_inst *di, *prev_di; + struct pd *pd; + struct probe *probe; + struct option *option; + GVariant *gvar; + GHashTable *probes, *opts; + GSList *pdl, *l, *annl; + int idx; + char **dec_ann; + + if (op->outfile) { + if ((op->outfd = open(op->outfile, O_CREAT|O_WRONLY, 0600)) == -1) { + ERR("Unable to open %s for writing: %s", op->outfile, + strerror(errno)); + return FALSE; + } + } + + if (sr_session_load(infile) != SR_OK) + return FALSE; + + if (srd_session_new(&sess) != SRD_OK) + return FALSE; + sr_session_datafeed_callback_add(sr_cb, sess); + srd_pd_output_callback_add(sess, SRD_OUTPUT_ANN, srd_cb_ann, op); + + prev_di = NULL; + pd = NULL; + for (pdl = pdlist; pdl; pdl = pdl->next) { + pd = pdl->data; + if (srd_decoder_load(pd->name) != SRD_OK) + return FALSE; + + /* Instantiate decoder and pass in options. */ + opts = g_hash_table_new_full(g_str_hash, g_str_equal, NULL, + (GDestroyNotify)g_variant_unref); + for (l = pd->options; l; l = l->next) { + option = l->data; + g_hash_table_insert(opts, option->key, option->value); + } + if (!(di = srd_inst_new(sess, pd->name, opts))) + return FALSE; + g_hash_table_destroy(opts); + + /* Map probes. */ + if (pd->probes) { + probes = g_hash_table_new_full(g_str_hash, g_str_equal, NULL, + (GDestroyNotify)g_variant_unref); + for (l = pd->probes; l; l = l->next) { + probe = l->data; + gvar = g_variant_new_int32(probe->probe); + g_variant_ref_sink(gvar); + g_hash_table_insert(probes, probe->name, gvar); + } + if (srd_inst_probe_set_all(di, probes) != SRD_OK) + return FALSE; + g_hash_table_destroy(probes); + } + + + /* If this is not the first decoder in the list, stack it + * on top of the previous one. */ + if (prev_di) { + if (srd_inst_stack(sess, prev_di, di) != SRD_OK) { + ERR("Failed to stack decoder instances."); + return FALSE; + } + } + prev_di = di; + } + + /* Resolve top decoder's class index, so we can match. */ + dec = srd_decoder_get_by_id(pd->name); + if (op->class) { + if (op->type == SRD_OUTPUT_ANN) + annl = dec->annotations; + /* TODO can't dereference this for binary yet + else if (op->type == SRD_OUTPUT_BINARY) + annl = dec->binary; + */ + else + /* Only annotations and binary for now. */ + return FALSE; + idx = 0; + while(annl) { + dec_ann = annl->data; + /* TODO can't dereference this for binary yet */ + if (!strcmp(dec_ann[0], op->class)) { + op->class_idx = idx; + break; + } else + idx++; + annl = annl->next; + } + if (op->class_idx == -1) { + ERR("Output class '%s' not found in decoder %s.", + op->class, pd->name); + return FALSE; + } + } + + sr_session_start(); + sr_session_run(); + sr_session_stop(); + + srd_session_destroy(sess); + + if (op->outfile) + close(op->outfd); + + return TRUE; +} + +int main(int argc, char **argv) +{ + struct sr_context *ctx; + GSList *pdlist; + struct pd *pd; + struct probe *probe; + struct option *option; + struct output *op; + char c, *opt_infile, **kv, **opstr; + + op = malloc(sizeof(struct output)); + op->pd = NULL; + op->type = -1; + op->class = NULL; + op->class_idx = -1; + op->outfd = 1; + + pdlist = NULL; + opt_infile = NULL; + pd = NULL; + while((c = getopt(argc, argv, "dP:p:o:i:O:f:S")) != -1) { + switch(c) { + case 'd': + debug = TRUE; + break; + case 'P': + pd = g_malloc(sizeof(struct pd)); + pd->name = g_strdup(optarg); + pd->probes = pd->options = NULL; + pdlist = g_slist_append(pdlist, pd); + break; + case 'p': + case 'o': + if (g_slist_length(pdlist) == 0) { + /* No previous -P. */ + ERR("Syntax error at '%s'", optarg); + usage(NULL); + } + kv = g_strsplit(optarg, "=", 0); + if (!kv[0] || (!kv[1] || kv[2])) { + /* Need x=y. */ + ERR("Syntax error at '%s'", optarg); + g_strfreev(kv); + usage(NULL); + } + if (c == 'p') { + probe = malloc(sizeof(struct probe)); + probe->name = g_strdup(kv[0]); + probe->probe = strtoul(kv[1], 0, 10); + /* Apply to last PD. */ + pd->probes = g_slist_append(pd->probes, probe); + } else { + option = malloc(sizeof(struct option)); + option->key = g_strdup(kv[0]); + option->value = g_strdup(kv[1]); + /* Apply to last PD. */ + pd->options = g_slist_append(pd->options, option); + } + break; + case 'i': + opt_infile = optarg; + break; + case 'O': + opstr = g_strsplit(optarg, ":", 0); + if (!opstr[0] || !opstr[1]) { + /* Need at least abc:def. */ + ERR("Syntax error at '%s'", optarg); + g_strfreev(opstr); + usage(NULL); + } + op->pd = g_strdup(opstr[0]); + if (!strcmp(opstr[1], "annotation")) + op->type = SRD_OUTPUT_ANN; + else if (!strcmp(opstr[1], "binary")) + op->type = SRD_OUTPUT_BINARY; + else if (!strcmp(opstr[1], "python")) + op->type = SRD_OUTPUT_PYTHON; + else { + ERR("Unknown output type '%s'", opstr[1]); + g_strfreev(opstr); + usage(NULL); + } + if (opstr[2]) + op->class = g_strdup(opstr[2]); + g_strfreev(opstr); + break; + case 'f': + op->outfile = g_strdup(optarg); + op->outfd = -1; + break; + case 'S': + statistics = TRUE; + break; + default: + usage(NULL); + } + } + if (argc > optind) + usage(NULL); + if (g_slist_length(pdlist) == 0) + usage(NULL); + if (!opt_infile) + usage(NULL); + if (!op->pd || op->type == -1) + usage(NULL); + + if (sr_init(&ctx) != SR_OK) + return 1; + + srd_log_callback_set(srd_log, NULL); + if (srd_init(NULL) != SRD_OK) + return 1; + + run_testcase(opt_infile, pdlist, op); + + srd_exit(); + sr_exit(ctx); + + return 0; +} + +