Skip to content

Commit 12a4655

Browse files
niklarm0xc0170
authored andcommitted
Use functions instead of macros for porting.
1 parent 207660c commit 12a4655

File tree

3 files changed

+48
-27
lines changed

3 files changed

+48
-27
lines changed

frameworks\utest/source/harness.cpp

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -49,20 +49,25 @@ namespace
4949

5050
location_t location = LOCATION_UNKNOWN;
5151

52-
utest_v1_scheduler_t scheduler = utest_v1_scheduler;
52+
utest_v1_scheduler_t scheduler = {NULL, NULL, NULL, NULL};
5353
}
5454

5555
static void die() {
5656
while(1) ;
5757
}
5858

59-
bool Harness::set_scheduler(const utest_v1_scheduler_t scheduler)
59+
static bool is_scheduler_valid(const utest_v1_scheduler_t scheduler)
6060
{
61-
if (!scheduler.init || !scheduler.post || !scheduler.cancel || !scheduler.run)
62-
return false;
61+
return (scheduler.init && scheduler.post && scheduler.cancel && scheduler.run);
62+
}
6363

64-
::scheduler = scheduler;
65-
return true;
64+
bool Harness::set_scheduler(const utest_v1_scheduler_t scheduler)
65+
{
66+
if (is_scheduler_valid(scheduler)) {
67+
::scheduler = scheduler;
68+
return true;
69+
}
70+
return false;
6671
}
6772

6873
bool Harness::run(const Specification& specification, size_t)
@@ -76,9 +81,13 @@ bool Harness::run(const Specification& specification)
7681
if (is_busy())
7782
return false;
7883

79-
if (!scheduler.init || !scheduler.post || !scheduler.cancel || !scheduler.run)
84+
// if the scheduler is invalid, this is the first time we are calling
85+
if (!is_scheduler_valid(scheduler))
86+
scheduler = utest_v1_get_scheduler();
87+
// if the scheduler is still invalid, abort
88+
if (!is_scheduler_valid(scheduler))
8089
return false;
81-
90+
// if the scheduler failed to initialize, abort
8291
if (scheduler.init() != 0)
8392
return false;
8493

frameworks\utest/source/shim.cpp

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818

1919
#include "utest/shim.h"
2020

21-
2221
#if UTEST_SHIM_SCHEDULER_USE_MINAR
2322
#include "minar/minar.h"
2423

@@ -40,20 +39,26 @@ static int32_t utest_minar_run()
4039
{
4140
return 0;
4241
}
43-
extern "C" const utest_v1_scheduler_t utest_v1_scheduler =
42+
extern "C" {
43+
static const utest_v1_scheduler_t utest_v1_scheduler =
4444
{
4545
utest_minar_init,
4646
utest_minar_post,
4747
utest_minar_cancel,
4848
utest_minar_run
4949
};
50+
utest_v1_scheduler_t utest_v1_get_scheduler()
51+
{
52+
return utest_v1_scheduler;
53+
}
54+
}
5055

5156
#elif UTEST_SHIM_SCHEDULER_USE_US_TICKER
5257
// only one callback is active at any given time
53-
volatile utest_v1_harness_callback_t minimal_callback;
54-
volatile utest_v1_harness_callback_t ticker_callback;
55-
const ticker_data_t *ticker_data;
56-
ticker_event_t ticker_event;
58+
static volatile utest_v1_harness_callback_t minimal_callback;
59+
static volatile utest_v1_harness_callback_t ticker_callback;
60+
static const ticker_data_t *ticker_data;
61+
static ticker_event_t ticker_event;
5762

5863
static void ticker_handler(uint32_t)
5964
{
@@ -64,15 +69,14 @@ static void ticker_handler(uint32_t)
6469
static int32_t utest_us_ticker_init()
6570
{
6671
ticker_data = get_us_ticker_data();
72+
ticker_set_handler(ticker_data, ticker_handler);
6773
return 0;
6874
}
6975
static void *utest_us_ticker_post(const utest_v1_harness_callback_t callback, const uint32_t delay_ms)
7076
{
7177
// printf("\t\t>>> Schedule %p with %ums delay => %p.\n", callback, (unsigned int)delay_ms, (void*)1);
7278
if (delay_ms) {
7379
ticker_callback = callback;
74-
// setup ticker to call the handler manually
75-
ticker_set_handler(ticker_data, ticker_handler);
7680
// fire the interrupt in 1000us * delay_ms
7781
ticker_insert_event(ticker_data, &ticker_event, ticker_read(ticker_data) + delay_ms * 1000, 0);
7882
} else {
@@ -107,19 +111,23 @@ static int32_t utest_us_ticker_run()
107111
}
108112
return 0;
109113
}
110-
const utest_v1_scheduler_t utest_v1_scheduler =
114+
extern "C" {
115+
static const utest_v1_scheduler_t utest_v1_scheduler =
111116
{
112117
utest_us_ticker_init,
113118
utest_us_ticker_post,
114119
utest_us_ticker_cancel,
115120
utest_us_ticker_run
116121
};
117-
#else
118-
const utest_v1_scheduler_t utest_v1_scheduler =
122+
utest_v1_scheduler_t utest_v1_get_scheduler()
119123
{
120-
NULL,
121-
NULL,
122-
NULL,
123-
NULL
124-
};
124+
return utest_v1_scheduler;
125+
}
126+
}
127+
#endif
128+
129+
#ifdef YOTTA_CORE_UTIL_VERSION_STRING
130+
// their functionality is implemented using the CriticalSectionLock class
131+
void utest_v1_enter_critical_section(void) {}
132+
void utest_v1_leave_critical_section(void) {}
125133
#endif

frameworks\utest/utest/shim.h

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,10 @@
4242
# define UTEST_LEAVE_CRITICAL_SECTION
4343
#else
4444
# ifndef UTEST_ENTER_CRITICAL_SECTION
45-
# error "You must provide a UTEST_ENTER_CRITICAL_SECTION implementation!"
45+
# define UTEST_ENTER_CRITICAL_SECTION utest_v1_enter_critical_section()
4646
# endif
4747
# ifndef UTEST_LEAVE_CRITICAL_SECTION
48-
# error "You must provide a UTEST_LEAVE_CRITICAL_SECTION implementation!"
48+
# define UTEST_LEAVE_CRITICAL_SECTION utest_v1_leave_critical_section()
4949
# endif
5050
#endif
5151

@@ -67,8 +67,12 @@
6767
extern "C" {
6868
#endif
6969

70+
/// must be implemented by the port
71+
void utest_v1_enter_critical_section(void);
72+
void utest_v1_leave_critical_section(void);
73+
7074
/// This is the default scheduler implementation used by the harness.
71-
extern const utest_v1_scheduler_t utest_v1_scheduler;
75+
utest_v1_scheduler_t utest_v1_get_scheduler(void);
7276

7377
#ifdef __cplusplus
7478
}

0 commit comments

Comments
 (0)