blob: 697bf0db0bcf44b816b21a6f1886097b809e3e3d [file] [log] [blame]
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +01001/*
2// Copyright (c) 2018 Intel Corporation
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15*/
16#pragma once
17
Ed Tanous0ec8b832022-03-14 14:56:47 -070018#include "generated/enums/sensor.hpp"
19
John Edward Broadbent7e860f12021-04-08 15:57:16 -070020#include <app.hpp>
Ed Tanous11ba3972022-07-11 09:50:41 -070021#include <boost/algorithm/string/classification.hpp>
Ed Tanous1d7c0052022-08-09 12:32:26 -070022#include <boost/algorithm/string/find.hpp>
23#include <boost/algorithm/string/predicate.hpp>
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +010024#include <boost/algorithm/string/split.hpp>
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +010025#include <boost/range/algorithm/replace_copy_if.hpp>
Ed Tanous1abe55e2018-09-05 08:30:59 -070026#include <dbus_singleton.hpp>
Ed Tanous168e20c2021-12-13 14:39:53 -080027#include <dbus_utility.hpp>
Ed Tanous45ca1b82022-03-25 13:07:27 -070028#include <query.hpp>
Ed Tanoused398212021-06-09 17:05:54 -070029#include <registries/privilege_registry.hpp>
Jonathan Doman1e1e5982021-06-11 09:36:17 -070030#include <sdbusplus/asio/property.hpp>
Krzysztof Grobelny86d89ed2022-08-29 14:49:20 +020031#include <sdbusplus/unpack_properties.hpp>
32#include <utils/dbus_utils.hpp>
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +053033#include <utils/json_utils.hpp>
Nan Zhou928fefb2022-03-28 08:45:00 -070034#include <utils/query_param.hpp>
Gunnar Mills1214b7e2020-06-04 10:11:30 -050035
36#include <cmath>
Nan Zhoufe04d492022-06-22 17:10:41 +000037#include <iterator>
38#include <map>
39#include <set>
Ed Tanousb5a76932020-09-29 16:16:58 -070040#include <utility>
Ed Tanousabf2add2019-01-22 16:40:12 -080041#include <variant>
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +010042
Ed Tanous1abe55e2018-09-05 08:30:59 -070043namespace redfish
44{
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +010045
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +020046namespace sensors
47{
48namespace node
49{
50static constexpr std::string_view power = "Power";
51static constexpr std::string_view sensors = "Sensors";
52static constexpr std::string_view thermal = "Thermal";
53} // namespace node
54
Ed Tanous02da7c52022-02-27 00:09:02 -080055// clang-format off
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +020056namespace dbus
57{
Ed Tanous4ee8e212022-05-28 09:42:51 -070058static auto powerPaths = std::to_array<std::string_view>({
Ed Tanous02da7c52022-02-27 00:09:02 -080059 "/xyz/openbmc_project/sensors/voltage",
60 "/xyz/openbmc_project/sensors/power"
61});
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +000062
Ed Tanous4ee8e212022-05-28 09:42:51 -070063static auto sensorPaths = std::to_array<std::string_view>({
Ed Tanous02da7c52022-02-27 00:09:02 -080064 "/xyz/openbmc_project/sensors/power",
65 "/xyz/openbmc_project/sensors/current",
66 "/xyz/openbmc_project/sensors/airflow",
Ed Tanous4e777662022-08-06 09:39:13 -070067 "/xyz/openbmc_project/sensors/humidity",
George Liue8204932021-02-01 14:42:49 +080068#ifdef BMCWEB_NEW_POWERSUBSYSTEM_THERMALSUBSYSTEM
Ed Tanous02da7c52022-02-27 00:09:02 -080069 "/xyz/openbmc_project/sensors/voltage",
70 "/xyz/openbmc_project/sensors/fan_tach",
71 "/xyz/openbmc_project/sensors/temperature",
72 "/xyz/openbmc_project/sensors/fan_pwm",
73 "/xyz/openbmc_project/sensors/altitude",
74 "/xyz/openbmc_project/sensors/energy",
George Liue8204932021-02-01 14:42:49 +080075#endif
Ed Tanous02da7c52022-02-27 00:09:02 -080076 "/xyz/openbmc_project/sensors/utilization"
77});
78
Ed Tanous4ee8e212022-05-28 09:42:51 -070079static auto thermalPaths = std::to_array<std::string_view>({
Ed Tanous02da7c52022-02-27 00:09:02 -080080 "/xyz/openbmc_project/sensors/fan_tach",
81 "/xyz/openbmc_project/sensors/temperature",
82 "/xyz/openbmc_project/sensors/fan_pwm"
83});
84
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +000085} // namespace dbus
Ed Tanous02da7c52022-02-27 00:09:02 -080086// clang-format on
87
88using sensorPair = std::pair<std::string_view, std::span<std::string_view>>;
89static constexpr std::array<sensorPair, 3> paths = {
90 {{node::power, std::span<std::string_view>(dbus::powerPaths)},
91 {node::sensors, std::span<std::string_view>(dbus::sensorPaths)},
92 {node::thermal, std::span<std::string_view>(dbus::thermalPaths)}}};
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +000093
Ed Tanous0ec8b832022-03-14 14:56:47 -070094inline sensor::ReadingType toReadingType(std::string_view sensorType)
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +000095{
96 if (sensorType == "voltage")
97 {
Ed Tanous0ec8b832022-03-14 14:56:47 -070098 return sensor::ReadingType::Voltage;
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +000099 }
100 if (sensorType == "power")
101 {
Ed Tanous0ec8b832022-03-14 14:56:47 -0700102 return sensor::ReadingType::Power;
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000103 }
104 if (sensorType == "current")
105 {
Ed Tanous0ec8b832022-03-14 14:56:47 -0700106 return sensor::ReadingType::Current;
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000107 }
108 if (sensorType == "fan_tach")
109 {
Ed Tanous0ec8b832022-03-14 14:56:47 -0700110 return sensor::ReadingType::Rotational;
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000111 }
112 if (sensorType == "temperature")
113 {
Ed Tanous0ec8b832022-03-14 14:56:47 -0700114 return sensor::ReadingType::Temperature;
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000115 }
116 if (sensorType == "fan_pwm" || sensorType == "utilization")
117 {
Ed Tanous0ec8b832022-03-14 14:56:47 -0700118 return sensor::ReadingType::Percent;
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000119 }
Gunnar Mills5deabed2022-04-20 13:43:45 -0600120 if (sensorType == "humidity")
121 {
Ed Tanous0ec8b832022-03-14 14:56:47 -0700122 return sensor::ReadingType::Humidity;
Gunnar Mills5deabed2022-04-20 13:43:45 -0600123 }
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000124 if (sensorType == "altitude")
125 {
Ed Tanous0ec8b832022-03-14 14:56:47 -0700126 return sensor::ReadingType::Altitude;
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000127 }
128 if (sensorType == "airflow")
129 {
Ed Tanous0ec8b832022-03-14 14:56:47 -0700130 return sensor::ReadingType::AirFlow;
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000131 }
132 if (sensorType == "energy")
133 {
Ed Tanous0ec8b832022-03-14 14:56:47 -0700134 return sensor::ReadingType::EnergyJoules;
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000135 }
Ed Tanous0ec8b832022-03-14 14:56:47 -0700136 return sensor::ReadingType::Invalid;
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000137}
138
Ed Tanous1d7c0052022-08-09 12:32:26 -0700139inline std::string_view toReadingUnits(std::string_view sensorType)
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000140{
141 if (sensorType == "voltage")
142 {
143 return "V";
144 }
145 if (sensorType == "power")
146 {
147 return "W";
148 }
149 if (sensorType == "current")
150 {
151 return "A";
152 }
153 if (sensorType == "fan_tach")
154 {
155 return "RPM";
156 }
157 if (sensorType == "temperature")
158 {
159 return "Cel";
160 }
Gunnar Mills5deabed2022-04-20 13:43:45 -0600161 if (sensorType == "fan_pwm" || sensorType == "utilization" ||
162 sensorType == "humidity")
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000163 {
164 return "%";
165 }
166 if (sensorType == "altitude")
167 {
168 return "m";
169 }
170 if (sensorType == "airflow")
171 {
172 return "cft_i/min";
173 }
174 if (sensorType == "energy")
175 {
176 return "J";
177 }
178 return "";
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200179}
180} // namespace sensors
181
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100182/**
Kowalski, Kamil588c3f02018-04-03 14:55:27 +0200183 * SensorsAsyncResp
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100184 * Gathers data needed for response processing after async calls are done
185 */
Ed Tanous1abe55e2018-09-05 08:30:59 -0700186class SensorsAsyncResp
187{
188 public:
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200189 using DataCompleteCb = std::function<void(
190 const boost::beast::http::status status,
Nan Zhoufe04d492022-06-22 17:10:41 +0000191 const std::map<std::string, std::string>& uriToDbus)>;
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200192
193 struct SensorData
194 {
195 const std::string name;
196 std::string uri;
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200197 const std::string dbusPath;
198 };
199
Ed Tanous8a592812022-06-04 09:06:59 -0700200 SensorsAsyncResp(const std::shared_ptr<bmcweb::AsyncResp>& asyncRespIn,
zhanghch058d1b46d2021-04-01 11:18:24 +0800201 const std::string& chassisIdIn,
Ed Tanous02da7c52022-02-27 00:09:02 -0800202 std::span<std::string_view> typesIn,
203 std::string_view subNode) :
Ed Tanous8a592812022-06-04 09:06:59 -0700204 asyncResp(asyncRespIn),
Nan Zhou928fefb2022-03-28 08:45:00 -0700205 chassisId(chassisIdIn), types(typesIn), chassisSubNode(subNode),
206 efficientExpand(false)
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500207 {}
Kowalski, Kamil588c3f02018-04-03 14:55:27 +0200208
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200209 // Store extra data about sensor mapping and return it in callback
Ed Tanous8a592812022-06-04 09:06:59 -0700210 SensorsAsyncResp(const std::shared_ptr<bmcweb::AsyncResp>& asyncRespIn,
zhanghch058d1b46d2021-04-01 11:18:24 +0800211 const std::string& chassisIdIn,
Ed Tanous02da7c52022-02-27 00:09:02 -0800212 std::span<std::string_view> typesIn,
213 std::string_view subNode,
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200214 DataCompleteCb&& creationComplete) :
Ed Tanous8a592812022-06-04 09:06:59 -0700215 asyncResp(asyncRespIn),
Nan Zhou928fefb2022-03-28 08:45:00 -0700216 chassisId(chassisIdIn), types(typesIn), chassisSubNode(subNode),
217 efficientExpand(false), metadata{std::vector<SensorData>()},
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200218 dataComplete{std::move(creationComplete)}
219 {}
220
Nan Zhou928fefb2022-03-28 08:45:00 -0700221 // sensor collections expand
Ed Tanous8a592812022-06-04 09:06:59 -0700222 SensorsAsyncResp(const std::shared_ptr<bmcweb::AsyncResp>& asyncRespIn,
Nan Zhou928fefb2022-03-28 08:45:00 -0700223 const std::string& chassisIdIn,
Ed Tanous02da7c52022-02-27 00:09:02 -0800224 const std::span<std::string_view> typesIn,
Ed Tanous8a592812022-06-04 09:06:59 -0700225 const std::string_view& subNode, bool efficientExpandIn) :
226 asyncResp(asyncRespIn),
Nan Zhou928fefb2022-03-28 08:45:00 -0700227 chassisId(chassisIdIn), types(typesIn), chassisSubNode(subNode),
Ed Tanous8a592812022-06-04 09:06:59 -0700228 efficientExpand(efficientExpandIn)
Nan Zhou928fefb2022-03-28 08:45:00 -0700229 {}
230
Ed Tanous1abe55e2018-09-05 08:30:59 -0700231 ~SensorsAsyncResp()
232 {
zhanghch058d1b46d2021-04-01 11:18:24 +0800233 if (asyncResp->res.result() ==
234 boost::beast::http::status::internal_server_error)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700235 {
236 // Reset the json object to clear out any data that made it in
237 // before the error happened todo(ed) handle error condition with
238 // proper code
zhanghch058d1b46d2021-04-01 11:18:24 +0800239 asyncResp->res.jsonValue = nlohmann::json::object();
Ed Tanous1abe55e2018-09-05 08:30:59 -0700240 }
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200241
242 if (dataComplete && metadata)
243 {
Nan Zhoufe04d492022-06-22 17:10:41 +0000244 std::map<std::string, std::string> map;
zhanghch058d1b46d2021-04-01 11:18:24 +0800245 if (asyncResp->res.result() == boost::beast::http::status::ok)
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200246 {
247 for (auto& sensor : *metadata)
248 {
Ed Tanousc1d019a2022-08-06 09:36:06 -0700249 map.emplace(sensor.uri, sensor.dbusPath);
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200250 }
251 }
zhanghch058d1b46d2021-04-01 11:18:24 +0800252 dataComplete(asyncResp->res.result(), map);
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200253 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700254 }
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100255
Ed Tanousecd6a3a2022-01-07 09:18:40 -0800256 SensorsAsyncResp(const SensorsAsyncResp&) = delete;
257 SensorsAsyncResp(SensorsAsyncResp&&) = delete;
258 SensorsAsyncResp& operator=(const SensorsAsyncResp&) = delete;
259 SensorsAsyncResp& operator=(SensorsAsyncResp&&) = delete;
260
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200261 void addMetadata(const nlohmann::json& sensorObject,
Ed Tanousc1d019a2022-08-06 09:36:06 -0700262 const std::string& dbusPath)
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200263 {
264 if (metadata)
265 {
Ed Tanousc1d019a2022-08-06 09:36:06 -0700266 metadata->emplace_back(SensorData{
267 sensorObject["Name"], sensorObject["@odata.id"], dbusPath});
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200268 }
269 }
270
271 void updateUri(const std::string& name, const std::string& uri)
272 {
273 if (metadata)
274 {
275 for (auto& sensor : *metadata)
276 {
277 if (sensor.name == name)
278 {
279 sensor.uri = uri;
280 }
281 }
282 }
283 }
284
zhanghch058d1b46d2021-04-01 11:18:24 +0800285 const std::shared_ptr<bmcweb::AsyncResp> asyncResp;
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200286 const std::string chassisId;
Ed Tanous02da7c52022-02-27 00:09:02 -0800287 const std::span<std::string_view> types;
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200288 const std::string chassisSubNode;
Nan Zhou928fefb2022-03-28 08:45:00 -0700289 const bool efficientExpand;
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200290
291 private:
292 std::optional<std::vector<SensorData>> metadata;
293 DataCompleteCb dataComplete;
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100294};
295
296/**
Anthony Wilsond5005492019-07-31 16:34:17 -0500297 * Possible states for physical inventory leds
298 */
299enum class LedState
300{
301 OFF,
302 ON,
303 BLINK,
304 UNKNOWN
305};
306
307/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500308 * D-Bus inventory item associated with one or more sensors.
309 */
310class InventoryItem
311{
312 public:
Ed Tanous4e23a442022-06-06 09:57:26 -0700313 explicit InventoryItem(const std::string& objPath) : objectPath(objPath)
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500314 {
315 // Set inventory item name to last node of object path
George Liu28aa8de2021-02-01 15:13:30 +0800316 sdbusplus::message::object_path path(objectPath);
317 name = path.filename();
318 if (name.empty())
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500319 {
George Liu28aa8de2021-02-01 15:13:30 +0800320 BMCWEB_LOG_ERROR << "Failed to find '/' in " << objectPath;
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500321 }
322 }
323
324 std::string objectPath;
325 std::string name;
Ed Tanouse05aec52022-01-25 10:28:56 -0800326 bool isPresent = true;
327 bool isFunctional = true;
328 bool isPowerSupply = false;
329 int powerSupplyEfficiencyPercent = -1;
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500330 std::string manufacturer;
331 std::string model;
332 std::string partNumber;
333 std::string serialNumber;
334 std::set<std::string> sensors;
Anthony Wilsond5005492019-07-31 16:34:17 -0500335 std::string ledObjectPath;
Ed Tanouse05aec52022-01-25 10:28:56 -0800336 LedState ledState = LedState::UNKNOWN;
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500337};
338
339/**
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530340 * @brief Get objects with connection necessary for sensors
Kowalski, Kamil588c3f02018-04-03 14:55:27 +0200341 * @param SensorsAsyncResp Pointer to object holding response data
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100342 * @param sensorNames Sensors retrieved from chassis
343 * @param callback Callback for processing gathered connections
344 */
345template <typename Callback>
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530346void getObjectsWithConnection(
Ed Tanous81ce6092020-12-17 16:54:55 +0000347 const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
Nan Zhoufe04d492022-06-22 17:10:41 +0000348 const std::shared_ptr<std::set<std::string>>& sensorNames,
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530349 Callback&& callback)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700350{
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530351 BMCWEB_LOG_DEBUG << "getObjectsWithConnection enter";
Ed Tanous1abe55e2018-09-05 08:30:59 -0700352 const std::string path = "/xyz/openbmc_project/sensors";
353 const std::array<std::string, 1> interfaces = {
354 "xyz.openbmc_project.Sensor.Value"};
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100355
Ed Tanous1abe55e2018-09-05 08:30:59 -0700356 // Response handler for parsing objects subtree
Ed Tanous002d39b2022-05-31 08:59:27 -0700357 auto respHandler =
358 [callback{std::forward<Callback>(callback)}, sensorsAsyncResp,
359 sensorNames](const boost::system::error_code ec,
360 const dbus::utility::MapperGetSubTreeResponse& subtree) {
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530361 BMCWEB_LOG_DEBUG << "getObjectsWithConnection resp_handler enter";
Ed Tanous1abe55e2018-09-05 08:30:59 -0700362 if (ec)
363 {
zhanghch058d1b46d2021-04-01 11:18:24 +0800364 messages::internalError(sensorsAsyncResp->asyncResp->res);
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530365 BMCWEB_LOG_ERROR
366 << "getObjectsWithConnection resp_handler: Dbus error " << ec;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700367 return;
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100368 }
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100369
Ed Tanous1abe55e2018-09-05 08:30:59 -0700370 BMCWEB_LOG_DEBUG << "Found " << subtree.size() << " subtrees";
371
372 // Make unique list of connections only for requested sensor types and
373 // found in the chassis
Nan Zhoufe04d492022-06-22 17:10:41 +0000374 std::set<std::string> connections;
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530375 std::set<std::pair<std::string, std::string>> objectsWithConnection;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700376
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700377 BMCWEB_LOG_DEBUG << "sensorNames list count: " << sensorNames->size();
378 for (const std::string& tsensor : *sensorNames)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700379 {
380 BMCWEB_LOG_DEBUG << "Sensor to find: " << tsensor;
381 }
382
383 for (const std::pair<
384 std::string,
385 std::vector<std::pair<std::string, std::vector<std::string>>>>&
386 object : subtree)
387 {
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700388 if (sensorNames->find(object.first) != sensorNames->end())
Ed Tanous1abe55e2018-09-05 08:30:59 -0700389 {
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700390 for (const std::pair<std::string, std::vector<std::string>>&
391 objData : object.second)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700392 {
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700393 BMCWEB_LOG_DEBUG << "Adding connection: " << objData.first;
394 connections.insert(objData.first);
395 objectsWithConnection.insert(
396 std::make_pair(object.first, objData.first));
Ed Tanous1abe55e2018-09-05 08:30:59 -0700397 }
398 }
399 }
400 BMCWEB_LOG_DEBUG << "Found " << connections.size() << " connections";
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530401 callback(std::move(connections), std::move(objectsWithConnection));
402 BMCWEB_LOG_DEBUG << "getObjectsWithConnection resp_handler exit";
Ed Tanous1abe55e2018-09-05 08:30:59 -0700403 };
Ed Tanous1abe55e2018-09-05 08:30:59 -0700404 // Make call to ObjectMapper to find all sensors objects
405 crow::connections::systemBus->async_method_call(
406 std::move(respHandler), "xyz.openbmc_project.ObjectMapper",
407 "/xyz/openbmc_project/object_mapper",
408 "xyz.openbmc_project.ObjectMapper", "GetSubTree", path, 2, interfaces);
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530409 BMCWEB_LOG_DEBUG << "getObjectsWithConnection exit";
410}
411
412/**
413 * @brief Create connections necessary for sensors
414 * @param SensorsAsyncResp Pointer to object holding response data
415 * @param sensorNames Sensors retrieved from chassis
416 * @param callback Callback for processing gathered connections
417 */
418template <typename Callback>
Nan Zhoufe04d492022-06-22 17:10:41 +0000419void getConnections(std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
420 const std::shared_ptr<std::set<std::string>> sensorNames,
421 Callback&& callback)
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530422{
423 auto objectsWithConnectionCb =
Nan Zhoufe04d492022-06-22 17:10:41 +0000424 [callback](const std::set<std::string>& connections,
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530425 const std::set<std::pair<std::string, std::string>>&
Ed Tanous3174e4d2020-10-07 11:41:22 -0700426 /*objectsWithConnection*/) { callback(connections); };
Ed Tanous81ce6092020-12-17 16:54:55 +0000427 getObjectsWithConnection(sensorsAsyncResp, sensorNames,
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530428 std::move(objectsWithConnectionCb));
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100429}
430
431/**
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700432 * @brief Shrinks the list of sensors for processing
433 * @param SensorsAysncResp The class holding the Redfish response
434 * @param allSensors A list of all the sensors associated to the
435 * chassis element (i.e. baseboard, front panel, etc...)
436 * @param activeSensors A list that is a reduction of the incoming
437 * allSensors list. Eliminate Thermal sensors when a Power request is
438 * made, and eliminate Power sensors when a Thermal request is made.
439 */
Ed Tanous23a21a12020-07-25 04:45:05 +0000440inline void reduceSensorList(
Ed Tanous7f1cc262022-08-09 13:33:57 -0700441 crow::Response& res, std::string_view chassisSubNode,
442 std::span<std::string_view> sensorTypes,
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700443 const std::vector<std::string>* allSensors,
Nan Zhoufe04d492022-06-22 17:10:41 +0000444 const std::shared_ptr<std::set<std::string>>& activeSensors)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700445{
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700446 if ((allSensors == nullptr) || (activeSensors == nullptr))
447 {
Ed Tanous7f1cc262022-08-09 13:33:57 -0700448 messages::resourceNotFound(res, chassisSubNode,
449 chassisSubNode == sensors::node::thermal
450 ? "Temperatures"
451 : "Voltages");
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700452
453 return;
454 }
455 if (allSensors->empty())
456 {
457 // Nothing to do, the activeSensors object is also empty
458 return;
459 }
460
Ed Tanous7f1cc262022-08-09 13:33:57 -0700461 for (std::string_view type : sensorTypes)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700462 {
463 for (const std::string& sensor : *allSensors)
464 {
Ed Tanous11ba3972022-07-11 09:50:41 -0700465 if (sensor.starts_with(type))
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700466 {
467 activeSensors->emplace(sensor);
468 }
469 }
470 }
471}
472
Ed Tanous7f1cc262022-08-09 13:33:57 -0700473/*
474 *Populates the top level collection for a given subnode. Populates
475 *SensorCollection, Power, or Thermal schemas.
476 *
477 * */
478inline void populateChassisNode(nlohmann::json& jsonValue,
479 std::string_view chassisSubNode)
480{
481 if (chassisSubNode == sensors::node::power)
482 {
483 jsonValue["@odata.type"] = "#Power.v1_5_2.Power";
484 }
485 else if (chassisSubNode == sensors::node::thermal)
486 {
487 jsonValue["@odata.type"] = "#Thermal.v1_4_0.Thermal";
488 jsonValue["Fans"] = nlohmann::json::array();
489 jsonValue["Temperatures"] = nlohmann::json::array();
490 }
491 else if (chassisSubNode == sensors::node::sensors)
492 {
493 jsonValue["@odata.type"] = "#SensorCollection.SensorCollection";
494 jsonValue["Description"] = "Collection of Sensors for this Chassis";
495 jsonValue["Members"] = nlohmann::json::array();
496 jsonValue["Members@odata.count"] = 0;
497 }
498
499 if (chassisSubNode != sensors::node::sensors)
500 {
501 jsonValue["Id"] = chassisSubNode;
502 }
503 jsonValue["Name"] = chassisSubNode;
504}
505
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700506/**
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100507 * @brief Retrieves requested chassis sensors and redundancy data from DBus .
Kowalski, Kamil588c3f02018-04-03 14:55:27 +0200508 * @param SensorsAsyncResp Pointer to object holding response data
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100509 * @param callback Callback for next step in gathered sensor processing
510 */
511template <typename Callback>
Ed Tanous7f1cc262022-08-09 13:33:57 -0700512void getChassis(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
513 std::string_view chassisId, std::string_view chassisSubNode,
514 std::span<std::string_view> sensorTypes, Callback&& callback)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700515{
516 BMCWEB_LOG_DEBUG << "getChassis enter";
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500517 const std::array<const char*, 2> interfaces = {
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700518 "xyz.openbmc_project.Inventory.Item.Board",
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500519 "xyz.openbmc_project.Inventory.Item.Chassis"};
Ed Tanous002d39b2022-05-31 08:59:27 -0700520 auto respHandler =
Ed Tanous7f1cc262022-08-09 13:33:57 -0700521 [callback{std::forward<Callback>(callback)}, asyncResp,
522 chassisIdStr{std::string(chassisId)},
523 chassisSubNode{std::string(chassisSubNode)}, sensorTypes](
Ed Tanous002d39b2022-05-31 08:59:27 -0700524 const boost::system::error_code ec,
525 const dbus::utility::MapperGetSubTreePathsResponse& chassisPaths) {
Ed Tanous1abe55e2018-09-05 08:30:59 -0700526 BMCWEB_LOG_DEBUG << "getChassis respHandler enter";
527 if (ec)
528 {
529 BMCWEB_LOG_ERROR << "getChassis respHandler DBUS error: " << ec;
Ed Tanous7f1cc262022-08-09 13:33:57 -0700530 messages::internalError(asyncResp->res);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700531 return;
532 }
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700533 const std::string* chassisPath = nullptr;
534 std::string chassisName;
535 for (const std::string& chassis : chassisPaths)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700536 {
George Liu28aa8de2021-02-01 15:13:30 +0800537 sdbusplus::message::object_path path(chassis);
538 chassisName = path.filename();
539 if (chassisName.empty())
Ed Tanous1abe55e2018-09-05 08:30:59 -0700540 {
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700541 BMCWEB_LOG_ERROR << "Failed to find '/' in " << chassis;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700542 continue;
543 }
Ed Tanous7f1cc262022-08-09 13:33:57 -0700544 if (chassisName == chassisIdStr)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700545 {
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700546 chassisPath = &chassis;
547 break;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700548 }
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700549 }
550 if (chassisPath == nullptr)
551 {
Ed Tanous7f1cc262022-08-09 13:33:57 -0700552 messages::resourceNotFound(asyncResp->res, "Chassis", chassisIdStr);
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700553 return;
554 }
Ed Tanous7f1cc262022-08-09 13:33:57 -0700555 populateChassisNode(asyncResp->res.jsonValue, chassisSubNode);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700556
Ed Tanous7f1cc262022-08-09 13:33:57 -0700557 asyncResp->res.jsonValue["@odata.id"] =
558 "/redfish/v1/Chassis/" + chassisIdStr + "/" + chassisSubNode;
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500559
Shawn McCarney8fb49dd2019-06-12 17:47:00 -0500560 // Get the list of all sensors for this Chassis element
561 std::string sensorPath = *chassisPath + "/all_sensors";
Jonathan Doman1e1e5982021-06-11 09:36:17 -0700562 sdbusplus::asio::getProperty<std::vector<std::string>>(
563 *crow::connections::systemBus, "xyz.openbmc_project.ObjectMapper",
564 sensorPath, "xyz.openbmc_project.Association", "endpoints",
Ed Tanous7f1cc262022-08-09 13:33:57 -0700565 [asyncResp, chassisSubNode, sensorTypes,
Ed Tanousf94c4ec2022-01-06 12:44:41 -0800566 callback{std::forward<const Callback>(callback)}](
Ed Tanous271584a2019-07-09 16:24:22 -0700567 const boost::system::error_code& e,
Jonathan Doman1e1e5982021-06-11 09:36:17 -0700568 const std::vector<std::string>& nodeSensorList) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700569 if (e)
570 {
571 if (e.value() != EBADR)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700572 {
Ed Tanous7f1cc262022-08-09 13:33:57 -0700573 messages::internalError(asyncResp->res);
Ed Tanous002d39b2022-05-31 08:59:27 -0700574 return;
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700575 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700576 }
Nan Zhoufe04d492022-06-22 17:10:41 +0000577 const std::shared_ptr<std::set<std::string>> culledSensorList =
578 std::make_shared<std::set<std::string>>();
Ed Tanous7f1cc262022-08-09 13:33:57 -0700579 reduceSensorList(asyncResp->res, chassisSubNode, sensorTypes,
580 &nodeSensorList, culledSensorList);
581 BMCWEB_LOG_DEBUG << "Finishing with " << culledSensorList->size();
Ed Tanous002d39b2022-05-31 08:59:27 -0700582 callback(culledSensorList);
Jonathan Doman1e1e5982021-06-11 09:36:17 -0700583 });
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100584 };
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100585
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700586 // Get the Chassis Collection
Ed Tanous1abe55e2018-09-05 08:30:59 -0700587 crow::connections::systemBus->async_method_call(
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700588 respHandler, "xyz.openbmc_project.ObjectMapper",
589 "/xyz/openbmc_project/object_mapper",
590 "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths",
Ed Tanous271584a2019-07-09 16:24:22 -0700591 "/xyz/openbmc_project/inventory", 0, interfaces);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700592 BMCWEB_LOG_DEBUG << "getChassis exit";
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100593}
594
595/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500596 * @brief Returns the Redfish State value for the specified inventory item.
597 * @param inventoryItem D-Bus inventory item associated with a sensor.
598 * @return State value for inventory item.
James Feist34dd1792019-05-17 14:10:54 -0700599 */
Ed Tanous23a21a12020-07-25 04:45:05 +0000600inline std::string getState(const InventoryItem* inventoryItem)
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500601{
602 if ((inventoryItem != nullptr) && !(inventoryItem->isPresent))
603 {
604 return "Absent";
605 }
James Feist34dd1792019-05-17 14:10:54 -0700606
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500607 return "Enabled";
608}
609
610/**
611 * @brief Returns the Redfish Health value for the specified sensor.
612 * @param sensorJson Sensor JSON object.
Ed Tanous1d7c0052022-08-09 12:32:26 -0700613 * @param valuesDict Map of all sensor DBus values.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500614 * @param inventoryItem D-Bus inventory item associated with the sensor. Will
615 * be nullptr if no associated inventory item was found.
616 * @return Health value for sensor.
617 */
Ed Tanous1d7c0052022-08-09 12:32:26 -0700618inline std::string getHealth(nlohmann::json& sensorJson,
619 const dbus::utility::DBusPropertiesMap& valuesDict,
620 const InventoryItem* inventoryItem)
James Feist34dd1792019-05-17 14:10:54 -0700621{
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500622 // Get current health value (if any) in the sensor JSON object. Some JSON
623 // objects contain multiple sensors (such as PowerSupplies). We want to set
624 // the overall health to be the most severe of any of the sensors.
625 std::string currentHealth;
626 auto statusIt = sensorJson.find("Status");
627 if (statusIt != sensorJson.end())
628 {
629 auto healthIt = statusIt->find("Health");
630 if (healthIt != statusIt->end())
631 {
632 std::string* health = healthIt->get_ptr<std::string*>();
633 if (health != nullptr)
634 {
635 currentHealth = *health;
636 }
637 }
638 }
639
640 // If current health in JSON object is already Critical, return that. This
641 // should override the sensor health, which might be less severe.
642 if (currentHealth == "Critical")
643 {
644 return "Critical";
645 }
646
Krzysztof Grobelnyc1343bf2022-08-31 13:15:26 +0200647 const bool* criticalAlarmHigh = nullptr;
648 const bool* criticalAlarmLow = nullptr;
649 const bool* warningAlarmHigh = nullptr;
650 const bool* warningAlarmLow = nullptr;
Ed Tanous711ac7a2021-12-20 09:34:41 -0800651
Krzysztof Grobelnyc1343bf2022-08-31 13:15:26 +0200652 const bool success = sdbusplus::unpackPropertiesNoThrow(
653 dbus_utils::UnpackErrorPrinter(), valuesDict, "CriticalAlarmHigh",
654 criticalAlarmHigh, "CriticalAlarmLow", criticalAlarmLow,
655 "WarningAlarmHigh", warningAlarmHigh, "WarningAlarmLow",
656 warningAlarmLow);
657
658 if (success)
James Feist34dd1792019-05-17 14:10:54 -0700659 {
Krzysztof Grobelnyc1343bf2022-08-31 13:15:26 +0200660 // Check if sensor has critical threshold alarm
661 if ((criticalAlarmHigh != nullptr && *criticalAlarmHigh) ||
662 (criticalAlarmLow != nullptr && *criticalAlarmLow))
James Feist34dd1792019-05-17 14:10:54 -0700663 {
Krzysztof Grobelnyc1343bf2022-08-31 13:15:26 +0200664 return "Critical";
James Feist34dd1792019-05-17 14:10:54 -0700665 }
666 }
667
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500668 // Check if associated inventory item is not functional
669 if ((inventoryItem != nullptr) && !(inventoryItem->isFunctional))
670 {
671 return "Critical";
672 }
673
Krzysztof Grobelnyc1343bf2022-08-31 13:15:26 +0200674 // If current health in JSON object is already Warning, return that. This
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500675 // should override the sensor status, which might be less severe.
676 if (currentHealth == "Warning")
677 {
678 return "Warning";
679 }
680
Krzysztof Grobelnyc1343bf2022-08-31 13:15:26 +0200681 if (success)
James Feist34dd1792019-05-17 14:10:54 -0700682 {
Krzysztof Grobelnyc1343bf2022-08-31 13:15:26 +0200683 // Check if sensor has warning threshold alarm
684 if ((warningAlarmHigh != nullptr && *warningAlarmHigh) ||
685 (warningAlarmLow != nullptr && *warningAlarmLow))
James Feist34dd1792019-05-17 14:10:54 -0700686 {
Krzysztof Grobelnyc1343bf2022-08-31 13:15:26 +0200687 return "Warning";
James Feist34dd1792019-05-17 14:10:54 -0700688 }
689 }
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500690
James Feist34dd1792019-05-17 14:10:54 -0700691 return "OK";
692}
693
Ed Tanous23a21a12020-07-25 04:45:05 +0000694inline void setLedState(nlohmann::json& sensorJson,
Anthony Wilsond5005492019-07-31 16:34:17 -0500695 const InventoryItem* inventoryItem)
696{
697 if (inventoryItem != nullptr && !inventoryItem->ledObjectPath.empty())
698 {
699 switch (inventoryItem->ledState)
700 {
701 case LedState::OFF:
702 sensorJson["IndicatorLED"] = "Off";
703 break;
704 case LedState::ON:
705 sensorJson["IndicatorLED"] = "Lit";
706 break;
707 case LedState::BLINK:
708 sensorJson["IndicatorLED"] = "Blinking";
709 break;
Ed Tanous23a21a12020-07-25 04:45:05 +0000710 case LedState::UNKNOWN:
Anthony Wilsond5005492019-07-31 16:34:17 -0500711 break;
712 }
713 }
714}
715
James Feist34dd1792019-05-17 14:10:54 -0700716/**
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100717 * @brief Builds a json sensor representation of a sensor.
718 * @param sensorName The name of the sensor to be built
Gunnar Mills274fad52018-06-13 15:45:36 -0500719 * @param sensorType The type (temperature, fan_tach, etc) of the sensor to
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100720 * build
Ed Tanous1d7c0052022-08-09 12:32:26 -0700721 * @param chassisSubNode The subnode (thermal, sensor, ect) of the sensor
722 * @param propertiesDict A dictionary of the properties to build the sensor
723 * from.
724 * @param sensorJson The json object to fill
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500725 * @param inventoryItem D-Bus inventory item associated with the sensor. Will
726 * be nullptr if no associated inventory item was found.
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100727 */
Ed Tanous1d7c0052022-08-09 12:32:26 -0700728inline void objectPropertiesToJson(
729 std::string_view sensorName, std::string_view sensorType,
730 std::string_view chassisSubNode,
731 const dbus::utility::DBusPropertiesMap& propertiesDict,
Ed Tanous81ce6092020-12-17 16:54:55 +0000732 nlohmann::json& sensorJson, InventoryItem* inventoryItem)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700733{
Ed Tanous1d7c0052022-08-09 12:32:26 -0700734 if (chassisSubNode == sensors::node::sensors)
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500735 {
Ed Tanousc1d019a2022-08-06 09:36:06 -0700736 std::string subNodeEscaped(chassisSubNode);
737 subNodeEscaped.erase(
738 std::remove(subNodeEscaped.begin(), subNodeEscaped.end(), '_'),
739 subNodeEscaped.end());
740
741 // For sensors in SensorCollection we set Id instead of MemberId,
742 // including power sensors.
743 subNodeEscaped += '_';
744 subNodeEscaped += sensorName;
745 sensorJson["Id"] = std::move(subNodeEscaped);
746
Ed Tanous1d7c0052022-08-09 12:32:26 -0700747 std::string sensorNameEs(sensorName);
748 std::replace(sensorNameEs.begin(), sensorNameEs.end(), '_', ' ');
749 sensorJson["Name"] = std::move(sensorNameEs);
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500750 }
751 else if (sensorType != "power")
752 {
753 // Set MemberId and Name for non-power sensors. For PowerSupplies and
754 // PowerControl, those properties have more general values because
755 // multiple sensors can be stored in the same JSON object.
Ed Tanous81ce6092020-12-17 16:54:55 +0000756 sensorJson["MemberId"] = sensorName;
Ed Tanous1d7c0052022-08-09 12:32:26 -0700757 std::string sensorNameEs(sensorName);
758 std::replace(sensorNameEs.begin(), sensorNameEs.end(), '_', ' ');
759 sensorJson["Name"] = std::move(sensorNameEs);
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500760 }
Ed Tanouse742b6c2019-05-03 15:06:53 -0700761
Ed Tanous81ce6092020-12-17 16:54:55 +0000762 sensorJson["Status"]["State"] = getState(inventoryItem);
763 sensorJson["Status"]["Health"] =
Ed Tanous1d7c0052022-08-09 12:32:26 -0700764 getHealth(sensorJson, propertiesDict, inventoryItem);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700765
766 // Parameter to set to override the type we get from dbus, and force it to
767 // int, regardless of what is available. This is used for schemas like fan,
768 // that require integers, not floats.
769 bool forceToInt = false;
770
Anthony Wilson3929aca2019-07-19 15:42:33 -0500771 nlohmann::json::json_pointer unit("/Reading");
Ed Tanous1d7c0052022-08-09 12:32:26 -0700772 if (chassisSubNode == sensors::node::sensors)
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500773 {
Shounak Mitra2a4ba192022-06-01 23:34:12 +0000774 sensorJson["@odata.type"] = "#Sensor.v1_2_0.Sensor";
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000775
Ed Tanous0ec8b832022-03-14 14:56:47 -0700776 sensor::ReadingType readingType = sensors::toReadingType(sensorType);
777 if (readingType == sensor::ReadingType::Invalid)
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500778 {
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000779 BMCWEB_LOG_ERROR << "Redfish cannot map reading type for "
780 << sensorType;
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500781 }
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000782 else
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500783 {
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000784 sensorJson["ReadingType"] = readingType;
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500785 }
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000786
Ed Tanous1d7c0052022-08-09 12:32:26 -0700787 std::string_view readingUnits = sensors::toReadingUnits(sensorType);
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000788 if (readingUnits.empty())
Adrian Ambrożewiczf8ede152020-06-02 13:26:33 +0200789 {
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000790 BMCWEB_LOG_ERROR << "Redfish cannot map reading unit for "
791 << sensorType;
792 }
793 else
794 {
795 sensorJson["ReadingUnits"] = readingUnits;
Adrian Ambrożewiczf8ede152020-06-02 13:26:33 +0200796 }
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500797 }
798 else if (sensorType == "temperature")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700799 {
Anthony Wilson3929aca2019-07-19 15:42:33 -0500800 unit = "/ReadingCelsius"_json_pointer;
Ed Tanous81ce6092020-12-17 16:54:55 +0000801 sensorJson["@odata.type"] = "#Thermal.v1_3_0.Temperature";
Ed Tanous1abe55e2018-09-05 08:30:59 -0700802 // TODO(ed) Documentation says that path should be type fan_tach,
803 // implementation seems to implement fan
804 }
805 else if (sensorType == "fan" || sensorType == "fan_tach")
806 {
Anthony Wilson3929aca2019-07-19 15:42:33 -0500807 unit = "/Reading"_json_pointer;
Ed Tanous81ce6092020-12-17 16:54:55 +0000808 sensorJson["ReadingUnits"] = "RPM";
809 sensorJson["@odata.type"] = "#Thermal.v1_3_0.Fan";
810 setLedState(sensorJson, inventoryItem);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700811 forceToInt = true;
812 }
Ed Tanous6f6d0d32018-10-12 11:16:43 -0700813 else if (sensorType == "fan_pwm")
814 {
Anthony Wilson3929aca2019-07-19 15:42:33 -0500815 unit = "/Reading"_json_pointer;
Ed Tanous81ce6092020-12-17 16:54:55 +0000816 sensorJson["ReadingUnits"] = "Percent";
817 sensorJson["@odata.type"] = "#Thermal.v1_3_0.Fan";
818 setLedState(sensorJson, inventoryItem);
Ed Tanous6f6d0d32018-10-12 11:16:43 -0700819 forceToInt = true;
820 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700821 else if (sensorType == "voltage")
822 {
Anthony Wilson3929aca2019-07-19 15:42:33 -0500823 unit = "/ReadingVolts"_json_pointer;
Ed Tanous81ce6092020-12-17 16:54:55 +0000824 sensorJson["@odata.type"] = "#Power.v1_0_0.Voltage";
Ed Tanous1abe55e2018-09-05 08:30:59 -0700825 }
Ed Tanous2474adf2018-09-05 16:31:16 -0700826 else if (sensorType == "power")
827 {
Ed Tanous1d7c0052022-08-09 12:32:26 -0700828 if (boost::iequals(sensorName, "total_power"))
Eddie James028f7eb2019-05-17 21:24:36 +0000829 {
Ed Tanous81ce6092020-12-17 16:54:55 +0000830 sensorJson["@odata.type"] = "#Power.v1_0_0.PowerControl";
Gunnar Mills7ab06f42019-07-02 13:07:16 -0500831 // Put multiple "sensors" into a single PowerControl, so have
832 // generic names for MemberId and Name. Follows Redfish mockup.
Ed Tanous81ce6092020-12-17 16:54:55 +0000833 sensorJson["MemberId"] = "0";
834 sensorJson["Name"] = "Chassis Power Control";
Anthony Wilson3929aca2019-07-19 15:42:33 -0500835 unit = "/PowerConsumedWatts"_json_pointer;
Eddie James028f7eb2019-05-17 21:24:36 +0000836 }
Ed Tanous1d7c0052022-08-09 12:32:26 -0700837 else if (boost::ifind_first(sensorName, "input").empty())
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700838 {
Anthony Wilson3929aca2019-07-19 15:42:33 -0500839 unit = "/PowerInputWatts"_json_pointer;
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700840 }
841 else
842 {
Anthony Wilson3929aca2019-07-19 15:42:33 -0500843 unit = "/PowerOutputWatts"_json_pointer;
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700844 }
Ed Tanous2474adf2018-09-05 16:31:16 -0700845 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700846 else
847 {
848 BMCWEB_LOG_ERROR << "Redfish cannot map object type for " << sensorName;
849 return;
850 }
851 // Map of dbus interface name, dbus property name and redfish property_name
Anthony Wilson3929aca2019-07-19 15:42:33 -0500852 std::vector<
853 std::tuple<const char*, const char*, nlohmann::json::json_pointer>>
854 properties;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700855 properties.reserve(7);
856
857 properties.emplace_back("xyz.openbmc_project.Sensor.Value", "Value", unit);
Shawn McCarneyde629b62019-03-08 10:42:51 -0600858
Ed Tanous1d7c0052022-08-09 12:32:26 -0700859 if (chassisSubNode == sensors::node::sensors)
Anthony Wilson3929aca2019-07-19 15:42:33 -0500860 {
861 properties.emplace_back(
862 "xyz.openbmc_project.Sensor.Threshold.Warning", "WarningHigh",
863 "/Thresholds/UpperCaution/Reading"_json_pointer);
864 properties.emplace_back(
865 "xyz.openbmc_project.Sensor.Threshold.Warning", "WarningLow",
866 "/Thresholds/LowerCaution/Reading"_json_pointer);
867 properties.emplace_back(
868 "xyz.openbmc_project.Sensor.Threshold.Critical", "CriticalHigh",
869 "/Thresholds/UpperCritical/Reading"_json_pointer);
870 properties.emplace_back(
871 "xyz.openbmc_project.Sensor.Threshold.Critical", "CriticalLow",
872 "/Thresholds/LowerCritical/Reading"_json_pointer);
873 }
874 else if (sensorType != "power")
Shawn McCarneyde629b62019-03-08 10:42:51 -0600875 {
876 properties.emplace_back("xyz.openbmc_project.Sensor.Threshold.Warning",
Anthony Wilson3929aca2019-07-19 15:42:33 -0500877 "WarningHigh",
878 "/UpperThresholdNonCritical"_json_pointer);
Shawn McCarneyde629b62019-03-08 10:42:51 -0600879 properties.emplace_back("xyz.openbmc_project.Sensor.Threshold.Warning",
Anthony Wilson3929aca2019-07-19 15:42:33 -0500880 "WarningLow",
881 "/LowerThresholdNonCritical"_json_pointer);
Shawn McCarneyde629b62019-03-08 10:42:51 -0600882 properties.emplace_back("xyz.openbmc_project.Sensor.Threshold.Critical",
Anthony Wilson3929aca2019-07-19 15:42:33 -0500883 "CriticalHigh",
884 "/UpperThresholdCritical"_json_pointer);
Shawn McCarneyde629b62019-03-08 10:42:51 -0600885 properties.emplace_back("xyz.openbmc_project.Sensor.Threshold.Critical",
Anthony Wilson3929aca2019-07-19 15:42:33 -0500886 "CriticalLow",
887 "/LowerThresholdCritical"_json_pointer);
Shawn McCarneyde629b62019-03-08 10:42:51 -0600888 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700889
Ed Tanous2474adf2018-09-05 16:31:16 -0700890 // TODO Need to get UpperThresholdFatal and LowerThresholdFatal
891
Ed Tanous1d7c0052022-08-09 12:32:26 -0700892 if (chassisSubNode == sensors::node::sensors)
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500893 {
894 properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MinValue",
Anthony Wilson3929aca2019-07-19 15:42:33 -0500895 "/ReadingRangeMin"_json_pointer);
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500896 properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MaxValue",
Anthony Wilson3929aca2019-07-19 15:42:33 -0500897 "/ReadingRangeMax"_json_pointer);
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500898 }
899 else if (sensorType == "temperature")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700900 {
901 properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MinValue",
Anthony Wilson3929aca2019-07-19 15:42:33 -0500902 "/MinReadingRangeTemp"_json_pointer);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700903 properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MaxValue",
Anthony Wilson3929aca2019-07-19 15:42:33 -0500904 "/MaxReadingRangeTemp"_json_pointer);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700905 }
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500906 else if (sensorType != "power")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700907 {
908 properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MinValue",
Anthony Wilson3929aca2019-07-19 15:42:33 -0500909 "/MinReadingRange"_json_pointer);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700910 properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MaxValue",
Anthony Wilson3929aca2019-07-19 15:42:33 -0500911 "/MaxReadingRange"_json_pointer);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700912 }
913
Anthony Wilson3929aca2019-07-19 15:42:33 -0500914 for (const std::tuple<const char*, const char*,
915 nlohmann::json::json_pointer>& p : properties)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700916 {
Ed Tanous1d7c0052022-08-09 12:32:26 -0700917 for (const auto& [valueName, valueVariant] : propertiesDict)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700918 {
Ed Tanous1d7c0052022-08-09 12:32:26 -0700919 if (valueName != std::get<1>(p))
Ed Tanous1abe55e2018-09-05 08:30:59 -0700920 {
Ed Tanous711ac7a2021-12-20 09:34:41 -0800921 continue;
922 }
Ed Tanous1d7c0052022-08-09 12:32:26 -0700923
924 // The property we want to set may be nested json, so use
925 // a json_pointer for easy indexing into the json structure.
926 const nlohmann::json::json_pointer& key = std::get<2>(p);
927
Ed Tanous1d7c0052022-08-09 12:32:26 -0700928 const double* doubleValue = std::get_if<double>(&valueVariant);
Ed Tanous40e4f382022-08-09 18:42:51 -0700929 if (doubleValue == nullptr)
Ed Tanous711ac7a2021-12-20 09:34:41 -0800930 {
Ed Tanous40e4f382022-08-09 18:42:51 -0700931 BMCWEB_LOG_ERROR << "Got value interface that wasn't double";
Ed Tanous1d7c0052022-08-09 12:32:26 -0700932 continue;
933 }
Ed Tanous1d7c0052022-08-09 12:32:26 -0700934 if (forceToInt)
935 {
Ed Tanous40e4f382022-08-09 18:42:51 -0700936 sensorJson[key] = static_cast<int64_t>(*doubleValue);
Ed Tanous1d7c0052022-08-09 12:32:26 -0700937 }
938 else
939 {
Ed Tanous40e4f382022-08-09 18:42:51 -0700940 sensorJson[key] = *doubleValue;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700941 }
942 }
943 }
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100944}
945
Ed Tanous1d7c0052022-08-09 12:32:26 -0700946/**
947 * @brief Builds a json sensor representation of a sensor.
948 * @param sensorName The name of the sensor to be built
949 * @param sensorType The type (temperature, fan_tach, etc) of the sensor to
950 * build
951 * @param chassisSubNode The subnode (thermal, sensor, ect) of the sensor
952 * @param interfacesDict A dictionary of the interfaces and properties of said
953 * interfaces to be built from
954 * @param sensorJson The json object to fill
955 * @param inventoryItem D-Bus inventory item associated with the sensor. Will
956 * be nullptr if no associated inventory item was found.
957 */
958inline void objectInterfacesToJson(
959 const std::string& sensorName, const std::string& sensorType,
960 const std::string& chassisSubNode,
961 const dbus::utility::DBusInteracesMap& interfacesDict,
962 nlohmann::json& sensorJson, InventoryItem* inventoryItem)
963{
964
965 for (const auto& [interface, valuesDict] : interfacesDict)
966 {
967 objectPropertiesToJson(sensorName, sensorType, chassisSubNode,
968 valuesDict, sensorJson, inventoryItem);
969 }
Ed Tanousc1d019a2022-08-06 09:36:06 -0700970 BMCWEB_LOG_DEBUG << "Added sensor " << sensorName;
Ed Tanous1d7c0052022-08-09 12:32:26 -0700971}
972
Ed Tanousb5a76932020-09-29 16:16:58 -0700973inline void populateFanRedundancy(
974 const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp)
James Feist8bd25cc2019-03-15 15:14:00 -0700975{
976 crow::connections::systemBus->async_method_call(
Ed Tanousb9d36b42022-02-26 21:42:46 -0800977 [sensorsAsyncResp](
978 const boost::system::error_code ec,
979 const dbus::utility::MapperGetSubTreeResponse& resp) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700980 if (ec)
981 {
982 return; // don't have to have this interface
983 }
984 for (const std::pair<
985 std::string,
986 std::vector<std::pair<std::string, std::vector<std::string>>>>&
987 pathPair : resp)
988 {
989 const std::string& path = pathPair.first;
990 const std::vector<std::pair<std::string, std::vector<std::string>>>&
991 objDict = pathPair.second;
992 if (objDict.empty())
James Feist8bd25cc2019-03-15 15:14:00 -0700993 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700994 continue; // this should be impossible
James Feist8bd25cc2019-03-15 15:14:00 -0700995 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700996
997 const std::string& owner = objDict.begin()->first;
998 sdbusplus::asio::getProperty<std::vector<std::string>>(
999 *crow::connections::systemBus,
1000 "xyz.openbmc_project.ObjectMapper", path + "/chassis",
1001 "xyz.openbmc_project.Association", "endpoints",
1002 [path, owner,
1003 sensorsAsyncResp](const boost::system::error_code e,
1004 const std::vector<std::string>& endpoints) {
1005 if (e)
James Feist8bd25cc2019-03-15 15:14:00 -07001006 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001007 return; // if they don't have an association we
1008 // can't tell what chassis is
James Feist8bd25cc2019-03-15 15:14:00 -07001009 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001010 auto found =
1011 std::find_if(endpoints.begin(), endpoints.end(),
1012 [sensorsAsyncResp](const std::string& entry) {
1013 return entry.find(sensorsAsyncResp->chassisId) !=
1014 std::string::npos;
1015 });
James Feist8bd25cc2019-03-15 15:14:00 -07001016
Ed Tanous002d39b2022-05-31 08:59:27 -07001017 if (found == endpoints.end())
1018 {
1019 return;
1020 }
Krzysztof Grobelny86d89ed2022-08-29 14:49:20 +02001021 sdbusplus::asio::getAllProperties(
1022 *crow::connections::systemBus, owner, path,
1023 "xyz.openbmc_project.Control.FanRedundancy",
Ed Tanous002d39b2022-05-31 08:59:27 -07001024 [path, sensorsAsyncResp](
1025 const boost::system::error_code& err,
Krzysztof Grobelny86d89ed2022-08-29 14:49:20 +02001026 const dbus::utility::DBusPropertiesMap& ret) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001027 if (err)
1028 {
1029 return; // don't have to have this
1030 // interface
1031 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001032
Krzysztof Grobelny86d89ed2022-08-29 14:49:20 +02001033 const uint8_t* allowedFailures = nullptr;
1034 const std::vector<std::string>* collection = nullptr;
1035 const std::string* status = nullptr;
1036
1037 const bool success = sdbusplus::unpackPropertiesNoThrow(
1038 dbus_utils::UnpackErrorPrinter(), ret,
1039 "AllowedFailures", allowedFailures, "Collection",
1040 collection, "Status", status);
1041
1042 if (!success)
1043 {
1044 messages::internalError(
1045 sensorsAsyncResp->asyncResp->res);
1046 return;
1047 }
1048
1049 if (allowedFailures == nullptr || collection == nullptr ||
1050 status == nullptr)
Ed Tanous002d39b2022-05-31 08:59:27 -07001051 {
1052 BMCWEB_LOG_ERROR << "Invalid redundancy interface";
1053 messages::internalError(
1054 sensorsAsyncResp->asyncResp->res);
1055 return;
1056 }
1057
Ed Tanous002d39b2022-05-31 08:59:27 -07001058 sdbusplus::message::object_path objectPath(path);
1059 std::string name = objectPath.filename();
1060 if (name.empty())
1061 {
1062 // this should be impossible
1063 messages::internalError(
1064 sensorsAsyncResp->asyncResp->res);
1065 return;
1066 }
1067 std::replace(name.begin(), name.end(), '_', ' ');
1068
1069 std::string health;
1070
Ed Tanous11ba3972022-07-11 09:50:41 -07001071 if (status->ends_with("Full"))
Ed Tanous002d39b2022-05-31 08:59:27 -07001072 {
1073 health = "OK";
1074 }
Ed Tanous11ba3972022-07-11 09:50:41 -07001075 else if (status->ends_with("Degraded"))
Ed Tanous002d39b2022-05-31 08:59:27 -07001076 {
1077 health = "Warning";
1078 }
1079 else
1080 {
1081 health = "Critical";
1082 }
1083 nlohmann::json::array_t redfishCollection;
1084 const auto& fanRedfish =
1085 sensorsAsyncResp->asyncResp->res.jsonValue["Fans"];
1086 for (const std::string& item : *collection)
1087 {
Ed Tanous8a592812022-06-04 09:06:59 -07001088 sdbusplus::message::object_path itemPath(item);
1089 std::string itemName = itemPath.filename();
Ed Tanous002d39b2022-05-31 08:59:27 -07001090 if (itemName.empty())
James Feist8bd25cc2019-03-15 15:14:00 -07001091 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001092 continue;
James Feist8bd25cc2019-03-15 15:14:00 -07001093 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001094 /*
1095 todo(ed): merge patch that fixes the names
1096 std::replace(itemName.begin(),
1097 itemName.end(), '_', ' ');*/
1098 auto schemaItem =
1099 std::find_if(fanRedfish.begin(), fanRedfish.end(),
1100 [itemName](const nlohmann::json& fan) {
1101 return fan["MemberId"] == itemName;
James Feist8bd25cc2019-03-15 15:14:00 -07001102 });
Ed Tanous002d39b2022-05-31 08:59:27 -07001103 if (schemaItem != fanRedfish.end())
James Feist8bd25cc2019-03-15 15:14:00 -07001104 {
Ed Tanous8a592812022-06-04 09:06:59 -07001105 nlohmann::json::object_t collectionId;
1106 collectionId["@odata.id"] =
Ed Tanous002d39b2022-05-31 08:59:27 -07001107 (*schemaItem)["@odata.id"];
1108 redfishCollection.emplace_back(
Ed Tanous8a592812022-06-04 09:06:59 -07001109 std::move(collectionId));
Ed Tanous002d39b2022-05-31 08:59:27 -07001110 }
1111 else
1112 {
1113 BMCWEB_LOG_ERROR << "failed to find fan in schema";
1114 messages::internalError(
1115 sensorsAsyncResp->asyncResp->res);
James Feist8bd25cc2019-03-15 15:14:00 -07001116 return;
1117 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001118 }
James Feist8bd25cc2019-03-15 15:14:00 -07001119
Ed Tanous002d39b2022-05-31 08:59:27 -07001120 size_t minNumNeeded =
1121 collection->empty()
1122 ? 0
1123 : collection->size() - *allowedFailures;
1124 nlohmann::json& jResp = sensorsAsyncResp->asyncResp->res
1125 .jsonValue["Redundancy"];
James Feist8bd25cc2019-03-15 15:14:00 -07001126
Ed Tanous002d39b2022-05-31 08:59:27 -07001127 nlohmann::json::object_t redundancy;
1128 redundancy["@odata.id"] =
1129 "/redfish/v1/Chassis/" + sensorsAsyncResp->chassisId +
1130 "/" + sensorsAsyncResp->chassisSubNode +
1131 "#/Redundancy/" + std::to_string(jResp.size());
1132 redundancy["@odata.type"] = "#Redundancy.v1_3_2.Redundancy";
1133 redundancy["MinNumNeeded"] = minNumNeeded;
1134 redundancy["MemberId"] = name;
1135 redundancy["Mode"] = "N+m";
1136 redundancy["Name"] = name;
1137 redundancy["RedundancySet"] = redfishCollection;
1138 redundancy["Status"]["Health"] = health;
1139 redundancy["Status"]["State"] = "Enabled";
James Feist8bd25cc2019-03-15 15:14:00 -07001140
Ed Tanous002d39b2022-05-31 08:59:27 -07001141 jResp.push_back(std::move(redundancy));
Krzysztof Grobelny86d89ed2022-08-29 14:49:20 +02001142 });
Ed Tanous002d39b2022-05-31 08:59:27 -07001143 });
1144 }
James Feist8bd25cc2019-03-15 15:14:00 -07001145 },
1146 "xyz.openbmc_project.ObjectMapper",
1147 "/xyz/openbmc_project/object_mapper",
1148 "xyz.openbmc_project.ObjectMapper", "GetSubTree",
1149 "/xyz/openbmc_project/control", 2,
1150 std::array<const char*, 1>{
1151 "xyz.openbmc_project.Control.FanRedundancy"});
1152}
1153
Ed Tanousb5a76932020-09-29 16:16:58 -07001154inline void
Ed Tanous81ce6092020-12-17 16:54:55 +00001155 sortJSONResponse(const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07001156{
zhanghch058d1b46d2021-04-01 11:18:24 +08001157 nlohmann::json& response = sensorsAsyncResp->asyncResp->res.jsonValue;
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07001158 std::array<std::string, 2> sensorHeaders{"Temperatures", "Fans"};
Ed Tanous81ce6092020-12-17 16:54:55 +00001159 if (sensorsAsyncResp->chassisSubNode == sensors::node::power)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07001160 {
1161 sensorHeaders = {"Voltages", "PowerSupplies"};
1162 }
1163 for (const std::string& sensorGroup : sensorHeaders)
1164 {
1165 nlohmann::json::iterator entry = response.find(sensorGroup);
1166 if (entry != response.end())
1167 {
1168 std::sort(entry->begin(), entry->end(),
Ed Tanous02cad962022-06-30 16:50:15 -07001169 [](const nlohmann::json& c1, const nlohmann::json& c2) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001170 return c1["Name"] < c2["Name"];
1171 });
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07001172
1173 // add the index counts to the end of each entry
1174 size_t count = 0;
1175 for (nlohmann::json& sensorJson : *entry)
1176 {
1177 nlohmann::json::iterator odata = sensorJson.find("@odata.id");
1178 if (odata == sensorJson.end())
1179 {
1180 continue;
1181 }
1182 std::string* value = odata->get_ptr<std::string*>();
1183 if (value != nullptr)
1184 {
1185 *value += std::to_string(count);
1186 count++;
Ed Tanous81ce6092020-12-17 16:54:55 +00001187 sensorsAsyncResp->updateUri(sensorJson["Name"], *value);
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07001188 }
1189 }
1190 }
1191 }
1192}
1193
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +01001194/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001195 * @brief Finds the inventory item with the specified object path.
1196 * @param inventoryItems D-Bus inventory items associated with sensors.
1197 * @param invItemObjPath D-Bus object path of inventory item.
1198 * @return Inventory item within vector, or nullptr if no match found.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001199 */
Ed Tanous23a21a12020-07-25 04:45:05 +00001200inline InventoryItem* findInventoryItem(
Ed Tanousb5a76932020-09-29 16:16:58 -07001201 const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems,
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001202 const std::string& invItemObjPath)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001203{
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001204 for (InventoryItem& inventoryItem : *inventoryItems)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001205 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001206 if (inventoryItem.objectPath == invItemObjPath)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001207 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001208 return &inventoryItem;
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001209 }
1210 }
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001211 return nullptr;
1212}
1213
1214/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001215 * @brief Finds the inventory item associated with the specified sensor.
1216 * @param inventoryItems D-Bus inventory items associated with sensors.
1217 * @param sensorObjPath D-Bus object path of sensor.
1218 * @return Inventory item within vector, or nullptr if no match found.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001219 */
Ed Tanous23a21a12020-07-25 04:45:05 +00001220inline InventoryItem* findInventoryItemForSensor(
Ed Tanousb5a76932020-09-29 16:16:58 -07001221 const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems,
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001222 const std::string& sensorObjPath)
1223{
1224 for (InventoryItem& inventoryItem : *inventoryItems)
1225 {
1226 if (inventoryItem.sensors.count(sensorObjPath) > 0)
1227 {
1228 return &inventoryItem;
1229 }
1230 }
1231 return nullptr;
1232}
1233
1234/**
Anthony Wilsond5005492019-07-31 16:34:17 -05001235 * @brief Finds the inventory item associated with the specified led path.
1236 * @param inventoryItems D-Bus inventory items associated with sensors.
1237 * @param ledObjPath D-Bus object path of led.
1238 * @return Inventory item within vector, or nullptr if no match found.
1239 */
1240inline InventoryItem*
1241 findInventoryItemForLed(std::vector<InventoryItem>& inventoryItems,
1242 const std::string& ledObjPath)
1243{
1244 for (InventoryItem& inventoryItem : inventoryItems)
1245 {
1246 if (inventoryItem.ledObjectPath == ledObjPath)
1247 {
1248 return &inventoryItem;
1249 }
1250 }
1251 return nullptr;
1252}
1253
1254/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001255 * @brief Adds inventory item and associated sensor to specified vector.
1256 *
1257 * Adds a new InventoryItem to the vector if necessary. Searches for an
1258 * existing InventoryItem with the specified object path. If not found, one is
1259 * added to the vector.
1260 *
1261 * Next, the specified sensor is added to the set of sensors associated with the
1262 * InventoryItem.
1263 *
1264 * @param inventoryItems D-Bus inventory items associated with sensors.
1265 * @param invItemObjPath D-Bus object path of inventory item.
1266 * @param sensorObjPath D-Bus object path of sensor
1267 */
Ed Tanousb5a76932020-09-29 16:16:58 -07001268inline void addInventoryItem(
1269 const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems,
1270 const std::string& invItemObjPath, const std::string& sensorObjPath)
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001271{
1272 // Look for inventory item in vector
1273 InventoryItem* inventoryItem =
1274 findInventoryItem(inventoryItems, invItemObjPath);
1275
1276 // If inventory item doesn't exist in vector, add it
1277 if (inventoryItem == nullptr)
1278 {
1279 inventoryItems->emplace_back(invItemObjPath);
1280 inventoryItem = &(inventoryItems->back());
1281 }
1282
1283 // Add sensor to set of sensors associated with inventory item
1284 inventoryItem->sensors.emplace(sensorObjPath);
1285}
1286
1287/**
1288 * @brief Stores D-Bus data in the specified inventory item.
1289 *
1290 * Finds D-Bus data in the specified map of interfaces. Stores the data in the
1291 * specified InventoryItem.
1292 *
1293 * This data is later used to provide sensor property values in the JSON
1294 * response.
1295 *
1296 * @param inventoryItem Inventory item where data will be stored.
1297 * @param interfacesDict Map containing D-Bus interfaces and their properties
1298 * for the specified inventory item.
1299 */
Ed Tanous23a21a12020-07-25 04:45:05 +00001300inline void storeInventoryItemData(
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001301 InventoryItem& inventoryItem,
Ed Tanous711ac7a2021-12-20 09:34:41 -08001302 const dbus::utility::DBusInteracesMap& interfacesDict)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001303{
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001304 // Get properties from Inventory.Item interface
Ed Tanous711ac7a2021-12-20 09:34:41 -08001305
Ed Tanous9eb808c2022-01-25 10:19:23 -08001306 for (const auto& [interface, values] : interfacesDict)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001307 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001308 if (interface == "xyz.openbmc_project.Inventory.Item")
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001309 {
Ed Tanous9eb808c2022-01-25 10:19:23 -08001310 for (const auto& [name, dbusValue] : values)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001311 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001312 if (name == "Present")
1313 {
1314 const bool* value = std::get_if<bool>(&dbusValue);
1315 if (value != nullptr)
1316 {
1317 inventoryItem.isPresent = *value;
1318 }
1319 }
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001320 }
1321 }
Ed Tanous711ac7a2021-12-20 09:34:41 -08001322 // Check if Inventory.Item.PowerSupply interface is present
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001323
Ed Tanous711ac7a2021-12-20 09:34:41 -08001324 if (interface == "xyz.openbmc_project.Inventory.Item.PowerSupply")
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001325 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001326 inventoryItem.isPowerSupply = true;
1327 }
1328
1329 // Get properties from Inventory.Decorator.Asset interface
1330 if (interface == "xyz.openbmc_project.Inventory.Decorator.Asset")
1331 {
Ed Tanous9eb808c2022-01-25 10:19:23 -08001332 for (const auto& [name, dbusValue] : values)
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001333 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001334 if (name == "Manufacturer")
1335 {
1336 const std::string* value =
1337 std::get_if<std::string>(&dbusValue);
1338 if (value != nullptr)
1339 {
1340 inventoryItem.manufacturer = *value;
1341 }
1342 }
1343 if (name == "Model")
1344 {
1345 const std::string* value =
1346 std::get_if<std::string>(&dbusValue);
1347 if (value != nullptr)
1348 {
1349 inventoryItem.model = *value;
1350 }
1351 }
1352 if (name == "SerialNumber")
1353 {
1354 const std::string* value =
1355 std::get_if<std::string>(&dbusValue);
1356 if (value != nullptr)
1357 {
1358 inventoryItem.serialNumber = *value;
1359 }
1360 }
1361 if (name == "PartNumber")
1362 {
1363 const std::string* value =
1364 std::get_if<std::string>(&dbusValue);
1365 if (value != nullptr)
1366 {
1367 inventoryItem.partNumber = *value;
1368 }
1369 }
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001370 }
1371 }
1372
Ed Tanous711ac7a2021-12-20 09:34:41 -08001373 if (interface ==
1374 "xyz.openbmc_project.State.Decorator.OperationalStatus")
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001375 {
Ed Tanous9eb808c2022-01-25 10:19:23 -08001376 for (const auto& [name, dbusValue] : values)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001377 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001378 if (name == "Functional")
1379 {
1380 const bool* value = std::get_if<bool>(&dbusValue);
1381 if (value != nullptr)
1382 {
1383 inventoryItem.isFunctional = *value;
1384 }
1385 }
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001386 }
1387 }
1388 }
1389}
1390
1391/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001392 * @brief Gets D-Bus data for inventory items associated with sensors.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001393 *
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001394 * Uses the specified connections (services) to obtain D-Bus data for inventory
1395 * items associated with sensors. Stores the resulting data in the
1396 * inventoryItems vector.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001397 *
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001398 * This data is later used to provide sensor property values in the JSON
1399 * response.
1400 *
1401 * Finds the inventory item data asynchronously. Invokes callback when data has
1402 * been obtained.
1403 *
1404 * The callback must have the following signature:
1405 * @code
Anthony Wilsond5005492019-07-31 16:34:17 -05001406 * callback(void)
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001407 * @endcode
1408 *
1409 * This function is called recursively, obtaining data asynchronously from one
1410 * connection in each call. This ensures the callback is not invoked until the
1411 * last asynchronous function has completed.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001412 *
1413 * @param sensorsAsyncResp Pointer to object holding response data.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001414 * @param inventoryItems D-Bus inventory items associated with sensors.
1415 * @param invConnections Connections that provide data for the inventory items.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001416 * implements ObjectManager.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001417 * @param callback Callback to invoke when inventory data has been obtained.
1418 * @param invConnectionsIndex Current index in invConnections. Only specified
1419 * in recursive calls to this function.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001420 */
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001421template <typename Callback>
1422static void getInventoryItemsData(
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001423 std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001424 std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
Ed Tanousd0090732022-10-04 17:22:56 -07001425 std::shared_ptr<std::set<std::string>> invConnections, Callback&& callback,
1426 size_t invConnectionsIndex = 0)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001427{
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001428 BMCWEB_LOG_DEBUG << "getInventoryItemsData enter";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001429
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001430 // If no more connections left, call callback
1431 if (invConnectionsIndex >= invConnections->size())
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001432 {
Anthony Wilsond5005492019-07-31 16:34:17 -05001433 callback();
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001434 BMCWEB_LOG_DEBUG << "getInventoryItemsData exit";
1435 return;
1436 }
1437
1438 // Get inventory item data from current connection
Nan Zhoufe04d492022-06-22 17:10:41 +00001439 auto it = invConnections->begin();
1440 std::advance(it, invConnectionsIndex);
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001441 if (it != invConnections->end())
1442 {
1443 const std::string& invConnection = *it;
1444
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001445 // Response handler for GetManagedObjects
Ed Tanousd0090732022-10-04 17:22:56 -07001446 auto respHandler = [sensorsAsyncResp, inventoryItems, invConnections,
1447 callback{std::forward<Callback>(callback)},
1448 invConnectionsIndex](
1449 const boost::system::error_code ec,
1450 const dbus::utility::ManagedObjectType& resp) {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001451 BMCWEB_LOG_DEBUG << "getInventoryItemsData respHandler enter";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001452 if (ec)
1453 {
1454 BMCWEB_LOG_ERROR
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001455 << "getInventoryItemsData respHandler DBus error " << ec;
zhanghch058d1b46d2021-04-01 11:18:24 +08001456 messages::internalError(sensorsAsyncResp->asyncResp->res);
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001457 return;
1458 }
1459
1460 // Loop through returned object paths
1461 for (const auto& objDictEntry : resp)
1462 {
1463 const std::string& objPath =
1464 static_cast<const std::string&>(objDictEntry.first);
1465
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001466 // If this object path is one of the specified inventory items
1467 InventoryItem* inventoryItem =
1468 findInventoryItem(inventoryItems, objPath);
1469 if (inventoryItem != nullptr)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001470 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001471 // Store inventory data in InventoryItem
1472 storeInventoryItemData(*inventoryItem, objDictEntry.second);
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001473 }
1474 }
1475
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001476 // Recurse to get inventory item data from next connection
1477 getInventoryItemsData(sensorsAsyncResp, inventoryItems,
Ed Tanousd0090732022-10-04 17:22:56 -07001478 invConnections, std::move(callback),
1479 invConnectionsIndex + 1);
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001480
1481 BMCWEB_LOG_DEBUG << "getInventoryItemsData respHandler exit";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001482 };
1483
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001484 // Get all object paths and their interfaces for current connection
1485 crow::connections::systemBus->async_method_call(
Ed Tanousd0090732022-10-04 17:22:56 -07001486 std::move(respHandler), invConnection,
1487 "/xyz/openbmc_project/sensors",
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001488 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
1489 }
1490
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001491 BMCWEB_LOG_DEBUG << "getInventoryItemsData exit";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001492}
1493
1494/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001495 * @brief Gets connections that provide D-Bus data for inventory items.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001496 *
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001497 * Gets the D-Bus connections (services) that provide data for the inventory
1498 * items that are associated with sensors.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001499 *
1500 * Finds the connections asynchronously. Invokes callback when information has
1501 * been obtained.
1502 *
1503 * The callback must have the following signature:
1504 * @code
Nan Zhoufe04d492022-06-22 17:10:41 +00001505 * callback(std::shared_ptr<std::set<std::string>> invConnections)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001506 * @endcode
1507 *
1508 * @param sensorsAsyncResp Pointer to object holding response data.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001509 * @param inventoryItems D-Bus inventory items associated with sensors.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001510 * @param callback Callback to invoke when connections have been obtained.
1511 */
1512template <typename Callback>
1513static void getInventoryItemsConnections(
Ed Tanousb5a76932020-09-29 16:16:58 -07001514 const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
1515 const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems,
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001516 Callback&& callback)
1517{
1518 BMCWEB_LOG_DEBUG << "getInventoryItemsConnections enter";
1519
1520 const std::string path = "/xyz/openbmc_project/inventory";
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001521 const std::array<std::string, 4> interfaces = {
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001522 "xyz.openbmc_project.Inventory.Item",
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001523 "xyz.openbmc_project.Inventory.Item.PowerSupply",
1524 "xyz.openbmc_project.Inventory.Decorator.Asset",
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001525 "xyz.openbmc_project.State.Decorator.OperationalStatus"};
1526
1527 // Response handler for parsing output from GetSubTree
Ed Tanous002d39b2022-05-31 08:59:27 -07001528 auto respHandler =
1529 [callback{std::forward<Callback>(callback)}, sensorsAsyncResp,
1530 inventoryItems](
1531 const boost::system::error_code ec,
1532 const dbus::utility::MapperGetSubTreeResponse& subtree) {
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001533 BMCWEB_LOG_DEBUG << "getInventoryItemsConnections respHandler enter";
1534 if (ec)
1535 {
zhanghch058d1b46d2021-04-01 11:18:24 +08001536 messages::internalError(sensorsAsyncResp->asyncResp->res);
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001537 BMCWEB_LOG_ERROR
1538 << "getInventoryItemsConnections respHandler DBus error " << ec;
1539 return;
1540 }
1541
1542 // Make unique list of connections for desired inventory items
Nan Zhoufe04d492022-06-22 17:10:41 +00001543 std::shared_ptr<std::set<std::string>> invConnections =
1544 std::make_shared<std::set<std::string>>();
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001545
1546 // Loop through objects from GetSubTree
1547 for (const std::pair<
1548 std::string,
1549 std::vector<std::pair<std::string, std::vector<std::string>>>>&
1550 object : subtree)
1551 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001552 // Check if object path is one of the specified inventory items
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001553 const std::string& objPath = object.first;
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001554 if (findInventoryItem(inventoryItems, objPath) != nullptr)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001555 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001556 // Store all connections to inventory item
1557 for (const std::pair<std::string, std::vector<std::string>>&
1558 objData : object.second)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001559 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001560 const std::string& invConnection = objData.first;
1561 invConnections->insert(invConnection);
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001562 }
1563 }
1564 }
Anthony Wilsond5005492019-07-31 16:34:17 -05001565
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001566 callback(invConnections);
1567 BMCWEB_LOG_DEBUG << "getInventoryItemsConnections respHandler exit";
1568 };
1569
1570 // Make call to ObjectMapper to find all inventory items
1571 crow::connections::systemBus->async_method_call(
1572 std::move(respHandler), "xyz.openbmc_project.ObjectMapper",
1573 "/xyz/openbmc_project/object_mapper",
1574 "xyz.openbmc_project.ObjectMapper", "GetSubTree", path, 0, interfaces);
1575 BMCWEB_LOG_DEBUG << "getInventoryItemsConnections exit";
1576}
1577
1578/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001579 * @brief Gets associations from sensors to inventory items.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001580 *
1581 * Looks for ObjectMapper associations from the specified sensors to related
Anthony Wilsond5005492019-07-31 16:34:17 -05001582 * inventory items. Then finds the associations from those inventory items to
1583 * their LEDs, if any.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001584 *
1585 * Finds the inventory items asynchronously. Invokes callback when information
1586 * has been obtained.
1587 *
1588 * The callback must have the following signature:
1589 * @code
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001590 * callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001591 * @endcode
1592 *
1593 * @param sensorsAsyncResp Pointer to object holding response data.
1594 * @param sensorNames All sensors within the current chassis.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001595 * implements ObjectManager.
1596 * @param callback Callback to invoke when inventory items have been obtained.
1597 */
1598template <typename Callback>
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001599static void getInventoryItemAssociations(
Ed Tanousb5a76932020-09-29 16:16:58 -07001600 const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
Nan Zhoufe04d492022-06-22 17:10:41 +00001601 const std::shared_ptr<std::set<std::string>>& sensorNames,
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001602 Callback&& callback)
1603{
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001604 BMCWEB_LOG_DEBUG << "getInventoryItemAssociations enter";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001605
1606 // Response handler for GetManagedObjects
Ed Tanous02cad962022-06-30 16:50:15 -07001607 auto respHandler =
1608 [callback{std::forward<Callback>(callback)}, sensorsAsyncResp,
1609 sensorNames](const boost::system::error_code ec,
1610 const dbus::utility::ManagedObjectType& resp) {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001611 BMCWEB_LOG_DEBUG << "getInventoryItemAssociations respHandler enter";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001612 if (ec)
1613 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001614 BMCWEB_LOG_ERROR
1615 << "getInventoryItemAssociations respHandler DBus error " << ec;
zhanghch058d1b46d2021-04-01 11:18:24 +08001616 messages::internalError(sensorsAsyncResp->asyncResp->res);
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001617 return;
1618 }
1619
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001620 // Create vector to hold list of inventory items
1621 std::shared_ptr<std::vector<InventoryItem>> inventoryItems =
1622 std::make_shared<std::vector<InventoryItem>>();
1623
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001624 // Loop through returned object paths
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001625 std::string sensorAssocPath;
1626 sensorAssocPath.reserve(128); // avoid memory allocations
1627 for (const auto& objDictEntry : resp)
1628 {
1629 const std::string& objPath =
1630 static_cast<const std::string&>(objDictEntry.first);
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001631
1632 // If path is inventory association for one of the specified sensors
1633 for (const std::string& sensorName : *sensorNames)
1634 {
1635 sensorAssocPath = sensorName;
1636 sensorAssocPath += "/inventory";
1637 if (objPath == sensorAssocPath)
1638 {
1639 // Get Association interface for object path
Ed Tanous711ac7a2021-12-20 09:34:41 -08001640 for (const auto& [interface, values] : objDictEntry.second)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001641 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001642 if (interface == "xyz.openbmc_project.Association")
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001643 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001644 for (const auto& [valueName, value] : values)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001645 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001646 if (valueName == "endpoints")
1647 {
1648 const std::vector<std::string>* endpoints =
1649 std::get_if<std::vector<std::string>>(
1650 &value);
1651 if ((endpoints != nullptr) &&
1652 !endpoints->empty())
1653 {
1654 // Add inventory item to vector
1655 const std::string& invItemPath =
1656 endpoints->front();
1657 addInventoryItem(inventoryItems,
1658 invItemPath,
1659 sensorName);
1660 }
1661 }
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001662 }
1663 }
1664 }
1665 break;
1666 }
1667 }
1668 }
1669
Anthony Wilsond5005492019-07-31 16:34:17 -05001670 // Now loop through the returned object paths again, this time to
1671 // find the leds associated with the inventory items we just found
1672 std::string inventoryAssocPath;
1673 inventoryAssocPath.reserve(128); // avoid memory allocations
1674 for (const auto& objDictEntry : resp)
1675 {
1676 const std::string& objPath =
1677 static_cast<const std::string&>(objDictEntry.first);
Anthony Wilsond5005492019-07-31 16:34:17 -05001678
1679 for (InventoryItem& inventoryItem : *inventoryItems)
1680 {
1681 inventoryAssocPath = inventoryItem.objectPath;
1682 inventoryAssocPath += "/leds";
1683 if (objPath == inventoryAssocPath)
1684 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001685 for (const auto& [interface, values] : objDictEntry.second)
Anthony Wilsond5005492019-07-31 16:34:17 -05001686 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001687 if (interface == "xyz.openbmc_project.Association")
Anthony Wilsond5005492019-07-31 16:34:17 -05001688 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001689 for (const auto& [valueName, value] : values)
Anthony Wilsond5005492019-07-31 16:34:17 -05001690 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001691 if (valueName == "endpoints")
1692 {
1693 const std::vector<std::string>* endpoints =
1694 std::get_if<std::vector<std::string>>(
1695 &value);
1696 if ((endpoints != nullptr) &&
1697 !endpoints->empty())
1698 {
1699 // Add inventory item to vector
1700 // Store LED path in inventory item
1701 const std::string& ledPath =
1702 endpoints->front();
1703 inventoryItem.ledObjectPath = ledPath;
1704 }
1705 }
Anthony Wilsond5005492019-07-31 16:34:17 -05001706 }
1707 }
1708 }
Ed Tanous711ac7a2021-12-20 09:34:41 -08001709
Anthony Wilsond5005492019-07-31 16:34:17 -05001710 break;
1711 }
1712 }
1713 }
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001714 callback(inventoryItems);
1715 BMCWEB_LOG_DEBUG << "getInventoryItemAssociations respHandler exit";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001716 };
1717
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001718 // Call GetManagedObjects on the ObjectMapper to get all associations
1719 crow::connections::systemBus->async_method_call(
Ed Tanousd0090732022-10-04 17:22:56 -07001720 std::move(respHandler), "xyz.openbmc_project.ObjectMapper", "/",
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001721 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
1722
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001723 BMCWEB_LOG_DEBUG << "getInventoryItemAssociations exit";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001724}
1725
1726/**
Anthony Wilsond5005492019-07-31 16:34:17 -05001727 * @brief Gets D-Bus data for inventory item leds associated with sensors.
1728 *
1729 * Uses the specified connections (services) to obtain D-Bus data for inventory
1730 * item leds associated with sensors. Stores the resulting data in the
1731 * inventoryItems vector.
1732 *
1733 * This data is later used to provide sensor property values in the JSON
1734 * response.
1735 *
1736 * Finds the inventory item led data asynchronously. Invokes callback when data
1737 * has been obtained.
1738 *
1739 * The callback must have the following signature:
1740 * @code
Gunnar Mills42cbe532019-08-15 15:26:54 -05001741 * callback()
Anthony Wilsond5005492019-07-31 16:34:17 -05001742 * @endcode
1743 *
1744 * This function is called recursively, obtaining data asynchronously from one
1745 * connection in each call. This ensures the callback is not invoked until the
1746 * last asynchronous function has completed.
1747 *
1748 * @param sensorsAsyncResp Pointer to object holding response data.
1749 * @param inventoryItems D-Bus inventory items associated with sensors.
1750 * @param ledConnections Connections that provide data for the inventory leds.
1751 * @param callback Callback to invoke when inventory data has been obtained.
1752 * @param ledConnectionsIndex Current index in ledConnections. Only specified
1753 * in recursive calls to this function.
1754 */
1755template <typename Callback>
1756void getInventoryLedData(
1757 std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
1758 std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
Nan Zhoufe04d492022-06-22 17:10:41 +00001759 std::shared_ptr<std::map<std::string, std::string>> ledConnections,
Anthony Wilsond5005492019-07-31 16:34:17 -05001760 Callback&& callback, size_t ledConnectionsIndex = 0)
1761{
1762 BMCWEB_LOG_DEBUG << "getInventoryLedData enter";
1763
1764 // If no more connections left, call callback
1765 if (ledConnectionsIndex >= ledConnections->size())
1766 {
Gunnar Mills42cbe532019-08-15 15:26:54 -05001767 callback();
Anthony Wilsond5005492019-07-31 16:34:17 -05001768 BMCWEB_LOG_DEBUG << "getInventoryLedData exit";
1769 return;
1770 }
1771
1772 // Get inventory item data from current connection
Nan Zhoufe04d492022-06-22 17:10:41 +00001773 auto it = ledConnections->begin();
1774 std::advance(it, ledConnectionsIndex);
Anthony Wilsond5005492019-07-31 16:34:17 -05001775 if (it != ledConnections->end())
1776 {
1777 const std::string& ledPath = (*it).first;
1778 const std::string& ledConnection = (*it).second;
1779 // Response handler for Get State property
Jonathan Doman1e1e5982021-06-11 09:36:17 -07001780 auto respHandler =
1781 [sensorsAsyncResp, inventoryItems, ledConnections, ledPath,
Ed Tanousf94c4ec2022-01-06 12:44:41 -08001782 callback{std::forward<Callback>(callback)}, ledConnectionsIndex](
Jonathan Doman1e1e5982021-06-11 09:36:17 -07001783 const boost::system::error_code ec, const std::string& state) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001784 BMCWEB_LOG_DEBUG << "getInventoryLedData respHandler enter";
1785 if (ec)
1786 {
1787 BMCWEB_LOG_ERROR
1788 << "getInventoryLedData respHandler DBus error " << ec;
1789 messages::internalError(sensorsAsyncResp->asyncResp->res);
1790 return;
1791 }
1792
1793 BMCWEB_LOG_DEBUG << "Led state: " << state;
1794 // Find inventory item with this LED object path
1795 InventoryItem* inventoryItem =
1796 findInventoryItemForLed(*inventoryItems, ledPath);
1797 if (inventoryItem != nullptr)
1798 {
1799 // Store LED state in InventoryItem
Ed Tanous11ba3972022-07-11 09:50:41 -07001800 if (state.ends_with("On"))
Jonathan Doman1e1e5982021-06-11 09:36:17 -07001801 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001802 inventoryItem->ledState = LedState::ON;
Jonathan Doman1e1e5982021-06-11 09:36:17 -07001803 }
Ed Tanous11ba3972022-07-11 09:50:41 -07001804 else if (state.ends_with("Blink"))
Anthony Wilsond5005492019-07-31 16:34:17 -05001805 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001806 inventoryItem->ledState = LedState::BLINK;
Anthony Wilsond5005492019-07-31 16:34:17 -05001807 }
Ed Tanous11ba3972022-07-11 09:50:41 -07001808 else if (state.ends_with("Off"))
Ed Tanous002d39b2022-05-31 08:59:27 -07001809 {
1810 inventoryItem->ledState = LedState::OFF;
1811 }
1812 else
1813 {
1814 inventoryItem->ledState = LedState::UNKNOWN;
1815 }
1816 }
Anthony Wilsond5005492019-07-31 16:34:17 -05001817
Ed Tanous002d39b2022-05-31 08:59:27 -07001818 // Recurse to get LED data from next connection
1819 getInventoryLedData(sensorsAsyncResp, inventoryItems,
1820 ledConnections, std::move(callback),
1821 ledConnectionsIndex + 1);
Anthony Wilsond5005492019-07-31 16:34:17 -05001822
Ed Tanous002d39b2022-05-31 08:59:27 -07001823 BMCWEB_LOG_DEBUG << "getInventoryLedData respHandler exit";
1824 };
Anthony Wilsond5005492019-07-31 16:34:17 -05001825
1826 // Get the State property for the current LED
Jonathan Doman1e1e5982021-06-11 09:36:17 -07001827 sdbusplus::asio::getProperty<std::string>(
1828 *crow::connections::systemBus, ledConnection, ledPath,
1829 "xyz.openbmc_project.Led.Physical", "State",
1830 std::move(respHandler));
Anthony Wilsond5005492019-07-31 16:34:17 -05001831 }
1832
1833 BMCWEB_LOG_DEBUG << "getInventoryLedData exit";
1834}
1835
1836/**
1837 * @brief Gets LED data for LEDs associated with given inventory items.
1838 *
1839 * Gets the D-Bus connections (services) that provide LED data for the LEDs
1840 * associated with the specified inventory items. Then gets the LED data from
1841 * each connection and stores it in the inventory item.
1842 *
1843 * This data is later used to provide sensor property values in the JSON
1844 * response.
1845 *
1846 * Finds the LED data asynchronously. Invokes callback when information has
1847 * been obtained.
1848 *
1849 * The callback must have the following signature:
1850 * @code
Gunnar Mills42cbe532019-08-15 15:26:54 -05001851 * callback()
Anthony Wilsond5005492019-07-31 16:34:17 -05001852 * @endcode
1853 *
1854 * @param sensorsAsyncResp Pointer to object holding response data.
1855 * @param inventoryItems D-Bus inventory items associated with sensors.
1856 * @param callback Callback to invoke when inventory items have been obtained.
1857 */
1858template <typename Callback>
1859void getInventoryLeds(
1860 std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
1861 std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
1862 Callback&& callback)
1863{
1864 BMCWEB_LOG_DEBUG << "getInventoryLeds enter";
1865
1866 const std::string path = "/xyz/openbmc_project";
1867 const std::array<std::string, 1> interfaces = {
1868 "xyz.openbmc_project.Led.Physical"};
1869
1870 // Response handler for parsing output from GetSubTree
Ed Tanous002d39b2022-05-31 08:59:27 -07001871 auto respHandler =
1872 [callback{std::forward<Callback>(callback)}, sensorsAsyncResp,
1873 inventoryItems](
1874 const boost::system::error_code ec,
1875 const dbus::utility::MapperGetSubTreeResponse& subtree) {
Anthony Wilsond5005492019-07-31 16:34:17 -05001876 BMCWEB_LOG_DEBUG << "getInventoryLeds respHandler enter";
1877 if (ec)
1878 {
zhanghch058d1b46d2021-04-01 11:18:24 +08001879 messages::internalError(sensorsAsyncResp->asyncResp->res);
Anthony Wilsond5005492019-07-31 16:34:17 -05001880 BMCWEB_LOG_ERROR << "getInventoryLeds respHandler DBus error "
1881 << ec;
1882 return;
1883 }
1884
1885 // Build map of LED object paths to connections
Nan Zhoufe04d492022-06-22 17:10:41 +00001886 std::shared_ptr<std::map<std::string, std::string>> ledConnections =
1887 std::make_shared<std::map<std::string, std::string>>();
Anthony Wilsond5005492019-07-31 16:34:17 -05001888
1889 // Loop through objects from GetSubTree
1890 for (const std::pair<
1891 std::string,
1892 std::vector<std::pair<std::string, std::vector<std::string>>>>&
1893 object : subtree)
1894 {
1895 // Check if object path is LED for one of the specified inventory
1896 // items
1897 const std::string& ledPath = object.first;
1898 if (findInventoryItemForLed(*inventoryItems, ledPath) != nullptr)
1899 {
1900 // Add mapping from ledPath to connection
1901 const std::string& connection = object.second.begin()->first;
1902 (*ledConnections)[ledPath] = connection;
1903 BMCWEB_LOG_DEBUG << "Added mapping " << ledPath << " -> "
1904 << connection;
1905 }
1906 }
1907
1908 getInventoryLedData(sensorsAsyncResp, inventoryItems, ledConnections,
1909 std::move(callback));
1910 BMCWEB_LOG_DEBUG << "getInventoryLeds respHandler exit";
1911 };
1912 // Make call to ObjectMapper to find all inventory items
1913 crow::connections::systemBus->async_method_call(
1914 std::move(respHandler), "xyz.openbmc_project.ObjectMapper",
1915 "/xyz/openbmc_project/object_mapper",
1916 "xyz.openbmc_project.ObjectMapper", "GetSubTree", path, 0, interfaces);
1917 BMCWEB_LOG_DEBUG << "getInventoryLeds exit";
1918}
1919
1920/**
Gunnar Mills42cbe532019-08-15 15:26:54 -05001921 * @brief Gets D-Bus data for Power Supply Attributes such as EfficiencyPercent
1922 *
1923 * Uses the specified connections (services) (currently assumes just one) to
1924 * obtain D-Bus data for Power Supply Attributes. Stores the resulting data in
1925 * the inventoryItems vector. Only stores data in Power Supply inventoryItems.
1926 *
1927 * This data is later used to provide sensor property values in the JSON
1928 * response.
1929 *
1930 * Finds the Power Supply Attributes data asynchronously. Invokes callback
1931 * when data has been obtained.
1932 *
1933 * The callback must have the following signature:
1934 * @code
1935 * callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems)
1936 * @endcode
1937 *
1938 * @param sensorsAsyncResp Pointer to object holding response data.
1939 * @param inventoryItems D-Bus inventory items associated with sensors.
1940 * @param psAttributesConnections Connections that provide data for the Power
1941 * Supply Attributes
1942 * @param callback Callback to invoke when data has been obtained.
1943 */
1944template <typename Callback>
1945void getPowerSupplyAttributesData(
Ed Tanousb5a76932020-09-29 16:16:58 -07001946 const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
Gunnar Mills42cbe532019-08-15 15:26:54 -05001947 std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
Nan Zhoufe04d492022-06-22 17:10:41 +00001948 const std::map<std::string, std::string>& psAttributesConnections,
Gunnar Mills42cbe532019-08-15 15:26:54 -05001949 Callback&& callback)
1950{
1951 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributesData enter";
1952
1953 if (psAttributesConnections.empty())
1954 {
1955 BMCWEB_LOG_DEBUG << "Can't find PowerSupplyAttributes, no connections!";
1956 callback(inventoryItems);
1957 return;
1958 }
1959
1960 // Assuming just one connection (service) for now
Nan Zhoufe04d492022-06-22 17:10:41 +00001961 auto it = psAttributesConnections.begin();
Gunnar Mills42cbe532019-08-15 15:26:54 -05001962
1963 const std::string& psAttributesPath = (*it).first;
1964 const std::string& psAttributesConnection = (*it).second;
1965
1966 // Response handler for Get DeratingFactor property
Ed Tanous002d39b2022-05-31 08:59:27 -07001967 auto respHandler =
1968 [sensorsAsyncResp, inventoryItems,
1969 callback{std::forward<Callback>(callback)}](
1970 const boost::system::error_code ec, const uint32_t value) {
Gunnar Mills42cbe532019-08-15 15:26:54 -05001971 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributesData respHandler enter";
1972 if (ec)
1973 {
1974 BMCWEB_LOG_ERROR
1975 << "getPowerSupplyAttributesData respHandler DBus error " << ec;
zhanghch058d1b46d2021-04-01 11:18:24 +08001976 messages::internalError(sensorsAsyncResp->asyncResp->res);
Gunnar Mills42cbe532019-08-15 15:26:54 -05001977 return;
1978 }
1979
Jonathan Doman1e1e5982021-06-11 09:36:17 -07001980 BMCWEB_LOG_DEBUG << "PS EfficiencyPercent value: " << value;
1981 // Store value in Power Supply Inventory Items
1982 for (InventoryItem& inventoryItem : *inventoryItems)
Gunnar Mills42cbe532019-08-15 15:26:54 -05001983 {
Ed Tanous55f79e62022-01-25 11:26:16 -08001984 if (inventoryItem.isPowerSupply)
Gunnar Mills42cbe532019-08-15 15:26:54 -05001985 {
Jonathan Doman1e1e5982021-06-11 09:36:17 -07001986 inventoryItem.powerSupplyEfficiencyPercent =
1987 static_cast<int>(value);
Gunnar Mills42cbe532019-08-15 15:26:54 -05001988 }
1989 }
Gunnar Mills42cbe532019-08-15 15:26:54 -05001990
1991 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributesData respHandler exit";
1992 callback(inventoryItems);
1993 };
1994
1995 // Get the DeratingFactor property for the PowerSupplyAttributes
1996 // Currently only property on the interface/only one we care about
Jonathan Doman1e1e5982021-06-11 09:36:17 -07001997 sdbusplus::asio::getProperty<uint32_t>(
1998 *crow::connections::systemBus, psAttributesConnection, psAttributesPath,
1999 "xyz.openbmc_project.Control.PowerSupplyAttributes", "DeratingFactor",
2000 std::move(respHandler));
Gunnar Mills42cbe532019-08-15 15:26:54 -05002001
2002 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributesData exit";
2003}
2004
2005/**
2006 * @brief Gets the Power Supply Attributes such as EfficiencyPercent
2007 *
2008 * Gets the D-Bus connection (service) that provides Power Supply Attributes
2009 * data. Then gets the Power Supply Attributes data from the connection
2010 * (currently just assumes 1 connection) and stores the data in the inventory
2011 * item.
2012 *
2013 * This data is later used to provide sensor property values in the JSON
2014 * response. DeratingFactor on D-Bus is mapped to EfficiencyPercent on Redfish.
2015 *
2016 * Finds the Power Supply Attributes data asynchronously. Invokes callback
2017 * when information has been obtained.
2018 *
2019 * The callback must have the following signature:
2020 * @code
2021 * callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems)
2022 * @endcode
2023 *
2024 * @param sensorsAsyncResp Pointer to object holding response data.
2025 * @param inventoryItems D-Bus inventory items associated with sensors.
2026 * @param callback Callback to invoke when data has been obtained.
2027 */
2028template <typename Callback>
2029void getPowerSupplyAttributes(
2030 std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
2031 std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
2032 Callback&& callback)
2033{
2034 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes enter";
2035
2036 // Only need the power supply attributes when the Power Schema
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02002037 if (sensorsAsyncResp->chassisSubNode != sensors::node::power)
Gunnar Mills42cbe532019-08-15 15:26:54 -05002038 {
2039 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes exit since not Power";
2040 callback(inventoryItems);
2041 return;
2042 }
2043
2044 const std::array<std::string, 1> interfaces = {
2045 "xyz.openbmc_project.Control.PowerSupplyAttributes"};
2046
2047 // Response handler for parsing output from GetSubTree
Ed Tanousb9d36b42022-02-26 21:42:46 -08002048 auto respHandler =
2049 [callback{std::forward<Callback>(callback)}, sensorsAsyncResp,
2050 inventoryItems](
2051 const boost::system::error_code ec,
2052 const dbus::utility::MapperGetSubTreeResponse& subtree) {
Ed Tanous002d39b2022-05-31 08:59:27 -07002053 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes respHandler enter";
2054 if (ec)
2055 {
2056 messages::internalError(sensorsAsyncResp->asyncResp->res);
2057 BMCWEB_LOG_ERROR
2058 << "getPowerSupplyAttributes respHandler DBus error " << ec;
2059 return;
2060 }
2061 if (subtree.empty())
2062 {
2063 BMCWEB_LOG_DEBUG << "Can't find Power Supply Attributes!";
2064 callback(inventoryItems);
2065 return;
2066 }
Gunnar Mills42cbe532019-08-15 15:26:54 -05002067
Ed Tanous002d39b2022-05-31 08:59:27 -07002068 // Currently we only support 1 power supply attribute, use this for
2069 // all the power supplies. Build map of object path to connection.
2070 // Assume just 1 connection and 1 path for now.
Nan Zhoufe04d492022-06-22 17:10:41 +00002071 std::map<std::string, std::string> psAttributesConnections;
Gunnar Mills42cbe532019-08-15 15:26:54 -05002072
Ed Tanous002d39b2022-05-31 08:59:27 -07002073 if (subtree[0].first.empty() || subtree[0].second.empty())
2074 {
2075 BMCWEB_LOG_DEBUG << "Power Supply Attributes mapper error!";
2076 callback(inventoryItems);
2077 return;
2078 }
Gunnar Mills42cbe532019-08-15 15:26:54 -05002079
Ed Tanous002d39b2022-05-31 08:59:27 -07002080 const std::string& psAttributesPath = subtree[0].first;
2081 const std::string& connection = subtree[0].second.begin()->first;
Gunnar Mills42cbe532019-08-15 15:26:54 -05002082
Ed Tanous002d39b2022-05-31 08:59:27 -07002083 if (connection.empty())
2084 {
2085 BMCWEB_LOG_DEBUG << "Power Supply Attributes mapper error!";
2086 callback(inventoryItems);
2087 return;
2088 }
Gunnar Mills42cbe532019-08-15 15:26:54 -05002089
Ed Tanous002d39b2022-05-31 08:59:27 -07002090 psAttributesConnections[psAttributesPath] = connection;
2091 BMCWEB_LOG_DEBUG << "Added mapping " << psAttributesPath << " -> "
2092 << connection;
Gunnar Mills42cbe532019-08-15 15:26:54 -05002093
Ed Tanous002d39b2022-05-31 08:59:27 -07002094 getPowerSupplyAttributesData(sensorsAsyncResp, inventoryItems,
2095 psAttributesConnections,
2096 std::move(callback));
2097 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes respHandler exit";
2098 };
Gunnar Mills42cbe532019-08-15 15:26:54 -05002099 // Make call to ObjectMapper to find the PowerSupplyAttributes service
2100 crow::connections::systemBus->async_method_call(
2101 std::move(respHandler), "xyz.openbmc_project.ObjectMapper",
2102 "/xyz/openbmc_project/object_mapper",
2103 "xyz.openbmc_project.ObjectMapper", "GetSubTree",
2104 "/xyz/openbmc_project", 0, interfaces);
2105 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes exit";
2106}
2107
2108/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002109 * @brief Gets inventory items associated with sensors.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002110 *
2111 * Finds the inventory items that are associated with the specified sensors.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002112 * Then gets D-Bus data for the inventory items, such as presence and VPD.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002113 *
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002114 * This data is later used to provide sensor property values in the JSON
2115 * response.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002116 *
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002117 * Finds the inventory items asynchronously. Invokes callback when the
2118 * inventory items have been obtained.
2119 *
2120 * The callback must have the following signature:
2121 * @code
2122 * callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems)
2123 * @endcode
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002124 *
2125 * @param sensorsAsyncResp Pointer to object holding response data.
2126 * @param sensorNames All sensors within the current chassis.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002127 * implements ObjectManager.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002128 * @param callback Callback to invoke when inventory items have been obtained.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002129 */
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002130template <typename Callback>
Ed Tanousd0090732022-10-04 17:22:56 -07002131static void
2132 getInventoryItems(std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
2133 const std::shared_ptr<std::set<std::string>> sensorNames,
2134 Callback&& callback)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002135{
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002136 BMCWEB_LOG_DEBUG << "getInventoryItems enter";
2137 auto getInventoryItemAssociationsCb =
Ed Tanousd0090732022-10-04 17:22:56 -07002138 [sensorsAsyncResp, callback{std::forward<Callback>(callback)}](
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002139 std::shared_ptr<std::vector<InventoryItem>> inventoryItems) {
Ed Tanous002d39b2022-05-31 08:59:27 -07002140 BMCWEB_LOG_DEBUG << "getInventoryItemAssociationsCb enter";
2141 auto getInventoryItemsConnectionsCb =
Ed Tanousd0090732022-10-04 17:22:56 -07002142 [sensorsAsyncResp, inventoryItems,
Ed Tanous002d39b2022-05-31 08:59:27 -07002143 callback{std::forward<const Callback>(callback)}](
Nan Zhoufe04d492022-06-22 17:10:41 +00002144 std::shared_ptr<std::set<std::string>> invConnections) {
Ed Tanous002d39b2022-05-31 08:59:27 -07002145 BMCWEB_LOG_DEBUG << "getInventoryItemsConnectionsCb enter";
2146 auto getInventoryItemsDataCb = [sensorsAsyncResp, inventoryItems,
2147 callback{std::move(callback)}]() {
2148 BMCWEB_LOG_DEBUG << "getInventoryItemsDataCb enter";
Gunnar Mills42cbe532019-08-15 15:26:54 -05002149
Ed Tanous002d39b2022-05-31 08:59:27 -07002150 auto getInventoryLedsCb = [sensorsAsyncResp, inventoryItems,
2151 callback{std::move(callback)}]() {
2152 BMCWEB_LOG_DEBUG << "getInventoryLedsCb enter";
2153 // Find Power Supply Attributes and get the data
2154 getPowerSupplyAttributes(sensorsAsyncResp, inventoryItems,
2155 std::move(callback));
2156 BMCWEB_LOG_DEBUG << "getInventoryLedsCb exit";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002157 };
2158
Ed Tanous002d39b2022-05-31 08:59:27 -07002159 // Find led connections and get the data
2160 getInventoryLeds(sensorsAsyncResp, inventoryItems,
2161 std::move(getInventoryLedsCb));
2162 BMCWEB_LOG_DEBUG << "getInventoryItemsDataCb exit";
2163 };
2164
2165 // Get inventory item data from connections
2166 getInventoryItemsData(sensorsAsyncResp, inventoryItems,
Ed Tanousd0090732022-10-04 17:22:56 -07002167 invConnections,
Ed Tanous002d39b2022-05-31 08:59:27 -07002168 std::move(getInventoryItemsDataCb));
2169 BMCWEB_LOG_DEBUG << "getInventoryItemsConnectionsCb exit";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002170 };
2171
Ed Tanous002d39b2022-05-31 08:59:27 -07002172 // Get connections that provide inventory item data
2173 getInventoryItemsConnections(sensorsAsyncResp, inventoryItems,
2174 std::move(getInventoryItemsConnectionsCb));
2175 BMCWEB_LOG_DEBUG << "getInventoryItemAssociationsCb exit";
2176 };
2177
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002178 // Get associations from sensors to inventory items
Ed Tanousd0090732022-10-04 17:22:56 -07002179 getInventoryItemAssociations(sensorsAsyncResp, sensorNames,
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002180 std::move(getInventoryItemAssociationsCb));
2181 BMCWEB_LOG_DEBUG << "getInventoryItems exit";
2182}
2183
2184/**
2185 * @brief Returns JSON PowerSupply object for the specified inventory item.
2186 *
2187 * Searches for a JSON PowerSupply object that matches the specified inventory
2188 * item. If one is not found, a new PowerSupply object is added to the JSON
2189 * array.
2190 *
2191 * Multiple sensors are often associated with one power supply inventory item.
2192 * As a result, multiple sensor values are stored in one JSON PowerSupply
2193 * object.
2194 *
2195 * @param powerSupplyArray JSON array containing Redfish PowerSupply objects.
2196 * @param inventoryItem Inventory item for the power supply.
2197 * @param chassisId Chassis that contains the power supply.
2198 * @return JSON PowerSupply object for the specified inventory item.
2199 */
Ed Tanous23a21a12020-07-25 04:45:05 +00002200inline nlohmann::json& getPowerSupply(nlohmann::json& powerSupplyArray,
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002201 const InventoryItem& inventoryItem,
2202 const std::string& chassisId)
2203{
2204 // Check if matching PowerSupply object already exists in JSON array
2205 for (nlohmann::json& powerSupply : powerSupplyArray)
2206 {
2207 if (powerSupply["MemberId"] == inventoryItem.name)
2208 {
2209 return powerSupply;
2210 }
2211 }
2212
2213 // Add new PowerSupply object to JSON array
2214 powerSupplyArray.push_back({});
2215 nlohmann::json& powerSupply = powerSupplyArray.back();
2216 powerSupply["@odata.id"] =
2217 "/redfish/v1/Chassis/" + chassisId + "/Power#/PowerSupplies/";
2218 powerSupply["MemberId"] = inventoryItem.name;
2219 powerSupply["Name"] = boost::replace_all_copy(inventoryItem.name, "_", " ");
2220 powerSupply["Manufacturer"] = inventoryItem.manufacturer;
2221 powerSupply["Model"] = inventoryItem.model;
2222 powerSupply["PartNumber"] = inventoryItem.partNumber;
2223 powerSupply["SerialNumber"] = inventoryItem.serialNumber;
Anthony Wilsond5005492019-07-31 16:34:17 -05002224 setLedState(powerSupply, &inventoryItem);
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002225
Gunnar Mills42cbe532019-08-15 15:26:54 -05002226 if (inventoryItem.powerSupplyEfficiencyPercent >= 0)
2227 {
2228 powerSupply["EfficiencyPercent"] =
2229 inventoryItem.powerSupplyEfficiencyPercent;
2230 }
2231
2232 powerSupply["Status"]["State"] = getState(&inventoryItem);
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002233 const char* health = inventoryItem.isFunctional ? "OK" : "Critical";
2234 powerSupply["Status"]["Health"] = health;
2235
2236 return powerSupply;
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002237}
2238
2239/**
Shawn McCarneyde629b62019-03-08 10:42:51 -06002240 * @brief Gets the values of the specified sensors.
2241 *
2242 * Stores the results as JSON in the SensorsAsyncResp.
2243 *
2244 * Gets the sensor values asynchronously. Stores the results later when the
2245 * information has been obtained.
2246 *
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002247 * The sensorNames set contains all requested sensors for the current chassis.
Shawn McCarneyde629b62019-03-08 10:42:51 -06002248 *
2249 * To minimize the number of DBus calls, the DBus method
2250 * org.freedesktop.DBus.ObjectManager.GetManagedObjects() is used to get the
2251 * values of all sensors provided by a connection (service).
2252 *
2253 * The connections set contains all the connections that provide sensor values.
2254 *
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002255 * The InventoryItem vector contains D-Bus inventory items associated with the
2256 * sensors. Inventory item data is needed for some Redfish sensor properties.
2257 *
Shawn McCarneyde629b62019-03-08 10:42:51 -06002258 * @param SensorsAsyncResp Pointer to object holding response data.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002259 * @param sensorNames All requested sensors within the current chassis.
Shawn McCarneyde629b62019-03-08 10:42:51 -06002260 * @param connections Connections that provide sensor values.
Shawn McCarneyde629b62019-03-08 10:42:51 -06002261 * implements ObjectManager.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002262 * @param inventoryItems Inventory items associated with the sensors.
Shawn McCarneyde629b62019-03-08 10:42:51 -06002263 */
Ed Tanous23a21a12020-07-25 04:45:05 +00002264inline void getSensorData(
Ed Tanous81ce6092020-12-17 16:54:55 +00002265 const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
Nan Zhoufe04d492022-06-22 17:10:41 +00002266 const std::shared_ptr<std::set<std::string>>& sensorNames,
2267 const std::set<std::string>& connections,
Ed Tanousb5a76932020-09-29 16:16:58 -07002268 const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems)
Shawn McCarneyde629b62019-03-08 10:42:51 -06002269{
2270 BMCWEB_LOG_DEBUG << "getSensorData enter";
2271 // Get managed objects from all services exposing sensors
2272 for (const std::string& connection : connections)
2273 {
2274 // Response handler to process managed objects
Ed Tanous002d39b2022-05-31 08:59:27 -07002275 auto getManagedObjectsCb =
2276 [sensorsAsyncResp, sensorNames,
2277 inventoryItems](const boost::system::error_code ec,
Ed Tanous02cad962022-06-30 16:50:15 -07002278 const dbus::utility::ManagedObjectType& resp) {
Shawn McCarneyde629b62019-03-08 10:42:51 -06002279 BMCWEB_LOG_DEBUG << "getManagedObjectsCb enter";
2280 if (ec)
2281 {
2282 BMCWEB_LOG_ERROR << "getManagedObjectsCb DBUS error: " << ec;
zhanghch058d1b46d2021-04-01 11:18:24 +08002283 messages::internalError(sensorsAsyncResp->asyncResp->res);
Shawn McCarneyde629b62019-03-08 10:42:51 -06002284 return;
2285 }
2286 // Go through all objects and update response with sensor data
2287 for (const auto& objDictEntry : resp)
2288 {
2289 const std::string& objPath =
2290 static_cast<const std::string&>(objDictEntry.first);
2291 BMCWEB_LOG_DEBUG << "getManagedObjectsCb parsing object "
2292 << objPath;
2293
Shawn McCarneyde629b62019-03-08 10:42:51 -06002294 std::vector<std::string> split;
2295 // Reserve space for
2296 // /xyz/openbmc_project/sensors/<name>/<subname>
2297 split.reserve(6);
2298 boost::algorithm::split(split, objPath, boost::is_any_of("/"));
2299 if (split.size() < 6)
2300 {
2301 BMCWEB_LOG_ERROR << "Got path that isn't long enough "
2302 << objPath;
2303 continue;
2304 }
2305 // These indexes aren't intuitive, as boost::split puts an empty
2306 // string at the beginning
2307 const std::string& sensorType = split[4];
2308 const std::string& sensorName = split[5];
2309 BMCWEB_LOG_DEBUG << "sensorName " << sensorName
2310 << " sensorType " << sensorType;
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002311 if (sensorNames->find(objPath) == sensorNames->end())
Shawn McCarneyde629b62019-03-08 10:42:51 -06002312 {
Andrew Geissleraccdbb22021-11-09 15:24:45 -06002313 BMCWEB_LOG_DEBUG << sensorName << " not in sensor list ";
Shawn McCarneyde629b62019-03-08 10:42:51 -06002314 continue;
2315 }
2316
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002317 // Find inventory item (if any) associated with sensor
2318 InventoryItem* inventoryItem =
2319 findInventoryItemForSensor(inventoryItems, objPath);
2320
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002321 const std::string& sensorSchema =
Ed Tanous81ce6092020-12-17 16:54:55 +00002322 sensorsAsyncResp->chassisSubNode;
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002323
2324 nlohmann::json* sensorJson = nullptr;
2325
Nan Zhou928fefb2022-03-28 08:45:00 -07002326 if (sensorSchema == sensors::node::sensors &&
2327 !sensorsAsyncResp->efficientExpand)
Shawn McCarneyde629b62019-03-08 10:42:51 -06002328 {
Ed Tanousc1d019a2022-08-06 09:36:06 -07002329 std::string sensorTypeEscaped(sensorType);
2330 sensorTypeEscaped.erase(
2331 std::remove(sensorTypeEscaped.begin(),
2332 sensorTypeEscaped.end(), '_'),
2333 sensorTypeEscaped.end());
2334 std::string sensorId(sensorTypeEscaped);
2335 sensorId += "_";
2336 sensorId += sensorName;
2337
zhanghch058d1b46d2021-04-01 11:18:24 +08002338 sensorsAsyncResp->asyncResp->res.jsonValue["@odata.id"] =
Ed Tanousc1d019a2022-08-06 09:36:06 -07002339 crow::utility::urlFromPieces(
2340 "redfish", "v1", "Chassis",
2341 sensorsAsyncResp->chassisId,
2342 sensorsAsyncResp->chassisSubNode, sensorId);
zhanghch058d1b46d2021-04-01 11:18:24 +08002343 sensorJson = &(sensorsAsyncResp->asyncResp->res.jsonValue);
Shawn McCarneyde629b62019-03-08 10:42:51 -06002344 }
2345 else
2346 {
Ed Tanous271584a2019-07-09 16:24:22 -07002347 std::string fieldName;
Nan Zhou928fefb2022-03-28 08:45:00 -07002348 if (sensorsAsyncResp->efficientExpand)
2349 {
2350 fieldName = "Members";
2351 }
2352 else if (sensorType == "temperature")
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002353 {
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002354 fieldName = "Temperatures";
2355 }
2356 else if (sensorType == "fan" || sensorType == "fan_tach" ||
2357 sensorType == "fan_pwm")
2358 {
2359 fieldName = "Fans";
2360 }
2361 else if (sensorType == "voltage")
2362 {
2363 fieldName = "Voltages";
2364 }
2365 else if (sensorType == "power")
2366 {
Ed Tanous55f79e62022-01-25 11:26:16 -08002367 if (sensorName == "total_power")
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002368 {
2369 fieldName = "PowerControl";
2370 }
2371 else if ((inventoryItem != nullptr) &&
2372 (inventoryItem->isPowerSupply))
2373 {
2374 fieldName = "PowerSupplies";
2375 }
2376 else
2377 {
2378 // Other power sensors are in SensorCollection
2379 continue;
2380 }
2381 }
2382 else
2383 {
2384 BMCWEB_LOG_ERROR << "Unsure how to handle sensorType "
2385 << sensorType;
2386 continue;
2387 }
2388
2389 nlohmann::json& tempArray =
zhanghch058d1b46d2021-04-01 11:18:24 +08002390 sensorsAsyncResp->asyncResp->res.jsonValue[fieldName];
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002391 if (fieldName == "PowerControl")
2392 {
2393 if (tempArray.empty())
2394 {
2395 // Put multiple "sensors" into a single
2396 // PowerControl. Follows MemberId naming and
2397 // naming in power.hpp.
Ed Tanous14766872022-03-15 10:44:42 -07002398 nlohmann::json::object_t power;
2399 power["@odata.id"] =
2400 "/redfish/v1/Chassis/" +
2401 sensorsAsyncResp->chassisId + "/" +
2402 sensorsAsyncResp->chassisSubNode + "#/" +
2403 fieldName + "/0";
2404 tempArray.push_back(std::move(power));
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002405 }
2406 sensorJson = &(tempArray.back());
2407 }
2408 else if (fieldName == "PowerSupplies")
2409 {
2410 if (inventoryItem != nullptr)
2411 {
2412 sensorJson =
2413 &(getPowerSupply(tempArray, *inventoryItem,
Ed Tanous81ce6092020-12-17 16:54:55 +00002414 sensorsAsyncResp->chassisId));
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002415 }
2416 }
Nan Zhou928fefb2022-03-28 08:45:00 -07002417 else if (fieldName == "Members")
2418 {
Ed Tanous677bb752022-09-15 10:52:19 -07002419 std::string sensorTypeEscaped(sensorType);
2420 sensorTypeEscaped.erase(
2421 std::remove(sensorTypeEscaped.begin(),
2422 sensorTypeEscaped.end(), '_'),
2423 sensorTypeEscaped.end());
2424 std::string sensorId(sensorTypeEscaped);
2425 sensorId += "_";
2426 sensorId += sensorName;
2427
Ed Tanous14766872022-03-15 10:44:42 -07002428 nlohmann::json::object_t member;
Ed Tanous677bb752022-09-15 10:52:19 -07002429 member["@odata.id"] = crow::utility::urlFromPieces(
2430 "redfish", "v1", "Chassis",
2431 sensorsAsyncResp->chassisId,
2432 sensorsAsyncResp->chassisSubNode, sensorId);
Ed Tanous14766872022-03-15 10:44:42 -07002433 tempArray.push_back(std::move(member));
Nan Zhou928fefb2022-03-28 08:45:00 -07002434 sensorJson = &(tempArray.back());
2435 }
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002436 else
2437 {
Ed Tanous14766872022-03-15 10:44:42 -07002438 nlohmann::json::object_t member;
2439 member["@odata.id"] = "/redfish/v1/Chassis/" +
2440 sensorsAsyncResp->chassisId +
2441 "/" +
2442 sensorsAsyncResp->chassisSubNode +
2443 "#/" + fieldName + "/";
2444 tempArray.push_back(std::move(member));
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002445 sensorJson = &(tempArray.back());
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002446 }
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002447 }
Shawn McCarneyde629b62019-03-08 10:42:51 -06002448
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002449 if (sensorJson != nullptr)
2450 {
Ed Tanous1d7c0052022-08-09 12:32:26 -07002451 objectInterfacesToJson(sensorName, sensorType,
2452 sensorsAsyncResp->chassisSubNode,
2453 objDictEntry.second, *sensorJson,
2454 inventoryItem);
2455
2456 std::string path = "/xyz/openbmc_project/sensors/";
2457 path += sensorType;
2458 path += "/";
2459 path += sensorName;
Ed Tanousc1d019a2022-08-06 09:36:06 -07002460 sensorsAsyncResp->addMetadata(*sensorJson, path);
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002461 }
Shawn McCarneyde629b62019-03-08 10:42:51 -06002462 }
Ed Tanous81ce6092020-12-17 16:54:55 +00002463 if (sensorsAsyncResp.use_count() == 1)
James Feist8bd25cc2019-03-15 15:14:00 -07002464 {
Ed Tanous81ce6092020-12-17 16:54:55 +00002465 sortJSONResponse(sensorsAsyncResp);
Nan Zhou928fefb2022-03-28 08:45:00 -07002466 if (sensorsAsyncResp->chassisSubNode ==
2467 sensors::node::sensors &&
2468 sensorsAsyncResp->efficientExpand)
2469 {
2470 sensorsAsyncResp->asyncResp->res
2471 .jsonValue["Members@odata.count"] =
2472 sensorsAsyncResp->asyncResp->res.jsonValue["Members"]
2473 .size();
2474 }
2475 else if (sensorsAsyncResp->chassisSubNode ==
2476 sensors::node::thermal)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002477 {
Ed Tanous81ce6092020-12-17 16:54:55 +00002478 populateFanRedundancy(sensorsAsyncResp);
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002479 }
James Feist8bd25cc2019-03-15 15:14:00 -07002480 }
Shawn McCarneyde629b62019-03-08 10:42:51 -06002481 BMCWEB_LOG_DEBUG << "getManagedObjectsCb exit";
2482 };
2483
Shawn McCarneyde629b62019-03-08 10:42:51 -06002484 crow::connections::systemBus->async_method_call(
Ed Tanousd0090732022-10-04 17:22:56 -07002485 getManagedObjectsCb, connection, "/xyz/openbmc_project/sensors",
Shawn McCarneyde629b62019-03-08 10:42:51 -06002486 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
Ed Tanous23a21a12020-07-25 04:45:05 +00002487 }
Shawn McCarneyde629b62019-03-08 10:42:51 -06002488 BMCWEB_LOG_DEBUG << "getSensorData exit";
2489}
2490
Nan Zhoufe04d492022-06-22 17:10:41 +00002491inline void
2492 processSensorList(const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
2493 const std::shared_ptr<std::set<std::string>>& sensorNames)
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002494{
Nan Zhoufe04d492022-06-22 17:10:41 +00002495 auto getConnectionCb = [sensorsAsyncResp, sensorNames](
2496 const std::set<std::string>& connections) {
Ed Tanous002d39b2022-05-31 08:59:27 -07002497 BMCWEB_LOG_DEBUG << "getConnectionCb enter";
Ed Tanousd0090732022-10-04 17:22:56 -07002498 auto getInventoryItemsCb =
2499 [sensorsAsyncResp, sensorNames,
2500 connections](const std::shared_ptr<std::vector<InventoryItem>>&
2501 inventoryItems) {
2502 BMCWEB_LOG_DEBUG << "getInventoryItemsCb enter";
2503 // Get sensor data and store results in JSON
2504 getSensorData(sensorsAsyncResp, sensorNames, connections,
2505 inventoryItems);
2506 BMCWEB_LOG_DEBUG << "getInventoryItemsCb exit";
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002507 };
2508
Ed Tanousd0090732022-10-04 17:22:56 -07002509 // Get inventory items associated with sensors
2510 getInventoryItems(sensorsAsyncResp, sensorNames,
2511 std::move(getInventoryItemsCb));
2512
Ed Tanous002d39b2022-05-31 08:59:27 -07002513 BMCWEB_LOG_DEBUG << "getConnectionCb exit";
2514 };
2515
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002516 // Get set of connections that provide sensor values
Ed Tanous81ce6092020-12-17 16:54:55 +00002517 getConnections(sensorsAsyncResp, sensorNames, std::move(getConnectionCb));
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002518}
2519
Shawn McCarneyde629b62019-03-08 10:42:51 -06002520/**
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +01002521 * @brief Entry point for retrieving sensors data related to requested
2522 * chassis.
Kowalski, Kamil588c3f02018-04-03 14:55:27 +02002523 * @param SensorsAsyncResp Pointer to object holding response data
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +01002524 */
Ed Tanousb5a76932020-09-29 16:16:58 -07002525inline void
Ed Tanous81ce6092020-12-17 16:54:55 +00002526 getChassisData(const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp)
Ed Tanous1abe55e2018-09-05 08:30:59 -07002527{
2528 BMCWEB_LOG_DEBUG << "getChassisData enter";
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002529 auto getChassisCb =
Ed Tanous81ce6092020-12-17 16:54:55 +00002530 [sensorsAsyncResp](
Nan Zhoufe04d492022-06-22 17:10:41 +00002531 const std::shared_ptr<std::set<std::string>>& sensorNames) {
Ed Tanous002d39b2022-05-31 08:59:27 -07002532 BMCWEB_LOG_DEBUG << "getChassisCb enter";
2533 processSensorList(sensorsAsyncResp, sensorNames);
2534 BMCWEB_LOG_DEBUG << "getChassisCb exit";
2535 };
Nan Zhou928fefb2022-03-28 08:45:00 -07002536 // SensorCollection doesn't contain the Redundancy property
2537 if (sensorsAsyncResp->chassisSubNode != sensors::node::sensors)
2538 {
2539 sensorsAsyncResp->asyncResp->res.jsonValue["Redundancy"] =
2540 nlohmann::json::array();
2541 }
Shawn McCarney26f03892019-05-03 13:20:24 -05002542 // Get set of sensors in chassis
Ed Tanous7f1cc262022-08-09 13:33:57 -07002543 getChassis(sensorsAsyncResp->asyncResp, sensorsAsyncResp->chassisId,
2544 sensorsAsyncResp->chassisSubNode, sensorsAsyncResp->types,
2545 std::move(getChassisCb));
Ed Tanous1abe55e2018-09-05 08:30:59 -07002546 BMCWEB_LOG_DEBUG << "getChassisData exit";
Ed Tanous271584a2019-07-09 16:24:22 -07002547}
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +01002548
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302549/**
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002550 * @brief Find the requested sensorName in the list of all sensors supplied by
2551 * the chassis node
2552 *
2553 * @param sensorName The sensor name supplied in the PATCH request
2554 * @param sensorsList The list of sensors managed by the chassis node
2555 * @param sensorsModified The list of sensors that were found as a result of
2556 * repeated calls to this function
2557 */
Nan Zhoufe04d492022-06-22 17:10:41 +00002558inline bool
2559 findSensorNameUsingSensorPath(std::string_view sensorName,
Ed Tanous02cad962022-06-30 16:50:15 -07002560 const std::set<std::string>& sensorsList,
Nan Zhoufe04d492022-06-22 17:10:41 +00002561 std::set<std::string>& sensorsModified)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002562{
Nan Zhoufe04d492022-06-22 17:10:41 +00002563 for (const auto& chassisSensor : sensorsList)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002564 {
George Liu28aa8de2021-02-01 15:13:30 +08002565 sdbusplus::message::object_path path(chassisSensor);
Ed Tanousb00dcc22021-02-23 12:52:50 -08002566 std::string thisSensorName = path.filename();
George Liu28aa8de2021-02-01 15:13:30 +08002567 if (thisSensorName.empty())
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002568 {
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002569 continue;
2570 }
2571 if (thisSensorName == sensorName)
2572 {
2573 sensorsModified.emplace(chassisSensor);
2574 return true;
2575 }
2576 }
2577 return false;
2578}
2579
2580/**
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302581 * @brief Entry point for overriding sensor values of given sensor
2582 *
zhanghch058d1b46d2021-04-01 11:18:24 +08002583 * @param sensorAsyncResp response object
Carol Wang4bb3dc32019-10-17 18:15:02 +08002584 * @param allCollections Collections extract from sensors' request patch info
jayaprakash Mutyala91e130a2020-03-04 22:26:38 +00002585 * @param chassisSubNode Chassis Node for which the query has to happen
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302586 */
Ed Tanous23a21a12020-07-25 04:45:05 +00002587inline void setSensorsOverride(
Ed Tanousb5a76932020-09-29 16:16:58 -07002588 const std::shared_ptr<SensorsAsyncResp>& sensorAsyncResp,
Carol Wang4bb3dc32019-10-17 18:15:02 +08002589 std::unordered_map<std::string, std::vector<nlohmann::json>>&
jayaprakash Mutyala397fd612020-02-06 23:33:34 +00002590 allCollections)
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302591{
jayaprakash Mutyala70d1d0a2020-01-21 23:41:36 +00002592 BMCWEB_LOG_INFO << "setSensorsOverride for subNode"
Carol Wang4bb3dc32019-10-17 18:15:02 +08002593 << sensorAsyncResp->chassisSubNode << "\n";
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302594
Ed Tanous543f4402022-01-06 13:12:53 -08002595 const char* propertyValueName = nullptr;
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302596 std::unordered_map<std::string, std::pair<double, std::string>> overrideMap;
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302597 std::string memberId;
Ed Tanous543f4402022-01-06 13:12:53 -08002598 double value = 0.0;
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302599 for (auto& collectionItems : allCollections)
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302600 {
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302601 if (collectionItems.first == "Temperatures")
2602 {
2603 propertyValueName = "ReadingCelsius";
2604 }
2605 else if (collectionItems.first == "Fans")
2606 {
2607 propertyValueName = "Reading";
2608 }
2609 else
2610 {
2611 propertyValueName = "ReadingVolts";
2612 }
2613 for (auto& item : collectionItems.second)
2614 {
zhanghch058d1b46d2021-04-01 11:18:24 +08002615 if (!json_util::readJson(item, sensorAsyncResp->asyncResp->res,
2616 "MemberId", memberId, propertyValueName,
2617 value))
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302618 {
2619 return;
2620 }
2621 overrideMap.emplace(memberId,
2622 std::make_pair(value, collectionItems.first));
2623 }
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302624 }
Carol Wang4bb3dc32019-10-17 18:15:02 +08002625
Ed Tanous002d39b2022-05-31 08:59:27 -07002626 auto getChassisSensorListCb =
2627 [sensorAsyncResp, overrideMap](
Nan Zhoufe04d492022-06-22 17:10:41 +00002628 const std::shared_ptr<std::set<std::string>>& sensorsList) {
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002629 // Match sensor names in the PATCH request to those managed by the
2630 // chassis node
Nan Zhoufe04d492022-06-22 17:10:41 +00002631 const std::shared_ptr<std::set<std::string>> sensorNames =
2632 std::make_shared<std::set<std::string>>();
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302633 for (const auto& item : overrideMap)
2634 {
2635 const auto& sensor = item.first;
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002636 if (!findSensorNameUsingSensorPath(sensor, *sensorsList,
2637 *sensorNames))
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302638 {
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302639 BMCWEB_LOG_INFO << "Unable to find memberId " << item.first;
zhanghch058d1b46d2021-04-01 11:18:24 +08002640 messages::resourceNotFound(sensorAsyncResp->asyncResp->res,
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302641 item.second.second, item.first);
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302642 return;
2643 }
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302644 }
2645 // Get the connection to which the memberId belongs
Ed Tanous002d39b2022-05-31 08:59:27 -07002646 auto getObjectsWithConnectionCb =
Nan Zhoufe04d492022-06-22 17:10:41 +00002647 [sensorAsyncResp,
2648 overrideMap](const std::set<std::string>& /*connections*/,
2649 const std::set<std::pair<std::string, std::string>>&
2650 objectsWithConnection) {
Jayaprakash Mutyala4f277b52021-12-08 22:46:49 +00002651 if (objectsWithConnection.size() != overrideMap.size())
2652 {
2653 BMCWEB_LOG_INFO
2654 << "Unable to find all objects with proper connection "
2655 << objectsWithConnection.size() << " requested "
2656 << overrideMap.size() << "\n";
2657 messages::resourceNotFound(sensorAsyncResp->asyncResp->res,
2658 sensorAsyncResp->chassisSubNode ==
2659 sensors::node::thermal
2660 ? "Temperatures"
2661 : "Voltages",
2662 "Count");
2663 return;
2664 }
2665 for (const auto& item : objectsWithConnection)
2666 {
2667 sdbusplus::message::object_path path(item.first);
2668 std::string sensorName = path.filename();
2669 if (sensorName.empty())
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302670 {
Jayaprakash Mutyala4f277b52021-12-08 22:46:49 +00002671 messages::internalError(sensorAsyncResp->asyncResp->res);
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302672 return;
2673 }
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302674
Jayaprakash Mutyala4f277b52021-12-08 22:46:49 +00002675 const auto& iterator = overrideMap.find(sensorName);
2676 if (iterator == overrideMap.end())
2677 {
2678 BMCWEB_LOG_INFO << "Unable to find sensor object"
2679 << item.first << "\n";
2680 messages::internalError(sensorAsyncResp->asyncResp->res);
2681 return;
2682 }
2683 crow::connections::systemBus->async_method_call(
2684 [sensorAsyncResp](const boost::system::error_code ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -07002685 if (ec)
2686 {
2687 if (ec.value() ==
2688 boost::system::errc::permission_denied)
Jayaprakash Mutyala4f277b52021-12-08 22:46:49 +00002689 {
Ed Tanous002d39b2022-05-31 08:59:27 -07002690 BMCWEB_LOG_WARNING
2691 << "Manufacturing mode is not Enabled...can't "
2692 "Override the sensor value. ";
Jayaprakash Mutyala4f277b52021-12-08 22:46:49 +00002693
Ed Tanous002d39b2022-05-31 08:59:27 -07002694 messages::insufficientPrivilege(
Jayaprakash Mutyala4f277b52021-12-08 22:46:49 +00002695 sensorAsyncResp->asyncResp->res);
Ed Tanous002d39b2022-05-31 08:59:27 -07002696 return;
Jayaprakash Mutyala4f277b52021-12-08 22:46:49 +00002697 }
Ed Tanous002d39b2022-05-31 08:59:27 -07002698 BMCWEB_LOG_DEBUG
2699 << "setOverrideValueStatus DBUS error: " << ec;
2700 messages::internalError(
2701 sensorAsyncResp->asyncResp->res);
2702 }
Jayaprakash Mutyala4f277b52021-12-08 22:46:49 +00002703 },
2704 item.second, item.first, "org.freedesktop.DBus.Properties",
2705 "Set", "xyz.openbmc_project.Sensor.Value", "Value",
Ed Tanous168e20c2021-12-13 14:39:53 -08002706 dbus::utility::DbusVariantType(iterator->second.first));
Jayaprakash Mutyala4f277b52021-12-08 22:46:49 +00002707 }
2708 };
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302709 // Get object with connection for the given sensor name
2710 getObjectsWithConnection(sensorAsyncResp, sensorNames,
2711 std::move(getObjectsWithConnectionCb));
2712 };
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302713 // get full sensor list for the given chassisId and cross verify the sensor.
Ed Tanous7f1cc262022-08-09 13:33:57 -07002714 getChassis(sensorAsyncResp->asyncResp, sensorAsyncResp->chassisId,
2715 sensorAsyncResp->chassisSubNode, sensorAsyncResp->types,
2716 std::move(getChassisSensorListCb));
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302717}
2718
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02002719/**
2720 * @brief Retrieves mapping of Redfish URIs to sensor value property to D-Bus
2721 * path of the sensor.
2722 *
2723 * Function builds valid Redfish response for sensor query of given chassis and
2724 * node. It then builds metadata about Redfish<->D-Bus correlations and provides
2725 * it to caller in a callback.
2726 *
2727 * @param chassis Chassis for which retrieval should be performed
2728 * @param node Node (group) of sensors. See sensors::node for supported values
2729 * @param mapComplete Callback to be called with retrieval result
2730 */
Krzysztof Grobelny021d32c2021-10-29 16:00:07 +02002731inline void retrieveUriToDbusMap(const std::string& chassis,
2732 const std::string& node,
2733 SensorsAsyncResp::DataCompleteCb&& mapComplete)
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02002734{
Ed Tanous02da7c52022-02-27 00:09:02 -08002735 decltype(sensors::paths)::const_iterator pathIt =
2736 std::find_if(sensors::paths.cbegin(), sensors::paths.cend(),
2737 [&node](auto&& val) { return val.first == node; });
2738 if (pathIt == sensors::paths.cend())
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02002739 {
2740 BMCWEB_LOG_ERROR << "Wrong node provided : " << node;
2741 mapComplete(boost::beast::http::status::bad_request, {});
2742 return;
2743 }
Krzysztof Grobelnyd51e0722021-04-16 13:15:21 +00002744
Nan Zhou72374eb2022-01-27 17:06:51 -08002745 auto asyncResp = std::make_shared<bmcweb::AsyncResp>();
Nan Zhoufe04d492022-06-22 17:10:41 +00002746 auto callback = [asyncResp, mapCompleteCb{std::move(mapComplete)}](
2747 const boost::beast::http::status status,
2748 const std::map<std::string, std::string>& uriToDbus) {
2749 mapCompleteCb(status, uriToDbus);
2750 };
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02002751
2752 auto resp = std::make_shared<SensorsAsyncResp>(
Krzysztof Grobelnyd51e0722021-04-16 13:15:21 +00002753 asyncResp, chassis, pathIt->second, node, std::move(callback));
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02002754 getChassisData(resp);
2755}
2756
Nan Zhoubacb2162022-04-06 11:28:32 -07002757namespace sensors
2758{
Nan Zhou928fefb2022-03-28 08:45:00 -07002759
Nan Zhoubacb2162022-04-06 11:28:32 -07002760inline void getChassisCallback(
Ed Tanousc1d019a2022-08-06 09:36:06 -07002761 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2762 std::string_view chassisId, std::string_view chassisSubNode,
Nan Zhoufe04d492022-06-22 17:10:41 +00002763 const std::shared_ptr<std::set<std::string>>& sensorNames)
Nan Zhoubacb2162022-04-06 11:28:32 -07002764{
Ed Tanousc1d019a2022-08-06 09:36:06 -07002765 BMCWEB_LOG_DEBUG << "getChassisCallback enter ";
Nan Zhoubacb2162022-04-06 11:28:32 -07002766
Ed Tanousc1d019a2022-08-06 09:36:06 -07002767 nlohmann::json& entriesArray = asyncResp->res.jsonValue["Members"];
2768 for (const std::string& sensor : *sensorNames)
Nan Zhoubacb2162022-04-06 11:28:32 -07002769 {
2770 BMCWEB_LOG_DEBUG << "Adding sensor: " << sensor;
2771
2772 sdbusplus::message::object_path path(sensor);
2773 std::string sensorName = path.filename();
2774 if (sensorName.empty())
2775 {
2776 BMCWEB_LOG_ERROR << "Invalid sensor path: " << sensor;
Ed Tanousc1d019a2022-08-06 09:36:06 -07002777 messages::internalError(asyncResp->res);
Nan Zhoubacb2162022-04-06 11:28:32 -07002778 return;
2779 }
Ed Tanousc1d019a2022-08-06 09:36:06 -07002780 std::string type = path.parent_path().filename();
2781 // fan_tach has an underscore in it, so remove it to "normalize" the
2782 // type in the URI
2783 type.erase(std::remove(type.begin(), type.end(), '_'), type.end());
2784
Ed Tanous14766872022-03-15 10:44:42 -07002785 nlohmann::json::object_t member;
Ed Tanousc1d019a2022-08-06 09:36:06 -07002786 std::string id = type;
2787 id += "_";
2788 id += sensorName;
2789 member["@odata.id"] = crow::utility::urlFromPieces(
2790 "redfish", "v1", "Chassis", chassisId, chassisSubNode, id);
2791
Ed Tanous14766872022-03-15 10:44:42 -07002792 entriesArray.push_back(std::move(member));
Nan Zhoubacb2162022-04-06 11:28:32 -07002793 }
2794
Ed Tanousc1d019a2022-08-06 09:36:06 -07002795 asyncResp->res.jsonValue["Members@odata.count"] = entriesArray.size();
Nan Zhoubacb2162022-04-06 11:28:32 -07002796 BMCWEB_LOG_DEBUG << "getChassisCallback exit";
2797}
Nan Zhoue6bd8462022-06-01 04:35:35 +00002798
Nan Zhoude167a62022-06-01 04:47:45 +00002799inline void
2800 handleSensorCollectionGet(App& app, const crow::Request& req,
2801 const std::shared_ptr<bmcweb::AsyncResp>& aResp,
2802 const std::string& chassisId)
2803{
2804 query_param::QueryCapabilities capabilities = {
2805 .canDelegateExpandLevel = 1,
2806 };
2807 query_param::Query delegatedQuery;
Carson Labrado3ba00072022-06-06 19:40:56 +00002808 if (!redfish::setUpRedfishRouteWithDelegation(app, req, aResp,
Nan Zhoude167a62022-06-01 04:47:45 +00002809 delegatedQuery, capabilities))
2810 {
2811 return;
2812 }
2813
2814 if (delegatedQuery.expandType != query_param::ExpandType::None)
2815 {
2816 // we perform efficient expand.
2817 auto asyncResp = std::make_shared<SensorsAsyncResp>(
2818 aResp, chassisId, sensors::dbus::sensorPaths,
2819 sensors::node::sensors,
2820 /*efficientExpand=*/true);
2821 getChassisData(asyncResp);
2822
2823 BMCWEB_LOG_DEBUG
2824 << "SensorCollection doGet exit via efficient expand handler";
2825 return;
Ed Tanous0bad3202022-06-02 13:53:59 -07002826 }
Nan Zhoude167a62022-06-01 04:47:45 +00002827
Nan Zhoude167a62022-06-01 04:47:45 +00002828 // We get all sensors as hyperlinkes in the chassis (this
2829 // implies we reply on the default query parameters handler)
Ed Tanousc1d019a2022-08-06 09:36:06 -07002830 getChassis(aResp, chassisId, sensors::node::sensors, dbus::sensorPaths,
2831 std::bind_front(sensors::getChassisCallback, aResp, chassisId,
2832 sensors::node::sensors));
2833}
Ed Tanous7f1cc262022-08-09 13:33:57 -07002834
Ed Tanousc1d019a2022-08-06 09:36:06 -07002835inline void
2836 getSensorFromDbus(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2837 const std::string& sensorPath,
2838 const ::dbus::utility::MapperGetObject& mapperResponse)
2839{
2840 if (mapperResponse.size() != 1)
2841 {
2842 messages::internalError(asyncResp->res);
2843 return;
2844 }
2845 const auto& valueIface = *mapperResponse.begin();
2846 const std::string& connectionName = valueIface.first;
2847 BMCWEB_LOG_DEBUG << "Looking up " << connectionName;
2848 BMCWEB_LOG_DEBUG << "Path " << sensorPath;
Krzysztof Grobelnyc1343bf2022-08-31 13:15:26 +02002849
2850 sdbusplus::asio::getAllProperties(
2851 *crow::connections::systemBus, connectionName, sensorPath, "",
Ed Tanousc1d019a2022-08-06 09:36:06 -07002852 [asyncResp,
2853 sensorPath](const boost::system::error_code ec,
2854 const ::dbus::utility::DBusPropertiesMap& valuesDict) {
2855 if (ec)
2856 {
2857 messages::internalError(asyncResp->res);
2858 return;
2859 }
2860 sdbusplus::message::object_path path(sensorPath);
2861 std::string name = path.filename();
2862 path = path.parent_path();
2863 std::string type = path.filename();
2864 objectPropertiesToJson(name, type, sensors::node::sensors, valuesDict,
2865 asyncResp->res.jsonValue, nullptr);
Krzysztof Grobelnyc1343bf2022-08-31 13:15:26 +02002866 });
Nan Zhoude167a62022-06-01 04:47:45 +00002867}
2868
Nan Zhoue6bd8462022-06-01 04:35:35 +00002869inline void handleSensorGet(App& app, const crow::Request& req,
Ed Tanousc1d019a2022-08-06 09:36:06 -07002870 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous677bb752022-09-15 10:52:19 -07002871 const std::string& chassisId,
Ed Tanousc1d019a2022-08-06 09:36:06 -07002872 const std::string& sensorId)
Nan Zhoue6bd8462022-06-01 04:35:35 +00002873{
Ed Tanousc1d019a2022-08-06 09:36:06 -07002874 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Nan Zhoue6bd8462022-06-01 04:35:35 +00002875 {
2876 return;
2877 }
Ed Tanousc1d019a2022-08-06 09:36:06 -07002878 size_t index = sensorId.find('_');
2879 if (index == std::string::npos)
2880 {
2881 messages::resourceNotFound(asyncResp->res, sensorId, "Sensor");
2882 return;
2883 }
Ed Tanous677bb752022-09-15 10:52:19 -07002884 asyncResp->res.jsonValue["@odata.id"] = crow::utility::urlFromPieces(
2885 "redfish", "v1", "Chassis", chassisId, "Sensors", sensorId);
Ed Tanousc1d019a2022-08-06 09:36:06 -07002886 std::string sensorType = sensorId.substr(0, index);
2887 std::string sensorName = sensorId.substr(index + 1);
2888 // fan_pwm and fan_tach need special handling
2889 if (sensorType == "fantach" || sensorType == "fanpwm")
2890 {
2891 sensorType.insert(3, 1, '_');
2892 }
2893
Nan Zhoue6bd8462022-06-01 04:35:35 +00002894 BMCWEB_LOG_DEBUG << "Sensor doGet enter";
Nan Zhoue6bd8462022-06-01 04:35:35 +00002895
2896 const std::array<const char*, 1> interfaces = {
2897 "xyz.openbmc_project.Sensor.Value"};
Ed Tanousc1d019a2022-08-06 09:36:06 -07002898 std::string sensorPath =
2899 "/xyz/openbmc_project/sensors/" + sensorType + '/' + sensorName;
Nan Zhoue6bd8462022-06-01 04:35:35 +00002900 // Get a list of all of the sensors that implement Sensor.Value
2901 // and get the path and service name associated with the sensor
2902 crow::connections::systemBus->async_method_call(
Ed Tanousc1d019a2022-08-06 09:36:06 -07002903 [asyncResp, sensorPath,
Nan Zhoue6bd8462022-06-01 04:35:35 +00002904 sensorName](const boost::system::error_code ec,
Ed Tanousc1d019a2022-08-06 09:36:06 -07002905 const ::dbus::utility::MapperGetObject& subtree) {
Nan Zhoue6bd8462022-06-01 04:35:35 +00002906 BMCWEB_LOG_DEBUG << "respHandler1 enter";
2907 if (ec)
2908 {
Ed Tanousc1d019a2022-08-06 09:36:06 -07002909 messages::internalError(asyncResp->res);
Nan Zhoue6bd8462022-06-01 04:35:35 +00002910 BMCWEB_LOG_ERROR << "Sensor getSensorPaths resp_handler: "
2911 << "Dbus error " << ec;
2912 return;
2913 }
Ed Tanousc1d019a2022-08-06 09:36:06 -07002914 getSensorFromDbus(asyncResp, sensorPath, subtree);
Nan Zhoue6bd8462022-06-01 04:35:35 +00002915 BMCWEB_LOG_DEBUG << "respHandler1 exit";
2916 },
2917 "xyz.openbmc_project.ObjectMapper",
2918 "/xyz/openbmc_project/object_mapper",
Ed Tanousc1d019a2022-08-06 09:36:06 -07002919 "xyz.openbmc_project.ObjectMapper", "GetObject", sensorPath,
2920 interfaces);
Nan Zhoue6bd8462022-06-01 04:35:35 +00002921}
2922
Nan Zhoubacb2162022-04-06 11:28:32 -07002923} // namespace sensors
2924
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002925inline void requestRoutesSensorCollection(App& app)
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002926{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002927 BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/Sensors/")
Ed Tanoused398212021-06-09 17:05:54 -07002928 .privileges(redfish::privileges::getSensorCollection)
Ed Tanous002d39b2022-05-31 08:59:27 -07002929 .methods(boost::beast::http::verb::get)(
Nan Zhoude167a62022-06-01 04:47:45 +00002930 std::bind_front(sensors::handleSensorCollectionGet, std::ref(app)));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002931}
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002932
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002933inline void requestRoutesSensor(App& app)
2934{
2935 BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/Sensors/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07002936 .privileges(redfish::privileges::getSensor)
Ed Tanous002d39b2022-05-31 08:59:27 -07002937 .methods(boost::beast::http::verb::get)(
Nan Zhoue6bd8462022-06-01 04:35:35 +00002938 std::bind_front(sensors::handleSensorGet, std::ref(app)));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002939}
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002940
Ed Tanous1abe55e2018-09-05 08:30:59 -07002941} // namespace redfish