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