blob: d94418a8d39c385510aee5c03b9b6b3d1883ada7 [file] [log] [blame]
Brian Silvermanea2c95f2021-02-10 18:10:26 -08001#include <unistd.h>
2
3#include <iostream>
4
5#include "aos/aos_cli_utils.h"
6#include "aos/configuration.h"
7#include "aos/init.h"
8#include "aos/json_to_flatbuffer.h"
9#include "gflags/gflags.h"
10#include "glog/logging.h"
11
12int main(int argc, char **argv) {
13 gflags::SetUsageMessage(
14 "Sends messages on arbitrary channels.\n"
15 "Typical Usage: aos_send [--config path_to_config.json]"
16 " channel_name message_type '{\"foo\": \"bar\"}'\n"
17 "Example usage: aos_send /test aos.examples.Ping "
18 "'{\"value\": 1}'");
19 aos::InitGoogle(&argc, &argv);
20
21 aos::CliUtilInfo cli_info;
22 if (cli_info.Initialize(&argc, &argv,
23 [&cli_info](const aos::Channel *channel) {
24 return aos::configuration::ChannelIsSendableOnNode(
25 channel, cli_info.event_loop->node());
26 })) {
27 return 0;
28 }
29 if (cli_info.found_channels.size() > 1) {
30 LOG(FATAL) << "Matched multiple channels, but may only send on 1";
31 }
32
33 if (argc == 1) {
34 LOG(FATAL) << "Must specify a message to send";
35 }
36
37 const aos::Channel *const channel = cli_info.found_channels[0];
38 const std::unique_ptr<aos::RawSender> sender =
39 cli_info.event_loop->MakeRawSender(channel);
40 flatbuffers::FlatBufferBuilder fbb(sender->fbb_allocator()->size(),
41 sender->fbb_allocator());
Austin Schuha36cfa82021-03-20 21:56:03 -070042 fbb.ForceDefaults(true);
Brian Silvermanea2c95f2021-02-10 18:10:26 -080043 fbb.Finish(aos::JsonToFlatbuffer(std::string_view(argv[1]), channel->schema(),
44 &fbb));
45 sender->Send(fbb.GetSize());
46
47 return 0;
48}