blob: 25ac5e044829ae5c1c95c5639a03a93871ddb356 [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 Tanous45ca1b82022-03-25 13:07:27 -070025#include <query.hpp>
Ed Tanoused398212021-06-09 17:05:54 -070026#include <registries/privilege_registry.hpp>
Jonathan Doman1e1e5982021-06-11 09:36:17 -070027#include <sdbusplus/asio/property.hpp>
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +053028#include <utils/json_utils.hpp>
Gunnar Mills1214b7e2020-06-04 10:11:30 -050029
30#include <cmath>
Ed Tanousb5a76932020-09-29 16:16:58 -070031#include <utility>
Ed Tanousabf2add2019-01-22 16:40:12 -080032#include <variant>
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +010033
Ed Tanous1abe55e2018-09-05 08:30:59 -070034namespace redfish
35{
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +010036
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +020037namespace sensors
38{
39namespace node
40{
41static constexpr std::string_view power = "Power";
42static constexpr std::string_view sensors = "Sensors";
43static constexpr std::string_view thermal = "Thermal";
44} // namespace node
45
46namespace dbus
47{
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +000048
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +020049static const boost::container::flat_map<std::string_view,
50 std::vector<const char*>>
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +000051 paths = {{node::power,
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +020052 {"/xyz/openbmc_project/sensors/voltage",
53 "/xyz/openbmc_project/sensors/power"}},
54 {node::sensors,
55 {"/xyz/openbmc_project/sensors/power",
56 "/xyz/openbmc_project/sensors/current",
Basheer Ahmed Muddebihal70886902021-07-22 02:11:17 -070057 "/xyz/openbmc_project/sensors/airflow",
George Liue8204932021-02-01 14:42:49 +080058#ifdef BMCWEB_NEW_POWERSUBSYSTEM_THERMALSUBSYSTEM
59 "/xyz/openbmc_project/sensors/voltage",
60 "/xyz/openbmc_project/sensors/fan_tach",
61 "/xyz/openbmc_project/sensors/temperature",
62 "/xyz/openbmc_project/sensors/fan_pwm",
63 "/xyz/openbmc_project/sensors/altitude",
64 "/xyz/openbmc_project/sensors/energy",
65#endif
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +020066 "/xyz/openbmc_project/sensors/utilization"}},
67 {node::thermal,
68 {"/xyz/openbmc_project/sensors/fan_tach",
69 "/xyz/openbmc_project/sensors/temperature",
70 "/xyz/openbmc_project/sensors/fan_pwm"}}};
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +000071} // namespace dbus
72
73inline const char* toReadingType(const std::string& sensorType)
74{
75 if (sensorType == "voltage")
76 {
77 return "Voltage";
78 }
79 if (sensorType == "power")
80 {
81 return "Power";
82 }
83 if (sensorType == "current")
84 {
85 return "Current";
86 }
87 if (sensorType == "fan_tach")
88 {
89 return "Rotational";
90 }
91 if (sensorType == "temperature")
92 {
93 return "Temperature";
94 }
95 if (sensorType == "fan_pwm" || sensorType == "utilization")
96 {
97 return "Percent";
98 }
99 if (sensorType == "altitude")
100 {
101 return "Altitude";
102 }
103 if (sensorType == "airflow")
104 {
105 return "AirFlow";
106 }
107 if (sensorType == "energy")
108 {
109 return "EnergyJoules";
110 }
111 return "";
112}
113
114inline const char* toReadingUnits(const std::string& sensorType)
115{
116 if (sensorType == "voltage")
117 {
118 return "V";
119 }
120 if (sensorType == "power")
121 {
122 return "W";
123 }
124 if (sensorType == "current")
125 {
126 return "A";
127 }
128 if (sensorType == "fan_tach")
129 {
130 return "RPM";
131 }
132 if (sensorType == "temperature")
133 {
134 return "Cel";
135 }
136 if (sensorType == "fan_pwm" || sensorType == "utilization")
137 {
138 return "%";
139 }
140 if (sensorType == "altitude")
141 {
142 return "m";
143 }
144 if (sensorType == "airflow")
145 {
146 return "cft_i/min";
147 }
148 if (sensorType == "energy")
149 {
150 return "J";
151 }
152 return "";
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200153}
154} // namespace sensors
155
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100156/**
Kowalski, Kamil588c3f02018-04-03 14:55:27 +0200157 * SensorsAsyncResp
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100158 * Gathers data needed for response processing after async calls are done
159 */
Ed Tanous1abe55e2018-09-05 08:30:59 -0700160class SensorsAsyncResp
161{
162 public:
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200163 using DataCompleteCb = std::function<void(
164 const boost::beast::http::status status,
165 const boost::container::flat_map<std::string, std::string>& uriToDbus)>;
166
167 struct SensorData
168 {
169 const std::string name;
170 std::string uri;
171 const std::string valueKey;
172 const std::string dbusPath;
173 };
174
zhanghch058d1b46d2021-04-01 11:18:24 +0800175 SensorsAsyncResp(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
176 const std::string& chassisIdIn,
Ed Tanousb5a76932020-09-29 16:16:58 -0700177 const std::vector<const char*>& typesIn,
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200178 const std::string_view& subNode) :
zhanghch058d1b46d2021-04-01 11:18:24 +0800179 asyncResp(asyncResp),
Ed Tanous271584a2019-07-09 16:24:22 -0700180 chassisId(chassisIdIn), types(typesIn), chassisSubNode(subNode)
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500181 {}
Kowalski, Kamil588c3f02018-04-03 14:55:27 +0200182
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200183 // Store extra data about sensor mapping and return it in callback
zhanghch058d1b46d2021-04-01 11:18:24 +0800184 SensorsAsyncResp(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
185 const std::string& chassisIdIn,
Ed Tanousb5a76932020-09-29 16:16:58 -0700186 const std::vector<const char*>& typesIn,
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200187 const std::string_view& subNode,
188 DataCompleteCb&& creationComplete) :
zhanghch058d1b46d2021-04-01 11:18:24 +0800189 asyncResp(asyncResp),
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200190 chassisId(chassisIdIn), types(typesIn),
191 chassisSubNode(subNode), metadata{std::vector<SensorData>()},
192 dataComplete{std::move(creationComplete)}
193 {}
194
Ed Tanous1abe55e2018-09-05 08:30:59 -0700195 ~SensorsAsyncResp()
196 {
zhanghch058d1b46d2021-04-01 11:18:24 +0800197 if (asyncResp->res.result() ==
198 boost::beast::http::status::internal_server_error)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700199 {
200 // Reset the json object to clear out any data that made it in
201 // before the error happened todo(ed) handle error condition with
202 // proper code
zhanghch058d1b46d2021-04-01 11:18:24 +0800203 asyncResp->res.jsonValue = nlohmann::json::object();
Ed Tanous1abe55e2018-09-05 08:30:59 -0700204 }
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200205
206 if (dataComplete && metadata)
207 {
208 boost::container::flat_map<std::string, std::string> map;
zhanghch058d1b46d2021-04-01 11:18:24 +0800209 if (asyncResp->res.result() == boost::beast::http::status::ok)
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200210 {
211 for (auto& sensor : *metadata)
212 {
213 map.insert(std::make_pair(sensor.uri + sensor.valueKey,
214 sensor.dbusPath));
215 }
216 }
zhanghch058d1b46d2021-04-01 11:18:24 +0800217 dataComplete(asyncResp->res.result(), map);
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200218 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700219 }
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100220
Ed Tanousecd6a3a2022-01-07 09:18:40 -0800221 SensorsAsyncResp(const SensorsAsyncResp&) = delete;
222 SensorsAsyncResp(SensorsAsyncResp&&) = delete;
223 SensorsAsyncResp& operator=(const SensorsAsyncResp&) = delete;
224 SensorsAsyncResp& operator=(SensorsAsyncResp&&) = delete;
225
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200226 void addMetadata(const nlohmann::json& sensorObject,
227 const std::string& valueKey, const std::string& dbusPath)
228 {
229 if (metadata)
230 {
231 metadata->emplace_back(SensorData{sensorObject["Name"],
232 sensorObject["@odata.id"],
233 valueKey, dbusPath});
234 }
235 }
236
237 void updateUri(const std::string& name, const std::string& uri)
238 {
239 if (metadata)
240 {
241 for (auto& sensor : *metadata)
242 {
243 if (sensor.name == name)
244 {
245 sensor.uri = uri;
246 }
247 }
248 }
249 }
250
zhanghch058d1b46d2021-04-01 11:18:24 +0800251 const std::shared_ptr<bmcweb::AsyncResp> asyncResp;
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200252 const std::string chassisId;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700253 const std::vector<const char*> types;
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200254 const std::string chassisSubNode;
255
256 private:
257 std::optional<std::vector<SensorData>> metadata;
258 DataCompleteCb dataComplete;
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100259};
260
261/**
Anthony Wilsond5005492019-07-31 16:34:17 -0500262 * Possible states for physical inventory leds
263 */
264enum class LedState
265{
266 OFF,
267 ON,
268 BLINK,
269 UNKNOWN
270};
271
272/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500273 * D-Bus inventory item associated with one or more sensors.
274 */
275class InventoryItem
276{
277 public:
Ed Tanouse05aec52022-01-25 10:28:56 -0800278 InventoryItem(const std::string& objPath) : objectPath(objPath)
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500279 {
280 // Set inventory item name to last node of object path
George Liu28aa8de2021-02-01 15:13:30 +0800281 sdbusplus::message::object_path path(objectPath);
282 name = path.filename();
283 if (name.empty())
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500284 {
George Liu28aa8de2021-02-01 15:13:30 +0800285 BMCWEB_LOG_ERROR << "Failed to find '/' in " << objectPath;
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500286 }
287 }
288
289 std::string objectPath;
290 std::string name;
Ed Tanouse05aec52022-01-25 10:28:56 -0800291 bool isPresent = true;
292 bool isFunctional = true;
293 bool isPowerSupply = false;
294 int powerSupplyEfficiencyPercent = -1;
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500295 std::string manufacturer;
296 std::string model;
297 std::string partNumber;
298 std::string serialNumber;
299 std::set<std::string> sensors;
Anthony Wilsond5005492019-07-31 16:34:17 -0500300 std::string ledObjectPath;
Ed Tanouse05aec52022-01-25 10:28:56 -0800301 LedState ledState = LedState::UNKNOWN;
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500302};
303
304/**
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530305 * @brief Get objects with connection necessary for sensors
Kowalski, Kamil588c3f02018-04-03 14:55:27 +0200306 * @param SensorsAsyncResp Pointer to object holding response data
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100307 * @param sensorNames Sensors retrieved from chassis
308 * @param callback Callback for processing gathered connections
309 */
310template <typename Callback>
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530311void getObjectsWithConnection(
Ed Tanous81ce6092020-12-17 16:54:55 +0000312 const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
Ed Tanousb5a76932020-09-29 16:16:58 -0700313 const std::shared_ptr<boost::container::flat_set<std::string>>& sensorNames,
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530314 Callback&& callback)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700315{
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530316 BMCWEB_LOG_DEBUG << "getObjectsWithConnection enter";
Ed Tanous1abe55e2018-09-05 08:30:59 -0700317 const std::string path = "/xyz/openbmc_project/sensors";
318 const std::array<std::string, 1> interfaces = {
319 "xyz.openbmc_project.Sensor.Value"};
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100320
Ed Tanous1abe55e2018-09-05 08:30:59 -0700321 // Response handler for parsing objects subtree
Ed Tanousf94c4ec2022-01-06 12:44:41 -0800322 auto respHandler = [callback{std::forward<Callback>(callback)},
Ed Tanousb9d36b42022-02-26 21:42:46 -0800323 sensorsAsyncResp, sensorNames](
324 const boost::system::error_code ec,
325 const dbus::utility::MapperGetSubTreeResponse&
326 subtree) {
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530327 BMCWEB_LOG_DEBUG << "getObjectsWithConnection resp_handler enter";
Ed Tanous1abe55e2018-09-05 08:30:59 -0700328 if (ec)
329 {
zhanghch058d1b46d2021-04-01 11:18:24 +0800330 messages::internalError(sensorsAsyncResp->asyncResp->res);
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530331 BMCWEB_LOG_ERROR
332 << "getObjectsWithConnection resp_handler: Dbus error " << ec;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700333 return;
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100334 }
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100335
Ed Tanous1abe55e2018-09-05 08:30:59 -0700336 BMCWEB_LOG_DEBUG << "Found " << subtree.size() << " subtrees";
337
338 // Make unique list of connections only for requested sensor types and
339 // found in the chassis
340 boost::container::flat_set<std::string> connections;
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530341 std::set<std::pair<std::string, std::string>> objectsWithConnection;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700342 // Intrinsic to avoid malloc. Most systems will have < 8 sensor
343 // producers
344 connections.reserve(8);
345
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700346 BMCWEB_LOG_DEBUG << "sensorNames list count: " << sensorNames->size();
347 for (const std::string& tsensor : *sensorNames)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700348 {
349 BMCWEB_LOG_DEBUG << "Sensor to find: " << tsensor;
350 }
351
352 for (const std::pair<
353 std::string,
354 std::vector<std::pair<std::string, std::vector<std::string>>>>&
355 object : subtree)
356 {
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700357 if (sensorNames->find(object.first) != sensorNames->end())
Ed Tanous1abe55e2018-09-05 08:30:59 -0700358 {
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700359 for (const std::pair<std::string, std::vector<std::string>>&
360 objData : object.second)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700361 {
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700362 BMCWEB_LOG_DEBUG << "Adding connection: " << objData.first;
363 connections.insert(objData.first);
364 objectsWithConnection.insert(
365 std::make_pair(object.first, objData.first));
Ed Tanous1abe55e2018-09-05 08:30:59 -0700366 }
367 }
368 }
369 BMCWEB_LOG_DEBUG << "Found " << connections.size() << " connections";
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530370 callback(std::move(connections), std::move(objectsWithConnection));
371 BMCWEB_LOG_DEBUG << "getObjectsWithConnection resp_handler exit";
Ed Tanous1abe55e2018-09-05 08:30:59 -0700372 };
Ed Tanous1abe55e2018-09-05 08:30:59 -0700373 // Make call to ObjectMapper to find all sensors objects
374 crow::connections::systemBus->async_method_call(
375 std::move(respHandler), "xyz.openbmc_project.ObjectMapper",
376 "/xyz/openbmc_project/object_mapper",
377 "xyz.openbmc_project.ObjectMapper", "GetSubTree", path, 2, interfaces);
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530378 BMCWEB_LOG_DEBUG << "getObjectsWithConnection exit";
379}
380
381/**
382 * @brief Create connections necessary for sensors
383 * @param SensorsAsyncResp Pointer to object holding response data
384 * @param sensorNames Sensors retrieved from chassis
385 * @param callback Callback for processing gathered connections
386 */
387template <typename Callback>
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700388void getConnections(
Ed Tanous81ce6092020-12-17 16:54:55 +0000389 std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700390 const std::shared_ptr<boost::container::flat_set<std::string>> sensorNames,
391 Callback&& callback)
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530392{
393 auto objectsWithConnectionCb =
394 [callback](const boost::container::flat_set<std::string>& connections,
395 const std::set<std::pair<std::string, std::string>>&
Ed Tanous3174e4d2020-10-07 11:41:22 -0700396 /*objectsWithConnection*/) { callback(connections); };
Ed Tanous81ce6092020-12-17 16:54:55 +0000397 getObjectsWithConnection(sensorsAsyncResp, sensorNames,
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530398 std::move(objectsWithConnectionCb));
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100399}
400
401/**
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700402 * @brief Shrinks the list of sensors for processing
403 * @param SensorsAysncResp The class holding the Redfish response
404 * @param allSensors A list of all the sensors associated to the
405 * chassis element (i.e. baseboard, front panel, etc...)
406 * @param activeSensors A list that is a reduction of the incoming
407 * allSensors list. Eliminate Thermal sensors when a Power request is
408 * made, and eliminate Power sensors when a Thermal request is made.
409 */
Ed Tanous23a21a12020-07-25 04:45:05 +0000410inline void reduceSensorList(
Ed Tanous81ce6092020-12-17 16:54:55 +0000411 const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700412 const std::vector<std::string>* allSensors,
Ed Tanousb5a76932020-09-29 16:16:58 -0700413 const std::shared_ptr<boost::container::flat_set<std::string>>&
414 activeSensors)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700415{
Ed Tanous81ce6092020-12-17 16:54:55 +0000416 if (sensorsAsyncResp == nullptr)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700417 {
418 return;
419 }
420 if ((allSensors == nullptr) || (activeSensors == nullptr))
421 {
422 messages::resourceNotFound(
zhanghch058d1b46d2021-04-01 11:18:24 +0800423 sensorsAsyncResp->asyncResp->res, sensorsAsyncResp->chassisSubNode,
Ed Tanous81ce6092020-12-17 16:54:55 +0000424 sensorsAsyncResp->chassisSubNode == sensors::node::thermal
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200425 ? "Temperatures"
426 : "Voltages");
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700427
428 return;
429 }
430 if (allSensors->empty())
431 {
432 // Nothing to do, the activeSensors object is also empty
433 return;
434 }
435
Ed Tanous81ce6092020-12-17 16:54:55 +0000436 for (const char* type : sensorsAsyncResp->types)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700437 {
438 for (const std::string& sensor : *allSensors)
439 {
440 if (boost::starts_with(sensor, type))
441 {
442 activeSensors->emplace(sensor);
443 }
444 }
445 }
446}
447
448/**
Carol Wang4bb3dc32019-10-17 18:15:02 +0800449 * @brief Retrieves valid chassis path
450 * @param asyncResp Pointer to object holding response data
451 * @param callback Callback for next step to get valid chassis path
452 */
453template <typename Callback>
Ed Tanousb5a76932020-09-29 16:16:58 -0700454void getValidChassisPath(const std::shared_ptr<SensorsAsyncResp>& asyncResp,
Carol Wang4bb3dc32019-10-17 18:15:02 +0800455 Callback&& callback)
456{
457 BMCWEB_LOG_DEBUG << "checkChassisId enter";
458 const std::array<const char*, 2> interfaces = {
459 "xyz.openbmc_project.Inventory.Item.Board",
460 "xyz.openbmc_project.Inventory.Item.Chassis"};
461
Ed Tanousb9d36b42022-02-26 21:42:46 -0800462 auto respHandler = [callback{std::forward<Callback>(callback)}, asyncResp](
463 const boost::system::error_code ec,
464 const dbus::utility::MapperGetSubTreePathsResponse&
465 chassisPaths) mutable {
466 BMCWEB_LOG_DEBUG << "getValidChassisPath respHandler enter";
467 if (ec)
468 {
469 BMCWEB_LOG_ERROR << "getValidChassisPath respHandler DBUS error: "
470 << ec;
471 messages::internalError(asyncResp->asyncResp->res);
472 return;
473 }
Carol Wang4bb3dc32019-10-17 18:15:02 +0800474
Ed Tanousb9d36b42022-02-26 21:42:46 -0800475 std::optional<std::string> chassisPath;
476 std::string chassisName;
477 for (const std::string& chassis : chassisPaths)
478 {
479 sdbusplus::message::object_path path(chassis);
480 chassisName = path.filename();
481 if (chassisName.empty())
Carol Wang4bb3dc32019-10-17 18:15:02 +0800482 {
Ed Tanousb9d36b42022-02-26 21:42:46 -0800483 BMCWEB_LOG_ERROR << "Failed to find '/' in " << chassis;
484 continue;
Carol Wang4bb3dc32019-10-17 18:15:02 +0800485 }
Ed Tanousb9d36b42022-02-26 21:42:46 -0800486 if (chassisName == asyncResp->chassisId)
487 {
488 chassisPath = chassis;
489 break;
490 }
491 }
492 callback(chassisPath);
493 };
Carol Wang4bb3dc32019-10-17 18:15:02 +0800494
495 // Get the Chassis Collection
496 crow::connections::systemBus->async_method_call(
497 respHandler, "xyz.openbmc_project.ObjectMapper",
498 "/xyz/openbmc_project/object_mapper",
499 "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths",
500 "/xyz/openbmc_project/inventory", 0, interfaces);
501 BMCWEB_LOG_DEBUG << "checkChassisId exit";
502}
503
504/**
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100505 * @brief Retrieves requested chassis sensors and redundancy data from DBus .
Kowalski, Kamil588c3f02018-04-03 14:55:27 +0200506 * @param SensorsAsyncResp Pointer to object holding response data
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100507 * @param callback Callback for next step in gathered sensor processing
508 */
509template <typename Callback>
Ed Tanousb5a76932020-09-29 16:16:58 -0700510void getChassis(const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
Ed Tanous1abe55e2018-09-05 08:30:59 -0700511 Callback&& callback)
512{
513 BMCWEB_LOG_DEBUG << "getChassis enter";
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500514 const std::array<const char*, 2> interfaces = {
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700515 "xyz.openbmc_project.Inventory.Item.Board",
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500516 "xyz.openbmc_project.Inventory.Item.Chassis"};
Ed Tanousf94c4ec2022-01-06 12:44:41 -0800517 auto respHandler = [callback{std::forward<Callback>(callback)},
518 sensorsAsyncResp](
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700519 const boost::system::error_code ec,
Ed Tanousb9d36b42022-02-26 21:42:46 -0800520 const dbus::utility::MapperGetSubTreePathsResponse&
521 chassisPaths) {
Ed Tanous1abe55e2018-09-05 08:30:59 -0700522 BMCWEB_LOG_DEBUG << "getChassis respHandler enter";
523 if (ec)
524 {
525 BMCWEB_LOG_ERROR << "getChassis respHandler DBUS error: " << ec;
zhanghch058d1b46d2021-04-01 11:18:24 +0800526 messages::internalError(sensorsAsyncResp->asyncResp->res);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700527 return;
528 }
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100529
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700530 const std::string* chassisPath = nullptr;
531 std::string chassisName;
532 for (const std::string& chassis : chassisPaths)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700533 {
George Liu28aa8de2021-02-01 15:13:30 +0800534 sdbusplus::message::object_path path(chassis);
535 chassisName = path.filename();
536 if (chassisName.empty())
Ed Tanous1abe55e2018-09-05 08:30:59 -0700537 {
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700538 BMCWEB_LOG_ERROR << "Failed to find '/' in " << chassis;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700539 continue;
540 }
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700541 if (chassisName == sensorsAsyncResp->chassisId)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700542 {
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700543 chassisPath = &chassis;
544 break;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700545 }
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700546 }
547 if (chassisPath == nullptr)
548 {
zhanghch058d1b46d2021-04-01 11:18:24 +0800549 messages::resourceNotFound(sensorsAsyncResp->asyncResp->res,
550 "Chassis", sensorsAsyncResp->chassisId);
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700551 return;
552 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700553
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700554 const std::string& chassisSubNode = sensorsAsyncResp->chassisSubNode;
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200555 if (chassisSubNode == sensors::node::power)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700556 {
zhanghch058d1b46d2021-04-01 11:18:24 +0800557 sensorsAsyncResp->asyncResp->res.jsonValue["@odata.type"] =
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700558 "#Power.v1_5_2.Power";
Ed Tanous1abe55e2018-09-05 08:30:59 -0700559 }
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200560 else if (chassisSubNode == sensors::node::thermal)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700561 {
zhanghch058d1b46d2021-04-01 11:18:24 +0800562 sensorsAsyncResp->asyncResp->res.jsonValue["@odata.type"] =
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700563 "#Thermal.v1_4_0.Thermal";
zhanghch058d1b46d2021-04-01 11:18:24 +0800564 sensorsAsyncResp->asyncResp->res.jsonValue["Fans"] =
565 nlohmann::json::array();
566 sensorsAsyncResp->asyncResp->res.jsonValue["Temperatures"] =
Jennifer Lee4f9a2132019-03-04 12:45:19 -0800567 nlohmann::json::array();
Ed Tanous1abe55e2018-09-05 08:30:59 -0700568 }
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200569 else if (chassisSubNode == sensors::node::sensors)
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500570 {
zhanghch058d1b46d2021-04-01 11:18:24 +0800571 sensorsAsyncResp->asyncResp->res.jsonValue["@odata.type"] =
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500572 "#SensorCollection.SensorCollection";
zhanghch058d1b46d2021-04-01 11:18:24 +0800573 sensorsAsyncResp->asyncResp->res.jsonValue["Description"] =
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500574 "Collection of Sensors for this Chassis";
zhanghch058d1b46d2021-04-01 11:18:24 +0800575 sensorsAsyncResp->asyncResp->res.jsonValue["Members"] =
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500576 nlohmann::json::array();
zhanghch058d1b46d2021-04-01 11:18:24 +0800577 sensorsAsyncResp->asyncResp->res.jsonValue["Members@odata.count"] =
578 0;
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500579 }
580
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200581 if (chassisSubNode != sensors::node::sensors)
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500582 {
zhanghch058d1b46d2021-04-01 11:18:24 +0800583 sensorsAsyncResp->asyncResp->res.jsonValue["Id"] = chassisSubNode;
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500584 }
585
zhanghch058d1b46d2021-04-01 11:18:24 +0800586 sensorsAsyncResp->asyncResp->res.jsonValue["@odata.id"] =
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700587 "/redfish/v1/Chassis/" + sensorsAsyncResp->chassisId + "/" +
588 chassisSubNode;
zhanghch058d1b46d2021-04-01 11:18:24 +0800589 sensorsAsyncResp->asyncResp->res.jsonValue["Name"] = chassisSubNode;
Shawn McCarney8fb49dd2019-06-12 17:47:00 -0500590 // Get the list of all sensors for this Chassis element
591 std::string sensorPath = *chassisPath + "/all_sensors";
Jonathan Doman1e1e5982021-06-11 09:36:17 -0700592 sdbusplus::asio::getProperty<std::vector<std::string>>(
593 *crow::connections::systemBus, "xyz.openbmc_project.ObjectMapper",
594 sensorPath, "xyz.openbmc_project.Association", "endpoints",
Ed Tanousf94c4ec2022-01-06 12:44:41 -0800595 [sensorsAsyncResp,
596 callback{std::forward<const Callback>(callback)}](
Ed Tanous271584a2019-07-09 16:24:22 -0700597 const boost::system::error_code& e,
Jonathan Doman1e1e5982021-06-11 09:36:17 -0700598 const std::vector<std::string>& nodeSensorList) {
Ed Tanous271584a2019-07-09 16:24:22 -0700599 if (e)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700600 {
Ed Tanous271584a2019-07-09 16:24:22 -0700601 if (e.value() != EBADR)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700602 {
zhanghch058d1b46d2021-04-01 11:18:24 +0800603 messages::internalError(
604 sensorsAsyncResp->asyncResp->res);
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700605 return;
606 }
607 }
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700608 const std::shared_ptr<boost::container::flat_set<std::string>>
609 culledSensorList = std::make_shared<
610 boost::container::flat_set<std::string>>();
Jonathan Doman1e1e5982021-06-11 09:36:17 -0700611 reduceSensorList(sensorsAsyncResp, &nodeSensorList,
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700612 culledSensorList);
613 callback(culledSensorList);
Jonathan Doman1e1e5982021-06-11 09:36:17 -0700614 });
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100615 };
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100616
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700617 // Get the Chassis Collection
Ed Tanous1abe55e2018-09-05 08:30:59 -0700618 crow::connections::systemBus->async_method_call(
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700619 respHandler, "xyz.openbmc_project.ObjectMapper",
620 "/xyz/openbmc_project/object_mapper",
621 "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths",
Ed Tanous271584a2019-07-09 16:24:22 -0700622 "/xyz/openbmc_project/inventory", 0, interfaces);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700623 BMCWEB_LOG_DEBUG << "getChassis exit";
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100624}
625
626/**
Shawn McCarneyde629b62019-03-08 10:42:51 -0600627 * @brief Finds all DBus object paths that implement ObjectManager.
628 *
629 * Creates a mapping from the associated connection name to the object path.
630 *
631 * Finds the object paths asynchronously. Invokes callback when information has
632 * been obtained.
633 *
634 * The callback must have the following signature:
635 * @code
Shawn McCarney8fb49dd2019-06-12 17:47:00 -0500636 * callback(std::shared_ptr<boost::container::flat_map<std::string,
637 * std::string>> objectMgrPaths)
Shawn McCarneyde629b62019-03-08 10:42:51 -0600638 * @endcode
639 *
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700640 * @param sensorsAsyncResp Pointer to object holding response data.
Shawn McCarneyde629b62019-03-08 10:42:51 -0600641 * @param callback Callback to invoke when object paths obtained.
642 */
643template <typename Callback>
Ed Tanousb5a76932020-09-29 16:16:58 -0700644void getObjectManagerPaths(
Ed Tanous81ce6092020-12-17 16:54:55 +0000645 const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
Ed Tanousb5a76932020-09-29 16:16:58 -0700646 Callback&& callback)
Shawn McCarneyde629b62019-03-08 10:42:51 -0600647{
648 BMCWEB_LOG_DEBUG << "getObjectManagerPaths enter";
649 const std::array<std::string, 1> interfaces = {
650 "org.freedesktop.DBus.ObjectManager"};
651
652 // Response handler for GetSubTree DBus method
Ed Tanousf94c4ec2022-01-06 12:44:41 -0800653 auto respHandler = [callback{std::forward<Callback>(callback)},
Ed Tanousb9d36b42022-02-26 21:42:46 -0800654 sensorsAsyncResp](
655 const boost::system::error_code ec,
656 const dbus::utility::MapperGetSubTreeResponse&
657 subtree) {
Shawn McCarneyde629b62019-03-08 10:42:51 -0600658 BMCWEB_LOG_DEBUG << "getObjectManagerPaths respHandler enter";
659 if (ec)
660 {
zhanghch058d1b46d2021-04-01 11:18:24 +0800661 messages::internalError(sensorsAsyncResp->asyncResp->res);
Shawn McCarneyde629b62019-03-08 10:42:51 -0600662 BMCWEB_LOG_ERROR << "getObjectManagerPaths respHandler: DBus error "
663 << ec;
664 return;
665 }
666
667 // Loop over returned object paths
Shawn McCarney8fb49dd2019-06-12 17:47:00 -0500668 std::shared_ptr<boost::container::flat_map<std::string, std::string>>
669 objectMgrPaths = std::make_shared<
670 boost::container::flat_map<std::string, std::string>>();
Shawn McCarneyde629b62019-03-08 10:42:51 -0600671 for (const std::pair<
672 std::string,
673 std::vector<std::pair<std::string, std::vector<std::string>>>>&
674 object : subtree)
675 {
676 // Loop over connections for current object path
677 const std::string& objectPath = object.first;
678 for (const std::pair<std::string, std::vector<std::string>>&
679 objData : object.second)
680 {
681 // Add mapping from connection to object path
682 const std::string& connection = objData.first;
Shawn McCarney8fb49dd2019-06-12 17:47:00 -0500683 (*objectMgrPaths)[connection] = objectPath;
Shawn McCarneyde629b62019-03-08 10:42:51 -0600684 BMCWEB_LOG_DEBUG << "Added mapping " << connection << " -> "
685 << objectPath;
686 }
687 }
Shawn McCarney8fb49dd2019-06-12 17:47:00 -0500688 callback(objectMgrPaths);
Shawn McCarneyde629b62019-03-08 10:42:51 -0600689 BMCWEB_LOG_DEBUG << "getObjectManagerPaths respHandler exit";
690 };
691
692 // Query mapper for all DBus object paths that implement ObjectManager
693 crow::connections::systemBus->async_method_call(
694 std::move(respHandler), "xyz.openbmc_project.ObjectMapper",
695 "/xyz/openbmc_project/object_mapper",
Ed Tanous271584a2019-07-09 16:24:22 -0700696 "xyz.openbmc_project.ObjectMapper", "GetSubTree", "/", 0, interfaces);
Shawn McCarneyde629b62019-03-08 10:42:51 -0600697 BMCWEB_LOG_DEBUG << "getObjectManagerPaths exit";
698}
699
700/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500701 * @brief Returns the Redfish State value for the specified inventory item.
702 * @param inventoryItem D-Bus inventory item associated with a sensor.
703 * @return State value for inventory item.
James Feist34dd1792019-05-17 14:10:54 -0700704 */
Ed Tanous23a21a12020-07-25 04:45:05 +0000705inline std::string getState(const InventoryItem* inventoryItem)
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500706{
707 if ((inventoryItem != nullptr) && !(inventoryItem->isPresent))
708 {
709 return "Absent";
710 }
James Feist34dd1792019-05-17 14:10:54 -0700711
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500712 return "Enabled";
713}
714
715/**
716 * @brief Returns the Redfish Health value for the specified sensor.
717 * @param sensorJson Sensor JSON object.
718 * @param interfacesDict Map of all sensor interfaces.
719 * @param inventoryItem D-Bus inventory item associated with the sensor. Will
720 * be nullptr if no associated inventory item was found.
721 * @return Health value for sensor.
722 */
Ed Tanous711ac7a2021-12-20 09:34:41 -0800723inline std::string
724 getHealth(nlohmann::json& sensorJson,
725 const dbus::utility::DBusInteracesMap& interfacesDict,
726 const InventoryItem* inventoryItem)
James Feist34dd1792019-05-17 14:10:54 -0700727{
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500728 // Get current health value (if any) in the sensor JSON object. Some JSON
729 // objects contain multiple sensors (such as PowerSupplies). We want to set
730 // the overall health to be the most severe of any of the sensors.
731 std::string currentHealth;
732 auto statusIt = sensorJson.find("Status");
733 if (statusIt != sensorJson.end())
734 {
735 auto healthIt = statusIt->find("Health");
736 if (healthIt != statusIt->end())
737 {
738 std::string* health = healthIt->get_ptr<std::string*>();
739 if (health != nullptr)
740 {
741 currentHealth = *health;
742 }
743 }
744 }
745
746 // If current health in JSON object is already Critical, return that. This
747 // should override the sensor health, which might be less severe.
748 if (currentHealth == "Critical")
749 {
750 return "Critical";
751 }
752
753 // Check if sensor has critical threshold alarm
Ed Tanous711ac7a2021-12-20 09:34:41 -0800754
Ed Tanous9eb808c2022-01-25 10:19:23 -0800755 for (const auto& [interface, values] : interfacesDict)
James Feist34dd1792019-05-17 14:10:54 -0700756 {
Ed Tanous711ac7a2021-12-20 09:34:41 -0800757 if (interface == "xyz.openbmc_project.Sensor.Threshold.Critical")
James Feist34dd1792019-05-17 14:10:54 -0700758 {
Ed Tanous9eb808c2022-01-25 10:19:23 -0800759 for (const auto& [valueName, value] : values)
James Feist34dd1792019-05-17 14:10:54 -0700760 {
Ed Tanous711ac7a2021-12-20 09:34:41 -0800761 if (valueName == "CriticalAlarmHigh" ||
762 valueName == "CriticalAlarmLow")
763 {
764 const bool* asserted = std::get_if<bool>(&value);
765 if (asserted == nullptr)
766 {
767 BMCWEB_LOG_ERROR << "Illegal sensor threshold";
768 }
769 else if (*asserted)
770 {
771 return "Critical";
772 }
773 }
James Feist34dd1792019-05-17 14:10:54 -0700774 }
775 }
776 }
777
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500778 // Check if associated inventory item is not functional
779 if ((inventoryItem != nullptr) && !(inventoryItem->isFunctional))
780 {
781 return "Critical";
782 }
783
784 // If current health in JSON object is already Warning, return that. This
785 // should override the sensor status, which might be less severe.
786 if (currentHealth == "Warning")
787 {
788 return "Warning";
789 }
790
791 // Check if sensor has warning threshold alarm
Ed Tanous9eb808c2022-01-25 10:19:23 -0800792 for (const auto& [interface, values] : interfacesDict)
James Feist34dd1792019-05-17 14:10:54 -0700793 {
Ed Tanous711ac7a2021-12-20 09:34:41 -0800794 if (interface == "xyz.openbmc_project.Sensor.Threshold.Warning")
James Feist34dd1792019-05-17 14:10:54 -0700795 {
Ed Tanous9eb808c2022-01-25 10:19:23 -0800796 for (const auto& [valueName, value] : values)
James Feist34dd1792019-05-17 14:10:54 -0700797 {
Ed Tanous711ac7a2021-12-20 09:34:41 -0800798 if (valueName == "WarningAlarmHigh" ||
799 valueName == "WarningAlarmLow")
800 {
801 const bool* asserted = std::get_if<bool>(&value);
802 if (asserted == nullptr)
803 {
804 BMCWEB_LOG_ERROR << "Illegal sensor threshold";
805 }
806 else if (*asserted)
807 {
Ed Tanousebe4d912022-01-12 12:38:17 -0800808 return "Warning";
Ed Tanous711ac7a2021-12-20 09:34:41 -0800809 }
810 }
James Feist34dd1792019-05-17 14:10:54 -0700811 }
812 }
813 }
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500814
James Feist34dd1792019-05-17 14:10:54 -0700815 return "OK";
816}
817
Ed Tanous23a21a12020-07-25 04:45:05 +0000818inline void setLedState(nlohmann::json& sensorJson,
Anthony Wilsond5005492019-07-31 16:34:17 -0500819 const InventoryItem* inventoryItem)
820{
821 if (inventoryItem != nullptr && !inventoryItem->ledObjectPath.empty())
822 {
823 switch (inventoryItem->ledState)
824 {
825 case LedState::OFF:
826 sensorJson["IndicatorLED"] = "Off";
827 break;
828 case LedState::ON:
829 sensorJson["IndicatorLED"] = "Lit";
830 break;
831 case LedState::BLINK:
832 sensorJson["IndicatorLED"] = "Blinking";
833 break;
Ed Tanous23a21a12020-07-25 04:45:05 +0000834 case LedState::UNKNOWN:
Anthony Wilsond5005492019-07-31 16:34:17 -0500835 break;
836 }
837 }
838}
839
James Feist34dd1792019-05-17 14:10:54 -0700840/**
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100841 * @brief Builds a json sensor representation of a sensor.
842 * @param sensorName The name of the sensor to be built
Gunnar Mills274fad52018-06-13 15:45:36 -0500843 * @param sensorType The type (temperature, fan_tach, etc) of the sensor to
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100844 * build
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200845 * @param sensorsAsyncResp Sensor metadata
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100846 * @param interfacesDict A dictionary of the interfaces and properties of said
847 * interfaces to be built from
848 * @param sensor_json The json object to fill
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500849 * @param inventoryItem D-Bus inventory item associated with the sensor. Will
850 * be nullptr if no associated inventory item was found.
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100851 */
Ed Tanous23a21a12020-07-25 04:45:05 +0000852inline void objectInterfacesToJson(
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100853 const std::string& sensorName, const std::string& sensorType,
Ed Tanousb5a76932020-09-29 16:16:58 -0700854 const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
Ed Tanous711ac7a2021-12-20 09:34:41 -0800855 const dbus::utility::DBusInteracesMap& interfacesDict,
Ed Tanous81ce6092020-12-17 16:54:55 +0000856 nlohmann::json& sensorJson, InventoryItem* inventoryItem)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700857{
Ed Tanous1abe55e2018-09-05 08:30:59 -0700858 // Assume values exist as is (10^0 == 1) if no scale exists
859 int64_t scaleMultiplier = 0;
Ed Tanous9eb808c2022-01-25 10:19:23 -0800860 for (const auto& [interface, values] : interfacesDict)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700861 {
Ed Tanous711ac7a2021-12-20 09:34:41 -0800862 if (interface == "xyz.openbmc_project.Sensor.Value")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700863 {
Ed Tanous9eb808c2022-01-25 10:19:23 -0800864 for (const auto& [valueName, value] : values)
Ed Tanous711ac7a2021-12-20 09:34:41 -0800865 {
866 if (valueName == "Scale")
867 {
868 const int64_t* int64Value = std::get_if<int64_t>(&value);
869 if (int64Value != nullptr)
870 {
871 scaleMultiplier = *int64Value;
872 }
873 }
874 }
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100875 }
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100876 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700877
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200878 if (sensorsAsyncResp->chassisSubNode == sensors::node::sensors)
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500879 {
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500880 // For sensors in SensorCollection we set Id instead of MemberId,
881 // including power sensors.
Ed Tanous81ce6092020-12-17 16:54:55 +0000882 sensorJson["Id"] = sensorName;
883 sensorJson["Name"] = boost::replace_all_copy(sensorName, "_", " ");
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500884 }
885 else if (sensorType != "power")
886 {
887 // Set MemberId and Name for non-power sensors. For PowerSupplies and
888 // PowerControl, those properties have more general values because
889 // multiple sensors can be stored in the same JSON object.
Ed Tanous81ce6092020-12-17 16:54:55 +0000890 sensorJson["MemberId"] = sensorName;
891 sensorJson["Name"] = boost::replace_all_copy(sensorName, "_", " ");
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500892 }
Ed Tanouse742b6c2019-05-03 15:06:53 -0700893
Ed Tanous81ce6092020-12-17 16:54:55 +0000894 sensorJson["Status"]["State"] = getState(inventoryItem);
895 sensorJson["Status"]["Health"] =
896 getHealth(sensorJson, interfacesDict, inventoryItem);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700897
898 // Parameter to set to override the type we get from dbus, and force it to
899 // int, regardless of what is available. This is used for schemas like fan,
900 // that require integers, not floats.
901 bool forceToInt = false;
902
Anthony Wilson3929aca2019-07-19 15:42:33 -0500903 nlohmann::json::json_pointer unit("/Reading");
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200904 if (sensorsAsyncResp->chassisSubNode == sensors::node::sensors)
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500905 {
Ed Tanous81ce6092020-12-17 16:54:55 +0000906 sensorJson["@odata.type"] = "#Sensor.v1_0_0.Sensor";
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000907
908 const std::string& readingType = sensors::toReadingType(sensorType);
909 if (readingType.empty())
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500910 {
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000911 BMCWEB_LOG_ERROR << "Redfish cannot map reading type for "
912 << sensorType;
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500913 }
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000914 else
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500915 {
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000916 sensorJson["ReadingType"] = readingType;
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500917 }
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000918
919 const std::string& readingUnits = sensors::toReadingUnits(sensorType);
920 if (readingUnits.empty())
Adrian Ambrożewiczf8ede152020-06-02 13:26:33 +0200921 {
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000922 BMCWEB_LOG_ERROR << "Redfish cannot map reading unit for "
923 << sensorType;
924 }
925 else
926 {
927 sensorJson["ReadingUnits"] = readingUnits;
Adrian Ambrożewiczf8ede152020-06-02 13:26:33 +0200928 }
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500929 }
930 else if (sensorType == "temperature")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700931 {
Anthony Wilson3929aca2019-07-19 15:42:33 -0500932 unit = "/ReadingCelsius"_json_pointer;
Ed Tanous81ce6092020-12-17 16:54:55 +0000933 sensorJson["@odata.type"] = "#Thermal.v1_3_0.Temperature";
Ed Tanous1abe55e2018-09-05 08:30:59 -0700934 // TODO(ed) Documentation says that path should be type fan_tach,
935 // implementation seems to implement fan
936 }
937 else if (sensorType == "fan" || sensorType == "fan_tach")
938 {
Anthony Wilson3929aca2019-07-19 15:42:33 -0500939 unit = "/Reading"_json_pointer;
Ed Tanous81ce6092020-12-17 16:54:55 +0000940 sensorJson["ReadingUnits"] = "RPM";
941 sensorJson["@odata.type"] = "#Thermal.v1_3_0.Fan";
942 setLedState(sensorJson, inventoryItem);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700943 forceToInt = true;
944 }
Ed Tanous6f6d0d32018-10-12 11:16:43 -0700945 else if (sensorType == "fan_pwm")
946 {
Anthony Wilson3929aca2019-07-19 15:42:33 -0500947 unit = "/Reading"_json_pointer;
Ed Tanous81ce6092020-12-17 16:54:55 +0000948 sensorJson["ReadingUnits"] = "Percent";
949 sensorJson["@odata.type"] = "#Thermal.v1_3_0.Fan";
950 setLedState(sensorJson, inventoryItem);
Ed Tanous6f6d0d32018-10-12 11:16:43 -0700951 forceToInt = true;
952 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700953 else if (sensorType == "voltage")
954 {
Anthony Wilson3929aca2019-07-19 15:42:33 -0500955 unit = "/ReadingVolts"_json_pointer;
Ed Tanous81ce6092020-12-17 16:54:55 +0000956 sensorJson["@odata.type"] = "#Power.v1_0_0.Voltage";
Ed Tanous1abe55e2018-09-05 08:30:59 -0700957 }
Ed Tanous2474adf2018-09-05 16:31:16 -0700958 else if (sensorType == "power")
959 {
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700960 std::string sensorNameLower =
961 boost::algorithm::to_lower_copy(sensorName);
962
Ed Tanous55f79e62022-01-25 11:26:16 -0800963 if (sensorName == "total_power")
Eddie James028f7eb2019-05-17 21:24:36 +0000964 {
Ed Tanous81ce6092020-12-17 16:54:55 +0000965 sensorJson["@odata.type"] = "#Power.v1_0_0.PowerControl";
Gunnar Mills7ab06f42019-07-02 13:07:16 -0500966 // Put multiple "sensors" into a single PowerControl, so have
967 // generic names for MemberId and Name. Follows Redfish mockup.
Ed Tanous81ce6092020-12-17 16:54:55 +0000968 sensorJson["MemberId"] = "0";
969 sensorJson["Name"] = "Chassis Power Control";
Anthony Wilson3929aca2019-07-19 15:42:33 -0500970 unit = "/PowerConsumedWatts"_json_pointer;
Eddie James028f7eb2019-05-17 21:24:36 +0000971 }
972 else if (sensorNameLower.find("input") != std::string::npos)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700973 {
Anthony Wilson3929aca2019-07-19 15:42:33 -0500974 unit = "/PowerInputWatts"_json_pointer;
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700975 }
976 else
977 {
Anthony Wilson3929aca2019-07-19 15:42:33 -0500978 unit = "/PowerOutputWatts"_json_pointer;
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700979 }
Ed Tanous2474adf2018-09-05 16:31:16 -0700980 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700981 else
982 {
983 BMCWEB_LOG_ERROR << "Redfish cannot map object type for " << sensorName;
984 return;
985 }
986 // Map of dbus interface name, dbus property name and redfish property_name
Anthony Wilson3929aca2019-07-19 15:42:33 -0500987 std::vector<
988 std::tuple<const char*, const char*, nlohmann::json::json_pointer>>
989 properties;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700990 properties.reserve(7);
991
992 properties.emplace_back("xyz.openbmc_project.Sensor.Value", "Value", unit);
Shawn McCarneyde629b62019-03-08 10:42:51 -0600993
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200994 if (sensorsAsyncResp->chassisSubNode == sensors::node::sensors)
Anthony Wilson3929aca2019-07-19 15:42:33 -0500995 {
996 properties.emplace_back(
997 "xyz.openbmc_project.Sensor.Threshold.Warning", "WarningHigh",
998 "/Thresholds/UpperCaution/Reading"_json_pointer);
999 properties.emplace_back(
1000 "xyz.openbmc_project.Sensor.Threshold.Warning", "WarningLow",
1001 "/Thresholds/LowerCaution/Reading"_json_pointer);
1002 properties.emplace_back(
1003 "xyz.openbmc_project.Sensor.Threshold.Critical", "CriticalHigh",
1004 "/Thresholds/UpperCritical/Reading"_json_pointer);
1005 properties.emplace_back(
1006 "xyz.openbmc_project.Sensor.Threshold.Critical", "CriticalLow",
1007 "/Thresholds/LowerCritical/Reading"_json_pointer);
1008 }
1009 else if (sensorType != "power")
Shawn McCarneyde629b62019-03-08 10:42:51 -06001010 {
1011 properties.emplace_back("xyz.openbmc_project.Sensor.Threshold.Warning",
Anthony Wilson3929aca2019-07-19 15:42:33 -05001012 "WarningHigh",
1013 "/UpperThresholdNonCritical"_json_pointer);
Shawn McCarneyde629b62019-03-08 10:42:51 -06001014 properties.emplace_back("xyz.openbmc_project.Sensor.Threshold.Warning",
Anthony Wilson3929aca2019-07-19 15:42:33 -05001015 "WarningLow",
1016 "/LowerThresholdNonCritical"_json_pointer);
Shawn McCarneyde629b62019-03-08 10:42:51 -06001017 properties.emplace_back("xyz.openbmc_project.Sensor.Threshold.Critical",
Anthony Wilson3929aca2019-07-19 15:42:33 -05001018 "CriticalHigh",
1019 "/UpperThresholdCritical"_json_pointer);
Shawn McCarneyde629b62019-03-08 10:42:51 -06001020 properties.emplace_back("xyz.openbmc_project.Sensor.Threshold.Critical",
Anthony Wilson3929aca2019-07-19 15:42:33 -05001021 "CriticalLow",
1022 "/LowerThresholdCritical"_json_pointer);
Shawn McCarneyde629b62019-03-08 10:42:51 -06001023 }
Ed Tanous1abe55e2018-09-05 08:30:59 -07001024
Ed Tanous2474adf2018-09-05 16:31:16 -07001025 // TODO Need to get UpperThresholdFatal and LowerThresholdFatal
1026
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02001027 if (sensorsAsyncResp->chassisSubNode == sensors::node::sensors)
Anthony Wilson95a3eca2019-06-11 10:44:47 -05001028 {
1029 properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MinValue",
Anthony Wilson3929aca2019-07-19 15:42:33 -05001030 "/ReadingRangeMin"_json_pointer);
Anthony Wilson95a3eca2019-06-11 10:44:47 -05001031 properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MaxValue",
Anthony Wilson3929aca2019-07-19 15:42:33 -05001032 "/ReadingRangeMax"_json_pointer);
Anthony Wilson95a3eca2019-06-11 10:44:47 -05001033 }
1034 else if (sensorType == "temperature")
Ed Tanous1abe55e2018-09-05 08:30:59 -07001035 {
1036 properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MinValue",
Anthony Wilson3929aca2019-07-19 15:42:33 -05001037 "/MinReadingRangeTemp"_json_pointer);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001038 properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MaxValue",
Anthony Wilson3929aca2019-07-19 15:42:33 -05001039 "/MaxReadingRangeTemp"_json_pointer);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001040 }
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001041 else if (sensorType != "power")
Ed Tanous1abe55e2018-09-05 08:30:59 -07001042 {
1043 properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MinValue",
Anthony Wilson3929aca2019-07-19 15:42:33 -05001044 "/MinReadingRange"_json_pointer);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001045 properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MaxValue",
Anthony Wilson3929aca2019-07-19 15:42:33 -05001046 "/MaxReadingRange"_json_pointer);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001047 }
1048
Anthony Wilson3929aca2019-07-19 15:42:33 -05001049 for (const std::tuple<const char*, const char*,
1050 nlohmann::json::json_pointer>& p : properties)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001051 {
Ed Tanous55f79e62022-01-25 11:26:16 -08001052 for (const auto& [interface, values] : interfacesDict)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001053 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001054 if (interface != std::get<0>(p))
Ed Tanous1abe55e2018-09-05 08:30:59 -07001055 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001056 continue;
1057 }
Ed Tanous55f79e62022-01-25 11:26:16 -08001058 for (const auto& [valueName, valueVariant] : values)
Ed Tanous711ac7a2021-12-20 09:34:41 -08001059 {
1060 if (valueName != std::get<1>(p))
1061 {
1062 continue;
1063 }
Anthony Wilson3929aca2019-07-19 15:42:33 -05001064
1065 // The property we want to set may be nested json, so use
1066 // a json_pointer for easy indexing into the json structure.
1067 const nlohmann::json::json_pointer& key = std::get<2>(p);
1068
Ed Tanous1abe55e2018-09-05 08:30:59 -07001069 // Attempt to pull the int64 directly
Ed Tanousabf2add2019-01-22 16:40:12 -08001070 const int64_t* int64Value = std::get_if<int64_t>(&valueVariant);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001071
Ed Tanousabf2add2019-01-22 16:40:12 -08001072 const double* doubleValue = std::get_if<double>(&valueVariant);
Eddie James028f7eb2019-05-17 21:24:36 +00001073 const uint32_t* uValue = std::get_if<uint32_t>(&valueVariant);
Ed Tanous6f6d0d32018-10-12 11:16:43 -07001074 double temp = 0.0;
1075 if (int64Value != nullptr)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001076 {
Ed Tanous271584a2019-07-09 16:24:22 -07001077 temp = static_cast<double>(*int64Value);
Ed Tanous6f6d0d32018-10-12 11:16:43 -07001078 }
1079 else if (doubleValue != nullptr)
1080 {
1081 temp = *doubleValue;
1082 }
Eddie James028f7eb2019-05-17 21:24:36 +00001083 else if (uValue != nullptr)
1084 {
1085 temp = *uValue;
1086 }
Ed Tanous6f6d0d32018-10-12 11:16:43 -07001087 else
1088 {
1089 BMCWEB_LOG_ERROR
1090 << "Got value interface that wasn't int or double";
1091 continue;
1092 }
1093 temp = temp * std::pow(10, scaleMultiplier);
1094 if (forceToInt)
1095 {
Ed Tanous81ce6092020-12-17 16:54:55 +00001096 sensorJson[key] = static_cast<int64_t>(temp);
Ed Tanous6f6d0d32018-10-12 11:16:43 -07001097 }
1098 else
1099 {
Ed Tanous81ce6092020-12-17 16:54:55 +00001100 sensorJson[key] = temp;
Ed Tanous1abe55e2018-09-05 08:30:59 -07001101 }
1102 }
1103 }
1104 }
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02001105
Ed Tanous81ce6092020-12-17 16:54:55 +00001106 sensorsAsyncResp->addMetadata(sensorJson, unit.to_string(),
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02001107 "/xyz/openbmc_project/sensors/" + sensorType +
1108 "/" + sensorName);
1109
Ed Tanous1abe55e2018-09-05 08:30:59 -07001110 BMCWEB_LOG_DEBUG << "Added sensor " << sensorName;
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +01001111}
1112
Ed Tanousb5a76932020-09-29 16:16:58 -07001113inline void populateFanRedundancy(
1114 const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp)
James Feist8bd25cc2019-03-15 15:14:00 -07001115{
1116 crow::connections::systemBus->async_method_call(
Ed Tanousb9d36b42022-02-26 21:42:46 -08001117 [sensorsAsyncResp](
1118 const boost::system::error_code ec,
1119 const dbus::utility::MapperGetSubTreeResponse& resp) {
James Feist8bd25cc2019-03-15 15:14:00 -07001120 if (ec)
1121 {
1122 return; // don't have to have this interface
1123 }
Ed Tanouse278c182019-03-13 16:23:37 -07001124 for (const std::pair<std::string,
1125 std::vector<std::pair<
1126 std::string, std::vector<std::string>>>>&
1127 pathPair : resp)
James Feist8bd25cc2019-03-15 15:14:00 -07001128 {
Ed Tanouse278c182019-03-13 16:23:37 -07001129 const std::string& path = pathPair.first;
1130 const std::vector<
1131 std::pair<std::string, std::vector<std::string>>>& objDict =
1132 pathPair.second;
James Feist8bd25cc2019-03-15 15:14:00 -07001133 if (objDict.empty())
1134 {
1135 continue; // this should be impossible
1136 }
1137
1138 const std::string& owner = objDict.begin()->first;
Jonathan Doman1e1e5982021-06-11 09:36:17 -07001139 sdbusplus::asio::getProperty<std::vector<std::string>>(
1140 *crow::connections::systemBus,
1141 "xyz.openbmc_project.ObjectMapper", path + "/chassis",
1142 "xyz.openbmc_project.Association", "endpoints",
1143 [path, owner, sensorsAsyncResp](
1144 const boost::system::error_code e,
1145 const std::vector<std::string>& endpoints) {
Ed Tanous271584a2019-07-09 16:24:22 -07001146 if (e)
James Feist8bd25cc2019-03-15 15:14:00 -07001147 {
1148 return; // if they don't have an association we
1149 // can't tell what chassis is
1150 }
James Feist8bd25cc2019-03-15 15:14:00 -07001151 auto found = std::find_if(
Jonathan Doman1e1e5982021-06-11 09:36:17 -07001152 endpoints.begin(), endpoints.end(),
James Feist8bd25cc2019-03-15 15:14:00 -07001153 [sensorsAsyncResp](const std::string& entry) {
1154 return entry.find(
1155 sensorsAsyncResp->chassisId) !=
1156 std::string::npos;
1157 });
1158
Jonathan Doman1e1e5982021-06-11 09:36:17 -07001159 if (found == endpoints.end())
James Feist8bd25cc2019-03-15 15:14:00 -07001160 {
1161 return;
1162 }
1163 crow::connections::systemBus->async_method_call(
1164 [path, sensorsAsyncResp](
Ed Tanous271584a2019-07-09 16:24:22 -07001165 const boost::system::error_code& err,
James Feist8bd25cc2019-03-15 15:14:00 -07001166 const boost::container::flat_map<
1167 std::string,
Ed Tanous168e20c2021-12-13 14:39:53 -08001168 dbus::utility::DbusVariantType>& ret) {
Ed Tanous271584a2019-07-09 16:24:22 -07001169 if (err)
James Feist8bd25cc2019-03-15 15:14:00 -07001170 {
1171 return; // don't have to have this
1172 // interface
1173 }
1174 auto findFailures = ret.find("AllowedFailures");
1175 auto findCollection = ret.find("Collection");
1176 auto findStatus = ret.find("Status");
1177
1178 if (findFailures == ret.end() ||
1179 findCollection == ret.end() ||
1180 findStatus == ret.end())
1181 {
1182 BMCWEB_LOG_ERROR
1183 << "Invalid redundancy interface";
1184 messages::internalError(
zhanghch058d1b46d2021-04-01 11:18:24 +08001185 sensorsAsyncResp->asyncResp->res);
James Feist8bd25cc2019-03-15 15:14:00 -07001186 return;
1187 }
1188
Ed Tanous9eb808c2022-01-25 10:19:23 -08001189 const uint8_t* allowedFailures =
1190 std::get_if<uint8_t>(
1191 &(findFailures->second));
1192 const std::vector<std::string>* collection =
James Feist8bd25cc2019-03-15 15:14:00 -07001193 std::get_if<std::vector<std::string>>(
1194 &(findCollection->second));
Ed Tanous9eb808c2022-01-25 10:19:23 -08001195 const std::string* status =
1196 std::get_if<std::string>(
1197 &(findStatus->second));
James Feist8bd25cc2019-03-15 15:14:00 -07001198
1199 if (allowedFailures == nullptr ||
1200 collection == nullptr || status == nullptr)
1201 {
1202
1203 BMCWEB_LOG_ERROR
George Liu0fda0f12021-11-16 10:06:17 +08001204 << "Invalid redundancy interface types";
James Feist8bd25cc2019-03-15 15:14:00 -07001205 messages::internalError(
zhanghch058d1b46d2021-04-01 11:18:24 +08001206 sensorsAsyncResp->asyncResp->res);
James Feist8bd25cc2019-03-15 15:14:00 -07001207 return;
1208 }
George Liu28aa8de2021-02-01 15:13:30 +08001209 sdbusplus::message::object_path objectPath(
1210 path);
1211 std::string name = objectPath.filename();
1212 if (name.empty())
James Feist8bd25cc2019-03-15 15:14:00 -07001213 {
1214 // this should be impossible
1215 messages::internalError(
zhanghch058d1b46d2021-04-01 11:18:24 +08001216 sensorsAsyncResp->asyncResp->res);
James Feist8bd25cc2019-03-15 15:14:00 -07001217 return;
1218 }
James Feist8bd25cc2019-03-15 15:14:00 -07001219 std::replace(name.begin(), name.end(), '_',
1220 ' ');
1221
1222 std::string health;
1223
1224 if (boost::ends_with(*status, "Full"))
1225 {
1226 health = "OK";
1227 }
1228 else if (boost::ends_with(*status, "Degraded"))
1229 {
1230 health = "Warning";
1231 }
1232 else
1233 {
1234 health = "Critical";
1235 }
1236 std::vector<nlohmann::json> redfishCollection;
1237 const auto& fanRedfish =
zhanghch058d1b46d2021-04-01 11:18:24 +08001238 sensorsAsyncResp->asyncResp->res
1239 .jsonValue["Fans"];
James Feist8bd25cc2019-03-15 15:14:00 -07001240 for (const std::string& item : *collection)
1241 {
George Liu28aa8de2021-02-01 15:13:30 +08001242 sdbusplus::message::object_path path(item);
1243 std::string itemName = path.filename();
1244 if (itemName.empty())
1245 {
1246 continue;
1247 }
James Feist8bd25cc2019-03-15 15:14:00 -07001248 /*
1249 todo(ed): merge patch that fixes the names
1250 std::replace(itemName.begin(),
1251 itemName.end(), '_', ' ');*/
1252 auto schemaItem = std::find_if(
1253 fanRedfish.begin(), fanRedfish.end(),
1254 [itemName](const nlohmann::json& fan) {
1255 return fan["MemberId"] == itemName;
1256 });
1257 if (schemaItem != fanRedfish.end())
1258 {
1259 redfishCollection.push_back(
1260 {{"@odata.id",
1261 (*schemaItem)["@odata.id"]}});
1262 }
1263 else
1264 {
1265 BMCWEB_LOG_ERROR
1266 << "failed to find fan in schema";
1267 messages::internalError(
zhanghch058d1b46d2021-04-01 11:18:24 +08001268 sensorsAsyncResp->asyncResp->res);
James Feist8bd25cc2019-03-15 15:14:00 -07001269 return;
1270 }
1271 }
1272
Kuiying Wang3e9e72e2020-07-07 10:18:32 +08001273 size_t minNumNeeded =
Ed Tanous26f69762022-01-25 09:49:11 -08001274 collection->empty()
1275 ? 0
1276 : collection->size() - *allowedFailures;
Ed Tanous271584a2019-07-09 16:24:22 -07001277 nlohmann::json& jResp =
zhanghch058d1b46d2021-04-01 11:18:24 +08001278 sensorsAsyncResp->asyncResp->res
Ed Tanous271584a2019-07-09 16:24:22 -07001279 .jsonValue["Redundancy"];
1280 jResp.push_back(
James Feist8bd25cc2019-03-15 15:14:00 -07001281 {{"@odata.id",
AppaRao Puli717794d2019-10-18 22:54:53 +05301282 "/redfish/v1/Chassis/" +
James Feist8bd25cc2019-03-15 15:14:00 -07001283 sensorsAsyncResp->chassisId + "/" +
1284 sensorsAsyncResp->chassisSubNode +
1285 "#/Redundancy/" +
Ed Tanous271584a2019-07-09 16:24:22 -07001286 std::to_string(jResp.size())},
James Feist8bd25cc2019-03-15 15:14:00 -07001287 {"@odata.type",
1288 "#Redundancy.v1_3_2.Redundancy"},
Kuiying Wang3e9e72e2020-07-07 10:18:32 +08001289 {"MinNumNeeded", minNumNeeded},
James Feist8bd25cc2019-03-15 15:14:00 -07001290 {"MemberId", name},
1291 {"Mode", "N+m"},
1292 {"Name", name},
1293 {"RedundancySet", redfishCollection},
1294 {"Status",
1295 {{"Health", health},
1296 {"State", "Enabled"}}}});
1297 },
1298 owner, path, "org.freedesktop.DBus.Properties",
1299 "GetAll",
1300 "xyz.openbmc_project.Control.FanRedundancy");
Jonathan Doman1e1e5982021-06-11 09:36:17 -07001301 });
James Feist8bd25cc2019-03-15 15:14:00 -07001302 }
1303 },
1304 "xyz.openbmc_project.ObjectMapper",
1305 "/xyz/openbmc_project/object_mapper",
1306 "xyz.openbmc_project.ObjectMapper", "GetSubTree",
1307 "/xyz/openbmc_project/control", 2,
1308 std::array<const char*, 1>{
1309 "xyz.openbmc_project.Control.FanRedundancy"});
1310}
1311
Ed Tanousb5a76932020-09-29 16:16:58 -07001312inline void
Ed Tanous81ce6092020-12-17 16:54:55 +00001313 sortJSONResponse(const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07001314{
zhanghch058d1b46d2021-04-01 11:18:24 +08001315 nlohmann::json& response = sensorsAsyncResp->asyncResp->res.jsonValue;
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07001316 std::array<std::string, 2> sensorHeaders{"Temperatures", "Fans"};
Ed Tanous81ce6092020-12-17 16:54:55 +00001317 if (sensorsAsyncResp->chassisSubNode == sensors::node::power)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07001318 {
1319 sensorHeaders = {"Voltages", "PowerSupplies"};
1320 }
1321 for (const std::string& sensorGroup : sensorHeaders)
1322 {
1323 nlohmann::json::iterator entry = response.find(sensorGroup);
1324 if (entry != response.end())
1325 {
1326 std::sort(entry->begin(), entry->end(),
1327 [](nlohmann::json& c1, nlohmann::json& c2) {
1328 return c1["Name"] < c2["Name"];
1329 });
1330
1331 // add the index counts to the end of each entry
1332 size_t count = 0;
1333 for (nlohmann::json& sensorJson : *entry)
1334 {
1335 nlohmann::json::iterator odata = sensorJson.find("@odata.id");
1336 if (odata == sensorJson.end())
1337 {
1338 continue;
1339 }
1340 std::string* value = odata->get_ptr<std::string*>();
1341 if (value != nullptr)
1342 {
1343 *value += std::to_string(count);
1344 count++;
Ed Tanous81ce6092020-12-17 16:54:55 +00001345 sensorsAsyncResp->updateUri(sensorJson["Name"], *value);
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07001346 }
1347 }
1348 }
1349 }
1350}
1351
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +01001352/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001353 * @brief Finds the inventory item with the specified object path.
1354 * @param inventoryItems D-Bus inventory items associated with sensors.
1355 * @param invItemObjPath D-Bus object path of inventory item.
1356 * @return Inventory item within vector, or nullptr if no match found.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001357 */
Ed Tanous23a21a12020-07-25 04:45:05 +00001358inline InventoryItem* findInventoryItem(
Ed Tanousb5a76932020-09-29 16:16:58 -07001359 const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems,
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001360 const std::string& invItemObjPath)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001361{
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001362 for (InventoryItem& inventoryItem : *inventoryItems)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001363 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001364 if (inventoryItem.objectPath == invItemObjPath)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001365 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001366 return &inventoryItem;
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001367 }
1368 }
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001369 return nullptr;
1370}
1371
1372/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001373 * @brief Finds the inventory item associated with the specified sensor.
1374 * @param inventoryItems D-Bus inventory items associated with sensors.
1375 * @param sensorObjPath D-Bus object path of sensor.
1376 * @return Inventory item within vector, or nullptr if no match found.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001377 */
Ed Tanous23a21a12020-07-25 04:45:05 +00001378inline InventoryItem* findInventoryItemForSensor(
Ed Tanousb5a76932020-09-29 16:16:58 -07001379 const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems,
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001380 const std::string& sensorObjPath)
1381{
1382 for (InventoryItem& inventoryItem : *inventoryItems)
1383 {
1384 if (inventoryItem.sensors.count(sensorObjPath) > 0)
1385 {
1386 return &inventoryItem;
1387 }
1388 }
1389 return nullptr;
1390}
1391
1392/**
Anthony Wilsond5005492019-07-31 16:34:17 -05001393 * @brief Finds the inventory item associated with the specified led path.
1394 * @param inventoryItems D-Bus inventory items associated with sensors.
1395 * @param ledObjPath D-Bus object path of led.
1396 * @return Inventory item within vector, or nullptr if no match found.
1397 */
1398inline InventoryItem*
1399 findInventoryItemForLed(std::vector<InventoryItem>& inventoryItems,
1400 const std::string& ledObjPath)
1401{
1402 for (InventoryItem& inventoryItem : inventoryItems)
1403 {
1404 if (inventoryItem.ledObjectPath == ledObjPath)
1405 {
1406 return &inventoryItem;
1407 }
1408 }
1409 return nullptr;
1410}
1411
1412/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001413 * @brief Adds inventory item and associated sensor to specified vector.
1414 *
1415 * Adds a new InventoryItem to the vector if necessary. Searches for an
1416 * existing InventoryItem with the specified object path. If not found, one is
1417 * added to the vector.
1418 *
1419 * Next, the specified sensor is added to the set of sensors associated with the
1420 * InventoryItem.
1421 *
1422 * @param inventoryItems D-Bus inventory items associated with sensors.
1423 * @param invItemObjPath D-Bus object path of inventory item.
1424 * @param sensorObjPath D-Bus object path of sensor
1425 */
Ed Tanousb5a76932020-09-29 16:16:58 -07001426inline void addInventoryItem(
1427 const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems,
1428 const std::string& invItemObjPath, const std::string& sensorObjPath)
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001429{
1430 // Look for inventory item in vector
1431 InventoryItem* inventoryItem =
1432 findInventoryItem(inventoryItems, invItemObjPath);
1433
1434 // If inventory item doesn't exist in vector, add it
1435 if (inventoryItem == nullptr)
1436 {
1437 inventoryItems->emplace_back(invItemObjPath);
1438 inventoryItem = &(inventoryItems->back());
1439 }
1440
1441 // Add sensor to set of sensors associated with inventory item
1442 inventoryItem->sensors.emplace(sensorObjPath);
1443}
1444
1445/**
1446 * @brief Stores D-Bus data in the specified inventory item.
1447 *
1448 * Finds D-Bus data in the specified map of interfaces. Stores the data in the
1449 * specified InventoryItem.
1450 *
1451 * This data is later used to provide sensor property values in the JSON
1452 * response.
1453 *
1454 * @param inventoryItem Inventory item where data will be stored.
1455 * @param interfacesDict Map containing D-Bus interfaces and their properties
1456 * for the specified inventory item.
1457 */
Ed Tanous23a21a12020-07-25 04:45:05 +00001458inline void storeInventoryItemData(
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001459 InventoryItem& inventoryItem,
Ed Tanous711ac7a2021-12-20 09:34:41 -08001460 const dbus::utility::DBusInteracesMap& interfacesDict)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001461{
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001462 // Get properties from Inventory.Item interface
Ed Tanous711ac7a2021-12-20 09:34:41 -08001463
Ed Tanous9eb808c2022-01-25 10:19:23 -08001464 for (const auto& [interface, values] : interfacesDict)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001465 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001466 if (interface == "xyz.openbmc_project.Inventory.Item")
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001467 {
Ed Tanous9eb808c2022-01-25 10:19:23 -08001468 for (const auto& [name, dbusValue] : values)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001469 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001470 if (name == "Present")
1471 {
1472 const bool* value = std::get_if<bool>(&dbusValue);
1473 if (value != nullptr)
1474 {
1475 inventoryItem.isPresent = *value;
1476 }
1477 }
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001478 }
1479 }
Ed Tanous711ac7a2021-12-20 09:34:41 -08001480 // Check if Inventory.Item.PowerSupply interface is present
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001481
Ed Tanous711ac7a2021-12-20 09:34:41 -08001482 if (interface == "xyz.openbmc_project.Inventory.Item.PowerSupply")
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001483 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001484 inventoryItem.isPowerSupply = true;
1485 }
1486
1487 // Get properties from Inventory.Decorator.Asset interface
1488 if (interface == "xyz.openbmc_project.Inventory.Decorator.Asset")
1489 {
Ed Tanous9eb808c2022-01-25 10:19:23 -08001490 for (const auto& [name, dbusValue] : values)
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001491 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001492 if (name == "Manufacturer")
1493 {
1494 const std::string* value =
1495 std::get_if<std::string>(&dbusValue);
1496 if (value != nullptr)
1497 {
1498 inventoryItem.manufacturer = *value;
1499 }
1500 }
1501 if (name == "Model")
1502 {
1503 const std::string* value =
1504 std::get_if<std::string>(&dbusValue);
1505 if (value != nullptr)
1506 {
1507 inventoryItem.model = *value;
1508 }
1509 }
1510 if (name == "SerialNumber")
1511 {
1512 const std::string* value =
1513 std::get_if<std::string>(&dbusValue);
1514 if (value != nullptr)
1515 {
1516 inventoryItem.serialNumber = *value;
1517 }
1518 }
1519 if (name == "PartNumber")
1520 {
1521 const std::string* value =
1522 std::get_if<std::string>(&dbusValue);
1523 if (value != nullptr)
1524 {
1525 inventoryItem.partNumber = *value;
1526 }
1527 }
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001528 }
1529 }
1530
Ed Tanous711ac7a2021-12-20 09:34:41 -08001531 if (interface ==
1532 "xyz.openbmc_project.State.Decorator.OperationalStatus")
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001533 {
Ed Tanous9eb808c2022-01-25 10:19:23 -08001534 for (const auto& [name, dbusValue] : values)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001535 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001536 if (name == "Functional")
1537 {
1538 const bool* value = std::get_if<bool>(&dbusValue);
1539 if (value != nullptr)
1540 {
1541 inventoryItem.isFunctional = *value;
1542 }
1543 }
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001544 }
1545 }
1546 }
1547}
1548
1549/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001550 * @brief Gets D-Bus data for inventory items associated with sensors.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001551 *
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001552 * Uses the specified connections (services) to obtain D-Bus data for inventory
1553 * items associated with sensors. Stores the resulting data in the
1554 * inventoryItems vector.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001555 *
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001556 * This data is later used to provide sensor property values in the JSON
1557 * response.
1558 *
1559 * Finds the inventory item data asynchronously. Invokes callback when data has
1560 * been obtained.
1561 *
1562 * The callback must have the following signature:
1563 * @code
Anthony Wilsond5005492019-07-31 16:34:17 -05001564 * callback(void)
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001565 * @endcode
1566 *
1567 * This function is called recursively, obtaining data asynchronously from one
1568 * connection in each call. This ensures the callback is not invoked until the
1569 * last asynchronous function has completed.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001570 *
1571 * @param sensorsAsyncResp Pointer to object holding response data.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001572 * @param inventoryItems D-Bus inventory items associated with sensors.
1573 * @param invConnections Connections that provide data for the inventory items.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001574 * @param objectMgrPaths Mappings from connection name to DBus object path that
1575 * implements ObjectManager.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001576 * @param callback Callback to invoke when inventory data has been obtained.
1577 * @param invConnectionsIndex Current index in invConnections. Only specified
1578 * in recursive calls to this function.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001579 */
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001580template <typename Callback>
1581static void getInventoryItemsData(
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001582 std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001583 std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001584 std::shared_ptr<boost::container::flat_set<std::string>> invConnections,
1585 std::shared_ptr<boost::container::flat_map<std::string, std::string>>
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001586 objectMgrPaths,
Ed Tanous271584a2019-07-09 16:24:22 -07001587 Callback&& callback, size_t invConnectionsIndex = 0)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001588{
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001589 BMCWEB_LOG_DEBUG << "getInventoryItemsData enter";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001590
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001591 // If no more connections left, call callback
1592 if (invConnectionsIndex >= invConnections->size())
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001593 {
Anthony Wilsond5005492019-07-31 16:34:17 -05001594 callback();
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001595 BMCWEB_LOG_DEBUG << "getInventoryItemsData exit";
1596 return;
1597 }
1598
1599 // Get inventory item data from current connection
1600 auto it = invConnections->nth(invConnectionsIndex);
1601 if (it != invConnections->end())
1602 {
1603 const std::string& invConnection = *it;
1604
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001605 // Response handler for GetManagedObjects
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001606 auto respHandler = [sensorsAsyncResp, inventoryItems, invConnections,
Ed Tanousf94c4ec2022-01-06 12:44:41 -08001607 objectMgrPaths,
1608 callback{std::forward<Callback>(callback)},
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001609 invConnectionsIndex](
1610 const boost::system::error_code ec,
Ed Tanous711ac7a2021-12-20 09:34:41 -08001611 dbus::utility::ManagedObjectType& resp) {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001612 BMCWEB_LOG_DEBUG << "getInventoryItemsData respHandler enter";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001613 if (ec)
1614 {
1615 BMCWEB_LOG_ERROR
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001616 << "getInventoryItemsData respHandler DBus error " << ec;
zhanghch058d1b46d2021-04-01 11:18:24 +08001617 messages::internalError(sensorsAsyncResp->asyncResp->res);
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001618 return;
1619 }
1620
1621 // Loop through returned object paths
1622 for (const auto& objDictEntry : resp)
1623 {
1624 const std::string& objPath =
1625 static_cast<const std::string&>(objDictEntry.first);
1626
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001627 // If this object path is one of the specified inventory items
1628 InventoryItem* inventoryItem =
1629 findInventoryItem(inventoryItems, objPath);
1630 if (inventoryItem != nullptr)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001631 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001632 // Store inventory data in InventoryItem
1633 storeInventoryItemData(*inventoryItem, objDictEntry.second);
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001634 }
1635 }
1636
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001637 // Recurse to get inventory item data from next connection
1638 getInventoryItemsData(sensorsAsyncResp, inventoryItems,
1639 invConnections, objectMgrPaths,
1640 std::move(callback), invConnectionsIndex + 1);
1641
1642 BMCWEB_LOG_DEBUG << "getInventoryItemsData respHandler exit";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001643 };
1644
1645 // Find DBus object path that implements ObjectManager for the current
1646 // connection. If no mapping found, default to "/".
1647 auto iter = objectMgrPaths->find(invConnection);
1648 const std::string& objectMgrPath =
1649 (iter != objectMgrPaths->end()) ? iter->second : "/";
1650 BMCWEB_LOG_DEBUG << "ObjectManager path for " << invConnection << " is "
1651 << objectMgrPath;
1652
1653 // Get all object paths and their interfaces for current connection
1654 crow::connections::systemBus->async_method_call(
1655 std::move(respHandler), invConnection, objectMgrPath,
1656 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
1657 }
1658
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001659 BMCWEB_LOG_DEBUG << "getInventoryItemsData exit";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001660}
1661
1662/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001663 * @brief Gets connections that provide D-Bus data for inventory items.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001664 *
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001665 * Gets the D-Bus connections (services) that provide data for the inventory
1666 * items that are associated with sensors.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001667 *
1668 * Finds the connections asynchronously. Invokes callback when information has
1669 * been obtained.
1670 *
1671 * The callback must have the following signature:
1672 * @code
1673 * callback(std::shared_ptr<boost::container::flat_set<std::string>>
1674 * invConnections)
1675 * @endcode
1676 *
1677 * @param sensorsAsyncResp Pointer to object holding response data.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001678 * @param inventoryItems D-Bus inventory items associated with sensors.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001679 * @param callback Callback to invoke when connections have been obtained.
1680 */
1681template <typename Callback>
1682static void getInventoryItemsConnections(
Ed Tanousb5a76932020-09-29 16:16:58 -07001683 const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
1684 const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems,
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001685 Callback&& callback)
1686{
1687 BMCWEB_LOG_DEBUG << "getInventoryItemsConnections enter";
1688
1689 const std::string path = "/xyz/openbmc_project/inventory";
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001690 const std::array<std::string, 4> interfaces = {
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001691 "xyz.openbmc_project.Inventory.Item",
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001692 "xyz.openbmc_project.Inventory.Item.PowerSupply",
1693 "xyz.openbmc_project.Inventory.Decorator.Asset",
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001694 "xyz.openbmc_project.State.Decorator.OperationalStatus"};
1695
1696 // Response handler for parsing output from GetSubTree
Ed Tanousf94c4ec2022-01-06 12:44:41 -08001697 auto respHandler = [callback{std::forward<Callback>(callback)},
Ed Tanousb9d36b42022-02-26 21:42:46 -08001698 sensorsAsyncResp, inventoryItems](
1699 const boost::system::error_code ec,
1700 const dbus::utility::MapperGetSubTreeResponse&
1701 subtree) {
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001702 BMCWEB_LOG_DEBUG << "getInventoryItemsConnections respHandler enter";
1703 if (ec)
1704 {
zhanghch058d1b46d2021-04-01 11:18:24 +08001705 messages::internalError(sensorsAsyncResp->asyncResp->res);
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001706 BMCWEB_LOG_ERROR
1707 << "getInventoryItemsConnections respHandler DBus error " << ec;
1708 return;
1709 }
1710
1711 // Make unique list of connections for desired inventory items
1712 std::shared_ptr<boost::container::flat_set<std::string>>
1713 invConnections =
1714 std::make_shared<boost::container::flat_set<std::string>>();
1715 invConnections->reserve(8);
1716
1717 // Loop through objects from GetSubTree
1718 for (const std::pair<
1719 std::string,
1720 std::vector<std::pair<std::string, std::vector<std::string>>>>&
1721 object : subtree)
1722 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001723 // Check if object path is one of the specified inventory items
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001724 const std::string& objPath = object.first;
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001725 if (findInventoryItem(inventoryItems, objPath) != nullptr)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001726 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001727 // Store all connections to inventory item
1728 for (const std::pair<std::string, std::vector<std::string>>&
1729 objData : object.second)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001730 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001731 const std::string& invConnection = objData.first;
1732 invConnections->insert(invConnection);
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001733 }
1734 }
1735 }
Anthony Wilsond5005492019-07-31 16:34:17 -05001736
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001737 callback(invConnections);
1738 BMCWEB_LOG_DEBUG << "getInventoryItemsConnections respHandler exit";
1739 };
1740
1741 // Make call to ObjectMapper to find all inventory items
1742 crow::connections::systemBus->async_method_call(
1743 std::move(respHandler), "xyz.openbmc_project.ObjectMapper",
1744 "/xyz/openbmc_project/object_mapper",
1745 "xyz.openbmc_project.ObjectMapper", "GetSubTree", path, 0, interfaces);
1746 BMCWEB_LOG_DEBUG << "getInventoryItemsConnections exit";
1747}
1748
1749/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001750 * @brief Gets associations from sensors to inventory items.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001751 *
1752 * Looks for ObjectMapper associations from the specified sensors to related
Anthony Wilsond5005492019-07-31 16:34:17 -05001753 * inventory items. Then finds the associations from those inventory items to
1754 * their LEDs, if any.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001755 *
1756 * Finds the inventory items asynchronously. Invokes callback when information
1757 * has been obtained.
1758 *
1759 * The callback must have the following signature:
1760 * @code
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001761 * callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001762 * @endcode
1763 *
1764 * @param sensorsAsyncResp Pointer to object holding response data.
1765 * @param sensorNames All sensors within the current chassis.
1766 * @param objectMgrPaths Mappings from connection name to DBus object path that
1767 * implements ObjectManager.
1768 * @param callback Callback to invoke when inventory items have been obtained.
1769 */
1770template <typename Callback>
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001771static void getInventoryItemAssociations(
Ed Tanousb5a76932020-09-29 16:16:58 -07001772 const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
1773 const std::shared_ptr<boost::container::flat_set<std::string>>& sensorNames,
1774 const std::shared_ptr<boost::container::flat_map<std::string, std::string>>&
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001775 objectMgrPaths,
1776 Callback&& callback)
1777{
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001778 BMCWEB_LOG_DEBUG << "getInventoryItemAssociations enter";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001779
1780 // Response handler for GetManagedObjects
Ed Tanousf94c4ec2022-01-06 12:44:41 -08001781 auto respHandler = [callback{std::forward<Callback>(callback)},
1782 sensorsAsyncResp,
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001783 sensorNames](const boost::system::error_code ec,
1784 dbus::utility::ManagedObjectType& resp) {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001785 BMCWEB_LOG_DEBUG << "getInventoryItemAssociations respHandler enter";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001786 if (ec)
1787 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001788 BMCWEB_LOG_ERROR
1789 << "getInventoryItemAssociations respHandler DBus error " << ec;
zhanghch058d1b46d2021-04-01 11:18:24 +08001790 messages::internalError(sensorsAsyncResp->asyncResp->res);
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001791 return;
1792 }
1793
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001794 // Create vector to hold list of inventory items
1795 std::shared_ptr<std::vector<InventoryItem>> inventoryItems =
1796 std::make_shared<std::vector<InventoryItem>>();
1797
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001798 // Loop through returned object paths
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001799 std::string sensorAssocPath;
1800 sensorAssocPath.reserve(128); // avoid memory allocations
1801 for (const auto& objDictEntry : resp)
1802 {
1803 const std::string& objPath =
1804 static_cast<const std::string&>(objDictEntry.first);
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001805
1806 // If path is inventory association for one of the specified sensors
1807 for (const std::string& sensorName : *sensorNames)
1808 {
1809 sensorAssocPath = sensorName;
1810 sensorAssocPath += "/inventory";
1811 if (objPath == sensorAssocPath)
1812 {
1813 // Get Association interface for object path
Ed Tanous711ac7a2021-12-20 09:34:41 -08001814 for (const auto& [interface, values] : objDictEntry.second)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001815 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001816 if (interface == "xyz.openbmc_project.Association")
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001817 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001818 for (const auto& [valueName, value] : values)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001819 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001820 if (valueName == "endpoints")
1821 {
1822 const std::vector<std::string>* endpoints =
1823 std::get_if<std::vector<std::string>>(
1824 &value);
1825 if ((endpoints != nullptr) &&
1826 !endpoints->empty())
1827 {
1828 // Add inventory item to vector
1829 const std::string& invItemPath =
1830 endpoints->front();
1831 addInventoryItem(inventoryItems,
1832 invItemPath,
1833 sensorName);
1834 }
1835 }
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001836 }
1837 }
1838 }
1839 break;
1840 }
1841 }
1842 }
1843
Anthony Wilsond5005492019-07-31 16:34:17 -05001844 // Now loop through the returned object paths again, this time to
1845 // find the leds associated with the inventory items we just found
1846 std::string inventoryAssocPath;
1847 inventoryAssocPath.reserve(128); // avoid memory allocations
1848 for (const auto& objDictEntry : resp)
1849 {
1850 const std::string& objPath =
1851 static_cast<const std::string&>(objDictEntry.first);
Anthony Wilsond5005492019-07-31 16:34:17 -05001852
1853 for (InventoryItem& inventoryItem : *inventoryItems)
1854 {
1855 inventoryAssocPath = inventoryItem.objectPath;
1856 inventoryAssocPath += "/leds";
1857 if (objPath == inventoryAssocPath)
1858 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001859 for (const auto& [interface, values] : objDictEntry.second)
Anthony Wilsond5005492019-07-31 16:34:17 -05001860 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001861 if (interface == "xyz.openbmc_project.Association")
Anthony Wilsond5005492019-07-31 16:34:17 -05001862 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001863 for (const auto& [valueName, value] : values)
Anthony Wilsond5005492019-07-31 16:34:17 -05001864 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001865 if (valueName == "endpoints")
1866 {
1867 const std::vector<std::string>* endpoints =
1868 std::get_if<std::vector<std::string>>(
1869 &value);
1870 if ((endpoints != nullptr) &&
1871 !endpoints->empty())
1872 {
1873 // Add inventory item to vector
1874 // Store LED path in inventory item
1875 const std::string& ledPath =
1876 endpoints->front();
1877 inventoryItem.ledObjectPath = ledPath;
1878 }
1879 }
Anthony Wilsond5005492019-07-31 16:34:17 -05001880 }
1881 }
1882 }
Ed Tanous711ac7a2021-12-20 09:34:41 -08001883
Anthony Wilsond5005492019-07-31 16:34:17 -05001884 break;
1885 }
1886 }
1887 }
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001888 callback(inventoryItems);
1889 BMCWEB_LOG_DEBUG << "getInventoryItemAssociations respHandler exit";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001890 };
1891
1892 // Find DBus object path that implements ObjectManager for ObjectMapper
1893 std::string connection = "xyz.openbmc_project.ObjectMapper";
1894 auto iter = objectMgrPaths->find(connection);
1895 const std::string& objectMgrPath =
1896 (iter != objectMgrPaths->end()) ? iter->second : "/";
1897 BMCWEB_LOG_DEBUG << "ObjectManager path for " << connection << " is "
1898 << objectMgrPath;
1899
1900 // Call GetManagedObjects on the ObjectMapper to get all associations
1901 crow::connections::systemBus->async_method_call(
1902 std::move(respHandler), connection, objectMgrPath,
1903 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
1904
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001905 BMCWEB_LOG_DEBUG << "getInventoryItemAssociations exit";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001906}
1907
1908/**
Anthony Wilsond5005492019-07-31 16:34:17 -05001909 * @brief Gets D-Bus data for inventory item leds associated with sensors.
1910 *
1911 * Uses the specified connections (services) to obtain D-Bus data for inventory
1912 * item leds associated with sensors. Stores the resulting data in the
1913 * inventoryItems vector.
1914 *
1915 * This data is later used to provide sensor property values in the JSON
1916 * response.
1917 *
1918 * Finds the inventory item led data asynchronously. Invokes callback when data
1919 * has been obtained.
1920 *
1921 * The callback must have the following signature:
1922 * @code
Gunnar Mills42cbe532019-08-15 15:26:54 -05001923 * callback()
Anthony Wilsond5005492019-07-31 16:34:17 -05001924 * @endcode
1925 *
1926 * This function is called recursively, obtaining data asynchronously from one
1927 * connection in each call. This ensures the callback is not invoked until the
1928 * last asynchronous function has completed.
1929 *
1930 * @param sensorsAsyncResp Pointer to object holding response data.
1931 * @param inventoryItems D-Bus inventory items associated with sensors.
1932 * @param ledConnections Connections that provide data for the inventory leds.
1933 * @param callback Callback to invoke when inventory data has been obtained.
1934 * @param ledConnectionsIndex Current index in ledConnections. Only specified
1935 * in recursive calls to this function.
1936 */
1937template <typename Callback>
1938void getInventoryLedData(
1939 std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
1940 std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
1941 std::shared_ptr<boost::container::flat_map<std::string, std::string>>
1942 ledConnections,
1943 Callback&& callback, size_t ledConnectionsIndex = 0)
1944{
1945 BMCWEB_LOG_DEBUG << "getInventoryLedData enter";
1946
1947 // If no more connections left, call callback
1948 if (ledConnectionsIndex >= ledConnections->size())
1949 {
Gunnar Mills42cbe532019-08-15 15:26:54 -05001950 callback();
Anthony Wilsond5005492019-07-31 16:34:17 -05001951 BMCWEB_LOG_DEBUG << "getInventoryLedData exit";
1952 return;
1953 }
1954
1955 // Get inventory item data from current connection
1956 auto it = ledConnections->nth(ledConnectionsIndex);
1957 if (it != ledConnections->end())
1958 {
1959 const std::string& ledPath = (*it).first;
1960 const std::string& ledConnection = (*it).second;
1961 // Response handler for Get State property
Jonathan Doman1e1e5982021-06-11 09:36:17 -07001962 auto respHandler =
1963 [sensorsAsyncResp, inventoryItems, ledConnections, ledPath,
Ed Tanousf94c4ec2022-01-06 12:44:41 -08001964 callback{std::forward<Callback>(callback)}, ledConnectionsIndex](
Jonathan Doman1e1e5982021-06-11 09:36:17 -07001965 const boost::system::error_code ec, const std::string& state) {
1966 BMCWEB_LOG_DEBUG << "getInventoryLedData respHandler enter";
1967 if (ec)
1968 {
1969 BMCWEB_LOG_ERROR
1970 << "getInventoryLedData respHandler DBus error " << ec;
1971 messages::internalError(sensorsAsyncResp->asyncResp->res);
1972 return;
1973 }
Anthony Wilsond5005492019-07-31 16:34:17 -05001974
Jonathan Doman1e1e5982021-06-11 09:36:17 -07001975 BMCWEB_LOG_DEBUG << "Led state: " << state;
Ed Tanous168e20c2021-12-13 14:39:53 -08001976 // Find inventory item with this LED object path
1977 InventoryItem* inventoryItem =
1978 findInventoryItemForLed(*inventoryItems, ledPath);
1979 if (inventoryItem != nullptr)
Anthony Wilsond5005492019-07-31 16:34:17 -05001980 {
Ed Tanous168e20c2021-12-13 14:39:53 -08001981 // Store LED state in InventoryItem
Jonathan Doman1e1e5982021-06-11 09:36:17 -07001982 if (boost::ends_with(state, "On"))
Anthony Wilsond5005492019-07-31 16:34:17 -05001983 {
Ed Tanous168e20c2021-12-13 14:39:53 -08001984 inventoryItem->ledState = LedState::ON;
1985 }
Jonathan Doman1e1e5982021-06-11 09:36:17 -07001986 else if (boost::ends_with(state, "Blink"))
Ed Tanous168e20c2021-12-13 14:39:53 -08001987 {
1988 inventoryItem->ledState = LedState::BLINK;
1989 }
Jonathan Doman1e1e5982021-06-11 09:36:17 -07001990 else if (boost::ends_with(state, "Off"))
Ed Tanous168e20c2021-12-13 14:39:53 -08001991 {
1992 inventoryItem->ledState = LedState::OFF;
1993 }
1994 else
1995 {
1996 inventoryItem->ledState = LedState::UNKNOWN;
Anthony Wilsond5005492019-07-31 16:34:17 -05001997 }
1998 }
Anthony Wilsond5005492019-07-31 16:34:17 -05001999
Jonathan Doman1e1e5982021-06-11 09:36:17 -07002000 // Recurse to get LED data from next connection
2001 getInventoryLedData(sensorsAsyncResp, inventoryItems,
2002 ledConnections, std::move(callback),
2003 ledConnectionsIndex + 1);
Anthony Wilsond5005492019-07-31 16:34:17 -05002004
Jonathan Doman1e1e5982021-06-11 09:36:17 -07002005 BMCWEB_LOG_DEBUG << "getInventoryLedData respHandler exit";
2006 };
Anthony Wilsond5005492019-07-31 16:34:17 -05002007
2008 // Get the State property for the current LED
Jonathan Doman1e1e5982021-06-11 09:36:17 -07002009 sdbusplus::asio::getProperty<std::string>(
2010 *crow::connections::systemBus, ledConnection, ledPath,
2011 "xyz.openbmc_project.Led.Physical", "State",
2012 std::move(respHandler));
Anthony Wilsond5005492019-07-31 16:34:17 -05002013 }
2014
2015 BMCWEB_LOG_DEBUG << "getInventoryLedData exit";
2016}
2017
2018/**
2019 * @brief Gets LED data for LEDs associated with given inventory items.
2020 *
2021 * Gets the D-Bus connections (services) that provide LED data for the LEDs
2022 * associated with the specified inventory items. Then gets the LED data from
2023 * each connection and stores it in the inventory item.
2024 *
2025 * This data is later used to provide sensor property values in the JSON
2026 * response.
2027 *
2028 * Finds the LED data asynchronously. Invokes callback when information has
2029 * been obtained.
2030 *
2031 * The callback must have the following signature:
2032 * @code
Gunnar Mills42cbe532019-08-15 15:26:54 -05002033 * callback()
Anthony Wilsond5005492019-07-31 16:34:17 -05002034 * @endcode
2035 *
2036 * @param sensorsAsyncResp Pointer to object holding response data.
2037 * @param inventoryItems D-Bus inventory items associated with sensors.
2038 * @param callback Callback to invoke when inventory items have been obtained.
2039 */
2040template <typename Callback>
2041void getInventoryLeds(
2042 std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
2043 std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
2044 Callback&& callback)
2045{
2046 BMCWEB_LOG_DEBUG << "getInventoryLeds enter";
2047
2048 const std::string path = "/xyz/openbmc_project";
2049 const std::array<std::string, 1> interfaces = {
2050 "xyz.openbmc_project.Led.Physical"};
2051
2052 // Response handler for parsing output from GetSubTree
Ed Tanousf94c4ec2022-01-06 12:44:41 -08002053 auto respHandler = [callback{std::forward<Callback>(callback)},
Ed Tanousb9d36b42022-02-26 21:42:46 -08002054 sensorsAsyncResp, inventoryItems](
2055 const boost::system::error_code ec,
2056 const dbus::utility::MapperGetSubTreeResponse&
2057 subtree) {
Anthony Wilsond5005492019-07-31 16:34:17 -05002058 BMCWEB_LOG_DEBUG << "getInventoryLeds respHandler enter";
2059 if (ec)
2060 {
zhanghch058d1b46d2021-04-01 11:18:24 +08002061 messages::internalError(sensorsAsyncResp->asyncResp->res);
Anthony Wilsond5005492019-07-31 16:34:17 -05002062 BMCWEB_LOG_ERROR << "getInventoryLeds respHandler DBus error "
2063 << ec;
2064 return;
2065 }
2066
2067 // Build map of LED object paths to connections
2068 std::shared_ptr<boost::container::flat_map<std::string, std::string>>
2069 ledConnections = std::make_shared<
2070 boost::container::flat_map<std::string, std::string>>();
2071
2072 // Loop through objects from GetSubTree
2073 for (const std::pair<
2074 std::string,
2075 std::vector<std::pair<std::string, std::vector<std::string>>>>&
2076 object : subtree)
2077 {
2078 // Check if object path is LED for one of the specified inventory
2079 // items
2080 const std::string& ledPath = object.first;
2081 if (findInventoryItemForLed(*inventoryItems, ledPath) != nullptr)
2082 {
2083 // Add mapping from ledPath to connection
2084 const std::string& connection = object.second.begin()->first;
2085 (*ledConnections)[ledPath] = connection;
2086 BMCWEB_LOG_DEBUG << "Added mapping " << ledPath << " -> "
2087 << connection;
2088 }
2089 }
2090
2091 getInventoryLedData(sensorsAsyncResp, inventoryItems, ledConnections,
2092 std::move(callback));
2093 BMCWEB_LOG_DEBUG << "getInventoryLeds respHandler exit";
2094 };
2095 // Make call to ObjectMapper to find all inventory items
2096 crow::connections::systemBus->async_method_call(
2097 std::move(respHandler), "xyz.openbmc_project.ObjectMapper",
2098 "/xyz/openbmc_project/object_mapper",
2099 "xyz.openbmc_project.ObjectMapper", "GetSubTree", path, 0, interfaces);
2100 BMCWEB_LOG_DEBUG << "getInventoryLeds exit";
2101}
2102
2103/**
Gunnar Mills42cbe532019-08-15 15:26:54 -05002104 * @brief Gets D-Bus data for Power Supply Attributes such as EfficiencyPercent
2105 *
2106 * Uses the specified connections (services) (currently assumes just one) to
2107 * obtain D-Bus data for Power Supply Attributes. Stores the resulting data in
2108 * the inventoryItems vector. Only stores data in Power Supply inventoryItems.
2109 *
2110 * This data is later used to provide sensor property values in the JSON
2111 * response.
2112 *
2113 * Finds the Power Supply Attributes data asynchronously. Invokes callback
2114 * when data has been obtained.
2115 *
2116 * The callback must have the following signature:
2117 * @code
2118 * callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems)
2119 * @endcode
2120 *
2121 * @param sensorsAsyncResp Pointer to object holding response data.
2122 * @param inventoryItems D-Bus inventory items associated with sensors.
2123 * @param psAttributesConnections Connections that provide data for the Power
2124 * Supply Attributes
2125 * @param callback Callback to invoke when data has been obtained.
2126 */
2127template <typename Callback>
2128void getPowerSupplyAttributesData(
Ed Tanousb5a76932020-09-29 16:16:58 -07002129 const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
Gunnar Mills42cbe532019-08-15 15:26:54 -05002130 std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
2131 const boost::container::flat_map<std::string, std::string>&
2132 psAttributesConnections,
2133 Callback&& callback)
2134{
2135 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributesData enter";
2136
2137 if (psAttributesConnections.empty())
2138 {
2139 BMCWEB_LOG_DEBUG << "Can't find PowerSupplyAttributes, no connections!";
2140 callback(inventoryItems);
2141 return;
2142 }
2143
2144 // Assuming just one connection (service) for now
2145 auto it = psAttributesConnections.nth(0);
2146
2147 const std::string& psAttributesPath = (*it).first;
2148 const std::string& psAttributesConnection = (*it).second;
2149
2150 // Response handler for Get DeratingFactor property
2151 auto respHandler = [sensorsAsyncResp, inventoryItems,
Ed Tanousf94c4ec2022-01-06 12:44:41 -08002152 callback{std::forward<Callback>(callback)}](
Gunnar Mills42cbe532019-08-15 15:26:54 -05002153 const boost::system::error_code ec,
Jonathan Doman1e1e5982021-06-11 09:36:17 -07002154 const uint32_t value) {
Gunnar Mills42cbe532019-08-15 15:26:54 -05002155 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributesData respHandler enter";
2156 if (ec)
2157 {
2158 BMCWEB_LOG_ERROR
2159 << "getPowerSupplyAttributesData respHandler DBus error " << ec;
zhanghch058d1b46d2021-04-01 11:18:24 +08002160 messages::internalError(sensorsAsyncResp->asyncResp->res);
Gunnar Mills42cbe532019-08-15 15:26:54 -05002161 return;
2162 }
2163
Jonathan Doman1e1e5982021-06-11 09:36:17 -07002164 BMCWEB_LOG_DEBUG << "PS EfficiencyPercent value: " << value;
2165 // Store value in Power Supply Inventory Items
2166 for (InventoryItem& inventoryItem : *inventoryItems)
Gunnar Mills42cbe532019-08-15 15:26:54 -05002167 {
Ed Tanous55f79e62022-01-25 11:26:16 -08002168 if (inventoryItem.isPowerSupply)
Gunnar Mills42cbe532019-08-15 15:26:54 -05002169 {
Jonathan Doman1e1e5982021-06-11 09:36:17 -07002170 inventoryItem.powerSupplyEfficiencyPercent =
2171 static_cast<int>(value);
Gunnar Mills42cbe532019-08-15 15:26:54 -05002172 }
2173 }
Gunnar Mills42cbe532019-08-15 15:26:54 -05002174
2175 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributesData respHandler exit";
2176 callback(inventoryItems);
2177 };
2178
2179 // Get the DeratingFactor property for the PowerSupplyAttributes
2180 // Currently only property on the interface/only one we care about
Jonathan Doman1e1e5982021-06-11 09:36:17 -07002181 sdbusplus::asio::getProperty<uint32_t>(
2182 *crow::connections::systemBus, psAttributesConnection, psAttributesPath,
2183 "xyz.openbmc_project.Control.PowerSupplyAttributes", "DeratingFactor",
2184 std::move(respHandler));
Gunnar Mills42cbe532019-08-15 15:26:54 -05002185
2186 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributesData exit";
2187}
2188
2189/**
2190 * @brief Gets the Power Supply Attributes such as EfficiencyPercent
2191 *
2192 * Gets the D-Bus connection (service) that provides Power Supply Attributes
2193 * data. Then gets the Power Supply Attributes data from the connection
2194 * (currently just assumes 1 connection) and stores the data in the inventory
2195 * item.
2196 *
2197 * This data is later used to provide sensor property values in the JSON
2198 * response. DeratingFactor on D-Bus is mapped to EfficiencyPercent on Redfish.
2199 *
2200 * Finds the Power Supply Attributes data asynchronously. Invokes callback
2201 * when information has been obtained.
2202 *
2203 * The callback must have the following signature:
2204 * @code
2205 * callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems)
2206 * @endcode
2207 *
2208 * @param sensorsAsyncResp Pointer to object holding response data.
2209 * @param inventoryItems D-Bus inventory items associated with sensors.
2210 * @param callback Callback to invoke when data has been obtained.
2211 */
2212template <typename Callback>
2213void getPowerSupplyAttributes(
2214 std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
2215 std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
2216 Callback&& callback)
2217{
2218 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes enter";
2219
2220 // Only need the power supply attributes when the Power Schema
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02002221 if (sensorsAsyncResp->chassisSubNode != sensors::node::power)
Gunnar Mills42cbe532019-08-15 15:26:54 -05002222 {
2223 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes exit since not Power";
2224 callback(inventoryItems);
2225 return;
2226 }
2227
2228 const std::array<std::string, 1> interfaces = {
2229 "xyz.openbmc_project.Control.PowerSupplyAttributes"};
2230
2231 // Response handler for parsing output from GetSubTree
Ed Tanousb9d36b42022-02-26 21:42:46 -08002232 auto respHandler =
2233 [callback{std::forward<Callback>(callback)}, sensorsAsyncResp,
2234 inventoryItems](
2235 const boost::system::error_code ec,
2236 const dbus::utility::MapperGetSubTreeResponse& subtree) {
2237 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes respHandler enter";
2238 if (ec)
2239 {
2240 messages::internalError(sensorsAsyncResp->asyncResp->res);
2241 BMCWEB_LOG_ERROR
2242 << "getPowerSupplyAttributes respHandler DBus error " << ec;
2243 return;
2244 }
2245 if (subtree.empty())
2246 {
2247 BMCWEB_LOG_DEBUG << "Can't find Power Supply Attributes!";
2248 callback(inventoryItems);
2249 return;
2250 }
Gunnar Mills42cbe532019-08-15 15:26:54 -05002251
Ed Tanousb9d36b42022-02-26 21:42:46 -08002252 // Currently we only support 1 power supply attribute, use this for
2253 // all the power supplies. Build map of object path to connection.
2254 // Assume just 1 connection and 1 path for now.
2255 boost::container::flat_map<std::string, std::string>
2256 psAttributesConnections;
Gunnar Mills42cbe532019-08-15 15:26:54 -05002257
Ed Tanousb9d36b42022-02-26 21:42:46 -08002258 if (subtree[0].first.empty() || subtree[0].second.empty())
2259 {
2260 BMCWEB_LOG_DEBUG << "Power Supply Attributes mapper error!";
2261 callback(inventoryItems);
2262 return;
2263 }
Gunnar Mills42cbe532019-08-15 15:26:54 -05002264
Ed Tanousb9d36b42022-02-26 21:42:46 -08002265 const std::string& psAttributesPath = subtree[0].first;
2266 const std::string& connection = subtree[0].second.begin()->first;
Gunnar Mills42cbe532019-08-15 15:26:54 -05002267
Ed Tanousb9d36b42022-02-26 21:42:46 -08002268 if (connection.empty())
2269 {
2270 BMCWEB_LOG_DEBUG << "Power Supply Attributes mapper error!";
2271 callback(inventoryItems);
2272 return;
2273 }
Gunnar Mills42cbe532019-08-15 15:26:54 -05002274
Ed Tanousb9d36b42022-02-26 21:42:46 -08002275 psAttributesConnections[psAttributesPath] = connection;
2276 BMCWEB_LOG_DEBUG << "Added mapping " << psAttributesPath << " -> "
2277 << connection;
Gunnar Mills42cbe532019-08-15 15:26:54 -05002278
Ed Tanousb9d36b42022-02-26 21:42:46 -08002279 getPowerSupplyAttributesData(sensorsAsyncResp, inventoryItems,
2280 psAttributesConnections,
2281 std::move(callback));
2282 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes respHandler exit";
2283 };
Gunnar Mills42cbe532019-08-15 15:26:54 -05002284 // Make call to ObjectMapper to find the PowerSupplyAttributes service
2285 crow::connections::systemBus->async_method_call(
2286 std::move(respHandler), "xyz.openbmc_project.ObjectMapper",
2287 "/xyz/openbmc_project/object_mapper",
2288 "xyz.openbmc_project.ObjectMapper", "GetSubTree",
2289 "/xyz/openbmc_project", 0, interfaces);
2290 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes exit";
2291}
2292
2293/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002294 * @brief Gets inventory items associated with sensors.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002295 *
2296 * Finds the inventory items that are associated with the specified sensors.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002297 * Then gets D-Bus data for the inventory items, such as presence and VPD.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002298 *
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002299 * This data is later used to provide sensor property values in the JSON
2300 * response.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002301 *
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002302 * Finds the inventory items asynchronously. Invokes callback when the
2303 * inventory items have been obtained.
2304 *
2305 * The callback must have the following signature:
2306 * @code
2307 * callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems)
2308 * @endcode
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002309 *
2310 * @param sensorsAsyncResp Pointer to object holding response data.
2311 * @param sensorNames All sensors within the current chassis.
2312 * @param objectMgrPaths Mappings from connection name to DBus object path that
2313 * implements ObjectManager.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002314 * @param callback Callback to invoke when inventory items have been obtained.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002315 */
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002316template <typename Callback>
2317static void getInventoryItems(
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002318 std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
2319 const std::shared_ptr<boost::container::flat_set<std::string>> sensorNames,
2320 std::shared_ptr<boost::container::flat_map<std::string, std::string>>
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002321 objectMgrPaths,
2322 Callback&& callback)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002323{
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002324 BMCWEB_LOG_DEBUG << "getInventoryItems enter";
2325 auto getInventoryItemAssociationsCb =
Ed Tanousf94c4ec2022-01-06 12:44:41 -08002326 [sensorsAsyncResp, objectMgrPaths,
2327 callback{std::forward<Callback>(callback)}](
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002328 std::shared_ptr<std::vector<InventoryItem>> inventoryItems) {
2329 BMCWEB_LOG_DEBUG << "getInventoryItemAssociationsCb enter";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002330 auto getInventoryItemsConnectionsCb =
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002331 [sensorsAsyncResp, inventoryItems, objectMgrPaths,
Ed Tanousf94c4ec2022-01-06 12:44:41 -08002332 callback{std::forward<const Callback>(callback)}](
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002333 std::shared_ptr<boost::container::flat_set<std::string>>
2334 invConnections) {
2335 BMCWEB_LOG_DEBUG << "getInventoryItemsConnectionsCb enter";
Anthony Wilsond5005492019-07-31 16:34:17 -05002336 auto getInventoryItemsDataCb =
2337 [sensorsAsyncResp, inventoryItems,
2338 callback{std::move(callback)}]() {
2339 BMCWEB_LOG_DEBUG << "getInventoryItemsDataCb enter";
Gunnar Mills42cbe532019-08-15 15:26:54 -05002340
2341 auto getInventoryLedsCb = [sensorsAsyncResp,
2342 inventoryItems,
2343 callback{std::move(
2344 callback)}]() {
2345 BMCWEB_LOG_DEBUG << "getInventoryLedsCb enter";
2346 // Find Power Supply Attributes and get the data
2347 getPowerSupplyAttributes(sensorsAsyncResp,
2348 inventoryItems,
2349 std::move(callback));
2350 BMCWEB_LOG_DEBUG << "getInventoryLedsCb exit";
2351 };
2352
Anthony Wilsond5005492019-07-31 16:34:17 -05002353 // Find led connections and get the data
2354 getInventoryLeds(sensorsAsyncResp, inventoryItems,
Gunnar Mills42cbe532019-08-15 15:26:54 -05002355 std::move(getInventoryLedsCb));
Anthony Wilsond5005492019-07-31 16:34:17 -05002356 BMCWEB_LOG_DEBUG << "getInventoryItemsDataCb exit";
2357 };
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002358
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002359 // Get inventory item data from connections
2360 getInventoryItemsData(sensorsAsyncResp, inventoryItems,
2361 invConnections, objectMgrPaths,
Anthony Wilsond5005492019-07-31 16:34:17 -05002362 std::move(getInventoryItemsDataCb));
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002363 BMCWEB_LOG_DEBUG << "getInventoryItemsConnectionsCb exit";
2364 };
2365
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002366 // Get connections that provide inventory item data
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002367 getInventoryItemsConnections(
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002368 sensorsAsyncResp, inventoryItems,
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002369 std::move(getInventoryItemsConnectionsCb));
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002370 BMCWEB_LOG_DEBUG << "getInventoryItemAssociationsCb exit";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002371 };
2372
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002373 // Get associations from sensors to inventory items
2374 getInventoryItemAssociations(sensorsAsyncResp, sensorNames, objectMgrPaths,
2375 std::move(getInventoryItemAssociationsCb));
2376 BMCWEB_LOG_DEBUG << "getInventoryItems exit";
2377}
2378
2379/**
2380 * @brief Returns JSON PowerSupply object for the specified inventory item.
2381 *
2382 * Searches for a JSON PowerSupply object that matches the specified inventory
2383 * item. If one is not found, a new PowerSupply object is added to the JSON
2384 * array.
2385 *
2386 * Multiple sensors are often associated with one power supply inventory item.
2387 * As a result, multiple sensor values are stored in one JSON PowerSupply
2388 * object.
2389 *
2390 * @param powerSupplyArray JSON array containing Redfish PowerSupply objects.
2391 * @param inventoryItem Inventory item for the power supply.
2392 * @param chassisId Chassis that contains the power supply.
2393 * @return JSON PowerSupply object for the specified inventory item.
2394 */
Ed Tanous23a21a12020-07-25 04:45:05 +00002395inline nlohmann::json& getPowerSupply(nlohmann::json& powerSupplyArray,
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002396 const InventoryItem& inventoryItem,
2397 const std::string& chassisId)
2398{
2399 // Check if matching PowerSupply object already exists in JSON array
2400 for (nlohmann::json& powerSupply : powerSupplyArray)
2401 {
2402 if (powerSupply["MemberId"] == inventoryItem.name)
2403 {
2404 return powerSupply;
2405 }
2406 }
2407
2408 // Add new PowerSupply object to JSON array
2409 powerSupplyArray.push_back({});
2410 nlohmann::json& powerSupply = powerSupplyArray.back();
2411 powerSupply["@odata.id"] =
2412 "/redfish/v1/Chassis/" + chassisId + "/Power#/PowerSupplies/";
2413 powerSupply["MemberId"] = inventoryItem.name;
2414 powerSupply["Name"] = boost::replace_all_copy(inventoryItem.name, "_", " ");
2415 powerSupply["Manufacturer"] = inventoryItem.manufacturer;
2416 powerSupply["Model"] = inventoryItem.model;
2417 powerSupply["PartNumber"] = inventoryItem.partNumber;
2418 powerSupply["SerialNumber"] = inventoryItem.serialNumber;
Anthony Wilsond5005492019-07-31 16:34:17 -05002419 setLedState(powerSupply, &inventoryItem);
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002420
Gunnar Mills42cbe532019-08-15 15:26:54 -05002421 if (inventoryItem.powerSupplyEfficiencyPercent >= 0)
2422 {
2423 powerSupply["EfficiencyPercent"] =
2424 inventoryItem.powerSupplyEfficiencyPercent;
2425 }
2426
2427 powerSupply["Status"]["State"] = getState(&inventoryItem);
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002428 const char* health = inventoryItem.isFunctional ? "OK" : "Critical";
2429 powerSupply["Status"]["Health"] = health;
2430
2431 return powerSupply;
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002432}
2433
2434/**
Shawn McCarneyde629b62019-03-08 10:42:51 -06002435 * @brief Gets the values of the specified sensors.
2436 *
2437 * Stores the results as JSON in the SensorsAsyncResp.
2438 *
2439 * Gets the sensor values asynchronously. Stores the results later when the
2440 * information has been obtained.
2441 *
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002442 * The sensorNames set contains all requested sensors for the current chassis.
Shawn McCarneyde629b62019-03-08 10:42:51 -06002443 *
2444 * To minimize the number of DBus calls, the DBus method
2445 * org.freedesktop.DBus.ObjectManager.GetManagedObjects() is used to get the
2446 * values of all sensors provided by a connection (service).
2447 *
2448 * The connections set contains all the connections that provide sensor values.
2449 *
2450 * The objectMgrPaths map contains mappings from a connection name to the
2451 * corresponding DBus object path that implements ObjectManager.
2452 *
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002453 * The InventoryItem vector contains D-Bus inventory items associated with the
2454 * sensors. Inventory item data is needed for some Redfish sensor properties.
2455 *
Shawn McCarneyde629b62019-03-08 10:42:51 -06002456 * @param SensorsAsyncResp Pointer to object holding response data.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002457 * @param sensorNames All requested sensors within the current chassis.
Shawn McCarneyde629b62019-03-08 10:42:51 -06002458 * @param connections Connections that provide sensor values.
2459 * @param objectMgrPaths Mappings from connection name to DBus object path that
2460 * implements ObjectManager.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002461 * @param inventoryItems Inventory items associated with the sensors.
Shawn McCarneyde629b62019-03-08 10:42:51 -06002462 */
Ed Tanous23a21a12020-07-25 04:45:05 +00002463inline void getSensorData(
Ed Tanous81ce6092020-12-17 16:54:55 +00002464 const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
Ed Tanousb5a76932020-09-29 16:16:58 -07002465 const std::shared_ptr<boost::container::flat_set<std::string>>& sensorNames,
Shawn McCarneyde629b62019-03-08 10:42:51 -06002466 const boost::container::flat_set<std::string>& connections,
Ed Tanousb5a76932020-09-29 16:16:58 -07002467 const std::shared_ptr<boost::container::flat_map<std::string, std::string>>&
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002468 objectMgrPaths,
Ed Tanousb5a76932020-09-29 16:16:58 -07002469 const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems)
Shawn McCarneyde629b62019-03-08 10:42:51 -06002470{
2471 BMCWEB_LOG_DEBUG << "getSensorData enter";
2472 // Get managed objects from all services exposing sensors
2473 for (const std::string& connection : connections)
2474 {
2475 // Response handler to process managed objects
Ed Tanous81ce6092020-12-17 16:54:55 +00002476 auto getManagedObjectsCb = [sensorsAsyncResp, sensorNames,
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002477 inventoryItems](
Shawn McCarneyde629b62019-03-08 10:42:51 -06002478 const boost::system::error_code ec,
Ed Tanous711ac7a2021-12-20 09:34:41 -08002479 dbus::utility::ManagedObjectType& resp) {
Shawn McCarneyde629b62019-03-08 10:42:51 -06002480 BMCWEB_LOG_DEBUG << "getManagedObjectsCb enter";
2481 if (ec)
2482 {
2483 BMCWEB_LOG_ERROR << "getManagedObjectsCb DBUS error: " << ec;
zhanghch058d1b46d2021-04-01 11:18:24 +08002484 messages::internalError(sensorsAsyncResp->asyncResp->res);
Shawn McCarneyde629b62019-03-08 10:42:51 -06002485 return;
2486 }
2487 // Go through all objects and update response with sensor data
2488 for (const auto& objDictEntry : resp)
2489 {
2490 const std::string& objPath =
2491 static_cast<const std::string&>(objDictEntry.first);
2492 BMCWEB_LOG_DEBUG << "getManagedObjectsCb parsing object "
2493 << objPath;
2494
Shawn McCarneyde629b62019-03-08 10:42:51 -06002495 std::vector<std::string> split;
2496 // Reserve space for
2497 // /xyz/openbmc_project/sensors/<name>/<subname>
2498 split.reserve(6);
2499 boost::algorithm::split(split, objPath, boost::is_any_of("/"));
2500 if (split.size() < 6)
2501 {
2502 BMCWEB_LOG_ERROR << "Got path that isn't long enough "
2503 << objPath;
2504 continue;
2505 }
2506 // These indexes aren't intuitive, as boost::split puts an empty
2507 // string at the beginning
2508 const std::string& sensorType = split[4];
2509 const std::string& sensorName = split[5];
2510 BMCWEB_LOG_DEBUG << "sensorName " << sensorName
2511 << " sensorType " << sensorType;
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002512 if (sensorNames->find(objPath) == sensorNames->end())
Shawn McCarneyde629b62019-03-08 10:42:51 -06002513 {
Andrew Geissleraccdbb22021-11-09 15:24:45 -06002514 BMCWEB_LOG_DEBUG << sensorName << " not in sensor list ";
Shawn McCarneyde629b62019-03-08 10:42:51 -06002515 continue;
2516 }
2517
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002518 // Find inventory item (if any) associated with sensor
2519 InventoryItem* inventoryItem =
2520 findInventoryItemForSensor(inventoryItems, objPath);
2521
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002522 const std::string& sensorSchema =
Ed Tanous81ce6092020-12-17 16:54:55 +00002523 sensorsAsyncResp->chassisSubNode;
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002524
2525 nlohmann::json* sensorJson = nullptr;
2526
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02002527 if (sensorSchema == sensors::node::sensors)
Shawn McCarneyde629b62019-03-08 10:42:51 -06002528 {
zhanghch058d1b46d2021-04-01 11:18:24 +08002529 sensorsAsyncResp->asyncResp->res.jsonValue["@odata.id"] =
Ed Tanous81ce6092020-12-17 16:54:55 +00002530 "/redfish/v1/Chassis/" + sensorsAsyncResp->chassisId +
2531 "/" + sensorsAsyncResp->chassisSubNode + "/" +
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002532 sensorName;
zhanghch058d1b46d2021-04-01 11:18:24 +08002533 sensorJson = &(sensorsAsyncResp->asyncResp->res.jsonValue);
Shawn McCarneyde629b62019-03-08 10:42:51 -06002534 }
2535 else
2536 {
Ed Tanous271584a2019-07-09 16:24:22 -07002537 std::string fieldName;
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002538 if (sensorType == "temperature")
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002539 {
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002540 fieldName = "Temperatures";
2541 }
2542 else if (sensorType == "fan" || sensorType == "fan_tach" ||
2543 sensorType == "fan_pwm")
2544 {
2545 fieldName = "Fans";
2546 }
2547 else if (sensorType == "voltage")
2548 {
2549 fieldName = "Voltages";
2550 }
2551 else if (sensorType == "power")
2552 {
Ed Tanous55f79e62022-01-25 11:26:16 -08002553 if (sensorName == "total_power")
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002554 {
2555 fieldName = "PowerControl";
2556 }
2557 else if ((inventoryItem != nullptr) &&
2558 (inventoryItem->isPowerSupply))
2559 {
2560 fieldName = "PowerSupplies";
2561 }
2562 else
2563 {
2564 // Other power sensors are in SensorCollection
2565 continue;
2566 }
2567 }
2568 else
2569 {
2570 BMCWEB_LOG_ERROR << "Unsure how to handle sensorType "
2571 << sensorType;
2572 continue;
2573 }
2574
2575 nlohmann::json& tempArray =
zhanghch058d1b46d2021-04-01 11:18:24 +08002576 sensorsAsyncResp->asyncResp->res.jsonValue[fieldName];
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002577 if (fieldName == "PowerControl")
2578 {
2579 if (tempArray.empty())
2580 {
2581 // Put multiple "sensors" into a single
2582 // PowerControl. Follows MemberId naming and
2583 // naming in power.hpp.
2584 tempArray.push_back(
2585 {{"@odata.id",
2586 "/redfish/v1/Chassis/" +
Ed Tanous81ce6092020-12-17 16:54:55 +00002587 sensorsAsyncResp->chassisId + "/" +
2588 sensorsAsyncResp->chassisSubNode + "#/" +
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002589 fieldName + "/0"}});
2590 }
2591 sensorJson = &(tempArray.back());
2592 }
2593 else if (fieldName == "PowerSupplies")
2594 {
2595 if (inventoryItem != nullptr)
2596 {
2597 sensorJson =
2598 &(getPowerSupply(tempArray, *inventoryItem,
Ed Tanous81ce6092020-12-17 16:54:55 +00002599 sensorsAsyncResp->chassisId));
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002600 }
2601 }
2602 else
2603 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002604 tempArray.push_back(
2605 {{"@odata.id",
2606 "/redfish/v1/Chassis/" +
Ed Tanous81ce6092020-12-17 16:54:55 +00002607 sensorsAsyncResp->chassisId + "/" +
2608 sensorsAsyncResp->chassisSubNode + "#/" +
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002609 fieldName + "/"}});
2610 sensorJson = &(tempArray.back());
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002611 }
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002612 }
Shawn McCarneyde629b62019-03-08 10:42:51 -06002613
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002614 if (sensorJson != nullptr)
2615 {
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02002616 objectInterfacesToJson(
Ed Tanous81ce6092020-12-17 16:54:55 +00002617 sensorName, sensorType, sensorsAsyncResp,
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02002618 objDictEntry.second, *sensorJson, inventoryItem);
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002619 }
Shawn McCarneyde629b62019-03-08 10:42:51 -06002620 }
Ed Tanous81ce6092020-12-17 16:54:55 +00002621 if (sensorsAsyncResp.use_count() == 1)
James Feist8bd25cc2019-03-15 15:14:00 -07002622 {
Ed Tanous81ce6092020-12-17 16:54:55 +00002623 sortJSONResponse(sensorsAsyncResp);
2624 if (sensorsAsyncResp->chassisSubNode == sensors::node::thermal)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002625 {
Ed Tanous81ce6092020-12-17 16:54:55 +00002626 populateFanRedundancy(sensorsAsyncResp);
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002627 }
James Feist8bd25cc2019-03-15 15:14:00 -07002628 }
Shawn McCarneyde629b62019-03-08 10:42:51 -06002629 BMCWEB_LOG_DEBUG << "getManagedObjectsCb exit";
2630 };
2631
2632 // Find DBus object path that implements ObjectManager for the current
2633 // connection. If no mapping found, default to "/".
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002634 auto iter = objectMgrPaths->find(connection);
Shawn McCarneyde629b62019-03-08 10:42:51 -06002635 const std::string& objectMgrPath =
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002636 (iter != objectMgrPaths->end()) ? iter->second : "/";
Shawn McCarneyde629b62019-03-08 10:42:51 -06002637 BMCWEB_LOG_DEBUG << "ObjectManager path for " << connection << " is "
2638 << objectMgrPath;
2639
2640 crow::connections::systemBus->async_method_call(
2641 getManagedObjectsCb, connection, objectMgrPath,
2642 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
Ed Tanous23a21a12020-07-25 04:45:05 +00002643 }
Shawn McCarneyde629b62019-03-08 10:42:51 -06002644 BMCWEB_LOG_DEBUG << "getSensorData exit";
2645}
2646
Ed Tanous23a21a12020-07-25 04:45:05 +00002647inline void processSensorList(
Ed Tanous81ce6092020-12-17 16:54:55 +00002648 const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
Ed Tanousb5a76932020-09-29 16:16:58 -07002649 const std::shared_ptr<boost::container::flat_set<std::string>>& sensorNames)
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002650{
2651 auto getConnectionCb =
Ed Tanous81ce6092020-12-17 16:54:55 +00002652 [sensorsAsyncResp, sensorNames](
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002653 const boost::container::flat_set<std::string>& connections) {
2654 BMCWEB_LOG_DEBUG << "getConnectionCb enter";
2655 auto getObjectManagerPathsCb =
Ed Tanous81ce6092020-12-17 16:54:55 +00002656 [sensorsAsyncResp, sensorNames,
Ed Tanousb5a76932020-09-29 16:16:58 -07002657 connections](const std::shared_ptr<boost::container::flat_map<
2658 std::string, std::string>>& objectMgrPaths) {
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002659 BMCWEB_LOG_DEBUG << "getObjectManagerPathsCb enter";
2660 auto getInventoryItemsCb =
Ed Tanous81ce6092020-12-17 16:54:55 +00002661 [sensorsAsyncResp, sensorNames, connections,
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002662 objectMgrPaths](
Ed Tanousf23b7292020-10-15 09:41:17 -07002663 const std::shared_ptr<std::vector<InventoryItem>>&
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002664 inventoryItems) {
2665 BMCWEB_LOG_DEBUG << "getInventoryItemsCb enter";
2666 // Get sensor data and store results in JSON
Ed Tanous81ce6092020-12-17 16:54:55 +00002667 getSensorData(sensorsAsyncResp, sensorNames,
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002668 connections, objectMgrPaths,
Ed Tanousf23b7292020-10-15 09:41:17 -07002669 inventoryItems);
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002670 BMCWEB_LOG_DEBUG << "getInventoryItemsCb exit";
2671 };
2672
2673 // Get inventory items associated with sensors
Ed Tanous81ce6092020-12-17 16:54:55 +00002674 getInventoryItems(sensorsAsyncResp, sensorNames,
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002675 objectMgrPaths,
2676 std::move(getInventoryItemsCb));
2677
2678 BMCWEB_LOG_DEBUG << "getObjectManagerPathsCb exit";
2679 };
2680
2681 // Get mapping from connection names to the DBus object
2682 // paths that implement the ObjectManager interface
Ed Tanous81ce6092020-12-17 16:54:55 +00002683 getObjectManagerPaths(sensorsAsyncResp,
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002684 std::move(getObjectManagerPathsCb));
2685 BMCWEB_LOG_DEBUG << "getConnectionCb exit";
2686 };
2687
2688 // Get set of connections that provide sensor values
Ed Tanous81ce6092020-12-17 16:54:55 +00002689 getConnections(sensorsAsyncResp, sensorNames, std::move(getConnectionCb));
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002690}
2691
Shawn McCarneyde629b62019-03-08 10:42:51 -06002692/**
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +01002693 * @brief Entry point for retrieving sensors data related to requested
2694 * chassis.
Kowalski, Kamil588c3f02018-04-03 14:55:27 +02002695 * @param SensorsAsyncResp Pointer to object holding response data
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +01002696 */
Ed Tanousb5a76932020-09-29 16:16:58 -07002697inline void
Ed Tanous81ce6092020-12-17 16:54:55 +00002698 getChassisData(const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp)
Ed Tanous1abe55e2018-09-05 08:30:59 -07002699{
2700 BMCWEB_LOG_DEBUG << "getChassisData enter";
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002701 auto getChassisCb =
Ed Tanous81ce6092020-12-17 16:54:55 +00002702 [sensorsAsyncResp](
Ed Tanousf23b7292020-10-15 09:41:17 -07002703 const std::shared_ptr<boost::container::flat_set<std::string>>&
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002704 sensorNames) {
2705 BMCWEB_LOG_DEBUG << "getChassisCb enter";
Ed Tanous81ce6092020-12-17 16:54:55 +00002706 processSensorList(sensorsAsyncResp, sensorNames);
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002707 BMCWEB_LOG_DEBUG << "getChassisCb exit";
2708 };
zhanghch058d1b46d2021-04-01 11:18:24 +08002709 sensorsAsyncResp->asyncResp->res.jsonValue["Redundancy"] =
2710 nlohmann::json::array();
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +01002711
Shawn McCarney26f03892019-05-03 13:20:24 -05002712 // Get set of sensors in chassis
Ed Tanous81ce6092020-12-17 16:54:55 +00002713 getChassis(sensorsAsyncResp, std::move(getChassisCb));
Ed Tanous1abe55e2018-09-05 08:30:59 -07002714 BMCWEB_LOG_DEBUG << "getChassisData exit";
Ed Tanous271584a2019-07-09 16:24:22 -07002715}
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +01002716
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302717/**
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002718 * @brief Find the requested sensorName in the list of all sensors supplied by
2719 * the chassis node
2720 *
2721 * @param sensorName The sensor name supplied in the PATCH request
2722 * @param sensorsList The list of sensors managed by the chassis node
2723 * @param sensorsModified The list of sensors that were found as a result of
2724 * repeated calls to this function
2725 */
Ed Tanous23a21a12020-07-25 04:45:05 +00002726inline bool findSensorNameUsingSensorPath(
Richard Marian Thomaiyar0a86feb2019-05-27 23:16:40 +05302727 std::string_view sensorName,
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002728 boost::container::flat_set<std::string>& sensorsList,
2729 boost::container::flat_set<std::string>& sensorsModified)
2730{
George Liu28aa8de2021-02-01 15:13:30 +08002731 for (auto& chassisSensor : sensorsList)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002732 {
George Liu28aa8de2021-02-01 15:13:30 +08002733 sdbusplus::message::object_path path(chassisSensor);
Ed Tanousb00dcc22021-02-23 12:52:50 -08002734 std::string thisSensorName = path.filename();
George Liu28aa8de2021-02-01 15:13:30 +08002735 if (thisSensorName.empty())
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002736 {
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002737 continue;
2738 }
2739 if (thisSensorName == sensorName)
2740 {
2741 sensorsModified.emplace(chassisSensor);
2742 return true;
2743 }
2744 }
2745 return false;
2746}
2747
2748/**
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302749 * @brief Entry point for overriding sensor values of given sensor
2750 *
zhanghch058d1b46d2021-04-01 11:18:24 +08002751 * @param sensorAsyncResp response object
Carol Wang4bb3dc32019-10-17 18:15:02 +08002752 * @param allCollections Collections extract from sensors' request patch info
jayaprakash Mutyala91e130a2020-03-04 22:26:38 +00002753 * @param chassisSubNode Chassis Node for which the query has to happen
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302754 */
Ed Tanous23a21a12020-07-25 04:45:05 +00002755inline void setSensorsOverride(
Ed Tanousb5a76932020-09-29 16:16:58 -07002756 const std::shared_ptr<SensorsAsyncResp>& sensorAsyncResp,
Carol Wang4bb3dc32019-10-17 18:15:02 +08002757 std::unordered_map<std::string, std::vector<nlohmann::json>>&
jayaprakash Mutyala397fd612020-02-06 23:33:34 +00002758 allCollections)
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302759{
jayaprakash Mutyala70d1d0a2020-01-21 23:41:36 +00002760 BMCWEB_LOG_INFO << "setSensorsOverride for subNode"
Carol Wang4bb3dc32019-10-17 18:15:02 +08002761 << sensorAsyncResp->chassisSubNode << "\n";
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302762
Ed Tanous543f4402022-01-06 13:12:53 -08002763 const char* propertyValueName = nullptr;
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302764 std::unordered_map<std::string, std::pair<double, std::string>> overrideMap;
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302765 std::string memberId;
Ed Tanous543f4402022-01-06 13:12:53 -08002766 double value = 0.0;
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302767 for (auto& collectionItems : allCollections)
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302768 {
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302769 if (collectionItems.first == "Temperatures")
2770 {
2771 propertyValueName = "ReadingCelsius";
2772 }
2773 else if (collectionItems.first == "Fans")
2774 {
2775 propertyValueName = "Reading";
2776 }
2777 else
2778 {
2779 propertyValueName = "ReadingVolts";
2780 }
2781 for (auto& item : collectionItems.second)
2782 {
zhanghch058d1b46d2021-04-01 11:18:24 +08002783 if (!json_util::readJson(item, sensorAsyncResp->asyncResp->res,
2784 "MemberId", memberId, propertyValueName,
2785 value))
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302786 {
2787 return;
2788 }
2789 overrideMap.emplace(memberId,
2790 std::make_pair(value, collectionItems.first));
2791 }
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302792 }
Carol Wang4bb3dc32019-10-17 18:15:02 +08002793
Ed Tanousb5a76932020-09-29 16:16:58 -07002794 auto getChassisSensorListCb = [sensorAsyncResp, overrideMap](
2795 const std::shared_ptr<
2796 boost::container::flat_set<
2797 std::string>>& sensorsList) {
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002798 // Match sensor names in the PATCH request to those managed by the
2799 // chassis node
2800 const std::shared_ptr<boost::container::flat_set<std::string>>
2801 sensorNames =
2802 std::make_shared<boost::container::flat_set<std::string>>();
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302803 for (const auto& item : overrideMap)
2804 {
2805 const auto& sensor = item.first;
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002806 if (!findSensorNameUsingSensorPath(sensor, *sensorsList,
2807 *sensorNames))
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302808 {
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302809 BMCWEB_LOG_INFO << "Unable to find memberId " << item.first;
zhanghch058d1b46d2021-04-01 11:18:24 +08002810 messages::resourceNotFound(sensorAsyncResp->asyncResp->res,
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302811 item.second.second, item.first);
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302812 return;
2813 }
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302814 }
2815 // Get the connection to which the memberId belongs
Jayaprakash Mutyala4f277b52021-12-08 22:46:49 +00002816 auto getObjectsWithConnectionCb = [sensorAsyncResp, overrideMap](
2817 const boost::container::flat_set<
2818 std::string>& /*connections*/,
2819 const std::set<std::pair<
2820 std::string, std::string>>&
2821 objectsWithConnection) {
2822 if (objectsWithConnection.size() != overrideMap.size())
2823 {
2824 BMCWEB_LOG_INFO
2825 << "Unable to find all objects with proper connection "
2826 << objectsWithConnection.size() << " requested "
2827 << overrideMap.size() << "\n";
2828 messages::resourceNotFound(sensorAsyncResp->asyncResp->res,
2829 sensorAsyncResp->chassisSubNode ==
2830 sensors::node::thermal
2831 ? "Temperatures"
2832 : "Voltages",
2833 "Count");
2834 return;
2835 }
2836 for (const auto& item : objectsWithConnection)
2837 {
2838 sdbusplus::message::object_path path(item.first);
2839 std::string sensorName = path.filename();
2840 if (sensorName.empty())
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302841 {
Jayaprakash Mutyala4f277b52021-12-08 22:46:49 +00002842 messages::internalError(sensorAsyncResp->asyncResp->res);
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302843 return;
2844 }
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302845
Jayaprakash Mutyala4f277b52021-12-08 22:46:49 +00002846 const auto& iterator = overrideMap.find(sensorName);
2847 if (iterator == overrideMap.end())
2848 {
2849 BMCWEB_LOG_INFO << "Unable to find sensor object"
2850 << item.first << "\n";
2851 messages::internalError(sensorAsyncResp->asyncResp->res);
2852 return;
2853 }
2854 crow::connections::systemBus->async_method_call(
2855 [sensorAsyncResp](const boost::system::error_code ec) {
2856 if (ec)
2857 {
2858 if (ec.value() ==
2859 boost::system::errc::permission_denied)
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302860 {
Jayaprakash Mutyala4f277b52021-12-08 22:46:49 +00002861 BMCWEB_LOG_WARNING
2862 << "Manufacturing mode is not Enabled...can't "
2863 "Override the sensor value. ";
2864
2865 messages::insufficientPrivilege(
zhanghch058d1b46d2021-04-01 11:18:24 +08002866 sensorAsyncResp->asyncResp->res);
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302867 return;
2868 }
Jayaprakash Mutyala4f277b52021-12-08 22:46:49 +00002869 BMCWEB_LOG_DEBUG
2870 << "setOverrideValueStatus DBUS error: " << ec;
2871 messages::internalError(
2872 sensorAsyncResp->asyncResp->res);
2873 }
2874 },
2875 item.second, item.first, "org.freedesktop.DBus.Properties",
2876 "Set", "xyz.openbmc_project.Sensor.Value", "Value",
Ed Tanous168e20c2021-12-13 14:39:53 -08002877 dbus::utility::DbusVariantType(iterator->second.first));
Jayaprakash Mutyala4f277b52021-12-08 22:46:49 +00002878 }
2879 };
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302880 // Get object with connection for the given sensor name
2881 getObjectsWithConnection(sensorAsyncResp, sensorNames,
2882 std::move(getObjectsWithConnectionCb));
2883 };
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302884 // get full sensor list for the given chassisId and cross verify the sensor.
2885 getChassis(sensorAsyncResp, std::move(getChassisSensorListCb));
2886}
2887
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02002888/**
2889 * @brief Retrieves mapping of Redfish URIs to sensor value property to D-Bus
2890 * path of the sensor.
2891 *
2892 * Function builds valid Redfish response for sensor query of given chassis and
2893 * node. It then builds metadata about Redfish<->D-Bus correlations and provides
2894 * it to caller in a callback.
2895 *
2896 * @param chassis Chassis for which retrieval should be performed
2897 * @param node Node (group) of sensors. See sensors::node for supported values
2898 * @param mapComplete Callback to be called with retrieval result
2899 */
Krzysztof Grobelny021d32c2021-10-29 16:00:07 +02002900inline void retrieveUriToDbusMap(const std::string& chassis,
2901 const std::string& node,
2902 SensorsAsyncResp::DataCompleteCb&& mapComplete)
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02002903{
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +00002904 auto pathIt = sensors::dbus::paths.find(node);
2905 if (pathIt == sensors::dbus::paths.end())
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02002906 {
2907 BMCWEB_LOG_ERROR << "Wrong node provided : " << node;
2908 mapComplete(boost::beast::http::status::bad_request, {});
2909 return;
2910 }
Krzysztof Grobelnyd51e0722021-04-16 13:15:21 +00002911
Nan Zhou72374eb2022-01-27 17:06:51 -08002912 auto asyncResp = std::make_shared<bmcweb::AsyncResp>();
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02002913 auto callback =
Nan Zhou72374eb2022-01-27 17:06:51 -08002914 [asyncResp, mapCompleteCb{std::move(mapComplete)}](
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02002915 const boost::beast::http::status status,
2916 const boost::container::flat_map<std::string, std::string>&
2917 uriToDbus) { mapCompleteCb(status, uriToDbus); };
2918
2919 auto resp = std::make_shared<SensorsAsyncResp>(
Krzysztof Grobelnyd51e0722021-04-16 13:15:21 +00002920 asyncResp, chassis, pathIt->second, node, std::move(callback));
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02002921 getChassisData(resp);
2922}
2923
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002924inline void requestRoutesSensorCollection(App& app)
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002925{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002926 BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/Sensors/")
Ed Tanoused398212021-06-09 17:05:54 -07002927 .privileges(redfish::privileges::getSensorCollection)
Ed Tanous45ca1b82022-03-25 13:07:27 -07002928 .methods(boost::beast::http::verb::get)(
2929 [&app](const crow::Request& req,
2930 const std::shared_ptr<bmcweb::AsyncResp>& aResp,
2931 const std::string& chassisId) {
2932 if (!redfish::setUpRedfishRoute(app, req, aResp->res))
2933 {
2934 return;
2935 }
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002936
Ed Tanous45ca1b82022-03-25 13:07:27 -07002937 BMCWEB_LOG_DEBUG << "SensorCollection doGet enter";
zhanghch058d1b46d2021-04-01 11:18:24 +08002938
Ed Tanous45ca1b82022-03-25 13:07:27 -07002939 std::shared_ptr<SensorsAsyncResp> asyncResp =
2940 std::make_shared<SensorsAsyncResp>(
2941 aResp, chassisId,
2942 sensors::dbus::paths.at(sensors::node::sensors),
2943 sensors::node::sensors);
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002944
Ed Tanous45ca1b82022-03-25 13:07:27 -07002945 auto getChassisCb =
2946 [asyncResp](const std::shared_ptr<
2947 boost::container::flat_set<std::string>>&
2948 sensorNames) {
2949 BMCWEB_LOG_DEBUG << "getChassisCb enter";
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002950
Ed Tanous45ca1b82022-03-25 13:07:27 -07002951 nlohmann::json& entriesArray =
2952 asyncResp->asyncResp->res.jsonValue["Members"];
2953 for (auto& sensor : *sensorNames)
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002954 {
Ed Tanous45ca1b82022-03-25 13:07:27 -07002955 BMCWEB_LOG_DEBUG << "Adding sensor: " << sensor;
2956
2957 sdbusplus::message::object_path path(sensor);
2958 std::string sensorName = path.filename();
2959 if (sensorName.empty())
2960 {
2961 BMCWEB_LOG_ERROR << "Invalid sensor path: "
2962 << sensor;
2963 messages::internalError(
2964 asyncResp->asyncResp->res);
2965 return;
2966 }
2967 entriesArray.push_back(
2968 {{"@odata.id", "/redfish/v1/Chassis/" +
2969 asyncResp->chassisId + "/" +
2970 asyncResp->chassisSubNode +
2971 "/" + sensorName}});
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002972 }
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002973
Ed Tanous45ca1b82022-03-25 13:07:27 -07002974 asyncResp->asyncResp->res
2975 .jsonValue["Members@odata.count"] =
2976 entriesArray.size();
2977 BMCWEB_LOG_DEBUG << "getChassisCb exit";
2978 };
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002979
Ed Tanous45ca1b82022-03-25 13:07:27 -07002980 // Get set of sensors in chassis
2981 getChassis(asyncResp, std::move(getChassisCb));
2982 BMCWEB_LOG_DEBUG << "SensorCollection doGet exit";
2983 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002984}
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002985
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002986inline void requestRoutesSensor(App& app)
2987{
2988 BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/Sensors/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07002989 .privileges(redfish::privileges::getSensor)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002990 .methods(
Ed Tanous45ca1b82022-03-25 13:07:27 -07002991 boost::beast::http::verb::get)([&app](
2992 const crow::Request& req,
2993 const std::shared_ptr<
2994 bmcweb::AsyncResp>& aResp,
2995 const std::string& chassisId,
2996 const std::string& sensorName) {
2997 if (!redfish::setUpRedfishRoute(app, req, aResp->res))
2998 {
2999 return;
3000 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003001 BMCWEB_LOG_DEBUG << "Sensor doGet enter";
3002 std::shared_ptr<SensorsAsyncResp> asyncResp =
3003 std::make_shared<SensorsAsyncResp>(aResp, chassisId,
3004 std::vector<const char*>(),
3005 sensors::node::sensors);
Anthony Wilson95a3eca2019-06-11 10:44:47 -05003006
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003007 const std::array<const char*, 1> interfaces = {
3008 "xyz.openbmc_project.Sensor.Value"};
3009
3010 // Get a list of all of the sensors that implement Sensor.Value
3011 // and get the path and service name associated with the sensor
3012 crow::connections::systemBus->async_method_call(
Ed Tanousb9d36b42022-02-26 21:42:46 -08003013 [asyncResp, sensorName](
3014 const boost::system::error_code ec,
3015 const dbus::utility::MapperGetSubTreeResponse& subtree) {
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003016 BMCWEB_LOG_DEBUG << "respHandler1 enter";
3017 if (ec)
3018 {
3019 messages::internalError(asyncResp->asyncResp->res);
3020 BMCWEB_LOG_ERROR
3021 << "Sensor getSensorPaths resp_handler: "
3022 << "Dbus error " << ec;
3023 return;
3024 }
3025
Ed Tanousb9d36b42022-02-26 21:42:46 -08003026 dbus::utility::MapperGetSubTreeResponse::const_iterator it =
3027 std::find_if(
3028 subtree.begin(), subtree.end(),
3029 [sensorName](
3030 const std::pair<std::string,
3031 std::vector<std::pair<
3032 std::string,
3033 std::vector<std::string>>>>&
3034 object) {
3035 sdbusplus::message::object_path path(
3036 object.first);
3037 std::string name = path.filename();
3038 if (name.empty())
3039 {
3040 BMCWEB_LOG_ERROR << "Invalid sensor path: "
3041 << object.first;
3042 return false;
3043 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003044
Ed Tanousb9d36b42022-02-26 21:42:46 -08003045 return name == sensorName;
3046 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003047
3048 if (it == subtree.end())
3049 {
3050 BMCWEB_LOG_ERROR << "Could not find path for sensor: "
3051 << sensorName;
3052 messages::resourceNotFound(asyncResp->asyncResp->res,
3053 "Sensor", sensorName);
3054 return;
3055 }
3056 std::string_view sensorPath = (*it).first;
3057 BMCWEB_LOG_DEBUG << "Found sensor path for sensor '"
3058 << sensorName << "': " << sensorPath;
3059
3060 const std::shared_ptr<
3061 boost::container::flat_set<std::string>>
3062 sensorList = std::make_shared<
3063 boost::container::flat_set<std::string>>();
3064
3065 sensorList->emplace(sensorPath);
3066 processSensorList(asyncResp, sensorList);
3067 BMCWEB_LOG_DEBUG << "respHandler1 exit";
3068 },
3069 "xyz.openbmc_project.ObjectMapper",
3070 "/xyz/openbmc_project/object_mapper",
3071 "xyz.openbmc_project.ObjectMapper", "GetSubTree",
3072 "/xyz/openbmc_project/sensors", 2, interfaces);
3073 });
3074}
Anthony Wilson95a3eca2019-06-11 10:44:47 -05003075
Ed Tanous1abe55e2018-09-05 08:30:59 -07003076} // namespace redfish