2006-09-06 12:37:08 +02:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2006, Swedish Institute of Computer Science.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. Neither the name of the Institute nor the names of its contributors
|
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*
|
2009-03-17 11:34:02 +01:00
|
|
|
* $Id: contiki_template.c,v 1.12 2009/03/17 10:34:02 fros4943 Exp $
|
2006-09-06 12:37:08 +02:00
|
|
|
*/
|
|
|
|
|
2006-08-21 14:11:16 +02:00
|
|
|
/**
|
|
|
|
* \defgroup coojacore COOJA Simulator Core
|
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* \file
|
2007-11-25 23:50:12 +01:00
|
|
|
* C code template for generating Contiki main startup files from
|
|
|
|
* inside the COOJA Simulator. This file should not be compiled directly.
|
2006-08-21 14:11:16 +02:00
|
|
|
* \author
|
|
|
|
* Fredrik Osterlind <fros@sics.se>
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
/**
|
|
|
|
* \page coojacore COOJA Simulator Core
|
|
|
|
*
|
|
|
|
* COOJA Simulator Core ("the Core") represents the entire C code part of COOJA
|
|
|
|
* Simulator.
|
|
|
|
* This part shortly consists of simulated sensors and actuators and code for
|
|
|
|
* communicating with the Java part.
|
|
|
|
*
|
|
|
|
* Communication with the Core should always be handled via dedicated classes
|
|
|
|
* (MoteType), and never directly. MoteType works as an interface giving access
|
|
|
|
* to variable's values given their name (through CoreComm->Lib?).
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <jni.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include "contiki.h"
|
|
|
|
#include "contiki-net.h"
|
|
|
|
#include "contiki-lib.h"
|
|
|
|
#include "contiki-conf.h"
|
|
|
|
#include "sys/clock.h"
|
|
|
|
|
|
|
|
#include "lib/simEnvChange.h"
|
|
|
|
#include "lib/sensors.h"
|
2007-11-25 23:50:12 +01:00
|
|
|
#include "dev/cooja-radio.h"
|
2006-08-21 14:11:16 +02:00
|
|
|
#include "sys/etimer.h"
|
2006-09-29 16:32:15 +02:00
|
|
|
#include "sys/cooja_mt.h"
|
2007-05-18 15:51:42 +02:00
|
|
|
#include "net/init-net.h"
|
2006-08-21 14:11:16 +02:00
|
|
|
|
2009-01-21 13:18:10 +01:00
|
|
|
#include "node-id.h"
|
|
|
|
|
2009-03-17 11:34:02 +01:00
|
|
|
PROCINIT(&etimer_process,&sensors_process);
|
2006-08-21 14:11:16 +02:00
|
|
|
|
|
|
|
/* Declare all sensors */
|
|
|
|
[SENSOR_DEFINITIONS]
|
|
|
|
|
|
|
|
/* Declare all simulation interfaces */
|
|
|
|
[INTERFACE_DEFINITIONS]
|
|
|
|
|
|
|
|
|
|
|
|
/* Create sensor array */
|
|
|
|
[SENSOR_ARRAY]
|
|
|
|
|
|
|
|
/* Create simulation interfaces array */
|
|
|
|
[INTERFACE_ARRAY]
|
|
|
|
|
|
|
|
/*
|
|
|
|
* referenceVar is used for comparing absolute and process relative memory.
|
|
|
|
* (this must not be static due to memory locations)
|
|
|
|
*/
|
2008-11-20 17:34:45 +01:00
|
|
|
long referenceVar;
|
2006-08-21 14:11:16 +02:00
|
|
|
|
2006-09-29 16:32:15 +02:00
|
|
|
/*
|
|
|
|
* process_run() infinite loop.
|
|
|
|
* Is yielded at least once per function call.
|
|
|
|
*/
|
|
|
|
static struct cooja_mt_thread process_run_thread;
|
|
|
|
|
2009-01-21 13:18:10 +01:00
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static void
|
|
|
|
print_processes(struct process * const processes[])
|
|
|
|
{
|
|
|
|
/* const struct process * const * p = processes;*/
|
|
|
|
printf("Starting");
|
|
|
|
while(*processes != NULL) {
|
|
|
|
printf(" '%s'", (*processes)->name);
|
|
|
|
processes++;
|
|
|
|
}
|
|
|
|
putchar('\n');
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2006-09-29 16:32:15 +02:00
|
|
|
static void
|
|
|
|
start_process_run_loop(void *data)
|
|
|
|
{
|
2009-01-21 13:18:10 +01:00
|
|
|
int i;
|
|
|
|
|
2007-08-23 09:36:34 +02:00
|
|
|
/* Initialize random generator */
|
|
|
|
random_init(0);
|
|
|
|
|
|
|
|
/* Start process handler */
|
|
|
|
process_init();
|
|
|
|
|
2009-03-10 22:24:46 +01:00
|
|
|
/* Start Contiki processes */
|
|
|
|
procinit_init();
|
|
|
|
|
2009-01-21 13:18:10 +01:00
|
|
|
/* Print startup information */
|
|
|
|
printf(CONTIKI_VERSION_STRING " started. ");
|
|
|
|
if(node_id > 0) {
|
|
|
|
printf("Node id is set to %u.\n", node_id);
|
|
|
|
} else {
|
|
|
|
printf("Node id is not set.\n");
|
|
|
|
}
|
2007-08-23 09:36:34 +02:00
|
|
|
|
|
|
|
/* Initialize communication stack */
|
|
|
|
init_net();
|
2009-01-21 13:18:10 +01:00
|
|
|
printf("Rime started with address ");
|
|
|
|
for(i = 0; i < sizeof(rimeaddr_node_addr.u8) - 1; i++) {
|
|
|
|
printf("%d.", rimeaddr_node_addr.u8[i]);
|
|
|
|
}
|
|
|
|
printf("%d\n", rimeaddr_node_addr.u8[i]);
|
2008-11-20 17:34:45 +01:00
|
|
|
|
2009-03-10 22:24:46 +01:00
|
|
|
/* Start autostart processes (defined in Contiki application) */
|
2009-01-21 13:18:10 +01:00
|
|
|
print_processes(autostart_processes);
|
2008-02-10 23:36:36 +01:00
|
|
|
autostart_start(autostart_processes);
|
2008-11-20 17:34:45 +01:00
|
|
|
|
2007-08-23 09:36:34 +02:00
|
|
|
while(1)
|
2006-09-29 16:32:15 +02:00
|
|
|
{
|
|
|
|
/* Always pretend we have processes left while inside process_run() */
|
|
|
|
simProcessRunValue = 1;
|
2006-10-02 17:07:33 +02:00
|
|
|
|
2007-08-21 15:09:40 +02:00
|
|
|
if (simDoReceiverCallback) {
|
|
|
|
simDoReceiverCallback = 0;
|
|
|
|
radio_call_receiver();
|
2006-10-09 16:42:25 +02:00
|
|
|
}
|
|
|
|
|
2006-09-29 16:32:15 +02:00
|
|
|
simProcessRunValue = process_run();
|
|
|
|
|
2006-10-05 14:09:40 +02:00
|
|
|
// Check if we must stay awake
|
|
|
|
if (simDontFallAsleep) {
|
|
|
|
simDontFallAsleep=0;
|
|
|
|
simProcessRunValue = 1;
|
|
|
|
}
|
|
|
|
|
2006-09-29 16:32:15 +02:00
|
|
|
/* Yield thread when one process_run has completed */
|
|
|
|
cooja_mt_yield();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-08-21 14:11:16 +02:00
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
/**
|
|
|
|
* \brief Initialize a mote by starting processes etc.
|
|
|
|
*
|
|
|
|
* This function initializes a mote by starting certain
|
|
|
|
* processes and setting up the environment.
|
|
|
|
*
|
|
|
|
* This is a JNI function and should only be called via the
|
|
|
|
* responsible Java part (MoteType.java).
|
|
|
|
*/
|
|
|
|
JNIEXPORT void JNICALL
|
|
|
|
Java_se_sics_cooja_corecomm_[CLASS_NAME]_init(JNIEnv *env, jobject obj)
|
|
|
|
{
|
2006-09-29 16:32:15 +02:00
|
|
|
/* Prepare thread that will do the process_run()-loop */
|
|
|
|
cooja_mt_start(&process_run_thread, &start_process_run_loop, NULL);
|
2006-08-21 14:11:16 +02:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
/**
|
|
|
|
* \brief Get a segment from the process memory.
|
|
|
|
* \param start Start address of segment
|
|
|
|
* \param length Size of memory segment
|
|
|
|
* \return Java byte array containing a copy of memory segment.
|
|
|
|
*
|
|
|
|
* Fetches a memory segment from the process memory starting at
|
|
|
|
* (start), with size (length). This function does not perform
|
|
|
|
* ANY error checking, and the process may crash if addresses are
|
|
|
|
* not available/readable.
|
|
|
|
*
|
|
|
|
* This is a JNI function and should only be called via the
|
|
|
|
* responsible Java part (MoteType.java).
|
|
|
|
*/
|
2006-08-23 19:11:09 +02:00
|
|
|
JNIEXPORT void JNICALL
|
2008-11-20 17:34:45 +01:00
|
|
|
Java_se_sics_cooja_corecomm_[CLASS_NAME]_getMemory(JNIEnv *env, jobject obj, jint rel_addr, jint length, jbyteArray mem_arr)
|
2006-08-21 14:11:16 +02:00
|
|
|
{
|
2008-11-20 17:34:45 +01:00
|
|
|
(*env)->SetByteArrayRegion(
|
|
|
|
env,
|
|
|
|
mem_arr,
|
|
|
|
0,
|
|
|
|
(size_t) length,
|
|
|
|
(jbyte *) (((long)rel_addr) + referenceVar)
|
|
|
|
);
|
2006-08-21 14:11:16 +02:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
/**
|
|
|
|
* \brief Replace a segment of the process memory with given byte array.
|
|
|
|
* \param start Start address of segment
|
|
|
|
* \param length Size of memory segment
|
|
|
|
* \param mem_arr Byte array contaning new memory
|
|
|
|
*
|
|
|
|
* Replaces a process memory segment with given byte array.
|
|
|
|
* This function does not perform ANY error checking, and the
|
|
|
|
* process may crash if addresses are not available/writable.
|
|
|
|
*
|
|
|
|
* This is a JNI function and should only be called via the
|
|
|
|
* responsible Java part (MoteType.java).
|
|
|
|
*/
|
|
|
|
JNIEXPORT void JNICALL
|
2008-11-20 17:34:45 +01:00
|
|
|
Java_se_sics_cooja_corecomm_[CLASS_NAME]_setMemory(JNIEnv *env, jobject obj, jint rel_addr, jint length, jbyteArray mem_arr)
|
2006-08-21 14:11:16 +02:00
|
|
|
{
|
|
|
|
jbyte *mem = (*env)->GetByteArrayElements(env, mem_arr, 0);
|
2008-11-20 17:34:45 +01:00
|
|
|
memcpy(
|
|
|
|
(char*) (((long)rel_addr) + referenceVar),
|
|
|
|
mem,
|
|
|
|
length);
|
2006-08-21 14:11:16 +02:00
|
|
|
(*env)->ReleaseByteArrayElements(env, mem_arr, mem, 0);
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
/**
|
|
|
|
* \brief Let mote execute one "block" of code (tick mote).
|
|
|
|
*
|
|
|
|
* Let mote defined by the active contiki processes and current
|
|
|
|
* process memory execute some program code. This code must not block
|
|
|
|
* or else this function will never return. A typical contiki
|
|
|
|
* process will return when it executes PROCESS_WAIT..() statements.
|
|
|
|
*
|
|
|
|
* Before the control is left to contiki processes, any messages
|
|
|
|
* from the Java part are handled. These may for example be
|
|
|
|
* incoming network data. After the contiki processes return control,
|
|
|
|
* messages to the Java part are also handled (those which may need
|
|
|
|
* special attention).
|
|
|
|
*
|
|
|
|
* This is a JNI function and should only be called via the
|
|
|
|
* responsible Java part (MoteType.java).
|
|
|
|
*/
|
|
|
|
JNIEXPORT void JNICALL
|
|
|
|
Java_se_sics_cooja_corecomm_[CLASS_NAME]_tick(JNIEnv *env, jobject obj)
|
|
|
|
{
|
|
|
|
/* Let all simulation interfaces act first */
|
2006-10-09 16:13:42 +02:00
|
|
|
simNoYield = 1;
|
2006-08-21 14:11:16 +02:00
|
|
|
doActionsBeforeTick();
|
2006-10-09 16:13:42 +02:00
|
|
|
simNoYield = 0;
|
2006-08-21 14:11:16 +02:00
|
|
|
|
|
|
|
/* Check if any e-timers are pending (save result for state decisions) */
|
|
|
|
if (etimer_pending()) {
|
|
|
|
/* Poll etimers */
|
|
|
|
etimer_request_poll();
|
|
|
|
simEtimerPending = 1;
|
|
|
|
} else {
|
|
|
|
simEtimerPending = 0;
|
|
|
|
}
|
|
|
|
|
2006-09-29 16:32:15 +02:00
|
|
|
/* Let Contiki execute one or a part of the process_run()-function via the thread.
|
|
|
|
This call stores the process_run() return value */
|
|
|
|
cooja_mt_exec(&process_run_thread);
|
2006-08-21 14:11:16 +02:00
|
|
|
|
|
|
|
/* Let all simulation interfaces act before returning to java */
|
2006-10-09 16:13:42 +02:00
|
|
|
simNoYield = 1;
|
2006-08-21 14:11:16 +02:00
|
|
|
doActionsAfterTick();
|
2006-10-09 16:13:42 +02:00
|
|
|
simNoYield = 0;
|
2006-08-21 14:11:16 +02:00
|
|
|
|
|
|
|
/* Look for new e-timers */
|
|
|
|
if (!simEtimerPending && etimer_pending()) {
|
|
|
|
/* Poll etimers */
|
|
|
|
etimer_request_poll();
|
|
|
|
simEtimerPending = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Save nearest event timer expiration time (0 if no timers) */
|
|
|
|
simNextExpirationTime = etimer_next_expiration_time();
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
/**
|
2008-11-20 18:04:34 +01:00
|
|
|
* \brief Set the relative memory address of the reference variable.
|
|
|
|
* \return Relative memory address.
|
2006-08-21 14:11:16 +02:00
|
|
|
*
|
|
|
|
* This is a JNI function and should only be called via the
|
|
|
|
* responsible Java part (MoteType.java).
|
|
|
|
*/
|
2008-11-20 18:04:34 +01:00
|
|
|
JNIEXPORT void JNICALL
|
2008-11-20 17:34:45 +01:00
|
|
|
Java_se_sics_cooja_corecomm_[CLASS_NAME]_setReferenceAddress(JNIEnv *env, jobject obj, jint addr)
|
2006-08-21 14:11:16 +02:00
|
|
|
{
|
2008-11-20 17:34:45 +01:00
|
|
|
referenceVar = (((long)&referenceVar) - ((long)addr));
|
2006-08-21 14:11:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/** @} */
|