blob: 6e72fea43d2840df8d41e4acae5e8104e1bb98a6 [file] [log] [blame]
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +01001/*
2// Copyright (c) 2018 Intel 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*/
16#pragma once
17
John Edward Broadbent7e860f12021-04-08 15:57:16 -070018#include <app.hpp>
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +010019#include <boost/algorithm/string/predicate.hpp>
20#include <boost/algorithm/string/split.hpp>
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +010021#include <boost/range/algorithm/replace_copy_if.hpp>
Ed Tanous1abe55e2018-09-05 08:30:59 -070022#include <dbus_singleton.hpp>
Ed Tanous168e20c2021-12-13 14:39:53 -080023#include <dbus_utility.hpp>
Ed Tanous45ca1b82022-03-25 13:07:27 -070024#include <query.hpp>
Ed Tanoused398212021-06-09 17:05:54 -070025#include <registries/privilege_registry.hpp>
Jonathan Doman1e1e5982021-06-11 09:36:17 -070026#include <sdbusplus/asio/property.hpp>
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +053027#include <utils/json_utils.hpp>
Nan Zhou928fefb2022-03-28 08:45:00 -070028#include <utils/query_param.hpp>
Gunnar Mills1214b7e2020-06-04 10:11:30 -050029
30#include <cmath>
Nan Zhoufe04d492022-06-22 17:10:41 +000031#include <iterator>
32#include <map>
33#include <set>
Ed Tanousb5a76932020-09-29 16:16:58 -070034#include <utility>
Ed Tanousabf2add2019-01-22 16:40:12 -080035#include <variant>
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +010036
Ed Tanous1abe55e2018-09-05 08:30:59 -070037namespace redfish
38{
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +010039
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +020040namespace sensors
41{
42namespace node
43{
44static constexpr std::string_view power = "Power";
45static constexpr std::string_view sensors = "Sensors";
46static constexpr std::string_view thermal = "Thermal";
47} // namespace node
48
Ed Tanous02da7c52022-02-27 00:09:02 -080049// clang-format off
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +020050namespace dbus
51{
Ed Tanous4ee8e212022-05-28 09:42:51 -070052static auto powerPaths = std::to_array<std::string_view>({
Ed Tanous02da7c52022-02-27 00:09:02 -080053 "/xyz/openbmc_project/sensors/voltage",
54 "/xyz/openbmc_project/sensors/power"
55});
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +000056
Ed Tanous4ee8e212022-05-28 09:42:51 -070057static auto sensorPaths = std::to_array<std::string_view>({
Ed Tanous02da7c52022-02-27 00:09:02 -080058 "/xyz/openbmc_project/sensors/power",
59 "/xyz/openbmc_project/sensors/current",
60 "/xyz/openbmc_project/sensors/airflow",
Gunnar Mills5deabed2022-04-20 13:43:45 -060061 "/xyz/openbmc_project/sensors/humidity",
George Liue8204932021-02-01 14:42:49 +080062#ifdef BMCWEB_NEW_POWERSUBSYSTEM_THERMALSUBSYSTEM
Ed Tanous02da7c52022-02-27 00:09:02 -080063 "/xyz/openbmc_project/sensors/voltage",
64 "/xyz/openbmc_project/sensors/fan_tach",
65 "/xyz/openbmc_project/sensors/temperature",
66 "/xyz/openbmc_project/sensors/fan_pwm",
67 "/xyz/openbmc_project/sensors/altitude",
68 "/xyz/openbmc_project/sensors/energy",
George Liue8204932021-02-01 14:42:49 +080069#endif
Ed Tanous02da7c52022-02-27 00:09:02 -080070 "/xyz/openbmc_project/sensors/utilization"
71});
72
Ed Tanous4ee8e212022-05-28 09:42:51 -070073static auto thermalPaths = std::to_array<std::string_view>({
Ed Tanous02da7c52022-02-27 00:09:02 -080074 "/xyz/openbmc_project/sensors/fan_tach",
75 "/xyz/openbmc_project/sensors/temperature",
76 "/xyz/openbmc_project/sensors/fan_pwm"
77});
78
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +000079} // namespace dbus
Ed Tanous02da7c52022-02-27 00:09:02 -080080// clang-format on
81
82using sensorPair = std::pair<std::string_view, std::span<std::string_view>>;
83static constexpr std::array<sensorPair, 3> paths = {
84 {{node::power, std::span<std::string_view>(dbus::powerPaths)},
85 {node::sensors, std::span<std::string_view>(dbus::sensorPaths)},
86 {node::thermal, std::span<std::string_view>(dbus::thermalPaths)}}};
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +000087
88inline const char* toReadingType(const std::string& sensorType)
89{
90 if (sensorType == "voltage")
91 {
92 return "Voltage";
93 }
94 if (sensorType == "power")
95 {
96 return "Power";
97 }
98 if (sensorType == "current")
99 {
100 return "Current";
101 }
102 if (sensorType == "fan_tach")
103 {
104 return "Rotational";
105 }
106 if (sensorType == "temperature")
107 {
108 return "Temperature";
109 }
110 if (sensorType == "fan_pwm" || sensorType == "utilization")
111 {
112 return "Percent";
113 }
Gunnar Mills5deabed2022-04-20 13:43:45 -0600114 if (sensorType == "humidity")
115 {
116 return "Humidity";
117 }
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000118 if (sensorType == "altitude")
119 {
120 return "Altitude";
121 }
122 if (sensorType == "airflow")
123 {
124 return "AirFlow";
125 }
126 if (sensorType == "energy")
127 {
128 return "EnergyJoules";
129 }
130 return "";
131}
132
133inline const char* toReadingUnits(const std::string& sensorType)
134{
135 if (sensorType == "voltage")
136 {
137 return "V";
138 }
139 if (sensorType == "power")
140 {
141 return "W";
142 }
143 if (sensorType == "current")
144 {
145 return "A";
146 }
147 if (sensorType == "fan_tach")
148 {
149 return "RPM";
150 }
151 if (sensorType == "temperature")
152 {
153 return "Cel";
154 }
Gunnar Mills5deabed2022-04-20 13:43:45 -0600155 if (sensorType == "fan_pwm" || sensorType == "utilization" ||
156 sensorType == "humidity")
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000157 {
158 return "%";
159 }
160 if (sensorType == "altitude")
161 {
162 return "m";
163 }
164 if (sensorType == "airflow")
165 {
166 return "cft_i/min";
167 }
168 if (sensorType == "energy")
169 {
170 return "J";
171 }
172 return "";
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200173}
174} // namespace sensors
175
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100176/**
Kowalski, Kamil588c3f02018-04-03 14:55:27 +0200177 * SensorsAsyncResp
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100178 * Gathers data needed for response processing after async calls are done
179 */
Ed Tanous1abe55e2018-09-05 08:30:59 -0700180class SensorsAsyncResp
181{
182 public:
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200183 using DataCompleteCb = std::function<void(
184 const boost::beast::http::status status,
Nan Zhoufe04d492022-06-22 17:10:41 +0000185 const std::map<std::string, std::string>& uriToDbus)>;
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200186
187 struct SensorData
188 {
189 const std::string name;
190 std::string uri;
191 const std::string valueKey;
192 const std::string dbusPath;
193 };
194
Ed Tanous8a592812022-06-04 09:06:59 -0700195 SensorsAsyncResp(const std::shared_ptr<bmcweb::AsyncResp>& asyncRespIn,
zhanghch058d1b46d2021-04-01 11:18:24 +0800196 const std::string& chassisIdIn,
Ed Tanous02da7c52022-02-27 00:09:02 -0800197 std::span<std::string_view> typesIn,
198 std::string_view subNode) :
Ed Tanous8a592812022-06-04 09:06:59 -0700199 asyncResp(asyncRespIn),
Nan Zhou928fefb2022-03-28 08:45:00 -0700200 chassisId(chassisIdIn), types(typesIn), chassisSubNode(subNode),
201 efficientExpand(false)
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500202 {}
Kowalski, Kamil588c3f02018-04-03 14:55:27 +0200203
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200204 // Store extra data about sensor mapping and return it in callback
Ed Tanous8a592812022-06-04 09:06:59 -0700205 SensorsAsyncResp(const std::shared_ptr<bmcweb::AsyncResp>& asyncRespIn,
zhanghch058d1b46d2021-04-01 11:18:24 +0800206 const std::string& chassisIdIn,
Ed Tanous02da7c52022-02-27 00:09:02 -0800207 std::span<std::string_view> typesIn,
208 std::string_view subNode,
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200209 DataCompleteCb&& creationComplete) :
Ed Tanous8a592812022-06-04 09:06:59 -0700210 asyncResp(asyncRespIn),
Nan Zhou928fefb2022-03-28 08:45:00 -0700211 chassisId(chassisIdIn), types(typesIn), chassisSubNode(subNode),
212 efficientExpand(false), metadata{std::vector<SensorData>()},
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200213 dataComplete{std::move(creationComplete)}
214 {}
215
Nan Zhou928fefb2022-03-28 08:45:00 -0700216 // sensor collections expand
Ed Tanous8a592812022-06-04 09:06:59 -0700217 SensorsAsyncResp(const std::shared_ptr<bmcweb::AsyncResp>& asyncRespIn,
Nan Zhou928fefb2022-03-28 08:45:00 -0700218 const std::string& chassisIdIn,
Ed Tanous02da7c52022-02-27 00:09:02 -0800219 const std::span<std::string_view> typesIn,
Ed Tanous8a592812022-06-04 09:06:59 -0700220 const std::string_view& subNode, bool efficientExpandIn) :
221 asyncResp(asyncRespIn),
Nan Zhou928fefb2022-03-28 08:45:00 -0700222 chassisId(chassisIdIn), types(typesIn), chassisSubNode(subNode),
Ed Tanous8a592812022-06-04 09:06:59 -0700223 efficientExpand(efficientExpandIn)
Nan Zhou928fefb2022-03-28 08:45:00 -0700224 {}
225
Ed Tanous1abe55e2018-09-05 08:30:59 -0700226 ~SensorsAsyncResp()
227 {
zhanghch058d1b46d2021-04-01 11:18:24 +0800228 if (asyncResp->res.result() ==
229 boost::beast::http::status::internal_server_error)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700230 {
231 // Reset the json object to clear out any data that made it in
232 // before the error happened todo(ed) handle error condition with
233 // proper code
zhanghch058d1b46d2021-04-01 11:18:24 +0800234 asyncResp->res.jsonValue = nlohmann::json::object();
Ed Tanous1abe55e2018-09-05 08:30:59 -0700235 }
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200236
237 if (dataComplete && metadata)
238 {
Nan Zhoufe04d492022-06-22 17:10:41 +0000239 std::map<std::string, std::string> map;
zhanghch058d1b46d2021-04-01 11:18:24 +0800240 if (asyncResp->res.result() == boost::beast::http::status::ok)
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200241 {
242 for (auto& sensor : *metadata)
243 {
244 map.insert(std::make_pair(sensor.uri + sensor.valueKey,
245 sensor.dbusPath));
246 }
247 }
zhanghch058d1b46d2021-04-01 11:18:24 +0800248 dataComplete(asyncResp->res.result(), map);
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200249 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700250 }
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100251
Ed Tanousecd6a3a2022-01-07 09:18:40 -0800252 SensorsAsyncResp(const SensorsAsyncResp&) = delete;
253 SensorsAsyncResp(SensorsAsyncResp&&) = delete;
254 SensorsAsyncResp& operator=(const SensorsAsyncResp&) = delete;
255 SensorsAsyncResp& operator=(SensorsAsyncResp&&) = delete;
256
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200257 void addMetadata(const nlohmann::json& sensorObject,
258 const std::string& valueKey, const std::string& dbusPath)
259 {
260 if (metadata)
261 {
262 metadata->emplace_back(SensorData{sensorObject["Name"],
263 sensorObject["@odata.id"],
264 valueKey, dbusPath});
265 }
266 }
267
268 void updateUri(const std::string& name, const std::string& uri)
269 {
270 if (metadata)
271 {
272 for (auto& sensor : *metadata)
273 {
274 if (sensor.name == name)
275 {
276 sensor.uri = uri;
277 }
278 }
279 }
280 }
281
zhanghch058d1b46d2021-04-01 11:18:24 +0800282 const std::shared_ptr<bmcweb::AsyncResp> asyncResp;
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200283 const std::string chassisId;
Ed Tanous02da7c52022-02-27 00:09:02 -0800284 const std::span<std::string_view> types;
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200285 const std::string chassisSubNode;
Nan Zhou928fefb2022-03-28 08:45:00 -0700286 const bool efficientExpand;
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200287
288 private:
289 std::optional<std::vector<SensorData>> metadata;
290 DataCompleteCb dataComplete;
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100291};
292
293/**
Anthony Wilsond5005492019-07-31 16:34:17 -0500294 * Possible states for physical inventory leds
295 */
296enum class LedState
297{
298 OFF,
299 ON,
300 BLINK,
301 UNKNOWN
302};
303
304/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500305 * D-Bus inventory item associated with one or more sensors.
306 */
307class InventoryItem
308{
309 public:
Ed Tanous4e23a442022-06-06 09:57:26 -0700310 explicit InventoryItem(const std::string& objPath) : objectPath(objPath)
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500311 {
312 // Set inventory item name to last node of object path
George Liu28aa8de2021-02-01 15:13:30 +0800313 sdbusplus::message::object_path path(objectPath);
314 name = path.filename();
315 if (name.empty())
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500316 {
George Liu28aa8de2021-02-01 15:13:30 +0800317 BMCWEB_LOG_ERROR << "Failed to find '/' in " << objectPath;
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500318 }
319 }
320
321 std::string objectPath;
322 std::string name;
Ed Tanouse05aec52022-01-25 10:28:56 -0800323 bool isPresent = true;
324 bool isFunctional = true;
325 bool isPowerSupply = false;
326 int powerSupplyEfficiencyPercent = -1;
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500327 std::string manufacturer;
328 std::string model;
329 std::string partNumber;
330 std::string serialNumber;
331 std::set<std::string> sensors;
Anthony Wilsond5005492019-07-31 16:34:17 -0500332 std::string ledObjectPath;
Ed Tanouse05aec52022-01-25 10:28:56 -0800333 LedState ledState = LedState::UNKNOWN;
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500334};
335
336/**
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530337 * @brief Get objects with connection necessary for sensors
Kowalski, Kamil588c3f02018-04-03 14:55:27 +0200338 * @param SensorsAsyncResp Pointer to object holding response data
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100339 * @param sensorNames Sensors retrieved from chassis
340 * @param callback Callback for processing gathered connections
341 */
342template <typename Callback>
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530343void getObjectsWithConnection(
Ed Tanous81ce6092020-12-17 16:54:55 +0000344 const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
Nan Zhoufe04d492022-06-22 17:10:41 +0000345 const std::shared_ptr<std::set<std::string>>& sensorNames,
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530346 Callback&& callback)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700347{
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530348 BMCWEB_LOG_DEBUG << "getObjectsWithConnection enter";
Ed Tanous1abe55e2018-09-05 08:30:59 -0700349 const std::string path = "/xyz/openbmc_project/sensors";
350 const std::array<std::string, 1> interfaces = {
351 "xyz.openbmc_project.Sensor.Value"};
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100352
Ed Tanous1abe55e2018-09-05 08:30:59 -0700353 // Response handler for parsing objects subtree
Ed Tanous002d39b2022-05-31 08:59:27 -0700354 auto respHandler =
355 [callback{std::forward<Callback>(callback)}, sensorsAsyncResp,
356 sensorNames](const boost::system::error_code ec,
357 const dbus::utility::MapperGetSubTreeResponse& subtree) {
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530358 BMCWEB_LOG_DEBUG << "getObjectsWithConnection resp_handler enter";
Ed Tanous1abe55e2018-09-05 08:30:59 -0700359 if (ec)
360 {
zhanghch058d1b46d2021-04-01 11:18:24 +0800361 messages::internalError(sensorsAsyncResp->asyncResp->res);
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530362 BMCWEB_LOG_ERROR
363 << "getObjectsWithConnection resp_handler: Dbus error " << ec;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700364 return;
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100365 }
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100366
Ed Tanous1abe55e2018-09-05 08:30:59 -0700367 BMCWEB_LOG_DEBUG << "Found " << subtree.size() << " subtrees";
368
369 // Make unique list of connections only for requested sensor types and
370 // found in the chassis
Nan Zhoufe04d492022-06-22 17:10:41 +0000371 std::set<std::string> connections;
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530372 std::set<std::pair<std::string, std::string>> objectsWithConnection;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700373
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700374 BMCWEB_LOG_DEBUG << "sensorNames list count: " << sensorNames->size();
375 for (const std::string& tsensor : *sensorNames)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700376 {
377 BMCWEB_LOG_DEBUG << "Sensor to find: " << tsensor;
378 }
379
380 for (const std::pair<
381 std::string,
382 std::vector<std::pair<std::string, std::vector<std::string>>>>&
383 object : subtree)
384 {
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700385 if (sensorNames->find(object.first) != sensorNames->end())
Ed Tanous1abe55e2018-09-05 08:30:59 -0700386 {
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700387 for (const std::pair<std::string, std::vector<std::string>>&
388 objData : object.second)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700389 {
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700390 BMCWEB_LOG_DEBUG << "Adding connection: " << objData.first;
391 connections.insert(objData.first);
392 objectsWithConnection.insert(
393 std::make_pair(object.first, objData.first));
Ed Tanous1abe55e2018-09-05 08:30:59 -0700394 }
395 }
396 }
397 BMCWEB_LOG_DEBUG << "Found " << connections.size() << " connections";
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530398 callback(std::move(connections), std::move(objectsWithConnection));
399 BMCWEB_LOG_DEBUG << "getObjectsWithConnection resp_handler exit";
Ed Tanous1abe55e2018-09-05 08:30:59 -0700400 };
Ed Tanous1abe55e2018-09-05 08:30:59 -0700401 // Make call to ObjectMapper to find all sensors objects
402 crow::connections::systemBus->async_method_call(
403 std::move(respHandler), "xyz.openbmc_project.ObjectMapper",
404 "/xyz/openbmc_project/object_mapper",
405 "xyz.openbmc_project.ObjectMapper", "GetSubTree", path, 2, interfaces);
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530406 BMCWEB_LOG_DEBUG << "getObjectsWithConnection exit";
407}
408
409/**
410 * @brief Create connections necessary for sensors
411 * @param SensorsAsyncResp Pointer to object holding response data
412 * @param sensorNames Sensors retrieved from chassis
413 * @param callback Callback for processing gathered connections
414 */
415template <typename Callback>
Nan Zhoufe04d492022-06-22 17:10:41 +0000416void getConnections(std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
417 const std::shared_ptr<std::set<std::string>> sensorNames,
418 Callback&& callback)
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530419{
420 auto objectsWithConnectionCb =
Nan Zhoufe04d492022-06-22 17:10:41 +0000421 [callback](const std::set<std::string>& connections,
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530422 const std::set<std::pair<std::string, std::string>>&
Ed Tanous3174e4d2020-10-07 11:41:22 -0700423 /*objectsWithConnection*/) { callback(connections); };
Ed Tanous81ce6092020-12-17 16:54:55 +0000424 getObjectsWithConnection(sensorsAsyncResp, sensorNames,
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530425 std::move(objectsWithConnectionCb));
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100426}
427
428/**
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700429 * @brief Shrinks the list of sensors for processing
430 * @param SensorsAysncResp The class holding the Redfish response
431 * @param allSensors A list of all the sensors associated to the
432 * chassis element (i.e. baseboard, front panel, etc...)
433 * @param activeSensors A list that is a reduction of the incoming
434 * allSensors list. Eliminate Thermal sensors when a Power request is
435 * made, and eliminate Power sensors when a Thermal request is made.
436 */
Ed Tanous23a21a12020-07-25 04:45:05 +0000437inline void reduceSensorList(
Ed Tanous81ce6092020-12-17 16:54:55 +0000438 const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700439 const std::vector<std::string>* allSensors,
Nan Zhoufe04d492022-06-22 17:10:41 +0000440 const std::shared_ptr<std::set<std::string>>& activeSensors)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700441{
Ed Tanous81ce6092020-12-17 16:54:55 +0000442 if (sensorsAsyncResp == nullptr)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700443 {
444 return;
445 }
446 if ((allSensors == nullptr) || (activeSensors == nullptr))
447 {
448 messages::resourceNotFound(
zhanghch058d1b46d2021-04-01 11:18:24 +0800449 sensorsAsyncResp->asyncResp->res, sensorsAsyncResp->chassisSubNode,
Ed Tanous81ce6092020-12-17 16:54:55 +0000450 sensorsAsyncResp->chassisSubNode == sensors::node::thermal
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200451 ? "Temperatures"
452 : "Voltages");
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700453
454 return;
455 }
456 if (allSensors->empty())
457 {
458 // Nothing to do, the activeSensors object is also empty
459 return;
460 }
461
Ed Tanous02da7c52022-02-27 00:09:02 -0800462 for (std::string_view type : sensorsAsyncResp->types)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700463 {
464 for (const std::string& sensor : *allSensors)
465 {
466 if (boost::starts_with(sensor, type))
467 {
468 activeSensors->emplace(sensor);
469 }
470 }
471 }
472}
473
474/**
Carol Wang4bb3dc32019-10-17 18:15:02 +0800475 * @brief Retrieves valid chassis path
476 * @param asyncResp Pointer to object holding response data
477 * @param callback Callback for next step to get valid chassis path
478 */
479template <typename Callback>
Ed Tanousb5a76932020-09-29 16:16:58 -0700480void getValidChassisPath(const std::shared_ptr<SensorsAsyncResp>& asyncResp,
Carol Wang4bb3dc32019-10-17 18:15:02 +0800481 Callback&& callback)
482{
483 BMCWEB_LOG_DEBUG << "checkChassisId enter";
484 const std::array<const char*, 2> interfaces = {
485 "xyz.openbmc_project.Inventory.Item.Board",
486 "xyz.openbmc_project.Inventory.Item.Chassis"};
487
Ed Tanousb9d36b42022-02-26 21:42:46 -0800488 auto respHandler = [callback{std::forward<Callback>(callback)}, asyncResp](
489 const boost::system::error_code ec,
490 const dbus::utility::MapperGetSubTreePathsResponse&
491 chassisPaths) mutable {
492 BMCWEB_LOG_DEBUG << "getValidChassisPath respHandler enter";
493 if (ec)
494 {
495 BMCWEB_LOG_ERROR << "getValidChassisPath respHandler DBUS error: "
496 << ec;
497 messages::internalError(asyncResp->asyncResp->res);
498 return;
499 }
Carol Wang4bb3dc32019-10-17 18:15:02 +0800500
Ed Tanousb9d36b42022-02-26 21:42:46 -0800501 std::optional<std::string> chassisPath;
502 std::string chassisName;
503 for (const std::string& chassis : chassisPaths)
504 {
505 sdbusplus::message::object_path path(chassis);
506 chassisName = path.filename();
507 if (chassisName.empty())
Carol Wang4bb3dc32019-10-17 18:15:02 +0800508 {
Ed Tanousb9d36b42022-02-26 21:42:46 -0800509 BMCWEB_LOG_ERROR << "Failed to find '/' in " << chassis;
510 continue;
Carol Wang4bb3dc32019-10-17 18:15:02 +0800511 }
Ed Tanousb9d36b42022-02-26 21:42:46 -0800512 if (chassisName == asyncResp->chassisId)
513 {
514 chassisPath = chassis;
515 break;
516 }
517 }
518 callback(chassisPath);
519 };
Carol Wang4bb3dc32019-10-17 18:15:02 +0800520
521 // Get the Chassis Collection
522 crow::connections::systemBus->async_method_call(
523 respHandler, "xyz.openbmc_project.ObjectMapper",
524 "/xyz/openbmc_project/object_mapper",
525 "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths",
526 "/xyz/openbmc_project/inventory", 0, interfaces);
527 BMCWEB_LOG_DEBUG << "checkChassisId exit";
528}
529
530/**
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100531 * @brief Retrieves requested chassis sensors and redundancy data from DBus .
Kowalski, Kamil588c3f02018-04-03 14:55:27 +0200532 * @param SensorsAsyncResp Pointer to object holding response data
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100533 * @param callback Callback for next step in gathered sensor processing
534 */
535template <typename Callback>
Ed Tanousb5a76932020-09-29 16:16:58 -0700536void getChassis(const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
Ed Tanous1abe55e2018-09-05 08:30:59 -0700537 Callback&& callback)
538{
539 BMCWEB_LOG_DEBUG << "getChassis enter";
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500540 const std::array<const char*, 2> interfaces = {
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700541 "xyz.openbmc_project.Inventory.Item.Board",
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500542 "xyz.openbmc_project.Inventory.Item.Chassis"};
Ed Tanous002d39b2022-05-31 08:59:27 -0700543 auto respHandler =
544 [callback{std::forward<Callback>(callback)}, sensorsAsyncResp](
545 const boost::system::error_code ec,
546 const dbus::utility::MapperGetSubTreePathsResponse& chassisPaths) {
Ed Tanous1abe55e2018-09-05 08:30:59 -0700547 BMCWEB_LOG_DEBUG << "getChassis respHandler enter";
548 if (ec)
549 {
550 BMCWEB_LOG_ERROR << "getChassis respHandler DBUS error: " << ec;
zhanghch058d1b46d2021-04-01 11:18:24 +0800551 messages::internalError(sensorsAsyncResp->asyncResp->res);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700552 return;
553 }
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100554
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700555 const std::string* chassisPath = nullptr;
556 std::string chassisName;
557 for (const std::string& chassis : chassisPaths)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700558 {
George Liu28aa8de2021-02-01 15:13:30 +0800559 sdbusplus::message::object_path path(chassis);
560 chassisName = path.filename();
561 if (chassisName.empty())
Ed Tanous1abe55e2018-09-05 08:30:59 -0700562 {
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700563 BMCWEB_LOG_ERROR << "Failed to find '/' in " << chassis;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700564 continue;
565 }
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700566 if (chassisName == sensorsAsyncResp->chassisId)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700567 {
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700568 chassisPath = &chassis;
569 break;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700570 }
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700571 }
572 if (chassisPath == nullptr)
573 {
zhanghch058d1b46d2021-04-01 11:18:24 +0800574 messages::resourceNotFound(sensorsAsyncResp->asyncResp->res,
575 "Chassis", sensorsAsyncResp->chassisId);
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700576 return;
577 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700578
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700579 const std::string& chassisSubNode = sensorsAsyncResp->chassisSubNode;
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200580 if (chassisSubNode == sensors::node::power)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700581 {
zhanghch058d1b46d2021-04-01 11:18:24 +0800582 sensorsAsyncResp->asyncResp->res.jsonValue["@odata.type"] =
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700583 "#Power.v1_5_2.Power";
Ed Tanous1abe55e2018-09-05 08:30:59 -0700584 }
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200585 else if (chassisSubNode == sensors::node::thermal)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700586 {
zhanghch058d1b46d2021-04-01 11:18:24 +0800587 sensorsAsyncResp->asyncResp->res.jsonValue["@odata.type"] =
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700588 "#Thermal.v1_4_0.Thermal";
zhanghch058d1b46d2021-04-01 11:18:24 +0800589 sensorsAsyncResp->asyncResp->res.jsonValue["Fans"] =
590 nlohmann::json::array();
591 sensorsAsyncResp->asyncResp->res.jsonValue["Temperatures"] =
Jennifer Lee4f9a2132019-03-04 12:45:19 -0800592 nlohmann::json::array();
Ed Tanous1abe55e2018-09-05 08:30:59 -0700593 }
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200594 else if (chassisSubNode == sensors::node::sensors)
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500595 {
zhanghch058d1b46d2021-04-01 11:18:24 +0800596 sensorsAsyncResp->asyncResp->res.jsonValue["@odata.type"] =
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500597 "#SensorCollection.SensorCollection";
zhanghch058d1b46d2021-04-01 11:18:24 +0800598 sensorsAsyncResp->asyncResp->res.jsonValue["Description"] =
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500599 "Collection of Sensors for this Chassis";
zhanghch058d1b46d2021-04-01 11:18:24 +0800600 sensorsAsyncResp->asyncResp->res.jsonValue["Members"] =
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500601 nlohmann::json::array();
zhanghch058d1b46d2021-04-01 11:18:24 +0800602 sensorsAsyncResp->asyncResp->res.jsonValue["Members@odata.count"] =
603 0;
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500604 }
605
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200606 if (chassisSubNode != sensors::node::sensors)
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500607 {
zhanghch058d1b46d2021-04-01 11:18:24 +0800608 sensorsAsyncResp->asyncResp->res.jsonValue["Id"] = chassisSubNode;
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500609 }
610
zhanghch058d1b46d2021-04-01 11:18:24 +0800611 sensorsAsyncResp->asyncResp->res.jsonValue["@odata.id"] =
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700612 "/redfish/v1/Chassis/" + sensorsAsyncResp->chassisId + "/" +
613 chassisSubNode;
zhanghch058d1b46d2021-04-01 11:18:24 +0800614 sensorsAsyncResp->asyncResp->res.jsonValue["Name"] = chassisSubNode;
Shawn McCarney8fb49dd2019-06-12 17:47:00 -0500615 // Get the list of all sensors for this Chassis element
616 std::string sensorPath = *chassisPath + "/all_sensors";
Jonathan Doman1e1e5982021-06-11 09:36:17 -0700617 sdbusplus::asio::getProperty<std::vector<std::string>>(
618 *crow::connections::systemBus, "xyz.openbmc_project.ObjectMapper",
619 sensorPath, "xyz.openbmc_project.Association", "endpoints",
Ed Tanousf94c4ec2022-01-06 12:44:41 -0800620 [sensorsAsyncResp,
621 callback{std::forward<const Callback>(callback)}](
Ed Tanous271584a2019-07-09 16:24:22 -0700622 const boost::system::error_code& e,
Jonathan Doman1e1e5982021-06-11 09:36:17 -0700623 const std::vector<std::string>& nodeSensorList) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700624 if (e)
625 {
626 if (e.value() != EBADR)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700627 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700628 messages::internalError(sensorsAsyncResp->asyncResp->res);
629 return;
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700630 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700631 }
Nan Zhoufe04d492022-06-22 17:10:41 +0000632 const std::shared_ptr<std::set<std::string>> culledSensorList =
633 std::make_shared<std::set<std::string>>();
Ed Tanous002d39b2022-05-31 08:59:27 -0700634 reduceSensorList(sensorsAsyncResp, &nodeSensorList,
635 culledSensorList);
636 callback(culledSensorList);
Jonathan Doman1e1e5982021-06-11 09:36:17 -0700637 });
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100638 };
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100639
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700640 // Get the Chassis Collection
Ed Tanous1abe55e2018-09-05 08:30:59 -0700641 crow::connections::systemBus->async_method_call(
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700642 respHandler, "xyz.openbmc_project.ObjectMapper",
643 "/xyz/openbmc_project/object_mapper",
644 "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths",
Ed Tanous271584a2019-07-09 16:24:22 -0700645 "/xyz/openbmc_project/inventory", 0, interfaces);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700646 BMCWEB_LOG_DEBUG << "getChassis exit";
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100647}
648
649/**
Shawn McCarneyde629b62019-03-08 10:42:51 -0600650 * @brief Finds all DBus object paths that implement ObjectManager.
651 *
652 * Creates a mapping from the associated connection name to the object path.
653 *
654 * Finds the object paths asynchronously. Invokes callback when information has
655 * been obtained.
656 *
657 * The callback must have the following signature:
658 * @code
Nan Zhoufe04d492022-06-22 17:10:41 +0000659 * callback(std::shared_ptr<std::map<std::string,std::string>> objectMgrPaths)
Shawn McCarneyde629b62019-03-08 10:42:51 -0600660 * @endcode
661 *
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700662 * @param sensorsAsyncResp Pointer to object holding response data.
Shawn McCarneyde629b62019-03-08 10:42:51 -0600663 * @param callback Callback to invoke when object paths obtained.
664 */
665template <typename Callback>
Ed Tanousb5a76932020-09-29 16:16:58 -0700666void getObjectManagerPaths(
Ed Tanous81ce6092020-12-17 16:54:55 +0000667 const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
Ed Tanousb5a76932020-09-29 16:16:58 -0700668 Callback&& callback)
Shawn McCarneyde629b62019-03-08 10:42:51 -0600669{
670 BMCWEB_LOG_DEBUG << "getObjectManagerPaths enter";
671 const std::array<std::string, 1> interfaces = {
672 "org.freedesktop.DBus.ObjectManager"};
673
674 // Response handler for GetSubTree DBus method
Ed Tanous002d39b2022-05-31 08:59:27 -0700675 auto respHandler =
676 [callback{std::forward<Callback>(callback)}, sensorsAsyncResp](
677 const boost::system::error_code ec,
678 const dbus::utility::MapperGetSubTreeResponse& subtree) {
Shawn McCarneyde629b62019-03-08 10:42:51 -0600679 BMCWEB_LOG_DEBUG << "getObjectManagerPaths respHandler enter";
680 if (ec)
681 {
zhanghch058d1b46d2021-04-01 11:18:24 +0800682 messages::internalError(sensorsAsyncResp->asyncResp->res);
Shawn McCarneyde629b62019-03-08 10:42:51 -0600683 BMCWEB_LOG_ERROR << "getObjectManagerPaths respHandler: DBus error "
684 << ec;
685 return;
686 }
687
688 // Loop over returned object paths
Nan Zhoufe04d492022-06-22 17:10:41 +0000689 std::shared_ptr<std::map<std::string, std::string>> objectMgrPaths =
690 std::make_shared<std::map<std::string, std::string>>();
Shawn McCarneyde629b62019-03-08 10:42:51 -0600691 for (const std::pair<
692 std::string,
693 std::vector<std::pair<std::string, std::vector<std::string>>>>&
694 object : subtree)
695 {
696 // Loop over connections for current object path
697 const std::string& objectPath = object.first;
698 for (const std::pair<std::string, std::vector<std::string>>&
699 objData : object.second)
700 {
701 // Add mapping from connection to object path
702 const std::string& connection = objData.first;
Shawn McCarney8fb49dd2019-06-12 17:47:00 -0500703 (*objectMgrPaths)[connection] = objectPath;
Shawn McCarneyde629b62019-03-08 10:42:51 -0600704 BMCWEB_LOG_DEBUG << "Added mapping " << connection << " -> "
705 << objectPath;
706 }
707 }
Shawn McCarney8fb49dd2019-06-12 17:47:00 -0500708 callback(objectMgrPaths);
Shawn McCarneyde629b62019-03-08 10:42:51 -0600709 BMCWEB_LOG_DEBUG << "getObjectManagerPaths respHandler exit";
710 };
711
712 // Query mapper for all DBus object paths that implement ObjectManager
713 crow::connections::systemBus->async_method_call(
714 std::move(respHandler), "xyz.openbmc_project.ObjectMapper",
715 "/xyz/openbmc_project/object_mapper",
Ed Tanous271584a2019-07-09 16:24:22 -0700716 "xyz.openbmc_project.ObjectMapper", "GetSubTree", "/", 0, interfaces);
Shawn McCarneyde629b62019-03-08 10:42:51 -0600717 BMCWEB_LOG_DEBUG << "getObjectManagerPaths exit";
718}
719
720/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500721 * @brief Returns the Redfish State value for the specified inventory item.
722 * @param inventoryItem D-Bus inventory item associated with a sensor.
723 * @return State value for inventory item.
James Feist34dd1792019-05-17 14:10:54 -0700724 */
Ed Tanous23a21a12020-07-25 04:45:05 +0000725inline std::string getState(const InventoryItem* inventoryItem)
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500726{
727 if ((inventoryItem != nullptr) && !(inventoryItem->isPresent))
728 {
729 return "Absent";
730 }
James Feist34dd1792019-05-17 14:10:54 -0700731
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500732 return "Enabled";
733}
734
735/**
736 * @brief Returns the Redfish Health value for the specified sensor.
737 * @param sensorJson Sensor JSON object.
738 * @param interfacesDict Map of all sensor interfaces.
739 * @param inventoryItem D-Bus inventory item associated with the sensor. Will
740 * be nullptr if no associated inventory item was found.
741 * @return Health value for sensor.
742 */
Ed Tanous711ac7a2021-12-20 09:34:41 -0800743inline std::string
744 getHealth(nlohmann::json& sensorJson,
745 const dbus::utility::DBusInteracesMap& interfacesDict,
746 const InventoryItem* inventoryItem)
James Feist34dd1792019-05-17 14:10:54 -0700747{
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500748 // Get current health value (if any) in the sensor JSON object. Some JSON
749 // objects contain multiple sensors (such as PowerSupplies). We want to set
750 // the overall health to be the most severe of any of the sensors.
751 std::string currentHealth;
752 auto statusIt = sensorJson.find("Status");
753 if (statusIt != sensorJson.end())
754 {
755 auto healthIt = statusIt->find("Health");
756 if (healthIt != statusIt->end())
757 {
758 std::string* health = healthIt->get_ptr<std::string*>();
759 if (health != nullptr)
760 {
761 currentHealth = *health;
762 }
763 }
764 }
765
766 // If current health in JSON object is already Critical, return that. This
767 // should override the sensor health, which might be less severe.
768 if (currentHealth == "Critical")
769 {
770 return "Critical";
771 }
772
773 // Check if sensor has critical threshold alarm
Ed Tanous711ac7a2021-12-20 09:34:41 -0800774
Ed Tanous9eb808c2022-01-25 10:19:23 -0800775 for (const auto& [interface, values] : interfacesDict)
James Feist34dd1792019-05-17 14:10:54 -0700776 {
Ed Tanous711ac7a2021-12-20 09:34:41 -0800777 if (interface == "xyz.openbmc_project.Sensor.Threshold.Critical")
James Feist34dd1792019-05-17 14:10:54 -0700778 {
Ed Tanous9eb808c2022-01-25 10:19:23 -0800779 for (const auto& [valueName, value] : values)
James Feist34dd1792019-05-17 14:10:54 -0700780 {
Ed Tanous711ac7a2021-12-20 09:34:41 -0800781 if (valueName == "CriticalAlarmHigh" ||
782 valueName == "CriticalAlarmLow")
783 {
784 const bool* asserted = std::get_if<bool>(&value);
785 if (asserted == nullptr)
786 {
787 BMCWEB_LOG_ERROR << "Illegal sensor threshold";
788 }
789 else if (*asserted)
790 {
791 return "Critical";
792 }
793 }
James Feist34dd1792019-05-17 14:10:54 -0700794 }
795 }
796 }
797
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500798 // Check if associated inventory item is not functional
799 if ((inventoryItem != nullptr) && !(inventoryItem->isFunctional))
800 {
801 return "Critical";
802 }
803
804 // If current health in JSON object is already Warning, return that. This
805 // should override the sensor status, which might be less severe.
806 if (currentHealth == "Warning")
807 {
808 return "Warning";
809 }
810
811 // Check if sensor has warning threshold alarm
Ed Tanous9eb808c2022-01-25 10:19:23 -0800812 for (const auto& [interface, values] : interfacesDict)
James Feist34dd1792019-05-17 14:10:54 -0700813 {
Ed Tanous711ac7a2021-12-20 09:34:41 -0800814 if (interface == "xyz.openbmc_project.Sensor.Threshold.Warning")
James Feist34dd1792019-05-17 14:10:54 -0700815 {
Ed Tanous9eb808c2022-01-25 10:19:23 -0800816 for (const auto& [valueName, value] : values)
James Feist34dd1792019-05-17 14:10:54 -0700817 {
Ed Tanous711ac7a2021-12-20 09:34:41 -0800818 if (valueName == "WarningAlarmHigh" ||
819 valueName == "WarningAlarmLow")
820 {
821 const bool* asserted = std::get_if<bool>(&value);
822 if (asserted == nullptr)
823 {
824 BMCWEB_LOG_ERROR << "Illegal sensor threshold";
825 }
826 else if (*asserted)
827 {
Ed Tanousebe4d912022-01-12 12:38:17 -0800828 return "Warning";
Ed Tanous711ac7a2021-12-20 09:34:41 -0800829 }
830 }
James Feist34dd1792019-05-17 14:10:54 -0700831 }
832 }
833 }
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500834
James Feist34dd1792019-05-17 14:10:54 -0700835 return "OK";
836}
837
Ed Tanous23a21a12020-07-25 04:45:05 +0000838inline void setLedState(nlohmann::json& sensorJson,
Anthony Wilsond5005492019-07-31 16:34:17 -0500839 const InventoryItem* inventoryItem)
840{
841 if (inventoryItem != nullptr && !inventoryItem->ledObjectPath.empty())
842 {
843 switch (inventoryItem->ledState)
844 {
845 case LedState::OFF:
846 sensorJson["IndicatorLED"] = "Off";
847 break;
848 case LedState::ON:
849 sensorJson["IndicatorLED"] = "Lit";
850 break;
851 case LedState::BLINK:
852 sensorJson["IndicatorLED"] = "Blinking";
853 break;
Ed Tanous23a21a12020-07-25 04:45:05 +0000854 case LedState::UNKNOWN:
Anthony Wilsond5005492019-07-31 16:34:17 -0500855 break;
856 }
857 }
858}
859
James Feist34dd1792019-05-17 14:10:54 -0700860/**
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100861 * @brief Builds a json sensor representation of a sensor.
862 * @param sensorName The name of the sensor to be built
Gunnar Mills274fad52018-06-13 15:45:36 -0500863 * @param sensorType The type (temperature, fan_tach, etc) of the sensor to
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100864 * build
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200865 * @param sensorsAsyncResp Sensor metadata
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100866 * @param interfacesDict A dictionary of the interfaces and properties of said
867 * interfaces to be built from
868 * @param sensor_json The json object to fill
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500869 * @param inventoryItem D-Bus inventory item associated with the sensor. Will
870 * be nullptr if no associated inventory item was found.
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100871 */
Ed Tanous23a21a12020-07-25 04:45:05 +0000872inline void objectInterfacesToJson(
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100873 const std::string& sensorName, const std::string& sensorType,
Ed Tanousb5a76932020-09-29 16:16:58 -0700874 const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
Ed Tanous711ac7a2021-12-20 09:34:41 -0800875 const dbus::utility::DBusInteracesMap& interfacesDict,
Ed Tanous81ce6092020-12-17 16:54:55 +0000876 nlohmann::json& sensorJson, InventoryItem* inventoryItem)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700877{
Ed Tanous1abe55e2018-09-05 08:30:59 -0700878 // Assume values exist as is (10^0 == 1) if no scale exists
879 int64_t scaleMultiplier = 0;
Ed Tanous9eb808c2022-01-25 10:19:23 -0800880 for (const auto& [interface, values] : interfacesDict)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700881 {
Ed Tanous711ac7a2021-12-20 09:34:41 -0800882 if (interface == "xyz.openbmc_project.Sensor.Value")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700883 {
Ed Tanous9eb808c2022-01-25 10:19:23 -0800884 for (const auto& [valueName, value] : values)
Ed Tanous711ac7a2021-12-20 09:34:41 -0800885 {
886 if (valueName == "Scale")
887 {
888 const int64_t* int64Value = std::get_if<int64_t>(&value);
889 if (int64Value != nullptr)
890 {
891 scaleMultiplier = *int64Value;
892 }
893 }
894 }
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100895 }
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100896 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700897
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200898 if (sensorsAsyncResp->chassisSubNode == sensors::node::sensors)
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500899 {
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500900 // For sensors in SensorCollection we set Id instead of MemberId,
901 // including power sensors.
Ed Tanous81ce6092020-12-17 16:54:55 +0000902 sensorJson["Id"] = sensorName;
903 sensorJson["Name"] = boost::replace_all_copy(sensorName, "_", " ");
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500904 }
905 else if (sensorType != "power")
906 {
907 // Set MemberId and Name for non-power sensors. For PowerSupplies and
908 // PowerControl, those properties have more general values because
909 // multiple sensors can be stored in the same JSON object.
Ed Tanous81ce6092020-12-17 16:54:55 +0000910 sensorJson["MemberId"] = sensorName;
911 sensorJson["Name"] = boost::replace_all_copy(sensorName, "_", " ");
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500912 }
Ed Tanouse742b6c2019-05-03 15:06:53 -0700913
Ed Tanous81ce6092020-12-17 16:54:55 +0000914 sensorJson["Status"]["State"] = getState(inventoryItem);
915 sensorJson["Status"]["Health"] =
916 getHealth(sensorJson, interfacesDict, inventoryItem);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700917
918 // Parameter to set to override the type we get from dbus, and force it to
919 // int, regardless of what is available. This is used for schemas like fan,
920 // that require integers, not floats.
921 bool forceToInt = false;
922
Anthony Wilson3929aca2019-07-19 15:42:33 -0500923 nlohmann::json::json_pointer unit("/Reading");
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200924 if (sensorsAsyncResp->chassisSubNode == sensors::node::sensors)
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500925 {
Shounak Mitra2a4ba192022-06-01 23:34:12 +0000926 sensorJson["@odata.type"] = "#Sensor.v1_2_0.Sensor";
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000927
928 const std::string& readingType = sensors::toReadingType(sensorType);
929 if (readingType.empty())
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500930 {
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000931 BMCWEB_LOG_ERROR << "Redfish cannot map reading type for "
932 << sensorType;
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500933 }
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000934 else
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500935 {
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000936 sensorJson["ReadingType"] = readingType;
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500937 }
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000938
939 const std::string& readingUnits = sensors::toReadingUnits(sensorType);
940 if (readingUnits.empty())
Adrian Ambrożewiczf8ede152020-06-02 13:26:33 +0200941 {
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000942 BMCWEB_LOG_ERROR << "Redfish cannot map reading unit for "
943 << sensorType;
944 }
945 else
946 {
947 sensorJson["ReadingUnits"] = readingUnits;
Adrian Ambrożewiczf8ede152020-06-02 13:26:33 +0200948 }
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500949 }
950 else if (sensorType == "temperature")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700951 {
Anthony Wilson3929aca2019-07-19 15:42:33 -0500952 unit = "/ReadingCelsius"_json_pointer;
Ed Tanous81ce6092020-12-17 16:54:55 +0000953 sensorJson["@odata.type"] = "#Thermal.v1_3_0.Temperature";
Ed Tanous1abe55e2018-09-05 08:30:59 -0700954 // TODO(ed) Documentation says that path should be type fan_tach,
955 // implementation seems to implement fan
956 }
957 else if (sensorType == "fan" || sensorType == "fan_tach")
958 {
Anthony Wilson3929aca2019-07-19 15:42:33 -0500959 unit = "/Reading"_json_pointer;
Ed Tanous81ce6092020-12-17 16:54:55 +0000960 sensorJson["ReadingUnits"] = "RPM";
961 sensorJson["@odata.type"] = "#Thermal.v1_3_0.Fan";
962 setLedState(sensorJson, inventoryItem);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700963 forceToInt = true;
964 }
Ed Tanous6f6d0d32018-10-12 11:16:43 -0700965 else if (sensorType == "fan_pwm")
966 {
Anthony Wilson3929aca2019-07-19 15:42:33 -0500967 unit = "/Reading"_json_pointer;
Ed Tanous81ce6092020-12-17 16:54:55 +0000968 sensorJson["ReadingUnits"] = "Percent";
969 sensorJson["@odata.type"] = "#Thermal.v1_3_0.Fan";
970 setLedState(sensorJson, inventoryItem);
Ed Tanous6f6d0d32018-10-12 11:16:43 -0700971 forceToInt = true;
972 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700973 else if (sensorType == "voltage")
974 {
Anthony Wilson3929aca2019-07-19 15:42:33 -0500975 unit = "/ReadingVolts"_json_pointer;
Ed Tanous81ce6092020-12-17 16:54:55 +0000976 sensorJson["@odata.type"] = "#Power.v1_0_0.Voltage";
Ed Tanous1abe55e2018-09-05 08:30:59 -0700977 }
Ed Tanous2474adf2018-09-05 16:31:16 -0700978 else if (sensorType == "power")
979 {
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700980 std::string sensorNameLower =
981 boost::algorithm::to_lower_copy(sensorName);
982
Ed Tanous55f79e62022-01-25 11:26:16 -0800983 if (sensorName == "total_power")
Eddie James028f7eb2019-05-17 21:24:36 +0000984 {
Ed Tanous81ce6092020-12-17 16:54:55 +0000985 sensorJson["@odata.type"] = "#Power.v1_0_0.PowerControl";
Gunnar Mills7ab06f42019-07-02 13:07:16 -0500986 // Put multiple "sensors" into a single PowerControl, so have
987 // generic names for MemberId and Name. Follows Redfish mockup.
Ed Tanous81ce6092020-12-17 16:54:55 +0000988 sensorJson["MemberId"] = "0";
989 sensorJson["Name"] = "Chassis Power Control";
Anthony Wilson3929aca2019-07-19 15:42:33 -0500990 unit = "/PowerConsumedWatts"_json_pointer;
Eddie James028f7eb2019-05-17 21:24:36 +0000991 }
992 else if (sensorNameLower.find("input") != std::string::npos)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700993 {
Anthony Wilson3929aca2019-07-19 15:42:33 -0500994 unit = "/PowerInputWatts"_json_pointer;
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700995 }
996 else
997 {
Anthony Wilson3929aca2019-07-19 15:42:33 -0500998 unit = "/PowerOutputWatts"_json_pointer;
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700999 }
Ed Tanous2474adf2018-09-05 16:31:16 -07001000 }
Ed Tanous1abe55e2018-09-05 08:30:59 -07001001 else
1002 {
1003 BMCWEB_LOG_ERROR << "Redfish cannot map object type for " << sensorName;
1004 return;
1005 }
1006 // Map of dbus interface name, dbus property name and redfish property_name
Anthony Wilson3929aca2019-07-19 15:42:33 -05001007 std::vector<
1008 std::tuple<const char*, const char*, nlohmann::json::json_pointer>>
1009 properties;
Ed Tanous1abe55e2018-09-05 08:30:59 -07001010 properties.reserve(7);
1011
1012 properties.emplace_back("xyz.openbmc_project.Sensor.Value", "Value", unit);
Shawn McCarneyde629b62019-03-08 10:42:51 -06001013
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02001014 if (sensorsAsyncResp->chassisSubNode == sensors::node::sensors)
Anthony Wilson3929aca2019-07-19 15:42:33 -05001015 {
1016 properties.emplace_back(
1017 "xyz.openbmc_project.Sensor.Threshold.Warning", "WarningHigh",
1018 "/Thresholds/UpperCaution/Reading"_json_pointer);
1019 properties.emplace_back(
1020 "xyz.openbmc_project.Sensor.Threshold.Warning", "WarningLow",
1021 "/Thresholds/LowerCaution/Reading"_json_pointer);
1022 properties.emplace_back(
1023 "xyz.openbmc_project.Sensor.Threshold.Critical", "CriticalHigh",
1024 "/Thresholds/UpperCritical/Reading"_json_pointer);
1025 properties.emplace_back(
1026 "xyz.openbmc_project.Sensor.Threshold.Critical", "CriticalLow",
1027 "/Thresholds/LowerCritical/Reading"_json_pointer);
1028 }
1029 else if (sensorType != "power")
Shawn McCarneyde629b62019-03-08 10:42:51 -06001030 {
1031 properties.emplace_back("xyz.openbmc_project.Sensor.Threshold.Warning",
Anthony Wilson3929aca2019-07-19 15:42:33 -05001032 "WarningHigh",
1033 "/UpperThresholdNonCritical"_json_pointer);
Shawn McCarneyde629b62019-03-08 10:42:51 -06001034 properties.emplace_back("xyz.openbmc_project.Sensor.Threshold.Warning",
Anthony Wilson3929aca2019-07-19 15:42:33 -05001035 "WarningLow",
1036 "/LowerThresholdNonCritical"_json_pointer);
Shawn McCarneyde629b62019-03-08 10:42:51 -06001037 properties.emplace_back("xyz.openbmc_project.Sensor.Threshold.Critical",
Anthony Wilson3929aca2019-07-19 15:42:33 -05001038 "CriticalHigh",
1039 "/UpperThresholdCritical"_json_pointer);
Shawn McCarneyde629b62019-03-08 10:42:51 -06001040 properties.emplace_back("xyz.openbmc_project.Sensor.Threshold.Critical",
Anthony Wilson3929aca2019-07-19 15:42:33 -05001041 "CriticalLow",
1042 "/LowerThresholdCritical"_json_pointer);
Shawn McCarneyde629b62019-03-08 10:42:51 -06001043 }
Ed Tanous1abe55e2018-09-05 08:30:59 -07001044
Ed Tanous2474adf2018-09-05 16:31:16 -07001045 // TODO Need to get UpperThresholdFatal and LowerThresholdFatal
1046
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02001047 if (sensorsAsyncResp->chassisSubNode == sensors::node::sensors)
Anthony Wilson95a3eca2019-06-11 10:44:47 -05001048 {
1049 properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MinValue",
Anthony Wilson3929aca2019-07-19 15:42:33 -05001050 "/ReadingRangeMin"_json_pointer);
Anthony Wilson95a3eca2019-06-11 10:44:47 -05001051 properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MaxValue",
Anthony Wilson3929aca2019-07-19 15:42:33 -05001052 "/ReadingRangeMax"_json_pointer);
Anthony Wilson95a3eca2019-06-11 10:44:47 -05001053 }
1054 else if (sensorType == "temperature")
Ed Tanous1abe55e2018-09-05 08:30:59 -07001055 {
1056 properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MinValue",
Anthony Wilson3929aca2019-07-19 15:42:33 -05001057 "/MinReadingRangeTemp"_json_pointer);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001058 properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MaxValue",
Anthony Wilson3929aca2019-07-19 15:42:33 -05001059 "/MaxReadingRangeTemp"_json_pointer);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001060 }
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001061 else if (sensorType != "power")
Ed Tanous1abe55e2018-09-05 08:30:59 -07001062 {
1063 properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MinValue",
Anthony Wilson3929aca2019-07-19 15:42:33 -05001064 "/MinReadingRange"_json_pointer);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001065 properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MaxValue",
Anthony Wilson3929aca2019-07-19 15:42:33 -05001066 "/MaxReadingRange"_json_pointer);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001067 }
1068
Anthony Wilson3929aca2019-07-19 15:42:33 -05001069 for (const std::tuple<const char*, const char*,
1070 nlohmann::json::json_pointer>& p : properties)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001071 {
Ed Tanous55f79e62022-01-25 11:26:16 -08001072 for (const auto& [interface, values] : interfacesDict)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001073 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001074 if (interface != std::get<0>(p))
Ed Tanous1abe55e2018-09-05 08:30:59 -07001075 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001076 continue;
1077 }
Ed Tanous55f79e62022-01-25 11:26:16 -08001078 for (const auto& [valueName, valueVariant] : values)
Ed Tanous711ac7a2021-12-20 09:34:41 -08001079 {
1080 if (valueName != std::get<1>(p))
1081 {
1082 continue;
1083 }
Anthony Wilson3929aca2019-07-19 15:42:33 -05001084
1085 // The property we want to set may be nested json, so use
1086 // a json_pointer for easy indexing into the json structure.
1087 const nlohmann::json::json_pointer& key = std::get<2>(p);
1088
Ed Tanous1abe55e2018-09-05 08:30:59 -07001089 // Attempt to pull the int64 directly
Ed Tanousabf2add2019-01-22 16:40:12 -08001090 const int64_t* int64Value = std::get_if<int64_t>(&valueVariant);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001091
Ed Tanousabf2add2019-01-22 16:40:12 -08001092 const double* doubleValue = std::get_if<double>(&valueVariant);
Eddie James028f7eb2019-05-17 21:24:36 +00001093 const uint32_t* uValue = std::get_if<uint32_t>(&valueVariant);
Ed Tanous6f6d0d32018-10-12 11:16:43 -07001094 double temp = 0.0;
1095 if (int64Value != nullptr)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001096 {
Ed Tanous271584a2019-07-09 16:24:22 -07001097 temp = static_cast<double>(*int64Value);
Ed Tanous6f6d0d32018-10-12 11:16:43 -07001098 }
1099 else if (doubleValue != nullptr)
1100 {
1101 temp = *doubleValue;
1102 }
Eddie James028f7eb2019-05-17 21:24:36 +00001103 else if (uValue != nullptr)
1104 {
1105 temp = *uValue;
1106 }
Ed Tanous6f6d0d32018-10-12 11:16:43 -07001107 else
1108 {
1109 BMCWEB_LOG_ERROR
1110 << "Got value interface that wasn't int or double";
1111 continue;
1112 }
1113 temp = temp * std::pow(10, scaleMultiplier);
1114 if (forceToInt)
1115 {
Ed Tanous81ce6092020-12-17 16:54:55 +00001116 sensorJson[key] = static_cast<int64_t>(temp);
Ed Tanous6f6d0d32018-10-12 11:16:43 -07001117 }
1118 else
1119 {
Ed Tanous81ce6092020-12-17 16:54:55 +00001120 sensorJson[key] = temp;
Ed Tanous1abe55e2018-09-05 08:30:59 -07001121 }
1122 }
1123 }
1124 }
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02001125
Ed Tanous81ce6092020-12-17 16:54:55 +00001126 sensorsAsyncResp->addMetadata(sensorJson, unit.to_string(),
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02001127 "/xyz/openbmc_project/sensors/" + sensorType +
1128 "/" + sensorName);
1129
Ed Tanous1abe55e2018-09-05 08:30:59 -07001130 BMCWEB_LOG_DEBUG << "Added sensor " << sensorName;
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +01001131}
1132
Ed Tanousb5a76932020-09-29 16:16:58 -07001133inline void populateFanRedundancy(
1134 const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp)
James Feist8bd25cc2019-03-15 15:14:00 -07001135{
1136 crow::connections::systemBus->async_method_call(
Ed Tanousb9d36b42022-02-26 21:42:46 -08001137 [sensorsAsyncResp](
1138 const boost::system::error_code ec,
1139 const dbus::utility::MapperGetSubTreeResponse& resp) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001140 if (ec)
1141 {
1142 return; // don't have to have this interface
1143 }
1144 for (const std::pair<
1145 std::string,
1146 std::vector<std::pair<std::string, std::vector<std::string>>>>&
1147 pathPair : resp)
1148 {
1149 const std::string& path = pathPair.first;
1150 const std::vector<std::pair<std::string, std::vector<std::string>>>&
1151 objDict = pathPair.second;
1152 if (objDict.empty())
James Feist8bd25cc2019-03-15 15:14:00 -07001153 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001154 continue; // this should be impossible
James Feist8bd25cc2019-03-15 15:14:00 -07001155 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001156
1157 const std::string& owner = objDict.begin()->first;
1158 sdbusplus::asio::getProperty<std::vector<std::string>>(
1159 *crow::connections::systemBus,
1160 "xyz.openbmc_project.ObjectMapper", path + "/chassis",
1161 "xyz.openbmc_project.Association", "endpoints",
1162 [path, owner,
1163 sensorsAsyncResp](const boost::system::error_code e,
1164 const std::vector<std::string>& endpoints) {
1165 if (e)
James Feist8bd25cc2019-03-15 15:14:00 -07001166 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001167 return; // if they don't have an association we
1168 // can't tell what chassis is
James Feist8bd25cc2019-03-15 15:14:00 -07001169 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001170 auto found =
1171 std::find_if(endpoints.begin(), endpoints.end(),
1172 [sensorsAsyncResp](const std::string& entry) {
1173 return entry.find(sensorsAsyncResp->chassisId) !=
1174 std::string::npos;
1175 });
James Feist8bd25cc2019-03-15 15:14:00 -07001176
Ed Tanous002d39b2022-05-31 08:59:27 -07001177 if (found == endpoints.end())
1178 {
1179 return;
1180 }
1181 crow::connections::systemBus->async_method_call(
1182 [path, sensorsAsyncResp](
1183 const boost::system::error_code& err,
Nan Zhoufe04d492022-06-22 17:10:41 +00001184 const std::map<std::string,
1185 dbus::utility::DbusVariantType>& ret) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001186 if (err)
1187 {
1188 return; // don't have to have this
1189 // interface
1190 }
1191 auto findFailures = ret.find("AllowedFailures");
1192 auto findCollection = ret.find("Collection");
1193 auto findStatus = ret.find("Status");
1194
1195 if (findFailures == ret.end() ||
1196 findCollection == ret.end() || findStatus == ret.end())
1197 {
1198 BMCWEB_LOG_ERROR << "Invalid redundancy interface";
1199 messages::internalError(
1200 sensorsAsyncResp->asyncResp->res);
1201 return;
1202 }
1203
1204 const uint8_t* allowedFailures =
1205 std::get_if<uint8_t>(&(findFailures->second));
1206 const std::vector<std::string>* collection =
1207 std::get_if<std::vector<std::string>>(
1208 &(findCollection->second));
1209 const std::string* status =
1210 std::get_if<std::string>(&(findStatus->second));
1211
1212 if (allowedFailures == nullptr || collection == nullptr ||
1213 status == nullptr)
1214 {
1215
1216 BMCWEB_LOG_ERROR
1217 << "Invalid redundancy interface types";
1218 messages::internalError(
1219 sensorsAsyncResp->asyncResp->res);
1220 return;
1221 }
1222 sdbusplus::message::object_path objectPath(path);
1223 std::string name = objectPath.filename();
1224 if (name.empty())
1225 {
1226 // this should be impossible
1227 messages::internalError(
1228 sensorsAsyncResp->asyncResp->res);
1229 return;
1230 }
1231 std::replace(name.begin(), name.end(), '_', ' ');
1232
1233 std::string health;
1234
1235 if (boost::ends_with(*status, "Full"))
1236 {
1237 health = "OK";
1238 }
1239 else if (boost::ends_with(*status, "Degraded"))
1240 {
1241 health = "Warning";
1242 }
1243 else
1244 {
1245 health = "Critical";
1246 }
1247 nlohmann::json::array_t redfishCollection;
1248 const auto& fanRedfish =
1249 sensorsAsyncResp->asyncResp->res.jsonValue["Fans"];
1250 for (const std::string& item : *collection)
1251 {
Ed Tanous8a592812022-06-04 09:06:59 -07001252 sdbusplus::message::object_path itemPath(item);
1253 std::string itemName = itemPath.filename();
Ed Tanous002d39b2022-05-31 08:59:27 -07001254 if (itemName.empty())
James Feist8bd25cc2019-03-15 15:14:00 -07001255 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001256 continue;
James Feist8bd25cc2019-03-15 15:14:00 -07001257 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001258 /*
1259 todo(ed): merge patch that fixes the names
1260 std::replace(itemName.begin(),
1261 itemName.end(), '_', ' ');*/
1262 auto schemaItem =
1263 std::find_if(fanRedfish.begin(), fanRedfish.end(),
1264 [itemName](const nlohmann::json& fan) {
1265 return fan["MemberId"] == itemName;
James Feist8bd25cc2019-03-15 15:14:00 -07001266 });
Ed Tanous002d39b2022-05-31 08:59:27 -07001267 if (schemaItem != fanRedfish.end())
James Feist8bd25cc2019-03-15 15:14:00 -07001268 {
Ed Tanous8a592812022-06-04 09:06:59 -07001269 nlohmann::json::object_t collectionId;
1270 collectionId["@odata.id"] =
Ed Tanous002d39b2022-05-31 08:59:27 -07001271 (*schemaItem)["@odata.id"];
1272 redfishCollection.emplace_back(
Ed Tanous8a592812022-06-04 09:06:59 -07001273 std::move(collectionId));
Ed Tanous002d39b2022-05-31 08:59:27 -07001274 }
1275 else
1276 {
1277 BMCWEB_LOG_ERROR << "failed to find fan in schema";
1278 messages::internalError(
1279 sensorsAsyncResp->asyncResp->res);
James Feist8bd25cc2019-03-15 15:14:00 -07001280 return;
1281 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001282 }
James Feist8bd25cc2019-03-15 15:14:00 -07001283
Ed Tanous002d39b2022-05-31 08:59:27 -07001284 size_t minNumNeeded =
1285 collection->empty()
1286 ? 0
1287 : collection->size() - *allowedFailures;
1288 nlohmann::json& jResp = sensorsAsyncResp->asyncResp->res
1289 .jsonValue["Redundancy"];
James Feist8bd25cc2019-03-15 15:14:00 -07001290
Ed Tanous002d39b2022-05-31 08:59:27 -07001291 nlohmann::json::object_t redundancy;
1292 redundancy["@odata.id"] =
1293 "/redfish/v1/Chassis/" + sensorsAsyncResp->chassisId +
1294 "/" + sensorsAsyncResp->chassisSubNode +
1295 "#/Redundancy/" + std::to_string(jResp.size());
1296 redundancy["@odata.type"] = "#Redundancy.v1_3_2.Redundancy";
1297 redundancy["MinNumNeeded"] = minNumNeeded;
1298 redundancy["MemberId"] = name;
1299 redundancy["Mode"] = "N+m";
1300 redundancy["Name"] = name;
1301 redundancy["RedundancySet"] = redfishCollection;
1302 redundancy["Status"]["Health"] = health;
1303 redundancy["Status"]["State"] = "Enabled";
James Feist8bd25cc2019-03-15 15:14:00 -07001304
Ed Tanous002d39b2022-05-31 08:59:27 -07001305 jResp.push_back(std::move(redundancy));
1306 },
1307 owner, path, "org.freedesktop.DBus.Properties", "GetAll",
1308 "xyz.openbmc_project.Control.FanRedundancy");
1309 });
1310 }
James Feist8bd25cc2019-03-15 15:14:00 -07001311 },
1312 "xyz.openbmc_project.ObjectMapper",
1313 "/xyz/openbmc_project/object_mapper",
1314 "xyz.openbmc_project.ObjectMapper", "GetSubTree",
1315 "/xyz/openbmc_project/control", 2,
1316 std::array<const char*, 1>{
1317 "xyz.openbmc_project.Control.FanRedundancy"});
1318}
1319
Ed Tanousb5a76932020-09-29 16:16:58 -07001320inline void
Ed Tanous81ce6092020-12-17 16:54:55 +00001321 sortJSONResponse(const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07001322{
zhanghch058d1b46d2021-04-01 11:18:24 +08001323 nlohmann::json& response = sensorsAsyncResp->asyncResp->res.jsonValue;
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07001324 std::array<std::string, 2> sensorHeaders{"Temperatures", "Fans"};
Ed Tanous81ce6092020-12-17 16:54:55 +00001325 if (sensorsAsyncResp->chassisSubNode == sensors::node::power)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07001326 {
1327 sensorHeaders = {"Voltages", "PowerSupplies"};
1328 }
1329 for (const std::string& sensorGroup : sensorHeaders)
1330 {
1331 nlohmann::json::iterator entry = response.find(sensorGroup);
1332 if (entry != response.end())
1333 {
1334 std::sort(entry->begin(), entry->end(),
Ed Tanous02cad962022-06-30 16:50:15 -07001335 [](const nlohmann::json& c1, const nlohmann::json& c2) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001336 return c1["Name"] < c2["Name"];
1337 });
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07001338
1339 // add the index counts to the end of each entry
1340 size_t count = 0;
1341 for (nlohmann::json& sensorJson : *entry)
1342 {
1343 nlohmann::json::iterator odata = sensorJson.find("@odata.id");
1344 if (odata == sensorJson.end())
1345 {
1346 continue;
1347 }
1348 std::string* value = odata->get_ptr<std::string*>();
1349 if (value != nullptr)
1350 {
1351 *value += std::to_string(count);
1352 count++;
Ed Tanous81ce6092020-12-17 16:54:55 +00001353 sensorsAsyncResp->updateUri(sensorJson["Name"], *value);
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07001354 }
1355 }
1356 }
1357 }
1358}
1359
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +01001360/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001361 * @brief Finds the inventory item with the specified object path.
1362 * @param inventoryItems D-Bus inventory items associated with sensors.
1363 * @param invItemObjPath D-Bus object path of inventory item.
1364 * @return Inventory item within vector, or nullptr if no match found.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001365 */
Ed Tanous23a21a12020-07-25 04:45:05 +00001366inline InventoryItem* findInventoryItem(
Ed Tanousb5a76932020-09-29 16:16:58 -07001367 const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems,
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001368 const std::string& invItemObjPath)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001369{
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001370 for (InventoryItem& inventoryItem : *inventoryItems)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001371 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001372 if (inventoryItem.objectPath == invItemObjPath)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001373 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001374 return &inventoryItem;
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001375 }
1376 }
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001377 return nullptr;
1378}
1379
1380/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001381 * @brief Finds the inventory item associated with the specified sensor.
1382 * @param inventoryItems D-Bus inventory items associated with sensors.
1383 * @param sensorObjPath D-Bus object path of sensor.
1384 * @return Inventory item within vector, or nullptr if no match found.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001385 */
Ed Tanous23a21a12020-07-25 04:45:05 +00001386inline InventoryItem* findInventoryItemForSensor(
Ed Tanousb5a76932020-09-29 16:16:58 -07001387 const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems,
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001388 const std::string& sensorObjPath)
1389{
1390 for (InventoryItem& inventoryItem : *inventoryItems)
1391 {
1392 if (inventoryItem.sensors.count(sensorObjPath) > 0)
1393 {
1394 return &inventoryItem;
1395 }
1396 }
1397 return nullptr;
1398}
1399
1400/**
Anthony Wilsond5005492019-07-31 16:34:17 -05001401 * @brief Finds the inventory item associated with the specified led path.
1402 * @param inventoryItems D-Bus inventory items associated with sensors.
1403 * @param ledObjPath D-Bus object path of led.
1404 * @return Inventory item within vector, or nullptr if no match found.
1405 */
1406inline InventoryItem*
1407 findInventoryItemForLed(std::vector<InventoryItem>& inventoryItems,
1408 const std::string& ledObjPath)
1409{
1410 for (InventoryItem& inventoryItem : inventoryItems)
1411 {
1412 if (inventoryItem.ledObjectPath == ledObjPath)
1413 {
1414 return &inventoryItem;
1415 }
1416 }
1417 return nullptr;
1418}
1419
1420/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001421 * @brief Adds inventory item and associated sensor to specified vector.
1422 *
1423 * Adds a new InventoryItem to the vector if necessary. Searches for an
1424 * existing InventoryItem with the specified object path. If not found, one is
1425 * added to the vector.
1426 *
1427 * Next, the specified sensor is added to the set of sensors associated with the
1428 * InventoryItem.
1429 *
1430 * @param inventoryItems D-Bus inventory items associated with sensors.
1431 * @param invItemObjPath D-Bus object path of inventory item.
1432 * @param sensorObjPath D-Bus object path of sensor
1433 */
Ed Tanousb5a76932020-09-29 16:16:58 -07001434inline void addInventoryItem(
1435 const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems,
1436 const std::string& invItemObjPath, const std::string& sensorObjPath)
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001437{
1438 // Look for inventory item in vector
1439 InventoryItem* inventoryItem =
1440 findInventoryItem(inventoryItems, invItemObjPath);
1441
1442 // If inventory item doesn't exist in vector, add it
1443 if (inventoryItem == nullptr)
1444 {
1445 inventoryItems->emplace_back(invItemObjPath);
1446 inventoryItem = &(inventoryItems->back());
1447 }
1448
1449 // Add sensor to set of sensors associated with inventory item
1450 inventoryItem->sensors.emplace(sensorObjPath);
1451}
1452
1453/**
1454 * @brief Stores D-Bus data in the specified inventory item.
1455 *
1456 * Finds D-Bus data in the specified map of interfaces. Stores the data in the
1457 * specified InventoryItem.
1458 *
1459 * This data is later used to provide sensor property values in the JSON
1460 * response.
1461 *
1462 * @param inventoryItem Inventory item where data will be stored.
1463 * @param interfacesDict Map containing D-Bus interfaces and their properties
1464 * for the specified inventory item.
1465 */
Ed Tanous23a21a12020-07-25 04:45:05 +00001466inline void storeInventoryItemData(
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001467 InventoryItem& inventoryItem,
Ed Tanous711ac7a2021-12-20 09:34:41 -08001468 const dbus::utility::DBusInteracesMap& interfacesDict)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001469{
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001470 // Get properties from Inventory.Item interface
Ed Tanous711ac7a2021-12-20 09:34:41 -08001471
Ed Tanous9eb808c2022-01-25 10:19:23 -08001472 for (const auto& [interface, values] : interfacesDict)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001473 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001474 if (interface == "xyz.openbmc_project.Inventory.Item")
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001475 {
Ed Tanous9eb808c2022-01-25 10:19:23 -08001476 for (const auto& [name, dbusValue] : values)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001477 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001478 if (name == "Present")
1479 {
1480 const bool* value = std::get_if<bool>(&dbusValue);
1481 if (value != nullptr)
1482 {
1483 inventoryItem.isPresent = *value;
1484 }
1485 }
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001486 }
1487 }
Ed Tanous711ac7a2021-12-20 09:34:41 -08001488 // Check if Inventory.Item.PowerSupply interface is present
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001489
Ed Tanous711ac7a2021-12-20 09:34:41 -08001490 if (interface == "xyz.openbmc_project.Inventory.Item.PowerSupply")
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001491 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001492 inventoryItem.isPowerSupply = true;
1493 }
1494
1495 // Get properties from Inventory.Decorator.Asset interface
1496 if (interface == "xyz.openbmc_project.Inventory.Decorator.Asset")
1497 {
Ed Tanous9eb808c2022-01-25 10:19:23 -08001498 for (const auto& [name, dbusValue] : values)
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001499 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001500 if (name == "Manufacturer")
1501 {
1502 const std::string* value =
1503 std::get_if<std::string>(&dbusValue);
1504 if (value != nullptr)
1505 {
1506 inventoryItem.manufacturer = *value;
1507 }
1508 }
1509 if (name == "Model")
1510 {
1511 const std::string* value =
1512 std::get_if<std::string>(&dbusValue);
1513 if (value != nullptr)
1514 {
1515 inventoryItem.model = *value;
1516 }
1517 }
1518 if (name == "SerialNumber")
1519 {
1520 const std::string* value =
1521 std::get_if<std::string>(&dbusValue);
1522 if (value != nullptr)
1523 {
1524 inventoryItem.serialNumber = *value;
1525 }
1526 }
1527 if (name == "PartNumber")
1528 {
1529 const std::string* value =
1530 std::get_if<std::string>(&dbusValue);
1531 if (value != nullptr)
1532 {
1533 inventoryItem.partNumber = *value;
1534 }
1535 }
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001536 }
1537 }
1538
Ed Tanous711ac7a2021-12-20 09:34:41 -08001539 if (interface ==
1540 "xyz.openbmc_project.State.Decorator.OperationalStatus")
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001541 {
Ed Tanous9eb808c2022-01-25 10:19:23 -08001542 for (const auto& [name, dbusValue] : values)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001543 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001544 if (name == "Functional")
1545 {
1546 const bool* value = std::get_if<bool>(&dbusValue);
1547 if (value != nullptr)
1548 {
1549 inventoryItem.isFunctional = *value;
1550 }
1551 }
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001552 }
1553 }
1554 }
1555}
1556
1557/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001558 * @brief Gets D-Bus data for inventory items associated with sensors.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001559 *
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001560 * Uses the specified connections (services) to obtain D-Bus data for inventory
1561 * items associated with sensors. Stores the resulting data in the
1562 * inventoryItems vector.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001563 *
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001564 * This data is later used to provide sensor property values in the JSON
1565 * response.
1566 *
1567 * Finds the inventory item data asynchronously. Invokes callback when data has
1568 * been obtained.
1569 *
1570 * The callback must have the following signature:
1571 * @code
Anthony Wilsond5005492019-07-31 16:34:17 -05001572 * callback(void)
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001573 * @endcode
1574 *
1575 * This function is called recursively, obtaining data asynchronously from one
1576 * connection in each call. This ensures the callback is not invoked until the
1577 * last asynchronous function has completed.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001578 *
1579 * @param sensorsAsyncResp Pointer to object holding response data.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001580 * @param inventoryItems D-Bus inventory items associated with sensors.
1581 * @param invConnections Connections that provide data for the inventory items.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001582 * @param objectMgrPaths Mappings from connection name to DBus object path that
1583 * implements ObjectManager.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001584 * @param callback Callback to invoke when inventory data has been obtained.
1585 * @param invConnectionsIndex Current index in invConnections. Only specified
1586 * in recursive calls to this function.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001587 */
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001588template <typename Callback>
1589static void getInventoryItemsData(
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001590 std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001591 std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
Nan Zhoufe04d492022-06-22 17:10:41 +00001592 std::shared_ptr<std::set<std::string>> invConnections,
1593 std::shared_ptr<std::map<std::string, std::string>> objectMgrPaths,
Ed Tanous271584a2019-07-09 16:24:22 -07001594 Callback&& callback, size_t invConnectionsIndex = 0)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001595{
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001596 BMCWEB_LOG_DEBUG << "getInventoryItemsData enter";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001597
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001598 // If no more connections left, call callback
1599 if (invConnectionsIndex >= invConnections->size())
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001600 {
Anthony Wilsond5005492019-07-31 16:34:17 -05001601 callback();
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001602 BMCWEB_LOG_DEBUG << "getInventoryItemsData exit";
1603 return;
1604 }
1605
1606 // Get inventory item data from current connection
Nan Zhoufe04d492022-06-22 17:10:41 +00001607 auto it = invConnections->begin();
1608 std::advance(it, invConnectionsIndex);
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001609 if (it != invConnections->end())
1610 {
1611 const std::string& invConnection = *it;
1612
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001613 // Response handler for GetManagedObjects
Ed Tanous002d39b2022-05-31 08:59:27 -07001614 auto respHandler =
1615 [sensorsAsyncResp, inventoryItems, invConnections, objectMgrPaths,
Ed Tanous02cad962022-06-30 16:50:15 -07001616 callback{std::forward<Callback>(callback)}, invConnectionsIndex](
1617 const boost::system::error_code ec,
1618 const dbus::utility::ManagedObjectType& resp) {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001619 BMCWEB_LOG_DEBUG << "getInventoryItemsData respHandler enter";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001620 if (ec)
1621 {
1622 BMCWEB_LOG_ERROR
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001623 << "getInventoryItemsData respHandler DBus error " << ec;
zhanghch058d1b46d2021-04-01 11:18:24 +08001624 messages::internalError(sensorsAsyncResp->asyncResp->res);
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001625 return;
1626 }
1627
1628 // Loop through returned object paths
1629 for (const auto& objDictEntry : resp)
1630 {
1631 const std::string& objPath =
1632 static_cast<const std::string&>(objDictEntry.first);
1633
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001634 // If this object path is one of the specified inventory items
1635 InventoryItem* inventoryItem =
1636 findInventoryItem(inventoryItems, objPath);
1637 if (inventoryItem != nullptr)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001638 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001639 // Store inventory data in InventoryItem
1640 storeInventoryItemData(*inventoryItem, objDictEntry.second);
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001641 }
1642 }
1643
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001644 // Recurse to get inventory item data from next connection
1645 getInventoryItemsData(sensorsAsyncResp, inventoryItems,
1646 invConnections, objectMgrPaths,
1647 std::move(callback), invConnectionsIndex + 1);
1648
1649 BMCWEB_LOG_DEBUG << "getInventoryItemsData respHandler exit";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001650 };
1651
1652 // Find DBus object path that implements ObjectManager for the current
1653 // connection. If no mapping found, default to "/".
1654 auto iter = objectMgrPaths->find(invConnection);
1655 const std::string& objectMgrPath =
1656 (iter != objectMgrPaths->end()) ? iter->second : "/";
1657 BMCWEB_LOG_DEBUG << "ObjectManager path for " << invConnection << " is "
1658 << objectMgrPath;
1659
1660 // Get all object paths and their interfaces for current connection
1661 crow::connections::systemBus->async_method_call(
1662 std::move(respHandler), invConnection, objectMgrPath,
1663 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
1664 }
1665
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001666 BMCWEB_LOG_DEBUG << "getInventoryItemsData exit";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001667}
1668
1669/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001670 * @brief Gets connections that provide D-Bus data for inventory items.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001671 *
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001672 * Gets the D-Bus connections (services) that provide data for the inventory
1673 * items that are associated with sensors.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001674 *
1675 * Finds the connections asynchronously. Invokes callback when information has
1676 * been obtained.
1677 *
1678 * The callback must have the following signature:
1679 * @code
Nan Zhoufe04d492022-06-22 17:10:41 +00001680 * callback(std::shared_ptr<std::set<std::string>> invConnections)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001681 * @endcode
1682 *
1683 * @param sensorsAsyncResp Pointer to object holding response data.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001684 * @param inventoryItems D-Bus inventory items associated with sensors.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001685 * @param callback Callback to invoke when connections have been obtained.
1686 */
1687template <typename Callback>
1688static void getInventoryItemsConnections(
Ed Tanousb5a76932020-09-29 16:16:58 -07001689 const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
1690 const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems,
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001691 Callback&& callback)
1692{
1693 BMCWEB_LOG_DEBUG << "getInventoryItemsConnections enter";
1694
1695 const std::string path = "/xyz/openbmc_project/inventory";
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001696 const std::array<std::string, 4> interfaces = {
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001697 "xyz.openbmc_project.Inventory.Item",
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001698 "xyz.openbmc_project.Inventory.Item.PowerSupply",
1699 "xyz.openbmc_project.Inventory.Decorator.Asset",
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001700 "xyz.openbmc_project.State.Decorator.OperationalStatus"};
1701
1702 // Response handler for parsing output from GetSubTree
Ed Tanous002d39b2022-05-31 08:59:27 -07001703 auto respHandler =
1704 [callback{std::forward<Callback>(callback)}, sensorsAsyncResp,
1705 inventoryItems](
1706 const boost::system::error_code ec,
1707 const dbus::utility::MapperGetSubTreeResponse& subtree) {
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001708 BMCWEB_LOG_DEBUG << "getInventoryItemsConnections respHandler enter";
1709 if (ec)
1710 {
zhanghch058d1b46d2021-04-01 11:18:24 +08001711 messages::internalError(sensorsAsyncResp->asyncResp->res);
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001712 BMCWEB_LOG_ERROR
1713 << "getInventoryItemsConnections respHandler DBus error " << ec;
1714 return;
1715 }
1716
1717 // Make unique list of connections for desired inventory items
Nan Zhoufe04d492022-06-22 17:10:41 +00001718 std::shared_ptr<std::set<std::string>> invConnections =
1719 std::make_shared<std::set<std::string>>();
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001720
1721 // Loop through objects from GetSubTree
1722 for (const std::pair<
1723 std::string,
1724 std::vector<std::pair<std::string, std::vector<std::string>>>>&
1725 object : subtree)
1726 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001727 // Check if object path is one of the specified inventory items
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001728 const std::string& objPath = object.first;
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001729 if (findInventoryItem(inventoryItems, objPath) != nullptr)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001730 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001731 // Store all connections to inventory item
1732 for (const std::pair<std::string, std::vector<std::string>>&
1733 objData : object.second)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001734 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001735 const std::string& invConnection = objData.first;
1736 invConnections->insert(invConnection);
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001737 }
1738 }
1739 }
Anthony Wilsond5005492019-07-31 16:34:17 -05001740
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001741 callback(invConnections);
1742 BMCWEB_LOG_DEBUG << "getInventoryItemsConnections respHandler exit";
1743 };
1744
1745 // Make call to ObjectMapper to find all inventory items
1746 crow::connections::systemBus->async_method_call(
1747 std::move(respHandler), "xyz.openbmc_project.ObjectMapper",
1748 "/xyz/openbmc_project/object_mapper",
1749 "xyz.openbmc_project.ObjectMapper", "GetSubTree", path, 0, interfaces);
1750 BMCWEB_LOG_DEBUG << "getInventoryItemsConnections exit";
1751}
1752
1753/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001754 * @brief Gets associations from sensors to inventory items.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001755 *
1756 * Looks for ObjectMapper associations from the specified sensors to related
Anthony Wilsond5005492019-07-31 16:34:17 -05001757 * inventory items. Then finds the associations from those inventory items to
1758 * their LEDs, if any.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001759 *
1760 * Finds the inventory items asynchronously. Invokes callback when information
1761 * has been obtained.
1762 *
1763 * The callback must have the following signature:
1764 * @code
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001765 * callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001766 * @endcode
1767 *
1768 * @param sensorsAsyncResp Pointer to object holding response data.
1769 * @param sensorNames All sensors within the current chassis.
1770 * @param objectMgrPaths Mappings from connection name to DBus object path that
1771 * implements ObjectManager.
1772 * @param callback Callback to invoke when inventory items have been obtained.
1773 */
1774template <typename Callback>
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001775static void getInventoryItemAssociations(
Ed Tanousb5a76932020-09-29 16:16:58 -07001776 const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
Nan Zhoufe04d492022-06-22 17:10:41 +00001777 const std::shared_ptr<std::set<std::string>>& sensorNames,
1778 const std::shared_ptr<std::map<std::string, std::string>>& objectMgrPaths,
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001779 Callback&& callback)
1780{
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001781 BMCWEB_LOG_DEBUG << "getInventoryItemAssociations enter";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001782
1783 // Response handler for GetManagedObjects
Ed Tanous02cad962022-06-30 16:50:15 -07001784 auto respHandler =
1785 [callback{std::forward<Callback>(callback)}, sensorsAsyncResp,
1786 sensorNames](const boost::system::error_code ec,
1787 const dbus::utility::ManagedObjectType& resp) {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001788 BMCWEB_LOG_DEBUG << "getInventoryItemAssociations respHandler enter";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001789 if (ec)
1790 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001791 BMCWEB_LOG_ERROR
1792 << "getInventoryItemAssociations respHandler DBus error " << ec;
zhanghch058d1b46d2021-04-01 11:18:24 +08001793 messages::internalError(sensorsAsyncResp->asyncResp->res);
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001794 return;
1795 }
1796
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001797 // Create vector to hold list of inventory items
1798 std::shared_ptr<std::vector<InventoryItem>> inventoryItems =
1799 std::make_shared<std::vector<InventoryItem>>();
1800
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001801 // Loop through returned object paths
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001802 std::string sensorAssocPath;
1803 sensorAssocPath.reserve(128); // avoid memory allocations
1804 for (const auto& objDictEntry : resp)
1805 {
1806 const std::string& objPath =
1807 static_cast<const std::string&>(objDictEntry.first);
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001808
1809 // If path is inventory association for one of the specified sensors
1810 for (const std::string& sensorName : *sensorNames)
1811 {
1812 sensorAssocPath = sensorName;
1813 sensorAssocPath += "/inventory";
1814 if (objPath == sensorAssocPath)
1815 {
1816 // Get Association interface for object path
Ed Tanous711ac7a2021-12-20 09:34:41 -08001817 for (const auto& [interface, values] : objDictEntry.second)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001818 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001819 if (interface == "xyz.openbmc_project.Association")
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001820 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001821 for (const auto& [valueName, value] : values)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001822 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001823 if (valueName == "endpoints")
1824 {
1825 const std::vector<std::string>* endpoints =
1826 std::get_if<std::vector<std::string>>(
1827 &value);
1828 if ((endpoints != nullptr) &&
1829 !endpoints->empty())
1830 {
1831 // Add inventory item to vector
1832 const std::string& invItemPath =
1833 endpoints->front();
1834 addInventoryItem(inventoryItems,
1835 invItemPath,
1836 sensorName);
1837 }
1838 }
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001839 }
1840 }
1841 }
1842 break;
1843 }
1844 }
1845 }
1846
Anthony Wilsond5005492019-07-31 16:34:17 -05001847 // Now loop through the returned object paths again, this time to
1848 // find the leds associated with the inventory items we just found
1849 std::string inventoryAssocPath;
1850 inventoryAssocPath.reserve(128); // avoid memory allocations
1851 for (const auto& objDictEntry : resp)
1852 {
1853 const std::string& objPath =
1854 static_cast<const std::string&>(objDictEntry.first);
Anthony Wilsond5005492019-07-31 16:34:17 -05001855
1856 for (InventoryItem& inventoryItem : *inventoryItems)
1857 {
1858 inventoryAssocPath = inventoryItem.objectPath;
1859 inventoryAssocPath += "/leds";
1860 if (objPath == inventoryAssocPath)
1861 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001862 for (const auto& [interface, values] : objDictEntry.second)
Anthony Wilsond5005492019-07-31 16:34:17 -05001863 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001864 if (interface == "xyz.openbmc_project.Association")
Anthony Wilsond5005492019-07-31 16:34:17 -05001865 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001866 for (const auto& [valueName, value] : values)
Anthony Wilsond5005492019-07-31 16:34:17 -05001867 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001868 if (valueName == "endpoints")
1869 {
1870 const std::vector<std::string>* endpoints =
1871 std::get_if<std::vector<std::string>>(
1872 &value);
1873 if ((endpoints != nullptr) &&
1874 !endpoints->empty())
1875 {
1876 // Add inventory item to vector
1877 // Store LED path in inventory item
1878 const std::string& ledPath =
1879 endpoints->front();
1880 inventoryItem.ledObjectPath = ledPath;
1881 }
1882 }
Anthony Wilsond5005492019-07-31 16:34:17 -05001883 }
1884 }
1885 }
Ed Tanous711ac7a2021-12-20 09:34:41 -08001886
Anthony Wilsond5005492019-07-31 16:34:17 -05001887 break;
1888 }
1889 }
1890 }
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001891 callback(inventoryItems);
1892 BMCWEB_LOG_DEBUG << "getInventoryItemAssociations respHandler exit";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001893 };
1894
1895 // Find DBus object path that implements ObjectManager for ObjectMapper
1896 std::string connection = "xyz.openbmc_project.ObjectMapper";
1897 auto iter = objectMgrPaths->find(connection);
1898 const std::string& objectMgrPath =
1899 (iter != objectMgrPaths->end()) ? iter->second : "/";
1900 BMCWEB_LOG_DEBUG << "ObjectManager path for " << connection << " is "
1901 << objectMgrPath;
1902
1903 // Call GetManagedObjects on the ObjectMapper to get all associations
1904 crow::connections::systemBus->async_method_call(
1905 std::move(respHandler), connection, objectMgrPath,
1906 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
1907
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001908 BMCWEB_LOG_DEBUG << "getInventoryItemAssociations exit";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001909}
1910
1911/**
Anthony Wilsond5005492019-07-31 16:34:17 -05001912 * @brief Gets D-Bus data for inventory item leds associated with sensors.
1913 *
1914 * Uses the specified connections (services) to obtain D-Bus data for inventory
1915 * item leds associated with sensors. Stores the resulting data in the
1916 * inventoryItems vector.
1917 *
1918 * This data is later used to provide sensor property values in the JSON
1919 * response.
1920 *
1921 * Finds the inventory item led data asynchronously. Invokes callback when data
1922 * has been obtained.
1923 *
1924 * The callback must have the following signature:
1925 * @code
Gunnar Mills42cbe532019-08-15 15:26:54 -05001926 * callback()
Anthony Wilsond5005492019-07-31 16:34:17 -05001927 * @endcode
1928 *
1929 * This function is called recursively, obtaining data asynchronously from one
1930 * connection in each call. This ensures the callback is not invoked until the
1931 * last asynchronous function has completed.
1932 *
1933 * @param sensorsAsyncResp Pointer to object holding response data.
1934 * @param inventoryItems D-Bus inventory items associated with sensors.
1935 * @param ledConnections Connections that provide data for the inventory leds.
1936 * @param callback Callback to invoke when inventory data has been obtained.
1937 * @param ledConnectionsIndex Current index in ledConnections. Only specified
1938 * in recursive calls to this function.
1939 */
1940template <typename Callback>
1941void getInventoryLedData(
1942 std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
1943 std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
Nan Zhoufe04d492022-06-22 17:10:41 +00001944 std::shared_ptr<std::map<std::string, std::string>> ledConnections,
Anthony Wilsond5005492019-07-31 16:34:17 -05001945 Callback&& callback, size_t ledConnectionsIndex = 0)
1946{
1947 BMCWEB_LOG_DEBUG << "getInventoryLedData enter";
1948
1949 // If no more connections left, call callback
1950 if (ledConnectionsIndex >= ledConnections->size())
1951 {
Gunnar Mills42cbe532019-08-15 15:26:54 -05001952 callback();
Anthony Wilsond5005492019-07-31 16:34:17 -05001953 BMCWEB_LOG_DEBUG << "getInventoryLedData exit";
1954 return;
1955 }
1956
1957 // Get inventory item data from current connection
Nan Zhoufe04d492022-06-22 17:10:41 +00001958 auto it = ledConnections->begin();
1959 std::advance(it, ledConnectionsIndex);
Anthony Wilsond5005492019-07-31 16:34:17 -05001960 if (it != ledConnections->end())
1961 {
1962 const std::string& ledPath = (*it).first;
1963 const std::string& ledConnection = (*it).second;
1964 // Response handler for Get State property
Jonathan Doman1e1e5982021-06-11 09:36:17 -07001965 auto respHandler =
1966 [sensorsAsyncResp, inventoryItems, ledConnections, ledPath,
Ed Tanousf94c4ec2022-01-06 12:44:41 -08001967 callback{std::forward<Callback>(callback)}, ledConnectionsIndex](
Jonathan Doman1e1e5982021-06-11 09:36:17 -07001968 const boost::system::error_code ec, const std::string& state) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001969 BMCWEB_LOG_DEBUG << "getInventoryLedData respHandler enter";
1970 if (ec)
1971 {
1972 BMCWEB_LOG_ERROR
1973 << "getInventoryLedData respHandler DBus error " << ec;
1974 messages::internalError(sensorsAsyncResp->asyncResp->res);
1975 return;
1976 }
1977
1978 BMCWEB_LOG_DEBUG << "Led state: " << state;
1979 // Find inventory item with this LED object path
1980 InventoryItem* inventoryItem =
1981 findInventoryItemForLed(*inventoryItems, ledPath);
1982 if (inventoryItem != nullptr)
1983 {
1984 // Store LED state in InventoryItem
1985 if (boost::ends_with(state, "On"))
Jonathan Doman1e1e5982021-06-11 09:36:17 -07001986 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001987 inventoryItem->ledState = LedState::ON;
Jonathan Doman1e1e5982021-06-11 09:36:17 -07001988 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001989 else if (boost::ends_with(state, "Blink"))
Anthony Wilsond5005492019-07-31 16:34:17 -05001990 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001991 inventoryItem->ledState = LedState::BLINK;
Anthony Wilsond5005492019-07-31 16:34:17 -05001992 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001993 else if (boost::ends_with(state, "Off"))
1994 {
1995 inventoryItem->ledState = LedState::OFF;
1996 }
1997 else
1998 {
1999 inventoryItem->ledState = LedState::UNKNOWN;
2000 }
2001 }
Anthony Wilsond5005492019-07-31 16:34:17 -05002002
Ed Tanous002d39b2022-05-31 08:59:27 -07002003 // Recurse to get LED data from next connection
2004 getInventoryLedData(sensorsAsyncResp, inventoryItems,
2005 ledConnections, std::move(callback),
2006 ledConnectionsIndex + 1);
Anthony Wilsond5005492019-07-31 16:34:17 -05002007
Ed Tanous002d39b2022-05-31 08:59:27 -07002008 BMCWEB_LOG_DEBUG << "getInventoryLedData respHandler exit";
2009 };
Anthony Wilsond5005492019-07-31 16:34:17 -05002010
2011 // Get the State property for the current LED
Jonathan Doman1e1e5982021-06-11 09:36:17 -07002012 sdbusplus::asio::getProperty<std::string>(
2013 *crow::connections::systemBus, ledConnection, ledPath,
2014 "xyz.openbmc_project.Led.Physical", "State",
2015 std::move(respHandler));
Anthony Wilsond5005492019-07-31 16:34:17 -05002016 }
2017
2018 BMCWEB_LOG_DEBUG << "getInventoryLedData exit";
2019}
2020
2021/**
2022 * @brief Gets LED data for LEDs associated with given inventory items.
2023 *
2024 * Gets the D-Bus connections (services) that provide LED data for the LEDs
2025 * associated with the specified inventory items. Then gets the LED data from
2026 * each connection and stores it in the inventory item.
2027 *
2028 * This data is later used to provide sensor property values in the JSON
2029 * response.
2030 *
2031 * Finds the LED data asynchronously. Invokes callback when information has
2032 * been obtained.
2033 *
2034 * The callback must have the following signature:
2035 * @code
Gunnar Mills42cbe532019-08-15 15:26:54 -05002036 * callback()
Anthony Wilsond5005492019-07-31 16:34:17 -05002037 * @endcode
2038 *
2039 * @param sensorsAsyncResp Pointer to object holding response data.
2040 * @param inventoryItems D-Bus inventory items associated with sensors.
2041 * @param callback Callback to invoke when inventory items have been obtained.
2042 */
2043template <typename Callback>
2044void getInventoryLeds(
2045 std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
2046 std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
2047 Callback&& callback)
2048{
2049 BMCWEB_LOG_DEBUG << "getInventoryLeds enter";
2050
2051 const std::string path = "/xyz/openbmc_project";
2052 const std::array<std::string, 1> interfaces = {
2053 "xyz.openbmc_project.Led.Physical"};
2054
2055 // Response handler for parsing output from GetSubTree
Ed Tanous002d39b2022-05-31 08:59:27 -07002056 auto respHandler =
2057 [callback{std::forward<Callback>(callback)}, sensorsAsyncResp,
2058 inventoryItems](
2059 const boost::system::error_code ec,
2060 const dbus::utility::MapperGetSubTreeResponse& subtree) {
Anthony Wilsond5005492019-07-31 16:34:17 -05002061 BMCWEB_LOG_DEBUG << "getInventoryLeds respHandler enter";
2062 if (ec)
2063 {
zhanghch058d1b46d2021-04-01 11:18:24 +08002064 messages::internalError(sensorsAsyncResp->asyncResp->res);
Anthony Wilsond5005492019-07-31 16:34:17 -05002065 BMCWEB_LOG_ERROR << "getInventoryLeds respHandler DBus error "
2066 << ec;
2067 return;
2068 }
2069
2070 // Build map of LED object paths to connections
Nan Zhoufe04d492022-06-22 17:10:41 +00002071 std::shared_ptr<std::map<std::string, std::string>> ledConnections =
2072 std::make_shared<std::map<std::string, std::string>>();
Anthony Wilsond5005492019-07-31 16:34:17 -05002073
2074 // Loop through objects from GetSubTree
2075 for (const std::pair<
2076 std::string,
2077 std::vector<std::pair<std::string, std::vector<std::string>>>>&
2078 object : subtree)
2079 {
2080 // Check if object path is LED for one of the specified inventory
2081 // items
2082 const std::string& ledPath = object.first;
2083 if (findInventoryItemForLed(*inventoryItems, ledPath) != nullptr)
2084 {
2085 // Add mapping from ledPath to connection
2086 const std::string& connection = object.second.begin()->first;
2087 (*ledConnections)[ledPath] = connection;
2088 BMCWEB_LOG_DEBUG << "Added mapping " << ledPath << " -> "
2089 << connection;
2090 }
2091 }
2092
2093 getInventoryLedData(sensorsAsyncResp, inventoryItems, ledConnections,
2094 std::move(callback));
2095 BMCWEB_LOG_DEBUG << "getInventoryLeds respHandler exit";
2096 };
2097 // Make call to ObjectMapper to find all inventory items
2098 crow::connections::systemBus->async_method_call(
2099 std::move(respHandler), "xyz.openbmc_project.ObjectMapper",
2100 "/xyz/openbmc_project/object_mapper",
2101 "xyz.openbmc_project.ObjectMapper", "GetSubTree", path, 0, interfaces);
2102 BMCWEB_LOG_DEBUG << "getInventoryLeds exit";
2103}
2104
2105/**
Gunnar Mills42cbe532019-08-15 15:26:54 -05002106 * @brief Gets D-Bus data for Power Supply Attributes such as EfficiencyPercent
2107 *
2108 * Uses the specified connections (services) (currently assumes just one) to
2109 * obtain D-Bus data for Power Supply Attributes. Stores the resulting data in
2110 * the inventoryItems vector. Only stores data in Power Supply inventoryItems.
2111 *
2112 * This data is later used to provide sensor property values in the JSON
2113 * response.
2114 *
2115 * Finds the Power Supply Attributes data asynchronously. Invokes callback
2116 * when data has been obtained.
2117 *
2118 * The callback must have the following signature:
2119 * @code
2120 * callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems)
2121 * @endcode
2122 *
2123 * @param sensorsAsyncResp Pointer to object holding response data.
2124 * @param inventoryItems D-Bus inventory items associated with sensors.
2125 * @param psAttributesConnections Connections that provide data for the Power
2126 * Supply Attributes
2127 * @param callback Callback to invoke when data has been obtained.
2128 */
2129template <typename Callback>
2130void getPowerSupplyAttributesData(
Ed Tanousb5a76932020-09-29 16:16:58 -07002131 const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
Gunnar Mills42cbe532019-08-15 15:26:54 -05002132 std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
Nan Zhoufe04d492022-06-22 17:10:41 +00002133 const std::map<std::string, std::string>& psAttributesConnections,
Gunnar Mills42cbe532019-08-15 15:26:54 -05002134 Callback&& callback)
2135{
2136 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributesData enter";
2137
2138 if (psAttributesConnections.empty())
2139 {
2140 BMCWEB_LOG_DEBUG << "Can't find PowerSupplyAttributes, no connections!";
2141 callback(inventoryItems);
2142 return;
2143 }
2144
2145 // Assuming just one connection (service) for now
Nan Zhoufe04d492022-06-22 17:10:41 +00002146 auto it = psAttributesConnections.begin();
Gunnar Mills42cbe532019-08-15 15:26:54 -05002147
2148 const std::string& psAttributesPath = (*it).first;
2149 const std::string& psAttributesConnection = (*it).second;
2150
2151 // Response handler for Get DeratingFactor property
Ed Tanous002d39b2022-05-31 08:59:27 -07002152 auto respHandler =
2153 [sensorsAsyncResp, inventoryItems,
2154 callback{std::forward<Callback>(callback)}](
2155 const boost::system::error_code ec, const uint32_t value) {
Gunnar Mills42cbe532019-08-15 15:26:54 -05002156 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributesData respHandler enter";
2157 if (ec)
2158 {
2159 BMCWEB_LOG_ERROR
2160 << "getPowerSupplyAttributesData respHandler DBus error " << ec;
zhanghch058d1b46d2021-04-01 11:18:24 +08002161 messages::internalError(sensorsAsyncResp->asyncResp->res);
Gunnar Mills42cbe532019-08-15 15:26:54 -05002162 return;
2163 }
2164
Jonathan Doman1e1e5982021-06-11 09:36:17 -07002165 BMCWEB_LOG_DEBUG << "PS EfficiencyPercent value: " << value;
2166 // Store value in Power Supply Inventory Items
2167 for (InventoryItem& inventoryItem : *inventoryItems)
Gunnar Mills42cbe532019-08-15 15:26:54 -05002168 {
Ed Tanous55f79e62022-01-25 11:26:16 -08002169 if (inventoryItem.isPowerSupply)
Gunnar Mills42cbe532019-08-15 15:26:54 -05002170 {
Jonathan Doman1e1e5982021-06-11 09:36:17 -07002171 inventoryItem.powerSupplyEfficiencyPercent =
2172 static_cast<int>(value);
Gunnar Mills42cbe532019-08-15 15:26:54 -05002173 }
2174 }
Gunnar Mills42cbe532019-08-15 15:26:54 -05002175
2176 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributesData respHandler exit";
2177 callback(inventoryItems);
2178 };
2179
2180 // Get the DeratingFactor property for the PowerSupplyAttributes
2181 // Currently only property on the interface/only one we care about
Jonathan Doman1e1e5982021-06-11 09:36:17 -07002182 sdbusplus::asio::getProperty<uint32_t>(
2183 *crow::connections::systemBus, psAttributesConnection, psAttributesPath,
2184 "xyz.openbmc_project.Control.PowerSupplyAttributes", "DeratingFactor",
2185 std::move(respHandler));
Gunnar Mills42cbe532019-08-15 15:26:54 -05002186
2187 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributesData exit";
2188}
2189
2190/**
2191 * @brief Gets the Power Supply Attributes such as EfficiencyPercent
2192 *
2193 * Gets the D-Bus connection (service) that provides Power Supply Attributes
2194 * data. Then gets the Power Supply Attributes data from the connection
2195 * (currently just assumes 1 connection) and stores the data in the inventory
2196 * item.
2197 *
2198 * This data is later used to provide sensor property values in the JSON
2199 * response. DeratingFactor on D-Bus is mapped to EfficiencyPercent on Redfish.
2200 *
2201 * Finds the Power Supply Attributes data asynchronously. Invokes callback
2202 * when information has been obtained.
2203 *
2204 * The callback must have the following signature:
2205 * @code
2206 * callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems)
2207 * @endcode
2208 *
2209 * @param sensorsAsyncResp Pointer to object holding response data.
2210 * @param inventoryItems D-Bus inventory items associated with sensors.
2211 * @param callback Callback to invoke when data has been obtained.
2212 */
2213template <typename Callback>
2214void getPowerSupplyAttributes(
2215 std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
2216 std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
2217 Callback&& callback)
2218{
2219 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes enter";
2220
2221 // Only need the power supply attributes when the Power Schema
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02002222 if (sensorsAsyncResp->chassisSubNode != sensors::node::power)
Gunnar Mills42cbe532019-08-15 15:26:54 -05002223 {
2224 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes exit since not Power";
2225 callback(inventoryItems);
2226 return;
2227 }
2228
2229 const std::array<std::string, 1> interfaces = {
2230 "xyz.openbmc_project.Control.PowerSupplyAttributes"};
2231
2232 // Response handler for parsing output from GetSubTree
Ed Tanousb9d36b42022-02-26 21:42:46 -08002233 auto respHandler =
2234 [callback{std::forward<Callback>(callback)}, sensorsAsyncResp,
2235 inventoryItems](
2236 const boost::system::error_code ec,
2237 const dbus::utility::MapperGetSubTreeResponse& subtree) {
Ed Tanous002d39b2022-05-31 08:59:27 -07002238 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes respHandler enter";
2239 if (ec)
2240 {
2241 messages::internalError(sensorsAsyncResp->asyncResp->res);
2242 BMCWEB_LOG_ERROR
2243 << "getPowerSupplyAttributes respHandler DBus error " << ec;
2244 return;
2245 }
2246 if (subtree.empty())
2247 {
2248 BMCWEB_LOG_DEBUG << "Can't find Power Supply Attributes!";
2249 callback(inventoryItems);
2250 return;
2251 }
Gunnar Mills42cbe532019-08-15 15:26:54 -05002252
Ed Tanous002d39b2022-05-31 08:59:27 -07002253 // Currently we only support 1 power supply attribute, use this for
2254 // all the power supplies. Build map of object path to connection.
2255 // Assume just 1 connection and 1 path for now.
Nan Zhoufe04d492022-06-22 17:10:41 +00002256 std::map<std::string, std::string> psAttributesConnections;
Gunnar Mills42cbe532019-08-15 15:26:54 -05002257
Ed Tanous002d39b2022-05-31 08:59:27 -07002258 if (subtree[0].first.empty() || subtree[0].second.empty())
2259 {
2260 BMCWEB_LOG_DEBUG << "Power Supply Attributes mapper error!";
2261 callback(inventoryItems);
2262 return;
2263 }
Gunnar Mills42cbe532019-08-15 15:26:54 -05002264
Ed Tanous002d39b2022-05-31 08:59:27 -07002265 const std::string& psAttributesPath = subtree[0].first;
2266 const std::string& connection = subtree[0].second.begin()->first;
Gunnar Mills42cbe532019-08-15 15:26:54 -05002267
Ed Tanous002d39b2022-05-31 08:59:27 -07002268 if (connection.empty())
2269 {
2270 BMCWEB_LOG_DEBUG << "Power Supply Attributes mapper error!";
2271 callback(inventoryItems);
2272 return;
2273 }
Gunnar Mills42cbe532019-08-15 15:26:54 -05002274
Ed Tanous002d39b2022-05-31 08:59:27 -07002275 psAttributesConnections[psAttributesPath] = connection;
2276 BMCWEB_LOG_DEBUG << "Added mapping " << psAttributesPath << " -> "
2277 << connection;
Gunnar Mills42cbe532019-08-15 15:26:54 -05002278
Ed Tanous002d39b2022-05-31 08:59:27 -07002279 getPowerSupplyAttributesData(sensorsAsyncResp, inventoryItems,
2280 psAttributesConnections,
2281 std::move(callback));
2282 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes respHandler exit";
2283 };
Gunnar Mills42cbe532019-08-15 15:26:54 -05002284 // Make call to ObjectMapper to find the PowerSupplyAttributes service
2285 crow::connections::systemBus->async_method_call(
2286 std::move(respHandler), "xyz.openbmc_project.ObjectMapper",
2287 "/xyz/openbmc_project/object_mapper",
2288 "xyz.openbmc_project.ObjectMapper", "GetSubTree",
2289 "/xyz/openbmc_project", 0, interfaces);
2290 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes exit";
2291}
2292
2293/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002294 * @brief Gets inventory items associated with sensors.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002295 *
2296 * Finds the inventory items that are associated with the specified sensors.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002297 * Then gets D-Bus data for the inventory items, such as presence and VPD.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002298 *
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002299 * This data is later used to provide sensor property values in the JSON
2300 * response.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002301 *
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002302 * Finds the inventory items asynchronously. Invokes callback when the
2303 * inventory items have been obtained.
2304 *
2305 * The callback must have the following signature:
2306 * @code
2307 * callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems)
2308 * @endcode
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002309 *
2310 * @param sensorsAsyncResp Pointer to object holding response data.
2311 * @param sensorNames All sensors within the current chassis.
2312 * @param objectMgrPaths Mappings from connection name to DBus object path that
2313 * implements ObjectManager.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002314 * @param callback Callback to invoke when inventory items have been obtained.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002315 */
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002316template <typename Callback>
2317static void getInventoryItems(
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002318 std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
Nan Zhoufe04d492022-06-22 17:10:41 +00002319 const std::shared_ptr<std::set<std::string>> sensorNames,
2320 std::shared_ptr<std::map<std::string, std::string>> objectMgrPaths,
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002321 Callback&& callback)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002322{
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002323 BMCWEB_LOG_DEBUG << "getInventoryItems enter";
2324 auto getInventoryItemAssociationsCb =
Ed Tanousf94c4ec2022-01-06 12:44:41 -08002325 [sensorsAsyncResp, objectMgrPaths,
2326 callback{std::forward<Callback>(callback)}](
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002327 std::shared_ptr<std::vector<InventoryItem>> inventoryItems) {
Ed Tanous002d39b2022-05-31 08:59:27 -07002328 BMCWEB_LOG_DEBUG << "getInventoryItemAssociationsCb enter";
2329 auto getInventoryItemsConnectionsCb =
2330 [sensorsAsyncResp, inventoryItems, objectMgrPaths,
2331 callback{std::forward<const Callback>(callback)}](
Nan Zhoufe04d492022-06-22 17:10:41 +00002332 std::shared_ptr<std::set<std::string>> invConnections) {
Ed Tanous002d39b2022-05-31 08:59:27 -07002333 BMCWEB_LOG_DEBUG << "getInventoryItemsConnectionsCb enter";
2334 auto getInventoryItemsDataCb = [sensorsAsyncResp, inventoryItems,
2335 callback{std::move(callback)}]() {
2336 BMCWEB_LOG_DEBUG << "getInventoryItemsDataCb enter";
Gunnar Mills42cbe532019-08-15 15:26:54 -05002337
Ed Tanous002d39b2022-05-31 08:59:27 -07002338 auto getInventoryLedsCb = [sensorsAsyncResp, inventoryItems,
2339 callback{std::move(callback)}]() {
2340 BMCWEB_LOG_DEBUG << "getInventoryLedsCb enter";
2341 // Find Power Supply Attributes and get the data
2342 getPowerSupplyAttributes(sensorsAsyncResp, inventoryItems,
2343 std::move(callback));
2344 BMCWEB_LOG_DEBUG << "getInventoryLedsCb exit";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002345 };
2346
Ed Tanous002d39b2022-05-31 08:59:27 -07002347 // Find led connections and get the data
2348 getInventoryLeds(sensorsAsyncResp, inventoryItems,
2349 std::move(getInventoryLedsCb));
2350 BMCWEB_LOG_DEBUG << "getInventoryItemsDataCb exit";
2351 };
2352
2353 // Get inventory item data from connections
2354 getInventoryItemsData(sensorsAsyncResp, inventoryItems,
2355 invConnections, objectMgrPaths,
2356 std::move(getInventoryItemsDataCb));
2357 BMCWEB_LOG_DEBUG << "getInventoryItemsConnectionsCb exit";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002358 };
2359
Ed Tanous002d39b2022-05-31 08:59:27 -07002360 // Get connections that provide inventory item data
2361 getInventoryItemsConnections(sensorsAsyncResp, inventoryItems,
2362 std::move(getInventoryItemsConnectionsCb));
2363 BMCWEB_LOG_DEBUG << "getInventoryItemAssociationsCb exit";
2364 };
2365
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002366 // Get associations from sensors to inventory items
2367 getInventoryItemAssociations(sensorsAsyncResp, sensorNames, objectMgrPaths,
2368 std::move(getInventoryItemAssociationsCb));
2369 BMCWEB_LOG_DEBUG << "getInventoryItems exit";
2370}
2371
2372/**
2373 * @brief Returns JSON PowerSupply object for the specified inventory item.
2374 *
2375 * Searches for a JSON PowerSupply object that matches the specified inventory
2376 * item. If one is not found, a new PowerSupply object is added to the JSON
2377 * array.
2378 *
2379 * Multiple sensors are often associated with one power supply inventory item.
2380 * As a result, multiple sensor values are stored in one JSON PowerSupply
2381 * object.
2382 *
2383 * @param powerSupplyArray JSON array containing Redfish PowerSupply objects.
2384 * @param inventoryItem Inventory item for the power supply.
2385 * @param chassisId Chassis that contains the power supply.
2386 * @return JSON PowerSupply object for the specified inventory item.
2387 */
Ed Tanous23a21a12020-07-25 04:45:05 +00002388inline nlohmann::json& getPowerSupply(nlohmann::json& powerSupplyArray,
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002389 const InventoryItem& inventoryItem,
2390 const std::string& chassisId)
2391{
2392 // Check if matching PowerSupply object already exists in JSON array
2393 for (nlohmann::json& powerSupply : powerSupplyArray)
2394 {
2395 if (powerSupply["MemberId"] == inventoryItem.name)
2396 {
2397 return powerSupply;
2398 }
2399 }
2400
2401 // Add new PowerSupply object to JSON array
2402 powerSupplyArray.push_back({});
2403 nlohmann::json& powerSupply = powerSupplyArray.back();
2404 powerSupply["@odata.id"] =
2405 "/redfish/v1/Chassis/" + chassisId + "/Power#/PowerSupplies/";
2406 powerSupply["MemberId"] = inventoryItem.name;
2407 powerSupply["Name"] = boost::replace_all_copy(inventoryItem.name, "_", " ");
2408 powerSupply["Manufacturer"] = inventoryItem.manufacturer;
2409 powerSupply["Model"] = inventoryItem.model;
2410 powerSupply["PartNumber"] = inventoryItem.partNumber;
2411 powerSupply["SerialNumber"] = inventoryItem.serialNumber;
Anthony Wilsond5005492019-07-31 16:34:17 -05002412 setLedState(powerSupply, &inventoryItem);
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002413
Gunnar Mills42cbe532019-08-15 15:26:54 -05002414 if (inventoryItem.powerSupplyEfficiencyPercent >= 0)
2415 {
2416 powerSupply["EfficiencyPercent"] =
2417 inventoryItem.powerSupplyEfficiencyPercent;
2418 }
2419
2420 powerSupply["Status"]["State"] = getState(&inventoryItem);
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002421 const char* health = inventoryItem.isFunctional ? "OK" : "Critical";
2422 powerSupply["Status"]["Health"] = health;
2423
2424 return powerSupply;
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002425}
2426
2427/**
Shawn McCarneyde629b62019-03-08 10:42:51 -06002428 * @brief Gets the values of the specified sensors.
2429 *
2430 * Stores the results as JSON in the SensorsAsyncResp.
2431 *
2432 * Gets the sensor values asynchronously. Stores the results later when the
2433 * information has been obtained.
2434 *
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002435 * The sensorNames set contains all requested sensors for the current chassis.
Shawn McCarneyde629b62019-03-08 10:42:51 -06002436 *
2437 * To minimize the number of DBus calls, the DBus method
2438 * org.freedesktop.DBus.ObjectManager.GetManagedObjects() is used to get the
2439 * values of all sensors provided by a connection (service).
2440 *
2441 * The connections set contains all the connections that provide sensor values.
2442 *
2443 * The objectMgrPaths map contains mappings from a connection name to the
2444 * corresponding DBus object path that implements ObjectManager.
2445 *
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002446 * The InventoryItem vector contains D-Bus inventory items associated with the
2447 * sensors. Inventory item data is needed for some Redfish sensor properties.
2448 *
Shawn McCarneyde629b62019-03-08 10:42:51 -06002449 * @param SensorsAsyncResp Pointer to object holding response data.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002450 * @param sensorNames All requested sensors within the current chassis.
Shawn McCarneyde629b62019-03-08 10:42:51 -06002451 * @param connections Connections that provide sensor values.
2452 * @param objectMgrPaths Mappings from connection name to DBus object path that
2453 * implements ObjectManager.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002454 * @param inventoryItems Inventory items associated with the sensors.
Shawn McCarneyde629b62019-03-08 10:42:51 -06002455 */
Ed Tanous23a21a12020-07-25 04:45:05 +00002456inline void getSensorData(
Ed Tanous81ce6092020-12-17 16:54:55 +00002457 const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
Nan Zhoufe04d492022-06-22 17:10:41 +00002458 const std::shared_ptr<std::set<std::string>>& sensorNames,
2459 const std::set<std::string>& connections,
2460 const std::shared_ptr<std::map<std::string, std::string>>& objectMgrPaths,
Ed Tanousb5a76932020-09-29 16:16:58 -07002461 const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems)
Shawn McCarneyde629b62019-03-08 10:42:51 -06002462{
2463 BMCWEB_LOG_DEBUG << "getSensorData enter";
2464 // Get managed objects from all services exposing sensors
2465 for (const std::string& connection : connections)
2466 {
2467 // Response handler to process managed objects
Ed Tanous002d39b2022-05-31 08:59:27 -07002468 auto getManagedObjectsCb =
2469 [sensorsAsyncResp, sensorNames,
2470 inventoryItems](const boost::system::error_code ec,
Ed Tanous02cad962022-06-30 16:50:15 -07002471 const dbus::utility::ManagedObjectType& resp) {
Shawn McCarneyde629b62019-03-08 10:42:51 -06002472 BMCWEB_LOG_DEBUG << "getManagedObjectsCb enter";
2473 if (ec)
2474 {
2475 BMCWEB_LOG_ERROR << "getManagedObjectsCb DBUS error: " << ec;
zhanghch058d1b46d2021-04-01 11:18:24 +08002476 messages::internalError(sensorsAsyncResp->asyncResp->res);
Shawn McCarneyde629b62019-03-08 10:42:51 -06002477 return;
2478 }
2479 // Go through all objects and update response with sensor data
2480 for (const auto& objDictEntry : resp)
2481 {
2482 const std::string& objPath =
2483 static_cast<const std::string&>(objDictEntry.first);
2484 BMCWEB_LOG_DEBUG << "getManagedObjectsCb parsing object "
2485 << objPath;
2486
Shawn McCarneyde629b62019-03-08 10:42:51 -06002487 std::vector<std::string> split;
2488 // Reserve space for
2489 // /xyz/openbmc_project/sensors/<name>/<subname>
2490 split.reserve(6);
2491 boost::algorithm::split(split, objPath, boost::is_any_of("/"));
2492 if (split.size() < 6)
2493 {
2494 BMCWEB_LOG_ERROR << "Got path that isn't long enough "
2495 << objPath;
2496 continue;
2497 }
2498 // These indexes aren't intuitive, as boost::split puts an empty
2499 // string at the beginning
2500 const std::string& sensorType = split[4];
2501 const std::string& sensorName = split[5];
2502 BMCWEB_LOG_DEBUG << "sensorName " << sensorName
2503 << " sensorType " << sensorType;
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002504 if (sensorNames->find(objPath) == sensorNames->end())
Shawn McCarneyde629b62019-03-08 10:42:51 -06002505 {
Andrew Geissleraccdbb22021-11-09 15:24:45 -06002506 BMCWEB_LOG_DEBUG << sensorName << " not in sensor list ";
Shawn McCarneyde629b62019-03-08 10:42:51 -06002507 continue;
2508 }
2509
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002510 // Find inventory item (if any) associated with sensor
2511 InventoryItem* inventoryItem =
2512 findInventoryItemForSensor(inventoryItems, objPath);
2513
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002514 const std::string& sensorSchema =
Ed Tanous81ce6092020-12-17 16:54:55 +00002515 sensorsAsyncResp->chassisSubNode;
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002516
2517 nlohmann::json* sensorJson = nullptr;
2518
Nan Zhou928fefb2022-03-28 08:45:00 -07002519 if (sensorSchema == sensors::node::sensors &&
2520 !sensorsAsyncResp->efficientExpand)
Shawn McCarneyde629b62019-03-08 10:42:51 -06002521 {
zhanghch058d1b46d2021-04-01 11:18:24 +08002522 sensorsAsyncResp->asyncResp->res.jsonValue["@odata.id"] =
Ed Tanous81ce6092020-12-17 16:54:55 +00002523 "/redfish/v1/Chassis/" + sensorsAsyncResp->chassisId +
2524 "/" + sensorsAsyncResp->chassisSubNode + "/" +
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002525 sensorName;
zhanghch058d1b46d2021-04-01 11:18:24 +08002526 sensorJson = &(sensorsAsyncResp->asyncResp->res.jsonValue);
Shawn McCarneyde629b62019-03-08 10:42:51 -06002527 }
2528 else
2529 {
Ed Tanous271584a2019-07-09 16:24:22 -07002530 std::string fieldName;
Nan Zhou928fefb2022-03-28 08:45:00 -07002531 if (sensorsAsyncResp->efficientExpand)
2532 {
2533 fieldName = "Members";
2534 }
2535 else if (sensorType == "temperature")
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002536 {
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002537 fieldName = "Temperatures";
2538 }
2539 else if (sensorType == "fan" || sensorType == "fan_tach" ||
2540 sensorType == "fan_pwm")
2541 {
2542 fieldName = "Fans";
2543 }
2544 else if (sensorType == "voltage")
2545 {
2546 fieldName = "Voltages";
2547 }
2548 else if (sensorType == "power")
2549 {
Ed Tanous55f79e62022-01-25 11:26:16 -08002550 if (sensorName == "total_power")
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002551 {
2552 fieldName = "PowerControl";
2553 }
2554 else if ((inventoryItem != nullptr) &&
2555 (inventoryItem->isPowerSupply))
2556 {
2557 fieldName = "PowerSupplies";
2558 }
2559 else
2560 {
2561 // Other power sensors are in SensorCollection
2562 continue;
2563 }
2564 }
2565 else
2566 {
2567 BMCWEB_LOG_ERROR << "Unsure how to handle sensorType "
2568 << sensorType;
2569 continue;
2570 }
2571
2572 nlohmann::json& tempArray =
zhanghch058d1b46d2021-04-01 11:18:24 +08002573 sensorsAsyncResp->asyncResp->res.jsonValue[fieldName];
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002574 if (fieldName == "PowerControl")
2575 {
2576 if (tempArray.empty())
2577 {
2578 // Put multiple "sensors" into a single
2579 // PowerControl. Follows MemberId naming and
2580 // naming in power.hpp.
Ed Tanous14766872022-03-15 10:44:42 -07002581 nlohmann::json::object_t power;
2582 power["@odata.id"] =
2583 "/redfish/v1/Chassis/" +
2584 sensorsAsyncResp->chassisId + "/" +
2585 sensorsAsyncResp->chassisSubNode + "#/" +
2586 fieldName + "/0";
2587 tempArray.push_back(std::move(power));
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002588 }
2589 sensorJson = &(tempArray.back());
2590 }
2591 else if (fieldName == "PowerSupplies")
2592 {
2593 if (inventoryItem != nullptr)
2594 {
2595 sensorJson =
2596 &(getPowerSupply(tempArray, *inventoryItem,
Ed Tanous81ce6092020-12-17 16:54:55 +00002597 sensorsAsyncResp->chassisId));
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002598 }
2599 }
Nan Zhou928fefb2022-03-28 08:45:00 -07002600 else if (fieldName == "Members")
2601 {
Ed Tanous14766872022-03-15 10:44:42 -07002602 nlohmann::json::object_t member;
2603 member["@odata.id"] =
2604 "/redfish/v1/Chassis/" +
2605 sensorsAsyncResp->chassisId + "/" +
2606 sensorsAsyncResp->chassisSubNode + "/" + sensorName;
2607 tempArray.push_back(std::move(member));
Nan Zhou928fefb2022-03-28 08:45:00 -07002608 sensorJson = &(tempArray.back());
2609 }
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002610 else
2611 {
Ed Tanous14766872022-03-15 10:44:42 -07002612 nlohmann::json::object_t member;
2613 member["@odata.id"] = "/redfish/v1/Chassis/" +
2614 sensorsAsyncResp->chassisId +
2615 "/" +
2616 sensorsAsyncResp->chassisSubNode +
2617 "#/" + fieldName + "/";
2618 tempArray.push_back(std::move(member));
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002619 sensorJson = &(tempArray.back());
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002620 }
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002621 }
Shawn McCarneyde629b62019-03-08 10:42:51 -06002622
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002623 if (sensorJson != nullptr)
2624 {
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02002625 objectInterfacesToJson(
Ed Tanous81ce6092020-12-17 16:54:55 +00002626 sensorName, sensorType, sensorsAsyncResp,
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02002627 objDictEntry.second, *sensorJson, inventoryItem);
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002628 }
Shawn McCarneyde629b62019-03-08 10:42:51 -06002629 }
Ed Tanous81ce6092020-12-17 16:54:55 +00002630 if (sensorsAsyncResp.use_count() == 1)
James Feist8bd25cc2019-03-15 15:14:00 -07002631 {
Ed Tanous81ce6092020-12-17 16:54:55 +00002632 sortJSONResponse(sensorsAsyncResp);
Nan Zhou928fefb2022-03-28 08:45:00 -07002633 if (sensorsAsyncResp->chassisSubNode ==
2634 sensors::node::sensors &&
2635 sensorsAsyncResp->efficientExpand)
2636 {
2637 sensorsAsyncResp->asyncResp->res
2638 .jsonValue["Members@odata.count"] =
2639 sensorsAsyncResp->asyncResp->res.jsonValue["Members"]
2640 .size();
2641 }
2642 else if (sensorsAsyncResp->chassisSubNode ==
2643 sensors::node::thermal)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002644 {
Ed Tanous81ce6092020-12-17 16:54:55 +00002645 populateFanRedundancy(sensorsAsyncResp);
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002646 }
James Feist8bd25cc2019-03-15 15:14:00 -07002647 }
Shawn McCarneyde629b62019-03-08 10:42:51 -06002648 BMCWEB_LOG_DEBUG << "getManagedObjectsCb exit";
2649 };
2650
2651 // Find DBus object path that implements ObjectManager for the current
2652 // connection. If no mapping found, default to "/".
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002653 auto iter = objectMgrPaths->find(connection);
Shawn McCarneyde629b62019-03-08 10:42:51 -06002654 const std::string& objectMgrPath =
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002655 (iter != objectMgrPaths->end()) ? iter->second : "/";
Shawn McCarneyde629b62019-03-08 10:42:51 -06002656 BMCWEB_LOG_DEBUG << "ObjectManager path for " << connection << " is "
2657 << objectMgrPath;
2658
2659 crow::connections::systemBus->async_method_call(
2660 getManagedObjectsCb, connection, objectMgrPath,
2661 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
Ed Tanous23a21a12020-07-25 04:45:05 +00002662 }
Shawn McCarneyde629b62019-03-08 10:42:51 -06002663 BMCWEB_LOG_DEBUG << "getSensorData exit";
2664}
2665
Nan Zhoufe04d492022-06-22 17:10:41 +00002666inline void
2667 processSensorList(const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
2668 const std::shared_ptr<std::set<std::string>>& sensorNames)
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002669{
Nan Zhoufe04d492022-06-22 17:10:41 +00002670 auto getConnectionCb = [sensorsAsyncResp, sensorNames](
2671 const std::set<std::string>& connections) {
Ed Tanous002d39b2022-05-31 08:59:27 -07002672 BMCWEB_LOG_DEBUG << "getConnectionCb enter";
2673 auto getObjectManagerPathsCb =
Nan Zhoufe04d492022-06-22 17:10:41 +00002674 [sensorsAsyncResp, sensorNames, connections](
2675 const std::shared_ptr<std::map<std::string, std::string>>&
2676 objectMgrPaths) {
Ed Tanous002d39b2022-05-31 08:59:27 -07002677 BMCWEB_LOG_DEBUG << "getObjectManagerPathsCb enter";
2678 auto getInventoryItemsCb =
2679 [sensorsAsyncResp, sensorNames, connections, objectMgrPaths](
2680 const std::shared_ptr<std::vector<InventoryItem>>&
2681 inventoryItems) {
2682 BMCWEB_LOG_DEBUG << "getInventoryItemsCb enter";
2683 // Get sensor data and store results in JSON
2684 getSensorData(sensorsAsyncResp, sensorNames, connections,
2685 objectMgrPaths, inventoryItems);
2686 BMCWEB_LOG_DEBUG << "getInventoryItemsCb exit";
2687 };
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002688
Ed Tanous002d39b2022-05-31 08:59:27 -07002689 // Get inventory items associated with sensors
2690 getInventoryItems(sensorsAsyncResp, sensorNames, objectMgrPaths,
2691 std::move(getInventoryItemsCb));
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002692
Ed Tanous002d39b2022-05-31 08:59:27 -07002693 BMCWEB_LOG_DEBUG << "getObjectManagerPathsCb exit";
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002694 };
2695
Ed Tanous002d39b2022-05-31 08:59:27 -07002696 // Get mapping from connection names to the DBus object
2697 // paths that implement the ObjectManager interface
2698 getObjectManagerPaths(sensorsAsyncResp,
2699 std::move(getObjectManagerPathsCb));
2700 BMCWEB_LOG_DEBUG << "getConnectionCb exit";
2701 };
2702
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002703 // Get set of connections that provide sensor values
Ed Tanous81ce6092020-12-17 16:54:55 +00002704 getConnections(sensorsAsyncResp, sensorNames, std::move(getConnectionCb));
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002705}
2706
Shawn McCarneyde629b62019-03-08 10:42:51 -06002707/**
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +01002708 * @brief Entry point for retrieving sensors data related to requested
2709 * chassis.
Kowalski, Kamil588c3f02018-04-03 14:55:27 +02002710 * @param SensorsAsyncResp Pointer to object holding response data
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +01002711 */
Ed Tanousb5a76932020-09-29 16:16:58 -07002712inline void
Ed Tanous81ce6092020-12-17 16:54:55 +00002713 getChassisData(const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp)
Ed Tanous1abe55e2018-09-05 08:30:59 -07002714{
2715 BMCWEB_LOG_DEBUG << "getChassisData enter";
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002716 auto getChassisCb =
Ed Tanous81ce6092020-12-17 16:54:55 +00002717 [sensorsAsyncResp](
Nan Zhoufe04d492022-06-22 17:10:41 +00002718 const std::shared_ptr<std::set<std::string>>& sensorNames) {
Ed Tanous002d39b2022-05-31 08:59:27 -07002719 BMCWEB_LOG_DEBUG << "getChassisCb enter";
2720 processSensorList(sensorsAsyncResp, sensorNames);
2721 BMCWEB_LOG_DEBUG << "getChassisCb exit";
2722 };
Nan Zhou928fefb2022-03-28 08:45:00 -07002723 // SensorCollection doesn't contain the Redundancy property
2724 if (sensorsAsyncResp->chassisSubNode != sensors::node::sensors)
2725 {
2726 sensorsAsyncResp->asyncResp->res.jsonValue["Redundancy"] =
2727 nlohmann::json::array();
2728 }
Shawn McCarney26f03892019-05-03 13:20:24 -05002729 // Get set of sensors in chassis
Ed Tanous81ce6092020-12-17 16:54:55 +00002730 getChassis(sensorsAsyncResp, std::move(getChassisCb));
Ed Tanous1abe55e2018-09-05 08:30:59 -07002731 BMCWEB_LOG_DEBUG << "getChassisData exit";
Ed Tanous271584a2019-07-09 16:24:22 -07002732}
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +01002733
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302734/**
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002735 * @brief Find the requested sensorName in the list of all sensors supplied by
2736 * the chassis node
2737 *
2738 * @param sensorName The sensor name supplied in the PATCH request
2739 * @param sensorsList The list of sensors managed by the chassis node
2740 * @param sensorsModified The list of sensors that were found as a result of
2741 * repeated calls to this function
2742 */
Nan Zhoufe04d492022-06-22 17:10:41 +00002743inline bool
2744 findSensorNameUsingSensorPath(std::string_view sensorName,
Ed Tanous02cad962022-06-30 16:50:15 -07002745 const std::set<std::string>& sensorsList,
Nan Zhoufe04d492022-06-22 17:10:41 +00002746 std::set<std::string>& sensorsModified)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002747{
Nan Zhoufe04d492022-06-22 17:10:41 +00002748 for (const auto& chassisSensor : sensorsList)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002749 {
George Liu28aa8de2021-02-01 15:13:30 +08002750 sdbusplus::message::object_path path(chassisSensor);
Ed Tanousb00dcc22021-02-23 12:52:50 -08002751 std::string thisSensorName = path.filename();
George Liu28aa8de2021-02-01 15:13:30 +08002752 if (thisSensorName.empty())
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002753 {
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002754 continue;
2755 }
2756 if (thisSensorName == sensorName)
2757 {
2758 sensorsModified.emplace(chassisSensor);
2759 return true;
2760 }
2761 }
2762 return false;
2763}
2764
2765/**
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302766 * @brief Entry point for overriding sensor values of given sensor
2767 *
zhanghch058d1b46d2021-04-01 11:18:24 +08002768 * @param sensorAsyncResp response object
Carol Wang4bb3dc32019-10-17 18:15:02 +08002769 * @param allCollections Collections extract from sensors' request patch info
jayaprakash Mutyala91e130a2020-03-04 22:26:38 +00002770 * @param chassisSubNode Chassis Node for which the query has to happen
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302771 */
Ed Tanous23a21a12020-07-25 04:45:05 +00002772inline void setSensorsOverride(
Ed Tanousb5a76932020-09-29 16:16:58 -07002773 const std::shared_ptr<SensorsAsyncResp>& sensorAsyncResp,
Carol Wang4bb3dc32019-10-17 18:15:02 +08002774 std::unordered_map<std::string, std::vector<nlohmann::json>>&
jayaprakash Mutyala397fd612020-02-06 23:33:34 +00002775 allCollections)
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302776{
jayaprakash Mutyala70d1d0a2020-01-21 23:41:36 +00002777 BMCWEB_LOG_INFO << "setSensorsOverride for subNode"
Carol Wang4bb3dc32019-10-17 18:15:02 +08002778 << sensorAsyncResp->chassisSubNode << "\n";
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302779
Ed Tanous543f4402022-01-06 13:12:53 -08002780 const char* propertyValueName = nullptr;
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302781 std::unordered_map<std::string, std::pair<double, std::string>> overrideMap;
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302782 std::string memberId;
Ed Tanous543f4402022-01-06 13:12:53 -08002783 double value = 0.0;
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302784 for (auto& collectionItems : allCollections)
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302785 {
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302786 if (collectionItems.first == "Temperatures")
2787 {
2788 propertyValueName = "ReadingCelsius";
2789 }
2790 else if (collectionItems.first == "Fans")
2791 {
2792 propertyValueName = "Reading";
2793 }
2794 else
2795 {
2796 propertyValueName = "ReadingVolts";
2797 }
2798 for (auto& item : collectionItems.second)
2799 {
zhanghch058d1b46d2021-04-01 11:18:24 +08002800 if (!json_util::readJson(item, sensorAsyncResp->asyncResp->res,
2801 "MemberId", memberId, propertyValueName,
2802 value))
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302803 {
2804 return;
2805 }
2806 overrideMap.emplace(memberId,
2807 std::make_pair(value, collectionItems.first));
2808 }
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302809 }
Carol Wang4bb3dc32019-10-17 18:15:02 +08002810
Ed Tanous002d39b2022-05-31 08:59:27 -07002811 auto getChassisSensorListCb =
2812 [sensorAsyncResp, overrideMap](
Nan Zhoufe04d492022-06-22 17:10:41 +00002813 const std::shared_ptr<std::set<std::string>>& sensorsList) {
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002814 // Match sensor names in the PATCH request to those managed by the
2815 // chassis node
Nan Zhoufe04d492022-06-22 17:10:41 +00002816 const std::shared_ptr<std::set<std::string>> sensorNames =
2817 std::make_shared<std::set<std::string>>();
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302818 for (const auto& item : overrideMap)
2819 {
2820 const auto& sensor = item.first;
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002821 if (!findSensorNameUsingSensorPath(sensor, *sensorsList,
2822 *sensorNames))
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302823 {
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302824 BMCWEB_LOG_INFO << "Unable to find memberId " << item.first;
zhanghch058d1b46d2021-04-01 11:18:24 +08002825 messages::resourceNotFound(sensorAsyncResp->asyncResp->res,
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302826 item.second.second, item.first);
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302827 return;
2828 }
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302829 }
2830 // Get the connection to which the memberId belongs
Ed Tanous002d39b2022-05-31 08:59:27 -07002831 auto getObjectsWithConnectionCb =
Nan Zhoufe04d492022-06-22 17:10:41 +00002832 [sensorAsyncResp,
2833 overrideMap](const std::set<std::string>& /*connections*/,
2834 const std::set<std::pair<std::string, std::string>>&
2835 objectsWithConnection) {
Jayaprakash Mutyala4f277b52021-12-08 22:46:49 +00002836 if (objectsWithConnection.size() != overrideMap.size())
2837 {
2838 BMCWEB_LOG_INFO
2839 << "Unable to find all objects with proper connection "
2840 << objectsWithConnection.size() << " requested "
2841 << overrideMap.size() << "\n";
2842 messages::resourceNotFound(sensorAsyncResp->asyncResp->res,
2843 sensorAsyncResp->chassisSubNode ==
2844 sensors::node::thermal
2845 ? "Temperatures"
2846 : "Voltages",
2847 "Count");
2848 return;
2849 }
2850 for (const auto& item : objectsWithConnection)
2851 {
2852 sdbusplus::message::object_path path(item.first);
2853 std::string sensorName = path.filename();
2854 if (sensorName.empty())
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302855 {
Jayaprakash Mutyala4f277b52021-12-08 22:46:49 +00002856 messages::internalError(sensorAsyncResp->asyncResp->res);
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302857 return;
2858 }
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302859
Jayaprakash Mutyala4f277b52021-12-08 22:46:49 +00002860 const auto& iterator = overrideMap.find(sensorName);
2861 if (iterator == overrideMap.end())
2862 {
2863 BMCWEB_LOG_INFO << "Unable to find sensor object"
2864 << item.first << "\n";
2865 messages::internalError(sensorAsyncResp->asyncResp->res);
2866 return;
2867 }
2868 crow::connections::systemBus->async_method_call(
2869 [sensorAsyncResp](const boost::system::error_code ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -07002870 if (ec)
2871 {
2872 if (ec.value() ==
2873 boost::system::errc::permission_denied)
Jayaprakash Mutyala4f277b52021-12-08 22:46:49 +00002874 {
Ed Tanous002d39b2022-05-31 08:59:27 -07002875 BMCWEB_LOG_WARNING
2876 << "Manufacturing mode is not Enabled...can't "
2877 "Override the sensor value. ";
Jayaprakash Mutyala4f277b52021-12-08 22:46:49 +00002878
Ed Tanous002d39b2022-05-31 08:59:27 -07002879 messages::insufficientPrivilege(
Jayaprakash Mutyala4f277b52021-12-08 22:46:49 +00002880 sensorAsyncResp->asyncResp->res);
Ed Tanous002d39b2022-05-31 08:59:27 -07002881 return;
Jayaprakash Mutyala4f277b52021-12-08 22:46:49 +00002882 }
Ed Tanous002d39b2022-05-31 08:59:27 -07002883 BMCWEB_LOG_DEBUG
2884 << "setOverrideValueStatus DBUS error: " << ec;
2885 messages::internalError(
2886 sensorAsyncResp->asyncResp->res);
2887 }
Jayaprakash Mutyala4f277b52021-12-08 22:46:49 +00002888 },
2889 item.second, item.first, "org.freedesktop.DBus.Properties",
2890 "Set", "xyz.openbmc_project.Sensor.Value", "Value",
Ed Tanous168e20c2021-12-13 14:39:53 -08002891 dbus::utility::DbusVariantType(iterator->second.first));
Jayaprakash Mutyala4f277b52021-12-08 22:46:49 +00002892 }
2893 };
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302894 // Get object with connection for the given sensor name
2895 getObjectsWithConnection(sensorAsyncResp, sensorNames,
2896 std::move(getObjectsWithConnectionCb));
2897 };
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302898 // get full sensor list for the given chassisId and cross verify the sensor.
2899 getChassis(sensorAsyncResp, std::move(getChassisSensorListCb));
2900}
2901
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02002902/**
2903 * @brief Retrieves mapping of Redfish URIs to sensor value property to D-Bus
2904 * path of the sensor.
2905 *
2906 * Function builds valid Redfish response for sensor query of given chassis and
2907 * node. It then builds metadata about Redfish<->D-Bus correlations and provides
2908 * it to caller in a callback.
2909 *
2910 * @param chassis Chassis for which retrieval should be performed
2911 * @param node Node (group) of sensors. See sensors::node for supported values
2912 * @param mapComplete Callback to be called with retrieval result
2913 */
Krzysztof Grobelny021d32c2021-10-29 16:00:07 +02002914inline void retrieveUriToDbusMap(const std::string& chassis,
2915 const std::string& node,
2916 SensorsAsyncResp::DataCompleteCb&& mapComplete)
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02002917{
Ed Tanous02da7c52022-02-27 00:09:02 -08002918 decltype(sensors::paths)::const_iterator pathIt =
2919 std::find_if(sensors::paths.cbegin(), sensors::paths.cend(),
2920 [&node](auto&& val) { return val.first == node; });
2921 if (pathIt == sensors::paths.cend())
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02002922 {
2923 BMCWEB_LOG_ERROR << "Wrong node provided : " << node;
2924 mapComplete(boost::beast::http::status::bad_request, {});
2925 return;
2926 }
Krzysztof Grobelnyd51e0722021-04-16 13:15:21 +00002927
Nan Zhou72374eb2022-01-27 17:06:51 -08002928 auto asyncResp = std::make_shared<bmcweb::AsyncResp>();
Nan Zhoufe04d492022-06-22 17:10:41 +00002929 auto callback = [asyncResp, mapCompleteCb{std::move(mapComplete)}](
2930 const boost::beast::http::status status,
2931 const std::map<std::string, std::string>& uriToDbus) {
2932 mapCompleteCb(status, uriToDbus);
2933 };
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02002934
2935 auto resp = std::make_shared<SensorsAsyncResp>(
Krzysztof Grobelnyd51e0722021-04-16 13:15:21 +00002936 asyncResp, chassis, pathIt->second, node, std::move(callback));
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02002937 getChassisData(resp);
2938}
2939
Nan Zhoubacb2162022-04-06 11:28:32 -07002940namespace sensors
2941{
Nan Zhou928fefb2022-03-28 08:45:00 -07002942
Nan Zhoubacb2162022-04-06 11:28:32 -07002943inline void getChassisCallback(
2944 const std::shared_ptr<SensorsAsyncResp>& asyncResp,
Nan Zhoufe04d492022-06-22 17:10:41 +00002945 const std::shared_ptr<std::set<std::string>>& sensorNames)
Nan Zhoubacb2162022-04-06 11:28:32 -07002946{
2947 BMCWEB_LOG_DEBUG << "getChassisCallback enter";
2948
2949 nlohmann::json& entriesArray =
2950 asyncResp->asyncResp->res.jsonValue["Members"];
Nan Zhoufe04d492022-06-22 17:10:41 +00002951 for (const auto& sensor : *sensorNames)
Nan Zhoubacb2162022-04-06 11:28:32 -07002952 {
2953 BMCWEB_LOG_DEBUG << "Adding sensor: " << sensor;
2954
2955 sdbusplus::message::object_path path(sensor);
2956 std::string sensorName = path.filename();
2957 if (sensorName.empty())
2958 {
2959 BMCWEB_LOG_ERROR << "Invalid sensor path: " << sensor;
2960 messages::internalError(asyncResp->asyncResp->res);
2961 return;
2962 }
Ed Tanous14766872022-03-15 10:44:42 -07002963 nlohmann::json::object_t member;
2964 member["@odata.id"] = "/redfish/v1/Chassis/" + asyncResp->chassisId +
2965 "/" + asyncResp->chassisSubNode + "/" +
2966 sensorName;
2967 entriesArray.push_back(std::move(member));
Nan Zhoubacb2162022-04-06 11:28:32 -07002968 }
2969
2970 asyncResp->asyncResp->res.jsonValue["Members@odata.count"] =
2971 entriesArray.size();
2972 BMCWEB_LOG_DEBUG << "getChassisCallback exit";
2973}
Nan Zhoue6bd8462022-06-01 04:35:35 +00002974
Nan Zhoude167a62022-06-01 04:47:45 +00002975inline void
2976 handleSensorCollectionGet(App& app, const crow::Request& req,
2977 const std::shared_ptr<bmcweb::AsyncResp>& aResp,
2978 const std::string& chassisId)
2979{
2980 query_param::QueryCapabilities capabilities = {
2981 .canDelegateExpandLevel = 1,
2982 };
2983 query_param::Query delegatedQuery;
Carson Labrado3ba00072022-06-06 19:40:56 +00002984 if (!redfish::setUpRedfishRouteWithDelegation(app, req, aResp,
Nan Zhoude167a62022-06-01 04:47:45 +00002985 delegatedQuery, capabilities))
2986 {
2987 return;
2988 }
2989
2990 if (delegatedQuery.expandType != query_param::ExpandType::None)
2991 {
2992 // we perform efficient expand.
2993 auto asyncResp = std::make_shared<SensorsAsyncResp>(
2994 aResp, chassisId, sensors::dbus::sensorPaths,
2995 sensors::node::sensors,
2996 /*efficientExpand=*/true);
2997 getChassisData(asyncResp);
2998
2999 BMCWEB_LOG_DEBUG
3000 << "SensorCollection doGet exit via efficient expand handler";
3001 return;
Ed Tanous0bad3202022-06-02 13:53:59 -07003002 }
Nan Zhoude167a62022-06-01 04:47:45 +00003003
3004 // if there's no efficient expand available, we use the default
3005 // Query Parameters route
3006 auto asyncResp = std::make_shared<SensorsAsyncResp>(
3007 aResp, chassisId, sensors::dbus::sensorPaths, sensors::node::sensors);
3008
3009 // We get all sensors as hyperlinkes in the chassis (this
3010 // implies we reply on the default query parameters handler)
3011 getChassis(asyncResp,
3012 std::bind_front(sensors::getChassisCallback, asyncResp));
3013 BMCWEB_LOG_DEBUG << "SensorCollection doGet exit";
3014}
3015
Nan Zhoue6bd8462022-06-01 04:35:35 +00003016inline void handleSensorGet(App& app, const crow::Request& req,
3017 const std::shared_ptr<bmcweb::AsyncResp>& aResp,
3018 const std::string& chassisId,
3019 const std::string& sensorName)
3020{
Carson Labrado3ba00072022-06-06 19:40:56 +00003021 if (!redfish::setUpRedfishRoute(app, req, aResp))
Nan Zhoue6bd8462022-06-01 04:35:35 +00003022 {
3023 return;
3024 }
3025 BMCWEB_LOG_DEBUG << "Sensor doGet enter";
3026 std::shared_ptr<SensorsAsyncResp> asyncResp =
3027 std::make_shared<SensorsAsyncResp>(aResp, chassisId,
3028 std::span<std::string_view>(),
3029 sensors::node::sensors);
3030
3031 const std::array<const char*, 1> interfaces = {
3032 "xyz.openbmc_project.Sensor.Value"};
3033
3034 // Get a list of all of the sensors that implement Sensor.Value
3035 // and get the path and service name associated with the sensor
3036 crow::connections::systemBus->async_method_call(
3037 [asyncResp,
3038 sensorName](const boost::system::error_code ec,
3039 const ::dbus::utility::MapperGetSubTreeResponse& subtree) {
3040 BMCWEB_LOG_DEBUG << "respHandler1 enter";
3041 if (ec)
3042 {
3043 messages::internalError(asyncResp->asyncResp->res);
3044 BMCWEB_LOG_ERROR << "Sensor getSensorPaths resp_handler: "
3045 << "Dbus error " << ec;
3046 return;
3047 }
3048
3049 ::dbus::utility::MapperGetSubTreeResponse::const_iterator it =
3050 std::find_if(
3051 subtree.begin(), subtree.end(),
3052 [sensorName](
3053 const std::pair<
3054 std::string,
3055 std::vector<std::pair<
3056 std::string, std::vector<std::string>>>>& object) {
3057 sdbusplus::message::object_path path(object.first);
3058 std::string name = path.filename();
3059 if (name.empty())
3060 {
3061 BMCWEB_LOG_ERROR << "Invalid sensor path: " << object.first;
3062 return false;
3063 }
3064
3065 return name == sensorName;
3066 });
3067
3068 if (it == subtree.end())
3069 {
3070 BMCWEB_LOG_ERROR << "Could not find path for sensor: "
3071 << sensorName;
3072 messages::resourceNotFound(asyncResp->asyncResp->res, "Sensor",
3073 sensorName);
3074 return;
3075 }
3076 std::string_view sensorPath = (*it).first;
3077 BMCWEB_LOG_DEBUG << "Found sensor path for sensor '" << sensorName
3078 << "': " << sensorPath;
3079
Nan Zhoufe04d492022-06-22 17:10:41 +00003080 const std::shared_ptr<std::set<std::string>> sensorList =
3081 std::make_shared<std::set<std::string>>();
Nan Zhoue6bd8462022-06-01 04:35:35 +00003082
3083 sensorList->emplace(sensorPath);
3084 processSensorList(asyncResp, sensorList);
3085 BMCWEB_LOG_DEBUG << "respHandler1 exit";
3086 },
3087 "xyz.openbmc_project.ObjectMapper",
3088 "/xyz/openbmc_project/object_mapper",
3089 "xyz.openbmc_project.ObjectMapper", "GetSubTree",
3090 "/xyz/openbmc_project/sensors", 2, interfaces);
3091}
3092
Nan Zhoubacb2162022-04-06 11:28:32 -07003093} // namespace sensors
3094
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003095inline void requestRoutesSensorCollection(App& app)
Anthony Wilson95a3eca2019-06-11 10:44:47 -05003096{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003097 BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/Sensors/")
Ed Tanoused398212021-06-09 17:05:54 -07003098 .privileges(redfish::privileges::getSensorCollection)
Ed Tanous002d39b2022-05-31 08:59:27 -07003099 .methods(boost::beast::http::verb::get)(
Nan Zhoude167a62022-06-01 04:47:45 +00003100 std::bind_front(sensors::handleSensorCollectionGet, std::ref(app)));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003101}
Anthony Wilson95a3eca2019-06-11 10:44:47 -05003102
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003103inline void requestRoutesSensor(App& app)
3104{
3105 BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/Sensors/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07003106 .privileges(redfish::privileges::getSensor)
Ed Tanous002d39b2022-05-31 08:59:27 -07003107 .methods(boost::beast::http::verb::get)(
Nan Zhoue6bd8462022-06-01 04:35:35 +00003108 std::bind_front(sensors::handleSensorGet, std::ref(app)));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003109}
Anthony Wilson95a3eca2019-06-11 10:44:47 -05003110
Ed Tanous1abe55e2018-09-05 08:30:59 -07003111} // namespace redfish