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

set_option api checking range restored #13328

Merged
merged 3 commits into from
Sep 10, 2024
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
41 changes: 40 additions & 1 deletion src/rs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -825,7 +825,46 @@ void rs2_set_option(const rs2_options* options, rs2_option option, float value,
{
VALIDATE_NOT_NULL(options);
VALIDATE_OPTION_ENABLED(options, option);
options->options->get_option(option).set( value );
auto& option_ref = options->options->get_option(option);
auto range = option_ref.get_range();
switch (option_ref.get_value_type())
{
case RS2_OPTION_TYPE_FLOAT:
if (range.min != range.max && range.step)
VALIDATE_RANGE(value, range.min, range.max);
option_ref.set(value);
break;

case RS2_OPTION_TYPE_INTEGER:
if (range.min != range.max && range.step)
VALIDATE_RANGE(value, range.min, range.max);
if ((int)value != value)
throw invalid_value_exception(rsutils::string::from() << "not an integer: " << value);
option_ref.set(value);
break;

case RS2_OPTION_TYPE_BOOLEAN:
if (value == 0.f)
option_ref.set_value(false);
else if (value == 1.f)
option_ref.set_value(true);
else
throw invalid_value_exception(rsutils::string::from() << "not a boolean: " << value);
break;

case RS2_OPTION_TYPE_STRING:
// We can convert "enum" options to a float value
if ((int)value == value && range.min == 0.f && range.step == 1.f)
{
auto desc = option_ref.get_value_description(value);
if (desc)
{
option_ref.set_value(desc);
break;
}
}
throw not_implemented_exception("use rs2_set_option_value to set string values");
}
}
HANDLE_EXCEPTIONS_AND_RETURN(, options, option, value)

Expand Down
34 changes: 34 additions & 0 deletions unit-tests/live/options/test-out-of-range-throw.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# License: Apache 2.0. See LICENSE file in root directory.
# Copyright(c) 2020 Intel Corporation. All Rights Reserved.

# test:device D400*

import pyrealsense2 as rs
from rspy import test
from rspy import tests_wrapper as tw


def check_min_max_throw(sensor):
options_to_check = [rs.option.exposure, rs.option.enable_auto_exposure]
for option in options_to_check:
if not sensor.supports(option):
continue
option_range = sensor.get_option_range(option)
# below min
test.check_throws(lambda: sensor.set_option(option, option_range.min - 1), RuntimeError,
"out of range value for argument \"value\"")
# above max
test.check_throws(lambda: sensor.set_option(option, option_range.max + 1), RuntimeError,
"out of range value for argument \"value\"")


dev, _ = test.find_first_device_or_exit()
tw.start_wrapper(dev)
with test.closure("Options out of Range throwing exception"):
sensors = dev.query_sensors()
for sensor in sensors:
check_min_max_throw(sensor)

tw.stop_wrapper(dev)

test.print_results_and_exit()
Loading