Skip to content

Commit

Permalink
dds-adapter reads librealsense settings for defaults
Browse files Browse the repository at this point in the history
  • Loading branch information
maloel committed Nov 19, 2023
1 parent e985ab6 commit 516c1d3
Showing 1 changed file with 65 additions and 5 deletions.
70 changes: 65 additions & 5 deletions tools/dds/dds-adapter/rs-dds-adapter.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
// License: Apache 2.0. See LICENSE file in root directory.
// Copyright(c) 2022 Intel Corporation. All Rights Reserved.

#include <rsutils/easylogging/easyloggingpp.h>
#include <rsutils/json.h>

#include <realdds/dds-device-server.h>
#include <realdds/dds-stream-server.h>
#include <realdds/dds-participant.h>
Expand All @@ -17,6 +14,11 @@
#include <tclap/CmdLine.h>
#include <tclap/ValueArg.h>

#include <rsutils/os/executable-name.h>
#include <rsutils/os/special-folder.h>
#include <rsutils/easylogging/easyloggingpp.h>
#include <rsutils/json.h>

#include <string>
#include <iostream>
#include <map>
Expand Down Expand Up @@ -58,10 +60,68 @@ topics::device_info rs2_device_to_info( rs2::device const & dev )
}


static nlohmann::json load_config()
{
std::ifstream f( rsutils::os::get_special_folder( rsutils::os::special_folder::app_data ) + RS2_CONFIG_FILENAME );
if( ! f.good() )
return nlohmann::json::object();

// Load the realsense configuration file settings
try
{
return nlohmann::json::parse( f );
}
catch( std::exception const & e )
{
throw std::runtime_error( "failed to load configuration file: " + std::string( e.what() ) );
}
}


static void merge_settings( nlohmann::json & settings, nlohmann::json const & overrides, std::string const & name )
{
if( ! overrides.is_object() )
throw std::runtime_error( name + ": expecting an object; got " + overrides.dump() );
try
{
settings.merge_patch( overrides );
}
catch( std::exception const & e )
{
throw std::runtime_error( "failed to merge context settings with configuration '" + name
+ "': " + std::string( e.what() ) );
}
}


static nlohmann::json load_settings( nlohmann::json const & local_settings )
{
// Allow ignoring of any other settings, global or not!
auto config = load_config();

// Take the global 'context' settings out of the configuration
nlohmann::json settings;
if( auto global_context = rsutils::json::nested( config, "context" ) )
merge_settings( settings, global_context, "configuration-file/context" );

// Merge any application-specific context settings
auto const executable = rsutils::os::executable_name();
if( auto executable_context = rsutils::json::nested( config, executable, "context" ) )
merge_settings( settings, executable_context, "configuration/" + executable + "/context" );

// Take the "dds" settings only
settings = rsutils::json::nested( settings, "dds" );

// Finally, merge the given local settings on top of all that
merge_settings( settings, local_settings, "local settings" );
return settings;
}


int main( int argc, char * argv[] )
try
{
dds_domain_id domain = 0;
dds_domain_id domain = -1; // from settings; default to 0
CmdLine cmd( "librealsense rs-dds-adapter tool, use CTRL + C to stop..", ' ' );
ValueArg< dds_domain_id > domain_arg( "d",
"domain",
Expand Down Expand Up @@ -106,7 +166,7 @@ try

// Create a DDS publisher
auto participant = std::make_shared< dds_participant >();
auto settings = nlohmann::json::object();
auto settings = load_settings( nlohmann::json::object() );
participant->init( domain, "rs-dds-adapter", std::move( settings ) );

struct device_handler
Expand Down

0 comments on commit 516c1d3

Please sign in to comment.