OVR_HIDDeviceImpl.h
Go to the documentation of this file.
00001 /************************************************************************************
00002 
00003 Filename    :   OVR_HIDDeviceImpl.h
00004 Content     :   Implementation of HIDDevice.
00005 Created     :   March 7, 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_HIDDeviceImpl_h
00017 #define OVR_HIDDeviceImpl_h
00018 
00019 //#include "OVR_Device.h"
00020 #include "OVR_DeviceImpl.h"
00021 
00022 namespace OVR {
00023 
00024 //-------------------------------------------------------------------------------------
00025 class HIDDeviceCreateDesc : public DeviceCreateDesc
00026 {
00027 public:
00028     HIDDeviceCreateDesc(DeviceFactory* factory, DeviceType type, const HIDDeviceDesc& hidDesc)
00029         : DeviceCreateDesc(factory, type), HIDDesc(hidDesc) { }
00030     HIDDeviceCreateDesc(const HIDDeviceCreateDesc& other)
00031         : DeviceCreateDesc(other.pFactory, other.Type), HIDDesc(other.HIDDesc) { }
00032 
00033     virtual bool MatchDevice(const String& path)
00034     {
00035         // should it be case insensitive?
00036         return HIDDesc.Path.CompareNoCase(path) == 0;
00037     }
00038 
00039     HIDDeviceDesc HIDDesc;
00040 };
00041 
00042 //-------------------------------------------------------------------------------------
00043 template<class B>
00044 class HIDDeviceImpl : public DeviceImpl<B>, public HIDDevice::HIDHandler
00045 {
00046 public:
00047     HIDDeviceImpl(HIDDeviceCreateDesc* createDesc, DeviceBase* parent)
00048      :  DeviceImpl<B>(createDesc, parent)        
00049     {
00050     }
00051 
00052     // HIDDevice::Handler interface.
00053     virtual void OnDeviceMessage(HIDDeviceMessageType messageType)
00054     {
00055         MessageType handlerMessageType;
00056         switch (messageType) {
00057             case HIDDeviceMessage_DeviceAdded:
00058                 handlerMessageType = Message_DeviceAdded;
00059                 break;
00060 
00061             case HIDDeviceMessage_DeviceRemoved:
00062                 handlerMessageType = Message_DeviceRemoved;
00063                 break;
00064 
00065             default: OVR_ASSERT(0); return;
00066         }
00067 
00068         // Do device notification.
00069         {
00070             Lock::Locker scopeLock(this->HandlerRef.GetLock());
00071 
00072             if (this->HandlerRef.GetHandler())
00073             {
00074                 MessageDeviceStatus status(handlerMessageType, this, OVR::DeviceHandle(this->pCreateDesc));
00075                 this->HandlerRef.GetHandler()->OnMessage(status);
00076             }
00077         }
00078 
00079         // Do device manager notification.
00080         DeviceManagerImpl*   manager = this->GetManagerImpl();
00081         switch (handlerMessageType) {
00082             case Message_DeviceAdded:
00083                 manager->CallOnDeviceAdded(this->pCreateDesc);
00084                 break;
00085                 
00086             case Message_DeviceRemoved:
00087                 manager->CallOnDeviceRemoved(this->pCreateDesc);
00088                 break;
00089                 
00090             default:;
00091         }
00092     }
00093 
00094     virtual bool Initialize(DeviceBase* parent)
00095     {
00096         // Open HID device.
00097         HIDDeviceDesc&          hidDesc = *getHIDDesc();
00098         HIDDeviceManager*   pManager = GetHIDDeviceManager();
00099 
00100 
00101         HIDDevice* device = pManager->Open(hidDesc.Path);
00102         if (!device)
00103         {
00104             return false;
00105         }
00106 
00107         InternalDevice = *device;
00108         InternalDevice->SetHandler(this);
00109 
00110         // AddRef() to parent, forcing chain to stay alive.
00111         DeviceImpl<B>::pParent = parent;
00112 
00113         return true;
00114     }
00115 
00116     virtual void Shutdown()
00117     {   
00118         InternalDevice->SetHandler(NULL);
00119 
00120         // Remove the handler, if any.
00121         this->HandlerRef.SetHandler(0);
00122 
00123         DeviceImpl<B>::pParent.Clear();
00124     }
00125 
00126     DeviceManager* GetDeviceManager()
00127     {
00128         return DeviceImpl<B>::pCreateDesc->GetManagerImpl();
00129     }
00130 
00131     HIDDeviceManager* GetHIDDeviceManager()
00132     {
00133         return DeviceImpl<B>::pCreateDesc->GetManagerImpl()->GetHIDDeviceManager();
00134     }
00135 
00136 
00137     struct WriteData
00138     {
00139         enum { BufferSize = 64 };
00140         UByte Buffer[64];
00141         UPInt Size;
00142 
00143         WriteData(UByte* data, UPInt size) : Size(size)
00144         {
00145             OVR_ASSERT(size <= BufferSize);
00146             memcpy(Buffer, data, size);
00147         }
00148     };
00149 
00150     bool SetFeatureReport(UByte* data, UInt32 length)
00151     { 
00152         WriteData writeData(data, length);
00153 
00154         // Push call with wait.
00155         bool result = false;
00156 
00157                 ThreadCommandQueue* pQueue = this->GetManagerImpl()->GetThreadQueue();
00158         if (!pQueue->PushCallAndWaitResult(this, &HIDDeviceImpl::setFeatureReport, &result, writeData))
00159             return false;
00160 
00161         return result;
00162     }
00163 
00164     bool setFeatureReport(const WriteData& data)
00165     {
00166         return InternalDevice->SetFeatureReport((UByte*) data.Buffer, (UInt32) data.Size);
00167     }
00168 
00169     bool GetFeatureReport(UByte* data, UInt32 length)
00170     { 
00171         bool result = false;
00172 
00173                 ThreadCommandQueue* pQueue = this->GetManagerImpl()->GetThreadQueue();
00174         if (!pQueue->PushCallAndWaitResult(this, &HIDDeviceImpl::getFeatureReport, &result, data, length))
00175             return false;
00176 
00177         return result;
00178     }
00179 
00180     bool getFeatureReport(UByte* data, UInt32 length)
00181     {
00182         return InternalDevice->GetFeatureReport(data, length);
00183     }
00184 
00185 protected:
00186     HIDDevice* GetInternalDevice() const
00187     {
00188         return InternalDevice;
00189     }
00190 
00191     HIDDeviceDesc* getHIDDesc() const
00192     { return &getCreateDesc()->HIDDesc; }
00193 
00194     HIDDeviceCreateDesc* getCreateDesc() const
00195     { return (HIDDeviceCreateDesc*) &(*DeviceImpl<B>::pCreateDesc); }
00196 
00197 private:
00198     Ptr<HIDDevice> InternalDevice;
00199 };
00200 
00201 } // namespace OVR
00202 
00203 #endif


oculus_sdk
Author(s):
autogenerated on Fri Aug 28 2015 11:53:11