blob: 8d6c2b18ca97464b8934355e096db21a05c56d0a [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 Schuh15649d62019-12-28 16:36:38 -080011
Tyler Chatow5e369a42019-11-23 11:57:31 -080012int 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 Kuszmaul5c22e082019-12-14 20:43:07 -080029 event_loop.SkipTimingReport();
Tyler Chatow5e369a42019-11-23 11:57:31 -080030
31 if (argc == 1) {
32 std::cout << "Channels:\n";
33 for (const aos::Channel *channel : *config_msg->channels()) {
34 std::cout << channel->name()->c_str() << ' ' << channel->type()->c_str()
35 << '\n';
36 }
37 return 0;
38 }
39
40 int found_channels = 0;
41 const flatbuffers::Vector<flatbuffers::Offset<aos::Channel>> *channels =
42 config_msg->channels();
43 for (const aos::Channel *channel : *channels) {
44 if (channel->name()->c_str() == channel_name &&
45 channel->type()->str().find(message_type) != std::string::npos) {
46 event_loop.MakeRawWatcher(
Austin Schuh15649d62019-12-28 16:36:38 -080047 channel, [channel](const aos::Context &context, const void *message) {
48 // Print the flatbuffer out to stdout, both to remove the
49 // unnecessary cruft from glog and to allow the user to readily
50 // redirect just the logged output independent of any debugging
51 // information on stderr.
52 if (context.monotonic_remote_time != context.monotonic_event_time) {
53 std::cout << context.realtime_remote_time << " ("
54 << context.monotonic_remote_time << ") delivered "
55 << context.realtime_event_time << " ("
56 << context.monotonic_event_time << "): "
57 << aos::FlatbufferToJson(
58 channel->schema(),
59 static_cast<const uint8_t *>(message))
60 << '\n';
61 } else {
62 std::cout << context.realtime_event_time << " ("
63 << context.monotonic_event_time << "): "
64 << aos::FlatbufferToJson(
65 channel->schema(),
66 static_cast<const uint8_t *>(message))
67 << '\n';
68 }
Tyler Chatow5e369a42019-11-23 11:57:31 -080069 });
70 found_channels++;
71 }
72 }
73
74 if (found_channels == 0) {
75 LOG(FATAL) << "Could not find any channels with the given name and type.";
76 } else if (found_channels > 1 && message_type.size() != 0) {
77 LOG(FATAL) << "Multiple channels found with same type";
78 }
79
80 event_loop.Run();
81 ::aos::Cleanup();
82 return 0;
83}