blob: 4c114050ec39660854f301fc6a1da769196280cd [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 << "\" }";
Austin Schuh4b42b252020-10-19 11:35:20 -0700644 if (original_name != name) {
645 LOG(WARNING) << "Remapped from {\"name\": \"" << original_name
646 << "\", \"type\": \"" << type << "\"}, to {\"name\": \""
647 << name << "\", \"type\": \"" << type
648 << "\"}, but no channel by that name exists.";
649 }
Austin Schuhcb108412019-10-13 16:09:54 -0700650 return nullptr;
651 }
652}
653
Austin Schuhc9e10ec2020-01-26 16:08:28 -0800654size_t ChannelIndex(const Configuration *configuration,
655 const Channel *channel) {
656 CHECK(configuration->channels() != nullptr) << ": No channels";
657
658 auto c = std::find(configuration->channels()->begin(),
659 configuration->channels()->end(), channel);
660 CHECK(c != configuration->channels()->end())
661 << ": Channel pointer not found in configuration()->channels()";
662
663 return std::distance(configuration->channels()->begin(), c);
664}
665
Austin Schuhbca6cf02019-12-22 17:28:34 -0800666std::string CleanedChannelToString(const Channel *channel) {
667 FlatbufferDetachedBuffer<Channel> cleaned_channel = CopyFlatBuffer(channel);
668 cleaned_channel.mutable_message()->clear_schema();
669 return FlatbufferToJson(cleaned_channel);
670}
671
Austin Schuha81454b2020-05-12 19:58:36 -0700672std::string StrippedChannelToString(const Channel *channel) {
673 return absl::StrCat("{ \"name\": \"", channel->name()->string_view(),
674 "\", \"type\": \"", channel->type()->string_view(),
675 "\" }");
676}
677
Alex Perrycb7da4b2019-08-28 19:35:56 -0700678FlatbufferDetachedBuffer<Configuration> MergeConfiguration(
679 const Flatbuffer<Configuration> &config,
680 const std::vector<aos::FlatbufferString<reflection::Schema>> &schemas) {
681 flatbuffers::FlatBufferBuilder fbb;
Austin Schuhd7b15da2020-02-17 15:06:11 -0800682 fbb.ForceDefaults(true);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700683
James Kuszmaul3c998592020-07-27 21:04:47 -0700684 // auto_merge_config will contain all the fields of the Configuration that are
685 // to be passed through unmodified to the result of MergeConfiguration().
686 // In the processing below, we mutate auto_merge_config to remove any fields
687 // which we do need to alter (hence why we can't use the input config
688 // directly), and then merge auto_merge_config back in at the end.
689 aos::FlatbufferDetachedBuffer<aos::Configuration> auto_merge_config =
690 aos::CopyFlatBuffer(&config.message());
691
Alex Perrycb7da4b2019-08-28 19:35:56 -0700692 flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<Channel>>>
693 channels_offset;
694 if (config.message().has_channels()) {
James Kuszmaul3c998592020-07-27 21:04:47 -0700695 auto_merge_config.mutable_message()->clear_channels();
Alex Perrycb7da4b2019-08-28 19:35:56 -0700696 std::vector<flatbuffers::Offset<Channel>> channel_offsets;
697 for (const Channel *c : *config.message().channels()) {
698 flatbuffers::FlatBufferBuilder channel_fbb;
Austin Schuhd7b15da2020-02-17 15:06:11 -0800699 channel_fbb.ForceDefaults(true);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700700
701 // Search for a schema with a matching type.
702 const aos::FlatbufferString<reflection::Schema> *found_schema = nullptr;
Austin Schuhf1fff282020-03-28 16:57:32 -0700703 for (const aos::FlatbufferString<reflection::Schema> &schema : schemas) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700704 if (schema.message().root_table() != nullptr) {
705 if (schema.message().root_table()->name()->string_view() ==
706 c->type()->string_view()) {
707 found_schema = &schema;
708 }
709 }
710 }
711
712 CHECK(found_schema != nullptr)
713 << ": Failed to find schema for " << FlatbufferToJson(c);
714
715 // The following is wasteful, but works.
716 //
717 // Copy it into a Channel object by creating an object with only the
718 // schema populated and merge that into the current channel.
719 flatbuffers::Offset<reflection::Schema> schema_offset =
720 CopyFlatBuffer<reflection::Schema>(&found_schema->message(),
721 &channel_fbb);
722 Channel::Builder channel_builder(channel_fbb);
723 channel_builder.add_schema(schema_offset);
724 channel_fbb.Finish(channel_builder.Finish());
725 FlatbufferDetachedBuffer<Channel> channel_schema_flatbuffer(
726 channel_fbb.Release());
727
728 FlatbufferDetachedBuffer<Channel> merged_channel(
729 MergeFlatBuffers(channel_schema_flatbuffer, CopyFlatBuffer(c)));
730
731 channel_offsets.emplace_back(
732 CopyFlatBuffer<Channel>(&merged_channel.message(), &fbb));
733 }
734 channels_offset = fbb.CreateVector(channel_offsets);
735 }
736
Austin Schuh217a9782019-12-21 23:02:50 -0800737
738 // Now insert everything else in unmodified.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700739 ConfigurationBuilder configuration_builder(fbb);
740 if (config.message().has_channels()) {
741 configuration_builder.add_channels(channels_offset);
742 }
Alex Perrycb7da4b2019-08-28 19:35:56 -0700743 fbb.Finish(configuration_builder.Finish());
James Kuszmaul3c998592020-07-27 21:04:47 -0700744 aos::FlatbufferDetachedBuffer<aos::Configuration> modified_config(
745 fbb.Release());
746
747 return MergeFlatBuffers(modified_config, auto_merge_config);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700748}
749
Austin Schuh217a9782019-12-21 23:02:50 -0800750const Node *GetNodeFromHostname(const Configuration *config,
751 std::string_view hostname) {
752 for (const Node *node : *config->nodes()) {
Brian Silvermanaa2633f2020-02-17 21:04:14 -0800753 if (node->has_hostname() && node->hostname()->string_view() == hostname) {
Austin Schuh217a9782019-12-21 23:02:50 -0800754 return node;
755 }
Brian Silvermanaa2633f2020-02-17 21:04:14 -0800756 if (node->has_hostnames()) {
757 for (const auto &candidate : *node->hostnames()) {
758 if (candidate->string_view() == hostname) {
759 return node;
760 }
761 }
762 }
Austin Schuh217a9782019-12-21 23:02:50 -0800763 }
764 return nullptr;
765}
Austin Schuhac0771c2020-01-07 18:36:30 -0800766
Austin Schuh217a9782019-12-21 23:02:50 -0800767const Node *GetMyNode(const Configuration *config) {
768 const std::string hostname = (FLAGS_override_hostname.size() > 0)
769 ? FLAGS_override_hostname
770 : network::GetHostname();
771 const Node *node = GetNodeFromHostname(config, hostname);
772 if (node != nullptr) return node;
773
774 LOG(FATAL) << "Unknown node for host: " << hostname
775 << ". Consider using --override_hostname if hostname detection "
776 "is wrong.";
777 return nullptr;
778}
779
Austin Schuhc9e10ec2020-01-26 16:08:28 -0800780const Node *GetNode(const Configuration *config, const Node *node) {
781 if (!MultiNode(config)) {
782 CHECK(node == nullptr) << ": Provided a node in a single node world.";
783 return nullptr;
784 } else {
785 CHECK(node != nullptr);
786 CHECK(node->has_name());
787 return GetNode(config, node->name()->string_view());
788 }
789}
790
Austin Schuh217a9782019-12-21 23:02:50 -0800791const Node *GetNode(const Configuration *config, std::string_view name) {
Austin Schuhfd960622020-01-01 13:22:55 -0800792 CHECK(config->has_nodes())
793 << ": Asking for a node from a single node configuration.";
Austin Schuh217a9782019-12-21 23:02:50 -0800794 for (const Node *node : *config->nodes()) {
Austin Schuhfd960622020-01-01 13:22:55 -0800795 CHECK(node->has_name()) << ": Malformed node " << FlatbufferToJson(node);
Austin Schuh217a9782019-12-21 23:02:50 -0800796 if (node->name()->string_view() == name) {
797 return node;
798 }
799 }
800 return nullptr;
801}
802
Austin Schuhc9e10ec2020-01-26 16:08:28 -0800803const Node *GetNodeOrDie(const Configuration *config, const Node *node) {
804 if (!MultiNode(config)) {
805 CHECK(node == nullptr) << ": Provided a node in a single node world.";
806 return nullptr;
807 } else {
808 const Node *config_node = GetNode(config, node);
809 if (config_node == nullptr) {
810 LOG(FATAL) << "Couldn't find node matching " << FlatbufferToJson(node);
811 }
812 return config_node;
813 }
814}
815
Austin Schuh8bd96322020-02-13 21:18:22 -0800816namespace {
817int GetNodeIndexFromConfig(const Configuration *config, const Node *node) {
Austin Schuhc9e10ec2020-01-26 16:08:28 -0800818 int node_index = 0;
819 for (const Node *iterated_node : *config->nodes()) {
820 if (iterated_node == node) {
821 return node_index;
822 }
823 ++node_index;
824 }
Austin Schuh8bd96322020-02-13 21:18:22 -0800825 return -1;
826}
827} // namespace
828
829int GetNodeIndex(const Configuration *config, const Node *node) {
830 if (!MultiNode(config)) {
831 return 0;
832 }
833
834 {
835 int node_index = GetNodeIndexFromConfig(config, node);
836 if (node_index != -1) {
837 return node_index;
838 }
839 }
840
841 const Node *result = GetNode(config, node);
842 CHECK(result != nullptr);
843
844 {
Austin Schuh04408fc2020-02-16 21:48:54 -0800845 int node_index = GetNodeIndexFromConfig(config, result);
Austin Schuh8bd96322020-02-13 21:18:22 -0800846 if (node_index != -1) {
847 return node_index;
848 }
849 }
850
851 LOG(FATAL) << "Node " << FlatbufferToJson(node)
852 << " not found in the configuration.";
Austin Schuhc9e10ec2020-01-26 16:08:28 -0800853}
854
Austin Schuh04408fc2020-02-16 21:48:54 -0800855int GetNodeIndex(const Configuration *config, std::string_view name) {
856 if (!MultiNode(config)) {
857 return 0;
858 }
859
860 {
861 int node_index = 0;
862 for (const Node *iterated_node : *config->nodes()) {
863 if (iterated_node->name()->string_view() == name) {
864 return node_index;
865 }
866 ++node_index;
867 }
868 }
869 LOG(FATAL) << "Node " << name << " not found in the configuration.";
870}
871
Austin Schuhc9e10ec2020-01-26 16:08:28 -0800872std::vector<const Node *> GetNodes(const Configuration *config) {
873 std::vector<const Node *> nodes;
Austin Schuh8bd96322020-02-13 21:18:22 -0800874 if (MultiNode(config)) {
Austin Schuhc9e10ec2020-01-26 16:08:28 -0800875 for (const Node *node : *config->nodes()) {
876 nodes.emplace_back(node);
877 }
878 } else {
879 nodes.emplace_back(nullptr);
880 }
881 return nodes;
882}
883
Austin Schuhac0771c2020-01-07 18:36:30 -0800884bool MultiNode(const Configuration *config) { return config->has_nodes(); }
885
Austin Schuh217a9782019-12-21 23:02:50 -0800886bool ChannelIsSendableOnNode(const Channel *channel, const Node *node) {
Austin Schuhca4828c2019-12-28 14:21:35 -0800887 if (node == nullptr) {
888 return true;
889 }
Austin Schuh3c5dae52020-10-06 18:55:18 -0700890 CHECK(channel->has_source_node()) << FlatbufferToJson(channel);
891 CHECK(node->has_name()) << FlatbufferToJson(node);
Austin Schuh196a4452020-03-15 23:12:03 -0700892 return (CHECK_NOTNULL(channel)->source_node()->string_view() ==
893 node->name()->string_view());
Austin Schuh217a9782019-12-21 23:02:50 -0800894}
895
896bool ChannelIsReadableOnNode(const Channel *channel, const Node *node) {
Austin Schuhca4828c2019-12-28 14:21:35 -0800897 if (node == nullptr) {
898 return true;
899 }
900
Austin Schuh217a9782019-12-21 23:02:50 -0800901 if (channel->source_node()->string_view() == node->name()->string_view()) {
902 return true;
903 }
904
905 if (!channel->has_destination_nodes()) {
906 return false;
907 }
908
Austin Schuh719946b2019-12-28 14:51:01 -0800909 for (const Connection *connection : *channel->destination_nodes()) {
910 CHECK(connection->has_name());
911 if (connection->name()->string_view() == node->name()->string_view()) {
Austin Schuh217a9782019-12-21 23:02:50 -0800912 return true;
913 }
914 }
915
916 return false;
917}
918
Austin Schuh719946b2019-12-28 14:51:01 -0800919bool ChannelMessageIsLoggedOnNode(const Channel *channel, const Node *node) {
Austin Schuhf1fff282020-03-28 16:57:32 -0700920 switch (channel->logger()) {
Austin Schuh719946b2019-12-28 14:51:01 -0800921 case LoggerConfig::LOCAL_LOGGER:
922 if (node == nullptr) {
923 // Single node world. If there is a local logger, then we want to use
924 // it.
925 return true;
926 }
927 return channel->source_node()->string_view() ==
928 node->name()->string_view();
Austin Schuh719946b2019-12-28 14:51:01 -0800929 case LoggerConfig::LOCAL_AND_REMOTE_LOGGER:
Austin Schuhda40e472020-03-28 15:15:29 -0700930 CHECK(channel->has_logger_nodes());
931 CHECK_GT(channel->logger_nodes()->size(), 0u);
Austin Schuh719946b2019-12-28 14:51:01 -0800932
933 if (channel->source_node()->string_view() ==
934 CHECK_NOTNULL(node)->name()->string_view()) {
935 return true;
936 }
Austin Schuhda40e472020-03-28 15:15:29 -0700937
938 [[fallthrough]];
939 case LoggerConfig::REMOTE_LOGGER:
940 CHECK(channel->has_logger_nodes());
941 CHECK_GT(channel->logger_nodes()->size(), 0u);
942 for (const flatbuffers::String *logger_node : *channel->logger_nodes()) {
943 if (logger_node->string_view() ==
944 CHECK_NOTNULL(node)->name()->string_view()) {
945 return true;
946 }
Austin Schuh719946b2019-12-28 14:51:01 -0800947 }
948
949 return false;
950 case LoggerConfig::NOT_LOGGED:
951 return false;
952 }
953
954 LOG(FATAL) << "Unknown logger config " << static_cast<int>(channel->logger());
955}
956
957const Connection *ConnectionToNode(const Channel *channel, const Node *node) {
958 if (!channel->has_destination_nodes()) {
959 return nullptr;
960 }
961 for (const Connection *connection : *channel->destination_nodes()) {
962 if (connection->name()->string_view() == node->name()->string_view()) {
963 return connection;
964 }
965 }
966 return nullptr;
967}
968
969bool ConnectionDeliveryTimeIsLoggedOnNode(const Channel *channel,
970 const Node *node,
971 const Node *logger_node) {
972 const Connection *connection = ConnectionToNode(channel, node);
973 if (connection == nullptr) {
974 return false;
975 }
976 return ConnectionDeliveryTimeIsLoggedOnNode(connection, logger_node);
977}
978
979bool ConnectionDeliveryTimeIsLoggedOnNode(const Connection *connection,
980 const Node *node) {
981 switch (connection->timestamp_logger()) {
982 case LoggerConfig::LOCAL_AND_REMOTE_LOGGER:
Austin Schuhda40e472020-03-28 15:15:29 -0700983 CHECK(connection->has_timestamp_logger_nodes());
984 CHECK_GT(connection->timestamp_logger_nodes()->size(), 0u);
Austin Schuh719946b2019-12-28 14:51:01 -0800985 if (connection->name()->string_view() == node->name()->string_view()) {
986 return true;
987 }
988
Austin Schuhda40e472020-03-28 15:15:29 -0700989 [[fallthrough]];
990 case LoggerConfig::REMOTE_LOGGER:
991 CHECK(connection->has_timestamp_logger_nodes());
992 CHECK_GT(connection->timestamp_logger_nodes()->size(), 0u);
993 for (const flatbuffers::String *timestamp_logger_node :
994 *connection->timestamp_logger_nodes()) {
995 if (timestamp_logger_node->string_view() ==
996 node->name()->string_view()) {
997 return true;
998 }
Austin Schuh719946b2019-12-28 14:51:01 -0800999 }
1000
1001 return false;
1002 case LoggerConfig::LOCAL_LOGGER:
1003 return connection->name()->string_view() == node->name()->string_view();
Austin Schuh719946b2019-12-28 14:51:01 -08001004 case LoggerConfig::NOT_LOGGED:
1005 return false;
1006 }
1007
1008 LOG(FATAL) << "Unknown logger config "
1009 << static_cast<int>(connection->timestamp_logger());
1010}
1011
Austin Schuhe84c3ed2019-12-14 15:29:48 -08001012std::vector<std::string_view> SourceNodeNames(const Configuration *config,
1013 const Node *my_node) {
1014 std::set<std::string_view> result_set;
1015
1016 for (const Channel *channel : *config->channels()) {
1017 if (channel->has_destination_nodes()) {
1018 for (const Connection *connection : *channel->destination_nodes()) {
1019 if (connection->name()->string_view() ==
1020 my_node->name()->string_view()) {
1021 result_set.insert(channel->source_node()->string_view());
1022 }
1023 }
1024 }
1025 }
1026
1027 std::vector<std::string_view> result;
1028 for (const std::string_view source : result_set) {
1029 VLOG(1) << "Found a source node of " << source;
1030 result.emplace_back(source);
1031 }
1032 return result;
1033}
1034
1035std::vector<std::string_view> DestinationNodeNames(const Configuration *config,
1036 const Node *my_node) {
1037 std::vector<std::string_view> result;
1038
1039 for (const Channel *channel : *config->channels()) {
1040 if (channel->has_source_node() && channel->source_node()->string_view() ==
1041 my_node->name()->string_view()) {
1042 if (!channel->has_destination_nodes()) continue;
1043
1044 if (channel->source_node()->string_view() !=
1045 my_node->name()->string_view()) {
1046 continue;
1047 }
1048
1049 for (const Connection *connection : *channel->destination_nodes()) {
1050 if (std::find(result.begin(), result.end(),
1051 connection->name()->string_view()) == result.end()) {
1052 result.emplace_back(connection->name()->string_view());
1053 }
1054 }
1055 }
1056 }
1057
1058 for (const std::string_view destination : result) {
1059 VLOG(1) << "Found a destination node of " << destination;
1060 }
1061 return result;
1062}
1063
Austin Schuh8d7e0bb2020-10-02 17:57:00 -07001064std::vector<const Node *> TimestampNodes(const Configuration *config,
1065 const Node *my_node) {
1066 if (!configuration::MultiNode(config)) {
1067 CHECK(my_node == nullptr);
1068 return std::vector<const Node *>{};
1069 }
1070
1071 std::set<const Node *> timestamp_logger_nodes;
1072 for (const Channel *channel : *config->channels()) {
1073 if (!configuration::ChannelIsSendableOnNode(channel, my_node)) {
1074 continue;
1075 }
1076 if (!channel->has_destination_nodes()) {
1077 continue;
1078 }
1079 for (const Connection *connection : *channel->destination_nodes()) {
1080 const Node *other_node =
1081 configuration::GetNode(config, connection->name()->string_view());
1082
1083 if (configuration::ConnectionDeliveryTimeIsLoggedOnNode(connection,
1084 my_node)) {
1085 VLOG(1) << "Timestamps are logged from "
1086 << FlatbufferToJson(other_node);
1087 timestamp_logger_nodes.insert(other_node);
1088 }
1089 }
1090 }
1091
1092 std::vector<const Node *> result;
1093 for (const Node *node : timestamp_logger_nodes) {
1094 result.emplace_back(node);
1095 }
1096 return result;
1097}
1098
Brian Silverman66f079a2013-08-26 16:24:30 -07001099} // namespace configuration
1100} // namespace aos