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