blob: 627ba7e50e677cc3d69ed92a685ce5e3b3b291e3 [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;
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700534 for (const std::string& chassis : chassisPaths)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700535 {
George Liu28aa8de2021-02-01 15:13:30 +0800536 sdbusplus::message::object_path path(chassis);
Ed Tanousf8fe53e2022-06-30 15:55:45 -0700537 std::string chassisName = path.filename();
George Liu28aa8de2021-02-01 15:13:30 +0800538 if (chassisName.empty())
Ed Tanous1abe55e2018-09-05 08:30:59 -0700539 {
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700540 BMCWEB_LOG_ERROR << "Failed to find '/' in " << chassis;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700541 continue;
542 }
Ed Tanous7f1cc262022-08-09 13:33:57 -0700543 if (chassisName == chassisIdStr)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700544 {
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700545 chassisPath = &chassis;
546 break;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700547 }
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700548 }
549 if (chassisPath == nullptr)
550 {
Ed Tanous7f1cc262022-08-09 13:33:57 -0700551 messages::resourceNotFound(asyncResp->res, "Chassis", chassisIdStr);
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700552 return;
553 }
Ed Tanous7f1cc262022-08-09 13:33:57 -0700554 populateChassisNode(asyncResp->res.jsonValue, chassisSubNode);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700555
Ed Tanous7f1cc262022-08-09 13:33:57 -0700556 asyncResp->res.jsonValue["@odata.id"] =
557 "/redfish/v1/Chassis/" + chassisIdStr + "/" + chassisSubNode;
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500558
Shawn McCarney8fb49dd2019-06-12 17:47:00 -0500559 // Get the list of all sensors for this Chassis element
560 std::string sensorPath = *chassisPath + "/all_sensors";
Jonathan Doman1e1e5982021-06-11 09:36:17 -0700561 sdbusplus::asio::getProperty<std::vector<std::string>>(
562 *crow::connections::systemBus, "xyz.openbmc_project.ObjectMapper",
563 sensorPath, "xyz.openbmc_project.Association", "endpoints",
Ed Tanous7f1cc262022-08-09 13:33:57 -0700564 [asyncResp, chassisSubNode, sensorTypes,
Ed Tanousf94c4ec2022-01-06 12:44:41 -0800565 callback{std::forward<const Callback>(callback)}](
Ed Tanous271584a2019-07-09 16:24:22 -0700566 const boost::system::error_code& e,
Jonathan Doman1e1e5982021-06-11 09:36:17 -0700567 const std::vector<std::string>& nodeSensorList) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700568 if (e)
569 {
570 if (e.value() != EBADR)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700571 {
Ed Tanous7f1cc262022-08-09 13:33:57 -0700572 messages::internalError(asyncResp->res);
Ed Tanous002d39b2022-05-31 08:59:27 -0700573 return;
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700574 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700575 }
Nan Zhoufe04d492022-06-22 17:10:41 +0000576 const std::shared_ptr<std::set<std::string>> culledSensorList =
577 std::make_shared<std::set<std::string>>();
Ed Tanous7f1cc262022-08-09 13:33:57 -0700578 reduceSensorList(asyncResp->res, chassisSubNode, sensorTypes,
579 &nodeSensorList, culledSensorList);
580 BMCWEB_LOG_DEBUG << "Finishing with " << culledSensorList->size();
Ed Tanous002d39b2022-05-31 08:59:27 -0700581 callback(culledSensorList);
Jonathan Doman1e1e5982021-06-11 09:36:17 -0700582 });
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100583 };
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100584
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700585 // Get the Chassis Collection
Ed Tanous1abe55e2018-09-05 08:30:59 -0700586 crow::connections::systemBus->async_method_call(
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700587 respHandler, "xyz.openbmc_project.ObjectMapper",
588 "/xyz/openbmc_project/object_mapper",
589 "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths",
Ed Tanous271584a2019-07-09 16:24:22 -0700590 "/xyz/openbmc_project/inventory", 0, interfaces);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700591 BMCWEB_LOG_DEBUG << "getChassis exit";
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100592}
593
594/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500595 * @brief Returns the Redfish State value for the specified inventory item.
596 * @param inventoryItem D-Bus inventory item associated with a sensor.
597 * @return State value for inventory item.
James Feist34dd1792019-05-17 14:10:54 -0700598 */
Ed Tanous23a21a12020-07-25 04:45:05 +0000599inline std::string getState(const InventoryItem* inventoryItem)
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500600{
601 if ((inventoryItem != nullptr) && !(inventoryItem->isPresent))
602 {
603 return "Absent";
604 }
James Feist34dd1792019-05-17 14:10:54 -0700605
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500606 return "Enabled";
607}
608
609/**
610 * @brief Returns the Redfish Health value for the specified sensor.
611 * @param sensorJson Sensor JSON object.
Ed Tanous1d7c0052022-08-09 12:32:26 -0700612 * @param valuesDict Map of all sensor DBus values.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500613 * @param inventoryItem D-Bus inventory item associated with the sensor. Will
614 * be nullptr if no associated inventory item was found.
615 * @return Health value for sensor.
616 */
Ed Tanous1d7c0052022-08-09 12:32:26 -0700617inline std::string getHealth(nlohmann::json& sensorJson,
618 const dbus::utility::DBusPropertiesMap& valuesDict,
619 const InventoryItem* inventoryItem)
James Feist34dd1792019-05-17 14:10:54 -0700620{
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500621 // Get current health value (if any) in the sensor JSON object. Some JSON
622 // objects contain multiple sensors (such as PowerSupplies). We want to set
623 // the overall health to be the most severe of any of the sensors.
624 std::string currentHealth;
625 auto statusIt = sensorJson.find("Status");
626 if (statusIt != sensorJson.end())
627 {
628 auto healthIt = statusIt->find("Health");
629 if (healthIt != statusIt->end())
630 {
631 std::string* health = healthIt->get_ptr<std::string*>();
632 if (health != nullptr)
633 {
634 currentHealth = *health;
635 }
636 }
637 }
638
639 // If current health in JSON object is already Critical, return that. This
640 // should override the sensor health, which might be less severe.
641 if (currentHealth == "Critical")
642 {
643 return "Critical";
644 }
645
Krzysztof Grobelnyc1343bf2022-08-31 13:15:26 +0200646 const bool* criticalAlarmHigh = nullptr;
647 const bool* criticalAlarmLow = nullptr;
648 const bool* warningAlarmHigh = nullptr;
649 const bool* warningAlarmLow = nullptr;
Ed Tanous711ac7a2021-12-20 09:34:41 -0800650
Krzysztof Grobelnyc1343bf2022-08-31 13:15:26 +0200651 const bool success = sdbusplus::unpackPropertiesNoThrow(
652 dbus_utils::UnpackErrorPrinter(), valuesDict, "CriticalAlarmHigh",
653 criticalAlarmHigh, "CriticalAlarmLow", criticalAlarmLow,
654 "WarningAlarmHigh", warningAlarmHigh, "WarningAlarmLow",
655 warningAlarmLow);
656
657 if (success)
James Feist34dd1792019-05-17 14:10:54 -0700658 {
Krzysztof Grobelnyc1343bf2022-08-31 13:15:26 +0200659 // Check if sensor has critical threshold alarm
660 if ((criticalAlarmHigh != nullptr && *criticalAlarmHigh) ||
661 (criticalAlarmLow != nullptr && *criticalAlarmLow))
James Feist34dd1792019-05-17 14:10:54 -0700662 {
Krzysztof Grobelnyc1343bf2022-08-31 13:15:26 +0200663 return "Critical";
James Feist34dd1792019-05-17 14:10:54 -0700664 }
665 }
666
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500667 // Check if associated inventory item is not functional
668 if ((inventoryItem != nullptr) && !(inventoryItem->isFunctional))
669 {
670 return "Critical";
671 }
672
Krzysztof Grobelnyc1343bf2022-08-31 13:15:26 +0200673 // If current health in JSON object is already Warning, return that. This
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500674 // should override the sensor status, which might be less severe.
675 if (currentHealth == "Warning")
676 {
677 return "Warning";
678 }
679
Krzysztof Grobelnyc1343bf2022-08-31 13:15:26 +0200680 if (success)
James Feist34dd1792019-05-17 14:10:54 -0700681 {
Krzysztof Grobelnyc1343bf2022-08-31 13:15:26 +0200682 // Check if sensor has warning threshold alarm
683 if ((warningAlarmHigh != nullptr && *warningAlarmHigh) ||
684 (warningAlarmLow != nullptr && *warningAlarmLow))
James Feist34dd1792019-05-17 14:10:54 -0700685 {
Krzysztof Grobelnyc1343bf2022-08-31 13:15:26 +0200686 return "Warning";
James Feist34dd1792019-05-17 14:10:54 -0700687 }
688 }
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500689
James Feist34dd1792019-05-17 14:10:54 -0700690 return "OK";
691}
692
Ed Tanous23a21a12020-07-25 04:45:05 +0000693inline void setLedState(nlohmann::json& sensorJson,
Anthony Wilsond5005492019-07-31 16:34:17 -0500694 const InventoryItem* inventoryItem)
695{
696 if (inventoryItem != nullptr && !inventoryItem->ledObjectPath.empty())
697 {
698 switch (inventoryItem->ledState)
699 {
700 case LedState::OFF:
701 sensorJson["IndicatorLED"] = "Off";
702 break;
703 case LedState::ON:
704 sensorJson["IndicatorLED"] = "Lit";
705 break;
706 case LedState::BLINK:
707 sensorJson["IndicatorLED"] = "Blinking";
708 break;
Ed Tanous23a21a12020-07-25 04:45:05 +0000709 case LedState::UNKNOWN:
Anthony Wilsond5005492019-07-31 16:34:17 -0500710 break;
711 }
712 }
713}
714
James Feist34dd1792019-05-17 14:10:54 -0700715/**
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100716 * @brief Builds a json sensor representation of a sensor.
717 * @param sensorName The name of the sensor to be built
Gunnar Mills274fad52018-06-13 15:45:36 -0500718 * @param sensorType The type (temperature, fan_tach, etc) of the sensor to
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100719 * build
Ed Tanous1d7c0052022-08-09 12:32:26 -0700720 * @param chassisSubNode The subnode (thermal, sensor, ect) of the sensor
721 * @param propertiesDict A dictionary of the properties to build the sensor
722 * from.
723 * @param sensorJson The json object to fill
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500724 * @param inventoryItem D-Bus inventory item associated with the sensor. Will
725 * be nullptr if no associated inventory item was found.
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100726 */
Ed Tanous1d7c0052022-08-09 12:32:26 -0700727inline void objectPropertiesToJson(
728 std::string_view sensorName, std::string_view sensorType,
729 std::string_view chassisSubNode,
730 const dbus::utility::DBusPropertiesMap& propertiesDict,
Ed Tanous81ce6092020-12-17 16:54:55 +0000731 nlohmann::json& sensorJson, InventoryItem* inventoryItem)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700732{
Ed Tanous1d7c0052022-08-09 12:32:26 -0700733 if (chassisSubNode == sensors::node::sensors)
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500734 {
Ed Tanousc1d019a2022-08-06 09:36:06 -0700735 std::string subNodeEscaped(chassisSubNode);
736 subNodeEscaped.erase(
737 std::remove(subNodeEscaped.begin(), subNodeEscaped.end(), '_'),
738 subNodeEscaped.end());
739
740 // For sensors in SensorCollection we set Id instead of MemberId,
741 // including power sensors.
742 subNodeEscaped += '_';
743 subNodeEscaped += sensorName;
744 sensorJson["Id"] = std::move(subNodeEscaped);
745
Ed Tanous1d7c0052022-08-09 12:32:26 -0700746 std::string sensorNameEs(sensorName);
747 std::replace(sensorNameEs.begin(), sensorNameEs.end(), '_', ' ');
748 sensorJson["Name"] = std::move(sensorNameEs);
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500749 }
750 else if (sensorType != "power")
751 {
752 // Set MemberId and Name for non-power sensors. For PowerSupplies and
753 // PowerControl, those properties have more general values because
754 // multiple sensors can be stored in the same JSON object.
Ed Tanous81ce6092020-12-17 16:54:55 +0000755 sensorJson["MemberId"] = sensorName;
Ed Tanous1d7c0052022-08-09 12:32:26 -0700756 std::string sensorNameEs(sensorName);
757 std::replace(sensorNameEs.begin(), sensorNameEs.end(), '_', ' ');
758 sensorJson["Name"] = std::move(sensorNameEs);
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500759 }
Ed Tanouse742b6c2019-05-03 15:06:53 -0700760
Ed Tanous81ce6092020-12-17 16:54:55 +0000761 sensorJson["Status"]["State"] = getState(inventoryItem);
762 sensorJson["Status"]["Health"] =
Ed Tanous1d7c0052022-08-09 12:32:26 -0700763 getHealth(sensorJson, propertiesDict, inventoryItem);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700764
765 // Parameter to set to override the type we get from dbus, and force it to
766 // int, regardless of what is available. This is used for schemas like fan,
767 // that require integers, not floats.
768 bool forceToInt = false;
769
Anthony Wilson3929aca2019-07-19 15:42:33 -0500770 nlohmann::json::json_pointer unit("/Reading");
Ed Tanous1d7c0052022-08-09 12:32:26 -0700771 if (chassisSubNode == sensors::node::sensors)
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500772 {
Shounak Mitra2a4ba192022-06-01 23:34:12 +0000773 sensorJson["@odata.type"] = "#Sensor.v1_2_0.Sensor";
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000774
Ed Tanous0ec8b832022-03-14 14:56:47 -0700775 sensor::ReadingType readingType = sensors::toReadingType(sensorType);
776 if (readingType == sensor::ReadingType::Invalid)
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500777 {
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000778 BMCWEB_LOG_ERROR << "Redfish cannot map reading type for "
779 << sensorType;
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500780 }
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000781 else
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500782 {
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000783 sensorJson["ReadingType"] = readingType;
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500784 }
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000785
Ed Tanous1d7c0052022-08-09 12:32:26 -0700786 std::string_view readingUnits = sensors::toReadingUnits(sensorType);
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000787 if (readingUnits.empty())
Adrian Ambrożewiczf8ede152020-06-02 13:26:33 +0200788 {
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000789 BMCWEB_LOG_ERROR << "Redfish cannot map reading unit for "
790 << sensorType;
791 }
792 else
793 {
794 sensorJson["ReadingUnits"] = readingUnits;
Adrian Ambrożewiczf8ede152020-06-02 13:26:33 +0200795 }
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500796 }
797 else if (sensorType == "temperature")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700798 {
Anthony Wilson3929aca2019-07-19 15:42:33 -0500799 unit = "/ReadingCelsius"_json_pointer;
Ed Tanous81ce6092020-12-17 16:54:55 +0000800 sensorJson["@odata.type"] = "#Thermal.v1_3_0.Temperature";
Ed Tanous1abe55e2018-09-05 08:30:59 -0700801 // TODO(ed) Documentation says that path should be type fan_tach,
802 // implementation seems to implement fan
803 }
804 else if (sensorType == "fan" || sensorType == "fan_tach")
805 {
Anthony Wilson3929aca2019-07-19 15:42:33 -0500806 unit = "/Reading"_json_pointer;
Ed Tanous81ce6092020-12-17 16:54:55 +0000807 sensorJson["ReadingUnits"] = "RPM";
808 sensorJson["@odata.type"] = "#Thermal.v1_3_0.Fan";
809 setLedState(sensorJson, inventoryItem);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700810 forceToInt = true;
811 }
Ed Tanous6f6d0d32018-10-12 11:16:43 -0700812 else if (sensorType == "fan_pwm")
813 {
Anthony Wilson3929aca2019-07-19 15:42:33 -0500814 unit = "/Reading"_json_pointer;
Ed Tanous81ce6092020-12-17 16:54:55 +0000815 sensorJson["ReadingUnits"] = "Percent";
816 sensorJson["@odata.type"] = "#Thermal.v1_3_0.Fan";
817 setLedState(sensorJson, inventoryItem);
Ed Tanous6f6d0d32018-10-12 11:16:43 -0700818 forceToInt = true;
819 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700820 else if (sensorType == "voltage")
821 {
Anthony Wilson3929aca2019-07-19 15:42:33 -0500822 unit = "/ReadingVolts"_json_pointer;
Ed Tanous81ce6092020-12-17 16:54:55 +0000823 sensorJson["@odata.type"] = "#Power.v1_0_0.Voltage";
Ed Tanous1abe55e2018-09-05 08:30:59 -0700824 }
Ed Tanous2474adf2018-09-05 16:31:16 -0700825 else if (sensorType == "power")
826 {
Ed Tanous1d7c0052022-08-09 12:32:26 -0700827 if (boost::iequals(sensorName, "total_power"))
Eddie James028f7eb2019-05-17 21:24:36 +0000828 {
Ed Tanous81ce6092020-12-17 16:54:55 +0000829 sensorJson["@odata.type"] = "#Power.v1_0_0.PowerControl";
Gunnar Mills7ab06f42019-07-02 13:07:16 -0500830 // Put multiple "sensors" into a single PowerControl, so have
831 // generic names for MemberId and Name. Follows Redfish mockup.
Ed Tanous81ce6092020-12-17 16:54:55 +0000832 sensorJson["MemberId"] = "0";
833 sensorJson["Name"] = "Chassis Power Control";
Anthony Wilson3929aca2019-07-19 15:42:33 -0500834 unit = "/PowerConsumedWatts"_json_pointer;
Eddie James028f7eb2019-05-17 21:24:36 +0000835 }
Ed Tanous1d7c0052022-08-09 12:32:26 -0700836 else if (boost::ifind_first(sensorName, "input").empty())
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700837 {
Anthony Wilson3929aca2019-07-19 15:42:33 -0500838 unit = "/PowerInputWatts"_json_pointer;
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700839 }
840 else
841 {
Anthony Wilson3929aca2019-07-19 15:42:33 -0500842 unit = "/PowerOutputWatts"_json_pointer;
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700843 }
Ed Tanous2474adf2018-09-05 16:31:16 -0700844 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700845 else
846 {
847 BMCWEB_LOG_ERROR << "Redfish cannot map object type for " << sensorName;
848 return;
849 }
850 // Map of dbus interface name, dbus property name and redfish property_name
Anthony Wilson3929aca2019-07-19 15:42:33 -0500851 std::vector<
852 std::tuple<const char*, const char*, nlohmann::json::json_pointer>>
853 properties;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700854 properties.reserve(7);
855
856 properties.emplace_back("xyz.openbmc_project.Sensor.Value", "Value", unit);
Shawn McCarneyde629b62019-03-08 10:42:51 -0600857
Ed Tanous1d7c0052022-08-09 12:32:26 -0700858 if (chassisSubNode == sensors::node::sensors)
Anthony Wilson3929aca2019-07-19 15:42:33 -0500859 {
860 properties.emplace_back(
861 "xyz.openbmc_project.Sensor.Threshold.Warning", "WarningHigh",
862 "/Thresholds/UpperCaution/Reading"_json_pointer);
863 properties.emplace_back(
864 "xyz.openbmc_project.Sensor.Threshold.Warning", "WarningLow",
865 "/Thresholds/LowerCaution/Reading"_json_pointer);
866 properties.emplace_back(
867 "xyz.openbmc_project.Sensor.Threshold.Critical", "CriticalHigh",
868 "/Thresholds/UpperCritical/Reading"_json_pointer);
869 properties.emplace_back(
870 "xyz.openbmc_project.Sensor.Threshold.Critical", "CriticalLow",
871 "/Thresholds/LowerCritical/Reading"_json_pointer);
872 }
873 else if (sensorType != "power")
Shawn McCarneyde629b62019-03-08 10:42:51 -0600874 {
875 properties.emplace_back("xyz.openbmc_project.Sensor.Threshold.Warning",
Anthony Wilson3929aca2019-07-19 15:42:33 -0500876 "WarningHigh",
877 "/UpperThresholdNonCritical"_json_pointer);
Shawn McCarneyde629b62019-03-08 10:42:51 -0600878 properties.emplace_back("xyz.openbmc_project.Sensor.Threshold.Warning",
Anthony Wilson3929aca2019-07-19 15:42:33 -0500879 "WarningLow",
880 "/LowerThresholdNonCritical"_json_pointer);
Shawn McCarneyde629b62019-03-08 10:42:51 -0600881 properties.emplace_back("xyz.openbmc_project.Sensor.Threshold.Critical",
Anthony Wilson3929aca2019-07-19 15:42:33 -0500882 "CriticalHigh",
883 "/UpperThresholdCritical"_json_pointer);
Shawn McCarneyde629b62019-03-08 10:42:51 -0600884 properties.emplace_back("xyz.openbmc_project.Sensor.Threshold.Critical",
Anthony Wilson3929aca2019-07-19 15:42:33 -0500885 "CriticalLow",
886 "/LowerThresholdCritical"_json_pointer);
Shawn McCarneyde629b62019-03-08 10:42:51 -0600887 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700888
Ed Tanous2474adf2018-09-05 16:31:16 -0700889 // TODO Need to get UpperThresholdFatal and LowerThresholdFatal
890
Ed Tanous1d7c0052022-08-09 12:32:26 -0700891 if (chassisSubNode == sensors::node::sensors)
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500892 {
893 properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MinValue",
Anthony Wilson3929aca2019-07-19 15:42:33 -0500894 "/ReadingRangeMin"_json_pointer);
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500895 properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MaxValue",
Anthony Wilson3929aca2019-07-19 15:42:33 -0500896 "/ReadingRangeMax"_json_pointer);
George Liu51c35a82022-10-13 20:22:14 +0800897 properties.emplace_back("xyz.openbmc_project.Sensor.Accuracy",
898 "Accuracy", "/Accuracy"_json_pointer);
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500899 }
900 else if (sensorType == "temperature")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700901 {
902 properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MinValue",
Anthony Wilson3929aca2019-07-19 15:42:33 -0500903 "/MinReadingRangeTemp"_json_pointer);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700904 properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MaxValue",
Anthony Wilson3929aca2019-07-19 15:42:33 -0500905 "/MaxReadingRangeTemp"_json_pointer);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700906 }
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500907 else if (sensorType != "power")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700908 {
909 properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MinValue",
Anthony Wilson3929aca2019-07-19 15:42:33 -0500910 "/MinReadingRange"_json_pointer);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700911 properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MaxValue",
Anthony Wilson3929aca2019-07-19 15:42:33 -0500912 "/MaxReadingRange"_json_pointer);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700913 }
914
Anthony Wilson3929aca2019-07-19 15:42:33 -0500915 for (const std::tuple<const char*, const char*,
916 nlohmann::json::json_pointer>& p : properties)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700917 {
Ed Tanous1d7c0052022-08-09 12:32:26 -0700918 for (const auto& [valueName, valueVariant] : propertiesDict)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700919 {
Ed Tanous1d7c0052022-08-09 12:32:26 -0700920 if (valueName != std::get<1>(p))
Ed Tanous1abe55e2018-09-05 08:30:59 -0700921 {
Ed Tanous711ac7a2021-12-20 09:34:41 -0800922 continue;
923 }
Ed Tanous1d7c0052022-08-09 12:32:26 -0700924
925 // The property we want to set may be nested json, so use
926 // a json_pointer for easy indexing into the json structure.
927 const nlohmann::json::json_pointer& key = std::get<2>(p);
928
Ed Tanous1d7c0052022-08-09 12:32:26 -0700929 const double* doubleValue = std::get_if<double>(&valueVariant);
Ed Tanous40e4f382022-08-09 18:42:51 -0700930 if (doubleValue == nullptr)
Ed Tanous711ac7a2021-12-20 09:34:41 -0800931 {
Ed Tanous40e4f382022-08-09 18:42:51 -0700932 BMCWEB_LOG_ERROR << "Got value interface that wasn't double";
Ed Tanous1d7c0052022-08-09 12:32:26 -0700933 continue;
934 }
Ed Tanous1d7c0052022-08-09 12:32:26 -0700935 if (forceToInt)
936 {
Ed Tanous40e4f382022-08-09 18:42:51 -0700937 sensorJson[key] = static_cast<int64_t>(*doubleValue);
Ed Tanous1d7c0052022-08-09 12:32:26 -0700938 }
939 else
940 {
Ed Tanous40e4f382022-08-09 18:42:51 -0700941 sensorJson[key] = *doubleValue;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700942 }
943 }
944 }
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100945}
946
Ed Tanous1d7c0052022-08-09 12:32:26 -0700947/**
948 * @brief Builds a json sensor representation of a sensor.
949 * @param sensorName The name of the sensor to be built
950 * @param sensorType The type (temperature, fan_tach, etc) of the sensor to
951 * build
952 * @param chassisSubNode The subnode (thermal, sensor, ect) of the sensor
953 * @param interfacesDict A dictionary of the interfaces and properties of said
954 * interfaces to be built from
955 * @param sensorJson The json object to fill
956 * @param inventoryItem D-Bus inventory item associated with the sensor. Will
957 * be nullptr if no associated inventory item was found.
958 */
959inline void objectInterfacesToJson(
960 const std::string& sensorName, const std::string& sensorType,
961 const std::string& chassisSubNode,
962 const dbus::utility::DBusInteracesMap& interfacesDict,
963 nlohmann::json& sensorJson, InventoryItem* inventoryItem)
964{
965
966 for (const auto& [interface, valuesDict] : interfacesDict)
967 {
968 objectPropertiesToJson(sensorName, sensorType, chassisSubNode,
969 valuesDict, sensorJson, inventoryItem);
970 }
Ed Tanousc1d019a2022-08-06 09:36:06 -0700971 BMCWEB_LOG_DEBUG << "Added sensor " << sensorName;
Ed Tanous1d7c0052022-08-09 12:32:26 -0700972}
973
Ed Tanousb5a76932020-09-29 16:16:58 -0700974inline void populateFanRedundancy(
975 const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp)
James Feist8bd25cc2019-03-15 15:14:00 -0700976{
977 crow::connections::systemBus->async_method_call(
Ed Tanousb9d36b42022-02-26 21:42:46 -0800978 [sensorsAsyncResp](
979 const boost::system::error_code ec,
980 const dbus::utility::MapperGetSubTreeResponse& resp) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700981 if (ec)
982 {
983 return; // don't have to have this interface
984 }
985 for (const std::pair<
986 std::string,
987 std::vector<std::pair<std::string, std::vector<std::string>>>>&
988 pathPair : resp)
989 {
990 const std::string& path = pathPair.first;
991 const std::vector<std::pair<std::string, std::vector<std::string>>>&
992 objDict = pathPair.second;
993 if (objDict.empty())
James Feist8bd25cc2019-03-15 15:14:00 -0700994 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700995 continue; // this should be impossible
James Feist8bd25cc2019-03-15 15:14:00 -0700996 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700997
998 const std::string& owner = objDict.begin()->first;
999 sdbusplus::asio::getProperty<std::vector<std::string>>(
1000 *crow::connections::systemBus,
1001 "xyz.openbmc_project.ObjectMapper", path + "/chassis",
1002 "xyz.openbmc_project.Association", "endpoints",
1003 [path, owner,
1004 sensorsAsyncResp](const boost::system::error_code e,
1005 const std::vector<std::string>& endpoints) {
1006 if (e)
James Feist8bd25cc2019-03-15 15:14:00 -07001007 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001008 return; // if they don't have an association we
1009 // can't tell what chassis is
James Feist8bd25cc2019-03-15 15:14:00 -07001010 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001011 auto found =
1012 std::find_if(endpoints.begin(), endpoints.end(),
1013 [sensorsAsyncResp](const std::string& entry) {
1014 return entry.find(sensorsAsyncResp->chassisId) !=
1015 std::string::npos;
1016 });
James Feist8bd25cc2019-03-15 15:14:00 -07001017
Ed Tanous002d39b2022-05-31 08:59:27 -07001018 if (found == endpoints.end())
1019 {
1020 return;
1021 }
Krzysztof Grobelny86d89ed2022-08-29 14:49:20 +02001022 sdbusplus::asio::getAllProperties(
1023 *crow::connections::systemBus, owner, path,
1024 "xyz.openbmc_project.Control.FanRedundancy",
Ed Tanous002d39b2022-05-31 08:59:27 -07001025 [path, sensorsAsyncResp](
1026 const boost::system::error_code& err,
Krzysztof Grobelny86d89ed2022-08-29 14:49:20 +02001027 const dbus::utility::DBusPropertiesMap& ret) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001028 if (err)
1029 {
1030 return; // don't have to have this
1031 // interface
1032 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001033
Krzysztof Grobelny86d89ed2022-08-29 14:49:20 +02001034 const uint8_t* allowedFailures = nullptr;
1035 const std::vector<std::string>* collection = nullptr;
1036 const std::string* status = nullptr;
1037
1038 const bool success = sdbusplus::unpackPropertiesNoThrow(
1039 dbus_utils::UnpackErrorPrinter(), ret,
1040 "AllowedFailures", allowedFailures, "Collection",
1041 collection, "Status", status);
1042
1043 if (!success)
1044 {
1045 messages::internalError(
1046 sensorsAsyncResp->asyncResp->res);
1047 return;
1048 }
1049
1050 if (allowedFailures == nullptr || collection == nullptr ||
1051 status == nullptr)
Ed Tanous002d39b2022-05-31 08:59:27 -07001052 {
1053 BMCWEB_LOG_ERROR << "Invalid redundancy interface";
1054 messages::internalError(
1055 sensorsAsyncResp->asyncResp->res);
1056 return;
1057 }
1058
Ed Tanous002d39b2022-05-31 08:59:27 -07001059 sdbusplus::message::object_path objectPath(path);
1060 std::string name = objectPath.filename();
1061 if (name.empty())
1062 {
1063 // this should be impossible
1064 messages::internalError(
1065 sensorsAsyncResp->asyncResp->res);
1066 return;
1067 }
1068 std::replace(name.begin(), name.end(), '_', ' ');
1069
1070 std::string health;
1071
Ed Tanous11ba3972022-07-11 09:50:41 -07001072 if (status->ends_with("Full"))
Ed Tanous002d39b2022-05-31 08:59:27 -07001073 {
1074 health = "OK";
1075 }
Ed Tanous11ba3972022-07-11 09:50:41 -07001076 else if (status->ends_with("Degraded"))
Ed Tanous002d39b2022-05-31 08:59:27 -07001077 {
1078 health = "Warning";
1079 }
1080 else
1081 {
1082 health = "Critical";
1083 }
1084 nlohmann::json::array_t redfishCollection;
1085 const auto& fanRedfish =
1086 sensorsAsyncResp->asyncResp->res.jsonValue["Fans"];
1087 for (const std::string& item : *collection)
1088 {
Ed Tanous8a592812022-06-04 09:06:59 -07001089 sdbusplus::message::object_path itemPath(item);
1090 std::string itemName = itemPath.filename();
Ed Tanous002d39b2022-05-31 08:59:27 -07001091 if (itemName.empty())
James Feist8bd25cc2019-03-15 15:14:00 -07001092 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001093 continue;
James Feist8bd25cc2019-03-15 15:14:00 -07001094 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001095 /*
1096 todo(ed): merge patch that fixes the names
1097 std::replace(itemName.begin(),
1098 itemName.end(), '_', ' ');*/
1099 auto schemaItem =
1100 std::find_if(fanRedfish.begin(), fanRedfish.end(),
1101 [itemName](const nlohmann::json& fan) {
1102 return fan["MemberId"] == itemName;
James Feist8bd25cc2019-03-15 15:14:00 -07001103 });
Ed Tanous002d39b2022-05-31 08:59:27 -07001104 if (schemaItem != fanRedfish.end())
James Feist8bd25cc2019-03-15 15:14:00 -07001105 {
Ed Tanous8a592812022-06-04 09:06:59 -07001106 nlohmann::json::object_t collectionId;
1107 collectionId["@odata.id"] =
Ed Tanous002d39b2022-05-31 08:59:27 -07001108 (*schemaItem)["@odata.id"];
1109 redfishCollection.emplace_back(
Ed Tanous8a592812022-06-04 09:06:59 -07001110 std::move(collectionId));
Ed Tanous002d39b2022-05-31 08:59:27 -07001111 }
1112 else
1113 {
1114 BMCWEB_LOG_ERROR << "failed to find fan in schema";
1115 messages::internalError(
1116 sensorsAsyncResp->asyncResp->res);
James Feist8bd25cc2019-03-15 15:14:00 -07001117 return;
1118 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001119 }
James Feist8bd25cc2019-03-15 15:14:00 -07001120
Ed Tanous002d39b2022-05-31 08:59:27 -07001121 size_t minNumNeeded =
1122 collection->empty()
1123 ? 0
1124 : collection->size() - *allowedFailures;
1125 nlohmann::json& jResp = sensorsAsyncResp->asyncResp->res
1126 .jsonValue["Redundancy"];
James Feist8bd25cc2019-03-15 15:14:00 -07001127
Ed Tanous002d39b2022-05-31 08:59:27 -07001128 nlohmann::json::object_t redundancy;
1129 redundancy["@odata.id"] =
1130 "/redfish/v1/Chassis/" + sensorsAsyncResp->chassisId +
1131 "/" + sensorsAsyncResp->chassisSubNode +
1132 "#/Redundancy/" + std::to_string(jResp.size());
1133 redundancy["@odata.type"] = "#Redundancy.v1_3_2.Redundancy";
1134 redundancy["MinNumNeeded"] = minNumNeeded;
1135 redundancy["MemberId"] = name;
1136 redundancy["Mode"] = "N+m";
1137 redundancy["Name"] = name;
1138 redundancy["RedundancySet"] = redfishCollection;
1139 redundancy["Status"]["Health"] = health;
1140 redundancy["Status"]["State"] = "Enabled";
James Feist8bd25cc2019-03-15 15:14:00 -07001141
Ed Tanous002d39b2022-05-31 08:59:27 -07001142 jResp.push_back(std::move(redundancy));
Krzysztof Grobelny86d89ed2022-08-29 14:49:20 +02001143 });
Ed Tanous002d39b2022-05-31 08:59:27 -07001144 });
1145 }
James Feist8bd25cc2019-03-15 15:14:00 -07001146 },
1147 "xyz.openbmc_project.ObjectMapper",
1148 "/xyz/openbmc_project/object_mapper",
1149 "xyz.openbmc_project.ObjectMapper", "GetSubTree",
1150 "/xyz/openbmc_project/control", 2,
1151 std::array<const char*, 1>{
1152 "xyz.openbmc_project.Control.FanRedundancy"});
1153}
1154
Ed Tanousb5a76932020-09-29 16:16:58 -07001155inline void
Ed Tanous81ce6092020-12-17 16:54:55 +00001156 sortJSONResponse(const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07001157{
zhanghch058d1b46d2021-04-01 11:18:24 +08001158 nlohmann::json& response = sensorsAsyncResp->asyncResp->res.jsonValue;
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07001159 std::array<std::string, 2> sensorHeaders{"Temperatures", "Fans"};
Ed Tanous81ce6092020-12-17 16:54:55 +00001160 if (sensorsAsyncResp->chassisSubNode == sensors::node::power)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07001161 {
1162 sensorHeaders = {"Voltages", "PowerSupplies"};
1163 }
1164 for (const std::string& sensorGroup : sensorHeaders)
1165 {
1166 nlohmann::json::iterator entry = response.find(sensorGroup);
1167 if (entry != response.end())
1168 {
1169 std::sort(entry->begin(), entry->end(),
Ed Tanous02cad962022-06-30 16:50:15 -07001170 [](const nlohmann::json& c1, const nlohmann::json& c2) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001171 return c1["Name"] < c2["Name"];
1172 });
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07001173
1174 // add the index counts to the end of each entry
1175 size_t count = 0;
1176 for (nlohmann::json& sensorJson : *entry)
1177 {
1178 nlohmann::json::iterator odata = sensorJson.find("@odata.id");
1179 if (odata == sensorJson.end())
1180 {
1181 continue;
1182 }
1183 std::string* value = odata->get_ptr<std::string*>();
1184 if (value != nullptr)
1185 {
1186 *value += std::to_string(count);
1187 count++;
Ed Tanous81ce6092020-12-17 16:54:55 +00001188 sensorsAsyncResp->updateUri(sensorJson["Name"], *value);
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07001189 }
1190 }
1191 }
1192 }
1193}
1194
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +01001195/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001196 * @brief Finds the inventory item with the specified object path.
1197 * @param inventoryItems D-Bus inventory items associated with sensors.
1198 * @param invItemObjPath D-Bus object path of inventory item.
1199 * @return Inventory item within vector, or nullptr if no match found.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001200 */
Ed Tanous23a21a12020-07-25 04:45:05 +00001201inline InventoryItem* findInventoryItem(
Ed Tanousb5a76932020-09-29 16:16:58 -07001202 const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems,
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001203 const std::string& invItemObjPath)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001204{
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001205 for (InventoryItem& inventoryItem : *inventoryItems)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001206 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001207 if (inventoryItem.objectPath == invItemObjPath)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001208 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001209 return &inventoryItem;
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001210 }
1211 }
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001212 return nullptr;
1213}
1214
1215/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001216 * @brief Finds the inventory item associated with the specified sensor.
1217 * @param inventoryItems D-Bus inventory items associated with sensors.
1218 * @param sensorObjPath D-Bus object path of sensor.
1219 * @return Inventory item within vector, or nullptr if no match found.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001220 */
Ed Tanous23a21a12020-07-25 04:45:05 +00001221inline InventoryItem* findInventoryItemForSensor(
Ed Tanousb5a76932020-09-29 16:16:58 -07001222 const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems,
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001223 const std::string& sensorObjPath)
1224{
1225 for (InventoryItem& inventoryItem : *inventoryItems)
1226 {
1227 if (inventoryItem.sensors.count(sensorObjPath) > 0)
1228 {
1229 return &inventoryItem;
1230 }
1231 }
1232 return nullptr;
1233}
1234
1235/**
Anthony Wilsond5005492019-07-31 16:34:17 -05001236 * @brief Finds the inventory item associated with the specified led path.
1237 * @param inventoryItems D-Bus inventory items associated with sensors.
1238 * @param ledObjPath D-Bus object path of led.
1239 * @return Inventory item within vector, or nullptr if no match found.
1240 */
1241inline InventoryItem*
1242 findInventoryItemForLed(std::vector<InventoryItem>& inventoryItems,
1243 const std::string& ledObjPath)
1244{
1245 for (InventoryItem& inventoryItem : inventoryItems)
1246 {
1247 if (inventoryItem.ledObjectPath == ledObjPath)
1248 {
1249 return &inventoryItem;
1250 }
1251 }
1252 return nullptr;
1253}
1254
1255/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001256 * @brief Adds inventory item and associated sensor to specified vector.
1257 *
1258 * Adds a new InventoryItem to the vector if necessary. Searches for an
1259 * existing InventoryItem with the specified object path. If not found, one is
1260 * added to the vector.
1261 *
1262 * Next, the specified sensor is added to the set of sensors associated with the
1263 * InventoryItem.
1264 *
1265 * @param inventoryItems D-Bus inventory items associated with sensors.
1266 * @param invItemObjPath D-Bus object path of inventory item.
1267 * @param sensorObjPath D-Bus object path of sensor
1268 */
Ed Tanousb5a76932020-09-29 16:16:58 -07001269inline void addInventoryItem(
1270 const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems,
1271 const std::string& invItemObjPath, const std::string& sensorObjPath)
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001272{
1273 // Look for inventory item in vector
1274 InventoryItem* inventoryItem =
1275 findInventoryItem(inventoryItems, invItemObjPath);
1276
1277 // If inventory item doesn't exist in vector, add it
1278 if (inventoryItem == nullptr)
1279 {
1280 inventoryItems->emplace_back(invItemObjPath);
1281 inventoryItem = &(inventoryItems->back());
1282 }
1283
1284 // Add sensor to set of sensors associated with inventory item
1285 inventoryItem->sensors.emplace(sensorObjPath);
1286}
1287
1288/**
1289 * @brief Stores D-Bus data in the specified inventory item.
1290 *
1291 * Finds D-Bus data in the specified map of interfaces. Stores the data in the
1292 * specified InventoryItem.
1293 *
1294 * This data is later used to provide sensor property values in the JSON
1295 * response.
1296 *
1297 * @param inventoryItem Inventory item where data will be stored.
1298 * @param interfacesDict Map containing D-Bus interfaces and their properties
1299 * for the specified inventory item.
1300 */
Ed Tanous23a21a12020-07-25 04:45:05 +00001301inline void storeInventoryItemData(
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001302 InventoryItem& inventoryItem,
Ed Tanous711ac7a2021-12-20 09:34:41 -08001303 const dbus::utility::DBusInteracesMap& interfacesDict)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001304{
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001305 // Get properties from Inventory.Item interface
Ed Tanous711ac7a2021-12-20 09:34:41 -08001306
Ed Tanous9eb808c2022-01-25 10:19:23 -08001307 for (const auto& [interface, values] : interfacesDict)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001308 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001309 if (interface == "xyz.openbmc_project.Inventory.Item")
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001310 {
Ed Tanous9eb808c2022-01-25 10:19:23 -08001311 for (const auto& [name, dbusValue] : values)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001312 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001313 if (name == "Present")
1314 {
1315 const bool* value = std::get_if<bool>(&dbusValue);
1316 if (value != nullptr)
1317 {
1318 inventoryItem.isPresent = *value;
1319 }
1320 }
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001321 }
1322 }
Ed Tanous711ac7a2021-12-20 09:34:41 -08001323 // Check if Inventory.Item.PowerSupply interface is present
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001324
Ed Tanous711ac7a2021-12-20 09:34:41 -08001325 if (interface == "xyz.openbmc_project.Inventory.Item.PowerSupply")
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001326 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001327 inventoryItem.isPowerSupply = true;
1328 }
1329
1330 // Get properties from Inventory.Decorator.Asset interface
1331 if (interface == "xyz.openbmc_project.Inventory.Decorator.Asset")
1332 {
Ed Tanous9eb808c2022-01-25 10:19:23 -08001333 for (const auto& [name, dbusValue] : values)
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001334 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001335 if (name == "Manufacturer")
1336 {
1337 const std::string* value =
1338 std::get_if<std::string>(&dbusValue);
1339 if (value != nullptr)
1340 {
1341 inventoryItem.manufacturer = *value;
1342 }
1343 }
1344 if (name == "Model")
1345 {
1346 const std::string* value =
1347 std::get_if<std::string>(&dbusValue);
1348 if (value != nullptr)
1349 {
1350 inventoryItem.model = *value;
1351 }
1352 }
1353 if (name == "SerialNumber")
1354 {
1355 const std::string* value =
1356 std::get_if<std::string>(&dbusValue);
1357 if (value != nullptr)
1358 {
1359 inventoryItem.serialNumber = *value;
1360 }
1361 }
1362 if (name == "PartNumber")
1363 {
1364 const std::string* value =
1365 std::get_if<std::string>(&dbusValue);
1366 if (value != nullptr)
1367 {
1368 inventoryItem.partNumber = *value;
1369 }
1370 }
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001371 }
1372 }
1373
Ed Tanous711ac7a2021-12-20 09:34:41 -08001374 if (interface ==
1375 "xyz.openbmc_project.State.Decorator.OperationalStatus")
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001376 {
Ed Tanous9eb808c2022-01-25 10:19:23 -08001377 for (const auto& [name, dbusValue] : values)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001378 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001379 if (name == "Functional")
1380 {
1381 const bool* value = std::get_if<bool>(&dbusValue);
1382 if (value != nullptr)
1383 {
1384 inventoryItem.isFunctional = *value;
1385 }
1386 }
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001387 }
1388 }
1389 }
1390}
1391
1392/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001393 * @brief Gets D-Bus data for inventory items associated with sensors.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001394 *
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001395 * Uses the specified connections (services) to obtain D-Bus data for inventory
1396 * items associated with sensors. Stores the resulting data in the
1397 * inventoryItems vector.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001398 *
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001399 * This data is later used to provide sensor property values in the JSON
1400 * response.
1401 *
1402 * Finds the inventory item data asynchronously. Invokes callback when data has
1403 * been obtained.
1404 *
1405 * The callback must have the following signature:
1406 * @code
Anthony Wilsond5005492019-07-31 16:34:17 -05001407 * callback(void)
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001408 * @endcode
1409 *
1410 * This function is called recursively, obtaining data asynchronously from one
1411 * connection in each call. This ensures the callback is not invoked until the
1412 * last asynchronous function has completed.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001413 *
1414 * @param sensorsAsyncResp Pointer to object holding response data.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001415 * @param inventoryItems D-Bus inventory items associated with sensors.
1416 * @param invConnections Connections that provide data for the inventory items.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001417 * implements ObjectManager.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001418 * @param callback Callback to invoke when inventory data has been obtained.
1419 * @param invConnectionsIndex Current index in invConnections. Only specified
1420 * in recursive calls to this function.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001421 */
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001422template <typename Callback>
1423static void getInventoryItemsData(
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001424 std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001425 std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
Ed Tanousd0090732022-10-04 17:22:56 -07001426 std::shared_ptr<std::set<std::string>> invConnections, Callback&& callback,
1427 size_t invConnectionsIndex = 0)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001428{
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001429 BMCWEB_LOG_DEBUG << "getInventoryItemsData enter";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001430
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001431 // If no more connections left, call callback
1432 if (invConnectionsIndex >= invConnections->size())
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001433 {
Anthony Wilsond5005492019-07-31 16:34:17 -05001434 callback();
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001435 BMCWEB_LOG_DEBUG << "getInventoryItemsData exit";
1436 return;
1437 }
1438
1439 // Get inventory item data from current connection
Nan Zhoufe04d492022-06-22 17:10:41 +00001440 auto it = invConnections->begin();
1441 std::advance(it, invConnectionsIndex);
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001442 if (it != invConnections->end())
1443 {
1444 const std::string& invConnection = *it;
1445
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001446 // Response handler for GetManagedObjects
Ed Tanousd0090732022-10-04 17:22:56 -07001447 auto respHandler = [sensorsAsyncResp, inventoryItems, invConnections,
1448 callback{std::forward<Callback>(callback)},
1449 invConnectionsIndex](
1450 const boost::system::error_code ec,
1451 const dbus::utility::ManagedObjectType& resp) {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001452 BMCWEB_LOG_DEBUG << "getInventoryItemsData respHandler enter";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001453 if (ec)
1454 {
1455 BMCWEB_LOG_ERROR
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001456 << "getInventoryItemsData respHandler DBus error " << ec;
zhanghch058d1b46d2021-04-01 11:18:24 +08001457 messages::internalError(sensorsAsyncResp->asyncResp->res);
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001458 return;
1459 }
1460
1461 // Loop through returned object paths
1462 for (const auto& objDictEntry : resp)
1463 {
1464 const std::string& objPath =
1465 static_cast<const std::string&>(objDictEntry.first);
1466
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001467 // If this object path is one of the specified inventory items
1468 InventoryItem* inventoryItem =
1469 findInventoryItem(inventoryItems, objPath);
1470 if (inventoryItem != nullptr)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001471 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001472 // Store inventory data in InventoryItem
1473 storeInventoryItemData(*inventoryItem, objDictEntry.second);
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001474 }
1475 }
1476
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001477 // Recurse to get inventory item data from next connection
1478 getInventoryItemsData(sensorsAsyncResp, inventoryItems,
Ed Tanousd0090732022-10-04 17:22:56 -07001479 invConnections, std::move(callback),
1480 invConnectionsIndex + 1);
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001481
1482 BMCWEB_LOG_DEBUG << "getInventoryItemsData respHandler exit";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001483 };
1484
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001485 // Get all object paths and their interfaces for current connection
1486 crow::connections::systemBus->async_method_call(
Ed Tanousd0090732022-10-04 17:22:56 -07001487 std::move(respHandler), invConnection,
Ed Tanousf8bb0ff2022-12-09 11:08:45 -08001488 "/xyz/openbmc_project/inventory",
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001489 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
1490 }
1491
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001492 BMCWEB_LOG_DEBUG << "getInventoryItemsData exit";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001493}
1494
1495/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001496 * @brief Gets connections that provide D-Bus data for inventory items.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001497 *
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001498 * Gets the D-Bus connections (services) that provide data for the inventory
1499 * items that are associated with sensors.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001500 *
1501 * Finds the connections asynchronously. Invokes callback when information has
1502 * been obtained.
1503 *
1504 * The callback must have the following signature:
1505 * @code
Nan Zhoufe04d492022-06-22 17:10:41 +00001506 * callback(std::shared_ptr<std::set<std::string>> invConnections)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001507 * @endcode
1508 *
1509 * @param sensorsAsyncResp Pointer to object holding response data.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001510 * @param inventoryItems D-Bus inventory items associated with sensors.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001511 * @param callback Callback to invoke when connections have been obtained.
1512 */
1513template <typename Callback>
1514static void getInventoryItemsConnections(
Ed Tanousb5a76932020-09-29 16:16:58 -07001515 const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
1516 const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems,
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001517 Callback&& callback)
1518{
1519 BMCWEB_LOG_DEBUG << "getInventoryItemsConnections enter";
1520
1521 const std::string path = "/xyz/openbmc_project/inventory";
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001522 const std::array<std::string, 4> interfaces = {
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001523 "xyz.openbmc_project.Inventory.Item",
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001524 "xyz.openbmc_project.Inventory.Item.PowerSupply",
1525 "xyz.openbmc_project.Inventory.Decorator.Asset",
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001526 "xyz.openbmc_project.State.Decorator.OperationalStatus"};
1527
1528 // Response handler for parsing output from GetSubTree
Ed Tanous002d39b2022-05-31 08:59:27 -07001529 auto respHandler =
1530 [callback{std::forward<Callback>(callback)}, sensorsAsyncResp,
1531 inventoryItems](
1532 const boost::system::error_code ec,
1533 const dbus::utility::MapperGetSubTreeResponse& subtree) {
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001534 BMCWEB_LOG_DEBUG << "getInventoryItemsConnections respHandler enter";
1535 if (ec)
1536 {
zhanghch058d1b46d2021-04-01 11:18:24 +08001537 messages::internalError(sensorsAsyncResp->asyncResp->res);
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001538 BMCWEB_LOG_ERROR
1539 << "getInventoryItemsConnections respHandler DBus error " << ec;
1540 return;
1541 }
1542
1543 // Make unique list of connections for desired inventory items
Nan Zhoufe04d492022-06-22 17:10:41 +00001544 std::shared_ptr<std::set<std::string>> invConnections =
1545 std::make_shared<std::set<std::string>>();
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001546
1547 // Loop through objects from GetSubTree
1548 for (const std::pair<
1549 std::string,
1550 std::vector<std::pair<std::string, std::vector<std::string>>>>&
1551 object : subtree)
1552 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001553 // Check if object path is one of the specified inventory items
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001554 const std::string& objPath = object.first;
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001555 if (findInventoryItem(inventoryItems, objPath) != nullptr)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001556 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001557 // Store all connections to inventory item
1558 for (const std::pair<std::string, std::vector<std::string>>&
1559 objData : object.second)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001560 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001561 const std::string& invConnection = objData.first;
1562 invConnections->insert(invConnection);
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001563 }
1564 }
1565 }
Anthony Wilsond5005492019-07-31 16:34:17 -05001566
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001567 callback(invConnections);
1568 BMCWEB_LOG_DEBUG << "getInventoryItemsConnections respHandler exit";
1569 };
1570
1571 // Make call to ObjectMapper to find all inventory items
1572 crow::connections::systemBus->async_method_call(
1573 std::move(respHandler), "xyz.openbmc_project.ObjectMapper",
1574 "/xyz/openbmc_project/object_mapper",
1575 "xyz.openbmc_project.ObjectMapper", "GetSubTree", path, 0, interfaces);
1576 BMCWEB_LOG_DEBUG << "getInventoryItemsConnections exit";
1577}
1578
1579/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001580 * @brief Gets associations from sensors to inventory items.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001581 *
1582 * Looks for ObjectMapper associations from the specified sensors to related
Anthony Wilsond5005492019-07-31 16:34:17 -05001583 * inventory items. Then finds the associations from those inventory items to
1584 * their LEDs, if any.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001585 *
1586 * Finds the inventory items asynchronously. Invokes callback when information
1587 * has been obtained.
1588 *
1589 * The callback must have the following signature:
1590 * @code
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001591 * callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001592 * @endcode
1593 *
1594 * @param sensorsAsyncResp Pointer to object holding response data.
1595 * @param sensorNames All sensors within the current chassis.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001596 * implements ObjectManager.
1597 * @param callback Callback to invoke when inventory items have been obtained.
1598 */
1599template <typename Callback>
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001600static void getInventoryItemAssociations(
Ed Tanousb5a76932020-09-29 16:16:58 -07001601 const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
Nan Zhoufe04d492022-06-22 17:10:41 +00001602 const std::shared_ptr<std::set<std::string>>& sensorNames,
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001603 Callback&& callback)
1604{
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001605 BMCWEB_LOG_DEBUG << "getInventoryItemAssociations enter";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001606
1607 // Response handler for GetManagedObjects
Ed Tanous02cad962022-06-30 16:50:15 -07001608 auto respHandler =
1609 [callback{std::forward<Callback>(callback)}, sensorsAsyncResp,
1610 sensorNames](const boost::system::error_code ec,
1611 const dbus::utility::ManagedObjectType& resp) {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001612 BMCWEB_LOG_DEBUG << "getInventoryItemAssociations respHandler enter";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001613 if (ec)
1614 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001615 BMCWEB_LOG_ERROR
1616 << "getInventoryItemAssociations respHandler DBus error " << ec;
zhanghch058d1b46d2021-04-01 11:18:24 +08001617 messages::internalError(sensorsAsyncResp->asyncResp->res);
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001618 return;
1619 }
1620
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001621 // Create vector to hold list of inventory items
1622 std::shared_ptr<std::vector<InventoryItem>> inventoryItems =
1623 std::make_shared<std::vector<InventoryItem>>();
1624
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001625 // Loop through returned object paths
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001626 std::string sensorAssocPath;
1627 sensorAssocPath.reserve(128); // avoid memory allocations
1628 for (const auto& objDictEntry : resp)
1629 {
1630 const std::string& objPath =
1631 static_cast<const std::string&>(objDictEntry.first);
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001632
1633 // If path is inventory association for one of the specified sensors
1634 for (const std::string& sensorName : *sensorNames)
1635 {
1636 sensorAssocPath = sensorName;
1637 sensorAssocPath += "/inventory";
1638 if (objPath == sensorAssocPath)
1639 {
1640 // Get Association interface for object path
Ed Tanous711ac7a2021-12-20 09:34:41 -08001641 for (const auto& [interface, values] : objDictEntry.second)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001642 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001643 if (interface == "xyz.openbmc_project.Association")
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001644 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001645 for (const auto& [valueName, value] : values)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001646 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001647 if (valueName == "endpoints")
1648 {
1649 const std::vector<std::string>* endpoints =
1650 std::get_if<std::vector<std::string>>(
1651 &value);
1652 if ((endpoints != nullptr) &&
1653 !endpoints->empty())
1654 {
1655 // Add inventory item to vector
1656 const std::string& invItemPath =
1657 endpoints->front();
1658 addInventoryItem(inventoryItems,
1659 invItemPath,
1660 sensorName);
1661 }
1662 }
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001663 }
1664 }
1665 }
1666 break;
1667 }
1668 }
1669 }
1670
Anthony Wilsond5005492019-07-31 16:34:17 -05001671 // Now loop through the returned object paths again, this time to
1672 // find the leds associated with the inventory items we just found
1673 std::string inventoryAssocPath;
1674 inventoryAssocPath.reserve(128); // avoid memory allocations
1675 for (const auto& objDictEntry : resp)
1676 {
1677 const std::string& objPath =
1678 static_cast<const std::string&>(objDictEntry.first);
Anthony Wilsond5005492019-07-31 16:34:17 -05001679
1680 for (InventoryItem& inventoryItem : *inventoryItems)
1681 {
1682 inventoryAssocPath = inventoryItem.objectPath;
1683 inventoryAssocPath += "/leds";
1684 if (objPath == inventoryAssocPath)
1685 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001686 for (const auto& [interface, values] : objDictEntry.second)
Anthony Wilsond5005492019-07-31 16:34:17 -05001687 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001688 if (interface == "xyz.openbmc_project.Association")
Anthony Wilsond5005492019-07-31 16:34:17 -05001689 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001690 for (const auto& [valueName, value] : values)
Anthony Wilsond5005492019-07-31 16:34:17 -05001691 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001692 if (valueName == "endpoints")
1693 {
1694 const std::vector<std::string>* endpoints =
1695 std::get_if<std::vector<std::string>>(
1696 &value);
1697 if ((endpoints != nullptr) &&
1698 !endpoints->empty())
1699 {
1700 // Add inventory item to vector
1701 // Store LED path in inventory item
1702 const std::string& ledPath =
1703 endpoints->front();
1704 inventoryItem.ledObjectPath = ledPath;
1705 }
1706 }
Anthony Wilsond5005492019-07-31 16:34:17 -05001707 }
1708 }
1709 }
Ed Tanous711ac7a2021-12-20 09:34:41 -08001710
Anthony Wilsond5005492019-07-31 16:34:17 -05001711 break;
1712 }
1713 }
1714 }
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001715 callback(inventoryItems);
1716 BMCWEB_LOG_DEBUG << "getInventoryItemAssociations respHandler exit";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001717 };
1718
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001719 // Call GetManagedObjects on the ObjectMapper to get all associations
1720 crow::connections::systemBus->async_method_call(
Ed Tanousd0090732022-10-04 17:22:56 -07001721 std::move(respHandler), "xyz.openbmc_project.ObjectMapper", "/",
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001722 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
1723
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001724 BMCWEB_LOG_DEBUG << "getInventoryItemAssociations exit";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001725}
1726
1727/**
Anthony Wilsond5005492019-07-31 16:34:17 -05001728 * @brief Gets D-Bus data for inventory item leds associated with sensors.
1729 *
1730 * Uses the specified connections (services) to obtain D-Bus data for inventory
1731 * item leds associated with sensors. Stores the resulting data in the
1732 * inventoryItems vector.
1733 *
1734 * This data is later used to provide sensor property values in the JSON
1735 * response.
1736 *
1737 * Finds the inventory item led data asynchronously. Invokes callback when data
1738 * has been obtained.
1739 *
1740 * The callback must have the following signature:
1741 * @code
Gunnar Mills42cbe532019-08-15 15:26:54 -05001742 * callback()
Anthony Wilsond5005492019-07-31 16:34:17 -05001743 * @endcode
1744 *
1745 * This function is called recursively, obtaining data asynchronously from one
1746 * connection in each call. This ensures the callback is not invoked until the
1747 * last asynchronous function has completed.
1748 *
1749 * @param sensorsAsyncResp Pointer to object holding response data.
1750 * @param inventoryItems D-Bus inventory items associated with sensors.
1751 * @param ledConnections Connections that provide data for the inventory leds.
1752 * @param callback Callback to invoke when inventory data has been obtained.
1753 * @param ledConnectionsIndex Current index in ledConnections. Only specified
1754 * in recursive calls to this function.
1755 */
1756template <typename Callback>
1757void getInventoryLedData(
1758 std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
1759 std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
Nan Zhoufe04d492022-06-22 17:10:41 +00001760 std::shared_ptr<std::map<std::string, std::string>> ledConnections,
Anthony Wilsond5005492019-07-31 16:34:17 -05001761 Callback&& callback, size_t ledConnectionsIndex = 0)
1762{
1763 BMCWEB_LOG_DEBUG << "getInventoryLedData enter";
1764
1765 // If no more connections left, call callback
1766 if (ledConnectionsIndex >= ledConnections->size())
1767 {
Gunnar Mills42cbe532019-08-15 15:26:54 -05001768 callback();
Anthony Wilsond5005492019-07-31 16:34:17 -05001769 BMCWEB_LOG_DEBUG << "getInventoryLedData exit";
1770 return;
1771 }
1772
1773 // Get inventory item data from current connection
Nan Zhoufe04d492022-06-22 17:10:41 +00001774 auto it = ledConnections->begin();
1775 std::advance(it, ledConnectionsIndex);
Anthony Wilsond5005492019-07-31 16:34:17 -05001776 if (it != ledConnections->end())
1777 {
1778 const std::string& ledPath = (*it).first;
1779 const std::string& ledConnection = (*it).second;
1780 // Response handler for Get State property
Jonathan Doman1e1e5982021-06-11 09:36:17 -07001781 auto respHandler =
1782 [sensorsAsyncResp, inventoryItems, ledConnections, ledPath,
Ed Tanousf94c4ec2022-01-06 12:44:41 -08001783 callback{std::forward<Callback>(callback)}, ledConnectionsIndex](
Jonathan Doman1e1e5982021-06-11 09:36:17 -07001784 const boost::system::error_code ec, const std::string& state) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001785 BMCWEB_LOG_DEBUG << "getInventoryLedData respHandler enter";
1786 if (ec)
1787 {
1788 BMCWEB_LOG_ERROR
1789 << "getInventoryLedData respHandler DBus error " << ec;
1790 messages::internalError(sensorsAsyncResp->asyncResp->res);
1791 return;
1792 }
1793
1794 BMCWEB_LOG_DEBUG << "Led state: " << state;
1795 // Find inventory item with this LED object path
1796 InventoryItem* inventoryItem =
1797 findInventoryItemForLed(*inventoryItems, ledPath);
1798 if (inventoryItem != nullptr)
1799 {
1800 // Store LED state in InventoryItem
Ed Tanous11ba3972022-07-11 09:50:41 -07001801 if (state.ends_with("On"))
Jonathan Doman1e1e5982021-06-11 09:36:17 -07001802 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001803 inventoryItem->ledState = LedState::ON;
Jonathan Doman1e1e5982021-06-11 09:36:17 -07001804 }
Ed Tanous11ba3972022-07-11 09:50:41 -07001805 else if (state.ends_with("Blink"))
Anthony Wilsond5005492019-07-31 16:34:17 -05001806 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001807 inventoryItem->ledState = LedState::BLINK;
Anthony Wilsond5005492019-07-31 16:34:17 -05001808 }
Ed Tanous11ba3972022-07-11 09:50:41 -07001809 else if (state.ends_with("Off"))
Ed Tanous002d39b2022-05-31 08:59:27 -07001810 {
1811 inventoryItem->ledState = LedState::OFF;
1812 }
1813 else
1814 {
1815 inventoryItem->ledState = LedState::UNKNOWN;
1816 }
1817 }
Anthony Wilsond5005492019-07-31 16:34:17 -05001818
Ed Tanous002d39b2022-05-31 08:59:27 -07001819 // Recurse to get LED data from next connection
1820 getInventoryLedData(sensorsAsyncResp, inventoryItems,
1821 ledConnections, std::move(callback),
1822 ledConnectionsIndex + 1);
Anthony Wilsond5005492019-07-31 16:34:17 -05001823
Ed Tanous002d39b2022-05-31 08:59:27 -07001824 BMCWEB_LOG_DEBUG << "getInventoryLedData respHandler exit";
1825 };
Anthony Wilsond5005492019-07-31 16:34:17 -05001826
1827 // Get the State property for the current LED
Jonathan Doman1e1e5982021-06-11 09:36:17 -07001828 sdbusplus::asio::getProperty<std::string>(
1829 *crow::connections::systemBus, ledConnection, ledPath,
1830 "xyz.openbmc_project.Led.Physical", "State",
1831 std::move(respHandler));
Anthony Wilsond5005492019-07-31 16:34:17 -05001832 }
1833
1834 BMCWEB_LOG_DEBUG << "getInventoryLedData exit";
1835}
1836
1837/**
1838 * @brief Gets LED data for LEDs associated with given inventory items.
1839 *
1840 * Gets the D-Bus connections (services) that provide LED data for the LEDs
1841 * associated with the specified inventory items. Then gets the LED data from
1842 * each connection and stores it in the inventory item.
1843 *
1844 * This data is later used to provide sensor property values in the JSON
1845 * response.
1846 *
1847 * Finds the LED data asynchronously. Invokes callback when information has
1848 * been obtained.
1849 *
1850 * The callback must have the following signature:
1851 * @code
Gunnar Mills42cbe532019-08-15 15:26:54 -05001852 * callback()
Anthony Wilsond5005492019-07-31 16:34:17 -05001853 * @endcode
1854 *
1855 * @param sensorsAsyncResp Pointer to object holding response data.
1856 * @param inventoryItems D-Bus inventory items associated with sensors.
1857 * @param callback Callback to invoke when inventory items have been obtained.
1858 */
1859template <typename Callback>
1860void getInventoryLeds(
1861 std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
1862 std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
1863 Callback&& callback)
1864{
1865 BMCWEB_LOG_DEBUG << "getInventoryLeds enter";
1866
1867 const std::string path = "/xyz/openbmc_project";
1868 const std::array<std::string, 1> interfaces = {
1869 "xyz.openbmc_project.Led.Physical"};
1870
1871 // Response handler for parsing output from GetSubTree
Ed Tanous002d39b2022-05-31 08:59:27 -07001872 auto respHandler =
1873 [callback{std::forward<Callback>(callback)}, sensorsAsyncResp,
1874 inventoryItems](
1875 const boost::system::error_code ec,
1876 const dbus::utility::MapperGetSubTreeResponse& subtree) {
Anthony Wilsond5005492019-07-31 16:34:17 -05001877 BMCWEB_LOG_DEBUG << "getInventoryLeds respHandler enter";
1878 if (ec)
1879 {
zhanghch058d1b46d2021-04-01 11:18:24 +08001880 messages::internalError(sensorsAsyncResp->asyncResp->res);
Anthony Wilsond5005492019-07-31 16:34:17 -05001881 BMCWEB_LOG_ERROR << "getInventoryLeds respHandler DBus error "
1882 << ec;
1883 return;
1884 }
1885
1886 // Build map of LED object paths to connections
Nan Zhoufe04d492022-06-22 17:10:41 +00001887 std::shared_ptr<std::map<std::string, std::string>> ledConnections =
1888 std::make_shared<std::map<std::string, std::string>>();
Anthony Wilsond5005492019-07-31 16:34:17 -05001889
1890 // Loop through objects from GetSubTree
1891 for (const std::pair<
1892 std::string,
1893 std::vector<std::pair<std::string, std::vector<std::string>>>>&
1894 object : subtree)
1895 {
1896 // Check if object path is LED for one of the specified inventory
1897 // items
1898 const std::string& ledPath = object.first;
1899 if (findInventoryItemForLed(*inventoryItems, ledPath) != nullptr)
1900 {
1901 // Add mapping from ledPath to connection
1902 const std::string& connection = object.second.begin()->first;
1903 (*ledConnections)[ledPath] = connection;
1904 BMCWEB_LOG_DEBUG << "Added mapping " << ledPath << " -> "
1905 << connection;
1906 }
1907 }
1908
1909 getInventoryLedData(sensorsAsyncResp, inventoryItems, ledConnections,
1910 std::move(callback));
1911 BMCWEB_LOG_DEBUG << "getInventoryLeds respHandler exit";
1912 };
1913 // Make call to ObjectMapper to find all inventory items
1914 crow::connections::systemBus->async_method_call(
1915 std::move(respHandler), "xyz.openbmc_project.ObjectMapper",
1916 "/xyz/openbmc_project/object_mapper",
1917 "xyz.openbmc_project.ObjectMapper", "GetSubTree", path, 0, interfaces);
1918 BMCWEB_LOG_DEBUG << "getInventoryLeds exit";
1919}
1920
1921/**
Gunnar Mills42cbe532019-08-15 15:26:54 -05001922 * @brief Gets D-Bus data for Power Supply Attributes such as EfficiencyPercent
1923 *
1924 * Uses the specified connections (services) (currently assumes just one) to
1925 * obtain D-Bus data for Power Supply Attributes. Stores the resulting data in
1926 * the inventoryItems vector. Only stores data in Power Supply inventoryItems.
1927 *
1928 * This data is later used to provide sensor property values in the JSON
1929 * response.
1930 *
1931 * Finds the Power Supply Attributes data asynchronously. Invokes callback
1932 * when data has been obtained.
1933 *
1934 * The callback must have the following signature:
1935 * @code
1936 * callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems)
1937 * @endcode
1938 *
1939 * @param sensorsAsyncResp Pointer to object holding response data.
1940 * @param inventoryItems D-Bus inventory items associated with sensors.
1941 * @param psAttributesConnections Connections that provide data for the Power
1942 * Supply Attributes
1943 * @param callback Callback to invoke when data has been obtained.
1944 */
1945template <typename Callback>
1946void getPowerSupplyAttributesData(
Ed Tanousb5a76932020-09-29 16:16:58 -07001947 const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
Gunnar Mills42cbe532019-08-15 15:26:54 -05001948 std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
Nan Zhoufe04d492022-06-22 17:10:41 +00001949 const std::map<std::string, std::string>& psAttributesConnections,
Gunnar Mills42cbe532019-08-15 15:26:54 -05001950 Callback&& callback)
1951{
1952 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributesData enter";
1953
1954 if (psAttributesConnections.empty())
1955 {
1956 BMCWEB_LOG_DEBUG << "Can't find PowerSupplyAttributes, no connections!";
1957 callback(inventoryItems);
1958 return;
1959 }
1960
1961 // Assuming just one connection (service) for now
Nan Zhoufe04d492022-06-22 17:10:41 +00001962 auto it = psAttributesConnections.begin();
Gunnar Mills42cbe532019-08-15 15:26:54 -05001963
1964 const std::string& psAttributesPath = (*it).first;
1965 const std::string& psAttributesConnection = (*it).second;
1966
1967 // Response handler for Get DeratingFactor property
Ed Tanous002d39b2022-05-31 08:59:27 -07001968 auto respHandler =
1969 [sensorsAsyncResp, inventoryItems,
1970 callback{std::forward<Callback>(callback)}](
1971 const boost::system::error_code ec, const uint32_t value) {
Gunnar Mills42cbe532019-08-15 15:26:54 -05001972 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributesData respHandler enter";
1973 if (ec)
1974 {
1975 BMCWEB_LOG_ERROR
1976 << "getPowerSupplyAttributesData respHandler DBus error " << ec;
zhanghch058d1b46d2021-04-01 11:18:24 +08001977 messages::internalError(sensorsAsyncResp->asyncResp->res);
Gunnar Mills42cbe532019-08-15 15:26:54 -05001978 return;
1979 }
1980
Jonathan Doman1e1e5982021-06-11 09:36:17 -07001981 BMCWEB_LOG_DEBUG << "PS EfficiencyPercent value: " << value;
1982 // Store value in Power Supply Inventory Items
1983 for (InventoryItem& inventoryItem : *inventoryItems)
Gunnar Mills42cbe532019-08-15 15:26:54 -05001984 {
Ed Tanous55f79e62022-01-25 11:26:16 -08001985 if (inventoryItem.isPowerSupply)
Gunnar Mills42cbe532019-08-15 15:26:54 -05001986 {
Jonathan Doman1e1e5982021-06-11 09:36:17 -07001987 inventoryItem.powerSupplyEfficiencyPercent =
1988 static_cast<int>(value);
Gunnar Mills42cbe532019-08-15 15:26:54 -05001989 }
1990 }
Gunnar Mills42cbe532019-08-15 15:26:54 -05001991
1992 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributesData respHandler exit";
1993 callback(inventoryItems);
1994 };
1995
1996 // Get the DeratingFactor property for the PowerSupplyAttributes
1997 // Currently only property on the interface/only one we care about
Jonathan Doman1e1e5982021-06-11 09:36:17 -07001998 sdbusplus::asio::getProperty<uint32_t>(
1999 *crow::connections::systemBus, psAttributesConnection, psAttributesPath,
2000 "xyz.openbmc_project.Control.PowerSupplyAttributes", "DeratingFactor",
2001 std::move(respHandler));
Gunnar Mills42cbe532019-08-15 15:26:54 -05002002
2003 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributesData exit";
2004}
2005
2006/**
2007 * @brief Gets the Power Supply Attributes such as EfficiencyPercent
2008 *
2009 * Gets the D-Bus connection (service) that provides Power Supply Attributes
2010 * data. Then gets the Power Supply Attributes data from the connection
2011 * (currently just assumes 1 connection) and stores the data in the inventory
2012 * item.
2013 *
2014 * This data is later used to provide sensor property values in the JSON
2015 * response. DeratingFactor on D-Bus is mapped to EfficiencyPercent on Redfish.
2016 *
2017 * Finds the Power Supply Attributes data asynchronously. Invokes callback
2018 * when information has been obtained.
2019 *
2020 * The callback must have the following signature:
2021 * @code
2022 * callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems)
2023 * @endcode
2024 *
2025 * @param sensorsAsyncResp Pointer to object holding response data.
2026 * @param inventoryItems D-Bus inventory items associated with sensors.
2027 * @param callback Callback to invoke when data has been obtained.
2028 */
2029template <typename Callback>
2030void getPowerSupplyAttributes(
2031 std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
2032 std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
2033 Callback&& callback)
2034{
2035 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes enter";
2036
2037 // Only need the power supply attributes when the Power Schema
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02002038 if (sensorsAsyncResp->chassisSubNode != sensors::node::power)
Gunnar Mills42cbe532019-08-15 15:26:54 -05002039 {
2040 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes exit since not Power";
2041 callback(inventoryItems);
2042 return;
2043 }
2044
2045 const std::array<std::string, 1> interfaces = {
2046 "xyz.openbmc_project.Control.PowerSupplyAttributes"};
2047
2048 // Response handler for parsing output from GetSubTree
Ed Tanousb9d36b42022-02-26 21:42:46 -08002049 auto respHandler =
2050 [callback{std::forward<Callback>(callback)}, sensorsAsyncResp,
2051 inventoryItems](
2052 const boost::system::error_code ec,
2053 const dbus::utility::MapperGetSubTreeResponse& subtree) {
Ed Tanous002d39b2022-05-31 08:59:27 -07002054 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes respHandler enter";
2055 if (ec)
2056 {
2057 messages::internalError(sensorsAsyncResp->asyncResp->res);
2058 BMCWEB_LOG_ERROR
2059 << "getPowerSupplyAttributes respHandler DBus error " << ec;
2060 return;
2061 }
2062 if (subtree.empty())
2063 {
2064 BMCWEB_LOG_DEBUG << "Can't find Power Supply Attributes!";
2065 callback(inventoryItems);
2066 return;
2067 }
Gunnar Mills42cbe532019-08-15 15:26:54 -05002068
Ed Tanous002d39b2022-05-31 08:59:27 -07002069 // Currently we only support 1 power supply attribute, use this for
2070 // all the power supplies. Build map of object path to connection.
2071 // Assume just 1 connection and 1 path for now.
Nan Zhoufe04d492022-06-22 17:10:41 +00002072 std::map<std::string, std::string> psAttributesConnections;
Gunnar Mills42cbe532019-08-15 15:26:54 -05002073
Ed Tanous002d39b2022-05-31 08:59:27 -07002074 if (subtree[0].first.empty() || subtree[0].second.empty())
2075 {
2076 BMCWEB_LOG_DEBUG << "Power Supply Attributes mapper error!";
2077 callback(inventoryItems);
2078 return;
2079 }
Gunnar Mills42cbe532019-08-15 15:26:54 -05002080
Ed Tanous002d39b2022-05-31 08:59:27 -07002081 const std::string& psAttributesPath = subtree[0].first;
2082 const std::string& connection = subtree[0].second.begin()->first;
Gunnar Mills42cbe532019-08-15 15:26:54 -05002083
Ed Tanous002d39b2022-05-31 08:59:27 -07002084 if (connection.empty())
2085 {
2086 BMCWEB_LOG_DEBUG << "Power Supply Attributes mapper error!";
2087 callback(inventoryItems);
2088 return;
2089 }
Gunnar Mills42cbe532019-08-15 15:26:54 -05002090
Ed Tanous002d39b2022-05-31 08:59:27 -07002091 psAttributesConnections[psAttributesPath] = connection;
2092 BMCWEB_LOG_DEBUG << "Added mapping " << psAttributesPath << " -> "
2093 << connection;
Gunnar Mills42cbe532019-08-15 15:26:54 -05002094
Ed Tanous002d39b2022-05-31 08:59:27 -07002095 getPowerSupplyAttributesData(sensorsAsyncResp, inventoryItems,
2096 psAttributesConnections,
2097 std::move(callback));
2098 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes respHandler exit";
2099 };
Gunnar Mills42cbe532019-08-15 15:26:54 -05002100 // Make call to ObjectMapper to find the PowerSupplyAttributes service
2101 crow::connections::systemBus->async_method_call(
2102 std::move(respHandler), "xyz.openbmc_project.ObjectMapper",
2103 "/xyz/openbmc_project/object_mapper",
2104 "xyz.openbmc_project.ObjectMapper", "GetSubTree",
2105 "/xyz/openbmc_project", 0, interfaces);
2106 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes exit";
2107}
2108
2109/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002110 * @brief Gets inventory items associated with sensors.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002111 *
2112 * Finds the inventory items that are associated with the specified sensors.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002113 * Then gets D-Bus data for the inventory items, such as presence and VPD.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002114 *
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002115 * This data is later used to provide sensor property values in the JSON
2116 * response.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002117 *
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002118 * Finds the inventory items asynchronously. Invokes callback when the
2119 * inventory items have been obtained.
2120 *
2121 * The callback must have the following signature:
2122 * @code
2123 * callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems)
2124 * @endcode
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002125 *
2126 * @param sensorsAsyncResp Pointer to object holding response data.
2127 * @param sensorNames All sensors within the current chassis.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002128 * implements ObjectManager.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002129 * @param callback Callback to invoke when inventory items have been obtained.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002130 */
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002131template <typename Callback>
Ed Tanousd0090732022-10-04 17:22:56 -07002132static void
2133 getInventoryItems(std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
2134 const std::shared_ptr<std::set<std::string>> sensorNames,
2135 Callback&& callback)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002136{
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002137 BMCWEB_LOG_DEBUG << "getInventoryItems enter";
2138 auto getInventoryItemAssociationsCb =
Ed Tanousd0090732022-10-04 17:22:56 -07002139 [sensorsAsyncResp, callback{std::forward<Callback>(callback)}](
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002140 std::shared_ptr<std::vector<InventoryItem>> inventoryItems) {
Ed Tanous002d39b2022-05-31 08:59:27 -07002141 BMCWEB_LOG_DEBUG << "getInventoryItemAssociationsCb enter";
2142 auto getInventoryItemsConnectionsCb =
Ed Tanousd0090732022-10-04 17:22:56 -07002143 [sensorsAsyncResp, inventoryItems,
Ed Tanous002d39b2022-05-31 08:59:27 -07002144 callback{std::forward<const Callback>(callback)}](
Nan Zhoufe04d492022-06-22 17:10:41 +00002145 std::shared_ptr<std::set<std::string>> invConnections) {
Ed Tanous002d39b2022-05-31 08:59:27 -07002146 BMCWEB_LOG_DEBUG << "getInventoryItemsConnectionsCb enter";
2147 auto getInventoryItemsDataCb = [sensorsAsyncResp, inventoryItems,
2148 callback{std::move(callback)}]() {
2149 BMCWEB_LOG_DEBUG << "getInventoryItemsDataCb enter";
Gunnar Mills42cbe532019-08-15 15:26:54 -05002150
Ed Tanous002d39b2022-05-31 08:59:27 -07002151 auto getInventoryLedsCb = [sensorsAsyncResp, inventoryItems,
2152 callback{std::move(callback)}]() {
2153 BMCWEB_LOG_DEBUG << "getInventoryLedsCb enter";
2154 // Find Power Supply Attributes and get the data
2155 getPowerSupplyAttributes(sensorsAsyncResp, inventoryItems,
2156 std::move(callback));
2157 BMCWEB_LOG_DEBUG << "getInventoryLedsCb exit";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002158 };
2159
Ed Tanous002d39b2022-05-31 08:59:27 -07002160 // Find led connections and get the data
2161 getInventoryLeds(sensorsAsyncResp, inventoryItems,
2162 std::move(getInventoryLedsCb));
2163 BMCWEB_LOG_DEBUG << "getInventoryItemsDataCb exit";
2164 };
2165
2166 // Get inventory item data from connections
2167 getInventoryItemsData(sensorsAsyncResp, inventoryItems,
Ed Tanousd0090732022-10-04 17:22:56 -07002168 invConnections,
Ed Tanous002d39b2022-05-31 08:59:27 -07002169 std::move(getInventoryItemsDataCb));
2170 BMCWEB_LOG_DEBUG << "getInventoryItemsConnectionsCb exit";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002171 };
2172
Ed Tanous002d39b2022-05-31 08:59:27 -07002173 // Get connections that provide inventory item data
2174 getInventoryItemsConnections(sensorsAsyncResp, inventoryItems,
2175 std::move(getInventoryItemsConnectionsCb));
2176 BMCWEB_LOG_DEBUG << "getInventoryItemAssociationsCb exit";
2177 };
2178
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002179 // Get associations from sensors to inventory items
Ed Tanousd0090732022-10-04 17:22:56 -07002180 getInventoryItemAssociations(sensorsAsyncResp, sensorNames,
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002181 std::move(getInventoryItemAssociationsCb));
2182 BMCWEB_LOG_DEBUG << "getInventoryItems exit";
2183}
2184
2185/**
2186 * @brief Returns JSON PowerSupply object for the specified inventory item.
2187 *
2188 * Searches for a JSON PowerSupply object that matches the specified inventory
2189 * item. If one is not found, a new PowerSupply object is added to the JSON
2190 * array.
2191 *
2192 * Multiple sensors are often associated with one power supply inventory item.
2193 * As a result, multiple sensor values are stored in one JSON PowerSupply
2194 * object.
2195 *
2196 * @param powerSupplyArray JSON array containing Redfish PowerSupply objects.
2197 * @param inventoryItem Inventory item for the power supply.
2198 * @param chassisId Chassis that contains the power supply.
2199 * @return JSON PowerSupply object for the specified inventory item.
2200 */
Ed Tanous23a21a12020-07-25 04:45:05 +00002201inline nlohmann::json& getPowerSupply(nlohmann::json& powerSupplyArray,
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002202 const InventoryItem& inventoryItem,
2203 const std::string& chassisId)
2204{
2205 // Check if matching PowerSupply object already exists in JSON array
2206 for (nlohmann::json& powerSupply : powerSupplyArray)
2207 {
2208 if (powerSupply["MemberId"] == inventoryItem.name)
2209 {
2210 return powerSupply;
2211 }
2212 }
2213
2214 // Add new PowerSupply object to JSON array
2215 powerSupplyArray.push_back({});
2216 nlohmann::json& powerSupply = powerSupplyArray.back();
2217 powerSupply["@odata.id"] =
2218 "/redfish/v1/Chassis/" + chassisId + "/Power#/PowerSupplies/";
2219 powerSupply["MemberId"] = inventoryItem.name;
2220 powerSupply["Name"] = boost::replace_all_copy(inventoryItem.name, "_", " ");
2221 powerSupply["Manufacturer"] = inventoryItem.manufacturer;
2222 powerSupply["Model"] = inventoryItem.model;
2223 powerSupply["PartNumber"] = inventoryItem.partNumber;
2224 powerSupply["SerialNumber"] = inventoryItem.serialNumber;
Anthony Wilsond5005492019-07-31 16:34:17 -05002225 setLedState(powerSupply, &inventoryItem);
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002226
Gunnar Mills42cbe532019-08-15 15:26:54 -05002227 if (inventoryItem.powerSupplyEfficiencyPercent >= 0)
2228 {
2229 powerSupply["EfficiencyPercent"] =
2230 inventoryItem.powerSupplyEfficiencyPercent;
2231 }
2232
2233 powerSupply["Status"]["State"] = getState(&inventoryItem);
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002234 const char* health = inventoryItem.isFunctional ? "OK" : "Critical";
2235 powerSupply["Status"]["Health"] = health;
2236
2237 return powerSupply;
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002238}
2239
2240/**
Shawn McCarneyde629b62019-03-08 10:42:51 -06002241 * @brief Gets the values of the specified sensors.
2242 *
2243 * Stores the results as JSON in the SensorsAsyncResp.
2244 *
2245 * Gets the sensor values asynchronously. Stores the results later when the
2246 * information has been obtained.
2247 *
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002248 * The sensorNames set contains all requested sensors for the current chassis.
Shawn McCarneyde629b62019-03-08 10:42:51 -06002249 *
2250 * To minimize the number of DBus calls, the DBus method
2251 * org.freedesktop.DBus.ObjectManager.GetManagedObjects() is used to get the
2252 * values of all sensors provided by a connection (service).
2253 *
2254 * The connections set contains all the connections that provide sensor values.
2255 *
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002256 * The InventoryItem vector contains D-Bus inventory items associated with the
2257 * sensors. Inventory item data is needed for some Redfish sensor properties.
2258 *
Shawn McCarneyde629b62019-03-08 10:42:51 -06002259 * @param SensorsAsyncResp Pointer to object holding response data.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002260 * @param sensorNames All requested sensors within the current chassis.
Shawn McCarneyde629b62019-03-08 10:42:51 -06002261 * @param connections Connections that provide sensor values.
Shawn McCarneyde629b62019-03-08 10:42:51 -06002262 * implements ObjectManager.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002263 * @param inventoryItems Inventory items associated with the sensors.
Shawn McCarneyde629b62019-03-08 10:42:51 -06002264 */
Ed Tanous23a21a12020-07-25 04:45:05 +00002265inline void getSensorData(
Ed Tanous81ce6092020-12-17 16:54:55 +00002266 const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
Nan Zhoufe04d492022-06-22 17:10:41 +00002267 const std::shared_ptr<std::set<std::string>>& sensorNames,
2268 const std::set<std::string>& connections,
Ed Tanousb5a76932020-09-29 16:16:58 -07002269 const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems)
Shawn McCarneyde629b62019-03-08 10:42:51 -06002270{
2271 BMCWEB_LOG_DEBUG << "getSensorData enter";
2272 // Get managed objects from all services exposing sensors
2273 for (const std::string& connection : connections)
2274 {
2275 // Response handler to process managed objects
Ed Tanous002d39b2022-05-31 08:59:27 -07002276 auto getManagedObjectsCb =
2277 [sensorsAsyncResp, sensorNames,
2278 inventoryItems](const boost::system::error_code ec,
Ed Tanous02cad962022-06-30 16:50:15 -07002279 const dbus::utility::ManagedObjectType& resp) {
Shawn McCarneyde629b62019-03-08 10:42:51 -06002280 BMCWEB_LOG_DEBUG << "getManagedObjectsCb enter";
2281 if (ec)
2282 {
2283 BMCWEB_LOG_ERROR << "getManagedObjectsCb DBUS error: " << ec;
zhanghch058d1b46d2021-04-01 11:18:24 +08002284 messages::internalError(sensorsAsyncResp->asyncResp->res);
Shawn McCarneyde629b62019-03-08 10:42:51 -06002285 return;
2286 }
2287 // Go through all objects and update response with sensor data
2288 for (const auto& objDictEntry : resp)
2289 {
2290 const std::string& objPath =
2291 static_cast<const std::string&>(objDictEntry.first);
2292 BMCWEB_LOG_DEBUG << "getManagedObjectsCb parsing object "
2293 << objPath;
2294
Shawn McCarneyde629b62019-03-08 10:42:51 -06002295 std::vector<std::string> split;
2296 // Reserve space for
2297 // /xyz/openbmc_project/sensors/<name>/<subname>
2298 split.reserve(6);
2299 boost::algorithm::split(split, objPath, boost::is_any_of("/"));
2300 if (split.size() < 6)
2301 {
2302 BMCWEB_LOG_ERROR << "Got path that isn't long enough "
2303 << objPath;
2304 continue;
2305 }
2306 // These indexes aren't intuitive, as boost::split puts an empty
2307 // string at the beginning
2308 const std::string& sensorType = split[4];
2309 const std::string& sensorName = split[5];
2310 BMCWEB_LOG_DEBUG << "sensorName " << sensorName
2311 << " sensorType " << sensorType;
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002312 if (sensorNames->find(objPath) == sensorNames->end())
Shawn McCarneyde629b62019-03-08 10:42:51 -06002313 {
Andrew Geissleraccdbb22021-11-09 15:24:45 -06002314 BMCWEB_LOG_DEBUG << sensorName << " not in sensor list ";
Shawn McCarneyde629b62019-03-08 10:42:51 -06002315 continue;
2316 }
2317
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002318 // Find inventory item (if any) associated with sensor
2319 InventoryItem* inventoryItem =
2320 findInventoryItemForSensor(inventoryItems, objPath);
2321
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002322 const std::string& sensorSchema =
Ed Tanous81ce6092020-12-17 16:54:55 +00002323 sensorsAsyncResp->chassisSubNode;
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002324
2325 nlohmann::json* sensorJson = nullptr;
2326
Nan Zhou928fefb2022-03-28 08:45:00 -07002327 if (sensorSchema == sensors::node::sensors &&
2328 !sensorsAsyncResp->efficientExpand)
Shawn McCarneyde629b62019-03-08 10:42:51 -06002329 {
Ed Tanousc1d019a2022-08-06 09:36:06 -07002330 std::string sensorTypeEscaped(sensorType);
2331 sensorTypeEscaped.erase(
2332 std::remove(sensorTypeEscaped.begin(),
2333 sensorTypeEscaped.end(), '_'),
2334 sensorTypeEscaped.end());
2335 std::string sensorId(sensorTypeEscaped);
2336 sensorId += "_";
2337 sensorId += sensorName;
2338
zhanghch058d1b46d2021-04-01 11:18:24 +08002339 sensorsAsyncResp->asyncResp->res.jsonValue["@odata.id"] =
Ed Tanousc1d019a2022-08-06 09:36:06 -07002340 crow::utility::urlFromPieces(
2341 "redfish", "v1", "Chassis",
2342 sensorsAsyncResp->chassisId,
2343 sensorsAsyncResp->chassisSubNode, sensorId);
zhanghch058d1b46d2021-04-01 11:18:24 +08002344 sensorJson = &(sensorsAsyncResp->asyncResp->res.jsonValue);
Shawn McCarneyde629b62019-03-08 10:42:51 -06002345 }
2346 else
2347 {
Ed Tanous271584a2019-07-09 16:24:22 -07002348 std::string fieldName;
Nan Zhou928fefb2022-03-28 08:45:00 -07002349 if (sensorsAsyncResp->efficientExpand)
2350 {
2351 fieldName = "Members";
2352 }
2353 else if (sensorType == "temperature")
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002354 {
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002355 fieldName = "Temperatures";
2356 }
2357 else if (sensorType == "fan" || sensorType == "fan_tach" ||
2358 sensorType == "fan_pwm")
2359 {
2360 fieldName = "Fans";
2361 }
2362 else if (sensorType == "voltage")
2363 {
2364 fieldName = "Voltages";
2365 }
2366 else if (sensorType == "power")
2367 {
Ed Tanous55f79e62022-01-25 11:26:16 -08002368 if (sensorName == "total_power")
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002369 {
2370 fieldName = "PowerControl";
2371 }
2372 else if ((inventoryItem != nullptr) &&
2373 (inventoryItem->isPowerSupply))
2374 {
2375 fieldName = "PowerSupplies";
2376 }
2377 else
2378 {
2379 // Other power sensors are in SensorCollection
2380 continue;
2381 }
2382 }
2383 else
2384 {
2385 BMCWEB_LOG_ERROR << "Unsure how to handle sensorType "
2386 << sensorType;
2387 continue;
2388 }
2389
2390 nlohmann::json& tempArray =
zhanghch058d1b46d2021-04-01 11:18:24 +08002391 sensorsAsyncResp->asyncResp->res.jsonValue[fieldName];
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002392 if (fieldName == "PowerControl")
2393 {
2394 if (tempArray.empty())
2395 {
2396 // Put multiple "sensors" into a single
2397 // PowerControl. Follows MemberId naming and
2398 // naming in power.hpp.
Ed Tanous14766872022-03-15 10:44:42 -07002399 nlohmann::json::object_t power;
2400 power["@odata.id"] =
2401 "/redfish/v1/Chassis/" +
2402 sensorsAsyncResp->chassisId + "/" +
2403 sensorsAsyncResp->chassisSubNode + "#/" +
2404 fieldName + "/0";
2405 tempArray.push_back(std::move(power));
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002406 }
2407 sensorJson = &(tempArray.back());
2408 }
2409 else if (fieldName == "PowerSupplies")
2410 {
2411 if (inventoryItem != nullptr)
2412 {
2413 sensorJson =
2414 &(getPowerSupply(tempArray, *inventoryItem,
Ed Tanous81ce6092020-12-17 16:54:55 +00002415 sensorsAsyncResp->chassisId));
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002416 }
2417 }
Nan Zhou928fefb2022-03-28 08:45:00 -07002418 else if (fieldName == "Members")
2419 {
Ed Tanous677bb752022-09-15 10:52:19 -07002420 std::string sensorTypeEscaped(sensorType);
2421 sensorTypeEscaped.erase(
2422 std::remove(sensorTypeEscaped.begin(),
2423 sensorTypeEscaped.end(), '_'),
2424 sensorTypeEscaped.end());
2425 std::string sensorId(sensorTypeEscaped);
2426 sensorId += "_";
2427 sensorId += sensorName;
2428
Ed Tanous14766872022-03-15 10:44:42 -07002429 nlohmann::json::object_t member;
Ed Tanous677bb752022-09-15 10:52:19 -07002430 member["@odata.id"] = crow::utility::urlFromPieces(
2431 "redfish", "v1", "Chassis",
2432 sensorsAsyncResp->chassisId,
2433 sensorsAsyncResp->chassisSubNode, sensorId);
Ed Tanous14766872022-03-15 10:44:42 -07002434 tempArray.push_back(std::move(member));
Nan Zhou928fefb2022-03-28 08:45:00 -07002435 sensorJson = &(tempArray.back());
2436 }
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002437 else
2438 {
Ed Tanous14766872022-03-15 10:44:42 -07002439 nlohmann::json::object_t member;
2440 member["@odata.id"] = "/redfish/v1/Chassis/" +
2441 sensorsAsyncResp->chassisId +
2442 "/" +
2443 sensorsAsyncResp->chassisSubNode +
2444 "#/" + fieldName + "/";
2445 tempArray.push_back(std::move(member));
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002446 sensorJson = &(tempArray.back());
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002447 }
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002448 }
Shawn McCarneyde629b62019-03-08 10:42:51 -06002449
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002450 if (sensorJson != nullptr)
2451 {
Ed Tanous1d7c0052022-08-09 12:32:26 -07002452 objectInterfacesToJson(sensorName, sensorType,
2453 sensorsAsyncResp->chassisSubNode,
2454 objDictEntry.second, *sensorJson,
2455 inventoryItem);
2456
2457 std::string path = "/xyz/openbmc_project/sensors/";
2458 path += sensorType;
2459 path += "/";
2460 path += sensorName;
Ed Tanousc1d019a2022-08-06 09:36:06 -07002461 sensorsAsyncResp->addMetadata(*sensorJson, path);
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002462 }
Shawn McCarneyde629b62019-03-08 10:42:51 -06002463 }
Ed Tanous81ce6092020-12-17 16:54:55 +00002464 if (sensorsAsyncResp.use_count() == 1)
James Feist8bd25cc2019-03-15 15:14:00 -07002465 {
Ed Tanous81ce6092020-12-17 16:54:55 +00002466 sortJSONResponse(sensorsAsyncResp);
Nan Zhou928fefb2022-03-28 08:45:00 -07002467 if (sensorsAsyncResp->chassisSubNode ==
2468 sensors::node::sensors &&
2469 sensorsAsyncResp->efficientExpand)
2470 {
2471 sensorsAsyncResp->asyncResp->res
2472 .jsonValue["Members@odata.count"] =
2473 sensorsAsyncResp->asyncResp->res.jsonValue["Members"]
2474 .size();
2475 }
2476 else if (sensorsAsyncResp->chassisSubNode ==
2477 sensors::node::thermal)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002478 {
Ed Tanous81ce6092020-12-17 16:54:55 +00002479 populateFanRedundancy(sensorsAsyncResp);
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002480 }
James Feist8bd25cc2019-03-15 15:14:00 -07002481 }
Shawn McCarneyde629b62019-03-08 10:42:51 -06002482 BMCWEB_LOG_DEBUG << "getManagedObjectsCb exit";
2483 };
2484
Shawn McCarneyde629b62019-03-08 10:42:51 -06002485 crow::connections::systemBus->async_method_call(
Ed Tanousd0090732022-10-04 17:22:56 -07002486 getManagedObjectsCb, connection, "/xyz/openbmc_project/sensors",
Shawn McCarneyde629b62019-03-08 10:42:51 -06002487 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
Ed Tanous23a21a12020-07-25 04:45:05 +00002488 }
Shawn McCarneyde629b62019-03-08 10:42:51 -06002489 BMCWEB_LOG_DEBUG << "getSensorData exit";
2490}
2491
Nan Zhoufe04d492022-06-22 17:10:41 +00002492inline void
2493 processSensorList(const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
2494 const std::shared_ptr<std::set<std::string>>& sensorNames)
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002495{
Nan Zhoufe04d492022-06-22 17:10:41 +00002496 auto getConnectionCb = [sensorsAsyncResp, sensorNames](
2497 const std::set<std::string>& connections) {
Ed Tanous002d39b2022-05-31 08:59:27 -07002498 BMCWEB_LOG_DEBUG << "getConnectionCb enter";
Ed Tanousd0090732022-10-04 17:22:56 -07002499 auto getInventoryItemsCb =
2500 [sensorsAsyncResp, sensorNames,
2501 connections](const std::shared_ptr<std::vector<InventoryItem>>&
2502 inventoryItems) {
2503 BMCWEB_LOG_DEBUG << "getInventoryItemsCb enter";
2504 // Get sensor data and store results in JSON
2505 getSensorData(sensorsAsyncResp, sensorNames, connections,
2506 inventoryItems);
2507 BMCWEB_LOG_DEBUG << "getInventoryItemsCb exit";
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002508 };
2509
Ed Tanousd0090732022-10-04 17:22:56 -07002510 // Get inventory items associated with sensors
2511 getInventoryItems(sensorsAsyncResp, sensorNames,
2512 std::move(getInventoryItemsCb));
2513
Ed Tanous002d39b2022-05-31 08:59:27 -07002514 BMCWEB_LOG_DEBUG << "getConnectionCb exit";
2515 };
2516
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002517 // Get set of connections that provide sensor values
Ed Tanous81ce6092020-12-17 16:54:55 +00002518 getConnections(sensorsAsyncResp, sensorNames, std::move(getConnectionCb));
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002519}
2520
Shawn McCarneyde629b62019-03-08 10:42:51 -06002521/**
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +01002522 * @brief Entry point for retrieving sensors data related to requested
2523 * chassis.
Kowalski, Kamil588c3f02018-04-03 14:55:27 +02002524 * @param SensorsAsyncResp Pointer to object holding response data
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +01002525 */
Ed Tanousb5a76932020-09-29 16:16:58 -07002526inline void
Ed Tanous81ce6092020-12-17 16:54:55 +00002527 getChassisData(const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp)
Ed Tanous1abe55e2018-09-05 08:30:59 -07002528{
2529 BMCWEB_LOG_DEBUG << "getChassisData enter";
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002530 auto getChassisCb =
Ed Tanous81ce6092020-12-17 16:54:55 +00002531 [sensorsAsyncResp](
Nan Zhoufe04d492022-06-22 17:10:41 +00002532 const std::shared_ptr<std::set<std::string>>& sensorNames) {
Ed Tanous002d39b2022-05-31 08:59:27 -07002533 BMCWEB_LOG_DEBUG << "getChassisCb enter";
2534 processSensorList(sensorsAsyncResp, sensorNames);
2535 BMCWEB_LOG_DEBUG << "getChassisCb exit";
2536 };
Nan Zhou928fefb2022-03-28 08:45:00 -07002537 // SensorCollection doesn't contain the Redundancy property
2538 if (sensorsAsyncResp->chassisSubNode != sensors::node::sensors)
2539 {
2540 sensorsAsyncResp->asyncResp->res.jsonValue["Redundancy"] =
2541 nlohmann::json::array();
2542 }
Shawn McCarney26f03892019-05-03 13:20:24 -05002543 // Get set of sensors in chassis
Ed Tanous7f1cc262022-08-09 13:33:57 -07002544 getChassis(sensorsAsyncResp->asyncResp, sensorsAsyncResp->chassisId,
2545 sensorsAsyncResp->chassisSubNode, sensorsAsyncResp->types,
2546 std::move(getChassisCb));
Ed Tanous1abe55e2018-09-05 08:30:59 -07002547 BMCWEB_LOG_DEBUG << "getChassisData exit";
Ed Tanous271584a2019-07-09 16:24:22 -07002548}
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +01002549
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302550/**
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002551 * @brief Find the requested sensorName in the list of all sensors supplied by
2552 * the chassis node
2553 *
2554 * @param sensorName The sensor name supplied in the PATCH request
2555 * @param sensorsList The list of sensors managed by the chassis node
2556 * @param sensorsModified The list of sensors that were found as a result of
2557 * repeated calls to this function
2558 */
Nan Zhoufe04d492022-06-22 17:10:41 +00002559inline bool
2560 findSensorNameUsingSensorPath(std::string_view sensorName,
Ed Tanous02cad962022-06-30 16:50:15 -07002561 const std::set<std::string>& sensorsList,
Nan Zhoufe04d492022-06-22 17:10:41 +00002562 std::set<std::string>& sensorsModified)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002563{
Nan Zhoufe04d492022-06-22 17:10:41 +00002564 for (const auto& chassisSensor : sensorsList)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002565 {
George Liu28aa8de2021-02-01 15:13:30 +08002566 sdbusplus::message::object_path path(chassisSensor);
Ed Tanousb00dcc22021-02-23 12:52:50 -08002567 std::string thisSensorName = path.filename();
George Liu28aa8de2021-02-01 15:13:30 +08002568 if (thisSensorName.empty())
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002569 {
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002570 continue;
2571 }
2572 if (thisSensorName == sensorName)
2573 {
2574 sensorsModified.emplace(chassisSensor);
2575 return true;
2576 }
2577 }
2578 return false;
2579}
2580
2581/**
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302582 * @brief Entry point for overriding sensor values of given sensor
2583 *
zhanghch058d1b46d2021-04-01 11:18:24 +08002584 * @param sensorAsyncResp response object
Carol Wang4bb3dc32019-10-17 18:15:02 +08002585 * @param allCollections Collections extract from sensors' request patch info
jayaprakash Mutyala91e130a2020-03-04 22:26:38 +00002586 * @param chassisSubNode Chassis Node for which the query has to happen
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302587 */
Ed Tanous23a21a12020-07-25 04:45:05 +00002588inline void setSensorsOverride(
Ed Tanousb5a76932020-09-29 16:16:58 -07002589 const std::shared_ptr<SensorsAsyncResp>& sensorAsyncResp,
Carol Wang4bb3dc32019-10-17 18:15:02 +08002590 std::unordered_map<std::string, std::vector<nlohmann::json>>&
jayaprakash Mutyala397fd612020-02-06 23:33:34 +00002591 allCollections)
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302592{
jayaprakash Mutyala70d1d0a2020-01-21 23:41:36 +00002593 BMCWEB_LOG_INFO << "setSensorsOverride for subNode"
Carol Wang4bb3dc32019-10-17 18:15:02 +08002594 << sensorAsyncResp->chassisSubNode << "\n";
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302595
Ed Tanous543f4402022-01-06 13:12:53 -08002596 const char* propertyValueName = nullptr;
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302597 std::unordered_map<std::string, std::pair<double, std::string>> overrideMap;
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302598 std::string memberId;
Ed Tanous543f4402022-01-06 13:12:53 -08002599 double value = 0.0;
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302600 for (auto& collectionItems : allCollections)
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302601 {
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302602 if (collectionItems.first == "Temperatures")
2603 {
2604 propertyValueName = "ReadingCelsius";
2605 }
2606 else if (collectionItems.first == "Fans")
2607 {
2608 propertyValueName = "Reading";
2609 }
2610 else
2611 {
2612 propertyValueName = "ReadingVolts";
2613 }
2614 for (auto& item : collectionItems.second)
2615 {
zhanghch058d1b46d2021-04-01 11:18:24 +08002616 if (!json_util::readJson(item, sensorAsyncResp->asyncResp->res,
2617 "MemberId", memberId, propertyValueName,
2618 value))
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302619 {
2620 return;
2621 }
2622 overrideMap.emplace(memberId,
2623 std::make_pair(value, collectionItems.first));
2624 }
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302625 }
Carol Wang4bb3dc32019-10-17 18:15:02 +08002626
Ed Tanous002d39b2022-05-31 08:59:27 -07002627 auto getChassisSensorListCb =
2628 [sensorAsyncResp, overrideMap](
Nan Zhoufe04d492022-06-22 17:10:41 +00002629 const std::shared_ptr<std::set<std::string>>& sensorsList) {
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002630 // Match sensor names in the PATCH request to those managed by the
2631 // chassis node
Nan Zhoufe04d492022-06-22 17:10:41 +00002632 const std::shared_ptr<std::set<std::string>> sensorNames =
2633 std::make_shared<std::set<std::string>>();
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302634 for (const auto& item : overrideMap)
2635 {
2636 const auto& sensor = item.first;
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002637 if (!findSensorNameUsingSensorPath(sensor, *sensorsList,
2638 *sensorNames))
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302639 {
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302640 BMCWEB_LOG_INFO << "Unable to find memberId " << item.first;
zhanghch058d1b46d2021-04-01 11:18:24 +08002641 messages::resourceNotFound(sensorAsyncResp->asyncResp->res,
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302642 item.second.second, item.first);
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302643 return;
2644 }
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302645 }
2646 // Get the connection to which the memberId belongs
Ed Tanous002d39b2022-05-31 08:59:27 -07002647 auto getObjectsWithConnectionCb =
Nan Zhoufe04d492022-06-22 17:10:41 +00002648 [sensorAsyncResp,
2649 overrideMap](const std::set<std::string>& /*connections*/,
2650 const std::set<std::pair<std::string, std::string>>&
2651 objectsWithConnection) {
Jayaprakash Mutyala4f277b52021-12-08 22:46:49 +00002652 if (objectsWithConnection.size() != overrideMap.size())
2653 {
2654 BMCWEB_LOG_INFO
2655 << "Unable to find all objects with proper connection "
2656 << objectsWithConnection.size() << " requested "
2657 << overrideMap.size() << "\n";
2658 messages::resourceNotFound(sensorAsyncResp->asyncResp->res,
2659 sensorAsyncResp->chassisSubNode ==
2660 sensors::node::thermal
2661 ? "Temperatures"
2662 : "Voltages",
2663 "Count");
2664 return;
2665 }
2666 for (const auto& item : objectsWithConnection)
2667 {
2668 sdbusplus::message::object_path path(item.first);
2669 std::string sensorName = path.filename();
2670 if (sensorName.empty())
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302671 {
Jayaprakash Mutyala4f277b52021-12-08 22:46:49 +00002672 messages::internalError(sensorAsyncResp->asyncResp->res);
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302673 return;
2674 }
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302675
Jayaprakash Mutyala4f277b52021-12-08 22:46:49 +00002676 const auto& iterator = overrideMap.find(sensorName);
2677 if (iterator == overrideMap.end())
2678 {
2679 BMCWEB_LOG_INFO << "Unable to find sensor object"
2680 << item.first << "\n";
2681 messages::internalError(sensorAsyncResp->asyncResp->res);
2682 return;
2683 }
2684 crow::connections::systemBus->async_method_call(
2685 [sensorAsyncResp](const boost::system::error_code ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -07002686 if (ec)
2687 {
2688 if (ec.value() ==
2689 boost::system::errc::permission_denied)
Jayaprakash Mutyala4f277b52021-12-08 22:46:49 +00002690 {
Ed Tanous002d39b2022-05-31 08:59:27 -07002691 BMCWEB_LOG_WARNING
2692 << "Manufacturing mode is not Enabled...can't "
2693 "Override the sensor value. ";
Jayaprakash Mutyala4f277b52021-12-08 22:46:49 +00002694
Ed Tanous002d39b2022-05-31 08:59:27 -07002695 messages::insufficientPrivilege(
Jayaprakash Mutyala4f277b52021-12-08 22:46:49 +00002696 sensorAsyncResp->asyncResp->res);
Ed Tanous002d39b2022-05-31 08:59:27 -07002697 return;
Jayaprakash Mutyala4f277b52021-12-08 22:46:49 +00002698 }
Ed Tanous002d39b2022-05-31 08:59:27 -07002699 BMCWEB_LOG_DEBUG
2700 << "setOverrideValueStatus DBUS error: " << ec;
2701 messages::internalError(
2702 sensorAsyncResp->asyncResp->res);
2703 }
Jayaprakash Mutyala4f277b52021-12-08 22:46:49 +00002704 },
2705 item.second, item.first, "org.freedesktop.DBus.Properties",
2706 "Set", "xyz.openbmc_project.Sensor.Value", "Value",
Ed Tanous168e20c2021-12-13 14:39:53 -08002707 dbus::utility::DbusVariantType(iterator->second.first));
Jayaprakash Mutyala4f277b52021-12-08 22:46:49 +00002708 }
2709 };
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302710 // Get object with connection for the given sensor name
2711 getObjectsWithConnection(sensorAsyncResp, sensorNames,
2712 std::move(getObjectsWithConnectionCb));
2713 };
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302714 // get full sensor list for the given chassisId and cross verify the sensor.
Ed Tanous7f1cc262022-08-09 13:33:57 -07002715 getChassis(sensorAsyncResp->asyncResp, sensorAsyncResp->chassisId,
2716 sensorAsyncResp->chassisSubNode, sensorAsyncResp->types,
2717 std::move(getChassisSensorListCb));
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302718}
2719
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02002720/**
2721 * @brief Retrieves mapping of Redfish URIs to sensor value property to D-Bus
2722 * path of the sensor.
2723 *
2724 * Function builds valid Redfish response for sensor query of given chassis and
2725 * node. It then builds metadata about Redfish<->D-Bus correlations and provides
2726 * it to caller in a callback.
2727 *
2728 * @param chassis Chassis for which retrieval should be performed
2729 * @param node Node (group) of sensors. See sensors::node for supported values
2730 * @param mapComplete Callback to be called with retrieval result
2731 */
Krzysztof Grobelny021d32c2021-10-29 16:00:07 +02002732inline void retrieveUriToDbusMap(const std::string& chassis,
2733 const std::string& node,
2734 SensorsAsyncResp::DataCompleteCb&& mapComplete)
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02002735{
Ed Tanous02da7c52022-02-27 00:09:02 -08002736 decltype(sensors::paths)::const_iterator pathIt =
2737 std::find_if(sensors::paths.cbegin(), sensors::paths.cend(),
2738 [&node](auto&& val) { return val.first == node; });
2739 if (pathIt == sensors::paths.cend())
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02002740 {
2741 BMCWEB_LOG_ERROR << "Wrong node provided : " << node;
2742 mapComplete(boost::beast::http::status::bad_request, {});
2743 return;
2744 }
Krzysztof Grobelnyd51e0722021-04-16 13:15:21 +00002745
Nan Zhou72374eb2022-01-27 17:06:51 -08002746 auto asyncResp = std::make_shared<bmcweb::AsyncResp>();
Nan Zhoufe04d492022-06-22 17:10:41 +00002747 auto callback = [asyncResp, mapCompleteCb{std::move(mapComplete)}](
2748 const boost::beast::http::status status,
2749 const std::map<std::string, std::string>& uriToDbus) {
2750 mapCompleteCb(status, uriToDbus);
2751 };
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02002752
2753 auto resp = std::make_shared<SensorsAsyncResp>(
Krzysztof Grobelnyd51e0722021-04-16 13:15:21 +00002754 asyncResp, chassis, pathIt->second, node, std::move(callback));
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02002755 getChassisData(resp);
2756}
2757
Nan Zhoubacb2162022-04-06 11:28:32 -07002758namespace sensors
2759{
Nan Zhou928fefb2022-03-28 08:45:00 -07002760
Nan Zhoubacb2162022-04-06 11:28:32 -07002761inline void getChassisCallback(
Ed Tanousc1d019a2022-08-06 09:36:06 -07002762 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2763 std::string_view chassisId, std::string_view chassisSubNode,
Nan Zhoufe04d492022-06-22 17:10:41 +00002764 const std::shared_ptr<std::set<std::string>>& sensorNames)
Nan Zhoubacb2162022-04-06 11:28:32 -07002765{
Ed Tanousc1d019a2022-08-06 09:36:06 -07002766 BMCWEB_LOG_DEBUG << "getChassisCallback enter ";
Nan Zhoubacb2162022-04-06 11:28:32 -07002767
Ed Tanousc1d019a2022-08-06 09:36:06 -07002768 nlohmann::json& entriesArray = asyncResp->res.jsonValue["Members"];
2769 for (const std::string& sensor : *sensorNames)
Nan Zhoubacb2162022-04-06 11:28:32 -07002770 {
2771 BMCWEB_LOG_DEBUG << "Adding sensor: " << sensor;
2772
2773 sdbusplus::message::object_path path(sensor);
2774 std::string sensorName = path.filename();
2775 if (sensorName.empty())
2776 {
2777 BMCWEB_LOG_ERROR << "Invalid sensor path: " << sensor;
Ed Tanousc1d019a2022-08-06 09:36:06 -07002778 messages::internalError(asyncResp->res);
Nan Zhoubacb2162022-04-06 11:28:32 -07002779 return;
2780 }
Ed Tanousc1d019a2022-08-06 09:36:06 -07002781 std::string type = path.parent_path().filename();
2782 // fan_tach has an underscore in it, so remove it to "normalize" the
2783 // type in the URI
2784 type.erase(std::remove(type.begin(), type.end(), '_'), type.end());
2785
Ed Tanous14766872022-03-15 10:44:42 -07002786 nlohmann::json::object_t member;
Ed Tanousc1d019a2022-08-06 09:36:06 -07002787 std::string id = type;
2788 id += "_";
2789 id += sensorName;
2790 member["@odata.id"] = crow::utility::urlFromPieces(
2791 "redfish", "v1", "Chassis", chassisId, chassisSubNode, id);
2792
Ed Tanous14766872022-03-15 10:44:42 -07002793 entriesArray.push_back(std::move(member));
Nan Zhoubacb2162022-04-06 11:28:32 -07002794 }
2795
Ed Tanousc1d019a2022-08-06 09:36:06 -07002796 asyncResp->res.jsonValue["Members@odata.count"] = entriesArray.size();
Nan Zhoubacb2162022-04-06 11:28:32 -07002797 BMCWEB_LOG_DEBUG << "getChassisCallback exit";
2798}
Nan Zhoue6bd8462022-06-01 04:35:35 +00002799
Nan Zhoude167a62022-06-01 04:47:45 +00002800inline void
2801 handleSensorCollectionGet(App& app, const crow::Request& req,
2802 const std::shared_ptr<bmcweb::AsyncResp>& aResp,
2803 const std::string& chassisId)
2804{
2805 query_param::QueryCapabilities capabilities = {
2806 .canDelegateExpandLevel = 1,
2807 };
2808 query_param::Query delegatedQuery;
Carson Labrado3ba00072022-06-06 19:40:56 +00002809 if (!redfish::setUpRedfishRouteWithDelegation(app, req, aResp,
Nan Zhoude167a62022-06-01 04:47:45 +00002810 delegatedQuery, capabilities))
2811 {
2812 return;
2813 }
2814
2815 if (delegatedQuery.expandType != query_param::ExpandType::None)
2816 {
2817 // we perform efficient expand.
2818 auto asyncResp = std::make_shared<SensorsAsyncResp>(
2819 aResp, chassisId, sensors::dbus::sensorPaths,
2820 sensors::node::sensors,
2821 /*efficientExpand=*/true);
2822 getChassisData(asyncResp);
2823
2824 BMCWEB_LOG_DEBUG
2825 << "SensorCollection doGet exit via efficient expand handler";
2826 return;
Ed Tanous0bad3202022-06-02 13:53:59 -07002827 }
Nan Zhoude167a62022-06-01 04:47:45 +00002828
Nan Zhoude167a62022-06-01 04:47:45 +00002829 // We get all sensors as hyperlinkes in the chassis (this
2830 // implies we reply on the default query parameters handler)
Ed Tanousc1d019a2022-08-06 09:36:06 -07002831 getChassis(aResp, chassisId, sensors::node::sensors, dbus::sensorPaths,
2832 std::bind_front(sensors::getChassisCallback, aResp, chassisId,
2833 sensors::node::sensors));
2834}
Ed Tanous7f1cc262022-08-09 13:33:57 -07002835
Ed Tanousc1d019a2022-08-06 09:36:06 -07002836inline void
2837 getSensorFromDbus(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2838 const std::string& sensorPath,
2839 const ::dbus::utility::MapperGetObject& mapperResponse)
2840{
2841 if (mapperResponse.size() != 1)
2842 {
2843 messages::internalError(asyncResp->res);
2844 return;
2845 }
2846 const auto& valueIface = *mapperResponse.begin();
2847 const std::string& connectionName = valueIface.first;
2848 BMCWEB_LOG_DEBUG << "Looking up " << connectionName;
2849 BMCWEB_LOG_DEBUG << "Path " << sensorPath;
Krzysztof Grobelnyc1343bf2022-08-31 13:15:26 +02002850
2851 sdbusplus::asio::getAllProperties(
2852 *crow::connections::systemBus, connectionName, sensorPath, "",
Ed Tanousc1d019a2022-08-06 09:36:06 -07002853 [asyncResp,
2854 sensorPath](const boost::system::error_code ec,
2855 const ::dbus::utility::DBusPropertiesMap& valuesDict) {
2856 if (ec)
2857 {
2858 messages::internalError(asyncResp->res);
2859 return;
2860 }
2861 sdbusplus::message::object_path path(sensorPath);
2862 std::string name = path.filename();
2863 path = path.parent_path();
2864 std::string type = path.filename();
2865 objectPropertiesToJson(name, type, sensors::node::sensors, valuesDict,
2866 asyncResp->res.jsonValue, nullptr);
Krzysztof Grobelnyc1343bf2022-08-31 13:15:26 +02002867 });
Nan Zhoude167a62022-06-01 04:47:45 +00002868}
2869
Nan Zhoue6bd8462022-06-01 04:35:35 +00002870inline void handleSensorGet(App& app, const crow::Request& req,
Ed Tanousc1d019a2022-08-06 09:36:06 -07002871 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous677bb752022-09-15 10:52:19 -07002872 const std::string& chassisId,
Ed Tanousc1d019a2022-08-06 09:36:06 -07002873 const std::string& sensorId)
Nan Zhoue6bd8462022-06-01 04:35:35 +00002874{
Ed Tanousc1d019a2022-08-06 09:36:06 -07002875 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Nan Zhoue6bd8462022-06-01 04:35:35 +00002876 {
2877 return;
2878 }
Ed Tanousc1d019a2022-08-06 09:36:06 -07002879 size_t index = sensorId.find('_');
2880 if (index == std::string::npos)
2881 {
2882 messages::resourceNotFound(asyncResp->res, sensorId, "Sensor");
2883 return;
2884 }
Ed Tanous677bb752022-09-15 10:52:19 -07002885 asyncResp->res.jsonValue["@odata.id"] = crow::utility::urlFromPieces(
2886 "redfish", "v1", "Chassis", chassisId, "Sensors", sensorId);
Ed Tanousc1d019a2022-08-06 09:36:06 -07002887 std::string sensorType = sensorId.substr(0, index);
2888 std::string sensorName = sensorId.substr(index + 1);
2889 // fan_pwm and fan_tach need special handling
2890 if (sensorType == "fantach" || sensorType == "fanpwm")
2891 {
2892 sensorType.insert(3, 1, '_');
2893 }
2894
Nan Zhoue6bd8462022-06-01 04:35:35 +00002895 BMCWEB_LOG_DEBUG << "Sensor doGet enter";
Nan Zhoue6bd8462022-06-01 04:35:35 +00002896
2897 const std::array<const char*, 1> interfaces = {
2898 "xyz.openbmc_project.Sensor.Value"};
Ed Tanousc1d019a2022-08-06 09:36:06 -07002899 std::string sensorPath =
2900 "/xyz/openbmc_project/sensors/" + sensorType + '/' + sensorName;
Nan Zhoue6bd8462022-06-01 04:35:35 +00002901 // Get a list of all of the sensors that implement Sensor.Value
2902 // and get the path and service name associated with the sensor
2903 crow::connections::systemBus->async_method_call(
Ed Tanousc1d019a2022-08-06 09:36:06 -07002904 [asyncResp, sensorPath,
Nan Zhoue6bd8462022-06-01 04:35:35 +00002905 sensorName](const boost::system::error_code ec,
Ed Tanousc1d019a2022-08-06 09:36:06 -07002906 const ::dbus::utility::MapperGetObject& subtree) {
Nan Zhoue6bd8462022-06-01 04:35:35 +00002907 BMCWEB_LOG_DEBUG << "respHandler1 enter";
2908 if (ec)
2909 {
Ed Tanousc1d019a2022-08-06 09:36:06 -07002910 messages::internalError(asyncResp->res);
Nan Zhoue6bd8462022-06-01 04:35:35 +00002911 BMCWEB_LOG_ERROR << "Sensor getSensorPaths resp_handler: "
2912 << "Dbus error " << ec;
2913 return;
2914 }
Ed Tanousc1d019a2022-08-06 09:36:06 -07002915 getSensorFromDbus(asyncResp, sensorPath, subtree);
Nan Zhoue6bd8462022-06-01 04:35:35 +00002916 BMCWEB_LOG_DEBUG << "respHandler1 exit";
2917 },
2918 "xyz.openbmc_project.ObjectMapper",
2919 "/xyz/openbmc_project/object_mapper",
Ed Tanousc1d019a2022-08-06 09:36:06 -07002920 "xyz.openbmc_project.ObjectMapper", "GetObject", sensorPath,
2921 interfaces);
Nan Zhoue6bd8462022-06-01 04:35:35 +00002922}
2923
Nan Zhoubacb2162022-04-06 11:28:32 -07002924} // namespace sensors
2925
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002926inline void requestRoutesSensorCollection(App& app)
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002927{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002928 BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/Sensors/")
Ed Tanoused398212021-06-09 17:05:54 -07002929 .privileges(redfish::privileges::getSensorCollection)
Ed Tanous002d39b2022-05-31 08:59:27 -07002930 .methods(boost::beast::http::verb::get)(
Nan Zhoude167a62022-06-01 04:47:45 +00002931 std::bind_front(sensors::handleSensorCollectionGet, std::ref(app)));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002932}
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002933
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002934inline void requestRoutesSensor(App& app)
2935{
2936 BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/Sensors/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07002937 .privileges(redfish::privileges::getSensor)
Ed Tanous002d39b2022-05-31 08:59:27 -07002938 .methods(boost::beast::http::verb::get)(
Nan Zhoue6bd8462022-06-01 04:35:35 +00002939 std::bind_front(sensors::handleSensorGet, std::ref(app)));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002940}
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002941
Ed Tanous1abe55e2018-09-05 08:30:59 -07002942} // namespace redfish