test_file_server.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 <string>
7 #include <cstdlib>
8 #include <cstdio>
9 #include <sys/types.h>
10 #include <unistd.h>
11 #include "debug.hpp"
12 // UAVCAN
14 // UAVCAN Linux drivers
16 // UAVCAN POSIX drivers
17 #include <uavcan_posix/basic_file_server_backend.hpp>
18 #include <uavcan_posix/firmware_version_checker.hpp> // Compilability test
19 
20 namespace
21 {
22 
23 uavcan_linux::NodePtr initNode(const std::vector<std::string>& ifaces, uavcan::NodeID nid, const std::string& name)
24 {
25  auto node = uavcan_linux::makeNode(ifaces);
26 
27  node->setNodeID(nid);
28  node->setName(name.c_str());
29  node->getLogger().setLevel(uavcan::protocol::debug::LogLevel::DEBUG);
30 
31  {
32  const auto app_id = uavcan_linux::makeApplicationID(uavcan_linux::MachineIDReader().read(), name, nid.get());
33 
34  uavcan::protocol::HardwareVersion hwver;
35  std::copy(app_id.begin(), app_id.end(), hwver.unique_id.begin());
36  std::cout << hwver << std::endl;
37 
38  node->setHardwareVersion(hwver);
39  }
40 
41  const int start_res = node->start();
42  ENFORCE(0 == start_res);
43 
44  node->setModeOperational();
45 
46  return node;
47 }
48 
50 {
51  uavcan_posix::BasicFileServerBackend backend(*node);
52 
53  uavcan::FileServer server(*node, backend);
54 
55  const int server_init_res = server.start();
56  if (server_init_res < 0)
57  {
58  throw std::runtime_error("Failed to start the server; error " + std::to_string(server_init_res));
59  }
60 
61  while (true)
62  {
63  const int res = node->spin(uavcan::MonotonicDuration::fromMSec(100));
64  if (res < 0)
65  {
66  std::cerr << "Spin error: " << res << std::endl;
67  }
68  }
69 }
70 
71 struct Options
72 {
73  uavcan::NodeID node_id;
74  std::vector<std::string> ifaces;
75 };
76 
77 Options parseOptions(int argc, const char** argv)
78 {
79  const char* const executable_name = *argv++;
80  argc--;
81 
82  const auto enforce = [executable_name](bool condition, const char* error_text) {
83  if (!condition)
84  {
85  std::cerr << error_text << "\n"
86  << "Usage:\n\t"
87  << executable_name
88  << " <node-id> <can-iface-name-1> [can-iface-name-N...]"
89  << std::endl;
90  std::exit(1);
91  }
92  };
93 
94  enforce(argc >= 2, "Not enough arguments");
95 
96  /*
97  * Node ID is always at the first position
98  */
99  argc--;
100  const int node_id = std::stoi(*argv++);
101  enforce(node_id >= 1 && node_id <= 127, "Invalid node ID");
102 
103  Options out;
104  out.node_id = uavcan::NodeID(std::uint8_t(node_id));
105 
106  while (argc --> 0)
107  {
108  const std::string token(*argv++);
109 
110  if (token[0] != '-')
111  {
112  out.ifaces.push_back(token);
113  }
114  else
115  {
116  enforce(false, "Unexpected argument");
117  }
118  }
119 
120  return out;
121 }
122 
123 }
124 
125 int main(int argc, const char** argv)
126 {
127  try
128  {
129  auto options = parseOptions(argc, argv);
130 
131  std::cout << "Self node ID: " << int(options.node_id.get()) << "\n"
132  "Num ifaces: " << options.ifaces.size() << "\n"
133 #ifdef NDEBUG
134  "Build mode: Release"
135 #else
136  "Build mode: Debug"
137 #endif
138  << std::endl;
139 
140  auto node = initNode(options.ifaces, options.node_id, "org.uavcan.linux_test_file_server");
141  runForever(node);
142  return 0;
143  }
144  catch (const std::exception& ex)
145  {
146  std::cerr << "Error: " << ex.what() << std::endl;
147  return 1;
148  }
149 }
uavcan::FileServer
Definition: file_server.hpp:191
uavcan::NodeID::get
uint8_t get() const
Definition: transfer.hpp:132
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
initNode
static uavcan_linux::NodePtr initNode(const std::vector< std::string > &ifaces, uavcan::NodeID nid, const std::string &name)
Definition: test_node.cpp:10
main
int main(int argc, const char **argv)
Definition: test_file_server.cpp:125
pyuavcan_v0.dsdl.parser.enforce
def enforce(cond, fmt, *args)
Definition: parser.py:726
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
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
int
int
Definition: libstubs.cpp:120
file_server.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