package org.contikios.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);
            /* and the data */
            out.write(data);
            out.flush();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    
    
}

@@ -193,16 +193,37 @@
 ception;
+%0Aimport java.io.File;
 %0A%0Apublic
@@ -355,24 +355,37 @@
 id openPcap(
+File pcapFile
 ) throws IOE
@@ -406,107 +406,320 @@
     
+ if (
  out 
+!
 = n
-ew DataOutputStream(new FileOutputStream(%22radiolog-%22 + System.currentTimeMillis() + %22.pcap%22
+ull ) %7B%0A            closePcap();%0A        %7D%0A        if ( pcapFile == null ) %7B%0A            /* pcap file not specified, use default file name */%0A            pcapFile = new File(%22radiolog-%22 + System.currentTimeMillis() + %22.pcap%22);%0A        %7D%0A    %09out = new DataOutputStream(new FileOutputStream(pcapFile
 ));%0A
@@ -1052,10 +1052,21 @@
 file
-!%22
+ %22 + pcapFile
 );%0A 
@@ -1136,24 +1136,44 @@
 ut.close();%0A
+        out = null;%0A
     %7D%0A%0A    p
@@ -1257,24 +1257,24 @@
  == null) %7B%0A
-
             
@@ -1278,24 +1278,28 @@
    openPcap(
+null
 );%0A        %7D

Allow configuration of pcap output file