context-libusb.cpp
Go to the documentation of this file.
1 // License: Apache 2.0. See LICENSE file in root directory.
2 // Copyright(c) 2015 Intel Corporation. All Rights Reserved.
3 
4 #include "context-libusb.h"
5 #include "../types.h"
6 
7 namespace librealsense
8 {
9  namespace platform
10  {
11  usb_context::usb_context() : _ctx(NULL), _list(NULL), _count(0)
12  {
13  auto sts = libusb_init(&_ctx);
14  if(sts != LIBUSB_SUCCESS)
15  {
16  LOG_ERROR("libusb_init failed");
17  }
18  _count = libusb_get_device_list(_ctx, &_list);
19  }
20 
22  {
23  libusb_free_device_list(_list, true);
24  assert(_handler_requests == 0); // we need the last libusb_close to trigger an event to stop the event thread
25  if (_event_handler.joinable())
26  _event_handler.join();
27  libusb_exit(_ctx);
28  }
29 
30  libusb_context* usb_context::get()
31  {
32  return _ctx;
33  }
34 
36  {
37  std::lock_guard<std::mutex> lk(_mutex);
38  if (!_handler_requests) {
39  // see "Applications which do not use hotplug support" in libusb's io.c
40  if (_event_handler.joinable()) {
41  _event_handler.join();
43  }
44  _event_handler = std::thread([this]() {
45  while (!_kill_handler_thread)
46  libusb_handle_events_completed(_ctx, &_kill_handler_thread);
47  });
48  }
50  }
51 
53  {
54  std::lock_guard<std::mutex> lk(_mutex);
56  if (!_handler_requests)
57  // the last libusb_close will trigger and event and the handler thread will notice this is set
59  }
60 
62  {
63  return index < _count ? _list[index] : NULL;
64  }
65 
67  {
68  return _count;
69  }
70  }
71 }
#define LOG_ERROR(...)
Definition: easyloggingpp.h:58
unsigned char uint8_t
Definition: stdint.h:78
GLuint index
libusb_device * get_device(uint8_t index)
#define assert(condition)
Definition: lz4.c:245
#define NULL
Definition: tinycthread.c:47
struct libusb_context * _ctx


librealsense2
Author(s): LibRealSense ROS Team
autogenerated on Thu Dec 22 2022 03:43:16