-
Notifications
You must be signed in to change notification settings - Fork 52
/
Copy pathutils.cpp
417 lines (369 loc) · 12.7 KB
/
utils.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
/*
// Copyright (c) 2017 Intel Corporation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
*/
/// \file utils.cpp
#include "utils.hpp"
#include "expression.hpp"
#include "variant_visitors.hpp"
#include <boost/algorithm/string/classification.hpp>
#include <boost/algorithm/string/find.hpp>
#include <boost/algorithm/string/predicate.hpp>
#include <boost/algorithm/string/replace.hpp>
#include <boost/algorithm/string/split.hpp>
#include <boost/container/flat_map.hpp>
#include <boost/lexical_cast.hpp>
#include <sdbusplus/bus/match.hpp>
#include <valijson/adapters/nlohmann_json_adapter.hpp>
#include <valijson/schema.hpp>
#include <valijson/schema_parser.hpp>
#include <valijson/validator.hpp>
#include <charconv>
#include <filesystem>
#include <fstream>
#include <map>
#include <regex>
constexpr const char* templateChar = "$";
namespace fs = std::filesystem;
// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
static bool powerStatusOn = false;
// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
static std::unique_ptr<sdbusplus::bus::match_t> powerMatch = nullptr;
bool findFiles(const fs::path& dirPath, const std::string& matchString,
std::vector<fs::path>& foundPaths)
{
if (!fs::exists(dirPath))
{
return false;
}
std::regex search(matchString);
std::smatch match;
for (const auto& p : fs::directory_iterator(dirPath))
{
std::string path = p.path().string();
if (std::regex_search(path, match, search))
{
foundPaths.emplace_back(p.path());
}
}
return true;
}
bool findFiles(const std::vector<fs::path>&& dirPaths,
const std::string& matchString,
std::vector<fs::path>& foundPaths)
{
std::map<fs::path, fs::path> paths;
std::regex search(matchString);
std::smatch match;
for (const auto& dirPath : dirPaths)
{
if (!fs::exists(dirPath))
{
continue;
}
for (const auto& p : fs::directory_iterator(dirPath))
{
std::string path = p.path().string();
if (std::regex_search(path, match, search))
{
paths[p.path().filename()] = p.path();
}
}
}
for (const auto& [key, value] : paths)
{
foundPaths.emplace_back(value);
}
return !foundPaths.empty();
}
bool getI2cDevicePaths(const fs::path& dirPath,
boost::container::flat_map<size_t, fs::path>& busPaths)
{
if (!fs::exists(dirPath))
{
return false;
}
// Regex for matching the path
std::regex searchPath(std::string(R"(i2c-\d+$)"));
// Regex for matching the bus numbers
std::regex searchBus(std::string(R"(\w[^-]*$)"));
std::smatch matchPath;
std::smatch matchBus;
for (const auto& p : fs::directory_iterator(dirPath))
{
std::string path = p.path().string();
if (std::regex_search(path, matchPath, searchPath))
{
if (std::regex_search(path, matchBus, searchBus))
{
size_t bus = stoul(*matchBus.begin());
busPaths.insert(std::pair<size_t, fs::path>(bus, p.path()));
}
}
}
return true;
}
bool validateJson(const nlohmann::json& schemaFile, const nlohmann::json& input)
{
valijson::Schema schema;
valijson::SchemaParser parser;
valijson::adapters::NlohmannJsonAdapter schemaAdapter(schemaFile);
parser.populateSchema(schemaAdapter, schema);
valijson::Validator validator;
valijson::adapters::NlohmannJsonAdapter targetAdapter(input);
return validator.validate(schema, targetAdapter, nullptr);
}
bool isPowerOn()
{
if (!powerMatch)
{
throw std::runtime_error("Power Match Not Created");
}
return powerStatusOn;
}
void setupPowerMatch(const std::shared_ptr<sdbusplus::asio::connection>& conn)
{
powerMatch = std::make_unique<sdbusplus::bus::match_t>(
static_cast<sdbusplus::bus_t&>(*conn),
"type='signal',interface='" + std::string(properties::interface) +
"',path='" + std::string(power::path) + "',arg0='" +
std::string(power::interface) + "'",
[](sdbusplus::message_t& message) {
std::string objectName;
boost::container::flat_map<std::string, std::variant<std::string>>
values;
message.read(objectName, values);
auto findState = values.find(power::property);
if (findState != values.end())
{
powerStatusOn = boost::ends_with(
std::get<std::string>(findState->second), "Running");
}
});
conn->async_method_call(
[](boost::system::error_code ec,
const std::variant<std::string>& state) {
if (ec)
{
return;
}
powerStatusOn =
boost::ends_with(std::get<std::string>(state), "Running");
},
power::busname, power::path, properties::interface, properties::get,
power::interface, power::property);
}
// Replaces the template character like the other version of this function,
// but checks all properties on all interfaces provided to do the substitution
// with.
std::optional<std::string> templateCharReplace(
nlohmann::json::iterator& keyPair, const DBusObject& object,
const size_t index, const std::optional<std::string>& replaceStr)
{
for (const auto& [_, interface] : object)
{
auto ret = templateCharReplace(keyPair, interface, index, replaceStr);
if (ret)
{
return ret;
}
}
return std::nullopt;
}
// finds the template character (currently set to $) and replaces the value with
// the field found in a dbus object i.e. $ADDRESS would get populated with the
// ADDRESS field from a object on dbus
std::optional<std::string> templateCharReplace(
nlohmann::json::iterator& keyPair, const DBusInterface& interface,
const size_t index, const std::optional<std::string>& replaceStr)
{
std::optional<std::string> ret = std::nullopt;
if (keyPair.value().type() == nlohmann::json::value_t::object ||
keyPair.value().type() == nlohmann::json::value_t::array)
{
for (auto nextLayer = keyPair.value().begin();
nextLayer != keyPair.value().end(); nextLayer++)
{
templateCharReplace(nextLayer, interface, index, replaceStr);
}
return ret;
}
std::string* strPtr = keyPair.value().get_ptr<std::string*>();
if (strPtr == nullptr)
{
return ret;
}
boost::replace_all(*strPtr, std::string(templateChar) + "index",
std::to_string(index));
if (replaceStr)
{
boost::replace_all(*strPtr, *replaceStr, std::to_string(index));
}
for (const auto& [propName, propValue] : interface)
{
std::string templateName = templateChar + propName;
boost::iterator_range<std::string::const_iterator> find =
boost::ifind_first(*strPtr, templateName);
if (!find)
{
continue;
}
size_t start = find.begin() - strPtr->begin();
// check for additional operations
if ((start == 0U) && find.end() == strPtr->end())
{
std::visit([&](auto&& val) { keyPair.value() = val; }, propValue);
return ret;
}
constexpr const std::array<char, 5> mathChars = {'+', '-', '%', '*',
'/'};
size_t nextItemIdx = start + templateName.size() + 1;
if (nextItemIdx > strPtr->size() ||
std::find(mathChars.begin(), mathChars.end(),
strPtr->at(nextItemIdx)) == mathChars.end())
{
std::string val = std::visit(VariantToStringVisitor(), propValue);
boost::ireplace_all(*strPtr, templateName, val);
continue;
}
// save the prefix
std::string prefix = strPtr->substr(0, start);
// operate on the rest
std::string end = strPtr->substr(nextItemIdx);
std::vector<std::string> split;
boost::split(split, end, boost::is_any_of(" "));
// need at least 1 operation and number
if (split.size() < 2)
{
std::cerr << "Syntax error on template replacement of " << *strPtr
<< "\n";
for (const std::string& data : split)
{
std::cerr << data << " ";
}
std::cerr << "\n";
continue;
}
// we assume that the replacement is a number, because we can
// only do math on numbers.. we might concatenate strings in the
// future, but thats later
int number = std::visit(VariantToIntVisitor(), propValue);
auto exprBegin = split.begin();
auto exprEnd = split.end();
number = expression::evaluate(number, exprBegin, exprEnd);
std::string replaced(find.begin(), find.end());
while (exprBegin != exprEnd)
{
replaced.append(" ").append(*exprBegin++);
}
ret = replaced;
std::string result = prefix + std::to_string(number);
while (exprEnd != split.end())
{
result.append(" ").append(*exprEnd++);
}
keyPair.value() = result;
// We probably just invalidated the pointer abovei,
// reset and continue to handle multiple templates
strPtr = keyPair.value().get_ptr<std::string*>();
if (strPtr == nullptr)
{
break;
}
}
strPtr = keyPair.value().get_ptr<std::string*>();
if (strPtr == nullptr)
{
return ret;
}
std::string_view strView = *strPtr;
int base = 10;
if (boost::starts_with(strView, "0x"))
{
strView.remove_prefix(2);
base = 16;
}
uint64_t temp = 0;
const char* strDataEndPtr = strView.data() + strView.size();
const std::from_chars_result res =
std::from_chars(strView.data(), strDataEndPtr, temp, base);
if (res.ec == std::errc{} && res.ptr == strDataEndPtr)
{
keyPair.value() = temp;
}
return ret;
}
/// \brief JSON/DBus matching Callable for std::variant (visitor)
///
/// Default match JSON/DBus match implementation
/// \tparam T The concrete DBus value type from DBusValueVariant
template <typename T>
struct MatchProbe
{
/// \param probe the probe statement to match against
/// \param value the property value being matched to a probe
/// \return true if the dbusValue matched the probe otherwise false
static bool match(const nlohmann::json& probe, const T& value)
{
return probe == value;
}
};
/// \brief JSON/DBus matching Callable for std::variant (visitor)
///
/// std::string specialization of MatchProbe enabling regex matching
template <>
struct MatchProbe<std::string>
{
/// \param probe the probe statement to match against
/// \param value the string value being matched to a probe
/// \return true if the dbusValue matched the probe otherwise false
static bool match(const nlohmann::json& probe, const std::string& value)
{
if (probe.is_string())
{
try
{
std::regex search(probe);
std::smatch regMatch;
return std::regex_search(value, regMatch, search);
}
catch (const std::regex_error&)
{
std::cerr << "Syntax error in regular expression: " << probe
<< " will never match";
}
}
// Skip calling nlohmann here, since it will never match a non-string
// to a std::string
return false;
}
};
/// \brief Forwarding JSON/DBus matching Callable for std::variant (visitor)
///
/// Forward calls to the correct template instantiation of MatchProbe
struct MatchProbeForwarder
{
explicit MatchProbeForwarder(const nlohmann::json& probe) : probeRef(probe)
{}
const nlohmann::json& probeRef;
template <typename T>
bool operator()(const T& dbusValue) const
{
return MatchProbe<T>::match(probeRef, dbusValue);
}
};
bool matchProbe(const nlohmann::json& probe, const DBusValueVariant& dbusValue)
{
return std::visit(MatchProbeForwarder(probe), dbusValue);
}