Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rs usb api and implementations added #3828

Merged
merged 3 commits into from
May 16, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CMake/external_libusb.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ ExternalProject_Add(
GIT_TAG "2a7372db54094a406a755f0b8548b614ba8c78ec" # "v1.0.22" + Mac get_device_list hang fix

UPDATE_COMMAND ${CMAKE_COMMAND} -E copy_if_different
${CMAKE_CURRENT_SOURCE_DIR}/third-party/libusb/CMakeLists.txt
${CMAKE_SOURCE_DIR}/third-party/libusb/CMakeLists.txt
${CMAKE_CURRENT_BINARY_DIR}/third-party/libusb/CMakeLists.txt
PATCH_COMMAND ""

Expand Down
2 changes: 1 addition & 1 deletion CMake/libusb_config.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ if (NOT TARGET usb)
target_include_directories(usb INTERFACE ${LIBUSB_INC})
target_link_libraries(usb INTERFACE ${LIBUSB_LIB})
else()
include(CMake/external_libusb.cmake)
include(${CMAKE_SOURCE_DIR}/CMake/external_libusb.cmake)
endif()
install(TARGETS usb EXPORT realsense2Targets)
endif()
1 change: 1 addition & 0 deletions CMake/lrs_options.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ option(TRACE_API "Log all C API calls" OFF)
option(HWM_OVER_XU "Send HWM commands over UVC XU control" ON)
option(BUILD_SHARED_LIBS "Build shared library" ON)
option(BUILD_UNIT_TESTS "Build realsense unit tests. Note that when enabled, additional tests data set will be downloaded from a web server and stored in a temp directory" OFF)
option(BUILD_INTERNAL_UNIT_TESTS "Test package for components under librealsense namespace, requires BUILD_SHARED_LIBS=OFF and BUILD_UNIT_TESTS=ON" OFF)
option(BUILD_EXAMPLES "Build realsense examples and tools." ON)
option(ENFORCE_METADATA "Require WinSDK with Metadata support during compilation. Windows OS Only" OFF)
option(BUILD_PYTHON_BINDINGS "Build Python bindings" OFF)
Expand Down
7 changes: 6 additions & 1 deletion CMake/unix_config.cmake
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
message(STATUS "Setting Unix configurations")

macro(os_set_flags)
set(BACKEND RS2_USE_V4L2_BACKEND)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fPIC -pedantic -g -D_BSD_SOURCE")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fPIC -pedantic -g -Wno-missing-field-initializers")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-switch -Wno-multichar -Wsequence-point -Wformat-security")
Expand All @@ -28,6 +27,12 @@ macro(os_set_flags)
set(FORCE_LIBUVC ON)
set(BUILD_WITH_TM2 ON)
endif()

if(FORCE_LIBUVC)
set(BACKEND RS2_USE_LIBUVC_BACKEND)
else()
set(BACKEND RS2_USE_V4L2_BACKEND)
endif()
endmacro()

macro(os_target_config)
Expand Down
12 changes: 4 additions & 8 deletions CMake/windows_config.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,14 @@ macro(os_set_flags)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR})

## Check for Windows Version ##
if( (${CMAKE_SYSTEM_VERSION} EQUAL 6.1) OR (FORCE_WINUSB_UVC) ) # Windows 7
message(STATUS "Build for Win7")
set(BUILD_FOR_WIN7 ON)
if(${CMAKE_SYSTEM_VERSION} EQUAL 6.1) # Windows 7
set(FORCE_WINUSB_UVC ON)
else() # Some other windows version
message(STATUS "Build for Windows > Win7")
set(BUILD_FOR_OTHER_WIN ON)
set(BACKEND RS2_USE_WMF_BACKEND)
endif()

if(FORCE_WINUSB_UVC)
set(BACKEND RS2_USE_WINUSB_UVC_BACKEND)
else()
set(BACKEND RS2_USE_WMF_BACKEND)
endif()

if(MSVC)
Expand Down Expand Up @@ -59,7 +55,7 @@ macro(os_target_config)
)
endif()

if(BUILD_FOR_WIN7)
if(FORCE_WINUSB_UVC)
if (NOT CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_CURRENT_BINARY_DIR)
message("Preparing Windows 7 drivers" )
make_directory(${CMAKE_CURRENT_BINARY_DIR}/drivers/)
Expand Down
42 changes: 29 additions & 13 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,31 +10,46 @@ include(${_rel_path}/mock/CMakeLists.txt)
include(${_rel_path}/proc/CMakeLists.txt)
include(${_rel_path}/res/CMakeLists.txt)
include(${_rel_path}/pipeline/CMakeLists.txt)
include(${_rel_path}/usb/CMakeLists.txt)

if(FORCE_LIBUVC)
include(${_rel_path}/libuvc/CMakeLists.txt)
endif()
message(STATUS "using ${BACKEND}")

if (BUILD_WITH_TM2)
include(${_rel_path}/tm2/CMakeLists.txt)
if(UNIX OR FORCE_LIBUVC)
include(${_rel_path}/libusb/CMakeLists.txt)
endif()

if(BUILD_WITH_CUDA)
include(${_rel_path}/cuda/CMakeLists.txt)

if(WIN32)
include(${_rel_path}/win/CMakeLists.txt)
include(${_rel_path}/winusb/CMakeLists.txt)
endif()

if(ANDROID_USB_HOST_UVC)
if(${BACKEND} STREQUAL RS2_USE_ANDROID_BACKEND)
include(${_rel_path}/usbhost/CMakeLists.txt)
include(${_rel_path}/android/CMakeLists.txt)
else()
endif()

if(${BACKEND} STREQUAL RS2_USE_V4L2_BACKEND)
include(${_rel_path}/linux/CMakeLists.txt)
endif()

if(BUILD_FOR_WIN7)
if(${BACKEND} STREQUAL RS2_USE_WMF_BACKEND)
include(${_rel_path}/mf/CMakeLists.txt)
endif()

if(${BACKEND} STREQUAL RS2_USE_WINUSB_UVC_BACKEND)
include(${_rel_path}/win7/CMakeLists.txt)
endif()

if(BUILD_FOR_OTHER_WIN)
include(${_rel_path}/win/CMakeLists.txt)
if(${BACKEND} STREQUAL RS2_USE_LIBUVC_BACKEND)
include(${_rel_path}/libuvc/CMakeLists.txt)
endif()

if (BUILD_WITH_TM2)
include(${_rel_path}/tm2/CMakeLists.txt)
endif()

if(BUILD_WITH_CUDA)
include(${_rel_path}/cuda/CMakeLists.txt)
endif()

if(LRS_TRY_USE_AVX)
Expand Down Expand Up @@ -92,4 +107,5 @@ target_sources(${LRS_TARGET}
"${CMAKE_CURRENT_LIST_DIR}/stream.h"
"${CMAKE_CURRENT_LIST_DIR}/sync.h"
"${CMAKE_CURRENT_LIST_DIR}/types.h"
"${CMAKE_CURRENT_LIST_DIR}/command_transfer.h"
)
17 changes: 2 additions & 15 deletions src/android/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,18 @@

target_sources(${LRS_TARGET}
PRIVATE
"${CMAKE_CURRENT_LIST_DIR}/usb_host/usbhost.c"
"${CMAKE_CURRENT_LIST_DIR}/usb_host/usbhost.h"

"${CMAKE_CURRENT_LIST_DIR}/android-uvc.cpp"
"${CMAKE_CURRENT_LIST_DIR}/android-uvc.h"
"${CMAKE_CURRENT_LIST_DIR}/android-hid.cpp"
"${CMAKE_CURRENT_LIST_DIR}/android-hid.h"
"${CMAKE_CURRENT_LIST_DIR}/android-backend.cpp"
"${CMAKE_CURRENT_LIST_DIR}/android-backend.h"
"${CMAKE_CURRENT_LIST_DIR}/device_watcher.h"
"${CMAKE_CURRENT_LIST_DIR}/device_watcher.cpp"

"${CMAKE_CURRENT_LIST_DIR}/usb_host/android_uvc.cpp"
"${CMAKE_CURRENT_LIST_DIR}/usb_host/android_uvc.h"

"${CMAKE_CURRENT_LIST_DIR}/usb_host/usb_endpoint.h"
"${CMAKE_CURRENT_LIST_DIR}/usb_host/usb_interface.h"
"${CMAKE_CURRENT_LIST_DIR}/usb_host/usb_interface_association.h"
"${CMAKE_CURRENT_LIST_DIR}/usb_host/usb_configuration.h"
"${CMAKE_CURRENT_LIST_DIR}/usb_host/usb_pipe.h"
"${CMAKE_CURRENT_LIST_DIR}/usb_host/usb_pipe.cpp"
"${CMAKE_CURRENT_LIST_DIR}/usb_host/usb_device.h"
"${CMAKE_CURRENT_LIST_DIR}/usb_host/usb_device.cpp"

"${CMAKE_CURRENT_LIST_DIR}/usb_host/device_watcher.h"
"${CMAKE_CURRENT_LIST_DIR}/usb_host/device_watcher.cpp"

"${CMAKE_CURRENT_LIST_DIR}/jni/error.h"
"${CMAKE_CURRENT_LIST_DIR}/jni/error.cpp"
"${CMAKE_CURRENT_LIST_DIR}/jni/context.cpp"
Expand Down
19 changes: 10 additions & 9 deletions src/android/android-backend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
#include "android-uvc.h"
#include "android-hid.h"
#include "../types.h"
#include "usb_host/device_watcher.h"
#include "device_watcher.h"
#include "../usb/usb-enumerator.h"
#include <chrono>
#include <cctype> // std::tolower

Expand Down Expand Up @@ -38,18 +39,18 @@ namespace librealsense {
}

std::vector<uvc_device_info> android_backend::query_uvc_devices() const {
return usb_host::device_watcher::query_uvc_devices();
return device_watcher_usbhost::instance()->query_uvc_devices();
}

std::shared_ptr<usb_device> android_backend::create_usb_device(usb_device_info info) const {
throw std::runtime_error("create_usb_device Not supported");
std::shared_ptr<command_transfer> android_backend::create_usb_device(usb_device_info info) const {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The method create_usb_device returns command_transfer ?
If so - please use "_dev" suffix as command_transfer is too vague
Also is this intended to work with non-Realsense devices, such as platform cam ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the original usb_device was an empty class which inherited command_transfer, since I removed usb_device I needed to replace it with command_transfer but I don't want to make more changes to the backend API in this PR unless it is necessary .

auto dev = usb_enumerator::create_usb_device(info);
if(dev != nullptr)
return std::make_shared<platform::command_transfer_usb>(dev);
return nullptr;
}

std::vector<usb_device_info> android_backend::query_usb_devices() const {

std::vector<usb_device_info> result;
// Not supported
return result;
return usb_enumerator::query_devices_info();
}

std::shared_ptr<hid_device> android_backend::create_hid_device(hid_device_info info) const {
Expand All @@ -68,7 +69,7 @@ namespace librealsense {


std::shared_ptr<device_watcher> android_backend::create_device_watcher() const {
return std::make_shared<usb_host::device_watcher>();
return device_watcher_usbhost::instance();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There should be no need to wrap a singleton with shared_ptr.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I need to implement the backend API:
std::shared_ptr<device_watcher> create_device_watcher() const override;

}
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/android/android-backend.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#pragma once

#include "../backend.h"
#include "../command_transfer.h"

namespace librealsense
{
Expand All @@ -20,7 +21,7 @@ namespace librealsense


// don't change
std::shared_ptr<usb_device> create_usb_device(usb_device_info info) const override;
std::shared_ptr<command_transfer> create_usb_device(usb_device_info info) const override;
std::vector<usb_device_info> query_usb_devices() const override;

// Not supported
Expand Down
74 changes: 36 additions & 38 deletions src/android/android-uvc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#include "android-uvc.h"
#include "../types.h"
#include "libuvc/utlist.h"
#include "usb_host/device_watcher.h"
#include "../usb/usb-enumerator.h"
#include "../libuvc/utlist.h"


Expand All @@ -19,6 +19,7 @@

namespace librealsense {
namespace platform {

bool android_uvc_device::is_connected(const uvc_device_info &info) {
auto result = true;

Expand Down Expand Up @@ -308,53 +309,50 @@ namespace librealsense {
return result;
}

std::shared_ptr<usbhost_uvc_device> android_uvc_device::open_uvc_device()
{
for(auto&& info : usb_enumerator::query_devices_info())
{
if(info.unique_id != _info.unique_id || info.mi != _info.mi)
continue;
auto dev = usb_enumerator::create_usb_device(info);
std::shared_ptr<usbhost_uvc_device> uvc(new usbhost_uvc_device(),
[](usbhost_uvc_device *ptr) {usbhost_close(ptr); });
uvc->device = std::static_pointer_cast<usb_device_usbhost>(dev);
uvc->vid = info.vid;
uvc->pid = info.pid;
usbhost_open(uvc.get(), info.mi);
return uvc;
}
return nullptr;
}

void android_uvc_device::set_power_state(power_state state) {
std::lock_guard<std::mutex> lock(_power_mutex);

if (state == D0 && _power_state == D3) {
uint16_t vid = _info.vid, pid = _info.pid, mi = _info.mi;

std::vector<std::shared_ptr<usbhost_uvc_device>> uvc_devices;
for(auto&& dev : usb_host::device_watcher::get_device_list())
{
std::shared_ptr<usbhost_uvc_device> uvc(new usbhost_uvc_device(),
[](usbhost_uvc_device *ptr) {usbhost_close(ptr); });
uvc->device = dev;
uvc->vid = dev->get_vid();
uvc->pid = dev->get_pid();
uvc_devices.push_back(uvc);
}

for (auto device : uvc_devices) {
_device = open_uvc_device();

usbhost_open(device.get(), mi);
if(!_device)
throw std::runtime_error("Device not found!");

if (device->deviceData.ctrl_if.bInterfaceNumber == mi) {
_device = device;

for (auto ct = _device->deviceData.ctrl_if.input_term_descs;
ct; ct = ct->next) {
_input_terminal = ct->bTerminalID;
}

for (auto pu = _device->deviceData.ctrl_if.processing_unit_descs;
pu; pu = pu->next) {
_processing_unit = pu->bUnitID;
}

for (auto eu = _device->deviceData.ctrl_if.extension_unit_descs;
eu; eu = eu->next) {
_extension_unit = eu->bUnitID;
}
for (auto ct = _device->deviceData.ctrl_if.input_term_descs;
ct; ct = ct->next) {
_input_terminal = ct->bTerminalID;
}

_device->device->claim_interface(mi);
for (auto pu = _device->deviceData.ctrl_if.processing_unit_descs;
pu; pu = pu->next) {
_processing_unit = pu->bUnitID;
}

_power_state = D0;
return;
}
for (auto eu = _device->deviceData.ctrl_if.extension_unit_descs;
eu; eu = eu->next) {
_extension_unit = eu->bUnitID;
}

throw std::runtime_error("Device not found!");
_power_state = D0;

}
if (state == D3 && _power_state == D0) {
_device.reset();
Expand Down
4 changes: 2 additions & 2 deletions src/android/android-uvc.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ namespace librealsense
private:
friend class source_reader_callback;

std::shared_ptr<usbhost_uvc_device> open_uvc_device();
int32_t rs2_value_translate(uvc_req_code action, rs2_option option, int32_t value) const;
void play_profile(stream_profile profile, frame_callback callback);
void stop_stream_cleanup(const stream_profile& profile, std::vector<profile_and_callback>::iterator& elem);
Expand All @@ -87,8 +88,7 @@ namespace librealsense
std::vector<profile_and_callback> _streams;
std::mutex _streams_mutex;

std::shared_ptr<const android_backend> _backend;

std::shared_ptr<const android_backend> _backend;
std::string _location;
std::vector<stream_profile> _profiles;
std::vector<frame_callback> _frame_callbacks;
Expand Down
Loading