blob: bc7182c5de6305d5140b14708f40d40a245b840d [file] [log] [blame]
John Park398c74a2018-10-20 21:17:39 -07001#include "aos/configuration.h"
Brian Silverman66f079a2013-08-26 16:24:30 -07002
Brian Silverman66f079a2013-08-26 16:24:30 -07003#include <arpa/inet.h>
4#include <ifaddrs.h>
Austin Schuhf1fff282020-03-28 16:57:32 -07005#include <netinet/in.h>
Austin Schuhf1fff282020-03-28 16:57:32 -07006#include <sys/types.h>
Brian Silverman66f079a2013-08-26 16:24:30 -07007#include <unistd.h>
Austin Schuhe84c3ed2019-12-14 15:29:48 -08008
Tyler Chatowbf0609c2021-07-31 16:13:27 -07009#include <cstdlib>
10#include <cstring>
Austin Schuh68d98592020-11-01 23:22:57 -080011#include <map>
Austin Schuhe84c3ed2019-12-14 15:29:48 -080012#include <set>
Austin Schuhef38cd22021-07-21 15:24:23 -070013#include <string>
James Kuszmaul3ae42262019-11-08 12:33:41 -080014#include <string_view>
Austin Schuhef38cd22021-07-21 15:24:23 -070015#include <vector>
Brian Silverman66f079a2013-08-26 16:24:30 -070016
Austin Schuhcb108412019-10-13 16:09:54 -070017#include "absl/container/btree_set.h"
Austin Schuha81454b2020-05-12 19:58:36 -070018#include "absl/strings/str_cat.h"
Austin Schuhef38cd22021-07-21 15:24:23 -070019#include "absl/strings/str_join.h"
20#include "absl/strings/str_split.h"
Austin Schuhcb108412019-10-13 16:09:54 -070021#include "aos/configuration_generated.h"
22#include "aos/flatbuffer_merge.h"
23#include "aos/json_to_flatbuffer.h"
Austin Schuh217a9782019-12-21 23:02:50 -080024#include "aos/network/team_number.h"
Austin Schuhcb108412019-10-13 16:09:54 -070025#include "aos/unique_malloc_ptr.h"
26#include "aos/util/file.h"
Austin Schuh217a9782019-12-21 23:02:50 -080027#include "gflags/gflags.h"
Austin Schuhcb108412019-10-13 16:09:54 -070028#include "glog/logging.h"
Austin Schuh217a9782019-12-21 23:02:50 -080029
Brian Silverman66f079a2013-08-26 16:24:30 -070030namespace aos {
Austin Schuh15182322020-10-10 15:25:21 -070031namespace {
32bool EndsWith(std::string_view str, std::string_view end) {
33 if (str.size() < end.size()) {
34 return false;
35 }
36 if (str.substr(str.size() - end.size(), end.size()) != end) {
37 return false;
38 }
39 return true;
40}
41
42std::string MaybeReplaceExtension(std::string_view filename,
43 std::string_view extension,
44 std::string_view replacement) {
45 if (!EndsWith(filename, extension)) {
46 return std::string(filename);
47 }
48 filename.remove_suffix(extension.size());
49 return absl::StrCat(filename, replacement);
50}
51
52FlatbufferDetachedBuffer<Configuration> ReadConfigFile(std::string_view path,
53 bool binary) {
54 if (binary) {
55 FlatbufferVector<Configuration> config =
56 FileToFlatbuffer<Configuration>(path);
57 return CopySpanAsDetachedBuffer(config.span());
58 }
59
60 flatbuffers::DetachedBuffer buffer = JsonToFlatbuffer(
61 util::ReadFileToStringOrDie(path), ConfigurationTypeTable());
62
Austin Schuh84a039a2021-11-03 16:50:34 -070063 CHECK_GT(buffer.size(), 0u) << ": Failed to parse JSON file: " << path;
Austin Schuh15182322020-10-10 15:25:21 -070064
65 return FlatbufferDetachedBuffer<Configuration>(std::move(buffer));
66}
67
68} // namespace
Austin Schuhcb108412019-10-13 16:09:54 -070069
Austin Schuh40485ed2019-10-26 21:51:44 -070070// Define the compare and equal operators for Channel and Application so we can
Austin Schuhcb108412019-10-13 16:09:54 -070071// insert them in the btree below.
Austin Schuh40485ed2019-10-26 21:51:44 -070072bool operator<(const FlatbufferDetachedBuffer<Channel> &lhs,
73 const FlatbufferDetachedBuffer<Channel> &rhs) {
Austin Schuhcb108412019-10-13 16:09:54 -070074 int name_compare = lhs.message().name()->string_view().compare(
75 rhs.message().name()->string_view());
76 if (name_compare == 0) {
77 return lhs.message().type()->string_view() <
78 rhs.message().type()->string_view();
79 } else if (name_compare < 0) {
80 return true;
81 } else {
82 return false;
83 }
84}
85
Austin Schuh40485ed2019-10-26 21:51:44 -070086bool operator==(const FlatbufferDetachedBuffer<Channel> &lhs,
87 const FlatbufferDetachedBuffer<Channel> &rhs) {
Austin Schuhcb108412019-10-13 16:09:54 -070088 return lhs.message().name()->string_view() ==
89 rhs.message().name()->string_view() &&
90 lhs.message().type()->string_view() ==
91 rhs.message().type()->string_view();
92}
93
Austin Schuha7996eb2021-10-11 19:03:24 -070094bool operator<(const FlatbufferDetachedBuffer<Connection> &lhs,
95 const FlatbufferDetachedBuffer<Connection> &rhs) {
96 return lhs.message().name()->string_view() <
97 rhs.message().name()->string_view();
98}
99
100bool operator==(const FlatbufferDetachedBuffer<Connection> &lhs,
101 const FlatbufferDetachedBuffer<Connection> &rhs) {
102 return lhs.message().name()->string_view() ==
103 rhs.message().name()->string_view();
104}
105
Austin Schuh40485ed2019-10-26 21:51:44 -0700106bool operator==(const FlatbufferDetachedBuffer<Application> &lhs,
107 const FlatbufferDetachedBuffer<Application> &rhs) {
Austin Schuhcb108412019-10-13 16:09:54 -0700108 return lhs.message().name()->string_view() ==
109 rhs.message().name()->string_view();
110}
111
Austin Schuh40485ed2019-10-26 21:51:44 -0700112bool operator<(const FlatbufferDetachedBuffer<Application> &lhs,
113 const FlatbufferDetachedBuffer<Application> &rhs) {
Austin Schuhcb108412019-10-13 16:09:54 -0700114 return lhs.message().name()->string_view() <
115 rhs.message().name()->string_view();
116}
117
Austin Schuh217a9782019-12-21 23:02:50 -0800118bool operator==(const FlatbufferDetachedBuffer<Node> &lhs,
119 const FlatbufferDetachedBuffer<Node> &rhs) {
120 return lhs.message().name()->string_view() ==
121 rhs.message().name()->string_view();
122}
123
124bool operator<(const FlatbufferDetachedBuffer<Node> &lhs,
125 const FlatbufferDetachedBuffer<Node> &rhs) {
126 return lhs.message().name()->string_view() <
127 rhs.message().name()->string_view();
128}
129
Brian Silverman66f079a2013-08-26 16:24:30 -0700130namespace configuration {
131namespace {
132
Austin Schuhcb108412019-10-13 16:09:54 -0700133// Extracts the folder part of a path. Returns ./ if there is no path.
Austin Schuhf1fff282020-03-28 16:57:32 -0700134std::string_view ExtractFolder(const std::string_view filename) {
Austin Schuhcb108412019-10-13 16:09:54 -0700135 auto last_slash_pos = filename.find_last_of("/\\");
136
James Kuszmaul3ae42262019-11-08 12:33:41 -0800137 return last_slash_pos == std::string_view::npos
138 ? std::string_view("./")
Austin Schuhcb108412019-10-13 16:09:54 -0700139 : filename.substr(0, last_slash_pos + 1);
140}
141
James Kuszmaulc0c08da2020-05-10 18:56:07 -0700142std::string AbsolutePath(const std::string_view filename) {
143 // Uses an std::string so that we know the input will be null-terminated.
144 const std::string terminated_file(filename);
145 char buffer[PATH_MAX];
146 PCHECK(NULL != realpath(terminated_file.c_str(), buffer));
147 return buffer;
148}
149
Austin Schuhef38cd22021-07-21 15:24:23 -0700150std::string RemoveDotDots(const std::string_view filename) {
151 std::vector<std::string> split = absl::StrSplit(filename, '/');
152 auto iterator = split.begin();
153 while (iterator != split.end()) {
154 if (iterator->empty()) {
155 iterator = split.erase(iterator);
156 } else if (*iterator == ".") {
157 iterator = split.erase(iterator);
158 } else if (*iterator == "..") {
159 CHECK(iterator != split.begin())
160 << ": Import path may not start with ..: " << filename;
161 auto previous = iterator;
162 --previous;
163 split.erase(iterator);
164 iterator = split.erase(previous);
165 } else {
166 ++iterator;
167 }
168 }
169 return absl::StrJoin(split, "/");
170}
171
Austin Schuh40485ed2019-10-26 21:51:44 -0700172FlatbufferDetachedBuffer<Configuration> ReadConfig(
James Kuszmaulc0c08da2020-05-10 18:56:07 -0700173 const std::string_view path, absl::btree_set<std::string> *visited_paths,
174 const std::vector<std::string_view> &extra_import_paths) {
Austin Schuh15182322020-10-10 15:25:21 -0700175 std::string binary_path = MaybeReplaceExtension(path, ".json", ".bfbs");
Austin Schuhef38cd22021-07-21 15:24:23 -0700176 VLOG(1) << "Looking up: " << path << ", starting with: " << binary_path;
Austin Schuh15182322020-10-10 15:25:21 -0700177 bool binary_path_exists = util::PathExists(binary_path);
James Kuszmaulc0c08da2020-05-10 18:56:07 -0700178 std::string raw_path(path);
Austin Schuh15182322020-10-10 15:25:21 -0700179 // For each .json file, look and see if we can find a .bfbs file next to it
180 // with the same base name. If we can, assume it is the same and use it
181 // instead. It is much faster to load .bfbs files than .json files.
182 if (!binary_path_exists && !util::PathExists(raw_path)) {
183 const bool path_is_absolute = raw_path.size() > 0 && raw_path[0] == '/';
James Kuszmaulc0c08da2020-05-10 18:56:07 -0700184 if (path_is_absolute) {
185 CHECK(extra_import_paths.empty())
186 << "Can't specify extra import paths if attempting to read a config "
187 "file from an absolute path (path is "
Austin Schuh15182322020-10-10 15:25:21 -0700188 << raw_path << ").";
James Kuszmaulc0c08da2020-05-10 18:56:07 -0700189 }
190
191 bool found_path = false;
192 for (const auto &import_path : extra_import_paths) {
Austin Schuhef38cd22021-07-21 15:24:23 -0700193 raw_path = std::string(import_path) + "/" + RemoveDotDots(path);
Austin Schuh15182322020-10-10 15:25:21 -0700194 binary_path = MaybeReplaceExtension(raw_path, ".json", ".bfbs");
Austin Schuhef38cd22021-07-21 15:24:23 -0700195 VLOG(1) << "Checking: " << binary_path;
Austin Schuh15182322020-10-10 15:25:21 -0700196 binary_path_exists = util::PathExists(binary_path);
197 if (binary_path_exists) {
198 found_path = true;
199 break;
200 }
Austin Schuhef38cd22021-07-21 15:24:23 -0700201 VLOG(1) << "Checking: " << raw_path;
James Kuszmaulc0c08da2020-05-10 18:56:07 -0700202 if (util::PathExists(raw_path)) {
203 found_path = true;
204 break;
205 }
206 }
207 CHECK(found_path) << ": Failed to find file " << path << ".";
208 }
Alex Perrycb7da4b2019-08-28 19:35:56 -0700209
Austin Schuh15182322020-10-10 15:25:21 -0700210 FlatbufferDetachedBuffer<Configuration> config = ReadConfigFile(
211 binary_path_exists ? binary_path : raw_path, binary_path_exists);
James Kuszmaulc0c08da2020-05-10 18:56:07 -0700212
Austin Schuhcb108412019-10-13 16:09:54 -0700213 // Depth first. Take the following example:
214 //
215 // config1.json:
216 // {
Austin Schuh40485ed2019-10-26 21:51:44 -0700217 // "channels": [
Austin Schuhcb108412019-10-13 16:09:54 -0700218 // {
219 // "name": "/foo",
220 // "type": ".aos.bar",
221 // "max_size": 5
222 // }
223 // ],
224 // "imports": [
225 // "config2.json",
226 // ]
227 // }
228 //
229 // config2.json:
230 // {
Austin Schuh40485ed2019-10-26 21:51:44 -0700231 // "channels": [
Austin Schuhcb108412019-10-13 16:09:54 -0700232 // {
233 // "name": "/foo",
234 // "type": ".aos.bar",
235 // "max_size": 7
236 // }
237 // ],
238 // }
239 //
240 // We want the main config (config1.json) to be able to override the imported
241 // config. That means that it needs to be merged into the imported configs,
242 // not the other way around.
243
Austin Schuh15182322020-10-10 15:25:21 -0700244 const std::string absolute_path =
245 AbsolutePath(binary_path_exists ? binary_path : raw_path);
246 // Track that we have seen this file before recursing. Track the path we
247 // actually loaded (which should be consistent if imported twice).
James Kuszmaulc0c08da2020-05-10 18:56:07 -0700248 if (!visited_paths->insert(absolute_path).second) {
249 for (const auto &visited_path : *visited_paths) {
250 LOG(INFO) << "Already visited: " << visited_path;
251 }
252 LOG(FATAL)
253 << "Already imported " << path << " (i.e. " << absolute_path
254 << "). See above for the files that have already been processed.";
255 }
Austin Schuhcb108412019-10-13 16:09:54 -0700256
257 if (config.message().has_imports()) {
258 // Capture the imports.
259 const flatbuffers::Vector<flatbuffers::Offset<flatbuffers::String>> *v =
260 config.message().imports();
261
262 // And then wipe them. This gets GCed when we merge later.
263 config.mutable_message()->clear_imports();
264
265 // Start with an empty configuration to merge into.
Austin Schuh40485ed2019-10-26 21:51:44 -0700266 FlatbufferDetachedBuffer<Configuration> merged_config =
267 FlatbufferDetachedBuffer<Configuration>::Empty();
Austin Schuhcb108412019-10-13 16:09:54 -0700268
James Kuszmaulc0c08da2020-05-10 18:56:07 -0700269 const std::string path_folder(ExtractFolder(path));
Austin Schuhcb108412019-10-13 16:09:54 -0700270 for (const flatbuffers::String *str : *v) {
James Kuszmaulc0c08da2020-05-10 18:56:07 -0700271 const std::string included_config =
272 path_folder + "/" + std::string(str->string_view());
Austin Schuhcb108412019-10-13 16:09:54 -0700273
274 // And them merge everything in.
275 merged_config = MergeFlatBuffers(
James Kuszmaulc0c08da2020-05-10 18:56:07 -0700276 merged_config,
277 ReadConfig(included_config, visited_paths, extra_import_paths));
Austin Schuhcb108412019-10-13 16:09:54 -0700278 }
279
280 // Finally, merge this file in.
281 config = MergeFlatBuffers(merged_config, config);
282 }
283 return config;
284}
285
Alex Perrycb7da4b2019-08-28 19:35:56 -0700286// Compares (c < p) a channel, and a name, type tuple.
287bool CompareChannels(const Channel *c,
James Kuszmaul3ae42262019-11-08 12:33:41 -0800288 ::std::pair<std::string_view, std::string_view> p) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700289 int name_compare = c->name()->string_view().compare(p.first);
290 if (name_compare == 0) {
291 return c->type()->string_view() < p.second;
292 } else if (name_compare < 0) {
293 return true;
294 } else {
295 return false;
296 }
297};
298
299// Compares for equality (c == p) a channel, and a name, type tuple.
300bool EqualsChannels(const Channel *c,
Austin Schuhf1fff282020-03-28 16:57:32 -0700301 ::std::pair<std::string_view, std::string_view> p) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700302 return c->name()->string_view() == p.first &&
303 c->type()->string_view() == p.second;
304}
305
306// Compares (c < p) an application, and a name;
James Kuszmaul3ae42262019-11-08 12:33:41 -0800307bool CompareApplications(const Application *a, std::string_view name) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700308 return a->name()->string_view() < name;
309};
310
311// Compares for equality (c == p) an application, and a name;
James Kuszmaul3ae42262019-11-08 12:33:41 -0800312bool EqualsApplications(const Application *a, std::string_view name) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700313 return a->name()->string_view() == name;
314}
315
Austin Schuh15182322020-10-10 15:25:21 -0700316void ValidateConfiguration(const Flatbuffer<Configuration> &config) {
317 // No imports should be left.
318 CHECK(!config.message().has_imports());
319
320 // Check that if there is a node list, all the source nodes are filled out and
321 // valid, and all the destination nodes are valid (and not the source). This
322 // is a basic consistency check.
323 if (config.message().has_channels()) {
324 const Channel *last_channel = nullptr;
325 for (const Channel *c : *config.message().channels()) {
326 CHECK(c->has_name());
327 CHECK(c->has_type());
328 if (c->name()->string_view().back() == '/') {
329 LOG(FATAL) << "Channel names can't end with '/'";
330 }
331 if (c->name()->string_view().find("//") != std::string_view::npos) {
332 LOG(FATAL) << ": Invalid channel name " << c->name()->string_view()
333 << ", can't use //.";
334 }
335 for (const char data : c->name()->string_view()) {
336 if (data >= '0' && data <= '9') {
337 continue;
338 }
339 if (data >= 'a' && data <= 'z') {
340 continue;
341 }
342 if (data >= 'A' && data <= 'Z') {
343 continue;
344 }
345 if (data == '-' || data == '_' || data == '/') {
346 continue;
347 }
348 LOG(FATAL) << "Invalid channel name " << c->name()->string_view()
349 << ", can only use [-a-zA-Z0-9_/]";
350 }
351
Austin Schuha156fb22021-10-11 19:23:21 -0700352 if (c->has_logger_nodes()) {
353 // Confirm that we don't have duplicate logger nodes.
354 absl::btree_set<std::string_view> logger_nodes;
355 for (const flatbuffers::String *s : *c->logger_nodes()) {
356 logger_nodes.insert(s->string_view());
357 }
358 CHECK_EQ(static_cast<size_t>(logger_nodes.size()),
359 c->logger_nodes()->size())
360 << ": Found duplicate logger_nodes in "
361 << CleanedChannelToString(c);
362 }
363
364 if (c->has_destination_nodes()) {
365 // Confirm that we don't have duplicate timestamp logger nodes.
Austin Schuh5e95bd62021-10-11 18:40:22 -0700366 for (const Connection *d : *c->destination_nodes()) {
Austin Schuha156fb22021-10-11 19:23:21 -0700367 if (d->has_timestamp_logger_nodes()) {
368 absl::btree_set<std::string_view> timestamp_logger_nodes;
369 for (const flatbuffers::String *s : *d->timestamp_logger_nodes()) {
370 timestamp_logger_nodes.insert(s->string_view());
371 }
372 CHECK_EQ(static_cast<size_t>(timestamp_logger_nodes.size()),
373 d->timestamp_logger_nodes()->size())
374 << ": Found duplicate timestamp_logger_nodes in "
375 << CleanedChannelToString(c);
376 }
377 }
378
379 // There is no good use case today for logging timestamps but not the
380 // corresponding data. Instead of plumbing through all of this on the
381 // reader side, let'd just disallow it for now.
382 if (c->logger() == LoggerConfig::NOT_LOGGED) {
383 for (const Connection *d : *c->destination_nodes()) {
384 CHECK(d->timestamp_logger() == LoggerConfig::NOT_LOGGED)
385 << ": Logging timestamps without data is not supported. If "
386 "you have a good use case, let's talk. "
387 << CleanedChannelToString(c);
388 }
Austin Schuh5e95bd62021-10-11 18:40:22 -0700389 }
390 }
391
Austin Schuh15182322020-10-10 15:25:21 -0700392 // Make sure everything is sorted while we are here... If this fails,
393 // there will be a bunch of weird errors.
394 if (last_channel != nullptr) {
395 CHECK(CompareChannels(
396 last_channel,
397 std::make_pair(c->name()->string_view(), c->type()->string_view())))
398 << ": Channels not sorted!";
399 }
400 last_channel = c;
401 }
402 }
403
404 if (config.message().has_nodes() && config.message().has_channels()) {
405 for (const Channel *c : *config.message().channels()) {
406 CHECK(c->has_source_node()) << ": Channel " << FlatbufferToJson(c)
407 << " is missing \"source_node\"";
408 CHECK(GetNode(&config.message(), c->source_node()->string_view()) !=
409 nullptr)
410 << ": Channel " << FlatbufferToJson(c)
411 << " has an unknown \"source_node\"";
412
413 if (c->has_destination_nodes()) {
414 for (const Connection *connection : *c->destination_nodes()) {
415 CHECK(connection->has_name());
416 CHECK(GetNode(&config.message(), connection->name()->string_view()) !=
417 nullptr)
418 << ": Channel " << FlatbufferToJson(c)
419 << " has an unknown \"destination_nodes\" "
420 << connection->name()->string_view();
421
422 switch (connection->timestamp_logger()) {
423 case LoggerConfig::LOCAL_LOGGER:
424 case LoggerConfig::NOT_LOGGED:
425 CHECK(!connection->has_timestamp_logger_nodes());
426 break;
427 case LoggerConfig::REMOTE_LOGGER:
428 case LoggerConfig::LOCAL_AND_REMOTE_LOGGER:
429 CHECK(connection->has_timestamp_logger_nodes());
430 CHECK_GT(connection->timestamp_logger_nodes()->size(), 0u);
431 for (const flatbuffers::String *timestamp_logger_node :
432 *connection->timestamp_logger_nodes()) {
433 CHECK(GetNode(&config.message(),
434 timestamp_logger_node->string_view()) != nullptr)
435 << ": Channel " << FlatbufferToJson(c)
436 << " has an unknown \"timestamp_logger_node\""
437 << connection->name()->string_view();
438 }
439 break;
440 }
441
442 CHECK_NE(connection->name()->string_view(),
443 c->source_node()->string_view())
444 << ": Channel " << FlatbufferToJson(c)
445 << " is forwarding data to itself";
446 }
447 }
448 }
449 }
450}
451
Alex Perrycb7da4b2019-08-28 19:35:56 -0700452} // namespace
453
Austin Schuh006a9f52021-04-07 16:24:18 -0700454// Maps name for the provided maps. Modifies name.
Brian Silvermanf3798cb2021-11-10 12:26:34 -0800455//
456// This is called many times during startup, and it dereferences a lot of
457// pointers. These combine to make it a performance hotspot during many tests
458// under msan, so there is some optimizing around caching intermediates instead
459// of dereferencing the pointer multiple times.
Austin Schuh006a9f52021-04-07 16:24:18 -0700460void HandleMaps(const flatbuffers::Vector<flatbuffers::Offset<aos::Map>> *maps,
461 std::string *name, std::string_view type, const Node *node) {
462 // For the same reason we merge configs in reverse order, we want to process
463 // maps in reverse order. That lets the outer config overwrite channels from
464 // the inner configs.
465 for (auto i = maps->rbegin(); i != maps->rend(); ++i) {
Brian Silvermanf3798cb2021-11-10 12:26:34 -0800466 const Channel *const match = i->match();
467 if (!match) {
Austin Schuh006a9f52021-04-07 16:24:18 -0700468 continue;
469 }
Brian Silvermanf3798cb2021-11-10 12:26:34 -0800470 const flatbuffers::String *const match_name_string = match->name();
471 if (!match_name_string) {
472 continue;
473 }
474 const Channel *const rename = i->rename();
475 if (!rename) {
476 continue;
477 }
478 const flatbuffers::String *const rename_name_string = rename->name();
479 if (!rename_name_string) {
Austin Schuh006a9f52021-04-07 16:24:18 -0700480 continue;
481 }
482
483 // Handle normal maps (now that we know that match and rename are filled
484 // out).
Brian Silvermanf3798cb2021-11-10 12:26:34 -0800485 const std::string_view match_name = match_name_string->string_view();
Austin Schuh006a9f52021-04-07 16:24:18 -0700486 if (match_name != *name) {
487 if (match_name.back() == '*' &&
488 std::string_view(*name).substr(
489 0, std::min(name->size(), match_name.size() - 1)) ==
490 match_name.substr(0, match_name.size() - 1)) {
491 CHECK_EQ(match_name.find('*'), match_name.size() - 1);
492 } else {
493 continue;
494 }
495 }
496
497 // Handle type specific maps.
Brian Silvermanf3798cb2021-11-10 12:26:34 -0800498 const flatbuffers::String *const match_type_string = match->type();
499 if (match_type_string && match_type_string->string_view() != type) {
Austin Schuh006a9f52021-04-07 16:24:18 -0700500 continue;
501 }
502
503 // Now handle node specific maps.
Brian Silvermanf3798cb2021-11-10 12:26:34 -0800504 const flatbuffers::String *const match_source_node_string =
505 match->source_node();
506 if (node && match_source_node_string &&
507 match_source_node_string->string_view() !=
Austin Schuh006a9f52021-04-07 16:24:18 -0700508 node->name()->string_view()) {
509 continue;
510 }
511
Brian Silvermanf3798cb2021-11-10 12:26:34 -0800512 std::string new_name(rename_name_string->string_view());
Austin Schuh006a9f52021-04-07 16:24:18 -0700513 if (match_name.back() == '*') {
514 new_name += std::string(name->substr(match_name.size() - 1));
515 }
516 VLOG(1) << "Renamed \"" << *name << "\" to \"" << new_name << "\"";
517 *name = std::move(new_name);
518 }
519}
520
Austin Schuh40485ed2019-10-26 21:51:44 -0700521FlatbufferDetachedBuffer<Configuration> MergeConfiguration(
Austin Schuhcb108412019-10-13 16:09:54 -0700522 const Flatbuffer<Configuration> &config) {
James Kuszmaul3c998592020-07-27 21:04:47 -0700523 // auto_merge_config will contain all the fields of the Configuration that are
524 // to be passed through unmodified to the result of MergeConfiguration().
525 // In the processing below, we mutate auto_merge_config to remove any fields
526 // which we do need to alter (hence why we can't use the input config
527 // directly), and then merge auto_merge_config back in at the end.
528 aos::FlatbufferDetachedBuffer<aos::Configuration> auto_merge_config =
Austin Schuha4fc60f2020-11-01 23:06:47 -0800529 aos::RecursiveCopyFlatBuffer(&config.message());
James Kuszmaul3c998592020-07-27 21:04:47 -0700530
Austin Schuh40485ed2019-10-26 21:51:44 -0700531 // Store all the channels in a sorted set. This lets us track channels we
Austin Schuhcb108412019-10-13 16:09:54 -0700532 // have seen before and merge the updates in.
Austin Schuh40485ed2019-10-26 21:51:44 -0700533 absl::btree_set<FlatbufferDetachedBuffer<Channel>> channels;
Austin Schuhcb108412019-10-13 16:09:54 -0700534
Austin Schuh40485ed2019-10-26 21:51:44 -0700535 if (config.message().has_channels()) {
James Kuszmaul3c998592020-07-27 21:04:47 -0700536 auto_merge_config.mutable_message()->clear_channels();
Austin Schuh40485ed2019-10-26 21:51:44 -0700537 for (const Channel *c : *config.message().channels()) {
Austin Schuhcb108412019-10-13 16:09:54 -0700538 // Ignore malformed entries.
Austin Schuh40485ed2019-10-26 21:51:44 -0700539 if (!c->has_name()) {
Austin Schuhcb108412019-10-13 16:09:54 -0700540 continue;
541 }
Austin Schuh40485ed2019-10-26 21:51:44 -0700542 if (!c->has_type()) {
Austin Schuhcb108412019-10-13 16:09:54 -0700543 continue;
544 }
545
Brian Silverman77162972020-08-12 19:52:40 -0700546 CHECK_EQ(c->read_method() == ReadMethod::PIN, c->num_readers() != 0)
547 << ": num_readers may be set if and only if read_method is PIN,"
548 " if you want 0 readers do not set PIN: "
549 << CleanedChannelToString(c);
550
Austin Schuh40485ed2019-10-26 21:51:44 -0700551 // Attempt to insert the channel.
Austin Schuha4fc60f2020-11-01 23:06:47 -0800552 auto result = channels.insert(RecursiveCopyFlatBuffer(c));
Austin Schuhcb108412019-10-13 16:09:54 -0700553 if (!result.second) {
554 // Already there, so merge the new table into the original.
Austin Schuh4a5f5d22021-10-12 15:09:35 -0700555 // Schemas merge poorly, so pick the newest one.
556 if (result.first->message().has_schema() && c->has_schema()) {
557 result.first->mutable_message()->clear_schema();
558 }
Austin Schuha7996eb2021-10-11 19:03:24 -0700559 auto merged =
Austin Schuha4fc60f2020-11-01 23:06:47 -0800560 MergeFlatBuffers(*result.first, RecursiveCopyFlatBuffer(c));
Austin Schuha7996eb2021-10-11 19:03:24 -0700561
562 if (merged.message().has_destination_nodes()) {
563 absl::btree_set<FlatbufferDetachedBuffer<Connection>> connections;
564 for (const Connection *connection :
565 *merged.message().destination_nodes()) {
566 auto connection_result =
567 connections.insert(RecursiveCopyFlatBuffer(connection));
568 if (!connection_result.second) {
569 *connection_result.first =
570 MergeFlatBuffers(*connection_result.first,
571 RecursiveCopyFlatBuffer(connection));
572 }
573 }
574 if (static_cast<size_t>(connections.size()) !=
575 merged.message().destination_nodes()->size()) {
576 merged.mutable_message()->clear_destination_nodes();
577 flatbuffers::FlatBufferBuilder fbb;
578 fbb.ForceDefaults(true);
579 std::vector<flatbuffers::Offset<Connection>> connection_offsets;
580 for (const FlatbufferDetachedBuffer<Connection> &connection :
581 connections) {
582 connection_offsets.push_back(
583 RecursiveCopyFlatBuffer(&connection.message(), &fbb));
584 }
585 flatbuffers::Offset<
586 flatbuffers::Vector<flatbuffers::Offset<Connection>>>
587 destination_nodes_offset = fbb.CreateVector(connection_offsets);
588 Channel::Builder channel_builder(fbb);
589 channel_builder.add_destination_nodes(destination_nodes_offset);
590 fbb.Finish(channel_builder.Finish());
591 FlatbufferDetachedBuffer<Channel> destinations_channel(
592 fbb.Release());
593 merged = MergeFlatBuffers(merged, destinations_channel);
594 }
595 }
596
597 *result.first = std::move(merged);
Austin Schuhcb108412019-10-13 16:09:54 -0700598 }
599 }
600 }
601
602 // Now repeat this for the application list.
Austin Schuh40485ed2019-10-26 21:51:44 -0700603 absl::btree_set<FlatbufferDetachedBuffer<Application>> applications;
Austin Schuhcb108412019-10-13 16:09:54 -0700604 if (config.message().has_applications()) {
James Kuszmaul3c998592020-07-27 21:04:47 -0700605 auto_merge_config.mutable_message()->clear_applications();
Austin Schuhcb108412019-10-13 16:09:54 -0700606 for (const Application *a : *config.message().applications()) {
607 if (!a->has_name()) {
608 continue;
609 }
610
Austin Schuha4fc60f2020-11-01 23:06:47 -0800611 auto result = applications.insert(RecursiveCopyFlatBuffer(a));
Austin Schuhcb108412019-10-13 16:09:54 -0700612 if (!result.second) {
Austin Schuha4fc60f2020-11-01 23:06:47 -0800613 *result.first =
614 MergeFlatBuffers(*result.first, RecursiveCopyFlatBuffer(a));
Austin Schuhcb108412019-10-13 16:09:54 -0700615 }
616 }
617 }
618
Austin Schuh217a9782019-12-21 23:02:50 -0800619 // Now repeat this for the node list.
620 absl::btree_set<FlatbufferDetachedBuffer<Node>> nodes;
621 if (config.message().has_nodes()) {
James Kuszmaul3c998592020-07-27 21:04:47 -0700622 auto_merge_config.mutable_message()->clear_nodes();
Austin Schuh217a9782019-12-21 23:02:50 -0800623 for (const Node *n : *config.message().nodes()) {
624 if (!n->has_name()) {
625 continue;
626 }
627
Austin Schuha4fc60f2020-11-01 23:06:47 -0800628 auto result = nodes.insert(RecursiveCopyFlatBuffer(n));
Austin Schuh217a9782019-12-21 23:02:50 -0800629 if (!result.second) {
Austin Schuha4fc60f2020-11-01 23:06:47 -0800630 *result.first =
631 MergeFlatBuffers(*result.first, RecursiveCopyFlatBuffer(n));
Austin Schuh217a9782019-12-21 23:02:50 -0800632 }
633 }
634 }
635
Austin Schuhcb108412019-10-13 16:09:54 -0700636 flatbuffers::FlatBufferBuilder fbb;
Austin Schuhd7b15da2020-02-17 15:06:11 -0800637 fbb.ForceDefaults(true);
Austin Schuhcb108412019-10-13 16:09:54 -0700638
639 // Start by building the vectors. They need to come before the final table.
Austin Schuh40485ed2019-10-26 21:51:44 -0700640 // Channels
641 flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<Channel>>>
642 channels_offset;
Austin Schuhcb108412019-10-13 16:09:54 -0700643 {
Austin Schuh40485ed2019-10-26 21:51:44 -0700644 ::std::vector<flatbuffers::Offset<Channel>> channel_offsets;
645 for (const FlatbufferDetachedBuffer<Channel> &c : channels) {
Austin Schuha4fc60f2020-11-01 23:06:47 -0800646 channel_offsets.emplace_back(
647 RecursiveCopyFlatBuffer<Channel>(&c.message(), &fbb));
Austin Schuhcb108412019-10-13 16:09:54 -0700648 }
Austin Schuh40485ed2019-10-26 21:51:44 -0700649 channels_offset = fbb.CreateVector(channel_offsets);
Austin Schuhcb108412019-10-13 16:09:54 -0700650 }
651
652 // Applications
653 flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<Application>>>
654 applications_offset;
655 {
656 ::std::vector<flatbuffers::Offset<Application>> applications_offsets;
Austin Schuh40485ed2019-10-26 21:51:44 -0700657 for (const FlatbufferDetachedBuffer<Application> &a : applications) {
Austin Schuhcb108412019-10-13 16:09:54 -0700658 applications_offsets.emplace_back(
Austin Schuha4fc60f2020-11-01 23:06:47 -0800659 RecursiveCopyFlatBuffer<Application>(&a.message(), &fbb));
Austin Schuhcb108412019-10-13 16:09:54 -0700660 }
661 applications_offset = fbb.CreateVector(applications_offsets);
662 }
663
Austin Schuh217a9782019-12-21 23:02:50 -0800664 // Nodes
665 flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<Node>>>
666 nodes_offset;
667 {
668 ::std::vector<flatbuffers::Offset<Node>> node_offsets;
669 for (const FlatbufferDetachedBuffer<Node> &n : nodes) {
Austin Schuha4fc60f2020-11-01 23:06:47 -0800670 node_offsets.emplace_back(
671 RecursiveCopyFlatBuffer<Node>(&n.message(), &fbb));
Austin Schuh217a9782019-12-21 23:02:50 -0800672 }
673 nodes_offset = fbb.CreateVector(node_offsets);
674 }
675
Austin Schuhcb108412019-10-13 16:09:54 -0700676 // And then build a Configuration with them all.
677 ConfigurationBuilder configuration_builder(fbb);
Austin Schuh40485ed2019-10-26 21:51:44 -0700678 configuration_builder.add_channels(channels_offset);
Austin Schuh217a9782019-12-21 23:02:50 -0800679 if (config.message().has_applications()) {
680 configuration_builder.add_applications(applications_offset);
681 }
682 if (config.message().has_nodes()) {
683 configuration_builder.add_nodes(nodes_offset);
684 }
Austin Schuhcb108412019-10-13 16:09:54 -0700685
686 fbb.Finish(configuration_builder.Finish());
Austin Schuh217a9782019-12-21 23:02:50 -0800687
James Kuszmaul3c998592020-07-27 21:04:47 -0700688 aos::FlatbufferDetachedBuffer<aos::Configuration> modified_config(
689 fbb.Release());
690
Austin Schuh217a9782019-12-21 23:02:50 -0800691 // Now, validate that if there is a node list, every channel has a source
692 // node.
James Kuszmaul3c998592020-07-27 21:04:47 -0700693 FlatbufferDetachedBuffer<Configuration> result =
694 MergeFlatBuffers(modified_config, auto_merge_config);
Austin Schuh217a9782019-12-21 23:02:50 -0800695
Austin Schuh15182322020-10-10 15:25:21 -0700696 ValidateConfiguration(result);
Austin Schuh217a9782019-12-21 23:02:50 -0800697
698 return result;
Austin Schuhcb108412019-10-13 16:09:54 -0700699}
700
Austin Schuh40485ed2019-10-26 21:51:44 -0700701FlatbufferDetachedBuffer<Configuration> ReadConfig(
James Kuszmaulc0c08da2020-05-10 18:56:07 -0700702 const std::string_view path,
Austin Schuhef38cd22021-07-21 15:24:23 -0700703 const std::vector<std::string_view> &extra_import_paths) {
Austin Schuhcb108412019-10-13 16:09:54 -0700704 // We only want to read a file once. So track the visited files in a set.
705 absl::btree_set<std::string> visited_paths;
Austin Schuh15182322020-10-10 15:25:21 -0700706 FlatbufferDetachedBuffer<Configuration> read_config =
Austin Schuhef38cd22021-07-21 15:24:23 -0700707 ReadConfig(path, &visited_paths, extra_import_paths);
Austin Schuh15182322020-10-10 15:25:21 -0700708
709 // If we only read one file, and it had a .bfbs extension, it has to be a
710 // fully formatted config. Do a quick verification and return it.
711 if (visited_paths.size() == 1 && EndsWith(*visited_paths.begin(), ".bfbs")) {
712 ValidateConfiguration(read_config);
713 return read_config;
714 }
715
716 return MergeConfiguration(read_config);
Austin Schuhcb108412019-10-13 16:09:54 -0700717}
718
Austin Schuh8d6cea82020-02-28 12:17:16 -0800719FlatbufferDetachedBuffer<Configuration> MergeWithConfig(
Brian Silverman24f5aa82020-06-23 16:21:28 -0700720 const Configuration *config, const Flatbuffer<Configuration> &addition) {
721 return MergeConfiguration(MergeFlatBuffers(config, &addition.message()));
722}
723
724FlatbufferDetachedBuffer<Configuration> MergeWithConfig(
Austin Schuh8d6cea82020-02-28 12:17:16 -0800725 const Configuration *config, std::string_view json) {
726 FlatbufferDetachedBuffer<Configuration> addition =
727 JsonToFlatbuffer(json, Configuration::MiniReflectTypeTable());
728
Brian Silverman24f5aa82020-06-23 16:21:28 -0700729 return MergeWithConfig(config, addition);
Austin Schuh8d6cea82020-02-28 12:17:16 -0800730}
731
James Kuszmaul3ae42262019-11-08 12:33:41 -0800732const Channel *GetChannel(const Configuration *config, std::string_view name,
733 std::string_view type,
Austin Schuh0de30f32020-12-06 12:44:28 -0800734 std::string_view application_name, const Node *node,
735 bool quiet) {
Brian Silverman9fcf2c72020-12-21 18:30:58 -0800736 if (!config->has_channels()) {
737 return nullptr;
738 }
739
Austin Schuhbca6cf02019-12-22 17:28:34 -0800740 const std::string_view original_name = name;
Austin Schuhf1fff282020-03-28 16:57:32 -0700741 std::string mutable_name;
Austin Schuh4c3b9702020-08-30 11:34:55 -0700742 if (node != nullptr) {
743 VLOG(1) << "Looking up { \"name\": \"" << name << "\", \"type\": \"" << type
744 << "\" } on " << aos::FlatbufferToJson(node);
745 } else {
746 VLOG(1) << "Looking up { \"name\": \"" << name << "\", \"type\": \"" << type
747 << "\" }";
748 }
Austin Schuhcb108412019-10-13 16:09:54 -0700749
750 // First handle application specific maps. Only do this if we have a matching
751 // application name, and it has maps.
Austin Schuhd2e2f6a2021-02-07 20:46:16 -0800752 {
753 const Application *application =
754 GetApplication(config, node, application_name);
755 if (application != nullptr && application->has_maps()) {
756 mutable_name = std::string(name);
757 HandleMaps(application->maps(), &mutable_name, type, node);
758 name = std::string_view(mutable_name);
Austin Schuhcb108412019-10-13 16:09:54 -0700759 }
760 }
761
762 // Now do global maps.
Austin Schuh40485ed2019-10-26 21:51:44 -0700763 if (config->has_maps()) {
Austin Schuhf1fff282020-03-28 16:57:32 -0700764 mutable_name = std::string(name);
765 HandleMaps(config->maps(), &mutable_name, type, node);
766 name = std::string_view(mutable_name);
Austin Schuhcb108412019-10-13 16:09:54 -0700767 }
768
Austin Schuhbca6cf02019-12-22 17:28:34 -0800769 if (original_name != name) {
770 VLOG(1) << "Remapped to { \"name\": \"" << name << "\", \"type\": \""
771 << type << "\" }";
772 }
Alex Perrycb7da4b2019-08-28 19:35:56 -0700773
James Kuszmaul71a81932020-12-15 21:08:01 -0800774 // Then look for the channel (note that this relies on the channels being
775 // sorted in the config).
Austin Schuh40485ed2019-10-26 21:51:44 -0700776 auto channel_iterator =
Austin Schuhf1fff282020-03-28 16:57:32 -0700777 std::lower_bound(config->channels()->cbegin(), config->channels()->cend(),
Austin Schuh40485ed2019-10-26 21:51:44 -0700778 std::make_pair(name, type), CompareChannels);
Austin Schuhcb108412019-10-13 16:09:54 -0700779
780 // Make sure we actually found it, and it matches.
Austin Schuh40485ed2019-10-26 21:51:44 -0700781 if (channel_iterator != config->channels()->cend() &&
782 EqualsChannels(*channel_iterator, std::make_pair(name, type))) {
Austin Schuhbca6cf02019-12-22 17:28:34 -0800783 if (VLOG_IS_ON(2)) {
784 VLOG(2) << "Found: " << FlatbufferToJson(*channel_iterator);
785 } else if (VLOG_IS_ON(1)) {
786 VLOG(1) << "Found: " << CleanedChannelToString(*channel_iterator);
787 }
Austin Schuh40485ed2019-10-26 21:51:44 -0700788 return *channel_iterator;
Austin Schuhcb108412019-10-13 16:09:54 -0700789 } else {
790 VLOG(1) << "No match for { \"name\": \"" << name << "\", \"type\": \""
791 << type << "\" }";
Austin Schuh0de30f32020-12-06 12:44:28 -0800792 if (original_name != name && !quiet) {
Austin Schuh4b42b252020-10-19 11:35:20 -0700793 LOG(WARNING) << "Remapped from {\"name\": \"" << original_name
794 << "\", \"type\": \"" << type << "\"}, to {\"name\": \""
795 << name << "\", \"type\": \"" << type
796 << "\"}, but no channel by that name exists.";
797 }
Austin Schuhcb108412019-10-13 16:09:54 -0700798 return nullptr;
799 }
800}
801
Austin Schuhc9e10ec2020-01-26 16:08:28 -0800802size_t ChannelIndex(const Configuration *configuration,
803 const Channel *channel) {
804 CHECK(configuration->channels() != nullptr) << ": No channels";
805
Brian Silvermana9698c92021-11-10 12:27:04 -0800806 const auto c = std::lower_bound(
807 configuration->channels()->cbegin(), configuration->channels()->cend(),
808 std::make_pair(channel->name()->string_view(),
809 channel->type()->string_view()),
810 CompareChannels);
811 CHECK(c != configuration->channels()->cend())
812 << ": Channel pointer not found in configuration()->channels()";
813 CHECK(*c == channel)
Austin Schuhc9e10ec2020-01-26 16:08:28 -0800814 << ": Channel pointer not found in configuration()->channels()";
815
Brian Silvermana9698c92021-11-10 12:27:04 -0800816 return std::distance(configuration->channels()->cbegin(), c);
Austin Schuhc9e10ec2020-01-26 16:08:28 -0800817}
818
Austin Schuhbca6cf02019-12-22 17:28:34 -0800819std::string CleanedChannelToString(const Channel *channel) {
820 FlatbufferDetachedBuffer<Channel> cleaned_channel = CopyFlatBuffer(channel);
821 cleaned_channel.mutable_message()->clear_schema();
822 return FlatbufferToJson(cleaned_channel);
823}
824
Austin Schuha81454b2020-05-12 19:58:36 -0700825std::string StrippedChannelToString(const Channel *channel) {
826 return absl::StrCat("{ \"name\": \"", channel->name()->string_view(),
827 "\", \"type\": \"", channel->type()->string_view(),
828 "\" }");
829}
830
Alex Perrycb7da4b2019-08-28 19:35:56 -0700831FlatbufferDetachedBuffer<Configuration> MergeConfiguration(
832 const Flatbuffer<Configuration> &config,
Austin Schuh0de30f32020-12-06 12:44:28 -0800833 const std::vector<aos::FlatbufferVector<reflection::Schema>> &schemas) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700834 flatbuffers::FlatBufferBuilder fbb;
Austin Schuhd7b15da2020-02-17 15:06:11 -0800835 fbb.ForceDefaults(true);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700836
Austin Schuh68d98592020-11-01 23:22:57 -0800837 // Cache for holding already inserted schemas.
838 std::map<std::string_view, flatbuffers::Offset<reflection::Schema>>
839 schema_cache;
840
841 CHECK_EQ(Channel::MiniReflectTypeTable()->num_elems, 13u)
842 << ": Merging logic needs to be updated when the number of channel "
843 "fields changes.";
James Kuszmaul3c998592020-07-27 21:04:47 -0700844
Alex Perrycb7da4b2019-08-28 19:35:56 -0700845 flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<Channel>>>
846 channels_offset;
847 if (config.message().has_channels()) {
848 std::vector<flatbuffers::Offset<Channel>> channel_offsets;
849 for (const Channel *c : *config.message().channels()) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700850 // Search for a schema with a matching type.
Austin Schuh0de30f32020-12-06 12:44:28 -0800851 const aos::FlatbufferVector<reflection::Schema> *found_schema = nullptr;
852 for (const aos::FlatbufferVector<reflection::Schema> &schema : schemas) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700853 if (schema.message().root_table() != nullptr) {
854 if (schema.message().root_table()->name()->string_view() ==
855 c->type()->string_view()) {
856 found_schema = &schema;
857 }
858 }
859 }
860
861 CHECK(found_schema != nullptr)
862 << ": Failed to find schema for " << FlatbufferToJson(c);
863
Austin Schuh68d98592020-11-01 23:22:57 -0800864 // Now copy the message manually.
865 auto cached_schema = schema_cache.find(c->type()->string_view());
866 flatbuffers::Offset<reflection::Schema> schema_offset;
867 if (cached_schema != schema_cache.end()) {
868 schema_offset = cached_schema->second;
869 } else {
Austin Schuha4fc60f2020-11-01 23:06:47 -0800870 schema_offset = RecursiveCopyFlatBuffer<reflection::Schema>(
871 &found_schema->message(), &fbb);
Austin Schuh68d98592020-11-01 23:22:57 -0800872 schema_cache.emplace(c->type()->string_view(), schema_offset);
873 }
874
875 flatbuffers::Offset<flatbuffers::String> name_offset =
876 fbb.CreateSharedString(c->name()->str());
877 flatbuffers::Offset<flatbuffers::String> type_offset =
878 fbb.CreateSharedString(c->type()->str());
879 flatbuffers::Offset<flatbuffers::String> source_node_offset =
Austin Schuha4fc60f2020-11-01 23:06:47 -0800880 c->has_source_node() ? fbb.CreateSharedString(c->source_node()->str())
881 : 0;
Austin Schuh68d98592020-11-01 23:22:57 -0800882
883 flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<Connection>>>
884 destination_nodes_offset =
Austin Schuh5c255aa2020-11-05 18:32:46 -0800885 aos::RecursiveCopyVectorTable(c->destination_nodes(), &fbb);
Austin Schuh68d98592020-11-01 23:22:57 -0800886
887 flatbuffers::Offset<
888 flatbuffers::Vector<flatbuffers::Offset<flatbuffers::String>>>
889 logger_nodes_offset =
890 aos::CopyVectorSharedString(c->logger_nodes(), &fbb);
891
892 Channel::Builder channel_builder(fbb);
893 channel_builder.add_name(name_offset);
894 channel_builder.add_type(type_offset);
895 if (c->has_frequency()) {
896 channel_builder.add_frequency(c->frequency());
897 }
898 if (c->has_max_size()) {
899 channel_builder.add_max_size(c->max_size());
900 }
901 if (c->has_num_senders()) {
902 channel_builder.add_num_senders(c->num_senders());
903 }
904 if (c->has_num_watchers()) {
905 channel_builder.add_num_watchers(c->num_watchers());
906 }
Alex Perrycb7da4b2019-08-28 19:35:56 -0700907 channel_builder.add_schema(schema_offset);
Austin Schuh68d98592020-11-01 23:22:57 -0800908 if (!source_node_offset.IsNull()) {
909 channel_builder.add_source_node(source_node_offset);
910 }
911 if (!destination_nodes_offset.IsNull()) {
912 channel_builder.add_destination_nodes(destination_nodes_offset);
913 }
914 if (c->has_logger()) {
915 channel_builder.add_logger(c->logger());
916 }
917 if (!logger_nodes_offset.IsNull()) {
918 channel_builder.add_logger_nodes(logger_nodes_offset);
919 }
920 if (c->has_read_method()) {
921 channel_builder.add_read_method(c->read_method());
922 }
923 if (c->has_num_readers()) {
924 channel_builder.add_num_readers(c->num_readers());
925 }
926 channel_offsets.emplace_back(channel_builder.Finish());
Alex Perrycb7da4b2019-08-28 19:35:56 -0700927 }
928 channels_offset = fbb.CreateVector(channel_offsets);
929 }
930
Austin Schuh68d98592020-11-01 23:22:57 -0800931 flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<Map>>>
Austin Schuh5c255aa2020-11-05 18:32:46 -0800932 maps_offset =
933 aos::RecursiveCopyVectorTable(config.message().maps(), &fbb);
Austin Schuh68d98592020-11-01 23:22:57 -0800934
935 flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<Node>>>
Austin Schuh5c255aa2020-11-05 18:32:46 -0800936 nodes_offset =
937 aos::RecursiveCopyVectorTable(config.message().nodes(), &fbb);
Austin Schuh68d98592020-11-01 23:22:57 -0800938
939 flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<Application>>>
940 applications_offset =
Austin Schuh5c255aa2020-11-05 18:32:46 -0800941 aos::RecursiveCopyVectorTable(config.message().applications(), &fbb);
Austin Schuh217a9782019-12-21 23:02:50 -0800942
943 // Now insert everything else in unmodified.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700944 ConfigurationBuilder configuration_builder(fbb);
945 if (config.message().has_channels()) {
946 configuration_builder.add_channels(channels_offset);
947 }
Austin Schuh68d98592020-11-01 23:22:57 -0800948 if (!maps_offset.IsNull()) {
949 configuration_builder.add_maps(maps_offset);
950 }
951 if (!nodes_offset.IsNull()) {
952 configuration_builder.add_nodes(nodes_offset);
953 }
954 if (!applications_offset.IsNull()) {
955 configuration_builder.add_applications(applications_offset);
956 }
957
958 if (config.message().has_channel_storage_duration()) {
959 configuration_builder.add_channel_storage_duration(
960 config.message().channel_storage_duration());
961 }
962
963 CHECK_EQ(Configuration::MiniReflectTypeTable()->num_elems, 6u)
964 << ": Merging logic needs to be updated when the number of configuration "
965 "fields changes.";
966
Alex Perrycb7da4b2019-08-28 19:35:56 -0700967 fbb.Finish(configuration_builder.Finish());
James Kuszmaul3c998592020-07-27 21:04:47 -0700968 aos::FlatbufferDetachedBuffer<aos::Configuration> modified_config(
969 fbb.Release());
970
Austin Schuh68d98592020-11-01 23:22:57 -0800971 return modified_config;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700972}
973
Austin Schuh217a9782019-12-21 23:02:50 -0800974const Node *GetNodeFromHostname(const Configuration *config,
975 std::string_view hostname) {
976 for (const Node *node : *config->nodes()) {
Brian Silvermanaa2633f2020-02-17 21:04:14 -0800977 if (node->has_hostname() && node->hostname()->string_view() == hostname) {
Austin Schuh217a9782019-12-21 23:02:50 -0800978 return node;
979 }
Brian Silvermanaa2633f2020-02-17 21:04:14 -0800980 if (node->has_hostnames()) {
981 for (const auto &candidate : *node->hostnames()) {
982 if (candidate->string_view() == hostname) {
983 return node;
984 }
985 }
986 }
Austin Schuh217a9782019-12-21 23:02:50 -0800987 }
988 return nullptr;
989}
Austin Schuhac0771c2020-01-07 18:36:30 -0800990
Austin Schuh217a9782019-12-21 23:02:50 -0800991const Node *GetMyNode(const Configuration *config) {
992 const std::string hostname = (FLAGS_override_hostname.size() > 0)
993 ? FLAGS_override_hostname
994 : network::GetHostname();
995 const Node *node = GetNodeFromHostname(config, hostname);
996 if (node != nullptr) return node;
997
998 LOG(FATAL) << "Unknown node for host: " << hostname
999 << ". Consider using --override_hostname if hostname detection "
1000 "is wrong.";
1001 return nullptr;
1002}
1003
Austin Schuhc9e10ec2020-01-26 16:08:28 -08001004const Node *GetNode(const Configuration *config, const Node *node) {
1005 if (!MultiNode(config)) {
1006 CHECK(node == nullptr) << ": Provided a node in a single node world.";
1007 return nullptr;
1008 } else {
1009 CHECK(node != nullptr);
1010 CHECK(node->has_name());
1011 return GetNode(config, node->name()->string_view());
1012 }
1013}
1014
Austin Schuh217a9782019-12-21 23:02:50 -08001015const Node *GetNode(const Configuration *config, std::string_view name) {
Austin Schuhfd960622020-01-01 13:22:55 -08001016 CHECK(config->has_nodes())
1017 << ": Asking for a node from a single node configuration.";
Austin Schuh217a9782019-12-21 23:02:50 -08001018 for (const Node *node : *config->nodes()) {
Austin Schuhfd960622020-01-01 13:22:55 -08001019 CHECK(node->has_name()) << ": Malformed node " << FlatbufferToJson(node);
Austin Schuh217a9782019-12-21 23:02:50 -08001020 if (node->name()->string_view() == name) {
1021 return node;
1022 }
1023 }
1024 return nullptr;
1025}
1026
Austin Schuh0ca1fd32020-12-18 22:53:05 -08001027const Node *GetNode(const Configuration *config, size_t node_index) {
1028 if (!MultiNode(config)) {
1029 CHECK_EQ(node_index, 0u) << ": Invalid node in a single node world.";
1030 return nullptr;
1031 } else {
1032 CHECK_LT(node_index, config->nodes()->size());
1033 return config->nodes()->Get(node_index);
1034 }
1035}
1036
Austin Schuhc9e10ec2020-01-26 16:08:28 -08001037const Node *GetNodeOrDie(const Configuration *config, const Node *node) {
1038 if (!MultiNode(config)) {
1039 CHECK(node == nullptr) << ": Provided a node in a single node world.";
1040 return nullptr;
1041 } else {
1042 const Node *config_node = GetNode(config, node);
1043 if (config_node == nullptr) {
1044 LOG(FATAL) << "Couldn't find node matching " << FlatbufferToJson(node);
1045 }
1046 return config_node;
1047 }
1048}
1049
Austin Schuh8bd96322020-02-13 21:18:22 -08001050namespace {
1051int GetNodeIndexFromConfig(const Configuration *config, const Node *node) {
Austin Schuhc9e10ec2020-01-26 16:08:28 -08001052 int node_index = 0;
1053 for (const Node *iterated_node : *config->nodes()) {
1054 if (iterated_node == node) {
1055 return node_index;
1056 }
1057 ++node_index;
1058 }
Austin Schuh8bd96322020-02-13 21:18:22 -08001059 return -1;
1060}
1061} // namespace
1062
Austin Schuha9df9ad2021-06-16 14:49:39 -07001063aos::FlatbufferDetachedBuffer<aos::Configuration> AddSchema(
1064 std::string_view json,
1065 const std::vector<aos::FlatbufferVector<reflection::Schema>> &schemas) {
1066 FlatbufferDetachedBuffer<Configuration> addition =
1067 JsonToFlatbuffer(json, Configuration::MiniReflectTypeTable());
1068 return MergeConfiguration(addition, schemas);
1069}
1070
Austin Schuh8bd96322020-02-13 21:18:22 -08001071int GetNodeIndex(const Configuration *config, const Node *node) {
1072 if (!MultiNode(config)) {
1073 return 0;
1074 }
1075
1076 {
1077 int node_index = GetNodeIndexFromConfig(config, node);
1078 if (node_index != -1) {
1079 return node_index;
1080 }
1081 }
1082
1083 const Node *result = GetNode(config, node);
1084 CHECK(result != nullptr);
1085
1086 {
Austin Schuh04408fc2020-02-16 21:48:54 -08001087 int node_index = GetNodeIndexFromConfig(config, result);
Austin Schuh8bd96322020-02-13 21:18:22 -08001088 if (node_index != -1) {
1089 return node_index;
1090 }
1091 }
1092
1093 LOG(FATAL) << "Node " << FlatbufferToJson(node)
1094 << " not found in the configuration.";
Austin Schuhc9e10ec2020-01-26 16:08:28 -08001095}
1096
Austin Schuh04408fc2020-02-16 21:48:54 -08001097int GetNodeIndex(const Configuration *config, std::string_view name) {
1098 if (!MultiNode(config)) {
1099 return 0;
1100 }
1101
1102 {
1103 int node_index = 0;
1104 for (const Node *iterated_node : *config->nodes()) {
1105 if (iterated_node->name()->string_view() == name) {
1106 return node_index;
1107 }
1108 ++node_index;
1109 }
1110 }
1111 LOG(FATAL) << "Node " << name << " not found in the configuration.";
1112}
1113
Austin Schuh681a2472020-12-31 23:55:40 -08001114size_t NodesCount(const Configuration *config) {
1115 if (!MultiNode(config)) {
1116 return 1u;
1117 }
1118
1119 return config->nodes()->size();
1120}
1121
Austin Schuhc9e10ec2020-01-26 16:08:28 -08001122std::vector<const Node *> GetNodes(const Configuration *config) {
1123 std::vector<const Node *> nodes;
Austin Schuh8bd96322020-02-13 21:18:22 -08001124 if (MultiNode(config)) {
Austin Schuhc9e10ec2020-01-26 16:08:28 -08001125 for (const Node *node : *config->nodes()) {
1126 nodes.emplace_back(node);
1127 }
1128 } else {
1129 nodes.emplace_back(nullptr);
1130 }
1131 return nodes;
1132}
1133
Austin Schuh65465332020-11-05 17:36:53 -08001134std::vector<const Node *> GetNodesWithTag(const Configuration *config,
1135 std::string_view tag) {
1136 std::vector<const Node *> nodes;
1137 if (!MultiNode(config)) {
1138 nodes.emplace_back(nullptr);
1139 } else {
1140 for (const Node *node : *config->nodes()) {
1141 if (!node->has_tags()) {
1142 continue;
1143 }
1144 bool did_found_tag = false;
1145 for (const flatbuffers::String *found_tag : *node->tags()) {
1146 if (found_tag->string_view() == tag) {
1147 did_found_tag = true;
1148 break;
1149 }
1150 }
1151 if (did_found_tag) {
1152 nodes.emplace_back(node);
1153 }
1154 }
1155 }
1156 return nodes;
1157}
1158
Brian Silverman631b6262021-11-10 12:25:08 -08001159bool NodeHasTag(const Node *node, std::string_view tag) {
1160 if (node == nullptr) {
1161 return true;
1162 }
1163
1164 const auto *const tags = node->tags();
1165 return std::find_if(tags->begin(), tags->end(),
1166 [tag](const flatbuffers::String *candidate) {
1167 return candidate->string_view() == tag;
1168 }) != tags->end();
1169}
1170
Austin Schuhac0771c2020-01-07 18:36:30 -08001171bool MultiNode(const Configuration *config) { return config->has_nodes(); }
1172
Austin Schuh217a9782019-12-21 23:02:50 -08001173bool ChannelIsSendableOnNode(const Channel *channel, const Node *node) {
Austin Schuhca4828c2019-12-28 14:21:35 -08001174 if (node == nullptr) {
1175 return true;
1176 }
Austin Schuh3c5dae52020-10-06 18:55:18 -07001177 CHECK(channel->has_source_node()) << FlatbufferToJson(channel);
1178 CHECK(node->has_name()) << FlatbufferToJson(node);
Austin Schuh196a4452020-03-15 23:12:03 -07001179 return (CHECK_NOTNULL(channel)->source_node()->string_view() ==
1180 node->name()->string_view());
Austin Schuh217a9782019-12-21 23:02:50 -08001181}
1182
1183bool ChannelIsReadableOnNode(const Channel *channel, const Node *node) {
Austin Schuhca4828c2019-12-28 14:21:35 -08001184 if (node == nullptr) {
1185 return true;
1186 }
1187
Austin Schuh217a9782019-12-21 23:02:50 -08001188 if (channel->source_node()->string_view() == node->name()->string_view()) {
1189 return true;
1190 }
1191
1192 if (!channel->has_destination_nodes()) {
1193 return false;
1194 }
1195
Austin Schuh719946b2019-12-28 14:51:01 -08001196 for (const Connection *connection : *channel->destination_nodes()) {
1197 CHECK(connection->has_name());
1198 if (connection->name()->string_view() == node->name()->string_view()) {
Austin Schuh217a9782019-12-21 23:02:50 -08001199 return true;
1200 }
1201 }
1202
1203 return false;
1204}
1205
Austin Schuh719946b2019-12-28 14:51:01 -08001206bool ChannelMessageIsLoggedOnNode(const Channel *channel, const Node *node) {
Austin Schuh48e94502021-06-18 18:35:53 -07001207 if (node == nullptr) {
Austin Schuh2bb80e02021-03-20 21:46:17 -07001208 // Single node world. If there is a local logger, then we want to use
1209 // it.
Austin Schuh48e94502021-06-18 18:35:53 -07001210 if (channel->logger() == LoggerConfig::LOCAL_LOGGER) {
1211 return true;
1212 } else if (channel->logger() == LoggerConfig::NOT_LOGGED) {
1213 return false;
1214 }
1215 LOG(FATAL) << "Unsupported logging configuration in a single node world: "
1216 << CleanedChannelToString(channel);
Austin Schuh2bb80e02021-03-20 21:46:17 -07001217 }
1218 return ChannelMessageIsLoggedOnNode(
1219 channel, CHECK_NOTNULL(node)->name()->string_view());
1220}
1221
1222bool ChannelMessageIsLoggedOnNode(const Channel *channel,
1223 std::string_view node_name) {
Austin Schuhf1fff282020-03-28 16:57:32 -07001224 switch (channel->logger()) {
Austin Schuh719946b2019-12-28 14:51:01 -08001225 case LoggerConfig::LOCAL_LOGGER:
Austin Schuh2bb80e02021-03-20 21:46:17 -07001226 return channel->source_node()->string_view() == node_name;
Austin Schuh719946b2019-12-28 14:51:01 -08001227 case LoggerConfig::LOCAL_AND_REMOTE_LOGGER:
Austin Schuhda40e472020-03-28 15:15:29 -07001228 CHECK(channel->has_logger_nodes());
1229 CHECK_GT(channel->logger_nodes()->size(), 0u);
Austin Schuh719946b2019-12-28 14:51:01 -08001230
Austin Schuh2bb80e02021-03-20 21:46:17 -07001231 if (channel->source_node()->string_view() == node_name) {
Austin Schuh719946b2019-12-28 14:51:01 -08001232 return true;
1233 }
Austin Schuhda40e472020-03-28 15:15:29 -07001234
1235 [[fallthrough]];
1236 case LoggerConfig::REMOTE_LOGGER:
1237 CHECK(channel->has_logger_nodes());
1238 CHECK_GT(channel->logger_nodes()->size(), 0u);
1239 for (const flatbuffers::String *logger_node : *channel->logger_nodes()) {
Austin Schuh2bb80e02021-03-20 21:46:17 -07001240 if (logger_node->string_view() == node_name) {
Austin Schuhda40e472020-03-28 15:15:29 -07001241 return true;
1242 }
Austin Schuh719946b2019-12-28 14:51:01 -08001243 }
1244
1245 return false;
1246 case LoggerConfig::NOT_LOGGED:
1247 return false;
1248 }
1249
1250 LOG(FATAL) << "Unknown logger config " << static_cast<int>(channel->logger());
1251}
1252
Austin Schuh58646e22021-08-23 23:51:46 -07001253size_t ConnectionCount(const Channel *channel) {
1254 if (!channel->has_destination_nodes()) {
1255 return 0;
1256 }
1257 return channel->destination_nodes()->size();
1258}
1259
Austin Schuh719946b2019-12-28 14:51:01 -08001260const Connection *ConnectionToNode(const Channel *channel, const Node *node) {
1261 if (!channel->has_destination_nodes()) {
1262 return nullptr;
1263 }
1264 for (const Connection *connection : *channel->destination_nodes()) {
1265 if (connection->name()->string_view() == node->name()->string_view()) {
1266 return connection;
1267 }
1268 }
1269 return nullptr;
1270}
1271
1272bool ConnectionDeliveryTimeIsLoggedOnNode(const Channel *channel,
1273 const Node *node,
1274 const Node *logger_node) {
Austin Schuh72211ae2021-08-05 14:02:30 -07001275 return ConnectionDeliveryTimeIsLoggedOnNode(ConnectionToNode(channel, node),
1276 logger_node);
Austin Schuh719946b2019-12-28 14:51:01 -08001277}
1278
1279bool ConnectionDeliveryTimeIsLoggedOnNode(const Connection *connection,
1280 const Node *node) {
Austin Schuh72211ae2021-08-05 14:02:30 -07001281 if (connection == nullptr) {
1282 return false;
1283 }
Austin Schuh719946b2019-12-28 14:51:01 -08001284 switch (connection->timestamp_logger()) {
1285 case LoggerConfig::LOCAL_AND_REMOTE_LOGGER:
Austin Schuhda40e472020-03-28 15:15:29 -07001286 CHECK(connection->has_timestamp_logger_nodes());
1287 CHECK_GT(connection->timestamp_logger_nodes()->size(), 0u);
Austin Schuh719946b2019-12-28 14:51:01 -08001288 if (connection->name()->string_view() == node->name()->string_view()) {
1289 return true;
1290 }
1291
Austin Schuhda40e472020-03-28 15:15:29 -07001292 [[fallthrough]];
1293 case LoggerConfig::REMOTE_LOGGER:
1294 CHECK(connection->has_timestamp_logger_nodes());
1295 CHECK_GT(connection->timestamp_logger_nodes()->size(), 0u);
1296 for (const flatbuffers::String *timestamp_logger_node :
1297 *connection->timestamp_logger_nodes()) {
1298 if (timestamp_logger_node->string_view() ==
1299 node->name()->string_view()) {
1300 return true;
1301 }
Austin Schuh719946b2019-12-28 14:51:01 -08001302 }
1303
1304 return false;
1305 case LoggerConfig::LOCAL_LOGGER:
1306 return connection->name()->string_view() == node->name()->string_view();
Austin Schuh719946b2019-12-28 14:51:01 -08001307 case LoggerConfig::NOT_LOGGED:
1308 return false;
1309 }
1310
1311 LOG(FATAL) << "Unknown logger config "
1312 << static_cast<int>(connection->timestamp_logger());
1313}
1314
Austin Schuhe84c3ed2019-12-14 15:29:48 -08001315std::vector<std::string_view> SourceNodeNames(const Configuration *config,
1316 const Node *my_node) {
1317 std::set<std::string_view> result_set;
1318
1319 for (const Channel *channel : *config->channels()) {
1320 if (channel->has_destination_nodes()) {
1321 for (const Connection *connection : *channel->destination_nodes()) {
1322 if (connection->name()->string_view() ==
1323 my_node->name()->string_view()) {
1324 result_set.insert(channel->source_node()->string_view());
1325 }
1326 }
1327 }
1328 }
1329
1330 std::vector<std::string_view> result;
1331 for (const std::string_view source : result_set) {
1332 VLOG(1) << "Found a source node of " << source;
1333 result.emplace_back(source);
1334 }
1335 return result;
1336}
1337
1338std::vector<std::string_view> DestinationNodeNames(const Configuration *config,
1339 const Node *my_node) {
1340 std::vector<std::string_view> result;
1341
1342 for (const Channel *channel : *config->channels()) {
1343 if (channel->has_source_node() && channel->source_node()->string_view() ==
1344 my_node->name()->string_view()) {
1345 if (!channel->has_destination_nodes()) continue;
1346
1347 if (channel->source_node()->string_view() !=
1348 my_node->name()->string_view()) {
1349 continue;
1350 }
1351
1352 for (const Connection *connection : *channel->destination_nodes()) {
1353 if (std::find(result.begin(), result.end(),
1354 connection->name()->string_view()) == result.end()) {
1355 result.emplace_back(connection->name()->string_view());
1356 }
1357 }
1358 }
1359 }
1360
1361 for (const std::string_view destination : result) {
1362 VLOG(1) << "Found a destination node of " << destination;
1363 }
1364 return result;
1365}
1366
Austin Schuh8d7e0bb2020-10-02 17:57:00 -07001367std::vector<const Node *> TimestampNodes(const Configuration *config,
1368 const Node *my_node) {
1369 if (!configuration::MultiNode(config)) {
1370 CHECK(my_node == nullptr);
1371 return std::vector<const Node *>{};
1372 }
1373
1374 std::set<const Node *> timestamp_logger_nodes;
1375 for (const Channel *channel : *config->channels()) {
1376 if (!configuration::ChannelIsSendableOnNode(channel, my_node)) {
1377 continue;
1378 }
1379 if (!channel->has_destination_nodes()) {
1380 continue;
1381 }
1382 for (const Connection *connection : *channel->destination_nodes()) {
1383 const Node *other_node =
1384 configuration::GetNode(config, connection->name()->string_view());
1385
1386 if (configuration::ConnectionDeliveryTimeIsLoggedOnNode(connection,
1387 my_node)) {
1388 VLOG(1) << "Timestamps are logged from "
1389 << FlatbufferToJson(other_node);
1390 timestamp_logger_nodes.insert(other_node);
1391 }
1392 }
1393 }
1394
1395 std::vector<const Node *> result;
1396 for (const Node *node : timestamp_logger_nodes) {
1397 result.emplace_back(node);
1398 }
1399 return result;
1400}
1401
Austin Schuhc41fa3c2021-10-16 14:35:35 -07001402bool ApplicationShouldStart(const Configuration *config, const Node *my_node,
1403 const Application *application) {
1404 if (MultiNode(config)) {
1405 // Ok, we need
1406 CHECK(application->has_nodes());
1407 CHECK(my_node != nullptr);
1408 for (const flatbuffers::String *str : *application->nodes()) {
1409 if (str->string_view() == my_node->name()->string_view()) {
1410 return true;
1411 }
1412 }
1413 return false;
1414 } else {
1415 return true;
1416 }
1417}
1418
Austin Schuhd2e2f6a2021-02-07 20:46:16 -08001419const Application *GetApplication(const Configuration *config,
1420 const Node *my_node,
1421 std::string_view application_name) {
1422 if (config->has_applications()) {
1423 auto application_iterator = std::lower_bound(
1424 config->applications()->cbegin(), config->applications()->cend(),
1425 application_name, CompareApplications);
1426 if (application_iterator != config->applications()->cend() &&
1427 EqualsApplications(*application_iterator, application_name)) {
Austin Schuhc41fa3c2021-10-16 14:35:35 -07001428 if (ApplicationShouldStart(config, my_node, *application_iterator)) {
Austin Schuhd2e2f6a2021-02-07 20:46:16 -08001429 return *application_iterator;
1430 }
1431 }
1432 }
1433 return nullptr;
1434}
1435
Austin Schuhfc7b6a02021-07-12 21:19:07 -07001436std::vector<size_t> SourceNodeIndex(const Configuration *config) {
1437 CHECK(config->has_channels());
1438 std::vector<size_t> result;
1439 result.resize(config->channels()->size(), 0u);
1440 if (MultiNode(config)) {
1441 for (size_t i = 0; i < config->channels()->size(); ++i) {
1442 result[i] = GetNodeIndex(
1443 config, config->channels()->Get(i)->source_node()->string_view());
1444 }
1445 }
1446 return result;
1447}
1448
Brian Silverman66f079a2013-08-26 16:24:30 -07001449} // namespace configuration
1450} // namespace aos