Andrei Kartashev | 50fe8ee | 2022-01-04 21:24:22 +0300 | [diff] [blame] | 1 | /* |
| 2 | * SPDX-License-Identifier: Apache-2.0 |
| 3 | * Copyright (C) 2021-2022 YADRO. |
| 4 | */ |
| 5 | |
| 6 | #pragma once |
| 7 | |
| 8 | #include <nlohmann/json.hpp> |
| 9 | |
| 10 | #include <filesystem> |
| 11 | #include <string_view> |
| 12 | |
| 13 | namespace power_control |
| 14 | { |
| 15 | |
| 16 | /** |
| 17 | * @brief Persistent State Manager |
| 18 | * |
| 19 | * This manager supposed to store runtime parameters that supposed to be |
| 20 | * persistent over BMC reboot. It provides simple Get/Set interface and handle |
| 21 | * default values, hardcoded in getDefault() method. |
| 22 | * @note: currently only string parameters supported |
| 23 | */ |
| 24 | class PersistentState |
| 25 | { |
| 26 | public: |
| 27 | /** |
| 28 | * List of all supported parameters |
| 29 | */ |
| 30 | enum class Params |
| 31 | { |
| 32 | PowerState, |
| 33 | }; |
| 34 | |
| 35 | /** |
| 36 | * @brief Persistent storage initialization |
| 37 | * |
| 38 | * Class constructor automatically load last state from JSON file |
| 39 | */ |
| 40 | PersistentState(); |
| 41 | /** |
| 42 | * @brief Persistent storage cleanup |
| 43 | * |
| 44 | * Class destructor automatically save state to JSON file |
| 45 | */ |
| 46 | ~PersistentState(); |
| 47 | /** |
| 48 | * @brief Get parameter value from the storage |
| 49 | * |
| 50 | * Get the parameter from cached storage. Default value returned, if |
| 51 | * parameter was not set before. |
| 52 | * @param parameter - parameter to get |
| 53 | * @return parameter value |
| 54 | */ |
| 55 | const std::string get(Params parameter); |
| 56 | /** |
| 57 | * @brief Store parameter value |
| 58 | * |
| 59 | * Set the parameter value in cached storage and dump it to disk. |
| 60 | * @param parameter - parameter to set |
| 61 | * @param value - parameter value to assign |
| 62 | */ |
| 63 | void set(Params parameter, const std::string& value); |
| 64 | |
| 65 | private: |
| 66 | nlohmann::json stateData; |
| 67 | const std::filesystem::path powerControlDir = "/var/lib/power-control"; |
| 68 | const std::string_view stateFile = "state.json"; |
| 69 | const int indentationSize = 2; |
| 70 | |
| 71 | /** |
| 72 | * @brief Covert parameter ID to name |
| 73 | * |
| 74 | * Get the name corresponding to the given parameter. |
| 75 | * String name only used by the manager internal to generate human-readable |
| 76 | * JSON. |
| 77 | * @param parameter - parameter to convert |
| 78 | * @return parameter name |
| 79 | */ |
| 80 | const std::string getName(const Params parameter); |
| 81 | /** |
| 82 | * @brief Get default parameter value |
| 83 | * |
| 84 | * Get the default value, associated with given parameter. |
| 85 | * @param parameter - parameter to get |
| 86 | * @return parameter default value |
| 87 | */ |
| 88 | const std::string getDefault(const Params parameter); |
| 89 | /** |
| 90 | * @brief Save cache to file on disk |
| 91 | */ |
| 92 | void saveState(); |
| 93 | }; |
| 94 | |
| 95 | } // namespace power_control |