OVR_Win32_HIDDevice.h
Go to the documentation of this file.
00001 /************************************************************************************
00002 
00003 Filename    :   OVR_Win32_HIDDevice.h
00004 Content     :   Win32 HID device implementation.
00005 Created     :   February 22, 2013
00006 Authors     :   Lee Cooper
00007 
00008 Copyright   :   Copyright 2013 Oculus VR, Inc. All Rights reserved.
00009 
00010 Use of this software is subject to the terms of the Oculus license
00011 agreement provided at the time of installation or download, or which
00012 otherwise accompanies this software in either electronic or hard copy form.
00013 
00014 *************************************************************************************/
00015 
00016 #ifndef OVR_Win32_HIDDevice_h
00017 #define OVR_Win32_HIDDevice_h
00018 
00019 #include "OVR_HIDDevice.h"
00020 #include "OVR_Win32_DeviceManager.h"
00021 
00022 #include <windows.h>
00023 #include <setupapi.h>
00024 
00025 //-------------------------------------------------------------------------------------
00026 // Define needed "hidsdi.h" functionality to avoid requiring DDK installation.
00027 // #include "hidsdi.h"
00028 
00029 #ifndef _HIDSDI_H
00030 #define _HIDSDI_H
00031 #include <pshpack4.h>
00032 
00033 #define HIDP_STATUS_SUCCESS (0x11 << 16)
00034 struct HIDP_PREPARSED_DATA;
00035 
00036 struct HIDD_ATTRIBUTES
00037 {
00038     ULONG   Size; // = sizeof (struct _HIDD_ATTRIBUTES)
00039     USHORT  VendorID;
00040     USHORT  ProductID;
00041     USHORT  VersionNumber;
00042 };
00043 
00044 struct HIDP_CAPS
00045 {
00046     USHORT   Usage;
00047     USHORT   UsagePage;
00048     USHORT   InputReportByteLength;
00049     USHORT   OutputReportByteLength;
00050     USHORT   FeatureReportByteLength;
00051     USHORT   Reserved[17];
00052 
00053     USHORT   NumberLinkCollectionNodes;
00054     USHORT   NumberInputButtonCaps;
00055     USHORT   NumberInputValueCaps;
00056     USHORT   NumberInputDataIndices;
00057     USHORT   NumberOutputButtonCaps;
00058     USHORT   NumberOutputValueCaps;
00059     USHORT   NumberOutputDataIndices;
00060     USHORT   NumberFeatureButtonCaps;
00061     USHORT   NumberFeatureValueCaps;
00062     USHORT   NumberFeatureDataIndices;
00063 };
00064 
00065 #include <poppack.h>
00066 #endif
00067 
00068 
00069 namespace OVR { namespace Win32 { 
00070 
00071 class HIDDeviceManager;
00072 class DeviceManager;
00073 
00074 //-------------------------------------------------------------------------------------
00075 // ***** Win32 HIDDevice
00076 
00077 class HIDDevice : public OVR::HIDDevice, public DeviceManagerThread::Notifier
00078 {
00079 public:
00080 
00081     HIDDevice(HIDDeviceManager* manager);
00082 
00083     // This is a minimal constructor used during enumeration for us to pass
00084     // a HIDDevice to the visit function (so that it can query feature reports). 
00085     HIDDevice(HIDDeviceManager* manager, HANDLE device);
00086 
00087     ~HIDDevice();
00088 
00089     bool HIDInitialize(const String& path);
00090     void HIDShutdown();
00091 
00092     // OVR::HIDDevice
00093         bool SetFeatureReport(UByte* data, UInt32 length);
00094         bool GetFeatureReport(UByte* data, UInt32 length);
00095     
00096 
00097     // DeviceManagerThread::Notifier
00098     void OnOverlappedEvent(HANDLE hevent);
00099     UInt64 OnTicks(UInt64 ticksMks);
00100     bool OnDeviceMessage(DeviceMessageType messageType, const String& devicePath, bool* error);
00101 
00102 private:
00103     bool openDevice();
00104     bool initInfo();
00105     bool initializeRead();
00106     bool processReadResult();
00107     void closeDevice();
00108     void closeDeviceOnIOError();
00109 
00110     bool                inMinimalMode;
00111     HIDDeviceManager*   HIDManager;
00112         HANDLE              Device;
00113     HIDDeviceDesc       DevDesc; 
00114 
00115     OVERLAPPED          ReadOverlapped;
00116     bool                ReadRequested;
00117 
00118     enum { ReadBufferSize = 96 };
00119     UByte               ReadBuffer[ReadBufferSize];
00120 
00121     UInt16              InputReportBufferLength;
00122     UInt16              OutputReportBufferLength;
00123     UInt16              FeatureReportBufferLength;
00124 };
00125 
00126 //-------------------------------------------------------------------------------------
00127 // ***** Win32 HIDDeviceManager
00128 
00129 class HIDDeviceManager : public OVR::HIDDeviceManager
00130 {
00131         friend class HIDDevice;
00132 public:
00133 
00134         HIDDeviceManager(DeviceManager* manager);
00135     virtual ~HIDDeviceManager();
00136 
00137     virtual bool Initialize();
00138     virtual void Shutdown();
00139 
00140     virtual bool Enumerate(HIDEnumerateVisitor* enumVisitor);
00141     virtual OVR::HIDDevice* Open(const String& path);
00142 
00143     // Fills HIDDeviceDesc by using the path.
00144     // Returns 'true' if successful, 'false' otherwise.
00145     bool GetHIDDeviceDesc(const String& path, HIDDeviceDesc* pdevDesc) const;
00146 
00147     GUID GetHIDGuid() { return HidGuid; }
00148     
00149     static HIDDeviceManager* CreateInternal(DeviceManager* manager);
00150 
00151 private:
00152 
00153     DeviceManager* Manager;     // Back pointer can just be a raw pointer.
00154 
00155     HMODULE hHidLib;
00156     GUID    HidGuid;
00157 
00158     // Macros to declare and resolve needed functions from library.
00159 #define OVR_DECLARE_HIDFUNC(func, rettype, args)   \
00160 typedef rettype (__stdcall *PFn_##func) args;  \
00161 PFn_##func      func;
00162 #define OVR_RESOLVE_HIDFUNC(func) \
00163 func = (PFn_##func)::GetProcAddress(hHidLib, #func)
00164 
00165     OVR_DECLARE_HIDFUNC(HidD_GetHidGuid,            void,    (GUID *hidGuid));
00166     OVR_DECLARE_HIDFUNC(HidD_SetNumInputBuffers,    BOOLEAN, (HANDLE hidDev, ULONG numberBuffers));
00167     OVR_DECLARE_HIDFUNC(HidD_GetFeature,            BOOLEAN, (HANDLE hidDev, PVOID buffer, ULONG bufferLength));
00168     OVR_DECLARE_HIDFUNC(HidD_SetFeature,            BOOLEAN, (HANDLE hidDev, PVOID buffer, ULONG bufferLength));
00169     OVR_DECLARE_HIDFUNC(HidD_GetAttributes,         BOOLEAN, (HANDLE hidDev, HIDD_ATTRIBUTES *attributes));
00170     OVR_DECLARE_HIDFUNC(HidD_GetManufacturerString, BOOLEAN, (HANDLE hidDev, PVOID buffer, ULONG bufferLength));
00171     OVR_DECLARE_HIDFUNC(HidD_GetProductString,      BOOLEAN, (HANDLE hidDev, PVOID buffer, ULONG bufferLength));
00172     OVR_DECLARE_HIDFUNC(HidD_GetSerialNumberString, BOOLEAN, (HANDLE hidDev, PVOID buffer, ULONG bufferLength));
00173     OVR_DECLARE_HIDFUNC(HidD_GetPreparsedData,      BOOLEAN, (HANDLE hidDev, HIDP_PREPARSED_DATA **preparsedData));
00174     OVR_DECLARE_HIDFUNC(HidD_FreePreparsedData,     BOOLEAN, (HIDP_PREPARSED_DATA *preparsedData));
00175     OVR_DECLARE_HIDFUNC(HidP_GetCaps,               NTSTATUS,(HIDP_PREPARSED_DATA *preparsedData, HIDP_CAPS* caps));
00176 
00177     HANDLE CreateHIDFile(const char* path, bool exclusiveAccess = true) const
00178     {
00179         return ::CreateFileA(path, GENERIC_WRITE|GENERIC_READ,
00180             (!exclusiveAccess) ? (FILE_SHARE_READ|FILE_SHARE_WRITE) : 0x0, 
00181             NULL, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, 0);
00182     }
00183 
00184     // Helper functions to fill in HIDDeviceDesc from open device handle.
00185     bool initVendorProductVersion(HANDLE hidDev, HIDDeviceDesc* desc) const;
00186     bool initUsage(HANDLE hidDev, HIDDeviceDesc* desc) const;
00187     void initStrings(HANDLE hidDev, HIDDeviceDesc* desc) const;
00188 
00189     bool getFullDesc(HANDLE hidDev, HIDDeviceDesc* desc) const;
00190 };
00191 
00192 }} // namespace OVR::Win32
00193 
00194 #endif // OVR_Win32_HIDDevice_h


oculus_sdk
Author(s):
autogenerated on Mon Oct 6 2014 03:01:19