blob: dd3effd562b4c57ba409b4a1f1130bb2ba805649 [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*/
16
17#pragma once
James Feist481c5d52019-08-13 14:40:40 -070018
James Feist1df06a42019-04-11 14:23:04 -070019#include <boost/container/flat_map.hpp>
James Feistb4383f42018-08-06 16:54:10 -070020#include <nlohmann/json.hpp>
James Feist1df06a42019-04-11 14:23:04 -070021#include <sdbusplus/asio/connection.hpp>
James Feist68500ff2018-08-08 15:40:42 -070022#include <sdbusplus/exception.hpp>
James Feist3cb5fec2018-01-23 14:41:51 -080023
James Feist8c505da2020-05-28 10:06:33 -070024#include <filesystem>
25#include <fstream>
26#include <iostream>
27
James Feist1df06a42019-04-11 14:23:04 -070028constexpr const char* configurationOutDir = "/var/configuration/";
29constexpr const char* versionHashFile = "/var/configuration/version";
30constexpr const char* versionFile = "/etc/os-release";
31
James Feist481c5d52019-08-13 14:40:40 -070032using BasicVariantType =
33 std::variant<std::string, int64_t, uint64_t, double, int32_t, uint32_t,
34 int16_t, uint16_t, uint8_t, bool>;
35
36enum class TemplateOperation
37{
38 addition,
39 division,
40 multiplication,
41 subtraction,
42 modulo,
43};
44
James Feist61f5ac42020-03-11 14:37:25 -070045namespace properties
46{
47constexpr const char* interface = "org.freedesktop.DBus.Properties";
48constexpr const char* get = "Get";
49} // namespace properties
50
51namespace power
52{
53const static constexpr char* busname = "xyz.openbmc_project.State.Host";
54const static constexpr char* interface = "xyz.openbmc_project.State.Host";
55const static constexpr char* path = "/xyz/openbmc_project/state/host0";
56const static constexpr char* property = "CurrentHostState";
57} // namespace power
58
James Feista465ccc2019-02-08 12:51:01 -080059bool findFiles(const std::filesystem::path& dirPath,
60 const std::string& matchString,
61 std::vector<std::filesystem::path>& foundPaths);
James Feistb4383f42018-08-06 16:54:10 -070062
Nikhil Potaded8884f12019-03-27 13:27:13 -070063bool getI2cDevicePaths(
64 const std::filesystem::path& dirPath,
65 boost::container::flat_map<size_t, std::filesystem::path>& busPaths);
66
James Feista465ccc2019-02-08 12:51:01 -080067bool validateJson(const nlohmann::json& schemaFile,
68 const nlohmann::json& input);
James Feist1df06a42019-04-11 14:23:04 -070069
70bool isPowerOn(void);
71void setupPowerMatch(const std::shared_ptr<sdbusplus::asio::connection>& conn);
James Feist68500ff2018-08-08 15:40:42 -070072struct DBusInternalError final : public sdbusplus::exception_t
73{
James Feista465ccc2019-02-08 12:51:01 -080074 const char* name() const noexcept override
James Feist68500ff2018-08-08 15:40:42 -070075 {
76 return "org.freedesktop.DBus.Error.Failed";
77 };
James Feista465ccc2019-02-08 12:51:01 -080078 const char* description() const noexcept override
James Feist68500ff2018-08-08 15:40:42 -070079 {
80 return "internal error";
81 };
James Feista465ccc2019-02-08 12:51:01 -080082 const char* what() const noexcept override
James Feist68500ff2018-08-08 15:40:42 -070083 {
84 return "org.freedesktop.DBus.Error.Failed: "
85 "internal error";
86 };
87};
James Feist1df06a42019-04-11 14:23:04 -070088
89inline bool fwVersionIsSame(void)
90{
91 std::ifstream version(versionFile);
92 if (!version.good())
93 {
94 std::cerr << "Can't read " << versionFile << "\n";
95 return false;
96 }
97
98 std::string versionData;
99 std::string line;
100 while (std::getline(version, line))
101 {
102 versionData += line;
103 }
104
105 std::string expectedHash =
106 std::to_string(std::hash<std::string>{}(versionData));
107
108 std::filesystem::create_directory(configurationOutDir);
109 std::ifstream hashFile(versionHashFile);
110 if (hashFile.good())
111 {
112
113 std::string hashString;
114 hashFile >> hashString;
115
116 if (expectedHash == hashString)
117 {
118 return true;
119 }
120 hashFile.close();
121 }
122
123 std::ofstream output(versionHashFile);
124 output << expectedHash;
125 return false;
James Feist481c5d52019-08-13 14:40:40 -0700126}
127
James Feist35f5e0e2020-03-16 14:02:27 -0700128std::optional<std::string> templateCharReplace(
James Feist481c5d52019-08-13 14:40:40 -0700129 nlohmann::json::iterator& keyPair,
130 const boost::container::flat_map<std::string, BasicVariantType>&
131 foundDevice,
James Feist35f5e0e2020-03-16 14:02:27 -0700132 const size_t foundDeviceIdx,
James Feist1ffa4a42020-04-22 18:27:17 -0700133 const std::optional<std::string>& replaceStr = std::nullopt);
134
135inline bool deviceHasLogging(const nlohmann::json& json)
136{
137 auto logging = json.find("Logging");
138 if (logging != json.end())
139 {
140 auto ptr = logging->get_ptr<const std::string*>();
141 if (ptr)
142 {
143 if (*ptr == "Off")
144 {
145 return false;
146 }
147 }
148 }
149 return true;
150}