main.h
资源名称:虚拟串口驱动程序.rar [点击查看]
上传用户:zanmei2
上传日期:2010-03-06
资源大小:775k
文件大小:9k
源码类别:
通讯编程文档
开发平台:
C/C++
- /*++
- Copyright (c) 2005 Changzhi Zhou All Rights Reserved
- Module Name:
- main.h
- Abstract:
- Declaration for all functions.
- Define all structures.
- Environment:
- Kernel mode
- Revision History:
- Changzhi Zhou Dec 20 2004
- --*/
- //#if !defined(_FILTER_H_)
- //#define _FILTER_H_
- #ifndef __MAIN_H_
- #define __MAIN_H_
- #include <csq.h> // Include this header file and link with csq.lib to use this sample on Win2K.
- #include "tdi.h"
- #define DRIVERNAME "Sample: "
- #define ORIGINALDATA_SIZE 65536
- #if DBG
- #define DebugPrint(_x_)
- DbgPrint (DRIVERNAME);
- DbgPrint _x_;
- #define TRAP() DbgBreakPoint()
- #else
- #define DebugPrint(_x_)
- #define TRAP()
- #endif
- #if DBG
- #define CSAMP_KDPRINT(_x_)
- DbgPrint("CancelIrp.c: ");
- DbgPrint _x_;
- #else
- #define CSAMP_KDPRINT(_x_)
- #endif
- #include "wmilib.h"
- #include <ntddser.h>
- #define DEVICE_OBJECT_NAME_LENGTH 128
- #define DOS_DEVICE_NAME L"\DosDevices\COM"
- #define SYMBOLIC_NAME_LENGTH 128
- #define SERIAL_DEVICE_MAP L"SERIALCOMM"
- #define DEVICE_NAME L"\Device\Serial"
- #define RINGBUFFER_SIZE 65536
- #define RINGBUFFER_SIZE_80FULL 52428
- #define RECVREMAINDER_BUFFER_SIZE 65536
- // read interval timeout 1 second
- #define READINTERVAL_TIMEOUT 10000000
- //
- // These are the states Filter transition to upon
- // receiving a specific PnP Irp. Refer to the PnP Device States
- // diagram in DDK documentation for better understanding.
- //
- typedef enum _DEVICE_PNP_STATE {
- NotStarted, // not started
- Stopped, // device stopped
- Working, // started and working
- //Connecting,
- PendingStop, // stop pending
- PendingRemove, // remove pending
- SurpriseRemoved, // removed by surprise
- Removed // removed
- } DEVICE_PNP_STATE;
- #define INITIALIZE_PNP_STATE(_Data_)
- (_Data_)->DevicePnPState = NotStarted;
- (_Data_)->PreviousPnPState = NotStarted;
- #define SET_NEW_PNP_STATE(_Data_, _state_)
- KeAcquireSpinLock( &(_Data_)->DevStateLock, &oldIrql );
- (_Data_)->PreviousPnPState = (_Data_)->DevicePnPState;
- (_Data_)->DevicePnPState = (_state_);
- KeReleaseSpinLock( &(_Data_)->DevStateLock, oldIrql );
- #define RESTORE_PREVIOUS_PNP_STATE(_Data_)
- KeAcquireSpinLock( &(_Data_)->DevStateLock, &oldIrql );
- (_Data_)->DevicePnPState = (_Data_)->PreviousPnPState;
- KeReleaseSpinLock( &(_Data_)->DevStateLock, oldIrql );
- typedef struct _RecvContext{
- PIRP pIrp;
- PMDL pMdl;
- BOOLEAN bLocked;
- IO_STATUS_BLOCK IoStatus;
- KEVENT Event;
- TDI_CONNECTION_INFORMATION ReceiveDatagramInfo;
- TDI_CONNECTION_INFORMATION ReturnInfo;
- PVOID RemainderBuffer;
- }RECV_CONTEXT, *PRECV_CONTEXT;
- typedef struct _DEVICE_EXTENSION
- {
- // regular varialbes
- PDEVICE_OBJECT Self;
- PDEVICE_OBJECT NextLowerDriver;
- // PnP state
- DEVICE_PNP_STATE DevicePnPState;
- DEVICE_PNP_STATE PreviousPnPState;
- // obtain and hold this lock while changing the device state,
- // the queue state and while processing the queue.
- KSPIN_LOCK DevStateLock;
- // relevant to PnP
- KEVENT RemoveEvent;
- KEVENT StopEvent;
- ULONG OutStandingIO;
- KSPIN_LOCK IOCountLock;
- BOOLEAN bIsOpen;
- // Names
- UNICODE_STRING InterfaceName; // The name returned from IoRegisterDeviceInterface,
- UNICODE_STRING DeviceName;
- UNICODE_STRING SymbolicLinkName;
- UNICODE_STRING DosName;
- ULONG ComX;
- BOOLEAN CreatedSymbolicLink;
- BOOLEAN CreatedSerialCommEntry;
- KSPIN_LOCK ThreadSpinLock; // Spinlock for threads
- // relevant to Serial Port
- ULONG CurrentBaud;
- SERIAL_TIMEOUTS Timeouts;
- SERIAL_LINE_CONTROL LineControl;
- SERIAL_CHARS SpecialChars;
- SERIAL_STATUS SerialStatus;
- ULONG BufferSize;
- SERIAL_HANDFLOW HandFlow;
- ULONG WaitMask;
- PIRP WaitOnMaskIrp;
- PIRP PendingReadIrp;
- KTIMER ReadTimer;
- KDPC ReadDpc;
- // relevant to read/write buffer
- PUCHAR RxBuffer;
- PUCHAR lpRead;
- PUCHAR lpRx;
- // SpinLock to protect access to the queue
- /* KSPIN_LOCK QueueLock;
- IO_CSQ CancelSafeQueue;
- // Irps waiting to be processed are queued here
- LIST_ENTRY PendingIrpQueue;
- */
- // variables about TDI client
- HANDLE hTransAddr;
- PFILE_OBJECT lpTransAddrFileObject;
- PDEVICE_OBJECT TDILowerDeviceObject;
- RECV_CONTEXT recvContext;
- USHORT RemotePort;
- ULONG RemoteAddress;
- // WMI
- WMILIB_CONTEXT WmiLibInfo; // WMI Information
- } DEVICE_EXTENSION, *PDEVICE_EXTENSION;
- #if 0
- typedef struct _ProcContext{
- PDEVICE_EXTENSION deviceExtension;
- PIRP GetTimeIrp;
- KTIMER Timer;
- KDPC dpc;
- }PROC_CONTEXT, *PPROC_CONTEXT;
- typedef struct _ShareMemoryData{
- PFILE_OBJECT pFileObject;
- PVOID SystemVirtualAddress;
- PVOID UserVirtualAddress;
- PMDL Mdl;
- LIST_ENTRY Link;
- } SHARE_MEMORY_DATA, *PSHARE_MEMORY_DATA;
- #endif
- PCHAR
- PnPMinorFunctionString (
- UCHAR MinorFunction
- );
- NTSTATUS
- AddDevice(
- IN PDRIVER_OBJECT DriverObject,
- IN PDEVICE_OBJECT PhysicalDeviceObject
- );
- NTSTATUS
- SamplePnpDispatch (
- IN PDEVICE_OBJECT DeviceObject,
- IN PIRP Irp
- );
- NTSTATUS
- SamplePowerDispatch(
- IN PDEVICE_OBJECT DeviceObject,
- IN PIRP Irp
- );
- VOID
- Unload(
- IN PDRIVER_OBJECT DriverObject
- );
- NTSTATUS DefaultPnpHandler(
- IN PDEVICE_OBJECT DeviceObject,
- IN PIRP Irp
- );
- NTSTATUS PnpStartDevice(
- IN PDEVICE_OBJECT DeviceObject,
- IN PIRP Irp
- );
- NTSTATUS PnpRemoveDevice(
- IN PDEVICE_OBJECT DeviceObject,
- IN PIRP Irp
- );
- NTSTATUS ForwardAndWait(
- IN PDEVICE_OBJECT DeviceObject,
- IN PIRP Irp
- );
- NTSTATUS SampleCreate (
- IN PDEVICE_OBJECT DeviceObject,
- IN PIRP Irp
- );
- NTSTATUS SampleClose(
- IN PDEVICE_OBJECT DeviceObject,
- IN PIRP Irp
- );
- NTSTATUS SampleIoControl(
- IN PDEVICE_OBJECT DeviceObject,
- IN PIRP Irp
- );
- NTSTATUS SampleCleanup(
- IN PDEVICE_OBJECT DeviceObject,
- IN PIRP Irp
- );
- NTSTATUS PnpSurpriseRemoval(
- IN PDEVICE_OBJECT DeviceObject,
- IN PIRP Irp
- );
- NTSTATUS CompleteRequest (
- IN PIRP Irp,
- IN NTSTATUS status,
- IN ULONG info
- );
- NTSTATUS OnRequestComplete(
- IN PDEVICE_OBJECT DeviceObject,
- IN PIRP Irp,
- IN PKEVENT event
- );
- NTSTATUS
- SampleWrite(
- IN PDEVICE_OBJECT DeviceObject,
- IN PIRP Irp
- );
- NTSTATUS
- SampleRead(
- IN PDEVICE_OBJECT DeviceObject,
- IN PIRP Irp
- );
- VOID SampleCancelRoutine(
- IN PDEVICE_OBJECT DeviceObject,
- IN PIRP Irp
- );
- NTSTATUS SampleCompletionRoutine(
- IN PDEVICE_OBJECT DeviceObject,
- IN PIRP Irp,
- IN PVOID Context
- );
- NTSTATUS
- ToasterSystemControl (
- IN PDEVICE_OBJECT DeviceObject,
- IN PIRP Irp
- );
- NTSTATUS
- PnpQueryStopDevice(
- IN PDEVICE_OBJECT DeviceObject,
- IN PIRP Irp
- );
- NTSTATUS
- PnpCancelStopDevice(
- IN PDEVICE_OBJECT DeviceObject,
- IN PIRP Irp
- );
- NTSTATUS
- PnpQueryRemoveDevice(
- IN PDEVICE_OBJECT DeviceObject,
- IN PIRP Irp
- );
- NTSTATUS
- PnpCancelRemoveDevice(
- IN PDEVICE_OBJECT DeviceObject,
- IN PIRP Irp
- );
- NTSTATUS PnpQueryCapabilities(
- IN PDEVICE_OBJECT DeviceObject,
- IN PIRP Irp
- );
- LONG SampleIoIncrement(
- IN OUT PDEVICE_EXTENSION DeviceExtension
- );
- LONG SampleIoDecrement(
- IN OUT PDEVICE_EXTENSION DeviceExtension
- );
- BOOLEAN InitializeSerialDevName(
- PUNICODE_STRING lpDeviceName
- );
- NTSTATUS
- SerialDoExternalNaming(
- IN PDEVICE_EXTENSION deviceExtension,
- IN LONG ComX
- );
- NTSTATUS
- SerialRemoveDevObj(
- IN PDEVICE_OBJECT PDevObj
- );
- NTSTATUS
- SerialUndoExternalNaming(
- IN PDEVICE_EXTENSION deviceExtension
- );
- NTSTATUS CreateDosName( IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp);
- VOID
- ReadDpcRoutine(
- IN PKDPC Dpc,
- IN PVOID DeferredContext,
- IN PVOID SystemArgument1,
- IN PVOID SystemArgument2
- );
- ULONG CopyFromRingBuffer(
- PDEVICE_EXTENSION deviceExtension,
- PIRP Irp );
- // CancelIrp
- VOID CsampInsertIrp (
- IN PIO_CSQ Csq,
- IN PIRP Irp
- );
- VOID CsampRemoveIrp(
- IN PIO_CSQ Csq,
- IN PIRP Irp
- );
- PIRP CsampPeekNextIrp(
- IN PIO_CSQ Csq,
- IN PIRP Irp,
- IN PVOID PeekContext
- );
- VOID CsampAcquireLock(
- IN PIO_CSQ Csq,
- OUT PKIRQL Irql
- );
- VOID CsampReleaseLock(
- IN PIO_CSQ Csq,
- IN KIRQL Irql
- );
- VOID CsampCompleteCanceledIrp(
- IN PIO_CSQ pCsq,
- IN PIRP Irp
- );
- // TDI
- NTSTATUS Disconnection ( PDEVICE_EXTENSION deviceExtension );
- NTSTATUS InitializeConnection( PDEVICE_EXTENSION deviceExtension );
- NTSTATUS TDIQueryNetworkInformation( PDEVICE_OBJECT DeviceObject, PIRP Irp );
- NTSTATUS TDIRecvDatagram ( PDEVICE_EXTENSION deviceExtension );
- NTSTATUS TDISendDatagram( DeviceObject, Irp );
- NTSTATUS
- DriverEntry(
- IN PDRIVER_OBJECT DriverObject,
- IN PUNICODE_STRING RegistryPath
- );
- #endif