Contiki 2.5
rtimer-arch.c
1 /*
2  * Copyright (c) 2010, Swedish Institute of Computer Science.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the Institute nor the names of its contributors
14  * may be used to endorse or promote products derived from this software
15  * without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * This file is part of the Contiki operating system.
30  *
31  * $Id: rtimer-arch.c,v 1.4 2010/03/23 13:13:17 fros4943 Exp $
32  */
33 
34 #include <signal.h>
35 #include <sys/time.h>
36 #include <stddef.h>
37 
38 #include "sys/rtimer.h"
39 #include "sys/clock.h"
40 #include "sys/cooja_mt.h"
41 
42 #include "lib/simEnvChange.h"
43 
44 #define DEBUG 0
45 #if DEBUG
46 #include <stdio.h>
47 #define PRINTF(...) printf(__VA_ARGS__)
48 #else
49 #define PRINTF(...)
50 #endif
51 
52 extern clock_time_t simCurrentTime;
53 
54 static int pending_rtimer = 0;
55 static rtimer_clock_t next_rtimer = 0;
56 static clock_time_t last_rtimer_now = 0;
57 
58 /*---------------------------------------------------------------------------*/
59 void
60 rtimer_arch_init(void)
61 {
62  next_rtimer = 0;
63  last_rtimer_now = 0;
64  pending_rtimer = 0;
65 }
66 /*---------------------------------------------------------------------------*/
67 void
68 rtimer_arch_schedule(rtimer_clock_t t)
69 {
70  next_rtimer = t;
71  pending_rtimer = 1;
72 }
73 /*---------------------------------------------------------------------------*/
74 rtimer_clock_t
75 rtimer_arch_next(void)
76 {
77  return next_rtimer;
78 }
79 /*---------------------------------------------------------------------------*/
80 int
81 rtimer_arch_pending(void)
82 {
83  return pending_rtimer;
84 }
85 /*---------------------------------------------------------------------------*/
86 int
87 rtimer_arch_check(void)
88 {
89  if (simCurrentTime == next_rtimer) {
90  /* Execute rtimer */
91  pending_rtimer = 0;
93  return 1;
94  }
95  return 0;
96 }
97 /*---------------------------------------------------------------------------*/
98 rtimer_clock_t
99 rtimer_arch_now(void)
100 {
101  if(last_rtimer_now == simCurrentTime) {
102  /* Yield to COOJA, to allow time to change */
103  simProcessRunValue = 1;
104  simNextExpirationTime = simCurrentTime + 1;
105  cooja_mt_yield();
106  }
107  last_rtimer_now = simCurrentTime;
108  return simCurrentTime;
109 }
110 /*---------------------------------------------------------------------------*/
111