blob: cb6ed102cc878780a8e6e7827cc644a15234068e [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 Bishop1fb9f3f2020-08-28 08:15:13 -040016/// \file Overlay.cpp
James Feistc95cb142018-02-26 10:41:42 -080017
Patrick Venturea49dc332019-10-26 08:32:02 -070018#include "Overlay.hpp"
19
20#include "Utils.hpp"
21#include "devices.hpp"
22
James Feista465ccc2019-02-08 12:51:01 -080023#include <boost/algorithm/string/predicate.hpp>
24#include <boost/container/flat_map.hpp>
25#include <boost/container/flat_set.hpp>
26#include <boost/process/child.hpp>
James Feist8c505da2020-05-28 10:06:33 -070027#include <nlohmann/json.hpp>
28
James Feist637b3ef2019-04-15 16:35:30 -070029#include <filesystem>
James Feista465ccc2019-02-08 12:51:01 -080030#include <iomanip>
31#include <iostream>
James Feista465ccc2019-02-08 12:51:01 -080032#include <regex>
33#include <string>
James Feistc95cb142018-02-26 10:41:42 -080034
James Feista465ccc2019-02-08 12:51:01 -080035constexpr const char* OUTPUT_DIR = "/tmp/overlays";
James Feista465ccc2019-02-08 12:51:01 -080036constexpr const char* TEMPLATE_CHAR = "$";
37constexpr const char* HEX_FORMAT_STR = "0x";
James Feista465ccc2019-02-08 12:51:01 -080038constexpr const char* I2C_DEVS_DIR = "/sys/bus/i2c/devices";
39constexpr const char* MUX_SYMLINK_DIR = "/dev/i2c-mux";
James Feistc95cb142018-02-26 10:41:42 -080040
Josh Lehan96b8a6e2019-10-09 14:39:49 -070041constexpr const bool DEBUG = false;
42
James Feistc95cb142018-02-26 10:41:42 -080043std::regex ILLEGAL_NAME_REGEX("[^A-Za-z0-9_]");
44
James Feistc95cb142018-02-26 10:41:42 -080045// helper function to make json types into string
James Feista465ccc2019-02-08 12:51:01 -080046std::string jsonToString(const nlohmann::json& in)
James Feistc95cb142018-02-26 10:41:42 -080047{
48 if (in.type() == nlohmann::json::value_t::string)
49 {
50 return in.get<std::string>();
51 }
52 else if (in.type() == nlohmann::json::value_t::array)
53 {
54 // remove brackets and comma from array
55 std::string array = in.dump();
56 array = array.substr(1, array.size() - 2);
57 boost::replace_all(array, ",", " ");
58 return array;
59 }
60 return in.dump();
61}
62
Ed Tanousee3357a2019-02-26 12:44:14 -080063void linkMux(const std::string& muxName, size_t busIndex, size_t address,
James Feista465ccc2019-02-08 12:51:01 -080064 const nlohmann::json::array_t& channelNames)
James Feistc95cb142018-02-26 10:41:42 -080065{
James Feist286babc2019-02-07 16:48:28 -080066 std::error_code ec;
Ed Tanousee3357a2019-02-26 12:44:14 -080067 std::filesystem::path muxSymlinkDir(MUX_SYMLINK_DIR);
68 std::filesystem::create_directory(muxSymlinkDir, ec);
69 // ignore error codes here if the directory already exists
70 ec.clear();
71 std::filesystem::path linkDir = muxSymlinkDir / muxName;
72 std::filesystem::create_directory(linkDir, ec);
73
74 std::ostringstream hexAddress;
75 hexAddress << std::hex << std::setfill('0') << std::setw(4) << address;
James Feist286babc2019-02-07 16:48:28 -080076
77 std::filesystem::path devDir(I2C_DEVS_DIR);
Ed Tanousee3357a2019-02-26 12:44:14 -080078 devDir /= std::to_string(busIndex) + "-" + hexAddress.str();
James Feist286babc2019-02-07 16:48:28 -080079
Ed Tanousee3357a2019-02-26 12:44:14 -080080 for (std::size_t channelIndex = 0; channelIndex < channelNames.size();
81 channelIndex++)
James Feist286babc2019-02-07 16:48:28 -080082 {
Ed Tanousee3357a2019-02-26 12:44:14 -080083 const std::string* channelName =
84 channelNames[channelIndex].get_ptr<const std::string*>();
85 if (channelName == nullptr)
86 {
87 continue;
88 }
89 if (channelName->empty())
90 {
91 continue;
92 }
93
94 std::filesystem::path channelPath =
95 devDir / ("channel-" + std::to_string(channelIndex));
96 if (!is_symlink(channelPath))
97 {
98 std::cerr << channelPath << "for mux channel " << *channelName
99 << " doesn't exist!\n";
100 continue;
101 }
102 std::filesystem::path bus = std::filesystem::read_symlink(channelPath);
103
104 std::filesystem::path fp("/dev" / bus.filename());
105 std::filesystem::path link(linkDir / *channelName);
106
107 std::filesystem::create_symlink(fp, link, ec);
108 if (ec)
109 {
110 std::cerr << "Failure creating symlink for " << fp << " to " << link
111 << "\n";
112 }
James Feistc95cb142018-02-26 10:41:42 -0800113 }
114}
115
James Feista465ccc2019-02-08 12:51:01 -0800116void exportDevice(const std::string& type,
117 const devices::ExportTemplate& exportTemplate,
118 const nlohmann::json& configuration)
James Feist053a6642018-10-15 13:17:09 -0700119{
120
121 std::string parameters = exportTemplate.parameters;
122 std::string device = exportTemplate.device;
123 std::string name = "unknown";
James Feista465ccc2019-02-08 12:51:01 -0800124 const uint64_t* bus = nullptr;
125 const uint64_t* address = nullptr;
126 const nlohmann::json::array_t* channels = nullptr;
James Feist053a6642018-10-15 13:17:09 -0700127
128 for (auto keyPair = configuration.begin(); keyPair != configuration.end();
129 keyPair++)
130 {
131 std::string subsituteString;
132
133 if (keyPair.key() == "Name" &&
134 keyPair.value().type() == nlohmann::json::value_t::string)
135 {
136 subsituteString = std::regex_replace(
137 keyPair.value().get<std::string>(), ILLEGAL_NAME_REGEX, "_");
138 name = subsituteString;
139 }
140 else
141 {
142 subsituteString = jsonToString(keyPair.value());
143 }
144
145 if (keyPair.key() == "Bus")
146 {
James Feista465ccc2019-02-08 12:51:01 -0800147 bus = keyPair.value().get_ptr<const uint64_t*>();
James Feist053a6642018-10-15 13:17:09 -0700148 }
149 else if (keyPair.key() == "Address")
150 {
James Feista465ccc2019-02-08 12:51:01 -0800151 address = keyPair.value().get_ptr<const uint64_t*>();
James Feist053a6642018-10-15 13:17:09 -0700152 }
James Feist286babc2019-02-07 16:48:28 -0800153 else if (keyPair.key() == "ChannelNames")
154 {
155 channels =
James Feista465ccc2019-02-08 12:51:01 -0800156 keyPair.value().get_ptr<const nlohmann::json::array_t*>();
James Feist286babc2019-02-07 16:48:28 -0800157 }
James Feist053a6642018-10-15 13:17:09 -0700158 boost::replace_all(parameters, TEMPLATE_CHAR + keyPair.key(),
159 subsituteString);
160 boost::replace_all(device, TEMPLATE_CHAR + keyPair.key(),
161 subsituteString);
162 }
163
164 // if we found bus and address we can attempt to prevent errors
165 if (bus != nullptr && address != nullptr)
166 {
167 std::ostringstream hex;
168 hex << std::hex << *address;
James Feista465ccc2019-02-08 12:51:01 -0800169 const std::string& addressHex = hex.str();
James Feist053a6642018-10-15 13:17:09 -0700170 std::string busStr = std::to_string(*bus);
171
Ed Tanous072e25d2018-12-16 21:45:20 -0800172 std::filesystem::path devicePath(device);
James Feist58b9c262019-02-12 13:38:45 -0800173 std::filesystem::path parentPath = devicePath.parent_path();
174 if (std::filesystem::is_directory(parentPath))
James Feist053a6642018-10-15 13:17:09 -0700175 {
James Feist58b9c262019-02-12 13:38:45 -0800176 for (const auto& path :
177 std::filesystem::directory_iterator(parentPath))
James Feist053a6642018-10-15 13:17:09 -0700178 {
James Feist58b9c262019-02-12 13:38:45 -0800179 if (!std::filesystem::is_directory(path))
180 {
181 continue;
182 }
James Feist053a6642018-10-15 13:17:09 -0700183
James Feist58b9c262019-02-12 13:38:45 -0800184 const std::string& directoryName = path.path().filename();
185 if (boost::starts_with(directoryName, busStr) &&
186 boost::ends_with(directoryName, addressHex))
187 {
188 return; // already exported
189 }
James Feist053a6642018-10-15 13:17:09 -0700190 }
191 }
192 }
193
194 std::ofstream deviceFile(device);
195 if (!deviceFile.good())
196 {
197 std::cerr << "Error writing " << device << "\n";
198 return;
199 }
200 deviceFile << parameters;
201 deviceFile.close();
James Feist286babc2019-02-07 16:48:28 -0800202 if (boost::ends_with(type, "Mux") && bus && address && channels)
203 {
James Feist98132792019-07-09 13:29:09 -0700204 linkMux(name, static_cast<size_t>(*bus), static_cast<size_t>(*address),
205 *channels);
James Feist286babc2019-02-07 16:48:28 -0800206 }
James Feist053a6642018-10-15 13:17:09 -0700207}
208
James Feista465ccc2019-02-08 12:51:01 -0800209bool loadOverlays(const nlohmann::json& systemConfiguration)
James Feistc95cb142018-02-26 10:41:42 -0800210{
Ed Tanous072e25d2018-12-16 21:45:20 -0800211 std::filesystem::create_directory(OUTPUT_DIR);
James Feistc95cb142018-02-26 10:41:42 -0800212 for (auto entity = systemConfiguration.begin();
213 entity != systemConfiguration.end(); entity++)
214 {
James Feist1e3e6982018-08-03 16:09:28 -0700215 auto findExposes = entity.value().find("Exposes");
James Feistc95cb142018-02-26 10:41:42 -0800216 if (findExposes == entity.value().end() ||
217 findExposes->type() != nlohmann::json::value_t::array)
218 {
219 continue;
220 }
221
James Feista465ccc2019-02-08 12:51:01 -0800222 for (auto& configuration : *findExposes)
James Feistc95cb142018-02-26 10:41:42 -0800223 {
James Feist1e3e6982018-08-03 16:09:28 -0700224 auto findStatus = configuration.find("Status");
James Feistc95cb142018-02-26 10:41:42 -0800225 // status missing is assumed to be 'okay'
226 if (findStatus != configuration.end() && *findStatus == "disabled")
227 {
228 continue;
229 }
James Feistd63d18a2018-07-19 15:23:45 -0700230 auto findType = configuration.find("Type");
James Feistc95cb142018-02-26 10:41:42 -0800231 if (findType == configuration.end() ||
232 findType->type() != nlohmann::json::value_t::string)
233 {
234 continue;
235 }
236 std::string type = findType.value().get<std::string>();
James Feist053a6642018-10-15 13:17:09 -0700237 auto device = devices::exportTemplates.find(type.c_str());
238 if (device != devices::exportTemplates.end())
239 {
James Feist286babc2019-02-07 16:48:28 -0800240 exportDevice(type, device->second, configuration);
Josh Lehan96b8a6e2019-10-09 14:39:49 -0700241 continue;
242 }
243
244 // Because many devices are intentionally not exportable,
245 // this error message is not printed in all situations.
246 // If wondering why your device not appearing, add your type to
247 // the exportTemplates array in the devices.hpp file.
248 if constexpr (DEBUG)
249 {
250 std::cerr << "Device type " << type
251 << " not found in export map whitelist\n";
James Feist053a6642018-10-15 13:17:09 -0700252 }
James Feistc95cb142018-02-26 10:41:42 -0800253 }
254 }
255
256 return true;
Jae Hyun Yoo6d1d0142018-07-25 10:07:43 -0700257}