blob: 57c787f7cb6209b6cb6450d62290c123a664c558 [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*/
Brad Bishope45d8c72022-05-25 15:12:53 -040016/// \file utils.hpp
James Feist3cb5fec2018-01-23 14:41:51 -080017
18#pragma once
James Feist481c5d52019-08-13 14:40:40 -070019
James Feist1df06a42019-04-11 14:23:04 -070020#include <boost/container/flat_map.hpp>
James Feistb4383f42018-08-06 16:54:10 -070021#include <nlohmann/json.hpp>
James Feist1df06a42019-04-11 14:23:04 -070022#include <sdbusplus/asio/connection.hpp>
James Feist68500ff2018-08-08 15:40:42 -070023#include <sdbusplus/exception.hpp>
James Feist3cb5fec2018-01-23 14:41:51 -080024
James Feist8c505da2020-05-28 10:06:33 -070025#include <filesystem>
26#include <fstream>
27#include <iostream>
28
James Feist1df06a42019-04-11 14:23:04 -070029constexpr const char* configurationOutDir = "/var/configuration/";
30constexpr const char* versionHashFile = "/var/configuration/version";
31constexpr const char* versionFile = "/etc/os-release";
32
Ed Tanousfc171422024-04-04 17:18:16 -070033// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
Johnathan Mantey9b867872020-10-13 15:00:51 -070034extern boost::asio::io_context io;
35
Andrew Jefferyeab49292022-04-05 14:42:20 +093036using DBusValueVariant =
James Feist481c5d52019-08-13 14:40:40 -070037 std::variant<std::string, int64_t, uint64_t, double, int32_t, uint32_t,
Brad Bishopcd1868e2020-08-28 17:58:52 -040038 int16_t, uint16_t, uint8_t, bool, std::vector<uint8_t>>;
Andrew Jeffery1983d2f2022-04-05 14:55:13 +093039using DBusInterface = boost::container::flat_map<std::string, DBusValueVariant>;
40using DBusObject = boost::container::flat_map<std::string, DBusInterface>;
41using MapperGetSubTreeResponse =
42 boost::container::flat_map<std::string, DBusObject>;
James Feist481c5d52019-08-13 14:40:40 -070043
James Feist61f5ac42020-03-11 14:37:25 -070044namespace properties
45{
46constexpr const char* interface = "org.freedesktop.DBus.Properties";
47constexpr const char* get = "Get";
48} // namespace properties
49
50namespace power
51{
52const static constexpr char* busname = "xyz.openbmc_project.State.Host";
53const static constexpr char* interface = "xyz.openbmc_project.State.Host";
54const static constexpr char* path = "/xyz/openbmc_project/state/host0";
55const static constexpr char* property = "CurrentHostState";
56} // namespace power
57
James Feista465ccc2019-02-08 12:51:01 -080058bool findFiles(const std::filesystem::path& dirPath,
59 const std::string& matchString,
60 std::vector<std::filesystem::path>& foundPaths);
Andrew Jefferya9c58922021-06-01 09:28:59 +093061bool findFiles(const std::vector<std::filesystem::path>&& dirPaths,
62 const std::string& matchString,
63 std::vector<std::filesystem::path>& foundPaths);
James Feistb4383f42018-08-06 16:54:10 -070064
Nikhil Potaded8884f12019-03-27 13:27:13 -070065bool getI2cDevicePaths(
66 const std::filesystem::path& dirPath,
67 boost::container::flat_map<size_t, std::filesystem::path>& busPaths);
68
James Feista465ccc2019-02-08 12:51:01 -080069bool validateJson(const nlohmann::json& schemaFile,
70 const nlohmann::json& input);
James Feist1df06a42019-04-11 14:23:04 -070071
Delphine CC Chiua3ca14a2024-03-27 17:02:24 +080072bool isPowerOn();
James Feist1df06a42019-04-11 14:23:04 -070073void setupPowerMatch(const std::shared_ptr<sdbusplus::asio::connection>& conn);
James Feist68500ff2018-08-08 15:40:42 -070074struct DBusInternalError final : public sdbusplus::exception_t
75{
James Feista465ccc2019-02-08 12:51:01 -080076 const char* name() const noexcept override
James Feist68500ff2018-08-08 15:40:42 -070077 {
78 return "org.freedesktop.DBus.Error.Failed";
Patrick Williamscca78562021-09-08 15:43:23 -050079 }
James Feista465ccc2019-02-08 12:51:01 -080080 const char* description() const noexcept override
James Feist68500ff2018-08-08 15:40:42 -070081 {
82 return "internal error";
Patrick Williamscca78562021-09-08 15:43:23 -050083 }
James Feista465ccc2019-02-08 12:51:01 -080084 const char* what() const noexcept override
James Feist68500ff2018-08-08 15:40:42 -070085 {
86 return "org.freedesktop.DBus.Error.Failed: "
87 "internal error";
Patrick Williamscca78562021-09-08 15:43:23 -050088 }
89
90 int get_errno() const noexcept override
91 {
92 return EACCES;
93 }
James Feist68500ff2018-08-08 15:40:42 -070094};
James Feist1df06a42019-04-11 14:23:04 -070095
Delphine CC Chiua3ca14a2024-03-27 17:02:24 +080096inline bool fwVersionIsSame()
James Feist1df06a42019-04-11 14:23:04 -070097{
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 Feist1df06a42019-04-11 14:23:04 -0700119 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 Feist481c5d52019-08-13 14:40:40 -0700132}
133
James Feist35f5e0e2020-03-16 14:02:27 -0700134std::optional<std::string> templateCharReplace(
Ed Tanous3013fb42022-07-09 08:27:06 -0700135 nlohmann::json::iterator& keyPair, const DBusObject& object, size_t index,
Matt Spinlere789bf12021-02-24 12:33:01 -0600136 const std::optional<std::string>& replaceStr = std::nullopt);
137
138std::optional<std::string> templateCharReplace(
Andrew Jeffery1983d2f2022-04-05 14:55:13 +0930139 nlohmann::json::iterator& keyPair, const DBusInterface& interface,
Ed Tanous3013fb42022-07-09 08:27:06 -0700140 size_t index, const std::optional<std::string>& replaceStr = std::nullopt);
James Feist1ffa4a42020-04-22 18:27:17 -0700141
142inline bool deviceHasLogging(const nlohmann::json& json)
143{
144 auto logging = json.find("Logging");
145 if (logging != json.end())
146 {
Ed Tanous3013fb42022-07-09 08:27:06 -0700147 const auto* ptr = logging->get_ptr<const std::string*>();
148 if (ptr != nullptr)
James Feist1ffa4a42020-04-22 18:27:17 -0700149 {
150 if (*ptr == "Off")
151 {
152 return false;
153 }
154 }
155 }
156 return true;
157}
Brad Bishop3cb8a602020-08-25 17:40:54 -0400158
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 Jefferyeab49292022-04-05 14:42:20 +0930163bool matchProbe(const nlohmann::json& probe, const DBusValueVariant& dbusValue);