blob: 8189bdb505c17a8a1723baf1d79ba9d4beb57283 [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 Tanous1d7c0052022-08-09 12:32:26 -0700754 std::string sensorNameEs(sensorName);
755 std::replace(sensorNameEs.begin(), sensorNameEs.end(), '_', ' ');
756 sensorJson["Name"] = std::move(sensorNameEs);
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500757 }
Ed Tanouse742b6c2019-05-03 15:06:53 -0700758
Ed Tanous81ce6092020-12-17 16:54:55 +0000759 sensorJson["Status"]["State"] = getState(inventoryItem);
760 sensorJson["Status"]["Health"] =
Ed Tanous1d7c0052022-08-09 12:32:26 -0700761 getHealth(sensorJson, propertiesDict, inventoryItem);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700762
763 // Parameter to set to override the type we get from dbus, and force it to
764 // int, regardless of what is available. This is used for schemas like fan,
765 // that require integers, not floats.
766 bool forceToInt = false;
767
Anthony Wilson3929aca2019-07-19 15:42:33 -0500768 nlohmann::json::json_pointer unit("/Reading");
Ed Tanous1d7c0052022-08-09 12:32:26 -0700769 if (chassisSubNode == sensors::node::sensors)
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500770 {
Shounak Mitra2a4ba192022-06-01 23:34:12 +0000771 sensorJson["@odata.type"] = "#Sensor.v1_2_0.Sensor";
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000772
Ed Tanous0ec8b832022-03-14 14:56:47 -0700773 sensor::ReadingType readingType = sensors::toReadingType(sensorType);
774 if (readingType == sensor::ReadingType::Invalid)
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500775 {
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000776 BMCWEB_LOG_ERROR << "Redfish cannot map reading type for "
777 << sensorType;
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500778 }
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000779 else
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500780 {
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000781 sensorJson["ReadingType"] = readingType;
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500782 }
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000783
Ed Tanous1d7c0052022-08-09 12:32:26 -0700784 std::string_view readingUnits = sensors::toReadingUnits(sensorType);
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000785 if (readingUnits.empty())
Adrian Ambrożewiczf8ede152020-06-02 13:26:33 +0200786 {
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000787 BMCWEB_LOG_ERROR << "Redfish cannot map reading unit for "
788 << sensorType;
789 }
790 else
791 {
792 sensorJson["ReadingUnits"] = readingUnits;
Adrian Ambrożewiczf8ede152020-06-02 13:26:33 +0200793 }
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500794 }
795 else if (sensorType == "temperature")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700796 {
Anthony Wilson3929aca2019-07-19 15:42:33 -0500797 unit = "/ReadingCelsius"_json_pointer;
Ed Tanous81ce6092020-12-17 16:54:55 +0000798 sensorJson["@odata.type"] = "#Thermal.v1_3_0.Temperature";
Ed Tanous1abe55e2018-09-05 08:30:59 -0700799 // TODO(ed) Documentation says that path should be type fan_tach,
800 // implementation seems to implement fan
801 }
802 else if (sensorType == "fan" || sensorType == "fan_tach")
803 {
Anthony Wilson3929aca2019-07-19 15:42:33 -0500804 unit = "/Reading"_json_pointer;
Ed Tanous81ce6092020-12-17 16:54:55 +0000805 sensorJson["ReadingUnits"] = "RPM";
806 sensorJson["@odata.type"] = "#Thermal.v1_3_0.Fan";
807 setLedState(sensorJson, inventoryItem);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700808 forceToInt = true;
809 }
Ed Tanous6f6d0d32018-10-12 11:16:43 -0700810 else if (sensorType == "fan_pwm")
811 {
Anthony Wilson3929aca2019-07-19 15:42:33 -0500812 unit = "/Reading"_json_pointer;
Ed Tanous81ce6092020-12-17 16:54:55 +0000813 sensorJson["ReadingUnits"] = "Percent";
814 sensorJson["@odata.type"] = "#Thermal.v1_3_0.Fan";
815 setLedState(sensorJson, inventoryItem);
Ed Tanous6f6d0d32018-10-12 11:16:43 -0700816 forceToInt = true;
817 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700818 else if (sensorType == "voltage")
819 {
Anthony Wilson3929aca2019-07-19 15:42:33 -0500820 unit = "/ReadingVolts"_json_pointer;
Ed Tanous81ce6092020-12-17 16:54:55 +0000821 sensorJson["@odata.type"] = "#Power.v1_0_0.Voltage";
Ed Tanous1abe55e2018-09-05 08:30:59 -0700822 }
Ed Tanous2474adf2018-09-05 16:31:16 -0700823 else if (sensorType == "power")
824 {
Ed Tanous1d7c0052022-08-09 12:32:26 -0700825 if (boost::iequals(sensorName, "total_power"))
Eddie James028f7eb2019-05-17 21:24:36 +0000826 {
Ed Tanous81ce6092020-12-17 16:54:55 +0000827 sensorJson["@odata.type"] = "#Power.v1_0_0.PowerControl";
Gunnar Mills7ab06f42019-07-02 13:07:16 -0500828 // Put multiple "sensors" into a single PowerControl, so have
829 // generic names for MemberId and Name. Follows Redfish mockup.
Ed Tanous81ce6092020-12-17 16:54:55 +0000830 sensorJson["MemberId"] = "0";
831 sensorJson["Name"] = "Chassis Power Control";
Anthony Wilson3929aca2019-07-19 15:42:33 -0500832 unit = "/PowerConsumedWatts"_json_pointer;
Eddie James028f7eb2019-05-17 21:24:36 +0000833 }
Ed Tanous1d7c0052022-08-09 12:32:26 -0700834 else if (boost::ifind_first(sensorName, "input").empty())
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700835 {
Anthony Wilson3929aca2019-07-19 15:42:33 -0500836 unit = "/PowerInputWatts"_json_pointer;
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700837 }
838 else
839 {
Anthony Wilson3929aca2019-07-19 15:42:33 -0500840 unit = "/PowerOutputWatts"_json_pointer;
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700841 }
Ed Tanous2474adf2018-09-05 16:31:16 -0700842 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700843 else
844 {
845 BMCWEB_LOG_ERROR << "Redfish cannot map object type for " << sensorName;
846 return;
847 }
848 // Map of dbus interface name, dbus property name and redfish property_name
Anthony Wilson3929aca2019-07-19 15:42:33 -0500849 std::vector<
850 std::tuple<const char*, const char*, nlohmann::json::json_pointer>>
851 properties;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700852 properties.reserve(7);
853
854 properties.emplace_back("xyz.openbmc_project.Sensor.Value", "Value", unit);
Shawn McCarneyde629b62019-03-08 10:42:51 -0600855
Ed Tanous1d7c0052022-08-09 12:32:26 -0700856 if (chassisSubNode == sensors::node::sensors)
Anthony Wilson3929aca2019-07-19 15:42:33 -0500857 {
858 properties.emplace_back(
859 "xyz.openbmc_project.Sensor.Threshold.Warning", "WarningHigh",
860 "/Thresholds/UpperCaution/Reading"_json_pointer);
861 properties.emplace_back(
862 "xyz.openbmc_project.Sensor.Threshold.Warning", "WarningLow",
863 "/Thresholds/LowerCaution/Reading"_json_pointer);
864 properties.emplace_back(
865 "xyz.openbmc_project.Sensor.Threshold.Critical", "CriticalHigh",
866 "/Thresholds/UpperCritical/Reading"_json_pointer);
867 properties.emplace_back(
868 "xyz.openbmc_project.Sensor.Threshold.Critical", "CriticalLow",
869 "/Thresholds/LowerCritical/Reading"_json_pointer);
870 }
871 else if (sensorType != "power")
Shawn McCarneyde629b62019-03-08 10:42:51 -0600872 {
873 properties.emplace_back("xyz.openbmc_project.Sensor.Threshold.Warning",
Anthony Wilson3929aca2019-07-19 15:42:33 -0500874 "WarningHigh",
875 "/UpperThresholdNonCritical"_json_pointer);
Shawn McCarneyde629b62019-03-08 10:42:51 -0600876 properties.emplace_back("xyz.openbmc_project.Sensor.Threshold.Warning",
Anthony Wilson3929aca2019-07-19 15:42:33 -0500877 "WarningLow",
878 "/LowerThresholdNonCritical"_json_pointer);
Shawn McCarneyde629b62019-03-08 10:42:51 -0600879 properties.emplace_back("xyz.openbmc_project.Sensor.Threshold.Critical",
Anthony Wilson3929aca2019-07-19 15:42:33 -0500880 "CriticalHigh",
881 "/UpperThresholdCritical"_json_pointer);
Shawn McCarneyde629b62019-03-08 10:42:51 -0600882 properties.emplace_back("xyz.openbmc_project.Sensor.Threshold.Critical",
Anthony Wilson3929aca2019-07-19 15:42:33 -0500883 "CriticalLow",
884 "/LowerThresholdCritical"_json_pointer);
Shawn McCarneyde629b62019-03-08 10:42:51 -0600885 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700886
Ed Tanous2474adf2018-09-05 16:31:16 -0700887 // TODO Need to get UpperThresholdFatal and LowerThresholdFatal
888
Ed Tanous1d7c0052022-08-09 12:32:26 -0700889 if (chassisSubNode == sensors::node::sensors)
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500890 {
891 properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MinValue",
Anthony Wilson3929aca2019-07-19 15:42:33 -0500892 "/ReadingRangeMin"_json_pointer);
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500893 properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MaxValue",
Anthony Wilson3929aca2019-07-19 15:42:33 -0500894 "/ReadingRangeMax"_json_pointer);
George Liu51c35a82022-10-13 20:22:14 +0800895 properties.emplace_back("xyz.openbmc_project.Sensor.Accuracy",
896 "Accuracy", "/Accuracy"_json_pointer);
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500897 }
898 else if (sensorType == "temperature")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700899 {
900 properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MinValue",
Anthony Wilson3929aca2019-07-19 15:42:33 -0500901 "/MinReadingRangeTemp"_json_pointer);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700902 properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MaxValue",
Anthony Wilson3929aca2019-07-19 15:42:33 -0500903 "/MaxReadingRangeTemp"_json_pointer);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700904 }
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500905 else if (sensorType != "power")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700906 {
907 properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MinValue",
Anthony Wilson3929aca2019-07-19 15:42:33 -0500908 "/MinReadingRange"_json_pointer);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700909 properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MaxValue",
Anthony Wilson3929aca2019-07-19 15:42:33 -0500910 "/MaxReadingRange"_json_pointer);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700911 }
912
Anthony Wilson3929aca2019-07-19 15:42:33 -0500913 for (const std::tuple<const char*, const char*,
914 nlohmann::json::json_pointer>& p : properties)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700915 {
Ed Tanous1d7c0052022-08-09 12:32:26 -0700916 for (const auto& [valueName, valueVariant] : propertiesDict)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700917 {
Ed Tanous1d7c0052022-08-09 12:32:26 -0700918 if (valueName != std::get<1>(p))
Ed Tanous1abe55e2018-09-05 08:30:59 -0700919 {
Ed Tanous711ac7a2021-12-20 09:34:41 -0800920 continue;
921 }
Ed Tanous1d7c0052022-08-09 12:32:26 -0700922
923 // The property we want to set may be nested json, so use
924 // a json_pointer for easy indexing into the json structure.
925 const nlohmann::json::json_pointer& key = std::get<2>(p);
926
Ed Tanous1d7c0052022-08-09 12:32:26 -0700927 const double* doubleValue = std::get_if<double>(&valueVariant);
Ed Tanous40e4f382022-08-09 18:42:51 -0700928 if (doubleValue == nullptr)
Ed Tanous711ac7a2021-12-20 09:34:41 -0800929 {
Ed Tanous40e4f382022-08-09 18:42:51 -0700930 BMCWEB_LOG_ERROR << "Got value interface that wasn't double";
Ed Tanous1d7c0052022-08-09 12:32:26 -0700931 continue;
932 }
Ed Tanous1d7c0052022-08-09 12:32:26 -0700933 if (forceToInt)
934 {
Ed Tanous40e4f382022-08-09 18:42:51 -0700935 sensorJson[key] = static_cast<int64_t>(*doubleValue);
Ed Tanous1d7c0052022-08-09 12:32:26 -0700936 }
937 else
938 {
Ed Tanous40e4f382022-08-09 18:42:51 -0700939 sensorJson[key] = *doubleValue;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700940 }
941 }
942 }
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100943}
944
Ed Tanous1d7c0052022-08-09 12:32:26 -0700945/**
946 * @brief Builds a json sensor representation of a sensor.
947 * @param sensorName The name of the sensor to be built
948 * @param sensorType The type (temperature, fan_tach, etc) of the sensor to
949 * build
950 * @param chassisSubNode The subnode (thermal, sensor, ect) of the sensor
951 * @param interfacesDict A dictionary of the interfaces and properties of said
952 * interfaces to be built from
953 * @param sensorJson The json object to fill
954 * @param inventoryItem D-Bus inventory item associated with the sensor. Will
955 * be nullptr if no associated inventory item was found.
956 */
957inline void objectInterfacesToJson(
958 const std::string& sensorName, const std::string& sensorType,
959 const std::string& chassisSubNode,
960 const dbus::utility::DBusInteracesMap& interfacesDict,
961 nlohmann::json& sensorJson, InventoryItem* inventoryItem)
962{
963
964 for (const auto& [interface, valuesDict] : interfacesDict)
965 {
966 objectPropertiesToJson(sensorName, sensorType, chassisSubNode,
967 valuesDict, sensorJson, inventoryItem);
968 }
Ed Tanousc1d019a2022-08-06 09:36:06 -0700969 BMCWEB_LOG_DEBUG << "Added sensor " << sensorName;
Ed Tanous1d7c0052022-08-09 12:32:26 -0700970}
971
Ed Tanousb5a76932020-09-29 16:16:58 -0700972inline void populateFanRedundancy(
973 const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp)
James Feist8bd25cc2019-03-15 15:14:00 -0700974{
George Liue99073f2022-12-09 11:06:16 +0800975 constexpr std::array<std::string_view, 1> interfaces = {
976 "xyz.openbmc_project.Control.FanRedundancy"};
977 dbus::utility::getSubTree(
978 "/xyz/openbmc_project/control", 2, interfaces,
Ed Tanousb9d36b42022-02-26 21:42:46 -0800979 [sensorsAsyncResp](
George Liue99073f2022-12-09 11:06:16 +0800980 const boost::system::error_code& ec,
Ed Tanousb9d36b42022-02-26 21:42:46 -0800981 const dbus::utility::MapperGetSubTreeResponse& resp) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700982 if (ec)
983 {
984 return; // don't have to have this interface
985 }
986 for (const std::pair<
987 std::string,
988 std::vector<std::pair<std::string, std::vector<std::string>>>>&
989 pathPair : resp)
990 {
991 const std::string& path = pathPair.first;
992 const std::vector<std::pair<std::string, std::vector<std::string>>>&
993 objDict = pathPair.second;
994 if (objDict.empty())
James Feist8bd25cc2019-03-15 15:14:00 -0700995 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700996 continue; // this should be impossible
James Feist8bd25cc2019-03-15 15:14:00 -0700997 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700998
999 const std::string& owner = objDict.begin()->first;
1000 sdbusplus::asio::getProperty<std::vector<std::string>>(
1001 *crow::connections::systemBus,
1002 "xyz.openbmc_project.ObjectMapper", path + "/chassis",
1003 "xyz.openbmc_project.Association", "endpoints",
1004 [path, owner,
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08001005 sensorsAsyncResp](const boost::system::error_code& e,
Ed Tanous002d39b2022-05-31 08:59:27 -07001006 const std::vector<std::string>& endpoints) {
1007 if (e)
James Feist8bd25cc2019-03-15 15:14:00 -07001008 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001009 return; // if they don't have an association we
1010 // can't tell what chassis is
James Feist8bd25cc2019-03-15 15:14:00 -07001011 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001012 auto found =
1013 std::find_if(endpoints.begin(), endpoints.end(),
1014 [sensorsAsyncResp](const std::string& entry) {
1015 return entry.find(sensorsAsyncResp->chassisId) !=
1016 std::string::npos;
1017 });
James Feist8bd25cc2019-03-15 15:14:00 -07001018
Ed Tanous002d39b2022-05-31 08:59:27 -07001019 if (found == endpoints.end())
1020 {
1021 return;
1022 }
Krzysztof Grobelny86d89ed2022-08-29 14:49:20 +02001023 sdbusplus::asio::getAllProperties(
1024 *crow::connections::systemBus, owner, path,
1025 "xyz.openbmc_project.Control.FanRedundancy",
Ed Tanous002d39b2022-05-31 08:59:27 -07001026 [path, sensorsAsyncResp](
1027 const boost::system::error_code& err,
Krzysztof Grobelny86d89ed2022-08-29 14:49:20 +02001028 const dbus::utility::DBusPropertiesMap& ret) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001029 if (err)
1030 {
1031 return; // don't have to have this
1032 // interface
1033 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001034
Krzysztof Grobelny86d89ed2022-08-29 14:49:20 +02001035 const uint8_t* allowedFailures = nullptr;
1036 const std::vector<std::string>* collection = nullptr;
1037 const std::string* status = nullptr;
1038
1039 const bool success = sdbusplus::unpackPropertiesNoThrow(
1040 dbus_utils::UnpackErrorPrinter(), ret,
1041 "AllowedFailures", allowedFailures, "Collection",
1042 collection, "Status", status);
1043
1044 if (!success)
1045 {
1046 messages::internalError(
1047 sensorsAsyncResp->asyncResp->res);
1048 return;
1049 }
1050
1051 if (allowedFailures == nullptr || collection == nullptr ||
1052 status == nullptr)
Ed Tanous002d39b2022-05-31 08:59:27 -07001053 {
1054 BMCWEB_LOG_ERROR << "Invalid redundancy interface";
1055 messages::internalError(
1056 sensorsAsyncResp->asyncResp->res);
1057 return;
1058 }
1059
Ed Tanous002d39b2022-05-31 08:59:27 -07001060 sdbusplus::message::object_path objectPath(path);
1061 std::string name = objectPath.filename();
1062 if (name.empty())
1063 {
1064 // this should be impossible
1065 messages::internalError(
1066 sensorsAsyncResp->asyncResp->res);
1067 return;
1068 }
1069 std::replace(name.begin(), name.end(), '_', ' ');
1070
1071 std::string health;
1072
Ed Tanous11ba3972022-07-11 09:50:41 -07001073 if (status->ends_with("Full"))
Ed Tanous002d39b2022-05-31 08:59:27 -07001074 {
1075 health = "OK";
1076 }
Ed Tanous11ba3972022-07-11 09:50:41 -07001077 else if (status->ends_with("Degraded"))
Ed Tanous002d39b2022-05-31 08:59:27 -07001078 {
1079 health = "Warning";
1080 }
1081 else
1082 {
1083 health = "Critical";
1084 }
1085 nlohmann::json::array_t redfishCollection;
1086 const auto& fanRedfish =
1087 sensorsAsyncResp->asyncResp->res.jsonValue["Fans"];
1088 for (const std::string& item : *collection)
1089 {
Ed Tanous8a592812022-06-04 09:06:59 -07001090 sdbusplus::message::object_path itemPath(item);
1091 std::string itemName = itemPath.filename();
Ed Tanous002d39b2022-05-31 08:59:27 -07001092 if (itemName.empty())
James Feist8bd25cc2019-03-15 15:14:00 -07001093 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001094 continue;
James Feist8bd25cc2019-03-15 15:14:00 -07001095 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001096 /*
1097 todo(ed): merge patch that fixes the names
1098 std::replace(itemName.begin(),
1099 itemName.end(), '_', ' ');*/
1100 auto schemaItem =
1101 std::find_if(fanRedfish.begin(), fanRedfish.end(),
1102 [itemName](const nlohmann::json& fan) {
George Liu3e35c762023-03-08 16:56:38 +08001103 return fan["Name"] == itemName;
James Feist8bd25cc2019-03-15 15:14:00 -07001104 });
Ed Tanous002d39b2022-05-31 08:59:27 -07001105 if (schemaItem != fanRedfish.end())
James Feist8bd25cc2019-03-15 15:14:00 -07001106 {
Ed Tanous8a592812022-06-04 09:06:59 -07001107 nlohmann::json::object_t collectionId;
1108 collectionId["@odata.id"] =
Ed Tanous002d39b2022-05-31 08:59:27 -07001109 (*schemaItem)["@odata.id"];
1110 redfishCollection.emplace_back(
Ed Tanous8a592812022-06-04 09:06:59 -07001111 std::move(collectionId));
Ed Tanous002d39b2022-05-31 08:59:27 -07001112 }
1113 else
1114 {
1115 BMCWEB_LOG_ERROR << "failed to find fan in schema";
1116 messages::internalError(
1117 sensorsAsyncResp->asyncResp->res);
James Feist8bd25cc2019-03-15 15:14:00 -07001118 return;
1119 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001120 }
James Feist8bd25cc2019-03-15 15:14:00 -07001121
Ed Tanous002d39b2022-05-31 08:59:27 -07001122 size_t minNumNeeded =
1123 collection->empty()
1124 ? 0
1125 : collection->size() - *allowedFailures;
1126 nlohmann::json& jResp = sensorsAsyncResp->asyncResp->res
1127 .jsonValue["Redundancy"];
James Feist8bd25cc2019-03-15 15:14:00 -07001128
Ed Tanous002d39b2022-05-31 08:59:27 -07001129 nlohmann::json::object_t redundancy;
Willy Tueddfc432022-09-26 16:46:38 +00001130 boost::urls::url url = crow::utility::urlFromPieces(
1131 "redfish", "v1", "Chassis", sensorsAsyncResp->chassisId,
1132 sensorsAsyncResp->chassisSubNode);
1133 url.set_fragment(("/Redundancy"_json_pointer / jResp.size())
1134 .to_string());
1135 redundancy["@odata.id"] = std::move(url);
Ed Tanous002d39b2022-05-31 08:59:27 -07001136 redundancy["@odata.type"] = "#Redundancy.v1_3_2.Redundancy";
1137 redundancy["MinNumNeeded"] = minNumNeeded;
Ed Tanous002d39b2022-05-31 08:59:27 -07001138 redundancy["Mode"] = "N+m";
1139 redundancy["Name"] = name;
1140 redundancy["RedundancySet"] = redfishCollection;
1141 redundancy["Status"]["Health"] = health;
1142 redundancy["Status"]["State"] = "Enabled";
James Feist8bd25cc2019-03-15 15:14:00 -07001143
Ed Tanous002d39b2022-05-31 08:59:27 -07001144 jResp.push_back(std::move(redundancy));
Krzysztof Grobelny86d89ed2022-08-29 14:49:20 +02001145 });
Ed Tanous002d39b2022-05-31 08:59:27 -07001146 });
1147 }
George Liue99073f2022-12-09 11:06:16 +08001148 });
James Feist8bd25cc2019-03-15 15:14:00 -07001149}
1150
Ed Tanousb5a76932020-09-29 16:16:58 -07001151inline void
Ed Tanous81ce6092020-12-17 16:54:55 +00001152 sortJSONResponse(const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07001153{
zhanghch058d1b46d2021-04-01 11:18:24 +08001154 nlohmann::json& response = sensorsAsyncResp->asyncResp->res.jsonValue;
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07001155 std::array<std::string, 2> sensorHeaders{"Temperatures", "Fans"};
Ed Tanous81ce6092020-12-17 16:54:55 +00001156 if (sensorsAsyncResp->chassisSubNode == sensors::node::power)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07001157 {
1158 sensorHeaders = {"Voltages", "PowerSupplies"};
1159 }
1160 for (const std::string& sensorGroup : sensorHeaders)
1161 {
1162 nlohmann::json::iterator entry = response.find(sensorGroup);
1163 if (entry != response.end())
1164 {
1165 std::sort(entry->begin(), entry->end(),
Ed Tanous02cad962022-06-30 16:50:15 -07001166 [](const nlohmann::json& c1, const nlohmann::json& c2) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001167 return c1["Name"] < c2["Name"];
1168 });
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07001169
1170 // add the index counts to the end of each entry
1171 size_t count = 0;
1172 for (nlohmann::json& sensorJson : *entry)
1173 {
1174 nlohmann::json::iterator odata = sensorJson.find("@odata.id");
1175 if (odata == sensorJson.end())
1176 {
1177 continue;
1178 }
1179 std::string* value = odata->get_ptr<std::string*>();
1180 if (value != nullptr)
1181 {
Willy Tueddfc432022-09-26 16:46:38 +00001182 *value += "/" + std::to_string(count);
George Liu3e35c762023-03-08 16:56:38 +08001183 sensorJson["MemberId"] = std::to_string(count);
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07001184 count++;
Ed Tanous81ce6092020-12-17 16:54:55 +00001185 sensorsAsyncResp->updateUri(sensorJson["Name"], *value);
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07001186 }
1187 }
1188 }
1189 }
1190}
1191
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +01001192/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001193 * @brief Finds the inventory item with the specified object path.
1194 * @param inventoryItems D-Bus inventory items associated with sensors.
1195 * @param invItemObjPath D-Bus object path of inventory item.
1196 * @return Inventory item within vector, or nullptr if no match found.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001197 */
Ed Tanous23a21a12020-07-25 04:45:05 +00001198inline InventoryItem* findInventoryItem(
Ed Tanousb5a76932020-09-29 16:16:58 -07001199 const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems,
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001200 const std::string& invItemObjPath)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001201{
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001202 for (InventoryItem& inventoryItem : *inventoryItems)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001203 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001204 if (inventoryItem.objectPath == invItemObjPath)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001205 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001206 return &inventoryItem;
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001207 }
1208 }
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001209 return nullptr;
1210}
1211
1212/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001213 * @brief Finds the inventory item associated with the specified sensor.
1214 * @param inventoryItems D-Bus inventory items associated with sensors.
1215 * @param sensorObjPath D-Bus object path of sensor.
1216 * @return Inventory item within vector, or nullptr if no match found.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001217 */
Ed Tanous23a21a12020-07-25 04:45:05 +00001218inline InventoryItem* findInventoryItemForSensor(
Ed Tanousb5a76932020-09-29 16:16:58 -07001219 const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems,
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001220 const std::string& sensorObjPath)
1221{
1222 for (InventoryItem& inventoryItem : *inventoryItems)
1223 {
1224 if (inventoryItem.sensors.count(sensorObjPath) > 0)
1225 {
1226 return &inventoryItem;
1227 }
1228 }
1229 return nullptr;
1230}
1231
1232/**
Anthony Wilsond5005492019-07-31 16:34:17 -05001233 * @brief Finds the inventory item associated with the specified led path.
1234 * @param inventoryItems D-Bus inventory items associated with sensors.
1235 * @param ledObjPath D-Bus object path of led.
1236 * @return Inventory item within vector, or nullptr if no match found.
1237 */
1238inline InventoryItem*
1239 findInventoryItemForLed(std::vector<InventoryItem>& inventoryItems,
1240 const std::string& ledObjPath)
1241{
1242 for (InventoryItem& inventoryItem : inventoryItems)
1243 {
1244 if (inventoryItem.ledObjectPath == ledObjPath)
1245 {
1246 return &inventoryItem;
1247 }
1248 }
1249 return nullptr;
1250}
1251
1252/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001253 * @brief Adds inventory item and associated sensor to specified vector.
1254 *
1255 * Adds a new InventoryItem to the vector if necessary. Searches for an
1256 * existing InventoryItem with the specified object path. If not found, one is
1257 * added to the vector.
1258 *
1259 * Next, the specified sensor is added to the set of sensors associated with the
1260 * InventoryItem.
1261 *
1262 * @param inventoryItems D-Bus inventory items associated with sensors.
1263 * @param invItemObjPath D-Bus object path of inventory item.
1264 * @param sensorObjPath D-Bus object path of sensor
1265 */
Ed Tanousb5a76932020-09-29 16:16:58 -07001266inline void addInventoryItem(
1267 const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems,
1268 const std::string& invItemObjPath, const std::string& sensorObjPath)
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001269{
1270 // Look for inventory item in vector
1271 InventoryItem* inventoryItem =
1272 findInventoryItem(inventoryItems, invItemObjPath);
1273
1274 // If inventory item doesn't exist in vector, add it
1275 if (inventoryItem == nullptr)
1276 {
1277 inventoryItems->emplace_back(invItemObjPath);
1278 inventoryItem = &(inventoryItems->back());
1279 }
1280
1281 // Add sensor to set of sensors associated with inventory item
1282 inventoryItem->sensors.emplace(sensorObjPath);
1283}
1284
1285/**
1286 * @brief Stores D-Bus data in the specified inventory item.
1287 *
1288 * Finds D-Bus data in the specified map of interfaces. Stores the data in the
1289 * specified InventoryItem.
1290 *
1291 * This data is later used to provide sensor property values in the JSON
1292 * response.
1293 *
1294 * @param inventoryItem Inventory item where data will be stored.
1295 * @param interfacesDict Map containing D-Bus interfaces and their properties
1296 * for the specified inventory item.
1297 */
Ed Tanous23a21a12020-07-25 04:45:05 +00001298inline void storeInventoryItemData(
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001299 InventoryItem& inventoryItem,
Ed Tanous711ac7a2021-12-20 09:34:41 -08001300 const dbus::utility::DBusInteracesMap& interfacesDict)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001301{
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001302 // Get properties from Inventory.Item interface
Ed Tanous711ac7a2021-12-20 09:34:41 -08001303
Ed Tanous9eb808c2022-01-25 10:19:23 -08001304 for (const auto& [interface, values] : interfacesDict)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001305 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001306 if (interface == "xyz.openbmc_project.Inventory.Item")
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001307 {
Ed Tanous9eb808c2022-01-25 10:19:23 -08001308 for (const auto& [name, dbusValue] : values)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001309 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001310 if (name == "Present")
1311 {
1312 const bool* value = std::get_if<bool>(&dbusValue);
1313 if (value != nullptr)
1314 {
1315 inventoryItem.isPresent = *value;
1316 }
1317 }
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001318 }
1319 }
Ed Tanous711ac7a2021-12-20 09:34:41 -08001320 // Check if Inventory.Item.PowerSupply interface is present
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001321
Ed Tanous711ac7a2021-12-20 09:34:41 -08001322 if (interface == "xyz.openbmc_project.Inventory.Item.PowerSupply")
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001323 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001324 inventoryItem.isPowerSupply = true;
1325 }
1326
1327 // Get properties from Inventory.Decorator.Asset interface
1328 if (interface == "xyz.openbmc_project.Inventory.Decorator.Asset")
1329 {
Ed Tanous9eb808c2022-01-25 10:19:23 -08001330 for (const auto& [name, dbusValue] : values)
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001331 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001332 if (name == "Manufacturer")
1333 {
1334 const std::string* value =
1335 std::get_if<std::string>(&dbusValue);
1336 if (value != nullptr)
1337 {
1338 inventoryItem.manufacturer = *value;
1339 }
1340 }
1341 if (name == "Model")
1342 {
1343 const std::string* value =
1344 std::get_if<std::string>(&dbusValue);
1345 if (value != nullptr)
1346 {
1347 inventoryItem.model = *value;
1348 }
1349 }
1350 if (name == "SerialNumber")
1351 {
1352 const std::string* value =
1353 std::get_if<std::string>(&dbusValue);
1354 if (value != nullptr)
1355 {
1356 inventoryItem.serialNumber = *value;
1357 }
1358 }
1359 if (name == "PartNumber")
1360 {
1361 const std::string* value =
1362 std::get_if<std::string>(&dbusValue);
1363 if (value != nullptr)
1364 {
1365 inventoryItem.partNumber = *value;
1366 }
1367 }
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001368 }
1369 }
1370
Ed Tanous711ac7a2021-12-20 09:34:41 -08001371 if (interface ==
1372 "xyz.openbmc_project.State.Decorator.OperationalStatus")
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001373 {
Ed Tanous9eb808c2022-01-25 10:19:23 -08001374 for (const auto& [name, dbusValue] : values)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001375 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001376 if (name == "Functional")
1377 {
1378 const bool* value = std::get_if<bool>(&dbusValue);
1379 if (value != nullptr)
1380 {
1381 inventoryItem.isFunctional = *value;
1382 }
1383 }
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001384 }
1385 }
1386 }
1387}
1388
1389/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001390 * @brief Gets D-Bus data for inventory items associated with sensors.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001391 *
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001392 * Uses the specified connections (services) to obtain D-Bus data for inventory
1393 * items associated with sensors. Stores the resulting data in the
1394 * inventoryItems vector.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001395 *
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001396 * This data is later used to provide sensor property values in the JSON
1397 * response.
1398 *
1399 * Finds the inventory item data asynchronously. Invokes callback when data has
1400 * been obtained.
1401 *
1402 * The callback must have the following signature:
1403 * @code
Anthony Wilsond5005492019-07-31 16:34:17 -05001404 * callback(void)
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001405 * @endcode
1406 *
1407 * This function is called recursively, obtaining data asynchronously from one
1408 * connection in each call. This ensures the callback is not invoked until the
1409 * last asynchronous function has completed.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001410 *
1411 * @param sensorsAsyncResp Pointer to object holding response data.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001412 * @param inventoryItems D-Bus inventory items associated with sensors.
1413 * @param invConnections Connections that provide data for the inventory items.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001414 * implements ObjectManager.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001415 * @param callback Callback to invoke when inventory data has been obtained.
1416 * @param invConnectionsIndex Current index in invConnections. Only specified
1417 * in recursive calls to this function.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001418 */
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001419template <typename Callback>
1420static void getInventoryItemsData(
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001421 std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001422 std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
Ed Tanousd0090732022-10-04 17:22:56 -07001423 std::shared_ptr<std::set<std::string>> invConnections, Callback&& callback,
1424 size_t invConnectionsIndex = 0)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001425{
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001426 BMCWEB_LOG_DEBUG << "getInventoryItemsData enter";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001427
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001428 // If no more connections left, call callback
1429 if (invConnectionsIndex >= invConnections->size())
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001430 {
Anthony Wilsond5005492019-07-31 16:34:17 -05001431 callback();
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001432 BMCWEB_LOG_DEBUG << "getInventoryItemsData exit";
1433 return;
1434 }
1435
1436 // Get inventory item data from current connection
Nan Zhoufe04d492022-06-22 17:10:41 +00001437 auto it = invConnections->begin();
1438 std::advance(it, invConnectionsIndex);
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001439 if (it != invConnections->end())
1440 {
1441 const std::string& invConnection = *it;
1442
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001443 // Response handler for GetManagedObjects
Ed Tanousd0090732022-10-04 17:22:56 -07001444 auto respHandler = [sensorsAsyncResp, inventoryItems, invConnections,
1445 callback{std::forward<Callback>(callback)},
1446 invConnectionsIndex](
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08001447 const boost::system::error_code& ec,
Ed Tanousd0090732022-10-04 17:22:56 -07001448 const dbus::utility::ManagedObjectType& resp) {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001449 BMCWEB_LOG_DEBUG << "getInventoryItemsData respHandler enter";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001450 if (ec)
1451 {
1452 BMCWEB_LOG_ERROR
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001453 << "getInventoryItemsData respHandler DBus error " << ec;
zhanghch058d1b46d2021-04-01 11:18:24 +08001454 messages::internalError(sensorsAsyncResp->asyncResp->res);
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001455 return;
1456 }
1457
1458 // Loop through returned object paths
1459 for (const auto& objDictEntry : resp)
1460 {
1461 const std::string& objPath =
1462 static_cast<const std::string&>(objDictEntry.first);
1463
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001464 // If this object path is one of the specified inventory items
1465 InventoryItem* inventoryItem =
1466 findInventoryItem(inventoryItems, objPath);
1467 if (inventoryItem != nullptr)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001468 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001469 // Store inventory data in InventoryItem
1470 storeInventoryItemData(*inventoryItem, objDictEntry.second);
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001471 }
1472 }
1473
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001474 // Recurse to get inventory item data from next connection
1475 getInventoryItemsData(sensorsAsyncResp, inventoryItems,
Ed Tanousd0090732022-10-04 17:22:56 -07001476 invConnections, std::move(callback),
1477 invConnectionsIndex + 1);
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001478
1479 BMCWEB_LOG_DEBUG << "getInventoryItemsData respHandler exit";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001480 };
1481
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001482 // Get all object paths and their interfaces for current connection
1483 crow::connections::systemBus->async_method_call(
Ed Tanousd0090732022-10-04 17:22:56 -07001484 std::move(respHandler), invConnection,
Ed Tanousf8bb0ff2022-12-09 11:08:45 -08001485 "/xyz/openbmc_project/inventory",
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001486 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
1487 }
1488
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001489 BMCWEB_LOG_DEBUG << "getInventoryItemsData exit";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001490}
1491
1492/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001493 * @brief Gets connections that provide D-Bus data for inventory items.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001494 *
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001495 * Gets the D-Bus connections (services) that provide data for the inventory
1496 * items that are associated with sensors.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001497 *
1498 * Finds the connections asynchronously. Invokes callback when information has
1499 * been obtained.
1500 *
1501 * The callback must have the following signature:
1502 * @code
Nan Zhoufe04d492022-06-22 17:10:41 +00001503 * callback(std::shared_ptr<std::set<std::string>> invConnections)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001504 * @endcode
1505 *
1506 * @param sensorsAsyncResp Pointer to object holding response data.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001507 * @param inventoryItems D-Bus inventory items associated with sensors.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001508 * @param callback Callback to invoke when connections have been obtained.
1509 */
1510template <typename Callback>
1511static void getInventoryItemsConnections(
Ed Tanousb5a76932020-09-29 16:16:58 -07001512 const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
1513 const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems,
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001514 Callback&& callback)
1515{
1516 BMCWEB_LOG_DEBUG << "getInventoryItemsConnections enter";
1517
1518 const std::string path = "/xyz/openbmc_project/inventory";
George Liue99073f2022-12-09 11:06:16 +08001519 constexpr std::array<std::string_view, 4> interfaces = {
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001520 "xyz.openbmc_project.Inventory.Item",
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001521 "xyz.openbmc_project.Inventory.Item.PowerSupply",
1522 "xyz.openbmc_project.Inventory.Decorator.Asset",
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001523 "xyz.openbmc_project.State.Decorator.OperationalStatus"};
1524
George Liue99073f2022-12-09 11:06:16 +08001525 // Make call to ObjectMapper to find all inventory items
1526 dbus::utility::getSubTree(
1527 path, 0, interfaces,
Ed Tanous002d39b2022-05-31 08:59:27 -07001528 [callback{std::forward<Callback>(callback)}, sensorsAsyncResp,
1529 inventoryItems](
George Liue99073f2022-12-09 11:06:16 +08001530 const boost::system::error_code& ec,
Ed Tanous002d39b2022-05-31 08:59:27 -07001531 const dbus::utility::MapperGetSubTreeResponse& subtree) {
George Liue99073f2022-12-09 11:06:16 +08001532 // Response handler for parsing output from GetSubTree
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001533 BMCWEB_LOG_DEBUG << "getInventoryItemsConnections respHandler enter";
1534 if (ec)
1535 {
zhanghch058d1b46d2021-04-01 11:18:24 +08001536 messages::internalError(sensorsAsyncResp->asyncResp->res);
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001537 BMCWEB_LOG_ERROR
1538 << "getInventoryItemsConnections respHandler DBus error " << ec;
1539 return;
1540 }
1541
1542 // Make unique list of connections for desired inventory items
Nan Zhoufe04d492022-06-22 17:10:41 +00001543 std::shared_ptr<std::set<std::string>> invConnections =
1544 std::make_shared<std::set<std::string>>();
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001545
1546 // Loop through objects from GetSubTree
1547 for (const std::pair<
1548 std::string,
1549 std::vector<std::pair<std::string, std::vector<std::string>>>>&
1550 object : subtree)
1551 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001552 // Check if object path is one of the specified inventory items
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001553 const std::string& objPath = object.first;
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001554 if (findInventoryItem(inventoryItems, objPath) != nullptr)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001555 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001556 // Store all connections to inventory item
1557 for (const std::pair<std::string, std::vector<std::string>>&
1558 objData : object.second)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001559 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001560 const std::string& invConnection = objData.first;
1561 invConnections->insert(invConnection);
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001562 }
1563 }
1564 }
Anthony Wilsond5005492019-07-31 16:34:17 -05001565
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001566 callback(invConnections);
1567 BMCWEB_LOG_DEBUG << "getInventoryItemsConnections respHandler exit";
George Liue99073f2022-12-09 11:06:16 +08001568 });
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001569 BMCWEB_LOG_DEBUG << "getInventoryItemsConnections exit";
1570}
1571
1572/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001573 * @brief Gets associations from sensors to inventory items.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001574 *
1575 * Looks for ObjectMapper associations from the specified sensors to related
Anthony Wilsond5005492019-07-31 16:34:17 -05001576 * inventory items. Then finds the associations from those inventory items to
1577 * their LEDs, if any.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001578 *
1579 * Finds the inventory items asynchronously. Invokes callback when information
1580 * has been obtained.
1581 *
1582 * The callback must have the following signature:
1583 * @code
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001584 * callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001585 * @endcode
1586 *
1587 * @param sensorsAsyncResp Pointer to object holding response data.
1588 * @param sensorNames All sensors within the current chassis.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001589 * implements ObjectManager.
1590 * @param callback Callback to invoke when inventory items have been obtained.
1591 */
1592template <typename Callback>
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001593static void getInventoryItemAssociations(
Ed Tanousb5a76932020-09-29 16:16:58 -07001594 const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
Nan Zhoufe04d492022-06-22 17:10:41 +00001595 const std::shared_ptr<std::set<std::string>>& sensorNames,
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001596 Callback&& callback)
1597{
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001598 BMCWEB_LOG_DEBUG << "getInventoryItemAssociations enter";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001599
1600 // Response handler for GetManagedObjects
Ed Tanous02cad962022-06-30 16:50:15 -07001601 auto respHandler =
1602 [callback{std::forward<Callback>(callback)}, sensorsAsyncResp,
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08001603 sensorNames](const boost::system::error_code& ec,
Ed Tanous02cad962022-06-30 16:50:15 -07001604 const dbus::utility::ManagedObjectType& resp) {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001605 BMCWEB_LOG_DEBUG << "getInventoryItemAssociations respHandler enter";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001606 if (ec)
1607 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001608 BMCWEB_LOG_ERROR
1609 << "getInventoryItemAssociations respHandler DBus error " << ec;
zhanghch058d1b46d2021-04-01 11:18:24 +08001610 messages::internalError(sensorsAsyncResp->asyncResp->res);
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001611 return;
1612 }
1613
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001614 // Create vector to hold list of inventory items
1615 std::shared_ptr<std::vector<InventoryItem>> inventoryItems =
1616 std::make_shared<std::vector<InventoryItem>>();
1617
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001618 // Loop through returned object paths
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001619 std::string sensorAssocPath;
1620 sensorAssocPath.reserve(128); // avoid memory allocations
1621 for (const auto& objDictEntry : resp)
1622 {
1623 const std::string& objPath =
1624 static_cast<const std::string&>(objDictEntry.first);
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001625
1626 // If path is inventory association for one of the specified sensors
1627 for (const std::string& sensorName : *sensorNames)
1628 {
1629 sensorAssocPath = sensorName;
1630 sensorAssocPath += "/inventory";
1631 if (objPath == sensorAssocPath)
1632 {
1633 // Get Association interface for object path
Ed Tanous711ac7a2021-12-20 09:34:41 -08001634 for (const auto& [interface, values] : objDictEntry.second)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001635 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001636 if (interface == "xyz.openbmc_project.Association")
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001637 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001638 for (const auto& [valueName, value] : values)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001639 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001640 if (valueName == "endpoints")
1641 {
1642 const std::vector<std::string>* endpoints =
1643 std::get_if<std::vector<std::string>>(
1644 &value);
1645 if ((endpoints != nullptr) &&
1646 !endpoints->empty())
1647 {
1648 // Add inventory item to vector
1649 const std::string& invItemPath =
1650 endpoints->front();
1651 addInventoryItem(inventoryItems,
1652 invItemPath,
1653 sensorName);
1654 }
1655 }
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001656 }
1657 }
1658 }
1659 break;
1660 }
1661 }
1662 }
1663
Anthony Wilsond5005492019-07-31 16:34:17 -05001664 // Now loop through the returned object paths again, this time to
1665 // find the leds associated with the inventory items we just found
1666 std::string inventoryAssocPath;
1667 inventoryAssocPath.reserve(128); // avoid memory allocations
1668 for (const auto& objDictEntry : resp)
1669 {
1670 const std::string& objPath =
1671 static_cast<const std::string&>(objDictEntry.first);
Anthony Wilsond5005492019-07-31 16:34:17 -05001672
1673 for (InventoryItem& inventoryItem : *inventoryItems)
1674 {
1675 inventoryAssocPath = inventoryItem.objectPath;
1676 inventoryAssocPath += "/leds";
1677 if (objPath == inventoryAssocPath)
1678 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001679 for (const auto& [interface, values] : objDictEntry.second)
Anthony Wilsond5005492019-07-31 16:34:17 -05001680 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001681 if (interface == "xyz.openbmc_project.Association")
Anthony Wilsond5005492019-07-31 16:34:17 -05001682 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001683 for (const auto& [valueName, value] : values)
Anthony Wilsond5005492019-07-31 16:34:17 -05001684 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001685 if (valueName == "endpoints")
1686 {
1687 const std::vector<std::string>* endpoints =
1688 std::get_if<std::vector<std::string>>(
1689 &value);
1690 if ((endpoints != nullptr) &&
1691 !endpoints->empty())
1692 {
1693 // Add inventory item to vector
1694 // Store LED path in inventory item
1695 const std::string& ledPath =
1696 endpoints->front();
1697 inventoryItem.ledObjectPath = ledPath;
1698 }
1699 }
Anthony Wilsond5005492019-07-31 16:34:17 -05001700 }
1701 }
1702 }
Ed Tanous711ac7a2021-12-20 09:34:41 -08001703
Anthony Wilsond5005492019-07-31 16:34:17 -05001704 break;
1705 }
1706 }
1707 }
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001708 callback(inventoryItems);
1709 BMCWEB_LOG_DEBUG << "getInventoryItemAssociations respHandler exit";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001710 };
1711
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001712 // Call GetManagedObjects on the ObjectMapper to get all associations
1713 crow::connections::systemBus->async_method_call(
Ed Tanousd0090732022-10-04 17:22:56 -07001714 std::move(respHandler), "xyz.openbmc_project.ObjectMapper", "/",
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001715 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
1716
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001717 BMCWEB_LOG_DEBUG << "getInventoryItemAssociations exit";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001718}
1719
1720/**
Anthony Wilsond5005492019-07-31 16:34:17 -05001721 * @brief Gets D-Bus data for inventory item leds associated with sensors.
1722 *
1723 * Uses the specified connections (services) to obtain D-Bus data for inventory
1724 * item leds associated with sensors. Stores the resulting data in the
1725 * inventoryItems vector.
1726 *
1727 * This data is later used to provide sensor property values in the JSON
1728 * response.
1729 *
1730 * Finds the inventory item led data asynchronously. Invokes callback when data
1731 * has been obtained.
1732 *
1733 * The callback must have the following signature:
1734 * @code
Gunnar Mills42cbe532019-08-15 15:26:54 -05001735 * callback()
Anthony Wilsond5005492019-07-31 16:34:17 -05001736 * @endcode
1737 *
1738 * This function is called recursively, obtaining data asynchronously from one
1739 * connection in each call. This ensures the callback is not invoked until the
1740 * last asynchronous function has completed.
1741 *
1742 * @param sensorsAsyncResp Pointer to object holding response data.
1743 * @param inventoryItems D-Bus inventory items associated with sensors.
1744 * @param ledConnections Connections that provide data for the inventory leds.
1745 * @param callback Callback to invoke when inventory data has been obtained.
1746 * @param ledConnectionsIndex Current index in ledConnections. Only specified
1747 * in recursive calls to this function.
1748 */
1749template <typename Callback>
1750void getInventoryLedData(
1751 std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
1752 std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
Nan Zhoufe04d492022-06-22 17:10:41 +00001753 std::shared_ptr<std::map<std::string, std::string>> ledConnections,
Anthony Wilsond5005492019-07-31 16:34:17 -05001754 Callback&& callback, size_t ledConnectionsIndex = 0)
1755{
1756 BMCWEB_LOG_DEBUG << "getInventoryLedData enter";
1757
1758 // If no more connections left, call callback
1759 if (ledConnectionsIndex >= ledConnections->size())
1760 {
Gunnar Mills42cbe532019-08-15 15:26:54 -05001761 callback();
Anthony Wilsond5005492019-07-31 16:34:17 -05001762 BMCWEB_LOG_DEBUG << "getInventoryLedData exit";
1763 return;
1764 }
1765
1766 // Get inventory item data from current connection
Nan Zhoufe04d492022-06-22 17:10:41 +00001767 auto it = ledConnections->begin();
1768 std::advance(it, ledConnectionsIndex);
Anthony Wilsond5005492019-07-31 16:34:17 -05001769 if (it != ledConnections->end())
1770 {
1771 const std::string& ledPath = (*it).first;
1772 const std::string& ledConnection = (*it).second;
1773 // Response handler for Get State property
Jonathan Doman1e1e5982021-06-11 09:36:17 -07001774 auto respHandler =
1775 [sensorsAsyncResp, inventoryItems, ledConnections, ledPath,
Ed Tanousf94c4ec2022-01-06 12:44:41 -08001776 callback{std::forward<Callback>(callback)}, ledConnectionsIndex](
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08001777 const boost::system::error_code& ec, const std::string& state) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001778 BMCWEB_LOG_DEBUG << "getInventoryLedData respHandler enter";
1779 if (ec)
1780 {
1781 BMCWEB_LOG_ERROR
1782 << "getInventoryLedData respHandler DBus error " << ec;
1783 messages::internalError(sensorsAsyncResp->asyncResp->res);
1784 return;
1785 }
1786
1787 BMCWEB_LOG_DEBUG << "Led state: " << state;
1788 // Find inventory item with this LED object path
1789 InventoryItem* inventoryItem =
1790 findInventoryItemForLed(*inventoryItems, ledPath);
1791 if (inventoryItem != nullptr)
1792 {
1793 // Store LED state in InventoryItem
Ed Tanous11ba3972022-07-11 09:50:41 -07001794 if (state.ends_with("On"))
Jonathan Doman1e1e5982021-06-11 09:36:17 -07001795 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001796 inventoryItem->ledState = LedState::ON;
Jonathan Doman1e1e5982021-06-11 09:36:17 -07001797 }
Ed Tanous11ba3972022-07-11 09:50:41 -07001798 else if (state.ends_with("Blink"))
Anthony Wilsond5005492019-07-31 16:34:17 -05001799 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001800 inventoryItem->ledState = LedState::BLINK;
Anthony Wilsond5005492019-07-31 16:34:17 -05001801 }
Ed Tanous11ba3972022-07-11 09:50:41 -07001802 else if (state.ends_with("Off"))
Ed Tanous002d39b2022-05-31 08:59:27 -07001803 {
1804 inventoryItem->ledState = LedState::OFF;
1805 }
1806 else
1807 {
1808 inventoryItem->ledState = LedState::UNKNOWN;
1809 }
1810 }
Anthony Wilsond5005492019-07-31 16:34:17 -05001811
Ed Tanous002d39b2022-05-31 08:59:27 -07001812 // Recurse to get LED data from next connection
1813 getInventoryLedData(sensorsAsyncResp, inventoryItems,
1814 ledConnections, std::move(callback),
1815 ledConnectionsIndex + 1);
Anthony Wilsond5005492019-07-31 16:34:17 -05001816
Ed Tanous002d39b2022-05-31 08:59:27 -07001817 BMCWEB_LOG_DEBUG << "getInventoryLedData respHandler exit";
1818 };
Anthony Wilsond5005492019-07-31 16:34:17 -05001819
1820 // Get the State property for the current LED
Jonathan Doman1e1e5982021-06-11 09:36:17 -07001821 sdbusplus::asio::getProperty<std::string>(
1822 *crow::connections::systemBus, ledConnection, ledPath,
1823 "xyz.openbmc_project.Led.Physical", "State",
1824 std::move(respHandler));
Anthony Wilsond5005492019-07-31 16:34:17 -05001825 }
1826
1827 BMCWEB_LOG_DEBUG << "getInventoryLedData exit";
1828}
1829
1830/**
1831 * @brief Gets LED data for LEDs associated with given inventory items.
1832 *
1833 * Gets the D-Bus connections (services) that provide LED data for the LEDs
1834 * associated with the specified inventory items. Then gets the LED data from
1835 * each connection and stores it in the inventory item.
1836 *
1837 * This data is later used to provide sensor property values in the JSON
1838 * response.
1839 *
1840 * Finds the LED data asynchronously. Invokes callback when information has
1841 * been obtained.
1842 *
1843 * The callback must have the following signature:
1844 * @code
Gunnar Mills42cbe532019-08-15 15:26:54 -05001845 * callback()
Anthony Wilsond5005492019-07-31 16:34:17 -05001846 * @endcode
1847 *
1848 * @param sensorsAsyncResp Pointer to object holding response data.
1849 * @param inventoryItems D-Bus inventory items associated with sensors.
1850 * @param callback Callback to invoke when inventory items have been obtained.
1851 */
1852template <typename Callback>
1853void getInventoryLeds(
1854 std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
1855 std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
1856 Callback&& callback)
1857{
1858 BMCWEB_LOG_DEBUG << "getInventoryLeds enter";
1859
1860 const std::string path = "/xyz/openbmc_project";
George Liue99073f2022-12-09 11:06:16 +08001861 constexpr std::array<std::string_view, 1> interfaces = {
Anthony Wilsond5005492019-07-31 16:34:17 -05001862 "xyz.openbmc_project.Led.Physical"};
1863
George Liue99073f2022-12-09 11:06:16 +08001864 // Make call to ObjectMapper to find all inventory items
1865 dbus::utility::getSubTree(
1866 path, 0, interfaces,
Ed Tanous002d39b2022-05-31 08:59:27 -07001867 [callback{std::forward<Callback>(callback)}, sensorsAsyncResp,
1868 inventoryItems](
George Liue99073f2022-12-09 11:06:16 +08001869 const boost::system::error_code& ec,
Ed Tanous002d39b2022-05-31 08:59:27 -07001870 const dbus::utility::MapperGetSubTreeResponse& subtree) {
George Liue99073f2022-12-09 11:06:16 +08001871 // Response handler for parsing output from GetSubTree
Anthony Wilsond5005492019-07-31 16:34:17 -05001872 BMCWEB_LOG_DEBUG << "getInventoryLeds respHandler enter";
1873 if (ec)
1874 {
zhanghch058d1b46d2021-04-01 11:18:24 +08001875 messages::internalError(sensorsAsyncResp->asyncResp->res);
Anthony Wilsond5005492019-07-31 16:34:17 -05001876 BMCWEB_LOG_ERROR << "getInventoryLeds respHandler DBus error "
1877 << ec;
1878 return;
1879 }
1880
1881 // Build map of LED object paths to connections
Nan Zhoufe04d492022-06-22 17:10:41 +00001882 std::shared_ptr<std::map<std::string, std::string>> ledConnections =
1883 std::make_shared<std::map<std::string, std::string>>();
Anthony Wilsond5005492019-07-31 16:34:17 -05001884
1885 // Loop through objects from GetSubTree
1886 for (const std::pair<
1887 std::string,
1888 std::vector<std::pair<std::string, std::vector<std::string>>>>&
1889 object : subtree)
1890 {
1891 // Check if object path is LED for one of the specified inventory
1892 // items
1893 const std::string& ledPath = object.first;
1894 if (findInventoryItemForLed(*inventoryItems, ledPath) != nullptr)
1895 {
1896 // Add mapping from ledPath to connection
1897 const std::string& connection = object.second.begin()->first;
1898 (*ledConnections)[ledPath] = connection;
1899 BMCWEB_LOG_DEBUG << "Added mapping " << ledPath << " -> "
1900 << connection;
1901 }
1902 }
1903
1904 getInventoryLedData(sensorsAsyncResp, inventoryItems, ledConnections,
1905 std::move(callback));
1906 BMCWEB_LOG_DEBUG << "getInventoryLeds respHandler exit";
George Liue99073f2022-12-09 11:06:16 +08001907 });
Anthony Wilsond5005492019-07-31 16:34:17 -05001908 BMCWEB_LOG_DEBUG << "getInventoryLeds exit";
1909}
1910
1911/**
Gunnar Mills42cbe532019-08-15 15:26:54 -05001912 * @brief Gets D-Bus data for Power Supply Attributes such as EfficiencyPercent
1913 *
1914 * Uses the specified connections (services) (currently assumes just one) to
1915 * obtain D-Bus data for Power Supply Attributes. Stores the resulting data in
1916 * the inventoryItems vector. Only stores data in Power Supply inventoryItems.
1917 *
1918 * This data is later used to provide sensor property values in the JSON
1919 * response.
1920 *
1921 * Finds the Power Supply Attributes data asynchronously. Invokes callback
1922 * when data has been obtained.
1923 *
1924 * The callback must have the following signature:
1925 * @code
1926 * callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems)
1927 * @endcode
1928 *
1929 * @param sensorsAsyncResp Pointer to object holding response data.
1930 * @param inventoryItems D-Bus inventory items associated with sensors.
1931 * @param psAttributesConnections Connections that provide data for the Power
1932 * Supply Attributes
1933 * @param callback Callback to invoke when data has been obtained.
1934 */
1935template <typename Callback>
1936void getPowerSupplyAttributesData(
Ed Tanousb5a76932020-09-29 16:16:58 -07001937 const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
Gunnar Mills42cbe532019-08-15 15:26:54 -05001938 std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
Nan Zhoufe04d492022-06-22 17:10:41 +00001939 const std::map<std::string, std::string>& psAttributesConnections,
Gunnar Mills42cbe532019-08-15 15:26:54 -05001940 Callback&& callback)
1941{
1942 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributesData enter";
1943
1944 if (psAttributesConnections.empty())
1945 {
1946 BMCWEB_LOG_DEBUG << "Can't find PowerSupplyAttributes, no connections!";
1947 callback(inventoryItems);
1948 return;
1949 }
1950
1951 // Assuming just one connection (service) for now
Nan Zhoufe04d492022-06-22 17:10:41 +00001952 auto it = psAttributesConnections.begin();
Gunnar Mills42cbe532019-08-15 15:26:54 -05001953
1954 const std::string& psAttributesPath = (*it).first;
1955 const std::string& psAttributesConnection = (*it).second;
1956
1957 // Response handler for Get DeratingFactor property
Ed Tanous002d39b2022-05-31 08:59:27 -07001958 auto respHandler =
1959 [sensorsAsyncResp, inventoryItems,
1960 callback{std::forward<Callback>(callback)}](
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08001961 const boost::system::error_code& ec, const uint32_t value) {
Gunnar Mills42cbe532019-08-15 15:26:54 -05001962 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributesData respHandler enter";
1963 if (ec)
1964 {
1965 BMCWEB_LOG_ERROR
1966 << "getPowerSupplyAttributesData respHandler DBus error " << ec;
zhanghch058d1b46d2021-04-01 11:18:24 +08001967 messages::internalError(sensorsAsyncResp->asyncResp->res);
Gunnar Mills42cbe532019-08-15 15:26:54 -05001968 return;
1969 }
1970
Jonathan Doman1e1e5982021-06-11 09:36:17 -07001971 BMCWEB_LOG_DEBUG << "PS EfficiencyPercent value: " << value;
1972 // Store value in Power Supply Inventory Items
1973 for (InventoryItem& inventoryItem : *inventoryItems)
Gunnar Mills42cbe532019-08-15 15:26:54 -05001974 {
Ed Tanous55f79e62022-01-25 11:26:16 -08001975 if (inventoryItem.isPowerSupply)
Gunnar Mills42cbe532019-08-15 15:26:54 -05001976 {
Jonathan Doman1e1e5982021-06-11 09:36:17 -07001977 inventoryItem.powerSupplyEfficiencyPercent =
1978 static_cast<int>(value);
Gunnar Mills42cbe532019-08-15 15:26:54 -05001979 }
1980 }
Gunnar Mills42cbe532019-08-15 15:26:54 -05001981
1982 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributesData respHandler exit";
1983 callback(inventoryItems);
1984 };
1985
1986 // Get the DeratingFactor property for the PowerSupplyAttributes
1987 // Currently only property on the interface/only one we care about
Jonathan Doman1e1e5982021-06-11 09:36:17 -07001988 sdbusplus::asio::getProperty<uint32_t>(
1989 *crow::connections::systemBus, psAttributesConnection, psAttributesPath,
1990 "xyz.openbmc_project.Control.PowerSupplyAttributes", "DeratingFactor",
1991 std::move(respHandler));
Gunnar Mills42cbe532019-08-15 15:26:54 -05001992
1993 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributesData exit";
1994}
1995
1996/**
1997 * @brief Gets the Power Supply Attributes such as EfficiencyPercent
1998 *
1999 * Gets the D-Bus connection (service) that provides Power Supply Attributes
2000 * data. Then gets the Power Supply Attributes data from the connection
2001 * (currently just assumes 1 connection) and stores the data in the inventory
2002 * item.
2003 *
2004 * This data is later used to provide sensor property values in the JSON
2005 * response. DeratingFactor on D-Bus is mapped to EfficiencyPercent on Redfish.
2006 *
2007 * Finds the Power Supply Attributes data asynchronously. Invokes callback
2008 * when information has been obtained.
2009 *
2010 * The callback must have the following signature:
2011 * @code
2012 * callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems)
2013 * @endcode
2014 *
2015 * @param sensorsAsyncResp Pointer to object holding response data.
2016 * @param inventoryItems D-Bus inventory items associated with sensors.
2017 * @param callback Callback to invoke when data has been obtained.
2018 */
2019template <typename Callback>
2020void getPowerSupplyAttributes(
2021 std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
2022 std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
2023 Callback&& callback)
2024{
2025 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes enter";
2026
2027 // Only need the power supply attributes when the Power Schema
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02002028 if (sensorsAsyncResp->chassisSubNode != sensors::node::power)
Gunnar Mills42cbe532019-08-15 15:26:54 -05002029 {
2030 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes exit since not Power";
2031 callback(inventoryItems);
2032 return;
2033 }
2034
George Liue99073f2022-12-09 11:06:16 +08002035 constexpr std::array<std::string_view, 1> interfaces = {
Gunnar Mills42cbe532019-08-15 15:26:54 -05002036 "xyz.openbmc_project.Control.PowerSupplyAttributes"};
2037
George Liue99073f2022-12-09 11:06:16 +08002038 // Make call to ObjectMapper to find the PowerSupplyAttributes service
2039 dbus::utility::getSubTree(
2040 "/xyz/openbmc_project", 0, interfaces,
Ed Tanousb9d36b42022-02-26 21:42:46 -08002041 [callback{std::forward<Callback>(callback)}, sensorsAsyncResp,
2042 inventoryItems](
George Liue99073f2022-12-09 11:06:16 +08002043 const boost::system::error_code& ec,
Ed Tanousb9d36b42022-02-26 21:42:46 -08002044 const dbus::utility::MapperGetSubTreeResponse& subtree) {
George Liue99073f2022-12-09 11:06:16 +08002045 // Response handler for parsing output from GetSubTree
Ed Tanous002d39b2022-05-31 08:59:27 -07002046 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes respHandler enter";
2047 if (ec)
2048 {
2049 messages::internalError(sensorsAsyncResp->asyncResp->res);
2050 BMCWEB_LOG_ERROR
2051 << "getPowerSupplyAttributes respHandler DBus error " << ec;
2052 return;
2053 }
2054 if (subtree.empty())
2055 {
2056 BMCWEB_LOG_DEBUG << "Can't find Power Supply Attributes!";
2057 callback(inventoryItems);
2058 return;
2059 }
Gunnar Mills42cbe532019-08-15 15:26:54 -05002060
Ed Tanous002d39b2022-05-31 08:59:27 -07002061 // Currently we only support 1 power supply attribute, use this for
2062 // all the power supplies. Build map of object path to connection.
2063 // Assume just 1 connection and 1 path for now.
Nan Zhoufe04d492022-06-22 17:10:41 +00002064 std::map<std::string, std::string> psAttributesConnections;
Gunnar Mills42cbe532019-08-15 15:26:54 -05002065
Ed Tanous002d39b2022-05-31 08:59:27 -07002066 if (subtree[0].first.empty() || subtree[0].second.empty())
2067 {
2068 BMCWEB_LOG_DEBUG << "Power Supply Attributes mapper error!";
2069 callback(inventoryItems);
2070 return;
2071 }
Gunnar Mills42cbe532019-08-15 15:26:54 -05002072
Ed Tanous002d39b2022-05-31 08:59:27 -07002073 const std::string& psAttributesPath = subtree[0].first;
2074 const std::string& connection = subtree[0].second.begin()->first;
Gunnar Mills42cbe532019-08-15 15:26:54 -05002075
Ed Tanous002d39b2022-05-31 08:59:27 -07002076 if (connection.empty())
2077 {
2078 BMCWEB_LOG_DEBUG << "Power Supply Attributes mapper error!";
2079 callback(inventoryItems);
2080 return;
2081 }
Gunnar Mills42cbe532019-08-15 15:26:54 -05002082
Ed Tanous002d39b2022-05-31 08:59:27 -07002083 psAttributesConnections[psAttributesPath] = connection;
2084 BMCWEB_LOG_DEBUG << "Added mapping " << psAttributesPath << " -> "
2085 << connection;
Gunnar Mills42cbe532019-08-15 15:26:54 -05002086
Ed Tanous002d39b2022-05-31 08:59:27 -07002087 getPowerSupplyAttributesData(sensorsAsyncResp, inventoryItems,
2088 psAttributesConnections,
2089 std::move(callback));
2090 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes respHandler exit";
George Liue99073f2022-12-09 11:06:16 +08002091 });
Gunnar Mills42cbe532019-08-15 15:26:54 -05002092 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes exit";
2093}
2094
2095/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002096 * @brief Gets inventory items associated with sensors.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002097 *
2098 * Finds the inventory items that are associated with the specified sensors.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002099 * Then gets D-Bus data for the inventory items, such as presence and VPD.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002100 *
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002101 * This data is later used to provide sensor property values in the JSON
2102 * response.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002103 *
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002104 * Finds the inventory items asynchronously. Invokes callback when the
2105 * inventory items have been obtained.
2106 *
2107 * The callback must have the following signature:
2108 * @code
2109 * callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems)
2110 * @endcode
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002111 *
2112 * @param sensorsAsyncResp Pointer to object holding response data.
2113 * @param sensorNames All sensors within the current chassis.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002114 * implements ObjectManager.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002115 * @param callback Callback to invoke when inventory items have been obtained.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002116 */
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002117template <typename Callback>
Ed Tanousd0090732022-10-04 17:22:56 -07002118static void
2119 getInventoryItems(std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
2120 const std::shared_ptr<std::set<std::string>> sensorNames,
2121 Callback&& callback)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002122{
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002123 BMCWEB_LOG_DEBUG << "getInventoryItems enter";
2124 auto getInventoryItemAssociationsCb =
Ed Tanousd0090732022-10-04 17:22:56 -07002125 [sensorsAsyncResp, callback{std::forward<Callback>(callback)}](
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002126 std::shared_ptr<std::vector<InventoryItem>> inventoryItems) {
Ed Tanous002d39b2022-05-31 08:59:27 -07002127 BMCWEB_LOG_DEBUG << "getInventoryItemAssociationsCb enter";
2128 auto getInventoryItemsConnectionsCb =
Ed Tanousd0090732022-10-04 17:22:56 -07002129 [sensorsAsyncResp, inventoryItems,
Ed Tanous002d39b2022-05-31 08:59:27 -07002130 callback{std::forward<const Callback>(callback)}](
Nan Zhoufe04d492022-06-22 17:10:41 +00002131 std::shared_ptr<std::set<std::string>> invConnections) {
Ed Tanous002d39b2022-05-31 08:59:27 -07002132 BMCWEB_LOG_DEBUG << "getInventoryItemsConnectionsCb enter";
2133 auto getInventoryItemsDataCb = [sensorsAsyncResp, inventoryItems,
2134 callback{std::move(callback)}]() {
2135 BMCWEB_LOG_DEBUG << "getInventoryItemsDataCb enter";
Gunnar Mills42cbe532019-08-15 15:26:54 -05002136
Ed Tanous002d39b2022-05-31 08:59:27 -07002137 auto getInventoryLedsCb = [sensorsAsyncResp, inventoryItems,
2138 callback{std::move(callback)}]() {
2139 BMCWEB_LOG_DEBUG << "getInventoryLedsCb enter";
2140 // Find Power Supply Attributes and get the data
2141 getPowerSupplyAttributes(sensorsAsyncResp, inventoryItems,
2142 std::move(callback));
2143 BMCWEB_LOG_DEBUG << "getInventoryLedsCb exit";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002144 };
2145
Ed Tanous002d39b2022-05-31 08:59:27 -07002146 // Find led connections and get the data
2147 getInventoryLeds(sensorsAsyncResp, inventoryItems,
2148 std::move(getInventoryLedsCb));
2149 BMCWEB_LOG_DEBUG << "getInventoryItemsDataCb exit";
2150 };
2151
2152 // Get inventory item data from connections
2153 getInventoryItemsData(sensorsAsyncResp, inventoryItems,
Ed Tanousd0090732022-10-04 17:22:56 -07002154 invConnections,
Ed Tanous002d39b2022-05-31 08:59:27 -07002155 std::move(getInventoryItemsDataCb));
2156 BMCWEB_LOG_DEBUG << "getInventoryItemsConnectionsCb exit";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002157 };
2158
Ed Tanous002d39b2022-05-31 08:59:27 -07002159 // Get connections that provide inventory item data
2160 getInventoryItemsConnections(sensorsAsyncResp, inventoryItems,
2161 std::move(getInventoryItemsConnectionsCb));
2162 BMCWEB_LOG_DEBUG << "getInventoryItemAssociationsCb exit";
2163 };
2164
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002165 // Get associations from sensors to inventory items
Ed Tanousd0090732022-10-04 17:22:56 -07002166 getInventoryItemAssociations(sensorsAsyncResp, sensorNames,
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002167 std::move(getInventoryItemAssociationsCb));
2168 BMCWEB_LOG_DEBUG << "getInventoryItems exit";
2169}
2170
2171/**
2172 * @brief Returns JSON PowerSupply object for the specified inventory item.
2173 *
2174 * Searches for a JSON PowerSupply object that matches the specified inventory
2175 * item. If one is not found, a new PowerSupply object is added to the JSON
2176 * array.
2177 *
2178 * Multiple sensors are often associated with one power supply inventory item.
2179 * As a result, multiple sensor values are stored in one JSON PowerSupply
2180 * object.
2181 *
2182 * @param powerSupplyArray JSON array containing Redfish PowerSupply objects.
2183 * @param inventoryItem Inventory item for the power supply.
2184 * @param chassisId Chassis that contains the power supply.
2185 * @return JSON PowerSupply object for the specified inventory item.
2186 */
Ed Tanous23a21a12020-07-25 04:45:05 +00002187inline nlohmann::json& getPowerSupply(nlohmann::json& powerSupplyArray,
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002188 const InventoryItem& inventoryItem,
2189 const std::string& chassisId)
2190{
2191 // Check if matching PowerSupply object already exists in JSON array
2192 for (nlohmann::json& powerSupply : powerSupplyArray)
2193 {
George Liu3e35c762023-03-08 16:56:38 +08002194 if (powerSupply["Name"] == inventoryItem.name)
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002195 {
2196 return powerSupply;
2197 }
2198 }
2199
2200 // Add new PowerSupply object to JSON array
2201 powerSupplyArray.push_back({});
2202 nlohmann::json& powerSupply = powerSupplyArray.back();
Willy Tueddfc432022-09-26 16:46:38 +00002203 boost::urls::url url = crow::utility::urlFromPieces(
2204 "redfish", "v1", "Chassis", chassisId, "Power");
2205 url.set_fragment(("/PowerSupplies"_json_pointer).to_string());
2206 powerSupply["@odata.id"] = std::move(url);
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002207 powerSupply["Name"] = boost::replace_all_copy(inventoryItem.name, "_", " ");
2208 powerSupply["Manufacturer"] = inventoryItem.manufacturer;
2209 powerSupply["Model"] = inventoryItem.model;
2210 powerSupply["PartNumber"] = inventoryItem.partNumber;
2211 powerSupply["SerialNumber"] = inventoryItem.serialNumber;
Anthony Wilsond5005492019-07-31 16:34:17 -05002212 setLedState(powerSupply, &inventoryItem);
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002213
Gunnar Mills42cbe532019-08-15 15:26:54 -05002214 if (inventoryItem.powerSupplyEfficiencyPercent >= 0)
2215 {
2216 powerSupply["EfficiencyPercent"] =
2217 inventoryItem.powerSupplyEfficiencyPercent;
2218 }
2219
2220 powerSupply["Status"]["State"] = getState(&inventoryItem);
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002221 const char* health = inventoryItem.isFunctional ? "OK" : "Critical";
2222 powerSupply["Status"]["Health"] = health;
2223
2224 return powerSupply;
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002225}
2226
2227/**
Shawn McCarneyde629b62019-03-08 10:42:51 -06002228 * @brief Gets the values of the specified sensors.
2229 *
2230 * Stores the results as JSON in the SensorsAsyncResp.
2231 *
2232 * Gets the sensor values asynchronously. Stores the results later when the
2233 * information has been obtained.
2234 *
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002235 * The sensorNames set contains all requested sensors for the current chassis.
Shawn McCarneyde629b62019-03-08 10:42:51 -06002236 *
2237 * To minimize the number of DBus calls, the DBus method
2238 * org.freedesktop.DBus.ObjectManager.GetManagedObjects() is used to get the
2239 * values of all sensors provided by a connection (service).
2240 *
2241 * The connections set contains all the connections that provide sensor values.
2242 *
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002243 * The InventoryItem vector contains D-Bus inventory items associated with the
2244 * sensors. Inventory item data is needed for some Redfish sensor properties.
2245 *
Shawn McCarneyde629b62019-03-08 10:42:51 -06002246 * @param SensorsAsyncResp Pointer to object holding response data.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002247 * @param sensorNames All requested sensors within the current chassis.
Shawn McCarneyde629b62019-03-08 10:42:51 -06002248 * @param connections Connections that provide sensor values.
Shawn McCarneyde629b62019-03-08 10:42:51 -06002249 * implements ObjectManager.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002250 * @param inventoryItems Inventory items associated with the sensors.
Shawn McCarneyde629b62019-03-08 10:42:51 -06002251 */
Ed Tanous23a21a12020-07-25 04:45:05 +00002252inline void getSensorData(
Ed Tanous81ce6092020-12-17 16:54:55 +00002253 const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
Nan Zhoufe04d492022-06-22 17:10:41 +00002254 const std::shared_ptr<std::set<std::string>>& sensorNames,
2255 const std::set<std::string>& connections,
Ed Tanousb5a76932020-09-29 16:16:58 -07002256 const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems)
Shawn McCarneyde629b62019-03-08 10:42:51 -06002257{
2258 BMCWEB_LOG_DEBUG << "getSensorData enter";
2259 // Get managed objects from all services exposing sensors
2260 for (const std::string& connection : connections)
2261 {
2262 // Response handler to process managed objects
Ed Tanous002d39b2022-05-31 08:59:27 -07002263 auto getManagedObjectsCb =
2264 [sensorsAsyncResp, sensorNames,
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08002265 inventoryItems](const boost::system::error_code& ec,
Ed Tanous02cad962022-06-30 16:50:15 -07002266 const dbus::utility::ManagedObjectType& resp) {
Shawn McCarneyde629b62019-03-08 10:42:51 -06002267 BMCWEB_LOG_DEBUG << "getManagedObjectsCb enter";
2268 if (ec)
2269 {
2270 BMCWEB_LOG_ERROR << "getManagedObjectsCb DBUS error: " << ec;
zhanghch058d1b46d2021-04-01 11:18:24 +08002271 messages::internalError(sensorsAsyncResp->asyncResp->res);
Shawn McCarneyde629b62019-03-08 10:42:51 -06002272 return;
2273 }
2274 // Go through all objects and update response with sensor data
2275 for (const auto& objDictEntry : resp)
2276 {
2277 const std::string& objPath =
2278 static_cast<const std::string&>(objDictEntry.first);
2279 BMCWEB_LOG_DEBUG << "getManagedObjectsCb parsing object "
2280 << objPath;
2281
Shawn McCarneyde629b62019-03-08 10:42:51 -06002282 std::vector<std::string> split;
2283 // Reserve space for
2284 // /xyz/openbmc_project/sensors/<name>/<subname>
2285 split.reserve(6);
Ed Tanous50ebd4a2023-01-19 19:03:17 -08002286 // NOLINTNEXTLINE
2287 bmcweb::split(split, objPath, '/');
Shawn McCarneyde629b62019-03-08 10:42:51 -06002288 if (split.size() < 6)
2289 {
2290 BMCWEB_LOG_ERROR << "Got path that isn't long enough "
2291 << objPath;
2292 continue;
2293 }
Ed Tanous50ebd4a2023-01-19 19:03:17 -08002294 // These indexes aren't intuitive, as split puts an empty
Shawn McCarneyde629b62019-03-08 10:42:51 -06002295 // string at the beginning
2296 const std::string& sensorType = split[4];
2297 const std::string& sensorName = split[5];
2298 BMCWEB_LOG_DEBUG << "sensorName " << sensorName
2299 << " sensorType " << sensorType;
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002300 if (sensorNames->find(objPath) == sensorNames->end())
Shawn McCarneyde629b62019-03-08 10:42:51 -06002301 {
Andrew Geissleraccdbb22021-11-09 15:24:45 -06002302 BMCWEB_LOG_DEBUG << sensorName << " not in sensor list ";
Shawn McCarneyde629b62019-03-08 10:42:51 -06002303 continue;
2304 }
2305
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002306 // Find inventory item (if any) associated with sensor
2307 InventoryItem* inventoryItem =
2308 findInventoryItemForSensor(inventoryItems, objPath);
2309
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002310 const std::string& sensorSchema =
Ed Tanous81ce6092020-12-17 16:54:55 +00002311 sensorsAsyncResp->chassisSubNode;
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002312
2313 nlohmann::json* sensorJson = nullptr;
2314
Nan Zhou928fefb2022-03-28 08:45:00 -07002315 if (sensorSchema == sensors::node::sensors &&
2316 !sensorsAsyncResp->efficientExpand)
Shawn McCarneyde629b62019-03-08 10:42:51 -06002317 {
Ed Tanousc1d019a2022-08-06 09:36:06 -07002318 std::string sensorTypeEscaped(sensorType);
2319 sensorTypeEscaped.erase(
2320 std::remove(sensorTypeEscaped.begin(),
2321 sensorTypeEscaped.end(), '_'),
2322 sensorTypeEscaped.end());
2323 std::string sensorId(sensorTypeEscaped);
2324 sensorId += "_";
2325 sensorId += sensorName;
2326
zhanghch058d1b46d2021-04-01 11:18:24 +08002327 sensorsAsyncResp->asyncResp->res.jsonValue["@odata.id"] =
Ed Tanousc1d019a2022-08-06 09:36:06 -07002328 crow::utility::urlFromPieces(
2329 "redfish", "v1", "Chassis",
2330 sensorsAsyncResp->chassisId,
2331 sensorsAsyncResp->chassisSubNode, sensorId);
zhanghch058d1b46d2021-04-01 11:18:24 +08002332 sensorJson = &(sensorsAsyncResp->asyncResp->res.jsonValue);
Shawn McCarneyde629b62019-03-08 10:42:51 -06002333 }
2334 else
2335 {
Ed Tanous271584a2019-07-09 16:24:22 -07002336 std::string fieldName;
Nan Zhou928fefb2022-03-28 08:45:00 -07002337 if (sensorsAsyncResp->efficientExpand)
2338 {
2339 fieldName = "Members";
2340 }
2341 else if (sensorType == "temperature")
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002342 {
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002343 fieldName = "Temperatures";
2344 }
2345 else if (sensorType == "fan" || sensorType == "fan_tach" ||
2346 sensorType == "fan_pwm")
2347 {
2348 fieldName = "Fans";
2349 }
2350 else if (sensorType == "voltage")
2351 {
2352 fieldName = "Voltages";
2353 }
2354 else if (sensorType == "power")
2355 {
Ed Tanous55f79e62022-01-25 11:26:16 -08002356 if (sensorName == "total_power")
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002357 {
2358 fieldName = "PowerControl";
2359 }
2360 else if ((inventoryItem != nullptr) &&
2361 (inventoryItem->isPowerSupply))
2362 {
2363 fieldName = "PowerSupplies";
2364 }
2365 else
2366 {
2367 // Other power sensors are in SensorCollection
2368 continue;
2369 }
2370 }
2371 else
2372 {
2373 BMCWEB_LOG_ERROR << "Unsure how to handle sensorType "
2374 << sensorType;
2375 continue;
2376 }
2377
2378 nlohmann::json& tempArray =
zhanghch058d1b46d2021-04-01 11:18:24 +08002379 sensorsAsyncResp->asyncResp->res.jsonValue[fieldName];
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002380 if (fieldName == "PowerControl")
2381 {
2382 if (tempArray.empty())
2383 {
2384 // Put multiple "sensors" into a single
2385 // PowerControl. Follows MemberId naming and
2386 // naming in power.hpp.
Ed Tanous14766872022-03-15 10:44:42 -07002387 nlohmann::json::object_t power;
Willy Tueddfc432022-09-26 16:46:38 +00002388 boost::urls::url url = crow::utility::urlFromPieces(
2389 "redfish", "v1", "Chassis",
2390 sensorsAsyncResp->chassisId,
2391 sensorsAsyncResp->chassisSubNode);
2392 url.set_fragment((""_json_pointer / fieldName / "0")
2393 .to_string());
2394 power["@odata.id"] = std::move(url);
Ed Tanous14766872022-03-15 10:44:42 -07002395 tempArray.push_back(std::move(power));
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002396 }
2397 sensorJson = &(tempArray.back());
2398 }
2399 else if (fieldName == "PowerSupplies")
2400 {
2401 if (inventoryItem != nullptr)
2402 {
2403 sensorJson =
2404 &(getPowerSupply(tempArray, *inventoryItem,
Ed Tanous81ce6092020-12-17 16:54:55 +00002405 sensorsAsyncResp->chassisId));
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002406 }
2407 }
Nan Zhou928fefb2022-03-28 08:45:00 -07002408 else if (fieldName == "Members")
2409 {
Ed Tanous677bb752022-09-15 10:52:19 -07002410 std::string sensorTypeEscaped(sensorType);
2411 sensorTypeEscaped.erase(
2412 std::remove(sensorTypeEscaped.begin(),
2413 sensorTypeEscaped.end(), '_'),
2414 sensorTypeEscaped.end());
2415 std::string sensorId(sensorTypeEscaped);
2416 sensorId += "_";
2417 sensorId += sensorName;
2418
Ed Tanous14766872022-03-15 10:44:42 -07002419 nlohmann::json::object_t member;
Ed Tanous677bb752022-09-15 10:52:19 -07002420 member["@odata.id"] = crow::utility::urlFromPieces(
2421 "redfish", "v1", "Chassis",
2422 sensorsAsyncResp->chassisId,
2423 sensorsAsyncResp->chassisSubNode, sensorId);
Ed Tanous14766872022-03-15 10:44:42 -07002424 tempArray.push_back(std::move(member));
Nan Zhou928fefb2022-03-28 08:45:00 -07002425 sensorJson = &(tempArray.back());
2426 }
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002427 else
2428 {
Ed Tanous14766872022-03-15 10:44:42 -07002429 nlohmann::json::object_t member;
Willy Tueddfc432022-09-26 16:46:38 +00002430 boost::urls::url url = crow::utility::urlFromPieces(
2431 "redfish", "v1", "Chassis",
2432 sensorsAsyncResp->chassisId,
2433 sensorsAsyncResp->chassisSubNode);
2434 url.set_fragment(
2435 (""_json_pointer / fieldName).to_string());
2436 member["@odata.id"] = std::move(url);
Ed Tanous14766872022-03-15 10:44:42 -07002437 tempArray.push_back(std::move(member));
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002438 sensorJson = &(tempArray.back());
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002439 }
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002440 }
Shawn McCarneyde629b62019-03-08 10:42:51 -06002441
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002442 if (sensorJson != nullptr)
2443 {
Ed Tanous1d7c0052022-08-09 12:32:26 -07002444 objectInterfacesToJson(sensorName, sensorType,
2445 sensorsAsyncResp->chassisSubNode,
2446 objDictEntry.second, *sensorJson,
2447 inventoryItem);
2448
2449 std::string path = "/xyz/openbmc_project/sensors/";
2450 path += sensorType;
2451 path += "/";
2452 path += sensorName;
Ed Tanousc1d019a2022-08-06 09:36:06 -07002453 sensorsAsyncResp->addMetadata(*sensorJson, path);
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002454 }
Shawn McCarneyde629b62019-03-08 10:42:51 -06002455 }
Ed Tanous81ce6092020-12-17 16:54:55 +00002456 if (sensorsAsyncResp.use_count() == 1)
James Feist8bd25cc2019-03-15 15:14:00 -07002457 {
Ed Tanous81ce6092020-12-17 16:54:55 +00002458 sortJSONResponse(sensorsAsyncResp);
Nan Zhou928fefb2022-03-28 08:45:00 -07002459 if (sensorsAsyncResp->chassisSubNode ==
2460 sensors::node::sensors &&
2461 sensorsAsyncResp->efficientExpand)
2462 {
2463 sensorsAsyncResp->asyncResp->res
2464 .jsonValue["Members@odata.count"] =
2465 sensorsAsyncResp->asyncResp->res.jsonValue["Members"]
2466 .size();
2467 }
2468 else if (sensorsAsyncResp->chassisSubNode ==
2469 sensors::node::thermal)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002470 {
Ed Tanous81ce6092020-12-17 16:54:55 +00002471 populateFanRedundancy(sensorsAsyncResp);
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002472 }
James Feist8bd25cc2019-03-15 15:14:00 -07002473 }
Shawn McCarneyde629b62019-03-08 10:42:51 -06002474 BMCWEB_LOG_DEBUG << "getManagedObjectsCb exit";
2475 };
2476
Shawn McCarneyde629b62019-03-08 10:42:51 -06002477 crow::connections::systemBus->async_method_call(
Ed Tanousd0090732022-10-04 17:22:56 -07002478 getManagedObjectsCb, connection, "/xyz/openbmc_project/sensors",
Shawn McCarneyde629b62019-03-08 10:42:51 -06002479 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
Ed Tanous23a21a12020-07-25 04:45:05 +00002480 }
Shawn McCarneyde629b62019-03-08 10:42:51 -06002481 BMCWEB_LOG_DEBUG << "getSensorData exit";
2482}
2483
Nan Zhoufe04d492022-06-22 17:10:41 +00002484inline void
2485 processSensorList(const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
2486 const std::shared_ptr<std::set<std::string>>& sensorNames)
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002487{
Nan Zhoufe04d492022-06-22 17:10:41 +00002488 auto getConnectionCb = [sensorsAsyncResp, sensorNames](
2489 const std::set<std::string>& connections) {
Ed Tanous002d39b2022-05-31 08:59:27 -07002490 BMCWEB_LOG_DEBUG << "getConnectionCb enter";
Ed Tanousd0090732022-10-04 17:22:56 -07002491 auto getInventoryItemsCb =
2492 [sensorsAsyncResp, sensorNames,
2493 connections](const std::shared_ptr<std::vector<InventoryItem>>&
2494 inventoryItems) {
2495 BMCWEB_LOG_DEBUG << "getInventoryItemsCb enter";
2496 // Get sensor data and store results in JSON
2497 getSensorData(sensorsAsyncResp, sensorNames, connections,
2498 inventoryItems);
2499 BMCWEB_LOG_DEBUG << "getInventoryItemsCb exit";
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002500 };
2501
Ed Tanousd0090732022-10-04 17:22:56 -07002502 // Get inventory items associated with sensors
2503 getInventoryItems(sensorsAsyncResp, sensorNames,
2504 std::move(getInventoryItemsCb));
2505
Ed Tanous002d39b2022-05-31 08:59:27 -07002506 BMCWEB_LOG_DEBUG << "getConnectionCb exit";
2507 };
2508
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002509 // Get set of connections that provide sensor values
Ed Tanous81ce6092020-12-17 16:54:55 +00002510 getConnections(sensorsAsyncResp, sensorNames, std::move(getConnectionCb));
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002511}
2512
Shawn McCarneyde629b62019-03-08 10:42:51 -06002513/**
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +01002514 * @brief Entry point for retrieving sensors data related to requested
2515 * chassis.
Kowalski, Kamil588c3f02018-04-03 14:55:27 +02002516 * @param SensorsAsyncResp Pointer to object holding response data
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +01002517 */
Ed Tanousb5a76932020-09-29 16:16:58 -07002518inline void
Ed Tanous81ce6092020-12-17 16:54:55 +00002519 getChassisData(const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp)
Ed Tanous1abe55e2018-09-05 08:30:59 -07002520{
2521 BMCWEB_LOG_DEBUG << "getChassisData enter";
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002522 auto getChassisCb =
Ed Tanous81ce6092020-12-17 16:54:55 +00002523 [sensorsAsyncResp](
Nan Zhoufe04d492022-06-22 17:10:41 +00002524 const std::shared_ptr<std::set<std::string>>& sensorNames) {
Ed Tanous002d39b2022-05-31 08:59:27 -07002525 BMCWEB_LOG_DEBUG << "getChassisCb enter";
2526 processSensorList(sensorsAsyncResp, sensorNames);
2527 BMCWEB_LOG_DEBUG << "getChassisCb exit";
2528 };
Nan Zhou928fefb2022-03-28 08:45:00 -07002529 // SensorCollection doesn't contain the Redundancy property
2530 if (sensorsAsyncResp->chassisSubNode != sensors::node::sensors)
2531 {
2532 sensorsAsyncResp->asyncResp->res.jsonValue["Redundancy"] =
2533 nlohmann::json::array();
2534 }
Shawn McCarney26f03892019-05-03 13:20:24 -05002535 // Get set of sensors in chassis
Ed Tanous7f1cc262022-08-09 13:33:57 -07002536 getChassis(sensorsAsyncResp->asyncResp, sensorsAsyncResp->chassisId,
2537 sensorsAsyncResp->chassisSubNode, sensorsAsyncResp->types,
2538 std::move(getChassisCb));
Ed Tanous1abe55e2018-09-05 08:30:59 -07002539 BMCWEB_LOG_DEBUG << "getChassisData exit";
Ed Tanous271584a2019-07-09 16:24:22 -07002540}
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +01002541
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302542/**
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002543 * @brief Find the requested sensorName in the list of all sensors supplied by
2544 * the chassis node
2545 *
2546 * @param sensorName The sensor name supplied in the PATCH request
2547 * @param sensorsList The list of sensors managed by the chassis node
2548 * @param sensorsModified The list of sensors that were found as a result of
2549 * repeated calls to this function
2550 */
Nan Zhoufe04d492022-06-22 17:10:41 +00002551inline bool
2552 findSensorNameUsingSensorPath(std::string_view sensorName,
Ed Tanous02cad962022-06-30 16:50:15 -07002553 const std::set<std::string>& sensorsList,
Nan Zhoufe04d492022-06-22 17:10:41 +00002554 std::set<std::string>& sensorsModified)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002555{
Nan Zhoufe04d492022-06-22 17:10:41 +00002556 for (const auto& chassisSensor : sensorsList)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002557 {
George Liu28aa8de2021-02-01 15:13:30 +08002558 sdbusplus::message::object_path path(chassisSensor);
Ed Tanousb00dcc22021-02-23 12:52:50 -08002559 std::string thisSensorName = path.filename();
George Liu28aa8de2021-02-01 15:13:30 +08002560 if (thisSensorName.empty())
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002561 {
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002562 continue;
2563 }
2564 if (thisSensorName == sensorName)
2565 {
2566 sensorsModified.emplace(chassisSensor);
2567 return true;
2568 }
2569 }
2570 return false;
2571}
2572
Ed Tanousc71d6122022-11-29 14:10:32 -08002573inline std::pair<std::string, std::string>
2574 splitSensorNameAndType(std::string_view sensorId)
2575{
2576 size_t index = sensorId.find('_');
2577 if (index == std::string::npos)
2578 {
2579 return std::make_pair<std::string, std::string>("", "");
2580 }
2581 std::string sensorType{sensorId.substr(0, index)};
2582 std::string sensorName{sensorId.substr(index + 1)};
2583 // fan_pwm and fan_tach need special handling
2584 if (sensorType == "fantach" || sensorType == "fanpwm")
2585 {
2586 sensorType.insert(3, 1, '_');
2587 }
2588 return std::make_pair(sensorType, sensorName);
2589}
2590
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002591/**
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302592 * @brief Entry point for overriding sensor values of given sensor
2593 *
zhanghch058d1b46d2021-04-01 11:18:24 +08002594 * @param sensorAsyncResp response object
Carol Wang4bb3dc32019-10-17 18:15:02 +08002595 * @param allCollections Collections extract from sensors' request patch info
jayaprakash Mutyala91e130a2020-03-04 22:26:38 +00002596 * @param chassisSubNode Chassis Node for which the query has to happen
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302597 */
Ed Tanous23a21a12020-07-25 04:45:05 +00002598inline void setSensorsOverride(
Ed Tanousb5a76932020-09-29 16:16:58 -07002599 const std::shared_ptr<SensorsAsyncResp>& sensorAsyncResp,
Carol Wang4bb3dc32019-10-17 18:15:02 +08002600 std::unordered_map<std::string, std::vector<nlohmann::json>>&
jayaprakash Mutyala397fd612020-02-06 23:33:34 +00002601 allCollections)
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302602{
jayaprakash Mutyala70d1d0a2020-01-21 23:41:36 +00002603 BMCWEB_LOG_INFO << "setSensorsOverride for subNode"
Carol Wang4bb3dc32019-10-17 18:15:02 +08002604 << sensorAsyncResp->chassisSubNode << "\n";
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302605
Ed Tanous543f4402022-01-06 13:12:53 -08002606 const char* propertyValueName = nullptr;
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302607 std::unordered_map<std::string, std::pair<double, std::string>> overrideMap;
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302608 std::string memberId;
Ed Tanous543f4402022-01-06 13:12:53 -08002609 double value = 0.0;
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302610 for (auto& collectionItems : allCollections)
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302611 {
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302612 if (collectionItems.first == "Temperatures")
2613 {
2614 propertyValueName = "ReadingCelsius";
2615 }
2616 else if (collectionItems.first == "Fans")
2617 {
2618 propertyValueName = "Reading";
2619 }
2620 else
2621 {
2622 propertyValueName = "ReadingVolts";
2623 }
2624 for (auto& item : collectionItems.second)
2625 {
zhanghch058d1b46d2021-04-01 11:18:24 +08002626 if (!json_util::readJson(item, sensorAsyncResp->asyncResp->res,
2627 "MemberId", memberId, propertyValueName,
2628 value))
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302629 {
2630 return;
2631 }
2632 overrideMap.emplace(memberId,
2633 std::make_pair(value, collectionItems.first));
2634 }
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302635 }
Carol Wang4bb3dc32019-10-17 18:15:02 +08002636
Ed Tanous002d39b2022-05-31 08:59:27 -07002637 auto getChassisSensorListCb =
2638 [sensorAsyncResp, overrideMap](
Nan Zhoufe04d492022-06-22 17:10:41 +00002639 const std::shared_ptr<std::set<std::string>>& sensorsList) {
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002640 // Match sensor names in the PATCH request to those managed by the
2641 // chassis node
Nan Zhoufe04d492022-06-22 17:10:41 +00002642 const std::shared_ptr<std::set<std::string>> sensorNames =
2643 std::make_shared<std::set<std::string>>();
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302644 for (const auto& item : overrideMap)
2645 {
2646 const auto& sensor = item.first;
Ed Tanousc71d6122022-11-29 14:10:32 -08002647 std::pair<std::string, std::string> sensorNameType =
2648 splitSensorNameAndType(sensor);
2649 if (!findSensorNameUsingSensorPath(sensorNameType.second,
2650 *sensorsList, *sensorNames))
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302651 {
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302652 BMCWEB_LOG_INFO << "Unable to find memberId " << item.first;
zhanghch058d1b46d2021-04-01 11:18:24 +08002653 messages::resourceNotFound(sensorAsyncResp->asyncResp->res,
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302654 item.second.second, item.first);
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302655 return;
2656 }
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302657 }
2658 // Get the connection to which the memberId belongs
Ed Tanous002d39b2022-05-31 08:59:27 -07002659 auto getObjectsWithConnectionCb =
Nan Zhoufe04d492022-06-22 17:10:41 +00002660 [sensorAsyncResp,
2661 overrideMap](const std::set<std::string>& /*connections*/,
2662 const std::set<std::pair<std::string, std::string>>&
2663 objectsWithConnection) {
Jayaprakash Mutyala4f277b52021-12-08 22:46:49 +00002664 if (objectsWithConnection.size() != overrideMap.size())
2665 {
2666 BMCWEB_LOG_INFO
2667 << "Unable to find all objects with proper connection "
2668 << objectsWithConnection.size() << " requested "
2669 << overrideMap.size() << "\n";
2670 messages::resourceNotFound(sensorAsyncResp->asyncResp->res,
2671 sensorAsyncResp->chassisSubNode ==
2672 sensors::node::thermal
2673 ? "Temperatures"
2674 : "Voltages",
2675 "Count");
2676 return;
2677 }
2678 for (const auto& item : objectsWithConnection)
2679 {
2680 sdbusplus::message::object_path path(item.first);
2681 std::string sensorName = path.filename();
2682 if (sensorName.empty())
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302683 {
Jayaprakash Mutyala4f277b52021-12-08 22:46:49 +00002684 messages::internalError(sensorAsyncResp->asyncResp->res);
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302685 return;
2686 }
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302687
Jayaprakash Mutyala4f277b52021-12-08 22:46:49 +00002688 const auto& iterator = overrideMap.find(sensorName);
2689 if (iterator == overrideMap.end())
2690 {
2691 BMCWEB_LOG_INFO << "Unable to find sensor object"
2692 << item.first << "\n";
2693 messages::internalError(sensorAsyncResp->asyncResp->res);
2694 return;
2695 }
2696 crow::connections::systemBus->async_method_call(
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08002697 [sensorAsyncResp](const boost::system::error_code& ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -07002698 if (ec)
2699 {
2700 if (ec.value() ==
2701 boost::system::errc::permission_denied)
Jayaprakash Mutyala4f277b52021-12-08 22:46:49 +00002702 {
Ed Tanous002d39b2022-05-31 08:59:27 -07002703 BMCWEB_LOG_WARNING
2704 << "Manufacturing mode is not Enabled...can't "
2705 "Override the sensor value. ";
Jayaprakash Mutyala4f277b52021-12-08 22:46:49 +00002706
Ed Tanous002d39b2022-05-31 08:59:27 -07002707 messages::insufficientPrivilege(
Jayaprakash Mutyala4f277b52021-12-08 22:46:49 +00002708 sensorAsyncResp->asyncResp->res);
Ed Tanous002d39b2022-05-31 08:59:27 -07002709 return;
Jayaprakash Mutyala4f277b52021-12-08 22:46:49 +00002710 }
Ed Tanous002d39b2022-05-31 08:59:27 -07002711 BMCWEB_LOG_DEBUG
2712 << "setOverrideValueStatus DBUS error: " << ec;
2713 messages::internalError(
2714 sensorAsyncResp->asyncResp->res);
2715 }
Jayaprakash Mutyala4f277b52021-12-08 22:46:49 +00002716 },
2717 item.second, item.first, "org.freedesktop.DBus.Properties",
2718 "Set", "xyz.openbmc_project.Sensor.Value", "Value",
Ed Tanous168e20c2021-12-13 14:39:53 -08002719 dbus::utility::DbusVariantType(iterator->second.first));
Jayaprakash Mutyala4f277b52021-12-08 22:46:49 +00002720 }
2721 };
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302722 // Get object with connection for the given sensor name
2723 getObjectsWithConnection(sensorAsyncResp, sensorNames,
2724 std::move(getObjectsWithConnectionCb));
2725 };
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302726 // get full sensor list for the given chassisId and cross verify the sensor.
Ed Tanous7f1cc262022-08-09 13:33:57 -07002727 getChassis(sensorAsyncResp->asyncResp, sensorAsyncResp->chassisId,
2728 sensorAsyncResp->chassisSubNode, sensorAsyncResp->types,
2729 std::move(getChassisSensorListCb));
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302730}
2731
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02002732/**
2733 * @brief Retrieves mapping of Redfish URIs to sensor value property to D-Bus
2734 * path of the sensor.
2735 *
2736 * Function builds valid Redfish response for sensor query of given chassis and
2737 * node. It then builds metadata about Redfish<->D-Bus correlations and provides
2738 * it to caller in a callback.
2739 *
2740 * @param chassis Chassis for which retrieval should be performed
2741 * @param node Node (group) of sensors. See sensors::node for supported values
2742 * @param mapComplete Callback to be called with retrieval result
2743 */
Krzysztof Grobelny021d32c2021-10-29 16:00:07 +02002744inline void retrieveUriToDbusMap(const std::string& chassis,
2745 const std::string& node,
2746 SensorsAsyncResp::DataCompleteCb&& mapComplete)
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02002747{
Ed Tanous02da7c52022-02-27 00:09:02 -08002748 decltype(sensors::paths)::const_iterator pathIt =
2749 std::find_if(sensors::paths.cbegin(), sensors::paths.cend(),
2750 [&node](auto&& val) { return val.first == node; });
2751 if (pathIt == sensors::paths.cend())
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02002752 {
2753 BMCWEB_LOG_ERROR << "Wrong node provided : " << node;
2754 mapComplete(boost::beast::http::status::bad_request, {});
2755 return;
2756 }
Krzysztof Grobelnyd51e0722021-04-16 13:15:21 +00002757
Nan Zhou72374eb2022-01-27 17:06:51 -08002758 auto asyncResp = std::make_shared<bmcweb::AsyncResp>();
Nan Zhoufe04d492022-06-22 17:10:41 +00002759 auto callback = [asyncResp, mapCompleteCb{std::move(mapComplete)}](
2760 const boost::beast::http::status status,
2761 const std::map<std::string, std::string>& uriToDbus) {
2762 mapCompleteCb(status, uriToDbus);
2763 };
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02002764
2765 auto resp = std::make_shared<SensorsAsyncResp>(
Krzysztof Grobelnyd51e0722021-04-16 13:15:21 +00002766 asyncResp, chassis, pathIt->second, node, std::move(callback));
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02002767 getChassisData(resp);
2768}
2769
Nan Zhoubacb2162022-04-06 11:28:32 -07002770namespace sensors
2771{
Nan Zhou928fefb2022-03-28 08:45:00 -07002772
Nan Zhoubacb2162022-04-06 11:28:32 -07002773inline void getChassisCallback(
Ed Tanousc1d019a2022-08-06 09:36:06 -07002774 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2775 std::string_view chassisId, std::string_view chassisSubNode,
Nan Zhoufe04d492022-06-22 17:10:41 +00002776 const std::shared_ptr<std::set<std::string>>& sensorNames)
Nan Zhoubacb2162022-04-06 11:28:32 -07002777{
Ed Tanousc1d019a2022-08-06 09:36:06 -07002778 BMCWEB_LOG_DEBUG << "getChassisCallback enter ";
Nan Zhoubacb2162022-04-06 11:28:32 -07002779
Ed Tanousc1d019a2022-08-06 09:36:06 -07002780 nlohmann::json& entriesArray = asyncResp->res.jsonValue["Members"];
2781 for (const std::string& sensor : *sensorNames)
Nan Zhoubacb2162022-04-06 11:28:32 -07002782 {
2783 BMCWEB_LOG_DEBUG << "Adding sensor: " << sensor;
2784
2785 sdbusplus::message::object_path path(sensor);
2786 std::string sensorName = path.filename();
2787 if (sensorName.empty())
2788 {
2789 BMCWEB_LOG_ERROR << "Invalid sensor path: " << sensor;
Ed Tanousc1d019a2022-08-06 09:36:06 -07002790 messages::internalError(asyncResp->res);
Nan Zhoubacb2162022-04-06 11:28:32 -07002791 return;
2792 }
Ed Tanousc1d019a2022-08-06 09:36:06 -07002793 std::string type = path.parent_path().filename();
2794 // fan_tach has an underscore in it, so remove it to "normalize" the
2795 // type in the URI
2796 type.erase(std::remove(type.begin(), type.end(), '_'), type.end());
2797
Ed Tanous14766872022-03-15 10:44:42 -07002798 nlohmann::json::object_t member;
Ed Tanousc1d019a2022-08-06 09:36:06 -07002799 std::string id = type;
2800 id += "_";
2801 id += sensorName;
2802 member["@odata.id"] = crow::utility::urlFromPieces(
2803 "redfish", "v1", "Chassis", chassisId, chassisSubNode, id);
2804
Ed Tanous14766872022-03-15 10:44:42 -07002805 entriesArray.push_back(std::move(member));
Nan Zhoubacb2162022-04-06 11:28:32 -07002806 }
2807
Ed Tanousc1d019a2022-08-06 09:36:06 -07002808 asyncResp->res.jsonValue["Members@odata.count"] = entriesArray.size();
Nan Zhoubacb2162022-04-06 11:28:32 -07002809 BMCWEB_LOG_DEBUG << "getChassisCallback exit";
2810}
Nan Zhoue6bd8462022-06-01 04:35:35 +00002811
Nan Zhoude167a62022-06-01 04:47:45 +00002812inline void
2813 handleSensorCollectionGet(App& app, const crow::Request& req,
2814 const std::shared_ptr<bmcweb::AsyncResp>& aResp,
2815 const std::string& chassisId)
2816{
2817 query_param::QueryCapabilities capabilities = {
2818 .canDelegateExpandLevel = 1,
2819 };
2820 query_param::Query delegatedQuery;
Carson Labrado3ba00072022-06-06 19:40:56 +00002821 if (!redfish::setUpRedfishRouteWithDelegation(app, req, aResp,
Nan Zhoude167a62022-06-01 04:47:45 +00002822 delegatedQuery, capabilities))
2823 {
2824 return;
2825 }
2826
2827 if (delegatedQuery.expandType != query_param::ExpandType::None)
2828 {
2829 // we perform efficient expand.
2830 auto asyncResp = std::make_shared<SensorsAsyncResp>(
2831 aResp, chassisId, sensors::dbus::sensorPaths,
2832 sensors::node::sensors,
2833 /*efficientExpand=*/true);
2834 getChassisData(asyncResp);
2835
2836 BMCWEB_LOG_DEBUG
2837 << "SensorCollection doGet exit via efficient expand handler";
2838 return;
Ed Tanous0bad3202022-06-02 13:53:59 -07002839 }
Nan Zhoude167a62022-06-01 04:47:45 +00002840
Nan Zhoude167a62022-06-01 04:47:45 +00002841 // We get all sensors as hyperlinkes in the chassis (this
2842 // implies we reply on the default query parameters handler)
Ed Tanousc1d019a2022-08-06 09:36:06 -07002843 getChassis(aResp, chassisId, sensors::node::sensors, dbus::sensorPaths,
2844 std::bind_front(sensors::getChassisCallback, aResp, chassisId,
2845 sensors::node::sensors));
2846}
Ed Tanous7f1cc262022-08-09 13:33:57 -07002847
Ed Tanousc1d019a2022-08-06 09:36:06 -07002848inline void
2849 getSensorFromDbus(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2850 const std::string& sensorPath,
2851 const ::dbus::utility::MapperGetObject& mapperResponse)
2852{
2853 if (mapperResponse.size() != 1)
2854 {
2855 messages::internalError(asyncResp->res);
2856 return;
2857 }
2858 const auto& valueIface = *mapperResponse.begin();
2859 const std::string& connectionName = valueIface.first;
2860 BMCWEB_LOG_DEBUG << "Looking up " << connectionName;
2861 BMCWEB_LOG_DEBUG << "Path " << sensorPath;
Krzysztof Grobelnyc1343bf2022-08-31 13:15:26 +02002862
2863 sdbusplus::asio::getAllProperties(
2864 *crow::connections::systemBus, connectionName, sensorPath, "",
Ed Tanousc1d019a2022-08-06 09:36:06 -07002865 [asyncResp,
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08002866 sensorPath](const boost::system::error_code& ec,
Ed Tanousc1d019a2022-08-06 09:36:06 -07002867 const ::dbus::utility::DBusPropertiesMap& valuesDict) {
2868 if (ec)
2869 {
2870 messages::internalError(asyncResp->res);
2871 return;
2872 }
2873 sdbusplus::message::object_path path(sensorPath);
2874 std::string name = path.filename();
2875 path = path.parent_path();
2876 std::string type = path.filename();
2877 objectPropertiesToJson(name, type, sensors::node::sensors, valuesDict,
2878 asyncResp->res.jsonValue, nullptr);
Krzysztof Grobelnyc1343bf2022-08-31 13:15:26 +02002879 });
Nan Zhoude167a62022-06-01 04:47:45 +00002880}
2881
Nan Zhoue6bd8462022-06-01 04:35:35 +00002882inline void handleSensorGet(App& app, const crow::Request& req,
Ed Tanousc1d019a2022-08-06 09:36:06 -07002883 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous677bb752022-09-15 10:52:19 -07002884 const std::string& chassisId,
Ed Tanousc1d019a2022-08-06 09:36:06 -07002885 const std::string& sensorId)
Nan Zhoue6bd8462022-06-01 04:35:35 +00002886{
Ed Tanousc1d019a2022-08-06 09:36:06 -07002887 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Nan Zhoue6bd8462022-06-01 04:35:35 +00002888 {
2889 return;
2890 }
Ed Tanousc71d6122022-11-29 14:10:32 -08002891 std::pair<std::string, std::string> nameType =
2892 splitSensorNameAndType(sensorId);
2893 if (nameType.first.empty() || nameType.second.empty())
Ed Tanousc1d019a2022-08-06 09:36:06 -07002894 {
2895 messages::resourceNotFound(asyncResp->res, sensorId, "Sensor");
2896 return;
2897 }
Ed Tanousc71d6122022-11-29 14:10:32 -08002898
Ed Tanous677bb752022-09-15 10:52:19 -07002899 asyncResp->res.jsonValue["@odata.id"] = crow::utility::urlFromPieces(
2900 "redfish", "v1", "Chassis", chassisId, "Sensors", sensorId);
Ed Tanousc1d019a2022-08-06 09:36:06 -07002901
Nan Zhoue6bd8462022-06-01 04:35:35 +00002902 BMCWEB_LOG_DEBUG << "Sensor doGet enter";
Nan Zhoue6bd8462022-06-01 04:35:35 +00002903
George Liu2b731192023-01-11 16:27:13 +08002904 constexpr std::array<std::string_view, 1> interfaces = {
Nan Zhoue6bd8462022-06-01 04:35:35 +00002905 "xyz.openbmc_project.Sensor.Value"};
Ed Tanousc71d6122022-11-29 14:10:32 -08002906 std::string sensorPath = "/xyz/openbmc_project/sensors/" + nameType.first +
2907 '/' + nameType.second;
Nan Zhoue6bd8462022-06-01 04:35:35 +00002908 // Get a list of all of the sensors that implement Sensor.Value
2909 // and get the path and service name associated with the sensor
George Liu2b731192023-01-11 16:27:13 +08002910 ::dbus::utility::getDbusObject(
2911 sensorPath, interfaces,
Ed Tanousc71d6122022-11-29 14:10:32 -08002912 [asyncResp,
George Liu2b731192023-01-11 16:27:13 +08002913 sensorPath](const boost::system::error_code& ec,
Ed Tanousc1d019a2022-08-06 09:36:06 -07002914 const ::dbus::utility::MapperGetObject& subtree) {
Nan Zhoue6bd8462022-06-01 04:35:35 +00002915 BMCWEB_LOG_DEBUG << "respHandler1 enter";
2916 if (ec)
2917 {
Ed Tanousc1d019a2022-08-06 09:36:06 -07002918 messages::internalError(asyncResp->res);
Nan Zhoue6bd8462022-06-01 04:35:35 +00002919 BMCWEB_LOG_ERROR << "Sensor getSensorPaths resp_handler: "
2920 << "Dbus error " << ec;
2921 return;
2922 }
Ed Tanousc1d019a2022-08-06 09:36:06 -07002923 getSensorFromDbus(asyncResp, sensorPath, subtree);
Nan Zhoue6bd8462022-06-01 04:35:35 +00002924 BMCWEB_LOG_DEBUG << "respHandler1 exit";
George Liu2b731192023-01-11 16:27:13 +08002925 });
Nan Zhoue6bd8462022-06-01 04:35:35 +00002926}
2927
Nan Zhoubacb2162022-04-06 11:28:32 -07002928} // namespace sensors
2929
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002930inline void requestRoutesSensorCollection(App& app)
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002931{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002932 BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/Sensors/")
Ed Tanoused398212021-06-09 17:05:54 -07002933 .privileges(redfish::privileges::getSensorCollection)
Ed Tanous002d39b2022-05-31 08:59:27 -07002934 .methods(boost::beast::http::verb::get)(
Nan Zhoude167a62022-06-01 04:47:45 +00002935 std::bind_front(sensors::handleSensorCollectionGet, std::ref(app)));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002936}
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002937
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002938inline void requestRoutesSensor(App& app)
2939{
2940 BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/Sensors/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07002941 .privileges(redfish::privileges::getSensor)
Ed Tanous002d39b2022-05-31 08:59:27 -07002942 .methods(boost::beast::http::verb::get)(
Nan Zhoue6bd8462022-06-01 04:35:35 +00002943 std::bind_front(sensors::handleSensorGet, std::ref(app)));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002944}
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002945
Ed Tanous1abe55e2018-09-05 08:30:59 -07002946} // namespace redfish