From f5ae0b9c82e854cd683d4186db65666238324db7 Mon Sep 17 00:00:00 2001 From: joxe Date: Tue, 17 Nov 2009 14:09:02 +0000 Subject: [PATCH] fixed mote id for micaz --- .../se/sics/cooja/avrmote/AvrMoteMemory.java | 8 +- .../src/se/sics/cooja/avrmote/MicaZMote.java | 5 +- .../cooja/avrmote/interfaces/MicaZID.java | 151 ++++++++++++++---- 3 files changed, 125 insertions(+), 39 deletions(-) diff --git a/tools/cooja/apps/avrora/src/se/sics/cooja/avrmote/AvrMoteMemory.java b/tools/cooja/apps/avrora/src/se/sics/cooja/avrmote/AvrMoteMemory.java index d62178f37..270034823 100644 --- a/tools/cooja/apps/avrora/src/se/sics/cooja/avrmote/AvrMoteMemory.java +++ b/tools/cooja/apps/avrora/src/se/sics/cooja/avrmote/AvrMoteMemory.java @@ -24,7 +24,7 @@ * (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: AvrMoteMemory.java,v 1.2 2009/11/13 09:50:25 joxe Exp $ + * $Id: AvrMoteMemory.java,v 1.3 2009/11/17 14:09:02 joxe Exp $ */ package se.sics.cooja.avrmote; @@ -40,6 +40,8 @@ import avrora.arch.avr.AVRProperties; import avrora.core.SourceMapping; import avrora.core.SourceMapping.Location; import avrora.sim.AtmelInterpreter; +import avrora.sim.State; +import avrora.sim.Simulator.Watch; /** * @author Joakim Eriksson */ @@ -55,6 +57,10 @@ public class AvrMoteMemory implements MoteMemory, AddressMemory { this.interpreter = interpreter; this.avrProperties = avrProperties; } + + public void insertWatch(Watch w, int address) { + interpreter.getSimulator().insertWatch(w, address); + } public void clearMemory() { logger.fatal("not implemented"); diff --git a/tools/cooja/apps/avrora/src/se/sics/cooja/avrmote/MicaZMote.java b/tools/cooja/apps/avrora/src/se/sics/cooja/avrmote/MicaZMote.java index 7d440c02f..6fa8e935f 100755 --- a/tools/cooja/apps/avrora/src/se/sics/cooja/avrmote/MicaZMote.java +++ b/tools/cooja/apps/avrora/src/se/sics/cooja/avrmote/MicaZMote.java @@ -26,7 +26,7 @@ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * - * $Id: MicaZMote.java,v 1.10 2009/11/12 12:49:34 joxe Exp $ + * $Id: MicaZMote.java,v 1.11 2009/11/17 14:09:02 joxe Exp $ */ package se.sics.cooja.avrmote; @@ -45,11 +45,9 @@ import se.sics.cooja.MoteMemory; import se.sics.cooja.MoteType; import se.sics.cooja.Simulation; import se.sics.cooja.motes.AbstractEmulatedMote; -import se.sics.cooja.mspmote.MspMoteMemory; import avrora.arch.avr.AVRProperties; import avrora.core.LoadableProgram; import avrora.sim.AtmelInterpreter; -import avrora.sim.Interpreter; import avrora.sim.Simulator; import avrora.sim.State; import avrora.sim.mcu.Microcontroller; @@ -165,6 +163,7 @@ public class MicaZMote extends AbstractEmulatedMote implements Mote { /* called when moteID is updated */ public void idUpdated(int newID) { + } public MoteType getType() { diff --git a/tools/cooja/apps/avrora/src/se/sics/cooja/avrmote/interfaces/MicaZID.java b/tools/cooja/apps/avrora/src/se/sics/cooja/avrmote/interfaces/MicaZID.java index dca4c35b7..ffe70e5a7 100644 --- a/tools/cooja/apps/avrora/src/se/sics/cooja/avrmote/interfaces/MicaZID.java +++ b/tools/cooja/apps/avrora/src/se/sics/cooja/avrmote/interfaces/MicaZID.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2007, Swedish Institute of Computer Science. + * Copyright (c) 2009, Swedish Institute of Computer Science. * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -26,7 +26,7 @@ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * - * $Id: MicaZID.java,v 1.1 2009/09/17 10:45:13 fros4943 Exp $ + * $Id: MicaZID.java,v 1.2 2009/11/17 14:09:03 joxe Exp $ */ package se.sics.cooja.avrmote.interfaces; @@ -39,54 +39,135 @@ import javax.swing.JPanel; import org.apache.log4j.Logger; import org.jdom.Element; +import avrora.sim.State; +import avrora.sim.Simulator.Watch; + import se.sics.cooja.Mote; +import se.sics.cooja.MoteTimeEvent; +import se.sics.cooja.Simulation; +import se.sics.cooja.TimeEvent; +import se.sics.cooja.avrmote.AvrMoteMemory; import se.sics.cooja.interfaces.MoteID; public class MicaZID extends MoteID { - private static Logger logger = Logger.getLogger(MicaZID.class); - private int moteID = -1; /* TODO Implement */ + private static final boolean PERSISTENT_SET_ID = true; - public MicaZID(Mote mote) { - } + private static Logger logger = Logger.getLogger(MicaZID.class); - public int getMoteID() { - return moteID; - } + private int moteID = -1; /* TODO Implement */ - public void setMoteID(int newID) { - moteID = newID; - } + private AvrMoteMemory moteMem; + boolean tosID = false; + boolean contikiID = false; + private Mote mote; + private int persistentSetIDCounter = 1000; - public JPanel getInterfaceVisualizer() { - return null; - } + TimeEvent persistentSetIDEvent = new MoteTimeEvent(mote, 0) { + public void execute(long t) { + if (persistentSetIDCounter-- > 0) { + setMoteID(moteID); + if (t + mote.getInterfaces().getClock().getDrift() < 0) { + /* Wait until node is booting */ + mote.getSimulation().scheduleEvent(this, -mote.getInterfaces().getClock().getDrift()); + } else { + mote.getSimulation().scheduleEvent(this, t + Simulation.MILLISECOND / 16); + } + } + } + }; - public void releaseInterfaceVisualizer(JPanel panel) { - } - public double energyConsumption() { - return 0; - } + public MicaZID(Mote mote) { + this.mote = mote; + this.moteMem = (AvrMoteMemory) mote.getMemory(); - public Collection getConfigXML() { - Vector config = new Vector(); - Element element; + if (moteMem.variableExists("node_id")) { + contikiID = true; - // Infinite boolean - element = new Element("id"); - element.setText(Integer.toString(getMoteID())); - config.add(element); + int addr = moteMem.getVariableAddress("node_id"); + moteMem.insertWatch(new Watch() { + public void fireAfterRead(State arg0, int arg1, byte arg2) { + System.out.println("Read from node_id: " + arg2); + } + public void fireAfterWrite(State arg0, int arg1, byte arg2) { + } + public void fireBeforeRead(State arg0, int arg1) { + } + public void fireBeforeWrite(State arg0, int arg1, byte arg2) { + System.out.println("Writing to node_id: " + arg2); + }}, addr); + } - return config; - } + if (moteMem.variableExists("TOS_NODE_ID")) { + tosID = true; + } - public void setConfigXML(Collection configXML, boolean visAvailable) { - for (Element element : configXML) { - if (element.getName().equals("id")) { - setMoteID(Integer.parseInt(element.getText())); - } + + if (PERSISTENT_SET_ID) { + mote.getSimulation().invokeSimulationThread(new Runnable() { + public void run() { + persistentSetIDEvent.execute(MicaZID.this.mote.getSimulation().getSimulationTime()); + }; + }); + } } - } + public int getMoteID() { + if (contikiID) { + return moteMem.getIntValueOf("node_id"); + } + + if (tosID) { + return moteMem.getIntValueOf("TOS_NODE_ID"); + } + return moteID; + } + + public void setMoteID(int newID) { + moteID = newID; + if (contikiID) { + System.out.println("Setting node id: " + newID); + moteMem.setIntValueOf("node_id", newID); + } + if (tosID) { + moteMem.setIntValueOf("TOS_NODE_ID", newID); + moteMem.setIntValueOf("ActiveMessageAddressC$addr", newID); + } + setChanged(); + notifyObservers(); + return; + } + + + public JPanel getInterfaceVisualizer() { + return null; + } + + public void releaseInterfaceVisualizer(JPanel panel) { + } + + public double energyConsumption() { + return 0; + } + + public Collection getConfigXML() { + Vector config = new Vector(); + Element element; + + // Infinite boolean + element = new Element("id"); + element.setText(Integer.toString(getMoteID())); + config.add(element); + + return config; + } + + public void setConfigXML(Collection configXML, boolean visAvailable) { + for (Element element : configXML) { + if (element.getName().equals("id")) { + setMoteID(Integer.parseInt(element.getText())); + } + } + } }