test_dynamic_node_id_client.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2015 Pavel Kirienko <pavel.kirienko@gmail.com>
3  */
4 
5 #include <iostream>
6 #include "debug.hpp"
9 
10 namespace
11 {
12 
13 uavcan_linux::NodePtr initNodeWithDynamicID(const std::vector<std::string>& ifaces,
14  const std::uint8_t instance_id,
15  const uavcan::NodeID preferred_node_id,
16  const std::string& name)
17 {
18  /*
19  * Initializing the node object
20  */
21  auto node = uavcan_linux::makeNode(ifaces);
22 
23  node->setName(name.c_str());
24  node->getLogger().setLevel(uavcan::protocol::debug::LogLevel::DEBUG);
25 
26  {
27  const auto app_id = uavcan_linux::makeApplicationID(uavcan_linux::MachineIDReader().read(), name, instance_id);
28 
29  uavcan::protocol::HardwareVersion hwver;
30  std::copy(app_id.begin(), app_id.end(), hwver.unique_id.begin());
31  std::cout << hwver << std::endl;
32 
33  node->setHardwareVersion(hwver);
34  }
35 
36  /*
37  * Starting the node
38  */
39  const int start_res = node->start();
40  ENFORCE(0 == start_res);
41 
42  /*
43  * Running the dynamic node ID client until it's done
44  */
46 
47  ENFORCE(0 <= client.start(node->getNodeStatusProvider().getHardwareVersion().unique_id, preferred_node_id));
48 
49  std::cout << "Waiting for dynamic node ID allocation..." << std::endl;
50 
51  while (!client.isAllocationComplete())
52  {
53  const int res = node->spin(uavcan::MonotonicDuration::fromMSec(100));
54  if (res < 0)
55  {
56  std::cerr << "Spin error: " << res << std::endl;
57  }
58  }
59 
60  std::cout << "Node ID " << int(client.getAllocatedNodeID().get())
61  << " allocated by " << int(client.getAllocatorNodeID().get()) << std::endl;
62 
63  /*
64  * Finishing the node initialization
65  */
66  node->setNodeID(client.getAllocatedNodeID());
67 
68  node->setModeOperational();
69 
70  return node;
71 }
72 
74 {
75  while (true)
76  {
77  const int res = node->spin(uavcan::MonotonicDuration::fromMSec(100));
78  if (res < 0)
79  {
80  std::cerr << "Spin error: " << res << std::endl;
81  }
82  }
83 }
84 
85 }
86 
87 int main(int argc, const char** argv)
88 {
89  try
90  {
91  if (argc < 3)
92  {
93  std::cerr << "Usage:\n\t"
94  << argv[0] << " <instance-id> <can-iface-name-1> [can-iface-name-N...]\n"
95  << "Where <instance-id> is used to augment the unique node ID and also indicates\n"
96  << "the preferred node ID value. Valid range is [0, 127]."
97  << std::endl;
98  return 1;
99  }
100 
101  const int instance_id = std::stoi(argv[1]);
102  if (instance_id < 0 || instance_id > 127)
103  {
104  std::cerr << "Invalid instance ID: " << instance_id << std::endl;
105  std::exit(1);
106  }
107 
108  uavcan_linux::NodePtr node = initNodeWithDynamicID(std::vector<std::string>(argv + 2, argv + argc),
109  std::uint8_t(instance_id),
110  std::uint8_t(instance_id),
111  "org.uavcan.linux_test_dynamic_node_id_client");
112  runForever(node);
113 
114  return 0;
115  }
116  catch (const std::exception& ex)
117  {
118  std::cerr << "Error: " << ex.what() << std::endl;
119  return 1;
120  }
121 }
uavcan::NodeID
Definition: transfer.hpp:112
uavcan::DurationBase< MonotonicDuration >::fromMSec
static MonotonicDuration fromMSec(int64_t ms)
Definition: time.hpp:41
runForever
static void runForever(const uavcan_linux::NodePtr &node)
Definition: test_node.cpp:42
ENFORCE
#define ENFORCE(x)
Definition: platform_specific_components/linux/libuavcan/apps/debug.hpp:13
uavcan::uint8_t
std::uint8_t uint8_t
Definition: std.hpp:24
uavcan_linux.hpp
main
int main(int argc, const char **argv)
Definition: test_dynamic_node_id_client.cpp:87
debug.hpp
uavcan_linux::makeApplicationID
std::array< std::uint8_t, 16 > makeApplicationID(const MachineIDReader::MachineID &machine_id, const std::string &node_name, const std::uint8_t instance_id=0)
Definition: system_utils.hpp:143
uavcan::DynamicNodeIDClient
Definition: dynamic_node_id_client.hpp:31
int
int
Definition: libstubs.cpp:120
dynamic_node_id_client.hpp
uavcan_linux::MachineIDReader
Definition: system_utils.hpp:32
uavcan_linux::NodePtr
std::shared_ptr< Node > NodePtr
Definition: platform_specific_components/linux/libuavcan/include/uavcan_linux/helpers.hpp:354
pyuavcan_v0.introspect.node
node
Definition: introspect.py:398
uavcan::copy
UAVCAN_EXPORT OutputIt copy(InputIt first, InputIt last, OutputIt result)
Definition: templates.hpp:238
uavcan_linux::makeNode
static NodePtr makeNode(const std::vector< std::string > &iface_names, ClockAdjustmentMode clock_adjustment_mode=SystemClock::detectPreferredClockAdjustmentMode())
Definition: platform_specific_components/linux/libuavcan/include/uavcan_linux/helpers.hpp:363


uavcan_communicator
Author(s):
autogenerated on Fri Dec 13 2024 03:10:03