From 12a46551a5f72417e2b1be0022cb7bdd109ecbd1 Mon Sep 17 00:00:00 2001 From: Niklas Hauser Date: Wed, 6 Apr 2016 22:07:22 +0100 Subject: [PATCH] Use functions instead of macros for porting. --- "frameworks\\utest/source/harness.cpp" | 25 ++++++++++------ "frameworks\\utest/source/shim.cpp" | 40 +++++++++++++++----------- "frameworks\\utest/utest/shim.h" | 10 +++++-- 3 files changed, 48 insertions(+), 27 deletions(-) diff --git "a/frameworks\\utest/source/harness.cpp" "b/frameworks\\utest/source/harness.cpp" index f00b0adc32..8403bffcd1 100644 --- "a/frameworks\\utest/source/harness.cpp" +++ "b/frameworks\\utest/source/harness.cpp" @@ -49,20 +49,25 @@ namespace location_t location = LOCATION_UNKNOWN; - utest_v1_scheduler_t scheduler = utest_v1_scheduler; + utest_v1_scheduler_t scheduler = {NULL, NULL, NULL, NULL}; } static void die() { while(1) ; } +static bool is_scheduler_valid(const utest_v1_scheduler_t scheduler) +{ + return (scheduler.init && scheduler.post && scheduler.cancel && scheduler.run); +} + bool Harness::set_scheduler(const utest_v1_scheduler_t scheduler) { - if (!scheduler.init || !scheduler.post || !scheduler.cancel || !scheduler.run) - return false; - - ::scheduler = scheduler; - return true; + if (is_scheduler_valid(scheduler)) { + ::scheduler = scheduler; + return true; + } + return false; } bool Harness::run(const Specification& specification, size_t) @@ -76,9 +81,13 @@ bool Harness::run(const Specification& specification) if (is_busy()) return false; - if (!scheduler.init || !scheduler.post || !scheduler.cancel || !scheduler.run) + // if the scheduler is invalid, this is the first time we are calling + if (!is_scheduler_valid(scheduler)) + scheduler = utest_v1_get_scheduler(); + // if the scheduler is still invalid, abort + if (!is_scheduler_valid(scheduler)) return false; - + // if the scheduler failed to initialize, abort if (scheduler.init() != 0) return false; diff --git "a/frameworks\\utest/source/shim.cpp" "b/frameworks\\utest/source/shim.cpp" index bfb603cee8..80d783aae7 100644 --- "a/frameworks\\utest/source/shim.cpp" +++ "b/frameworks\\utest/source/shim.cpp" @@ -18,7 +18,6 @@ #include "utest/shim.h" - #if UTEST_SHIM_SCHEDULER_USE_MINAR #include "minar/minar.h" @@ -40,20 +39,26 @@ static int32_t utest_minar_run() { return 0; } -extern "C" const utest_v1_scheduler_t utest_v1_scheduler = +extern "C" { +static const utest_v1_scheduler_t utest_v1_scheduler = { utest_minar_init, utest_minar_post, utest_minar_cancel, utest_minar_run }; +utest_v1_scheduler_t utest_v1_get_scheduler() +{ + return utest_v1_scheduler; +} +} #elif UTEST_SHIM_SCHEDULER_USE_US_TICKER // only one callback is active at any given time -volatile utest_v1_harness_callback_t minimal_callback; -volatile utest_v1_harness_callback_t ticker_callback; -const ticker_data_t *ticker_data; -ticker_event_t ticker_event; +static volatile utest_v1_harness_callback_t minimal_callback; +static volatile utest_v1_harness_callback_t ticker_callback; +static const ticker_data_t *ticker_data; +static ticker_event_t ticker_event; static void ticker_handler(uint32_t) { @@ -64,6 +69,7 @@ static void ticker_handler(uint32_t) static int32_t utest_us_ticker_init() { ticker_data = get_us_ticker_data(); + ticker_set_handler(ticker_data, ticker_handler); return 0; } static void *utest_us_ticker_post(const utest_v1_harness_callback_t callback, const uint32_t delay_ms) @@ -71,8 +77,6 @@ static void *utest_us_ticker_post(const utest_v1_harness_callback_t callback, co // printf("\t\t>>> Schedule %p with %ums delay => %p.\n", callback, (unsigned int)delay_ms, (void*)1); if (delay_ms) { ticker_callback = callback; - // setup ticker to call the handler manually - ticker_set_handler(ticker_data, ticker_handler); // fire the interrupt in 1000us * delay_ms ticker_insert_event(ticker_data, &ticker_event, ticker_read(ticker_data) + delay_ms * 1000, 0); } else { @@ -107,19 +111,23 @@ static int32_t utest_us_ticker_run() } return 0; } -const utest_v1_scheduler_t utest_v1_scheduler = +extern "C" { +static const utest_v1_scheduler_t utest_v1_scheduler = { utest_us_ticker_init, utest_us_ticker_post, utest_us_ticker_cancel, utest_us_ticker_run }; -#else -const utest_v1_scheduler_t utest_v1_scheduler = +utest_v1_scheduler_t utest_v1_get_scheduler() { - NULL, - NULL, - NULL, - NULL -}; + return utest_v1_scheduler; +} +} +#endif + +#ifdef YOTTA_CORE_UTIL_VERSION_STRING +// their functionality is implemented using the CriticalSectionLock class +void utest_v1_enter_critical_section(void) {} +void utest_v1_leave_critical_section(void) {} #endif diff --git "a/frameworks\\utest/utest/shim.h" "b/frameworks\\utest/utest/shim.h" index dd8f8437ca..c154bfa83a 100644 --- "a/frameworks\\utest/utest/shim.h" +++ "b/frameworks\\utest/utest/shim.h" @@ -42,10 +42,10 @@ # define UTEST_LEAVE_CRITICAL_SECTION #else # ifndef UTEST_ENTER_CRITICAL_SECTION -# error "You must provide a UTEST_ENTER_CRITICAL_SECTION implementation!" +# define UTEST_ENTER_CRITICAL_SECTION utest_v1_enter_critical_section() # endif # ifndef UTEST_LEAVE_CRITICAL_SECTION -# error "You must provide a UTEST_LEAVE_CRITICAL_SECTION implementation!" +# define UTEST_LEAVE_CRITICAL_SECTION utest_v1_leave_critical_section() # endif #endif @@ -67,8 +67,12 @@ extern "C" { #endif +/// must be implemented by the port +void utest_v1_enter_critical_section(void); +void utest_v1_leave_critical_section(void); + /// This is the default scheduler implementation used by the harness. -extern const utest_v1_scheduler_t utest_v1_scheduler; +utest_v1_scheduler_t utest_v1_get_scheduler(void); #ifdef __cplusplus }