From 926791b092b70314fba647acd16120a9cad1e73f Mon Sep 17 00:00:00 2001 From: david gauchard Date: Tue, 5 Jan 2021 10:31:57 +0100 Subject: [PATCH 1/4] emulation on host: millis()/micros() now start at 0 --- tests/host/common/Arduino.cpp | 5 +++-- tests/host/common/ArduinoMain.cpp | 4 ++++ tests/host/common/ArduinoMain.h | 9 +++++++++ 3 files changed, 16 insertions(+), 2 deletions(-) create mode 100644 tests/host/common/ArduinoMain.h diff --git a/tests/host/common/Arduino.cpp b/tests/host/common/Arduino.cpp index 8a0162b477..9088a16b24 100644 --- a/tests/host/common/Arduino.cpp +++ b/tests/host/common/Arduino.cpp @@ -15,6 +15,7 @@ #include #include "Arduino.h" +#include "ArduinoMain.h" #include @@ -22,14 +23,14 @@ extern "C" unsigned long millis() { timeval time; gettimeofday(&time, NULL); - return (time.tv_sec * 1000) + (time.tv_usec / 1000); + return ((time.tv_sec - gtod0.tv_sec) * 1000) + ((time.tv_usec - gtod0.tv_usec) / 1000); } extern "C" unsigned long micros() { timeval time; gettimeofday(&time, NULL); - return (time.tv_sec * 1000000) + time.tv_usec; + return ((time.tv_sec - gtod0.tv_sec) * 1000000) + time.tv_usec - gtod0.tv_usec; } diff --git a/tests/host/common/ArduinoMain.cpp b/tests/host/common/ArduinoMain.cpp index a9d8d696f6..790b246424 100644 --- a/tests/host/common/ArduinoMain.cpp +++ b/tests/host/common/ArduinoMain.cpp @@ -31,6 +31,7 @@ #include #include // wifi_get_ip_info() +#include #include #include @@ -51,6 +52,7 @@ bool restore_tty = false; bool mockdebug = false; int mock_port_shifter = MOCK_PORT_SHIFTER; const char* fspath = nullptr; +struct timeval gtod0; #define STDIN STDIN_FILENO @@ -295,6 +297,8 @@ int main (int argc, char* const argv []) // install exit handler in case Esp.restart() is called atexit(cleanup); + gettimeofday(>od0, nullptr); + setup(); while (!user_exit) { diff --git a/tests/host/common/ArduinoMain.h b/tests/host/common/ArduinoMain.h new file mode 100644 index 0000000000..797f44390f --- /dev/null +++ b/tests/host/common/ArduinoMain.h @@ -0,0 +1,9 @@ + +#ifndef __ESP8266ARDUINOMAIN_H +#define __ESP8266ARDUINOMAIN_H + +#include + +extern struct timeval gtod0; + +#endif // __ESP8266ARDUINOMAIN_H \ No newline at end of file From f2b9cc06acbabf9d691164668d6e198dcf7f7a11 Mon Sep 17 00:00:00 2001 From: david gauchard Date: Tue, 5 Jan 2021 11:38:34 +0100 Subject: [PATCH 2/4] fix for host tests --- tests/host/common/Arduino.cpp | 14 +++++++++++++- tests/host/common/ArduinoMain.cpp | 5 ++--- tests/host/common/ArduinoMain.h | 9 --------- tests/host/common/mock.h | 1 + 4 files changed, 16 insertions(+), 13 deletions(-) delete mode 100644 tests/host/common/ArduinoMain.h diff --git a/tests/host/common/Arduino.cpp b/tests/host/common/Arduino.cpp index 9088a16b24..859be13773 100644 --- a/tests/host/common/Arduino.cpp +++ b/tests/host/common/Arduino.cpp @@ -15,12 +15,21 @@ #include #include "Arduino.h" -#include "ArduinoMain.h" #include +static struct timeval gtod0 = { 0, 0 }; + +void mockInitMillisMicros () +{ + gettimeofday(>od0, nullptr); +} + extern "C" unsigned long millis() { + if (gtod0.tv_sec == 0) + mockInitMillisMicros(); + timeval time; gettimeofday(&time, NULL); return ((time.tv_sec - gtod0.tv_sec) * 1000) + ((time.tv_usec - gtod0.tv_usec) / 1000); @@ -28,6 +37,9 @@ extern "C" unsigned long millis() extern "C" unsigned long micros() { + if (gtod0.tv_sec == 0) + mockInitMillisMicros(); + timeval time; gettimeofday(&time, NULL); return ((time.tv_sec - gtod0.tv_sec) * 1000000) + time.tv_usec - gtod0.tv_usec; diff --git a/tests/host/common/ArduinoMain.cpp b/tests/host/common/ArduinoMain.cpp index 790b246424..08c4add9e1 100644 --- a/tests/host/common/ArduinoMain.cpp +++ b/tests/host/common/ArduinoMain.cpp @@ -31,7 +31,6 @@ #include #include // wifi_get_ip_info() -#include #include #include @@ -52,7 +51,6 @@ bool restore_tty = false; bool mockdebug = false; int mock_port_shifter = MOCK_PORT_SHIFTER; const char* fspath = nullptr; -struct timeval gtod0; #define STDIN STDIN_FILENO @@ -297,7 +295,8 @@ int main (int argc, char* const argv []) // install exit handler in case Esp.restart() is called atexit(cleanup); - gettimeofday(>od0, nullptr); + // now is millis() and micros() beginning + mockInitMillisMicros(); setup(); while (!user_exit) diff --git a/tests/host/common/ArduinoMain.h b/tests/host/common/ArduinoMain.h deleted file mode 100644 index 797f44390f..0000000000 --- a/tests/host/common/ArduinoMain.h +++ /dev/null @@ -1,9 +0,0 @@ - -#ifndef __ESP8266ARDUINOMAIN_H -#define __ESP8266ARDUINOMAIN_H - -#include - -extern struct timeval gtod0; - -#endif // __ESP8266ARDUINOMAIN_H \ No newline at end of file diff --git a/tests/host/common/mock.h b/tests/host/common/mock.h index da58c889aa..b572a153ed 100644 --- a/tests/host/common/mock.h +++ b/tests/host/common/mock.h @@ -104,6 +104,7 @@ int ets_printf (const char* fmt, ...) __attribute__ ((format (printf, 1, 2))); #define os_printf_plus printf #define ets_vsnprintf vsnprintf +void mockInitMillisMicros (); int mockverbose (const char* fmt, ...) __attribute__ ((format (printf, 1, 2))); extern const char* host_interface; // cmdline parameter From 0b008c33f1a1ebaa3662b344deb18cabaad7fb81 Mon Sep 17 00:00:00 2001 From: david gauchard Date: Tue, 5 Jan 2021 11:42:45 +0100 Subject: [PATCH 3/4] simplify --- tests/host/common/Arduino.cpp | 5 ----- tests/host/common/ArduinoMain.cpp | 4 ++-- tests/host/common/mock.h | 1 - 3 files changed, 2 insertions(+), 8 deletions(-) diff --git a/tests/host/common/Arduino.cpp b/tests/host/common/Arduino.cpp index 859be13773..1c0d38f58e 100644 --- a/tests/host/common/Arduino.cpp +++ b/tests/host/common/Arduino.cpp @@ -20,11 +20,6 @@ static struct timeval gtod0 = { 0, 0 }; -void mockInitMillisMicros () -{ - gettimeofday(>od0, nullptr); -} - extern "C" unsigned long millis() { if (gtod0.tv_sec == 0) diff --git a/tests/host/common/ArduinoMain.cpp b/tests/host/common/ArduinoMain.cpp index 08c4add9e1..051ca0dbc1 100644 --- a/tests/host/common/ArduinoMain.cpp +++ b/tests/host/common/ArduinoMain.cpp @@ -295,8 +295,8 @@ int main (int argc, char* const argv []) // install exit handler in case Esp.restart() is called atexit(cleanup); - // now is millis() and micros() beginning - mockInitMillisMicros(); + // first call to millis(): now is millis() and micros() beginning + millis(); setup(); while (!user_exit) diff --git a/tests/host/common/mock.h b/tests/host/common/mock.h index b572a153ed..da58c889aa 100644 --- a/tests/host/common/mock.h +++ b/tests/host/common/mock.h @@ -104,7 +104,6 @@ int ets_printf (const char* fmt, ...) __attribute__ ((format (printf, 1, 2))); #define os_printf_plus printf #define ets_vsnprintf vsnprintf -void mockInitMillisMicros (); int mockverbose (const char* fmt, ...) __attribute__ ((format (printf, 1, 2))); extern const char* host_interface; // cmdline parameter From 515a3b7b369a818c0b59919090e1aa2356b269c8 Mon Sep 17 00:00:00 2001 From: david gauchard Date: Tue, 5 Jan 2021 11:52:27 +0100 Subject: [PATCH 4/4] simplify --- tests/host/common/Arduino.cpp | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/tests/host/common/Arduino.cpp b/tests/host/common/Arduino.cpp index 1c0d38f58e..c02457f1a5 100644 --- a/tests/host/common/Arduino.cpp +++ b/tests/host/common/Arduino.cpp @@ -22,21 +22,19 @@ static struct timeval gtod0 = { 0, 0 }; extern "C" unsigned long millis() { - if (gtod0.tv_sec == 0) - mockInitMillisMicros(); - timeval time; gettimeofday(&time, NULL); + if (gtod0.tv_sec == 0) + memcpy(>od0, &time, sizeof gtod0); return ((time.tv_sec - gtod0.tv_sec) * 1000) + ((time.tv_usec - gtod0.tv_usec) / 1000); } extern "C" unsigned long micros() { - if (gtod0.tv_sec == 0) - mockInitMillisMicros(); - timeval time; gettimeofday(&time, NULL); + if (gtod0.tv_sec == 0) + memcpy(>od0, &time, sizeof gtod0); return ((time.tv_sec - gtod0.tv_sec) * 1000000) + time.tv_usec - gtod0.tv_usec; }