OSVR Framework (Internal Development Docs)  0.6-1962-g59773924
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
NormalizeDeviceDescriptor.cpp
Go to the documentation of this file.
1 
12 // Copyright 2015 Sensics, Inc.
13 //
14 // Licensed under the Apache License, Version 2.0 (the "License");
15 // you may not use this file except in compliance with the License.
16 // You may obtain a copy of the License at
17 //
18 // http://www.apache.org/licenses/LICENSE-2.0
19 //
20 // Unless required by applicable law or agreed to in writing, software
21 // distributed under the License is distributed on an "AS IS" BASIS,
22 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
23 // See the License for the specific language governing permissions and
24 // limitations under the License.
25 
26 // Internal Includes
27 #include <boost/type_traits/remove_cv.hpp>
29 
30 // Library/third-party includes
31 // - none
32 
33 // Standard includes
34 #include <json/value.h>
35 #include <json/reader.h>
36 #include <boost/noncopyable.hpp>
37 #include <boost/variant/get.hpp>
38 #include <boost/algorithm/string/predicate.hpp>
39 #include <boost/algorithm/string/erase.hpp>
40 #include <boost/algorithm/string.hpp>
41 
42 #include <iostream>
43 
44 namespace osvr {
45 namespace common {
46 
47  static const char INTERFACES_KEY[] = "interfaces";
48  static const char EYETRACKER_KEY[] = "eyetracker";
49  static const char LOCATION2D_KEY[] = "location2D";
50  static const char DIRECTION_KEY[] = "direction";
51  static const char TRACKER_KEY[] = "tracker";
52  static const char BUTTON_KEY[] = "button";
53  static const char COUNT_KEY[] = "count";
54  static const char POSITION_KEY[] = "position";
55  static const char ORIENTATION_KEY[] = "orientation";
56  static const char BOUNDED_KEY[] = "bounded";
57 
63  void mergeIdenticalInterfaces(Json::Value &existingIface,
64  Json::Value &newIface,
65  std::string const &detail) {}
66 
68  void appendCurrentIface(Json::Value &augInterface,
69  Json::Value &currInterface) {
70 
71  for (auto &detail : currInterface.getMemberNames()) {
72  Json::Value const &obj = augInterface[detail];
73  if (obj.isObject()) {
76  } else {
77  augInterface[detail] = currInterface[detail];
78  }
79  }
80  }
81 
85  void normalizeForEyeTracker(Json::Value &descriptor,
86  std::string const &ifaceName) {
87 
88  // step into the interfaces object
89  Json::Value const &iface = descriptor[INTERFACES_KEY][ifaceName];
90 
91  // hold the count for eyetracker sensors to initialize
92  int count = descriptor[INTERFACES_KEY][ifaceName][COUNT_KEY].asInt();
93 
94  // if we couldn't find count then return
95  if (!count) {
96  return;
97  }
98 
99  Json::Value augInterfaces(Json::objectValue);
100  // go thru details of interface and
101  for (auto &subIface : iface.getMemberNames()) {
102  // check if subinterface is set to true
103  bool enabled =
104  descriptor[INTERFACES_KEY][ifaceName][subIface].asBool();
105 
106  if (enabled) {
107 
108  if (boost::iequals(subIface, LOCATION2D_KEY)) {
109  augInterfaces[LOCATION2D_KEY][COUNT_KEY] = count;
110  } else if (boost::iequals(subIface, DIRECTION_KEY)) {
111  augInterfaces[DIRECTION_KEY][COUNT_KEY] = count;
112  } else if (boost::iequals(subIface, TRACKER_KEY)) {
113 
114  augInterfaces[TRACKER_KEY][POSITION_KEY] = true;
115  augInterfaces[TRACKER_KEY][ORIENTATION_KEY] = false;
116  augInterfaces[TRACKER_KEY][BOUNDED_KEY] = true;
117  augInterfaces[TRACKER_KEY][COUNT_KEY] = count;
118  } else if (boost::iequals(subIface, BUTTON_KEY)) {
119  augInterfaces[BUTTON_KEY][COUNT_KEY] = count;
120  } else {
121  continue;
122  }
123  }
124  }
125 
126  if (augInterfaces.size() > 0) {
127  Json::Value &currInterfaces = descriptor[INTERFACES_KEY];
128 
129  appendCurrentIface(augInterfaces, currInterfaces);
130  descriptor[INTERFACES_KEY] = augInterfaces;
131  }
132  }
133 
135 
136  std::string normalizeDeviceDescriptor(std::string const &jsonDescriptor) {
137 
138  Json::Value descriptor;
139  {
140  Json::Reader reader;
141  if (!reader.parse(jsonDescriptor, descriptor)) {
144  return jsonDescriptor;
145  }
146  }
148  if (!descriptor.isMember(INTERFACES_KEY)) {
149  return jsonDescriptor;
150  }
151 
152  Json::Value const &ifaceNames = descriptor[INTERFACES_KEY];
153 
154  // interfaces member isn't an object
155  if (!ifaceNames.isObject()) {
156  return jsonDescriptor;
157  }
158 
159  for (auto const &ifaceName : ifaceNames.getMemberNames()) {
160 
161  if (boost::iequals(ifaceName, EYETRACKER_KEY)) {
162  normalizeForEyeTracker(descriptor, ifaceName);
163  } else if (boost::iequals(ifaceName, TRACKER_KEY)) {
165  } else {
167  }
168  }
169 
170  const std::string normalizedDescriptor = descriptor.toStyledString();
171  return normalizedDescriptor;
172  }
173 
174 } // namespace common
175 } // namespace osvr
std::string normalizeDeviceDescriptor(std::string const &jsonDescriptor)
"Normalizes" a device descriptor by parsing it and adding implied interfaces to the existing device d...
void normalizeForEyeTracker(Json::Value &descriptor, std::string const &ifaceName)
For eyetracker, it will add the following interfaces to the descriptor provided that they are set to ...
void mergeIdenticalInterfaces(Json::Value &existingIface, Json::Value &newIface, std::string const &detail)
void appendCurrentIface(Json::Value &augInterface, Json::Value &currInterface)
appends json value for a given string