Brian Silverman | ea2c95f | 2021-02-10 18:10:26 -0800 | [diff] [blame] | 1 | #include "aos/aos_cli_utils.h" |
| 2 | |
| 3 | #include <sys/stat.h> |
| 4 | #include <sys/types.h> |
| 5 | #include <unistd.h> |
| 6 | |
Austin Schuh | 893d7f4 | 2022-09-16 15:01:35 -0700 | [diff] [blame] | 7 | #include <chrono> |
Brian Silverman | ea2c95f | 2021-02-10 18:10:26 -0800 | [diff] [blame] | 8 | #include <iostream> |
| 9 | |
Austin Schuh | 893d7f4 | 2022-09-16 15:01:35 -0700 | [diff] [blame] | 10 | #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 Schuh | 8e2dfc6 | 2022-08-17 16:36:00 -0700 | [diff] [blame] | 15 | DEFINE_string(config, "aos_config.json", "File path of aos configuration"); |
Brian Silverman | ea2c95f | 2021-02-10 18:10:26 -0800 | [diff] [blame] | 16 | |
| 17 | DEFINE_bool( |
| 18 | _bash_autocomplete, false, |
| 19 | "Internal use: Outputs channel list for use with autocomplete script."); |
Brennan Coslett | 275965f | 2023-02-20 15:15:46 -0600 | [diff] [blame^] | 20 | |
| 21 | DEFINE_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 Silverman | ea2c95f | 2021-02-10 18:10:26 -0800 | [diff] [blame] | 25 | DEFINE_string(_bash_autocomplete_word, "", |
| 26 | "Internal use: Current word being autocompleted"); |
| 27 | |
| 28 | DEFINE_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 | |
| 32 | namespace aos { |
| 33 | namespace { |
| 34 | |
Austin Schuh | 893d7f4 | 2022-09-16 15:01:35 -0700 | [diff] [blame] | 35 | namespace chrono = std::chrono; |
| 36 | |
Brian Silverman | ea2c95f | 2021-02-10 18:10:26 -0800 | [diff] [blame] | 37 | bool 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 Schuh | 893d7f4 | 2022-09-16 15:01:35 -0700 | [diff] [blame] | 44 | void 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 Silverman | ea2c95f | 2021-02-10 18:10:26 -0800 | [diff] [blame] | 65 | } // namespace |
| 66 | |
| 67 | bool CliUtilInfo::Initialize( |
| 68 | int *argc, char ***argv, |
Austin Schuh | 59f3b0f | 2021-07-31 20:50:40 -0700 | [diff] [blame] | 69 | std::function<bool(const aos::Channel *)> channel_filter, |
Austin Schuh | ba2c865 | 2022-08-17 14:56:08 -0700 | [diff] [blame] | 70 | std::string_view channel_filter_description, bool expect_args) { |
Brian Silverman | ea2c95f | 2021-02-10 18:10:26 -0800 | [diff] [blame] | 71 | // Don't generate failure output if the config doesn't exist while attempting |
| 72 | // to autocomplete. |
Milind Upadhyay | 17098ba | 2022-04-15 22:18:50 -0700 | [diff] [blame] | 73 | if (FLAGS__bash_autocomplete && |
| 74 | (!(EndsWith(FLAGS_config, ".json") || EndsWith(FLAGS_config, ".bfbs")))) { |
Brian Silverman | ea2c95f | 2021-02-10 18:10:26 -0800 | [diff] [blame] | 75 | std::cout << "COMPREPLY=()"; |
| 76 | return true; |
| 77 | } |
| 78 | |
Milind Upadhyay | 17098ba | 2022-04-15 22:18:50 -0700 | [diff] [blame] | 79 | 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 Silverman | ea2c95f | 2021-02-10 18:10:26 -0800 | [diff] [blame] | 86 | event_loop.emplace(&config->message()); |
| 87 | event_loop->SkipTimingReport(); |
| 88 | event_loop->SkipAosLog(); |
| 89 | |
Austin Schuh | d30a5ca | 2021-07-31 20:49:35 -0700 | [diff] [blame] | 90 | const flatbuffers::Vector<flatbuffers::Offset<aos::Channel>> *channels = |
Brian Silverman | ea2c95f | 2021-02-10 18:10:26 -0800 | [diff] [blame] | 91 | event_loop->configuration()->channels(); |
Austin Schuh | d30a5ca | 2021-07-31 20:49:35 -0700 | [diff] [blame] | 92 | |
| 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 Silverman | ea2c95f | 2021-02-10 18:10:26 -0800 | [diff] [blame] | 123 | for (const aos::Channel *channel : *channels) { |
Austin Schuh | d30a5ca | 2021-07-31 20:49:35 -0700 | [diff] [blame] | 124 | if (channel->name()->c_str() != channel_name) { |
| 125 | continue; |
Brian Silverman | ea2c95f | 2021-02-10 18:10:26 -0800 | [diff] [blame] | 126 | } |
Austin Schuh | ba2c865 | 2022-08-17 14:56:08 -0700 | [diff] [blame] | 127 | |
Austin Schuh | d30a5ca | 2021-07-31 20:49:35 -0700 | [diff] [blame] | 128 | 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 Schuh | ba2c865 | 2022-08-17 14:56:08 -0700 | [diff] [blame] | 138 | |
| 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 Schuh | d30a5ca | 2021-07-31 20:49:35 -0700 | [diff] [blame] | 146 | found_channels_now.push_back(channel); |
Brian Silverman | ea2c95f | 2021-02-10 18:10:26 -0800 | [diff] [blame] | 147 | } |
Brian Silverman | ea2c95f | 2021-02-10 18:10:26 -0800 | [diff] [blame] | 148 | |
Austin Schuh | d30a5ca | 2021-07-31 20:49:35 -0700 | [diff] [blame] | 149 | 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 Schuh | 59f3b0f | 2021-07-31 20:50:40 -0700 | [diff] [blame] | 160 | } while (expect_args && *argc > 1); |
Brian Silverman | ea2c95f | 2021-02-10 18:10:26 -0800 | [diff] [blame] | 161 | |
| 162 | return false; |
| 163 | } |
| 164 | |
| 165 | void 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 Coslett | 275965f | 2023-02-20 15:15:46 -0600 | [diff] [blame^] | 203 | // If _split_complete flag is set then dont return |
| 204 | // pairs of values |
| 205 | if (!FLAGS__zsh_compatability && message_type.empty()) { |
Brian Silverman | ea2c95f | 2021-02-10 18:10:26 -0800 | [diff] [blame] | 206 | 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 Schuh | 893d7f4 | 2022-09-16 15:01:35 -0700 | [diff] [blame] | 218 | void 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 Schuh | be6d296 | 2022-11-01 09:24:56 -0700 | [diff] [blame] | 238 | options.pretty_max, options.hex}); |
Austin Schuh | 893d7f4 | 2022-09-16 15:01:35 -0700 | [diff] [blame] | 239 | |
| 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 Gupta | 5404721 | 2022-09-23 14:10:30 -0700 | [diff] [blame] | 259 | << ", \"data\": " << *builder << "}"; |
Austin Schuh | 893d7f4 | 2022-09-16 15:01:35 -0700 | [diff] [blame] | 260 | } 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 Gupta | 5404721 | 2022-09-23 14:10:30 -0700 | [diff] [blame] | 272 | << ": " << *builder; |
Austin Schuh | 893d7f4 | 2022-09-16 15:01:35 -0700 | [diff] [blame] | 273 | } else { |
| 274 | std::cout << context.realtime_event_time << " (" |
| 275 | << context.monotonic_event_time << ") " |
| 276 | << channel->name()->c_str() << ' ' << channel->type()->c_str() |
Naman Gupta | 5404721 | 2022-09-23 14:10:30 -0700 | [diff] [blame] | 277 | << ": " << *builder; |
Austin Schuh | 893d7f4 | 2022-09-16 15:01:35 -0700 | [diff] [blame] | 278 | } |
| 279 | } else { |
Naman Gupta | 5404721 | 2022-09-23 14:10:30 -0700 | [diff] [blame] | 280 | std::cout << *builder; |
Austin Schuh | 893d7f4 | 2022-09-16 15:01:35 -0700 | [diff] [blame] | 281 | } |
| 282 | } |
| 283 | } |
| 284 | |
| 285 | void PrintMessage(const aos::Channel *channel, const aos::Context &context, |
| 286 | aos::FastStringBuilder *builder, PrintOptions options) { |
| 287 | PrintMessage("", channel, context, builder, options); |
| 288 | } |
| 289 | |
| 290 | void 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 Gupta | 5404721 | 2022-09-23 14:10:30 -0700 | [diff] [blame] | 301 | Printer::Printer(PrintOptions options, bool flush) |
| 302 | : options_(options), flush_(flush) { |
| 303 | if (options_.json) { |
| 304 | std::cout << "["; |
| 305 | } |
| 306 | } |
| 307 | |
| 308 | Printer::~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 | |
| 318 | void 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 | |
| 343 | void 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 Silverman | ea2c95f | 2021-02-10 18:10:26 -0800 | [diff] [blame] | 365 | } // namespace aos |