James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 1 | /* |
| 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 Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 17 | #include <Utils.hpp> |
James Feist | 637b3ef | 2019-04-15 16:35:30 -0700 | [diff] [blame] | 18 | #include <filesystem> |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 19 | #include <fstream> |
| 20 | #include <regex> |
James Feist | 1df06a4 | 2019-04-11 14:23:04 -0700 | [diff] [blame] | 21 | #include <sdbusplus/bus/match.hpp> |
James Feist | b4383f4 | 2018-08-06 16:54:10 -0700 | [diff] [blame] | 22 | #include <valijson/adapters/nlohmann_json_adapter.hpp> |
| 23 | #include <valijson/schema.hpp> |
| 24 | #include <valijson/schema_parser.hpp> |
| 25 | #include <valijson/validator.hpp> |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 26 | |
Ed Tanous | 072e25d | 2018-12-16 21:45:20 -0800 | [diff] [blame] | 27 | namespace fs = std::filesystem; |
James Feist | 1df06a4 | 2019-04-11 14:23:04 -0700 | [diff] [blame] | 28 | static bool powerStatusOn = false; |
| 29 | static std::unique_ptr<sdbusplus::bus::match::match> powerMatch = nullptr; |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 30 | |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 31 | bool findFiles(const fs::path& dirPath, const std::string& matchString, |
| 32 | std::vector<fs::path>& foundPaths) |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 33 | { |
James Feist | a3c180a | 2018-08-09 16:06:04 -0700 | [diff] [blame] | 34 | if (!fs::exists(dirPath)) |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 35 | return false; |
| 36 | |
James Feist | a3c180a | 2018-08-09 16:06:04 -0700 | [diff] [blame] | 37 | std::regex search(matchString); |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 38 | std::smatch match; |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 39 | for (const auto& p : fs::directory_iterator(dirPath)) |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 40 | { |
| 41 | std::string path = p.path().string(); |
James Feist | c95cb14 | 2018-02-26 10:41:42 -0800 | [diff] [blame] | 42 | if (std::regex_search(path, match, search)) |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 43 | { |
James Feist | a3c180a | 2018-08-09 16:06:04 -0700 | [diff] [blame] | 44 | foundPaths.emplace_back(p.path()); |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 45 | } |
| 46 | } |
| 47 | return true; |
James Feist | b4383f4 | 2018-08-06 16:54:10 -0700 | [diff] [blame] | 48 | } |
| 49 | |
Nikhil Potade | d8884f1 | 2019-03-27 13:27:13 -0700 | [diff] [blame] | 50 | bool 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 Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 80 | bool validateJson(const nlohmann::json& schemaFile, const nlohmann::json& input) |
James Feist | b4383f4 | 2018-08-06 16:54:10 -0700 | [diff] [blame] | 81 | { |
| 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 Tanous | 072e25d | 2018-12-16 21:45:20 -0800 | [diff] [blame] | 93 | } |
James Feist | 1df06a4 | 2019-04-11 14:23:04 -0700 | [diff] [blame] | 94 | |
| 95 | bool isPowerOn(void) |
| 96 | { |
| 97 | if (!powerMatch) |
| 98 | { |
| 99 | throw std::runtime_error("Power Match Not Created"); |
| 100 | } |
| 101 | return powerStatusOn; |
| 102 | } |
| 103 | |
| 104 | void 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 | } |