Contiki-NG
Loading...
Searching...
No Matches
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 "lib/list.h"
44#include "sys/cc.h"
45#include "sys/cooja_mt.h"
46
47/* The main function, implemented in contiki-main.c */
48int main(void);
49
50/*
51 * referenceVar is used for comparing absolute and process relative memory.
52 * (this must not be static due to memory locations)
53 */
54intptr_t referenceVar;
55
56/*
57 * Interface handlers.
58 */
59LIST(pre_tick_actions);
60LIST(post_tick_actions);
61/*---------------------------------------------------------------------------*/
62void
63cooja_add_pre_tick_action(struct cooja_tick_action *handler)
64{
65 /* Constructor order is per module on macOS, so init list here instead. */
66 static bool initialized = false;
67 if(!initialized) {
68 list_init(pre_tick_actions);
69 initialized = true;
70 }
71 list_add(pre_tick_actions, handler);
72}
73/*---------------------------------------------------------------------------*/
74void
75cooja_add_post_tick_action(struct cooja_tick_action *handler)
76{
77 /* Constructor order is per module on macOS, so init list here instead. */
78 static bool initialized = false;
79 if(!initialized) {
80 list_init(post_tick_actions);
81 initialized = true;
82 }
83 list_add(post_tick_actions, handler);
84}
85/*---------------------------------------------------------------------------*/
86/*
87 * Contiki and rtimer threads.
88 */
89static struct cooja_mt_thread cooja_thread;
90static struct cooja_mt_thread rtimer_thread;
91static struct cooja_mt_thread process_run_thread;
92/*---------------------------------------------------------------------------*/
93#ifdef __APPLE__
94extern int macos_data_start __asm("section$start$__DATA$__data");
95extern int macos_data_end __asm("section$end$__DATA$__data");
96extern int macos_bss_start __asm("section$start$__DATA$__bss");
97extern int macos_bss_end __asm("section$end$__DATA$__bss");
98extern int macos_common_start __asm("section$start$__DATA$__common");
99extern int macos_common_end __asm("section$end$__DATA$__common");
100
101uintptr_t
102cooja_data_start(void)
103{
104 return (uintptr_t)&macos_data_start;
105}
106
107int
108cooja_data_size(void)
109{
110 return (int)((uintptr_t)&macos_data_end - (uintptr_t)&macos_data_start);
111}
112
113uintptr_t
114cooja_bss_start(void)
115{
116 return (uintptr_t)&macos_bss_start;
117}
118
119int
120cooja_bss_size(void)
121{
122 return (int)((uintptr_t)&macos_bss_end - (uintptr_t)&macos_bss_start);
123}
124
125uintptr_t
126cooja_common_start(void)
127{
128 return (uintptr_t)&macos_common_start;
129}
130
131int
132cooja_common_size(void)
133{
134 return (int)((uintptr_t)&macos_common_end - (uintptr_t)&macos_common_start);
135}
136#endif /* __APPLE__ */
137/*---------------------------------------------------------------------------*/
138static void
139rtimer_thread_loop(void)
140{
141 while(1) {
142 rtimer_arch_check();
143
144 /* Return to COOJA */
145 cooja_mt_yield();
146 }
147}
148/*---------------------------------------------------------------------------*/
149static void
150process_run_thread_loop(void)
151{
152 /* Yield once during bootup */
153 simProcessRunValue = 1;
154 cooja_mt_yield();
155 /* Then call common Contiki-NG main function */
156 main();
157}
158/*---------------------------------------------------------------------------*/
159int
160cooja_init(void)
161{
162 int rv;
163 /* Create rtimers and Contiki threads */
164 if((rv = cooja_mt_init(&cooja_thread))) {
165 return rv;
166 }
167 cooja_mt_start(&cooja_thread, &rtimer_thread, rtimer_thread_loop);
168 cooja_mt_start(&cooja_thread, &process_run_thread, process_run_thread_loop);
169 return 0;
170}
171/*---------------------------------------------------------------------------*/
172void
173cooja_tick(void)
174{
175 simProcessRunValue = 0;
176
177 /* Let all simulation interfaces act first */
178 for(struct cooja_tick_action *r = list_head(pre_tick_actions);
179 r != NULL; r = r->next) {
180 r->action();
181 }
182
183 /* Poll etimer process */
184 if(etimer_pending()) {
186 }
187
188 /* Let rtimers run.
189 * Sets simProcessRunValue */
190 cooja_mt_exec(&cooja_thread, &rtimer_thread);
191
192 if(simProcessRunValue == 0) {
193 /* Rtimers done: Let Contiki handle a few events.
194 * Sets simProcessRunValue */
195 cooja_mt_exec(&cooja_thread, &process_run_thread);
196 }
197
198 /* Let all simulation interfaces act before returning to java */
199 for(struct cooja_tick_action *r = list_head(post_tick_actions);
200 r != NULL; r = r->next) {
201 r->action();
202 }
203
204 /* Do we have any pending timers */
205 simEtimerPending = etimer_pending();
206
207 /* Save nearest expiration time */
208 simEtimerNextExpirationTime = etimer_next_expiration_time();
209}
Default definitions of C compiler quirk work-arounds.
int etimer_pending(void)
Check if there are any non-expired event timers.
Definition etimer.c:225
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:231
static void list_init(list_t list)
Initialize a list.
Definition list.h:152
#define LIST(name)
Declare a linked list.
Definition list.h:90
void list_add(list_t list, void *item)
Add an item at the end of a list.
Definition list.c:71
static void * list_head(const_list_t list)
Get a pointer to the first element of a list.
Definition list.h:169
Linked list manipulation routines.