Skip to content

Commit 323917c

Browse files
authored
Merge pull request contiki-ng#2179 from nfi/cooja-mtype
Enable the Cooja platform to build without using Cooja
2 parents 978b7f3 + 98af7a4 commit 323917c

File tree

3 files changed

+257
-214
lines changed

3 files changed

+257
-214
lines changed

arch/platform/cooja/Makefile.cooja

+3-3
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ COOJA_INTFS = beep.c ip.c leds-arch.c moteid.c \
8686
clock.c cooja-log.c cfs-cooja.c cooja-radio.c \
8787
eeprom.c slip-arch.c
8888

89-
COOJA_CORE = random.c sensors.c leds.c gpio-hal-arch.c buttons.c
89+
COOJA_CORE = platform.c random.c sensors.c leds.c gpio-hal-arch.c buttons.c
9090

9191
# (COOJA_SOURCEFILES contains additional sources set from simulator)
9292
CONTIKI_TARGET_SOURCEFILES = \
@@ -107,7 +107,7 @@ CFLAGS += $(CFLAGSNO)
107107
# of the rule resides in Makefile.include.
108108
$(BUILD_DIR_BOARD)/%.$(TARGET): MAPFILE = $(LIBNAME:.cooja=.map)
109109

110-
# This is mtype<NNN>.o which is built from platform.c with
110+
# This is mtype<NNN>.o which is built from mtype.c with
111111
# CLASSNAME passed from the environment by Cooja.
112112
MTYPE_OBJ = $(LIBNAME:.cooja=.o)
113113
MTYPE_DEP = $(DEPDIR)/$(notdir $(LIBNAME:.cooja=.d))
@@ -119,6 +119,6 @@ endif # LIBNAME
119119

120120
PROJECT_OBJECTFILES += $(MTYPE_OBJ)
121121

122-
$(MTYPE_OBJ): platform.c $(MTYPE_DEP) | $(DEPDIR)
122+
$(MTYPE_OBJ): mtype.c $(MTYPE_DEP) | $(DEPDIR)
123123
$(TRACE_CC)
124124
$(Q)$(CCACHE) $(CC) $(CFLAGS) -MT $@ -MMD -MP -MF $(MTYPE_DEP) -c $< -o $@

arch/platform/cooja/mtype.c

+254
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,254 @@
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 <[email protected]>
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+
#include <jni.h>
46+
47+
/* JNI-defined functions, depends on the environment variable CLASSNAME */
48+
#ifndef CLASSNAME
49+
#error CLASSNAME is undefined, required by mtype.c
50+
#endif /* CLASSNAME */
51+
/* Construct the name of JNI method m in class c. */
52+
#define COOJA_METHOD(c, m) COOJA_QUOTEME(c, m)
53+
/* Indirection to get the right preprocessor behavior. */
54+
#define COOJA_QUOTEME(c, m) Java_org_contikios_cooja_corecomm_##c##_##m
55+
/* Names of JNI methods. */
56+
#define CLASS_init COOJA_METHOD(CLASSNAME, init)
57+
#define CLASS_getMemory COOJA_METHOD(CLASSNAME, getMemory)
58+
#define CLASS_setMemory COOJA_METHOD(CLASSNAME, setMemory)
59+
#define CLASS_tick COOJA_METHOD(CLASSNAME, tick)
60+
#define CLASS_setReferenceAddress COOJA_METHOD(CLASSNAME, setReferenceAddress)
61+
62+
/* The main function, implemented in contiki-main.c */
63+
int main(void);
64+
65+
/*
66+
* referenceVar is used for comparing absolute and process relative memory.
67+
* (this must not be static due to memory locations)
68+
*/
69+
intptr_t referenceVar;
70+
71+
/*
72+
* Contiki and rtimer threads.
73+
*/
74+
static struct cooja_mt_thread rtimer_thread;
75+
static struct cooja_mt_thread process_run_thread;
76+
/*---------------------------------------------------------------------------*/
77+
static void
78+
rtimer_thread_loop(void *data)
79+
{
80+
while(1) {
81+
rtimer_arch_check();
82+
83+
/* Return to COOJA */
84+
cooja_mt_yield();
85+
}
86+
}
87+
/*---------------------------------------------------------------------------*/
88+
static void
89+
process_run_thread_loop(void *data)
90+
{
91+
/* Yield once during bootup */
92+
simProcessRunValue = 1;
93+
cooja_mt_yield();
94+
95+
/* Then call common Contiki-NG main function */
96+
main();
97+
}
98+
/*---------------------------------------------------------------------------*/
99+
/**
100+
* \brief Callback on load of library.
101+
* \param vm unused
102+
* \param reserved unused
103+
*
104+
* This function is required to return at least the JNI version for
105+
* the functions we use.
106+
*
107+
* Java 11 is the oldest supported Java version so the function returns
108+
* JNI_VERSION_10 for now.
109+
*/
110+
JNIEXPORT jint JNICALL
111+
JNI_OnLoad(JavaVM *vm, void *reserved)
112+
{
113+
return JNI_VERSION_10;
114+
}
115+
/*---------------------------------------------------------------------------*/
116+
/**
117+
* \brief Initialize a mote by starting processes etc.
118+
* \param env JNI Environment interface pointer
119+
* \param obj unused
120+
*
121+
* This function initializes a mote by starting certain
122+
* processes and setting up the environment.
123+
*
124+
* This is a JNI function and should only be called via the
125+
* responsible Java part (MoteType.java).
126+
*/
127+
JNIEXPORT void JNICALL
128+
CLASS_init(JNIEnv *env, jobject obj)
129+
{
130+
/* Create rtimers and Contiki threads */
131+
cooja_mt_start(&rtimer_thread, &rtimer_thread_loop, NULL);
132+
cooja_mt_start(&process_run_thread, &process_run_thread_loop, NULL);
133+
}
134+
/*---------------------------------------------------------------------------*/
135+
/**
136+
* \brief Get a segment from the process memory.
137+
* \param env JNI Environment interface pointer
138+
* \param obj unused
139+
* \param rel_addr Start address of segment
140+
* \param length Size of memory segment
141+
* \param mem_arr Byte array destination for the fetched memory segment
142+
* \return Java byte array containing a copy of memory segment.
143+
*
144+
* Fetches a memory segment from the process memory starting at
145+
* (rel_addr), with size (length). This function does not perform
146+
* ANY error checking, and the process may crash if addresses are
147+
* not available/readable.
148+
*
149+
* This is a JNI function and should only be called via the
150+
* responsible Java part (MoteType.java).
151+
*/
152+
JNIEXPORT void JNICALL
153+
CLASS_getMemory(JNIEnv *env, jobject obj, jlong rel_addr, jint length,
154+
jbyteArray mem_arr)
155+
{
156+
(*env)->SetByteArrayRegion(
157+
env,
158+
mem_arr,
159+
0,
160+
(size_t) length,
161+
(jbyte *) (((intptr_t)rel_addr) + referenceVar)
162+
);
163+
}
164+
/*---------------------------------------------------------------------------*/
165+
/**
166+
* \brief Replace a segment of the process memory with given byte array.
167+
* \param env JNI Environment interface pointer
168+
* \param obj unused
169+
* \param rel_addr Start address of segment
170+
* \param length Size of memory segment
171+
* \param mem_arr Byte array contaning new memory
172+
*
173+
* Replaces a process memory segment with given byte array.
174+
* This function does not perform ANY error checking, and the
175+
* process may crash if addresses are not available/writable.
176+
*
177+
* This is a JNI function and should only be called via the
178+
* responsible Java part (MoteType.java).
179+
*/
180+
JNIEXPORT void JNICALL
181+
CLASS_setMemory(JNIEnv *env, jobject obj, jlong rel_addr, jint length,
182+
jbyteArray mem_arr)
183+
{
184+
(*env)->GetByteArrayRegion(env, mem_arr, 0, length,
185+
(jbyte *)((intptr_t)rel_addr + referenceVar));
186+
}
187+
/*---------------------------------------------------------------------------*/
188+
/**
189+
* \brief Let mote execute one "block" of code (tick mote).
190+
* \param env JNI Environment interface pointer
191+
* \param obj unused
192+
*
193+
* Let mote defined by the active contiki processes and current
194+
* process memory execute some program code. This code must not block
195+
* or else this function will never return. A typical contiki
196+
* process will return when it executes PROCESS_WAIT..() statements.
197+
*
198+
* Before the control is left to contiki processes, any messages
199+
* from the Java part are handled. These may for example be
200+
* incoming network data. After the contiki processes return control,
201+
* messages to the Java part are also handled (those which may need
202+
* special attention).
203+
*
204+
* This is a JNI function and should only be called via the
205+
* responsible Java part (MoteType.java).
206+
*/
207+
JNIEXPORT void JNICALL
208+
CLASS_tick(JNIEnv *env, jobject obj)
209+
{
210+
simProcessRunValue = 0;
211+
212+
/* Let all simulation interfaces act first */
213+
doActionsBeforeTick();
214+
215+
/* Poll etimer process */
216+
if(etimer_pending()) {
217+
etimer_request_poll();
218+
}
219+
220+
/* Let rtimers run.
221+
* Sets simProcessRunValue */
222+
cooja_mt_exec(&rtimer_thread);
223+
224+
if(simProcessRunValue == 0) {
225+
/* Rtimers done: Let Contiki handle a few events.
226+
* Sets simProcessRunValue */
227+
cooja_mt_exec(&process_run_thread);
228+
}
229+
230+
/* Let all simulation interfaces act before returning to java */
231+
doActionsAfterTick();
232+
233+
/* Do we have any pending timers */
234+
simEtimerPending = etimer_pending();
235+
236+
/* Save nearest expiration time */
237+
simEtimerNextExpirationTime = etimer_next_expiration_time();
238+
}
239+
/*---------------------------------------------------------------------------*/
240+
/**
241+
* \brief Set the relative memory address of the reference variable.
242+
* \param env JNI Environment interface pointer
243+
* \param obj unused
244+
* \param addr Relative memory address
245+
*
246+
* This is a JNI function and should only be called via the
247+
* responsible Java part (MoteType.java).
248+
*/
249+
JNIEXPORT void JNICALL
250+
CLASS_setReferenceAddress(JNIEnv *env, jobject obj, jlong addr)
251+
{
252+
referenceVar = (((intptr_t)&referenceVar) - ((intptr_t)addr));
253+
}
254+
/*---------------------------------------------------------------------------*/

0 commit comments

Comments
 (0)