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"); |
Austin Schuh | 15649d6 | 2019-12-28 16:36:38 -0800 | [diff] [blame] | 13 | |
Tyler Chatow | 5e369a4 | 2019-11-23 11:57:31 -0800 | [diff] [blame] | 14 | int main(int argc, char **argv) { |
| 15 | aos::InitGoogle(&argc, &argv); |
| 16 | |
| 17 | std::string channel_name; |
| 18 | std::string message_type; |
| 19 | if (argc > 1) { |
| 20 | channel_name = argv[1]; |
| 21 | } |
| 22 | if (argc > 2) { |
| 23 | message_type = argv[2]; |
| 24 | } |
| 25 | |
| 26 | aos::FlatbufferDetachedBuffer<aos::Configuration> config = |
| 27 | aos::configuration::ReadConfig(FLAGS_config); |
| 28 | |
| 29 | const aos::Configuration *config_msg = &config.message(); |
| 30 | ::aos::ShmEventLoop event_loop(config_msg); |
James Kuszmaul | 5c22e08 | 2019-12-14 20:43:07 -0800 | [diff] [blame] | 31 | event_loop.SkipTimingReport(); |
Tyler Chatow | 67ddb03 | 2020-01-12 14:30:04 -0800 | [diff] [blame] | 32 | event_loop.SkipAosLog(); |
Tyler Chatow | 5e369a4 | 2019-11-23 11:57:31 -0800 | [diff] [blame] | 33 | |
| 34 | if (argc == 1) { |
| 35 | std::cout << "Channels:\n"; |
| 36 | for (const aos::Channel *channel : *config_msg->channels()) { |
| 37 | std::cout << channel->name()->c_str() << ' ' << channel->type()->c_str() |
| 38 | << '\n'; |
| 39 | } |
| 40 | return 0; |
| 41 | } |
| 42 | |
| 43 | int found_channels = 0; |
| 44 | const flatbuffers::Vector<flatbuffers::Offset<aos::Channel>> *channels = |
| 45 | config_msg->channels(); |
| 46 | for (const aos::Channel *channel : *channels) { |
| 47 | if (channel->name()->c_str() == channel_name && |
| 48 | channel->type()->str().find(message_type) != std::string::npos) { |
| 49 | event_loop.MakeRawWatcher( |
Austin Schuh | 15649d6 | 2019-12-28 16:36:38 -0800 | [diff] [blame] | 50 | channel, [channel](const aos::Context &context, const void *message) { |
| 51 | // Print the flatbuffer out to stdout, both to remove the |
| 52 | // unnecessary cruft from glog and to allow the user to readily |
| 53 | // redirect just the logged output independent of any debugging |
| 54 | // information on stderr. |
| 55 | if (context.monotonic_remote_time != context.monotonic_event_time) { |
| 56 | std::cout << context.realtime_remote_time << " (" |
| 57 | << context.monotonic_remote_time << ") delivered " |
| 58 | << context.realtime_event_time << " (" |
| 59 | << context.monotonic_event_time << "): " |
| 60 | << aos::FlatbufferToJson( |
| 61 | channel->schema(), |
Austin Schuh | d393620 | 2020-04-07 20:11:07 -0700 | [diff] [blame^] | 62 | static_cast<const uint8_t *>(message), |
| 63 | FLAGS_max_vector_size) |
Austin Schuh | 15649d6 | 2019-12-28 16:36:38 -0800 | [diff] [blame] | 64 | << '\n'; |
| 65 | } else { |
| 66 | std::cout << context.realtime_event_time << " (" |
| 67 | << context.monotonic_event_time << "): " |
| 68 | << aos::FlatbufferToJson( |
| 69 | channel->schema(), |
Austin Schuh | d393620 | 2020-04-07 20:11:07 -0700 | [diff] [blame^] | 70 | static_cast<const uint8_t *>(message), |
| 71 | FLAGS_max_vector_size) |
Austin Schuh | 15649d6 | 2019-12-28 16:36:38 -0800 | [diff] [blame] | 72 | << '\n'; |
| 73 | } |
Tyler Chatow | 5e369a4 | 2019-11-23 11:57:31 -0800 | [diff] [blame] | 74 | }); |
| 75 | found_channels++; |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | if (found_channels == 0) { |
| 80 | LOG(FATAL) << "Could not find any channels with the given name and type."; |
| 81 | } else if (found_channels > 1 && message_type.size() != 0) { |
| 82 | LOG(FATAL) << "Multiple channels found with same type"; |
| 83 | } |
| 84 | |
| 85 | event_loop.Run(); |
| 86 | ::aos::Cleanup(); |
| 87 | return 0; |
| 88 | } |