-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathProperties.cpp
317 lines (288 loc) · 11.1 KB
/
Properties.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
/** @file
@brief Implementation
@date 2017
@author
Sensics, Inc.
<http://sensics.com/osvr>
*/
// Copyright 2017 Sensics, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Internal Includes
#include "Properties.h"
#include "FindDriver.h"
#include "ValveStrCpy.h"
// Library/third-party includes
// - none
// Standard includes
#include <iostream>
#include <string>
#include <type_traits>
using namespace vr;
namespace {
template <typename T>
struct PropertyTypeTagTrait
: std::integral_constant<PropertyTypeTag_t, k_unInvalidPropertyTag> {};
/// Semicolon intentionally omitted at the end of this macro definition
#define OSVRVIVE_DEFINE_PROPERTY_TYPE_TAG(TYPE, TAG) \
template <> \
struct PropertyTypeTagTrait<TYPE> \
: std::integral_constant<PropertyTypeTag_t, TAG> {}
OSVRVIVE_DEFINE_PROPERTY_TYPE_TAG(float, k_unFloatPropertyTag);
OSVRVIVE_DEFINE_PROPERTY_TYPE_TAG(std::int32_t, k_unInt32PropertyTag);
OSVRVIVE_DEFINE_PROPERTY_TYPE_TAG(std::uint64_t, k_unUint64PropertyTag);
OSVRVIVE_DEFINE_PROPERTY_TYPE_TAG(bool, k_unBoolPropertyTag);
OSVRVIVE_DEFINE_PROPERTY_TYPE_TAG(std::string, k_unStringPropertyTag);
OSVRVIVE_DEFINE_PROPERTY_TYPE_TAG(vr::HmdMatrix34_t,
k_unHmdMatrix34PropertyTag);
OSVRVIVE_DEFINE_PROPERTY_TYPE_TAG(vr::HmdVector2_t, k_unHiddenAreaPropertyTag);
/// Note: if you add a type here, you must also make sure it's added to the
/// PropertyStoreVariant, as well as to Properties::WritePropertyBatch
#undef OSVRVIVE_DEFINE_PROPERTY_TYPE_TAG
struct GetContainedTypeTag : boost::static_visitor<PropertyTypeTag_t> {
template <typename T> PropertyTypeTag_t operator()(T const &val) {
return PropertyTypeTagTrait<T>::value;
}
};
struct ValueSizeGetter : boost::static_visitor<std::size_t> {
template <typename T> std::size_t operator()(T const &) const {
return sizeof(T);
}
std::size_t operator()(std::string const &s) const {
// add 1 for null terminator
return s.size() + 1;
}
};
template <typename T> inline std::size_t getValueSize(T const &val) {
return ValueSizeGetter()(val);
}
struct ValueGetter : boost::static_visitor<> {
ValueGetter(PropertyRead_t &batchEntry) : batchEntry_(&batchEntry) {}
template <typename T> void operator()(T const &val) const {
*reinterpret_cast<T *>(batchEntry_->pvBuffer) = val;
}
void operator()(std::string const &s) const {
char *strBuf = reinterpret_cast<char *>(batchEntry_->pvBuffer);
valveStrCpy(s, strBuf, batchEntry_->unBufferSize);
}
private:
PropertyRead_t *batchEntry_;
};
} // namespace
inline void readProperty(PropertiesStore const &store,
PropertyRead_t &batchEntry) {
if (!store.containsAnyType(batchEntry.prop)) {
batchEntry.eError = TrackedProp_UnknownProperty;
return;
}
auto &val = store.getVariant(batchEntry.prop);
auto containedTypeTag = GetContainedTypeTag();
auto containedTag = val.apply_visitor(containedTypeTag);
bool actuallyGet = true;
if (containedTag != batchEntry.unTag) {
actuallyGet = false;
batchEntry.unTag = containedTag;
}
auto valueSizeGetter = ValueSizeGetter();
batchEntry.unRequiredBufferSize =
static_cast<std::uint32_t>(val.apply_visitor(valueSizeGetter));
if (batchEntry.unRequiredBufferSize > batchEntry.unBufferSize) {
return;
}
if (!actuallyGet) {
return;
}
auto be = ValueGetter(batchEntry);
val.apply_visitor(be);
}
Properties::Properties()
: m_logger(osvr::util::log::make_logger("Properties")) {
osvr::vive::LocationInfo locations;
locations = osvr::vive::findLocationInfoForDriver();
if (locations.driverFound) {
addDeviceAt(0);
vr::PropertyWrite_t writeBatch;
writeBatch.prop = Prop_UserConfigPath_String;
writeBatch.writeType = PropertyWrite_Set;
std::string userConfigPath = locations.driverConfigDir;
writeBatch.unBufferSize = userConfigPath.length();
writeBatch.pvBuffer = (char *)userConfigPath.c_str();
writeBatch.unTag = k_unStringPropertyTag;
WritePropertyBatch(1, &writeBatch, 1);
writeBatch.prop = Prop_InstallPath_String;
writeBatch.writeType = PropertyWrite_Set;
std::string installPath = locations.driverRoot;
writeBatch.unBufferSize = installPath.length();
writeBatch.pvBuffer = (char *)userConfigPath.c_str();
writeBatch.unTag = k_unStringPropertyTag;
WritePropertyBatch(1, &writeBatch, 1);
}
}
ETrackedPropertyError
Properties::ReadPropertyBatch(PropertyContainerHandle_t ulContainerHandle,
PropertyRead_t *pBatch,
uint32_t unBatchEntryCount) {
uint64_t deviceId = ulContainerHandle - 1;
if (!hasDeviceAt(deviceId)) {
m_logger->error("doesn't have the property container with id: ")
<< ulContainerHandle;
return TrackedProp_InvalidDevice;
}
auto &store = m_propertyStores.at(deviceId);
ETrackedPropertyError ret = TrackedProp_Success;
for (std::uint32_t i = 0; i < unBatchEntryCount; ++i) {
auto &entry = pBatch[i];
m_logger->debug() << "ReadPropertyBatch: Entry " << i
<< ", Property ID " << entry.prop
<< ", Supplied tag type " << entry.unTag;
readProperty(store, pBatch[i]);
if (pBatch[i].eError != TrackedProp_Success) {
ret = pBatch[i].eError;
/// @todo OK to early out on first error?
return ret;
}
}
return ret;
}
ETrackedPropertyError
Properties::WritePropertyBatch(PropertyContainerHandle_t ulContainerHandle,
PropertyWrite_t *pBatch,
uint32_t unBatchEntryCount) {
if (pBatch == nullptr) {
m_logger->error("pBatch is null");
return TrackedProp_InvalidOperation;
}
uint64_t deviceId = ulContainerHandle - 1;
if (!hasDeviceAt(deviceId)) {
addDeviceAt(deviceId);
}
PropertiesStore &pStore = m_propertyStores[deviceId];
for (std::uint32_t i = 0; i < unBatchEntryCount; ++i) {
auto &entry = pBatch[i];
if (entry.writeType == PropertyWrite_Set) {
entry.eError = vr::TrackedProp_Success;
switch (entry.unTag) {
case k_unFloatPropertyTag: {
auto val = *(reinterpret_cast<float *>(entry.pvBuffer));
pStore.set(pBatch->prop, val);
break;
}
case k_unInt32PropertyTag: {
auto val = *(reinterpret_cast<int32_t *>(entry.pvBuffer));
pStore.set(entry.prop, val);
break;
}
case k_unUint64PropertyTag: {
auto val = *(reinterpret_cast<uint64_t *>(entry.pvBuffer));
pStore.set(entry.prop, val);
break;
}
case k_unBoolPropertyTag: {
auto val = *(reinterpret_cast<bool *>(entry.pvBuffer));
pStore.set(entry.prop, val);
break;
}
case k_unStringPropertyTag: {
pStore.set(entry.prop, std::string(reinterpret_cast<char *>(
entry.pvBuffer)));
break;
}
case k_unHmdMatrix34PropertyTag: {
auto val =
*(reinterpret_cast<vr::HmdMatrix34_t *>(entry.pvBuffer));
pStore.set(entry.prop, val);
break;
}
case k_unHiddenAreaPropertyTag: {
auto val =
*(reinterpret_cast<vr::HmdVector2_t *>(entry.pvBuffer));
pStore.set(entry.prop, val);
break;
}
default: {
m_logger->error()
<< "In property setting: Unhandled property tag type: "
<< entry.unTag << " for property ID" << entry.prop;
entry.eError = vr::TrackedProp_InvalidOperation;
break;
}
}
} else if (entry.writeType == PropertyWrite_Erase) {
pStore.erase(entry.prop);
} else if (entry.writeType == PropertyWrite_SetError) {
m_logger->debug("set error");
} else {
m_logger->error("unknown writeType: ") << entry.writeType;
return vr::TrackedProp_InvalidOperation;
}
}
return vr::TrackedProp_Success;
}
const char *Properties::GetPropErrorNameFromEnum(ETrackedPropertyError error) {
switch (error) {
case vr::TrackedProp_Success:
return "Success";
case vr::TrackedProp_WrongDataType:
return "Wrong Data Type";
case vr::TrackedProp_WrongDeviceClass:
return "Wrong Device Class";
case vr::TrackedProp_BufferTooSmall:
return "Buffer too small";
case vr::TrackedProp_UnknownProperty:
return "Unknown property";
case vr::TrackedProp_InvalidDevice:
return "Invalid device";
case vr::TrackedProp_CouldNotContactServer:
return "Could not contact server";
case vr::TrackedProp_ValueNotProvidedByDevice:
return "Value not provided by device";
case vr::TrackedProp_StringExceedsMaximumLength:
return "String exceeds maximum length";
case vr::TrackedProp_NotYetAvailable:
return "Not yet available";
case vr::TrackedProp_PermissionDenied:
return "Permission denied";
case vr::TrackedProp_InvalidOperation:
return "Invalid operation";
default:
return "Other undocumented error";
}
}
PropertyContainerHandle_t
Properties::TrackedDeviceToPropertyContainer(TrackedDeviceIndex_t nDevice) {
m_logger->debug("TrackedDeviceToPropertyContainer")
<< " at index " << nDevice;
if (!hasDeviceAt(nDevice)) {
addDeviceAt(nDevice);
}
PropertyContainerHandle_t containerId = nDevice + 1;
return containerId;
}
bool Properties::hasDeviceAt(const std::uint64_t idx) const {
return (idx < m_propertyStores.size());
}
void Properties::addDeviceAt(const std::uint64_t idx) {
/// @todo this is kind of redundant with reserveIds
m_logger->debug("addDeviceAt ") << idx;
if (!(idx < m_propertyStores.size())) {
/// OK, we need to reserve more room
reserveIds(idx + 1);
}
}
bool Properties::reserveIds(std::uint64_t n) {
if (m_propertyStores.size() < n) {
m_propertyStores.resize(n);
return true;
}
return false;
}