Skip to content

Commit

Permalink
PR #12259 from Eran: Device hub fixes and small refactor in backend
Browse files Browse the repository at this point in the history
  • Loading branch information
maloel authored Oct 5, 2023
2 parents 25b4b59 + 954033e commit 0533d07
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 84 deletions.
49 changes: 8 additions & 41 deletions src/backend-device-factory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,52 +25,25 @@ std::shared_ptr< backend > create_backend();
} // namespace librealsense


namespace {


// Returns true if the left group is completely accounted for in the right group
//
bool group_contained_in( librealsense::platform::backend_device_group const & first_data,
librealsense::platform::backend_device_group const & second_data )
{
for( auto && uvc : first_data.uvc_devices )
{
if( std::find( second_data.uvc_devices.begin(), second_data.uvc_devices.end(), uvc )
== second_data.uvc_devices.end() )
return false;
}
for( auto && usb : first_data.usb_devices )
{
if( std::find( second_data.usb_devices.begin(), second_data.usb_devices.end(), usb )
== second_data.usb_devices.end() )
return false;
}
for( auto && hid : first_data.hid_devices )
{
if( std::find( second_data.hid_devices.begin(), second_data.hid_devices.end(), hid )
== second_data.hid_devices.end() )
return false;
}
return true;
}
namespace librealsense {


std::vector< std::shared_ptr< librealsense::platform::platform_device_info > >
subtract_sets( const std::vector< std::shared_ptr< librealsense::platform::platform_device_info > > & first,
const std::vector< std::shared_ptr< librealsense::platform::platform_device_info > > & second )
std::vector< std::shared_ptr< platform::platform_device_info > >
subtract_sets( const std::vector< std::shared_ptr< platform::platform_device_info > > & first,
const std::vector< std::shared_ptr< platform::platform_device_info > > & second )
{
std::vector< std::shared_ptr< librealsense::platform::platform_device_info > > results;
std::vector< std::shared_ptr< platform::platform_device_info > > results;
std::for_each(
first.begin(),
first.end(),
[&]( std::shared_ptr< librealsense::platform::platform_device_info > const & data )
[&]( std::shared_ptr< platform::platform_device_info > const & data )
{
if( std::find_if(
second.begin(),
second.end(),
[&]( std::shared_ptr< librealsense::platform::platform_device_info > const & new_dev )
[&]( std::shared_ptr< platform::platform_device_info > const & new_dev )
{
return group_contained_in( data->get_group(), new_dev->get_group() );
return data->get_group().is_contained_in( new_dev->get_group() );
} )
== second.end() )
{
Expand All @@ -81,12 +54,6 @@ subtract_sets( const std::vector< std::shared_ptr< librealsense::platform::platf
}


} // namespace


namespace librealsense {


// This singleton creates the actual backend; as long as someone holds it, the backend will stay alive.
// The instance is held below by the device-watcher. I.e., the device-watcher triggers creation of the
// backend!
Expand Down
48 changes: 7 additions & 41 deletions src/device_hub.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,57 +6,23 @@
#include "core/processing.h"
#include "proc/synthetic-stream.h"
#include "device_hub.h"
#include "platform/platform-device-info.h"

namespace librealsense
{
typedef rs2::devices_changed_callback<std::function<void(rs2::event_information& info)>> hub_devices_changed_callback;

std::vector< std::shared_ptr< device_info > >
filter_by_vid( std::vector< std::shared_ptr< device_info > > const & devices, int vid )
device_hub::device_hub(std::shared_ptr<librealsense::context> ctx, int mask)
: _ctx( ctx )
, _mask( mask )
, _device_changes_callback_id( 0 )
{
std::vector<std::shared_ptr<device_info>> result;
for (auto & dev : devices)
{
auto pdev = std::dynamic_pointer_cast< platform::platform_device_info >( dev );
if( ! pdev )
{
if( vid == 0 )
result.push_back( dev );
continue;
}
auto & data = pdev->get_group();
for (const auto& usb : data.usb_devices)
{
if (usb.vid == vid || vid == 0)
{
result.push_back(dev);
break;
}
}
for (const auto& uvc : data.uvc_devices)
{
if (uvc.vid == vid || vid == 0)
{
result.push_back(dev);
break;
}
}
}
return result;
}

device_hub::device_hub(std::shared_ptr<librealsense::context> ctx, int mask, int vid)
: _ctx(ctx), _vid(vid),
_device_changes_callback_id(0)
{
_device_list = filter_by_vid(_ctx->query_devices(mask), _vid);
_device_list = _ctx->query_devices(mask);

auto cb = new hub_devices_changed_callback([&,mask](rs2::event_information&)
{
std::unique_lock<std::mutex> lock(_mutex);

_device_list = filter_by_vid(_ctx->query_devices(mask), _vid);
_device_list = _ctx->query_devices(mask);

// Current device will point to the first available device
_camera_index = 0;
Expand Down Expand Up @@ -126,7 +92,7 @@ namespace librealsense
std::shared_ptr<device_interface> res = nullptr;

// check if there is at least one device connected
_device_list = filter_by_vid(_ctx->query_devices(RS2_PRODUCT_LINE_ANY), _vid);
_device_list = _ctx->query_devices(_mask);
if (_device_list.size() > 0)
{
res = create_device(serial, loop_through_devices);
Expand Down
4 changes: 2 additions & 2 deletions src/device_hub.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ namespace librealsense
class device_hub
{
public:
explicit device_hub(std::shared_ptr<librealsense::context> ctx, int mask = RS2_PRODUCT_LINE_ANY, int vid = 0);
explicit device_hub(std::shared_ptr<librealsense::context> ctx, int mask = RS2_PRODUCT_LINE_ANY);

~device_hub();

Expand Down Expand Up @@ -52,7 +52,7 @@ namespace librealsense
std::condition_variable _cv;
std::vector<std::shared_ptr<device_info>> _device_list;
int _camera_index = 0;
int _vid = 0;
uint64_t _device_changes_callback_id;
int _mask;
};
}
27 changes: 27 additions & 0 deletions src/platform/backend-device-group.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include <functional>
#include <vector>
#include <string>
#include <algorithm>


namespace librealsense {
Expand Down Expand Up @@ -106,6 +107,32 @@ struct backend_device_group

return s;
}


// Returns true if this group is completely accounted for in the right group
//
bool is_contained_in( backend_device_group const & second_data ) const
{
for( auto & uvc : uvc_devices )
{
if( std::find( second_data.uvc_devices.begin(), second_data.uvc_devices.end(), uvc )
== second_data.uvc_devices.end() )
return false;
}
for( auto & usb : usb_devices )
{
if( std::find( second_data.usb_devices.begin(), second_data.usb_devices.end(), usb )
== second_data.usb_devices.end() )
return false;
}
for( auto & hid : hid_devices )
{
if( std::find( second_data.hid_devices.begin(), second_data.hid_devices.end(), hid )
== second_data.hid_devices.end() )
return false;
}
return true;
}
};


Expand Down
10 changes: 10 additions & 0 deletions src/platform/platform-device-info.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <src/device-info.h>

#include <memory>
#include <vector>


namespace librealsense {
Expand Down Expand Up @@ -59,4 +60,13 @@ class platform_device_info : public device_info


} // namespace platform


// subtract_sets( left, right ) = what is in left that's not in right
//
std::vector< std::shared_ptr< platform::platform_device_info > >
subtract_sets( const std::vector< std::shared_ptr< platform::platform_device_info > > & left,
const std::vector< std::shared_ptr< platform::platform_device_info > > & right );


} // namespace librealsense

0 comments on commit 0533d07

Please sign in to comment.