From 944a812baaedab9308f5643acbbd01d41fe247e1 Mon Sep 17 00:00:00 2001 From: fros4943 Date: Mon, 26 Feb 2007 13:28:14 +0000 Subject: [PATCH] test implementation for forwarding serial data on per-node-basis to external applications (useful when connecting serialport-bound application used to communicating with real nodes) --- tools/cooja/apps/serial2pipe/build.xml | 36 ++++ tools/cooja/apps/serial2pipe/cooja.config | 2 + .../se/sics/cooja/plugins/Serial2Pipe.java | 177 ++++++++++++++++++ .../apps/serial2pipe/lib/serial2pipe.jar | Bin 0 -> 8484 bytes 4 files changed, 215 insertions(+) create mode 100644 tools/cooja/apps/serial2pipe/build.xml create mode 100644 tools/cooja/apps/serial2pipe/cooja.config create mode 100644 tools/cooja/apps/serial2pipe/java/se/sics/cooja/plugins/Serial2Pipe.java create mode 100644 tools/cooja/apps/serial2pipe/lib/serial2pipe.jar diff --git a/tools/cooja/apps/serial2pipe/build.xml b/tools/cooja/apps/serial2pipe/build.xml new file mode 100644 index 000000000..9d8cebfa6 --- /dev/null +++ b/tools/cooja/apps/serial2pipe/build.xml @@ -0,0 +1,36 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/tools/cooja/apps/serial2pipe/cooja.config b/tools/cooja/apps/serial2pipe/cooja.config new file mode 100644 index 000000000..766cefd53 --- /dev/null +++ b/tools/cooja/apps/serial2pipe/cooja.config @@ -0,0 +1,2 @@ +se.sics.cooja.GUI.PLUGINS = + se.sics.cooja.plugins.Serial2Pipe +se.sics.cooja.GUI.JARFILES = + serial2pipe.jar diff --git a/tools/cooja/apps/serial2pipe/java/se/sics/cooja/plugins/Serial2Pipe.java b/tools/cooja/apps/serial2pipe/java/se/sics/cooja/plugins/Serial2Pipe.java new file mode 100644 index 000000000..102d7a434 --- /dev/null +++ b/tools/cooja/apps/serial2pipe/java/se/sics/cooja/plugins/Serial2Pipe.java @@ -0,0 +1,177 @@ +/* + * Copyright (c) 2006, Swedish Institute of Computer Science. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the Institute nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * $Id: Serial2Pipe.java,v 1.1 2007/02/26 13:28:14 fros4943 Exp $ + */ + +package se.sics.cooja.plugins; + +import java.awt.GridLayout; +import java.io.BufferedReader; +import java.io.InputStreamReader; +import java.util.Observable; +import java.util.Observer; +import javax.swing.*; +import org.apache.log4j.Logger; + +import se.sics.cooja.*; +import se.sics.cooja.contikimote.interfaces.ContikiRS232; + +@ClassDescription("Serial 2 Pipe") +@PluginType(PluginType.MOTE_PLUGIN) +public class Serial2Pipe extends VisPlugin { + private static Logger logger = Logger.getLogger(Serial2Pipe.class); + + private static final long serialVersionUID = 1L; + + private static int pipeNrCounter = 1; + + private Mote mote; + + private ContikiRS232 serialInterface; + + private String inPipeName; + + private String outPipeName; + + private int pipeNr = pipeNrCounter++; // Increase pipe number + + private Thread pipeListener; + + private Observer serialObserver; + + public Serial2Pipe(Mote moteToView, Simulation simulation, GUI gui) { + super("Serial 2 Pipe (" + moteToView + ")", gui); + mote = moteToView; + + // Determine pipe names + inPipeName = "s2p" + pipeNr + "in"; + outPipeName = "s2p" + pipeNr + "out"; + + // Create pipes + try { + Process subProcess = Runtime.getRuntime().exec( + new String[] { "mkfifo", inPipeName }); + subProcess.waitFor(); +// logger.debug("Creating in pipe returned: " + subProcess.exitValue()); + subProcess = Runtime.getRuntime().exec( + new String[] { "mkfifo", outPipeName }); + subProcess.waitFor(); +// logger.debug("Creating out pipe returned: " + subProcess.exitValue()); + } catch (Exception e) { + logger.fatal("Error when creating pipes: " + e); + } + + // Forward serial interface to out pipe + serialInterface = mote.getInterfaces().getInterfaceOfType( + ContikiRS232.class); + serialInterface.addObserver(serialObserver = new Observer() { + public void update(Observable obs, Object obj) { + // Start shell process which forwards the data + try { + Runtime.getRuntime().exec( + new String[] { + "sh", + "-c", + "echo " + serialInterface.getSerialMessages() + " > " + + outPipeName }); +// logger.debug("Forwarding from serial " +// + serialInterface.getSerialMessages()); + } catch (Exception ex) { + logger.fatal("Error when writing to out pipe: " + ex); + } + } + }); + + // Forward incoming data from in pipe (separate thread) + pipeListener = new Thread(new Runnable() { + public void run() { + try { + while (true) { + // Start shell process which reads the data + Process subProcess = Runtime.getRuntime().exec( + new String[] { "sh", "-c", "cat " + inPipeName }); + BufferedReader in = new BufferedReader(new InputStreamReader( + subProcess.getInputStream())); + char[] buffer = new char[512]; + int bytes_read; + bytes_read = in.read(buffer); + if (bytes_read > 0) { + String msg = String.copyValueOf(buffer, 0, bytes_read); +// logger.debug("Forwarding to serial: " + msg); + serialInterface.sendSerialMessage(msg); + } + in.close(); + subProcess.waitFor(); + } + } catch (Exception e) { + logger.fatal("Error while reading from in pipe: " + e); + } + } + }); + pipeListener.start(); + + // Add GUI labels + setLayout(new GridLayout(2, 2)); + + add(new JLabel("In pipe:")); + add(new JLabel(inPipeName)); + + add(new JLabel("Out pipe:")); + add(new JLabel(outPipeName)); + + setSize(300, 70); + + try { + setSelected(true); + } catch (java.beans.PropertyVetoException e) { + // Could not select + } + } + + public void closePlugin() { + // Stop listening to serial port + if (serialInterface != null && serialObserver != null) { + serialInterface.deleteObserver(serialObserver); + serialObserver = null; + } + + // Stop listening to in pipe + if (pipeListener != null) + pipeListener.interrupt(); + + // Remove earlier created pipes + try { + Runtime.getRuntime().exec(new String[] { "rm", inPipeName }); + Runtime.getRuntime().exec(new String[] { "rm", outPipeName }); + } catch (Exception e) { + logger.fatal("Error when deleting pipes"); + } + } + +} diff --git a/tools/cooja/apps/serial2pipe/lib/serial2pipe.jar b/tools/cooja/apps/serial2pipe/lib/serial2pipe.jar new file mode 100644 index 0000000000000000000000000000000000000000..a485e72de75663c5a4668017a4b3361c61b0e78c GIT binary patch literal 8484 zcmWIWW@h1H0D+RCC^HZZ!<-B(48E=*j=G+HZu*(j{<{BKL=j-;__snS@Z(Y5MyxzK6=gyqp9At3C_`%bnGtNSVIvzgf zJVF_|7*)@H@u}Q7r#nd4J1Dqhu9o8~->*E6cy&rA^Yn$X0|UB8m>C!tic|FiaQc)3qM$f4xfqWs zJ|tDi`T1Fic+`qvs4d7TP0!3jn9l)rru})T{I^UD3{O}Y7&LL}2~I7_Ow2I~$Sg=z zG1N=WNh~g&8uB{mvV+J!F|Xicku_Xeaf=k#1e~?+9nj|J5_5dD_VIev*vkng$|mXl za{E{2#=Uqk{{v^sdAjDBT{mXtmF+M7d(LvVeO=v8K85z_zb|yk3CF%LEtIQe(@S}f zav?wAKuhC29=5OF7jECV>|q7!sUWE=~y#^MH96pW3E0h6~110_`r(BXtq4# zC84SwRqc{hT%84z4|vD;pK^T{c6gS&OJ}r5;eo?{Vgf{ym(&?|Fu9i1o|F6XJKkW4 z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