rewriting mote interfaces for faster simulation execution.

cfs interface
This commit is contained in:
fros4943 2008-10-28 09:53:23 +00:00
parent ed9063719f
commit f9d2581e06

View file

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2006, Swedish Institute of Computer Science. * Copyright (c) 2008, 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
@ -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: ContikiCFS.java,v 1.4 2007/01/09 10:05:19 fros4943 Exp $ * $Id: ContikiCFS.java,v 1.5 2008/10/28 09:53:23 fros4943 Exp $
*/ */
package se.sics.cooja.contikimote.interfaces; package se.sics.cooja.contikimote.interfaces;
@ -38,18 +38,17 @@ import java.awt.event.ActionListener;
import java.io.*; import java.io.*;
import java.util.*; import java.util.*;
import javax.swing.*; import javax.swing.*;
import org.apache.log4j.Logger; import org.apache.log4j.Logger;
import org.jdom.Element; import org.jdom.Element;
import se.sics.cooja.*; import se.sics.cooja.*;
import se.sics.cooja.contikimote.ContikiMoteInterface; import se.sics.cooja.contikimote.ContikiMoteInterface;
import se.sics.cooja.interfaces.PolledAfterActiveTicks;
/** /**
* The class represents a Contiki filesystem, for example an EEPROM or external * Contiki FileSystem (CFS) interface (such as external flash).
* flash memory. *
* * Contiki variables:
* It needs read/write access to the following core variables:
* <ul> * <ul>
* <li>char[] simCFSData * <li>char[] simCFSData
* <li>char simCFSChanged (1=filesystem has been altered) * <li>char simCFSChanged (1=filesystem has been altered)
@ -57,28 +56,28 @@ import se.sics.cooja.contikimote.ContikiMoteInterface;
* <li>int simCFSWritten (bytes written to filesystem) * <li>int simCFSWritten (bytes written to filesystem)
* </ul> * </ul>
* <p> * <p>
* Dependency core interfaces are: *
* Core interface:
* <ul> * <ul>
* <li>cfs_interface * <li>cfs_interface
* </ul> * </ul>
* <p> * <p>
* This observable is changed and notifies observers whenever the filesystem has * This observable notifies when the filesystem is used (read/write).
* been read from or written to. *
* * @author Fredrik Österlind
* @author Fredrik Osterlind
*/ */
@ClassDescription("Filesystem") @ClassDescription("Filesystem (CFS)")
public class ContikiCFS extends MoteInterface implements ContikiMoteInterface { public class ContikiCFS extends MoteInterface implements ContikiMoteInterface, PolledAfterActiveTicks {
private static Logger logger = Logger.getLogger(ContikiCFS.class); private static Logger logger = Logger.getLogger(ContikiCFS.class);
public int FILESYSTEM_SIZE = 60*1024; public int FILESYSTEM_SIZE = 60*1024; /* Configure me */
private Mote mote = null; private Mote mote = null;
private SectionMoteMemory moteMem = null; private SectionMoteMemory moteMem = null;
private int lastRead = 0; private int lastRead = 0;
private int lastWritten = 0; private int lastWritten = 0;
/** /**
* Approximate energy consumption of every character read from filesystem (mQ). * Approximate energy consumption of every character read from filesystem (mQ).
*/ */
@ -89,7 +88,7 @@ public class ContikiCFS extends MoteInterface implements ContikiMoteInterface {
/** /**
* Creates an interface to the filesystem at mote. * Creates an interface to the filesystem at mote.
* *
* @param mote * @param mote
* Mote. * Mote.
* @see Mote * @see Mote
@ -110,9 +109,11 @@ public class ContikiCFS extends MoteInterface implements ContikiMoteInterface {
return new String[]{"cfs_interface"}; return new String[]{"cfs_interface"};
} }
public void doActionsBeforeTick() { private TimeEvent doneEvent = new TimeEvent(0) {
// Nothing to do public void execute(int t) {
} myEnergyConsumption = 0.0;
}
};
public void doActionsAfterTick() { public void doActionsAfterTick() {
if (moteMem.getByteValueOf("simCFSChanged") == 1) { if (moteMem.getByteValueOf("simCFSChanged") == 1) {
@ -123,19 +124,21 @@ public class ContikiCFS extends MoteInterface implements ContikiMoteInterface {
moteMem.setIntValueOf("simCFSWritten", 0); moteMem.setIntValueOf("simCFSWritten", 0);
moteMem.setByteValueOf("simCFSChanged", (byte) 0); moteMem.setByteValueOf("simCFSChanged", (byte) 0);
myEnergyConsumption = myEnergyConsumption =
ENERGY_CONSUMPTION_PER_READ_CHAR_mQ*lastRead + ENERGY_CONSUMPTION_PER_READ_CHAR_mQ*lastRead +
ENERGY_CONSUMPTION_PER_WRITTEN_CHAR_mQ*lastWritten; ENERGY_CONSUMPTION_PER_WRITTEN_CHAR_mQ*lastWritten;
this.setChanged(); this.setChanged();
this.notifyObservers(mote); this.notifyObservers(mote);
} else
myEnergyConsumption = 0.0; /* Reset energy consumption */
mote.getSimulation().addEvent(doneEvent, mote.getSimulation().getSimulationTime());
}
} }
/** /**
* Set filesystem data. * Set filesystem data.
* *
* @param data Data * @param data Data
* @return True if operation successful * @return True if operation successful
*/ */
@ -151,7 +154,7 @@ public class ContikiCFS extends MoteInterface implements ContikiMoteInterface {
/** /**
* Get filesystem data. * Get filesystem data.
* *
* @return Filesystem data * @return Filesystem data
*/ */
public byte[] getFilesystemData() { public byte[] getFilesystemData() {
@ -176,7 +179,7 @@ public class ContikiCFS extends MoteInterface implements ContikiMoteInterface {
JPanel panel = new JPanel(); JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS)); panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
final JLabel lastTimeLabel = new JLabel("Last change at: [unknown]"); final JLabel lastTimeLabel = new JLabel("Last change at: ?");
final JLabel lastReadLabel = new JLabel("Last change read bytes: 0"); final JLabel lastReadLabel = new JLabel("Last change read bytes: 0");
final JLabel lastWrittenLabel = new JLabel("Last change wrote bytes: 0"); final JLabel lastWrittenLabel = new JLabel("Last change wrote bytes: 0");
final JButton uploadButton = new JButton("Upload binary file"); final JButton uploadButton = new JButton("Upload binary file");
@ -196,7 +199,7 @@ public class ContikiCFS extends MoteInterface implements ContikiMoteInterface {
} }
} }
}); });
Observer observer; Observer observer;
this.addObserver(observer = new Observer() { this.addObserver(observer = new Observer() {
public void update(Observable obs, Object obj) { public void update(Observable obs, Object obj) {
@ -238,8 +241,8 @@ public class ContikiCFS extends MoteInterface implements ContikiMoteInterface {
} }
/** /**
* Opens a file dialog and returns the contents of the selected fileor null if dialog aborted. * Opens a file dialog and returns the contents of the selected file or null if dialog aborted.
* *
* @param parent Dialog parent, may be null * @param parent Dialog parent, may be null
* @return Binary contents of user selected file * @return Binary contents of user selected file
*/ */
@ -249,12 +252,13 @@ public class ContikiCFS extends MoteInterface implements ContikiMoteInterface {
JFileChooser fileChooser = new JFileChooser(); JFileChooser fileChooser = new JFileChooser();
fileChooser.setCurrentDirectory(new java.io.File(".")); fileChooser.setCurrentDirectory(new java.io.File("."));
fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY); fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
fileChooser.setDialogTitle("Select Contiki executable (.ce)"); fileChooser.setDialogTitle("Select binary data");
if (fileChooser.showOpenDialog(parent) == JFileChooser.APPROVE_OPTION) { if (fileChooser.showOpenDialog(parent) == JFileChooser.APPROVE_OPTION) {
file = fileChooser.getSelectedFile(); file = fileChooser.getSelectedFile();
} else } else {
return null; return null;
}
// Read file data // Read file data
long fileSize = file.length(); long fileSize = file.length();
@ -281,5 +285,5 @@ public class ContikiCFS extends MoteInterface implements ContikiMoteInterface {
return fileData; return fileData;
} }
} }