Tyler Chatow | 5e369a4 | 2019-11-23 11:57:31 -0800 | [diff] [blame] | 1 | #include <iostream> |
| 2 | #include <map> |
| 3 | |
| 4 | #include "aos/configuration.h" |
| 5 | #include "aos/events/shm_event_loop.h" |
| 6 | #include "aos/init.h" |
| 7 | #include "aos/json_to_flatbuffer.h" |
| 8 | #include "gflags/gflags.h" |
| 9 | |
| 10 | DEFINE_string(config, "./config.json", "File path of aos configuration"); |
Austin Schuh | d393620 | 2020-04-07 20:11:07 -0700 | [diff] [blame] | 11 | DEFINE_int32(max_vector_size, 100, |
| 12 | "If positive, vectors longer than this will not be printed"); |
Brian Silverman | 1bc2e96 | 2020-04-28 15:22:01 -0700 | [diff] [blame] | 13 | DEFINE_bool(fetch, false, |
| 14 | "If true, fetch the current message on the channel first"); |
| 15 | |
| 16 | namespace { |
| 17 | |
Tyler Chatow | fcf16f4 | 2020-07-26 12:41:36 -0700 | [diff] [blame] | 18 | void PrintMessage(const aos::Channel *channel, const aos::Context &context, |
| 19 | aos::FastStringBuilder *builder) { |
Brian Silverman | 1bc2e96 | 2020-04-28 15:22:01 -0700 | [diff] [blame] | 20 | // Print the flatbuffer out to stdout, both to remove the |
| 21 | // unnecessary cruft from glog and to allow the user to readily |
| 22 | // redirect just the logged output independent of any debugging |
| 23 | // information on stderr. |
Tyler Chatow | fcf16f4 | 2020-07-26 12:41:36 -0700 | [diff] [blame] | 24 | |
| 25 | builder->Reset(); |
| 26 | aos::FlatbufferToJson(builder, channel->schema(), |
| 27 | static_cast<const uint8_t *>(context.data), |
| 28 | {false, static_cast<size_t>(FLAGS_max_vector_size)}); |
| 29 | |
Brian Silverman | 1bc2e96 | 2020-04-28 15:22:01 -0700 | [diff] [blame] | 30 | if (context.monotonic_remote_time != context.monotonic_event_time) { |
| 31 | std::cout << context.realtime_remote_time << " (" |
| 32 | << context.monotonic_remote_time << ") delivered " |
| 33 | << context.realtime_event_time << " (" |
Tyler Chatow | fcf16f4 | 2020-07-26 12:41:36 -0700 | [diff] [blame] | 34 | << context.monotonic_event_time << "): " << *builder << '\n'; |
Brian Silverman | 1bc2e96 | 2020-04-28 15:22:01 -0700 | [diff] [blame] | 35 | } else { |
| 36 | std::cout << context.realtime_event_time << " (" |
Tyler Chatow | fcf16f4 | 2020-07-26 12:41:36 -0700 | [diff] [blame] | 37 | << context.monotonic_event_time << "): " << *builder << '\n'; |
Brian Silverman | 1bc2e96 | 2020-04-28 15:22:01 -0700 | [diff] [blame] | 38 | } |
| 39 | } |
| 40 | |
| 41 | } // namespace |
Austin Schuh | 15649d6 | 2019-12-28 16:36:38 -0800 | [diff] [blame] | 42 | |
Tyler Chatow | 5e369a4 | 2019-11-23 11:57:31 -0800 | [diff] [blame] | 43 | int main(int argc, char **argv) { |
| 44 | aos::InitGoogle(&argc, &argv); |
| 45 | |
| 46 | std::string channel_name; |
| 47 | std::string message_type; |
| 48 | if (argc > 1) { |
| 49 | channel_name = argv[1]; |
| 50 | } |
| 51 | if (argc > 2) { |
| 52 | message_type = argv[2]; |
| 53 | } |
| 54 | |
| 55 | aos::FlatbufferDetachedBuffer<aos::Configuration> config = |
| 56 | aos::configuration::ReadConfig(FLAGS_config); |
| 57 | |
| 58 | const aos::Configuration *config_msg = &config.message(); |
| 59 | ::aos::ShmEventLoop event_loop(config_msg); |
James Kuszmaul | 5c22e08 | 2019-12-14 20:43:07 -0800 | [diff] [blame] | 60 | event_loop.SkipTimingReport(); |
Tyler Chatow | 67ddb03 | 2020-01-12 14:30:04 -0800 | [diff] [blame] | 61 | event_loop.SkipAosLog(); |
Tyler Chatow | 5e369a4 | 2019-11-23 11:57:31 -0800 | [diff] [blame] | 62 | |
| 63 | if (argc == 1) { |
| 64 | std::cout << "Channels:\n"; |
| 65 | for (const aos::Channel *channel : *config_msg->channels()) { |
| 66 | std::cout << channel->name()->c_str() << ' ' << channel->type()->c_str() |
| 67 | << '\n'; |
| 68 | } |
| 69 | return 0; |
| 70 | } |
| 71 | |
Brian Silverman | 83ff9c1 | 2020-06-23 16:20:27 -0700 | [diff] [blame] | 72 | std::vector<const aos::Channel *> found_channels; |
Tyler Chatow | 5e369a4 | 2019-11-23 11:57:31 -0800 | [diff] [blame] | 73 | const flatbuffers::Vector<flatbuffers::Offset<aos::Channel>> *channels = |
| 74 | config_msg->channels(); |
Brian Silverman | 83ff9c1 | 2020-06-23 16:20:27 -0700 | [diff] [blame] | 75 | bool found_exact = false; |
Tyler Chatow | 5e369a4 | 2019-11-23 11:57:31 -0800 | [diff] [blame] | 76 | for (const aos::Channel *channel : *channels) { |
Brian Silverman | 83ff9c1 | 2020-06-23 16:20:27 -0700 | [diff] [blame] | 77 | if (channel->name()->c_str() != channel_name) { |
| 78 | continue; |
Tyler Chatow | 5e369a4 | 2019-11-23 11:57:31 -0800 | [diff] [blame] | 79 | } |
Brian Silverman | 83ff9c1 | 2020-06-23 16:20:27 -0700 | [diff] [blame] | 80 | if (channel->type()->string_view() == message_type) { |
| 81 | if (!found_exact) { |
| 82 | found_channels.clear(); |
| 83 | found_exact = true; |
| 84 | } |
| 85 | } else if (!found_exact && channel->type()->string_view().find( |
| 86 | message_type) != std::string_view::npos) { |
| 87 | } else { |
| 88 | continue; |
| 89 | } |
| 90 | found_channels.push_back(channel); |
Tyler Chatow | 5e369a4 | 2019-11-23 11:57:31 -0800 | [diff] [blame] | 91 | } |
| 92 | |
Brian Silverman | 83ff9c1 | 2020-06-23 16:20:27 -0700 | [diff] [blame] | 93 | if (found_channels.empty()) { |
Tyler Chatow | 5e369a4 | 2019-11-23 11:57:31 -0800 | [diff] [blame] | 94 | LOG(FATAL) << "Could not find any channels with the given name and type."; |
Brian Silverman | 83ff9c1 | 2020-06-23 16:20:27 -0700 | [diff] [blame] | 95 | } else if (found_channels.size() > 1 && !message_type.empty()) { |
Tyler Chatow | 5e369a4 | 2019-11-23 11:57:31 -0800 | [diff] [blame] | 96 | LOG(FATAL) << "Multiple channels found with same type"; |
| 97 | } |
| 98 | |
Tyler Chatow | fcf16f4 | 2020-07-26 12:41:36 -0700 | [diff] [blame] | 99 | aos::FastStringBuilder str_builder; |
| 100 | |
Brian Silverman | 83ff9c1 | 2020-06-23 16:20:27 -0700 | [diff] [blame] | 101 | for (const aos::Channel *channel : found_channels) { |
| 102 | if (FLAGS_fetch) { |
| 103 | const std::unique_ptr<aos::RawFetcher> fetcher = |
| 104 | event_loop.MakeRawFetcher(channel); |
| 105 | if (fetcher->Fetch()) { |
Tyler Chatow | fcf16f4 | 2020-07-26 12:41:36 -0700 | [diff] [blame] | 106 | PrintMessage(channel, fetcher->context(), &str_builder); |
Brian Silverman | 83ff9c1 | 2020-06-23 16:20:27 -0700 | [diff] [blame] | 107 | } |
| 108 | } |
| 109 | |
Tyler Chatow | fcf16f4 | 2020-07-26 12:41:36 -0700 | [diff] [blame] | 110 | event_loop.MakeRawWatcher( |
| 111 | channel, [channel, &str_builder](const aos::Context &context, |
| 112 | const void * /*message*/) { |
| 113 | PrintMessage(channel, context, &str_builder); |
| 114 | }); |
Brian Silverman | 83ff9c1 | 2020-06-23 16:20:27 -0700 | [diff] [blame] | 115 | } |
| 116 | |
Tyler Chatow | 5e369a4 | 2019-11-23 11:57:31 -0800 | [diff] [blame] | 117 | event_loop.Run(); |
Austin Schuh | ae87e31 | 2020-08-01 16:15:01 -0700 | [diff] [blame^] | 118 | |
Tyler Chatow | 5e369a4 | 2019-11-23 11:57:31 -0800 | [diff] [blame] | 119 | return 0; |
| 120 | } |