From 0ddd6ec15ebb4ba0f95ada88ff72c121292b4b26 Mon Sep 17 00:00:00 2001 From: Oleg Hahm Date: Sat, 14 Nov 2015 20:33:01 +0100 Subject: [PATCH 1/2] xtimer: implement xtimer_msg_receive_timeout --- sys/xtimer/xtimer.c | 40 +++++++++++++++++++++++++++++++--------- 1 file changed, 31 insertions(+), 9 deletions(-) diff --git a/sys/xtimer/xtimer.c b/sys/xtimer/xtimer.c index c9993aae2..7ec551999 100644 --- a/sys/xtimer/xtimer.c +++ b/sys/xtimer/xtimer.c @@ -159,22 +159,44 @@ void xtimer_now_timex(timex_t *out) out->microseconds = now - (out->seconds * SEC_IN_USEC); } -int xtimer_msg_receive_timeout64(msg_t *m, uint64_t timeout) { - msg_t tmsg; - tmsg.type = MSG_XTIMER; - tmsg.content.ptr = (char *) &tmsg; +/* Prepares the message to trigger the timeout. + * Additionally, the xtimer_t struct gets initialized. + */ +static void _setup_timer_msg(msg_t *m, xtimer_t *t) +{ + m->type = MSG_XTIMER; + m->content.ptr = (char *) m; - xtimer_t t; - t.target = t.long_target = 0; - xtimer_set_msg64(&t, timeout, &tmsg, sched_active_pid); + t->target = t->long_target = 0; +} +/* Waits for incoming message or timeout. */ +static int _msg_wait(msg_t *m, msg_t *tmsg, xtimer_t *t) +{ msg_receive(m); - if (m->type == MSG_XTIMER && m->content.ptr == (char *) &tmsg) { + if (m->type == MSG_XTIMER && m->content.ptr == (char *) tmsg) { /* we hit the timeout */ return -1; } else { - xtimer_remove(&t); + xtimer_remove(t); return 1; } } + +int xtimer_msg_receive_timeout64(msg_t *m, uint64_t timeout) { + msg_t tmsg; + xtimer_t t; + _setup_timer_msg(&tmsg, &t); + xtimer_set_msg64(&t, timeout, &tmsg, sched_active_pid); + return _msg_wait(m, &tmsg, &t); +} + +int xtimer_msg_receive_timeout(msg_t *msg, uint32_t us) +{ + msg_t tmsg; + xtimer_t t; + _setup_timer_msg(&tmsg, &t); + xtimer_set_msg(&t, us, &tmsg, sched_active_pid); + return _msg_wait(msg, &tmsg, &t); +} From 24ce427a3af15b10f19d65f5908896f2ab761668 Mon Sep 17 00:00:00 2001 From: Oleg Hahm Date: Sat, 14 Nov 2015 20:33:39 +0100 Subject: [PATCH 2/2] tests: added test for xtimer_msg_receive_timeout --- tests/xtimer_msg_receive_timeout/Makefile | 7 +++ tests/xtimer_msg_receive_timeout/main.c | 44 +++++++++++++++++++ .../tests/01-run.py | 39 ++++++++++++++++ 3 files changed, 90 insertions(+) create mode 100644 tests/xtimer_msg_receive_timeout/Makefile create mode 100644 tests/xtimer_msg_receive_timeout/main.c create mode 100755 tests/xtimer_msg_receive_timeout/tests/01-run.py diff --git a/tests/xtimer_msg_receive_timeout/Makefile b/tests/xtimer_msg_receive_timeout/Makefile new file mode 100644 index 000000000..8b5b8e937 --- /dev/null +++ b/tests/xtimer_msg_receive_timeout/Makefile @@ -0,0 +1,7 @@ +APPLICATION = xtimer_msg_receive_timeout +include ../Makefile.tests_common + +FEATURES_REQUIRED += periph_timer +USEMODULE += xtimer + +include $(RIOTBASE)/Makefile.include diff --git a/tests/xtimer_msg_receive_timeout/main.c b/tests/xtimer_msg_receive_timeout/main.c new file mode 100644 index 000000000..47ebfd1d9 --- /dev/null +++ b/tests/xtimer_msg_receive_timeout/main.c @@ -0,0 +1,44 @@ +/* + * Copyright (C) 2015 INRIA + * + * This file is subject to the terms and conditions of the GNU Lesser + * General Public License v2.1. See the file LICENSE in the top level + * directory for more details. + */ + +/** + * @ingroup tests + * @{ + * + * @file + * @brief test application for xtimer_msg_receive_timeout() + * + * @author Oliver Hahm + * + * @} + */ + +#include "thread.h" +#include "msg.h" +#include "xtimer.h" +#include "timex.h" + +int main(void) +{ + msg_t m, tmsg; + xtimer_t t; + int64_t offset = -1000; + tmsg.type = 44; + + for (int i = 0; i < 10; i++) { + xtimer_set_msg(&t, SEC_IN_USEC + offset, &tmsg, sched_active_pid); + if (xtimer_msg_receive_timeout(&m, SEC_IN_USEC) < 0) { + puts("Timeout!"); + } + else { + printf("Message received: %" PRIu16 "\n", m.type); + } + offset = (offset < 0) ? 1000 : -1000; + } + return 0; +} diff --git a/tests/xtimer_msg_receive_timeout/tests/01-run.py b/tests/xtimer_msg_receive_timeout/tests/01-run.py new file mode 100755 index 000000000..aad13d956 --- /dev/null +++ b/tests/xtimer_msg_receive_timeout/tests/01-run.py @@ -0,0 +1,39 @@ +#!/usr/bin/env python + +# Copyright (C) 2015 INRIA +# +# This file is subject to the terms and conditions of the GNU Lesser +# General Public License v2.1. See the file LICENSE in the top level +# directory for more details. + +import os, signal, sys +from pexpect import TIMEOUT, EOF +if sys.version_info[0] == 2: + from pexpect import spawn +else: + from pexpect import spawnu as spawn + +DEFAULT_TIMEOUT = 2 + +def main(): + p = None + + try: + p = spawn("make term", timeout=DEFAULT_TIMEOUT) + p.logfile = sys.stdout + + for i in range(10): + p.expect("Message received: 44") + p.expect("Timeout") + except TIMEOUT as exc: + print(exc) + return 1 + finally: + if p and not p.terminate(): + print("SUCCESS") + os.killpg(p.pid, signal.SIGKILL) + + return 0 + +if __name__ == "__main__": + sys.exit(main())