blob: e04eac671f1ff51ae616655de98be10498c808b8 [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";
George Liu6c3e9452023-03-03 13:55:29 +0800567 dbus::utility::getAssociationEndPoints(
568 sensorPath,
Ed Tanous7f1cc262022-08-09 13:33:57 -0700569 [asyncResp, chassisSubNode, sensorTypes,
Ed Tanousf94c4ec2022-01-06 12:44:41 -0800570 callback{std::forward<const Callback>(callback)}](
Ed Tanous271584a2019-07-09 16:24:22 -0700571 const boost::system::error_code& e,
George Liu6c3e9452023-03-03 13:55:29 +0800572 const dbus::utility::MapperEndPoints& nodeSensorList) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700573 if (e)
574 {
575 if (e.value() != EBADR)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700576 {
Ed Tanous7f1cc262022-08-09 13:33:57 -0700577 messages::internalError(asyncResp->res);
Ed Tanous002d39b2022-05-31 08:59:27 -0700578 return;
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700579 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700580 }
Nan Zhoufe04d492022-06-22 17:10:41 +0000581 const std::shared_ptr<std::set<std::string>> culledSensorList =
582 std::make_shared<std::set<std::string>>();
Ed Tanous7f1cc262022-08-09 13:33:57 -0700583 reduceSensorList(asyncResp->res, chassisSubNode, sensorTypes,
584 &nodeSensorList, culledSensorList);
585 BMCWEB_LOG_DEBUG << "Finishing with " << culledSensorList->size();
Ed Tanous002d39b2022-05-31 08:59:27 -0700586 callback(culledSensorList);
Jonathan Doman1e1e5982021-06-11 09:36:17 -0700587 });
George Liu7a1dbc42022-12-07 16:03:22 +0800588 });
Ed Tanous1abe55e2018-09-05 08:30:59 -0700589 BMCWEB_LOG_DEBUG << "getChassis exit";
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100590}
591
592/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500593 * @brief Returns the Redfish State value for the specified inventory item.
594 * @param inventoryItem D-Bus inventory item associated with a sensor.
595 * @return State value for inventory item.
James Feist34dd1792019-05-17 14:10:54 -0700596 */
Ed Tanous23a21a12020-07-25 04:45:05 +0000597inline std::string getState(const InventoryItem* inventoryItem)
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500598{
599 if ((inventoryItem != nullptr) && !(inventoryItem->isPresent))
600 {
601 return "Absent";
602 }
James Feist34dd1792019-05-17 14:10:54 -0700603
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500604 return "Enabled";
605}
606
607/**
608 * @brief Returns the Redfish Health value for the specified sensor.
609 * @param sensorJson Sensor JSON object.
Ed Tanous1d7c0052022-08-09 12:32:26 -0700610 * @param valuesDict Map of all sensor DBus values.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500611 * @param inventoryItem D-Bus inventory item associated with the sensor. Will
612 * be nullptr if no associated inventory item was found.
613 * @return Health value for sensor.
614 */
Ed Tanous1d7c0052022-08-09 12:32:26 -0700615inline std::string getHealth(nlohmann::json& sensorJson,
616 const dbus::utility::DBusPropertiesMap& valuesDict,
617 const InventoryItem* inventoryItem)
James Feist34dd1792019-05-17 14:10:54 -0700618{
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500619 // Get current health value (if any) in the sensor JSON object. Some JSON
620 // objects contain multiple sensors (such as PowerSupplies). We want to set
621 // the overall health to be the most severe of any of the sensors.
622 std::string currentHealth;
623 auto statusIt = sensorJson.find("Status");
624 if (statusIt != sensorJson.end())
625 {
626 auto healthIt = statusIt->find("Health");
627 if (healthIt != statusIt->end())
628 {
629 std::string* health = healthIt->get_ptr<std::string*>();
630 if (health != nullptr)
631 {
632 currentHealth = *health;
633 }
634 }
635 }
636
637 // If current health in JSON object is already Critical, return that. This
638 // should override the sensor health, which might be less severe.
639 if (currentHealth == "Critical")
640 {
641 return "Critical";
642 }
643
Krzysztof Grobelnyc1343bf2022-08-31 13:15:26 +0200644 const bool* criticalAlarmHigh = nullptr;
645 const bool* criticalAlarmLow = nullptr;
646 const bool* warningAlarmHigh = nullptr;
647 const bool* warningAlarmLow = nullptr;
Ed Tanous711ac7a2021-12-20 09:34:41 -0800648
Krzysztof Grobelnyc1343bf2022-08-31 13:15:26 +0200649 const bool success = sdbusplus::unpackPropertiesNoThrow(
650 dbus_utils::UnpackErrorPrinter(), valuesDict, "CriticalAlarmHigh",
651 criticalAlarmHigh, "CriticalAlarmLow", criticalAlarmLow,
652 "WarningAlarmHigh", warningAlarmHigh, "WarningAlarmLow",
653 warningAlarmLow);
654
655 if (success)
James Feist34dd1792019-05-17 14:10:54 -0700656 {
Krzysztof Grobelnyc1343bf2022-08-31 13:15:26 +0200657 // Check if sensor has critical threshold alarm
658 if ((criticalAlarmHigh != nullptr && *criticalAlarmHigh) ||
659 (criticalAlarmLow != nullptr && *criticalAlarmLow))
James Feist34dd1792019-05-17 14:10:54 -0700660 {
Krzysztof Grobelnyc1343bf2022-08-31 13:15:26 +0200661 return "Critical";
James Feist34dd1792019-05-17 14:10:54 -0700662 }
663 }
664
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500665 // Check if associated inventory item is not functional
666 if ((inventoryItem != nullptr) && !(inventoryItem->isFunctional))
667 {
668 return "Critical";
669 }
670
Krzysztof Grobelnyc1343bf2022-08-31 13:15:26 +0200671 // If current health in JSON object is already Warning, return that. This
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500672 // should override the sensor status, which might be less severe.
673 if (currentHealth == "Warning")
674 {
675 return "Warning";
676 }
677
Krzysztof Grobelnyc1343bf2022-08-31 13:15:26 +0200678 if (success)
James Feist34dd1792019-05-17 14:10:54 -0700679 {
Krzysztof Grobelnyc1343bf2022-08-31 13:15:26 +0200680 // Check if sensor has warning threshold alarm
681 if ((warningAlarmHigh != nullptr && *warningAlarmHigh) ||
682 (warningAlarmLow != nullptr && *warningAlarmLow))
James Feist34dd1792019-05-17 14:10:54 -0700683 {
Krzysztof Grobelnyc1343bf2022-08-31 13:15:26 +0200684 return "Warning";
James Feist34dd1792019-05-17 14:10:54 -0700685 }
686 }
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500687
James Feist34dd1792019-05-17 14:10:54 -0700688 return "OK";
689}
690
Ed Tanous23a21a12020-07-25 04:45:05 +0000691inline void setLedState(nlohmann::json& sensorJson,
Anthony Wilsond5005492019-07-31 16:34:17 -0500692 const InventoryItem* inventoryItem)
693{
694 if (inventoryItem != nullptr && !inventoryItem->ledObjectPath.empty())
695 {
696 switch (inventoryItem->ledState)
697 {
698 case LedState::OFF:
699 sensorJson["IndicatorLED"] = "Off";
700 break;
701 case LedState::ON:
702 sensorJson["IndicatorLED"] = "Lit";
703 break;
704 case LedState::BLINK:
705 sensorJson["IndicatorLED"] = "Blinking";
706 break;
Ed Tanous23a21a12020-07-25 04:45:05 +0000707 case LedState::UNKNOWN:
Anthony Wilsond5005492019-07-31 16:34:17 -0500708 break;
709 }
710 }
711}
712
James Feist34dd1792019-05-17 14:10:54 -0700713/**
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100714 * @brief Builds a json sensor representation of a sensor.
715 * @param sensorName The name of the sensor to be built
Gunnar Mills274fad52018-06-13 15:45:36 -0500716 * @param sensorType The type (temperature, fan_tach, etc) of the sensor to
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100717 * build
Ed Tanous1d7c0052022-08-09 12:32:26 -0700718 * @param chassisSubNode The subnode (thermal, sensor, ect) of the sensor
719 * @param propertiesDict A dictionary of the properties to build the sensor
720 * from.
721 * @param sensorJson The json object to fill
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500722 * @param inventoryItem D-Bus inventory item associated with the sensor. Will
723 * be nullptr if no associated inventory item was found.
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100724 */
Ed Tanous1d7c0052022-08-09 12:32:26 -0700725inline void objectPropertiesToJson(
726 std::string_view sensorName, std::string_view sensorType,
727 std::string_view chassisSubNode,
728 const dbus::utility::DBusPropertiesMap& propertiesDict,
Ed Tanous81ce6092020-12-17 16:54:55 +0000729 nlohmann::json& sensorJson, InventoryItem* inventoryItem)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700730{
Ed Tanous1d7c0052022-08-09 12:32:26 -0700731 if (chassisSubNode == sensors::node::sensors)
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500732 {
Ed Tanousc71d6122022-11-29 14:10:32 -0800733 std::string subNodeEscaped(sensorType);
Ed Tanousc1d019a2022-08-06 09:36:06 -0700734 subNodeEscaped.erase(
735 std::remove(subNodeEscaped.begin(), subNodeEscaped.end(), '_'),
736 subNodeEscaped.end());
737
738 // For sensors in SensorCollection we set Id instead of MemberId,
739 // including power sensors.
740 subNodeEscaped += '_';
741 subNodeEscaped += sensorName;
742 sensorJson["Id"] = std::move(subNodeEscaped);
743
Ed Tanous1d7c0052022-08-09 12:32:26 -0700744 std::string sensorNameEs(sensorName);
745 std::replace(sensorNameEs.begin(), sensorNameEs.end(), '_', ' ');
746 sensorJson["Name"] = std::move(sensorNameEs);
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500747 }
748 else if (sensorType != "power")
749 {
750 // Set MemberId and Name for non-power sensors. For PowerSupplies and
751 // PowerControl, those properties have more general values because
752 // multiple sensors can be stored in the same JSON object.
Ed Tanous1d7c0052022-08-09 12:32:26 -0700753 std::string sensorNameEs(sensorName);
754 std::replace(sensorNameEs.begin(), sensorNameEs.end(), '_', ' ');
755 sensorJson["Name"] = std::move(sensorNameEs);
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500756 }
Ed Tanouse742b6c2019-05-03 15:06:53 -0700757
Ed Tanous81ce6092020-12-17 16:54:55 +0000758 sensorJson["Status"]["State"] = getState(inventoryItem);
Patrick Williams89492a12023-05-10 07:51:34 -0500759 sensorJson["Status"]["Health"] = getHealth(sensorJson, propertiesDict,
760 inventoryItem);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700761
762 // Parameter to set to override the type we get from dbus, and force it to
763 // int, regardless of what is available. This is used for schemas like fan,
764 // that require integers, not floats.
765 bool forceToInt = false;
766
Anthony Wilson3929aca2019-07-19 15:42:33 -0500767 nlohmann::json::json_pointer unit("/Reading");
Ed Tanous1d7c0052022-08-09 12:32:26 -0700768 if (chassisSubNode == sensors::node::sensors)
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500769 {
Shounak Mitra2a4ba192022-06-01 23:34:12 +0000770 sensorJson["@odata.type"] = "#Sensor.v1_2_0.Sensor";
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000771
Ed Tanous0ec8b832022-03-14 14:56:47 -0700772 sensor::ReadingType readingType = sensors::toReadingType(sensorType);
773 if (readingType == sensor::ReadingType::Invalid)
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500774 {
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000775 BMCWEB_LOG_ERROR << "Redfish cannot map reading type for "
776 << sensorType;
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500777 }
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000778 else
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500779 {
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000780 sensorJson["ReadingType"] = readingType;
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500781 }
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000782
Ed Tanous1d7c0052022-08-09 12:32:26 -0700783 std::string_view readingUnits = sensors::toReadingUnits(sensorType);
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000784 if (readingUnits.empty())
Adrian Ambrożewiczf8ede152020-06-02 13:26:33 +0200785 {
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000786 BMCWEB_LOG_ERROR << "Redfish cannot map reading unit for "
787 << sensorType;
788 }
789 else
790 {
791 sensorJson["ReadingUnits"] = readingUnits;
Adrian Ambrożewiczf8ede152020-06-02 13:26:33 +0200792 }
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500793 }
794 else if (sensorType == "temperature")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700795 {
Anthony Wilson3929aca2019-07-19 15:42:33 -0500796 unit = "/ReadingCelsius"_json_pointer;
Ed Tanous81ce6092020-12-17 16:54:55 +0000797 sensorJson["@odata.type"] = "#Thermal.v1_3_0.Temperature";
Ed Tanous1abe55e2018-09-05 08:30:59 -0700798 // TODO(ed) Documentation says that path should be type fan_tach,
799 // implementation seems to implement fan
800 }
801 else if (sensorType == "fan" || sensorType == "fan_tach")
802 {
Anthony Wilson3929aca2019-07-19 15:42:33 -0500803 unit = "/Reading"_json_pointer;
Ed Tanous81ce6092020-12-17 16:54:55 +0000804 sensorJson["ReadingUnits"] = "RPM";
805 sensorJson["@odata.type"] = "#Thermal.v1_3_0.Fan";
806 setLedState(sensorJson, inventoryItem);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700807 forceToInt = true;
808 }
Ed Tanous6f6d0d32018-10-12 11:16:43 -0700809 else if (sensorType == "fan_pwm")
810 {
Anthony Wilson3929aca2019-07-19 15:42:33 -0500811 unit = "/Reading"_json_pointer;
Ed Tanous81ce6092020-12-17 16:54:55 +0000812 sensorJson["ReadingUnits"] = "Percent";
813 sensorJson["@odata.type"] = "#Thermal.v1_3_0.Fan";
814 setLedState(sensorJson, inventoryItem);
Ed Tanous6f6d0d32018-10-12 11:16:43 -0700815 forceToInt = true;
816 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700817 else if (sensorType == "voltage")
818 {
Anthony Wilson3929aca2019-07-19 15:42:33 -0500819 unit = "/ReadingVolts"_json_pointer;
Ed Tanous81ce6092020-12-17 16:54:55 +0000820 sensorJson["@odata.type"] = "#Power.v1_0_0.Voltage";
Ed Tanous1abe55e2018-09-05 08:30:59 -0700821 }
Ed Tanous2474adf2018-09-05 16:31:16 -0700822 else if (sensorType == "power")
823 {
Ed Tanous1d7c0052022-08-09 12:32:26 -0700824 if (boost::iequals(sensorName, "total_power"))
Eddie James028f7eb2019-05-17 21:24:36 +0000825 {
Ed Tanous81ce6092020-12-17 16:54:55 +0000826 sensorJson["@odata.type"] = "#Power.v1_0_0.PowerControl";
Gunnar Mills7ab06f42019-07-02 13:07:16 -0500827 // Put multiple "sensors" into a single PowerControl, so have
828 // generic names for MemberId and Name. Follows Redfish mockup.
Ed Tanous81ce6092020-12-17 16:54:55 +0000829 sensorJson["MemberId"] = "0";
830 sensorJson["Name"] = "Chassis Power Control";
Anthony Wilson3929aca2019-07-19 15:42:33 -0500831 unit = "/PowerConsumedWatts"_json_pointer;
Eddie James028f7eb2019-05-17 21:24:36 +0000832 }
Ed Tanous1d7c0052022-08-09 12:32:26 -0700833 else if (boost::ifind_first(sensorName, "input").empty())
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700834 {
Anthony Wilson3929aca2019-07-19 15:42:33 -0500835 unit = "/PowerInputWatts"_json_pointer;
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700836 }
837 else
838 {
Anthony Wilson3929aca2019-07-19 15:42:33 -0500839 unit = "/PowerOutputWatts"_json_pointer;
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700840 }
Ed Tanous2474adf2018-09-05 16:31:16 -0700841 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700842 else
843 {
844 BMCWEB_LOG_ERROR << "Redfish cannot map object type for " << sensorName;
845 return;
846 }
847 // Map of dbus interface name, dbus property name and redfish property_name
Anthony Wilson3929aca2019-07-19 15:42:33 -0500848 std::vector<
849 std::tuple<const char*, const char*, nlohmann::json::json_pointer>>
850 properties;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700851 properties.reserve(7);
852
853 properties.emplace_back("xyz.openbmc_project.Sensor.Value", "Value", unit);
Shawn McCarneyde629b62019-03-08 10:42:51 -0600854
Ed Tanous1d7c0052022-08-09 12:32:26 -0700855 if (chassisSubNode == sensors::node::sensors)
Anthony Wilson3929aca2019-07-19 15:42:33 -0500856 {
857 properties.emplace_back(
858 "xyz.openbmc_project.Sensor.Threshold.Warning", "WarningHigh",
859 "/Thresholds/UpperCaution/Reading"_json_pointer);
860 properties.emplace_back(
861 "xyz.openbmc_project.Sensor.Threshold.Warning", "WarningLow",
862 "/Thresholds/LowerCaution/Reading"_json_pointer);
863 properties.emplace_back(
864 "xyz.openbmc_project.Sensor.Threshold.Critical", "CriticalHigh",
865 "/Thresholds/UpperCritical/Reading"_json_pointer);
866 properties.emplace_back(
867 "xyz.openbmc_project.Sensor.Threshold.Critical", "CriticalLow",
868 "/Thresholds/LowerCritical/Reading"_json_pointer);
869 }
870 else if (sensorType != "power")
Shawn McCarneyde629b62019-03-08 10:42:51 -0600871 {
872 properties.emplace_back("xyz.openbmc_project.Sensor.Threshold.Warning",
Anthony Wilson3929aca2019-07-19 15:42:33 -0500873 "WarningHigh",
874 "/UpperThresholdNonCritical"_json_pointer);
Shawn McCarneyde629b62019-03-08 10:42:51 -0600875 properties.emplace_back("xyz.openbmc_project.Sensor.Threshold.Warning",
Anthony Wilson3929aca2019-07-19 15:42:33 -0500876 "WarningLow",
877 "/LowerThresholdNonCritical"_json_pointer);
Shawn McCarneyde629b62019-03-08 10:42:51 -0600878 properties.emplace_back("xyz.openbmc_project.Sensor.Threshold.Critical",
Anthony Wilson3929aca2019-07-19 15:42:33 -0500879 "CriticalHigh",
880 "/UpperThresholdCritical"_json_pointer);
Shawn McCarneyde629b62019-03-08 10:42:51 -0600881 properties.emplace_back("xyz.openbmc_project.Sensor.Threshold.Critical",
Anthony Wilson3929aca2019-07-19 15:42:33 -0500882 "CriticalLow",
883 "/LowerThresholdCritical"_json_pointer);
Shawn McCarneyde629b62019-03-08 10:42:51 -0600884 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700885
Ed Tanous2474adf2018-09-05 16:31:16 -0700886 // TODO Need to get UpperThresholdFatal and LowerThresholdFatal
887
Ed Tanous1d7c0052022-08-09 12:32:26 -0700888 if (chassisSubNode == sensors::node::sensors)
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500889 {
890 properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MinValue",
Anthony Wilson3929aca2019-07-19 15:42:33 -0500891 "/ReadingRangeMin"_json_pointer);
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500892 properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MaxValue",
Anthony Wilson3929aca2019-07-19 15:42:33 -0500893 "/ReadingRangeMax"_json_pointer);
George Liu51c35a82022-10-13 20:22:14 +0800894 properties.emplace_back("xyz.openbmc_project.Sensor.Accuracy",
895 "Accuracy", "/Accuracy"_json_pointer);
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500896 }
897 else if (sensorType == "temperature")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700898 {
899 properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MinValue",
Anthony Wilson3929aca2019-07-19 15:42:33 -0500900 "/MinReadingRangeTemp"_json_pointer);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700901 properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MaxValue",
Anthony Wilson3929aca2019-07-19 15:42:33 -0500902 "/MaxReadingRangeTemp"_json_pointer);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700903 }
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500904 else if (sensorType != "power")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700905 {
906 properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MinValue",
Anthony Wilson3929aca2019-07-19 15:42:33 -0500907 "/MinReadingRange"_json_pointer);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700908 properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MaxValue",
Anthony Wilson3929aca2019-07-19 15:42:33 -0500909 "/MaxReadingRange"_json_pointer);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700910 }
911
Anthony Wilson3929aca2019-07-19 15:42:33 -0500912 for (const std::tuple<const char*, const char*,
913 nlohmann::json::json_pointer>& p : properties)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700914 {
Ed Tanous1d7c0052022-08-09 12:32:26 -0700915 for (const auto& [valueName, valueVariant] : propertiesDict)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700916 {
Ed Tanous1d7c0052022-08-09 12:32:26 -0700917 if (valueName != std::get<1>(p))
Ed Tanous1abe55e2018-09-05 08:30:59 -0700918 {
Ed Tanous711ac7a2021-12-20 09:34:41 -0800919 continue;
920 }
Ed Tanous1d7c0052022-08-09 12:32:26 -0700921
922 // The property we want to set may be nested json, so use
923 // a json_pointer for easy indexing into the json structure.
924 const nlohmann::json::json_pointer& key = std::get<2>(p);
925
Ed Tanous1d7c0052022-08-09 12:32:26 -0700926 const double* doubleValue = std::get_if<double>(&valueVariant);
Ed Tanous40e4f382022-08-09 18:42:51 -0700927 if (doubleValue == nullptr)
Ed Tanous711ac7a2021-12-20 09:34:41 -0800928 {
Ed Tanous40e4f382022-08-09 18:42:51 -0700929 BMCWEB_LOG_ERROR << "Got value interface that wasn't double";
Ed Tanous1d7c0052022-08-09 12:32:26 -0700930 continue;
931 }
Ed Tanous1d7c0052022-08-09 12:32:26 -0700932 if (forceToInt)
933 {
Ed Tanous40e4f382022-08-09 18:42:51 -0700934 sensorJson[key] = static_cast<int64_t>(*doubleValue);
Ed Tanous1d7c0052022-08-09 12:32:26 -0700935 }
936 else
937 {
Ed Tanous40e4f382022-08-09 18:42:51 -0700938 sensorJson[key] = *doubleValue;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700939 }
940 }
941 }
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100942}
943
Ed Tanous1d7c0052022-08-09 12:32:26 -0700944/**
945 * @brief Builds a json sensor representation of a sensor.
946 * @param sensorName The name of the sensor to be built
947 * @param sensorType The type (temperature, fan_tach, etc) of the sensor to
948 * build
949 * @param chassisSubNode The subnode (thermal, sensor, ect) of the sensor
950 * @param interfacesDict A dictionary of the interfaces and properties of said
951 * interfaces to be built from
952 * @param sensorJson The json object to fill
953 * @param inventoryItem D-Bus inventory item associated with the sensor. Will
954 * be nullptr if no associated inventory item was found.
955 */
956inline void objectInterfacesToJson(
957 const std::string& sensorName, const std::string& sensorType,
958 const std::string& chassisSubNode,
959 const dbus::utility::DBusInteracesMap& interfacesDict,
960 nlohmann::json& sensorJson, InventoryItem* inventoryItem)
961{
Ed Tanous1d7c0052022-08-09 12:32:26 -0700962 for (const auto& [interface, valuesDict] : interfacesDict)
963 {
964 objectPropertiesToJson(sensorName, sensorType, chassisSubNode,
965 valuesDict, sensorJson, inventoryItem);
966 }
Ed Tanousc1d019a2022-08-06 09:36:06 -0700967 BMCWEB_LOG_DEBUG << "Added sensor " << sensorName;
Ed Tanous1d7c0052022-08-09 12:32:26 -0700968}
969
Ed Tanousb5a76932020-09-29 16:16:58 -0700970inline void populateFanRedundancy(
971 const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp)
James Feist8bd25cc2019-03-15 15:14:00 -0700972{
George Liue99073f2022-12-09 11:06:16 +0800973 constexpr std::array<std::string_view, 1> interfaces = {
974 "xyz.openbmc_project.Control.FanRedundancy"};
975 dbus::utility::getSubTree(
976 "/xyz/openbmc_project/control", 2, interfaces,
Ed Tanousb9d36b42022-02-26 21:42:46 -0800977 [sensorsAsyncResp](
George Liue99073f2022-12-09 11:06:16 +0800978 const boost::system::error_code& ec,
Ed Tanousb9d36b42022-02-26 21:42:46 -0800979 const dbus::utility::MapperGetSubTreeResponse& resp) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700980 if (ec)
981 {
982 return; // don't have to have this interface
983 }
George Liu6c3e9452023-03-03 13:55:29 +0800984 for (const std::pair<std::string, dbus::utility::MapperServiceMap>&
Ed Tanous002d39b2022-05-31 08:59:27 -0700985 pathPair : resp)
986 {
987 const std::string& path = pathPair.first;
George Liu6c3e9452023-03-03 13:55:29 +0800988 const dbus::utility::MapperServiceMap& objDict = pathPair.second;
Ed Tanous002d39b2022-05-31 08:59:27 -0700989 if (objDict.empty())
James Feist8bd25cc2019-03-15 15:14:00 -0700990 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700991 continue; // this should be impossible
James Feist8bd25cc2019-03-15 15:14:00 -0700992 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700993
994 const std::string& owner = objDict.begin()->first;
George Liu6c3e9452023-03-03 13:55:29 +0800995 dbus::utility::getAssociationEndPoints(
996 path + "/chassis",
997 [path, owner, sensorsAsyncResp](
998 const boost::system::error_code& e,
999 const dbus::utility::MapperEndPoints& endpoints) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001000 if (e)
James Feist8bd25cc2019-03-15 15:14:00 -07001001 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001002 return; // if they don't have an association we
1003 // can't tell what chassis is
James Feist8bd25cc2019-03-15 15:14:00 -07001004 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001005 auto found =
1006 std::find_if(endpoints.begin(), endpoints.end(),
1007 [sensorsAsyncResp](const std::string& entry) {
1008 return entry.find(sensorsAsyncResp->chassisId) !=
1009 std::string::npos;
1010 });
James Feist8bd25cc2019-03-15 15:14:00 -07001011
Ed Tanous002d39b2022-05-31 08:59:27 -07001012 if (found == endpoints.end())
1013 {
1014 return;
1015 }
Krzysztof Grobelny86d89ed2022-08-29 14:49:20 +02001016 sdbusplus::asio::getAllProperties(
1017 *crow::connections::systemBus, owner, path,
1018 "xyz.openbmc_project.Control.FanRedundancy",
Ed Tanous002d39b2022-05-31 08:59:27 -07001019 [path, sensorsAsyncResp](
1020 const boost::system::error_code& err,
Krzysztof Grobelny86d89ed2022-08-29 14:49:20 +02001021 const dbus::utility::DBusPropertiesMap& ret) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001022 if (err)
1023 {
1024 return; // don't have to have this
1025 // interface
1026 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001027
Krzysztof Grobelny86d89ed2022-08-29 14:49:20 +02001028 const uint8_t* allowedFailures = nullptr;
1029 const std::vector<std::string>* collection = nullptr;
1030 const std::string* status = nullptr;
1031
1032 const bool success = sdbusplus::unpackPropertiesNoThrow(
1033 dbus_utils::UnpackErrorPrinter(), ret,
1034 "AllowedFailures", allowedFailures, "Collection",
1035 collection, "Status", status);
1036
1037 if (!success)
1038 {
1039 messages::internalError(
1040 sensorsAsyncResp->asyncResp->res);
1041 return;
1042 }
1043
1044 if (allowedFailures == nullptr || collection == nullptr ||
1045 status == nullptr)
Ed Tanous002d39b2022-05-31 08:59:27 -07001046 {
1047 BMCWEB_LOG_ERROR << "Invalid redundancy interface";
1048 messages::internalError(
1049 sensorsAsyncResp->asyncResp->res);
1050 return;
1051 }
1052
Ed Tanous002d39b2022-05-31 08:59:27 -07001053 sdbusplus::message::object_path objectPath(path);
1054 std::string name = objectPath.filename();
1055 if (name.empty())
1056 {
1057 // this should be impossible
1058 messages::internalError(
1059 sensorsAsyncResp->asyncResp->res);
1060 return;
1061 }
1062 std::replace(name.begin(), name.end(), '_', ' ');
1063
1064 std::string health;
1065
Ed Tanous11ba3972022-07-11 09:50:41 -07001066 if (status->ends_with("Full"))
Ed Tanous002d39b2022-05-31 08:59:27 -07001067 {
1068 health = "OK";
1069 }
Ed Tanous11ba3972022-07-11 09:50:41 -07001070 else if (status->ends_with("Degraded"))
Ed Tanous002d39b2022-05-31 08:59:27 -07001071 {
1072 health = "Warning";
1073 }
1074 else
1075 {
1076 health = "Critical";
1077 }
1078 nlohmann::json::array_t redfishCollection;
1079 const auto& fanRedfish =
1080 sensorsAsyncResp->asyncResp->res.jsonValue["Fans"];
1081 for (const std::string& item : *collection)
1082 {
Ed Tanous8a592812022-06-04 09:06:59 -07001083 sdbusplus::message::object_path itemPath(item);
1084 std::string itemName = itemPath.filename();
Ed Tanous002d39b2022-05-31 08:59:27 -07001085 if (itemName.empty())
James Feist8bd25cc2019-03-15 15:14:00 -07001086 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001087 continue;
James Feist8bd25cc2019-03-15 15:14:00 -07001088 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001089 /*
1090 todo(ed): merge patch that fixes the names
1091 std::replace(itemName.begin(),
1092 itemName.end(), '_', ' ');*/
1093 auto schemaItem =
1094 std::find_if(fanRedfish.begin(), fanRedfish.end(),
1095 [itemName](const nlohmann::json& fan) {
George Liu3e35c762023-03-08 16:56:38 +08001096 return fan["Name"] == itemName;
James Feist8bd25cc2019-03-15 15:14:00 -07001097 });
Ed Tanous002d39b2022-05-31 08:59:27 -07001098 if (schemaItem != fanRedfish.end())
James Feist8bd25cc2019-03-15 15:14:00 -07001099 {
Ed Tanous8a592812022-06-04 09:06:59 -07001100 nlohmann::json::object_t collectionId;
1101 collectionId["@odata.id"] =
Ed Tanous002d39b2022-05-31 08:59:27 -07001102 (*schemaItem)["@odata.id"];
1103 redfishCollection.emplace_back(
Ed Tanous8a592812022-06-04 09:06:59 -07001104 std::move(collectionId));
Ed Tanous002d39b2022-05-31 08:59:27 -07001105 }
1106 else
1107 {
1108 BMCWEB_LOG_ERROR << "failed to find fan in schema";
1109 messages::internalError(
1110 sensorsAsyncResp->asyncResp->res);
James Feist8bd25cc2019-03-15 15:14:00 -07001111 return;
1112 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001113 }
James Feist8bd25cc2019-03-15 15:14:00 -07001114
Patrick Williams89492a12023-05-10 07:51:34 -05001115 size_t minNumNeeded = collection->empty()
1116 ? 0
1117 : collection->size() -
1118 *allowedFailures;
Ed Tanous002d39b2022-05-31 08:59:27 -07001119 nlohmann::json& jResp = sensorsAsyncResp->asyncResp->res
1120 .jsonValue["Redundancy"];
James Feist8bd25cc2019-03-15 15:14:00 -07001121
Ed Tanous002d39b2022-05-31 08:59:27 -07001122 nlohmann::json::object_t redundancy;
Willy Tueddfc432022-09-26 16:46:38 +00001123 boost::urls::url url = crow::utility::urlFromPieces(
1124 "redfish", "v1", "Chassis", sensorsAsyncResp->chassisId,
1125 sensorsAsyncResp->chassisSubNode);
1126 url.set_fragment(("/Redundancy"_json_pointer / jResp.size())
1127 .to_string());
1128 redundancy["@odata.id"] = std::move(url);
Ed Tanous002d39b2022-05-31 08:59:27 -07001129 redundancy["@odata.type"] = "#Redundancy.v1_3_2.Redundancy";
1130 redundancy["MinNumNeeded"] = minNumNeeded;
Ed Tanous002d39b2022-05-31 08:59:27 -07001131 redundancy["Mode"] = "N+m";
1132 redundancy["Name"] = name;
1133 redundancy["RedundancySet"] = redfishCollection;
1134 redundancy["Status"]["Health"] = health;
1135 redundancy["Status"]["State"] = "Enabled";
James Feist8bd25cc2019-03-15 15:14:00 -07001136
Ed Tanous002d39b2022-05-31 08:59:27 -07001137 jResp.push_back(std::move(redundancy));
Krzysztof Grobelny86d89ed2022-08-29 14:49:20 +02001138 });
Ed Tanous002d39b2022-05-31 08:59:27 -07001139 });
1140 }
George Liue99073f2022-12-09 11:06:16 +08001141 });
James Feist8bd25cc2019-03-15 15:14:00 -07001142}
1143
Ed Tanousb5a76932020-09-29 16:16:58 -07001144inline void
Ed Tanous81ce6092020-12-17 16:54:55 +00001145 sortJSONResponse(const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07001146{
zhanghch058d1b46d2021-04-01 11:18:24 +08001147 nlohmann::json& response = sensorsAsyncResp->asyncResp->res.jsonValue;
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07001148 std::array<std::string, 2> sensorHeaders{"Temperatures", "Fans"};
Ed Tanous81ce6092020-12-17 16:54:55 +00001149 if (sensorsAsyncResp->chassisSubNode == sensors::node::power)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07001150 {
1151 sensorHeaders = {"Voltages", "PowerSupplies"};
1152 }
1153 for (const std::string& sensorGroup : sensorHeaders)
1154 {
1155 nlohmann::json::iterator entry = response.find(sensorGroup);
1156 if (entry != response.end())
1157 {
1158 std::sort(entry->begin(), entry->end(),
Ed Tanous02cad962022-06-30 16:50:15 -07001159 [](const nlohmann::json& c1, const nlohmann::json& c2) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001160 return c1["Name"] < c2["Name"];
1161 });
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07001162
1163 // add the index counts to the end of each entry
1164 size_t count = 0;
1165 for (nlohmann::json& sensorJson : *entry)
1166 {
1167 nlohmann::json::iterator odata = sensorJson.find("@odata.id");
1168 if (odata == sensorJson.end())
1169 {
1170 continue;
1171 }
1172 std::string* value = odata->get_ptr<std::string*>();
1173 if (value != nullptr)
1174 {
Willy Tueddfc432022-09-26 16:46:38 +00001175 *value += "/" + std::to_string(count);
George Liu3e35c762023-03-08 16:56:38 +08001176 sensorJson["MemberId"] = std::to_string(count);
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07001177 count++;
Ed Tanous81ce6092020-12-17 16:54:55 +00001178 sensorsAsyncResp->updateUri(sensorJson["Name"], *value);
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07001179 }
1180 }
1181 }
1182 }
1183}
1184
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +01001185/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001186 * @brief Finds the inventory item with the specified object path.
1187 * @param inventoryItems D-Bus inventory items associated with sensors.
1188 * @param invItemObjPath D-Bus object path of inventory item.
1189 * @return Inventory item within vector, or nullptr if no match found.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001190 */
Ed Tanous23a21a12020-07-25 04:45:05 +00001191inline InventoryItem* findInventoryItem(
Ed Tanousb5a76932020-09-29 16:16:58 -07001192 const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems,
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001193 const std::string& invItemObjPath)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001194{
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001195 for (InventoryItem& inventoryItem : *inventoryItems)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001196 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001197 if (inventoryItem.objectPath == invItemObjPath)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001198 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001199 return &inventoryItem;
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001200 }
1201 }
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001202 return nullptr;
1203}
1204
1205/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001206 * @brief Finds the inventory item associated with the specified sensor.
1207 * @param inventoryItems D-Bus inventory items associated with sensors.
1208 * @param sensorObjPath D-Bus object path of sensor.
1209 * @return Inventory item within vector, or nullptr if no match found.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001210 */
Ed Tanous23a21a12020-07-25 04:45:05 +00001211inline InventoryItem* findInventoryItemForSensor(
Ed Tanousb5a76932020-09-29 16:16:58 -07001212 const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems,
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001213 const std::string& sensorObjPath)
1214{
1215 for (InventoryItem& inventoryItem : *inventoryItems)
1216 {
1217 if (inventoryItem.sensors.count(sensorObjPath) > 0)
1218 {
1219 return &inventoryItem;
1220 }
1221 }
1222 return nullptr;
1223}
1224
1225/**
Anthony Wilsond5005492019-07-31 16:34:17 -05001226 * @brief Finds the inventory item associated with the specified led path.
1227 * @param inventoryItems D-Bus inventory items associated with sensors.
1228 * @param ledObjPath D-Bus object path of led.
1229 * @return Inventory item within vector, or nullptr if no match found.
1230 */
1231inline InventoryItem*
1232 findInventoryItemForLed(std::vector<InventoryItem>& inventoryItems,
1233 const std::string& ledObjPath)
1234{
1235 for (InventoryItem& inventoryItem : inventoryItems)
1236 {
1237 if (inventoryItem.ledObjectPath == ledObjPath)
1238 {
1239 return &inventoryItem;
1240 }
1241 }
1242 return nullptr;
1243}
1244
1245/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001246 * @brief Adds inventory item and associated sensor to specified vector.
1247 *
1248 * Adds a new InventoryItem to the vector if necessary. Searches for an
1249 * existing InventoryItem with the specified object path. If not found, one is
1250 * added to the vector.
1251 *
1252 * Next, the specified sensor is added to the set of sensors associated with the
1253 * InventoryItem.
1254 *
1255 * @param inventoryItems D-Bus inventory items associated with sensors.
1256 * @param invItemObjPath D-Bus object path of inventory item.
1257 * @param sensorObjPath D-Bus object path of sensor
1258 */
Ed Tanousb5a76932020-09-29 16:16:58 -07001259inline void addInventoryItem(
1260 const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems,
1261 const std::string& invItemObjPath, const std::string& sensorObjPath)
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001262{
1263 // Look for inventory item in vector
Patrick Williams89492a12023-05-10 07:51:34 -05001264 InventoryItem* inventoryItem = findInventoryItem(inventoryItems,
1265 invItemObjPath);
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001266
1267 // If inventory item doesn't exist in vector, add it
1268 if (inventoryItem == nullptr)
1269 {
1270 inventoryItems->emplace_back(invItemObjPath);
1271 inventoryItem = &(inventoryItems->back());
1272 }
1273
1274 // Add sensor to set of sensors associated with inventory item
1275 inventoryItem->sensors.emplace(sensorObjPath);
1276}
1277
1278/**
1279 * @brief Stores D-Bus data in the specified inventory item.
1280 *
1281 * Finds D-Bus data in the specified map of interfaces. Stores the data in the
1282 * specified InventoryItem.
1283 *
1284 * This data is later used to provide sensor property values in the JSON
1285 * response.
1286 *
1287 * @param inventoryItem Inventory item where data will be stored.
1288 * @param interfacesDict Map containing D-Bus interfaces and their properties
1289 * for the specified inventory item.
1290 */
Ed Tanous23a21a12020-07-25 04:45:05 +00001291inline void storeInventoryItemData(
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001292 InventoryItem& inventoryItem,
Ed Tanous711ac7a2021-12-20 09:34:41 -08001293 const dbus::utility::DBusInteracesMap& interfacesDict)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001294{
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001295 // Get properties from Inventory.Item interface
Ed Tanous711ac7a2021-12-20 09:34:41 -08001296
Ed Tanous9eb808c2022-01-25 10:19:23 -08001297 for (const auto& [interface, values] : interfacesDict)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001298 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001299 if (interface == "xyz.openbmc_project.Inventory.Item")
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001300 {
Ed Tanous9eb808c2022-01-25 10:19:23 -08001301 for (const auto& [name, dbusValue] : values)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001302 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001303 if (name == "Present")
1304 {
1305 const bool* value = std::get_if<bool>(&dbusValue);
1306 if (value != nullptr)
1307 {
1308 inventoryItem.isPresent = *value;
1309 }
1310 }
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001311 }
1312 }
Ed Tanous711ac7a2021-12-20 09:34:41 -08001313 // Check if Inventory.Item.PowerSupply interface is present
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001314
Ed Tanous711ac7a2021-12-20 09:34:41 -08001315 if (interface == "xyz.openbmc_project.Inventory.Item.PowerSupply")
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001316 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001317 inventoryItem.isPowerSupply = true;
1318 }
1319
1320 // Get properties from Inventory.Decorator.Asset interface
1321 if (interface == "xyz.openbmc_project.Inventory.Decorator.Asset")
1322 {
Ed Tanous9eb808c2022-01-25 10:19:23 -08001323 for (const auto& [name, dbusValue] : values)
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001324 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001325 if (name == "Manufacturer")
1326 {
1327 const std::string* value =
1328 std::get_if<std::string>(&dbusValue);
1329 if (value != nullptr)
1330 {
1331 inventoryItem.manufacturer = *value;
1332 }
1333 }
1334 if (name == "Model")
1335 {
1336 const std::string* value =
1337 std::get_if<std::string>(&dbusValue);
1338 if (value != nullptr)
1339 {
1340 inventoryItem.model = *value;
1341 }
1342 }
1343 if (name == "SerialNumber")
1344 {
1345 const std::string* value =
1346 std::get_if<std::string>(&dbusValue);
1347 if (value != nullptr)
1348 {
1349 inventoryItem.serialNumber = *value;
1350 }
1351 }
1352 if (name == "PartNumber")
1353 {
1354 const std::string* value =
1355 std::get_if<std::string>(&dbusValue);
1356 if (value != nullptr)
1357 {
1358 inventoryItem.partNumber = *value;
1359 }
1360 }
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001361 }
1362 }
1363
Ed Tanous711ac7a2021-12-20 09:34:41 -08001364 if (interface ==
1365 "xyz.openbmc_project.State.Decorator.OperationalStatus")
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001366 {
Ed Tanous9eb808c2022-01-25 10:19:23 -08001367 for (const auto& [name, dbusValue] : values)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001368 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001369 if (name == "Functional")
1370 {
1371 const bool* value = std::get_if<bool>(&dbusValue);
1372 if (value != nullptr)
1373 {
1374 inventoryItem.isFunctional = *value;
1375 }
1376 }
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001377 }
1378 }
1379 }
1380}
1381
1382/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001383 * @brief Gets D-Bus data for inventory items associated with sensors.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001384 *
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001385 * Uses the specified connections (services) to obtain D-Bus data for inventory
1386 * items associated with sensors. Stores the resulting data in the
1387 * inventoryItems vector.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001388 *
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001389 * This data is later used to provide sensor property values in the JSON
1390 * response.
1391 *
1392 * Finds the inventory item data asynchronously. Invokes callback when data has
1393 * been obtained.
1394 *
1395 * The callback must have the following signature:
1396 * @code
Anthony Wilsond5005492019-07-31 16:34:17 -05001397 * callback(void)
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001398 * @endcode
1399 *
1400 * This function is called recursively, obtaining data asynchronously from one
1401 * connection in each call. This ensures the callback is not invoked until the
1402 * last asynchronous function has completed.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001403 *
1404 * @param sensorsAsyncResp Pointer to object holding response data.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001405 * @param inventoryItems D-Bus inventory items associated with sensors.
1406 * @param invConnections Connections that provide data for the inventory items.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001407 * implements ObjectManager.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001408 * @param callback Callback to invoke when inventory data has been obtained.
1409 * @param invConnectionsIndex Current index in invConnections. Only specified
1410 * in recursive calls to this function.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001411 */
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001412template <typename Callback>
1413static void getInventoryItemsData(
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001414 std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001415 std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
Ed Tanousd0090732022-10-04 17:22:56 -07001416 std::shared_ptr<std::set<std::string>> invConnections, Callback&& callback,
1417 size_t invConnectionsIndex = 0)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001418{
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001419 BMCWEB_LOG_DEBUG << "getInventoryItemsData enter";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001420
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001421 // If no more connections left, call callback
1422 if (invConnectionsIndex >= invConnections->size())
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001423 {
Anthony Wilsond5005492019-07-31 16:34:17 -05001424 callback();
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001425 BMCWEB_LOG_DEBUG << "getInventoryItemsData exit";
1426 return;
1427 }
1428
1429 // Get inventory item data from current connection
Nan Zhoufe04d492022-06-22 17:10:41 +00001430 auto it = invConnections->begin();
1431 std::advance(it, invConnectionsIndex);
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001432 if (it != invConnections->end())
1433 {
1434 const std::string& invConnection = *it;
1435
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001436 // Response handler for GetManagedObjects
Ed Tanousd0090732022-10-04 17:22:56 -07001437 auto respHandler = [sensorsAsyncResp, inventoryItems, invConnections,
1438 callback{std::forward<Callback>(callback)},
1439 invConnectionsIndex](
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08001440 const boost::system::error_code& ec,
Ed Tanousd0090732022-10-04 17:22:56 -07001441 const dbus::utility::ManagedObjectType& resp) {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001442 BMCWEB_LOG_DEBUG << "getInventoryItemsData respHandler enter";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001443 if (ec)
1444 {
1445 BMCWEB_LOG_ERROR
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001446 << "getInventoryItemsData respHandler DBus error " << ec;
zhanghch058d1b46d2021-04-01 11:18:24 +08001447 messages::internalError(sensorsAsyncResp->asyncResp->res);
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001448 return;
1449 }
1450
1451 // Loop through returned object paths
1452 for (const auto& objDictEntry : resp)
1453 {
1454 const std::string& objPath =
1455 static_cast<const std::string&>(objDictEntry.first);
1456
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001457 // If this object path is one of the specified inventory items
Patrick Williams89492a12023-05-10 07:51:34 -05001458 InventoryItem* inventoryItem = findInventoryItem(inventoryItems,
1459 objPath);
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001460 if (inventoryItem != nullptr)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001461 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001462 // Store inventory data in InventoryItem
1463 storeInventoryItemData(*inventoryItem, objDictEntry.second);
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001464 }
1465 }
1466
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001467 // Recurse to get inventory item data from next connection
1468 getInventoryItemsData(sensorsAsyncResp, inventoryItems,
Ed Tanousd0090732022-10-04 17:22:56 -07001469 invConnections, std::move(callback),
1470 invConnectionsIndex + 1);
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001471
1472 BMCWEB_LOG_DEBUG << "getInventoryItemsData respHandler exit";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001473 };
1474
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001475 // Get all object paths and their interfaces for current connection
1476 crow::connections::systemBus->async_method_call(
Ed Tanousd0090732022-10-04 17:22:56 -07001477 std::move(respHandler), invConnection,
Ed Tanousf8bb0ff2022-12-09 11:08:45 -08001478 "/xyz/openbmc_project/inventory",
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001479 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
1480 }
1481
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001482 BMCWEB_LOG_DEBUG << "getInventoryItemsData exit";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001483}
1484
1485/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001486 * @brief Gets connections that provide D-Bus data for inventory items.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001487 *
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001488 * Gets the D-Bus connections (services) that provide data for the inventory
1489 * items that are associated with sensors.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001490 *
1491 * Finds the connections asynchronously. Invokes callback when information has
1492 * been obtained.
1493 *
1494 * The callback must have the following signature:
1495 * @code
Nan Zhoufe04d492022-06-22 17:10:41 +00001496 * callback(std::shared_ptr<std::set<std::string>> invConnections)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001497 * @endcode
1498 *
1499 * @param sensorsAsyncResp Pointer to object holding response data.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001500 * @param inventoryItems D-Bus inventory items associated with sensors.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001501 * @param callback Callback to invoke when connections have been obtained.
1502 */
1503template <typename Callback>
1504static void getInventoryItemsConnections(
Ed Tanousb5a76932020-09-29 16:16:58 -07001505 const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
1506 const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems,
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001507 Callback&& callback)
1508{
1509 BMCWEB_LOG_DEBUG << "getInventoryItemsConnections enter";
1510
1511 const std::string path = "/xyz/openbmc_project/inventory";
George Liue99073f2022-12-09 11:06:16 +08001512 constexpr std::array<std::string_view, 4> interfaces = {
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001513 "xyz.openbmc_project.Inventory.Item",
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001514 "xyz.openbmc_project.Inventory.Item.PowerSupply",
1515 "xyz.openbmc_project.Inventory.Decorator.Asset",
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001516 "xyz.openbmc_project.State.Decorator.OperationalStatus"};
1517
George Liue99073f2022-12-09 11:06:16 +08001518 // Make call to ObjectMapper to find all inventory items
1519 dbus::utility::getSubTree(
1520 path, 0, interfaces,
Ed Tanous002d39b2022-05-31 08:59:27 -07001521 [callback{std::forward<Callback>(callback)}, sensorsAsyncResp,
1522 inventoryItems](
George Liue99073f2022-12-09 11:06:16 +08001523 const boost::system::error_code& ec,
Ed Tanous002d39b2022-05-31 08:59:27 -07001524 const dbus::utility::MapperGetSubTreeResponse& subtree) {
George Liue99073f2022-12-09 11:06:16 +08001525 // Response handler for parsing output from GetSubTree
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001526 BMCWEB_LOG_DEBUG << "getInventoryItemsConnections respHandler enter";
1527 if (ec)
1528 {
zhanghch058d1b46d2021-04-01 11:18:24 +08001529 messages::internalError(sensorsAsyncResp->asyncResp->res);
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001530 BMCWEB_LOG_ERROR
1531 << "getInventoryItemsConnections respHandler DBus error " << ec;
1532 return;
1533 }
1534
1535 // Make unique list of connections for desired inventory items
Nan Zhoufe04d492022-06-22 17:10:41 +00001536 std::shared_ptr<std::set<std::string>> invConnections =
1537 std::make_shared<std::set<std::string>>();
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001538
1539 // Loop through objects from GetSubTree
1540 for (const std::pair<
1541 std::string,
1542 std::vector<std::pair<std::string, std::vector<std::string>>>>&
1543 object : subtree)
1544 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001545 // Check if object path is one of the specified inventory items
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001546 const std::string& objPath = object.first;
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001547 if (findInventoryItem(inventoryItems, objPath) != nullptr)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001548 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001549 // Store all connections to inventory item
1550 for (const std::pair<std::string, std::vector<std::string>>&
1551 objData : object.second)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001552 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001553 const std::string& invConnection = objData.first;
1554 invConnections->insert(invConnection);
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001555 }
1556 }
1557 }
Anthony Wilsond5005492019-07-31 16:34:17 -05001558
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001559 callback(invConnections);
1560 BMCWEB_LOG_DEBUG << "getInventoryItemsConnections respHandler exit";
George Liue99073f2022-12-09 11:06:16 +08001561 });
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001562 BMCWEB_LOG_DEBUG << "getInventoryItemsConnections exit";
1563}
1564
1565/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001566 * @brief Gets associations from sensors to inventory items.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001567 *
1568 * Looks for ObjectMapper associations from the specified sensors to related
Anthony Wilsond5005492019-07-31 16:34:17 -05001569 * inventory items. Then finds the associations from those inventory items to
1570 * their LEDs, if any.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001571 *
1572 * Finds the inventory items asynchronously. Invokes callback when information
1573 * has been obtained.
1574 *
1575 * The callback must have the following signature:
1576 * @code
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001577 * callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001578 * @endcode
1579 *
1580 * @param sensorsAsyncResp Pointer to object holding response data.
1581 * @param sensorNames All sensors within the current chassis.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001582 * implements ObjectManager.
1583 * @param callback Callback to invoke when inventory items have been obtained.
1584 */
1585template <typename Callback>
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001586static void getInventoryItemAssociations(
Ed Tanousb5a76932020-09-29 16:16:58 -07001587 const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
Nan Zhoufe04d492022-06-22 17:10:41 +00001588 const std::shared_ptr<std::set<std::string>>& sensorNames,
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001589 Callback&& callback)
1590{
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001591 BMCWEB_LOG_DEBUG << "getInventoryItemAssociations enter";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001592
1593 // Response handler for GetManagedObjects
Ed Tanous02cad962022-06-30 16:50:15 -07001594 auto respHandler =
1595 [callback{std::forward<Callback>(callback)}, sensorsAsyncResp,
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08001596 sensorNames](const boost::system::error_code& ec,
Ed Tanous02cad962022-06-30 16:50:15 -07001597 const dbus::utility::ManagedObjectType& resp) {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001598 BMCWEB_LOG_DEBUG << "getInventoryItemAssociations respHandler enter";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001599 if (ec)
1600 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001601 BMCWEB_LOG_ERROR
1602 << "getInventoryItemAssociations respHandler DBus error " << ec;
zhanghch058d1b46d2021-04-01 11:18:24 +08001603 messages::internalError(sensorsAsyncResp->asyncResp->res);
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001604 return;
1605 }
1606
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001607 // Create vector to hold list of inventory items
1608 std::shared_ptr<std::vector<InventoryItem>> inventoryItems =
1609 std::make_shared<std::vector<InventoryItem>>();
1610
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001611 // Loop through returned object paths
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001612 std::string sensorAssocPath;
1613 sensorAssocPath.reserve(128); // avoid memory allocations
1614 for (const auto& objDictEntry : resp)
1615 {
1616 const std::string& objPath =
1617 static_cast<const std::string&>(objDictEntry.first);
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001618
1619 // If path is inventory association for one of the specified sensors
1620 for (const std::string& sensorName : *sensorNames)
1621 {
1622 sensorAssocPath = sensorName;
1623 sensorAssocPath += "/inventory";
1624 if (objPath == sensorAssocPath)
1625 {
1626 // Get Association interface for object path
Ed Tanous711ac7a2021-12-20 09:34:41 -08001627 for (const auto& [interface, values] : objDictEntry.second)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001628 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001629 if (interface == "xyz.openbmc_project.Association")
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001630 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001631 for (const auto& [valueName, value] : values)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001632 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001633 if (valueName == "endpoints")
1634 {
1635 const std::vector<std::string>* endpoints =
1636 std::get_if<std::vector<std::string>>(
1637 &value);
1638 if ((endpoints != nullptr) &&
1639 !endpoints->empty())
1640 {
1641 // Add inventory item to vector
1642 const std::string& invItemPath =
1643 endpoints->front();
1644 addInventoryItem(inventoryItems,
1645 invItemPath,
1646 sensorName);
1647 }
1648 }
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001649 }
1650 }
1651 }
1652 break;
1653 }
1654 }
1655 }
1656
Anthony Wilsond5005492019-07-31 16:34:17 -05001657 // Now loop through the returned object paths again, this time to
1658 // find the leds associated with the inventory items we just found
1659 std::string inventoryAssocPath;
1660 inventoryAssocPath.reserve(128); // avoid memory allocations
1661 for (const auto& objDictEntry : resp)
1662 {
1663 const std::string& objPath =
1664 static_cast<const std::string&>(objDictEntry.first);
Anthony Wilsond5005492019-07-31 16:34:17 -05001665
1666 for (InventoryItem& inventoryItem : *inventoryItems)
1667 {
1668 inventoryAssocPath = inventoryItem.objectPath;
1669 inventoryAssocPath += "/leds";
1670 if (objPath == inventoryAssocPath)
1671 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001672 for (const auto& [interface, values] : objDictEntry.second)
Anthony Wilsond5005492019-07-31 16:34:17 -05001673 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001674 if (interface == "xyz.openbmc_project.Association")
Anthony Wilsond5005492019-07-31 16:34:17 -05001675 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001676 for (const auto& [valueName, value] : values)
Anthony Wilsond5005492019-07-31 16:34:17 -05001677 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001678 if (valueName == "endpoints")
1679 {
1680 const std::vector<std::string>* endpoints =
1681 std::get_if<std::vector<std::string>>(
1682 &value);
1683 if ((endpoints != nullptr) &&
1684 !endpoints->empty())
1685 {
1686 // Add inventory item to vector
1687 // Store LED path in inventory item
1688 const std::string& ledPath =
1689 endpoints->front();
1690 inventoryItem.ledObjectPath = ledPath;
1691 }
1692 }
Anthony Wilsond5005492019-07-31 16:34:17 -05001693 }
1694 }
1695 }
Ed Tanous711ac7a2021-12-20 09:34:41 -08001696
Anthony Wilsond5005492019-07-31 16:34:17 -05001697 break;
1698 }
1699 }
1700 }
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001701 callback(inventoryItems);
1702 BMCWEB_LOG_DEBUG << "getInventoryItemAssociations respHandler exit";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001703 };
1704
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001705 // Call GetManagedObjects on the ObjectMapper to get all associations
1706 crow::connections::systemBus->async_method_call(
Ed Tanousd0090732022-10-04 17:22:56 -07001707 std::move(respHandler), "xyz.openbmc_project.ObjectMapper", "/",
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001708 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
1709
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001710 BMCWEB_LOG_DEBUG << "getInventoryItemAssociations exit";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001711}
1712
1713/**
Anthony Wilsond5005492019-07-31 16:34:17 -05001714 * @brief Gets D-Bus data for inventory item leds associated with sensors.
1715 *
1716 * Uses the specified connections (services) to obtain D-Bus data for inventory
1717 * item leds associated with sensors. Stores the resulting data in the
1718 * inventoryItems vector.
1719 *
1720 * This data is later used to provide sensor property values in the JSON
1721 * response.
1722 *
1723 * Finds the inventory item led data asynchronously. Invokes callback when data
1724 * has been obtained.
1725 *
1726 * The callback must have the following signature:
1727 * @code
Gunnar Mills42cbe532019-08-15 15:26:54 -05001728 * callback()
Anthony Wilsond5005492019-07-31 16:34:17 -05001729 * @endcode
1730 *
1731 * This function is called recursively, obtaining data asynchronously from one
1732 * connection in each call. This ensures the callback is not invoked until the
1733 * last asynchronous function has completed.
1734 *
1735 * @param sensorsAsyncResp Pointer to object holding response data.
1736 * @param inventoryItems D-Bus inventory items associated with sensors.
1737 * @param ledConnections Connections that provide data for the inventory leds.
1738 * @param callback Callback to invoke when inventory data has been obtained.
1739 * @param ledConnectionsIndex Current index in ledConnections. Only specified
1740 * in recursive calls to this function.
1741 */
1742template <typename Callback>
1743void getInventoryLedData(
1744 std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
1745 std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
Nan Zhoufe04d492022-06-22 17:10:41 +00001746 std::shared_ptr<std::map<std::string, std::string>> ledConnections,
Anthony Wilsond5005492019-07-31 16:34:17 -05001747 Callback&& callback, size_t ledConnectionsIndex = 0)
1748{
1749 BMCWEB_LOG_DEBUG << "getInventoryLedData enter";
1750
1751 // If no more connections left, call callback
1752 if (ledConnectionsIndex >= ledConnections->size())
1753 {
Gunnar Mills42cbe532019-08-15 15:26:54 -05001754 callback();
Anthony Wilsond5005492019-07-31 16:34:17 -05001755 BMCWEB_LOG_DEBUG << "getInventoryLedData exit";
1756 return;
1757 }
1758
1759 // Get inventory item data from current connection
Nan Zhoufe04d492022-06-22 17:10:41 +00001760 auto it = ledConnections->begin();
1761 std::advance(it, ledConnectionsIndex);
Anthony Wilsond5005492019-07-31 16:34:17 -05001762 if (it != ledConnections->end())
1763 {
1764 const std::string& ledPath = (*it).first;
1765 const std::string& ledConnection = (*it).second;
1766 // Response handler for Get State property
Jonathan Doman1e1e5982021-06-11 09:36:17 -07001767 auto respHandler =
1768 [sensorsAsyncResp, inventoryItems, ledConnections, ledPath,
Ed Tanousf94c4ec2022-01-06 12:44:41 -08001769 callback{std::forward<Callback>(callback)}, ledConnectionsIndex](
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08001770 const boost::system::error_code& ec, const std::string& state) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001771 BMCWEB_LOG_DEBUG << "getInventoryLedData respHandler enter";
1772 if (ec)
1773 {
1774 BMCWEB_LOG_ERROR
1775 << "getInventoryLedData respHandler DBus error " << ec;
1776 messages::internalError(sensorsAsyncResp->asyncResp->res);
1777 return;
1778 }
1779
1780 BMCWEB_LOG_DEBUG << "Led state: " << state;
1781 // Find inventory item with this LED object path
1782 InventoryItem* inventoryItem =
1783 findInventoryItemForLed(*inventoryItems, ledPath);
1784 if (inventoryItem != nullptr)
1785 {
1786 // Store LED state in InventoryItem
Ed Tanous11ba3972022-07-11 09:50:41 -07001787 if (state.ends_with("On"))
Jonathan Doman1e1e5982021-06-11 09:36:17 -07001788 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001789 inventoryItem->ledState = LedState::ON;
Jonathan Doman1e1e5982021-06-11 09:36:17 -07001790 }
Ed Tanous11ba3972022-07-11 09:50:41 -07001791 else if (state.ends_with("Blink"))
Anthony Wilsond5005492019-07-31 16:34:17 -05001792 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001793 inventoryItem->ledState = LedState::BLINK;
Anthony Wilsond5005492019-07-31 16:34:17 -05001794 }
Ed Tanous11ba3972022-07-11 09:50:41 -07001795 else if (state.ends_with("Off"))
Ed Tanous002d39b2022-05-31 08:59:27 -07001796 {
1797 inventoryItem->ledState = LedState::OFF;
1798 }
1799 else
1800 {
1801 inventoryItem->ledState = LedState::UNKNOWN;
1802 }
1803 }
Anthony Wilsond5005492019-07-31 16:34:17 -05001804
Ed Tanous002d39b2022-05-31 08:59:27 -07001805 // Recurse to get LED data from next connection
1806 getInventoryLedData(sensorsAsyncResp, inventoryItems,
1807 ledConnections, std::move(callback),
1808 ledConnectionsIndex + 1);
Anthony Wilsond5005492019-07-31 16:34:17 -05001809
Ed Tanous002d39b2022-05-31 08:59:27 -07001810 BMCWEB_LOG_DEBUG << "getInventoryLedData respHandler exit";
1811 };
Anthony Wilsond5005492019-07-31 16:34:17 -05001812
1813 // Get the State property for the current LED
Jonathan Doman1e1e5982021-06-11 09:36:17 -07001814 sdbusplus::asio::getProperty<std::string>(
1815 *crow::connections::systemBus, ledConnection, ledPath,
1816 "xyz.openbmc_project.Led.Physical", "State",
1817 std::move(respHandler));
Anthony Wilsond5005492019-07-31 16:34:17 -05001818 }
1819
1820 BMCWEB_LOG_DEBUG << "getInventoryLedData exit";
1821}
1822
1823/**
1824 * @brief Gets LED data for LEDs associated with given inventory items.
1825 *
1826 * Gets the D-Bus connections (services) that provide LED data for the LEDs
1827 * associated with the specified inventory items. Then gets the LED data from
1828 * each connection and stores it in the inventory item.
1829 *
1830 * This data is later used to provide sensor property values in the JSON
1831 * response.
1832 *
1833 * Finds the LED data asynchronously. Invokes callback when information has
1834 * been obtained.
1835 *
1836 * The callback must have the following signature:
1837 * @code
Gunnar Mills42cbe532019-08-15 15:26:54 -05001838 * callback()
Anthony Wilsond5005492019-07-31 16:34:17 -05001839 * @endcode
1840 *
1841 * @param sensorsAsyncResp Pointer to object holding response data.
1842 * @param inventoryItems D-Bus inventory items associated with sensors.
1843 * @param callback Callback to invoke when inventory items have been obtained.
1844 */
1845template <typename Callback>
1846void getInventoryLeds(
1847 std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
1848 std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
1849 Callback&& callback)
1850{
1851 BMCWEB_LOG_DEBUG << "getInventoryLeds enter";
1852
1853 const std::string path = "/xyz/openbmc_project";
George Liue99073f2022-12-09 11:06:16 +08001854 constexpr std::array<std::string_view, 1> interfaces = {
Anthony Wilsond5005492019-07-31 16:34:17 -05001855 "xyz.openbmc_project.Led.Physical"};
1856
George Liue99073f2022-12-09 11:06:16 +08001857 // Make call to ObjectMapper to find all inventory items
1858 dbus::utility::getSubTree(
1859 path, 0, interfaces,
Ed Tanous002d39b2022-05-31 08:59:27 -07001860 [callback{std::forward<Callback>(callback)}, sensorsAsyncResp,
1861 inventoryItems](
George Liue99073f2022-12-09 11:06:16 +08001862 const boost::system::error_code& ec,
Ed Tanous002d39b2022-05-31 08:59:27 -07001863 const dbus::utility::MapperGetSubTreeResponse& subtree) {
George Liue99073f2022-12-09 11:06:16 +08001864 // Response handler for parsing output from GetSubTree
Anthony Wilsond5005492019-07-31 16:34:17 -05001865 BMCWEB_LOG_DEBUG << "getInventoryLeds respHandler enter";
1866 if (ec)
1867 {
zhanghch058d1b46d2021-04-01 11:18:24 +08001868 messages::internalError(sensorsAsyncResp->asyncResp->res);
Anthony Wilsond5005492019-07-31 16:34:17 -05001869 BMCWEB_LOG_ERROR << "getInventoryLeds respHandler DBus error "
1870 << ec;
1871 return;
1872 }
1873
1874 // Build map of LED object paths to connections
Nan Zhoufe04d492022-06-22 17:10:41 +00001875 std::shared_ptr<std::map<std::string, std::string>> ledConnections =
1876 std::make_shared<std::map<std::string, std::string>>();
Anthony Wilsond5005492019-07-31 16:34:17 -05001877
1878 // Loop through objects from GetSubTree
1879 for (const std::pair<
1880 std::string,
1881 std::vector<std::pair<std::string, std::vector<std::string>>>>&
1882 object : subtree)
1883 {
1884 // Check if object path is LED for one of the specified inventory
1885 // items
1886 const std::string& ledPath = object.first;
1887 if (findInventoryItemForLed(*inventoryItems, ledPath) != nullptr)
1888 {
1889 // Add mapping from ledPath to connection
1890 const std::string& connection = object.second.begin()->first;
1891 (*ledConnections)[ledPath] = connection;
1892 BMCWEB_LOG_DEBUG << "Added mapping " << ledPath << " -> "
1893 << connection;
1894 }
1895 }
1896
1897 getInventoryLedData(sensorsAsyncResp, inventoryItems, ledConnections,
1898 std::move(callback));
1899 BMCWEB_LOG_DEBUG << "getInventoryLeds respHandler exit";
George Liue99073f2022-12-09 11:06:16 +08001900 });
Anthony Wilsond5005492019-07-31 16:34:17 -05001901 BMCWEB_LOG_DEBUG << "getInventoryLeds exit";
1902}
1903
1904/**
Gunnar Mills42cbe532019-08-15 15:26:54 -05001905 * @brief Gets D-Bus data for Power Supply Attributes such as EfficiencyPercent
1906 *
1907 * Uses the specified connections (services) (currently assumes just one) to
1908 * obtain D-Bus data for Power Supply Attributes. Stores the resulting data in
1909 * the inventoryItems vector. Only stores data in Power Supply inventoryItems.
1910 *
1911 * This data is later used to provide sensor property values in the JSON
1912 * response.
1913 *
1914 * Finds the Power Supply Attributes data asynchronously. Invokes callback
1915 * when data has been obtained.
1916 *
1917 * The callback must have the following signature:
1918 * @code
1919 * callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems)
1920 * @endcode
1921 *
1922 * @param sensorsAsyncResp Pointer to object holding response data.
1923 * @param inventoryItems D-Bus inventory items associated with sensors.
1924 * @param psAttributesConnections Connections that provide data for the Power
1925 * Supply Attributes
1926 * @param callback Callback to invoke when data has been obtained.
1927 */
1928template <typename Callback>
1929void getPowerSupplyAttributesData(
Ed Tanousb5a76932020-09-29 16:16:58 -07001930 const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
Gunnar Mills42cbe532019-08-15 15:26:54 -05001931 std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
Nan Zhoufe04d492022-06-22 17:10:41 +00001932 const std::map<std::string, std::string>& psAttributesConnections,
Gunnar Mills42cbe532019-08-15 15:26:54 -05001933 Callback&& callback)
1934{
1935 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributesData enter";
1936
1937 if (psAttributesConnections.empty())
1938 {
1939 BMCWEB_LOG_DEBUG << "Can't find PowerSupplyAttributes, no connections!";
1940 callback(inventoryItems);
1941 return;
1942 }
1943
1944 // Assuming just one connection (service) for now
Nan Zhoufe04d492022-06-22 17:10:41 +00001945 auto it = psAttributesConnections.begin();
Gunnar Mills42cbe532019-08-15 15:26:54 -05001946
1947 const std::string& psAttributesPath = (*it).first;
1948 const std::string& psAttributesConnection = (*it).second;
1949
1950 // Response handler for Get DeratingFactor property
Ed Tanous002d39b2022-05-31 08:59:27 -07001951 auto respHandler =
1952 [sensorsAsyncResp, inventoryItems,
1953 callback{std::forward<Callback>(callback)}](
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08001954 const boost::system::error_code& ec, const uint32_t value) {
Gunnar Mills42cbe532019-08-15 15:26:54 -05001955 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributesData respHandler enter";
1956 if (ec)
1957 {
1958 BMCWEB_LOG_ERROR
1959 << "getPowerSupplyAttributesData respHandler DBus error " << ec;
zhanghch058d1b46d2021-04-01 11:18:24 +08001960 messages::internalError(sensorsAsyncResp->asyncResp->res);
Gunnar Mills42cbe532019-08-15 15:26:54 -05001961 return;
1962 }
1963
Jonathan Doman1e1e5982021-06-11 09:36:17 -07001964 BMCWEB_LOG_DEBUG << "PS EfficiencyPercent value: " << value;
1965 // Store value in Power Supply Inventory Items
1966 for (InventoryItem& inventoryItem : *inventoryItems)
Gunnar Mills42cbe532019-08-15 15:26:54 -05001967 {
Ed Tanous55f79e62022-01-25 11:26:16 -08001968 if (inventoryItem.isPowerSupply)
Gunnar Mills42cbe532019-08-15 15:26:54 -05001969 {
Jonathan Doman1e1e5982021-06-11 09:36:17 -07001970 inventoryItem.powerSupplyEfficiencyPercent =
1971 static_cast<int>(value);
Gunnar Mills42cbe532019-08-15 15:26:54 -05001972 }
1973 }
Gunnar Mills42cbe532019-08-15 15:26:54 -05001974
1975 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributesData respHandler exit";
1976 callback(inventoryItems);
1977 };
1978
1979 // Get the DeratingFactor property for the PowerSupplyAttributes
1980 // Currently only property on the interface/only one we care about
Jonathan Doman1e1e5982021-06-11 09:36:17 -07001981 sdbusplus::asio::getProperty<uint32_t>(
1982 *crow::connections::systemBus, psAttributesConnection, psAttributesPath,
1983 "xyz.openbmc_project.Control.PowerSupplyAttributes", "DeratingFactor",
1984 std::move(respHandler));
Gunnar Mills42cbe532019-08-15 15:26:54 -05001985
1986 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributesData exit";
1987}
1988
1989/**
1990 * @brief Gets the Power Supply Attributes such as EfficiencyPercent
1991 *
1992 * Gets the D-Bus connection (service) that provides Power Supply Attributes
1993 * data. Then gets the Power Supply Attributes data from the connection
1994 * (currently just assumes 1 connection) and stores the data in the inventory
1995 * item.
1996 *
1997 * This data is later used to provide sensor property values in the JSON
1998 * response. DeratingFactor on D-Bus is mapped to EfficiencyPercent on Redfish.
1999 *
2000 * Finds the Power Supply Attributes data asynchronously. Invokes callback
2001 * when information has been obtained.
2002 *
2003 * The callback must have the following signature:
2004 * @code
2005 * callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems)
2006 * @endcode
2007 *
2008 * @param sensorsAsyncResp Pointer to object holding response data.
2009 * @param inventoryItems D-Bus inventory items associated with sensors.
2010 * @param callback Callback to invoke when data has been obtained.
2011 */
2012template <typename Callback>
2013void getPowerSupplyAttributes(
2014 std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
2015 std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
2016 Callback&& callback)
2017{
2018 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes enter";
2019
2020 // Only need the power supply attributes when the Power Schema
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02002021 if (sensorsAsyncResp->chassisSubNode != sensors::node::power)
Gunnar Mills42cbe532019-08-15 15:26:54 -05002022 {
2023 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes exit since not Power";
2024 callback(inventoryItems);
2025 return;
2026 }
2027
George Liue99073f2022-12-09 11:06:16 +08002028 constexpr std::array<std::string_view, 1> interfaces = {
Gunnar Mills42cbe532019-08-15 15:26:54 -05002029 "xyz.openbmc_project.Control.PowerSupplyAttributes"};
2030
George Liue99073f2022-12-09 11:06:16 +08002031 // Make call to ObjectMapper to find the PowerSupplyAttributes service
2032 dbus::utility::getSubTree(
2033 "/xyz/openbmc_project", 0, interfaces,
Ed Tanousb9d36b42022-02-26 21:42:46 -08002034 [callback{std::forward<Callback>(callback)}, sensorsAsyncResp,
2035 inventoryItems](
George Liue99073f2022-12-09 11:06:16 +08002036 const boost::system::error_code& ec,
Ed Tanousb9d36b42022-02-26 21:42:46 -08002037 const dbus::utility::MapperGetSubTreeResponse& subtree) {
George Liue99073f2022-12-09 11:06:16 +08002038 // Response handler for parsing output from GetSubTree
Ed Tanous002d39b2022-05-31 08:59:27 -07002039 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes respHandler enter";
2040 if (ec)
2041 {
2042 messages::internalError(sensorsAsyncResp->asyncResp->res);
2043 BMCWEB_LOG_ERROR
2044 << "getPowerSupplyAttributes respHandler DBus error " << ec;
2045 return;
2046 }
2047 if (subtree.empty())
2048 {
2049 BMCWEB_LOG_DEBUG << "Can't find Power Supply Attributes!";
2050 callback(inventoryItems);
2051 return;
2052 }
Gunnar Mills42cbe532019-08-15 15:26:54 -05002053
Ed Tanous002d39b2022-05-31 08:59:27 -07002054 // Currently we only support 1 power supply attribute, use this for
2055 // all the power supplies. Build map of object path to connection.
2056 // Assume just 1 connection and 1 path for now.
Nan Zhoufe04d492022-06-22 17:10:41 +00002057 std::map<std::string, std::string> psAttributesConnections;
Gunnar Mills42cbe532019-08-15 15:26:54 -05002058
Ed Tanous002d39b2022-05-31 08:59:27 -07002059 if (subtree[0].first.empty() || subtree[0].second.empty())
2060 {
2061 BMCWEB_LOG_DEBUG << "Power Supply Attributes mapper error!";
2062 callback(inventoryItems);
2063 return;
2064 }
Gunnar Mills42cbe532019-08-15 15:26:54 -05002065
Ed Tanous002d39b2022-05-31 08:59:27 -07002066 const std::string& psAttributesPath = subtree[0].first;
2067 const std::string& connection = subtree[0].second.begin()->first;
Gunnar Mills42cbe532019-08-15 15:26:54 -05002068
Ed Tanous002d39b2022-05-31 08:59:27 -07002069 if (connection.empty())
2070 {
2071 BMCWEB_LOG_DEBUG << "Power Supply Attributes mapper error!";
2072 callback(inventoryItems);
2073 return;
2074 }
Gunnar Mills42cbe532019-08-15 15:26:54 -05002075
Ed Tanous002d39b2022-05-31 08:59:27 -07002076 psAttributesConnections[psAttributesPath] = connection;
2077 BMCWEB_LOG_DEBUG << "Added mapping " << psAttributesPath << " -> "
2078 << connection;
Gunnar Mills42cbe532019-08-15 15:26:54 -05002079
Ed Tanous002d39b2022-05-31 08:59:27 -07002080 getPowerSupplyAttributesData(sensorsAsyncResp, inventoryItems,
2081 psAttributesConnections,
2082 std::move(callback));
2083 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes respHandler exit";
George Liue99073f2022-12-09 11:06:16 +08002084 });
Gunnar Mills42cbe532019-08-15 15:26:54 -05002085 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes exit";
2086}
2087
2088/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002089 * @brief Gets inventory items associated with sensors.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002090 *
2091 * Finds the inventory items that are associated with the specified sensors.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002092 * Then gets D-Bus data for the inventory items, such as presence and VPD.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002093 *
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002094 * This data is later used to provide sensor property values in the JSON
2095 * response.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002096 *
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002097 * Finds the inventory items asynchronously. Invokes callback when the
2098 * inventory items have been obtained.
2099 *
2100 * The callback must have the following signature:
2101 * @code
2102 * callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems)
2103 * @endcode
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002104 *
2105 * @param sensorsAsyncResp Pointer to object holding response data.
2106 * @param sensorNames All sensors within the current chassis.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002107 * implements ObjectManager.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002108 * @param callback Callback to invoke when inventory items have been obtained.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002109 */
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002110template <typename Callback>
Ed Tanousd0090732022-10-04 17:22:56 -07002111static void
2112 getInventoryItems(std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
2113 const std::shared_ptr<std::set<std::string>> sensorNames,
2114 Callback&& callback)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002115{
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002116 BMCWEB_LOG_DEBUG << "getInventoryItems enter";
2117 auto getInventoryItemAssociationsCb =
Ed Tanousd0090732022-10-04 17:22:56 -07002118 [sensorsAsyncResp, callback{std::forward<Callback>(callback)}](
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002119 std::shared_ptr<std::vector<InventoryItem>> inventoryItems) {
Ed Tanous002d39b2022-05-31 08:59:27 -07002120 BMCWEB_LOG_DEBUG << "getInventoryItemAssociationsCb enter";
2121 auto getInventoryItemsConnectionsCb =
Ed Tanousd0090732022-10-04 17:22:56 -07002122 [sensorsAsyncResp, inventoryItems,
Ed Tanous002d39b2022-05-31 08:59:27 -07002123 callback{std::forward<const Callback>(callback)}](
Nan Zhoufe04d492022-06-22 17:10:41 +00002124 std::shared_ptr<std::set<std::string>> invConnections) {
Ed Tanous002d39b2022-05-31 08:59:27 -07002125 BMCWEB_LOG_DEBUG << "getInventoryItemsConnectionsCb enter";
2126 auto getInventoryItemsDataCb = [sensorsAsyncResp, inventoryItems,
2127 callback{std::move(callback)}]() {
2128 BMCWEB_LOG_DEBUG << "getInventoryItemsDataCb enter";
Gunnar Mills42cbe532019-08-15 15:26:54 -05002129
Ed Tanous002d39b2022-05-31 08:59:27 -07002130 auto getInventoryLedsCb = [sensorsAsyncResp, inventoryItems,
2131 callback{std::move(callback)}]() {
2132 BMCWEB_LOG_DEBUG << "getInventoryLedsCb enter";
2133 // Find Power Supply Attributes and get the data
2134 getPowerSupplyAttributes(sensorsAsyncResp, inventoryItems,
2135 std::move(callback));
2136 BMCWEB_LOG_DEBUG << "getInventoryLedsCb exit";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002137 };
2138
Ed Tanous002d39b2022-05-31 08:59:27 -07002139 // Find led connections and get the data
2140 getInventoryLeds(sensorsAsyncResp, inventoryItems,
2141 std::move(getInventoryLedsCb));
2142 BMCWEB_LOG_DEBUG << "getInventoryItemsDataCb exit";
2143 };
2144
2145 // Get inventory item data from connections
2146 getInventoryItemsData(sensorsAsyncResp, inventoryItems,
Ed Tanousd0090732022-10-04 17:22:56 -07002147 invConnections,
Ed Tanous002d39b2022-05-31 08:59:27 -07002148 std::move(getInventoryItemsDataCb));
2149 BMCWEB_LOG_DEBUG << "getInventoryItemsConnectionsCb exit";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002150 };
2151
Ed Tanous002d39b2022-05-31 08:59:27 -07002152 // Get connections that provide inventory item data
2153 getInventoryItemsConnections(sensorsAsyncResp, inventoryItems,
2154 std::move(getInventoryItemsConnectionsCb));
2155 BMCWEB_LOG_DEBUG << "getInventoryItemAssociationsCb exit";
2156 };
2157
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002158 // Get associations from sensors to inventory items
Ed Tanousd0090732022-10-04 17:22:56 -07002159 getInventoryItemAssociations(sensorsAsyncResp, sensorNames,
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002160 std::move(getInventoryItemAssociationsCb));
2161 BMCWEB_LOG_DEBUG << "getInventoryItems exit";
2162}
2163
2164/**
2165 * @brief Returns JSON PowerSupply object for the specified inventory item.
2166 *
2167 * Searches for a JSON PowerSupply object that matches the specified inventory
2168 * item. If one is not found, a new PowerSupply object is added to the JSON
2169 * array.
2170 *
2171 * Multiple sensors are often associated with one power supply inventory item.
2172 * As a result, multiple sensor values are stored in one JSON PowerSupply
2173 * object.
2174 *
2175 * @param powerSupplyArray JSON array containing Redfish PowerSupply objects.
2176 * @param inventoryItem Inventory item for the power supply.
2177 * @param chassisId Chassis that contains the power supply.
2178 * @return JSON PowerSupply object for the specified inventory item.
2179 */
Ed Tanous23a21a12020-07-25 04:45:05 +00002180inline nlohmann::json& getPowerSupply(nlohmann::json& powerSupplyArray,
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002181 const InventoryItem& inventoryItem,
2182 const std::string& chassisId)
2183{
2184 // Check if matching PowerSupply object already exists in JSON array
2185 for (nlohmann::json& powerSupply : powerSupplyArray)
2186 {
Alexander Hansenf7afb172023-04-27 14:34:08 +02002187 if (powerSupply["Name"] ==
2188 boost::replace_all_copy(inventoryItem.name, "_", " "))
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002189 {
2190 return powerSupply;
2191 }
2192 }
2193
2194 // Add new PowerSupply object to JSON array
2195 powerSupplyArray.push_back({});
2196 nlohmann::json& powerSupply = powerSupplyArray.back();
Willy Tueddfc432022-09-26 16:46:38 +00002197 boost::urls::url url = crow::utility::urlFromPieces(
2198 "redfish", "v1", "Chassis", chassisId, "Power");
2199 url.set_fragment(("/PowerSupplies"_json_pointer).to_string());
2200 powerSupply["@odata.id"] = std::move(url);
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002201 powerSupply["Name"] = boost::replace_all_copy(inventoryItem.name, "_", " ");
2202 powerSupply["Manufacturer"] = inventoryItem.manufacturer;
2203 powerSupply["Model"] = inventoryItem.model;
2204 powerSupply["PartNumber"] = inventoryItem.partNumber;
2205 powerSupply["SerialNumber"] = inventoryItem.serialNumber;
Anthony Wilsond5005492019-07-31 16:34:17 -05002206 setLedState(powerSupply, &inventoryItem);
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002207
Gunnar Mills42cbe532019-08-15 15:26:54 -05002208 if (inventoryItem.powerSupplyEfficiencyPercent >= 0)
2209 {
2210 powerSupply["EfficiencyPercent"] =
2211 inventoryItem.powerSupplyEfficiencyPercent;
2212 }
2213
2214 powerSupply["Status"]["State"] = getState(&inventoryItem);
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002215 const char* health = inventoryItem.isFunctional ? "OK" : "Critical";
2216 powerSupply["Status"]["Health"] = health;
2217
2218 return powerSupply;
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002219}
2220
2221/**
Shawn McCarneyde629b62019-03-08 10:42:51 -06002222 * @brief Gets the values of the specified sensors.
2223 *
2224 * Stores the results as JSON in the SensorsAsyncResp.
2225 *
2226 * Gets the sensor values asynchronously. Stores the results later when the
2227 * information has been obtained.
2228 *
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002229 * The sensorNames set contains all requested sensors for the current chassis.
Shawn McCarneyde629b62019-03-08 10:42:51 -06002230 *
2231 * To minimize the number of DBus calls, the DBus method
2232 * org.freedesktop.DBus.ObjectManager.GetManagedObjects() is used to get the
2233 * values of all sensors provided by a connection (service).
2234 *
2235 * The connections set contains all the connections that provide sensor values.
2236 *
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002237 * The InventoryItem vector contains D-Bus inventory items associated with the
2238 * sensors. Inventory item data is needed for some Redfish sensor properties.
2239 *
Shawn McCarneyde629b62019-03-08 10:42:51 -06002240 * @param SensorsAsyncResp Pointer to object holding response data.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002241 * @param sensorNames All requested sensors within the current chassis.
Shawn McCarneyde629b62019-03-08 10:42:51 -06002242 * @param connections Connections that provide sensor values.
Shawn McCarneyde629b62019-03-08 10:42:51 -06002243 * implements ObjectManager.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002244 * @param inventoryItems Inventory items associated with the sensors.
Shawn McCarneyde629b62019-03-08 10:42:51 -06002245 */
Ed Tanous23a21a12020-07-25 04:45:05 +00002246inline void getSensorData(
Ed Tanous81ce6092020-12-17 16:54:55 +00002247 const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
Nan Zhoufe04d492022-06-22 17:10:41 +00002248 const std::shared_ptr<std::set<std::string>>& sensorNames,
2249 const std::set<std::string>& connections,
Ed Tanousb5a76932020-09-29 16:16:58 -07002250 const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems)
Shawn McCarneyde629b62019-03-08 10:42:51 -06002251{
2252 BMCWEB_LOG_DEBUG << "getSensorData enter";
2253 // Get managed objects from all services exposing sensors
2254 for (const std::string& connection : connections)
2255 {
2256 // Response handler to process managed objects
Ed Tanous002d39b2022-05-31 08:59:27 -07002257 auto getManagedObjectsCb =
2258 [sensorsAsyncResp, sensorNames,
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08002259 inventoryItems](const boost::system::error_code& ec,
Ed Tanous02cad962022-06-30 16:50:15 -07002260 const dbus::utility::ManagedObjectType& resp) {
Shawn McCarneyde629b62019-03-08 10:42:51 -06002261 BMCWEB_LOG_DEBUG << "getManagedObjectsCb enter";
2262 if (ec)
2263 {
2264 BMCWEB_LOG_ERROR << "getManagedObjectsCb DBUS error: " << ec;
zhanghch058d1b46d2021-04-01 11:18:24 +08002265 messages::internalError(sensorsAsyncResp->asyncResp->res);
Shawn McCarneyde629b62019-03-08 10:42:51 -06002266 return;
2267 }
2268 // Go through all objects and update response with sensor data
2269 for (const auto& objDictEntry : resp)
2270 {
2271 const std::string& objPath =
2272 static_cast<const std::string&>(objDictEntry.first);
2273 BMCWEB_LOG_DEBUG << "getManagedObjectsCb parsing object "
2274 << objPath;
2275
Shawn McCarneyde629b62019-03-08 10:42:51 -06002276 std::vector<std::string> split;
2277 // Reserve space for
2278 // /xyz/openbmc_project/sensors/<name>/<subname>
2279 split.reserve(6);
Ed Tanous50ebd4a2023-01-19 19:03:17 -08002280 // NOLINTNEXTLINE
2281 bmcweb::split(split, objPath, '/');
Shawn McCarneyde629b62019-03-08 10:42:51 -06002282 if (split.size() < 6)
2283 {
2284 BMCWEB_LOG_ERROR << "Got path that isn't long enough "
2285 << objPath;
2286 continue;
2287 }
Ed Tanous50ebd4a2023-01-19 19:03:17 -08002288 // These indexes aren't intuitive, as split puts an empty
Shawn McCarneyde629b62019-03-08 10:42:51 -06002289 // string at the beginning
2290 const std::string& sensorType = split[4];
2291 const std::string& sensorName = split[5];
2292 BMCWEB_LOG_DEBUG << "sensorName " << sensorName
2293 << " sensorType " << sensorType;
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002294 if (sensorNames->find(objPath) == sensorNames->end())
Shawn McCarneyde629b62019-03-08 10:42:51 -06002295 {
Andrew Geissleraccdbb22021-11-09 15:24:45 -06002296 BMCWEB_LOG_DEBUG << sensorName << " not in sensor list ";
Shawn McCarneyde629b62019-03-08 10:42:51 -06002297 continue;
2298 }
2299
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002300 // Find inventory item (if any) associated with sensor
2301 InventoryItem* inventoryItem =
2302 findInventoryItemForSensor(inventoryItems, objPath);
2303
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002304 const std::string& sensorSchema =
Ed Tanous81ce6092020-12-17 16:54:55 +00002305 sensorsAsyncResp->chassisSubNode;
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002306
2307 nlohmann::json* sensorJson = nullptr;
2308
Nan Zhou928fefb2022-03-28 08:45:00 -07002309 if (sensorSchema == sensors::node::sensors &&
2310 !sensorsAsyncResp->efficientExpand)
Shawn McCarneyde629b62019-03-08 10:42:51 -06002311 {
Ed Tanousc1d019a2022-08-06 09:36:06 -07002312 std::string sensorTypeEscaped(sensorType);
2313 sensorTypeEscaped.erase(
2314 std::remove(sensorTypeEscaped.begin(),
2315 sensorTypeEscaped.end(), '_'),
2316 sensorTypeEscaped.end());
2317 std::string sensorId(sensorTypeEscaped);
2318 sensorId += "_";
2319 sensorId += sensorName;
2320
zhanghch058d1b46d2021-04-01 11:18:24 +08002321 sensorsAsyncResp->asyncResp->res.jsonValue["@odata.id"] =
Ed Tanousc1d019a2022-08-06 09:36:06 -07002322 crow::utility::urlFromPieces(
2323 "redfish", "v1", "Chassis",
2324 sensorsAsyncResp->chassisId,
2325 sensorsAsyncResp->chassisSubNode, sensorId);
zhanghch058d1b46d2021-04-01 11:18:24 +08002326 sensorJson = &(sensorsAsyncResp->asyncResp->res.jsonValue);
Shawn McCarneyde629b62019-03-08 10:42:51 -06002327 }
2328 else
2329 {
Ed Tanous271584a2019-07-09 16:24:22 -07002330 std::string fieldName;
Nan Zhou928fefb2022-03-28 08:45:00 -07002331 if (sensorsAsyncResp->efficientExpand)
2332 {
2333 fieldName = "Members";
2334 }
2335 else if (sensorType == "temperature")
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002336 {
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002337 fieldName = "Temperatures";
2338 }
2339 else if (sensorType == "fan" || sensorType == "fan_tach" ||
2340 sensorType == "fan_pwm")
2341 {
2342 fieldName = "Fans";
2343 }
2344 else if (sensorType == "voltage")
2345 {
2346 fieldName = "Voltages";
2347 }
2348 else if (sensorType == "power")
2349 {
Ed Tanous55f79e62022-01-25 11:26:16 -08002350 if (sensorName == "total_power")
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002351 {
2352 fieldName = "PowerControl";
2353 }
2354 else if ((inventoryItem != nullptr) &&
2355 (inventoryItem->isPowerSupply))
2356 {
2357 fieldName = "PowerSupplies";
2358 }
2359 else
2360 {
2361 // Other power sensors are in SensorCollection
2362 continue;
2363 }
2364 }
2365 else
2366 {
2367 BMCWEB_LOG_ERROR << "Unsure how to handle sensorType "
2368 << sensorType;
2369 continue;
2370 }
2371
2372 nlohmann::json& tempArray =
zhanghch058d1b46d2021-04-01 11:18:24 +08002373 sensorsAsyncResp->asyncResp->res.jsonValue[fieldName];
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002374 if (fieldName == "PowerControl")
2375 {
2376 if (tempArray.empty())
2377 {
2378 // Put multiple "sensors" into a single
2379 // PowerControl. Follows MemberId naming and
2380 // naming in power.hpp.
Ed Tanous14766872022-03-15 10:44:42 -07002381 nlohmann::json::object_t power;
Willy Tueddfc432022-09-26 16:46:38 +00002382 boost::urls::url url = crow::utility::urlFromPieces(
2383 "redfish", "v1", "Chassis",
2384 sensorsAsyncResp->chassisId,
2385 sensorsAsyncResp->chassisSubNode);
2386 url.set_fragment((""_json_pointer / fieldName / "0")
2387 .to_string());
2388 power["@odata.id"] = std::move(url);
Ed Tanous14766872022-03-15 10:44:42 -07002389 tempArray.push_back(std::move(power));
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002390 }
2391 sensorJson = &(tempArray.back());
2392 }
2393 else if (fieldName == "PowerSupplies")
2394 {
2395 if (inventoryItem != nullptr)
2396 {
2397 sensorJson =
2398 &(getPowerSupply(tempArray, *inventoryItem,
Ed Tanous81ce6092020-12-17 16:54:55 +00002399 sensorsAsyncResp->chassisId));
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002400 }
2401 }
Nan Zhou928fefb2022-03-28 08:45:00 -07002402 else if (fieldName == "Members")
2403 {
Ed Tanous677bb752022-09-15 10:52:19 -07002404 std::string sensorTypeEscaped(sensorType);
2405 sensorTypeEscaped.erase(
2406 std::remove(sensorTypeEscaped.begin(),
2407 sensorTypeEscaped.end(), '_'),
2408 sensorTypeEscaped.end());
2409 std::string sensorId(sensorTypeEscaped);
2410 sensorId += "_";
2411 sensorId += sensorName;
2412
Ed Tanous14766872022-03-15 10:44:42 -07002413 nlohmann::json::object_t member;
Ed Tanous677bb752022-09-15 10:52:19 -07002414 member["@odata.id"] = crow::utility::urlFromPieces(
2415 "redfish", "v1", "Chassis",
2416 sensorsAsyncResp->chassisId,
2417 sensorsAsyncResp->chassisSubNode, sensorId);
Ed Tanous14766872022-03-15 10:44:42 -07002418 tempArray.push_back(std::move(member));
Nan Zhou928fefb2022-03-28 08:45:00 -07002419 sensorJson = &(tempArray.back());
2420 }
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002421 else
2422 {
Ed Tanous14766872022-03-15 10:44:42 -07002423 nlohmann::json::object_t member;
Willy Tueddfc432022-09-26 16:46:38 +00002424 boost::urls::url url = crow::utility::urlFromPieces(
2425 "redfish", "v1", "Chassis",
2426 sensorsAsyncResp->chassisId,
2427 sensorsAsyncResp->chassisSubNode);
2428 url.set_fragment(
2429 (""_json_pointer / fieldName).to_string());
2430 member["@odata.id"] = std::move(url);
Ed Tanous14766872022-03-15 10:44:42 -07002431 tempArray.push_back(std::move(member));
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002432 sensorJson = &(tempArray.back());
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002433 }
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002434 }
Shawn McCarneyde629b62019-03-08 10:42:51 -06002435
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002436 if (sensorJson != nullptr)
2437 {
Ed Tanous1d7c0052022-08-09 12:32:26 -07002438 objectInterfacesToJson(sensorName, sensorType,
2439 sensorsAsyncResp->chassisSubNode,
2440 objDictEntry.second, *sensorJson,
2441 inventoryItem);
2442
2443 std::string path = "/xyz/openbmc_project/sensors/";
2444 path += sensorType;
2445 path += "/";
2446 path += sensorName;
Ed Tanousc1d019a2022-08-06 09:36:06 -07002447 sensorsAsyncResp->addMetadata(*sensorJson, path);
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002448 }
Shawn McCarneyde629b62019-03-08 10:42:51 -06002449 }
Ed Tanous81ce6092020-12-17 16:54:55 +00002450 if (sensorsAsyncResp.use_count() == 1)
James Feist8bd25cc2019-03-15 15:14:00 -07002451 {
Ed Tanous81ce6092020-12-17 16:54:55 +00002452 sortJSONResponse(sensorsAsyncResp);
Nan Zhou928fefb2022-03-28 08:45:00 -07002453 if (sensorsAsyncResp->chassisSubNode ==
2454 sensors::node::sensors &&
2455 sensorsAsyncResp->efficientExpand)
2456 {
2457 sensorsAsyncResp->asyncResp->res
2458 .jsonValue["Members@odata.count"] =
2459 sensorsAsyncResp->asyncResp->res.jsonValue["Members"]
2460 .size();
2461 }
2462 else if (sensorsAsyncResp->chassisSubNode ==
2463 sensors::node::thermal)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002464 {
Ed Tanous81ce6092020-12-17 16:54:55 +00002465 populateFanRedundancy(sensorsAsyncResp);
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002466 }
James Feist8bd25cc2019-03-15 15:14:00 -07002467 }
Shawn McCarneyde629b62019-03-08 10:42:51 -06002468 BMCWEB_LOG_DEBUG << "getManagedObjectsCb exit";
2469 };
2470
Shawn McCarneyde629b62019-03-08 10:42:51 -06002471 crow::connections::systemBus->async_method_call(
Ed Tanousd0090732022-10-04 17:22:56 -07002472 getManagedObjectsCb, connection, "/xyz/openbmc_project/sensors",
Shawn McCarneyde629b62019-03-08 10:42:51 -06002473 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
Ed Tanous23a21a12020-07-25 04:45:05 +00002474 }
Shawn McCarneyde629b62019-03-08 10:42:51 -06002475 BMCWEB_LOG_DEBUG << "getSensorData exit";
2476}
2477
Nan Zhoufe04d492022-06-22 17:10:41 +00002478inline void
2479 processSensorList(const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
2480 const std::shared_ptr<std::set<std::string>>& sensorNames)
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002481{
Nan Zhoufe04d492022-06-22 17:10:41 +00002482 auto getConnectionCb = [sensorsAsyncResp, sensorNames](
2483 const std::set<std::string>& connections) {
Ed Tanous002d39b2022-05-31 08:59:27 -07002484 BMCWEB_LOG_DEBUG << "getConnectionCb enter";
Ed Tanousd0090732022-10-04 17:22:56 -07002485 auto getInventoryItemsCb =
2486 [sensorsAsyncResp, sensorNames,
2487 connections](const std::shared_ptr<std::vector<InventoryItem>>&
2488 inventoryItems) {
2489 BMCWEB_LOG_DEBUG << "getInventoryItemsCb enter";
2490 // Get sensor data and store results in JSON
2491 getSensorData(sensorsAsyncResp, sensorNames, connections,
2492 inventoryItems);
2493 BMCWEB_LOG_DEBUG << "getInventoryItemsCb exit";
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002494 };
2495
Ed Tanousd0090732022-10-04 17:22:56 -07002496 // Get inventory items associated with sensors
2497 getInventoryItems(sensorsAsyncResp, sensorNames,
2498 std::move(getInventoryItemsCb));
2499
Ed Tanous002d39b2022-05-31 08:59:27 -07002500 BMCWEB_LOG_DEBUG << "getConnectionCb exit";
2501 };
2502
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002503 // Get set of connections that provide sensor values
Ed Tanous81ce6092020-12-17 16:54:55 +00002504 getConnections(sensorsAsyncResp, sensorNames, std::move(getConnectionCb));
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002505}
2506
Shawn McCarneyde629b62019-03-08 10:42:51 -06002507/**
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +01002508 * @brief Entry point for retrieving sensors data related to requested
2509 * chassis.
Kowalski, Kamil588c3f02018-04-03 14:55:27 +02002510 * @param SensorsAsyncResp Pointer to object holding response data
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +01002511 */
Ed Tanousb5a76932020-09-29 16:16:58 -07002512inline void
Ed Tanous81ce6092020-12-17 16:54:55 +00002513 getChassisData(const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp)
Ed Tanous1abe55e2018-09-05 08:30:59 -07002514{
2515 BMCWEB_LOG_DEBUG << "getChassisData enter";
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002516 auto getChassisCb =
Ed Tanous81ce6092020-12-17 16:54:55 +00002517 [sensorsAsyncResp](
Nan Zhoufe04d492022-06-22 17:10:41 +00002518 const std::shared_ptr<std::set<std::string>>& sensorNames) {
Ed Tanous002d39b2022-05-31 08:59:27 -07002519 BMCWEB_LOG_DEBUG << "getChassisCb enter";
2520 processSensorList(sensorsAsyncResp, sensorNames);
2521 BMCWEB_LOG_DEBUG << "getChassisCb exit";
2522 };
Nan Zhou928fefb2022-03-28 08:45:00 -07002523 // SensorCollection doesn't contain the Redundancy property
2524 if (sensorsAsyncResp->chassisSubNode != sensors::node::sensors)
2525 {
2526 sensorsAsyncResp->asyncResp->res.jsonValue["Redundancy"] =
2527 nlohmann::json::array();
2528 }
Shawn McCarney26f03892019-05-03 13:20:24 -05002529 // Get set of sensors in chassis
Ed Tanous7f1cc262022-08-09 13:33:57 -07002530 getChassis(sensorsAsyncResp->asyncResp, sensorsAsyncResp->chassisId,
2531 sensorsAsyncResp->chassisSubNode, sensorsAsyncResp->types,
2532 std::move(getChassisCb));
Ed Tanous1abe55e2018-09-05 08:30:59 -07002533 BMCWEB_LOG_DEBUG << "getChassisData exit";
Ed Tanous271584a2019-07-09 16:24:22 -07002534}
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +01002535
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302536/**
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002537 * @brief Find the requested sensorName in the list of all sensors supplied by
2538 * the chassis node
2539 *
2540 * @param sensorName The sensor name supplied in the PATCH request
2541 * @param sensorsList The list of sensors managed by the chassis node
2542 * @param sensorsModified The list of sensors that were found as a result of
2543 * repeated calls to this function
2544 */
Nan Zhoufe04d492022-06-22 17:10:41 +00002545inline bool
2546 findSensorNameUsingSensorPath(std::string_view sensorName,
Ed Tanous02cad962022-06-30 16:50:15 -07002547 const std::set<std::string>& sensorsList,
Nan Zhoufe04d492022-06-22 17:10:41 +00002548 std::set<std::string>& sensorsModified)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002549{
Nan Zhoufe04d492022-06-22 17:10:41 +00002550 for (const auto& chassisSensor : sensorsList)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002551 {
George Liu28aa8de2021-02-01 15:13:30 +08002552 sdbusplus::message::object_path path(chassisSensor);
Ed Tanousb00dcc22021-02-23 12:52:50 -08002553 std::string thisSensorName = path.filename();
George Liu28aa8de2021-02-01 15:13:30 +08002554 if (thisSensorName.empty())
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002555 {
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002556 continue;
2557 }
2558 if (thisSensorName == sensorName)
2559 {
2560 sensorsModified.emplace(chassisSensor);
2561 return true;
2562 }
2563 }
2564 return false;
2565}
2566
Ed Tanousc71d6122022-11-29 14:10:32 -08002567inline std::pair<std::string, std::string>
2568 splitSensorNameAndType(std::string_view sensorId)
2569{
2570 size_t index = sensorId.find('_');
2571 if (index == std::string::npos)
2572 {
2573 return std::make_pair<std::string, std::string>("", "");
2574 }
2575 std::string sensorType{sensorId.substr(0, index)};
2576 std::string sensorName{sensorId.substr(index + 1)};
2577 // fan_pwm and fan_tach need special handling
2578 if (sensorType == "fantach" || sensorType == "fanpwm")
2579 {
2580 sensorType.insert(3, 1, '_');
2581 }
2582 return std::make_pair(sensorType, sensorName);
2583}
2584
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002585/**
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302586 * @brief Entry point for overriding sensor values of given sensor
2587 *
zhanghch058d1b46d2021-04-01 11:18:24 +08002588 * @param sensorAsyncResp response object
Carol Wang4bb3dc32019-10-17 18:15:02 +08002589 * @param allCollections Collections extract from sensors' request patch info
jayaprakash Mutyala91e130a2020-03-04 22:26:38 +00002590 * @param chassisSubNode Chassis Node for which the query has to happen
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302591 */
Ed Tanous23a21a12020-07-25 04:45:05 +00002592inline void setSensorsOverride(
Ed Tanousb5a76932020-09-29 16:16:58 -07002593 const std::shared_ptr<SensorsAsyncResp>& sensorAsyncResp,
Carol Wang4bb3dc32019-10-17 18:15:02 +08002594 std::unordered_map<std::string, std::vector<nlohmann::json>>&
jayaprakash Mutyala397fd612020-02-06 23:33:34 +00002595 allCollections)
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302596{
jayaprakash Mutyala70d1d0a2020-01-21 23:41:36 +00002597 BMCWEB_LOG_INFO << "setSensorsOverride for subNode"
Carol Wang4bb3dc32019-10-17 18:15:02 +08002598 << sensorAsyncResp->chassisSubNode << "\n";
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302599
Ed Tanous543f4402022-01-06 13:12:53 -08002600 const char* propertyValueName = nullptr;
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302601 std::unordered_map<std::string, std::pair<double, std::string>> overrideMap;
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302602 std::string memberId;
Ed Tanous543f4402022-01-06 13:12:53 -08002603 double value = 0.0;
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302604 for (auto& collectionItems : allCollections)
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302605 {
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302606 if (collectionItems.first == "Temperatures")
2607 {
2608 propertyValueName = "ReadingCelsius";
2609 }
2610 else if (collectionItems.first == "Fans")
2611 {
2612 propertyValueName = "Reading";
2613 }
2614 else
2615 {
2616 propertyValueName = "ReadingVolts";
2617 }
2618 for (auto& item : collectionItems.second)
2619 {
zhanghch058d1b46d2021-04-01 11:18:24 +08002620 if (!json_util::readJson(item, sensorAsyncResp->asyncResp->res,
2621 "MemberId", memberId, propertyValueName,
2622 value))
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302623 {
2624 return;
2625 }
2626 overrideMap.emplace(memberId,
2627 std::make_pair(value, collectionItems.first));
2628 }
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302629 }
Carol Wang4bb3dc32019-10-17 18:15:02 +08002630
Ed Tanous002d39b2022-05-31 08:59:27 -07002631 auto getChassisSensorListCb =
2632 [sensorAsyncResp, overrideMap](
Nan Zhoufe04d492022-06-22 17:10:41 +00002633 const std::shared_ptr<std::set<std::string>>& sensorsList) {
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002634 // Match sensor names in the PATCH request to those managed by the
2635 // chassis node
Nan Zhoufe04d492022-06-22 17:10:41 +00002636 const std::shared_ptr<std::set<std::string>> sensorNames =
2637 std::make_shared<std::set<std::string>>();
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302638 for (const auto& item : overrideMap)
2639 {
2640 const auto& sensor = item.first;
Ed Tanousc71d6122022-11-29 14:10:32 -08002641 std::pair<std::string, std::string> sensorNameType =
2642 splitSensorNameAndType(sensor);
2643 if (!findSensorNameUsingSensorPath(sensorNameType.second,
2644 *sensorsList, *sensorNames))
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302645 {
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302646 BMCWEB_LOG_INFO << "Unable to find memberId " << item.first;
zhanghch058d1b46d2021-04-01 11:18:24 +08002647 messages::resourceNotFound(sensorAsyncResp->asyncResp->res,
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302648 item.second.second, item.first);
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302649 return;
2650 }
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302651 }
2652 // Get the connection to which the memberId belongs
Ed Tanous002d39b2022-05-31 08:59:27 -07002653 auto getObjectsWithConnectionCb =
Nan Zhoufe04d492022-06-22 17:10:41 +00002654 [sensorAsyncResp,
2655 overrideMap](const std::set<std::string>& /*connections*/,
2656 const std::set<std::pair<std::string, std::string>>&
2657 objectsWithConnection) {
Jayaprakash Mutyala4f277b52021-12-08 22:46:49 +00002658 if (objectsWithConnection.size() != overrideMap.size())
2659 {
2660 BMCWEB_LOG_INFO
2661 << "Unable to find all objects with proper connection "
2662 << objectsWithConnection.size() << " requested "
2663 << overrideMap.size() << "\n";
2664 messages::resourceNotFound(sensorAsyncResp->asyncResp->res,
2665 sensorAsyncResp->chassisSubNode ==
2666 sensors::node::thermal
2667 ? "Temperatures"
2668 : "Voltages",
2669 "Count");
2670 return;
2671 }
2672 for (const auto& item : objectsWithConnection)
2673 {
2674 sdbusplus::message::object_path path(item.first);
2675 std::string sensorName = path.filename();
2676 if (sensorName.empty())
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302677 {
Jayaprakash Mutyala4f277b52021-12-08 22:46:49 +00002678 messages::internalError(sensorAsyncResp->asyncResp->res);
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302679 return;
2680 }
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302681
Jayaprakash Mutyala4f277b52021-12-08 22:46:49 +00002682 const auto& iterator = overrideMap.find(sensorName);
2683 if (iterator == overrideMap.end())
2684 {
2685 BMCWEB_LOG_INFO << "Unable to find sensor object"
2686 << item.first << "\n";
2687 messages::internalError(sensorAsyncResp->asyncResp->res);
2688 return;
2689 }
2690 crow::connections::systemBus->async_method_call(
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08002691 [sensorAsyncResp](const boost::system::error_code& ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -07002692 if (ec)
2693 {
2694 if (ec.value() ==
2695 boost::system::errc::permission_denied)
Jayaprakash Mutyala4f277b52021-12-08 22:46:49 +00002696 {
Ed Tanous002d39b2022-05-31 08:59:27 -07002697 BMCWEB_LOG_WARNING
2698 << "Manufacturing mode is not Enabled...can't "
2699 "Override the sensor value. ";
Jayaprakash Mutyala4f277b52021-12-08 22:46:49 +00002700
Ed Tanous002d39b2022-05-31 08:59:27 -07002701 messages::insufficientPrivilege(
Jayaprakash Mutyala4f277b52021-12-08 22:46:49 +00002702 sensorAsyncResp->asyncResp->res);
Ed Tanous002d39b2022-05-31 08:59:27 -07002703 return;
Jayaprakash Mutyala4f277b52021-12-08 22:46:49 +00002704 }
Ed Tanous002d39b2022-05-31 08:59:27 -07002705 BMCWEB_LOG_DEBUG
2706 << "setOverrideValueStatus DBUS error: " << ec;
2707 messages::internalError(
2708 sensorAsyncResp->asyncResp->res);
2709 }
Jayaprakash Mutyala4f277b52021-12-08 22:46:49 +00002710 },
2711 item.second, item.first, "org.freedesktop.DBus.Properties",
2712 "Set", "xyz.openbmc_project.Sensor.Value", "Value",
Ed Tanous168e20c2021-12-13 14:39:53 -08002713 dbus::utility::DbusVariantType(iterator->second.first));
Jayaprakash Mutyala4f277b52021-12-08 22:46:49 +00002714 }
2715 };
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302716 // Get object with connection for the given sensor name
2717 getObjectsWithConnection(sensorAsyncResp, sensorNames,
2718 std::move(getObjectsWithConnectionCb));
2719 };
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302720 // get full sensor list for the given chassisId and cross verify the sensor.
Ed Tanous7f1cc262022-08-09 13:33:57 -07002721 getChassis(sensorAsyncResp->asyncResp, sensorAsyncResp->chassisId,
2722 sensorAsyncResp->chassisSubNode, sensorAsyncResp->types,
2723 std::move(getChassisSensorListCb));
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302724}
2725
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02002726/**
2727 * @brief Retrieves mapping of Redfish URIs to sensor value property to D-Bus
2728 * path of the sensor.
2729 *
2730 * Function builds valid Redfish response for sensor query of given chassis and
2731 * node. It then builds metadata about Redfish<->D-Bus correlations and provides
2732 * it to caller in a callback.
2733 *
2734 * @param chassis Chassis for which retrieval should be performed
2735 * @param node Node (group) of sensors. See sensors::node for supported values
2736 * @param mapComplete Callback to be called with retrieval result
2737 */
Krzysztof Grobelny021d32c2021-10-29 16:00:07 +02002738inline void retrieveUriToDbusMap(const std::string& chassis,
2739 const std::string& node,
2740 SensorsAsyncResp::DataCompleteCb&& mapComplete)
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02002741{
Ed Tanous02da7c52022-02-27 00:09:02 -08002742 decltype(sensors::paths)::const_iterator pathIt =
2743 std::find_if(sensors::paths.cbegin(), sensors::paths.cend(),
2744 [&node](auto&& val) { return val.first == node; });
2745 if (pathIt == sensors::paths.cend())
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02002746 {
2747 BMCWEB_LOG_ERROR << "Wrong node provided : " << node;
2748 mapComplete(boost::beast::http::status::bad_request, {});
2749 return;
2750 }
Krzysztof Grobelnyd51e0722021-04-16 13:15:21 +00002751
Nan Zhou72374eb2022-01-27 17:06:51 -08002752 auto asyncResp = std::make_shared<bmcweb::AsyncResp>();
Nan Zhoufe04d492022-06-22 17:10:41 +00002753 auto callback = [asyncResp, mapCompleteCb{std::move(mapComplete)}](
2754 const boost::beast::http::status status,
2755 const std::map<std::string, std::string>& uriToDbus) {
2756 mapCompleteCb(status, uriToDbus);
2757 };
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02002758
2759 auto resp = std::make_shared<SensorsAsyncResp>(
Krzysztof Grobelnyd51e0722021-04-16 13:15:21 +00002760 asyncResp, chassis, pathIt->second, node, std::move(callback));
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02002761 getChassisData(resp);
2762}
2763
Nan Zhoubacb2162022-04-06 11:28:32 -07002764namespace sensors
2765{
Nan Zhou928fefb2022-03-28 08:45:00 -07002766
Nan Zhoubacb2162022-04-06 11:28:32 -07002767inline void getChassisCallback(
Ed Tanousc1d019a2022-08-06 09:36:06 -07002768 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2769 std::string_view chassisId, std::string_view chassisSubNode,
Nan Zhoufe04d492022-06-22 17:10:41 +00002770 const std::shared_ptr<std::set<std::string>>& sensorNames)
Nan Zhoubacb2162022-04-06 11:28:32 -07002771{
Ed Tanousc1d019a2022-08-06 09:36:06 -07002772 BMCWEB_LOG_DEBUG << "getChassisCallback enter ";
Nan Zhoubacb2162022-04-06 11:28:32 -07002773
Ed Tanousc1d019a2022-08-06 09:36:06 -07002774 nlohmann::json& entriesArray = asyncResp->res.jsonValue["Members"];
2775 for (const std::string& sensor : *sensorNames)
Nan Zhoubacb2162022-04-06 11:28:32 -07002776 {
2777 BMCWEB_LOG_DEBUG << "Adding sensor: " << sensor;
2778
2779 sdbusplus::message::object_path path(sensor);
2780 std::string sensorName = path.filename();
2781 if (sensorName.empty())
2782 {
2783 BMCWEB_LOG_ERROR << "Invalid sensor path: " << sensor;
Ed Tanousc1d019a2022-08-06 09:36:06 -07002784 messages::internalError(asyncResp->res);
Nan Zhoubacb2162022-04-06 11:28:32 -07002785 return;
2786 }
Ed Tanousc1d019a2022-08-06 09:36:06 -07002787 std::string type = path.parent_path().filename();
2788 // fan_tach has an underscore in it, so remove it to "normalize" the
2789 // type in the URI
2790 type.erase(std::remove(type.begin(), type.end(), '_'), type.end());
2791
Ed Tanous14766872022-03-15 10:44:42 -07002792 nlohmann::json::object_t member;
Ed Tanousc1d019a2022-08-06 09:36:06 -07002793 std::string id = type;
2794 id += "_";
2795 id += sensorName;
2796 member["@odata.id"] = crow::utility::urlFromPieces(
2797 "redfish", "v1", "Chassis", chassisId, chassisSubNode, id);
2798
Ed Tanous14766872022-03-15 10:44:42 -07002799 entriesArray.push_back(std::move(member));
Nan Zhoubacb2162022-04-06 11:28:32 -07002800 }
2801
Ed Tanousc1d019a2022-08-06 09:36:06 -07002802 asyncResp->res.jsonValue["Members@odata.count"] = entriesArray.size();
Nan Zhoubacb2162022-04-06 11:28:32 -07002803 BMCWEB_LOG_DEBUG << "getChassisCallback exit";
2804}
Nan Zhoue6bd8462022-06-01 04:35:35 +00002805
Nan Zhoude167a62022-06-01 04:47:45 +00002806inline void
2807 handleSensorCollectionGet(App& app, const crow::Request& req,
2808 const std::shared_ptr<bmcweb::AsyncResp>& aResp,
2809 const std::string& chassisId)
2810{
2811 query_param::QueryCapabilities capabilities = {
2812 .canDelegateExpandLevel = 1,
2813 };
2814 query_param::Query delegatedQuery;
Carson Labrado3ba00072022-06-06 19:40:56 +00002815 if (!redfish::setUpRedfishRouteWithDelegation(app, req, aResp,
Nan Zhoude167a62022-06-01 04:47:45 +00002816 delegatedQuery, capabilities))
2817 {
2818 return;
2819 }
2820
2821 if (delegatedQuery.expandType != query_param::ExpandType::None)
2822 {
2823 // we perform efficient expand.
2824 auto asyncResp = std::make_shared<SensorsAsyncResp>(
2825 aResp, chassisId, sensors::dbus::sensorPaths,
2826 sensors::node::sensors,
2827 /*efficientExpand=*/true);
2828 getChassisData(asyncResp);
2829
2830 BMCWEB_LOG_DEBUG
2831 << "SensorCollection doGet exit via efficient expand handler";
2832 return;
Ed Tanous0bad3202022-06-02 13:53:59 -07002833 }
Nan Zhoude167a62022-06-01 04:47:45 +00002834
Nan Zhoude167a62022-06-01 04:47:45 +00002835 // We get all sensors as hyperlinkes in the chassis (this
2836 // implies we reply on the default query parameters handler)
Ed Tanousc1d019a2022-08-06 09:36:06 -07002837 getChassis(aResp, chassisId, sensors::node::sensors, dbus::sensorPaths,
2838 std::bind_front(sensors::getChassisCallback, aResp, chassisId,
2839 sensors::node::sensors));
2840}
Ed Tanous7f1cc262022-08-09 13:33:57 -07002841
Ed Tanousc1d019a2022-08-06 09:36:06 -07002842inline void
2843 getSensorFromDbus(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2844 const std::string& sensorPath,
2845 const ::dbus::utility::MapperGetObject& mapperResponse)
2846{
2847 if (mapperResponse.size() != 1)
2848 {
2849 messages::internalError(asyncResp->res);
2850 return;
2851 }
2852 const auto& valueIface = *mapperResponse.begin();
2853 const std::string& connectionName = valueIface.first;
2854 BMCWEB_LOG_DEBUG << "Looking up " << connectionName;
2855 BMCWEB_LOG_DEBUG << "Path " << sensorPath;
Krzysztof Grobelnyc1343bf2022-08-31 13:15:26 +02002856
2857 sdbusplus::asio::getAllProperties(
2858 *crow::connections::systemBus, connectionName, sensorPath, "",
Ed Tanousc1d019a2022-08-06 09:36:06 -07002859 [asyncResp,
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08002860 sensorPath](const boost::system::error_code& ec,
Ed Tanousc1d019a2022-08-06 09:36:06 -07002861 const ::dbus::utility::DBusPropertiesMap& valuesDict) {
2862 if (ec)
2863 {
2864 messages::internalError(asyncResp->res);
2865 return;
2866 }
2867 sdbusplus::message::object_path path(sensorPath);
2868 std::string name = path.filename();
2869 path = path.parent_path();
2870 std::string type = path.filename();
2871 objectPropertiesToJson(name, type, sensors::node::sensors, valuesDict,
2872 asyncResp->res.jsonValue, nullptr);
Krzysztof Grobelnyc1343bf2022-08-31 13:15:26 +02002873 });
Nan Zhoude167a62022-06-01 04:47:45 +00002874}
2875
Nan Zhoue6bd8462022-06-01 04:35:35 +00002876inline void handleSensorGet(App& app, const crow::Request& req,
Ed Tanousc1d019a2022-08-06 09:36:06 -07002877 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous677bb752022-09-15 10:52:19 -07002878 const std::string& chassisId,
Ed Tanousc1d019a2022-08-06 09:36:06 -07002879 const std::string& sensorId)
Nan Zhoue6bd8462022-06-01 04:35:35 +00002880{
Ed Tanousc1d019a2022-08-06 09:36:06 -07002881 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Nan Zhoue6bd8462022-06-01 04:35:35 +00002882 {
2883 return;
2884 }
Ed Tanousc71d6122022-11-29 14:10:32 -08002885 std::pair<std::string, std::string> nameType =
2886 splitSensorNameAndType(sensorId);
2887 if (nameType.first.empty() || nameType.second.empty())
Ed Tanousc1d019a2022-08-06 09:36:06 -07002888 {
2889 messages::resourceNotFound(asyncResp->res, sensorId, "Sensor");
2890 return;
2891 }
Ed Tanousc71d6122022-11-29 14:10:32 -08002892
Ed Tanous677bb752022-09-15 10:52:19 -07002893 asyncResp->res.jsonValue["@odata.id"] = crow::utility::urlFromPieces(
2894 "redfish", "v1", "Chassis", chassisId, "Sensors", sensorId);
Ed Tanousc1d019a2022-08-06 09:36:06 -07002895
Nan Zhoue6bd8462022-06-01 04:35:35 +00002896 BMCWEB_LOG_DEBUG << "Sensor doGet enter";
Nan Zhoue6bd8462022-06-01 04:35:35 +00002897
George Liu2b731192023-01-11 16:27:13 +08002898 constexpr std::array<std::string_view, 1> interfaces = {
Nan Zhoue6bd8462022-06-01 04:35:35 +00002899 "xyz.openbmc_project.Sensor.Value"};
Ed Tanousc71d6122022-11-29 14:10:32 -08002900 std::string sensorPath = "/xyz/openbmc_project/sensors/" + nameType.first +
2901 '/' + nameType.second;
Nan Zhoue6bd8462022-06-01 04:35:35 +00002902 // Get a list of all of the sensors that implement Sensor.Value
2903 // and get the path and service name associated with the sensor
George Liu2b731192023-01-11 16:27:13 +08002904 ::dbus::utility::getDbusObject(
2905 sensorPath, interfaces,
Ed Tanousc71d6122022-11-29 14:10:32 -08002906 [asyncResp,
George Liu2b731192023-01-11 16:27:13 +08002907 sensorPath](const boost::system::error_code& ec,
Ed Tanousc1d019a2022-08-06 09:36:06 -07002908 const ::dbus::utility::MapperGetObject& subtree) {
Nan Zhoue6bd8462022-06-01 04:35:35 +00002909 BMCWEB_LOG_DEBUG << "respHandler1 enter";
2910 if (ec)
2911 {
Ed Tanousc1d019a2022-08-06 09:36:06 -07002912 messages::internalError(asyncResp->res);
Nan Zhoue6bd8462022-06-01 04:35:35 +00002913 BMCWEB_LOG_ERROR << "Sensor getSensorPaths resp_handler: "
2914 << "Dbus error " << ec;
2915 return;
2916 }
Ed Tanousc1d019a2022-08-06 09:36:06 -07002917 getSensorFromDbus(asyncResp, sensorPath, subtree);
Nan Zhoue6bd8462022-06-01 04:35:35 +00002918 BMCWEB_LOG_DEBUG << "respHandler1 exit";
George Liu2b731192023-01-11 16:27:13 +08002919 });
Nan Zhoue6bd8462022-06-01 04:35:35 +00002920}
2921
Nan Zhoubacb2162022-04-06 11:28:32 -07002922} // namespace sensors
2923
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002924inline void requestRoutesSensorCollection(App& app)
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002925{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002926 BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/Sensors/")
Ed Tanoused398212021-06-09 17:05:54 -07002927 .privileges(redfish::privileges::getSensorCollection)
Ed Tanous002d39b2022-05-31 08:59:27 -07002928 .methods(boost::beast::http::verb::get)(
Nan Zhoude167a62022-06-01 04:47:45 +00002929 std::bind_front(sensors::handleSensorCollectionGet, std::ref(app)));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002930}
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002931
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002932inline void requestRoutesSensor(App& app)
2933{
2934 BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/Sensors/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07002935 .privileges(redfish::privileges::getSensor)
Ed Tanous002d39b2022-05-31 08:59:27 -07002936 .methods(boost::beast::http::verb::get)(
Nan Zhoue6bd8462022-06-01 04:35:35 +00002937 std::bind_front(sensors::handleSensorGet, std::ref(app)));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002938}
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002939
Ed Tanous1abe55e2018-09-05 08:30:59 -07002940} // namespace redfish