blob: 66442c53767a0c9f3d748e2fccbf747d1fc226e2 [file] [log] [blame]
Matt Spinler711d51d2019-11-06 09:36:51 -06001/**
2 * Copyright © 2019 IBM Corporation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Matt Spinlerc8705e22019-09-11 12:36:07 -050016#include "data_interface.hpp"
17
Matt Spinlera7d9d962019-11-06 15:01:25 -060018#include <xyz/openbmc_project/State/OperatingSystem/Status/server.hpp>
19
Matt Spinlerc8705e22019-09-11 12:36:07 -050020namespace openpower
21{
22namespace pels
23{
24
25namespace service_name
26{
27constexpr auto objectMapper = "xyz.openbmc_project.ObjectMapper";
28} // namespace service_name
29
30namespace object_path
31{
32constexpr auto objectMapper = "/xyz/openbmc_project/object_mapper";
33constexpr auto systemInv = "/xyz/openbmc_project/inventory/system";
Matt Spinlera7d9d962019-11-06 15:01:25 -060034constexpr auto hostState = "/xyz/openbmc_project/state/host0";
Matt Spinlerc8705e22019-09-11 12:36:07 -050035} // namespace object_path
36
37namespace interface
38{
39constexpr auto dbusProperty = "org.freedesktop.DBus.Properties";
40constexpr auto objectMapper = "xyz.openbmc_project.ObjectMapper";
41constexpr auto invAsset = "xyz.openbmc_project.Inventory.Decorator.Asset";
Matt Spinlera7d9d962019-11-06 15:01:25 -060042constexpr auto osStatus = "xyz.openbmc_project.State.OperatingSystem.Status";
Matt Spinlerc8705e22019-09-11 12:36:07 -050043} // namespace interface
44
Matt Spinlera7d9d962019-11-06 15:01:25 -060045using namespace sdbusplus::xyz::openbmc_project::State::OperatingSystem::server;
46
Matt Spinlerc8705e22019-09-11 12:36:07 -050047DataInterface::DataInterface(sdbusplus::bus::bus& bus) : _bus(bus)
48{
49 readMTMS();
Matt Spinlera7d9d962019-11-06 15:01:25 -060050 readHostState();
Matt Spinlerc8705e22019-09-11 12:36:07 -050051}
52
53void DataInterface::readMTMS()
54{
55 // If this runs when the inventory service isn't running, it will get the
56 // value whenever it starts via the propertiesChanged callback.
57 try
58 {
59 auto inventoryService =
60 getService(object_path::systemInv, interface::invAsset);
61
62 if (!inventoryService.empty())
63 {
64 auto properties = getAllProperties(
65 inventoryService, object_path::systemInv, interface::invAsset);
66
67 _machineTypeModel = std::get<std::string>(properties["Model"]);
68
69 _machineSerialNumber =
70 std::get<std::string>(properties["SerialNumber"]);
71 }
72 }
73 catch (std::exception& e)
74 {
75 // Inventory must not be running at this moment.
76 }
77
78 // Keep up to date by watching for the propertiesChanged signal.
79 _sysInventoryPropMatch = std::make_unique<sdbusplus::bus::match_t>(
80 _bus,
81 sdbusplus::bus::match::rules::propertiesChanged(object_path::systemInv,
82 interface::invAsset),
83 std::bind(std::mem_fn(&DataInterface::sysAssetPropChanged), this,
84 std::placeholders::_1));
85}
86
87void DataInterface::sysAssetPropChanged(sdbusplus::message::message& msg)
88{
89 DBusInterface interface;
90 DBusPropertyMap properties;
91
92 msg.read(interface, properties);
93
94 auto model = properties.find("Model");
95 if (model != properties.end())
96 {
97 _machineTypeModel = std::get<std::string>(model->second);
98 }
99
100 auto sn = properties.find("SerialNumber");
101 if (sn != properties.end())
102 {
103 _machineSerialNumber = std::get<std::string>(sn->second);
104 }
105}
106
107DBusPropertyMap DataInterface::getAllProperties(const std::string& service,
108 const std::string& objectPath,
109 const std::string& interface)
110{
111 DBusPropertyMap properties;
112
113 auto method = _bus.new_method_call(service.c_str(), objectPath.c_str(),
114 interface::dbusProperty, "GetAll");
115 method.append(interface);
116 auto reply = _bus.call(method);
117
118 reply.read(properties);
119
120 return properties;
121}
122
Matt Spinlera7d9d962019-11-06 15:01:25 -0600123void DataInterface::getProperty(const std::string& service,
124 const std::string& objectPath,
125 const std::string& interface,
126 const std::string& property, DBusValue& value)
127{
128
129 auto method = _bus.new_method_call(service.c_str(), objectPath.c_str(),
130 interface::dbusProperty, "Get");
131 method.append(interface, property);
132 auto reply = _bus.call(method);
133
134 reply.read(value);
135}
136
Matt Spinlerc8705e22019-09-11 12:36:07 -0500137DBusService DataInterface::getService(const std::string& objectPath,
138 const std::string& interface)
139{
140 auto method = _bus.new_method_call(service_name::objectMapper,
141 object_path::objectMapper,
142 interface::objectMapper, "GetObject");
143
144 method.append(objectPath, std::vector<std::string>({interface}));
145
146 auto reply = _bus.call(method);
147
148 std::map<DBusService, DBusInterfaceList> response;
149 reply.read(response);
150
151 if (!response.empty())
152 {
153 return response.begin()->first;
154 }
155
156 return std::string{};
157}
Matt Spinlera7d9d962019-11-06 15:01:25 -0600158
159void DataInterface::readHostState()
160{
161 _hostUp = false;
162
163 try
164 {
165 auto service = getService(object_path::hostState, interface::osStatus);
166 if (!service.empty())
167 {
168 DBusValue value;
169 getProperty(service, object_path::hostState, interface::osStatus,
170 "OperatingSystemState", value);
171
172 auto status =
173 Status::convertOSStatusFromString(std::get<std::string>(value));
174
175 if ((status == Status::OSStatus::BootComplete) ||
176 (status == Status::OSStatus::Standby))
177 {
178 _hostUp = true;
179 }
180 }
181 }
182 catch (std::exception& e)
183 {
184 // Not available yet.
185 }
186
187 // Keep up to date by watching for the propertiesChanged signal.
188 _osStateMatch = std::make_unique<sdbusplus::bus::match_t>(
189 _bus,
190 sdbusplus::bus::match::rules::propertiesChanged(object_path::hostState,
191 interface::osStatus),
192 std::bind(std::mem_fn(&DataInterface::osStatePropChanged), this,
193 std::placeholders::_1));
194}
195
196void DataInterface::osStatePropChanged(sdbusplus::message::message& msg)
197{
198 DBusInterface interface;
199 DBusPropertyMap properties;
200
201 msg.read(interface, properties);
202
203 auto state = properties.find("OperatingSystemState");
204 if (state != properties.end())
205 {
206 auto status = Status::convertOSStatusFromString(
207 std::get<std::string>(state->second));
208
209 bool newHostState = false;
210 if ((status == Status::OSStatus::BootComplete) ||
211 (status == Status::OSStatus::Standby))
212 {
213 newHostState = true;
214 }
215
216 setHostState(newHostState);
217 }
218}
219
Matt Spinlerc8705e22019-09-11 12:36:07 -0500220} // namespace pels
221} // namespace openpower