blob: d986565c450ac09c10dad9f6c8805991b06e331a [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>
21#include <boost/container/flat_map.hpp>
22#include <boost/range/algorithm/replace_copy_if.hpp>
Ed Tanous1abe55e2018-09-05 08:30:59 -070023#include <dbus_singleton.hpp>
Ed Tanoused398212021-06-09 17:05:54 -070024#include <registries/privilege_registry.hpp>
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +053025#include <utils/json_utils.hpp>
Gunnar Mills1214b7e2020-06-04 10:11:30 -050026
27#include <cmath>
Ed Tanousb5a76932020-09-29 16:16:58 -070028#include <utility>
Ed Tanousabf2add2019-01-22 16:40:12 -080029#include <variant>
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +010030
Ed Tanous1abe55e2018-09-05 08:30:59 -070031namespace redfish
32{
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +010033
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +010034using GetSubTreeType = std::vector<
35 std::pair<std::string,
36 std::vector<std::pair<std::string, std::vector<std::string>>>>>;
37
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -050038using SensorVariant =
39 std::variant<int64_t, double, uint32_t, bool, std::string>;
Ed Tanousaa2e59c2018-04-12 12:17:20 -070040
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +010041using ManagedObjectsVectorType = std::vector<std::pair<
Ed Tanousaa2e59c2018-04-12 12:17:20 -070042 sdbusplus::message::object_path,
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +010043 boost::container::flat_map<
Ed Tanousaa2e59c2018-04-12 12:17:20 -070044 std::string, boost::container::flat_map<std::string, SensorVariant>>>>;
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +010045
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +020046namespace sensors
47{
48namespace node
49{
50static constexpr std::string_view power = "Power";
51static constexpr std::string_view sensors = "Sensors";
52static constexpr std::string_view thermal = "Thermal";
53} // namespace node
54
55namespace dbus
56{
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +000057
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +020058static const boost::container::flat_map<std::string_view,
59 std::vector<const char*>>
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +000060 paths = {{node::power,
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +020061 {"/xyz/openbmc_project/sensors/voltage",
62 "/xyz/openbmc_project/sensors/power"}},
63 {node::sensors,
64 {"/xyz/openbmc_project/sensors/power",
65 "/xyz/openbmc_project/sensors/current",
66 "/xyz/openbmc_project/sensors/utilization"}},
67 {node::thermal,
68 {"/xyz/openbmc_project/sensors/fan_tach",
69 "/xyz/openbmc_project/sensors/temperature",
70 "/xyz/openbmc_project/sensors/fan_pwm"}}};
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +000071} // namespace dbus
72
73inline const char* toReadingType(const std::string& sensorType)
74{
75 if (sensorType == "voltage")
76 {
77 return "Voltage";
78 }
79 if (sensorType == "power")
80 {
81 return "Power";
82 }
83 if (sensorType == "current")
84 {
85 return "Current";
86 }
87 if (sensorType == "fan_tach")
88 {
89 return "Rotational";
90 }
91 if (sensorType == "temperature")
92 {
93 return "Temperature";
94 }
95 if (sensorType == "fan_pwm" || sensorType == "utilization")
96 {
97 return "Percent";
98 }
99 if (sensorType == "altitude")
100 {
101 return "Altitude";
102 }
103 if (sensorType == "airflow")
104 {
105 return "AirFlow";
106 }
107 if (sensorType == "energy")
108 {
109 return "EnergyJoules";
110 }
111 return "";
112}
113
114inline const char* toReadingUnits(const std::string& sensorType)
115{
116 if (sensorType == "voltage")
117 {
118 return "V";
119 }
120 if (sensorType == "power")
121 {
122 return "W";
123 }
124 if (sensorType == "current")
125 {
126 return "A";
127 }
128 if (sensorType == "fan_tach")
129 {
130 return "RPM";
131 }
132 if (sensorType == "temperature")
133 {
134 return "Cel";
135 }
136 if (sensorType == "fan_pwm" || sensorType == "utilization")
137 {
138 return "%";
139 }
140 if (sensorType == "altitude")
141 {
142 return "m";
143 }
144 if (sensorType == "airflow")
145 {
146 return "cft_i/min";
147 }
148 if (sensorType == "energy")
149 {
150 return "J";
151 }
152 return "";
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200153}
154} // namespace sensors
155
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100156/**
Kowalski, Kamil588c3f02018-04-03 14:55:27 +0200157 * SensorsAsyncResp
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100158 * Gathers data needed for response processing after async calls are done
159 */
Ed Tanous1abe55e2018-09-05 08:30:59 -0700160class SensorsAsyncResp
161{
162 public:
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200163 using DataCompleteCb = std::function<void(
164 const boost::beast::http::status status,
165 const boost::container::flat_map<std::string, std::string>& uriToDbus)>;
166
167 struct SensorData
168 {
169 const std::string name;
170 std::string uri;
171 const std::string valueKey;
172 const std::string dbusPath;
173 };
174
zhanghch058d1b46d2021-04-01 11:18:24 +0800175 SensorsAsyncResp(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
176 const std::string& chassisIdIn,
Ed Tanousb5a76932020-09-29 16:16:58 -0700177 const std::vector<const char*>& typesIn,
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200178 const std::string_view& subNode) :
zhanghch058d1b46d2021-04-01 11:18:24 +0800179 asyncResp(asyncResp),
Ed Tanous271584a2019-07-09 16:24:22 -0700180 chassisId(chassisIdIn), types(typesIn), chassisSubNode(subNode)
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500181 {}
Kowalski, Kamil588c3f02018-04-03 14:55:27 +0200182
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200183 // Store extra data about sensor mapping and return it in callback
zhanghch058d1b46d2021-04-01 11:18:24 +0800184 SensorsAsyncResp(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
185 const std::string& chassisIdIn,
Ed Tanousb5a76932020-09-29 16:16:58 -0700186 const std::vector<const char*>& typesIn,
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200187 const std::string_view& subNode,
188 DataCompleteCb&& creationComplete) :
zhanghch058d1b46d2021-04-01 11:18:24 +0800189 asyncResp(asyncResp),
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200190 chassisId(chassisIdIn), types(typesIn),
191 chassisSubNode(subNode), metadata{std::vector<SensorData>()},
192 dataComplete{std::move(creationComplete)}
193 {}
194
Ed Tanous1abe55e2018-09-05 08:30:59 -0700195 ~SensorsAsyncResp()
196 {
zhanghch058d1b46d2021-04-01 11:18:24 +0800197 if (asyncResp->res.result() ==
198 boost::beast::http::status::internal_server_error)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700199 {
200 // Reset the json object to clear out any data that made it in
201 // before the error happened todo(ed) handle error condition with
202 // proper code
zhanghch058d1b46d2021-04-01 11:18:24 +0800203 asyncResp->res.jsonValue = nlohmann::json::object();
Ed Tanous1abe55e2018-09-05 08:30:59 -0700204 }
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200205
206 if (dataComplete && metadata)
207 {
208 boost::container::flat_map<std::string, std::string> map;
zhanghch058d1b46d2021-04-01 11:18:24 +0800209 if (asyncResp->res.result() == boost::beast::http::status::ok)
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200210 {
211 for (auto& sensor : *metadata)
212 {
213 map.insert(std::make_pair(sensor.uri + sensor.valueKey,
214 sensor.dbusPath));
215 }
216 }
zhanghch058d1b46d2021-04-01 11:18:24 +0800217 dataComplete(asyncResp->res.result(), map);
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200218 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700219 }
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100220
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200221 void addMetadata(const nlohmann::json& sensorObject,
222 const std::string& valueKey, const std::string& dbusPath)
223 {
224 if (metadata)
225 {
226 metadata->emplace_back(SensorData{sensorObject["Name"],
227 sensorObject["@odata.id"],
228 valueKey, dbusPath});
229 }
230 }
231
232 void updateUri(const std::string& name, const std::string& uri)
233 {
234 if (metadata)
235 {
236 for (auto& sensor : *metadata)
237 {
238 if (sensor.name == name)
239 {
240 sensor.uri = uri;
241 }
242 }
243 }
244 }
245
zhanghch058d1b46d2021-04-01 11:18:24 +0800246 const std::shared_ptr<bmcweb::AsyncResp> asyncResp;
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200247 const std::string chassisId;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700248 const std::vector<const char*> types;
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200249 const std::string chassisSubNode;
250
251 private:
252 std::optional<std::vector<SensorData>> metadata;
253 DataCompleteCb dataComplete;
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100254};
255
256/**
Anthony Wilsond5005492019-07-31 16:34:17 -0500257 * Possible states for physical inventory leds
258 */
259enum class LedState
260{
261 OFF,
262 ON,
263 BLINK,
264 UNKNOWN
265};
266
267/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500268 * D-Bus inventory item associated with one or more sensors.
269 */
270class InventoryItem
271{
272 public:
273 InventoryItem(const std::string& objPath) :
274 objectPath(objPath), name(), isPresent(true), isFunctional(true),
Gunnar Mills42cbe532019-08-15 15:26:54 -0500275 isPowerSupply(false), powerSupplyEfficiencyPercent(-1), manufacturer(),
276 model(), partNumber(), serialNumber(), sensors(), ledObjectPath(""),
Anthony Wilsond5005492019-07-31 16:34:17 -0500277 ledState(LedState::UNKNOWN)
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500278 {
279 // Set inventory item name to last node of object path
George Liu28aa8de2021-02-01 15:13:30 +0800280 sdbusplus::message::object_path path(objectPath);
281 name = path.filename();
282 if (name.empty())
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500283 {
George Liu28aa8de2021-02-01 15:13:30 +0800284 BMCWEB_LOG_ERROR << "Failed to find '/' in " << objectPath;
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500285 }
286 }
287
288 std::string objectPath;
289 std::string name;
290 bool isPresent;
291 bool isFunctional;
292 bool isPowerSupply;
Gunnar Mills42cbe532019-08-15 15:26:54 -0500293 int powerSupplyEfficiencyPercent;
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500294 std::string manufacturer;
295 std::string model;
296 std::string partNumber;
297 std::string serialNumber;
298 std::set<std::string> sensors;
Anthony Wilsond5005492019-07-31 16:34:17 -0500299 std::string ledObjectPath;
300 LedState ledState;
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500301};
302
303/**
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530304 * @brief Get objects with connection necessary for sensors
Kowalski, Kamil588c3f02018-04-03 14:55:27 +0200305 * @param SensorsAsyncResp Pointer to object holding response data
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100306 * @param sensorNames Sensors retrieved from chassis
307 * @param callback Callback for processing gathered connections
308 */
309template <typename Callback>
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530310void getObjectsWithConnection(
Ed Tanous81ce6092020-12-17 16:54:55 +0000311 const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
Ed Tanousb5a76932020-09-29 16:16:58 -0700312 const std::shared_ptr<boost::container::flat_set<std::string>>& sensorNames,
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530313 Callback&& callback)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700314{
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530315 BMCWEB_LOG_DEBUG << "getObjectsWithConnection enter";
Ed Tanous1abe55e2018-09-05 08:30:59 -0700316 const std::string path = "/xyz/openbmc_project/sensors";
317 const std::array<std::string, 1> interfaces = {
318 "xyz.openbmc_project.Sensor.Value"};
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100319
Ed Tanous1abe55e2018-09-05 08:30:59 -0700320 // Response handler for parsing objects subtree
Ed Tanous81ce6092020-12-17 16:54:55 +0000321 auto respHandler = [callback{std::move(callback)}, sensorsAsyncResp,
Ed Tanous1abe55e2018-09-05 08:30:59 -0700322 sensorNames](const boost::system::error_code ec,
323 const GetSubTreeType& subtree) {
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530324 BMCWEB_LOG_DEBUG << "getObjectsWithConnection resp_handler enter";
Ed Tanous1abe55e2018-09-05 08:30:59 -0700325 if (ec)
326 {
zhanghch058d1b46d2021-04-01 11:18:24 +0800327 messages::internalError(sensorsAsyncResp->asyncResp->res);
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530328 BMCWEB_LOG_ERROR
329 << "getObjectsWithConnection resp_handler: Dbus error " << ec;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700330 return;
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100331 }
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100332
Ed Tanous1abe55e2018-09-05 08:30:59 -0700333 BMCWEB_LOG_DEBUG << "Found " << subtree.size() << " subtrees";
334
335 // Make unique list of connections only for requested sensor types and
336 // found in the chassis
337 boost::container::flat_set<std::string> connections;
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530338 std::set<std::pair<std::string, std::string>> objectsWithConnection;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700339 // Intrinsic to avoid malloc. Most systems will have < 8 sensor
340 // producers
341 connections.reserve(8);
342
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700343 BMCWEB_LOG_DEBUG << "sensorNames list count: " << sensorNames->size();
344 for (const std::string& tsensor : *sensorNames)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700345 {
346 BMCWEB_LOG_DEBUG << "Sensor to find: " << tsensor;
347 }
348
349 for (const std::pair<
350 std::string,
351 std::vector<std::pair<std::string, std::vector<std::string>>>>&
352 object : subtree)
353 {
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700354 if (sensorNames->find(object.first) != sensorNames->end())
Ed Tanous1abe55e2018-09-05 08:30:59 -0700355 {
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700356 for (const std::pair<std::string, std::vector<std::string>>&
357 objData : object.second)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700358 {
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700359 BMCWEB_LOG_DEBUG << "Adding connection: " << objData.first;
360 connections.insert(objData.first);
361 objectsWithConnection.insert(
362 std::make_pair(object.first, objData.first));
Ed Tanous1abe55e2018-09-05 08:30:59 -0700363 }
364 }
365 }
366 BMCWEB_LOG_DEBUG << "Found " << connections.size() << " connections";
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530367 callback(std::move(connections), std::move(objectsWithConnection));
368 BMCWEB_LOG_DEBUG << "getObjectsWithConnection resp_handler exit";
Ed Tanous1abe55e2018-09-05 08:30:59 -0700369 };
Ed Tanous1abe55e2018-09-05 08:30:59 -0700370 // Make call to ObjectMapper to find all sensors objects
371 crow::connections::systemBus->async_method_call(
372 std::move(respHandler), "xyz.openbmc_project.ObjectMapper",
373 "/xyz/openbmc_project/object_mapper",
374 "xyz.openbmc_project.ObjectMapper", "GetSubTree", path, 2, interfaces);
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530375 BMCWEB_LOG_DEBUG << "getObjectsWithConnection exit";
376}
377
378/**
379 * @brief Create connections necessary for sensors
380 * @param SensorsAsyncResp Pointer to object holding response data
381 * @param sensorNames Sensors retrieved from chassis
382 * @param callback Callback for processing gathered connections
383 */
384template <typename Callback>
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700385void getConnections(
Ed Tanous81ce6092020-12-17 16:54:55 +0000386 std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700387 const std::shared_ptr<boost::container::flat_set<std::string>> sensorNames,
388 Callback&& callback)
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530389{
390 auto objectsWithConnectionCb =
391 [callback](const boost::container::flat_set<std::string>& connections,
392 const std::set<std::pair<std::string, std::string>>&
Ed Tanous3174e4d2020-10-07 11:41:22 -0700393 /*objectsWithConnection*/) { callback(connections); };
Ed Tanous81ce6092020-12-17 16:54:55 +0000394 getObjectsWithConnection(sensorsAsyncResp, sensorNames,
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530395 std::move(objectsWithConnectionCb));
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100396}
397
398/**
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700399 * @brief Shrinks the list of sensors for processing
400 * @param SensorsAysncResp The class holding the Redfish response
401 * @param allSensors A list of all the sensors associated to the
402 * chassis element (i.e. baseboard, front panel, etc...)
403 * @param activeSensors A list that is a reduction of the incoming
404 * allSensors list. Eliminate Thermal sensors when a Power request is
405 * made, and eliminate Power sensors when a Thermal request is made.
406 */
Ed Tanous23a21a12020-07-25 04:45:05 +0000407inline void reduceSensorList(
Ed Tanous81ce6092020-12-17 16:54:55 +0000408 const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700409 const std::vector<std::string>* allSensors,
Ed Tanousb5a76932020-09-29 16:16:58 -0700410 const std::shared_ptr<boost::container::flat_set<std::string>>&
411 activeSensors)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700412{
Ed Tanous81ce6092020-12-17 16:54:55 +0000413 if (sensorsAsyncResp == nullptr)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700414 {
415 return;
416 }
417 if ((allSensors == nullptr) || (activeSensors == nullptr))
418 {
419 messages::resourceNotFound(
zhanghch058d1b46d2021-04-01 11:18:24 +0800420 sensorsAsyncResp->asyncResp->res, sensorsAsyncResp->chassisSubNode,
Ed Tanous81ce6092020-12-17 16:54:55 +0000421 sensorsAsyncResp->chassisSubNode == sensors::node::thermal
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200422 ? "Temperatures"
423 : "Voltages");
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700424
425 return;
426 }
427 if (allSensors->empty())
428 {
429 // Nothing to do, the activeSensors object is also empty
430 return;
431 }
432
Ed Tanous81ce6092020-12-17 16:54:55 +0000433 for (const char* type : sensorsAsyncResp->types)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700434 {
435 for (const std::string& sensor : *allSensors)
436 {
437 if (boost::starts_with(sensor, type))
438 {
439 activeSensors->emplace(sensor);
440 }
441 }
442 }
443}
444
445/**
Carol Wang4bb3dc32019-10-17 18:15:02 +0800446 * @brief Retrieves valid chassis path
447 * @param asyncResp Pointer to object holding response data
448 * @param callback Callback for next step to get valid chassis path
449 */
450template <typename Callback>
Ed Tanousb5a76932020-09-29 16:16:58 -0700451void getValidChassisPath(const std::shared_ptr<SensorsAsyncResp>& asyncResp,
Carol Wang4bb3dc32019-10-17 18:15:02 +0800452 Callback&& callback)
453{
454 BMCWEB_LOG_DEBUG << "checkChassisId enter";
455 const std::array<const char*, 2> interfaces = {
456 "xyz.openbmc_project.Inventory.Item.Board",
457 "xyz.openbmc_project.Inventory.Item.Chassis"};
458
459 auto respHandler =
460 [callback{std::move(callback)},
461 asyncResp](const boost::system::error_code ec,
462 const std::vector<std::string>& chassisPaths) mutable {
463 BMCWEB_LOG_DEBUG << "getValidChassisPath respHandler enter";
464 if (ec)
465 {
466 BMCWEB_LOG_ERROR
467 << "getValidChassisPath respHandler DBUS error: " << ec;
zhanghch058d1b46d2021-04-01 11:18:24 +0800468 messages::internalError(asyncResp->asyncResp->res);
Carol Wang4bb3dc32019-10-17 18:15:02 +0800469 return;
470 }
471
472 std::optional<std::string> chassisPath;
473 std::string chassisName;
474 for (const std::string& chassis : chassisPaths)
475 {
George Liu28aa8de2021-02-01 15:13:30 +0800476 sdbusplus::message::object_path path(chassis);
477 chassisName = path.filename();
478 if (chassisName.empty())
Carol Wang4bb3dc32019-10-17 18:15:02 +0800479 {
480 BMCWEB_LOG_ERROR << "Failed to find '/' in " << chassis;
481 continue;
482 }
Carol Wang4bb3dc32019-10-17 18:15:02 +0800483 if (chassisName == asyncResp->chassisId)
484 {
485 chassisPath = chassis;
486 break;
487 }
488 }
489 callback(chassisPath);
490 };
491
492 // Get the Chassis Collection
493 crow::connections::systemBus->async_method_call(
494 respHandler, "xyz.openbmc_project.ObjectMapper",
495 "/xyz/openbmc_project/object_mapper",
496 "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths",
497 "/xyz/openbmc_project/inventory", 0, interfaces);
498 BMCWEB_LOG_DEBUG << "checkChassisId exit";
499}
500
501/**
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100502 * @brief Retrieves requested chassis sensors and redundancy data from DBus .
Kowalski, Kamil588c3f02018-04-03 14:55:27 +0200503 * @param SensorsAsyncResp Pointer to object holding response data
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100504 * @param callback Callback for next step in gathered sensor processing
505 */
506template <typename Callback>
Ed Tanousb5a76932020-09-29 16:16:58 -0700507void getChassis(const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
Ed Tanous1abe55e2018-09-05 08:30:59 -0700508 Callback&& callback)
509{
510 BMCWEB_LOG_DEBUG << "getChassis enter";
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500511 const std::array<const char*, 2> interfaces = {
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700512 "xyz.openbmc_project.Inventory.Item.Board",
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500513 "xyz.openbmc_project.Inventory.Item.Chassis"};
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700514 auto respHandler = [callback{std::move(callback)}, sensorsAsyncResp](
515 const boost::system::error_code ec,
516 const std::vector<std::string>& chassisPaths) {
Ed Tanous1abe55e2018-09-05 08:30:59 -0700517 BMCWEB_LOG_DEBUG << "getChassis respHandler enter";
518 if (ec)
519 {
520 BMCWEB_LOG_ERROR << "getChassis respHandler DBUS error: " << ec;
zhanghch058d1b46d2021-04-01 11:18:24 +0800521 messages::internalError(sensorsAsyncResp->asyncResp->res);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700522 return;
523 }
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100524
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700525 const std::string* chassisPath = nullptr;
526 std::string chassisName;
527 for (const std::string& chassis : chassisPaths)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700528 {
George Liu28aa8de2021-02-01 15:13:30 +0800529 sdbusplus::message::object_path path(chassis);
530 chassisName = path.filename();
531 if (chassisName.empty())
Ed Tanous1abe55e2018-09-05 08:30:59 -0700532 {
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700533 BMCWEB_LOG_ERROR << "Failed to find '/' in " << chassis;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700534 continue;
535 }
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700536 if (chassisName == sensorsAsyncResp->chassisId)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700537 {
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700538 chassisPath = &chassis;
539 break;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700540 }
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700541 }
542 if (chassisPath == nullptr)
543 {
zhanghch058d1b46d2021-04-01 11:18:24 +0800544 messages::resourceNotFound(sensorsAsyncResp->asyncResp->res,
545 "Chassis", sensorsAsyncResp->chassisId);
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700546 return;
547 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700548
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700549 const std::string& chassisSubNode = sensorsAsyncResp->chassisSubNode;
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200550 if (chassisSubNode == sensors::node::power)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700551 {
zhanghch058d1b46d2021-04-01 11:18:24 +0800552 sensorsAsyncResp->asyncResp->res.jsonValue["@odata.type"] =
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700553 "#Power.v1_5_2.Power";
Ed Tanous1abe55e2018-09-05 08:30:59 -0700554 }
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200555 else if (chassisSubNode == sensors::node::thermal)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700556 {
zhanghch058d1b46d2021-04-01 11:18:24 +0800557 sensorsAsyncResp->asyncResp->res.jsonValue["@odata.type"] =
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700558 "#Thermal.v1_4_0.Thermal";
zhanghch058d1b46d2021-04-01 11:18:24 +0800559 sensorsAsyncResp->asyncResp->res.jsonValue["Fans"] =
560 nlohmann::json::array();
561 sensorsAsyncResp->asyncResp->res.jsonValue["Temperatures"] =
Jennifer Lee4f9a2132019-03-04 12:45:19 -0800562 nlohmann::json::array();
Ed Tanous1abe55e2018-09-05 08:30:59 -0700563 }
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200564 else if (chassisSubNode == sensors::node::sensors)
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500565 {
zhanghch058d1b46d2021-04-01 11:18:24 +0800566 sensorsAsyncResp->asyncResp->res.jsonValue["@odata.type"] =
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500567 "#SensorCollection.SensorCollection";
zhanghch058d1b46d2021-04-01 11:18:24 +0800568 sensorsAsyncResp->asyncResp->res.jsonValue["Description"] =
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500569 "Collection of Sensors for this Chassis";
zhanghch058d1b46d2021-04-01 11:18:24 +0800570 sensorsAsyncResp->asyncResp->res.jsonValue["Members"] =
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500571 nlohmann::json::array();
zhanghch058d1b46d2021-04-01 11:18:24 +0800572 sensorsAsyncResp->asyncResp->res.jsonValue["Members@odata.count"] =
573 0;
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500574 }
575
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200576 if (chassisSubNode != sensors::node::sensors)
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500577 {
zhanghch058d1b46d2021-04-01 11:18:24 +0800578 sensorsAsyncResp->asyncResp->res.jsonValue["Id"] = chassisSubNode;
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500579 }
580
zhanghch058d1b46d2021-04-01 11:18:24 +0800581 sensorsAsyncResp->asyncResp->res.jsonValue["@odata.id"] =
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700582 "/redfish/v1/Chassis/" + sensorsAsyncResp->chassisId + "/" +
583 chassisSubNode;
zhanghch058d1b46d2021-04-01 11:18:24 +0800584 sensorsAsyncResp->asyncResp->res.jsonValue["Name"] = chassisSubNode;
Shawn McCarney8fb49dd2019-06-12 17:47:00 -0500585 // Get the list of all sensors for this Chassis element
586 std::string sensorPath = *chassisPath + "/all_sensors";
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700587 crow::connections::systemBus->async_method_call(
588 [sensorsAsyncResp, callback{std::move(callback)}](
Ed Tanous271584a2019-07-09 16:24:22 -0700589 const boost::system::error_code& e,
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700590 const std::variant<std::vector<std::string>>&
591 variantEndpoints) {
Ed Tanous271584a2019-07-09 16:24:22 -0700592 if (e)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700593 {
Ed Tanous271584a2019-07-09 16:24:22 -0700594 if (e.value() != EBADR)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700595 {
zhanghch058d1b46d2021-04-01 11:18:24 +0800596 messages::internalError(
597 sensorsAsyncResp->asyncResp->res);
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700598 return;
599 }
600 }
601 const std::vector<std::string>* nodeSensorList =
602 std::get_if<std::vector<std::string>>(&(variantEndpoints));
603 if (nodeSensorList == nullptr)
604 {
605 messages::resourceNotFound(
zhanghch058d1b46d2021-04-01 11:18:24 +0800606 sensorsAsyncResp->asyncResp->res,
607 sensorsAsyncResp->chassisSubNode,
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200608 sensorsAsyncResp->chassisSubNode ==
609 sensors::node::thermal
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700610 ? "Temperatures"
Patrick Williams738c1e62021-02-22 17:14:25 -0600611 : sensorsAsyncResp->chassisSubNode ==
612 sensors::node::power
613 ? "Voltages"
614 : "Sensors");
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700615 return;
616 }
617 const std::shared_ptr<boost::container::flat_set<std::string>>
618 culledSensorList = std::make_shared<
619 boost::container::flat_set<std::string>>();
620 reduceSensorList(sensorsAsyncResp, nodeSensorList,
621 culledSensorList);
622 callback(culledSensorList);
623 },
624 "xyz.openbmc_project.ObjectMapper", sensorPath,
625 "org.freedesktop.DBus.Properties", "Get",
626 "xyz.openbmc_project.Association", "endpoints");
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100627 };
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100628
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700629 // Get the Chassis Collection
Ed Tanous1abe55e2018-09-05 08:30:59 -0700630 crow::connections::systemBus->async_method_call(
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700631 respHandler, "xyz.openbmc_project.ObjectMapper",
632 "/xyz/openbmc_project/object_mapper",
633 "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths",
Ed Tanous271584a2019-07-09 16:24:22 -0700634 "/xyz/openbmc_project/inventory", 0, interfaces);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700635 BMCWEB_LOG_DEBUG << "getChassis exit";
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100636}
637
638/**
Shawn McCarneyde629b62019-03-08 10:42:51 -0600639 * @brief Finds all DBus object paths that implement ObjectManager.
640 *
641 * Creates a mapping from the associated connection name to the object path.
642 *
643 * Finds the object paths asynchronously. Invokes callback when information has
644 * been obtained.
645 *
646 * The callback must have the following signature:
647 * @code
Shawn McCarney8fb49dd2019-06-12 17:47:00 -0500648 * callback(std::shared_ptr<boost::container::flat_map<std::string,
649 * std::string>> objectMgrPaths)
Shawn McCarneyde629b62019-03-08 10:42:51 -0600650 * @endcode
651 *
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700652 * @param sensorsAsyncResp Pointer to object holding response data.
Shawn McCarneyde629b62019-03-08 10:42:51 -0600653 * @param callback Callback to invoke when object paths obtained.
654 */
655template <typename Callback>
Ed Tanousb5a76932020-09-29 16:16:58 -0700656void getObjectManagerPaths(
Ed Tanous81ce6092020-12-17 16:54:55 +0000657 const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
Ed Tanousb5a76932020-09-29 16:16:58 -0700658 Callback&& callback)
Shawn McCarneyde629b62019-03-08 10:42:51 -0600659{
660 BMCWEB_LOG_DEBUG << "getObjectManagerPaths enter";
661 const std::array<std::string, 1> interfaces = {
662 "org.freedesktop.DBus.ObjectManager"};
663
664 // Response handler for GetSubTree DBus method
665 auto respHandler = [callback{std::move(callback)},
Ed Tanous81ce6092020-12-17 16:54:55 +0000666 sensorsAsyncResp](const boost::system::error_code ec,
Shawn McCarneyde629b62019-03-08 10:42:51 -0600667 const GetSubTreeType& subtree) {
668 BMCWEB_LOG_DEBUG << "getObjectManagerPaths respHandler enter";
669 if (ec)
670 {
zhanghch058d1b46d2021-04-01 11:18:24 +0800671 messages::internalError(sensorsAsyncResp->asyncResp->res);
Shawn McCarneyde629b62019-03-08 10:42:51 -0600672 BMCWEB_LOG_ERROR << "getObjectManagerPaths respHandler: DBus error "
673 << ec;
674 return;
675 }
676
677 // Loop over returned object paths
Shawn McCarney8fb49dd2019-06-12 17:47:00 -0500678 std::shared_ptr<boost::container::flat_map<std::string, std::string>>
679 objectMgrPaths = std::make_shared<
680 boost::container::flat_map<std::string, std::string>>();
Shawn McCarneyde629b62019-03-08 10:42:51 -0600681 for (const std::pair<
682 std::string,
683 std::vector<std::pair<std::string, std::vector<std::string>>>>&
684 object : subtree)
685 {
686 // Loop over connections for current object path
687 const std::string& objectPath = object.first;
688 for (const std::pair<std::string, std::vector<std::string>>&
689 objData : object.second)
690 {
691 // Add mapping from connection to object path
692 const std::string& connection = objData.first;
Shawn McCarney8fb49dd2019-06-12 17:47:00 -0500693 (*objectMgrPaths)[connection] = objectPath;
Shawn McCarneyde629b62019-03-08 10:42:51 -0600694 BMCWEB_LOG_DEBUG << "Added mapping " << connection << " -> "
695 << objectPath;
696 }
697 }
Shawn McCarney8fb49dd2019-06-12 17:47:00 -0500698 callback(objectMgrPaths);
Shawn McCarneyde629b62019-03-08 10:42:51 -0600699 BMCWEB_LOG_DEBUG << "getObjectManagerPaths respHandler exit";
700 };
701
702 // Query mapper for all DBus object paths that implement ObjectManager
703 crow::connections::systemBus->async_method_call(
704 std::move(respHandler), "xyz.openbmc_project.ObjectMapper",
705 "/xyz/openbmc_project/object_mapper",
Ed Tanous271584a2019-07-09 16:24:22 -0700706 "xyz.openbmc_project.ObjectMapper", "GetSubTree", "/", 0, interfaces);
Shawn McCarneyde629b62019-03-08 10:42:51 -0600707 BMCWEB_LOG_DEBUG << "getObjectManagerPaths exit";
708}
709
710/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500711 * @brief Returns the Redfish State value for the specified inventory item.
712 * @param inventoryItem D-Bus inventory item associated with a sensor.
713 * @return State value for inventory item.
James Feist34dd1792019-05-17 14:10:54 -0700714 */
Ed Tanous23a21a12020-07-25 04:45:05 +0000715inline std::string getState(const InventoryItem* inventoryItem)
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500716{
717 if ((inventoryItem != nullptr) && !(inventoryItem->isPresent))
718 {
719 return "Absent";
720 }
James Feist34dd1792019-05-17 14:10:54 -0700721
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500722 return "Enabled";
723}
724
725/**
726 * @brief Returns the Redfish Health value for the specified sensor.
727 * @param sensorJson Sensor JSON object.
728 * @param interfacesDict Map of all sensor interfaces.
729 * @param inventoryItem D-Bus inventory item associated with the sensor. Will
730 * be nullptr if no associated inventory item was found.
731 * @return Health value for sensor.
732 */
Ed Tanous23a21a12020-07-25 04:45:05 +0000733inline std::string getHealth(
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500734 nlohmann::json& sensorJson,
James Feist34dd1792019-05-17 14:10:54 -0700735 const boost::container::flat_map<
736 std::string, boost::container::flat_map<std::string, SensorVariant>>&
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500737 interfacesDict,
738 const InventoryItem* inventoryItem)
James Feist34dd1792019-05-17 14:10:54 -0700739{
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500740 // Get current health value (if any) in the sensor JSON object. Some JSON
741 // objects contain multiple sensors (such as PowerSupplies). We want to set
742 // the overall health to be the most severe of any of the sensors.
743 std::string currentHealth;
744 auto statusIt = sensorJson.find("Status");
745 if (statusIt != sensorJson.end())
746 {
747 auto healthIt = statusIt->find("Health");
748 if (healthIt != statusIt->end())
749 {
750 std::string* health = healthIt->get_ptr<std::string*>();
751 if (health != nullptr)
752 {
753 currentHealth = *health;
754 }
755 }
756 }
757
758 // If current health in JSON object is already Critical, return that. This
759 // should override the sensor health, which might be less severe.
760 if (currentHealth == "Critical")
761 {
762 return "Critical";
763 }
764
765 // Check if sensor has critical threshold alarm
James Feist34dd1792019-05-17 14:10:54 -0700766 auto criticalThresholdIt =
767 interfacesDict.find("xyz.openbmc_project.Sensor.Threshold.Critical");
768 if (criticalThresholdIt != interfacesDict.end())
769 {
770 auto thresholdHighIt =
771 criticalThresholdIt->second.find("CriticalAlarmHigh");
772 auto thresholdLowIt =
773 criticalThresholdIt->second.find("CriticalAlarmLow");
774 if (thresholdHighIt != criticalThresholdIt->second.end())
775 {
776 const bool* asserted = std::get_if<bool>(&thresholdHighIt->second);
777 if (asserted == nullptr)
778 {
779 BMCWEB_LOG_ERROR << "Illegal sensor threshold";
780 }
781 else if (*asserted)
782 {
783 return "Critical";
784 }
785 }
786 if (thresholdLowIt != criticalThresholdIt->second.end())
787 {
788 const bool* asserted = std::get_if<bool>(&thresholdLowIt->second);
789 if (asserted == nullptr)
790 {
791 BMCWEB_LOG_ERROR << "Illegal sensor threshold";
792 }
793 else if (*asserted)
794 {
795 return "Critical";
796 }
797 }
798 }
799
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500800 // Check if associated inventory item is not functional
801 if ((inventoryItem != nullptr) && !(inventoryItem->isFunctional))
802 {
803 return "Critical";
804 }
805
806 // If current health in JSON object is already Warning, return that. This
807 // should override the sensor status, which might be less severe.
808 if (currentHealth == "Warning")
809 {
810 return "Warning";
811 }
812
813 // Check if sensor has warning threshold alarm
James Feist34dd1792019-05-17 14:10:54 -0700814 auto warningThresholdIt =
815 interfacesDict.find("xyz.openbmc_project.Sensor.Threshold.Warning");
816 if (warningThresholdIt != interfacesDict.end())
817 {
818 auto thresholdHighIt =
819 warningThresholdIt->second.find("WarningAlarmHigh");
820 auto thresholdLowIt =
821 warningThresholdIt->second.find("WarningAlarmLow");
822 if (thresholdHighIt != warningThresholdIt->second.end())
823 {
824 const bool* asserted = std::get_if<bool>(&thresholdHighIt->second);
825 if (asserted == nullptr)
826 {
827 BMCWEB_LOG_ERROR << "Illegal sensor threshold";
828 }
829 else if (*asserted)
830 {
831 return "Warning";
832 }
833 }
834 if (thresholdLowIt != warningThresholdIt->second.end())
835 {
836 const bool* asserted = std::get_if<bool>(&thresholdLowIt->second);
837 if (asserted == nullptr)
838 {
839 BMCWEB_LOG_ERROR << "Illegal sensor threshold";
840 }
841 else if (*asserted)
842 {
843 return "Warning";
844 }
845 }
846 }
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500847
James Feist34dd1792019-05-17 14:10:54 -0700848 return "OK";
849}
850
Ed Tanous23a21a12020-07-25 04:45:05 +0000851inline void setLedState(nlohmann::json& sensorJson,
Anthony Wilsond5005492019-07-31 16:34:17 -0500852 const InventoryItem* inventoryItem)
853{
854 if (inventoryItem != nullptr && !inventoryItem->ledObjectPath.empty())
855 {
856 switch (inventoryItem->ledState)
857 {
858 case LedState::OFF:
859 sensorJson["IndicatorLED"] = "Off";
860 break;
861 case LedState::ON:
862 sensorJson["IndicatorLED"] = "Lit";
863 break;
864 case LedState::BLINK:
865 sensorJson["IndicatorLED"] = "Blinking";
866 break;
Ed Tanous23a21a12020-07-25 04:45:05 +0000867 case LedState::UNKNOWN:
Anthony Wilsond5005492019-07-31 16:34:17 -0500868 break;
869 }
870 }
871}
872
James Feist34dd1792019-05-17 14:10:54 -0700873/**
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100874 * @brief Builds a json sensor representation of a sensor.
875 * @param sensorName The name of the sensor to be built
Gunnar Mills274fad52018-06-13 15:45:36 -0500876 * @param sensorType The type (temperature, fan_tach, etc) of the sensor to
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100877 * build
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200878 * @param sensorsAsyncResp Sensor metadata
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100879 * @param interfacesDict A dictionary of the interfaces and properties of said
880 * interfaces to be built from
881 * @param sensor_json The json object to fill
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500882 * @param inventoryItem D-Bus inventory item associated with the sensor. Will
883 * be nullptr if no associated inventory item was found.
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100884 */
Ed Tanous23a21a12020-07-25 04:45:05 +0000885inline void objectInterfacesToJson(
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100886 const std::string& sensorName, const std::string& sensorType,
Ed Tanousb5a76932020-09-29 16:16:58 -0700887 const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100888 const boost::container::flat_map<
Ed Tanousaa2e59c2018-04-12 12:17:20 -0700889 std::string, boost::container::flat_map<std::string, SensorVariant>>&
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100890 interfacesDict,
Ed Tanous81ce6092020-12-17 16:54:55 +0000891 nlohmann::json& sensorJson, InventoryItem* inventoryItem)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700892{
893 // We need a value interface before we can do anything with it
894 auto valueIt = interfacesDict.find("xyz.openbmc_project.Sensor.Value");
895 if (valueIt == interfacesDict.end())
896 {
897 BMCWEB_LOG_ERROR << "Sensor doesn't have a value interface";
898 return;
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100899 }
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100900
Ed Tanous1abe55e2018-09-05 08:30:59 -0700901 // Assume values exist as is (10^0 == 1) if no scale exists
902 int64_t scaleMultiplier = 0;
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100903
Ed Tanous1abe55e2018-09-05 08:30:59 -0700904 auto scaleIt = valueIt->second.find("Scale");
905 // If a scale exists, pull value as int64, and use the scaling.
906 if (scaleIt != valueIt->second.end())
907 {
Ed Tanousabf2add2019-01-22 16:40:12 -0800908 const int64_t* int64Value = std::get_if<int64_t>(&scaleIt->second);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700909 if (int64Value != nullptr)
910 {
911 scaleMultiplier = *int64Value;
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100912 }
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100913 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700914
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200915 if (sensorsAsyncResp->chassisSubNode == sensors::node::sensors)
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500916 {
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500917 // For sensors in SensorCollection we set Id instead of MemberId,
918 // including power sensors.
Ed Tanous81ce6092020-12-17 16:54:55 +0000919 sensorJson["Id"] = sensorName;
920 sensorJson["Name"] = boost::replace_all_copy(sensorName, "_", " ");
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500921 }
922 else if (sensorType != "power")
923 {
924 // Set MemberId and Name for non-power sensors. For PowerSupplies and
925 // PowerControl, those properties have more general values because
926 // multiple sensors can be stored in the same JSON object.
Ed Tanous81ce6092020-12-17 16:54:55 +0000927 sensorJson["MemberId"] = sensorName;
928 sensorJson["Name"] = boost::replace_all_copy(sensorName, "_", " ");
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500929 }
Ed Tanouse742b6c2019-05-03 15:06:53 -0700930
Ed Tanous81ce6092020-12-17 16:54:55 +0000931 sensorJson["Status"]["State"] = getState(inventoryItem);
932 sensorJson["Status"]["Health"] =
933 getHealth(sensorJson, interfacesDict, inventoryItem);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700934
935 // Parameter to set to override the type we get from dbus, and force it to
936 // int, regardless of what is available. This is used for schemas like fan,
937 // that require integers, not floats.
938 bool forceToInt = false;
939
Anthony Wilson3929aca2019-07-19 15:42:33 -0500940 nlohmann::json::json_pointer unit("/Reading");
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200941 if (sensorsAsyncResp->chassisSubNode == sensors::node::sensors)
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500942 {
Ed Tanous81ce6092020-12-17 16:54:55 +0000943 sensorJson["@odata.type"] = "#Sensor.v1_0_0.Sensor";
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000944
945 const std::string& readingType = sensors::toReadingType(sensorType);
946 if (readingType.empty())
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500947 {
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000948 BMCWEB_LOG_ERROR << "Redfish cannot map reading type for "
949 << sensorType;
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500950 }
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000951 else
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500952 {
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000953 sensorJson["ReadingType"] = readingType;
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500954 }
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000955
956 const std::string& readingUnits = sensors::toReadingUnits(sensorType);
957 if (readingUnits.empty())
Adrian Ambrożewiczf8ede152020-06-02 13:26:33 +0200958 {
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000959 BMCWEB_LOG_ERROR << "Redfish cannot map reading unit for "
960 << sensorType;
961 }
962 else
963 {
964 sensorJson["ReadingUnits"] = readingUnits;
Adrian Ambrożewiczf8ede152020-06-02 13:26:33 +0200965 }
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500966 }
967 else if (sensorType == "temperature")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700968 {
Anthony Wilson3929aca2019-07-19 15:42:33 -0500969 unit = "/ReadingCelsius"_json_pointer;
Ed Tanous81ce6092020-12-17 16:54:55 +0000970 sensorJson["@odata.type"] = "#Thermal.v1_3_0.Temperature";
Ed Tanous1abe55e2018-09-05 08:30:59 -0700971 // TODO(ed) Documentation says that path should be type fan_tach,
972 // implementation seems to implement fan
973 }
974 else if (sensorType == "fan" || sensorType == "fan_tach")
975 {
Anthony Wilson3929aca2019-07-19 15:42:33 -0500976 unit = "/Reading"_json_pointer;
Ed Tanous81ce6092020-12-17 16:54:55 +0000977 sensorJson["ReadingUnits"] = "RPM";
978 sensorJson["@odata.type"] = "#Thermal.v1_3_0.Fan";
979 setLedState(sensorJson, inventoryItem);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700980 forceToInt = true;
981 }
Ed Tanous6f6d0d32018-10-12 11:16:43 -0700982 else if (sensorType == "fan_pwm")
983 {
Anthony Wilson3929aca2019-07-19 15:42:33 -0500984 unit = "/Reading"_json_pointer;
Ed Tanous81ce6092020-12-17 16:54:55 +0000985 sensorJson["ReadingUnits"] = "Percent";
986 sensorJson["@odata.type"] = "#Thermal.v1_3_0.Fan";
987 setLedState(sensorJson, inventoryItem);
Ed Tanous6f6d0d32018-10-12 11:16:43 -0700988 forceToInt = true;
989 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700990 else if (sensorType == "voltage")
991 {
Anthony Wilson3929aca2019-07-19 15:42:33 -0500992 unit = "/ReadingVolts"_json_pointer;
Ed Tanous81ce6092020-12-17 16:54:55 +0000993 sensorJson["@odata.type"] = "#Power.v1_0_0.Voltage";
Ed Tanous1abe55e2018-09-05 08:30:59 -0700994 }
Ed Tanous2474adf2018-09-05 16:31:16 -0700995 else if (sensorType == "power")
996 {
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700997 std::string sensorNameLower =
998 boost::algorithm::to_lower_copy(sensorName);
999
Eddie James028f7eb2019-05-17 21:24:36 +00001000 if (!sensorName.compare("total_power"))
1001 {
Ed Tanous81ce6092020-12-17 16:54:55 +00001002 sensorJson["@odata.type"] = "#Power.v1_0_0.PowerControl";
Gunnar Mills7ab06f42019-07-02 13:07:16 -05001003 // Put multiple "sensors" into a single PowerControl, so have
1004 // generic names for MemberId and Name. Follows Redfish mockup.
Ed Tanous81ce6092020-12-17 16:54:55 +00001005 sensorJson["MemberId"] = "0";
1006 sensorJson["Name"] = "Chassis Power Control";
Anthony Wilson3929aca2019-07-19 15:42:33 -05001007 unit = "/PowerConsumedWatts"_json_pointer;
Eddie James028f7eb2019-05-17 21:24:36 +00001008 }
1009 else if (sensorNameLower.find("input") != std::string::npos)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07001010 {
Anthony Wilson3929aca2019-07-19 15:42:33 -05001011 unit = "/PowerInputWatts"_json_pointer;
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07001012 }
1013 else
1014 {
Anthony Wilson3929aca2019-07-19 15:42:33 -05001015 unit = "/PowerOutputWatts"_json_pointer;
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07001016 }
Ed Tanous2474adf2018-09-05 16:31:16 -07001017 }
Ed Tanous1abe55e2018-09-05 08:30:59 -07001018 else
1019 {
1020 BMCWEB_LOG_ERROR << "Redfish cannot map object type for " << sensorName;
1021 return;
1022 }
1023 // Map of dbus interface name, dbus property name and redfish property_name
Anthony Wilson3929aca2019-07-19 15:42:33 -05001024 std::vector<
1025 std::tuple<const char*, const char*, nlohmann::json::json_pointer>>
1026 properties;
Ed Tanous1abe55e2018-09-05 08:30:59 -07001027 properties.reserve(7);
1028
1029 properties.emplace_back("xyz.openbmc_project.Sensor.Value", "Value", unit);
Shawn McCarneyde629b62019-03-08 10:42:51 -06001030
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02001031 if (sensorsAsyncResp->chassisSubNode == sensors::node::sensors)
Anthony Wilson3929aca2019-07-19 15:42:33 -05001032 {
1033 properties.emplace_back(
1034 "xyz.openbmc_project.Sensor.Threshold.Warning", "WarningHigh",
1035 "/Thresholds/UpperCaution/Reading"_json_pointer);
1036 properties.emplace_back(
1037 "xyz.openbmc_project.Sensor.Threshold.Warning", "WarningLow",
1038 "/Thresholds/LowerCaution/Reading"_json_pointer);
1039 properties.emplace_back(
1040 "xyz.openbmc_project.Sensor.Threshold.Critical", "CriticalHigh",
1041 "/Thresholds/UpperCritical/Reading"_json_pointer);
1042 properties.emplace_back(
1043 "xyz.openbmc_project.Sensor.Threshold.Critical", "CriticalLow",
1044 "/Thresholds/LowerCritical/Reading"_json_pointer);
1045 }
1046 else if (sensorType != "power")
Shawn McCarneyde629b62019-03-08 10:42:51 -06001047 {
1048 properties.emplace_back("xyz.openbmc_project.Sensor.Threshold.Warning",
Anthony Wilson3929aca2019-07-19 15:42:33 -05001049 "WarningHigh",
1050 "/UpperThresholdNonCritical"_json_pointer);
Shawn McCarneyde629b62019-03-08 10:42:51 -06001051 properties.emplace_back("xyz.openbmc_project.Sensor.Threshold.Warning",
Anthony Wilson3929aca2019-07-19 15:42:33 -05001052 "WarningLow",
1053 "/LowerThresholdNonCritical"_json_pointer);
Shawn McCarneyde629b62019-03-08 10:42:51 -06001054 properties.emplace_back("xyz.openbmc_project.Sensor.Threshold.Critical",
Anthony Wilson3929aca2019-07-19 15:42:33 -05001055 "CriticalHigh",
1056 "/UpperThresholdCritical"_json_pointer);
Shawn McCarneyde629b62019-03-08 10:42:51 -06001057 properties.emplace_back("xyz.openbmc_project.Sensor.Threshold.Critical",
Anthony Wilson3929aca2019-07-19 15:42:33 -05001058 "CriticalLow",
1059 "/LowerThresholdCritical"_json_pointer);
Shawn McCarneyde629b62019-03-08 10:42:51 -06001060 }
Ed Tanous1abe55e2018-09-05 08:30:59 -07001061
Ed Tanous2474adf2018-09-05 16:31:16 -07001062 // TODO Need to get UpperThresholdFatal and LowerThresholdFatal
1063
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02001064 if (sensorsAsyncResp->chassisSubNode == sensors::node::sensors)
Anthony Wilson95a3eca2019-06-11 10:44:47 -05001065 {
1066 properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MinValue",
Anthony Wilson3929aca2019-07-19 15:42:33 -05001067 "/ReadingRangeMin"_json_pointer);
Anthony Wilson95a3eca2019-06-11 10:44:47 -05001068 properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MaxValue",
Anthony Wilson3929aca2019-07-19 15:42:33 -05001069 "/ReadingRangeMax"_json_pointer);
Anthony Wilson95a3eca2019-06-11 10:44:47 -05001070 }
1071 else if (sensorType == "temperature")
Ed Tanous1abe55e2018-09-05 08:30:59 -07001072 {
1073 properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MinValue",
Anthony Wilson3929aca2019-07-19 15:42:33 -05001074 "/MinReadingRangeTemp"_json_pointer);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001075 properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MaxValue",
Anthony Wilson3929aca2019-07-19 15:42:33 -05001076 "/MaxReadingRangeTemp"_json_pointer);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001077 }
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001078 else if (sensorType != "power")
Ed Tanous1abe55e2018-09-05 08:30:59 -07001079 {
1080 properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MinValue",
Anthony Wilson3929aca2019-07-19 15:42:33 -05001081 "/MinReadingRange"_json_pointer);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001082 properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MaxValue",
Anthony Wilson3929aca2019-07-19 15:42:33 -05001083 "/MaxReadingRange"_json_pointer);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001084 }
1085
Anthony Wilson3929aca2019-07-19 15:42:33 -05001086 for (const std::tuple<const char*, const char*,
1087 nlohmann::json::json_pointer>& p : properties)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001088 {
1089 auto interfaceProperties = interfacesDict.find(std::get<0>(p));
1090 if (interfaceProperties != interfacesDict.end())
1091 {
Ed Tanous271584a2019-07-09 16:24:22 -07001092 auto thisValueIt = interfaceProperties->second.find(std::get<1>(p));
1093 if (thisValueIt != interfaceProperties->second.end())
Ed Tanous1abe55e2018-09-05 08:30:59 -07001094 {
Ed Tanous271584a2019-07-09 16:24:22 -07001095 const SensorVariant& valueVariant = thisValueIt->second;
Anthony Wilson3929aca2019-07-19 15:42:33 -05001096
1097 // The property we want to set may be nested json, so use
1098 // a json_pointer for easy indexing into the json structure.
1099 const nlohmann::json::json_pointer& key = std::get<2>(p);
1100
Ed Tanous1abe55e2018-09-05 08:30:59 -07001101 // Attempt to pull the int64 directly
Ed Tanousabf2add2019-01-22 16:40:12 -08001102 const int64_t* int64Value = std::get_if<int64_t>(&valueVariant);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001103
Ed Tanousabf2add2019-01-22 16:40:12 -08001104 const double* doubleValue = std::get_if<double>(&valueVariant);
Eddie James028f7eb2019-05-17 21:24:36 +00001105 const uint32_t* uValue = std::get_if<uint32_t>(&valueVariant);
Ed Tanous6f6d0d32018-10-12 11:16:43 -07001106 double temp = 0.0;
1107 if (int64Value != nullptr)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001108 {
Ed Tanous271584a2019-07-09 16:24:22 -07001109 temp = static_cast<double>(*int64Value);
Ed Tanous6f6d0d32018-10-12 11:16:43 -07001110 }
1111 else if (doubleValue != nullptr)
1112 {
1113 temp = *doubleValue;
1114 }
Eddie James028f7eb2019-05-17 21:24:36 +00001115 else if (uValue != nullptr)
1116 {
1117 temp = *uValue;
1118 }
Ed Tanous6f6d0d32018-10-12 11:16:43 -07001119 else
1120 {
1121 BMCWEB_LOG_ERROR
1122 << "Got value interface that wasn't int or double";
1123 continue;
1124 }
1125 temp = temp * std::pow(10, scaleMultiplier);
1126 if (forceToInt)
1127 {
Ed Tanous81ce6092020-12-17 16:54:55 +00001128 sensorJson[key] = static_cast<int64_t>(temp);
Ed Tanous6f6d0d32018-10-12 11:16:43 -07001129 }
1130 else
1131 {
Ed Tanous81ce6092020-12-17 16:54:55 +00001132 sensorJson[key] = temp;
Ed Tanous1abe55e2018-09-05 08:30:59 -07001133 }
1134 }
1135 }
1136 }
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02001137
Ed Tanous81ce6092020-12-17 16:54:55 +00001138 sensorsAsyncResp->addMetadata(sensorJson, unit.to_string(),
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02001139 "/xyz/openbmc_project/sensors/" + sensorType +
1140 "/" + sensorName);
1141
Ed Tanous1abe55e2018-09-05 08:30:59 -07001142 BMCWEB_LOG_DEBUG << "Added sensor " << sensorName;
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +01001143}
1144
Ed Tanousb5a76932020-09-29 16:16:58 -07001145inline void populateFanRedundancy(
1146 const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp)
James Feist8bd25cc2019-03-15 15:14:00 -07001147{
1148 crow::connections::systemBus->async_method_call(
1149 [sensorsAsyncResp](const boost::system::error_code ec,
1150 const GetSubTreeType& resp) {
1151 if (ec)
1152 {
1153 return; // don't have to have this interface
1154 }
Ed Tanouse278c182019-03-13 16:23:37 -07001155 for (const std::pair<std::string,
1156 std::vector<std::pair<
1157 std::string, std::vector<std::string>>>>&
1158 pathPair : resp)
James Feist8bd25cc2019-03-15 15:14:00 -07001159 {
Ed Tanouse278c182019-03-13 16:23:37 -07001160 const std::string& path = pathPair.first;
1161 const std::vector<
1162 std::pair<std::string, std::vector<std::string>>>& objDict =
1163 pathPair.second;
James Feist8bd25cc2019-03-15 15:14:00 -07001164 if (objDict.empty())
1165 {
1166 continue; // this should be impossible
1167 }
1168
1169 const std::string& owner = objDict.begin()->first;
1170 crow::connections::systemBus->async_method_call(
1171 [path, owner,
Ed Tanous271584a2019-07-09 16:24:22 -07001172 sensorsAsyncResp](const boost::system::error_code e,
James Feist8bd25cc2019-03-15 15:14:00 -07001173 std::variant<std::vector<std::string>>
1174 variantEndpoints) {
Ed Tanous271584a2019-07-09 16:24:22 -07001175 if (e)
James Feist8bd25cc2019-03-15 15:14:00 -07001176 {
1177 return; // if they don't have an association we
1178 // can't tell what chassis is
1179 }
1180 // verify part of the right chassis
1181 auto endpoints = std::get_if<std::vector<std::string>>(
1182 &variantEndpoints);
1183
1184 if (endpoints == nullptr)
1185 {
1186 BMCWEB_LOG_ERROR << "Invalid association interface";
zhanghch058d1b46d2021-04-01 11:18:24 +08001187 messages::internalError(
1188 sensorsAsyncResp->asyncResp->res);
James Feist8bd25cc2019-03-15 15:14:00 -07001189 return;
1190 }
1191
1192 auto found = std::find_if(
1193 endpoints->begin(), endpoints->end(),
1194 [sensorsAsyncResp](const std::string& entry) {
1195 return entry.find(
1196 sensorsAsyncResp->chassisId) !=
1197 std::string::npos;
1198 });
1199
1200 if (found == endpoints->end())
1201 {
1202 return;
1203 }
1204 crow::connections::systemBus->async_method_call(
1205 [path, sensorsAsyncResp](
Ed Tanous271584a2019-07-09 16:24:22 -07001206 const boost::system::error_code& err,
James Feist8bd25cc2019-03-15 15:14:00 -07001207 const boost::container::flat_map<
1208 std::string,
1209 std::variant<uint8_t,
1210 std::vector<std::string>,
1211 std::string>>& ret) {
Ed Tanous271584a2019-07-09 16:24:22 -07001212 if (err)
James Feist8bd25cc2019-03-15 15:14:00 -07001213 {
1214 return; // don't have to have this
1215 // interface
1216 }
1217 auto findFailures = ret.find("AllowedFailures");
1218 auto findCollection = ret.find("Collection");
1219 auto findStatus = ret.find("Status");
1220
1221 if (findFailures == ret.end() ||
1222 findCollection == ret.end() ||
1223 findStatus == ret.end())
1224 {
1225 BMCWEB_LOG_ERROR
1226 << "Invalid redundancy interface";
1227 messages::internalError(
zhanghch058d1b46d2021-04-01 11:18:24 +08001228 sensorsAsyncResp->asyncResp->res);
James Feist8bd25cc2019-03-15 15:14:00 -07001229 return;
1230 }
1231
1232 auto allowedFailures = std::get_if<uint8_t>(
1233 &(findFailures->second));
1234 auto collection =
1235 std::get_if<std::vector<std::string>>(
1236 &(findCollection->second));
1237 auto status = std::get_if<std::string>(
1238 &(findStatus->second));
1239
1240 if (allowedFailures == nullptr ||
1241 collection == nullptr || status == nullptr)
1242 {
1243
1244 BMCWEB_LOG_ERROR
1245 << "Invalid redundancy interface "
1246 "types";
1247 messages::internalError(
zhanghch058d1b46d2021-04-01 11:18:24 +08001248 sensorsAsyncResp->asyncResp->res);
James Feist8bd25cc2019-03-15 15:14:00 -07001249 return;
1250 }
George Liu28aa8de2021-02-01 15:13:30 +08001251 sdbusplus::message::object_path objectPath(
1252 path);
1253 std::string name = objectPath.filename();
1254 if (name.empty())
James Feist8bd25cc2019-03-15 15:14:00 -07001255 {
1256 // this should be impossible
1257 messages::internalError(
zhanghch058d1b46d2021-04-01 11:18:24 +08001258 sensorsAsyncResp->asyncResp->res);
James Feist8bd25cc2019-03-15 15:14:00 -07001259 return;
1260 }
James Feist8bd25cc2019-03-15 15:14:00 -07001261 std::replace(name.begin(), name.end(), '_',
1262 ' ');
1263
1264 std::string health;
1265
1266 if (boost::ends_with(*status, "Full"))
1267 {
1268 health = "OK";
1269 }
1270 else if (boost::ends_with(*status, "Degraded"))
1271 {
1272 health = "Warning";
1273 }
1274 else
1275 {
1276 health = "Critical";
1277 }
1278 std::vector<nlohmann::json> redfishCollection;
1279 const auto& fanRedfish =
zhanghch058d1b46d2021-04-01 11:18:24 +08001280 sensorsAsyncResp->asyncResp->res
1281 .jsonValue["Fans"];
James Feist8bd25cc2019-03-15 15:14:00 -07001282 for (const std::string& item : *collection)
1283 {
George Liu28aa8de2021-02-01 15:13:30 +08001284 sdbusplus::message::object_path path(item);
1285 std::string itemName = path.filename();
1286 if (itemName.empty())
1287 {
1288 continue;
1289 }
James Feist8bd25cc2019-03-15 15:14:00 -07001290 /*
1291 todo(ed): merge patch that fixes the names
1292 std::replace(itemName.begin(),
1293 itemName.end(), '_', ' ');*/
1294 auto schemaItem = std::find_if(
1295 fanRedfish.begin(), fanRedfish.end(),
1296 [itemName](const nlohmann::json& fan) {
1297 return fan["MemberId"] == itemName;
1298 });
1299 if (schemaItem != fanRedfish.end())
1300 {
1301 redfishCollection.push_back(
1302 {{"@odata.id",
1303 (*schemaItem)["@odata.id"]}});
1304 }
1305 else
1306 {
1307 BMCWEB_LOG_ERROR
1308 << "failed to find fan in schema";
1309 messages::internalError(
zhanghch058d1b46d2021-04-01 11:18:24 +08001310 sensorsAsyncResp->asyncResp->res);
James Feist8bd25cc2019-03-15 15:14:00 -07001311 return;
1312 }
1313 }
1314
Kuiying Wang3e9e72e2020-07-07 10:18:32 +08001315 size_t minNumNeeded =
1316 collection->size() > 0
1317 ? collection->size() - *allowedFailures
1318 : 0;
Ed Tanous271584a2019-07-09 16:24:22 -07001319 nlohmann::json& jResp =
zhanghch058d1b46d2021-04-01 11:18:24 +08001320 sensorsAsyncResp->asyncResp->res
Ed Tanous271584a2019-07-09 16:24:22 -07001321 .jsonValue["Redundancy"];
1322 jResp.push_back(
James Feist8bd25cc2019-03-15 15:14:00 -07001323 {{"@odata.id",
AppaRao Puli717794d2019-10-18 22:54:53 +05301324 "/redfish/v1/Chassis/" +
James Feist8bd25cc2019-03-15 15:14:00 -07001325 sensorsAsyncResp->chassisId + "/" +
1326 sensorsAsyncResp->chassisSubNode +
1327 "#/Redundancy/" +
Ed Tanous271584a2019-07-09 16:24:22 -07001328 std::to_string(jResp.size())},
James Feist8bd25cc2019-03-15 15:14:00 -07001329 {"@odata.type",
1330 "#Redundancy.v1_3_2.Redundancy"},
Kuiying Wang3e9e72e2020-07-07 10:18:32 +08001331 {"MinNumNeeded", minNumNeeded},
James Feist8bd25cc2019-03-15 15:14:00 -07001332 {"MemberId", name},
1333 {"Mode", "N+m"},
1334 {"Name", name},
1335 {"RedundancySet", redfishCollection},
1336 {"Status",
1337 {{"Health", health},
1338 {"State", "Enabled"}}}});
1339 },
1340 owner, path, "org.freedesktop.DBus.Properties",
1341 "GetAll",
1342 "xyz.openbmc_project.Control.FanRedundancy");
1343 },
James Feist02e92e32019-06-26 12:07:05 -07001344 "xyz.openbmc_project.ObjectMapper", path + "/chassis",
James Feist8bd25cc2019-03-15 15:14:00 -07001345 "org.freedesktop.DBus.Properties", "Get",
1346 "xyz.openbmc_project.Association", "endpoints");
1347 }
1348 },
1349 "xyz.openbmc_project.ObjectMapper",
1350 "/xyz/openbmc_project/object_mapper",
1351 "xyz.openbmc_project.ObjectMapper", "GetSubTree",
1352 "/xyz/openbmc_project/control", 2,
1353 std::array<const char*, 1>{
1354 "xyz.openbmc_project.Control.FanRedundancy"});
1355}
1356
Ed Tanousb5a76932020-09-29 16:16:58 -07001357inline void
Ed Tanous81ce6092020-12-17 16:54:55 +00001358 sortJSONResponse(const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07001359{
zhanghch058d1b46d2021-04-01 11:18:24 +08001360 nlohmann::json& response = sensorsAsyncResp->asyncResp->res.jsonValue;
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07001361 std::array<std::string, 2> sensorHeaders{"Temperatures", "Fans"};
Ed Tanous81ce6092020-12-17 16:54:55 +00001362 if (sensorsAsyncResp->chassisSubNode == sensors::node::power)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07001363 {
1364 sensorHeaders = {"Voltages", "PowerSupplies"};
1365 }
1366 for (const std::string& sensorGroup : sensorHeaders)
1367 {
1368 nlohmann::json::iterator entry = response.find(sensorGroup);
1369 if (entry != response.end())
1370 {
1371 std::sort(entry->begin(), entry->end(),
1372 [](nlohmann::json& c1, nlohmann::json& c2) {
1373 return c1["Name"] < c2["Name"];
1374 });
1375
1376 // add the index counts to the end of each entry
1377 size_t count = 0;
1378 for (nlohmann::json& sensorJson : *entry)
1379 {
1380 nlohmann::json::iterator odata = sensorJson.find("@odata.id");
1381 if (odata == sensorJson.end())
1382 {
1383 continue;
1384 }
1385 std::string* value = odata->get_ptr<std::string*>();
1386 if (value != nullptr)
1387 {
1388 *value += std::to_string(count);
1389 count++;
Ed Tanous81ce6092020-12-17 16:54:55 +00001390 sensorsAsyncResp->updateUri(sensorJson["Name"], *value);
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07001391 }
1392 }
1393 }
1394 }
1395}
1396
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +01001397/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001398 * @brief Finds the inventory item with the specified object path.
1399 * @param inventoryItems D-Bus inventory items associated with sensors.
1400 * @param invItemObjPath D-Bus object path of inventory item.
1401 * @return Inventory item within vector, or nullptr if no match found.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001402 */
Ed Tanous23a21a12020-07-25 04:45:05 +00001403inline InventoryItem* findInventoryItem(
Ed Tanousb5a76932020-09-29 16:16:58 -07001404 const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems,
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001405 const std::string& invItemObjPath)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001406{
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001407 for (InventoryItem& inventoryItem : *inventoryItems)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001408 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001409 if (inventoryItem.objectPath == invItemObjPath)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001410 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001411 return &inventoryItem;
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001412 }
1413 }
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001414 return nullptr;
1415}
1416
1417/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001418 * @brief Finds the inventory item associated with the specified sensor.
1419 * @param inventoryItems D-Bus inventory items associated with sensors.
1420 * @param sensorObjPath D-Bus object path of sensor.
1421 * @return Inventory item within vector, or nullptr if no match found.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001422 */
Ed Tanous23a21a12020-07-25 04:45:05 +00001423inline InventoryItem* findInventoryItemForSensor(
Ed Tanousb5a76932020-09-29 16:16:58 -07001424 const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems,
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001425 const std::string& sensorObjPath)
1426{
1427 for (InventoryItem& inventoryItem : *inventoryItems)
1428 {
1429 if (inventoryItem.sensors.count(sensorObjPath) > 0)
1430 {
1431 return &inventoryItem;
1432 }
1433 }
1434 return nullptr;
1435}
1436
1437/**
Anthony Wilsond5005492019-07-31 16:34:17 -05001438 * @brief Finds the inventory item associated with the specified led path.
1439 * @param inventoryItems D-Bus inventory items associated with sensors.
1440 * @param ledObjPath D-Bus object path of led.
1441 * @return Inventory item within vector, or nullptr if no match found.
1442 */
1443inline InventoryItem*
1444 findInventoryItemForLed(std::vector<InventoryItem>& inventoryItems,
1445 const std::string& ledObjPath)
1446{
1447 for (InventoryItem& inventoryItem : inventoryItems)
1448 {
1449 if (inventoryItem.ledObjectPath == ledObjPath)
1450 {
1451 return &inventoryItem;
1452 }
1453 }
1454 return nullptr;
1455}
1456
1457/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001458 * @brief Adds inventory item and associated sensor to specified vector.
1459 *
1460 * Adds a new InventoryItem to the vector if necessary. Searches for an
1461 * existing InventoryItem with the specified object path. If not found, one is
1462 * added to the vector.
1463 *
1464 * Next, the specified sensor is added to the set of sensors associated with the
1465 * InventoryItem.
1466 *
1467 * @param inventoryItems D-Bus inventory items associated with sensors.
1468 * @param invItemObjPath D-Bus object path of inventory item.
1469 * @param sensorObjPath D-Bus object path of sensor
1470 */
Ed Tanousb5a76932020-09-29 16:16:58 -07001471inline void addInventoryItem(
1472 const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems,
1473 const std::string& invItemObjPath, const std::string& sensorObjPath)
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001474{
1475 // Look for inventory item in vector
1476 InventoryItem* inventoryItem =
1477 findInventoryItem(inventoryItems, invItemObjPath);
1478
1479 // If inventory item doesn't exist in vector, add it
1480 if (inventoryItem == nullptr)
1481 {
1482 inventoryItems->emplace_back(invItemObjPath);
1483 inventoryItem = &(inventoryItems->back());
1484 }
1485
1486 // Add sensor to set of sensors associated with inventory item
1487 inventoryItem->sensors.emplace(sensorObjPath);
1488}
1489
1490/**
1491 * @brief Stores D-Bus data in the specified inventory item.
1492 *
1493 * Finds D-Bus data in the specified map of interfaces. Stores the data in the
1494 * specified InventoryItem.
1495 *
1496 * This data is later used to provide sensor property values in the JSON
1497 * response.
1498 *
1499 * @param inventoryItem Inventory item where data will be stored.
1500 * @param interfacesDict Map containing D-Bus interfaces and their properties
1501 * for the specified inventory item.
1502 */
Ed Tanous23a21a12020-07-25 04:45:05 +00001503inline void storeInventoryItemData(
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001504 InventoryItem& inventoryItem,
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001505 const boost::container::flat_map<
1506 std::string, boost::container::flat_map<std::string, SensorVariant>>&
1507 interfacesDict)
1508{
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001509 // Get properties from Inventory.Item interface
1510 auto interfaceIt =
1511 interfacesDict.find("xyz.openbmc_project.Inventory.Item");
1512 if (interfaceIt != interfacesDict.end())
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001513 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001514 auto propertyIt = interfaceIt->second.find("Present");
1515 if (propertyIt != interfaceIt->second.end())
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001516 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001517 const bool* value = std::get_if<bool>(&propertyIt->second);
1518 if (value != nullptr)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001519 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001520 inventoryItem.isPresent = *value;
1521 }
1522 }
1523 }
1524
1525 // Check if Inventory.Item.PowerSupply interface is present
1526 interfaceIt =
1527 interfacesDict.find("xyz.openbmc_project.Inventory.Item.PowerSupply");
1528 if (interfaceIt != interfacesDict.end())
1529 {
1530 inventoryItem.isPowerSupply = true;
1531 }
1532
1533 // Get properties from Inventory.Decorator.Asset interface
1534 interfaceIt =
1535 interfacesDict.find("xyz.openbmc_project.Inventory.Decorator.Asset");
1536 if (interfaceIt != interfacesDict.end())
1537 {
1538 auto propertyIt = interfaceIt->second.find("Manufacturer");
1539 if (propertyIt != interfaceIt->second.end())
1540 {
1541 const std::string* value =
1542 std::get_if<std::string>(&propertyIt->second);
1543 if (value != nullptr)
1544 {
1545 inventoryItem.manufacturer = *value;
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001546 }
1547 }
1548
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001549 propertyIt = interfaceIt->second.find("Model");
1550 if (propertyIt != interfaceIt->second.end())
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001551 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001552 const std::string* value =
1553 std::get_if<std::string>(&propertyIt->second);
1554 if (value != nullptr)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001555 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001556 inventoryItem.model = *value;
1557 }
1558 }
1559
1560 propertyIt = interfaceIt->second.find("PartNumber");
1561 if (propertyIt != interfaceIt->second.end())
1562 {
1563 const std::string* value =
1564 std::get_if<std::string>(&propertyIt->second);
1565 if (value != nullptr)
1566 {
1567 inventoryItem.partNumber = *value;
1568 }
1569 }
1570
1571 propertyIt = interfaceIt->second.find("SerialNumber");
1572 if (propertyIt != interfaceIt->second.end())
1573 {
1574 const std::string* value =
1575 std::get_if<std::string>(&propertyIt->second);
1576 if (value != nullptr)
1577 {
1578 inventoryItem.serialNumber = *value;
1579 }
1580 }
1581 }
1582
1583 // Get properties from State.Decorator.OperationalStatus interface
1584 interfaceIt = interfacesDict.find(
1585 "xyz.openbmc_project.State.Decorator.OperationalStatus");
1586 if (interfaceIt != interfacesDict.end())
1587 {
1588 auto propertyIt = interfaceIt->second.find("Functional");
1589 if (propertyIt != interfaceIt->second.end())
1590 {
1591 const bool* value = std::get_if<bool>(&propertyIt->second);
1592 if (value != nullptr)
1593 {
1594 inventoryItem.isFunctional = *value;
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001595 }
1596 }
1597 }
1598}
1599
1600/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001601 * @brief Gets D-Bus data for inventory items associated with sensors.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001602 *
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001603 * Uses the specified connections (services) to obtain D-Bus data for inventory
1604 * items associated with sensors. Stores the resulting data in the
1605 * inventoryItems vector.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001606 *
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001607 * This data is later used to provide sensor property values in the JSON
1608 * response.
1609 *
1610 * Finds the inventory item data asynchronously. Invokes callback when data has
1611 * been obtained.
1612 *
1613 * The callback must have the following signature:
1614 * @code
Anthony Wilsond5005492019-07-31 16:34:17 -05001615 * callback(void)
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001616 * @endcode
1617 *
1618 * This function is called recursively, obtaining data asynchronously from one
1619 * connection in each call. This ensures the callback is not invoked until the
1620 * last asynchronous function has completed.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001621 *
1622 * @param sensorsAsyncResp Pointer to object holding response data.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001623 * @param inventoryItems D-Bus inventory items associated with sensors.
1624 * @param invConnections Connections that provide data for the inventory items.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001625 * @param objectMgrPaths Mappings from connection name to DBus object path that
1626 * implements ObjectManager.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001627 * @param callback Callback to invoke when inventory data has been obtained.
1628 * @param invConnectionsIndex Current index in invConnections. Only specified
1629 * in recursive calls to this function.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001630 */
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001631template <typename Callback>
1632static void getInventoryItemsData(
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001633 std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001634 std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001635 std::shared_ptr<boost::container::flat_set<std::string>> invConnections,
1636 std::shared_ptr<boost::container::flat_map<std::string, std::string>>
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001637 objectMgrPaths,
Ed Tanous271584a2019-07-09 16:24:22 -07001638 Callback&& callback, size_t invConnectionsIndex = 0)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001639{
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001640 BMCWEB_LOG_DEBUG << "getInventoryItemsData enter";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001641
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001642 // If no more connections left, call callback
1643 if (invConnectionsIndex >= invConnections->size())
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001644 {
Anthony Wilsond5005492019-07-31 16:34:17 -05001645 callback();
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001646 BMCWEB_LOG_DEBUG << "getInventoryItemsData exit";
1647 return;
1648 }
1649
1650 // Get inventory item data from current connection
1651 auto it = invConnections->nth(invConnectionsIndex);
1652 if (it != invConnections->end())
1653 {
1654 const std::string& invConnection = *it;
1655
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001656 // Response handler for GetManagedObjects
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001657 auto respHandler = [sensorsAsyncResp, inventoryItems, invConnections,
1658 objectMgrPaths, callback{std::move(callback)},
1659 invConnectionsIndex](
1660 const boost::system::error_code ec,
1661 ManagedObjectsVectorType& resp) {
1662 BMCWEB_LOG_DEBUG << "getInventoryItemsData respHandler enter";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001663 if (ec)
1664 {
1665 BMCWEB_LOG_ERROR
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001666 << "getInventoryItemsData respHandler DBus error " << ec;
zhanghch058d1b46d2021-04-01 11:18:24 +08001667 messages::internalError(sensorsAsyncResp->asyncResp->res);
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001668 return;
1669 }
1670
1671 // Loop through returned object paths
1672 for (const auto& objDictEntry : resp)
1673 {
1674 const std::string& objPath =
1675 static_cast<const std::string&>(objDictEntry.first);
1676
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001677 // If this object path is one of the specified inventory items
1678 InventoryItem* inventoryItem =
1679 findInventoryItem(inventoryItems, objPath);
1680 if (inventoryItem != nullptr)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001681 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001682 // Store inventory data in InventoryItem
1683 storeInventoryItemData(*inventoryItem, objDictEntry.second);
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001684 }
1685 }
1686
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001687 // Recurse to get inventory item data from next connection
1688 getInventoryItemsData(sensorsAsyncResp, inventoryItems,
1689 invConnections, objectMgrPaths,
1690 std::move(callback), invConnectionsIndex + 1);
1691
1692 BMCWEB_LOG_DEBUG << "getInventoryItemsData respHandler exit";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001693 };
1694
1695 // Find DBus object path that implements ObjectManager for the current
1696 // connection. If no mapping found, default to "/".
1697 auto iter = objectMgrPaths->find(invConnection);
1698 const std::string& objectMgrPath =
1699 (iter != objectMgrPaths->end()) ? iter->second : "/";
1700 BMCWEB_LOG_DEBUG << "ObjectManager path for " << invConnection << " is "
1701 << objectMgrPath;
1702
1703 // Get all object paths and their interfaces for current connection
1704 crow::connections::systemBus->async_method_call(
1705 std::move(respHandler), invConnection, objectMgrPath,
1706 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
1707 }
1708
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001709 BMCWEB_LOG_DEBUG << "getInventoryItemsData exit";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001710}
1711
1712/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001713 * @brief Gets connections that provide D-Bus data for inventory items.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001714 *
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001715 * Gets the D-Bus connections (services) that provide data for the inventory
1716 * items that are associated with sensors.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001717 *
1718 * Finds the connections asynchronously. Invokes callback when information has
1719 * been obtained.
1720 *
1721 * The callback must have the following signature:
1722 * @code
1723 * callback(std::shared_ptr<boost::container::flat_set<std::string>>
1724 * invConnections)
1725 * @endcode
1726 *
1727 * @param sensorsAsyncResp Pointer to object holding response data.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001728 * @param inventoryItems D-Bus inventory items associated with sensors.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001729 * @param callback Callback to invoke when connections have been obtained.
1730 */
1731template <typename Callback>
1732static void getInventoryItemsConnections(
Ed Tanousb5a76932020-09-29 16:16:58 -07001733 const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
1734 const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems,
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001735 Callback&& callback)
1736{
1737 BMCWEB_LOG_DEBUG << "getInventoryItemsConnections enter";
1738
1739 const std::string path = "/xyz/openbmc_project/inventory";
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001740 const std::array<std::string, 4> interfaces = {
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001741 "xyz.openbmc_project.Inventory.Item",
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001742 "xyz.openbmc_project.Inventory.Item.PowerSupply",
1743 "xyz.openbmc_project.Inventory.Decorator.Asset",
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001744 "xyz.openbmc_project.State.Decorator.OperationalStatus"};
1745
1746 // Response handler for parsing output from GetSubTree
1747 auto respHandler = [callback{std::move(callback)}, sensorsAsyncResp,
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001748 inventoryItems](const boost::system::error_code ec,
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001749 const GetSubTreeType& subtree) {
1750 BMCWEB_LOG_DEBUG << "getInventoryItemsConnections respHandler enter";
1751 if (ec)
1752 {
zhanghch058d1b46d2021-04-01 11:18:24 +08001753 messages::internalError(sensorsAsyncResp->asyncResp->res);
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001754 BMCWEB_LOG_ERROR
1755 << "getInventoryItemsConnections respHandler DBus error " << ec;
1756 return;
1757 }
1758
1759 // Make unique list of connections for desired inventory items
1760 std::shared_ptr<boost::container::flat_set<std::string>>
1761 invConnections =
1762 std::make_shared<boost::container::flat_set<std::string>>();
1763 invConnections->reserve(8);
1764
1765 // Loop through objects from GetSubTree
1766 for (const std::pair<
1767 std::string,
1768 std::vector<std::pair<std::string, std::vector<std::string>>>>&
1769 object : subtree)
1770 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001771 // Check if object path is one of the specified inventory items
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001772 const std::string& objPath = object.first;
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001773 if (findInventoryItem(inventoryItems, objPath) != nullptr)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001774 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001775 // Store all connections to inventory item
1776 for (const std::pair<std::string, std::vector<std::string>>&
1777 objData : object.second)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001778 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001779 const std::string& invConnection = objData.first;
1780 invConnections->insert(invConnection);
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001781 }
1782 }
1783 }
Anthony Wilsond5005492019-07-31 16:34:17 -05001784
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001785 callback(invConnections);
1786 BMCWEB_LOG_DEBUG << "getInventoryItemsConnections respHandler exit";
1787 };
1788
1789 // Make call to ObjectMapper to find all inventory items
1790 crow::connections::systemBus->async_method_call(
1791 std::move(respHandler), "xyz.openbmc_project.ObjectMapper",
1792 "/xyz/openbmc_project/object_mapper",
1793 "xyz.openbmc_project.ObjectMapper", "GetSubTree", path, 0, interfaces);
1794 BMCWEB_LOG_DEBUG << "getInventoryItemsConnections exit";
1795}
1796
1797/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001798 * @brief Gets associations from sensors to inventory items.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001799 *
1800 * Looks for ObjectMapper associations from the specified sensors to related
Anthony Wilsond5005492019-07-31 16:34:17 -05001801 * inventory items. Then finds the associations from those inventory items to
1802 * their LEDs, if any.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001803 *
1804 * Finds the inventory items asynchronously. Invokes callback when information
1805 * has been obtained.
1806 *
1807 * The callback must have the following signature:
1808 * @code
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001809 * callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001810 * @endcode
1811 *
1812 * @param sensorsAsyncResp Pointer to object holding response data.
1813 * @param sensorNames All sensors within the current chassis.
1814 * @param objectMgrPaths Mappings from connection name to DBus object path that
1815 * implements ObjectManager.
1816 * @param callback Callback to invoke when inventory items have been obtained.
1817 */
1818template <typename Callback>
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001819static void getInventoryItemAssociations(
Ed Tanousb5a76932020-09-29 16:16:58 -07001820 const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
1821 const std::shared_ptr<boost::container::flat_set<std::string>>& sensorNames,
1822 const std::shared_ptr<boost::container::flat_map<std::string, std::string>>&
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001823 objectMgrPaths,
1824 Callback&& callback)
1825{
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001826 BMCWEB_LOG_DEBUG << "getInventoryItemAssociations enter";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001827
1828 // Response handler for GetManagedObjects
1829 auto respHandler = [callback{std::move(callback)}, sensorsAsyncResp,
1830 sensorNames](const boost::system::error_code ec,
1831 dbus::utility::ManagedObjectType& resp) {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001832 BMCWEB_LOG_DEBUG << "getInventoryItemAssociations respHandler enter";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001833 if (ec)
1834 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001835 BMCWEB_LOG_ERROR
1836 << "getInventoryItemAssociations respHandler DBus error " << ec;
zhanghch058d1b46d2021-04-01 11:18:24 +08001837 messages::internalError(sensorsAsyncResp->asyncResp->res);
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001838 return;
1839 }
1840
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001841 // Create vector to hold list of inventory items
1842 std::shared_ptr<std::vector<InventoryItem>> inventoryItems =
1843 std::make_shared<std::vector<InventoryItem>>();
1844
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001845 // Loop through returned object paths
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001846 std::string sensorAssocPath;
1847 sensorAssocPath.reserve(128); // avoid memory allocations
1848 for (const auto& objDictEntry : resp)
1849 {
1850 const std::string& objPath =
1851 static_cast<const std::string&>(objDictEntry.first);
1852 const boost::container::flat_map<
1853 std::string, boost::container::flat_map<
1854 std::string, dbus::utility::DbusVariantType>>&
1855 interfacesDict = objDictEntry.second;
1856
1857 // If path is inventory association for one of the specified sensors
1858 for (const std::string& sensorName : *sensorNames)
1859 {
1860 sensorAssocPath = sensorName;
1861 sensorAssocPath += "/inventory";
1862 if (objPath == sensorAssocPath)
1863 {
1864 // Get Association interface for object path
1865 auto assocIt =
1866 interfacesDict.find("xyz.openbmc_project.Association");
1867 if (assocIt != interfacesDict.end())
1868 {
1869 // Get inventory item from end point
1870 auto endpointsIt = assocIt->second.find("endpoints");
1871 if (endpointsIt != assocIt->second.end())
1872 {
1873 const std::vector<std::string>* endpoints =
1874 std::get_if<std::vector<std::string>>(
1875 &endpointsIt->second);
1876 if ((endpoints != nullptr) && !endpoints->empty())
1877 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001878 // Add inventory item to vector
1879 const std::string& invItemPath =
1880 endpoints->front();
1881 addInventoryItem(inventoryItems, invItemPath,
1882 sensorName);
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001883 }
1884 }
1885 }
1886 break;
1887 }
1888 }
1889 }
1890
Anthony Wilsond5005492019-07-31 16:34:17 -05001891 // Now loop through the returned object paths again, this time to
1892 // find the leds associated with the inventory items we just found
1893 std::string inventoryAssocPath;
1894 inventoryAssocPath.reserve(128); // avoid memory allocations
1895 for (const auto& objDictEntry : resp)
1896 {
1897 const std::string& objPath =
1898 static_cast<const std::string&>(objDictEntry.first);
1899 const boost::container::flat_map<
1900 std::string, boost::container::flat_map<
1901 std::string, dbus::utility::DbusVariantType>>&
1902 interfacesDict = objDictEntry.second;
1903
1904 for (InventoryItem& inventoryItem : *inventoryItems)
1905 {
1906 inventoryAssocPath = inventoryItem.objectPath;
1907 inventoryAssocPath += "/leds";
1908 if (objPath == inventoryAssocPath)
1909 {
1910 // Get Association interface for object path
1911 auto assocIt =
1912 interfacesDict.find("xyz.openbmc_project.Association");
1913 if (assocIt != interfacesDict.end())
1914 {
1915 // Get inventory item from end point
1916 auto endpointsIt = assocIt->second.find("endpoints");
1917 if (endpointsIt != assocIt->second.end())
1918 {
1919 const std::vector<std::string>* endpoints =
1920 std::get_if<std::vector<std::string>>(
1921 &endpointsIt->second);
1922 if ((endpoints != nullptr) && !endpoints->empty())
1923 {
1924 // Store LED path in inventory item
1925 const std::string& ledPath = endpoints->front();
1926 inventoryItem.ledObjectPath = ledPath;
1927 }
1928 }
1929 }
1930 break;
1931 }
1932 }
1933 }
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001934 callback(inventoryItems);
1935 BMCWEB_LOG_DEBUG << "getInventoryItemAssociations respHandler exit";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001936 };
1937
1938 // Find DBus object path that implements ObjectManager for ObjectMapper
1939 std::string connection = "xyz.openbmc_project.ObjectMapper";
1940 auto iter = objectMgrPaths->find(connection);
1941 const std::string& objectMgrPath =
1942 (iter != objectMgrPaths->end()) ? iter->second : "/";
1943 BMCWEB_LOG_DEBUG << "ObjectManager path for " << connection << " is "
1944 << objectMgrPath;
1945
1946 // Call GetManagedObjects on the ObjectMapper to get all associations
1947 crow::connections::systemBus->async_method_call(
1948 std::move(respHandler), connection, objectMgrPath,
1949 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
1950
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001951 BMCWEB_LOG_DEBUG << "getInventoryItemAssociations exit";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001952}
1953
1954/**
Anthony Wilsond5005492019-07-31 16:34:17 -05001955 * @brief Gets D-Bus data for inventory item leds associated with sensors.
1956 *
1957 * Uses the specified connections (services) to obtain D-Bus data for inventory
1958 * item leds associated with sensors. Stores the resulting data in the
1959 * inventoryItems vector.
1960 *
1961 * This data is later used to provide sensor property values in the JSON
1962 * response.
1963 *
1964 * Finds the inventory item led data asynchronously. Invokes callback when data
1965 * has been obtained.
1966 *
1967 * The callback must have the following signature:
1968 * @code
Gunnar Mills42cbe532019-08-15 15:26:54 -05001969 * callback()
Anthony Wilsond5005492019-07-31 16:34:17 -05001970 * @endcode
1971 *
1972 * This function is called recursively, obtaining data asynchronously from one
1973 * connection in each call. This ensures the callback is not invoked until the
1974 * last asynchronous function has completed.
1975 *
1976 * @param sensorsAsyncResp Pointer to object holding response data.
1977 * @param inventoryItems D-Bus inventory items associated with sensors.
1978 * @param ledConnections Connections that provide data for the inventory leds.
1979 * @param callback Callback to invoke when inventory data has been obtained.
1980 * @param ledConnectionsIndex Current index in ledConnections. Only specified
1981 * in recursive calls to this function.
1982 */
1983template <typename Callback>
1984void getInventoryLedData(
1985 std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
1986 std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
1987 std::shared_ptr<boost::container::flat_map<std::string, std::string>>
1988 ledConnections,
1989 Callback&& callback, size_t ledConnectionsIndex = 0)
1990{
1991 BMCWEB_LOG_DEBUG << "getInventoryLedData enter";
1992
1993 // If no more connections left, call callback
1994 if (ledConnectionsIndex >= ledConnections->size())
1995 {
Gunnar Mills42cbe532019-08-15 15:26:54 -05001996 callback();
Anthony Wilsond5005492019-07-31 16:34:17 -05001997 BMCWEB_LOG_DEBUG << "getInventoryLedData exit";
1998 return;
1999 }
2000
2001 // Get inventory item data from current connection
2002 auto it = ledConnections->nth(ledConnectionsIndex);
2003 if (it != ledConnections->end())
2004 {
2005 const std::string& ledPath = (*it).first;
2006 const std::string& ledConnection = (*it).second;
2007 // Response handler for Get State property
2008 auto respHandler =
2009 [sensorsAsyncResp, inventoryItems, ledConnections, ledPath,
2010 callback{std::move(callback)},
2011 ledConnectionsIndex](const boost::system::error_code ec,
2012 const std::variant<std::string>& ledState) {
2013 BMCWEB_LOG_DEBUG << "getInventoryLedData respHandler enter";
2014 if (ec)
2015 {
2016 BMCWEB_LOG_ERROR
2017 << "getInventoryLedData respHandler DBus error " << ec;
zhanghch058d1b46d2021-04-01 11:18:24 +08002018 messages::internalError(sensorsAsyncResp->asyncResp->res);
Anthony Wilsond5005492019-07-31 16:34:17 -05002019 return;
2020 }
2021
2022 const std::string* state = std::get_if<std::string>(&ledState);
2023 if (state != nullptr)
2024 {
2025 BMCWEB_LOG_DEBUG << "Led state: " << *state;
2026 // Find inventory item with this LED object path
2027 InventoryItem* inventoryItem =
2028 findInventoryItemForLed(*inventoryItems, ledPath);
2029 if (inventoryItem != nullptr)
2030 {
2031 // Store LED state in InventoryItem
2032 if (boost::ends_with(*state, "On"))
2033 {
2034 inventoryItem->ledState = LedState::ON;
2035 }
2036 else if (boost::ends_with(*state, "Blink"))
2037 {
2038 inventoryItem->ledState = LedState::BLINK;
2039 }
2040 else if (boost::ends_with(*state, "Off"))
2041 {
2042 inventoryItem->ledState = LedState::OFF;
2043 }
2044 else
2045 {
2046 inventoryItem->ledState = LedState::UNKNOWN;
2047 }
2048 }
2049 }
2050 else
2051 {
2052 BMCWEB_LOG_DEBUG << "Failed to find State data for LED: "
2053 << ledPath;
2054 }
2055
2056 // Recurse to get LED data from next connection
2057 getInventoryLedData(sensorsAsyncResp, inventoryItems,
2058 ledConnections, std::move(callback),
2059 ledConnectionsIndex + 1);
2060
2061 BMCWEB_LOG_DEBUG << "getInventoryLedData respHandler exit";
2062 };
2063
2064 // Get the State property for the current LED
2065 crow::connections::systemBus->async_method_call(
2066 std::move(respHandler), ledConnection, ledPath,
2067 "org.freedesktop.DBus.Properties", "Get",
2068 "xyz.openbmc_project.Led.Physical", "State");
2069 }
2070
2071 BMCWEB_LOG_DEBUG << "getInventoryLedData exit";
2072}
2073
2074/**
2075 * @brief Gets LED data for LEDs associated with given inventory items.
2076 *
2077 * Gets the D-Bus connections (services) that provide LED data for the LEDs
2078 * associated with the specified inventory items. Then gets the LED data from
2079 * each connection and stores it in the inventory item.
2080 *
2081 * This data is later used to provide sensor property values in the JSON
2082 * response.
2083 *
2084 * Finds the LED data asynchronously. Invokes callback when information has
2085 * been obtained.
2086 *
2087 * The callback must have the following signature:
2088 * @code
Gunnar Mills42cbe532019-08-15 15:26:54 -05002089 * callback()
Anthony Wilsond5005492019-07-31 16:34:17 -05002090 * @endcode
2091 *
2092 * @param sensorsAsyncResp Pointer to object holding response data.
2093 * @param inventoryItems D-Bus inventory items associated with sensors.
2094 * @param callback Callback to invoke when inventory items have been obtained.
2095 */
2096template <typename Callback>
2097void getInventoryLeds(
2098 std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
2099 std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
2100 Callback&& callback)
2101{
2102 BMCWEB_LOG_DEBUG << "getInventoryLeds enter";
2103
2104 const std::string path = "/xyz/openbmc_project";
2105 const std::array<std::string, 1> interfaces = {
2106 "xyz.openbmc_project.Led.Physical"};
2107
2108 // Response handler for parsing output from GetSubTree
2109 auto respHandler = [callback{std::move(callback)}, sensorsAsyncResp,
2110 inventoryItems](const boost::system::error_code ec,
2111 const GetSubTreeType& subtree) {
2112 BMCWEB_LOG_DEBUG << "getInventoryLeds respHandler enter";
2113 if (ec)
2114 {
zhanghch058d1b46d2021-04-01 11:18:24 +08002115 messages::internalError(sensorsAsyncResp->asyncResp->res);
Anthony Wilsond5005492019-07-31 16:34:17 -05002116 BMCWEB_LOG_ERROR << "getInventoryLeds respHandler DBus error "
2117 << ec;
2118 return;
2119 }
2120
2121 // Build map of LED object paths to connections
2122 std::shared_ptr<boost::container::flat_map<std::string, std::string>>
2123 ledConnections = std::make_shared<
2124 boost::container::flat_map<std::string, std::string>>();
2125
2126 // Loop through objects from GetSubTree
2127 for (const std::pair<
2128 std::string,
2129 std::vector<std::pair<std::string, std::vector<std::string>>>>&
2130 object : subtree)
2131 {
2132 // Check if object path is LED for one of the specified inventory
2133 // items
2134 const std::string& ledPath = object.first;
2135 if (findInventoryItemForLed(*inventoryItems, ledPath) != nullptr)
2136 {
2137 // Add mapping from ledPath to connection
2138 const std::string& connection = object.second.begin()->first;
2139 (*ledConnections)[ledPath] = connection;
2140 BMCWEB_LOG_DEBUG << "Added mapping " << ledPath << " -> "
2141 << connection;
2142 }
2143 }
2144
2145 getInventoryLedData(sensorsAsyncResp, inventoryItems, ledConnections,
2146 std::move(callback));
2147 BMCWEB_LOG_DEBUG << "getInventoryLeds respHandler exit";
2148 };
2149 // Make call to ObjectMapper to find all inventory items
2150 crow::connections::systemBus->async_method_call(
2151 std::move(respHandler), "xyz.openbmc_project.ObjectMapper",
2152 "/xyz/openbmc_project/object_mapper",
2153 "xyz.openbmc_project.ObjectMapper", "GetSubTree", path, 0, interfaces);
2154 BMCWEB_LOG_DEBUG << "getInventoryLeds exit";
2155}
2156
2157/**
Gunnar Mills42cbe532019-08-15 15:26:54 -05002158 * @brief Gets D-Bus data for Power Supply Attributes such as EfficiencyPercent
2159 *
2160 * Uses the specified connections (services) (currently assumes just one) to
2161 * obtain D-Bus data for Power Supply Attributes. Stores the resulting data in
2162 * the inventoryItems vector. Only stores data in Power Supply inventoryItems.
2163 *
2164 * This data is later used to provide sensor property values in the JSON
2165 * response.
2166 *
2167 * Finds the Power Supply Attributes data asynchronously. Invokes callback
2168 * when data has been obtained.
2169 *
2170 * The callback must have the following signature:
2171 * @code
2172 * callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems)
2173 * @endcode
2174 *
2175 * @param sensorsAsyncResp Pointer to object holding response data.
2176 * @param inventoryItems D-Bus inventory items associated with sensors.
2177 * @param psAttributesConnections Connections that provide data for the Power
2178 * Supply Attributes
2179 * @param callback Callback to invoke when data has been obtained.
2180 */
2181template <typename Callback>
2182void getPowerSupplyAttributesData(
Ed Tanousb5a76932020-09-29 16:16:58 -07002183 const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
Gunnar Mills42cbe532019-08-15 15:26:54 -05002184 std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
2185 const boost::container::flat_map<std::string, std::string>&
2186 psAttributesConnections,
2187 Callback&& callback)
2188{
2189 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributesData enter";
2190
2191 if (psAttributesConnections.empty())
2192 {
2193 BMCWEB_LOG_DEBUG << "Can't find PowerSupplyAttributes, no connections!";
2194 callback(inventoryItems);
2195 return;
2196 }
2197
2198 // Assuming just one connection (service) for now
2199 auto it = psAttributesConnections.nth(0);
2200
2201 const std::string& psAttributesPath = (*it).first;
2202 const std::string& psAttributesConnection = (*it).second;
2203
2204 // Response handler for Get DeratingFactor property
2205 auto respHandler = [sensorsAsyncResp, inventoryItems,
2206 callback{std::move(callback)}](
2207 const boost::system::error_code ec,
2208 const std::variant<uint32_t>& deratingFactor) {
2209 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributesData respHandler enter";
2210 if (ec)
2211 {
2212 BMCWEB_LOG_ERROR
2213 << "getPowerSupplyAttributesData respHandler DBus error " << ec;
zhanghch058d1b46d2021-04-01 11:18:24 +08002214 messages::internalError(sensorsAsyncResp->asyncResp->res);
Gunnar Mills42cbe532019-08-15 15:26:54 -05002215 return;
2216 }
2217
2218 const uint32_t* value = std::get_if<uint32_t>(&deratingFactor);
2219 if (value != nullptr)
2220 {
2221 BMCWEB_LOG_DEBUG << "PS EfficiencyPercent value: " << *value;
2222 // Store value in Power Supply Inventory Items
2223 for (InventoryItem& inventoryItem : *inventoryItems)
2224 {
2225 if (inventoryItem.isPowerSupply == true)
2226 {
2227 inventoryItem.powerSupplyEfficiencyPercent =
2228 static_cast<int>(*value);
2229 }
2230 }
2231 }
2232 else
2233 {
2234 BMCWEB_LOG_DEBUG
2235 << "Failed to find EfficiencyPercent value for PowerSupplies";
2236 }
2237
2238 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributesData respHandler exit";
2239 callback(inventoryItems);
2240 };
2241
2242 // Get the DeratingFactor property for the PowerSupplyAttributes
2243 // Currently only property on the interface/only one we care about
2244 crow::connections::systemBus->async_method_call(
2245 std::move(respHandler), psAttributesConnection, psAttributesPath,
2246 "org.freedesktop.DBus.Properties", "Get",
2247 "xyz.openbmc_project.Control.PowerSupplyAttributes", "DeratingFactor");
2248
2249 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributesData exit";
2250}
2251
2252/**
2253 * @brief Gets the Power Supply Attributes such as EfficiencyPercent
2254 *
2255 * Gets the D-Bus connection (service) that provides Power Supply Attributes
2256 * data. Then gets the Power Supply Attributes data from the connection
2257 * (currently just assumes 1 connection) and stores the data in the inventory
2258 * item.
2259 *
2260 * This data is later used to provide sensor property values in the JSON
2261 * response. DeratingFactor on D-Bus is mapped to EfficiencyPercent on Redfish.
2262 *
2263 * Finds the Power Supply Attributes data asynchronously. Invokes callback
2264 * when information has been obtained.
2265 *
2266 * The callback must have the following signature:
2267 * @code
2268 * callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems)
2269 * @endcode
2270 *
2271 * @param sensorsAsyncResp Pointer to object holding response data.
2272 * @param inventoryItems D-Bus inventory items associated with sensors.
2273 * @param callback Callback to invoke when data has been obtained.
2274 */
2275template <typename Callback>
2276void getPowerSupplyAttributes(
2277 std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
2278 std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
2279 Callback&& callback)
2280{
2281 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes enter";
2282
2283 // Only need the power supply attributes when the Power Schema
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02002284 if (sensorsAsyncResp->chassisSubNode != sensors::node::power)
Gunnar Mills42cbe532019-08-15 15:26:54 -05002285 {
2286 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes exit since not Power";
2287 callback(inventoryItems);
2288 return;
2289 }
2290
2291 const std::array<std::string, 1> interfaces = {
2292 "xyz.openbmc_project.Control.PowerSupplyAttributes"};
2293
2294 // Response handler for parsing output from GetSubTree
2295 auto respHandler = [callback{std::move(callback)}, sensorsAsyncResp,
2296 inventoryItems](const boost::system::error_code ec,
2297 const GetSubTreeType& subtree) {
2298 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes respHandler enter";
2299 if (ec)
2300 {
zhanghch058d1b46d2021-04-01 11:18:24 +08002301 messages::internalError(sensorsAsyncResp->asyncResp->res);
Gunnar Mills42cbe532019-08-15 15:26:54 -05002302 BMCWEB_LOG_ERROR
2303 << "getPowerSupplyAttributes respHandler DBus error " << ec;
2304 return;
2305 }
2306 if (subtree.size() == 0)
2307 {
2308 BMCWEB_LOG_DEBUG << "Can't find Power Supply Attributes!";
2309 callback(inventoryItems);
2310 return;
2311 }
2312
2313 // Currently we only support 1 power supply attribute, use this for
2314 // all the power supplies. Build map of object path to connection.
2315 // Assume just 1 connection and 1 path for now.
2316 boost::container::flat_map<std::string, std::string>
2317 psAttributesConnections;
2318
2319 if (subtree[0].first.empty() || subtree[0].second.empty())
2320 {
2321 BMCWEB_LOG_DEBUG << "Power Supply Attributes mapper error!";
2322 callback(inventoryItems);
2323 return;
2324 }
2325
2326 const std::string& psAttributesPath = subtree[0].first;
2327 const std::string& connection = subtree[0].second.begin()->first;
2328
2329 if (connection.empty())
2330 {
2331 BMCWEB_LOG_DEBUG << "Power Supply Attributes mapper error!";
2332 callback(inventoryItems);
2333 return;
2334 }
2335
2336 psAttributesConnections[psAttributesPath] = connection;
2337 BMCWEB_LOG_DEBUG << "Added mapping " << psAttributesPath << " -> "
2338 << connection;
2339
2340 getPowerSupplyAttributesData(sensorsAsyncResp, inventoryItems,
2341 psAttributesConnections,
2342 std::move(callback));
2343 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes respHandler exit";
2344 };
2345 // Make call to ObjectMapper to find the PowerSupplyAttributes service
2346 crow::connections::systemBus->async_method_call(
2347 std::move(respHandler), "xyz.openbmc_project.ObjectMapper",
2348 "/xyz/openbmc_project/object_mapper",
2349 "xyz.openbmc_project.ObjectMapper", "GetSubTree",
2350 "/xyz/openbmc_project", 0, interfaces);
2351 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes exit";
2352}
2353
2354/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002355 * @brief Gets inventory items associated with sensors.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002356 *
2357 * Finds the inventory items that are associated with the specified sensors.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002358 * Then gets D-Bus data for the inventory items, such as presence and VPD.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002359 *
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002360 * This data is later used to provide sensor property values in the JSON
2361 * response.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002362 *
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002363 * Finds the inventory items asynchronously. Invokes callback when the
2364 * inventory items have been obtained.
2365 *
2366 * The callback must have the following signature:
2367 * @code
2368 * callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems)
2369 * @endcode
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002370 *
2371 * @param sensorsAsyncResp Pointer to object holding response data.
2372 * @param sensorNames All sensors within the current chassis.
2373 * @param objectMgrPaths Mappings from connection name to DBus object path that
2374 * implements ObjectManager.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002375 * @param callback Callback to invoke when inventory items have been obtained.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002376 */
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002377template <typename Callback>
2378static void getInventoryItems(
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002379 std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
2380 const std::shared_ptr<boost::container::flat_set<std::string>> sensorNames,
2381 std::shared_ptr<boost::container::flat_map<std::string, std::string>>
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002382 objectMgrPaths,
2383 Callback&& callback)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002384{
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002385 BMCWEB_LOG_DEBUG << "getInventoryItems enter";
2386 auto getInventoryItemAssociationsCb =
2387 [sensorsAsyncResp, objectMgrPaths, callback{std::move(callback)}](
2388 std::shared_ptr<std::vector<InventoryItem>> inventoryItems) {
2389 BMCWEB_LOG_DEBUG << "getInventoryItemAssociationsCb enter";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002390 auto getInventoryItemsConnectionsCb =
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002391 [sensorsAsyncResp, inventoryItems, objectMgrPaths,
2392 callback{std::move(callback)}](
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002393 std::shared_ptr<boost::container::flat_set<std::string>>
2394 invConnections) {
2395 BMCWEB_LOG_DEBUG << "getInventoryItemsConnectionsCb enter";
Anthony Wilsond5005492019-07-31 16:34:17 -05002396 auto getInventoryItemsDataCb =
2397 [sensorsAsyncResp, inventoryItems,
2398 callback{std::move(callback)}]() {
2399 BMCWEB_LOG_DEBUG << "getInventoryItemsDataCb enter";
Gunnar Mills42cbe532019-08-15 15:26:54 -05002400
2401 auto getInventoryLedsCb = [sensorsAsyncResp,
2402 inventoryItems,
2403 callback{std::move(
2404 callback)}]() {
2405 BMCWEB_LOG_DEBUG << "getInventoryLedsCb enter";
2406 // Find Power Supply Attributes and get the data
2407 getPowerSupplyAttributes(sensorsAsyncResp,
2408 inventoryItems,
2409 std::move(callback));
2410 BMCWEB_LOG_DEBUG << "getInventoryLedsCb exit";
2411 };
2412
Anthony Wilsond5005492019-07-31 16:34:17 -05002413 // Find led connections and get the data
2414 getInventoryLeds(sensorsAsyncResp, inventoryItems,
Gunnar Mills42cbe532019-08-15 15:26:54 -05002415 std::move(getInventoryLedsCb));
Anthony Wilsond5005492019-07-31 16:34:17 -05002416 BMCWEB_LOG_DEBUG << "getInventoryItemsDataCb exit";
2417 };
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002418
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002419 // Get inventory item data from connections
2420 getInventoryItemsData(sensorsAsyncResp, inventoryItems,
2421 invConnections, objectMgrPaths,
Anthony Wilsond5005492019-07-31 16:34:17 -05002422 std::move(getInventoryItemsDataCb));
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002423 BMCWEB_LOG_DEBUG << "getInventoryItemsConnectionsCb exit";
2424 };
2425
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002426 // Get connections that provide inventory item data
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002427 getInventoryItemsConnections(
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002428 sensorsAsyncResp, inventoryItems,
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002429 std::move(getInventoryItemsConnectionsCb));
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002430 BMCWEB_LOG_DEBUG << "getInventoryItemAssociationsCb exit";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002431 };
2432
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002433 // Get associations from sensors to inventory items
2434 getInventoryItemAssociations(sensorsAsyncResp, sensorNames, objectMgrPaths,
2435 std::move(getInventoryItemAssociationsCb));
2436 BMCWEB_LOG_DEBUG << "getInventoryItems exit";
2437}
2438
2439/**
2440 * @brief Returns JSON PowerSupply object for the specified inventory item.
2441 *
2442 * Searches for a JSON PowerSupply object that matches the specified inventory
2443 * item. If one is not found, a new PowerSupply object is added to the JSON
2444 * array.
2445 *
2446 * Multiple sensors are often associated with one power supply inventory item.
2447 * As a result, multiple sensor values are stored in one JSON PowerSupply
2448 * object.
2449 *
2450 * @param powerSupplyArray JSON array containing Redfish PowerSupply objects.
2451 * @param inventoryItem Inventory item for the power supply.
2452 * @param chassisId Chassis that contains the power supply.
2453 * @return JSON PowerSupply object for the specified inventory item.
2454 */
Ed Tanous23a21a12020-07-25 04:45:05 +00002455inline nlohmann::json& getPowerSupply(nlohmann::json& powerSupplyArray,
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002456 const InventoryItem& inventoryItem,
2457 const std::string& chassisId)
2458{
2459 // Check if matching PowerSupply object already exists in JSON array
2460 for (nlohmann::json& powerSupply : powerSupplyArray)
2461 {
2462 if (powerSupply["MemberId"] == inventoryItem.name)
2463 {
2464 return powerSupply;
2465 }
2466 }
2467
2468 // Add new PowerSupply object to JSON array
2469 powerSupplyArray.push_back({});
2470 nlohmann::json& powerSupply = powerSupplyArray.back();
2471 powerSupply["@odata.id"] =
2472 "/redfish/v1/Chassis/" + chassisId + "/Power#/PowerSupplies/";
2473 powerSupply["MemberId"] = inventoryItem.name;
2474 powerSupply["Name"] = boost::replace_all_copy(inventoryItem.name, "_", " ");
2475 powerSupply["Manufacturer"] = inventoryItem.manufacturer;
2476 powerSupply["Model"] = inventoryItem.model;
2477 powerSupply["PartNumber"] = inventoryItem.partNumber;
2478 powerSupply["SerialNumber"] = inventoryItem.serialNumber;
Anthony Wilsond5005492019-07-31 16:34:17 -05002479 setLedState(powerSupply, &inventoryItem);
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002480
Gunnar Mills42cbe532019-08-15 15:26:54 -05002481 if (inventoryItem.powerSupplyEfficiencyPercent >= 0)
2482 {
2483 powerSupply["EfficiencyPercent"] =
2484 inventoryItem.powerSupplyEfficiencyPercent;
2485 }
2486
2487 powerSupply["Status"]["State"] = getState(&inventoryItem);
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002488 const char* health = inventoryItem.isFunctional ? "OK" : "Critical";
2489 powerSupply["Status"]["Health"] = health;
2490
2491 return powerSupply;
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002492}
2493
2494/**
Shawn McCarneyde629b62019-03-08 10:42:51 -06002495 * @brief Gets the values of the specified sensors.
2496 *
2497 * Stores the results as JSON in the SensorsAsyncResp.
2498 *
2499 * Gets the sensor values asynchronously. Stores the results later when the
2500 * information has been obtained.
2501 *
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002502 * The sensorNames set contains all requested sensors for the current chassis.
Shawn McCarneyde629b62019-03-08 10:42:51 -06002503 *
2504 * To minimize the number of DBus calls, the DBus method
2505 * org.freedesktop.DBus.ObjectManager.GetManagedObjects() is used to get the
2506 * values of all sensors provided by a connection (service).
2507 *
2508 * The connections set contains all the connections that provide sensor values.
2509 *
2510 * The objectMgrPaths map contains mappings from a connection name to the
2511 * corresponding DBus object path that implements ObjectManager.
2512 *
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002513 * The InventoryItem vector contains D-Bus inventory items associated with the
2514 * sensors. Inventory item data is needed for some Redfish sensor properties.
2515 *
Shawn McCarneyde629b62019-03-08 10:42:51 -06002516 * @param SensorsAsyncResp Pointer to object holding response data.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002517 * @param sensorNames All requested sensors within the current chassis.
Shawn McCarneyde629b62019-03-08 10:42:51 -06002518 * @param connections Connections that provide sensor values.
2519 * @param objectMgrPaths Mappings from connection name to DBus object path that
2520 * implements ObjectManager.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002521 * @param inventoryItems Inventory items associated with the sensors.
Shawn McCarneyde629b62019-03-08 10:42:51 -06002522 */
Ed Tanous23a21a12020-07-25 04:45:05 +00002523inline void getSensorData(
Ed Tanous81ce6092020-12-17 16:54:55 +00002524 const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
Ed Tanousb5a76932020-09-29 16:16:58 -07002525 const std::shared_ptr<boost::container::flat_set<std::string>>& sensorNames,
Shawn McCarneyde629b62019-03-08 10:42:51 -06002526 const boost::container::flat_set<std::string>& connections,
Ed Tanousb5a76932020-09-29 16:16:58 -07002527 const std::shared_ptr<boost::container::flat_map<std::string, std::string>>&
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002528 objectMgrPaths,
Ed Tanousb5a76932020-09-29 16:16:58 -07002529 const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems)
Shawn McCarneyde629b62019-03-08 10:42:51 -06002530{
2531 BMCWEB_LOG_DEBUG << "getSensorData enter";
2532 // Get managed objects from all services exposing sensors
2533 for (const std::string& connection : connections)
2534 {
2535 // Response handler to process managed objects
Ed Tanous81ce6092020-12-17 16:54:55 +00002536 auto getManagedObjectsCb = [sensorsAsyncResp, sensorNames,
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002537 inventoryItems](
Shawn McCarneyde629b62019-03-08 10:42:51 -06002538 const boost::system::error_code ec,
2539 ManagedObjectsVectorType& resp) {
2540 BMCWEB_LOG_DEBUG << "getManagedObjectsCb enter";
2541 if (ec)
2542 {
2543 BMCWEB_LOG_ERROR << "getManagedObjectsCb DBUS error: " << ec;
zhanghch058d1b46d2021-04-01 11:18:24 +08002544 messages::internalError(sensorsAsyncResp->asyncResp->res);
Shawn McCarneyde629b62019-03-08 10:42:51 -06002545 return;
2546 }
2547 // Go through all objects and update response with sensor data
2548 for (const auto& objDictEntry : resp)
2549 {
2550 const std::string& objPath =
2551 static_cast<const std::string&>(objDictEntry.first);
2552 BMCWEB_LOG_DEBUG << "getManagedObjectsCb parsing object "
2553 << objPath;
2554
Shawn McCarneyde629b62019-03-08 10:42:51 -06002555 std::vector<std::string> split;
2556 // Reserve space for
2557 // /xyz/openbmc_project/sensors/<name>/<subname>
2558 split.reserve(6);
2559 boost::algorithm::split(split, objPath, boost::is_any_of("/"));
2560 if (split.size() < 6)
2561 {
2562 BMCWEB_LOG_ERROR << "Got path that isn't long enough "
2563 << objPath;
2564 continue;
2565 }
2566 // These indexes aren't intuitive, as boost::split puts an empty
2567 // string at the beginning
2568 const std::string& sensorType = split[4];
2569 const std::string& sensorName = split[5];
2570 BMCWEB_LOG_DEBUG << "sensorName " << sensorName
2571 << " sensorType " << sensorType;
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002572 if (sensorNames->find(objPath) == sensorNames->end())
Shawn McCarneyde629b62019-03-08 10:42:51 -06002573 {
2574 BMCWEB_LOG_ERROR << sensorName << " not in sensor list ";
2575 continue;
2576 }
2577
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002578 // Find inventory item (if any) associated with sensor
2579 InventoryItem* inventoryItem =
2580 findInventoryItemForSensor(inventoryItems, objPath);
2581
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002582 const std::string& sensorSchema =
Ed Tanous81ce6092020-12-17 16:54:55 +00002583 sensorsAsyncResp->chassisSubNode;
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002584
2585 nlohmann::json* sensorJson = nullptr;
2586
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02002587 if (sensorSchema == sensors::node::sensors)
Shawn McCarneyde629b62019-03-08 10:42:51 -06002588 {
zhanghch058d1b46d2021-04-01 11:18:24 +08002589 sensorsAsyncResp->asyncResp->res.jsonValue["@odata.id"] =
Ed Tanous81ce6092020-12-17 16:54:55 +00002590 "/redfish/v1/Chassis/" + sensorsAsyncResp->chassisId +
2591 "/" + sensorsAsyncResp->chassisSubNode + "/" +
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002592 sensorName;
zhanghch058d1b46d2021-04-01 11:18:24 +08002593 sensorJson = &(sensorsAsyncResp->asyncResp->res.jsonValue);
Shawn McCarneyde629b62019-03-08 10:42:51 -06002594 }
2595 else
2596 {
Ed Tanous271584a2019-07-09 16:24:22 -07002597 std::string fieldName;
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002598 if (sensorType == "temperature")
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002599 {
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002600 fieldName = "Temperatures";
2601 }
2602 else if (sensorType == "fan" || sensorType == "fan_tach" ||
2603 sensorType == "fan_pwm")
2604 {
2605 fieldName = "Fans";
2606 }
2607 else if (sensorType == "voltage")
2608 {
2609 fieldName = "Voltages";
2610 }
2611 else if (sensorType == "power")
2612 {
2613 if (!sensorName.compare("total_power"))
2614 {
2615 fieldName = "PowerControl";
2616 }
2617 else if ((inventoryItem != nullptr) &&
2618 (inventoryItem->isPowerSupply))
2619 {
2620 fieldName = "PowerSupplies";
2621 }
2622 else
2623 {
2624 // Other power sensors are in SensorCollection
2625 continue;
2626 }
2627 }
2628 else
2629 {
2630 BMCWEB_LOG_ERROR << "Unsure how to handle sensorType "
2631 << sensorType;
2632 continue;
2633 }
2634
2635 nlohmann::json& tempArray =
zhanghch058d1b46d2021-04-01 11:18:24 +08002636 sensorsAsyncResp->asyncResp->res.jsonValue[fieldName];
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002637 if (fieldName == "PowerControl")
2638 {
2639 if (tempArray.empty())
2640 {
2641 // Put multiple "sensors" into a single
2642 // PowerControl. Follows MemberId naming and
2643 // naming in power.hpp.
2644 tempArray.push_back(
2645 {{"@odata.id",
2646 "/redfish/v1/Chassis/" +
Ed Tanous81ce6092020-12-17 16:54:55 +00002647 sensorsAsyncResp->chassisId + "/" +
2648 sensorsAsyncResp->chassisSubNode + "#/" +
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002649 fieldName + "/0"}});
2650 }
2651 sensorJson = &(tempArray.back());
2652 }
2653 else if (fieldName == "PowerSupplies")
2654 {
2655 if (inventoryItem != nullptr)
2656 {
2657 sensorJson =
2658 &(getPowerSupply(tempArray, *inventoryItem,
Ed Tanous81ce6092020-12-17 16:54:55 +00002659 sensorsAsyncResp->chassisId));
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002660 }
2661 }
2662 else
2663 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002664 tempArray.push_back(
2665 {{"@odata.id",
2666 "/redfish/v1/Chassis/" +
Ed Tanous81ce6092020-12-17 16:54:55 +00002667 sensorsAsyncResp->chassisId + "/" +
2668 sensorsAsyncResp->chassisSubNode + "#/" +
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002669 fieldName + "/"}});
2670 sensorJson = &(tempArray.back());
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002671 }
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002672 }
Shawn McCarneyde629b62019-03-08 10:42:51 -06002673
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002674 if (sensorJson != nullptr)
2675 {
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02002676 objectInterfacesToJson(
Ed Tanous81ce6092020-12-17 16:54:55 +00002677 sensorName, sensorType, sensorsAsyncResp,
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02002678 objDictEntry.second, *sensorJson, inventoryItem);
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002679 }
Shawn McCarneyde629b62019-03-08 10:42:51 -06002680 }
Ed Tanous81ce6092020-12-17 16:54:55 +00002681 if (sensorsAsyncResp.use_count() == 1)
James Feist8bd25cc2019-03-15 15:14:00 -07002682 {
Ed Tanous81ce6092020-12-17 16:54:55 +00002683 sortJSONResponse(sensorsAsyncResp);
2684 if (sensorsAsyncResp->chassisSubNode == sensors::node::thermal)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002685 {
Ed Tanous81ce6092020-12-17 16:54:55 +00002686 populateFanRedundancy(sensorsAsyncResp);
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002687 }
James Feist8bd25cc2019-03-15 15:14:00 -07002688 }
Shawn McCarneyde629b62019-03-08 10:42:51 -06002689 BMCWEB_LOG_DEBUG << "getManagedObjectsCb exit";
2690 };
2691
2692 // Find DBus object path that implements ObjectManager for the current
2693 // connection. If no mapping found, default to "/".
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002694 auto iter = objectMgrPaths->find(connection);
Shawn McCarneyde629b62019-03-08 10:42:51 -06002695 const std::string& objectMgrPath =
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002696 (iter != objectMgrPaths->end()) ? iter->second : "/";
Shawn McCarneyde629b62019-03-08 10:42:51 -06002697 BMCWEB_LOG_DEBUG << "ObjectManager path for " << connection << " is "
2698 << objectMgrPath;
2699
2700 crow::connections::systemBus->async_method_call(
2701 getManagedObjectsCb, connection, objectMgrPath,
2702 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
Ed Tanous23a21a12020-07-25 04:45:05 +00002703 }
Shawn McCarneyde629b62019-03-08 10:42:51 -06002704 BMCWEB_LOG_DEBUG << "getSensorData exit";
2705}
2706
Ed Tanous23a21a12020-07-25 04:45:05 +00002707inline void processSensorList(
Ed Tanous81ce6092020-12-17 16:54:55 +00002708 const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
Ed Tanousb5a76932020-09-29 16:16:58 -07002709 const std::shared_ptr<boost::container::flat_set<std::string>>& sensorNames)
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002710{
2711 auto getConnectionCb =
Ed Tanous81ce6092020-12-17 16:54:55 +00002712 [sensorsAsyncResp, sensorNames](
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002713 const boost::container::flat_set<std::string>& connections) {
2714 BMCWEB_LOG_DEBUG << "getConnectionCb enter";
2715 auto getObjectManagerPathsCb =
Ed Tanous81ce6092020-12-17 16:54:55 +00002716 [sensorsAsyncResp, sensorNames,
Ed Tanousb5a76932020-09-29 16:16:58 -07002717 connections](const std::shared_ptr<boost::container::flat_map<
2718 std::string, std::string>>& objectMgrPaths) {
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002719 BMCWEB_LOG_DEBUG << "getObjectManagerPathsCb enter";
2720 auto getInventoryItemsCb =
Ed Tanous81ce6092020-12-17 16:54:55 +00002721 [sensorsAsyncResp, sensorNames, connections,
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002722 objectMgrPaths](
Ed Tanousf23b7292020-10-15 09:41:17 -07002723 const std::shared_ptr<std::vector<InventoryItem>>&
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002724 inventoryItems) {
2725 BMCWEB_LOG_DEBUG << "getInventoryItemsCb enter";
2726 // Get sensor data and store results in JSON
Ed Tanous81ce6092020-12-17 16:54:55 +00002727 getSensorData(sensorsAsyncResp, sensorNames,
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002728 connections, objectMgrPaths,
Ed Tanousf23b7292020-10-15 09:41:17 -07002729 inventoryItems);
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002730 BMCWEB_LOG_DEBUG << "getInventoryItemsCb exit";
2731 };
2732
2733 // Get inventory items associated with sensors
Ed Tanous81ce6092020-12-17 16:54:55 +00002734 getInventoryItems(sensorsAsyncResp, sensorNames,
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002735 objectMgrPaths,
2736 std::move(getInventoryItemsCb));
2737
2738 BMCWEB_LOG_DEBUG << "getObjectManagerPathsCb exit";
2739 };
2740
2741 // Get mapping from connection names to the DBus object
2742 // paths that implement the ObjectManager interface
Ed Tanous81ce6092020-12-17 16:54:55 +00002743 getObjectManagerPaths(sensorsAsyncResp,
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002744 std::move(getObjectManagerPathsCb));
2745 BMCWEB_LOG_DEBUG << "getConnectionCb exit";
2746 };
2747
2748 // Get set of connections that provide sensor values
Ed Tanous81ce6092020-12-17 16:54:55 +00002749 getConnections(sensorsAsyncResp, sensorNames, std::move(getConnectionCb));
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002750}
2751
Shawn McCarneyde629b62019-03-08 10:42:51 -06002752/**
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +01002753 * @brief Entry point for retrieving sensors data related to requested
2754 * chassis.
Kowalski, Kamil588c3f02018-04-03 14:55:27 +02002755 * @param SensorsAsyncResp Pointer to object holding response data
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +01002756 */
Ed Tanousb5a76932020-09-29 16:16:58 -07002757inline void
Ed Tanous81ce6092020-12-17 16:54:55 +00002758 getChassisData(const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp)
Ed Tanous1abe55e2018-09-05 08:30:59 -07002759{
2760 BMCWEB_LOG_DEBUG << "getChassisData enter";
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002761 auto getChassisCb =
Ed Tanous81ce6092020-12-17 16:54:55 +00002762 [sensorsAsyncResp](
Ed Tanousf23b7292020-10-15 09:41:17 -07002763 const std::shared_ptr<boost::container::flat_set<std::string>>&
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002764 sensorNames) {
2765 BMCWEB_LOG_DEBUG << "getChassisCb enter";
Ed Tanous81ce6092020-12-17 16:54:55 +00002766 processSensorList(sensorsAsyncResp, sensorNames);
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002767 BMCWEB_LOG_DEBUG << "getChassisCb exit";
2768 };
zhanghch058d1b46d2021-04-01 11:18:24 +08002769 sensorsAsyncResp->asyncResp->res.jsonValue["Redundancy"] =
2770 nlohmann::json::array();
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +01002771
Shawn McCarney26f03892019-05-03 13:20:24 -05002772 // Get set of sensors in chassis
Ed Tanous81ce6092020-12-17 16:54:55 +00002773 getChassis(sensorsAsyncResp, std::move(getChassisCb));
Ed Tanous1abe55e2018-09-05 08:30:59 -07002774 BMCWEB_LOG_DEBUG << "getChassisData exit";
Ed Tanous271584a2019-07-09 16:24:22 -07002775}
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +01002776
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302777/**
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002778 * @brief Find the requested sensorName in the list of all sensors supplied by
2779 * the chassis node
2780 *
2781 * @param sensorName The sensor name supplied in the PATCH request
2782 * @param sensorsList The list of sensors managed by the chassis node
2783 * @param sensorsModified The list of sensors that were found as a result of
2784 * repeated calls to this function
2785 */
Ed Tanous23a21a12020-07-25 04:45:05 +00002786inline bool findSensorNameUsingSensorPath(
Richard Marian Thomaiyar0a86feb2019-05-27 23:16:40 +05302787 std::string_view sensorName,
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002788 boost::container::flat_set<std::string>& sensorsList,
2789 boost::container::flat_set<std::string>& sensorsModified)
2790{
George Liu28aa8de2021-02-01 15:13:30 +08002791 for (auto& chassisSensor : sensorsList)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002792 {
George Liu28aa8de2021-02-01 15:13:30 +08002793 sdbusplus::message::object_path path(chassisSensor);
Ed Tanousb00dcc22021-02-23 12:52:50 -08002794 std::string thisSensorName = path.filename();
George Liu28aa8de2021-02-01 15:13:30 +08002795 if (thisSensorName.empty())
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002796 {
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002797 continue;
2798 }
2799 if (thisSensorName == sensorName)
2800 {
2801 sensorsModified.emplace(chassisSensor);
2802 return true;
2803 }
2804 }
2805 return false;
2806}
2807
2808/**
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302809 * @brief Entry point for overriding sensor values of given sensor
2810 *
zhanghch058d1b46d2021-04-01 11:18:24 +08002811 * @param sensorAsyncResp response object
Carol Wang4bb3dc32019-10-17 18:15:02 +08002812 * @param allCollections Collections extract from sensors' request patch info
jayaprakash Mutyala91e130a2020-03-04 22:26:38 +00002813 * @param chassisSubNode Chassis Node for which the query has to happen
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302814 */
Ed Tanous23a21a12020-07-25 04:45:05 +00002815inline void setSensorsOverride(
Ed Tanousb5a76932020-09-29 16:16:58 -07002816 const std::shared_ptr<SensorsAsyncResp>& sensorAsyncResp,
Carol Wang4bb3dc32019-10-17 18:15:02 +08002817 std::unordered_map<std::string, std::vector<nlohmann::json>>&
jayaprakash Mutyala397fd612020-02-06 23:33:34 +00002818 allCollections)
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302819{
jayaprakash Mutyala70d1d0a2020-01-21 23:41:36 +00002820 BMCWEB_LOG_INFO << "setSensorsOverride for subNode"
Carol Wang4bb3dc32019-10-17 18:15:02 +08002821 << sensorAsyncResp->chassisSubNode << "\n";
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302822
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302823 const char* propertyValueName;
2824 std::unordered_map<std::string, std::pair<double, std::string>> overrideMap;
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302825 std::string memberId;
2826 double value;
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302827 for (auto& collectionItems : allCollections)
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302828 {
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302829 if (collectionItems.first == "Temperatures")
2830 {
2831 propertyValueName = "ReadingCelsius";
2832 }
2833 else if (collectionItems.first == "Fans")
2834 {
2835 propertyValueName = "Reading";
2836 }
2837 else
2838 {
2839 propertyValueName = "ReadingVolts";
2840 }
2841 for (auto& item : collectionItems.second)
2842 {
zhanghch058d1b46d2021-04-01 11:18:24 +08002843 if (!json_util::readJson(item, sensorAsyncResp->asyncResp->res,
2844 "MemberId", memberId, propertyValueName,
2845 value))
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302846 {
2847 return;
2848 }
2849 overrideMap.emplace(memberId,
2850 std::make_pair(value, collectionItems.first));
2851 }
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302852 }
Carol Wang4bb3dc32019-10-17 18:15:02 +08002853
Ed Tanousb5a76932020-09-29 16:16:58 -07002854 auto getChassisSensorListCb = [sensorAsyncResp, overrideMap](
2855 const std::shared_ptr<
2856 boost::container::flat_set<
2857 std::string>>& sensorsList) {
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002858 // Match sensor names in the PATCH request to those managed by the
2859 // chassis node
2860 const std::shared_ptr<boost::container::flat_set<std::string>>
2861 sensorNames =
2862 std::make_shared<boost::container::flat_set<std::string>>();
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302863 for (const auto& item : overrideMap)
2864 {
2865 const auto& sensor = item.first;
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002866 if (!findSensorNameUsingSensorPath(sensor, *sensorsList,
2867 *sensorNames))
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302868 {
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302869 BMCWEB_LOG_INFO << "Unable to find memberId " << item.first;
zhanghch058d1b46d2021-04-01 11:18:24 +08002870 messages::resourceNotFound(sensorAsyncResp->asyncResp->res,
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302871 item.second.second, item.first);
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302872 return;
2873 }
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302874 }
2875 // Get the connection to which the memberId belongs
2876 auto getObjectsWithConnectionCb =
2877 [sensorAsyncResp, overrideMap](
Ed Tanouscb13a392020-07-25 19:02:03 +00002878 const boost::container::flat_set<std::string>& /*connections*/,
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302879 const std::set<std::pair<std::string, std::string>>&
2880 objectsWithConnection) {
2881 if (objectsWithConnection.size() != overrideMap.size())
2882 {
2883 BMCWEB_LOG_INFO
2884 << "Unable to find all objects with proper connection "
2885 << objectsWithConnection.size() << " requested "
2886 << overrideMap.size() << "\n";
2887 messages::resourceNotFound(
zhanghch058d1b46d2021-04-01 11:18:24 +08002888 sensorAsyncResp->asyncResp->res,
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02002889 sensorAsyncResp->chassisSubNode ==
2890 sensors::node::thermal
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302891 ? "Temperatures"
2892 : "Voltages",
2893 "Count");
2894 return;
2895 }
2896 for (const auto& item : objectsWithConnection)
2897 {
George Liu28aa8de2021-02-01 15:13:30 +08002898 sdbusplus::message::object_path path(item.first);
2899 std::string sensorName = path.filename();
2900 if (sensorName.empty())
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302901 {
zhanghch058d1b46d2021-04-01 11:18:24 +08002902 messages::internalError(
2903 sensorAsyncResp->asyncResp->res);
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302904 return;
2905 }
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302906
2907 const auto& iterator = overrideMap.find(sensorName);
2908 if (iterator == overrideMap.end())
2909 {
2910 BMCWEB_LOG_INFO << "Unable to find sensor object"
2911 << item.first << "\n";
zhanghch058d1b46d2021-04-01 11:18:24 +08002912 messages::internalError(
2913 sensorAsyncResp->asyncResp->res);
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302914 return;
2915 }
2916 crow::connections::systemBus->async_method_call(
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302917 [sensorAsyncResp](const boost::system::error_code ec) {
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302918 if (ec)
2919 {
2920 BMCWEB_LOG_DEBUG
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302921 << "setOverrideValueStatus DBUS error: "
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302922 << ec;
zhanghch058d1b46d2021-04-01 11:18:24 +08002923 messages::internalError(
2924 sensorAsyncResp->asyncResp->res);
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302925 return;
2926 }
2927 },
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302928 item.second, item.first,
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302929 "org.freedesktop.DBus.Properties", "Set",
2930 "xyz.openbmc_project.Sensor.Value", "Value",
Patrick Williams19bd78d2020-05-13 17:38:24 -05002931 std::variant<double>(iterator->second.first));
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302932 }
2933 };
2934 // Get object with connection for the given sensor name
2935 getObjectsWithConnection(sensorAsyncResp, sensorNames,
2936 std::move(getObjectsWithConnectionCb));
2937 };
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302938 // get full sensor list for the given chassisId and cross verify the sensor.
2939 getChassis(sensorAsyncResp, std::move(getChassisSensorListCb));
2940}
2941
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02002942/**
2943 * @brief Retrieves mapping of Redfish URIs to sensor value property to D-Bus
2944 * path of the sensor.
2945 *
2946 * Function builds valid Redfish response for sensor query of given chassis and
2947 * node. It then builds metadata about Redfish<->D-Bus correlations and provides
2948 * it to caller in a callback.
2949 *
2950 * @param chassis Chassis for which retrieval should be performed
2951 * @param node Node (group) of sensors. See sensors::node for supported values
2952 * @param mapComplete Callback to be called with retrieval result
2953 */
Ed Tanous23a21a12020-07-25 04:45:05 +00002954inline void retrieveUriToDbusMap(const std::string& chassis,
2955 const std::string& node,
2956 SensorsAsyncResp::DataCompleteCb&& mapComplete)
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02002957{
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +00002958 auto pathIt = sensors::dbus::paths.find(node);
2959 if (pathIt == sensors::dbus::paths.end())
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02002960 {
2961 BMCWEB_LOG_ERROR << "Wrong node provided : " << node;
2962 mapComplete(boost::beast::http::status::bad_request, {});
2963 return;
2964 }
Krzysztof Grobelnyd51e0722021-04-16 13:15:21 +00002965
2966 auto res = std::make_shared<crow::Response>();
2967 auto asyncResp = std::make_shared<bmcweb::AsyncResp>(*res);
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02002968 auto callback =
Krzysztof Grobelnyd51e0722021-04-16 13:15:21 +00002969 [res, asyncResp, mapCompleteCb{std::move(mapComplete)}](
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02002970 const boost::beast::http::status status,
2971 const boost::container::flat_map<std::string, std::string>&
2972 uriToDbus) { mapCompleteCb(status, uriToDbus); };
2973
2974 auto resp = std::make_shared<SensorsAsyncResp>(
Krzysztof Grobelnyd51e0722021-04-16 13:15:21 +00002975 asyncResp, chassis, pathIt->second, node, std::move(callback));
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02002976 getChassisData(resp);
2977}
2978
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002979inline void requestRoutesSensorCollection(App& app)
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002980{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002981 BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/Sensors/")
Ed Tanoused398212021-06-09 17:05:54 -07002982 .privileges(redfish::privileges::getSensorCollection)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002983 .methods(
2984 boost::beast::http::verb::get)([](const crow::Request&,
2985 const std::shared_ptr<
2986 bmcweb::AsyncResp>& aResp,
2987 const std::string& chassisId) {
2988 BMCWEB_LOG_DEBUG << "SensorCollection doGet enter";
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002989
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002990 std::shared_ptr<SensorsAsyncResp> asyncResp =
2991 std::make_shared<SensorsAsyncResp>(
2992 aResp, chassisId,
2993 sensors::dbus::paths.at(sensors::node::sensors),
2994 sensors::node::sensors);
zhanghch058d1b46d2021-04-01 11:18:24 +08002995
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002996 auto getChassisCb =
2997 [asyncResp](
2998 const std::shared_ptr<
2999 boost::container::flat_set<std::string>>& sensorNames) {
3000 BMCWEB_LOG_DEBUG << "getChassisCb enter";
Anthony Wilson95a3eca2019-06-11 10:44:47 -05003001
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003002 nlohmann::json& entriesArray =
3003 asyncResp->asyncResp->res.jsonValue["Members"];
3004 for (auto& sensor : *sensorNames)
Anthony Wilson95a3eca2019-06-11 10:44:47 -05003005 {
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003006 BMCWEB_LOG_DEBUG << "Adding sensor: " << sensor;
Anthony Wilson95a3eca2019-06-11 10:44:47 -05003007
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003008 sdbusplus::message::object_path path(sensor);
3009 std::string sensorName = path.filename();
3010 if (sensorName.empty())
Anthony Wilson95a3eca2019-06-11 10:44:47 -05003011 {
3012 BMCWEB_LOG_ERROR << "Invalid sensor path: "
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003013 << sensor;
3014 messages::internalError(asyncResp->asyncResp->res);
3015 return;
Anthony Wilson95a3eca2019-06-11 10:44:47 -05003016 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003017 entriesArray.push_back(
3018 {{"@odata.id", "/redfish/v1/Chassis/" +
3019 asyncResp->chassisId + "/" +
3020 asyncResp->chassisSubNode + "/" +
3021 sensorName}});
3022 }
Anthony Wilson95a3eca2019-06-11 10:44:47 -05003023
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003024 asyncResp->asyncResp->res.jsonValue["Members@odata.count"] =
3025 entriesArray.size();
3026 BMCWEB_LOG_DEBUG << "getChassisCb exit";
3027 };
Anthony Wilson95a3eca2019-06-11 10:44:47 -05003028
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003029 // Get set of sensors in chassis
3030 getChassis(asyncResp, std::move(getChassisCb));
3031 BMCWEB_LOG_DEBUG << "SensorCollection doGet exit";
3032 });
3033}
Anthony Wilson95a3eca2019-06-11 10:44:47 -05003034
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003035inline void requestRoutesSensor(App& app)
3036{
3037 BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/Sensors/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07003038 .privileges(redfish::privileges::getSensor)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003039 .methods(
3040 boost::beast::http::verb::get)([](const crow::Request&,
3041 const std::shared_ptr<
3042 bmcweb::AsyncResp>& aResp,
3043 const std::string& chassisId,
3044 const std::string& sensorName) {
3045 BMCWEB_LOG_DEBUG << "Sensor doGet enter";
3046 std::shared_ptr<SensorsAsyncResp> asyncResp =
3047 std::make_shared<SensorsAsyncResp>(aResp, chassisId,
3048 std::vector<const char*>(),
3049 sensors::node::sensors);
Anthony Wilson95a3eca2019-06-11 10:44:47 -05003050
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003051 const std::array<const char*, 1> interfaces = {
3052 "xyz.openbmc_project.Sensor.Value"};
3053
3054 // Get a list of all of the sensors that implement Sensor.Value
3055 // and get the path and service name associated with the sensor
3056 crow::connections::systemBus->async_method_call(
3057 [asyncResp, sensorName](const boost::system::error_code ec,
3058 const GetSubTreeType& subtree) {
3059 BMCWEB_LOG_DEBUG << "respHandler1 enter";
3060 if (ec)
3061 {
3062 messages::internalError(asyncResp->asyncResp->res);
3063 BMCWEB_LOG_ERROR
3064 << "Sensor getSensorPaths resp_handler: "
3065 << "Dbus error " << ec;
3066 return;
3067 }
3068
3069 GetSubTreeType::const_iterator it = std::find_if(
3070 subtree.begin(), subtree.end(),
3071 [sensorName](
3072 const std::pair<
3073 std::string,
3074 std::vector<std::pair<
3075 std::string, std::vector<std::string>>>>&
3076 object) {
3077 sdbusplus::message::object_path path(object.first);
3078 std::string name = path.filename();
3079 if (name.empty())
3080 {
3081 BMCWEB_LOG_ERROR << "Invalid sensor path: "
3082 << object.first;
3083 return false;
3084 }
3085
3086 return name == sensorName;
3087 });
3088
3089 if (it == subtree.end())
3090 {
3091 BMCWEB_LOG_ERROR << "Could not find path for sensor: "
3092 << sensorName;
3093 messages::resourceNotFound(asyncResp->asyncResp->res,
3094 "Sensor", sensorName);
3095 return;
3096 }
3097 std::string_view sensorPath = (*it).first;
3098 BMCWEB_LOG_DEBUG << "Found sensor path for sensor '"
3099 << sensorName << "': " << sensorPath;
3100
3101 const std::shared_ptr<
3102 boost::container::flat_set<std::string>>
3103 sensorList = std::make_shared<
3104 boost::container::flat_set<std::string>>();
3105
3106 sensorList->emplace(sensorPath);
3107 processSensorList(asyncResp, sensorList);
3108 BMCWEB_LOG_DEBUG << "respHandler1 exit";
3109 },
3110 "xyz.openbmc_project.ObjectMapper",
3111 "/xyz/openbmc_project/object_mapper",
3112 "xyz.openbmc_project.ObjectMapper", "GetSubTree",
3113 "/xyz/openbmc_project/sensors", 2, interfaces);
3114 });
3115}
Anthony Wilson95a3eca2019-06-11 10:44:47 -05003116
Ed Tanous1abe55e2018-09-05 08:30:59 -07003117} // namespace redfish