context.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.h"
5 #include "device-info.h"
6 
8 #ifdef BUILD_WITH_DDS
10 #endif
12 
13 #include <librealsense2/hpp/rs_types.hpp> // rs2_devices_changed_callback
14 #include <librealsense2/rs.h> // RS2_API_FULL_VERSION_STR
16 
20 #include <rsutils/string/from.h>
21 #include <rsutils/json.h>
22 #include <rsutils/json-config.h>
24 
25 
26 namespace librealsense {
27 
28 
29  static rsutils::json load_settings( rsutils::json const & context_settings )
30  {
31  // Allow ignoring of any other settings, global or not!
32  if( ! context_settings.nested( "inherit" ).default_value( true ) )
33  return context_settings;
34 
37 
38  // Take only the 'context' part of it
39  config = rsutils::json_config::load_settings( config, "context", "config-file" );
40 
41  // Patch the given context settings into the configuration
42  config.override( context_settings, "context settings" );
43  return config;
44  }
45 
46 
48  : _settings( load_settings( settings ) ) // global | application | local
49  , _device_mask( _settings.nested( "device-mask" ).default_value< unsigned >( RS2_PRODUCT_LINE_ANY ) )
50  {
51  static bool version_logged = false;
52  if( ! version_logged )
53  {
54  version_logged = true;
55  LOG_DEBUG( "Librealsense VERSION: " << RS2_API_FULL_VERSION_STR );
56  }
57  }
58 
59 
60  void context::create_factories( std::shared_ptr< context > const & sptr )
61  {
62  _factories.push_back( std::make_shared< backend_device_factory >(
63  sptr,
64  [this]( std::vector< std::shared_ptr< device_info > > const & removed,
65  std::vector< std::shared_ptr< device_info > > const & added )
66  { invoke_devices_changed_callbacks( removed, added ); } ) );
67 
68 #ifdef BUILD_WITH_DDS
69  _factories.push_back( std::make_shared< rsdds_device_factory >(
70  sptr,
71  [this]( std::vector< std::shared_ptr< device_info > > const & removed,
72  std::vector< std::shared_ptr< device_info > > const & added )
73  { invoke_devices_changed_callbacks( removed, added ); } ) );
74 #endif
75  }
76 
77 
78  /*static*/ std::shared_ptr< context > context::make( json const & settings )
79  {
80  std::shared_ptr< context > sptr( new context( settings ) );
81  sptr->create_factories( sptr );
82  return sptr;
83  }
84 
85 
86  /*static*/ std::shared_ptr< context > context::make( char const * json_settings )
87  {
88  return make( ( ! json_settings || ! *json_settings ) ? json::object() : json::parse( json_settings ) );
89  }
90 
91 
93  {
94  }
95 
96 
97  /*static*/ unsigned context::combine_device_masks( unsigned const requested_mask, unsigned const mask_in_settings )
98  {
99  unsigned mask = requested_mask;
100  // The normal bits enable, so enable only those that are on
101  mask &= mask_in_settings & ~RS2_PRODUCT_LINE_SW_ONLY;
102  // But the above turned off the SW-only bits, so turn them back on again
103  if( ( mask_in_settings & RS2_PRODUCT_LINE_SW_ONLY ) || ( requested_mask & RS2_PRODUCT_LINE_SW_ONLY ) )
105  return mask;
106  }
107 
108 
109  std::vector< std::shared_ptr< device_info > > context::query_devices( int requested_mask ) const
110  {
111  std::vector< std::shared_ptr< device_info > > list;
112  for( auto & factory : _factories )
113  {
114  for( auto & dev_info : factory->query_devices( requested_mask ) )
115  {
116  LOG_INFO( "... " << dev_info->get_address() );
117  list.push_back( dev_info );
118  }
119  }
120  for( auto & item : _user_devices )
121  {
122  if( auto dev_info = item.second.lock() )
123  {
124  LOG_INFO( "... " << dev_info->get_address() );
125  list.push_back( dev_info );
126  }
127  }
128  LOG_INFO( "Found " << list.size() << " RealSense devices (0x" << std::hex << requested_mask << " requested & 0x"
129  << get_device_mask() << " from device-mask in settings)" << std::dec );
130  return list;
131  }
132 
133 
135  std::vector< std::shared_ptr< device_info > > const & rs2_devices_info_removed,
136  std::vector< std::shared_ptr< device_info > > const & rs2_devices_info_added )
137  {
138  _devices_changed.raise( rs2_devices_info_removed, rs2_devices_info_added );
139  }
140 
141 
143  {
144  return _devices_changed.subscribe( std::move( callback ) );
145  }
146 
147 
148  void context::add_device( std::shared_ptr< device_info > const & dev )
149  {
150  auto address = dev->get_address();
151 
152  auto it = _user_devices.find( address );
153  if( it != _user_devices.end() && it->second.lock() )
154  throw librealsense::invalid_value_exception( "device already in context: " + address );
155  _user_devices[address] = dev;
156 
157  std::vector< std::shared_ptr< device_info > > rs2_device_info_added{ dev };
158  invoke_devices_changed_callbacks( {}, rs2_device_info_added );
159  }
160 
161 
162  void context::remove_device( std::shared_ptr< device_info > const & dev )
163  {
164  auto address = dev->get_address();
165 
166  auto it = _user_devices.find( address );
167  if(it == _user_devices.end() )
168  return; // Why not throw?!
169  auto dev_info = it->second.lock();
170  _user_devices.erase(it);
171 
172  if( dev_info )
173  {
174  std::vector< std::shared_ptr< device_info > > rs2_device_info_removed{ dev_info };
175  invoke_devices_changed_callbacks( rs2_device_info_removed, {} );
176  }
177  }
178 
179 
180  std::shared_ptr< processing_block_interface > context::create_pp_block( std::string const & name,
181  rsutils::json const & settings )
182  {
184  }
185 
186 } // namespace librealsense
librealsense
Definition: algo.h:18
easyloggingpp.h
RS2_PRODUCT_LINE_ANY
#define RS2_PRODUCT_LINE_ANY
Definition: rs_context.h:116
librealsense::invalid_value_exception
Definition: librealsense-exception.h:114
rscore-pp-block-factory.h
factory
Factory * factory
Definition: librealsense_mex.cpp:12
string
GLsizei const GLchar *const * string
Definition: glad/glad/glad.h:2861
RS2_API_FULL_VERSION_STR
#define RS2_API_FULL_VERSION_STR
Definition: rs.h:45
LOG_DEBUG
#define LOG_DEBUG(...)
Definition: easyloggingpp.h:70
from.h
librealsense::context::query_devices
std::vector< std::shared_ptr< device_info > > query_devices(int mask) const
Definition: context.cpp:109
librealsense::context::_user_devices
std::map< std::string, std::weak_ptr< device_info > > _user_devices
Definition: context.h:74
librealsense::rscore_pp_block_factory
Definition: rscore-pp-block-factory.h:11
mask
GLint GLuint mask
Definition: glad/glad/glad.h:1460
librealsense::json
rsutils::json json
Definition: json_loader.hpp:26
librealsense::context::create_factories
void create_factories(std::shared_ptr< context > const &sptr)
Definition: context.cpp:60
rsutils::json_config::load_from_file
json load_from_file(std::string const &filename)
Definition: json.cpp:55
librealsense::context::combine_device_masks
static unsigned combine_device_masks(unsigned requested_mask, unsigned mask_in_settings)
Definition: context.cpp:97
executable-name.h
librealsense::load_settings
static rsutils::json load_settings(rsutils::json const &context_settings)
Definition: context.cpp:29
librealsense::context::create_pp_block
std::shared_ptr< processing_block_interface > create_pp_block(std::string const &name, rsutils::json const &settings)
Definition: context.cpp:180
librealsense::context::remove_device
void remove_device(std::shared_ptr< device_info > const &)
Definition: context.cpp:162
librealsense-exception.h
librealsense::context::invoke_devices_changed_callbacks
void invoke_devices_changed_callbacks(std::vector< std::shared_ptr< device_info > > const &devices_removed, std::vector< std::shared_ptr< device_info > > const &devices_added)
Definition: context.cpp:134
device-info.h
devices.settings
dictionary settings
Definition: third-party/realdds/scripts/devices.py:38
RS2_CONFIG_FILENAME
#define RS2_CONFIG_FILENAME
Definition: rs.h:59
test-device-discovery.nested
nested
Definition: test-device-discovery.py:59
librealsense::rscore_pp_block_factory::create_pp_block
std::shared_ptr< processing_block_interface > create_pp_block(std::string const &name, rsutils::json const &settings) override
Definition: rscore-pp-block-factory.cpp:23
test-projection-from-recording.dev
dev
Definition: test-projection-from-recording.py:17
librealsense::context::_devices_changed
rsutils::signal< std::vector< std::shared_ptr< device_info > > const &, std::vector< std::shared_ptr< device_info > > const & > _devices_changed
Definition: context.h:78
librealsense::context::~context
~context()
Definition: context.cpp:92
name
GLuint const GLchar * name
Definition: glad/glad/glad.h:2777
librealsense::context::_factories
std::vector< std::shared_ptr< device_factory > > _factories
Definition: context.h:83
rsutils::json_config::load_settings
json load_settings(json const &global, json_key const &subkey, std::string const &error_context)
Definition: json.cpp:135
librealsense::context::on_device_changes
rsutils::subscription on_device_changes(devices_changed_callback &&)
Definition: context.cpp:142
rsutils::json
nlohmann::basic_json< std::map, std::vector, json_key, bool, std::int64_t, std::uint64_t, double, std::allocator, nlohmann::adl_serializer, std::vector< std::uint8_t >, json_base > json
Definition: json-fwd.h:66
rsutils::os::get_special_folder
std::string get_special_folder(special_folder)
Definition: special-folder.cpp:26
test-projection-from-recording.filename
filename
Definition: test-projection-from-recording.py:15
librealsense::context::add_device
void add_device(std::shared_ptr< device_info > const &)
Definition: context.cpp:148
librealsense::context::make
static std::shared_ptr< context > make(rsutils::json const &)
Definition: context.cpp:78
librealsense::context::context
context(rsutils::json const &)
Definition: context.cpp:47
rsutils::os::special_folder::app_data
@ app_data
json
rsutils::json json
Definition: rs-config.cpp:11
test-d405-calibration-stream.config
config
Definition: test-d405-calibration-stream.py:22
rs.h
Exposes librealsense functionality for C compilers.
rsutils::subscription
Definition: subscription.h:25
json.h
context.h
librealsense::context::get_device_mask
unsigned get_device_mask() const
Definition: context.h:32
json-config.h
special-folder.h
RS2_PRODUCT_LINE_SW_ONLY
#define RS2_PRODUCT_LINE_SW_ONLY
Definition: rs_context.h:124
it
static auto it
Definition: openvino-face-detection.cpp:375
rsdds-device-factory.h
librealsense::context::devices_changed_callback
std::function< void(std::vector< std::shared_ptr< device_info > > const &devices_removed, std::vector< std::shared_ptr< device_info > > const &devices_added) > devices_changed_callback
Definition: context.h:52
backend-device-factory.h
LOG_INFO
LOG_INFO("Log message using LOG_INFO()")
MatlabParamParser::parse
static T parse(const mxArray *cell)
Definition: MatlabParamParser.h:161
rs_types.hpp


librealsense2
Author(s): LibRealSense ROS Team
autogenerated on Fri Aug 2 2024 08:30:01