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