calling contiki compile library for compiling instead of as earlier the msp mote type

This commit is contained in:
fros4943 2009-03-09 16:03:58 +00:00
parent bd3107ae0d
commit 83ad0c18ba
2 changed files with 192 additions and 18 deletions

View file

@ -26,7 +26,7 @@
* 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: ESBMoteType.java,v 1.5 2008/06/27 14:09:26 nifi Exp $ * $Id: ESBMoteType.java,v 1.6 2009/03/09 16:03:58 fros4943 Exp $
*/ */
package se.sics.cooja.mspmote; package se.sics.cooja.mspmote;
@ -35,23 +35,24 @@ import java.awt.*;
import java.io.File; import java.io.File;
import java.net.URL; import java.net.URL;
import javax.swing.*; import javax.swing.*;
import org.apache.log4j.Logger; import org.apache.log4j.Logger;
import se.sics.cooja.*; import se.sics.cooja.*;
import se.sics.cooja.dialogs.CompileContiki;
import se.sics.cooja.dialogs.MessageList;
import se.sics.cooja.dialogs.MessageList.MessageContainer;
@ClassDescription("ESB Mote Type") @ClassDescription("ESB Mote Type")
@AbstractionLevelDescription("Emulated level") @AbstractionLevelDescription("Emulated level")
public class ESBMoteType extends MspMoteType { public class ESBMoteType extends MspMoteType {
private static Logger logger = Logger.getLogger(ESBMoteType.class); private static Logger logger = Logger.getLogger(ESBMoteType.class);
public static final String target = "esb";
public static final String targetNice = "ESB";
public ESBMoteType() { public ESBMoteType() {
} }
public ESBMoteType(String identifier) { public ESBMoteType(String identifier) {
setIdentifier(identifier); setIdentifier(identifier);
setDescription(targetNice + " Mote Type #" + identifier); setDescription("ESB Mote Type #" + identifier);
} }
public Icon getMoteTypeIcon() { public Icon getMoteTypeIcon() {
@ -76,12 +77,15 @@ public class ESBMoteType extends MspMoteType {
return new ESBMote(this, simulation); return new ESBMote(this, simulation);
} }
public boolean configureAndInit(Container parentContainer, Simulation simulation, public boolean configureAndInit(Container parentContainer, Simulation simulation, boolean visAvailable)
boolean visAvailable) throws MoteTypeCreationException { throws MoteTypeCreationException {
/* SPECIAL CASE: Cooja started in applet.
* Use preconfigured Contiki firmware */
if (GUI.isVisualizedInApplet()) { if (GUI.isVisualizedInApplet()) {
String firmware = GUI.getExternalToolsSetting("ESB_FIRMWARE", ""); String firmware = GUI.getExternalToolsSetting("ESB_FIRMWARE", "");
if (!firmware.equals("")) { if (!firmware.equals("")) {
setELFFile(new File(firmware)); setContikiFirmwareFile(new File(firmware));
JOptionPane.showMessageDialog(GUI.getTopParentContainer(), JOptionPane.showMessageDialog(GUI.getTopParentContainer(),
"Creating mote type from precompiled firmware: " + firmware, "Creating mote type from precompiled firmware: " + firmware,
"Compiled firmware file available", JOptionPane.INFORMATION_MESSAGE); "Compiled firmware file available", JOptionPane.INFORMATION_MESSAGE);
@ -93,7 +97,90 @@ public class ESBMoteType extends MspMoteType {
} }
} }
return configureAndInitMspType(parentContainer, simulation, visAvailable, target, targetNice); /* If visualized, show compile dialog and let user configure */
if (visAvailable) {
/* Create unique identifier */
if (getIdentifier() == null) {
int counter = 0;
boolean identifierOK = false;
while (!identifierOK) {
identifierOK = true;
counter++;
setIdentifier("esb" + counter);
for (MoteType existingMoteType : simulation.getMoteTypes()) {
if (existingMoteType == this) {
continue;
}
if (existingMoteType.getIdentifier().equals(getIdentifier())) {
identifierOK = false;
break;
}
}
}
}
/* Create initial description */
if (getDescription() == null) {
setDescription("ESB Mote Type #" + getIdentifier());
}
return ESBCompileDialog.showDialog(parentContainer, simulation, this);
}
/* Not visualized: Compile Contiki immediately */
if (getIdentifier() == null) {
throw new MoteTypeCreationException("No identifier");
}
final MessageList compilationOutput = new MessageList();
if (getCompileCommands() != null) {
/* Handle multiple compilation commands one by one */
String[] arr = getCompileCommands().split("\n");
for (String cmd: arr) {
if (cmd.trim().isEmpty()) {
continue;
}
try {
CompileContiki.compile(
cmd,
null /* No output file */,
getContikiSourceFile().getParentFile(),
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 10 compilation errors to console */
MessageContainer[] messages = compilationOutput.getMessages();
for (int i=messages.length-10; i < messages.length; i++) {
if (i < 0) {
continue;
}
logger.fatal(">> " + messages[i]);
}
logger.fatal("Compilation error: " + e.getMessage());
throw newException;
}
}
}
if (getContikiFirmwareFile() == null ||
!getContikiFirmwareFile().exists()) {
throw new MoteTypeCreationException("Contiki firmware file does not exist: " + getContikiFirmwareFile());
}
return true;
} }
} }

View file

@ -26,7 +26,7 @@
* 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: SkyMoteType.java,v 1.4 2008/06/27 14:09:26 nifi Exp $ * $Id: SkyMoteType.java,v 1.5 2009/03/09 16:03:58 fros4943 Exp $
*/ */
package se.sics.cooja.mspmote; package se.sics.cooja.mspmote;
@ -35,23 +35,24 @@ import java.awt.*;
import java.io.File; import java.io.File;
import java.net.URL; import java.net.URL;
import javax.swing.*; import javax.swing.*;
import org.apache.log4j.Logger; import org.apache.log4j.Logger;
import se.sics.cooja.*; import se.sics.cooja.*;
import se.sics.cooja.dialogs.CompileContiki;
import se.sics.cooja.dialogs.MessageList;
import se.sics.cooja.dialogs.MessageList.MessageContainer;
@ClassDescription("Sky Mote Type") @ClassDescription("Sky Mote Type")
@AbstractionLevelDescription("Emulated level") @AbstractionLevelDescription("Emulated level")
public class SkyMoteType extends MspMoteType { public class SkyMoteType extends MspMoteType {
private static Logger logger = Logger.getLogger(SkyMoteType.class); private static Logger logger = Logger.getLogger(SkyMoteType.class);
public static final String target = "sky";
public static final String targetNice = "Sky";
public SkyMoteType() { public SkyMoteType() {
} }
public SkyMoteType(String identifier) { public SkyMoteType(String identifier) {
setIdentifier(identifier); setIdentifier(identifier);
setDescription(targetNice + " Mote Type #" + identifier); setDescription("Sky Mote Type #" + identifier);
} }
public Icon getMoteTypeIcon() { public Icon getMoteTypeIcon() {
@ -76,12 +77,15 @@ public class SkyMoteType extends MspMoteType {
return new SkyMote(this, simulation); return new SkyMote(this, simulation);
} }
public boolean configureAndInit(Container parentContainer, Simulation simulation, public boolean configureAndInit(Container parentContainer, Simulation simulation, boolean visAvailable)
boolean visAvailable) throws MoteTypeCreationException { throws MoteTypeCreationException {
/* SPECIAL CASE: Cooja started in applet.
* Use preconfigured Contiki firmware */
if (GUI.isVisualizedInApplet()) { if (GUI.isVisualizedInApplet()) {
String firmware = GUI.getExternalToolsSetting("SKY_FIRMWARE", ""); String firmware = GUI.getExternalToolsSetting("SKY_FIRMWARE", "");
if (!firmware.equals("")) { if (!firmware.equals("")) {
setELFFile(new File(firmware)); setContikiFirmwareFile(new File(firmware));
JOptionPane.showMessageDialog(GUI.getTopParentContainer(), JOptionPane.showMessageDialog(GUI.getTopParentContainer(),
"Creating mote type from precompiled firmware: " + firmware, "Creating mote type from precompiled firmware: " + firmware,
"Compiled firmware file available", JOptionPane.INFORMATION_MESSAGE); "Compiled firmware file available", JOptionPane.INFORMATION_MESSAGE);
@ -93,7 +97,90 @@ public class SkyMoteType extends MspMoteType {
} }
} }
return configureAndInitMspType(parentContainer, simulation, visAvailable, target, targetNice); /* If visualized, show compile dialog and let user configure */
if (visAvailable) {
/* Create unique identifier */
if (getIdentifier() == null) {
int counter = 0;
boolean identifierOK = false;
while (!identifierOK) {
identifierOK = true;
counter++;
setIdentifier("sky" + counter);
for (MoteType existingMoteType : simulation.getMoteTypes()) {
if (existingMoteType == this) {
continue;
}
if (existingMoteType.getIdentifier().equals(getIdentifier())) {
identifierOK = false;
break;
}
}
}
}
/* Create initial description */
if (getDescription() == null) {
setDescription("Sky Mote Type #" + getIdentifier());
}
return SkyCompileDialog.showDialog(parentContainer, simulation, this);
}
/* Not visualized: Compile Contiki immediately */
if (getIdentifier() == null) {
throw new MoteTypeCreationException("No identifier");
}
final MessageList compilationOutput = new MessageList();
if (getCompileCommands() != null) {
/* Handle multiple compilation commands one by one */
String[] arr = getCompileCommands().split("\n");
for (String cmd: arr) {
if (cmd.trim().isEmpty()) {
continue;
}
try {
CompileContiki.compile(
cmd,
null /* No output file */,
getContikiSourceFile().getParentFile(),
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 10 compilation errors to console */
MessageContainer[] messages = compilationOutput.getMessages();
for (int i=messages.length-10; i < messages.length; i++) {
if (i < 0) {
continue;
}
logger.fatal(">> " + messages[i]);
}
logger.fatal("Compilation error: " + e.getMessage());
throw newException;
}
}
}
if (getContikiFirmwareFile() == null ||
!getContikiFirmwareFile().exists()) {
throw new MoteTypeCreationException("Contiki firmware file does not exist: " + getContikiFirmwareFile());
}
return true;
} }
} }