package se.sics.cooja.plugins.analyzers;

import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;

public class PcapExporter {

    DataOutputStream out;
    
    public PcapExporter() throws IOException {
    }
    
    public void openPcap() throws IOException {
        out = new DataOutputStream(new FileOutputStream("radiolog-" + System.currentTimeMillis() + ".pcap"));
        /* pcap header */
        out.writeInt(0xa1b2c3d4);
        out.writeShort(0x0002);
        out.writeShort(0x0004);
        out.writeInt(0);
        out.writeInt(0);
        out.writeInt(4096);
        out.writeInt(195); /* 195 for LINKTYPE_IEEE802_15_4 */
        out.flush();
        System.out.println("Opened pcap file!");
    }
    public void closePcap() throws IOException {
        out.close();
    }

    public void exportPacketData(byte[] data) throws IOException {
        if (out == null) {
            openPcap();
        }
        try {
            /* pcap packet header */
            out.writeInt((int) System.currentTimeMillis() / 1000);
            out.writeInt((int) ((System.currentTimeMillis() % 1000) * 1000));
            out.writeInt(data.length);
            out.writeInt(data.length+2);
            /* and the data */
            out.write(data);
            out.flush();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    
    
}

@@ -187,16 +187,37 @@
 ception;
+%0Aimport java.io.File;
 %0A%0Apublic
@@ -349,24 +349,37 @@
 id openPcap(
+File pcapFile
 ) throws IOE
@@ -397,110 +397,240 @@
     
-   
+%09if (
  out 
+!
 = n
-ew DataOutputStream(new FileOutputStream(%22radiolog-%22 + System.currentTimeMillis() + %22.pcap%22
+ull ) %7B%0A    %09%09closePcap();%0A    %09%7D%0A    %09if ( pcapFile == null ) %7B%0A            pcapFile = new File(%22radiolog-%22 + System.currentTimeMillis() + %22.pcap%22);%0A    %09%7D%0A    %09out = new DataOutputStream(new FileOutputStream(pcapFile
 ));%0A
@@ -963,10 +963,21 @@
 file
-!%22
+ %22 + pcapFile
 );%0A 
@@ -1047,24 +1047,44 @@
 ut.close();%0A
+        out = null;%0A
     %7D%0A%0A    p
@@ -1168,24 +1168,24 @@
  == null) %7B%0A
-
             
@@ -1189,24 +1189,28 @@
    openPcap(
+null
 );%0A        %7D

Allow configuration of pcap output file