Merge pull request #475 from cetic/cooja-radiologger-pcap

Allow user configuration of RadioLogger pcap file
This commit is contained in:
Fredrik Österlind 2013-11-28 04:54:07 -08:00
commit 189b1535e9
3 changed files with 41 additions and 5 deletions

View file

@ -144,6 +144,8 @@ public class RadioLogger extends VisPlugin {
private HashMap<String,Action> analyzerMap = new HashMap<String,Action>();
private String analyzerName = null;
private ArrayList<PacketAnalyzer> 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<PacketAnalyzer> lowpanAnalyzersPcap = new ArrayList<PacketAnalyzer>();
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;

View file

@ -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;
}

View file

@ -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 */