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 | */ |
Brad Bishop | 1fb9f3f | 2020-08-28 08:15:13 -0400 | [diff] [blame] | 16 | /// \file Utils.hpp |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 17 | |
| 18 | #pragma once |
James Feist | 481c5d5 | 2019-08-13 14:40:40 -0700 | [diff] [blame] | 19 | |
James Feist | 1df06a4 | 2019-04-11 14:23:04 -0700 | [diff] [blame] | 20 | #include <boost/container/flat_map.hpp> |
James Feist | b4383f4 | 2018-08-06 16:54:10 -0700 | [diff] [blame] | 21 | #include <nlohmann/json.hpp> |
James Feist | 1df06a4 | 2019-04-11 14:23:04 -0700 | [diff] [blame] | 22 | #include <sdbusplus/asio/connection.hpp> |
James Feist | 68500ff | 2018-08-08 15:40:42 -0700 | [diff] [blame] | 23 | #include <sdbusplus/exception.hpp> |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 24 | |
James Feist | 8c505da | 2020-05-28 10:06:33 -0700 | [diff] [blame] | 25 | #include <filesystem> |
| 26 | #include <fstream> |
| 27 | #include <iostream> |
| 28 | |
James Feist | 1df06a4 | 2019-04-11 14:23:04 -0700 | [diff] [blame] | 29 | constexpr const char* configurationOutDir = "/var/configuration/"; |
| 30 | constexpr const char* versionHashFile = "/var/configuration/version"; |
| 31 | constexpr const char* versionFile = "/etc/os-release"; |
| 32 | |
Johnathan Mantey | 9b86787 | 2020-10-13 15:00:51 -0700 | [diff] [blame^] | 33 | extern boost::asio::io_context io; |
| 34 | |
James Feist | 481c5d5 | 2019-08-13 14:40:40 -0700 | [diff] [blame] | 35 | using BasicVariantType = |
| 36 | std::variant<std::string, int64_t, uint64_t, double, int32_t, uint32_t, |
Brad Bishop | cd1868e | 2020-08-28 17:58:52 -0400 | [diff] [blame] | 37 | int16_t, uint16_t, uint8_t, bool, std::vector<uint8_t>>; |
James Feist | 481c5d5 | 2019-08-13 14:40:40 -0700 | [diff] [blame] | 38 | |
| 39 | enum class TemplateOperation |
| 40 | { |
| 41 | addition, |
| 42 | division, |
| 43 | multiplication, |
| 44 | subtraction, |
| 45 | modulo, |
| 46 | }; |
| 47 | |
James Feist | 61f5ac4 | 2020-03-11 14:37:25 -0700 | [diff] [blame] | 48 | namespace properties |
| 49 | { |
| 50 | constexpr const char* interface = "org.freedesktop.DBus.Properties"; |
| 51 | constexpr const char* get = "Get"; |
| 52 | } // namespace properties |
| 53 | |
| 54 | namespace power |
| 55 | { |
| 56 | const static constexpr char* busname = "xyz.openbmc_project.State.Host"; |
| 57 | const static constexpr char* interface = "xyz.openbmc_project.State.Host"; |
| 58 | const static constexpr char* path = "/xyz/openbmc_project/state/host0"; |
| 59 | const static constexpr char* property = "CurrentHostState"; |
| 60 | } // namespace power |
| 61 | |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 62 | bool findFiles(const std::filesystem::path& dirPath, |
| 63 | const std::string& matchString, |
| 64 | std::vector<std::filesystem::path>& foundPaths); |
James Feist | b4383f4 | 2018-08-06 16:54:10 -0700 | [diff] [blame] | 65 | |
Nikhil Potade | d8884f1 | 2019-03-27 13:27:13 -0700 | [diff] [blame] | 66 | bool getI2cDevicePaths( |
| 67 | const std::filesystem::path& dirPath, |
| 68 | boost::container::flat_map<size_t, std::filesystem::path>& busPaths); |
| 69 | |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 70 | bool validateJson(const nlohmann::json& schemaFile, |
| 71 | const nlohmann::json& input); |
James Feist | 1df06a4 | 2019-04-11 14:23:04 -0700 | [diff] [blame] | 72 | |
| 73 | bool isPowerOn(void); |
| 74 | void setupPowerMatch(const std::shared_ptr<sdbusplus::asio::connection>& conn); |
James Feist | 68500ff | 2018-08-08 15:40:42 -0700 | [diff] [blame] | 75 | struct DBusInternalError final : public sdbusplus::exception_t |
| 76 | { |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 77 | const char* name() const noexcept override |
James Feist | 68500ff | 2018-08-08 15:40:42 -0700 | [diff] [blame] | 78 | { |
| 79 | return "org.freedesktop.DBus.Error.Failed"; |
| 80 | }; |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 81 | const char* description() const noexcept override |
James Feist | 68500ff | 2018-08-08 15:40:42 -0700 | [diff] [blame] | 82 | { |
| 83 | return "internal error"; |
| 84 | }; |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 85 | const char* what() const noexcept override |
James Feist | 68500ff | 2018-08-08 15:40:42 -0700 | [diff] [blame] | 86 | { |
| 87 | return "org.freedesktop.DBus.Error.Failed: " |
| 88 | "internal error"; |
| 89 | }; |
| 90 | }; |
James Feist | 1df06a4 | 2019-04-11 14:23:04 -0700 | [diff] [blame] | 91 | |
| 92 | inline bool fwVersionIsSame(void) |
| 93 | { |
| 94 | std::ifstream version(versionFile); |
| 95 | if (!version.good()) |
| 96 | { |
| 97 | std::cerr << "Can't read " << versionFile << "\n"; |
| 98 | return false; |
| 99 | } |
| 100 | |
| 101 | std::string versionData; |
| 102 | std::string line; |
| 103 | while (std::getline(version, line)) |
| 104 | { |
| 105 | versionData += line; |
| 106 | } |
| 107 | |
| 108 | std::string expectedHash = |
| 109 | std::to_string(std::hash<std::string>{}(versionData)); |
| 110 | |
| 111 | std::filesystem::create_directory(configurationOutDir); |
| 112 | std::ifstream hashFile(versionHashFile); |
| 113 | if (hashFile.good()) |
| 114 | { |
| 115 | |
| 116 | std::string hashString; |
| 117 | hashFile >> hashString; |
| 118 | |
| 119 | if (expectedHash == hashString) |
| 120 | { |
| 121 | return true; |
| 122 | } |
| 123 | hashFile.close(); |
| 124 | } |
| 125 | |
| 126 | std::ofstream output(versionHashFile); |
| 127 | output << expectedHash; |
| 128 | return false; |
James Feist | 481c5d5 | 2019-08-13 14:40:40 -0700 | [diff] [blame] | 129 | } |
| 130 | |
James Feist | 35f5e0e | 2020-03-16 14:02:27 -0700 | [diff] [blame] | 131 | std::optional<std::string> templateCharReplace( |
James Feist | 481c5d5 | 2019-08-13 14:40:40 -0700 | [diff] [blame] | 132 | nlohmann::json::iterator& keyPair, |
Matt Spinler | e789bf1 | 2021-02-24 12:33:01 -0600 | [diff] [blame] | 133 | const boost::container::flat_map< |
| 134 | std::string, boost::container::flat_map<std::string, BasicVariantType>>& |
| 135 | allInterfaces, |
| 136 | const size_t foundDeviceIdx, |
| 137 | const std::optional<std::string>& replaceStr = std::nullopt); |
| 138 | |
| 139 | std::optional<std::string> templateCharReplace( |
| 140 | nlohmann::json::iterator& keyPair, |
James Feist | 481c5d5 | 2019-08-13 14:40:40 -0700 | [diff] [blame] | 141 | const boost::container::flat_map<std::string, BasicVariantType>& |
| 142 | foundDevice, |
James Feist | 35f5e0e | 2020-03-16 14:02:27 -0700 | [diff] [blame] | 143 | const size_t foundDeviceIdx, |
James Feist | 1ffa4a4 | 2020-04-22 18:27:17 -0700 | [diff] [blame] | 144 | const std::optional<std::string>& replaceStr = std::nullopt); |
| 145 | |
| 146 | inline bool deviceHasLogging(const nlohmann::json& json) |
| 147 | { |
| 148 | auto logging = json.find("Logging"); |
| 149 | if (logging != json.end()) |
| 150 | { |
| 151 | auto ptr = logging->get_ptr<const std::string*>(); |
| 152 | if (ptr) |
| 153 | { |
| 154 | if (*ptr == "Off") |
| 155 | { |
| 156 | return false; |
| 157 | } |
| 158 | } |
| 159 | } |
| 160 | return true; |
| 161 | } |
Brad Bishop | 3cb8a60 | 2020-08-25 17:40:54 -0400 | [diff] [blame] | 162 | |
| 163 | /// \brief Match a Dbus property against a probe statement. |
| 164 | /// \param probe the probe statement to match against. |
| 165 | /// \param dbusValue the property value being matched to a probe. |
| 166 | /// \return true if the dbusValue matched the probe otherwise false. |
| 167 | bool matchProbe(const nlohmann::json& probe, const BasicVariantType& dbusValue); |