blob: 160e9fe92152b04236119494b62e2b532a666c7b [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
Ed Tanous072e25d2018-12-16 21:45:20 -080017#include "filesystem.hpp"
James Feista465ccc2019-02-08 12:51:01 -080018
19#include <Utils.hpp>
James Feist3cb5fec2018-01-23 14:41:51 -080020#include <fstream>
21#include <regex>
James Feist1df06a42019-04-11 14:23:04 -070022#include <sdbusplus/bus/match.hpp>
James Feistb4383f42018-08-06 16:54:10 -070023#include <valijson/adapters/nlohmann_json_adapter.hpp>
24#include <valijson/schema.hpp>
25#include <valijson/schema_parser.hpp>
26#include <valijson/validator.hpp>
James Feist3cb5fec2018-01-23 14:41:51 -080027
Ed Tanous072e25d2018-12-16 21:45:20 -080028namespace fs = std::filesystem;
James Feist1df06a42019-04-11 14:23:04 -070029static bool powerStatusOn = false;
30static std::unique_ptr<sdbusplus::bus::match::match> powerMatch = nullptr;
James Feist3cb5fec2018-01-23 14:41:51 -080031
James Feista465ccc2019-02-08 12:51:01 -080032bool findFiles(const fs::path& dirPath, const std::string& matchString,
33 std::vector<fs::path>& foundPaths)
James Feist3cb5fec2018-01-23 14:41:51 -080034{
James Feista3c180a2018-08-09 16:06:04 -070035 if (!fs::exists(dirPath))
James Feist3cb5fec2018-01-23 14:41:51 -080036 return false;
37
James Feista3c180a2018-08-09 16:06:04 -070038 std::regex search(matchString);
James Feist3cb5fec2018-01-23 14:41:51 -080039 std::smatch match;
James Feista465ccc2019-02-08 12:51:01 -080040 for (const auto& p : fs::directory_iterator(dirPath))
James Feist3cb5fec2018-01-23 14:41:51 -080041 {
42 std::string path = p.path().string();
James Feistc95cb142018-02-26 10:41:42 -080043 if (std::regex_search(path, match, search))
James Feist3cb5fec2018-01-23 14:41:51 -080044 {
James Feista3c180a2018-08-09 16:06:04 -070045 foundPaths.emplace_back(p.path());
James Feist3cb5fec2018-01-23 14:41:51 -080046 }
47 }
48 return true;
James Feistb4383f42018-08-06 16:54:10 -070049}
50
James Feista465ccc2019-02-08 12:51:01 -080051bool validateJson(const nlohmann::json& schemaFile, const nlohmann::json& input)
James Feistb4383f42018-08-06 16:54:10 -070052{
53 valijson::Schema schema;
54 valijson::SchemaParser parser;
55 valijson::adapters::NlohmannJsonAdapter schemaAdapter(schemaFile);
56 parser.populateSchema(schemaAdapter, schema);
57 valijson::Validator validator;
58 valijson::adapters::NlohmannJsonAdapter targetAdapter(input);
59 if (!validator.validate(schema, targetAdapter, NULL))
60 {
61 return false;
62 }
63 return true;
Ed Tanous072e25d2018-12-16 21:45:20 -080064}
James Feist1df06a42019-04-11 14:23:04 -070065
66bool isPowerOn(void)
67{
68 if (!powerMatch)
69 {
70 throw std::runtime_error("Power Match Not Created");
71 }
72 return powerStatusOn;
73}
74
75void setupPowerMatch(const std::shared_ptr<sdbusplus::asio::connection>& conn)
76{
77 // create a match for powergood changes, first time do a method call to
78 // cache the correct value
79 std::function<void(sdbusplus::message::message & message)> eventHandler =
80 [](sdbusplus::message::message& message) {
81 std::string objectName;
82 boost::container::flat_map<std::string, std::variant<int32_t, bool>>
83 values;
84 message.read(objectName, values);
85 auto findPgood = values.find("pgood");
86 if (findPgood != values.end())
87 {
88 powerStatusOn = std::get<int32_t>(findPgood->second);
89 }
90 };
91
92 powerMatch = std::make_unique<sdbusplus::bus::match::match>(
93 static_cast<sdbusplus::bus::bus&>(*conn),
94 "type='signal',interface='org.freedesktop.DBus.Properties',path_"
95 "namespace='/xyz/openbmc_project/Chassis/Control/"
96 "Power0',arg0='xyz.openbmc_project.Chassis.Control.Power'",
97 eventHandler);
98}