From ef35fe6e6ef5f4366811b7ca2273385e69f12998 Mon Sep 17 00:00:00 2001 From: fros4943 Date: Thu, 11 Mar 2010 22:15:58 +0000 Subject: [PATCH] added experimental visualizer skin that shows the current source code of each mote in the simulation (for emulated mspmotes only) --- .../src/se/sics/cooja/mspmote/MspMote.java | 8 +- .../mspmote/plugins/CodeVisualizerSkin.java | 136 ++++++++++++++++++ 2 files changed, 143 insertions(+), 1 deletion(-) create mode 100644 tools/cooja/apps/mspsim/src/se/sics/cooja/mspmote/plugins/CodeVisualizerSkin.java diff --git a/tools/cooja/apps/mspsim/src/se/sics/cooja/mspmote/MspMote.java b/tools/cooja/apps/mspsim/src/se/sics/cooja/mspmote/MspMote.java index 808af8842..8602096c1 100644 --- a/tools/cooja/apps/mspsim/src/se/sics/cooja/mspmote/MspMote.java +++ b/tools/cooja/apps/mspsim/src/se/sics/cooja/mspmote/MspMote.java @@ -26,7 +26,7 @@ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * - * $Id: MspMote.java,v 1.43 2010/03/02 13:22:29 fros4943 Exp $ + * $Id: MspMote.java,v 1.44 2010/03/11 22:15:58 fros4943 Exp $ */ package se.sics.cooja.mspmote; @@ -58,6 +58,8 @@ import se.sics.cooja.interfaces.IPAddress; import se.sics.cooja.motes.AbstractEmulatedMote; import se.sics.cooja.mspmote.interfaces.MspSerial; import se.sics.cooja.mspmote.plugins.MspBreakpointContainer; +import se.sics.cooja.mspmote.plugins.CodeVisualizerSkin; +import se.sics.cooja.plugins.Visualizer; import se.sics.mspsim.cli.CommandHandler; import se.sics.mspsim.cli.LineListener; import se.sics.mspsim.cli.LineOutputStream; @@ -80,6 +82,10 @@ public abstract class MspMote extends AbstractEmulatedMote implements Mote, Watc private final static int EXECUTE_DURATION_US = 1; /* We always execute in 1 us steps */ + { + Visualizer.registerVisualizerSkin(CodeVisualizerSkin.class); + } + private Simulation simulation; private CommandHandler commandHandler; private ArrayList commandListeners = new ArrayList(); diff --git a/tools/cooja/apps/mspsim/src/se/sics/cooja/mspmote/plugins/CodeVisualizerSkin.java b/tools/cooja/apps/mspsim/src/se/sics/cooja/mspmote/plugins/CodeVisualizerSkin.java new file mode 100644 index 000000000..f67e387a8 --- /dev/null +++ b/tools/cooja/apps/mspsim/src/se/sics/cooja/mspmote/plugins/CodeVisualizerSkin.java @@ -0,0 +1,136 @@ +/* + * Copyright (c) 2010, 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: CodeVisualizerSkin.java,v 1.1 2010/03/11 22:15:58 fros4943 Exp $ + */ + +package se.sics.cooja.mspmote.plugins; + +import java.awt.Color; +import java.awt.FontMetrics; +import java.awt.Graphics; +import java.awt.Point; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.util.Observable; +import java.util.Observer; + +import javax.swing.Timer; + +import se.sics.cooja.ClassDescription; +import se.sics.cooja.Mote; +import se.sics.cooja.Simulation; +import se.sics.cooja.interfaces.Position; +import se.sics.cooja.mspmote.MspMote; +import se.sics.cooja.plugins.Visualizer; +import se.sics.cooja.plugins.VisualizerSkin; +import se.sics.mspsim.core.MSP430; +import se.sics.mspsim.util.DebugInfo; + +/** + * Code visualizer skin for MSPSim motes. + * + * Writes the current code line below each mote. + * + * @author Fredrik Osterlind + */ +@ClassDescription("Code") +public class CodeVisualizerSkin implements VisualizerSkin { + private Simulation simulation = null; + private Visualizer visualizer = null; + + private Timer repaintTimer = new Timer(100, new ActionListener() { + public void actionPerformed(ActionEvent e) { + if (simulation.isRunning()) { + visualizer.repaint(); + } + } + }); + + private Observer simulationObserver = new Observer() { + public void update(Observable obs, Object obj) { + visualizer.repaint(); + } + }; + + public void setActive(Simulation simulation, Visualizer vis) { + this.simulation = simulation; + this.visualizer = vis; + simulation.addObserver(simulationObserver); + repaintTimer.start(); + } + + public void setInactive() { + simulation.deleteObserver(simulationObserver); + repaintTimer.stop(); + } + + public Color[] getColorOf(Mote mote) { + return null; + } + + public void paintBeforeMotes(Graphics g) { + } + + private static String getMoteString(Mote mote) { + if (!(mote instanceof MspMote)) { + return null; + } + try { + DebugInfo debugInfo = + ((MspMote)mote).getELF().getDebugInfo(((MspMote)mote).getCPU().reg[MSP430.PC]); + if (debugInfo == null) { + return null; + } + return debugInfo.getFile() + ":" + debugInfo.getLine(); + } catch (Exception e) { + return "?"; + } + } + + public void paintAfterMotes(Graphics g) { + FontMetrics fm = g.getFontMetrics(); + g.setColor(Color.BLACK); + + Mote[] motes = simulation.getMotes(); + for (Mote mote: motes) { + String msg = getMoteString(mote); + if (msg == null) { + continue; + } + Position pos = mote.getInterfaces().getPosition(); + Point pixel = visualizer.transformPositionToPixel(pos); + int msgWidth = fm.stringWidth(msg); + g.drawString(msg, pixel.x - msgWidth/2, pixel.y + 2*Visualizer.MOTE_RADIUS + 3); + } + } + + public Visualizer getVisualizer() { + return visualizer; + } +}