James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 1 | /* |
| 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 Feist | 481c5d5 | 2019-08-13 14:40:40 -0700 | [diff] [blame] | 18 | |
James Feist | 1df06a4 | 2019-04-11 14:23:04 -0700 | [diff] [blame] | 19 | #include <boost/container/flat_map.hpp> |
James Feist | 637b3ef | 2019-04-15 16:35:30 -0700 | [diff] [blame] | 20 | #include <filesystem> |
James Feist | 1df06a4 | 2019-04-11 14:23:04 -0700 | [diff] [blame] | 21 | #include <fstream> |
| 22 | #include <iostream> |
James Feist | b4383f4 | 2018-08-06 16:54:10 -0700 | [diff] [blame] | 23 | #include <nlohmann/json.hpp> |
James Feist | 1df06a4 | 2019-04-11 14:23:04 -0700 | [diff] [blame] | 24 | #include <sdbusplus/asio/connection.hpp> |
James Feist | 68500ff | 2018-08-08 15:40:42 -0700 | [diff] [blame] | 25 | #include <sdbusplus/exception.hpp> |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 26 | |
James Feist | 1df06a4 | 2019-04-11 14:23:04 -0700 | [diff] [blame] | 27 | constexpr const char* configurationOutDir = "/var/configuration/"; |
| 28 | constexpr const char* versionHashFile = "/var/configuration/version"; |
| 29 | constexpr const char* versionFile = "/etc/os-release"; |
| 30 | |
James Feist | 481c5d5 | 2019-08-13 14:40:40 -0700 | [diff] [blame] | 31 | using BasicVariantType = |
| 32 | std::variant<std::string, int64_t, uint64_t, double, int32_t, uint32_t, |
| 33 | int16_t, uint16_t, uint8_t, bool>; |
| 34 | |
| 35 | enum class TemplateOperation |
| 36 | { |
| 37 | addition, |
| 38 | division, |
| 39 | multiplication, |
| 40 | subtraction, |
| 41 | modulo, |
| 42 | }; |
| 43 | |
James Feist | 61f5ac4 | 2020-03-11 14:37:25 -0700 | [diff] [blame^] | 44 | namespace properties |
| 45 | { |
| 46 | constexpr const char* interface = "org.freedesktop.DBus.Properties"; |
| 47 | constexpr const char* get = "Get"; |
| 48 | } // namespace properties |
| 49 | |
| 50 | namespace power |
| 51 | { |
| 52 | const static constexpr char* busname = "xyz.openbmc_project.State.Host"; |
| 53 | const static constexpr char* interface = "xyz.openbmc_project.State.Host"; |
| 54 | const static constexpr char* path = "/xyz/openbmc_project/state/host0"; |
| 55 | const static constexpr char* property = "CurrentHostState"; |
| 56 | } // namespace power |
| 57 | |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 58 | bool findFiles(const std::filesystem::path& dirPath, |
| 59 | const std::string& matchString, |
| 60 | std::vector<std::filesystem::path>& foundPaths); |
James Feist | b4383f4 | 2018-08-06 16:54:10 -0700 | [diff] [blame] | 61 | |
Nikhil Potade | d8884f1 | 2019-03-27 13:27:13 -0700 | [diff] [blame] | 62 | bool getI2cDevicePaths( |
| 63 | const std::filesystem::path& dirPath, |
| 64 | boost::container::flat_map<size_t, std::filesystem::path>& busPaths); |
| 65 | |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 66 | bool validateJson(const nlohmann::json& schemaFile, |
| 67 | const nlohmann::json& input); |
James Feist | 1df06a4 | 2019-04-11 14:23:04 -0700 | [diff] [blame] | 68 | |
| 69 | bool isPowerOn(void); |
| 70 | void setupPowerMatch(const std::shared_ptr<sdbusplus::asio::connection>& conn); |
James Feist | 68500ff | 2018-08-08 15:40:42 -0700 | [diff] [blame] | 71 | struct DBusInternalError final : public sdbusplus::exception_t |
| 72 | { |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 73 | const char* name() const noexcept override |
James Feist | 68500ff | 2018-08-08 15:40:42 -0700 | [diff] [blame] | 74 | { |
| 75 | return "org.freedesktop.DBus.Error.Failed"; |
| 76 | }; |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 77 | const char* description() const noexcept override |
James Feist | 68500ff | 2018-08-08 15:40:42 -0700 | [diff] [blame] | 78 | { |
| 79 | return "internal error"; |
| 80 | }; |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 81 | const char* what() const noexcept override |
James Feist | 68500ff | 2018-08-08 15:40:42 -0700 | [diff] [blame] | 82 | { |
| 83 | return "org.freedesktop.DBus.Error.Failed: " |
| 84 | "internal error"; |
| 85 | }; |
| 86 | }; |
James Feist | 1df06a4 | 2019-04-11 14:23:04 -0700 | [diff] [blame] | 87 | |
| 88 | inline 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 Feist | 481c5d5 | 2019-08-13 14:40:40 -0700 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | void templateCharReplace( |
| 128 | nlohmann::json::iterator& keyPair, |
| 129 | const boost::container::flat_map<std::string, BasicVariantType>& |
| 130 | foundDevice, |
| 131 | const size_t foundDeviceIdx); |