blob: e57d14649e5635c96cb453ff00dea8e454ab1433 [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 Feist1df06a42019-04-11 14:23:04 -070018#include <boost/container/flat_map.hpp>
James Feist637b3ef2019-04-15 16:35:30 -070019#include <filesystem>
James Feist1df06a42019-04-11 14:23:04 -070020#include <fstream>
21#include <iostream>
James Feistb4383f42018-08-06 16:54:10 -070022#include <nlohmann/json.hpp>
James Feist1df06a42019-04-11 14:23:04 -070023#include <sdbusplus/asio/connection.hpp>
James Feist68500ff2018-08-08 15:40:42 -070024#include <sdbusplus/exception.hpp>
James Feist3cb5fec2018-01-23 14:41:51 -080025
James Feist1df06a42019-04-11 14:23:04 -070026constexpr const char* configurationOutDir = "/var/configuration/";
27constexpr const char* versionHashFile = "/var/configuration/version";
28constexpr const char* versionFile = "/etc/os-release";
29
James Feista465ccc2019-02-08 12:51:01 -080030bool findFiles(const std::filesystem::path& dirPath,
31 const std::string& matchString,
32 std::vector<std::filesystem::path>& foundPaths);
James Feistb4383f42018-08-06 16:54:10 -070033
Nikhil Potaded8884f12019-03-27 13:27:13 -070034bool getI2cDevicePaths(
35 const std::filesystem::path& dirPath,
36 boost::container::flat_map<size_t, std::filesystem::path>& busPaths);
37
James Feista465ccc2019-02-08 12:51:01 -080038bool validateJson(const nlohmann::json& schemaFile,
39 const nlohmann::json& input);
James Feist1df06a42019-04-11 14:23:04 -070040
41bool isPowerOn(void);
42void setupPowerMatch(const std::shared_ptr<sdbusplus::asio::connection>& conn);
James Feist68500ff2018-08-08 15:40:42 -070043struct DBusInternalError final : public sdbusplus::exception_t
44{
James Feista465ccc2019-02-08 12:51:01 -080045 const char* name() const noexcept override
James Feist68500ff2018-08-08 15:40:42 -070046 {
47 return "org.freedesktop.DBus.Error.Failed";
48 };
James Feista465ccc2019-02-08 12:51:01 -080049 const char* description() const noexcept override
James Feist68500ff2018-08-08 15:40:42 -070050 {
51 return "internal error";
52 };
James Feista465ccc2019-02-08 12:51:01 -080053 const char* what() const noexcept override
James Feist68500ff2018-08-08 15:40:42 -070054 {
55 return "org.freedesktop.DBus.Error.Failed: "
56 "internal error";
57 };
58};
James Feist1df06a42019-04-11 14:23:04 -070059
60inline bool fwVersionIsSame(void)
61{
62 std::ifstream version(versionFile);
63 if (!version.good())
64 {
65 std::cerr << "Can't read " << versionFile << "\n";
66 return false;
67 }
68
69 std::string versionData;
70 std::string line;
71 while (std::getline(version, line))
72 {
73 versionData += line;
74 }
75
76 std::string expectedHash =
77 std::to_string(std::hash<std::string>{}(versionData));
78
79 std::filesystem::create_directory(configurationOutDir);
80 std::ifstream hashFile(versionHashFile);
81 if (hashFile.good())
82 {
83
84 std::string hashString;
85 hashFile >> hashString;
86
87 if (expectedHash == hashString)
88 {
89 return true;
90 }
91 hashFile.close();
92 }
93
94 std::ofstream output(versionHashFile);
95 output << expectedHash;
96 return false;
97}