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::vector<std::string> valueDisplayNames;
73     std::string defaultValue;
74 
75     /** @brief Get index of the given value in possible values
76      *  @param[in] value - The given value
77      *  @param[in] pVs - The possible values
78      *  @return Index of the given value in possible values
79      */
80     uint8_t getValueIndex(const std::string& value,
81                           const std::vector<std::string>& pVs);
82 
83     /** @brief Get handles of possible values
84      *  @param[in] stringTable - The bios string table
85      *  @param[in] pVs - The possible values
86      *  @return The handles
87      */
88     std::vector<uint16_t>
89         getPossibleValuesHandle(const BIOSStringTable& stringTable,
90                                 const std::vector<std::string>& pVs);
91 
92     /** @brief Method to populate the valueDisplayNamesMap
93      *  @param[in] attrHandle - attribute handle
94      */
95     void populateValueDisplayNamesMap(uint16_t attrHandle);
96 
97     using ValMap = std::map<pldm::utils::PropertyValue, std::string>;
98 
99     /** @brief Map of value on dbus and pldm */
100     ValMap valMap;
101 
102     /** @brief Build the map of dbus value to pldm enum value
103      *  @param[in] dbusVals - The dbus values in the json's dbus section
104      */
105     void buildValMap(const Json& dbusVals);
106 
107     /** @brief Get index of the current value in possible values
108      *  @return The index of the current value in possible values
109      */
110     uint8_t getAttrValueIndex();
111 
112     /** @brief Get index of the property value in possible values
113      *  @param[in] propValue - property values
114      *
115      *  @return The index of the property value in possible values
116      */
117     uint8_t getAttrValueIndex(const pldm::utils::PropertyValue& propValue);
118 };
119 
120 } // namespace bios
121 } // namespace responder
122 } // namespace pldm
123