Bike-X  0.8
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
OVR_HIDDeviceImpl.h
Go to the documentation of this file.
1 /************************************************************************************
2 
3 Filename : OVR_HIDDeviceImpl.h
4 Content : Implementation of HIDDevice.
5 Created : March 7, 2013
6 Authors : Lee Cooper
7 
8 Copyright : Copyright 2014 Oculus VR, Inc. All Rights reserved.
9 
10 Licensed under the Oculus VR Rift SDK License Version 3.1 (the "License");
11 you may not use the Oculus VR Rift SDK except in compliance with the License,
12 which is provided at the time of installation or download, or which
13 otherwise accompanies this software in either electronic or hard copy form.
14 
15 You may obtain a copy of the License at
16 
17 http://www.oculusvr.com/licenses/LICENSE-3.1
18 
19 Unless required by applicable law or agreed to in writing, the Oculus VR SDK
20 distributed under the License is distributed on an "AS IS" BASIS,
21 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22 See the License for the specific language governing permissions and
23 limitations under the License.
24 
25 *************************************************************************************/
26 
27 #ifndef OVR_HIDDeviceImpl_h
28 #define OVR_HIDDeviceImpl_h
29 
30 //#include "OVR_Device.h"
31 #include "OVR_DeviceImpl.h"
32 
33 namespace OVR {
34 
35 //-------------------------------------------------------------------------------------
37 {
38 public:
40  : DeviceCreateDesc(factory, type), HIDDesc(hidDesc) { }
42  : DeviceCreateDesc(other.pFactory, other.Type), HIDDesc(other.HIDDesc) { }
43 
44  virtual bool MatchDevice(const String& path)
45  {
46  // should it be case insensitive?
47  return HIDDesc.Path.CompareNoCase(path) == 0;
48  }
49 
51 };
52 
53 //-------------------------------------------------------------------------------------
54 template<class B>
56 {
57 public:
59  : DeviceImpl<B>(createDesc, parent)
60  {
61  }
62 
63  // HIDDevice::Handler interface.
64  virtual void OnDeviceMessage(HIDDeviceMessageType messageType)
65  {
66  MessageType handlerMessageType;
67  switch (messageType) {
69  handlerMessageType = Message_DeviceAdded;
71  break;
72 
74  handlerMessageType = Message_DeviceRemoved;
76  break;
77 
78  default: OVR_ASSERT(0); return;
79  }
80 
81  // Do device notification.
82  MessageDeviceStatus status(handlerMessageType, this, OVR::DeviceHandle(this->pCreateDesc));
83  this->HandlerRef.Call(status);
84 
85  // Do device manager notification.
86  DeviceManagerImpl* manager = this->GetManagerImpl();
87  switch (handlerMessageType) {
89  manager->CallOnDeviceAdded(this->pCreateDesc);
90  break;
91 
93  manager->CallOnDeviceRemoved(this->pCreateDesc);
94  break;
95 
96  default:;
97  }
98  }
99 
100  virtual bool Initialize(DeviceBase* parent)
101  {
102  // Open HID device.
103  HIDDeviceDesc& hidDesc = *getHIDDesc();
105 
106 
107  HIDDevice* device = pManager->Open(hidDesc.Path);
108  if (!device)
109  {
110  return false;
111  }
112 
113  InternalDevice = *device;
114  InternalDevice->SetHandler(this);
115 
116  // AddRef() to parent, forcing chain to stay alive.
117  DeviceImpl<B>::pParent = parent;
118 
119  return true;
120  }
121 
122  virtual void Shutdown()
123  {
124  InternalDevice->SetHandler(NULL);
125 
126  DeviceImpl<B>::pParent.Clear();
127  }
128 
130  {
132  }
133 
135  {
137  }
138 
139  bool SetFeatureReport(UByte* data, UInt32 length)
140  {
141  // Push call with wait.
142  bool result = false;
143 
144  ThreadCommandQueue* pQueue = this->GetManagerImpl()->GetThreadQueue();
145  if (!pQueue->PushCallAndWaitResult(this, &HIDDeviceImpl::setFeatureReport, &result, data, length))
146  return false;
147 
148  return result;
149  }
150 
151  bool setFeatureReport(UByte* data, UInt32 length)
152  {
153  return InternalDevice->SetFeatureReport(data, length);
154  }
155 
156  bool GetFeatureReport(UByte* data, UInt32 length)
157  {
158  bool result = false;
159 
160  ThreadCommandQueue* pQueue = this->GetManagerImpl()->GetThreadQueue();
161  if (!pQueue->PushCallAndWaitResult(this, &HIDDeviceImpl::getFeatureReport, &result, data, length))
162  return false;
163 
164  return result;
165  }
166 
167  bool getFeatureReport(UByte* data, UInt32 length)
168  {
169  return InternalDevice->GetFeatureReport(data, length);
170  }
171 
173  {
174  UInt16 versionNumber = getHIDDesc()->VersionNumber;
175 
176  // Our interface and hardware versions are represented as two BCD digits each.
177  // Interface version is in the last two digits.
178  UByte interfaceVersion = (UByte) ((versionNumber & 0x000F) >> 0) * 1 +
179  ((versionNumber & 0x00F0) >> 4) * 10;
180  return interfaceVersion;
181  }
182 
183 protected:
185  {
186  return InternalDevice;
187  }
188 
190  { return &getCreateDesc()->HIDDesc; }
191 
194 
195 private:
197 };
198 
199 } // namespace OVR
200 
201 #endif
void CallOnDeviceRemoved(DeviceCreateDesc *desc)
DeviceFactory *const pFactory
bool getFeatureReport(UByte *data, UInt32 length)
HIDDeviceImpl(HIDDeviceCreateDesc *createDesc, DeviceBase *parent)
#define NULL
virtual ThreadCommandQueue * GetThreadQueue()=0
Ptr< DeviceCreateDesc > pCreateDesc
uint16_t UInt16
Definition: OVR_Types.h:251
uint32_t UInt32
Definition: OVR_Types.h:253
void CallOnDeviceAdded(DeviceCreateDesc *desc)
virtual void OnDeviceMessage(HIDDeviceMessageType messageType)
bool SetFeatureReport(UByte *data, UInt32 length)
Ptr< HIDDevice > InternalDevice
HIDDeviceCreateDesc(DeviceFactory *factory, DeviceType type, const HIDDeviceDesc &hidDesc)
uint8_t UByte
Definition: OVR_Types.h:249
virtual bool Initialize(DeviceBase *parent)
HIDDeviceCreateDesc(const HIDDeviceCreateDesc &other)
const DeviceType Type
HIDDeviceCreateDesc * getCreateDesc() const
virtual void Shutdown()
#define OVR_ASSERT(p)
HIDDeviceDesc * getHIDDesc() const
MessageHandlerRef HandlerRef
virtual HIDDeviceManager * GetHIDDeviceManager() const
HIDDeviceManager * GetHIDDeviceManager()
bool GetFeatureReport(UByte *data, UInt32 length)
DeviceManager * GetDeviceManager()
DeviceManagerImpl * GetManagerImpl() const
virtual bool MatchDevice(const String &path)
HIDDevice * GetInternalDevice() const
virtual HIDDevice * Open(const String &path)=0
void Call(const Message &msg)
bool setFeatureReport(UByte *data, UInt32 length)
static int OVR_STDCALL CompareNoCase(const char *a, const char *b)
Definition: OVR_String.cpp:488
bool PushCallAndWaitResult(R(C::*fn)(), R *ret)