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

refactor option.h and related #12369

Merged
merged 1 commit into from
Nov 7, 2023
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: 2 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,8 @@ target_sources(${LRS_TARGET}
"${CMAKE_CURRENT_LIST_DIR}/core/time-service.h"
"${CMAKE_CURRENT_LIST_DIR}/platform/uvc-device.h"
"${CMAKE_CURRENT_LIST_DIR}/platform/uvc-device-info.h"
"${CMAKE_CURRENT_LIST_DIR}/platform/uvc-option.cpp"
"${CMAKE_CURRENT_LIST_DIR}/platform/uvc-option.h"
"${CMAKE_CURRENT_LIST_DIR}/context.h"
"${CMAKE_CURRENT_LIST_DIR}/device.h"
"${CMAKE_CURRENT_LIST_DIR}/device-info.h"
Expand Down
2 changes: 1 addition & 1 deletion src/algo.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#pragma once
#include "core/frame-holder.h"
#include "core/roi.h"
#include "core/options.h"
#include "core/options-container.h"
#include "types.h"

#include <stdint.h>
Expand Down
5 changes: 5 additions & 0 deletions src/api.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@
#include <type_traits>
#include <iostream>

namespace librealsense {
struct notification;
}


struct rs2_raw_data_buffer
{
std::vector<uint8_t> buffer;
Expand Down
5 changes: 4 additions & 1 deletion src/core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,12 @@ target_sources(${LRS_TARGET}
"${CMAKE_CURRENT_LIST_DIR}/matcher-factory.cpp"
"${CMAKE_CURRENT_LIST_DIR}/motion.h"
"${CMAKE_CURRENT_LIST_DIR}/motion-frame.h"
"${CMAKE_CURRENT_LIST_DIR}/notification.h"
"${CMAKE_CURRENT_LIST_DIR}/video.h"
"${CMAKE_CURRENT_LIST_DIR}/video-frame.h"
"${CMAKE_CURRENT_LIST_DIR}/options.h"
"${CMAKE_CURRENT_LIST_DIR}/options-container.h"
"${CMAKE_CURRENT_LIST_DIR}/options-container.cpp"
"${CMAKE_CURRENT_LIST_DIR}/options-interface.h"
"${CMAKE_CURRENT_LIST_DIR}/options-registry.h"
"${CMAKE_CURRENT_LIST_DIR}/options-registry.cpp"
"${CMAKE_CURRENT_LIST_DIR}/info.h"
Expand Down
1 change: 1 addition & 0 deletions src/core/info.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

#include "info-interface.h"

#include <librealsense2/h/rs_sensor.h>
#include <map>
#include <string>
#include <memory>
Expand Down
65 changes: 65 additions & 0 deletions src/core/notification.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// License: Apache 2.0. See LICENSE file in root directory.
// Copyright(c) 2023 Intel Corporation. All Rights Reserved.
#pragma once

#include <librealsense2/h/rs_types.h>
#include <rsutils/concurrency/concurrency.h>
#include <rsutils/easylogging/easyloggingpp.h>
#include <memory>
#include <string>


namespace librealsense {


using notifications_callback_ptr = std::shared_ptr< rs2_notifications_callback >;


struct notification
{
notification( rs2_notification_category category, int type, rs2_log_severity severity, std::string const & description )
: category( category )
, type( type )
, severity( severity )
, description( description )
{
timestamp = std::chrono::duration< double, std::milli >( std::chrono::system_clock::now().time_since_epoch() )
.count();
LOG_INFO( description );
}

rs2_notification_category category;
int type;
rs2_log_severity severity;
std::string description;
double timestamp;
std::string serialized_data;
};


class notification_decoder
{
public:
virtual ~notification_decoder() = default;
virtual notification decode( int value ) = 0;
};


class notifications_processor
{
public:
notifications_processor();
~notifications_processor();

void set_callback( notifications_callback_ptr callback );
notifications_callback_ptr get_callback() const;
void raise_notification( const notification );

private:
notifications_callback_ptr _callback;
std::mutex _callback_mutex;
dispatcher _dispatcher;
};


} // namespace librealsense
39 changes: 39 additions & 0 deletions src/core/option-interface.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// License: Apache 2.0. See LICENSE file in root directory.
// Copyright(c) 2023 Intel Corporation. All Rights Reserved.
#pragma once

#include "extension.h"

#include <src/basics.h>


namespace librealsense {


struct LRS_EXTENSION_API option_range
{
float min;
float max;
float step;
float def;
};

class LRS_EXTENSION_API option : public recordable< option >
{
public:
virtual void set( float value ) = 0;
virtual float query() const = 0;
virtual option_range get_range() const = 0;
virtual bool is_enabled() const = 0;
virtual bool is_read_only() const { return false; }
virtual const char * get_description() const = 0;
virtual const char * get_value_description( float ) const { return nullptr; }

// recordable< option >
virtual void create_snapshot( std::shared_ptr< option > & snapshot ) const override;

virtual ~option() = default;
};


} // namespace librealsense
53 changes: 53 additions & 0 deletions src/core/options-container.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// License: Apache 2.0. See LICENSE file in root directory.
// Copyright(c) 2023 Intel Corporation. All Rights Reserved.

#include "options-container.h"
#include "enum-helpers.h"
#include <src/librealsense-exception.h>
#include <rsutils/string/from.h>


namespace librealsense {


const option & options_container::get_option( rs2_option id ) const
{
auto it = _options.find( id );
if( it == _options.end() )
{
throw invalid_value_exception( rsutils::string::from()
<< "Device does not support option " << get_option_name( id ) << "!" );
}
return *it->second;
}


void options_container::update( std::shared_ptr< extension_snapshot > ext )
{
auto ctr = As< options_container >( ext );
if( ! ctr )
return;
for( auto && opt : ctr->_options )
{
_options[opt.first] = opt.second;
}
}


std::vector< rs2_option > options_container::get_supported_options() const
{
std::vector< rs2_option > options;
for( auto option : _options )
options.push_back( option.first );

return options;
}


std::string const & options_container::get_option_name( rs2_option option ) const
{
return get_string( option );
}


} // namespace librealsense
83 changes: 83 additions & 0 deletions src/core/options-container.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
// License: Apache 2.0. See LICENSE file in root directory.
// Copyright(c) 2015 Intel Corporation. All Rights Reserved.
#pragma once

#include "options-interface.h"
#include "extension.h"

#include <librealsense2/h/rs_option.h>
#include <src/basics.h>
#include "enum-helpers.h"
#include <src/librealsense-exception.h>

#include <map>
#include <vector>
#include <functional>
#include <memory>


namespace librealsense {


class LRS_EXTENSION_API options_container : public virtual options_interface, public extension_snapshot
{
public:
bool supports_option(rs2_option id) const override
{
auto it = _options.find(id);
if (it == _options.end()) return false;
return it->second->is_enabled();
}

option& get_option(rs2_option id) override
{
return const_cast<option&>(const_cast<const options_container*>(this)->get_option(id));
}

const option & get_option( rs2_option id ) const override;

std::shared_ptr<option> get_option_handler(rs2_option id)
{
return (const_cast<const options_container*>(this)->get_option_handler(id));
}

std::shared_ptr<option> get_option_handler(rs2_option id) const
{
auto it = _options.find(id);
return (it == _options.end() ? std::shared_ptr<option>(nullptr) : it->second);
}

void register_option(rs2_option id, std::shared_ptr<option> option)
{
_options[id] = option;
_recording_function(*this);
}

void unregister_option(rs2_option id)
{
_options.erase(id);
}

void create_snapshot(std::shared_ptr<options_interface>& snapshot) const override
{
snapshot = std::make_shared<options_container>(*this);
}

void enable_recording(std::function<void(const options_interface&)> record_action) override
{
_recording_function = record_action;
}

void update( std::shared_ptr<extension_snapshot> ext ) override;

std::vector<rs2_option> get_supported_options() const override;

std::string const & get_option_name( rs2_option option ) const override;

protected:
std::map<rs2_option, std::shared_ptr<option>> _options;
std::function<void(const options_interface&)> _recording_function = [](const options_interface&) {};
};


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

#pragma once

#include "option-interface.h"
#include "extension.h"

#include <librealsense2/h/rs_option.h>
#include <vector>
#include <string>


namespace librealsense {


class options_interface : public recordable< options_interface >
{
public:
virtual option & get_option( rs2_option id ) = 0;
virtual const option & get_option( rs2_option id ) const = 0;
virtual bool supports_option( rs2_option id ) const = 0;
virtual std::vector< rs2_option > get_supported_options() const = 0;
virtual std::string const & get_option_name( rs2_option ) const = 0;
virtual ~options_interface() = default;
};

MAP_EXTENSION( RS2_EXTENSION_OPTIONS, librealsense::options_interface );


} // namespace librealsense
Loading
Loading