1 #pragma once
2 
3 #include "bios_attribute.hpp"
4 
5 #include <map>
6 #include <set>
7 #include <string>
8 #include <variant>
9 
10 class TestBIOSEnumAttribute;
11 
12 namespace pldm
13 {
14 namespace responder
15 {
16 namespace bios
17 {
18 
19 /** @class BIOSEnumAttribute
20  *  @brief Associate enum entry(attr table and attribute value table) and
21  *         dbus attribute
22  */
23 class BIOSEnumAttribute : public BIOSAttribute
24 {
25   public:
26     friend class ::TestBIOSEnumAttribute;
27     /** @brief Construct a bios enum attribute
28      *  @param[in] entry - Json Object
29      *  @param[in] dbusHandler - Dbus Handler
30      */
31     BIOSEnumAttribute(const Json& entry,
32                       pldm::utils::DBusHandler* const dbusHandler);
33 
34     /** @brief Set Attribute value On Dbus according to the attribute value
35      *         entry
36      *  @param[in] attrValueEntry - The attribute value entry
37      *  @param[in] attrEntry - The attribute entry corresponding to the
38      *                         attribute value entry
39      *  @param[in] stringTable - The string table
40      */
41     void
42         setAttrValueOnDbus(const pldm_bios_attr_val_table_entry* attrValueEntry,
43                            const pldm_bios_attr_table_entry* attrEntry,
44                            const BIOSStringTable& stringTable) override;
45 
46     /** @brief Construct corresponding entries at the end of the attribute table
47      *         and attribute value tables
48      *  @param[in] stringTable - The string Table
49      *  @param[in,out] attrTable - The attribute table
50      *  @param[in,out] attrValueTable - The attribute value table
51      *  @param[in,out] optAttributeValue - init value of the attribute
52      */
53     void constructEntry(const BIOSStringTable& stringTable, Table& attrTable,
54                         Table& attrValueTable,
55                         std::optional<std::variant<int64_t, std::string>>
56                             optAttributeValue = std::nullopt) override;
57 
58     /** @brief Generate attribute entry by the spec DSP0247_1.0.0 Table 14
59      *  @param[in] attributevalue - attribute value(Enumeration, String and
60      *             Integer)
61      *  @param[in,out] attrValueEntry - attribute entry
62      */
63     void generateAttributeEntry(
64         const std::variant<int64_t, std::string>& attributevalue,
65         Table& attrValueEntry) override;
66 
67     int updateAttrVal(Table& newValue, uint16_t attrHdl, uint8_t attrType,
68                       const pldm::utils::PropertyValue& newPropVal) override;
69 
70   private:
71     std::vector<std::string> possibleValues;
72     std::string defaultValue;
73 
74     /** @brief Get index of the given value in possible values
75      *  @param[in] value - The given value
76      *  @param[in] pVs - The possible values
77      *  @return Index of the given value in possible values
78      */
79     uint8_t getValueIndex(const std::string& value,
80                           const std::vector<std::string>& pVs);
81 
82     /** @brief Get handles of possible values
83      *  @param[in] stringTable - The bios string table
84      *  @param[in] pVs - The possible values
85      *  @return The handles
86      */
87     std::vector<uint16_t>
88         getPossibleValuesHandle(const BIOSStringTable& stringTable,
89                                 const std::vector<std::string>& pVs);
90 
91     using ValMap = std::map<pldm::utils::PropertyValue, std::string>;
92 
93     /** @brief Map of value on dbus and pldm */
94     ValMap valMap;
95 
96     /** @brief Build the map of dbus value to pldm enum value
97      *  @param[in] dbusVals - The dbus values in the json's dbus section
98      */
99     void buildValMap(const Json& dbusVals);
100 
101     /** @brief Get index of the current value in possible values
102      *  @return The index of the current value in possible values
103      */
104     uint8_t getAttrValueIndex();
105 
106     /** @brief Get index of the property value in possible values
107      *  @param[in] propValue - property values
108      *
109      *  @return The index of the property value in possible values
110      */
111     uint8_t getAttrValueIndex(const pldm::utils::PropertyValue& propValue);
112 };
113 
114 } // namespace bios
115 } // namespace responder
116 } // namespace pldm
117