blob: 06d2549cae04229f0e12dd83cb73c868aa5cd159 [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
James Feista465ccc2019-02-08 12:51:01 -080050bool validateJson(const nlohmann::json& schemaFile, const nlohmann::json& input)
James Feistb4383f42018-08-06 16:54:10 -070051{
52 valijson::Schema schema;
53 valijson::SchemaParser parser;
54 valijson::adapters::NlohmannJsonAdapter schemaAdapter(schemaFile);
55 parser.populateSchema(schemaAdapter, schema);
56 valijson::Validator validator;
57 valijson::adapters::NlohmannJsonAdapter targetAdapter(input);
58 if (!validator.validate(schema, targetAdapter, NULL))
59 {
60 return false;
61 }
62 return true;
Ed Tanous072e25d2018-12-16 21:45:20 -080063}
James Feist1df06a42019-04-11 14:23:04 -070064
65bool isPowerOn(void)
66{
67 if (!powerMatch)
68 {
69 throw std::runtime_error("Power Match Not Created");
70 }
71 return powerStatusOn;
72}
73
74void setupPowerMatch(const std::shared_ptr<sdbusplus::asio::connection>& conn)
75{
76 // create a match for powergood changes, first time do a method call to
77 // cache the correct value
78 std::function<void(sdbusplus::message::message & message)> eventHandler =
79 [](sdbusplus::message::message& message) {
80 std::string objectName;
81 boost::container::flat_map<std::string, std::variant<int32_t, bool>>
82 values;
83 message.read(objectName, values);
84 auto findPgood = values.find("pgood");
85 if (findPgood != values.end())
86 {
87 powerStatusOn = std::get<int32_t>(findPgood->second);
88 }
89 };
90
91 powerMatch = std::make_unique<sdbusplus::bus::match::match>(
92 static_cast<sdbusplus::bus::bus&>(*conn),
93 "type='signal',interface='org.freedesktop.DBus.Properties',path_"
94 "namespace='/xyz/openbmc_project/Chassis/Control/"
95 "Power0',arg0='xyz.openbmc_project.Chassis.Control.Power'",
96 eventHandler);
97}