blob: c2ed056b4d362ea4abb7a0a458f3cf0c754c1664 [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);
28
29 if (argc == 1) {
30 std::cout << "Channels:\n";
31 for (const aos::Channel *channel : *config_msg->channels()) {
32 std::cout << channel->name()->c_str() << ' ' << channel->type()->c_str()
33 << '\n';
34 }
35 return 0;
36 }
37
38 int found_channels = 0;
39 const flatbuffers::Vector<flatbuffers::Offset<aos::Channel>> *channels =
40 config_msg->channels();
41 for (const aos::Channel *channel : *channels) {
42 if (channel->name()->c_str() == channel_name &&
43 channel->type()->str().find(message_type) != std::string::npos) {
44 event_loop.MakeRawWatcher(
45 channel,
46 [channel](const aos::Context /* &context*/, const void *message) {
47 LOG(INFO) << '(' << channel->type()->c_str() << ") "
48 << aos::FlatbufferToJson(
49 channel->schema(),
50 static_cast<const uint8_t *>(message))
51 << '\n';
52 });
53 found_channels++;
54 }
55 }
56
57 if (found_channels == 0) {
58 LOG(FATAL) << "Could not find any channels with the given name and type.";
59 } else if (found_channels > 1 && message_type.size() != 0) {
60 LOG(FATAL) << "Multiple channels found with same type";
61 }
62
63 event_loop.Run();
64 ::aos::Cleanup();
65 return 0;
66}