1 ## This file is a template.  The comment below is emitted
2 ## into the rendered file; feel free to edit this file.
3 // WARNING: Generated header. Do not edit!
4 
5 
6 #pragma once
7 
8 #include <map>
9 #include <iostream>
10 #include <sdbusplus/server.hpp>
11 #include <log.hpp>
12 #include "defines.hpp"
13 #include "store.hpp"
14 
15 namespace openpower
16 {
17 namespace vpd
18 {
19 namespace inventory
20 {
21 
22 using Property = std::string;
23 using Value = sdbusplus::message::variant<bool, int64_t, std::string>;
24 using PropertyMap = std::map<Property, Value>;
25 
26 using Interface = std::string;
27 using InterfaceMap = std::map<Interface, PropertyMap>;
28 
29 using Object = sdbusplus::message::object_path;
30 using ObjectMap = std::map<Object, InterfaceMap>;
31 
32 using namespace std::string_literals;
33 static const auto pimPath = "/xyz/openbmc_project/Inventory"s;
34 static const auto pimIntf = "xyz.openbmc_project.Inventory.Manager"s;
35 
36 /** @brief Get inventory-manager's d-bus service
37  */
38 auto getPIMService()
39 {
40     auto bus = sdbusplus::bus::new_default();
41     auto mapper =
42         bus.new_method_call(
43             "xyz.openbmc_project.ObjectMapper",
44             "/xyz/openbmc_project/ObjectMapper",
45             "xyz.openbmc_project.ObjectMapper",
46             "GetObject");
47 
48     mapper.append(pimPath);
49     mapper.append(std::vector<std::string>({pimIntf}));
50 
51     auto result = bus.call(mapper);
52     if(result.is_method_error())
53     {
54         throw std::runtime_error("ObjectMapper GetObject failed");
55     }
56 
57     std::map<std::string, std::vector<std::string>> response;
58     result.read(response);
59     if(response.empty())
60     {
61         throw std::runtime_error("ObjectMapper GetObject bad response");
62     }
63 
64     return response.begin()->first;
65 }
66 
67 auto callPIM(ObjectMap&& objects)
68 {
69     std::string service;
70 
71     try
72     {
73         service = getPIMService();
74         auto bus = sdbusplus::bus::new_default();
75         auto pimMsg = bus.new_method_call(
76                               service.c_str(),
77                               pimPath.c_str(),
78                               pimIntf.c_str(),
79                               "Notify");
80         pimMsg.append(std::move(objects));
81         auto result = bus.call(pimMsg);
82         if(result.is_method_error())
83         {
84             std::cerr << "PIM Notify() failed\n";
85         }
86     }
87     catch (const std::runtime_error& e)
88     {
89         using namespace phosphor::logging;
90         log<level::ERR>(e.what());
91     }
92 }
93 
94 /** @brief API to write parsed VPD to inventory,
95  *      for a specifc FRU
96  *
97  *  @param [in] vpdStore - Store object containing
98  *      parsed VPD
99  *  @param [in] path - FRU object path
100  */
101 template<Fru F>
102 void writeFru(const Store& vpdStore, const std::string& path);
103 
104 % for key in fruDict.iterkeys():
105 <%
106     fru = fruDict[key]
107 %>\
108 // Specialization of ${key}
109 template<>
110 void writeFru<Fru::${key}>(const Store& vpdStore,
111                            const std::string& path)
112 {
113     ObjectMap objects;
114     InterfaceMap interfaces;
115 
116     // Inventory manager needs object path, list of interface names to be
117     // implemented, and property:value pairs contained in said interfaces
118 
119     % for interface, properties in fru.iteritems():
120 <%
121         names = interface.split(".")
122         intfName = names[0] + names[-1]
123 %>\
124     PropertyMap ${intfName}Props;
125         % for name, value in properties.iteritems():
126             % if fru and interface and name and value:
127 <%
128                 record, keyword = value.split(",")
129 %>\
130     ${intfName}Props["${name}"] =
131         vpdStore.get<Record::${record}, record::Keyword::${keyword}>();
132             % endif
133         % endfor
134     interfaces.emplace("${interface}",
135                        std::move(${intfName}Props));
136     % endfor
137 
138     sdbusplus::message::object_path object(path);
139     objects.emplace(std::move(object), std::move(interfaces));
140 
141     callPIM(std::move(objects));
142 }
143 
144 % endfor
145 } // namespace inventory
146 } // namespace vpd
147 } // namespace openpower
148