blob: a6255375c02dd6ce9487745b726f2dfa4e162482 [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 Feist637b3ef2019-04-15 16:35:30 -070026#include <filesystem>
James Feista465ccc2019-02-08 12:51:01 -080027#include <iomanip>
28#include <iostream>
29#include <nlohmann/json.hpp>
30#include <regex>
31#include <string>
James Feistc95cb142018-02-26 10:41:42 -080032
James Feista465ccc2019-02-08 12:51:01 -080033constexpr const char* OUTPUT_DIR = "/tmp/overlays";
James Feista465ccc2019-02-08 12:51:01 -080034constexpr const char* TEMPLATE_CHAR = "$";
35constexpr const char* HEX_FORMAT_STR = "0x";
James Feista465ccc2019-02-08 12:51:01 -080036constexpr const char* I2C_DEVS_DIR = "/sys/bus/i2c/devices";
37constexpr const char* MUX_SYMLINK_DIR = "/dev/i2c-mux";
James Feistc95cb142018-02-26 10:41:42 -080038
Josh Lehan96b8a6e2019-10-09 14:39:49 -070039constexpr const bool DEBUG = false;
40
James Feistc95cb142018-02-26 10:41:42 -080041std::regex ILLEGAL_NAME_REGEX("[^A-Za-z0-9_]");
42
James Feistc95cb142018-02-26 10:41:42 -080043// helper function to make json types into string
James Feista465ccc2019-02-08 12:51:01 -080044std::string jsonToString(const nlohmann::json& in)
James Feistc95cb142018-02-26 10:41:42 -080045{
46 if (in.type() == nlohmann::json::value_t::string)
47 {
48 return in.get<std::string>();
49 }
50 else if (in.type() == nlohmann::json::value_t::array)
51 {
52 // remove brackets and comma from array
53 std::string array = in.dump();
54 array = array.substr(1, array.size() - 2);
55 boost::replace_all(array, ",", " ");
56 return array;
57 }
58 return in.dump();
59}
60
Ed Tanousee3357a2019-02-26 12:44:14 -080061void linkMux(const std::string& muxName, size_t busIndex, size_t address,
James Feista465ccc2019-02-08 12:51:01 -080062 const nlohmann::json::array_t& channelNames)
James Feistc95cb142018-02-26 10:41:42 -080063{
James Feist286babc2019-02-07 16:48:28 -080064 std::error_code ec;
Ed Tanousee3357a2019-02-26 12:44:14 -080065 std::filesystem::path muxSymlinkDir(MUX_SYMLINK_DIR);
66 std::filesystem::create_directory(muxSymlinkDir, ec);
67 // ignore error codes here if the directory already exists
68 ec.clear();
69 std::filesystem::path linkDir = muxSymlinkDir / muxName;
70 std::filesystem::create_directory(linkDir, ec);
71
72 std::ostringstream hexAddress;
73 hexAddress << std::hex << std::setfill('0') << std::setw(4) << address;
James Feist286babc2019-02-07 16:48:28 -080074
75 std::filesystem::path devDir(I2C_DEVS_DIR);
Ed Tanousee3357a2019-02-26 12:44:14 -080076 devDir /= std::to_string(busIndex) + "-" + hexAddress.str();
James Feist286babc2019-02-07 16:48:28 -080077
Ed Tanousee3357a2019-02-26 12:44:14 -080078 for (std::size_t channelIndex = 0; channelIndex < channelNames.size();
79 channelIndex++)
James Feist286babc2019-02-07 16:48:28 -080080 {
Ed Tanousee3357a2019-02-26 12:44:14 -080081 const std::string* channelName =
82 channelNames[channelIndex].get_ptr<const std::string*>();
83 if (channelName == nullptr)
84 {
85 continue;
86 }
87 if (channelName->empty())
88 {
89 continue;
90 }
91
92 std::filesystem::path channelPath =
93 devDir / ("channel-" + std::to_string(channelIndex));
94 if (!is_symlink(channelPath))
95 {
96 std::cerr << channelPath << "for mux channel " << *channelName
97 << " doesn't exist!\n";
98 continue;
99 }
100 std::filesystem::path bus = std::filesystem::read_symlink(channelPath);
101
102 std::filesystem::path fp("/dev" / bus.filename());
103 std::filesystem::path link(linkDir / *channelName);
104
105 std::filesystem::create_symlink(fp, link, ec);
106 if (ec)
107 {
108 std::cerr << "Failure creating symlink for " << fp << " to " << link
109 << "\n";
110 }
James Feistc95cb142018-02-26 10:41:42 -0800111 }
112}
113
James Feista465ccc2019-02-08 12:51:01 -0800114void exportDevice(const std::string& type,
115 const devices::ExportTemplate& exportTemplate,
116 const nlohmann::json& configuration)
James Feist053a6642018-10-15 13:17:09 -0700117{
118
119 std::string parameters = exportTemplate.parameters;
120 std::string device = exportTemplate.device;
121 std::string name = "unknown";
James Feista465ccc2019-02-08 12:51:01 -0800122 const uint64_t* bus = nullptr;
123 const uint64_t* address = nullptr;
124 const nlohmann::json::array_t* channels = nullptr;
James Feist053a6642018-10-15 13:17:09 -0700125
126 for (auto keyPair = configuration.begin(); keyPair != configuration.end();
127 keyPair++)
128 {
129 std::string subsituteString;
130
131 if (keyPair.key() == "Name" &&
132 keyPair.value().type() == nlohmann::json::value_t::string)
133 {
134 subsituteString = std::regex_replace(
135 keyPair.value().get<std::string>(), ILLEGAL_NAME_REGEX, "_");
136 name = subsituteString;
137 }
138 else
139 {
140 subsituteString = jsonToString(keyPair.value());
141 }
142
143 if (keyPair.key() == "Bus")
144 {
James Feista465ccc2019-02-08 12:51:01 -0800145 bus = keyPair.value().get_ptr<const uint64_t*>();
James Feist053a6642018-10-15 13:17:09 -0700146 }
147 else if (keyPair.key() == "Address")
148 {
James Feista465ccc2019-02-08 12:51:01 -0800149 address = keyPair.value().get_ptr<const uint64_t*>();
James Feist053a6642018-10-15 13:17:09 -0700150 }
James Feist286babc2019-02-07 16:48:28 -0800151 else if (keyPair.key() == "ChannelNames")
152 {
153 channels =
James Feista465ccc2019-02-08 12:51:01 -0800154 keyPair.value().get_ptr<const nlohmann::json::array_t*>();
James Feist286babc2019-02-07 16:48:28 -0800155 }
James Feist053a6642018-10-15 13:17:09 -0700156 boost::replace_all(parameters, TEMPLATE_CHAR + keyPair.key(),
157 subsituteString);
158 boost::replace_all(device, TEMPLATE_CHAR + keyPair.key(),
159 subsituteString);
160 }
161
162 // if we found bus and address we can attempt to prevent errors
163 if (bus != nullptr && address != nullptr)
164 {
165 std::ostringstream hex;
166 hex << std::hex << *address;
James Feista465ccc2019-02-08 12:51:01 -0800167 const std::string& addressHex = hex.str();
James Feist053a6642018-10-15 13:17:09 -0700168 std::string busStr = std::to_string(*bus);
169
Ed Tanous072e25d2018-12-16 21:45:20 -0800170 std::filesystem::path devicePath(device);
James Feist58b9c262019-02-12 13:38:45 -0800171 std::filesystem::path parentPath = devicePath.parent_path();
172 if (std::filesystem::is_directory(parentPath))
James Feist053a6642018-10-15 13:17:09 -0700173 {
James Feist58b9c262019-02-12 13:38:45 -0800174 for (const auto& path :
175 std::filesystem::directory_iterator(parentPath))
James Feist053a6642018-10-15 13:17:09 -0700176 {
James Feist58b9c262019-02-12 13:38:45 -0800177 if (!std::filesystem::is_directory(path))
178 {
179 continue;
180 }
James Feist053a6642018-10-15 13:17:09 -0700181
James Feist58b9c262019-02-12 13:38:45 -0800182 const std::string& directoryName = path.path().filename();
183 if (boost::starts_with(directoryName, busStr) &&
184 boost::ends_with(directoryName, addressHex))
185 {
186 return; // already exported
187 }
James Feist053a6642018-10-15 13:17:09 -0700188 }
189 }
190 }
191
192 std::ofstream deviceFile(device);
193 if (!deviceFile.good())
194 {
195 std::cerr << "Error writing " << device << "\n";
196 return;
197 }
198 deviceFile << parameters;
199 deviceFile.close();
James Feist286babc2019-02-07 16:48:28 -0800200 if (boost::ends_with(type, "Mux") && bus && address && channels)
201 {
James Feist98132792019-07-09 13:29:09 -0700202 linkMux(name, static_cast<size_t>(*bus), static_cast<size_t>(*address),
203 *channels);
James Feist286babc2019-02-07 16:48:28 -0800204 }
James Feist053a6642018-10-15 13:17:09 -0700205}
206
James Feista465ccc2019-02-08 12:51:01 -0800207bool loadOverlays(const nlohmann::json& systemConfiguration)
James Feistc95cb142018-02-26 10:41:42 -0800208{
Ed Tanous072e25d2018-12-16 21:45:20 -0800209 std::filesystem::create_directory(OUTPUT_DIR);
James Feistc95cb142018-02-26 10:41:42 -0800210 for (auto entity = systemConfiguration.begin();
211 entity != systemConfiguration.end(); entity++)
212 {
James Feist1e3e6982018-08-03 16:09:28 -0700213 auto findExposes = entity.value().find("Exposes");
James Feistc95cb142018-02-26 10:41:42 -0800214 if (findExposes == entity.value().end() ||
215 findExposes->type() != nlohmann::json::value_t::array)
216 {
217 continue;
218 }
219
James Feista465ccc2019-02-08 12:51:01 -0800220 for (auto& configuration : *findExposes)
James Feistc95cb142018-02-26 10:41:42 -0800221 {
James Feist1e3e6982018-08-03 16:09:28 -0700222 auto findStatus = configuration.find("Status");
James Feistc95cb142018-02-26 10:41:42 -0800223 // status missing is assumed to be 'okay'
224 if (findStatus != configuration.end() && *findStatus == "disabled")
225 {
226 continue;
227 }
James Feistd63d18a2018-07-19 15:23:45 -0700228 auto findType = configuration.find("Type");
James Feistc95cb142018-02-26 10:41:42 -0800229 if (findType == configuration.end() ||
230 findType->type() != nlohmann::json::value_t::string)
231 {
232 continue;
233 }
234 std::string type = findType.value().get<std::string>();
James Feist053a6642018-10-15 13:17:09 -0700235 auto device = devices::exportTemplates.find(type.c_str());
236 if (device != devices::exportTemplates.end())
237 {
James Feist286babc2019-02-07 16:48:28 -0800238 exportDevice(type, device->second, configuration);
Josh Lehan96b8a6e2019-10-09 14:39:49 -0700239 continue;
240 }
241
242 // Because many devices are intentionally not exportable,
243 // this error message is not printed in all situations.
244 // If wondering why your device not appearing, add your type to
245 // the exportTemplates array in the devices.hpp file.
246 if constexpr (DEBUG)
247 {
248 std::cerr << "Device type " << type
249 << " not found in export map whitelist\n";
James Feist053a6642018-10-15 13:17:09 -0700250 }
James Feistc95cb142018-02-26 10:41:42 -0800251 }
252 }
253
254 return true;
Jae Hyun Yoo6d1d0142018-07-25 10:07:43 -0700255}