📄 mspmotetype.java
字号:
/* * Copyright (c) 2007, 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: MspMoteType.java,v 1.19 2008/11/04 17:37:09 fros4943 Exp $ */package se.sics.cooja.mspmote;import java.awt.*;import java.awt.Dialog.ModalityType;import java.awt.event.*;import java.io.*;import java.util.Collection;import java.util.Vector;import javax.swing.*;import javax.swing.event.DocumentEvent;import javax.swing.event.DocumentListener;import javax.swing.filechooser.FileFilter;import org.apache.log4j.Logger;import org.jdom.Element;import se.sics.cooja.*;import se.sics.cooja.dialogs.MessageList;@ClassDescription("Msp Mote Type")public abstract class MspMoteType implements MoteType { private static Logger logger = Logger.getLogger(MspMoteType.class); protected static String getTargetFileExtension(String target) { return "." + target; } /* Convenience: Preselecting last used directory */ protected static File lastParentDirectory = null; private String identifier = null; private String description = null; /* If source file is defined, (re)compilation is performed */ private File fileFirmware = null; private File fileSource = null; private String compileCommand = null; public String getIdentifier() { return identifier; } public void setIdentifier(String identifier) { this.identifier = identifier; } public String getDescription() { return description; } public void setDescription(String description) { this.description = description; } /** * Set ELF file. * * @param file * ELF file */ public void setELFFile(File file) { this.fileFirmware = file; } /** * Set compile command. * * @param command * Compile command */ public void setCompileCommand(String command) { this.compileCommand = command; } /** * @return ELF file */ public File getELFFile() { return fileFirmware; } /** * @return Compile command */ public String getCompileCommand() { return compileCommand; } /** * Set source file * * @param file Source file */ public void setSourceFile(File file) { fileSource = file; } /** * @return Source file */ public File getSourceFile() { return fileSource; } public final Mote generateMote(Simulation simulation) { MspMote mote = createMote(simulation); mote.initMote(); return mote; } protected abstract MspMote createMote(Simulation simulation); /** * Configures and initialized Msp mote types. * * @param parentContainer Graphical parent container * @param simulation Current simulation * @param visAvailable Enable graphical interfaces and user input * @param target Contiki target platform name * @param targetNice Nicer representation of target * @return True is successful * @throws MoteTypeCreationException Mote type creation failed */ protected boolean configureAndInitMspType(Container parentContainer, Simulation simulation, boolean visAvailable, final String target, final String targetNice) throws MoteTypeCreationException { boolean compileFromSource = false; if (getIdentifier() == null && !visAvailable) { throw new MoteTypeCreationException("No identifier"); } /* Generate unique identifier */ if (getIdentifier() == null) { int counter = 0; boolean identifierOK = false; while (!identifierOK) { counter++; setIdentifier(target + counter); identifierOK = true; // Check if identifier is already used by some other type for (MoteType existingMoteType : simulation.getMoteTypes()) { if (existingMoteType != this && existingMoteType.getIdentifier().equals(getIdentifier())) { identifierOK = false; break; } } } if (getDescription() == null) { setDescription(targetNice + " Mote Type #" + counter); } /* Let user choose whether to compile or load existing binaries */ Object[] options = { "Compile", "Existing" }; String question = targetNice + " mote type loads a firmware (ELF).\n\n" + "To compile a Contiki application from source: 'Compile'\n" + "To use a pre-compiled existing firmware: 'Existing'.\n"; String title = "Compile or load existing " + targetNice + " firmware"; if (GUI.isVisualizedInApplet()) { compileFromSource = false; } else { int answer = JOptionPane.showOptionDialog(GUI.getTopParentContainer(), question, title, JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, options[0]); if (answer != JOptionPane.YES_OPTION && answer != JOptionPane.NO_OPTION) { return false; } compileFromSource = answer == JOptionPane.YES_OPTION; } } /* Description */ if (getDescription() == null) { setDescription(targetNice + " Mote Type #" + getIdentifier()); } if (getSourceFile() != null) { compileFromSource = true; } if (compileFromSource) { MspELFCompiler compiler = new MspELFCompiler(target); compiler.setCompileCommand(compileCommand); if (visAvailable) { boolean success = compiler.showDialog(GUI.getTopParentContainer(), this); if (success) { setSourceFile(compiler.getSourceFile()); setELFFile(compiler.getOutputFile()); setCompileCommand(compiler.getLastCompileCommand()); return true; } else { return false; } } else { MessageList compilationOutput = new MessageList(); try { compiler.compileFirmware(getSourceFile(), null, null, compilationOutput, true); } catch (Exception e) { MoteTypeCreationException newException = new MoteTypeCreationException( "Mote type creation failed: " + e.getMessage()); newException = (MoteTypeCreationException) newException.initCause(e); newException.setCompilationOutput(compilationOutput); /* Print last compilation errors */ try { Thread.sleep(500); } catch (InterruptedException ignore) { } ListModel tmp = compilationOutput.getModel(); for (int i=tmp.getSize()-5; i < tmp.getSize(); i++) { if (i < 0) { continue; } logger.fatal(">> " + tmp.getElementAt(i)); } logger.fatal("Compilation error: " + e.getMessage()); throw newException; } setSourceFile(compiler.getSourceFile()); setELFFile(compiler.getOutputFile()); setCompileCommand(compiler.getLastCompileCommand()); return true; } } if (GUI.isVisualizedInApplet()) { return true; } // Check dependency files if (getELFFile() == null || !getELFFile().exists()) { if (!visAvailable) { throw new MoteTypeCreationException("ELF file does not exist: " + getELFFile()); } JFileChooser fc = new JFileChooser(); // Select previous directory if (lastParentDirectory != null) { fc.setCurrentDirectory(lastParentDirectory); } else { fc.setCurrentDirectory(new java.io.File(GUI.getExternalToolsSetting("PATH_CONTIKI"))); } fc.setFileSelectionMode(JFileChooser.FILES_ONLY); fc.addChoosableFileFilter(new FileFilter() { public boolean accept(File f) { String filename = f.getName(); if (f.isDirectory()) { return true; } if (filename != null) { if (filename.endsWith(getTargetFileExtension(target)) || filename.endsWith(".firmware")) { return true; } } return false; } public String getDescription() { return "Firmware files"; } }); fc.addChoosableFileFilter(new FileFilter() { public boolean accept(File f) { String filename = f.getName(); if (f.isDirectory()) { return true; } if (filename != null) { if (filename.endsWith(getTargetFileExtension(target))) { return true; } } return false; } public String getDescription() { return targetNice + " firmware files"; } }); fc.setDialogTitle("Select firmware file"); if (fc.showOpenDialog(parentContainer) == JFileChooser.APPROVE_OPTION) { File selectedFile = fc.getSelectedFile(); if (!selectedFile.exists()) { logger.fatal("Selected file \"" + selectedFile + "\" does not exist"); return false; }// if (!selectedFile.getName().endsWith(firmwareFileExtension)) {// logger.fatal("Selected file \"" + selectedFile + "\" does not end with " + firmwareFileExtension);// return false;// } setELFFile(fc.getSelectedFile()); } else { return false; } } return true; } protected static class MspELFCompiler { private static int LABEL_WIDTH = 170; private static int LABEL_HEIGHT = 15; private JButton cancelButton = new JButton("Cancel"); private JButton cleanButton = new JButton("Clean"); private JButton compileButton = new JButton("Compile"); private JButton createButton = new JButton("Create"); private JTextField sourceTextField = new JTextField(); private JTextField compileCommandTextField = new JTextField(); // private JFormattedTextField nodeIDTextField; // private NumberFormat integerFormat = NumberFormat.getIntegerInstance(); private String lastCompileCommand = null; private File sourceFile = null; private File ELFFile = null;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -