blob: 62fb5cb8895f07cb6f18a90b7aba8f6a4176dd2a [file] [log] [blame]
Tyler Chatow5e369a42019-11-23 11:57:31 -08001#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
10DEFINE_string(config, "./config.json", "File path of aos configuration");
Austin Schuhd3936202020-04-07 20:11:07 -070011DEFINE_int32(max_vector_size, 100,
12 "If positive, vectors longer than this will not be printed");
Brian Silverman1bc2e962020-04-28 15:22:01 -070013DEFINE_bool(fetch, false,
14 "If true, fetch the current message on the channel first");
15
16namespace {
17
Tyler Chatowfcf16f42020-07-26 12:41:36 -070018void PrintMessage(const aos::Channel *channel, const aos::Context &context,
19 aos::FastStringBuilder *builder) {
Brian Silverman1bc2e962020-04-28 15:22:01 -070020 // 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 Chatowfcf16f42020-07-26 12:41:36 -070024
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 Silverman1bc2e962020-04-28 15:22:01 -070030 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 Chatowfcf16f42020-07-26 12:41:36 -070034 << context.monotonic_event_time << "): " << *builder << '\n';
Brian Silverman1bc2e962020-04-28 15:22:01 -070035 } else {
36 std::cout << context.realtime_event_time << " ("
Tyler Chatowfcf16f42020-07-26 12:41:36 -070037 << context.monotonic_event_time << "): " << *builder << '\n';
Brian Silverman1bc2e962020-04-28 15:22:01 -070038 }
39}
40
41} // namespace
Austin Schuh15649d62019-12-28 16:36:38 -080042
Tyler Chatow5e369a42019-11-23 11:57:31 -080043int 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 Kuszmaul5c22e082019-12-14 20:43:07 -080060 event_loop.SkipTimingReport();
Tyler Chatow67ddb032020-01-12 14:30:04 -080061 event_loop.SkipAosLog();
Tyler Chatow5e369a42019-11-23 11:57:31 -080062
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 Silverman83ff9c12020-06-23 16:20:27 -070072 std::vector<const aos::Channel *> found_channels;
Tyler Chatow5e369a42019-11-23 11:57:31 -080073 const flatbuffers::Vector<flatbuffers::Offset<aos::Channel>> *channels =
74 config_msg->channels();
Brian Silverman83ff9c12020-06-23 16:20:27 -070075 bool found_exact = false;
Tyler Chatow5e369a42019-11-23 11:57:31 -080076 for (const aos::Channel *channel : *channels) {
Brian Silverman83ff9c12020-06-23 16:20:27 -070077 if (channel->name()->c_str() != channel_name) {
78 continue;
Tyler Chatow5e369a42019-11-23 11:57:31 -080079 }
Brian Silverman83ff9c12020-06-23 16:20:27 -070080 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 Chatow5e369a42019-11-23 11:57:31 -080091 }
92
Brian Silverman83ff9c12020-06-23 16:20:27 -070093 if (found_channels.empty()) {
Tyler Chatow5e369a42019-11-23 11:57:31 -080094 LOG(FATAL) << "Could not find any channels with the given name and type.";
Brian Silverman83ff9c12020-06-23 16:20:27 -070095 } else if (found_channels.size() > 1 && !message_type.empty()) {
Tyler Chatow5e369a42019-11-23 11:57:31 -080096 LOG(FATAL) << "Multiple channels found with same type";
97 }
98
Tyler Chatowfcf16f42020-07-26 12:41:36 -070099 aos::FastStringBuilder str_builder;
100
Brian Silverman83ff9c12020-06-23 16:20:27 -0700101 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 Chatowfcf16f42020-07-26 12:41:36 -0700106 PrintMessage(channel, fetcher->context(), &str_builder);
Brian Silverman83ff9c12020-06-23 16:20:27 -0700107 }
108 }
109
Tyler Chatowfcf16f42020-07-26 12:41:36 -0700110 event_loop.MakeRawWatcher(
111 channel, [channel, &str_builder](const aos::Context &context,
112 const void * /*message*/) {
113 PrintMessage(channel, context, &str_builder);
114 });
Brian Silverman83ff9c12020-06-23 16:20:27 -0700115 }
116
Tyler Chatow5e369a42019-11-23 11:57:31 -0800117 event_loop.Run();
Austin Schuhae87e312020-08-01 16:15:01 -0700118
Tyler Chatow5e369a42019-11-23 11:57:31 -0800119 return 0;
120}