[cooja] ContikiMoteType: Non-functional indention and code style updates

This commit is contained in:
Enrico Joerns 2014-07-23 11:46:09 +02:00
parent 434c4db1a2
commit dae92d93bb

View file

@ -27,7 +27,6 @@
* SUCH DAMAGE.
*
*/
package org.contikios.cooja.contikimote;
import java.awt.Container;
@ -41,6 +40,7 @@ import java.lang.reflect.Method;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.Random;
@ -93,7 +93,8 @@ import org.contikios.cooja.util.StringUtils;
@ClassDescription("Cooja mote")
@AbstractionLevelDescription("OS level")
public class ContikiMoteType implements MoteType {
private static Logger logger = Logger.getLogger(ContikiMoteType.class);
private static final Logger logger = Logger.getLogger(ContikiMoteType.class);
public static final String ID_PREFIX = "mtype";
@ -121,6 +122,7 @@ public class ContikiMoteType implements MoteType {
* Communication stacks in Contiki.
*/
public enum NetworkStack {
DEFAULT, MANUAL;
public String manualHeader = "netstack-conf-example.h";
@ -177,10 +179,15 @@ public class ContikiMoteType implements MoteType {
/* For internal use only: using during Contiki compilation. */
private File contikiApp = null; /* Contiki application: hello-world.c */
public File libSource = null; /* JNI library: obj_cooja/mtype1.c */
public File libFile = null; /* JNI library: obj_cooja/mtype1.lib */
public File archiveFile = null; /* Contiki archive: obj_cooja/mtype1.a */
public File mapFile = null; /* Contiki map: obj_cooja/mtype1.map */
public String javaClassName = null; /* Loading Java class name: Lib1 */
private String[] coreInterfaces = null;
@ -223,8 +230,8 @@ public class ContikiMoteType implements MoteType {
}
/* Compile Contiki from dialog */
boolean compileOK =
ContikiMoteCompileDialog.showDialog(parentContainer, simulation, this);
boolean compileOK
= ContikiMoteCompileDialog.showDialog(parentContainer, simulation, this);
if (!compileOK) {
return false;
}
@ -291,8 +298,7 @@ public class ContikiMoteType implements MoteType {
env
);
} catch (Exception e) {
throw (MoteTypeCreationException) new MoteTypeCreationException(
"Error when creating environment: " + e.getMessage()).initCause(e);
throw new MoteTypeCreationException("Error when creating environment: " + e.getMessage(), e);
}
String[] envOneDimension = new String[env.length];
for (int i = 0; i < env.length; i++) {
@ -322,8 +328,8 @@ public class ContikiMoteType implements MoteType {
true
);
} catch (Exception e) {
MoteTypeCreationException newException =
new MoteTypeCreationException("Mote type creation failed: " + e.getMessage());
MoteTypeCreationException newException
= new MoteTypeCreationException("Mote type creation failed: " + e.getMessage());
newException = (MoteTypeCreationException) newException.initCause(e);
newException.setCompilationOutput(compilationOutput);
@ -342,8 +348,8 @@ public class ContikiMoteType implements MoteType {
}
/* Make sure compiled firmware exists */
if (getContikiFirmwareFile() == null ||
!getContikiFirmwareFile().exists()) {
if (getContikiFirmwareFile() == null
|| !getContikiFirmwareFile().exists()) {
throw new MoteTypeCreationException("Contiki firmware file does not exist: " + getContikiFirmwareFile());
}
}
@ -363,8 +369,10 @@ public class ContikiMoteType implements MoteType {
/**
* For internal use.
*
* This method creates a core communicator linking a Contiki library and a Java class.
* It furthermore parses library Contiki memory addresses and creates the initial memory.
* This method creates a core communicator linking a Contiki library and a
* Java class.
* It furthermore parses library Contiki memory addresses and creates the
* initial memory.
*
* @throws MoteTypeCreationException
*/
@ -375,8 +383,8 @@ public class ContikiMoteType implements MoteType {
"Core communicator already used: " + myCoreComm.getClass().getName());
}
if (getContikiFirmwareFile() == null ||
!getContikiFirmwareFile().exists()) {
if (getContikiFirmwareFile() == null
|| !getContikiFirmwareFile().exists()) {
throw new MoteTypeCreationException("Library file could not be found: " + getContikiFirmwareFile());
}
@ -426,8 +434,8 @@ public class ContikiMoteType implements MoteType {
} else {
/* Parse command output */
if (mapFile == null ||
!mapFile.exists()) {
if (mapFile == null
|| !mapFile.exists()) {
throw new MoteTypeCreationException("Map file " + mapFile + " could not be found");
}
String[] mapData = loadMapFile(mapFile);
@ -453,34 +461,34 @@ public class ContikiMoteType implements MoteType {
}
if (dataSectionAddr >= 0) {
logger.info(getContikiFirmwareFile().getName() +
": data section at 0x" + Integer.toHexString(dataSectionAddr) +
" (" + dataSectionSize + " == 0x" + Integer.toHexString(dataSectionSize) + " bytes)");
logger.info(getContikiFirmwareFile().getName()
+ ": data section at 0x" + Integer.toHexString(dataSectionAddr)
+ " (" + dataSectionSize + " == 0x" + Integer.toHexString(dataSectionSize) + " bytes)");
} else {
logger.fatal(getContikiFirmwareFile().getName() + ": no data section found");
}
if (bssSectionAddr >= 0) {
logger.info(getContikiFirmwareFile().getName() +
": BSS section at 0x" + Integer.toHexString(bssSectionAddr) +
" (" + bssSectionSize + " == 0x" + Integer.toHexString(bssSectionSize) + " bytes)");
logger.info(getContikiFirmwareFile().getName()
+ ": BSS section at 0x" + Integer.toHexString(bssSectionAddr)
+ " (" + bssSectionSize + " == 0x" + Integer.toHexString(bssSectionSize) + " bytes)");
} else {
logger.fatal(getContikiFirmwareFile().getName() + ": no BSS section found");
}
if (commonSectionAddr >= 0) {
logger.info(getContikiFirmwareFile().getName() +
": common section at 0x" + Integer.toHexString(commonSectionAddr) +
" (" + commonSectionSize + " == 0x" + Integer.toHexString(commonSectionSize) + " bytes)");
logger.info(getContikiFirmwareFile().getName()
+ ": common section at 0x" + Integer.toHexString(commonSectionAddr)
+ " (" + commonSectionSize + " == 0x" + Integer.toHexString(commonSectionSize) + " bytes)");
} else {
logger.info(getContikiFirmwareFile().getName() + ": no common section found");
}
if (readonlySectionAddr >= 0) {
logger.info(getContikiFirmwareFile().getName() +
": readonly section at 0x" + Integer.toHexString(readonlySectionAddr) +
" (" + readonlySectionSize + " == 0x" + Integer.toHexString(readonlySectionSize) + " bytes)");
logger.info(getContikiFirmwareFile().getName()
+ ": readonly section at 0x" + Integer.toHexString(readonlySectionAddr)
+ " (" + readonlySectionSize + " == 0x" + Integer.toHexString(readonlySectionSize) + " bytes)");
} else {
logger.warn(getContikiFirmwareFile().getName() + ": no readonly section found");
}
if (addresses.size() == 0) {
if (addresses.isEmpty()) {
throw new MoteTypeCreationException("Library variables parsing failed");
}
if (dataSectionAddr <= 0 || dataSectionSize <= 0
@ -493,8 +501,7 @@ public class ContikiMoteType implements MoteType {
int referenceVar = addresses.get("referenceVar");
myCoreComm.setReferenceAddress(referenceVar);
} catch (Exception e) {
throw (MoteTypeCreationException) new MoteTypeCreationException(
"JNI call error: " + e.getMessage()).initCause(e);
throw new MoteTypeCreationException("JNI call error: " + e.getMessage(), e);
}
/* We first need the value of Contiki's referenceVar, which tells us the
@ -515,8 +522,8 @@ public class ContikiMoteType implements MoteType {
tmp.setMemorySegment(bssSectionAddr, bss);
offset = varMem.getIntValueOf("referenceVar");
logger.info(getContikiFirmwareFile().getName() +
": offsetting Cooja mote address space: " + offset);
logger.info(getContikiFirmwareFile().getName()
+ ": offsetting Cooja mote address space: " + offset);
}
/* Create initial memory: data+bss+optional common */
@ -616,8 +623,8 @@ public class ContikiMoteType implements MoteType {
public static boolean parseCommandData(String[] output, HashMap<String, Integer> addresses) {
int nrNew = 0, nrOld = 0, nrMismatch = 0;
Pattern pattern =
Pattern.compile(Cooja.getExternalToolsSetting("COMMAND_VAR_NAME_ADDRESS"));
Pattern pattern
= Pattern.compile(Cooja.getExternalToolsSetting("COMMAND_VAR_NAME_ADDRESS"));
for (String line : output) {
Matcher matcher = pattern.matcher(line);
@ -650,7 +657,6 @@ public class ContikiMoteType implements MoteType {
logger.debug("Command response parsing summary: Added " + nrNew
+ " variables. Found " + nrOld + " old variables");
}*/
return (nrNew + nrOld) > 0;
}
@ -664,6 +670,7 @@ public class ContikiMoteType implements MoteType {
public void getCoreMemory(SectionMoteMemory mem) {
for (int i = 0; i < mem.getNumberOfSections(); i++) {
int startAddr = mem.getSectionNativeAddress(i); /* native address space */
int size = mem.getSizeOfSection(i);
byte[] data = mem.getDataOfSection(i);
getCoreMemory(startAddr, size, data);
@ -750,10 +757,10 @@ public class ContikiMoteType implements MoteType {
return varAddrInteger.intValue();
}
String regExp =
Cooja.getExternalToolsSetting("MAPFILE_VAR_ADDRESS_1") +
varName +
Cooja.getExternalToolsSetting("MAPFILE_VAR_ADDRESS_2");
String regExp
= Cooja.getExternalToolsSetting("MAPFILE_VAR_ADDRESS_1")
+ varName
+ Cooja.getExternalToolsSetting("MAPFILE_VAR_ADDRESS_2");
String retString = getFirstMatchGroup(mapFileData, regExp, 1);
if (retString != null) {
@ -792,7 +799,7 @@ public class ContikiMoteType implements MoteType {
* @return Variable names found in the data and bss section
*/
public static String[] getMapFileVarNames(String[] mapFileData) {
ArrayList<String> varNames = new ArrayList<String>();
ArrayList<String> varNames = new ArrayList<>();
String[] dataVariables = getAllVariableNames(
mapFileData,
@ -806,16 +813,14 @@ public class ContikiMoteType implements MoteType {
mapFileData,
parseMapBssSectionAddr(mapFileData),
parseMapBssSectionAddr(mapFileData) + parseMapBssSectionSize(mapFileData));
for (String v: bssVariables) {
varNames.add(v);
}
varNames.addAll(Arrays.asList(bssVariables));
return varNames.toArray(new String[0]);
}
private static String[] getAllVariableNames(String[] lines,
int startAddress, int endAddress) {
ArrayList<String> varNames = new ArrayList<String>();
ArrayList<String> varNames = new ArrayList<>();
Pattern pattern = Pattern.compile(Cooja.getExternalToolsSetting("MAPFILE_VAR_NAME"));
for (String line : lines) {
@ -832,9 +837,9 @@ public class ContikiMoteType implements MoteType {
protected int getVariableSize(Vector<String> lines, String varName) {
Pattern pattern = Pattern.compile(
Cooja.getExternalToolsSetting("MAPFILE_VAR_SIZE_1") +
varName +
Cooja.getExternalToolsSetting("MAPFILE_VAR_SIZE_2"));
Cooja.getExternalToolsSetting("MAPFILE_VAR_SIZE_1")
+ varName
+ Cooja.getExternalToolsSetting("MAPFILE_VAR_SIZE_2"));
for (int i = 0; i < lines.size(); i++) {
Matcher matcher = pattern.matcher(lines.get(i));
if (matcher.find()) {
@ -845,8 +850,8 @@ public class ContikiMoteType implements MoteType {
}
private static int parseFirstHexInt(String regexp, String[] data) {
String retString =
getFirstMatchGroup(data, regexp, 1);
String retString
= getFirstMatchGroup(data, regexp, 1);
if (retString != null) {
return Integer.parseInt(retString.trim(), 16);
@ -862,6 +867,7 @@ public class ContikiMoteType implements MoteType {
}
return parseFirstHexInt(regexp, mapFileData);
}
public static int parseMapDataSectionSize(String[] mapFileData) {
String regexp = Cooja.getExternalToolsSetting("MAPFILE_DATA_SIZE", "");
if (regexp.equals("")) {
@ -869,6 +875,7 @@ public class ContikiMoteType implements MoteType {
}
return parseFirstHexInt(regexp, mapFileData);
}
public static int parseMapBssSectionAddr(String[] mapFileData) {
String regexp = Cooja.getExternalToolsSetting("MAPFILE_BSS_START", "");
if (regexp.equals("")) {
@ -876,6 +883,7 @@ public class ContikiMoteType implements MoteType {
}
return parseFirstHexInt(regexp, mapFileData);
}
public static int parseMapBssSectionSize(String[] mapFileData) {
String regexp = Cooja.getExternalToolsSetting("MAPFILE_BSS_SIZE", "");
if (regexp.equals("")) {
@ -883,6 +891,7 @@ public class ContikiMoteType implements MoteType {
}
return parseFirstHexInt(regexp, mapFileData);
}
public static int parseMapCommonSectionAddr(String[] mapFileData) {
String regexp = Cooja.getExternalToolsSetting("MAPFILE_COMMON_START", "");
if (regexp.equals("")) {
@ -890,6 +899,7 @@ public class ContikiMoteType implements MoteType {
}
return parseFirstHexInt(regexp, mapFileData);
}
public static int parseMapCommonSectionSize(String[] mapFileData) {
String regexp = Cooja.getExternalToolsSetting("MAPFILE_COMMON_SIZE", "");
if (regexp.equals("")) {
@ -905,6 +915,7 @@ public class ContikiMoteType implements MoteType {
}
return parseFirstHexInt(regexp, output);
}
public static int parseCommandDataSectionSize(String[] output) {
String regexp = Cooja.getExternalToolsSetting("COMMAND_DATA_END", "");
if (regexp.equals("")) {
@ -921,6 +932,7 @@ public class ContikiMoteType implements MoteType {
}
return end - start;
}
public static int parseCommandBssSectionAddr(String[] output) {
String regexp = Cooja.getExternalToolsSetting("COMMAND_BSS_START", "");
if (regexp.equals("")) {
@ -928,6 +940,7 @@ public class ContikiMoteType implements MoteType {
}
return parseFirstHexInt(regexp, output);
}
public static int parseCommandBssSectionSize(String[] output) {
String regexp = Cooja.getExternalToolsSetting("COMMAND_BSS_END", "");
if (regexp.equals("")) {
@ -944,6 +957,7 @@ public class ContikiMoteType implements MoteType {
}
return end - start;
}
public static int parseCommandCommonSectionAddr(String[] output) {
String regexp = Cooja.getExternalToolsSetting("COMMAND_COMMON_START", "");
if (regexp.equals("")) {
@ -951,6 +965,7 @@ public class ContikiMoteType implements MoteType {
}
return parseFirstHexInt(regexp, output);
}
public static int parseCommandCommonSectionSize(String[] output) {
String regexp = Cooja.getExternalToolsSetting("COMMAND_COMMON_END", "");
if (regexp.equals("")) {
@ -971,6 +986,7 @@ public class ContikiMoteType implements MoteType {
private static int parseCommandReadonlySectionAddr(String[] output) {
return parseFirstHexInt("^([0-9A-Fa-f]*)[ \t]t[ \t].text$", output);
}
private static int parseCommandReadonlySectionSize(String[] output) {
int start = parseCommandReadonlySectionAddr(output);
if (start < 0) {
@ -984,10 +1000,10 @@ public class ContikiMoteType implements MoteType {
}
private static int getRelVarAddr(String mapFileData[], String varName) {
String regExp =
Cooja.getExternalToolsSetting("MAPFILE_VAR_ADDRESS_1") +
varName +
Cooja.getExternalToolsSetting("MAPFILE_VAR_ADDRESS_2");
String regExp
= Cooja.getExternalToolsSetting("MAPFILE_VAR_ADDRESS_1")
+ varName
+ Cooja.getExternalToolsSetting("MAPFILE_VAR_ADDRESS_2");
String retString = getFirstMatchGroup(mapFileData, regExp, 1);
if (retString != null) {
@ -1012,7 +1028,7 @@ public class ContikiMoteType implements MoteType {
* @return Execution response, or null at failure
*/
public static String[] loadCommandData(File libraryFile) {
ArrayList<String> output = new ArrayList<String>();
ArrayList<String> output = new ArrayList<>();
try {
String command = Cooja.getExternalToolsSetting("PARSE_COMMAND");
@ -1040,11 +1056,11 @@ public class ContikiMoteType implements MoteType {
}
input.close();
if (output == null || output.size() == 0) {
if (output == null || output.isEmpty()) {
return null;
}
return output.toArray(new String[0]);
} catch (Exception err) {
} catch (IOException err) {
logger.fatal("Command error: " + err.getMessage(), err);
return null;
}
@ -1116,10 +1132,8 @@ public class ContikiMoteType implements MoteType {
@Override
public void setMoteInterfaceClasses(Class<? extends MoteInterface>[] moteInterfaces) {
this.moteInterfacesClasses = new ArrayList<Class<? extends MoteInterface>>();
for (Class<? extends MoteInterface> intf: moteInterfaces) {
this.moteInterfacesClasses.add(intf);
}
this.moteInterfacesClasses = new ArrayList<>();
this.moteInterfacesClasses.addAll(Arrays.asList(moteInterfaces));
}
/**
@ -1146,9 +1160,7 @@ public class ContikiMoteType implements MoteType {
}
}
fileInputStream.close();
} catch (NoSuchAlgorithmException e) {
return null;
} catch (IOException e) {
} catch (NoSuchAlgorithmException | IOException e) {
return null;
}
return messageDigest.digest();
@ -1261,7 +1273,7 @@ public class ContikiMoteType implements MoteType {
@Override
public Collection<Element> getConfigXML(Simulation simulation) {
ArrayList<Element> config = new ArrayList<Element>();
ArrayList<Element> config = new ArrayList<>();
Element element;
element = new Element("identifier");
@ -1311,91 +1323,87 @@ public class ContikiMoteType implements MoteType {
for (Element element : configXML) {
String name = element.getName();
if (name.equals("identifier")) {
switch (name) {
case "identifier":
identifier = element.getText();
} else if (name.equals("description")) {
break;
case "description":
description = element.getText();
} else if (name.equals("contikiapp") || name.equals("source")) {
break;
case "contikiapp":
case "source":
File file = new File(element.getText());
if (!file.exists()) {
file = simulation.getCooja().restorePortablePath(file);
}
setContikiSourceFile(file);
} setContikiSourceFile(file);
/* XXX Do not load the generated firmware. Instead, load the unique library file directly */
File contikiFirmware = new File(
getContikiSourceFile().getParentFile(),
"obj_cooja/" + getIdentifier() + librarySuffix);
setContikiFirmwareFile(contikiFirmware);
} else if (name.equals("commands")) {
break;
case "commands":
compileCommands = element.getText();
} else if (name.equals("symbols")) {
break;
case "symbols":
hasSystemSymbols = Boolean.parseBoolean(element.getText());
} else if (name.equals("commstack")) {
break;
case "commstack":
logger.warn("The Cooja communication stack config was removed: " + element.getText());
logger.warn("Instead assuming default network stack.");
netStack = NetworkStack.DEFAULT;
} else if (name.equals("netstack")) {
break;
case "netstack":
netStack = NetworkStack.parseConfig(element.getText());
} else if (name.equals("moteinterface")) {
break;
case "moteinterface":
String intfClass = element.getText().trim();
/* Backwards compatibility: se.sics -> org.contikios */
if (intfClass.startsWith("se.sics")) {
intfClass = intfClass.replaceFirst("se\\.sics", "org.contikios");
}
Class<? extends MoteInterface> moteInterfaceClass =
simulation.getCooja().tryLoadClass(
} Class<? extends MoteInterface> moteInterfaceClass
= simulation.getCooja().tryLoadClass(
this, MoteInterface.class, intfClass);
if (moteInterfaceClass == null) {
logger.warn("Can't find mote interface class: " + intfClass);
} else {
moteInterfacesClasses.add(moteInterfaceClass);
}
} else if (
name.equals("contikibasedir") ||
name.equals("contikicoredir") ||
name.equals("projectdir") ||
name.equals("compilefile") ||
name.equals("process") ||
name.equals("sensor") ||
name.equals("coreinterface")) {
} break;
case "contikibasedir":
case "contikicoredir":
case "projectdir":
case "compilefile":
case "process":
case "sensor":
case "coreinterface":
/* Backwards compatibility: old cooja mote type is being loaded */
if (!warnedOldVersion) {
warnedOldVersion = true;
logger.warn("Old simulation config detected: Cooja mote types may not load correctly");
}
if (name.equals("compilefile")) {
} if (name.equals("compilefile")) {
if (element.getText().endsWith(".c")) {
File potentialFile = new File(element.getText());
if (potentialFile.exists()) {
oldVersionSource = potentialFile;
}
}
}
} else {
} break;
default:
logger.fatal("Unrecognized entry in loaded configuration: " + name);
break;
}
}
/* Create initial core interface dependencies */
Class<? extends MoteInterface>[] arr =
new Class[moteInterfacesClasses.size()];
Class<? extends MoteInterface>[] arr
= new Class[moteInterfacesClasses.size()];
moteInterfacesClasses.toArray(arr);
setCoreInterfaces(ContikiMoteType.getRequiredCoreInterfaces(arr));
/* Backwards compatibility: old cooja mote type is being loaded */
if (getContikiSourceFile() == null &&
warnedOldVersion &&
oldVersionSource != null)
{
if (getContikiSourceFile() == null
&& warnedOldVersion
&& oldVersionSource != null) {
/* Guess Contiki source */
setContikiSourceFile(oldVersionSource);
logger.info("Guessing Contiki source: " + oldVersionSource.getAbsolutePath());
@ -1404,8 +1412,8 @@ public class ContikiMoteType implements MoteType {
logger.info("Guessing Contiki firmware: " + getContikiFirmwareFile().getAbsolutePath());
/* Guess compile commands */
String compileCommands =
"make " + getExpectedFirmwareFile(oldVersionSource).getName() + " TARGET=cooja";
String compileCommands
= "make " + getExpectedFirmwareFile(oldVersionSource).getName() + " TARGET=cooja";
logger.info("Guessing compile commands: " + compileCommands);
setCompileCommands(compileCommands);
}
@ -1417,7 +1425,7 @@ public class ContikiMoteType implements MoteType {
public static String[] getRequiredCoreInterfaces(
Class<? extends MoteInterface>[] moteInterfaces) {
/* Extract Contiki dependencies from currently selected mote interfaces */
ArrayList<String> coreInterfacesList = new ArrayList<String>();
ArrayList<String> coreInterfacesList = new ArrayList<>();
for (Class<? extends MoteInterface> intf : moteInterfaces) {
if (!ContikiMoteInterface.class.isAssignableFrom(intf)) {
continue;
@ -1437,10 +1445,7 @@ public class ContikiMoteType implements MoteType {
if (deps == null || deps.length == 0) {
continue;
}
for (String dep: deps) {
coreInterfacesList.add(dep);
}
coreInterfacesList.addAll(Arrays.asList(deps));
}
String[] coreInterfaces = new String[coreInterfacesList.size()];