event based mote interfaces: less dependencies on tick polling + using new mote time events

This commit is contained in:
fros4943 2009-05-26 14:24:20 +00:00
parent fba3701a86
commit d996ae1476
12 changed files with 182 additions and 116 deletions

View file

@ -26,7 +26,7 @@
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE. * SUCH DAMAGE.
* *
* $Id: ContikiBeeper.java,v 1.9 2009/02/25 14:46:24 fros4943 Exp $ * $Id: ContikiBeeper.java,v 1.10 2009/05/26 14:24:20 fros4943 Exp $
*/ */
package se.sics.cooja.contikimote.interfaces; package se.sics.cooja.contikimote.interfaces;
@ -101,7 +101,7 @@ public class ContikiBeeper extends Beeper implements ContikiMoteInterface, Polle
return new String[]{"beep_interface"}; return new String[]{"beep_interface"};
} }
private TimeEvent stopBeepEvent = new TimeEvent(0) { private TimeEvent stopBeepEvent = new MoteTimeEvent(mote, 0) {
public void execute(long t) { public void execute(long t) {
myEnergyConsumption = 0.0; myEnergyConsumption = 0.0;
} }
@ -117,7 +117,7 @@ public class ContikiBeeper extends Beeper implements ContikiMoteInterface, Polle
moteMem.setByteValueOf("simBeeped", (byte) 0); moteMem.setByteValueOf("simBeeped", (byte) 0);
/* Schedule stop beeping (reset energy consumption) */ /* Schedule stop beeping (reset energy consumption) */
mote.getSimulation().scheduleEvent(stopBeepEvent, mote.getSimulation().getSimulationTime()); mote.getSimulation().scheduleEvent(stopBeepEvent, mote.getSimulation().getSimulationTime()+Simulation.MILLISECOND);
} }
} }

View file

@ -26,7 +26,7 @@
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE. * SUCH DAMAGE.
* *
* $Id: ContikiButton.java,v 1.12 2009/02/25 14:46:24 fros4943 Exp $ * $Id: ContikiButton.java,v 1.13 2009/05/26 14:24:20 fros4943 Exp $
*/ */
package se.sics.cooja.contikimote.interfaces; package se.sics.cooja.contikimote.interfaces;
@ -38,6 +38,7 @@ import org.apache.log4j.Logger;
import org.jdom.Element; import org.jdom.Element;
import se.sics.cooja.*; import se.sics.cooja.*;
import se.sics.cooja.contikimote.ContikiMote;
import se.sics.cooja.contikimote.ContikiMoteInterface; import se.sics.cooja.contikimote.ContikiMoteInterface;
import se.sics.cooja.interfaces.Button; import se.sics.cooja.interfaces.Button;
@ -63,20 +64,19 @@ import se.sics.cooja.interfaces.Button;
*/ */
public class ContikiButton extends Button implements ContikiMoteInterface { public class ContikiButton extends Button implements ContikiMoteInterface {
private SectionMoteMemory moteMem; private SectionMoteMemory moteMem;
private Mote mote; private ContikiMote mote;
private static Logger logger = Logger.getLogger(ContikiButton.class); private static Logger logger = Logger.getLogger(ContikiButton.class);
/** /**
* Creates an interface to the button at mote. * Creates an interface to the button at mote.
* *
* @param mote * @param mote Mote
* Button's mote.
* @see Mote * @see Mote
* @see se.sics.cooja.MoteInterfaceHandler * @see se.sics.cooja.MoteInterfaceHandler
*/ */
public ContikiButton(Mote mote) { public ContikiButton(Mote mote) {
this.mote = mote; this.mote = (ContikiMote) mote;
this.moteMem = (SectionMoteMemory) mote.getMemory(); this.moteMem = (SectionMoteMemory) mote.getMemory();
} }
@ -84,20 +84,23 @@ public class ContikiButton extends Button implements ContikiMoteInterface {
return new String[]{"button_interface"}; return new String[]{"button_interface"};
} }
private TimeEvent releaseButtonEvent = new TimeEvent(0) { private TimeEvent pressButtonEvent = new MoteTimeEvent(mote, 0) {
public void execute(long t) { public void execute(long t) {
doPressButton();
}
};
/* Force mote awake when button is down */ private TimeEvent releaseButtonEvent = new MoteTimeEvent(mote, 0) {
mote.setState(Mote.State.ACTIVE); public void execute(long t) {
/* Wait until button change is handled by Contiki */ /* Wait until button change is handled by Contiki */
if (moteMem.getByteValueOf("simButtonChanged") == 0) { if (moteMem.getByteValueOf("simButtonChanged") != 0) {
/*logger.info("Releasing button at: " + t);*/ /* Postpone button release */
releaseButton(); mote.getSimulation().scheduleEvent(releaseButtonEvent, t + Simulation.MILLISECOND);
} else { return;
/* Reschedule button release */
mote.getSimulation().scheduleEvent(releaseButtonEvent, t+1);
} }
/*logger.info("Releasing button at: " + t);*/
doReleaseButton();
} }
}; };
@ -105,34 +108,40 @@ public class ContikiButton extends Button implements ContikiMoteInterface {
* Clicks button: Presses and immediately releases button. * Clicks button: Presses and immediately releases button.
*/ */
public void clickButton() { public void clickButton() {
pressButton(); mote.getSimulation().scheduleEvent(pressButtonEvent, mote.getSimulation().getSimulationTime());
mote.getSimulation().scheduleEvent(releaseButtonEvent, mote.getSimulation().getSimulationTime() + Simulation.MILLISECOND);
}
/* Schedule release button */ public void pressButton() {
mote.getSimulation().scheduleEvent(releaseButtonEvent, mote.getSimulation().getSimulationTime()); mote.getSimulation().scheduleEvent(pressButtonEvent, mote.getSimulation().getSimulationTime());
} }
public void releaseButton() { public void releaseButton() {
mote.getSimulation().scheduleEvent(releaseButtonEvent, mote.getSimulation().getSimulationTime());
}
private void doReleaseButton() {
moteMem.setByteValueOf("simButtonIsDown", (byte) 0); moteMem.setByteValueOf("simButtonIsDown", (byte) 0);
if (moteMem.getByteValueOf("simButtonIsActive") == 1) { if (moteMem.getByteValueOf("simButtonIsActive") == 1) {
moteMem.setByteValueOf("simButtonChanged", (byte) 1); moteMem.setByteValueOf("simButtonChanged", (byte) 1);
/* If mote is inactive, wake it up */ /* If mote is inactive, wake it up */
mote.setState(Mote.State.ACTIVE); mote.scheduleImmediateWakeup();
setChanged(); setChanged();
notifyObservers(); notifyObservers();
} }
} }
public void pressButton() { private void doPressButton() {
moteMem.setByteValueOf("simButtonIsDown", (byte) 1); moteMem.setByteValueOf("simButtonIsDown", (byte) 1);
if (moteMem.getByteValueOf("simButtonIsActive") == 1) { if (moteMem.getByteValueOf("simButtonIsActive") == 1) {
moteMem.setByteValueOf("simButtonChanged", (byte) 1); moteMem.setByteValueOf("simButtonChanged", (byte) 1);
/* If mote is inactive, wake it up */ /* If mote is inactive, wake it up */
mote.setState(Mote.State.ACTIVE); mote.scheduleImmediateWakeup();
setChanged(); setChanged();
notifyObservers(); notifyObservers();

View file

@ -26,7 +26,7 @@
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE. * SUCH DAMAGE.
* *
* $Id: ContikiCFS.java,v 1.9 2009/02/25 14:46:24 fros4943 Exp $ * $Id: ContikiCFS.java,v 1.10 2009/05/26 14:24:20 fros4943 Exp $
*/ */
package se.sics.cooja.contikimote.interfaces; package se.sics.cooja.contikimote.interfaces;
@ -84,8 +84,6 @@ public class ContikiCFS extends MoteInterface implements ContikiMoteInterface, P
public final double ENERGY_CONSUMPTION_PER_READ_CHAR_mQ; public final double ENERGY_CONSUMPTION_PER_READ_CHAR_mQ;
public final double ENERGY_CONSUMPTION_PER_WRITTEN_CHAR_mQ; public final double ENERGY_CONSUMPTION_PER_WRITTEN_CHAR_mQ;
private double myEnergyConsumption = 0.0;
/** /**
* Creates an interface to the filesystem at mote. * Creates an interface to the filesystem at mote.
* *
@ -109,12 +107,6 @@ public class ContikiCFS extends MoteInterface implements ContikiMoteInterface, P
return new String[]{"cfs_interface"}; return new String[]{"cfs_interface"};
} }
private TimeEvent doneEvent = new TimeEvent(0) {
public void execute(long t) {
myEnergyConsumption = 0.0;
}
};
public void doActionsAfterTick() { public void doActionsAfterTick() {
if (moteMem.getByteValueOf("simCFSChanged") == 1) { if (moteMem.getByteValueOf("simCFSChanged") == 1) {
lastRead = moteMem.getIntValueOf("simCFSRead"); lastRead = moteMem.getIntValueOf("simCFSRead");
@ -124,15 +116,8 @@ public class ContikiCFS extends MoteInterface implements ContikiMoteInterface, P
moteMem.setIntValueOf("simCFSWritten", 0); moteMem.setIntValueOf("simCFSWritten", 0);
moteMem.setByteValueOf("simCFSChanged", (byte) 0); moteMem.setByteValueOf("simCFSChanged", (byte) 0);
myEnergyConsumption =
ENERGY_CONSUMPTION_PER_READ_CHAR_mQ*lastRead +
ENERGY_CONSUMPTION_PER_WRITTEN_CHAR_mQ*lastWritten;
this.setChanged(); this.setChanged();
this.notifyObservers(mote); this.notifyObservers(mote);
/* Reset energy consumption */
mote.getSimulation().scheduleEvent(doneEvent, mote.getSimulation().getSimulationTime());
} }
} }
@ -230,7 +215,7 @@ public class ContikiCFS extends MoteInterface implements ContikiMoteInterface, P
} }
public double energyConsumption() { public double energyConsumption() {
return myEnergyConsumption; return 0.0;
} }
public Collection<Element> getConfigXML() { public Collection<Element> getConfigXML() {

View file

@ -26,18 +26,23 @@
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE. * SUCH DAMAGE.
* *
* $Id: ContikiClock.java,v 1.8 2009/02/25 14:46:24 fros4943 Exp $ * $Id: ContikiClock.java,v 1.9 2009/05/26 14:24:20 fros4943 Exp $
*/ */
package se.sics.cooja.contikimote.interfaces; package se.sics.cooja.contikimote.interfaces;
import java.util.Collection; import java.util.Collection;
import javax.swing.JPanel; import javax.swing.JPanel;
import org.apache.log4j.Logger;
import org.jdom.Element; import org.jdom.Element;
import se.sics.cooja.*; import se.sics.cooja.*;
import se.sics.cooja.Mote.State;
import se.sics.cooja.contikimote.ContikiMote;
import se.sics.cooja.contikimote.ContikiMoteInterface; import se.sics.cooja.contikimote.ContikiMoteInterface;
import se.sics.cooja.interfaces.Clock; import se.sics.cooja.interfaces.Clock;
import se.sics.cooja.interfaces.PolledAfterAllTicks;
import se.sics.cooja.interfaces.PolledBeforeActiveTicks; import se.sics.cooja.interfaces.PolledBeforeActiveTicks;
/** /**
@ -45,7 +50,10 @@ import se.sics.cooja.interfaces.PolledBeforeActiveTicks;
* *
* Contiki variables: * Contiki variables:
* <ul> * <ul>
* <li>int simCurrentTime * <li>clock_time_t simCurrentTime
* <li>clock_time_t simNextExpirationTime
* <li>int simProcessRunValue
* <li>int simEtimerPending
* </ul> * </ul>
* *
* Core interface: * Core interface:
@ -58,13 +66,16 @@ import se.sics.cooja.interfaces.PolledBeforeActiveTicks;
* *
* @author Fredrik Osterlind * @author Fredrik Osterlind
*/ */
public class ContikiClock extends Clock implements ContikiMoteInterface, PolledBeforeActiveTicks { public class ContikiClock extends Clock implements ContikiMoteInterface, PolledBeforeActiveTicks, PolledAfterAllTicks {
private static Logger logger = Logger.getLogger(ContikiClock.class);
private Mote mote = null; private Simulation simulation;
private SectionMoteMemory moteMem = null; private ContikiMote mote;
private SectionMoteMemory moteMem;
private int timeDrift = 0;
private long moteTime; /* Microseconds */
private long timeDrift; /* Microseconds */
/** /**
* @param mote Mote * @param mote Mote
* *
@ -72,8 +83,11 @@ public class ContikiClock extends Clock implements ContikiMoteInterface, PolledB
* @see se.sics.cooja.MoteInterfaceHandler * @see se.sics.cooja.MoteInterfaceHandler
*/ */
public ContikiClock(Mote mote) { public ContikiClock(Mote mote) {
this.mote = mote; this.simulation = mote.getSimulation();
this.mote = (ContikiMote) mote;
this.moteMem = (SectionMoteMemory) mote.getMemory(); this.moteMem = (SectionMoteMemory) mote.getMemory();
timeDrift = 0;
moteTime = 0;
} }
public static String[] getCoreInterfaceDependencies() { public static String[] getCoreInterfaceDependencies() {
@ -81,30 +95,51 @@ public class ContikiClock extends Clock implements ContikiMoteInterface, PolledB
} }
public void setTime(long newTime) { public void setTime(long newTime) {
/* TODO: check if this is correct even if newTime > MAX_INT */ moteTime = newTime;
moteMem.setIntValueOf("simCurrentTime", (int)newTime); if (moteTime > 0) {
moteMem.setIntValueOf("simCurrentTime", (int)(newTime/1000));
}
} }
public void setDrift(int timeDrift) { public void setDrift(long drift) {
this.timeDrift = timeDrift; this.timeDrift = drift - (drift % 1000); /* Round to ms */
setTime(timeDrift);
} }
public int getDrift() { public long getDrift() {
return timeDrift; return timeDrift;
} }
public long getTime() { public long getTime() {
return moteMem.getIntValueOf("simCurrentTime"); return moteTime;
} }
public void doActionsBeforeTick() { public void doActionsBeforeTick() {
/* Update time */ /* Update time */
long moteTime = mote.getSimulation().getSimulationTime() + timeDrift; setTime(mote.getSimulation().getSimulationTime() + timeDrift);
if (moteTime > 0) {
setTime(moteTime);
}
} }
public void doActionsAfterTick() {
/* Request next tick for remaining events / timers */
int processRunValue = moteMem.getIntValueOf("simProcessRunValue");
if (processRunValue != 0) {
/* Handle next Contiki event in one millisecond */
mote.scheduleNextWakeup(simulation.getSimulationTime() + 1000L);
return;
}
int etimersPending = moteMem.getIntValueOf("simEtimerPending");
if (etimersPending == 0) {
/* No timers */
return;
}
/* Request tick next wakeup time */
int nextExpirationTime = moteMem.getIntValueOf("simNextExpirationTime");
mote.scheduleNextWakeup(simulation.getSimulationTime() + 1000L*nextExpirationTime);
}
public JPanel getInterfaceVisualizer() { public JPanel getInterfaceVisualizer() {
return null; return null;

View file

@ -26,7 +26,7 @@
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE. * SUCH DAMAGE.
* *
* $Id: ContikiPIR.java,v 1.6 2009/02/25 14:46:24 fros4943 Exp $ * $Id: ContikiPIR.java,v 1.7 2009/05/26 14:24:20 fros4943 Exp $
*/ */
package se.sics.cooja.contikimote.interfaces; package se.sics.cooja.contikimote.interfaces;
@ -38,6 +38,7 @@ import org.apache.log4j.Logger;
import org.jdom.Element; import org.jdom.Element;
import se.sics.cooja.*; import se.sics.cooja.*;
import se.sics.cooja.contikimote.ContikiMote;
import se.sics.cooja.contikimote.ContikiMoteInterface; import se.sics.cooja.contikimote.ContikiMoteInterface;
import se.sics.cooja.interfaces.PIR; import se.sics.cooja.interfaces.PIR;
@ -72,7 +73,7 @@ public class ContikiPIR extends PIR implements ContikiMoteInterface {
private double energyActivePerTick = -1; private double energyActivePerTick = -1;
private Mote mote; private ContikiMote mote;
private SectionMoteMemory moteMem; private SectionMoteMemory moteMem;
private double myEnergyConsumption = 0.0; private double myEnergyConsumption = 0.0;
@ -89,7 +90,7 @@ public class ContikiPIR extends PIR implements ContikiMoteInterface {
ENERGY_CONSUMPTION_PIR_mA = mote.getType().getConfig().getDoubleValue( ENERGY_CONSUMPTION_PIR_mA = mote.getType().getConfig().getDoubleValue(
ContikiPIR.class, "ACTIVE_CONSUMPTION_mA"); ContikiPIR.class, "ACTIVE_CONSUMPTION_mA");
this.mote = mote; this.mote = (ContikiMote) mote;
this.moteMem = (SectionMoteMemory) mote.getMemory(); this.moteMem = (SectionMoteMemory) mote.getMemory();
if (energyActivePerTick < 0) { if (energyActivePerTick < 0) {
@ -101,14 +102,24 @@ public class ContikiPIR extends PIR implements ContikiMoteInterface {
return new String[]{"pir_interface"}; return new String[]{"pir_interface"};
} }
public void triggerChange() { /**
* Simulates a change in the PIR sensor.
*/
public void triggerChange() {
mote.getSimulation().scheduleEvent(pirEvent, mote.getSimulation().getSimulationTime());
}
private TimeEvent pirEvent = new MoteTimeEvent(mote, 0) {
public void execute(long t) {
doTriggerChange();
}
};
public void doTriggerChange() {
if (moteMem.getByteValueOf("simPirIsActive") == 1) { if (moteMem.getByteValueOf("simPirIsActive") == 1) {
moteMem.setByteValueOf("simPirChanged", (byte) 1); moteMem.setByteValueOf("simPirChanged", (byte) 1);
mote.setState(Mote.State.ACTIVE); mote.scheduleImmediateWakeup();
this.setChanged();
this.notifyObservers();
} }
} }

View file

@ -26,7 +26,7 @@
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE. * SUCH DAMAGE.
* *
* $Id: ContikiRS232.java,v 1.8 2009/04/23 09:17:01 fros4943 Exp $ * $Id: ContikiRS232.java,v 1.9 2009/05/26 14:24:20 fros4943 Exp $
*/ */
package se.sics.cooja.contikimote.interfaces; package se.sics.cooja.contikimote.interfaces;
@ -35,6 +35,7 @@ import java.util.Vector;
import org.apache.log4j.Logger; import org.apache.log4j.Logger;
import se.sics.cooja.*; import se.sics.cooja.*;
import se.sics.cooja.contikimote.ContikiMote;
import se.sics.cooja.contikimote.ContikiMoteInterface; import se.sics.cooja.contikimote.ContikiMoteInterface;
import se.sics.cooja.dialogs.SerialUI; import se.sics.cooja.dialogs.SerialUI;
import se.sics.cooja.interfaces.PolledAfterActiveTicks; import se.sics.cooja.interfaces.PolledAfterActiveTicks;
@ -67,7 +68,7 @@ import se.sics.cooja.interfaces.PolledAfterActiveTicks;
public class ContikiRS232 extends SerialUI implements ContikiMoteInterface, PolledAfterActiveTicks { public class ContikiRS232 extends SerialUI implements ContikiMoteInterface, PolledAfterActiveTicks {
private static Logger logger = Logger.getLogger(ContikiRS232.class); private static Logger logger = Logger.getLogger(ContikiRS232.class);
private Mote mote = null; private ContikiMote mote = null;
private SectionMoteMemory moteMem = null; private SectionMoteMemory moteMem = null;
/** /**
@ -89,7 +90,7 @@ public class ContikiRS232 extends SerialUI implements ContikiMoteInterface, Poll
ENERGY_CONSUMPTION_PER_CHAR_mQ = ENERGY_CONSUMPTION_PER_CHAR_mQ =
mote.getType().getConfig().getDoubleValue(ContikiRS232.class, "CONSUMPTION_PER_CHAR_mQ"); mote.getType().getConfig().getDoubleValue(ContikiRS232.class, "CONSUMPTION_PER_CHAR_mQ");
this.mote = mote; this.mote = (ContikiMote) mote;
this.moteMem = (SectionMoteMemory) mote.getMemory(); this.moteMem = (SectionMoteMemory) mote.getMemory();
} }
@ -118,7 +119,7 @@ public class ContikiRS232 extends SerialUI implements ContikiMoteInterface, Poll
public void writeString(String message) { public void writeString(String message) {
final byte[] dataToAppend = message.getBytes(); final byte[] dataToAppend = message.getBytes();
TimeEvent writeStringEvent = new TimeEvent(0) { TimeEvent writeStringEvent = new MoteTimeEvent(mote, 0) {
public void execute(long t) { public void execute(long t) {
/* Append to existing buffer */ /* Append to existing buffer */
int oldSize = moteMem.getIntValueOf("simSerialReceivingLength"); int oldSize = moteMem.getIntValueOf("simSerialReceivingLength");
@ -134,7 +135,7 @@ public class ContikiRS232 extends SerialUI implements ContikiMoteInterface, Poll
moteMem.setByteArray("simSerialReceivingData", newData); moteMem.setByteArray("simSerialReceivingData", newData);
moteMem.setByteValueOf("simSerialReceivingFlag", (byte) 1); moteMem.setByteValueOf("simSerialReceivingFlag", (byte) 1);
mote.setState(Mote.State.ACTIVE); mote.scheduleImmediateWakeup();
} }
}; };
mote.getSimulation().scheduleEvent( mote.getSimulation().scheduleEvent(
@ -158,13 +159,13 @@ public class ContikiRS232 extends SerialUI implements ContikiMoteInterface, Poll
pendingBytes.add(b); pendingBytes.add(b);
} }
mote.setState(Mote.State.ACTIVE); mote.scheduleImmediateWakeup();
if (pendingBytesEvent != null) { if (pendingBytesEvent != null) {
/* Event is already scheduled, no need to reschedule */ /* Event is already scheduled, no need to reschedule */
return; return;
} }
pendingBytesEvent = new TimeEvent(0) { pendingBytesEvent = new MoteTimeEvent(mote, 0) {
public void execute(long t) { public void execute(long t) {
ContikiRS232.this.pendingBytesEvent = null; ContikiRS232.this.pendingBytesEvent = null;
if (pendingBytes.isEmpty()) { if (pendingBytes.isEmpty()) {
@ -207,13 +208,13 @@ public class ContikiRS232 extends SerialUI implements ContikiMoteInterface, Poll
public void writeByte(final byte b) { public void writeByte(final byte b) {
pendingBytes.add(b); pendingBytes.add(b);
mote.setState(Mote.State.ACTIVE); mote.scheduleImmediateWakeup();
if (pendingBytesEvent != null) { if (pendingBytesEvent != null) {
/* Event is already scheduled, no need to reschedule */ /* Event is already scheduled, no need to reschedule */
return; return;
} }
pendingBytesEvent = new TimeEvent(0) { pendingBytesEvent = new MoteTimeEvent(mote, 0) {
public void execute(long t) { public void execute(long t) {
ContikiRS232.this.pendingBytesEvent = null; ContikiRS232.this.pendingBytesEvent = null;
if (pendingBytes.isEmpty()) { if (pendingBytes.isEmpty()) {

View file

@ -26,7 +26,7 @@
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE. * SUCH DAMAGE.
* *
* $Id: ContikiRadio.java,v 1.27 2009/04/16 14:26:35 fros4943 Exp $ * $Id: ContikiRadio.java,v 1.28 2009/05/26 14:24:20 fros4943 Exp $
*/ */
package se.sics.cooja.contikimote.interfaces; package se.sics.cooja.contikimote.interfaces;
@ -40,6 +40,7 @@ import org.apache.log4j.Logger;
import org.jdom.Element; import org.jdom.Element;
import se.sics.cooja.*; import se.sics.cooja.*;
import se.sics.cooja.contikimote.ContikiMote;
import se.sics.cooja.contikimote.ContikiMoteInterface; import se.sics.cooja.contikimote.ContikiMoteInterface;
import se.sics.cooja.interfaces.PolledAfterActiveTicks; import se.sics.cooja.interfaces.PolledAfterActiveTicks;
import se.sics.cooja.interfaces.Position; import se.sics.cooja.interfaces.Position;
@ -86,7 +87,7 @@ import se.sics.cooja.radiomediums.UDGM;
* @author Fredrik Osterlind * @author Fredrik Osterlind
*/ */
public class ContikiRadio extends Radio implements ContikiMoteInterface, PolledAfterActiveTicks { public class ContikiRadio extends Radio implements ContikiMoteInterface, PolledAfterActiveTicks {
private Mote myMote; private ContikiMote mote;
private SectionMoteMemory myMoteMemory; private SectionMoteMemory myMoteMemory;
@ -146,7 +147,7 @@ public class ContikiRadio extends Radio implements ContikiMoteInterface, PolledA
RADIO_TRANSMISSION_RATE_kbps = mote.getType().getConfig().getDoubleValue( RADIO_TRANSMISSION_RATE_kbps = mote.getType().getConfig().getDoubleValue(
ContikiRadio.class, "RADIO_TRANSMISSION_RATE_kbps"); ContikiRadio.class, "RADIO_TRANSMISSION_RATE_kbps");
this.myMote = mote; this.mote = (ContikiMote) mote;
this.myMoteMemory = (SectionMoteMemory) mote.getMemory(); this.myMoteMemory = (SectionMoteMemory) mote.getMemory();
// Calculate energy consumption of a listening radio // Calculate energy consumption of a listening radio
@ -185,11 +186,7 @@ public class ContikiRadio extends Radio implements ContikiMoteInterface, PolledA
} }
public boolean isReceiving() { public boolean isReceiving() {
if (isLockedAtReceiving()) { return isLockedAtReceiving();
return true;
}
return myMoteMemory.getIntValueOf("simInSize") != 0;
} }
public boolean isInterfered() { public boolean isInterfered() {
@ -208,7 +205,7 @@ public class ContikiRadio extends Radio implements ContikiMoteInterface, PolledA
} }
lockInReceivingMode(); lockInReceivingMode();
lastEventTime = myMote.getSimulation().getSimulationTime(); lastEventTime = mote.getSimulation().getSimulationTime();
lastEvent = RadioEvent.RECEPTION_STARTED; lastEvent = RadioEvent.RECEPTION_STARTED;
this.setChanged(); this.setChanged();
this.notifyObservers(); this.notifyObservers();
@ -226,6 +223,12 @@ public class ContikiRadio extends Radio implements ContikiMoteInterface, PolledA
// Unlock (if locked) // Unlock (if locked)
myMoteMemory.setByteValueOf("simReceiving", (byte) 0); myMoteMemory.setByteValueOf("simReceiving", (byte) 0);
mote.scheduleImmediateWakeup();
lastEventTime = mote.getSimulation().getSimulationTime();
lastEvent = RadioEvent.RECEPTION_FINISHED;
this.setChanged();
this.notifyObservers();
return; return;
} }
@ -236,8 +239,9 @@ public class ContikiRadio extends Radio implements ContikiMoteInterface, PolledA
myMoteMemory.setIntValueOf("simInSize", packetToMote.getPacketData().length); myMoteMemory.setIntValueOf("simInSize", packetToMote.getPacketData().length);
myMoteMemory.setByteArray("simInDataBuffer", packetToMote.getPacketData()); myMoteMemory.setByteArray("simInDataBuffer", packetToMote.getPacketData());
lastEventTime = myMote.getSimulation().getSimulationTime(); lastEventTime = mote.getSimulation().getSimulationTime();
lastEvent = RadioEvent.RECEPTION_FINISHED; lastEvent = RadioEvent.RECEPTION_FINISHED;
mote.scheduleImmediateWakeup();
this.setChanged(); this.setChanged();
this.notifyObservers(); this.notifyObservers();
} }
@ -251,7 +255,7 @@ public class ContikiRadio extends Radio implements ContikiMoteInterface, PolledA
isInterfered = true; isInterfered = true;
lastEvent = RadioEvent.RECEPTION_INTERFERED; lastEvent = RadioEvent.RECEPTION_INTERFERED;
lastEventTime = myMote.getSimulation().getSimulationTime(); lastEventTime = mote.getSimulation().getSimulationTime();
this.setChanged(); this.setChanged();
this.notifyObservers(); this.notifyObservers();
} }
@ -280,7 +284,7 @@ public class ContikiRadio extends Radio implements ContikiMoteInterface, PolledA
} }
public Position getPosition() { public Position getPosition() {
return myMote.getInterfaces().getPosition(); return mote.getInterfaces().getPosition();
} }
/** /**
@ -305,11 +309,11 @@ public class ContikiRadio extends Radio implements ContikiMoteInterface, PolledA
*/ */
private void lockInReceivingMode() { private void lockInReceivingMode() {
// If mote is inactive, try to wake it up // If mote is inactive, try to wake it up
if (myMote.getState() != Mote.State.ACTIVE) { if (mote.getState() != Mote.State.ACTIVE) {
if (RAISES_EXTERNAL_INTERRUPT) { if (RAISES_EXTERNAL_INTERRUPT) {
myMote.setState(Mote.State.ACTIVE); mote.scheduleImmediateWakeup();
} }
if (myMote.getState() != Mote.State.ACTIVE) { if (mote.getState() != Mote.State.ACTIVE) {
return; return;
} }
} }
@ -335,7 +339,7 @@ public class ContikiRadio extends Radio implements ContikiMoteInterface, PolledA
lastEvent = RadioEvent.HW_ON; lastEvent = RadioEvent.HW_ON;
} }
lastEventTime = myMote.getSimulation().getSimulationTime(); lastEventTime = mote.getSimulation().getSimulationTime();
this.setChanged(); this.setChanged();
this.notifyObservers(); this.notifyObservers();
} }
@ -356,12 +360,12 @@ public class ContikiRadio extends Radio implements ContikiMoteInterface, PolledA
/* Are we transmitting but should stop? */ /* Are we transmitting but should stop? */
/* TODO Use time events */ /* TODO Use time events */
if (isTransmitting if (isTransmitting
&& myMote.getSimulation().getSimulationTime() >= transmissionEndTime) { && mote.getSimulation().getSimulationTime() >= transmissionEndTime) {
myMoteMemory.setByteValueOf("simTransmitting", (byte) 0); myMoteMemory.setByteValueOf("simTransmitting", (byte) 0);
myMoteMemory.setIntValueOf("simOutSize", 0); myMoteMemory.setIntValueOf("simOutSize", 0);
isTransmitting = false; isTransmitting = false;
lastEventTime = myMote.getSimulation().getSimulationTime(); lastEventTime = mote.getSimulation().getSimulationTime();
lastEvent = RadioEvent.TRANSMISSION_FINISHED; lastEvent = RadioEvent.TRANSMISSION_FINISHED;
// TODO Energy consumption of transmitted packet? // TODO Energy consumption of transmitted packet?
this.setChanged(); this.setChanged();
@ -386,10 +390,10 @@ public class ContikiRadio extends Radio implements ContikiMoteInterface, PolledA
isTransmitting = true; isTransmitting = true;
// Calculate transmission duration (ms) // Calculate transmission duration (us)
int duration = (int) ((280 + 10 * size) / RADIO_TRANSMISSION_RATE_kbps); long duration = (int) (Simulation.MILLISECOND*((280 + 10 * size) / RADIO_TRANSMISSION_RATE_kbps));
transmissionEndTime = myMote.getSimulation().getSimulationTime() + Math.max(1, duration); transmissionEndTime = mote.getSimulation().getSimulationTime() + Math.max(1, duration);
lastEventTime = myMote.getSimulation().getSimulationTime(); lastEventTime = mote.getSimulation().getSimulationTime();
lastEvent = RadioEvent.TRANSMISSION_STARTED; lastEvent = RadioEvent.TRANSMISSION_STARTED;
this.setChanged(); this.setChanged();
@ -494,10 +498,10 @@ public class ContikiRadio extends Radio implements ContikiMoteInterface, PolledA
} }
public Mote getMote() { public Mote getMote() {
return myMote; return mote;
} }
public String toString() { public String toString() {
return "Radio at " + myMote; return "Radio at " + mote;
} }
} }

View file

@ -26,7 +26,7 @@
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE. * SUCH DAMAGE.
* *
* $Id: ContikiVib.java,v 1.6 2009/02/25 14:46:24 fros4943 Exp $ * $Id: ContikiVib.java,v 1.7 2009/05/26 14:24:20 fros4943 Exp $
*/ */
package se.sics.cooja.contikimote.interfaces; package se.sics.cooja.contikimote.interfaces;
@ -38,6 +38,7 @@ import org.apache.log4j.Logger;
import org.jdom.Element; import org.jdom.Element;
import se.sics.cooja.*; import se.sics.cooja.*;
import se.sics.cooja.contikimote.ContikiMote;
import se.sics.cooja.contikimote.ContikiMoteInterface; import se.sics.cooja.contikimote.ContikiMoteInterface;
/** /**
@ -72,7 +73,7 @@ public class ContikiVib extends MoteInterface implements ContikiMoteInterface {
private double energyActiveVibPerTick = -1; private double energyActiveVibPerTick = -1;
private Mote mote; private ContikiMote mote;
private SectionMoteMemory moteMem; private SectionMoteMemory moteMem;
private double myEnergyConsumption = 0.0; private double myEnergyConsumption = 0.0;
@ -89,7 +90,7 @@ public class ContikiVib extends MoteInterface implements ContikiMoteInterface {
ENERGY_CONSUMPTION_VIB_mA = mote.getType().getConfig().getDoubleValue( ENERGY_CONSUMPTION_VIB_mA = mote.getType().getConfig().getDoubleValue(
ContikiVib.class, "ACTIVE_CONSUMPTION_mA"); ContikiVib.class, "ACTIVE_CONSUMPTION_mA");
this.mote = mote; this.mote = (ContikiMote) mote;
this.moteMem = (SectionMoteMemory) mote.getMemory(); this.moteMem = (SectionMoteMemory) mote.getMemory();
if (energyActiveVibPerTick < 0) { if (energyActiveVibPerTick < 0) {
@ -105,10 +106,20 @@ public class ContikiVib extends MoteInterface implements ContikiMoteInterface {
* Simulates a change in the vibration sensor. * Simulates a change in the vibration sensor.
*/ */
public void triggerChange() { public void triggerChange() {
mote.getSimulation().scheduleEvent(vibrateEvent, mote.getSimulation().getSimulationTime());
}
private TimeEvent vibrateEvent = new MoteTimeEvent(mote, 0) {
public void execute(long t) {
doTriggerChange();
}
};
public void doTriggerChange() {
if (moteMem.getByteValueOf("simVibIsActive") == 1) { if (moteMem.getByteValueOf("simVibIsActive") == 1) {
moteMem.setByteValueOf("simVibChanged", (byte) 1); moteMem.setByteValueOf("simVibChanged", (byte) 1);
mote.setState(Mote.State.ACTIVE); mote.scheduleImmediateWakeup();
} }
} }

View file

@ -26,7 +26,7 @@
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE. * SUCH DAMAGE.
* *
* $Id: Battery.java,v 1.7 2009/03/09 14:08:54 fros4943 Exp $ * $Id: Battery.java,v 1.8 2009/05/26 14:24:20 fros4943 Exp $
*/ */
package se.sics.cooja.interfaces; package se.sics.cooja.interfaces;
@ -37,6 +37,7 @@ import org.apache.log4j.Logger;
import org.jdom.Element; import org.jdom.Element;
import se.sics.cooja.*; import se.sics.cooja.*;
import se.sics.cooja.contikimote.ContikiMote;
/** /**
* A Battery represents the energy source for a mote. This implementation has no * A Battery represents the energy source for a mote. This implementation has no
@ -79,7 +80,7 @@ public class Battery extends MoteInterface implements PolledAfterAllTicks {
*/ */
public final double INITIAL_ENERGY; public final double INITIAL_ENERGY;
private Mote mote = null; private ContikiMote mote = null;
private double cpuEnergyConsumptionLPMPerMs; private double cpuEnergyConsumptionLPMPerMs;
private double cpuEnergyConsumptionAwakePerMs; private double cpuEnergyConsumptionAwakePerMs;
@ -108,7 +109,7 @@ public class Battery extends MoteInterface implements PolledAfterAllTicks {
cpuEnergyConsumptionAwakePerMs = CPU_ENERGY_CONSUMPTION_AWAKE_mA * 0.001; /* TODO Voltage */ cpuEnergyConsumptionAwakePerMs = CPU_ENERGY_CONSUMPTION_AWAKE_mA * 0.001; /* TODO Voltage */
cpuEnergyConsumptionLPMPerMs = CPU_ENERGY_CONSUMPTION_LPM_mA * 0.001; /* TODO Voltage */ cpuEnergyConsumptionLPMPerMs = CPU_ENERGY_CONSUMPTION_LPM_mA * 0.001; /* TODO Voltage */
this.mote = mote; this.mote = (ContikiMote) mote;
} }
public void doActionsAfterTick() { public void doActionsAfterTick() {
@ -135,7 +136,7 @@ public class Battery extends MoteInterface implements PolledAfterAllTicks {
/* Check if we are out of energy */ /* Check if we are out of energy */
if (getEnergyConsumption() > INITIAL_ENERGY) { if (getEnergyConsumption() > INITIAL_ENERGY) {
mote.setState(Mote.State.DEAD); mote.scheduleImmediateWakeup();
} }
setChanged(); setChanged();

View file

@ -26,7 +26,7 @@
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE. * SUCH DAMAGE.
* *
* $Id: Clock.java,v 1.5 2009/03/09 14:08:54 fros4943 Exp $ * $Id: Clock.java,v 1.6 2009/05/26 14:24:21 fros4943 Exp $
*/ */
package se.sics.cooja.interfaces; package se.sics.cooja.interfaces;
@ -61,11 +61,20 @@ public abstract class Clock extends MoteInterface {
* *
* @param timeDrift Time drift * @param timeDrift Time drift
*/ */
public abstract void setDrift(int timeDrift); public abstract void setDrift(long timeDrift);
/** /**
* The clock drift provides information about the mote's internal time,
* and can the used to calculate for instance its startup time.
*
* The startup time is the negative drift time.
*
* The mote internal time can be calculated by:
* [current simulation time] + [mote drift].
*
* @see Simulation#getSimulationTime()
* @return Time drift * @return Time drift
*/ */
public abstract int getDrift(); public abstract long getDrift();
} }

View file

@ -26,7 +26,7 @@
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE. * SUCH DAMAGE.
* *
* $Id: IPAddress.java,v 1.5 2009/04/28 07:29:26 fros4943 Exp $ * $Id: IPAddress.java,v 1.6 2009/05/26 14:24:21 fros4943 Exp $
*/ */
package se.sics.cooja.interfaces; package se.sics.cooja.interfaces;
@ -77,7 +77,7 @@ public class IPAddress extends MoteInterface {
/* Postpone until IP has been set */ /* Postpone until IP has been set */
mote.getSimulation().scheduleEvent( mote.getSimulation().scheduleEvent(
this, this,
mote.getSimulation().getSimulationTime() + 1); mote.getSimulation().getSimulationTime() + Simulation.MILLISECOND);
return; return;
} }
}; };

View file

@ -26,7 +26,7 @@
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE. * SUCH DAMAGE.
* *
* $Id: RimeAddress.java,v 1.2 2009/04/28 07:29:26 fros4943 Exp $ * $Id: RimeAddress.java,v 1.3 2009/05/26 14:24:21 fros4943 Exp $
*/ */
package se.sics.cooja.interfaces; package se.sics.cooja.interfaces;
@ -75,7 +75,7 @@ public class RimeAddress extends MoteInterface {
/* Postpone until address has been set */ /* Postpone until address has been set */
mote.getSimulation().scheduleEvent( mote.getSimulation().scheduleEvent(
this, this,
mote.getSimulation().getSimulationTime() + 1); mote.getSimulation().getSimulationTime() + Simulation.MILLISECOND);
return; return;
} }
}; };