blob: 5bbb642cc4fa81ac180c00705d726da5391a1f86 [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 Tanous0ec8b832022-03-14 14:56:47 -070018#include "generated/enums/sensor.hpp"
19
John Edward Broadbent7e860f12021-04-08 15:57:16 -070020#include <app.hpp>
Ed Tanous11ba3972022-07-11 09:50:41 -070021#include <boost/algorithm/string/classification.hpp>
Ed Tanous1d7c0052022-08-09 12:32:26 -070022#include <boost/algorithm/string/find.hpp>
23#include <boost/algorithm/string/predicate.hpp>
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +010024#include <boost/algorithm/string/split.hpp>
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +010025#include <boost/range/algorithm/replace_copy_if.hpp>
Ed Tanous1abe55e2018-09-05 08:30:59 -070026#include <dbus_singleton.hpp>
Ed Tanous168e20c2021-12-13 14:39:53 -080027#include <dbus_utility.hpp>
Ed Tanous45ca1b82022-03-25 13:07:27 -070028#include <query.hpp>
Ed Tanoused398212021-06-09 17:05:54 -070029#include <registries/privilege_registry.hpp>
Jonathan Doman1e1e5982021-06-11 09:36:17 -070030#include <sdbusplus/asio/property.hpp>
Krzysztof Grobelny86d89ed2022-08-29 14:49:20 +020031#include <sdbusplus/unpack_properties.hpp>
32#include <utils/dbus_utils.hpp>
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +053033#include <utils/json_utils.hpp>
Nan Zhou928fefb2022-03-28 08:45:00 -070034#include <utils/query_param.hpp>
Gunnar Mills1214b7e2020-06-04 10:11:30 -050035
36#include <cmath>
Nan Zhoufe04d492022-06-22 17:10:41 +000037#include <iterator>
38#include <map>
39#include <set>
Ed Tanousb5a76932020-09-29 16:16:58 -070040#include <utility>
Ed Tanousabf2add2019-01-22 16:40:12 -080041#include <variant>
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +010042
Ed Tanous1abe55e2018-09-05 08:30:59 -070043namespace redfish
44{
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +010045
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +020046namespace sensors
47{
48namespace node
49{
50static constexpr std::string_view power = "Power";
51static constexpr std::string_view sensors = "Sensors";
52static constexpr std::string_view thermal = "Thermal";
53} // namespace node
54
Ed Tanous02da7c52022-02-27 00:09:02 -080055// clang-format off
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +020056namespace dbus
57{
Ed Tanous4ee8e212022-05-28 09:42:51 -070058static auto powerPaths = std::to_array<std::string_view>({
Ed Tanous02da7c52022-02-27 00:09:02 -080059 "/xyz/openbmc_project/sensors/voltage",
60 "/xyz/openbmc_project/sensors/power"
61});
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +000062
Ed Tanous4ee8e212022-05-28 09:42:51 -070063static auto sensorPaths = std::to_array<std::string_view>({
Ed Tanous02da7c52022-02-27 00:09:02 -080064 "/xyz/openbmc_project/sensors/power",
65 "/xyz/openbmc_project/sensors/current",
66 "/xyz/openbmc_project/sensors/airflow",
Ed Tanous4e777662022-08-06 09:39:13 -070067 "/xyz/openbmc_project/sensors/humidity",
George Liue8204932021-02-01 14:42:49 +080068#ifdef BMCWEB_NEW_POWERSUBSYSTEM_THERMALSUBSYSTEM
Ed Tanous02da7c52022-02-27 00:09:02 -080069 "/xyz/openbmc_project/sensors/voltage",
70 "/xyz/openbmc_project/sensors/fan_tach",
71 "/xyz/openbmc_project/sensors/temperature",
72 "/xyz/openbmc_project/sensors/fan_pwm",
73 "/xyz/openbmc_project/sensors/altitude",
74 "/xyz/openbmc_project/sensors/energy",
George Liue8204932021-02-01 14:42:49 +080075#endif
Ed Tanous02da7c52022-02-27 00:09:02 -080076 "/xyz/openbmc_project/sensors/utilization"
77});
78
Ed Tanous4ee8e212022-05-28 09:42:51 -070079static auto thermalPaths = std::to_array<std::string_view>({
Ed Tanous02da7c52022-02-27 00:09:02 -080080 "/xyz/openbmc_project/sensors/fan_tach",
81 "/xyz/openbmc_project/sensors/temperature",
82 "/xyz/openbmc_project/sensors/fan_pwm"
83});
84
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +000085} // namespace dbus
Ed Tanous02da7c52022-02-27 00:09:02 -080086// clang-format on
87
88using sensorPair = std::pair<std::string_view, std::span<std::string_view>>;
89static constexpr std::array<sensorPair, 3> paths = {
90 {{node::power, std::span<std::string_view>(dbus::powerPaths)},
91 {node::sensors, std::span<std::string_view>(dbus::sensorPaths)},
92 {node::thermal, std::span<std::string_view>(dbus::thermalPaths)}}};
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +000093
Ed Tanous0ec8b832022-03-14 14:56:47 -070094inline sensor::ReadingType toReadingType(std::string_view sensorType)
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +000095{
96 if (sensorType == "voltage")
97 {
Ed Tanous0ec8b832022-03-14 14:56:47 -070098 return sensor::ReadingType::Voltage;
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +000099 }
100 if (sensorType == "power")
101 {
Ed Tanous0ec8b832022-03-14 14:56:47 -0700102 return sensor::ReadingType::Power;
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000103 }
104 if (sensorType == "current")
105 {
Ed Tanous0ec8b832022-03-14 14:56:47 -0700106 return sensor::ReadingType::Current;
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000107 }
108 if (sensorType == "fan_tach")
109 {
Ed Tanous0ec8b832022-03-14 14:56:47 -0700110 return sensor::ReadingType::Rotational;
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000111 }
112 if (sensorType == "temperature")
113 {
Ed Tanous0ec8b832022-03-14 14:56:47 -0700114 return sensor::ReadingType::Temperature;
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000115 }
116 if (sensorType == "fan_pwm" || sensorType == "utilization")
117 {
Ed Tanous0ec8b832022-03-14 14:56:47 -0700118 return sensor::ReadingType::Percent;
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000119 }
Gunnar Mills5deabed2022-04-20 13:43:45 -0600120 if (sensorType == "humidity")
121 {
Ed Tanous0ec8b832022-03-14 14:56:47 -0700122 return sensor::ReadingType::Humidity;
Gunnar Mills5deabed2022-04-20 13:43:45 -0600123 }
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000124 if (sensorType == "altitude")
125 {
Ed Tanous0ec8b832022-03-14 14:56:47 -0700126 return sensor::ReadingType::Altitude;
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000127 }
128 if (sensorType == "airflow")
129 {
Ed Tanous0ec8b832022-03-14 14:56:47 -0700130 return sensor::ReadingType::AirFlow;
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000131 }
132 if (sensorType == "energy")
133 {
Ed Tanous0ec8b832022-03-14 14:56:47 -0700134 return sensor::ReadingType::EnergyJoules;
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000135 }
Ed Tanous0ec8b832022-03-14 14:56:47 -0700136 return sensor::ReadingType::Invalid;
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000137}
138
Ed Tanous1d7c0052022-08-09 12:32:26 -0700139inline std::string_view toReadingUnits(std::string_view sensorType)
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000140{
141 if (sensorType == "voltage")
142 {
143 return "V";
144 }
145 if (sensorType == "power")
146 {
147 return "W";
148 }
149 if (sensorType == "current")
150 {
151 return "A";
152 }
153 if (sensorType == "fan_tach")
154 {
155 return "RPM";
156 }
157 if (sensorType == "temperature")
158 {
159 return "Cel";
160 }
Gunnar Mills5deabed2022-04-20 13:43:45 -0600161 if (sensorType == "fan_pwm" || sensorType == "utilization" ||
162 sensorType == "humidity")
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000163 {
164 return "%";
165 }
166 if (sensorType == "altitude")
167 {
168 return "m";
169 }
170 if (sensorType == "airflow")
171 {
172 return "cft_i/min";
173 }
174 if (sensorType == "energy")
175 {
176 return "J";
177 }
178 return "";
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200179}
180} // namespace sensors
181
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100182/**
Kowalski, Kamil588c3f02018-04-03 14:55:27 +0200183 * SensorsAsyncResp
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100184 * Gathers data needed for response processing after async calls are done
185 */
Ed Tanous1abe55e2018-09-05 08:30:59 -0700186class SensorsAsyncResp
187{
188 public:
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200189 using DataCompleteCb = std::function<void(
190 const boost::beast::http::status status,
Nan Zhoufe04d492022-06-22 17:10:41 +0000191 const std::map<std::string, std::string>& uriToDbus)>;
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200192
193 struct SensorData
194 {
195 const std::string name;
196 std::string uri;
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200197 const std::string dbusPath;
198 };
199
Ed Tanous8a592812022-06-04 09:06:59 -0700200 SensorsAsyncResp(const std::shared_ptr<bmcweb::AsyncResp>& asyncRespIn,
zhanghch058d1b46d2021-04-01 11:18:24 +0800201 const std::string& chassisIdIn,
Ed Tanous02da7c52022-02-27 00:09:02 -0800202 std::span<std::string_view> typesIn,
203 std::string_view subNode) :
Ed Tanous8a592812022-06-04 09:06:59 -0700204 asyncResp(asyncRespIn),
Nan Zhou928fefb2022-03-28 08:45:00 -0700205 chassisId(chassisIdIn), types(typesIn), chassisSubNode(subNode),
206 efficientExpand(false)
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500207 {}
Kowalski, Kamil588c3f02018-04-03 14:55:27 +0200208
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200209 // Store extra data about sensor mapping and return it in callback
Ed Tanous8a592812022-06-04 09:06:59 -0700210 SensorsAsyncResp(const std::shared_ptr<bmcweb::AsyncResp>& asyncRespIn,
zhanghch058d1b46d2021-04-01 11:18:24 +0800211 const std::string& chassisIdIn,
Ed Tanous02da7c52022-02-27 00:09:02 -0800212 std::span<std::string_view> typesIn,
213 std::string_view subNode,
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200214 DataCompleteCb&& creationComplete) :
Ed Tanous8a592812022-06-04 09:06:59 -0700215 asyncResp(asyncRespIn),
Nan Zhou928fefb2022-03-28 08:45:00 -0700216 chassisId(chassisIdIn), types(typesIn), chassisSubNode(subNode),
217 efficientExpand(false), metadata{std::vector<SensorData>()},
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200218 dataComplete{std::move(creationComplete)}
219 {}
220
Nan Zhou928fefb2022-03-28 08:45:00 -0700221 // sensor collections expand
Ed Tanous8a592812022-06-04 09:06:59 -0700222 SensorsAsyncResp(const std::shared_ptr<bmcweb::AsyncResp>& asyncRespIn,
Nan Zhou928fefb2022-03-28 08:45:00 -0700223 const std::string& chassisIdIn,
Ed Tanous02da7c52022-02-27 00:09:02 -0800224 const std::span<std::string_view> typesIn,
Ed Tanous8a592812022-06-04 09:06:59 -0700225 const std::string_view& subNode, bool efficientExpandIn) :
226 asyncResp(asyncRespIn),
Nan Zhou928fefb2022-03-28 08:45:00 -0700227 chassisId(chassisIdIn), types(typesIn), chassisSubNode(subNode),
Ed Tanous8a592812022-06-04 09:06:59 -0700228 efficientExpand(efficientExpandIn)
Nan Zhou928fefb2022-03-28 08:45:00 -0700229 {}
230
Ed Tanous1abe55e2018-09-05 08:30:59 -0700231 ~SensorsAsyncResp()
232 {
zhanghch058d1b46d2021-04-01 11:18:24 +0800233 if (asyncResp->res.result() ==
234 boost::beast::http::status::internal_server_error)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700235 {
236 // Reset the json object to clear out any data that made it in
237 // before the error happened todo(ed) handle error condition with
238 // proper code
zhanghch058d1b46d2021-04-01 11:18:24 +0800239 asyncResp->res.jsonValue = nlohmann::json::object();
Ed Tanous1abe55e2018-09-05 08:30:59 -0700240 }
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200241
242 if (dataComplete && metadata)
243 {
Nan Zhoufe04d492022-06-22 17:10:41 +0000244 std::map<std::string, std::string> map;
zhanghch058d1b46d2021-04-01 11:18:24 +0800245 if (asyncResp->res.result() == boost::beast::http::status::ok)
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200246 {
247 for (auto& sensor : *metadata)
248 {
Ed Tanousc1d019a2022-08-06 09:36:06 -0700249 map.emplace(sensor.uri, sensor.dbusPath);
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200250 }
251 }
zhanghch058d1b46d2021-04-01 11:18:24 +0800252 dataComplete(asyncResp->res.result(), map);
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200253 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700254 }
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100255
Ed Tanousecd6a3a2022-01-07 09:18:40 -0800256 SensorsAsyncResp(const SensorsAsyncResp&) = delete;
257 SensorsAsyncResp(SensorsAsyncResp&&) = delete;
258 SensorsAsyncResp& operator=(const SensorsAsyncResp&) = delete;
259 SensorsAsyncResp& operator=(SensorsAsyncResp&&) = delete;
260
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200261 void addMetadata(const nlohmann::json& sensorObject,
Ed Tanousc1d019a2022-08-06 09:36:06 -0700262 const std::string& dbusPath)
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200263 {
264 if (metadata)
265 {
Ed Tanousc1d019a2022-08-06 09:36:06 -0700266 metadata->emplace_back(SensorData{
267 sensorObject["Name"], sensorObject["@odata.id"], dbusPath});
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200268 }
269 }
270
271 void updateUri(const std::string& name, const std::string& uri)
272 {
273 if (metadata)
274 {
275 for (auto& sensor : *metadata)
276 {
277 if (sensor.name == name)
278 {
279 sensor.uri = uri;
280 }
281 }
282 }
283 }
284
zhanghch058d1b46d2021-04-01 11:18:24 +0800285 const std::shared_ptr<bmcweb::AsyncResp> asyncResp;
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200286 const std::string chassisId;
Ed Tanous02da7c52022-02-27 00:09:02 -0800287 const std::span<std::string_view> types;
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200288 const std::string chassisSubNode;
Nan Zhou928fefb2022-03-28 08:45:00 -0700289 const bool efficientExpand;
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200290
291 private:
292 std::optional<std::vector<SensorData>> metadata;
293 DataCompleteCb dataComplete;
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100294};
295
296/**
Anthony Wilsond5005492019-07-31 16:34:17 -0500297 * Possible states for physical inventory leds
298 */
299enum class LedState
300{
301 OFF,
302 ON,
303 BLINK,
304 UNKNOWN
305};
306
307/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500308 * D-Bus inventory item associated with one or more sensors.
309 */
310class InventoryItem
311{
312 public:
Ed Tanous4e23a442022-06-06 09:57:26 -0700313 explicit InventoryItem(const std::string& objPath) : objectPath(objPath)
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500314 {
315 // Set inventory item name to last node of object path
George Liu28aa8de2021-02-01 15:13:30 +0800316 sdbusplus::message::object_path path(objectPath);
317 name = path.filename();
318 if (name.empty())
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500319 {
George Liu28aa8de2021-02-01 15:13:30 +0800320 BMCWEB_LOG_ERROR << "Failed to find '/' in " << objectPath;
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500321 }
322 }
323
324 std::string objectPath;
325 std::string name;
Ed Tanouse05aec52022-01-25 10:28:56 -0800326 bool isPresent = true;
327 bool isFunctional = true;
328 bool isPowerSupply = false;
329 int powerSupplyEfficiencyPercent = -1;
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500330 std::string manufacturer;
331 std::string model;
332 std::string partNumber;
333 std::string serialNumber;
334 std::set<std::string> sensors;
Anthony Wilsond5005492019-07-31 16:34:17 -0500335 std::string ledObjectPath;
Ed Tanouse05aec52022-01-25 10:28:56 -0800336 LedState ledState = LedState::UNKNOWN;
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500337};
338
339/**
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530340 * @brief Get objects with connection necessary for sensors
Kowalski, Kamil588c3f02018-04-03 14:55:27 +0200341 * @param SensorsAsyncResp Pointer to object holding response data
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100342 * @param sensorNames Sensors retrieved from chassis
343 * @param callback Callback for processing gathered connections
344 */
345template <typename Callback>
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530346void getObjectsWithConnection(
Ed Tanous81ce6092020-12-17 16:54:55 +0000347 const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
Nan Zhoufe04d492022-06-22 17:10:41 +0000348 const std::shared_ptr<std::set<std::string>>& sensorNames,
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530349 Callback&& callback)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700350{
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530351 BMCWEB_LOG_DEBUG << "getObjectsWithConnection enter";
Ed Tanous1abe55e2018-09-05 08:30:59 -0700352 const std::string path = "/xyz/openbmc_project/sensors";
353 const std::array<std::string, 1> interfaces = {
354 "xyz.openbmc_project.Sensor.Value"};
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100355
Ed Tanous1abe55e2018-09-05 08:30:59 -0700356 // Response handler for parsing objects subtree
Ed Tanous002d39b2022-05-31 08:59:27 -0700357 auto respHandler =
358 [callback{std::forward<Callback>(callback)}, sensorsAsyncResp,
359 sensorNames](const boost::system::error_code ec,
360 const dbus::utility::MapperGetSubTreeResponse& subtree) {
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530361 BMCWEB_LOG_DEBUG << "getObjectsWithConnection resp_handler enter";
Ed Tanous1abe55e2018-09-05 08:30:59 -0700362 if (ec)
363 {
zhanghch058d1b46d2021-04-01 11:18:24 +0800364 messages::internalError(sensorsAsyncResp->asyncResp->res);
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530365 BMCWEB_LOG_ERROR
366 << "getObjectsWithConnection resp_handler: Dbus error " << ec;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700367 return;
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100368 }
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100369
Ed Tanous1abe55e2018-09-05 08:30:59 -0700370 BMCWEB_LOG_DEBUG << "Found " << subtree.size() << " subtrees";
371
372 // Make unique list of connections only for requested sensor types and
373 // found in the chassis
Nan Zhoufe04d492022-06-22 17:10:41 +0000374 std::set<std::string> connections;
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530375 std::set<std::pair<std::string, std::string>> objectsWithConnection;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700376
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700377 BMCWEB_LOG_DEBUG << "sensorNames list count: " << sensorNames->size();
378 for (const std::string& tsensor : *sensorNames)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700379 {
380 BMCWEB_LOG_DEBUG << "Sensor to find: " << tsensor;
381 }
382
383 for (const std::pair<
384 std::string,
385 std::vector<std::pair<std::string, std::vector<std::string>>>>&
386 object : subtree)
387 {
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700388 if (sensorNames->find(object.first) != sensorNames->end())
Ed Tanous1abe55e2018-09-05 08:30:59 -0700389 {
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700390 for (const std::pair<std::string, std::vector<std::string>>&
391 objData : object.second)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700392 {
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700393 BMCWEB_LOG_DEBUG << "Adding connection: " << objData.first;
394 connections.insert(objData.first);
395 objectsWithConnection.insert(
396 std::make_pair(object.first, objData.first));
Ed Tanous1abe55e2018-09-05 08:30:59 -0700397 }
398 }
399 }
400 BMCWEB_LOG_DEBUG << "Found " << connections.size() << " connections";
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530401 callback(std::move(connections), std::move(objectsWithConnection));
402 BMCWEB_LOG_DEBUG << "getObjectsWithConnection resp_handler exit";
Ed Tanous1abe55e2018-09-05 08:30:59 -0700403 };
Ed Tanous1abe55e2018-09-05 08:30:59 -0700404 // Make call to ObjectMapper to find all sensors objects
405 crow::connections::systemBus->async_method_call(
406 std::move(respHandler), "xyz.openbmc_project.ObjectMapper",
407 "/xyz/openbmc_project/object_mapper",
408 "xyz.openbmc_project.ObjectMapper", "GetSubTree", path, 2, interfaces);
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530409 BMCWEB_LOG_DEBUG << "getObjectsWithConnection exit";
410}
411
412/**
413 * @brief Create connections necessary for sensors
414 * @param SensorsAsyncResp Pointer to object holding response data
415 * @param sensorNames Sensors retrieved from chassis
416 * @param callback Callback for processing gathered connections
417 */
418template <typename Callback>
Nan Zhoufe04d492022-06-22 17:10:41 +0000419void getConnections(std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
420 const std::shared_ptr<std::set<std::string>> sensorNames,
421 Callback&& callback)
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530422{
423 auto objectsWithConnectionCb =
Nan Zhoufe04d492022-06-22 17:10:41 +0000424 [callback](const std::set<std::string>& connections,
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530425 const std::set<std::pair<std::string, std::string>>&
Ed Tanous3174e4d2020-10-07 11:41:22 -0700426 /*objectsWithConnection*/) { callback(connections); };
Ed Tanous81ce6092020-12-17 16:54:55 +0000427 getObjectsWithConnection(sensorsAsyncResp, sensorNames,
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530428 std::move(objectsWithConnectionCb));
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100429}
430
431/**
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700432 * @brief Shrinks the list of sensors for processing
433 * @param SensorsAysncResp The class holding the Redfish response
434 * @param allSensors A list of all the sensors associated to the
435 * chassis element (i.e. baseboard, front panel, etc...)
436 * @param activeSensors A list that is a reduction of the incoming
437 * allSensors list. Eliminate Thermal sensors when a Power request is
438 * made, and eliminate Power sensors when a Thermal request is made.
439 */
Ed Tanous23a21a12020-07-25 04:45:05 +0000440inline void reduceSensorList(
Ed Tanous7f1cc262022-08-09 13:33:57 -0700441 crow::Response& res, std::string_view chassisSubNode,
442 std::span<std::string_view> sensorTypes,
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700443 const std::vector<std::string>* allSensors,
Nan Zhoufe04d492022-06-22 17:10:41 +0000444 const std::shared_ptr<std::set<std::string>>& activeSensors)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700445{
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700446 if ((allSensors == nullptr) || (activeSensors == nullptr))
447 {
Ed Tanous7f1cc262022-08-09 13:33:57 -0700448 messages::resourceNotFound(res, chassisSubNode,
449 chassisSubNode == sensors::node::thermal
450 ? "Temperatures"
451 : "Voltages");
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700452
453 return;
454 }
455 if (allSensors->empty())
456 {
457 // Nothing to do, the activeSensors object is also empty
458 return;
459 }
460
Ed Tanous7f1cc262022-08-09 13:33:57 -0700461 for (std::string_view type : sensorTypes)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700462 {
463 for (const std::string& sensor : *allSensors)
464 {
Ed Tanous11ba3972022-07-11 09:50:41 -0700465 if (sensor.starts_with(type))
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700466 {
467 activeSensors->emplace(sensor);
468 }
469 }
470 }
471}
472
Ed Tanous7f1cc262022-08-09 13:33:57 -0700473/*
474 *Populates the top level collection for a given subnode. Populates
475 *SensorCollection, Power, or Thermal schemas.
476 *
477 * */
478inline void populateChassisNode(nlohmann::json& jsonValue,
479 std::string_view chassisSubNode)
480{
481 if (chassisSubNode == sensors::node::power)
482 {
483 jsonValue["@odata.type"] = "#Power.v1_5_2.Power";
484 }
485 else if (chassisSubNode == sensors::node::thermal)
486 {
487 jsonValue["@odata.type"] = "#Thermal.v1_4_0.Thermal";
488 jsonValue["Fans"] = nlohmann::json::array();
489 jsonValue["Temperatures"] = nlohmann::json::array();
490 }
491 else if (chassisSubNode == sensors::node::sensors)
492 {
493 jsonValue["@odata.type"] = "#SensorCollection.SensorCollection";
494 jsonValue["Description"] = "Collection of Sensors for this Chassis";
495 jsonValue["Members"] = nlohmann::json::array();
496 jsonValue["Members@odata.count"] = 0;
497 }
498
499 if (chassisSubNode != sensors::node::sensors)
500 {
501 jsonValue["Id"] = chassisSubNode;
502 }
503 jsonValue["Name"] = chassisSubNode;
504}
505
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700506/**
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100507 * @brief Retrieves requested chassis sensors and redundancy data from DBus .
Kowalski, Kamil588c3f02018-04-03 14:55:27 +0200508 * @param SensorsAsyncResp Pointer to object holding response data
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100509 * @param callback Callback for next step in gathered sensor processing
510 */
511template <typename Callback>
Ed Tanous7f1cc262022-08-09 13:33:57 -0700512void getChassis(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
513 std::string_view chassisId, std::string_view chassisSubNode,
514 std::span<std::string_view> sensorTypes, Callback&& callback)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700515{
516 BMCWEB_LOG_DEBUG << "getChassis enter";
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500517 const std::array<const char*, 2> interfaces = {
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700518 "xyz.openbmc_project.Inventory.Item.Board",
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500519 "xyz.openbmc_project.Inventory.Item.Chassis"};
Ed Tanous002d39b2022-05-31 08:59:27 -0700520 auto respHandler =
Ed Tanous7f1cc262022-08-09 13:33:57 -0700521 [callback{std::forward<Callback>(callback)}, asyncResp,
522 chassisIdStr{std::string(chassisId)},
523 chassisSubNode{std::string(chassisSubNode)}, sensorTypes](
Ed Tanous002d39b2022-05-31 08:59:27 -0700524 const boost::system::error_code ec,
525 const dbus::utility::MapperGetSubTreePathsResponse& chassisPaths) {
Ed Tanous1abe55e2018-09-05 08:30:59 -0700526 BMCWEB_LOG_DEBUG << "getChassis respHandler enter";
527 if (ec)
528 {
529 BMCWEB_LOG_ERROR << "getChassis respHandler DBUS error: " << ec;
Ed Tanous7f1cc262022-08-09 13:33:57 -0700530 messages::internalError(asyncResp->res);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700531 return;
532 }
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700533 const std::string* chassisPath = nullptr;
534 std::string chassisName;
535 for (const std::string& chassis : chassisPaths)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700536 {
George Liu28aa8de2021-02-01 15:13:30 +0800537 sdbusplus::message::object_path path(chassis);
538 chassisName = path.filename();
539 if (chassisName.empty())
Ed Tanous1abe55e2018-09-05 08:30:59 -0700540 {
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700541 BMCWEB_LOG_ERROR << "Failed to find '/' in " << chassis;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700542 continue;
543 }
Ed Tanous7f1cc262022-08-09 13:33:57 -0700544 if (chassisName == chassisIdStr)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700545 {
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700546 chassisPath = &chassis;
547 break;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700548 }
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700549 }
550 if (chassisPath == nullptr)
551 {
Ed Tanous7f1cc262022-08-09 13:33:57 -0700552 messages::resourceNotFound(asyncResp->res, "Chassis", chassisIdStr);
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700553 return;
554 }
Ed Tanous7f1cc262022-08-09 13:33:57 -0700555 populateChassisNode(asyncResp->res.jsonValue, chassisSubNode);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700556
Ed Tanous7f1cc262022-08-09 13:33:57 -0700557 asyncResp->res.jsonValue["@odata.id"] =
558 "/redfish/v1/Chassis/" + chassisIdStr + "/" + chassisSubNode;
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500559
Shawn McCarney8fb49dd2019-06-12 17:47:00 -0500560 // Get the list of all sensors for this Chassis element
561 std::string sensorPath = *chassisPath + "/all_sensors";
Jonathan Doman1e1e5982021-06-11 09:36:17 -0700562 sdbusplus::asio::getProperty<std::vector<std::string>>(
563 *crow::connections::systemBus, "xyz.openbmc_project.ObjectMapper",
564 sensorPath, "xyz.openbmc_project.Association", "endpoints",
Ed Tanous7f1cc262022-08-09 13:33:57 -0700565 [asyncResp, chassisSubNode, sensorTypes,
Ed Tanousf94c4ec2022-01-06 12:44:41 -0800566 callback{std::forward<const Callback>(callback)}](
Ed Tanous271584a2019-07-09 16:24:22 -0700567 const boost::system::error_code& e,
Jonathan Doman1e1e5982021-06-11 09:36:17 -0700568 const std::vector<std::string>& nodeSensorList) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700569 if (e)
570 {
571 if (e.value() != EBADR)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700572 {
Ed Tanous7f1cc262022-08-09 13:33:57 -0700573 messages::internalError(asyncResp->res);
Ed Tanous002d39b2022-05-31 08:59:27 -0700574 return;
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700575 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700576 }
Nan Zhoufe04d492022-06-22 17:10:41 +0000577 const std::shared_ptr<std::set<std::string>> culledSensorList =
578 std::make_shared<std::set<std::string>>();
Ed Tanous7f1cc262022-08-09 13:33:57 -0700579 reduceSensorList(asyncResp->res, chassisSubNode, sensorTypes,
580 &nodeSensorList, culledSensorList);
581 BMCWEB_LOG_DEBUG << "Finishing with " << culledSensorList->size();
Ed Tanous002d39b2022-05-31 08:59:27 -0700582 callback(culledSensorList);
Jonathan Doman1e1e5982021-06-11 09:36:17 -0700583 });
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100584 };
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100585
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700586 // Get the Chassis Collection
Ed Tanous1abe55e2018-09-05 08:30:59 -0700587 crow::connections::systemBus->async_method_call(
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700588 respHandler, "xyz.openbmc_project.ObjectMapper",
589 "/xyz/openbmc_project/object_mapper",
590 "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths",
Ed Tanous271584a2019-07-09 16:24:22 -0700591 "/xyz/openbmc_project/inventory", 0, interfaces);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700592 BMCWEB_LOG_DEBUG << "getChassis exit";
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100593}
594
595/**
Shawn McCarneyde629b62019-03-08 10:42:51 -0600596 * @brief Finds all DBus object paths that implement ObjectManager.
597 *
598 * Creates a mapping from the associated connection name to the object path.
599 *
600 * Finds the object paths asynchronously. Invokes callback when information has
601 * been obtained.
602 *
603 * The callback must have the following signature:
604 * @code
Nan Zhoufe04d492022-06-22 17:10:41 +0000605 * callback(std::shared_ptr<std::map<std::string,std::string>> objectMgrPaths)
Shawn McCarneyde629b62019-03-08 10:42:51 -0600606 * @endcode
607 *
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700608 * @param sensorsAsyncResp Pointer to object holding response data.
Shawn McCarneyde629b62019-03-08 10:42:51 -0600609 * @param callback Callback to invoke when object paths obtained.
610 */
611template <typename Callback>
Ed Tanousb5a76932020-09-29 16:16:58 -0700612void getObjectManagerPaths(
Ed Tanous81ce6092020-12-17 16:54:55 +0000613 const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
Ed Tanousb5a76932020-09-29 16:16:58 -0700614 Callback&& callback)
Shawn McCarneyde629b62019-03-08 10:42:51 -0600615{
616 BMCWEB_LOG_DEBUG << "getObjectManagerPaths enter";
617 const std::array<std::string, 1> interfaces = {
618 "org.freedesktop.DBus.ObjectManager"};
619
620 // Response handler for GetSubTree DBus method
Ed Tanous002d39b2022-05-31 08:59:27 -0700621 auto respHandler =
622 [callback{std::forward<Callback>(callback)}, sensorsAsyncResp](
623 const boost::system::error_code ec,
624 const dbus::utility::MapperGetSubTreeResponse& subtree) {
Shawn McCarneyde629b62019-03-08 10:42:51 -0600625 BMCWEB_LOG_DEBUG << "getObjectManagerPaths respHandler enter";
626 if (ec)
627 {
zhanghch058d1b46d2021-04-01 11:18:24 +0800628 messages::internalError(sensorsAsyncResp->asyncResp->res);
Shawn McCarneyde629b62019-03-08 10:42:51 -0600629 BMCWEB_LOG_ERROR << "getObjectManagerPaths respHandler: DBus error "
630 << ec;
631 return;
632 }
633
634 // Loop over returned object paths
Nan Zhoufe04d492022-06-22 17:10:41 +0000635 std::shared_ptr<std::map<std::string, std::string>> objectMgrPaths =
636 std::make_shared<std::map<std::string, std::string>>();
Shawn McCarneyde629b62019-03-08 10:42:51 -0600637 for (const std::pair<
638 std::string,
639 std::vector<std::pair<std::string, std::vector<std::string>>>>&
640 object : subtree)
641 {
642 // Loop over connections for current object path
643 const std::string& objectPath = object.first;
644 for (const std::pair<std::string, std::vector<std::string>>&
645 objData : object.second)
646 {
647 // Add mapping from connection to object path
648 const std::string& connection = objData.first;
Shawn McCarney8fb49dd2019-06-12 17:47:00 -0500649 (*objectMgrPaths)[connection] = objectPath;
Shawn McCarneyde629b62019-03-08 10:42:51 -0600650 BMCWEB_LOG_DEBUG << "Added mapping " << connection << " -> "
651 << objectPath;
652 }
653 }
Shawn McCarney8fb49dd2019-06-12 17:47:00 -0500654 callback(objectMgrPaths);
Shawn McCarneyde629b62019-03-08 10:42:51 -0600655 BMCWEB_LOG_DEBUG << "getObjectManagerPaths respHandler exit";
656 };
657
658 // Query mapper for all DBus object paths that implement ObjectManager
659 crow::connections::systemBus->async_method_call(
660 std::move(respHandler), "xyz.openbmc_project.ObjectMapper",
661 "/xyz/openbmc_project/object_mapper",
Ed Tanous271584a2019-07-09 16:24:22 -0700662 "xyz.openbmc_project.ObjectMapper", "GetSubTree", "/", 0, interfaces);
Shawn McCarneyde629b62019-03-08 10:42:51 -0600663 BMCWEB_LOG_DEBUG << "getObjectManagerPaths exit";
664}
665
666/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500667 * @brief Returns the Redfish State value for the specified inventory item.
668 * @param inventoryItem D-Bus inventory item associated with a sensor.
669 * @return State value for inventory item.
James Feist34dd1792019-05-17 14:10:54 -0700670 */
Ed Tanous23a21a12020-07-25 04:45:05 +0000671inline std::string getState(const InventoryItem* inventoryItem)
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500672{
673 if ((inventoryItem != nullptr) && !(inventoryItem->isPresent))
674 {
675 return "Absent";
676 }
James Feist34dd1792019-05-17 14:10:54 -0700677
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500678 return "Enabled";
679}
680
681/**
682 * @brief Returns the Redfish Health value for the specified sensor.
683 * @param sensorJson Sensor JSON object.
Ed Tanous1d7c0052022-08-09 12:32:26 -0700684 * @param valuesDict Map of all sensor DBus values.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500685 * @param inventoryItem D-Bus inventory item associated with the sensor. Will
686 * be nullptr if no associated inventory item was found.
687 * @return Health value for sensor.
688 */
Ed Tanous1d7c0052022-08-09 12:32:26 -0700689inline std::string getHealth(nlohmann::json& sensorJson,
690 const dbus::utility::DBusPropertiesMap& valuesDict,
691 const InventoryItem* inventoryItem)
James Feist34dd1792019-05-17 14:10:54 -0700692{
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500693 // Get current health value (if any) in the sensor JSON object. Some JSON
694 // objects contain multiple sensors (such as PowerSupplies). We want to set
695 // the overall health to be the most severe of any of the sensors.
696 std::string currentHealth;
697 auto statusIt = sensorJson.find("Status");
698 if (statusIt != sensorJson.end())
699 {
700 auto healthIt = statusIt->find("Health");
701 if (healthIt != statusIt->end())
702 {
703 std::string* health = healthIt->get_ptr<std::string*>();
704 if (health != nullptr)
705 {
706 currentHealth = *health;
707 }
708 }
709 }
710
711 // If current health in JSON object is already Critical, return that. This
712 // should override the sensor health, which might be less severe.
713 if (currentHealth == "Critical")
714 {
715 return "Critical";
716 }
717
Krzysztof Grobelnyc1343bf2022-08-31 13:15:26 +0200718 const bool* criticalAlarmHigh = nullptr;
719 const bool* criticalAlarmLow = nullptr;
720 const bool* warningAlarmHigh = nullptr;
721 const bool* warningAlarmLow = nullptr;
Ed Tanous711ac7a2021-12-20 09:34:41 -0800722
Krzysztof Grobelnyc1343bf2022-08-31 13:15:26 +0200723 const bool success = sdbusplus::unpackPropertiesNoThrow(
724 dbus_utils::UnpackErrorPrinter(), valuesDict, "CriticalAlarmHigh",
725 criticalAlarmHigh, "CriticalAlarmLow", criticalAlarmLow,
726 "WarningAlarmHigh", warningAlarmHigh, "WarningAlarmLow",
727 warningAlarmLow);
728
729 if (success)
James Feist34dd1792019-05-17 14:10:54 -0700730 {
Krzysztof Grobelnyc1343bf2022-08-31 13:15:26 +0200731 // Check if sensor has critical threshold alarm
732 if ((criticalAlarmHigh != nullptr && *criticalAlarmHigh) ||
733 (criticalAlarmLow != nullptr && *criticalAlarmLow))
James Feist34dd1792019-05-17 14:10:54 -0700734 {
Krzysztof Grobelnyc1343bf2022-08-31 13:15:26 +0200735 return "Critical";
James Feist34dd1792019-05-17 14:10:54 -0700736 }
737 }
738
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500739 // Check if associated inventory item is not functional
740 if ((inventoryItem != nullptr) && !(inventoryItem->isFunctional))
741 {
742 return "Critical";
743 }
744
Krzysztof Grobelnyc1343bf2022-08-31 13:15:26 +0200745 // If current health in JSON object is already Warning, return that. This
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500746 // should override the sensor status, which might be less severe.
747 if (currentHealth == "Warning")
748 {
749 return "Warning";
750 }
751
Krzysztof Grobelnyc1343bf2022-08-31 13:15:26 +0200752 if (success)
James Feist34dd1792019-05-17 14:10:54 -0700753 {
Krzysztof Grobelnyc1343bf2022-08-31 13:15:26 +0200754 // Check if sensor has warning threshold alarm
755 if ((warningAlarmHigh != nullptr && *warningAlarmHigh) ||
756 (warningAlarmLow != nullptr && *warningAlarmLow))
James Feist34dd1792019-05-17 14:10:54 -0700757 {
Krzysztof Grobelnyc1343bf2022-08-31 13:15:26 +0200758 return "Warning";
James Feist34dd1792019-05-17 14:10:54 -0700759 }
760 }
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500761
James Feist34dd1792019-05-17 14:10:54 -0700762 return "OK";
763}
764
Ed Tanous23a21a12020-07-25 04:45:05 +0000765inline void setLedState(nlohmann::json& sensorJson,
Anthony Wilsond5005492019-07-31 16:34:17 -0500766 const InventoryItem* inventoryItem)
767{
768 if (inventoryItem != nullptr && !inventoryItem->ledObjectPath.empty())
769 {
770 switch (inventoryItem->ledState)
771 {
772 case LedState::OFF:
773 sensorJson["IndicatorLED"] = "Off";
774 break;
775 case LedState::ON:
776 sensorJson["IndicatorLED"] = "Lit";
777 break;
778 case LedState::BLINK:
779 sensorJson["IndicatorLED"] = "Blinking";
780 break;
Ed Tanous23a21a12020-07-25 04:45:05 +0000781 case LedState::UNKNOWN:
Anthony Wilsond5005492019-07-31 16:34:17 -0500782 break;
783 }
784 }
785}
786
James Feist34dd1792019-05-17 14:10:54 -0700787/**
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100788 * @brief Builds a json sensor representation of a sensor.
789 * @param sensorName The name of the sensor to be built
Gunnar Mills274fad52018-06-13 15:45:36 -0500790 * @param sensorType The type (temperature, fan_tach, etc) of the sensor to
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100791 * build
Ed Tanous1d7c0052022-08-09 12:32:26 -0700792 * @param chassisSubNode The subnode (thermal, sensor, ect) of the sensor
793 * @param propertiesDict A dictionary of the properties to build the sensor
794 * from.
795 * @param sensorJson The json object to fill
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500796 * @param inventoryItem D-Bus inventory item associated with the sensor. Will
797 * be nullptr if no associated inventory item was found.
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100798 */
Ed Tanous1d7c0052022-08-09 12:32:26 -0700799inline void objectPropertiesToJson(
800 std::string_view sensorName, std::string_view sensorType,
801 std::string_view chassisSubNode,
802 const dbus::utility::DBusPropertiesMap& propertiesDict,
Ed Tanous81ce6092020-12-17 16:54:55 +0000803 nlohmann::json& sensorJson, InventoryItem* inventoryItem)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700804{
Ed Tanous1d7c0052022-08-09 12:32:26 -0700805 if (chassisSubNode == sensors::node::sensors)
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500806 {
Ed Tanousc1d019a2022-08-06 09:36:06 -0700807 std::string subNodeEscaped(chassisSubNode);
808 subNodeEscaped.erase(
809 std::remove(subNodeEscaped.begin(), subNodeEscaped.end(), '_'),
810 subNodeEscaped.end());
811
812 // For sensors in SensorCollection we set Id instead of MemberId,
813 // including power sensors.
814 subNodeEscaped += '_';
815 subNodeEscaped += sensorName;
816 sensorJson["Id"] = std::move(subNodeEscaped);
817
Ed Tanous1d7c0052022-08-09 12:32:26 -0700818 std::string sensorNameEs(sensorName);
819 std::replace(sensorNameEs.begin(), sensorNameEs.end(), '_', ' ');
820 sensorJson["Name"] = std::move(sensorNameEs);
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500821 }
822 else if (sensorType != "power")
823 {
824 // Set MemberId and Name for non-power sensors. For PowerSupplies and
825 // PowerControl, those properties have more general values because
826 // multiple sensors can be stored in the same JSON object.
Ed Tanous81ce6092020-12-17 16:54:55 +0000827 sensorJson["MemberId"] = sensorName;
Ed Tanous1d7c0052022-08-09 12:32:26 -0700828 std::string sensorNameEs(sensorName);
829 std::replace(sensorNameEs.begin(), sensorNameEs.end(), '_', ' ');
830 sensorJson["Name"] = std::move(sensorNameEs);
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500831 }
Ed Tanouse742b6c2019-05-03 15:06:53 -0700832
Ed Tanous81ce6092020-12-17 16:54:55 +0000833 sensorJson["Status"]["State"] = getState(inventoryItem);
834 sensorJson["Status"]["Health"] =
Ed Tanous1d7c0052022-08-09 12:32:26 -0700835 getHealth(sensorJson, propertiesDict, inventoryItem);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700836
837 // Parameter to set to override the type we get from dbus, and force it to
838 // int, regardless of what is available. This is used for schemas like fan,
839 // that require integers, not floats.
840 bool forceToInt = false;
841
Anthony Wilson3929aca2019-07-19 15:42:33 -0500842 nlohmann::json::json_pointer unit("/Reading");
Ed Tanous1d7c0052022-08-09 12:32:26 -0700843 if (chassisSubNode == sensors::node::sensors)
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500844 {
Shounak Mitra2a4ba192022-06-01 23:34:12 +0000845 sensorJson["@odata.type"] = "#Sensor.v1_2_0.Sensor";
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000846
Ed Tanous0ec8b832022-03-14 14:56:47 -0700847 sensor::ReadingType readingType = sensors::toReadingType(sensorType);
848 if (readingType == sensor::ReadingType::Invalid)
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500849 {
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000850 BMCWEB_LOG_ERROR << "Redfish cannot map reading type for "
851 << sensorType;
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500852 }
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000853 else
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500854 {
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000855 sensorJson["ReadingType"] = readingType;
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500856 }
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000857
Ed Tanous1d7c0052022-08-09 12:32:26 -0700858 std::string_view readingUnits = sensors::toReadingUnits(sensorType);
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000859 if (readingUnits.empty())
Adrian Ambrożewiczf8ede152020-06-02 13:26:33 +0200860 {
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000861 BMCWEB_LOG_ERROR << "Redfish cannot map reading unit for "
862 << sensorType;
863 }
864 else
865 {
866 sensorJson["ReadingUnits"] = readingUnits;
Adrian Ambrożewiczf8ede152020-06-02 13:26:33 +0200867 }
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500868 }
869 else if (sensorType == "temperature")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700870 {
Anthony Wilson3929aca2019-07-19 15:42:33 -0500871 unit = "/ReadingCelsius"_json_pointer;
Ed Tanous81ce6092020-12-17 16:54:55 +0000872 sensorJson["@odata.type"] = "#Thermal.v1_3_0.Temperature";
Ed Tanous1abe55e2018-09-05 08:30:59 -0700873 // TODO(ed) Documentation says that path should be type fan_tach,
874 // implementation seems to implement fan
875 }
876 else if (sensorType == "fan" || sensorType == "fan_tach")
877 {
Anthony Wilson3929aca2019-07-19 15:42:33 -0500878 unit = "/Reading"_json_pointer;
Ed Tanous81ce6092020-12-17 16:54:55 +0000879 sensorJson["ReadingUnits"] = "RPM";
880 sensorJson["@odata.type"] = "#Thermal.v1_3_0.Fan";
881 setLedState(sensorJson, inventoryItem);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700882 forceToInt = true;
883 }
Ed Tanous6f6d0d32018-10-12 11:16:43 -0700884 else if (sensorType == "fan_pwm")
885 {
Anthony Wilson3929aca2019-07-19 15:42:33 -0500886 unit = "/Reading"_json_pointer;
Ed Tanous81ce6092020-12-17 16:54:55 +0000887 sensorJson["ReadingUnits"] = "Percent";
888 sensorJson["@odata.type"] = "#Thermal.v1_3_0.Fan";
889 setLedState(sensorJson, inventoryItem);
Ed Tanous6f6d0d32018-10-12 11:16:43 -0700890 forceToInt = true;
891 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700892 else if (sensorType == "voltage")
893 {
Anthony Wilson3929aca2019-07-19 15:42:33 -0500894 unit = "/ReadingVolts"_json_pointer;
Ed Tanous81ce6092020-12-17 16:54:55 +0000895 sensorJson["@odata.type"] = "#Power.v1_0_0.Voltage";
Ed Tanous1abe55e2018-09-05 08:30:59 -0700896 }
Ed Tanous2474adf2018-09-05 16:31:16 -0700897 else if (sensorType == "power")
898 {
Ed Tanous1d7c0052022-08-09 12:32:26 -0700899 if (boost::iequals(sensorName, "total_power"))
Eddie James028f7eb2019-05-17 21:24:36 +0000900 {
Ed Tanous81ce6092020-12-17 16:54:55 +0000901 sensorJson["@odata.type"] = "#Power.v1_0_0.PowerControl";
Gunnar Mills7ab06f42019-07-02 13:07:16 -0500902 // Put multiple "sensors" into a single PowerControl, so have
903 // generic names for MemberId and Name. Follows Redfish mockup.
Ed Tanous81ce6092020-12-17 16:54:55 +0000904 sensorJson["MemberId"] = "0";
905 sensorJson["Name"] = "Chassis Power Control";
Anthony Wilson3929aca2019-07-19 15:42:33 -0500906 unit = "/PowerConsumedWatts"_json_pointer;
Eddie James028f7eb2019-05-17 21:24:36 +0000907 }
Ed Tanous1d7c0052022-08-09 12:32:26 -0700908 else if (boost::ifind_first(sensorName, "input").empty())
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700909 {
Anthony Wilson3929aca2019-07-19 15:42:33 -0500910 unit = "/PowerInputWatts"_json_pointer;
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700911 }
912 else
913 {
Anthony Wilson3929aca2019-07-19 15:42:33 -0500914 unit = "/PowerOutputWatts"_json_pointer;
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700915 }
Ed Tanous2474adf2018-09-05 16:31:16 -0700916 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700917 else
918 {
919 BMCWEB_LOG_ERROR << "Redfish cannot map object type for " << sensorName;
920 return;
921 }
922 // Map of dbus interface name, dbus property name and redfish property_name
Anthony Wilson3929aca2019-07-19 15:42:33 -0500923 std::vector<
924 std::tuple<const char*, const char*, nlohmann::json::json_pointer>>
925 properties;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700926 properties.reserve(7);
927
928 properties.emplace_back("xyz.openbmc_project.Sensor.Value", "Value", unit);
Shawn McCarneyde629b62019-03-08 10:42:51 -0600929
Ed Tanous1d7c0052022-08-09 12:32:26 -0700930 if (chassisSubNode == sensors::node::sensors)
Anthony Wilson3929aca2019-07-19 15:42:33 -0500931 {
932 properties.emplace_back(
933 "xyz.openbmc_project.Sensor.Threshold.Warning", "WarningHigh",
934 "/Thresholds/UpperCaution/Reading"_json_pointer);
935 properties.emplace_back(
936 "xyz.openbmc_project.Sensor.Threshold.Warning", "WarningLow",
937 "/Thresholds/LowerCaution/Reading"_json_pointer);
938 properties.emplace_back(
939 "xyz.openbmc_project.Sensor.Threshold.Critical", "CriticalHigh",
940 "/Thresholds/UpperCritical/Reading"_json_pointer);
941 properties.emplace_back(
942 "xyz.openbmc_project.Sensor.Threshold.Critical", "CriticalLow",
943 "/Thresholds/LowerCritical/Reading"_json_pointer);
944 }
945 else if (sensorType != "power")
Shawn McCarneyde629b62019-03-08 10:42:51 -0600946 {
947 properties.emplace_back("xyz.openbmc_project.Sensor.Threshold.Warning",
Anthony Wilson3929aca2019-07-19 15:42:33 -0500948 "WarningHigh",
949 "/UpperThresholdNonCritical"_json_pointer);
Shawn McCarneyde629b62019-03-08 10:42:51 -0600950 properties.emplace_back("xyz.openbmc_project.Sensor.Threshold.Warning",
Anthony Wilson3929aca2019-07-19 15:42:33 -0500951 "WarningLow",
952 "/LowerThresholdNonCritical"_json_pointer);
Shawn McCarneyde629b62019-03-08 10:42:51 -0600953 properties.emplace_back("xyz.openbmc_project.Sensor.Threshold.Critical",
Anthony Wilson3929aca2019-07-19 15:42:33 -0500954 "CriticalHigh",
955 "/UpperThresholdCritical"_json_pointer);
Shawn McCarneyde629b62019-03-08 10:42:51 -0600956 properties.emplace_back("xyz.openbmc_project.Sensor.Threshold.Critical",
Anthony Wilson3929aca2019-07-19 15:42:33 -0500957 "CriticalLow",
958 "/LowerThresholdCritical"_json_pointer);
Shawn McCarneyde629b62019-03-08 10:42:51 -0600959 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700960
Ed Tanous2474adf2018-09-05 16:31:16 -0700961 // TODO Need to get UpperThresholdFatal and LowerThresholdFatal
962
Ed Tanous1d7c0052022-08-09 12:32:26 -0700963 if (chassisSubNode == sensors::node::sensors)
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500964 {
965 properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MinValue",
Anthony Wilson3929aca2019-07-19 15:42:33 -0500966 "/ReadingRangeMin"_json_pointer);
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500967 properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MaxValue",
Anthony Wilson3929aca2019-07-19 15:42:33 -0500968 "/ReadingRangeMax"_json_pointer);
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500969 }
970 else if (sensorType == "temperature")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700971 {
972 properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MinValue",
Anthony Wilson3929aca2019-07-19 15:42:33 -0500973 "/MinReadingRangeTemp"_json_pointer);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700974 properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MaxValue",
Anthony Wilson3929aca2019-07-19 15:42:33 -0500975 "/MaxReadingRangeTemp"_json_pointer);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700976 }
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500977 else if (sensorType != "power")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700978 {
979 properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MinValue",
Anthony Wilson3929aca2019-07-19 15:42:33 -0500980 "/MinReadingRange"_json_pointer);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700981 properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MaxValue",
Anthony Wilson3929aca2019-07-19 15:42:33 -0500982 "/MaxReadingRange"_json_pointer);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700983 }
984
Anthony Wilson3929aca2019-07-19 15:42:33 -0500985 for (const std::tuple<const char*, const char*,
986 nlohmann::json::json_pointer>& p : properties)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700987 {
Ed Tanous1d7c0052022-08-09 12:32:26 -0700988 for (const auto& [valueName, valueVariant] : propertiesDict)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700989 {
Ed Tanous1d7c0052022-08-09 12:32:26 -0700990 if (valueName != std::get<1>(p))
Ed Tanous1abe55e2018-09-05 08:30:59 -0700991 {
Ed Tanous711ac7a2021-12-20 09:34:41 -0800992 continue;
993 }
Ed Tanous1d7c0052022-08-09 12:32:26 -0700994
995 // The property we want to set may be nested json, so use
996 // a json_pointer for easy indexing into the json structure.
997 const nlohmann::json::json_pointer& key = std::get<2>(p);
998
Ed Tanous1d7c0052022-08-09 12:32:26 -0700999 const double* doubleValue = std::get_if<double>(&valueVariant);
Ed Tanous40e4f382022-08-09 18:42:51 -07001000 if (doubleValue == nullptr)
Ed Tanous711ac7a2021-12-20 09:34:41 -08001001 {
Ed Tanous40e4f382022-08-09 18:42:51 -07001002 BMCWEB_LOG_ERROR << "Got value interface that wasn't double";
Ed Tanous1d7c0052022-08-09 12:32:26 -07001003 continue;
1004 }
Ed Tanous1d7c0052022-08-09 12:32:26 -07001005 if (forceToInt)
1006 {
Ed Tanous40e4f382022-08-09 18:42:51 -07001007 sensorJson[key] = static_cast<int64_t>(*doubleValue);
Ed Tanous1d7c0052022-08-09 12:32:26 -07001008 }
1009 else
1010 {
Ed Tanous40e4f382022-08-09 18:42:51 -07001011 sensorJson[key] = *doubleValue;
Ed Tanous1abe55e2018-09-05 08:30:59 -07001012 }
1013 }
1014 }
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +01001015}
1016
Ed Tanous1d7c0052022-08-09 12:32:26 -07001017/**
1018 * @brief Builds a json sensor representation of a sensor.
1019 * @param sensorName The name of the sensor to be built
1020 * @param sensorType The type (temperature, fan_tach, etc) of the sensor to
1021 * build
1022 * @param chassisSubNode The subnode (thermal, sensor, ect) of the sensor
1023 * @param interfacesDict A dictionary of the interfaces and properties of said
1024 * interfaces to be built from
1025 * @param sensorJson The json object to fill
1026 * @param inventoryItem D-Bus inventory item associated with the sensor. Will
1027 * be nullptr if no associated inventory item was found.
1028 */
1029inline void objectInterfacesToJson(
1030 const std::string& sensorName, const std::string& sensorType,
1031 const std::string& chassisSubNode,
1032 const dbus::utility::DBusInteracesMap& interfacesDict,
1033 nlohmann::json& sensorJson, InventoryItem* inventoryItem)
1034{
1035
1036 for (const auto& [interface, valuesDict] : interfacesDict)
1037 {
1038 objectPropertiesToJson(sensorName, sensorType, chassisSubNode,
1039 valuesDict, sensorJson, inventoryItem);
1040 }
Ed Tanousc1d019a2022-08-06 09:36:06 -07001041 BMCWEB_LOG_DEBUG << "Added sensor " << sensorName;
Ed Tanous1d7c0052022-08-09 12:32:26 -07001042}
1043
Ed Tanousb5a76932020-09-29 16:16:58 -07001044inline void populateFanRedundancy(
1045 const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp)
James Feist8bd25cc2019-03-15 15:14:00 -07001046{
1047 crow::connections::systemBus->async_method_call(
Ed Tanousb9d36b42022-02-26 21:42:46 -08001048 [sensorsAsyncResp](
1049 const boost::system::error_code ec,
1050 const dbus::utility::MapperGetSubTreeResponse& resp) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001051 if (ec)
1052 {
1053 return; // don't have to have this interface
1054 }
1055 for (const std::pair<
1056 std::string,
1057 std::vector<std::pair<std::string, std::vector<std::string>>>>&
1058 pathPair : resp)
1059 {
1060 const std::string& path = pathPair.first;
1061 const std::vector<std::pair<std::string, std::vector<std::string>>>&
1062 objDict = pathPair.second;
1063 if (objDict.empty())
James Feist8bd25cc2019-03-15 15:14:00 -07001064 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001065 continue; // this should be impossible
James Feist8bd25cc2019-03-15 15:14:00 -07001066 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001067
1068 const std::string& owner = objDict.begin()->first;
1069 sdbusplus::asio::getProperty<std::vector<std::string>>(
1070 *crow::connections::systemBus,
1071 "xyz.openbmc_project.ObjectMapper", path + "/chassis",
1072 "xyz.openbmc_project.Association", "endpoints",
1073 [path, owner,
1074 sensorsAsyncResp](const boost::system::error_code e,
1075 const std::vector<std::string>& endpoints) {
1076 if (e)
James Feist8bd25cc2019-03-15 15:14:00 -07001077 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001078 return; // if they don't have an association we
1079 // can't tell what chassis is
James Feist8bd25cc2019-03-15 15:14:00 -07001080 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001081 auto found =
1082 std::find_if(endpoints.begin(), endpoints.end(),
1083 [sensorsAsyncResp](const std::string& entry) {
1084 return entry.find(sensorsAsyncResp->chassisId) !=
1085 std::string::npos;
1086 });
James Feist8bd25cc2019-03-15 15:14:00 -07001087
Ed Tanous002d39b2022-05-31 08:59:27 -07001088 if (found == endpoints.end())
1089 {
1090 return;
1091 }
Krzysztof Grobelny86d89ed2022-08-29 14:49:20 +02001092 sdbusplus::asio::getAllProperties(
1093 *crow::connections::systemBus, owner, path,
1094 "xyz.openbmc_project.Control.FanRedundancy",
Ed Tanous002d39b2022-05-31 08:59:27 -07001095 [path, sensorsAsyncResp](
1096 const boost::system::error_code& err,
Krzysztof Grobelny86d89ed2022-08-29 14:49:20 +02001097 const dbus::utility::DBusPropertiesMap& ret) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001098 if (err)
1099 {
1100 return; // don't have to have this
1101 // interface
1102 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001103
Krzysztof Grobelny86d89ed2022-08-29 14:49:20 +02001104 const uint8_t* allowedFailures = nullptr;
1105 const std::vector<std::string>* collection = nullptr;
1106 const std::string* status = nullptr;
1107
1108 const bool success = sdbusplus::unpackPropertiesNoThrow(
1109 dbus_utils::UnpackErrorPrinter(), ret,
1110 "AllowedFailures", allowedFailures, "Collection",
1111 collection, "Status", status);
1112
1113 if (!success)
1114 {
1115 messages::internalError(
1116 sensorsAsyncResp->asyncResp->res);
1117 return;
1118 }
1119
1120 if (allowedFailures == nullptr || collection == nullptr ||
1121 status == nullptr)
Ed Tanous002d39b2022-05-31 08:59:27 -07001122 {
1123 BMCWEB_LOG_ERROR << "Invalid redundancy interface";
1124 messages::internalError(
1125 sensorsAsyncResp->asyncResp->res);
1126 return;
1127 }
1128
Ed Tanous002d39b2022-05-31 08:59:27 -07001129 sdbusplus::message::object_path objectPath(path);
1130 std::string name = objectPath.filename();
1131 if (name.empty())
1132 {
1133 // this should be impossible
1134 messages::internalError(
1135 sensorsAsyncResp->asyncResp->res);
1136 return;
1137 }
1138 std::replace(name.begin(), name.end(), '_', ' ');
1139
1140 std::string health;
1141
Ed Tanous11ba3972022-07-11 09:50:41 -07001142 if (status->ends_with("Full"))
Ed Tanous002d39b2022-05-31 08:59:27 -07001143 {
1144 health = "OK";
1145 }
Ed Tanous11ba3972022-07-11 09:50:41 -07001146 else if (status->ends_with("Degraded"))
Ed Tanous002d39b2022-05-31 08:59:27 -07001147 {
1148 health = "Warning";
1149 }
1150 else
1151 {
1152 health = "Critical";
1153 }
1154 nlohmann::json::array_t redfishCollection;
1155 const auto& fanRedfish =
1156 sensorsAsyncResp->asyncResp->res.jsonValue["Fans"];
1157 for (const std::string& item : *collection)
1158 {
Ed Tanous8a592812022-06-04 09:06:59 -07001159 sdbusplus::message::object_path itemPath(item);
1160 std::string itemName = itemPath.filename();
Ed Tanous002d39b2022-05-31 08:59:27 -07001161 if (itemName.empty())
James Feist8bd25cc2019-03-15 15:14:00 -07001162 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001163 continue;
James Feist8bd25cc2019-03-15 15:14:00 -07001164 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001165 /*
1166 todo(ed): merge patch that fixes the names
1167 std::replace(itemName.begin(),
1168 itemName.end(), '_', ' ');*/
1169 auto schemaItem =
1170 std::find_if(fanRedfish.begin(), fanRedfish.end(),
1171 [itemName](const nlohmann::json& fan) {
1172 return fan["MemberId"] == itemName;
James Feist8bd25cc2019-03-15 15:14:00 -07001173 });
Ed Tanous002d39b2022-05-31 08:59:27 -07001174 if (schemaItem != fanRedfish.end())
James Feist8bd25cc2019-03-15 15:14:00 -07001175 {
Ed Tanous8a592812022-06-04 09:06:59 -07001176 nlohmann::json::object_t collectionId;
1177 collectionId["@odata.id"] =
Ed Tanous002d39b2022-05-31 08:59:27 -07001178 (*schemaItem)["@odata.id"];
1179 redfishCollection.emplace_back(
Ed Tanous8a592812022-06-04 09:06:59 -07001180 std::move(collectionId));
Ed Tanous002d39b2022-05-31 08:59:27 -07001181 }
1182 else
1183 {
1184 BMCWEB_LOG_ERROR << "failed to find fan in schema";
1185 messages::internalError(
1186 sensorsAsyncResp->asyncResp->res);
James Feist8bd25cc2019-03-15 15:14:00 -07001187 return;
1188 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001189 }
James Feist8bd25cc2019-03-15 15:14:00 -07001190
Ed Tanous002d39b2022-05-31 08:59:27 -07001191 size_t minNumNeeded =
1192 collection->empty()
1193 ? 0
1194 : collection->size() - *allowedFailures;
1195 nlohmann::json& jResp = sensorsAsyncResp->asyncResp->res
1196 .jsonValue["Redundancy"];
James Feist8bd25cc2019-03-15 15:14:00 -07001197
Ed Tanous002d39b2022-05-31 08:59:27 -07001198 nlohmann::json::object_t redundancy;
1199 redundancy["@odata.id"] =
1200 "/redfish/v1/Chassis/" + sensorsAsyncResp->chassisId +
1201 "/" + sensorsAsyncResp->chassisSubNode +
1202 "#/Redundancy/" + std::to_string(jResp.size());
1203 redundancy["@odata.type"] = "#Redundancy.v1_3_2.Redundancy";
1204 redundancy["MinNumNeeded"] = minNumNeeded;
1205 redundancy["MemberId"] = name;
1206 redundancy["Mode"] = "N+m";
1207 redundancy["Name"] = name;
1208 redundancy["RedundancySet"] = redfishCollection;
1209 redundancy["Status"]["Health"] = health;
1210 redundancy["Status"]["State"] = "Enabled";
James Feist8bd25cc2019-03-15 15:14:00 -07001211
Ed Tanous002d39b2022-05-31 08:59:27 -07001212 jResp.push_back(std::move(redundancy));
Krzysztof Grobelny86d89ed2022-08-29 14:49:20 +02001213 });
Ed Tanous002d39b2022-05-31 08:59:27 -07001214 });
1215 }
James Feist8bd25cc2019-03-15 15:14:00 -07001216 },
1217 "xyz.openbmc_project.ObjectMapper",
1218 "/xyz/openbmc_project/object_mapper",
1219 "xyz.openbmc_project.ObjectMapper", "GetSubTree",
1220 "/xyz/openbmc_project/control", 2,
1221 std::array<const char*, 1>{
1222 "xyz.openbmc_project.Control.FanRedundancy"});
1223}
1224
Ed Tanousb5a76932020-09-29 16:16:58 -07001225inline void
Ed Tanous81ce6092020-12-17 16:54:55 +00001226 sortJSONResponse(const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07001227{
zhanghch058d1b46d2021-04-01 11:18:24 +08001228 nlohmann::json& response = sensorsAsyncResp->asyncResp->res.jsonValue;
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07001229 std::array<std::string, 2> sensorHeaders{"Temperatures", "Fans"};
Ed Tanous81ce6092020-12-17 16:54:55 +00001230 if (sensorsAsyncResp->chassisSubNode == sensors::node::power)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07001231 {
1232 sensorHeaders = {"Voltages", "PowerSupplies"};
1233 }
1234 for (const std::string& sensorGroup : sensorHeaders)
1235 {
1236 nlohmann::json::iterator entry = response.find(sensorGroup);
1237 if (entry != response.end())
1238 {
1239 std::sort(entry->begin(), entry->end(),
Ed Tanous02cad962022-06-30 16:50:15 -07001240 [](const nlohmann::json& c1, const nlohmann::json& c2) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001241 return c1["Name"] < c2["Name"];
1242 });
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07001243
1244 // add the index counts to the end of each entry
1245 size_t count = 0;
1246 for (nlohmann::json& sensorJson : *entry)
1247 {
1248 nlohmann::json::iterator odata = sensorJson.find("@odata.id");
1249 if (odata == sensorJson.end())
1250 {
1251 continue;
1252 }
1253 std::string* value = odata->get_ptr<std::string*>();
1254 if (value != nullptr)
1255 {
1256 *value += std::to_string(count);
1257 count++;
Ed Tanous81ce6092020-12-17 16:54:55 +00001258 sensorsAsyncResp->updateUri(sensorJson["Name"], *value);
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07001259 }
1260 }
1261 }
1262 }
1263}
1264
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +01001265/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001266 * @brief Finds the inventory item with the specified object path.
1267 * @param inventoryItems D-Bus inventory items associated with sensors.
1268 * @param invItemObjPath D-Bus object path of inventory item.
1269 * @return Inventory item within vector, or nullptr if no match found.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001270 */
Ed Tanous23a21a12020-07-25 04:45:05 +00001271inline InventoryItem* findInventoryItem(
Ed Tanousb5a76932020-09-29 16:16:58 -07001272 const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems,
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001273 const std::string& invItemObjPath)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001274{
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001275 for (InventoryItem& inventoryItem : *inventoryItems)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001276 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001277 if (inventoryItem.objectPath == invItemObjPath)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001278 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001279 return &inventoryItem;
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001280 }
1281 }
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001282 return nullptr;
1283}
1284
1285/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001286 * @brief Finds the inventory item associated with the specified sensor.
1287 * @param inventoryItems D-Bus inventory items associated with sensors.
1288 * @param sensorObjPath D-Bus object path of sensor.
1289 * @return Inventory item within vector, or nullptr if no match found.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001290 */
Ed Tanous23a21a12020-07-25 04:45:05 +00001291inline InventoryItem* findInventoryItemForSensor(
Ed Tanousb5a76932020-09-29 16:16:58 -07001292 const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems,
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001293 const std::string& sensorObjPath)
1294{
1295 for (InventoryItem& inventoryItem : *inventoryItems)
1296 {
1297 if (inventoryItem.sensors.count(sensorObjPath) > 0)
1298 {
1299 return &inventoryItem;
1300 }
1301 }
1302 return nullptr;
1303}
1304
1305/**
Anthony Wilsond5005492019-07-31 16:34:17 -05001306 * @brief Finds the inventory item associated with the specified led path.
1307 * @param inventoryItems D-Bus inventory items associated with sensors.
1308 * @param ledObjPath D-Bus object path of led.
1309 * @return Inventory item within vector, or nullptr if no match found.
1310 */
1311inline InventoryItem*
1312 findInventoryItemForLed(std::vector<InventoryItem>& inventoryItems,
1313 const std::string& ledObjPath)
1314{
1315 for (InventoryItem& inventoryItem : inventoryItems)
1316 {
1317 if (inventoryItem.ledObjectPath == ledObjPath)
1318 {
1319 return &inventoryItem;
1320 }
1321 }
1322 return nullptr;
1323}
1324
1325/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001326 * @brief Adds inventory item and associated sensor to specified vector.
1327 *
1328 * Adds a new InventoryItem to the vector if necessary. Searches for an
1329 * existing InventoryItem with the specified object path. If not found, one is
1330 * added to the vector.
1331 *
1332 * Next, the specified sensor is added to the set of sensors associated with the
1333 * InventoryItem.
1334 *
1335 * @param inventoryItems D-Bus inventory items associated with sensors.
1336 * @param invItemObjPath D-Bus object path of inventory item.
1337 * @param sensorObjPath D-Bus object path of sensor
1338 */
Ed Tanousb5a76932020-09-29 16:16:58 -07001339inline void addInventoryItem(
1340 const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems,
1341 const std::string& invItemObjPath, const std::string& sensorObjPath)
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001342{
1343 // Look for inventory item in vector
1344 InventoryItem* inventoryItem =
1345 findInventoryItem(inventoryItems, invItemObjPath);
1346
1347 // If inventory item doesn't exist in vector, add it
1348 if (inventoryItem == nullptr)
1349 {
1350 inventoryItems->emplace_back(invItemObjPath);
1351 inventoryItem = &(inventoryItems->back());
1352 }
1353
1354 // Add sensor to set of sensors associated with inventory item
1355 inventoryItem->sensors.emplace(sensorObjPath);
1356}
1357
1358/**
1359 * @brief Stores D-Bus data in the specified inventory item.
1360 *
1361 * Finds D-Bus data in the specified map of interfaces. Stores the data in the
1362 * specified InventoryItem.
1363 *
1364 * This data is later used to provide sensor property values in the JSON
1365 * response.
1366 *
1367 * @param inventoryItem Inventory item where data will be stored.
1368 * @param interfacesDict Map containing D-Bus interfaces and their properties
1369 * for the specified inventory item.
1370 */
Ed Tanous23a21a12020-07-25 04:45:05 +00001371inline void storeInventoryItemData(
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001372 InventoryItem& inventoryItem,
Ed Tanous711ac7a2021-12-20 09:34:41 -08001373 const dbus::utility::DBusInteracesMap& interfacesDict)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001374{
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001375 // Get properties from Inventory.Item interface
Ed Tanous711ac7a2021-12-20 09:34:41 -08001376
Ed Tanous9eb808c2022-01-25 10:19:23 -08001377 for (const auto& [interface, values] : interfacesDict)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001378 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001379 if (interface == "xyz.openbmc_project.Inventory.Item")
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001380 {
Ed Tanous9eb808c2022-01-25 10:19:23 -08001381 for (const auto& [name, dbusValue] : values)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001382 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001383 if (name == "Present")
1384 {
1385 const bool* value = std::get_if<bool>(&dbusValue);
1386 if (value != nullptr)
1387 {
1388 inventoryItem.isPresent = *value;
1389 }
1390 }
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001391 }
1392 }
Ed Tanous711ac7a2021-12-20 09:34:41 -08001393 // Check if Inventory.Item.PowerSupply interface is present
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001394
Ed Tanous711ac7a2021-12-20 09:34:41 -08001395 if (interface == "xyz.openbmc_project.Inventory.Item.PowerSupply")
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001396 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001397 inventoryItem.isPowerSupply = true;
1398 }
1399
1400 // Get properties from Inventory.Decorator.Asset interface
1401 if (interface == "xyz.openbmc_project.Inventory.Decorator.Asset")
1402 {
Ed Tanous9eb808c2022-01-25 10:19:23 -08001403 for (const auto& [name, dbusValue] : values)
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001404 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001405 if (name == "Manufacturer")
1406 {
1407 const std::string* value =
1408 std::get_if<std::string>(&dbusValue);
1409 if (value != nullptr)
1410 {
1411 inventoryItem.manufacturer = *value;
1412 }
1413 }
1414 if (name == "Model")
1415 {
1416 const std::string* value =
1417 std::get_if<std::string>(&dbusValue);
1418 if (value != nullptr)
1419 {
1420 inventoryItem.model = *value;
1421 }
1422 }
1423 if (name == "SerialNumber")
1424 {
1425 const std::string* value =
1426 std::get_if<std::string>(&dbusValue);
1427 if (value != nullptr)
1428 {
1429 inventoryItem.serialNumber = *value;
1430 }
1431 }
1432 if (name == "PartNumber")
1433 {
1434 const std::string* value =
1435 std::get_if<std::string>(&dbusValue);
1436 if (value != nullptr)
1437 {
1438 inventoryItem.partNumber = *value;
1439 }
1440 }
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001441 }
1442 }
1443
Ed Tanous711ac7a2021-12-20 09:34:41 -08001444 if (interface ==
1445 "xyz.openbmc_project.State.Decorator.OperationalStatus")
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001446 {
Ed Tanous9eb808c2022-01-25 10:19:23 -08001447 for (const auto& [name, dbusValue] : values)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001448 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001449 if (name == "Functional")
1450 {
1451 const bool* value = std::get_if<bool>(&dbusValue);
1452 if (value != nullptr)
1453 {
1454 inventoryItem.isFunctional = *value;
1455 }
1456 }
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001457 }
1458 }
1459 }
1460}
1461
1462/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001463 * @brief Gets D-Bus data for inventory items associated with sensors.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001464 *
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001465 * Uses the specified connections (services) to obtain D-Bus data for inventory
1466 * items associated with sensors. Stores the resulting data in the
1467 * inventoryItems vector.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001468 *
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001469 * This data is later used to provide sensor property values in the JSON
1470 * response.
1471 *
1472 * Finds the inventory item data asynchronously. Invokes callback when data has
1473 * been obtained.
1474 *
1475 * The callback must have the following signature:
1476 * @code
Anthony Wilsond5005492019-07-31 16:34:17 -05001477 * callback(void)
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001478 * @endcode
1479 *
1480 * This function is called recursively, obtaining data asynchronously from one
1481 * connection in each call. This ensures the callback is not invoked until the
1482 * last asynchronous function has completed.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001483 *
1484 * @param sensorsAsyncResp Pointer to object holding response data.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001485 * @param inventoryItems D-Bus inventory items associated with sensors.
1486 * @param invConnections Connections that provide data for the inventory items.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001487 * @param objectMgrPaths Mappings from connection name to DBus object path that
1488 * implements ObjectManager.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001489 * @param callback Callback to invoke when inventory data has been obtained.
1490 * @param invConnectionsIndex Current index in invConnections. Only specified
1491 * in recursive calls to this function.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001492 */
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001493template <typename Callback>
1494static void getInventoryItemsData(
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001495 std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001496 std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
Nan Zhoufe04d492022-06-22 17:10:41 +00001497 std::shared_ptr<std::set<std::string>> invConnections,
1498 std::shared_ptr<std::map<std::string, std::string>> objectMgrPaths,
Ed Tanous271584a2019-07-09 16:24:22 -07001499 Callback&& callback, size_t invConnectionsIndex = 0)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001500{
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001501 BMCWEB_LOG_DEBUG << "getInventoryItemsData enter";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001502
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001503 // If no more connections left, call callback
1504 if (invConnectionsIndex >= invConnections->size())
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001505 {
Anthony Wilsond5005492019-07-31 16:34:17 -05001506 callback();
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001507 BMCWEB_LOG_DEBUG << "getInventoryItemsData exit";
1508 return;
1509 }
1510
1511 // Get inventory item data from current connection
Nan Zhoufe04d492022-06-22 17:10:41 +00001512 auto it = invConnections->begin();
1513 std::advance(it, invConnectionsIndex);
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001514 if (it != invConnections->end())
1515 {
1516 const std::string& invConnection = *it;
1517
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001518 // Response handler for GetManagedObjects
Ed Tanous002d39b2022-05-31 08:59:27 -07001519 auto respHandler =
1520 [sensorsAsyncResp, inventoryItems, invConnections, objectMgrPaths,
Ed Tanous02cad962022-06-30 16:50:15 -07001521 callback{std::forward<Callback>(callback)}, invConnectionsIndex](
1522 const boost::system::error_code ec,
1523 const dbus::utility::ManagedObjectType& resp) {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001524 BMCWEB_LOG_DEBUG << "getInventoryItemsData respHandler enter";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001525 if (ec)
1526 {
1527 BMCWEB_LOG_ERROR
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001528 << "getInventoryItemsData respHandler DBus error " << ec;
zhanghch058d1b46d2021-04-01 11:18:24 +08001529 messages::internalError(sensorsAsyncResp->asyncResp->res);
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001530 return;
1531 }
1532
1533 // Loop through returned object paths
1534 for (const auto& objDictEntry : resp)
1535 {
1536 const std::string& objPath =
1537 static_cast<const std::string&>(objDictEntry.first);
1538
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001539 // If this object path is one of the specified inventory items
1540 InventoryItem* inventoryItem =
1541 findInventoryItem(inventoryItems, objPath);
1542 if (inventoryItem != nullptr)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001543 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001544 // Store inventory data in InventoryItem
1545 storeInventoryItemData(*inventoryItem, objDictEntry.second);
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001546 }
1547 }
1548
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001549 // Recurse to get inventory item data from next connection
1550 getInventoryItemsData(sensorsAsyncResp, inventoryItems,
1551 invConnections, objectMgrPaths,
1552 std::move(callback), invConnectionsIndex + 1);
1553
1554 BMCWEB_LOG_DEBUG << "getInventoryItemsData respHandler exit";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001555 };
1556
1557 // Find DBus object path that implements ObjectManager for the current
1558 // connection. If no mapping found, default to "/".
1559 auto iter = objectMgrPaths->find(invConnection);
1560 const std::string& objectMgrPath =
1561 (iter != objectMgrPaths->end()) ? iter->second : "/";
1562 BMCWEB_LOG_DEBUG << "ObjectManager path for " << invConnection << " is "
1563 << objectMgrPath;
1564
1565 // Get all object paths and their interfaces for current connection
1566 crow::connections::systemBus->async_method_call(
1567 std::move(respHandler), invConnection, objectMgrPath,
1568 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
1569 }
1570
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001571 BMCWEB_LOG_DEBUG << "getInventoryItemsData exit";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001572}
1573
1574/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001575 * @brief Gets connections that provide D-Bus data for inventory items.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001576 *
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001577 * Gets the D-Bus connections (services) that provide data for the inventory
1578 * items that are associated with sensors.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001579 *
1580 * Finds the connections asynchronously. Invokes callback when information has
1581 * been obtained.
1582 *
1583 * The callback must have the following signature:
1584 * @code
Nan Zhoufe04d492022-06-22 17:10:41 +00001585 * callback(std::shared_ptr<std::set<std::string>> invConnections)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001586 * @endcode
1587 *
1588 * @param sensorsAsyncResp Pointer to object holding response data.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001589 * @param inventoryItems D-Bus inventory items associated with sensors.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001590 * @param callback Callback to invoke when connections have been obtained.
1591 */
1592template <typename Callback>
1593static void getInventoryItemsConnections(
Ed Tanousb5a76932020-09-29 16:16:58 -07001594 const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
1595 const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems,
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001596 Callback&& callback)
1597{
1598 BMCWEB_LOG_DEBUG << "getInventoryItemsConnections enter";
1599
1600 const std::string path = "/xyz/openbmc_project/inventory";
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001601 const std::array<std::string, 4> interfaces = {
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001602 "xyz.openbmc_project.Inventory.Item",
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001603 "xyz.openbmc_project.Inventory.Item.PowerSupply",
1604 "xyz.openbmc_project.Inventory.Decorator.Asset",
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001605 "xyz.openbmc_project.State.Decorator.OperationalStatus"};
1606
1607 // Response handler for parsing output from GetSubTree
Ed Tanous002d39b2022-05-31 08:59:27 -07001608 auto respHandler =
1609 [callback{std::forward<Callback>(callback)}, sensorsAsyncResp,
1610 inventoryItems](
1611 const boost::system::error_code ec,
1612 const dbus::utility::MapperGetSubTreeResponse& subtree) {
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001613 BMCWEB_LOG_DEBUG << "getInventoryItemsConnections respHandler enter";
1614 if (ec)
1615 {
zhanghch058d1b46d2021-04-01 11:18:24 +08001616 messages::internalError(sensorsAsyncResp->asyncResp->res);
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001617 BMCWEB_LOG_ERROR
1618 << "getInventoryItemsConnections respHandler DBus error " << ec;
1619 return;
1620 }
1621
1622 // Make unique list of connections for desired inventory items
Nan Zhoufe04d492022-06-22 17:10:41 +00001623 std::shared_ptr<std::set<std::string>> invConnections =
1624 std::make_shared<std::set<std::string>>();
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001625
1626 // Loop through objects from GetSubTree
1627 for (const std::pair<
1628 std::string,
1629 std::vector<std::pair<std::string, std::vector<std::string>>>>&
1630 object : subtree)
1631 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001632 // Check if object path is one of the specified inventory items
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001633 const std::string& objPath = object.first;
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001634 if (findInventoryItem(inventoryItems, objPath) != nullptr)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001635 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001636 // Store all connections to inventory item
1637 for (const std::pair<std::string, std::vector<std::string>>&
1638 objData : object.second)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001639 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001640 const std::string& invConnection = objData.first;
1641 invConnections->insert(invConnection);
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001642 }
1643 }
1644 }
Anthony Wilsond5005492019-07-31 16:34:17 -05001645
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001646 callback(invConnections);
1647 BMCWEB_LOG_DEBUG << "getInventoryItemsConnections respHandler exit";
1648 };
1649
1650 // Make call to ObjectMapper to find all inventory items
1651 crow::connections::systemBus->async_method_call(
1652 std::move(respHandler), "xyz.openbmc_project.ObjectMapper",
1653 "/xyz/openbmc_project/object_mapper",
1654 "xyz.openbmc_project.ObjectMapper", "GetSubTree", path, 0, interfaces);
1655 BMCWEB_LOG_DEBUG << "getInventoryItemsConnections exit";
1656}
1657
1658/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001659 * @brief Gets associations from sensors to inventory items.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001660 *
1661 * Looks for ObjectMapper associations from the specified sensors to related
Anthony Wilsond5005492019-07-31 16:34:17 -05001662 * inventory items. Then finds the associations from those inventory items to
1663 * their LEDs, if any.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001664 *
1665 * Finds the inventory items asynchronously. Invokes callback when information
1666 * has been obtained.
1667 *
1668 * The callback must have the following signature:
1669 * @code
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001670 * callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001671 * @endcode
1672 *
1673 * @param sensorsAsyncResp Pointer to object holding response data.
1674 * @param sensorNames All sensors within the current chassis.
1675 * @param objectMgrPaths Mappings from connection name to DBus object path that
1676 * implements ObjectManager.
1677 * @param callback Callback to invoke when inventory items have been obtained.
1678 */
1679template <typename Callback>
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001680static void getInventoryItemAssociations(
Ed Tanousb5a76932020-09-29 16:16:58 -07001681 const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
Nan Zhoufe04d492022-06-22 17:10:41 +00001682 const std::shared_ptr<std::set<std::string>>& sensorNames,
1683 const std::shared_ptr<std::map<std::string, std::string>>& objectMgrPaths,
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001684 Callback&& callback)
1685{
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001686 BMCWEB_LOG_DEBUG << "getInventoryItemAssociations enter";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001687
1688 // Response handler for GetManagedObjects
Ed Tanous02cad962022-06-30 16:50:15 -07001689 auto respHandler =
1690 [callback{std::forward<Callback>(callback)}, sensorsAsyncResp,
1691 sensorNames](const boost::system::error_code ec,
1692 const dbus::utility::ManagedObjectType& resp) {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001693 BMCWEB_LOG_DEBUG << "getInventoryItemAssociations respHandler enter";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001694 if (ec)
1695 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001696 BMCWEB_LOG_ERROR
1697 << "getInventoryItemAssociations respHandler DBus error " << ec;
zhanghch058d1b46d2021-04-01 11:18:24 +08001698 messages::internalError(sensorsAsyncResp->asyncResp->res);
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001699 return;
1700 }
1701
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001702 // Create vector to hold list of inventory items
1703 std::shared_ptr<std::vector<InventoryItem>> inventoryItems =
1704 std::make_shared<std::vector<InventoryItem>>();
1705
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001706 // Loop through returned object paths
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001707 std::string sensorAssocPath;
1708 sensorAssocPath.reserve(128); // avoid memory allocations
1709 for (const auto& objDictEntry : resp)
1710 {
1711 const std::string& objPath =
1712 static_cast<const std::string&>(objDictEntry.first);
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001713
1714 // If path is inventory association for one of the specified sensors
1715 for (const std::string& sensorName : *sensorNames)
1716 {
1717 sensorAssocPath = sensorName;
1718 sensorAssocPath += "/inventory";
1719 if (objPath == sensorAssocPath)
1720 {
1721 // Get Association interface for object path
Ed Tanous711ac7a2021-12-20 09:34:41 -08001722 for (const auto& [interface, values] : objDictEntry.second)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001723 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001724 if (interface == "xyz.openbmc_project.Association")
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001725 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001726 for (const auto& [valueName, value] : values)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001727 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001728 if (valueName == "endpoints")
1729 {
1730 const std::vector<std::string>* endpoints =
1731 std::get_if<std::vector<std::string>>(
1732 &value);
1733 if ((endpoints != nullptr) &&
1734 !endpoints->empty())
1735 {
1736 // Add inventory item to vector
1737 const std::string& invItemPath =
1738 endpoints->front();
1739 addInventoryItem(inventoryItems,
1740 invItemPath,
1741 sensorName);
1742 }
1743 }
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001744 }
1745 }
1746 }
1747 break;
1748 }
1749 }
1750 }
1751
Anthony Wilsond5005492019-07-31 16:34:17 -05001752 // Now loop through the returned object paths again, this time to
1753 // find the leds associated with the inventory items we just found
1754 std::string inventoryAssocPath;
1755 inventoryAssocPath.reserve(128); // avoid memory allocations
1756 for (const auto& objDictEntry : resp)
1757 {
1758 const std::string& objPath =
1759 static_cast<const std::string&>(objDictEntry.first);
Anthony Wilsond5005492019-07-31 16:34:17 -05001760
1761 for (InventoryItem& inventoryItem : *inventoryItems)
1762 {
1763 inventoryAssocPath = inventoryItem.objectPath;
1764 inventoryAssocPath += "/leds";
1765 if (objPath == inventoryAssocPath)
1766 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001767 for (const auto& [interface, values] : objDictEntry.second)
Anthony Wilsond5005492019-07-31 16:34:17 -05001768 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001769 if (interface == "xyz.openbmc_project.Association")
Anthony Wilsond5005492019-07-31 16:34:17 -05001770 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001771 for (const auto& [valueName, value] : values)
Anthony Wilsond5005492019-07-31 16:34:17 -05001772 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001773 if (valueName == "endpoints")
1774 {
1775 const std::vector<std::string>* endpoints =
1776 std::get_if<std::vector<std::string>>(
1777 &value);
1778 if ((endpoints != nullptr) &&
1779 !endpoints->empty())
1780 {
1781 // Add inventory item to vector
1782 // Store LED path in inventory item
1783 const std::string& ledPath =
1784 endpoints->front();
1785 inventoryItem.ledObjectPath = ledPath;
1786 }
1787 }
Anthony Wilsond5005492019-07-31 16:34:17 -05001788 }
1789 }
1790 }
Ed Tanous711ac7a2021-12-20 09:34:41 -08001791
Anthony Wilsond5005492019-07-31 16:34:17 -05001792 break;
1793 }
1794 }
1795 }
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001796 callback(inventoryItems);
1797 BMCWEB_LOG_DEBUG << "getInventoryItemAssociations respHandler exit";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001798 };
1799
1800 // Find DBus object path that implements ObjectManager for ObjectMapper
1801 std::string connection = "xyz.openbmc_project.ObjectMapper";
1802 auto iter = objectMgrPaths->find(connection);
1803 const std::string& objectMgrPath =
1804 (iter != objectMgrPaths->end()) ? iter->second : "/";
1805 BMCWEB_LOG_DEBUG << "ObjectManager path for " << connection << " is "
1806 << objectMgrPath;
1807
1808 // Call GetManagedObjects on the ObjectMapper to get all associations
1809 crow::connections::systemBus->async_method_call(
1810 std::move(respHandler), connection, objectMgrPath,
1811 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
1812
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001813 BMCWEB_LOG_DEBUG << "getInventoryItemAssociations exit";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001814}
1815
1816/**
Anthony Wilsond5005492019-07-31 16:34:17 -05001817 * @brief Gets D-Bus data for inventory item leds associated with sensors.
1818 *
1819 * Uses the specified connections (services) to obtain D-Bus data for inventory
1820 * item leds associated with sensors. Stores the resulting data in the
1821 * inventoryItems vector.
1822 *
1823 * This data is later used to provide sensor property values in the JSON
1824 * response.
1825 *
1826 * Finds the inventory item led data asynchronously. Invokes callback when data
1827 * has been obtained.
1828 *
1829 * The callback must have the following signature:
1830 * @code
Gunnar Mills42cbe532019-08-15 15:26:54 -05001831 * callback()
Anthony Wilsond5005492019-07-31 16:34:17 -05001832 * @endcode
1833 *
1834 * This function is called recursively, obtaining data asynchronously from one
1835 * connection in each call. This ensures the callback is not invoked until the
1836 * last asynchronous function has completed.
1837 *
1838 * @param sensorsAsyncResp Pointer to object holding response data.
1839 * @param inventoryItems D-Bus inventory items associated with sensors.
1840 * @param ledConnections Connections that provide data for the inventory leds.
1841 * @param callback Callback to invoke when inventory data has been obtained.
1842 * @param ledConnectionsIndex Current index in ledConnections. Only specified
1843 * in recursive calls to this function.
1844 */
1845template <typename Callback>
1846void getInventoryLedData(
1847 std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
1848 std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
Nan Zhoufe04d492022-06-22 17:10:41 +00001849 std::shared_ptr<std::map<std::string, std::string>> ledConnections,
Anthony Wilsond5005492019-07-31 16:34:17 -05001850 Callback&& callback, size_t ledConnectionsIndex = 0)
1851{
1852 BMCWEB_LOG_DEBUG << "getInventoryLedData enter";
1853
1854 // If no more connections left, call callback
1855 if (ledConnectionsIndex >= ledConnections->size())
1856 {
Gunnar Mills42cbe532019-08-15 15:26:54 -05001857 callback();
Anthony Wilsond5005492019-07-31 16:34:17 -05001858 BMCWEB_LOG_DEBUG << "getInventoryLedData exit";
1859 return;
1860 }
1861
1862 // Get inventory item data from current connection
Nan Zhoufe04d492022-06-22 17:10:41 +00001863 auto it = ledConnections->begin();
1864 std::advance(it, ledConnectionsIndex);
Anthony Wilsond5005492019-07-31 16:34:17 -05001865 if (it != ledConnections->end())
1866 {
1867 const std::string& ledPath = (*it).first;
1868 const std::string& ledConnection = (*it).second;
1869 // Response handler for Get State property
Jonathan Doman1e1e5982021-06-11 09:36:17 -07001870 auto respHandler =
1871 [sensorsAsyncResp, inventoryItems, ledConnections, ledPath,
Ed Tanousf94c4ec2022-01-06 12:44:41 -08001872 callback{std::forward<Callback>(callback)}, ledConnectionsIndex](
Jonathan Doman1e1e5982021-06-11 09:36:17 -07001873 const boost::system::error_code ec, const std::string& state) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001874 BMCWEB_LOG_DEBUG << "getInventoryLedData respHandler enter";
1875 if (ec)
1876 {
1877 BMCWEB_LOG_ERROR
1878 << "getInventoryLedData respHandler DBus error " << ec;
1879 messages::internalError(sensorsAsyncResp->asyncResp->res);
1880 return;
1881 }
1882
1883 BMCWEB_LOG_DEBUG << "Led state: " << state;
1884 // Find inventory item with this LED object path
1885 InventoryItem* inventoryItem =
1886 findInventoryItemForLed(*inventoryItems, ledPath);
1887 if (inventoryItem != nullptr)
1888 {
1889 // Store LED state in InventoryItem
Ed Tanous11ba3972022-07-11 09:50:41 -07001890 if (state.ends_with("On"))
Jonathan Doman1e1e5982021-06-11 09:36:17 -07001891 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001892 inventoryItem->ledState = LedState::ON;
Jonathan Doman1e1e5982021-06-11 09:36:17 -07001893 }
Ed Tanous11ba3972022-07-11 09:50:41 -07001894 else if (state.ends_with("Blink"))
Anthony Wilsond5005492019-07-31 16:34:17 -05001895 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001896 inventoryItem->ledState = LedState::BLINK;
Anthony Wilsond5005492019-07-31 16:34:17 -05001897 }
Ed Tanous11ba3972022-07-11 09:50:41 -07001898 else if (state.ends_with("Off"))
Ed Tanous002d39b2022-05-31 08:59:27 -07001899 {
1900 inventoryItem->ledState = LedState::OFF;
1901 }
1902 else
1903 {
1904 inventoryItem->ledState = LedState::UNKNOWN;
1905 }
1906 }
Anthony Wilsond5005492019-07-31 16:34:17 -05001907
Ed Tanous002d39b2022-05-31 08:59:27 -07001908 // Recurse to get LED data from next connection
1909 getInventoryLedData(sensorsAsyncResp, inventoryItems,
1910 ledConnections, std::move(callback),
1911 ledConnectionsIndex + 1);
Anthony Wilsond5005492019-07-31 16:34:17 -05001912
Ed Tanous002d39b2022-05-31 08:59:27 -07001913 BMCWEB_LOG_DEBUG << "getInventoryLedData respHandler exit";
1914 };
Anthony Wilsond5005492019-07-31 16:34:17 -05001915
1916 // Get the State property for the current LED
Jonathan Doman1e1e5982021-06-11 09:36:17 -07001917 sdbusplus::asio::getProperty<std::string>(
1918 *crow::connections::systemBus, ledConnection, ledPath,
1919 "xyz.openbmc_project.Led.Physical", "State",
1920 std::move(respHandler));
Anthony Wilsond5005492019-07-31 16:34:17 -05001921 }
1922
1923 BMCWEB_LOG_DEBUG << "getInventoryLedData exit";
1924}
1925
1926/**
1927 * @brief Gets LED data for LEDs associated with given inventory items.
1928 *
1929 * Gets the D-Bus connections (services) that provide LED data for the LEDs
1930 * associated with the specified inventory items. Then gets the LED data from
1931 * each connection and stores it in the inventory item.
1932 *
1933 * This data is later used to provide sensor property values in the JSON
1934 * response.
1935 *
1936 * Finds the LED data asynchronously. Invokes callback when information has
1937 * been obtained.
1938 *
1939 * The callback must have the following signature:
1940 * @code
Gunnar Mills42cbe532019-08-15 15:26:54 -05001941 * callback()
Anthony Wilsond5005492019-07-31 16:34:17 -05001942 * @endcode
1943 *
1944 * @param sensorsAsyncResp Pointer to object holding response data.
1945 * @param inventoryItems D-Bus inventory items associated with sensors.
1946 * @param callback Callback to invoke when inventory items have been obtained.
1947 */
1948template <typename Callback>
1949void getInventoryLeds(
1950 std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
1951 std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
1952 Callback&& callback)
1953{
1954 BMCWEB_LOG_DEBUG << "getInventoryLeds enter";
1955
1956 const std::string path = "/xyz/openbmc_project";
1957 const std::array<std::string, 1> interfaces = {
1958 "xyz.openbmc_project.Led.Physical"};
1959
1960 // Response handler for parsing output from GetSubTree
Ed Tanous002d39b2022-05-31 08:59:27 -07001961 auto respHandler =
1962 [callback{std::forward<Callback>(callback)}, sensorsAsyncResp,
1963 inventoryItems](
1964 const boost::system::error_code ec,
1965 const dbus::utility::MapperGetSubTreeResponse& subtree) {
Anthony Wilsond5005492019-07-31 16:34:17 -05001966 BMCWEB_LOG_DEBUG << "getInventoryLeds respHandler enter";
1967 if (ec)
1968 {
zhanghch058d1b46d2021-04-01 11:18:24 +08001969 messages::internalError(sensorsAsyncResp->asyncResp->res);
Anthony Wilsond5005492019-07-31 16:34:17 -05001970 BMCWEB_LOG_ERROR << "getInventoryLeds respHandler DBus error "
1971 << ec;
1972 return;
1973 }
1974
1975 // Build map of LED object paths to connections
Nan Zhoufe04d492022-06-22 17:10:41 +00001976 std::shared_ptr<std::map<std::string, std::string>> ledConnections =
1977 std::make_shared<std::map<std::string, std::string>>();
Anthony Wilsond5005492019-07-31 16:34:17 -05001978
1979 // Loop through objects from GetSubTree
1980 for (const std::pair<
1981 std::string,
1982 std::vector<std::pair<std::string, std::vector<std::string>>>>&
1983 object : subtree)
1984 {
1985 // Check if object path is LED for one of the specified inventory
1986 // items
1987 const std::string& ledPath = object.first;
1988 if (findInventoryItemForLed(*inventoryItems, ledPath) != nullptr)
1989 {
1990 // Add mapping from ledPath to connection
1991 const std::string& connection = object.second.begin()->first;
1992 (*ledConnections)[ledPath] = connection;
1993 BMCWEB_LOG_DEBUG << "Added mapping " << ledPath << " -> "
1994 << connection;
1995 }
1996 }
1997
1998 getInventoryLedData(sensorsAsyncResp, inventoryItems, ledConnections,
1999 std::move(callback));
2000 BMCWEB_LOG_DEBUG << "getInventoryLeds respHandler exit";
2001 };
2002 // Make call to ObjectMapper to find all inventory items
2003 crow::connections::systemBus->async_method_call(
2004 std::move(respHandler), "xyz.openbmc_project.ObjectMapper",
2005 "/xyz/openbmc_project/object_mapper",
2006 "xyz.openbmc_project.ObjectMapper", "GetSubTree", path, 0, interfaces);
2007 BMCWEB_LOG_DEBUG << "getInventoryLeds exit";
2008}
2009
2010/**
Gunnar Mills42cbe532019-08-15 15:26:54 -05002011 * @brief Gets D-Bus data for Power Supply Attributes such as EfficiencyPercent
2012 *
2013 * Uses the specified connections (services) (currently assumes just one) to
2014 * obtain D-Bus data for Power Supply Attributes. Stores the resulting data in
2015 * the inventoryItems vector. Only stores data in Power Supply inventoryItems.
2016 *
2017 * This data is later used to provide sensor property values in the JSON
2018 * response.
2019 *
2020 * Finds the Power Supply Attributes data asynchronously. Invokes callback
2021 * when data has been obtained.
2022 *
2023 * The callback must have the following signature:
2024 * @code
2025 * callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems)
2026 * @endcode
2027 *
2028 * @param sensorsAsyncResp Pointer to object holding response data.
2029 * @param inventoryItems D-Bus inventory items associated with sensors.
2030 * @param psAttributesConnections Connections that provide data for the Power
2031 * Supply Attributes
2032 * @param callback Callback to invoke when data has been obtained.
2033 */
2034template <typename Callback>
2035void getPowerSupplyAttributesData(
Ed Tanousb5a76932020-09-29 16:16:58 -07002036 const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
Gunnar Mills42cbe532019-08-15 15:26:54 -05002037 std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
Nan Zhoufe04d492022-06-22 17:10:41 +00002038 const std::map<std::string, std::string>& psAttributesConnections,
Gunnar Mills42cbe532019-08-15 15:26:54 -05002039 Callback&& callback)
2040{
2041 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributesData enter";
2042
2043 if (psAttributesConnections.empty())
2044 {
2045 BMCWEB_LOG_DEBUG << "Can't find PowerSupplyAttributes, no connections!";
2046 callback(inventoryItems);
2047 return;
2048 }
2049
2050 // Assuming just one connection (service) for now
Nan Zhoufe04d492022-06-22 17:10:41 +00002051 auto it = psAttributesConnections.begin();
Gunnar Mills42cbe532019-08-15 15:26:54 -05002052
2053 const std::string& psAttributesPath = (*it).first;
2054 const std::string& psAttributesConnection = (*it).second;
2055
2056 // Response handler for Get DeratingFactor property
Ed Tanous002d39b2022-05-31 08:59:27 -07002057 auto respHandler =
2058 [sensorsAsyncResp, inventoryItems,
2059 callback{std::forward<Callback>(callback)}](
2060 const boost::system::error_code ec, const uint32_t value) {
Gunnar Mills42cbe532019-08-15 15:26:54 -05002061 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributesData respHandler enter";
2062 if (ec)
2063 {
2064 BMCWEB_LOG_ERROR
2065 << "getPowerSupplyAttributesData respHandler DBus error " << ec;
zhanghch058d1b46d2021-04-01 11:18:24 +08002066 messages::internalError(sensorsAsyncResp->asyncResp->res);
Gunnar Mills42cbe532019-08-15 15:26:54 -05002067 return;
2068 }
2069
Jonathan Doman1e1e5982021-06-11 09:36:17 -07002070 BMCWEB_LOG_DEBUG << "PS EfficiencyPercent value: " << value;
2071 // Store value in Power Supply Inventory Items
2072 for (InventoryItem& inventoryItem : *inventoryItems)
Gunnar Mills42cbe532019-08-15 15:26:54 -05002073 {
Ed Tanous55f79e62022-01-25 11:26:16 -08002074 if (inventoryItem.isPowerSupply)
Gunnar Mills42cbe532019-08-15 15:26:54 -05002075 {
Jonathan Doman1e1e5982021-06-11 09:36:17 -07002076 inventoryItem.powerSupplyEfficiencyPercent =
2077 static_cast<int>(value);
Gunnar Mills42cbe532019-08-15 15:26:54 -05002078 }
2079 }
Gunnar Mills42cbe532019-08-15 15:26:54 -05002080
2081 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributesData respHandler exit";
2082 callback(inventoryItems);
2083 };
2084
2085 // Get the DeratingFactor property for the PowerSupplyAttributes
2086 // Currently only property on the interface/only one we care about
Jonathan Doman1e1e5982021-06-11 09:36:17 -07002087 sdbusplus::asio::getProperty<uint32_t>(
2088 *crow::connections::systemBus, psAttributesConnection, psAttributesPath,
2089 "xyz.openbmc_project.Control.PowerSupplyAttributes", "DeratingFactor",
2090 std::move(respHandler));
Gunnar Mills42cbe532019-08-15 15:26:54 -05002091
2092 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributesData exit";
2093}
2094
2095/**
2096 * @brief Gets the Power Supply Attributes such as EfficiencyPercent
2097 *
2098 * Gets the D-Bus connection (service) that provides Power Supply Attributes
2099 * data. Then gets the Power Supply Attributes data from the connection
2100 * (currently just assumes 1 connection) and stores the data in the inventory
2101 * item.
2102 *
2103 * This data is later used to provide sensor property values in the JSON
2104 * response. DeratingFactor on D-Bus is mapped to EfficiencyPercent on Redfish.
2105 *
2106 * Finds the Power Supply Attributes data asynchronously. Invokes callback
2107 * when information has been obtained.
2108 *
2109 * The callback must have the following signature:
2110 * @code
2111 * callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems)
2112 * @endcode
2113 *
2114 * @param sensorsAsyncResp Pointer to object holding response data.
2115 * @param inventoryItems D-Bus inventory items associated with sensors.
2116 * @param callback Callback to invoke when data has been obtained.
2117 */
2118template <typename Callback>
2119void getPowerSupplyAttributes(
2120 std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
2121 std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
2122 Callback&& callback)
2123{
2124 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes enter";
2125
2126 // Only need the power supply attributes when the Power Schema
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02002127 if (sensorsAsyncResp->chassisSubNode != sensors::node::power)
Gunnar Mills42cbe532019-08-15 15:26:54 -05002128 {
2129 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes exit since not Power";
2130 callback(inventoryItems);
2131 return;
2132 }
2133
2134 const std::array<std::string, 1> interfaces = {
2135 "xyz.openbmc_project.Control.PowerSupplyAttributes"};
2136
2137 // Response handler for parsing output from GetSubTree
Ed Tanousb9d36b42022-02-26 21:42:46 -08002138 auto respHandler =
2139 [callback{std::forward<Callback>(callback)}, sensorsAsyncResp,
2140 inventoryItems](
2141 const boost::system::error_code ec,
2142 const dbus::utility::MapperGetSubTreeResponse& subtree) {
Ed Tanous002d39b2022-05-31 08:59:27 -07002143 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes respHandler enter";
2144 if (ec)
2145 {
2146 messages::internalError(sensorsAsyncResp->asyncResp->res);
2147 BMCWEB_LOG_ERROR
2148 << "getPowerSupplyAttributes respHandler DBus error " << ec;
2149 return;
2150 }
2151 if (subtree.empty())
2152 {
2153 BMCWEB_LOG_DEBUG << "Can't find Power Supply Attributes!";
2154 callback(inventoryItems);
2155 return;
2156 }
Gunnar Mills42cbe532019-08-15 15:26:54 -05002157
Ed Tanous002d39b2022-05-31 08:59:27 -07002158 // Currently we only support 1 power supply attribute, use this for
2159 // all the power supplies. Build map of object path to connection.
2160 // Assume just 1 connection and 1 path for now.
Nan Zhoufe04d492022-06-22 17:10:41 +00002161 std::map<std::string, std::string> psAttributesConnections;
Gunnar Mills42cbe532019-08-15 15:26:54 -05002162
Ed Tanous002d39b2022-05-31 08:59:27 -07002163 if (subtree[0].first.empty() || subtree[0].second.empty())
2164 {
2165 BMCWEB_LOG_DEBUG << "Power Supply Attributes mapper error!";
2166 callback(inventoryItems);
2167 return;
2168 }
Gunnar Mills42cbe532019-08-15 15:26:54 -05002169
Ed Tanous002d39b2022-05-31 08:59:27 -07002170 const std::string& psAttributesPath = subtree[0].first;
2171 const std::string& connection = subtree[0].second.begin()->first;
Gunnar Mills42cbe532019-08-15 15:26:54 -05002172
Ed Tanous002d39b2022-05-31 08:59:27 -07002173 if (connection.empty())
2174 {
2175 BMCWEB_LOG_DEBUG << "Power Supply Attributes mapper error!";
2176 callback(inventoryItems);
2177 return;
2178 }
Gunnar Mills42cbe532019-08-15 15:26:54 -05002179
Ed Tanous002d39b2022-05-31 08:59:27 -07002180 psAttributesConnections[psAttributesPath] = connection;
2181 BMCWEB_LOG_DEBUG << "Added mapping " << psAttributesPath << " -> "
2182 << connection;
Gunnar Mills42cbe532019-08-15 15:26:54 -05002183
Ed Tanous002d39b2022-05-31 08:59:27 -07002184 getPowerSupplyAttributesData(sensorsAsyncResp, inventoryItems,
2185 psAttributesConnections,
2186 std::move(callback));
2187 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes respHandler exit";
2188 };
Gunnar Mills42cbe532019-08-15 15:26:54 -05002189 // Make call to ObjectMapper to find the PowerSupplyAttributes service
2190 crow::connections::systemBus->async_method_call(
2191 std::move(respHandler), "xyz.openbmc_project.ObjectMapper",
2192 "/xyz/openbmc_project/object_mapper",
2193 "xyz.openbmc_project.ObjectMapper", "GetSubTree",
2194 "/xyz/openbmc_project", 0, interfaces);
2195 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes exit";
2196}
2197
2198/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002199 * @brief Gets inventory items associated with sensors.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002200 *
2201 * Finds the inventory items that are associated with the specified sensors.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002202 * Then gets D-Bus data for the inventory items, such as presence and VPD.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002203 *
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002204 * This data is later used to provide sensor property values in the JSON
2205 * response.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002206 *
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002207 * Finds the inventory items asynchronously. Invokes callback when the
2208 * inventory items have been obtained.
2209 *
2210 * The callback must have the following signature:
2211 * @code
2212 * callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems)
2213 * @endcode
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002214 *
2215 * @param sensorsAsyncResp Pointer to object holding response data.
2216 * @param sensorNames All sensors within the current chassis.
2217 * @param objectMgrPaths Mappings from connection name to DBus object path that
2218 * implements ObjectManager.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002219 * @param callback Callback to invoke when inventory items have been obtained.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002220 */
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002221template <typename Callback>
2222static void getInventoryItems(
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002223 std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
Nan Zhoufe04d492022-06-22 17:10:41 +00002224 const std::shared_ptr<std::set<std::string>> sensorNames,
2225 std::shared_ptr<std::map<std::string, std::string>> objectMgrPaths,
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002226 Callback&& callback)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002227{
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002228 BMCWEB_LOG_DEBUG << "getInventoryItems enter";
2229 auto getInventoryItemAssociationsCb =
Ed Tanousf94c4ec2022-01-06 12:44:41 -08002230 [sensorsAsyncResp, objectMgrPaths,
2231 callback{std::forward<Callback>(callback)}](
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002232 std::shared_ptr<std::vector<InventoryItem>> inventoryItems) {
Ed Tanous002d39b2022-05-31 08:59:27 -07002233 BMCWEB_LOG_DEBUG << "getInventoryItemAssociationsCb enter";
2234 auto getInventoryItemsConnectionsCb =
2235 [sensorsAsyncResp, inventoryItems, objectMgrPaths,
2236 callback{std::forward<const Callback>(callback)}](
Nan Zhoufe04d492022-06-22 17:10:41 +00002237 std::shared_ptr<std::set<std::string>> invConnections) {
Ed Tanous002d39b2022-05-31 08:59:27 -07002238 BMCWEB_LOG_DEBUG << "getInventoryItemsConnectionsCb enter";
2239 auto getInventoryItemsDataCb = [sensorsAsyncResp, inventoryItems,
2240 callback{std::move(callback)}]() {
2241 BMCWEB_LOG_DEBUG << "getInventoryItemsDataCb enter";
Gunnar Mills42cbe532019-08-15 15:26:54 -05002242
Ed Tanous002d39b2022-05-31 08:59:27 -07002243 auto getInventoryLedsCb = [sensorsAsyncResp, inventoryItems,
2244 callback{std::move(callback)}]() {
2245 BMCWEB_LOG_DEBUG << "getInventoryLedsCb enter";
2246 // Find Power Supply Attributes and get the data
2247 getPowerSupplyAttributes(sensorsAsyncResp, inventoryItems,
2248 std::move(callback));
2249 BMCWEB_LOG_DEBUG << "getInventoryLedsCb exit";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002250 };
2251
Ed Tanous002d39b2022-05-31 08:59:27 -07002252 // Find led connections and get the data
2253 getInventoryLeds(sensorsAsyncResp, inventoryItems,
2254 std::move(getInventoryLedsCb));
2255 BMCWEB_LOG_DEBUG << "getInventoryItemsDataCb exit";
2256 };
2257
2258 // Get inventory item data from connections
2259 getInventoryItemsData(sensorsAsyncResp, inventoryItems,
2260 invConnections, objectMgrPaths,
2261 std::move(getInventoryItemsDataCb));
2262 BMCWEB_LOG_DEBUG << "getInventoryItemsConnectionsCb exit";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002263 };
2264
Ed Tanous002d39b2022-05-31 08:59:27 -07002265 // Get connections that provide inventory item data
2266 getInventoryItemsConnections(sensorsAsyncResp, inventoryItems,
2267 std::move(getInventoryItemsConnectionsCb));
2268 BMCWEB_LOG_DEBUG << "getInventoryItemAssociationsCb exit";
2269 };
2270
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002271 // Get associations from sensors to inventory items
2272 getInventoryItemAssociations(sensorsAsyncResp, sensorNames, objectMgrPaths,
2273 std::move(getInventoryItemAssociationsCb));
2274 BMCWEB_LOG_DEBUG << "getInventoryItems exit";
2275}
2276
2277/**
2278 * @brief Returns JSON PowerSupply object for the specified inventory item.
2279 *
2280 * Searches for a JSON PowerSupply object that matches the specified inventory
2281 * item. If one is not found, a new PowerSupply object is added to the JSON
2282 * array.
2283 *
2284 * Multiple sensors are often associated with one power supply inventory item.
2285 * As a result, multiple sensor values are stored in one JSON PowerSupply
2286 * object.
2287 *
2288 * @param powerSupplyArray JSON array containing Redfish PowerSupply objects.
2289 * @param inventoryItem Inventory item for the power supply.
2290 * @param chassisId Chassis that contains the power supply.
2291 * @return JSON PowerSupply object for the specified inventory item.
2292 */
Ed Tanous23a21a12020-07-25 04:45:05 +00002293inline nlohmann::json& getPowerSupply(nlohmann::json& powerSupplyArray,
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002294 const InventoryItem& inventoryItem,
2295 const std::string& chassisId)
2296{
2297 // Check if matching PowerSupply object already exists in JSON array
2298 for (nlohmann::json& powerSupply : powerSupplyArray)
2299 {
2300 if (powerSupply["MemberId"] == inventoryItem.name)
2301 {
2302 return powerSupply;
2303 }
2304 }
2305
2306 // Add new PowerSupply object to JSON array
2307 powerSupplyArray.push_back({});
2308 nlohmann::json& powerSupply = powerSupplyArray.back();
2309 powerSupply["@odata.id"] =
2310 "/redfish/v1/Chassis/" + chassisId + "/Power#/PowerSupplies/";
2311 powerSupply["MemberId"] = inventoryItem.name;
2312 powerSupply["Name"] = boost::replace_all_copy(inventoryItem.name, "_", " ");
2313 powerSupply["Manufacturer"] = inventoryItem.manufacturer;
2314 powerSupply["Model"] = inventoryItem.model;
2315 powerSupply["PartNumber"] = inventoryItem.partNumber;
2316 powerSupply["SerialNumber"] = inventoryItem.serialNumber;
Anthony Wilsond5005492019-07-31 16:34:17 -05002317 setLedState(powerSupply, &inventoryItem);
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002318
Gunnar Mills42cbe532019-08-15 15:26:54 -05002319 if (inventoryItem.powerSupplyEfficiencyPercent >= 0)
2320 {
2321 powerSupply["EfficiencyPercent"] =
2322 inventoryItem.powerSupplyEfficiencyPercent;
2323 }
2324
2325 powerSupply["Status"]["State"] = getState(&inventoryItem);
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002326 const char* health = inventoryItem.isFunctional ? "OK" : "Critical";
2327 powerSupply["Status"]["Health"] = health;
2328
2329 return powerSupply;
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002330}
2331
2332/**
Shawn McCarneyde629b62019-03-08 10:42:51 -06002333 * @brief Gets the values of the specified sensors.
2334 *
2335 * Stores the results as JSON in the SensorsAsyncResp.
2336 *
2337 * Gets the sensor values asynchronously. Stores the results later when the
2338 * information has been obtained.
2339 *
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002340 * The sensorNames set contains all requested sensors for the current chassis.
Shawn McCarneyde629b62019-03-08 10:42:51 -06002341 *
2342 * To minimize the number of DBus calls, the DBus method
2343 * org.freedesktop.DBus.ObjectManager.GetManagedObjects() is used to get the
2344 * values of all sensors provided by a connection (service).
2345 *
2346 * The connections set contains all the connections that provide sensor values.
2347 *
2348 * The objectMgrPaths map contains mappings from a connection name to the
2349 * corresponding DBus object path that implements ObjectManager.
2350 *
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002351 * The InventoryItem vector contains D-Bus inventory items associated with the
2352 * sensors. Inventory item data is needed for some Redfish sensor properties.
2353 *
Shawn McCarneyde629b62019-03-08 10:42:51 -06002354 * @param SensorsAsyncResp Pointer to object holding response data.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002355 * @param sensorNames All requested sensors within the current chassis.
Shawn McCarneyde629b62019-03-08 10:42:51 -06002356 * @param connections Connections that provide sensor values.
2357 * @param objectMgrPaths Mappings from connection name to DBus object path that
2358 * implements ObjectManager.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002359 * @param inventoryItems Inventory items associated with the sensors.
Shawn McCarneyde629b62019-03-08 10:42:51 -06002360 */
Ed Tanous23a21a12020-07-25 04:45:05 +00002361inline void getSensorData(
Ed Tanous81ce6092020-12-17 16:54:55 +00002362 const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
Nan Zhoufe04d492022-06-22 17:10:41 +00002363 const std::shared_ptr<std::set<std::string>>& sensorNames,
2364 const std::set<std::string>& connections,
2365 const std::shared_ptr<std::map<std::string, std::string>>& objectMgrPaths,
Ed Tanousb5a76932020-09-29 16:16:58 -07002366 const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems)
Shawn McCarneyde629b62019-03-08 10:42:51 -06002367{
2368 BMCWEB_LOG_DEBUG << "getSensorData enter";
2369 // Get managed objects from all services exposing sensors
2370 for (const std::string& connection : connections)
2371 {
2372 // Response handler to process managed objects
Ed Tanous002d39b2022-05-31 08:59:27 -07002373 auto getManagedObjectsCb =
2374 [sensorsAsyncResp, sensorNames,
2375 inventoryItems](const boost::system::error_code ec,
Ed Tanous02cad962022-06-30 16:50:15 -07002376 const dbus::utility::ManagedObjectType& resp) {
Shawn McCarneyde629b62019-03-08 10:42:51 -06002377 BMCWEB_LOG_DEBUG << "getManagedObjectsCb enter";
2378 if (ec)
2379 {
2380 BMCWEB_LOG_ERROR << "getManagedObjectsCb DBUS error: " << ec;
zhanghch058d1b46d2021-04-01 11:18:24 +08002381 messages::internalError(sensorsAsyncResp->asyncResp->res);
Shawn McCarneyde629b62019-03-08 10:42:51 -06002382 return;
2383 }
2384 // Go through all objects and update response with sensor data
2385 for (const auto& objDictEntry : resp)
2386 {
2387 const std::string& objPath =
2388 static_cast<const std::string&>(objDictEntry.first);
2389 BMCWEB_LOG_DEBUG << "getManagedObjectsCb parsing object "
2390 << objPath;
2391
Shawn McCarneyde629b62019-03-08 10:42:51 -06002392 std::vector<std::string> split;
2393 // Reserve space for
2394 // /xyz/openbmc_project/sensors/<name>/<subname>
2395 split.reserve(6);
2396 boost::algorithm::split(split, objPath, boost::is_any_of("/"));
2397 if (split.size() < 6)
2398 {
2399 BMCWEB_LOG_ERROR << "Got path that isn't long enough "
2400 << objPath;
2401 continue;
2402 }
2403 // These indexes aren't intuitive, as boost::split puts an empty
2404 // string at the beginning
2405 const std::string& sensorType = split[4];
2406 const std::string& sensorName = split[5];
2407 BMCWEB_LOG_DEBUG << "sensorName " << sensorName
2408 << " sensorType " << sensorType;
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002409 if (sensorNames->find(objPath) == sensorNames->end())
Shawn McCarneyde629b62019-03-08 10:42:51 -06002410 {
Andrew Geissleraccdbb22021-11-09 15:24:45 -06002411 BMCWEB_LOG_DEBUG << sensorName << " not in sensor list ";
Shawn McCarneyde629b62019-03-08 10:42:51 -06002412 continue;
2413 }
2414
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002415 // Find inventory item (if any) associated with sensor
2416 InventoryItem* inventoryItem =
2417 findInventoryItemForSensor(inventoryItems, objPath);
2418
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002419 const std::string& sensorSchema =
Ed Tanous81ce6092020-12-17 16:54:55 +00002420 sensorsAsyncResp->chassisSubNode;
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002421
2422 nlohmann::json* sensorJson = nullptr;
2423
Nan Zhou928fefb2022-03-28 08:45:00 -07002424 if (sensorSchema == sensors::node::sensors &&
2425 !sensorsAsyncResp->efficientExpand)
Shawn McCarneyde629b62019-03-08 10:42:51 -06002426 {
Ed Tanousc1d019a2022-08-06 09:36:06 -07002427 std::string sensorTypeEscaped(sensorType);
2428 sensorTypeEscaped.erase(
2429 std::remove(sensorTypeEscaped.begin(),
2430 sensorTypeEscaped.end(), '_'),
2431 sensorTypeEscaped.end());
2432 std::string sensorId(sensorTypeEscaped);
2433 sensorId += "_";
2434 sensorId += sensorName;
2435
zhanghch058d1b46d2021-04-01 11:18:24 +08002436 sensorsAsyncResp->asyncResp->res.jsonValue["@odata.id"] =
Ed Tanousc1d019a2022-08-06 09:36:06 -07002437 crow::utility::urlFromPieces(
2438 "redfish", "v1", "Chassis",
2439 sensorsAsyncResp->chassisId,
2440 sensorsAsyncResp->chassisSubNode, sensorId);
zhanghch058d1b46d2021-04-01 11:18:24 +08002441 sensorJson = &(sensorsAsyncResp->asyncResp->res.jsonValue);
Shawn McCarneyde629b62019-03-08 10:42:51 -06002442 }
2443 else
2444 {
Ed Tanous271584a2019-07-09 16:24:22 -07002445 std::string fieldName;
Nan Zhou928fefb2022-03-28 08:45:00 -07002446 if (sensorsAsyncResp->efficientExpand)
2447 {
2448 fieldName = "Members";
2449 }
2450 else if (sensorType == "temperature")
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002451 {
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002452 fieldName = "Temperatures";
2453 }
2454 else if (sensorType == "fan" || sensorType == "fan_tach" ||
2455 sensorType == "fan_pwm")
2456 {
2457 fieldName = "Fans";
2458 }
2459 else if (sensorType == "voltage")
2460 {
2461 fieldName = "Voltages";
2462 }
2463 else if (sensorType == "power")
2464 {
Ed Tanous55f79e62022-01-25 11:26:16 -08002465 if (sensorName == "total_power")
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002466 {
2467 fieldName = "PowerControl";
2468 }
2469 else if ((inventoryItem != nullptr) &&
2470 (inventoryItem->isPowerSupply))
2471 {
2472 fieldName = "PowerSupplies";
2473 }
2474 else
2475 {
2476 // Other power sensors are in SensorCollection
2477 continue;
2478 }
2479 }
2480 else
2481 {
2482 BMCWEB_LOG_ERROR << "Unsure how to handle sensorType "
2483 << sensorType;
2484 continue;
2485 }
2486
2487 nlohmann::json& tempArray =
zhanghch058d1b46d2021-04-01 11:18:24 +08002488 sensorsAsyncResp->asyncResp->res.jsonValue[fieldName];
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002489 if (fieldName == "PowerControl")
2490 {
2491 if (tempArray.empty())
2492 {
2493 // Put multiple "sensors" into a single
2494 // PowerControl. Follows MemberId naming and
2495 // naming in power.hpp.
Ed Tanous14766872022-03-15 10:44:42 -07002496 nlohmann::json::object_t power;
2497 power["@odata.id"] =
2498 "/redfish/v1/Chassis/" +
2499 sensorsAsyncResp->chassisId + "/" +
2500 sensorsAsyncResp->chassisSubNode + "#/" +
2501 fieldName + "/0";
2502 tempArray.push_back(std::move(power));
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002503 }
2504 sensorJson = &(tempArray.back());
2505 }
2506 else if (fieldName == "PowerSupplies")
2507 {
2508 if (inventoryItem != nullptr)
2509 {
2510 sensorJson =
2511 &(getPowerSupply(tempArray, *inventoryItem,
Ed Tanous81ce6092020-12-17 16:54:55 +00002512 sensorsAsyncResp->chassisId));
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002513 }
2514 }
Nan Zhou928fefb2022-03-28 08:45:00 -07002515 else if (fieldName == "Members")
2516 {
Ed Tanous677bb752022-09-15 10:52:19 -07002517 std::string sensorTypeEscaped(sensorType);
2518 sensorTypeEscaped.erase(
2519 std::remove(sensorTypeEscaped.begin(),
2520 sensorTypeEscaped.end(), '_'),
2521 sensorTypeEscaped.end());
2522 std::string sensorId(sensorTypeEscaped);
2523 sensorId += "_";
2524 sensorId += sensorName;
2525
Ed Tanous14766872022-03-15 10:44:42 -07002526 nlohmann::json::object_t member;
Ed Tanous677bb752022-09-15 10:52:19 -07002527 member["@odata.id"] = crow::utility::urlFromPieces(
2528 "redfish", "v1", "Chassis",
2529 sensorsAsyncResp->chassisId,
2530 sensorsAsyncResp->chassisSubNode, sensorId);
Ed Tanous14766872022-03-15 10:44:42 -07002531 tempArray.push_back(std::move(member));
Nan Zhou928fefb2022-03-28 08:45:00 -07002532 sensorJson = &(tempArray.back());
2533 }
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002534 else
2535 {
Ed Tanous14766872022-03-15 10:44:42 -07002536 nlohmann::json::object_t member;
2537 member["@odata.id"] = "/redfish/v1/Chassis/" +
2538 sensorsAsyncResp->chassisId +
2539 "/" +
2540 sensorsAsyncResp->chassisSubNode +
2541 "#/" + fieldName + "/";
2542 tempArray.push_back(std::move(member));
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002543 sensorJson = &(tempArray.back());
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002544 }
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002545 }
Shawn McCarneyde629b62019-03-08 10:42:51 -06002546
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002547 if (sensorJson != nullptr)
2548 {
Ed Tanous1d7c0052022-08-09 12:32:26 -07002549 objectInterfacesToJson(sensorName, sensorType,
2550 sensorsAsyncResp->chassisSubNode,
2551 objDictEntry.second, *sensorJson,
2552 inventoryItem);
2553
2554 std::string path = "/xyz/openbmc_project/sensors/";
2555 path += sensorType;
2556 path += "/";
2557 path += sensorName;
Ed Tanousc1d019a2022-08-06 09:36:06 -07002558 sensorsAsyncResp->addMetadata(*sensorJson, path);
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002559 }
Shawn McCarneyde629b62019-03-08 10:42:51 -06002560 }
Ed Tanous81ce6092020-12-17 16:54:55 +00002561 if (sensorsAsyncResp.use_count() == 1)
James Feist8bd25cc2019-03-15 15:14:00 -07002562 {
Ed Tanous81ce6092020-12-17 16:54:55 +00002563 sortJSONResponse(sensorsAsyncResp);
Nan Zhou928fefb2022-03-28 08:45:00 -07002564 if (sensorsAsyncResp->chassisSubNode ==
2565 sensors::node::sensors &&
2566 sensorsAsyncResp->efficientExpand)
2567 {
2568 sensorsAsyncResp->asyncResp->res
2569 .jsonValue["Members@odata.count"] =
2570 sensorsAsyncResp->asyncResp->res.jsonValue["Members"]
2571 .size();
2572 }
2573 else if (sensorsAsyncResp->chassisSubNode ==
2574 sensors::node::thermal)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002575 {
Ed Tanous81ce6092020-12-17 16:54:55 +00002576 populateFanRedundancy(sensorsAsyncResp);
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002577 }
James Feist8bd25cc2019-03-15 15:14:00 -07002578 }
Shawn McCarneyde629b62019-03-08 10:42:51 -06002579 BMCWEB_LOG_DEBUG << "getManagedObjectsCb exit";
2580 };
2581
2582 // Find DBus object path that implements ObjectManager for the current
2583 // connection. If no mapping found, default to "/".
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002584 auto iter = objectMgrPaths->find(connection);
Shawn McCarneyde629b62019-03-08 10:42:51 -06002585 const std::string& objectMgrPath =
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002586 (iter != objectMgrPaths->end()) ? iter->second : "/";
Shawn McCarneyde629b62019-03-08 10:42:51 -06002587 BMCWEB_LOG_DEBUG << "ObjectManager path for " << connection << " is "
2588 << objectMgrPath;
2589
2590 crow::connections::systemBus->async_method_call(
2591 getManagedObjectsCb, connection, objectMgrPath,
2592 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
Ed Tanous23a21a12020-07-25 04:45:05 +00002593 }
Shawn McCarneyde629b62019-03-08 10:42:51 -06002594 BMCWEB_LOG_DEBUG << "getSensorData exit";
2595}
2596
Nan Zhoufe04d492022-06-22 17:10:41 +00002597inline void
2598 processSensorList(const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
2599 const std::shared_ptr<std::set<std::string>>& sensorNames)
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002600{
Nan Zhoufe04d492022-06-22 17:10:41 +00002601 auto getConnectionCb = [sensorsAsyncResp, sensorNames](
2602 const std::set<std::string>& connections) {
Ed Tanous002d39b2022-05-31 08:59:27 -07002603 BMCWEB_LOG_DEBUG << "getConnectionCb enter";
2604 auto getObjectManagerPathsCb =
Nan Zhoufe04d492022-06-22 17:10:41 +00002605 [sensorsAsyncResp, sensorNames, connections](
2606 const std::shared_ptr<std::map<std::string, std::string>>&
2607 objectMgrPaths) {
Ed Tanous002d39b2022-05-31 08:59:27 -07002608 BMCWEB_LOG_DEBUG << "getObjectManagerPathsCb enter";
2609 auto getInventoryItemsCb =
2610 [sensorsAsyncResp, sensorNames, connections, objectMgrPaths](
2611 const std::shared_ptr<std::vector<InventoryItem>>&
2612 inventoryItems) {
2613 BMCWEB_LOG_DEBUG << "getInventoryItemsCb enter";
2614 // Get sensor data and store results in JSON
2615 getSensorData(sensorsAsyncResp, sensorNames, connections,
2616 objectMgrPaths, inventoryItems);
2617 BMCWEB_LOG_DEBUG << "getInventoryItemsCb exit";
2618 };
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002619
Ed Tanous002d39b2022-05-31 08:59:27 -07002620 // Get inventory items associated with sensors
2621 getInventoryItems(sensorsAsyncResp, sensorNames, objectMgrPaths,
2622 std::move(getInventoryItemsCb));
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002623
Ed Tanous002d39b2022-05-31 08:59:27 -07002624 BMCWEB_LOG_DEBUG << "getObjectManagerPathsCb exit";
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002625 };
2626
Ed Tanous002d39b2022-05-31 08:59:27 -07002627 // Get mapping from connection names to the DBus object
2628 // paths that implement the ObjectManager interface
2629 getObjectManagerPaths(sensorsAsyncResp,
2630 std::move(getObjectManagerPathsCb));
2631 BMCWEB_LOG_DEBUG << "getConnectionCb exit";
2632 };
2633
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002634 // Get set of connections that provide sensor values
Ed Tanous81ce6092020-12-17 16:54:55 +00002635 getConnections(sensorsAsyncResp, sensorNames, std::move(getConnectionCb));
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002636}
2637
Shawn McCarneyde629b62019-03-08 10:42:51 -06002638/**
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +01002639 * @brief Entry point for retrieving sensors data related to requested
2640 * chassis.
Kowalski, Kamil588c3f02018-04-03 14:55:27 +02002641 * @param SensorsAsyncResp Pointer to object holding response data
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +01002642 */
Ed Tanousb5a76932020-09-29 16:16:58 -07002643inline void
Ed Tanous81ce6092020-12-17 16:54:55 +00002644 getChassisData(const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp)
Ed Tanous1abe55e2018-09-05 08:30:59 -07002645{
2646 BMCWEB_LOG_DEBUG << "getChassisData enter";
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002647 auto getChassisCb =
Ed Tanous81ce6092020-12-17 16:54:55 +00002648 [sensorsAsyncResp](
Nan Zhoufe04d492022-06-22 17:10:41 +00002649 const std::shared_ptr<std::set<std::string>>& sensorNames) {
Ed Tanous002d39b2022-05-31 08:59:27 -07002650 BMCWEB_LOG_DEBUG << "getChassisCb enter";
2651 processSensorList(sensorsAsyncResp, sensorNames);
2652 BMCWEB_LOG_DEBUG << "getChassisCb exit";
2653 };
Nan Zhou928fefb2022-03-28 08:45:00 -07002654 // SensorCollection doesn't contain the Redundancy property
2655 if (sensorsAsyncResp->chassisSubNode != sensors::node::sensors)
2656 {
2657 sensorsAsyncResp->asyncResp->res.jsonValue["Redundancy"] =
2658 nlohmann::json::array();
2659 }
Shawn McCarney26f03892019-05-03 13:20:24 -05002660 // Get set of sensors in chassis
Ed Tanous7f1cc262022-08-09 13:33:57 -07002661 getChassis(sensorsAsyncResp->asyncResp, sensorsAsyncResp->chassisId,
2662 sensorsAsyncResp->chassisSubNode, sensorsAsyncResp->types,
2663 std::move(getChassisCb));
Ed Tanous1abe55e2018-09-05 08:30:59 -07002664 BMCWEB_LOG_DEBUG << "getChassisData exit";
Ed Tanous271584a2019-07-09 16:24:22 -07002665}
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +01002666
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302667/**
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002668 * @brief Find the requested sensorName in the list of all sensors supplied by
2669 * the chassis node
2670 *
2671 * @param sensorName The sensor name supplied in the PATCH request
2672 * @param sensorsList The list of sensors managed by the chassis node
2673 * @param sensorsModified The list of sensors that were found as a result of
2674 * repeated calls to this function
2675 */
Nan Zhoufe04d492022-06-22 17:10:41 +00002676inline bool
2677 findSensorNameUsingSensorPath(std::string_view sensorName,
Ed Tanous02cad962022-06-30 16:50:15 -07002678 const std::set<std::string>& sensorsList,
Nan Zhoufe04d492022-06-22 17:10:41 +00002679 std::set<std::string>& sensorsModified)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002680{
Nan Zhoufe04d492022-06-22 17:10:41 +00002681 for (const auto& chassisSensor : sensorsList)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002682 {
George Liu28aa8de2021-02-01 15:13:30 +08002683 sdbusplus::message::object_path path(chassisSensor);
Ed Tanousb00dcc22021-02-23 12:52:50 -08002684 std::string thisSensorName = path.filename();
George Liu28aa8de2021-02-01 15:13:30 +08002685 if (thisSensorName.empty())
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002686 {
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002687 continue;
2688 }
2689 if (thisSensorName == sensorName)
2690 {
2691 sensorsModified.emplace(chassisSensor);
2692 return true;
2693 }
2694 }
2695 return false;
2696}
2697
2698/**
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302699 * @brief Entry point for overriding sensor values of given sensor
2700 *
zhanghch058d1b46d2021-04-01 11:18:24 +08002701 * @param sensorAsyncResp response object
Carol Wang4bb3dc32019-10-17 18:15:02 +08002702 * @param allCollections Collections extract from sensors' request patch info
jayaprakash Mutyala91e130a2020-03-04 22:26:38 +00002703 * @param chassisSubNode Chassis Node for which the query has to happen
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302704 */
Ed Tanous23a21a12020-07-25 04:45:05 +00002705inline void setSensorsOverride(
Ed Tanousb5a76932020-09-29 16:16:58 -07002706 const std::shared_ptr<SensorsAsyncResp>& sensorAsyncResp,
Carol Wang4bb3dc32019-10-17 18:15:02 +08002707 std::unordered_map<std::string, std::vector<nlohmann::json>>&
jayaprakash Mutyala397fd612020-02-06 23:33:34 +00002708 allCollections)
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302709{
jayaprakash Mutyala70d1d0a2020-01-21 23:41:36 +00002710 BMCWEB_LOG_INFO << "setSensorsOverride for subNode"
Carol Wang4bb3dc32019-10-17 18:15:02 +08002711 << sensorAsyncResp->chassisSubNode << "\n";
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302712
Ed Tanous543f4402022-01-06 13:12:53 -08002713 const char* propertyValueName = nullptr;
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302714 std::unordered_map<std::string, std::pair<double, std::string>> overrideMap;
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302715 std::string memberId;
Ed Tanous543f4402022-01-06 13:12:53 -08002716 double value = 0.0;
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302717 for (auto& collectionItems : allCollections)
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302718 {
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302719 if (collectionItems.first == "Temperatures")
2720 {
2721 propertyValueName = "ReadingCelsius";
2722 }
2723 else if (collectionItems.first == "Fans")
2724 {
2725 propertyValueName = "Reading";
2726 }
2727 else
2728 {
2729 propertyValueName = "ReadingVolts";
2730 }
2731 for (auto& item : collectionItems.second)
2732 {
zhanghch058d1b46d2021-04-01 11:18:24 +08002733 if (!json_util::readJson(item, sensorAsyncResp->asyncResp->res,
2734 "MemberId", memberId, propertyValueName,
2735 value))
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302736 {
2737 return;
2738 }
2739 overrideMap.emplace(memberId,
2740 std::make_pair(value, collectionItems.first));
2741 }
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302742 }
Carol Wang4bb3dc32019-10-17 18:15:02 +08002743
Ed Tanous002d39b2022-05-31 08:59:27 -07002744 auto getChassisSensorListCb =
2745 [sensorAsyncResp, overrideMap](
Nan Zhoufe04d492022-06-22 17:10:41 +00002746 const std::shared_ptr<std::set<std::string>>& sensorsList) {
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002747 // Match sensor names in the PATCH request to those managed by the
2748 // chassis node
Nan Zhoufe04d492022-06-22 17:10:41 +00002749 const std::shared_ptr<std::set<std::string>> sensorNames =
2750 std::make_shared<std::set<std::string>>();
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302751 for (const auto& item : overrideMap)
2752 {
2753 const auto& sensor = item.first;
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002754 if (!findSensorNameUsingSensorPath(sensor, *sensorsList,
2755 *sensorNames))
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302756 {
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302757 BMCWEB_LOG_INFO << "Unable to find memberId " << item.first;
zhanghch058d1b46d2021-04-01 11:18:24 +08002758 messages::resourceNotFound(sensorAsyncResp->asyncResp->res,
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302759 item.second.second, item.first);
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302760 return;
2761 }
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302762 }
2763 // Get the connection to which the memberId belongs
Ed Tanous002d39b2022-05-31 08:59:27 -07002764 auto getObjectsWithConnectionCb =
Nan Zhoufe04d492022-06-22 17:10:41 +00002765 [sensorAsyncResp,
2766 overrideMap](const std::set<std::string>& /*connections*/,
2767 const std::set<std::pair<std::string, std::string>>&
2768 objectsWithConnection) {
Jayaprakash Mutyala4f277b52021-12-08 22:46:49 +00002769 if (objectsWithConnection.size() != overrideMap.size())
2770 {
2771 BMCWEB_LOG_INFO
2772 << "Unable to find all objects with proper connection "
2773 << objectsWithConnection.size() << " requested "
2774 << overrideMap.size() << "\n";
2775 messages::resourceNotFound(sensorAsyncResp->asyncResp->res,
2776 sensorAsyncResp->chassisSubNode ==
2777 sensors::node::thermal
2778 ? "Temperatures"
2779 : "Voltages",
2780 "Count");
2781 return;
2782 }
2783 for (const auto& item : objectsWithConnection)
2784 {
2785 sdbusplus::message::object_path path(item.first);
2786 std::string sensorName = path.filename();
2787 if (sensorName.empty())
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302788 {
Jayaprakash Mutyala4f277b52021-12-08 22:46:49 +00002789 messages::internalError(sensorAsyncResp->asyncResp->res);
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302790 return;
2791 }
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302792
Jayaprakash Mutyala4f277b52021-12-08 22:46:49 +00002793 const auto& iterator = overrideMap.find(sensorName);
2794 if (iterator == overrideMap.end())
2795 {
2796 BMCWEB_LOG_INFO << "Unable to find sensor object"
2797 << item.first << "\n";
2798 messages::internalError(sensorAsyncResp->asyncResp->res);
2799 return;
2800 }
2801 crow::connections::systemBus->async_method_call(
2802 [sensorAsyncResp](const boost::system::error_code ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -07002803 if (ec)
2804 {
2805 if (ec.value() ==
2806 boost::system::errc::permission_denied)
Jayaprakash Mutyala4f277b52021-12-08 22:46:49 +00002807 {
Ed Tanous002d39b2022-05-31 08:59:27 -07002808 BMCWEB_LOG_WARNING
2809 << "Manufacturing mode is not Enabled...can't "
2810 "Override the sensor value. ";
Jayaprakash Mutyala4f277b52021-12-08 22:46:49 +00002811
Ed Tanous002d39b2022-05-31 08:59:27 -07002812 messages::insufficientPrivilege(
Jayaprakash Mutyala4f277b52021-12-08 22:46:49 +00002813 sensorAsyncResp->asyncResp->res);
Ed Tanous002d39b2022-05-31 08:59:27 -07002814 return;
Jayaprakash Mutyala4f277b52021-12-08 22:46:49 +00002815 }
Ed Tanous002d39b2022-05-31 08:59:27 -07002816 BMCWEB_LOG_DEBUG
2817 << "setOverrideValueStatus DBUS error: " << ec;
2818 messages::internalError(
2819 sensorAsyncResp->asyncResp->res);
2820 }
Jayaprakash Mutyala4f277b52021-12-08 22:46:49 +00002821 },
2822 item.second, item.first, "org.freedesktop.DBus.Properties",
2823 "Set", "xyz.openbmc_project.Sensor.Value", "Value",
Ed Tanous168e20c2021-12-13 14:39:53 -08002824 dbus::utility::DbusVariantType(iterator->second.first));
Jayaprakash Mutyala4f277b52021-12-08 22:46:49 +00002825 }
2826 };
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302827 // Get object with connection for the given sensor name
2828 getObjectsWithConnection(sensorAsyncResp, sensorNames,
2829 std::move(getObjectsWithConnectionCb));
2830 };
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302831 // get full sensor list for the given chassisId and cross verify the sensor.
Ed Tanous7f1cc262022-08-09 13:33:57 -07002832 getChassis(sensorAsyncResp->asyncResp, sensorAsyncResp->chassisId,
2833 sensorAsyncResp->chassisSubNode, sensorAsyncResp->types,
2834 std::move(getChassisSensorListCb));
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302835}
2836
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02002837/**
2838 * @brief Retrieves mapping of Redfish URIs to sensor value property to D-Bus
2839 * path of the sensor.
2840 *
2841 * Function builds valid Redfish response for sensor query of given chassis and
2842 * node. It then builds metadata about Redfish<->D-Bus correlations and provides
2843 * it to caller in a callback.
2844 *
2845 * @param chassis Chassis for which retrieval should be performed
2846 * @param node Node (group) of sensors. See sensors::node for supported values
2847 * @param mapComplete Callback to be called with retrieval result
2848 */
Krzysztof Grobelny021d32c2021-10-29 16:00:07 +02002849inline void retrieveUriToDbusMap(const std::string& chassis,
2850 const std::string& node,
2851 SensorsAsyncResp::DataCompleteCb&& mapComplete)
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02002852{
Ed Tanous02da7c52022-02-27 00:09:02 -08002853 decltype(sensors::paths)::const_iterator pathIt =
2854 std::find_if(sensors::paths.cbegin(), sensors::paths.cend(),
2855 [&node](auto&& val) { return val.first == node; });
2856 if (pathIt == sensors::paths.cend())
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02002857 {
2858 BMCWEB_LOG_ERROR << "Wrong node provided : " << node;
2859 mapComplete(boost::beast::http::status::bad_request, {});
2860 return;
2861 }
Krzysztof Grobelnyd51e0722021-04-16 13:15:21 +00002862
Nan Zhou72374eb2022-01-27 17:06:51 -08002863 auto asyncResp = std::make_shared<bmcweb::AsyncResp>();
Nan Zhoufe04d492022-06-22 17:10:41 +00002864 auto callback = [asyncResp, mapCompleteCb{std::move(mapComplete)}](
2865 const boost::beast::http::status status,
2866 const std::map<std::string, std::string>& uriToDbus) {
2867 mapCompleteCb(status, uriToDbus);
2868 };
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02002869
2870 auto resp = std::make_shared<SensorsAsyncResp>(
Krzysztof Grobelnyd51e0722021-04-16 13:15:21 +00002871 asyncResp, chassis, pathIt->second, node, std::move(callback));
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02002872 getChassisData(resp);
2873}
2874
Nan Zhoubacb2162022-04-06 11:28:32 -07002875namespace sensors
2876{
Nan Zhou928fefb2022-03-28 08:45:00 -07002877
Nan Zhoubacb2162022-04-06 11:28:32 -07002878inline void getChassisCallback(
Ed Tanousc1d019a2022-08-06 09:36:06 -07002879 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2880 std::string_view chassisId, std::string_view chassisSubNode,
Nan Zhoufe04d492022-06-22 17:10:41 +00002881 const std::shared_ptr<std::set<std::string>>& sensorNames)
Nan Zhoubacb2162022-04-06 11:28:32 -07002882{
Ed Tanousc1d019a2022-08-06 09:36:06 -07002883 BMCWEB_LOG_DEBUG << "getChassisCallback enter ";
Nan Zhoubacb2162022-04-06 11:28:32 -07002884
Ed Tanousc1d019a2022-08-06 09:36:06 -07002885 nlohmann::json& entriesArray = asyncResp->res.jsonValue["Members"];
2886 for (const std::string& sensor : *sensorNames)
Nan Zhoubacb2162022-04-06 11:28:32 -07002887 {
2888 BMCWEB_LOG_DEBUG << "Adding sensor: " << sensor;
2889
2890 sdbusplus::message::object_path path(sensor);
2891 std::string sensorName = path.filename();
2892 if (sensorName.empty())
2893 {
2894 BMCWEB_LOG_ERROR << "Invalid sensor path: " << sensor;
Ed Tanousc1d019a2022-08-06 09:36:06 -07002895 messages::internalError(asyncResp->res);
Nan Zhoubacb2162022-04-06 11:28:32 -07002896 return;
2897 }
Ed Tanousc1d019a2022-08-06 09:36:06 -07002898 std::string type = path.parent_path().filename();
2899 // fan_tach has an underscore in it, so remove it to "normalize" the
2900 // type in the URI
2901 type.erase(std::remove(type.begin(), type.end(), '_'), type.end());
2902
Ed Tanous14766872022-03-15 10:44:42 -07002903 nlohmann::json::object_t member;
Ed Tanousc1d019a2022-08-06 09:36:06 -07002904 std::string id = type;
2905 id += "_";
2906 id += sensorName;
2907 member["@odata.id"] = crow::utility::urlFromPieces(
2908 "redfish", "v1", "Chassis", chassisId, chassisSubNode, id);
2909
Ed Tanous14766872022-03-15 10:44:42 -07002910 entriesArray.push_back(std::move(member));
Nan Zhoubacb2162022-04-06 11:28:32 -07002911 }
2912
Ed Tanousc1d019a2022-08-06 09:36:06 -07002913 asyncResp->res.jsonValue["Members@odata.count"] = entriesArray.size();
Nan Zhoubacb2162022-04-06 11:28:32 -07002914 BMCWEB_LOG_DEBUG << "getChassisCallback exit";
2915}
Nan Zhoue6bd8462022-06-01 04:35:35 +00002916
Nan Zhoude167a62022-06-01 04:47:45 +00002917inline void
2918 handleSensorCollectionGet(App& app, const crow::Request& req,
2919 const std::shared_ptr<bmcweb::AsyncResp>& aResp,
2920 const std::string& chassisId)
2921{
2922 query_param::QueryCapabilities capabilities = {
2923 .canDelegateExpandLevel = 1,
2924 };
2925 query_param::Query delegatedQuery;
Carson Labrado3ba00072022-06-06 19:40:56 +00002926 if (!redfish::setUpRedfishRouteWithDelegation(app, req, aResp,
Nan Zhoude167a62022-06-01 04:47:45 +00002927 delegatedQuery, capabilities))
2928 {
2929 return;
2930 }
2931
2932 if (delegatedQuery.expandType != query_param::ExpandType::None)
2933 {
2934 // we perform efficient expand.
2935 auto asyncResp = std::make_shared<SensorsAsyncResp>(
2936 aResp, chassisId, sensors::dbus::sensorPaths,
2937 sensors::node::sensors,
2938 /*efficientExpand=*/true);
2939 getChassisData(asyncResp);
2940
2941 BMCWEB_LOG_DEBUG
2942 << "SensorCollection doGet exit via efficient expand handler";
2943 return;
Ed Tanous0bad3202022-06-02 13:53:59 -07002944 }
Nan Zhoude167a62022-06-01 04:47:45 +00002945
Nan Zhoude167a62022-06-01 04:47:45 +00002946 // We get all sensors as hyperlinkes in the chassis (this
2947 // implies we reply on the default query parameters handler)
Ed Tanousc1d019a2022-08-06 09:36:06 -07002948 getChassis(aResp, chassisId, sensors::node::sensors, dbus::sensorPaths,
2949 std::bind_front(sensors::getChassisCallback, aResp, chassisId,
2950 sensors::node::sensors));
2951}
Ed Tanous7f1cc262022-08-09 13:33:57 -07002952
Ed Tanousc1d019a2022-08-06 09:36:06 -07002953inline void
2954 getSensorFromDbus(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2955 const std::string& sensorPath,
2956 const ::dbus::utility::MapperGetObject& mapperResponse)
2957{
2958 if (mapperResponse.size() != 1)
2959 {
2960 messages::internalError(asyncResp->res);
2961 return;
2962 }
2963 const auto& valueIface = *mapperResponse.begin();
2964 const std::string& connectionName = valueIface.first;
2965 BMCWEB_LOG_DEBUG << "Looking up " << connectionName;
2966 BMCWEB_LOG_DEBUG << "Path " << sensorPath;
Krzysztof Grobelnyc1343bf2022-08-31 13:15:26 +02002967
2968 sdbusplus::asio::getAllProperties(
2969 *crow::connections::systemBus, connectionName, sensorPath, "",
Ed Tanousc1d019a2022-08-06 09:36:06 -07002970 [asyncResp,
2971 sensorPath](const boost::system::error_code ec,
2972 const ::dbus::utility::DBusPropertiesMap& valuesDict) {
2973 if (ec)
2974 {
2975 messages::internalError(asyncResp->res);
2976 return;
2977 }
2978 sdbusplus::message::object_path path(sensorPath);
2979 std::string name = path.filename();
2980 path = path.parent_path();
2981 std::string type = path.filename();
2982 objectPropertiesToJson(name, type, sensors::node::sensors, valuesDict,
2983 asyncResp->res.jsonValue, nullptr);
Krzysztof Grobelnyc1343bf2022-08-31 13:15:26 +02002984 });
Nan Zhoude167a62022-06-01 04:47:45 +00002985}
2986
Nan Zhoue6bd8462022-06-01 04:35:35 +00002987inline void handleSensorGet(App& app, const crow::Request& req,
Ed Tanousc1d019a2022-08-06 09:36:06 -07002988 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous677bb752022-09-15 10:52:19 -07002989 const std::string& chassisId,
Ed Tanousc1d019a2022-08-06 09:36:06 -07002990 const std::string& sensorId)
Nan Zhoue6bd8462022-06-01 04:35:35 +00002991{
Ed Tanousc1d019a2022-08-06 09:36:06 -07002992 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Nan Zhoue6bd8462022-06-01 04:35:35 +00002993 {
2994 return;
2995 }
Ed Tanousc1d019a2022-08-06 09:36:06 -07002996 size_t index = sensorId.find('_');
2997 if (index == std::string::npos)
2998 {
2999 messages::resourceNotFound(asyncResp->res, sensorId, "Sensor");
3000 return;
3001 }
Ed Tanous677bb752022-09-15 10:52:19 -07003002 asyncResp->res.jsonValue["@odata.id"] = crow::utility::urlFromPieces(
3003 "redfish", "v1", "Chassis", chassisId, "Sensors", sensorId);
Ed Tanousc1d019a2022-08-06 09:36:06 -07003004 std::string sensorType = sensorId.substr(0, index);
3005 std::string sensorName = sensorId.substr(index + 1);
3006 // fan_pwm and fan_tach need special handling
3007 if (sensorType == "fantach" || sensorType == "fanpwm")
3008 {
3009 sensorType.insert(3, 1, '_');
3010 }
3011
Nan Zhoue6bd8462022-06-01 04:35:35 +00003012 BMCWEB_LOG_DEBUG << "Sensor doGet enter";
Nan Zhoue6bd8462022-06-01 04:35:35 +00003013
3014 const std::array<const char*, 1> interfaces = {
3015 "xyz.openbmc_project.Sensor.Value"};
Ed Tanousc1d019a2022-08-06 09:36:06 -07003016 std::string sensorPath =
3017 "/xyz/openbmc_project/sensors/" + sensorType + '/' + sensorName;
Nan Zhoue6bd8462022-06-01 04:35:35 +00003018 // Get a list of all of the sensors that implement Sensor.Value
3019 // and get the path and service name associated with the sensor
3020 crow::connections::systemBus->async_method_call(
Ed Tanousc1d019a2022-08-06 09:36:06 -07003021 [asyncResp, sensorPath,
Nan Zhoue6bd8462022-06-01 04:35:35 +00003022 sensorName](const boost::system::error_code ec,
Ed Tanousc1d019a2022-08-06 09:36:06 -07003023 const ::dbus::utility::MapperGetObject& subtree) {
Nan Zhoue6bd8462022-06-01 04:35:35 +00003024 BMCWEB_LOG_DEBUG << "respHandler1 enter";
3025 if (ec)
3026 {
Ed Tanousc1d019a2022-08-06 09:36:06 -07003027 messages::internalError(asyncResp->res);
Nan Zhoue6bd8462022-06-01 04:35:35 +00003028 BMCWEB_LOG_ERROR << "Sensor getSensorPaths resp_handler: "
3029 << "Dbus error " << ec;
3030 return;
3031 }
Ed Tanousc1d019a2022-08-06 09:36:06 -07003032 getSensorFromDbus(asyncResp, sensorPath, subtree);
Nan Zhoue6bd8462022-06-01 04:35:35 +00003033 BMCWEB_LOG_DEBUG << "respHandler1 exit";
3034 },
3035 "xyz.openbmc_project.ObjectMapper",
3036 "/xyz/openbmc_project/object_mapper",
Ed Tanousc1d019a2022-08-06 09:36:06 -07003037 "xyz.openbmc_project.ObjectMapper", "GetObject", sensorPath,
3038 interfaces);
Nan Zhoue6bd8462022-06-01 04:35:35 +00003039}
3040
Nan Zhoubacb2162022-04-06 11:28:32 -07003041} // namespace sensors
3042
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003043inline void requestRoutesSensorCollection(App& app)
Anthony Wilson95a3eca2019-06-11 10:44:47 -05003044{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003045 BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/Sensors/")
Ed Tanoused398212021-06-09 17:05:54 -07003046 .privileges(redfish::privileges::getSensorCollection)
Ed Tanous002d39b2022-05-31 08:59:27 -07003047 .methods(boost::beast::http::verb::get)(
Nan Zhoude167a62022-06-01 04:47:45 +00003048 std::bind_front(sensors::handleSensorCollectionGet, std::ref(app)));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003049}
Anthony Wilson95a3eca2019-06-11 10:44:47 -05003050
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003051inline void requestRoutesSensor(App& app)
3052{
3053 BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/Sensors/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07003054 .privileges(redfish::privileges::getSensor)
Ed Tanous002d39b2022-05-31 08:59:27 -07003055 .methods(boost::beast::http::verb::get)(
Nan Zhoue6bd8462022-06-01 04:35:35 +00003056 std::bind_front(sensors::handleSensorGet, std::ref(app)));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003057}
Anthony Wilson95a3eca2019-06-11 10:44:47 -05003058
Ed Tanous1abe55e2018-09-05 08:30:59 -07003059} // namespace redfish