OSVR Framework (Internal Development Docs)  0.6-1962-g59773924
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
BaseDevice.cpp
Go to the documentation of this file.
1 
11 // Copyright 2015 Sensics, Inc.
12 //
13 // Licensed under the Apache License, Version 2.0 (the "License");
14 // you may not use this file except in compliance with the License.
15 // You may obtain a copy of the License at
16 //
17 // http://www.apache.org/licenses/LICENSE-2.0
18 //
19 // Unless required by applicable law or agreed to in writing, software
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 #define OSVR_DEV_VERBOSE_DISABLE
26 
27 // Internal Includes
28 #include <osvr/Common/BaseDevice.h>
30 #include <osvr/Util/Verbosity.h>
31 
32 // Library/third-party includes
33 #include <boost/assert.hpp>
34 
35 // Standard includes
36 #include <stdexcept>
37 
38 namespace osvr {
39 namespace common {
40 
45  m_components.clear();
46  }
47 
48  void BaseDevice::m_addComponent(DeviceComponentPtr component) {
49  if (!component) {
50  throw std::logic_error(
51  "Tried to add a null component pointer to a base device!");
52  }
53  m_components.push_back(component);
54  component->recordParent(*this);
55  }
56 
57  void BaseDevice::registerHandler(vrpn_MESSAGEHANDLER handler,
58  void *userdata,
59  RawMessageType const &msgType) {
60  m_getConnection()->register_handler(msgType.get(), handler, userdata,
61  getSender().get());
62  }
63 
64  void BaseDevice::unregisterHandler(vrpn_MESSAGEHANDLER handler,
65  void *userdata,
66  RawMessageType const &msgType) {
67  m_getConnection()->unregister_handler(msgType.get(), handler, userdata,
68  getSender().get());
69  }
70 
71  RawMessageType BaseDevice::m_registerMessageType(const char *msgString) {
72  OSVR_DEV_VERBOSE("BaseDevice registering message type " << msgString);
73  return RawMessageType(
74  m_getConnection()->register_message_type(msgString));
75  }
76 
77  RawSenderType BaseDevice::getSender() { return m_sender; }
78 
80  for (auto const &component : m_components) {
81  component->update();
82  }
83  m_update();
84  }
85 
87  m_getConnection()->send_pending_reports();
88  }
89 
90  std::string const &BaseDevice::getDeviceName() const { return m_name; }
91 
92  void BaseDevice::m_packMessage(size_t len, const char *buf,
93  RawMessageType const &msgType,
94  util::time::TimeValue const &timestamp,
95  uint32_t classOfService) {
96  struct timeval t;
97  util::time::toStructTimeval(t, timestamp);
98  auto ret = m_getConnection()->pack_message(
99  static_cast<uint32_t>(len), t, msgType.get(), getSender().get(),
100  buf, classOfService);
101  if (ret != 0) {
102  throw std::runtime_error("Could not pack message!");
103  }
104  }
105 
106  void BaseDevice::m_setup(vrpn_ConnectionPtr conn, RawSenderType sender,
107  std::string const &name) {
108  m_conn = conn;
109  m_sender = sender;
110  m_name = name;
111  }
112 
113  vrpn_ConnectionPtr BaseDevice::m_getConnection() const { return m_conn; }
114 } // namespace common
115 } // namespace osvr
void toStructTimeval(struct timeval &dest, TimeValue const &src)
Convert a TimeValue to a struct timeval.
Definition: TimeValue.h:98
vrpn_ConnectionPtr m_getConnection() const
Accessor for underlying connection.
Definition: BaseDevice.cpp:113
virtual void m_update()=0
Implementation-specific update (call client_mainloop() or server_mainloop() in it!) ...
BaseDevice()
Constructor.
Definition: BaseDevice.cpp:41
void sendPending()
Called from a component to send pending messages instead of waiting for next time.
Definition: BaseDevice.cpp:86
void m_setup(vrpn_ConnectionPtr conn, RawSenderType sender, std::string const &name)
Should be called by derived class to set the connection, etc.
Definition: BaseDevice.cpp:106
void update()
Called from the outside to run the mainloop on the device and its components.
Definition: BaseDevice.cpp:79
UnderlyingSenderType get() const
Gets the registered sender value or default.
Internal, configured header file for verbosity macros.
Header.
virtual ~BaseDevice()
Virtual destructor.
Definition: BaseDevice.cpp:42
Standardized, portable parallel to struct timeval for representing both absolute times and time inter...
Definition: TimeValueC.h:81
Type-safe wrapper with built-in default for a VRPN "sender type" integer.
Definition: RawSenderType.h:45