From 921ceabb50f2f757e81c7585ad390565108ed136 Mon Sep 17 00:00:00 2001 From: fros4943 Date: Thu, 23 Apr 2009 09:17:01 +0000 Subject: [PATCH] implemented writeArray() and writeByte() for Contiki serial interface --- .../contikimote/interfaces/ContikiRS232.java | 104 +++++++++++++++++- 1 file changed, 100 insertions(+), 4 deletions(-) diff --git a/tools/cooja/java/se/sics/cooja/contikimote/interfaces/ContikiRS232.java b/tools/cooja/java/se/sics/cooja/contikimote/interfaces/ContikiRS232.java index 6aec6f345..faa19e7bc 100644 --- a/tools/cooja/java/se/sics/cooja/contikimote/interfaces/ContikiRS232.java +++ b/tools/cooja/java/se/sics/cooja/contikimote/interfaces/ContikiRS232.java @@ -26,11 +26,13 @@ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * 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; +import java.util.Vector; + import org.apache.log4j.Logger; import se.sics.cooja.*; import se.sics.cooja.contikimote.ContikiMoteInterface; @@ -149,12 +151,106 @@ public class ContikiRS232 extends SerialUI implements ContikiMoteInterface, Poll return mote; } + private TimeEvent pendingBytesEvent = null; + private Vector pendingBytes = new Vector(); public void writeArray(byte[] s) { - logger.fatal("NOT IMPLEMENTED"); + for (byte b: s) { + 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() + ); } - public void writeByte(byte b) { - logger.fatal("NOT IMPLEMENTED"); + 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() + ); } }