diff --git a/include/librealsense2/h/rs_option.h b/include/librealsense2/h/rs_option.h index 1d862eef814..19ea0294d6f 100644 --- a/include/librealsense2/h/rs_option.h +++ b/include/librealsense2/h/rs_option.h @@ -47,11 +47,11 @@ extern "C" { RS2_OPTION_ASIC_TEMPERATURE, /**< Current Asic Temperature */ RS2_OPTION_ERROR_POLLING_ENABLED, /**< disable error handling */ RS2_OPTION_PROJECTOR_TEMPERATURE, /**< Current Projector Temperature */ - RS2_OPTION_OUTPUT_TRIGGER_ENABLED, /**< Enable / disable trigger to be outputed from the camera to any external device on every depth frame */ + RS2_OPTION_OUTPUT_TRIGGER_ENABLED, /**< Enable / disable trigger to be outputted from the camera to any external device on every depth frame */ RS2_OPTION_MOTION_MODULE_TEMPERATURE, /**< Current Motion-Module Temperature */ RS2_OPTION_DEPTH_UNITS, /**< Number of meters represented by a single depth unit */ RS2_OPTION_ENABLE_MOTION_CORRECTION, /**< Enable/Disable automatic correction of the motion data */ - RS2_OPTION_AUTO_EXPOSURE_PRIORITY, /**< Allows sensor to dynamically ajust the frame rate depending on lighting conditions */ + RS2_OPTION_AUTO_EXPOSURE_PRIORITY, /**< Allows sensor to dynamically adjust the frame rate depending on lighting conditions */ RS2_OPTION_COLOR_SCHEME, /**< Color scheme for data visualization */ RS2_OPTION_HISTOGRAM_EQUALIZATION_ENABLED, /**< Perform histogram equalization post-processing on the depth data */ RS2_OPTION_MIN_DISTANCE, /**< Minimal distance to the target */ @@ -62,7 +62,7 @@ extern "C" { RS2_OPTION_FILTER_SMOOTH_DELTA, /**< 2D-filter range/validity threshold*/ RS2_OPTION_HOLES_FILL, /**< Enhance depth data post-processing with holes filling where appropriate*/ RS2_OPTION_STEREO_BASELINE, /**< The distance in mm between the first and the second imagers in stereo-based depth cameras*/ - RS2_OPTION_AUTO_EXPOSURE_CONVERGE_STEP, /**< Allows dynamically ajust the converge step value of the target exposure in Auto-Exposure algorithm*/ + RS2_OPTION_AUTO_EXPOSURE_CONVERGE_STEP, /**< Allows dynamically adjust the converge step value of the target exposure in Auto-Exposure algorithm*/ RS2_OPTION_INTER_CAM_SYNC_MODE, /**< Impose Inter-camera HW synchronization mode. Applicable for D400/L500/Rolling Shutter SKUs */ RS2_OPTION_STREAM_FILTER, /**< Select a stream to process */ RS2_OPTION_STREAM_FORMAT_FILTER, /**< Select a stream format to process */ diff --git a/src/ds/d400/d400-active.cpp b/src/ds/d400/d400-active.cpp index 2bb4fb6c9a7..7fb88d26a96 100644 --- a/src/ds/d400/d400-active.cpp +++ b/src/ds/d400/d400-active.cpp @@ -33,7 +33,6 @@ namespace librealsense //PROJECTOR TEMPERATURE OPTION auto pid = this->_pid; auto& depth_ep = get_depth_sensor(); - auto &raw_depth_ep = get_raw_depth_sensor(); if( pid == ds::RS457_PID ) { @@ -44,13 +43,8 @@ namespace librealsense } else { - auto uvc_s = std::dynamic_pointer_cast< uvc_sensor >( raw_depth_ep.shared_from_this() ); - if( ! uvc_s ) - throw std::runtime_error( "Sensor base is not a uvc sensor" ); - depth_ep.register_option( RS2_OPTION_PROJECTOR_TEMPERATURE, - std::make_shared< asic_and_projector_temperature_options >( - std::move( uvc_s ), + std::make_shared< asic_and_projector_temperature_options >( get_raw_depth_sensor(), RS2_OPTION_PROJECTOR_TEMPERATURE ) ); } } diff --git a/src/ds/d400/d400-color.cpp b/src/ds/d400/d400-color.cpp index 131cbc07394..339df585fb9 100644 --- a/src/ds/d400/d400-color.cpp +++ b/src/ds/d400/d400-color.cpp @@ -131,7 +131,7 @@ namespace librealsense void d400_color::init() { auto& color_ep = get_color_sensor(); - auto& raw_color_ep = get_raw_color_sensor(); + auto raw_color_ep = get_raw_color_sensor(); _ds_color_common = std::make_shared(raw_color_ep, color_ep, _fw_version, _hw_monitor, this); @@ -151,7 +151,6 @@ namespace librealsense void d400_color::register_options() { auto& color_ep = get_color_sensor(); - auto& raw_color_ep = get_raw_color_sensor(); if (!val_in_range(_pid, { ds::RS457_PID })) { @@ -222,7 +221,7 @@ namespace librealsense // attributes of md_capture_stats auto& color_ep = get_color_sensor(); // attributes of md_rgb_control - auto& raw_color_ep = get_raw_color_sensor(); + auto raw_color_ep = get_raw_color_sensor(); if (_pid != ds::RS457_PID) { @@ -231,7 +230,7 @@ namespace librealsense } else { - auto uvc_dev = raw_color_ep.get_uvc_device(); + auto uvc_dev = raw_color_ep->get_uvc_device(); if (uvc_dev->is_platform_jetson()) { // Work-around for discrepancy between the RGB YUYV descriptor and the parser . Use UYUV parser instead diff --git a/src/ds/d400/d400-color.h b/src/ds/d400/d400-color.h index ccbf9fdd7d7..c07c60398f2 100644 --- a/src/ds/d400/d400-color.h +++ b/src/ds/d400/d400-color.h @@ -24,10 +24,10 @@ namespace librealsense return dynamic_cast(get_sensor(_color_device_idx)); } - uvc_sensor& get_raw_color_sensor() + std::shared_ptr< uvc_sensor > get_raw_color_sensor() { - synthetic_sensor& color_sensor = get_color_sensor(); - return dynamic_cast(*color_sensor.get_raw_sensor()); + synthetic_sensor & color_sensor = get_color_sensor(); + return std::dynamic_pointer_cast< uvc_sensor >( color_sensor.get_raw_sensor() ); } protected: diff --git a/src/ds/d400/d400-device.cpp b/src/ds/d400/d400-device.cpp index bdf64596969..1bb9e2b679a 100644 --- a/src/ds/d400/d400-device.cpp +++ b/src/ds/d400/d400-device.cpp @@ -523,7 +523,7 @@ namespace librealsense { using namespace ds; - auto& raw_sensor = get_raw_depth_sensor(); + auto raw_sensor = get_raw_depth_sensor(); _pid = group.uvc_devices.front().pid; // to be changed for D457 bool mipi_sensor = (RS457_PID == _pid); @@ -537,16 +537,16 @@ namespace librealsense { _hw_monitor = std::make_shared( std::make_shared( - std::make_shared( - raw_sensor, depth_xu, DS5_HWMONITOR), - raw_sensor)); + std::make_shared( *raw_sensor, depth_xu, DS5_HWMONITOR ), + raw_sensor ) ); } else { if( ! mipi_sensor ) - _hw_monitor = std::make_shared< hw_monitor >( std::make_shared< locked_transfer >( - get_backend()->create_usb_device( group.usb_devices.front() ), - raw_sensor ) ); + _hw_monitor = std::make_shared< hw_monitor >( + std::make_shared< locked_transfer >( + get_backend()->create_usb_device( group.usb_devices.front() ), + raw_sensor ) ); } set_hw_monitor_for_auto_calib(_hw_monitor); @@ -578,7 +578,7 @@ namespace librealsense std::vector gvd_buff(HW_MONITOR_BUFFER_SIZE); auto& depth_sensor = get_depth_sensor(); - auto& raw_depth_sensor = get_raw_depth_sensor(); + auto raw_depth_sensor = get_raw_depth_sensor(); using namespace platform; @@ -607,7 +607,7 @@ namespace librealsense usb_modality = (_fw_version >= firmware_version("5.9.8.0")); if (usb_modality) { - _usb_mode = raw_depth_sensor.get_usb_specification(); + _usb_mode = raw_depth_sensor->get_usb_specification(); if (usb_spec_names.count(_usb_mode) && (usb_undefined != _usb_mode)) usb_type_str = usb_spec_names.at(_usb_mode); else // Backend fails to provide USB descriptor - occurs with RS3 build. Requires further work @@ -648,10 +648,10 @@ namespace librealsense if ((_pid == RS416_PID || _pid == RS416_RGB_PID) && _fw_version >= firmware_version("5.12.0.1")) { depth_sensor.register_option(RS2_OPTION_HARDWARE_PRESET, - std::make_shared>(raw_depth_sensor, depth_xu, DS5_HARDWARE_PRESET, - "Hardware pipe configuration")); + std::make_shared>( raw_depth_sensor, depth_xu, DS5_HARDWARE_PRESET, + "Hardware pipe configuration" ) ); depth_sensor.register_option(RS2_OPTION_LED_POWER, - std::make_shared>(raw_depth_sensor, depth_xu, DS5_LED_PWR, + std::make_shared>( raw_depth_sensor, depth_xu, DS5_LED_PWR, "Set the power level of the LED, with 0 meaning LED off")); } @@ -673,23 +673,20 @@ namespace librealsense else { depth_sensor.register_option(RS2_OPTION_OUTPUT_TRIGGER_ENABLED, - std::make_shared>(raw_depth_sensor, depth_xu, DS5_EXT_TRIGGER, + std::make_shared>( raw_depth_sensor, depth_xu, DS5_EXT_TRIGGER, "Generate trigger from the camera to external device once per frame")); - auto uvc_s = std::dynamic_pointer_cast(raw_depth_sensor.shared_from_this()); - if (!uvc_s) - throw std::runtime_error("Sensor base is not uvc sensor"); - depth_sensor.register_option(RS2_OPTION_ASIC_TEMPERATURE, - std::make_shared(std::move(uvc_s), - RS2_OPTION_ASIC_TEMPERATURE)); + std::make_shared< asic_and_projector_temperature_options >( raw_depth_sensor, + RS2_OPTION_ASIC_TEMPERATURE ) ); // D457 dev - get_xu fails for D457 - error polling id not defined - auto error_control = std::make_shared>(raw_depth_sensor, depth_xu, DS5_ERROR_REPORTING, "Error reporting"); + auto error_control = std::make_shared>( raw_depth_sensor, depth_xu, + DS5_ERROR_REPORTING, "Error reporting"); _polling_error_handler = std::make_shared(1000, error_control, - raw_depth_sensor.get_notifications_processor(), + raw_depth_sensor->get_notifications_processor(), std::make_shared()); depth_sensor.register_option(RS2_OPTION_ERROR_POLLING_ENABLED, std::make_shared(_polling_error_handler)); @@ -698,7 +695,7 @@ namespace librealsense if ((val_in_range(_pid, { RS455_PID })) && (_fw_version >= firmware_version("5.12.11.0"))) { - auto thermal_compensation_toggle = std::make_shared>(raw_depth_sensor, depth_xu, + auto thermal_compensation_toggle = std::make_shared>( raw_depth_sensor, depth_xu, ds::DS5_THERMAL_COMPENSATION, "Toggle Thermal Compensation Mechanism"); auto temperature_sensor = depth_sensor.get_option_handler(RS2_OPTION_ASIC_TEMPERATURE); @@ -730,16 +727,16 @@ namespace librealsense std::shared_ptr hdr_enabled_option = nullptr; //EXPOSURE AND GAIN - preparing uvc options - auto uvc_xu_exposure_option = std::make_shared>(raw_depth_sensor, + auto uvc_xu_exposure_option = std::make_shared>( raw_depth_sensor, depth_xu, DS5_EXPOSURE, "Depth Exposure (usec)"); option_range exposure_range = uvc_xu_exposure_option->get_range(); - auto uvc_pu_gain_option = std::make_shared(raw_depth_sensor, RS2_OPTION_GAIN); + auto uvc_pu_gain_option = std::make_shared( raw_depth_sensor, RS2_OPTION_GAIN); option_range gain_range = uvc_pu_gain_option->get_range(); //AUTO EXPOSURE - auto enable_auto_exposure = std::make_shared>(raw_depth_sensor, + auto enable_auto_exposure = std::make_shared>( raw_depth_sensor, depth_xu, DS5_ENABLE_AUTO_EXPOSURE, "Enable Auto Exposure"); @@ -830,8 +827,8 @@ namespace librealsense if ((_fw_version >= firmware_version("5.11.3.0")) && ((_device_capabilities & mask) == mask)) { bool is_fw_version_using_id = (_fw_version >= firmware_version("5.12.8.100")); - auto alternating_emitter_opt = std::make_shared(*_hw_monitor, &raw_depth_sensor, is_fw_version_using_id); - auto emitter_always_on_opt = std::make_shared(*_hw_monitor, &depth_sensor); + auto alternating_emitter_opt = std::make_shared(*_hw_monitor, is_fw_version_using_id); + auto emitter_always_on_opt = std::make_shared( *_hw_monitor ); if ((_fw_version >= firmware_version("5.12.1.0")) && ((_device_capabilities & ds_caps::CAP_GLOBAL_SHUTTER) == ds_caps::CAP_GLOBAL_SHUTTER)) { @@ -874,17 +871,17 @@ namespace librealsense if (_fw_version >= firmware_version("5.12.12.100") && (_device_capabilities & ds_caps::CAP_GLOBAL_SHUTTER) == ds_caps::CAP_GLOBAL_SHUTTER) { depth_sensor.register_option(RS2_OPTION_INTER_CAM_SYNC_MODE, - std::make_shared(*_hw_monitor, &raw_depth_sensor, 3)); + std::make_shared(*_hw_monitor, raw_depth_sensor, 3)); } else if (_fw_version >= firmware_version("5.12.4.0") && (_device_capabilities & ds_caps::CAP_GLOBAL_SHUTTER) == ds_caps::CAP_GLOBAL_SHUTTER) { depth_sensor.register_option(RS2_OPTION_INTER_CAM_SYNC_MODE, - std::make_shared(*_hw_monitor, &raw_depth_sensor, 2)); + std::make_shared(*_hw_monitor, raw_depth_sensor, 2)); } else if (_fw_version >= firmware_version("5.9.15.1")) { depth_sensor.register_option(RS2_OPTION_INTER_CAM_SYNC_MODE, - std::make_shared(*_hw_monitor, &raw_depth_sensor, 1)); + std::make_shared(*_hw_monitor, raw_depth_sensor, 1)); } } @@ -1283,27 +1280,23 @@ namespace librealsense auto pid = dev_info->get_group().uvc_devices.front().pid; if (pid != RS_USB2_PID) { - auto& depth_ep = get_raw_depth_sensor(); + auto depth_ep = get_raw_depth_sensor(); auto emitter_enabled = std::make_shared(depth_ep); - depth_ep.register_option(RS2_OPTION_EMITTER_ENABLED, emitter_enabled); + depth_ep->register_option(RS2_OPTION_EMITTER_ENABLED, emitter_enabled); auto laser_power = std::make_shared>(depth_ep, depth_xu, DS5_LASER_POWER, - "Manual laser power in mw. applicable only when laser power mode is set to Manual"); - depth_ep.register_option(RS2_OPTION_LASER_POWER, - std::make_shared( - laser_power, - emitter_enabled, - std::vector{0.f, 2.f}, 1.f)); - - auto uvc_s = std::dynamic_pointer_cast(depth_ep.shared_from_this()); - if (!uvc_s) - throw std::runtime_error("Sensor base is not uvc sensor"); - - depth_ep.register_option(RS2_OPTION_PROJECTOR_TEMPERATURE, - std::make_shared(std::move(uvc_s), - RS2_OPTION_PROJECTOR_TEMPERATURE)); + "Manual laser power in mw. applicable only when laser power mode is set to Manual" ); + depth_ep->register_option(RS2_OPTION_LASER_POWER, + std::make_shared< auto_disabling_control >( laser_power, + emitter_enabled, + std::vector< float >{ 0.f, 2.f }, + 1.f ) ); + + depth_ep->register_option(RS2_OPTION_PROJECTOR_TEMPERATURE, + std::make_shared< asic_and_projector_temperature_options >( depth_ep, + RS2_OPTION_PROJECTOR_TEMPERATURE ) ); } } } diff --git a/src/ds/d400/d400-device.h b/src/ds/d400/d400-device.h index f182c4afbd6..e77140f319f 100644 --- a/src/ds/d400/d400-device.h +++ b/src/ds/d400/d400-device.h @@ -39,10 +39,10 @@ namespace librealsense return dynamic_cast(get_sensor(_depth_device_idx)); } - uvc_sensor& get_raw_depth_sensor() + std::shared_ptr< uvc_sensor > get_raw_depth_sensor() { synthetic_sensor& depth_sensor = get_depth_sensor(); - return dynamic_cast(*depth_sensor.get_raw_sensor()); + return std::dynamic_pointer_cast< uvc_sensor >( depth_sensor.get_raw_sensor() ); } d400_device( std::shared_ptr< const d400_info > const & ); diff --git a/src/ds/d500/d500-color.cpp b/src/ds/d500/d500-color.cpp index 6e8ca518dfd..a9c98e95b82 100644 --- a/src/ds/d500/d500-color.cpp +++ b/src/ds/d500/d500-color.cpp @@ -94,10 +94,9 @@ namespace librealsense void d500_color::init() { auto& color_ep = get_color_sensor(); - auto& raw_color_ep = get_raw_color_sensor(); + auto raw_color_ep = get_raw_color_sensor(); - _ds_color_common = std::make_shared(raw_color_ep, color_ep, - _fw_version, _hw_monitor, this); + _ds_color_common = std::make_shared(raw_color_ep, color_ep, _fw_version, _hw_monitor, this); register_color_features(); register_options(); @@ -134,7 +133,7 @@ namespace librealsense void d500_color::register_options() { auto& color_ep = get_color_sensor(); - auto& raw_color_ep = get_raw_color_sensor(); + auto raw_color_ep = get_raw_color_sensor(); _ds_color_common->register_color_options(); diff --git a/src/ds/d500/d500-color.h b/src/ds/d500/d500-color.h index d5ad00d595f..619fd96b572 100644 --- a/src/ds/d500/d500-color.h +++ b/src/ds/d500/d500-color.h @@ -25,10 +25,10 @@ namespace librealsense return dynamic_cast(get_sensor(_color_device_idx)); } - uvc_sensor& get_raw_color_sensor() + std::shared_ptr< uvc_sensor > get_raw_color_sensor() { - synthetic_sensor& color_sensor = get_color_sensor(); - return dynamic_cast(*color_sensor.get_raw_sensor()); + synthetic_sensor & color_sensor = get_color_sensor(); + return std::dynamic_pointer_cast< uvc_sensor >( color_sensor.get_raw_sensor() ); } protected: diff --git a/src/ds/d500/d500-device.cpp b/src/ds/d500/d500-device.cpp index 7a071114838..104731376bd 100644 --- a/src/ds/d500/d500-device.cpp +++ b/src/ds/d500/d500-device.cpp @@ -396,7 +396,7 @@ namespace librealsense { using namespace ds; - auto& raw_sensor = get_raw_depth_sensor(); + auto raw_sensor = get_raw_depth_sensor(); _pid = group.uvc_devices.front().pid; _color_calib_table_raw = [this]() @@ -409,7 +409,7 @@ namespace librealsense _hw_monitor = std::make_shared( std::make_shared( std::make_shared( - raw_sensor, depth_xu, DS5_HWMONITOR), + *raw_sensor, depth_xu, DS5_HWMONITOR), raw_sensor)); } else @@ -447,7 +447,7 @@ namespace librealsense std::vector gvd_buff(HW_MONITOR_BUFFER_SIZE); auto& depth_sensor = get_depth_sensor(); - auto& raw_depth_sensor = get_raw_depth_sensor(); + auto raw_depth_sensor = get_raw_depth_sensor(); using namespace platform; @@ -467,7 +467,7 @@ namespace librealsense auto _usb_mode = usb3_type; usb_type_str = usb_spec_names.at(_usb_mode); - _usb_mode = raw_depth_sensor.get_usb_specification(); + _usb_mode = raw_depth_sensor->get_usb_specification(); if (usb_spec_names.count(_usb_mode) && (usb_undefined != _usb_mode)) usb_type_str = usb_spec_names.at(_usb_mode); else // Backend fails to provide USB descriptor - occurs with RS3 build. Requires further work @@ -576,7 +576,7 @@ namespace librealsense if ((_device_capabilities & ds_caps::CAP_GLOBAL_SHUTTER) == ds_caps::CAP_GLOBAL_SHUTTER) { - auto emitter_always_on_opt = std::make_shared(*_hw_monitor, &depth_sensor); + auto emitter_always_on_opt = std::make_shared( *_hw_monitor ); depth_sensor.register_option(RS2_OPTION_EMITTER_ALWAYS_ON,emitter_always_on_opt); } @@ -609,15 +609,15 @@ namespace librealsense } depth_sensor.register_option(RS2_OPTION_SOC_PVT_TEMPERATURE, - std::make_shared(_hw_monitor, &raw_depth_sensor, + std::make_shared(_hw_monitor, temperature_option::temperature_component::HKR_PVT, "Temperature reading for SOC PVT")); depth_sensor.register_option(RS2_OPTION_OHM_TEMPERATURE, - std::make_shared(_hw_monitor, &raw_depth_sensor, + std::make_shared(_hw_monitor, temperature_option::temperature_component::LEFT_IR, "Temperature reading for Left Infrared Sensor")); depth_sensor.register_option(RS2_OPTION_PROJECTOR_TEMPERATURE, - std::make_shared(_hw_monitor, &raw_depth_sensor, + std::make_shared(_hw_monitor, temperature_option::temperature_component::LEFT_PROJ, "Temperature reading for Left Projector")); // Metadata registration diff --git a/src/ds/d500/d500-device.h b/src/ds/d500/d500-device.h index c68803e5b03..b9d902a7f7f 100644 --- a/src/ds/d500/d500-device.h +++ b/src/ds/d500/d500-device.h @@ -48,10 +48,10 @@ namespace librealsense return dynamic_cast(get_sensor(_depth_device_idx)); } - uvc_sensor& get_raw_depth_sensor() + std::shared_ptr< uvc_sensor > get_raw_depth_sensor() { - synthetic_sensor& depth_sensor = get_depth_sensor(); - return dynamic_cast(*depth_sensor.get_raw_sensor()); + synthetic_sensor & depth_sensor = get_depth_sensor(); + return std::dynamic_pointer_cast< uvc_sensor >( depth_sensor.get_raw_sensor() ); } d500_device( std::shared_ptr< const d500_info > const & ); diff --git a/src/ds/d500/d500-options.cpp b/src/ds/d500/d500-options.cpp index 729cc27e9ee..57ee6e8bc57 100644 --- a/src/ds/d500/d500-options.cpp +++ b/src/ds/d500/d500-options.cpp @@ -6,9 +6,12 @@ namespace librealsense { - temperature_option::temperature_option(std::shared_ptr hwm, sensor_base* ep, - temperature_component component, const char* description) - : _hwm(hwm), _sensor(ep), _component(component), _description(description) + temperature_option::temperature_option( std::shared_ptr< hw_monitor > hwm, + temperature_component component, + const char * description ) + : _hwm( hwm ) + , _component( component ) + , _description( description ) { _range = [this]() { diff --git a/src/ds/d500/d500-options.h b/src/ds/d500/d500-options.h index 1d1eef6e6ae..698166e128e 100644 --- a/src/ds/d500/d500-options.h +++ b/src/ds/d500/d500-options.h @@ -15,7 +15,7 @@ namespace librealsense class rgb_tnr_option : public option { public: - rgb_tnr_option(std::shared_ptr hwm, sensor_base* ep); + rgb_tnr_option(std::shared_ptr hwm, const std::weak_ptr< sensor_base > & ep); virtual ~rgb_tnr_option() = default; virtual void set(float value) override; virtual float query() const override; @@ -34,7 +34,7 @@ namespace librealsense std::function _record_action = [](const option&) {}; rsutils::lazy< option_range > _range; std::shared_ptr _hwm; - sensor_base* _sensor; + std::weak_ptr< sensor_base > _sensor; }; class temperature_option : public readonly_option @@ -53,8 +53,9 @@ namespace librealsense SMCU, COUNT }; - explicit temperature_option(std::shared_ptr hwm, sensor_base* ep, - temperature_component component, const char* description); + explicit temperature_option( std::shared_ptr< hw_monitor > hwm, + temperature_component component, + const char * description ); float query() const override; inline option_range get_range() const override { return *_range; } inline bool is_enabled() const override { return true; } @@ -70,7 +71,6 @@ namespace librealsense std::function _record_action = [](const option&) {}; rsutils::lazy< option_range > _range; std::shared_ptr _hwm; - sensor_base* _sensor; temperature_component _component; const char* _description; }; diff --git a/src/ds/ds-active-common.cpp b/src/ds/ds-active-common.cpp index c959dc5c60e..25c76854dde 100644 --- a/src/ds/ds-active-common.cpp +++ b/src/ds/ds-active-common.cpp @@ -11,18 +11,20 @@ namespace librealsense { using namespace ds; - ds_active_common::ds_active_common(uvc_sensor& raw_depth_ep, - synthetic_sensor& depth_ep, - backend_device* owner, - ds_caps device_capabilities, - std::shared_ptr hw_monitor, - firmware_version fw_version) : - _raw_depth_ep(raw_depth_ep), - _depth_ep(depth_ep), - _owner(owner), - _device_capabilities(device_capabilities), - _hw_monitor(hw_monitor), - _fw_version(fw_version) {} + ds_active_common::ds_active_common( const std::shared_ptr< uvc_sensor > & raw_depth_ep, + synthetic_sensor & depth_ep, + backend_device * owner, + ds_caps device_capabilities, + std::shared_ptr< hw_monitor > hw_monitor, + firmware_version fw_version ) + : _raw_depth_ep( raw_depth_ep ) + , _depth_ep( depth_ep ) + , _owner( owner ) + , _device_capabilities( device_capabilities ) + , _hw_monitor( hw_monitor ) + , _fw_version( fw_version ) + { + } void ds_active_common::register_options() { diff --git a/src/ds/ds-active-common.h b/src/ds/ds-active-common.h index 8b9ee8df3a8..d61069e74d1 100644 --- a/src/ds/ds-active-common.h +++ b/src/ds/ds-active-common.h @@ -15,16 +15,16 @@ namespace librealsense class ds_active_common { public: - ds_active_common(uvc_sensor& raw_depth_ep, - synthetic_sensor& depth_ep, - backend_device* owner, - ds::ds_caps device_capabilities, - std::shared_ptr hw_monitor, - firmware_version firmware_version); + ds_active_common( const std::shared_ptr< uvc_sensor > & raw_depth_ep, + synthetic_sensor & depth_ep, + backend_device * owner, + ds::ds_caps device_capabilities, + std::shared_ptr< hw_monitor > hw_monitor, + firmware_version firmware_version ); void register_options(); private: - uvc_sensor& _raw_depth_ep; + std::shared_ptr< uvc_sensor > _raw_depth_ep; synthetic_sensor& _depth_ep; backend_device* _owner; ds::ds_caps _device_capabilities; diff --git a/src/ds/ds-color-common.cpp b/src/ds/ds-color-common.cpp index ad1838c4f66..84cf3e7eebc 100644 --- a/src/ds/ds-color-common.cpp +++ b/src/ds/ds-color-common.cpp @@ -14,15 +14,18 @@ namespace librealsense { using namespace ds; - ds_color_common::ds_color_common(uvc_sensor& raw_color_ep, - synthetic_sensor& color_ep, - firmware_version fw_version, - std::shared_ptr hw_monitor, device* owner) : - _raw_color_ep(raw_color_ep), - _color_ep(color_ep), - _fw_version(fw_version), - _hw_monitor(hw_monitor), - _owner(owner) {} + ds_color_common::ds_color_common( const std::shared_ptr< uvc_sensor > & raw_color_ep, + synthetic_sensor & color_ep, + firmware_version fw_version, + std::shared_ptr< hw_monitor > hw_monitor, + device * owner ) + : _raw_color_ep( raw_color_ep ) + , _color_ep( color_ep ) + , _fw_version( fw_version ) + , _hw_monitor( hw_monitor ) + , _owner( owner ) + { + } void ds_color_common::register_color_options() { diff --git a/src/ds/ds-color-common.h b/src/ds/ds-color-common.h index a001c020898..0c0063c0a16 100644 --- a/src/ds/ds-color-common.h +++ b/src/ds/ds-color-common.h @@ -12,7 +12,7 @@ namespace librealsense class ds_color_common { public: - ds_color_common(uvc_sensor& raw_color_ep, + ds_color_common( const std::shared_ptr< uvc_sensor > & raw_color_ep, synthetic_sensor& color_ep, firmware_version fw_version, std::shared_ptr hw_monitor, @@ -22,7 +22,7 @@ namespace librealsense void register_metadata(); private: - uvc_sensor& _raw_color_ep; + std::shared_ptr< uvc_sensor > _raw_color_ep; synthetic_sensor& _color_ep; firmware_version _fw_version; std::shared_ptr _hw_monitor; diff --git a/src/ds/ds-device-common.cpp b/src/ds/ds-device-common.cpp index ab18bec1741..d40e6cd4b8c 100644 --- a/src/ds/ds-device-common.cpp +++ b/src/ds/ds-device-common.cpp @@ -106,13 +106,13 @@ namespace librealsense } } - uvc_sensor& ds_device_common::get_raw_depth_sensor() + std::shared_ptr< uvc_sensor > ds_device_common::get_raw_depth_sensor() { - if (auto dev = dynamic_cast(_owner)) + if( auto dev = dynamic_cast< d400_device * >( _owner ) ) return dev->get_raw_depth_sensor(); - if (auto dev = dynamic_cast(_owner)) + if( auto dev = dynamic_cast< d500_device * >( _owner ) ) return dev->get_raw_depth_sensor(); - throw std::runtime_error("device not referenced in the product line"); + throw std::runtime_error( "device not referenced in the product line" ); } bool ds_device_common::is_locked( const uint8_t * gvd_buff, uint32_t offset ) @@ -138,8 +138,8 @@ namespace librealsense flash.reserve(flash_size); LOG_DEBUG("Flash backup started..."); - uvc_sensor& raw_depth_sensor = get_raw_depth_sensor(); - raw_depth_sensor.invoke_powered([&](platform::uvc_device& dev) + std::shared_ptr< uvc_sensor > raw_depth_sensor = get_raw_depth_sensor(); + raw_depth_sensor->invoke_powered([&](platform::uvc_device& dev) { for (int i = 0; i < max_iterations; i++) { @@ -269,8 +269,8 @@ namespace librealsense if (_is_locked) throw std::runtime_error("this camera is locked and doesn't allow direct flash write, for firmware update use rs2_update_firmware method (DFU)"); - auto& raw_depth_sensor = get_raw_depth_sensor(); - raw_depth_sensor.invoke_powered([&](platform::uvc_device& dev) + auto raw_depth_sensor = get_raw_depth_sensor(); + raw_depth_sensor->invoke_powered([&](platform::uvc_device& dev) { command cmdPFD(ds::PFD); cmdPFD.require_response = false; diff --git a/src/ds/ds-device-common.h b/src/ds/ds-device-common.h index 1526b00e149..3fbbf8680ad 100644 --- a/src/ds/ds-device-common.h +++ b/src/ds/ds-device-common.h @@ -42,7 +42,7 @@ namespace librealsense void get_fw_details( const std::vector &gvd_buff, std::string& optic_serial, std::string& asic_serial, std::string& fwv ) const; private: - uvc_sensor& get_raw_depth_sensor(); + std::shared_ptr< uvc_sensor > get_raw_depth_sensor(); device* _owner; std::shared_ptr _hw_monitor; diff --git a/src/ds/ds-motion-common.cpp b/src/ds/ds-motion-common.cpp index 6edbdbecf7e..740ecf05fbc 100644 --- a/src/ds/ds-motion-common.cpp +++ b/src/ds/ds-motion-common.cpp @@ -211,9 +211,9 @@ namespace librealsense std::shared_ptr ds_motion_common::register_auto_exposure_options(synthetic_sensor* ep, const platform::extension_unit* fisheye_xu) { auto uvc_raw_sensor = As(ep->get_raw_sensor()); - auto gain_option = std::make_shared(*uvc_raw_sensor, RS2_OPTION_GAIN); + auto gain_option = std::make_shared(uvc_raw_sensor, RS2_OPTION_GAIN); - auto exposure_option = std::make_shared>(*uvc_raw_sensor, + auto exposure_option = std::make_shared>(uvc_raw_sensor, *fisheye_xu, librealsense::ds::FISHEYE_EXPOSURE, "Exposure time of Fisheye camera"); @@ -393,10 +393,10 @@ namespace librealsense else { _fisheye_ep->register_option(RS2_OPTION_GAIN, - std::make_shared(*_raw_fisheye_ep.get(), + std::make_shared(_raw_fisheye_ep, RS2_OPTION_GAIN)); _fisheye_ep->register_option(RS2_OPTION_EXPOSURE, - std::make_shared>(*_raw_fisheye_ep.get(), + std::make_shared>(_raw_fisheye_ep, ds::fisheye_xu, librealsense::ds::FISHEYE_EXPOSURE, "Exposure time of Fisheye camera")); diff --git a/src/ds/ds-options.cpp b/src/ds/ds-options.cpp index 6941155a58c..7890c1641af 100644 --- a/src/ds/ds-options.cpp +++ b/src/ds/ds-options.cpp @@ -33,7 +33,7 @@ namespace librealsense } } - emitter_option::emitter_option(uvc_sensor& ep) + emitter_option::emitter_option( const std::weak_ptr< uvc_sensor > & ep ) : uvc_xu_option(ep, ds::depth_xu, ds::DS5_DEPTH_EMITTER_ENABLED, "Emitter select, 0-disable all emitters, 1-enable laser, 2-enable laser auto (opt), 3-enable LED (opt)") {} @@ -126,7 +126,7 @@ namespace librealsense } } - asic_and_projector_temperature_options::asic_and_projector_temperature_options(std::shared_ptr && ep, rs2_option opt) + asic_and_projector_temperature_options::asic_and_projector_temperature_options( const std::weak_ptr & ep, rs2_option opt) : _option(opt), _ep(std::move(ep)) {} @@ -386,7 +386,7 @@ namespace librealsense return *_range; } - external_sync_mode::external_sync_mode(hw_monitor& hwm, sensor_base* ep, int ver) + external_sync_mode::external_sync_mode( hw_monitor & hwm, const std::weak_ptr< sensor_base > & ep, int ver ) : _hwm(hwm), _sensor(ep), _ver(ver) { _range = [this]() @@ -407,7 +407,11 @@ namespace librealsense } else { - if (_sensor->is_streaming()) + auto strong = _sensor.lock(); + if( ! strong ) + throw std::runtime_error( "Cannot set Inter-camera HW synchronization, sensor is not alive" ); + + if( strong->is_streaming() ) throw std::runtime_error("Cannot change Inter-camera HW synchronization mode while streaming!"); if (value < 4) @@ -457,7 +461,13 @@ namespace librealsense return *_range; } - emitter_on_and_off_option::emitter_on_and_off_option(hw_monitor& hwm, sensor_base* ep) + bool external_sync_mode::is_read_only() const + { + auto strong = _sensor.lock(); + return strong && strong->is_opened(); + } + + emitter_on_and_off_option::emitter_on_and_off_option( hw_monitor & hwm, const std::weak_ptr< sensor_base > & ep ) : _hwm(hwm), _sensor(ep) { _range = [this]() @@ -468,7 +478,11 @@ namespace librealsense void emitter_on_and_off_option::set(float value) { - if (_sensor->is_streaming()) + auto strong = _sensor.lock(); + if( ! strong ) + throw std::runtime_error( "Cannot set Emitter On/Off option, sensor is not alive" ); + + if( strong->is_streaming() ) throw std::runtime_error("Cannot change Emitter On/Off option while streaming!"); command cmd(ds::SET_PWM_ON_OFF); @@ -503,8 +517,8 @@ namespace librealsense return "Inter-camera synchronization mode: 0:Default, 1:Master, 2:Slave"; } - alternating_emitter_option::alternating_emitter_option(hw_monitor& hwm, sensor_base* ep, bool is_fw_version_using_id) - : _hwm(hwm), _sensor(ep), _is_fw_version_using_id(is_fw_version_using_id) + alternating_emitter_option::alternating_emitter_option(hw_monitor& hwm, bool is_fw_version_using_id) + : _hwm(hwm), _is_fw_version_using_id(is_fw_version_using_id) { _range = [this]() { @@ -562,8 +576,8 @@ namespace librealsense } } - emitter_always_on_option::emitter_always_on_option(hw_monitor& hwm, sensor_base* ep) - : _hwm(hwm), _sensor(ep) + emitter_always_on_option::emitter_always_on_option( hw_monitor & hwm ) + : _hwm(hwm) { _range = [this]() { diff --git a/src/ds/ds-options.h b/src/ds/ds-options.h index b599cba1ecc..627cc629b76 100644 --- a/src/ds/ds-options.h +++ b/src/ds/ds-options.h @@ -17,7 +17,7 @@ namespace librealsense { public: const char* get_value_description(float val) const override; - explicit emitter_option(uvc_sensor& ep); + explicit emitter_option( const std::weak_ptr< uvc_sensor > & ep ); }; class asic_and_projector_temperature_options : public readonly_option @@ -31,7 +31,7 @@ namespace librealsense const char* get_description() const override; - explicit asic_and_projector_temperature_options(std::shared_ptr && ep, rs2_option opt); + explicit asic_and_projector_temperature_options( const std::weak_ptr< uvc_sensor > & ep, rs2_option opt ); private: std::weak_ptr _ep; @@ -193,16 +193,18 @@ namespace librealsense class external_sync_mode : public option { public: - external_sync_mode(hw_monitor& hwm, sensor_base* depth_ep = nullptr, int ver = 1); // ver = 1, for firmware 5.9.15.1 and later, INTERCAM_SYNC_MAX is 2 with master and slave mode only. - // ver = 2, for firmware 5.12.4.0 and later, INTERCAM_SYNC_MAX is 258 by adding FULL SLAVE mode and genlock with trigger frequency 1 - 255. - // ver = 3 for firmware 5.12.12.100 and later, INTERCAM_SYNC_MAX is 260 by adding genlock with laser on-off and Off-On two frames. + external_sync_mode( hw_monitor & hwm, + const std::weak_ptr< sensor_base > & depth_ep = std::weak_ptr< sensor_base >(), + int ver = 1 ); // ver = 1, for firmware 5.9.15.1 and later, INTERCAM_SYNC_MAX is 2 with master and slave mode only. + // ver = 2, for firmware 5.12.4.0 and later, INTERCAM_SYNC_MAX is 258 by adding FULL SLAVE mode and genlock with trigger frequency 1 - 255. + // ver = 3 for firmware 5.12.12.100 and later, INTERCAM_SYNC_MAX is 260 by adding genlock with laser on-off and Off-On two frames. virtual ~external_sync_mode() = default; virtual void set(float value) override; virtual float query() const override; virtual option_range get_range() const override; virtual bool is_enabled() const override { return true; } - virtual bool is_read_only() const override { return _sensor && _sensor->is_opened(); } + virtual bool is_read_only() const override; const char* get_description() const override; void enable_recording(std::function record_action) override @@ -213,14 +215,14 @@ namespace librealsense std::function _record_action = [](const option&) {}; rsutils::lazy< option_range > _range; hw_monitor& _hwm; - sensor_base* _sensor; + std::weak_ptr< sensor_base > _sensor; int _ver; }; class emitter_on_and_off_option : public option { public: - emitter_on_and_off_option(hw_monitor& hwm, sensor_base* depth_ep); + emitter_on_and_off_option( hw_monitor & hwm, const std::weak_ptr< sensor_base > & depth_ep ); virtual ~emitter_on_and_off_option() = default; virtual void set(float value) override; virtual float query() const override; @@ -236,13 +238,13 @@ namespace librealsense std::function _record_action = [](const option&) {}; rsutils::lazy< option_range > _range; hw_monitor& _hwm; - sensor_base* _sensor; + std::weak_ptr< sensor_base > _sensor; }; class alternating_emitter_option : public option { public: - alternating_emitter_option(hw_monitor& hwm, sensor_base* depth_ep, bool is_fw_version_using_id); + alternating_emitter_option(hw_monitor& hwm, bool is_fw_version_using_id); virtual ~alternating_emitter_option() = default; virtual void set(float value) override; virtual float query() const override; @@ -258,14 +260,13 @@ namespace librealsense std::function _record_action = [](const option&) {}; rsutils::lazy< option_range > _range; hw_monitor& _hwm; - sensor_base* _sensor; bool _is_fw_version_using_id; }; class emitter_always_on_option : public option { public: - emitter_always_on_option(hw_monitor& hwm, sensor_base* depth_ep); + emitter_always_on_option( hw_monitor & hwm ); virtual ~emitter_always_on_option() = default; virtual void set(float value) override; virtual float query() const override; @@ -281,7 +282,6 @@ namespace librealsense std::function _record_action = [](const option&) {}; rsutils::lazy< option_range > _range; hw_monitor& _hwm; - sensor_base* _sensor; }; class hdr_option : public option diff --git a/src/ds/features/emitter-frequency-feature.cpp b/src/ds/features/emitter-frequency-feature.cpp index 83778455457..cee83a6e1c0 100644 --- a/src/ds/features/emitter-frequency-feature.cpp +++ b/src/ds/features/emitter-frequency-feature.cpp @@ -15,7 +15,7 @@ namespace librealsense { emitter_frequency_feature::emitter_frequency_feature( synthetic_sensor & sensor ) { auto emitter_freq_option = std::make_shared< uvc_xu_option< uint16_t > >( - dynamic_cast< uvc_sensor & >( *sensor.get_raw_sensor() ), + std::dynamic_pointer_cast< uvc_sensor >( sensor.get_raw_sensor() ), ds::depth_xu, ds::DS5_EMITTER_FREQUENCY, "Controls the emitter frequency, 57 [KHZ] / 91 [KHZ]", diff --git a/src/hw-monitor.h b/src/hw-monitor.h index ef5538e9e7b..d416f282a5b 100644 --- a/src/hw-monitor.h +++ b/src/hw-monitor.h @@ -198,7 +198,7 @@ namespace librealsense class locked_transfer { public: - locked_transfer(std::shared_ptr command_transfer, uvc_sensor& uvc_ep) + locked_transfer(std::shared_ptr command_transfer, std::shared_ptr< uvc_sensor> uvc_ep) :_command_transfer(command_transfer), _uvc_sensor_base(uvc_ep) {} @@ -215,8 +215,11 @@ namespace librealsense if( !token.get() ) throw io_exception( "heap allocation failed" ); std::lock_guard lock(_local_mtx); - return _uvc_sensor_base.invoke_powered([&] - (platform::uvc_device& dev) + auto strong_uvc = _uvc_sensor_base.lock(); + if( ! strong_uvc ) + return std::vector< uint8_t >(); + + return strong_uvc->invoke_powered([&] ( platform::uvc_device & dev ) { std::lock_guard lock(dev); return _command_transfer->send_receive(data, timeout_ms, require_response); @@ -236,7 +239,7 @@ namespace librealsense } private: std::shared_ptr _command_transfer; - uvc_sensor& _uvc_sensor_base; + std::weak_ptr< uvc_sensor> _uvc_sensor_base; std::recursive_mutex _local_mtx; small_heap _heap; }; diff --git a/src/platform/uvc-option.cpp b/src/platform/uvc-option.cpp index 1778a1eefe1..eec641b5fa7 100644 --- a/src/platform/uvc-option.cpp +++ b/src/platform/uvc-option.cpp @@ -8,19 +8,23 @@ namespace librealsense { -uvc_pu_option::uvc_pu_option(uvc_sensor& ep, rs2_option id) +uvc_pu_option::uvc_pu_option( const std::weak_ptr< uvc_sensor > & ep, rs2_option id ) : uvc_pu_option(ep, id, std::map()) { } -uvc_pu_option::uvc_pu_option(uvc_sensor& ep, rs2_option id, const std::map& description_per_value) +uvc_pu_option::uvc_pu_option( const std::weak_ptr< uvc_sensor > & ep, rs2_option id, + const std::map< float, std::string > & description_per_value ) : _ep(ep), _id(id), _description_per_value(description_per_value) { _range = [this]() { - auto uvc_range = _ep.invoke_powered( - [this](platform::uvc_device& dev) + auto ep = _ep.lock(); + if( ! ep ) + throw invalid_value_exception( "Cannot set range, UVC sensor is not alive" ); + + auto uvc_range = ep->invoke_powered( [this]( platform::uvc_device & dev ) { return dev.get_pu_range(_id); }); @@ -41,7 +45,10 @@ uvc_pu_option::uvc_pu_option(uvc_sensor& ep, rs2_option id, const std::mapinvoke_powered( [this, value](platform::uvc_device& dev) { if (!dev.set_pu(_id, static_cast(value))) @@ -55,7 +62,10 @@ void uvc_pu_option::set(float value) float uvc_pu_option::query() const { - return static_cast(_ep.invoke_powered( + auto ep = _ep.lock(); + if( ! ep ) + throw invalid_value_exception( "Cannot query option, UVC sensor is not alive" ); + return static_cast(ep->invoke_powered( [this](platform::uvc_device& dev) { int32_t value = 0; @@ -93,47 +103,54 @@ const char* uvc_pu_option::get_description() const case RS2_OPTION_ENABLE_AUTO_WHITE_BALANCE: return "Enable / disable auto-white-balance"; case RS2_OPTION_POWER_LINE_FREQUENCY: return "Power Line Frequency"; case RS2_OPTION_AUTO_EXPOSURE_PRIORITY: return "Restrict Auto-Exposure to enforce constant FPS rate. Turn ON to remove the restrictions (may result in FPS drop)"; - default: return _ep.get_option_name( _id ).c_str(); + default: + auto ep = _ep.lock(); + if( ! ep ) + throw invalid_value_exception( "Cannot get option name, UVC sensor is not alive" ); + + return ep->get_option_name( _id ).c_str(); } } std::vector command_transfer_over_xu::send_receive(const std::vector& data, int, bool require_response) { - return _uvc.invoke_powered([this, &data, require_response] - (platform::uvc_device& dev) + return _uvc.invoke_powered([this, &data, require_response]( platform::uvc_device & dev ) { std::vector result; - std::lock_guard lock(dev); - if (data.size() > HW_MONITOR_BUFFER_SIZE) + std::lock_guard< platform::uvc_device > lock(dev); + + if( data.size() > HW_MONITOR_BUFFER_SIZE ) { - LOG_ERROR("XU command size is invalid"); + LOG_ERROR( "XU command size is invalid" ); throw invalid_value_exception( rsutils::string::from() << "Requested XU command size " << std::dec << data.size() << " exceeds permitted limit " << HW_MONITOR_BUFFER_SIZE ); } - std::vector transmit_buf(HW_MONITOR_BUFFER_SIZE, 0); - std::copy(data.begin(), data.end(), transmit_buf.begin()); + std::vector< uint8_t > transmit_buf( HW_MONITOR_BUFFER_SIZE, 0 ); + std::copy( data.begin(), data.end(), transmit_buf.begin() ); - if (!dev.set_xu(_xu, _ctrl, transmit_buf.data(), static_cast(transmit_buf.size()))) + if( ! dev.set_xu( _xu, _ctrl, transmit_buf.data(), static_cast< int >( transmit_buf.size() ) ) ) throw invalid_value_exception( rsutils::string::from() << "set_xu(ctrl=" << unsigned( _ctrl ) << ") failed!" << " Last Error: " << strerror( errno ) ); - if (require_response) + if( require_response ) { - result.resize(HW_MONITOR_BUFFER_SIZE); - if (!dev.get_xu(_xu, _ctrl, result.data(), static_cast(result.size()))) + result.resize( HW_MONITOR_BUFFER_SIZE ); + if( ! dev.get_xu( _xu, _ctrl, result.data(), static_cast< int >( result.size() ) ) ) throw invalid_value_exception( rsutils::string::from() << "get_xu(ctrl=" << unsigned( _ctrl ) << ") failed!" << " Last Error: " << strerror( errno ) ); // Returned data size located in the last 4 bytes - auto data_size = *(reinterpret_cast(result.data() + HW_MONITOR_DATA_SIZE_OFFSET)) + SIZE_OF_HW_MONITOR_HEADER; - result.resize(data_size); + auto data_size = *( reinterpret_cast< uint32_t * >( result.data() + HW_MONITOR_DATA_SIZE_OFFSET ) ) + + SIZE_OF_HW_MONITOR_HEADER; + result.resize( data_size ); } + return result; }); } diff --git a/src/platform/uvc-option.h b/src/platform/uvc-option.h index b0b7ccb4340..58b7292fccc 100644 --- a/src/platform/uvc-option.h +++ b/src/platform/uvc-option.h @@ -15,7 +15,7 @@ namespace librealsense { class uvc_pu_option : public option { - uvc_sensor & _ep; + std::weak_ptr< uvc_sensor > _ep; rs2_option _id; const std::map< float, std::string > _description_per_value; std::function< void( const option & ) > _record = []( const option & ) {}; @@ -30,9 +30,10 @@ class uvc_pu_option : public option bool is_enabled() const override { return true; } - uvc_pu_option( uvc_sensor & ep, rs2_option id ); + uvc_pu_option( const std::weak_ptr< uvc_sensor > & ep, rs2_option id ); - uvc_pu_option( uvc_sensor & ep, rs2_option id, const std::map< float, std::string > & description_per_value ); + uvc_pu_option( const std::weak_ptr < uvc_sensor > & ep, rs2_option id, + const std::map< float, std::string > & description_per_value ); const char * get_description() const override; @@ -53,11 +54,14 @@ class uvc_xu_option : public option public: void set( float value ) override { - if( ! _allow_set_while_streaming && _ep.is_streaming() ) - throw invalid_value_exception( rsutils::string::from() - << "setting this option during streaming is not allowed!" ); + auto ep = _ep.lock(); + if( ! ep ) + throw invalid_value_exception( "UVC sensor is not alive for setting" ); - _ep.invoke_powered( + if( ! _allow_set_while_streaming && ep->is_streaming() ) + throw invalid_value_exception( "setting this option during streaming is not allowed!" ); + + ep->invoke_powered( [this, value]( platform::uvc_device & dev ) { T t = static_cast< T >( value ); @@ -71,7 +75,11 @@ class uvc_xu_option : public option float query() const override { - return static_cast< float >( _ep.invoke_powered( + auto ep = _ep.lock(); + if( ! ep ) + return static_cast< float >( T() ); + + return static_cast< float >( ep->invoke_powered( [this]( platform::uvc_device & dev ) { T t; @@ -86,7 +94,11 @@ class uvc_xu_option : public option option_range get_range() const override { - auto uvc_range = _ep.invoke_powered( [this]( platform::uvc_device & dev ) + auto uvc_range = platform::control_range(); + + auto ep = _ep.lock(); + if( ep ) + uvc_range = ep->invoke_powered( [this]( platform::uvc_device & dev ) { return dev.get_xu_range( _xu, _id, sizeof( T ) ); } ); if( uvc_range.min.size() < sizeof( int32_t ) ) @@ -104,7 +116,7 @@ class uvc_xu_option : public option bool is_enabled() const override { return true; } - uvc_xu_option( uvc_sensor & ep, + uvc_xu_option( const std::weak_ptr< uvc_sensor > & ep, platform::extension_unit xu, uint8_t id, std::string description, @@ -117,7 +129,7 @@ class uvc_xu_option : public option { } - uvc_xu_option( uvc_sensor & ep, + uvc_xu_option( const std::weak_ptr< uvc_sensor > & ep, platform::extension_unit xu, uint8_t id, std::string description, @@ -145,7 +157,7 @@ class uvc_xu_option : public option } protected: - uvc_sensor & _ep; + std::weak_ptr < uvc_sensor > _ep; platform::extension_unit _xu; uint8_t _id; std::string _desciption; @@ -160,12 +172,15 @@ template< typename T > class protected_xu_option : public uvc_xu_option< T > { public: - protected_xu_option( uvc_sensor & ep, platform::extension_unit xu, uint8_t id, std::string description ) + protected_xu_option( const std::weak_ptr< uvc_sensor > & ep, + platform::extension_unit xu, + uint8_t id, + std::string description ) : uvc_xu_option< T >( ep, xu, id, description ) { } - protected_xu_option( uvc_sensor & ep, + protected_xu_option( const std::weak_ptr< uvc_sensor > & ep, platform::extension_unit xu, uint8_t id, std::string description, @@ -195,7 +210,7 @@ template< typename T > class ensure_set_xu_option : public uvc_xu_option< T > { public: - ensure_set_xu_option( uvc_sensor & ep, + ensure_set_xu_option( const std::weak_ptr< uvc_sensor > & ep, platform::extension_unit xu, uint8_t id, std::string description, @@ -206,7 +221,7 @@ class ensure_set_xu_option : public uvc_xu_option< T > { } - ensure_set_xu_option( uvc_sensor & ep, + ensure_set_xu_option( const std::weak_ptr< uvc_sensor > & ep, platform::extension_unit xu, uint8_t id, std::string description, diff --git a/src/sensor.cpp b/src/sensor.cpp index 37342131b0b..409f72daad2 100644 --- a/src/sensor.cpp +++ b/src/sensor.cpp @@ -493,14 +493,14 @@ void log_callback_end( uint32_t fps, void synthetic_sensor::register_pu(rs2_option id) { - const auto&& raw_uvc_sensor = As(_raw_sensor); - register_option(id, std::make_shared(*raw_uvc_sensor.get(), id)); + const auto raw_uvc_sensor = As(_raw_sensor); + register_option(id, std::make_shared(raw_uvc_sensor, id)); } bool synthetic_sensor::try_register_pu(rs2_option id) { - const auto&& raw_uvc_sensor = As(_raw_sensor); - return try_register_option(id, std::make_shared(*raw_uvc_sensor.get(), id)); + const auto raw_uvc_sensor = As(_raw_sensor); + return try_register_option(id, std::make_shared(raw_uvc_sensor, id)); } void sensor_base::sort_profiles( stream_profiles & profiles ) diff --git a/src/syntethic-options-watcher.h b/src/syntethic-options-watcher.h deleted file mode 100644 index a2c028395f1..00000000000 --- a/src/syntethic-options-watcher.h +++ /dev/null @@ -1,26 +0,0 @@ -// License: Apache 2.0. See LICENSE file in root directory. -// Copyright(c) 2023 Intel Corporation. All Rights Reserved. -#pragma once - - -#include - - -namespace librealsense { - -class raw_sensor_base; - -// Used by synthetic sensor and uses the raw_sensor bulk operations. -class synthetic_options_watcher : public options_watcher -{ -public: - synthetic_options_watcher( const std::shared_ptr< raw_sensor_base > & raw_sensor ); - -protected: - std::map< rs2_option, std::shared_ptr< option > > update_options() override; - - std::weak_ptr< raw_sensor_base > _raw_sensor; -}; - - -} // namespace librealsense diff --git a/src/uvc-sensor.cpp b/src/uvc-sensor.cpp index 6041cd20b97..451dfb4c276 100644 --- a/src/uvc-sensor.cpp +++ b/src/uvc-sensor.cpp @@ -339,7 +339,7 @@ void uvc_sensor::close() void uvc_sensor::register_pu( rs2_option id ) { - register_option( id, std::make_shared< uvc_pu_option >( *this, id ) ); + register_option( id, std::make_shared< uvc_pu_option >( std::dynamic_pointer_cast< uvc_sensor >( shared_from_this() ), id ) ); }