src
libusb
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
21
usb_context::~usb_context
()
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
35
void
usb_context::start_event_handler
()
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();
42
_kill_handler_thread
= 0;
43
}
44
_event_handler
= std::thread([
this
]() {
45
while
(!
_kill_handler_thread
)
46
libusb_handle_events_completed(
_ctx
, &
_kill_handler_thread
);
47
});
48
}
49
_handler_requests
++;
50
}
51
52
void
usb_context::stop_event_handler
()
53
{
54
std::lock_guard<std::mutex> lk(
_mutex
);
55
_handler_requests
--;
56
if
(!
_handler_requests
)
57
// the last libusb_close will trigger and event and the handler thread will notice this is set
58
_kill_handler_thread
= 1;
59
}
60
61
libusb_device*
usb_context::get_device
(
uint8_t
index
)
62
{
63
return
index
<
_count
?
_list
[
index
] :
NULL
;
64
}
65
66
size_t
usb_context::device_count
()
67
{
68
return
_count
;
69
}
70
}
71
}
librealsense
Definition:
algo.h:18
uint8_t
unsigned char uint8_t
Definition:
stdint.h:78
librealsense::platform::usb_context::_mutex
std::mutex _mutex
Definition:
context-libusb.h:32
context-libusb.h
librealsense::platform::usb_context::start_event_handler
void start_event_handler()
Definition:
context-libusb.cpp:35
librealsense::platform::usb_context::_ctx
struct libusb_context * _ctx
Definition:
context-libusb.h:36
index
GLuint index
Definition:
glad/glad/glad.h:2777
librealsense::platform::usb_context::get_device
libusb_device * get_device(uint8_t index)
Definition:
context-libusb.cpp:61
librealsense::platform::usb_context::device_count
size_t device_count()
Definition:
context-libusb.cpp:66
librealsense::platform::usb_context::usb_context
usb_context()
Definition:
context-libusb.cpp:11
librealsense::platform::usb_context::~usb_context
~usb_context()
Definition:
context-libusb.cpp:21
NULL
#define NULL
Definition:
tinycthread.c:47
librealsense::platform::usb_context::get
libusb_context * get()
Definition:
context-libusb.cpp:30
librealsense::platform::usb_context::_count
size_t _count
Definition:
context-libusb.h:34
librealsense::platform::usb_context::stop_event_handler
void stop_event_handler()
Definition:
context-libusb.cpp:52
librealsense::platform::usb_context::_event_handler
std::thread _event_handler
Definition:
context-libusb.h:38
assert
#define assert(condition)
Definition:
lz4.c:245
librealsense::platform::usb_context::_kill_handler_thread
int _kill_handler_thread
Definition:
context-libusb.h:37
librealsense::platform::usb_context::_list
libusb_device ** _list
Definition:
context-libusb.h:33
LOG_ERROR
#define LOG_ERROR(...)
Definition:
easyloggingpp.h:73
librealsense::platform::usb_context::_handler_requests
int _handler_requests
Definition:
context-libusb.h:35
librealsense2
Author(s): LibRealSense ROS Team
autogenerated on Fri Aug 2 2024 08:30:01