blob: ed99e2aa8d743ac79ffff58b16e58a9b35f0afc9 [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 {
32bool EndsWith(std::string_view str, std::string_view end) {
33 if (str.size() < end.size()) {
34 return false;
35 }
36 if (str.substr(str.size() - end.size(), end.size()) != end) {
37 return false;
38 }
39 return true;
40}
41
42std::string MaybeReplaceExtension(std::string_view filename,
43 std::string_view extension,
44 std::string_view replacement) {
45 if (!EndsWith(filename, extension)) {
46 return std::string(filename);
47 }
48 filename.remove_suffix(extension.size());
49 return absl::StrCat(filename, replacement);
50}
51
52FlatbufferDetachedBuffer<Configuration> ReadConfigFile(std::string_view path,
53 bool binary) {
54 if (binary) {
55 FlatbufferVector<Configuration> config =
56 FileToFlatbuffer<Configuration>(path);
57 return CopySpanAsDetachedBuffer(config.span());
58 }
59
60 flatbuffers::DetachedBuffer buffer = JsonToFlatbuffer(
61 util::ReadFileToStringOrDie(path), ConfigurationTypeTable());
62
Austin Schuh84a039a2021-11-03 16:50:34 -070063 CHECK_GT(buffer.size(), 0u) << ": Failed to parse JSON file: " << path;
Austin Schuh15182322020-10-10 15:25:21 -070064
65 return FlatbufferDetachedBuffer<Configuration>(std::move(buffer));
66}
67
68} // namespace
Austin Schuhcb108412019-10-13 16:09:54 -070069
Austin Schuh40485ed2019-10-26 21:51:44 -070070// Define the compare and equal operators for Channel and Application so we can
Austin Schuhcb108412019-10-13 16:09:54 -070071// insert them in the btree below.
Austin Schuh40485ed2019-10-26 21:51:44 -070072bool operator<(const FlatbufferDetachedBuffer<Channel> &lhs,
73 const FlatbufferDetachedBuffer<Channel> &rhs) {
Austin Schuhcb108412019-10-13 16:09:54 -070074 int name_compare = lhs.message().name()->string_view().compare(
75 rhs.message().name()->string_view());
76 if (name_compare == 0) {
77 return lhs.message().type()->string_view() <
78 rhs.message().type()->string_view();
79 } else if (name_compare < 0) {
80 return true;
81 } else {
82 return false;
83 }
84}
85
Austin Schuh40485ed2019-10-26 21:51:44 -070086bool operator==(const FlatbufferDetachedBuffer<Channel> &lhs,
87 const FlatbufferDetachedBuffer<Channel> &rhs) {
Austin Schuhcb108412019-10-13 16:09:54 -070088 return lhs.message().name()->string_view() ==
89 rhs.message().name()->string_view() &&
90 lhs.message().type()->string_view() ==
91 rhs.message().type()->string_view();
92}
93
Austin Schuha7996eb2021-10-11 19:03:24 -070094bool operator<(const FlatbufferDetachedBuffer<Connection> &lhs,
95 const FlatbufferDetachedBuffer<Connection> &rhs) {
96 return lhs.message().name()->string_view() <
97 rhs.message().name()->string_view();
98}
99
100bool operator==(const FlatbufferDetachedBuffer<Connection> &lhs,
101 const FlatbufferDetachedBuffer<Connection> &rhs) {
102 return lhs.message().name()->string_view() ==
103 rhs.message().name()->string_view();
104}
105
Austin Schuh40485ed2019-10-26 21:51:44 -0700106bool operator==(const FlatbufferDetachedBuffer<Application> &lhs,
107 const FlatbufferDetachedBuffer<Application> &rhs) {
Austin Schuhcb108412019-10-13 16:09:54 -0700108 return lhs.message().name()->string_view() ==
109 rhs.message().name()->string_view();
110}
111
Austin Schuh40485ed2019-10-26 21:51:44 -0700112bool operator<(const FlatbufferDetachedBuffer<Application> &lhs,
113 const FlatbufferDetachedBuffer<Application> &rhs) {
Austin Schuhcb108412019-10-13 16:09:54 -0700114 return lhs.message().name()->string_view() <
115 rhs.message().name()->string_view();
116}
117
Austin Schuh217a9782019-12-21 23:02:50 -0800118bool operator==(const FlatbufferDetachedBuffer<Node> &lhs,
119 const FlatbufferDetachedBuffer<Node> &rhs) {
120 return lhs.message().name()->string_view() ==
121 rhs.message().name()->string_view();
122}
123
124bool operator<(const FlatbufferDetachedBuffer<Node> &lhs,
125 const FlatbufferDetachedBuffer<Node> &rhs) {
126 return lhs.message().name()->string_view() <
127 rhs.message().name()->string_view();
128}
129
Brian Silverman66f079a2013-08-26 16:24:30 -0700130namespace configuration {
131namespace {
132
Austin Schuhcb108412019-10-13 16:09:54 -0700133// Extracts the folder part of a path. Returns ./ if there is no path.
Austin Schuhf1fff282020-03-28 16:57:32 -0700134std::string_view ExtractFolder(const std::string_view filename) {
Austin Schuhcb108412019-10-13 16:09:54 -0700135 auto last_slash_pos = filename.find_last_of("/\\");
136
James Kuszmaul3ae42262019-11-08 12:33:41 -0800137 return last_slash_pos == std::string_view::npos
138 ? std::string_view("./")
Austin Schuhcb108412019-10-13 16:09:54 -0700139 : filename.substr(0, last_slash_pos + 1);
140}
141
James Kuszmaulc0c08da2020-05-10 18:56:07 -0700142std::string AbsolutePath(const std::string_view filename) {
143 // Uses an std::string so that we know the input will be null-terminated.
144 const std::string terminated_file(filename);
145 char buffer[PATH_MAX];
146 PCHECK(NULL != realpath(terminated_file.c_str(), buffer));
147 return buffer;
148}
149
Austin Schuhef38cd22021-07-21 15:24:23 -0700150std::string RemoveDotDots(const std::string_view filename) {
151 std::vector<std::string> split = absl::StrSplit(filename, '/');
152 auto iterator = split.begin();
153 while (iterator != split.end()) {
154 if (iterator->empty()) {
155 iterator = split.erase(iterator);
156 } else if (*iterator == ".") {
157 iterator = split.erase(iterator);
158 } else if (*iterator == "..") {
159 CHECK(iterator != split.begin())
160 << ": Import path may not start with ..: " << filename;
161 auto previous = iterator;
162 --previous;
163 split.erase(iterator);
164 iterator = split.erase(previous);
165 } else {
166 ++iterator;
167 }
168 }
169 return absl::StrJoin(split, "/");
170}
171
Milind Upadhyay17098ba2022-04-15 22:18:50 -0700172std::optional<FlatbufferDetachedBuffer<Configuration>> MaybeReadConfig(
James Kuszmaulc0c08da2020-05-10 18:56:07 -0700173 const std::string_view path, absl::btree_set<std::string> *visited_paths,
174 const std::vector<std::string_view> &extra_import_paths) {
Austin Schuh15182322020-10-10 15:25:21 -0700175 std::string binary_path = MaybeReplaceExtension(path, ".json", ".bfbs");
Austin Schuhef38cd22021-07-21 15:24:23 -0700176 VLOG(1) << "Looking up: " << path << ", starting with: " << binary_path;
Austin Schuh15182322020-10-10 15:25:21 -0700177 bool binary_path_exists = util::PathExists(binary_path);
James Kuszmaulc0c08da2020-05-10 18:56:07 -0700178 std::string raw_path(path);
Austin Schuh15182322020-10-10 15:25:21 -0700179 // For each .json file, look and see if we can find a .bfbs file next to it
180 // with the same base name. If we can, assume it is the same and use it
181 // instead. It is much faster to load .bfbs files than .json files.
182 if (!binary_path_exists && !util::PathExists(raw_path)) {
183 const bool path_is_absolute = raw_path.size() > 0 && raw_path[0] == '/';
Milind Upadhyay17098ba2022-04-15 22:18:50 -0700184 if (path_is_absolute && !extra_import_paths.empty()) {
185 LOG(ERROR)
James Kuszmaulc0c08da2020-05-10 18:56:07 -0700186 << "Can't specify extra import paths if attempting to read a config "
187 "file from an absolute path (path is "
Austin Schuh15182322020-10-10 15:25:21 -0700188 << raw_path << ").";
Milind Upadhyay17098ba2022-04-15 22:18:50 -0700189 return std::nullopt;
James Kuszmaulc0c08da2020-05-10 18:56:07 -0700190 }
191
192 bool found_path = false;
193 for (const auto &import_path : extra_import_paths) {
Austin Schuhef38cd22021-07-21 15:24:23 -0700194 raw_path = std::string(import_path) + "/" + RemoveDotDots(path);
Austin Schuh15182322020-10-10 15:25:21 -0700195 binary_path = MaybeReplaceExtension(raw_path, ".json", ".bfbs");
Austin Schuhef38cd22021-07-21 15:24:23 -0700196 VLOG(1) << "Checking: " << binary_path;
Austin Schuh15182322020-10-10 15:25:21 -0700197 binary_path_exists = util::PathExists(binary_path);
198 if (binary_path_exists) {
199 found_path = true;
200 break;
201 }
Austin Schuhef38cd22021-07-21 15:24:23 -0700202 VLOG(1) << "Checking: " << raw_path;
James Kuszmaulc0c08da2020-05-10 18:56:07 -0700203 if (util::PathExists(raw_path)) {
204 found_path = true;
205 break;
206 }
207 }
Milind Upadhyay17098ba2022-04-15 22:18:50 -0700208 if (!found_path) {
209 LOG(ERROR) << ": Failed to find file " << path << ".";
210 return std::nullopt;
211 }
James Kuszmaulc0c08da2020-05-10 18:56:07 -0700212 }
Alex Perrycb7da4b2019-08-28 19:35:56 -0700213
Milind Upadhyay17098ba2022-04-15 22:18:50 -0700214 std::optional<FlatbufferDetachedBuffer<Configuration>> config =
215 ReadConfigFile(binary_path_exists ? binary_path : raw_path,
216 binary_path_exists);
James Kuszmaulc0c08da2020-05-10 18:56:07 -0700217
Austin Schuhcb108412019-10-13 16:09:54 -0700218 // Depth first. Take the following example:
219 //
220 // config1.json:
221 // {
Austin Schuh40485ed2019-10-26 21:51:44 -0700222 // "channels": [
Austin Schuhcb108412019-10-13 16:09:54 -0700223 // {
224 // "name": "/foo",
225 // "type": ".aos.bar",
226 // "max_size": 5
227 // }
228 // ],
229 // "imports": [
230 // "config2.json",
231 // ]
232 // }
233 //
234 // config2.json:
235 // {
Austin Schuh40485ed2019-10-26 21:51:44 -0700236 // "channels": [
Austin Schuhcb108412019-10-13 16:09:54 -0700237 // {
238 // "name": "/foo",
239 // "type": ".aos.bar",
240 // "max_size": 7
241 // }
242 // ],
243 // }
244 //
245 // We want the main config (config1.json) to be able to override the imported
246 // config. That means that it needs to be merged into the imported configs,
247 // not the other way around.
248
Austin Schuh15182322020-10-10 15:25:21 -0700249 const std::string absolute_path =
250 AbsolutePath(binary_path_exists ? binary_path : raw_path);
251 // Track that we have seen this file before recursing. Track the path we
252 // actually loaded (which should be consistent if imported twice).
James Kuszmaulc0c08da2020-05-10 18:56:07 -0700253 if (!visited_paths->insert(absolute_path).second) {
254 for (const auto &visited_path : *visited_paths) {
255 LOG(INFO) << "Already visited: " << visited_path;
256 }
257 LOG(FATAL)
258 << "Already imported " << path << " (i.e. " << absolute_path
259 << "). See above for the files that have already been processed.";
Milind Upadhyay17098ba2022-04-15 22:18:50 -0700260 return std::nullopt;
James Kuszmaulc0c08da2020-05-10 18:56:07 -0700261 }
Austin Schuhcb108412019-10-13 16:09:54 -0700262
Milind Upadhyay17098ba2022-04-15 22:18:50 -0700263 if (config->message().has_imports()) {
Austin Schuhcb108412019-10-13 16:09:54 -0700264 // Capture the imports.
265 const flatbuffers::Vector<flatbuffers::Offset<flatbuffers::String>> *v =
Milind Upadhyay17098ba2022-04-15 22:18:50 -0700266 config->message().imports();
Austin Schuhcb108412019-10-13 16:09:54 -0700267
268 // And then wipe them. This gets GCed when we merge later.
Milind Upadhyay17098ba2022-04-15 22:18:50 -0700269 config->mutable_message()->clear_imports();
Austin Schuhcb108412019-10-13 16:09:54 -0700270
271 // Start with an empty configuration to merge into.
Austin Schuh40485ed2019-10-26 21:51:44 -0700272 FlatbufferDetachedBuffer<Configuration> merged_config =
273 FlatbufferDetachedBuffer<Configuration>::Empty();
Austin Schuhcb108412019-10-13 16:09:54 -0700274
James Kuszmaulc0c08da2020-05-10 18:56:07 -0700275 const std::string path_folder(ExtractFolder(path));
Austin Schuhcb108412019-10-13 16:09:54 -0700276 for (const flatbuffers::String *str : *v) {
James Kuszmaulc0c08da2020-05-10 18:56:07 -0700277 const std::string included_config =
278 path_folder + "/" + std::string(str->string_view());
Austin Schuhcb108412019-10-13 16:09:54 -0700279
Milind Upadhyay17098ba2022-04-15 22:18:50 -0700280 const auto optional_config =
281 MaybeReadConfig(included_config, visited_paths, extra_import_paths);
282 if (!optional_config.has_value()) {
283 return std::nullopt;
284 }
Austin Schuhcb108412019-10-13 16:09:54 -0700285 // And them merge everything in.
Milind Upadhyay17098ba2022-04-15 22:18:50 -0700286 merged_config = MergeFlatBuffers(merged_config, *optional_config);
Austin Schuhcb108412019-10-13 16:09:54 -0700287 }
288
289 // Finally, merge this file in.
Milind Upadhyay17098ba2022-04-15 22:18:50 -0700290 config = MergeFlatBuffers(merged_config, *config);
Austin Schuhcb108412019-10-13 16:09:54 -0700291 }
292 return config;
293}
294
Alex Perrycb7da4b2019-08-28 19:35:56 -0700295// Compares (c < p) a channel, and a name, type tuple.
296bool CompareChannels(const Channel *c,
James Kuszmaul3ae42262019-11-08 12:33:41 -0800297 ::std::pair<std::string_view, std::string_view> p) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700298 int name_compare = c->name()->string_view().compare(p.first);
299 if (name_compare == 0) {
300 return c->type()->string_view() < p.second;
301 } else if (name_compare < 0) {
302 return true;
303 } else {
304 return false;
305 }
306};
307
308// Compares for equality (c == p) a channel, and a name, type tuple.
309bool EqualsChannels(const Channel *c,
Austin Schuhf1fff282020-03-28 16:57:32 -0700310 ::std::pair<std::string_view, std::string_view> p) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700311 return c->name()->string_view() == p.first &&
312 c->type()->string_view() == p.second;
313}
314
315// Compares (c < p) an application, and a name;
James Kuszmaul3ae42262019-11-08 12:33:41 -0800316bool CompareApplications(const Application *a, std::string_view name) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700317 return a->name()->string_view() < name;
318};
319
320// Compares for equality (c == p) an application, and a name;
James Kuszmaul3ae42262019-11-08 12:33:41 -0800321bool EqualsApplications(const Application *a, std::string_view name) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700322 return a->name()->string_view() == name;
323}
324
Austin Schuh15182322020-10-10 15:25:21 -0700325void ValidateConfiguration(const Flatbuffer<Configuration> &config) {
326 // No imports should be left.
327 CHECK(!config.message().has_imports());
328
329 // Check that if there is a node list, all the source nodes are filled out and
330 // valid, and all the destination nodes are valid (and not the source). This
331 // is a basic consistency check.
332 if (config.message().has_channels()) {
333 const Channel *last_channel = nullptr;
334 for (const Channel *c : *config.message().channels()) {
335 CHECK(c->has_name());
336 CHECK(c->has_type());
337 if (c->name()->string_view().back() == '/') {
338 LOG(FATAL) << "Channel names can't end with '/'";
339 }
340 if (c->name()->string_view().find("//") != std::string_view::npos) {
341 LOG(FATAL) << ": Invalid channel name " << c->name()->string_view()
342 << ", can't use //.";
343 }
344 for (const char data : c->name()->string_view()) {
345 if (data >= '0' && data <= '9') {
346 continue;
347 }
348 if (data >= 'a' && data <= 'z') {
349 continue;
350 }
351 if (data >= 'A' && data <= 'Z') {
352 continue;
353 }
354 if (data == '-' || data == '_' || data == '/') {
355 continue;
356 }
357 LOG(FATAL) << "Invalid channel name " << c->name()->string_view()
358 << ", can only use [-a-zA-Z0-9_/]";
359 }
360
Austin Schuha156fb22021-10-11 19:23:21 -0700361 if (c->has_logger_nodes()) {
362 // Confirm that we don't have duplicate logger nodes.
363 absl::btree_set<std::string_view> logger_nodes;
364 for (const flatbuffers::String *s : *c->logger_nodes()) {
365 logger_nodes.insert(s->string_view());
366 }
367 CHECK_EQ(static_cast<size_t>(logger_nodes.size()),
368 c->logger_nodes()->size())
369 << ": Found duplicate logger_nodes in "
370 << CleanedChannelToString(c);
371 }
372
373 if (c->has_destination_nodes()) {
374 // Confirm that we don't have duplicate timestamp logger nodes.
Austin Schuh5e95bd62021-10-11 18:40:22 -0700375 for (const Connection *d : *c->destination_nodes()) {
Austin Schuha156fb22021-10-11 19:23:21 -0700376 if (d->has_timestamp_logger_nodes()) {
377 absl::btree_set<std::string_view> timestamp_logger_nodes;
378 for (const flatbuffers::String *s : *d->timestamp_logger_nodes()) {
379 timestamp_logger_nodes.insert(s->string_view());
380 }
381 CHECK_EQ(static_cast<size_t>(timestamp_logger_nodes.size()),
382 d->timestamp_logger_nodes()->size())
383 << ": Found duplicate timestamp_logger_nodes in "
384 << CleanedChannelToString(c);
385 }
386 }
387
388 // There is no good use case today for logging timestamps but not the
389 // corresponding data. Instead of plumbing through all of this on the
390 // reader side, let'd just disallow it for now.
391 if (c->logger() == LoggerConfig::NOT_LOGGED) {
392 for (const Connection *d : *c->destination_nodes()) {
393 CHECK(d->timestamp_logger() == LoggerConfig::NOT_LOGGED)
394 << ": Logging timestamps without data is not supported. If "
395 "you have a good use case, let's talk. "
396 << CleanedChannelToString(c);
397 }
Austin Schuh5e95bd62021-10-11 18:40:22 -0700398 }
399 }
400
Austin Schuh15182322020-10-10 15:25:21 -0700401 // Make sure everything is sorted while we are here... If this fails,
402 // there will be a bunch of weird errors.
403 if (last_channel != nullptr) {
404 CHECK(CompareChannels(
405 last_channel,
406 std::make_pair(c->name()->string_view(), c->type()->string_view())))
407 << ": Channels not sorted!";
408 }
409 last_channel = c;
410 }
411 }
412
413 if (config.message().has_nodes() && config.message().has_channels()) {
414 for (const Channel *c : *config.message().channels()) {
415 CHECK(c->has_source_node()) << ": Channel " << FlatbufferToJson(c)
416 << " is missing \"source_node\"";
417 CHECK(GetNode(&config.message(), c->source_node()->string_view()) !=
418 nullptr)
419 << ": Channel " << FlatbufferToJson(c)
420 << " has an unknown \"source_node\"";
421
422 if (c->has_destination_nodes()) {
423 for (const Connection *connection : *c->destination_nodes()) {
424 CHECK(connection->has_name());
425 CHECK(GetNode(&config.message(), connection->name()->string_view()) !=
426 nullptr)
427 << ": Channel " << FlatbufferToJson(c)
428 << " has an unknown \"destination_nodes\" "
429 << connection->name()->string_view();
430
431 switch (connection->timestamp_logger()) {
432 case LoggerConfig::LOCAL_LOGGER:
433 case LoggerConfig::NOT_LOGGED:
434 CHECK(!connection->has_timestamp_logger_nodes());
435 break;
436 case LoggerConfig::REMOTE_LOGGER:
437 case LoggerConfig::LOCAL_AND_REMOTE_LOGGER:
438 CHECK(connection->has_timestamp_logger_nodes());
439 CHECK_GT(connection->timestamp_logger_nodes()->size(), 0u);
440 for (const flatbuffers::String *timestamp_logger_node :
441 *connection->timestamp_logger_nodes()) {
442 CHECK(GetNode(&config.message(),
443 timestamp_logger_node->string_view()) != nullptr)
444 << ": Channel " << FlatbufferToJson(c)
445 << " has an unknown \"timestamp_logger_node\""
446 << connection->name()->string_view();
447 }
448 break;
449 }
450
451 CHECK_NE(connection->name()->string_view(),
452 c->source_node()->string_view())
453 << ": Channel " << FlatbufferToJson(c)
454 << " is forwarding data to itself";
455 }
456 }
457 }
458 }
459}
460
Alex Perrycb7da4b2019-08-28 19:35:56 -0700461} // namespace
462
Austin Schuh006a9f52021-04-07 16:24:18 -0700463// Maps name for the provided maps. Modifies name.
Brian Silvermanf3798cb2021-11-10 12:26:34 -0800464//
465// This is called many times during startup, and it dereferences a lot of
466// pointers. These combine to make it a performance hotspot during many tests
467// under msan, so there is some optimizing around caching intermediates instead
468// of dereferencing the pointer multiple times.
Austin Schuh006a9f52021-04-07 16:24:18 -0700469void HandleMaps(const flatbuffers::Vector<flatbuffers::Offset<aos::Map>> *maps,
470 std::string *name, std::string_view type, const Node *node) {
471 // For the same reason we merge configs in reverse order, we want to process
472 // maps in reverse order. That lets the outer config overwrite channels from
473 // the inner configs.
474 for (auto i = maps->rbegin(); i != maps->rend(); ++i) {
Brian Silvermanf3798cb2021-11-10 12:26:34 -0800475 const Channel *const match = i->match();
476 if (!match) {
Austin Schuh006a9f52021-04-07 16:24:18 -0700477 continue;
478 }
Brian Silvermanf3798cb2021-11-10 12:26:34 -0800479 const flatbuffers::String *const match_name_string = match->name();
480 if (!match_name_string) {
481 continue;
482 }
483 const Channel *const rename = i->rename();
484 if (!rename) {
485 continue;
486 }
487 const flatbuffers::String *const rename_name_string = rename->name();
488 if (!rename_name_string) {
Austin Schuh006a9f52021-04-07 16:24:18 -0700489 continue;
490 }
491
492 // Handle normal maps (now that we know that match and rename are filled
493 // out).
Brian Silvermanf3798cb2021-11-10 12:26:34 -0800494 const std::string_view match_name = match_name_string->string_view();
Austin Schuh006a9f52021-04-07 16:24:18 -0700495 if (match_name != *name) {
496 if (match_name.back() == '*' &&
497 std::string_view(*name).substr(
498 0, std::min(name->size(), match_name.size() - 1)) ==
499 match_name.substr(0, match_name.size() - 1)) {
500 CHECK_EQ(match_name.find('*'), match_name.size() - 1);
501 } else {
502 continue;
503 }
504 }
505
506 // Handle type specific maps.
Brian Silvermanf3798cb2021-11-10 12:26:34 -0800507 const flatbuffers::String *const match_type_string = match->type();
508 if (match_type_string && match_type_string->string_view() != type) {
Austin Schuh006a9f52021-04-07 16:24:18 -0700509 continue;
510 }
511
512 // Now handle node specific maps.
Brian Silvermanf3798cb2021-11-10 12:26:34 -0800513 const flatbuffers::String *const match_source_node_string =
514 match->source_node();
515 if (node && match_source_node_string &&
516 match_source_node_string->string_view() !=
Austin Schuh006a9f52021-04-07 16:24:18 -0700517 node->name()->string_view()) {
518 continue;
519 }
520
Brian Silvermanf3798cb2021-11-10 12:26:34 -0800521 std::string new_name(rename_name_string->string_view());
Austin Schuh006a9f52021-04-07 16:24:18 -0700522 if (match_name.back() == '*') {
523 new_name += std::string(name->substr(match_name.size() - 1));
524 }
525 VLOG(1) << "Renamed \"" << *name << "\" to \"" << new_name << "\"";
526 *name = std::move(new_name);
527 }
528}
529
Austin Schuh40485ed2019-10-26 21:51:44 -0700530FlatbufferDetachedBuffer<Configuration> MergeConfiguration(
Austin Schuhcb108412019-10-13 16:09:54 -0700531 const Flatbuffer<Configuration> &config) {
James Kuszmaul3c998592020-07-27 21:04:47 -0700532 // auto_merge_config will contain all the fields of the Configuration that are
533 // to be passed through unmodified to the result of MergeConfiguration().
534 // In the processing below, we mutate auto_merge_config to remove any fields
535 // which we do need to alter (hence why we can't use the input config
536 // directly), and then merge auto_merge_config back in at the end.
537 aos::FlatbufferDetachedBuffer<aos::Configuration> auto_merge_config =
Austin Schuha4fc60f2020-11-01 23:06:47 -0800538 aos::RecursiveCopyFlatBuffer(&config.message());
James Kuszmaul3c998592020-07-27 21:04:47 -0700539
Austin Schuh40485ed2019-10-26 21:51:44 -0700540 // Store all the channels in a sorted set. This lets us track channels we
Austin Schuhcb108412019-10-13 16:09:54 -0700541 // have seen before and merge the updates in.
Austin Schuh40485ed2019-10-26 21:51:44 -0700542 absl::btree_set<FlatbufferDetachedBuffer<Channel>> channels;
Austin Schuhcb108412019-10-13 16:09:54 -0700543
Austin Schuh40485ed2019-10-26 21:51:44 -0700544 if (config.message().has_channels()) {
James Kuszmaul3c998592020-07-27 21:04:47 -0700545 auto_merge_config.mutable_message()->clear_channels();
Austin Schuh40485ed2019-10-26 21:51:44 -0700546 for (const Channel *c : *config.message().channels()) {
Austin Schuhcb108412019-10-13 16:09:54 -0700547 // Ignore malformed entries.
Austin Schuh40485ed2019-10-26 21:51:44 -0700548 if (!c->has_name()) {
Austin Schuhcb108412019-10-13 16:09:54 -0700549 continue;
550 }
Austin Schuh40485ed2019-10-26 21:51:44 -0700551 if (!c->has_type()) {
Austin Schuhcb108412019-10-13 16:09:54 -0700552 continue;
553 }
554
Brian Silverman77162972020-08-12 19:52:40 -0700555 CHECK_EQ(c->read_method() == ReadMethod::PIN, c->num_readers() != 0)
556 << ": num_readers may be set if and only if read_method is PIN,"
557 " if you want 0 readers do not set PIN: "
558 << CleanedChannelToString(c);
559
Austin Schuh40485ed2019-10-26 21:51:44 -0700560 // Attempt to insert the channel.
Austin Schuha4fc60f2020-11-01 23:06:47 -0800561 auto result = channels.insert(RecursiveCopyFlatBuffer(c));
Austin Schuhcb108412019-10-13 16:09:54 -0700562 if (!result.second) {
563 // Already there, so merge the new table into the original.
Austin Schuh4a5f5d22021-10-12 15:09:35 -0700564 // Schemas merge poorly, so pick the newest one.
565 if (result.first->message().has_schema() && c->has_schema()) {
566 result.first->mutable_message()->clear_schema();
567 }
Austin Schuha7996eb2021-10-11 19:03:24 -0700568 auto merged =
Austin Schuha4fc60f2020-11-01 23:06:47 -0800569 MergeFlatBuffers(*result.first, RecursiveCopyFlatBuffer(c));
Austin Schuha7996eb2021-10-11 19:03:24 -0700570
571 if (merged.message().has_destination_nodes()) {
572 absl::btree_set<FlatbufferDetachedBuffer<Connection>> connections;
573 for (const Connection *connection :
574 *merged.message().destination_nodes()) {
575 auto connection_result =
576 connections.insert(RecursiveCopyFlatBuffer(connection));
577 if (!connection_result.second) {
578 *connection_result.first =
579 MergeFlatBuffers(*connection_result.first,
580 RecursiveCopyFlatBuffer(connection));
581 }
582 }
583 if (static_cast<size_t>(connections.size()) !=
584 merged.message().destination_nodes()->size()) {
585 merged.mutable_message()->clear_destination_nodes();
586 flatbuffers::FlatBufferBuilder fbb;
587 fbb.ForceDefaults(true);
588 std::vector<flatbuffers::Offset<Connection>> connection_offsets;
589 for (const FlatbufferDetachedBuffer<Connection> &connection :
590 connections) {
591 connection_offsets.push_back(
592 RecursiveCopyFlatBuffer(&connection.message(), &fbb));
593 }
594 flatbuffers::Offset<
595 flatbuffers::Vector<flatbuffers::Offset<Connection>>>
596 destination_nodes_offset = fbb.CreateVector(connection_offsets);
597 Channel::Builder channel_builder(fbb);
598 channel_builder.add_destination_nodes(destination_nodes_offset);
599 fbb.Finish(channel_builder.Finish());
600 FlatbufferDetachedBuffer<Channel> destinations_channel(
601 fbb.Release());
602 merged = MergeFlatBuffers(merged, destinations_channel);
603 }
604 }
605
606 *result.first = std::move(merged);
Austin Schuhcb108412019-10-13 16:09:54 -0700607 }
608 }
609 }
610
611 // Now repeat this for the application list.
Austin Schuh40485ed2019-10-26 21:51:44 -0700612 absl::btree_set<FlatbufferDetachedBuffer<Application>> applications;
Austin Schuhcb108412019-10-13 16:09:54 -0700613 if (config.message().has_applications()) {
James Kuszmaul3c998592020-07-27 21:04:47 -0700614 auto_merge_config.mutable_message()->clear_applications();
Austin Schuhcb108412019-10-13 16:09:54 -0700615 for (const Application *a : *config.message().applications()) {
616 if (!a->has_name()) {
617 continue;
618 }
619
Austin Schuha4fc60f2020-11-01 23:06:47 -0800620 auto result = applications.insert(RecursiveCopyFlatBuffer(a));
Austin Schuhcb108412019-10-13 16:09:54 -0700621 if (!result.second) {
Austin Schuhf81c2522021-12-08 12:03:30 -0800622 if (a->has_args()) {
623 result.first->mutable_message()->clear_args();
624 }
Austin Schuha4fc60f2020-11-01 23:06:47 -0800625 *result.first =
626 MergeFlatBuffers(*result.first, RecursiveCopyFlatBuffer(a));
Austin Schuhcb108412019-10-13 16:09:54 -0700627 }
628 }
629 }
630
Austin Schuh217a9782019-12-21 23:02:50 -0800631 // Now repeat this for the node list.
632 absl::btree_set<FlatbufferDetachedBuffer<Node>> nodes;
633 if (config.message().has_nodes()) {
James Kuszmaul3c998592020-07-27 21:04:47 -0700634 auto_merge_config.mutable_message()->clear_nodes();
Austin Schuh217a9782019-12-21 23:02:50 -0800635 for (const Node *n : *config.message().nodes()) {
636 if (!n->has_name()) {
637 continue;
638 }
639
Austin Schuha4fc60f2020-11-01 23:06:47 -0800640 auto result = nodes.insert(RecursiveCopyFlatBuffer(n));
Austin Schuh217a9782019-12-21 23:02:50 -0800641 if (!result.second) {
Austin Schuha4fc60f2020-11-01 23:06:47 -0800642 *result.first =
643 MergeFlatBuffers(*result.first, RecursiveCopyFlatBuffer(n));
Austin Schuh217a9782019-12-21 23:02:50 -0800644 }
645 }
646 }
647
Austin Schuhcb108412019-10-13 16:09:54 -0700648 flatbuffers::FlatBufferBuilder fbb;
Austin Schuhd7b15da2020-02-17 15:06:11 -0800649 fbb.ForceDefaults(true);
Austin Schuhcb108412019-10-13 16:09:54 -0700650
651 // Start by building the vectors. They need to come before the final table.
Austin Schuh40485ed2019-10-26 21:51:44 -0700652 // Channels
653 flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<Channel>>>
654 channels_offset;
Austin Schuhcb108412019-10-13 16:09:54 -0700655 {
Austin Schuh40485ed2019-10-26 21:51:44 -0700656 ::std::vector<flatbuffers::Offset<Channel>> channel_offsets;
657 for (const FlatbufferDetachedBuffer<Channel> &c : channels) {
Austin Schuha4fc60f2020-11-01 23:06:47 -0800658 channel_offsets.emplace_back(
659 RecursiveCopyFlatBuffer<Channel>(&c.message(), &fbb));
Austin Schuhcb108412019-10-13 16:09:54 -0700660 }
Austin Schuh40485ed2019-10-26 21:51:44 -0700661 channels_offset = fbb.CreateVector(channel_offsets);
Austin Schuhcb108412019-10-13 16:09:54 -0700662 }
663
664 // Applications
665 flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<Application>>>
666 applications_offset;
667 {
668 ::std::vector<flatbuffers::Offset<Application>> applications_offsets;
Austin Schuh40485ed2019-10-26 21:51:44 -0700669 for (const FlatbufferDetachedBuffer<Application> &a : applications) {
Austin Schuhcb108412019-10-13 16:09:54 -0700670 applications_offsets.emplace_back(
Austin Schuha4fc60f2020-11-01 23:06:47 -0800671 RecursiveCopyFlatBuffer<Application>(&a.message(), &fbb));
Austin Schuhcb108412019-10-13 16:09:54 -0700672 }
673 applications_offset = fbb.CreateVector(applications_offsets);
674 }
675
Austin Schuh217a9782019-12-21 23:02:50 -0800676 // Nodes
677 flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<Node>>>
678 nodes_offset;
679 {
680 ::std::vector<flatbuffers::Offset<Node>> node_offsets;
681 for (const FlatbufferDetachedBuffer<Node> &n : nodes) {
Austin Schuha4fc60f2020-11-01 23:06:47 -0800682 node_offsets.emplace_back(
683 RecursiveCopyFlatBuffer<Node>(&n.message(), &fbb));
Austin Schuh217a9782019-12-21 23:02:50 -0800684 }
685 nodes_offset = fbb.CreateVector(node_offsets);
686 }
687
Austin Schuhcb108412019-10-13 16:09:54 -0700688 // And then build a Configuration with them all.
689 ConfigurationBuilder configuration_builder(fbb);
Austin Schuh40485ed2019-10-26 21:51:44 -0700690 configuration_builder.add_channels(channels_offset);
Austin Schuh217a9782019-12-21 23:02:50 -0800691 if (config.message().has_applications()) {
692 configuration_builder.add_applications(applications_offset);
693 }
694 if (config.message().has_nodes()) {
695 configuration_builder.add_nodes(nodes_offset);
696 }
Austin Schuhcb108412019-10-13 16:09:54 -0700697
698 fbb.Finish(configuration_builder.Finish());
Austin Schuh217a9782019-12-21 23:02:50 -0800699
James Kuszmaul3c998592020-07-27 21:04:47 -0700700 aos::FlatbufferDetachedBuffer<aos::Configuration> modified_config(
701 fbb.Release());
702
Austin Schuh217a9782019-12-21 23:02:50 -0800703 // Now, validate that if there is a node list, every channel has a source
704 // node.
James Kuszmaul3c998592020-07-27 21:04:47 -0700705 FlatbufferDetachedBuffer<Configuration> result =
706 MergeFlatBuffers(modified_config, auto_merge_config);
Austin Schuh217a9782019-12-21 23:02:50 -0800707
Austin Schuh15182322020-10-10 15:25:21 -0700708 ValidateConfiguration(result);
Austin Schuh217a9782019-12-21 23:02:50 -0800709
710 return result;
Austin Schuhcb108412019-10-13 16:09:54 -0700711}
712
Milind Upadhyay17098ba2022-04-15 22:18:50 -0700713std::optional<FlatbufferDetachedBuffer<Configuration>> MaybeReadConfig(
James Kuszmaulc0c08da2020-05-10 18:56:07 -0700714 const std::string_view path,
Austin Schuhef38cd22021-07-21 15:24:23 -0700715 const std::vector<std::string_view> &extra_import_paths) {
Austin Schuh89026f52022-02-25 14:24:04 -0800716 // Add the executable directory to the search path. That makes it so that
717 // tools can be run from any directory without hard-coding an absolute path to
718 // the config into all binaries.
719 std::vector<std::string_view> extra_import_paths_with_exe =
720 extra_import_paths;
721 char proc_self_exec_buffer[PATH_MAX + 1];
722 std::memset(proc_self_exec_buffer, 0, sizeof(proc_self_exec_buffer));
723 ssize_t s = readlink("/proc/self/exe", proc_self_exec_buffer, PATH_MAX);
724 if (s > 0) {
725 // If the readlink call fails, the worst thing that happens is that we don't
726 // automatically find the config next to the binary. VLOG to make it easier
727 // to debug.
728 std::string_view proc_self_exec(proc_self_exec_buffer);
729
730 extra_import_paths_with_exe.emplace_back(
731 proc_self_exec.substr(0, proc_self_exec.rfind("/")));
732 } else {
733 VLOG(1) << "Failed to read /proc/self/exe";
734 }
735
Austin Schuhcb108412019-10-13 16:09:54 -0700736 // We only want to read a file once. So track the visited files in a set.
737 absl::btree_set<std::string> visited_paths;
Milind Upadhyay17098ba2022-04-15 22:18:50 -0700738 std::optional<FlatbufferDetachedBuffer<Configuration>> read_config =
739 MaybeReadConfig(path, &visited_paths, extra_import_paths_with_exe);
740
741 if (read_config == std::nullopt) {
742 return read_config;
743 }
Austin Schuh15182322020-10-10 15:25:21 -0700744
745 // If we only read one file, and it had a .bfbs extension, it has to be a
746 // fully formatted config. Do a quick verification and return it.
747 if (visited_paths.size() == 1 && EndsWith(*visited_paths.begin(), ".bfbs")) {
Milind Upadhyay17098ba2022-04-15 22:18:50 -0700748 ValidateConfiguration(*read_config);
Austin Schuh15182322020-10-10 15:25:21 -0700749 return read_config;
750 }
751
Milind Upadhyay17098ba2022-04-15 22:18:50 -0700752 return MergeConfiguration(*read_config);
753}
754
755FlatbufferDetachedBuffer<Configuration> ReadConfig(
756 const std::string_view path,
757 const std::vector<std::string_view> &extra_import_paths) {
758 auto optional_config = MaybeReadConfig(path, extra_import_paths);
759 CHECK(optional_config) << "Could not read config. See above errors";
760 return std::move(*optional_config);
Austin Schuhcb108412019-10-13 16:09:54 -0700761}
762
Austin Schuh8d6cea82020-02-28 12:17:16 -0800763FlatbufferDetachedBuffer<Configuration> MergeWithConfig(
Brian Silverman24f5aa82020-06-23 16:21:28 -0700764 const Configuration *config, const Flatbuffer<Configuration> &addition) {
765 return MergeConfiguration(MergeFlatBuffers(config, &addition.message()));
766}
767
768FlatbufferDetachedBuffer<Configuration> MergeWithConfig(
Austin Schuh8d6cea82020-02-28 12:17:16 -0800769 const Configuration *config, std::string_view json) {
770 FlatbufferDetachedBuffer<Configuration> addition =
771 JsonToFlatbuffer(json, Configuration::MiniReflectTypeTable());
772
Brian Silverman24f5aa82020-06-23 16:21:28 -0700773 return MergeWithConfig(config, addition);
Austin Schuh8d6cea82020-02-28 12:17:16 -0800774}
775
James Kuszmaul3ae42262019-11-08 12:33:41 -0800776const Channel *GetChannel(const Configuration *config, std::string_view name,
777 std::string_view type,
Austin Schuh0de30f32020-12-06 12:44:28 -0800778 std::string_view application_name, const Node *node,
779 bool quiet) {
Brian Silverman9fcf2c72020-12-21 18:30:58 -0800780 if (!config->has_channels()) {
781 return nullptr;
782 }
783
Austin Schuhbca6cf02019-12-22 17:28:34 -0800784 const std::string_view original_name = name;
Austin Schuhf1fff282020-03-28 16:57:32 -0700785 std::string mutable_name;
Austin Schuh4c3b9702020-08-30 11:34:55 -0700786 if (node != nullptr) {
787 VLOG(1) << "Looking up { \"name\": \"" << name << "\", \"type\": \"" << type
788 << "\" } on " << aos::FlatbufferToJson(node);
789 } else {
790 VLOG(1) << "Looking up { \"name\": \"" << name << "\", \"type\": \"" << type
791 << "\" }";
792 }
Austin Schuhcb108412019-10-13 16:09:54 -0700793
794 // First handle application specific maps. Only do this if we have a matching
795 // application name, and it has maps.
Austin Schuhd2e2f6a2021-02-07 20:46:16 -0800796 {
797 const Application *application =
798 GetApplication(config, node, application_name);
799 if (application != nullptr && application->has_maps()) {
800 mutable_name = std::string(name);
801 HandleMaps(application->maps(), &mutable_name, type, node);
802 name = std::string_view(mutable_name);
Austin Schuhcb108412019-10-13 16:09:54 -0700803 }
804 }
805
806 // Now do global maps.
Austin Schuh40485ed2019-10-26 21:51:44 -0700807 if (config->has_maps()) {
Austin Schuhf1fff282020-03-28 16:57:32 -0700808 mutable_name = std::string(name);
809 HandleMaps(config->maps(), &mutable_name, type, node);
810 name = std::string_view(mutable_name);
Austin Schuhcb108412019-10-13 16:09:54 -0700811 }
812
Austin Schuhbca6cf02019-12-22 17:28:34 -0800813 if (original_name != name) {
814 VLOG(1) << "Remapped to { \"name\": \"" << name << "\", \"type\": \""
815 << type << "\" }";
816 }
Alex Perrycb7da4b2019-08-28 19:35:56 -0700817
James Kuszmaul71a81932020-12-15 21:08:01 -0800818 // Then look for the channel (note that this relies on the channels being
819 // sorted in the config).
Austin Schuh40485ed2019-10-26 21:51:44 -0700820 auto channel_iterator =
Austin Schuhf1fff282020-03-28 16:57:32 -0700821 std::lower_bound(config->channels()->cbegin(), config->channels()->cend(),
Austin Schuh40485ed2019-10-26 21:51:44 -0700822 std::make_pair(name, type), CompareChannels);
Austin Schuhcb108412019-10-13 16:09:54 -0700823
824 // Make sure we actually found it, and it matches.
Austin Schuh40485ed2019-10-26 21:51:44 -0700825 if (channel_iterator != config->channels()->cend() &&
826 EqualsChannels(*channel_iterator, std::make_pair(name, type))) {
Austin Schuhbca6cf02019-12-22 17:28:34 -0800827 if (VLOG_IS_ON(2)) {
828 VLOG(2) << "Found: " << FlatbufferToJson(*channel_iterator);
829 } else if (VLOG_IS_ON(1)) {
830 VLOG(1) << "Found: " << CleanedChannelToString(*channel_iterator);
831 }
Austin Schuh40485ed2019-10-26 21:51:44 -0700832 return *channel_iterator;
Austin Schuhcb108412019-10-13 16:09:54 -0700833 } else {
834 VLOG(1) << "No match for { \"name\": \"" << name << "\", \"type\": \""
835 << type << "\" }";
Austin Schuh0de30f32020-12-06 12:44:28 -0800836 if (original_name != name && !quiet) {
Austin Schuh4b42b252020-10-19 11:35:20 -0700837 LOG(WARNING) << "Remapped from {\"name\": \"" << original_name
838 << "\", \"type\": \"" << type << "\"}, to {\"name\": \""
839 << name << "\", \"type\": \"" << type
840 << "\"}, but no channel by that name exists.";
841 }
Austin Schuhcb108412019-10-13 16:09:54 -0700842 return nullptr;
843 }
844}
845
Austin Schuhc9e10ec2020-01-26 16:08:28 -0800846size_t ChannelIndex(const Configuration *configuration,
847 const Channel *channel) {
848 CHECK(configuration->channels() != nullptr) << ": No channels";
849
Brian Silvermana9698c92021-11-10 12:27:04 -0800850 const auto c = std::lower_bound(
851 configuration->channels()->cbegin(), configuration->channels()->cend(),
852 std::make_pair(channel->name()->string_view(),
853 channel->type()->string_view()),
854 CompareChannels);
855 CHECK(c != configuration->channels()->cend())
856 << ": Channel pointer not found in configuration()->channels()";
857 CHECK(*c == channel)
Austin Schuhc9e10ec2020-01-26 16:08:28 -0800858 << ": Channel pointer not found in configuration()->channels()";
859
Brian Silvermana9698c92021-11-10 12:27:04 -0800860 return std::distance(configuration->channels()->cbegin(), c);
Austin Schuhc9e10ec2020-01-26 16:08:28 -0800861}
862
Austin Schuhbca6cf02019-12-22 17:28:34 -0800863std::string CleanedChannelToString(const Channel *channel) {
864 FlatbufferDetachedBuffer<Channel> cleaned_channel = CopyFlatBuffer(channel);
865 cleaned_channel.mutable_message()->clear_schema();
866 return FlatbufferToJson(cleaned_channel);
867}
868
Austin Schuha81454b2020-05-12 19:58:36 -0700869std::string StrippedChannelToString(const Channel *channel) {
870 return absl::StrCat("{ \"name\": \"", channel->name()->string_view(),
871 "\", \"type\": \"", channel->type()->string_view(),
872 "\" }");
873}
874
Alex Perrycb7da4b2019-08-28 19:35:56 -0700875FlatbufferDetachedBuffer<Configuration> MergeConfiguration(
876 const Flatbuffer<Configuration> &config,
Austin Schuh0de30f32020-12-06 12:44:28 -0800877 const std::vector<aos::FlatbufferVector<reflection::Schema>> &schemas) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700878 flatbuffers::FlatBufferBuilder fbb;
Austin Schuhd7b15da2020-02-17 15:06:11 -0800879 fbb.ForceDefaults(true);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700880
Austin Schuh68d98592020-11-01 23:22:57 -0800881 // Cache for holding already inserted schemas.
882 std::map<std::string_view, flatbuffers::Offset<reflection::Schema>>
883 schema_cache;
884
885 CHECK_EQ(Channel::MiniReflectTypeTable()->num_elems, 13u)
886 << ": Merging logic needs to be updated when the number of channel "
887 "fields changes.";
James Kuszmaul3c998592020-07-27 21:04:47 -0700888
Alex Perrycb7da4b2019-08-28 19:35:56 -0700889 flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<Channel>>>
890 channels_offset;
891 if (config.message().has_channels()) {
892 std::vector<flatbuffers::Offset<Channel>> channel_offsets;
893 for (const Channel *c : *config.message().channels()) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700894 // Search for a schema with a matching type.
Austin Schuh0de30f32020-12-06 12:44:28 -0800895 const aos::FlatbufferVector<reflection::Schema> *found_schema = nullptr;
896 for (const aos::FlatbufferVector<reflection::Schema> &schema : schemas) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700897 if (schema.message().root_table() != nullptr) {
898 if (schema.message().root_table()->name()->string_view() ==
899 c->type()->string_view()) {
900 found_schema = &schema;
901 }
902 }
903 }
904
905 CHECK(found_schema != nullptr)
906 << ": Failed to find schema for " << FlatbufferToJson(c);
907
Austin Schuh68d98592020-11-01 23:22:57 -0800908 // Now copy the message manually.
909 auto cached_schema = schema_cache.find(c->type()->string_view());
910 flatbuffers::Offset<reflection::Schema> schema_offset;
911 if (cached_schema != schema_cache.end()) {
912 schema_offset = cached_schema->second;
913 } else {
Austin Schuha4fc60f2020-11-01 23:06:47 -0800914 schema_offset = RecursiveCopyFlatBuffer<reflection::Schema>(
915 &found_schema->message(), &fbb);
Austin Schuh68d98592020-11-01 23:22:57 -0800916 schema_cache.emplace(c->type()->string_view(), schema_offset);
917 }
918
919 flatbuffers::Offset<flatbuffers::String> name_offset =
920 fbb.CreateSharedString(c->name()->str());
921 flatbuffers::Offset<flatbuffers::String> type_offset =
922 fbb.CreateSharedString(c->type()->str());
923 flatbuffers::Offset<flatbuffers::String> source_node_offset =
Austin Schuha4fc60f2020-11-01 23:06:47 -0800924 c->has_source_node() ? fbb.CreateSharedString(c->source_node()->str())
925 : 0;
Austin Schuh68d98592020-11-01 23:22:57 -0800926
927 flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<Connection>>>
928 destination_nodes_offset =
Austin Schuh5c255aa2020-11-05 18:32:46 -0800929 aos::RecursiveCopyVectorTable(c->destination_nodes(), &fbb);
Austin Schuh68d98592020-11-01 23:22:57 -0800930
931 flatbuffers::Offset<
932 flatbuffers::Vector<flatbuffers::Offset<flatbuffers::String>>>
933 logger_nodes_offset =
934 aos::CopyVectorSharedString(c->logger_nodes(), &fbb);
935
936 Channel::Builder channel_builder(fbb);
937 channel_builder.add_name(name_offset);
938 channel_builder.add_type(type_offset);
939 if (c->has_frequency()) {
940 channel_builder.add_frequency(c->frequency());
941 }
942 if (c->has_max_size()) {
943 channel_builder.add_max_size(c->max_size());
944 }
945 if (c->has_num_senders()) {
946 channel_builder.add_num_senders(c->num_senders());
947 }
948 if (c->has_num_watchers()) {
949 channel_builder.add_num_watchers(c->num_watchers());
950 }
Alex Perrycb7da4b2019-08-28 19:35:56 -0700951 channel_builder.add_schema(schema_offset);
Austin Schuh68d98592020-11-01 23:22:57 -0800952 if (!source_node_offset.IsNull()) {
953 channel_builder.add_source_node(source_node_offset);
954 }
955 if (!destination_nodes_offset.IsNull()) {
956 channel_builder.add_destination_nodes(destination_nodes_offset);
957 }
958 if (c->has_logger()) {
959 channel_builder.add_logger(c->logger());
960 }
961 if (!logger_nodes_offset.IsNull()) {
962 channel_builder.add_logger_nodes(logger_nodes_offset);
963 }
964 if (c->has_read_method()) {
965 channel_builder.add_read_method(c->read_method());
966 }
967 if (c->has_num_readers()) {
968 channel_builder.add_num_readers(c->num_readers());
969 }
970 channel_offsets.emplace_back(channel_builder.Finish());
Alex Perrycb7da4b2019-08-28 19:35:56 -0700971 }
972 channels_offset = fbb.CreateVector(channel_offsets);
973 }
974
Austin Schuh68d98592020-11-01 23:22:57 -0800975 flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<Map>>>
Austin Schuh5c255aa2020-11-05 18:32:46 -0800976 maps_offset =
977 aos::RecursiveCopyVectorTable(config.message().maps(), &fbb);
Austin Schuh68d98592020-11-01 23:22:57 -0800978
979 flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<Node>>>
Austin Schuh5c255aa2020-11-05 18:32:46 -0800980 nodes_offset =
981 aos::RecursiveCopyVectorTable(config.message().nodes(), &fbb);
Austin Schuh68d98592020-11-01 23:22:57 -0800982
983 flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<Application>>>
984 applications_offset =
Austin Schuh5c255aa2020-11-05 18:32:46 -0800985 aos::RecursiveCopyVectorTable(config.message().applications(), &fbb);
Austin Schuh217a9782019-12-21 23:02:50 -0800986
987 // Now insert everything else in unmodified.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700988 ConfigurationBuilder configuration_builder(fbb);
989 if (config.message().has_channels()) {
990 configuration_builder.add_channels(channels_offset);
991 }
Austin Schuh68d98592020-11-01 23:22:57 -0800992 if (!maps_offset.IsNull()) {
993 configuration_builder.add_maps(maps_offset);
994 }
995 if (!nodes_offset.IsNull()) {
996 configuration_builder.add_nodes(nodes_offset);
997 }
998 if (!applications_offset.IsNull()) {
999 configuration_builder.add_applications(applications_offset);
1000 }
1001
1002 if (config.message().has_channel_storage_duration()) {
1003 configuration_builder.add_channel_storage_duration(
1004 config.message().channel_storage_duration());
1005 }
1006
1007 CHECK_EQ(Configuration::MiniReflectTypeTable()->num_elems, 6u)
1008 << ": Merging logic needs to be updated when the number of configuration "
1009 "fields changes.";
1010
Alex Perrycb7da4b2019-08-28 19:35:56 -07001011 fbb.Finish(configuration_builder.Finish());
James Kuszmaul3c998592020-07-27 21:04:47 -07001012 aos::FlatbufferDetachedBuffer<aos::Configuration> modified_config(
1013 fbb.Release());
1014
Austin Schuh68d98592020-11-01 23:22:57 -08001015 return modified_config;
Alex Perrycb7da4b2019-08-28 19:35:56 -07001016}
1017
Austin Schuh217a9782019-12-21 23:02:50 -08001018const Node *GetNodeFromHostname(const Configuration *config,
1019 std::string_view hostname) {
1020 for (const Node *node : *config->nodes()) {
Brian Silvermanaa2633f2020-02-17 21:04:14 -08001021 if (node->has_hostname() && node->hostname()->string_view() == hostname) {
Austin Schuh217a9782019-12-21 23:02:50 -08001022 return node;
1023 }
Brian Silvermanaa2633f2020-02-17 21:04:14 -08001024 if (node->has_hostnames()) {
1025 for (const auto &candidate : *node->hostnames()) {
1026 if (candidate->string_view() == hostname) {
1027 return node;
1028 }
1029 }
1030 }
Austin Schuh217a9782019-12-21 23:02:50 -08001031 }
1032 return nullptr;
1033}
Austin Schuhac0771c2020-01-07 18:36:30 -08001034
Austin Schuh217a9782019-12-21 23:02:50 -08001035const Node *GetMyNode(const Configuration *config) {
1036 const std::string hostname = (FLAGS_override_hostname.size() > 0)
1037 ? FLAGS_override_hostname
1038 : network::GetHostname();
1039 const Node *node = GetNodeFromHostname(config, hostname);
1040 if (node != nullptr) return node;
1041
1042 LOG(FATAL) << "Unknown node for host: " << hostname
1043 << ". Consider using --override_hostname if hostname detection "
1044 "is wrong.";
1045 return nullptr;
1046}
1047
Austin Schuhc9e10ec2020-01-26 16:08:28 -08001048const Node *GetNode(const Configuration *config, const Node *node) {
1049 if (!MultiNode(config)) {
1050 CHECK(node == nullptr) << ": Provided a node in a single node world.";
1051 return nullptr;
1052 } else {
1053 CHECK(node != nullptr);
1054 CHECK(node->has_name());
1055 return GetNode(config, node->name()->string_view());
1056 }
1057}
1058
Austin Schuh217a9782019-12-21 23:02:50 -08001059const Node *GetNode(const Configuration *config, std::string_view name) {
Austin Schuhfd960622020-01-01 13:22:55 -08001060 CHECK(config->has_nodes())
1061 << ": Asking for a node from a single node configuration.";
Austin Schuh217a9782019-12-21 23:02:50 -08001062 for (const Node *node : *config->nodes()) {
Austin Schuhfd960622020-01-01 13:22:55 -08001063 CHECK(node->has_name()) << ": Malformed node " << FlatbufferToJson(node);
Austin Schuh217a9782019-12-21 23:02:50 -08001064 if (node->name()->string_view() == name) {
1065 return node;
1066 }
1067 }
1068 return nullptr;
1069}
1070
Austin Schuh0ca1fd32020-12-18 22:53:05 -08001071const Node *GetNode(const Configuration *config, size_t node_index) {
1072 if (!MultiNode(config)) {
1073 CHECK_EQ(node_index, 0u) << ": Invalid node in a single node world.";
1074 return nullptr;
1075 } else {
1076 CHECK_LT(node_index, config->nodes()->size());
1077 return config->nodes()->Get(node_index);
1078 }
1079}
1080
Austin Schuhc9e10ec2020-01-26 16:08:28 -08001081const Node *GetNodeOrDie(const Configuration *config, const Node *node) {
1082 if (!MultiNode(config)) {
1083 CHECK(node == nullptr) << ": Provided a node in a single node world.";
1084 return nullptr;
1085 } else {
1086 const Node *config_node = GetNode(config, node);
1087 if (config_node == nullptr) {
1088 LOG(FATAL) << "Couldn't find node matching " << FlatbufferToJson(node);
1089 }
1090 return config_node;
1091 }
1092}
1093
Austin Schuh8bd96322020-02-13 21:18:22 -08001094namespace {
1095int GetNodeIndexFromConfig(const Configuration *config, const Node *node) {
Austin Schuhc9e10ec2020-01-26 16:08:28 -08001096 int node_index = 0;
1097 for (const Node *iterated_node : *config->nodes()) {
1098 if (iterated_node == node) {
1099 return node_index;
1100 }
1101 ++node_index;
1102 }
Austin Schuh8bd96322020-02-13 21:18:22 -08001103 return -1;
1104}
1105} // namespace
1106
Austin Schuha9df9ad2021-06-16 14:49:39 -07001107aos::FlatbufferDetachedBuffer<aos::Configuration> AddSchema(
1108 std::string_view json,
1109 const std::vector<aos::FlatbufferVector<reflection::Schema>> &schemas) {
1110 FlatbufferDetachedBuffer<Configuration> addition =
1111 JsonToFlatbuffer(json, Configuration::MiniReflectTypeTable());
1112 return MergeConfiguration(addition, schemas);
1113}
1114
Austin Schuh8bd96322020-02-13 21:18:22 -08001115int GetNodeIndex(const Configuration *config, const Node *node) {
1116 if (!MultiNode(config)) {
1117 return 0;
1118 }
1119
1120 {
1121 int node_index = GetNodeIndexFromConfig(config, node);
1122 if (node_index != -1) {
1123 return node_index;
1124 }
1125 }
1126
1127 const Node *result = GetNode(config, node);
1128 CHECK(result != nullptr);
1129
1130 {
Austin Schuh04408fc2020-02-16 21:48:54 -08001131 int node_index = GetNodeIndexFromConfig(config, result);
Austin Schuh8bd96322020-02-13 21:18:22 -08001132 if (node_index != -1) {
1133 return node_index;
1134 }
1135 }
1136
1137 LOG(FATAL) << "Node " << FlatbufferToJson(node)
1138 << " not found in the configuration.";
Austin Schuhc9e10ec2020-01-26 16:08:28 -08001139}
1140
Austin Schuh04408fc2020-02-16 21:48:54 -08001141int GetNodeIndex(const Configuration *config, std::string_view name) {
1142 if (!MultiNode(config)) {
1143 return 0;
1144 }
1145
1146 {
1147 int node_index = 0;
1148 for (const Node *iterated_node : *config->nodes()) {
1149 if (iterated_node->name()->string_view() == name) {
1150 return node_index;
1151 }
1152 ++node_index;
1153 }
1154 }
1155 LOG(FATAL) << "Node " << name << " not found in the configuration.";
1156}
1157
Austin Schuh681a2472020-12-31 23:55:40 -08001158size_t NodesCount(const Configuration *config) {
1159 if (!MultiNode(config)) {
1160 return 1u;
1161 }
1162
1163 return config->nodes()->size();
1164}
1165
Austin Schuhc9e10ec2020-01-26 16:08:28 -08001166std::vector<const Node *> GetNodes(const Configuration *config) {
1167 std::vector<const Node *> nodes;
Austin Schuh8bd96322020-02-13 21:18:22 -08001168 if (MultiNode(config)) {
Austin Schuhc9e10ec2020-01-26 16:08:28 -08001169 for (const Node *node : *config->nodes()) {
1170 nodes.emplace_back(node);
1171 }
1172 } else {
1173 nodes.emplace_back(nullptr);
1174 }
1175 return nodes;
1176}
1177
Austin Schuh65465332020-11-05 17:36:53 -08001178std::vector<const Node *> GetNodesWithTag(const Configuration *config,
1179 std::string_view tag) {
1180 std::vector<const Node *> nodes;
1181 if (!MultiNode(config)) {
1182 nodes.emplace_back(nullptr);
1183 } else {
1184 for (const Node *node : *config->nodes()) {
1185 if (!node->has_tags()) {
1186 continue;
1187 }
1188 bool did_found_tag = false;
1189 for (const flatbuffers::String *found_tag : *node->tags()) {
1190 if (found_tag->string_view() == tag) {
1191 did_found_tag = true;
1192 break;
1193 }
1194 }
1195 if (did_found_tag) {
1196 nodes.emplace_back(node);
1197 }
1198 }
1199 }
1200 return nodes;
1201}
1202
Brian Silverman631b6262021-11-10 12:25:08 -08001203bool NodeHasTag(const Node *node, std::string_view tag) {
1204 if (node == nullptr) {
1205 return true;
1206 }
1207
1208 const auto *const tags = node->tags();
1209 return std::find_if(tags->begin(), tags->end(),
1210 [tag](const flatbuffers::String *candidate) {
1211 return candidate->string_view() == tag;
1212 }) != tags->end();
1213}
1214
Austin Schuhac0771c2020-01-07 18:36:30 -08001215bool MultiNode(const Configuration *config) { return config->has_nodes(); }
1216
Austin Schuh217a9782019-12-21 23:02:50 -08001217bool ChannelIsSendableOnNode(const Channel *channel, const Node *node) {
Austin Schuhca4828c2019-12-28 14:21:35 -08001218 if (node == nullptr) {
1219 return true;
1220 }
Austin Schuh3c5dae52020-10-06 18:55:18 -07001221 CHECK(channel->has_source_node()) << FlatbufferToJson(channel);
1222 CHECK(node->has_name()) << FlatbufferToJson(node);
Austin Schuh196a4452020-03-15 23:12:03 -07001223 return (CHECK_NOTNULL(channel)->source_node()->string_view() ==
1224 node->name()->string_view());
Austin Schuh217a9782019-12-21 23:02:50 -08001225}
1226
1227bool ChannelIsReadableOnNode(const Channel *channel, const Node *node) {
Austin Schuhca4828c2019-12-28 14:21:35 -08001228 if (node == nullptr) {
1229 return true;
1230 }
1231
Austin Schuh217a9782019-12-21 23:02:50 -08001232 if (channel->source_node()->string_view() == node->name()->string_view()) {
1233 return true;
1234 }
1235
1236 if (!channel->has_destination_nodes()) {
1237 return false;
1238 }
1239
Austin Schuh719946b2019-12-28 14:51:01 -08001240 for (const Connection *connection : *channel->destination_nodes()) {
1241 CHECK(connection->has_name());
1242 if (connection->name()->string_view() == node->name()->string_view()) {
Austin Schuh217a9782019-12-21 23:02:50 -08001243 return true;
1244 }
1245 }
1246
1247 return false;
1248}
1249
Austin Schuh719946b2019-12-28 14:51:01 -08001250bool ChannelMessageIsLoggedOnNode(const Channel *channel, const Node *node) {
Austin Schuh48e94502021-06-18 18:35:53 -07001251 if (node == nullptr) {
Austin Schuh2bb80e02021-03-20 21:46:17 -07001252 // Single node world. If there is a local logger, then we want to use
1253 // it.
Austin Schuh48e94502021-06-18 18:35:53 -07001254 if (channel->logger() == LoggerConfig::LOCAL_LOGGER) {
1255 return true;
1256 } else if (channel->logger() == LoggerConfig::NOT_LOGGED) {
1257 return false;
1258 }
1259 LOG(FATAL) << "Unsupported logging configuration in a single node world: "
1260 << CleanedChannelToString(channel);
Austin Schuh2bb80e02021-03-20 21:46:17 -07001261 }
1262 return ChannelMessageIsLoggedOnNode(
1263 channel, CHECK_NOTNULL(node)->name()->string_view());
1264}
1265
1266bool ChannelMessageIsLoggedOnNode(const Channel *channel,
1267 std::string_view node_name) {
Austin Schuhf1fff282020-03-28 16:57:32 -07001268 switch (channel->logger()) {
Austin Schuh719946b2019-12-28 14:51:01 -08001269 case LoggerConfig::LOCAL_LOGGER:
Austin Schuh2bb80e02021-03-20 21:46:17 -07001270 return channel->source_node()->string_view() == node_name;
Austin Schuh719946b2019-12-28 14:51:01 -08001271 case LoggerConfig::LOCAL_AND_REMOTE_LOGGER:
Austin Schuhda40e472020-03-28 15:15:29 -07001272 CHECK(channel->has_logger_nodes());
1273 CHECK_GT(channel->logger_nodes()->size(), 0u);
Austin Schuh719946b2019-12-28 14:51:01 -08001274
Austin Schuh2bb80e02021-03-20 21:46:17 -07001275 if (channel->source_node()->string_view() == node_name) {
Austin Schuh719946b2019-12-28 14:51:01 -08001276 return true;
1277 }
Austin Schuhda40e472020-03-28 15:15:29 -07001278
1279 [[fallthrough]];
1280 case LoggerConfig::REMOTE_LOGGER:
1281 CHECK(channel->has_logger_nodes());
1282 CHECK_GT(channel->logger_nodes()->size(), 0u);
1283 for (const flatbuffers::String *logger_node : *channel->logger_nodes()) {
Austin Schuh2bb80e02021-03-20 21:46:17 -07001284 if (logger_node->string_view() == node_name) {
Austin Schuhda40e472020-03-28 15:15:29 -07001285 return true;
1286 }
Austin Schuh719946b2019-12-28 14:51:01 -08001287 }
1288
1289 return false;
1290 case LoggerConfig::NOT_LOGGED:
1291 return false;
1292 }
1293
1294 LOG(FATAL) << "Unknown logger config " << static_cast<int>(channel->logger());
1295}
1296
Austin Schuh58646e22021-08-23 23:51:46 -07001297size_t ConnectionCount(const Channel *channel) {
1298 if (!channel->has_destination_nodes()) {
1299 return 0;
1300 }
1301 return channel->destination_nodes()->size();
1302}
1303
Austin Schuh719946b2019-12-28 14:51:01 -08001304const Connection *ConnectionToNode(const Channel *channel, const Node *node) {
1305 if (!channel->has_destination_nodes()) {
1306 return nullptr;
1307 }
1308 for (const Connection *connection : *channel->destination_nodes()) {
1309 if (connection->name()->string_view() == node->name()->string_view()) {
1310 return connection;
1311 }
1312 }
1313 return nullptr;
1314}
1315
1316bool ConnectionDeliveryTimeIsLoggedOnNode(const Channel *channel,
1317 const Node *node,
1318 const Node *logger_node) {
Austin Schuh72211ae2021-08-05 14:02:30 -07001319 return ConnectionDeliveryTimeIsLoggedOnNode(ConnectionToNode(channel, node),
1320 logger_node);
Austin Schuh719946b2019-12-28 14:51:01 -08001321}
1322
1323bool ConnectionDeliveryTimeIsLoggedOnNode(const Connection *connection,
1324 const Node *node) {
Austin Schuh72211ae2021-08-05 14:02:30 -07001325 if (connection == nullptr) {
1326 return false;
1327 }
Austin Schuh719946b2019-12-28 14:51:01 -08001328 switch (connection->timestamp_logger()) {
1329 case LoggerConfig::LOCAL_AND_REMOTE_LOGGER:
Austin Schuhda40e472020-03-28 15:15:29 -07001330 CHECK(connection->has_timestamp_logger_nodes());
1331 CHECK_GT(connection->timestamp_logger_nodes()->size(), 0u);
Austin Schuh719946b2019-12-28 14:51:01 -08001332 if (connection->name()->string_view() == node->name()->string_view()) {
1333 return true;
1334 }
1335
Austin Schuhda40e472020-03-28 15:15:29 -07001336 [[fallthrough]];
1337 case LoggerConfig::REMOTE_LOGGER:
1338 CHECK(connection->has_timestamp_logger_nodes());
1339 CHECK_GT(connection->timestamp_logger_nodes()->size(), 0u);
1340 for (const flatbuffers::String *timestamp_logger_node :
1341 *connection->timestamp_logger_nodes()) {
1342 if (timestamp_logger_node->string_view() ==
1343 node->name()->string_view()) {
1344 return true;
1345 }
Austin Schuh719946b2019-12-28 14:51:01 -08001346 }
1347
1348 return false;
1349 case LoggerConfig::LOCAL_LOGGER:
1350 return connection->name()->string_view() == node->name()->string_view();
Austin Schuh719946b2019-12-28 14:51:01 -08001351 case LoggerConfig::NOT_LOGGED:
1352 return false;
1353 }
1354
1355 LOG(FATAL) << "Unknown logger config "
1356 << static_cast<int>(connection->timestamp_logger());
1357}
1358
Austin Schuhe84c3ed2019-12-14 15:29:48 -08001359std::vector<std::string_view> SourceNodeNames(const Configuration *config,
1360 const Node *my_node) {
1361 std::set<std::string_view> result_set;
1362
1363 for (const Channel *channel : *config->channels()) {
1364 if (channel->has_destination_nodes()) {
1365 for (const Connection *connection : *channel->destination_nodes()) {
1366 if (connection->name()->string_view() ==
1367 my_node->name()->string_view()) {
1368 result_set.insert(channel->source_node()->string_view());
1369 }
1370 }
1371 }
1372 }
1373
1374 std::vector<std::string_view> result;
1375 for (const std::string_view source : result_set) {
1376 VLOG(1) << "Found a source node of " << source;
1377 result.emplace_back(source);
1378 }
1379 return result;
1380}
1381
1382std::vector<std::string_view> DestinationNodeNames(const Configuration *config,
1383 const Node *my_node) {
1384 std::vector<std::string_view> result;
1385
1386 for (const Channel *channel : *config->channels()) {
1387 if (channel->has_source_node() && channel->source_node()->string_view() ==
1388 my_node->name()->string_view()) {
1389 if (!channel->has_destination_nodes()) continue;
1390
1391 if (channel->source_node()->string_view() !=
1392 my_node->name()->string_view()) {
1393 continue;
1394 }
1395
1396 for (const Connection *connection : *channel->destination_nodes()) {
1397 if (std::find(result.begin(), result.end(),
1398 connection->name()->string_view()) == result.end()) {
1399 result.emplace_back(connection->name()->string_view());
1400 }
1401 }
1402 }
1403 }
1404
1405 for (const std::string_view destination : result) {
1406 VLOG(1) << "Found a destination node of " << destination;
1407 }
1408 return result;
1409}
1410
Austin Schuh8d7e0bb2020-10-02 17:57:00 -07001411std::vector<const Node *> TimestampNodes(const Configuration *config,
1412 const Node *my_node) {
1413 if (!configuration::MultiNode(config)) {
1414 CHECK(my_node == nullptr);
1415 return std::vector<const Node *>{};
1416 }
1417
1418 std::set<const Node *> timestamp_logger_nodes;
1419 for (const Channel *channel : *config->channels()) {
1420 if (!configuration::ChannelIsSendableOnNode(channel, my_node)) {
1421 continue;
1422 }
1423 if (!channel->has_destination_nodes()) {
1424 continue;
1425 }
1426 for (const Connection *connection : *channel->destination_nodes()) {
1427 const Node *other_node =
1428 configuration::GetNode(config, connection->name()->string_view());
1429
1430 if (configuration::ConnectionDeliveryTimeIsLoggedOnNode(connection,
1431 my_node)) {
1432 VLOG(1) << "Timestamps are logged from "
1433 << FlatbufferToJson(other_node);
1434 timestamp_logger_nodes.insert(other_node);
1435 }
1436 }
1437 }
1438
1439 std::vector<const Node *> result;
1440 for (const Node *node : timestamp_logger_nodes) {
1441 result.emplace_back(node);
1442 }
1443 return result;
1444}
1445
Austin Schuhc41fa3c2021-10-16 14:35:35 -07001446bool ApplicationShouldStart(const Configuration *config, const Node *my_node,
1447 const Application *application) {
1448 if (MultiNode(config)) {
1449 // Ok, we need
1450 CHECK(application->has_nodes());
1451 CHECK(my_node != nullptr);
1452 for (const flatbuffers::String *str : *application->nodes()) {
1453 if (str->string_view() == my_node->name()->string_view()) {
1454 return true;
1455 }
1456 }
1457 return false;
1458 } else {
1459 return true;
1460 }
1461}
1462
Austin Schuhd2e2f6a2021-02-07 20:46:16 -08001463const Application *GetApplication(const Configuration *config,
1464 const Node *my_node,
1465 std::string_view application_name) {
1466 if (config->has_applications()) {
1467 auto application_iterator = std::lower_bound(
1468 config->applications()->cbegin(), config->applications()->cend(),
1469 application_name, CompareApplications);
1470 if (application_iterator != config->applications()->cend() &&
1471 EqualsApplications(*application_iterator, application_name)) {
Austin Schuhc41fa3c2021-10-16 14:35:35 -07001472 if (ApplicationShouldStart(config, my_node, *application_iterator)) {
Austin Schuhd2e2f6a2021-02-07 20:46:16 -08001473 return *application_iterator;
1474 }
1475 }
1476 }
1477 return nullptr;
1478}
1479
Austin Schuhfc7b6a02021-07-12 21:19:07 -07001480std::vector<size_t> SourceNodeIndex(const Configuration *config) {
1481 CHECK(config->has_channels());
1482 std::vector<size_t> result;
1483 result.resize(config->channels()->size(), 0u);
1484 if (MultiNode(config)) {
1485 for (size_t i = 0; i < config->channels()->size(); ++i) {
1486 result[i] = GetNodeIndex(
1487 config, config->channels()->Get(i)->source_node()->string_view());
1488 }
1489 }
1490 return result;
1491}
1492
Brian Silverman66f079a2013-08-26 16:24:30 -07001493} // namespace configuration
1494} // namespace aos