Rs2Driver.cpp
Go to the documentation of this file.
1 #include "Rs2Driver.h"
2 
3 #if defined(PROF_ENABLED)
4 #include "Profiler.inl"
5 #endif
6 
7 namespace oni { namespace driver {
8 
9 Rs2Driver::Rs2Driver(OniDriverServices* driverServices)
10 :
11  DriverBase(driverServices),
12  m_context(nullptr)
13 {
14  rsLogDebug("+Rs2Driver");
16 }
17 
19 {
20  rsLogDebug("~Rs2Driver");
21  shutdown();
23 }
24 
26  DeviceConnectedCallback connectedCallback,
27  DeviceDisconnectedCallback disconnectedCallback,
28  DeviceStateChangedCallback deviceStateChangedCallback,
29  void* cookie)
30 {
31  rsTraceFunc("");
32 
33  for (;;)
34  {
36 
37  if (m_context)
38  {
39  rsTraceError("Already initialized");
40  break;
41  }
42 
43  if (DriverBase::initialize(connectedCallback, disconnectedCallback, deviceStateChangedCallback, cookie) != ONI_STATUS_OK)
44  {
45  rsTraceError("DriverBase::initialize failed");
46  break;
47  }
48 
49  Rs2Error e;
51  if (!e.success())
52  {
53  rsTraceError("rs2_create_context failed: %s", e.get_message());
54  break;
55  }
56 
58 
60  if (!e.success())
61  {
62  rsTraceError("rs2_set_devices_changed_callback failed: %s", e.get_message());
63  break;
64  }
65 
66  rsLogDebug("Rs2Driver INITIALIZED");
67  return ONI_STATUS_OK;
68  }
69 
70  shutdown();
71  return ONI_STATUS_ERROR;
72 }
73 
75 {
76  if (m_context) { rsTraceFunc(""); }
77 
79 
80  {
81  Rs2ScopedMutex devLock(m_devicesMx);
82  for (auto iter = m_devices.begin(); iter != m_devices.end(); ++iter) { delete(iter->second); }
83  m_devices.clear();
84  }
85 
86  if (m_context)
87  {
89  m_context = nullptr;
90  }
91 }
92 
94 {
95  rsTraceFunc("");
96 
97  Rs2Error e;
98  rs2_device_list* deviceList = rs2_query_devices(m_context, &e);
99  if (!e.success())
100  {
101  rsTraceError("rs2_query_devices failed: %s", e.get_message());
102  }
103  else
104  {
105  devicesChanged(nullptr, deviceList);
106  rs2_delete_device_list(deviceList);
107  }
108 }
109 
111 {
112  ((Rs2Driver*)param)->devicesChanged(removed, added);
113 }
114 
116 {
117  rsTraceFunc("removed=%p added=%p", removed, added);
118 
120  Rs2Error e;
121 
122  if (removed)
123  {
124  std::list<Rs2Device*> removedList;
125 
126  for (auto iter = m_devices.begin(); iter != m_devices.end(); ++iter)
127  {
128  if (rs2_device_list_contains(removed, iter->second->getRsDevice(), &e))
129  {
130  removedList.push_back(iter->second);
131  }
132  }
133 
134  for (auto iter = removedList.begin(); iter != removedList.end(); ++iter)
135  {
136  rsLogDebug("deviceDisconnected");
137  deviceDisconnected((*iter)->getInfo());
138  }
139  }
140 
141  if (added)
142  {
143  const int count = rs2_get_device_count(added, &e);
144  for (int i = 0; i < count; i++)
145  {
146  rs2_device* device = rs2_create_device(added, i, &e);
147  if (!e.success())
148  {
149  rsTraceError("rs2_create_device failed: %s", e.get_message());
150  }
151  else
152  {
153  rsLogDebug("deviceConnected");
154  OniDeviceInfo info;
155  Rs2Device::queryDeviceInfo(device, &info);
156  deviceConnected(&info);
157  rs2_delete_device(device);
158  }
159  }
160  }
161 }
162 
163 DeviceBase* Rs2Driver::deviceOpen(const char* uri, const char* mode)
164 {
165  rsTraceFunc("uri=%s", uri);
166 
167  rs2_device_list* deviceList = nullptr;
168  rs2_device* device = nullptr;
169  Rs2Device* deviceObj = nullptr;
170 
171  for (;;)
172  {
174 
175  if (m_devices.find(uri) != m_devices.end())
176  {
177  rsTraceError("Already opened");
178  break;
179  }
180 
181  Rs2Error e;
182  deviceList = rs2_query_devices(m_context, &e);
183  if (!e.success())
184  {
185  rsTraceError("rs2_query_devices failed: %s", e.get_message());
186  break;
187  }
188 
189  const int count = rs2_get_device_count(deviceList, &e);
190  for (int i = 0; i < count; i++)
191  {
192  if (device) { rs2_delete_device(device); }
193 
194  device = rs2_create_device(deviceList, i, &e);
195  if (!e.success())
196  {
197  rsTraceError("rs2_create_device failed: %s", e.get_message());
198  break;
199  }
200 
201  const char* serial = rs2_get_device_info(device, RS2_CAMERA_INFO_SERIAL_NUMBER, &e);
202  if (!e.success())
203  {
204  rsTraceError("rs2_get_device_info failed: %s", e.get_message());
205  break;
206  }
207 
208  if (strcmp(uri, serial) == 0)
209  {
210  deviceObj = new Rs2Device(this, device);
211  if (deviceObj->initialize() != ONI_STATUS_OK)
212  {
213  rsTraceError("Rs2Device::initialize failed");
214  delete(deviceObj);
215  deviceObj = nullptr;
216  }
217  else
218  {
219  m_devices[serial] = deviceObj;
220  device = nullptr; // don't release device handle
221  }
222  break;
223  }
224  }
225 
226  break;
227  }
228 
229  if (device) { rs2_delete_device(device); }
230  if (deviceList) { rs2_delete_device_list(deviceList); }
231 
232  return deviceObj;
233 }
234 
235 void Rs2Driver::deviceClose(DeviceBase* deviceBase)
236 {
237  rsTraceFunc("ptr=%p", deviceBase);
238 
239  if (deviceBase)
240  {
242 
243  Rs2Device* deviceObj = (Rs2Device*)deviceBase;
244  m_devices.erase(deviceObj->getInfo()->uri);
245  delete(deviceObj);
246  }
247 }
248 
249 OniStatus Rs2Driver::tryDevice(const char* uri)
250 {
251  rsTraceFunc("uri=%s", uri);
252 
253  return ONI_STATUS_ERROR;
254 }
255 
256 void* Rs2Driver::enableFrameSync(StreamBase** streams, int streamCount)
257 {
258  rsTraceError("FrameSync not supported");
259 
260  return nullptr;
261 }
262 
263 void Rs2Driver::disableFrameSync(void* frameSyncGroup)
264 {
265  rsTraceError("FrameSync not supported");
266 }
267 
268 #if !defined(XN_NEW)
269 #define XN_NEW(type, arg) new type(arg)
270 #endif
271 
272 #if !defined(XN_DELETE)
273 #define XN_DELETE(arg) delete arg
274 #endif
275 
277 
278 }} // namespace
static const textual_icon lock
Definition: model-views.h:218
int rs2_device_list_contains(const rs2_device_list *info_list, const rs2_device *device, rs2_error **error)
Definition: rs.cpp:890
#define RS2_API_VERSION
Definition: rs.h:41
virtual void shutdown()
Definition: Rs2Driver.cpp:74
rs2_device_list * rs2_query_devices(const rs2_context *context, rs2_error **error)
Definition: rs.cpp:208
static OniStatus queryDeviceInfo(rs2_device *device, OniDeviceInfo *deviceInfo)
Definition: Rs2Device.cpp:118
OniStatus initialize()
Definition: Rs2Device.cpp:36
virtual OniStatus initialize(DeviceConnectedCallback connectedCallback, DeviceDisconnectedCallback disconnectedCallback, DeviceStateChangedCallback deviceStateChangedCallback, void *cookie)
Definition: Rs2Driver.cpp:25
void rs2_delete_device(rs2_device *device)
Definition: rs.cpp:301
rs2_context * rs2_create_context(int api_version, rs2_error **error)
Creates RealSense context that is required for the rest of the API.
Definition: rs.cpp:163
void rs2_delete_context(rs2_context *context)
Frees the relevant context object.
Definition: rs.cpp:171
rs2_device * rs2_create_device(const rs2_device_list *info_list, int index, rs2_error **error)
Definition: rs.cpp:289
e
Definition: rmse.py:177
virtual void disableFrameSync(void *frameSyncGroup)
Definition: Rs2Driver.cpp:263
rs2_context * m_context
Definition: Rs2Driver.h:45
#define rsTraceError(format,...)
Definition: Rs2Base.h:41
Definition: Rs2Base.cpp:3
def info(name, value, persistent=False)
Definition: test.py:301
GLenum mode
dictionary streams
Definition: t265_stereo.py:140
virtual DeviceBase * deviceOpen(const char *uri, const char *mode)
Definition: Rs2Driver.cpp:163
#define SHUT_PROFILER
Definition: Rs2Base.h:30
static void devicesChangedCallback(rs2_device_list *removed, rs2_device_list *added, void *param)
Definition: Rs2Driver.cpp:110
virtual void deviceClose(DeviceBase *deviceBase)
Definition: Rs2Driver.cpp:235
Definition: api.h:28
#define INIT_PROFILER
Definition: Rs2Base.h:29
const char * get_message() const
Definition: Rs2Base.h:59
#define rsLogDebug(format,...)
Definition: Rs2Base.h:43
#define rsTraceFunc(format,...)
Definition: Rs2Base.h:42
std::lock_guard< std::mutex > Rs2ScopedMutex
Definition: Rs2Base.h:48
GLenum GLfloat param
virtual OniStatus tryDevice(const char *uri)
Definition: Rs2Driver.cpp:249
GLint GLsizei count
OniDeviceInfo * getInfo()
Definition: Rs2Device.h:32
bool success() const
Definition: Rs2Base.h:58
virtual void * enableFrameSync(StreamBase **streams, int streamCount)
Definition: Rs2Driver.cpp:256
void rs2_set_devices_changed_callback(const rs2_context *context, rs2_devices_changed_callback_ptr callback, void *user, rs2_error **error)
Definition: rs.cpp:787
int i
virtual void devicesChanged(rs2_device_list *removed, rs2_device_list *added)
Definition: Rs2Driver.cpp:115
ONI_EXPORT_DRIVER(Rs2Driver)
void rs2_delete_device_list(rs2_device_list *info_list)
Definition: rs.cpp:275
std::map< std::string, class Rs2Device * > m_devices
Definition: Rs2Driver.h:46
int rs2_get_device_count(const rs2_device_list *info_list, rs2_error **error)
Definition: rs.cpp:259
auto device
Definition: pyrs_net.cpp:17
Rs2Driver(OniDriverServices *driverServices)
Definition: Rs2Driver.cpp:9
const char * rs2_get_device_info(const rs2_device *device, rs2_camera_info info, rs2_error **error)
Definition: rs.cpp:703


librealsense2
Author(s): Sergey Dorodnicov , Doron Hirshberg , Mark Horn , Reagan Lopez , Itay Carpis
autogenerated on Mon May 3 2021 02:47:40