blob: edd00f748720eb407f1e6d965839b02f8050754e [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*/
16
Patrick Venturea49dc332019-10-26 08:32:02 -070017#include "Overlay.hpp"
18
19#include "Utils.hpp"
20#include "devices.hpp"
21
James Feista465ccc2019-02-08 12:51:01 -080022#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 Feist8c505da2020-05-28 10:06:33 -070026#include <nlohmann/json.hpp>
27
James Feist637b3ef2019-04-15 16:35:30 -070028#include <filesystem>
James Feista465ccc2019-02-08 12:51:01 -080029#include <iomanip>
30#include <iostream>
James Feista465ccc2019-02-08 12:51:01 -080031#include <regex>
32#include <string>
James Feistc95cb142018-02-26 10:41:42 -080033
James Feista465ccc2019-02-08 12:51:01 -080034constexpr const char* OUTPUT_DIR = "/tmp/overlays";
James Feista465ccc2019-02-08 12:51:01 -080035constexpr const char* TEMPLATE_CHAR = "$";
36constexpr const char* HEX_FORMAT_STR = "0x";
James Feista465ccc2019-02-08 12:51:01 -080037constexpr const char* I2C_DEVS_DIR = "/sys/bus/i2c/devices";
38constexpr const char* MUX_SYMLINK_DIR = "/dev/i2c-mux";
James Feistc95cb142018-02-26 10:41:42 -080039
Josh Lehan96b8a6e2019-10-09 14:39:49 -070040constexpr const bool DEBUG = false;
41
James Feistc95cb142018-02-26 10:41:42 -080042std::regex ILLEGAL_NAME_REGEX("[^A-Za-z0-9_]");
43
James Feistc95cb142018-02-26 10:41:42 -080044// helper function to make json types into string
James Feista465ccc2019-02-08 12:51:01 -080045std::string jsonToString(const nlohmann::json& in)
James Feistc95cb142018-02-26 10:41:42 -080046{
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 Tanousee3357a2019-02-26 12:44:14 -080062void linkMux(const std::string& muxName, size_t busIndex, size_t address,
James Feista465ccc2019-02-08 12:51:01 -080063 const nlohmann::json::array_t& channelNames)
James Feistc95cb142018-02-26 10:41:42 -080064{
James Feist286babc2019-02-07 16:48:28 -080065 std::error_code ec;
Ed Tanousee3357a2019-02-26 12:44:14 -080066 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 Feist286babc2019-02-07 16:48:28 -080075
76 std::filesystem::path devDir(I2C_DEVS_DIR);
Ed Tanousee3357a2019-02-26 12:44:14 -080077 devDir /= std::to_string(busIndex) + "-" + hexAddress.str();
James Feist286babc2019-02-07 16:48:28 -080078
Ed Tanousee3357a2019-02-26 12:44:14 -080079 for (std::size_t channelIndex = 0; channelIndex < channelNames.size();
80 channelIndex++)
James Feist286babc2019-02-07 16:48:28 -080081 {
Ed Tanousee3357a2019-02-26 12:44:14 -080082 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 Feistc95cb142018-02-26 10:41:42 -0800112 }
113}
114
James Feista465ccc2019-02-08 12:51:01 -0800115void exportDevice(const std::string& type,
116 const devices::ExportTemplate& exportTemplate,
117 const nlohmann::json& configuration)
James Feist053a6642018-10-15 13:17:09 -0700118{
119
120 std::string parameters = exportTemplate.parameters;
121 std::string device = exportTemplate.device;
122 std::string name = "unknown";
James Feista465ccc2019-02-08 12:51:01 -0800123 const uint64_t* bus = nullptr;
124 const uint64_t* address = nullptr;
125 const nlohmann::json::array_t* channels = nullptr;
James Feist053a6642018-10-15 13:17:09 -0700126
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 Feista465ccc2019-02-08 12:51:01 -0800146 bus = keyPair.value().get_ptr<const uint64_t*>();
James Feist053a6642018-10-15 13:17:09 -0700147 }
148 else if (keyPair.key() == "Address")
149 {
James Feista465ccc2019-02-08 12:51:01 -0800150 address = keyPair.value().get_ptr<const uint64_t*>();
James Feist053a6642018-10-15 13:17:09 -0700151 }
James Feist286babc2019-02-07 16:48:28 -0800152 else if (keyPair.key() == "ChannelNames")
153 {
154 channels =
James Feista465ccc2019-02-08 12:51:01 -0800155 keyPair.value().get_ptr<const nlohmann::json::array_t*>();
James Feist286babc2019-02-07 16:48:28 -0800156 }
James Feist053a6642018-10-15 13:17:09 -0700157 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 Feista465ccc2019-02-08 12:51:01 -0800168 const std::string& addressHex = hex.str();
James Feist053a6642018-10-15 13:17:09 -0700169 std::string busStr = std::to_string(*bus);
170
Ed Tanous072e25d2018-12-16 21:45:20 -0800171 std::filesystem::path devicePath(device);
James Feist58b9c262019-02-12 13:38:45 -0800172 std::filesystem::path parentPath = devicePath.parent_path();
173 if (std::filesystem::is_directory(parentPath))
James Feist053a6642018-10-15 13:17:09 -0700174 {
James Feist58b9c262019-02-12 13:38:45 -0800175 for (const auto& path :
176 std::filesystem::directory_iterator(parentPath))
James Feist053a6642018-10-15 13:17:09 -0700177 {
James Feist58b9c262019-02-12 13:38:45 -0800178 if (!std::filesystem::is_directory(path))
179 {
180 continue;
181 }
James Feist053a6642018-10-15 13:17:09 -0700182
James Feist58b9c262019-02-12 13:38:45 -0800183 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 Feist053a6642018-10-15 13:17:09 -0700189 }
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 Feist286babc2019-02-07 16:48:28 -0800201 if (boost::ends_with(type, "Mux") && bus && address && channels)
202 {
James Feist98132792019-07-09 13:29:09 -0700203 linkMux(name, static_cast<size_t>(*bus), static_cast<size_t>(*address),
204 *channels);
James Feist286babc2019-02-07 16:48:28 -0800205 }
James Feist053a6642018-10-15 13:17:09 -0700206}
207
James Feista465ccc2019-02-08 12:51:01 -0800208bool loadOverlays(const nlohmann::json& systemConfiguration)
James Feistc95cb142018-02-26 10:41:42 -0800209{
Ed Tanous072e25d2018-12-16 21:45:20 -0800210 std::filesystem::create_directory(OUTPUT_DIR);
James Feistc95cb142018-02-26 10:41:42 -0800211 for (auto entity = systemConfiguration.begin();
212 entity != systemConfiguration.end(); entity++)
213 {
James Feist1e3e6982018-08-03 16:09:28 -0700214 auto findExposes = entity.value().find("Exposes");
James Feistc95cb142018-02-26 10:41:42 -0800215 if (findExposes == entity.value().end() ||
216 findExposes->type() != nlohmann::json::value_t::array)
217 {
218 continue;
219 }
220
James Feista465ccc2019-02-08 12:51:01 -0800221 for (auto& configuration : *findExposes)
James Feistc95cb142018-02-26 10:41:42 -0800222 {
James Feist1e3e6982018-08-03 16:09:28 -0700223 auto findStatus = configuration.find("Status");
James Feistc95cb142018-02-26 10:41:42 -0800224 // status missing is assumed to be 'okay'
225 if (findStatus != configuration.end() && *findStatus == "disabled")
226 {
227 continue;
228 }
James Feistd63d18a2018-07-19 15:23:45 -0700229 auto findType = configuration.find("Type");
James Feistc95cb142018-02-26 10:41:42 -0800230 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 Feist053a6642018-10-15 13:17:09 -0700236 auto device = devices::exportTemplates.find(type.c_str());
237 if (device != devices::exportTemplates.end())
238 {
James Feist286babc2019-02-07 16:48:28 -0800239 exportDevice(type, device->second, configuration);
Josh Lehan96b8a6e2019-10-09 14:39:49 -0700240 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 Feist053a6642018-10-15 13:17:09 -0700251 }
James Feistc95cb142018-02-26 10:41:42 -0800252 }
253 }
254
255 return true;
Jae Hyun Yoo6d1d0142018-07-25 10:07:43 -0700256}