blob: 6352cba97e2250f30b5da9dbf497dde6707533b2 [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
Patrick Venturea49dc332019-10-26 08:32:02 -070020#include "devices.hpp"
Ed Tanous3013fb42022-07-09 08:27:06 -070021#include "utils.hpp"
Patrick Venturea49dc332019-10-26 08:32:02 -070022
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;
Johnathan Mantey7b21ef22022-08-08 12:57:55 -070067 name << bus << "-" << std::hex << std::setw(4) << std::setfill('0')
68 << address;
Zev Weiss9afef3a2022-07-13 16:38:23 -070069 return name.str();
70}
71
Ed Tanousee3357a2019-02-26 12:44:14 -080072void linkMux(const std::string& muxName, size_t busIndex, size_t address,
James Feista465ccc2019-02-08 12:51:01 -080073 const nlohmann::json::array_t& channelNames)
James Feistc95cb142018-02-26 10:41:42 -080074{
James Feist286babc2019-02-07 16:48:28 -080075 std::error_code ec;
Ed Tanous07d467b2021-02-23 14:48:37 -080076 std::filesystem::path muxSymlinkDirPath(muxSymlinkDir);
77 std::filesystem::create_directory(muxSymlinkDirPath, ec);
Ed Tanousee3357a2019-02-26 12:44:14 -080078 // ignore error codes here if the directory already exists
79 ec.clear();
Ed Tanous07d467b2021-02-23 14:48:37 -080080 std::filesystem::path linkDir = muxSymlinkDirPath / muxName;
Ed Tanousee3357a2019-02-26 12:44:14 -080081 std::filesystem::create_directory(linkDir, ec);
82
Ed Tanous07d467b2021-02-23 14:48:37 -080083 std::filesystem::path devDir(i2CDevsDir);
Zev Weiss9afef3a2022-07-13 16:38:23 -070084 devDir /= deviceDirName(busIndex, address);
James Feist286babc2019-02-07 16:48:28 -080085
Ed Tanousee3357a2019-02-26 12:44:14 -080086 for (std::size_t channelIndex = 0; channelIndex < channelNames.size();
87 channelIndex++)
James Feist286babc2019-02-07 16:48:28 -080088 {
Ed Tanousee3357a2019-02-26 12:44:14 -080089 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 Weisse7d56e02022-07-15 12:58:30 -0700104 std::cerr << channelPath << " for mux channel " << *channelName
Ed Tanousee3357a2019-02-26 12:44:14 -0800105 << " 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 Feistc95cb142018-02-26 10:41:42 -0800119 }
120}
121
Zev Weissc11b5da2022-07-12 16:31:37 -0700122static int deleteDevice(const std::string& busPath,
Ed Tanous07d467b2021-02-23 14:48:37 -0800123 const std::shared_ptr<uint64_t>& address,
Johnathan Mantey9b867872020-10-13 15:00:51 -0700124 const std::string& destructor)
125{
126 if (!address)
127 {
128 return -1;
129 }
Zev Weissc11b5da2022-07-12 16:31:37 -0700130 std::filesystem::path deviceDestructor(busPath);
Johnathan Mantey9b867872020-10-13 15:00:51 -0700131 deviceDestructor /= destructor;
132 std::ofstream deviceFile(deviceDestructor);
133 if (!deviceFile.good())
134 {
135 std::cerr << "Error writing " << deviceDestructor << "\n";
136 return -1;
137 }
138 deviceFile << std::to_string(*address);
139 deviceFile.close();
140 return 0;
141}
142
Zev Weissc11b5da2022-07-12 16:31:37 -0700143static int createDevice(const std::string& busPath,
Johnathan Mantey9b867872020-10-13 15:00:51 -0700144 const std::string& parameters,
145 const std::string& constructor)
146{
Zev Weissc11b5da2022-07-12 16:31:37 -0700147 std::filesystem::path deviceConstructor(busPath);
Johnathan Mantey9b867872020-10-13 15:00:51 -0700148 deviceConstructor /= constructor;
149 std::ofstream deviceFile(deviceConstructor);
150 if (!deviceFile.good())
151 {
152 std::cerr << "Error writing " << deviceConstructor << "\n";
153 return -1;
154 }
155 deviceFile << parameters;
156 deviceFile.close();
157
158 return 0;
159}
160
Zev Weissc11b5da2022-07-12 16:31:37 -0700161static bool deviceIsCreated(const std::string& busPath,
Ed Tanous07d467b2021-02-23 14:48:37 -0800162 const std::shared_ptr<uint64_t>& bus,
163 const std::shared_ptr<uint64_t>& address,
Johnathan Mantey7b21ef22022-08-08 12:57:55 -0700164 const devices::createsHWMon hasHWMonDir)
Johnathan Mantey9b867872020-10-13 15:00:51 -0700165{
166 if (!bus || !address)
167 {
168 return false;
169 }
170
Zev Weiss67003d62022-07-15 16:38:19 -0700171 std::filesystem::path dirPath = busPath;
172 dirPath /= deviceDirName(*bus, *address);
Johnathan Mantey7b21ef22022-08-08 12:57:55 -0700173 if (hasHWMonDir == devices::createsHWMon::hasHWMonDir)
Zev Weiss67003d62022-07-15 16:38:19 -0700174 {
175 dirPath /= "hwmon";
176 }
Johnathan Mantey9b867872020-10-13 15:00:51 -0700177
Ed Tanous37e142b2021-04-30 12:19:26 -0700178 std::error_code ec;
Zev Weiss67003d62022-07-15 16:38:19 -0700179 // Ignore errors; anything but a clean 'true' is just fine as 'false'
180 return std::filesystem::exists(dirPath, ec);
Johnathan Mantey9b867872020-10-13 15:00:51 -0700181}
182
Zev Weissc11b5da2022-07-12 16:31:37 -0700183static int buildDevice(const std::string& busPath,
Johnathan Mantey9b867872020-10-13 15:00:51 -0700184 const std::string& parameters,
Ed Tanous07d467b2021-02-23 14:48:37 -0800185 const std::shared_ptr<uint64_t>& bus,
186 const std::shared_ptr<uint64_t>& address,
Johnathan Mantey9b867872020-10-13 15:00:51 -0700187 const std::string& constructor,
Johnathan Mantey7b21ef22022-08-08 12:57:55 -0700188 const std::string& destructor,
189 const devices::createsHWMon hasHWMonDir,
Johnathan Mantey9b867872020-10-13 15:00:51 -0700190 const size_t retries = 5)
191{
Ed Tanous3013fb42022-07-09 08:27:06 -0700192 if (retries == 0U)
Johnathan Mantey9b867872020-10-13 15:00:51 -0700193 {
194 return -1;
195 }
196
Zev Weisscbcd17f2022-07-15 15:39:21 -0700197 // If it's already instantiated, there's nothing we need to do.
Johnathan Mantey7b21ef22022-08-08 12:57:55 -0700198 if (deviceIsCreated(busPath, bus, address, hasHWMonDir))
Johnathan Mantey9b867872020-10-13 15:00:51 -0700199 {
Zev Weisscbcd17f2022-07-15 15:39:21 -0700200 return 0;
Johnathan Mantey9b867872020-10-13 15:00:51 -0700201 }
202
Zev Weisscbcd17f2022-07-15 15:39:21 -0700203 // Try to create the device
204 createDevice(busPath, parameters, constructor);
205
206 // If it didn't work, delete it and try again in 500ms
Johnathan Mantey7b21ef22022-08-08 12:57:55 -0700207 if (!deviceIsCreated(busPath, bus, address, hasHWMonDir))
Johnathan Mantey9b867872020-10-13 15:00:51 -0700208 {
Zev Weisscbcd17f2022-07-15 15:39:21 -0700209 deleteDevice(busPath, address, destructor);
210
Johnathan Mantey9b867872020-10-13 15:00:51 -0700211 std::shared_ptr<boost::asio::steady_timer> createTimer =
212 std::make_shared<boost::asio::steady_timer>(io);
213 createTimer->expires_after(std::chrono::milliseconds(500));
Johnathan Mantey7b21ef22022-08-08 12:57:55 -0700214 createTimer->async_wait([createTimer, busPath, parameters, bus, address,
215 constructor, destructor, hasHWMonDir,
Johnathan Mantey9b867872020-10-13 15:00:51 -0700216 retries](const boost::system::error_code& ec) {
217 if (ec)
218 {
219 std::cerr << "Timer error: " << ec << "\n";
220 return -2;
221 }
Johnathan Mantey7b21ef22022-08-08 12:57:55 -0700222 return buildDevice(busPath, parameters, bus, address, constructor,
223 destructor, hasHWMonDir, retries - 1);
Johnathan Mantey9b867872020-10-13 15:00:51 -0700224 });
225 }
Zev Weisscbcd17f2022-07-15 15:39:21 -0700226
Johnathan Mantey9b867872020-10-13 15:00:51 -0700227 return 0;
228}
229
James Feista465ccc2019-02-08 12:51:01 -0800230void exportDevice(const std::string& type,
231 const devices::ExportTemplate& exportTemplate,
232 const nlohmann::json& configuration)
James Feist053a6642018-10-15 13:17:09 -0700233{
234
235 std::string parameters = exportTemplate.parameters;
Zev Weissc11b5da2022-07-12 16:31:37 -0700236 std::string busPath = exportTemplate.busPath;
Johnathan Mantey9b867872020-10-13 15:00:51 -0700237 std::string constructor = exportTemplate.add;
238 std::string destructor = exportTemplate.remove;
Johnathan Mantey7b21ef22022-08-08 12:57:55 -0700239 devices::createsHWMon hasHWMonDir = exportTemplate.hasHWMonDir;
James Feist053a6642018-10-15 13:17:09 -0700240 std::string name = "unknown";
Johnathan Mantey9b867872020-10-13 15:00:51 -0700241 std::shared_ptr<uint64_t> bus = nullptr;
242 std::shared_ptr<uint64_t> address = nullptr;
James Feista465ccc2019-02-08 12:51:01 -0800243 const nlohmann::json::array_t* channels = nullptr;
James Feist053a6642018-10-15 13:17:09 -0700244
245 for (auto keyPair = configuration.begin(); keyPair != configuration.end();
246 keyPair++)
247 {
248 std::string subsituteString;
249
250 if (keyPair.key() == "Name" &&
251 keyPair.value().type() == nlohmann::json::value_t::string)
252 {
253 subsituteString = std::regex_replace(
Ed Tanous07d467b2021-02-23 14:48:37 -0800254 keyPair.value().get<std::string>(), illegalNameRegex, "_");
James Feist053a6642018-10-15 13:17:09 -0700255 name = subsituteString;
256 }
257 else
258 {
259 subsituteString = jsonToString(keyPair.value());
260 }
261
262 if (keyPair.key() == "Bus")
263 {
Johnathan Mantey9b867872020-10-13 15:00:51 -0700264 bus = std::make_shared<uint64_t>(
265 *keyPair.value().get_ptr<const uint64_t*>());
James Feist053a6642018-10-15 13:17:09 -0700266 }
267 else if (keyPair.key() == "Address")
268 {
Johnathan Mantey9b867872020-10-13 15:00:51 -0700269 address = std::make_shared<uint64_t>(
270 *keyPair.value().get_ptr<const uint64_t*>());
James Feist053a6642018-10-15 13:17:09 -0700271 }
James Feist286babc2019-02-07 16:48:28 -0800272 else if (keyPair.key() == "ChannelNames")
273 {
274 channels =
James Feista465ccc2019-02-08 12:51:01 -0800275 keyPair.value().get_ptr<const nlohmann::json::array_t*>();
James Feist286babc2019-02-07 16:48:28 -0800276 }
Ed Tanous07d467b2021-02-23 14:48:37 -0800277 boost::replace_all(parameters, templateChar + keyPair.key(),
James Feist053a6642018-10-15 13:17:09 -0700278 subsituteString);
Zev Weissc11b5da2022-07-12 16:31:37 -0700279 boost::replace_all(busPath, templateChar + keyPair.key(),
James Feist053a6642018-10-15 13:17:09 -0700280 subsituteString);
281 }
282
Zev Weissc11b5da2022-07-12 16:31:37 -0700283 int err = buildDevice(busPath, parameters, bus, address, constructor,
Johnathan Mantey7b21ef22022-08-08 12:57:55 -0700284 destructor, hasHWMonDir);
James Feist053a6642018-10-15 13:17:09 -0700285
Ed Tanous3013fb42022-07-09 08:27:06 -0700286 if ((err == 0) && boost::ends_with(type, "Mux") && bus && address &&
287 (channels != nullptr))
James Feist286babc2019-02-07 16:48:28 -0800288 {
James Feist98132792019-07-09 13:29:09 -0700289 linkMux(name, static_cast<size_t>(*bus), static_cast<size_t>(*address),
290 *channels);
James Feist286babc2019-02-07 16:48:28 -0800291 }
James Feist053a6642018-10-15 13:17:09 -0700292}
293
James Feista465ccc2019-02-08 12:51:01 -0800294bool loadOverlays(const nlohmann::json& systemConfiguration)
James Feistc95cb142018-02-26 10:41:42 -0800295{
Ed Tanous07d467b2021-02-23 14:48:37 -0800296 std::filesystem::create_directory(outputDir);
James Feistc95cb142018-02-26 10:41:42 -0800297 for (auto entity = systemConfiguration.begin();
298 entity != systemConfiguration.end(); entity++)
299 {
James Feist1e3e6982018-08-03 16:09:28 -0700300 auto findExposes = entity.value().find("Exposes");
James Feistc95cb142018-02-26 10:41:42 -0800301 if (findExposes == entity.value().end() ||
302 findExposes->type() != nlohmann::json::value_t::array)
303 {
304 continue;
305 }
306
Ed Tanous3013fb42022-07-09 08:27:06 -0700307 for (const auto& configuration : *findExposes)
James Feistc95cb142018-02-26 10:41:42 -0800308 {
James Feist1e3e6982018-08-03 16:09:28 -0700309 auto findStatus = configuration.find("Status");
James Feistc95cb142018-02-26 10:41:42 -0800310 // status missing is assumed to be 'okay'
311 if (findStatus != configuration.end() && *findStatus == "disabled")
312 {
313 continue;
314 }
James Feistd63d18a2018-07-19 15:23:45 -0700315 auto findType = configuration.find("Type");
James Feistc95cb142018-02-26 10:41:42 -0800316 if (findType == configuration.end() ||
317 findType->type() != nlohmann::json::value_t::string)
318 {
319 continue;
320 }
321 std::string type = findType.value().get<std::string>();
James Feist053a6642018-10-15 13:17:09 -0700322 auto device = devices::exportTemplates.find(type.c_str());
323 if (device != devices::exportTemplates.end())
324 {
James Feist286babc2019-02-07 16:48:28 -0800325 exportDevice(type, device->second, configuration);
Josh Lehan96b8a6e2019-10-09 14:39:49 -0700326 continue;
327 }
328
329 // Because many devices are intentionally not exportable,
330 // this error message is not printed in all situations.
331 // If wondering why your device not appearing, add your type to
332 // the exportTemplates array in the devices.hpp file.
Ed Tanous07d467b2021-02-23 14:48:37 -0800333 if constexpr (debug)
Josh Lehan96b8a6e2019-10-09 14:39:49 -0700334 {
335 std::cerr << "Device type " << type
336 << " not found in export map whitelist\n";
James Feist053a6642018-10-15 13:17:09 -0700337 }
James Feistc95cb142018-02-26 10:41:42 -0800338 }
339 }
340
341 return true;
Jae Hyun Yoo6d1d0142018-07-25 10:07:43 -0700342}