blob: ac16d36285182fdf926cac932c05598b036a88d0 [file] [log] [blame]
James Feist3cb5fec2018-01-23 14:41:51 -08001/*
2// Copyright (c) 2017 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
James Feista465ccc2019-02-08 12:51:01 -080017#include <Utils.hpp>
James Feist637b3ef2019-04-15 16:35:30 -070018#include <filesystem>
James Feist3cb5fec2018-01-23 14:41:51 -080019#include <fstream>
20#include <regex>
James Feist1df06a42019-04-11 14:23:04 -070021#include <sdbusplus/bus/match.hpp>
James Feistb4383f42018-08-06 16:54:10 -070022#include <valijson/adapters/nlohmann_json_adapter.hpp>
23#include <valijson/schema.hpp>
24#include <valijson/schema_parser.hpp>
25#include <valijson/validator.hpp>
James Feist3cb5fec2018-01-23 14:41:51 -080026
Ed Tanous072e25d2018-12-16 21:45:20 -080027namespace fs = std::filesystem;
James Feist1df06a42019-04-11 14:23:04 -070028static bool powerStatusOn = false;
29static std::unique_ptr<sdbusplus::bus::match::match> powerMatch = nullptr;
James Feist3cb5fec2018-01-23 14:41:51 -080030
James Feista465ccc2019-02-08 12:51:01 -080031bool findFiles(const fs::path& dirPath, const std::string& matchString,
32 std::vector<fs::path>& foundPaths)
James Feist3cb5fec2018-01-23 14:41:51 -080033{
James Feista3c180a2018-08-09 16:06:04 -070034 if (!fs::exists(dirPath))
James Feist3cb5fec2018-01-23 14:41:51 -080035 return false;
36
James Feista3c180a2018-08-09 16:06:04 -070037 std::regex search(matchString);
James Feist3cb5fec2018-01-23 14:41:51 -080038 std::smatch match;
James Feista465ccc2019-02-08 12:51:01 -080039 for (const auto& p : fs::directory_iterator(dirPath))
James Feist3cb5fec2018-01-23 14:41:51 -080040 {
41 std::string path = p.path().string();
James Feistc95cb142018-02-26 10:41:42 -080042 if (std::regex_search(path, match, search))
James Feist3cb5fec2018-01-23 14:41:51 -080043 {
James Feista3c180a2018-08-09 16:06:04 -070044 foundPaths.emplace_back(p.path());
James Feist3cb5fec2018-01-23 14:41:51 -080045 }
46 }
47 return true;
James Feistb4383f42018-08-06 16:54:10 -070048}
49
Nikhil Potaded8884f12019-03-27 13:27:13 -070050bool getI2cDevicePaths(const fs::path& dirPath,
51 boost::container::flat_map<size_t, fs::path>& busPaths)
52{
53 if (!fs::exists(dirPath))
54 {
55 return false;
56 }
57
58 // Regex for matching the path
59 std::regex searchPath(std::string(R"(i2c-\d+$)"));
60 // Regex for matching the bus numbers
61 std::regex searchBus(std::string(R"(\w[^-]*$)"));
62 std::smatch matchPath;
63 std::smatch matchBus;
64 for (const auto& p : fs::directory_iterator(dirPath))
65 {
66 std::string path = p.path().string();
67 if (std::regex_search(path, matchPath, searchPath))
68 {
69 if (std::regex_search(path, matchBus, searchBus))
70 {
71 size_t bus = stoul(*matchBus.begin());
72 busPaths.insert(std::pair<size_t, fs::path>(bus, p.path()));
73 }
74 }
75 }
76
77 return true;
78}
79
James Feista465ccc2019-02-08 12:51:01 -080080bool validateJson(const nlohmann::json& schemaFile, const nlohmann::json& input)
James Feistb4383f42018-08-06 16:54:10 -070081{
82 valijson::Schema schema;
83 valijson::SchemaParser parser;
84 valijson::adapters::NlohmannJsonAdapter schemaAdapter(schemaFile);
85 parser.populateSchema(schemaAdapter, schema);
86 valijson::Validator validator;
87 valijson::adapters::NlohmannJsonAdapter targetAdapter(input);
88 if (!validator.validate(schema, targetAdapter, NULL))
89 {
90 return false;
91 }
92 return true;
Ed Tanous072e25d2018-12-16 21:45:20 -080093}
James Feist1df06a42019-04-11 14:23:04 -070094
95bool isPowerOn(void)
96{
97 if (!powerMatch)
98 {
99 throw std::runtime_error("Power Match Not Created");
100 }
101 return powerStatusOn;
102}
103
104void setupPowerMatch(const std::shared_ptr<sdbusplus::asio::connection>& conn)
105{
106 // create a match for powergood changes, first time do a method call to
107 // cache the correct value
108 std::function<void(sdbusplus::message::message & message)> eventHandler =
109 [](sdbusplus::message::message& message) {
110 std::string objectName;
111 boost::container::flat_map<std::string, std::variant<int32_t, bool>>
112 values;
113 message.read(objectName, values);
114 auto findPgood = values.find("pgood");
115 if (findPgood != values.end())
116 {
117 powerStatusOn = std::get<int32_t>(findPgood->second);
118 }
119 };
120
121 powerMatch = std::make_unique<sdbusplus::bus::match::match>(
122 static_cast<sdbusplus::bus::bus&>(*conn),
123 "type='signal',interface='org.freedesktop.DBus.Properties',path_"
124 "namespace='/xyz/openbmc_project/Chassis/Control/"
125 "Power0',arg0='xyz.openbmc_project.Chassis.Control.Power'",
126 eventHandler);
127}