uavcan_linux::makeApplicationID()

This commit is contained in:
Pavel Kirienko
2015-05-26 01:22:26 +03:00
parent 258da95d12
commit e289a1e09c
2 changed files with 53 additions and 4 deletions
@@ -39,10 +39,10 @@ uavcan_linux::NodePtr initNode(const std::vector<std::string>& ifaces, uavcan::N
node->getLogger().setLevel(uavcan::protocol::debug::LogLevel::DEBUG);
{
const auto machine_id = uavcan_linux::MachineIDReader().read();
const auto app_id = uavcan_linux::makeApplicationID(uavcan_linux::MachineIDReader().read(), name, nid.get());
uavcan::protocol::HardwareVersion hwver;
std::copy(machine_id.begin(), machine_id.end(), hwver.unique_id.begin());
std::copy(app_id.begin(), app_id.end(), hwver.unique_id.begin());
std::cout << hwver << std::endl;
node->setHardwareVersion(hwver);
@@ -498,7 +498,6 @@ void runForever(const uavcan_linux::NodePtr& node,
* Storage backend
*/
uavcan_posix::dynamic_node_id_server::FileStorageBackend storage_backend;
ENFORCE(0 <= storage_backend.init(persistent_storage_path.c_str()));
/*
@@ -506,7 +505,16 @@ void runForever(const uavcan_linux::NodePtr& node,
*/
uavcan::dynamic_node_id_server::DistributedServer server(*node, storage_backend, event_tracer);
ENFORCE(0 <= server.init(node->getNodeStatusProvider().getHardwareVersion().unique_id, cluster_size));
const int server_init_res = server.init(node->getNodeStatusProvider().getHardwareVersion().unique_id, cluster_size);
if (server_init_res < 0)
{
throw std::runtime_error("Failed to start the server; error " + std::to_string(server_init_res));
}
/*
* Preparing the CLI
*/
std::printf("\x1b[2J"); // Clear entire screen; this will preserve initialization output in the scrollback
/*
* Spinning the node
@@ -14,6 +14,7 @@
#include <algorithm>
#include <utility>
#include <uavcan_linux/exception.hpp>
#include <uavcan/data_type.hpp>
namespace uavcan_linux
{
@@ -132,5 +133,45 @@ public:
}
};
/**
* This class computes unique ID for a UAVCAN node in a Linux application.
* It takes the following inputs:
* - Unique machine ID
* - Node name string (e.g. "org.uavcan.linux_app.dynamic_node_id_server")
* - Instance ID byte, e.g. node ID
*/
std::array<std::uint8_t, 16> makeApplicationID(const MachineIDReader::MachineID& machine_id,
const std::string& node_name,
const std::uint8_t instance_id)
{
union HalfID
{
std::uint64_t num;
std::uint8_t bytes[8];
HalfID(std::uint64_t arg_num) : num(arg_num) { }
};
std::array<std::uint8_t, 16> out;
// First 8 bytes of the application ID are CRC64 of the machine ID in native byte order
{
uavcan::DataTypeSignatureCRC crc;
crc.add(machine_id.data(), static_cast<unsigned>(machine_id.size()));
HalfID half(crc.get());
std::copy_n(half.bytes, 8, out.begin());
}
// Last 8 bytes of the application ID are CRC64 of the node name and optionally node ID
{
uavcan::DataTypeSignatureCRC crc;
crc.add(reinterpret_cast<const std::uint8_t*>(node_name.c_str()), static_cast<unsigned>(node_name.length()));
crc.add(instance_id);
HalfID half(crc.get());
std::copy_n(half.bytes, 8, out.begin() + 8);
}
return out;
}
}