xref: /openbmc/bios-settings-mgr/include/manager.hpp (revision 1a448ad88fdaec7e082b4a1c437f7f3c990402cd)
1642f437eSKuiying Wang /*
2642f437eSKuiying Wang // Copyright (c) 2020 Intel Corporation
3642f437eSKuiying Wang //
4642f437eSKuiying Wang // Licensed under the Apache License, Version 2.0 (the "License");
5642f437eSKuiying Wang // you may not use this file except in compliance with the License.
6642f437eSKuiying Wang // You may obtain a copy of the License at
7642f437eSKuiying Wang //
8642f437eSKuiying Wang //      http://www.apache.org/licenses/LICENSE-2.0
9642f437eSKuiying Wang //
10642f437eSKuiying Wang // Unless required by applicable law or agreed to in writing, software
11642f437eSKuiying Wang // distributed under the License is distributed on an "AS IS" BASIS,
12642f437eSKuiying Wang // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13642f437eSKuiying Wang // See the License for the specific language governing permissions and
14642f437eSKuiying Wang // limitations under the License.
15642f437eSKuiying Wang */
16642f437eSKuiying Wang #pragma once
17642f437eSKuiying Wang 
18f1101df2STom Joseph #include "config.h"
19f1101df2STom Joseph 
20642f437eSKuiying Wang #include <sdbusplus/asio/object_server.hpp>
21642f437eSKuiying Wang #include <sdbusplus/server.hpp>
22642f437eSKuiying Wang #include <xyz/openbmc_project/BIOSConfig/Manager/server.hpp>
23642f437eSKuiying Wang 
24f1101df2STom Joseph #include <filesystem>
25642f437eSKuiying Wang #include <string>
26642f437eSKuiying Wang 
27642f437eSKuiying Wang namespace bios_config
28642f437eSKuiying Wang {
29642f437eSKuiying Wang 
30642f437eSKuiying Wang static constexpr auto service = "xyz.openbmc_project.BIOSConfigManager";
31642f437eSKuiying Wang static constexpr auto objectPath = "/xyz/openbmc_project/bios_config/manager";
32f1101df2STom Joseph constexpr auto biosPersistFile = "biosData";
33642f437eSKuiying Wang 
34642f437eSKuiying Wang using Base = sdbusplus::xyz::openbmc_project::BIOSConfig::server::Manager;
35f1101df2STom Joseph namespace fs = std::filesystem;
36642f437eSKuiying Wang 
37642f437eSKuiying Wang /** @class Manager
38642f437eSKuiying Wang  *
39642f437eSKuiying Wang  *  @brief Implements the BIOS Manager
40642f437eSKuiying Wang  */
41642f437eSKuiying Wang class Manager : public Base
42642f437eSKuiying Wang {
43642f437eSKuiying Wang   public:
44642f437eSKuiying Wang     using BaseTable = std::map<
45642f437eSKuiying Wang         std::string,
46*1a448ad8SArun Lal K M         std::tuple<
47*1a448ad8SArun Lal K M             AttributeType, bool, std::string, std::string, std::string,
48642f437eSKuiying Wang             std::variant<int64_t, std::string>,
49642f437eSKuiying Wang             std::variant<int64_t, std::string>,
50642f437eSKuiying Wang             std::vector<std::tuple<
51*1a448ad8SArun Lal K M                 BoundType, std::variant<int64_t, std::string>, std::string>>>>;
52642f437eSKuiying Wang 
535e2cb720SSnehalatha Venkatesh     using ResetFlag = std::map<std::string, ResetFlag>;
545e2cb720SSnehalatha Venkatesh 
55642f437eSKuiying Wang     using PendingAttributes =
56642f437eSKuiying Wang         std::map<std::string,
57642f437eSKuiying Wang                  std::tuple<AttributeType, std::variant<int64_t, std::string>>>;
58642f437eSKuiying Wang 
59642f437eSKuiying Wang     using PendingAttribute =
60642f437eSKuiying Wang         std::tuple<AttributeType, std::variant<int64_t, std::string>>;
61642f437eSKuiying Wang 
62642f437eSKuiying Wang     using AttributeName = std::string;
63642f437eSKuiying Wang     using AttributeValue = std::variant<int64_t, std::string>;
64642f437eSKuiying Wang     using CurrentValue = std::variant<int64_t, std::string>;
65642f437eSKuiying Wang     using PendingValue = std::variant<int64_t, std::string>;
66642f437eSKuiying Wang     using AttributeDetails =
67642f437eSKuiying Wang         std::tuple<AttributeType, CurrentValue, PendingValue>;
68642f437eSKuiying Wang 
69642f437eSKuiying Wang     Manager() = delete;
70642f437eSKuiying Wang     ~Manager() = default;
71642f437eSKuiying Wang     Manager(const Manager&) = delete;
72642f437eSKuiying Wang     Manager& operator=(const Manager&) = delete;
73642f437eSKuiying Wang     Manager(Manager&&) = delete;
74642f437eSKuiying Wang     Manager& operator=(Manager&&) = delete;
75642f437eSKuiying Wang 
76642f437eSKuiying Wang     /** @brief Constructs Manager object.
77642f437eSKuiying Wang      *
78642f437eSKuiying Wang      *  @param[in] objectServer  - object server
79642f437eSKuiying Wang      *  @param[in] systemBus - bus connection
80642f437eSKuiying Wang      */
81642f437eSKuiying Wang     Manager(sdbusplus::asio::object_server& objectServer,
82642f437eSKuiying Wang             std::shared_ptr<sdbusplus::asio::connection>& systemBus);
83642f437eSKuiying Wang 
84642f437eSKuiying Wang     /** @brief Set the BIOS attribute with a new value, the new value is added
85642f437eSKuiying Wang      *         to the PendingAttribute.
86642f437eSKuiying Wang      *
87642f437eSKuiying Wang      *  @param[in] attribute - attribute name
88642f437eSKuiying Wang      *  @param[in] value - new value for the attribute
89642f437eSKuiying Wang      *
90642f437eSKuiying Wang      *  @return On error, throw exception
91642f437eSKuiying Wang      */
92642f437eSKuiying Wang     void setAttribute(AttributeName attribute, AttributeValue value) override;
93642f437eSKuiying Wang 
94642f437eSKuiying Wang     /** @brief Get the details of the BIOS attribute
95642f437eSKuiying Wang      *
96642f437eSKuiying Wang      *  @param[in] attribute - attribute name
97642f437eSKuiying Wang      *
98642f437eSKuiying Wang      *  @return On success, return the attribute details: attribute type,
99642f437eSKuiying Wang      *          current value, pending value. On error, throw exception
100642f437eSKuiying Wang      */
101642f437eSKuiying Wang     AttributeDetails getAttribute(AttributeName attribute) override;
102642f437eSKuiying Wang 
103642f437eSKuiying Wang     /** @brief Set the BaseBIOSTable property and clears the PendingAttributes
104642f437eSKuiying Wang      *         property
105642f437eSKuiying Wang      *
106642f437eSKuiying Wang      *  @param[in] value - new BaseBIOSTable
107642f437eSKuiying Wang      *
108642f437eSKuiying Wang      *  @return The new BaseBIOSTable that is applied.
109642f437eSKuiying Wang      */
110642f437eSKuiying Wang     BaseTable baseBIOSTable(BaseTable value) override;
111642f437eSKuiying Wang 
1125e2cb720SSnehalatha Venkatesh     ResetFlag resetBIOSSettings(ResetFlag value);
1135e2cb720SSnehalatha Venkatesh 
114642f437eSKuiying Wang     /** @brief Set the PendingAttributes property, additionally checks if the
115642f437eSKuiying Wang      *         attributes are in the BaseBIOSTable, whether the attributes are
116642f437eSKuiying Wang      *         read only and validate the attribute value based on the
117642f437eSKuiying Wang      *         attribute type. PendingAttributes is cleared if value is empty.
118642f437eSKuiying Wang      *
119642f437eSKuiying Wang      *  @param[in] value - new PendingAttributes to append to the
120642f437eSKuiying Wang      *                     PendingAttributes property
121642f437eSKuiying Wang      *
122642f437eSKuiying Wang      *  @return On success, return the new PendingAttributes property that is
123642f437eSKuiying Wang      *          set.Throw exception if the validation fails.
124642f437eSKuiying Wang      */
125642f437eSKuiying Wang     PendingAttributes pendingAttributes(PendingAttributes value) override;
126642f437eSKuiying Wang 
127642f437eSKuiying Wang   private:
128642f437eSKuiying Wang     /** @enum Index into the fields in the BaseBIOSTable
129642f437eSKuiying Wang      */
130642f437eSKuiying Wang     enum class Index : uint8_t
131642f437eSKuiying Wang     {
132642f437eSKuiying Wang         attributeType = 0,
133642f437eSKuiying Wang         readOnly,
134642f437eSKuiying Wang         displayName,
135642f437eSKuiying Wang         description,
136642f437eSKuiying Wang         menuPath,
137642f437eSKuiying Wang         currentValue,
138642f437eSKuiying Wang         defaultValue,
139642f437eSKuiying Wang         options,
140642f437eSKuiying Wang     };
141642f437eSKuiying Wang 
142ad54c7cbSyes     bool validateEnumOption(
143ad54c7cbSyes         const std::string& attrValue,
144*1a448ad8SArun Lal K M         const std::vector<std::tuple<
145*1a448ad8SArun Lal K M             BoundType, std::variant<int64_t, std::string>, std::string>>&
146ad54c7cbSyes             options);
147ad54c7cbSyes 
148ad54c7cbSyes     bool validateStringOption(
149ad54c7cbSyes         const std::string& attrValue,
150*1a448ad8SArun Lal K M         const std::vector<std::tuple<
151*1a448ad8SArun Lal K M             BoundType, std::variant<int64_t, std::string>, std::string>>&
152ad54c7cbSyes             options);
153ad54c7cbSyes 
154ad54c7cbSyes     bool validateIntegerOption(
155ad54c7cbSyes         const int64_t& attrValue,
156*1a448ad8SArun Lal K M         const std::vector<std::tuple<
157*1a448ad8SArun Lal K M             BoundType, std::variant<int64_t, std::string>, std::string>>&
158ad54c7cbSyes             options);
159ad54c7cbSyes 
160642f437eSKuiying Wang     sdbusplus::asio::object_server& objServer;
161642f437eSKuiying Wang     std::shared_ptr<sdbusplus::asio::connection>& systemBus;
162f1101df2STom Joseph     std::filesystem::path biosFile;
163642f437eSKuiying Wang };
164642f437eSKuiying Wang 
165642f437eSKuiying Wang } // namespace bios_config
166