示例#1
0
    static void usbDump(int numPackets, byte capMask)
    {
        // Setup variables
        byte[] packet      = new byte[1036];
        byte[] packetKBits = new byte[130];

        int packetnum = 0;

        BeagleUsb5000CaptureStatus captureStatus =
            BeagleUsb5000CaptureStatus.BG_USB5000_CAPTURE_STATUS_INACTIVE;
        uint pretrigAmt   = 0;
        uint pretrigTotal = 0;
        uint capAmt       = 0;
        uint capTotal     = 0;
        int  ret          = 0;

        #if STATS_MODE
        // Statistic mode variables
        ulong dispTimeSop     = 0;
        ulong sstxByteCount   = 0;
        ulong ssrxByteCount   = 0;
        ulong sstxPacketCount = 0;
        ulong ssrxPacketCount = 0;
        ulong usb2ByteCount   = 0;
        ulong usb2PacketCount = 0;
        #endif

        // Configure Beagle 480 for realtime capture
        if (BeagleApi.bg_usb5000_configure(
                beagle, capMask,
                BeagleUsb5000TriggerMode.BG_USB5000_TRIGGER_MODE_IMMEDIATE) !=
            (int)BeagleStatus.BG_OK)
        {
            Console.Write(
                "error: could not configure Beagle 5000 with desired mode\n");
            Environment.Exit(1);
        }

        // Start the capture
        if (BeagleApi.bg_enable(beagle, BeagleProtocol.BG_PROTOCOL_USB) !=
            (int)BeagleStatus.BG_OK)
        {
            Console.Write("error: could not enable USB capture; exiting...\n");
            Environment.Exit(1);
        }

        // Wait for the analyzer to trigger for up to 2 seconds...
        if ((capMask & BeagleApi.BG_USB5000_CAPTURE_USB2) != 0)
        {
            ret = BeagleApi.bg_usb5000_usb2_capture_status(
                beagle, 2000, ref captureStatus,
                ref pretrigAmt, ref pretrigTotal,
                ref capAmt, ref capTotal);
        }
        else
        {
            ret = BeagleApi.bg_usb5000_usb3_capture_status(
                beagle, 2000, ref captureStatus,
                ref pretrigAmt, ref pretrigTotal,
                ref capAmt, ref capTotal);
        }

        if (ret != (int)BeagleStatus.BG_OK)
        {
            Console.Write(
                "error: could not query capture status; exiting...\n");
            Environment.Exit(1);
        }

        if (captureStatus <= BeagleUsb5000CaptureStatus.
            BG_USB5000_CAPTURE_STATUS_PRE_TRIGGER)
        {
            Console.Write("did not trigger.\n");
            Environment.Exit(1);
        }

        // Output the header...
        #if STATS_MODE
        Console.Write(
            "   SSTX PACKETS ( MB/s )    SSRX PACKETS ( MB/s )    USB2 PACKETS ( MB/s )\n");
        #else
        Console.Write(
            "index,time(ns),source,event,status,data0 ... dataN(*)\n");
        #endif
        Console.Out.Flush();

        // ...then start decoding packets
        while (packetnum < numPackets || (numPackets == 0))
        {
            uint  status               = 0;
            uint  events               = 0;
            ulong timeSop              = 0;
            ulong timeDuration         = 0;
            uint  timeDataOffset       = 0;
            BeagleUsb5000Source source =
                BeagleUsb5000Source.BG_USB5000_SOURCE_ASYNC;

            int length = BeagleApi.bg_usb5000_read(
                beagle,
                ref status, ref events, ref timeSop, ref timeDuration,
                ref timeDataOffset, ref source,
                1036, packet, 130, packetKBits);

            // Make sure capture is triggered.
            if (length == (int)BeagleStatus.BG_CAPTURE_NOT_TRIGGERED)
            {
                continue;
            }

            // Check for invalid packet or Beagle error
            if (length < 0)
            {
                Console.Write("error={0:d}\n", length);
                break;
            }

            // Exit if observed end of capture
            if (status == BeagleApi.BG_READ_USB_END_OF_CAPTURE)
            {
                Console.Write("\n");
                Console.Write("End of capture\n");
                break;
            }

            #if STATS_MODE
            // Count the number of packets and bytes
            if (length > 0)
            {
                switch (source)
                {
                case BeagleUsb5000Source.BG_USB5000_SOURCE_USB2:
                    usb2PacketCount++;
                    usb2ByteCount += (ulong)length;
                    break;

                case BeagleUsb5000Source.BG_USB5000_SOURCE_TX:
                    sstxPacketCount++;
                    sstxByteCount += (ulong)length;
                    break;

                case BeagleUsb5000Source.BG_USB5000_SOURCE_RX:
                    ssrxPacketCount++;
                    ssrxByteCount += (ulong)length;
                    break;

                default:
                    break;
                }
            }

            // Periodically print out the stats
            if ((timeSop - dispTimeSop) > DISPLAY_TIME_TICKS)
            {
                double timeS = (double)timestampToNS(timeSop - dispTimeSop,
                                                     samplerateKHz) / 1E9;
                double sstxMBps =
                    ((double)sstxByteCount) / (1024 * 1024L) / timeS;
                double ssrxMBps =
                    ((double)ssrxByteCount) / (1024 * 1024L) / timeS;
                double usb2MBps =
                    ((double)usb2ByteCount) / (1024 * 1024L) / timeS;

                Console.Write("\r{0,15:d} ({1,6:##0.00})" +
                              " {2,15:d} ({3,6:##0.00})" +
                              " {4,15:d} ({5,6:##0.00})",
                              sstxPacketCount, sstxMBps,
                              ssrxPacketCount, ssrxMBps,
                              usb2PacketCount, usb2MBps);

                sstxByteCount = 0;
                ssrxByteCount = 0;
                usb2ByteCount = 0;
                dispTimeSop   = timeSop;
            }
            #else
            // Grab the next packet on a timeout.
            if (length == 0 &&
                status == BeagleApi.BG_READ_TIMEOUT &&
                events == 0)
            {
                continue;
            }

            // Print the packet details
            Console.Write("{0},", packetnum);
            Console.Write("{0},", timestampToNS(timeSop, samplerateKHz));
            printSource(source);
            Console.Write(",");
            printEvents(source, events);
            Console.Write(",");
            printStatus(source, status);
            Console.Write(",");
            printPacket(source, packet, packetKBits, length);
            Console.Write("\n");
            #endif

            packetnum++;
        }

        // Stop the capture
        BeagleApi.bg_disable(beagle);
    }