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 | */ |
Brad Bishop | e45d8c7 | 2022-05-25 15:12:53 -0400 | [diff] [blame] | 16 | /// \file overlay.cpp |
James Feist | c95cb14 | 2018-02-26 10:41:42 -0800 | [diff] [blame] | 17 | |
Brad Bishop | e45d8c7 | 2022-05-25 15:12:53 -0400 | [diff] [blame] | 18 | #include "overlay.hpp" |
Patrick Venture | a49dc33 | 2019-10-26 08:32:02 -0700 | [diff] [blame] | 19 | |
Brad Bishop | e45d8c7 | 2022-05-25 15:12:53 -0400 | [diff] [blame] | 20 | #include "utils.hpp" |
Patrick Venture | a49dc33 | 2019-10-26 08:32:02 -0700 | [diff] [blame] | 21 | #include "devices.hpp" |
| 22 | |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 23 | #include <boost/algorithm/string/predicate.hpp> |
Johnathan Mantey | 9b86787 | 2020-10-13 15:00:51 -0700 | [diff] [blame] | 24 | #include <boost/asio/io_context.hpp> |
| 25 | #include <boost/asio/steady_timer.hpp> |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 26 | #include <boost/container/flat_map.hpp> |
| 27 | #include <boost/container/flat_set.hpp> |
| 28 | #include <boost/process/child.hpp> |
James Feist | 8c505da | 2020-05-28 10:06:33 -0700 | [diff] [blame] | 29 | #include <nlohmann/json.hpp> |
| 30 | |
James Feist | 637b3ef | 2019-04-15 16:35:30 -0700 | [diff] [blame] | 31 | #include <filesystem> |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 32 | #include <iomanip> |
| 33 | #include <iostream> |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 34 | #include <regex> |
| 35 | #include <string> |
James Feist | c95cb14 | 2018-02-26 10:41:42 -0800 | [diff] [blame] | 36 | |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame] | 37 | constexpr const char* outputDir = "/tmp/overlays"; |
| 38 | constexpr const char* templateChar = "$"; |
| 39 | constexpr const char* i2CDevsDir = "/sys/bus/i2c/devices"; |
| 40 | constexpr const char* muxSymlinkDir = "/dev/i2c-mux"; |
James Feist | c95cb14 | 2018-02-26 10:41:42 -0800 | [diff] [blame] | 41 | |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame] | 42 | constexpr const bool debug = false; |
Josh Lehan | 96b8a6e | 2019-10-09 14:39:49 -0700 | [diff] [blame] | 43 | |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame] | 44 | std::regex illegalNameRegex("[^A-Za-z0-9_]"); |
James Feist | c95cb14 | 2018-02-26 10:41:42 -0800 | [diff] [blame] | 45 | |
James Feist | c95cb14 | 2018-02-26 10:41:42 -0800 | [diff] [blame] | 46 | // helper function to make json types into string |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 47 | std::string jsonToString(const nlohmann::json& in) |
James Feist | c95cb14 | 2018-02-26 10:41:42 -0800 | [diff] [blame] | 48 | { |
| 49 | if (in.type() == nlohmann::json::value_t::string) |
| 50 | { |
| 51 | return in.get<std::string>(); |
| 52 | } |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame] | 53 | if (in.type() == nlohmann::json::value_t::array) |
James Feist | c95cb14 | 2018-02-26 10:41:42 -0800 | [diff] [blame] | 54 | { |
| 55 | // remove brackets and comma from array |
| 56 | std::string array = in.dump(); |
| 57 | array = array.substr(1, array.size() - 2); |
| 58 | boost::replace_all(array, ",", " "); |
| 59 | return array; |
| 60 | } |
| 61 | return in.dump(); |
| 62 | } |
| 63 | |
Zev Weiss | 9afef3a | 2022-07-13 16:38:23 -0700 | [diff] [blame^] | 64 | static std::string deviceDirName(uint64_t bus, uint64_t address) |
| 65 | { |
| 66 | std::ostringstream name; |
| 67 | name << bus << "-" << std::hex << std::setw(4) << std::setfill('0') << address; |
| 68 | return name.str(); |
| 69 | } |
| 70 | |
Ed Tanous | ee3357a | 2019-02-26 12:44:14 -0800 | [diff] [blame] | 71 | void linkMux(const std::string& muxName, size_t busIndex, size_t address, |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 72 | const nlohmann::json::array_t& channelNames) |
James Feist | c95cb14 | 2018-02-26 10:41:42 -0800 | [diff] [blame] | 73 | { |
James Feist | 286babc | 2019-02-07 16:48:28 -0800 | [diff] [blame] | 74 | std::error_code ec; |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame] | 75 | std::filesystem::path muxSymlinkDirPath(muxSymlinkDir); |
| 76 | std::filesystem::create_directory(muxSymlinkDirPath, ec); |
Ed Tanous | ee3357a | 2019-02-26 12:44:14 -0800 | [diff] [blame] | 77 | // ignore error codes here if the directory already exists |
| 78 | ec.clear(); |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame] | 79 | std::filesystem::path linkDir = muxSymlinkDirPath / muxName; |
Ed Tanous | ee3357a | 2019-02-26 12:44:14 -0800 | [diff] [blame] | 80 | std::filesystem::create_directory(linkDir, ec); |
| 81 | |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame] | 82 | std::filesystem::path devDir(i2CDevsDir); |
Zev Weiss | 9afef3a | 2022-07-13 16:38:23 -0700 | [diff] [blame^] | 83 | devDir /= deviceDirName(busIndex, address); |
James Feist | 286babc | 2019-02-07 16:48:28 -0800 | [diff] [blame] | 84 | |
Ed Tanous | ee3357a | 2019-02-26 12:44:14 -0800 | [diff] [blame] | 85 | for (std::size_t channelIndex = 0; channelIndex < channelNames.size(); |
| 86 | channelIndex++) |
James Feist | 286babc | 2019-02-07 16:48:28 -0800 | [diff] [blame] | 87 | { |
Ed Tanous | ee3357a | 2019-02-26 12:44:14 -0800 | [diff] [blame] | 88 | const std::string* channelName = |
| 89 | channelNames[channelIndex].get_ptr<const std::string*>(); |
| 90 | if (channelName == nullptr) |
| 91 | { |
| 92 | continue; |
| 93 | } |
| 94 | if (channelName->empty()) |
| 95 | { |
| 96 | continue; |
| 97 | } |
| 98 | |
| 99 | std::filesystem::path channelPath = |
| 100 | devDir / ("channel-" + std::to_string(channelIndex)); |
| 101 | if (!is_symlink(channelPath)) |
| 102 | { |
Zev Weiss | e7d56e0 | 2022-07-15 12:58:30 -0700 | [diff] [blame] | 103 | std::cerr << channelPath << " for mux channel " << *channelName |
Ed Tanous | ee3357a | 2019-02-26 12:44:14 -0800 | [diff] [blame] | 104 | << " doesn't exist!\n"; |
| 105 | continue; |
| 106 | } |
| 107 | std::filesystem::path bus = std::filesystem::read_symlink(channelPath); |
| 108 | |
| 109 | std::filesystem::path fp("/dev" / bus.filename()); |
| 110 | std::filesystem::path link(linkDir / *channelName); |
| 111 | |
| 112 | std::filesystem::create_symlink(fp, link, ec); |
| 113 | if (ec) |
| 114 | { |
| 115 | std::cerr << "Failure creating symlink for " << fp << " to " << link |
| 116 | << "\n"; |
| 117 | } |
James Feist | c95cb14 | 2018-02-26 10:41:42 -0800 | [diff] [blame] | 118 | } |
| 119 | } |
| 120 | |
Zev Weiss | c11b5da | 2022-07-12 16:31:37 -0700 | [diff] [blame] | 121 | static int deleteDevice(const std::string& busPath, |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame] | 122 | const std::shared_ptr<uint64_t>& address, |
Johnathan Mantey | 9b86787 | 2020-10-13 15:00:51 -0700 | [diff] [blame] | 123 | const std::string& destructor) |
| 124 | { |
| 125 | if (!address) |
| 126 | { |
| 127 | return -1; |
| 128 | } |
Zev Weiss | c11b5da | 2022-07-12 16:31:37 -0700 | [diff] [blame] | 129 | std::filesystem::path deviceDestructor(busPath); |
Johnathan Mantey | 9b86787 | 2020-10-13 15:00:51 -0700 | [diff] [blame] | 130 | deviceDestructor /= destructor; |
| 131 | std::ofstream deviceFile(deviceDestructor); |
| 132 | if (!deviceFile.good()) |
| 133 | { |
| 134 | std::cerr << "Error writing " << deviceDestructor << "\n"; |
| 135 | return -1; |
| 136 | } |
| 137 | deviceFile << std::to_string(*address); |
| 138 | deviceFile.close(); |
| 139 | return 0; |
| 140 | } |
| 141 | |
Zev Weiss | c11b5da | 2022-07-12 16:31:37 -0700 | [diff] [blame] | 142 | static int createDevice(const std::string& busPath, |
Johnathan Mantey | 9b86787 | 2020-10-13 15:00:51 -0700 | [diff] [blame] | 143 | const std::string& parameters, |
| 144 | const std::string& constructor) |
| 145 | { |
Zev Weiss | c11b5da | 2022-07-12 16:31:37 -0700 | [diff] [blame] | 146 | std::filesystem::path deviceConstructor(busPath); |
Johnathan Mantey | 9b86787 | 2020-10-13 15:00:51 -0700 | [diff] [blame] | 147 | deviceConstructor /= constructor; |
| 148 | std::ofstream deviceFile(deviceConstructor); |
| 149 | if (!deviceFile.good()) |
| 150 | { |
| 151 | std::cerr << "Error writing " << deviceConstructor << "\n"; |
| 152 | return -1; |
| 153 | } |
| 154 | deviceFile << parameters; |
| 155 | deviceFile.close(); |
| 156 | |
| 157 | return 0; |
| 158 | } |
| 159 | |
Zev Weiss | c11b5da | 2022-07-12 16:31:37 -0700 | [diff] [blame] | 160 | static bool deviceIsCreated(const std::string& busPath, |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame] | 161 | const std::shared_ptr<uint64_t>& bus, |
| 162 | const std::shared_ptr<uint64_t>& address, |
Johnathan Mantey | 9b86787 | 2020-10-13 15:00:51 -0700 | [diff] [blame] | 163 | const bool retrying) |
| 164 | { |
| 165 | if (!bus || !address) |
| 166 | { |
| 167 | return false; |
| 168 | } |
| 169 | |
Zev Weiss | 9afef3a | 2022-07-13 16:38:23 -0700 | [diff] [blame^] | 170 | std::string dirName = deviceDirName(*bus, *address); |
Johnathan Mantey | 9b86787 | 2020-10-13 15:00:51 -0700 | [diff] [blame] | 171 | |
Ed Tanous | 37e142b | 2021-04-30 12:19:26 -0700 | [diff] [blame] | 172 | std::error_code ec; |
Zev Weiss | c11b5da | 2022-07-12 16:31:37 -0700 | [diff] [blame] | 173 | auto path = std::filesystem::recursive_directory_iterator(busPath, ec); |
Ed Tanous | 37e142b | 2021-04-30 12:19:26 -0700 | [diff] [blame] | 174 | if (ec) |
| 175 | { |
Zev Weiss | c11b5da | 2022-07-12 16:31:37 -0700 | [diff] [blame] | 176 | std::cerr << "Unable to open path " << busPath << "\n"; |
Ed Tanous | 37e142b | 2021-04-30 12:19:26 -0700 | [diff] [blame] | 177 | return false; |
| 178 | } |
| 179 | for (; path != std::filesystem::recursive_directory_iterator(); path++) |
Johnathan Mantey | 9b86787 | 2020-10-13 15:00:51 -0700 | [diff] [blame] | 180 | { |
| 181 | if (!std::filesystem::is_directory(*path)) |
| 182 | { |
| 183 | continue; |
| 184 | } |
| 185 | |
Zev Weiss | 9afef3a | 2022-07-13 16:38:23 -0700 | [diff] [blame^] | 186 | const std::string foundName = path->path().filename(); |
| 187 | if (foundName == dirName) |
Johnathan Mantey | 9b86787 | 2020-10-13 15:00:51 -0700 | [diff] [blame] | 188 | { |
| 189 | // The first time the BMC boots the kernel has creates a |
| 190 | // filesystem enumerating the I2C devices. The I2C device has not |
| 191 | // been initialized for use. This requires a call to a device |
| 192 | // node, such as "new_device". The first pass through this |
| 193 | // function is only confirming the filesystem contains the device |
| 194 | // entry of interest (i.e. i2c4-0050). |
| 195 | // |
| 196 | // An upper level function performs the device creation |
| 197 | // action. This action may fail. The device driver (dd) used to |
| 198 | // create the I2C filesystem substructure eats any error codes, |
| 199 | // and always returns 0. This is by design. It is also possible |
| 200 | // for the new_device action to fail because the device is not |
| 201 | // actually in the system, i.e. optional equipment. |
| 202 | // |
| 203 | // The 'retrying' pass of this function is used to confirm the |
| 204 | // 'dd' device driver succeeded. Success is measured by finding |
| 205 | // the 'hwmon' subdirectory in the filesystem. The first attempt |
| 206 | // is delayed by an arbitrary amount, in order to permit the |
| 207 | // kernel time to create the filesystem entries. The upper level |
| 208 | // function determines the number of times to retry calling this |
| 209 | // function. |
| 210 | if (retrying) |
| 211 | { |
| 212 | std::error_code ec; |
Zev Weiss | c11b5da | 2022-07-12 16:31:37 -0700 | [diff] [blame] | 213 | std::filesystem::path hwmonDir(busPath); |
Zev Weiss | 9afef3a | 2022-07-13 16:38:23 -0700 | [diff] [blame^] | 214 | hwmonDir /= dirName; |
Johnathan Mantey | 9b86787 | 2020-10-13 15:00:51 -0700 | [diff] [blame] | 215 | hwmonDir /= "hwmon"; |
| 216 | return std::filesystem::is_directory(hwmonDir, ec); |
| 217 | } |
| 218 | return true; |
| 219 | } |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame] | 220 | path.disable_recursion_pending(); |
Johnathan Mantey | 9b86787 | 2020-10-13 15:00:51 -0700 | [diff] [blame] | 221 | } |
| 222 | return false; |
| 223 | } |
| 224 | |
Zev Weiss | c11b5da | 2022-07-12 16:31:37 -0700 | [diff] [blame] | 225 | static int buildDevice(const std::string& busPath, |
Johnathan Mantey | 9b86787 | 2020-10-13 15:00:51 -0700 | [diff] [blame] | 226 | const std::string& parameters, |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame] | 227 | const std::shared_ptr<uint64_t>& bus, |
| 228 | const std::shared_ptr<uint64_t>& address, |
Johnathan Mantey | 9b86787 | 2020-10-13 15:00:51 -0700 | [diff] [blame] | 229 | const std::string& constructor, |
| 230 | const std::string& destructor, const bool createsHWMon, |
| 231 | const size_t retries = 5) |
| 232 | { |
| 233 | bool tryAgain = false; |
| 234 | if (!retries) |
| 235 | { |
| 236 | return -1; |
| 237 | } |
| 238 | |
Zev Weiss | c11b5da | 2022-07-12 16:31:37 -0700 | [diff] [blame] | 239 | if (!deviceIsCreated(busPath, bus, address, false)) |
Johnathan Mantey | 9b86787 | 2020-10-13 15:00:51 -0700 | [diff] [blame] | 240 | { |
Zev Weiss | c11b5da | 2022-07-12 16:31:37 -0700 | [diff] [blame] | 241 | createDevice(busPath, parameters, constructor); |
Johnathan Mantey | 9b86787 | 2020-10-13 15:00:51 -0700 | [diff] [blame] | 242 | tryAgain = true; |
| 243 | } |
Zev Weiss | c11b5da | 2022-07-12 16:31:37 -0700 | [diff] [blame] | 244 | else if (createsHWMon && !deviceIsCreated(busPath, bus, address, true)) |
Johnathan Mantey | 9b86787 | 2020-10-13 15:00:51 -0700 | [diff] [blame] | 245 | { |
| 246 | // device is present, hwmon subdir missing |
Zev Weiss | c11b5da | 2022-07-12 16:31:37 -0700 | [diff] [blame] | 247 | deleteDevice(busPath, address, destructor); |
Johnathan Mantey | 9b86787 | 2020-10-13 15:00:51 -0700 | [diff] [blame] | 248 | tryAgain = true; |
| 249 | } |
| 250 | |
| 251 | if (tryAgain) |
| 252 | { |
| 253 | std::shared_ptr<boost::asio::steady_timer> createTimer = |
| 254 | std::make_shared<boost::asio::steady_timer>(io); |
| 255 | createTimer->expires_after(std::chrono::milliseconds(500)); |
Zev Weiss | c11b5da | 2022-07-12 16:31:37 -0700 | [diff] [blame] | 256 | createTimer->async_wait([createTimer, busPath, parameters, bus, |
Johnathan Mantey | 9b86787 | 2020-10-13 15:00:51 -0700 | [diff] [blame] | 257 | address, constructor, destructor, createsHWMon, |
| 258 | retries](const boost::system::error_code& ec) { |
| 259 | if (ec) |
| 260 | { |
| 261 | std::cerr << "Timer error: " << ec << "\n"; |
| 262 | return -2; |
| 263 | } |
Zev Weiss | c11b5da | 2022-07-12 16:31:37 -0700 | [diff] [blame] | 264 | return buildDevice(busPath, parameters, bus, address, |
Johnathan Mantey | 9b86787 | 2020-10-13 15:00:51 -0700 | [diff] [blame] | 265 | constructor, destructor, createsHWMon, |
| 266 | retries - 1); |
| 267 | }); |
| 268 | } |
| 269 | return 0; |
| 270 | } |
| 271 | |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 272 | void exportDevice(const std::string& type, |
| 273 | const devices::ExportTemplate& exportTemplate, |
| 274 | const nlohmann::json& configuration) |
James Feist | 053a664 | 2018-10-15 13:17:09 -0700 | [diff] [blame] | 275 | { |
| 276 | |
| 277 | std::string parameters = exportTemplate.parameters; |
Zev Weiss | c11b5da | 2022-07-12 16:31:37 -0700 | [diff] [blame] | 278 | std::string busPath = exportTemplate.busPath; |
Johnathan Mantey | 9b86787 | 2020-10-13 15:00:51 -0700 | [diff] [blame] | 279 | std::string constructor = exportTemplate.add; |
| 280 | std::string destructor = exportTemplate.remove; |
| 281 | bool createsHWMon = exportTemplate.createsHWMon; |
James Feist | 053a664 | 2018-10-15 13:17:09 -0700 | [diff] [blame] | 282 | std::string name = "unknown"; |
Johnathan Mantey | 9b86787 | 2020-10-13 15:00:51 -0700 | [diff] [blame] | 283 | std::shared_ptr<uint64_t> bus = nullptr; |
| 284 | std::shared_ptr<uint64_t> address = nullptr; |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 285 | const nlohmann::json::array_t* channels = nullptr; |
James Feist | 053a664 | 2018-10-15 13:17:09 -0700 | [diff] [blame] | 286 | |
| 287 | for (auto keyPair = configuration.begin(); keyPair != configuration.end(); |
| 288 | keyPair++) |
| 289 | { |
| 290 | std::string subsituteString; |
| 291 | |
| 292 | if (keyPair.key() == "Name" && |
| 293 | keyPair.value().type() == nlohmann::json::value_t::string) |
| 294 | { |
| 295 | subsituteString = std::regex_replace( |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame] | 296 | keyPair.value().get<std::string>(), illegalNameRegex, "_"); |
James Feist | 053a664 | 2018-10-15 13:17:09 -0700 | [diff] [blame] | 297 | name = subsituteString; |
| 298 | } |
| 299 | else |
| 300 | { |
| 301 | subsituteString = jsonToString(keyPair.value()); |
| 302 | } |
| 303 | |
| 304 | if (keyPair.key() == "Bus") |
| 305 | { |
Johnathan Mantey | 9b86787 | 2020-10-13 15:00:51 -0700 | [diff] [blame] | 306 | bus = std::make_shared<uint64_t>( |
| 307 | *keyPair.value().get_ptr<const uint64_t*>()); |
James Feist | 053a664 | 2018-10-15 13:17:09 -0700 | [diff] [blame] | 308 | } |
| 309 | else if (keyPair.key() == "Address") |
| 310 | { |
Johnathan Mantey | 9b86787 | 2020-10-13 15:00:51 -0700 | [diff] [blame] | 311 | address = std::make_shared<uint64_t>( |
| 312 | *keyPair.value().get_ptr<const uint64_t*>()); |
James Feist | 053a664 | 2018-10-15 13:17:09 -0700 | [diff] [blame] | 313 | } |
James Feist | 286babc | 2019-02-07 16:48:28 -0800 | [diff] [blame] | 314 | else if (keyPair.key() == "ChannelNames") |
| 315 | { |
| 316 | channels = |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 317 | keyPair.value().get_ptr<const nlohmann::json::array_t*>(); |
James Feist | 286babc | 2019-02-07 16:48:28 -0800 | [diff] [blame] | 318 | } |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame] | 319 | boost::replace_all(parameters, templateChar + keyPair.key(), |
James Feist | 053a664 | 2018-10-15 13:17:09 -0700 | [diff] [blame] | 320 | subsituteString); |
Zev Weiss | c11b5da | 2022-07-12 16:31:37 -0700 | [diff] [blame] | 321 | boost::replace_all(busPath, templateChar + keyPair.key(), |
James Feist | 053a664 | 2018-10-15 13:17:09 -0700 | [diff] [blame] | 322 | subsituteString); |
| 323 | } |
| 324 | |
Zev Weiss | c11b5da | 2022-07-12 16:31:37 -0700 | [diff] [blame] | 325 | int err = buildDevice(busPath, parameters, bus, address, constructor, |
Johnathan Mantey | 9b86787 | 2020-10-13 15:00:51 -0700 | [diff] [blame] | 326 | destructor, createsHWMon); |
James Feist | 053a664 | 2018-10-15 13:17:09 -0700 | [diff] [blame] | 327 | |
Johnathan Mantey | 9b86787 | 2020-10-13 15:00:51 -0700 | [diff] [blame] | 328 | if (!err && boost::ends_with(type, "Mux") && bus && address && channels) |
James Feist | 286babc | 2019-02-07 16:48:28 -0800 | [diff] [blame] | 329 | { |
James Feist | 9813279 | 2019-07-09 13:29:09 -0700 | [diff] [blame] | 330 | linkMux(name, static_cast<size_t>(*bus), static_cast<size_t>(*address), |
| 331 | *channels); |
James Feist | 286babc | 2019-02-07 16:48:28 -0800 | [diff] [blame] | 332 | } |
James Feist | 053a664 | 2018-10-15 13:17:09 -0700 | [diff] [blame] | 333 | } |
| 334 | |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 335 | bool loadOverlays(const nlohmann::json& systemConfiguration) |
James Feist | c95cb14 | 2018-02-26 10:41:42 -0800 | [diff] [blame] | 336 | { |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame] | 337 | std::filesystem::create_directory(outputDir); |
James Feist | c95cb14 | 2018-02-26 10:41:42 -0800 | [diff] [blame] | 338 | for (auto entity = systemConfiguration.begin(); |
| 339 | entity != systemConfiguration.end(); entity++) |
| 340 | { |
James Feist | 1e3e698 | 2018-08-03 16:09:28 -0700 | [diff] [blame] | 341 | auto findExposes = entity.value().find("Exposes"); |
James Feist | c95cb14 | 2018-02-26 10:41:42 -0800 | [diff] [blame] | 342 | if (findExposes == entity.value().end() || |
| 343 | findExposes->type() != nlohmann::json::value_t::array) |
| 344 | { |
| 345 | continue; |
| 346 | } |
| 347 | |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 348 | for (auto& configuration : *findExposes) |
James Feist | c95cb14 | 2018-02-26 10:41:42 -0800 | [diff] [blame] | 349 | { |
James Feist | 1e3e698 | 2018-08-03 16:09:28 -0700 | [diff] [blame] | 350 | auto findStatus = configuration.find("Status"); |
James Feist | c95cb14 | 2018-02-26 10:41:42 -0800 | [diff] [blame] | 351 | // status missing is assumed to be 'okay' |
| 352 | if (findStatus != configuration.end() && *findStatus == "disabled") |
| 353 | { |
| 354 | continue; |
| 355 | } |
James Feist | d63d18a | 2018-07-19 15:23:45 -0700 | [diff] [blame] | 356 | auto findType = configuration.find("Type"); |
James Feist | c95cb14 | 2018-02-26 10:41:42 -0800 | [diff] [blame] | 357 | if (findType == configuration.end() || |
| 358 | findType->type() != nlohmann::json::value_t::string) |
| 359 | { |
| 360 | continue; |
| 361 | } |
| 362 | std::string type = findType.value().get<std::string>(); |
James Feist | 053a664 | 2018-10-15 13:17:09 -0700 | [diff] [blame] | 363 | auto device = devices::exportTemplates.find(type.c_str()); |
| 364 | if (device != devices::exportTemplates.end()) |
| 365 | { |
James Feist | 286babc | 2019-02-07 16:48:28 -0800 | [diff] [blame] | 366 | exportDevice(type, device->second, configuration); |
Josh Lehan | 96b8a6e | 2019-10-09 14:39:49 -0700 | [diff] [blame] | 367 | continue; |
| 368 | } |
| 369 | |
| 370 | // Because many devices are intentionally not exportable, |
| 371 | // this error message is not printed in all situations. |
| 372 | // If wondering why your device not appearing, add your type to |
| 373 | // the exportTemplates array in the devices.hpp file. |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame] | 374 | if constexpr (debug) |
Josh Lehan | 96b8a6e | 2019-10-09 14:39:49 -0700 | [diff] [blame] | 375 | { |
| 376 | std::cerr << "Device type " << type |
| 377 | << " not found in export map whitelist\n"; |
James Feist | 053a664 | 2018-10-15 13:17:09 -0700 | [diff] [blame] | 378 | } |
James Feist | c95cb14 | 2018-02-26 10:41:42 -0800 | [diff] [blame] | 379 | } |
| 380 | } |
| 381 | |
| 382 | return true; |
Jae Hyun Yoo | 6d1d014 | 2018-07-25 10:07:43 -0700 | [diff] [blame] | 383 | } |