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