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

View file

@ -31,10 +31,10 @@
package se.sics.cooja.mspmote.interfaces; package se.sics.cooja.mspmote.interfaces;
import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.Observable; import java.util.Observable;
import java.util.Observer; import java.util.Observer;
import java.util.Vector;
import javax.swing.JLabel; import javax.swing.JLabel;
import javax.swing.JPanel; import javax.swing.JPanel;
@ -45,249 +45,155 @@ import org.jdom.Element;
import se.sics.cooja.Mote; import se.sics.cooja.Mote;
import se.sics.cooja.MoteTimeEvent; import se.sics.cooja.MoteTimeEvent;
import se.sics.cooja.Simulation; import se.sics.cooja.Simulation;
import se.sics.cooja.TimeEvent;
import se.sics.cooja.interfaces.MoteID; import se.sics.cooja.interfaces.MoteID;
import se.sics.cooja.mspmote.MspMote; import se.sics.cooja.mspmote.MspMote;
import se.sics.cooja.mspmote.MspMoteMemory; import se.sics.cooja.mspmote.MspMoteMemory;
import se.sics.mspsim.core.CPUMonitor;
/** /**
* Mote ID number. * Mote ID.
*
* @see #GENERATE_ID_HEADER
* @see #PERSISTENT_SET_ID
* *
* @author Fredrik Osterlind * @author Fredrik Osterlind
*/ */
public class MspMoteID extends MoteID { public class MspMoteID extends MoteID {
private static Logger logger = Logger.getLogger(MspMoteID.class); private static Logger logger = Logger.getLogger(MspMoteID.class);
private MspMote mote; private MspMote mote;
private MspMoteMemory moteMem = null; private MspMoteMemory moteMem = null;
/** private boolean writeFlashHeader = true;
* Write ID to flash at mote startup. private int moteID = -1;
*/
public static final boolean GENERATE_ID_HEADER = true;
/**
* Creates an interface to the mote ID at mote.
*
* @param mote ID
* @see Mote
* @see se.sics.cooja.MoteInterfaceHandler
*/
public MspMoteID(Mote m) {
this.mote = (MspMote) m;
this.moteMem = (MspMoteMemory) mote.getMemory();
/** final MoteTimeEvent writeIDEvent = new MoteTimeEvent(mote, 0) {
* Persistently set ID in mote memory multiple times at node startup. public void execute(long t) {
* May be used if emulator resets memory when firmware is loaded. setMoteID(moteID);
*/ }
public static final boolean PERSISTENT_SET_ID = true; };
private int persistentSetIDCounter = 1000;
private enum ID_LOCATION { if (moteMem.variableExists("node_id")) {
VARIABLE_NODE_ID, this.mote.getCPU().setBreakPoint(moteMem.getVariableAddress("node_id"), new CPUMonitor() {
VARIABLE_TOS_NODE_ID, public void cpuAction(int type, int adr, int data) {
VARIABLES_BOTH, if (type != MEMORY_WRITE) {
JAVA_ONLY 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());
}
});
}
}
private ID_LOCATION location = ID_LOCATION.JAVA_ONLY; public int getMoteID() {
return moteID;
}
private int moteID = -1; public void setMoteID(int newID) {
if (moteID != newID) {
mote.idUpdated(newID);
setChanged();
}
moteID = newID;
/** if (moteMem.variableExists("node_id")) {
* Creates an interface to the mote ID at mote. moteMem.setIntValueOf("node_id", moteID);
*
* @param mote
* Mote ID's mote.
* @see Mote
* @see se.sics.cooja.MoteInterfaceHandler
*/
public MspMoteID(Mote m) {
this.mote = (MspMote) m;
this.moteMem = (MspMoteMemory) mote.getMemory();
if (moteMem.variableExists("node_id") && if (writeFlashHeader) {
moteMem.variableExists("TOS_NODE_ID")) { /* Write to external flash */
location = ID_LOCATION.VARIABLES_BOTH; SkyFlash flash = mote.getInterfaces().getInterfaceOfType(SkyFlash.class);
} else if (moteMem.variableExists("node_id")) { if (flash != null) {
location = ID_LOCATION.VARIABLE_NODE_ID; flash.writeIDheader(moteID);
} else if (moteMem.variableExists("TOS_NODE_ID")) { }
location = ID_LOCATION.VARIABLE_TOS_NODE_ID; writeFlashHeader = false;
} else { }
location = ID_LOCATION.JAVA_ONLY; /* 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", moteID);
}
if (moteMem.variableExists("ActiveMessageAddressC__addr")) {
moteMem.setIntValueOf("ActiveMessageAddressC__addr", newID);
}
if (moteMem.variableExists("ActiveMessageAddressC$addr")) {
moteMem.setIntValueOf("ActiveMessageAddressC$addr", newID);
}
/*logger.debug("ID location: " + location);*/ notifyObservers();
}
final TimeEvent persistentSetIDEvent = new MoteTimeEvent(mote, 0) { public JPanel getInterfaceVisualizer() {
public void execute(long t) { JPanel panel = new JPanel();
final JLabel idLabel = new JLabel();
if (persistentSetIDCounter-- > 0) idLabel.setText("Mote ID: " + getMoteID());
{
setMoteID(moteID);
/*logger.info("Setting ID: " + moteID + " at " + t);*/
if (t + mote.getInterfaces().getClock().getDrift() < 0) { panel.add(idLabel);
/* 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) { Observer observer;
mote.getSimulation().invokeSimulationThread(new Runnable() { this.addObserver(observer = new Observer() {
public void run() { public void update(Observable obs, Object obj) {
persistentSetIDEvent.execute(MspMoteID.this.mote.getSimulation().getSimulationTime()); idLabel.setText("Mote ID: " + getMoteID());
}; }
}); });
}
}
public int getMoteID() { panel.putClientProperty("intf_obs", observer);
/*if (location == ID_LOCATION.VARIABLE_NODE_ID) {
return moteMem.getIntValueOf("node_id");
}
if (location == ID_LOCATION.VARIABLES_BOTH) { return panel;
return moteMem.getIntValueOf("node_id"); }
}
if (location == ID_LOCATION.VARIABLE_TOS_NODE_ID) { public void releaseInterfaceVisualizer(JPanel panel) {
return moteMem.getIntValueOf("TOS_NODE_ID"); Observer observer = (Observer) panel.getClientProperty("intf_obs");
} if (observer == null) {
logger.fatal("Error when releasing panel, observer is null");
return;
}
if (location == ID_LOCATION.JAVA_ONLY) { this.deleteObserver(observer);
return moteID; }
}*/
return moteID; public Collection<Element> getConfigXML() {
} ArrayList<Element> config = new ArrayList<Element>();
Element element = new Element("id");
public void setMoteID(int newID) { element.setText(Integer.toString(getMoteID()));
/* tell mote instance */ config.add(element);
if (moteID != newID) { return config;
mote.idUpdated(newID); }
}
moteID = newID;
if (location == ID_LOCATION.VARIABLE_NODE_ID) {
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));
}
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);
}
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.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() {
JPanel panel = new JPanel();
final JLabel idLabel = new JLabel();
idLabel.setText("Mote ID: " + getMoteID());
panel.add(idLabel);
Observer observer;
this.addObserver(observer = new Observer() {
public void update(Observable obs, Object obj) {
idLabel.setText("Mote ID: " + getMoteID());
}
});
// Saving observer reference for releaseInterfaceVisualizer
panel.putClientProperty("intf_obs", observer);
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() {
Vector<Element> config = new Vector<Element>();
Element element;
// Infinite boolean
element = new Element("id");
element.setText(Integer.toString(getMoteID()));
config.add(element);
return config;
}
public void setConfigXML(Collection<Element> configXML, boolean visAvailable) {
for (Element element : configXML) {
if (element.getName().equals("id")) {
setMoteID(Integer.parseInt(element.getText()));
}
}
}
public void setConfigXML(Collection<Element> configXML, boolean visAvailable) {
for (Element element : configXML) {
if (element.getName().equals("id")) {
setMoteID(Integer.parseInt(element.getText()));
}
}
}
} }