using memory breakpoints instead of polling memory region during

bootup
This commit is contained in:
Fredrik Osterlind 2011-02-11 15:30:17 +01:00
parent 747eafd5f0
commit a4e99e1dc2
1 changed files with 123 additions and 217 deletions

View File

@ -31,10 +31,10 @@
package se.sics.cooja.mspmote.interfaces;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Observable;
import java.util.Observer;
import java.util.Vector;
import javax.swing.JLabel;
import javax.swing.JPanel;
@ -45,16 +45,13 @@ import org.jdom.Element;
import se.sics.cooja.Mote;
import se.sics.cooja.MoteTimeEvent;
import se.sics.cooja.Simulation;
import se.sics.cooja.TimeEvent;
import se.sics.cooja.interfaces.MoteID;
import se.sics.cooja.mspmote.MspMote;
import se.sics.cooja.mspmote.MspMoteMemory;
import se.sics.mspsim.core.CPUMonitor;
/**
* Mote ID number.
*
* @see #GENERATE_ID_HEADER
* @see #PERSISTENT_SET_ID
* Mote ID.
*
* @author Fredrik Osterlind
*/
@ -64,35 +61,13 @@ public class MspMoteID extends MoteID {
private MspMote mote;
private MspMoteMemory moteMem = null;
/**
* Write ID to flash at mote startup.
*/
public static final boolean GENERATE_ID_HEADER = true;
/**
* Persistently set ID in mote memory multiple times at node startup.
* May be used if emulator resets memory when firmware is loaded.
*/
public static final boolean PERSISTENT_SET_ID = true;
private int persistentSetIDCounter = 1000;
private enum ID_LOCATION {
VARIABLE_NODE_ID,
VARIABLE_TOS_NODE_ID,
VARIABLES_BOTH,
JAVA_ONLY
}
private ID_LOCATION location = ID_LOCATION.JAVA_ONLY;
private boolean writeFlashHeader = true;
private int moteID = -1;
/**
* Creates an interface to the mote ID at mote.
*
* @param mote
* Mote ID's mote.
* @param mote ID
* @see Mote
* @see se.sics.cooja.MoteInterfaceHandler
*/
@ -100,107 +75,71 @@ public class MspMoteID extends MoteID {
this.mote = (MspMote) m;
this.moteMem = (MspMoteMemory) mote.getMemory();
if (moteMem.variableExists("node_id") &&
moteMem.variableExists("TOS_NODE_ID")) {
location = ID_LOCATION.VARIABLES_BOTH;
} else if (moteMem.variableExists("node_id")) {
location = ID_LOCATION.VARIABLE_NODE_ID;
} else if (moteMem.variableExists("TOS_NODE_ID")) {
location = ID_LOCATION.VARIABLE_TOS_NODE_ID;
} else {
location = ID_LOCATION.JAVA_ONLY;
}
/*logger.debug("ID location: " + location);*/
final TimeEvent persistentSetIDEvent = new MoteTimeEvent(mote, 0) {
final MoteTimeEvent writeIDEvent = new MoteTimeEvent(mote, 0) {
public void execute(long t) {
if (persistentSetIDCounter-- > 0)
{
setMoteID(moteID);
/*logger.info("Setting ID: " + moteID + " at " + t);*/
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);
}
}
}
};
if (PERSISTENT_SET_ID) {
mote.getSimulation().invokeSimulationThread(new Runnable() {
public void run() {
persistentSetIDEvent.execute(MspMoteID.this.mote.getSimulation().getSimulationTime());
};
if (moteMem.variableExists("node_id")) {
this.mote.getCPU().setBreakPoint(moteMem.getVariableAddress("node_id"), new CPUMonitor() {
public void cpuAction(int type, int adr, int data) {
if (type != MEMORY_WRITE) {
return;
}
if (data == moteID) {
return;
}
Simulation s = mote.getSimulation();
s.scheduleEvent(writeIDEvent, s.getSimulationTime());
}
});
}
if (moteMem.variableExists("TOS_NODE_ID")) {
this.mote.getCPU().setBreakPoint(moteMem.getVariableAddress("TOS_NODE_ID"), new CPUMonitor() {
public void cpuAction(int type, int adr, int data) {
if (type != MEMORY_WRITE) {
return;
}
if (data == moteID) {
return;
}
Simulation s = mote.getSimulation();
s.scheduleEvent(writeIDEvent, s.getSimulationTime());
}
});
}
}
public int getMoteID() {
/*if (location == ID_LOCATION.VARIABLE_NODE_ID) {
return moteMem.getIntValueOf("node_id");
}
if (location == ID_LOCATION.VARIABLES_BOTH) {
return moteMem.getIntValueOf("node_id");
}
if (location == ID_LOCATION.VARIABLE_TOS_NODE_ID) {
return moteMem.getIntValueOf("TOS_NODE_ID");
}
if (location == ID_LOCATION.JAVA_ONLY) {
return moteID;
}*/
return moteID;
}
public void setMoteID(int newID) {
/* tell mote instance */
if (moteID != newID) {
mote.idUpdated(newID);
setChanged();
}
moteID = newID;
if (location == ID_LOCATION.VARIABLE_NODE_ID) {
if (GENERATE_ID_HEADER) {
if (moteMem.variableExists("node_id")) {
moteMem.setIntValueOf("node_id", moteID);
if (writeFlashHeader) {
/* Write to external flash */
SkyFlash flash = mote.getInterfaces().getInterfaceOfType(SkyFlash.class);
if (flash != null) {
flash.writeIDheader(newID);
flash.writeIDheader(moteID);
}
writeFlashHeader = false;
}
moteMem.setIntValueOf("node_id", newID);
/* Experimental: set Contiki random seed variable if it exists */
if (moteMem.variableExists("rseed")) {
moteMem.setIntValueOf("rseed", (int) (mote.getSimulation().getRandomSeed() + newID));
}
setChanged();
notifyObservers();
return;
}
if (location == ID_LOCATION.VARIABLES_BOTH) {
if (GENERATE_ID_HEADER) {
/* Write to external flash */
SkyFlash flash = mote.getInterfaces().getInterfaceOfType(SkyFlash.class);
if (flash != null) {
flash.writeIDheader(newID);
}
}
moteMem.setIntValueOf("node_id", newID);
/* Experimental: set Contiki random seed variable if it exists */
if (moteMem.variableExists("rseed")) {
moteMem.setIntValueOf("rseed", (int) (mote.getSimulation().getRandomSeed() + newID));
}
if (moteMem.variableExists("TOS_NODE_ID")) {
moteMem.setIntValueOf("TOS_NODE_ID", newID);
moteMem.setIntValueOf("TOS_NODE_ID", moteID);
}
if (moteMem.variableExists("ActiveMessageAddressC__addr")) {
moteMem.setIntValueOf("ActiveMessageAddressC__addr", newID);
@ -209,34 +148,7 @@ public class MspMoteID extends MoteID {
moteMem.setIntValueOf("ActiveMessageAddressC$addr", newID);
}
setChanged();
notifyObservers();
return;
}
if (location == ID_LOCATION.VARIABLE_TOS_NODE_ID) {
if (moteMem.variableExists("TOS_NODE_ID")) {
moteMem.setIntValueOf("TOS_NODE_ID", newID);
}
if (moteMem.variableExists("ActiveMessageAddressC__addr")) {
moteMem.setIntValueOf("ActiveMessageAddressC__addr", newID);
}
if (moteMem.variableExists("ActiveMessageAddressC$addr")) {
moteMem.setIntValueOf("ActiveMessageAddressC$addr", newID);
}
setChanged();
notifyObservers();
return;
}
if (location == ID_LOCATION.JAVA_ONLY) {
setChanged();
notifyObservers();
return;
}
logger.fatal("Unknown node ID location");
}
public JPanel getInterfaceVisualizer() {
@ -254,7 +166,6 @@ public class MspMoteID extends MoteID {
}
});
// Saving observer reference for releaseInterfaceVisualizer
panel.putClientProperty("intf_obs", observer);
return panel;
@ -271,14 +182,10 @@ public class MspMoteID extends MoteID {
}
public Collection<Element> getConfigXML() {
Vector<Element> config = new Vector<Element>();
Element element;
// Infinite boolean
element = new Element("id");
ArrayList<Element> config = new ArrayList<Element>();
Element element = new Element("id");
element.setText(Integer.toString(getMoteID()));
config.add(element);
return config;
}
@ -289,5 +196,4 @@ public class MspMoteID extends MoteID {
}
}
}
}