1 #pragma once
2 
3 #include <filesystem>
4 #include <map>
5 #include <set>
6 #include <string>
7 #include <tuple>
8 #include <vector>
9 
10 namespace pldm
11 {
12 
13 namespace responder
14 {
15 
16 namespace dbus
17 {
18 
19 using Service = std::string;
20 using RootPath = std::string;
21 using Interface = std::string;
22 using Interfaces = std::set<Interface>;
23 using Property = std::string;
24 using PropertyType = std::string;
25 using EntityType = uint8_t;
26 
27 } // namespace dbus
28 
29 namespace fru
30 {
31 
32 using RecordType = uint8_t;
33 using EncodingType = uint8_t;
34 using FieldType = uint8_t;
35 
36 } // namespace fru
37 
38 namespace fru_parser
39 {
40 
41 namespace fs = std::filesystem;
42 using namespace dbus;
43 using namespace fru;
44 
45 // DBusLookupInfo contains info to lookup in the D-Bus inventory, D-Bus
46 // inventory service bus name, root path of the inventory D-Bus objects and list
47 //  of xyz.openbmc_project.Inventory.Item.* interface names.
48 using DBusLookupInfo = std::tuple<Service, RootPath, Interfaces>;
49 using FieldInfo = std::tuple<Interface, Property, PropertyType, FieldType>;
50 
51 using FruRecordInfo =
52     std::tuple<RecordType, EncodingType, std::vector<FieldInfo>>;
53 using FruRecordInfos = std::vector<FruRecordInfo>;
54 
55 using ItemIntfName = std::string;
56 using FruRecordMap = std::map<ItemIntfName, FruRecordInfos>;
57 
58 /** @class FruParser
59  *
60  *  @brief Parses the PLDM FRU configuration files to populate the data
61  *         structure, containing the information needed to map the D-Bus
62  *         inventory information into PLDM FRU Record.
63  */
64 class FruParser
65 {
66 
67   public:
68     FruParser() = delete;
69     explicit FruParser(const std::string& dirPath);
70     virtual ~FruParser() = default;
71     FruParser(const FruParser&) = default;
72     FruParser& operator=(const FruParser&) = default;
73     FruParser(FruParser&&) = default;
74     FruParser& operator=(FruParser&&) = default;
75 
76     /** @brief Provides the service, root D-Bus path and the interfaces that is
77      *         needed to build FRU record data table
78      *
79      *  @return service and inventory interfaces needed to build the FRU records
80      */
81     const DBusLookupInfo& inventoryLookup() const
82     {
83         return lookupInfo.value();
84     }
85 
86     /** @brief Get the information need to create PLDM FRU records for a
87      * inventory item type. The parameter to this API is the inventory item
88      * type, for example xyz.openbmc_project.Inventory.Item.Cpu for CPU's
89      *
90      *  @param[in] intf - name of the item interface
91      *
92      *  @return return the info create the PLDM FRU records from inventory D-Bus
93      *          objects
94      */
95     const FruRecordInfos& getRecordInfo(const Interface& intf) const
96     {
97         return recordMap.at(intf);
98     }
99 
100     EntityType getEntityType(const Interface& intf) const
101     {
102         return intfToEntityType.at(intf);
103     }
104 
105   private:
106     /** @brief Parse the FRU_Master.json file and populate the D-Bus lookup
107      *         information which provides the service, root D-Bus path and the
108      *         item interfaces.
109      *
110      *  @param[in] filePath - file path to FRU_Master.json
111      */
112     void setupDBusLookup(const fs::path& filePath);
113 
114     /** @brief Parse the FRU Configuration JSON file in the directory path
115      *         except the FRU_Master.json and build the FRU record information
116      *
117      *  @param[in] dirPath - directory path where all the FRU configuration JSON
118      *                       files exist
119      */
120     void setupFruRecordMap(const std::string& dirPath);
121 
122     std::optional<DBusLookupInfo> lookupInfo;
123     FruRecordMap recordMap;
124     std::map<Interface, EntityType> intfToEntityType;
125 };
126 
127 } // namespace fru_parser
128 
129 } // namespace responder
130 
131 } // namespace pldm
132