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 | */ |
| 16 | |
Patrick Venture | a49dc33 | 2019-10-26 08:32:02 -0700 | [diff] [blame] | 17 | #include "Overlay.hpp" |
| 18 | |
| 19 | #include "Utils.hpp" |
| 20 | #include "devices.hpp" |
| 21 | |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 22 | #include <boost/algorithm/string/predicate.hpp> |
| 23 | #include <boost/container/flat_map.hpp> |
| 24 | #include <boost/container/flat_set.hpp> |
| 25 | #include <boost/process/child.hpp> |
James Feist | 8c505da | 2020-05-28 10:06:33 -0700 | [diff] [blame^] | 26 | #include <nlohmann/json.hpp> |
| 27 | |
James Feist | 637b3ef | 2019-04-15 16:35:30 -0700 | [diff] [blame] | 28 | #include <filesystem> |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 29 | #include <iomanip> |
| 30 | #include <iostream> |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 31 | #include <regex> |
| 32 | #include <string> |
James Feist | c95cb14 | 2018-02-26 10:41:42 -0800 | [diff] [blame] | 33 | |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 34 | constexpr const char* OUTPUT_DIR = "/tmp/overlays"; |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 35 | constexpr const char* TEMPLATE_CHAR = "$"; |
| 36 | constexpr const char* HEX_FORMAT_STR = "0x"; |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 37 | constexpr const char* I2C_DEVS_DIR = "/sys/bus/i2c/devices"; |
| 38 | constexpr const char* MUX_SYMLINK_DIR = "/dev/i2c-mux"; |
James Feist | c95cb14 | 2018-02-26 10:41:42 -0800 | [diff] [blame] | 39 | |
Josh Lehan | 96b8a6e | 2019-10-09 14:39:49 -0700 | [diff] [blame] | 40 | constexpr const bool DEBUG = false; |
| 41 | |
James Feist | c95cb14 | 2018-02-26 10:41:42 -0800 | [diff] [blame] | 42 | std::regex ILLEGAL_NAME_REGEX("[^A-Za-z0-9_]"); |
| 43 | |
James Feist | c95cb14 | 2018-02-26 10:41:42 -0800 | [diff] [blame] | 44 | // helper function to make json types into string |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 45 | std::string jsonToString(const nlohmann::json& in) |
James Feist | c95cb14 | 2018-02-26 10:41:42 -0800 | [diff] [blame] | 46 | { |
| 47 | if (in.type() == nlohmann::json::value_t::string) |
| 48 | { |
| 49 | return in.get<std::string>(); |
| 50 | } |
| 51 | else if (in.type() == nlohmann::json::value_t::array) |
| 52 | { |
| 53 | // remove brackets and comma from array |
| 54 | std::string array = in.dump(); |
| 55 | array = array.substr(1, array.size() - 2); |
| 56 | boost::replace_all(array, ",", " "); |
| 57 | return array; |
| 58 | } |
| 59 | return in.dump(); |
| 60 | } |
| 61 | |
Ed Tanous | ee3357a | 2019-02-26 12:44:14 -0800 | [diff] [blame] | 62 | void linkMux(const std::string& muxName, size_t busIndex, size_t address, |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 63 | const nlohmann::json::array_t& channelNames) |
James Feist | c95cb14 | 2018-02-26 10:41:42 -0800 | [diff] [blame] | 64 | { |
James Feist | 286babc | 2019-02-07 16:48:28 -0800 | [diff] [blame] | 65 | std::error_code ec; |
Ed Tanous | ee3357a | 2019-02-26 12:44:14 -0800 | [diff] [blame] | 66 | std::filesystem::path muxSymlinkDir(MUX_SYMLINK_DIR); |
| 67 | std::filesystem::create_directory(muxSymlinkDir, ec); |
| 68 | // ignore error codes here if the directory already exists |
| 69 | ec.clear(); |
| 70 | std::filesystem::path linkDir = muxSymlinkDir / muxName; |
| 71 | std::filesystem::create_directory(linkDir, ec); |
| 72 | |
| 73 | std::ostringstream hexAddress; |
| 74 | hexAddress << std::hex << std::setfill('0') << std::setw(4) << address; |
James Feist | 286babc | 2019-02-07 16:48:28 -0800 | [diff] [blame] | 75 | |
| 76 | std::filesystem::path devDir(I2C_DEVS_DIR); |
Ed Tanous | ee3357a | 2019-02-26 12:44:14 -0800 | [diff] [blame] | 77 | devDir /= std::to_string(busIndex) + "-" + hexAddress.str(); |
James Feist | 286babc | 2019-02-07 16:48:28 -0800 | [diff] [blame] | 78 | |
Ed Tanous | ee3357a | 2019-02-26 12:44:14 -0800 | [diff] [blame] | 79 | for (std::size_t channelIndex = 0; channelIndex < channelNames.size(); |
| 80 | channelIndex++) |
James Feist | 286babc | 2019-02-07 16:48:28 -0800 | [diff] [blame] | 81 | { |
Ed Tanous | ee3357a | 2019-02-26 12:44:14 -0800 | [diff] [blame] | 82 | const std::string* channelName = |
| 83 | channelNames[channelIndex].get_ptr<const std::string*>(); |
| 84 | if (channelName == nullptr) |
| 85 | { |
| 86 | continue; |
| 87 | } |
| 88 | if (channelName->empty()) |
| 89 | { |
| 90 | continue; |
| 91 | } |
| 92 | |
| 93 | std::filesystem::path channelPath = |
| 94 | devDir / ("channel-" + std::to_string(channelIndex)); |
| 95 | if (!is_symlink(channelPath)) |
| 96 | { |
| 97 | std::cerr << channelPath << "for mux channel " << *channelName |
| 98 | << " doesn't exist!\n"; |
| 99 | continue; |
| 100 | } |
| 101 | std::filesystem::path bus = std::filesystem::read_symlink(channelPath); |
| 102 | |
| 103 | std::filesystem::path fp("/dev" / bus.filename()); |
| 104 | std::filesystem::path link(linkDir / *channelName); |
| 105 | |
| 106 | std::filesystem::create_symlink(fp, link, ec); |
| 107 | if (ec) |
| 108 | { |
| 109 | std::cerr << "Failure creating symlink for " << fp << " to " << link |
| 110 | << "\n"; |
| 111 | } |
James Feist | c95cb14 | 2018-02-26 10:41:42 -0800 | [diff] [blame] | 112 | } |
| 113 | } |
| 114 | |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 115 | void exportDevice(const std::string& type, |
| 116 | const devices::ExportTemplate& exportTemplate, |
| 117 | const nlohmann::json& configuration) |
James Feist | 053a664 | 2018-10-15 13:17:09 -0700 | [diff] [blame] | 118 | { |
| 119 | |
| 120 | std::string parameters = exportTemplate.parameters; |
| 121 | std::string device = exportTemplate.device; |
| 122 | std::string name = "unknown"; |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 123 | const uint64_t* bus = nullptr; |
| 124 | const uint64_t* address = nullptr; |
| 125 | const nlohmann::json::array_t* channels = nullptr; |
James Feist | 053a664 | 2018-10-15 13:17:09 -0700 | [diff] [blame] | 126 | |
| 127 | for (auto keyPair = configuration.begin(); keyPair != configuration.end(); |
| 128 | keyPair++) |
| 129 | { |
| 130 | std::string subsituteString; |
| 131 | |
| 132 | if (keyPair.key() == "Name" && |
| 133 | keyPair.value().type() == nlohmann::json::value_t::string) |
| 134 | { |
| 135 | subsituteString = std::regex_replace( |
| 136 | keyPair.value().get<std::string>(), ILLEGAL_NAME_REGEX, "_"); |
| 137 | name = subsituteString; |
| 138 | } |
| 139 | else |
| 140 | { |
| 141 | subsituteString = jsonToString(keyPair.value()); |
| 142 | } |
| 143 | |
| 144 | if (keyPair.key() == "Bus") |
| 145 | { |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 146 | bus = keyPair.value().get_ptr<const uint64_t*>(); |
James Feist | 053a664 | 2018-10-15 13:17:09 -0700 | [diff] [blame] | 147 | } |
| 148 | else if (keyPair.key() == "Address") |
| 149 | { |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 150 | address = keyPair.value().get_ptr<const uint64_t*>(); |
James Feist | 053a664 | 2018-10-15 13:17:09 -0700 | [diff] [blame] | 151 | } |
James Feist | 286babc | 2019-02-07 16:48:28 -0800 | [diff] [blame] | 152 | else if (keyPair.key() == "ChannelNames") |
| 153 | { |
| 154 | channels = |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 155 | keyPair.value().get_ptr<const nlohmann::json::array_t*>(); |
James Feist | 286babc | 2019-02-07 16:48:28 -0800 | [diff] [blame] | 156 | } |
James Feist | 053a664 | 2018-10-15 13:17:09 -0700 | [diff] [blame] | 157 | boost::replace_all(parameters, TEMPLATE_CHAR + keyPair.key(), |
| 158 | subsituteString); |
| 159 | boost::replace_all(device, TEMPLATE_CHAR + keyPair.key(), |
| 160 | subsituteString); |
| 161 | } |
| 162 | |
| 163 | // if we found bus and address we can attempt to prevent errors |
| 164 | if (bus != nullptr && address != nullptr) |
| 165 | { |
| 166 | std::ostringstream hex; |
| 167 | hex << std::hex << *address; |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 168 | const std::string& addressHex = hex.str(); |
James Feist | 053a664 | 2018-10-15 13:17:09 -0700 | [diff] [blame] | 169 | std::string busStr = std::to_string(*bus); |
| 170 | |
Ed Tanous | 072e25d | 2018-12-16 21:45:20 -0800 | [diff] [blame] | 171 | std::filesystem::path devicePath(device); |
James Feist | 58b9c26 | 2019-02-12 13:38:45 -0800 | [diff] [blame] | 172 | std::filesystem::path parentPath = devicePath.parent_path(); |
| 173 | if (std::filesystem::is_directory(parentPath)) |
James Feist | 053a664 | 2018-10-15 13:17:09 -0700 | [diff] [blame] | 174 | { |
James Feist | 58b9c26 | 2019-02-12 13:38:45 -0800 | [diff] [blame] | 175 | for (const auto& path : |
| 176 | std::filesystem::directory_iterator(parentPath)) |
James Feist | 053a664 | 2018-10-15 13:17:09 -0700 | [diff] [blame] | 177 | { |
James Feist | 58b9c26 | 2019-02-12 13:38:45 -0800 | [diff] [blame] | 178 | if (!std::filesystem::is_directory(path)) |
| 179 | { |
| 180 | continue; |
| 181 | } |
James Feist | 053a664 | 2018-10-15 13:17:09 -0700 | [diff] [blame] | 182 | |
James Feist | 58b9c26 | 2019-02-12 13:38:45 -0800 | [diff] [blame] | 183 | const std::string& directoryName = path.path().filename(); |
| 184 | if (boost::starts_with(directoryName, busStr) && |
| 185 | boost::ends_with(directoryName, addressHex)) |
| 186 | { |
| 187 | return; // already exported |
| 188 | } |
James Feist | 053a664 | 2018-10-15 13:17:09 -0700 | [diff] [blame] | 189 | } |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | std::ofstream deviceFile(device); |
| 194 | if (!deviceFile.good()) |
| 195 | { |
| 196 | std::cerr << "Error writing " << device << "\n"; |
| 197 | return; |
| 198 | } |
| 199 | deviceFile << parameters; |
| 200 | deviceFile.close(); |
James Feist | 286babc | 2019-02-07 16:48:28 -0800 | [diff] [blame] | 201 | if (boost::ends_with(type, "Mux") && bus && address && channels) |
| 202 | { |
James Feist | 9813279 | 2019-07-09 13:29:09 -0700 | [diff] [blame] | 203 | linkMux(name, static_cast<size_t>(*bus), static_cast<size_t>(*address), |
| 204 | *channels); |
James Feist | 286babc | 2019-02-07 16:48:28 -0800 | [diff] [blame] | 205 | } |
James Feist | 053a664 | 2018-10-15 13:17:09 -0700 | [diff] [blame] | 206 | } |
| 207 | |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 208 | bool loadOverlays(const nlohmann::json& systemConfiguration) |
James Feist | c95cb14 | 2018-02-26 10:41:42 -0800 | [diff] [blame] | 209 | { |
Ed Tanous | 072e25d | 2018-12-16 21:45:20 -0800 | [diff] [blame] | 210 | std::filesystem::create_directory(OUTPUT_DIR); |
James Feist | c95cb14 | 2018-02-26 10:41:42 -0800 | [diff] [blame] | 211 | for (auto entity = systemConfiguration.begin(); |
| 212 | entity != systemConfiguration.end(); entity++) |
| 213 | { |
James Feist | 1e3e698 | 2018-08-03 16:09:28 -0700 | [diff] [blame] | 214 | auto findExposes = entity.value().find("Exposes"); |
James Feist | c95cb14 | 2018-02-26 10:41:42 -0800 | [diff] [blame] | 215 | if (findExposes == entity.value().end() || |
| 216 | findExposes->type() != nlohmann::json::value_t::array) |
| 217 | { |
| 218 | continue; |
| 219 | } |
| 220 | |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 221 | for (auto& configuration : *findExposes) |
James Feist | c95cb14 | 2018-02-26 10:41:42 -0800 | [diff] [blame] | 222 | { |
James Feist | 1e3e698 | 2018-08-03 16:09:28 -0700 | [diff] [blame] | 223 | auto findStatus = configuration.find("Status"); |
James Feist | c95cb14 | 2018-02-26 10:41:42 -0800 | [diff] [blame] | 224 | // status missing is assumed to be 'okay' |
| 225 | if (findStatus != configuration.end() && *findStatus == "disabled") |
| 226 | { |
| 227 | continue; |
| 228 | } |
James Feist | d63d18a | 2018-07-19 15:23:45 -0700 | [diff] [blame] | 229 | auto findType = configuration.find("Type"); |
James Feist | c95cb14 | 2018-02-26 10:41:42 -0800 | [diff] [blame] | 230 | if (findType == configuration.end() || |
| 231 | findType->type() != nlohmann::json::value_t::string) |
| 232 | { |
| 233 | continue; |
| 234 | } |
| 235 | std::string type = findType.value().get<std::string>(); |
James Feist | 053a664 | 2018-10-15 13:17:09 -0700 | [diff] [blame] | 236 | auto device = devices::exportTemplates.find(type.c_str()); |
| 237 | if (device != devices::exportTemplates.end()) |
| 238 | { |
James Feist | 286babc | 2019-02-07 16:48:28 -0800 | [diff] [blame] | 239 | exportDevice(type, device->second, configuration); |
Josh Lehan | 96b8a6e | 2019-10-09 14:39:49 -0700 | [diff] [blame] | 240 | continue; |
| 241 | } |
| 242 | |
| 243 | // Because many devices are intentionally not exportable, |
| 244 | // this error message is not printed in all situations. |
| 245 | // If wondering why your device not appearing, add your type to |
| 246 | // the exportTemplates array in the devices.hpp file. |
| 247 | if constexpr (DEBUG) |
| 248 | { |
| 249 | std::cerr << "Device type " << type |
| 250 | << " not found in export map whitelist\n"; |
James Feist | 053a664 | 2018-10-15 13:17:09 -0700 | [diff] [blame] | 251 | } |
James Feist | c95cb14 | 2018-02-26 10:41:42 -0800 | [diff] [blame] | 252 | } |
| 253 | } |
| 254 | |
| 255 | return true; |
Jae Hyun Yoo | 6d1d014 | 2018-07-25 10:07:43 -0700 | [diff] [blame] | 256 | } |