xref: /openbmc/bios-settings-mgr/include/manager.hpp (revision 104b2131a9dec6409d6c014b9915e302b687ea55)
1 /*
2  Copyright (c) 2020 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
18 
19 #include <sdbusplus/asio/object_server.hpp>
20 #include <sdbusplus/server.hpp>
21 #include <xyz/openbmc_project/BIOSConfig/Manager/server.hpp>
22 
23 #include <filesystem>
24 #include <string>
25 
26 namespace bios_config
27 {
28 
29 static constexpr auto service = "xyz.openbmc_project.BIOSConfigManager";
30 static constexpr auto objectPath = "/xyz/openbmc_project/bios_config/manager";
31 constexpr auto biosPersistFile = "biosData";
32 
33 using Base = sdbusplus::xyz::openbmc_project::BIOSConfig::server::Manager;
34 namespace fs = std::filesystem;
35 
36 /** @class Manager
37  *
38  *  @brief Implements the BIOS Manager
39  */
40 class Manager : public Base
41 {
42   public:
43     using BaseTable = std::map<
44         std::string,
45         std::tuple<
46             AttributeType, bool, std::string, std::string, std::string,
47             std::variant<int64_t, std::string>,
48             std::variant<int64_t, std::string>,
49             std::vector<std::tuple<
50                 BoundType, std::variant<int64_t, std::string>, std::string>>>>;
51 
52     using ResetFlag = std::map<std::string, ResetFlag>;
53 
54     using PendingAttributes =
55         std::map<std::string,
56                  std::tuple<AttributeType, std::variant<int64_t, std::string>>>;
57 
58     using PendingAttribute =
59         std::tuple<AttributeType, std::variant<int64_t, std::string>>;
60 
61     using AttributeName = std::string;
62     using AttributeValue = std::variant<int64_t, std::string>;
63     using CurrentValue = std::variant<int64_t, std::string>;
64     using PendingValue = std::variant<int64_t, std::string>;
65     using AttributeDetails =
66         std::tuple<AttributeType, CurrentValue, PendingValue>;
67     using Base::resetBIOSSettings;
68 
69     Manager() = delete;
70     ~Manager() = default;
71     Manager(const Manager&) = delete;
72     Manager& operator=(const Manager&) = delete;
73     Manager(Manager&&) = delete;
74     Manager& operator=(Manager&&) = delete;
75 
76     /** @brief Constructs Manager object.
77      *
78      *  @param[in] objectServer  - object server
79      *  @param[in] systemBus - bus connection
80      */
81     Manager(sdbusplus::asio::object_server& objectServer,
82             std::shared_ptr<sdbusplus::asio::connection>& systemBus,
83             std::string persistPath);
84 
85     /** @brief Set the BIOS attribute with a new value, the new value is added
86      *         to the PendingAttribute.
87      *
88      *  @param[in] attribute - attribute name
89      *  @param[in] value - new value for the attribute
90      *
91      *  @return On error, throw exception
92      */
93     void setAttribute(AttributeName attribute, AttributeValue value) override;
94 
95     /** @brief Get the details of the BIOS attribute
96      *
97      *  @param[in] attribute - attribute name
98      *
99      *  @return On success, return the attribute details: attribute type,
100      *          current value, pending value. On error, throw exception
101      */
102     AttributeDetails getAttribute(AttributeName attribute) override;
103 
104     /** @brief Set the BaseBIOSTable property and clears the PendingAttributes
105      *         property
106      *
107      *  @param[in] value - new BaseBIOSTable
108      *
109      *  @return The new BaseBIOSTable that is applied.
110      */
111     BaseTable baseBIOSTable(BaseTable value) override;
112 
113     ResetFlag resetBIOSSettings(ResetFlag value);
114 
115     /** @brief Set the PendingAttributes property, additionally checks if the
116      *         attributes are in the BaseBIOSTable, whether the attributes are
117      *         read only and validate the attribute value based on the
118      *         attribute type. PendingAttributes is cleared if value is empty.
119      *
120      *  @param[in] value - new PendingAttributes to append to the
121      *                     PendingAttributes property
122      *
123      *  @return On success, return the new PendingAttributes property that is
124      *          set.Throw exception if the validation fails.
125      */
126     PendingAttributes pendingAttributes(PendingAttributes value) override;
127 
128   private:
129     /** @enum Index into the fields in the BaseBIOSTable
130      */
131     enum class Index : uint8_t
132     {
133         attributeType = 0,
134         readOnly,
135         displayName,
136         description,
137         menuPath,
138         currentValue,
139         defaultValue,
140         options,
141     };
142 
143     bool validateEnumOption(
144         const std::string& attrValue,
145         const std::vector<std::tuple<
146             BoundType, std::variant<int64_t, std::string>, std::string>>&
147             options);
148 
149     bool validateStringOption(
150         const std::string& attrValue,
151         const std::vector<std::tuple<
152             BoundType, std::variant<int64_t, std::string>, std::string>>&
153             options);
154 
155     bool validateIntegerOption(
156         const int64_t& attrValue,
157         const std::vector<std::tuple<
158             BoundType, std::variant<int64_t, std::string>, std::string>>&
159             options);
160 
161     sdbusplus::asio::object_server& objServer;
162     std::shared_ptr<sdbusplus::asio::connection>& systemBus;
163     std::filesystem::path biosFile;
164 };
165 
166 } // namespace bios_config
167