blob: 31396356489248138739ec21cecf1838f6abf99f [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 Tanous168e20c2021-12-13 14:39:53 -080024#include <dbus_utility.hpp>
Ed Tanoused398212021-06-09 17:05:54 -070025#include <registries/privilege_registry.hpp>
Jonathan Doman1e1e5982021-06-11 09:36:17 -070026#include <sdbusplus/asio/property.hpp>
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +053027#include <utils/json_utils.hpp>
Gunnar Mills1214b7e2020-06-04 10:11:30 -050028
29#include <cmath>
Ed Tanousb5a76932020-09-29 16:16:58 -070030#include <utility>
Ed Tanousabf2add2019-01-22 16:40:12 -080031#include <variant>
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +010032
Ed Tanous1abe55e2018-09-05 08:30:59 -070033namespace redfish
34{
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +010035
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +010036using GetSubTreeType = std::vector<
37 std::pair<std::string,
38 std::vector<std::pair<std::string, std::vector<std::string>>>>>;
39
40using ManagedObjectsVectorType = std::vector<std::pair<
Ed Tanousaa2e59c2018-04-12 12:17:20 -070041 sdbusplus::message::object_path,
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +010042 boost::container::flat_map<
Ed Tanous168e20c2021-12-13 14:39:53 -080043 std::string, boost::container::flat_map<
44 std::string, dbus::utility::DbusVariantType>>>>;
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",
Basheer Ahmed Muddebihal70886902021-07-22 02:11:17 -070066 "/xyz/openbmc_project/sensors/airflow",
George Liue8204932021-02-01 14:42:49 +080067#ifdef BMCWEB_NEW_POWERSUBSYSTEM_THERMALSUBSYSTEM
68 "/xyz/openbmc_project/sensors/voltage",
69 "/xyz/openbmc_project/sensors/fan_tach",
70 "/xyz/openbmc_project/sensors/temperature",
71 "/xyz/openbmc_project/sensors/fan_pwm",
72 "/xyz/openbmc_project/sensors/altitude",
73 "/xyz/openbmc_project/sensors/energy",
74#endif
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +020075 "/xyz/openbmc_project/sensors/utilization"}},
76 {node::thermal,
77 {"/xyz/openbmc_project/sensors/fan_tach",
78 "/xyz/openbmc_project/sensors/temperature",
79 "/xyz/openbmc_project/sensors/fan_pwm"}}};
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +000080} // namespace dbus
81
82inline const char* toReadingType(const std::string& sensorType)
83{
84 if (sensorType == "voltage")
85 {
86 return "Voltage";
87 }
88 if (sensorType == "power")
89 {
90 return "Power";
91 }
92 if (sensorType == "current")
93 {
94 return "Current";
95 }
96 if (sensorType == "fan_tach")
97 {
98 return "Rotational";
99 }
100 if (sensorType == "temperature")
101 {
102 return "Temperature";
103 }
104 if (sensorType == "fan_pwm" || sensorType == "utilization")
105 {
106 return "Percent";
107 }
108 if (sensorType == "altitude")
109 {
110 return "Altitude";
111 }
112 if (sensorType == "airflow")
113 {
114 return "AirFlow";
115 }
116 if (sensorType == "energy")
117 {
118 return "EnergyJoules";
119 }
120 return "";
121}
122
123inline const char* toReadingUnits(const std::string& sensorType)
124{
125 if (sensorType == "voltage")
126 {
127 return "V";
128 }
129 if (sensorType == "power")
130 {
131 return "W";
132 }
133 if (sensorType == "current")
134 {
135 return "A";
136 }
137 if (sensorType == "fan_tach")
138 {
139 return "RPM";
140 }
141 if (sensorType == "temperature")
142 {
143 return "Cel";
144 }
145 if (sensorType == "fan_pwm" || sensorType == "utilization")
146 {
147 return "%";
148 }
149 if (sensorType == "altitude")
150 {
151 return "m";
152 }
153 if (sensorType == "airflow")
154 {
155 return "cft_i/min";
156 }
157 if (sensorType == "energy")
158 {
159 return "J";
160 }
161 return "";
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200162}
163} // namespace sensors
164
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100165/**
Kowalski, Kamil588c3f02018-04-03 14:55:27 +0200166 * SensorsAsyncResp
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100167 * Gathers data needed for response processing after async calls are done
168 */
Ed Tanous1abe55e2018-09-05 08:30:59 -0700169class SensorsAsyncResp
170{
171 public:
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200172 using DataCompleteCb = std::function<void(
173 const boost::beast::http::status status,
174 const boost::container::flat_map<std::string, std::string>& uriToDbus)>;
175
176 struct SensorData
177 {
178 const std::string name;
179 std::string uri;
180 const std::string valueKey;
181 const std::string dbusPath;
182 };
183
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) :
zhanghch058d1b46d2021-04-01 11:18:24 +0800188 asyncResp(asyncResp),
Ed Tanous271584a2019-07-09 16:24:22 -0700189 chassisId(chassisIdIn), types(typesIn), chassisSubNode(subNode)
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500190 {}
Kowalski, Kamil588c3f02018-04-03 14:55:27 +0200191
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200192 // Store extra data about sensor mapping and return it in callback
zhanghch058d1b46d2021-04-01 11:18:24 +0800193 SensorsAsyncResp(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
194 const std::string& chassisIdIn,
Ed Tanousb5a76932020-09-29 16:16:58 -0700195 const std::vector<const char*>& typesIn,
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200196 const std::string_view& subNode,
197 DataCompleteCb&& creationComplete) :
zhanghch058d1b46d2021-04-01 11:18:24 +0800198 asyncResp(asyncResp),
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200199 chassisId(chassisIdIn), types(typesIn),
200 chassisSubNode(subNode), metadata{std::vector<SensorData>()},
201 dataComplete{std::move(creationComplete)}
202 {}
203
Ed Tanous1abe55e2018-09-05 08:30:59 -0700204 ~SensorsAsyncResp()
205 {
zhanghch058d1b46d2021-04-01 11:18:24 +0800206 if (asyncResp->res.result() ==
207 boost::beast::http::status::internal_server_error)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700208 {
209 // Reset the json object to clear out any data that made it in
210 // before the error happened todo(ed) handle error condition with
211 // proper code
zhanghch058d1b46d2021-04-01 11:18:24 +0800212 asyncResp->res.jsonValue = nlohmann::json::object();
Ed Tanous1abe55e2018-09-05 08:30:59 -0700213 }
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200214
215 if (dataComplete && metadata)
216 {
217 boost::container::flat_map<std::string, std::string> map;
zhanghch058d1b46d2021-04-01 11:18:24 +0800218 if (asyncResp->res.result() == boost::beast::http::status::ok)
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200219 {
220 for (auto& sensor : *metadata)
221 {
222 map.insert(std::make_pair(sensor.uri + sensor.valueKey,
223 sensor.dbusPath));
224 }
225 }
zhanghch058d1b46d2021-04-01 11:18:24 +0800226 dataComplete(asyncResp->res.result(), map);
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200227 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700228 }
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100229
Ed Tanousecd6a3a2022-01-07 09:18:40 -0800230 SensorsAsyncResp(const SensorsAsyncResp&) = delete;
231 SensorsAsyncResp(SensorsAsyncResp&&) = delete;
232 SensorsAsyncResp& operator=(const SensorsAsyncResp&) = delete;
233 SensorsAsyncResp& operator=(SensorsAsyncResp&&) = delete;
234
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200235 void addMetadata(const nlohmann::json& sensorObject,
236 const std::string& valueKey, const std::string& dbusPath)
237 {
238 if (metadata)
239 {
240 metadata->emplace_back(SensorData{sensorObject["Name"],
241 sensorObject["@odata.id"],
242 valueKey, dbusPath});
243 }
244 }
245
246 void updateUri(const std::string& name, const std::string& uri)
247 {
248 if (metadata)
249 {
250 for (auto& sensor : *metadata)
251 {
252 if (sensor.name == name)
253 {
254 sensor.uri = uri;
255 }
256 }
257 }
258 }
259
zhanghch058d1b46d2021-04-01 11:18:24 +0800260 const std::shared_ptr<bmcweb::AsyncResp> asyncResp;
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200261 const std::string chassisId;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700262 const std::vector<const char*> types;
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200263 const std::string chassisSubNode;
264
265 private:
266 std::optional<std::vector<SensorData>> metadata;
267 DataCompleteCb dataComplete;
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100268};
269
270/**
Anthony Wilsond5005492019-07-31 16:34:17 -0500271 * Possible states for physical inventory leds
272 */
273enum class LedState
274{
275 OFF,
276 ON,
277 BLINK,
278 UNKNOWN
279};
280
281/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500282 * D-Bus inventory item associated with one or more sensors.
283 */
284class InventoryItem
285{
286 public:
Ed Tanouse05aec52022-01-25 10:28:56 -0800287 InventoryItem(const std::string& objPath) : objectPath(objPath)
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500288 {
289 // Set inventory item name to last node of object path
George Liu28aa8de2021-02-01 15:13:30 +0800290 sdbusplus::message::object_path path(objectPath);
291 name = path.filename();
292 if (name.empty())
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500293 {
George Liu28aa8de2021-02-01 15:13:30 +0800294 BMCWEB_LOG_ERROR << "Failed to find '/' in " << objectPath;
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500295 }
296 }
297
298 std::string objectPath;
299 std::string name;
Ed Tanouse05aec52022-01-25 10:28:56 -0800300 bool isPresent = true;
301 bool isFunctional = true;
302 bool isPowerSupply = false;
303 int powerSupplyEfficiencyPercent = -1;
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500304 std::string manufacturer;
305 std::string model;
306 std::string partNumber;
307 std::string serialNumber;
308 std::set<std::string> sensors;
Anthony Wilsond5005492019-07-31 16:34:17 -0500309 std::string ledObjectPath;
Ed Tanouse05aec52022-01-25 10:28:56 -0800310 LedState ledState = LedState::UNKNOWN;
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500311};
312
313/**
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530314 * @brief Get objects with connection necessary for sensors
Kowalski, Kamil588c3f02018-04-03 14:55:27 +0200315 * @param SensorsAsyncResp Pointer to object holding response data
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100316 * @param sensorNames Sensors retrieved from chassis
317 * @param callback Callback for processing gathered connections
318 */
319template <typename Callback>
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530320void getObjectsWithConnection(
Ed Tanous81ce6092020-12-17 16:54:55 +0000321 const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
Ed Tanousb5a76932020-09-29 16:16:58 -0700322 const std::shared_ptr<boost::container::flat_set<std::string>>& sensorNames,
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530323 Callback&& callback)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700324{
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530325 BMCWEB_LOG_DEBUG << "getObjectsWithConnection enter";
Ed Tanous1abe55e2018-09-05 08:30:59 -0700326 const std::string path = "/xyz/openbmc_project/sensors";
327 const std::array<std::string, 1> interfaces = {
328 "xyz.openbmc_project.Sensor.Value"};
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100329
Ed Tanous1abe55e2018-09-05 08:30:59 -0700330 // Response handler for parsing objects subtree
Ed Tanousf94c4ec2022-01-06 12:44:41 -0800331 auto respHandler = [callback{std::forward<Callback>(callback)},
332 sensorsAsyncResp,
Ed Tanous1abe55e2018-09-05 08:30:59 -0700333 sensorNames](const boost::system::error_code ec,
334 const GetSubTreeType& subtree) {
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530335 BMCWEB_LOG_DEBUG << "getObjectsWithConnection resp_handler enter";
Ed Tanous1abe55e2018-09-05 08:30:59 -0700336 if (ec)
337 {
zhanghch058d1b46d2021-04-01 11:18:24 +0800338 messages::internalError(sensorsAsyncResp->asyncResp->res);
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530339 BMCWEB_LOG_ERROR
340 << "getObjectsWithConnection resp_handler: Dbus error " << ec;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700341 return;
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100342 }
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100343
Ed Tanous1abe55e2018-09-05 08:30:59 -0700344 BMCWEB_LOG_DEBUG << "Found " << subtree.size() << " subtrees";
345
346 // Make unique list of connections only for requested sensor types and
347 // found in the chassis
348 boost::container::flat_set<std::string> connections;
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530349 std::set<std::pair<std::string, std::string>> objectsWithConnection;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700350 // Intrinsic to avoid malloc. Most systems will have < 8 sensor
351 // producers
352 connections.reserve(8);
353
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700354 BMCWEB_LOG_DEBUG << "sensorNames list count: " << sensorNames->size();
355 for (const std::string& tsensor : *sensorNames)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700356 {
357 BMCWEB_LOG_DEBUG << "Sensor to find: " << tsensor;
358 }
359
360 for (const std::pair<
361 std::string,
362 std::vector<std::pair<std::string, std::vector<std::string>>>>&
363 object : subtree)
364 {
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700365 if (sensorNames->find(object.first) != sensorNames->end())
Ed Tanous1abe55e2018-09-05 08:30:59 -0700366 {
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700367 for (const std::pair<std::string, std::vector<std::string>>&
368 objData : object.second)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700369 {
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700370 BMCWEB_LOG_DEBUG << "Adding connection: " << objData.first;
371 connections.insert(objData.first);
372 objectsWithConnection.insert(
373 std::make_pair(object.first, objData.first));
Ed Tanous1abe55e2018-09-05 08:30:59 -0700374 }
375 }
376 }
377 BMCWEB_LOG_DEBUG << "Found " << connections.size() << " connections";
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530378 callback(std::move(connections), std::move(objectsWithConnection));
379 BMCWEB_LOG_DEBUG << "getObjectsWithConnection resp_handler exit";
Ed Tanous1abe55e2018-09-05 08:30:59 -0700380 };
Ed Tanous1abe55e2018-09-05 08:30:59 -0700381 // Make call to ObjectMapper to find all sensors objects
382 crow::connections::systemBus->async_method_call(
383 std::move(respHandler), "xyz.openbmc_project.ObjectMapper",
384 "/xyz/openbmc_project/object_mapper",
385 "xyz.openbmc_project.ObjectMapper", "GetSubTree", path, 2, interfaces);
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530386 BMCWEB_LOG_DEBUG << "getObjectsWithConnection exit";
387}
388
389/**
390 * @brief Create connections necessary for sensors
391 * @param SensorsAsyncResp Pointer to object holding response data
392 * @param sensorNames Sensors retrieved from chassis
393 * @param callback Callback for processing gathered connections
394 */
395template <typename Callback>
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700396void getConnections(
Ed Tanous81ce6092020-12-17 16:54:55 +0000397 std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700398 const std::shared_ptr<boost::container::flat_set<std::string>> sensorNames,
399 Callback&& callback)
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530400{
401 auto objectsWithConnectionCb =
402 [callback](const boost::container::flat_set<std::string>& connections,
403 const std::set<std::pair<std::string, std::string>>&
Ed Tanous3174e4d2020-10-07 11:41:22 -0700404 /*objectsWithConnection*/) { callback(connections); };
Ed Tanous81ce6092020-12-17 16:54:55 +0000405 getObjectsWithConnection(sensorsAsyncResp, sensorNames,
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530406 std::move(objectsWithConnectionCb));
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100407}
408
409/**
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700410 * @brief Shrinks the list of sensors for processing
411 * @param SensorsAysncResp The class holding the Redfish response
412 * @param allSensors A list of all the sensors associated to the
413 * chassis element (i.e. baseboard, front panel, etc...)
414 * @param activeSensors A list that is a reduction of the incoming
415 * allSensors list. Eliminate Thermal sensors when a Power request is
416 * made, and eliminate Power sensors when a Thermal request is made.
417 */
Ed Tanous23a21a12020-07-25 04:45:05 +0000418inline void reduceSensorList(
Ed Tanous81ce6092020-12-17 16:54:55 +0000419 const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700420 const std::vector<std::string>* allSensors,
Ed Tanousb5a76932020-09-29 16:16:58 -0700421 const std::shared_ptr<boost::container::flat_set<std::string>>&
422 activeSensors)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700423{
Ed Tanous81ce6092020-12-17 16:54:55 +0000424 if (sensorsAsyncResp == nullptr)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700425 {
426 return;
427 }
428 if ((allSensors == nullptr) || (activeSensors == nullptr))
429 {
430 messages::resourceNotFound(
zhanghch058d1b46d2021-04-01 11:18:24 +0800431 sensorsAsyncResp->asyncResp->res, sensorsAsyncResp->chassisSubNode,
Ed Tanous81ce6092020-12-17 16:54:55 +0000432 sensorsAsyncResp->chassisSubNode == sensors::node::thermal
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200433 ? "Temperatures"
434 : "Voltages");
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700435
436 return;
437 }
438 if (allSensors->empty())
439 {
440 // Nothing to do, the activeSensors object is also empty
441 return;
442 }
443
Ed Tanous81ce6092020-12-17 16:54:55 +0000444 for (const char* type : sensorsAsyncResp->types)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700445 {
446 for (const std::string& sensor : *allSensors)
447 {
448 if (boost::starts_with(sensor, type))
449 {
450 activeSensors->emplace(sensor);
451 }
452 }
453 }
454}
455
456/**
Carol Wang4bb3dc32019-10-17 18:15:02 +0800457 * @brief Retrieves valid chassis path
458 * @param asyncResp Pointer to object holding response data
459 * @param callback Callback for next step to get valid chassis path
460 */
461template <typename Callback>
Ed Tanousb5a76932020-09-29 16:16:58 -0700462void getValidChassisPath(const std::shared_ptr<SensorsAsyncResp>& asyncResp,
Carol Wang4bb3dc32019-10-17 18:15:02 +0800463 Callback&& callback)
464{
465 BMCWEB_LOG_DEBUG << "checkChassisId enter";
466 const std::array<const char*, 2> interfaces = {
467 "xyz.openbmc_project.Inventory.Item.Board",
468 "xyz.openbmc_project.Inventory.Item.Chassis"};
469
470 auto respHandler =
Ed Tanousf94c4ec2022-01-06 12:44:41 -0800471 [callback{std::forward<Callback>(callback)},
Carol Wang4bb3dc32019-10-17 18:15:02 +0800472 asyncResp](const boost::system::error_code ec,
473 const std::vector<std::string>& chassisPaths) mutable {
474 BMCWEB_LOG_DEBUG << "getValidChassisPath respHandler enter";
475 if (ec)
476 {
477 BMCWEB_LOG_ERROR
478 << "getValidChassisPath respHandler DBUS error: " << ec;
zhanghch058d1b46d2021-04-01 11:18:24 +0800479 messages::internalError(asyncResp->asyncResp->res);
Carol Wang4bb3dc32019-10-17 18:15:02 +0800480 return;
481 }
482
483 std::optional<std::string> chassisPath;
484 std::string chassisName;
485 for (const std::string& chassis : chassisPaths)
486 {
George Liu28aa8de2021-02-01 15:13:30 +0800487 sdbusplus::message::object_path path(chassis);
488 chassisName = path.filename();
489 if (chassisName.empty())
Carol Wang4bb3dc32019-10-17 18:15:02 +0800490 {
491 BMCWEB_LOG_ERROR << "Failed to find '/' in " << chassis;
492 continue;
493 }
Carol Wang4bb3dc32019-10-17 18:15:02 +0800494 if (chassisName == asyncResp->chassisId)
495 {
496 chassisPath = chassis;
497 break;
498 }
499 }
500 callback(chassisPath);
501 };
502
503 // Get the Chassis Collection
504 crow::connections::systemBus->async_method_call(
505 respHandler, "xyz.openbmc_project.ObjectMapper",
506 "/xyz/openbmc_project/object_mapper",
507 "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths",
508 "/xyz/openbmc_project/inventory", 0, interfaces);
509 BMCWEB_LOG_DEBUG << "checkChassisId exit";
510}
511
512/**
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100513 * @brief Retrieves requested chassis sensors and redundancy data from DBus .
Kowalski, Kamil588c3f02018-04-03 14:55:27 +0200514 * @param SensorsAsyncResp Pointer to object holding response data
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100515 * @param callback Callback for next step in gathered sensor processing
516 */
517template <typename Callback>
Ed Tanousb5a76932020-09-29 16:16:58 -0700518void getChassis(const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
Ed Tanous1abe55e2018-09-05 08:30:59 -0700519 Callback&& callback)
520{
521 BMCWEB_LOG_DEBUG << "getChassis enter";
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500522 const std::array<const char*, 2> interfaces = {
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700523 "xyz.openbmc_project.Inventory.Item.Board",
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500524 "xyz.openbmc_project.Inventory.Item.Chassis"};
Ed Tanousf94c4ec2022-01-06 12:44:41 -0800525 auto respHandler = [callback{std::forward<Callback>(callback)},
526 sensorsAsyncResp](
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700527 const boost::system::error_code ec,
528 const std::vector<std::string>& chassisPaths) {
Ed Tanous1abe55e2018-09-05 08:30:59 -0700529 BMCWEB_LOG_DEBUG << "getChassis respHandler enter";
530 if (ec)
531 {
532 BMCWEB_LOG_ERROR << "getChassis respHandler DBUS error: " << ec;
zhanghch058d1b46d2021-04-01 11:18:24 +0800533 messages::internalError(sensorsAsyncResp->asyncResp->res);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700534 return;
535 }
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100536
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700537 const std::string* chassisPath = nullptr;
538 std::string chassisName;
539 for (const std::string& chassis : chassisPaths)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700540 {
George Liu28aa8de2021-02-01 15:13:30 +0800541 sdbusplus::message::object_path path(chassis);
542 chassisName = path.filename();
543 if (chassisName.empty())
Ed Tanous1abe55e2018-09-05 08:30:59 -0700544 {
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700545 BMCWEB_LOG_ERROR << "Failed to find '/' in " << chassis;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700546 continue;
547 }
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700548 if (chassisName == sensorsAsyncResp->chassisId)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700549 {
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700550 chassisPath = &chassis;
551 break;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700552 }
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700553 }
554 if (chassisPath == nullptr)
555 {
zhanghch058d1b46d2021-04-01 11:18:24 +0800556 messages::resourceNotFound(sensorsAsyncResp->asyncResp->res,
557 "Chassis", sensorsAsyncResp->chassisId);
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700558 return;
559 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700560
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700561 const std::string& chassisSubNode = sensorsAsyncResp->chassisSubNode;
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200562 if (chassisSubNode == sensors::node::power)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700563 {
zhanghch058d1b46d2021-04-01 11:18:24 +0800564 sensorsAsyncResp->asyncResp->res.jsonValue["@odata.type"] =
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700565 "#Power.v1_5_2.Power";
Ed Tanous1abe55e2018-09-05 08:30:59 -0700566 }
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200567 else if (chassisSubNode == sensors::node::thermal)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700568 {
zhanghch058d1b46d2021-04-01 11:18:24 +0800569 sensorsAsyncResp->asyncResp->res.jsonValue["@odata.type"] =
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700570 "#Thermal.v1_4_0.Thermal";
zhanghch058d1b46d2021-04-01 11:18:24 +0800571 sensorsAsyncResp->asyncResp->res.jsonValue["Fans"] =
572 nlohmann::json::array();
573 sensorsAsyncResp->asyncResp->res.jsonValue["Temperatures"] =
Jennifer Lee4f9a2132019-03-04 12:45:19 -0800574 nlohmann::json::array();
Ed Tanous1abe55e2018-09-05 08:30:59 -0700575 }
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200576 else if (chassisSubNode == sensors::node::sensors)
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500577 {
zhanghch058d1b46d2021-04-01 11:18:24 +0800578 sensorsAsyncResp->asyncResp->res.jsonValue["@odata.type"] =
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500579 "#SensorCollection.SensorCollection";
zhanghch058d1b46d2021-04-01 11:18:24 +0800580 sensorsAsyncResp->asyncResp->res.jsonValue["Description"] =
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500581 "Collection of Sensors for this Chassis";
zhanghch058d1b46d2021-04-01 11:18:24 +0800582 sensorsAsyncResp->asyncResp->res.jsonValue["Members"] =
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500583 nlohmann::json::array();
zhanghch058d1b46d2021-04-01 11:18:24 +0800584 sensorsAsyncResp->asyncResp->res.jsonValue["Members@odata.count"] =
585 0;
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500586 }
587
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200588 if (chassisSubNode != sensors::node::sensors)
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500589 {
zhanghch058d1b46d2021-04-01 11:18:24 +0800590 sensorsAsyncResp->asyncResp->res.jsonValue["Id"] = chassisSubNode;
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500591 }
592
zhanghch058d1b46d2021-04-01 11:18:24 +0800593 sensorsAsyncResp->asyncResp->res.jsonValue["@odata.id"] =
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700594 "/redfish/v1/Chassis/" + sensorsAsyncResp->chassisId + "/" +
595 chassisSubNode;
zhanghch058d1b46d2021-04-01 11:18:24 +0800596 sensorsAsyncResp->asyncResp->res.jsonValue["Name"] = chassisSubNode;
Shawn McCarney8fb49dd2019-06-12 17:47:00 -0500597 // Get the list of all sensors for this Chassis element
598 std::string sensorPath = *chassisPath + "/all_sensors";
Jonathan Doman1e1e5982021-06-11 09:36:17 -0700599 sdbusplus::asio::getProperty<std::vector<std::string>>(
600 *crow::connections::systemBus, "xyz.openbmc_project.ObjectMapper",
601 sensorPath, "xyz.openbmc_project.Association", "endpoints",
Ed Tanousf94c4ec2022-01-06 12:44:41 -0800602 [sensorsAsyncResp,
603 callback{std::forward<const Callback>(callback)}](
Ed Tanous271584a2019-07-09 16:24:22 -0700604 const boost::system::error_code& e,
Jonathan Doman1e1e5982021-06-11 09:36:17 -0700605 const std::vector<std::string>& nodeSensorList) {
Ed Tanous271584a2019-07-09 16:24:22 -0700606 if (e)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700607 {
Ed Tanous271584a2019-07-09 16:24:22 -0700608 if (e.value() != EBADR)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700609 {
zhanghch058d1b46d2021-04-01 11:18:24 +0800610 messages::internalError(
611 sensorsAsyncResp->asyncResp->res);
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700612 return;
613 }
614 }
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700615 const std::shared_ptr<boost::container::flat_set<std::string>>
616 culledSensorList = std::make_shared<
617 boost::container::flat_set<std::string>>();
Jonathan Doman1e1e5982021-06-11 09:36:17 -0700618 reduceSensorList(sensorsAsyncResp, &nodeSensorList,
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700619 culledSensorList);
620 callback(culledSensorList);
Jonathan Doman1e1e5982021-06-11 09:36:17 -0700621 });
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100622 };
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100623
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700624 // Get the Chassis Collection
Ed Tanous1abe55e2018-09-05 08:30:59 -0700625 crow::connections::systemBus->async_method_call(
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700626 respHandler, "xyz.openbmc_project.ObjectMapper",
627 "/xyz/openbmc_project/object_mapper",
628 "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths",
Ed Tanous271584a2019-07-09 16:24:22 -0700629 "/xyz/openbmc_project/inventory", 0, interfaces);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700630 BMCWEB_LOG_DEBUG << "getChassis exit";
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100631}
632
633/**
Shawn McCarneyde629b62019-03-08 10:42:51 -0600634 * @brief Finds all DBus object paths that implement ObjectManager.
635 *
636 * Creates a mapping from the associated connection name to the object path.
637 *
638 * Finds the object paths asynchronously. Invokes callback when information has
639 * been obtained.
640 *
641 * The callback must have the following signature:
642 * @code
Shawn McCarney8fb49dd2019-06-12 17:47:00 -0500643 * callback(std::shared_ptr<boost::container::flat_map<std::string,
644 * std::string>> objectMgrPaths)
Shawn McCarneyde629b62019-03-08 10:42:51 -0600645 * @endcode
646 *
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700647 * @param sensorsAsyncResp Pointer to object holding response data.
Shawn McCarneyde629b62019-03-08 10:42:51 -0600648 * @param callback Callback to invoke when object paths obtained.
649 */
650template <typename Callback>
Ed Tanousb5a76932020-09-29 16:16:58 -0700651void getObjectManagerPaths(
Ed Tanous81ce6092020-12-17 16:54:55 +0000652 const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
Ed Tanousb5a76932020-09-29 16:16:58 -0700653 Callback&& callback)
Shawn McCarneyde629b62019-03-08 10:42:51 -0600654{
655 BMCWEB_LOG_DEBUG << "getObjectManagerPaths enter";
656 const std::array<std::string, 1> interfaces = {
657 "org.freedesktop.DBus.ObjectManager"};
658
659 // Response handler for GetSubTree DBus method
Ed Tanousf94c4ec2022-01-06 12:44:41 -0800660 auto respHandler = [callback{std::forward<Callback>(callback)},
Ed Tanous81ce6092020-12-17 16:54:55 +0000661 sensorsAsyncResp](const boost::system::error_code ec,
Shawn McCarneyde629b62019-03-08 10:42:51 -0600662 const GetSubTreeType& subtree) {
663 BMCWEB_LOG_DEBUG << "getObjectManagerPaths respHandler enter";
664 if (ec)
665 {
zhanghch058d1b46d2021-04-01 11:18:24 +0800666 messages::internalError(sensorsAsyncResp->asyncResp->res);
Shawn McCarneyde629b62019-03-08 10:42:51 -0600667 BMCWEB_LOG_ERROR << "getObjectManagerPaths respHandler: DBus error "
668 << ec;
669 return;
670 }
671
672 // Loop over returned object paths
Shawn McCarney8fb49dd2019-06-12 17:47:00 -0500673 std::shared_ptr<boost::container::flat_map<std::string, std::string>>
674 objectMgrPaths = std::make_shared<
675 boost::container::flat_map<std::string, std::string>>();
Shawn McCarneyde629b62019-03-08 10:42:51 -0600676 for (const std::pair<
677 std::string,
678 std::vector<std::pair<std::string, std::vector<std::string>>>>&
679 object : subtree)
680 {
681 // Loop over connections for current object path
682 const std::string& objectPath = object.first;
683 for (const std::pair<std::string, std::vector<std::string>>&
684 objData : object.second)
685 {
686 // Add mapping from connection to object path
687 const std::string& connection = objData.first;
Shawn McCarney8fb49dd2019-06-12 17:47:00 -0500688 (*objectMgrPaths)[connection] = objectPath;
Shawn McCarneyde629b62019-03-08 10:42:51 -0600689 BMCWEB_LOG_DEBUG << "Added mapping " << connection << " -> "
690 << objectPath;
691 }
692 }
Shawn McCarney8fb49dd2019-06-12 17:47:00 -0500693 callback(objectMgrPaths);
Shawn McCarneyde629b62019-03-08 10:42:51 -0600694 BMCWEB_LOG_DEBUG << "getObjectManagerPaths respHandler exit";
695 };
696
697 // Query mapper for all DBus object paths that implement ObjectManager
698 crow::connections::systemBus->async_method_call(
699 std::move(respHandler), "xyz.openbmc_project.ObjectMapper",
700 "/xyz/openbmc_project/object_mapper",
Ed Tanous271584a2019-07-09 16:24:22 -0700701 "xyz.openbmc_project.ObjectMapper", "GetSubTree", "/", 0, interfaces);
Shawn McCarneyde629b62019-03-08 10:42:51 -0600702 BMCWEB_LOG_DEBUG << "getObjectManagerPaths exit";
703}
704
705/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500706 * @brief Returns the Redfish State value for the specified inventory item.
707 * @param inventoryItem D-Bus inventory item associated with a sensor.
708 * @return State value for inventory item.
James Feist34dd1792019-05-17 14:10:54 -0700709 */
Ed Tanous23a21a12020-07-25 04:45:05 +0000710inline std::string getState(const InventoryItem* inventoryItem)
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500711{
712 if ((inventoryItem != nullptr) && !(inventoryItem->isPresent))
713 {
714 return "Absent";
715 }
James Feist34dd1792019-05-17 14:10:54 -0700716
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500717 return "Enabled";
718}
719
720/**
721 * @brief Returns the Redfish Health value for the specified sensor.
722 * @param sensorJson Sensor JSON object.
723 * @param interfacesDict Map of all sensor interfaces.
724 * @param inventoryItem D-Bus inventory item associated with the sensor. Will
725 * be nullptr if no associated inventory item was found.
726 * @return Health value for sensor.
727 */
Ed Tanous711ac7a2021-12-20 09:34:41 -0800728inline std::string
729 getHealth(nlohmann::json& sensorJson,
730 const dbus::utility::DBusInteracesMap& interfacesDict,
731 const InventoryItem* inventoryItem)
James Feist34dd1792019-05-17 14:10:54 -0700732{
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500733 // Get current health value (if any) in the sensor JSON object. Some JSON
734 // objects contain multiple sensors (such as PowerSupplies). We want to set
735 // the overall health to be the most severe of any of the sensors.
736 std::string currentHealth;
737 auto statusIt = sensorJson.find("Status");
738 if (statusIt != sensorJson.end())
739 {
740 auto healthIt = statusIt->find("Health");
741 if (healthIt != statusIt->end())
742 {
743 std::string* health = healthIt->get_ptr<std::string*>();
744 if (health != nullptr)
745 {
746 currentHealth = *health;
747 }
748 }
749 }
750
751 // If current health in JSON object is already Critical, return that. This
752 // should override the sensor health, which might be less severe.
753 if (currentHealth == "Critical")
754 {
755 return "Critical";
756 }
757
758 // Check if sensor has critical threshold alarm
Ed Tanous711ac7a2021-12-20 09:34:41 -0800759
Ed Tanous9eb808c2022-01-25 10:19:23 -0800760 for (const auto& [interface, values] : interfacesDict)
James Feist34dd1792019-05-17 14:10:54 -0700761 {
Ed Tanous711ac7a2021-12-20 09:34:41 -0800762 if (interface == "xyz.openbmc_project.Sensor.Threshold.Critical")
James Feist34dd1792019-05-17 14:10:54 -0700763 {
Ed Tanous9eb808c2022-01-25 10:19:23 -0800764 for (const auto& [valueName, value] : values)
James Feist34dd1792019-05-17 14:10:54 -0700765 {
Ed Tanous711ac7a2021-12-20 09:34:41 -0800766 if (valueName == "CriticalAlarmHigh" ||
767 valueName == "CriticalAlarmLow")
768 {
769 const bool* asserted = std::get_if<bool>(&value);
770 if (asserted == nullptr)
771 {
772 BMCWEB_LOG_ERROR << "Illegal sensor threshold";
773 }
774 else if (*asserted)
775 {
776 return "Critical";
777 }
778 }
James Feist34dd1792019-05-17 14:10:54 -0700779 }
780 }
781 }
782
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500783 // Check if associated inventory item is not functional
784 if ((inventoryItem != nullptr) && !(inventoryItem->isFunctional))
785 {
786 return "Critical";
787 }
788
789 // If current health in JSON object is already Warning, return that. This
790 // should override the sensor status, which might be less severe.
791 if (currentHealth == "Warning")
792 {
793 return "Warning";
794 }
795
796 // Check if sensor has warning threshold alarm
Ed Tanous9eb808c2022-01-25 10:19:23 -0800797 for (const auto& [interface, values] : interfacesDict)
James Feist34dd1792019-05-17 14:10:54 -0700798 {
Ed Tanous711ac7a2021-12-20 09:34:41 -0800799 if (interface == "xyz.openbmc_project.Sensor.Threshold.Warning")
James Feist34dd1792019-05-17 14:10:54 -0700800 {
Ed Tanous9eb808c2022-01-25 10:19:23 -0800801 for (const auto& [valueName, value] : values)
James Feist34dd1792019-05-17 14:10:54 -0700802 {
Ed Tanous711ac7a2021-12-20 09:34:41 -0800803 if (valueName == "WarningAlarmHigh" ||
804 valueName == "WarningAlarmLow")
805 {
806 const bool* asserted = std::get_if<bool>(&value);
807 if (asserted == nullptr)
808 {
809 BMCWEB_LOG_ERROR << "Illegal sensor threshold";
810 }
811 else if (*asserted)
812 {
Ed Tanousebe4d912022-01-12 12:38:17 -0800813 return "Warning";
Ed Tanous711ac7a2021-12-20 09:34:41 -0800814 }
815 }
James Feist34dd1792019-05-17 14:10:54 -0700816 }
817 }
818 }
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500819
James Feist34dd1792019-05-17 14:10:54 -0700820 return "OK";
821}
822
Ed Tanous23a21a12020-07-25 04:45:05 +0000823inline void setLedState(nlohmann::json& sensorJson,
Anthony Wilsond5005492019-07-31 16:34:17 -0500824 const InventoryItem* inventoryItem)
825{
826 if (inventoryItem != nullptr && !inventoryItem->ledObjectPath.empty())
827 {
828 switch (inventoryItem->ledState)
829 {
830 case LedState::OFF:
831 sensorJson["IndicatorLED"] = "Off";
832 break;
833 case LedState::ON:
834 sensorJson["IndicatorLED"] = "Lit";
835 break;
836 case LedState::BLINK:
837 sensorJson["IndicatorLED"] = "Blinking";
838 break;
Ed Tanous23a21a12020-07-25 04:45:05 +0000839 case LedState::UNKNOWN:
Anthony Wilsond5005492019-07-31 16:34:17 -0500840 break;
841 }
842 }
843}
844
James Feist34dd1792019-05-17 14:10:54 -0700845/**
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100846 * @brief Builds a json sensor representation of a sensor.
847 * @param sensorName The name of the sensor to be built
Gunnar Mills274fad52018-06-13 15:45:36 -0500848 * @param sensorType The type (temperature, fan_tach, etc) of the sensor to
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100849 * build
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200850 * @param sensorsAsyncResp Sensor metadata
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100851 * @param interfacesDict A dictionary of the interfaces and properties of said
852 * interfaces to be built from
853 * @param sensor_json The json object to fill
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500854 * @param inventoryItem D-Bus inventory item associated with the sensor. Will
855 * be nullptr if no associated inventory item was found.
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100856 */
Ed Tanous23a21a12020-07-25 04:45:05 +0000857inline void objectInterfacesToJson(
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100858 const std::string& sensorName, const std::string& sensorType,
Ed Tanousb5a76932020-09-29 16:16:58 -0700859 const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
Ed Tanous711ac7a2021-12-20 09:34:41 -0800860 const dbus::utility::DBusInteracesMap& interfacesDict,
Ed Tanous81ce6092020-12-17 16:54:55 +0000861 nlohmann::json& sensorJson, InventoryItem* inventoryItem)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700862{
Ed Tanous1abe55e2018-09-05 08:30:59 -0700863 // Assume values exist as is (10^0 == 1) if no scale exists
864 int64_t scaleMultiplier = 0;
Ed Tanous9eb808c2022-01-25 10:19:23 -0800865 for (const auto& [interface, values] : interfacesDict)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700866 {
Ed Tanous711ac7a2021-12-20 09:34:41 -0800867 if (interface == "xyz.openbmc_project.Sensor.Value")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700868 {
Ed Tanous9eb808c2022-01-25 10:19:23 -0800869 for (const auto& [valueName, value] : values)
Ed Tanous711ac7a2021-12-20 09:34:41 -0800870 {
871 if (valueName == "Scale")
872 {
873 const int64_t* int64Value = std::get_if<int64_t>(&value);
874 if (int64Value != nullptr)
875 {
876 scaleMultiplier = *int64Value;
877 }
878 }
879 }
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100880 }
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100881 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700882
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200883 if (sensorsAsyncResp->chassisSubNode == sensors::node::sensors)
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500884 {
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500885 // For sensors in SensorCollection we set Id instead of MemberId,
886 // including power sensors.
Ed Tanous81ce6092020-12-17 16:54:55 +0000887 sensorJson["Id"] = sensorName;
888 sensorJson["Name"] = boost::replace_all_copy(sensorName, "_", " ");
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500889 }
890 else if (sensorType != "power")
891 {
892 // Set MemberId and Name for non-power sensors. For PowerSupplies and
893 // PowerControl, those properties have more general values because
894 // multiple sensors can be stored in the same JSON object.
Ed Tanous81ce6092020-12-17 16:54:55 +0000895 sensorJson["MemberId"] = sensorName;
896 sensorJson["Name"] = boost::replace_all_copy(sensorName, "_", " ");
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500897 }
Ed Tanouse742b6c2019-05-03 15:06:53 -0700898
Ed Tanous81ce6092020-12-17 16:54:55 +0000899 sensorJson["Status"]["State"] = getState(inventoryItem);
900 sensorJson["Status"]["Health"] =
901 getHealth(sensorJson, interfacesDict, inventoryItem);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700902
903 // Parameter to set to override the type we get from dbus, and force it to
904 // int, regardless of what is available. This is used for schemas like fan,
905 // that require integers, not floats.
906 bool forceToInt = false;
907
Anthony Wilson3929aca2019-07-19 15:42:33 -0500908 nlohmann::json::json_pointer unit("/Reading");
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200909 if (sensorsAsyncResp->chassisSubNode == sensors::node::sensors)
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500910 {
Ed Tanous81ce6092020-12-17 16:54:55 +0000911 sensorJson["@odata.type"] = "#Sensor.v1_0_0.Sensor";
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000912
913 const std::string& readingType = sensors::toReadingType(sensorType);
914 if (readingType.empty())
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500915 {
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000916 BMCWEB_LOG_ERROR << "Redfish cannot map reading type for "
917 << sensorType;
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500918 }
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000919 else
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500920 {
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000921 sensorJson["ReadingType"] = readingType;
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500922 }
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000923
924 const std::string& readingUnits = sensors::toReadingUnits(sensorType);
925 if (readingUnits.empty())
Adrian Ambrożewiczf8ede152020-06-02 13:26:33 +0200926 {
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000927 BMCWEB_LOG_ERROR << "Redfish cannot map reading unit for "
928 << sensorType;
929 }
930 else
931 {
932 sensorJson["ReadingUnits"] = readingUnits;
Adrian Ambrożewiczf8ede152020-06-02 13:26:33 +0200933 }
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500934 }
935 else if (sensorType == "temperature")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700936 {
Anthony Wilson3929aca2019-07-19 15:42:33 -0500937 unit = "/ReadingCelsius"_json_pointer;
Ed Tanous81ce6092020-12-17 16:54:55 +0000938 sensorJson["@odata.type"] = "#Thermal.v1_3_0.Temperature";
Ed Tanous1abe55e2018-09-05 08:30:59 -0700939 // TODO(ed) Documentation says that path should be type fan_tach,
940 // implementation seems to implement fan
941 }
942 else if (sensorType == "fan" || sensorType == "fan_tach")
943 {
Anthony Wilson3929aca2019-07-19 15:42:33 -0500944 unit = "/Reading"_json_pointer;
Ed Tanous81ce6092020-12-17 16:54:55 +0000945 sensorJson["ReadingUnits"] = "RPM";
946 sensorJson["@odata.type"] = "#Thermal.v1_3_0.Fan";
947 setLedState(sensorJson, inventoryItem);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700948 forceToInt = true;
949 }
Ed Tanous6f6d0d32018-10-12 11:16:43 -0700950 else if (sensorType == "fan_pwm")
951 {
Anthony Wilson3929aca2019-07-19 15:42:33 -0500952 unit = "/Reading"_json_pointer;
Ed Tanous81ce6092020-12-17 16:54:55 +0000953 sensorJson["ReadingUnits"] = "Percent";
954 sensorJson["@odata.type"] = "#Thermal.v1_3_0.Fan";
955 setLedState(sensorJson, inventoryItem);
Ed Tanous6f6d0d32018-10-12 11:16:43 -0700956 forceToInt = true;
957 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700958 else if (sensorType == "voltage")
959 {
Anthony Wilson3929aca2019-07-19 15:42:33 -0500960 unit = "/ReadingVolts"_json_pointer;
Ed Tanous81ce6092020-12-17 16:54:55 +0000961 sensorJson["@odata.type"] = "#Power.v1_0_0.Voltage";
Ed Tanous1abe55e2018-09-05 08:30:59 -0700962 }
Ed Tanous2474adf2018-09-05 16:31:16 -0700963 else if (sensorType == "power")
964 {
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700965 std::string sensorNameLower =
966 boost::algorithm::to_lower_copy(sensorName);
967
Ed Tanous55f79e62022-01-25 11:26:16 -0800968 if (sensorName == "total_power")
Eddie James028f7eb2019-05-17 21:24:36 +0000969 {
Ed Tanous81ce6092020-12-17 16:54:55 +0000970 sensorJson["@odata.type"] = "#Power.v1_0_0.PowerControl";
Gunnar Mills7ab06f42019-07-02 13:07:16 -0500971 // Put multiple "sensors" into a single PowerControl, so have
972 // generic names for MemberId and Name. Follows Redfish mockup.
Ed Tanous81ce6092020-12-17 16:54:55 +0000973 sensorJson["MemberId"] = "0";
974 sensorJson["Name"] = "Chassis Power Control";
Anthony Wilson3929aca2019-07-19 15:42:33 -0500975 unit = "/PowerConsumedWatts"_json_pointer;
Eddie James028f7eb2019-05-17 21:24:36 +0000976 }
977 else if (sensorNameLower.find("input") != std::string::npos)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700978 {
Anthony Wilson3929aca2019-07-19 15:42:33 -0500979 unit = "/PowerInputWatts"_json_pointer;
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700980 }
981 else
982 {
Anthony Wilson3929aca2019-07-19 15:42:33 -0500983 unit = "/PowerOutputWatts"_json_pointer;
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700984 }
Ed Tanous2474adf2018-09-05 16:31:16 -0700985 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700986 else
987 {
988 BMCWEB_LOG_ERROR << "Redfish cannot map object type for " << sensorName;
989 return;
990 }
991 // Map of dbus interface name, dbus property name and redfish property_name
Anthony Wilson3929aca2019-07-19 15:42:33 -0500992 std::vector<
993 std::tuple<const char*, const char*, nlohmann::json::json_pointer>>
994 properties;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700995 properties.reserve(7);
996
997 properties.emplace_back("xyz.openbmc_project.Sensor.Value", "Value", unit);
Shawn McCarneyde629b62019-03-08 10:42:51 -0600998
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200999 if (sensorsAsyncResp->chassisSubNode == sensors::node::sensors)
Anthony Wilson3929aca2019-07-19 15:42:33 -05001000 {
1001 properties.emplace_back(
1002 "xyz.openbmc_project.Sensor.Threshold.Warning", "WarningHigh",
1003 "/Thresholds/UpperCaution/Reading"_json_pointer);
1004 properties.emplace_back(
1005 "xyz.openbmc_project.Sensor.Threshold.Warning", "WarningLow",
1006 "/Thresholds/LowerCaution/Reading"_json_pointer);
1007 properties.emplace_back(
1008 "xyz.openbmc_project.Sensor.Threshold.Critical", "CriticalHigh",
1009 "/Thresholds/UpperCritical/Reading"_json_pointer);
1010 properties.emplace_back(
1011 "xyz.openbmc_project.Sensor.Threshold.Critical", "CriticalLow",
1012 "/Thresholds/LowerCritical/Reading"_json_pointer);
1013 }
1014 else if (sensorType != "power")
Shawn McCarneyde629b62019-03-08 10:42:51 -06001015 {
1016 properties.emplace_back("xyz.openbmc_project.Sensor.Threshold.Warning",
Anthony Wilson3929aca2019-07-19 15:42:33 -05001017 "WarningHigh",
1018 "/UpperThresholdNonCritical"_json_pointer);
Shawn McCarneyde629b62019-03-08 10:42:51 -06001019 properties.emplace_back("xyz.openbmc_project.Sensor.Threshold.Warning",
Anthony Wilson3929aca2019-07-19 15:42:33 -05001020 "WarningLow",
1021 "/LowerThresholdNonCritical"_json_pointer);
Shawn McCarneyde629b62019-03-08 10:42:51 -06001022 properties.emplace_back("xyz.openbmc_project.Sensor.Threshold.Critical",
Anthony Wilson3929aca2019-07-19 15:42:33 -05001023 "CriticalHigh",
1024 "/UpperThresholdCritical"_json_pointer);
Shawn McCarneyde629b62019-03-08 10:42:51 -06001025 properties.emplace_back("xyz.openbmc_project.Sensor.Threshold.Critical",
Anthony Wilson3929aca2019-07-19 15:42:33 -05001026 "CriticalLow",
1027 "/LowerThresholdCritical"_json_pointer);
Shawn McCarneyde629b62019-03-08 10:42:51 -06001028 }
Ed Tanous1abe55e2018-09-05 08:30:59 -07001029
Ed Tanous2474adf2018-09-05 16:31:16 -07001030 // TODO Need to get UpperThresholdFatal and LowerThresholdFatal
1031
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02001032 if (sensorsAsyncResp->chassisSubNode == sensors::node::sensors)
Anthony Wilson95a3eca2019-06-11 10:44:47 -05001033 {
1034 properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MinValue",
Anthony Wilson3929aca2019-07-19 15:42:33 -05001035 "/ReadingRangeMin"_json_pointer);
Anthony Wilson95a3eca2019-06-11 10:44:47 -05001036 properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MaxValue",
Anthony Wilson3929aca2019-07-19 15:42:33 -05001037 "/ReadingRangeMax"_json_pointer);
Anthony Wilson95a3eca2019-06-11 10:44:47 -05001038 }
1039 else if (sensorType == "temperature")
Ed Tanous1abe55e2018-09-05 08:30:59 -07001040 {
1041 properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MinValue",
Anthony Wilson3929aca2019-07-19 15:42:33 -05001042 "/MinReadingRangeTemp"_json_pointer);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001043 properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MaxValue",
Anthony Wilson3929aca2019-07-19 15:42:33 -05001044 "/MaxReadingRangeTemp"_json_pointer);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001045 }
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001046 else if (sensorType != "power")
Ed Tanous1abe55e2018-09-05 08:30:59 -07001047 {
1048 properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MinValue",
Anthony Wilson3929aca2019-07-19 15:42:33 -05001049 "/MinReadingRange"_json_pointer);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001050 properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MaxValue",
Anthony Wilson3929aca2019-07-19 15:42:33 -05001051 "/MaxReadingRange"_json_pointer);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001052 }
1053
Anthony Wilson3929aca2019-07-19 15:42:33 -05001054 for (const std::tuple<const char*, const char*,
1055 nlohmann::json::json_pointer>& p : properties)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001056 {
Ed Tanous55f79e62022-01-25 11:26:16 -08001057 for (const auto& [interface, values] : interfacesDict)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001058 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001059 if (interface != std::get<0>(p))
Ed Tanous1abe55e2018-09-05 08:30:59 -07001060 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001061 continue;
1062 }
Ed Tanous55f79e62022-01-25 11:26:16 -08001063 for (const auto& [valueName, valueVariant] : values)
Ed Tanous711ac7a2021-12-20 09:34:41 -08001064 {
1065 if (valueName != std::get<1>(p))
1066 {
1067 continue;
1068 }
Anthony Wilson3929aca2019-07-19 15:42:33 -05001069
1070 // The property we want to set may be nested json, so use
1071 // a json_pointer for easy indexing into the json structure.
1072 const nlohmann::json::json_pointer& key = std::get<2>(p);
1073
Ed Tanous1abe55e2018-09-05 08:30:59 -07001074 // Attempt to pull the int64 directly
Ed Tanousabf2add2019-01-22 16:40:12 -08001075 const int64_t* int64Value = std::get_if<int64_t>(&valueVariant);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001076
Ed Tanousabf2add2019-01-22 16:40:12 -08001077 const double* doubleValue = std::get_if<double>(&valueVariant);
Eddie James028f7eb2019-05-17 21:24:36 +00001078 const uint32_t* uValue = std::get_if<uint32_t>(&valueVariant);
Ed Tanous6f6d0d32018-10-12 11:16:43 -07001079 double temp = 0.0;
1080 if (int64Value != nullptr)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001081 {
Ed Tanous271584a2019-07-09 16:24:22 -07001082 temp = static_cast<double>(*int64Value);
Ed Tanous6f6d0d32018-10-12 11:16:43 -07001083 }
1084 else if (doubleValue != nullptr)
1085 {
1086 temp = *doubleValue;
1087 }
Eddie James028f7eb2019-05-17 21:24:36 +00001088 else if (uValue != nullptr)
1089 {
1090 temp = *uValue;
1091 }
Ed Tanous6f6d0d32018-10-12 11:16:43 -07001092 else
1093 {
1094 BMCWEB_LOG_ERROR
1095 << "Got value interface that wasn't int or double";
1096 continue;
1097 }
1098 temp = temp * std::pow(10, scaleMultiplier);
1099 if (forceToInt)
1100 {
Ed Tanous81ce6092020-12-17 16:54:55 +00001101 sensorJson[key] = static_cast<int64_t>(temp);
Ed Tanous6f6d0d32018-10-12 11:16:43 -07001102 }
1103 else
1104 {
Ed Tanous81ce6092020-12-17 16:54:55 +00001105 sensorJson[key] = temp;
Ed Tanous1abe55e2018-09-05 08:30:59 -07001106 }
1107 }
1108 }
1109 }
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02001110
Ed Tanous81ce6092020-12-17 16:54:55 +00001111 sensorsAsyncResp->addMetadata(sensorJson, unit.to_string(),
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02001112 "/xyz/openbmc_project/sensors/" + sensorType +
1113 "/" + sensorName);
1114
Ed Tanous1abe55e2018-09-05 08:30:59 -07001115 BMCWEB_LOG_DEBUG << "Added sensor " << sensorName;
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +01001116}
1117
Ed Tanousb5a76932020-09-29 16:16:58 -07001118inline void populateFanRedundancy(
1119 const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp)
James Feist8bd25cc2019-03-15 15:14:00 -07001120{
1121 crow::connections::systemBus->async_method_call(
1122 [sensorsAsyncResp](const boost::system::error_code ec,
1123 const GetSubTreeType& resp) {
1124 if (ec)
1125 {
1126 return; // don't have to have this interface
1127 }
Ed Tanouse278c182019-03-13 16:23:37 -07001128 for (const std::pair<std::string,
1129 std::vector<std::pair<
1130 std::string, std::vector<std::string>>>>&
1131 pathPair : resp)
James Feist8bd25cc2019-03-15 15:14:00 -07001132 {
Ed Tanouse278c182019-03-13 16:23:37 -07001133 const std::string& path = pathPair.first;
1134 const std::vector<
1135 std::pair<std::string, std::vector<std::string>>>& objDict =
1136 pathPair.second;
James Feist8bd25cc2019-03-15 15:14:00 -07001137 if (objDict.empty())
1138 {
1139 continue; // this should be impossible
1140 }
1141
1142 const std::string& owner = objDict.begin()->first;
Jonathan Doman1e1e5982021-06-11 09:36:17 -07001143 sdbusplus::asio::getProperty<std::vector<std::string>>(
1144 *crow::connections::systemBus,
1145 "xyz.openbmc_project.ObjectMapper", path + "/chassis",
1146 "xyz.openbmc_project.Association", "endpoints",
1147 [path, owner, sensorsAsyncResp](
1148 const boost::system::error_code e,
1149 const std::vector<std::string>& endpoints) {
Ed Tanous271584a2019-07-09 16:24:22 -07001150 if (e)
James Feist8bd25cc2019-03-15 15:14:00 -07001151 {
1152 return; // if they don't have an association we
1153 // can't tell what chassis is
1154 }
James Feist8bd25cc2019-03-15 15:14:00 -07001155 auto found = std::find_if(
Jonathan Doman1e1e5982021-06-11 09:36:17 -07001156 endpoints.begin(), endpoints.end(),
James Feist8bd25cc2019-03-15 15:14:00 -07001157 [sensorsAsyncResp](const std::string& entry) {
1158 return entry.find(
1159 sensorsAsyncResp->chassisId) !=
1160 std::string::npos;
1161 });
1162
Jonathan Doman1e1e5982021-06-11 09:36:17 -07001163 if (found == endpoints.end())
James Feist8bd25cc2019-03-15 15:14:00 -07001164 {
1165 return;
1166 }
1167 crow::connections::systemBus->async_method_call(
1168 [path, sensorsAsyncResp](
Ed Tanous271584a2019-07-09 16:24:22 -07001169 const boost::system::error_code& err,
James Feist8bd25cc2019-03-15 15:14:00 -07001170 const boost::container::flat_map<
1171 std::string,
Ed Tanous168e20c2021-12-13 14:39:53 -08001172 dbus::utility::DbusVariantType>& ret) {
Ed Tanous271584a2019-07-09 16:24:22 -07001173 if (err)
James Feist8bd25cc2019-03-15 15:14:00 -07001174 {
1175 return; // don't have to have this
1176 // interface
1177 }
1178 auto findFailures = ret.find("AllowedFailures");
1179 auto findCollection = ret.find("Collection");
1180 auto findStatus = ret.find("Status");
1181
1182 if (findFailures == ret.end() ||
1183 findCollection == ret.end() ||
1184 findStatus == ret.end())
1185 {
1186 BMCWEB_LOG_ERROR
1187 << "Invalid redundancy interface";
1188 messages::internalError(
zhanghch058d1b46d2021-04-01 11:18:24 +08001189 sensorsAsyncResp->asyncResp->res);
James Feist8bd25cc2019-03-15 15:14:00 -07001190 return;
1191 }
1192
Ed Tanous9eb808c2022-01-25 10:19:23 -08001193 const uint8_t* allowedFailures =
1194 std::get_if<uint8_t>(
1195 &(findFailures->second));
1196 const std::vector<std::string>* collection =
James Feist8bd25cc2019-03-15 15:14:00 -07001197 std::get_if<std::vector<std::string>>(
1198 &(findCollection->second));
Ed Tanous9eb808c2022-01-25 10:19:23 -08001199 const std::string* status =
1200 std::get_if<std::string>(
1201 &(findStatus->second));
James Feist8bd25cc2019-03-15 15:14:00 -07001202
1203 if (allowedFailures == nullptr ||
1204 collection == nullptr || status == nullptr)
1205 {
1206
1207 BMCWEB_LOG_ERROR
George Liu0fda0f12021-11-16 10:06:17 +08001208 << "Invalid redundancy interface types";
James Feist8bd25cc2019-03-15 15:14:00 -07001209 messages::internalError(
zhanghch058d1b46d2021-04-01 11:18:24 +08001210 sensorsAsyncResp->asyncResp->res);
James Feist8bd25cc2019-03-15 15:14:00 -07001211 return;
1212 }
George Liu28aa8de2021-02-01 15:13:30 +08001213 sdbusplus::message::object_path objectPath(
1214 path);
1215 std::string name = objectPath.filename();
1216 if (name.empty())
James Feist8bd25cc2019-03-15 15:14:00 -07001217 {
1218 // this should be impossible
1219 messages::internalError(
zhanghch058d1b46d2021-04-01 11:18:24 +08001220 sensorsAsyncResp->asyncResp->res);
James Feist8bd25cc2019-03-15 15:14:00 -07001221 return;
1222 }
James Feist8bd25cc2019-03-15 15:14:00 -07001223 std::replace(name.begin(), name.end(), '_',
1224 ' ');
1225
1226 std::string health;
1227
1228 if (boost::ends_with(*status, "Full"))
1229 {
1230 health = "OK";
1231 }
1232 else if (boost::ends_with(*status, "Degraded"))
1233 {
1234 health = "Warning";
1235 }
1236 else
1237 {
1238 health = "Critical";
1239 }
1240 std::vector<nlohmann::json> redfishCollection;
1241 const auto& fanRedfish =
zhanghch058d1b46d2021-04-01 11:18:24 +08001242 sensorsAsyncResp->asyncResp->res
1243 .jsonValue["Fans"];
James Feist8bd25cc2019-03-15 15:14:00 -07001244 for (const std::string& item : *collection)
1245 {
George Liu28aa8de2021-02-01 15:13:30 +08001246 sdbusplus::message::object_path path(item);
1247 std::string itemName = path.filename();
1248 if (itemName.empty())
1249 {
1250 continue;
1251 }
James Feist8bd25cc2019-03-15 15:14:00 -07001252 /*
1253 todo(ed): merge patch that fixes the names
1254 std::replace(itemName.begin(),
1255 itemName.end(), '_', ' ');*/
1256 auto schemaItem = std::find_if(
1257 fanRedfish.begin(), fanRedfish.end(),
1258 [itemName](const nlohmann::json& fan) {
1259 return fan["MemberId"] == itemName;
1260 });
1261 if (schemaItem != fanRedfish.end())
1262 {
1263 redfishCollection.push_back(
1264 {{"@odata.id",
1265 (*schemaItem)["@odata.id"]}});
1266 }
1267 else
1268 {
1269 BMCWEB_LOG_ERROR
1270 << "failed to find fan in schema";
1271 messages::internalError(
zhanghch058d1b46d2021-04-01 11:18:24 +08001272 sensorsAsyncResp->asyncResp->res);
James Feist8bd25cc2019-03-15 15:14:00 -07001273 return;
1274 }
1275 }
1276
Kuiying Wang3e9e72e2020-07-07 10:18:32 +08001277 size_t minNumNeeded =
Ed Tanous26f69762022-01-25 09:49:11 -08001278 collection->empty()
1279 ? 0
1280 : collection->size() - *allowedFailures;
Ed Tanous271584a2019-07-09 16:24:22 -07001281 nlohmann::json& jResp =
zhanghch058d1b46d2021-04-01 11:18:24 +08001282 sensorsAsyncResp->asyncResp->res
Ed Tanous271584a2019-07-09 16:24:22 -07001283 .jsonValue["Redundancy"];
1284 jResp.push_back(
James Feist8bd25cc2019-03-15 15:14:00 -07001285 {{"@odata.id",
AppaRao Puli717794d2019-10-18 22:54:53 +05301286 "/redfish/v1/Chassis/" +
James Feist8bd25cc2019-03-15 15:14:00 -07001287 sensorsAsyncResp->chassisId + "/" +
1288 sensorsAsyncResp->chassisSubNode +
1289 "#/Redundancy/" +
Ed Tanous271584a2019-07-09 16:24:22 -07001290 std::to_string(jResp.size())},
James Feist8bd25cc2019-03-15 15:14:00 -07001291 {"@odata.type",
1292 "#Redundancy.v1_3_2.Redundancy"},
Kuiying Wang3e9e72e2020-07-07 10:18:32 +08001293 {"MinNumNeeded", minNumNeeded},
James Feist8bd25cc2019-03-15 15:14:00 -07001294 {"MemberId", name},
1295 {"Mode", "N+m"},
1296 {"Name", name},
1297 {"RedundancySet", redfishCollection},
1298 {"Status",
1299 {{"Health", health},
1300 {"State", "Enabled"}}}});
1301 },
1302 owner, path, "org.freedesktop.DBus.Properties",
1303 "GetAll",
1304 "xyz.openbmc_project.Control.FanRedundancy");
Jonathan Doman1e1e5982021-06-11 09:36:17 -07001305 });
James Feist8bd25cc2019-03-15 15:14:00 -07001306 }
1307 },
1308 "xyz.openbmc_project.ObjectMapper",
1309 "/xyz/openbmc_project/object_mapper",
1310 "xyz.openbmc_project.ObjectMapper", "GetSubTree",
1311 "/xyz/openbmc_project/control", 2,
1312 std::array<const char*, 1>{
1313 "xyz.openbmc_project.Control.FanRedundancy"});
1314}
1315
Ed Tanousb5a76932020-09-29 16:16:58 -07001316inline void
Ed Tanous81ce6092020-12-17 16:54:55 +00001317 sortJSONResponse(const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07001318{
zhanghch058d1b46d2021-04-01 11:18:24 +08001319 nlohmann::json& response = sensorsAsyncResp->asyncResp->res.jsonValue;
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07001320 std::array<std::string, 2> sensorHeaders{"Temperatures", "Fans"};
Ed Tanous81ce6092020-12-17 16:54:55 +00001321 if (sensorsAsyncResp->chassisSubNode == sensors::node::power)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07001322 {
1323 sensorHeaders = {"Voltages", "PowerSupplies"};
1324 }
1325 for (const std::string& sensorGroup : sensorHeaders)
1326 {
1327 nlohmann::json::iterator entry = response.find(sensorGroup);
1328 if (entry != response.end())
1329 {
1330 std::sort(entry->begin(), entry->end(),
1331 [](nlohmann::json& c1, nlohmann::json& c2) {
1332 return c1["Name"] < c2["Name"];
1333 });
1334
1335 // add the index counts to the end of each entry
1336 size_t count = 0;
1337 for (nlohmann::json& sensorJson : *entry)
1338 {
1339 nlohmann::json::iterator odata = sensorJson.find("@odata.id");
1340 if (odata == sensorJson.end())
1341 {
1342 continue;
1343 }
1344 std::string* value = odata->get_ptr<std::string*>();
1345 if (value != nullptr)
1346 {
1347 *value += std::to_string(count);
1348 count++;
Ed Tanous81ce6092020-12-17 16:54:55 +00001349 sensorsAsyncResp->updateUri(sensorJson["Name"], *value);
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07001350 }
1351 }
1352 }
1353 }
1354}
1355
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +01001356/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001357 * @brief Finds the inventory item with the specified object path.
1358 * @param inventoryItems D-Bus inventory items associated with sensors.
1359 * @param invItemObjPath D-Bus object path of inventory item.
1360 * @return Inventory item within vector, or nullptr if no match found.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001361 */
Ed Tanous23a21a12020-07-25 04:45:05 +00001362inline InventoryItem* findInventoryItem(
Ed Tanousb5a76932020-09-29 16:16:58 -07001363 const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems,
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001364 const std::string& invItemObjPath)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001365{
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001366 for (InventoryItem& inventoryItem : *inventoryItems)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001367 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001368 if (inventoryItem.objectPath == invItemObjPath)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001369 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001370 return &inventoryItem;
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001371 }
1372 }
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001373 return nullptr;
1374}
1375
1376/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001377 * @brief Finds the inventory item associated with the specified sensor.
1378 * @param inventoryItems D-Bus inventory items associated with sensors.
1379 * @param sensorObjPath D-Bus object path of sensor.
1380 * @return Inventory item within vector, or nullptr if no match found.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001381 */
Ed Tanous23a21a12020-07-25 04:45:05 +00001382inline InventoryItem* findInventoryItemForSensor(
Ed Tanousb5a76932020-09-29 16:16:58 -07001383 const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems,
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001384 const std::string& sensorObjPath)
1385{
1386 for (InventoryItem& inventoryItem : *inventoryItems)
1387 {
1388 if (inventoryItem.sensors.count(sensorObjPath) > 0)
1389 {
1390 return &inventoryItem;
1391 }
1392 }
1393 return nullptr;
1394}
1395
1396/**
Anthony Wilsond5005492019-07-31 16:34:17 -05001397 * @brief Finds the inventory item associated with the specified led path.
1398 * @param inventoryItems D-Bus inventory items associated with sensors.
1399 * @param ledObjPath D-Bus object path of led.
1400 * @return Inventory item within vector, or nullptr if no match found.
1401 */
1402inline InventoryItem*
1403 findInventoryItemForLed(std::vector<InventoryItem>& inventoryItems,
1404 const std::string& ledObjPath)
1405{
1406 for (InventoryItem& inventoryItem : inventoryItems)
1407 {
1408 if (inventoryItem.ledObjectPath == ledObjPath)
1409 {
1410 return &inventoryItem;
1411 }
1412 }
1413 return nullptr;
1414}
1415
1416/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001417 * @brief Adds inventory item and associated sensor to specified vector.
1418 *
1419 * Adds a new InventoryItem to the vector if necessary. Searches for an
1420 * existing InventoryItem with the specified object path. If not found, one is
1421 * added to the vector.
1422 *
1423 * Next, the specified sensor is added to the set of sensors associated with the
1424 * InventoryItem.
1425 *
1426 * @param inventoryItems D-Bus inventory items associated with sensors.
1427 * @param invItemObjPath D-Bus object path of inventory item.
1428 * @param sensorObjPath D-Bus object path of sensor
1429 */
Ed Tanousb5a76932020-09-29 16:16:58 -07001430inline void addInventoryItem(
1431 const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems,
1432 const std::string& invItemObjPath, const std::string& sensorObjPath)
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001433{
1434 // Look for inventory item in vector
1435 InventoryItem* inventoryItem =
1436 findInventoryItem(inventoryItems, invItemObjPath);
1437
1438 // If inventory item doesn't exist in vector, add it
1439 if (inventoryItem == nullptr)
1440 {
1441 inventoryItems->emplace_back(invItemObjPath);
1442 inventoryItem = &(inventoryItems->back());
1443 }
1444
1445 // Add sensor to set of sensors associated with inventory item
1446 inventoryItem->sensors.emplace(sensorObjPath);
1447}
1448
1449/**
1450 * @brief Stores D-Bus data in the specified inventory item.
1451 *
1452 * Finds D-Bus data in the specified map of interfaces. Stores the data in the
1453 * specified InventoryItem.
1454 *
1455 * This data is later used to provide sensor property values in the JSON
1456 * response.
1457 *
1458 * @param inventoryItem Inventory item where data will be stored.
1459 * @param interfacesDict Map containing D-Bus interfaces and their properties
1460 * for the specified inventory item.
1461 */
Ed Tanous23a21a12020-07-25 04:45:05 +00001462inline void storeInventoryItemData(
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001463 InventoryItem& inventoryItem,
Ed Tanous711ac7a2021-12-20 09:34:41 -08001464 const dbus::utility::DBusInteracesMap& interfacesDict)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001465{
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001466 // Get properties from Inventory.Item interface
Ed Tanous711ac7a2021-12-20 09:34:41 -08001467
Ed Tanous9eb808c2022-01-25 10:19:23 -08001468 for (const auto& [interface, values] : interfacesDict)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001469 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001470 if (interface == "xyz.openbmc_project.Inventory.Item")
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001471 {
Ed Tanous9eb808c2022-01-25 10:19:23 -08001472 for (const auto& [name, dbusValue] : values)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001473 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001474 if (name == "Present")
1475 {
1476 const bool* value = std::get_if<bool>(&dbusValue);
1477 if (value != nullptr)
1478 {
1479 inventoryItem.isPresent = *value;
1480 }
1481 }
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001482 }
1483 }
Ed Tanous711ac7a2021-12-20 09:34:41 -08001484 // Check if Inventory.Item.PowerSupply interface is present
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001485
Ed Tanous711ac7a2021-12-20 09:34:41 -08001486 if (interface == "xyz.openbmc_project.Inventory.Item.PowerSupply")
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001487 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001488 inventoryItem.isPowerSupply = true;
1489 }
1490
1491 // Get properties from Inventory.Decorator.Asset interface
1492 if (interface == "xyz.openbmc_project.Inventory.Decorator.Asset")
1493 {
Ed Tanous9eb808c2022-01-25 10:19:23 -08001494 for (const auto& [name, dbusValue] : values)
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001495 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001496 if (name == "Manufacturer")
1497 {
1498 const std::string* value =
1499 std::get_if<std::string>(&dbusValue);
1500 if (value != nullptr)
1501 {
1502 inventoryItem.manufacturer = *value;
1503 }
1504 }
1505 if (name == "Model")
1506 {
1507 const std::string* value =
1508 std::get_if<std::string>(&dbusValue);
1509 if (value != nullptr)
1510 {
1511 inventoryItem.model = *value;
1512 }
1513 }
1514 if (name == "SerialNumber")
1515 {
1516 const std::string* value =
1517 std::get_if<std::string>(&dbusValue);
1518 if (value != nullptr)
1519 {
1520 inventoryItem.serialNumber = *value;
1521 }
1522 }
1523 if (name == "PartNumber")
1524 {
1525 const std::string* value =
1526 std::get_if<std::string>(&dbusValue);
1527 if (value != nullptr)
1528 {
1529 inventoryItem.partNumber = *value;
1530 }
1531 }
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001532 }
1533 }
1534
Ed Tanous711ac7a2021-12-20 09:34:41 -08001535 if (interface ==
1536 "xyz.openbmc_project.State.Decorator.OperationalStatus")
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001537 {
Ed Tanous9eb808c2022-01-25 10:19:23 -08001538 for (const auto& [name, dbusValue] : values)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001539 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001540 if (name == "Functional")
1541 {
1542 const bool* value = std::get_if<bool>(&dbusValue);
1543 if (value != nullptr)
1544 {
1545 inventoryItem.isFunctional = *value;
1546 }
1547 }
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001548 }
1549 }
1550 }
1551}
1552
1553/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001554 * @brief Gets D-Bus data for inventory items associated with sensors.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001555 *
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001556 * Uses the specified connections (services) to obtain D-Bus data for inventory
1557 * items associated with sensors. Stores the resulting data in the
1558 * inventoryItems vector.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001559 *
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001560 * This data is later used to provide sensor property values in the JSON
1561 * response.
1562 *
1563 * Finds the inventory item data asynchronously. Invokes callback when data has
1564 * been obtained.
1565 *
1566 * The callback must have the following signature:
1567 * @code
Anthony Wilsond5005492019-07-31 16:34:17 -05001568 * callback(void)
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001569 * @endcode
1570 *
1571 * This function is called recursively, obtaining data asynchronously from one
1572 * connection in each call. This ensures the callback is not invoked until the
1573 * last asynchronous function has completed.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001574 *
1575 * @param sensorsAsyncResp Pointer to object holding response data.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001576 * @param inventoryItems D-Bus inventory items associated with sensors.
1577 * @param invConnections Connections that provide data for the inventory items.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001578 * @param objectMgrPaths Mappings from connection name to DBus object path that
1579 * implements ObjectManager.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001580 * @param callback Callback to invoke when inventory data has been obtained.
1581 * @param invConnectionsIndex Current index in invConnections. Only specified
1582 * in recursive calls to this function.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001583 */
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001584template <typename Callback>
1585static void getInventoryItemsData(
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001586 std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001587 std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001588 std::shared_ptr<boost::container::flat_set<std::string>> invConnections,
1589 std::shared_ptr<boost::container::flat_map<std::string, std::string>>
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001590 objectMgrPaths,
Ed Tanous271584a2019-07-09 16:24:22 -07001591 Callback&& callback, size_t invConnectionsIndex = 0)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001592{
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001593 BMCWEB_LOG_DEBUG << "getInventoryItemsData enter";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001594
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001595 // If no more connections left, call callback
1596 if (invConnectionsIndex >= invConnections->size())
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001597 {
Anthony Wilsond5005492019-07-31 16:34:17 -05001598 callback();
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001599 BMCWEB_LOG_DEBUG << "getInventoryItemsData exit";
1600 return;
1601 }
1602
1603 // Get inventory item data from current connection
1604 auto it = invConnections->nth(invConnectionsIndex);
1605 if (it != invConnections->end())
1606 {
1607 const std::string& invConnection = *it;
1608
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001609 // Response handler for GetManagedObjects
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001610 auto respHandler = [sensorsAsyncResp, inventoryItems, invConnections,
Ed Tanousf94c4ec2022-01-06 12:44:41 -08001611 objectMgrPaths,
1612 callback{std::forward<Callback>(callback)},
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001613 invConnectionsIndex](
1614 const boost::system::error_code ec,
Ed Tanous711ac7a2021-12-20 09:34:41 -08001615 dbus::utility::ManagedObjectType& resp) {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001616 BMCWEB_LOG_DEBUG << "getInventoryItemsData respHandler enter";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001617 if (ec)
1618 {
1619 BMCWEB_LOG_ERROR
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001620 << "getInventoryItemsData respHandler DBus error " << ec;
zhanghch058d1b46d2021-04-01 11:18:24 +08001621 messages::internalError(sensorsAsyncResp->asyncResp->res);
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001622 return;
1623 }
1624
1625 // Loop through returned object paths
1626 for (const auto& objDictEntry : resp)
1627 {
1628 const std::string& objPath =
1629 static_cast<const std::string&>(objDictEntry.first);
1630
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001631 // If this object path is one of the specified inventory items
1632 InventoryItem* inventoryItem =
1633 findInventoryItem(inventoryItems, objPath);
1634 if (inventoryItem != nullptr)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001635 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001636 // Store inventory data in InventoryItem
1637 storeInventoryItemData(*inventoryItem, objDictEntry.second);
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001638 }
1639 }
1640
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001641 // Recurse to get inventory item data from next connection
1642 getInventoryItemsData(sensorsAsyncResp, inventoryItems,
1643 invConnections, objectMgrPaths,
1644 std::move(callback), invConnectionsIndex + 1);
1645
1646 BMCWEB_LOG_DEBUG << "getInventoryItemsData respHandler exit";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001647 };
1648
1649 // Find DBus object path that implements ObjectManager for the current
1650 // connection. If no mapping found, default to "/".
1651 auto iter = objectMgrPaths->find(invConnection);
1652 const std::string& objectMgrPath =
1653 (iter != objectMgrPaths->end()) ? iter->second : "/";
1654 BMCWEB_LOG_DEBUG << "ObjectManager path for " << invConnection << " is "
1655 << objectMgrPath;
1656
1657 // Get all object paths and their interfaces for current connection
1658 crow::connections::systemBus->async_method_call(
1659 std::move(respHandler), invConnection, objectMgrPath,
1660 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
1661 }
1662
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001663 BMCWEB_LOG_DEBUG << "getInventoryItemsData exit";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001664}
1665
1666/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001667 * @brief Gets connections that provide D-Bus data for inventory items.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001668 *
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001669 * Gets the D-Bus connections (services) that provide data for the inventory
1670 * items that are associated with sensors.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001671 *
1672 * Finds the connections asynchronously. Invokes callback when information has
1673 * been obtained.
1674 *
1675 * The callback must have the following signature:
1676 * @code
1677 * callback(std::shared_ptr<boost::container::flat_set<std::string>>
1678 * invConnections)
1679 * @endcode
1680 *
1681 * @param sensorsAsyncResp Pointer to object holding response data.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001682 * @param inventoryItems D-Bus inventory items associated with sensors.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001683 * @param callback Callback to invoke when connections have been obtained.
1684 */
1685template <typename Callback>
1686static void getInventoryItemsConnections(
Ed Tanousb5a76932020-09-29 16:16:58 -07001687 const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
1688 const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems,
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001689 Callback&& callback)
1690{
1691 BMCWEB_LOG_DEBUG << "getInventoryItemsConnections enter";
1692
1693 const std::string path = "/xyz/openbmc_project/inventory";
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001694 const std::array<std::string, 4> interfaces = {
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001695 "xyz.openbmc_project.Inventory.Item",
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001696 "xyz.openbmc_project.Inventory.Item.PowerSupply",
1697 "xyz.openbmc_project.Inventory.Decorator.Asset",
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001698 "xyz.openbmc_project.State.Decorator.OperationalStatus"};
1699
1700 // Response handler for parsing output from GetSubTree
Ed Tanousf94c4ec2022-01-06 12:44:41 -08001701 auto respHandler = [callback{std::forward<Callback>(callback)},
1702 sensorsAsyncResp,
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001703 inventoryItems](const boost::system::error_code ec,
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001704 const GetSubTreeType& subtree) {
1705 BMCWEB_LOG_DEBUG << "getInventoryItemsConnections respHandler enter";
1706 if (ec)
1707 {
zhanghch058d1b46d2021-04-01 11:18:24 +08001708 messages::internalError(sensorsAsyncResp->asyncResp->res);
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001709 BMCWEB_LOG_ERROR
1710 << "getInventoryItemsConnections respHandler DBus error " << ec;
1711 return;
1712 }
1713
1714 // Make unique list of connections for desired inventory items
1715 std::shared_ptr<boost::container::flat_set<std::string>>
1716 invConnections =
1717 std::make_shared<boost::container::flat_set<std::string>>();
1718 invConnections->reserve(8);
1719
1720 // Loop through objects from GetSubTree
1721 for (const std::pair<
1722 std::string,
1723 std::vector<std::pair<std::string, std::vector<std::string>>>>&
1724 object : subtree)
1725 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001726 // Check if object path is one of the specified inventory items
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001727 const std::string& objPath = object.first;
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001728 if (findInventoryItem(inventoryItems, objPath) != nullptr)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001729 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001730 // Store all connections to inventory item
1731 for (const std::pair<std::string, std::vector<std::string>>&
1732 objData : object.second)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001733 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001734 const std::string& invConnection = objData.first;
1735 invConnections->insert(invConnection);
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001736 }
1737 }
1738 }
Anthony Wilsond5005492019-07-31 16:34:17 -05001739
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001740 callback(invConnections);
1741 BMCWEB_LOG_DEBUG << "getInventoryItemsConnections respHandler exit";
1742 };
1743
1744 // Make call to ObjectMapper to find all inventory items
1745 crow::connections::systemBus->async_method_call(
1746 std::move(respHandler), "xyz.openbmc_project.ObjectMapper",
1747 "/xyz/openbmc_project/object_mapper",
1748 "xyz.openbmc_project.ObjectMapper", "GetSubTree", path, 0, interfaces);
1749 BMCWEB_LOG_DEBUG << "getInventoryItemsConnections exit";
1750}
1751
1752/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001753 * @brief Gets associations from sensors to inventory items.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001754 *
1755 * Looks for ObjectMapper associations from the specified sensors to related
Anthony Wilsond5005492019-07-31 16:34:17 -05001756 * inventory items. Then finds the associations from those inventory items to
1757 * their LEDs, if any.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001758 *
1759 * Finds the inventory items asynchronously. Invokes callback when information
1760 * has been obtained.
1761 *
1762 * The callback must have the following signature:
1763 * @code
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001764 * callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001765 * @endcode
1766 *
1767 * @param sensorsAsyncResp Pointer to object holding response data.
1768 * @param sensorNames All sensors within the current chassis.
1769 * @param objectMgrPaths Mappings from connection name to DBus object path that
1770 * implements ObjectManager.
1771 * @param callback Callback to invoke when inventory items have been obtained.
1772 */
1773template <typename Callback>
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001774static void getInventoryItemAssociations(
Ed Tanousb5a76932020-09-29 16:16:58 -07001775 const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
1776 const std::shared_ptr<boost::container::flat_set<std::string>>& sensorNames,
1777 const std::shared_ptr<boost::container::flat_map<std::string, std::string>>&
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001778 objectMgrPaths,
1779 Callback&& callback)
1780{
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001781 BMCWEB_LOG_DEBUG << "getInventoryItemAssociations enter";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001782
1783 // Response handler for GetManagedObjects
Ed Tanousf94c4ec2022-01-06 12:44:41 -08001784 auto respHandler = [callback{std::forward<Callback>(callback)},
1785 sensorsAsyncResp,
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001786 sensorNames](const boost::system::error_code ec,
1787 dbus::utility::ManagedObjectType& resp) {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001788 BMCWEB_LOG_DEBUG << "getInventoryItemAssociations respHandler enter";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001789 if (ec)
1790 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001791 BMCWEB_LOG_ERROR
1792 << "getInventoryItemAssociations respHandler DBus error " << ec;
zhanghch058d1b46d2021-04-01 11:18:24 +08001793 messages::internalError(sensorsAsyncResp->asyncResp->res);
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001794 return;
1795 }
1796
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001797 // Create vector to hold list of inventory items
1798 std::shared_ptr<std::vector<InventoryItem>> inventoryItems =
1799 std::make_shared<std::vector<InventoryItem>>();
1800
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001801 // Loop through returned object paths
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001802 std::string sensorAssocPath;
1803 sensorAssocPath.reserve(128); // avoid memory allocations
1804 for (const auto& objDictEntry : resp)
1805 {
1806 const std::string& objPath =
1807 static_cast<const std::string&>(objDictEntry.first);
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001808
1809 // If path is inventory association for one of the specified sensors
1810 for (const std::string& sensorName : *sensorNames)
1811 {
1812 sensorAssocPath = sensorName;
1813 sensorAssocPath += "/inventory";
1814 if (objPath == sensorAssocPath)
1815 {
1816 // Get Association interface for object path
Ed Tanous711ac7a2021-12-20 09:34:41 -08001817 for (const auto& [interface, values] : objDictEntry.second)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001818 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001819 if (interface == "xyz.openbmc_project.Association")
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001820 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001821 for (const auto& [valueName, value] : values)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001822 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001823 if (valueName == "endpoints")
1824 {
1825 const std::vector<std::string>* endpoints =
1826 std::get_if<std::vector<std::string>>(
1827 &value);
1828 if ((endpoints != nullptr) &&
1829 !endpoints->empty())
1830 {
1831 // Add inventory item to vector
1832 const std::string& invItemPath =
1833 endpoints->front();
1834 addInventoryItem(inventoryItems,
1835 invItemPath,
1836 sensorName);
1837 }
1838 }
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001839 }
1840 }
1841 }
1842 break;
1843 }
1844 }
1845 }
1846
Anthony Wilsond5005492019-07-31 16:34:17 -05001847 // Now loop through the returned object paths again, this time to
1848 // find the leds associated with the inventory items we just found
1849 std::string inventoryAssocPath;
1850 inventoryAssocPath.reserve(128); // avoid memory allocations
1851 for (const auto& objDictEntry : resp)
1852 {
1853 const std::string& objPath =
1854 static_cast<const std::string&>(objDictEntry.first);
Anthony Wilsond5005492019-07-31 16:34:17 -05001855
1856 for (InventoryItem& inventoryItem : *inventoryItems)
1857 {
1858 inventoryAssocPath = inventoryItem.objectPath;
1859 inventoryAssocPath += "/leds";
1860 if (objPath == inventoryAssocPath)
1861 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001862 for (const auto& [interface, values] : objDictEntry.second)
Anthony Wilsond5005492019-07-31 16:34:17 -05001863 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001864 if (interface == "xyz.openbmc_project.Association")
Anthony Wilsond5005492019-07-31 16:34:17 -05001865 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001866 for (const auto& [valueName, value] : values)
Anthony Wilsond5005492019-07-31 16:34:17 -05001867 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001868 if (valueName == "endpoints")
1869 {
1870 const std::vector<std::string>* endpoints =
1871 std::get_if<std::vector<std::string>>(
1872 &value);
1873 if ((endpoints != nullptr) &&
1874 !endpoints->empty())
1875 {
1876 // Add inventory item to vector
1877 // Store LED path in inventory item
1878 const std::string& ledPath =
1879 endpoints->front();
1880 inventoryItem.ledObjectPath = ledPath;
1881 }
1882 }
Anthony Wilsond5005492019-07-31 16:34:17 -05001883 }
1884 }
1885 }
Ed Tanous711ac7a2021-12-20 09:34:41 -08001886
Anthony Wilsond5005492019-07-31 16:34:17 -05001887 break;
1888 }
1889 }
1890 }
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001891 callback(inventoryItems);
1892 BMCWEB_LOG_DEBUG << "getInventoryItemAssociations respHandler exit";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001893 };
1894
1895 // Find DBus object path that implements ObjectManager for ObjectMapper
1896 std::string connection = "xyz.openbmc_project.ObjectMapper";
1897 auto iter = objectMgrPaths->find(connection);
1898 const std::string& objectMgrPath =
1899 (iter != objectMgrPaths->end()) ? iter->second : "/";
1900 BMCWEB_LOG_DEBUG << "ObjectManager path for " << connection << " is "
1901 << objectMgrPath;
1902
1903 // Call GetManagedObjects on the ObjectMapper to get all associations
1904 crow::connections::systemBus->async_method_call(
1905 std::move(respHandler), connection, objectMgrPath,
1906 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
1907
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001908 BMCWEB_LOG_DEBUG << "getInventoryItemAssociations exit";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001909}
1910
1911/**
Anthony Wilsond5005492019-07-31 16:34:17 -05001912 * @brief Gets D-Bus data for inventory item leds associated with sensors.
1913 *
1914 * Uses the specified connections (services) to obtain D-Bus data for inventory
1915 * item leds associated with sensors. Stores the resulting data in the
1916 * inventoryItems vector.
1917 *
1918 * This data is later used to provide sensor property values in the JSON
1919 * response.
1920 *
1921 * Finds the inventory item led data asynchronously. Invokes callback when data
1922 * has been obtained.
1923 *
1924 * The callback must have the following signature:
1925 * @code
Gunnar Mills42cbe532019-08-15 15:26:54 -05001926 * callback()
Anthony Wilsond5005492019-07-31 16:34:17 -05001927 * @endcode
1928 *
1929 * This function is called recursively, obtaining data asynchronously from one
1930 * connection in each call. This ensures the callback is not invoked until the
1931 * last asynchronous function has completed.
1932 *
1933 * @param sensorsAsyncResp Pointer to object holding response data.
1934 * @param inventoryItems D-Bus inventory items associated with sensors.
1935 * @param ledConnections Connections that provide data for the inventory leds.
1936 * @param callback Callback to invoke when inventory data has been obtained.
1937 * @param ledConnectionsIndex Current index in ledConnections. Only specified
1938 * in recursive calls to this function.
1939 */
1940template <typename Callback>
1941void getInventoryLedData(
1942 std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
1943 std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
1944 std::shared_ptr<boost::container::flat_map<std::string, std::string>>
1945 ledConnections,
1946 Callback&& callback, size_t ledConnectionsIndex = 0)
1947{
1948 BMCWEB_LOG_DEBUG << "getInventoryLedData enter";
1949
1950 // If no more connections left, call callback
1951 if (ledConnectionsIndex >= ledConnections->size())
1952 {
Gunnar Mills42cbe532019-08-15 15:26:54 -05001953 callback();
Anthony Wilsond5005492019-07-31 16:34:17 -05001954 BMCWEB_LOG_DEBUG << "getInventoryLedData exit";
1955 return;
1956 }
1957
1958 // Get inventory item data from current connection
1959 auto it = ledConnections->nth(ledConnectionsIndex);
1960 if (it != ledConnections->end())
1961 {
1962 const std::string& ledPath = (*it).first;
1963 const std::string& ledConnection = (*it).second;
1964 // Response handler for Get State property
Jonathan Doman1e1e5982021-06-11 09:36:17 -07001965 auto respHandler =
1966 [sensorsAsyncResp, inventoryItems, ledConnections, ledPath,
Ed Tanousf94c4ec2022-01-06 12:44:41 -08001967 callback{std::forward<Callback>(callback)}, ledConnectionsIndex](
Jonathan Doman1e1e5982021-06-11 09:36:17 -07001968 const boost::system::error_code ec, const std::string& state) {
1969 BMCWEB_LOG_DEBUG << "getInventoryLedData respHandler enter";
1970 if (ec)
1971 {
1972 BMCWEB_LOG_ERROR
1973 << "getInventoryLedData respHandler DBus error " << ec;
1974 messages::internalError(sensorsAsyncResp->asyncResp->res);
1975 return;
1976 }
Anthony Wilsond5005492019-07-31 16:34:17 -05001977
Jonathan Doman1e1e5982021-06-11 09:36:17 -07001978 BMCWEB_LOG_DEBUG << "Led state: " << state;
Ed Tanous168e20c2021-12-13 14:39:53 -08001979 // Find inventory item with this LED object path
1980 InventoryItem* inventoryItem =
1981 findInventoryItemForLed(*inventoryItems, ledPath);
1982 if (inventoryItem != nullptr)
Anthony Wilsond5005492019-07-31 16:34:17 -05001983 {
Ed Tanous168e20c2021-12-13 14:39:53 -08001984 // Store LED state in InventoryItem
Jonathan Doman1e1e5982021-06-11 09:36:17 -07001985 if (boost::ends_with(state, "On"))
Anthony Wilsond5005492019-07-31 16:34:17 -05001986 {
Ed Tanous168e20c2021-12-13 14:39:53 -08001987 inventoryItem->ledState = LedState::ON;
1988 }
Jonathan Doman1e1e5982021-06-11 09:36:17 -07001989 else if (boost::ends_with(state, "Blink"))
Ed Tanous168e20c2021-12-13 14:39:53 -08001990 {
1991 inventoryItem->ledState = LedState::BLINK;
1992 }
Jonathan Doman1e1e5982021-06-11 09:36:17 -07001993 else if (boost::ends_with(state, "Off"))
Ed Tanous168e20c2021-12-13 14:39:53 -08001994 {
1995 inventoryItem->ledState = LedState::OFF;
1996 }
1997 else
1998 {
1999 inventoryItem->ledState = LedState::UNKNOWN;
Anthony Wilsond5005492019-07-31 16:34:17 -05002000 }
2001 }
Anthony Wilsond5005492019-07-31 16:34:17 -05002002
Jonathan Doman1e1e5982021-06-11 09:36:17 -07002003 // Recurse to get LED data from next connection
2004 getInventoryLedData(sensorsAsyncResp, inventoryItems,
2005 ledConnections, std::move(callback),
2006 ledConnectionsIndex + 1);
Anthony Wilsond5005492019-07-31 16:34:17 -05002007
Jonathan Doman1e1e5982021-06-11 09:36:17 -07002008 BMCWEB_LOG_DEBUG << "getInventoryLedData respHandler exit";
2009 };
Anthony Wilsond5005492019-07-31 16:34:17 -05002010
2011 // Get the State property for the current LED
Jonathan Doman1e1e5982021-06-11 09:36:17 -07002012 sdbusplus::asio::getProperty<std::string>(
2013 *crow::connections::systemBus, ledConnection, ledPath,
2014 "xyz.openbmc_project.Led.Physical", "State",
2015 std::move(respHandler));
Anthony Wilsond5005492019-07-31 16:34:17 -05002016 }
2017
2018 BMCWEB_LOG_DEBUG << "getInventoryLedData exit";
2019}
2020
2021/**
2022 * @brief Gets LED data for LEDs associated with given inventory items.
2023 *
2024 * Gets the D-Bus connections (services) that provide LED data for the LEDs
2025 * associated with the specified inventory items. Then gets the LED data from
2026 * each connection and stores it in the inventory item.
2027 *
2028 * This data is later used to provide sensor property values in the JSON
2029 * response.
2030 *
2031 * Finds the LED data asynchronously. Invokes callback when information has
2032 * been obtained.
2033 *
2034 * The callback must have the following signature:
2035 * @code
Gunnar Mills42cbe532019-08-15 15:26:54 -05002036 * callback()
Anthony Wilsond5005492019-07-31 16:34:17 -05002037 * @endcode
2038 *
2039 * @param sensorsAsyncResp Pointer to object holding response data.
2040 * @param inventoryItems D-Bus inventory items associated with sensors.
2041 * @param callback Callback to invoke when inventory items have been obtained.
2042 */
2043template <typename Callback>
2044void getInventoryLeds(
2045 std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
2046 std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
2047 Callback&& callback)
2048{
2049 BMCWEB_LOG_DEBUG << "getInventoryLeds enter";
2050
2051 const std::string path = "/xyz/openbmc_project";
2052 const std::array<std::string, 1> interfaces = {
2053 "xyz.openbmc_project.Led.Physical"};
2054
2055 // Response handler for parsing output from GetSubTree
Ed Tanousf94c4ec2022-01-06 12:44:41 -08002056 auto respHandler = [callback{std::forward<Callback>(callback)},
2057 sensorsAsyncResp,
Anthony Wilsond5005492019-07-31 16:34:17 -05002058 inventoryItems](const boost::system::error_code ec,
2059 const GetSubTreeType& subtree) {
2060 BMCWEB_LOG_DEBUG << "getInventoryLeds respHandler enter";
2061 if (ec)
2062 {
zhanghch058d1b46d2021-04-01 11:18:24 +08002063 messages::internalError(sensorsAsyncResp->asyncResp->res);
Anthony Wilsond5005492019-07-31 16:34:17 -05002064 BMCWEB_LOG_ERROR << "getInventoryLeds respHandler DBus error "
2065 << ec;
2066 return;
2067 }
2068
2069 // Build map of LED object paths to connections
2070 std::shared_ptr<boost::container::flat_map<std::string, std::string>>
2071 ledConnections = std::make_shared<
2072 boost::container::flat_map<std::string, std::string>>();
2073
2074 // Loop through objects from GetSubTree
2075 for (const std::pair<
2076 std::string,
2077 std::vector<std::pair<std::string, std::vector<std::string>>>>&
2078 object : subtree)
2079 {
2080 // Check if object path is LED for one of the specified inventory
2081 // items
2082 const std::string& ledPath = object.first;
2083 if (findInventoryItemForLed(*inventoryItems, ledPath) != nullptr)
2084 {
2085 // Add mapping from ledPath to connection
2086 const std::string& connection = object.second.begin()->first;
2087 (*ledConnections)[ledPath] = connection;
2088 BMCWEB_LOG_DEBUG << "Added mapping " << ledPath << " -> "
2089 << connection;
2090 }
2091 }
2092
2093 getInventoryLedData(sensorsAsyncResp, inventoryItems, ledConnections,
2094 std::move(callback));
2095 BMCWEB_LOG_DEBUG << "getInventoryLeds respHandler exit";
2096 };
2097 // Make call to ObjectMapper to find all inventory items
2098 crow::connections::systemBus->async_method_call(
2099 std::move(respHandler), "xyz.openbmc_project.ObjectMapper",
2100 "/xyz/openbmc_project/object_mapper",
2101 "xyz.openbmc_project.ObjectMapper", "GetSubTree", path, 0, interfaces);
2102 BMCWEB_LOG_DEBUG << "getInventoryLeds exit";
2103}
2104
2105/**
Gunnar Mills42cbe532019-08-15 15:26:54 -05002106 * @brief Gets D-Bus data for Power Supply Attributes such as EfficiencyPercent
2107 *
2108 * Uses the specified connections (services) (currently assumes just one) to
2109 * obtain D-Bus data for Power Supply Attributes. Stores the resulting data in
2110 * the inventoryItems vector. Only stores data in Power Supply inventoryItems.
2111 *
2112 * This data is later used to provide sensor property values in the JSON
2113 * response.
2114 *
2115 * Finds the Power Supply Attributes data asynchronously. Invokes callback
2116 * when data has been obtained.
2117 *
2118 * The callback must have the following signature:
2119 * @code
2120 * callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems)
2121 * @endcode
2122 *
2123 * @param sensorsAsyncResp Pointer to object holding response data.
2124 * @param inventoryItems D-Bus inventory items associated with sensors.
2125 * @param psAttributesConnections Connections that provide data for the Power
2126 * Supply Attributes
2127 * @param callback Callback to invoke when data has been obtained.
2128 */
2129template <typename Callback>
2130void getPowerSupplyAttributesData(
Ed Tanousb5a76932020-09-29 16:16:58 -07002131 const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
Gunnar Mills42cbe532019-08-15 15:26:54 -05002132 std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
2133 const boost::container::flat_map<std::string, std::string>&
2134 psAttributesConnections,
2135 Callback&& callback)
2136{
2137 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributesData enter";
2138
2139 if (psAttributesConnections.empty())
2140 {
2141 BMCWEB_LOG_DEBUG << "Can't find PowerSupplyAttributes, no connections!";
2142 callback(inventoryItems);
2143 return;
2144 }
2145
2146 // Assuming just one connection (service) for now
2147 auto it = psAttributesConnections.nth(0);
2148
2149 const std::string& psAttributesPath = (*it).first;
2150 const std::string& psAttributesConnection = (*it).second;
2151
2152 // Response handler for Get DeratingFactor property
2153 auto respHandler = [sensorsAsyncResp, inventoryItems,
Ed Tanousf94c4ec2022-01-06 12:44:41 -08002154 callback{std::forward<Callback>(callback)}](
Gunnar Mills42cbe532019-08-15 15:26:54 -05002155 const boost::system::error_code ec,
Jonathan Doman1e1e5982021-06-11 09:36:17 -07002156 const uint32_t value) {
Gunnar Mills42cbe532019-08-15 15:26:54 -05002157 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributesData respHandler enter";
2158 if (ec)
2159 {
2160 BMCWEB_LOG_ERROR
2161 << "getPowerSupplyAttributesData respHandler DBus error " << ec;
zhanghch058d1b46d2021-04-01 11:18:24 +08002162 messages::internalError(sensorsAsyncResp->asyncResp->res);
Gunnar Mills42cbe532019-08-15 15:26:54 -05002163 return;
2164 }
2165
Jonathan Doman1e1e5982021-06-11 09:36:17 -07002166 BMCWEB_LOG_DEBUG << "PS EfficiencyPercent value: " << value;
2167 // Store value in Power Supply Inventory Items
2168 for (InventoryItem& inventoryItem : *inventoryItems)
Gunnar Mills42cbe532019-08-15 15:26:54 -05002169 {
Ed Tanous55f79e62022-01-25 11:26:16 -08002170 if (inventoryItem.isPowerSupply)
Gunnar Mills42cbe532019-08-15 15:26:54 -05002171 {
Jonathan Doman1e1e5982021-06-11 09:36:17 -07002172 inventoryItem.powerSupplyEfficiencyPercent =
2173 static_cast<int>(value);
Gunnar Mills42cbe532019-08-15 15:26:54 -05002174 }
2175 }
Gunnar Mills42cbe532019-08-15 15:26:54 -05002176
2177 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributesData respHandler exit";
2178 callback(inventoryItems);
2179 };
2180
2181 // Get the DeratingFactor property for the PowerSupplyAttributes
2182 // Currently only property on the interface/only one we care about
Jonathan Doman1e1e5982021-06-11 09:36:17 -07002183 sdbusplus::asio::getProperty<uint32_t>(
2184 *crow::connections::systemBus, psAttributesConnection, psAttributesPath,
2185 "xyz.openbmc_project.Control.PowerSupplyAttributes", "DeratingFactor",
2186 std::move(respHandler));
Gunnar Mills42cbe532019-08-15 15:26:54 -05002187
2188 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributesData exit";
2189}
2190
2191/**
2192 * @brief Gets the Power Supply Attributes such as EfficiencyPercent
2193 *
2194 * Gets the D-Bus connection (service) that provides Power Supply Attributes
2195 * data. Then gets the Power Supply Attributes data from the connection
2196 * (currently just assumes 1 connection) and stores the data in the inventory
2197 * item.
2198 *
2199 * This data is later used to provide sensor property values in the JSON
2200 * response. DeratingFactor on D-Bus is mapped to EfficiencyPercent on Redfish.
2201 *
2202 * Finds the Power Supply Attributes data asynchronously. Invokes callback
2203 * when information has been obtained.
2204 *
2205 * The callback must have the following signature:
2206 * @code
2207 * callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems)
2208 * @endcode
2209 *
2210 * @param sensorsAsyncResp Pointer to object holding response data.
2211 * @param inventoryItems D-Bus inventory items associated with sensors.
2212 * @param callback Callback to invoke when data has been obtained.
2213 */
2214template <typename Callback>
2215void getPowerSupplyAttributes(
2216 std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
2217 std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
2218 Callback&& callback)
2219{
2220 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes enter";
2221
2222 // Only need the power supply attributes when the Power Schema
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02002223 if (sensorsAsyncResp->chassisSubNode != sensors::node::power)
Gunnar Mills42cbe532019-08-15 15:26:54 -05002224 {
2225 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes exit since not Power";
2226 callback(inventoryItems);
2227 return;
2228 }
2229
2230 const std::array<std::string, 1> interfaces = {
2231 "xyz.openbmc_project.Control.PowerSupplyAttributes"};
2232
2233 // Response handler for parsing output from GetSubTree
Ed Tanousf94c4ec2022-01-06 12:44:41 -08002234 auto respHandler = [callback{std::forward<Callback>(callback)},
2235 sensorsAsyncResp,
Gunnar Mills42cbe532019-08-15 15:26:54 -05002236 inventoryItems](const boost::system::error_code ec,
2237 const GetSubTreeType& subtree) {
2238 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes respHandler enter";
2239 if (ec)
2240 {
zhanghch058d1b46d2021-04-01 11:18:24 +08002241 messages::internalError(sensorsAsyncResp->asyncResp->res);
Gunnar Mills42cbe532019-08-15 15:26:54 -05002242 BMCWEB_LOG_ERROR
2243 << "getPowerSupplyAttributes respHandler DBus error " << ec;
2244 return;
2245 }
Ed Tanous26f69762022-01-25 09:49:11 -08002246 if (subtree.empty())
Gunnar Mills42cbe532019-08-15 15:26:54 -05002247 {
2248 BMCWEB_LOG_DEBUG << "Can't find Power Supply Attributes!";
2249 callback(inventoryItems);
2250 return;
2251 }
2252
2253 // Currently we only support 1 power supply attribute, use this for
2254 // all the power supplies. Build map of object path to connection.
2255 // Assume just 1 connection and 1 path for now.
2256 boost::container::flat_map<std::string, std::string>
2257 psAttributesConnections;
2258
2259 if (subtree[0].first.empty() || subtree[0].second.empty())
2260 {
2261 BMCWEB_LOG_DEBUG << "Power Supply Attributes mapper error!";
2262 callback(inventoryItems);
2263 return;
2264 }
2265
2266 const std::string& psAttributesPath = subtree[0].first;
2267 const std::string& connection = subtree[0].second.begin()->first;
2268
2269 if (connection.empty())
2270 {
2271 BMCWEB_LOG_DEBUG << "Power Supply Attributes mapper error!";
2272 callback(inventoryItems);
2273 return;
2274 }
2275
2276 psAttributesConnections[psAttributesPath] = connection;
2277 BMCWEB_LOG_DEBUG << "Added mapping " << psAttributesPath << " -> "
2278 << connection;
2279
2280 getPowerSupplyAttributesData(sensorsAsyncResp, inventoryItems,
2281 psAttributesConnections,
2282 std::move(callback));
2283 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes respHandler exit";
2284 };
2285 // Make call to ObjectMapper to find the PowerSupplyAttributes service
2286 crow::connections::systemBus->async_method_call(
2287 std::move(respHandler), "xyz.openbmc_project.ObjectMapper",
2288 "/xyz/openbmc_project/object_mapper",
2289 "xyz.openbmc_project.ObjectMapper", "GetSubTree",
2290 "/xyz/openbmc_project", 0, interfaces);
2291 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes exit";
2292}
2293
2294/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002295 * @brief Gets inventory items associated with sensors.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002296 *
2297 * Finds the inventory items that are associated with the specified sensors.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002298 * Then gets D-Bus data for the inventory items, such as presence and VPD.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002299 *
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002300 * This data is later used to provide sensor property values in the JSON
2301 * response.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002302 *
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002303 * Finds the inventory items asynchronously. Invokes callback when the
2304 * inventory items have been obtained.
2305 *
2306 * The callback must have the following signature:
2307 * @code
2308 * callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems)
2309 * @endcode
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002310 *
2311 * @param sensorsAsyncResp Pointer to object holding response data.
2312 * @param sensorNames All sensors within the current chassis.
2313 * @param objectMgrPaths Mappings from connection name to DBus object path that
2314 * implements ObjectManager.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002315 * @param callback Callback to invoke when inventory items have been obtained.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002316 */
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002317template <typename Callback>
2318static void getInventoryItems(
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002319 std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
2320 const std::shared_ptr<boost::container::flat_set<std::string>> sensorNames,
2321 std::shared_ptr<boost::container::flat_map<std::string, std::string>>
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002322 objectMgrPaths,
2323 Callback&& callback)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002324{
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002325 BMCWEB_LOG_DEBUG << "getInventoryItems enter";
2326 auto getInventoryItemAssociationsCb =
Ed Tanousf94c4ec2022-01-06 12:44:41 -08002327 [sensorsAsyncResp, objectMgrPaths,
2328 callback{std::forward<Callback>(callback)}](
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002329 std::shared_ptr<std::vector<InventoryItem>> inventoryItems) {
2330 BMCWEB_LOG_DEBUG << "getInventoryItemAssociationsCb enter";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002331 auto getInventoryItemsConnectionsCb =
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002332 [sensorsAsyncResp, inventoryItems, objectMgrPaths,
Ed Tanousf94c4ec2022-01-06 12:44:41 -08002333 callback{std::forward<const Callback>(callback)}](
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002334 std::shared_ptr<boost::container::flat_set<std::string>>
2335 invConnections) {
2336 BMCWEB_LOG_DEBUG << "getInventoryItemsConnectionsCb enter";
Anthony Wilsond5005492019-07-31 16:34:17 -05002337 auto getInventoryItemsDataCb =
2338 [sensorsAsyncResp, inventoryItems,
2339 callback{std::move(callback)}]() {
2340 BMCWEB_LOG_DEBUG << "getInventoryItemsDataCb enter";
Gunnar Mills42cbe532019-08-15 15:26:54 -05002341
2342 auto getInventoryLedsCb = [sensorsAsyncResp,
2343 inventoryItems,
2344 callback{std::move(
2345 callback)}]() {
2346 BMCWEB_LOG_DEBUG << "getInventoryLedsCb enter";
2347 // Find Power Supply Attributes and get the data
2348 getPowerSupplyAttributes(sensorsAsyncResp,
2349 inventoryItems,
2350 std::move(callback));
2351 BMCWEB_LOG_DEBUG << "getInventoryLedsCb exit";
2352 };
2353
Anthony Wilsond5005492019-07-31 16:34:17 -05002354 // Find led connections and get the data
2355 getInventoryLeds(sensorsAsyncResp, inventoryItems,
Gunnar Mills42cbe532019-08-15 15:26:54 -05002356 std::move(getInventoryLedsCb));
Anthony Wilsond5005492019-07-31 16:34:17 -05002357 BMCWEB_LOG_DEBUG << "getInventoryItemsDataCb exit";
2358 };
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002359
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002360 // Get inventory item data from connections
2361 getInventoryItemsData(sensorsAsyncResp, inventoryItems,
2362 invConnections, objectMgrPaths,
Anthony Wilsond5005492019-07-31 16:34:17 -05002363 std::move(getInventoryItemsDataCb));
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002364 BMCWEB_LOG_DEBUG << "getInventoryItemsConnectionsCb exit";
2365 };
2366
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002367 // Get connections that provide inventory item data
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002368 getInventoryItemsConnections(
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002369 sensorsAsyncResp, inventoryItems,
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002370 std::move(getInventoryItemsConnectionsCb));
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002371 BMCWEB_LOG_DEBUG << "getInventoryItemAssociationsCb exit";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002372 };
2373
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002374 // Get associations from sensors to inventory items
2375 getInventoryItemAssociations(sensorsAsyncResp, sensorNames, objectMgrPaths,
2376 std::move(getInventoryItemAssociationsCb));
2377 BMCWEB_LOG_DEBUG << "getInventoryItems exit";
2378}
2379
2380/**
2381 * @brief Returns JSON PowerSupply object for the specified inventory item.
2382 *
2383 * Searches for a JSON PowerSupply object that matches the specified inventory
2384 * item. If one is not found, a new PowerSupply object is added to the JSON
2385 * array.
2386 *
2387 * Multiple sensors are often associated with one power supply inventory item.
2388 * As a result, multiple sensor values are stored in one JSON PowerSupply
2389 * object.
2390 *
2391 * @param powerSupplyArray JSON array containing Redfish PowerSupply objects.
2392 * @param inventoryItem Inventory item for the power supply.
2393 * @param chassisId Chassis that contains the power supply.
2394 * @return JSON PowerSupply object for the specified inventory item.
2395 */
Ed Tanous23a21a12020-07-25 04:45:05 +00002396inline nlohmann::json& getPowerSupply(nlohmann::json& powerSupplyArray,
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002397 const InventoryItem& inventoryItem,
2398 const std::string& chassisId)
2399{
2400 // Check if matching PowerSupply object already exists in JSON array
2401 for (nlohmann::json& powerSupply : powerSupplyArray)
2402 {
2403 if (powerSupply["MemberId"] == inventoryItem.name)
2404 {
2405 return powerSupply;
2406 }
2407 }
2408
2409 // Add new PowerSupply object to JSON array
2410 powerSupplyArray.push_back({});
2411 nlohmann::json& powerSupply = powerSupplyArray.back();
2412 powerSupply["@odata.id"] =
2413 "/redfish/v1/Chassis/" + chassisId + "/Power#/PowerSupplies/";
2414 powerSupply["MemberId"] = inventoryItem.name;
2415 powerSupply["Name"] = boost::replace_all_copy(inventoryItem.name, "_", " ");
2416 powerSupply["Manufacturer"] = inventoryItem.manufacturer;
2417 powerSupply["Model"] = inventoryItem.model;
2418 powerSupply["PartNumber"] = inventoryItem.partNumber;
2419 powerSupply["SerialNumber"] = inventoryItem.serialNumber;
Anthony Wilsond5005492019-07-31 16:34:17 -05002420 setLedState(powerSupply, &inventoryItem);
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002421
Gunnar Mills42cbe532019-08-15 15:26:54 -05002422 if (inventoryItem.powerSupplyEfficiencyPercent >= 0)
2423 {
2424 powerSupply["EfficiencyPercent"] =
2425 inventoryItem.powerSupplyEfficiencyPercent;
2426 }
2427
2428 powerSupply["Status"]["State"] = getState(&inventoryItem);
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002429 const char* health = inventoryItem.isFunctional ? "OK" : "Critical";
2430 powerSupply["Status"]["Health"] = health;
2431
2432 return powerSupply;
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002433}
2434
2435/**
Shawn McCarneyde629b62019-03-08 10:42:51 -06002436 * @brief Gets the values of the specified sensors.
2437 *
2438 * Stores the results as JSON in the SensorsAsyncResp.
2439 *
2440 * Gets the sensor values asynchronously. Stores the results later when the
2441 * information has been obtained.
2442 *
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002443 * The sensorNames set contains all requested sensors for the current chassis.
Shawn McCarneyde629b62019-03-08 10:42:51 -06002444 *
2445 * To minimize the number of DBus calls, the DBus method
2446 * org.freedesktop.DBus.ObjectManager.GetManagedObjects() is used to get the
2447 * values of all sensors provided by a connection (service).
2448 *
2449 * The connections set contains all the connections that provide sensor values.
2450 *
2451 * The objectMgrPaths map contains mappings from a connection name to the
2452 * corresponding DBus object path that implements ObjectManager.
2453 *
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002454 * The InventoryItem vector contains D-Bus inventory items associated with the
2455 * sensors. Inventory item data is needed for some Redfish sensor properties.
2456 *
Shawn McCarneyde629b62019-03-08 10:42:51 -06002457 * @param SensorsAsyncResp Pointer to object holding response data.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002458 * @param sensorNames All requested sensors within the current chassis.
Shawn McCarneyde629b62019-03-08 10:42:51 -06002459 * @param connections Connections that provide sensor values.
2460 * @param objectMgrPaths Mappings from connection name to DBus object path that
2461 * implements ObjectManager.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002462 * @param inventoryItems Inventory items associated with the sensors.
Shawn McCarneyde629b62019-03-08 10:42:51 -06002463 */
Ed Tanous23a21a12020-07-25 04:45:05 +00002464inline void getSensorData(
Ed Tanous81ce6092020-12-17 16:54:55 +00002465 const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
Ed Tanousb5a76932020-09-29 16:16:58 -07002466 const std::shared_ptr<boost::container::flat_set<std::string>>& sensorNames,
Shawn McCarneyde629b62019-03-08 10:42:51 -06002467 const boost::container::flat_set<std::string>& connections,
Ed Tanousb5a76932020-09-29 16:16:58 -07002468 const std::shared_ptr<boost::container::flat_map<std::string, std::string>>&
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002469 objectMgrPaths,
Ed Tanousb5a76932020-09-29 16:16:58 -07002470 const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems)
Shawn McCarneyde629b62019-03-08 10:42:51 -06002471{
2472 BMCWEB_LOG_DEBUG << "getSensorData enter";
2473 // Get managed objects from all services exposing sensors
2474 for (const std::string& connection : connections)
2475 {
2476 // Response handler to process managed objects
Ed Tanous81ce6092020-12-17 16:54:55 +00002477 auto getManagedObjectsCb = [sensorsAsyncResp, sensorNames,
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002478 inventoryItems](
Shawn McCarneyde629b62019-03-08 10:42:51 -06002479 const boost::system::error_code ec,
Ed Tanous711ac7a2021-12-20 09:34:41 -08002480 dbus::utility::ManagedObjectType& resp) {
Shawn McCarneyde629b62019-03-08 10:42:51 -06002481 BMCWEB_LOG_DEBUG << "getManagedObjectsCb enter";
2482 if (ec)
2483 {
2484 BMCWEB_LOG_ERROR << "getManagedObjectsCb DBUS error: " << ec;
zhanghch058d1b46d2021-04-01 11:18:24 +08002485 messages::internalError(sensorsAsyncResp->asyncResp->res);
Shawn McCarneyde629b62019-03-08 10:42:51 -06002486 return;
2487 }
2488 // Go through all objects and update response with sensor data
2489 for (const auto& objDictEntry : resp)
2490 {
2491 const std::string& objPath =
2492 static_cast<const std::string&>(objDictEntry.first);
2493 BMCWEB_LOG_DEBUG << "getManagedObjectsCb parsing object "
2494 << objPath;
2495
Shawn McCarneyde629b62019-03-08 10:42:51 -06002496 std::vector<std::string> split;
2497 // Reserve space for
2498 // /xyz/openbmc_project/sensors/<name>/<subname>
2499 split.reserve(6);
2500 boost::algorithm::split(split, objPath, boost::is_any_of("/"));
2501 if (split.size() < 6)
2502 {
2503 BMCWEB_LOG_ERROR << "Got path that isn't long enough "
2504 << objPath;
2505 continue;
2506 }
2507 // These indexes aren't intuitive, as boost::split puts an empty
2508 // string at the beginning
2509 const std::string& sensorType = split[4];
2510 const std::string& sensorName = split[5];
2511 BMCWEB_LOG_DEBUG << "sensorName " << sensorName
2512 << " sensorType " << sensorType;
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002513 if (sensorNames->find(objPath) == sensorNames->end())
Shawn McCarneyde629b62019-03-08 10:42:51 -06002514 {
Andrew Geissleraccdbb22021-11-09 15:24:45 -06002515 BMCWEB_LOG_DEBUG << sensorName << " not in sensor list ";
Shawn McCarneyde629b62019-03-08 10:42:51 -06002516 continue;
2517 }
2518
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002519 // Find inventory item (if any) associated with sensor
2520 InventoryItem* inventoryItem =
2521 findInventoryItemForSensor(inventoryItems, objPath);
2522
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002523 const std::string& sensorSchema =
Ed Tanous81ce6092020-12-17 16:54:55 +00002524 sensorsAsyncResp->chassisSubNode;
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002525
2526 nlohmann::json* sensorJson = nullptr;
2527
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02002528 if (sensorSchema == sensors::node::sensors)
Shawn McCarneyde629b62019-03-08 10:42:51 -06002529 {
zhanghch058d1b46d2021-04-01 11:18:24 +08002530 sensorsAsyncResp->asyncResp->res.jsonValue["@odata.id"] =
Ed Tanous81ce6092020-12-17 16:54:55 +00002531 "/redfish/v1/Chassis/" + sensorsAsyncResp->chassisId +
2532 "/" + sensorsAsyncResp->chassisSubNode + "/" +
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002533 sensorName;
zhanghch058d1b46d2021-04-01 11:18:24 +08002534 sensorJson = &(sensorsAsyncResp->asyncResp->res.jsonValue);
Shawn McCarneyde629b62019-03-08 10:42:51 -06002535 }
2536 else
2537 {
Ed Tanous271584a2019-07-09 16:24:22 -07002538 std::string fieldName;
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002539 if (sensorType == "temperature")
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002540 {
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002541 fieldName = "Temperatures";
2542 }
2543 else if (sensorType == "fan" || sensorType == "fan_tach" ||
2544 sensorType == "fan_pwm")
2545 {
2546 fieldName = "Fans";
2547 }
2548 else if (sensorType == "voltage")
2549 {
2550 fieldName = "Voltages";
2551 }
2552 else if (sensorType == "power")
2553 {
Ed Tanous55f79e62022-01-25 11:26:16 -08002554 if (sensorName == "total_power")
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002555 {
2556 fieldName = "PowerControl";
2557 }
2558 else if ((inventoryItem != nullptr) &&
2559 (inventoryItem->isPowerSupply))
2560 {
2561 fieldName = "PowerSupplies";
2562 }
2563 else
2564 {
2565 // Other power sensors are in SensorCollection
2566 continue;
2567 }
2568 }
2569 else
2570 {
2571 BMCWEB_LOG_ERROR << "Unsure how to handle sensorType "
2572 << sensorType;
2573 continue;
2574 }
2575
2576 nlohmann::json& tempArray =
zhanghch058d1b46d2021-04-01 11:18:24 +08002577 sensorsAsyncResp->asyncResp->res.jsonValue[fieldName];
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002578 if (fieldName == "PowerControl")
2579 {
2580 if (tempArray.empty())
2581 {
2582 // Put multiple "sensors" into a single
2583 // PowerControl. Follows MemberId naming and
2584 // naming in power.hpp.
2585 tempArray.push_back(
2586 {{"@odata.id",
2587 "/redfish/v1/Chassis/" +
Ed Tanous81ce6092020-12-17 16:54:55 +00002588 sensorsAsyncResp->chassisId + "/" +
2589 sensorsAsyncResp->chassisSubNode + "#/" +
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002590 fieldName + "/0"}});
2591 }
2592 sensorJson = &(tempArray.back());
2593 }
2594 else if (fieldName == "PowerSupplies")
2595 {
2596 if (inventoryItem != nullptr)
2597 {
2598 sensorJson =
2599 &(getPowerSupply(tempArray, *inventoryItem,
Ed Tanous81ce6092020-12-17 16:54:55 +00002600 sensorsAsyncResp->chassisId));
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002601 }
2602 }
2603 else
2604 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002605 tempArray.push_back(
2606 {{"@odata.id",
2607 "/redfish/v1/Chassis/" +
Ed Tanous81ce6092020-12-17 16:54:55 +00002608 sensorsAsyncResp->chassisId + "/" +
2609 sensorsAsyncResp->chassisSubNode + "#/" +
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002610 fieldName + "/"}});
2611 sensorJson = &(tempArray.back());
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002612 }
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002613 }
Shawn McCarneyde629b62019-03-08 10:42:51 -06002614
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002615 if (sensorJson != nullptr)
2616 {
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02002617 objectInterfacesToJson(
Ed Tanous81ce6092020-12-17 16:54:55 +00002618 sensorName, sensorType, sensorsAsyncResp,
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02002619 objDictEntry.second, *sensorJson, inventoryItem);
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002620 }
Shawn McCarneyde629b62019-03-08 10:42:51 -06002621 }
Ed Tanous81ce6092020-12-17 16:54:55 +00002622 if (sensorsAsyncResp.use_count() == 1)
James Feist8bd25cc2019-03-15 15:14:00 -07002623 {
Ed Tanous81ce6092020-12-17 16:54:55 +00002624 sortJSONResponse(sensorsAsyncResp);
2625 if (sensorsAsyncResp->chassisSubNode == sensors::node::thermal)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002626 {
Ed Tanous81ce6092020-12-17 16:54:55 +00002627 populateFanRedundancy(sensorsAsyncResp);
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002628 }
James Feist8bd25cc2019-03-15 15:14:00 -07002629 }
Shawn McCarneyde629b62019-03-08 10:42:51 -06002630 BMCWEB_LOG_DEBUG << "getManagedObjectsCb exit";
2631 };
2632
2633 // Find DBus object path that implements ObjectManager for the current
2634 // connection. If no mapping found, default to "/".
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002635 auto iter = objectMgrPaths->find(connection);
Shawn McCarneyde629b62019-03-08 10:42:51 -06002636 const std::string& objectMgrPath =
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002637 (iter != objectMgrPaths->end()) ? iter->second : "/";
Shawn McCarneyde629b62019-03-08 10:42:51 -06002638 BMCWEB_LOG_DEBUG << "ObjectManager path for " << connection << " is "
2639 << objectMgrPath;
2640
2641 crow::connections::systemBus->async_method_call(
2642 getManagedObjectsCb, connection, objectMgrPath,
2643 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
Ed Tanous23a21a12020-07-25 04:45:05 +00002644 }
Shawn McCarneyde629b62019-03-08 10:42:51 -06002645 BMCWEB_LOG_DEBUG << "getSensorData exit";
2646}
2647
Ed Tanous23a21a12020-07-25 04:45:05 +00002648inline void processSensorList(
Ed Tanous81ce6092020-12-17 16:54:55 +00002649 const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
Ed Tanousb5a76932020-09-29 16:16:58 -07002650 const std::shared_ptr<boost::container::flat_set<std::string>>& sensorNames)
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002651{
2652 auto getConnectionCb =
Ed Tanous81ce6092020-12-17 16:54:55 +00002653 [sensorsAsyncResp, sensorNames](
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002654 const boost::container::flat_set<std::string>& connections) {
2655 BMCWEB_LOG_DEBUG << "getConnectionCb enter";
2656 auto getObjectManagerPathsCb =
Ed Tanous81ce6092020-12-17 16:54:55 +00002657 [sensorsAsyncResp, sensorNames,
Ed Tanousb5a76932020-09-29 16:16:58 -07002658 connections](const std::shared_ptr<boost::container::flat_map<
2659 std::string, std::string>>& objectMgrPaths) {
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002660 BMCWEB_LOG_DEBUG << "getObjectManagerPathsCb enter";
2661 auto getInventoryItemsCb =
Ed Tanous81ce6092020-12-17 16:54:55 +00002662 [sensorsAsyncResp, sensorNames, connections,
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002663 objectMgrPaths](
Ed Tanousf23b7292020-10-15 09:41:17 -07002664 const std::shared_ptr<std::vector<InventoryItem>>&
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002665 inventoryItems) {
2666 BMCWEB_LOG_DEBUG << "getInventoryItemsCb enter";
2667 // Get sensor data and store results in JSON
Ed Tanous81ce6092020-12-17 16:54:55 +00002668 getSensorData(sensorsAsyncResp, sensorNames,
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002669 connections, objectMgrPaths,
Ed Tanousf23b7292020-10-15 09:41:17 -07002670 inventoryItems);
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002671 BMCWEB_LOG_DEBUG << "getInventoryItemsCb exit";
2672 };
2673
2674 // Get inventory items associated with sensors
Ed Tanous81ce6092020-12-17 16:54:55 +00002675 getInventoryItems(sensorsAsyncResp, sensorNames,
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002676 objectMgrPaths,
2677 std::move(getInventoryItemsCb));
2678
2679 BMCWEB_LOG_DEBUG << "getObjectManagerPathsCb exit";
2680 };
2681
2682 // Get mapping from connection names to the DBus object
2683 // paths that implement the ObjectManager interface
Ed Tanous81ce6092020-12-17 16:54:55 +00002684 getObjectManagerPaths(sensorsAsyncResp,
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002685 std::move(getObjectManagerPathsCb));
2686 BMCWEB_LOG_DEBUG << "getConnectionCb exit";
2687 };
2688
2689 // Get set of connections that provide sensor values
Ed Tanous81ce6092020-12-17 16:54:55 +00002690 getConnections(sensorsAsyncResp, sensorNames, std::move(getConnectionCb));
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002691}
2692
Shawn McCarneyde629b62019-03-08 10:42:51 -06002693/**
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +01002694 * @brief Entry point for retrieving sensors data related to requested
2695 * chassis.
Kowalski, Kamil588c3f02018-04-03 14:55:27 +02002696 * @param SensorsAsyncResp Pointer to object holding response data
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +01002697 */
Ed Tanousb5a76932020-09-29 16:16:58 -07002698inline void
Ed Tanous81ce6092020-12-17 16:54:55 +00002699 getChassisData(const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp)
Ed Tanous1abe55e2018-09-05 08:30:59 -07002700{
2701 BMCWEB_LOG_DEBUG << "getChassisData enter";
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002702 auto getChassisCb =
Ed Tanous81ce6092020-12-17 16:54:55 +00002703 [sensorsAsyncResp](
Ed Tanousf23b7292020-10-15 09:41:17 -07002704 const std::shared_ptr<boost::container::flat_set<std::string>>&
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002705 sensorNames) {
2706 BMCWEB_LOG_DEBUG << "getChassisCb enter";
Ed Tanous81ce6092020-12-17 16:54:55 +00002707 processSensorList(sensorsAsyncResp, sensorNames);
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002708 BMCWEB_LOG_DEBUG << "getChassisCb exit";
2709 };
zhanghch058d1b46d2021-04-01 11:18:24 +08002710 sensorsAsyncResp->asyncResp->res.jsonValue["Redundancy"] =
2711 nlohmann::json::array();
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +01002712
Shawn McCarney26f03892019-05-03 13:20:24 -05002713 // Get set of sensors in chassis
Ed Tanous81ce6092020-12-17 16:54:55 +00002714 getChassis(sensorsAsyncResp, std::move(getChassisCb));
Ed Tanous1abe55e2018-09-05 08:30:59 -07002715 BMCWEB_LOG_DEBUG << "getChassisData exit";
Ed Tanous271584a2019-07-09 16:24:22 -07002716}
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +01002717
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302718/**
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002719 * @brief Find the requested sensorName in the list of all sensors supplied by
2720 * the chassis node
2721 *
2722 * @param sensorName The sensor name supplied in the PATCH request
2723 * @param sensorsList The list of sensors managed by the chassis node
2724 * @param sensorsModified The list of sensors that were found as a result of
2725 * repeated calls to this function
2726 */
Ed Tanous23a21a12020-07-25 04:45:05 +00002727inline bool findSensorNameUsingSensorPath(
Richard Marian Thomaiyar0a86feb2019-05-27 23:16:40 +05302728 std::string_view sensorName,
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002729 boost::container::flat_set<std::string>& sensorsList,
2730 boost::container::flat_set<std::string>& sensorsModified)
2731{
George Liu28aa8de2021-02-01 15:13:30 +08002732 for (auto& chassisSensor : sensorsList)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002733 {
George Liu28aa8de2021-02-01 15:13:30 +08002734 sdbusplus::message::object_path path(chassisSensor);
Ed Tanousb00dcc22021-02-23 12:52:50 -08002735 std::string thisSensorName = path.filename();
George Liu28aa8de2021-02-01 15:13:30 +08002736 if (thisSensorName.empty())
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002737 {
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002738 continue;
2739 }
2740 if (thisSensorName == sensorName)
2741 {
2742 sensorsModified.emplace(chassisSensor);
2743 return true;
2744 }
2745 }
2746 return false;
2747}
2748
2749/**
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302750 * @brief Entry point for overriding sensor values of given sensor
2751 *
zhanghch058d1b46d2021-04-01 11:18:24 +08002752 * @param sensorAsyncResp response object
Carol Wang4bb3dc32019-10-17 18:15:02 +08002753 * @param allCollections Collections extract from sensors' request patch info
jayaprakash Mutyala91e130a2020-03-04 22:26:38 +00002754 * @param chassisSubNode Chassis Node for which the query has to happen
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302755 */
Ed Tanous23a21a12020-07-25 04:45:05 +00002756inline void setSensorsOverride(
Ed Tanousb5a76932020-09-29 16:16:58 -07002757 const std::shared_ptr<SensorsAsyncResp>& sensorAsyncResp,
Carol Wang4bb3dc32019-10-17 18:15:02 +08002758 std::unordered_map<std::string, std::vector<nlohmann::json>>&
jayaprakash Mutyala397fd612020-02-06 23:33:34 +00002759 allCollections)
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302760{
jayaprakash Mutyala70d1d0a2020-01-21 23:41:36 +00002761 BMCWEB_LOG_INFO << "setSensorsOverride for subNode"
Carol Wang4bb3dc32019-10-17 18:15:02 +08002762 << sensorAsyncResp->chassisSubNode << "\n";
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302763
Ed Tanous543f4402022-01-06 13:12:53 -08002764 const char* propertyValueName = nullptr;
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302765 std::unordered_map<std::string, std::pair<double, std::string>> overrideMap;
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302766 std::string memberId;
Ed Tanous543f4402022-01-06 13:12:53 -08002767 double value = 0.0;
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302768 for (auto& collectionItems : allCollections)
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302769 {
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302770 if (collectionItems.first == "Temperatures")
2771 {
2772 propertyValueName = "ReadingCelsius";
2773 }
2774 else if (collectionItems.first == "Fans")
2775 {
2776 propertyValueName = "Reading";
2777 }
2778 else
2779 {
2780 propertyValueName = "ReadingVolts";
2781 }
2782 for (auto& item : collectionItems.second)
2783 {
zhanghch058d1b46d2021-04-01 11:18:24 +08002784 if (!json_util::readJson(item, sensorAsyncResp->asyncResp->res,
2785 "MemberId", memberId, propertyValueName,
2786 value))
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302787 {
2788 return;
2789 }
2790 overrideMap.emplace(memberId,
2791 std::make_pair(value, collectionItems.first));
2792 }
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302793 }
Carol Wang4bb3dc32019-10-17 18:15:02 +08002794
Ed Tanousb5a76932020-09-29 16:16:58 -07002795 auto getChassisSensorListCb = [sensorAsyncResp, overrideMap](
2796 const std::shared_ptr<
2797 boost::container::flat_set<
2798 std::string>>& sensorsList) {
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002799 // Match sensor names in the PATCH request to those managed by the
2800 // chassis node
2801 const std::shared_ptr<boost::container::flat_set<std::string>>
2802 sensorNames =
2803 std::make_shared<boost::container::flat_set<std::string>>();
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302804 for (const auto& item : overrideMap)
2805 {
2806 const auto& sensor = item.first;
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002807 if (!findSensorNameUsingSensorPath(sensor, *sensorsList,
2808 *sensorNames))
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302809 {
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302810 BMCWEB_LOG_INFO << "Unable to find memberId " << item.first;
zhanghch058d1b46d2021-04-01 11:18:24 +08002811 messages::resourceNotFound(sensorAsyncResp->asyncResp->res,
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302812 item.second.second, item.first);
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302813 return;
2814 }
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302815 }
2816 // Get the connection to which the memberId belongs
Jayaprakash Mutyala4f277b52021-12-08 22:46:49 +00002817 auto getObjectsWithConnectionCb = [sensorAsyncResp, overrideMap](
2818 const boost::container::flat_set<
2819 std::string>& /*connections*/,
2820 const std::set<std::pair<
2821 std::string, std::string>>&
2822 objectsWithConnection) {
2823 if (objectsWithConnection.size() != overrideMap.size())
2824 {
2825 BMCWEB_LOG_INFO
2826 << "Unable to find all objects with proper connection "
2827 << objectsWithConnection.size() << " requested "
2828 << overrideMap.size() << "\n";
2829 messages::resourceNotFound(sensorAsyncResp->asyncResp->res,
2830 sensorAsyncResp->chassisSubNode ==
2831 sensors::node::thermal
2832 ? "Temperatures"
2833 : "Voltages",
2834 "Count");
2835 return;
2836 }
2837 for (const auto& item : objectsWithConnection)
2838 {
2839 sdbusplus::message::object_path path(item.first);
2840 std::string sensorName = path.filename();
2841 if (sensorName.empty())
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302842 {
Jayaprakash Mutyala4f277b52021-12-08 22:46:49 +00002843 messages::internalError(sensorAsyncResp->asyncResp->res);
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302844 return;
2845 }
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302846
Jayaprakash Mutyala4f277b52021-12-08 22:46:49 +00002847 const auto& iterator = overrideMap.find(sensorName);
2848 if (iterator == overrideMap.end())
2849 {
2850 BMCWEB_LOG_INFO << "Unable to find sensor object"
2851 << item.first << "\n";
2852 messages::internalError(sensorAsyncResp->asyncResp->res);
2853 return;
2854 }
2855 crow::connections::systemBus->async_method_call(
2856 [sensorAsyncResp](const boost::system::error_code ec) {
2857 if (ec)
2858 {
2859 if (ec.value() ==
2860 boost::system::errc::permission_denied)
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302861 {
Jayaprakash Mutyala4f277b52021-12-08 22:46:49 +00002862 BMCWEB_LOG_WARNING
2863 << "Manufacturing mode is not Enabled...can't "
2864 "Override the sensor value. ";
2865
2866 messages::insufficientPrivilege(
zhanghch058d1b46d2021-04-01 11:18:24 +08002867 sensorAsyncResp->asyncResp->res);
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302868 return;
2869 }
Jayaprakash Mutyala4f277b52021-12-08 22:46:49 +00002870 BMCWEB_LOG_DEBUG
2871 << "setOverrideValueStatus DBUS error: " << ec;
2872 messages::internalError(
2873 sensorAsyncResp->asyncResp->res);
2874 }
2875 },
2876 item.second, item.first, "org.freedesktop.DBus.Properties",
2877 "Set", "xyz.openbmc_project.Sensor.Value", "Value",
Ed Tanous168e20c2021-12-13 14:39:53 -08002878 dbus::utility::DbusVariantType(iterator->second.first));
Jayaprakash Mutyala4f277b52021-12-08 22:46:49 +00002879 }
2880 };
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302881 // Get object with connection for the given sensor name
2882 getObjectsWithConnection(sensorAsyncResp, sensorNames,
2883 std::move(getObjectsWithConnectionCb));
2884 };
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302885 // get full sensor list for the given chassisId and cross verify the sensor.
2886 getChassis(sensorAsyncResp, std::move(getChassisSensorListCb));
2887}
2888
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02002889/**
2890 * @brief Retrieves mapping of Redfish URIs to sensor value property to D-Bus
2891 * path of the sensor.
2892 *
2893 * Function builds valid Redfish response for sensor query of given chassis and
2894 * node. It then builds metadata about Redfish<->D-Bus correlations and provides
2895 * it to caller in a callback.
2896 *
2897 * @param chassis Chassis for which retrieval should be performed
2898 * @param node Node (group) of sensors. See sensors::node for supported values
2899 * @param mapComplete Callback to be called with retrieval result
2900 */
Krzysztof Grobelny021d32c2021-10-29 16:00:07 +02002901inline void retrieveUriToDbusMap(const std::string& chassis,
2902 const std::string& node,
2903 SensorsAsyncResp::DataCompleteCb&& mapComplete)
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02002904{
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +00002905 auto pathIt = sensors::dbus::paths.find(node);
2906 if (pathIt == sensors::dbus::paths.end())
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02002907 {
2908 BMCWEB_LOG_ERROR << "Wrong node provided : " << node;
2909 mapComplete(boost::beast::http::status::bad_request, {});
2910 return;
2911 }
Krzysztof Grobelnyd51e0722021-04-16 13:15:21 +00002912
Nan Zhou72374eb2022-01-27 17:06:51 -08002913 auto asyncResp = std::make_shared<bmcweb::AsyncResp>();
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02002914 auto callback =
Nan Zhou72374eb2022-01-27 17:06:51 -08002915 [asyncResp, mapCompleteCb{std::move(mapComplete)}](
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02002916 const boost::beast::http::status status,
2917 const boost::container::flat_map<std::string, std::string>&
2918 uriToDbus) { mapCompleteCb(status, uriToDbus); };
2919
2920 auto resp = std::make_shared<SensorsAsyncResp>(
Krzysztof Grobelnyd51e0722021-04-16 13:15:21 +00002921 asyncResp, chassis, pathIt->second, node, std::move(callback));
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02002922 getChassisData(resp);
2923}
2924
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002925inline void requestRoutesSensorCollection(App& app)
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002926{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002927 BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/Sensors/")
Ed Tanoused398212021-06-09 17:05:54 -07002928 .privileges(redfish::privileges::getSensorCollection)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002929 .methods(
2930 boost::beast::http::verb::get)([](const crow::Request&,
2931 const std::shared_ptr<
2932 bmcweb::AsyncResp>& aResp,
2933 const std::string& chassisId) {
2934 BMCWEB_LOG_DEBUG << "SensorCollection doGet enter";
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002935
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002936 std::shared_ptr<SensorsAsyncResp> asyncResp =
2937 std::make_shared<SensorsAsyncResp>(
2938 aResp, chassisId,
2939 sensors::dbus::paths.at(sensors::node::sensors),
2940 sensors::node::sensors);
zhanghch058d1b46d2021-04-01 11:18:24 +08002941
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002942 auto getChassisCb =
2943 [asyncResp](
2944 const std::shared_ptr<
2945 boost::container::flat_set<std::string>>& sensorNames) {
2946 BMCWEB_LOG_DEBUG << "getChassisCb enter";
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002947
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002948 nlohmann::json& entriesArray =
2949 asyncResp->asyncResp->res.jsonValue["Members"];
2950 for (auto& sensor : *sensorNames)
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002951 {
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002952 BMCWEB_LOG_DEBUG << "Adding sensor: " << sensor;
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002953
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002954 sdbusplus::message::object_path path(sensor);
2955 std::string sensorName = path.filename();
2956 if (sensorName.empty())
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002957 {
2958 BMCWEB_LOG_ERROR << "Invalid sensor path: "
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002959 << sensor;
2960 messages::internalError(asyncResp->asyncResp->res);
2961 return;
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002962 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002963 entriesArray.push_back(
2964 {{"@odata.id", "/redfish/v1/Chassis/" +
2965 asyncResp->chassisId + "/" +
2966 asyncResp->chassisSubNode + "/" +
2967 sensorName}});
2968 }
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002969
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002970 asyncResp->asyncResp->res.jsonValue["Members@odata.count"] =
2971 entriesArray.size();
2972 BMCWEB_LOG_DEBUG << "getChassisCb exit";
2973 };
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002974
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002975 // Get set of sensors in chassis
2976 getChassis(asyncResp, std::move(getChassisCb));
2977 BMCWEB_LOG_DEBUG << "SensorCollection doGet exit";
2978 });
2979}
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002980
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002981inline void requestRoutesSensor(App& app)
2982{
2983 BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/Sensors/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07002984 .privileges(redfish::privileges::getSensor)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002985 .methods(
2986 boost::beast::http::verb::get)([](const crow::Request&,
2987 const std::shared_ptr<
2988 bmcweb::AsyncResp>& aResp,
2989 const std::string& chassisId,
2990 const std::string& sensorName) {
2991 BMCWEB_LOG_DEBUG << "Sensor doGet enter";
2992 std::shared_ptr<SensorsAsyncResp> asyncResp =
2993 std::make_shared<SensorsAsyncResp>(aResp, chassisId,
2994 std::vector<const char*>(),
2995 sensors::node::sensors);
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002996
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002997 const std::array<const char*, 1> interfaces = {
2998 "xyz.openbmc_project.Sensor.Value"};
2999
3000 // Get a list of all of the sensors that implement Sensor.Value
3001 // and get the path and service name associated with the sensor
3002 crow::connections::systemBus->async_method_call(
3003 [asyncResp, sensorName](const boost::system::error_code ec,
3004 const GetSubTreeType& subtree) {
3005 BMCWEB_LOG_DEBUG << "respHandler1 enter";
3006 if (ec)
3007 {
3008 messages::internalError(asyncResp->asyncResp->res);
3009 BMCWEB_LOG_ERROR
3010 << "Sensor getSensorPaths resp_handler: "
3011 << "Dbus error " << ec;
3012 return;
3013 }
3014
3015 GetSubTreeType::const_iterator it = std::find_if(
3016 subtree.begin(), subtree.end(),
3017 [sensorName](
3018 const std::pair<
3019 std::string,
3020 std::vector<std::pair<
3021 std::string, std::vector<std::string>>>>&
3022 object) {
3023 sdbusplus::message::object_path path(object.first);
3024 std::string name = path.filename();
3025 if (name.empty())
3026 {
3027 BMCWEB_LOG_ERROR << "Invalid sensor path: "
3028 << object.first;
3029 return false;
3030 }
3031
3032 return name == sensorName;
3033 });
3034
3035 if (it == subtree.end())
3036 {
3037 BMCWEB_LOG_ERROR << "Could not find path for sensor: "
3038 << sensorName;
3039 messages::resourceNotFound(asyncResp->asyncResp->res,
3040 "Sensor", sensorName);
3041 return;
3042 }
3043 std::string_view sensorPath = (*it).first;
3044 BMCWEB_LOG_DEBUG << "Found sensor path for sensor '"
3045 << sensorName << "': " << sensorPath;
3046
3047 const std::shared_ptr<
3048 boost::container::flat_set<std::string>>
3049 sensorList = std::make_shared<
3050 boost::container::flat_set<std::string>>();
3051
3052 sensorList->emplace(sensorPath);
3053 processSensorList(asyncResp, sensorList);
3054 BMCWEB_LOG_DEBUG << "respHandler1 exit";
3055 },
3056 "xyz.openbmc_project.ObjectMapper",
3057 "/xyz/openbmc_project/object_mapper",
3058 "xyz.openbmc_project.ObjectMapper", "GetSubTree",
3059 "/xyz/openbmc_project/sensors", 2, interfaces);
3060 });
3061}
Anthony Wilson95a3eca2019-06-11 10:44:47 -05003062
Ed Tanous1abe55e2018-09-05 08:30:59 -07003063} // namespace redfish