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."); |
| 20 | DEFINE_string(_bash_autocomplete_word, "", |
| 21 | "Internal use: Current word being autocompleted"); |
| 22 | |
| 23 | DEFINE_bool(all, false, |
| 24 | "If true, print out the channels for all nodes, not just the " |
| 25 | "channels which are visible on this node."); |
| 26 | |
| 27 | namespace aos { |
| 28 | namespace { |
| 29 | |
Austin Schuh | 893d7f4 | 2022-09-16 15:01:35 -0700 | [diff] [blame] | 30 | namespace chrono = std::chrono; |
| 31 | |
Brian Silverman | ea2c95f | 2021-02-10 18:10:26 -0800 | [diff] [blame] | 32 | bool EndsWith(std::string_view str, std::string_view ending) { |
| 33 | const std::size_t offset = str.size() - ending.size(); |
| 34 | return str.size() >= ending.size() && |
| 35 | std::equal(str.begin() + offset, str.end(), ending.begin(), |
| 36 | ending.end()); |
| 37 | } |
| 38 | |
Austin Schuh | 893d7f4 | 2022-09-16 15:01:35 -0700 | [diff] [blame] | 39 | void StreamSeconds(std::ostream &stream, |
| 40 | const aos::monotonic_clock::time_point now) { |
| 41 | if (now < monotonic_clock::epoch()) { |
| 42 | chrono::seconds seconds = |
| 43 | chrono::duration_cast<chrono::seconds>(now.time_since_epoch()); |
| 44 | |
| 45 | stream << "-" << -seconds.count() << "." << std::setfill('0') |
| 46 | << std::setw(9) |
| 47 | << chrono::duration_cast<chrono::nanoseconds>(seconds - |
| 48 | now.time_since_epoch()) |
| 49 | .count(); |
| 50 | } else { |
| 51 | chrono::seconds seconds = |
| 52 | chrono::duration_cast<chrono::seconds>(now.time_since_epoch()); |
| 53 | stream << seconds.count() << "." << std::setfill('0') << std::setw(9) |
| 54 | << chrono::duration_cast<chrono::nanoseconds>( |
| 55 | now.time_since_epoch() - seconds) |
| 56 | .count(); |
| 57 | } |
| 58 | } |
| 59 | |
Brian Silverman | ea2c95f | 2021-02-10 18:10:26 -0800 | [diff] [blame] | 60 | } // namespace |
| 61 | |
| 62 | bool CliUtilInfo::Initialize( |
| 63 | int *argc, char ***argv, |
Austin Schuh | 59f3b0f | 2021-07-31 20:50:40 -0700 | [diff] [blame] | 64 | std::function<bool(const aos::Channel *)> channel_filter, |
Austin Schuh | ba2c865 | 2022-08-17 14:56:08 -0700 | [diff] [blame] | 65 | std::string_view channel_filter_description, bool expect_args) { |
Brian Silverman | ea2c95f | 2021-02-10 18:10:26 -0800 | [diff] [blame] | 66 | // Don't generate failure output if the config doesn't exist while attempting |
| 67 | // to autocomplete. |
Milind Upadhyay | 17098ba | 2022-04-15 22:18:50 -0700 | [diff] [blame] | 68 | if (FLAGS__bash_autocomplete && |
| 69 | (!(EndsWith(FLAGS_config, ".json") || EndsWith(FLAGS_config, ".bfbs")))) { |
Brian Silverman | ea2c95f | 2021-02-10 18:10:26 -0800 | [diff] [blame] | 70 | std::cout << "COMPREPLY=()"; |
| 71 | return true; |
| 72 | } |
| 73 | |
Milind Upadhyay | 17098ba | 2022-04-15 22:18:50 -0700 | [diff] [blame] | 74 | config = aos::configuration::MaybeReadConfig(FLAGS_config); |
| 75 | if (FLAGS__bash_autocomplete && !config.has_value()) { |
| 76 | std::cout << "COMPREPLY=()"; |
| 77 | return true; |
| 78 | } |
| 79 | CHECK(config.has_value()) << "Could not read config. See above errors."; |
| 80 | |
Brian Silverman | ea2c95f | 2021-02-10 18:10:26 -0800 | [diff] [blame] | 81 | event_loop.emplace(&config->message()); |
| 82 | event_loop->SkipTimingReport(); |
| 83 | event_loop->SkipAosLog(); |
| 84 | |
Austin Schuh | d30a5ca | 2021-07-31 20:49:35 -0700 | [diff] [blame] | 85 | const flatbuffers::Vector<flatbuffers::Offset<aos::Channel>> *channels = |
Brian Silverman | ea2c95f | 2021-02-10 18:10:26 -0800 | [diff] [blame] | 86 | event_loop->configuration()->channels(); |
Austin Schuh | d30a5ca | 2021-07-31 20:49:35 -0700 | [diff] [blame] | 87 | |
| 88 | do { |
| 89 | std::string channel_name; |
| 90 | std::string message_type; |
| 91 | if (*argc > 1) { |
| 92 | channel_name = (*argv)[1]; |
| 93 | ShiftArgs(argc, argv); |
| 94 | } |
| 95 | if (*argc > 1) { |
| 96 | message_type = (*argv)[1]; |
| 97 | ShiftArgs(argc, argv); |
| 98 | } |
| 99 | |
| 100 | if (FLAGS__bash_autocomplete) { |
| 101 | Autocomplete(channel_name, message_type, channel_filter); |
| 102 | return true; |
| 103 | } |
| 104 | |
| 105 | if (channel_name.empty() && message_type.empty()) { |
| 106 | std::cout << "Channels:\n"; |
| 107 | for (const aos::Channel *channel : *channels) { |
| 108 | if (FLAGS_all || channel_filter(channel)) { |
| 109 | std::cout << channel->name()->c_str() << ' ' |
| 110 | << channel->type()->c_str() << '\n'; |
| 111 | } |
| 112 | } |
| 113 | return true; |
| 114 | } |
| 115 | |
| 116 | std::vector<const aos::Channel *> found_channels_now; |
| 117 | bool found_exact = false; |
Brian Silverman | ea2c95f | 2021-02-10 18:10:26 -0800 | [diff] [blame] | 118 | for (const aos::Channel *channel : *channels) { |
Austin Schuh | d30a5ca | 2021-07-31 20:49:35 -0700 | [diff] [blame] | 119 | if (channel->name()->c_str() != channel_name) { |
| 120 | continue; |
Brian Silverman | ea2c95f | 2021-02-10 18:10:26 -0800 | [diff] [blame] | 121 | } |
Austin Schuh | ba2c865 | 2022-08-17 14:56:08 -0700 | [diff] [blame] | 122 | |
Austin Schuh | d30a5ca | 2021-07-31 20:49:35 -0700 | [diff] [blame] | 123 | if (channel->type()->string_view() == message_type) { |
| 124 | if (!found_exact) { |
| 125 | found_channels_now.clear(); |
| 126 | found_exact = true; |
| 127 | } |
| 128 | } else if (!found_exact && channel->type()->string_view().find( |
| 129 | message_type) != std::string_view::npos) { |
| 130 | } else { |
| 131 | continue; |
| 132 | } |
Austin Schuh | ba2c865 | 2022-08-17 14:56:08 -0700 | [diff] [blame] | 133 | |
| 134 | if (!FLAGS_all && !channel_filter(channel)) { |
| 135 | LOG(FATAL) << "matched channel does not pass the channel filter: \"" |
| 136 | << channel_filter_description |
| 137 | << "\" [matched channel info]: " |
| 138 | << configuration::CleanedChannelToString(channel); |
| 139 | } |
| 140 | |
Austin Schuh | d30a5ca | 2021-07-31 20:49:35 -0700 | [diff] [blame] | 141 | found_channels_now.push_back(channel); |
Brian Silverman | ea2c95f | 2021-02-10 18:10:26 -0800 | [diff] [blame] | 142 | } |
Brian Silverman | ea2c95f | 2021-02-10 18:10:26 -0800 | [diff] [blame] | 143 | |
Austin Schuh | d30a5ca | 2021-07-31 20:49:35 -0700 | [diff] [blame] | 144 | if (found_channels_now.empty()) { |
| 145 | LOG(FATAL) |
| 146 | << "Could not find any channels with the given name and type for " |
| 147 | << channel_name << " " << message_type; |
| 148 | } else if (found_channels_now.size() > 1 && !message_type.empty()) { |
| 149 | LOG(FATAL) << "Multiple channels found with same type for " |
| 150 | << channel_name << " " << message_type; |
| 151 | } |
| 152 | for (const aos::Channel *channel : found_channels_now) { |
| 153 | found_channels.push_back(channel); |
| 154 | } |
Austin Schuh | 59f3b0f | 2021-07-31 20:50:40 -0700 | [diff] [blame] | 155 | } while (expect_args && *argc > 1); |
Brian Silverman | ea2c95f | 2021-02-10 18:10:26 -0800 | [diff] [blame] | 156 | |
| 157 | return false; |
| 158 | } |
| 159 | |
| 160 | void CliUtilInfo::Autocomplete( |
| 161 | std::string_view channel_name, std::string_view message_type, |
| 162 | std::function<bool(const aos::Channel *)> channel_filter) { |
| 163 | const aos::Configuration *const config_msg = event_loop->configuration(); |
| 164 | const bool unique_match = |
| 165 | std::count_if(config_msg->channels()->begin(), |
| 166 | config_msg->channels()->end(), |
| 167 | [channel_name, message_type](const aos::Channel *channel) { |
| 168 | return channel->name()->string_view() == channel_name && |
| 169 | channel->type()->string_view() == message_type; |
| 170 | }) == 1; |
| 171 | |
| 172 | const bool editing_message = |
| 173 | !channel_name.empty() && FLAGS__bash_autocomplete_word == message_type; |
| 174 | const bool editing_channel = |
| 175 | !editing_message && FLAGS__bash_autocomplete_word == channel_name; |
| 176 | |
| 177 | std::cout << "COMPREPLY=("; |
| 178 | |
| 179 | // If we have a unique match, don't provide any suggestions. Otherwise, check |
| 180 | // that were're editing one of the two positional arguments. |
| 181 | if (!unique_match && (editing_message || editing_channel)) { |
| 182 | for (const aos::Channel *channel : *config_msg->channels()) { |
| 183 | if (FLAGS_all || channel_filter(channel)) { |
| 184 | // Suggest only message types if the message type argument is being |
| 185 | // entered. |
| 186 | if (editing_message) { |
| 187 | // Then, filter for only channel names that match exactly and types |
| 188 | // that begin with message_type. |
| 189 | if (channel->name()->string_view() == channel_name && |
| 190 | channel->type()->string_view().find(message_type) == 0) { |
| 191 | std::cout << '\'' << channel->type()->c_str() << "' "; |
| 192 | } |
| 193 | } else if (channel->name()->string_view().find(channel_name) == 0) { |
| 194 | // If the message type empty, then return full autocomplete. |
| 195 | // Otherwise, since the message type is poulated yet not being edited, |
| 196 | // the user must be editing the channel name alone, in which case only |
| 197 | // suggest channel names, not pairs. |
| 198 | if (message_type.empty()) { |
| 199 | std::cout << '\'' << channel->name()->c_str() << ' ' |
| 200 | << channel->type()->c_str() << "' "; |
| 201 | } else { |
| 202 | std::cout << '\'' << channel->name()->c_str() << "' "; |
| 203 | } |
| 204 | } |
| 205 | } |
| 206 | } |
| 207 | } |
| 208 | std::cout << ')'; |
| 209 | } |
| 210 | |
Austin Schuh | 893d7f4 | 2022-09-16 15:01:35 -0700 | [diff] [blame] | 211 | void PrintMessage(const std::string_view node_name, const aos::Channel *channel, |
| 212 | const aos::Context &context, aos::FastStringBuilder *builder, |
| 213 | PrintOptions options) { |
| 214 | // Print the flatbuffer out to stdout, both to remove the |
| 215 | // unnecessary cruft from glog and to allow the user to readily |
| 216 | // redirect just the logged output independent of any debugging |
| 217 | // information on stderr. |
| 218 | |
| 219 | builder->Reset(); |
| 220 | |
| 221 | CHECK(flatbuffers::Verify(*channel->schema(), |
| 222 | *channel->schema()->root_table(), |
| 223 | static_cast<const uint8_t *>(context.data), |
| 224 | static_cast<size_t>(context.size))) |
| 225 | << ": Corrupted flatbuffer on " << channel->name()->c_str() << " " |
| 226 | << channel->type()->c_str(); |
| 227 | |
| 228 | aos::FlatbufferToJson( |
| 229 | builder, channel->schema(), static_cast<const uint8_t *>(context.data), |
| 230 | {options.pretty, static_cast<size_t>(options.max_vector_size), |
| 231 | options.pretty_max, options.use_hex}); |
| 232 | |
| 233 | if (options.json) { |
| 234 | std::cout << "{"; |
| 235 | if (!node_name.empty()) { |
| 236 | std::cout << "\"node\": \"" << node_name << "\", "; |
| 237 | } |
| 238 | std::cout << "\"monotonic_event_time\": "; |
| 239 | StreamSeconds(std::cout, context.monotonic_event_time); |
| 240 | std::cout << ", \"realtime_event_time\": \"" << context.realtime_event_time |
| 241 | << "\", "; |
| 242 | |
| 243 | if (context.monotonic_remote_time != context.monotonic_event_time) { |
| 244 | std::cout << "\"monotonic_remote_time\": "; |
| 245 | StreamSeconds(std::cout, context.monotonic_remote_time); |
| 246 | std::cout << ", \"realtime_remote_time\": \"" |
| 247 | << context.realtime_remote_time << "\", "; |
| 248 | } |
| 249 | |
| 250 | std::cout << "\"channel\": " |
| 251 | << aos::configuration::StrippedChannelToString(channel) |
Naman Gupta | 5404721 | 2022-09-23 14:10:30 -0700 | [diff] [blame^] | 252 | << ", \"data\": " << *builder << "}"; |
Austin Schuh | 893d7f4 | 2022-09-16 15:01:35 -0700 | [diff] [blame] | 253 | } else { |
| 254 | if (!node_name.empty()) { |
| 255 | std::cout << node_name << " "; |
| 256 | } |
| 257 | |
| 258 | if (options.print_timestamps) { |
| 259 | if (context.monotonic_remote_time != context.monotonic_event_time) { |
| 260 | std::cout << context.realtime_event_time << " (" |
| 261 | << context.monotonic_event_time << ") sent " |
| 262 | << context.realtime_remote_time << " (" |
| 263 | << context.monotonic_remote_time << ") " |
| 264 | << channel->name()->c_str() << ' ' << channel->type()->c_str() |
Naman Gupta | 5404721 | 2022-09-23 14:10:30 -0700 | [diff] [blame^] | 265 | << ": " << *builder; |
Austin Schuh | 893d7f4 | 2022-09-16 15:01:35 -0700 | [diff] [blame] | 266 | } else { |
| 267 | std::cout << context.realtime_event_time << " (" |
| 268 | << context.monotonic_event_time << ") " |
| 269 | << channel->name()->c_str() << ' ' << channel->type()->c_str() |
Naman Gupta | 5404721 | 2022-09-23 14:10:30 -0700 | [diff] [blame^] | 270 | << ": " << *builder; |
Austin Schuh | 893d7f4 | 2022-09-16 15:01:35 -0700 | [diff] [blame] | 271 | } |
| 272 | } else { |
Naman Gupta | 5404721 | 2022-09-23 14:10:30 -0700 | [diff] [blame^] | 273 | std::cout << *builder; |
Austin Schuh | 893d7f4 | 2022-09-16 15:01:35 -0700 | [diff] [blame] | 274 | } |
| 275 | } |
| 276 | } |
| 277 | |
| 278 | void PrintMessage(const aos::Channel *channel, const aos::Context &context, |
| 279 | aos::FastStringBuilder *builder, PrintOptions options) { |
| 280 | PrintMessage("", channel, context, builder, options); |
| 281 | } |
| 282 | |
| 283 | void PrintMessage(const std::string_view node_name, |
| 284 | aos::NodeEventLoopFactory *node_factory, |
| 285 | const aos::Channel *channel, const aos::Context &context, |
| 286 | aos::FastStringBuilder *builder, PrintOptions options) { |
| 287 | if (!options.json && options.distributed_clock) { |
| 288 | std::cout << node_factory->ToDistributedClock(context.monotonic_event_time) |
| 289 | << " "; |
| 290 | } |
| 291 | PrintMessage(node_name, channel, context, builder, options); |
| 292 | } |
| 293 | |
Naman Gupta | 5404721 | 2022-09-23 14:10:30 -0700 | [diff] [blame^] | 294 | Printer::Printer(PrintOptions options, bool flush) |
| 295 | : options_(options), flush_(flush) { |
| 296 | if (options_.json) { |
| 297 | std::cout << "["; |
| 298 | } |
| 299 | } |
| 300 | |
| 301 | Printer::~Printer() { |
| 302 | if (options_.json) { |
| 303 | if (message_count_ > 0) { |
| 304 | std::cout << "\n]\n"; |
| 305 | } else { |
| 306 | std::cout << "]\n"; |
| 307 | } |
| 308 | } |
| 309 | } |
| 310 | |
| 311 | void Printer::PrintMessage(const std::string_view node_name, |
| 312 | aos::NodeEventLoopFactory *node_factory, |
| 313 | const aos::Channel *channel, |
| 314 | const aos::Context &context) { |
| 315 | if (options_.json) { |
| 316 | if (message_count_ != 0) { |
| 317 | std::cout << ",\n "; |
| 318 | } else { |
| 319 | std::cout << "\n "; |
| 320 | } |
| 321 | } |
| 322 | |
| 323 | aos::PrintMessage(node_name, node_factory, channel, context, &str_builder_, |
| 324 | options_); |
| 325 | |
| 326 | if (!options_.json) { |
| 327 | if (flush_) { |
| 328 | std::cout << std::endl; |
| 329 | } else { |
| 330 | std::cout << "\n"; |
| 331 | } |
| 332 | } |
| 333 | ++message_count_; |
| 334 | } |
| 335 | |
| 336 | void Printer::PrintMessage(const aos::Channel *channel, |
| 337 | const aos::Context &context) { |
| 338 | if (options_.json) { |
| 339 | if (message_count_ != 0) { |
| 340 | std::cout << ",\n "; |
| 341 | } else { |
| 342 | std::cout << "\n "; |
| 343 | } |
| 344 | } |
| 345 | |
| 346 | aos::PrintMessage(channel, context, &str_builder_, options_); |
| 347 | |
| 348 | if (!options_.json) { |
| 349 | if (flush_) { |
| 350 | std::cout << std::endl; |
| 351 | } else { |
| 352 | std::cout << "\n"; |
| 353 | } |
| 354 | } |
| 355 | ++message_count_; |
| 356 | } |
| 357 | |
Brian Silverman | ea2c95f | 2021-02-10 18:10:26 -0800 | [diff] [blame] | 358 | } // namespace aos |