blob: e7d9d7cbe15707cf059fe0696127ec05f723b5f1 [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");
11int main(int argc, char **argv) {
12 aos::InitGoogle(&argc, &argv);
13
14 std::string channel_name;
15 std::string message_type;
16 if (argc > 1) {
17 channel_name = argv[1];
18 }
19 if (argc > 2) {
20 message_type = argv[2];
21 }
22
23 aos::FlatbufferDetachedBuffer<aos::Configuration> config =
24 aos::configuration::ReadConfig(FLAGS_config);
25
26 const aos::Configuration *config_msg = &config.message();
27 ::aos::ShmEventLoop event_loop(config_msg);
James Kuszmaul5c22e082019-12-14 20:43:07 -080028 event_loop.SkipTimingReport();
Tyler Chatow5e369a42019-11-23 11:57:31 -080029
30 if (argc == 1) {
31 std::cout << "Channels:\n";
32 for (const aos::Channel *channel : *config_msg->channels()) {
33 std::cout << channel->name()->c_str() << ' ' << channel->type()->c_str()
34 << '\n';
35 }
36 return 0;
37 }
38
39 int found_channels = 0;
40 const flatbuffers::Vector<flatbuffers::Offset<aos::Channel>> *channels =
41 config_msg->channels();
42 for (const aos::Channel *channel : *channels) {
43 if (channel->name()->c_str() == channel_name &&
44 channel->type()->str().find(message_type) != std::string::npos) {
45 event_loop.MakeRawWatcher(
46 channel,
47 [channel](const aos::Context /* &context*/, const void *message) {
48 LOG(INFO) << '(' << channel->type()->c_str() << ") "
49 << aos::FlatbufferToJson(
50 channel->schema(),
51 static_cast<const uint8_t *>(message))
52 << '\n';
53 });
54 found_channels++;
55 }
56 }
57
58 if (found_channels == 0) {
59 LOG(FATAL) << "Could not find any channels with the given name and type.";
60 } else if (found_channels > 1 && message_type.size() != 0) {
61 LOG(FATAL) << "Multiple channels found with same type";
62 }
63
64 event_loop.Run();
65 ::aos::Cleanup();
66 return 0;
67}