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

replace Linux polling_device_watcher with udev_device_watcher #9831

Merged
merged 5 commits into from
Oct 11, 2021
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
6 changes: 6 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -143,4 +143,10 @@ target_sources(${LRS_TARGET}
"${CMAKE_CURRENT_LIST_DIR}/points.h"
"${CMAKE_CURRENT_LIST_DIR}/depth-sensor.h"
"${CMAKE_CURRENT_LIST_DIR}/color-sensor.h"
"${CMAKE_CURRENT_LIST_DIR}/callback-invocation.h"
"${CMAKE_CURRENT_LIST_DIR}/easyloggingpp.h"
"${CMAKE_CURRENT_LIST_DIR}/librealsense-exception.h"
"${CMAKE_CURRENT_LIST_DIR}/polling-device-watcher.h"
"${CMAKE_CURRENT_LIST_DIR}/small-heap.h"
"${CMAKE_CURRENT_LIST_DIR}/basics.h"
)
2 changes: 2 additions & 0 deletions src/archive.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

#include "types.h"
#include "core/streaming.h"
#include "callback-invocation.h"


namespace librealsense
{
Expand Down
9 changes: 3 additions & 6 deletions src/backend.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@
// Copyright(c) 2015 Intel Corporation. All Rights Reserved.

#pragma once
#ifndef LIBREALSENSE_BACKEND_H
#define LIBREALSENSE_BACKEND_H

#include "../include/librealsense2/h/rs_types.h" // Inherit all type definitions in the public API
#include "../include/librealsense2/h/rs_option.h"
#include <librealsense2/h/rs_types.h> // Inherit all type definitions in the public API
#include <librealsense2/h/rs_option.h>
#include "usb/usb-types.h"
#include "usb/usb-device.h"
#include "hid/hid-types.h"
Expand Down Expand Up @@ -825,6 +823,5 @@ namespace librealsense
}

double monotonic_to_realtime(double monotonic);
}

#endif
} // namespace librealsense
16 changes: 16 additions & 0 deletions src/basics.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// License: Apache 2.0. See LICENSE file in root directory.
// Copyright(c) 2021 Intel Corporation. All Rights Reserved.

#pragma once


// Disable declspec(dllexport) warnings:
// Classes exported via LRS_EXTENSION_API are **not** part of official librealsense API (at least for now)
// Any extension relying on these APIs must be compiled and distributed together with realsense2.dll
#pragma warning(disable : 4275) /* disable: C4275: non dll-interface class used as base for dll-interface class */
#pragma warning(disable : 4251) /* disable: C4251: class needs to have dll-interface to be used by clients of class */
#ifdef WIN32
#define LRS_EXTENSION_API __declspec(dllexport)
#else
#define LRS_EXTENSION_API
#endif
67 changes: 67 additions & 0 deletions src/callback-invocation.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// License: Apache 2.0. See LICENSE file in root directory.
// Copyright(c) 2021 Intel Corporation. All Rights Reserved.

#pragma once

#include "small-heap.h"

#include <chrono>


namespace librealsense {


struct callback_invocation
{
std::chrono::high_resolution_clock::time_point started;
std::chrono::high_resolution_clock::time_point ended;
};

typedef small_heap< callback_invocation, 1 > callbacks_heap;

struct callback_invocation_holder
{
callback_invocation_holder()
: invocation( nullptr )
, owner( nullptr )
{
}
callback_invocation_holder( const callback_invocation_holder & ) = delete;
callback_invocation_holder & operator=( const callback_invocation_holder & ) = delete;

callback_invocation_holder( callback_invocation_holder && other )
: invocation( other.invocation )
, owner( other.owner )
{
other.invocation = nullptr;
}

callback_invocation_holder( callback_invocation * invocation, callbacks_heap * owner )
: invocation( invocation )
, owner( owner )
{
}

~callback_invocation_holder()
{
if( invocation )
owner->deallocate( invocation );
}

callback_invocation_holder & operator=( callback_invocation_holder && other )
{
invocation = other.invocation;
owner = other.owner;
other.invocation = nullptr;
return *this;
}

operator bool() { return invocation != nullptr; }

private:
callback_invocation * invocation;
callbacks_heap * owner;
};


} // namespace librealsense
7 changes: 4 additions & 3 deletions src/concurrency.h
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,8 @@ class dispatcher
: _owner(owner)
{}

bool was_stopped() const { return _owner->_was_stopped.load(); }

// Replacement for sleep() -- try to sleep for a time, but stop if the
// dispatcher is stopped
//
Expand All @@ -296,13 +298,12 @@ class dispatcher
using namespace std::chrono;

std::unique_lock<std::mutex> lock(_owner->_was_stopped_mutex);
auto dispatcher_was_stopped = [&]() { return _owner->_was_stopped.load(); };
if( dispatcher_was_stopped() )
if( was_stopped() )
return false;
// wait_for() returns "false if the predicate pred still evaluates to false after the
// rel_time timeout expired, otherwise true"
return ! (
_owner->_was_stopped_cv.wait_for( lock, sleep_time, dispatcher_was_stopped ) );
_owner->_was_stopped_cv.wait_for( lock, sleep_time, [&]() { return was_stopped(); } ) );
}
};

Expand Down
47 changes: 47 additions & 0 deletions src/easyloggingpp.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// License: Apache 2.0. See LICENSE file in root directory.
// Copyright(c) 2021 Intel Corporation. All Rights Reserved.

#pragma once

#if BUILD_EASYLOGGINGPP

#include "../third-party/easyloggingpp/src/easylogging++.h"

#ifdef RS2_USE_ANDROID_BACKEND
#include <android/log.h>
#include <sstream>

#define LOG_TAG "librs"

#define LOG_INFO(...) do { std::ostringstream ss; ss << __VA_ARGS__; __android_log_write(librealsense::ANDROID_LOG_INFO, LOG_TAG, ss.str().c_str()); } while(false)
#define LOG_WARNING(...) do { std::ostringstream ss; ss << __VA_ARGS__; __android_log_write(librealsense::ANDROID_LOG_WARN, LOG_TAG, ss.str().c_str()); } while(false)
#define LOG_ERROR(...) do { std::ostringstream ss; ss << __VA_ARGS__; __android_log_write(librealsense::ANDROID_LOG_ERROR, LOG_TAG, ss.str().c_str()); } while(false)
#define LOG_FATAL(...) do { std::ostringstream ss; ss << __VA_ARGS__; __android_log_write(librealsense::ANDROID_LOG_ERROR, LOG_TAG, ss.str().c_str()); } while(false)
#ifdef NDEBUG
#define LOG_DEBUG(...)
#else
#define LOG_DEBUG(...) do { std::ostringstream ss; ss << __VA_ARGS__; __android_log_write(librealsense::ANDROID_LOG_DEBUG, LOG_TAG, ss.str().c_str()); } while(false)
#endif

#else //RS2_USE_ANDROID_BACKEND

#define LOG_DEBUG(...) do { CLOG(DEBUG ,"librealsense") << __VA_ARGS__; } while(false)
#define LOG_INFO(...) do { CLOG(INFO ,"librealsense") << __VA_ARGS__; } while(false)
#define LOG_WARNING(...) do { CLOG(WARNING ,"librealsense") << __VA_ARGS__; } while(false)
#define LOG_ERROR(...) do { CLOG(ERROR ,"librealsense") << __VA_ARGS__; } while(false)
#define LOG_FATAL(...) do { CLOG(FATAL ,"librealsense") << __VA_ARGS__; } while(false)

#endif // RS2_USE_ANDROID_BACKEND


#else // BUILD_EASYLOGGINGPP


#define LOG_DEBUG(...) do { ; } while(false)
#define LOG_INFO(...) do { ; } while(false)
#define LOG_WARNING(...) do { ; } while(false)
#define LOG_ERROR(...) do { ; } while(false)
#define LOG_FATAL(...) do { ; } while(false)


#endif // BUILD_EASYLOGGINGPP
4 changes: 2 additions & 2 deletions src/gl/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# License: Apache 2.0. See LICENSE file in root directory.
# Copyright(c) 2019 Intel Corporation. All Rights Reserved.

cmake_minimum_required(VERSION 2.8.9)
cmake_minimum_required(VERSION 2.8.9...3.20.5)

project(realsense2-gl)

Expand Down Expand Up @@ -88,7 +88,7 @@ configure_package_config_file(../../CMake/realsense2-glConfig.cmake.in realsense

configure_file(../../config/librealsense-gl.pc.in ../../config/realsense2-gl.pc @ONLY)

target_link_libraries(${PROJECT_NAME}
target_link_libraries(${PROJECT_NAME}
PRIVATE ${DEPENDENCIES}
)

Expand Down
144 changes: 144 additions & 0 deletions src/librealsense-exception.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
// License: Apache 2.0. See LICENSE file in root directory.
// Copyright(c) 2021 Intel Corporation. All Rights Reserved.

#pragma once

#include <librealsense2/h/rs_types.h>
#include "easyloggingpp.h"
#include "basics.h" // LRS_EXTENSION_API

#include <exception>
#include <string>


namespace librealsense {


class librealsense_exception : public std::exception
{
public:
const char * get_message() const noexcept { return _msg.c_str(); }

rs2_exception_type get_exception_type() const noexcept { return _exception_type; }

const char * what() const noexcept override { return _msg.c_str(); }

protected:
librealsense_exception( const std::string & msg, rs2_exception_type exception_type ) noexcept
: _msg( msg )
, _exception_type( exception_type )
{
}

private:
std::string _msg;
rs2_exception_type _exception_type;
};


class LRS_EXTENSION_API recoverable_exception : public librealsense_exception
{
public:
recoverable_exception( const std::string & msg, rs2_exception_type exception_type ) noexcept;
};


class unrecoverable_exception : public librealsense_exception
{
public:
unrecoverable_exception( const std::string & msg, rs2_exception_type exception_type ) noexcept
: librealsense_exception( msg, exception_type )
{
LOG_ERROR( msg );
}
};


class io_exception : public unrecoverable_exception
{
public:
io_exception( const std::string & msg ) noexcept
: unrecoverable_exception( msg, RS2_EXCEPTION_TYPE_IO )
{
}
};


class camera_disconnected_exception : public unrecoverable_exception
{
public:
camera_disconnected_exception( const std::string & msg ) noexcept
: unrecoverable_exception( msg, RS2_EXCEPTION_TYPE_CAMERA_DISCONNECTED )
{
}
};


class backend_exception : public unrecoverable_exception
{
public:
backend_exception( const std::string & msg, rs2_exception_type exception_type ) noexcept
: unrecoverable_exception( msg, exception_type )
{
}
};


class linux_backend_exception : public backend_exception
{
public:
linux_backend_exception( const std::string & msg ) noexcept
: backend_exception( generate_last_error_message( msg ), RS2_EXCEPTION_TYPE_BACKEND )
{
}

private:
std::string generate_last_error_message( const std::string & msg ) const
{
return msg + " Last Error: " + strerror( errno );
}
};


class windows_backend_exception : public backend_exception
{
public:
// TODO: get last error
windows_backend_exception( const std::string & msg ) noexcept
: backend_exception( msg, RS2_EXCEPTION_TYPE_BACKEND )
{
}
};


class invalid_value_exception : public recoverable_exception
{
public:
invalid_value_exception( const std::string & msg ) noexcept
: recoverable_exception( msg, RS2_EXCEPTION_TYPE_INVALID_VALUE )
{
}
};


class wrong_api_call_sequence_exception : public recoverable_exception
{
public:
wrong_api_call_sequence_exception( const std::string & msg ) noexcept
: recoverable_exception( msg, RS2_EXCEPTION_TYPE_WRONG_API_CALL_SEQUENCE )
{
}
};


class not_implemented_exception : public recoverable_exception
{
public:
not_implemented_exception( const std::string & msg ) noexcept
: recoverable_exception( msg, RS2_EXCEPTION_TYPE_NOT_IMPLEMENTED )
{
}
};


} // namespace librealsense
1 change: 1 addition & 0 deletions src/libuvc/rsusb-backend-linux.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include "rsusb-backend-linux.h"
#include "types.h"
#include "../polling-device-watcher.h"
#include "../uvc/uvc-device.h"

namespace librealsense
Expand Down
13 changes: 13 additions & 0 deletions src/linux/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,16 @@ target_link_libraries(${LRS_TARGET} PRIVATE usb)
if(USE_EXTERNAL_USB)
add_dependencies(${LRS_TARGET} libusb)
endif()

include(${CMAKE_CURRENT_LIST_DIR}/find_udev.cmake)
if(UDEV_FOUND)
target_sources(${LRS_TARGET}
PRIVATE
"${CMAKE_CURRENT_LIST_DIR}/udev-device-watcher.cpp"
"${CMAKE_CURRENT_LIST_DIR}/udev-device-watcher.h"
)
target_link_libraries(${LRS_TARGET} PRIVATE udev)
add_definitions(-DUSING_UDEV)
else()
message(STATUS "UDEV not found; using polling device-watcher!")
endif()
Loading