From 5f1fda0406ca4b59aa154ee07f26fe02b621f776 Mon Sep 17 00:00:00 2001 From: fros4943 Date: Thu, 26 Mar 2009 16:23:47 +0000 Subject: [PATCH] removed unused variables + writing serial data in timeevent --- platform/cooja/dev/rs232.c | 52 ++++++++++--------- .../contikimote/interfaces/ContikiRS232.java | 44 +++++++++------- 2 files changed, 51 insertions(+), 45 deletions(-) diff --git a/platform/cooja/dev/rs232.c b/platform/cooja/dev/rs232.c index 4c1d43a20..15de5b200 100644 --- a/platform/cooja/dev/rs232.c +++ b/platform/cooja/dev/rs232.c @@ -26,7 +26,7 @@ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * - * $Id: rs232.c,v 1.2 2009/03/17 15:56:32 adamdunkels Exp $ + * $Id: rs232.c,v 1.3 2009/03/26 16:23:48 fros4943 Exp $ */ #include "lib/sensors.h" @@ -44,9 +44,6 @@ const struct simInterface rs232_interface; char simSerialReceivingData[SERIAL_BUF_SIZE]; int simSerialReceivingLength; char simSerialReceivingFlag; -char simSerialSendingData[SERIAL_BUF_SIZE]; -int simSerialSendingLength; -char simSerialSendingFlag; static int (* input_handler)(unsigned char) = NULL; @@ -62,40 +59,45 @@ rs232_set_input(int (*f)(unsigned char)) } /*-----------------------------------------------------------------------------------*/ void rs232_send(char c) { - simSerialSendingData[simSerialSendingLength] = c; - simSerialSendingLength += 1; - simSerialSendingFlag = 1; + printf("%c", c); } /*-----------------------------------------------------------------------------------*/ void rs232_print(char *message) { - memcpy(&simSerialSendingData[0] + simSerialSendingLength, &message[0], strlen(message)); - simSerialSendingLength += strlen(message); - simSerialSendingFlag = 1; + printf("%s", message); } /*-----------------------------------------------------------------------------------*/ static void doInterfaceActionsBeforeTick(void) { int i; - - // Check if this mote has received data on RS232 - if (simSerialReceivingFlag && simSerialReceivingLength > 0) { - // Tell user specified poll function - if(input_handler != NULL) - for (i=0; i < simSerialReceivingLength; i++) - input_handler(simSerialReceivingData[i]); - // Tell serial process - for (i=0; i < simSerialReceivingLength; i++) - serial_line_input_byte(simSerialReceivingData[i]); - - serial_line_input_byte(0x0a); - - simSerialReceivingLength = 0; - simSerialReceivingFlag = 0; + if (!simSerialReceivingFlag) { + return; } + + if (simSerialReceivingLength == 0) { + /* Error, should not be zero */ + simSerialReceivingFlag = 0; + return; + } + + /* Notify rs232 handler */ + if(input_handler != NULL) { + for (i=0; i < simSerialReceivingLength; i++) { + input_handler(simSerialReceivingData[i]); + } + } + + /* Notify serial process */ + for (i=0; i < simSerialReceivingLength; i++) { + serial_line_input_byte(simSerialReceivingData[i]); + } + serial_line_input_byte(0x0a); + + simSerialReceivingLength = 0; + simSerialReceivingFlag = 0; } /*-----------------------------------------------------------------------------------*/ static void 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 188b51623..6aec6f345 100644 --- a/tools/cooja/java/se/sics/cooja/contikimote/interfaces/ContikiRS232.java +++ b/tools/cooja/java/se/sics/cooja/contikimote/interfaces/ContikiRS232.java @@ -26,7 +26,7 @@ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * - * $Id: ContikiRS232.java,v 1.6 2009/03/21 15:41:42 fros4943 Exp $ + * $Id: ContikiRS232.java,v 1.7 2009/03/26 16:23:47 fros4943 Exp $ */ package se.sics.cooja.contikimote.interfaces; @@ -43,12 +43,9 @@ import se.sics.cooja.interfaces.PolledAfterActiveTicks; * * Contiki variables: * *

* @@ -117,24 +114,31 @@ public class ContikiRS232 extends SerialUI implements ContikiMoteInterface, Poll } public void writeString(String message) { - moteMem.setByteValueOf("simSerialReceivingFlag", (byte) 1); + final byte[] dataToAppend = message.getBytes(); - byte[] dataToAppend = message.getBytes(); + TimeEvent writeStringEvent = new TimeEvent(0) { + public void execute(long t) { + /* Append to existing buffer */ + int oldSize = moteMem.getIntValueOf("simSerialReceivingLength"); + int newSize = oldSize + dataToAppend.length; + moteMem.setIntValueOf("simSerialReceivingLength", newSize); - /* 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]; - 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); - System.arraycopy(oldData, 0, newData, 0, oldData.length); - System.arraycopy(dataToAppend, 0, newData, oldSize, dataToAppend.length); + moteMem.setByteArray("simSerialReceivingData", newData); - moteMem.setByteArray("simSerialReceivingData", newData); - - mote.setState(Mote.State.ACTIVE); + moteMem.setByteValueOf("simSerialReceivingFlag", (byte) 1); + mote.setState(Mote.State.ACTIVE); + } + }; + mote.getSimulation().scheduleEvent( + writeStringEvent, + mote.getSimulation().getSimulationTime() + ); } public double energyConsumption() {