| Alexander Hansen | 4e1142d | 2025-07-25 17:07:27 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: Apache-2.0 | 
|  | 2 | // SPDX-FileCopyrightText: Copyright 2018 Intel Corporation | 
| James Feist | c95cb14 | 2018-02-26 10:41:42 -0800 | [diff] [blame] | 3 |  | 
| Brad Bishop | e45d8c7 | 2022-05-25 15:12:53 -0400 | [diff] [blame] | 4 | #include "overlay.hpp" | 
| Patrick Venture | a49dc33 | 2019-10-26 08:32:02 -0700 | [diff] [blame] | 5 |  | 
| George Liu | 5a61ec8 | 2025-08-25 11:16:44 +0800 | [diff] [blame^] | 6 | #include "../utils.hpp" | 
| Patrick Venture | a49dc33 | 2019-10-26 08:32:02 -0700 | [diff] [blame] | 7 | #include "devices.hpp" | 
| Christopher Meis | 59ef1e7 | 2025-04-16 08:53:25 +0200 | [diff] [blame] | 8 | #include "utils.hpp" | 
| Patrick Venture | a49dc33 | 2019-10-26 08:32:02 -0700 | [diff] [blame] | 9 |  | 
| Johnathan Mantey | 9b86787 | 2020-10-13 15:00:51 -0700 | [diff] [blame] | 10 | #include <boost/asio/io_context.hpp> | 
|  | 11 | #include <boost/asio/steady_timer.hpp> | 
| James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 12 | #include <boost/container/flat_map.hpp> | 
|  | 13 | #include <boost/container/flat_set.hpp> | 
| James Feist | 8c505da | 2020-05-28 10:06:33 -0700 | [diff] [blame] | 14 | #include <nlohmann/json.hpp> | 
| Alexander Hansen | c3db2c3 | 2024-08-20 15:01:38 +0200 | [diff] [blame] | 15 | #include <phosphor-logging/lg2.hpp> | 
| James Feist | 8c505da | 2020-05-28 10:06:33 -0700 | [diff] [blame] | 16 |  | 
| James Feist | 637b3ef | 2019-04-15 16:35:30 -0700 | [diff] [blame] | 17 | #include <filesystem> | 
| Christopher Meis | 59ef1e7 | 2025-04-16 08:53:25 +0200 | [diff] [blame] | 18 | #include <fstream> | 
| James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 19 | #include <iomanip> | 
| James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 20 | #include <regex> | 
|  | 21 | #include <string> | 
| James Feist | c95cb14 | 2018-02-26 10:41:42 -0800 | [diff] [blame] | 22 |  | 
| Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame] | 23 | constexpr const char* outputDir = "/tmp/overlays"; | 
|  | 24 | constexpr const char* templateChar = "$"; | 
|  | 25 | constexpr const char* i2CDevsDir = "/sys/bus/i2c/devices"; | 
|  | 26 | constexpr const char* muxSymlinkDir = "/dev/i2c-mux"; | 
| James Feist | c95cb14 | 2018-02-26 10:41:42 -0800 | [diff] [blame] | 27 |  | 
| Ed Tanous | fc17142 | 2024-04-04 17:18:16 -0700 | [diff] [blame] | 28 | const std::regex illegalNameRegex("[^A-Za-z0-9_]"); | 
| James Feist | c95cb14 | 2018-02-26 10:41:42 -0800 | [diff] [blame] | 29 |  | 
| James Feist | c95cb14 | 2018-02-26 10:41:42 -0800 | [diff] [blame] | 30 | // helper function to make json types into string | 
| James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 31 | std::string jsonToString(const nlohmann::json& in) | 
| James Feist | c95cb14 | 2018-02-26 10:41:42 -0800 | [diff] [blame] | 32 | { | 
|  | 33 | if (in.type() == nlohmann::json::value_t::string) | 
|  | 34 | { | 
|  | 35 | return in.get<std::string>(); | 
|  | 36 | } | 
| Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame] | 37 | if (in.type() == nlohmann::json::value_t::array) | 
| James Feist | c95cb14 | 2018-02-26 10:41:42 -0800 | [diff] [blame] | 38 | { | 
|  | 39 | // remove brackets and comma from array | 
|  | 40 | std::string array = in.dump(); | 
|  | 41 | array = array.substr(1, array.size() - 2); | 
| George Liu | 5a61ec8 | 2025-08-25 11:16:44 +0800 | [diff] [blame^] | 42 | std::ranges::replace(array, ',', ' '); | 
| James Feist | c95cb14 | 2018-02-26 10:41:42 -0800 | [diff] [blame] | 43 | return array; | 
|  | 44 | } | 
|  | 45 | return in.dump(); | 
|  | 46 | } | 
|  | 47 |  | 
| Zev Weiss | 9afef3a | 2022-07-13 16:38:23 -0700 | [diff] [blame] | 48 | static std::string deviceDirName(uint64_t bus, uint64_t address) | 
|  | 49 | { | 
|  | 50 | std::ostringstream name; | 
| Johnathan Mantey | 7b21ef2 | 2022-08-08 12:57:55 -0700 | [diff] [blame] | 51 | name << bus << "-" << std::hex << std::setw(4) << std::setfill('0') | 
|  | 52 | << address; | 
| Zev Weiss | 9afef3a | 2022-07-13 16:38:23 -0700 | [diff] [blame] | 53 | return name.str(); | 
|  | 54 | } | 
|  | 55 |  | 
| Jonathan Doman | d0eb129 | 2023-04-19 11:21:56 -0700 | [diff] [blame] | 56 | void linkMux(const std::string& muxName, uint64_t busIndex, uint64_t address, | 
| Jonathan Doman | 6af72c9 | 2023-04-19 11:55:39 -0700 | [diff] [blame] | 57 | const std::vector<std::string>& channelNames) | 
| James Feist | c95cb14 | 2018-02-26 10:41:42 -0800 | [diff] [blame] | 58 | { | 
| James Feist | 286babc | 2019-02-07 16:48:28 -0800 | [diff] [blame] | 59 | std::error_code ec; | 
| Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame] | 60 | std::filesystem::path muxSymlinkDirPath(muxSymlinkDir); | 
|  | 61 | std::filesystem::create_directory(muxSymlinkDirPath, ec); | 
| Ed Tanous | ee3357a | 2019-02-26 12:44:14 -0800 | [diff] [blame] | 62 | // ignore error codes here if the directory already exists | 
|  | 63 | ec.clear(); | 
| Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame] | 64 | std::filesystem::path linkDir = muxSymlinkDirPath / muxName; | 
| Ed Tanous | ee3357a | 2019-02-26 12:44:14 -0800 | [diff] [blame] | 65 | std::filesystem::create_directory(linkDir, ec); | 
|  | 66 |  | 
| Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame] | 67 | std::filesystem::path devDir(i2CDevsDir); | 
| Zev Weiss | 9afef3a | 2022-07-13 16:38:23 -0700 | [diff] [blame] | 68 | devDir /= deviceDirName(busIndex, address); | 
| James Feist | 286babc | 2019-02-07 16:48:28 -0800 | [diff] [blame] | 69 |  | 
| Ed Tanous | ee3357a | 2019-02-26 12:44:14 -0800 | [diff] [blame] | 70 | for (std::size_t channelIndex = 0; channelIndex < channelNames.size(); | 
|  | 71 | channelIndex++) | 
| James Feist | 286babc | 2019-02-07 16:48:28 -0800 | [diff] [blame] | 72 | { | 
| Jonathan Doman | 6af72c9 | 2023-04-19 11:55:39 -0700 | [diff] [blame] | 73 | const std::string& channelName = channelNames[channelIndex]; | 
|  | 74 | if (channelName.empty()) | 
| Ed Tanous | ee3357a | 2019-02-26 12:44:14 -0800 | [diff] [blame] | 75 | { | 
|  | 76 | continue; | 
|  | 77 | } | 
|  | 78 |  | 
|  | 79 | std::filesystem::path channelPath = | 
|  | 80 | devDir / ("channel-" + std::to_string(channelIndex)); | 
|  | 81 | if (!is_symlink(channelPath)) | 
|  | 82 | { | 
| Alexander Hansen | 8feb045 | 2025-09-15 14:29:20 +0200 | [diff] [blame] | 83 | lg2::error("{PATH} for mux channel {CHANNEL} doesn't exist!", | 
|  | 84 | "PATH", channelPath.string(), "CHANNEL", channelName); | 
| Ed Tanous | ee3357a | 2019-02-26 12:44:14 -0800 | [diff] [blame] | 85 | continue; | 
|  | 86 | } | 
|  | 87 | std::filesystem::path bus = std::filesystem::read_symlink(channelPath); | 
|  | 88 |  | 
|  | 89 | std::filesystem::path fp("/dev" / bus.filename()); | 
| Jonathan Doman | 6af72c9 | 2023-04-19 11:55:39 -0700 | [diff] [blame] | 90 | std::filesystem::path link(linkDir / channelName); | 
| Ed Tanous | ee3357a | 2019-02-26 12:44:14 -0800 | [diff] [blame] | 91 |  | 
|  | 92 | std::filesystem::create_symlink(fp, link, ec); | 
|  | 93 | if (ec) | 
|  | 94 | { | 
| Alexander Hansen | 8feb045 | 2025-09-15 14:29:20 +0200 | [diff] [blame] | 95 | lg2::error("Failure creating symlink for {PATH} to {LINK}", "PATH", | 
|  | 96 | fp.string(), "LINK", link.string()); | 
| Ed Tanous | ee3357a | 2019-02-26 12:44:14 -0800 | [diff] [blame] | 97 | } | 
| James Feist | c95cb14 | 2018-02-26 10:41:42 -0800 | [diff] [blame] | 98 | } | 
|  | 99 | } | 
|  | 100 |  | 
| Jonathan Doman | d0eb129 | 2023-04-19 11:21:56 -0700 | [diff] [blame] | 101 | static int deleteDevice(const std::string& busPath, uint64_t address, | 
| Johnathan Mantey | 9b86787 | 2020-10-13 15:00:51 -0700 | [diff] [blame] | 102 | const std::string& destructor) | 
|  | 103 | { | 
| Zev Weiss | c11b5da | 2022-07-12 16:31:37 -0700 | [diff] [blame] | 104 | std::filesystem::path deviceDestructor(busPath); | 
| Johnathan Mantey | 9b86787 | 2020-10-13 15:00:51 -0700 | [diff] [blame] | 105 | deviceDestructor /= destructor; | 
|  | 106 | std::ofstream deviceFile(deviceDestructor); | 
|  | 107 | if (!deviceFile.good()) | 
|  | 108 | { | 
| Alexander Hansen | 8feb045 | 2025-09-15 14:29:20 +0200 | [diff] [blame] | 109 | lg2::error("Error writing {PATH}", "PATH", deviceDestructor.string()); | 
| Johnathan Mantey | 9b86787 | 2020-10-13 15:00:51 -0700 | [diff] [blame] | 110 | return -1; | 
|  | 111 | } | 
| Jonathan Doman | d0eb129 | 2023-04-19 11:21:56 -0700 | [diff] [blame] | 112 | deviceFile << std::to_string(address); | 
| Johnathan Mantey | 9b86787 | 2020-10-13 15:00:51 -0700 | [diff] [blame] | 113 | deviceFile.close(); | 
|  | 114 | return 0; | 
|  | 115 | } | 
|  | 116 |  | 
| Zev Weiss | c11b5da | 2022-07-12 16:31:37 -0700 | [diff] [blame] | 117 | static int createDevice(const std::string& busPath, | 
| Johnathan Mantey | 9b86787 | 2020-10-13 15:00:51 -0700 | [diff] [blame] | 118 | const std::string& parameters, | 
|  | 119 | const std::string& constructor) | 
|  | 120 | { | 
| Zev Weiss | c11b5da | 2022-07-12 16:31:37 -0700 | [diff] [blame] | 121 | std::filesystem::path deviceConstructor(busPath); | 
| Johnathan Mantey | 9b86787 | 2020-10-13 15:00:51 -0700 | [diff] [blame] | 122 | deviceConstructor /= constructor; | 
|  | 123 | std::ofstream deviceFile(deviceConstructor); | 
|  | 124 | if (!deviceFile.good()) | 
|  | 125 | { | 
| Alexander Hansen | 8feb045 | 2025-09-15 14:29:20 +0200 | [diff] [blame] | 126 | lg2::error("Error writing {PATH}", "PATH", deviceConstructor.string()); | 
| Johnathan Mantey | 9b86787 | 2020-10-13 15:00:51 -0700 | [diff] [blame] | 127 | return -1; | 
|  | 128 | } | 
|  | 129 | deviceFile << parameters; | 
|  | 130 | deviceFile.close(); | 
|  | 131 |  | 
|  | 132 | return 0; | 
|  | 133 | } | 
|  | 134 |  | 
| Jonathan Doman | d0eb129 | 2023-04-19 11:21:56 -0700 | [diff] [blame] | 135 | static bool deviceIsCreated(const std::string& busPath, uint64_t bus, | 
|  | 136 | uint64_t address, | 
| Johnathan Mantey | 7b21ef2 | 2022-08-08 12:57:55 -0700 | [diff] [blame] | 137 | const devices::createsHWMon hasHWMonDir) | 
| Johnathan Mantey | 9b86787 | 2020-10-13 15:00:51 -0700 | [diff] [blame] | 138 | { | 
| Zev Weiss | 67003d6 | 2022-07-15 16:38:19 -0700 | [diff] [blame] | 139 | std::filesystem::path dirPath = busPath; | 
| Jonathan Doman | d0eb129 | 2023-04-19 11:21:56 -0700 | [diff] [blame] | 140 | dirPath /= deviceDirName(bus, address); | 
| Johnathan Mantey | 7b21ef2 | 2022-08-08 12:57:55 -0700 | [diff] [blame] | 141 | if (hasHWMonDir == devices::createsHWMon::hasHWMonDir) | 
| Zev Weiss | 67003d6 | 2022-07-15 16:38:19 -0700 | [diff] [blame] | 142 | { | 
|  | 143 | dirPath /= "hwmon"; | 
|  | 144 | } | 
| Johnathan Mantey | 9b86787 | 2020-10-13 15:00:51 -0700 | [diff] [blame] | 145 |  | 
| Ed Tanous | 37e142b | 2021-04-30 12:19:26 -0700 | [diff] [blame] | 146 | std::error_code ec; | 
| Zev Weiss | 67003d6 | 2022-07-15 16:38:19 -0700 | [diff] [blame] | 147 | // Ignore errors; anything but a clean 'true' is just fine as 'false' | 
|  | 148 | return std::filesystem::exists(dirPath, ec); | 
| Johnathan Mantey | 9b86787 | 2020-10-13 15:00:51 -0700 | [diff] [blame] | 149 | } | 
|  | 150 |  | 
| Patrick Williams | 5a80703 | 2025-03-03 11:20:39 -0500 | [diff] [blame] | 151 | static int buildDevice( | 
|  | 152 | const std::string& name, const std::string& busPath, | 
|  | 153 | const std::string& parameters, uint64_t bus, uint64_t address, | 
|  | 154 | const std::string& constructor, const std::string& destructor, | 
|  | 155 | const devices::createsHWMon hasHWMonDir, | 
| Alexander Hansen | a555acf | 2025-06-27 11:59:10 +0200 | [diff] [blame] | 156 | std::vector<std::string> channelNames, boost::asio::io_context& io, | 
|  | 157 | const size_t retries = 5) | 
| Johnathan Mantey | 9b86787 | 2020-10-13 15:00:51 -0700 | [diff] [blame] | 158 | { | 
| Ed Tanous | 3013fb4 | 2022-07-09 08:27:06 -0700 | [diff] [blame] | 159 | if (retries == 0U) | 
| Johnathan Mantey | 9b86787 | 2020-10-13 15:00:51 -0700 | [diff] [blame] | 160 | { | 
|  | 161 | return -1; | 
|  | 162 | } | 
|  | 163 |  | 
| Jonathan Doman | 6af72c9 | 2023-04-19 11:55:39 -0700 | [diff] [blame] | 164 | // If it's already instantiated, we don't need to create it again. | 
| Johnathan Mantey | 7b21ef2 | 2022-08-08 12:57:55 -0700 | [diff] [blame] | 165 | if (!deviceIsCreated(busPath, bus, address, hasHWMonDir)) | 
| Johnathan Mantey | 9b86787 | 2020-10-13 15:00:51 -0700 | [diff] [blame] | 166 | { | 
| Jonathan Doman | 6af72c9 | 2023-04-19 11:55:39 -0700 | [diff] [blame] | 167 | // Try to create the device | 
|  | 168 | createDevice(busPath, parameters, constructor); | 
| Zev Weiss | cbcd17f | 2022-07-15 15:39:21 -0700 | [diff] [blame] | 169 |  | 
| Jonathan Doman | 6af72c9 | 2023-04-19 11:55:39 -0700 | [diff] [blame] | 170 | // If it didn't work, delete it and try again in 500ms | 
|  | 171 | if (!deviceIsCreated(busPath, bus, address, hasHWMonDir)) | 
|  | 172 | { | 
|  | 173 | deleteDevice(busPath, address, destructor); | 
|  | 174 |  | 
|  | 175 | std::shared_ptr<boost::asio::steady_timer> createTimer = | 
|  | 176 | std::make_shared<boost::asio::steady_timer>(io); | 
|  | 177 | createTimer->expires_after(std::chrono::milliseconds(500)); | 
|  | 178 | createTimer->async_wait( | 
|  | 179 | [createTimer, name, busPath, parameters, bus, address, | 
|  | 180 | constructor, destructor, hasHWMonDir, | 
| Alexander Hansen | a555acf | 2025-06-27 11:59:10 +0200 | [diff] [blame] | 181 | channelNames(std::move(channelNames)), retries, | 
|  | 182 | &io](const boost::system::error_code& ec) mutable { | 
| Patrick Williams | b707743 | 2024-08-16 15:22:21 -0400 | [diff] [blame] | 183 | if (ec) | 
|  | 184 | { | 
| Alexander Hansen | 8feb045 | 2025-09-15 14:29:20 +0200 | [diff] [blame] | 185 | lg2::error("Timer error: {ERR}", "ERR", ec.message()); | 
| Patrick Williams | b707743 | 2024-08-16 15:22:21 -0400 | [diff] [blame] | 186 | return -2; | 
|  | 187 | } | 
|  | 188 | return buildDevice(name, busPath, parameters, bus, address, | 
|  | 189 | constructor, destructor, hasHWMonDir, | 
| Alexander Hansen | a555acf | 2025-06-27 11:59:10 +0200 | [diff] [blame] | 190 | std::move(channelNames), io, | 
|  | 191 | retries - 1); | 
| Patrick Williams | b707743 | 2024-08-16 15:22:21 -0400 | [diff] [blame] | 192 | }); | 
| Jonathan Doman | 6af72c9 | 2023-04-19 11:55:39 -0700 | [diff] [blame] | 193 | return -1; | 
|  | 194 | } | 
|  | 195 | } | 
|  | 196 |  | 
|  | 197 | // Link the mux channels if needed once the device is created. | 
|  | 198 | if (!channelNames.empty()) | 
|  | 199 | { | 
|  | 200 | linkMux(name, bus, address, channelNames); | 
| Johnathan Mantey | 9b86787 | 2020-10-13 15:00:51 -0700 | [diff] [blame] | 201 | } | 
| Zev Weiss | cbcd17f | 2022-07-15 15:39:21 -0700 | [diff] [blame] | 202 |  | 
| Johnathan Mantey | 9b86787 | 2020-10-13 15:00:51 -0700 | [diff] [blame] | 203 | return 0; | 
|  | 204 | } | 
|  | 205 |  | 
| James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 206 | void exportDevice(const std::string& type, | 
|  | 207 | const devices::ExportTemplate& exportTemplate, | 
| Alexander Hansen | a555acf | 2025-06-27 11:59:10 +0200 | [diff] [blame] | 208 | const nlohmann::json& configuration, | 
|  | 209 | boost::asio::io_context& io) | 
| James Feist | 053a664 | 2018-10-15 13:17:09 -0700 | [diff] [blame] | 210 | { | 
| James Feist | 053a664 | 2018-10-15 13:17:09 -0700 | [diff] [blame] | 211 | std::string parameters = exportTemplate.parameters; | 
| Zev Weiss | c11b5da | 2022-07-12 16:31:37 -0700 | [diff] [blame] | 212 | std::string busPath = exportTemplate.busPath; | 
| Johnathan Mantey | 9b86787 | 2020-10-13 15:00:51 -0700 | [diff] [blame] | 213 | std::string constructor = exportTemplate.add; | 
|  | 214 | std::string destructor = exportTemplate.remove; | 
| Johnathan Mantey | 7b21ef2 | 2022-08-08 12:57:55 -0700 | [diff] [blame] | 215 | devices::createsHWMon hasHWMonDir = exportTemplate.hasHWMonDir; | 
| James Feist | 053a664 | 2018-10-15 13:17:09 -0700 | [diff] [blame] | 216 | std::string name = "unknown"; | 
| Jonathan Doman | d0eb129 | 2023-04-19 11:21:56 -0700 | [diff] [blame] | 217 | std::optional<uint64_t> bus; | 
|  | 218 | std::optional<uint64_t> address; | 
| Jonathan Doman | 6af72c9 | 2023-04-19 11:55:39 -0700 | [diff] [blame] | 219 | std::vector<std::string> channels; | 
| James Feist | 053a664 | 2018-10-15 13:17:09 -0700 | [diff] [blame] | 220 |  | 
|  | 221 | for (auto keyPair = configuration.begin(); keyPair != configuration.end(); | 
|  | 222 | keyPair++) | 
|  | 223 | { | 
|  | 224 | std::string subsituteString; | 
|  | 225 |  | 
|  | 226 | if (keyPair.key() == "Name" && | 
|  | 227 | keyPair.value().type() == nlohmann::json::value_t::string) | 
|  | 228 | { | 
|  | 229 | subsituteString = std::regex_replace( | 
| Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame] | 230 | keyPair.value().get<std::string>(), illegalNameRegex, "_"); | 
| James Feist | 053a664 | 2018-10-15 13:17:09 -0700 | [diff] [blame] | 231 | name = subsituteString; | 
|  | 232 | } | 
|  | 233 | else | 
|  | 234 | { | 
|  | 235 | subsituteString = jsonToString(keyPair.value()); | 
|  | 236 | } | 
|  | 237 |  | 
|  | 238 | if (keyPair.key() == "Bus") | 
|  | 239 | { | 
| Jonathan Doman | d0eb129 | 2023-04-19 11:21:56 -0700 | [diff] [blame] | 240 | bus = keyPair.value().get<uint64_t>(); | 
| James Feist | 053a664 | 2018-10-15 13:17:09 -0700 | [diff] [blame] | 241 | } | 
|  | 242 | else if (keyPair.key() == "Address") | 
|  | 243 | { | 
| Jonathan Doman | d0eb129 | 2023-04-19 11:21:56 -0700 | [diff] [blame] | 244 | address = keyPair.value().get<uint64_t>(); | 
| James Feist | 053a664 | 2018-10-15 13:17:09 -0700 | [diff] [blame] | 245 | } | 
| Jonathan Doman | 6af72c9 | 2023-04-19 11:55:39 -0700 | [diff] [blame] | 246 | else if (keyPair.key() == "ChannelNames" && type.ends_with("Mux")) | 
| James Feist | 286babc | 2019-02-07 16:48:28 -0800 | [diff] [blame] | 247 | { | 
| Jonathan Doman | 6af72c9 | 2023-04-19 11:55:39 -0700 | [diff] [blame] | 248 | channels = keyPair.value().get<std::vector<std::string>>(); | 
| James Feist | 286babc | 2019-02-07 16:48:28 -0800 | [diff] [blame] | 249 | } | 
| George Liu | 5a61ec8 | 2025-08-25 11:16:44 +0800 | [diff] [blame^] | 250 | replaceAll(parameters, templateChar + keyPair.key(), subsituteString); | 
|  | 251 | replaceAll(busPath, templateChar + keyPair.key(), subsituteString); | 
| James Feist | 053a664 | 2018-10-15 13:17:09 -0700 | [diff] [blame] | 252 | } | 
|  | 253 |  | 
| Jonathan Doman | d0eb129 | 2023-04-19 11:21:56 -0700 | [diff] [blame] | 254 | if (!bus || !address) | 
|  | 255 | { | 
|  | 256 | createDevice(busPath, parameters, constructor); | 
|  | 257 | return; | 
|  | 258 | } | 
|  | 259 |  | 
| Jonathan Doman | 6af72c9 | 2023-04-19 11:55:39 -0700 | [diff] [blame] | 260 | buildDevice(name, busPath, parameters, *bus, *address, constructor, | 
| Alexander Hansen | a555acf | 2025-06-27 11:59:10 +0200 | [diff] [blame] | 261 | destructor, hasHWMonDir, std::move(channels), io); | 
| James Feist | 053a664 | 2018-10-15 13:17:09 -0700 | [diff] [blame] | 262 | } | 
|  | 263 |  | 
| Alexander Hansen | a555acf | 2025-06-27 11:59:10 +0200 | [diff] [blame] | 264 | bool loadOverlays(const nlohmann::json& systemConfiguration, | 
|  | 265 | boost::asio::io_context& io) | 
| James Feist | c95cb14 | 2018-02-26 10:41:42 -0800 | [diff] [blame] | 266 | { | 
| Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame] | 267 | std::filesystem::create_directory(outputDir); | 
| James Feist | c95cb14 | 2018-02-26 10:41:42 -0800 | [diff] [blame] | 268 | for (auto entity = systemConfiguration.begin(); | 
|  | 269 | entity != systemConfiguration.end(); entity++) | 
|  | 270 | { | 
| James Feist | 1e3e698 | 2018-08-03 16:09:28 -0700 | [diff] [blame] | 271 | auto findExposes = entity.value().find("Exposes"); | 
| James Feist | c95cb14 | 2018-02-26 10:41:42 -0800 | [diff] [blame] | 272 | if (findExposes == entity.value().end() || | 
|  | 273 | findExposes->type() != nlohmann::json::value_t::array) | 
|  | 274 | { | 
|  | 275 | continue; | 
|  | 276 | } | 
|  | 277 |  | 
| Ed Tanous | 3013fb4 | 2022-07-09 08:27:06 -0700 | [diff] [blame] | 278 | for (const auto& configuration : *findExposes) | 
| James Feist | c95cb14 | 2018-02-26 10:41:42 -0800 | [diff] [blame] | 279 | { | 
| James Feist | 1e3e698 | 2018-08-03 16:09:28 -0700 | [diff] [blame] | 280 | auto findStatus = configuration.find("Status"); | 
| James Feist | c95cb14 | 2018-02-26 10:41:42 -0800 | [diff] [blame] | 281 | // status missing is assumed to be 'okay' | 
|  | 282 | if (findStatus != configuration.end() && *findStatus == "disabled") | 
|  | 283 | { | 
|  | 284 | continue; | 
|  | 285 | } | 
| James Feist | d63d18a | 2018-07-19 15:23:45 -0700 | [diff] [blame] | 286 | auto findType = configuration.find("Type"); | 
| James Feist | c95cb14 | 2018-02-26 10:41:42 -0800 | [diff] [blame] | 287 | if (findType == configuration.end() || | 
|  | 288 | findType->type() != nlohmann::json::value_t::string) | 
|  | 289 | { | 
|  | 290 | continue; | 
|  | 291 | } | 
|  | 292 | std::string type = findType.value().get<std::string>(); | 
| James Feist | 053a664 | 2018-10-15 13:17:09 -0700 | [diff] [blame] | 293 | auto device = devices::exportTemplates.find(type.c_str()); | 
|  | 294 | if (device != devices::exportTemplates.end()) | 
|  | 295 | { | 
| Alexander Hansen | a555acf | 2025-06-27 11:59:10 +0200 | [diff] [blame] | 296 | exportDevice(type, device->second, configuration, io); | 
| Josh Lehan | 96b8a6e | 2019-10-09 14:39:49 -0700 | [diff] [blame] | 297 | continue; | 
|  | 298 | } | 
|  | 299 |  | 
|  | 300 | // Because many devices are intentionally not exportable, | 
|  | 301 | // this error message is not printed in all situations. | 
|  | 302 | // If wondering why your device not appearing, add your type to | 
|  | 303 | // the exportTemplates array in the devices.hpp file. | 
| Alexander Hansen | c3db2c3 | 2024-08-20 15:01:38 +0200 | [diff] [blame] | 304 | lg2::debug("Device type {TYPE} not found in export map allowlist", | 
|  | 305 | "TYPE", type); | 
| James Feist | c95cb14 | 2018-02-26 10:41:42 -0800 | [diff] [blame] | 306 | } | 
|  | 307 | } | 
|  | 308 |  | 
|  | 309 | return true; | 
| Jae Hyun Yoo | 6d1d014 | 2018-07-25 10:07:43 -0700 | [diff] [blame] | 310 | } |