16 #include <uavcan/protocol/param/GetSet.hpp>
17 #include <uavcan/protocol/param/ExecuteOpcode.hpp>
18 #include <uavcan/equipment/hardpoint/Command.hpp>
25 mutable std::mutex mutex_;
27 std::queue<std::string> queue_;
34 std::getline(std::cin, input);
35 std::lock_guard<std::mutex> lock(mutex_);
42 : thread_(&StdinLineReader::worker, this)
47 bool hasPendingInput()
const
49 std::lock_guard<std::mutex> lock(mutex_);
50 return !queue_.empty();
55 std::lock_guard<std::mutex> lock(mutex_);
58 throw std::runtime_error(
"Input queue is empty");
60 auto ret = queue_.front();
65 std::vector<std::string> getSplitLine()
67 std::istringstream iss(getLine());
68 std::vector<std::string> out;
69 std::copy(std::istream_iterator<std::string>(iss), std::istream_iterator<std::string>(),
70 std::back_inserter(out));
78 uavcan::protocol::SoftwareVersion(), uavcan::protocol::HardwareVersion(),
80 node->setModeOperational();
84 template <
typename DataType>
86 uavcan::NodeID server_node_id,
const typename DataType::Request& request)
100 const std::map<std::string,
101 std::pair<std::string,
103 const std::vector<std::string>&)>
110 "No arguments supplied - requests all params from a remote node\n"
111 "<param_name> <param_value> - assigns parameter <param_name> to value <param_value>",
114 auto client =
node->makeBlockingServiceClient<uavcan::protocol::param::GetSet>();
115 uavcan::protocol::param::GetSet::Request request;
127 <<
"\n" << std::string(80,
'-')
134 request.name =
args.at(0).c_str();
136 request.value.to<uavcan::protocol::param::Value::Tag::real_value>() = std::stof(
args.at(1));
137 std::cout <<
call(*client, node_id, request) << std::endl;
145 "Calls uavcan.protocol.param.ExecuteOpcode on a remote node with OPCODE_SAVE",
148 auto client =
node->makeBlockingServiceClient<uavcan::protocol::param::ExecuteOpcode>();
149 uavcan::protocol::param::ExecuteOpcode::Request request;
150 request.opcode = request.OPCODE_SAVE;
151 std::cout <<
call(*client, node_id, request) << std::endl;
158 "Calls uavcan.protocol.param.ExecuteOpcode on a remote node with OPCODE_ERASE",
161 auto client =
node->makeBlockingServiceClient<uavcan::protocol::param::ExecuteOpcode>();
162 uavcan::protocol::param::ExecuteOpcode::Request request;
163 request.opcode = request.OPCODE_ERASE;
164 std::cout <<
call(*client, node_id, request) << std::endl;
171 "Restarts a remote node using uavcan.protocol.RestartNode",
174 auto client =
node->makeBlockingServiceClient<uavcan::protocol::RestartNode>();
175 uavcan::protocol::RestartNode::Request request;
176 request.magic_number = request.MAGIC_NUMBER;
184 std::cout <<
"<NO RESPONSE>" << std::endl;
192 "Calls uavcan.protocol.GetNodeInfo on a remote node",
195 auto client =
node->makeBlockingServiceClient<uavcan::protocol::GetNodeInfo>();
196 std::cout <<
call(*client, node_id, uavcan::protocol::GetNodeInfo::Request()) << std::endl;
203 "Calls uavcan.protocol.GetTransportStats on a remote node",
206 auto client =
node->makeBlockingServiceClient<uavcan::protocol::GetTransportStats>();
207 std::cout <<
call(*client, node_id, uavcan::protocol::GetTransportStats::Request()) << std::endl;
214 "Publishes uavcan.equipment.hardpoint.Command\n"
215 "Expected argument: command",
218 uavcan::equipment::hardpoint::Command msg;
219 msg.command = std::stoi(
args.at(0));
220 auto pub =
node->makePublisher<uavcan::equipment::hardpoint::Command>();
221 (void)pub->broadcast(msg);
229 StdinLineReader stdin_reader;
230 std::cout <<
"> " << std::flush;
234 if (!stdin_reader.hasPendingInput())
238 const auto words = stdin_reader.getSplitLine();
239 bool command_is_known =
false;
243 if (words.size() >= 2)
245 const auto cmd = words.at(0);
247 auto it = commands.find(cmd);
248 if (it != std::end(commands))
250 command_is_known =
true;
251 it->second.second(
node, node_id, std::vector<std::string>(words.begin() + 2, words.end()));
255 catch (std::exception& ex)
257 std::cout <<
"FAILURE\n" << ex.what() << std::endl;
260 if (!command_is_known)
262 std::cout <<
"<command> <remote node id> [args...]\n";
263 std::cout <<
"Say 'help' to get help.\n";
265 if (!words.empty() && words.at(0) ==
"help")
267 std::cout <<
"Usage:\n\n";
268 for (
auto& cmd : commands)
270 std::cout << cmd.first <<
"\n" << cmd.second.first <<
"\n\n";
274 std::cout <<
"> " << std::flush;
280 int main(
int argc,
const char** argv)
286 std::cerr <<
"Usage:\n\t" << argv[0] <<
" <node-id> <can-iface-name-1> [can-iface-name-N...]" << std::endl;
289 const int self_node_id = std::stoi(argv[1]);
290 const std::vector<std::string> iface_names(argv + 2, argv + argc);
295 catch (
const std::exception& ex)
297 std::cerr <<
"Error: " << ex.what() << std::endl;