Skip to content

Commit

Permalink
add 'build-command' capability to hwm & remove hw-monitor dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
maloel committed Oct 9, 2023
1 parent a248872 commit fecfabd
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 10 deletions.
20 changes: 18 additions & 2 deletions src/dds/rs-dds-device-proxy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@

#include <src/stream.h>
#include <src/environment.h>
#include <src/hw-monitor.h>

#include <rsutils/json.h>
#include <rsutils/string/hexarray.h>
Expand Down Expand Up @@ -519,7 +518,24 @@ std::vector< uint8_t > dds_device_proxy::build_command( uint32_t opcode,
size_t dataLength ) const
{
// debug_interface function
return hw_monitor::build_command( opcode, param1, param2, param3, param4, data, dataLength );
rsutils::string::hexarray hexdata( std::vector< uint8_t >( data, data + dataLength ) );
nlohmann::json control = nlohmann::json::object( { { "id", "hwm" },
{ "data", hexdata },
{ "opcode", opcode },
{ "param1", param1 },
{ "param2", param2 },
{ "param3", param3 },
{ "param4", param4 },
{ "build-command", true } } );
nlohmann::json reply;
_dds_dev->send_control( control, &reply );
std::string default_status( "OK", 2 );
if( rsutils::json::get( reply, "status", default_status ) != default_status )
throw std::runtime_error( "Failed build-command: "
+ rsutils::json::get( reply, "status", std::string( "unknown reason" ) ) );
if( ! rsutils::json::get_ex( reply, "data", &hexdata ) )
throw std::runtime_error( "Failed HWM: missing 'data' in reply" );
return hexdata.detach();
}


Expand Down
12 changes: 11 additions & 1 deletion third-party/realdds/doc/control.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,6 @@ A [disconnection event](discovery.md#disconnection) can be expected if the reply

Can be used to send internal commands to the hardware and may brick the device if used. May or may not be implemented, and is not documented.

* `opcode` (string) is suggested, but optional
* `data` is required

It is up to the server to validate and make sure everything is as expected.
Expand Down Expand Up @@ -143,6 +142,17 @@ A reply can be expected. Attaching the control is recommended so it's clear what
}
```

#### A more verbose method

You may also instruct the server to **build the HWM** command if you have knowledge of the available opcodes and data needed:

* `opcode` (string) is required
* `param1`, `param2`, `param3`, `param4` are all 32-bit integers whose meaning depends on the `opcode`, and with a default of `0`
* The `data` field, in this case, points to the command data, if needed, and becomes part of the generated HWM

If an optional `build-command` field is present and `true`, then the reply `data` will contain the generated HWM command **without running it**.


#### `hexarray` type

A `hexarray` is a special encoding for a `bytearray`, or array of bytes, in JSON.
Expand Down
29 changes: 22 additions & 7 deletions tools/dds/dds-adapter/lrs-device-controller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -867,23 +867,38 @@ bool lrs_device_controller::on_hwm( nlohmann::json const & control, nlohmann::js
if( ! dp )
throw std::runtime_error( "device does not have a debug protocol implemented" );

rsutils::string::hexarray bytes;
if( ! rsutils::json::get_ex( control, "data", &bytes ) )
throw std::runtime_error( "no 'data' or 'opcode' in HWM control" );
rsutils::string::hexarray data;

uint32_t opcode, param1 = 0, param2 = 0, param3 = 0, param4 = 0;
uint32_t opcode;
if( rsutils::json::get_ex( control, "opcode", &opcode ) )
{
// In the presence of 'opcode', we're asked to build the command using optional parameters
uint32_t param1 = 0, param2 = 0, param3 = 0, param4 = 0;
rsutils::json::get_ex( control, "param1", &param1 );
rsutils::json::get_ex( control, "param2", &param2 );
rsutils::json::get_ex( control, "param3", &param3 );
rsutils::json::get_ex( control, "param4", &param4 );

bytes = dp.build_command( opcode, param1, param2, param3, param4, bytes.get_bytes() );
rsutils::json::get_ex( control, "data", &data ); // optional

// Build the HWM command
data = dp.build_command( opcode, param1, param2, param3, param4, data.get_bytes() );

// And, if told to not actually run it, we return the HWM command
if( rsutils::json::get< bool >( control, "build-command", false ) )
{
reply["data"] = data;
return true;
}
}
else
{
if( ! rsutils::json::get_ex( control, "data", &data ) )
throw std::runtime_error( "no 'data' in HWM control" );
}

bytes = dp.send_and_receive_raw_data( bytes.get_bytes() );
reply["data"] = bytes;
data = dp.send_and_receive_raw_data( data.get_bytes() );
reply["data"] = data;
return true;
}

Expand Down

0 comments on commit fecfabd

Please sign in to comment.