implemented writeArray() and writeByte() for Contiki serial interface

This commit is contained in:
fros4943 2009-04-23 09:17:01 +00:00
parent 9f299c277e
commit 921ceabb50

View file

@ -26,11 +26,13 @@
* 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.7 2009/03/26 16:23:47 fros4943 Exp $ * $Id: ContikiRS232.java,v 1.8 2009/04/23 09:17:01 fros4943 Exp $
*/ */
package se.sics.cooja.contikimote.interfaces; package se.sics.cooja.contikimote.interfaces;
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.ContikiMoteInterface; import se.sics.cooja.contikimote.ContikiMoteInterface;
@ -149,12 +151,106 @@ public class ContikiRS232 extends SerialUI implements ContikiMoteInterface, Poll
return mote; return mote;
} }
private TimeEvent pendingBytesEvent = null;
private Vector<Byte> pendingBytes = new Vector<Byte>();
public void writeArray(byte[] s) { public void writeArray(byte[] s) {
logger.fatal("NOT IMPLEMENTED"); for (byte b: s) {
pendingBytes.add(b);
} }
public void writeByte(byte b) { mote.setState(Mote.State.ACTIVE);
logger.fatal("NOT IMPLEMENTED"); if (pendingBytesEvent != null) {
/* Event is already scheduled, no need to reschedule */
return;
}
pendingBytesEvent = new TimeEvent(0) {
public void execute(long t) {
ContikiRS232.this.pendingBytesEvent = null;
if (pendingBytes.isEmpty()) {
return;
}
/* Move bytes from synchronized vector to Contiki buffer */
int nrBytes = pendingBytes.size();
byte[] dataToAppend = new byte[nrBytes];
for (int i=0; i < nrBytes; i++) {
dataToAppend[i] = pendingBytes.firstElement();
pendingBytes.remove(0);
}
/* Append to existing buffer */
int oldSize = moteMem.getIntValueOf("simSerialReceivingLength");
int newSize = oldSize + dataToAppend.length;
moteMem.setIntValueOf("simSerialReceivingLength", newSize);
byte[] oldData = moteMem.getByteArray("simSerialReceivingData", oldSize);
byte[] newData = new byte[newSize];
System.arraycopy(oldData, 0, newData, 0, oldData.length);
System.arraycopy(dataToAppend, 0, newData, oldSize, dataToAppend.length);
moteMem.setByteArray("simSerialReceivingData", newData);
moteMem.setByteValueOf("simSerialReceivingFlag", (byte) 1);
/* Reschedule us if more bytes are available */
mote.getSimulation().scheduleEvent(this, t);
}
};
mote.getSimulation().scheduleEvent(
pendingBytesEvent,
mote.getSimulation().getSimulationTime()
);
}
public void writeByte(final byte b) {
pendingBytes.add(b);
mote.setState(Mote.State.ACTIVE);
if (pendingBytesEvent != null) {
/* Event is already scheduled, no need to reschedule */
return;
}
pendingBytesEvent = new TimeEvent(0) {
public void execute(long t) {
ContikiRS232.this.pendingBytesEvent = null;
if (pendingBytes.isEmpty()) {
return;
}
/* Move bytes from synchronized vector to Contiki buffer */
int nrBytes = pendingBytes.size();
byte[] dataToAppend = new byte[nrBytes];
for (int i=0; i < nrBytes; i++) {
dataToAppend[i] = pendingBytes.firstElement();
pendingBytes.remove(0);
}
/* Append to existing buffer */
int oldSize = moteMem.getIntValueOf("simSerialReceivingLength");
int newSize = oldSize + dataToAppend.length;
moteMem.setIntValueOf("simSerialReceivingLength", newSize);
byte[] oldData = moteMem.getByteArray("simSerialReceivingData", oldSize);
byte[] newData = new byte[newSize];
System.arraycopy(oldData, 0, newData, 0, oldData.length);
System.arraycopy(dataToAppend, 0, newData, oldSize, dataToAppend.length);
moteMem.setByteArray("simSerialReceivingData", newData);
moteMem.setByteValueOf("simSerialReceivingFlag", (byte) 1);
/* Reschedule us if more bytes are available */
mote.getSimulation().scheduleEvent(this, t);
}
};
mote.getSimulation().scheduleEvent(
pendingBytesEvent,
mote.getSimulation().getSimulationTime()
);
} }
} }