blob: ffc7957cdca68cdd2e0a14f597380e4f561f7936 [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;
Austin Schuh59f3b0f2021-07-31 20:50:40 -070022 if (cli_info.Initialize(
23 &argc, &argv,
24 [&cli_info](const aos::Channel *channel) {
25 return aos::configuration::ChannelIsSendableOnNode(
26 channel, cli_info.event_loop->node());
27 },
28 false)) {
Brian Silvermanea2c95f2021-02-10 18:10:26 -080029 return 0;
30 }
31 if (cli_info.found_channels.size() > 1) {
32 LOG(FATAL) << "Matched multiple channels, but may only send on 1";
33 }
34
35 if (argc == 1) {
36 LOG(FATAL) << "Must specify a message to send";
37 }
38
39 const aos::Channel *const channel = cli_info.found_channels[0];
40 const std::unique_ptr<aos::RawSender> sender =
41 cli_info.event_loop->MakeRawSender(channel);
42 flatbuffers::FlatBufferBuilder fbb(sender->fbb_allocator()->size(),
43 sender->fbb_allocator());
Austin Schuha36cfa82021-03-20 21:56:03 -070044 fbb.ForceDefaults(true);
Brian Silvermanea2c95f2021-02-10 18:10:26 -080045 fbb.Finish(aos::JsonToFlatbuffer(std::string_view(argv[1]), channel->schema(),
46 &fbb));
milind1f1dca32021-07-03 13:50:07 -070047 sender->CheckOk(sender->Send(fbb.GetSize()));
Brian Silvermanea2c95f2021-02-10 18:10:26 -080048
49 return 0;
50}