blob: 8ac27a1845d78b8fa342425a3241d034828a96b0 [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 Bishop1fb9f3f2020-08-28 08:15:13 -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
James Feist481c5d52019-08-13 14:40:40 -070033using BasicVariantType =
34 std::variant<std::string, int64_t, uint64_t, double, int32_t, uint32_t,
Brad Bishopcd1868e2020-08-28 17:58:52 -040035 int16_t, uint16_t, uint8_t, bool, std::vector<uint8_t>>;
James Feist481c5d52019-08-13 14:40:40 -070036
37enum class TemplateOperation
38{
39 addition,
40 division,
41 multiplication,
42 subtraction,
43 modulo,
44};
45
James Feist61f5ac42020-03-11 14:37:25 -070046namespace properties
47{
48constexpr const char* interface = "org.freedesktop.DBus.Properties";
49constexpr const char* get = "Get";
50} // namespace properties
51
52namespace power
53{
54const static constexpr char* busname = "xyz.openbmc_project.State.Host";
55const static constexpr char* interface = "xyz.openbmc_project.State.Host";
56const static constexpr char* path = "/xyz/openbmc_project/state/host0";
57const static constexpr char* property = "CurrentHostState";
58} // namespace power
59
James Feista465ccc2019-02-08 12:51:01 -080060bool findFiles(const std::filesystem::path& dirPath,
61 const std::string& matchString,
62 std::vector<std::filesystem::path>& foundPaths);
James Feistb4383f42018-08-06 16:54:10 -070063
Nikhil Potaded8884f12019-03-27 13:27:13 -070064bool getI2cDevicePaths(
65 const std::filesystem::path& dirPath,
66 boost::container::flat_map<size_t, std::filesystem::path>& busPaths);
67
James Feista465ccc2019-02-08 12:51:01 -080068bool validateJson(const nlohmann::json& schemaFile,
69 const nlohmann::json& input);
James Feist1df06a42019-04-11 14:23:04 -070070
71bool isPowerOn(void);
72void setupPowerMatch(const std::shared_ptr<sdbusplus::asio::connection>& conn);
James Feist68500ff2018-08-08 15:40:42 -070073struct DBusInternalError final : public sdbusplus::exception_t
74{
James Feista465ccc2019-02-08 12:51:01 -080075 const char* name() const noexcept override
James Feist68500ff2018-08-08 15:40:42 -070076 {
77 return "org.freedesktop.DBus.Error.Failed";
78 };
James Feista465ccc2019-02-08 12:51:01 -080079 const char* description() const noexcept override
James Feist68500ff2018-08-08 15:40:42 -070080 {
81 return "internal error";
82 };
James Feista465ccc2019-02-08 12:51:01 -080083 const char* what() const noexcept override
James Feist68500ff2018-08-08 15:40:42 -070084 {
85 return "org.freedesktop.DBus.Error.Failed: "
86 "internal error";
87 };
88};
James Feist1df06a42019-04-11 14:23:04 -070089
90inline bool fwVersionIsSame(void)
91{
92 std::ifstream version(versionFile);
93 if (!version.good())
94 {
95 std::cerr << "Can't read " << versionFile << "\n";
96 return false;
97 }
98
99 std::string versionData;
100 std::string line;
101 while (std::getline(version, line))
102 {
103 versionData += line;
104 }
105
106 std::string expectedHash =
107 std::to_string(std::hash<std::string>{}(versionData));
108
109 std::filesystem::create_directory(configurationOutDir);
110 std::ifstream hashFile(versionHashFile);
111 if (hashFile.good())
112 {
113
114 std::string hashString;
115 hashFile >> hashString;
116
117 if (expectedHash == hashString)
118 {
119 return true;
120 }
121 hashFile.close();
122 }
123
124 std::ofstream output(versionHashFile);
125 output << expectedHash;
126 return false;
James Feist481c5d52019-08-13 14:40:40 -0700127}
128
James Feist35f5e0e2020-03-16 14:02:27 -0700129std::optional<std::string> templateCharReplace(
James Feist481c5d52019-08-13 14:40:40 -0700130 nlohmann::json::iterator& keyPair,
131 const boost::container::flat_map<std::string, BasicVariantType>&
132 foundDevice,
James Feist35f5e0e2020-03-16 14:02:27 -0700133 const size_t foundDeviceIdx,
James Feist1ffa4a42020-04-22 18:27:17 -0700134 const std::optional<std::string>& replaceStr = std::nullopt);
135
136inline bool deviceHasLogging(const nlohmann::json& json)
137{
138 auto logging = json.find("Logging");
139 if (logging != json.end())
140 {
141 auto ptr = logging->get_ptr<const std::string*>();
142 if (ptr)
143 {
144 if (*ptr == "Off")
145 {
146 return false;
147 }
148 }
149 }
150 return true;
151}
Brad Bishop3cb8a602020-08-25 17:40:54 -0400152
153/// \brief Match a Dbus property against a probe statement.
154/// \param probe the probe statement to match against.
155/// \param dbusValue the property value being matched to a probe.
156/// \return true if the dbusValue matched the probe otherwise false.
157bool matchProbe(const nlohmann::json& probe, const BasicVariantType& dbusValue);