1 #include "data_interface.hpp"
2 
3 namespace openpower
4 {
5 namespace pels
6 {
7 
8 namespace service_name
9 {
10 constexpr auto objectMapper = "xyz.openbmc_project.ObjectMapper";
11 } // namespace service_name
12 
13 namespace object_path
14 {
15 constexpr auto objectMapper = "/xyz/openbmc_project/object_mapper";
16 constexpr auto systemInv = "/xyz/openbmc_project/inventory/system";
17 } // namespace object_path
18 
19 namespace interface
20 {
21 constexpr auto dbusProperty = "org.freedesktop.DBus.Properties";
22 constexpr auto objectMapper = "xyz.openbmc_project.ObjectMapper";
23 constexpr auto invAsset = "xyz.openbmc_project.Inventory.Decorator.Asset";
24 } // namespace interface
25 
26 DataInterface::DataInterface(sdbusplus::bus::bus& bus) : _bus(bus)
27 {
28     readMTMS();
29 }
30 
31 void DataInterface::readMTMS()
32 {
33     // If this runs when the inventory service isn't running, it will get the
34     // value whenever it starts via the propertiesChanged callback.
35     try
36     {
37         auto inventoryService =
38             getService(object_path::systemInv, interface::invAsset);
39 
40         if (!inventoryService.empty())
41         {
42             auto properties = getAllProperties(
43                 inventoryService, object_path::systemInv, interface::invAsset);
44 
45             _machineTypeModel = std::get<std::string>(properties["Model"]);
46 
47             _machineSerialNumber =
48                 std::get<std::string>(properties["SerialNumber"]);
49         }
50     }
51     catch (std::exception& e)
52     {
53         // Inventory must not be running at this moment.
54     }
55 
56     // Keep up to date by watching for the propertiesChanged signal.
57     _sysInventoryPropMatch = std::make_unique<sdbusplus::bus::match_t>(
58         _bus,
59         sdbusplus::bus::match::rules::propertiesChanged(object_path::systemInv,
60                                                         interface::invAsset),
61         std::bind(std::mem_fn(&DataInterface::sysAssetPropChanged), this,
62                   std::placeholders::_1));
63 }
64 
65 void DataInterface::sysAssetPropChanged(sdbusplus::message::message& msg)
66 {
67     DBusInterface interface;
68     DBusPropertyMap properties;
69 
70     msg.read(interface, properties);
71 
72     auto model = properties.find("Model");
73     if (model != properties.end())
74     {
75         _machineTypeModel = std::get<std::string>(model->second);
76     }
77 
78     auto sn = properties.find("SerialNumber");
79     if (sn != properties.end())
80     {
81         _machineSerialNumber = std::get<std::string>(sn->second);
82     }
83 }
84 
85 DBusPropertyMap DataInterface::getAllProperties(const std::string& service,
86                                                 const std::string& objectPath,
87                                                 const std::string& interface)
88 {
89     DBusPropertyMap properties;
90 
91     auto method = _bus.new_method_call(service.c_str(), objectPath.c_str(),
92                                        interface::dbusProperty, "GetAll");
93     method.append(interface);
94     auto reply = _bus.call(method);
95 
96     reply.read(properties);
97 
98     return properties;
99 }
100 
101 DBusService DataInterface::getService(const std::string& objectPath,
102                                       const std::string& interface)
103 {
104     auto method = _bus.new_method_call(service_name::objectMapper,
105                                        object_path::objectMapper,
106                                        interface::objectMapper, "GetObject");
107 
108     method.append(objectPath, std::vector<std::string>({interface}));
109 
110     auto reply = _bus.call(method);
111 
112     std::map<DBusService, DBusInterfaceList> response;
113     reply.read(response);
114 
115     if (!response.empty())
116     {
117         return response.begin()->first;
118     }
119 
120     return std::string{};
121 }
122 } // namespace pels
123 } // namespace openpower
124