OSVR Framework (Internal Development Docs)  0.6-1962-g59773924
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
DeviceInterfaceC.cpp
Go to the documentation of this file.
1 
11 // Copyright 2014 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
35 #include <osvr/Util/Verbosity.h>
36 #include "HandleNullContext.h"
37 
38 // Library/third-party includes
39 #include <boost/thread.hpp>
40 
41 // Standard includes
42 #include <string>
43 
46  OSVR_PLUGIN_HANDLE_NULL_CONTEXT_CONSTRUCTOR("osvrDeviceCreateInitOptions",
47  ctx);
48  return new OSVR_DeviceInitObject(ctx);
49 }
50 
51 OSVR_ReturnCode osvrDeviceSendData(OSVR_IN_PTR OSVR_DeviceToken dev,
52  OSVR_IN_PTR OSVR_MessageType msg,
53  OSVR_IN_READS(len) const char *bytestream,
54  OSVR_IN size_t len) {
55  OSVR_DEV_VERBOSE(
56  "In osvrDeviceSendData, trying to send a message of length " << len);
57  OSVR_PLUGIN_HANDLE_NULL_CONTEXT("osvrDeviceSendData device token", dev);
58  OSVR_PLUGIN_HANDLE_NULL_CONTEXT("osvrDeviceSendData message type", msg);
59  dev->sendData(msg, bytestream, len);
60  return OSVR_RETURN_SUCCESS;
61 }
62 
63 OSVR_ReturnCode osvrDeviceSendTimestampedData(
64  OSVR_IN_PTR OSVR_DeviceToken dev, OSVR_IN_PTR OSVR_TimeValue *timestamp,
65  OSVR_IN_PTR OSVR_MessageType msg, OSVR_IN_READS(len) const char *bytestream,
66  OSVR_IN size_t len) {
67  OSVR_DEV_VERBOSE(
68  "In osvrDeviceSendData, trying to send a timestamped message of length "
69  << len);
70  OSVR_PLUGIN_HANDLE_NULL_CONTEXT("osvrDeviceSendData device token", dev);
71  OSVR_PLUGIN_HANDLE_NULL_CONTEXT("osvrDeviceSendData message type", msg);
72  dev->sendData(*timestamp, msg, bytestream, len);
73  return OSVR_RETURN_SUCCESS;
74 }
75 
76 OSVR_ReturnCode osvrDeviceSendJsonDescriptor(OSVR_IN_PTR OSVR_DeviceToken dev,
77  OSVR_IN_READS(len)
78  const char *json,
79  OSVR_IN size_t len) {
80  OSVR_PLUGIN_HANDLE_NULL_CONTEXT("osvrDeviceSendJsonDescriptor", dev);
81  OSVR_PLUGIN_HANDLE_NULL_CONTEXT("osvrDeviceSendJsonDescriptor descriptor",
82  json);
83 
84  dev->setDeviceDescriptor(std::string(json, len));
85  return OSVR_RETURN_SUCCESS;
86 }
87 
88 OSVR_ReturnCode
90  OSVR_IN_STRZ const char *name,
91  OSVR_OUT_PTR OSVR_MessageType *msgtype) {
92  OSVR_PLUGIN_HANDLE_NULL_CONTEXT("osvrDeviceRegisterMessageType", ctx);
93  OSVR_DEV_VERBOSE("In osvrDeviceRegisterMessageType for a message named "
94  << name);
95 
96  // Extract the connection from the overall context
98  osvr::connection::Connection::retrieveConnection(
100  .getParent());
101  osvr::connection::MessageTypePtr ret = conn->registerMessageType(name);
102 
103  // Transfer ownership of the message type object to the plugin context.
104  try {
105  *msgtype =
106  osvr::pluginkit::registerObjectForDeletion(ctx, ret.release());
107  } catch (std::exception &e) {
108  std::cerr << "Error in osvrDeviceRegisterMessageType: " << e.what()
109  << std::endl;
110  return OSVR_RETURN_FAILURE;
111  } catch (...) {
112  return OSVR_RETURN_FAILURE;
113  }
114  return OSVR_RETURN_SUCCESS;
115 }
116 
117 template <typename FactoryFunction>
118 inline static OSVR_ReturnCode
119 osvrDeviceGenericInit(OSVR_DeviceInitOptions options, OSVR_DeviceToken *device,
120  FactoryFunction f) {
121 
122  osvr::connection::DeviceTokenPtr dev = f(*options);
123  if (!dev) {
124  OSVR_DEV_VERBOSE("Device token factory returned a null "
125  "pointer - this shouldn't happen!");
126  return OSVR_RETURN_FAILURE;
127  }
128  // Transfer ownership of the device token object to the plugin context.
129  try {
130  *device =
131  options->getContext()->registerDataWithGenericDelete(dev.release());
133  options->getContext()->registerDataWithGenericDelete(options);
134  options->notifyToken(*device);
135  } catch (std::exception &e) {
136  std::cerr << "Error in osvrDeviceGenericInit: " << e.what()
137  << std::endl;
138  return OSVR_RETURN_FAILURE;
139  } catch (...) {
140  return OSVR_RETURN_FAILURE;
141  }
142  return OSVR_RETURN_SUCCESS;
143 }
144 
145 template <typename FactoryFunction>
146 inline static OSVR_ReturnCode
147 osvrDeviceGenericInit(OSVR_DeviceInitOptions options, const char *name,
148  OSVR_DeviceToken *device, FactoryFunction f) {
149  options->setName(name);
150 
151  return osvrDeviceGenericInit(options, device, f);
152 }
153 
154 template <typename FactoryFunction>
155 inline static OSVR_ReturnCode
156 osvrDeviceGenericInit(OSVR_PluginRegContext ctx, const char *name,
157  OSVR_DeviceToken *device, FactoryFunction f) {
159 
160  return osvrDeviceGenericInit(options, name, device, f);
161 }
162 
163 OSVR_ReturnCode osvrDeviceSyncInit(OSVR_IN_PTR OSVR_PluginRegContext ctx,
164  OSVR_IN_STRZ const char *name,
165  OSVR_OUT_PTR OSVR_DeviceToken *device) {
166  OSVR_PLUGIN_HANDLE_NULL_CONTEXT("osvrDeviceSyncInit", ctx);
167  OSVR_DEV_VERBOSE("In osvrDeviceSyncInit for a device named " << name);
168  return osvrDeviceGenericInit(ctx, name, device,
170 }
171 OSVR_ReturnCode
173  OSVR_IN_STRZ const char *name,
174  OSVR_IN_PTR OSVR_DeviceInitOptions options,
175  OSVR_OUT_PTR OSVR_DeviceToken *device) {
176  OSVR_PLUGIN_HANDLE_NULL_CONTEXT("osvrDeviceSyncInitWithOptions", options);
177  return osvrDeviceGenericInit(options, name, device,
179 }
180 
181 OSVR_ReturnCode
184  updateCallback,
185  OSVR_IN_OPT void *userData) {
186  OSVR_DEV_VERBOSE("In osvrDeviceRegisterUpdateCallback");
188  "osvrDeviceRegisterUpdateCallback device token", dev);
189  dev->setUpdateCallback(
190  [updateCallback, userData] { return updateCallback(userData); });
191  return OSVR_RETURN_SUCCESS;
192 }
193 
194 OSVR_ReturnCode osvrDeviceAsyncInit(OSVR_IN_PTR OSVR_PluginRegContext ctx,
195  OSVR_IN_STRZ const char *name,
196  OSVR_OUT_PTR OSVR_DeviceToken *device) {
197  OSVR_PLUGIN_HANDLE_NULL_CONTEXT("osvrDeviceAsyncInit", ctx);
198  OSVR_DEV_VERBOSE("In osvrDeviceAsyncInit for a device named " << name);
199  return osvrDeviceGenericInit(ctx, name, device,
200  OSVR_DeviceTokenObject::createAsyncDevice);
201 }
202 
203 OSVR_ReturnCode
205  OSVR_IN_STRZ const char *name,
206  OSVR_IN_PTR OSVR_DeviceInitOptions options,
207  OSVR_OUT_PTR OSVR_DeviceToken *device) {
208  OSVR_PLUGIN_HANDLE_NULL_CONTEXT("osvrDeviceAsyncInitWithOptions", options);
209  return osvrDeviceGenericInit(options, name, device,
210  OSVR_DeviceTokenObject::createAsyncDevice);
211 }
212 
213 OSVR_ReturnCode osvrDeviceMicrosleep(OSVR_IN uint64_t microseconds) {
214  boost::this_thread::sleep(boost::posix_time::microseconds(microseconds));
215  return OSVR_RETURN_SUCCESS;
216 }
OSVR_ReturnCode osvrDeviceAsyncInit(OSVR_PluginRegContext ctx, const char *name, OSVR_DeviceToken *device)
Initialize an asynchronous device token.
Header.
Header providing C++ interface wrappers around functionality in PluginRegistrationC.h.
OSVR_ReturnCode osvrDeviceSendTimestampedData(OSVR_DeviceToken dev, const OSVR_TimeValue *timestamp, OSVR_MessageType msg, const char *bytestream, size_t len)
Send a raw bytestream from your device, with a known timestamp.
A DeviceToken connects the generic device interaction code in PluginKit's C API with the workings of ...
Definition: DeviceToken.h:56
#define OSVR_PLUGIN_HANDLE_NULL_CONTEXT_CONSTRUCTOR(FUNC, CONTEXT_NAME)
Internal macro for use in C API function implementations to check the validity of a context parameter...
shared_ptr< Connection > ConnectionPtr
How one must hold a Connection.
Definition: ConnectionPtr.h:40
void setName(std::string const &n)
Set the (unqualified) name of the device to create.
Structure used internally to construct the desired type of device.
#define OSVR_PLUGIN_HANDLE_NULL_CONTEXT(FUNC, CONTEXT_NAME)
Internal macro for use in C API function implementations to check the validity of a context parameter...
Base class for connection-specific message type registration.
Definition: MessageType.h:38
OSVR_ReturnCode osvrDeviceAsyncInitWithOptions(OSVR_PluginRegContext, const char *name, OSVR_DeviceInitOptions options, OSVR_DeviceToken *device)
Initialize an asynchronous device token.
OSVR_ReturnCode osvrDeviceRegisterUpdateCallback(OSVR_DeviceToken dev, OSVR_DeviceUpdateCallback updateCallback, void *userData)
Register the update callback of a device.
#define OSVR_RETURN_FAILURE
The "failure" value for an OSVR_ReturnCode.
Definition: ReturnCodesC.h:47
OSVR_ReturnCode osvrDeviceMicrosleep(uint64_t microseconds)
Request a thread sleep for at least the given number of microseconds. DO NOT use within a Sync plugin...
T * registerObjectForDeletion(OSVR_PluginRegContext ctx, T *obj)
Registers an object to be destroyed with delete when the plugin is unloaded.
OSVR_ReturnCode osvrDeviceSendData(OSVR_DeviceToken dev, OSVR_MessageType msg, const char *bytestream, size_t len)
Send a raw bytestream from your device.
#define OSVR_RETURN_SUCCESS
The "success" value for an OSVR_ReturnCode.
Definition: ReturnCodesC.h:45
OSVR_DeviceInitOptions osvrDeviceCreateInitOptions(OSVR_PluginRegContext ctx)
Create a OSVR_DeviceInitOptions object.
OSVR_ReturnCode osvrDeviceSendJsonDescriptor(OSVR_DeviceToken dev, const char *json, size_t len)
Submit a JSON self-descriptor string for the device.
OSVR_ReturnCode osvrDeviceSyncInitWithOptions(OSVR_PluginRegContext, const char *name, OSVR_DeviceInitOptions options, OSVR_DeviceToken *device)
Initialize a synchronous device token.
void notifyToken(OSVR_DeviceTokenObject *dev)
Notify all those interested what the device token is.
unique_ptr< MessageType > MessageTypePtr
a uniquely-owned handle for holding a message type registration.
Internal, configured header file for verbosity macros.
T * registerDataWithGenericDelete(T *data)
Register data allocated with new to be deleted on plugin unload.
OSVR_EXTERN_C_BEGIN typedef void * OSVR_PluginRegContext
A context pointer passed in to your plugin's entry point and other locations of control flow transfer...
Standardized, portable parallel to struct timeval for representing both absolute times and time inter...
Definition: TimeValueC.h:81
OSVR_ReturnCode(* OSVR_DeviceUpdateCallback)(void *userData)
Function type of a Device Update callback.
static osvr::connection::DeviceTokenPtr createSyncDevice(osvr::connection::DeviceInitObject &init)
Creates a device token (and underlying ConnectionDevice) that has an update method that runs in the s...
Definition: DeviceToken.cpp:58
OSVR_ReturnCode osvrDeviceSyncInit(OSVR_PluginRegContext ctx, const char *name, OSVR_DeviceToken *device)
Initialize a synchronous device token.
OSVR_ReturnCode osvrDeviceRegisterMessageType(OSVR_PluginRegContext ctx, const char *name, OSVR_MessageType *msgtype)
Register (or recall) a message type by name.
osvr::pluginhost::PluginSpecificRegistrationContext * getContext()
Retrieves the plugin context.
static PluginSpecificRegistrationContext & get(OSVR_PluginRegContext ctx)
Retrieve this interface from an OSVR_PluginRegContext opaque pointer.