blob: 7f8076c3086fa01f7c092c4fa2a4703f6360fb43 [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 Feista465ccc2019-02-08 12:51:01 -080044bool findFiles(const std::filesystem::path& dirPath,
45 const std::string& matchString,
46 std::vector<std::filesystem::path>& foundPaths);
James Feistb4383f42018-08-06 16:54:10 -070047
Nikhil Potaded8884f12019-03-27 13:27:13 -070048bool getI2cDevicePaths(
49 const std::filesystem::path& dirPath,
50 boost::container::flat_map<size_t, std::filesystem::path>& busPaths);
51
James Feista465ccc2019-02-08 12:51:01 -080052bool validateJson(const nlohmann::json& schemaFile,
53 const nlohmann::json& input);
James Feist1df06a42019-04-11 14:23:04 -070054
55bool isPowerOn(void);
56void setupPowerMatch(const std::shared_ptr<sdbusplus::asio::connection>& conn);
James Feist68500ff2018-08-08 15:40:42 -070057struct DBusInternalError final : public sdbusplus::exception_t
58{
James Feista465ccc2019-02-08 12:51:01 -080059 const char* name() const noexcept override
James Feist68500ff2018-08-08 15:40:42 -070060 {
61 return "org.freedesktop.DBus.Error.Failed";
62 };
James Feista465ccc2019-02-08 12:51:01 -080063 const char* description() const noexcept override
James Feist68500ff2018-08-08 15:40:42 -070064 {
65 return "internal error";
66 };
James Feista465ccc2019-02-08 12:51:01 -080067 const char* what() const noexcept override
James Feist68500ff2018-08-08 15:40:42 -070068 {
69 return "org.freedesktop.DBus.Error.Failed: "
70 "internal error";
71 };
72};
James Feist1df06a42019-04-11 14:23:04 -070073
74inline bool fwVersionIsSame(void)
75{
76 std::ifstream version(versionFile);
77 if (!version.good())
78 {
79 std::cerr << "Can't read " << versionFile << "\n";
80 return false;
81 }
82
83 std::string versionData;
84 std::string line;
85 while (std::getline(version, line))
86 {
87 versionData += line;
88 }
89
90 std::string expectedHash =
91 std::to_string(std::hash<std::string>{}(versionData));
92
93 std::filesystem::create_directory(configurationOutDir);
94 std::ifstream hashFile(versionHashFile);
95 if (hashFile.good())
96 {
97
98 std::string hashString;
99 hashFile >> hashString;
100
101 if (expectedHash == hashString)
102 {
103 return true;
104 }
105 hashFile.close();
106 }
107
108 std::ofstream output(versionHashFile);
109 output << expectedHash;
110 return false;
James Feist481c5d52019-08-13 14:40:40 -0700111}
112
113void templateCharReplace(
114 nlohmann::json::iterator& keyPair,
115 const boost::container::flat_map<std::string, BasicVariantType>&
116 foundDevice,
117 const size_t foundDeviceIdx);