1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
|
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: Copyright OpenBMC Authors
#pragma once
#include "async_resp.hpp"
#include "dbus_utility.hpp"
#include "error_messages.hpp"
#include "generated/enums/resource.hpp"
#include "logging.hpp"
#include <asm-generic/errno.h>
#include <boost/system/error_code.hpp>
#include <nlohmann/json.hpp>
#include <functional>
#include <memory>
#include <string>
namespace redfish
{
namespace resource_utils
{
/**
* @brief Fetches resource status.health from DBus interfaces
*
*/
inline void determineResourceHealth(
const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
const nlohmann::json::json_pointer& jsonPtr, bool functional)
{
BMCWEB_LOG_DEBUG("determineResourceHealth");
if (!functional)
{
asyncResp->res.jsonValue[jsonPtr]["Status"]["Health"] =
resource::Health::Critical;
}
else
{
asyncResp->res.jsonValue[jsonPtr]["Status"]["Health"] =
resource::Health::OK;
}
}
inline void determineResourceState(
const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, bool present,
bool available, const nlohmann::json::json_pointer& jsonPtr)
{
BMCWEB_LOG_DEBUG("determineResourceState");
// Absent takes priority over unavailable
if (!present)
{
asyncResp->res.jsonValue[jsonPtr]["Status"]["State"] =
resource::State::Absent;
}
else if (!available)
{
asyncResp->res.jsonValue[jsonPtr]["Status"]["State"] =
resource::State::UnavailableOffline;
}
else
{
asyncResp->res.jsonValue[jsonPtr]["Status"]["State"] =
resource::State::Enabled;
}
}
inline void getStatusAvailableState(
const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
const nlohmann::json::json_pointer& jsonPtr, bool present,
const boost::system::error_code& ec, bool available)
{
if (ec)
{
if (ec.value() != EBADR)
{
BMCWEB_LOG_ERROR("DBUS response error for {}, ec {}", "Available",
ec.value());
messages::internalError(asyncResp->res);
return;
}
available = true;
}
determineResourceState(asyncResp, present, available, jsonPtr);
}
inline void getStatusPresentState(
const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
const std::string& service, const std::string& path,
const nlohmann::json::json_pointer& jsonPtr,
const boost::system::error_code& ec, bool present)
{
if (ec)
{
if (ec.value() != EBADR)
{
BMCWEB_LOG_ERROR("DBUS response error for {}, ec {}", "Present",
ec.value());
messages::internalError(asyncResp->res);
return;
}
// Interface is missing, default to true for Enabled
present = true;
}
dbus::utility::getProperty<bool>(
*crow::connections::systemBus, service, path,
"xyz.openbmc_project.State.Decorator.Availability", "Available",
std::bind_front(getStatusAvailableState, asyncResp, jsonPtr, present));
}
inline void getResourceState(
const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
const std::string& service, const std::string& path,
const nlohmann::json::json_pointer& jsonPtr)
{
BMCWEB_LOG_DEBUG("getResourceStatus");
dbus::utility::getProperty<bool>(
*crow::connections::systemBus, service, path,
"xyz.openbmc_project.Inventory.Item", "Present",
std::bind_front(getStatusPresentState, asyncResp, service, path,
jsonPtr));
}
/*
* @brief Retrieves the status.state of the first service that implements
* Inventory.Item or State.Decorator.Availability. If no service implements
* these interfaces, it will default to Status.State Enabled
*
* @param[in] asyncResp AsyncResp object to update
* @param[in] services Map of services to interfaces
* @param[in] path D-Bus object path
* @param[in] jsonPtr JSON pointer to where the parent JSON object is
*/
inline void getResourceState(
const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
const dbus::utility::MapperServiceMap& services, const std::string& path,
const nlohmann::json::json_pointer& jsonPtr)
{
for (const auto& [serviceName, interfaces] : services)
{
for (const auto& interface : interfaces)
{
if (interface == "xyz.openbmc_project.Inventory.Item" ||
interface == "xyz.openbmc_project.State.Decorator.Availability")
{
getResourceState(asyncResp, serviceName, path, jsonPtr);
return;
}
}
}
asyncResp->res.jsonValue[jsonPtr]["Status"]["State"] =
resource::State::Enabled;
}
inline void afterGetResourceHealth(
const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
const nlohmann::json::json_pointer& jsonPtr,
const boost::system::error_code& ec, bool functional)
{
if (ec)
{
if (ec.value() != EBADR)
{
BMCWEB_LOG_ERROR("DBUS response error for {}, ec {}", "Functional",
ec.value());
messages::internalError(asyncResp->res);
return;
}
// Interface missing, default to OK
functional = true;
}
determineResourceHealth(asyncResp, jsonPtr, functional);
}
/*
* @brief Retrieves the status of the resource's status.health
*
* Queries interface:
* - xyz.openbmc_project.State.Decorator.OperationalStatus::Functional
*/
inline void getResourceHealth(
const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
const std::string& service, const std::string& path,
const nlohmann::json::json_pointer& jsonPtr)
{
dbus::utility::getProperty<bool>(
*crow::connections::systemBus, service, path,
"xyz.openbmc_project.State.Decorator.OperationalStatus", "Functional",
std::bind_front(afterGetResourceHealth, asyncResp, jsonPtr));
}
/*
* @brief Retrieves the status.health of the first service that implements
* State.Decorator.OperationalStatus. If no service implements the interface,
* it will default to Status.Health OK
*
* @param[in] asyncResp AsyncResp object to update
* @param[in] services Map of services to interfaces
* @param[in] path D-Bus object path
* @param[in] jsonPtr JSON pointer to where the parent JSON object is
*/
inline void getResourceHealth(
const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
const dbus::utility::MapperServiceMap& services, const std::string& path,
const nlohmann::json::json_pointer& jsonPtr)
{
for (const auto& [serviceName, interfaces] : services)
{
for (const auto& interface : interfaces)
{
if (interface ==
"xyz.openbmc_project.State.Decorator.OperationalStatus")
{
getResourceHealth(asyncResp, serviceName, path, jsonPtr);
return;
}
}
}
asyncResp->res.jsonValue[jsonPtr]["Status"]["Health"] =
resource::Health::OK;
}
} // namespace resource_utils
} // namespace redfish
|