new experimental feature: possibility to change mote types when loading a simulation

made compile dialogs only select mote interfaces specified in the loaded configuration
This commit is contained in:
Fredrik Osterlind 2012-05-23 14:59:41 +02:00
parent c651604a4f
commit 50caa3650c
7 changed files with 170 additions and 97 deletions

View File

@ -117,7 +117,7 @@ $(CONTIKI_APP).cooja: $(JNILIB)
cp $(JNILIB) $@ cp $(JNILIB) $@
rm $(CONTIKI_APP_OBJ) rm $(CONTIKI_APP_OBJ)
mtype%.o: contiki-cooja-main.o %.o: contiki-cooja-main.o
mv contiki-cooja-main.o $@ mv contiki-cooja-main.o $@
symbols.c: symbols.c:

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2009, Swedish Institute of Computer Science. * Copyright (c) 2012, Swedish Institute of Computer Science.
* All rights reserved. * All rights reserved.
* *
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without
@ -25,8 +25,6 @@
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * 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 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE. * SUCH DAMAGE.
*
* $Id: MicaZCompileDialog.java,v 1.1 2009/09/17 10:45:14 fros4943 Exp $
*/ */
package se.sics.cooja.avrmote; package se.sics.cooja.avrmote;
@ -64,17 +62,10 @@ public class MicaZCompileDialog extends AbstractCompileDialog {
private MicaZCompileDialog(Container parent, Simulation simulation, MoteType moteType) { private MicaZCompileDialog(Container parent, Simulation simulation, MoteType moteType) {
super(parent, simulation, moteType); super(parent, simulation, moteType);
}
/* Add all available MicaZ mote interfaces public Class<? extends MoteInterface>[] getDefaultMoteInterfaces() {
* Selected by default unless interfaces already configured */ return ((MicaZMoteType)moteType).getAllMoteInterfaceClasses();
boolean selected = true;
if (moteIntfBox.getComponentCount() > 0) {
selected = false;
}
for (Class<? extends MoteInterface> intfClass: ((MicaZMoteType)moteType).getAllMoteInterfaceClasses()) {
addMoteInterface(intfClass, selected);
}
} }
public boolean canLoadFirmware(File file) { public boolean canLoadFirmware(File file) {

View File

@ -45,7 +45,7 @@ import se.sics.cooja.dialogs.AbstractCompileDialog;
public class MspCompileDialog extends AbstractCompileDialog { public class MspCompileDialog extends AbstractCompileDialog {
private static final long serialVersionUID = -7273193946433145019L; private static final long serialVersionUID = -7273193946433145019L;
private static String target; private final String target;
public static boolean showDialog( public static boolean showDialog(
Container parent, Container parent,
@ -53,7 +53,6 @@ public class MspCompileDialog extends AbstractCompileDialog {
MspMoteType moteType, MspMoteType moteType,
String target) { String target) {
MspCompileDialog.target = target;
final AbstractCompileDialog dialog = new MspCompileDialog(parent, simulation, moteType, target); final AbstractCompileDialog dialog = new MspCompileDialog(parent, simulation, moteType, target);
/* Show dialog and wait for user */ /* Show dialog and wait for user */
@ -68,20 +67,15 @@ public class MspCompileDialog extends AbstractCompileDialog {
private MspCompileDialog(Container parent, Simulation simulation, MspMoteType moteType, String target) { private MspCompileDialog(Container parent, Simulation simulation, MspMoteType moteType, String target) {
super(parent, simulation, moteType); super(parent, simulation, moteType);
this.target = target;
setTitle("Create Mote Type: Compile Contiki for " + target); setTitle("Create Mote Type: Compile Contiki for " + target);
/* Select all mote interfaces */
boolean selected = true;
if (moteIntfBox.getComponentCount() > 0) {
selected = false;
}
for (Class<? extends MoteInterface> intfClass: moteType.getAllMoteInterfaceClasses()) {
addMoteInterface(intfClass, selected);
}
addCompilationTipsTab(tabbedPane); addCompilationTipsTab(tabbedPane);
} }
public Class<? extends MoteInterface>[] getDefaultMoteInterfaces() {
return ((MspMoteType)moteType).getAllMoteInterfaceClasses();
}
private void addCompilationTipsTab(JTabbedPane parent) { private void addCompilationTipsTab(JTabbedPane parent) {
JTextArea textArea = new JTextArea(); JTextArea textArea = new JTextArea();
textArea.setEditable(false); textArea.setEditable(false);

View File

@ -37,6 +37,8 @@ import java.util.Observer;
import java.util.Random; import java.util.Random;
import java.util.Vector; import java.util.Vector;
import javax.swing.JOptionPane;
import org.apache.log4j.Logger; import org.apache.log4j.Logger;
import org.jdom.Element; import org.jdom.Element;
@ -634,6 +636,25 @@ public class Simulation extends Observable implements Runnable {
if (element.getName().equals("motetype")) { if (element.getName().equals("motetype")) {
String moteTypeClassName = element.getText().trim(); String moteTypeClassName = element.getText().trim();
/* Try to recreate simulation using a different mote type */
if (visAvailable) {
String[] availableMoteTypes = getGUI().getProjectConfig().getStringArrayValue("se.sics.cooja.GUI.MOTETYPES");
String newClass = (String) JOptionPane.showInputDialog(
GUI.getTopParentContainer(),
"The simulation is about to load '" + moteTypeClassName + "'\n" +
"You may try to load the simulation using a different mote type.\n",
"Loading mote type",
JOptionPane.QUESTION_MESSAGE,
null,
availableMoteTypes,
moteTypeClassName
);
if (newClass != null && !newClass.equals(moteTypeClassName)) {
logger.warn("Changing mote type class: " + moteTypeClassName + " -> " + newClass);
moteTypeClassName = newClass;
}
}
Class<? extends MoteType> moteTypeClass = myGUI.tryLoadClass(this, Class<? extends MoteType> moteTypeClass = myGUI.tryLoadClass(this,
MoteType.class, moteTypeClassName); MoteType.class, moteTypeClassName);

View File

@ -1306,7 +1306,7 @@ public class ContikiMoteType implements MoteType {
element.setText(getDescription()); element.setText(getDescription());
config.add(element); config.add(element);
element = new Element("contikiapp"); element = new Element("source");
File file = simulation.getGUI().createPortablePath(getContikiSourceFile()); File file = simulation.getGUI().createPortablePath(getContikiSourceFile());
element.setText(file.getPath().replaceAll("\\\\", "/")); element.setText(file.getPath().replaceAll("\\\\", "/"));
config.add(element); config.add(element);
@ -1350,7 +1350,7 @@ public class ContikiMoteType implements MoteType {
identifier = element.getText(); identifier = element.getText();
} else if (name.equals("description")) { } else if (name.equals("description")) {
description = element.getText(); description = element.getText();
} else if (name.equals("contikiapp")) { } else if (name.equals("contikiapp") || name.equals("source")) {
File file = new File(element.getText()); File file = new File(element.getText());
if (!file.exists()) { if (!file.exists()) {
file = simulation.getGUI().restorePortablePath(file); file = simulation.getGUI().restorePortablePath(file);

View File

@ -25,27 +25,58 @@
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * 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 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE. * SUCH DAMAGE.
*
* $Id: AbstractCompileDialog.java,v 1.9 2010/03/08 14:26:12 fros4943 Exp $
*/ */
package se.sics.cooja.dialogs; package se.sics.cooja.dialogs;
import java.awt.*; import java.awt.BorderLayout;
import java.awt.event.*; import java.awt.Component;
import java.awt.Container;
import java.awt.Dialog;
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.GraphicsEnvironment;
import java.awt.Rectangle;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import javax.swing.*; import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.InputMap;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.KeyStroke;
import javax.swing.SwingUtilities;
import javax.swing.event.DocumentEvent; import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener; import javax.swing.event.DocumentListener;
import javax.swing.filechooser.FileFilter; import javax.swing.filechooser.FileFilter;
import org.apache.log4j.Logger; import org.apache.log4j.Logger;
import se.sics.cooja.*; import se.sics.cooja.GUI;
import se.sics.cooja.dialogs.MessageList; import se.sics.cooja.MoteInterface;
import se.sics.cooja.MoteType;
import se.sics.cooja.Simulation;
import se.sics.cooja.interfaces.MoteID; import se.sics.cooja.interfaces.MoteID;
import se.sics.cooja.interfaces.Position; import se.sics.cooja.interfaces.Position;
@ -330,10 +361,24 @@ public abstract class AbstractCompileDialog extends JDialog {
} }
/* Restore mote interface classes */ /* Restore mote interface classes */
for (Component c : moteIntfBox.getComponents()) {
if (!(c instanceof JCheckBox)) {
continue;
}
((JCheckBox) c).setSelected(false);
}
if (moteType.getMoteInterfaceClasses() != null) { if (moteType.getMoteInterfaceClasses() != null) {
for (Class<? extends MoteInterface> intfClass: getDefaultMoteInterfaces()) {
addMoteInterface(intfClass, false);
}
for (Class<? extends MoteInterface> intf: moteType.getMoteInterfaceClasses()) { for (Class<? extends MoteInterface> intf: moteType.getMoteInterfaceClasses()) {
addMoteInterface(intf, true); addMoteInterface(intf, true);
} }
} else {
/* Select default mote interfaces */
for (Class<? extends MoteInterface> intfClass: getDefaultMoteInterfaces()) {
addMoteInterface(intfClass, true);
}
} }
/* Restore compile commands */ /* Restore compile commands */
@ -536,11 +581,11 @@ public abstract class AbstractCompileDialog extends JDialog {
compileButton.setEnabled(false); compileButton.setEnabled(false);
createButton.setEnabled(false); createButton.setEnabled(false);
commandsArea.setEnabled(false); commandsArea.setEnabled(false);
setCompileCommands("");
break; break;
case SELECTED_SOURCE: case SELECTED_SOURCE:
if (!sourceFile.exists()) { if (!sourceFile.exists()) {
logger.warn("Could not find Contiki source: " + sourceFile.getAbsolutePath());
setDialogState(DialogState.NO_SELECTION); setDialogState(DialogState.NO_SELECTION);
return; return;
} }
@ -561,6 +606,7 @@ public abstract class AbstractCompileDialog extends JDialog {
case AWAITING_COMPILATION: case AWAITING_COMPILATION:
if (!sourceFile.exists()) { if (!sourceFile.exists()) {
logger.warn("Could not find Contiki source: " + sourceFile.getAbsolutePath());
setDialogState(DialogState.NO_SELECTION); setDialogState(DialogState.NO_SELECTION);
return; return;
} }
@ -569,10 +615,10 @@ public abstract class AbstractCompileDialog extends JDialog {
return; return;
} }
cleanButton.setEnabled(true); cleanButton.setEnabled(true);
compileButton.setEnabled(true); compileButton.setEnabled(true);
createButton.setEnabled(false); createButton.setEnabled(false);
commandsArea.setEnabled(true); commandsArea.setEnabled(true);
break; break;
case IS_COMPILING: case IS_COMPILING:
@ -635,11 +681,34 @@ public abstract class AbstractCompileDialog extends JDialog {
private void addMoteInterfacesTab(JTabbedPane parent) { private void addMoteInterfacesTab(JTabbedPane parent) {
moteIntfBox = Box.createVerticalBox(); moteIntfBox = Box.createVerticalBox();
JPanel panel = new JPanel(new BorderLayout()); JPanel panel = new JPanel(new BorderLayout());
panel.add(BorderLayout.NORTH, new JLabel("COOJA interacts with simulated motes via mote interfaces. You normally do not need to change these settings!")); JLabel label = new JLabel("COOJA interacts with simulated motes via mote interfaces. You normally do not need to change these settings!");
Box b = Box.createHorizontalBox();
b.add(new JButton(defaultAction));
b.add(label);
panel.add(BorderLayout.NORTH, b);
panel.add(BorderLayout.CENTER, new JScrollPane(moteIntfBox)); panel.add(BorderLayout.CENTER, new JScrollPane(moteIntfBox));
parent.addTab("Mote interfaces", null, panel, "Mote interfaces"); parent.addTab("Mote interfaces", null, panel, "Mote interfaces");
} }
private Action defaultAction = new AbstractAction("Use default") {
public void actionPerformed(ActionEvent e) {
/* Unselect all */
for (Component c : moteIntfBox.getComponents()) {
if (!(c instanceof JCheckBox)) {
continue;
}
((JCheckBox) c).setSelected(false);
}
/* Select default */
for (Class<? extends MoteInterface> moteIntf : getDefaultMoteInterfaces()) {
addMoteInterface(moteIntf, true);
}
}
};
public abstract Class<? extends MoteInterface>[] getDefaultMoteInterfaces();
/** /**
* @return Currently selected mote interface classes * @return Currently selected mote interface classes
*/ */
@ -679,7 +748,7 @@ public abstract class AbstractCompileDialog extends JDialog {
* @param selected If true, interface will initially be selected * @param selected If true, interface will initially be selected
*/ */
public void addMoteInterface(Class<? extends MoteInterface> intfClass, boolean selected) { public void addMoteInterface(Class<? extends MoteInterface> intfClass, boolean selected) {
/* If mote interface was already added, do nothing */ /* If mote interface was already added */
for (Component c : moteIntfBox.getComponents()) { for (Component c : moteIntfBox.getComponents()) {
if (!(c instanceof JCheckBox)) { if (!(c instanceof JCheckBox)) {
continue; continue;
@ -694,6 +763,7 @@ public abstract class AbstractCompileDialog extends JDialog {
} }
if (existingClass == intfClass) { if (existingClass == intfClass) {
((JCheckBox) c).setSelected(selected);
return; return;
} }
} }

View File

@ -37,6 +37,7 @@ import java.awt.Container;
import java.awt.event.ActionEvent; import java.awt.event.ActionEvent;
import java.awt.event.ActionListener; import java.awt.event.ActionListener;
import java.io.File; import java.io.File;
import java.util.ArrayList;
import javax.swing.BorderFactory; import javax.swing.BorderFactory;
import javax.swing.Box; import javax.swing.Box;
@ -100,7 +101,6 @@ public class ContikiMoteCompileDialog extends AbstractCompileDialog {
} }
/* Add Contiki mote type specifics */ /* Add Contiki mote type specifics */
addMoteInterfaceClasses();
addAdvancedTab(tabbedPane); addAdvancedTab(tabbedPane);
} }
@ -200,14 +200,10 @@ public class ContikiMoteCompileDialog extends AbstractCompileDialog {
return ContikiMoteType.getExpectedFirmwareFile(source); return ContikiMoteType.getExpectedFirmwareFile(source);
} }
private void addMoteInterfaceClasses() { public Class<? extends MoteInterface>[] getDefaultMoteInterfaces() {
ProjectConfig projectConfig = moteType.getConfig(); ProjectConfig projectConfig = moteType.getConfig();
String[] intfNames = projectConfig.getStringArrayValue(ContikiMoteType.class, "MOTE_INTERFACES"); String[] intfNames = projectConfig.getStringArrayValue(ContikiMoteType.class, "MOTE_INTERFACES");
ArrayList<Class<? extends MoteInterface>> classes = new ArrayList<Class<? extends MoteInterface>>();
boolean selected = true;
if (moteIntfBox.getComponentCount() > 0) {
selected = false;
}
/* Load mote interface classes */ /* Load mote interface classes */
for (String intfName : intfNames) { for (String intfName : intfNames) {
@ -219,8 +215,9 @@ public class ContikiMoteCompileDialog extends AbstractCompileDialog {
continue; continue;
} }
addMoteInterface(intfClass, selected); classes.add(intfClass);
} }
return classes.toArray(new Class[0]);
} }
private void addAdvancedTab(JTabbedPane parent) { private void addAdvancedTab(JTabbedPane parent) {