diff --git a/tools/cooja/java/org/contikios/cooja/plugins/RadioLogger.java b/tools/cooja/java/org/contikios/cooja/plugins/RadioLogger.java index 2729d507f..f73a4af8c 100644 --- a/tools/cooja/java/org/contikios/cooja/plugins/RadioLogger.java +++ b/tools/cooja/java/org/contikios/cooja/plugins/RadioLogger.java @@ -144,6 +144,8 @@ public class RadioLogger extends VisPlugin { private HashMap analyzerMap = new HashMap(); private String analyzerName = null; private ArrayList analyzers = null; + private IEEE802154Analyzer analyzerWithPcap; + private File pcapFile; private JTextField searchField = new JTextField(30); @@ -174,8 +176,9 @@ public class RadioLogger extends VisPlugin { lowpanAnalyzers.add(new IPv6PacketAnalyzer()); lowpanAnalyzers.add(new ICMPv6Analyzer()); + analyzerWithPcap = new IEEE802154Analyzer(true); ArrayList lowpanAnalyzersPcap = new ArrayList(); - lowpanAnalyzersPcap.add(new IEEE802154Analyzer(true)); + lowpanAnalyzersPcap.add(analyzerWithPcap); lowpanAnalyzersPcap.add(new IPHCPacketAnalyzer()); lowpanAnalyzersPcap.add(new IPv6PacketAnalyzer()); lowpanAnalyzersPcap.add(new ICMPv6Analyzer()); @@ -801,6 +804,14 @@ public class RadioLogger extends VisPlugin { } } + if (pcapFile != null) { + element = new Element("pcap_file"); + File file = simulation.getCooja().createPortablePath(pcapFile); + element.setText(pcapFile.getPath().replaceAll("\\\\", "/")); + element.setAttribute("EXPORT", "discard"); + config.add(element); + } + return config; } @@ -833,6 +844,9 @@ public class RadioLogger extends VisPlugin { } }); } + } else if (name.equals("pcap_file")) { + pcapFile = simulation.getCooja().restorePortablePath(new File(element.getText())); + analyzerWithPcap.setPcapFile(pcapFile); } } return true; diff --git a/tools/cooja/java/org/contikios/cooja/plugins/analyzers/IEEE802154Analyzer.java b/tools/cooja/java/org/contikios/cooja/plugins/analyzers/IEEE802154Analyzer.java index db5718a76..d15903d51 100644 --- a/tools/cooja/java/org/contikios/cooja/plugins/analyzers/IEEE802154Analyzer.java +++ b/tools/cooja/java/org/contikios/cooja/plugins/analyzers/IEEE802154Analyzer.java @@ -1,6 +1,7 @@ package org.contikios.cooja.plugins.analyzers; import java.io.IOException; +import java.io.File; import org.contikios.cooja.util.StringUtils; @@ -36,6 +37,17 @@ public class IEEE802154Analyzer extends PacketAnalyzer { } } + public void setPcapFile(File pcapFile) { + if (pcapExporter != null) { + try { + pcapExporter.openPcap(pcapFile); + } catch (IOException e) { + System.err.println("Could not open pcap file"); + e.printStackTrace(); + } + } + } + public boolean matchPacket(Packet packet) { return packet.level == MAC_LEVEL; } diff --git a/tools/cooja/java/org/contikios/cooja/plugins/analyzers/PcapExporter.java b/tools/cooja/java/org/contikios/cooja/plugins/analyzers/PcapExporter.java index 638149a18..0e8a4244a 100644 --- a/tools/cooja/java/org/contikios/cooja/plugins/analyzers/PcapExporter.java +++ b/tools/cooja/java/org/contikios/cooja/plugins/analyzers/PcapExporter.java @@ -5,6 +5,7 @@ import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.FileWriter; import java.io.IOException; +import java.io.File; public class PcapExporter { @@ -13,8 +14,15 @@ public class PcapExporter { public PcapExporter() throws IOException { } - public void openPcap() throws IOException { - out = new DataOutputStream(new FileOutputStream("radiolog-" + System.currentTimeMillis() + ".pcap")); + public void openPcap(File pcapFile) throws IOException { + if ( out != null ) { + closePcap(); + } + if ( pcapFile == null ) { + /* pcap file not specified, use default file name */ + pcapFile = new File("radiolog-" + System.currentTimeMillis() + ".pcap"); + } + out = new DataOutputStream(new FileOutputStream(pcapFile)); /* pcap header */ out.writeInt(0xa1b2c3d4); out.writeShort(0x0002); @@ -24,15 +32,17 @@ public class PcapExporter { out.writeInt(4096); out.writeInt(195); /* 195 for LINKTYPE_IEEE802_15_4 */ out.flush(); - System.out.println("Opened pcap file!"); + System.out.println("Opened pcap file " + pcapFile); } public void closePcap() throws IOException { out.close(); + out = null; } public void exportPacketData(byte[] data) throws IOException { if (out == null) { - openPcap(); + /* pcap file never set, open default */ + openPcap(null); } try { /* pcap packet header */