219 lines
7 KiB
Text
219 lines
7 KiB
Text
/**
|
|
* \defgroup coojacore COOJA Simulator Core
|
|
* @{
|
|
*/
|
|
|
|
/**
|
|
* \file
|
|
* C code template for generating contiki source code files from COOJA
|
|
* Simulator. This file should not be compiled directly.
|
|
* \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"
|
|
#include "net/uip.h"
|
|
#include "dev/radio-arch.h"
|
|
#include "sys/etimer.h"
|
|
|
|
/* Declare all initialization processes */
|
|
[PROCESS_DEFINITIONS]
|
|
|
|
/* Declare all sensors */
|
|
[SENSOR_DEFINITIONS]
|
|
|
|
/* Declare all simulation interfaces */
|
|
[INTERFACE_DEFINITIONS]
|
|
|
|
|
|
/* Create initialization process array */
|
|
[PROCESS_ARRAY]
|
|
|
|
/* Create sensor array */
|
|
[SENSOR_ARRAY]
|
|
|
|
/* Create simulation interfaces array */
|
|
[INTERFACE_ARRAY]
|
|
|
|
// Default network interface
|
|
static struct uip_fw_netif simNetworkIF =
|
|
{UIP_FW_NETIF(0,0,0,0, 0,0,0,0, simDoSend)};
|
|
|
|
/*
|
|
* referenceVar is used for comparing absolute and process relative memory.
|
|
* (this must not be static due to memory locations)
|
|
*/
|
|
int referenceVar;
|
|
|
|
extern unsigned long _end;
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
/**
|
|
* \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)
|
|
{
|
|
/* Initialize random generator */
|
|
random_init(0);
|
|
|
|
/* Start process handler */
|
|
process_init();
|
|
|
|
/* Start Contiki processes */
|
|
procinit_init();
|
|
|
|
/* Initialize uIP */
|
|
uip_init();
|
|
uip_fw_init();
|
|
|
|
/* Register network interface */
|
|
uip_fw_default(&simNetworkIF);
|
|
|
|
/* Start user applications */
|
|
autostart_start((struct process **) autostart_processes);
|
|
}
|
|
/*---------------------------------------------------------------------------*/
|
|
/**
|
|
* \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).
|
|
*/
|
|
JNIEXPORT void JNICALL
|
|
Java_se_sics_cooja_corecomm_[CLASS_NAME]_getMemory(JNIEnv *env, jobject obj, jint start, jint length, jbyteArray mem_arr)
|
|
{
|
|
// jbyteArray ret=(*env)->NewByteArray(env, length);
|
|
(*env)->SetByteArrayRegion(env, mem_arr, 0, (size_t) length, (jbyte *) start);
|
|
|
|
// return (ret);
|
|
}
|
|
/*---------------------------------------------------------------------------*/
|
|
/**
|
|
* \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
|
|
Java_se_sics_cooja_corecomm_[CLASS_NAME]_setMemory(JNIEnv *env, jobject obj, jint start, jint length, jbyteArray mem_arr)
|
|
{
|
|
jbyte *mem = (*env)->GetByteArrayElements(env, mem_arr, 0);
|
|
memcpy((void *) start, mem, length);
|
|
(*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 */
|
|
doActionsBeforeTick();
|
|
|
|
/* 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;
|
|
}
|
|
|
|
/* Execute the contiki code (save process_run return value for state decisions) */
|
|
simProcessRunValue = process_run();
|
|
|
|
/* Let all simulation interfaces act before returning to java */
|
|
doActionsAfterTick();
|
|
|
|
/* 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();
|
|
}
|
|
/*---------------------------------------------------------------------------*/
|
|
/**
|
|
* \brief Get the absolute memory address of a special variable.
|
|
* \return Absolute memory address.
|
|
*
|
|
* Returns the absolute memory address of a special variable
|
|
* "referenceVar". By comparing this address with the relative
|
|
* address (from the map file) for referenceVar, an runtime offset
|
|
* can be calculated.
|
|
*
|
|
* This is a JNI function and should only be called via the
|
|
* responsible Java part (MoteType.java).
|
|
*/
|
|
JNIEXPORT jint JNICALL
|
|
Java_se_sics_cooja_corecomm_[CLASS_NAME]_getReferenceAbsAddr(JNIEnv *env, jobject obj)
|
|
{
|
|
return (jint) &referenceVar;
|
|
}
|
|
|
|
/** @} */
|