VirtualBox

source: vbox/trunk/src/VBox/Devices/USB/VUSBSniffer.cpp@ 53062

Last change on this file since 53062 was 53060, checked in by vboxsync, 10 years ago

VUSBSniffer: Fixes

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 22.1 KB
Line 
1/* $Id: VUSBSniffer.cpp 53060 2014-10-15 12:09:51Z vboxsync $ */
2/** @file
3 * Virtual USB - Sniffer facility.
4 */
5
6/*
7 * Copyright (C) 2014 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 */
17
18/*******************************************************************************
19* Header Files *
20*******************************************************************************/
21#define LOG_GROUP LOG_GROUP_DRV_VUSB
22#include <VBox/log.h>
23#include <iprt/file.h>
24#include <iprt/mem.h>
25#include <iprt/buildconfig.h>
26#include <iprt/string.h>
27#include <iprt/system.h>
28
29#include "VUSBSniffer.h"
30
31/*******************************************************************************
32* Defined Constants And Macros *
33*******************************************************************************/
34
35/** DumpFile Section Header Block type. */
36#define DUMPFILE_SHB_BLOCK_TYPE UINT32_C(0x0a0d0d0a)
37/** The byte order magic value. */
38#define DUMPFILE_SHB_BYTE_ORDER_MAGIC UINT32_C(0x1a2b3c4d)
39/** Current major version. */
40#define DUMPFILE_SHB_VERSION_MAJOR UINT16_C(1)
41/** Current minor version. */
42#define DUMPFILE_SHB_VERSION_MINOR UINT16_C(0)
43
44/** Block type for the interface descriptor block. */
45#define DUMPFILE_IDB_BLOCK_TYPE UINT32_C(0x00000001)
46/** USB link type. */
47#define DUMPFILE_IDB_LINK_TYPE_USB_LINUX_MMAPED UINT16_C(220)
48
49/** Block type for an enhanced packet block. */
50#define DUMPFILE_EPB_BLOCK_TYPE UINT32_C(0x00000006)
51
52/** USB packet event types. */
53#define DUMPFILE_USB_EVENT_TYPE_SUBMIT ('S')
54#define DUMPFILE_USB_EVENT_TYPE_COMPLETE ('C')
55#define DUMPFILE_USB_EVENT_TYPE_ERROR ('E')
56
57#define DUMPFILE_OPTION_CODE_END UINT16_C(0)
58#define DUMPFILE_OPTION_CODE_COMMENT UINT16_C(1)
59
60#define DUMPFILE_OPTION_CODE_HARDWARE UINT16_C(2)
61#define DUMPFILE_OPTION_CODE_OS UINT16_C(3)
62#define DUMPFILE_OPTION_CODE_USERAPP UINT16_C(4)
63
64#define DUMPFILE_IDB_OPTION_TS_RESOLUTION UINT16_C(9)
65
66/*******************************************************************************
67* DumpFile format structures *
68*******************************************************************************/
69
70/**
71 * DumpFile Block header.
72 */
73typedef struct DumpFileBlockHdr
74{
75 /** Block type. */
76 uint32_t u32BlockType;
77 /** Block total length. */
78 uint32_t u32BlockTotalLength;
79} DumpFileBlockHdr;
80/** Pointer to a block header. */
81typedef DumpFileBlockHdr *PDumpFileBlockHdr;
82
83/**
84 * DumpFile Option header.
85 */
86typedef struct DumpFileOptionHdr
87{
88 /** Option code. */
89 uint16_t u16OptionCode;
90 /** Block total length. */
91 uint16_t u16OptionLength;
92} DumpFileOptionHdr;
93/** Pointer to a option header. */
94typedef DumpFileOptionHdr *PDumpFileOptionHdr;
95
96/**
97 * DumpFile Section Header Block.
98 */
99typedef struct DumpFileShb
100{
101 /** Block header. */
102 DumpFileBlockHdr Hdr;
103 /** Byte order magic. */
104 uint32_t u32ByteOrderMagic;
105 /** Major version. */
106 uint16_t u16VersionMajor;
107 /** Minor version. */
108 uint16_t u16VersionMinor;
109 /** Section length. */
110 uint64_t u64SectionLength;
111} DumpFileShb;
112/** Pointer to a Section Header Block. */
113typedef DumpFileShb *PDumpFileShb;
114
115/**
116 * DumpFile Interface description block.
117 */
118typedef struct DumpFileIdb
119{
120 /** Block header. */
121 DumpFileBlockHdr Hdr;
122 /** Link type. */
123 uint16_t u16LinkType;
124 /** Reserved. */
125 uint16_t u16Reserved;
126 /** Maximum number of bytes dumped from each packet. */
127 uint32_t u32SnapLen;
128} DumpFileIdb;
129/** Pointer to an Interface description block. */
130typedef DumpFileIdb *PDumpFileIdb;
131
132/**
133 * DumpFile Enhanced packet block.
134 */
135typedef struct DumpFileEpb
136{
137 /** Block header. */
138 DumpFileBlockHdr Hdr;
139 /** Interface ID. */
140 uint32_t u32InterfaceId;
141 /** Timestamp (high). */
142 uint32_t u32TimestampHigh;
143 /** Timestamp (low). */
144 uint32_t u32TimestampLow;
145 /** Captured packet length. */
146 uint32_t u32CapturedLen;
147 /** Original packet length. */
148 uint32_t u32PacketLen;
149} DumpFileEpb;
150/** Pointer to an Enhanced packet block. */
151typedef DumpFileEpb *PDumpFileEpb;
152
153/**
154 * USB setup URB data.
155 */
156typedef struct DumpFileUsbSetup
157{
158 uint8_t bmRequestType;
159 uint8_t bRequest;
160 uint16_t wValue;
161 uint16_t wIndex;
162 uint16_t wLength;
163} DumpFileUsbSetup;
164typedef DumpFileUsbSetup *PDumpFileUsbSetup;
165
166/**
167 * USB Isochronous data.
168 */
169typedef struct DumpFileIsoRec
170{
171 int32_t i32ErrorCount;
172 int32_t i32NumDesc;
173} DumpFileIsoRec;
174typedef DumpFileIsoRec *PDumpFileIsoRec;
175
176/**
177 * USB packet header (Linux mmapped variant).
178 */
179typedef struct DumpFileUsbHeaderLnxMmapped
180{
181 /** Packet Id. */
182 uint64_t u64Id;
183 /** Event type. */
184 uint8_t u8EventType;
185 /** Transfer type. */
186 uint8_t u8TransferType;
187 /** Endpoint number. */
188 uint8_t u8EndpointNumber;
189 /** Device address. */
190 uint8_t u8DeviceAddress;
191 /** Bus id. */
192 uint16_t u16BusId;
193 /** Setup flag != 0 if the URB setup header is not present. */
194 uint8_t u8SetupFlag;
195 /** Data present flag != 0 if the URB data is not present. */
196 uint8_t u8DataFlag;
197 /** Timestamp (second part). */
198 uint64_t u64TimestampSec;
199 /** Timestamp (us part). */
200 uint32_t u32TimestampUSec;
201 /** Status. */
202 int32_t i32Status;
203 /** URB length. */
204 uint32_t u32UrbLength;
205 /** Recorded data length. */
206 uint32_t u32DataLength;
207 /** Union of data for different URB types. */
208 union
209 {
210 DumpFileUsbSetup UsbSetup;
211 DumpFileIsoRec IsoRec;
212 } u;
213 int32_t i32Interval;
214 int32_t i32StartFrame;
215 /** Copy of transfer flags. */
216 uint32_t u32XferFlags;
217 /** Number of isochronous descriptors. */
218 uint32_t u32NumDesc;
219} DumpFileUsbHeaderLnxMmapped;
220/** Pointer to a USB packet header. */
221typedef DumpFileUsbHeaderLnxMmapped *PDumpFileUsbHeaderLnxMmapped;
222
223AssertCompileSize(DumpFileUsbHeaderLnxMmapped, 64);
224
225/**
226 * USB packet isochronous descriptor.
227 */
228typedef struct DumpFileUsbIsoDesc
229{
230 int32_t i32Status;
231 uint32_t u32Offset;
232 uint32_t u32Len;
233 uint8_t au8Padding[4];
234} DumpFileUsbIsoDesc;
235typedef DumpFileUsbIsoDesc *PDumpFileUsbIsoDesc;
236
237/*******************************************************************************
238* Structures and Typedefs *
239*******************************************************************************/
240
241/**
242 * The internal VUSB sniffer state.
243 */
244typedef struct VUSBSNIFFERINT
245{
246 /** The file handle to dump to. */
247 RTFILE hFile;
248 /** Current size of the block being written. */
249 uint32_t cbBlockCur;
250 /** Maximum size allocated for the block. */
251 uint32_t cbBlockMax;
252 /** Current block header. */
253 PDumpFileBlockHdr pBlockHdr;
254 /** Pointer to the block data which will be written on commit. */
255 uint8_t *pbBlockData;
256} VUSBSNIFFERINT;
257/** Pointer to the internal VUSB sniffer state. */
258typedef VUSBSNIFFERINT *PVUSBSNIFFERINT;
259
260/**
261 * Allocates additional space for the block.
262 *
263 * @returns Pointer to the new unused space or NULL if out of memory.
264 * @param pThis The VUSB sniffer instance.
265 * @param cbAdditional The additional memory requested.
266 */
267static void *vusbSnifferBlockAllocSpace(PVUSBSNIFFERINT pThis, uint32_t cbAdditional)
268{
269 /* Fast path where we have enough memory allocated. */
270 if (pThis->cbBlockCur + cbAdditional <= pThis->cbBlockMax)
271 {
272 void *pv = pThis->pbBlockData + pThis->cbBlockCur;
273 pThis->cbBlockCur += cbAdditional;
274 return pv;
275 }
276
277 /* Allocate additional memory. */
278 uint32_t cbNew = pThis->cbBlockCur + cbAdditional;
279 uint8_t *pbDataNew = (uint8_t *)RTMemRealloc(pThis->pbBlockData, cbNew);
280 if (pbDataNew)
281 {
282 pThis->pbBlockData = pbDataNew;
283 pThis->pBlockHdr = (PDumpFileBlockHdr)pbDataNew;
284
285 void *pv = pThis->pbBlockData + pThis->cbBlockCur;
286 pThis->cbBlockCur = cbNew;
287 pThis->cbBlockMax = cbNew;
288 return pv;
289 }
290
291 return NULL;
292}
293
294/**
295 * Adds new data to the current block.
296 *
297 * @returns VBox status code.
298 * @param pThis The VUSB sniffer instance.
299 * @param pvData The data to add.
300 * @param cbData Amount of data to add.
301 */
302static int vusbSnifferBlockAddData(PVUSBSNIFFERINT pThis, const void *pvData, uint32_t cbData)
303{
304 int rc = VINF_SUCCESS;
305
306 Assert(pThis->cbBlockCur);
307 AssertPtr(pThis->pBlockHdr);
308
309 void *pv = vusbSnifferBlockAllocSpace(pThis, cbData);
310 if (pv)
311 memcpy(pv, pvData, cbData);
312 else
313 rc = VERR_NO_MEMORY;
314
315 return rc;
316}
317
318/**
319 * Commits the current block to the capture file.
320 *
321 * @returns VBox status code.
322 * @param pThis The VUSB sniffer instance.
323 */
324static int vusbSnifferBlockCommit(PVUSBSNIFFERINT pThis)
325{
326 int rc = VINF_SUCCESS;
327
328 AssertPtr(pThis->pBlockHdr);
329
330 /* Pad to 32bits. */
331 uint8_t abPad[3] = { 0 };
332 uint32_t cbPad = RT_ALIGN_32(pThis->cbBlockCur, 4) - pThis->cbBlockCur;
333
334 Assert(cbPad <= 3);
335 if (cbPad)
336 rc = vusbSnifferBlockAddData(pThis, abPad, cbPad);
337
338 if (RT_SUCCESS(rc))
339 {
340 /* Update the block total length field. */
341 uint32_t *pcbTotalLength = (uint32_t *)vusbSnifferBlockAllocSpace(pThis, 4);
342 if (pcbTotalLength)
343 {
344 *pcbTotalLength = pThis->cbBlockCur;
345 pThis->pBlockHdr->u32BlockTotalLength = pThis->cbBlockCur;
346
347 /* Write the data. */
348 rc = RTFileWrite(pThis->hFile, pThis->pbBlockData, pThis->cbBlockCur, NULL);
349 pThis->cbBlockCur = 0;
350 pThis->pBlockHdr = NULL;
351 }
352 else
353 rc = VERR_NO_MEMORY;
354 }
355
356 return rc;
357}
358
359/**
360 * Starts a new block for capturing.
361 *
362 * @returns VBox status code.
363 * @param pThis The VUSB sniffer instance.
364 * @param pBlockHdr Pointer to the block header for the new block.
365 * @param cbData Amount of data added with this block.
366 */
367static int vusbSnifferBlockNew(PVUSBSNIFFERINT pThis, PDumpFileBlockHdr pBlockHdr, uint32_t cbData)
368{
369 int rc = VINF_SUCCESS;
370
371 /* Validate we don't get called while another block is active. */
372 Assert(!pThis->cbBlockCur);
373 Assert(!pThis->pBlockHdr);
374 pThis->pBlockHdr = (PDumpFileBlockHdr)vusbSnifferBlockAllocSpace(pThis, cbData);
375 if (pThis->pBlockHdr)
376 memcpy(pThis->pBlockHdr, pBlockHdr, cbData);
377 else
378 rc = VERR_NO_MEMORY;
379
380 return rc;
381}
382
383/**
384 * Add a new option to the current block.
385 *
386 * @returns VBox status code.
387 * @param pThis The VUSB sniffer instance.
388 * @param u16OptionCode The option code identifying the type of option.
389 * @param pvOption Raw data for the option.
390 * @param cbOption Size of the optiob data.
391 */
392static int vusbSnifferAddOption(PVUSBSNIFFERINT pThis, uint16_t u16OptionCode, const void *pvOption, uint16_t cbOption)
393{
394 int rc = VINF_SUCCESS;
395 DumpFileOptionHdr OptHdr;
396
397 OptHdr.u16OptionCode = u16OptionCode;
398 OptHdr.u16OptionLength = cbOption;
399 rc = vusbSnifferBlockAddData(pThis, &OptHdr, sizeof(OptHdr));
400 if ( RT_SUCCESS(rc)
401 && u16OptionCode != DUMPFILE_OPTION_CODE_END
402 && cbOption != 0)
403 {
404 rc = vusbSnifferBlockAddData(pThis, pvOption, cbOption);
405 if (RT_SUCCESS(rc))
406 {
407 /* Pad to 32bits. */
408 uint8_t abPad[3] = { 0 };
409 uint32_t cbPad = RT_ALIGN_32(cbOption, 4) - cbOption;
410
411 Assert(cbPad <= 3);
412 if (cbPad)
413 rc = vusbSnifferBlockAddData(pThis, abPad, cbPad);
414 }
415 }
416
417 return rc;
418}
419
420DECLHIDDEN(int) VUSBSnifferCreate(PVUSBSNIFFER phSniffer, uint32_t fFlags,
421 const char *pszCaptureFilename, const char *pszDesc)
422{
423 int rc = VINF_SUCCESS;
424 PVUSBSNIFFERINT pThis = NULL;
425
426 pThis = (PVUSBSNIFFERINT)RTMemAllocZ(sizeof(VUSBSNIFFERINT));
427 if (pThis)
428 {
429 pThis->hFile = NIL_RTFILE;
430 pThis->cbBlockCur = 0;
431 pThis->cbBlockMax = 0;
432 pThis->pbBlockData = NULL;
433
434 rc = RTFileOpen(&pThis->hFile, pszCaptureFilename, RTFILE_O_DENY_NONE | RTFILE_O_CREATE | RTFILE_O_WRITE | RTFILE_O_READ);
435 if (RT_SUCCESS(rc))
436 {
437 /* Write header and link type blocks. */
438 DumpFileShb Shb;
439
440 Shb.Hdr.u32BlockType = DUMPFILE_SHB_BLOCK_TYPE;
441 Shb.Hdr.u32BlockTotalLength = 0; /* Filled out by lower layer. */
442 Shb.u32ByteOrderMagic = DUMPFILE_SHB_BYTE_ORDER_MAGIC;
443 Shb.u16VersionMajor = DUMPFILE_SHB_VERSION_MAJOR;
444 Shb.u16VersionMinor = DUMPFILE_SHB_VERSION_MINOR;
445 Shb.u64SectionLength = UINT64_C(0xffffffffffffffff); /* -1 */
446
447 /* Write the blocks. */
448 rc = vusbSnifferBlockNew(pThis, &Shb.Hdr, sizeof(Shb));
449 if (RT_SUCCESS(rc))
450 {
451 const char *pszOpt = RTBldCfgTargetDotArch();
452 rc = vusbSnifferAddOption(pThis, DUMPFILE_OPTION_CODE_HARDWARE, pszOpt, strlen(pszOpt) + 1);
453 }
454
455 if (RT_SUCCESS(rc))
456 {
457 char szTmp[512];
458 size_t cbTmp = sizeof(szTmp);
459
460 RT_ZERO(szTmp);
461
462 /* Build the OS code. */
463 rc = RTSystemQueryOSInfo(RTSYSOSINFO_PRODUCT, szTmp, cbTmp);
464 if (RT_SUCCESS(rc))
465 {
466 size_t cb = strlen(szTmp);
467
468 szTmp[cb] = ' ';
469 rc = RTSystemQueryOSInfo(RTSYSOSINFO_RELEASE, &szTmp[cb + 1], cbTmp - (cb + 1));
470 if (RT_SUCCESS(rc))
471 {
472 cb = strlen(szTmp);
473 szTmp[cb] = ' ';
474 rc = RTSystemQueryOSInfo(RTSYSOSINFO_VERSION, &szTmp[cb + 1], cbTmp - (cb + 1));
475 }
476 }
477
478 if (RT_SUCCESS(rc) || rc == VERR_BUFFER_OVERFLOW)
479 rc = vusbSnifferAddOption(pThis, DUMPFILE_OPTION_CODE_OS, szTmp, strlen(szTmp) + 1);
480 else
481 rc = VINF_SUCCESS; /* Skip OS code if building the string failed. */
482 }
483
484 if (RT_SUCCESS(rc))
485 {
486 /** @todo: Add product info. */
487 }
488
489 if (RT_SUCCESS(rc))
490 rc = vusbSnifferAddOption(pThis, DUMPFILE_OPTION_CODE_END, NULL, 0);
491 if (RT_SUCCESS(rc))
492 rc = vusbSnifferBlockCommit(pThis);
493
494 /* Write Interface descriptor block. */
495 if (RT_SUCCESS(rc))
496 {
497 DumpFileIdb Idb;
498
499 Idb.Hdr.u32BlockType = DUMPFILE_IDB_BLOCK_TYPE;
500 Idb.Hdr.u32BlockTotalLength = 0; /* Filled out by lower layer. */
501 Idb.u16LinkType = DUMPFILE_IDB_LINK_TYPE_USB_LINUX_MMAPED;
502 Idb.u16Reserved = 0;
503 Idb.u32SnapLen = UINT32_C(0xffffffff);
504
505 rc = vusbSnifferBlockNew(pThis, &Idb.Hdr, sizeof(Idb));
506 if (RT_SUCCESS(rc))
507 {
508 uint8_t u8TsResolution = 9; /* Nano second resolution. */
509 /* Add timestamp resolution option. */
510 rc = vusbSnifferAddOption(pThis, DUMPFILE_IDB_OPTION_TS_RESOLUTION,
511 &u8TsResolution, sizeof(u8TsResolution));
512 }
513 if (RT_SUCCESS(rc))
514 rc = vusbSnifferAddOption(pThis, DUMPFILE_OPTION_CODE_END, NULL, 0);
515 if (RT_SUCCESS(rc))
516 rc = vusbSnifferBlockCommit(pThis);
517 }
518
519 if (RT_SUCCESS(rc))
520 {
521 *phSniffer = pThis;
522 return VINF_SUCCESS;
523 }
524
525 RTFileClose(pThis->hFile);
526 pThis->hFile = NIL_RTFILE;
527 RTFileDelete(pszCaptureFilename);
528 }
529 VUSBSnifferDestroy(pThis);
530 }
531 else
532 rc = VERR_NO_MEMORY;
533
534 return rc;
535}
536
537/**
538 * Destroys the given VUSB sniffer instance.
539 *
540 * @returns nothing.
541 * @param hSniffer The sniffer instance to destroy.
542 */
543DECLHIDDEN(void) VUSBSnifferDestroy(VUSBSNIFFER hSniffer)
544{
545 PVUSBSNIFFERINT pThis = hSniffer;
546
547 if (pThis->hFile != NIL_RTFILE)
548 RTFileClose(pThis->hFile);
549 if (pThis->pbBlockData)
550 RTMemFree(pThis->pbBlockData);
551 RTMemFree(pThis);
552}
553
554/**
555 * Records an VUSB event.
556 *
557 * @returns VBox status code.
558 * @param hSniffer The sniffer instance.
559 * @param pUrb The URB triggering the event.
560 * @param enmEvent The type of event to record.
561 */
562DECLHIDDEN(int) VUSBSnifferRecordEvent(VUSBSNIFFER hSniffer, PVUSBURB pUrb, VUSBSNIFFEREVENT enmEvent)
563{
564 int rc = VINF_SUCCESS;
565 PVUSBSNIFFERINT pThis = hSniffer;
566 DumpFileEpb Epb;
567 DumpFileUsbHeaderLnxMmapped UsbHdr;
568 DumpFileUsbSetup UsbSetup;
569 uint64_t u64TimestampEvent = RTTimeNanoTS();
570
571 /* Start with the enhanced packet block. */
572 Epb.Hdr.u32BlockType = DUMPFILE_EPB_BLOCK_TYPE;
573 Epb.Hdr.u32BlockTotalLength = 0;
574 Epb.u32InterfaceId = 0;
575 Epb.u32TimestampHigh = (u64TimestampEvent >> 32) & UINT32_C(0xffffffff);
576 Epb.u32TimestampLow = u64TimestampEvent & UINT32_C(0xffffffff);
577
578 UsbHdr.u64Id = (uint64_t)pUrb; /** @todo: check whether the pointer is a good ID. */
579 switch (enmEvent)
580 {
581 case VUSBSNIFFEREVENT_SUBMIT:
582 UsbHdr.u8EventType = DUMPFILE_USB_EVENT_TYPE_SUBMIT;
583 break;
584 case VUSBSNIFFEREVENT_COMPLETE:
585 UsbHdr.u8EventType = DUMPFILE_USB_EVENT_TYPE_COMPLETE;
586 break;
587 case VUSBSNIFFEREVENT_ERROR_SUBMIT:
588 case VUSBSNIFFEREVENT_ERROR_COMPLETE:
589 UsbHdr.u8EventType = DUMPFILE_USB_EVENT_TYPE_ERROR;
590 break;
591 default:
592 AssertMsgFailed(("Invalid event type %d\n", enmEvent));
593 }
594 switch (pUrb->enmType)
595 {
596 case VUSBXFERTYPE_ISOC:
597 UsbHdr.u8TransferType = 0;
598 break;
599 case VUSBXFERTYPE_BULK:
600 UsbHdr.u8TransferType = 3;
601 break;
602 case VUSBXFERTYPE_INTR:
603 UsbHdr.u8TransferType = 1;
604 break;
605 case VUSBXFERTYPE_CTRL:
606 case VUSBXFERTYPE_MSG:
607 UsbHdr.u8TransferType = 2;
608 break;
609 default:
610 AssertMsgFailed(("invalid transfer type %d\n", pUrb->enmType));
611 }
612
613 bool fRecordData = false;
614 size_t cbUrbLength = 0;
615
616 if ( pUrb->cbData
617 && ( ( pUrb->enmDir == VUSBDIRECTION_OUT
618 && pUrb->enmType != VUSBXFERTYPE_CTRL
619 && pUrb->enmType != VUSBXFERTYPE_MSG
620 && enmEvent == VUSBSNIFFEREVENT_SUBMIT)
621 || ( pUrb->enmDir == VUSBDIRECTION_IN
622 && enmEvent == VUSBSNIFFEREVENT_COMPLETE)))
623 {
624 Epb.u32CapturedLen = sizeof(UsbHdr) + pUrb->cbData;
625 Epb.u32PacketLen = sizeof(UsbHdr) + pUrb->cbData;
626 fRecordData = true;
627 }
628 else
629 {
630 Epb.u32CapturedLen = sizeof(UsbHdr);
631 Epb.u32PacketLen = sizeof(UsbHdr);
632 }
633
634 if (pUrb->enmType == VUSBXFERTYPE_MSG || pUrb->enmType == VUSBXFERTYPE_CTRL)
635 {
636 PVUSBSETUP pSetup = (PVUSBSETUP)pUrb->abData;
637 cbUrbLength = pSetup->wLength;
638 }
639 else
640 cbUrbLength = pUrb->cbData;
641
642 UsbHdr.u8EndpointNumber = pUrb->EndPt | (pUrb->enmDir == VUSBDIRECTION_IN ? 0x80 : 0x00);
643 UsbHdr.u8DeviceAddress = pUrb->DstAddress;
644 UsbHdr.u16BusId = 0;
645 UsbHdr.u8DataFlag = fRecordData ? 0 : 1;
646 UsbHdr.u64TimestampSec = u64TimestampEvent / RT_NS_1SEC_64;;
647 UsbHdr.u32TimestampUSec = u64TimestampEvent / RT_NS_1US_64 - UsbHdr.u64TimestampSec * RT_US_1SEC;
648 UsbHdr.i32Status = pUrb->enmStatus;
649 UsbHdr.u32UrbLength = cbUrbLength;
650 UsbHdr.u32DataLength = fRecordData ? pUrb->cbData : 0;
651 UsbHdr.i32Interval = 0;
652 UsbHdr.i32StartFrame = 0;
653 UsbHdr.u32XferFlags = 0;
654 UsbHdr.u32NumDesc = pUrb->enmType == VUSBXFERTYPE_ISOC ? pUrb->cIsocPkts : 0;
655
656 if ( (pUrb->enmType == VUSBXFERTYPE_MSG || pUrb->enmType == VUSBXFERTYPE_CTRL)
657 && enmEvent == VUSBSNIFFEREVENT_SUBMIT)
658 {
659 PVUSBSETUP pSetup = (PVUSBSETUP)pUrb->abData;
660
661 UsbHdr.u.UsbSetup.bmRequestType = pSetup->bmRequestType;
662 UsbHdr.u.UsbSetup.bRequest = pSetup->bRequest;
663 UsbHdr.u.UsbSetup.wValue = pSetup->wValue;
664 UsbHdr.u.UsbSetup.wIndex = pSetup->wIndex;
665 UsbHdr.u.UsbSetup.wLength = pSetup->wLength;
666 UsbHdr.u8SetupFlag = 0;
667 }
668 else
669 UsbHdr.u8SetupFlag = '-'; /* Follow usbmon source here. */
670
671 rc = vusbSnifferBlockNew(pThis, &Epb.Hdr, sizeof(Epb));
672 if (RT_SUCCESS(rc))
673 rc = vusbSnifferBlockAddData(pThis, &UsbHdr, sizeof(UsbHdr));
674
675 if (pUrb->enmType == VUSBXFERTYPE_ISOC)
676 {
677 /* Add Isochronous descriptors now. */
678 for (unsigned i = 0; i < pUrb->cIsocPkts && RT_SUCCESS(rc); i++)
679 {
680 DumpFileUsbIsoDesc IsoDesc;
681 IsoDesc.i32Status = pUrb->aIsocPkts[i].enmStatus;
682 IsoDesc.u32Offset = pUrb->aIsocPkts[i].off;
683 IsoDesc.u32Len = pUrb->aIsocPkts[i].cb;
684 rc = vusbSnifferBlockAddData(pThis, &IsoDesc, sizeof(IsoDesc));
685 }
686 }
687
688 /* Record data. */
689 if ( RT_SUCCESS(rc)
690 && fRecordData)
691 rc = vusbSnifferBlockAddData(pThis, pUrb->abData, pUrb->cbData);
692
693 if (RT_SUCCESS(rc))
694 rc = vusbSnifferAddOption(pThis, DUMPFILE_OPTION_CODE_END, NULL, 0);
695
696 if (RT_SUCCESS(rc))
697 rc = vusbSnifferBlockCommit(pThis);
698
699 return rc;
700}
701
Note: See TracBrowser for help on using the repository browser.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette