From 2c83c51db149da814c66a6f1329fa49b7138d5f0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fredrik=20=C3=96hrstr=C3=B6m?= Date: Sun, 1 Aug 2021 18:24:19 +0200 Subject: [PATCH] Add field timestamp_ut for printing unix timestamp millis. --- Makefile | 27 ++++++++++----- simulations/simulation_unix_timestamp.txt | 3 ++ src/meters.cc | 13 ++++++++ src/meters.h | 1 + src/meters_common_implementation.h | 1 + test.sh | 3 ++ tests/test_unix_timestamp.sh | 40 +++++++++++++++++++++++ 7 files changed, 80 insertions(+), 8 deletions(-) create mode 100644 simulations/simulation_unix_timestamp.txt create mode 100755 tests/test_unix_timestamp.sh diff --git a/Makefile b/Makefile index 7f0dacd..fc4b23f 100644 --- a/Makefile +++ b/Makefile @@ -330,18 +330,29 @@ update_manufacturers: mv m.h src/manufacturers.h rm *.flags manufacturers.txt -build_fuzz: - @if [ "${AFLHOME}" = "" ]; then echo 'You must supply aflhome "make build_fuzz AFLHOME=/home/afl"'; exit 1; fi - $(MAKE) AFL_HARDEN=1 CXX=$(AFLHOME)/afl-g++ $(BUILD)/fuzz - $(MAKE) AFL_HARDEN=1 CXX=$(AFLHOME)/afl-g++ $(BUILD)/wmbusmeters + +GCC_MAJOR_VERSION:=$(shell gcc --version | head --lines=1 | sed 's/.* \([0-9]\)\.[0-9]\.[0-9]$$/\1/') +AFL_HOME:=AFLplusplus + +$(AFL_HOME)/src/afl-cc.c: + mkdir -p AFLplusplus + if ! dpkg -s gcc-$(GCC_MAJOR_VERSION)-plugin-dev 2>/dev/null >/dev/null ; then echo "Please run: sudo apt install gcc-$(GCC_MAJOR_VERSION)-plugin-dev"; exit 1; fi + git clone https://github.com/AFLplusplus/AFLplusplus.git + +afl_prepared: AFLplusplus/src/afl-cc.c + (cd AFLplusplus; make) + touch afl_prepared + +build_fuzz: afl_prepared + echo GURKA + $(MAKE) AFL_HARDEN=1 CXX=$(AFL_HOME)/afl-g++-fast $(BUILD)/fuzz + $(MAKE) AFL_HARDEN=1 CXX=$(AFL_HOME)/afl-g++-fast $(BUILD)/wmbusmeters run_fuzz_difvifparser: - @if [ "${AFLHOME}" = "" ]; then echo 'You must supply aflhome "make run_fuzz AFLHOME=/home/afl"'; exit 1; fi - ${AFLHOME}/afl-fuzz -i fuzz_testcases/difvifparser -o fuzz_findings/ build/fuzz + ${AFL_HOME}/afl-fuzz -i fuzz_testcases/difvifparser -o fuzz_findings/ build/fuzz run_fuzz_telegrams: - @if [ "${AFLHOME}" = "" ]; then echo 'You must supply aflhome "make run_fuzz AFLHOME=/home/afl"'; exit 1; fi - ${AFLHOME}/afl-fuzz -i fuzz_testcases/telegrams -o fuzz_findings/ build/wmbusmeters --listento=any stdin + ${AFL_HOME}/afl-fuzz -i fuzz_testcases/telegrams -o fuzz_findings/ build/wmbusmeters --listento=any stdin # Include dependency information generated by gcc in a previous compile. include $(wildcard $(patsubst %.o,%.d,$(METER_OBJS))) diff --git a/simulations/simulation_unix_timestamp.txt b/simulations/simulation_unix_timestamp.txt new file mode 100644 index 0000000..5d339d3 --- /dev/null +++ b/simulations/simulation_unix_timestamp.txt @@ -0,0 +1,3 @@ +# Test Aventies Water Meter +telegram=76442104710007612507727100076121042507B5006005E2E95A3C2A1279A5415E6732679B43369FD5FDDDD783EEEBB48236D34E7C94AF0A18A5FDA5F7D64111EB42D4D891622139F2952F9D12A20088DFA4CF8123871123EE1F6C1DCEA414879DDB4E05E508F1826D7EFBA6964DF804C9261EA23BBF03 +|466.472;UT;Votten;61070071 diff --git a/src/meters.cc b/src/meters.cc index 4bfff01..ba54f2a 100644 --- a/src/meters.cc +++ b/src/meters.cc @@ -400,6 +400,14 @@ string MeterCommonImplementation::datetimeOfUpdateRobot() return string(datetime); } +string MeterCommonImplementation::unixTimestampOfUpdate() +{ + char ut[40]; + memset(ut, 0, sizeof(ut)); + sprintf(ut, "%zu", datetime_of_update_); + return string(ut); +} + bool needsPolling(MeterDriver d) { #define X(mname,linkmodes,info,driver,cname) if (d == MeterDriver::driver && 0 != ((linkmodes) & MBUS_bit)) return true; @@ -642,6 +650,11 @@ string concatFields(Meter *m, Telegram *t, char c, vector &prints, vector s += m->datetimeOfUpdateHumanReadable() + c; continue; } + if (field == "timestamp_ut") + { + s += m->unixTimestampOfUpdate() + c; + continue; + } if (field == "device") { s += t->about.device + c; diff --git a/src/meters.h b/src/meters.h index 9a9bc8a..575ea41 100644 --- a/src/meters.h +++ b/src/meters.h @@ -219,6 +219,7 @@ struct Meter virtual string datetimeOfUpdateHumanReadable() = 0; virtual string datetimeOfUpdateRobot() = 0; + virtual string unixTimestampOfUpdate() = 0; virtual void onUpdate(std::function cb) = 0; virtual int numUpdates() = 0; diff --git a/src/meters_common_implementation.h b/src/meters_common_implementation.h index f96277d..1cf01ab 100644 --- a/src/meters_common_implementation.h +++ b/src/meters_common_implementation.h @@ -41,6 +41,7 @@ struct MeterCommonImplementation : public virtual Meter string datetimeOfUpdateHumanReadable(); string datetimeOfUpdateRobot(); + string unixTimestampOfUpdate(); void onUpdate(function cb); int numUpdates(); diff --git a/test.sh b/test.sh index 1f4c6a4..d7daeac 100755 --- a/test.sh +++ b/test.sh @@ -102,6 +102,9 @@ if [ "$?" != "0" ]; then RC="1"; fi ./tests/test_match_dll_and_tpl_id.sh $PROG if [ "$?" != "0" ]; then RC="1"; fi +./tests/test_unix_timestamp.sh $PROG +if [ "$?" != "0" ]; then RC="1"; fi + ./tests/test_log_timestamps.sh $PROG if [ "$?" != "0" ]; then RC="1"; fi diff --git a/tests/test_unix_timestamp.sh b/tests/test_unix_timestamp.sh new file mode 100755 index 0000000..43382cc --- /dev/null +++ b/tests/test_unix_timestamp.sh @@ -0,0 +1,40 @@ +#!/bin/sh + +PROG="$1" + +mkdir -p testoutput +TEST=testoutput + +TESTNAME="Test unix timestamp in fields" +TESTRESULT="ERROR" + +METERS="Votten aventieswm 61070071 A004EB23329A477F1DD2D7820B56EB3D" + +cat simulations/simulation_unix_timestamp.txt | grep '^{' > $TEST/test_expected.txt + +NOW=$(date +%s) +cat simulations/simulation_unix_timestamp.txt | grep '^|' | sed 's/^|//' | sed "s/UT/${NOW}/" > $TEST/test_expected.txt +$PROG --format=fields --selectfields=total_m3,timestamp_ut,name,id simulations/simulation_t1.txt $METERS > $TEST/test_output.txt 2> $TEST/test_stderr.txt +if [ "$?" = "0" ] +then + cat $TEST/test_output.txt | sed 's/[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9] [0-9][0-9]:[0-9][0-9].[0-9][0-9]$/1111-11-11 11:11.11/' > $TEST/test_responses.txt + diff $TEST/test_expected.txt $TEST/test_responses.txt + if [ "$?" = "0" ] + then + echo OK fields: $TESTNAME + TESTRESULT="OK" + else + TESTRESULT="ERROR" + fi +else + echo "wmbusmeters returned error code: $?" + cat $TEST/test_output.txt + cat $TEST/test_stderr.txt +fi + + +if [ "$TESTRESULT" = "ERROR" ] +then + echo ERROR: $TESTNAME + exit 1 +fi