added avrora-micaz node emulation

This commit is contained in:
joxe 2009-02-22 16:45:01 +00:00
parent bc7c90bb9f
commit 8be8d92923
7 changed files with 1699 additions and 1 deletions

View File

@ -0,0 +1,54 @@
<?xml version="1.0"?>
<project name="Avrora COOJA support" default="jar" basedir=".">
<property name="src" location="src"/>
<property name="build" location="build"/>
<property name="lib" location="lib"/>
<property name="javadoc" location="javadoc"/>
<property name="cooja" location="../.."/>
<property name="cooja_jar" value="${cooja}/dist/cooja.jar"/>
<property name="avrora_jar" value="${lib}/avrora.jar"/>
<property name="cooja_avrora_jar" value="${lib}/cooja_avrora.jar"/>
<target name="init">
<tstamp/>
<mkdir dir="${build}"/>
<mkdir dir="${lib}"/>
</target>
<target name="clean">
<delete dir="${build}"/>
<delete dir="${lib}"/>
</target>
<target name="compile" depends="init">
<javac srcdir="${src}" destdir="${build}" debug="on">
<classpath>
<pathelement location="${avrora_jar}"/>
<pathelement location="${cooja_jar}"/>
</classpath>
</javac>
</target>
<target name="javadoc" depends="compile">
<delete dir="${javadoc}" quiet="true"/>
<mkdir dir="${javadoc}/"/>
<javadoc destdir="${javadoc}">
<fileset dir="${src}/" includes="**/*.java"/>
<classpath>
<pathelement location="${avrora_jar}"/>
<pathelement location="${cooja_jar}"/>
</classpath>
</javadoc>
</target>
<target name="jar" depends="init, compile">
<jar destfile="${cooja_avrora_jar}" basedir="${build}">
<manifest>
<attribute name="Class-Path" value="."/>
</manifest>
</jar>
</target>
</project>

View File

@ -0,0 +1,2 @@
se.sics.cooja.GUI.MOTETYPES = + se.sics.cooja.avrmote.MicaZMoteType
se.sics.cooja.GUI.JARFILES = + cooja_avrora.jar avrora.jar

Binary file not shown.

View File

@ -0,0 +1,312 @@
/*
* Copyright (c) 2007, 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.
*
* $Id: MicaZMote.java,v 1.1 2009/02/22 16:45:01 joxe Exp $
*/
package se.sics.cooja.avrmote;
import java.io.File;
import java.io.IOException;
import java.io.PrintStream;
import java.net.URL;
import java.util.Collection;
import java.util.Observable;
import java.util.Observer;
import java.util.Random;
import java.util.Vector;
import org.apache.log4j.Logger;
import org.jdom.Element;
import se.sics.cooja.GUI;
import se.sics.cooja.Mote;
import se.sics.cooja.MoteInterface;
import se.sics.cooja.MoteInterfaceHandler;
import se.sics.cooja.MoteMemory;
import se.sics.cooja.MoteType;
import se.sics.cooja.Simulation;
import se.sics.cooja.avrmote.interfaces.MicaZLED;
import se.sics.cooja.interfaces.Position;
import avrora.sim.*;
import avrora.sim.platform.*;
import avrora.sim.mcu.*;
import avrora.core.*;
import avrora.sim.radio.*;
import avrora.sim.util.SimUtil;
/**
* @author Joakim Eriksson
*/
public class MicaZMote implements Mote {
private static Logger logger = Logger.getLogger(MicaZMote.class);
/* 8 MHz according to Contiki config */
public static long NR_CYCLES_PER_MSEC = 8000;
/* Cycle counter */
public long cycleCounter = 0;
public long cycleDrift = 0;
private Simulation mySimulation = null;
private Microcontroller myCpu = null;
private MicaZ micaZ = null;
private LoadableProgram program = null;
private Interpreter interpreter = null;
private MicaZMoteType myMoteType = null;
/* Stack monitoring variables */
private boolean stopNextInstruction = false;
private MoteInterfaceHandler myMoteInterfaceHandler;
/**
* Abort current tick immediately.
* May for example be called by a breakpoint handler.
*/
public void stopNextInstruction() {
stopNextInstruction = true;
}
public MicaZMote() {
mySimulation = null;
myCpu = null;
myMoteInterfaceHandler = null;
}
public MicaZMote(Simulation simulation, MicaZMoteType type) {
mySimulation = simulation;
myMoteType = type;
}
protected void initMote() {
if (myMoteType != null) {
initEmulator(myMoteType.getELFFile());
myMoteInterfaceHandler = createMoteInterfaceHandler();
}
}
protected boolean initEmulator(String fileELF) {
try {
prepareMote(fileELF);
} catch (Exception e) {
logger.fatal("Error when creating MicaZ mote:", e);
return false;
}
return true;
}
public Simulation getSimulation() {
return mySimulation;
}
public void setSimulation(Simulation simulation) {
mySimulation = simulation;
}
/**
* Prepares CPU, memory and ELF module.
*
* @param fileELF ELF file
* @param cpu MSP430 cpu
* @throws Exception
*/
protected void prepareMote(String file) throws Exception {
program = new LoadableProgram(file);
program.load();
PlatformFactory factory = new MicaZ.Factory();
micaZ = (MicaZ) factory.newPlatform(1, program.getProgram());
myCpu = micaZ.getMicrocontroller();
Simulator sim = myCpu.getSimulator();
interpreter = sim.getInterpreter();
// State state = interpreter.getState();
}
public void setState(State newState) {
logger.warn("MicaZ motes can't change state");
}
public State getState() {
return Mote.State.ACTIVE;
}
/* called when moteID is updated */
public void idUpdated(int newID) {
}
public MoteType getType() {
return myMoteType;
}
public void setType(MoteType type) {
//myMoteType = (MspMoteType) type;
}
public void addStateObserver(Observer newObserver) {
}
public void deleteStateObserver(Observer newObserver) {
}
public MoteInterfaceHandler getInterfaces() {
return myMoteInterfaceHandler;
}
public void setInterfaces(MoteInterfaceHandler moteInterfaceHandler) {
myMoteInterfaceHandler = moteInterfaceHandler;
}
/* return false when done - e.g. true means more work to do before finished with this tick */
private boolean firstTick = true;
private long cyclesExecuted = 0;
public boolean tick(long simTime) {
if (stopNextInstruction) {
stopNextInstruction = false;
throw new RuntimeException("MSPSim requested simulation stop");
}
/* Nodes may be added in an ongoing simulation:
* Update cycle drift to current simulation time */
if (firstTick) {
firstTick = false;
cycleDrift += (-NR_CYCLES_PER_MSEC*simTime);
}
long maxSimTimeCycles = NR_CYCLES_PER_MSEC * (simTime + 1) + cycleDrift;
if (maxSimTimeCycles <= cycleCounter) {
return false;
}
// Leave control to emulated CPU
cycleCounter += 1;
if (cyclesExecuted > cycleCounter) {
/* CPU already ticked too far - just wait it out */
return true;
}
// myMoteInterfaceHandler.doActiveActionsBeforeTick();
cyclesExecuted += interpreter.step();
//cpu.step(cycleCounter);
/* Check if radio has pending incoming bytes */
// if (myRadio != null && myRadio.hasPendingBytes()) {
// myRadio.tryDeliverNextByte(cpu.cycles);
// }
return true;
}
public boolean setConfigXML(Simulation simulation, Collection<Element> configXML, boolean visAvailable) {
for (Element element: configXML) {
String name = element.getName();
if (name.equals("motetype_identifier")) {
setSimulation(simulation);
myMoteType = (MicaZMoteType) simulation.getMoteType(element.getText());
getType().setIdentifier(element.getText());
try {
prepareMote(myMoteType.getELFFile());
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
myMoteInterfaceHandler = createMoteInterfaceHandler();
} else if (name.equals("interface_config")) {
Class<? extends MoteInterface> moteInterfaceClass = simulation.getGUI().tryLoadClass(
this, MoteInterface.class, element.getText().trim());
if (moteInterfaceClass == null) {
logger.fatal("Could not load mote interface class: " + element.getText().trim());
return false;
}
MoteInterface moteInterface = getInterfaces().getInterfaceOfType(moteInterfaceClass);
moteInterface.setConfigXML(element.getChildren(), visAvailable);
}
}
return true;
}
private MoteInterfaceHandler createMoteInterfaceHandler() {
System.out.println("Creating mote interface handler....");
MoteInterfaceHandler moteInterfaceHandler = new MoteInterfaceHandler();
// Add position interface
Position motePosition = new Position(this);
Random random = new Random(); /* Do not use main random generator for positioning */
motePosition.setCoordinates(random.nextDouble()*100, random.nextDouble()*100, random.nextDouble()*100);
moteInterfaceHandler.addInterface(motePosition);
// Add LED interface
MicaZLED moteLED = new MicaZLED(micaZ);
moteInterfaceHandler.addInterface(moteLED);
return moteInterfaceHandler;
}
public Collection<Element> getConfigXML() {
Vector<Element> config = new Vector<Element>();
Element element;
// Mote type identifier
element = new Element("motetype_identifier");
element.setText(getType().getIdentifier());
config.add(element);
// Mote interfaces
for (MoteInterface moteInterface: getInterfaces().getInterfaces()) {
element = new Element("interface_config");
element.setText(moteInterface.getClass().getName());
Collection<Element> interfaceXML = moteInterface.getConfigXML();
if (interfaceXML != null) {
element.addContent(interfaceXML);
config.add(element);
}
}
return config;
}
public MoteMemory getMemory() {
return null;
}
public void setMemory(MoteMemory memory) {
}
}

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,195 @@
/*
* Copyright (c) 2007, 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.
*
* $Id: MicaZLED.java,v 1.1 2009/02/22 16:45:01 joxe Exp $
*/
package se.sics.cooja.avrmote.interfaces;
import java.awt.*;
import java.util.*;
import javax.swing.*;
import org.apache.log4j.Logger;
import org.jdom.Element;
import avrora.sim.FiniteStateMachine;
import avrora.sim.platform.MicaZ;
import se.sics.cooja.*;
import se.sics.cooja.interfaces.LED;
/**
* @author Joakim Eriksson
*/
@ClassDescription("MicaZ LED")
public class MicaZLED extends LED {
private static Logger logger = Logger.getLogger(MicaZLED.class);
private boolean blueOn = false;
private boolean greenOn = false;
private boolean redOn = false;
private static final Color DARK_BLUE = new Color(0, 0, 100);
private static final Color DARK_GREEN = new Color(0, 100, 0);
private static final Color DARK_RED = new Color(100, 0, 0);
private static final Color BLUE = new Color(0, 0, 255);
private static final Color GREEN = new Color(0, 255, 0);
private static final Color RED = new Color(255, 0, 0);
public MicaZLED(MicaZ micaZ) {
avrora.sim.platform.LED.LEDGroup leds =
(avrora.sim.platform.LED.LEDGroup) micaZ.getDevice("leds");
leds.leds[0].getFSM().insertProbe(new FiniteStateMachine.Probe() {
public void fireAfterTransition(int old, int newstate) {
redOn = newstate > 0;
setChanged();
notifyObservers();
}
public void fireBeforeTransition(int arg0, int arg1) {
}
});
leds.leds[1].getFSM().insertProbe(new FiniteStateMachine.Probe() {
public void fireAfterTransition(int old, int newstate) {
blueOn = newstate > 0;
setChanged();
notifyObservers();
}
public void fireBeforeTransition(int arg0, int arg1) {
}
});
leds.leds[2].getFSM().insertProbe(new FiniteStateMachine.Probe() {
public void fireAfterTransition(int old, int newstate) {
greenOn = newstate > 0;
setChanged();
notifyObservers();
}
public void fireBeforeTransition(int arg0, int arg1) {
}
});
}
public boolean isAnyOn() {
return blueOn || greenOn || redOn;
}
public boolean isGreenOn() {
return greenOn;
}
public boolean isYellowOn() {
return blueOn; /* Returning blue */
}
public boolean isRedOn() {
return redOn;
}
public double energyConsumption() {
return 0;
}
public JPanel getInterfaceVisualizer() {
final JPanel panel = new JPanel() {
public void paintComponent(Graphics g) {
super.paintComponent(g);
int x = 20;
int y = 25;
int d = 25;
if (isGreenOn()) {
g.setColor(GREEN);
g.fillOval(x, y, d, d);
g.setColor(Color.BLACK);
g.drawOval(x, y, d, d);
} else {
g.setColor(DARK_GREEN);
g.fillOval(x + 5, y + 5, d-10, d-10);
}
x += 40;
if (isRedOn()) {
g.setColor(RED);
g.fillOval(x, y, d, d);
g.setColor(Color.BLACK);
g.drawOval(x, y, d, d);
} else {
g.setColor(DARK_RED);
g.fillOval(x + 5, y + 5, d-10, d-10);
}
x += 40;
if (isYellowOn()) {
g.setColor(BLUE);
g.fillOval(x, y, d, d);
g.setColor(Color.BLACK);
g.drawOval(x, y, d, d);
} else {
g.setColor(DARK_BLUE);
g.fillOval(x + 5, y + 5, d-10, d-10);
}
}
};
Observer observer;
this.addObserver(observer = new Observer() {
public void update(Observable obs, Object obj) {
panel.repaint();
}
});
// Saving observer reference for releaseInterfaceVisualizer
panel.putClientProperty("intf_obs", observer);
panel.setMinimumSize(new Dimension(140, 60));
panel.setPreferredSize(new Dimension(140, 60));
return panel;
}
public void releaseInterfaceVisualizer(JPanel panel) {
Observer observer = (Observer) panel.getClientProperty("intf_obs");
if (observer == null) {
logger.fatal("Error when releasing panel, observer is null");
return;
}
this.deleteObserver(observer);
}
public Collection<Element> getConfigXML() {
return null;
}
public void setConfigXML(Collection<Element> configXML, boolean visAvailable) {
}
}

View File

@ -22,7 +22,7 @@ CONTIKI_STANDARD_PROCESSES = sensors_process;etimer_process
CONTIKI_MAIN_TEMPLATE_FILENAME = contiki_template.c
CORECOMM_TEMPLATE_FILENAME = corecomm_template.java
PATH_JAVAC = javac
DEFAULT_PROJECTDIRS = ../apps/mrm;../apps/mspsim
DEFAULT_PROJECTDIRS = ../apps/mrm;../apps/mspsim;../apps/avrora
PARSE_WITH_COMMAND = true
PARSE_COMMAND=nm -C $(LIBFILE)