James Feist | c95cb14 | 2018-02-26 10:41:42 -0800 | [diff] [blame] | 1 | /* |
| 2 | // Copyright (c) 2018 Intel Corporation |
| 3 | // |
| 4 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | // you may not use this file except in compliance with the License. |
| 6 | // You may obtain a copy of the License at |
| 7 | // |
| 8 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software |
| 11 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | // See the License for the specific language governing permissions and |
| 14 | // limitations under the License. |
| 15 | */ |
| 16 | |
Patrick Venture | a49dc33 | 2019-10-26 08:32:02 -0700 | [diff] [blame] | 17 | #include "Overlay.hpp" |
| 18 | |
| 19 | #include "Utils.hpp" |
| 20 | #include "devices.hpp" |
| 21 | |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 22 | #include <boost/algorithm/string/predicate.hpp> |
| 23 | #include <boost/container/flat_map.hpp> |
| 24 | #include <boost/container/flat_set.hpp> |
| 25 | #include <boost/process/child.hpp> |
James Feist | 637b3ef | 2019-04-15 16:35:30 -0700 | [diff] [blame] | 26 | #include <filesystem> |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 27 | #include <iomanip> |
| 28 | #include <iostream> |
| 29 | #include <nlohmann/json.hpp> |
| 30 | #include <regex> |
| 31 | #include <string> |
James Feist | c95cb14 | 2018-02-26 10:41:42 -0800 | [diff] [blame] | 32 | |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 33 | constexpr const char* DT_OVERLAY = "/usr/bin/dtoverlay"; |
| 34 | constexpr const char* DTC = "/usr/bin/dtc"; |
| 35 | constexpr const char* OUTPUT_DIR = "/tmp/overlays"; |
| 36 | constexpr const char* TEMPLATE_DIR = PACKAGE_DIR "overlay_templates"; |
| 37 | constexpr const char* TEMPLATE_CHAR = "$"; |
| 38 | constexpr const char* HEX_FORMAT_STR = "0x"; |
| 39 | constexpr const char* PLATFORM = "aspeed,ast2500"; |
| 40 | constexpr const char* I2C_DEVS_DIR = "/sys/bus/i2c/devices"; |
| 41 | constexpr const char* MUX_SYMLINK_DIR = "/dev/i2c-mux"; |
James Feist | c95cb14 | 2018-02-26 10:41:42 -0800 | [diff] [blame] | 42 | |
Josh Lehan | 96b8a6e | 2019-10-09 14:39:49 -0700 | [diff] [blame] | 43 | constexpr const bool DEBUG = false; |
| 44 | |
James Feist | c95cb14 | 2018-02-26 10:41:42 -0800 | [diff] [blame] | 45 | // some drivers need to be unbind / bind to load device tree changes |
| 46 | static const boost::container::flat_map<std::string, std::string> FORCE_PROBES = |
| 47 | {{"IntelFanConnector", "/sys/bus/platform/drivers/aspeed_pwm_tacho"}}; |
| 48 | |
James Feist | c95cb14 | 2018-02-26 10:41:42 -0800 | [diff] [blame] | 49 | std::regex ILLEGAL_NAME_REGEX("[^A-Za-z0-9_]"); |
| 50 | |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 51 | void createOverlay(const std::string& templatePath, |
| 52 | const nlohmann::json& configuration); |
James Feist | c95cb14 | 2018-02-26 10:41:42 -0800 | [diff] [blame] | 53 | |
| 54 | void unloadAllOverlays(void) |
| 55 | { |
| 56 | boost::process::child c(DT_OVERLAY, "-d", OUTPUT_DIR, "-R"); |
| 57 | c.wait(); |
| 58 | } |
| 59 | |
| 60 | // this is hopefully temporary, but some drivers can't detect changes |
| 61 | // without an unbind and bind so this function must exist for now |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 62 | void forceProbe(const std::string& driver) |
James Feist | c95cb14 | 2018-02-26 10:41:42 -0800 | [diff] [blame] | 63 | { |
| 64 | std::ofstream driverUnbind(driver + "/unbind"); |
| 65 | std::ofstream driverBind(driver + "/bind"); |
| 66 | if (!driverUnbind.is_open()) |
| 67 | { |
| 68 | std::cerr << "force probe error opening " << driver << "\n"; |
| 69 | return; |
| 70 | } |
| 71 | if (!driverBind.is_open()) |
| 72 | { |
| 73 | driverUnbind.close(); |
| 74 | std::cerr << "force probe error opening " << driver << "\n"; |
| 75 | return; |
| 76 | } |
| 77 | |
Ed Tanous | 072e25d | 2018-12-16 21:45:20 -0800 | [diff] [blame] | 78 | std::filesystem::path pathObj(driver); |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 79 | for (auto& p : std::filesystem::directory_iterator(pathObj)) |
James Feist | c95cb14 | 2018-02-26 10:41:42 -0800 | [diff] [blame] | 80 | { |
| 81 | // symlinks are object names |
| 82 | if (is_symlink(p)) |
| 83 | { |
| 84 | std::string driverName = p.path().filename(); |
| 85 | driverUnbind << driverName; |
| 86 | driverBind << driverName; |
| 87 | break; |
| 88 | } |
| 89 | } |
| 90 | driverUnbind.close(); |
| 91 | driverBind.close(); |
| 92 | } |
| 93 | |
| 94 | // helper function to make json types into string |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 95 | std::string jsonToString(const nlohmann::json& in) |
James Feist | c95cb14 | 2018-02-26 10:41:42 -0800 | [diff] [blame] | 96 | { |
| 97 | if (in.type() == nlohmann::json::value_t::string) |
| 98 | { |
| 99 | return in.get<std::string>(); |
| 100 | } |
| 101 | else if (in.type() == nlohmann::json::value_t::array) |
| 102 | { |
| 103 | // remove brackets and comma from array |
| 104 | std::string array = in.dump(); |
| 105 | array = array.substr(1, array.size() - 2); |
| 106 | boost::replace_all(array, ",", " "); |
| 107 | return array; |
| 108 | } |
| 109 | return in.dump(); |
| 110 | } |
| 111 | |
Ed Tanous | ee3357a | 2019-02-26 12:44:14 -0800 | [diff] [blame] | 112 | void linkMux(const std::string& muxName, size_t busIndex, size_t address, |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 113 | const nlohmann::json::array_t& channelNames) |
James Feist | c95cb14 | 2018-02-26 10:41:42 -0800 | [diff] [blame] | 114 | { |
James Feist | 286babc | 2019-02-07 16:48:28 -0800 | [diff] [blame] | 115 | std::error_code ec; |
Ed Tanous | ee3357a | 2019-02-26 12:44:14 -0800 | [diff] [blame] | 116 | std::filesystem::path muxSymlinkDir(MUX_SYMLINK_DIR); |
| 117 | std::filesystem::create_directory(muxSymlinkDir, ec); |
| 118 | // ignore error codes here if the directory already exists |
| 119 | ec.clear(); |
| 120 | std::filesystem::path linkDir = muxSymlinkDir / muxName; |
| 121 | std::filesystem::create_directory(linkDir, ec); |
| 122 | |
| 123 | std::ostringstream hexAddress; |
| 124 | hexAddress << std::hex << std::setfill('0') << std::setw(4) << address; |
James Feist | 286babc | 2019-02-07 16:48:28 -0800 | [diff] [blame] | 125 | |
| 126 | std::filesystem::path devDir(I2C_DEVS_DIR); |
Ed Tanous | ee3357a | 2019-02-26 12:44:14 -0800 | [diff] [blame] | 127 | devDir /= std::to_string(busIndex) + "-" + hexAddress.str(); |
James Feist | 286babc | 2019-02-07 16:48:28 -0800 | [diff] [blame] | 128 | |
Ed Tanous | ee3357a | 2019-02-26 12:44:14 -0800 | [diff] [blame] | 129 | for (std::size_t channelIndex = 0; channelIndex < channelNames.size(); |
| 130 | channelIndex++) |
James Feist | 286babc | 2019-02-07 16:48:28 -0800 | [diff] [blame] | 131 | { |
Ed Tanous | ee3357a | 2019-02-26 12:44:14 -0800 | [diff] [blame] | 132 | const std::string* channelName = |
| 133 | channelNames[channelIndex].get_ptr<const std::string*>(); |
| 134 | if (channelName == nullptr) |
| 135 | { |
| 136 | continue; |
| 137 | } |
| 138 | if (channelName->empty()) |
| 139 | { |
| 140 | continue; |
| 141 | } |
| 142 | |
| 143 | std::filesystem::path channelPath = |
| 144 | devDir / ("channel-" + std::to_string(channelIndex)); |
| 145 | if (!is_symlink(channelPath)) |
| 146 | { |
| 147 | std::cerr << channelPath << "for mux channel " << *channelName |
| 148 | << " doesn't exist!\n"; |
| 149 | continue; |
| 150 | } |
| 151 | std::filesystem::path bus = std::filesystem::read_symlink(channelPath); |
| 152 | |
| 153 | std::filesystem::path fp("/dev" / bus.filename()); |
| 154 | std::filesystem::path link(linkDir / *channelName); |
| 155 | |
| 156 | std::filesystem::create_symlink(fp, link, ec); |
| 157 | if (ec) |
| 158 | { |
| 159 | std::cerr << "Failure creating symlink for " << fp << " to " << link |
| 160 | << "\n"; |
| 161 | } |
James Feist | c95cb14 | 2018-02-26 10:41:42 -0800 | [diff] [blame] | 162 | } |
| 163 | } |
| 164 | |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 165 | void exportDevice(const std::string& type, |
| 166 | const devices::ExportTemplate& exportTemplate, |
| 167 | const nlohmann::json& configuration) |
James Feist | 053a664 | 2018-10-15 13:17:09 -0700 | [diff] [blame] | 168 | { |
| 169 | |
| 170 | std::string parameters = exportTemplate.parameters; |
| 171 | std::string device = exportTemplate.device; |
| 172 | std::string name = "unknown"; |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 173 | const uint64_t* bus = nullptr; |
| 174 | const uint64_t* address = nullptr; |
| 175 | const nlohmann::json::array_t* channels = nullptr; |
James Feist | 053a664 | 2018-10-15 13:17:09 -0700 | [diff] [blame] | 176 | |
| 177 | for (auto keyPair = configuration.begin(); keyPair != configuration.end(); |
| 178 | keyPair++) |
| 179 | { |
| 180 | std::string subsituteString; |
| 181 | |
| 182 | if (keyPair.key() == "Name" && |
| 183 | keyPair.value().type() == nlohmann::json::value_t::string) |
| 184 | { |
| 185 | subsituteString = std::regex_replace( |
| 186 | keyPair.value().get<std::string>(), ILLEGAL_NAME_REGEX, "_"); |
| 187 | name = subsituteString; |
| 188 | } |
| 189 | else |
| 190 | { |
| 191 | subsituteString = jsonToString(keyPair.value()); |
| 192 | } |
| 193 | |
| 194 | if (keyPair.key() == "Bus") |
| 195 | { |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 196 | bus = keyPair.value().get_ptr<const uint64_t*>(); |
James Feist | 053a664 | 2018-10-15 13:17:09 -0700 | [diff] [blame] | 197 | } |
| 198 | else if (keyPair.key() == "Address") |
| 199 | { |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 200 | address = keyPair.value().get_ptr<const uint64_t*>(); |
James Feist | 053a664 | 2018-10-15 13:17:09 -0700 | [diff] [blame] | 201 | } |
James Feist | 286babc | 2019-02-07 16:48:28 -0800 | [diff] [blame] | 202 | else if (keyPair.key() == "ChannelNames") |
| 203 | { |
| 204 | channels = |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 205 | keyPair.value().get_ptr<const nlohmann::json::array_t*>(); |
James Feist | 286babc | 2019-02-07 16:48:28 -0800 | [diff] [blame] | 206 | } |
James Feist | 053a664 | 2018-10-15 13:17:09 -0700 | [diff] [blame] | 207 | boost::replace_all(parameters, TEMPLATE_CHAR + keyPair.key(), |
| 208 | subsituteString); |
| 209 | boost::replace_all(device, TEMPLATE_CHAR + keyPair.key(), |
| 210 | subsituteString); |
| 211 | } |
| 212 | |
| 213 | // if we found bus and address we can attempt to prevent errors |
| 214 | if (bus != nullptr && address != nullptr) |
| 215 | { |
| 216 | std::ostringstream hex; |
| 217 | hex << std::hex << *address; |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 218 | const std::string& addressHex = hex.str(); |
James Feist | 053a664 | 2018-10-15 13:17:09 -0700 | [diff] [blame] | 219 | std::string busStr = std::to_string(*bus); |
| 220 | |
Ed Tanous | 072e25d | 2018-12-16 21:45:20 -0800 | [diff] [blame] | 221 | std::filesystem::path devicePath(device); |
James Feist | 58b9c26 | 2019-02-12 13:38:45 -0800 | [diff] [blame] | 222 | std::filesystem::path parentPath = devicePath.parent_path(); |
| 223 | if (std::filesystem::is_directory(parentPath)) |
James Feist | 053a664 | 2018-10-15 13:17:09 -0700 | [diff] [blame] | 224 | { |
James Feist | 58b9c26 | 2019-02-12 13:38:45 -0800 | [diff] [blame] | 225 | for (const auto& path : |
| 226 | std::filesystem::directory_iterator(parentPath)) |
James Feist | 053a664 | 2018-10-15 13:17:09 -0700 | [diff] [blame] | 227 | { |
James Feist | 58b9c26 | 2019-02-12 13:38:45 -0800 | [diff] [blame] | 228 | if (!std::filesystem::is_directory(path)) |
| 229 | { |
| 230 | continue; |
| 231 | } |
James Feist | 053a664 | 2018-10-15 13:17:09 -0700 | [diff] [blame] | 232 | |
James Feist | 58b9c26 | 2019-02-12 13:38:45 -0800 | [diff] [blame] | 233 | const std::string& directoryName = path.path().filename(); |
| 234 | if (boost::starts_with(directoryName, busStr) && |
| 235 | boost::ends_with(directoryName, addressHex)) |
| 236 | { |
| 237 | return; // already exported |
| 238 | } |
James Feist | 053a664 | 2018-10-15 13:17:09 -0700 | [diff] [blame] | 239 | } |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | std::ofstream deviceFile(device); |
| 244 | if (!deviceFile.good()) |
| 245 | { |
| 246 | std::cerr << "Error writing " << device << "\n"; |
| 247 | return; |
| 248 | } |
| 249 | deviceFile << parameters; |
| 250 | deviceFile.close(); |
James Feist | 286babc | 2019-02-07 16:48:28 -0800 | [diff] [blame] | 251 | if (boost::ends_with(type, "Mux") && bus && address && channels) |
| 252 | { |
James Feist | 9813279 | 2019-07-09 13:29:09 -0700 | [diff] [blame] | 253 | linkMux(name, static_cast<size_t>(*bus), static_cast<size_t>(*address), |
| 254 | *channels); |
James Feist | 286babc | 2019-02-07 16:48:28 -0800 | [diff] [blame] | 255 | } |
James Feist | 053a664 | 2018-10-15 13:17:09 -0700 | [diff] [blame] | 256 | } |
| 257 | |
| 258 | // this is now deprecated |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 259 | void createOverlay(const std::string& templatePath, |
| 260 | const nlohmann::json& configuration) |
James Feist | c95cb14 | 2018-02-26 10:41:42 -0800 | [diff] [blame] | 261 | { |
| 262 | std::ifstream templateFile(templatePath); |
| 263 | |
| 264 | if (!templateFile.is_open()) |
| 265 | { |
| 266 | std::cerr << "createOverlay error opening " << templatePath << "\n"; |
| 267 | return; |
| 268 | } |
| 269 | std::stringstream buff; |
| 270 | buff << templateFile.rdbuf(); |
| 271 | std::string templateStr = buff.str(); |
| 272 | std::string name = "unknown"; |
James Feist | d63d18a | 2018-07-19 15:23:45 -0700 | [diff] [blame] | 273 | std::string type = configuration["Type"]; |
James Feist | c95cb14 | 2018-02-26 10:41:42 -0800 | [diff] [blame] | 274 | for (auto keyPair = configuration.begin(); keyPair != configuration.end(); |
| 275 | keyPair++) |
| 276 | { |
| 277 | std::string subsituteString; |
| 278 | |
| 279 | // device tree symbols are in decimal |
James Feist | 1e3e698 | 2018-08-03 16:09:28 -0700 | [diff] [blame] | 280 | if (keyPair.key() == "Bus" && |
James Feist | c95cb14 | 2018-02-26 10:41:42 -0800 | [diff] [blame] | 281 | keyPair.value().type() == nlohmann::json::value_t::string) |
| 282 | { |
James Feist | 9813279 | 2019-07-09 13:29:09 -0700 | [diff] [blame] | 283 | long unsigned int dec = |
James Feist | c95cb14 | 2018-02-26 10:41:42 -0800 | [diff] [blame] | 284 | std::stoul(keyPair.value().get<std::string>(), nullptr, 16); |
| 285 | subsituteString = std::to_string(dec); |
| 286 | } |
James Feist | d63d18a | 2018-07-19 15:23:45 -0700 | [diff] [blame] | 287 | else if (keyPair.key() == "Name" && |
James Feist | c95cb14 | 2018-02-26 10:41:42 -0800 | [diff] [blame] | 288 | keyPair.value().type() == nlohmann::json::value_t::string) |
| 289 | { |
| 290 | subsituteString = std::regex_replace( |
| 291 | keyPair.value().get<std::string>(), ILLEGAL_NAME_REGEX, "_"); |
| 292 | name = subsituteString; |
| 293 | } |
James Feist | 1e3e698 | 2018-08-03 16:09:28 -0700 | [diff] [blame] | 294 | else if (keyPair.key() == "Address") |
Jae Hyun Yoo | 6d1d014 | 2018-07-25 10:07:43 -0700 | [diff] [blame] | 295 | { |
| 296 | if (keyPair.value().type() == nlohmann::json::value_t::string) |
| 297 | { |
| 298 | subsituteString = keyPair.value().get<std::string>(); |
| 299 | subsituteString.erase( |
| 300 | 0, subsituteString.find_first_not_of(HEX_FORMAT_STR)); |
| 301 | } |
| 302 | else |
| 303 | { |
| 304 | std::ostringstream hex; |
| 305 | hex << std::hex << keyPair.value().get<unsigned int>(); |
| 306 | subsituteString = hex.str(); |
| 307 | } |
| 308 | } |
James Feist | c95cb14 | 2018-02-26 10:41:42 -0800 | [diff] [blame] | 309 | else |
| 310 | { |
| 311 | subsituteString = jsonToString(keyPair.value()); |
| 312 | } |
| 313 | boost::replace_all(templateStr, TEMPLATE_CHAR + keyPair.key(), |
| 314 | subsituteString); |
| 315 | } |
| 316 | // todo: this is a lame way to fill in platform, but we only |
| 317 | // care about ast2500 right now |
James Feist | 1e3e698 | 2018-08-03 16:09:28 -0700 | [diff] [blame] | 318 | boost::replace_all(templateStr, TEMPLATE_CHAR + std::string("Platform"), |
James Feist | c95cb14 | 2018-02-26 10:41:42 -0800 | [diff] [blame] | 319 | PLATFORM); |
| 320 | std::string dtsFilename = |
| 321 | std::string(OUTPUT_DIR) + "/" + name + "_" + type + ".dts"; |
| 322 | std::string dtboFilename = |
| 323 | std::string(OUTPUT_DIR) + "/" + name + "_" + type + ".dtbo"; |
| 324 | |
| 325 | std::ofstream out(dtsFilename); |
| 326 | if (!out.is_open()) |
| 327 | { |
| 328 | std::cerr << "createOverlay error opening " << dtsFilename << "\n"; |
| 329 | return; |
| 330 | } |
| 331 | out << templateStr; |
| 332 | out.close(); |
| 333 | |
James Feist | c95cb14 | 2018-02-26 10:41:42 -0800 | [diff] [blame] | 334 | // compile dtbo and load overlay |
| 335 | boost::process::child c1(DTC, "-@", "-q", "-I", "dts", "-O", "dtb", "-o", |
| 336 | dtboFilename, dtsFilename); |
| 337 | c1.wait(); |
| 338 | if (c1.exit_code()) |
| 339 | { |
| 340 | std::cerr << "DTC error with file " << dtsFilename << "\n"; |
| 341 | return; |
| 342 | } |
| 343 | boost::process::child c2(DT_OVERLAY, "-d", OUTPUT_DIR, name + "_" + type); |
| 344 | c2.wait(); |
| 345 | if (c2.exit_code()) |
| 346 | { |
| 347 | std::cerr << "DTOverlay error with file " << dtboFilename << "\n"; |
| 348 | return; |
| 349 | } |
| 350 | auto findForceProbe = FORCE_PROBES.find(type); |
| 351 | if (findForceProbe != FORCE_PROBES.end()) |
| 352 | { |
| 353 | forceProbe(findForceProbe->second); |
| 354 | } |
James Feist | c95cb14 | 2018-02-26 10:41:42 -0800 | [diff] [blame] | 355 | } |
| 356 | |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 357 | bool loadOverlays(const nlohmann::json& systemConfiguration) |
James Feist | c95cb14 | 2018-02-26 10:41:42 -0800 | [diff] [blame] | 358 | { |
| 359 | |
Ed Tanous | 072e25d | 2018-12-16 21:45:20 -0800 | [diff] [blame] | 360 | std::vector<std::filesystem::path> paths; |
James Feist | f861da8 | 2019-06-27 12:05:16 -0700 | [diff] [blame] | 361 | if (!findFiles(std::filesystem::path(TEMPLATE_DIR), R"(.*\.template)", |
| 362 | paths)) |
James Feist | c95cb14 | 2018-02-26 10:41:42 -0800 | [diff] [blame] | 363 | { |
Josh Lehan | e5cfe64 | 2019-10-09 14:21:35 -0700 | [diff] [blame] | 364 | std::cerr << "Unable to find any template files in " << TEMPLATE_DIR |
James Feist | c95cb14 | 2018-02-26 10:41:42 -0800 | [diff] [blame] | 365 | << "\n"; |
| 366 | return false; |
| 367 | } |
| 368 | |
Ed Tanous | 072e25d | 2018-12-16 21:45:20 -0800 | [diff] [blame] | 369 | std::filesystem::create_directory(OUTPUT_DIR); |
James Feist | c95cb14 | 2018-02-26 10:41:42 -0800 | [diff] [blame] | 370 | for (auto entity = systemConfiguration.begin(); |
| 371 | entity != systemConfiguration.end(); entity++) |
| 372 | { |
James Feist | 1e3e698 | 2018-08-03 16:09:28 -0700 | [diff] [blame] | 373 | auto findExposes = entity.value().find("Exposes"); |
James Feist | c95cb14 | 2018-02-26 10:41:42 -0800 | [diff] [blame] | 374 | if (findExposes == entity.value().end() || |
| 375 | findExposes->type() != nlohmann::json::value_t::array) |
| 376 | { |
| 377 | continue; |
| 378 | } |
| 379 | |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 380 | for (auto& configuration : *findExposes) |
James Feist | c95cb14 | 2018-02-26 10:41:42 -0800 | [diff] [blame] | 381 | { |
James Feist | 1e3e698 | 2018-08-03 16:09:28 -0700 | [diff] [blame] | 382 | auto findStatus = configuration.find("Status"); |
James Feist | c95cb14 | 2018-02-26 10:41:42 -0800 | [diff] [blame] | 383 | // status missing is assumed to be 'okay' |
| 384 | if (findStatus != configuration.end() && *findStatus == "disabled") |
| 385 | { |
| 386 | continue; |
| 387 | } |
James Feist | d63d18a | 2018-07-19 15:23:45 -0700 | [diff] [blame] | 388 | auto findType = configuration.find("Type"); |
James Feist | c95cb14 | 2018-02-26 10:41:42 -0800 | [diff] [blame] | 389 | if (findType == configuration.end() || |
| 390 | findType->type() != nlohmann::json::value_t::string) |
| 391 | { |
| 392 | continue; |
| 393 | } |
| 394 | std::string type = findType.value().get<std::string>(); |
James Feist | ce4367c | 2018-10-16 09:19:57 -0700 | [diff] [blame] | 395 | #if OVERLAYS |
| 396 | |
James Feist | c95cb14 | 2018-02-26 10:41:42 -0800 | [diff] [blame] | 397 | std::string typeFile = type + std::string(".template"); |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 398 | for (const auto& path : paths) |
James Feist | c95cb14 | 2018-02-26 10:41:42 -0800 | [diff] [blame] | 399 | { |
| 400 | if (path.filename() != typeFile) |
| 401 | { |
| 402 | continue; |
| 403 | } |
| 404 | createOverlay(path.string(), configuration); |
| 405 | break; |
| 406 | } |
James Feist | ce4367c | 2018-10-16 09:19:57 -0700 | [diff] [blame] | 407 | #endif |
James Feist | 053a664 | 2018-10-15 13:17:09 -0700 | [diff] [blame] | 408 | auto device = devices::exportTemplates.find(type.c_str()); |
| 409 | if (device != devices::exportTemplates.end()) |
| 410 | { |
James Feist | 286babc | 2019-02-07 16:48:28 -0800 | [diff] [blame] | 411 | exportDevice(type, device->second, configuration); |
Josh Lehan | 96b8a6e | 2019-10-09 14:39:49 -0700 | [diff] [blame] | 412 | continue; |
| 413 | } |
| 414 | |
| 415 | // Because many devices are intentionally not exportable, |
| 416 | // this error message is not printed in all situations. |
| 417 | // If wondering why your device not appearing, add your type to |
| 418 | // the exportTemplates array in the devices.hpp file. |
| 419 | if constexpr (DEBUG) |
| 420 | { |
| 421 | std::cerr << "Device type " << type |
| 422 | << " not found in export map whitelist\n"; |
James Feist | 053a664 | 2018-10-15 13:17:09 -0700 | [diff] [blame] | 423 | } |
James Feist | c95cb14 | 2018-02-26 10:41:42 -0800 | [diff] [blame] | 424 | } |
| 425 | } |
| 426 | |
| 427 | return true; |
Jae Hyun Yoo | 6d1d014 | 2018-07-25 10:07:43 -0700 | [diff] [blame] | 428 | } |