blob: da3accee3a02b233ba7307660e6f852a26b95eac [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 Feist637b3ef2019-04-15 16:35:30 -070020#include <filesystem>
James Feist1df06a42019-04-11 14:23:04 -070021#include <fstream>
22#include <iostream>
James Feistb4383f42018-08-06 16:54:10 -070023#include <nlohmann/json.hpp>
James Feist1df06a42019-04-11 14:23:04 -070024#include <sdbusplus/asio/connection.hpp>
James Feist68500ff2018-08-08 15:40:42 -070025#include <sdbusplus/exception.hpp>
James Feist3cb5fec2018-01-23 14:41:51 -080026
James Feist1df06a42019-04-11 14:23:04 -070027constexpr const char* configurationOutDir = "/var/configuration/";
28constexpr const char* versionHashFile = "/var/configuration/version";
29constexpr const char* versionFile = "/etc/os-release";
30
James Feist481c5d52019-08-13 14:40:40 -070031using BasicVariantType =
32 std::variant<std::string, int64_t, uint64_t, double, int32_t, uint32_t,
33 int16_t, uint16_t, uint8_t, bool>;
34
35enum class TemplateOperation
36{
37 addition,
38 division,
39 multiplication,
40 subtraction,
41 modulo,
42};
43
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);
James Feistb4383f42018-08-06 16:54:10 -070061
Nikhil Potaded8884f12019-03-27 13:27:13 -070062bool getI2cDevicePaths(
63 const std::filesystem::path& dirPath,
64 boost::container::flat_map<size_t, std::filesystem::path>& busPaths);
65
James Feista465ccc2019-02-08 12:51:01 -080066bool validateJson(const nlohmann::json& schemaFile,
67 const nlohmann::json& input);
James Feist1df06a42019-04-11 14:23:04 -070068
69bool isPowerOn(void);
70void setupPowerMatch(const std::shared_ptr<sdbusplus::asio::connection>& conn);
James Feist68500ff2018-08-08 15:40:42 -070071struct DBusInternalError final : public sdbusplus::exception_t
72{
James Feista465ccc2019-02-08 12:51:01 -080073 const char* name() const noexcept override
James Feist68500ff2018-08-08 15:40:42 -070074 {
75 return "org.freedesktop.DBus.Error.Failed";
76 };
James Feista465ccc2019-02-08 12:51:01 -080077 const char* description() const noexcept override
James Feist68500ff2018-08-08 15:40:42 -070078 {
79 return "internal error";
80 };
James Feista465ccc2019-02-08 12:51:01 -080081 const char* what() const noexcept override
James Feist68500ff2018-08-08 15:40:42 -070082 {
83 return "org.freedesktop.DBus.Error.Failed: "
84 "internal error";
85 };
86};
James Feist1df06a42019-04-11 14:23:04 -070087
88inline bool fwVersionIsSame(void)
89{
90 std::ifstream version(versionFile);
91 if (!version.good())
92 {
93 std::cerr << "Can't read " << versionFile << "\n";
94 return false;
95 }
96
97 std::string versionData;
98 std::string line;
99 while (std::getline(version, line))
100 {
101 versionData += line;
102 }
103
104 std::string expectedHash =
105 std::to_string(std::hash<std::string>{}(versionData));
106
107 std::filesystem::create_directory(configurationOutDir);
108 std::ifstream hashFile(versionHashFile);
109 if (hashFile.good())
110 {
111
112 std::string hashString;
113 hashFile >> hashString;
114
115 if (expectedHash == hashString)
116 {
117 return true;
118 }
119 hashFile.close();
120 }
121
122 std::ofstream output(versionHashFile);
123 output << expectedHash;
124 return false;
James Feist481c5d52019-08-13 14:40:40 -0700125}
126
127void templateCharReplace(
128 nlohmann::json::iterator& keyPair,
129 const boost::container::flat_map<std::string, BasicVariantType>&
130 foundDevice,
131 const size_t foundDeviceIdx);