blob: d5ac2a1d6e58f1c1cf7650ec543ee5915eb57d81 [file] [log] [blame]
John Park398c74a2018-10-20 21:17:39 -07001#include "aos/configuration.h"
Brian Silverman66f079a2013-08-26 16:24:30 -07002
Brian Silverman66f079a2013-08-26 16:24:30 -07003#include <arpa/inet.h>
4#include <ifaddrs.h>
Austin Schuhf1fff282020-03-28 16:57:32 -07005#include <netinet/in.h>
Austin Schuhf1fff282020-03-28 16:57:32 -07006#include <sys/types.h>
Brian Silverman66f079a2013-08-26 16:24:30 -07007#include <unistd.h>
Austin Schuhe84c3ed2019-12-14 15:29:48 -08008
Tyler Chatowbf0609c2021-07-31 16:13:27 -07009#include <cstdlib>
10#include <cstring>
Austin Schuh68d98592020-11-01 23:22:57 -080011#include <map>
Austin Schuhe84c3ed2019-12-14 15:29:48 -080012#include <set>
Austin Schuhef38cd22021-07-21 15:24:23 -070013#include <string>
James Kuszmaul3ae42262019-11-08 12:33:41 -080014#include <string_view>
Austin Schuhef38cd22021-07-21 15:24:23 -070015#include <vector>
Brian Silverman66f079a2013-08-26 16:24:30 -070016
Austin Schuhcb108412019-10-13 16:09:54 -070017#include "absl/container/btree_set.h"
Austin Schuha81454b2020-05-12 19:58:36 -070018#include "absl/strings/str_cat.h"
Austin Schuhef38cd22021-07-21 15:24:23 -070019#include "absl/strings/str_join.h"
20#include "absl/strings/str_split.h"
Austin Schuhcb108412019-10-13 16:09:54 -070021#include "aos/configuration_generated.h"
22#include "aos/flatbuffer_merge.h"
23#include "aos/json_to_flatbuffer.h"
Austin Schuh217a9782019-12-21 23:02:50 -080024#include "aos/network/team_number.h"
Austin Schuhcb108412019-10-13 16:09:54 -070025#include "aos/unique_malloc_ptr.h"
26#include "aos/util/file.h"
Austin Schuh217a9782019-12-21 23:02:50 -080027#include "gflags/gflags.h"
Austin Schuhcb108412019-10-13 16:09:54 -070028#include "glog/logging.h"
Austin Schuh217a9782019-12-21 23:02:50 -080029
Brian Silverman66f079a2013-08-26 16:24:30 -070030namespace aos {
Austin Schuh15182322020-10-10 15:25:21 -070031namespace {
Austin Schuhfb37c612022-08-11 15:24:51 -070032namespace chrono = std::chrono;
33
Austin Schuh15182322020-10-10 15:25:21 -070034bool EndsWith(std::string_view str, std::string_view end) {
35 if (str.size() < end.size()) {
36 return false;
37 }
38 if (str.substr(str.size() - end.size(), end.size()) != end) {
39 return false;
40 }
41 return true;
42}
43
44std::string MaybeReplaceExtension(std::string_view filename,
45 std::string_view extension,
46 std::string_view replacement) {
47 if (!EndsWith(filename, extension)) {
48 return std::string(filename);
49 }
50 filename.remove_suffix(extension.size());
51 return absl::StrCat(filename, replacement);
52}
53
54FlatbufferDetachedBuffer<Configuration> ReadConfigFile(std::string_view path,
55 bool binary) {
56 if (binary) {
57 FlatbufferVector<Configuration> config =
58 FileToFlatbuffer<Configuration>(path);
59 return CopySpanAsDetachedBuffer(config.span());
60 }
61
62 flatbuffers::DetachedBuffer buffer = JsonToFlatbuffer(
63 util::ReadFileToStringOrDie(path), ConfigurationTypeTable());
64
Austin Schuh84a039a2021-11-03 16:50:34 -070065 CHECK_GT(buffer.size(), 0u) << ": Failed to parse JSON file: " << path;
Austin Schuh15182322020-10-10 15:25:21 -070066
67 return FlatbufferDetachedBuffer<Configuration>(std::move(buffer));
68}
69
70} // namespace
Austin Schuhcb108412019-10-13 16:09:54 -070071
Austin Schuh40485ed2019-10-26 21:51:44 -070072// Define the compare and equal operators for Channel and Application so we can
Austin Schuhcb108412019-10-13 16:09:54 -070073// insert them in the btree below.
Austin Schuh40485ed2019-10-26 21:51:44 -070074bool operator<(const FlatbufferDetachedBuffer<Channel> &lhs,
75 const FlatbufferDetachedBuffer<Channel> &rhs) {
Austin Schuhcb108412019-10-13 16:09:54 -070076 int name_compare = lhs.message().name()->string_view().compare(
77 rhs.message().name()->string_view());
78 if (name_compare == 0) {
79 return lhs.message().type()->string_view() <
80 rhs.message().type()->string_view();
81 } else if (name_compare < 0) {
82 return true;
83 } else {
84 return false;
85 }
86}
87
Austin Schuh40485ed2019-10-26 21:51:44 -070088bool operator==(const FlatbufferDetachedBuffer<Channel> &lhs,
89 const FlatbufferDetachedBuffer<Channel> &rhs) {
Austin Schuhcb108412019-10-13 16:09:54 -070090 return lhs.message().name()->string_view() ==
91 rhs.message().name()->string_view() &&
92 lhs.message().type()->string_view() ==
93 rhs.message().type()->string_view();
94}
95
Austin Schuha7996eb2021-10-11 19:03:24 -070096bool operator<(const FlatbufferDetachedBuffer<Connection> &lhs,
97 const FlatbufferDetachedBuffer<Connection> &rhs) {
98 return lhs.message().name()->string_view() <
99 rhs.message().name()->string_view();
100}
101
102bool operator==(const FlatbufferDetachedBuffer<Connection> &lhs,
103 const FlatbufferDetachedBuffer<Connection> &rhs) {
104 return lhs.message().name()->string_view() ==
105 rhs.message().name()->string_view();
106}
107
Austin Schuh40485ed2019-10-26 21:51:44 -0700108bool operator==(const FlatbufferDetachedBuffer<Application> &lhs,
109 const FlatbufferDetachedBuffer<Application> &rhs) {
Austin Schuhcb108412019-10-13 16:09:54 -0700110 return lhs.message().name()->string_view() ==
111 rhs.message().name()->string_view();
112}
113
Austin Schuh40485ed2019-10-26 21:51:44 -0700114bool operator<(const FlatbufferDetachedBuffer<Application> &lhs,
115 const FlatbufferDetachedBuffer<Application> &rhs) {
Austin Schuhcb108412019-10-13 16:09:54 -0700116 return lhs.message().name()->string_view() <
117 rhs.message().name()->string_view();
118}
119
Austin Schuh217a9782019-12-21 23:02:50 -0800120bool operator==(const FlatbufferDetachedBuffer<Node> &lhs,
121 const FlatbufferDetachedBuffer<Node> &rhs) {
122 return lhs.message().name()->string_view() ==
123 rhs.message().name()->string_view();
124}
125
126bool operator<(const FlatbufferDetachedBuffer<Node> &lhs,
127 const FlatbufferDetachedBuffer<Node> &rhs) {
128 return lhs.message().name()->string_view() <
129 rhs.message().name()->string_view();
130}
131
Brian Silverman66f079a2013-08-26 16:24:30 -0700132namespace configuration {
133namespace {
134
Austin Schuhcb108412019-10-13 16:09:54 -0700135// Extracts the folder part of a path. Returns ./ if there is no path.
Austin Schuhf1fff282020-03-28 16:57:32 -0700136std::string_view ExtractFolder(const std::string_view filename) {
Austin Schuhcb108412019-10-13 16:09:54 -0700137 auto last_slash_pos = filename.find_last_of("/\\");
138
James Kuszmaul3ae42262019-11-08 12:33:41 -0800139 return last_slash_pos == std::string_view::npos
140 ? std::string_view("./")
Austin Schuhcb108412019-10-13 16:09:54 -0700141 : filename.substr(0, last_slash_pos + 1);
142}
143
James Kuszmaulc0c08da2020-05-10 18:56:07 -0700144std::string AbsolutePath(const std::string_view filename) {
145 // Uses an std::string so that we know the input will be null-terminated.
146 const std::string terminated_file(filename);
147 char buffer[PATH_MAX];
148 PCHECK(NULL != realpath(terminated_file.c_str(), buffer));
149 return buffer;
150}
151
Austin Schuhef38cd22021-07-21 15:24:23 -0700152std::string RemoveDotDots(const std::string_view filename) {
153 std::vector<std::string> split = absl::StrSplit(filename, '/');
154 auto iterator = split.begin();
155 while (iterator != split.end()) {
156 if (iterator->empty()) {
157 iterator = split.erase(iterator);
158 } else if (*iterator == ".") {
159 iterator = split.erase(iterator);
160 } else if (*iterator == "..") {
161 CHECK(iterator != split.begin())
162 << ": Import path may not start with ..: " << filename;
163 auto previous = iterator;
164 --previous;
165 split.erase(iterator);
166 iterator = split.erase(previous);
167 } else {
168 ++iterator;
169 }
170 }
171 return absl::StrJoin(split, "/");
172}
173
Milind Upadhyay17098ba2022-04-15 22:18:50 -0700174std::optional<FlatbufferDetachedBuffer<Configuration>> MaybeReadConfig(
James Kuszmaulc0c08da2020-05-10 18:56:07 -0700175 const std::string_view path, absl::btree_set<std::string> *visited_paths,
176 const std::vector<std::string_view> &extra_import_paths) {
Austin Schuh15182322020-10-10 15:25:21 -0700177 std::string binary_path = MaybeReplaceExtension(path, ".json", ".bfbs");
Austin Schuhef38cd22021-07-21 15:24:23 -0700178 VLOG(1) << "Looking up: " << path << ", starting with: " << binary_path;
Austin Schuh15182322020-10-10 15:25:21 -0700179 bool binary_path_exists = util::PathExists(binary_path);
James Kuszmaulc0c08da2020-05-10 18:56:07 -0700180 std::string raw_path(path);
Austin Schuh15182322020-10-10 15:25:21 -0700181 // For each .json file, look and see if we can find a .bfbs file next to it
182 // with the same base name. If we can, assume it is the same and use it
183 // instead. It is much faster to load .bfbs files than .json files.
184 if (!binary_path_exists && !util::PathExists(raw_path)) {
185 const bool path_is_absolute = raw_path.size() > 0 && raw_path[0] == '/';
Brian Silvermand0588192022-07-26 00:35:22 -0700186 if (path_is_absolute) {
187 // Nowhere else to look up an absolute path, so fail now. Note that we
188 // always have at least one extra import path based on /proc/self/exe, so
189 // warning about those paths existing isn't helpful.
190 LOG(ERROR) << ": Failed to find file " << path << ".";
Milind Upadhyay17098ba2022-04-15 22:18:50 -0700191 return std::nullopt;
James Kuszmaulc0c08da2020-05-10 18:56:07 -0700192 }
193
194 bool found_path = false;
195 for (const auto &import_path : extra_import_paths) {
Austin Schuhef38cd22021-07-21 15:24:23 -0700196 raw_path = std::string(import_path) + "/" + RemoveDotDots(path);
Austin Schuh15182322020-10-10 15:25:21 -0700197 binary_path = MaybeReplaceExtension(raw_path, ".json", ".bfbs");
Austin Schuhef38cd22021-07-21 15:24:23 -0700198 VLOG(1) << "Checking: " << binary_path;
Austin Schuh15182322020-10-10 15:25:21 -0700199 binary_path_exists = util::PathExists(binary_path);
200 if (binary_path_exists) {
201 found_path = true;
202 break;
203 }
Austin Schuhef38cd22021-07-21 15:24:23 -0700204 VLOG(1) << "Checking: " << raw_path;
James Kuszmaulc0c08da2020-05-10 18:56:07 -0700205 if (util::PathExists(raw_path)) {
206 found_path = true;
207 break;
208 }
209 }
Milind Upadhyay17098ba2022-04-15 22:18:50 -0700210 if (!found_path) {
211 LOG(ERROR) << ": Failed to find file " << path << ".";
212 return std::nullopt;
213 }
James Kuszmaulc0c08da2020-05-10 18:56:07 -0700214 }
Alex Perrycb7da4b2019-08-28 19:35:56 -0700215
Milind Upadhyay17098ba2022-04-15 22:18:50 -0700216 std::optional<FlatbufferDetachedBuffer<Configuration>> config =
217 ReadConfigFile(binary_path_exists ? binary_path : raw_path,
218 binary_path_exists);
James Kuszmaulc0c08da2020-05-10 18:56:07 -0700219
Austin Schuhcb108412019-10-13 16:09:54 -0700220 // Depth first. Take the following example:
221 //
222 // config1.json:
223 // {
Austin Schuh40485ed2019-10-26 21:51:44 -0700224 // "channels": [
Austin Schuhcb108412019-10-13 16:09:54 -0700225 // {
226 // "name": "/foo",
227 // "type": ".aos.bar",
228 // "max_size": 5
229 // }
230 // ],
231 // "imports": [
232 // "config2.json",
233 // ]
234 // }
235 //
236 // config2.json:
237 // {
Austin Schuh40485ed2019-10-26 21:51:44 -0700238 // "channels": [
Austin Schuhcb108412019-10-13 16:09:54 -0700239 // {
240 // "name": "/foo",
241 // "type": ".aos.bar",
242 // "max_size": 7
243 // }
244 // ],
245 // }
246 //
247 // We want the main config (config1.json) to be able to override the imported
248 // config. That means that it needs to be merged into the imported configs,
249 // not the other way around.
250
Austin Schuh15182322020-10-10 15:25:21 -0700251 const std::string absolute_path =
252 AbsolutePath(binary_path_exists ? binary_path : raw_path);
253 // Track that we have seen this file before recursing. Track the path we
254 // actually loaded (which should be consistent if imported twice).
James Kuszmaulc0c08da2020-05-10 18:56:07 -0700255 if (!visited_paths->insert(absolute_path).second) {
256 for (const auto &visited_path : *visited_paths) {
257 LOG(INFO) << "Already visited: " << visited_path;
258 }
259 LOG(FATAL)
260 << "Already imported " << path << " (i.e. " << absolute_path
261 << "). See above for the files that have already been processed.";
Milind Upadhyay17098ba2022-04-15 22:18:50 -0700262 return std::nullopt;
James Kuszmaulc0c08da2020-05-10 18:56:07 -0700263 }
Austin Schuhcb108412019-10-13 16:09:54 -0700264
Milind Upadhyay17098ba2022-04-15 22:18:50 -0700265 if (config->message().has_imports()) {
Austin Schuhcb108412019-10-13 16:09:54 -0700266 // Capture the imports.
267 const flatbuffers::Vector<flatbuffers::Offset<flatbuffers::String>> *v =
Milind Upadhyay17098ba2022-04-15 22:18:50 -0700268 config->message().imports();
Austin Schuhcb108412019-10-13 16:09:54 -0700269
270 // And then wipe them. This gets GCed when we merge later.
Milind Upadhyay17098ba2022-04-15 22:18:50 -0700271 config->mutable_message()->clear_imports();
Austin Schuhcb108412019-10-13 16:09:54 -0700272
273 // Start with an empty configuration to merge into.
Austin Schuh40485ed2019-10-26 21:51:44 -0700274 FlatbufferDetachedBuffer<Configuration> merged_config =
275 FlatbufferDetachedBuffer<Configuration>::Empty();
Austin Schuhcb108412019-10-13 16:09:54 -0700276
James Kuszmaulc0c08da2020-05-10 18:56:07 -0700277 const std::string path_folder(ExtractFolder(path));
Austin Schuhcb108412019-10-13 16:09:54 -0700278 for (const flatbuffers::String *str : *v) {
James Kuszmaulc0c08da2020-05-10 18:56:07 -0700279 const std::string included_config =
280 path_folder + "/" + std::string(str->string_view());
Austin Schuhcb108412019-10-13 16:09:54 -0700281
Milind Upadhyay17098ba2022-04-15 22:18:50 -0700282 const auto optional_config =
283 MaybeReadConfig(included_config, visited_paths, extra_import_paths);
284 if (!optional_config.has_value()) {
285 return std::nullopt;
286 }
Austin Schuhcb108412019-10-13 16:09:54 -0700287 // And them merge everything in.
Milind Upadhyay17098ba2022-04-15 22:18:50 -0700288 merged_config = MergeFlatBuffers(merged_config, *optional_config);
Austin Schuhcb108412019-10-13 16:09:54 -0700289 }
290
291 // Finally, merge this file in.
Milind Upadhyay17098ba2022-04-15 22:18:50 -0700292 config = MergeFlatBuffers(merged_config, *config);
Austin Schuhcb108412019-10-13 16:09:54 -0700293 }
294 return config;
295}
296
Alex Perrycb7da4b2019-08-28 19:35:56 -0700297// Compares (c < p) a channel, and a name, type tuple.
298bool CompareChannels(const Channel *c,
James Kuszmaul3ae42262019-11-08 12:33:41 -0800299 ::std::pair<std::string_view, std::string_view> p) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700300 int name_compare = c->name()->string_view().compare(p.first);
301 if (name_compare == 0) {
302 return c->type()->string_view() < p.second;
303 } else if (name_compare < 0) {
304 return true;
305 } else {
306 return false;
307 }
308};
309
310// Compares for equality (c == p) a channel, and a name, type tuple.
311bool EqualsChannels(const Channel *c,
Austin Schuhf1fff282020-03-28 16:57:32 -0700312 ::std::pair<std::string_view, std::string_view> p) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700313 return c->name()->string_view() == p.first &&
314 c->type()->string_view() == p.second;
315}
316
317// Compares (c < p) an application, and a name;
James Kuszmaul3ae42262019-11-08 12:33:41 -0800318bool CompareApplications(const Application *a, std::string_view name) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700319 return a->name()->string_view() < name;
320};
321
322// Compares for equality (c == p) an application, and a name;
James Kuszmaul3ae42262019-11-08 12:33:41 -0800323bool EqualsApplications(const Application *a, std::string_view name) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700324 return a->name()->string_view() == name;
325}
326
Austin Schuh15182322020-10-10 15:25:21 -0700327void ValidateConfiguration(const Flatbuffer<Configuration> &config) {
328 // No imports should be left.
329 CHECK(!config.message().has_imports());
330
331 // Check that if there is a node list, all the source nodes are filled out and
332 // valid, and all the destination nodes are valid (and not the source). This
333 // is a basic consistency check.
334 if (config.message().has_channels()) {
335 const Channel *last_channel = nullptr;
336 for (const Channel *c : *config.message().channels()) {
337 CHECK(c->has_name());
338 CHECK(c->has_type());
339 if (c->name()->string_view().back() == '/') {
340 LOG(FATAL) << "Channel names can't end with '/'";
341 }
342 if (c->name()->string_view().find("//") != std::string_view::npos) {
343 LOG(FATAL) << ": Invalid channel name " << c->name()->string_view()
344 << ", can't use //.";
345 }
346 for (const char data : c->name()->string_view()) {
347 if (data >= '0' && data <= '9') {
348 continue;
349 }
350 if (data >= 'a' && data <= 'z') {
351 continue;
352 }
353 if (data >= 'A' && data <= 'Z') {
354 continue;
355 }
356 if (data == '-' || data == '_' || data == '/') {
357 continue;
358 }
359 LOG(FATAL) << "Invalid channel name " << c->name()->string_view()
360 << ", can only use [-a-zA-Z0-9_/]";
361 }
362
Austin Schuhfb37c612022-08-11 15:24:51 -0700363 CHECK_LT(QueueSize(&config.message(), c) + QueueScratchBufferSize(c),
364 std::numeric_limits<uint16_t>::max())
365 << ": More messages/second configured than the queue can hold on "
366 << CleanedChannelToString(c) << ", " << c->frequency() << "hz for "
367 << config.message().channel_storage_duration() << "ns";
368
Austin Schuha156fb22021-10-11 19:23:21 -0700369 if (c->has_logger_nodes()) {
370 // Confirm that we don't have duplicate logger nodes.
371 absl::btree_set<std::string_view> logger_nodes;
372 for (const flatbuffers::String *s : *c->logger_nodes()) {
373 logger_nodes.insert(s->string_view());
374 }
375 CHECK_EQ(static_cast<size_t>(logger_nodes.size()),
376 c->logger_nodes()->size())
377 << ": Found duplicate logger_nodes in "
378 << CleanedChannelToString(c);
379 }
380
381 if (c->has_destination_nodes()) {
382 // Confirm that we don't have duplicate timestamp logger nodes.
Austin Schuh5e95bd62021-10-11 18:40:22 -0700383 for (const Connection *d : *c->destination_nodes()) {
Austin Schuha156fb22021-10-11 19:23:21 -0700384 if (d->has_timestamp_logger_nodes()) {
385 absl::btree_set<std::string_view> timestamp_logger_nodes;
386 for (const flatbuffers::String *s : *d->timestamp_logger_nodes()) {
387 timestamp_logger_nodes.insert(s->string_view());
388 }
389 CHECK_EQ(static_cast<size_t>(timestamp_logger_nodes.size()),
390 d->timestamp_logger_nodes()->size())
391 << ": Found duplicate timestamp_logger_nodes in "
392 << CleanedChannelToString(c);
393 }
394 }
395
396 // There is no good use case today for logging timestamps but not the
397 // corresponding data. Instead of plumbing through all of this on the
398 // reader side, let'd just disallow it for now.
399 if (c->logger() == LoggerConfig::NOT_LOGGED) {
400 for (const Connection *d : *c->destination_nodes()) {
401 CHECK(d->timestamp_logger() == LoggerConfig::NOT_LOGGED)
402 << ": Logging timestamps without data is not supported. If "
403 "you have a good use case, let's talk. "
404 << CleanedChannelToString(c);
405 }
Austin Schuh5e95bd62021-10-11 18:40:22 -0700406 }
407 }
408
Austin Schuh15182322020-10-10 15:25:21 -0700409 // Make sure everything is sorted while we are here... If this fails,
410 // there will be a bunch of weird errors.
411 if (last_channel != nullptr) {
412 CHECK(CompareChannels(
413 last_channel,
414 std::make_pair(c->name()->string_view(), c->type()->string_view())))
415 << ": Channels not sorted!";
416 }
417 last_channel = c;
418 }
419 }
420
421 if (config.message().has_nodes() && config.message().has_channels()) {
422 for (const Channel *c : *config.message().channels()) {
423 CHECK(c->has_source_node()) << ": Channel " << FlatbufferToJson(c)
424 << " is missing \"source_node\"";
425 CHECK(GetNode(&config.message(), c->source_node()->string_view()) !=
426 nullptr)
427 << ": Channel " << FlatbufferToJson(c)
428 << " has an unknown \"source_node\"";
429
430 if (c->has_destination_nodes()) {
431 for (const Connection *connection : *c->destination_nodes()) {
432 CHECK(connection->has_name());
433 CHECK(GetNode(&config.message(), connection->name()->string_view()) !=
434 nullptr)
435 << ": Channel " << FlatbufferToJson(c)
436 << " has an unknown \"destination_nodes\" "
437 << connection->name()->string_view();
438
439 switch (connection->timestamp_logger()) {
440 case LoggerConfig::LOCAL_LOGGER:
441 case LoggerConfig::NOT_LOGGED:
Austin Schuhb98d02d2022-08-16 13:27:25 -0700442 CHECK(!connection->has_timestamp_logger_nodes())
443 << ": " << CleanedChannelToString(c);
Austin Schuh15182322020-10-10 15:25:21 -0700444 break;
445 case LoggerConfig::REMOTE_LOGGER:
446 case LoggerConfig::LOCAL_AND_REMOTE_LOGGER:
447 CHECK(connection->has_timestamp_logger_nodes());
448 CHECK_GT(connection->timestamp_logger_nodes()->size(), 0u);
449 for (const flatbuffers::String *timestamp_logger_node :
450 *connection->timestamp_logger_nodes()) {
451 CHECK(GetNode(&config.message(),
452 timestamp_logger_node->string_view()) != nullptr)
453 << ": Channel " << FlatbufferToJson(c)
454 << " has an unknown \"timestamp_logger_node\""
455 << connection->name()->string_view();
456 }
457 break;
458 }
459
460 CHECK_NE(connection->name()->string_view(),
461 c->source_node()->string_view())
462 << ": Channel " << FlatbufferToJson(c)
463 << " is forwarding data to itself";
464 }
465 }
466 }
467 }
468}
469
James Kuszmaulc8503f32022-06-25 16:17:12 -0700470void HandleReverseMaps(
471 const flatbuffers::Vector<flatbuffers::Offset<aos::Map>> *maps,
472 std::string_view type, const Node *node, std::set<std::string> *names) {
473 for (const Map *map : *maps) {
474 CHECK_NOTNULL(map);
475 const Channel *const match = CHECK_NOTNULL(map->match());
476 const Channel *const rename = CHECK_NOTNULL(map->rename());
477
478 // Handle type specific maps.
479 const flatbuffers::String *const match_type_string = match->type();
480 if (match_type_string != nullptr &&
481 match_type_string->string_view() != type) {
482 continue;
483 }
484
485 // Now handle node specific maps.
486 const flatbuffers::String *const match_source_node_string =
487 match->source_node();
488 if (node != nullptr && match_source_node_string != nullptr &&
489 match_source_node_string->string_view() !=
490 node->name()->string_view()) {
491 continue;
492 }
493
494 const flatbuffers::String *const match_name_string = match->name();
495 const flatbuffers::String *const rename_name_string = rename->name();
496 if (match_name_string == nullptr || rename_name_string == nullptr) {
497 continue;
498 }
499
500 const std::string rename_name = rename_name_string->str();
501 const std::string_view match_name = match_name_string->string_view();
502
503 std::set<std::string> possible_renames;
504
505 // Check if the current name(s) could have been reached using the provided
506 // rename.
507 if (match_name.back() == '*') {
508 for (const std::string &option : *names) {
509 if (option.substr(0, rename_name.size()) == rename_name) {
510 possible_renames.insert(
511 absl::StrCat(match_name.substr(0, match_name.size() - 1),
512 option.substr(rename_name.size())));
513 }
514 }
515 names->insert(possible_renames.begin(), possible_renames.end());
516 } else if (names->count(rename_name) != 0) {
517 names->insert(std::string(match_name));
518 }
519 }
520}
521
Alex Perrycb7da4b2019-08-28 19:35:56 -0700522} // namespace
523
Austin Schuh006a9f52021-04-07 16:24:18 -0700524// Maps name for the provided maps. Modifies name.
Brian Silvermanf3798cb2021-11-10 12:26:34 -0800525//
526// This is called many times during startup, and it dereferences a lot of
527// pointers. These combine to make it a performance hotspot during many tests
528// under msan, so there is some optimizing around caching intermediates instead
529// of dereferencing the pointer multiple times.
James Kuszmaulc8503f32022-06-25 16:17:12 -0700530//
531// Deliberately not in an anonymous namespace so that the log-reading code can
532// reference it.
Austin Schuh006a9f52021-04-07 16:24:18 -0700533void HandleMaps(const flatbuffers::Vector<flatbuffers::Offset<aos::Map>> *maps,
534 std::string *name, std::string_view type, const Node *node) {
535 // For the same reason we merge configs in reverse order, we want to process
536 // maps in reverse order. That lets the outer config overwrite channels from
537 // the inner configs.
538 for (auto i = maps->rbegin(); i != maps->rend(); ++i) {
Brian Silvermanf3798cb2021-11-10 12:26:34 -0800539 const Channel *const match = i->match();
540 if (!match) {
Austin Schuh006a9f52021-04-07 16:24:18 -0700541 continue;
542 }
Brian Silvermanf3798cb2021-11-10 12:26:34 -0800543 const flatbuffers::String *const match_name_string = match->name();
544 if (!match_name_string) {
545 continue;
546 }
547 const Channel *const rename = i->rename();
548 if (!rename) {
549 continue;
550 }
551 const flatbuffers::String *const rename_name_string = rename->name();
552 if (!rename_name_string) {
Austin Schuh006a9f52021-04-07 16:24:18 -0700553 continue;
554 }
555
556 // Handle normal maps (now that we know that match and rename are filled
557 // out).
Brian Silvermanf3798cb2021-11-10 12:26:34 -0800558 const std::string_view match_name = match_name_string->string_view();
Austin Schuh006a9f52021-04-07 16:24:18 -0700559 if (match_name != *name) {
560 if (match_name.back() == '*' &&
561 std::string_view(*name).substr(
562 0, std::min(name->size(), match_name.size() - 1)) ==
563 match_name.substr(0, match_name.size() - 1)) {
564 CHECK_EQ(match_name.find('*'), match_name.size() - 1);
565 } else {
566 continue;
567 }
568 }
569
570 // Handle type specific maps.
Brian Silvermanf3798cb2021-11-10 12:26:34 -0800571 const flatbuffers::String *const match_type_string = match->type();
572 if (match_type_string && match_type_string->string_view() != type) {
Austin Schuh006a9f52021-04-07 16:24:18 -0700573 continue;
574 }
575
576 // Now handle node specific maps.
Brian Silvermanf3798cb2021-11-10 12:26:34 -0800577 const flatbuffers::String *const match_source_node_string =
578 match->source_node();
579 if (node && match_source_node_string &&
580 match_source_node_string->string_view() !=
Austin Schuh006a9f52021-04-07 16:24:18 -0700581 node->name()->string_view()) {
582 continue;
583 }
584
Brian Silvermanf3798cb2021-11-10 12:26:34 -0800585 std::string new_name(rename_name_string->string_view());
Austin Schuh006a9f52021-04-07 16:24:18 -0700586 if (match_name.back() == '*') {
587 new_name += std::string(name->substr(match_name.size() - 1));
588 }
589 VLOG(1) << "Renamed \"" << *name << "\" to \"" << new_name << "\"";
590 *name = std::move(new_name);
591 }
592}
593
James Kuszmaulc8503f32022-06-25 16:17:12 -0700594std::set<std::string> GetChannelAliases(const Configuration *config,
595 std::string_view name,
596 std::string_view type,
597 const std::string_view application_name,
598 const Node *node) {
599 std::set<std::string> names{std::string(name)};
600 if (config->has_maps()) {
601 HandleReverseMaps(config->maps(), type, node, &names);
602 }
603 {
604 const Application *application =
605 GetApplication(config, node, application_name);
606 if (application != nullptr && application->has_maps()) {
607 HandleReverseMaps(application->maps(), type, node, &names);
608 }
609 }
610 return names;
611}
612
Austin Schuh40485ed2019-10-26 21:51:44 -0700613FlatbufferDetachedBuffer<Configuration> MergeConfiguration(
Austin Schuhcb108412019-10-13 16:09:54 -0700614 const Flatbuffer<Configuration> &config) {
James Kuszmaul3c998592020-07-27 21:04:47 -0700615 // auto_merge_config will contain all the fields of the Configuration that are
616 // to be passed through unmodified to the result of MergeConfiguration().
617 // In the processing below, we mutate auto_merge_config to remove any fields
618 // which we do need to alter (hence why we can't use the input config
619 // directly), and then merge auto_merge_config back in at the end.
620 aos::FlatbufferDetachedBuffer<aos::Configuration> auto_merge_config =
Austin Schuha4fc60f2020-11-01 23:06:47 -0800621 aos::RecursiveCopyFlatBuffer(&config.message());
James Kuszmaul3c998592020-07-27 21:04:47 -0700622
Austin Schuh40485ed2019-10-26 21:51:44 -0700623 // Store all the channels in a sorted set. This lets us track channels we
Austin Schuhcb108412019-10-13 16:09:54 -0700624 // have seen before and merge the updates in.
Austin Schuh40485ed2019-10-26 21:51:44 -0700625 absl::btree_set<FlatbufferDetachedBuffer<Channel>> channels;
Austin Schuhcb108412019-10-13 16:09:54 -0700626
Austin Schuh40485ed2019-10-26 21:51:44 -0700627 if (config.message().has_channels()) {
James Kuszmaul3c998592020-07-27 21:04:47 -0700628 auto_merge_config.mutable_message()->clear_channels();
Austin Schuh40485ed2019-10-26 21:51:44 -0700629 for (const Channel *c : *config.message().channels()) {
Austin Schuhcb108412019-10-13 16:09:54 -0700630 // Ignore malformed entries.
Austin Schuh40485ed2019-10-26 21:51:44 -0700631 if (!c->has_name()) {
Austin Schuhcb108412019-10-13 16:09:54 -0700632 continue;
633 }
Austin Schuh40485ed2019-10-26 21:51:44 -0700634 if (!c->has_type()) {
Austin Schuhcb108412019-10-13 16:09:54 -0700635 continue;
636 }
637
Brian Silverman77162972020-08-12 19:52:40 -0700638 CHECK_EQ(c->read_method() == ReadMethod::PIN, c->num_readers() != 0)
639 << ": num_readers may be set if and only if read_method is PIN,"
640 " if you want 0 readers do not set PIN: "
641 << CleanedChannelToString(c);
642
Austin Schuh40485ed2019-10-26 21:51:44 -0700643 // Attempt to insert the channel.
Austin Schuha4fc60f2020-11-01 23:06:47 -0800644 auto result = channels.insert(RecursiveCopyFlatBuffer(c));
Austin Schuhcb108412019-10-13 16:09:54 -0700645 if (!result.second) {
646 // Already there, so merge the new table into the original.
Austin Schuh4a5f5d22021-10-12 15:09:35 -0700647 // Schemas merge poorly, so pick the newest one.
648 if (result.first->message().has_schema() && c->has_schema()) {
649 result.first->mutable_message()->clear_schema();
650 }
Austin Schuha7996eb2021-10-11 19:03:24 -0700651 auto merged =
Austin Schuha4fc60f2020-11-01 23:06:47 -0800652 MergeFlatBuffers(*result.first, RecursiveCopyFlatBuffer(c));
Austin Schuha7996eb2021-10-11 19:03:24 -0700653
654 if (merged.message().has_destination_nodes()) {
655 absl::btree_set<FlatbufferDetachedBuffer<Connection>> connections;
656 for (const Connection *connection :
657 *merged.message().destination_nodes()) {
658 auto connection_result =
659 connections.insert(RecursiveCopyFlatBuffer(connection));
660 if (!connection_result.second) {
661 *connection_result.first =
662 MergeFlatBuffers(*connection_result.first,
663 RecursiveCopyFlatBuffer(connection));
664 }
665 }
666 if (static_cast<size_t>(connections.size()) !=
667 merged.message().destination_nodes()->size()) {
668 merged.mutable_message()->clear_destination_nodes();
669 flatbuffers::FlatBufferBuilder fbb;
670 fbb.ForceDefaults(true);
671 std::vector<flatbuffers::Offset<Connection>> connection_offsets;
672 for (const FlatbufferDetachedBuffer<Connection> &connection :
673 connections) {
674 connection_offsets.push_back(
675 RecursiveCopyFlatBuffer(&connection.message(), &fbb));
676 }
677 flatbuffers::Offset<
678 flatbuffers::Vector<flatbuffers::Offset<Connection>>>
679 destination_nodes_offset = fbb.CreateVector(connection_offsets);
680 Channel::Builder channel_builder(fbb);
681 channel_builder.add_destination_nodes(destination_nodes_offset);
682 fbb.Finish(channel_builder.Finish());
683 FlatbufferDetachedBuffer<Channel> destinations_channel(
684 fbb.Release());
685 merged = MergeFlatBuffers(merged, destinations_channel);
686 }
687 }
688
689 *result.first = std::move(merged);
Austin Schuhcb108412019-10-13 16:09:54 -0700690 }
691 }
692 }
693
694 // Now repeat this for the application list.
Austin Schuh40485ed2019-10-26 21:51:44 -0700695 absl::btree_set<FlatbufferDetachedBuffer<Application>> applications;
Austin Schuhcb108412019-10-13 16:09:54 -0700696 if (config.message().has_applications()) {
James Kuszmaul3c998592020-07-27 21:04:47 -0700697 auto_merge_config.mutable_message()->clear_applications();
Austin Schuhcb108412019-10-13 16:09:54 -0700698 for (const Application *a : *config.message().applications()) {
699 if (!a->has_name()) {
700 continue;
701 }
702
Austin Schuha4fc60f2020-11-01 23:06:47 -0800703 auto result = applications.insert(RecursiveCopyFlatBuffer(a));
Austin Schuhcb108412019-10-13 16:09:54 -0700704 if (!result.second) {
Austin Schuhf81c2522021-12-08 12:03:30 -0800705 if (a->has_args()) {
706 result.first->mutable_message()->clear_args();
707 }
Austin Schuha4fc60f2020-11-01 23:06:47 -0800708 *result.first =
709 MergeFlatBuffers(*result.first, RecursiveCopyFlatBuffer(a));
Austin Schuhcb108412019-10-13 16:09:54 -0700710 }
711 }
712 }
713
Austin Schuh217a9782019-12-21 23:02:50 -0800714 // Now repeat this for the node list.
715 absl::btree_set<FlatbufferDetachedBuffer<Node>> nodes;
716 if (config.message().has_nodes()) {
James Kuszmaul3c998592020-07-27 21:04:47 -0700717 auto_merge_config.mutable_message()->clear_nodes();
Austin Schuh217a9782019-12-21 23:02:50 -0800718 for (const Node *n : *config.message().nodes()) {
719 if (!n->has_name()) {
720 continue;
721 }
722
Austin Schuha4fc60f2020-11-01 23:06:47 -0800723 auto result = nodes.insert(RecursiveCopyFlatBuffer(n));
Austin Schuh217a9782019-12-21 23:02:50 -0800724 if (!result.second) {
Austin Schuha4fc60f2020-11-01 23:06:47 -0800725 *result.first =
726 MergeFlatBuffers(*result.first, RecursiveCopyFlatBuffer(n));
Austin Schuh217a9782019-12-21 23:02:50 -0800727 }
728 }
729 }
730
Austin Schuhcb108412019-10-13 16:09:54 -0700731 flatbuffers::FlatBufferBuilder fbb;
Austin Schuhd7b15da2020-02-17 15:06:11 -0800732 fbb.ForceDefaults(true);
Austin Schuhcb108412019-10-13 16:09:54 -0700733
734 // Start by building the vectors. They need to come before the final table.
Austin Schuh40485ed2019-10-26 21:51:44 -0700735 // Channels
736 flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<Channel>>>
737 channels_offset;
Austin Schuhcb108412019-10-13 16:09:54 -0700738 {
Austin Schuh40485ed2019-10-26 21:51:44 -0700739 ::std::vector<flatbuffers::Offset<Channel>> channel_offsets;
740 for (const FlatbufferDetachedBuffer<Channel> &c : channels) {
Austin Schuha4fc60f2020-11-01 23:06:47 -0800741 channel_offsets.emplace_back(
742 RecursiveCopyFlatBuffer<Channel>(&c.message(), &fbb));
Austin Schuhcb108412019-10-13 16:09:54 -0700743 }
Austin Schuh40485ed2019-10-26 21:51:44 -0700744 channels_offset = fbb.CreateVector(channel_offsets);
Austin Schuhcb108412019-10-13 16:09:54 -0700745 }
746
747 // Applications
748 flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<Application>>>
749 applications_offset;
750 {
751 ::std::vector<flatbuffers::Offset<Application>> applications_offsets;
Austin Schuh40485ed2019-10-26 21:51:44 -0700752 for (const FlatbufferDetachedBuffer<Application> &a : applications) {
Austin Schuhcb108412019-10-13 16:09:54 -0700753 applications_offsets.emplace_back(
Austin Schuha4fc60f2020-11-01 23:06:47 -0800754 RecursiveCopyFlatBuffer<Application>(&a.message(), &fbb));
Austin Schuhcb108412019-10-13 16:09:54 -0700755 }
756 applications_offset = fbb.CreateVector(applications_offsets);
757 }
758
Austin Schuh217a9782019-12-21 23:02:50 -0800759 // Nodes
760 flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<Node>>>
761 nodes_offset;
762 {
763 ::std::vector<flatbuffers::Offset<Node>> node_offsets;
764 for (const FlatbufferDetachedBuffer<Node> &n : nodes) {
Austin Schuha4fc60f2020-11-01 23:06:47 -0800765 node_offsets.emplace_back(
766 RecursiveCopyFlatBuffer<Node>(&n.message(), &fbb));
Austin Schuh217a9782019-12-21 23:02:50 -0800767 }
768 nodes_offset = fbb.CreateVector(node_offsets);
769 }
770
Austin Schuhcb108412019-10-13 16:09:54 -0700771 // And then build a Configuration with them all.
772 ConfigurationBuilder configuration_builder(fbb);
Austin Schuh40485ed2019-10-26 21:51:44 -0700773 configuration_builder.add_channels(channels_offset);
Austin Schuh217a9782019-12-21 23:02:50 -0800774 if (config.message().has_applications()) {
775 configuration_builder.add_applications(applications_offset);
776 }
777 if (config.message().has_nodes()) {
778 configuration_builder.add_nodes(nodes_offset);
779 }
Austin Schuhcb108412019-10-13 16:09:54 -0700780
781 fbb.Finish(configuration_builder.Finish());
Austin Schuh217a9782019-12-21 23:02:50 -0800782
James Kuszmaul3c998592020-07-27 21:04:47 -0700783 aos::FlatbufferDetachedBuffer<aos::Configuration> modified_config(
784 fbb.Release());
785
Austin Schuh217a9782019-12-21 23:02:50 -0800786 // Now, validate that if there is a node list, every channel has a source
787 // node.
James Kuszmaul3c998592020-07-27 21:04:47 -0700788 FlatbufferDetachedBuffer<Configuration> result =
789 MergeFlatBuffers(modified_config, auto_merge_config);
Austin Schuh217a9782019-12-21 23:02:50 -0800790
Austin Schuh15182322020-10-10 15:25:21 -0700791 ValidateConfiguration(result);
Austin Schuh217a9782019-12-21 23:02:50 -0800792
793 return result;
Austin Schuhcb108412019-10-13 16:09:54 -0700794}
795
Milind Upadhyay17098ba2022-04-15 22:18:50 -0700796std::optional<FlatbufferDetachedBuffer<Configuration>> MaybeReadConfig(
James Kuszmaulc0c08da2020-05-10 18:56:07 -0700797 const std::string_view path,
Austin Schuhef38cd22021-07-21 15:24:23 -0700798 const std::vector<std::string_view> &extra_import_paths) {
Austin Schuh89026f52022-02-25 14:24:04 -0800799 // Add the executable directory to the search path. That makes it so that
800 // tools can be run from any directory without hard-coding an absolute path to
801 // the config into all binaries.
802 std::vector<std::string_view> extra_import_paths_with_exe =
803 extra_import_paths;
804 char proc_self_exec_buffer[PATH_MAX + 1];
805 std::memset(proc_self_exec_buffer, 0, sizeof(proc_self_exec_buffer));
806 ssize_t s = readlink("/proc/self/exe", proc_self_exec_buffer, PATH_MAX);
807 if (s > 0) {
808 // If the readlink call fails, the worst thing that happens is that we don't
809 // automatically find the config next to the binary. VLOG to make it easier
810 // to debug.
811 std::string_view proc_self_exec(proc_self_exec_buffer);
812
813 extra_import_paths_with_exe.emplace_back(
814 proc_self_exec.substr(0, proc_self_exec.rfind("/")));
815 } else {
816 VLOG(1) << "Failed to read /proc/self/exe";
817 }
818
Austin Schuhcb108412019-10-13 16:09:54 -0700819 // We only want to read a file once. So track the visited files in a set.
820 absl::btree_set<std::string> visited_paths;
Milind Upadhyay17098ba2022-04-15 22:18:50 -0700821 std::optional<FlatbufferDetachedBuffer<Configuration>> read_config =
822 MaybeReadConfig(path, &visited_paths, extra_import_paths_with_exe);
823
824 if (read_config == std::nullopt) {
825 return read_config;
826 }
Austin Schuh15182322020-10-10 15:25:21 -0700827
828 // If we only read one file, and it had a .bfbs extension, it has to be a
829 // fully formatted config. Do a quick verification and return it.
830 if (visited_paths.size() == 1 && EndsWith(*visited_paths.begin(), ".bfbs")) {
Milind Upadhyay17098ba2022-04-15 22:18:50 -0700831 ValidateConfiguration(*read_config);
Austin Schuh15182322020-10-10 15:25:21 -0700832 return read_config;
833 }
834
Milind Upadhyay17098ba2022-04-15 22:18:50 -0700835 return MergeConfiguration(*read_config);
836}
837
838FlatbufferDetachedBuffer<Configuration> ReadConfig(
839 const std::string_view path,
840 const std::vector<std::string_view> &extra_import_paths) {
841 auto optional_config = MaybeReadConfig(path, extra_import_paths);
842 CHECK(optional_config) << "Could not read config. See above errors";
843 return std::move(*optional_config);
Austin Schuhcb108412019-10-13 16:09:54 -0700844}
845
Austin Schuh8d6cea82020-02-28 12:17:16 -0800846FlatbufferDetachedBuffer<Configuration> MergeWithConfig(
Brian Silverman24f5aa82020-06-23 16:21:28 -0700847 const Configuration *config, const Flatbuffer<Configuration> &addition) {
848 return MergeConfiguration(MergeFlatBuffers(config, &addition.message()));
849}
850
851FlatbufferDetachedBuffer<Configuration> MergeWithConfig(
Austin Schuh8d6cea82020-02-28 12:17:16 -0800852 const Configuration *config, std::string_view json) {
853 FlatbufferDetachedBuffer<Configuration> addition =
854 JsonToFlatbuffer(json, Configuration::MiniReflectTypeTable());
855
Brian Silverman24f5aa82020-06-23 16:21:28 -0700856 return MergeWithConfig(config, addition);
Austin Schuh8d6cea82020-02-28 12:17:16 -0800857}
858
James Kuszmaul3ae42262019-11-08 12:33:41 -0800859const Channel *GetChannel(const Configuration *config, std::string_view name,
860 std::string_view type,
Austin Schuh0de30f32020-12-06 12:44:28 -0800861 std::string_view application_name, const Node *node,
862 bool quiet) {
Brian Silverman9fcf2c72020-12-21 18:30:58 -0800863 if (!config->has_channels()) {
864 return nullptr;
865 }
866
Austin Schuhbca6cf02019-12-22 17:28:34 -0800867 const std::string_view original_name = name;
Austin Schuhf1fff282020-03-28 16:57:32 -0700868 std::string mutable_name;
Austin Schuh4c3b9702020-08-30 11:34:55 -0700869 if (node != nullptr) {
870 VLOG(1) << "Looking up { \"name\": \"" << name << "\", \"type\": \"" << type
871 << "\" } on " << aos::FlatbufferToJson(node);
872 } else {
873 VLOG(1) << "Looking up { \"name\": \"" << name << "\", \"type\": \"" << type
874 << "\" }";
875 }
Austin Schuhcb108412019-10-13 16:09:54 -0700876
877 // First handle application specific maps. Only do this if we have a matching
878 // application name, and it has maps.
Austin Schuhd2e2f6a2021-02-07 20:46:16 -0800879 {
880 const Application *application =
881 GetApplication(config, node, application_name);
882 if (application != nullptr && application->has_maps()) {
883 mutable_name = std::string(name);
884 HandleMaps(application->maps(), &mutable_name, type, node);
885 name = std::string_view(mutable_name);
Austin Schuhcb108412019-10-13 16:09:54 -0700886 }
887 }
888
889 // Now do global maps.
Austin Schuh40485ed2019-10-26 21:51:44 -0700890 if (config->has_maps()) {
Austin Schuhf1fff282020-03-28 16:57:32 -0700891 mutable_name = std::string(name);
892 HandleMaps(config->maps(), &mutable_name, type, node);
893 name = std::string_view(mutable_name);
Austin Schuhcb108412019-10-13 16:09:54 -0700894 }
895
Austin Schuhbca6cf02019-12-22 17:28:34 -0800896 if (original_name != name) {
897 VLOG(1) << "Remapped to { \"name\": \"" << name << "\", \"type\": \""
898 << type << "\" }";
899 }
Alex Perrycb7da4b2019-08-28 19:35:56 -0700900
James Kuszmaul71a81932020-12-15 21:08:01 -0800901 // Then look for the channel (note that this relies on the channels being
902 // sorted in the config).
Austin Schuh40485ed2019-10-26 21:51:44 -0700903 auto channel_iterator =
Austin Schuhf1fff282020-03-28 16:57:32 -0700904 std::lower_bound(config->channels()->cbegin(), config->channels()->cend(),
Austin Schuh40485ed2019-10-26 21:51:44 -0700905 std::make_pair(name, type), CompareChannels);
Austin Schuhcb108412019-10-13 16:09:54 -0700906
907 // Make sure we actually found it, and it matches.
Austin Schuh40485ed2019-10-26 21:51:44 -0700908 if (channel_iterator != config->channels()->cend() &&
909 EqualsChannels(*channel_iterator, std::make_pair(name, type))) {
Austin Schuhbca6cf02019-12-22 17:28:34 -0800910 if (VLOG_IS_ON(2)) {
911 VLOG(2) << "Found: " << FlatbufferToJson(*channel_iterator);
912 } else if (VLOG_IS_ON(1)) {
913 VLOG(1) << "Found: " << CleanedChannelToString(*channel_iterator);
914 }
Austin Schuh40485ed2019-10-26 21:51:44 -0700915 return *channel_iterator;
Austin Schuhcb108412019-10-13 16:09:54 -0700916 } else {
917 VLOG(1) << "No match for { \"name\": \"" << name << "\", \"type\": \""
918 << type << "\" }";
Austin Schuh0de30f32020-12-06 12:44:28 -0800919 if (original_name != name && !quiet) {
Austin Schuh4b42b252020-10-19 11:35:20 -0700920 LOG(WARNING) << "Remapped from {\"name\": \"" << original_name
921 << "\", \"type\": \"" << type << "\"}, to {\"name\": \""
922 << name << "\", \"type\": \"" << type
923 << "\"}, but no channel by that name exists.";
924 }
Austin Schuhcb108412019-10-13 16:09:54 -0700925 return nullptr;
926 }
927}
928
Austin Schuhc9e10ec2020-01-26 16:08:28 -0800929size_t ChannelIndex(const Configuration *configuration,
930 const Channel *channel) {
931 CHECK(configuration->channels() != nullptr) << ": No channels";
932
Brian Silvermana9698c92021-11-10 12:27:04 -0800933 const auto c = std::lower_bound(
934 configuration->channels()->cbegin(), configuration->channels()->cend(),
935 std::make_pair(channel->name()->string_view(),
936 channel->type()->string_view()),
937 CompareChannels);
938 CHECK(c != configuration->channels()->cend())
939 << ": Channel pointer not found in configuration()->channels()";
940 CHECK(*c == channel)
Austin Schuhc9e10ec2020-01-26 16:08:28 -0800941 << ": Channel pointer not found in configuration()->channels()";
942
Brian Silvermana9698c92021-11-10 12:27:04 -0800943 return std::distance(configuration->channels()->cbegin(), c);
Austin Schuhc9e10ec2020-01-26 16:08:28 -0800944}
945
Austin Schuhbca6cf02019-12-22 17:28:34 -0800946std::string CleanedChannelToString(const Channel *channel) {
947 FlatbufferDetachedBuffer<Channel> cleaned_channel = CopyFlatBuffer(channel);
948 cleaned_channel.mutable_message()->clear_schema();
949 return FlatbufferToJson(cleaned_channel);
950}
951
Austin Schuha81454b2020-05-12 19:58:36 -0700952std::string StrippedChannelToString(const Channel *channel) {
953 return absl::StrCat("{ \"name\": \"", channel->name()->string_view(),
954 "\", \"type\": \"", channel->type()->string_view(),
955 "\" }");
956}
957
Alex Perrycb7da4b2019-08-28 19:35:56 -0700958FlatbufferDetachedBuffer<Configuration> MergeConfiguration(
959 const Flatbuffer<Configuration> &config,
Austin Schuh0de30f32020-12-06 12:44:28 -0800960 const std::vector<aos::FlatbufferVector<reflection::Schema>> &schemas) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700961 flatbuffers::FlatBufferBuilder fbb;
Austin Schuhd7b15da2020-02-17 15:06:11 -0800962 fbb.ForceDefaults(true);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700963
Austin Schuh68d98592020-11-01 23:22:57 -0800964 // Cache for holding already inserted schemas.
965 std::map<std::string_view, flatbuffers::Offset<reflection::Schema>>
966 schema_cache;
967
968 CHECK_EQ(Channel::MiniReflectTypeTable()->num_elems, 13u)
969 << ": Merging logic needs to be updated when the number of channel "
970 "fields changes.";
James Kuszmaul3c998592020-07-27 21:04:47 -0700971
Alex Perrycb7da4b2019-08-28 19:35:56 -0700972 flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<Channel>>>
973 channels_offset;
974 if (config.message().has_channels()) {
975 std::vector<flatbuffers::Offset<Channel>> channel_offsets;
976 for (const Channel *c : *config.message().channels()) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700977 // Search for a schema with a matching type.
Austin Schuh0de30f32020-12-06 12:44:28 -0800978 const aos::FlatbufferVector<reflection::Schema> *found_schema = nullptr;
979 for (const aos::FlatbufferVector<reflection::Schema> &schema : schemas) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700980 if (schema.message().root_table() != nullptr) {
981 if (schema.message().root_table()->name()->string_view() ==
982 c->type()->string_view()) {
983 found_schema = &schema;
984 }
985 }
986 }
987
988 CHECK(found_schema != nullptr)
989 << ": Failed to find schema for " << FlatbufferToJson(c);
990
Austin Schuh68d98592020-11-01 23:22:57 -0800991 // Now copy the message manually.
992 auto cached_schema = schema_cache.find(c->type()->string_view());
993 flatbuffers::Offset<reflection::Schema> schema_offset;
994 if (cached_schema != schema_cache.end()) {
995 schema_offset = cached_schema->second;
996 } else {
Austin Schuha4fc60f2020-11-01 23:06:47 -0800997 schema_offset = RecursiveCopyFlatBuffer<reflection::Schema>(
998 &found_schema->message(), &fbb);
Austin Schuh68d98592020-11-01 23:22:57 -0800999 schema_cache.emplace(c->type()->string_view(), schema_offset);
1000 }
1001
1002 flatbuffers::Offset<flatbuffers::String> name_offset =
1003 fbb.CreateSharedString(c->name()->str());
1004 flatbuffers::Offset<flatbuffers::String> type_offset =
1005 fbb.CreateSharedString(c->type()->str());
1006 flatbuffers::Offset<flatbuffers::String> source_node_offset =
Austin Schuha4fc60f2020-11-01 23:06:47 -08001007 c->has_source_node() ? fbb.CreateSharedString(c->source_node()->str())
1008 : 0;
Austin Schuh68d98592020-11-01 23:22:57 -08001009
1010 flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<Connection>>>
1011 destination_nodes_offset =
Austin Schuh5c255aa2020-11-05 18:32:46 -08001012 aos::RecursiveCopyVectorTable(c->destination_nodes(), &fbb);
Austin Schuh68d98592020-11-01 23:22:57 -08001013
1014 flatbuffers::Offset<
1015 flatbuffers::Vector<flatbuffers::Offset<flatbuffers::String>>>
1016 logger_nodes_offset =
1017 aos::CopyVectorSharedString(c->logger_nodes(), &fbb);
1018
1019 Channel::Builder channel_builder(fbb);
1020 channel_builder.add_name(name_offset);
1021 channel_builder.add_type(type_offset);
1022 if (c->has_frequency()) {
1023 channel_builder.add_frequency(c->frequency());
1024 }
1025 if (c->has_max_size()) {
1026 channel_builder.add_max_size(c->max_size());
1027 }
1028 if (c->has_num_senders()) {
1029 channel_builder.add_num_senders(c->num_senders());
1030 }
1031 if (c->has_num_watchers()) {
1032 channel_builder.add_num_watchers(c->num_watchers());
1033 }
Alex Perrycb7da4b2019-08-28 19:35:56 -07001034 channel_builder.add_schema(schema_offset);
Austin Schuh68d98592020-11-01 23:22:57 -08001035 if (!source_node_offset.IsNull()) {
1036 channel_builder.add_source_node(source_node_offset);
1037 }
1038 if (!destination_nodes_offset.IsNull()) {
1039 channel_builder.add_destination_nodes(destination_nodes_offset);
1040 }
1041 if (c->has_logger()) {
1042 channel_builder.add_logger(c->logger());
1043 }
1044 if (!logger_nodes_offset.IsNull()) {
1045 channel_builder.add_logger_nodes(logger_nodes_offset);
1046 }
1047 if (c->has_read_method()) {
1048 channel_builder.add_read_method(c->read_method());
1049 }
1050 if (c->has_num_readers()) {
1051 channel_builder.add_num_readers(c->num_readers());
1052 }
1053 channel_offsets.emplace_back(channel_builder.Finish());
Alex Perrycb7da4b2019-08-28 19:35:56 -07001054 }
1055 channels_offset = fbb.CreateVector(channel_offsets);
1056 }
1057
Austin Schuh68d98592020-11-01 23:22:57 -08001058 flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<Map>>>
Austin Schuh5c255aa2020-11-05 18:32:46 -08001059 maps_offset =
1060 aos::RecursiveCopyVectorTable(config.message().maps(), &fbb);
Austin Schuh68d98592020-11-01 23:22:57 -08001061
1062 flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<Node>>>
Austin Schuh5c255aa2020-11-05 18:32:46 -08001063 nodes_offset =
1064 aos::RecursiveCopyVectorTable(config.message().nodes(), &fbb);
Austin Schuh68d98592020-11-01 23:22:57 -08001065
1066 flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<Application>>>
1067 applications_offset =
Austin Schuh5c255aa2020-11-05 18:32:46 -08001068 aos::RecursiveCopyVectorTable(config.message().applications(), &fbb);
Austin Schuh217a9782019-12-21 23:02:50 -08001069
1070 // Now insert everything else in unmodified.
Alex Perrycb7da4b2019-08-28 19:35:56 -07001071 ConfigurationBuilder configuration_builder(fbb);
1072 if (config.message().has_channels()) {
1073 configuration_builder.add_channels(channels_offset);
1074 }
Austin Schuh68d98592020-11-01 23:22:57 -08001075 if (!maps_offset.IsNull()) {
1076 configuration_builder.add_maps(maps_offset);
1077 }
1078 if (!nodes_offset.IsNull()) {
1079 configuration_builder.add_nodes(nodes_offset);
1080 }
1081 if (!applications_offset.IsNull()) {
1082 configuration_builder.add_applications(applications_offset);
1083 }
1084
1085 if (config.message().has_channel_storage_duration()) {
1086 configuration_builder.add_channel_storage_duration(
1087 config.message().channel_storage_duration());
1088 }
1089
1090 CHECK_EQ(Configuration::MiniReflectTypeTable()->num_elems, 6u)
1091 << ": Merging logic needs to be updated when the number of configuration "
1092 "fields changes.";
1093
Alex Perrycb7da4b2019-08-28 19:35:56 -07001094 fbb.Finish(configuration_builder.Finish());
James Kuszmaul3c998592020-07-27 21:04:47 -07001095 aos::FlatbufferDetachedBuffer<aos::Configuration> modified_config(
1096 fbb.Release());
1097
Austin Schuh68d98592020-11-01 23:22:57 -08001098 return modified_config;
Alex Perrycb7da4b2019-08-28 19:35:56 -07001099}
1100
Austin Schuh217a9782019-12-21 23:02:50 -08001101const Node *GetNodeFromHostname(const Configuration *config,
1102 std::string_view hostname) {
1103 for (const Node *node : *config->nodes()) {
Brian Silvermanaa2633f2020-02-17 21:04:14 -08001104 if (node->has_hostname() && node->hostname()->string_view() == hostname) {
Austin Schuh217a9782019-12-21 23:02:50 -08001105 return node;
1106 }
Brian Silvermanaa2633f2020-02-17 21:04:14 -08001107 if (node->has_hostnames()) {
1108 for (const auto &candidate : *node->hostnames()) {
1109 if (candidate->string_view() == hostname) {
1110 return node;
1111 }
1112 }
1113 }
Austin Schuh217a9782019-12-21 23:02:50 -08001114 }
1115 return nullptr;
1116}
Austin Schuhac0771c2020-01-07 18:36:30 -08001117
Austin Schuh217a9782019-12-21 23:02:50 -08001118const Node *GetMyNode(const Configuration *config) {
1119 const std::string hostname = (FLAGS_override_hostname.size() > 0)
1120 ? FLAGS_override_hostname
1121 : network::GetHostname();
1122 const Node *node = GetNodeFromHostname(config, hostname);
1123 if (node != nullptr) return node;
1124
1125 LOG(FATAL) << "Unknown node for host: " << hostname
1126 << ". Consider using --override_hostname if hostname detection "
1127 "is wrong.";
1128 return nullptr;
1129}
1130
Austin Schuhc9e10ec2020-01-26 16:08:28 -08001131const Node *GetNode(const Configuration *config, const Node *node) {
1132 if (!MultiNode(config)) {
1133 CHECK(node == nullptr) << ": Provided a node in a single node world.";
1134 return nullptr;
1135 } else {
1136 CHECK(node != nullptr);
1137 CHECK(node->has_name());
1138 return GetNode(config, node->name()->string_view());
1139 }
1140}
1141
Austin Schuh217a9782019-12-21 23:02:50 -08001142const Node *GetNode(const Configuration *config, std::string_view name) {
Austin Schuhfd960622020-01-01 13:22:55 -08001143 CHECK(config->has_nodes())
1144 << ": Asking for a node from a single node configuration.";
Austin Schuh217a9782019-12-21 23:02:50 -08001145 for (const Node *node : *config->nodes()) {
Austin Schuhfd960622020-01-01 13:22:55 -08001146 CHECK(node->has_name()) << ": Malformed node " << FlatbufferToJson(node);
Austin Schuh217a9782019-12-21 23:02:50 -08001147 if (node->name()->string_view() == name) {
1148 return node;
1149 }
1150 }
1151 return nullptr;
1152}
1153
Austin Schuh0ca1fd32020-12-18 22:53:05 -08001154const Node *GetNode(const Configuration *config, size_t node_index) {
1155 if (!MultiNode(config)) {
1156 CHECK_EQ(node_index, 0u) << ": Invalid node in a single node world.";
1157 return nullptr;
1158 } else {
1159 CHECK_LT(node_index, config->nodes()->size());
1160 return config->nodes()->Get(node_index);
1161 }
1162}
1163
Austin Schuhc9e10ec2020-01-26 16:08:28 -08001164const Node *GetNodeOrDie(const Configuration *config, const Node *node) {
1165 if (!MultiNode(config)) {
1166 CHECK(node == nullptr) << ": Provided a node in a single node world.";
1167 return nullptr;
1168 } else {
1169 const Node *config_node = GetNode(config, node);
1170 if (config_node == nullptr) {
1171 LOG(FATAL) << "Couldn't find node matching " << FlatbufferToJson(node);
1172 }
1173 return config_node;
1174 }
1175}
1176
Austin Schuh8bd96322020-02-13 21:18:22 -08001177namespace {
1178int GetNodeIndexFromConfig(const Configuration *config, const Node *node) {
Austin Schuhc9e10ec2020-01-26 16:08:28 -08001179 int node_index = 0;
1180 for (const Node *iterated_node : *config->nodes()) {
1181 if (iterated_node == node) {
1182 return node_index;
1183 }
1184 ++node_index;
1185 }
Austin Schuh8bd96322020-02-13 21:18:22 -08001186 return -1;
1187}
1188} // namespace
1189
Austin Schuha9df9ad2021-06-16 14:49:39 -07001190aos::FlatbufferDetachedBuffer<aos::Configuration> AddSchema(
1191 std::string_view json,
1192 const std::vector<aos::FlatbufferVector<reflection::Schema>> &schemas) {
1193 FlatbufferDetachedBuffer<Configuration> addition =
1194 JsonToFlatbuffer(json, Configuration::MiniReflectTypeTable());
1195 return MergeConfiguration(addition, schemas);
1196}
1197
Austin Schuh8bd96322020-02-13 21:18:22 -08001198int GetNodeIndex(const Configuration *config, const Node *node) {
1199 if (!MultiNode(config)) {
1200 return 0;
1201 }
1202
1203 {
1204 int node_index = GetNodeIndexFromConfig(config, node);
1205 if (node_index != -1) {
1206 return node_index;
1207 }
1208 }
1209
1210 const Node *result = GetNode(config, node);
1211 CHECK(result != nullptr);
1212
1213 {
Austin Schuh04408fc2020-02-16 21:48:54 -08001214 int node_index = GetNodeIndexFromConfig(config, result);
Austin Schuh8bd96322020-02-13 21:18:22 -08001215 if (node_index != -1) {
1216 return node_index;
1217 }
1218 }
1219
1220 LOG(FATAL) << "Node " << FlatbufferToJson(node)
1221 << " not found in the configuration.";
Austin Schuhc9e10ec2020-01-26 16:08:28 -08001222}
1223
Austin Schuh04408fc2020-02-16 21:48:54 -08001224int GetNodeIndex(const Configuration *config, std::string_view name) {
1225 if (!MultiNode(config)) {
1226 return 0;
1227 }
1228
1229 {
1230 int node_index = 0;
1231 for (const Node *iterated_node : *config->nodes()) {
1232 if (iterated_node->name()->string_view() == name) {
1233 return node_index;
1234 }
1235 ++node_index;
1236 }
1237 }
1238 LOG(FATAL) << "Node " << name << " not found in the configuration.";
1239}
1240
Austin Schuh681a2472020-12-31 23:55:40 -08001241size_t NodesCount(const Configuration *config) {
1242 if (!MultiNode(config)) {
1243 return 1u;
1244 }
1245
1246 return config->nodes()->size();
1247}
1248
Austin Schuhc9e10ec2020-01-26 16:08:28 -08001249std::vector<const Node *> GetNodes(const Configuration *config) {
1250 std::vector<const Node *> nodes;
Austin Schuh8bd96322020-02-13 21:18:22 -08001251 if (MultiNode(config)) {
Austin Schuhc9e10ec2020-01-26 16:08:28 -08001252 for (const Node *node : *config->nodes()) {
1253 nodes.emplace_back(node);
1254 }
1255 } else {
1256 nodes.emplace_back(nullptr);
1257 }
1258 return nodes;
1259}
1260
Austin Schuh65465332020-11-05 17:36:53 -08001261std::vector<const Node *> GetNodesWithTag(const Configuration *config,
1262 std::string_view tag) {
1263 std::vector<const Node *> nodes;
1264 if (!MultiNode(config)) {
1265 nodes.emplace_back(nullptr);
1266 } else {
1267 for (const Node *node : *config->nodes()) {
1268 if (!node->has_tags()) {
1269 continue;
1270 }
1271 bool did_found_tag = false;
1272 for (const flatbuffers::String *found_tag : *node->tags()) {
1273 if (found_tag->string_view() == tag) {
1274 did_found_tag = true;
1275 break;
1276 }
1277 }
1278 if (did_found_tag) {
1279 nodes.emplace_back(node);
1280 }
1281 }
1282 }
1283 return nodes;
1284}
1285
Brian Silverman631b6262021-11-10 12:25:08 -08001286bool NodeHasTag(const Node *node, std::string_view tag) {
1287 if (node == nullptr) {
1288 return true;
1289 }
1290
Austin Schuh97a52432022-08-17 15:02:59 -07001291 if (!node->has_tags()) {
1292 return false;
1293 }
1294
Brian Silverman631b6262021-11-10 12:25:08 -08001295 const auto *const tags = node->tags();
1296 return std::find_if(tags->begin(), tags->end(),
1297 [tag](const flatbuffers::String *candidate) {
1298 return candidate->string_view() == tag;
1299 }) != tags->end();
1300}
1301
Austin Schuhac0771c2020-01-07 18:36:30 -08001302bool MultiNode(const Configuration *config) { return config->has_nodes(); }
1303
Austin Schuh217a9782019-12-21 23:02:50 -08001304bool ChannelIsSendableOnNode(const Channel *channel, const Node *node) {
Austin Schuhca4828c2019-12-28 14:21:35 -08001305 if (node == nullptr) {
1306 return true;
1307 }
Austin Schuh3c5dae52020-10-06 18:55:18 -07001308 CHECK(channel->has_source_node()) << FlatbufferToJson(channel);
1309 CHECK(node->has_name()) << FlatbufferToJson(node);
Austin Schuh196a4452020-03-15 23:12:03 -07001310 return (CHECK_NOTNULL(channel)->source_node()->string_view() ==
1311 node->name()->string_view());
Austin Schuh217a9782019-12-21 23:02:50 -08001312}
1313
1314bool ChannelIsReadableOnNode(const Channel *channel, const Node *node) {
Austin Schuhca4828c2019-12-28 14:21:35 -08001315 if (node == nullptr) {
1316 return true;
1317 }
1318
Austin Schuh217a9782019-12-21 23:02:50 -08001319 if (channel->source_node()->string_view() == node->name()->string_view()) {
1320 return true;
1321 }
1322
1323 if (!channel->has_destination_nodes()) {
1324 return false;
1325 }
1326
Austin Schuh719946b2019-12-28 14:51:01 -08001327 for (const Connection *connection : *channel->destination_nodes()) {
1328 CHECK(connection->has_name());
1329 if (connection->name()->string_view() == node->name()->string_view()) {
Austin Schuh217a9782019-12-21 23:02:50 -08001330 return true;
1331 }
1332 }
1333
1334 return false;
1335}
1336
Austin Schuh719946b2019-12-28 14:51:01 -08001337bool ChannelMessageIsLoggedOnNode(const Channel *channel, const Node *node) {
Austin Schuh48e94502021-06-18 18:35:53 -07001338 if (node == nullptr) {
Austin Schuh2bb80e02021-03-20 21:46:17 -07001339 // Single node world. If there is a local logger, then we want to use
1340 // it.
Austin Schuh48e94502021-06-18 18:35:53 -07001341 if (channel->logger() == LoggerConfig::LOCAL_LOGGER) {
1342 return true;
1343 } else if (channel->logger() == LoggerConfig::NOT_LOGGED) {
1344 return false;
1345 }
1346 LOG(FATAL) << "Unsupported logging configuration in a single node world: "
1347 << CleanedChannelToString(channel);
Austin Schuh2bb80e02021-03-20 21:46:17 -07001348 }
1349 return ChannelMessageIsLoggedOnNode(
1350 channel, CHECK_NOTNULL(node)->name()->string_view());
1351}
1352
1353bool ChannelMessageIsLoggedOnNode(const Channel *channel,
1354 std::string_view node_name) {
Austin Schuhf1fff282020-03-28 16:57:32 -07001355 switch (channel->logger()) {
Austin Schuh719946b2019-12-28 14:51:01 -08001356 case LoggerConfig::LOCAL_LOGGER:
Austin Schuh2bb80e02021-03-20 21:46:17 -07001357 return channel->source_node()->string_view() == node_name;
Austin Schuh719946b2019-12-28 14:51:01 -08001358 case LoggerConfig::LOCAL_AND_REMOTE_LOGGER:
Austin Schuhda40e472020-03-28 15:15:29 -07001359 CHECK(channel->has_logger_nodes());
1360 CHECK_GT(channel->logger_nodes()->size(), 0u);
Austin Schuh719946b2019-12-28 14:51:01 -08001361
Austin Schuh2bb80e02021-03-20 21:46:17 -07001362 if (channel->source_node()->string_view() == node_name) {
Austin Schuh719946b2019-12-28 14:51:01 -08001363 return true;
1364 }
Austin Schuhda40e472020-03-28 15:15:29 -07001365
1366 [[fallthrough]];
1367 case LoggerConfig::REMOTE_LOGGER:
1368 CHECK(channel->has_logger_nodes());
1369 CHECK_GT(channel->logger_nodes()->size(), 0u);
1370 for (const flatbuffers::String *logger_node : *channel->logger_nodes()) {
Austin Schuh2bb80e02021-03-20 21:46:17 -07001371 if (logger_node->string_view() == node_name) {
Austin Schuhda40e472020-03-28 15:15:29 -07001372 return true;
1373 }
Austin Schuh719946b2019-12-28 14:51:01 -08001374 }
1375
1376 return false;
1377 case LoggerConfig::NOT_LOGGED:
1378 return false;
1379 }
1380
1381 LOG(FATAL) << "Unknown logger config " << static_cast<int>(channel->logger());
1382}
1383
Austin Schuh58646e22021-08-23 23:51:46 -07001384size_t ConnectionCount(const Channel *channel) {
1385 if (!channel->has_destination_nodes()) {
1386 return 0;
1387 }
1388 return channel->destination_nodes()->size();
1389}
1390
Austin Schuh719946b2019-12-28 14:51:01 -08001391const Connection *ConnectionToNode(const Channel *channel, const Node *node) {
1392 if (!channel->has_destination_nodes()) {
1393 return nullptr;
1394 }
1395 for (const Connection *connection : *channel->destination_nodes()) {
1396 if (connection->name()->string_view() == node->name()->string_view()) {
1397 return connection;
1398 }
1399 }
1400 return nullptr;
1401}
1402
1403bool ConnectionDeliveryTimeIsLoggedOnNode(const Channel *channel,
1404 const Node *node,
1405 const Node *logger_node) {
Austin Schuh72211ae2021-08-05 14:02:30 -07001406 return ConnectionDeliveryTimeIsLoggedOnNode(ConnectionToNode(channel, node),
1407 logger_node);
Austin Schuh719946b2019-12-28 14:51:01 -08001408}
1409
1410bool ConnectionDeliveryTimeIsLoggedOnNode(const Connection *connection,
1411 const Node *node) {
Austin Schuh72211ae2021-08-05 14:02:30 -07001412 if (connection == nullptr) {
1413 return false;
1414 }
Austin Schuh719946b2019-12-28 14:51:01 -08001415 switch (connection->timestamp_logger()) {
1416 case LoggerConfig::LOCAL_AND_REMOTE_LOGGER:
Austin Schuhda40e472020-03-28 15:15:29 -07001417 CHECK(connection->has_timestamp_logger_nodes());
1418 CHECK_GT(connection->timestamp_logger_nodes()->size(), 0u);
Austin Schuh719946b2019-12-28 14:51:01 -08001419 if (connection->name()->string_view() == node->name()->string_view()) {
1420 return true;
1421 }
1422
Austin Schuhda40e472020-03-28 15:15:29 -07001423 [[fallthrough]];
1424 case LoggerConfig::REMOTE_LOGGER:
1425 CHECK(connection->has_timestamp_logger_nodes());
1426 CHECK_GT(connection->timestamp_logger_nodes()->size(), 0u);
1427 for (const flatbuffers::String *timestamp_logger_node :
1428 *connection->timestamp_logger_nodes()) {
1429 if (timestamp_logger_node->string_view() ==
1430 node->name()->string_view()) {
1431 return true;
1432 }
Austin Schuh719946b2019-12-28 14:51:01 -08001433 }
1434
1435 return false;
1436 case LoggerConfig::LOCAL_LOGGER:
1437 return connection->name()->string_view() == node->name()->string_view();
Austin Schuh719946b2019-12-28 14:51:01 -08001438 case LoggerConfig::NOT_LOGGED:
1439 return false;
1440 }
1441
1442 LOG(FATAL) << "Unknown logger config "
1443 << static_cast<int>(connection->timestamp_logger());
1444}
1445
Austin Schuhe84c3ed2019-12-14 15:29:48 -08001446std::vector<std::string_view> SourceNodeNames(const Configuration *config,
1447 const Node *my_node) {
1448 std::set<std::string_view> result_set;
1449
1450 for (const Channel *channel : *config->channels()) {
1451 if (channel->has_destination_nodes()) {
1452 for (const Connection *connection : *channel->destination_nodes()) {
1453 if (connection->name()->string_view() ==
1454 my_node->name()->string_view()) {
1455 result_set.insert(channel->source_node()->string_view());
1456 }
1457 }
1458 }
1459 }
1460
1461 std::vector<std::string_view> result;
1462 for (const std::string_view source : result_set) {
1463 VLOG(1) << "Found a source node of " << source;
1464 result.emplace_back(source);
1465 }
1466 return result;
1467}
1468
1469std::vector<std::string_view> DestinationNodeNames(const Configuration *config,
1470 const Node *my_node) {
1471 std::vector<std::string_view> result;
1472
1473 for (const Channel *channel : *config->channels()) {
1474 if (channel->has_source_node() && channel->source_node()->string_view() ==
1475 my_node->name()->string_view()) {
1476 if (!channel->has_destination_nodes()) continue;
1477
1478 if (channel->source_node()->string_view() !=
1479 my_node->name()->string_view()) {
1480 continue;
1481 }
1482
1483 for (const Connection *connection : *channel->destination_nodes()) {
1484 if (std::find(result.begin(), result.end(),
1485 connection->name()->string_view()) == result.end()) {
1486 result.emplace_back(connection->name()->string_view());
1487 }
1488 }
1489 }
1490 }
1491
1492 for (const std::string_view destination : result) {
1493 VLOG(1) << "Found a destination node of " << destination;
1494 }
1495 return result;
1496}
1497
Austin Schuh8d7e0bb2020-10-02 17:57:00 -07001498std::vector<const Node *> TimestampNodes(const Configuration *config,
1499 const Node *my_node) {
1500 if (!configuration::MultiNode(config)) {
1501 CHECK(my_node == nullptr);
1502 return std::vector<const Node *>{};
1503 }
1504
1505 std::set<const Node *> timestamp_logger_nodes;
1506 for (const Channel *channel : *config->channels()) {
1507 if (!configuration::ChannelIsSendableOnNode(channel, my_node)) {
1508 continue;
1509 }
1510 if (!channel->has_destination_nodes()) {
1511 continue;
1512 }
1513 for (const Connection *connection : *channel->destination_nodes()) {
1514 const Node *other_node =
1515 configuration::GetNode(config, connection->name()->string_view());
1516
1517 if (configuration::ConnectionDeliveryTimeIsLoggedOnNode(connection,
1518 my_node)) {
1519 VLOG(1) << "Timestamps are logged from "
1520 << FlatbufferToJson(other_node);
1521 timestamp_logger_nodes.insert(other_node);
1522 }
1523 }
1524 }
1525
1526 std::vector<const Node *> result;
1527 for (const Node *node : timestamp_logger_nodes) {
1528 result.emplace_back(node);
1529 }
1530 return result;
1531}
1532
Austin Schuhc41fa3c2021-10-16 14:35:35 -07001533bool ApplicationShouldStart(const Configuration *config, const Node *my_node,
1534 const Application *application) {
1535 if (MultiNode(config)) {
1536 // Ok, we need
1537 CHECK(application->has_nodes());
1538 CHECK(my_node != nullptr);
1539 for (const flatbuffers::String *str : *application->nodes()) {
1540 if (str->string_view() == my_node->name()->string_view()) {
1541 return true;
1542 }
1543 }
1544 return false;
1545 } else {
1546 return true;
1547 }
1548}
1549
Austin Schuhd2e2f6a2021-02-07 20:46:16 -08001550const Application *GetApplication(const Configuration *config,
1551 const Node *my_node,
1552 std::string_view application_name) {
1553 if (config->has_applications()) {
1554 auto application_iterator = std::lower_bound(
1555 config->applications()->cbegin(), config->applications()->cend(),
1556 application_name, CompareApplications);
1557 if (application_iterator != config->applications()->cend() &&
1558 EqualsApplications(*application_iterator, application_name)) {
Austin Schuhc41fa3c2021-10-16 14:35:35 -07001559 if (ApplicationShouldStart(config, my_node, *application_iterator)) {
Austin Schuhd2e2f6a2021-02-07 20:46:16 -08001560 return *application_iterator;
1561 }
1562 }
1563 }
1564 return nullptr;
1565}
1566
Austin Schuhfc7b6a02021-07-12 21:19:07 -07001567std::vector<size_t> SourceNodeIndex(const Configuration *config) {
1568 CHECK(config->has_channels());
1569 std::vector<size_t> result;
1570 result.resize(config->channels()->size(), 0u);
1571 if (MultiNode(config)) {
1572 for (size_t i = 0; i < config->channels()->size(); ++i) {
1573 result[i] = GetNodeIndex(
1574 config, config->channels()->Get(i)->source_node()->string_view());
1575 }
1576 }
1577 return result;
1578}
1579
Austin Schuhfb37c612022-08-11 15:24:51 -07001580int QueueSize(const Configuration *config, const Channel *channel) {
1581 return QueueSize(channel->frequency(),
1582 chrono::nanoseconds(config->channel_storage_duration()));
1583}
1584
1585int QueueSize(size_t frequency, chrono::nanoseconds channel_storage_duration) {
1586 // Use integer arithmetic and round up at all cost.
1587 return static_cast<int>(
1588 (999999999 + static_cast<int64_t>(frequency) *
1589 static_cast<int64_t>(channel_storage_duration.count())) /
1590 static_cast<int64_t>(1000000000));
1591}
1592
1593int QueueScratchBufferSize(const Channel *channel) {
1594 return channel->num_readers() + channel->num_senders();
1595}
1596
Brian Silverman66f079a2013-08-26 16:24:30 -07001597} // namespace configuration
1598} // namespace aos