blob: adba7dc45695510f1a6ff3ea464e6a047e407db9 [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>
6#include <stdlib.h>
7#include <string.h>
8#include <sys/types.h>
Brian Silverman66f079a2013-08-26 16:24:30 -07009#include <unistd.h>
Austin Schuhe84c3ed2019-12-14 15:29:48 -080010
11#include <set>
James Kuszmaul3ae42262019-11-08 12:33:41 -080012#include <string_view>
Brian Silverman66f079a2013-08-26 16:24:30 -070013
Austin Schuhcb108412019-10-13 16:09:54 -070014#include "absl/container/btree_set.h"
Austin Schuha81454b2020-05-12 19:58:36 -070015#include "absl/strings/str_cat.h"
Austin Schuhcb108412019-10-13 16:09:54 -070016#include "aos/configuration_generated.h"
17#include "aos/flatbuffer_merge.h"
18#include "aos/json_to_flatbuffer.h"
Austin Schuh217a9782019-12-21 23:02:50 -080019#include "aos/network/team_number.h"
Austin Schuhcb108412019-10-13 16:09:54 -070020#include "aos/unique_malloc_ptr.h"
21#include "aos/util/file.h"
Austin Schuh217a9782019-12-21 23:02:50 -080022#include "gflags/gflags.h"
Austin Schuhcb108412019-10-13 16:09:54 -070023#include "glog/logging.h"
Austin Schuh217a9782019-12-21 23:02:50 -080024
Brian Silverman66f079a2013-08-26 16:24:30 -070025namespace aos {
Austin Schuh15182322020-10-10 15:25:21 -070026namespace {
27bool EndsWith(std::string_view str, std::string_view end) {
28 if (str.size() < end.size()) {
29 return false;
30 }
31 if (str.substr(str.size() - end.size(), end.size()) != end) {
32 return false;
33 }
34 return true;
35}
36
37std::string MaybeReplaceExtension(std::string_view filename,
38 std::string_view extension,
39 std::string_view replacement) {
40 if (!EndsWith(filename, extension)) {
41 return std::string(filename);
42 }
43 filename.remove_suffix(extension.size());
44 return absl::StrCat(filename, replacement);
45}
46
47FlatbufferDetachedBuffer<Configuration> ReadConfigFile(std::string_view path,
48 bool binary) {
49 if (binary) {
50 FlatbufferVector<Configuration> config =
51 FileToFlatbuffer<Configuration>(path);
52 return CopySpanAsDetachedBuffer(config.span());
53 }
54
55 flatbuffers::DetachedBuffer buffer = JsonToFlatbuffer(
56 util::ReadFileToStringOrDie(path), ConfigurationTypeTable());
57
58 CHECK_GT(buffer.size(), 0u) << ": Failed to parse JSON file";
59
60 return FlatbufferDetachedBuffer<Configuration>(std::move(buffer));
61}
62
63} // namespace
Austin Schuhcb108412019-10-13 16:09:54 -070064
Austin Schuh40485ed2019-10-26 21:51:44 -070065// Define the compare and equal operators for Channel and Application so we can
Austin Schuhcb108412019-10-13 16:09:54 -070066// insert them in the btree below.
Austin Schuh40485ed2019-10-26 21:51:44 -070067bool operator<(const FlatbufferDetachedBuffer<Channel> &lhs,
68 const FlatbufferDetachedBuffer<Channel> &rhs) {
Austin Schuhcb108412019-10-13 16:09:54 -070069 int name_compare = lhs.message().name()->string_view().compare(
70 rhs.message().name()->string_view());
71 if (name_compare == 0) {
72 return lhs.message().type()->string_view() <
73 rhs.message().type()->string_view();
74 } else if (name_compare < 0) {
75 return true;
76 } else {
77 return false;
78 }
79}
80
Austin Schuh40485ed2019-10-26 21:51:44 -070081bool operator==(const FlatbufferDetachedBuffer<Channel> &lhs,
82 const FlatbufferDetachedBuffer<Channel> &rhs) {
Austin Schuhcb108412019-10-13 16:09:54 -070083 return lhs.message().name()->string_view() ==
84 rhs.message().name()->string_view() &&
85 lhs.message().type()->string_view() ==
86 rhs.message().type()->string_view();
87}
88
Austin Schuh40485ed2019-10-26 21:51:44 -070089bool operator==(const FlatbufferDetachedBuffer<Application> &lhs,
90 const FlatbufferDetachedBuffer<Application> &rhs) {
Austin Schuhcb108412019-10-13 16:09:54 -070091 return lhs.message().name()->string_view() ==
92 rhs.message().name()->string_view();
93}
94
Austin Schuh40485ed2019-10-26 21:51:44 -070095bool operator<(const FlatbufferDetachedBuffer<Application> &lhs,
96 const FlatbufferDetachedBuffer<Application> &rhs) {
Austin Schuhcb108412019-10-13 16:09:54 -070097 return lhs.message().name()->string_view() <
98 rhs.message().name()->string_view();
99}
100
Austin Schuh217a9782019-12-21 23:02:50 -0800101bool operator==(const FlatbufferDetachedBuffer<Node> &lhs,
102 const FlatbufferDetachedBuffer<Node> &rhs) {
103 return lhs.message().name()->string_view() ==
104 rhs.message().name()->string_view();
105}
106
107bool operator<(const FlatbufferDetachedBuffer<Node> &lhs,
108 const FlatbufferDetachedBuffer<Node> &rhs) {
109 return lhs.message().name()->string_view() <
110 rhs.message().name()->string_view();
111}
112
Brian Silverman66f079a2013-08-26 16:24:30 -0700113namespace configuration {
114namespace {
115
Austin Schuhcb108412019-10-13 16:09:54 -0700116// Extracts the folder part of a path. Returns ./ if there is no path.
Austin Schuhf1fff282020-03-28 16:57:32 -0700117std::string_view ExtractFolder(const std::string_view filename) {
Austin Schuhcb108412019-10-13 16:09:54 -0700118 auto last_slash_pos = filename.find_last_of("/\\");
119
James Kuszmaul3ae42262019-11-08 12:33:41 -0800120 return last_slash_pos == std::string_view::npos
121 ? std::string_view("./")
Austin Schuhcb108412019-10-13 16:09:54 -0700122 : filename.substr(0, last_slash_pos + 1);
123}
124
James Kuszmaulc0c08da2020-05-10 18:56:07 -0700125std::string AbsolutePath(const std::string_view filename) {
126 // Uses an std::string so that we know the input will be null-terminated.
127 const std::string terminated_file(filename);
128 char buffer[PATH_MAX];
129 PCHECK(NULL != realpath(terminated_file.c_str(), buffer));
130 return buffer;
131}
132
Austin Schuh40485ed2019-10-26 21:51:44 -0700133FlatbufferDetachedBuffer<Configuration> ReadConfig(
James Kuszmaulc0c08da2020-05-10 18:56:07 -0700134 const std::string_view path, absl::btree_set<std::string> *visited_paths,
135 const std::vector<std::string_view> &extra_import_paths) {
Austin Schuh15182322020-10-10 15:25:21 -0700136 std::string binary_path = MaybeReplaceExtension(path, ".json", ".bfbs");
137 bool binary_path_exists = util::PathExists(binary_path);
James Kuszmaulc0c08da2020-05-10 18:56:07 -0700138 std::string raw_path(path);
Austin Schuh15182322020-10-10 15:25:21 -0700139 // For each .json file, look and see if we can find a .bfbs file next to it
140 // with the same base name. If we can, assume it is the same and use it
141 // instead. It is much faster to load .bfbs files than .json files.
142 if (!binary_path_exists && !util::PathExists(raw_path)) {
143 const bool path_is_absolute = raw_path.size() > 0 && raw_path[0] == '/';
James Kuszmaulc0c08da2020-05-10 18:56:07 -0700144 if (path_is_absolute) {
145 CHECK(extra_import_paths.empty())
146 << "Can't specify extra import paths if attempting to read a config "
147 "file from an absolute path (path is "
Austin Schuh15182322020-10-10 15:25:21 -0700148 << raw_path << ").";
James Kuszmaulc0c08da2020-05-10 18:56:07 -0700149 }
150
151 bool found_path = false;
152 for (const auto &import_path : extra_import_paths) {
153 raw_path = std::string(import_path) + "/" + std::string(path);
Austin Schuh15182322020-10-10 15:25:21 -0700154 binary_path = MaybeReplaceExtension(raw_path, ".json", ".bfbs");
155 binary_path_exists = util::PathExists(binary_path);
156 if (binary_path_exists) {
157 found_path = true;
158 break;
159 }
James Kuszmaulc0c08da2020-05-10 18:56:07 -0700160 if (util::PathExists(raw_path)) {
161 found_path = true;
162 break;
163 }
164 }
165 CHECK(found_path) << ": Failed to find file " << path << ".";
166 }
Alex Perrycb7da4b2019-08-28 19:35:56 -0700167
Austin Schuh15182322020-10-10 15:25:21 -0700168 FlatbufferDetachedBuffer<Configuration> config = ReadConfigFile(
169 binary_path_exists ? binary_path : raw_path, binary_path_exists);
James Kuszmaulc0c08da2020-05-10 18:56:07 -0700170
Austin Schuhcb108412019-10-13 16:09:54 -0700171 // Depth first. Take the following example:
172 //
173 // config1.json:
174 // {
Austin Schuh40485ed2019-10-26 21:51:44 -0700175 // "channels": [
Austin Schuhcb108412019-10-13 16:09:54 -0700176 // {
177 // "name": "/foo",
178 // "type": ".aos.bar",
179 // "max_size": 5
180 // }
181 // ],
182 // "imports": [
183 // "config2.json",
184 // ]
185 // }
186 //
187 // config2.json:
188 // {
Austin Schuh40485ed2019-10-26 21:51:44 -0700189 // "channels": [
Austin Schuhcb108412019-10-13 16:09:54 -0700190 // {
191 // "name": "/foo",
192 // "type": ".aos.bar",
193 // "max_size": 7
194 // }
195 // ],
196 // }
197 //
198 // We want the main config (config1.json) to be able to override the imported
199 // config. That means that it needs to be merged into the imported configs,
200 // not the other way around.
201
Austin Schuh15182322020-10-10 15:25:21 -0700202 const std::string absolute_path =
203 AbsolutePath(binary_path_exists ? binary_path : raw_path);
204 // Track that we have seen this file before recursing. Track the path we
205 // actually loaded (which should be consistent if imported twice).
James Kuszmaulc0c08da2020-05-10 18:56:07 -0700206 if (!visited_paths->insert(absolute_path).second) {
207 for (const auto &visited_path : *visited_paths) {
208 LOG(INFO) << "Already visited: " << visited_path;
209 }
210 LOG(FATAL)
211 << "Already imported " << path << " (i.e. " << absolute_path
212 << "). See above for the files that have already been processed.";
213 }
Austin Schuhcb108412019-10-13 16:09:54 -0700214
215 if (config.message().has_imports()) {
216 // Capture the imports.
217 const flatbuffers::Vector<flatbuffers::Offset<flatbuffers::String>> *v =
218 config.message().imports();
219
220 // And then wipe them. This gets GCed when we merge later.
221 config.mutable_message()->clear_imports();
222
223 // Start with an empty configuration to merge into.
Austin Schuh40485ed2019-10-26 21:51:44 -0700224 FlatbufferDetachedBuffer<Configuration> merged_config =
225 FlatbufferDetachedBuffer<Configuration>::Empty();
Austin Schuhcb108412019-10-13 16:09:54 -0700226
James Kuszmaulc0c08da2020-05-10 18:56:07 -0700227 const std::string path_folder(ExtractFolder(path));
Austin Schuhcb108412019-10-13 16:09:54 -0700228 for (const flatbuffers::String *str : *v) {
James Kuszmaulc0c08da2020-05-10 18:56:07 -0700229 const std::string included_config =
230 path_folder + "/" + std::string(str->string_view());
Austin Schuhcb108412019-10-13 16:09:54 -0700231
232 // And them merge everything in.
233 merged_config = MergeFlatBuffers(
James Kuszmaulc0c08da2020-05-10 18:56:07 -0700234 merged_config,
235 ReadConfig(included_config, visited_paths, extra_import_paths));
Austin Schuhcb108412019-10-13 16:09:54 -0700236 }
237
238 // Finally, merge this file in.
239 config = MergeFlatBuffers(merged_config, config);
240 }
241 return config;
242}
243
Alex Perrycb7da4b2019-08-28 19:35:56 -0700244// Compares (c < p) a channel, and a name, type tuple.
245bool CompareChannels(const Channel *c,
James Kuszmaul3ae42262019-11-08 12:33:41 -0800246 ::std::pair<std::string_view, std::string_view> p) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700247 int name_compare = c->name()->string_view().compare(p.first);
248 if (name_compare == 0) {
249 return c->type()->string_view() < p.second;
250 } else if (name_compare < 0) {
251 return true;
252 } else {
253 return false;
254 }
255};
256
257// Compares for equality (c == p) a channel, and a name, type tuple.
258bool EqualsChannels(const Channel *c,
Austin Schuhf1fff282020-03-28 16:57:32 -0700259 ::std::pair<std::string_view, std::string_view> p) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700260 return c->name()->string_view() == p.first &&
261 c->type()->string_view() == p.second;
262}
263
264// Compares (c < p) an application, and a name;
James Kuszmaul3ae42262019-11-08 12:33:41 -0800265bool CompareApplications(const Application *a, std::string_view name) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700266 return a->name()->string_view() < name;
267};
268
269// Compares for equality (c == p) an application, and a name;
James Kuszmaul3ae42262019-11-08 12:33:41 -0800270bool EqualsApplications(const Application *a, std::string_view name) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700271 return a->name()->string_view() == name;
272}
273
274// Maps name for the provided maps. Modifies name.
275void HandleMaps(const flatbuffers::Vector<flatbuffers::Offset<aos::Map>> *maps,
Austin Schuhf1fff282020-03-28 16:57:32 -0700276 std::string *name, std::string_view type, const Node *node) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700277 // For the same reason we merge configs in reverse order, we want to process
278 // maps in reverse order. That lets the outer config overwrite channels from
279 // the inner configs.
280 for (auto i = maps->rbegin(); i != maps->rend(); ++i) {
Austin Schuhbca6cf02019-12-22 17:28:34 -0800281 if (!i->has_match() || !i->match()->has_name()) {
282 continue;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700283 }
Austin Schuhbca6cf02019-12-22 17:28:34 -0800284 if (!i->has_rename() || !i->rename()->has_name()) {
285 continue;
286 }
287
288 // Handle normal maps (now that we know that match and rename are filled
289 // out).
Austin Schuhf1fff282020-03-28 16:57:32 -0700290 const std::string_view match_name = i->match()->name()->string_view();
291 if (match_name != *name) {
292 if (match_name.back() == '*' &&
293 std::string_view(*name).substr(
294 0, std::min(name->size(), match_name.size() - 1)) ==
295 match_name.substr(0, match_name.size() - 1)) {
296 CHECK_EQ(match_name.find('*'), match_name.size() - 1);
297 } else {
298 continue;
299 }
Austin Schuhbca6cf02019-12-22 17:28:34 -0800300 }
301
302 // Handle type specific maps.
303 if (i->match()->has_type() && i->match()->type()->string_view() != type) {
304 continue;
305 }
306
Austin Schuhf1fff282020-03-28 16:57:32 -0700307 // Now handle node specific maps.
Austin Schuhbca6cf02019-12-22 17:28:34 -0800308 if (node != nullptr && i->match()->has_source_node() &&
309 i->match()->source_node()->string_view() !=
310 node->name()->string_view()) {
311 continue;
312 }
313
Austin Schuhf1fff282020-03-28 16:57:32 -0700314 std::string new_name(i->rename()->name()->string_view());
315 if (match_name.back() == '*') {
316 new_name += std::string(name->substr(match_name.size() - 1));
317 }
318 VLOG(1) << "Renamed \"" << *name << "\" to \"" << new_name << "\"";
319 *name = std::move(new_name);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700320 }
321}
322
Austin Schuh15182322020-10-10 15:25:21 -0700323void ValidateConfiguration(const Flatbuffer<Configuration> &config) {
324 // No imports should be left.
325 CHECK(!config.message().has_imports());
326
327 // Check that if there is a node list, all the source nodes are filled out and
328 // valid, and all the destination nodes are valid (and not the source). This
329 // is a basic consistency check.
330 if (config.message().has_channels()) {
331 const Channel *last_channel = nullptr;
332 for (const Channel *c : *config.message().channels()) {
333 CHECK(c->has_name());
334 CHECK(c->has_type());
335 if (c->name()->string_view().back() == '/') {
336 LOG(FATAL) << "Channel names can't end with '/'";
337 }
338 if (c->name()->string_view().find("//") != std::string_view::npos) {
339 LOG(FATAL) << ": Invalid channel name " << c->name()->string_view()
340 << ", can't use //.";
341 }
342 for (const char data : c->name()->string_view()) {
343 if (data >= '0' && data <= '9') {
344 continue;
345 }
346 if (data >= 'a' && data <= 'z') {
347 continue;
348 }
349 if (data >= 'A' && data <= 'Z') {
350 continue;
351 }
352 if (data == '-' || data == '_' || data == '/') {
353 continue;
354 }
355 LOG(FATAL) << "Invalid channel name " << c->name()->string_view()
356 << ", can only use [-a-zA-Z0-9_/]";
357 }
358
359 // Make sure everything is sorted while we are here... If this fails,
360 // there will be a bunch of weird errors.
361 if (last_channel != nullptr) {
362 CHECK(CompareChannels(
363 last_channel,
364 std::make_pair(c->name()->string_view(), c->type()->string_view())))
365 << ": Channels not sorted!";
366 }
367 last_channel = c;
368 }
369 }
370
371 if (config.message().has_nodes() && config.message().has_channels()) {
372 for (const Channel *c : *config.message().channels()) {
373 CHECK(c->has_source_node()) << ": Channel " << FlatbufferToJson(c)
374 << " is missing \"source_node\"";
375 CHECK(GetNode(&config.message(), c->source_node()->string_view()) !=
376 nullptr)
377 << ": Channel " << FlatbufferToJson(c)
378 << " has an unknown \"source_node\"";
379
380 if (c->has_destination_nodes()) {
381 for (const Connection *connection : *c->destination_nodes()) {
382 CHECK(connection->has_name());
383 CHECK(GetNode(&config.message(), connection->name()->string_view()) !=
384 nullptr)
385 << ": Channel " << FlatbufferToJson(c)
386 << " has an unknown \"destination_nodes\" "
387 << connection->name()->string_view();
388
389 switch (connection->timestamp_logger()) {
390 case LoggerConfig::LOCAL_LOGGER:
391 case LoggerConfig::NOT_LOGGED:
392 CHECK(!connection->has_timestamp_logger_nodes());
393 break;
394 case LoggerConfig::REMOTE_LOGGER:
395 case LoggerConfig::LOCAL_AND_REMOTE_LOGGER:
396 CHECK(connection->has_timestamp_logger_nodes());
397 CHECK_GT(connection->timestamp_logger_nodes()->size(), 0u);
398 for (const flatbuffers::String *timestamp_logger_node :
399 *connection->timestamp_logger_nodes()) {
400 CHECK(GetNode(&config.message(),
401 timestamp_logger_node->string_view()) != nullptr)
402 << ": Channel " << FlatbufferToJson(c)
403 << " has an unknown \"timestamp_logger_node\""
404 << connection->name()->string_view();
405 }
406 break;
407 }
408
409 CHECK_NE(connection->name()->string_view(),
410 c->source_node()->string_view())
411 << ": Channel " << FlatbufferToJson(c)
412 << " is forwarding data to itself";
413 }
414 }
415 }
416 }
417}
418
Alex Perrycb7da4b2019-08-28 19:35:56 -0700419} // namespace
420
Austin Schuh40485ed2019-10-26 21:51:44 -0700421FlatbufferDetachedBuffer<Configuration> MergeConfiguration(
Austin Schuhcb108412019-10-13 16:09:54 -0700422 const Flatbuffer<Configuration> &config) {
James Kuszmaul3c998592020-07-27 21:04:47 -0700423 // auto_merge_config will contain all the fields of the Configuration that are
424 // to be passed through unmodified to the result of MergeConfiguration().
425 // In the processing below, we mutate auto_merge_config to remove any fields
426 // which we do need to alter (hence why we can't use the input config
427 // directly), and then merge auto_merge_config back in at the end.
428 aos::FlatbufferDetachedBuffer<aos::Configuration> auto_merge_config =
429 aos::CopyFlatBuffer(&config.message());
430
Austin Schuh40485ed2019-10-26 21:51:44 -0700431 // Store all the channels in a sorted set. This lets us track channels we
Austin Schuhcb108412019-10-13 16:09:54 -0700432 // have seen before and merge the updates in.
Austin Schuh40485ed2019-10-26 21:51:44 -0700433 absl::btree_set<FlatbufferDetachedBuffer<Channel>> channels;
Austin Schuhcb108412019-10-13 16:09:54 -0700434
Austin Schuh40485ed2019-10-26 21:51:44 -0700435 if (config.message().has_channels()) {
James Kuszmaul3c998592020-07-27 21:04:47 -0700436 auto_merge_config.mutable_message()->clear_channels();
Austin Schuh40485ed2019-10-26 21:51:44 -0700437 for (const Channel *c : *config.message().channels()) {
Austin Schuhcb108412019-10-13 16:09:54 -0700438 // Ignore malformed entries.
Austin Schuh40485ed2019-10-26 21:51:44 -0700439 if (!c->has_name()) {
Austin Schuhcb108412019-10-13 16:09:54 -0700440 continue;
441 }
Austin Schuh40485ed2019-10-26 21:51:44 -0700442 if (!c->has_type()) {
Austin Schuhcb108412019-10-13 16:09:54 -0700443 continue;
444 }
445
Brian Silverman77162972020-08-12 19:52:40 -0700446 CHECK_EQ(c->read_method() == ReadMethod::PIN, c->num_readers() != 0)
447 << ": num_readers may be set if and only if read_method is PIN,"
448 " if you want 0 readers do not set PIN: "
449 << CleanedChannelToString(c);
450
Austin Schuh40485ed2019-10-26 21:51:44 -0700451 // Attempt to insert the channel.
452 auto result = channels.insert(CopyFlatBuffer(c));
Austin Schuhcb108412019-10-13 16:09:54 -0700453 if (!result.second) {
454 // Already there, so merge the new table into the original.
Austin Schuh40485ed2019-10-26 21:51:44 -0700455 *result.first = MergeFlatBuffers(*result.first, CopyFlatBuffer(c));
Austin Schuhcb108412019-10-13 16:09:54 -0700456 }
457 }
458 }
459
460 // Now repeat this for the application list.
Austin Schuh40485ed2019-10-26 21:51:44 -0700461 absl::btree_set<FlatbufferDetachedBuffer<Application>> applications;
Austin Schuhcb108412019-10-13 16:09:54 -0700462 if (config.message().has_applications()) {
James Kuszmaul3c998592020-07-27 21:04:47 -0700463 auto_merge_config.mutable_message()->clear_applications();
Austin Schuhcb108412019-10-13 16:09:54 -0700464 for (const Application *a : *config.message().applications()) {
465 if (!a->has_name()) {
466 continue;
467 }
468
469 auto result = applications.insert(CopyFlatBuffer(a));
470 if (!result.second) {
471 *result.first = MergeFlatBuffers(*result.first, CopyFlatBuffer(a));
472 }
473 }
474 }
475
Austin Schuh217a9782019-12-21 23:02:50 -0800476 // Now repeat this for the node list.
477 absl::btree_set<FlatbufferDetachedBuffer<Node>> nodes;
478 if (config.message().has_nodes()) {
James Kuszmaul3c998592020-07-27 21:04:47 -0700479 auto_merge_config.mutable_message()->clear_nodes();
Austin Schuh217a9782019-12-21 23:02:50 -0800480 for (const Node *n : *config.message().nodes()) {
481 if (!n->has_name()) {
482 continue;
483 }
484
485 auto result = nodes.insert(CopyFlatBuffer(n));
486 if (!result.second) {
487 *result.first = MergeFlatBuffers(*result.first, CopyFlatBuffer(n));
488 }
489 }
490 }
491
Austin Schuhcb108412019-10-13 16:09:54 -0700492 flatbuffers::FlatBufferBuilder fbb;
Austin Schuhd7b15da2020-02-17 15:06:11 -0800493 fbb.ForceDefaults(true);
Austin Schuhcb108412019-10-13 16:09:54 -0700494
495 // Start by building the vectors. They need to come before the final table.
Austin Schuh40485ed2019-10-26 21:51:44 -0700496 // Channels
497 flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<Channel>>>
498 channels_offset;
Austin Schuhcb108412019-10-13 16:09:54 -0700499 {
Austin Schuh40485ed2019-10-26 21:51:44 -0700500 ::std::vector<flatbuffers::Offset<Channel>> channel_offsets;
501 for (const FlatbufferDetachedBuffer<Channel> &c : channels) {
Austin Schuhf1fff282020-03-28 16:57:32 -0700502 channel_offsets.emplace_back(CopyFlatBuffer<Channel>(&c.message(), &fbb));
Austin Schuhcb108412019-10-13 16:09:54 -0700503 }
Austin Schuh40485ed2019-10-26 21:51:44 -0700504 channels_offset = fbb.CreateVector(channel_offsets);
Austin Schuhcb108412019-10-13 16:09:54 -0700505 }
506
507 // Applications
508 flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<Application>>>
509 applications_offset;
510 {
511 ::std::vector<flatbuffers::Offset<Application>> applications_offsets;
Austin Schuh40485ed2019-10-26 21:51:44 -0700512 for (const FlatbufferDetachedBuffer<Application> &a : applications) {
Austin Schuhcb108412019-10-13 16:09:54 -0700513 applications_offsets.emplace_back(
514 CopyFlatBuffer<Application>(&a.message(), &fbb));
515 }
516 applications_offset = fbb.CreateVector(applications_offsets);
517 }
518
Austin Schuh217a9782019-12-21 23:02:50 -0800519 // Nodes
520 flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<Node>>>
521 nodes_offset;
522 {
523 ::std::vector<flatbuffers::Offset<Node>> node_offsets;
524 for (const FlatbufferDetachedBuffer<Node> &n : nodes) {
525 node_offsets.emplace_back(CopyFlatBuffer<Node>(&n.message(), &fbb));
526 }
527 nodes_offset = fbb.CreateVector(node_offsets);
528 }
529
Austin Schuhcb108412019-10-13 16:09:54 -0700530 // And then build a Configuration with them all.
531 ConfigurationBuilder configuration_builder(fbb);
Austin Schuh40485ed2019-10-26 21:51:44 -0700532 configuration_builder.add_channels(channels_offset);
Austin Schuh217a9782019-12-21 23:02:50 -0800533 if (config.message().has_applications()) {
534 configuration_builder.add_applications(applications_offset);
535 }
536 if (config.message().has_nodes()) {
537 configuration_builder.add_nodes(nodes_offset);
538 }
Austin Schuhcb108412019-10-13 16:09:54 -0700539
540 fbb.Finish(configuration_builder.Finish());
Austin Schuh217a9782019-12-21 23:02:50 -0800541
James Kuszmaul3c998592020-07-27 21:04:47 -0700542 aos::FlatbufferDetachedBuffer<aos::Configuration> modified_config(
543 fbb.Release());
544
Austin Schuh217a9782019-12-21 23:02:50 -0800545 // Now, validate that if there is a node list, every channel has a source
546 // node.
James Kuszmaul3c998592020-07-27 21:04:47 -0700547 FlatbufferDetachedBuffer<Configuration> result =
548 MergeFlatBuffers(modified_config, auto_merge_config);
Austin Schuh217a9782019-12-21 23:02:50 -0800549
Austin Schuh15182322020-10-10 15:25:21 -0700550 ValidateConfiguration(result);
Austin Schuh217a9782019-12-21 23:02:50 -0800551
552 return result;
Austin Schuhcb108412019-10-13 16:09:54 -0700553}
554
Austin Schuh40485ed2019-10-26 21:51:44 -0700555FlatbufferDetachedBuffer<Configuration> ReadConfig(
James Kuszmaulc0c08da2020-05-10 18:56:07 -0700556 const std::string_view path,
557 const std::vector<std::string_view> &import_paths) {
Austin Schuhcb108412019-10-13 16:09:54 -0700558 // We only want to read a file once. So track the visited files in a set.
559 absl::btree_set<std::string> visited_paths;
Austin Schuh15182322020-10-10 15:25:21 -0700560 FlatbufferDetachedBuffer<Configuration> read_config =
561 ReadConfig(path, &visited_paths, import_paths);
562
563 // If we only read one file, and it had a .bfbs extension, it has to be a
564 // fully formatted config. Do a quick verification and return it.
565 if (visited_paths.size() == 1 && EndsWith(*visited_paths.begin(), ".bfbs")) {
566 ValidateConfiguration(read_config);
567 return read_config;
568 }
569
570 return MergeConfiguration(read_config);
Austin Schuhcb108412019-10-13 16:09:54 -0700571}
572
Austin Schuh8d6cea82020-02-28 12:17:16 -0800573FlatbufferDetachedBuffer<Configuration> MergeWithConfig(
Brian Silverman24f5aa82020-06-23 16:21:28 -0700574 const Configuration *config, const Flatbuffer<Configuration> &addition) {
575 return MergeConfiguration(MergeFlatBuffers(config, &addition.message()));
576}
577
578FlatbufferDetachedBuffer<Configuration> MergeWithConfig(
Austin Schuh8d6cea82020-02-28 12:17:16 -0800579 const Configuration *config, std::string_view json) {
580 FlatbufferDetachedBuffer<Configuration> addition =
581 JsonToFlatbuffer(json, Configuration::MiniReflectTypeTable());
582
Brian Silverman24f5aa82020-06-23 16:21:28 -0700583 return MergeWithConfig(config, addition);
Austin Schuh8d6cea82020-02-28 12:17:16 -0800584}
585
James Kuszmaul3ae42262019-11-08 12:33:41 -0800586const Channel *GetChannel(const Configuration *config, std::string_view name,
587 std::string_view type,
Austin Schuhbca6cf02019-12-22 17:28:34 -0800588 std::string_view application_name, const Node *node) {
589 const std::string_view original_name = name;
Austin Schuhf1fff282020-03-28 16:57:32 -0700590 std::string mutable_name;
Austin Schuh4c3b9702020-08-30 11:34:55 -0700591 if (node != nullptr) {
592 VLOG(1) << "Looking up { \"name\": \"" << name << "\", \"type\": \"" << type
593 << "\" } on " << aos::FlatbufferToJson(node);
594 } else {
595 VLOG(1) << "Looking up { \"name\": \"" << name << "\", \"type\": \"" << type
596 << "\" }";
597 }
Austin Schuhcb108412019-10-13 16:09:54 -0700598
599 // First handle application specific maps. Only do this if we have a matching
600 // application name, and it has maps.
Austin Schuh40485ed2019-10-26 21:51:44 -0700601 if (config->has_applications()) {
602 auto application_iterator = std::lower_bound(
603 config->applications()->cbegin(), config->applications()->cend(),
604 application_name, CompareApplications);
605 if (application_iterator != config->applications()->cend() &&
Austin Schuhcb108412019-10-13 16:09:54 -0700606 EqualsApplications(*application_iterator, application_name)) {
607 if (application_iterator->has_maps()) {
Austin Schuhf1fff282020-03-28 16:57:32 -0700608 mutable_name = std::string(name);
609 HandleMaps(application_iterator->maps(), &mutable_name, type, node);
610 name = std::string_view(mutable_name);
Austin Schuhcb108412019-10-13 16:09:54 -0700611 }
612 }
613 }
614
615 // Now do global maps.
Austin Schuh40485ed2019-10-26 21:51:44 -0700616 if (config->has_maps()) {
Austin Schuhf1fff282020-03-28 16:57:32 -0700617 mutable_name = std::string(name);
618 HandleMaps(config->maps(), &mutable_name, type, node);
619 name = std::string_view(mutable_name);
Austin Schuhcb108412019-10-13 16:09:54 -0700620 }
621
Austin Schuhbca6cf02019-12-22 17:28:34 -0800622 if (original_name != name) {
623 VLOG(1) << "Remapped to { \"name\": \"" << name << "\", \"type\": \""
624 << type << "\" }";
625 }
Alex Perrycb7da4b2019-08-28 19:35:56 -0700626
Austin Schuh40485ed2019-10-26 21:51:44 -0700627 // Then look for the channel.
628 auto channel_iterator =
Austin Schuhf1fff282020-03-28 16:57:32 -0700629 std::lower_bound(config->channels()->cbegin(), config->channels()->cend(),
Austin Schuh40485ed2019-10-26 21:51:44 -0700630 std::make_pair(name, type), CompareChannels);
Austin Schuhcb108412019-10-13 16:09:54 -0700631
632 // Make sure we actually found it, and it matches.
Austin Schuh40485ed2019-10-26 21:51:44 -0700633 if (channel_iterator != config->channels()->cend() &&
634 EqualsChannels(*channel_iterator, std::make_pair(name, type))) {
Austin Schuhbca6cf02019-12-22 17:28:34 -0800635 if (VLOG_IS_ON(2)) {
636 VLOG(2) << "Found: " << FlatbufferToJson(*channel_iterator);
637 } else if (VLOG_IS_ON(1)) {
638 VLOG(1) << "Found: " << CleanedChannelToString(*channel_iterator);
639 }
Austin Schuh40485ed2019-10-26 21:51:44 -0700640 return *channel_iterator;
Austin Schuhcb108412019-10-13 16:09:54 -0700641 } else {
642 VLOG(1) << "No match for { \"name\": \"" << name << "\", \"type\": \""
643 << type << "\" }";
644 return nullptr;
645 }
646}
647
Austin Schuhc9e10ec2020-01-26 16:08:28 -0800648size_t ChannelIndex(const Configuration *configuration,
649 const Channel *channel) {
650 CHECK(configuration->channels() != nullptr) << ": No channels";
651
652 auto c = std::find(configuration->channels()->begin(),
653 configuration->channels()->end(), channel);
654 CHECK(c != configuration->channels()->end())
655 << ": Channel pointer not found in configuration()->channels()";
656
657 return std::distance(configuration->channels()->begin(), c);
658}
659
Austin Schuhbca6cf02019-12-22 17:28:34 -0800660std::string CleanedChannelToString(const Channel *channel) {
661 FlatbufferDetachedBuffer<Channel> cleaned_channel = CopyFlatBuffer(channel);
662 cleaned_channel.mutable_message()->clear_schema();
663 return FlatbufferToJson(cleaned_channel);
664}
665
Austin Schuha81454b2020-05-12 19:58:36 -0700666std::string StrippedChannelToString(const Channel *channel) {
667 return absl::StrCat("{ \"name\": \"", channel->name()->string_view(),
668 "\", \"type\": \"", channel->type()->string_view(),
669 "\" }");
670}
671
Alex Perrycb7da4b2019-08-28 19:35:56 -0700672FlatbufferDetachedBuffer<Configuration> MergeConfiguration(
673 const Flatbuffer<Configuration> &config,
674 const std::vector<aos::FlatbufferString<reflection::Schema>> &schemas) {
675 flatbuffers::FlatBufferBuilder fbb;
Austin Schuhd7b15da2020-02-17 15:06:11 -0800676 fbb.ForceDefaults(true);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700677
James Kuszmaul3c998592020-07-27 21:04:47 -0700678 // auto_merge_config will contain all the fields of the Configuration that are
679 // to be passed through unmodified to the result of MergeConfiguration().
680 // In the processing below, we mutate auto_merge_config to remove any fields
681 // which we do need to alter (hence why we can't use the input config
682 // directly), and then merge auto_merge_config back in at the end.
683 aos::FlatbufferDetachedBuffer<aos::Configuration> auto_merge_config =
684 aos::CopyFlatBuffer(&config.message());
685
Alex Perrycb7da4b2019-08-28 19:35:56 -0700686 flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<Channel>>>
687 channels_offset;
688 if (config.message().has_channels()) {
James Kuszmaul3c998592020-07-27 21:04:47 -0700689 auto_merge_config.mutable_message()->clear_channels();
Alex Perrycb7da4b2019-08-28 19:35:56 -0700690 std::vector<flatbuffers::Offset<Channel>> channel_offsets;
691 for (const Channel *c : *config.message().channels()) {
692 flatbuffers::FlatBufferBuilder channel_fbb;
Austin Schuhd7b15da2020-02-17 15:06:11 -0800693 channel_fbb.ForceDefaults(true);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700694
695 // Search for a schema with a matching type.
696 const aos::FlatbufferString<reflection::Schema> *found_schema = nullptr;
Austin Schuhf1fff282020-03-28 16:57:32 -0700697 for (const aos::FlatbufferString<reflection::Schema> &schema : schemas) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700698 if (schema.message().root_table() != nullptr) {
699 if (schema.message().root_table()->name()->string_view() ==
700 c->type()->string_view()) {
701 found_schema = &schema;
702 }
703 }
704 }
705
706 CHECK(found_schema != nullptr)
707 << ": Failed to find schema for " << FlatbufferToJson(c);
708
709 // The following is wasteful, but works.
710 //
711 // Copy it into a Channel object by creating an object with only the
712 // schema populated and merge that into the current channel.
713 flatbuffers::Offset<reflection::Schema> schema_offset =
714 CopyFlatBuffer<reflection::Schema>(&found_schema->message(),
715 &channel_fbb);
716 Channel::Builder channel_builder(channel_fbb);
717 channel_builder.add_schema(schema_offset);
718 channel_fbb.Finish(channel_builder.Finish());
719 FlatbufferDetachedBuffer<Channel> channel_schema_flatbuffer(
720 channel_fbb.Release());
721
722 FlatbufferDetachedBuffer<Channel> merged_channel(
723 MergeFlatBuffers(channel_schema_flatbuffer, CopyFlatBuffer(c)));
724
725 channel_offsets.emplace_back(
726 CopyFlatBuffer<Channel>(&merged_channel.message(), &fbb));
727 }
728 channels_offset = fbb.CreateVector(channel_offsets);
729 }
730
Austin Schuh217a9782019-12-21 23:02:50 -0800731
732 // Now insert everything else in unmodified.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700733 ConfigurationBuilder configuration_builder(fbb);
734 if (config.message().has_channels()) {
735 configuration_builder.add_channels(channels_offset);
736 }
Alex Perrycb7da4b2019-08-28 19:35:56 -0700737 fbb.Finish(configuration_builder.Finish());
James Kuszmaul3c998592020-07-27 21:04:47 -0700738 aos::FlatbufferDetachedBuffer<aos::Configuration> modified_config(
739 fbb.Release());
740
741 return MergeFlatBuffers(modified_config, auto_merge_config);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700742}
743
Austin Schuh217a9782019-12-21 23:02:50 -0800744const Node *GetNodeFromHostname(const Configuration *config,
745 std::string_view hostname) {
746 for (const Node *node : *config->nodes()) {
Brian Silvermanaa2633f2020-02-17 21:04:14 -0800747 if (node->has_hostname() && node->hostname()->string_view() == hostname) {
Austin Schuh217a9782019-12-21 23:02:50 -0800748 return node;
749 }
Brian Silvermanaa2633f2020-02-17 21:04:14 -0800750 if (node->has_hostnames()) {
751 for (const auto &candidate : *node->hostnames()) {
752 if (candidate->string_view() == hostname) {
753 return node;
754 }
755 }
756 }
Austin Schuh217a9782019-12-21 23:02:50 -0800757 }
758 return nullptr;
759}
Austin Schuhac0771c2020-01-07 18:36:30 -0800760
Austin Schuh217a9782019-12-21 23:02:50 -0800761const Node *GetMyNode(const Configuration *config) {
762 const std::string hostname = (FLAGS_override_hostname.size() > 0)
763 ? FLAGS_override_hostname
764 : network::GetHostname();
765 const Node *node = GetNodeFromHostname(config, hostname);
766 if (node != nullptr) return node;
767
768 LOG(FATAL) << "Unknown node for host: " << hostname
769 << ". Consider using --override_hostname if hostname detection "
770 "is wrong.";
771 return nullptr;
772}
773
Austin Schuhc9e10ec2020-01-26 16:08:28 -0800774const Node *GetNode(const Configuration *config, const Node *node) {
775 if (!MultiNode(config)) {
776 CHECK(node == nullptr) << ": Provided a node in a single node world.";
777 return nullptr;
778 } else {
779 CHECK(node != nullptr);
780 CHECK(node->has_name());
781 return GetNode(config, node->name()->string_view());
782 }
783}
784
Austin Schuh217a9782019-12-21 23:02:50 -0800785const Node *GetNode(const Configuration *config, std::string_view name) {
Austin Schuhfd960622020-01-01 13:22:55 -0800786 CHECK(config->has_nodes())
787 << ": Asking for a node from a single node configuration.";
Austin Schuh217a9782019-12-21 23:02:50 -0800788 for (const Node *node : *config->nodes()) {
Austin Schuhfd960622020-01-01 13:22:55 -0800789 CHECK(node->has_name()) << ": Malformed node " << FlatbufferToJson(node);
Austin Schuh217a9782019-12-21 23:02:50 -0800790 if (node->name()->string_view() == name) {
791 return node;
792 }
793 }
794 return nullptr;
795}
796
Austin Schuhc9e10ec2020-01-26 16:08:28 -0800797const Node *GetNodeOrDie(const Configuration *config, const Node *node) {
798 if (!MultiNode(config)) {
799 CHECK(node == nullptr) << ": Provided a node in a single node world.";
800 return nullptr;
801 } else {
802 const Node *config_node = GetNode(config, node);
803 if (config_node == nullptr) {
804 LOG(FATAL) << "Couldn't find node matching " << FlatbufferToJson(node);
805 }
806 return config_node;
807 }
808}
809
Austin Schuh8bd96322020-02-13 21:18:22 -0800810namespace {
811int GetNodeIndexFromConfig(const Configuration *config, const Node *node) {
Austin Schuhc9e10ec2020-01-26 16:08:28 -0800812 int node_index = 0;
813 for (const Node *iterated_node : *config->nodes()) {
814 if (iterated_node == node) {
815 return node_index;
816 }
817 ++node_index;
818 }
Austin Schuh8bd96322020-02-13 21:18:22 -0800819 return -1;
820}
821} // namespace
822
823int GetNodeIndex(const Configuration *config, const Node *node) {
824 if (!MultiNode(config)) {
825 return 0;
826 }
827
828 {
829 int node_index = GetNodeIndexFromConfig(config, node);
830 if (node_index != -1) {
831 return node_index;
832 }
833 }
834
835 const Node *result = GetNode(config, node);
836 CHECK(result != nullptr);
837
838 {
Austin Schuh04408fc2020-02-16 21:48:54 -0800839 int node_index = GetNodeIndexFromConfig(config, result);
Austin Schuh8bd96322020-02-13 21:18:22 -0800840 if (node_index != -1) {
841 return node_index;
842 }
843 }
844
845 LOG(FATAL) << "Node " << FlatbufferToJson(node)
846 << " not found in the configuration.";
Austin Schuhc9e10ec2020-01-26 16:08:28 -0800847}
848
Austin Schuh04408fc2020-02-16 21:48:54 -0800849int GetNodeIndex(const Configuration *config, std::string_view name) {
850 if (!MultiNode(config)) {
851 return 0;
852 }
853
854 {
855 int node_index = 0;
856 for (const Node *iterated_node : *config->nodes()) {
857 if (iterated_node->name()->string_view() == name) {
858 return node_index;
859 }
860 ++node_index;
861 }
862 }
863 LOG(FATAL) << "Node " << name << " not found in the configuration.";
864}
865
Austin Schuhc9e10ec2020-01-26 16:08:28 -0800866std::vector<const Node *> GetNodes(const Configuration *config) {
867 std::vector<const Node *> nodes;
Austin Schuh8bd96322020-02-13 21:18:22 -0800868 if (MultiNode(config)) {
Austin Schuhc9e10ec2020-01-26 16:08:28 -0800869 for (const Node *node : *config->nodes()) {
870 nodes.emplace_back(node);
871 }
872 } else {
873 nodes.emplace_back(nullptr);
874 }
875 return nodes;
876}
877
Austin Schuhac0771c2020-01-07 18:36:30 -0800878bool MultiNode(const Configuration *config) { return config->has_nodes(); }
879
Austin Schuh217a9782019-12-21 23:02:50 -0800880bool ChannelIsSendableOnNode(const Channel *channel, const Node *node) {
Austin Schuhca4828c2019-12-28 14:21:35 -0800881 if (node == nullptr) {
882 return true;
883 }
Austin Schuh3c5dae52020-10-06 18:55:18 -0700884 CHECK(channel->has_source_node()) << FlatbufferToJson(channel);
885 CHECK(node->has_name()) << FlatbufferToJson(node);
Austin Schuh196a4452020-03-15 23:12:03 -0700886 return (CHECK_NOTNULL(channel)->source_node()->string_view() ==
887 node->name()->string_view());
Austin Schuh217a9782019-12-21 23:02:50 -0800888}
889
890bool ChannelIsReadableOnNode(const Channel *channel, const Node *node) {
Austin Schuhca4828c2019-12-28 14:21:35 -0800891 if (node == nullptr) {
892 return true;
893 }
894
Austin Schuh217a9782019-12-21 23:02:50 -0800895 if (channel->source_node()->string_view() == node->name()->string_view()) {
896 return true;
897 }
898
899 if (!channel->has_destination_nodes()) {
900 return false;
901 }
902
Austin Schuh719946b2019-12-28 14:51:01 -0800903 for (const Connection *connection : *channel->destination_nodes()) {
904 CHECK(connection->has_name());
905 if (connection->name()->string_view() == node->name()->string_view()) {
Austin Schuh217a9782019-12-21 23:02:50 -0800906 return true;
907 }
908 }
909
910 return false;
911}
912
Austin Schuh719946b2019-12-28 14:51:01 -0800913bool ChannelMessageIsLoggedOnNode(const Channel *channel, const Node *node) {
Austin Schuhf1fff282020-03-28 16:57:32 -0700914 switch (channel->logger()) {
Austin Schuh719946b2019-12-28 14:51:01 -0800915 case LoggerConfig::LOCAL_LOGGER:
916 if (node == nullptr) {
917 // Single node world. If there is a local logger, then we want to use
918 // it.
919 return true;
920 }
921 return channel->source_node()->string_view() ==
922 node->name()->string_view();
Austin Schuh719946b2019-12-28 14:51:01 -0800923 case LoggerConfig::LOCAL_AND_REMOTE_LOGGER:
Austin Schuhda40e472020-03-28 15:15:29 -0700924 CHECK(channel->has_logger_nodes());
925 CHECK_GT(channel->logger_nodes()->size(), 0u);
Austin Schuh719946b2019-12-28 14:51:01 -0800926
927 if (channel->source_node()->string_view() ==
928 CHECK_NOTNULL(node)->name()->string_view()) {
929 return true;
930 }
Austin Schuhda40e472020-03-28 15:15:29 -0700931
932 [[fallthrough]];
933 case LoggerConfig::REMOTE_LOGGER:
934 CHECK(channel->has_logger_nodes());
935 CHECK_GT(channel->logger_nodes()->size(), 0u);
936 for (const flatbuffers::String *logger_node : *channel->logger_nodes()) {
937 if (logger_node->string_view() ==
938 CHECK_NOTNULL(node)->name()->string_view()) {
939 return true;
940 }
Austin Schuh719946b2019-12-28 14:51:01 -0800941 }
942
943 return false;
944 case LoggerConfig::NOT_LOGGED:
945 return false;
946 }
947
948 LOG(FATAL) << "Unknown logger config " << static_cast<int>(channel->logger());
949}
950
951const Connection *ConnectionToNode(const Channel *channel, const Node *node) {
952 if (!channel->has_destination_nodes()) {
953 return nullptr;
954 }
955 for (const Connection *connection : *channel->destination_nodes()) {
956 if (connection->name()->string_view() == node->name()->string_view()) {
957 return connection;
958 }
959 }
960 return nullptr;
961}
962
963bool ConnectionDeliveryTimeIsLoggedOnNode(const Channel *channel,
964 const Node *node,
965 const Node *logger_node) {
966 const Connection *connection = ConnectionToNode(channel, node);
967 if (connection == nullptr) {
968 return false;
969 }
970 return ConnectionDeliveryTimeIsLoggedOnNode(connection, logger_node);
971}
972
973bool ConnectionDeliveryTimeIsLoggedOnNode(const Connection *connection,
974 const Node *node) {
975 switch (connection->timestamp_logger()) {
976 case LoggerConfig::LOCAL_AND_REMOTE_LOGGER:
Austin Schuhda40e472020-03-28 15:15:29 -0700977 CHECK(connection->has_timestamp_logger_nodes());
978 CHECK_GT(connection->timestamp_logger_nodes()->size(), 0u);
Austin Schuh719946b2019-12-28 14:51:01 -0800979 if (connection->name()->string_view() == node->name()->string_view()) {
980 return true;
981 }
982
Austin Schuhda40e472020-03-28 15:15:29 -0700983 [[fallthrough]];
984 case LoggerConfig::REMOTE_LOGGER:
985 CHECK(connection->has_timestamp_logger_nodes());
986 CHECK_GT(connection->timestamp_logger_nodes()->size(), 0u);
987 for (const flatbuffers::String *timestamp_logger_node :
988 *connection->timestamp_logger_nodes()) {
989 if (timestamp_logger_node->string_view() ==
990 node->name()->string_view()) {
991 return true;
992 }
Austin Schuh719946b2019-12-28 14:51:01 -0800993 }
994
995 return false;
996 case LoggerConfig::LOCAL_LOGGER:
997 return connection->name()->string_view() == node->name()->string_view();
Austin Schuh719946b2019-12-28 14:51:01 -0800998 case LoggerConfig::NOT_LOGGED:
999 return false;
1000 }
1001
1002 LOG(FATAL) << "Unknown logger config "
1003 << static_cast<int>(connection->timestamp_logger());
1004}
1005
Austin Schuhe84c3ed2019-12-14 15:29:48 -08001006std::vector<std::string_view> SourceNodeNames(const Configuration *config,
1007 const Node *my_node) {
1008 std::set<std::string_view> result_set;
1009
1010 for (const Channel *channel : *config->channels()) {
1011 if (channel->has_destination_nodes()) {
1012 for (const Connection *connection : *channel->destination_nodes()) {
1013 if (connection->name()->string_view() ==
1014 my_node->name()->string_view()) {
1015 result_set.insert(channel->source_node()->string_view());
1016 }
1017 }
1018 }
1019 }
1020
1021 std::vector<std::string_view> result;
1022 for (const std::string_view source : result_set) {
1023 VLOG(1) << "Found a source node of " << source;
1024 result.emplace_back(source);
1025 }
1026 return result;
1027}
1028
1029std::vector<std::string_view> DestinationNodeNames(const Configuration *config,
1030 const Node *my_node) {
1031 std::vector<std::string_view> result;
1032
1033 for (const Channel *channel : *config->channels()) {
1034 if (channel->has_source_node() && channel->source_node()->string_view() ==
1035 my_node->name()->string_view()) {
1036 if (!channel->has_destination_nodes()) continue;
1037
1038 if (channel->source_node()->string_view() !=
1039 my_node->name()->string_view()) {
1040 continue;
1041 }
1042
1043 for (const Connection *connection : *channel->destination_nodes()) {
1044 if (std::find(result.begin(), result.end(),
1045 connection->name()->string_view()) == result.end()) {
1046 result.emplace_back(connection->name()->string_view());
1047 }
1048 }
1049 }
1050 }
1051
1052 for (const std::string_view destination : result) {
1053 VLOG(1) << "Found a destination node of " << destination;
1054 }
1055 return result;
1056}
1057
Austin Schuh8d7e0bb2020-10-02 17:57:00 -07001058std::vector<const Node *> TimestampNodes(const Configuration *config,
1059 const Node *my_node) {
1060 if (!configuration::MultiNode(config)) {
1061 CHECK(my_node == nullptr);
1062 return std::vector<const Node *>{};
1063 }
1064
1065 std::set<const Node *> timestamp_logger_nodes;
1066 for (const Channel *channel : *config->channels()) {
1067 if (!configuration::ChannelIsSendableOnNode(channel, my_node)) {
1068 continue;
1069 }
1070 if (!channel->has_destination_nodes()) {
1071 continue;
1072 }
1073 for (const Connection *connection : *channel->destination_nodes()) {
1074 const Node *other_node =
1075 configuration::GetNode(config, connection->name()->string_view());
1076
1077 if (configuration::ConnectionDeliveryTimeIsLoggedOnNode(connection,
1078 my_node)) {
1079 VLOG(1) << "Timestamps are logged from "
1080 << FlatbufferToJson(other_node);
1081 timestamp_logger_nodes.insert(other_node);
1082 }
1083 }
1084 }
1085
1086 std::vector<const Node *> result;
1087 for (const Node *node : timestamp_logger_nodes) {
1088 result.emplace_back(node);
1089 }
1090 return result;
1091}
1092
Brian Silverman66f079a2013-08-26 16:24:30 -07001093} // namespace configuration
1094} // namespace aos