Eric Schmiedeberg | 7055b7e | 2022-07-19 10:04:58 -0600 | [diff] [blame] | 1 | #include <poll.h> |
Brian Silverman | ea2c95f | 2021-02-10 18:10:26 -0800 | [diff] [blame] | 2 | #include <unistd.h> |
| 3 | |
| 4 | #include <iostream> |
| 5 | |
Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame^] | 6 | #include "absl/flags/flag.h" |
| 7 | #include "absl/flags/usage.h" |
| 8 | #include "absl/log/log.h" |
Philipp Schrader | 790cb54 | 2023-07-05 21:06:52 -0700 | [diff] [blame] | 9 | |
Brian Silverman | ea2c95f | 2021-02-10 18:10:26 -0800 | [diff] [blame] | 10 | #include "aos/aos_cli_utils.h" |
| 11 | #include "aos/configuration.h" |
| 12 | #include "aos/init.h" |
| 13 | #include "aos/json_to_flatbuffer.h" |
Brian Silverman | ea2c95f | 2021-02-10 18:10:26 -0800 | [diff] [blame] | 14 | |
Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame^] | 15 | ABSL_FLAG(double, rate, -1, |
| 16 | "Rate at which to send the message (-1 to send once)."); |
James Kuszmaul | 27ff5ac | 2022-01-21 11:43:48 -0800 | [diff] [blame] | 17 | |
Brian Silverman | ea2c95f | 2021-02-10 18:10:26 -0800 | [diff] [blame] | 18 | int main(int argc, char **argv) { |
Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame^] | 19 | absl::SetProgramUsageMessage( |
Brian Silverman | ea2c95f | 2021-02-10 18:10:26 -0800 | [diff] [blame] | 20 | "Sends messages on arbitrary channels.\n" |
| 21 | "Typical Usage: aos_send [--config path_to_config.json]" |
| 22 | " channel_name message_type '{\"foo\": \"bar\"}'\n" |
| 23 | "Example usage: aos_send /test aos.examples.Ping " |
Eric Schmiedeberg | 7055b7e | 2022-07-19 10:04:58 -0600 | [diff] [blame] | 24 | "'{\"value\": 1}'\n" |
| 25 | "Pipe usage: cat ping.json | aos_send /test/ aos.examples.Ping -"); |
Brian Silverman | ea2c95f | 2021-02-10 18:10:26 -0800 | [diff] [blame] | 26 | aos::InitGoogle(&argc, &argv); |
| 27 | |
| 28 | aos::CliUtilInfo cli_info; |
Austin Schuh | 59f3b0f | 2021-07-31 20:50:40 -0700 | [diff] [blame] | 29 | if (cli_info.Initialize( |
| 30 | &argc, &argv, |
| 31 | [&cli_info](const aos::Channel *channel) { |
| 32 | return aos::configuration::ChannelIsSendableOnNode( |
| 33 | channel, cli_info.event_loop->node()); |
| 34 | }, |
Austin Schuh | ba2c865 | 2022-08-17 14:56:08 -0700 | [diff] [blame] | 35 | "channel is sendable on node", false)) { |
Brian Silverman | ea2c95f | 2021-02-10 18:10:26 -0800 | [diff] [blame] | 36 | return 0; |
| 37 | } |
| 38 | if (cli_info.found_channels.size() > 1) { |
| 39 | LOG(FATAL) << "Matched multiple channels, but may only send on 1"; |
| 40 | } |
| 41 | |
| 42 | if (argc == 1) { |
| 43 | LOG(FATAL) << "Must specify a message to send"; |
| 44 | } |
| 45 | |
Eric Schmiedeberg | 7055b7e | 2022-07-19 10:04:58 -0600 | [diff] [blame] | 46 | // Check if the user wants to use stdin (denoted by '-') or the argument |
| 47 | // present in argv for the message to send. CliUtilInfo will ensure the |
| 48 | // message data will be in argv[1] |
| 49 | std::string_view message_to_send{argv[1]}; |
| 50 | std::string stdin_data; |
| 51 | if (message_to_send == "-") { |
| 52 | // Read in everything from stdin, blocks when there's no data on stdin |
| 53 | stdin_data = std::string(std::istreambuf_iterator(std::cin), {}); |
| 54 | message_to_send = stdin_data; |
| 55 | } |
| 56 | |
Brian Silverman | ea2c95f | 2021-02-10 18:10:26 -0800 | [diff] [blame] | 57 | const aos::Channel *const channel = cli_info.found_channels[0]; |
| 58 | const std::unique_ptr<aos::RawSender> sender = |
| 59 | cli_info.event_loop->MakeRawSender(channel); |
| 60 | flatbuffers::FlatBufferBuilder fbb(sender->fbb_allocator()->size(), |
| 61 | sender->fbb_allocator()); |
Austin Schuh | a36cfa8 | 2021-03-20 21:56:03 -0700 | [diff] [blame] | 62 | fbb.ForceDefaults(true); |
Austin Schuh | e54988c | 2024-04-30 12:14:02 -0700 | [diff] [blame] | 63 | flatbuffers::Offset<flatbuffers::Table> msg_offset = |
| 64 | aos::JsonToFlatbuffer(message_to_send, channel->schema(), &fbb); |
| 65 | |
| 66 | if (msg_offset.IsNull()) { |
| 67 | return 1; |
| 68 | } |
| 69 | |
| 70 | fbb.Finish(msg_offset); |
James Kuszmaul | 27ff5ac | 2022-01-21 11:43:48 -0800 | [diff] [blame] | 71 | |
Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame^] | 72 | if (absl::GetFlag(FLAGS_rate) < 0) { |
James Kuszmaul | 27ff5ac | 2022-01-21 11:43:48 -0800 | [diff] [blame] | 73 | sender->CheckOk(sender->Send(fbb.GetSize())); |
| 74 | } else { |
| 75 | cli_info.event_loop |
| 76 | ->AddTimer([&fbb, &sender]() { |
| 77 | sender->CheckOk(sender->Send(fbb.GetBufferPointer(), fbb.GetSize())); |
| 78 | }) |
Philipp Schrader | a671252 | 2023-07-05 20:25:11 -0700 | [diff] [blame] | 79 | ->Schedule(cli_info.event_loop->monotonic_now(), |
| 80 | std::chrono::duration_cast<std::chrono::nanoseconds>( |
Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame^] | 81 | std::chrono::duration<double>( |
| 82 | 1.0 / absl::GetFlag(FLAGS_rate)))); |
James Kuszmaul | 27ff5ac | 2022-01-21 11:43:48 -0800 | [diff] [blame] | 83 | cli_info.event_loop->Run(); |
| 84 | } |
Brian Silverman | ea2c95f | 2021-02-10 18:10:26 -0800 | [diff] [blame] | 85 | |
| 86 | return 0; |
| 87 | } |