blob: f08cf99ff6a4298260464f4a645950917a9b6da8 [file] [log] [blame]
John Park398c74a2018-10-20 21:17:39 -07001#include "aos/configuration.h"
Brian Silverman66f079a2013-08-26 16:24:30 -07002
3#include <string.h>
Brian Silverman66f079a2013-08-26 16:24:30 -07004#include <stdlib.h>
5#include <sys/types.h>
6#include <netinet/in.h>
7#include <arpa/inet.h>
8#include <ifaddrs.h>
9#include <unistd.h>
James Kuszmaul3ae42262019-11-08 12:33:41 -080010#include <string_view>
Brian Silverman66f079a2013-08-26 16:24:30 -070011
Austin Schuh217a9782019-12-21 23:02:50 -080012#include "absl/base/call_once.h"
Austin Schuhcb108412019-10-13 16:09:54 -070013#include "absl/container/btree_set.h"
Austin Schuhcb108412019-10-13 16:09:54 -070014#include "aos/configuration_generated.h"
15#include "aos/flatbuffer_merge.h"
16#include "aos/json_to_flatbuffer.h"
Austin Schuh217a9782019-12-21 23:02:50 -080017#include "aos/network/team_number.h"
Austin Schuhcb108412019-10-13 16:09:54 -070018#include "aos/unique_malloc_ptr.h"
19#include "aos/util/file.h"
Austin Schuh217a9782019-12-21 23:02:50 -080020#include "gflags/gflags.h"
Austin Schuhcb108412019-10-13 16:09:54 -070021#include "glog/logging.h"
Austin Schuh217a9782019-12-21 23:02:50 -080022
23DEFINE_string(
24 override_hostname, "",
25 "If set, this forces the hostname of this node to be the provided "
26 "hostname.");
Brian Silverman66f079a2013-08-26 16:24:30 -070027
28namespace aos {
Austin Schuhcb108412019-10-13 16:09:54 -070029
Austin Schuh40485ed2019-10-26 21:51:44 -070030// Define the compare and equal operators for Channel and Application so we can
Austin Schuhcb108412019-10-13 16:09:54 -070031// insert them in the btree below.
Austin Schuh40485ed2019-10-26 21:51:44 -070032bool operator<(const FlatbufferDetachedBuffer<Channel> &lhs,
33 const FlatbufferDetachedBuffer<Channel> &rhs) {
Austin Schuhcb108412019-10-13 16:09:54 -070034 int name_compare = lhs.message().name()->string_view().compare(
35 rhs.message().name()->string_view());
36 if (name_compare == 0) {
37 return lhs.message().type()->string_view() <
38 rhs.message().type()->string_view();
39 } else if (name_compare < 0) {
40 return true;
41 } else {
42 return false;
43 }
44}
45
Austin Schuh40485ed2019-10-26 21:51:44 -070046bool operator==(const FlatbufferDetachedBuffer<Channel> &lhs,
47 const FlatbufferDetachedBuffer<Channel> &rhs) {
Austin Schuhcb108412019-10-13 16:09:54 -070048 return lhs.message().name()->string_view() ==
49 rhs.message().name()->string_view() &&
50 lhs.message().type()->string_view() ==
51 rhs.message().type()->string_view();
52}
53
Austin Schuh40485ed2019-10-26 21:51:44 -070054bool operator==(const FlatbufferDetachedBuffer<Application> &lhs,
55 const FlatbufferDetachedBuffer<Application> &rhs) {
Austin Schuhcb108412019-10-13 16:09:54 -070056 return lhs.message().name()->string_view() ==
57 rhs.message().name()->string_view();
58}
59
Austin Schuh40485ed2019-10-26 21:51:44 -070060bool operator<(const FlatbufferDetachedBuffer<Application> &lhs,
61 const FlatbufferDetachedBuffer<Application> &rhs) {
Austin Schuhcb108412019-10-13 16:09:54 -070062 return lhs.message().name()->string_view() <
63 rhs.message().name()->string_view();
64}
65
Austin Schuh217a9782019-12-21 23:02:50 -080066bool operator==(const FlatbufferDetachedBuffer<Node> &lhs,
67 const FlatbufferDetachedBuffer<Node> &rhs) {
68 return lhs.message().name()->string_view() ==
69 rhs.message().name()->string_view();
70}
71
72bool operator<(const FlatbufferDetachedBuffer<Node> &lhs,
73 const FlatbufferDetachedBuffer<Node> &rhs) {
74 return lhs.message().name()->string_view() <
75 rhs.message().name()->string_view();
76}
77
Brian Silverman66f079a2013-08-26 16:24:30 -070078namespace configuration {
79namespace {
80
John Park7bca9812019-10-14 21:23:45 -070081void DoGetRootDirectory(char** retu) {
Brian Silverman66f079a2013-08-26 16:24:30 -070082 ssize_t size = 0;
John Park7bca9812019-10-14 21:23:45 -070083 *retu = NULL;
Brian Silverman66f079a2013-08-26 16:24:30 -070084 while (true) {
Brian Silverman66f079a2013-08-26 16:24:30 -070085 size += 256;
John Park7bca9812019-10-14 21:23:45 -070086 if (*retu != nullptr) delete *retu;
87 *retu = new char[size];
Brian Silverman66f079a2013-08-26 16:24:30 -070088
John Park7bca9812019-10-14 21:23:45 -070089 ssize_t ret = readlink("/proc/self/exe", *retu, size);
Brian Silverman66f079a2013-08-26 16:24:30 -070090 if (ret < 0) {
91 if (ret != -1) {
Austin Schuhcb108412019-10-13 16:09:54 -070092 LOG(WARNING) << "it returned " << ret << ", not -1";
Brian Silverman66f079a2013-08-26 16:24:30 -070093 }
John Park7bca9812019-10-14 21:23:45 -070094
95 PLOG(FATAL) << "readlink(\"/proc/self/exe\", " << *retu << ", " << size
Austin Schuhcb108412019-10-13 16:09:54 -070096 << ") failed";
Brian Silverman66f079a2013-08-26 16:24:30 -070097 }
98 if (ret < size) {
John Park7bca9812019-10-14 21:23:45 -070099 void *last_slash = memrchr(*retu, '/', ret);
Brian Silverman66f079a2013-08-26 16:24:30 -0700100 if (last_slash == NULL) {
John Park7bca9812019-10-14 21:23:45 -0700101 *retu[ret] = '\0';
102 LOG(FATAL) << "couldn't find a '/' in \"" << *retu << "\"";
Brian Silverman66f079a2013-08-26 16:24:30 -0700103 }
104 *static_cast<char *>(last_slash) = '\0';
John Park7bca9812019-10-14 21:23:45 -0700105 LOG(INFO) << "got a root dir of \"" << *retu << "\"";
106 return;
Brian Silverman66f079a2013-08-26 16:24:30 -0700107 }
108 }
109}
110
John Park7bca9812019-10-14 21:23:45 -0700111void DoGetLoggingDirectory(char** retu) {
Brian Silverman66f079a2013-08-26 16:24:30 -0700112 static const char kSuffix[] = "/../../tmp/robot_logs";
113 const char *root = GetRootDirectory();
John Park7bca9812019-10-14 21:23:45 -0700114 *retu = new char[strlen(root) + sizeof(kSuffix)];
115 strcpy(*retu, root);
116 strcat(*retu, kSuffix);
Brian Silverman66f079a2013-08-26 16:24:30 -0700117}
118
Austin Schuhcb108412019-10-13 16:09:54 -0700119// Extracts the folder part of a path. Returns ./ if there is no path.
James Kuszmaul3ae42262019-11-08 12:33:41 -0800120std::string_view ExtractFolder(
121 const std::string_view filename) {
Austin Schuhcb108412019-10-13 16:09:54 -0700122 auto last_slash_pos = filename.find_last_of("/\\");
123
James Kuszmaul3ae42262019-11-08 12:33:41 -0800124 return last_slash_pos == std::string_view::npos
125 ? std::string_view("./")
Austin Schuhcb108412019-10-13 16:09:54 -0700126 : filename.substr(0, last_slash_pos + 1);
127}
128
Austin Schuh40485ed2019-10-26 21:51:44 -0700129FlatbufferDetachedBuffer<Configuration> ReadConfig(
James Kuszmaul3ae42262019-11-08 12:33:41 -0800130 const std::string_view path, absl::btree_set<std::string> *visited_paths) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700131 flatbuffers::DetachedBuffer buffer = JsonToFlatbuffer(
132 util::ReadFileToStringOrDie(path), ConfigurationTypeTable());
133
134 CHECK_GT(buffer.size(), 0u) << ": Failed to parse JSON file";
135
136 FlatbufferDetachedBuffer<Configuration> config(std::move(buffer));
Austin Schuhcb108412019-10-13 16:09:54 -0700137 // Depth first. Take the following example:
138 //
139 // config1.json:
140 // {
Austin Schuh40485ed2019-10-26 21:51:44 -0700141 // "channels": [
Austin Schuhcb108412019-10-13 16:09:54 -0700142 // {
143 // "name": "/foo",
144 // "type": ".aos.bar",
145 // "max_size": 5
146 // }
147 // ],
148 // "imports": [
149 // "config2.json",
150 // ]
151 // }
152 //
153 // config2.json:
154 // {
Austin Schuh40485ed2019-10-26 21:51:44 -0700155 // "channels": [
Austin Schuhcb108412019-10-13 16:09:54 -0700156 // {
157 // "name": "/foo",
158 // "type": ".aos.bar",
159 // "max_size": 7
160 // }
161 // ],
162 // }
163 //
164 // We want the main config (config1.json) to be able to override the imported
165 // config. That means that it needs to be merged into the imported configs,
166 // not the other way around.
167
168 // Track that we have seen this file before recursing.
169 visited_paths->insert(::std::string(path));
170
171 if (config.message().has_imports()) {
172 // Capture the imports.
173 const flatbuffers::Vector<flatbuffers::Offset<flatbuffers::String>> *v =
174 config.message().imports();
175
176 // And then wipe them. This gets GCed when we merge later.
177 config.mutable_message()->clear_imports();
178
179 // Start with an empty configuration to merge into.
Austin Schuh40485ed2019-10-26 21:51:44 -0700180 FlatbufferDetachedBuffer<Configuration> merged_config =
181 FlatbufferDetachedBuffer<Configuration>::Empty();
Austin Schuhcb108412019-10-13 16:09:54 -0700182
183 const ::std::string folder(ExtractFolder(path));
184
185 for (const flatbuffers::String *str : *v) {
186 const ::std::string included_config = folder + str->c_str();
187 // Abort on any paths we have already seen.
188 CHECK(visited_paths->find(included_config) == visited_paths->end())
189 << ": Found duplicate file " << included_config << " while reading "
190 << path;
191
192 // And them merge everything in.
193 merged_config = MergeFlatBuffers(
194 merged_config, ReadConfig(included_config, visited_paths));
195 }
196
197 // Finally, merge this file in.
198 config = MergeFlatBuffers(merged_config, config);
199 }
200 return config;
201}
202
Alex Perrycb7da4b2019-08-28 19:35:56 -0700203// Compares (c < p) a channel, and a name, type tuple.
204bool CompareChannels(const Channel *c,
James Kuszmaul3ae42262019-11-08 12:33:41 -0800205 ::std::pair<std::string_view, std::string_view> p) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700206 int name_compare = c->name()->string_view().compare(p.first);
207 if (name_compare == 0) {
208 return c->type()->string_view() < p.second;
209 } else if (name_compare < 0) {
210 return true;
211 } else {
212 return false;
213 }
214};
215
216// Compares for equality (c == p) a channel, and a name, type tuple.
217bool EqualsChannels(const Channel *c,
James Kuszmaul3ae42262019-11-08 12:33:41 -0800218 ::std::pair<std::string_view, std::string_view> p) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700219 return c->name()->string_view() == p.first &&
220 c->type()->string_view() == p.second;
221}
222
223// Compares (c < p) an application, and a name;
James Kuszmaul3ae42262019-11-08 12:33:41 -0800224bool CompareApplications(const Application *a, std::string_view name) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700225 return a->name()->string_view() < name;
226};
227
228// Compares for equality (c == p) an application, and a name;
James Kuszmaul3ae42262019-11-08 12:33:41 -0800229bool EqualsApplications(const Application *a, std::string_view name) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700230 return a->name()->string_view() == name;
231}
232
233// Maps name for the provided maps. Modifies name.
234void HandleMaps(const flatbuffers::Vector<flatbuffers::Offset<aos::Map>> *maps,
James Kuszmaul3ae42262019-11-08 12:33:41 -0800235 std::string_view *name) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700236 // For the same reason we merge configs in reverse order, we want to process
237 // maps in reverse order. That lets the outer config overwrite channels from
238 // the inner configs.
239 for (auto i = maps->rbegin(); i != maps->rend(); ++i) {
240 if (i->has_match() && i->match()->has_name() && i->has_rename() &&
241 i->rename()->has_name() && i->match()->name()->string_view() == *name) {
242 VLOG(1) << "Renamed \"" << *name << "\" to \""
243 << i->rename()->name()->string_view() << "\"";
244 *name = i->rename()->name()->string_view();
245 }
246 }
247}
248
249} // namespace
250
Austin Schuh40485ed2019-10-26 21:51:44 -0700251FlatbufferDetachedBuffer<Configuration> MergeConfiguration(
Austin Schuhcb108412019-10-13 16:09:54 -0700252 const Flatbuffer<Configuration> &config) {
Austin Schuh40485ed2019-10-26 21:51:44 -0700253 // Store all the channels in a sorted set. This lets us track channels we
Austin Schuhcb108412019-10-13 16:09:54 -0700254 // have seen before and merge the updates in.
Austin Schuh40485ed2019-10-26 21:51:44 -0700255 absl::btree_set<FlatbufferDetachedBuffer<Channel>> channels;
Austin Schuhcb108412019-10-13 16:09:54 -0700256
Austin Schuh40485ed2019-10-26 21:51:44 -0700257 if (config.message().has_channels()) {
258 for (const Channel *c : *config.message().channels()) {
Austin Schuhcb108412019-10-13 16:09:54 -0700259 // Ignore malformed entries.
Austin Schuh40485ed2019-10-26 21:51:44 -0700260 if (!c->has_name()) {
Austin Schuhcb108412019-10-13 16:09:54 -0700261 continue;
262 }
Austin Schuh40485ed2019-10-26 21:51:44 -0700263 if (!c->has_type()) {
Austin Schuhcb108412019-10-13 16:09:54 -0700264 continue;
265 }
266
Austin Schuh40485ed2019-10-26 21:51:44 -0700267 // Attempt to insert the channel.
268 auto result = channels.insert(CopyFlatBuffer(c));
Austin Schuhcb108412019-10-13 16:09:54 -0700269 if (!result.second) {
270 // Already there, so merge the new table into the original.
Austin Schuh40485ed2019-10-26 21:51:44 -0700271 *result.first = MergeFlatBuffers(*result.first, CopyFlatBuffer(c));
Austin Schuhcb108412019-10-13 16:09:54 -0700272 }
273 }
274 }
275
276 // Now repeat this for the application list.
Austin Schuh40485ed2019-10-26 21:51:44 -0700277 absl::btree_set<FlatbufferDetachedBuffer<Application>> applications;
Austin Schuhcb108412019-10-13 16:09:54 -0700278 if (config.message().has_applications()) {
279 for (const Application *a : *config.message().applications()) {
280 if (!a->has_name()) {
281 continue;
282 }
283
284 auto result = applications.insert(CopyFlatBuffer(a));
285 if (!result.second) {
286 *result.first = MergeFlatBuffers(*result.first, CopyFlatBuffer(a));
287 }
288 }
289 }
290
Austin Schuh217a9782019-12-21 23:02:50 -0800291 // Now repeat this for the node list.
292 absl::btree_set<FlatbufferDetachedBuffer<Node>> nodes;
293 if (config.message().has_nodes()) {
294 for (const Node *n : *config.message().nodes()) {
295 if (!n->has_name()) {
296 continue;
297 }
298
299 auto result = nodes.insert(CopyFlatBuffer(n));
300 if (!result.second) {
301 *result.first = MergeFlatBuffers(*result.first, CopyFlatBuffer(n));
302 }
303 }
304 }
305
Austin Schuhcb108412019-10-13 16:09:54 -0700306 flatbuffers::FlatBufferBuilder fbb;
307 fbb.ForceDefaults(1);
308
309 // Start by building the vectors. They need to come before the final table.
Austin Schuh40485ed2019-10-26 21:51:44 -0700310 // Channels
311 flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<Channel>>>
312 channels_offset;
Austin Schuhcb108412019-10-13 16:09:54 -0700313 {
Austin Schuh40485ed2019-10-26 21:51:44 -0700314 ::std::vector<flatbuffers::Offset<Channel>> channel_offsets;
315 for (const FlatbufferDetachedBuffer<Channel> &c : channels) {
316 channel_offsets.emplace_back(
317 CopyFlatBuffer<Channel>(&c.message(), &fbb));
Austin Schuhcb108412019-10-13 16:09:54 -0700318 }
Austin Schuh40485ed2019-10-26 21:51:44 -0700319 channels_offset = fbb.CreateVector(channel_offsets);
Austin Schuhcb108412019-10-13 16:09:54 -0700320 }
321
322 // Applications
323 flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<Application>>>
324 applications_offset;
325 {
326 ::std::vector<flatbuffers::Offset<Application>> applications_offsets;
Austin Schuh40485ed2019-10-26 21:51:44 -0700327 for (const FlatbufferDetachedBuffer<Application> &a : applications) {
Austin Schuhcb108412019-10-13 16:09:54 -0700328 applications_offsets.emplace_back(
329 CopyFlatBuffer<Application>(&a.message(), &fbb));
330 }
331 applications_offset = fbb.CreateVector(applications_offsets);
332 }
333
334 // Just copy the maps
335 flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<Map>>>
336 maps_offset;
337 {
338 ::std::vector<flatbuffers::Offset<Map>> map_offsets;
339 if (config.message().has_maps()) {
340 for (const Map *m : *config.message().maps()) {
341 map_offsets.emplace_back(CopyFlatBuffer<Map>(m, &fbb));
342 }
343 maps_offset = fbb.CreateVector(map_offsets);
344 }
345 }
346
Austin Schuh217a9782019-12-21 23:02:50 -0800347 // Nodes
348 flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<Node>>>
349 nodes_offset;
350 {
351 ::std::vector<flatbuffers::Offset<Node>> node_offsets;
352 for (const FlatbufferDetachedBuffer<Node> &n : nodes) {
353 node_offsets.emplace_back(CopyFlatBuffer<Node>(&n.message(), &fbb));
354 }
355 nodes_offset = fbb.CreateVector(node_offsets);
356 }
357
Austin Schuhcb108412019-10-13 16:09:54 -0700358 // And then build a Configuration with them all.
359 ConfigurationBuilder configuration_builder(fbb);
Austin Schuh40485ed2019-10-26 21:51:44 -0700360 configuration_builder.add_channels(channels_offset);
Austin Schuhcb108412019-10-13 16:09:54 -0700361 if (config.message().has_maps()) {
362 configuration_builder.add_maps(maps_offset);
363 }
Austin Schuh217a9782019-12-21 23:02:50 -0800364 if (config.message().has_applications()) {
365 configuration_builder.add_applications(applications_offset);
366 }
367 if (config.message().has_nodes()) {
368 configuration_builder.add_nodes(nodes_offset);
369 }
Austin Schuhcb108412019-10-13 16:09:54 -0700370
371 fbb.Finish(configuration_builder.Finish());
Austin Schuh217a9782019-12-21 23:02:50 -0800372
373 // Now, validate that if there is a node list, every channel has a source
374 // node.
375 FlatbufferDetachedBuffer<Configuration> result(fbb.Release());
376
377 // Check that if there is a node list, all the source nodes are filled out and
378 // valid, and all the destination nodes are valid (and not the source). This
379 // is a basic consistency check.
380 if (result.message().has_nodes()) {
381 for (const Channel *c : *config.message().channels()) {
382 CHECK(c->has_source_node()) << ": Channel " << FlatbufferToJson(c)
383 << " is missing \"source_node\"";
384 CHECK(GetNode(&result.message(), c->source_node()->string_view()) !=
385 nullptr)
386 << ": Channel " << FlatbufferToJson(c)
387 << " has an unknown \"source_node\"";
388
389 if (c->has_destination_nodes()) {
390 for (const flatbuffers::String *n : *c->destination_nodes()) {
391 CHECK(GetNode(&result.message(), n->string_view()) != nullptr)
392 << ": Channel " << FlatbufferToJson(c)
393 << " has an unknown \"destination_nodes\" " << n->string_view();
394
395 CHECK_NE(n->string_view(), c->source_node()->string_view())
396 << ": Channel " << FlatbufferToJson(c)
397 << " is forwarding data to itself";
398 }
399 }
400 }
401 }
402
403 return result;
Austin Schuhcb108412019-10-13 16:09:54 -0700404}
405
Brian Silverman66f079a2013-08-26 16:24:30 -0700406const char *GetRootDirectory() {
Austin Schuh217a9782019-12-21 23:02:50 -0800407 static char *root_dir; // return value
John Park7bca9812019-10-14 21:23:45 -0700408 static absl::once_flag once_;
409 absl::call_once(once_, DoGetRootDirectory, &root_dir);
410 return root_dir;
Brian Silverman66f079a2013-08-26 16:24:30 -0700411}
412
413const char *GetLoggingDirectory() {
Austin Schuh217a9782019-12-21 23:02:50 -0800414 static char *retu; // return value
John Park7bca9812019-10-14 21:23:45 -0700415 static absl::once_flag once_;
416 absl::call_once(once_, DoGetLoggingDirectory, &retu);
417 return retu;
Brian Silverman66f079a2013-08-26 16:24:30 -0700418}
419
Austin Schuh40485ed2019-10-26 21:51:44 -0700420FlatbufferDetachedBuffer<Configuration> ReadConfig(
James Kuszmaul3ae42262019-11-08 12:33:41 -0800421 const std::string_view path) {
Austin Schuhcb108412019-10-13 16:09:54 -0700422 // We only want to read a file once. So track the visited files in a set.
423 absl::btree_set<std::string> visited_paths;
424 return MergeConfiguration(ReadConfig(path, &visited_paths));
425}
426
James Kuszmaul3ae42262019-11-08 12:33:41 -0800427const Channel *GetChannel(const Configuration *config, std::string_view name,
428 std::string_view type,
429 std::string_view application_name) {
Austin Schuhcb108412019-10-13 16:09:54 -0700430 VLOG(1) << "Looking up { \"name\": \"" << name << "\", \"type\": \"" << type
431 << "\" }";
432
433 // First handle application specific maps. Only do this if we have a matching
434 // application name, and it has maps.
Austin Schuh40485ed2019-10-26 21:51:44 -0700435 if (config->has_applications()) {
436 auto application_iterator = std::lower_bound(
437 config->applications()->cbegin(), config->applications()->cend(),
438 application_name, CompareApplications);
439 if (application_iterator != config->applications()->cend() &&
Austin Schuhcb108412019-10-13 16:09:54 -0700440 EqualsApplications(*application_iterator, application_name)) {
441 if (application_iterator->has_maps()) {
442 HandleMaps(application_iterator->maps(), &name);
443 }
444 }
445 }
446
447 // Now do global maps.
Austin Schuh40485ed2019-10-26 21:51:44 -0700448 if (config->has_maps()) {
449 HandleMaps(config->maps(), &name);
Austin Schuhcb108412019-10-13 16:09:54 -0700450 }
451
Austin Schuh217a9782019-12-21 23:02:50 -0800452 VLOG(1) << "Actually looking up { \"name\": \"" << name << "\", \"type\": \""
Alex Perrycb7da4b2019-08-28 19:35:56 -0700453 << type << "\" }";
454
Austin Schuh40485ed2019-10-26 21:51:44 -0700455 // Then look for the channel.
456 auto channel_iterator =
457 std::lower_bound(config->channels()->cbegin(),
458 config->channels()->cend(),
459 std::make_pair(name, type), CompareChannels);
Austin Schuhcb108412019-10-13 16:09:54 -0700460
461 // Make sure we actually found it, and it matches.
Austin Schuh40485ed2019-10-26 21:51:44 -0700462 if (channel_iterator != config->channels()->cend() &&
463 EqualsChannels(*channel_iterator, std::make_pair(name, type))) {
464 VLOG(1) << "Found: " << FlatbufferToJson(*channel_iterator);
465 return *channel_iterator;
Austin Schuhcb108412019-10-13 16:09:54 -0700466 } else {
467 VLOG(1) << "No match for { \"name\": \"" << name << "\", \"type\": \""
468 << type << "\" }";
469 return nullptr;
470 }
471}
472
Alex Perrycb7da4b2019-08-28 19:35:56 -0700473FlatbufferDetachedBuffer<Configuration> MergeConfiguration(
474 const Flatbuffer<Configuration> &config,
475 const std::vector<aos::FlatbufferString<reflection::Schema>> &schemas) {
476 flatbuffers::FlatBufferBuilder fbb;
477 fbb.ForceDefaults(1);
478
479 flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<Channel>>>
480 channels_offset;
481 if (config.message().has_channels()) {
482 std::vector<flatbuffers::Offset<Channel>> channel_offsets;
483 for (const Channel *c : *config.message().channels()) {
484 flatbuffers::FlatBufferBuilder channel_fbb;
485 channel_fbb.ForceDefaults(1);
486
487 // Search for a schema with a matching type.
488 const aos::FlatbufferString<reflection::Schema> *found_schema = nullptr;
489 for (const aos::FlatbufferString<reflection::Schema> &schema: schemas) {
490 if (schema.message().root_table() != nullptr) {
491 if (schema.message().root_table()->name()->string_view() ==
492 c->type()->string_view()) {
493 found_schema = &schema;
494 }
495 }
496 }
497
498 CHECK(found_schema != nullptr)
499 << ": Failed to find schema for " << FlatbufferToJson(c);
500
501 // The following is wasteful, but works.
502 //
503 // Copy it into a Channel object by creating an object with only the
504 // schema populated and merge that into the current channel.
505 flatbuffers::Offset<reflection::Schema> schema_offset =
506 CopyFlatBuffer<reflection::Schema>(&found_schema->message(),
507 &channel_fbb);
508 Channel::Builder channel_builder(channel_fbb);
509 channel_builder.add_schema(schema_offset);
510 channel_fbb.Finish(channel_builder.Finish());
511 FlatbufferDetachedBuffer<Channel> channel_schema_flatbuffer(
512 channel_fbb.Release());
513
514 FlatbufferDetachedBuffer<Channel> merged_channel(
515 MergeFlatBuffers(channel_schema_flatbuffer, CopyFlatBuffer(c)));
516
517 channel_offsets.emplace_back(
518 CopyFlatBuffer<Channel>(&merged_channel.message(), &fbb));
519 }
520 channels_offset = fbb.CreateVector(channel_offsets);
521 }
522
523 // Copy the applications and maps unmodified.
524 flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<Application>>>
525 applications_offset;
526 {
527 ::std::vector<flatbuffers::Offset<Application>> applications_offsets;
528 if (config.message().has_applications()) {
529 for (const Application *a : *config.message().applications()) {
530 applications_offsets.emplace_back(CopyFlatBuffer<Application>(a, &fbb));
531 }
532 }
533 applications_offset = fbb.CreateVector(applications_offsets);
534 }
535
536 flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<Map>>>
537 maps_offset;
538 {
539 ::std::vector<flatbuffers::Offset<Map>> map_offsets;
540 if (config.message().has_maps()) {
541 for (const Map *m : *config.message().maps()) {
542 map_offsets.emplace_back(CopyFlatBuffer<Map>(m, &fbb));
543 }
544 maps_offset = fbb.CreateVector(map_offsets);
545 }
546 }
547
Austin Schuh217a9782019-12-21 23:02:50 -0800548 flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<Node>>>
549 nodes_offset;
550 {
551 ::std::vector<flatbuffers::Offset<Node>> node_offsets;
552 if (config.message().has_nodes()) {
553 for (const Node *n : *config.message().nodes()) {
554 node_offsets.emplace_back(CopyFlatBuffer<Node>(n, &fbb));
555 }
556 nodes_offset = fbb.CreateVector(node_offsets);
557 }
558 }
559
560 // Now insert everything else in unmodified.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700561 ConfigurationBuilder configuration_builder(fbb);
562 if (config.message().has_channels()) {
563 configuration_builder.add_channels(channels_offset);
564 }
565 if (config.message().has_maps()) {
566 configuration_builder.add_maps(maps_offset);
567 }
568 if (config.message().has_applications()) {
569 configuration_builder.add_applications(applications_offset);
570 }
Austin Schuh217a9782019-12-21 23:02:50 -0800571 if (config.message().has_nodes()) {
572 configuration_builder.add_nodes(nodes_offset);
573 }
Alex Perrycb7da4b2019-08-28 19:35:56 -0700574
575 fbb.Finish(configuration_builder.Finish());
576 return fbb.Release();
577}
578
Austin Schuh217a9782019-12-21 23:02:50 -0800579const Node *GetNodeFromHostname(const Configuration *config,
580 std::string_view hostname) {
581 for (const Node *node : *config->nodes()) {
582 if (node->hostname()->string_view() == hostname) {
583 return node;
584 }
585 }
586 return nullptr;
587}
588const Node *GetMyNode(const Configuration *config) {
589 const std::string hostname = (FLAGS_override_hostname.size() > 0)
590 ? FLAGS_override_hostname
591 : network::GetHostname();
592 const Node *node = GetNodeFromHostname(config, hostname);
593 if (node != nullptr) return node;
594
595 LOG(FATAL) << "Unknown node for host: " << hostname
596 << ". Consider using --override_hostname if hostname detection "
597 "is wrong.";
598 return nullptr;
599}
600
601const Node *GetNode(const Configuration *config, std::string_view name) {
602 for (const Node *node : *config->nodes()) {
603 if (node->name()->string_view() == name) {
604 return node;
605 }
606 }
607 return nullptr;
608}
609
610bool ChannelIsSendableOnNode(const Channel *channel, const Node *node) {
611 return (channel->source_node()->string_view() == node->name()->string_view());
612}
613
614bool ChannelIsReadableOnNode(const Channel *channel, const Node *node) {
615 if (channel->source_node()->string_view() == node->name()->string_view()) {
616 return true;
617 }
618
619 if (!channel->has_destination_nodes()) {
620 return false;
621 }
622
623 for (const flatbuffers::String *s : *channel->destination_nodes()) {
624 if (s->string_view() == node->name()->string_view()) {
625 return true;
626 }
627 }
628
629 return false;
630}
631
Brian Silverman66f079a2013-08-26 16:24:30 -0700632} // namespace configuration
633} // namespace aos