Add an aos_send
It's basically the opposite of aos_dump.
Change-Id: I12fc53f8afbc3eef3ad4293d37b1a575e7a8d596
diff --git a/aos/BUILD b/aos/BUILD
index 7c976b1..8e2d8ac 100644
--- a/aos/BUILD
+++ b/aos/BUILD
@@ -7,6 +7,7 @@
srcs = [
"//aos:aos_dump",
"//aos:aos_dump_autocomplete.sh",
+ "//aos:aos_send",
"//aos/starter",
],
visibility = ["//visibility:public"],
@@ -23,10 +24,10 @@
filegroup(
name = "prime_binaries_stripped",
srcs = [
- # starter is hard coded to look for a non-stripped core...
"//aos:aos_dump.stripped",
- "//aos/starter",
"//aos:aos_dump_autocomplete.sh",
+ "//aos:aos_send.stripped",
+ "//aos/starter",
],
visibility = ["//visibility:public"],
)
@@ -488,6 +489,24 @@
],
)
+cc_library(
+ name = "aos_cli_utils",
+ srcs = [
+ "aos_cli_utils.cc",
+ ],
+ hdrs = [
+ "aos_cli_utils.h",
+ ],
+ target_compatible_with = ["@platforms//os:linux"],
+ visibility = ["//visibility:public"],
+ deps = [
+ ":configuration",
+ "//aos:init",
+ "//aos/events:shm_event_loop",
+ "@com_github_google_glog//:glog",
+ ],
+)
+
cc_binary(
name = "aos_dump",
srcs = [
@@ -496,10 +515,27 @@
target_compatible_with = ["@platforms//os:linux"],
visibility = ["//visibility:public"],
deps = [
+ ":aos_cli_utils",
":configuration",
":json_to_flatbuffer",
"//aos:init",
- "//aos/events:shm_event_loop",
+ "@com_github_google_glog//:glog",
+ ],
+)
+
+cc_binary(
+ name = "aos_send",
+ srcs = [
+ "aos_send.cc",
+ ],
+ target_compatible_with = ["@platforms//os:linux"],
+ visibility = ["//visibility:public"],
+ deps = [
+ ":aos_cli_utils",
+ ":configuration",
+ ":init",
+ ":json_to_flatbuffer",
+ "@com_github_gflags_gflags//:gflags",
"@com_github_google_glog//:glog",
],
)
diff --git a/aos/aos_cli_utils.cc b/aos/aos_cli_utils.cc
new file mode 100644
index 0000000..e36c3d2
--- /dev/null
+++ b/aos/aos_cli_utils.cc
@@ -0,0 +1,162 @@
+#include "aos/aos_cli_utils.h"
+
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <unistd.h>
+
+#include <iostream>
+
+DEFINE_string(config, "./config.json", "File path of aos configuration");
+
+DEFINE_bool(
+ _bash_autocomplete, false,
+ "Internal use: Outputs channel list for use with autocomplete script.");
+DEFINE_string(_bash_autocomplete_word, "",
+ "Internal use: Current word being autocompleted");
+
+DEFINE_bool(all, false,
+ "If true, print out the channels for all nodes, not just the "
+ "channels which are visible on this node.");
+
+namespace aos {
+namespace {
+
+bool EndsWith(std::string_view str, std::string_view ending) {
+ const std::size_t offset = str.size() - ending.size();
+ return str.size() >= ending.size() &&
+ std::equal(str.begin() + offset, str.end(), ending.begin(),
+ ending.end());
+}
+
+} // namespace
+
+bool CliUtilInfo::Initialize(
+ int *argc, char ***argv,
+ std::function<bool(const aos::Channel *)> channel_filter) {
+ // Don't generate failure output if the config doesn't exist while attempting
+ // to autocomplete.
+ if (struct stat file_stat;
+ FLAGS__bash_autocomplete &&
+ (!(EndsWith(FLAGS_config, ".json") || EndsWith(FLAGS_config, ".bfbs")) ||
+ stat(FLAGS_config.c_str(), &file_stat) != 0 ||
+ (file_stat.st_mode & S_IFMT) != S_IFREG)) {
+ std::cout << "COMPREPLY=()";
+ return true;
+ }
+
+ std::string channel_name;
+ std::string message_type;
+ if (*argc > 1) {
+ channel_name = (*argv)[1];
+ ShiftArgs(argc, argv);
+ }
+ if (*argc > 1) {
+ message_type = (*argv)[1];
+ ShiftArgs(argc, argv);
+ }
+
+ config.emplace(aos::configuration::ReadConfig(FLAGS_config));
+ event_loop.emplace(&config->message());
+ event_loop->SkipTimingReport();
+ event_loop->SkipAosLog();
+
+ if (FLAGS__bash_autocomplete) {
+ Autocomplete(channel_name, message_type, channel_filter);
+ return true;
+ }
+
+ const flatbuffers::Vector<flatbuffers::Offset<aos::Channel>> *const channels =
+ event_loop->configuration()->channels();
+ if (channel_name.empty() && message_type.empty()) {
+ std::cout << "Channels:\n";
+ for (const aos::Channel *channel : *channels) {
+ if (FLAGS_all || channel_filter(channel)) {
+ std::cout << channel->name()->c_str() << ' ' << channel->type()->c_str()
+ << '\n';
+ }
+ }
+ return true;
+ }
+
+ bool found_exact = false;
+ for (const aos::Channel *channel : *channels) {
+ if (!FLAGS_all && !channel_filter(channel)) {
+ continue;
+ }
+ if (channel->name()->c_str() != channel_name) {
+ continue;
+ }
+ if (channel->type()->string_view() == message_type) {
+ if (!found_exact) {
+ found_channels.clear();
+ found_exact = true;
+ }
+ } else if (!found_exact && channel->type()->string_view().find(
+ message_type) != std::string_view::npos) {
+ } else {
+ continue;
+ }
+ found_channels.push_back(channel);
+ }
+
+ if (found_channels.empty()) {
+ LOG(FATAL) << "Could not find any channels with the given name and type.";
+ } else if (found_channels.size() > 1 && !message_type.empty()) {
+ LOG(FATAL) << "Multiple channels found with same type";
+ }
+
+ return false;
+}
+
+void CliUtilInfo::Autocomplete(
+ std::string_view channel_name, std::string_view message_type,
+ std::function<bool(const aos::Channel *)> channel_filter) {
+ const aos::Configuration *const config_msg = event_loop->configuration();
+ const bool unique_match =
+ std::count_if(config_msg->channels()->begin(),
+ config_msg->channels()->end(),
+ [channel_name, message_type](const aos::Channel *channel) {
+ return channel->name()->string_view() == channel_name &&
+ channel->type()->string_view() == message_type;
+ }) == 1;
+
+ const bool editing_message =
+ !channel_name.empty() && FLAGS__bash_autocomplete_word == message_type;
+ const bool editing_channel =
+ !editing_message && FLAGS__bash_autocomplete_word == channel_name;
+
+ std::cout << "COMPREPLY=(";
+
+ // If we have a unique match, don't provide any suggestions. Otherwise, check
+ // that were're editing one of the two positional arguments.
+ if (!unique_match && (editing_message || editing_channel)) {
+ for (const aos::Channel *channel : *config_msg->channels()) {
+ if (FLAGS_all || channel_filter(channel)) {
+ // Suggest only message types if the message type argument is being
+ // entered.
+ if (editing_message) {
+ // Then, filter for only channel names that match exactly and types
+ // that begin with message_type.
+ if (channel->name()->string_view() == channel_name &&
+ channel->type()->string_view().find(message_type) == 0) {
+ std::cout << '\'' << channel->type()->c_str() << "' ";
+ }
+ } else if (channel->name()->string_view().find(channel_name) == 0) {
+ // If the message type empty, then return full autocomplete.
+ // Otherwise, since the message type is poulated yet not being edited,
+ // the user must be editing the channel name alone, in which case only
+ // suggest channel names, not pairs.
+ if (message_type.empty()) {
+ std::cout << '\'' << channel->name()->c_str() << ' '
+ << channel->type()->c_str() << "' ";
+ } else {
+ std::cout << '\'' << channel->name()->c_str() << "' ";
+ }
+ }
+ }
+ }
+ }
+ std::cout << ')';
+}
+
+} // namespace aos
diff --git a/aos/aos_cli_utils.h b/aos/aos_cli_utils.h
new file mode 100644
index 0000000..e158737
--- /dev/null
+++ b/aos/aos_cli_utils.h
@@ -0,0 +1,42 @@
+#ifndef AOS_AOS_CLI_UTILS_H_
+#define AOS_AOS_CLI_UTILS_H_
+
+#include "aos/configuration.h"
+#include "aos/events/shm_event_loop.h"
+#include "gflags/gflags.h"
+
+namespace aos {
+
+// The information needed by the main function of a CLI tool.
+struct CliUtilInfo {
+ // If this returns true, main should return immediately with 0.
+ // If this returns false, the other fields will be filled out appropriately.
+ // event_loop will be filled out before channel_filter is called.
+ bool Initialize(int *argc, char ***argv,
+ std::function<bool(const aos::Channel *)> channel_filter);
+
+ std::optional<aos::FlatbufferDetachedBuffer<aos::Configuration>> config;
+ std::optional<aos::ShmEventLoop> event_loop;
+ std::vector<const aos::Channel *> found_channels;
+
+ private:
+ // Generate eval command to populate autocomplete responses. Eval escapes
+ // spaces so channels are paired with their types. If a complete channel name
+ // is found, only autocompletes the type to avoid repeating arguments. Returns
+ // no autocomplete suggestions if a channel and type is found with the current
+ // arguments.
+ void Autocomplete(std::string_view channel_name,
+ std::string_view message_type,
+ std::function<bool(const aos::Channel *)> channel_filter);
+
+ void ShiftArgs(int *argc, char ***argv) {
+ for (int i = 1; i + 1 < *argc; ++i) {
+ (*argv)[i] = (*argv)[i + 1];
+ }
+ --*argc;
+ }
+};
+
+} // namespace aos
+
+#endif // AOS_AOS_CLI_UTILS_H_
diff --git a/aos/aos_dump.cc b/aos/aos_dump.cc
index 3484b73..78df645 100644
--- a/aos/aos_dump.cc
+++ b/aos/aos_dump.cc
@@ -1,35 +1,25 @@
#include <unistd.h>
#include <iostream>
-#include <map>
+#include "aos/aos_cli_utils.h"
#include "aos/configuration.h"
-#include "aos/events/shm_event_loop.h"
#include "aos/init.h"
#include "aos/json_to_flatbuffer.h"
#include "gflags/gflags.h"
-DEFINE_string(config, "./config.json", "File path of aos configuration");
DEFINE_int32(max_vector_size, 100,
"If positive, vectors longer than this will not be printed");
DEFINE_bool(fetch, false,
"If true, fetch the current message on the channel first");
DEFINE_bool(pretty, false,
"If true, pretty print the messages on multiple lines");
-DEFINE_bool(all, false,
- "If true, print out the channels for all nodes, not just the "
- "channels which are visible on this node.");
DEFINE_bool(print_timestamps, true, "If true, timestamps are printed.");
DEFINE_uint64(count, 0,
"If >0, aos_dump will exit after printing this many messages.");
DEFINE_int32(rate_limit, 0,
"The minimum amount of time to wait in milliseconds before "
"sending another message");
-DEFINE_bool(
- _bash_autocomplete, false,
- "Internal use: Outputs channel list for use with autocomplete script.");
-DEFINE_string(_bash_autocomplete_word, "",
- "Intenal use: Current word being autocompleted");
namespace {
@@ -60,70 +50,6 @@
}
}
-// Generate eval command to populate autocomplete responses. Eval escapes spaces
-// so channels are paired with their types. If a complete channel name is found,
-// only autocompletes the type to avoid repeating arguments. Returns no
-// autocomplete suggestions if a channel and type is found with the current
-// arguments.
-void Autocomplete(const aos::Configuration *config_msg,
- const aos::ShmEventLoop &event_loop,
- std::string_view channel_name,
- std::string_view message_type) {
- const bool unique_match =
- std::count_if(config_msg->channels()->begin(),
- config_msg->channels()->end(),
- [channel_name, message_type](const aos::Channel *channel) {
- return channel->name()->string_view() == channel_name &&
- channel->type()->string_view() == message_type;
- }) == 1;
-
- const bool editing_message =
- !channel_name.empty() && FLAGS__bash_autocomplete_word == message_type;
- const bool editing_channel =
- !editing_message && FLAGS__bash_autocomplete_word == channel_name;
-
- std::cout << "COMPREPLY=(";
-
- // If we have a unique match, don't provide any suggestions. Otherwise, check
- // that were're editing one of the two positional arguments.
- if (!unique_match && (editing_message || editing_channel)) {
- for (const aos::Channel *channel : *config_msg->channels()) {
- if (FLAGS_all || aos::configuration::ChannelIsReadableOnNode(
- channel, event_loop.node())) {
- // Suggest only message types if the message type argument is being
- // entered.
- if (editing_message) {
- // Then, filter for only channel names that match exactly and types
- // that begin with message_type.
- if (channel->name()->string_view() == channel_name &&
- channel->type()->string_view().find(message_type) == 0) {
- std::cout << '\'' << channel->type()->c_str() << "' ";
- }
- } else if (channel->name()->string_view().find(channel_name) == 0) {
- // If the message type empty, then return full autocomplete.
- // Otherwise, since the message type is poulated yet not being edited,
- // the user must be editing the channel name alone, in which case only
- // suggest channel names, not pairs.
- if (message_type.empty()) {
- std::cout << '\'' << channel->name()->c_str() << ' '
- << channel->type()->c_str() << "' ";
- } else {
- std::cout << '\'' << channel->name()->c_str() << "' ";
- }
- }
- }
- }
- }
- std::cout << ')';
-}
-
-bool EndsWith(std::string_view str, std::string_view ending) {
- const std::size_t offset = str.size() - ending.size();
- return str.size() >= ending.size() &&
- std::equal(str.begin() + offset, str.end(), ending.begin(),
- ending.end());
-}
-
} // namespace
int main(int argc, char **argv) {
@@ -135,92 +61,25 @@
"/test aos.examples.Ping");
aos::InitGoogle(&argc, &argv);
- // Don't generate failure output if the config doesn't exist while attempting
- // to autocomplete.
- if (struct stat file_stat;
- FLAGS__bash_autocomplete &&
- (!(EndsWith(FLAGS_config, ".json") || EndsWith(FLAGS_config, ".bfbs")) ||
- stat(FLAGS_config.c_str(), &file_stat) != 0 ||
- (file_stat.st_mode & S_IFMT) != S_IFREG)) {
- std::cout << "COMPREPLY=()";
+ aos::CliUtilInfo cli_info;
+ if (cli_info.Initialize(&argc, &argv,
+ [&cli_info](const aos::Channel *channel) {
+ return aos::configuration::ChannelIsReadableOnNode(
+ channel, cli_info.event_loop->node());
+ })) {
return 0;
}
- std::string channel_name;
- std::string message_type;
- if (argc > 1) {
- channel_name = argv[1];
- }
- if (argc > 2) {
- message_type = argv[2];
- }
-
- aos::FlatbufferDetachedBuffer<aos::Configuration> config =
- aos::configuration::ReadConfig(FLAGS_config);
-
- const aos::Configuration *config_msg = &config.message();
- aos::ShmEventLoop event_loop(config_msg);
- event_loop.SkipTimingReport();
- event_loop.SkipAosLog();
-
- if (FLAGS__bash_autocomplete) {
- Autocomplete(config_msg, event_loop, channel_name, message_type);
- return 0;
- }
-
- if (argc == 1) {
- std::cout << "Channels:\n";
- for (const aos::Channel *channel : *config_msg->channels()) {
- if (FLAGS_all || aos::configuration::ChannelIsReadableOnNode(
- channel, event_loop.node())) {
- std::cout << channel->name()->c_str() << ' ' << channel->type()->c_str()
- << '\n';
- }
- }
- return 0;
- }
-
- std::vector<const aos::Channel *> found_channels;
- const flatbuffers::Vector<flatbuffers::Offset<aos::Channel>> *channels =
- config_msg->channels();
- bool found_exact = false;
- for (const aos::Channel *channel : *channels) {
- if (!FLAGS_all && !aos::configuration::ChannelIsReadableOnNode(
- channel, event_loop.node())) {
- continue;
- }
- if (channel->name()->c_str() != channel_name) {
- continue;
- }
- if (channel->type()->string_view() == message_type) {
- if (!found_exact) {
- found_channels.clear();
- found_exact = true;
- }
- } else if (!found_exact && channel->type()->string_view().find(
- message_type) != std::string_view::npos) {
- } else {
- continue;
- }
- found_channels.push_back(channel);
- }
-
- if (found_channels.empty()) {
- LOG(FATAL) << "Could not find any channels with the given name and type.";
- } else if (found_channels.size() > 1 && !message_type.empty()) {
- LOG(FATAL) << "Multiple channels found with same type";
- }
-
uint64_t message_count = 0;
aos::FastStringBuilder str_builder;
aos::monotonic_clock::time_point next_send_time =
aos::monotonic_clock::min_time;
- for (const aos::Channel *channel : found_channels) {
+ for (const aos::Channel *channel : cli_info.found_channels) {
if (FLAGS_fetch) {
const std::unique_ptr<aos::RawFetcher> fetcher =
- event_loop.MakeRawFetcher(channel);
+ cli_info.event_loop->MakeRawFetcher(channel);
if (fetcher->Fetch()) {
PrintMessage(channel, fetcher->context(), &str_builder);
++message_count;
@@ -231,9 +90,9 @@
return 0;
}
- event_loop.MakeRawWatcher(
+ cli_info.event_loop->MakeRawWatcher(
channel,
- [channel, &str_builder, &event_loop, &message_count, &next_send_time](
+ [channel, &str_builder, &cli_info, &message_count, &next_send_time](
const aos::Context &context, const void * /*message*/) {
if (context.monotonic_event_time > next_send_time) {
PrintMessage(channel, context, &str_builder);
@@ -241,13 +100,13 @@
next_send_time = context.monotonic_event_time +
std::chrono::milliseconds(FLAGS_rate_limit);
if (FLAGS_count > 0 && message_count >= FLAGS_count) {
- event_loop.Exit();
+ cli_info.event_loop->Exit();
}
}
});
}
- event_loop.Run();
+ cli_info.event_loop->Run();
return 0;
}
diff --git a/aos/aos_dump_autocomplete.sh b/aos/aos_dump_autocomplete.sh
index a49c540..c62af99 100644
--- a/aos/aos_dump_autocomplete.sh
+++ b/aos/aos_dump_autocomplete.sh
@@ -5,3 +5,5 @@
complete -F _aosdump_completions -o default aos_dump
complete -F _aosdump_completions -o default aos_dump.stripped
+complete -F _aosdump_completions -o default aos_send
+complete -F _aosdump_completions -o default aos_send.stripped
diff --git a/aos/aos_send.cc b/aos/aos_send.cc
new file mode 100644
index 0000000..d0dea01
--- /dev/null
+++ b/aos/aos_send.cc
@@ -0,0 +1,47 @@
+#include <unistd.h>
+
+#include <iostream>
+
+#include "aos/aos_cli_utils.h"
+#include "aos/configuration.h"
+#include "aos/init.h"
+#include "aos/json_to_flatbuffer.h"
+#include "gflags/gflags.h"
+#include "glog/logging.h"
+
+int main(int argc, char **argv) {
+ gflags::SetUsageMessage(
+ "Sends messages on arbitrary channels.\n"
+ "Typical Usage: aos_send [--config path_to_config.json]"
+ " channel_name message_type '{\"foo\": \"bar\"}'\n"
+ "Example usage: aos_send /test aos.examples.Ping "
+ "'{\"value\": 1}'");
+ aos::InitGoogle(&argc, &argv);
+
+ aos::CliUtilInfo cli_info;
+ if (cli_info.Initialize(&argc, &argv,
+ [&cli_info](const aos::Channel *channel) {
+ return aos::configuration::ChannelIsSendableOnNode(
+ channel, cli_info.event_loop->node());
+ })) {
+ return 0;
+ }
+ if (cli_info.found_channels.size() > 1) {
+ LOG(FATAL) << "Matched multiple channels, but may only send on 1";
+ }
+
+ if (argc == 1) {
+ LOG(FATAL) << "Must specify a message to send";
+ }
+
+ const aos::Channel *const channel = cli_info.found_channels[0];
+ const std::unique_ptr<aos::RawSender> sender =
+ cli_info.event_loop->MakeRawSender(channel);
+ flatbuffers::FlatBufferBuilder fbb(sender->fbb_allocator()->size(),
+ sender->fbb_allocator());
+ fbb.Finish(aos::JsonToFlatbuffer(std::string_view(argv[1]), channel->schema(),
+ &fbb));
+ sender->Send(fbb.GetSize());
+
+ return 0;
+}