blob: bd54e38212ebabd8d591229c122a025a9c9a978d [file] [log] [blame]
Brian Silvermanea2c95f2021-02-10 18:10:26 -08001#include "aos/aos_cli_utils.h"
2
3#include <sys/stat.h>
4#include <sys/types.h>
5#include <unistd.h>
6
Austin Schuh893d7f42022-09-16 15:01:35 -07007#include <chrono>
Brian Silvermanea2c95f2021-02-10 18:10:26 -08008#include <iostream>
9
Austin Schuh893d7f42022-09-16 15:01:35 -070010#include "aos/configuration.h"
11#include "aos/events/shm_event_loop.h"
12#include "aos/events/simulated_event_loop.h"
13#include "aos/time/time.h"
14
Austin Schuh8e2dfc62022-08-17 16:36:00 -070015DEFINE_string(config, "aos_config.json", "File path of aos configuration");
Brian Silvermanea2c95f2021-02-10 18:10:26 -080016
17DEFINE_bool(
18 _bash_autocomplete, false,
19 "Internal use: Outputs channel list for use with autocomplete script.");
Brennan Coslett275965f2023-02-20 15:15:46 -060020
21DEFINE_bool(_zsh_compatability, false,
22 "Internal use: Force completion to complete either channels or "
23 "message_types, zsh doesn't handle spaces well.");
24
Brian Silvermanea2c95f2021-02-10 18:10:26 -080025DEFINE_string(_bash_autocomplete_word, "",
26 "Internal use: Current word being autocompleted");
27
28DEFINE_bool(all, false,
29 "If true, print out the channels for all nodes, not just the "
30 "channels which are visible on this node.");
31
32namespace aos {
33namespace {
34
Austin Schuh893d7f42022-09-16 15:01:35 -070035namespace chrono = std::chrono;
36
Brian Silvermanea2c95f2021-02-10 18:10:26 -080037bool EndsWith(std::string_view str, std::string_view ending) {
38 const std::size_t offset = str.size() - ending.size();
39 return str.size() >= ending.size() &&
40 std::equal(str.begin() + offset, str.end(), ending.begin(),
41 ending.end());
42}
43
Austin Schuh893d7f42022-09-16 15:01:35 -070044void StreamSeconds(std::ostream &stream,
45 const aos::monotonic_clock::time_point now) {
46 if (now < monotonic_clock::epoch()) {
47 chrono::seconds seconds =
48 chrono::duration_cast<chrono::seconds>(now.time_since_epoch());
49
50 stream << "-" << -seconds.count() << "." << std::setfill('0')
51 << std::setw(9)
52 << chrono::duration_cast<chrono::nanoseconds>(seconds -
53 now.time_since_epoch())
54 .count();
55 } else {
56 chrono::seconds seconds =
57 chrono::duration_cast<chrono::seconds>(now.time_since_epoch());
58 stream << seconds.count() << "." << std::setfill('0') << std::setw(9)
59 << chrono::duration_cast<chrono::nanoseconds>(
60 now.time_since_epoch() - seconds)
61 .count();
62 }
63}
64
Brian Silvermanea2c95f2021-02-10 18:10:26 -080065} // namespace
66
67bool CliUtilInfo::Initialize(
68 int *argc, char ***argv,
Austin Schuh59f3b0f2021-07-31 20:50:40 -070069 std::function<bool(const aos::Channel *)> channel_filter,
Austin Schuhba2c8652022-08-17 14:56:08 -070070 std::string_view channel_filter_description, bool expect_args) {
Brian Silvermanea2c95f2021-02-10 18:10:26 -080071 // Don't generate failure output if the config doesn't exist while attempting
72 // to autocomplete.
Milind Upadhyay17098ba2022-04-15 22:18:50 -070073 if (FLAGS__bash_autocomplete &&
74 (!(EndsWith(FLAGS_config, ".json") || EndsWith(FLAGS_config, ".bfbs")))) {
Brian Silvermanea2c95f2021-02-10 18:10:26 -080075 std::cout << "COMPREPLY=()";
76 return true;
77 }
78
Milind Upadhyay17098ba2022-04-15 22:18:50 -070079 config = aos::configuration::MaybeReadConfig(FLAGS_config);
80 if (FLAGS__bash_autocomplete && !config.has_value()) {
81 std::cout << "COMPREPLY=()";
82 return true;
83 }
84 CHECK(config.has_value()) << "Could not read config. See above errors.";
85
Brian Silvermanea2c95f2021-02-10 18:10:26 -080086 event_loop.emplace(&config->message());
87 event_loop->SkipTimingReport();
88 event_loop->SkipAosLog();
89
Austin Schuhd30a5ca2021-07-31 20:49:35 -070090 const flatbuffers::Vector<flatbuffers::Offset<aos::Channel>> *channels =
Brian Silvermanea2c95f2021-02-10 18:10:26 -080091 event_loop->configuration()->channels();
Austin Schuhd30a5ca2021-07-31 20:49:35 -070092
93 do {
94 std::string channel_name;
95 std::string message_type;
96 if (*argc > 1) {
97 channel_name = (*argv)[1];
98 ShiftArgs(argc, argv);
99 }
100 if (*argc > 1) {
101 message_type = (*argv)[1];
102 ShiftArgs(argc, argv);
103 }
104
105 if (FLAGS__bash_autocomplete) {
106 Autocomplete(channel_name, message_type, channel_filter);
107 return true;
108 }
109
110 if (channel_name.empty() && message_type.empty()) {
111 std::cout << "Channels:\n";
112 for (const aos::Channel *channel : *channels) {
113 if (FLAGS_all || channel_filter(channel)) {
114 std::cout << channel->name()->c_str() << ' '
115 << channel->type()->c_str() << '\n';
116 }
117 }
118 return true;
119 }
120
121 std::vector<const aos::Channel *> found_channels_now;
122 bool found_exact = false;
Brian Silvermanea2c95f2021-02-10 18:10:26 -0800123 for (const aos::Channel *channel : *channels) {
Austin Schuhd30a5ca2021-07-31 20:49:35 -0700124 if (channel->name()->c_str() != channel_name) {
125 continue;
Brian Silvermanea2c95f2021-02-10 18:10:26 -0800126 }
Austin Schuhba2c8652022-08-17 14:56:08 -0700127
Austin Schuhd30a5ca2021-07-31 20:49:35 -0700128 if (channel->type()->string_view() == message_type) {
129 if (!found_exact) {
130 found_channels_now.clear();
131 found_exact = true;
132 }
133 } else if (!found_exact && channel->type()->string_view().find(
134 message_type) != std::string_view::npos) {
135 } else {
136 continue;
137 }
Austin Schuhba2c8652022-08-17 14:56:08 -0700138
139 if (!FLAGS_all && !channel_filter(channel)) {
140 LOG(FATAL) << "matched channel does not pass the channel filter: \""
141 << channel_filter_description
142 << "\" [matched channel info]: "
143 << configuration::CleanedChannelToString(channel);
144 }
145
Austin Schuhd30a5ca2021-07-31 20:49:35 -0700146 found_channels_now.push_back(channel);
Brian Silvermanea2c95f2021-02-10 18:10:26 -0800147 }
Brian Silvermanea2c95f2021-02-10 18:10:26 -0800148
Austin Schuhd30a5ca2021-07-31 20:49:35 -0700149 if (found_channels_now.empty()) {
150 LOG(FATAL)
151 << "Could not find any channels with the given name and type for "
152 << channel_name << " " << message_type;
153 } else if (found_channels_now.size() > 1 && !message_type.empty()) {
154 LOG(FATAL) << "Multiple channels found with same type for "
155 << channel_name << " " << message_type;
156 }
157 for (const aos::Channel *channel : found_channels_now) {
158 found_channels.push_back(channel);
159 }
Austin Schuh59f3b0f2021-07-31 20:50:40 -0700160 } while (expect_args && *argc > 1);
Brian Silvermanea2c95f2021-02-10 18:10:26 -0800161
162 return false;
163}
164
165void CliUtilInfo::Autocomplete(
166 std::string_view channel_name, std::string_view message_type,
167 std::function<bool(const aos::Channel *)> channel_filter) {
168 const aos::Configuration *const config_msg = event_loop->configuration();
169 const bool unique_match =
170 std::count_if(config_msg->channels()->begin(),
171 config_msg->channels()->end(),
172 [channel_name, message_type](const aos::Channel *channel) {
173 return channel->name()->string_view() == channel_name &&
174 channel->type()->string_view() == message_type;
175 }) == 1;
176
177 const bool editing_message =
178 !channel_name.empty() && FLAGS__bash_autocomplete_word == message_type;
179 const bool editing_channel =
180 !editing_message && FLAGS__bash_autocomplete_word == channel_name;
181
182 std::cout << "COMPREPLY=(";
183
184 // If we have a unique match, don't provide any suggestions. Otherwise, check
185 // that were're editing one of the two positional arguments.
186 if (!unique_match && (editing_message || editing_channel)) {
187 for (const aos::Channel *channel : *config_msg->channels()) {
188 if (FLAGS_all || channel_filter(channel)) {
189 // Suggest only message types if the message type argument is being
190 // entered.
191 if (editing_message) {
192 // Then, filter for only channel names that match exactly and types
193 // that begin with message_type.
194 if (channel->name()->string_view() == channel_name &&
195 channel->type()->string_view().find(message_type) == 0) {
196 std::cout << '\'' << channel->type()->c_str() << "' ";
197 }
198 } else if (channel->name()->string_view().find(channel_name) == 0) {
199 // If the message type empty, then return full autocomplete.
200 // Otherwise, since the message type is poulated yet not being edited,
201 // the user must be editing the channel name alone, in which case only
202 // suggest channel names, not pairs.
Brennan Coslett275965f2023-02-20 15:15:46 -0600203 // If _split_complete flag is set then dont return
204 // pairs of values
205 if (!FLAGS__zsh_compatability && message_type.empty()) {
Brian Silvermanea2c95f2021-02-10 18:10:26 -0800206 std::cout << '\'' << channel->name()->c_str() << ' '
207 << channel->type()->c_str() << "' ";
208 } else {
209 std::cout << '\'' << channel->name()->c_str() << "' ";
210 }
211 }
212 }
213 }
214 }
215 std::cout << ')';
216}
217
Austin Schuh893d7f42022-09-16 15:01:35 -0700218void PrintMessage(const std::string_view node_name, const aos::Channel *channel,
219 const aos::Context &context, aos::FastStringBuilder *builder,
220 PrintOptions options) {
221 // Print the flatbuffer out to stdout, both to remove the
222 // unnecessary cruft from glog and to allow the user to readily
223 // redirect just the logged output independent of any debugging
224 // information on stderr.
225
226 builder->Reset();
227
228 CHECK(flatbuffers::Verify(*channel->schema(),
229 *channel->schema()->root_table(),
230 static_cast<const uint8_t *>(context.data),
231 static_cast<size_t>(context.size)))
232 << ": Corrupted flatbuffer on " << channel->name()->c_str() << " "
233 << channel->type()->c_str();
234
235 aos::FlatbufferToJson(
236 builder, channel->schema(), static_cast<const uint8_t *>(context.data),
237 {options.pretty, static_cast<size_t>(options.max_vector_size),
Austin Schuhbe6d2962022-11-01 09:24:56 -0700238 options.pretty_max, options.hex});
Austin Schuh893d7f42022-09-16 15:01:35 -0700239
240 if (options.json) {
241 std::cout << "{";
242 if (!node_name.empty()) {
243 std::cout << "\"node\": \"" << node_name << "\", ";
244 }
245 std::cout << "\"monotonic_event_time\": ";
246 StreamSeconds(std::cout, context.monotonic_event_time);
247 std::cout << ", \"realtime_event_time\": \"" << context.realtime_event_time
248 << "\", ";
249
250 if (context.monotonic_remote_time != context.monotonic_event_time) {
251 std::cout << "\"monotonic_remote_time\": ";
252 StreamSeconds(std::cout, context.monotonic_remote_time);
253 std::cout << ", \"realtime_remote_time\": \""
254 << context.realtime_remote_time << "\", ";
255 }
256
257 std::cout << "\"channel\": "
258 << aos::configuration::StrippedChannelToString(channel)
Naman Gupta54047212022-09-23 14:10:30 -0700259 << ", \"data\": " << *builder << "}";
Austin Schuh893d7f42022-09-16 15:01:35 -0700260 } else {
261 if (!node_name.empty()) {
262 std::cout << node_name << " ";
263 }
264
265 if (options.print_timestamps) {
266 if (context.monotonic_remote_time != context.monotonic_event_time) {
267 std::cout << context.realtime_event_time << " ("
268 << context.monotonic_event_time << ") sent "
269 << context.realtime_remote_time << " ("
270 << context.monotonic_remote_time << ") "
271 << channel->name()->c_str() << ' ' << channel->type()->c_str()
Naman Gupta54047212022-09-23 14:10:30 -0700272 << ": " << *builder;
Austin Schuh893d7f42022-09-16 15:01:35 -0700273 } else {
274 std::cout << context.realtime_event_time << " ("
275 << context.monotonic_event_time << ") "
276 << channel->name()->c_str() << ' ' << channel->type()->c_str()
Naman Gupta54047212022-09-23 14:10:30 -0700277 << ": " << *builder;
Austin Schuh893d7f42022-09-16 15:01:35 -0700278 }
279 } else {
Naman Gupta54047212022-09-23 14:10:30 -0700280 std::cout << *builder;
Austin Schuh893d7f42022-09-16 15:01:35 -0700281 }
282 }
283}
284
285void PrintMessage(const aos::Channel *channel, const aos::Context &context,
286 aos::FastStringBuilder *builder, PrintOptions options) {
287 PrintMessage("", channel, context, builder, options);
288}
289
290void PrintMessage(const std::string_view node_name,
291 aos::NodeEventLoopFactory *node_factory,
292 const aos::Channel *channel, const aos::Context &context,
293 aos::FastStringBuilder *builder, PrintOptions options) {
294 if (!options.json && options.distributed_clock) {
295 std::cout << node_factory->ToDistributedClock(context.monotonic_event_time)
296 << " ";
297 }
298 PrintMessage(node_name, channel, context, builder, options);
299}
300
Naman Gupta54047212022-09-23 14:10:30 -0700301Printer::Printer(PrintOptions options, bool flush)
302 : options_(options), flush_(flush) {
303 if (options_.json) {
304 std::cout << "[";
305 }
306}
307
308Printer::~Printer() {
309 if (options_.json) {
310 if (message_count_ > 0) {
311 std::cout << "\n]\n";
312 } else {
313 std::cout << "]\n";
314 }
315 }
316}
317
318void Printer::PrintMessage(const std::string_view node_name,
319 aos::NodeEventLoopFactory *node_factory,
320 const aos::Channel *channel,
321 const aos::Context &context) {
322 if (options_.json) {
323 if (message_count_ != 0) {
324 std::cout << ",\n ";
325 } else {
326 std::cout << "\n ";
327 }
328 }
329
330 aos::PrintMessage(node_name, node_factory, channel, context, &str_builder_,
331 options_);
332
333 if (!options_.json) {
334 if (flush_) {
335 std::cout << std::endl;
336 } else {
337 std::cout << "\n";
338 }
339 }
340 ++message_count_;
341}
342
343void Printer::PrintMessage(const aos::Channel *channel,
344 const aos::Context &context) {
345 if (options_.json) {
346 if (message_count_ != 0) {
347 std::cout << ",\n ";
348 } else {
349 std::cout << "\n ";
350 }
351 }
352
353 aos::PrintMessage(channel, context, &str_builder_, options_);
354
355 if (!options_.json) {
356 if (flush_) {
357 std::cout << std::endl;
358 } else {
359 std::cout << "\n";
360 }
361 }
362 ++message_count_;
363}
364
Brian Silvermanea2c95f2021-02-10 18:10:26 -0800365} // namespace aos