blob: 112c567209e98714751558127e4a24dae713bbf3 [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,
Austin Schuhbca6cf02019-12-22 17:28:34 -0800235 std::string_view *name, std::string_view type,
236 const Node *node) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700237 // For the same reason we merge configs in reverse order, we want to process
238 // maps in reverse order. That lets the outer config overwrite channels from
239 // the inner configs.
240 for (auto i = maps->rbegin(); i != maps->rend(); ++i) {
Austin Schuhbca6cf02019-12-22 17:28:34 -0800241 if (!i->has_match() || !i->match()->has_name()) {
242 continue;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700243 }
Austin Schuhbca6cf02019-12-22 17:28:34 -0800244 if (!i->has_rename() || !i->rename()->has_name()) {
245 continue;
246 }
247
248 // Handle normal maps (now that we know that match and rename are filled
249 // out).
250 if (i->match()->name()->string_view() != *name) {
251 continue;
252 }
253
254 // Handle type specific maps.
255 if (i->match()->has_type() && i->match()->type()->string_view() != type) {
256 continue;
257 }
258
259 if (node != nullptr && i->match()->has_source_node() &&
260 i->match()->source_node()->string_view() !=
261 node->name()->string_view()) {
262 continue;
263 }
264
265 VLOG(1) << "Renamed \"" << *name << "\" to \""
266 << i->rename()->name()->string_view() << "\"";
267 *name = i->rename()->name()->string_view();
Alex Perrycb7da4b2019-08-28 19:35:56 -0700268 }
269}
270
271} // namespace
272
Austin Schuh40485ed2019-10-26 21:51:44 -0700273FlatbufferDetachedBuffer<Configuration> MergeConfiguration(
Austin Schuhcb108412019-10-13 16:09:54 -0700274 const Flatbuffer<Configuration> &config) {
Austin Schuh40485ed2019-10-26 21:51:44 -0700275 // Store all the channels in a sorted set. This lets us track channels we
Austin Schuhcb108412019-10-13 16:09:54 -0700276 // have seen before and merge the updates in.
Austin Schuh40485ed2019-10-26 21:51:44 -0700277 absl::btree_set<FlatbufferDetachedBuffer<Channel>> channels;
Austin Schuhcb108412019-10-13 16:09:54 -0700278
Austin Schuh40485ed2019-10-26 21:51:44 -0700279 if (config.message().has_channels()) {
280 for (const Channel *c : *config.message().channels()) {
Austin Schuhcb108412019-10-13 16:09:54 -0700281 // Ignore malformed entries.
Austin Schuh40485ed2019-10-26 21:51:44 -0700282 if (!c->has_name()) {
Austin Schuhcb108412019-10-13 16:09:54 -0700283 continue;
284 }
Austin Schuh40485ed2019-10-26 21:51:44 -0700285 if (!c->has_type()) {
Austin Schuhcb108412019-10-13 16:09:54 -0700286 continue;
287 }
288
Austin Schuh40485ed2019-10-26 21:51:44 -0700289 // Attempt to insert the channel.
290 auto result = channels.insert(CopyFlatBuffer(c));
Austin Schuhcb108412019-10-13 16:09:54 -0700291 if (!result.second) {
292 // Already there, so merge the new table into the original.
Austin Schuh40485ed2019-10-26 21:51:44 -0700293 *result.first = MergeFlatBuffers(*result.first, CopyFlatBuffer(c));
Austin Schuhcb108412019-10-13 16:09:54 -0700294 }
295 }
296 }
297
298 // Now repeat this for the application list.
Austin Schuh40485ed2019-10-26 21:51:44 -0700299 absl::btree_set<FlatbufferDetachedBuffer<Application>> applications;
Austin Schuhcb108412019-10-13 16:09:54 -0700300 if (config.message().has_applications()) {
301 for (const Application *a : *config.message().applications()) {
302 if (!a->has_name()) {
303 continue;
304 }
305
306 auto result = applications.insert(CopyFlatBuffer(a));
307 if (!result.second) {
308 *result.first = MergeFlatBuffers(*result.first, CopyFlatBuffer(a));
309 }
310 }
311 }
312
Austin Schuh217a9782019-12-21 23:02:50 -0800313 // Now repeat this for the node list.
314 absl::btree_set<FlatbufferDetachedBuffer<Node>> nodes;
315 if (config.message().has_nodes()) {
316 for (const Node *n : *config.message().nodes()) {
317 if (!n->has_name()) {
318 continue;
319 }
320
321 auto result = nodes.insert(CopyFlatBuffer(n));
322 if (!result.second) {
323 *result.first = MergeFlatBuffers(*result.first, CopyFlatBuffer(n));
324 }
325 }
326 }
327
Austin Schuhcb108412019-10-13 16:09:54 -0700328 flatbuffers::FlatBufferBuilder fbb;
329 fbb.ForceDefaults(1);
330
331 // Start by building the vectors. They need to come before the final table.
Austin Schuh40485ed2019-10-26 21:51:44 -0700332 // Channels
333 flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<Channel>>>
334 channels_offset;
Austin Schuhcb108412019-10-13 16:09:54 -0700335 {
Austin Schuh40485ed2019-10-26 21:51:44 -0700336 ::std::vector<flatbuffers::Offset<Channel>> channel_offsets;
337 for (const FlatbufferDetachedBuffer<Channel> &c : channels) {
338 channel_offsets.emplace_back(
339 CopyFlatBuffer<Channel>(&c.message(), &fbb));
Austin Schuhcb108412019-10-13 16:09:54 -0700340 }
Austin Schuh40485ed2019-10-26 21:51:44 -0700341 channels_offset = fbb.CreateVector(channel_offsets);
Austin Schuhcb108412019-10-13 16:09:54 -0700342 }
343
344 // Applications
345 flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<Application>>>
346 applications_offset;
347 {
348 ::std::vector<flatbuffers::Offset<Application>> applications_offsets;
Austin Schuh40485ed2019-10-26 21:51:44 -0700349 for (const FlatbufferDetachedBuffer<Application> &a : applications) {
Austin Schuhcb108412019-10-13 16:09:54 -0700350 applications_offsets.emplace_back(
351 CopyFlatBuffer<Application>(&a.message(), &fbb));
352 }
353 applications_offset = fbb.CreateVector(applications_offsets);
354 }
355
356 // Just copy the maps
357 flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<Map>>>
358 maps_offset;
359 {
360 ::std::vector<flatbuffers::Offset<Map>> map_offsets;
361 if (config.message().has_maps()) {
362 for (const Map *m : *config.message().maps()) {
363 map_offsets.emplace_back(CopyFlatBuffer<Map>(m, &fbb));
364 }
365 maps_offset = fbb.CreateVector(map_offsets);
366 }
367 }
368
Austin Schuh217a9782019-12-21 23:02:50 -0800369 // Nodes
370 flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<Node>>>
371 nodes_offset;
372 {
373 ::std::vector<flatbuffers::Offset<Node>> node_offsets;
374 for (const FlatbufferDetachedBuffer<Node> &n : nodes) {
375 node_offsets.emplace_back(CopyFlatBuffer<Node>(&n.message(), &fbb));
376 }
377 nodes_offset = fbb.CreateVector(node_offsets);
378 }
379
Austin Schuhcb108412019-10-13 16:09:54 -0700380 // And then build a Configuration with them all.
381 ConfigurationBuilder configuration_builder(fbb);
Austin Schuh40485ed2019-10-26 21:51:44 -0700382 configuration_builder.add_channels(channels_offset);
Austin Schuhcb108412019-10-13 16:09:54 -0700383 if (config.message().has_maps()) {
384 configuration_builder.add_maps(maps_offset);
385 }
Austin Schuh217a9782019-12-21 23:02:50 -0800386 if (config.message().has_applications()) {
387 configuration_builder.add_applications(applications_offset);
388 }
389 if (config.message().has_nodes()) {
390 configuration_builder.add_nodes(nodes_offset);
391 }
Austin Schuhcb108412019-10-13 16:09:54 -0700392
393 fbb.Finish(configuration_builder.Finish());
Austin Schuh217a9782019-12-21 23:02:50 -0800394
395 // Now, validate that if there is a node list, every channel has a source
396 // node.
397 FlatbufferDetachedBuffer<Configuration> result(fbb.Release());
398
399 // Check that if there is a node list, all the source nodes are filled out and
400 // valid, and all the destination nodes are valid (and not the source). This
401 // is a basic consistency check.
402 if (result.message().has_nodes()) {
403 for (const Channel *c : *config.message().channels()) {
404 CHECK(c->has_source_node()) << ": Channel " << FlatbufferToJson(c)
405 << " is missing \"source_node\"";
406 CHECK(GetNode(&result.message(), c->source_node()->string_view()) !=
407 nullptr)
408 << ": Channel " << FlatbufferToJson(c)
409 << " has an unknown \"source_node\"";
410
411 if (c->has_destination_nodes()) {
412 for (const flatbuffers::String *n : *c->destination_nodes()) {
413 CHECK(GetNode(&result.message(), n->string_view()) != nullptr)
414 << ": Channel " << FlatbufferToJson(c)
415 << " has an unknown \"destination_nodes\" " << n->string_view();
416
417 CHECK_NE(n->string_view(), c->source_node()->string_view())
418 << ": Channel " << FlatbufferToJson(c)
419 << " is forwarding data to itself";
420 }
421 }
422 }
423 }
424
425 return result;
Austin Schuhcb108412019-10-13 16:09:54 -0700426}
427
Brian Silverman66f079a2013-08-26 16:24:30 -0700428const char *GetRootDirectory() {
Austin Schuh217a9782019-12-21 23:02:50 -0800429 static char *root_dir; // return value
John Park7bca9812019-10-14 21:23:45 -0700430 static absl::once_flag once_;
431 absl::call_once(once_, DoGetRootDirectory, &root_dir);
432 return root_dir;
Brian Silverman66f079a2013-08-26 16:24:30 -0700433}
434
435const char *GetLoggingDirectory() {
Austin Schuh217a9782019-12-21 23:02:50 -0800436 static char *retu; // return value
John Park7bca9812019-10-14 21:23:45 -0700437 static absl::once_flag once_;
438 absl::call_once(once_, DoGetLoggingDirectory, &retu);
439 return retu;
Brian Silverman66f079a2013-08-26 16:24:30 -0700440}
441
Austin Schuh40485ed2019-10-26 21:51:44 -0700442FlatbufferDetachedBuffer<Configuration> ReadConfig(
James Kuszmaul3ae42262019-11-08 12:33:41 -0800443 const std::string_view path) {
Austin Schuhcb108412019-10-13 16:09:54 -0700444 // We only want to read a file once. So track the visited files in a set.
445 absl::btree_set<std::string> visited_paths;
446 return MergeConfiguration(ReadConfig(path, &visited_paths));
447}
448
James Kuszmaul3ae42262019-11-08 12:33:41 -0800449const Channel *GetChannel(const Configuration *config, std::string_view name,
450 std::string_view type,
Austin Schuhbca6cf02019-12-22 17:28:34 -0800451 std::string_view application_name, const Node *node) {
452 const std::string_view original_name = name;
Austin Schuhcb108412019-10-13 16:09:54 -0700453 VLOG(1) << "Looking up { \"name\": \"" << name << "\", \"type\": \"" << type
454 << "\" }";
455
456 // First handle application specific maps. Only do this if we have a matching
457 // application name, and it has maps.
Austin Schuh40485ed2019-10-26 21:51:44 -0700458 if (config->has_applications()) {
459 auto application_iterator = std::lower_bound(
460 config->applications()->cbegin(), config->applications()->cend(),
461 application_name, CompareApplications);
462 if (application_iterator != config->applications()->cend() &&
Austin Schuhcb108412019-10-13 16:09:54 -0700463 EqualsApplications(*application_iterator, application_name)) {
464 if (application_iterator->has_maps()) {
Austin Schuhbca6cf02019-12-22 17:28:34 -0800465 HandleMaps(application_iterator->maps(), &name, type, node);
Austin Schuhcb108412019-10-13 16:09:54 -0700466 }
467 }
468 }
469
470 // Now do global maps.
Austin Schuh40485ed2019-10-26 21:51:44 -0700471 if (config->has_maps()) {
Austin Schuhbca6cf02019-12-22 17:28:34 -0800472 HandleMaps(config->maps(), &name, type, node);
Austin Schuhcb108412019-10-13 16:09:54 -0700473 }
474
Austin Schuhbca6cf02019-12-22 17:28:34 -0800475 if (original_name != name) {
476 VLOG(1) << "Remapped to { \"name\": \"" << name << "\", \"type\": \""
477 << type << "\" }";
478 }
Alex Perrycb7da4b2019-08-28 19:35:56 -0700479
Austin Schuh40485ed2019-10-26 21:51:44 -0700480 // Then look for the channel.
481 auto channel_iterator =
482 std::lower_bound(config->channels()->cbegin(),
483 config->channels()->cend(),
484 std::make_pair(name, type), CompareChannels);
Austin Schuhcb108412019-10-13 16:09:54 -0700485
486 // Make sure we actually found it, and it matches.
Austin Schuh40485ed2019-10-26 21:51:44 -0700487 if (channel_iterator != config->channels()->cend() &&
488 EqualsChannels(*channel_iterator, std::make_pair(name, type))) {
Austin Schuhbca6cf02019-12-22 17:28:34 -0800489 if (VLOG_IS_ON(2)) {
490 VLOG(2) << "Found: " << FlatbufferToJson(*channel_iterator);
491 } else if (VLOG_IS_ON(1)) {
492 VLOG(1) << "Found: " << CleanedChannelToString(*channel_iterator);
493 }
Austin Schuh40485ed2019-10-26 21:51:44 -0700494 return *channel_iterator;
Austin Schuhcb108412019-10-13 16:09:54 -0700495 } else {
496 VLOG(1) << "No match for { \"name\": \"" << name << "\", \"type\": \""
497 << type << "\" }";
498 return nullptr;
499 }
500}
501
Austin Schuhbca6cf02019-12-22 17:28:34 -0800502std::string CleanedChannelToString(const Channel *channel) {
503 FlatbufferDetachedBuffer<Channel> cleaned_channel = CopyFlatBuffer(channel);
504 cleaned_channel.mutable_message()->clear_schema();
505 return FlatbufferToJson(cleaned_channel);
506}
507
Alex Perrycb7da4b2019-08-28 19:35:56 -0700508FlatbufferDetachedBuffer<Configuration> MergeConfiguration(
509 const Flatbuffer<Configuration> &config,
510 const std::vector<aos::FlatbufferString<reflection::Schema>> &schemas) {
511 flatbuffers::FlatBufferBuilder fbb;
512 fbb.ForceDefaults(1);
513
514 flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<Channel>>>
515 channels_offset;
516 if (config.message().has_channels()) {
517 std::vector<flatbuffers::Offset<Channel>> channel_offsets;
518 for (const Channel *c : *config.message().channels()) {
519 flatbuffers::FlatBufferBuilder channel_fbb;
520 channel_fbb.ForceDefaults(1);
521
522 // Search for a schema with a matching type.
523 const aos::FlatbufferString<reflection::Schema> *found_schema = nullptr;
524 for (const aos::FlatbufferString<reflection::Schema> &schema: schemas) {
525 if (schema.message().root_table() != nullptr) {
526 if (schema.message().root_table()->name()->string_view() ==
527 c->type()->string_view()) {
528 found_schema = &schema;
529 }
530 }
531 }
532
533 CHECK(found_schema != nullptr)
534 << ": Failed to find schema for " << FlatbufferToJson(c);
535
536 // The following is wasteful, but works.
537 //
538 // Copy it into a Channel object by creating an object with only the
539 // schema populated and merge that into the current channel.
540 flatbuffers::Offset<reflection::Schema> schema_offset =
541 CopyFlatBuffer<reflection::Schema>(&found_schema->message(),
542 &channel_fbb);
543 Channel::Builder channel_builder(channel_fbb);
544 channel_builder.add_schema(schema_offset);
545 channel_fbb.Finish(channel_builder.Finish());
546 FlatbufferDetachedBuffer<Channel> channel_schema_flatbuffer(
547 channel_fbb.Release());
548
549 FlatbufferDetachedBuffer<Channel> merged_channel(
550 MergeFlatBuffers(channel_schema_flatbuffer, CopyFlatBuffer(c)));
551
552 channel_offsets.emplace_back(
553 CopyFlatBuffer<Channel>(&merged_channel.message(), &fbb));
554 }
555 channels_offset = fbb.CreateVector(channel_offsets);
556 }
557
558 // Copy the applications and maps unmodified.
559 flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<Application>>>
560 applications_offset;
561 {
562 ::std::vector<flatbuffers::Offset<Application>> applications_offsets;
563 if (config.message().has_applications()) {
564 for (const Application *a : *config.message().applications()) {
565 applications_offsets.emplace_back(CopyFlatBuffer<Application>(a, &fbb));
566 }
567 }
568 applications_offset = fbb.CreateVector(applications_offsets);
569 }
570
571 flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<Map>>>
572 maps_offset;
573 {
574 ::std::vector<flatbuffers::Offset<Map>> map_offsets;
575 if (config.message().has_maps()) {
576 for (const Map *m : *config.message().maps()) {
577 map_offsets.emplace_back(CopyFlatBuffer<Map>(m, &fbb));
578 }
579 maps_offset = fbb.CreateVector(map_offsets);
580 }
581 }
582
Austin Schuh217a9782019-12-21 23:02:50 -0800583 flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<Node>>>
584 nodes_offset;
585 {
586 ::std::vector<flatbuffers::Offset<Node>> node_offsets;
587 if (config.message().has_nodes()) {
588 for (const Node *n : *config.message().nodes()) {
589 node_offsets.emplace_back(CopyFlatBuffer<Node>(n, &fbb));
590 }
591 nodes_offset = fbb.CreateVector(node_offsets);
592 }
593 }
594
595 // Now insert everything else in unmodified.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700596 ConfigurationBuilder configuration_builder(fbb);
597 if (config.message().has_channels()) {
598 configuration_builder.add_channels(channels_offset);
599 }
600 if (config.message().has_maps()) {
601 configuration_builder.add_maps(maps_offset);
602 }
603 if (config.message().has_applications()) {
604 configuration_builder.add_applications(applications_offset);
605 }
Austin Schuh217a9782019-12-21 23:02:50 -0800606 if (config.message().has_nodes()) {
607 configuration_builder.add_nodes(nodes_offset);
608 }
Alex Perrycb7da4b2019-08-28 19:35:56 -0700609
610 fbb.Finish(configuration_builder.Finish());
611 return fbb.Release();
612}
613
Austin Schuh217a9782019-12-21 23:02:50 -0800614const Node *GetNodeFromHostname(const Configuration *config,
615 std::string_view hostname) {
616 for (const Node *node : *config->nodes()) {
617 if (node->hostname()->string_view() == hostname) {
618 return node;
619 }
620 }
621 return nullptr;
622}
623const Node *GetMyNode(const Configuration *config) {
624 const std::string hostname = (FLAGS_override_hostname.size() > 0)
625 ? FLAGS_override_hostname
626 : network::GetHostname();
627 const Node *node = GetNodeFromHostname(config, hostname);
628 if (node != nullptr) return node;
629
630 LOG(FATAL) << "Unknown node for host: " << hostname
631 << ". Consider using --override_hostname if hostname detection "
632 "is wrong.";
633 return nullptr;
634}
635
636const Node *GetNode(const Configuration *config, std::string_view name) {
637 for (const Node *node : *config->nodes()) {
638 if (node->name()->string_view() == name) {
639 return node;
640 }
641 }
642 return nullptr;
643}
644
645bool ChannelIsSendableOnNode(const Channel *channel, const Node *node) {
646 return (channel->source_node()->string_view() == node->name()->string_view());
647}
648
649bool ChannelIsReadableOnNode(const Channel *channel, const Node *node) {
650 if (channel->source_node()->string_view() == node->name()->string_view()) {
651 return true;
652 }
653
654 if (!channel->has_destination_nodes()) {
655 return false;
656 }
657
658 for (const flatbuffers::String *s : *channel->destination_nodes()) {
659 if (s->string_view() == node->name()->string_view()) {
660 return true;
661 }
662 }
663
664 return false;
665}
666
Brian Silverman66f079a2013-08-26 16:24:30 -0700667} // namespace configuration
668} // namespace aos