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 | e45d8c7 | 2022-05-25 15:12:53 -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 | |
Ed Tanous | fc17142 | 2024-04-04 17:18:16 -0700 | [diff] [blame] | 33 | // NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables) |
Johnathan Mantey | 9b86787 | 2020-10-13 15:00:51 -0700 | [diff] [blame] | 34 | extern boost::asio::io_context io; |
| 35 | |
Andrew Jeffery | eab4929 | 2022-04-05 14:42:20 +0930 | [diff] [blame] | 36 | using DBusValueVariant = |
James Feist | 481c5d5 | 2019-08-13 14:40:40 -0700 | [diff] [blame] | 37 | 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] | 38 | int16_t, uint16_t, uint8_t, bool, std::vector<uint8_t>>; |
Andrew Jeffery | 1983d2f | 2022-04-05 14:55:13 +0930 | [diff] [blame] | 39 | using DBusInterface = boost::container::flat_map<std::string, DBusValueVariant>; |
| 40 | using DBusObject = boost::container::flat_map<std::string, DBusInterface>; |
| 41 | using MapperGetSubTreeResponse = |
| 42 | boost::container::flat_map<std::string, DBusObject>; |
James Feist | 481c5d5 | 2019-08-13 14:40:40 -0700 | [diff] [blame] | 43 | |
James Feist | 61f5ac4 | 2020-03-11 14:37:25 -0700 | [diff] [blame] | 44 | namespace properties |
| 45 | { |
| 46 | constexpr const char* interface = "org.freedesktop.DBus.Properties"; |
| 47 | constexpr const char* get = "Get"; |
| 48 | } // namespace properties |
| 49 | |
| 50 | namespace power |
| 51 | { |
| 52 | const static constexpr char* busname = "xyz.openbmc_project.State.Host"; |
| 53 | const static constexpr char* interface = "xyz.openbmc_project.State.Host"; |
| 54 | const static constexpr char* path = "/xyz/openbmc_project/state/host0"; |
| 55 | const static constexpr char* property = "CurrentHostState"; |
| 56 | } // namespace power |
| 57 | |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 58 | bool findFiles(const std::filesystem::path& dirPath, |
| 59 | const std::string& matchString, |
| 60 | std::vector<std::filesystem::path>& foundPaths); |
Andrew Jeffery | a9c5892 | 2021-06-01 09:28:59 +0930 | [diff] [blame] | 61 | bool findFiles(const std::vector<std::filesystem::path>&& dirPaths, |
| 62 | const std::string& matchString, |
| 63 | std::vector<std::filesystem::path>& foundPaths); |
James Feist | b4383f4 | 2018-08-06 16:54:10 -0700 | [diff] [blame] | 64 | |
Nikhil Potade | d8884f1 | 2019-03-27 13:27:13 -0700 | [diff] [blame] | 65 | bool getI2cDevicePaths( |
| 66 | const std::filesystem::path& dirPath, |
| 67 | boost::container::flat_map<size_t, std::filesystem::path>& busPaths); |
| 68 | |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 69 | bool validateJson(const nlohmann::json& schemaFile, |
| 70 | const nlohmann::json& input); |
James Feist | 1df06a4 | 2019-04-11 14:23:04 -0700 | [diff] [blame] | 71 | |
Delphine CC Chiu | a3ca14a | 2024-03-27 17:02:24 +0800 | [diff] [blame] | 72 | bool isPowerOn(); |
James Feist | 1df06a4 | 2019-04-11 14:23:04 -0700 | [diff] [blame] | 73 | void setupPowerMatch(const std::shared_ptr<sdbusplus::asio::connection>& conn); |
James Feist | 68500ff | 2018-08-08 15:40:42 -0700 | [diff] [blame] | 74 | struct DBusInternalError final : public sdbusplus::exception_t |
| 75 | { |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 76 | const char* name() const noexcept override |
James Feist | 68500ff | 2018-08-08 15:40:42 -0700 | [diff] [blame] | 77 | { |
| 78 | return "org.freedesktop.DBus.Error.Failed"; |
Patrick Williams | cca7856 | 2021-09-08 15:43:23 -0500 | [diff] [blame] | 79 | } |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 80 | const char* description() const noexcept override |
James Feist | 68500ff | 2018-08-08 15:40:42 -0700 | [diff] [blame] | 81 | { |
| 82 | return "internal error"; |
Patrick Williams | cca7856 | 2021-09-08 15:43:23 -0500 | [diff] [blame] | 83 | } |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 84 | const char* what() const noexcept override |
James Feist | 68500ff | 2018-08-08 15:40:42 -0700 | [diff] [blame] | 85 | { |
| 86 | return "org.freedesktop.DBus.Error.Failed: " |
| 87 | "internal error"; |
Patrick Williams | cca7856 | 2021-09-08 15:43:23 -0500 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | int get_errno() const noexcept override |
| 91 | { |
| 92 | return EACCES; |
| 93 | } |
James Feist | 68500ff | 2018-08-08 15:40:42 -0700 | [diff] [blame] | 94 | }; |
James Feist | 1df06a4 | 2019-04-11 14:23:04 -0700 | [diff] [blame] | 95 | |
Delphine CC Chiu | a3ca14a | 2024-03-27 17:02:24 +0800 | [diff] [blame] | 96 | inline bool fwVersionIsSame() |
James Feist | 1df06a4 | 2019-04-11 14:23:04 -0700 | [diff] [blame] | 97 | { |
| 98 | std::ifstream version(versionFile); |
| 99 | if (!version.good()) |
| 100 | { |
| 101 | std::cerr << "Can't read " << versionFile << "\n"; |
| 102 | return false; |
| 103 | } |
| 104 | |
| 105 | std::string versionData; |
| 106 | std::string line; |
| 107 | while (std::getline(version, line)) |
| 108 | { |
| 109 | versionData += line; |
| 110 | } |
| 111 | |
| 112 | std::string expectedHash = |
| 113 | std::to_string(std::hash<std::string>{}(versionData)); |
| 114 | |
| 115 | std::filesystem::create_directory(configurationOutDir); |
| 116 | std::ifstream hashFile(versionHashFile); |
| 117 | if (hashFile.good()) |
| 118 | { |
James Feist | 1df06a4 | 2019-04-11 14:23:04 -0700 | [diff] [blame] | 119 | std::string hashString; |
| 120 | hashFile >> hashString; |
| 121 | |
| 122 | if (expectedHash == hashString) |
| 123 | { |
| 124 | return true; |
| 125 | } |
| 126 | hashFile.close(); |
| 127 | } |
| 128 | |
| 129 | std::ofstream output(versionHashFile); |
| 130 | output << expectedHash; |
| 131 | return false; |
James Feist | 481c5d5 | 2019-08-13 14:40:40 -0700 | [diff] [blame] | 132 | } |
| 133 | |
James Feist | 35f5e0e | 2020-03-16 14:02:27 -0700 | [diff] [blame] | 134 | std::optional<std::string> templateCharReplace( |
Ed Tanous | 3013fb4 | 2022-07-09 08:27:06 -0700 | [diff] [blame] | 135 | nlohmann::json::iterator& keyPair, const DBusObject& object, size_t index, |
Matt Spinler | e789bf1 | 2021-02-24 12:33:01 -0600 | [diff] [blame] | 136 | const std::optional<std::string>& replaceStr = std::nullopt); |
| 137 | |
| 138 | std::optional<std::string> templateCharReplace( |
Andrew Jeffery | 1983d2f | 2022-04-05 14:55:13 +0930 | [diff] [blame] | 139 | nlohmann::json::iterator& keyPair, const DBusInterface& interface, |
Ed Tanous | 3013fb4 | 2022-07-09 08:27:06 -0700 | [diff] [blame] | 140 | size_t index, const std::optional<std::string>& replaceStr = std::nullopt); |
James Feist | 1ffa4a4 | 2020-04-22 18:27:17 -0700 | [diff] [blame] | 141 | |
| 142 | inline bool deviceHasLogging(const nlohmann::json& json) |
| 143 | { |
| 144 | auto logging = json.find("Logging"); |
| 145 | if (logging != json.end()) |
| 146 | { |
Ed Tanous | 3013fb4 | 2022-07-09 08:27:06 -0700 | [diff] [blame] | 147 | const auto* ptr = logging->get_ptr<const std::string*>(); |
| 148 | if (ptr != nullptr) |
James Feist | 1ffa4a4 | 2020-04-22 18:27:17 -0700 | [diff] [blame] | 149 | { |
| 150 | if (*ptr == "Off") |
| 151 | { |
| 152 | return false; |
| 153 | } |
| 154 | } |
| 155 | } |
| 156 | return true; |
| 157 | } |
Brad Bishop | 3cb8a60 | 2020-08-25 17:40:54 -0400 | [diff] [blame] | 158 | |
| 159 | /// \brief Match a Dbus property against a probe statement. |
| 160 | /// \param probe the probe statement to match against. |
| 161 | /// \param dbusValue the property value being matched to a probe. |
| 162 | /// \return true if the dbusValue matched the probe otherwise false. |
Andrew Jeffery | eab4929 | 2022-04-05 14:42:20 +0930 | [diff] [blame] | 163 | bool matchProbe(const nlohmann::json& probe, const DBusValueVariant& dbusValue); |