Contiki-NG
mtype.c
Go to the documentation of this file.
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 */
30
31/**
32 * \file
33 * COOJA Contiki mote type file.
34 * \author
35 * Fredrik Osterlind <fros@sics.se>
36 */
37
38#include <stdint.h>
39#include <stdio.h>
40#include <string.h>
41
42#include "contiki.h"
43#include "sys/cc.h"
44#include "sys/cooja_mt.h"
45
46/* The main function, implemented in contiki-main.c */
47int main(void);
48
49/*
50 * referenceVar is used for comparing absolute and process relative memory.
51 * (this must not be static due to memory locations)
52 */
53intptr_t referenceVar;
54
55/*
56 * Contiki and rtimer threads.
57 */
58static struct cooja_mt_thread rtimer_thread;
59static struct cooja_mt_thread process_run_thread;
60/*---------------------------------------------------------------------------*/
61static void
62rtimer_thread_loop(void *data)
63{
64 while(1) {
65 rtimer_arch_check();
66
67 /* Return to COOJA */
68 cooja_mt_yield();
69 }
70}
71/*---------------------------------------------------------------------------*/
72static void
73process_run_thread_loop(void *data)
74{
75 /* Yield once during bootup */
76 simProcessRunValue = 1;
77 cooja_mt_yield();
78
79 /* Then call common Contiki-NG main function */
80 main();
81}
82/*---------------------------------------------------------------------------*/
83void
84cooja_init(void)
85{
86 /* Create rtimers and Contiki threads */
87 cooja_mt_start(&rtimer_thread, &rtimer_thread_loop, NULL);
88 cooja_mt_start(&process_run_thread, &process_run_thread_loop, NULL);
89}
90/*---------------------------------------------------------------------------*/
91void
92cooja_tick(void)
93{
94 simProcessRunValue = 0;
95
96 /* Let all simulation interfaces act first */
97 doActionsBeforeTick();
98
99 /* Poll etimer process */
100 if(etimer_pending()) {
102 }
103
104 /* Let rtimers run.
105 * Sets simProcessRunValue */
106 cooja_mt_exec(&rtimer_thread);
107
108 if(simProcessRunValue == 0) {
109 /* Rtimers done: Let Contiki handle a few events.
110 * Sets simProcessRunValue */
111 cooja_mt_exec(&process_run_thread);
112 }
113
114 /* Let all simulation interfaces act before returning to java */
115 doActionsAfterTick();
116
117 /* Do we have any pending timers */
118 simEtimerPending = etimer_pending();
119
120 /* Save nearest expiration time */
121 simEtimerNextExpirationTime = etimer_next_expiration_time();
122}
Default definitions of C compiler quirk work-arounds.
int etimer_pending(void)
Check if there are any non-expired event timers.
Definition: etimer.c:231
void etimer_request_poll(void)
Make the event timer aware that the clock has changed.
Definition: etimer.c:145
clock_time_t etimer_next_expiration_time(void)
Get next event timer expiration time.
Definition: etimer.c:237