Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Restore ID setting for MSP430 mote types #683

Merged
merged 3 commits into from
Oct 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion java/org/contikios/cooja/Cooja.java
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@
import org.contikios.cooja.dialogs.MessageList;
import org.contikios.cooja.dialogs.MessageListUI;
import org.contikios.cooja.dialogs.ProjectDirectoriesDialog;
import org.contikios.cooja.mote.BaseContikiMoteType;
import org.contikios.cooja.motes.DisturberMoteType;
import org.contikios.cooja.motes.ImportAppMoteType;
import org.contikios.cooja.mspmote.SkyMoteType;
Expand Down Expand Up @@ -3132,7 +3133,7 @@ private Simulation createSimulation(Element root, boolean quick, boolean rewrite
}
}
for (var existingIdentifier : readNames) {
String newID = ContikiMoteType.generateUniqueMoteTypeID(reserved);
String newID = BaseContikiMoteType.generateUniqueMoteTypeID("mtype", reserved);
moteTypeIDMappings.put(existingIdentifier, newID);
reserved.add(newID);
}
Expand Down
33 changes: 0 additions & 33 deletions java/org/contikios/cooja/contikimote/ContikiMoteType.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,7 @@
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Random;
import java.util.Scanner;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.logging.log4j.LogManager;
Expand Down Expand Up @@ -117,11 +115,6 @@ public class ContikiMoteType extends BaseContikiMoteType {
*/
private static final String librarySuffix = ".cooja";

/**
* Random generator for generating a unique mote ID.
*/
private static final Random rnd = new Random();

private final Cooja gui;

/**
Expand Down Expand Up @@ -272,13 +265,6 @@ public LinkedHashMap<String, String> getCompilationEnvironment() {

@Override
protected AbstractCompileDialog createCompilationDialog(Simulation sim, MoteTypeConfig cfg) {
if (getIdentifier() == null) {
var usedNames = new HashSet<String>();
for (var mote : sim.getMoteTypes()) {
usedNames.add(mote.getIdentifier());
}
setIdentifier(generateUniqueMoteTypeID(usedNames));
}
return new ContikiMoteCompileDialog(sim, this, cfg);
}

Expand Down Expand Up @@ -837,25 +823,6 @@ private static MoteTypeCreationException createException(String message, Throwab
return e;
}

/**
* Generates a unique Cooja mote type ID.
*
* @param reservedIdentifiers Already reserved identifiers
* @return Unique mote type ID.
*/
public static String generateUniqueMoteTypeID(Set<String> reservedIdentifiers) {
String testID = "";
boolean available = false;

while (!available) {
testID = "mtype" + rnd.nextInt(1000000000);
available = !reservedIdentifiers.contains(testID);
// FIXME: add check that the library name is not already used.
}

return testID;
}

@Override
protected void appendVisualizerInfo(StringBuilder sb) {
/* JNI class */
Expand Down
33 changes: 33 additions & 0 deletions java/org/contikios/cooja/mote/BaseContikiMoteType.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,11 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Random;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.swing.Action;
Expand Down Expand Up @@ -87,6 +90,9 @@ public abstract class BaseContikiMoteType implements MoteType {
/** MoteInterface classes used by the mote type. */
protected final ArrayList<Class<? extends MoteInterface>> moteInterfaceClasses = new ArrayList<>();

/** Random generator for generating a unique mote ID. */
private static final Random rnd = new Random();

/** Returns file name extension for firmware. */
public abstract String getMoteType();

Expand Down Expand Up @@ -115,6 +121,24 @@ public void setIdentifier(String identifier) {
this.identifier = identifier;
}

/**
* Generates a unique mote type ID.
*
* @param prefix Beginning of name
* @param reservedIdentifiers Already reserved identifiers
* @return Unique mote type ID.
*/
public static String generateUniqueMoteTypeID(String prefix, Set<String> reservedIdentifiers) {
String testID = "";
boolean available = false;
while (!available) {
testID = prefix + rnd.nextInt(1000000000);
available = !reservedIdentifiers.contains(testID);
// FIXME: add check that the library name is not already used.
}
return testID;
}

@Override
public ProjectConfig getConfig() {
return projectConfig;
Expand Down Expand Up @@ -288,6 +312,15 @@ protected boolean setBaseConfigXML(Simulation sim, Collection<Element> configXML
@Override
public boolean configureAndInit(Container top, Simulation sim, boolean vis) throws MoteTypeCreationException {
if (vis && !sim.isQuickSetup()) {
if (getIdentifier() == null) {
var usedNames = new HashSet<String>();
for (var mote : sim.getMoteTypes()) {
usedNames.add(mote.getIdentifier());
}
// The "mtype" prefix for ContikiMoteType is hardcoded elsewhere, so use that instead of "cooja".
var namePrefix = getMoteType();
setIdentifier(generateUniqueMoteTypeID("cooja".equals(namePrefix) ? "mtype" : namePrefix, usedNames));
}
var currDesc = getDescription();
var desc = currDesc == null ? getMoteName() + " Mote Type #" + (sim.getMoteTypes().length + 1) : currDesc;
final var source = getContikiSourceFile();
Expand Down