8.7 KiB
source_files, generated_at, model, schema_version, sha256
| source_files | generated_at | model | schema_version | sha256 | |||
|---|---|---|---|---|---|---|---|
|
2026-04-16T11:50:38.580755+00:00 | zai-org/GLM-5-FP8 | 1 | 2043cd8e3c0f04ef |
Documentation: DTS.Common.WINUSBConnection
1. Purpose
This module provides USB connectivity abstractions within the DTS.Common.WINUSBConnection namespace. It contains two concrete implementations of the IConnection interface: CDCUSBConnection, which wraps a standard System.IO.Ports.SerialPort for CDC (Communications Device Class) USB devices, and WINUSBConnection, which provides low-level communication via the native winusb.dll API for custom WinUSB devices. Additionally, it defines the internal P/Invoke signatures and data structures required to interact with the Windows WinUSB driver stack.
2. Public Interface
Class: CDCUSBConnection
Implements IConnection. Represents a USB device connection via a virtual COM port (CDC).
void Create(string connectString, string hostIPAddress)- Parses the
connectStringto match against registry keys inRegKeys. If a match is found, it retrieves thePortNamefrom the registry (Device Parameterssubkey) and configures the internalSerialPortinstance.
- Parses the
IAsyncResult BeginConnect(AsyncCallback cb, object state)- Initiates an asynchronous connection. Queues the connection logic to the thread pool.
void EndConnect(IAsyncResult ar)- Completes the asynchronous connection. Configures the serial port (BaudRate, DataBits, etc.) and calls
_comPort.Open(). SetsConnectedtotrue.
- Completes the asynchronous connection. Configures the serial port (BaudRate, DataBits, etc.) and calls
IAsyncResult BeginSend(byte[] buffer, int offset, int size, AsyncCallback cb, object state)- Initiates an asynchronous send operation.
int EndSend(IAsyncResult ar)- Completes the send operation. Writes data to the internal
_comPortusing_comPort.Write.
- Completes the send operation. Writes data to the internal
Task<int> SendAsync(byte[] sendBuffer, int bufferStartOffset, int bufferSizeToSend)- Task-based asynchronous wrapper for
BeginSend/EndSend.
- Task-based asynchronous wrapper for
IAsyncResult BeginReceive(byte[] buffer, int offset, int size, AsyncCallback cb, object state)- Initiates an asynchronous receive operation.
int EndReceive(IAsyncResult ar)- Completes the receive operation. Reads data from
_comPort. Note: This method blocks in a loop (Thread.Sleep(1)) until at least one byte is read.
- Completes the receive operation. Reads data from
IAsyncResult BeginDisconnect(bool reuseSocket, AsyncCallback cb, Object state)- Initiates an asynchronous disconnect.
void EndDisconnect(IAsyncResult ar)- Completes the disconnect. Closes and disposes the internal
_comPort.
- Completes the disconnect. Closes and disposes the internal
- Properties
bool Connected: Gets the current connection state.string ConnectString: Returns the device pathname.IList<string> RegKeys: Static property that lazily loads registry keys for the device fromSYSTEM\CurrentControlSet\Enum\USB\.
Class: WINUSBConnection
Implements IConnection. Represents a USB device connection using the native WinUSB driver.
void Create(string connectString, string hostIPAddress)- Stores the
connectStringin theConnectStringproperty.
- Stores the
IAsyncResult BeginConnect(AsyncCallback cb, object state)- Initiates an asynchronous connection. Throws
NotConnectedExceptionif already connected.
- Initiates an asynchronous connection. Throws
void EndConnect(IAsyncResult ar)- Completes the connection. Instantiates a new
WinUsbDevice, retrieves a device handle viaGetDeviceHandle, and initializes the device viaInitializeDevice. Uses a mutex (_usbConnectionMutex) to synchronize access.
- Completes the connection. Instantiates a new
IAsyncResult BeginSend(byte[] buffer, int offset, int size, AsyncCallback cb, object state)- Initiates an asynchronous send. Validates buffer parameters.
int EndSend(IAsyncResult ar)- Completes the send. Checks
MyDevInfo.UseHybridBulkIntMode. If true, callsSendViaInterruptTransfer; otherwise, callsSendViaBulkTransfer.
- Completes the send. Checks
Task<int> SendAsync(byte[] sendBuffer, int bufferStartOffset, int bufferSizeToSend)- Task-based asynchronous wrapper for
BeginSend/EndSend.
- Task-based asynchronous wrapper for
IAsyncResult BeginReceive(byte[] buffer, int offset, int size, AsyncCallback cb, object state)- Initiates an asynchronous receive.
int EndReceive(IAsyncResult ar)- Completes the receive. Reads data via
ReadViaBulkTransfer. Blocks in a loop (Thread.Sleep(1)) until bytes are read. Returns 0 if the connection fails or closes.
- Completes the receive. Reads data via
IAsyncResult BeginDisconnect(bool reuseSocket, AsyncCallback cb, Object state)- Initiates an asynchronous disconnect.
void EndDisconnect(IAsyncResult ar)- Completes the disconnect. Calls
DisposeSliceDevto release the WinUSB handle.
- Completes the disconnect. Calls
- Properties
bool Connected: Gets the current connection state.string ConnectString: Gets the device pathname.
Internal Class: WinUsbDevice (Partial)
Defines P/Invoke signatures and structures for winusb.dll.
- Structs:
USBConfigurationDescriptor,USBInterfaceDescriptor,WinUSBPipeInformation,WinUSBSetupPacket. - Enums:
PolicyType,USBDPipeTypes,USBDeviceSpeeds. - Methods:
WinUsb_Initialize,WinUsb_Free,WinUsb_ControlTransfer,WinUsb_ReadPipe,WinUsb_WritePipe,WinUsb_QueryPipe,WinUsb_SetPipePolicy, etc.
3. Invariants
- Registry Dependency (CDCUSBConnection): The
CDCUSBConnectionrequires the device to be registered underSYSTEM\CurrentControlSet\Enum\USB\with a Vendor ID ofVID_1CB9and Product ID ofPID_001A. TheCreatemethod cannot resolve the port name if these registry keys are missing. - Thread Safety (WINUSBConnection): The
WINUSBConnection.EndConnectandDisposeSliceDevmethods rely on a named Mutex ("USBConnection") to prevent race conditions during device initialization and disposal. - Disposal State: Both connection classes track
DisposedandDisposingflags. Methods generally do not proceed ifDisposedis true. - APM Pattern: Both classes implement the Asynchronous Programming Model (APM) using internal
IAsyncResultimplementations (UsbRecAsyncResultandWinUSBRecAsyncResult). Callbacks are invoked viaThreadPool.QueueUserWorkItem.
4. Dependencies
Internal Dependencies
DTS.Common.Interface.Connection: ImplementsIConnection.DTS.Common.DASResource: Uses localized strings for exception messages (e.g.,Strings.CDCUSBConnection_BeginConnect_Err1).DTS.Common.Utilities.Logging: UsesAPILoggerfor logging exceptions and status messages.DTS.Common.USBFramework: UsesDeviceManagementclass (inWINUSBConnection).DTS.Common.Classes.Connection: UsesNotConnectedException.
External Dependencies
System.IO.Ports: Used byCDCUSBConnectionforSerialPort.System.Runtime.InteropServices: Used for P/Invoke attributes.Microsoft.Win32: Used for registry access (RegistryKey,SafeFileHandle).winusb.dll: Native Windows DLL imported byWinUsbDevice.
5. Gotchas
- Blocking Async Reads: The
EndReceivemethods in bothCDCUSBConnectionandWINUSBConnectioncontainwhileloops that sleep (Thread.Sleep(1)) if zero bytes are read. This effectively makes the "asynchronous" operation blocking and consumes a thread pool thread while waiting for data. - Recursive Comment: The
Create(string connectString)method in both connection classes contains a commented-out line//Create(connectString);with a note "this is recursive!". This suggests a historical bug or confusion regarding method overloads. - Empty Soft Disconnect:
SoftConnectandSoftDisconnectmethods are implemented but empty. The comments explicitly state they do nothing because there is no soft disconnect option implemented. - Hardcoded Device IDs:
CDCUSBConnectionhardcodesDTS_VENDOR_ID(0x1CB9) andDTS_TSR2_CDCUSB_PRODUCT_ID_STR("PID_001A"). It will not work with other USB devices. - Partial Class Source:
WinUsbDeviceis defined asinternal sealed partial class. The logic forGetDeviceHandle,InitializeDevice,SendViaBulkTransfer, etc., is referenced inWINUSBConnectionbut is not present in the provided source files (likely in another file part of the partial class definition). - Hybrid Mode Logic:
WINUSBConnection.EndSendswitches between interrupt and bulk transfers based on a booleanUseHybridBulkIntModelocated on aMyDevInfoobject. The definition ofMyDevInfois not visible in the provided source.