blob: 1af3032327c6e5670c7986b6d6865648e0c9e120 [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
Ed Tanous3ccb3ad2023-01-13 17:40:03 -080018#include "app.hpp"
19#include "dbus_singleton.hpp"
George Liu7a1dbc42022-12-07 16:03:22 +080020#include "dbus_utility.hpp"
Ed Tanous0ec8b832022-03-14 14:56:47 -070021#include "generated/enums/sensor.hpp"
Ed Tanous3ccb3ad2023-01-13 17:40:03 -080022#include "query.hpp"
23#include "registries/privilege_registry.hpp"
Ed Tanous50ebd4a2023-01-19 19:03:17 -080024#include "str_utility.hpp"
Ed Tanous3ccb3ad2023-01-13 17:40:03 -080025#include "utils/dbus_utils.hpp"
26#include "utils/json_utils.hpp"
27#include "utils/query_param.hpp"
Ed Tanous0ec8b832022-03-14 14:56:47 -070028
Ed Tanous11ba3972022-07-11 09:50:41 -070029#include <boost/algorithm/string/classification.hpp>
Ed Tanous1d7c0052022-08-09 12:32:26 -070030#include <boost/algorithm/string/find.hpp>
31#include <boost/algorithm/string/predicate.hpp>
Ed Tanousc71d6122022-11-29 14:10:32 -080032#include <boost/algorithm/string/replace.hpp>
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +010033#include <boost/range/algorithm/replace_copy_if.hpp>
George Liue99073f2022-12-09 11:06:16 +080034#include <boost/system/error_code.hpp>
Jonathan Doman1e1e5982021-06-11 09:36:17 -070035#include <sdbusplus/asio/property.hpp>
Krzysztof Grobelny86d89ed2022-08-29 14:49:20 +020036#include <sdbusplus/unpack_properties.hpp>
Gunnar Mills1214b7e2020-06-04 10:11:30 -050037
George Liu7a1dbc42022-12-07 16:03:22 +080038#include <array>
Gunnar Mills1214b7e2020-06-04 10:11:30 -050039#include <cmath>
Nan Zhoufe04d492022-06-22 17:10:41 +000040#include <iterator>
41#include <map>
42#include <set>
George Liu7a1dbc42022-12-07 16:03:22 +080043#include <string_view>
Ed Tanousb5a76932020-09-29 16:16:58 -070044#include <utility>
Ed Tanousabf2add2019-01-22 16:40:12 -080045#include <variant>
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +010046
Ed Tanous1abe55e2018-09-05 08:30:59 -070047namespace redfish
48{
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +010049
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +020050namespace sensors
51{
52namespace node
53{
54static constexpr std::string_view power = "Power";
55static constexpr std::string_view sensors = "Sensors";
56static constexpr std::string_view thermal = "Thermal";
57} // namespace node
58
Ed Tanous02da7c52022-02-27 00:09:02 -080059// clang-format off
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +020060namespace dbus
61{
Ed Tanouscf9e4172022-12-21 09:30:16 -080062constexpr auto powerPaths = std::to_array<std::string_view>({
Ed Tanous02da7c52022-02-27 00:09:02 -080063 "/xyz/openbmc_project/sensors/voltage",
64 "/xyz/openbmc_project/sensors/power"
65});
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +000066
Ed Tanouscf9e4172022-12-21 09:30:16 -080067constexpr auto sensorPaths = std::to_array<std::string_view>({
Ed Tanous02da7c52022-02-27 00:09:02 -080068 "/xyz/openbmc_project/sensors/power",
69 "/xyz/openbmc_project/sensors/current",
70 "/xyz/openbmc_project/sensors/airflow",
Ed Tanous4e777662022-08-06 09:39:13 -070071 "/xyz/openbmc_project/sensors/humidity",
George Liue8204932021-02-01 14:42:49 +080072#ifdef BMCWEB_NEW_POWERSUBSYSTEM_THERMALSUBSYSTEM
Ed Tanous02da7c52022-02-27 00:09:02 -080073 "/xyz/openbmc_project/sensors/voltage",
74 "/xyz/openbmc_project/sensors/fan_tach",
75 "/xyz/openbmc_project/sensors/temperature",
76 "/xyz/openbmc_project/sensors/fan_pwm",
77 "/xyz/openbmc_project/sensors/altitude",
78 "/xyz/openbmc_project/sensors/energy",
George Liue8204932021-02-01 14:42:49 +080079#endif
Ed Tanous02da7c52022-02-27 00:09:02 -080080 "/xyz/openbmc_project/sensors/utilization"
81});
82
Ed Tanouscf9e4172022-12-21 09:30:16 -080083constexpr auto thermalPaths = std::to_array<std::string_view>({
Ed Tanous02da7c52022-02-27 00:09:02 -080084 "/xyz/openbmc_project/sensors/fan_tach",
85 "/xyz/openbmc_project/sensors/temperature",
86 "/xyz/openbmc_project/sensors/fan_pwm"
87});
88
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +000089} // namespace dbus
Ed Tanous02da7c52022-02-27 00:09:02 -080090// clang-format on
91
Ed Tanouscf9e4172022-12-21 09:30:16 -080092using sensorPair =
93 std::pair<std::string_view, std::span<const std::string_view>>;
Ed Tanous02da7c52022-02-27 00:09:02 -080094static constexpr std::array<sensorPair, 3> paths = {
Ed Tanouscf9e4172022-12-21 09:30:16 -080095 {{node::power, dbus::powerPaths},
96 {node::sensors, dbus::sensorPaths},
97 {node::thermal, dbus::thermalPaths}}};
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +000098
Ed Tanous0ec8b832022-03-14 14:56:47 -070099inline sensor::ReadingType toReadingType(std::string_view sensorType)
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000100{
101 if (sensorType == "voltage")
102 {
Ed Tanous0ec8b832022-03-14 14:56:47 -0700103 return sensor::ReadingType::Voltage;
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000104 }
105 if (sensorType == "power")
106 {
Ed Tanous0ec8b832022-03-14 14:56:47 -0700107 return sensor::ReadingType::Power;
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000108 }
109 if (sensorType == "current")
110 {
Ed Tanous0ec8b832022-03-14 14:56:47 -0700111 return sensor::ReadingType::Current;
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000112 }
113 if (sensorType == "fan_tach")
114 {
Ed Tanous0ec8b832022-03-14 14:56:47 -0700115 return sensor::ReadingType::Rotational;
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000116 }
117 if (sensorType == "temperature")
118 {
Ed Tanous0ec8b832022-03-14 14:56:47 -0700119 return sensor::ReadingType::Temperature;
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000120 }
121 if (sensorType == "fan_pwm" || sensorType == "utilization")
122 {
Ed Tanous0ec8b832022-03-14 14:56:47 -0700123 return sensor::ReadingType::Percent;
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000124 }
Gunnar Mills5deabed2022-04-20 13:43:45 -0600125 if (sensorType == "humidity")
126 {
Ed Tanous0ec8b832022-03-14 14:56:47 -0700127 return sensor::ReadingType::Humidity;
Gunnar Mills5deabed2022-04-20 13:43:45 -0600128 }
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000129 if (sensorType == "altitude")
130 {
Ed Tanous0ec8b832022-03-14 14:56:47 -0700131 return sensor::ReadingType::Altitude;
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000132 }
133 if (sensorType == "airflow")
134 {
Ed Tanous0ec8b832022-03-14 14:56:47 -0700135 return sensor::ReadingType::AirFlow;
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000136 }
137 if (sensorType == "energy")
138 {
Ed Tanous0ec8b832022-03-14 14:56:47 -0700139 return sensor::ReadingType::EnergyJoules;
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000140 }
Ed Tanous0ec8b832022-03-14 14:56:47 -0700141 return sensor::ReadingType::Invalid;
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000142}
143
Ed Tanous1d7c0052022-08-09 12:32:26 -0700144inline std::string_view toReadingUnits(std::string_view sensorType)
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000145{
146 if (sensorType == "voltage")
147 {
148 return "V";
149 }
150 if (sensorType == "power")
151 {
152 return "W";
153 }
154 if (sensorType == "current")
155 {
156 return "A";
157 }
158 if (sensorType == "fan_tach")
159 {
160 return "RPM";
161 }
162 if (sensorType == "temperature")
163 {
164 return "Cel";
165 }
Gunnar Mills5deabed2022-04-20 13:43:45 -0600166 if (sensorType == "fan_pwm" || sensorType == "utilization" ||
167 sensorType == "humidity")
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000168 {
169 return "%";
170 }
171 if (sensorType == "altitude")
172 {
173 return "m";
174 }
175 if (sensorType == "airflow")
176 {
177 return "cft_i/min";
178 }
179 if (sensorType == "energy")
180 {
181 return "J";
182 }
183 return "";
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200184}
185} // namespace sensors
186
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100187/**
Kowalski, Kamil588c3f02018-04-03 14:55:27 +0200188 * SensorsAsyncResp
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100189 * Gathers data needed for response processing after async calls are done
190 */
Ed Tanous1abe55e2018-09-05 08:30:59 -0700191class SensorsAsyncResp
192{
193 public:
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200194 using DataCompleteCb = std::function<void(
195 const boost::beast::http::status status,
Nan Zhoufe04d492022-06-22 17:10:41 +0000196 const std::map<std::string, std::string>& uriToDbus)>;
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200197
198 struct SensorData
199 {
200 const std::string name;
201 std::string uri;
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200202 const std::string dbusPath;
203 };
204
Ed Tanous8a592812022-06-04 09:06:59 -0700205 SensorsAsyncResp(const std::shared_ptr<bmcweb::AsyncResp>& asyncRespIn,
zhanghch058d1b46d2021-04-01 11:18:24 +0800206 const std::string& chassisIdIn,
Ed Tanouscf9e4172022-12-21 09:30:16 -0800207 std::span<const std::string_view> typesIn,
Ed Tanous02da7c52022-02-27 00:09:02 -0800208 std::string_view subNode) :
Ed Tanous8a592812022-06-04 09:06:59 -0700209 asyncResp(asyncRespIn),
Nan Zhou928fefb2022-03-28 08:45:00 -0700210 chassisId(chassisIdIn), types(typesIn), chassisSubNode(subNode),
211 efficientExpand(false)
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500212 {}
Kowalski, Kamil588c3f02018-04-03 14:55:27 +0200213
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200214 // Store extra data about sensor mapping and return it in callback
Ed Tanous8a592812022-06-04 09:06:59 -0700215 SensorsAsyncResp(const std::shared_ptr<bmcweb::AsyncResp>& asyncRespIn,
zhanghch058d1b46d2021-04-01 11:18:24 +0800216 const std::string& chassisIdIn,
Ed Tanouscf9e4172022-12-21 09:30:16 -0800217 std::span<const std::string_view> typesIn,
Ed Tanous02da7c52022-02-27 00:09:02 -0800218 std::string_view subNode,
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200219 DataCompleteCb&& creationComplete) :
Ed Tanous8a592812022-06-04 09:06:59 -0700220 asyncResp(asyncRespIn),
Nan Zhou928fefb2022-03-28 08:45:00 -0700221 chassisId(chassisIdIn), types(typesIn), chassisSubNode(subNode),
222 efficientExpand(false), metadata{std::vector<SensorData>()},
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200223 dataComplete{std::move(creationComplete)}
224 {}
225
Nan Zhou928fefb2022-03-28 08:45:00 -0700226 // sensor collections expand
Ed Tanous8a592812022-06-04 09:06:59 -0700227 SensorsAsyncResp(const std::shared_ptr<bmcweb::AsyncResp>& asyncRespIn,
Nan Zhou928fefb2022-03-28 08:45:00 -0700228 const std::string& chassisIdIn,
Ed Tanouscf9e4172022-12-21 09:30:16 -0800229 std::span<const std::string_view> typesIn,
Ed Tanous8a592812022-06-04 09:06:59 -0700230 const std::string_view& subNode, bool efficientExpandIn) :
231 asyncResp(asyncRespIn),
Nan Zhou928fefb2022-03-28 08:45:00 -0700232 chassisId(chassisIdIn), types(typesIn), chassisSubNode(subNode),
Ed Tanous8a592812022-06-04 09:06:59 -0700233 efficientExpand(efficientExpandIn)
Nan Zhou928fefb2022-03-28 08:45:00 -0700234 {}
235
Ed Tanous1abe55e2018-09-05 08:30:59 -0700236 ~SensorsAsyncResp()
237 {
zhanghch058d1b46d2021-04-01 11:18:24 +0800238 if (asyncResp->res.result() ==
239 boost::beast::http::status::internal_server_error)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700240 {
241 // Reset the json object to clear out any data that made it in
242 // before the error happened todo(ed) handle error condition with
243 // proper code
zhanghch058d1b46d2021-04-01 11:18:24 +0800244 asyncResp->res.jsonValue = nlohmann::json::object();
Ed Tanous1abe55e2018-09-05 08:30:59 -0700245 }
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200246
247 if (dataComplete && metadata)
248 {
Nan Zhoufe04d492022-06-22 17:10:41 +0000249 std::map<std::string, std::string> map;
zhanghch058d1b46d2021-04-01 11:18:24 +0800250 if (asyncResp->res.result() == boost::beast::http::status::ok)
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200251 {
252 for (auto& sensor : *metadata)
253 {
Ed Tanousc1d019a2022-08-06 09:36:06 -0700254 map.emplace(sensor.uri, sensor.dbusPath);
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200255 }
256 }
zhanghch058d1b46d2021-04-01 11:18:24 +0800257 dataComplete(asyncResp->res.result(), map);
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200258 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700259 }
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100260
Ed Tanousecd6a3a2022-01-07 09:18:40 -0800261 SensorsAsyncResp(const SensorsAsyncResp&) = delete;
262 SensorsAsyncResp(SensorsAsyncResp&&) = delete;
263 SensorsAsyncResp& operator=(const SensorsAsyncResp&) = delete;
264 SensorsAsyncResp& operator=(SensorsAsyncResp&&) = delete;
265
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200266 void addMetadata(const nlohmann::json& sensorObject,
Ed Tanousc1d019a2022-08-06 09:36:06 -0700267 const std::string& dbusPath)
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200268 {
269 if (metadata)
270 {
Ed Tanousc1d019a2022-08-06 09:36:06 -0700271 metadata->emplace_back(SensorData{
272 sensorObject["Name"], sensorObject["@odata.id"], dbusPath});
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200273 }
274 }
275
276 void updateUri(const std::string& name, const std::string& uri)
277 {
278 if (metadata)
279 {
280 for (auto& sensor : *metadata)
281 {
282 if (sensor.name == name)
283 {
284 sensor.uri = uri;
285 }
286 }
287 }
288 }
289
zhanghch058d1b46d2021-04-01 11:18:24 +0800290 const std::shared_ptr<bmcweb::AsyncResp> asyncResp;
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200291 const std::string chassisId;
Ed Tanouscf9e4172022-12-21 09:30:16 -0800292 const std::span<const std::string_view> types;
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200293 const std::string chassisSubNode;
Nan Zhou928fefb2022-03-28 08:45:00 -0700294 const bool efficientExpand;
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200295
296 private:
297 std::optional<std::vector<SensorData>> metadata;
298 DataCompleteCb dataComplete;
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100299};
300
301/**
Anthony Wilsond5005492019-07-31 16:34:17 -0500302 * Possible states for physical inventory leds
303 */
304enum class LedState
305{
306 OFF,
307 ON,
308 BLINK,
309 UNKNOWN
310};
311
312/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500313 * D-Bus inventory item associated with one or more sensors.
314 */
315class InventoryItem
316{
317 public:
Ed Tanous4e23a442022-06-06 09:57:26 -0700318 explicit InventoryItem(const std::string& objPath) : objectPath(objPath)
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500319 {
320 // Set inventory item name to last node of object path
George Liu28aa8de2021-02-01 15:13:30 +0800321 sdbusplus::message::object_path path(objectPath);
322 name = path.filename();
323 if (name.empty())
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500324 {
George Liu28aa8de2021-02-01 15:13:30 +0800325 BMCWEB_LOG_ERROR << "Failed to find '/' in " << objectPath;
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500326 }
327 }
328
329 std::string objectPath;
330 std::string name;
Ed Tanouse05aec52022-01-25 10:28:56 -0800331 bool isPresent = true;
332 bool isFunctional = true;
333 bool isPowerSupply = false;
334 int powerSupplyEfficiencyPercent = -1;
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500335 std::string manufacturer;
336 std::string model;
337 std::string partNumber;
338 std::string serialNumber;
339 std::set<std::string> sensors;
Anthony Wilsond5005492019-07-31 16:34:17 -0500340 std::string ledObjectPath;
Ed Tanouse05aec52022-01-25 10:28:56 -0800341 LedState ledState = LedState::UNKNOWN;
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500342};
343
344/**
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530345 * @brief Get objects with connection necessary for sensors
Kowalski, Kamil588c3f02018-04-03 14:55:27 +0200346 * @param SensorsAsyncResp Pointer to object holding response data
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100347 * @param sensorNames Sensors retrieved from chassis
348 * @param callback Callback for processing gathered connections
349 */
350template <typename Callback>
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530351void getObjectsWithConnection(
Ed Tanous81ce6092020-12-17 16:54:55 +0000352 const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
Nan Zhoufe04d492022-06-22 17:10:41 +0000353 const std::shared_ptr<std::set<std::string>>& sensorNames,
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530354 Callback&& callback)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700355{
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530356 BMCWEB_LOG_DEBUG << "getObjectsWithConnection enter";
Ed Tanous1abe55e2018-09-05 08:30:59 -0700357 const std::string path = "/xyz/openbmc_project/sensors";
George Liue99073f2022-12-09 11:06:16 +0800358 constexpr std::array<std::string_view, 1> interfaces = {
Ed Tanous1abe55e2018-09-05 08:30:59 -0700359 "xyz.openbmc_project.Sensor.Value"};
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100360
George Liue99073f2022-12-09 11:06:16 +0800361 // Make call to ObjectMapper to find all sensors objects
362 dbus::utility::getSubTree(
363 path, 2, interfaces,
Ed Tanous002d39b2022-05-31 08:59:27 -0700364 [callback{std::forward<Callback>(callback)}, sensorsAsyncResp,
George Liue99073f2022-12-09 11:06:16 +0800365 sensorNames](const boost::system::error_code& ec,
Ed Tanous002d39b2022-05-31 08:59:27 -0700366 const dbus::utility::MapperGetSubTreeResponse& subtree) {
George Liue99073f2022-12-09 11:06:16 +0800367 // Response handler for parsing objects subtree
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530368 BMCWEB_LOG_DEBUG << "getObjectsWithConnection resp_handler enter";
Ed Tanous1abe55e2018-09-05 08:30:59 -0700369 if (ec)
370 {
zhanghch058d1b46d2021-04-01 11:18:24 +0800371 messages::internalError(sensorsAsyncResp->asyncResp->res);
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530372 BMCWEB_LOG_ERROR
373 << "getObjectsWithConnection resp_handler: Dbus error " << ec;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700374 return;
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100375 }
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100376
Ed Tanous1abe55e2018-09-05 08:30:59 -0700377 BMCWEB_LOG_DEBUG << "Found " << subtree.size() << " subtrees";
378
379 // Make unique list of connections only for requested sensor types and
380 // found in the chassis
Nan Zhoufe04d492022-06-22 17:10:41 +0000381 std::set<std::string> connections;
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530382 std::set<std::pair<std::string, std::string>> objectsWithConnection;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700383
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700384 BMCWEB_LOG_DEBUG << "sensorNames list count: " << sensorNames->size();
385 for (const std::string& tsensor : *sensorNames)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700386 {
387 BMCWEB_LOG_DEBUG << "Sensor to find: " << tsensor;
388 }
389
390 for (const std::pair<
391 std::string,
392 std::vector<std::pair<std::string, std::vector<std::string>>>>&
393 object : subtree)
394 {
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700395 if (sensorNames->find(object.first) != sensorNames->end())
Ed Tanous1abe55e2018-09-05 08:30:59 -0700396 {
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700397 for (const std::pair<std::string, std::vector<std::string>>&
398 objData : object.second)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700399 {
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700400 BMCWEB_LOG_DEBUG << "Adding connection: " << objData.first;
401 connections.insert(objData.first);
402 objectsWithConnection.insert(
403 std::make_pair(object.first, objData.first));
Ed Tanous1abe55e2018-09-05 08:30:59 -0700404 }
405 }
406 }
407 BMCWEB_LOG_DEBUG << "Found " << connections.size() << " connections";
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530408 callback(std::move(connections), std::move(objectsWithConnection));
409 BMCWEB_LOG_DEBUG << "getObjectsWithConnection resp_handler exit";
George Liue99073f2022-12-09 11:06:16 +0800410 });
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530411 BMCWEB_LOG_DEBUG << "getObjectsWithConnection exit";
412}
413
414/**
415 * @brief Create connections necessary for sensors
416 * @param SensorsAsyncResp Pointer to object holding response data
417 * @param sensorNames Sensors retrieved from chassis
418 * @param callback Callback for processing gathered connections
419 */
420template <typename Callback>
Nan Zhoufe04d492022-06-22 17:10:41 +0000421void getConnections(std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
422 const std::shared_ptr<std::set<std::string>> sensorNames,
423 Callback&& callback)
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530424{
425 auto objectsWithConnectionCb =
Nan Zhoufe04d492022-06-22 17:10:41 +0000426 [callback](const std::set<std::string>& connections,
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530427 const std::set<std::pair<std::string, std::string>>&
Ed Tanous3174e4d2020-10-07 11:41:22 -0700428 /*objectsWithConnection*/) { callback(connections); };
Ed Tanous81ce6092020-12-17 16:54:55 +0000429 getObjectsWithConnection(sensorsAsyncResp, sensorNames,
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530430 std::move(objectsWithConnectionCb));
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100431}
432
433/**
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700434 * @brief Shrinks the list of sensors for processing
435 * @param SensorsAysncResp The class holding the Redfish response
436 * @param allSensors A list of all the sensors associated to the
437 * chassis element (i.e. baseboard, front panel, etc...)
438 * @param activeSensors A list that is a reduction of the incoming
439 * allSensors list. Eliminate Thermal sensors when a Power request is
440 * made, and eliminate Power sensors when a Thermal request is made.
441 */
Ed Tanous23a21a12020-07-25 04:45:05 +0000442inline void reduceSensorList(
Ed Tanous7f1cc262022-08-09 13:33:57 -0700443 crow::Response& res, std::string_view chassisSubNode,
Ed Tanouscf9e4172022-12-21 09:30:16 -0800444 std::span<const std::string_view> sensorTypes,
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700445 const std::vector<std::string>* allSensors,
Nan Zhoufe04d492022-06-22 17:10:41 +0000446 const std::shared_ptr<std::set<std::string>>& activeSensors)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700447{
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700448 if ((allSensors == nullptr) || (activeSensors == nullptr))
449 {
Ed Tanous7f1cc262022-08-09 13:33:57 -0700450 messages::resourceNotFound(res, chassisSubNode,
451 chassisSubNode == sensors::node::thermal
452 ? "Temperatures"
453 : "Voltages");
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700454
455 return;
456 }
457 if (allSensors->empty())
458 {
459 // Nothing to do, the activeSensors object is also empty
460 return;
461 }
462
Ed Tanous7f1cc262022-08-09 13:33:57 -0700463 for (std::string_view type : sensorTypes)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700464 {
465 for (const std::string& sensor : *allSensors)
466 {
Ed Tanous11ba3972022-07-11 09:50:41 -0700467 if (sensor.starts_with(type))
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700468 {
469 activeSensors->emplace(sensor);
470 }
471 }
472 }
473}
474
Ed Tanous7f1cc262022-08-09 13:33:57 -0700475/*
476 *Populates the top level collection for a given subnode. Populates
477 *SensorCollection, Power, or Thermal schemas.
478 *
479 * */
480inline void populateChassisNode(nlohmann::json& jsonValue,
481 std::string_view chassisSubNode)
482{
483 if (chassisSubNode == sensors::node::power)
484 {
485 jsonValue["@odata.type"] = "#Power.v1_5_2.Power";
486 }
487 else if (chassisSubNode == sensors::node::thermal)
488 {
489 jsonValue["@odata.type"] = "#Thermal.v1_4_0.Thermal";
490 jsonValue["Fans"] = nlohmann::json::array();
491 jsonValue["Temperatures"] = nlohmann::json::array();
492 }
493 else if (chassisSubNode == sensors::node::sensors)
494 {
495 jsonValue["@odata.type"] = "#SensorCollection.SensorCollection";
496 jsonValue["Description"] = "Collection of Sensors for this Chassis";
497 jsonValue["Members"] = nlohmann::json::array();
498 jsonValue["Members@odata.count"] = 0;
499 }
500
501 if (chassisSubNode != sensors::node::sensors)
502 {
503 jsonValue["Id"] = chassisSubNode;
504 }
505 jsonValue["Name"] = chassisSubNode;
506}
507
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700508/**
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100509 * @brief Retrieves requested chassis sensors and redundancy data from DBus .
Kowalski, Kamil588c3f02018-04-03 14:55:27 +0200510 * @param SensorsAsyncResp Pointer to object holding response data
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100511 * @param callback Callback for next step in gathered sensor processing
512 */
513template <typename Callback>
Ed Tanous7f1cc262022-08-09 13:33:57 -0700514void getChassis(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
515 std::string_view chassisId, std::string_view chassisSubNode,
Ed Tanouscf9e4172022-12-21 09:30:16 -0800516 std::span<const std::string_view> sensorTypes,
517 Callback&& callback)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700518{
519 BMCWEB_LOG_DEBUG << "getChassis enter";
George Liu7a1dbc42022-12-07 16:03:22 +0800520 constexpr std::array<std::string_view, 2> interfaces = {
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700521 "xyz.openbmc_project.Inventory.Item.Board",
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500522 "xyz.openbmc_project.Inventory.Item.Chassis"};
George Liu7a1dbc42022-12-07 16:03:22 +0800523
524 // Get the Chassis Collection
525 dbus::utility::getSubTreePaths(
526 "/xyz/openbmc_project/inventory", 0, interfaces,
Ed Tanous7f1cc262022-08-09 13:33:57 -0700527 [callback{std::forward<Callback>(callback)}, asyncResp,
528 chassisIdStr{std::string(chassisId)},
529 chassisSubNode{std::string(chassisSubNode)}, sensorTypes](
George Liu7a1dbc42022-12-07 16:03:22 +0800530 const boost::system::error_code& ec,
Ed Tanous002d39b2022-05-31 08:59:27 -0700531 const dbus::utility::MapperGetSubTreePathsResponse& chassisPaths) {
Ed Tanous1abe55e2018-09-05 08:30:59 -0700532 BMCWEB_LOG_DEBUG << "getChassis respHandler enter";
533 if (ec)
534 {
535 BMCWEB_LOG_ERROR << "getChassis respHandler DBUS error: " << ec;
Ed Tanous7f1cc262022-08-09 13:33:57 -0700536 messages::internalError(asyncResp->res);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700537 return;
538 }
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700539 const std::string* chassisPath = nullptr;
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700540 for (const std::string& chassis : chassisPaths)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700541 {
George Liu28aa8de2021-02-01 15:13:30 +0800542 sdbusplus::message::object_path path(chassis);
Ed Tanousf8fe53e2022-06-30 15:55:45 -0700543 std::string chassisName = path.filename();
George Liu28aa8de2021-02-01 15:13:30 +0800544 if (chassisName.empty())
Ed Tanous1abe55e2018-09-05 08:30:59 -0700545 {
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700546 BMCWEB_LOG_ERROR << "Failed to find '/' in " << chassis;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700547 continue;
548 }
Ed Tanous7f1cc262022-08-09 13:33:57 -0700549 if (chassisName == chassisIdStr)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700550 {
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700551 chassisPath = &chassis;
552 break;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700553 }
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700554 }
555 if (chassisPath == nullptr)
556 {
Ed Tanous7f1cc262022-08-09 13:33:57 -0700557 messages::resourceNotFound(asyncResp->res, "Chassis", chassisIdStr);
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700558 return;
559 }
Ed Tanous7f1cc262022-08-09 13:33:57 -0700560 populateChassisNode(asyncResp->res.jsonValue, chassisSubNode);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700561
Willy Tueddfc432022-09-26 16:46:38 +0000562 asyncResp->res.jsonValue["@odata.id"] = crow::utility::urlFromPieces(
563 "redfish", "v1", "Chassis", chassisIdStr, chassisSubNode);
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500564
Shawn McCarney8fb49dd2019-06-12 17:47:00 -0500565 // Get the list of all sensors for this Chassis element
566 std::string sensorPath = *chassisPath + "/all_sensors";
Jonathan Doman1e1e5982021-06-11 09:36:17 -0700567 sdbusplus::asio::getProperty<std::vector<std::string>>(
568 *crow::connections::systemBus, "xyz.openbmc_project.ObjectMapper",
569 sensorPath, "xyz.openbmc_project.Association", "endpoints",
Ed Tanous7f1cc262022-08-09 13:33:57 -0700570 [asyncResp, chassisSubNode, sensorTypes,
Ed Tanousf94c4ec2022-01-06 12:44:41 -0800571 callback{std::forward<const Callback>(callback)}](
Ed Tanous271584a2019-07-09 16:24:22 -0700572 const boost::system::error_code& e,
Jonathan Doman1e1e5982021-06-11 09:36:17 -0700573 const std::vector<std::string>& nodeSensorList) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700574 if (e)
575 {
576 if (e.value() != EBADR)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700577 {
Ed Tanous7f1cc262022-08-09 13:33:57 -0700578 messages::internalError(asyncResp->res);
Ed Tanous002d39b2022-05-31 08:59:27 -0700579 return;
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700580 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700581 }
Nan Zhoufe04d492022-06-22 17:10:41 +0000582 const std::shared_ptr<std::set<std::string>> culledSensorList =
583 std::make_shared<std::set<std::string>>();
Ed Tanous7f1cc262022-08-09 13:33:57 -0700584 reduceSensorList(asyncResp->res, chassisSubNode, sensorTypes,
585 &nodeSensorList, culledSensorList);
586 BMCWEB_LOG_DEBUG << "Finishing with " << culledSensorList->size();
Ed Tanous002d39b2022-05-31 08:59:27 -0700587 callback(culledSensorList);
Jonathan Doman1e1e5982021-06-11 09:36:17 -0700588 });
George Liu7a1dbc42022-12-07 16:03:22 +0800589 });
Ed Tanous1abe55e2018-09-05 08:30:59 -0700590 BMCWEB_LOG_DEBUG << "getChassis exit";
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100591}
592
593/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500594 * @brief Returns the Redfish State value for the specified inventory item.
595 * @param inventoryItem D-Bus inventory item associated with a sensor.
596 * @return State value for inventory item.
James Feist34dd1792019-05-17 14:10:54 -0700597 */
Ed Tanous23a21a12020-07-25 04:45:05 +0000598inline std::string getState(const InventoryItem* inventoryItem)
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500599{
600 if ((inventoryItem != nullptr) && !(inventoryItem->isPresent))
601 {
602 return "Absent";
603 }
James Feist34dd1792019-05-17 14:10:54 -0700604
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500605 return "Enabled";
606}
607
608/**
609 * @brief Returns the Redfish Health value for the specified sensor.
610 * @param sensorJson Sensor JSON object.
Ed Tanous1d7c0052022-08-09 12:32:26 -0700611 * @param valuesDict Map of all sensor DBus values.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500612 * @param inventoryItem D-Bus inventory item associated with the sensor. Will
613 * be nullptr if no associated inventory item was found.
614 * @return Health value for sensor.
615 */
Ed Tanous1d7c0052022-08-09 12:32:26 -0700616inline std::string getHealth(nlohmann::json& sensorJson,
617 const dbus::utility::DBusPropertiesMap& valuesDict,
618 const InventoryItem* inventoryItem)
James Feist34dd1792019-05-17 14:10:54 -0700619{
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500620 // Get current health value (if any) in the sensor JSON object. Some JSON
621 // objects contain multiple sensors (such as PowerSupplies). We want to set
622 // the overall health to be the most severe of any of the sensors.
623 std::string currentHealth;
624 auto statusIt = sensorJson.find("Status");
625 if (statusIt != sensorJson.end())
626 {
627 auto healthIt = statusIt->find("Health");
628 if (healthIt != statusIt->end())
629 {
630 std::string* health = healthIt->get_ptr<std::string*>();
631 if (health != nullptr)
632 {
633 currentHealth = *health;
634 }
635 }
636 }
637
638 // If current health in JSON object is already Critical, return that. This
639 // should override the sensor health, which might be less severe.
640 if (currentHealth == "Critical")
641 {
642 return "Critical";
643 }
644
Krzysztof Grobelnyc1343bf2022-08-31 13:15:26 +0200645 const bool* criticalAlarmHigh = nullptr;
646 const bool* criticalAlarmLow = nullptr;
647 const bool* warningAlarmHigh = nullptr;
648 const bool* warningAlarmLow = nullptr;
Ed Tanous711ac7a2021-12-20 09:34:41 -0800649
Krzysztof Grobelnyc1343bf2022-08-31 13:15:26 +0200650 const bool success = sdbusplus::unpackPropertiesNoThrow(
651 dbus_utils::UnpackErrorPrinter(), valuesDict, "CriticalAlarmHigh",
652 criticalAlarmHigh, "CriticalAlarmLow", criticalAlarmLow,
653 "WarningAlarmHigh", warningAlarmHigh, "WarningAlarmLow",
654 warningAlarmLow);
655
656 if (success)
James Feist34dd1792019-05-17 14:10:54 -0700657 {
Krzysztof Grobelnyc1343bf2022-08-31 13:15:26 +0200658 // Check if sensor has critical threshold alarm
659 if ((criticalAlarmHigh != nullptr && *criticalAlarmHigh) ||
660 (criticalAlarmLow != nullptr && *criticalAlarmLow))
James Feist34dd1792019-05-17 14:10:54 -0700661 {
Krzysztof Grobelnyc1343bf2022-08-31 13:15:26 +0200662 return "Critical";
James Feist34dd1792019-05-17 14:10:54 -0700663 }
664 }
665
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500666 // Check if associated inventory item is not functional
667 if ((inventoryItem != nullptr) && !(inventoryItem->isFunctional))
668 {
669 return "Critical";
670 }
671
Krzysztof Grobelnyc1343bf2022-08-31 13:15:26 +0200672 // If current health in JSON object is already Warning, return that. This
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500673 // should override the sensor status, which might be less severe.
674 if (currentHealth == "Warning")
675 {
676 return "Warning";
677 }
678
Krzysztof Grobelnyc1343bf2022-08-31 13:15:26 +0200679 if (success)
James Feist34dd1792019-05-17 14:10:54 -0700680 {
Krzysztof Grobelnyc1343bf2022-08-31 13:15:26 +0200681 // Check if sensor has warning threshold alarm
682 if ((warningAlarmHigh != nullptr && *warningAlarmHigh) ||
683 (warningAlarmLow != nullptr && *warningAlarmLow))
James Feist34dd1792019-05-17 14:10:54 -0700684 {
Krzysztof Grobelnyc1343bf2022-08-31 13:15:26 +0200685 return "Warning";
James Feist34dd1792019-05-17 14:10:54 -0700686 }
687 }
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500688
James Feist34dd1792019-05-17 14:10:54 -0700689 return "OK";
690}
691
Ed Tanous23a21a12020-07-25 04:45:05 +0000692inline void setLedState(nlohmann::json& sensorJson,
Anthony Wilsond5005492019-07-31 16:34:17 -0500693 const InventoryItem* inventoryItem)
694{
695 if (inventoryItem != nullptr && !inventoryItem->ledObjectPath.empty())
696 {
697 switch (inventoryItem->ledState)
698 {
699 case LedState::OFF:
700 sensorJson["IndicatorLED"] = "Off";
701 break;
702 case LedState::ON:
703 sensorJson["IndicatorLED"] = "Lit";
704 break;
705 case LedState::BLINK:
706 sensorJson["IndicatorLED"] = "Blinking";
707 break;
Ed Tanous23a21a12020-07-25 04:45:05 +0000708 case LedState::UNKNOWN:
Anthony Wilsond5005492019-07-31 16:34:17 -0500709 break;
710 }
711 }
712}
713
James Feist34dd1792019-05-17 14:10:54 -0700714/**
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100715 * @brief Builds a json sensor representation of a sensor.
716 * @param sensorName The name of the sensor to be built
Gunnar Mills274fad52018-06-13 15:45:36 -0500717 * @param sensorType The type (temperature, fan_tach, etc) of the sensor to
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100718 * build
Ed Tanous1d7c0052022-08-09 12:32:26 -0700719 * @param chassisSubNode The subnode (thermal, sensor, ect) of the sensor
720 * @param propertiesDict A dictionary of the properties to build the sensor
721 * from.
722 * @param sensorJson The json object to fill
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500723 * @param inventoryItem D-Bus inventory item associated with the sensor. Will
724 * be nullptr if no associated inventory item was found.
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100725 */
Ed Tanous1d7c0052022-08-09 12:32:26 -0700726inline void objectPropertiesToJson(
727 std::string_view sensorName, std::string_view sensorType,
728 std::string_view chassisSubNode,
729 const dbus::utility::DBusPropertiesMap& propertiesDict,
Ed Tanous81ce6092020-12-17 16:54:55 +0000730 nlohmann::json& sensorJson, InventoryItem* inventoryItem)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700731{
Ed Tanous1d7c0052022-08-09 12:32:26 -0700732 if (chassisSubNode == sensors::node::sensors)
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500733 {
Ed Tanousc71d6122022-11-29 14:10:32 -0800734 std::string subNodeEscaped(sensorType);
Ed Tanousc1d019a2022-08-06 09:36:06 -0700735 subNodeEscaped.erase(
736 std::remove(subNodeEscaped.begin(), subNodeEscaped.end(), '_'),
737 subNodeEscaped.end());
738
739 // For sensors in SensorCollection we set Id instead of MemberId,
740 // including power sensors.
741 subNodeEscaped += '_';
742 subNodeEscaped += sensorName;
743 sensorJson["Id"] = std::move(subNodeEscaped);
744
Ed Tanous1d7c0052022-08-09 12:32:26 -0700745 std::string sensorNameEs(sensorName);
746 std::replace(sensorNameEs.begin(), sensorNameEs.end(), '_', ' ');
747 sensorJson["Name"] = std::move(sensorNameEs);
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500748 }
749 else if (sensorType != "power")
750 {
751 // Set MemberId and Name for non-power sensors. For PowerSupplies and
752 // PowerControl, those properties have more general values because
753 // multiple sensors can be stored in the same JSON object.
Ed Tanous81ce6092020-12-17 16:54:55 +0000754 sensorJson["MemberId"] = sensorName;
Ed Tanous1d7c0052022-08-09 12:32:26 -0700755 std::string sensorNameEs(sensorName);
756 std::replace(sensorNameEs.begin(), sensorNameEs.end(), '_', ' ');
757 sensorJson["Name"] = std::move(sensorNameEs);
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500758 }
Ed Tanouse742b6c2019-05-03 15:06:53 -0700759
Ed Tanous81ce6092020-12-17 16:54:55 +0000760 sensorJson["Status"]["State"] = getState(inventoryItem);
761 sensorJson["Status"]["Health"] =
Ed Tanous1d7c0052022-08-09 12:32:26 -0700762 getHealth(sensorJson, propertiesDict, inventoryItem);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700763
764 // Parameter to set to override the type we get from dbus, and force it to
765 // int, regardless of what is available. This is used for schemas like fan,
766 // that require integers, not floats.
767 bool forceToInt = false;
768
Anthony Wilson3929aca2019-07-19 15:42:33 -0500769 nlohmann::json::json_pointer unit("/Reading");
Ed Tanous1d7c0052022-08-09 12:32:26 -0700770 if (chassisSubNode == sensors::node::sensors)
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500771 {
Shounak Mitra2a4ba192022-06-01 23:34:12 +0000772 sensorJson["@odata.type"] = "#Sensor.v1_2_0.Sensor";
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000773
Ed Tanous0ec8b832022-03-14 14:56:47 -0700774 sensor::ReadingType readingType = sensors::toReadingType(sensorType);
775 if (readingType == sensor::ReadingType::Invalid)
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500776 {
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000777 BMCWEB_LOG_ERROR << "Redfish cannot map reading type for "
778 << sensorType;
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500779 }
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000780 else
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500781 {
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000782 sensorJson["ReadingType"] = readingType;
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500783 }
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000784
Ed Tanous1d7c0052022-08-09 12:32:26 -0700785 std::string_view readingUnits = sensors::toReadingUnits(sensorType);
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000786 if (readingUnits.empty())
Adrian Ambrożewiczf8ede152020-06-02 13:26:33 +0200787 {
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000788 BMCWEB_LOG_ERROR << "Redfish cannot map reading unit for "
789 << sensorType;
790 }
791 else
792 {
793 sensorJson["ReadingUnits"] = readingUnits;
Adrian Ambrożewiczf8ede152020-06-02 13:26:33 +0200794 }
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500795 }
796 else if (sensorType == "temperature")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700797 {
Anthony Wilson3929aca2019-07-19 15:42:33 -0500798 unit = "/ReadingCelsius"_json_pointer;
Ed Tanous81ce6092020-12-17 16:54:55 +0000799 sensorJson["@odata.type"] = "#Thermal.v1_3_0.Temperature";
Ed Tanous1abe55e2018-09-05 08:30:59 -0700800 // TODO(ed) Documentation says that path should be type fan_tach,
801 // implementation seems to implement fan
802 }
803 else if (sensorType == "fan" || sensorType == "fan_tach")
804 {
Anthony Wilson3929aca2019-07-19 15:42:33 -0500805 unit = "/Reading"_json_pointer;
Ed Tanous81ce6092020-12-17 16:54:55 +0000806 sensorJson["ReadingUnits"] = "RPM";
807 sensorJson["@odata.type"] = "#Thermal.v1_3_0.Fan";
808 setLedState(sensorJson, inventoryItem);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700809 forceToInt = true;
810 }
Ed Tanous6f6d0d32018-10-12 11:16:43 -0700811 else if (sensorType == "fan_pwm")
812 {
Anthony Wilson3929aca2019-07-19 15:42:33 -0500813 unit = "/Reading"_json_pointer;
Ed Tanous81ce6092020-12-17 16:54:55 +0000814 sensorJson["ReadingUnits"] = "Percent";
815 sensorJson["@odata.type"] = "#Thermal.v1_3_0.Fan";
816 setLedState(sensorJson, inventoryItem);
Ed Tanous6f6d0d32018-10-12 11:16:43 -0700817 forceToInt = true;
818 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700819 else if (sensorType == "voltage")
820 {
Anthony Wilson3929aca2019-07-19 15:42:33 -0500821 unit = "/ReadingVolts"_json_pointer;
Ed Tanous81ce6092020-12-17 16:54:55 +0000822 sensorJson["@odata.type"] = "#Power.v1_0_0.Voltage";
Ed Tanous1abe55e2018-09-05 08:30:59 -0700823 }
Ed Tanous2474adf2018-09-05 16:31:16 -0700824 else if (sensorType == "power")
825 {
Ed Tanous1d7c0052022-08-09 12:32:26 -0700826 if (boost::iequals(sensorName, "total_power"))
Eddie James028f7eb2019-05-17 21:24:36 +0000827 {
Ed Tanous81ce6092020-12-17 16:54:55 +0000828 sensorJson["@odata.type"] = "#Power.v1_0_0.PowerControl";
Gunnar Mills7ab06f42019-07-02 13:07:16 -0500829 // Put multiple "sensors" into a single PowerControl, so have
830 // generic names for MemberId and Name. Follows Redfish mockup.
Ed Tanous81ce6092020-12-17 16:54:55 +0000831 sensorJson["MemberId"] = "0";
832 sensorJson["Name"] = "Chassis Power Control";
Anthony Wilson3929aca2019-07-19 15:42:33 -0500833 unit = "/PowerConsumedWatts"_json_pointer;
Eddie James028f7eb2019-05-17 21:24:36 +0000834 }
Ed Tanous1d7c0052022-08-09 12:32:26 -0700835 else if (boost::ifind_first(sensorName, "input").empty())
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700836 {
Anthony Wilson3929aca2019-07-19 15:42:33 -0500837 unit = "/PowerInputWatts"_json_pointer;
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700838 }
839 else
840 {
Anthony Wilson3929aca2019-07-19 15:42:33 -0500841 unit = "/PowerOutputWatts"_json_pointer;
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700842 }
Ed Tanous2474adf2018-09-05 16:31:16 -0700843 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700844 else
845 {
846 BMCWEB_LOG_ERROR << "Redfish cannot map object type for " << sensorName;
847 return;
848 }
849 // Map of dbus interface name, dbus property name and redfish property_name
Anthony Wilson3929aca2019-07-19 15:42:33 -0500850 std::vector<
851 std::tuple<const char*, const char*, nlohmann::json::json_pointer>>
852 properties;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700853 properties.reserve(7);
854
855 properties.emplace_back("xyz.openbmc_project.Sensor.Value", "Value", unit);
Shawn McCarneyde629b62019-03-08 10:42:51 -0600856
Ed Tanous1d7c0052022-08-09 12:32:26 -0700857 if (chassisSubNode == sensors::node::sensors)
Anthony Wilson3929aca2019-07-19 15:42:33 -0500858 {
859 properties.emplace_back(
860 "xyz.openbmc_project.Sensor.Threshold.Warning", "WarningHigh",
861 "/Thresholds/UpperCaution/Reading"_json_pointer);
862 properties.emplace_back(
863 "xyz.openbmc_project.Sensor.Threshold.Warning", "WarningLow",
864 "/Thresholds/LowerCaution/Reading"_json_pointer);
865 properties.emplace_back(
866 "xyz.openbmc_project.Sensor.Threshold.Critical", "CriticalHigh",
867 "/Thresholds/UpperCritical/Reading"_json_pointer);
868 properties.emplace_back(
869 "xyz.openbmc_project.Sensor.Threshold.Critical", "CriticalLow",
870 "/Thresholds/LowerCritical/Reading"_json_pointer);
871 }
872 else if (sensorType != "power")
Shawn McCarneyde629b62019-03-08 10:42:51 -0600873 {
874 properties.emplace_back("xyz.openbmc_project.Sensor.Threshold.Warning",
Anthony Wilson3929aca2019-07-19 15:42:33 -0500875 "WarningHigh",
876 "/UpperThresholdNonCritical"_json_pointer);
Shawn McCarneyde629b62019-03-08 10:42:51 -0600877 properties.emplace_back("xyz.openbmc_project.Sensor.Threshold.Warning",
Anthony Wilson3929aca2019-07-19 15:42:33 -0500878 "WarningLow",
879 "/LowerThresholdNonCritical"_json_pointer);
Shawn McCarneyde629b62019-03-08 10:42:51 -0600880 properties.emplace_back("xyz.openbmc_project.Sensor.Threshold.Critical",
Anthony Wilson3929aca2019-07-19 15:42:33 -0500881 "CriticalHigh",
882 "/UpperThresholdCritical"_json_pointer);
Shawn McCarneyde629b62019-03-08 10:42:51 -0600883 properties.emplace_back("xyz.openbmc_project.Sensor.Threshold.Critical",
Anthony Wilson3929aca2019-07-19 15:42:33 -0500884 "CriticalLow",
885 "/LowerThresholdCritical"_json_pointer);
Shawn McCarneyde629b62019-03-08 10:42:51 -0600886 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700887
Ed Tanous2474adf2018-09-05 16:31:16 -0700888 // TODO Need to get UpperThresholdFatal and LowerThresholdFatal
889
Ed Tanous1d7c0052022-08-09 12:32:26 -0700890 if (chassisSubNode == sensors::node::sensors)
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500891 {
892 properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MinValue",
Anthony Wilson3929aca2019-07-19 15:42:33 -0500893 "/ReadingRangeMin"_json_pointer);
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500894 properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MaxValue",
Anthony Wilson3929aca2019-07-19 15:42:33 -0500895 "/ReadingRangeMax"_json_pointer);
George Liu51c35a82022-10-13 20:22:14 +0800896 properties.emplace_back("xyz.openbmc_project.Sensor.Accuracy",
897 "Accuracy", "/Accuracy"_json_pointer);
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500898 }
899 else if (sensorType == "temperature")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700900 {
901 properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MinValue",
Anthony Wilson3929aca2019-07-19 15:42:33 -0500902 "/MinReadingRangeTemp"_json_pointer);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700903 properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MaxValue",
Anthony Wilson3929aca2019-07-19 15:42:33 -0500904 "/MaxReadingRangeTemp"_json_pointer);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700905 }
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500906 else if (sensorType != "power")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700907 {
908 properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MinValue",
Anthony Wilson3929aca2019-07-19 15:42:33 -0500909 "/MinReadingRange"_json_pointer);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700910 properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MaxValue",
Anthony Wilson3929aca2019-07-19 15:42:33 -0500911 "/MaxReadingRange"_json_pointer);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700912 }
913
Anthony Wilson3929aca2019-07-19 15:42:33 -0500914 for (const std::tuple<const char*, const char*,
915 nlohmann::json::json_pointer>& p : properties)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700916 {
Ed Tanous1d7c0052022-08-09 12:32:26 -0700917 for (const auto& [valueName, valueVariant] : propertiesDict)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700918 {
Ed Tanous1d7c0052022-08-09 12:32:26 -0700919 if (valueName != std::get<1>(p))
Ed Tanous1abe55e2018-09-05 08:30:59 -0700920 {
Ed Tanous711ac7a2021-12-20 09:34:41 -0800921 continue;
922 }
Ed Tanous1d7c0052022-08-09 12:32:26 -0700923
924 // The property we want to set may be nested json, so use
925 // a json_pointer for easy indexing into the json structure.
926 const nlohmann::json::json_pointer& key = std::get<2>(p);
927
Ed Tanous1d7c0052022-08-09 12:32:26 -0700928 const double* doubleValue = std::get_if<double>(&valueVariant);
Ed Tanous40e4f382022-08-09 18:42:51 -0700929 if (doubleValue == nullptr)
Ed Tanous711ac7a2021-12-20 09:34:41 -0800930 {
Ed Tanous40e4f382022-08-09 18:42:51 -0700931 BMCWEB_LOG_ERROR << "Got value interface that wasn't double";
Ed Tanous1d7c0052022-08-09 12:32:26 -0700932 continue;
933 }
Ed Tanous1d7c0052022-08-09 12:32:26 -0700934 if (forceToInt)
935 {
Ed Tanous40e4f382022-08-09 18:42:51 -0700936 sensorJson[key] = static_cast<int64_t>(*doubleValue);
Ed Tanous1d7c0052022-08-09 12:32:26 -0700937 }
938 else
939 {
Ed Tanous40e4f382022-08-09 18:42:51 -0700940 sensorJson[key] = *doubleValue;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700941 }
942 }
943 }
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100944}
945
Ed Tanous1d7c0052022-08-09 12:32:26 -0700946/**
947 * @brief Builds a json sensor representation of a sensor.
948 * @param sensorName The name of the sensor to be built
949 * @param sensorType The type (temperature, fan_tach, etc) of the sensor to
950 * build
951 * @param chassisSubNode The subnode (thermal, sensor, ect) of the sensor
952 * @param interfacesDict A dictionary of the interfaces and properties of said
953 * interfaces to be built from
954 * @param sensorJson The json object to fill
955 * @param inventoryItem D-Bus inventory item associated with the sensor. Will
956 * be nullptr if no associated inventory item was found.
957 */
958inline void objectInterfacesToJson(
959 const std::string& sensorName, const std::string& sensorType,
960 const std::string& chassisSubNode,
961 const dbus::utility::DBusInteracesMap& interfacesDict,
962 nlohmann::json& sensorJson, InventoryItem* inventoryItem)
963{
964
965 for (const auto& [interface, valuesDict] : interfacesDict)
966 {
967 objectPropertiesToJson(sensorName, sensorType, chassisSubNode,
968 valuesDict, sensorJson, inventoryItem);
969 }
Ed Tanousc1d019a2022-08-06 09:36:06 -0700970 BMCWEB_LOG_DEBUG << "Added sensor " << sensorName;
Ed Tanous1d7c0052022-08-09 12:32:26 -0700971}
972
Ed Tanousb5a76932020-09-29 16:16:58 -0700973inline void populateFanRedundancy(
974 const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp)
James Feist8bd25cc2019-03-15 15:14:00 -0700975{
George Liue99073f2022-12-09 11:06:16 +0800976 constexpr std::array<std::string_view, 1> interfaces = {
977 "xyz.openbmc_project.Control.FanRedundancy"};
978 dbus::utility::getSubTree(
979 "/xyz/openbmc_project/control", 2, interfaces,
Ed Tanousb9d36b42022-02-26 21:42:46 -0800980 [sensorsAsyncResp](
George Liue99073f2022-12-09 11:06:16 +0800981 const boost::system::error_code& ec,
Ed Tanousb9d36b42022-02-26 21:42:46 -0800982 const dbus::utility::MapperGetSubTreeResponse& resp) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700983 if (ec)
984 {
985 return; // don't have to have this interface
986 }
987 for (const std::pair<
988 std::string,
989 std::vector<std::pair<std::string, std::vector<std::string>>>>&
990 pathPair : resp)
991 {
992 const std::string& path = pathPair.first;
993 const std::vector<std::pair<std::string, std::vector<std::string>>>&
994 objDict = pathPair.second;
995 if (objDict.empty())
James Feist8bd25cc2019-03-15 15:14:00 -0700996 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700997 continue; // this should be impossible
James Feist8bd25cc2019-03-15 15:14:00 -0700998 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700999
1000 const std::string& owner = objDict.begin()->first;
1001 sdbusplus::asio::getProperty<std::vector<std::string>>(
1002 *crow::connections::systemBus,
1003 "xyz.openbmc_project.ObjectMapper", path + "/chassis",
1004 "xyz.openbmc_project.Association", "endpoints",
1005 [path, owner,
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08001006 sensorsAsyncResp](const boost::system::error_code& e,
Ed Tanous002d39b2022-05-31 08:59:27 -07001007 const std::vector<std::string>& endpoints) {
1008 if (e)
James Feist8bd25cc2019-03-15 15:14:00 -07001009 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001010 return; // if they don't have an association we
1011 // can't tell what chassis is
James Feist8bd25cc2019-03-15 15:14:00 -07001012 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001013 auto found =
1014 std::find_if(endpoints.begin(), endpoints.end(),
1015 [sensorsAsyncResp](const std::string& entry) {
1016 return entry.find(sensorsAsyncResp->chassisId) !=
1017 std::string::npos;
1018 });
James Feist8bd25cc2019-03-15 15:14:00 -07001019
Ed Tanous002d39b2022-05-31 08:59:27 -07001020 if (found == endpoints.end())
1021 {
1022 return;
1023 }
Krzysztof Grobelny86d89ed2022-08-29 14:49:20 +02001024 sdbusplus::asio::getAllProperties(
1025 *crow::connections::systemBus, owner, path,
1026 "xyz.openbmc_project.Control.FanRedundancy",
Ed Tanous002d39b2022-05-31 08:59:27 -07001027 [path, sensorsAsyncResp](
1028 const boost::system::error_code& err,
Krzysztof Grobelny86d89ed2022-08-29 14:49:20 +02001029 const dbus::utility::DBusPropertiesMap& ret) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001030 if (err)
1031 {
1032 return; // don't have to have this
1033 // interface
1034 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001035
Krzysztof Grobelny86d89ed2022-08-29 14:49:20 +02001036 const uint8_t* allowedFailures = nullptr;
1037 const std::vector<std::string>* collection = nullptr;
1038 const std::string* status = nullptr;
1039
1040 const bool success = sdbusplus::unpackPropertiesNoThrow(
1041 dbus_utils::UnpackErrorPrinter(), ret,
1042 "AllowedFailures", allowedFailures, "Collection",
1043 collection, "Status", status);
1044
1045 if (!success)
1046 {
1047 messages::internalError(
1048 sensorsAsyncResp->asyncResp->res);
1049 return;
1050 }
1051
1052 if (allowedFailures == nullptr || collection == nullptr ||
1053 status == nullptr)
Ed Tanous002d39b2022-05-31 08:59:27 -07001054 {
1055 BMCWEB_LOG_ERROR << "Invalid redundancy interface";
1056 messages::internalError(
1057 sensorsAsyncResp->asyncResp->res);
1058 return;
1059 }
1060
Ed Tanous002d39b2022-05-31 08:59:27 -07001061 sdbusplus::message::object_path objectPath(path);
1062 std::string name = objectPath.filename();
1063 if (name.empty())
1064 {
1065 // this should be impossible
1066 messages::internalError(
1067 sensorsAsyncResp->asyncResp->res);
1068 return;
1069 }
1070 std::replace(name.begin(), name.end(), '_', ' ');
1071
1072 std::string health;
1073
Ed Tanous11ba3972022-07-11 09:50:41 -07001074 if (status->ends_with("Full"))
Ed Tanous002d39b2022-05-31 08:59:27 -07001075 {
1076 health = "OK";
1077 }
Ed Tanous11ba3972022-07-11 09:50:41 -07001078 else if (status->ends_with("Degraded"))
Ed Tanous002d39b2022-05-31 08:59:27 -07001079 {
1080 health = "Warning";
1081 }
1082 else
1083 {
1084 health = "Critical";
1085 }
1086 nlohmann::json::array_t redfishCollection;
1087 const auto& fanRedfish =
1088 sensorsAsyncResp->asyncResp->res.jsonValue["Fans"];
1089 for (const std::string& item : *collection)
1090 {
Ed Tanous8a592812022-06-04 09:06:59 -07001091 sdbusplus::message::object_path itemPath(item);
1092 std::string itemName = itemPath.filename();
Ed Tanous002d39b2022-05-31 08:59:27 -07001093 if (itemName.empty())
James Feist8bd25cc2019-03-15 15:14:00 -07001094 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001095 continue;
James Feist8bd25cc2019-03-15 15:14:00 -07001096 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001097 /*
1098 todo(ed): merge patch that fixes the names
1099 std::replace(itemName.begin(),
1100 itemName.end(), '_', ' ');*/
1101 auto schemaItem =
1102 std::find_if(fanRedfish.begin(), fanRedfish.end(),
1103 [itemName](const nlohmann::json& fan) {
1104 return fan["MemberId"] == itemName;
James Feist8bd25cc2019-03-15 15:14:00 -07001105 });
Ed Tanous002d39b2022-05-31 08:59:27 -07001106 if (schemaItem != fanRedfish.end())
James Feist8bd25cc2019-03-15 15:14:00 -07001107 {
Ed Tanous8a592812022-06-04 09:06:59 -07001108 nlohmann::json::object_t collectionId;
1109 collectionId["@odata.id"] =
Ed Tanous002d39b2022-05-31 08:59:27 -07001110 (*schemaItem)["@odata.id"];
1111 redfishCollection.emplace_back(
Ed Tanous8a592812022-06-04 09:06:59 -07001112 std::move(collectionId));
Ed Tanous002d39b2022-05-31 08:59:27 -07001113 }
1114 else
1115 {
1116 BMCWEB_LOG_ERROR << "failed to find fan in schema";
1117 messages::internalError(
1118 sensorsAsyncResp->asyncResp->res);
James Feist8bd25cc2019-03-15 15:14:00 -07001119 return;
1120 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001121 }
James Feist8bd25cc2019-03-15 15:14:00 -07001122
Ed Tanous002d39b2022-05-31 08:59:27 -07001123 size_t minNumNeeded =
1124 collection->empty()
1125 ? 0
1126 : collection->size() - *allowedFailures;
1127 nlohmann::json& jResp = sensorsAsyncResp->asyncResp->res
1128 .jsonValue["Redundancy"];
James Feist8bd25cc2019-03-15 15:14:00 -07001129
Ed Tanous002d39b2022-05-31 08:59:27 -07001130 nlohmann::json::object_t redundancy;
Willy Tueddfc432022-09-26 16:46:38 +00001131 boost::urls::url url = crow::utility::urlFromPieces(
1132 "redfish", "v1", "Chassis", sensorsAsyncResp->chassisId,
1133 sensorsAsyncResp->chassisSubNode);
1134 url.set_fragment(("/Redundancy"_json_pointer / jResp.size())
1135 .to_string());
1136 redundancy["@odata.id"] = std::move(url);
Ed Tanous002d39b2022-05-31 08:59:27 -07001137 redundancy["@odata.type"] = "#Redundancy.v1_3_2.Redundancy";
1138 redundancy["MinNumNeeded"] = minNumNeeded;
1139 redundancy["MemberId"] = name;
1140 redundancy["Mode"] = "N+m";
1141 redundancy["Name"] = name;
1142 redundancy["RedundancySet"] = redfishCollection;
1143 redundancy["Status"]["Health"] = health;
1144 redundancy["Status"]["State"] = "Enabled";
James Feist8bd25cc2019-03-15 15:14:00 -07001145
Ed Tanous002d39b2022-05-31 08:59:27 -07001146 jResp.push_back(std::move(redundancy));
Krzysztof Grobelny86d89ed2022-08-29 14:49:20 +02001147 });
Ed Tanous002d39b2022-05-31 08:59:27 -07001148 });
1149 }
George Liue99073f2022-12-09 11:06:16 +08001150 });
James Feist8bd25cc2019-03-15 15:14:00 -07001151}
1152
Ed Tanousb5a76932020-09-29 16:16:58 -07001153inline void
Ed Tanous81ce6092020-12-17 16:54:55 +00001154 sortJSONResponse(const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07001155{
zhanghch058d1b46d2021-04-01 11:18:24 +08001156 nlohmann::json& response = sensorsAsyncResp->asyncResp->res.jsonValue;
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07001157 std::array<std::string, 2> sensorHeaders{"Temperatures", "Fans"};
Ed Tanous81ce6092020-12-17 16:54:55 +00001158 if (sensorsAsyncResp->chassisSubNode == sensors::node::power)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07001159 {
1160 sensorHeaders = {"Voltages", "PowerSupplies"};
1161 }
1162 for (const std::string& sensorGroup : sensorHeaders)
1163 {
1164 nlohmann::json::iterator entry = response.find(sensorGroup);
1165 if (entry != response.end())
1166 {
1167 std::sort(entry->begin(), entry->end(),
Ed Tanous02cad962022-06-30 16:50:15 -07001168 [](const nlohmann::json& c1, const nlohmann::json& c2) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001169 return c1["Name"] < c2["Name"];
1170 });
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07001171
1172 // add the index counts to the end of each entry
1173 size_t count = 0;
1174 for (nlohmann::json& sensorJson : *entry)
1175 {
1176 nlohmann::json::iterator odata = sensorJson.find("@odata.id");
1177 if (odata == sensorJson.end())
1178 {
1179 continue;
1180 }
1181 std::string* value = odata->get_ptr<std::string*>();
1182 if (value != nullptr)
1183 {
Willy Tueddfc432022-09-26 16:46:38 +00001184 *value += "/" + std::to_string(count);
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07001185 count++;
Ed Tanous81ce6092020-12-17 16:54:55 +00001186 sensorsAsyncResp->updateUri(sensorJson["Name"], *value);
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07001187 }
1188 }
1189 }
1190 }
1191}
1192
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +01001193/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001194 * @brief Finds the inventory item with the specified object path.
1195 * @param inventoryItems D-Bus inventory items associated with sensors.
1196 * @param invItemObjPath D-Bus object path of inventory item.
1197 * @return Inventory item within vector, or nullptr if no match found.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001198 */
Ed Tanous23a21a12020-07-25 04:45:05 +00001199inline InventoryItem* findInventoryItem(
Ed Tanousb5a76932020-09-29 16:16:58 -07001200 const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems,
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001201 const std::string& invItemObjPath)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001202{
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001203 for (InventoryItem& inventoryItem : *inventoryItems)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001204 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001205 if (inventoryItem.objectPath == invItemObjPath)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001206 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001207 return &inventoryItem;
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001208 }
1209 }
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001210 return nullptr;
1211}
1212
1213/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001214 * @brief Finds the inventory item associated with the specified sensor.
1215 * @param inventoryItems D-Bus inventory items associated with sensors.
1216 * @param sensorObjPath D-Bus object path of sensor.
1217 * @return Inventory item within vector, or nullptr if no match found.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001218 */
Ed Tanous23a21a12020-07-25 04:45:05 +00001219inline InventoryItem* findInventoryItemForSensor(
Ed Tanousb5a76932020-09-29 16:16:58 -07001220 const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems,
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001221 const std::string& sensorObjPath)
1222{
1223 for (InventoryItem& inventoryItem : *inventoryItems)
1224 {
1225 if (inventoryItem.sensors.count(sensorObjPath) > 0)
1226 {
1227 return &inventoryItem;
1228 }
1229 }
1230 return nullptr;
1231}
1232
1233/**
Anthony Wilsond5005492019-07-31 16:34:17 -05001234 * @brief Finds the inventory item associated with the specified led path.
1235 * @param inventoryItems D-Bus inventory items associated with sensors.
1236 * @param ledObjPath D-Bus object path of led.
1237 * @return Inventory item within vector, or nullptr if no match found.
1238 */
1239inline InventoryItem*
1240 findInventoryItemForLed(std::vector<InventoryItem>& inventoryItems,
1241 const std::string& ledObjPath)
1242{
1243 for (InventoryItem& inventoryItem : inventoryItems)
1244 {
1245 if (inventoryItem.ledObjectPath == ledObjPath)
1246 {
1247 return &inventoryItem;
1248 }
1249 }
1250 return nullptr;
1251}
1252
1253/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001254 * @brief Adds inventory item and associated sensor to specified vector.
1255 *
1256 * Adds a new InventoryItem to the vector if necessary. Searches for an
1257 * existing InventoryItem with the specified object path. If not found, one is
1258 * added to the vector.
1259 *
1260 * Next, the specified sensor is added to the set of sensors associated with the
1261 * InventoryItem.
1262 *
1263 * @param inventoryItems D-Bus inventory items associated with sensors.
1264 * @param invItemObjPath D-Bus object path of inventory item.
1265 * @param sensorObjPath D-Bus object path of sensor
1266 */
Ed Tanousb5a76932020-09-29 16:16:58 -07001267inline void addInventoryItem(
1268 const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems,
1269 const std::string& invItemObjPath, const std::string& sensorObjPath)
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001270{
1271 // Look for inventory item in vector
1272 InventoryItem* inventoryItem =
1273 findInventoryItem(inventoryItems, invItemObjPath);
1274
1275 // If inventory item doesn't exist in vector, add it
1276 if (inventoryItem == nullptr)
1277 {
1278 inventoryItems->emplace_back(invItemObjPath);
1279 inventoryItem = &(inventoryItems->back());
1280 }
1281
1282 // Add sensor to set of sensors associated with inventory item
1283 inventoryItem->sensors.emplace(sensorObjPath);
1284}
1285
1286/**
1287 * @brief Stores D-Bus data in the specified inventory item.
1288 *
1289 * Finds D-Bus data in the specified map of interfaces. Stores the data in the
1290 * specified InventoryItem.
1291 *
1292 * This data is later used to provide sensor property values in the JSON
1293 * response.
1294 *
1295 * @param inventoryItem Inventory item where data will be stored.
1296 * @param interfacesDict Map containing D-Bus interfaces and their properties
1297 * for the specified inventory item.
1298 */
Ed Tanous23a21a12020-07-25 04:45:05 +00001299inline void storeInventoryItemData(
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001300 InventoryItem& inventoryItem,
Ed Tanous711ac7a2021-12-20 09:34:41 -08001301 const dbus::utility::DBusInteracesMap& interfacesDict)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001302{
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001303 // Get properties from Inventory.Item interface
Ed Tanous711ac7a2021-12-20 09:34:41 -08001304
Ed Tanous9eb808c2022-01-25 10:19:23 -08001305 for (const auto& [interface, values] : interfacesDict)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001306 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001307 if (interface == "xyz.openbmc_project.Inventory.Item")
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001308 {
Ed Tanous9eb808c2022-01-25 10:19:23 -08001309 for (const auto& [name, dbusValue] : values)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001310 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001311 if (name == "Present")
1312 {
1313 const bool* value = std::get_if<bool>(&dbusValue);
1314 if (value != nullptr)
1315 {
1316 inventoryItem.isPresent = *value;
1317 }
1318 }
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001319 }
1320 }
Ed Tanous711ac7a2021-12-20 09:34:41 -08001321 // Check if Inventory.Item.PowerSupply interface is present
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001322
Ed Tanous711ac7a2021-12-20 09:34:41 -08001323 if (interface == "xyz.openbmc_project.Inventory.Item.PowerSupply")
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001324 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001325 inventoryItem.isPowerSupply = true;
1326 }
1327
1328 // Get properties from Inventory.Decorator.Asset interface
1329 if (interface == "xyz.openbmc_project.Inventory.Decorator.Asset")
1330 {
Ed Tanous9eb808c2022-01-25 10:19:23 -08001331 for (const auto& [name, dbusValue] : values)
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001332 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001333 if (name == "Manufacturer")
1334 {
1335 const std::string* value =
1336 std::get_if<std::string>(&dbusValue);
1337 if (value != nullptr)
1338 {
1339 inventoryItem.manufacturer = *value;
1340 }
1341 }
1342 if (name == "Model")
1343 {
1344 const std::string* value =
1345 std::get_if<std::string>(&dbusValue);
1346 if (value != nullptr)
1347 {
1348 inventoryItem.model = *value;
1349 }
1350 }
1351 if (name == "SerialNumber")
1352 {
1353 const std::string* value =
1354 std::get_if<std::string>(&dbusValue);
1355 if (value != nullptr)
1356 {
1357 inventoryItem.serialNumber = *value;
1358 }
1359 }
1360 if (name == "PartNumber")
1361 {
1362 const std::string* value =
1363 std::get_if<std::string>(&dbusValue);
1364 if (value != nullptr)
1365 {
1366 inventoryItem.partNumber = *value;
1367 }
1368 }
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001369 }
1370 }
1371
Ed Tanous711ac7a2021-12-20 09:34:41 -08001372 if (interface ==
1373 "xyz.openbmc_project.State.Decorator.OperationalStatus")
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001374 {
Ed Tanous9eb808c2022-01-25 10:19:23 -08001375 for (const auto& [name, dbusValue] : values)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001376 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001377 if (name == "Functional")
1378 {
1379 const bool* value = std::get_if<bool>(&dbusValue);
1380 if (value != nullptr)
1381 {
1382 inventoryItem.isFunctional = *value;
1383 }
1384 }
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001385 }
1386 }
1387 }
1388}
1389
1390/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001391 * @brief Gets D-Bus data for inventory items associated with sensors.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001392 *
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001393 * Uses the specified connections (services) to obtain D-Bus data for inventory
1394 * items associated with sensors. Stores the resulting data in the
1395 * inventoryItems vector.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001396 *
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001397 * This data is later used to provide sensor property values in the JSON
1398 * response.
1399 *
1400 * Finds the inventory item data asynchronously. Invokes callback when data has
1401 * been obtained.
1402 *
1403 * The callback must have the following signature:
1404 * @code
Anthony Wilsond5005492019-07-31 16:34:17 -05001405 * callback(void)
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001406 * @endcode
1407 *
1408 * This function is called recursively, obtaining data asynchronously from one
1409 * connection in each call. This ensures the callback is not invoked until the
1410 * last asynchronous function has completed.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001411 *
1412 * @param sensorsAsyncResp Pointer to object holding response data.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001413 * @param inventoryItems D-Bus inventory items associated with sensors.
1414 * @param invConnections Connections that provide data for the inventory items.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001415 * implements ObjectManager.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001416 * @param callback Callback to invoke when inventory data has been obtained.
1417 * @param invConnectionsIndex Current index in invConnections. Only specified
1418 * in recursive calls to this function.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001419 */
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001420template <typename Callback>
1421static void getInventoryItemsData(
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001422 std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001423 std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
Ed Tanousd0090732022-10-04 17:22:56 -07001424 std::shared_ptr<std::set<std::string>> invConnections, Callback&& callback,
1425 size_t invConnectionsIndex = 0)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001426{
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001427 BMCWEB_LOG_DEBUG << "getInventoryItemsData enter";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001428
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001429 // If no more connections left, call callback
1430 if (invConnectionsIndex >= invConnections->size())
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001431 {
Anthony Wilsond5005492019-07-31 16:34:17 -05001432 callback();
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001433 BMCWEB_LOG_DEBUG << "getInventoryItemsData exit";
1434 return;
1435 }
1436
1437 // Get inventory item data from current connection
Nan Zhoufe04d492022-06-22 17:10:41 +00001438 auto it = invConnections->begin();
1439 std::advance(it, invConnectionsIndex);
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001440 if (it != invConnections->end())
1441 {
1442 const std::string& invConnection = *it;
1443
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001444 // Response handler for GetManagedObjects
Ed Tanousd0090732022-10-04 17:22:56 -07001445 auto respHandler = [sensorsAsyncResp, inventoryItems, invConnections,
1446 callback{std::forward<Callback>(callback)},
1447 invConnectionsIndex](
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08001448 const boost::system::error_code& ec,
Ed Tanousd0090732022-10-04 17:22:56 -07001449 const dbus::utility::ManagedObjectType& resp) {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001450 BMCWEB_LOG_DEBUG << "getInventoryItemsData respHandler enter";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001451 if (ec)
1452 {
1453 BMCWEB_LOG_ERROR
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001454 << "getInventoryItemsData respHandler DBus error " << ec;
zhanghch058d1b46d2021-04-01 11:18:24 +08001455 messages::internalError(sensorsAsyncResp->asyncResp->res);
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001456 return;
1457 }
1458
1459 // Loop through returned object paths
1460 for (const auto& objDictEntry : resp)
1461 {
1462 const std::string& objPath =
1463 static_cast<const std::string&>(objDictEntry.first);
1464
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001465 // If this object path is one of the specified inventory items
1466 InventoryItem* inventoryItem =
1467 findInventoryItem(inventoryItems, objPath);
1468 if (inventoryItem != nullptr)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001469 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001470 // Store inventory data in InventoryItem
1471 storeInventoryItemData(*inventoryItem, objDictEntry.second);
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001472 }
1473 }
1474
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001475 // Recurse to get inventory item data from next connection
1476 getInventoryItemsData(sensorsAsyncResp, inventoryItems,
Ed Tanousd0090732022-10-04 17:22:56 -07001477 invConnections, std::move(callback),
1478 invConnectionsIndex + 1);
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001479
1480 BMCWEB_LOG_DEBUG << "getInventoryItemsData respHandler exit";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001481 };
1482
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001483 // Get all object paths and their interfaces for current connection
1484 crow::connections::systemBus->async_method_call(
Ed Tanousd0090732022-10-04 17:22:56 -07001485 std::move(respHandler), invConnection,
Ed Tanousf8bb0ff2022-12-09 11:08:45 -08001486 "/xyz/openbmc_project/inventory",
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001487 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
1488 }
1489
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001490 BMCWEB_LOG_DEBUG << "getInventoryItemsData exit";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001491}
1492
1493/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001494 * @brief Gets connections that provide D-Bus data for inventory items.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001495 *
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001496 * Gets the D-Bus connections (services) that provide data for the inventory
1497 * items that are associated with sensors.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001498 *
1499 * Finds the connections asynchronously. Invokes callback when information has
1500 * been obtained.
1501 *
1502 * The callback must have the following signature:
1503 * @code
Nan Zhoufe04d492022-06-22 17:10:41 +00001504 * callback(std::shared_ptr<std::set<std::string>> invConnections)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001505 * @endcode
1506 *
1507 * @param sensorsAsyncResp Pointer to object holding response data.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001508 * @param inventoryItems D-Bus inventory items associated with sensors.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001509 * @param callback Callback to invoke when connections have been obtained.
1510 */
1511template <typename Callback>
1512static void getInventoryItemsConnections(
Ed Tanousb5a76932020-09-29 16:16:58 -07001513 const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
1514 const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems,
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001515 Callback&& callback)
1516{
1517 BMCWEB_LOG_DEBUG << "getInventoryItemsConnections enter";
1518
1519 const std::string path = "/xyz/openbmc_project/inventory";
George Liue99073f2022-12-09 11:06:16 +08001520 constexpr std::array<std::string_view, 4> interfaces = {
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001521 "xyz.openbmc_project.Inventory.Item",
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001522 "xyz.openbmc_project.Inventory.Item.PowerSupply",
1523 "xyz.openbmc_project.Inventory.Decorator.Asset",
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001524 "xyz.openbmc_project.State.Decorator.OperationalStatus"};
1525
George Liue99073f2022-12-09 11:06:16 +08001526 // Make call to ObjectMapper to find all inventory items
1527 dbus::utility::getSubTree(
1528 path, 0, interfaces,
Ed Tanous002d39b2022-05-31 08:59:27 -07001529 [callback{std::forward<Callback>(callback)}, sensorsAsyncResp,
1530 inventoryItems](
George Liue99073f2022-12-09 11:06:16 +08001531 const boost::system::error_code& ec,
Ed Tanous002d39b2022-05-31 08:59:27 -07001532 const dbus::utility::MapperGetSubTreeResponse& subtree) {
George Liue99073f2022-12-09 11:06:16 +08001533 // Response handler for parsing output from GetSubTree
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001534 BMCWEB_LOG_DEBUG << "getInventoryItemsConnections respHandler enter";
1535 if (ec)
1536 {
zhanghch058d1b46d2021-04-01 11:18:24 +08001537 messages::internalError(sensorsAsyncResp->asyncResp->res);
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001538 BMCWEB_LOG_ERROR
1539 << "getInventoryItemsConnections respHandler DBus error " << ec;
1540 return;
1541 }
1542
1543 // Make unique list of connections for desired inventory items
Nan Zhoufe04d492022-06-22 17:10:41 +00001544 std::shared_ptr<std::set<std::string>> invConnections =
1545 std::make_shared<std::set<std::string>>();
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001546
1547 // Loop through objects from GetSubTree
1548 for (const std::pair<
1549 std::string,
1550 std::vector<std::pair<std::string, std::vector<std::string>>>>&
1551 object : subtree)
1552 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001553 // Check if object path is one of the specified inventory items
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001554 const std::string& objPath = object.first;
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001555 if (findInventoryItem(inventoryItems, objPath) != nullptr)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001556 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001557 // Store all connections to inventory item
1558 for (const std::pair<std::string, std::vector<std::string>>&
1559 objData : object.second)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001560 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001561 const std::string& invConnection = objData.first;
1562 invConnections->insert(invConnection);
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001563 }
1564 }
1565 }
Anthony Wilsond5005492019-07-31 16:34:17 -05001566
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001567 callback(invConnections);
1568 BMCWEB_LOG_DEBUG << "getInventoryItemsConnections respHandler exit";
George Liue99073f2022-12-09 11:06:16 +08001569 });
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001570 BMCWEB_LOG_DEBUG << "getInventoryItemsConnections exit";
1571}
1572
1573/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001574 * @brief Gets associations from sensors to inventory items.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001575 *
1576 * Looks for ObjectMapper associations from the specified sensors to related
Anthony Wilsond5005492019-07-31 16:34:17 -05001577 * inventory items. Then finds the associations from those inventory items to
1578 * their LEDs, if any.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001579 *
1580 * Finds the inventory items asynchronously. Invokes callback when information
1581 * has been obtained.
1582 *
1583 * The callback must have the following signature:
1584 * @code
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001585 * callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001586 * @endcode
1587 *
1588 * @param sensorsAsyncResp Pointer to object holding response data.
1589 * @param sensorNames All sensors within the current chassis.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001590 * implements ObjectManager.
1591 * @param callback Callback to invoke when inventory items have been obtained.
1592 */
1593template <typename Callback>
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001594static void getInventoryItemAssociations(
Ed Tanousb5a76932020-09-29 16:16:58 -07001595 const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
Nan Zhoufe04d492022-06-22 17:10:41 +00001596 const std::shared_ptr<std::set<std::string>>& sensorNames,
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001597 Callback&& callback)
1598{
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001599 BMCWEB_LOG_DEBUG << "getInventoryItemAssociations enter";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001600
1601 // Response handler for GetManagedObjects
Ed Tanous02cad962022-06-30 16:50:15 -07001602 auto respHandler =
1603 [callback{std::forward<Callback>(callback)}, sensorsAsyncResp,
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08001604 sensorNames](const boost::system::error_code& ec,
Ed Tanous02cad962022-06-30 16:50:15 -07001605 const dbus::utility::ManagedObjectType& resp) {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001606 BMCWEB_LOG_DEBUG << "getInventoryItemAssociations respHandler enter";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001607 if (ec)
1608 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001609 BMCWEB_LOG_ERROR
1610 << "getInventoryItemAssociations respHandler DBus error " << ec;
zhanghch058d1b46d2021-04-01 11:18:24 +08001611 messages::internalError(sensorsAsyncResp->asyncResp->res);
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001612 return;
1613 }
1614
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001615 // Create vector to hold list of inventory items
1616 std::shared_ptr<std::vector<InventoryItem>> inventoryItems =
1617 std::make_shared<std::vector<InventoryItem>>();
1618
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001619 // Loop through returned object paths
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001620 std::string sensorAssocPath;
1621 sensorAssocPath.reserve(128); // avoid memory allocations
1622 for (const auto& objDictEntry : resp)
1623 {
1624 const std::string& objPath =
1625 static_cast<const std::string&>(objDictEntry.first);
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001626
1627 // If path is inventory association for one of the specified sensors
1628 for (const std::string& sensorName : *sensorNames)
1629 {
1630 sensorAssocPath = sensorName;
1631 sensorAssocPath += "/inventory";
1632 if (objPath == sensorAssocPath)
1633 {
1634 // Get Association interface for object path
Ed Tanous711ac7a2021-12-20 09:34:41 -08001635 for (const auto& [interface, values] : objDictEntry.second)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001636 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001637 if (interface == "xyz.openbmc_project.Association")
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001638 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001639 for (const auto& [valueName, value] : values)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001640 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001641 if (valueName == "endpoints")
1642 {
1643 const std::vector<std::string>* endpoints =
1644 std::get_if<std::vector<std::string>>(
1645 &value);
1646 if ((endpoints != nullptr) &&
1647 !endpoints->empty())
1648 {
1649 // Add inventory item to vector
1650 const std::string& invItemPath =
1651 endpoints->front();
1652 addInventoryItem(inventoryItems,
1653 invItemPath,
1654 sensorName);
1655 }
1656 }
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001657 }
1658 }
1659 }
1660 break;
1661 }
1662 }
1663 }
1664
Anthony Wilsond5005492019-07-31 16:34:17 -05001665 // Now loop through the returned object paths again, this time to
1666 // find the leds associated with the inventory items we just found
1667 std::string inventoryAssocPath;
1668 inventoryAssocPath.reserve(128); // avoid memory allocations
1669 for (const auto& objDictEntry : resp)
1670 {
1671 const std::string& objPath =
1672 static_cast<const std::string&>(objDictEntry.first);
Anthony Wilsond5005492019-07-31 16:34:17 -05001673
1674 for (InventoryItem& inventoryItem : *inventoryItems)
1675 {
1676 inventoryAssocPath = inventoryItem.objectPath;
1677 inventoryAssocPath += "/leds";
1678 if (objPath == inventoryAssocPath)
1679 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001680 for (const auto& [interface, values] : objDictEntry.second)
Anthony Wilsond5005492019-07-31 16:34:17 -05001681 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001682 if (interface == "xyz.openbmc_project.Association")
Anthony Wilsond5005492019-07-31 16:34:17 -05001683 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001684 for (const auto& [valueName, value] : values)
Anthony Wilsond5005492019-07-31 16:34:17 -05001685 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001686 if (valueName == "endpoints")
1687 {
1688 const std::vector<std::string>* endpoints =
1689 std::get_if<std::vector<std::string>>(
1690 &value);
1691 if ((endpoints != nullptr) &&
1692 !endpoints->empty())
1693 {
1694 // Add inventory item to vector
1695 // Store LED path in inventory item
1696 const std::string& ledPath =
1697 endpoints->front();
1698 inventoryItem.ledObjectPath = ledPath;
1699 }
1700 }
Anthony Wilsond5005492019-07-31 16:34:17 -05001701 }
1702 }
1703 }
Ed Tanous711ac7a2021-12-20 09:34:41 -08001704
Anthony Wilsond5005492019-07-31 16:34:17 -05001705 break;
1706 }
1707 }
1708 }
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001709 callback(inventoryItems);
1710 BMCWEB_LOG_DEBUG << "getInventoryItemAssociations respHandler exit";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001711 };
1712
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001713 // Call GetManagedObjects on the ObjectMapper to get all associations
1714 crow::connections::systemBus->async_method_call(
Ed Tanousd0090732022-10-04 17:22:56 -07001715 std::move(respHandler), "xyz.openbmc_project.ObjectMapper", "/",
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001716 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
1717
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001718 BMCWEB_LOG_DEBUG << "getInventoryItemAssociations exit";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001719}
1720
1721/**
Anthony Wilsond5005492019-07-31 16:34:17 -05001722 * @brief Gets D-Bus data for inventory item leds associated with sensors.
1723 *
1724 * Uses the specified connections (services) to obtain D-Bus data for inventory
1725 * item leds associated with sensors. Stores the resulting data in the
1726 * inventoryItems vector.
1727 *
1728 * This data is later used to provide sensor property values in the JSON
1729 * response.
1730 *
1731 * Finds the inventory item led data asynchronously. Invokes callback when data
1732 * has been obtained.
1733 *
1734 * The callback must have the following signature:
1735 * @code
Gunnar Mills42cbe532019-08-15 15:26:54 -05001736 * callback()
Anthony Wilsond5005492019-07-31 16:34:17 -05001737 * @endcode
1738 *
1739 * This function is called recursively, obtaining data asynchronously from one
1740 * connection in each call. This ensures the callback is not invoked until the
1741 * last asynchronous function has completed.
1742 *
1743 * @param sensorsAsyncResp Pointer to object holding response data.
1744 * @param inventoryItems D-Bus inventory items associated with sensors.
1745 * @param ledConnections Connections that provide data for the inventory leds.
1746 * @param callback Callback to invoke when inventory data has been obtained.
1747 * @param ledConnectionsIndex Current index in ledConnections. Only specified
1748 * in recursive calls to this function.
1749 */
1750template <typename Callback>
1751void getInventoryLedData(
1752 std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
1753 std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
Nan Zhoufe04d492022-06-22 17:10:41 +00001754 std::shared_ptr<std::map<std::string, std::string>> ledConnections,
Anthony Wilsond5005492019-07-31 16:34:17 -05001755 Callback&& callback, size_t ledConnectionsIndex = 0)
1756{
1757 BMCWEB_LOG_DEBUG << "getInventoryLedData enter";
1758
1759 // If no more connections left, call callback
1760 if (ledConnectionsIndex >= ledConnections->size())
1761 {
Gunnar Mills42cbe532019-08-15 15:26:54 -05001762 callback();
Anthony Wilsond5005492019-07-31 16:34:17 -05001763 BMCWEB_LOG_DEBUG << "getInventoryLedData exit";
1764 return;
1765 }
1766
1767 // Get inventory item data from current connection
Nan Zhoufe04d492022-06-22 17:10:41 +00001768 auto it = ledConnections->begin();
1769 std::advance(it, ledConnectionsIndex);
Anthony Wilsond5005492019-07-31 16:34:17 -05001770 if (it != ledConnections->end())
1771 {
1772 const std::string& ledPath = (*it).first;
1773 const std::string& ledConnection = (*it).second;
1774 // Response handler for Get State property
Jonathan Doman1e1e5982021-06-11 09:36:17 -07001775 auto respHandler =
1776 [sensorsAsyncResp, inventoryItems, ledConnections, ledPath,
Ed Tanousf94c4ec2022-01-06 12:44:41 -08001777 callback{std::forward<Callback>(callback)}, ledConnectionsIndex](
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08001778 const boost::system::error_code& ec, const std::string& state) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001779 BMCWEB_LOG_DEBUG << "getInventoryLedData respHandler enter";
1780 if (ec)
1781 {
1782 BMCWEB_LOG_ERROR
1783 << "getInventoryLedData respHandler DBus error " << ec;
1784 messages::internalError(sensorsAsyncResp->asyncResp->res);
1785 return;
1786 }
1787
1788 BMCWEB_LOG_DEBUG << "Led state: " << state;
1789 // Find inventory item with this LED object path
1790 InventoryItem* inventoryItem =
1791 findInventoryItemForLed(*inventoryItems, ledPath);
1792 if (inventoryItem != nullptr)
1793 {
1794 // Store LED state in InventoryItem
Ed Tanous11ba3972022-07-11 09:50:41 -07001795 if (state.ends_with("On"))
Jonathan Doman1e1e5982021-06-11 09:36:17 -07001796 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001797 inventoryItem->ledState = LedState::ON;
Jonathan Doman1e1e5982021-06-11 09:36:17 -07001798 }
Ed Tanous11ba3972022-07-11 09:50:41 -07001799 else if (state.ends_with("Blink"))
Anthony Wilsond5005492019-07-31 16:34:17 -05001800 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001801 inventoryItem->ledState = LedState::BLINK;
Anthony Wilsond5005492019-07-31 16:34:17 -05001802 }
Ed Tanous11ba3972022-07-11 09:50:41 -07001803 else if (state.ends_with("Off"))
Ed Tanous002d39b2022-05-31 08:59:27 -07001804 {
1805 inventoryItem->ledState = LedState::OFF;
1806 }
1807 else
1808 {
1809 inventoryItem->ledState = LedState::UNKNOWN;
1810 }
1811 }
Anthony Wilsond5005492019-07-31 16:34:17 -05001812
Ed Tanous002d39b2022-05-31 08:59:27 -07001813 // Recurse to get LED data from next connection
1814 getInventoryLedData(sensorsAsyncResp, inventoryItems,
1815 ledConnections, std::move(callback),
1816 ledConnectionsIndex + 1);
Anthony Wilsond5005492019-07-31 16:34:17 -05001817
Ed Tanous002d39b2022-05-31 08:59:27 -07001818 BMCWEB_LOG_DEBUG << "getInventoryLedData respHandler exit";
1819 };
Anthony Wilsond5005492019-07-31 16:34:17 -05001820
1821 // Get the State property for the current LED
Jonathan Doman1e1e5982021-06-11 09:36:17 -07001822 sdbusplus::asio::getProperty<std::string>(
1823 *crow::connections::systemBus, ledConnection, ledPath,
1824 "xyz.openbmc_project.Led.Physical", "State",
1825 std::move(respHandler));
Anthony Wilsond5005492019-07-31 16:34:17 -05001826 }
1827
1828 BMCWEB_LOG_DEBUG << "getInventoryLedData exit";
1829}
1830
1831/**
1832 * @brief Gets LED data for LEDs associated with given inventory items.
1833 *
1834 * Gets the D-Bus connections (services) that provide LED data for the LEDs
1835 * associated with the specified inventory items. Then gets the LED data from
1836 * each connection and stores it in the inventory item.
1837 *
1838 * This data is later used to provide sensor property values in the JSON
1839 * response.
1840 *
1841 * Finds the LED data asynchronously. Invokes callback when information has
1842 * been obtained.
1843 *
1844 * The callback must have the following signature:
1845 * @code
Gunnar Mills42cbe532019-08-15 15:26:54 -05001846 * callback()
Anthony Wilsond5005492019-07-31 16:34:17 -05001847 * @endcode
1848 *
1849 * @param sensorsAsyncResp Pointer to object holding response data.
1850 * @param inventoryItems D-Bus inventory items associated with sensors.
1851 * @param callback Callback to invoke when inventory items have been obtained.
1852 */
1853template <typename Callback>
1854void getInventoryLeds(
1855 std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
1856 std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
1857 Callback&& callback)
1858{
1859 BMCWEB_LOG_DEBUG << "getInventoryLeds enter";
1860
1861 const std::string path = "/xyz/openbmc_project";
George Liue99073f2022-12-09 11:06:16 +08001862 constexpr std::array<std::string_view, 1> interfaces = {
Anthony Wilsond5005492019-07-31 16:34:17 -05001863 "xyz.openbmc_project.Led.Physical"};
1864
George Liue99073f2022-12-09 11:06:16 +08001865 // Make call to ObjectMapper to find all inventory items
1866 dbus::utility::getSubTree(
1867 path, 0, interfaces,
Ed Tanous002d39b2022-05-31 08:59:27 -07001868 [callback{std::forward<Callback>(callback)}, sensorsAsyncResp,
1869 inventoryItems](
George Liue99073f2022-12-09 11:06:16 +08001870 const boost::system::error_code& ec,
Ed Tanous002d39b2022-05-31 08:59:27 -07001871 const dbus::utility::MapperGetSubTreeResponse& subtree) {
George Liue99073f2022-12-09 11:06:16 +08001872 // Response handler for parsing output from GetSubTree
Anthony Wilsond5005492019-07-31 16:34:17 -05001873 BMCWEB_LOG_DEBUG << "getInventoryLeds respHandler enter";
1874 if (ec)
1875 {
zhanghch058d1b46d2021-04-01 11:18:24 +08001876 messages::internalError(sensorsAsyncResp->asyncResp->res);
Anthony Wilsond5005492019-07-31 16:34:17 -05001877 BMCWEB_LOG_ERROR << "getInventoryLeds respHandler DBus error "
1878 << ec;
1879 return;
1880 }
1881
1882 // Build map of LED object paths to connections
Nan Zhoufe04d492022-06-22 17:10:41 +00001883 std::shared_ptr<std::map<std::string, std::string>> ledConnections =
1884 std::make_shared<std::map<std::string, std::string>>();
Anthony Wilsond5005492019-07-31 16:34:17 -05001885
1886 // Loop through objects from GetSubTree
1887 for (const std::pair<
1888 std::string,
1889 std::vector<std::pair<std::string, std::vector<std::string>>>>&
1890 object : subtree)
1891 {
1892 // Check if object path is LED for one of the specified inventory
1893 // items
1894 const std::string& ledPath = object.first;
1895 if (findInventoryItemForLed(*inventoryItems, ledPath) != nullptr)
1896 {
1897 // Add mapping from ledPath to connection
1898 const std::string& connection = object.second.begin()->first;
1899 (*ledConnections)[ledPath] = connection;
1900 BMCWEB_LOG_DEBUG << "Added mapping " << ledPath << " -> "
1901 << connection;
1902 }
1903 }
1904
1905 getInventoryLedData(sensorsAsyncResp, inventoryItems, ledConnections,
1906 std::move(callback));
1907 BMCWEB_LOG_DEBUG << "getInventoryLeds respHandler exit";
George Liue99073f2022-12-09 11:06:16 +08001908 });
Anthony Wilsond5005492019-07-31 16:34:17 -05001909 BMCWEB_LOG_DEBUG << "getInventoryLeds exit";
1910}
1911
1912/**
Gunnar Mills42cbe532019-08-15 15:26:54 -05001913 * @brief Gets D-Bus data for Power Supply Attributes such as EfficiencyPercent
1914 *
1915 * Uses the specified connections (services) (currently assumes just one) to
1916 * obtain D-Bus data for Power Supply Attributes. Stores the resulting data in
1917 * the inventoryItems vector. Only stores data in Power Supply inventoryItems.
1918 *
1919 * This data is later used to provide sensor property values in the JSON
1920 * response.
1921 *
1922 * Finds the Power Supply Attributes data asynchronously. Invokes callback
1923 * when data has been obtained.
1924 *
1925 * The callback must have the following signature:
1926 * @code
1927 * callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems)
1928 * @endcode
1929 *
1930 * @param sensorsAsyncResp Pointer to object holding response data.
1931 * @param inventoryItems D-Bus inventory items associated with sensors.
1932 * @param psAttributesConnections Connections that provide data for the Power
1933 * Supply Attributes
1934 * @param callback Callback to invoke when data has been obtained.
1935 */
1936template <typename Callback>
1937void getPowerSupplyAttributesData(
Ed Tanousb5a76932020-09-29 16:16:58 -07001938 const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
Gunnar Mills42cbe532019-08-15 15:26:54 -05001939 std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
Nan Zhoufe04d492022-06-22 17:10:41 +00001940 const std::map<std::string, std::string>& psAttributesConnections,
Gunnar Mills42cbe532019-08-15 15:26:54 -05001941 Callback&& callback)
1942{
1943 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributesData enter";
1944
1945 if (psAttributesConnections.empty())
1946 {
1947 BMCWEB_LOG_DEBUG << "Can't find PowerSupplyAttributes, no connections!";
1948 callback(inventoryItems);
1949 return;
1950 }
1951
1952 // Assuming just one connection (service) for now
Nan Zhoufe04d492022-06-22 17:10:41 +00001953 auto it = psAttributesConnections.begin();
Gunnar Mills42cbe532019-08-15 15:26:54 -05001954
1955 const std::string& psAttributesPath = (*it).first;
1956 const std::string& psAttributesConnection = (*it).second;
1957
1958 // Response handler for Get DeratingFactor property
Ed Tanous002d39b2022-05-31 08:59:27 -07001959 auto respHandler =
1960 [sensorsAsyncResp, inventoryItems,
1961 callback{std::forward<Callback>(callback)}](
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08001962 const boost::system::error_code& ec, const uint32_t value) {
Gunnar Mills42cbe532019-08-15 15:26:54 -05001963 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributesData respHandler enter";
1964 if (ec)
1965 {
1966 BMCWEB_LOG_ERROR
1967 << "getPowerSupplyAttributesData respHandler DBus error " << ec;
zhanghch058d1b46d2021-04-01 11:18:24 +08001968 messages::internalError(sensorsAsyncResp->asyncResp->res);
Gunnar Mills42cbe532019-08-15 15:26:54 -05001969 return;
1970 }
1971
Jonathan Doman1e1e5982021-06-11 09:36:17 -07001972 BMCWEB_LOG_DEBUG << "PS EfficiencyPercent value: " << value;
1973 // Store value in Power Supply Inventory Items
1974 for (InventoryItem& inventoryItem : *inventoryItems)
Gunnar Mills42cbe532019-08-15 15:26:54 -05001975 {
Ed Tanous55f79e62022-01-25 11:26:16 -08001976 if (inventoryItem.isPowerSupply)
Gunnar Mills42cbe532019-08-15 15:26:54 -05001977 {
Jonathan Doman1e1e5982021-06-11 09:36:17 -07001978 inventoryItem.powerSupplyEfficiencyPercent =
1979 static_cast<int>(value);
Gunnar Mills42cbe532019-08-15 15:26:54 -05001980 }
1981 }
Gunnar Mills42cbe532019-08-15 15:26:54 -05001982
1983 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributesData respHandler exit";
1984 callback(inventoryItems);
1985 };
1986
1987 // Get the DeratingFactor property for the PowerSupplyAttributes
1988 // Currently only property on the interface/only one we care about
Jonathan Doman1e1e5982021-06-11 09:36:17 -07001989 sdbusplus::asio::getProperty<uint32_t>(
1990 *crow::connections::systemBus, psAttributesConnection, psAttributesPath,
1991 "xyz.openbmc_project.Control.PowerSupplyAttributes", "DeratingFactor",
1992 std::move(respHandler));
Gunnar Mills42cbe532019-08-15 15:26:54 -05001993
1994 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributesData exit";
1995}
1996
1997/**
1998 * @brief Gets the Power Supply Attributes such as EfficiencyPercent
1999 *
2000 * Gets the D-Bus connection (service) that provides Power Supply Attributes
2001 * data. Then gets the Power Supply Attributes data from the connection
2002 * (currently just assumes 1 connection) and stores the data in the inventory
2003 * item.
2004 *
2005 * This data is later used to provide sensor property values in the JSON
2006 * response. DeratingFactor on D-Bus is mapped to EfficiencyPercent on Redfish.
2007 *
2008 * Finds the Power Supply Attributes data asynchronously. Invokes callback
2009 * when information has been obtained.
2010 *
2011 * The callback must have the following signature:
2012 * @code
2013 * callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems)
2014 * @endcode
2015 *
2016 * @param sensorsAsyncResp Pointer to object holding response data.
2017 * @param inventoryItems D-Bus inventory items associated with sensors.
2018 * @param callback Callback to invoke when data has been obtained.
2019 */
2020template <typename Callback>
2021void getPowerSupplyAttributes(
2022 std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
2023 std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
2024 Callback&& callback)
2025{
2026 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes enter";
2027
2028 // Only need the power supply attributes when the Power Schema
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02002029 if (sensorsAsyncResp->chassisSubNode != sensors::node::power)
Gunnar Mills42cbe532019-08-15 15:26:54 -05002030 {
2031 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes exit since not Power";
2032 callback(inventoryItems);
2033 return;
2034 }
2035
George Liue99073f2022-12-09 11:06:16 +08002036 constexpr std::array<std::string_view, 1> interfaces = {
Gunnar Mills42cbe532019-08-15 15:26:54 -05002037 "xyz.openbmc_project.Control.PowerSupplyAttributes"};
2038
George Liue99073f2022-12-09 11:06:16 +08002039 // Make call to ObjectMapper to find the PowerSupplyAttributes service
2040 dbus::utility::getSubTree(
2041 "/xyz/openbmc_project", 0, interfaces,
Ed Tanousb9d36b42022-02-26 21:42:46 -08002042 [callback{std::forward<Callback>(callback)}, sensorsAsyncResp,
2043 inventoryItems](
George Liue99073f2022-12-09 11:06:16 +08002044 const boost::system::error_code& ec,
Ed Tanousb9d36b42022-02-26 21:42:46 -08002045 const dbus::utility::MapperGetSubTreeResponse& subtree) {
George Liue99073f2022-12-09 11:06:16 +08002046 // Response handler for parsing output from GetSubTree
Ed Tanous002d39b2022-05-31 08:59:27 -07002047 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes respHandler enter";
2048 if (ec)
2049 {
2050 messages::internalError(sensorsAsyncResp->asyncResp->res);
2051 BMCWEB_LOG_ERROR
2052 << "getPowerSupplyAttributes respHandler DBus error " << ec;
2053 return;
2054 }
2055 if (subtree.empty())
2056 {
2057 BMCWEB_LOG_DEBUG << "Can't find Power Supply Attributes!";
2058 callback(inventoryItems);
2059 return;
2060 }
Gunnar Mills42cbe532019-08-15 15:26:54 -05002061
Ed Tanous002d39b2022-05-31 08:59:27 -07002062 // Currently we only support 1 power supply attribute, use this for
2063 // all the power supplies. Build map of object path to connection.
2064 // Assume just 1 connection and 1 path for now.
Nan Zhoufe04d492022-06-22 17:10:41 +00002065 std::map<std::string, std::string> psAttributesConnections;
Gunnar Mills42cbe532019-08-15 15:26:54 -05002066
Ed Tanous002d39b2022-05-31 08:59:27 -07002067 if (subtree[0].first.empty() || subtree[0].second.empty())
2068 {
2069 BMCWEB_LOG_DEBUG << "Power Supply Attributes mapper error!";
2070 callback(inventoryItems);
2071 return;
2072 }
Gunnar Mills42cbe532019-08-15 15:26:54 -05002073
Ed Tanous002d39b2022-05-31 08:59:27 -07002074 const std::string& psAttributesPath = subtree[0].first;
2075 const std::string& connection = subtree[0].second.begin()->first;
Gunnar Mills42cbe532019-08-15 15:26:54 -05002076
Ed Tanous002d39b2022-05-31 08:59:27 -07002077 if (connection.empty())
2078 {
2079 BMCWEB_LOG_DEBUG << "Power Supply Attributes mapper error!";
2080 callback(inventoryItems);
2081 return;
2082 }
Gunnar Mills42cbe532019-08-15 15:26:54 -05002083
Ed Tanous002d39b2022-05-31 08:59:27 -07002084 psAttributesConnections[psAttributesPath] = connection;
2085 BMCWEB_LOG_DEBUG << "Added mapping " << psAttributesPath << " -> "
2086 << connection;
Gunnar Mills42cbe532019-08-15 15:26:54 -05002087
Ed Tanous002d39b2022-05-31 08:59:27 -07002088 getPowerSupplyAttributesData(sensorsAsyncResp, inventoryItems,
2089 psAttributesConnections,
2090 std::move(callback));
2091 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes respHandler exit";
George Liue99073f2022-12-09 11:06:16 +08002092 });
Gunnar Mills42cbe532019-08-15 15:26:54 -05002093 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes exit";
2094}
2095
2096/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002097 * @brief Gets inventory items associated with sensors.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002098 *
2099 * Finds the inventory items that are associated with the specified sensors.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002100 * Then gets D-Bus data for the inventory items, such as presence and VPD.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002101 *
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002102 * This data is later used to provide sensor property values in the JSON
2103 * response.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002104 *
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002105 * Finds the inventory items asynchronously. Invokes callback when the
2106 * inventory items have been obtained.
2107 *
2108 * The callback must have the following signature:
2109 * @code
2110 * callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems)
2111 * @endcode
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002112 *
2113 * @param sensorsAsyncResp Pointer to object holding response data.
2114 * @param sensorNames All sensors within the current chassis.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002115 * implements ObjectManager.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002116 * @param callback Callback to invoke when inventory items have been obtained.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002117 */
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002118template <typename Callback>
Ed Tanousd0090732022-10-04 17:22:56 -07002119static void
2120 getInventoryItems(std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
2121 const std::shared_ptr<std::set<std::string>> sensorNames,
2122 Callback&& callback)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002123{
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002124 BMCWEB_LOG_DEBUG << "getInventoryItems enter";
2125 auto getInventoryItemAssociationsCb =
Ed Tanousd0090732022-10-04 17:22:56 -07002126 [sensorsAsyncResp, callback{std::forward<Callback>(callback)}](
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002127 std::shared_ptr<std::vector<InventoryItem>> inventoryItems) {
Ed Tanous002d39b2022-05-31 08:59:27 -07002128 BMCWEB_LOG_DEBUG << "getInventoryItemAssociationsCb enter";
2129 auto getInventoryItemsConnectionsCb =
Ed Tanousd0090732022-10-04 17:22:56 -07002130 [sensorsAsyncResp, inventoryItems,
Ed Tanous002d39b2022-05-31 08:59:27 -07002131 callback{std::forward<const Callback>(callback)}](
Nan Zhoufe04d492022-06-22 17:10:41 +00002132 std::shared_ptr<std::set<std::string>> invConnections) {
Ed Tanous002d39b2022-05-31 08:59:27 -07002133 BMCWEB_LOG_DEBUG << "getInventoryItemsConnectionsCb enter";
2134 auto getInventoryItemsDataCb = [sensorsAsyncResp, inventoryItems,
2135 callback{std::move(callback)}]() {
2136 BMCWEB_LOG_DEBUG << "getInventoryItemsDataCb enter";
Gunnar Mills42cbe532019-08-15 15:26:54 -05002137
Ed Tanous002d39b2022-05-31 08:59:27 -07002138 auto getInventoryLedsCb = [sensorsAsyncResp, inventoryItems,
2139 callback{std::move(callback)}]() {
2140 BMCWEB_LOG_DEBUG << "getInventoryLedsCb enter";
2141 // Find Power Supply Attributes and get the data
2142 getPowerSupplyAttributes(sensorsAsyncResp, inventoryItems,
2143 std::move(callback));
2144 BMCWEB_LOG_DEBUG << "getInventoryLedsCb exit";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002145 };
2146
Ed Tanous002d39b2022-05-31 08:59:27 -07002147 // Find led connections and get the data
2148 getInventoryLeds(sensorsAsyncResp, inventoryItems,
2149 std::move(getInventoryLedsCb));
2150 BMCWEB_LOG_DEBUG << "getInventoryItemsDataCb exit";
2151 };
2152
2153 // Get inventory item data from connections
2154 getInventoryItemsData(sensorsAsyncResp, inventoryItems,
Ed Tanousd0090732022-10-04 17:22:56 -07002155 invConnections,
Ed Tanous002d39b2022-05-31 08:59:27 -07002156 std::move(getInventoryItemsDataCb));
2157 BMCWEB_LOG_DEBUG << "getInventoryItemsConnectionsCb exit";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002158 };
2159
Ed Tanous002d39b2022-05-31 08:59:27 -07002160 // Get connections that provide inventory item data
2161 getInventoryItemsConnections(sensorsAsyncResp, inventoryItems,
2162 std::move(getInventoryItemsConnectionsCb));
2163 BMCWEB_LOG_DEBUG << "getInventoryItemAssociationsCb exit";
2164 };
2165
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002166 // Get associations from sensors to inventory items
Ed Tanousd0090732022-10-04 17:22:56 -07002167 getInventoryItemAssociations(sensorsAsyncResp, sensorNames,
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002168 std::move(getInventoryItemAssociationsCb));
2169 BMCWEB_LOG_DEBUG << "getInventoryItems exit";
2170}
2171
2172/**
2173 * @brief Returns JSON PowerSupply object for the specified inventory item.
2174 *
2175 * Searches for a JSON PowerSupply object that matches the specified inventory
2176 * item. If one is not found, a new PowerSupply object is added to the JSON
2177 * array.
2178 *
2179 * Multiple sensors are often associated with one power supply inventory item.
2180 * As a result, multiple sensor values are stored in one JSON PowerSupply
2181 * object.
2182 *
2183 * @param powerSupplyArray JSON array containing Redfish PowerSupply objects.
2184 * @param inventoryItem Inventory item for the power supply.
2185 * @param chassisId Chassis that contains the power supply.
2186 * @return JSON PowerSupply object for the specified inventory item.
2187 */
Ed Tanous23a21a12020-07-25 04:45:05 +00002188inline nlohmann::json& getPowerSupply(nlohmann::json& powerSupplyArray,
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002189 const InventoryItem& inventoryItem,
2190 const std::string& chassisId)
2191{
2192 // Check if matching PowerSupply object already exists in JSON array
2193 for (nlohmann::json& powerSupply : powerSupplyArray)
2194 {
2195 if (powerSupply["MemberId"] == inventoryItem.name)
2196 {
2197 return powerSupply;
2198 }
2199 }
2200
2201 // Add new PowerSupply object to JSON array
2202 powerSupplyArray.push_back({});
2203 nlohmann::json& powerSupply = powerSupplyArray.back();
Willy Tueddfc432022-09-26 16:46:38 +00002204 boost::urls::url url = crow::utility::urlFromPieces(
2205 "redfish", "v1", "Chassis", chassisId, "Power");
2206 url.set_fragment(("/PowerSupplies"_json_pointer).to_string());
2207 powerSupply["@odata.id"] = std::move(url);
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002208 powerSupply["MemberId"] = inventoryItem.name;
2209 powerSupply["Name"] = boost::replace_all_copy(inventoryItem.name, "_", " ");
2210 powerSupply["Manufacturer"] = inventoryItem.manufacturer;
2211 powerSupply["Model"] = inventoryItem.model;
2212 powerSupply["PartNumber"] = inventoryItem.partNumber;
2213 powerSupply["SerialNumber"] = inventoryItem.serialNumber;
Anthony Wilsond5005492019-07-31 16:34:17 -05002214 setLedState(powerSupply, &inventoryItem);
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002215
Gunnar Mills42cbe532019-08-15 15:26:54 -05002216 if (inventoryItem.powerSupplyEfficiencyPercent >= 0)
2217 {
2218 powerSupply["EfficiencyPercent"] =
2219 inventoryItem.powerSupplyEfficiencyPercent;
2220 }
2221
2222 powerSupply["Status"]["State"] = getState(&inventoryItem);
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002223 const char* health = inventoryItem.isFunctional ? "OK" : "Critical";
2224 powerSupply["Status"]["Health"] = health;
2225
2226 return powerSupply;
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002227}
2228
2229/**
Shawn McCarneyde629b62019-03-08 10:42:51 -06002230 * @brief Gets the values of the specified sensors.
2231 *
2232 * Stores the results as JSON in the SensorsAsyncResp.
2233 *
2234 * Gets the sensor values asynchronously. Stores the results later when the
2235 * information has been obtained.
2236 *
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002237 * The sensorNames set contains all requested sensors for the current chassis.
Shawn McCarneyde629b62019-03-08 10:42:51 -06002238 *
2239 * To minimize the number of DBus calls, the DBus method
2240 * org.freedesktop.DBus.ObjectManager.GetManagedObjects() is used to get the
2241 * values of all sensors provided by a connection (service).
2242 *
2243 * The connections set contains all the connections that provide sensor values.
2244 *
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002245 * The InventoryItem vector contains D-Bus inventory items associated with the
2246 * sensors. Inventory item data is needed for some Redfish sensor properties.
2247 *
Shawn McCarneyde629b62019-03-08 10:42:51 -06002248 * @param SensorsAsyncResp Pointer to object holding response data.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002249 * @param sensorNames All requested sensors within the current chassis.
Shawn McCarneyde629b62019-03-08 10:42:51 -06002250 * @param connections Connections that provide sensor values.
Shawn McCarneyde629b62019-03-08 10:42:51 -06002251 * implements ObjectManager.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002252 * @param inventoryItems Inventory items associated with the sensors.
Shawn McCarneyde629b62019-03-08 10:42:51 -06002253 */
Ed Tanous23a21a12020-07-25 04:45:05 +00002254inline void getSensorData(
Ed Tanous81ce6092020-12-17 16:54:55 +00002255 const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
Nan Zhoufe04d492022-06-22 17:10:41 +00002256 const std::shared_ptr<std::set<std::string>>& sensorNames,
2257 const std::set<std::string>& connections,
Ed Tanousb5a76932020-09-29 16:16:58 -07002258 const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems)
Shawn McCarneyde629b62019-03-08 10:42:51 -06002259{
2260 BMCWEB_LOG_DEBUG << "getSensorData enter";
2261 // Get managed objects from all services exposing sensors
2262 for (const std::string& connection : connections)
2263 {
2264 // Response handler to process managed objects
Ed Tanous002d39b2022-05-31 08:59:27 -07002265 auto getManagedObjectsCb =
2266 [sensorsAsyncResp, sensorNames,
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08002267 inventoryItems](const boost::system::error_code& ec,
Ed Tanous02cad962022-06-30 16:50:15 -07002268 const dbus::utility::ManagedObjectType& resp) {
Shawn McCarneyde629b62019-03-08 10:42:51 -06002269 BMCWEB_LOG_DEBUG << "getManagedObjectsCb enter";
2270 if (ec)
2271 {
2272 BMCWEB_LOG_ERROR << "getManagedObjectsCb DBUS error: " << ec;
zhanghch058d1b46d2021-04-01 11:18:24 +08002273 messages::internalError(sensorsAsyncResp->asyncResp->res);
Shawn McCarneyde629b62019-03-08 10:42:51 -06002274 return;
2275 }
2276 // Go through all objects and update response with sensor data
2277 for (const auto& objDictEntry : resp)
2278 {
2279 const std::string& objPath =
2280 static_cast<const std::string&>(objDictEntry.first);
2281 BMCWEB_LOG_DEBUG << "getManagedObjectsCb parsing object "
2282 << objPath;
2283
Shawn McCarneyde629b62019-03-08 10:42:51 -06002284 std::vector<std::string> split;
2285 // Reserve space for
2286 // /xyz/openbmc_project/sensors/<name>/<subname>
2287 split.reserve(6);
Ed Tanous50ebd4a2023-01-19 19:03:17 -08002288 // NOLINTNEXTLINE
2289 bmcweb::split(split, objPath, '/');
Shawn McCarneyde629b62019-03-08 10:42:51 -06002290 if (split.size() < 6)
2291 {
2292 BMCWEB_LOG_ERROR << "Got path that isn't long enough "
2293 << objPath;
2294 continue;
2295 }
Ed Tanous50ebd4a2023-01-19 19:03:17 -08002296 // These indexes aren't intuitive, as split puts an empty
Shawn McCarneyde629b62019-03-08 10:42:51 -06002297 // string at the beginning
2298 const std::string& sensorType = split[4];
2299 const std::string& sensorName = split[5];
2300 BMCWEB_LOG_DEBUG << "sensorName " << sensorName
2301 << " sensorType " << sensorType;
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002302 if (sensorNames->find(objPath) == sensorNames->end())
Shawn McCarneyde629b62019-03-08 10:42:51 -06002303 {
Andrew Geissleraccdbb22021-11-09 15:24:45 -06002304 BMCWEB_LOG_DEBUG << sensorName << " not in sensor list ";
Shawn McCarneyde629b62019-03-08 10:42:51 -06002305 continue;
2306 }
2307
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002308 // Find inventory item (if any) associated with sensor
2309 InventoryItem* inventoryItem =
2310 findInventoryItemForSensor(inventoryItems, objPath);
2311
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002312 const std::string& sensorSchema =
Ed Tanous81ce6092020-12-17 16:54:55 +00002313 sensorsAsyncResp->chassisSubNode;
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002314
2315 nlohmann::json* sensorJson = nullptr;
2316
Nan Zhou928fefb2022-03-28 08:45:00 -07002317 if (sensorSchema == sensors::node::sensors &&
2318 !sensorsAsyncResp->efficientExpand)
Shawn McCarneyde629b62019-03-08 10:42:51 -06002319 {
Ed Tanousc1d019a2022-08-06 09:36:06 -07002320 std::string sensorTypeEscaped(sensorType);
2321 sensorTypeEscaped.erase(
2322 std::remove(sensorTypeEscaped.begin(),
2323 sensorTypeEscaped.end(), '_'),
2324 sensorTypeEscaped.end());
2325 std::string sensorId(sensorTypeEscaped);
2326 sensorId += "_";
2327 sensorId += sensorName;
2328
zhanghch058d1b46d2021-04-01 11:18:24 +08002329 sensorsAsyncResp->asyncResp->res.jsonValue["@odata.id"] =
Ed Tanousc1d019a2022-08-06 09:36:06 -07002330 crow::utility::urlFromPieces(
2331 "redfish", "v1", "Chassis",
2332 sensorsAsyncResp->chassisId,
2333 sensorsAsyncResp->chassisSubNode, sensorId);
zhanghch058d1b46d2021-04-01 11:18:24 +08002334 sensorJson = &(sensorsAsyncResp->asyncResp->res.jsonValue);
Shawn McCarneyde629b62019-03-08 10:42:51 -06002335 }
2336 else
2337 {
Ed Tanous271584a2019-07-09 16:24:22 -07002338 std::string fieldName;
Nan Zhou928fefb2022-03-28 08:45:00 -07002339 if (sensorsAsyncResp->efficientExpand)
2340 {
2341 fieldName = "Members";
2342 }
2343 else if (sensorType == "temperature")
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002344 {
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002345 fieldName = "Temperatures";
2346 }
2347 else if (sensorType == "fan" || sensorType == "fan_tach" ||
2348 sensorType == "fan_pwm")
2349 {
2350 fieldName = "Fans";
2351 }
2352 else if (sensorType == "voltage")
2353 {
2354 fieldName = "Voltages";
2355 }
2356 else if (sensorType == "power")
2357 {
Ed Tanous55f79e62022-01-25 11:26:16 -08002358 if (sensorName == "total_power")
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002359 {
2360 fieldName = "PowerControl";
2361 }
2362 else if ((inventoryItem != nullptr) &&
2363 (inventoryItem->isPowerSupply))
2364 {
2365 fieldName = "PowerSupplies";
2366 }
2367 else
2368 {
2369 // Other power sensors are in SensorCollection
2370 continue;
2371 }
2372 }
2373 else
2374 {
2375 BMCWEB_LOG_ERROR << "Unsure how to handle sensorType "
2376 << sensorType;
2377 continue;
2378 }
2379
2380 nlohmann::json& tempArray =
zhanghch058d1b46d2021-04-01 11:18:24 +08002381 sensorsAsyncResp->asyncResp->res.jsonValue[fieldName];
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002382 if (fieldName == "PowerControl")
2383 {
2384 if (tempArray.empty())
2385 {
2386 // Put multiple "sensors" into a single
2387 // PowerControl. Follows MemberId naming and
2388 // naming in power.hpp.
Ed Tanous14766872022-03-15 10:44:42 -07002389 nlohmann::json::object_t power;
Willy Tueddfc432022-09-26 16:46:38 +00002390 boost::urls::url url = crow::utility::urlFromPieces(
2391 "redfish", "v1", "Chassis",
2392 sensorsAsyncResp->chassisId,
2393 sensorsAsyncResp->chassisSubNode);
2394 url.set_fragment((""_json_pointer / fieldName / "0")
2395 .to_string());
2396 power["@odata.id"] = std::move(url);
Ed Tanous14766872022-03-15 10:44:42 -07002397 tempArray.push_back(std::move(power));
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002398 }
2399 sensorJson = &(tempArray.back());
2400 }
2401 else if (fieldName == "PowerSupplies")
2402 {
2403 if (inventoryItem != nullptr)
2404 {
2405 sensorJson =
2406 &(getPowerSupply(tempArray, *inventoryItem,
Ed Tanous81ce6092020-12-17 16:54:55 +00002407 sensorsAsyncResp->chassisId));
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002408 }
2409 }
Nan Zhou928fefb2022-03-28 08:45:00 -07002410 else if (fieldName == "Members")
2411 {
Ed Tanous677bb752022-09-15 10:52:19 -07002412 std::string sensorTypeEscaped(sensorType);
2413 sensorTypeEscaped.erase(
2414 std::remove(sensorTypeEscaped.begin(),
2415 sensorTypeEscaped.end(), '_'),
2416 sensorTypeEscaped.end());
2417 std::string sensorId(sensorTypeEscaped);
2418 sensorId += "_";
2419 sensorId += sensorName;
2420
Ed Tanous14766872022-03-15 10:44:42 -07002421 nlohmann::json::object_t member;
Ed Tanous677bb752022-09-15 10:52:19 -07002422 member["@odata.id"] = crow::utility::urlFromPieces(
2423 "redfish", "v1", "Chassis",
2424 sensorsAsyncResp->chassisId,
2425 sensorsAsyncResp->chassisSubNode, sensorId);
Ed Tanous14766872022-03-15 10:44:42 -07002426 tempArray.push_back(std::move(member));
Nan Zhou928fefb2022-03-28 08:45:00 -07002427 sensorJson = &(tempArray.back());
2428 }
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002429 else
2430 {
Ed Tanous14766872022-03-15 10:44:42 -07002431 nlohmann::json::object_t member;
Willy Tueddfc432022-09-26 16:46:38 +00002432 boost::urls::url url = crow::utility::urlFromPieces(
2433 "redfish", "v1", "Chassis",
2434 sensorsAsyncResp->chassisId,
2435 sensorsAsyncResp->chassisSubNode);
2436 url.set_fragment(
2437 (""_json_pointer / fieldName).to_string());
2438 member["@odata.id"] = std::move(url);
Ed Tanous14766872022-03-15 10:44:42 -07002439 tempArray.push_back(std::move(member));
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002440 sensorJson = &(tempArray.back());
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002441 }
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002442 }
Shawn McCarneyde629b62019-03-08 10:42:51 -06002443
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002444 if (sensorJson != nullptr)
2445 {
Ed Tanous1d7c0052022-08-09 12:32:26 -07002446 objectInterfacesToJson(sensorName, sensorType,
2447 sensorsAsyncResp->chassisSubNode,
2448 objDictEntry.second, *sensorJson,
2449 inventoryItem);
2450
2451 std::string path = "/xyz/openbmc_project/sensors/";
2452 path += sensorType;
2453 path += "/";
2454 path += sensorName;
Ed Tanousc1d019a2022-08-06 09:36:06 -07002455 sensorsAsyncResp->addMetadata(*sensorJson, path);
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002456 }
Shawn McCarneyde629b62019-03-08 10:42:51 -06002457 }
Ed Tanous81ce6092020-12-17 16:54:55 +00002458 if (sensorsAsyncResp.use_count() == 1)
James Feist8bd25cc2019-03-15 15:14:00 -07002459 {
Ed Tanous81ce6092020-12-17 16:54:55 +00002460 sortJSONResponse(sensorsAsyncResp);
Nan Zhou928fefb2022-03-28 08:45:00 -07002461 if (sensorsAsyncResp->chassisSubNode ==
2462 sensors::node::sensors &&
2463 sensorsAsyncResp->efficientExpand)
2464 {
2465 sensorsAsyncResp->asyncResp->res
2466 .jsonValue["Members@odata.count"] =
2467 sensorsAsyncResp->asyncResp->res.jsonValue["Members"]
2468 .size();
2469 }
2470 else if (sensorsAsyncResp->chassisSubNode ==
2471 sensors::node::thermal)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002472 {
Ed Tanous81ce6092020-12-17 16:54:55 +00002473 populateFanRedundancy(sensorsAsyncResp);
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002474 }
James Feist8bd25cc2019-03-15 15:14:00 -07002475 }
Shawn McCarneyde629b62019-03-08 10:42:51 -06002476 BMCWEB_LOG_DEBUG << "getManagedObjectsCb exit";
2477 };
2478
Shawn McCarneyde629b62019-03-08 10:42:51 -06002479 crow::connections::systemBus->async_method_call(
Ed Tanousd0090732022-10-04 17:22:56 -07002480 getManagedObjectsCb, connection, "/xyz/openbmc_project/sensors",
Shawn McCarneyde629b62019-03-08 10:42:51 -06002481 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
Ed Tanous23a21a12020-07-25 04:45:05 +00002482 }
Shawn McCarneyde629b62019-03-08 10:42:51 -06002483 BMCWEB_LOG_DEBUG << "getSensorData exit";
2484}
2485
Nan Zhoufe04d492022-06-22 17:10:41 +00002486inline void
2487 processSensorList(const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
2488 const std::shared_ptr<std::set<std::string>>& sensorNames)
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002489{
Nan Zhoufe04d492022-06-22 17:10:41 +00002490 auto getConnectionCb = [sensorsAsyncResp, sensorNames](
2491 const std::set<std::string>& connections) {
Ed Tanous002d39b2022-05-31 08:59:27 -07002492 BMCWEB_LOG_DEBUG << "getConnectionCb enter";
Ed Tanousd0090732022-10-04 17:22:56 -07002493 auto getInventoryItemsCb =
2494 [sensorsAsyncResp, sensorNames,
2495 connections](const std::shared_ptr<std::vector<InventoryItem>>&
2496 inventoryItems) {
2497 BMCWEB_LOG_DEBUG << "getInventoryItemsCb enter";
2498 // Get sensor data and store results in JSON
2499 getSensorData(sensorsAsyncResp, sensorNames, connections,
2500 inventoryItems);
2501 BMCWEB_LOG_DEBUG << "getInventoryItemsCb exit";
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002502 };
2503
Ed Tanousd0090732022-10-04 17:22:56 -07002504 // Get inventory items associated with sensors
2505 getInventoryItems(sensorsAsyncResp, sensorNames,
2506 std::move(getInventoryItemsCb));
2507
Ed Tanous002d39b2022-05-31 08:59:27 -07002508 BMCWEB_LOG_DEBUG << "getConnectionCb exit";
2509 };
2510
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002511 // Get set of connections that provide sensor values
Ed Tanous81ce6092020-12-17 16:54:55 +00002512 getConnections(sensorsAsyncResp, sensorNames, std::move(getConnectionCb));
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002513}
2514
Shawn McCarneyde629b62019-03-08 10:42:51 -06002515/**
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +01002516 * @brief Entry point for retrieving sensors data related to requested
2517 * chassis.
Kowalski, Kamil588c3f02018-04-03 14:55:27 +02002518 * @param SensorsAsyncResp Pointer to object holding response data
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +01002519 */
Ed Tanousb5a76932020-09-29 16:16:58 -07002520inline void
Ed Tanous81ce6092020-12-17 16:54:55 +00002521 getChassisData(const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp)
Ed Tanous1abe55e2018-09-05 08:30:59 -07002522{
2523 BMCWEB_LOG_DEBUG << "getChassisData enter";
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002524 auto getChassisCb =
Ed Tanous81ce6092020-12-17 16:54:55 +00002525 [sensorsAsyncResp](
Nan Zhoufe04d492022-06-22 17:10:41 +00002526 const std::shared_ptr<std::set<std::string>>& sensorNames) {
Ed Tanous002d39b2022-05-31 08:59:27 -07002527 BMCWEB_LOG_DEBUG << "getChassisCb enter";
2528 processSensorList(sensorsAsyncResp, sensorNames);
2529 BMCWEB_LOG_DEBUG << "getChassisCb exit";
2530 };
Nan Zhou928fefb2022-03-28 08:45:00 -07002531 // SensorCollection doesn't contain the Redundancy property
2532 if (sensorsAsyncResp->chassisSubNode != sensors::node::sensors)
2533 {
2534 sensorsAsyncResp->asyncResp->res.jsonValue["Redundancy"] =
2535 nlohmann::json::array();
2536 }
Shawn McCarney26f03892019-05-03 13:20:24 -05002537 // Get set of sensors in chassis
Ed Tanous7f1cc262022-08-09 13:33:57 -07002538 getChassis(sensorsAsyncResp->asyncResp, sensorsAsyncResp->chassisId,
2539 sensorsAsyncResp->chassisSubNode, sensorsAsyncResp->types,
2540 std::move(getChassisCb));
Ed Tanous1abe55e2018-09-05 08:30:59 -07002541 BMCWEB_LOG_DEBUG << "getChassisData exit";
Ed Tanous271584a2019-07-09 16:24:22 -07002542}
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +01002543
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302544/**
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002545 * @brief Find the requested sensorName in the list of all sensors supplied by
2546 * the chassis node
2547 *
2548 * @param sensorName The sensor name supplied in the PATCH request
2549 * @param sensorsList The list of sensors managed by the chassis node
2550 * @param sensorsModified The list of sensors that were found as a result of
2551 * repeated calls to this function
2552 */
Nan Zhoufe04d492022-06-22 17:10:41 +00002553inline bool
2554 findSensorNameUsingSensorPath(std::string_view sensorName,
Ed Tanous02cad962022-06-30 16:50:15 -07002555 const std::set<std::string>& sensorsList,
Nan Zhoufe04d492022-06-22 17:10:41 +00002556 std::set<std::string>& sensorsModified)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002557{
Nan Zhoufe04d492022-06-22 17:10:41 +00002558 for (const auto& chassisSensor : sensorsList)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002559 {
George Liu28aa8de2021-02-01 15:13:30 +08002560 sdbusplus::message::object_path path(chassisSensor);
Ed Tanousb00dcc22021-02-23 12:52:50 -08002561 std::string thisSensorName = path.filename();
George Liu28aa8de2021-02-01 15:13:30 +08002562 if (thisSensorName.empty())
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002563 {
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002564 continue;
2565 }
2566 if (thisSensorName == sensorName)
2567 {
2568 sensorsModified.emplace(chassisSensor);
2569 return true;
2570 }
2571 }
2572 return false;
2573}
2574
Ed Tanousc71d6122022-11-29 14:10:32 -08002575inline std::pair<std::string, std::string>
2576 splitSensorNameAndType(std::string_view sensorId)
2577{
2578 size_t index = sensorId.find('_');
2579 if (index == std::string::npos)
2580 {
2581 return std::make_pair<std::string, std::string>("", "");
2582 }
2583 std::string sensorType{sensorId.substr(0, index)};
2584 std::string sensorName{sensorId.substr(index + 1)};
2585 // fan_pwm and fan_tach need special handling
2586 if (sensorType == "fantach" || sensorType == "fanpwm")
2587 {
2588 sensorType.insert(3, 1, '_');
2589 }
2590 return std::make_pair(sensorType, sensorName);
2591}
2592
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002593/**
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302594 * @brief Entry point for overriding sensor values of given sensor
2595 *
zhanghch058d1b46d2021-04-01 11:18:24 +08002596 * @param sensorAsyncResp response object
Carol Wang4bb3dc32019-10-17 18:15:02 +08002597 * @param allCollections Collections extract from sensors' request patch info
jayaprakash Mutyala91e130a2020-03-04 22:26:38 +00002598 * @param chassisSubNode Chassis Node for which the query has to happen
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302599 */
Ed Tanous23a21a12020-07-25 04:45:05 +00002600inline void setSensorsOverride(
Ed Tanousb5a76932020-09-29 16:16:58 -07002601 const std::shared_ptr<SensorsAsyncResp>& sensorAsyncResp,
Carol Wang4bb3dc32019-10-17 18:15:02 +08002602 std::unordered_map<std::string, std::vector<nlohmann::json>>&
jayaprakash Mutyala397fd612020-02-06 23:33:34 +00002603 allCollections)
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302604{
jayaprakash Mutyala70d1d0a2020-01-21 23:41:36 +00002605 BMCWEB_LOG_INFO << "setSensorsOverride for subNode"
Carol Wang4bb3dc32019-10-17 18:15:02 +08002606 << sensorAsyncResp->chassisSubNode << "\n";
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302607
Ed Tanous543f4402022-01-06 13:12:53 -08002608 const char* propertyValueName = nullptr;
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302609 std::unordered_map<std::string, std::pair<double, std::string>> overrideMap;
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302610 std::string memberId;
Ed Tanous543f4402022-01-06 13:12:53 -08002611 double value = 0.0;
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302612 for (auto& collectionItems : allCollections)
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302613 {
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302614 if (collectionItems.first == "Temperatures")
2615 {
2616 propertyValueName = "ReadingCelsius";
2617 }
2618 else if (collectionItems.first == "Fans")
2619 {
2620 propertyValueName = "Reading";
2621 }
2622 else
2623 {
2624 propertyValueName = "ReadingVolts";
2625 }
2626 for (auto& item : collectionItems.second)
2627 {
zhanghch058d1b46d2021-04-01 11:18:24 +08002628 if (!json_util::readJson(item, sensorAsyncResp->asyncResp->res,
2629 "MemberId", memberId, propertyValueName,
2630 value))
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302631 {
2632 return;
2633 }
2634 overrideMap.emplace(memberId,
2635 std::make_pair(value, collectionItems.first));
2636 }
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302637 }
Carol Wang4bb3dc32019-10-17 18:15:02 +08002638
Ed Tanous002d39b2022-05-31 08:59:27 -07002639 auto getChassisSensorListCb =
2640 [sensorAsyncResp, overrideMap](
Nan Zhoufe04d492022-06-22 17:10:41 +00002641 const std::shared_ptr<std::set<std::string>>& sensorsList) {
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002642 // Match sensor names in the PATCH request to those managed by the
2643 // chassis node
Nan Zhoufe04d492022-06-22 17:10:41 +00002644 const std::shared_ptr<std::set<std::string>> sensorNames =
2645 std::make_shared<std::set<std::string>>();
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302646 for (const auto& item : overrideMap)
2647 {
2648 const auto& sensor = item.first;
Ed Tanousc71d6122022-11-29 14:10:32 -08002649 std::pair<std::string, std::string> sensorNameType =
2650 splitSensorNameAndType(sensor);
2651 if (!findSensorNameUsingSensorPath(sensorNameType.second,
2652 *sensorsList, *sensorNames))
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302653 {
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302654 BMCWEB_LOG_INFO << "Unable to find memberId " << item.first;
zhanghch058d1b46d2021-04-01 11:18:24 +08002655 messages::resourceNotFound(sensorAsyncResp->asyncResp->res,
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302656 item.second.second, item.first);
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302657 return;
2658 }
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302659 }
2660 // Get the connection to which the memberId belongs
Ed Tanous002d39b2022-05-31 08:59:27 -07002661 auto getObjectsWithConnectionCb =
Nan Zhoufe04d492022-06-22 17:10:41 +00002662 [sensorAsyncResp,
2663 overrideMap](const std::set<std::string>& /*connections*/,
2664 const std::set<std::pair<std::string, std::string>>&
2665 objectsWithConnection) {
Jayaprakash Mutyala4f277b52021-12-08 22:46:49 +00002666 if (objectsWithConnection.size() != overrideMap.size())
2667 {
2668 BMCWEB_LOG_INFO
2669 << "Unable to find all objects with proper connection "
2670 << objectsWithConnection.size() << " requested "
2671 << overrideMap.size() << "\n";
2672 messages::resourceNotFound(sensorAsyncResp->asyncResp->res,
2673 sensorAsyncResp->chassisSubNode ==
2674 sensors::node::thermal
2675 ? "Temperatures"
2676 : "Voltages",
2677 "Count");
2678 return;
2679 }
2680 for (const auto& item : objectsWithConnection)
2681 {
2682 sdbusplus::message::object_path path(item.first);
2683 std::string sensorName = path.filename();
2684 if (sensorName.empty())
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302685 {
Jayaprakash Mutyala4f277b52021-12-08 22:46:49 +00002686 messages::internalError(sensorAsyncResp->asyncResp->res);
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302687 return;
2688 }
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302689
Jayaprakash Mutyala4f277b52021-12-08 22:46:49 +00002690 const auto& iterator = overrideMap.find(sensorName);
2691 if (iterator == overrideMap.end())
2692 {
2693 BMCWEB_LOG_INFO << "Unable to find sensor object"
2694 << item.first << "\n";
2695 messages::internalError(sensorAsyncResp->asyncResp->res);
2696 return;
2697 }
2698 crow::connections::systemBus->async_method_call(
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08002699 [sensorAsyncResp](const boost::system::error_code& ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -07002700 if (ec)
2701 {
2702 if (ec.value() ==
2703 boost::system::errc::permission_denied)
Jayaprakash Mutyala4f277b52021-12-08 22:46:49 +00002704 {
Ed Tanous002d39b2022-05-31 08:59:27 -07002705 BMCWEB_LOG_WARNING
2706 << "Manufacturing mode is not Enabled...can't "
2707 "Override the sensor value. ";
Jayaprakash Mutyala4f277b52021-12-08 22:46:49 +00002708
Ed Tanous002d39b2022-05-31 08:59:27 -07002709 messages::insufficientPrivilege(
Jayaprakash Mutyala4f277b52021-12-08 22:46:49 +00002710 sensorAsyncResp->asyncResp->res);
Ed Tanous002d39b2022-05-31 08:59:27 -07002711 return;
Jayaprakash Mutyala4f277b52021-12-08 22:46:49 +00002712 }
Ed Tanous002d39b2022-05-31 08:59:27 -07002713 BMCWEB_LOG_DEBUG
2714 << "setOverrideValueStatus DBUS error: " << ec;
2715 messages::internalError(
2716 sensorAsyncResp->asyncResp->res);
2717 }
Jayaprakash Mutyala4f277b52021-12-08 22:46:49 +00002718 },
2719 item.second, item.first, "org.freedesktop.DBus.Properties",
2720 "Set", "xyz.openbmc_project.Sensor.Value", "Value",
Ed Tanous168e20c2021-12-13 14:39:53 -08002721 dbus::utility::DbusVariantType(iterator->second.first));
Jayaprakash Mutyala4f277b52021-12-08 22:46:49 +00002722 }
2723 };
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302724 // Get object with connection for the given sensor name
2725 getObjectsWithConnection(sensorAsyncResp, sensorNames,
2726 std::move(getObjectsWithConnectionCb));
2727 };
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302728 // get full sensor list for the given chassisId and cross verify the sensor.
Ed Tanous7f1cc262022-08-09 13:33:57 -07002729 getChassis(sensorAsyncResp->asyncResp, sensorAsyncResp->chassisId,
2730 sensorAsyncResp->chassisSubNode, sensorAsyncResp->types,
2731 std::move(getChassisSensorListCb));
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302732}
2733
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02002734/**
2735 * @brief Retrieves mapping of Redfish URIs to sensor value property to D-Bus
2736 * path of the sensor.
2737 *
2738 * Function builds valid Redfish response for sensor query of given chassis and
2739 * node. It then builds metadata about Redfish<->D-Bus correlations and provides
2740 * it to caller in a callback.
2741 *
2742 * @param chassis Chassis for which retrieval should be performed
2743 * @param node Node (group) of sensors. See sensors::node for supported values
2744 * @param mapComplete Callback to be called with retrieval result
2745 */
Krzysztof Grobelny021d32c2021-10-29 16:00:07 +02002746inline void retrieveUriToDbusMap(const std::string& chassis,
2747 const std::string& node,
2748 SensorsAsyncResp::DataCompleteCb&& mapComplete)
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02002749{
Ed Tanous02da7c52022-02-27 00:09:02 -08002750 decltype(sensors::paths)::const_iterator pathIt =
2751 std::find_if(sensors::paths.cbegin(), sensors::paths.cend(),
2752 [&node](auto&& val) { return val.first == node; });
2753 if (pathIt == sensors::paths.cend())
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02002754 {
2755 BMCWEB_LOG_ERROR << "Wrong node provided : " << node;
2756 mapComplete(boost::beast::http::status::bad_request, {});
2757 return;
2758 }
Krzysztof Grobelnyd51e0722021-04-16 13:15:21 +00002759
Nan Zhou72374eb2022-01-27 17:06:51 -08002760 auto asyncResp = std::make_shared<bmcweb::AsyncResp>();
Nan Zhoufe04d492022-06-22 17:10:41 +00002761 auto callback = [asyncResp, mapCompleteCb{std::move(mapComplete)}](
2762 const boost::beast::http::status status,
2763 const std::map<std::string, std::string>& uriToDbus) {
2764 mapCompleteCb(status, uriToDbus);
2765 };
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02002766
2767 auto resp = std::make_shared<SensorsAsyncResp>(
Krzysztof Grobelnyd51e0722021-04-16 13:15:21 +00002768 asyncResp, chassis, pathIt->second, node, std::move(callback));
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02002769 getChassisData(resp);
2770}
2771
Nan Zhoubacb2162022-04-06 11:28:32 -07002772namespace sensors
2773{
Nan Zhou928fefb2022-03-28 08:45:00 -07002774
Nan Zhoubacb2162022-04-06 11:28:32 -07002775inline void getChassisCallback(
Ed Tanousc1d019a2022-08-06 09:36:06 -07002776 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2777 std::string_view chassisId, std::string_view chassisSubNode,
Nan Zhoufe04d492022-06-22 17:10:41 +00002778 const std::shared_ptr<std::set<std::string>>& sensorNames)
Nan Zhoubacb2162022-04-06 11:28:32 -07002779{
Ed Tanousc1d019a2022-08-06 09:36:06 -07002780 BMCWEB_LOG_DEBUG << "getChassisCallback enter ";
Nan Zhoubacb2162022-04-06 11:28:32 -07002781
Ed Tanousc1d019a2022-08-06 09:36:06 -07002782 nlohmann::json& entriesArray = asyncResp->res.jsonValue["Members"];
2783 for (const std::string& sensor : *sensorNames)
Nan Zhoubacb2162022-04-06 11:28:32 -07002784 {
2785 BMCWEB_LOG_DEBUG << "Adding sensor: " << sensor;
2786
2787 sdbusplus::message::object_path path(sensor);
2788 std::string sensorName = path.filename();
2789 if (sensorName.empty())
2790 {
2791 BMCWEB_LOG_ERROR << "Invalid sensor path: " << sensor;
Ed Tanousc1d019a2022-08-06 09:36:06 -07002792 messages::internalError(asyncResp->res);
Nan Zhoubacb2162022-04-06 11:28:32 -07002793 return;
2794 }
Ed Tanousc1d019a2022-08-06 09:36:06 -07002795 std::string type = path.parent_path().filename();
2796 // fan_tach has an underscore in it, so remove it to "normalize" the
2797 // type in the URI
2798 type.erase(std::remove(type.begin(), type.end(), '_'), type.end());
2799
Ed Tanous14766872022-03-15 10:44:42 -07002800 nlohmann::json::object_t member;
Ed Tanousc1d019a2022-08-06 09:36:06 -07002801 std::string id = type;
2802 id += "_";
2803 id += sensorName;
2804 member["@odata.id"] = crow::utility::urlFromPieces(
2805 "redfish", "v1", "Chassis", chassisId, chassisSubNode, id);
2806
Ed Tanous14766872022-03-15 10:44:42 -07002807 entriesArray.push_back(std::move(member));
Nan Zhoubacb2162022-04-06 11:28:32 -07002808 }
2809
Ed Tanousc1d019a2022-08-06 09:36:06 -07002810 asyncResp->res.jsonValue["Members@odata.count"] = entriesArray.size();
Nan Zhoubacb2162022-04-06 11:28:32 -07002811 BMCWEB_LOG_DEBUG << "getChassisCallback exit";
2812}
Nan Zhoue6bd8462022-06-01 04:35:35 +00002813
Nan Zhoude167a62022-06-01 04:47:45 +00002814inline void
2815 handleSensorCollectionGet(App& app, const crow::Request& req,
2816 const std::shared_ptr<bmcweb::AsyncResp>& aResp,
2817 const std::string& chassisId)
2818{
2819 query_param::QueryCapabilities capabilities = {
2820 .canDelegateExpandLevel = 1,
2821 };
2822 query_param::Query delegatedQuery;
Carson Labrado3ba00072022-06-06 19:40:56 +00002823 if (!redfish::setUpRedfishRouteWithDelegation(app, req, aResp,
Nan Zhoude167a62022-06-01 04:47:45 +00002824 delegatedQuery, capabilities))
2825 {
2826 return;
2827 }
2828
2829 if (delegatedQuery.expandType != query_param::ExpandType::None)
2830 {
2831 // we perform efficient expand.
2832 auto asyncResp = std::make_shared<SensorsAsyncResp>(
2833 aResp, chassisId, sensors::dbus::sensorPaths,
2834 sensors::node::sensors,
2835 /*efficientExpand=*/true);
2836 getChassisData(asyncResp);
2837
2838 BMCWEB_LOG_DEBUG
2839 << "SensorCollection doGet exit via efficient expand handler";
2840 return;
Ed Tanous0bad3202022-06-02 13:53:59 -07002841 }
Nan Zhoude167a62022-06-01 04:47:45 +00002842
Nan Zhoude167a62022-06-01 04:47:45 +00002843 // We get all sensors as hyperlinkes in the chassis (this
2844 // implies we reply on the default query parameters handler)
Ed Tanousc1d019a2022-08-06 09:36:06 -07002845 getChassis(aResp, chassisId, sensors::node::sensors, dbus::sensorPaths,
2846 std::bind_front(sensors::getChassisCallback, aResp, chassisId,
2847 sensors::node::sensors));
2848}
Ed Tanous7f1cc262022-08-09 13:33:57 -07002849
Ed Tanousc1d019a2022-08-06 09:36:06 -07002850inline void
2851 getSensorFromDbus(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2852 const std::string& sensorPath,
2853 const ::dbus::utility::MapperGetObject& mapperResponse)
2854{
2855 if (mapperResponse.size() != 1)
2856 {
2857 messages::internalError(asyncResp->res);
2858 return;
2859 }
2860 const auto& valueIface = *mapperResponse.begin();
2861 const std::string& connectionName = valueIface.first;
2862 BMCWEB_LOG_DEBUG << "Looking up " << connectionName;
2863 BMCWEB_LOG_DEBUG << "Path " << sensorPath;
Krzysztof Grobelnyc1343bf2022-08-31 13:15:26 +02002864
2865 sdbusplus::asio::getAllProperties(
2866 *crow::connections::systemBus, connectionName, sensorPath, "",
Ed Tanousc1d019a2022-08-06 09:36:06 -07002867 [asyncResp,
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08002868 sensorPath](const boost::system::error_code& ec,
Ed Tanousc1d019a2022-08-06 09:36:06 -07002869 const ::dbus::utility::DBusPropertiesMap& valuesDict) {
2870 if (ec)
2871 {
2872 messages::internalError(asyncResp->res);
2873 return;
2874 }
2875 sdbusplus::message::object_path path(sensorPath);
2876 std::string name = path.filename();
2877 path = path.parent_path();
2878 std::string type = path.filename();
2879 objectPropertiesToJson(name, type, sensors::node::sensors, valuesDict,
2880 asyncResp->res.jsonValue, nullptr);
Krzysztof Grobelnyc1343bf2022-08-31 13:15:26 +02002881 });
Nan Zhoude167a62022-06-01 04:47:45 +00002882}
2883
Nan Zhoue6bd8462022-06-01 04:35:35 +00002884inline void handleSensorGet(App& app, const crow::Request& req,
Ed Tanousc1d019a2022-08-06 09:36:06 -07002885 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous677bb752022-09-15 10:52:19 -07002886 const std::string& chassisId,
Ed Tanousc1d019a2022-08-06 09:36:06 -07002887 const std::string& sensorId)
Nan Zhoue6bd8462022-06-01 04:35:35 +00002888{
Ed Tanousc1d019a2022-08-06 09:36:06 -07002889 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Nan Zhoue6bd8462022-06-01 04:35:35 +00002890 {
2891 return;
2892 }
Ed Tanousc71d6122022-11-29 14:10:32 -08002893 std::pair<std::string, std::string> nameType =
2894 splitSensorNameAndType(sensorId);
2895 if (nameType.first.empty() || nameType.second.empty())
Ed Tanousc1d019a2022-08-06 09:36:06 -07002896 {
2897 messages::resourceNotFound(asyncResp->res, sensorId, "Sensor");
2898 return;
2899 }
Ed Tanousc71d6122022-11-29 14:10:32 -08002900
Ed Tanous677bb752022-09-15 10:52:19 -07002901 asyncResp->res.jsonValue["@odata.id"] = crow::utility::urlFromPieces(
2902 "redfish", "v1", "Chassis", chassisId, "Sensors", sensorId);
Ed Tanousc1d019a2022-08-06 09:36:06 -07002903
Nan Zhoue6bd8462022-06-01 04:35:35 +00002904 BMCWEB_LOG_DEBUG << "Sensor doGet enter";
Nan Zhoue6bd8462022-06-01 04:35:35 +00002905
George Liu2b731192023-01-11 16:27:13 +08002906 constexpr std::array<std::string_view, 1> interfaces = {
Nan Zhoue6bd8462022-06-01 04:35:35 +00002907 "xyz.openbmc_project.Sensor.Value"};
Ed Tanousc71d6122022-11-29 14:10:32 -08002908 std::string sensorPath = "/xyz/openbmc_project/sensors/" + nameType.first +
2909 '/' + nameType.second;
Nan Zhoue6bd8462022-06-01 04:35:35 +00002910 // Get a list of all of the sensors that implement Sensor.Value
2911 // and get the path and service name associated with the sensor
George Liu2b731192023-01-11 16:27:13 +08002912 ::dbus::utility::getDbusObject(
2913 sensorPath, interfaces,
Ed Tanousc71d6122022-11-29 14:10:32 -08002914 [asyncResp,
George Liu2b731192023-01-11 16:27:13 +08002915 sensorPath](const boost::system::error_code& ec,
Ed Tanousc1d019a2022-08-06 09:36:06 -07002916 const ::dbus::utility::MapperGetObject& subtree) {
Nan Zhoue6bd8462022-06-01 04:35:35 +00002917 BMCWEB_LOG_DEBUG << "respHandler1 enter";
2918 if (ec)
2919 {
Ed Tanousc1d019a2022-08-06 09:36:06 -07002920 messages::internalError(asyncResp->res);
Nan Zhoue6bd8462022-06-01 04:35:35 +00002921 BMCWEB_LOG_ERROR << "Sensor getSensorPaths resp_handler: "
2922 << "Dbus error " << ec;
2923 return;
2924 }
Ed Tanousc1d019a2022-08-06 09:36:06 -07002925 getSensorFromDbus(asyncResp, sensorPath, subtree);
Nan Zhoue6bd8462022-06-01 04:35:35 +00002926 BMCWEB_LOG_DEBUG << "respHandler1 exit";
George Liu2b731192023-01-11 16:27:13 +08002927 });
Nan Zhoue6bd8462022-06-01 04:35:35 +00002928}
2929
Nan Zhoubacb2162022-04-06 11:28:32 -07002930} // namespace sensors
2931
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002932inline void requestRoutesSensorCollection(App& app)
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002933{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002934 BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/Sensors/")
Ed Tanoused398212021-06-09 17:05:54 -07002935 .privileges(redfish::privileges::getSensorCollection)
Ed Tanous002d39b2022-05-31 08:59:27 -07002936 .methods(boost::beast::http::verb::get)(
Nan Zhoude167a62022-06-01 04:47:45 +00002937 std::bind_front(sensors::handleSensorCollectionGet, std::ref(app)));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002938}
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002939
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002940inline void requestRoutesSensor(App& app)
2941{
2942 BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/Sensors/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07002943 .privileges(redfish::privileges::getSensor)
Ed Tanous002d39b2022-05-31 08:59:27 -07002944 .methods(boost::beast::http::verb::get)(
Nan Zhoue6bd8462022-06-01 04:35:35 +00002945 std::bind_front(sensors::handleSensorGet, std::ref(app)));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002946}
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002947
Ed Tanous1abe55e2018-09-05 08:30:59 -07002948} // namespace redfish