blob: 4cb318135f7c140ae9ffffc46db7b06664940143 [file] [log] [blame]
James Feistc95cb142018-02-26 10:41:42 -08001/*
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 Bishope45d8c72022-05-25 15:12:53 -040016/// \file overlay.cpp
James Feistc95cb142018-02-26 10:41:42 -080017
Brad Bishope45d8c72022-05-25 15:12:53 -040018#include "overlay.hpp"
Patrick Venturea49dc332019-10-26 08:32:02 -070019
Brad Bishope45d8c72022-05-25 15:12:53 -040020#include "utils.hpp"
Patrick Venturea49dc332019-10-26 08:32:02 -070021#include "devices.hpp"
22
James Feista465ccc2019-02-08 12:51:01 -080023#include <boost/algorithm/string/predicate.hpp>
Johnathan Mantey9b867872020-10-13 15:00:51 -070024#include <boost/asio/io_context.hpp>
25#include <boost/asio/steady_timer.hpp>
James Feista465ccc2019-02-08 12:51:01 -080026#include <boost/container/flat_map.hpp>
27#include <boost/container/flat_set.hpp>
28#include <boost/process/child.hpp>
James Feist8c505da2020-05-28 10:06:33 -070029#include <nlohmann/json.hpp>
30
James Feist637b3ef2019-04-15 16:35:30 -070031#include <filesystem>
James Feista465ccc2019-02-08 12:51:01 -080032#include <iomanip>
33#include <iostream>
James Feista465ccc2019-02-08 12:51:01 -080034#include <regex>
35#include <string>
James Feistc95cb142018-02-26 10:41:42 -080036
Ed Tanous07d467b2021-02-23 14:48:37 -080037constexpr const char* outputDir = "/tmp/overlays";
38constexpr const char* templateChar = "$";
39constexpr const char* i2CDevsDir = "/sys/bus/i2c/devices";
40constexpr const char* muxSymlinkDir = "/dev/i2c-mux";
James Feistc95cb142018-02-26 10:41:42 -080041
Ed Tanous07d467b2021-02-23 14:48:37 -080042constexpr const bool debug = false;
Josh Lehan96b8a6e2019-10-09 14:39:49 -070043
Ed Tanous07d467b2021-02-23 14:48:37 -080044std::regex illegalNameRegex("[^A-Za-z0-9_]");
James Feistc95cb142018-02-26 10:41:42 -080045
James Feistc95cb142018-02-26 10:41:42 -080046// helper function to make json types into string
James Feista465ccc2019-02-08 12:51:01 -080047std::string jsonToString(const nlohmann::json& in)
James Feistc95cb142018-02-26 10:41:42 -080048{
49 if (in.type() == nlohmann::json::value_t::string)
50 {
51 return in.get<std::string>();
52 }
Ed Tanous07d467b2021-02-23 14:48:37 -080053 if (in.type() == nlohmann::json::value_t::array)
James Feistc95cb142018-02-26 10:41:42 -080054 {
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 Weiss9afef3a2022-07-13 16:38:23 -070064static std::string deviceDirName(uint64_t bus, uint64_t address)
65{
66 std::ostringstream name;
67 name << bus << "-" << std::hex << std::setw(4) << std::setfill('0') << address;
68 return name.str();
69}
70
Ed Tanousee3357a2019-02-26 12:44:14 -080071void linkMux(const std::string& muxName, size_t busIndex, size_t address,
James Feista465ccc2019-02-08 12:51:01 -080072 const nlohmann::json::array_t& channelNames)
James Feistc95cb142018-02-26 10:41:42 -080073{
James Feist286babc2019-02-07 16:48:28 -080074 std::error_code ec;
Ed Tanous07d467b2021-02-23 14:48:37 -080075 std::filesystem::path muxSymlinkDirPath(muxSymlinkDir);
76 std::filesystem::create_directory(muxSymlinkDirPath, ec);
Ed Tanousee3357a2019-02-26 12:44:14 -080077 // ignore error codes here if the directory already exists
78 ec.clear();
Ed Tanous07d467b2021-02-23 14:48:37 -080079 std::filesystem::path linkDir = muxSymlinkDirPath / muxName;
Ed Tanousee3357a2019-02-26 12:44:14 -080080 std::filesystem::create_directory(linkDir, ec);
81
Ed Tanous07d467b2021-02-23 14:48:37 -080082 std::filesystem::path devDir(i2CDevsDir);
Zev Weiss9afef3a2022-07-13 16:38:23 -070083 devDir /= deviceDirName(busIndex, address);
James Feist286babc2019-02-07 16:48:28 -080084
Ed Tanousee3357a2019-02-26 12:44:14 -080085 for (std::size_t channelIndex = 0; channelIndex < channelNames.size();
86 channelIndex++)
James Feist286babc2019-02-07 16:48:28 -080087 {
Ed Tanousee3357a2019-02-26 12:44:14 -080088 const std::string* channelName =
89 channelNames[channelIndex].get_ptr<const std::string*>();
90 if (channelName == nullptr)
91 {
92 continue;
93 }
94 if (channelName->empty())
95 {
96 continue;
97 }
98
99 std::filesystem::path channelPath =
100 devDir / ("channel-" + std::to_string(channelIndex));
101 if (!is_symlink(channelPath))
102 {
Zev Weisse7d56e02022-07-15 12:58:30 -0700103 std::cerr << channelPath << " for mux channel " << *channelName
Ed Tanousee3357a2019-02-26 12:44:14 -0800104 << " doesn't exist!\n";
105 continue;
106 }
107 std::filesystem::path bus = std::filesystem::read_symlink(channelPath);
108
109 std::filesystem::path fp("/dev" / bus.filename());
110 std::filesystem::path link(linkDir / *channelName);
111
112 std::filesystem::create_symlink(fp, link, ec);
113 if (ec)
114 {
115 std::cerr << "Failure creating symlink for " << fp << " to " << link
116 << "\n";
117 }
James Feistc95cb142018-02-26 10:41:42 -0800118 }
119}
120
Zev Weissc11b5da2022-07-12 16:31:37 -0700121static int deleteDevice(const std::string& busPath,
Ed Tanous07d467b2021-02-23 14:48:37 -0800122 const std::shared_ptr<uint64_t>& address,
Johnathan Mantey9b867872020-10-13 15:00:51 -0700123 const std::string& destructor)
124{
125 if (!address)
126 {
127 return -1;
128 }
Zev Weissc11b5da2022-07-12 16:31:37 -0700129 std::filesystem::path deviceDestructor(busPath);
Johnathan Mantey9b867872020-10-13 15:00:51 -0700130 deviceDestructor /= destructor;
131 std::ofstream deviceFile(deviceDestructor);
132 if (!deviceFile.good())
133 {
134 std::cerr << "Error writing " << deviceDestructor << "\n";
135 return -1;
136 }
137 deviceFile << std::to_string(*address);
138 deviceFile.close();
139 return 0;
140}
141
Zev Weissc11b5da2022-07-12 16:31:37 -0700142static int createDevice(const std::string& busPath,
Johnathan Mantey9b867872020-10-13 15:00:51 -0700143 const std::string& parameters,
144 const std::string& constructor)
145{
Zev Weissc11b5da2022-07-12 16:31:37 -0700146 std::filesystem::path deviceConstructor(busPath);
Johnathan Mantey9b867872020-10-13 15:00:51 -0700147 deviceConstructor /= constructor;
148 std::ofstream deviceFile(deviceConstructor);
149 if (!deviceFile.good())
150 {
151 std::cerr << "Error writing " << deviceConstructor << "\n";
152 return -1;
153 }
154 deviceFile << parameters;
155 deviceFile.close();
156
157 return 0;
158}
159
Zev Weissc11b5da2022-07-12 16:31:37 -0700160static bool deviceIsCreated(const std::string& busPath,
Ed Tanous07d467b2021-02-23 14:48:37 -0800161 const std::shared_ptr<uint64_t>& bus,
162 const std::shared_ptr<uint64_t>& address,
Zev Weisscbcd17f2022-07-15 15:39:21 -0700163 const bool createsHWMon)
Johnathan Mantey9b867872020-10-13 15:00:51 -0700164{
165 if (!bus || !address)
166 {
167 return false;
168 }
169
Zev Weiss9afef3a2022-07-13 16:38:23 -0700170 std::string dirName = deviceDirName(*bus, *address);
Johnathan Mantey9b867872020-10-13 15:00:51 -0700171
Ed Tanous37e142b2021-04-30 12:19:26 -0700172 std::error_code ec;
Zev Weissc11b5da2022-07-12 16:31:37 -0700173 auto path = std::filesystem::recursive_directory_iterator(busPath, ec);
Ed Tanous37e142b2021-04-30 12:19:26 -0700174 if (ec)
175 {
Zev Weissc11b5da2022-07-12 16:31:37 -0700176 std::cerr << "Unable to open path " << busPath << "\n";
Ed Tanous37e142b2021-04-30 12:19:26 -0700177 return false;
178 }
179 for (; path != std::filesystem::recursive_directory_iterator(); path++)
Johnathan Mantey9b867872020-10-13 15:00:51 -0700180 {
181 if (!std::filesystem::is_directory(*path))
182 {
183 continue;
184 }
185
Zev Weiss9afef3a2022-07-13 16:38:23 -0700186 const std::string foundName = path->path().filename();
187 if (foundName == dirName)
Johnathan Mantey9b867872020-10-13 15:00:51 -0700188 {
Zev Weisscbcd17f2022-07-15 15:39:21 -0700189 // Look for a .../hwmon subdirectory for devices that are expected
190 // to have one.
191 if (createsHWMon)
Johnathan Mantey9b867872020-10-13 15:00:51 -0700192 {
193 std::error_code ec;
Zev Weissc11b5da2022-07-12 16:31:37 -0700194 std::filesystem::path hwmonDir(busPath);
Zev Weiss9afef3a2022-07-13 16:38:23 -0700195 hwmonDir /= dirName;
Johnathan Mantey9b867872020-10-13 15:00:51 -0700196 hwmonDir /= "hwmon";
197 return std::filesystem::is_directory(hwmonDir, ec);
198 }
199 return true;
200 }
Ed Tanous07d467b2021-02-23 14:48:37 -0800201 path.disable_recursion_pending();
Johnathan Mantey9b867872020-10-13 15:00:51 -0700202 }
203 return false;
204}
205
Zev Weissc11b5da2022-07-12 16:31:37 -0700206static int buildDevice(const std::string& busPath,
Johnathan Mantey9b867872020-10-13 15:00:51 -0700207 const std::string& parameters,
Ed Tanous07d467b2021-02-23 14:48:37 -0800208 const std::shared_ptr<uint64_t>& bus,
209 const std::shared_ptr<uint64_t>& address,
Johnathan Mantey9b867872020-10-13 15:00:51 -0700210 const std::string& constructor,
211 const std::string& destructor, const bool createsHWMon,
212 const size_t retries = 5)
213{
Johnathan Mantey9b867872020-10-13 15:00:51 -0700214 if (!retries)
215 {
216 return -1;
217 }
218
Zev Weisscbcd17f2022-07-15 15:39:21 -0700219 // If it's already instantiated, there's nothing we need to do.
220 if (deviceIsCreated(busPath, bus, address, createsHWMon))
Johnathan Mantey9b867872020-10-13 15:00:51 -0700221 {
Zev Weisscbcd17f2022-07-15 15:39:21 -0700222 return 0;
Johnathan Mantey9b867872020-10-13 15:00:51 -0700223 }
224
Zev Weisscbcd17f2022-07-15 15:39:21 -0700225 // Try to create the device
226 createDevice(busPath, parameters, constructor);
227
228 // If it didn't work, delete it and try again in 500ms
229 if (!deviceIsCreated(busPath, bus, address, createsHWMon))
Johnathan Mantey9b867872020-10-13 15:00:51 -0700230 {
Zev Weisscbcd17f2022-07-15 15:39:21 -0700231 deleteDevice(busPath, address, destructor);
232
Johnathan Mantey9b867872020-10-13 15:00:51 -0700233 std::shared_ptr<boost::asio::steady_timer> createTimer =
234 std::make_shared<boost::asio::steady_timer>(io);
235 createTimer->expires_after(std::chrono::milliseconds(500));
Zev Weissc11b5da2022-07-12 16:31:37 -0700236 createTimer->async_wait([createTimer, busPath, parameters, bus,
Johnathan Mantey9b867872020-10-13 15:00:51 -0700237 address, constructor, destructor, createsHWMon,
238 retries](const boost::system::error_code& ec) {
239 if (ec)
240 {
241 std::cerr << "Timer error: " << ec << "\n";
242 return -2;
243 }
Zev Weissc11b5da2022-07-12 16:31:37 -0700244 return buildDevice(busPath, parameters, bus, address,
Johnathan Mantey9b867872020-10-13 15:00:51 -0700245 constructor, destructor, createsHWMon,
246 retries - 1);
247 });
248 }
Zev Weisscbcd17f2022-07-15 15:39:21 -0700249
Johnathan Mantey9b867872020-10-13 15:00:51 -0700250 return 0;
251}
252
James Feista465ccc2019-02-08 12:51:01 -0800253void exportDevice(const std::string& type,
254 const devices::ExportTemplate& exportTemplate,
255 const nlohmann::json& configuration)
James Feist053a6642018-10-15 13:17:09 -0700256{
257
258 std::string parameters = exportTemplate.parameters;
Zev Weissc11b5da2022-07-12 16:31:37 -0700259 std::string busPath = exportTemplate.busPath;
Johnathan Mantey9b867872020-10-13 15:00:51 -0700260 std::string constructor = exportTemplate.add;
261 std::string destructor = exportTemplate.remove;
262 bool createsHWMon = exportTemplate.createsHWMon;
James Feist053a6642018-10-15 13:17:09 -0700263 std::string name = "unknown";
Johnathan Mantey9b867872020-10-13 15:00:51 -0700264 std::shared_ptr<uint64_t> bus = nullptr;
265 std::shared_ptr<uint64_t> address = nullptr;
James Feista465ccc2019-02-08 12:51:01 -0800266 const nlohmann::json::array_t* channels = nullptr;
James Feist053a6642018-10-15 13:17:09 -0700267
268 for (auto keyPair = configuration.begin(); keyPair != configuration.end();
269 keyPair++)
270 {
271 std::string subsituteString;
272
273 if (keyPair.key() == "Name" &&
274 keyPair.value().type() == nlohmann::json::value_t::string)
275 {
276 subsituteString = std::regex_replace(
Ed Tanous07d467b2021-02-23 14:48:37 -0800277 keyPair.value().get<std::string>(), illegalNameRegex, "_");
James Feist053a6642018-10-15 13:17:09 -0700278 name = subsituteString;
279 }
280 else
281 {
282 subsituteString = jsonToString(keyPair.value());
283 }
284
285 if (keyPair.key() == "Bus")
286 {
Johnathan Mantey9b867872020-10-13 15:00:51 -0700287 bus = std::make_shared<uint64_t>(
288 *keyPair.value().get_ptr<const uint64_t*>());
James Feist053a6642018-10-15 13:17:09 -0700289 }
290 else if (keyPair.key() == "Address")
291 {
Johnathan Mantey9b867872020-10-13 15:00:51 -0700292 address = std::make_shared<uint64_t>(
293 *keyPair.value().get_ptr<const uint64_t*>());
James Feist053a6642018-10-15 13:17:09 -0700294 }
James Feist286babc2019-02-07 16:48:28 -0800295 else if (keyPair.key() == "ChannelNames")
296 {
297 channels =
James Feista465ccc2019-02-08 12:51:01 -0800298 keyPair.value().get_ptr<const nlohmann::json::array_t*>();
James Feist286babc2019-02-07 16:48:28 -0800299 }
Ed Tanous07d467b2021-02-23 14:48:37 -0800300 boost::replace_all(parameters, templateChar + keyPair.key(),
James Feist053a6642018-10-15 13:17:09 -0700301 subsituteString);
Zev Weissc11b5da2022-07-12 16:31:37 -0700302 boost::replace_all(busPath, templateChar + keyPair.key(),
James Feist053a6642018-10-15 13:17:09 -0700303 subsituteString);
304 }
305
Zev Weissc11b5da2022-07-12 16:31:37 -0700306 int err = buildDevice(busPath, parameters, bus, address, constructor,
Johnathan Mantey9b867872020-10-13 15:00:51 -0700307 destructor, createsHWMon);
James Feist053a6642018-10-15 13:17:09 -0700308
Johnathan Mantey9b867872020-10-13 15:00:51 -0700309 if (!err && boost::ends_with(type, "Mux") && bus && address && channels)
James Feist286babc2019-02-07 16:48:28 -0800310 {
James Feist98132792019-07-09 13:29:09 -0700311 linkMux(name, static_cast<size_t>(*bus), static_cast<size_t>(*address),
312 *channels);
James Feist286babc2019-02-07 16:48:28 -0800313 }
James Feist053a6642018-10-15 13:17:09 -0700314}
315
James Feista465ccc2019-02-08 12:51:01 -0800316bool loadOverlays(const nlohmann::json& systemConfiguration)
James Feistc95cb142018-02-26 10:41:42 -0800317{
Ed Tanous07d467b2021-02-23 14:48:37 -0800318 std::filesystem::create_directory(outputDir);
James Feistc95cb142018-02-26 10:41:42 -0800319 for (auto entity = systemConfiguration.begin();
320 entity != systemConfiguration.end(); entity++)
321 {
James Feist1e3e6982018-08-03 16:09:28 -0700322 auto findExposes = entity.value().find("Exposes");
James Feistc95cb142018-02-26 10:41:42 -0800323 if (findExposes == entity.value().end() ||
324 findExposes->type() != nlohmann::json::value_t::array)
325 {
326 continue;
327 }
328
James Feista465ccc2019-02-08 12:51:01 -0800329 for (auto& configuration : *findExposes)
James Feistc95cb142018-02-26 10:41:42 -0800330 {
James Feist1e3e6982018-08-03 16:09:28 -0700331 auto findStatus = configuration.find("Status");
James Feistc95cb142018-02-26 10:41:42 -0800332 // status missing is assumed to be 'okay'
333 if (findStatus != configuration.end() && *findStatus == "disabled")
334 {
335 continue;
336 }
James Feistd63d18a2018-07-19 15:23:45 -0700337 auto findType = configuration.find("Type");
James Feistc95cb142018-02-26 10:41:42 -0800338 if (findType == configuration.end() ||
339 findType->type() != nlohmann::json::value_t::string)
340 {
341 continue;
342 }
343 std::string type = findType.value().get<std::string>();
James Feist053a6642018-10-15 13:17:09 -0700344 auto device = devices::exportTemplates.find(type.c_str());
345 if (device != devices::exportTemplates.end())
346 {
James Feist286babc2019-02-07 16:48:28 -0800347 exportDevice(type, device->second, configuration);
Josh Lehan96b8a6e2019-10-09 14:39:49 -0700348 continue;
349 }
350
351 // Because many devices are intentionally not exportable,
352 // this error message is not printed in all situations.
353 // If wondering why your device not appearing, add your type to
354 // the exportTemplates array in the devices.hpp file.
Ed Tanous07d467b2021-02-23 14:48:37 -0800355 if constexpr (debug)
Josh Lehan96b8a6e2019-10-09 14:39:49 -0700356 {
357 std::cerr << "Device type " << type
358 << " not found in export map whitelist\n";
James Feist053a6642018-10-15 13:17:09 -0700359 }
James Feistc95cb142018-02-26 10:41:42 -0800360 }
361 }
362
363 return true;
Jae Hyun Yoo6d1d0142018-07-25 10:07:43 -0700364}