guard amount of serial data being written to simulated contiki nodes

This commit is contained in:
Fredrik Osterlind 2013-08-14 12:57:17 +02:00
parent 1842fae903
commit 5b7cc56cc5

View File

@ -70,6 +70,8 @@ public class ContikiRS232 extends SerialUI implements ContikiMoteInterface, Poll
private ContikiMote mote = null;
private SectionMoteMemory moteMem = null;
static final int SERIAL_BUF_SIZE = 1024; /* rs232.c:40 */
/**
* Creates an interface to the RS232 at mote.
*
@ -109,6 +111,11 @@ public class ContikiRS232 extends SerialUI implements ContikiMoteInterface, Poll
/* Append to existing buffer */
int oldSize = moteMem.getIntValueOf("simSerialReceivingLength");
int newSize = oldSize + dataToAppend.length;
if (newSize > SERIAL_BUF_SIZE) {
logger.fatal("ContikiRS232: dropping rs232 data #1, buffer full: " + oldSize + " -> " + newSize);
mote.requestImmediateWakeup();
return;
}
moteMem.setIntValueOf("simSerialReceivingLength", newSize);
byte[] oldData = moteMem.getByteArray("simSerialReceivingData", oldSize);
@ -159,6 +166,11 @@ public class ContikiRS232 extends SerialUI implements ContikiMoteInterface, Poll
/* Append to existing buffer */
int oldSize = moteMem.getIntValueOf("simSerialReceivingLength");
int newSize = oldSize + dataToAppend.length;
if (newSize > SERIAL_BUF_SIZE) {
logger.fatal("ContikiRS232: dropping rs232 data #2, buffer full: " + oldSize + " -> " + newSize);
mote.requestImmediateWakeup();
return;
}
moteMem.setIntValueOf("simSerialReceivingLength", newSize);
byte[] oldData = moteMem.getByteArray("simSerialReceivingData", oldSize);
@ -212,6 +224,11 @@ public class ContikiRS232 extends SerialUI implements ContikiMoteInterface, Poll
/* Append to existing buffer */
int oldSize = moteMem.getIntValueOf("simSerialReceivingLength");
int newSize = oldSize + dataToAppend.length;
if (newSize > SERIAL_BUF_SIZE) {
logger.fatal("ContikiRS232: dropping rs232 data #3, buffer full: " + oldSize + " -> " + newSize);
mote.requestImmediateWakeup();
return;
}
moteMem.setIntValueOf("simSerialReceivingLength", newSize);
byte[] oldData = moteMem.getByteArray("simSerialReceivingData", oldSize);
@ -229,13 +246,26 @@ public class ContikiRS232 extends SerialUI implements ContikiMoteInterface, Poll
mote.requestImmediateWakeup();
}
};
/* Simulation thread: schedule immediately */
if (mote.getSimulation().isSimulationThread()) {
mote.getSimulation().scheduleEvent(
pendingBytesEvent,
mote.getSimulation().getSimulationTime()
);
return;
}
mote.getSimulation().invokeSimulationThread(new Runnable() {
public void run() {
mote.getSimulation().scheduleEvent(
pendingBytesEvent,
mote.getSimulation().getSimulationTime()
);
}
public void run() {
if (pendingBytesEvent.isScheduled()) {
return;
}
mote.getSimulation().scheduleEvent(
pendingBytesEvent,
mote.getSimulation().getSimulationTime()
);
}
});
}