blob: 390a2a7bf2b50448ba5029db5c132356340e2cac [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
John Edward Broadbent7e860f12021-04-08 15:57:16 -070018#include <app.hpp>
Ed Tanous11ba3972022-07-11 09:50:41 -070019#include <boost/algorithm/string/classification.hpp>
Ed Tanous1d7c0052022-08-09 12:32:26 -070020#include <boost/algorithm/string/find.hpp>
21#include <boost/algorithm/string/predicate.hpp>
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +010022#include <boost/algorithm/string/split.hpp>
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +010023#include <boost/range/algorithm/replace_copy_if.hpp>
Ed Tanous1abe55e2018-09-05 08:30:59 -070024#include <dbus_singleton.hpp>
Ed Tanous168e20c2021-12-13 14:39:53 -080025#include <dbus_utility.hpp>
Ed Tanous45ca1b82022-03-25 13:07:27 -070026#include <query.hpp>
Ed Tanoused398212021-06-09 17:05:54 -070027#include <registries/privilege_registry.hpp>
Jonathan Doman1e1e5982021-06-11 09:36:17 -070028#include <sdbusplus/asio/property.hpp>
Krzysztof Grobelny86d89ed2022-08-29 14:49:20 +020029#include <sdbusplus/unpack_properties.hpp>
30#include <utils/dbus_utils.hpp>
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +053031#include <utils/json_utils.hpp>
Nan Zhou928fefb2022-03-28 08:45:00 -070032#include <utils/query_param.hpp>
Gunnar Mills1214b7e2020-06-04 10:11:30 -050033
34#include <cmath>
Nan Zhoufe04d492022-06-22 17:10:41 +000035#include <iterator>
36#include <map>
37#include <set>
Ed Tanousb5a76932020-09-29 16:16:58 -070038#include <utility>
Ed Tanousabf2add2019-01-22 16:40:12 -080039#include <variant>
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +010040
Ed Tanous1abe55e2018-09-05 08:30:59 -070041namespace redfish
42{
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +010043
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +020044namespace sensors
45{
46namespace node
47{
48static constexpr std::string_view power = "Power";
49static constexpr std::string_view sensors = "Sensors";
50static constexpr std::string_view thermal = "Thermal";
51} // namespace node
52
Ed Tanous02da7c52022-02-27 00:09:02 -080053// clang-format off
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +020054namespace dbus
55{
Ed Tanous4ee8e212022-05-28 09:42:51 -070056static auto powerPaths = std::to_array<std::string_view>({
Ed Tanous02da7c52022-02-27 00:09:02 -080057 "/xyz/openbmc_project/sensors/voltage",
58 "/xyz/openbmc_project/sensors/power"
59});
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +000060
Ed Tanous4ee8e212022-05-28 09:42:51 -070061static auto sensorPaths = std::to_array<std::string_view>({
Ed Tanous02da7c52022-02-27 00:09:02 -080062 "/xyz/openbmc_project/sensors/power",
63 "/xyz/openbmc_project/sensors/current",
64 "/xyz/openbmc_project/sensors/airflow",
Ed Tanous4e777662022-08-06 09:39:13 -070065 "/xyz/openbmc_project/sensors/humidity",
George Liue8204932021-02-01 14:42:49 +080066#ifdef BMCWEB_NEW_POWERSUBSYSTEM_THERMALSUBSYSTEM
Ed Tanous02da7c52022-02-27 00:09:02 -080067 "/xyz/openbmc_project/sensors/voltage",
68 "/xyz/openbmc_project/sensors/fan_tach",
69 "/xyz/openbmc_project/sensors/temperature",
70 "/xyz/openbmc_project/sensors/fan_pwm",
71 "/xyz/openbmc_project/sensors/altitude",
72 "/xyz/openbmc_project/sensors/energy",
George Liue8204932021-02-01 14:42:49 +080073#endif
Ed Tanous02da7c52022-02-27 00:09:02 -080074 "/xyz/openbmc_project/sensors/utilization"
75});
76
Ed Tanous4ee8e212022-05-28 09:42:51 -070077static auto thermalPaths = std::to_array<std::string_view>({
Ed Tanous02da7c52022-02-27 00:09:02 -080078 "/xyz/openbmc_project/sensors/fan_tach",
79 "/xyz/openbmc_project/sensors/temperature",
80 "/xyz/openbmc_project/sensors/fan_pwm"
81});
82
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +000083} // namespace dbus
Ed Tanous02da7c52022-02-27 00:09:02 -080084// clang-format on
85
86using sensorPair = std::pair<std::string_view, std::span<std::string_view>>;
87static constexpr std::array<sensorPair, 3> paths = {
88 {{node::power, std::span<std::string_view>(dbus::powerPaths)},
89 {node::sensors, std::span<std::string_view>(dbus::sensorPaths)},
90 {node::thermal, std::span<std::string_view>(dbus::thermalPaths)}}};
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +000091
Ed Tanous1d7c0052022-08-09 12:32:26 -070092inline std::string_view toReadingType(std::string_view sensorType)
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +000093{
94 if (sensorType == "voltage")
95 {
96 return "Voltage";
97 }
98 if (sensorType == "power")
99 {
100 return "Power";
101 }
102 if (sensorType == "current")
103 {
104 return "Current";
105 }
106 if (sensorType == "fan_tach")
107 {
108 return "Rotational";
109 }
110 if (sensorType == "temperature")
111 {
112 return "Temperature";
113 }
114 if (sensorType == "fan_pwm" || sensorType == "utilization")
115 {
116 return "Percent";
117 }
Gunnar Mills5deabed2022-04-20 13:43:45 -0600118 if (sensorType == "humidity")
119 {
120 return "Humidity";
121 }
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000122 if (sensorType == "altitude")
123 {
124 return "Altitude";
125 }
126 if (sensorType == "airflow")
127 {
128 return "AirFlow";
129 }
130 if (sensorType == "energy")
131 {
132 return "EnergyJoules";
133 }
134 return "";
135}
136
Ed Tanous1d7c0052022-08-09 12:32:26 -0700137inline std::string_view toReadingUnits(std::string_view sensorType)
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000138{
139 if (sensorType == "voltage")
140 {
141 return "V";
142 }
143 if (sensorType == "power")
144 {
145 return "W";
146 }
147 if (sensorType == "current")
148 {
149 return "A";
150 }
151 if (sensorType == "fan_tach")
152 {
153 return "RPM";
154 }
155 if (sensorType == "temperature")
156 {
157 return "Cel";
158 }
Gunnar Mills5deabed2022-04-20 13:43:45 -0600159 if (sensorType == "fan_pwm" || sensorType == "utilization" ||
160 sensorType == "humidity")
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000161 {
162 return "%";
163 }
164 if (sensorType == "altitude")
165 {
166 return "m";
167 }
168 if (sensorType == "airflow")
169 {
170 return "cft_i/min";
171 }
172 if (sensorType == "energy")
173 {
174 return "J";
175 }
176 return "";
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200177}
178} // namespace sensors
179
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100180/**
Kowalski, Kamil588c3f02018-04-03 14:55:27 +0200181 * SensorsAsyncResp
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100182 * Gathers data needed for response processing after async calls are done
183 */
Ed Tanous1abe55e2018-09-05 08:30:59 -0700184class SensorsAsyncResp
185{
186 public:
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200187 using DataCompleteCb = std::function<void(
188 const boost::beast::http::status status,
Nan Zhoufe04d492022-06-22 17:10:41 +0000189 const std::map<std::string, std::string>& uriToDbus)>;
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200190
191 struct SensorData
192 {
193 const std::string name;
194 std::string uri;
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200195 const std::string dbusPath;
196 };
197
Ed Tanous8a592812022-06-04 09:06:59 -0700198 SensorsAsyncResp(const std::shared_ptr<bmcweb::AsyncResp>& asyncRespIn,
zhanghch058d1b46d2021-04-01 11:18:24 +0800199 const std::string& chassisIdIn,
Ed Tanous02da7c52022-02-27 00:09:02 -0800200 std::span<std::string_view> typesIn,
201 std::string_view subNode) :
Ed Tanous8a592812022-06-04 09:06:59 -0700202 asyncResp(asyncRespIn),
Nan Zhou928fefb2022-03-28 08:45:00 -0700203 chassisId(chassisIdIn), types(typesIn), chassisSubNode(subNode),
204 efficientExpand(false)
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500205 {}
Kowalski, Kamil588c3f02018-04-03 14:55:27 +0200206
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200207 // Store extra data about sensor mapping and return it in callback
Ed Tanous8a592812022-06-04 09:06:59 -0700208 SensorsAsyncResp(const std::shared_ptr<bmcweb::AsyncResp>& asyncRespIn,
zhanghch058d1b46d2021-04-01 11:18:24 +0800209 const std::string& chassisIdIn,
Ed Tanous02da7c52022-02-27 00:09:02 -0800210 std::span<std::string_view> typesIn,
211 std::string_view subNode,
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200212 DataCompleteCb&& creationComplete) :
Ed Tanous8a592812022-06-04 09:06:59 -0700213 asyncResp(asyncRespIn),
Nan Zhou928fefb2022-03-28 08:45:00 -0700214 chassisId(chassisIdIn), types(typesIn), chassisSubNode(subNode),
215 efficientExpand(false), metadata{std::vector<SensorData>()},
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200216 dataComplete{std::move(creationComplete)}
217 {}
218
Nan Zhou928fefb2022-03-28 08:45:00 -0700219 // sensor collections expand
Ed Tanous8a592812022-06-04 09:06:59 -0700220 SensorsAsyncResp(const std::shared_ptr<bmcweb::AsyncResp>& asyncRespIn,
Nan Zhou928fefb2022-03-28 08:45:00 -0700221 const std::string& chassisIdIn,
Ed Tanous02da7c52022-02-27 00:09:02 -0800222 const std::span<std::string_view> typesIn,
Ed Tanous8a592812022-06-04 09:06:59 -0700223 const std::string_view& subNode, bool efficientExpandIn) :
224 asyncResp(asyncRespIn),
Nan Zhou928fefb2022-03-28 08:45:00 -0700225 chassisId(chassisIdIn), types(typesIn), chassisSubNode(subNode),
Ed Tanous8a592812022-06-04 09:06:59 -0700226 efficientExpand(efficientExpandIn)
Nan Zhou928fefb2022-03-28 08:45:00 -0700227 {}
228
Ed Tanous1abe55e2018-09-05 08:30:59 -0700229 ~SensorsAsyncResp()
230 {
zhanghch058d1b46d2021-04-01 11:18:24 +0800231 if (asyncResp->res.result() ==
232 boost::beast::http::status::internal_server_error)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700233 {
234 // Reset the json object to clear out any data that made it in
235 // before the error happened todo(ed) handle error condition with
236 // proper code
zhanghch058d1b46d2021-04-01 11:18:24 +0800237 asyncResp->res.jsonValue = nlohmann::json::object();
Ed Tanous1abe55e2018-09-05 08:30:59 -0700238 }
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200239
240 if (dataComplete && metadata)
241 {
Nan Zhoufe04d492022-06-22 17:10:41 +0000242 std::map<std::string, std::string> map;
zhanghch058d1b46d2021-04-01 11:18:24 +0800243 if (asyncResp->res.result() == boost::beast::http::status::ok)
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200244 {
245 for (auto& sensor : *metadata)
246 {
Ed Tanousc1d019a2022-08-06 09:36:06 -0700247 map.emplace(sensor.uri, sensor.dbusPath);
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200248 }
249 }
zhanghch058d1b46d2021-04-01 11:18:24 +0800250 dataComplete(asyncResp->res.result(), map);
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200251 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700252 }
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100253
Ed Tanousecd6a3a2022-01-07 09:18:40 -0800254 SensorsAsyncResp(const SensorsAsyncResp&) = delete;
255 SensorsAsyncResp(SensorsAsyncResp&&) = delete;
256 SensorsAsyncResp& operator=(const SensorsAsyncResp&) = delete;
257 SensorsAsyncResp& operator=(SensorsAsyncResp&&) = delete;
258
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200259 void addMetadata(const nlohmann::json& sensorObject,
Ed Tanousc1d019a2022-08-06 09:36:06 -0700260 const std::string& dbusPath)
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200261 {
262 if (metadata)
263 {
Ed Tanousc1d019a2022-08-06 09:36:06 -0700264 metadata->emplace_back(SensorData{
265 sensorObject["Name"], sensorObject["@odata.id"], dbusPath});
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200266 }
267 }
268
269 void updateUri(const std::string& name, const std::string& uri)
270 {
271 if (metadata)
272 {
273 for (auto& sensor : *metadata)
274 {
275 if (sensor.name == name)
276 {
277 sensor.uri = uri;
278 }
279 }
280 }
281 }
282
zhanghch058d1b46d2021-04-01 11:18:24 +0800283 const std::shared_ptr<bmcweb::AsyncResp> asyncResp;
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200284 const std::string chassisId;
Ed Tanous02da7c52022-02-27 00:09:02 -0800285 const std::span<std::string_view> types;
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200286 const std::string chassisSubNode;
Nan Zhou928fefb2022-03-28 08:45:00 -0700287 const bool efficientExpand;
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +0200288
289 private:
290 std::optional<std::vector<SensorData>> metadata;
291 DataCompleteCb dataComplete;
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100292};
293
294/**
Anthony Wilsond5005492019-07-31 16:34:17 -0500295 * Possible states for physical inventory leds
296 */
297enum class LedState
298{
299 OFF,
300 ON,
301 BLINK,
302 UNKNOWN
303};
304
305/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500306 * D-Bus inventory item associated with one or more sensors.
307 */
308class InventoryItem
309{
310 public:
Ed Tanous4e23a442022-06-06 09:57:26 -0700311 explicit InventoryItem(const std::string& objPath) : objectPath(objPath)
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500312 {
313 // Set inventory item name to last node of object path
George Liu28aa8de2021-02-01 15:13:30 +0800314 sdbusplus::message::object_path path(objectPath);
315 name = path.filename();
316 if (name.empty())
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500317 {
George Liu28aa8de2021-02-01 15:13:30 +0800318 BMCWEB_LOG_ERROR << "Failed to find '/' in " << objectPath;
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500319 }
320 }
321
322 std::string objectPath;
323 std::string name;
Ed Tanouse05aec52022-01-25 10:28:56 -0800324 bool isPresent = true;
325 bool isFunctional = true;
326 bool isPowerSupply = false;
327 int powerSupplyEfficiencyPercent = -1;
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500328 std::string manufacturer;
329 std::string model;
330 std::string partNumber;
331 std::string serialNumber;
332 std::set<std::string> sensors;
Anthony Wilsond5005492019-07-31 16:34:17 -0500333 std::string ledObjectPath;
Ed Tanouse05aec52022-01-25 10:28:56 -0800334 LedState ledState = LedState::UNKNOWN;
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500335};
336
337/**
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530338 * @brief Get objects with connection necessary for sensors
Kowalski, Kamil588c3f02018-04-03 14:55:27 +0200339 * @param SensorsAsyncResp Pointer to object holding response data
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100340 * @param sensorNames Sensors retrieved from chassis
341 * @param callback Callback for processing gathered connections
342 */
343template <typename Callback>
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530344void getObjectsWithConnection(
Ed Tanous81ce6092020-12-17 16:54:55 +0000345 const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
Nan Zhoufe04d492022-06-22 17:10:41 +0000346 const std::shared_ptr<std::set<std::string>>& sensorNames,
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530347 Callback&& callback)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700348{
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530349 BMCWEB_LOG_DEBUG << "getObjectsWithConnection enter";
Ed Tanous1abe55e2018-09-05 08:30:59 -0700350 const std::string path = "/xyz/openbmc_project/sensors";
351 const std::array<std::string, 1> interfaces = {
352 "xyz.openbmc_project.Sensor.Value"};
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100353
Ed Tanous1abe55e2018-09-05 08:30:59 -0700354 // Response handler for parsing objects subtree
Ed Tanous002d39b2022-05-31 08:59:27 -0700355 auto respHandler =
356 [callback{std::forward<Callback>(callback)}, sensorsAsyncResp,
357 sensorNames](const boost::system::error_code ec,
358 const dbus::utility::MapperGetSubTreeResponse& subtree) {
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530359 BMCWEB_LOG_DEBUG << "getObjectsWithConnection resp_handler enter";
Ed Tanous1abe55e2018-09-05 08:30:59 -0700360 if (ec)
361 {
zhanghch058d1b46d2021-04-01 11:18:24 +0800362 messages::internalError(sensorsAsyncResp->asyncResp->res);
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530363 BMCWEB_LOG_ERROR
364 << "getObjectsWithConnection resp_handler: Dbus error " << ec;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700365 return;
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100366 }
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100367
Ed Tanous1abe55e2018-09-05 08:30:59 -0700368 BMCWEB_LOG_DEBUG << "Found " << subtree.size() << " subtrees";
369
370 // Make unique list of connections only for requested sensor types and
371 // found in the chassis
Nan Zhoufe04d492022-06-22 17:10:41 +0000372 std::set<std::string> connections;
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530373 std::set<std::pair<std::string, std::string>> objectsWithConnection;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700374
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700375 BMCWEB_LOG_DEBUG << "sensorNames list count: " << sensorNames->size();
376 for (const std::string& tsensor : *sensorNames)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700377 {
378 BMCWEB_LOG_DEBUG << "Sensor to find: " << tsensor;
379 }
380
381 for (const std::pair<
382 std::string,
383 std::vector<std::pair<std::string, std::vector<std::string>>>>&
384 object : subtree)
385 {
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700386 if (sensorNames->find(object.first) != sensorNames->end())
Ed Tanous1abe55e2018-09-05 08:30:59 -0700387 {
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700388 for (const std::pair<std::string, std::vector<std::string>>&
389 objData : object.second)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700390 {
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700391 BMCWEB_LOG_DEBUG << "Adding connection: " << objData.first;
392 connections.insert(objData.first);
393 objectsWithConnection.insert(
394 std::make_pair(object.first, objData.first));
Ed Tanous1abe55e2018-09-05 08:30:59 -0700395 }
396 }
397 }
398 BMCWEB_LOG_DEBUG << "Found " << connections.size() << " connections";
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530399 callback(std::move(connections), std::move(objectsWithConnection));
400 BMCWEB_LOG_DEBUG << "getObjectsWithConnection resp_handler exit";
Ed Tanous1abe55e2018-09-05 08:30:59 -0700401 };
Ed Tanous1abe55e2018-09-05 08:30:59 -0700402 // Make call to ObjectMapper to find all sensors objects
403 crow::connections::systemBus->async_method_call(
404 std::move(respHandler), "xyz.openbmc_project.ObjectMapper",
405 "/xyz/openbmc_project/object_mapper",
406 "xyz.openbmc_project.ObjectMapper", "GetSubTree", path, 2, interfaces);
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530407 BMCWEB_LOG_DEBUG << "getObjectsWithConnection exit";
408}
409
410/**
411 * @brief Create connections necessary for sensors
412 * @param SensorsAsyncResp Pointer to object holding response data
413 * @param sensorNames Sensors retrieved from chassis
414 * @param callback Callback for processing gathered connections
415 */
416template <typename Callback>
Nan Zhoufe04d492022-06-22 17:10:41 +0000417void getConnections(std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
418 const std::shared_ptr<std::set<std::string>> sensorNames,
419 Callback&& callback)
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530420{
421 auto objectsWithConnectionCb =
Nan Zhoufe04d492022-06-22 17:10:41 +0000422 [callback](const std::set<std::string>& connections,
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530423 const std::set<std::pair<std::string, std::string>>&
Ed Tanous3174e4d2020-10-07 11:41:22 -0700424 /*objectsWithConnection*/) { callback(connections); };
Ed Tanous81ce6092020-12-17 16:54:55 +0000425 getObjectsWithConnection(sensorsAsyncResp, sensorNames,
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530426 std::move(objectsWithConnectionCb));
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100427}
428
429/**
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700430 * @brief Shrinks the list of sensors for processing
431 * @param SensorsAysncResp The class holding the Redfish response
432 * @param allSensors A list of all the sensors associated to the
433 * chassis element (i.e. baseboard, front panel, etc...)
434 * @param activeSensors A list that is a reduction of the incoming
435 * allSensors list. Eliminate Thermal sensors when a Power request is
436 * made, and eliminate Power sensors when a Thermal request is made.
437 */
Ed Tanous23a21a12020-07-25 04:45:05 +0000438inline void reduceSensorList(
Ed Tanous7f1cc262022-08-09 13:33:57 -0700439 crow::Response& res, std::string_view chassisSubNode,
440 std::span<std::string_view> sensorTypes,
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700441 const std::vector<std::string>* allSensors,
Nan Zhoufe04d492022-06-22 17:10:41 +0000442 const std::shared_ptr<std::set<std::string>>& activeSensors)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700443{
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700444 if ((allSensors == nullptr) || (activeSensors == nullptr))
445 {
Ed Tanous7f1cc262022-08-09 13:33:57 -0700446 messages::resourceNotFound(res, chassisSubNode,
447 chassisSubNode == sensors::node::thermal
448 ? "Temperatures"
449 : "Voltages");
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700450
451 return;
452 }
453 if (allSensors->empty())
454 {
455 // Nothing to do, the activeSensors object is also empty
456 return;
457 }
458
Ed Tanous7f1cc262022-08-09 13:33:57 -0700459 for (std::string_view type : sensorTypes)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700460 {
461 for (const std::string& sensor : *allSensors)
462 {
Ed Tanous11ba3972022-07-11 09:50:41 -0700463 if (sensor.starts_with(type))
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700464 {
465 activeSensors->emplace(sensor);
466 }
467 }
468 }
469}
470
Ed Tanous7f1cc262022-08-09 13:33:57 -0700471/*
472 *Populates the top level collection for a given subnode. Populates
473 *SensorCollection, Power, or Thermal schemas.
474 *
475 * */
476inline void populateChassisNode(nlohmann::json& jsonValue,
477 std::string_view chassisSubNode)
478{
479 if (chassisSubNode == sensors::node::power)
480 {
481 jsonValue["@odata.type"] = "#Power.v1_5_2.Power";
482 }
483 else if (chassisSubNode == sensors::node::thermal)
484 {
485 jsonValue["@odata.type"] = "#Thermal.v1_4_0.Thermal";
486 jsonValue["Fans"] = nlohmann::json::array();
487 jsonValue["Temperatures"] = nlohmann::json::array();
488 }
489 else if (chassisSubNode == sensors::node::sensors)
490 {
491 jsonValue["@odata.type"] = "#SensorCollection.SensorCollection";
492 jsonValue["Description"] = "Collection of Sensors for this Chassis";
493 jsonValue["Members"] = nlohmann::json::array();
494 jsonValue["Members@odata.count"] = 0;
495 }
496
497 if (chassisSubNode != sensors::node::sensors)
498 {
499 jsonValue["Id"] = chassisSubNode;
500 }
501 jsonValue["Name"] = chassisSubNode;
502}
503
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700504/**
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100505 * @brief Retrieves requested chassis sensors and redundancy data from DBus .
Kowalski, Kamil588c3f02018-04-03 14:55:27 +0200506 * @param SensorsAsyncResp Pointer to object holding response data
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100507 * @param callback Callback for next step in gathered sensor processing
508 */
509template <typename Callback>
Ed Tanous7f1cc262022-08-09 13:33:57 -0700510void getChassis(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
511 std::string_view chassisId, std::string_view chassisSubNode,
512 std::span<std::string_view> sensorTypes, Callback&& callback)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700513{
514 BMCWEB_LOG_DEBUG << "getChassis enter";
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500515 const std::array<const char*, 2> interfaces = {
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700516 "xyz.openbmc_project.Inventory.Item.Board",
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500517 "xyz.openbmc_project.Inventory.Item.Chassis"};
Ed Tanous002d39b2022-05-31 08:59:27 -0700518 auto respHandler =
Ed Tanous7f1cc262022-08-09 13:33:57 -0700519 [callback{std::forward<Callback>(callback)}, asyncResp,
520 chassisIdStr{std::string(chassisId)},
521 chassisSubNode{std::string(chassisSubNode)}, sensorTypes](
Ed Tanous002d39b2022-05-31 08:59:27 -0700522 const boost::system::error_code ec,
523 const dbus::utility::MapperGetSubTreePathsResponse& chassisPaths) {
Ed Tanous1abe55e2018-09-05 08:30:59 -0700524 BMCWEB_LOG_DEBUG << "getChassis respHandler enter";
525 if (ec)
526 {
527 BMCWEB_LOG_ERROR << "getChassis respHandler DBUS error: " << ec;
Ed Tanous7f1cc262022-08-09 13:33:57 -0700528 messages::internalError(asyncResp->res);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700529 return;
530 }
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700531 const std::string* chassisPath = nullptr;
532 std::string chassisName;
533 for (const std::string& chassis : chassisPaths)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700534 {
George Liu28aa8de2021-02-01 15:13:30 +0800535 sdbusplus::message::object_path path(chassis);
536 chassisName = path.filename();
537 if (chassisName.empty())
Ed Tanous1abe55e2018-09-05 08:30:59 -0700538 {
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700539 BMCWEB_LOG_ERROR << "Failed to find '/' in " << chassis;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700540 continue;
541 }
Ed Tanous7f1cc262022-08-09 13:33:57 -0700542 if (chassisName == chassisIdStr)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700543 {
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700544 chassisPath = &chassis;
545 break;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700546 }
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700547 }
548 if (chassisPath == nullptr)
549 {
Ed Tanous7f1cc262022-08-09 13:33:57 -0700550 messages::resourceNotFound(asyncResp->res, "Chassis", chassisIdStr);
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700551 return;
552 }
Ed Tanous7f1cc262022-08-09 13:33:57 -0700553 populateChassisNode(asyncResp->res.jsonValue, chassisSubNode);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700554
Ed Tanous7f1cc262022-08-09 13:33:57 -0700555 asyncResp->res.jsonValue["@odata.id"] =
556 "/redfish/v1/Chassis/" + chassisIdStr + "/" + chassisSubNode;
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500557
Shawn McCarney8fb49dd2019-06-12 17:47:00 -0500558 // Get the list of all sensors for this Chassis element
559 std::string sensorPath = *chassisPath + "/all_sensors";
Jonathan Doman1e1e5982021-06-11 09:36:17 -0700560 sdbusplus::asio::getProperty<std::vector<std::string>>(
561 *crow::connections::systemBus, "xyz.openbmc_project.ObjectMapper",
562 sensorPath, "xyz.openbmc_project.Association", "endpoints",
Ed Tanous7f1cc262022-08-09 13:33:57 -0700563 [asyncResp, chassisSubNode, sensorTypes,
Ed Tanousf94c4ec2022-01-06 12:44:41 -0800564 callback{std::forward<const Callback>(callback)}](
Ed Tanous271584a2019-07-09 16:24:22 -0700565 const boost::system::error_code& e,
Jonathan Doman1e1e5982021-06-11 09:36:17 -0700566 const std::vector<std::string>& nodeSensorList) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700567 if (e)
568 {
569 if (e.value() != EBADR)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700570 {
Ed Tanous7f1cc262022-08-09 13:33:57 -0700571 messages::internalError(asyncResp->res);
Ed Tanous002d39b2022-05-31 08:59:27 -0700572 return;
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700573 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700574 }
Nan Zhoufe04d492022-06-22 17:10:41 +0000575 const std::shared_ptr<std::set<std::string>> culledSensorList =
576 std::make_shared<std::set<std::string>>();
Ed Tanous7f1cc262022-08-09 13:33:57 -0700577 reduceSensorList(asyncResp->res, chassisSubNode, sensorTypes,
578 &nodeSensorList, culledSensorList);
579 BMCWEB_LOG_DEBUG << "Finishing with " << culledSensorList->size();
Ed Tanous002d39b2022-05-31 08:59:27 -0700580 callback(culledSensorList);
Jonathan Doman1e1e5982021-06-11 09:36:17 -0700581 });
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100582 };
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100583
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700584 // Get the Chassis Collection
Ed Tanous1abe55e2018-09-05 08:30:59 -0700585 crow::connections::systemBus->async_method_call(
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700586 respHandler, "xyz.openbmc_project.ObjectMapper",
587 "/xyz/openbmc_project/object_mapper",
588 "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths",
Ed Tanous271584a2019-07-09 16:24:22 -0700589 "/xyz/openbmc_project/inventory", 0, interfaces);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700590 BMCWEB_LOG_DEBUG << "getChassis exit";
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100591}
592
593/**
Shawn McCarneyde629b62019-03-08 10:42:51 -0600594 * @brief Finds all DBus object paths that implement ObjectManager.
595 *
596 * Creates a mapping from the associated connection name to the object path.
597 *
598 * Finds the object paths asynchronously. Invokes callback when information has
599 * been obtained.
600 *
601 * The callback must have the following signature:
602 * @code
Nan Zhoufe04d492022-06-22 17:10:41 +0000603 * callback(std::shared_ptr<std::map<std::string,std::string>> objectMgrPaths)
Shawn McCarneyde629b62019-03-08 10:42:51 -0600604 * @endcode
605 *
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700606 * @param sensorsAsyncResp Pointer to object holding response data.
Shawn McCarneyde629b62019-03-08 10:42:51 -0600607 * @param callback Callback to invoke when object paths obtained.
608 */
609template <typename Callback>
Ed Tanousb5a76932020-09-29 16:16:58 -0700610void getObjectManagerPaths(
Ed Tanous81ce6092020-12-17 16:54:55 +0000611 const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
Ed Tanousb5a76932020-09-29 16:16:58 -0700612 Callback&& callback)
Shawn McCarneyde629b62019-03-08 10:42:51 -0600613{
614 BMCWEB_LOG_DEBUG << "getObjectManagerPaths enter";
615 const std::array<std::string, 1> interfaces = {
616 "org.freedesktop.DBus.ObjectManager"};
617
618 // Response handler for GetSubTree DBus method
Ed Tanous002d39b2022-05-31 08:59:27 -0700619 auto respHandler =
620 [callback{std::forward<Callback>(callback)}, sensorsAsyncResp](
621 const boost::system::error_code ec,
622 const dbus::utility::MapperGetSubTreeResponse& subtree) {
Shawn McCarneyde629b62019-03-08 10:42:51 -0600623 BMCWEB_LOG_DEBUG << "getObjectManagerPaths respHandler enter";
624 if (ec)
625 {
zhanghch058d1b46d2021-04-01 11:18:24 +0800626 messages::internalError(sensorsAsyncResp->asyncResp->res);
Shawn McCarneyde629b62019-03-08 10:42:51 -0600627 BMCWEB_LOG_ERROR << "getObjectManagerPaths respHandler: DBus error "
628 << ec;
629 return;
630 }
631
632 // Loop over returned object paths
Nan Zhoufe04d492022-06-22 17:10:41 +0000633 std::shared_ptr<std::map<std::string, std::string>> objectMgrPaths =
634 std::make_shared<std::map<std::string, std::string>>();
Shawn McCarneyde629b62019-03-08 10:42:51 -0600635 for (const std::pair<
636 std::string,
637 std::vector<std::pair<std::string, std::vector<std::string>>>>&
638 object : subtree)
639 {
640 // Loop over connections for current object path
641 const std::string& objectPath = object.first;
642 for (const std::pair<std::string, std::vector<std::string>>&
643 objData : object.second)
644 {
645 // Add mapping from connection to object path
646 const std::string& connection = objData.first;
Shawn McCarney8fb49dd2019-06-12 17:47:00 -0500647 (*objectMgrPaths)[connection] = objectPath;
Shawn McCarneyde629b62019-03-08 10:42:51 -0600648 BMCWEB_LOG_DEBUG << "Added mapping " << connection << " -> "
649 << objectPath;
650 }
651 }
Shawn McCarney8fb49dd2019-06-12 17:47:00 -0500652 callback(objectMgrPaths);
Shawn McCarneyde629b62019-03-08 10:42:51 -0600653 BMCWEB_LOG_DEBUG << "getObjectManagerPaths respHandler exit";
654 };
655
656 // Query mapper for all DBus object paths that implement ObjectManager
657 crow::connections::systemBus->async_method_call(
658 std::move(respHandler), "xyz.openbmc_project.ObjectMapper",
659 "/xyz/openbmc_project/object_mapper",
Ed Tanous271584a2019-07-09 16:24:22 -0700660 "xyz.openbmc_project.ObjectMapper", "GetSubTree", "/", 0, interfaces);
Shawn McCarneyde629b62019-03-08 10:42:51 -0600661 BMCWEB_LOG_DEBUG << "getObjectManagerPaths exit";
662}
663
664/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500665 * @brief Returns the Redfish State value for the specified inventory item.
666 * @param inventoryItem D-Bus inventory item associated with a sensor.
667 * @return State value for inventory item.
James Feist34dd1792019-05-17 14:10:54 -0700668 */
Ed Tanous23a21a12020-07-25 04:45:05 +0000669inline std::string getState(const InventoryItem* inventoryItem)
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500670{
671 if ((inventoryItem != nullptr) && !(inventoryItem->isPresent))
672 {
673 return "Absent";
674 }
James Feist34dd1792019-05-17 14:10:54 -0700675
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500676 return "Enabled";
677}
678
679/**
680 * @brief Returns the Redfish Health value for the specified sensor.
681 * @param sensorJson Sensor JSON object.
Ed Tanous1d7c0052022-08-09 12:32:26 -0700682 * @param valuesDict Map of all sensor DBus values.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500683 * @param inventoryItem D-Bus inventory item associated with the sensor. Will
684 * be nullptr if no associated inventory item was found.
685 * @return Health value for sensor.
686 */
Ed Tanous1d7c0052022-08-09 12:32:26 -0700687inline std::string getHealth(nlohmann::json& sensorJson,
688 const dbus::utility::DBusPropertiesMap& valuesDict,
689 const InventoryItem* inventoryItem)
James Feist34dd1792019-05-17 14:10:54 -0700690{
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500691 // Get current health value (if any) in the sensor JSON object. Some JSON
692 // objects contain multiple sensors (such as PowerSupplies). We want to set
693 // the overall health to be the most severe of any of the sensors.
694 std::string currentHealth;
695 auto statusIt = sensorJson.find("Status");
696 if (statusIt != sensorJson.end())
697 {
698 auto healthIt = statusIt->find("Health");
699 if (healthIt != statusIt->end())
700 {
701 std::string* health = healthIt->get_ptr<std::string*>();
702 if (health != nullptr)
703 {
704 currentHealth = *health;
705 }
706 }
707 }
708
709 // If current health in JSON object is already Critical, return that. This
710 // should override the sensor health, which might be less severe.
711 if (currentHealth == "Critical")
712 {
713 return "Critical";
714 }
715
Krzysztof Grobelnyc1343bf2022-08-31 13:15:26 +0200716 const bool* criticalAlarmHigh = nullptr;
717 const bool* criticalAlarmLow = nullptr;
718 const bool* warningAlarmHigh = nullptr;
719 const bool* warningAlarmLow = nullptr;
Ed Tanous711ac7a2021-12-20 09:34:41 -0800720
Krzysztof Grobelnyc1343bf2022-08-31 13:15:26 +0200721 const bool success = sdbusplus::unpackPropertiesNoThrow(
722 dbus_utils::UnpackErrorPrinter(), valuesDict, "CriticalAlarmHigh",
723 criticalAlarmHigh, "CriticalAlarmLow", criticalAlarmLow,
724 "WarningAlarmHigh", warningAlarmHigh, "WarningAlarmLow",
725 warningAlarmLow);
726
727 if (success)
James Feist34dd1792019-05-17 14:10:54 -0700728 {
Krzysztof Grobelnyc1343bf2022-08-31 13:15:26 +0200729 // Check if sensor has critical threshold alarm
730 if ((criticalAlarmHigh != nullptr && *criticalAlarmHigh) ||
731 (criticalAlarmLow != nullptr && *criticalAlarmLow))
James Feist34dd1792019-05-17 14:10:54 -0700732 {
Krzysztof Grobelnyc1343bf2022-08-31 13:15:26 +0200733 return "Critical";
James Feist34dd1792019-05-17 14:10:54 -0700734 }
735 }
736
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500737 // Check if associated inventory item is not functional
738 if ((inventoryItem != nullptr) && !(inventoryItem->isFunctional))
739 {
740 return "Critical";
741 }
742
Krzysztof Grobelnyc1343bf2022-08-31 13:15:26 +0200743 // If current health in JSON object is already Warning, return that. This
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500744 // should override the sensor status, which might be less severe.
745 if (currentHealth == "Warning")
746 {
747 return "Warning";
748 }
749
Krzysztof Grobelnyc1343bf2022-08-31 13:15:26 +0200750 if (success)
James Feist34dd1792019-05-17 14:10:54 -0700751 {
Krzysztof Grobelnyc1343bf2022-08-31 13:15:26 +0200752 // Check if sensor has warning threshold alarm
753 if ((warningAlarmHigh != nullptr && *warningAlarmHigh) ||
754 (warningAlarmLow != nullptr && *warningAlarmLow))
James Feist34dd1792019-05-17 14:10:54 -0700755 {
Krzysztof Grobelnyc1343bf2022-08-31 13:15:26 +0200756 return "Warning";
James Feist34dd1792019-05-17 14:10:54 -0700757 }
758 }
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500759
James Feist34dd1792019-05-17 14:10:54 -0700760 return "OK";
761}
762
Ed Tanous23a21a12020-07-25 04:45:05 +0000763inline void setLedState(nlohmann::json& sensorJson,
Anthony Wilsond5005492019-07-31 16:34:17 -0500764 const InventoryItem* inventoryItem)
765{
766 if (inventoryItem != nullptr && !inventoryItem->ledObjectPath.empty())
767 {
768 switch (inventoryItem->ledState)
769 {
770 case LedState::OFF:
771 sensorJson["IndicatorLED"] = "Off";
772 break;
773 case LedState::ON:
774 sensorJson["IndicatorLED"] = "Lit";
775 break;
776 case LedState::BLINK:
777 sensorJson["IndicatorLED"] = "Blinking";
778 break;
Ed Tanous23a21a12020-07-25 04:45:05 +0000779 case LedState::UNKNOWN:
Anthony Wilsond5005492019-07-31 16:34:17 -0500780 break;
781 }
782 }
783}
784
James Feist34dd1792019-05-17 14:10:54 -0700785/**
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100786 * @brief Builds a json sensor representation of a sensor.
787 * @param sensorName The name of the sensor to be built
Gunnar Mills274fad52018-06-13 15:45:36 -0500788 * @param sensorType The type (temperature, fan_tach, etc) of the sensor to
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100789 * build
Ed Tanous1d7c0052022-08-09 12:32:26 -0700790 * @param chassisSubNode The subnode (thermal, sensor, ect) of the sensor
791 * @param propertiesDict A dictionary of the properties to build the sensor
792 * from.
793 * @param sensorJson The json object to fill
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500794 * @param inventoryItem D-Bus inventory item associated with the sensor. Will
795 * be nullptr if no associated inventory item was found.
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +0100796 */
Ed Tanous1d7c0052022-08-09 12:32:26 -0700797inline void objectPropertiesToJson(
798 std::string_view sensorName, std::string_view sensorType,
799 std::string_view chassisSubNode,
800 const dbus::utility::DBusPropertiesMap& propertiesDict,
Ed Tanous81ce6092020-12-17 16:54:55 +0000801 nlohmann::json& sensorJson, InventoryItem* inventoryItem)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700802{
Ed Tanous1d7c0052022-08-09 12:32:26 -0700803 if (chassisSubNode == sensors::node::sensors)
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500804 {
Ed Tanousc1d019a2022-08-06 09:36:06 -0700805 std::string subNodeEscaped(chassisSubNode);
806 subNodeEscaped.erase(
807 std::remove(subNodeEscaped.begin(), subNodeEscaped.end(), '_'),
808 subNodeEscaped.end());
809
810 // For sensors in SensorCollection we set Id instead of MemberId,
811 // including power sensors.
812 subNodeEscaped += '_';
813 subNodeEscaped += sensorName;
814 sensorJson["Id"] = std::move(subNodeEscaped);
815
Ed Tanous1d7c0052022-08-09 12:32:26 -0700816 std::string sensorNameEs(sensorName);
817 std::replace(sensorNameEs.begin(), sensorNameEs.end(), '_', ' ');
818 sensorJson["Name"] = std::move(sensorNameEs);
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500819 }
820 else if (sensorType != "power")
821 {
822 // Set MemberId and Name for non-power sensors. For PowerSupplies and
823 // PowerControl, those properties have more general values because
824 // multiple sensors can be stored in the same JSON object.
Ed Tanous81ce6092020-12-17 16:54:55 +0000825 sensorJson["MemberId"] = sensorName;
Ed Tanous1d7c0052022-08-09 12:32:26 -0700826 std::string sensorNameEs(sensorName);
827 std::replace(sensorNameEs.begin(), sensorNameEs.end(), '_', ' ');
828 sensorJson["Name"] = std::move(sensorNameEs);
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500829 }
Ed Tanouse742b6c2019-05-03 15:06:53 -0700830
Ed Tanous81ce6092020-12-17 16:54:55 +0000831 sensorJson["Status"]["State"] = getState(inventoryItem);
832 sensorJson["Status"]["Health"] =
Ed Tanous1d7c0052022-08-09 12:32:26 -0700833 getHealth(sensorJson, propertiesDict, inventoryItem);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700834
835 // Parameter to set to override the type we get from dbus, and force it to
836 // int, regardless of what is available. This is used for schemas like fan,
837 // that require integers, not floats.
838 bool forceToInt = false;
839
Anthony Wilson3929aca2019-07-19 15:42:33 -0500840 nlohmann::json::json_pointer unit("/Reading");
Ed Tanous1d7c0052022-08-09 12:32:26 -0700841 if (chassisSubNode == sensors::node::sensors)
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500842 {
Shounak Mitra2a4ba192022-06-01 23:34:12 +0000843 sensorJson["@odata.type"] = "#Sensor.v1_2_0.Sensor";
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000844
Ed Tanous1d7c0052022-08-09 12:32:26 -0700845 std::string_view readingType = sensors::toReadingType(sensorType);
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000846 if (readingType.empty())
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500847 {
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000848 BMCWEB_LOG_ERROR << "Redfish cannot map reading type for "
849 << sensorType;
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500850 }
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000851 else
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500852 {
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000853 sensorJson["ReadingType"] = readingType;
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500854 }
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000855
Ed Tanous1d7c0052022-08-09 12:32:26 -0700856 std::string_view readingUnits = sensors::toReadingUnits(sensorType);
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000857 if (readingUnits.empty())
Adrian Ambrożewiczf8ede152020-06-02 13:26:33 +0200858 {
Wludzik, Jozefc2bf7f92021-03-08 14:35:54 +0000859 BMCWEB_LOG_ERROR << "Redfish cannot map reading unit for "
860 << sensorType;
861 }
862 else
863 {
864 sensorJson["ReadingUnits"] = readingUnits;
Adrian Ambrożewiczf8ede152020-06-02 13:26:33 +0200865 }
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500866 }
867 else if (sensorType == "temperature")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700868 {
Anthony Wilson3929aca2019-07-19 15:42:33 -0500869 unit = "/ReadingCelsius"_json_pointer;
Ed Tanous81ce6092020-12-17 16:54:55 +0000870 sensorJson["@odata.type"] = "#Thermal.v1_3_0.Temperature";
Ed Tanous1abe55e2018-09-05 08:30:59 -0700871 // TODO(ed) Documentation says that path should be type fan_tach,
872 // implementation seems to implement fan
873 }
874 else if (sensorType == "fan" || sensorType == "fan_tach")
875 {
Anthony Wilson3929aca2019-07-19 15:42:33 -0500876 unit = "/Reading"_json_pointer;
Ed Tanous81ce6092020-12-17 16:54:55 +0000877 sensorJson["ReadingUnits"] = "RPM";
878 sensorJson["@odata.type"] = "#Thermal.v1_3_0.Fan";
879 setLedState(sensorJson, inventoryItem);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700880 forceToInt = true;
881 }
Ed Tanous6f6d0d32018-10-12 11:16:43 -0700882 else if (sensorType == "fan_pwm")
883 {
Anthony Wilson3929aca2019-07-19 15:42:33 -0500884 unit = "/Reading"_json_pointer;
Ed Tanous81ce6092020-12-17 16:54:55 +0000885 sensorJson["ReadingUnits"] = "Percent";
886 sensorJson["@odata.type"] = "#Thermal.v1_3_0.Fan";
887 setLedState(sensorJson, inventoryItem);
Ed Tanous6f6d0d32018-10-12 11:16:43 -0700888 forceToInt = true;
889 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700890 else if (sensorType == "voltage")
891 {
Anthony Wilson3929aca2019-07-19 15:42:33 -0500892 unit = "/ReadingVolts"_json_pointer;
Ed Tanous81ce6092020-12-17 16:54:55 +0000893 sensorJson["@odata.type"] = "#Power.v1_0_0.Voltage";
Ed Tanous1abe55e2018-09-05 08:30:59 -0700894 }
Ed Tanous2474adf2018-09-05 16:31:16 -0700895 else if (sensorType == "power")
896 {
Ed Tanous1d7c0052022-08-09 12:32:26 -0700897 if (boost::iequals(sensorName, "total_power"))
Eddie James028f7eb2019-05-17 21:24:36 +0000898 {
Ed Tanous81ce6092020-12-17 16:54:55 +0000899 sensorJson["@odata.type"] = "#Power.v1_0_0.PowerControl";
Gunnar Mills7ab06f42019-07-02 13:07:16 -0500900 // Put multiple "sensors" into a single PowerControl, so have
901 // generic names for MemberId and Name. Follows Redfish mockup.
Ed Tanous81ce6092020-12-17 16:54:55 +0000902 sensorJson["MemberId"] = "0";
903 sensorJson["Name"] = "Chassis Power Control";
Anthony Wilson3929aca2019-07-19 15:42:33 -0500904 unit = "/PowerConsumedWatts"_json_pointer;
Eddie James028f7eb2019-05-17 21:24:36 +0000905 }
Ed Tanous1d7c0052022-08-09 12:32:26 -0700906 else if (boost::ifind_first(sensorName, "input").empty())
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700907 {
Anthony Wilson3929aca2019-07-19 15:42:33 -0500908 unit = "/PowerInputWatts"_json_pointer;
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700909 }
910 else
911 {
Anthony Wilson3929aca2019-07-19 15:42:33 -0500912 unit = "/PowerOutputWatts"_json_pointer;
Johnathan Mantey49c53ac2019-05-02 09:22:38 -0700913 }
Ed Tanous2474adf2018-09-05 16:31:16 -0700914 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700915 else
916 {
917 BMCWEB_LOG_ERROR << "Redfish cannot map object type for " << sensorName;
918 return;
919 }
920 // Map of dbus interface name, dbus property name and redfish property_name
Anthony Wilson3929aca2019-07-19 15:42:33 -0500921 std::vector<
922 std::tuple<const char*, const char*, nlohmann::json::json_pointer>>
923 properties;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700924 properties.reserve(7);
925
926 properties.emplace_back("xyz.openbmc_project.Sensor.Value", "Value", unit);
Shawn McCarneyde629b62019-03-08 10:42:51 -0600927
Ed Tanous1d7c0052022-08-09 12:32:26 -0700928 if (chassisSubNode == sensors::node::sensors)
Anthony Wilson3929aca2019-07-19 15:42:33 -0500929 {
930 properties.emplace_back(
931 "xyz.openbmc_project.Sensor.Threshold.Warning", "WarningHigh",
932 "/Thresholds/UpperCaution/Reading"_json_pointer);
933 properties.emplace_back(
934 "xyz.openbmc_project.Sensor.Threshold.Warning", "WarningLow",
935 "/Thresholds/LowerCaution/Reading"_json_pointer);
936 properties.emplace_back(
937 "xyz.openbmc_project.Sensor.Threshold.Critical", "CriticalHigh",
938 "/Thresholds/UpperCritical/Reading"_json_pointer);
939 properties.emplace_back(
940 "xyz.openbmc_project.Sensor.Threshold.Critical", "CriticalLow",
941 "/Thresholds/LowerCritical/Reading"_json_pointer);
942 }
943 else if (sensorType != "power")
Shawn McCarneyde629b62019-03-08 10:42:51 -0600944 {
945 properties.emplace_back("xyz.openbmc_project.Sensor.Threshold.Warning",
Anthony Wilson3929aca2019-07-19 15:42:33 -0500946 "WarningHigh",
947 "/UpperThresholdNonCritical"_json_pointer);
Shawn McCarneyde629b62019-03-08 10:42:51 -0600948 properties.emplace_back("xyz.openbmc_project.Sensor.Threshold.Warning",
Anthony Wilson3929aca2019-07-19 15:42:33 -0500949 "WarningLow",
950 "/LowerThresholdNonCritical"_json_pointer);
Shawn McCarneyde629b62019-03-08 10:42:51 -0600951 properties.emplace_back("xyz.openbmc_project.Sensor.Threshold.Critical",
Anthony Wilson3929aca2019-07-19 15:42:33 -0500952 "CriticalHigh",
953 "/UpperThresholdCritical"_json_pointer);
Shawn McCarneyde629b62019-03-08 10:42:51 -0600954 properties.emplace_back("xyz.openbmc_project.Sensor.Threshold.Critical",
Anthony Wilson3929aca2019-07-19 15:42:33 -0500955 "CriticalLow",
956 "/LowerThresholdCritical"_json_pointer);
Shawn McCarneyde629b62019-03-08 10:42:51 -0600957 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700958
Ed Tanous2474adf2018-09-05 16:31:16 -0700959 // TODO Need to get UpperThresholdFatal and LowerThresholdFatal
960
Ed Tanous1d7c0052022-08-09 12:32:26 -0700961 if (chassisSubNode == sensors::node::sensors)
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500962 {
963 properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MinValue",
Anthony Wilson3929aca2019-07-19 15:42:33 -0500964 "/ReadingRangeMin"_json_pointer);
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500965 properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MaxValue",
Anthony Wilson3929aca2019-07-19 15:42:33 -0500966 "/ReadingRangeMax"_json_pointer);
Anthony Wilson95a3eca2019-06-11 10:44:47 -0500967 }
968 else if (sensorType == "temperature")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700969 {
970 properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MinValue",
Anthony Wilson3929aca2019-07-19 15:42:33 -0500971 "/MinReadingRangeTemp"_json_pointer);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700972 properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MaxValue",
Anthony Wilson3929aca2019-07-19 15:42:33 -0500973 "/MaxReadingRangeTemp"_json_pointer);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700974 }
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -0500975 else if (sensorType != "power")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700976 {
977 properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MinValue",
Anthony Wilson3929aca2019-07-19 15:42:33 -0500978 "/MinReadingRange"_json_pointer);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700979 properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MaxValue",
Anthony Wilson3929aca2019-07-19 15:42:33 -0500980 "/MaxReadingRange"_json_pointer);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700981 }
982
Anthony Wilson3929aca2019-07-19 15:42:33 -0500983 for (const std::tuple<const char*, const char*,
984 nlohmann::json::json_pointer>& p : properties)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700985 {
Ed Tanous1d7c0052022-08-09 12:32:26 -0700986 for (const auto& [valueName, valueVariant] : propertiesDict)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700987 {
Ed Tanous1d7c0052022-08-09 12:32:26 -0700988 if (valueName != std::get<1>(p))
Ed Tanous1abe55e2018-09-05 08:30:59 -0700989 {
Ed Tanous711ac7a2021-12-20 09:34:41 -0800990 continue;
991 }
Ed Tanous1d7c0052022-08-09 12:32:26 -0700992
993 // The property we want to set may be nested json, so use
994 // a json_pointer for easy indexing into the json structure.
995 const nlohmann::json::json_pointer& key = std::get<2>(p);
996
Ed Tanous1d7c0052022-08-09 12:32:26 -0700997 const double* doubleValue = std::get_if<double>(&valueVariant);
Ed Tanous40e4f382022-08-09 18:42:51 -0700998 if (doubleValue == nullptr)
Ed Tanous711ac7a2021-12-20 09:34:41 -0800999 {
Ed Tanous40e4f382022-08-09 18:42:51 -07001000 BMCWEB_LOG_ERROR << "Got value interface that wasn't double";
Ed Tanous1d7c0052022-08-09 12:32:26 -07001001 continue;
1002 }
Ed Tanous1d7c0052022-08-09 12:32:26 -07001003 if (forceToInt)
1004 {
Ed Tanous40e4f382022-08-09 18:42:51 -07001005 sensorJson[key] = static_cast<int64_t>(*doubleValue);
Ed Tanous1d7c0052022-08-09 12:32:26 -07001006 }
1007 else
1008 {
Ed Tanous40e4f382022-08-09 18:42:51 -07001009 sensorJson[key] = *doubleValue;
Ed Tanous1abe55e2018-09-05 08:30:59 -07001010 }
1011 }
1012 }
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +01001013}
1014
Ed Tanous1d7c0052022-08-09 12:32:26 -07001015/**
1016 * @brief Builds a json sensor representation of a sensor.
1017 * @param sensorName The name of the sensor to be built
1018 * @param sensorType The type (temperature, fan_tach, etc) of the sensor to
1019 * build
1020 * @param chassisSubNode The subnode (thermal, sensor, ect) of the sensor
1021 * @param interfacesDict A dictionary of the interfaces and properties of said
1022 * interfaces to be built from
1023 * @param sensorJson The json object to fill
1024 * @param inventoryItem D-Bus inventory item associated with the sensor. Will
1025 * be nullptr if no associated inventory item was found.
1026 */
1027inline void objectInterfacesToJson(
1028 const std::string& sensorName, const std::string& sensorType,
1029 const std::string& chassisSubNode,
1030 const dbus::utility::DBusInteracesMap& interfacesDict,
1031 nlohmann::json& sensorJson, InventoryItem* inventoryItem)
1032{
1033
1034 for (const auto& [interface, valuesDict] : interfacesDict)
1035 {
1036 objectPropertiesToJson(sensorName, sensorType, chassisSubNode,
1037 valuesDict, sensorJson, inventoryItem);
1038 }
Ed Tanousc1d019a2022-08-06 09:36:06 -07001039 BMCWEB_LOG_DEBUG << "Added sensor " << sensorName;
Ed Tanous1d7c0052022-08-09 12:32:26 -07001040}
1041
Ed Tanousb5a76932020-09-29 16:16:58 -07001042inline void populateFanRedundancy(
1043 const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp)
James Feist8bd25cc2019-03-15 15:14:00 -07001044{
1045 crow::connections::systemBus->async_method_call(
Ed Tanousb9d36b42022-02-26 21:42:46 -08001046 [sensorsAsyncResp](
1047 const boost::system::error_code ec,
1048 const dbus::utility::MapperGetSubTreeResponse& resp) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001049 if (ec)
1050 {
1051 return; // don't have to have this interface
1052 }
1053 for (const std::pair<
1054 std::string,
1055 std::vector<std::pair<std::string, std::vector<std::string>>>>&
1056 pathPair : resp)
1057 {
1058 const std::string& path = pathPair.first;
1059 const std::vector<std::pair<std::string, std::vector<std::string>>>&
1060 objDict = pathPair.second;
1061 if (objDict.empty())
James Feist8bd25cc2019-03-15 15:14:00 -07001062 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001063 continue; // this should be impossible
James Feist8bd25cc2019-03-15 15:14:00 -07001064 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001065
1066 const std::string& owner = objDict.begin()->first;
1067 sdbusplus::asio::getProperty<std::vector<std::string>>(
1068 *crow::connections::systemBus,
1069 "xyz.openbmc_project.ObjectMapper", path + "/chassis",
1070 "xyz.openbmc_project.Association", "endpoints",
1071 [path, owner,
1072 sensorsAsyncResp](const boost::system::error_code e,
1073 const std::vector<std::string>& endpoints) {
1074 if (e)
James Feist8bd25cc2019-03-15 15:14:00 -07001075 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001076 return; // if they don't have an association we
1077 // can't tell what chassis is
James Feist8bd25cc2019-03-15 15:14:00 -07001078 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001079 auto found =
1080 std::find_if(endpoints.begin(), endpoints.end(),
1081 [sensorsAsyncResp](const std::string& entry) {
1082 return entry.find(sensorsAsyncResp->chassisId) !=
1083 std::string::npos;
1084 });
James Feist8bd25cc2019-03-15 15:14:00 -07001085
Ed Tanous002d39b2022-05-31 08:59:27 -07001086 if (found == endpoints.end())
1087 {
1088 return;
1089 }
Krzysztof Grobelny86d89ed2022-08-29 14:49:20 +02001090 sdbusplus::asio::getAllProperties(
1091 *crow::connections::systemBus, owner, path,
1092 "xyz.openbmc_project.Control.FanRedundancy",
Ed Tanous002d39b2022-05-31 08:59:27 -07001093 [path, sensorsAsyncResp](
1094 const boost::system::error_code& err,
Krzysztof Grobelny86d89ed2022-08-29 14:49:20 +02001095 const dbus::utility::DBusPropertiesMap& ret) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001096 if (err)
1097 {
1098 return; // don't have to have this
1099 // interface
1100 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001101
Krzysztof Grobelny86d89ed2022-08-29 14:49:20 +02001102 const uint8_t* allowedFailures = nullptr;
1103 const std::vector<std::string>* collection = nullptr;
1104 const std::string* status = nullptr;
1105
1106 const bool success = sdbusplus::unpackPropertiesNoThrow(
1107 dbus_utils::UnpackErrorPrinter(), ret,
1108 "AllowedFailures", allowedFailures, "Collection",
1109 collection, "Status", status);
1110
1111 if (!success)
1112 {
1113 messages::internalError(
1114 sensorsAsyncResp->asyncResp->res);
1115 return;
1116 }
1117
1118 if (allowedFailures == nullptr || collection == nullptr ||
1119 status == nullptr)
Ed Tanous002d39b2022-05-31 08:59:27 -07001120 {
1121 BMCWEB_LOG_ERROR << "Invalid redundancy interface";
1122 messages::internalError(
1123 sensorsAsyncResp->asyncResp->res);
1124 return;
1125 }
1126
Ed Tanous002d39b2022-05-31 08:59:27 -07001127 sdbusplus::message::object_path objectPath(path);
1128 std::string name = objectPath.filename();
1129 if (name.empty())
1130 {
1131 // this should be impossible
1132 messages::internalError(
1133 sensorsAsyncResp->asyncResp->res);
1134 return;
1135 }
1136 std::replace(name.begin(), name.end(), '_', ' ');
1137
1138 std::string health;
1139
Ed Tanous11ba3972022-07-11 09:50:41 -07001140 if (status->ends_with("Full"))
Ed Tanous002d39b2022-05-31 08:59:27 -07001141 {
1142 health = "OK";
1143 }
Ed Tanous11ba3972022-07-11 09:50:41 -07001144 else if (status->ends_with("Degraded"))
Ed Tanous002d39b2022-05-31 08:59:27 -07001145 {
1146 health = "Warning";
1147 }
1148 else
1149 {
1150 health = "Critical";
1151 }
1152 nlohmann::json::array_t redfishCollection;
1153 const auto& fanRedfish =
1154 sensorsAsyncResp->asyncResp->res.jsonValue["Fans"];
1155 for (const std::string& item : *collection)
1156 {
Ed Tanous8a592812022-06-04 09:06:59 -07001157 sdbusplus::message::object_path itemPath(item);
1158 std::string itemName = itemPath.filename();
Ed Tanous002d39b2022-05-31 08:59:27 -07001159 if (itemName.empty())
James Feist8bd25cc2019-03-15 15:14:00 -07001160 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001161 continue;
James Feist8bd25cc2019-03-15 15:14:00 -07001162 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001163 /*
1164 todo(ed): merge patch that fixes the names
1165 std::replace(itemName.begin(),
1166 itemName.end(), '_', ' ');*/
1167 auto schemaItem =
1168 std::find_if(fanRedfish.begin(), fanRedfish.end(),
1169 [itemName](const nlohmann::json& fan) {
1170 return fan["MemberId"] == itemName;
James Feist8bd25cc2019-03-15 15:14:00 -07001171 });
Ed Tanous002d39b2022-05-31 08:59:27 -07001172 if (schemaItem != fanRedfish.end())
James Feist8bd25cc2019-03-15 15:14:00 -07001173 {
Ed Tanous8a592812022-06-04 09:06:59 -07001174 nlohmann::json::object_t collectionId;
1175 collectionId["@odata.id"] =
Ed Tanous002d39b2022-05-31 08:59:27 -07001176 (*schemaItem)["@odata.id"];
1177 redfishCollection.emplace_back(
Ed Tanous8a592812022-06-04 09:06:59 -07001178 std::move(collectionId));
Ed Tanous002d39b2022-05-31 08:59:27 -07001179 }
1180 else
1181 {
1182 BMCWEB_LOG_ERROR << "failed to find fan in schema";
1183 messages::internalError(
1184 sensorsAsyncResp->asyncResp->res);
James Feist8bd25cc2019-03-15 15:14:00 -07001185 return;
1186 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001187 }
James Feist8bd25cc2019-03-15 15:14:00 -07001188
Ed Tanous002d39b2022-05-31 08:59:27 -07001189 size_t minNumNeeded =
1190 collection->empty()
1191 ? 0
1192 : collection->size() - *allowedFailures;
1193 nlohmann::json& jResp = sensorsAsyncResp->asyncResp->res
1194 .jsonValue["Redundancy"];
James Feist8bd25cc2019-03-15 15:14:00 -07001195
Ed Tanous002d39b2022-05-31 08:59:27 -07001196 nlohmann::json::object_t redundancy;
1197 redundancy["@odata.id"] =
1198 "/redfish/v1/Chassis/" + sensorsAsyncResp->chassisId +
1199 "/" + sensorsAsyncResp->chassisSubNode +
1200 "#/Redundancy/" + std::to_string(jResp.size());
1201 redundancy["@odata.type"] = "#Redundancy.v1_3_2.Redundancy";
1202 redundancy["MinNumNeeded"] = minNumNeeded;
1203 redundancy["MemberId"] = name;
1204 redundancy["Mode"] = "N+m";
1205 redundancy["Name"] = name;
1206 redundancy["RedundancySet"] = redfishCollection;
1207 redundancy["Status"]["Health"] = health;
1208 redundancy["Status"]["State"] = "Enabled";
James Feist8bd25cc2019-03-15 15:14:00 -07001209
Ed Tanous002d39b2022-05-31 08:59:27 -07001210 jResp.push_back(std::move(redundancy));
Krzysztof Grobelny86d89ed2022-08-29 14:49:20 +02001211 });
Ed Tanous002d39b2022-05-31 08:59:27 -07001212 });
1213 }
James Feist8bd25cc2019-03-15 15:14:00 -07001214 },
1215 "xyz.openbmc_project.ObjectMapper",
1216 "/xyz/openbmc_project/object_mapper",
1217 "xyz.openbmc_project.ObjectMapper", "GetSubTree",
1218 "/xyz/openbmc_project/control", 2,
1219 std::array<const char*, 1>{
1220 "xyz.openbmc_project.Control.FanRedundancy"});
1221}
1222
Ed Tanousb5a76932020-09-29 16:16:58 -07001223inline void
Ed Tanous81ce6092020-12-17 16:54:55 +00001224 sortJSONResponse(const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07001225{
zhanghch058d1b46d2021-04-01 11:18:24 +08001226 nlohmann::json& response = sensorsAsyncResp->asyncResp->res.jsonValue;
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07001227 std::array<std::string, 2> sensorHeaders{"Temperatures", "Fans"};
Ed Tanous81ce6092020-12-17 16:54:55 +00001228 if (sensorsAsyncResp->chassisSubNode == sensors::node::power)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07001229 {
1230 sensorHeaders = {"Voltages", "PowerSupplies"};
1231 }
1232 for (const std::string& sensorGroup : sensorHeaders)
1233 {
1234 nlohmann::json::iterator entry = response.find(sensorGroup);
1235 if (entry != response.end())
1236 {
1237 std::sort(entry->begin(), entry->end(),
Ed Tanous02cad962022-06-30 16:50:15 -07001238 [](const nlohmann::json& c1, const nlohmann::json& c2) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001239 return c1["Name"] < c2["Name"];
1240 });
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07001241
1242 // add the index counts to the end of each entry
1243 size_t count = 0;
1244 for (nlohmann::json& sensorJson : *entry)
1245 {
1246 nlohmann::json::iterator odata = sensorJson.find("@odata.id");
1247 if (odata == sensorJson.end())
1248 {
1249 continue;
1250 }
1251 std::string* value = odata->get_ptr<std::string*>();
1252 if (value != nullptr)
1253 {
1254 *value += std::to_string(count);
1255 count++;
Ed Tanous81ce6092020-12-17 16:54:55 +00001256 sensorsAsyncResp->updateUri(sensorJson["Name"], *value);
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07001257 }
1258 }
1259 }
1260 }
1261}
1262
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +01001263/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001264 * @brief Finds the inventory item with the specified object path.
1265 * @param inventoryItems D-Bus inventory items associated with sensors.
1266 * @param invItemObjPath D-Bus object path of inventory item.
1267 * @return Inventory item within vector, or nullptr if no match found.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001268 */
Ed Tanous23a21a12020-07-25 04:45:05 +00001269inline InventoryItem* findInventoryItem(
Ed Tanousb5a76932020-09-29 16:16:58 -07001270 const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems,
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001271 const std::string& invItemObjPath)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001272{
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001273 for (InventoryItem& inventoryItem : *inventoryItems)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001274 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001275 if (inventoryItem.objectPath == invItemObjPath)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001276 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001277 return &inventoryItem;
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001278 }
1279 }
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001280 return nullptr;
1281}
1282
1283/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001284 * @brief Finds the inventory item associated with the specified sensor.
1285 * @param inventoryItems D-Bus inventory items associated with sensors.
1286 * @param sensorObjPath D-Bus object path of sensor.
1287 * @return Inventory item within vector, or nullptr if no match found.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001288 */
Ed Tanous23a21a12020-07-25 04:45:05 +00001289inline InventoryItem* findInventoryItemForSensor(
Ed Tanousb5a76932020-09-29 16:16:58 -07001290 const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems,
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001291 const std::string& sensorObjPath)
1292{
1293 for (InventoryItem& inventoryItem : *inventoryItems)
1294 {
1295 if (inventoryItem.sensors.count(sensorObjPath) > 0)
1296 {
1297 return &inventoryItem;
1298 }
1299 }
1300 return nullptr;
1301}
1302
1303/**
Anthony Wilsond5005492019-07-31 16:34:17 -05001304 * @brief Finds the inventory item associated with the specified led path.
1305 * @param inventoryItems D-Bus inventory items associated with sensors.
1306 * @param ledObjPath D-Bus object path of led.
1307 * @return Inventory item within vector, or nullptr if no match found.
1308 */
1309inline InventoryItem*
1310 findInventoryItemForLed(std::vector<InventoryItem>& inventoryItems,
1311 const std::string& ledObjPath)
1312{
1313 for (InventoryItem& inventoryItem : inventoryItems)
1314 {
1315 if (inventoryItem.ledObjectPath == ledObjPath)
1316 {
1317 return &inventoryItem;
1318 }
1319 }
1320 return nullptr;
1321}
1322
1323/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001324 * @brief Adds inventory item and associated sensor to specified vector.
1325 *
1326 * Adds a new InventoryItem to the vector if necessary. Searches for an
1327 * existing InventoryItem with the specified object path. If not found, one is
1328 * added to the vector.
1329 *
1330 * Next, the specified sensor is added to the set of sensors associated with the
1331 * InventoryItem.
1332 *
1333 * @param inventoryItems D-Bus inventory items associated with sensors.
1334 * @param invItemObjPath D-Bus object path of inventory item.
1335 * @param sensorObjPath D-Bus object path of sensor
1336 */
Ed Tanousb5a76932020-09-29 16:16:58 -07001337inline void addInventoryItem(
1338 const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems,
1339 const std::string& invItemObjPath, const std::string& sensorObjPath)
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001340{
1341 // Look for inventory item in vector
1342 InventoryItem* inventoryItem =
1343 findInventoryItem(inventoryItems, invItemObjPath);
1344
1345 // If inventory item doesn't exist in vector, add it
1346 if (inventoryItem == nullptr)
1347 {
1348 inventoryItems->emplace_back(invItemObjPath);
1349 inventoryItem = &(inventoryItems->back());
1350 }
1351
1352 // Add sensor to set of sensors associated with inventory item
1353 inventoryItem->sensors.emplace(sensorObjPath);
1354}
1355
1356/**
1357 * @brief Stores D-Bus data in the specified inventory item.
1358 *
1359 * Finds D-Bus data in the specified map of interfaces. Stores the data in the
1360 * specified InventoryItem.
1361 *
1362 * This data is later used to provide sensor property values in the JSON
1363 * response.
1364 *
1365 * @param inventoryItem Inventory item where data will be stored.
1366 * @param interfacesDict Map containing D-Bus interfaces and their properties
1367 * for the specified inventory item.
1368 */
Ed Tanous23a21a12020-07-25 04:45:05 +00001369inline void storeInventoryItemData(
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001370 InventoryItem& inventoryItem,
Ed Tanous711ac7a2021-12-20 09:34:41 -08001371 const dbus::utility::DBusInteracesMap& interfacesDict)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001372{
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001373 // Get properties from Inventory.Item interface
Ed Tanous711ac7a2021-12-20 09:34:41 -08001374
Ed Tanous9eb808c2022-01-25 10:19:23 -08001375 for (const auto& [interface, values] : interfacesDict)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001376 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001377 if (interface == "xyz.openbmc_project.Inventory.Item")
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001378 {
Ed Tanous9eb808c2022-01-25 10:19:23 -08001379 for (const auto& [name, dbusValue] : values)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001380 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001381 if (name == "Present")
1382 {
1383 const bool* value = std::get_if<bool>(&dbusValue);
1384 if (value != nullptr)
1385 {
1386 inventoryItem.isPresent = *value;
1387 }
1388 }
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001389 }
1390 }
Ed Tanous711ac7a2021-12-20 09:34:41 -08001391 // Check if Inventory.Item.PowerSupply interface is present
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001392
Ed Tanous711ac7a2021-12-20 09:34:41 -08001393 if (interface == "xyz.openbmc_project.Inventory.Item.PowerSupply")
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001394 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001395 inventoryItem.isPowerSupply = true;
1396 }
1397
1398 // Get properties from Inventory.Decorator.Asset interface
1399 if (interface == "xyz.openbmc_project.Inventory.Decorator.Asset")
1400 {
Ed Tanous9eb808c2022-01-25 10:19:23 -08001401 for (const auto& [name, dbusValue] : values)
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001402 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001403 if (name == "Manufacturer")
1404 {
1405 const std::string* value =
1406 std::get_if<std::string>(&dbusValue);
1407 if (value != nullptr)
1408 {
1409 inventoryItem.manufacturer = *value;
1410 }
1411 }
1412 if (name == "Model")
1413 {
1414 const std::string* value =
1415 std::get_if<std::string>(&dbusValue);
1416 if (value != nullptr)
1417 {
1418 inventoryItem.model = *value;
1419 }
1420 }
1421 if (name == "SerialNumber")
1422 {
1423 const std::string* value =
1424 std::get_if<std::string>(&dbusValue);
1425 if (value != nullptr)
1426 {
1427 inventoryItem.serialNumber = *value;
1428 }
1429 }
1430 if (name == "PartNumber")
1431 {
1432 const std::string* value =
1433 std::get_if<std::string>(&dbusValue);
1434 if (value != nullptr)
1435 {
1436 inventoryItem.partNumber = *value;
1437 }
1438 }
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001439 }
1440 }
1441
Ed Tanous711ac7a2021-12-20 09:34:41 -08001442 if (interface ==
1443 "xyz.openbmc_project.State.Decorator.OperationalStatus")
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001444 {
Ed Tanous9eb808c2022-01-25 10:19:23 -08001445 for (const auto& [name, dbusValue] : values)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001446 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001447 if (name == "Functional")
1448 {
1449 const bool* value = std::get_if<bool>(&dbusValue);
1450 if (value != nullptr)
1451 {
1452 inventoryItem.isFunctional = *value;
1453 }
1454 }
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001455 }
1456 }
1457 }
1458}
1459
1460/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001461 * @brief Gets D-Bus data for inventory items associated with sensors.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001462 *
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001463 * Uses the specified connections (services) to obtain D-Bus data for inventory
1464 * items associated with sensors. Stores the resulting data in the
1465 * inventoryItems vector.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001466 *
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001467 * This data is later used to provide sensor property values in the JSON
1468 * response.
1469 *
1470 * Finds the inventory item data asynchronously. Invokes callback when data has
1471 * been obtained.
1472 *
1473 * The callback must have the following signature:
1474 * @code
Anthony Wilsond5005492019-07-31 16:34:17 -05001475 * callback(void)
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001476 * @endcode
1477 *
1478 * This function is called recursively, obtaining data asynchronously from one
1479 * connection in each call. This ensures the callback is not invoked until the
1480 * last asynchronous function has completed.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001481 *
1482 * @param sensorsAsyncResp Pointer to object holding response data.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001483 * @param inventoryItems D-Bus inventory items associated with sensors.
1484 * @param invConnections Connections that provide data for the inventory items.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001485 * @param objectMgrPaths Mappings from connection name to DBus object path that
1486 * implements ObjectManager.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001487 * @param callback Callback to invoke when inventory data has been obtained.
1488 * @param invConnectionsIndex Current index in invConnections. Only specified
1489 * in recursive calls to this function.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001490 */
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001491template <typename Callback>
1492static void getInventoryItemsData(
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001493 std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001494 std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
Nan Zhoufe04d492022-06-22 17:10:41 +00001495 std::shared_ptr<std::set<std::string>> invConnections,
1496 std::shared_ptr<std::map<std::string, std::string>> objectMgrPaths,
Ed Tanous271584a2019-07-09 16:24:22 -07001497 Callback&& callback, size_t invConnectionsIndex = 0)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001498{
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001499 BMCWEB_LOG_DEBUG << "getInventoryItemsData enter";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001500
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001501 // If no more connections left, call callback
1502 if (invConnectionsIndex >= invConnections->size())
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001503 {
Anthony Wilsond5005492019-07-31 16:34:17 -05001504 callback();
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001505 BMCWEB_LOG_DEBUG << "getInventoryItemsData exit";
1506 return;
1507 }
1508
1509 // Get inventory item data from current connection
Nan Zhoufe04d492022-06-22 17:10:41 +00001510 auto it = invConnections->begin();
1511 std::advance(it, invConnectionsIndex);
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001512 if (it != invConnections->end())
1513 {
1514 const std::string& invConnection = *it;
1515
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001516 // Response handler for GetManagedObjects
Ed Tanous002d39b2022-05-31 08:59:27 -07001517 auto respHandler =
1518 [sensorsAsyncResp, inventoryItems, invConnections, objectMgrPaths,
Ed Tanous02cad962022-06-30 16:50:15 -07001519 callback{std::forward<Callback>(callback)}, invConnectionsIndex](
1520 const boost::system::error_code ec,
1521 const dbus::utility::ManagedObjectType& resp) {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001522 BMCWEB_LOG_DEBUG << "getInventoryItemsData respHandler enter";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001523 if (ec)
1524 {
1525 BMCWEB_LOG_ERROR
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001526 << "getInventoryItemsData respHandler DBus error " << ec;
zhanghch058d1b46d2021-04-01 11:18:24 +08001527 messages::internalError(sensorsAsyncResp->asyncResp->res);
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001528 return;
1529 }
1530
1531 // Loop through returned object paths
1532 for (const auto& objDictEntry : resp)
1533 {
1534 const std::string& objPath =
1535 static_cast<const std::string&>(objDictEntry.first);
1536
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001537 // If this object path is one of the specified inventory items
1538 InventoryItem* inventoryItem =
1539 findInventoryItem(inventoryItems, objPath);
1540 if (inventoryItem != nullptr)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001541 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001542 // Store inventory data in InventoryItem
1543 storeInventoryItemData(*inventoryItem, objDictEntry.second);
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001544 }
1545 }
1546
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001547 // Recurse to get inventory item data from next connection
1548 getInventoryItemsData(sensorsAsyncResp, inventoryItems,
1549 invConnections, objectMgrPaths,
1550 std::move(callback), invConnectionsIndex + 1);
1551
1552 BMCWEB_LOG_DEBUG << "getInventoryItemsData respHandler exit";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001553 };
1554
1555 // Find DBus object path that implements ObjectManager for the current
1556 // connection. If no mapping found, default to "/".
1557 auto iter = objectMgrPaths->find(invConnection);
1558 const std::string& objectMgrPath =
1559 (iter != objectMgrPaths->end()) ? iter->second : "/";
1560 BMCWEB_LOG_DEBUG << "ObjectManager path for " << invConnection << " is "
1561 << objectMgrPath;
1562
1563 // Get all object paths and their interfaces for current connection
1564 crow::connections::systemBus->async_method_call(
1565 std::move(respHandler), invConnection, objectMgrPath,
1566 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
1567 }
1568
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001569 BMCWEB_LOG_DEBUG << "getInventoryItemsData exit";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001570}
1571
1572/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001573 * @brief Gets connections that provide D-Bus data for inventory items.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001574 *
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001575 * Gets the D-Bus connections (services) that provide data for the inventory
1576 * items that are associated with sensors.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001577 *
1578 * Finds the connections asynchronously. Invokes callback when information has
1579 * been obtained.
1580 *
1581 * The callback must have the following signature:
1582 * @code
Nan Zhoufe04d492022-06-22 17:10:41 +00001583 * callback(std::shared_ptr<std::set<std::string>> invConnections)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001584 * @endcode
1585 *
1586 * @param sensorsAsyncResp Pointer to object holding response data.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001587 * @param inventoryItems D-Bus inventory items associated with sensors.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001588 * @param callback Callback to invoke when connections have been obtained.
1589 */
1590template <typename Callback>
1591static void getInventoryItemsConnections(
Ed Tanousb5a76932020-09-29 16:16:58 -07001592 const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
1593 const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems,
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001594 Callback&& callback)
1595{
1596 BMCWEB_LOG_DEBUG << "getInventoryItemsConnections enter";
1597
1598 const std::string path = "/xyz/openbmc_project/inventory";
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001599 const std::array<std::string, 4> interfaces = {
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001600 "xyz.openbmc_project.Inventory.Item",
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001601 "xyz.openbmc_project.Inventory.Item.PowerSupply",
1602 "xyz.openbmc_project.Inventory.Decorator.Asset",
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001603 "xyz.openbmc_project.State.Decorator.OperationalStatus"};
1604
1605 // Response handler for parsing output from GetSubTree
Ed Tanous002d39b2022-05-31 08:59:27 -07001606 auto respHandler =
1607 [callback{std::forward<Callback>(callback)}, sensorsAsyncResp,
1608 inventoryItems](
1609 const boost::system::error_code ec,
1610 const dbus::utility::MapperGetSubTreeResponse& subtree) {
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001611 BMCWEB_LOG_DEBUG << "getInventoryItemsConnections respHandler enter";
1612 if (ec)
1613 {
zhanghch058d1b46d2021-04-01 11:18:24 +08001614 messages::internalError(sensorsAsyncResp->asyncResp->res);
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001615 BMCWEB_LOG_ERROR
1616 << "getInventoryItemsConnections respHandler DBus error " << ec;
1617 return;
1618 }
1619
1620 // Make unique list of connections for desired inventory items
Nan Zhoufe04d492022-06-22 17:10:41 +00001621 std::shared_ptr<std::set<std::string>> invConnections =
1622 std::make_shared<std::set<std::string>>();
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001623
1624 // Loop through objects from GetSubTree
1625 for (const std::pair<
1626 std::string,
1627 std::vector<std::pair<std::string, std::vector<std::string>>>>&
1628 object : subtree)
1629 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001630 // Check if object path is one of the specified inventory items
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001631 const std::string& objPath = object.first;
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001632 if (findInventoryItem(inventoryItems, objPath) != nullptr)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001633 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001634 // Store all connections to inventory item
1635 for (const std::pair<std::string, std::vector<std::string>>&
1636 objData : object.second)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001637 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001638 const std::string& invConnection = objData.first;
1639 invConnections->insert(invConnection);
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001640 }
1641 }
1642 }
Anthony Wilsond5005492019-07-31 16:34:17 -05001643
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001644 callback(invConnections);
1645 BMCWEB_LOG_DEBUG << "getInventoryItemsConnections respHandler exit";
1646 };
1647
1648 // Make call to ObjectMapper to find all inventory items
1649 crow::connections::systemBus->async_method_call(
1650 std::move(respHandler), "xyz.openbmc_project.ObjectMapper",
1651 "/xyz/openbmc_project/object_mapper",
1652 "xyz.openbmc_project.ObjectMapper", "GetSubTree", path, 0, interfaces);
1653 BMCWEB_LOG_DEBUG << "getInventoryItemsConnections exit";
1654}
1655
1656/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001657 * @brief Gets associations from sensors to inventory items.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001658 *
1659 * Looks for ObjectMapper associations from the specified sensors to related
Anthony Wilsond5005492019-07-31 16:34:17 -05001660 * inventory items. Then finds the associations from those inventory items to
1661 * their LEDs, if any.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001662 *
1663 * Finds the inventory items asynchronously. Invokes callback when information
1664 * has been obtained.
1665 *
1666 * The callback must have the following signature:
1667 * @code
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001668 * callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001669 * @endcode
1670 *
1671 * @param sensorsAsyncResp Pointer to object holding response data.
1672 * @param sensorNames All sensors within the current chassis.
1673 * @param objectMgrPaths Mappings from connection name to DBus object path that
1674 * implements ObjectManager.
1675 * @param callback Callback to invoke when inventory items have been obtained.
1676 */
1677template <typename Callback>
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001678static void getInventoryItemAssociations(
Ed Tanousb5a76932020-09-29 16:16:58 -07001679 const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
Nan Zhoufe04d492022-06-22 17:10:41 +00001680 const std::shared_ptr<std::set<std::string>>& sensorNames,
1681 const std::shared_ptr<std::map<std::string, std::string>>& objectMgrPaths,
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001682 Callback&& callback)
1683{
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001684 BMCWEB_LOG_DEBUG << "getInventoryItemAssociations enter";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001685
1686 // Response handler for GetManagedObjects
Ed Tanous02cad962022-06-30 16:50:15 -07001687 auto respHandler =
1688 [callback{std::forward<Callback>(callback)}, sensorsAsyncResp,
1689 sensorNames](const boost::system::error_code ec,
1690 const dbus::utility::ManagedObjectType& resp) {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001691 BMCWEB_LOG_DEBUG << "getInventoryItemAssociations respHandler enter";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001692 if (ec)
1693 {
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001694 BMCWEB_LOG_ERROR
1695 << "getInventoryItemAssociations respHandler DBus error " << ec;
zhanghch058d1b46d2021-04-01 11:18:24 +08001696 messages::internalError(sensorsAsyncResp->asyncResp->res);
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001697 return;
1698 }
1699
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001700 // Create vector to hold list of inventory items
1701 std::shared_ptr<std::vector<InventoryItem>> inventoryItems =
1702 std::make_shared<std::vector<InventoryItem>>();
1703
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001704 // Loop through returned object paths
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001705 std::string sensorAssocPath;
1706 sensorAssocPath.reserve(128); // avoid memory allocations
1707 for (const auto& objDictEntry : resp)
1708 {
1709 const std::string& objPath =
1710 static_cast<const std::string&>(objDictEntry.first);
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001711
1712 // If path is inventory association for one of the specified sensors
1713 for (const std::string& sensorName : *sensorNames)
1714 {
1715 sensorAssocPath = sensorName;
1716 sensorAssocPath += "/inventory";
1717 if (objPath == sensorAssocPath)
1718 {
1719 // Get Association interface for object path
Ed Tanous711ac7a2021-12-20 09:34:41 -08001720 for (const auto& [interface, values] : objDictEntry.second)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001721 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001722 if (interface == "xyz.openbmc_project.Association")
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001723 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001724 for (const auto& [valueName, value] : values)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001725 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001726 if (valueName == "endpoints")
1727 {
1728 const std::vector<std::string>* endpoints =
1729 std::get_if<std::vector<std::string>>(
1730 &value);
1731 if ((endpoints != nullptr) &&
1732 !endpoints->empty())
1733 {
1734 // Add inventory item to vector
1735 const std::string& invItemPath =
1736 endpoints->front();
1737 addInventoryItem(inventoryItems,
1738 invItemPath,
1739 sensorName);
1740 }
1741 }
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001742 }
1743 }
1744 }
1745 break;
1746 }
1747 }
1748 }
1749
Anthony Wilsond5005492019-07-31 16:34:17 -05001750 // Now loop through the returned object paths again, this time to
1751 // find the leds associated with the inventory items we just found
1752 std::string inventoryAssocPath;
1753 inventoryAssocPath.reserve(128); // avoid memory allocations
1754 for (const auto& objDictEntry : resp)
1755 {
1756 const std::string& objPath =
1757 static_cast<const std::string&>(objDictEntry.first);
Anthony Wilsond5005492019-07-31 16:34:17 -05001758
1759 for (InventoryItem& inventoryItem : *inventoryItems)
1760 {
1761 inventoryAssocPath = inventoryItem.objectPath;
1762 inventoryAssocPath += "/leds";
1763 if (objPath == inventoryAssocPath)
1764 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001765 for (const auto& [interface, values] : objDictEntry.second)
Anthony Wilsond5005492019-07-31 16:34:17 -05001766 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001767 if (interface == "xyz.openbmc_project.Association")
Anthony Wilsond5005492019-07-31 16:34:17 -05001768 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001769 for (const auto& [valueName, value] : values)
Anthony Wilsond5005492019-07-31 16:34:17 -05001770 {
Ed Tanous711ac7a2021-12-20 09:34:41 -08001771 if (valueName == "endpoints")
1772 {
1773 const std::vector<std::string>* endpoints =
1774 std::get_if<std::vector<std::string>>(
1775 &value);
1776 if ((endpoints != nullptr) &&
1777 !endpoints->empty())
1778 {
1779 // Add inventory item to vector
1780 // Store LED path in inventory item
1781 const std::string& ledPath =
1782 endpoints->front();
1783 inventoryItem.ledObjectPath = ledPath;
1784 }
1785 }
Anthony Wilsond5005492019-07-31 16:34:17 -05001786 }
1787 }
1788 }
Ed Tanous711ac7a2021-12-20 09:34:41 -08001789
Anthony Wilsond5005492019-07-31 16:34:17 -05001790 break;
1791 }
1792 }
1793 }
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001794 callback(inventoryItems);
1795 BMCWEB_LOG_DEBUG << "getInventoryItemAssociations respHandler exit";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001796 };
1797
1798 // Find DBus object path that implements ObjectManager for ObjectMapper
1799 std::string connection = "xyz.openbmc_project.ObjectMapper";
1800 auto iter = objectMgrPaths->find(connection);
1801 const std::string& objectMgrPath =
1802 (iter != objectMgrPaths->end()) ? iter->second : "/";
1803 BMCWEB_LOG_DEBUG << "ObjectManager path for " << connection << " is "
1804 << objectMgrPath;
1805
1806 // Call GetManagedObjects on the ObjectMapper to get all associations
1807 crow::connections::systemBus->async_method_call(
1808 std::move(respHandler), connection, objectMgrPath,
1809 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
1810
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05001811 BMCWEB_LOG_DEBUG << "getInventoryItemAssociations exit";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05001812}
1813
1814/**
Anthony Wilsond5005492019-07-31 16:34:17 -05001815 * @brief Gets D-Bus data for inventory item leds associated with sensors.
1816 *
1817 * Uses the specified connections (services) to obtain D-Bus data for inventory
1818 * item leds associated with sensors. Stores the resulting data in the
1819 * inventoryItems vector.
1820 *
1821 * This data is later used to provide sensor property values in the JSON
1822 * response.
1823 *
1824 * Finds the inventory item led data asynchronously. Invokes callback when data
1825 * has been obtained.
1826 *
1827 * The callback must have the following signature:
1828 * @code
Gunnar Mills42cbe532019-08-15 15:26:54 -05001829 * callback()
Anthony Wilsond5005492019-07-31 16:34:17 -05001830 * @endcode
1831 *
1832 * This function is called recursively, obtaining data asynchronously from one
1833 * connection in each call. This ensures the callback is not invoked until the
1834 * last asynchronous function has completed.
1835 *
1836 * @param sensorsAsyncResp Pointer to object holding response data.
1837 * @param inventoryItems D-Bus inventory items associated with sensors.
1838 * @param ledConnections Connections that provide data for the inventory leds.
1839 * @param callback Callback to invoke when inventory data has been obtained.
1840 * @param ledConnectionsIndex Current index in ledConnections. Only specified
1841 * in recursive calls to this function.
1842 */
1843template <typename Callback>
1844void getInventoryLedData(
1845 std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
1846 std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
Nan Zhoufe04d492022-06-22 17:10:41 +00001847 std::shared_ptr<std::map<std::string, std::string>> ledConnections,
Anthony Wilsond5005492019-07-31 16:34:17 -05001848 Callback&& callback, size_t ledConnectionsIndex = 0)
1849{
1850 BMCWEB_LOG_DEBUG << "getInventoryLedData enter";
1851
1852 // If no more connections left, call callback
1853 if (ledConnectionsIndex >= ledConnections->size())
1854 {
Gunnar Mills42cbe532019-08-15 15:26:54 -05001855 callback();
Anthony Wilsond5005492019-07-31 16:34:17 -05001856 BMCWEB_LOG_DEBUG << "getInventoryLedData exit";
1857 return;
1858 }
1859
1860 // Get inventory item data from current connection
Nan Zhoufe04d492022-06-22 17:10:41 +00001861 auto it = ledConnections->begin();
1862 std::advance(it, ledConnectionsIndex);
Anthony Wilsond5005492019-07-31 16:34:17 -05001863 if (it != ledConnections->end())
1864 {
1865 const std::string& ledPath = (*it).first;
1866 const std::string& ledConnection = (*it).second;
1867 // Response handler for Get State property
Jonathan Doman1e1e5982021-06-11 09:36:17 -07001868 auto respHandler =
1869 [sensorsAsyncResp, inventoryItems, ledConnections, ledPath,
Ed Tanousf94c4ec2022-01-06 12:44:41 -08001870 callback{std::forward<Callback>(callback)}, ledConnectionsIndex](
Jonathan Doman1e1e5982021-06-11 09:36:17 -07001871 const boost::system::error_code ec, const std::string& state) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001872 BMCWEB_LOG_DEBUG << "getInventoryLedData respHandler enter";
1873 if (ec)
1874 {
1875 BMCWEB_LOG_ERROR
1876 << "getInventoryLedData respHandler DBus error " << ec;
1877 messages::internalError(sensorsAsyncResp->asyncResp->res);
1878 return;
1879 }
1880
1881 BMCWEB_LOG_DEBUG << "Led state: " << state;
1882 // Find inventory item with this LED object path
1883 InventoryItem* inventoryItem =
1884 findInventoryItemForLed(*inventoryItems, ledPath);
1885 if (inventoryItem != nullptr)
1886 {
1887 // Store LED state in InventoryItem
Ed Tanous11ba3972022-07-11 09:50:41 -07001888 if (state.ends_with("On"))
Jonathan Doman1e1e5982021-06-11 09:36:17 -07001889 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001890 inventoryItem->ledState = LedState::ON;
Jonathan Doman1e1e5982021-06-11 09:36:17 -07001891 }
Ed Tanous11ba3972022-07-11 09:50:41 -07001892 else if (state.ends_with("Blink"))
Anthony Wilsond5005492019-07-31 16:34:17 -05001893 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001894 inventoryItem->ledState = LedState::BLINK;
Anthony Wilsond5005492019-07-31 16:34:17 -05001895 }
Ed Tanous11ba3972022-07-11 09:50:41 -07001896 else if (state.ends_with("Off"))
Ed Tanous002d39b2022-05-31 08:59:27 -07001897 {
1898 inventoryItem->ledState = LedState::OFF;
1899 }
1900 else
1901 {
1902 inventoryItem->ledState = LedState::UNKNOWN;
1903 }
1904 }
Anthony Wilsond5005492019-07-31 16:34:17 -05001905
Ed Tanous002d39b2022-05-31 08:59:27 -07001906 // Recurse to get LED data from next connection
1907 getInventoryLedData(sensorsAsyncResp, inventoryItems,
1908 ledConnections, std::move(callback),
1909 ledConnectionsIndex + 1);
Anthony Wilsond5005492019-07-31 16:34:17 -05001910
Ed Tanous002d39b2022-05-31 08:59:27 -07001911 BMCWEB_LOG_DEBUG << "getInventoryLedData respHandler exit";
1912 };
Anthony Wilsond5005492019-07-31 16:34:17 -05001913
1914 // Get the State property for the current LED
Jonathan Doman1e1e5982021-06-11 09:36:17 -07001915 sdbusplus::asio::getProperty<std::string>(
1916 *crow::connections::systemBus, ledConnection, ledPath,
1917 "xyz.openbmc_project.Led.Physical", "State",
1918 std::move(respHandler));
Anthony Wilsond5005492019-07-31 16:34:17 -05001919 }
1920
1921 BMCWEB_LOG_DEBUG << "getInventoryLedData exit";
1922}
1923
1924/**
1925 * @brief Gets LED data for LEDs associated with given inventory items.
1926 *
1927 * Gets the D-Bus connections (services) that provide LED data for the LEDs
1928 * associated with the specified inventory items. Then gets the LED data from
1929 * each connection and stores it in the inventory item.
1930 *
1931 * This data is later used to provide sensor property values in the JSON
1932 * response.
1933 *
1934 * Finds the LED data asynchronously. Invokes callback when information has
1935 * been obtained.
1936 *
1937 * The callback must have the following signature:
1938 * @code
Gunnar Mills42cbe532019-08-15 15:26:54 -05001939 * callback()
Anthony Wilsond5005492019-07-31 16:34:17 -05001940 * @endcode
1941 *
1942 * @param sensorsAsyncResp Pointer to object holding response data.
1943 * @param inventoryItems D-Bus inventory items associated with sensors.
1944 * @param callback Callback to invoke when inventory items have been obtained.
1945 */
1946template <typename Callback>
1947void getInventoryLeds(
1948 std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
1949 std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
1950 Callback&& callback)
1951{
1952 BMCWEB_LOG_DEBUG << "getInventoryLeds enter";
1953
1954 const std::string path = "/xyz/openbmc_project";
1955 const std::array<std::string, 1> interfaces = {
1956 "xyz.openbmc_project.Led.Physical"};
1957
1958 // Response handler for parsing output from GetSubTree
Ed Tanous002d39b2022-05-31 08:59:27 -07001959 auto respHandler =
1960 [callback{std::forward<Callback>(callback)}, sensorsAsyncResp,
1961 inventoryItems](
1962 const boost::system::error_code ec,
1963 const dbus::utility::MapperGetSubTreeResponse& subtree) {
Anthony Wilsond5005492019-07-31 16:34:17 -05001964 BMCWEB_LOG_DEBUG << "getInventoryLeds respHandler enter";
1965 if (ec)
1966 {
zhanghch058d1b46d2021-04-01 11:18:24 +08001967 messages::internalError(sensorsAsyncResp->asyncResp->res);
Anthony Wilsond5005492019-07-31 16:34:17 -05001968 BMCWEB_LOG_ERROR << "getInventoryLeds respHandler DBus error "
1969 << ec;
1970 return;
1971 }
1972
1973 // Build map of LED object paths to connections
Nan Zhoufe04d492022-06-22 17:10:41 +00001974 std::shared_ptr<std::map<std::string, std::string>> ledConnections =
1975 std::make_shared<std::map<std::string, std::string>>();
Anthony Wilsond5005492019-07-31 16:34:17 -05001976
1977 // Loop through objects from GetSubTree
1978 for (const std::pair<
1979 std::string,
1980 std::vector<std::pair<std::string, std::vector<std::string>>>>&
1981 object : subtree)
1982 {
1983 // Check if object path is LED for one of the specified inventory
1984 // items
1985 const std::string& ledPath = object.first;
1986 if (findInventoryItemForLed(*inventoryItems, ledPath) != nullptr)
1987 {
1988 // Add mapping from ledPath to connection
1989 const std::string& connection = object.second.begin()->first;
1990 (*ledConnections)[ledPath] = connection;
1991 BMCWEB_LOG_DEBUG << "Added mapping " << ledPath << " -> "
1992 << connection;
1993 }
1994 }
1995
1996 getInventoryLedData(sensorsAsyncResp, inventoryItems, ledConnections,
1997 std::move(callback));
1998 BMCWEB_LOG_DEBUG << "getInventoryLeds respHandler exit";
1999 };
2000 // Make call to ObjectMapper to find all inventory items
2001 crow::connections::systemBus->async_method_call(
2002 std::move(respHandler), "xyz.openbmc_project.ObjectMapper",
2003 "/xyz/openbmc_project/object_mapper",
2004 "xyz.openbmc_project.ObjectMapper", "GetSubTree", path, 0, interfaces);
2005 BMCWEB_LOG_DEBUG << "getInventoryLeds exit";
2006}
2007
2008/**
Gunnar Mills42cbe532019-08-15 15:26:54 -05002009 * @brief Gets D-Bus data for Power Supply Attributes such as EfficiencyPercent
2010 *
2011 * Uses the specified connections (services) (currently assumes just one) to
2012 * obtain D-Bus data for Power Supply Attributes. Stores the resulting data in
2013 * the inventoryItems vector. Only stores data in Power Supply inventoryItems.
2014 *
2015 * This data is later used to provide sensor property values in the JSON
2016 * response.
2017 *
2018 * Finds the Power Supply Attributes data asynchronously. Invokes callback
2019 * when data has been obtained.
2020 *
2021 * The callback must have the following signature:
2022 * @code
2023 * callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems)
2024 * @endcode
2025 *
2026 * @param sensorsAsyncResp Pointer to object holding response data.
2027 * @param inventoryItems D-Bus inventory items associated with sensors.
2028 * @param psAttributesConnections Connections that provide data for the Power
2029 * Supply Attributes
2030 * @param callback Callback to invoke when data has been obtained.
2031 */
2032template <typename Callback>
2033void getPowerSupplyAttributesData(
Ed Tanousb5a76932020-09-29 16:16:58 -07002034 const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
Gunnar Mills42cbe532019-08-15 15:26:54 -05002035 std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
Nan Zhoufe04d492022-06-22 17:10:41 +00002036 const std::map<std::string, std::string>& psAttributesConnections,
Gunnar Mills42cbe532019-08-15 15:26:54 -05002037 Callback&& callback)
2038{
2039 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributesData enter";
2040
2041 if (psAttributesConnections.empty())
2042 {
2043 BMCWEB_LOG_DEBUG << "Can't find PowerSupplyAttributes, no connections!";
2044 callback(inventoryItems);
2045 return;
2046 }
2047
2048 // Assuming just one connection (service) for now
Nan Zhoufe04d492022-06-22 17:10:41 +00002049 auto it = psAttributesConnections.begin();
Gunnar Mills42cbe532019-08-15 15:26:54 -05002050
2051 const std::string& psAttributesPath = (*it).first;
2052 const std::string& psAttributesConnection = (*it).second;
2053
2054 // Response handler for Get DeratingFactor property
Ed Tanous002d39b2022-05-31 08:59:27 -07002055 auto respHandler =
2056 [sensorsAsyncResp, inventoryItems,
2057 callback{std::forward<Callback>(callback)}](
2058 const boost::system::error_code ec, const uint32_t value) {
Gunnar Mills42cbe532019-08-15 15:26:54 -05002059 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributesData respHandler enter";
2060 if (ec)
2061 {
2062 BMCWEB_LOG_ERROR
2063 << "getPowerSupplyAttributesData respHandler DBus error " << ec;
zhanghch058d1b46d2021-04-01 11:18:24 +08002064 messages::internalError(sensorsAsyncResp->asyncResp->res);
Gunnar Mills42cbe532019-08-15 15:26:54 -05002065 return;
2066 }
2067
Jonathan Doman1e1e5982021-06-11 09:36:17 -07002068 BMCWEB_LOG_DEBUG << "PS EfficiencyPercent value: " << value;
2069 // Store value in Power Supply Inventory Items
2070 for (InventoryItem& inventoryItem : *inventoryItems)
Gunnar Mills42cbe532019-08-15 15:26:54 -05002071 {
Ed Tanous55f79e62022-01-25 11:26:16 -08002072 if (inventoryItem.isPowerSupply)
Gunnar Mills42cbe532019-08-15 15:26:54 -05002073 {
Jonathan Doman1e1e5982021-06-11 09:36:17 -07002074 inventoryItem.powerSupplyEfficiencyPercent =
2075 static_cast<int>(value);
Gunnar Mills42cbe532019-08-15 15:26:54 -05002076 }
2077 }
Gunnar Mills42cbe532019-08-15 15:26:54 -05002078
2079 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributesData respHandler exit";
2080 callback(inventoryItems);
2081 };
2082
2083 // Get the DeratingFactor property for the PowerSupplyAttributes
2084 // Currently only property on the interface/only one we care about
Jonathan Doman1e1e5982021-06-11 09:36:17 -07002085 sdbusplus::asio::getProperty<uint32_t>(
2086 *crow::connections::systemBus, psAttributesConnection, psAttributesPath,
2087 "xyz.openbmc_project.Control.PowerSupplyAttributes", "DeratingFactor",
2088 std::move(respHandler));
Gunnar Mills42cbe532019-08-15 15:26:54 -05002089
2090 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributesData exit";
2091}
2092
2093/**
2094 * @brief Gets the Power Supply Attributes such as EfficiencyPercent
2095 *
2096 * Gets the D-Bus connection (service) that provides Power Supply Attributes
2097 * data. Then gets the Power Supply Attributes data from the connection
2098 * (currently just assumes 1 connection) and stores the data in the inventory
2099 * item.
2100 *
2101 * This data is later used to provide sensor property values in the JSON
2102 * response. DeratingFactor on D-Bus is mapped to EfficiencyPercent on Redfish.
2103 *
2104 * Finds the Power Supply Attributes data asynchronously. Invokes callback
2105 * when information has been obtained.
2106 *
2107 * The callback must have the following signature:
2108 * @code
2109 * callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems)
2110 * @endcode
2111 *
2112 * @param sensorsAsyncResp Pointer to object holding response data.
2113 * @param inventoryItems D-Bus inventory items associated with sensors.
2114 * @param callback Callback to invoke when data has been obtained.
2115 */
2116template <typename Callback>
2117void getPowerSupplyAttributes(
2118 std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
2119 std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
2120 Callback&& callback)
2121{
2122 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes enter";
2123
2124 // Only need the power supply attributes when the Power Schema
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02002125 if (sensorsAsyncResp->chassisSubNode != sensors::node::power)
Gunnar Mills42cbe532019-08-15 15:26:54 -05002126 {
2127 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes exit since not Power";
2128 callback(inventoryItems);
2129 return;
2130 }
2131
2132 const std::array<std::string, 1> interfaces = {
2133 "xyz.openbmc_project.Control.PowerSupplyAttributes"};
2134
2135 // Response handler for parsing output from GetSubTree
Ed Tanousb9d36b42022-02-26 21:42:46 -08002136 auto respHandler =
2137 [callback{std::forward<Callback>(callback)}, sensorsAsyncResp,
2138 inventoryItems](
2139 const boost::system::error_code ec,
2140 const dbus::utility::MapperGetSubTreeResponse& subtree) {
Ed Tanous002d39b2022-05-31 08:59:27 -07002141 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes respHandler enter";
2142 if (ec)
2143 {
2144 messages::internalError(sensorsAsyncResp->asyncResp->res);
2145 BMCWEB_LOG_ERROR
2146 << "getPowerSupplyAttributes respHandler DBus error " << ec;
2147 return;
2148 }
2149 if (subtree.empty())
2150 {
2151 BMCWEB_LOG_DEBUG << "Can't find Power Supply Attributes!";
2152 callback(inventoryItems);
2153 return;
2154 }
Gunnar Mills42cbe532019-08-15 15:26:54 -05002155
Ed Tanous002d39b2022-05-31 08:59:27 -07002156 // Currently we only support 1 power supply attribute, use this for
2157 // all the power supplies. Build map of object path to connection.
2158 // Assume just 1 connection and 1 path for now.
Nan Zhoufe04d492022-06-22 17:10:41 +00002159 std::map<std::string, std::string> psAttributesConnections;
Gunnar Mills42cbe532019-08-15 15:26:54 -05002160
Ed Tanous002d39b2022-05-31 08:59:27 -07002161 if (subtree[0].first.empty() || subtree[0].second.empty())
2162 {
2163 BMCWEB_LOG_DEBUG << "Power Supply Attributes mapper error!";
2164 callback(inventoryItems);
2165 return;
2166 }
Gunnar Mills42cbe532019-08-15 15:26:54 -05002167
Ed Tanous002d39b2022-05-31 08:59:27 -07002168 const std::string& psAttributesPath = subtree[0].first;
2169 const std::string& connection = subtree[0].second.begin()->first;
Gunnar Mills42cbe532019-08-15 15:26:54 -05002170
Ed Tanous002d39b2022-05-31 08:59:27 -07002171 if (connection.empty())
2172 {
2173 BMCWEB_LOG_DEBUG << "Power Supply Attributes mapper error!";
2174 callback(inventoryItems);
2175 return;
2176 }
Gunnar Mills42cbe532019-08-15 15:26:54 -05002177
Ed Tanous002d39b2022-05-31 08:59:27 -07002178 psAttributesConnections[psAttributesPath] = connection;
2179 BMCWEB_LOG_DEBUG << "Added mapping " << psAttributesPath << " -> "
2180 << connection;
Gunnar Mills42cbe532019-08-15 15:26:54 -05002181
Ed Tanous002d39b2022-05-31 08:59:27 -07002182 getPowerSupplyAttributesData(sensorsAsyncResp, inventoryItems,
2183 psAttributesConnections,
2184 std::move(callback));
2185 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes respHandler exit";
2186 };
Gunnar Mills42cbe532019-08-15 15:26:54 -05002187 // Make call to ObjectMapper to find the PowerSupplyAttributes service
2188 crow::connections::systemBus->async_method_call(
2189 std::move(respHandler), "xyz.openbmc_project.ObjectMapper",
2190 "/xyz/openbmc_project/object_mapper",
2191 "xyz.openbmc_project.ObjectMapper", "GetSubTree",
2192 "/xyz/openbmc_project", 0, interfaces);
2193 BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes exit";
2194}
2195
2196/**
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002197 * @brief Gets inventory items associated with sensors.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002198 *
2199 * Finds the inventory items that are associated with the specified sensors.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002200 * Then gets D-Bus data for the inventory items, such as presence and VPD.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002201 *
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002202 * This data is later used to provide sensor property values in the JSON
2203 * response.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002204 *
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002205 * Finds the inventory items asynchronously. Invokes callback when the
2206 * inventory items have been obtained.
2207 *
2208 * The callback must have the following signature:
2209 * @code
2210 * callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems)
2211 * @endcode
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002212 *
2213 * @param sensorsAsyncResp Pointer to object holding response data.
2214 * @param sensorNames All sensors within the current chassis.
2215 * @param objectMgrPaths Mappings from connection name to DBus object path that
2216 * implements ObjectManager.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002217 * @param callback Callback to invoke when inventory items have been obtained.
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002218 */
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002219template <typename Callback>
2220static void getInventoryItems(
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002221 std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
Nan Zhoufe04d492022-06-22 17:10:41 +00002222 const std::shared_ptr<std::set<std::string>> sensorNames,
2223 std::shared_ptr<std::map<std::string, std::string>> objectMgrPaths,
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002224 Callback&& callback)
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002225{
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002226 BMCWEB_LOG_DEBUG << "getInventoryItems enter";
2227 auto getInventoryItemAssociationsCb =
Ed Tanousf94c4ec2022-01-06 12:44:41 -08002228 [sensorsAsyncResp, objectMgrPaths,
2229 callback{std::forward<Callback>(callback)}](
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002230 std::shared_ptr<std::vector<InventoryItem>> inventoryItems) {
Ed Tanous002d39b2022-05-31 08:59:27 -07002231 BMCWEB_LOG_DEBUG << "getInventoryItemAssociationsCb enter";
2232 auto getInventoryItemsConnectionsCb =
2233 [sensorsAsyncResp, inventoryItems, objectMgrPaths,
2234 callback{std::forward<const Callback>(callback)}](
Nan Zhoufe04d492022-06-22 17:10:41 +00002235 std::shared_ptr<std::set<std::string>> invConnections) {
Ed Tanous002d39b2022-05-31 08:59:27 -07002236 BMCWEB_LOG_DEBUG << "getInventoryItemsConnectionsCb enter";
2237 auto getInventoryItemsDataCb = [sensorsAsyncResp, inventoryItems,
2238 callback{std::move(callback)}]() {
2239 BMCWEB_LOG_DEBUG << "getInventoryItemsDataCb enter";
Gunnar Mills42cbe532019-08-15 15:26:54 -05002240
Ed Tanous002d39b2022-05-31 08:59:27 -07002241 auto getInventoryLedsCb = [sensorsAsyncResp, inventoryItems,
2242 callback{std::move(callback)}]() {
2243 BMCWEB_LOG_DEBUG << "getInventoryLedsCb enter";
2244 // Find Power Supply Attributes and get the data
2245 getPowerSupplyAttributes(sensorsAsyncResp, inventoryItems,
2246 std::move(callback));
2247 BMCWEB_LOG_DEBUG << "getInventoryLedsCb exit";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002248 };
2249
Ed Tanous002d39b2022-05-31 08:59:27 -07002250 // Find led connections and get the data
2251 getInventoryLeds(sensorsAsyncResp, inventoryItems,
2252 std::move(getInventoryLedsCb));
2253 BMCWEB_LOG_DEBUG << "getInventoryItemsDataCb exit";
2254 };
2255
2256 // Get inventory item data from connections
2257 getInventoryItemsData(sensorsAsyncResp, inventoryItems,
2258 invConnections, objectMgrPaths,
2259 std::move(getInventoryItemsDataCb));
2260 BMCWEB_LOG_DEBUG << "getInventoryItemsConnectionsCb exit";
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002261 };
2262
Ed Tanous002d39b2022-05-31 08:59:27 -07002263 // Get connections that provide inventory item data
2264 getInventoryItemsConnections(sensorsAsyncResp, inventoryItems,
2265 std::move(getInventoryItemsConnectionsCb));
2266 BMCWEB_LOG_DEBUG << "getInventoryItemAssociationsCb exit";
2267 };
2268
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002269 // Get associations from sensors to inventory items
2270 getInventoryItemAssociations(sensorsAsyncResp, sensorNames, objectMgrPaths,
2271 std::move(getInventoryItemAssociationsCb));
2272 BMCWEB_LOG_DEBUG << "getInventoryItems exit";
2273}
2274
2275/**
2276 * @brief Returns JSON PowerSupply object for the specified inventory item.
2277 *
2278 * Searches for a JSON PowerSupply object that matches the specified inventory
2279 * item. If one is not found, a new PowerSupply object is added to the JSON
2280 * array.
2281 *
2282 * Multiple sensors are often associated with one power supply inventory item.
2283 * As a result, multiple sensor values are stored in one JSON PowerSupply
2284 * object.
2285 *
2286 * @param powerSupplyArray JSON array containing Redfish PowerSupply objects.
2287 * @param inventoryItem Inventory item for the power supply.
2288 * @param chassisId Chassis that contains the power supply.
2289 * @return JSON PowerSupply object for the specified inventory item.
2290 */
Ed Tanous23a21a12020-07-25 04:45:05 +00002291inline nlohmann::json& getPowerSupply(nlohmann::json& powerSupplyArray,
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002292 const InventoryItem& inventoryItem,
2293 const std::string& chassisId)
2294{
2295 // Check if matching PowerSupply object already exists in JSON array
2296 for (nlohmann::json& powerSupply : powerSupplyArray)
2297 {
2298 if (powerSupply["MemberId"] == inventoryItem.name)
2299 {
2300 return powerSupply;
2301 }
2302 }
2303
2304 // Add new PowerSupply object to JSON array
2305 powerSupplyArray.push_back({});
2306 nlohmann::json& powerSupply = powerSupplyArray.back();
2307 powerSupply["@odata.id"] =
2308 "/redfish/v1/Chassis/" + chassisId + "/Power#/PowerSupplies/";
2309 powerSupply["MemberId"] = inventoryItem.name;
2310 powerSupply["Name"] = boost::replace_all_copy(inventoryItem.name, "_", " ");
2311 powerSupply["Manufacturer"] = inventoryItem.manufacturer;
2312 powerSupply["Model"] = inventoryItem.model;
2313 powerSupply["PartNumber"] = inventoryItem.partNumber;
2314 powerSupply["SerialNumber"] = inventoryItem.serialNumber;
Anthony Wilsond5005492019-07-31 16:34:17 -05002315 setLedState(powerSupply, &inventoryItem);
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002316
Gunnar Mills42cbe532019-08-15 15:26:54 -05002317 if (inventoryItem.powerSupplyEfficiencyPercent >= 0)
2318 {
2319 powerSupply["EfficiencyPercent"] =
2320 inventoryItem.powerSupplyEfficiencyPercent;
2321 }
2322
2323 powerSupply["Status"]["State"] = getState(&inventoryItem);
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002324 const char* health = inventoryItem.isFunctional ? "OK" : "Critical";
2325 powerSupply["Status"]["Health"] = health;
2326
2327 return powerSupply;
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002328}
2329
2330/**
Shawn McCarneyde629b62019-03-08 10:42:51 -06002331 * @brief Gets the values of the specified sensors.
2332 *
2333 * Stores the results as JSON in the SensorsAsyncResp.
2334 *
2335 * Gets the sensor values asynchronously. Stores the results later when the
2336 * information has been obtained.
2337 *
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002338 * The sensorNames set contains all requested sensors for the current chassis.
Shawn McCarneyde629b62019-03-08 10:42:51 -06002339 *
2340 * To minimize the number of DBus calls, the DBus method
2341 * org.freedesktop.DBus.ObjectManager.GetManagedObjects() is used to get the
2342 * values of all sensors provided by a connection (service).
2343 *
2344 * The connections set contains all the connections that provide sensor values.
2345 *
2346 * The objectMgrPaths map contains mappings from a connection name to the
2347 * corresponding DBus object path that implements ObjectManager.
2348 *
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002349 * The InventoryItem vector contains D-Bus inventory items associated with the
2350 * sensors. Inventory item data is needed for some Redfish sensor properties.
2351 *
Shawn McCarneyde629b62019-03-08 10:42:51 -06002352 * @param SensorsAsyncResp Pointer to object holding response data.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002353 * @param sensorNames All requested sensors within the current chassis.
Shawn McCarneyde629b62019-03-08 10:42:51 -06002354 * @param connections Connections that provide sensor values.
2355 * @param objectMgrPaths Mappings from connection name to DBus object path that
2356 * implements ObjectManager.
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002357 * @param inventoryItems Inventory items associated with the sensors.
Shawn McCarneyde629b62019-03-08 10:42:51 -06002358 */
Ed Tanous23a21a12020-07-25 04:45:05 +00002359inline void getSensorData(
Ed Tanous81ce6092020-12-17 16:54:55 +00002360 const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
Nan Zhoufe04d492022-06-22 17:10:41 +00002361 const std::shared_ptr<std::set<std::string>>& sensorNames,
2362 const std::set<std::string>& connections,
2363 const std::shared_ptr<std::map<std::string, std::string>>& objectMgrPaths,
Ed Tanousb5a76932020-09-29 16:16:58 -07002364 const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems)
Shawn McCarneyde629b62019-03-08 10:42:51 -06002365{
2366 BMCWEB_LOG_DEBUG << "getSensorData enter";
2367 // Get managed objects from all services exposing sensors
2368 for (const std::string& connection : connections)
2369 {
2370 // Response handler to process managed objects
Ed Tanous002d39b2022-05-31 08:59:27 -07002371 auto getManagedObjectsCb =
2372 [sensorsAsyncResp, sensorNames,
2373 inventoryItems](const boost::system::error_code ec,
Ed Tanous02cad962022-06-30 16:50:15 -07002374 const dbus::utility::ManagedObjectType& resp) {
Shawn McCarneyde629b62019-03-08 10:42:51 -06002375 BMCWEB_LOG_DEBUG << "getManagedObjectsCb enter";
2376 if (ec)
2377 {
2378 BMCWEB_LOG_ERROR << "getManagedObjectsCb DBUS error: " << ec;
zhanghch058d1b46d2021-04-01 11:18:24 +08002379 messages::internalError(sensorsAsyncResp->asyncResp->res);
Shawn McCarneyde629b62019-03-08 10:42:51 -06002380 return;
2381 }
2382 // Go through all objects and update response with sensor data
2383 for (const auto& objDictEntry : resp)
2384 {
2385 const std::string& objPath =
2386 static_cast<const std::string&>(objDictEntry.first);
2387 BMCWEB_LOG_DEBUG << "getManagedObjectsCb parsing object "
2388 << objPath;
2389
Shawn McCarneyde629b62019-03-08 10:42:51 -06002390 std::vector<std::string> split;
2391 // Reserve space for
2392 // /xyz/openbmc_project/sensors/<name>/<subname>
2393 split.reserve(6);
2394 boost::algorithm::split(split, objPath, boost::is_any_of("/"));
2395 if (split.size() < 6)
2396 {
2397 BMCWEB_LOG_ERROR << "Got path that isn't long enough "
2398 << objPath;
2399 continue;
2400 }
2401 // These indexes aren't intuitive, as boost::split puts an empty
2402 // string at the beginning
2403 const std::string& sensorType = split[4];
2404 const std::string& sensorName = split[5];
2405 BMCWEB_LOG_DEBUG << "sensorName " << sensorName
2406 << " sensorType " << sensorType;
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002407 if (sensorNames->find(objPath) == sensorNames->end())
Shawn McCarneyde629b62019-03-08 10:42:51 -06002408 {
Andrew Geissleraccdbb22021-11-09 15:24:45 -06002409 BMCWEB_LOG_DEBUG << sensorName << " not in sensor list ";
Shawn McCarneyde629b62019-03-08 10:42:51 -06002410 continue;
2411 }
2412
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002413 // Find inventory item (if any) associated with sensor
2414 InventoryItem* inventoryItem =
2415 findInventoryItemForSensor(inventoryItems, objPath);
2416
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002417 const std::string& sensorSchema =
Ed Tanous81ce6092020-12-17 16:54:55 +00002418 sensorsAsyncResp->chassisSubNode;
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002419
2420 nlohmann::json* sensorJson = nullptr;
2421
Nan Zhou928fefb2022-03-28 08:45:00 -07002422 if (sensorSchema == sensors::node::sensors &&
2423 !sensorsAsyncResp->efficientExpand)
Shawn McCarneyde629b62019-03-08 10:42:51 -06002424 {
Ed Tanousc1d019a2022-08-06 09:36:06 -07002425 std::string sensorTypeEscaped(sensorType);
2426 sensorTypeEscaped.erase(
2427 std::remove(sensorTypeEscaped.begin(),
2428 sensorTypeEscaped.end(), '_'),
2429 sensorTypeEscaped.end());
2430 std::string sensorId(sensorTypeEscaped);
2431 sensorId += "_";
2432 sensorId += sensorName;
2433
zhanghch058d1b46d2021-04-01 11:18:24 +08002434 sensorsAsyncResp->asyncResp->res.jsonValue["@odata.id"] =
Ed Tanousc1d019a2022-08-06 09:36:06 -07002435 crow::utility::urlFromPieces(
2436 "redfish", "v1", "Chassis",
2437 sensorsAsyncResp->chassisId,
2438 sensorsAsyncResp->chassisSubNode, sensorId);
zhanghch058d1b46d2021-04-01 11:18:24 +08002439 sensorJson = &(sensorsAsyncResp->asyncResp->res.jsonValue);
Shawn McCarneyde629b62019-03-08 10:42:51 -06002440 }
2441 else
2442 {
Ed Tanous271584a2019-07-09 16:24:22 -07002443 std::string fieldName;
Nan Zhou928fefb2022-03-28 08:45:00 -07002444 if (sensorsAsyncResp->efficientExpand)
2445 {
2446 fieldName = "Members";
2447 }
2448 else if (sensorType == "temperature")
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002449 {
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002450 fieldName = "Temperatures";
2451 }
2452 else if (sensorType == "fan" || sensorType == "fan_tach" ||
2453 sensorType == "fan_pwm")
2454 {
2455 fieldName = "Fans";
2456 }
2457 else if (sensorType == "voltage")
2458 {
2459 fieldName = "Voltages";
2460 }
2461 else if (sensorType == "power")
2462 {
Ed Tanous55f79e62022-01-25 11:26:16 -08002463 if (sensorName == "total_power")
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002464 {
2465 fieldName = "PowerControl";
2466 }
2467 else if ((inventoryItem != nullptr) &&
2468 (inventoryItem->isPowerSupply))
2469 {
2470 fieldName = "PowerSupplies";
2471 }
2472 else
2473 {
2474 // Other power sensors are in SensorCollection
2475 continue;
2476 }
2477 }
2478 else
2479 {
2480 BMCWEB_LOG_ERROR << "Unsure how to handle sensorType "
2481 << sensorType;
2482 continue;
2483 }
2484
2485 nlohmann::json& tempArray =
zhanghch058d1b46d2021-04-01 11:18:24 +08002486 sensorsAsyncResp->asyncResp->res.jsonValue[fieldName];
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002487 if (fieldName == "PowerControl")
2488 {
2489 if (tempArray.empty())
2490 {
2491 // Put multiple "sensors" into a single
2492 // PowerControl. Follows MemberId naming and
2493 // naming in power.hpp.
Ed Tanous14766872022-03-15 10:44:42 -07002494 nlohmann::json::object_t power;
2495 power["@odata.id"] =
2496 "/redfish/v1/Chassis/" +
2497 sensorsAsyncResp->chassisId + "/" +
2498 sensorsAsyncResp->chassisSubNode + "#/" +
2499 fieldName + "/0";
2500 tempArray.push_back(std::move(power));
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002501 }
2502 sensorJson = &(tempArray.back());
2503 }
2504 else if (fieldName == "PowerSupplies")
2505 {
2506 if (inventoryItem != nullptr)
2507 {
2508 sensorJson =
2509 &(getPowerSupply(tempArray, *inventoryItem,
Ed Tanous81ce6092020-12-17 16:54:55 +00002510 sensorsAsyncResp->chassisId));
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002511 }
2512 }
Nan Zhou928fefb2022-03-28 08:45:00 -07002513 else if (fieldName == "Members")
2514 {
Ed Tanous677bb752022-09-15 10:52:19 -07002515 std::string sensorTypeEscaped(sensorType);
2516 sensorTypeEscaped.erase(
2517 std::remove(sensorTypeEscaped.begin(),
2518 sensorTypeEscaped.end(), '_'),
2519 sensorTypeEscaped.end());
2520 std::string sensorId(sensorTypeEscaped);
2521 sensorId += "_";
2522 sensorId += sensorName;
2523
Ed Tanous14766872022-03-15 10:44:42 -07002524 nlohmann::json::object_t member;
Ed Tanous677bb752022-09-15 10:52:19 -07002525 member["@odata.id"] = crow::utility::urlFromPieces(
2526 "redfish", "v1", "Chassis",
2527 sensorsAsyncResp->chassisId,
2528 sensorsAsyncResp->chassisSubNode, sensorId);
Ed Tanous14766872022-03-15 10:44:42 -07002529 tempArray.push_back(std::move(member));
Nan Zhou928fefb2022-03-28 08:45:00 -07002530 sensorJson = &(tempArray.back());
2531 }
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002532 else
2533 {
Ed Tanous14766872022-03-15 10:44:42 -07002534 nlohmann::json::object_t member;
2535 member["@odata.id"] = "/redfish/v1/Chassis/" +
2536 sensorsAsyncResp->chassisId +
2537 "/" +
2538 sensorsAsyncResp->chassisSubNode +
2539 "#/" + fieldName + "/";
2540 tempArray.push_back(std::move(member));
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002541 sensorJson = &(tempArray.back());
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002542 }
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002543 }
Shawn McCarneyde629b62019-03-08 10:42:51 -06002544
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002545 if (sensorJson != nullptr)
2546 {
Ed Tanous1d7c0052022-08-09 12:32:26 -07002547 objectInterfacesToJson(sensorName, sensorType,
2548 sensorsAsyncResp->chassisSubNode,
2549 objDictEntry.second, *sensorJson,
2550 inventoryItem);
2551
2552 std::string path = "/xyz/openbmc_project/sensors/";
2553 path += sensorType;
2554 path += "/";
2555 path += sensorName;
Ed Tanousc1d019a2022-08-06 09:36:06 -07002556 sensorsAsyncResp->addMetadata(*sensorJson, path);
Shawn McCarneyadc4f0d2019-07-24 09:21:50 -05002557 }
Shawn McCarneyde629b62019-03-08 10:42:51 -06002558 }
Ed Tanous81ce6092020-12-17 16:54:55 +00002559 if (sensorsAsyncResp.use_count() == 1)
James Feist8bd25cc2019-03-15 15:14:00 -07002560 {
Ed Tanous81ce6092020-12-17 16:54:55 +00002561 sortJSONResponse(sensorsAsyncResp);
Nan Zhou928fefb2022-03-28 08:45:00 -07002562 if (sensorsAsyncResp->chassisSubNode ==
2563 sensors::node::sensors &&
2564 sensorsAsyncResp->efficientExpand)
2565 {
2566 sensorsAsyncResp->asyncResp->res
2567 .jsonValue["Members@odata.count"] =
2568 sensorsAsyncResp->asyncResp->res.jsonValue["Members"]
2569 .size();
2570 }
2571 else if (sensorsAsyncResp->chassisSubNode ==
2572 sensors::node::thermal)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002573 {
Ed Tanous81ce6092020-12-17 16:54:55 +00002574 populateFanRedundancy(sensorsAsyncResp);
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002575 }
James Feist8bd25cc2019-03-15 15:14:00 -07002576 }
Shawn McCarneyde629b62019-03-08 10:42:51 -06002577 BMCWEB_LOG_DEBUG << "getManagedObjectsCb exit";
2578 };
2579
2580 // Find DBus object path that implements ObjectManager for the current
2581 // connection. If no mapping found, default to "/".
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002582 auto iter = objectMgrPaths->find(connection);
Shawn McCarneyde629b62019-03-08 10:42:51 -06002583 const std::string& objectMgrPath =
Shawn McCarney8fb49dd2019-06-12 17:47:00 -05002584 (iter != objectMgrPaths->end()) ? iter->second : "/";
Shawn McCarneyde629b62019-03-08 10:42:51 -06002585 BMCWEB_LOG_DEBUG << "ObjectManager path for " << connection << " is "
2586 << objectMgrPath;
2587
2588 crow::connections::systemBus->async_method_call(
2589 getManagedObjectsCb, connection, objectMgrPath,
2590 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
Ed Tanous23a21a12020-07-25 04:45:05 +00002591 }
Shawn McCarneyde629b62019-03-08 10:42:51 -06002592 BMCWEB_LOG_DEBUG << "getSensorData exit";
2593}
2594
Nan Zhoufe04d492022-06-22 17:10:41 +00002595inline void
2596 processSensorList(const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
2597 const std::shared_ptr<std::set<std::string>>& sensorNames)
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002598{
Nan Zhoufe04d492022-06-22 17:10:41 +00002599 auto getConnectionCb = [sensorsAsyncResp, sensorNames](
2600 const std::set<std::string>& connections) {
Ed Tanous002d39b2022-05-31 08:59:27 -07002601 BMCWEB_LOG_DEBUG << "getConnectionCb enter";
2602 auto getObjectManagerPathsCb =
Nan Zhoufe04d492022-06-22 17:10:41 +00002603 [sensorsAsyncResp, sensorNames, connections](
2604 const std::shared_ptr<std::map<std::string, std::string>>&
2605 objectMgrPaths) {
Ed Tanous002d39b2022-05-31 08:59:27 -07002606 BMCWEB_LOG_DEBUG << "getObjectManagerPathsCb enter";
2607 auto getInventoryItemsCb =
2608 [sensorsAsyncResp, sensorNames, connections, objectMgrPaths](
2609 const std::shared_ptr<std::vector<InventoryItem>>&
2610 inventoryItems) {
2611 BMCWEB_LOG_DEBUG << "getInventoryItemsCb enter";
2612 // Get sensor data and store results in JSON
2613 getSensorData(sensorsAsyncResp, sensorNames, connections,
2614 objectMgrPaths, inventoryItems);
2615 BMCWEB_LOG_DEBUG << "getInventoryItemsCb exit";
2616 };
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002617
Ed Tanous002d39b2022-05-31 08:59:27 -07002618 // Get inventory items associated with sensors
2619 getInventoryItems(sensorsAsyncResp, sensorNames, objectMgrPaths,
2620 std::move(getInventoryItemsCb));
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002621
Ed Tanous002d39b2022-05-31 08:59:27 -07002622 BMCWEB_LOG_DEBUG << "getObjectManagerPathsCb exit";
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002623 };
2624
Ed Tanous002d39b2022-05-31 08:59:27 -07002625 // Get mapping from connection names to the DBus object
2626 // paths that implement the ObjectManager interface
2627 getObjectManagerPaths(sensorsAsyncResp,
2628 std::move(getObjectManagerPathsCb));
2629 BMCWEB_LOG_DEBUG << "getConnectionCb exit";
2630 };
2631
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002632 // Get set of connections that provide sensor values
Ed Tanous81ce6092020-12-17 16:54:55 +00002633 getConnections(sensorsAsyncResp, sensorNames, std::move(getConnectionCb));
Anthony Wilson95a3eca2019-06-11 10:44:47 -05002634}
2635
Shawn McCarneyde629b62019-03-08 10:42:51 -06002636/**
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +01002637 * @brief Entry point for retrieving sensors data related to requested
2638 * chassis.
Kowalski, Kamil588c3f02018-04-03 14:55:27 +02002639 * @param SensorsAsyncResp Pointer to object holding response data
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +01002640 */
Ed Tanousb5a76932020-09-29 16:16:58 -07002641inline void
Ed Tanous81ce6092020-12-17 16:54:55 +00002642 getChassisData(const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp)
Ed Tanous1abe55e2018-09-05 08:30:59 -07002643{
2644 BMCWEB_LOG_DEBUG << "getChassisData enter";
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002645 auto getChassisCb =
Ed Tanous81ce6092020-12-17 16:54:55 +00002646 [sensorsAsyncResp](
Nan Zhoufe04d492022-06-22 17:10:41 +00002647 const std::shared_ptr<std::set<std::string>>& sensorNames) {
Ed Tanous002d39b2022-05-31 08:59:27 -07002648 BMCWEB_LOG_DEBUG << "getChassisCb enter";
2649 processSensorList(sensorsAsyncResp, sensorNames);
2650 BMCWEB_LOG_DEBUG << "getChassisCb exit";
2651 };
Nan Zhou928fefb2022-03-28 08:45:00 -07002652 // SensorCollection doesn't contain the Redundancy property
2653 if (sensorsAsyncResp->chassisSubNode != sensors::node::sensors)
2654 {
2655 sensorsAsyncResp->asyncResp->res.jsonValue["Redundancy"] =
2656 nlohmann::json::array();
2657 }
Shawn McCarney26f03892019-05-03 13:20:24 -05002658 // Get set of sensors in chassis
Ed Tanous7f1cc262022-08-09 13:33:57 -07002659 getChassis(sensorsAsyncResp->asyncResp, sensorsAsyncResp->chassisId,
2660 sensorsAsyncResp->chassisSubNode, sensorsAsyncResp->types,
2661 std::move(getChassisCb));
Ed Tanous1abe55e2018-09-05 08:30:59 -07002662 BMCWEB_LOG_DEBUG << "getChassisData exit";
Ed Tanous271584a2019-07-09 16:24:22 -07002663}
Lewanczyk, Dawid08777fb2018-03-22 23:33:49 +01002664
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302665/**
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002666 * @brief Find the requested sensorName in the list of all sensors supplied by
2667 * the chassis node
2668 *
2669 * @param sensorName The sensor name supplied in the PATCH request
2670 * @param sensorsList The list of sensors managed by the chassis node
2671 * @param sensorsModified The list of sensors that were found as a result of
2672 * repeated calls to this function
2673 */
Nan Zhoufe04d492022-06-22 17:10:41 +00002674inline bool
2675 findSensorNameUsingSensorPath(std::string_view sensorName,
Ed Tanous02cad962022-06-30 16:50:15 -07002676 const std::set<std::string>& sensorsList,
Nan Zhoufe04d492022-06-22 17:10:41 +00002677 std::set<std::string>& sensorsModified)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002678{
Nan Zhoufe04d492022-06-22 17:10:41 +00002679 for (const auto& chassisSensor : sensorsList)
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002680 {
George Liu28aa8de2021-02-01 15:13:30 +08002681 sdbusplus::message::object_path path(chassisSensor);
Ed Tanousb00dcc22021-02-23 12:52:50 -08002682 std::string thisSensorName = path.filename();
George Liu28aa8de2021-02-01 15:13:30 +08002683 if (thisSensorName.empty())
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002684 {
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002685 continue;
2686 }
2687 if (thisSensorName == sensorName)
2688 {
2689 sensorsModified.emplace(chassisSensor);
2690 return true;
2691 }
2692 }
2693 return false;
2694}
2695
2696/**
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302697 * @brief Entry point for overriding sensor values of given sensor
2698 *
zhanghch058d1b46d2021-04-01 11:18:24 +08002699 * @param sensorAsyncResp response object
Carol Wang4bb3dc32019-10-17 18:15:02 +08002700 * @param allCollections Collections extract from sensors' request patch info
jayaprakash Mutyala91e130a2020-03-04 22:26:38 +00002701 * @param chassisSubNode Chassis Node for which the query has to happen
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302702 */
Ed Tanous23a21a12020-07-25 04:45:05 +00002703inline void setSensorsOverride(
Ed Tanousb5a76932020-09-29 16:16:58 -07002704 const std::shared_ptr<SensorsAsyncResp>& sensorAsyncResp,
Carol Wang4bb3dc32019-10-17 18:15:02 +08002705 std::unordered_map<std::string, std::vector<nlohmann::json>>&
jayaprakash Mutyala397fd612020-02-06 23:33:34 +00002706 allCollections)
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302707{
jayaprakash Mutyala70d1d0a2020-01-21 23:41:36 +00002708 BMCWEB_LOG_INFO << "setSensorsOverride for subNode"
Carol Wang4bb3dc32019-10-17 18:15:02 +08002709 << sensorAsyncResp->chassisSubNode << "\n";
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302710
Ed Tanous543f4402022-01-06 13:12:53 -08002711 const char* propertyValueName = nullptr;
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302712 std::unordered_map<std::string, std::pair<double, std::string>> overrideMap;
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302713 std::string memberId;
Ed Tanous543f4402022-01-06 13:12:53 -08002714 double value = 0.0;
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302715 for (auto& collectionItems : allCollections)
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302716 {
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302717 if (collectionItems.first == "Temperatures")
2718 {
2719 propertyValueName = "ReadingCelsius";
2720 }
2721 else if (collectionItems.first == "Fans")
2722 {
2723 propertyValueName = "Reading";
2724 }
2725 else
2726 {
2727 propertyValueName = "ReadingVolts";
2728 }
2729 for (auto& item : collectionItems.second)
2730 {
zhanghch058d1b46d2021-04-01 11:18:24 +08002731 if (!json_util::readJson(item, sensorAsyncResp->asyncResp->res,
2732 "MemberId", memberId, propertyValueName,
2733 value))
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302734 {
2735 return;
2736 }
2737 overrideMap.emplace(memberId,
2738 std::make_pair(value, collectionItems.first));
2739 }
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302740 }
Carol Wang4bb3dc32019-10-17 18:15:02 +08002741
Ed Tanous002d39b2022-05-31 08:59:27 -07002742 auto getChassisSensorListCb =
2743 [sensorAsyncResp, overrideMap](
Nan Zhoufe04d492022-06-22 17:10:41 +00002744 const std::shared_ptr<std::set<std::string>>& sensorsList) {
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002745 // Match sensor names in the PATCH request to those managed by the
2746 // chassis node
Nan Zhoufe04d492022-06-22 17:10:41 +00002747 const std::shared_ptr<std::set<std::string>> sensorNames =
2748 std::make_shared<std::set<std::string>>();
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302749 for (const auto& item : overrideMap)
2750 {
2751 const auto& sensor = item.first;
Johnathan Mantey49c53ac2019-05-02 09:22:38 -07002752 if (!findSensorNameUsingSensorPath(sensor, *sensorsList,
2753 *sensorNames))
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302754 {
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302755 BMCWEB_LOG_INFO << "Unable to find memberId " << item.first;
zhanghch058d1b46d2021-04-01 11:18:24 +08002756 messages::resourceNotFound(sensorAsyncResp->asyncResp->res,
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302757 item.second.second, item.first);
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302758 return;
2759 }
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302760 }
2761 // Get the connection to which the memberId belongs
Ed Tanous002d39b2022-05-31 08:59:27 -07002762 auto getObjectsWithConnectionCb =
Nan Zhoufe04d492022-06-22 17:10:41 +00002763 [sensorAsyncResp,
2764 overrideMap](const std::set<std::string>& /*connections*/,
2765 const std::set<std::pair<std::string, std::string>>&
2766 objectsWithConnection) {
Jayaprakash Mutyala4f277b52021-12-08 22:46:49 +00002767 if (objectsWithConnection.size() != overrideMap.size())
2768 {
2769 BMCWEB_LOG_INFO
2770 << "Unable to find all objects with proper connection "
2771 << objectsWithConnection.size() << " requested "
2772 << overrideMap.size() << "\n";
2773 messages::resourceNotFound(sensorAsyncResp->asyncResp->res,
2774 sensorAsyncResp->chassisSubNode ==
2775 sensors::node::thermal
2776 ? "Temperatures"
2777 : "Voltages",
2778 "Count");
2779 return;
2780 }
2781 for (const auto& item : objectsWithConnection)
2782 {
2783 sdbusplus::message::object_path path(item.first);
2784 std::string sensorName = path.filename();
2785 if (sensorName.empty())
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302786 {
Jayaprakash Mutyala4f277b52021-12-08 22:46:49 +00002787 messages::internalError(sensorAsyncResp->asyncResp->res);
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302788 return;
2789 }
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302790
Jayaprakash Mutyala4f277b52021-12-08 22:46:49 +00002791 const auto& iterator = overrideMap.find(sensorName);
2792 if (iterator == overrideMap.end())
2793 {
2794 BMCWEB_LOG_INFO << "Unable to find sensor object"
2795 << item.first << "\n";
2796 messages::internalError(sensorAsyncResp->asyncResp->res);
2797 return;
2798 }
2799 crow::connections::systemBus->async_method_call(
2800 [sensorAsyncResp](const boost::system::error_code ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -07002801 if (ec)
2802 {
2803 if (ec.value() ==
2804 boost::system::errc::permission_denied)
Jayaprakash Mutyala4f277b52021-12-08 22:46:49 +00002805 {
Ed Tanous002d39b2022-05-31 08:59:27 -07002806 BMCWEB_LOG_WARNING
2807 << "Manufacturing mode is not Enabled...can't "
2808 "Override the sensor value. ";
Jayaprakash Mutyala4f277b52021-12-08 22:46:49 +00002809
Ed Tanous002d39b2022-05-31 08:59:27 -07002810 messages::insufficientPrivilege(
Jayaprakash Mutyala4f277b52021-12-08 22:46:49 +00002811 sensorAsyncResp->asyncResp->res);
Ed Tanous002d39b2022-05-31 08:59:27 -07002812 return;
Jayaprakash Mutyala4f277b52021-12-08 22:46:49 +00002813 }
Ed Tanous002d39b2022-05-31 08:59:27 -07002814 BMCWEB_LOG_DEBUG
2815 << "setOverrideValueStatus DBUS error: " << ec;
2816 messages::internalError(
2817 sensorAsyncResp->asyncResp->res);
2818 }
Jayaprakash Mutyala4f277b52021-12-08 22:46:49 +00002819 },
2820 item.second, item.first, "org.freedesktop.DBus.Properties",
2821 "Set", "xyz.openbmc_project.Sensor.Value", "Value",
Ed Tanous168e20c2021-12-13 14:39:53 -08002822 dbus::utility::DbusVariantType(iterator->second.first));
Jayaprakash Mutyala4f277b52021-12-08 22:46:49 +00002823 }
2824 };
Richard Marian Thomaiyarf65af9e2019-02-13 23:35:05 +05302825 // Get object with connection for the given sensor name
2826 getObjectsWithConnection(sensorAsyncResp, sensorNames,
2827 std::move(getObjectsWithConnectionCb));
2828 };
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302829 // get full sensor list for the given chassisId and cross verify the sensor.
Ed Tanous7f1cc262022-08-09 13:33:57 -07002830 getChassis(sensorAsyncResp->asyncResp, sensorAsyncResp->chassisId,
2831 sensorAsyncResp->chassisSubNode, sensorAsyncResp->types,
2832 std::move(getChassisSensorListCb));
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +05302833}
2834
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02002835/**
2836 * @brief Retrieves mapping of Redfish URIs to sensor value property to D-Bus
2837 * path of the sensor.
2838 *
2839 * Function builds valid Redfish response for sensor query of given chassis and
2840 * node. It then builds metadata about Redfish<->D-Bus correlations and provides
2841 * it to caller in a callback.
2842 *
2843 * @param chassis Chassis for which retrieval should be performed
2844 * @param node Node (group) of sensors. See sensors::node for supported values
2845 * @param mapComplete Callback to be called with retrieval result
2846 */
Krzysztof Grobelny021d32c2021-10-29 16:00:07 +02002847inline void retrieveUriToDbusMap(const std::string& chassis,
2848 const std::string& node,
2849 SensorsAsyncResp::DataCompleteCb&& mapComplete)
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02002850{
Ed Tanous02da7c52022-02-27 00:09:02 -08002851 decltype(sensors::paths)::const_iterator pathIt =
2852 std::find_if(sensors::paths.cbegin(), sensors::paths.cend(),
2853 [&node](auto&& val) { return val.first == node; });
2854 if (pathIt == sensors::paths.cend())
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02002855 {
2856 BMCWEB_LOG_ERROR << "Wrong node provided : " << node;
2857 mapComplete(boost::beast::http::status::bad_request, {});
2858 return;
2859 }
Krzysztof Grobelnyd51e0722021-04-16 13:15:21 +00002860
Nan Zhou72374eb2022-01-27 17:06:51 -08002861 auto asyncResp = std::make_shared<bmcweb::AsyncResp>();
Nan Zhoufe04d492022-06-22 17:10:41 +00002862 auto callback = [asyncResp, mapCompleteCb{std::move(mapComplete)}](
2863 const boost::beast::http::status status,
2864 const std::map<std::string, std::string>& uriToDbus) {
2865 mapCompleteCb(status, uriToDbus);
2866 };
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02002867
2868 auto resp = std::make_shared<SensorsAsyncResp>(
Krzysztof Grobelnyd51e0722021-04-16 13:15:21 +00002869 asyncResp, chassis, pathIt->second, node, std::move(callback));
Adrian Ambrożewicza0ec28b2020-04-10 14:47:28 +02002870 getChassisData(resp);
2871}
2872
Nan Zhoubacb2162022-04-06 11:28:32 -07002873namespace sensors
2874{
Nan Zhou928fefb2022-03-28 08:45:00 -07002875
Nan Zhoubacb2162022-04-06 11:28:32 -07002876inline void getChassisCallback(
Ed Tanousc1d019a2022-08-06 09:36:06 -07002877 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2878 std::string_view chassisId, std::string_view chassisSubNode,
Nan Zhoufe04d492022-06-22 17:10:41 +00002879 const std::shared_ptr<std::set<std::string>>& sensorNames)
Nan Zhoubacb2162022-04-06 11:28:32 -07002880{
Ed Tanousc1d019a2022-08-06 09:36:06 -07002881 BMCWEB_LOG_DEBUG << "getChassisCallback enter ";
Nan Zhoubacb2162022-04-06 11:28:32 -07002882
Ed Tanousc1d019a2022-08-06 09:36:06 -07002883 nlohmann::json& entriesArray = asyncResp->res.jsonValue["Members"];
2884 for (const std::string& sensor : *sensorNames)
Nan Zhoubacb2162022-04-06 11:28:32 -07002885 {
2886 BMCWEB_LOG_DEBUG << "Adding sensor: " << sensor;
2887
2888 sdbusplus::message::object_path path(sensor);
2889 std::string sensorName = path.filename();
2890 if (sensorName.empty())
2891 {
2892 BMCWEB_LOG_ERROR << "Invalid sensor path: " << sensor;
Ed Tanousc1d019a2022-08-06 09:36:06 -07002893 messages::internalError(asyncResp->res);
Nan Zhoubacb2162022-04-06 11:28:32 -07002894 return;
2895 }
Ed Tanousc1d019a2022-08-06 09:36:06 -07002896 std::string type = path.parent_path().filename();
2897 // fan_tach has an underscore in it, so remove it to "normalize" the
2898 // type in the URI
2899 type.erase(std::remove(type.begin(), type.end(), '_'), type.end());
2900
Ed Tanous14766872022-03-15 10:44:42 -07002901 nlohmann::json::object_t member;
Ed Tanousc1d019a2022-08-06 09:36:06 -07002902 std::string id = type;
2903 id += "_";
2904 id += sensorName;
2905 member["@odata.id"] = crow::utility::urlFromPieces(
2906 "redfish", "v1", "Chassis", chassisId, chassisSubNode, id);
2907
Ed Tanous14766872022-03-15 10:44:42 -07002908 entriesArray.push_back(std::move(member));
Nan Zhoubacb2162022-04-06 11:28:32 -07002909 }
2910
Ed Tanousc1d019a2022-08-06 09:36:06 -07002911 asyncResp->res.jsonValue["Members@odata.count"] = entriesArray.size();
Nan Zhoubacb2162022-04-06 11:28:32 -07002912 BMCWEB_LOG_DEBUG << "getChassisCallback exit";
2913}
Nan Zhoue6bd8462022-06-01 04:35:35 +00002914
Nan Zhoude167a62022-06-01 04:47:45 +00002915inline void
2916 handleSensorCollectionGet(App& app, const crow::Request& req,
2917 const std::shared_ptr<bmcweb::AsyncResp>& aResp,
2918 const std::string& chassisId)
2919{
2920 query_param::QueryCapabilities capabilities = {
2921 .canDelegateExpandLevel = 1,
2922 };
2923 query_param::Query delegatedQuery;
Carson Labrado3ba00072022-06-06 19:40:56 +00002924 if (!redfish::setUpRedfishRouteWithDelegation(app, req, aResp,
Nan Zhoude167a62022-06-01 04:47:45 +00002925 delegatedQuery, capabilities))
2926 {
2927 return;
2928 }
2929
2930 if (delegatedQuery.expandType != query_param::ExpandType::None)
2931 {
2932 // we perform efficient expand.
2933 auto asyncResp = std::make_shared<SensorsAsyncResp>(
2934 aResp, chassisId, sensors::dbus::sensorPaths,
2935 sensors::node::sensors,
2936 /*efficientExpand=*/true);
2937 getChassisData(asyncResp);
2938
2939 BMCWEB_LOG_DEBUG
2940 << "SensorCollection doGet exit via efficient expand handler";
2941 return;
Ed Tanous0bad3202022-06-02 13:53:59 -07002942 }
Nan Zhoude167a62022-06-01 04:47:45 +00002943
Nan Zhoude167a62022-06-01 04:47:45 +00002944 // We get all sensors as hyperlinkes in the chassis (this
2945 // implies we reply on the default query parameters handler)
Ed Tanousc1d019a2022-08-06 09:36:06 -07002946 getChassis(aResp, chassisId, sensors::node::sensors, dbus::sensorPaths,
2947 std::bind_front(sensors::getChassisCallback, aResp, chassisId,
2948 sensors::node::sensors));
2949}
Ed Tanous7f1cc262022-08-09 13:33:57 -07002950
Ed Tanousc1d019a2022-08-06 09:36:06 -07002951inline void
2952 getSensorFromDbus(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2953 const std::string& sensorPath,
2954 const ::dbus::utility::MapperGetObject& mapperResponse)
2955{
2956 if (mapperResponse.size() != 1)
2957 {
2958 messages::internalError(asyncResp->res);
2959 return;
2960 }
2961 const auto& valueIface = *mapperResponse.begin();
2962 const std::string& connectionName = valueIface.first;
2963 BMCWEB_LOG_DEBUG << "Looking up " << connectionName;
2964 BMCWEB_LOG_DEBUG << "Path " << sensorPath;
Krzysztof Grobelnyc1343bf2022-08-31 13:15:26 +02002965
2966 sdbusplus::asio::getAllProperties(
2967 *crow::connections::systemBus, connectionName, sensorPath, "",
Ed Tanousc1d019a2022-08-06 09:36:06 -07002968 [asyncResp,
2969 sensorPath](const boost::system::error_code ec,
2970 const ::dbus::utility::DBusPropertiesMap& valuesDict) {
2971 if (ec)
2972 {
2973 messages::internalError(asyncResp->res);
2974 return;
2975 }
2976 sdbusplus::message::object_path path(sensorPath);
2977 std::string name = path.filename();
2978 path = path.parent_path();
2979 std::string type = path.filename();
2980 objectPropertiesToJson(name, type, sensors::node::sensors, valuesDict,
2981 asyncResp->res.jsonValue, nullptr);
Krzysztof Grobelnyc1343bf2022-08-31 13:15:26 +02002982 });
Nan Zhoude167a62022-06-01 04:47:45 +00002983}
2984
Nan Zhoue6bd8462022-06-01 04:35:35 +00002985inline void handleSensorGet(App& app, const crow::Request& req,
Ed Tanousc1d019a2022-08-06 09:36:06 -07002986 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous677bb752022-09-15 10:52:19 -07002987 const std::string& chassisId,
Ed Tanousc1d019a2022-08-06 09:36:06 -07002988 const std::string& sensorId)
Nan Zhoue6bd8462022-06-01 04:35:35 +00002989{
Ed Tanousc1d019a2022-08-06 09:36:06 -07002990 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Nan Zhoue6bd8462022-06-01 04:35:35 +00002991 {
2992 return;
2993 }
Ed Tanousc1d019a2022-08-06 09:36:06 -07002994 size_t index = sensorId.find('_');
2995 if (index == std::string::npos)
2996 {
2997 messages::resourceNotFound(asyncResp->res, sensorId, "Sensor");
2998 return;
2999 }
Ed Tanous677bb752022-09-15 10:52:19 -07003000 asyncResp->res.jsonValue["@odata.id"] = crow::utility::urlFromPieces(
3001 "redfish", "v1", "Chassis", chassisId, "Sensors", sensorId);
Ed Tanousc1d019a2022-08-06 09:36:06 -07003002 std::string sensorType = sensorId.substr(0, index);
3003 std::string sensorName = sensorId.substr(index + 1);
3004 // fan_pwm and fan_tach need special handling
3005 if (sensorType == "fantach" || sensorType == "fanpwm")
3006 {
3007 sensorType.insert(3, 1, '_');
3008 }
3009
Nan Zhoue6bd8462022-06-01 04:35:35 +00003010 BMCWEB_LOG_DEBUG << "Sensor doGet enter";
Nan Zhoue6bd8462022-06-01 04:35:35 +00003011
3012 const std::array<const char*, 1> interfaces = {
3013 "xyz.openbmc_project.Sensor.Value"};
Ed Tanousc1d019a2022-08-06 09:36:06 -07003014 std::string sensorPath =
3015 "/xyz/openbmc_project/sensors/" + sensorType + '/' + sensorName;
Nan Zhoue6bd8462022-06-01 04:35:35 +00003016 // Get a list of all of the sensors that implement Sensor.Value
3017 // and get the path and service name associated with the sensor
3018 crow::connections::systemBus->async_method_call(
Ed Tanousc1d019a2022-08-06 09:36:06 -07003019 [asyncResp, sensorPath,
Nan Zhoue6bd8462022-06-01 04:35:35 +00003020 sensorName](const boost::system::error_code ec,
Ed Tanousc1d019a2022-08-06 09:36:06 -07003021 const ::dbus::utility::MapperGetObject& subtree) {
Nan Zhoue6bd8462022-06-01 04:35:35 +00003022 BMCWEB_LOG_DEBUG << "respHandler1 enter";
3023 if (ec)
3024 {
Ed Tanousc1d019a2022-08-06 09:36:06 -07003025 messages::internalError(asyncResp->res);
Nan Zhoue6bd8462022-06-01 04:35:35 +00003026 BMCWEB_LOG_ERROR << "Sensor getSensorPaths resp_handler: "
3027 << "Dbus error " << ec;
3028 return;
3029 }
Ed Tanousc1d019a2022-08-06 09:36:06 -07003030 getSensorFromDbus(asyncResp, sensorPath, subtree);
Nan Zhoue6bd8462022-06-01 04:35:35 +00003031 BMCWEB_LOG_DEBUG << "respHandler1 exit";
3032 },
3033 "xyz.openbmc_project.ObjectMapper",
3034 "/xyz/openbmc_project/object_mapper",
Ed Tanousc1d019a2022-08-06 09:36:06 -07003035 "xyz.openbmc_project.ObjectMapper", "GetObject", sensorPath,
3036 interfaces);
Nan Zhoue6bd8462022-06-01 04:35:35 +00003037}
3038
Nan Zhoubacb2162022-04-06 11:28:32 -07003039} // namespace sensors
3040
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003041inline void requestRoutesSensorCollection(App& app)
Anthony Wilson95a3eca2019-06-11 10:44:47 -05003042{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003043 BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/Sensors/")
Ed Tanoused398212021-06-09 17:05:54 -07003044 .privileges(redfish::privileges::getSensorCollection)
Ed Tanous002d39b2022-05-31 08:59:27 -07003045 .methods(boost::beast::http::verb::get)(
Nan Zhoude167a62022-06-01 04:47:45 +00003046 std::bind_front(sensors::handleSensorCollectionGet, std::ref(app)));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003047}
Anthony Wilson95a3eca2019-06-11 10:44:47 -05003048
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003049inline void requestRoutesSensor(App& app)
3050{
3051 BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/Sensors/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07003052 .privileges(redfish::privileges::getSensor)
Ed Tanous002d39b2022-05-31 08:59:27 -07003053 .methods(boost::beast::http::verb::get)(
Nan Zhoue6bd8462022-06-01 04:35:35 +00003054 std::bind_front(sensors::handleSensorGet, std::ref(app)));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003055}
Anthony Wilson95a3eca2019-06-11 10:44:47 -05003056
Ed Tanous1abe55e2018-09-05 08:30:59 -07003057} // namespace redfish