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 | |
| 75 | int found_channels = 0; |
| 76 | const flatbuffers::Vector<flatbuffers::Offset<aos::Channel>> *channels = |
| 77 | config_msg->channels(); |
| 78 | for (const aos::Channel *channel : *channels) { |
| 79 | if (channel->name()->c_str() == channel_name && |
| 80 | channel->type()->str().find(message_type) != std::string::npos) { |
Brian Silverman | 1bc2e96 | 2020-04-28 15:22:01 -0700 | [diff] [blame] | 81 | if (FLAGS_fetch) { |
| 82 | const std::unique_ptr<aos::RawFetcher> fetcher = |
| 83 | event_loop.MakeRawFetcher(channel); |
| 84 | if (fetcher->Fetch()) { |
| 85 | PrintMessage(channel, fetcher->context()); |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | event_loop.MakeRawWatcher(channel, [channel](const aos::Context &context, |
| 90 | const void * /*message*/) { |
| 91 | PrintMessage(channel, context); |
| 92 | }); |
| 93 | |
Tyler Chatow | 5e369a4 | 2019-11-23 11:57:31 -0800 | [diff] [blame] | 94 | found_channels++; |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | if (found_channels == 0) { |
| 99 | LOG(FATAL) << "Could not find any channels with the given name and type."; |
| 100 | } else if (found_channels > 1 && message_type.size() != 0) { |
| 101 | LOG(FATAL) << "Multiple channels found with same type"; |
| 102 | } |
| 103 | |
| 104 | event_loop.Run(); |
| 105 | ::aos::Cleanup(); |
| 106 | return 0; |
| 107 | } |