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 | |
| 3 | #include <string.h> |
Brian Silverman | 66f079a | 2013-08-26 16:24:30 -0700 | [diff] [blame] | 4 | #include <stdlib.h> |
| 5 | #include <sys/types.h> |
| 6 | #include <netinet/in.h> |
| 7 | #include <arpa/inet.h> |
| 8 | #include <ifaddrs.h> |
| 9 | #include <unistd.h> |
| 10 | |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 11 | #include "absl/container/btree_set.h" |
| 12 | #include "absl/strings/string_view.h" |
| 13 | #include "aos/configuration_generated.h" |
| 14 | #include "aos/flatbuffer_merge.h" |
| 15 | #include "aos/json_to_flatbuffer.h" |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 16 | #include "aos/unique_malloc_ptr.h" |
| 17 | #include "aos/util/file.h" |
| 18 | #include "glog/logging.h" |
John Park | 7bca981 | 2019-10-14 21:23:45 -0700 | [diff] [blame] | 19 | #include "absl/base/call_once.h" |
Brian Silverman | 66f079a | 2013-08-26 16:24:30 -0700 | [diff] [blame] | 20 | |
| 21 | namespace aos { |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 22 | |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 23 | // 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] | 24 | // insert them in the btree below. |
| 25 | // |
| 26 | // These are not in headers because they are only comparing part of the |
| 27 | // flatbuffer, and it seems weird to expose that as *the* compare operator. And |
| 28 | // I can't put them in an anonymous namespace because they wouldn't be found |
| 29 | // that way by the btree. |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 30 | bool operator<(const FlatbufferDetachedBuffer<Channel> &lhs, |
| 31 | const FlatbufferDetachedBuffer<Channel> &rhs) { |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 32 | int name_compare = lhs.message().name()->string_view().compare( |
| 33 | rhs.message().name()->string_view()); |
| 34 | if (name_compare == 0) { |
| 35 | return lhs.message().type()->string_view() < |
| 36 | rhs.message().type()->string_view(); |
| 37 | } else if (name_compare < 0) { |
| 38 | return true; |
| 39 | } else { |
| 40 | return false; |
| 41 | } |
| 42 | } |
| 43 | |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 44 | bool operator==(const FlatbufferDetachedBuffer<Channel> &lhs, |
| 45 | const FlatbufferDetachedBuffer<Channel> &rhs) { |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 46 | return lhs.message().name()->string_view() == |
| 47 | rhs.message().name()->string_view() && |
| 48 | lhs.message().type()->string_view() == |
| 49 | rhs.message().type()->string_view(); |
| 50 | } |
| 51 | |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 52 | bool operator==(const FlatbufferDetachedBuffer<Application> &lhs, |
| 53 | const FlatbufferDetachedBuffer<Application> &rhs) { |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 54 | return lhs.message().name()->string_view() == |
| 55 | rhs.message().name()->string_view(); |
| 56 | } |
| 57 | |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 58 | bool operator<(const FlatbufferDetachedBuffer<Application> &lhs, |
| 59 | const FlatbufferDetachedBuffer<Application> &rhs) { |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 60 | return lhs.message().name()->string_view() < |
| 61 | rhs.message().name()->string_view(); |
| 62 | } |
| 63 | |
Brian Silverman | 66f079a | 2013-08-26 16:24:30 -0700 | [diff] [blame] | 64 | namespace configuration { |
| 65 | namespace { |
| 66 | |
Austin Schuh | 629821e | 2014-02-10 21:18:27 -0800 | [diff] [blame] | 67 | // TODO(brians): This shouldn't be necesary for running tests. Provide a way to |
| 68 | // set the IP address when running tests from the test. |
Brian Silverman | 14fd0fb | 2014-01-14 21:42:01 -0800 | [diff] [blame] | 69 | const char *const kLinuxNetInterface = "eth0"; |
John Park | 7bca981 | 2019-10-14 21:23:45 -0700 | [diff] [blame] | 70 | |
| 71 | void DoGetOwnIPAddress(in_addr *retu) { |
Brian Silverman | 01be000 | 2014-05-10 15:44:38 -0700 | [diff] [blame] | 72 | static const char *kOverrideVariable = "FRC971_IP_OVERRIDE"; |
| 73 | const char *override_ip = getenv(kOverrideVariable); |
| 74 | if (override_ip != NULL) { |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 75 | LOG(INFO) << "Override IP is " << override_ip; |
John Park | 7bca981 | 2019-10-14 21:23:45 -0700 | [diff] [blame] | 76 | if (inet_aton(override_ip, retu) != 0) { |
| 77 | return; |
joe | 3779d0c | 2014-02-15 19:41:22 -0800 | [diff] [blame] | 78 | } else { |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 79 | LOG(WARNING) << "error parsing " << kOverrideVariable << " value '" |
| 80 | << override_ip << "'"; |
joe | 3779d0c | 2014-02-15 19:41:22 -0800 | [diff] [blame] | 81 | } |
Brian Silverman | 01be000 | 2014-05-10 15:44:38 -0700 | [diff] [blame] | 82 | } else { |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 83 | LOG(INFO) << "Couldn't get environmental variable."; |
Brian Silverman | 01be000 | 2014-05-10 15:44:38 -0700 | [diff] [blame] | 84 | } |
joe | 3779d0c | 2014-02-15 19:41:22 -0800 | [diff] [blame] | 85 | |
Brian Silverman | 66f079a | 2013-08-26 16:24:30 -0700 | [diff] [blame] | 86 | ifaddrs *addrs; |
| 87 | if (getifaddrs(&addrs) != 0) { |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 88 | PLOG(FATAL) << "getifaddrs(" << &addrs << ") failed"; |
Brian Silverman | 66f079a | 2013-08-26 16:24:30 -0700 | [diff] [blame] | 89 | } |
| 90 | // Smart pointers don't work very well for iterating through a linked list, |
| 91 | // but it does do a very nice job of making sure that addrs gets freed. |
| 92 | unique_c_ptr<ifaddrs, freeifaddrs> addrs_deleter(addrs); |
| 93 | |
Austin Schuh | 629821e | 2014-02-10 21:18:27 -0800 | [diff] [blame] | 94 | for (; addrs != nullptr; addrs = addrs->ifa_next) { |
Brian Silverman | 6da0427 | 2014-05-18 18:47:48 -0700 | [diff] [blame] | 95 | // ifa_addr tends to be nullptr on CAN interfaces. |
Austin Schuh | 629821e | 2014-02-10 21:18:27 -0800 | [diff] [blame] | 96 | if (addrs->ifa_addr != nullptr && addrs->ifa_addr->sa_family == AF_INET) { |
Brian Silverman | 14fd0fb | 2014-01-14 21:42:01 -0800 | [diff] [blame] | 97 | if (strcmp(kLinuxNetInterface, addrs->ifa_name) == 0) { |
John Park | 7bca981 | 2019-10-14 21:23:45 -0700 | [diff] [blame] | 98 | *retu = reinterpret_cast<sockaddr_in *>(__builtin_assume_aligned( |
Brian Silverman | 63cf241 | 2013-11-17 05:44:36 -0800 | [diff] [blame] | 99 | addrs->ifa_addr, alignof(sockaddr_in)))->sin_addr; |
John Park | 7bca981 | 2019-10-14 21:23:45 -0700 | [diff] [blame] | 100 | return; |
Brian Silverman | 66f079a | 2013-08-26 16:24:30 -0700 | [diff] [blame] | 101 | } |
| 102 | } |
| 103 | } |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 104 | LOG(FATAL) << "couldn't find an AF_INET interface named \"" |
| 105 | << kLinuxNetInterface << "\""; |
Brian Silverman | 66f079a | 2013-08-26 16:24:30 -0700 | [diff] [blame] | 106 | } |
| 107 | |
John Park | 7bca981 | 2019-10-14 21:23:45 -0700 | [diff] [blame] | 108 | void DoGetRootDirectory(char** retu) { |
Brian Silverman | 66f079a | 2013-08-26 16:24:30 -0700 | [diff] [blame] | 109 | ssize_t size = 0; |
John Park | 7bca981 | 2019-10-14 21:23:45 -0700 | [diff] [blame] | 110 | *retu = NULL; |
Brian Silverman | 66f079a | 2013-08-26 16:24:30 -0700 | [diff] [blame] | 111 | while (true) { |
Brian Silverman | 66f079a | 2013-08-26 16:24:30 -0700 | [diff] [blame] | 112 | size += 256; |
John Park | 7bca981 | 2019-10-14 21:23:45 -0700 | [diff] [blame] | 113 | if (*retu != nullptr) delete *retu; |
| 114 | *retu = new char[size]; |
Brian Silverman | 66f079a | 2013-08-26 16:24:30 -0700 | [diff] [blame] | 115 | |
John Park | 7bca981 | 2019-10-14 21:23:45 -0700 | [diff] [blame] | 116 | ssize_t ret = readlink("/proc/self/exe", *retu, size); |
Brian Silverman | 66f079a | 2013-08-26 16:24:30 -0700 | [diff] [blame] | 117 | if (ret < 0) { |
| 118 | if (ret != -1) { |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 119 | LOG(WARNING) << "it returned " << ret << ", not -1"; |
Brian Silverman | 66f079a | 2013-08-26 16:24:30 -0700 | [diff] [blame] | 120 | } |
John Park | 7bca981 | 2019-10-14 21:23:45 -0700 | [diff] [blame] | 121 | |
| 122 | PLOG(FATAL) << "readlink(\"/proc/self/exe\", " << *retu << ", " << size |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 123 | << ") failed"; |
Brian Silverman | 66f079a | 2013-08-26 16:24:30 -0700 | [diff] [blame] | 124 | } |
| 125 | if (ret < size) { |
John Park | 7bca981 | 2019-10-14 21:23:45 -0700 | [diff] [blame] | 126 | void *last_slash = memrchr(*retu, '/', ret); |
Brian Silverman | 66f079a | 2013-08-26 16:24:30 -0700 | [diff] [blame] | 127 | if (last_slash == NULL) { |
John Park | 7bca981 | 2019-10-14 21:23:45 -0700 | [diff] [blame] | 128 | *retu[ret] = '\0'; |
| 129 | LOG(FATAL) << "couldn't find a '/' in \"" << *retu << "\""; |
Brian Silverman | 66f079a | 2013-08-26 16:24:30 -0700 | [diff] [blame] | 130 | } |
| 131 | *static_cast<char *>(last_slash) = '\0'; |
John Park | 7bca981 | 2019-10-14 21:23:45 -0700 | [diff] [blame] | 132 | LOG(INFO) << "got a root dir of \"" << *retu << "\""; |
| 133 | return; |
Brian Silverman | 66f079a | 2013-08-26 16:24:30 -0700 | [diff] [blame] | 134 | } |
| 135 | } |
| 136 | } |
| 137 | |
John Park | 7bca981 | 2019-10-14 21:23:45 -0700 | [diff] [blame] | 138 | void DoGetLoggingDirectory(char** retu) { |
Brian Silverman | 66f079a | 2013-08-26 16:24:30 -0700 | [diff] [blame] | 139 | static const char kSuffix[] = "/../../tmp/robot_logs"; |
| 140 | const char *root = GetRootDirectory(); |
John Park | 7bca981 | 2019-10-14 21:23:45 -0700 | [diff] [blame] | 141 | *retu = new char[strlen(root) + sizeof(kSuffix)]; |
| 142 | strcpy(*retu, root); |
| 143 | strcat(*retu, kSuffix); |
Brian Silverman | 66f079a | 2013-08-26 16:24:30 -0700 | [diff] [blame] | 144 | } |
| 145 | |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 146 | // Extracts the folder part of a path. Returns ./ if there is no path. |
| 147 | absl::string_view ExtractFolder(const absl::string_view filename) { |
| 148 | auto last_slash_pos = filename.find_last_of("/\\"); |
| 149 | |
| 150 | return last_slash_pos == absl::string_view::npos |
| 151 | ? absl::string_view("./") |
| 152 | : filename.substr(0, last_slash_pos + 1); |
| 153 | } |
| 154 | |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 155 | FlatbufferDetachedBuffer<Configuration> ReadConfig( |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 156 | const absl::string_view path, absl::btree_set<std::string> *visited_paths) { |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 157 | FlatbufferDetachedBuffer<Configuration> config(JsonToFlatbuffer( |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 158 | util::ReadFileToStringOrDie(path), ConfigurationTypeTable())); |
| 159 | // Depth first. Take the following example: |
| 160 | // |
| 161 | // config1.json: |
| 162 | // { |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 163 | // "channels": [ |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 164 | // { |
| 165 | // "name": "/foo", |
| 166 | // "type": ".aos.bar", |
| 167 | // "max_size": 5 |
| 168 | // } |
| 169 | // ], |
| 170 | // "imports": [ |
| 171 | // "config2.json", |
| 172 | // ] |
| 173 | // } |
| 174 | // |
| 175 | // config2.json: |
| 176 | // { |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 177 | // "channels": [ |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 178 | // { |
| 179 | // "name": "/foo", |
| 180 | // "type": ".aos.bar", |
| 181 | // "max_size": 7 |
| 182 | // } |
| 183 | // ], |
| 184 | // } |
| 185 | // |
| 186 | // We want the main config (config1.json) to be able to override the imported |
| 187 | // config. That means that it needs to be merged into the imported configs, |
| 188 | // not the other way around. |
| 189 | |
| 190 | // Track that we have seen this file before recursing. |
| 191 | visited_paths->insert(::std::string(path)); |
| 192 | |
| 193 | if (config.message().has_imports()) { |
| 194 | // Capture the imports. |
| 195 | const flatbuffers::Vector<flatbuffers::Offset<flatbuffers::String>> *v = |
| 196 | config.message().imports(); |
| 197 | |
| 198 | // And then wipe them. This gets GCed when we merge later. |
| 199 | config.mutable_message()->clear_imports(); |
| 200 | |
| 201 | // Start with an empty configuration to merge into. |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 202 | FlatbufferDetachedBuffer<Configuration> merged_config = |
| 203 | FlatbufferDetachedBuffer<Configuration>::Empty(); |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 204 | |
| 205 | const ::std::string folder(ExtractFolder(path)); |
| 206 | |
| 207 | for (const flatbuffers::String *str : *v) { |
| 208 | const ::std::string included_config = folder + str->c_str(); |
| 209 | // Abort on any paths we have already seen. |
| 210 | CHECK(visited_paths->find(included_config) == visited_paths->end()) |
| 211 | << ": Found duplicate file " << included_config << " while reading " |
| 212 | << path; |
| 213 | |
| 214 | // And them merge everything in. |
| 215 | merged_config = MergeFlatBuffers( |
| 216 | merged_config, ReadConfig(included_config, visited_paths)); |
| 217 | } |
| 218 | |
| 219 | // Finally, merge this file in. |
| 220 | config = MergeFlatBuffers(merged_config, config); |
| 221 | } |
| 222 | return config; |
| 223 | } |
| 224 | |
| 225 | // Remove duplicate entries, and handle overrides. |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 226 | FlatbufferDetachedBuffer<Configuration> MergeConfiguration( |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 227 | const Flatbuffer<Configuration> &config) { |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 228 | // 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] | 229 | // have seen before and merge the updates in. |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 230 | absl::btree_set<FlatbufferDetachedBuffer<Channel>> channels; |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 231 | |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 232 | if (config.message().has_channels()) { |
| 233 | for (const Channel *c : *config.message().channels()) { |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 234 | // Ignore malformed entries. |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 235 | if (!c->has_name()) { |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 236 | continue; |
| 237 | } |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 238 | if (!c->has_type()) { |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 239 | continue; |
| 240 | } |
| 241 | |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 242 | // Attempt to insert the channel. |
| 243 | auto result = channels.insert(CopyFlatBuffer(c)); |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 244 | if (!result.second) { |
| 245 | // Already there, so merge the new table into the original. |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 246 | *result.first = MergeFlatBuffers(*result.first, CopyFlatBuffer(c)); |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 247 | } |
| 248 | } |
| 249 | } |
| 250 | |
| 251 | // Now repeat this for the application list. |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 252 | absl::btree_set<FlatbufferDetachedBuffer<Application>> applications; |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 253 | if (config.message().has_applications()) { |
| 254 | for (const Application *a : *config.message().applications()) { |
| 255 | if (!a->has_name()) { |
| 256 | continue; |
| 257 | } |
| 258 | |
| 259 | auto result = applications.insert(CopyFlatBuffer(a)); |
| 260 | if (!result.second) { |
| 261 | *result.first = MergeFlatBuffers(*result.first, CopyFlatBuffer(a)); |
| 262 | } |
| 263 | } |
| 264 | } |
| 265 | |
| 266 | flatbuffers::FlatBufferBuilder fbb; |
| 267 | fbb.ForceDefaults(1); |
| 268 | |
| 269 | // 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] | 270 | // Channels |
| 271 | flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<Channel>>> |
| 272 | channels_offset; |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 273 | { |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 274 | ::std::vector<flatbuffers::Offset<Channel>> channel_offsets; |
| 275 | for (const FlatbufferDetachedBuffer<Channel> &c : channels) { |
| 276 | channel_offsets.emplace_back( |
| 277 | CopyFlatBuffer<Channel>(&c.message(), &fbb)); |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 278 | } |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 279 | channels_offset = fbb.CreateVector(channel_offsets); |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 280 | } |
| 281 | |
| 282 | // Applications |
| 283 | flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<Application>>> |
| 284 | applications_offset; |
| 285 | { |
| 286 | ::std::vector<flatbuffers::Offset<Application>> applications_offsets; |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 287 | for (const FlatbufferDetachedBuffer<Application> &a : applications) { |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 288 | applications_offsets.emplace_back( |
| 289 | CopyFlatBuffer<Application>(&a.message(), &fbb)); |
| 290 | } |
| 291 | applications_offset = fbb.CreateVector(applications_offsets); |
| 292 | } |
| 293 | |
| 294 | // Just copy the maps |
| 295 | flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<Map>>> |
| 296 | maps_offset; |
| 297 | { |
| 298 | ::std::vector<flatbuffers::Offset<Map>> map_offsets; |
| 299 | if (config.message().has_maps()) { |
| 300 | for (const Map *m : *config.message().maps()) { |
| 301 | map_offsets.emplace_back(CopyFlatBuffer<Map>(m, &fbb)); |
| 302 | } |
| 303 | maps_offset = fbb.CreateVector(map_offsets); |
| 304 | } |
| 305 | } |
| 306 | |
| 307 | // And then build a Configuration with them all. |
| 308 | ConfigurationBuilder configuration_builder(fbb); |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 309 | configuration_builder.add_channels(channels_offset); |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 310 | if (config.message().has_maps()) { |
| 311 | configuration_builder.add_maps(maps_offset); |
| 312 | } |
| 313 | configuration_builder.add_applications(applications_offset); |
| 314 | |
| 315 | fbb.Finish(configuration_builder.Finish()); |
| 316 | return fbb.Release(); |
| 317 | } |
| 318 | |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 319 | // Compares (c < p) a channel, and a name, type tuple. |
| 320 | bool CompareChannels(const Channel *c, |
| 321 | ::std::pair<absl::string_view, absl::string_view> p) { |
| 322 | int name_compare = c->name()->string_view().compare(p.first); |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 323 | if (name_compare == 0) { |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 324 | return c->type()->string_view() < p.second; |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 325 | } else if (name_compare < 0) { |
| 326 | return true; |
| 327 | } else { |
| 328 | return false; |
| 329 | } |
| 330 | }; |
| 331 | |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 332 | // Compares for equality (c == p) a channel, and a name, type tuple. |
| 333 | bool EqualsChannels(const Channel *c, |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 334 | ::std::pair<absl::string_view, absl::string_view> p) { |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 335 | return c->name()->string_view() == p.first && |
| 336 | c->type()->string_view() == p.second; |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 337 | } |
| 338 | |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 339 | // Compares (c < p) an application, and a name; |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 340 | bool CompareApplications(const Application *a, absl::string_view name) { |
| 341 | return a->name()->string_view() < name; |
| 342 | }; |
| 343 | |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 344 | // Compares for equality (c == p) an application, and a name; |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 345 | bool EqualsApplications(const Application *a, absl::string_view name) { |
| 346 | return a->name()->string_view() == name; |
| 347 | } |
| 348 | |
| 349 | // Maps name for the provided maps. Modifies name. |
| 350 | void HandleMaps(const flatbuffers::Vector<flatbuffers::Offset<aos::Map>> *maps, |
| 351 | absl::string_view *name) { |
| 352 | // For the same reason we merge configs in reverse order, we want to process |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 353 | // maps in reverse order. That lets the outer config overwrite channels from |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 354 | // the inner configs. |
| 355 | for (auto i = maps->rbegin(); i != maps->rend(); ++i) { |
| 356 | if (i->has_match() && i->match()->has_name() && i->has_rename() && |
| 357 | i->rename()->has_name() && i->match()->name()->string_view() == *name) { |
| 358 | VLOG(1) << "Renamed \"" << *name << "\" to \"" |
| 359 | << i->rename()->name()->string_view() << "\""; |
| 360 | *name = i->rename()->name()->string_view(); |
| 361 | } |
| 362 | } |
| 363 | } |
| 364 | |
Brian Silverman | 66f079a | 2013-08-26 16:24:30 -0700 | [diff] [blame] | 365 | } // namespace |
| 366 | |
| 367 | const char *GetRootDirectory() { |
John Park | 7bca981 | 2019-10-14 21:23:45 -0700 | [diff] [blame] | 368 | static char* root_dir;// return value |
| 369 | static absl::once_flag once_; |
| 370 | absl::call_once(once_, DoGetRootDirectory, &root_dir); |
| 371 | return root_dir; |
Brian Silverman | 66f079a | 2013-08-26 16:24:30 -0700 | [diff] [blame] | 372 | } |
| 373 | |
| 374 | const char *GetLoggingDirectory() { |
John Park | 7bca981 | 2019-10-14 21:23:45 -0700 | [diff] [blame] | 375 | static char* retu;// return value |
| 376 | static absl::once_flag once_; |
| 377 | absl::call_once(once_, DoGetLoggingDirectory, &retu); |
| 378 | return retu; |
Brian Silverman | 66f079a | 2013-08-26 16:24:30 -0700 | [diff] [blame] | 379 | } |
| 380 | |
| 381 | const in_addr &GetOwnIPAddress() { |
John Park | 7bca981 | 2019-10-14 21:23:45 -0700 | [diff] [blame] | 382 | static in_addr retu;// return value |
| 383 | static absl::once_flag once_; |
| 384 | absl::call_once(once_, DoGetOwnIPAddress, &retu); |
| 385 | return retu; |
Brian Silverman | 66f079a | 2013-08-26 16:24:30 -0700 | [diff] [blame] | 386 | } |
| 387 | |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 388 | FlatbufferDetachedBuffer<Configuration> ReadConfig( |
| 389 | const absl::string_view path) { |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 390 | // We only want to read a file once. So track the visited files in a set. |
| 391 | absl::btree_set<std::string> visited_paths; |
| 392 | return MergeConfiguration(ReadConfig(path, &visited_paths)); |
| 393 | } |
| 394 | |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 395 | const Channel *GetChannel(const Configuration *config, absl::string_view name, |
| 396 | absl::string_view type, |
| 397 | absl::string_view application_name) { |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 398 | VLOG(1) << "Looking up { \"name\": \"" << name << "\", \"type\": \"" << type |
| 399 | << "\" }"; |
| 400 | |
| 401 | // First handle application specific maps. Only do this if we have a matching |
| 402 | // application name, and it has maps. |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 403 | if (config->has_applications()) { |
| 404 | auto application_iterator = std::lower_bound( |
| 405 | config->applications()->cbegin(), config->applications()->cend(), |
| 406 | application_name, CompareApplications); |
| 407 | if (application_iterator != config->applications()->cend() && |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 408 | EqualsApplications(*application_iterator, application_name)) { |
| 409 | if (application_iterator->has_maps()) { |
| 410 | HandleMaps(application_iterator->maps(), &name); |
| 411 | } |
| 412 | } |
| 413 | } |
| 414 | |
| 415 | // Now do global maps. |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 416 | if (config->has_maps()) { |
| 417 | HandleMaps(config->maps(), &name); |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 418 | } |
| 419 | |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 420 | // Then look for the channel. |
| 421 | auto channel_iterator = |
| 422 | std::lower_bound(config->channels()->cbegin(), |
| 423 | config->channels()->cend(), |
| 424 | std::make_pair(name, type), CompareChannels); |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 425 | |
| 426 | // Make sure we actually found it, and it matches. |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 427 | if (channel_iterator != config->channels()->cend() && |
| 428 | EqualsChannels(*channel_iterator, std::make_pair(name, type))) { |
| 429 | VLOG(1) << "Found: " << FlatbufferToJson(*channel_iterator); |
| 430 | return *channel_iterator; |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 431 | } else { |
| 432 | VLOG(1) << "No match for { \"name\": \"" << name << "\", \"type\": \"" |
| 433 | << type << "\" }"; |
| 434 | return nullptr; |
| 435 | } |
| 436 | } |
| 437 | |
Brian Silverman | 66f079a | 2013-08-26 16:24:30 -0700 | [diff] [blame] | 438 | } // namespace configuration |
| 439 | } // namespace aos |