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