John Park | 398c74a | 2018-10-20 21:17:39 -0700 | [diff] [blame] | 1 | #include "aos/configuration.h" |
Brian Silverman | 66f079a | 2013-08-26 16:24:30 -0700 | [diff] [blame] | 2 | |
Brian Silverman | 66f079a | 2013-08-26 16:24:30 -0700 | [diff] [blame] | 3 | #include <arpa/inet.h> |
| 4 | #include <ifaddrs.h> |
Austin Schuh | f1fff28 | 2020-03-28 16:57:32 -0700 | [diff] [blame] | 5 | #include <netinet/in.h> |
Austin Schuh | f1fff28 | 2020-03-28 16:57:32 -0700 | [diff] [blame] | 6 | #include <sys/types.h> |
Brian Silverman | 66f079a | 2013-08-26 16:24:30 -0700 | [diff] [blame] | 7 | #include <unistd.h> |
Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 8 | |
Tyler Chatow | bf0609c | 2021-07-31 16:13:27 -0700 | [diff] [blame] | 9 | #include <cstdlib> |
| 10 | #include <cstring> |
Austin Schuh | 68d9859 | 2020-11-01 23:22:57 -0800 | [diff] [blame] | 11 | #include <map> |
Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 12 | #include <set> |
Austin Schuh | ef38cd2 | 2021-07-21 15:24:23 -0700 | [diff] [blame] | 13 | #include <string> |
James Kuszmaul | 3ae4226 | 2019-11-08 12:33:41 -0800 | [diff] [blame] | 14 | #include <string_view> |
Austin Schuh | ef38cd2 | 2021-07-21 15:24:23 -0700 | [diff] [blame] | 15 | #include <vector> |
Brian Silverman | 66f079a | 2013-08-26 16:24:30 -0700 | [diff] [blame] | 16 | |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 17 | #include "absl/container/btree_set.h" |
Austin Schuh | a81454b | 2020-05-12 19:58:36 -0700 | [diff] [blame] | 18 | #include "absl/strings/str_cat.h" |
Austin Schuh | ef38cd2 | 2021-07-21 15:24:23 -0700 | [diff] [blame] | 19 | #include "absl/strings/str_join.h" |
| 20 | #include "absl/strings/str_split.h" |
Philipp Schrader | 790cb54 | 2023-07-05 21:06:52 -0700 | [diff] [blame] | 21 | #include "gflags/gflags.h" |
| 22 | #include "glog/logging.h" |
| 23 | |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 24 | #include "aos/configuration_generated.h" |
| 25 | #include "aos/flatbuffer_merge.h" |
Austin Schuh | 83cbb1e | 2023-06-23 12:59:02 -0700 | [diff] [blame] | 26 | #include "aos/ipc_lib/index.h" |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 27 | #include "aos/json_to_flatbuffer.h" |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 28 | #include "aos/network/team_number.h" |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 29 | #include "aos/unique_malloc_ptr.h" |
| 30 | #include "aos/util/file.h" |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 31 | |
Austin Schuh | 83cbb1e | 2023-06-23 12:59:02 -0700 | [diff] [blame] | 32 | DEFINE_uint32(max_queue_size_override, 0, |
| 33 | "If nonzero, this is the max number of elements in a queue to " |
| 34 | "enforce. If zero, use the number that the processor that this " |
| 35 | "application is compiled for can support. This is mostly useful " |
| 36 | "for config validation, and shouldn't be touched."); |
| 37 | |
Brian Silverman | 66f079a | 2013-08-26 16:24:30 -0700 | [diff] [blame] | 38 | namespace aos { |
Austin Schuh | 1518232 | 2020-10-10 15:25:21 -0700 | [diff] [blame] | 39 | namespace { |
Austin Schuh | fb37c61 | 2022-08-11 15:24:51 -0700 | [diff] [blame] | 40 | namespace chrono = std::chrono; |
| 41 | |
Austin Schuh | 1518232 | 2020-10-10 15:25:21 -0700 | [diff] [blame] | 42 | bool EndsWith(std::string_view str, std::string_view end) { |
| 43 | if (str.size() < end.size()) { |
| 44 | return false; |
| 45 | } |
| 46 | if (str.substr(str.size() - end.size(), end.size()) != end) { |
| 47 | return false; |
| 48 | } |
| 49 | return true; |
| 50 | } |
| 51 | |
| 52 | std::string MaybeReplaceExtension(std::string_view filename, |
| 53 | std::string_view extension, |
| 54 | std::string_view replacement) { |
| 55 | if (!EndsWith(filename, extension)) { |
| 56 | return std::string(filename); |
| 57 | } |
| 58 | filename.remove_suffix(extension.size()); |
| 59 | return absl::StrCat(filename, replacement); |
| 60 | } |
| 61 | |
| 62 | FlatbufferDetachedBuffer<Configuration> ReadConfigFile(std::string_view path, |
| 63 | bool binary) { |
| 64 | if (binary) { |
| 65 | FlatbufferVector<Configuration> config = |
| 66 | FileToFlatbuffer<Configuration>(path); |
| 67 | return CopySpanAsDetachedBuffer(config.span()); |
| 68 | } |
| 69 | |
| 70 | flatbuffers::DetachedBuffer buffer = JsonToFlatbuffer( |
| 71 | util::ReadFileToStringOrDie(path), ConfigurationTypeTable()); |
| 72 | |
Austin Schuh | 84a039a | 2021-11-03 16:50:34 -0700 | [diff] [blame] | 73 | CHECK_GT(buffer.size(), 0u) << ": Failed to parse JSON file: " << path; |
Austin Schuh | 1518232 | 2020-10-10 15:25:21 -0700 | [diff] [blame] | 74 | |
| 75 | return FlatbufferDetachedBuffer<Configuration>(std::move(buffer)); |
| 76 | } |
| 77 | |
| 78 | } // namespace |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 79 | // Define the compare and equal operators for Channel and Application so we can |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 80 | // insert them in the btree below. |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 81 | bool operator<(const FlatbufferDetachedBuffer<Channel> &lhs, |
| 82 | const FlatbufferDetachedBuffer<Channel> &rhs) { |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 83 | int name_compare = lhs.message().name()->string_view().compare( |
| 84 | rhs.message().name()->string_view()); |
| 85 | if (name_compare == 0) { |
| 86 | return lhs.message().type()->string_view() < |
| 87 | rhs.message().type()->string_view(); |
| 88 | } else if (name_compare < 0) { |
| 89 | return true; |
| 90 | } else { |
| 91 | return false; |
| 92 | } |
| 93 | } |
| 94 | |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 95 | bool operator==(const FlatbufferDetachedBuffer<Channel> &lhs, |
| 96 | const FlatbufferDetachedBuffer<Channel> &rhs) { |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 97 | return lhs.message().name()->string_view() == |
| 98 | rhs.message().name()->string_view() && |
| 99 | lhs.message().type()->string_view() == |
| 100 | rhs.message().type()->string_view(); |
| 101 | } |
| 102 | |
Austin Schuh | a7996eb | 2021-10-11 19:03:24 -0700 | [diff] [blame] | 103 | bool operator<(const FlatbufferDetachedBuffer<Connection> &lhs, |
| 104 | const FlatbufferDetachedBuffer<Connection> &rhs) { |
| 105 | return lhs.message().name()->string_view() < |
| 106 | rhs.message().name()->string_view(); |
| 107 | } |
| 108 | |
| 109 | bool operator==(const FlatbufferDetachedBuffer<Connection> &lhs, |
| 110 | const FlatbufferDetachedBuffer<Connection> &rhs) { |
| 111 | return lhs.message().name()->string_view() == |
| 112 | rhs.message().name()->string_view(); |
| 113 | } |
| 114 | |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 115 | bool operator==(const FlatbufferDetachedBuffer<Application> &lhs, |
| 116 | const FlatbufferDetachedBuffer<Application> &rhs) { |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 117 | return lhs.message().name()->string_view() == |
| 118 | rhs.message().name()->string_view(); |
| 119 | } |
| 120 | |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 121 | bool operator<(const FlatbufferDetachedBuffer<Application> &lhs, |
| 122 | const FlatbufferDetachedBuffer<Application> &rhs) { |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 123 | return lhs.message().name()->string_view() < |
| 124 | rhs.message().name()->string_view(); |
| 125 | } |
| 126 | |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 127 | bool operator==(const FlatbufferDetachedBuffer<Node> &lhs, |
| 128 | const FlatbufferDetachedBuffer<Node> &rhs) { |
| 129 | return lhs.message().name()->string_view() == |
| 130 | rhs.message().name()->string_view(); |
| 131 | } |
| 132 | |
| 133 | bool operator<(const FlatbufferDetachedBuffer<Node> &lhs, |
| 134 | const FlatbufferDetachedBuffer<Node> &rhs) { |
| 135 | return lhs.message().name()->string_view() < |
| 136 | rhs.message().name()->string_view(); |
| 137 | } |
| 138 | |
Brian Silverman | 66f079a | 2013-08-26 16:24:30 -0700 | [diff] [blame] | 139 | namespace configuration { |
| 140 | namespace { |
| 141 | |
Adam Snaider | 13d48d9 | 2023-08-03 12:20:15 -0700 | [diff] [blame] | 142 | template <typename T> |
| 143 | struct FbsContainer { |
| 144 | FbsContainer(aos::FlatbufferDetachedBuffer<T> table) { |
| 145 | this->table = |
| 146 | std::make_unique<FlatbufferDetachedBuffer<T>>(std::move(table)); |
| 147 | } |
| 148 | std::unique_ptr<FlatbufferDetachedBuffer<T>> table; |
| 149 | bool operator==(const FbsContainer<T> &other) const { |
| 150 | return *this->table == *other.table; |
| 151 | } |
| 152 | bool operator<(const FbsContainer<T> &other) const { |
| 153 | return *this->table < *other.table; |
| 154 | } |
| 155 | }; |
| 156 | |
| 157 | typedef FbsContainer<Channel> ChannelContainer; |
| 158 | typedef FbsContainer<Connection> ConnectionContainer; |
| 159 | typedef FbsContainer<Application> ApplicationContainer; |
| 160 | typedef FbsContainer<Node> NodeContainer; |
| 161 | |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 162 | // Extracts the folder part of a path. Returns ./ if there is no path. |
Austin Schuh | f1fff28 | 2020-03-28 16:57:32 -0700 | [diff] [blame] | 163 | std::string_view ExtractFolder(const std::string_view filename) { |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 164 | auto last_slash_pos = filename.find_last_of("/\\"); |
| 165 | |
James Kuszmaul | 3ae4226 | 2019-11-08 12:33:41 -0800 | [diff] [blame] | 166 | return last_slash_pos == std::string_view::npos |
| 167 | ? std::string_view("./") |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 168 | : filename.substr(0, last_slash_pos + 1); |
| 169 | } |
| 170 | |
James Kuszmaul | c0c08da | 2020-05-10 18:56:07 -0700 | [diff] [blame] | 171 | std::string AbsolutePath(const std::string_view filename) { |
| 172 | // Uses an std::string so that we know the input will be null-terminated. |
| 173 | const std::string terminated_file(filename); |
| 174 | char buffer[PATH_MAX]; |
| 175 | PCHECK(NULL != realpath(terminated_file.c_str(), buffer)); |
| 176 | return buffer; |
| 177 | } |
| 178 | |
Austin Schuh | ef38cd2 | 2021-07-21 15:24:23 -0700 | [diff] [blame] | 179 | std::string RemoveDotDots(const std::string_view filename) { |
| 180 | std::vector<std::string> split = absl::StrSplit(filename, '/'); |
| 181 | auto iterator = split.begin(); |
| 182 | while (iterator != split.end()) { |
| 183 | if (iterator->empty()) { |
| 184 | iterator = split.erase(iterator); |
| 185 | } else if (*iterator == ".") { |
| 186 | iterator = split.erase(iterator); |
| 187 | } else if (*iterator == "..") { |
| 188 | CHECK(iterator != split.begin()) |
| 189 | << ": Import path may not start with ..: " << filename; |
| 190 | auto previous = iterator; |
| 191 | --previous; |
| 192 | split.erase(iterator); |
| 193 | iterator = split.erase(previous); |
| 194 | } else { |
| 195 | ++iterator; |
| 196 | } |
| 197 | } |
| 198 | return absl::StrJoin(split, "/"); |
| 199 | } |
| 200 | |
Milind Upadhyay | 17098ba | 2022-04-15 22:18:50 -0700 | [diff] [blame] | 201 | std::optional<FlatbufferDetachedBuffer<Configuration>> MaybeReadConfig( |
James Kuszmaul | c0c08da | 2020-05-10 18:56:07 -0700 | [diff] [blame] | 202 | const std::string_view path, absl::btree_set<std::string> *visited_paths, |
| 203 | const std::vector<std::string_view> &extra_import_paths) { |
Austin Schuh | 1518232 | 2020-10-10 15:25:21 -0700 | [diff] [blame] | 204 | std::string binary_path = MaybeReplaceExtension(path, ".json", ".bfbs"); |
Austin Schuh | ef38cd2 | 2021-07-21 15:24:23 -0700 | [diff] [blame] | 205 | VLOG(1) << "Looking up: " << path << ", starting with: " << binary_path; |
Austin Schuh | 1518232 | 2020-10-10 15:25:21 -0700 | [diff] [blame] | 206 | bool binary_path_exists = util::PathExists(binary_path); |
James Kuszmaul | c0c08da | 2020-05-10 18:56:07 -0700 | [diff] [blame] | 207 | std::string raw_path(path); |
Austin Schuh | 1518232 | 2020-10-10 15:25:21 -0700 | [diff] [blame] | 208 | // For each .json file, look and see if we can find a .bfbs file next to it |
| 209 | // with the same base name. If we can, assume it is the same and use it |
| 210 | // instead. It is much faster to load .bfbs files than .json files. |
| 211 | if (!binary_path_exists && !util::PathExists(raw_path)) { |
| 212 | const bool path_is_absolute = raw_path.size() > 0 && raw_path[0] == '/'; |
Brian Silverman | d058819 | 2022-07-26 00:35:22 -0700 | [diff] [blame] | 213 | if (path_is_absolute) { |
| 214 | // Nowhere else to look up an absolute path, so fail now. Note that we |
| 215 | // always have at least one extra import path based on /proc/self/exe, so |
| 216 | // warning about those paths existing isn't helpful. |
| 217 | LOG(ERROR) << ": Failed to find file " << path << "."; |
Milind Upadhyay | 17098ba | 2022-04-15 22:18:50 -0700 | [diff] [blame] | 218 | return std::nullopt; |
James Kuszmaul | c0c08da | 2020-05-10 18:56:07 -0700 | [diff] [blame] | 219 | } |
| 220 | |
| 221 | bool found_path = false; |
| 222 | for (const auto &import_path : extra_import_paths) { |
Austin Schuh | ef38cd2 | 2021-07-21 15:24:23 -0700 | [diff] [blame] | 223 | raw_path = std::string(import_path) + "/" + RemoveDotDots(path); |
Austin Schuh | 1518232 | 2020-10-10 15:25:21 -0700 | [diff] [blame] | 224 | binary_path = MaybeReplaceExtension(raw_path, ".json", ".bfbs"); |
Austin Schuh | ef38cd2 | 2021-07-21 15:24:23 -0700 | [diff] [blame] | 225 | VLOG(1) << "Checking: " << binary_path; |
Austin Schuh | 1518232 | 2020-10-10 15:25:21 -0700 | [diff] [blame] | 226 | binary_path_exists = util::PathExists(binary_path); |
| 227 | if (binary_path_exists) { |
| 228 | found_path = true; |
| 229 | break; |
| 230 | } |
Austin Schuh | ef38cd2 | 2021-07-21 15:24:23 -0700 | [diff] [blame] | 231 | VLOG(1) << "Checking: " << raw_path; |
James Kuszmaul | c0c08da | 2020-05-10 18:56:07 -0700 | [diff] [blame] | 232 | if (util::PathExists(raw_path)) { |
| 233 | found_path = true; |
| 234 | break; |
| 235 | } |
| 236 | } |
Milind Upadhyay | 17098ba | 2022-04-15 22:18:50 -0700 | [diff] [blame] | 237 | if (!found_path) { |
| 238 | LOG(ERROR) << ": Failed to find file " << path << "."; |
| 239 | return std::nullopt; |
| 240 | } |
James Kuszmaul | c0c08da | 2020-05-10 18:56:07 -0700 | [diff] [blame] | 241 | } |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 242 | |
Milind Upadhyay | 17098ba | 2022-04-15 22:18:50 -0700 | [diff] [blame] | 243 | std::optional<FlatbufferDetachedBuffer<Configuration>> config = |
| 244 | ReadConfigFile(binary_path_exists ? binary_path : raw_path, |
| 245 | binary_path_exists); |
James Kuszmaul | c0c08da | 2020-05-10 18:56:07 -0700 | [diff] [blame] | 246 | |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 247 | // Depth first. Take the following example: |
| 248 | // |
| 249 | // config1.json: |
| 250 | // { |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 251 | // "channels": [ |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 252 | // { |
| 253 | // "name": "/foo", |
| 254 | // "type": ".aos.bar", |
| 255 | // "max_size": 5 |
| 256 | // } |
| 257 | // ], |
| 258 | // "imports": [ |
| 259 | // "config2.json", |
| 260 | // ] |
| 261 | // } |
| 262 | // |
| 263 | // config2.json: |
| 264 | // { |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 265 | // "channels": [ |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 266 | // { |
| 267 | // "name": "/foo", |
| 268 | // "type": ".aos.bar", |
| 269 | // "max_size": 7 |
| 270 | // } |
| 271 | // ], |
| 272 | // } |
| 273 | // |
| 274 | // We want the main config (config1.json) to be able to override the imported |
| 275 | // config. That means that it needs to be merged into the imported configs, |
| 276 | // not the other way around. |
| 277 | |
Austin Schuh | 1518232 | 2020-10-10 15:25:21 -0700 | [diff] [blame] | 278 | const std::string absolute_path = |
| 279 | AbsolutePath(binary_path_exists ? binary_path : raw_path); |
| 280 | // Track that we have seen this file before recursing. Track the path we |
| 281 | // actually loaded (which should be consistent if imported twice). |
James Kuszmaul | c0c08da | 2020-05-10 18:56:07 -0700 | [diff] [blame] | 282 | if (!visited_paths->insert(absolute_path).second) { |
| 283 | for (const auto &visited_path : *visited_paths) { |
| 284 | LOG(INFO) << "Already visited: " << visited_path; |
| 285 | } |
| 286 | LOG(FATAL) |
| 287 | << "Already imported " << path << " (i.e. " << absolute_path |
| 288 | << "). See above for the files that have already been processed."; |
Milind Upadhyay | 17098ba | 2022-04-15 22:18:50 -0700 | [diff] [blame] | 289 | return std::nullopt; |
James Kuszmaul | c0c08da | 2020-05-10 18:56:07 -0700 | [diff] [blame] | 290 | } |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 291 | |
Milind Upadhyay | 17098ba | 2022-04-15 22:18:50 -0700 | [diff] [blame] | 292 | if (config->message().has_imports()) { |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 293 | // Capture the imports. |
| 294 | const flatbuffers::Vector<flatbuffers::Offset<flatbuffers::String>> *v = |
Milind Upadhyay | 17098ba | 2022-04-15 22:18:50 -0700 | [diff] [blame] | 295 | config->message().imports(); |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 296 | |
| 297 | // And then wipe them. This gets GCed when we merge later. |
Milind Upadhyay | 17098ba | 2022-04-15 22:18:50 -0700 | [diff] [blame] | 298 | config->mutable_message()->clear_imports(); |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 299 | |
| 300 | // Start with an empty configuration to merge into. |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 301 | FlatbufferDetachedBuffer<Configuration> merged_config = |
| 302 | FlatbufferDetachedBuffer<Configuration>::Empty(); |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 303 | |
James Kuszmaul | c0c08da | 2020-05-10 18:56:07 -0700 | [diff] [blame] | 304 | const std::string path_folder(ExtractFolder(path)); |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 305 | for (const flatbuffers::String *str : *v) { |
James Kuszmaul | c0c08da | 2020-05-10 18:56:07 -0700 | [diff] [blame] | 306 | const std::string included_config = |
| 307 | path_folder + "/" + std::string(str->string_view()); |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 308 | |
Milind Upadhyay | 17098ba | 2022-04-15 22:18:50 -0700 | [diff] [blame] | 309 | const auto optional_config = |
| 310 | MaybeReadConfig(included_config, visited_paths, extra_import_paths); |
| 311 | if (!optional_config.has_value()) { |
| 312 | return std::nullopt; |
| 313 | } |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 314 | // And them merge everything in. |
Milind Upadhyay | 17098ba | 2022-04-15 22:18:50 -0700 | [diff] [blame] | 315 | merged_config = MergeFlatBuffers(merged_config, *optional_config); |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 316 | } |
| 317 | |
| 318 | // Finally, merge this file in. |
Milind Upadhyay | 17098ba | 2022-04-15 22:18:50 -0700 | [diff] [blame] | 319 | config = MergeFlatBuffers(merged_config, *config); |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 320 | } |
| 321 | return config; |
| 322 | } |
| 323 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 324 | // Compares (c < p) a channel, and a name, type tuple. |
| 325 | bool CompareChannels(const Channel *c, |
James Kuszmaul | 3ae4226 | 2019-11-08 12:33:41 -0800 | [diff] [blame] | 326 | ::std::pair<std::string_view, std::string_view> p) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 327 | int name_compare = c->name()->string_view().compare(p.first); |
| 328 | if (name_compare == 0) { |
| 329 | return c->type()->string_view() < p.second; |
| 330 | } else if (name_compare < 0) { |
| 331 | return true; |
| 332 | } else { |
| 333 | return false; |
| 334 | } |
| 335 | }; |
| 336 | |
| 337 | // Compares for equality (c == p) a channel, and a name, type tuple. |
| 338 | bool EqualsChannels(const Channel *c, |
Austin Schuh | f1fff28 | 2020-03-28 16:57:32 -0700 | [diff] [blame] | 339 | ::std::pair<std::string_view, std::string_view> p) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 340 | return c->name()->string_view() == p.first && |
| 341 | c->type()->string_view() == p.second; |
| 342 | } |
| 343 | |
| 344 | // Compares (c < p) an application, and a name; |
James Kuszmaul | 3ae4226 | 2019-11-08 12:33:41 -0800 | [diff] [blame] | 345 | bool CompareApplications(const Application *a, std::string_view name) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 346 | return a->name()->string_view() < name; |
| 347 | }; |
| 348 | |
| 349 | // Compares for equality (c == p) an application, and a name; |
James Kuszmaul | 3ae4226 | 2019-11-08 12:33:41 -0800 | [diff] [blame] | 350 | bool EqualsApplications(const Application *a, std::string_view name) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 351 | return a->name()->string_view() == name; |
| 352 | } |
| 353 | |
Austin Schuh | 1518232 | 2020-10-10 15:25:21 -0700 | [diff] [blame] | 354 | void ValidateConfiguration(const Flatbuffer<Configuration> &config) { |
| 355 | // No imports should be left. |
| 356 | CHECK(!config.message().has_imports()); |
| 357 | |
| 358 | // Check that if there is a node list, all the source nodes are filled out and |
| 359 | // valid, and all the destination nodes are valid (and not the source). This |
| 360 | // is a basic consistency check. |
| 361 | if (config.message().has_channels()) { |
| 362 | const Channel *last_channel = nullptr; |
| 363 | for (const Channel *c : *config.message().channels()) { |
| 364 | CHECK(c->has_name()); |
| 365 | CHECK(c->has_type()); |
| 366 | if (c->name()->string_view().back() == '/') { |
| 367 | LOG(FATAL) << "Channel names can't end with '/'"; |
| 368 | } |
Austin Schuh | 47e382e | 2023-05-28 11:20:56 -0700 | [diff] [blame] | 369 | if (c->name()->string_view().front() != '/') { |
| 370 | LOG(FATAL) << "Channel names must start with '/'"; |
| 371 | } |
Austin Schuh | 1518232 | 2020-10-10 15:25:21 -0700 | [diff] [blame] | 372 | if (c->name()->string_view().find("//") != std::string_view::npos) { |
| 373 | LOG(FATAL) << ": Invalid channel name " << c->name()->string_view() |
| 374 | << ", can't use //."; |
| 375 | } |
| 376 | for (const char data : c->name()->string_view()) { |
| 377 | if (data >= '0' && data <= '9') { |
| 378 | continue; |
| 379 | } |
| 380 | if (data >= 'a' && data <= 'z') { |
| 381 | continue; |
| 382 | } |
| 383 | if (data >= 'A' && data <= 'Z') { |
| 384 | continue; |
| 385 | } |
| 386 | if (data == '-' || data == '_' || data == '/') { |
| 387 | continue; |
| 388 | } |
| 389 | LOG(FATAL) << "Invalid channel name " << c->name()->string_view() |
| 390 | << ", can only use [-a-zA-Z0-9_/]"; |
| 391 | } |
| 392 | |
Austin Schuh | fb37c61 | 2022-08-11 15:24:51 -0700 | [diff] [blame] | 393 | CHECK_LT(QueueSize(&config.message(), c) + QueueScratchBufferSize(c), |
Austin Schuh | 83cbb1e | 2023-06-23 12:59:02 -0700 | [diff] [blame] | 394 | FLAGS_max_queue_size_override != 0 |
| 395 | ? FLAGS_max_queue_size_override |
| 396 | : std::numeric_limits< |
| 397 | ipc_lib::QueueIndex::PackedIndexType>::max()) |
Austin Schuh | fb37c61 | 2022-08-11 15:24:51 -0700 | [diff] [blame] | 398 | << ": More messages/second configured than the queue can hold on " |
| 399 | << CleanedChannelToString(c) << ", " << c->frequency() << "hz for " |
Austin Schuh | fff9c3a | 2023-06-16 18:48:23 -0700 | [diff] [blame] | 400 | << ChannelStorageDuration(&config.message(), c).count() << "ns"; |
Austin Schuh | fb37c61 | 2022-08-11 15:24:51 -0700 | [diff] [blame] | 401 | |
Austin Schuh | a156fb2 | 2021-10-11 19:23:21 -0700 | [diff] [blame] | 402 | if (c->has_logger_nodes()) { |
| 403 | // Confirm that we don't have duplicate logger nodes. |
| 404 | absl::btree_set<std::string_view> logger_nodes; |
| 405 | for (const flatbuffers::String *s : *c->logger_nodes()) { |
| 406 | logger_nodes.insert(s->string_view()); |
| 407 | } |
| 408 | CHECK_EQ(static_cast<size_t>(logger_nodes.size()), |
| 409 | c->logger_nodes()->size()) |
| 410 | << ": Found duplicate logger_nodes in " |
| 411 | << CleanedChannelToString(c); |
| 412 | } |
| 413 | |
| 414 | if (c->has_destination_nodes()) { |
| 415 | // Confirm that we don't have duplicate timestamp logger nodes. |
Austin Schuh | 5e95bd6 | 2021-10-11 18:40:22 -0700 | [diff] [blame] | 416 | for (const Connection *d : *c->destination_nodes()) { |
Austin Schuh | a156fb2 | 2021-10-11 19:23:21 -0700 | [diff] [blame] | 417 | if (d->has_timestamp_logger_nodes()) { |
| 418 | absl::btree_set<std::string_view> timestamp_logger_nodes; |
| 419 | for (const flatbuffers::String *s : *d->timestamp_logger_nodes()) { |
| 420 | timestamp_logger_nodes.insert(s->string_view()); |
| 421 | } |
| 422 | CHECK_EQ(static_cast<size_t>(timestamp_logger_nodes.size()), |
| 423 | d->timestamp_logger_nodes()->size()) |
| 424 | << ": Found duplicate timestamp_logger_nodes in " |
| 425 | << CleanedChannelToString(c); |
| 426 | } |
| 427 | } |
| 428 | |
| 429 | // There is no good use case today for logging timestamps but not the |
| 430 | // corresponding data. Instead of plumbing through all of this on the |
| 431 | // reader side, let'd just disallow it for now. |
| 432 | if (c->logger() == LoggerConfig::NOT_LOGGED) { |
| 433 | for (const Connection *d : *c->destination_nodes()) { |
| 434 | CHECK(d->timestamp_logger() == LoggerConfig::NOT_LOGGED) |
| 435 | << ": Logging timestamps without data is not supported. If " |
| 436 | "you have a good use case, let's talk. " |
| 437 | << CleanedChannelToString(c); |
| 438 | } |
Austin Schuh | 5e95bd6 | 2021-10-11 18:40:22 -0700 | [diff] [blame] | 439 | } |
| 440 | } |
| 441 | |
Austin Schuh | 1518232 | 2020-10-10 15:25:21 -0700 | [diff] [blame] | 442 | // Make sure everything is sorted while we are here... If this fails, |
| 443 | // there will be a bunch of weird errors. |
| 444 | if (last_channel != nullptr) { |
| 445 | CHECK(CompareChannels( |
| 446 | last_channel, |
| 447 | std::make_pair(c->name()->string_view(), c->type()->string_view()))) |
| 448 | << ": Channels not sorted!"; |
| 449 | } |
| 450 | last_channel = c; |
| 451 | } |
| 452 | } |
| 453 | |
| 454 | if (config.message().has_nodes() && config.message().has_channels()) { |
| 455 | for (const Channel *c : *config.message().channels()) { |
| 456 | CHECK(c->has_source_node()) << ": Channel " << FlatbufferToJson(c) |
| 457 | << " is missing \"source_node\""; |
| 458 | CHECK(GetNode(&config.message(), c->source_node()->string_view()) != |
| 459 | nullptr) |
| 460 | << ": Channel " << FlatbufferToJson(c) |
| 461 | << " has an unknown \"source_node\""; |
| 462 | |
| 463 | if (c->has_destination_nodes()) { |
| 464 | for (const Connection *connection : *c->destination_nodes()) { |
| 465 | CHECK(connection->has_name()); |
| 466 | CHECK(GetNode(&config.message(), connection->name()->string_view()) != |
| 467 | nullptr) |
| 468 | << ": Channel " << FlatbufferToJson(c) |
| 469 | << " has an unknown \"destination_nodes\" " |
| 470 | << connection->name()->string_view(); |
| 471 | |
| 472 | switch (connection->timestamp_logger()) { |
| 473 | case LoggerConfig::LOCAL_LOGGER: |
| 474 | case LoggerConfig::NOT_LOGGED: |
Austin Schuh | b98d02d | 2022-08-16 13:27:25 -0700 | [diff] [blame] | 475 | CHECK(!connection->has_timestamp_logger_nodes()) |
| 476 | << ": " << CleanedChannelToString(c); |
Austin Schuh | 1518232 | 2020-10-10 15:25:21 -0700 | [diff] [blame] | 477 | break; |
| 478 | case LoggerConfig::REMOTE_LOGGER: |
| 479 | case LoggerConfig::LOCAL_AND_REMOTE_LOGGER: |
| 480 | CHECK(connection->has_timestamp_logger_nodes()); |
| 481 | CHECK_GT(connection->timestamp_logger_nodes()->size(), 0u); |
| 482 | for (const flatbuffers::String *timestamp_logger_node : |
| 483 | *connection->timestamp_logger_nodes()) { |
| 484 | CHECK(GetNode(&config.message(), |
| 485 | timestamp_logger_node->string_view()) != nullptr) |
| 486 | << ": Channel " << FlatbufferToJson(c) |
| 487 | << " has an unknown \"timestamp_logger_node\"" |
| 488 | << connection->name()->string_view(); |
| 489 | } |
| 490 | break; |
| 491 | } |
| 492 | |
| 493 | CHECK_NE(connection->name()->string_view(), |
| 494 | c->source_node()->string_view()) |
| 495 | << ": Channel " << FlatbufferToJson(c) |
| 496 | << " is forwarding data to itself"; |
| 497 | } |
| 498 | } |
| 499 | } |
| 500 | } |
| 501 | } |
| 502 | |
James Kuszmaul | c8503f3 | 2022-06-25 16:17:12 -0700 | [diff] [blame] | 503 | void HandleReverseMaps( |
| 504 | const flatbuffers::Vector<flatbuffers::Offset<aos::Map>> *maps, |
| 505 | std::string_view type, const Node *node, std::set<std::string> *names) { |
| 506 | for (const Map *map : *maps) { |
| 507 | CHECK_NOTNULL(map); |
| 508 | const Channel *const match = CHECK_NOTNULL(map->match()); |
| 509 | const Channel *const rename = CHECK_NOTNULL(map->rename()); |
| 510 | |
| 511 | // Handle type specific maps. |
| 512 | const flatbuffers::String *const match_type_string = match->type(); |
| 513 | if (match_type_string != nullptr && |
| 514 | match_type_string->string_view() != type) { |
| 515 | continue; |
| 516 | } |
| 517 | |
| 518 | // Now handle node specific maps. |
| 519 | const flatbuffers::String *const match_source_node_string = |
| 520 | match->source_node(); |
| 521 | if (node != nullptr && match_source_node_string != nullptr && |
| 522 | match_source_node_string->string_view() != |
| 523 | node->name()->string_view()) { |
| 524 | continue; |
| 525 | } |
| 526 | |
| 527 | const flatbuffers::String *const match_name_string = match->name(); |
| 528 | const flatbuffers::String *const rename_name_string = rename->name(); |
| 529 | if (match_name_string == nullptr || rename_name_string == nullptr) { |
| 530 | continue; |
| 531 | } |
| 532 | |
| 533 | const std::string rename_name = rename_name_string->str(); |
| 534 | const std::string_view match_name = match_name_string->string_view(); |
| 535 | |
| 536 | std::set<std::string> possible_renames; |
| 537 | |
| 538 | // Check if the current name(s) could have been reached using the provided |
| 539 | // rename. |
| 540 | if (match_name.back() == '*') { |
| 541 | for (const std::string &option : *names) { |
| 542 | if (option.substr(0, rename_name.size()) == rename_name) { |
| 543 | possible_renames.insert( |
| 544 | absl::StrCat(match_name.substr(0, match_name.size() - 1), |
| 545 | option.substr(rename_name.size()))); |
| 546 | } |
| 547 | } |
| 548 | names->insert(possible_renames.begin(), possible_renames.end()); |
| 549 | } else if (names->count(rename_name) != 0) { |
| 550 | names->insert(std::string(match_name)); |
| 551 | } |
| 552 | } |
| 553 | } |
| 554 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 555 | } // namespace |
| 556 | |
Austin Schuh | 006a9f5 | 2021-04-07 16:24:18 -0700 | [diff] [blame] | 557 | // Maps name for the provided maps. Modifies name. |
Brian Silverman | f3798cb | 2021-11-10 12:26:34 -0800 | [diff] [blame] | 558 | // |
| 559 | // This is called many times during startup, and it dereferences a lot of |
| 560 | // pointers. These combine to make it a performance hotspot during many tests |
| 561 | // under msan, so there is some optimizing around caching intermediates instead |
| 562 | // of dereferencing the pointer multiple times. |
James Kuszmaul | c8503f3 | 2022-06-25 16:17:12 -0700 | [diff] [blame] | 563 | // |
| 564 | // Deliberately not in an anonymous namespace so that the log-reading code can |
| 565 | // reference it. |
Austin Schuh | 006a9f5 | 2021-04-07 16:24:18 -0700 | [diff] [blame] | 566 | void HandleMaps(const flatbuffers::Vector<flatbuffers::Offset<aos::Map>> *maps, |
| 567 | std::string *name, std::string_view type, const Node *node) { |
| 568 | // For the same reason we merge configs in reverse order, we want to process |
| 569 | // maps in reverse order. That lets the outer config overwrite channels from |
| 570 | // the inner configs. |
| 571 | for (auto i = maps->rbegin(); i != maps->rend(); ++i) { |
Brian Silverman | f3798cb | 2021-11-10 12:26:34 -0800 | [diff] [blame] | 572 | const Channel *const match = i->match(); |
| 573 | if (!match) { |
Austin Schuh | 006a9f5 | 2021-04-07 16:24:18 -0700 | [diff] [blame] | 574 | continue; |
| 575 | } |
Brian Silverman | f3798cb | 2021-11-10 12:26:34 -0800 | [diff] [blame] | 576 | const flatbuffers::String *const match_name_string = match->name(); |
| 577 | if (!match_name_string) { |
| 578 | continue; |
| 579 | } |
| 580 | const Channel *const rename = i->rename(); |
| 581 | if (!rename) { |
| 582 | continue; |
| 583 | } |
| 584 | const flatbuffers::String *const rename_name_string = rename->name(); |
| 585 | if (!rename_name_string) { |
Austin Schuh | 006a9f5 | 2021-04-07 16:24:18 -0700 | [diff] [blame] | 586 | continue; |
| 587 | } |
| 588 | |
| 589 | // Handle normal maps (now that we know that match and rename are filled |
| 590 | // out). |
Brian Silverman | f3798cb | 2021-11-10 12:26:34 -0800 | [diff] [blame] | 591 | const std::string_view match_name = match_name_string->string_view(); |
Austin Schuh | 006a9f5 | 2021-04-07 16:24:18 -0700 | [diff] [blame] | 592 | if (match_name != *name) { |
| 593 | if (match_name.back() == '*' && |
| 594 | std::string_view(*name).substr( |
| 595 | 0, std::min(name->size(), match_name.size() - 1)) == |
| 596 | match_name.substr(0, match_name.size() - 1)) { |
| 597 | CHECK_EQ(match_name.find('*'), match_name.size() - 1); |
| 598 | } else { |
| 599 | continue; |
| 600 | } |
| 601 | } |
| 602 | |
| 603 | // Handle type specific maps. |
Brian Silverman | f3798cb | 2021-11-10 12:26:34 -0800 | [diff] [blame] | 604 | const flatbuffers::String *const match_type_string = match->type(); |
| 605 | if (match_type_string && match_type_string->string_view() != type) { |
Austin Schuh | 006a9f5 | 2021-04-07 16:24:18 -0700 | [diff] [blame] | 606 | continue; |
| 607 | } |
| 608 | |
| 609 | // Now handle node specific maps. |
Brian Silverman | f3798cb | 2021-11-10 12:26:34 -0800 | [diff] [blame] | 610 | const flatbuffers::String *const match_source_node_string = |
| 611 | match->source_node(); |
| 612 | if (node && match_source_node_string && |
| 613 | match_source_node_string->string_view() != |
Austin Schuh | 006a9f5 | 2021-04-07 16:24:18 -0700 | [diff] [blame] | 614 | node->name()->string_view()) { |
| 615 | continue; |
| 616 | } |
| 617 | |
Brian Silverman | f3798cb | 2021-11-10 12:26:34 -0800 | [diff] [blame] | 618 | std::string new_name(rename_name_string->string_view()); |
Austin Schuh | 006a9f5 | 2021-04-07 16:24:18 -0700 | [diff] [blame] | 619 | if (match_name.back() == '*') { |
| 620 | new_name += std::string(name->substr(match_name.size() - 1)); |
| 621 | } |
| 622 | VLOG(1) << "Renamed \"" << *name << "\" to \"" << new_name << "\""; |
| 623 | *name = std::move(new_name); |
| 624 | } |
| 625 | } |
| 626 | |
James Kuszmaul | c8503f3 | 2022-06-25 16:17:12 -0700 | [diff] [blame] | 627 | std::set<std::string> GetChannelAliases(const Configuration *config, |
| 628 | std::string_view name, |
| 629 | std::string_view type, |
| 630 | const std::string_view application_name, |
| 631 | const Node *node) { |
| 632 | std::set<std::string> names{std::string(name)}; |
| 633 | if (config->has_maps()) { |
| 634 | HandleReverseMaps(config->maps(), type, node, &names); |
| 635 | } |
| 636 | { |
| 637 | const Application *application = |
| 638 | GetApplication(config, node, application_name); |
| 639 | if (application != nullptr && application->has_maps()) { |
| 640 | HandleReverseMaps(application->maps(), type, node, &names); |
| 641 | } |
| 642 | } |
| 643 | return names; |
| 644 | } |
| 645 | |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 646 | FlatbufferDetachedBuffer<Configuration> MergeConfiguration( |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 647 | const Flatbuffer<Configuration> &config) { |
James Kuszmaul | 3c99859 | 2020-07-27 21:04:47 -0700 | [diff] [blame] | 648 | // auto_merge_config will contain all the fields of the Configuration that are |
| 649 | // to be passed through unmodified to the result of MergeConfiguration(). |
| 650 | // In the processing below, we mutate auto_merge_config to remove any fields |
| 651 | // which we do need to alter (hence why we can't use the input config |
| 652 | // directly), and then merge auto_merge_config back in at the end. |
| 653 | aos::FlatbufferDetachedBuffer<aos::Configuration> auto_merge_config = |
Austin Schuh | a4fc60f | 2020-11-01 23:06:47 -0800 | [diff] [blame] | 654 | aos::RecursiveCopyFlatBuffer(&config.message()); |
James Kuszmaul | 3c99859 | 2020-07-27 21:04:47 -0700 | [diff] [blame] | 655 | |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 656 | // Store all the channels in a sorted set. This lets us track channels we |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 657 | // have seen before and merge the updates in. |
Adam Snaider | 13d48d9 | 2023-08-03 12:20:15 -0700 | [diff] [blame] | 658 | absl::btree_set<ChannelContainer> channels; |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 659 | |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 660 | if (config.message().has_channels()) { |
James Kuszmaul | 3c99859 | 2020-07-27 21:04:47 -0700 | [diff] [blame] | 661 | auto_merge_config.mutable_message()->clear_channels(); |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 662 | for (const Channel *c : *config.message().channels()) { |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 663 | // Ignore malformed entries. |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 664 | if (!c->has_name()) { |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 665 | continue; |
| 666 | } |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 667 | if (!c->has_type()) { |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 668 | continue; |
| 669 | } |
| 670 | |
Brian Silverman | 7716297 | 2020-08-12 19:52:40 -0700 | [diff] [blame] | 671 | CHECK_EQ(c->read_method() == ReadMethod::PIN, c->num_readers() != 0) |
| 672 | << ": num_readers may be set if and only if read_method is PIN," |
| 673 | " if you want 0 readers do not set PIN: " |
| 674 | << CleanedChannelToString(c); |
| 675 | |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 676 | // Attempt to insert the channel. |
Austin Schuh | a4fc60f | 2020-11-01 23:06:47 -0800 | [diff] [blame] | 677 | auto result = channels.insert(RecursiveCopyFlatBuffer(c)); |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 678 | if (!result.second) { |
| 679 | // Already there, so merge the new table into the original. |
Austin Schuh | 4a5f5d2 | 2021-10-12 15:09:35 -0700 | [diff] [blame] | 680 | // Schemas merge poorly, so pick the newest one. |
Adam Snaider | 13d48d9 | 2023-08-03 12:20:15 -0700 | [diff] [blame] | 681 | if (result.first->table->message().has_schema() && c->has_schema()) { |
| 682 | result.first->table->mutable_message()->clear_schema(); |
Austin Schuh | 4a5f5d2 | 2021-10-12 15:09:35 -0700 | [diff] [blame] | 683 | } |
Austin Schuh | a7996eb | 2021-10-11 19:03:24 -0700 | [diff] [blame] | 684 | auto merged = |
Adam Snaider | 13d48d9 | 2023-08-03 12:20:15 -0700 | [diff] [blame] | 685 | MergeFlatBuffers(*result.first->table, RecursiveCopyFlatBuffer(c)); |
Austin Schuh | a7996eb | 2021-10-11 19:03:24 -0700 | [diff] [blame] | 686 | |
| 687 | if (merged.message().has_destination_nodes()) { |
Adam Snaider | 13d48d9 | 2023-08-03 12:20:15 -0700 | [diff] [blame] | 688 | absl::btree_set<ConnectionContainer> connections; |
Austin Schuh | a7996eb | 2021-10-11 19:03:24 -0700 | [diff] [blame] | 689 | for (const Connection *connection : |
| 690 | *merged.message().destination_nodes()) { |
| 691 | auto connection_result = |
| 692 | connections.insert(RecursiveCopyFlatBuffer(connection)); |
| 693 | if (!connection_result.second) { |
Adam Snaider | 13d48d9 | 2023-08-03 12:20:15 -0700 | [diff] [blame] | 694 | *connection_result.first->table = |
| 695 | MergeFlatBuffers(*connection_result.first->table, |
Austin Schuh | a7996eb | 2021-10-11 19:03:24 -0700 | [diff] [blame] | 696 | RecursiveCopyFlatBuffer(connection)); |
| 697 | } |
| 698 | } |
| 699 | if (static_cast<size_t>(connections.size()) != |
| 700 | merged.message().destination_nodes()->size()) { |
| 701 | merged.mutable_message()->clear_destination_nodes(); |
| 702 | flatbuffers::FlatBufferBuilder fbb; |
| 703 | fbb.ForceDefaults(true); |
| 704 | std::vector<flatbuffers::Offset<Connection>> connection_offsets; |
Adam Snaider | 13d48d9 | 2023-08-03 12:20:15 -0700 | [diff] [blame] | 705 | for (const ConnectionContainer &connection : connections) { |
Austin Schuh | a7996eb | 2021-10-11 19:03:24 -0700 | [diff] [blame] | 706 | connection_offsets.push_back( |
Adam Snaider | 13d48d9 | 2023-08-03 12:20:15 -0700 | [diff] [blame] | 707 | RecursiveCopyFlatBuffer(&connection.table->message(), &fbb)); |
Austin Schuh | a7996eb | 2021-10-11 19:03:24 -0700 | [diff] [blame] | 708 | } |
| 709 | flatbuffers::Offset< |
| 710 | flatbuffers::Vector<flatbuffers::Offset<Connection>>> |
| 711 | destination_nodes_offset = fbb.CreateVector(connection_offsets); |
| 712 | Channel::Builder channel_builder(fbb); |
| 713 | channel_builder.add_destination_nodes(destination_nodes_offset); |
| 714 | fbb.Finish(channel_builder.Finish()); |
| 715 | FlatbufferDetachedBuffer<Channel> destinations_channel( |
| 716 | fbb.Release()); |
| 717 | merged = MergeFlatBuffers(merged, destinations_channel); |
| 718 | } |
| 719 | } |
| 720 | |
Adam Snaider | 13d48d9 | 2023-08-03 12:20:15 -0700 | [diff] [blame] | 721 | *result.first->table = std::move(merged); |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 722 | } |
| 723 | } |
| 724 | } |
| 725 | |
| 726 | // Now repeat this for the application list. |
Adam Snaider | 13d48d9 | 2023-08-03 12:20:15 -0700 | [diff] [blame] | 727 | absl::btree_set<ApplicationContainer> applications; |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 728 | if (config.message().has_applications()) { |
James Kuszmaul | 3c99859 | 2020-07-27 21:04:47 -0700 | [diff] [blame] | 729 | auto_merge_config.mutable_message()->clear_applications(); |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 730 | for (const Application *a : *config.message().applications()) { |
| 731 | if (!a->has_name()) { |
| 732 | continue; |
| 733 | } |
| 734 | |
Austin Schuh | a4fc60f | 2020-11-01 23:06:47 -0800 | [diff] [blame] | 735 | auto result = applications.insert(RecursiveCopyFlatBuffer(a)); |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 736 | if (!result.second) { |
Austin Schuh | f81c252 | 2021-12-08 12:03:30 -0800 | [diff] [blame] | 737 | if (a->has_args()) { |
Adam Snaider | 13d48d9 | 2023-08-03 12:20:15 -0700 | [diff] [blame] | 738 | result.first->table->mutable_message()->clear_args(); |
Austin Schuh | f81c252 | 2021-12-08 12:03:30 -0800 | [diff] [blame] | 739 | } |
Adam Snaider | 13d48d9 | 2023-08-03 12:20:15 -0700 | [diff] [blame] | 740 | *result.first->table = |
| 741 | MergeFlatBuffers(*result.first->table, RecursiveCopyFlatBuffer(a)); |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 742 | } |
| 743 | } |
| 744 | } |
| 745 | |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 746 | // Now repeat this for the node list. |
Adam Snaider | 13d48d9 | 2023-08-03 12:20:15 -0700 | [diff] [blame] | 747 | absl::btree_set<NodeContainer> nodes; |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 748 | if (config.message().has_nodes()) { |
James Kuszmaul | 3c99859 | 2020-07-27 21:04:47 -0700 | [diff] [blame] | 749 | auto_merge_config.mutable_message()->clear_nodes(); |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 750 | for (const Node *n : *config.message().nodes()) { |
| 751 | if (!n->has_name()) { |
| 752 | continue; |
| 753 | } |
| 754 | |
Austin Schuh | a4fc60f | 2020-11-01 23:06:47 -0800 | [diff] [blame] | 755 | auto result = nodes.insert(RecursiveCopyFlatBuffer(n)); |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 756 | if (!result.second) { |
Adam Snaider | 13d48d9 | 2023-08-03 12:20:15 -0700 | [diff] [blame] | 757 | *result.first->table = |
| 758 | MergeFlatBuffers(*result.first->table, RecursiveCopyFlatBuffer(n)); |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 759 | } |
| 760 | } |
| 761 | } |
| 762 | |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 763 | flatbuffers::FlatBufferBuilder fbb; |
Austin Schuh | d7b15da | 2020-02-17 15:06:11 -0800 | [diff] [blame] | 764 | fbb.ForceDefaults(true); |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 765 | |
| 766 | // Start by building the vectors. They need to come before the final table. |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 767 | // Channels |
| 768 | flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<Channel>>> |
| 769 | channels_offset; |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 770 | { |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 771 | ::std::vector<flatbuffers::Offset<Channel>> channel_offsets; |
Adam Snaider | 13d48d9 | 2023-08-03 12:20:15 -0700 | [diff] [blame] | 772 | for (const ChannelContainer &c : channels) { |
Austin Schuh | a4fc60f | 2020-11-01 23:06:47 -0800 | [diff] [blame] | 773 | channel_offsets.emplace_back( |
Adam Snaider | 13d48d9 | 2023-08-03 12:20:15 -0700 | [diff] [blame] | 774 | RecursiveCopyFlatBuffer<Channel>(&c.table->message(), &fbb)); |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 775 | } |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 776 | channels_offset = fbb.CreateVector(channel_offsets); |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 777 | } |
| 778 | |
| 779 | // Applications |
| 780 | flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<Application>>> |
| 781 | applications_offset; |
| 782 | { |
| 783 | ::std::vector<flatbuffers::Offset<Application>> applications_offsets; |
Adam Snaider | 13d48d9 | 2023-08-03 12:20:15 -0700 | [diff] [blame] | 784 | for (const ApplicationContainer &a : applications) { |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 785 | applications_offsets.emplace_back( |
Adam Snaider | 13d48d9 | 2023-08-03 12:20:15 -0700 | [diff] [blame] | 786 | RecursiveCopyFlatBuffer<Application>(&a.table->message(), &fbb)); |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 787 | } |
| 788 | applications_offset = fbb.CreateVector(applications_offsets); |
| 789 | } |
| 790 | |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 791 | // Nodes |
| 792 | flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<Node>>> |
| 793 | nodes_offset; |
| 794 | { |
| 795 | ::std::vector<flatbuffers::Offset<Node>> node_offsets; |
Adam Snaider | 13d48d9 | 2023-08-03 12:20:15 -0700 | [diff] [blame] | 796 | for (const NodeContainer &n : nodes) { |
Austin Schuh | a4fc60f | 2020-11-01 23:06:47 -0800 | [diff] [blame] | 797 | node_offsets.emplace_back( |
Adam Snaider | 13d48d9 | 2023-08-03 12:20:15 -0700 | [diff] [blame] | 798 | RecursiveCopyFlatBuffer<Node>(&n.table->message(), &fbb)); |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 799 | } |
| 800 | nodes_offset = fbb.CreateVector(node_offsets); |
| 801 | } |
| 802 | |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 803 | // And then build a Configuration with them all. |
| 804 | ConfigurationBuilder configuration_builder(fbb); |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 805 | configuration_builder.add_channels(channels_offset); |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 806 | if (config.message().has_applications()) { |
| 807 | configuration_builder.add_applications(applications_offset); |
| 808 | } |
| 809 | if (config.message().has_nodes()) { |
| 810 | configuration_builder.add_nodes(nodes_offset); |
| 811 | } |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 812 | |
| 813 | fbb.Finish(configuration_builder.Finish()); |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 814 | |
James Kuszmaul | 3c99859 | 2020-07-27 21:04:47 -0700 | [diff] [blame] | 815 | aos::FlatbufferDetachedBuffer<aos::Configuration> modified_config( |
| 816 | fbb.Release()); |
| 817 | |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 818 | // Now, validate that if there is a node list, every channel has a source |
| 819 | // node. |
James Kuszmaul | 3c99859 | 2020-07-27 21:04:47 -0700 | [diff] [blame] | 820 | FlatbufferDetachedBuffer<Configuration> result = |
| 821 | MergeFlatBuffers(modified_config, auto_merge_config); |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 822 | |
Austin Schuh | 1518232 | 2020-10-10 15:25:21 -0700 | [diff] [blame] | 823 | ValidateConfiguration(result); |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 824 | |
| 825 | return result; |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 826 | } |
| 827 | |
Milind Upadhyay | 17098ba | 2022-04-15 22:18:50 -0700 | [diff] [blame] | 828 | std::optional<FlatbufferDetachedBuffer<Configuration>> MaybeReadConfig( |
James Kuszmaul | c0c08da | 2020-05-10 18:56:07 -0700 | [diff] [blame] | 829 | const std::string_view path, |
Austin Schuh | ef38cd2 | 2021-07-21 15:24:23 -0700 | [diff] [blame] | 830 | const std::vector<std::string_view> &extra_import_paths) { |
Austin Schuh | 89026f5 | 2022-02-25 14:24:04 -0800 | [diff] [blame] | 831 | // Add the executable directory to the search path. That makes it so that |
| 832 | // tools can be run from any directory without hard-coding an absolute path to |
| 833 | // the config into all binaries. |
| 834 | std::vector<std::string_view> extra_import_paths_with_exe = |
| 835 | extra_import_paths; |
| 836 | char proc_self_exec_buffer[PATH_MAX + 1]; |
| 837 | std::memset(proc_self_exec_buffer, 0, sizeof(proc_self_exec_buffer)); |
| 838 | ssize_t s = readlink("/proc/self/exe", proc_self_exec_buffer, PATH_MAX); |
| 839 | if (s > 0) { |
| 840 | // If the readlink call fails, the worst thing that happens is that we don't |
| 841 | // automatically find the config next to the binary. VLOG to make it easier |
| 842 | // to debug. |
| 843 | std::string_view proc_self_exec(proc_self_exec_buffer); |
| 844 | |
| 845 | extra_import_paths_with_exe.emplace_back( |
| 846 | proc_self_exec.substr(0, proc_self_exec.rfind("/"))); |
| 847 | } else { |
| 848 | VLOG(1) << "Failed to read /proc/self/exe"; |
| 849 | } |
| 850 | |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 851 | // We only want to read a file once. So track the visited files in a set. |
| 852 | absl::btree_set<std::string> visited_paths; |
Milind Upadhyay | 17098ba | 2022-04-15 22:18:50 -0700 | [diff] [blame] | 853 | std::optional<FlatbufferDetachedBuffer<Configuration>> read_config = |
| 854 | MaybeReadConfig(path, &visited_paths, extra_import_paths_with_exe); |
| 855 | |
| 856 | if (read_config == std::nullopt) { |
| 857 | return read_config; |
| 858 | } |
Austin Schuh | 1518232 | 2020-10-10 15:25:21 -0700 | [diff] [blame] | 859 | |
| 860 | // If we only read one file, and it had a .bfbs extension, it has to be a |
| 861 | // fully formatted config. Do a quick verification and return it. |
| 862 | if (visited_paths.size() == 1 && EndsWith(*visited_paths.begin(), ".bfbs")) { |
Milind Upadhyay | 17098ba | 2022-04-15 22:18:50 -0700 | [diff] [blame] | 863 | ValidateConfiguration(*read_config); |
Austin Schuh | 1518232 | 2020-10-10 15:25:21 -0700 | [diff] [blame] | 864 | return read_config; |
| 865 | } |
| 866 | |
Milind Upadhyay | 17098ba | 2022-04-15 22:18:50 -0700 | [diff] [blame] | 867 | return MergeConfiguration(*read_config); |
| 868 | } |
| 869 | |
| 870 | FlatbufferDetachedBuffer<Configuration> ReadConfig( |
| 871 | const std::string_view path, |
| 872 | const std::vector<std::string_view> &extra_import_paths) { |
| 873 | auto optional_config = MaybeReadConfig(path, extra_import_paths); |
| 874 | CHECK(optional_config) << "Could not read config. See above errors"; |
| 875 | return std::move(*optional_config); |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 876 | } |
| 877 | |
Austin Schuh | 8d6cea8 | 2020-02-28 12:17:16 -0800 | [diff] [blame] | 878 | FlatbufferDetachedBuffer<Configuration> MergeWithConfig( |
Brian Silverman | 24f5aa8 | 2020-06-23 16:21:28 -0700 | [diff] [blame] | 879 | const Configuration *config, const Flatbuffer<Configuration> &addition) { |
| 880 | return MergeConfiguration(MergeFlatBuffers(config, &addition.message())); |
| 881 | } |
| 882 | |
| 883 | FlatbufferDetachedBuffer<Configuration> MergeWithConfig( |
Austin Schuh | 8d6cea8 | 2020-02-28 12:17:16 -0800 | [diff] [blame] | 884 | const Configuration *config, std::string_view json) { |
| 885 | FlatbufferDetachedBuffer<Configuration> addition = |
| 886 | JsonToFlatbuffer(json, Configuration::MiniReflectTypeTable()); |
| 887 | |
Brian Silverman | 24f5aa8 | 2020-06-23 16:21:28 -0700 | [diff] [blame] | 888 | return MergeWithConfig(config, addition); |
Austin Schuh | 8d6cea8 | 2020-02-28 12:17:16 -0800 | [diff] [blame] | 889 | } |
| 890 | |
James Kuszmaul | 3ae4226 | 2019-11-08 12:33:41 -0800 | [diff] [blame] | 891 | const Channel *GetChannel(const Configuration *config, std::string_view name, |
| 892 | std::string_view type, |
Austin Schuh | 0de30f3 | 2020-12-06 12:44:28 -0800 | [diff] [blame] | 893 | std::string_view application_name, const Node *node, |
| 894 | bool quiet) { |
Brian Silverman | 9fcf2c7 | 2020-12-21 18:30:58 -0800 | [diff] [blame] | 895 | if (!config->has_channels()) { |
| 896 | return nullptr; |
| 897 | } |
| 898 | |
Austin Schuh | bca6cf0 | 2019-12-22 17:28:34 -0800 | [diff] [blame] | 899 | const std::string_view original_name = name; |
Austin Schuh | f1fff28 | 2020-03-28 16:57:32 -0700 | [diff] [blame] | 900 | std::string mutable_name; |
Austin Schuh | 4c3b970 | 2020-08-30 11:34:55 -0700 | [diff] [blame] | 901 | if (node != nullptr) { |
| 902 | VLOG(1) << "Looking up { \"name\": \"" << name << "\", \"type\": \"" << type |
| 903 | << "\" } on " << aos::FlatbufferToJson(node); |
| 904 | } else { |
| 905 | VLOG(1) << "Looking up { \"name\": \"" << name << "\", \"type\": \"" << type |
| 906 | << "\" }"; |
| 907 | } |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 908 | |
| 909 | // First handle application specific maps. Only do this if we have a matching |
| 910 | // application name, and it has maps. |
Austin Schuh | d2e2f6a | 2021-02-07 20:46:16 -0800 | [diff] [blame] | 911 | { |
| 912 | const Application *application = |
| 913 | GetApplication(config, node, application_name); |
| 914 | if (application != nullptr && application->has_maps()) { |
| 915 | mutable_name = std::string(name); |
| 916 | HandleMaps(application->maps(), &mutable_name, type, node); |
| 917 | name = std::string_view(mutable_name); |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 918 | } |
| 919 | } |
| 920 | |
| 921 | // Now do global maps. |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 922 | if (config->has_maps()) { |
Austin Schuh | f1fff28 | 2020-03-28 16:57:32 -0700 | [diff] [blame] | 923 | mutable_name = std::string(name); |
| 924 | HandleMaps(config->maps(), &mutable_name, type, node); |
| 925 | name = std::string_view(mutable_name); |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 926 | } |
| 927 | |
Austin Schuh | bca6cf0 | 2019-12-22 17:28:34 -0800 | [diff] [blame] | 928 | if (original_name != name) { |
| 929 | VLOG(1) << "Remapped to { \"name\": \"" << name << "\", \"type\": \"" |
| 930 | << type << "\" }"; |
| 931 | } |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 932 | |
James Kuszmaul | 71a8193 | 2020-12-15 21:08:01 -0800 | [diff] [blame] | 933 | // Then look for the channel (note that this relies on the channels being |
| 934 | // sorted in the config). |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 935 | auto channel_iterator = |
Austin Schuh | f1fff28 | 2020-03-28 16:57:32 -0700 | [diff] [blame] | 936 | std::lower_bound(config->channels()->cbegin(), config->channels()->cend(), |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 937 | std::make_pair(name, type), CompareChannels); |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 938 | |
| 939 | // Make sure we actually found it, and it matches. |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 940 | if (channel_iterator != config->channels()->cend() && |
| 941 | EqualsChannels(*channel_iterator, std::make_pair(name, type))) { |
Austin Schuh | bca6cf0 | 2019-12-22 17:28:34 -0800 | [diff] [blame] | 942 | if (VLOG_IS_ON(2)) { |
| 943 | VLOG(2) << "Found: " << FlatbufferToJson(*channel_iterator); |
| 944 | } else if (VLOG_IS_ON(1)) { |
| 945 | VLOG(1) << "Found: " << CleanedChannelToString(*channel_iterator); |
| 946 | } |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 947 | return *channel_iterator; |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 948 | } else { |
| 949 | VLOG(1) << "No match for { \"name\": \"" << name << "\", \"type\": \"" |
| 950 | << type << "\" }"; |
Austin Schuh | 0de30f3 | 2020-12-06 12:44:28 -0800 | [diff] [blame] | 951 | if (original_name != name && !quiet) { |
Maxwell Gumley | a28d650 | 2023-11-17 10:43:36 -0700 | [diff] [blame^] | 952 | VLOG(1) << "Remapped from {\"name\": \"" << original_name |
| 953 | << "\", \"type\": \"" << type << "\"}, to {\"name\": \"" << name |
| 954 | << "\", \"type\": \"" << type |
| 955 | << "\"}, but no channel by that name exists."; |
Austin Schuh | 4b42b25 | 2020-10-19 11:35:20 -0700 | [diff] [blame] | 956 | } |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 957 | return nullptr; |
| 958 | } |
| 959 | } |
| 960 | |
Austin Schuh | c9e10ec | 2020-01-26 16:08:28 -0800 | [diff] [blame] | 961 | size_t ChannelIndex(const Configuration *configuration, |
| 962 | const Channel *channel) { |
| 963 | CHECK(configuration->channels() != nullptr) << ": No channels"; |
| 964 | |
Brian Silverman | a9698c9 | 2021-11-10 12:27:04 -0800 | [diff] [blame] | 965 | const auto c = std::lower_bound( |
| 966 | configuration->channels()->cbegin(), configuration->channels()->cend(), |
| 967 | std::make_pair(channel->name()->string_view(), |
| 968 | channel->type()->string_view()), |
| 969 | CompareChannels); |
| 970 | CHECK(c != configuration->channels()->cend()) |
| 971 | << ": Channel pointer not found in configuration()->channels()"; |
| 972 | CHECK(*c == channel) |
Austin Schuh | c9e10ec | 2020-01-26 16:08:28 -0800 | [diff] [blame] | 973 | << ": Channel pointer not found in configuration()->channels()"; |
| 974 | |
Brian Silverman | a9698c9 | 2021-11-10 12:27:04 -0800 | [diff] [blame] | 975 | return std::distance(configuration->channels()->cbegin(), c); |
Austin Schuh | c9e10ec | 2020-01-26 16:08:28 -0800 | [diff] [blame] | 976 | } |
| 977 | |
Austin Schuh | bca6cf0 | 2019-12-22 17:28:34 -0800 | [diff] [blame] | 978 | std::string CleanedChannelToString(const Channel *channel) { |
| 979 | FlatbufferDetachedBuffer<Channel> cleaned_channel = CopyFlatBuffer(channel); |
| 980 | cleaned_channel.mutable_message()->clear_schema(); |
| 981 | return FlatbufferToJson(cleaned_channel); |
| 982 | } |
| 983 | |
Austin Schuh | a81454b | 2020-05-12 19:58:36 -0700 | [diff] [blame] | 984 | std::string StrippedChannelToString(const Channel *channel) { |
| 985 | return absl::StrCat("{ \"name\": \"", channel->name()->string_view(), |
| 986 | "\", \"type\": \"", channel->type()->string_view(), |
| 987 | "\" }"); |
| 988 | } |
| 989 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 990 | FlatbufferDetachedBuffer<Configuration> MergeConfiguration( |
| 991 | const Flatbuffer<Configuration> &config, |
Austin Schuh | 0de30f3 | 2020-12-06 12:44:28 -0800 | [diff] [blame] | 992 | const std::vector<aos::FlatbufferVector<reflection::Schema>> &schemas) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 993 | flatbuffers::FlatBufferBuilder fbb; |
Austin Schuh | d7b15da | 2020-02-17 15:06:11 -0800 | [diff] [blame] | 994 | fbb.ForceDefaults(true); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 995 | |
Austin Schuh | 68d9859 | 2020-11-01 23:22:57 -0800 | [diff] [blame] | 996 | // Cache for holding already inserted schemas. |
| 997 | std::map<std::string_view, flatbuffers::Offset<reflection::Schema>> |
| 998 | schema_cache; |
| 999 | |
Austin Schuh | dda6db7 | 2023-06-21 17:02:34 -0700 | [diff] [blame] | 1000 | CHECK_EQ(Channel::MiniReflectTypeTable()->num_elems, 14u) |
Austin Schuh | 68d9859 | 2020-11-01 23:22:57 -0800 | [diff] [blame] | 1001 | << ": Merging logic needs to be updated when the number of channel " |
| 1002 | "fields changes."; |
James Kuszmaul | 3c99859 | 2020-07-27 21:04:47 -0700 | [diff] [blame] | 1003 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 1004 | flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<Channel>>> |
| 1005 | channels_offset; |
| 1006 | if (config.message().has_channels()) { |
| 1007 | std::vector<flatbuffers::Offset<Channel>> channel_offsets; |
| 1008 | for (const Channel *c : *config.message().channels()) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 1009 | // Search for a schema with a matching type. |
Austin Schuh | 0de30f3 | 2020-12-06 12:44:28 -0800 | [diff] [blame] | 1010 | const aos::FlatbufferVector<reflection::Schema> *found_schema = nullptr; |
| 1011 | for (const aos::FlatbufferVector<reflection::Schema> &schema : schemas) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 1012 | if (schema.message().root_table() != nullptr) { |
| 1013 | if (schema.message().root_table()->name()->string_view() == |
| 1014 | c->type()->string_view()) { |
| 1015 | found_schema = &schema; |
| 1016 | } |
| 1017 | } |
| 1018 | } |
| 1019 | |
Maxwell Gumley | 65d0658 | 2023-03-17 09:13:50 -0600 | [diff] [blame] | 1020 | if (found_schema == nullptr) { |
| 1021 | std::stringstream ss; |
| 1022 | for (const aos::FlatbufferVector<reflection::Schema> &schema : |
| 1023 | schemas) { |
| 1024 | if (schema.message().root_table() == nullptr) { |
| 1025 | continue; |
| 1026 | } |
| 1027 | auto name = schema.message().root_table()->name()->string_view(); |
| 1028 | ss << "\n\tname: " << name; |
| 1029 | } |
| 1030 | LOG(FATAL) << ": Failed to find schema for " << FlatbufferToJson(c) |
| 1031 | << "\n\tThe following schemas were found:\n" |
| 1032 | << ss.str(); |
| 1033 | } |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 1034 | |
Austin Schuh | 68d9859 | 2020-11-01 23:22:57 -0800 | [diff] [blame] | 1035 | // Now copy the message manually. |
| 1036 | auto cached_schema = schema_cache.find(c->type()->string_view()); |
| 1037 | flatbuffers::Offset<reflection::Schema> schema_offset; |
| 1038 | if (cached_schema != schema_cache.end()) { |
| 1039 | schema_offset = cached_schema->second; |
| 1040 | } else { |
Austin Schuh | a4fc60f | 2020-11-01 23:06:47 -0800 | [diff] [blame] | 1041 | schema_offset = RecursiveCopyFlatBuffer<reflection::Schema>( |
| 1042 | &found_schema->message(), &fbb); |
Austin Schuh | 68d9859 | 2020-11-01 23:22:57 -0800 | [diff] [blame] | 1043 | schema_cache.emplace(c->type()->string_view(), schema_offset); |
| 1044 | } |
| 1045 | |
| 1046 | flatbuffers::Offset<flatbuffers::String> name_offset = |
| 1047 | fbb.CreateSharedString(c->name()->str()); |
| 1048 | flatbuffers::Offset<flatbuffers::String> type_offset = |
| 1049 | fbb.CreateSharedString(c->type()->str()); |
| 1050 | flatbuffers::Offset<flatbuffers::String> source_node_offset = |
Austin Schuh | a4fc60f | 2020-11-01 23:06:47 -0800 | [diff] [blame] | 1051 | c->has_source_node() ? fbb.CreateSharedString(c->source_node()->str()) |
| 1052 | : 0; |
Austin Schuh | 68d9859 | 2020-11-01 23:22:57 -0800 | [diff] [blame] | 1053 | |
| 1054 | flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<Connection>>> |
| 1055 | destination_nodes_offset = |
Austin Schuh | 5c255aa | 2020-11-05 18:32:46 -0800 | [diff] [blame] | 1056 | aos::RecursiveCopyVectorTable(c->destination_nodes(), &fbb); |
Austin Schuh | 68d9859 | 2020-11-01 23:22:57 -0800 | [diff] [blame] | 1057 | |
| 1058 | flatbuffers::Offset< |
| 1059 | flatbuffers::Vector<flatbuffers::Offset<flatbuffers::String>>> |
| 1060 | logger_nodes_offset = |
| 1061 | aos::CopyVectorSharedString(c->logger_nodes(), &fbb); |
| 1062 | |
| 1063 | Channel::Builder channel_builder(fbb); |
| 1064 | channel_builder.add_name(name_offset); |
| 1065 | channel_builder.add_type(type_offset); |
| 1066 | if (c->has_frequency()) { |
| 1067 | channel_builder.add_frequency(c->frequency()); |
| 1068 | } |
| 1069 | if (c->has_max_size()) { |
| 1070 | channel_builder.add_max_size(c->max_size()); |
| 1071 | } |
| 1072 | if (c->has_num_senders()) { |
| 1073 | channel_builder.add_num_senders(c->num_senders()); |
| 1074 | } |
| 1075 | if (c->has_num_watchers()) { |
| 1076 | channel_builder.add_num_watchers(c->num_watchers()); |
| 1077 | } |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 1078 | channel_builder.add_schema(schema_offset); |
Austin Schuh | 68d9859 | 2020-11-01 23:22:57 -0800 | [diff] [blame] | 1079 | if (!source_node_offset.IsNull()) { |
| 1080 | channel_builder.add_source_node(source_node_offset); |
| 1081 | } |
| 1082 | if (!destination_nodes_offset.IsNull()) { |
| 1083 | channel_builder.add_destination_nodes(destination_nodes_offset); |
| 1084 | } |
| 1085 | if (c->has_logger()) { |
| 1086 | channel_builder.add_logger(c->logger()); |
| 1087 | } |
| 1088 | if (!logger_nodes_offset.IsNull()) { |
| 1089 | channel_builder.add_logger_nodes(logger_nodes_offset); |
| 1090 | } |
| 1091 | if (c->has_read_method()) { |
| 1092 | channel_builder.add_read_method(c->read_method()); |
| 1093 | } |
| 1094 | if (c->has_num_readers()) { |
| 1095 | channel_builder.add_num_readers(c->num_readers()); |
| 1096 | } |
Austin Schuh | dda6db7 | 2023-06-21 17:02:34 -0700 | [diff] [blame] | 1097 | if (c->has_channel_storage_duration()) { |
| 1098 | channel_builder.add_channel_storage_duration( |
| 1099 | c->channel_storage_duration()); |
| 1100 | } |
Austin Schuh | 68d9859 | 2020-11-01 23:22:57 -0800 | [diff] [blame] | 1101 | channel_offsets.emplace_back(channel_builder.Finish()); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 1102 | } |
| 1103 | channels_offset = fbb.CreateVector(channel_offsets); |
| 1104 | } |
| 1105 | |
Austin Schuh | 68d9859 | 2020-11-01 23:22:57 -0800 | [diff] [blame] | 1106 | flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<Map>>> |
Austin Schuh | 5c255aa | 2020-11-05 18:32:46 -0800 | [diff] [blame] | 1107 | maps_offset = |
| 1108 | aos::RecursiveCopyVectorTable(config.message().maps(), &fbb); |
Austin Schuh | 68d9859 | 2020-11-01 23:22:57 -0800 | [diff] [blame] | 1109 | |
| 1110 | flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<Node>>> |
Austin Schuh | 5c255aa | 2020-11-05 18:32:46 -0800 | [diff] [blame] | 1111 | nodes_offset = |
| 1112 | aos::RecursiveCopyVectorTable(config.message().nodes(), &fbb); |
Austin Schuh | 68d9859 | 2020-11-01 23:22:57 -0800 | [diff] [blame] | 1113 | |
| 1114 | flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<Application>>> |
| 1115 | applications_offset = |
Austin Schuh | 5c255aa | 2020-11-05 18:32:46 -0800 | [diff] [blame] | 1116 | aos::RecursiveCopyVectorTable(config.message().applications(), &fbb); |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 1117 | |
| 1118 | // Now insert everything else in unmodified. |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 1119 | ConfigurationBuilder configuration_builder(fbb); |
| 1120 | if (config.message().has_channels()) { |
| 1121 | configuration_builder.add_channels(channels_offset); |
| 1122 | } |
Austin Schuh | 68d9859 | 2020-11-01 23:22:57 -0800 | [diff] [blame] | 1123 | if (!maps_offset.IsNull()) { |
| 1124 | configuration_builder.add_maps(maps_offset); |
| 1125 | } |
| 1126 | if (!nodes_offset.IsNull()) { |
| 1127 | configuration_builder.add_nodes(nodes_offset); |
| 1128 | } |
| 1129 | if (!applications_offset.IsNull()) { |
| 1130 | configuration_builder.add_applications(applications_offset); |
| 1131 | } |
| 1132 | |
| 1133 | if (config.message().has_channel_storage_duration()) { |
| 1134 | configuration_builder.add_channel_storage_duration( |
| 1135 | config.message().channel_storage_duration()); |
| 1136 | } |
| 1137 | |
| 1138 | CHECK_EQ(Configuration::MiniReflectTypeTable()->num_elems, 6u) |
| 1139 | << ": Merging logic needs to be updated when the number of configuration " |
| 1140 | "fields changes."; |
| 1141 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 1142 | fbb.Finish(configuration_builder.Finish()); |
James Kuszmaul | 3c99859 | 2020-07-27 21:04:47 -0700 | [diff] [blame] | 1143 | aos::FlatbufferDetachedBuffer<aos::Configuration> modified_config( |
| 1144 | fbb.Release()); |
| 1145 | |
Austin Schuh | 68d9859 | 2020-11-01 23:22:57 -0800 | [diff] [blame] | 1146 | return modified_config; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 1147 | } |
| 1148 | |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 1149 | const Node *GetNodeFromHostname(const Configuration *config, |
| 1150 | std::string_view hostname) { |
| 1151 | for (const Node *node : *config->nodes()) { |
Brian Silverman | aa2633f | 2020-02-17 21:04:14 -0800 | [diff] [blame] | 1152 | if (node->has_hostname() && node->hostname()->string_view() == hostname) { |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 1153 | return node; |
| 1154 | } |
Brian Silverman | aa2633f | 2020-02-17 21:04:14 -0800 | [diff] [blame] | 1155 | if (node->has_hostnames()) { |
| 1156 | for (const auto &candidate : *node->hostnames()) { |
| 1157 | if (candidate->string_view() == hostname) { |
| 1158 | return node; |
| 1159 | } |
| 1160 | } |
| 1161 | } |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 1162 | } |
| 1163 | return nullptr; |
| 1164 | } |
Austin Schuh | ac0771c | 2020-01-07 18:36:30 -0800 | [diff] [blame] | 1165 | |
Austin Schuh | 6309726 | 2023-08-16 17:04:29 -0700 | [diff] [blame] | 1166 | std::string_view NodeName(const Configuration *config, size_t node_index) { |
| 1167 | if (!configuration::MultiNode(config)) { |
| 1168 | return "(singlenode)"; |
| 1169 | } |
| 1170 | return config->nodes()->Get(node_index)->name()->string_view(); |
| 1171 | } |
| 1172 | |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 1173 | const Node *GetMyNode(const Configuration *config) { |
| 1174 | const std::string hostname = (FLAGS_override_hostname.size() > 0) |
| 1175 | ? FLAGS_override_hostname |
| 1176 | : network::GetHostname(); |
| 1177 | const Node *node = GetNodeFromHostname(config, hostname); |
| 1178 | if (node != nullptr) return node; |
| 1179 | |
| 1180 | LOG(FATAL) << "Unknown node for host: " << hostname |
| 1181 | << ". Consider using --override_hostname if hostname detection " |
| 1182 | "is wrong."; |
| 1183 | return nullptr; |
| 1184 | } |
| 1185 | |
Austin Schuh | c9e10ec | 2020-01-26 16:08:28 -0800 | [diff] [blame] | 1186 | const Node *GetNode(const Configuration *config, const Node *node) { |
| 1187 | if (!MultiNode(config)) { |
| 1188 | CHECK(node == nullptr) << ": Provided a node in a single node world."; |
| 1189 | return nullptr; |
| 1190 | } else { |
| 1191 | CHECK(node != nullptr); |
| 1192 | CHECK(node->has_name()); |
| 1193 | return GetNode(config, node->name()->string_view()); |
| 1194 | } |
| 1195 | } |
| 1196 | |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 1197 | const Node *GetNode(const Configuration *config, std::string_view name) { |
Alexei Strots | 4b1e144 | 2023-05-01 22:11:05 -0700 | [diff] [blame] | 1198 | if (!MultiNode(config)) { |
| 1199 | if (name.empty()) { |
| 1200 | return nullptr; |
| 1201 | } |
| 1202 | LOG(FATAL) << ": Asking for a named node from a single node configuration."; |
| 1203 | } |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 1204 | for (const Node *node : *config->nodes()) { |
Austin Schuh | fd96062 | 2020-01-01 13:22:55 -0800 | [diff] [blame] | 1205 | CHECK(node->has_name()) << ": Malformed node " << FlatbufferToJson(node); |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 1206 | if (node->name()->string_view() == name) { |
| 1207 | return node; |
| 1208 | } |
| 1209 | } |
| 1210 | return nullptr; |
| 1211 | } |
| 1212 | |
Austin Schuh | 0ca1fd3 | 2020-12-18 22:53:05 -0800 | [diff] [blame] | 1213 | const Node *GetNode(const Configuration *config, size_t node_index) { |
| 1214 | if (!MultiNode(config)) { |
| 1215 | CHECK_EQ(node_index, 0u) << ": Invalid node in a single node world."; |
| 1216 | return nullptr; |
| 1217 | } else { |
| 1218 | CHECK_LT(node_index, config->nodes()->size()); |
| 1219 | return config->nodes()->Get(node_index); |
| 1220 | } |
| 1221 | } |
| 1222 | |
Austin Schuh | c9e10ec | 2020-01-26 16:08:28 -0800 | [diff] [blame] | 1223 | const Node *GetNodeOrDie(const Configuration *config, const Node *node) { |
| 1224 | if (!MultiNode(config)) { |
| 1225 | CHECK(node == nullptr) << ": Provided a node in a single node world."; |
| 1226 | return nullptr; |
| 1227 | } else { |
| 1228 | const Node *config_node = GetNode(config, node); |
| 1229 | if (config_node == nullptr) { |
| 1230 | LOG(FATAL) << "Couldn't find node matching " << FlatbufferToJson(node); |
| 1231 | } |
| 1232 | return config_node; |
| 1233 | } |
| 1234 | } |
| 1235 | |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 1236 | namespace { |
| 1237 | int GetNodeIndexFromConfig(const Configuration *config, const Node *node) { |
Austin Schuh | c9e10ec | 2020-01-26 16:08:28 -0800 | [diff] [blame] | 1238 | int node_index = 0; |
| 1239 | for (const Node *iterated_node : *config->nodes()) { |
| 1240 | if (iterated_node == node) { |
| 1241 | return node_index; |
| 1242 | } |
| 1243 | ++node_index; |
| 1244 | } |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 1245 | return -1; |
| 1246 | } |
| 1247 | } // namespace |
| 1248 | |
Austin Schuh | a9df9ad | 2021-06-16 14:49:39 -0700 | [diff] [blame] | 1249 | aos::FlatbufferDetachedBuffer<aos::Configuration> AddSchema( |
| 1250 | std::string_view json, |
| 1251 | const std::vector<aos::FlatbufferVector<reflection::Schema>> &schemas) { |
| 1252 | FlatbufferDetachedBuffer<Configuration> addition = |
| 1253 | JsonToFlatbuffer(json, Configuration::MiniReflectTypeTable()); |
| 1254 | return MergeConfiguration(addition, schemas); |
| 1255 | } |
| 1256 | |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 1257 | int GetNodeIndex(const Configuration *config, const Node *node) { |
| 1258 | if (!MultiNode(config)) { |
| 1259 | return 0; |
| 1260 | } |
| 1261 | |
| 1262 | { |
| 1263 | int node_index = GetNodeIndexFromConfig(config, node); |
| 1264 | if (node_index != -1) { |
| 1265 | return node_index; |
| 1266 | } |
| 1267 | } |
| 1268 | |
| 1269 | const Node *result = GetNode(config, node); |
| 1270 | CHECK(result != nullptr); |
| 1271 | |
| 1272 | { |
Austin Schuh | 04408fc | 2020-02-16 21:48:54 -0800 | [diff] [blame] | 1273 | int node_index = GetNodeIndexFromConfig(config, result); |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 1274 | if (node_index != -1) { |
| 1275 | return node_index; |
| 1276 | } |
| 1277 | } |
| 1278 | |
| 1279 | LOG(FATAL) << "Node " << FlatbufferToJson(node) |
| 1280 | << " not found in the configuration."; |
Austin Schuh | c9e10ec | 2020-01-26 16:08:28 -0800 | [diff] [blame] | 1281 | } |
| 1282 | |
Austin Schuh | 04408fc | 2020-02-16 21:48:54 -0800 | [diff] [blame] | 1283 | int GetNodeIndex(const Configuration *config, std::string_view name) { |
| 1284 | if (!MultiNode(config)) { |
| 1285 | return 0; |
| 1286 | } |
| 1287 | |
| 1288 | { |
| 1289 | int node_index = 0; |
| 1290 | for (const Node *iterated_node : *config->nodes()) { |
| 1291 | if (iterated_node->name()->string_view() == name) { |
| 1292 | return node_index; |
| 1293 | } |
| 1294 | ++node_index; |
| 1295 | } |
| 1296 | } |
| 1297 | LOG(FATAL) << "Node " << name << " not found in the configuration."; |
| 1298 | } |
| 1299 | |
Austin Schuh | 681a247 | 2020-12-31 23:55:40 -0800 | [diff] [blame] | 1300 | size_t NodesCount(const Configuration *config) { |
| 1301 | if (!MultiNode(config)) { |
| 1302 | return 1u; |
| 1303 | } |
| 1304 | |
| 1305 | return config->nodes()->size(); |
| 1306 | } |
| 1307 | |
Austin Schuh | c9e10ec | 2020-01-26 16:08:28 -0800 | [diff] [blame] | 1308 | std::vector<const Node *> GetNodes(const Configuration *config) { |
| 1309 | std::vector<const Node *> nodes; |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 1310 | if (MultiNode(config)) { |
Austin Schuh | c9e10ec | 2020-01-26 16:08:28 -0800 | [diff] [blame] | 1311 | for (const Node *node : *config->nodes()) { |
| 1312 | nodes.emplace_back(node); |
| 1313 | } |
| 1314 | } else { |
| 1315 | nodes.emplace_back(nullptr); |
| 1316 | } |
| 1317 | return nodes; |
| 1318 | } |
| 1319 | |
Austin Schuh | 6546533 | 2020-11-05 17:36:53 -0800 | [diff] [blame] | 1320 | std::vector<const Node *> GetNodesWithTag(const Configuration *config, |
| 1321 | std::string_view tag) { |
| 1322 | std::vector<const Node *> nodes; |
| 1323 | if (!MultiNode(config)) { |
| 1324 | nodes.emplace_back(nullptr); |
| 1325 | } else { |
| 1326 | for (const Node *node : *config->nodes()) { |
| 1327 | if (!node->has_tags()) { |
| 1328 | continue; |
| 1329 | } |
| 1330 | bool did_found_tag = false; |
| 1331 | for (const flatbuffers::String *found_tag : *node->tags()) { |
| 1332 | if (found_tag->string_view() == tag) { |
| 1333 | did_found_tag = true; |
| 1334 | break; |
| 1335 | } |
| 1336 | } |
| 1337 | if (did_found_tag) { |
| 1338 | nodes.emplace_back(node); |
| 1339 | } |
| 1340 | } |
| 1341 | } |
| 1342 | return nodes; |
| 1343 | } |
| 1344 | |
Brian Silverman | 631b626 | 2021-11-10 12:25:08 -0800 | [diff] [blame] | 1345 | bool NodeHasTag(const Node *node, std::string_view tag) { |
| 1346 | if (node == nullptr) { |
| 1347 | return true; |
| 1348 | } |
| 1349 | |
Austin Schuh | 97a5243 | 2022-08-17 15:02:59 -0700 | [diff] [blame] | 1350 | if (!node->has_tags()) { |
| 1351 | return false; |
| 1352 | } |
| 1353 | |
Brian Silverman | 631b626 | 2021-11-10 12:25:08 -0800 | [diff] [blame] | 1354 | const auto *const tags = node->tags(); |
| 1355 | return std::find_if(tags->begin(), tags->end(), |
| 1356 | [tag](const flatbuffers::String *candidate) { |
| 1357 | return candidate->string_view() == tag; |
| 1358 | }) != tags->end(); |
| 1359 | } |
| 1360 | |
Austin Schuh | ac0771c | 2020-01-07 18:36:30 -0800 | [diff] [blame] | 1361 | bool MultiNode(const Configuration *config) { return config->has_nodes(); } |
| 1362 | |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 1363 | bool ChannelIsSendableOnNode(const Channel *channel, const Node *node) { |
Austin Schuh | ca4828c | 2019-12-28 14:21:35 -0800 | [diff] [blame] | 1364 | if (node == nullptr) { |
| 1365 | return true; |
| 1366 | } |
Austin Schuh | 3c5dae5 | 2020-10-06 18:55:18 -0700 | [diff] [blame] | 1367 | CHECK(channel->has_source_node()) << FlatbufferToJson(channel); |
| 1368 | CHECK(node->has_name()) << FlatbufferToJson(node); |
Austin Schuh | 196a445 | 2020-03-15 23:12:03 -0700 | [diff] [blame] | 1369 | return (CHECK_NOTNULL(channel)->source_node()->string_view() == |
| 1370 | node->name()->string_view()); |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 1371 | } |
| 1372 | |
| 1373 | bool ChannelIsReadableOnNode(const Channel *channel, const Node *node) { |
Austin Schuh | ca4828c | 2019-12-28 14:21:35 -0800 | [diff] [blame] | 1374 | if (node == nullptr) { |
| 1375 | return true; |
| 1376 | } |
| 1377 | |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 1378 | if (channel->source_node()->string_view() == node->name()->string_view()) { |
| 1379 | return true; |
| 1380 | } |
| 1381 | |
| 1382 | if (!channel->has_destination_nodes()) { |
| 1383 | return false; |
| 1384 | } |
| 1385 | |
Austin Schuh | 719946b | 2019-12-28 14:51:01 -0800 | [diff] [blame] | 1386 | for (const Connection *connection : *channel->destination_nodes()) { |
| 1387 | CHECK(connection->has_name()); |
| 1388 | if (connection->name()->string_view() == node->name()->string_view()) { |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 1389 | return true; |
| 1390 | } |
| 1391 | } |
| 1392 | |
| 1393 | return false; |
| 1394 | } |
| 1395 | |
James Kuszmaul | 24db2d3 | 2023-05-26 11:40:12 -0700 | [diff] [blame] | 1396 | bool ChannelIsForwardedFromNode(const Channel *channel, const Node *node) { |
| 1397 | if (node == nullptr) { |
| 1398 | return false; |
| 1399 | } |
| 1400 | return ChannelIsSendableOnNode(channel, node) && |
| 1401 | channel->has_destination_nodes() && |
| 1402 | channel->destination_nodes()->size() > 0u; |
| 1403 | } |
| 1404 | |
Austin Schuh | 719946b | 2019-12-28 14:51:01 -0800 | [diff] [blame] | 1405 | bool ChannelMessageIsLoggedOnNode(const Channel *channel, const Node *node) { |
Austin Schuh | 48e9450 | 2021-06-18 18:35:53 -0700 | [diff] [blame] | 1406 | if (node == nullptr) { |
Austin Schuh | 2bb80e0 | 2021-03-20 21:46:17 -0700 | [diff] [blame] | 1407 | // Single node world. If there is a local logger, then we want to use |
| 1408 | // it. |
Austin Schuh | 48e9450 | 2021-06-18 18:35:53 -0700 | [diff] [blame] | 1409 | if (channel->logger() == LoggerConfig::LOCAL_LOGGER) { |
| 1410 | return true; |
| 1411 | } else if (channel->logger() == LoggerConfig::NOT_LOGGED) { |
| 1412 | return false; |
| 1413 | } |
| 1414 | LOG(FATAL) << "Unsupported logging configuration in a single node world: " |
| 1415 | << CleanedChannelToString(channel); |
Austin Schuh | 2bb80e0 | 2021-03-20 21:46:17 -0700 | [diff] [blame] | 1416 | } |
| 1417 | return ChannelMessageIsLoggedOnNode( |
| 1418 | channel, CHECK_NOTNULL(node)->name()->string_view()); |
| 1419 | } |
| 1420 | |
| 1421 | bool ChannelMessageIsLoggedOnNode(const Channel *channel, |
| 1422 | std::string_view node_name) { |
Austin Schuh | f1fff28 | 2020-03-28 16:57:32 -0700 | [diff] [blame] | 1423 | switch (channel->logger()) { |
Austin Schuh | 719946b | 2019-12-28 14:51:01 -0800 | [diff] [blame] | 1424 | case LoggerConfig::LOCAL_LOGGER: |
Austin Schuh | 2bb80e0 | 2021-03-20 21:46:17 -0700 | [diff] [blame] | 1425 | return channel->source_node()->string_view() == node_name; |
Austin Schuh | 719946b | 2019-12-28 14:51:01 -0800 | [diff] [blame] | 1426 | case LoggerConfig::LOCAL_AND_REMOTE_LOGGER: |
James Kuszmaul | f645c7d | 2023-02-23 14:21:04 -0800 | [diff] [blame] | 1427 | CHECK(channel->has_logger_nodes()) |
| 1428 | << "Missing logger nodes on " << StrippedChannelToString(channel); |
| 1429 | CHECK_GT(channel->logger_nodes()->size(), 0u) |
| 1430 | << "Missing logger nodes on " << StrippedChannelToString(channel); |
Austin Schuh | 719946b | 2019-12-28 14:51:01 -0800 | [diff] [blame] | 1431 | |
Austin Schuh | 2bb80e0 | 2021-03-20 21:46:17 -0700 | [diff] [blame] | 1432 | if (channel->source_node()->string_view() == node_name) { |
Austin Schuh | 719946b | 2019-12-28 14:51:01 -0800 | [diff] [blame] | 1433 | return true; |
| 1434 | } |
Austin Schuh | da40e47 | 2020-03-28 15:15:29 -0700 | [diff] [blame] | 1435 | |
| 1436 | [[fallthrough]]; |
| 1437 | case LoggerConfig::REMOTE_LOGGER: |
James Kuszmaul | f645c7d | 2023-02-23 14:21:04 -0800 | [diff] [blame] | 1438 | CHECK(channel->has_logger_nodes()) |
| 1439 | << "Missing logger nodes on " << StrippedChannelToString(channel); |
| 1440 | CHECK_GT(channel->logger_nodes()->size(), 0u) |
| 1441 | << "Missing logger nodes on " << StrippedChannelToString(channel); |
Austin Schuh | da40e47 | 2020-03-28 15:15:29 -0700 | [diff] [blame] | 1442 | for (const flatbuffers::String *logger_node : *channel->logger_nodes()) { |
Austin Schuh | 2bb80e0 | 2021-03-20 21:46:17 -0700 | [diff] [blame] | 1443 | if (logger_node->string_view() == node_name) { |
Austin Schuh | da40e47 | 2020-03-28 15:15:29 -0700 | [diff] [blame] | 1444 | return true; |
| 1445 | } |
Austin Schuh | 719946b | 2019-12-28 14:51:01 -0800 | [diff] [blame] | 1446 | } |
| 1447 | |
| 1448 | return false; |
| 1449 | case LoggerConfig::NOT_LOGGED: |
| 1450 | return false; |
| 1451 | } |
| 1452 | |
| 1453 | LOG(FATAL) << "Unknown logger config " << static_cast<int>(channel->logger()); |
| 1454 | } |
| 1455 | |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 1456 | size_t ConnectionCount(const Channel *channel) { |
| 1457 | if (!channel->has_destination_nodes()) { |
| 1458 | return 0; |
| 1459 | } |
| 1460 | return channel->destination_nodes()->size(); |
| 1461 | } |
| 1462 | |
Austin Schuh | 719946b | 2019-12-28 14:51:01 -0800 | [diff] [blame] | 1463 | const Connection *ConnectionToNode(const Channel *channel, const Node *node) { |
| 1464 | if (!channel->has_destination_nodes()) { |
| 1465 | return nullptr; |
| 1466 | } |
| 1467 | for (const Connection *connection : *channel->destination_nodes()) { |
| 1468 | if (connection->name()->string_view() == node->name()->string_view()) { |
| 1469 | return connection; |
| 1470 | } |
| 1471 | } |
| 1472 | return nullptr; |
| 1473 | } |
| 1474 | |
| 1475 | bool ConnectionDeliveryTimeIsLoggedOnNode(const Channel *channel, |
| 1476 | const Node *node, |
| 1477 | const Node *logger_node) { |
Austin Schuh | 72211ae | 2021-08-05 14:02:30 -0700 | [diff] [blame] | 1478 | return ConnectionDeliveryTimeIsLoggedOnNode(ConnectionToNode(channel, node), |
| 1479 | logger_node); |
Austin Schuh | 719946b | 2019-12-28 14:51:01 -0800 | [diff] [blame] | 1480 | } |
| 1481 | |
| 1482 | bool ConnectionDeliveryTimeIsLoggedOnNode(const Connection *connection, |
| 1483 | const Node *node) { |
Austin Schuh | 72211ae | 2021-08-05 14:02:30 -0700 | [diff] [blame] | 1484 | if (connection == nullptr) { |
| 1485 | return false; |
| 1486 | } |
Austin Schuh | 719946b | 2019-12-28 14:51:01 -0800 | [diff] [blame] | 1487 | switch (connection->timestamp_logger()) { |
| 1488 | case LoggerConfig::LOCAL_AND_REMOTE_LOGGER: |
Austin Schuh | da40e47 | 2020-03-28 15:15:29 -0700 | [diff] [blame] | 1489 | CHECK(connection->has_timestamp_logger_nodes()); |
| 1490 | CHECK_GT(connection->timestamp_logger_nodes()->size(), 0u); |
Austin Schuh | 719946b | 2019-12-28 14:51:01 -0800 | [diff] [blame] | 1491 | if (connection->name()->string_view() == node->name()->string_view()) { |
| 1492 | return true; |
| 1493 | } |
| 1494 | |
Austin Schuh | da40e47 | 2020-03-28 15:15:29 -0700 | [diff] [blame] | 1495 | [[fallthrough]]; |
| 1496 | case LoggerConfig::REMOTE_LOGGER: |
| 1497 | CHECK(connection->has_timestamp_logger_nodes()); |
| 1498 | CHECK_GT(connection->timestamp_logger_nodes()->size(), 0u); |
| 1499 | for (const flatbuffers::String *timestamp_logger_node : |
| 1500 | *connection->timestamp_logger_nodes()) { |
| 1501 | if (timestamp_logger_node->string_view() == |
| 1502 | node->name()->string_view()) { |
| 1503 | return true; |
| 1504 | } |
Austin Schuh | 719946b | 2019-12-28 14:51:01 -0800 | [diff] [blame] | 1505 | } |
| 1506 | |
| 1507 | return false; |
| 1508 | case LoggerConfig::LOCAL_LOGGER: |
| 1509 | return connection->name()->string_view() == node->name()->string_view(); |
Austin Schuh | 719946b | 2019-12-28 14:51:01 -0800 | [diff] [blame] | 1510 | case LoggerConfig::NOT_LOGGED: |
| 1511 | return false; |
| 1512 | } |
| 1513 | |
| 1514 | LOG(FATAL) << "Unknown logger config " |
| 1515 | << static_cast<int>(connection->timestamp_logger()); |
| 1516 | } |
| 1517 | |
Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 1518 | std::vector<std::string_view> SourceNodeNames(const Configuration *config, |
| 1519 | const Node *my_node) { |
| 1520 | std::set<std::string_view> result_set; |
| 1521 | |
| 1522 | for (const Channel *channel : *config->channels()) { |
| 1523 | if (channel->has_destination_nodes()) { |
| 1524 | for (const Connection *connection : *channel->destination_nodes()) { |
| 1525 | if (connection->name()->string_view() == |
| 1526 | my_node->name()->string_view()) { |
| 1527 | result_set.insert(channel->source_node()->string_view()); |
| 1528 | } |
| 1529 | } |
| 1530 | } |
| 1531 | } |
| 1532 | |
| 1533 | std::vector<std::string_view> result; |
| 1534 | for (const std::string_view source : result_set) { |
| 1535 | VLOG(1) << "Found a source node of " << source; |
| 1536 | result.emplace_back(source); |
| 1537 | } |
| 1538 | return result; |
| 1539 | } |
| 1540 | |
| 1541 | std::vector<std::string_view> DestinationNodeNames(const Configuration *config, |
| 1542 | const Node *my_node) { |
| 1543 | std::vector<std::string_view> result; |
| 1544 | |
| 1545 | for (const Channel *channel : *config->channels()) { |
| 1546 | if (channel->has_source_node() && channel->source_node()->string_view() == |
| 1547 | my_node->name()->string_view()) { |
| 1548 | if (!channel->has_destination_nodes()) continue; |
| 1549 | |
| 1550 | if (channel->source_node()->string_view() != |
| 1551 | my_node->name()->string_view()) { |
| 1552 | continue; |
| 1553 | } |
| 1554 | |
| 1555 | for (const Connection *connection : *channel->destination_nodes()) { |
| 1556 | if (std::find(result.begin(), result.end(), |
| 1557 | connection->name()->string_view()) == result.end()) { |
| 1558 | result.emplace_back(connection->name()->string_view()); |
| 1559 | } |
| 1560 | } |
| 1561 | } |
| 1562 | } |
| 1563 | |
| 1564 | for (const std::string_view destination : result) { |
| 1565 | VLOG(1) << "Found a destination node of " << destination; |
| 1566 | } |
| 1567 | return result; |
| 1568 | } |
| 1569 | |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 1570 | std::vector<const Node *> TimestampNodes(const Configuration *config, |
| 1571 | const Node *my_node) { |
| 1572 | if (!configuration::MultiNode(config)) { |
| 1573 | CHECK(my_node == nullptr); |
| 1574 | return std::vector<const Node *>{}; |
| 1575 | } |
| 1576 | |
| 1577 | std::set<const Node *> timestamp_logger_nodes; |
| 1578 | for (const Channel *channel : *config->channels()) { |
| 1579 | if (!configuration::ChannelIsSendableOnNode(channel, my_node)) { |
| 1580 | continue; |
| 1581 | } |
| 1582 | if (!channel->has_destination_nodes()) { |
| 1583 | continue; |
| 1584 | } |
| 1585 | for (const Connection *connection : *channel->destination_nodes()) { |
| 1586 | const Node *other_node = |
| 1587 | configuration::GetNode(config, connection->name()->string_view()); |
| 1588 | |
| 1589 | if (configuration::ConnectionDeliveryTimeIsLoggedOnNode(connection, |
| 1590 | my_node)) { |
| 1591 | VLOG(1) << "Timestamps are logged from " |
| 1592 | << FlatbufferToJson(other_node); |
| 1593 | timestamp_logger_nodes.insert(other_node); |
| 1594 | } |
| 1595 | } |
| 1596 | } |
| 1597 | |
| 1598 | std::vector<const Node *> result; |
| 1599 | for (const Node *node : timestamp_logger_nodes) { |
| 1600 | result.emplace_back(node); |
| 1601 | } |
| 1602 | return result; |
| 1603 | } |
| 1604 | |
Austin Schuh | c41fa3c | 2021-10-16 14:35:35 -0700 | [diff] [blame] | 1605 | bool ApplicationShouldStart(const Configuration *config, const Node *my_node, |
| 1606 | const Application *application) { |
| 1607 | if (MultiNode(config)) { |
| 1608 | // Ok, we need |
| 1609 | CHECK(application->has_nodes()); |
| 1610 | CHECK(my_node != nullptr); |
| 1611 | for (const flatbuffers::String *str : *application->nodes()) { |
| 1612 | if (str->string_view() == my_node->name()->string_view()) { |
| 1613 | return true; |
| 1614 | } |
| 1615 | } |
| 1616 | return false; |
| 1617 | } else { |
| 1618 | return true; |
| 1619 | } |
| 1620 | } |
| 1621 | |
Austin Schuh | d2e2f6a | 2021-02-07 20:46:16 -0800 | [diff] [blame] | 1622 | const Application *GetApplication(const Configuration *config, |
| 1623 | const Node *my_node, |
| 1624 | std::string_view application_name) { |
| 1625 | if (config->has_applications()) { |
| 1626 | auto application_iterator = std::lower_bound( |
| 1627 | config->applications()->cbegin(), config->applications()->cend(), |
| 1628 | application_name, CompareApplications); |
| 1629 | if (application_iterator != config->applications()->cend() && |
| 1630 | EqualsApplications(*application_iterator, application_name)) { |
Austin Schuh | c41fa3c | 2021-10-16 14:35:35 -0700 | [diff] [blame] | 1631 | if (ApplicationShouldStart(config, my_node, *application_iterator)) { |
Austin Schuh | d2e2f6a | 2021-02-07 20:46:16 -0800 | [diff] [blame] | 1632 | return *application_iterator; |
| 1633 | } |
| 1634 | } |
| 1635 | } |
| 1636 | return nullptr; |
| 1637 | } |
| 1638 | |
Austin Schuh | fc7b6a0 | 2021-07-12 21:19:07 -0700 | [diff] [blame] | 1639 | std::vector<size_t> SourceNodeIndex(const Configuration *config) { |
| 1640 | CHECK(config->has_channels()); |
| 1641 | std::vector<size_t> result; |
| 1642 | result.resize(config->channels()->size(), 0u); |
| 1643 | if (MultiNode(config)) { |
| 1644 | for (size_t i = 0; i < config->channels()->size(); ++i) { |
| 1645 | result[i] = GetNodeIndex( |
| 1646 | config, config->channels()->Get(i)->source_node()->string_view()); |
| 1647 | } |
| 1648 | } |
| 1649 | return result; |
| 1650 | } |
| 1651 | |
Austin Schuh | fff9c3a | 2023-06-16 18:48:23 -0700 | [diff] [blame] | 1652 | chrono::nanoseconds ChannelStorageDuration(const Configuration *config, |
| 1653 | const Channel *channel) { |
| 1654 | CHECK(channel != nullptr); |
Austin Schuh | dda6db7 | 2023-06-21 17:02:34 -0700 | [diff] [blame] | 1655 | if (channel->has_channel_storage_duration()) { |
| 1656 | return chrono::nanoseconds(channel->channel_storage_duration()); |
| 1657 | } |
Austin Schuh | fff9c3a | 2023-06-16 18:48:23 -0700 | [diff] [blame] | 1658 | return chrono::nanoseconds(config->channel_storage_duration()); |
| 1659 | } |
| 1660 | |
Austin Schuh | 83cbb1e | 2023-06-23 12:59:02 -0700 | [diff] [blame] | 1661 | size_t QueueSize(const Configuration *config, const Channel *channel) { |
Austin Schuh | fb37c61 | 2022-08-11 15:24:51 -0700 | [diff] [blame] | 1662 | return QueueSize(channel->frequency(), |
Austin Schuh | fff9c3a | 2023-06-16 18:48:23 -0700 | [diff] [blame] | 1663 | ChannelStorageDuration(config, channel)); |
Austin Schuh | fb37c61 | 2022-08-11 15:24:51 -0700 | [diff] [blame] | 1664 | } |
| 1665 | |
Austin Schuh | 83cbb1e | 2023-06-23 12:59:02 -0700 | [diff] [blame] | 1666 | size_t QueueSize(size_t frequency, |
| 1667 | chrono::nanoseconds channel_storage_duration) { |
Austin Schuh | fb37c61 | 2022-08-11 15:24:51 -0700 | [diff] [blame] | 1668 | // Use integer arithmetic and round up at all cost. |
| 1669 | return static_cast<int>( |
Philipp Schrader | 0434f90 | 2023-09-06 08:41:32 -0700 | [diff] [blame] | 1670 | (999'999'999 + |
| 1671 | static_cast<int64_t>(frequency) * |
| 1672 | static_cast<int64_t>(channel_storage_duration.count())) / |
| 1673 | static_cast<int64_t>(1'000'000'000)); |
Austin Schuh | fb37c61 | 2022-08-11 15:24:51 -0700 | [diff] [blame] | 1674 | } |
| 1675 | |
| 1676 | int QueueScratchBufferSize(const Channel *channel) { |
| 1677 | return channel->num_readers() + channel->num_senders(); |
| 1678 | } |
| 1679 | |
Nathan Leong | 307c969 | 2022-10-08 15:25:03 -0700 | [diff] [blame] | 1680 | // Searches through configurations for schemas that include a certain type |
| 1681 | const reflection::Schema *GetSchema(const Configuration *config, |
| 1682 | std::string_view schema_type) { |
| 1683 | if (config->has_channels()) { |
| 1684 | std::vector<flatbuffers::Offset<Channel>> channel_offsets; |
| 1685 | for (const Channel *c : *config->channels()) { |
| 1686 | if (schema_type == c->type()->string_view()) { |
| 1687 | return c->schema(); |
| 1688 | } |
| 1689 | } |
| 1690 | } |
| 1691 | return nullptr; |
| 1692 | } |
| 1693 | |
| 1694 | // Copy schema reflection into detached flatbuffer |
| 1695 | std::optional<FlatbufferDetachedBuffer<reflection::Schema>> |
| 1696 | GetSchemaDetachedBuffer(const Configuration *config, |
| 1697 | std::string_view schema_type) { |
| 1698 | const reflection::Schema *found_schema = GetSchema(config, schema_type); |
| 1699 | if (found_schema == nullptr) { |
| 1700 | return std::nullopt; |
| 1701 | } |
| 1702 | return RecursiveCopyFlatBuffer(found_schema); |
| 1703 | } |
| 1704 | |
James Kuszmaul | 741a4d0 | 2023-01-05 14:59:21 -0800 | [diff] [blame] | 1705 | aos::FlatbufferDetachedBuffer<Configuration> AddChannelToConfiguration( |
| 1706 | const Configuration *config, std::string_view name, |
| 1707 | aos::FlatbufferVector<reflection::Schema> schema, const aos::Node *node, |
| 1708 | ChannelT overrides) { |
| 1709 | overrides.name = name; |
James Kuszmaul | 80d6c42 | 2023-01-06 14:16:04 -0800 | [diff] [blame] | 1710 | CHECK(schema.message().has_root_table()); |
James Kuszmaul | 741a4d0 | 2023-01-05 14:59:21 -0800 | [diff] [blame] | 1711 | overrides.type = schema.message().root_table()->name()->string_view(); |
| 1712 | if (node != nullptr) { |
| 1713 | CHECK(node->has_name()); |
| 1714 | overrides.source_node = node->name()->string_view(); |
| 1715 | } |
| 1716 | flatbuffers::FlatBufferBuilder fbb; |
| 1717 | // Don't populate fields from overrides that the user doesn't explicitly |
| 1718 | // override. |
| 1719 | fbb.ForceDefaults(false); |
| 1720 | const flatbuffers::Offset<Channel> channel_offset = |
| 1721 | Channel::Pack(fbb, &overrides); |
| 1722 | const flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<Channel>>> |
| 1723 | channels_offset = fbb.CreateVector({channel_offset}); |
| 1724 | Configuration::Builder config_builder(fbb); |
| 1725 | config_builder.add_channels(channels_offset); |
| 1726 | fbb.Finish(config_builder.Finish()); |
| 1727 | FlatbufferDetachedBuffer<Configuration> new_channel_config = fbb.Release(); |
| 1728 | new_channel_config = MergeConfiguration(new_channel_config, {schema}); |
| 1729 | return MergeWithConfig(config, new_channel_config); |
| 1730 | } |
| 1731 | |
Brian Silverman | 66f079a | 2013-08-26 16:24:30 -0700 | [diff] [blame] | 1732 | } // namespace configuration |
| 1733 | } // namespace aos |