Ed Tanous | 40e9b92 | 2024-09-10 13:50:16 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: Apache-2.0 |
| 2 | // SPDX-FileCopyrightText: Copyright OpenBMC Authors |
| 3 | // SPDX-FileCopyrightText: Copyright 2018 Intel Corporation |
Lewanczyk, Dawid | 08777fb | 2018-03-22 23:33:49 +0100 | [diff] [blame] | 4 | #pragma once |
| 5 | |
Ed Tanous | d785720 | 2025-01-28 15:32:26 -0800 | [diff] [blame] | 6 | #include "bmcweb_config.h" |
| 7 | |
Ed Tanous | 3ccb3ad | 2023-01-13 17:40:03 -0800 | [diff] [blame] | 8 | #include "app.hpp" |
Ed Tanous | d785720 | 2025-01-28 15:32:26 -0800 | [diff] [blame] | 9 | #include "async_resp.hpp" |
Ed Tanous | 3ccb3ad | 2023-01-13 17:40:03 -0800 | [diff] [blame] | 10 | #include "dbus_singleton.hpp" |
George Liu | 7a1dbc4 | 2022-12-07 16:03:22 +0800 | [diff] [blame] | 11 | #include "dbus_utility.hpp" |
Ed Tanous | d785720 | 2025-01-28 15:32:26 -0800 | [diff] [blame] | 12 | #include "error_messages.hpp" |
Ed Tanous | 539d8c6 | 2024-06-19 14:38:27 -0700 | [diff] [blame] | 13 | #include "generated/enums/redundancy.hpp" |
Matt Simmering | aaf08ac | 2023-10-04 08:41:01 -0700 | [diff] [blame] | 14 | #include "generated/enums/resource.hpp" |
Ed Tanous | d785720 | 2025-01-28 15:32:26 -0800 | [diff] [blame] | 15 | #include "http_request.hpp" |
| 16 | #include "logging.hpp" |
Ed Tanous | 3ccb3ad | 2023-01-13 17:40:03 -0800 | [diff] [blame] | 17 | #include "query.hpp" |
| 18 | #include "registries/privilege_registry.hpp" |
Ed Tanous | 50ebd4a | 2023-01-19 19:03:17 -0800 | [diff] [blame] | 19 | #include "str_utility.hpp" |
Myung Bae | 3f95a27 | 2024-03-13 07:32:02 -0700 | [diff] [blame] | 20 | #include "utils/chassis_utils.hpp" |
Ed Tanous | 3ccb3ad | 2023-01-13 17:40:03 -0800 | [diff] [blame] | 21 | #include "utils/dbus_utils.hpp" |
| 22 | #include "utils/json_utils.hpp" |
| 23 | #include "utils/query_param.hpp" |
Janet Adkins | 1516c21 | 2024-08-14 13:22:41 -0500 | [diff] [blame] | 24 | #include "utils/sensor_utils.hpp" |
Ed Tanous | 0ec8b83 | 2022-03-14 14:56:47 -0700 | [diff] [blame] | 25 | |
Ed Tanous | d785720 | 2025-01-28 15:32:26 -0800 | [diff] [blame] | 26 | #include <asm-generic/errno.h> |
| 27 | |
| 28 | #include <boost/beast/http/status.hpp> |
| 29 | #include <boost/beast/http/verb.hpp> |
George Liu | e99073f | 2022-12-09 11:06:16 +0800 | [diff] [blame] | 30 | #include <boost/system/error_code.hpp> |
Ed Tanous | ef4c65b | 2023-04-24 15:28:50 -0700 | [diff] [blame] | 31 | #include <boost/url/format.hpp> |
Ed Tanous | d785720 | 2025-01-28 15:32:26 -0800 | [diff] [blame] | 32 | #include <boost/url/url.hpp> |
| 33 | #include <nlohmann/json.hpp> |
| 34 | #include <sdbusplus/message/native_types.hpp> |
Krzysztof Grobelny | 86d89ed | 2022-08-29 14:49:20 +0200 | [diff] [blame] | 35 | #include <sdbusplus/unpack_properties.hpp> |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 36 | |
Ed Tanous | d785720 | 2025-01-28 15:32:26 -0800 | [diff] [blame] | 37 | #include <algorithm> |
George Liu | 7a1dbc4 | 2022-12-07 16:03:22 +0800 | [diff] [blame] | 38 | #include <array> |
Ed Tanous | d785720 | 2025-01-28 15:32:26 -0800 | [diff] [blame] | 39 | #include <cstddef> |
| 40 | #include <cstdint> |
| 41 | #include <functional> |
Nan Zhou | fe04d49 | 2022-06-22 17:10:41 +0000 | [diff] [blame] | 42 | #include <iterator> |
| 43 | #include <map> |
Ed Tanous | d785720 | 2025-01-28 15:32:26 -0800 | [diff] [blame] | 44 | #include <memory> |
| 45 | #include <optional> |
Ed Tanous | 3544d2a | 2023-08-06 18:12:20 -0700 | [diff] [blame] | 46 | #include <ranges> |
Nan Zhou | fe04d49 | 2022-06-22 17:10:41 +0000 | [diff] [blame] | 47 | #include <set> |
Ed Tanous | d785720 | 2025-01-28 15:32:26 -0800 | [diff] [blame] | 48 | #include <span> |
Ed Tanous | 18f8f60 | 2023-07-18 10:07:23 -0700 | [diff] [blame] | 49 | #include <string> |
George Liu | 7a1dbc4 | 2022-12-07 16:03:22 +0800 | [diff] [blame] | 50 | #include <string_view> |
Ed Tanous | d785720 | 2025-01-28 15:32:26 -0800 | [diff] [blame] | 51 | #include <unordered_map> |
Ed Tanous | b5a7693 | 2020-09-29 16:16:58 -0700 | [diff] [blame] | 52 | #include <utility> |
Ed Tanous | abf2add | 2019-01-22 16:40:12 -0800 | [diff] [blame] | 53 | #include <variant> |
Ed Tanous | d785720 | 2025-01-28 15:32:26 -0800 | [diff] [blame] | 54 | #include <vector> |
Lewanczyk, Dawid | 08777fb | 2018-03-22 23:33:49 +0100 | [diff] [blame] | 55 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 56 | namespace redfish |
| 57 | { |
Lewanczyk, Dawid | 08777fb | 2018-03-22 23:33:49 +0100 | [diff] [blame] | 58 | |
Adrian Ambrożewicz | a0ec28b | 2020-04-10 14:47:28 +0200 | [diff] [blame] | 59 | namespace sensors |
| 60 | { |
Adrian Ambrożewicz | a0ec28b | 2020-04-10 14:47:28 +0200 | [diff] [blame] | 61 | |
Ed Tanous | 02da7c5 | 2022-02-27 00:09:02 -0800 | [diff] [blame] | 62 | // clang-format off |
Adrian Ambrożewicz | a0ec28b | 2020-04-10 14:47:28 +0200 | [diff] [blame] | 63 | namespace dbus |
| 64 | { |
Ed Tanous | cf9e417 | 2022-12-21 09:30:16 -0800 | [diff] [blame] | 65 | constexpr auto powerPaths = std::to_array<std::string_view>({ |
Ed Tanous | 02da7c5 | 2022-02-27 00:09:02 -0800 | [diff] [blame] | 66 | "/xyz/openbmc_project/sensors/voltage", |
| 67 | "/xyz/openbmc_project/sensors/power" |
| 68 | }); |
Wludzik, Jozef | c2bf7f9 | 2021-03-08 14:35:54 +0000 | [diff] [blame] | 69 | |
Ed Tanous | 25b54db | 2024-04-17 15:40:31 -0700 | [diff] [blame] | 70 | constexpr auto getSensorPaths(){ |
| 71 | if constexpr(BMCWEB_REDFISH_NEW_POWERSUBSYSTEM_THERMALSUBSYSTEM){ |
| 72 | return std::to_array<std::string_view>({ |
| 73 | "/xyz/openbmc_project/sensors/power", |
| 74 | "/xyz/openbmc_project/sensors/current", |
| 75 | "/xyz/openbmc_project/sensors/airflow", |
| 76 | "/xyz/openbmc_project/sensors/humidity", |
| 77 | "/xyz/openbmc_project/sensors/voltage", |
| 78 | "/xyz/openbmc_project/sensors/fan_tach", |
| 79 | "/xyz/openbmc_project/sensors/temperature", |
| 80 | "/xyz/openbmc_project/sensors/fan_pwm", |
| 81 | "/xyz/openbmc_project/sensors/altitude", |
| 82 | "/xyz/openbmc_project/sensors/energy", |
Zev Weiss | 4491419 | 2025-03-11 07:59:30 +0000 | [diff] [blame] | 83 | "/xyz/openbmc_project/sensors/liquidflow", |
| 84 | "/xyz/openbmc_project/sensors/pressure", |
Ed Tanous | 25b54db | 2024-04-17 15:40:31 -0700 | [diff] [blame] | 85 | "/xyz/openbmc_project/sensors/utilization"}); |
| 86 | } else { |
| 87 | return std::to_array<std::string_view>({"/xyz/openbmc_project/sensors/power", |
| 88 | "/xyz/openbmc_project/sensors/current", |
| 89 | "/xyz/openbmc_project/sensors/airflow", |
| 90 | "/xyz/openbmc_project/sensors/humidity", |
| 91 | "/xyz/openbmc_project/sensors/utilization"}); |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | constexpr auto sensorPaths = getSensorPaths(); |
Ed Tanous | 02da7c5 | 2022-02-27 00:09:02 -0800 | [diff] [blame] | 96 | |
Ed Tanous | cf9e417 | 2022-12-21 09:30:16 -0800 | [diff] [blame] | 97 | constexpr auto thermalPaths = std::to_array<std::string_view>({ |
Ed Tanous | 02da7c5 | 2022-02-27 00:09:02 -0800 | [diff] [blame] | 98 | "/xyz/openbmc_project/sensors/fan_tach", |
| 99 | "/xyz/openbmc_project/sensors/temperature", |
| 100 | "/xyz/openbmc_project/sensors/fan_pwm" |
| 101 | }); |
| 102 | |
Wludzik, Jozef | c2bf7f9 | 2021-03-08 14:35:54 +0000 | [diff] [blame] | 103 | } // namespace dbus |
Ed Tanous | 02da7c5 | 2022-02-27 00:09:02 -0800 | [diff] [blame] | 104 | // clang-format on |
| 105 | |
Janet Adkins | 0c728b4 | 2024-08-29 11:09:10 -0500 | [diff] [blame] | 106 | constexpr std::string_view powerNodeStr = sensor_utils::chassisSubNodeToString( |
| 107 | sensor_utils::ChassisSubNode::powerNode); |
| 108 | constexpr std::string_view sensorsNodeStr = |
| 109 | sensor_utils::chassisSubNodeToString( |
| 110 | sensor_utils::ChassisSubNode::sensorsNode); |
| 111 | constexpr std::string_view thermalNodeStr = |
| 112 | sensor_utils::chassisSubNodeToString( |
| 113 | sensor_utils::ChassisSubNode::thermalNode); |
| 114 | |
Ed Tanous | cf9e417 | 2022-12-21 09:30:16 -0800 | [diff] [blame] | 115 | using sensorPair = |
| 116 | std::pair<std::string_view, std::span<const std::string_view>>; |
Ed Tanous | 02da7c5 | 2022-02-27 00:09:02 -0800 | [diff] [blame] | 117 | static constexpr std::array<sensorPair, 3> paths = { |
Janet Adkins | 0c728b4 | 2024-08-29 11:09:10 -0500 | [diff] [blame] | 118 | {{sensors::powerNodeStr, dbus::powerPaths}, |
| 119 | {sensors::sensorsNodeStr, dbus::sensorPaths}, |
| 120 | {sensors::thermalNodeStr, dbus::thermalPaths}}}; |
Wludzik, Jozef | c2bf7f9 | 2021-03-08 14:35:54 +0000 | [diff] [blame] | 121 | |
Adrian Ambrożewicz | a0ec28b | 2020-04-10 14:47:28 +0200 | [diff] [blame] | 122 | } // namespace sensors |
| 123 | |
Lewanczyk, Dawid | 08777fb | 2018-03-22 23:33:49 +0100 | [diff] [blame] | 124 | /** |
Kowalski, Kamil | 588c3f0 | 2018-04-03 14:55:27 +0200 | [diff] [blame] | 125 | * SensorsAsyncResp |
Lewanczyk, Dawid | 08777fb | 2018-03-22 23:33:49 +0100 | [diff] [blame] | 126 | * Gathers data needed for response processing after async calls are done |
| 127 | */ |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 128 | class SensorsAsyncResp |
| 129 | { |
| 130 | public: |
Adrian Ambrożewicz | a0ec28b | 2020-04-10 14:47:28 +0200 | [diff] [blame] | 131 | using DataCompleteCb = std::function<void( |
| 132 | const boost::beast::http::status status, |
Nan Zhou | fe04d49 | 2022-06-22 17:10:41 +0000 | [diff] [blame] | 133 | const std::map<std::string, std::string>& uriToDbus)>; |
Adrian Ambrożewicz | a0ec28b | 2020-04-10 14:47:28 +0200 | [diff] [blame] | 134 | |
| 135 | struct SensorData |
| 136 | { |
Ed Tanous | f836c1d | 2024-09-06 16:05:11 -0700 | [diff] [blame] | 137 | std::string name; |
Adrian Ambrożewicz | a0ec28b | 2020-04-10 14:47:28 +0200 | [diff] [blame] | 138 | std::string uri; |
Ed Tanous | f836c1d | 2024-09-06 16:05:11 -0700 | [diff] [blame] | 139 | std::string dbusPath; |
Adrian Ambrożewicz | a0ec28b | 2020-04-10 14:47:28 +0200 | [diff] [blame] | 140 | }; |
| 141 | |
Ed Tanous | 8a59281 | 2022-06-04 09:06:59 -0700 | [diff] [blame] | 142 | SensorsAsyncResp(const std::shared_ptr<bmcweb::AsyncResp>& asyncRespIn, |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 143 | const std::string& chassisIdIn, |
Ed Tanous | cf9e417 | 2022-12-21 09:30:16 -0800 | [diff] [blame] | 144 | std::span<const std::string_view> typesIn, |
Ed Tanous | 02da7c5 | 2022-02-27 00:09:02 -0800 | [diff] [blame] | 145 | std::string_view subNode) : |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 146 | asyncResp(asyncRespIn), chassisId(chassisIdIn), types(typesIn), |
| 147 | chassisSubNode(subNode), efficientExpand(false) |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 148 | {} |
Kowalski, Kamil | 588c3f0 | 2018-04-03 14:55:27 +0200 | [diff] [blame] | 149 | |
Adrian Ambrożewicz | a0ec28b | 2020-04-10 14:47:28 +0200 | [diff] [blame] | 150 | // Store extra data about sensor mapping and return it in callback |
Ed Tanous | 8a59281 | 2022-06-04 09:06:59 -0700 | [diff] [blame] | 151 | SensorsAsyncResp(const std::shared_ptr<bmcweb::AsyncResp>& asyncRespIn, |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 152 | const std::string& chassisIdIn, |
Ed Tanous | cf9e417 | 2022-12-21 09:30:16 -0800 | [diff] [blame] | 153 | std::span<const std::string_view> typesIn, |
Ed Tanous | 02da7c5 | 2022-02-27 00:09:02 -0800 | [diff] [blame] | 154 | std::string_view subNode, |
Adrian Ambrożewicz | a0ec28b | 2020-04-10 14:47:28 +0200 | [diff] [blame] | 155 | DataCompleteCb&& creationComplete) : |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 156 | asyncResp(asyncRespIn), chassisId(chassisIdIn), types(typesIn), |
| 157 | chassisSubNode(subNode), efficientExpand(false), |
| 158 | metadata{std::vector<SensorData>()}, |
Adrian Ambrożewicz | a0ec28b | 2020-04-10 14:47:28 +0200 | [diff] [blame] | 159 | dataComplete{std::move(creationComplete)} |
| 160 | {} |
| 161 | |
Nan Zhou | 928fefb | 2022-03-28 08:45:00 -0700 | [diff] [blame] | 162 | // sensor collections expand |
Ed Tanous | 8a59281 | 2022-06-04 09:06:59 -0700 | [diff] [blame] | 163 | SensorsAsyncResp(const std::shared_ptr<bmcweb::AsyncResp>& asyncRespIn, |
Nan Zhou | 928fefb | 2022-03-28 08:45:00 -0700 | [diff] [blame] | 164 | const std::string& chassisIdIn, |
Ed Tanous | cf9e417 | 2022-12-21 09:30:16 -0800 | [diff] [blame] | 165 | std::span<const std::string_view> typesIn, |
Ed Tanous | 8a59281 | 2022-06-04 09:06:59 -0700 | [diff] [blame] | 166 | const std::string_view& subNode, bool efficientExpandIn) : |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 167 | asyncResp(asyncRespIn), chassisId(chassisIdIn), types(typesIn), |
| 168 | chassisSubNode(subNode), efficientExpand(efficientExpandIn) |
Nan Zhou | 928fefb | 2022-03-28 08:45:00 -0700 | [diff] [blame] | 169 | {} |
| 170 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 171 | ~SensorsAsyncResp() |
| 172 | { |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 173 | if (asyncResp->res.result() == |
| 174 | boost::beast::http::status::internal_server_error) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 175 | { |
| 176 | // Reset the json object to clear out any data that made it in |
| 177 | // before the error happened todo(ed) handle error condition with |
| 178 | // proper code |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 179 | asyncResp->res.jsonValue = nlohmann::json::object(); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 180 | } |
Adrian Ambrożewicz | a0ec28b | 2020-04-10 14:47:28 +0200 | [diff] [blame] | 181 | |
| 182 | if (dataComplete && metadata) |
| 183 | { |
Nan Zhou | fe04d49 | 2022-06-22 17:10:41 +0000 | [diff] [blame] | 184 | std::map<std::string, std::string> map; |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 185 | if (asyncResp->res.result() == boost::beast::http::status::ok) |
Adrian Ambrożewicz | a0ec28b | 2020-04-10 14:47:28 +0200 | [diff] [blame] | 186 | { |
| 187 | for (auto& sensor : *metadata) |
| 188 | { |
Ed Tanous | c1d019a | 2022-08-06 09:36:06 -0700 | [diff] [blame] | 189 | map.emplace(sensor.uri, sensor.dbusPath); |
Adrian Ambrożewicz | a0ec28b | 2020-04-10 14:47:28 +0200 | [diff] [blame] | 190 | } |
| 191 | } |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 192 | dataComplete(asyncResp->res.result(), map); |
Adrian Ambrożewicz | a0ec28b | 2020-04-10 14:47:28 +0200 | [diff] [blame] | 193 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 194 | } |
Lewanczyk, Dawid | 08777fb | 2018-03-22 23:33:49 +0100 | [diff] [blame] | 195 | |
Ed Tanous | ecd6a3a | 2022-01-07 09:18:40 -0800 | [diff] [blame] | 196 | SensorsAsyncResp(const SensorsAsyncResp&) = delete; |
| 197 | SensorsAsyncResp(SensorsAsyncResp&&) = delete; |
| 198 | SensorsAsyncResp& operator=(const SensorsAsyncResp&) = delete; |
| 199 | SensorsAsyncResp& operator=(SensorsAsyncResp&&) = delete; |
| 200 | |
Adrian Ambrożewicz | a0ec28b | 2020-04-10 14:47:28 +0200 | [diff] [blame] | 201 | void addMetadata(const nlohmann::json& sensorObject, |
Ed Tanous | c1d019a | 2022-08-06 09:36:06 -0700 | [diff] [blame] | 202 | const std::string& dbusPath) |
Adrian Ambrożewicz | a0ec28b | 2020-04-10 14:47:28 +0200 | [diff] [blame] | 203 | { |
Ed Tanous | 82b286f | 2025-05-06 13:29:48 -0700 | [diff] [blame] | 204 | if (!metadata) |
Adrian Ambrożewicz | a0ec28b | 2020-04-10 14:47:28 +0200 | [diff] [blame] | 205 | { |
Ed Tanous | 82b286f | 2025-05-06 13:29:48 -0700 | [diff] [blame] | 206 | return; |
Adrian Ambrożewicz | a0ec28b | 2020-04-10 14:47:28 +0200 | [diff] [blame] | 207 | } |
Ed Tanous | 82b286f | 2025-05-06 13:29:48 -0700 | [diff] [blame] | 208 | const auto nameIt = sensorObject.find("Name"); |
James Athappilly | d9cca62 | 2025-06-13 08:59:46 -0700 | [diff] [blame] | 209 | if (nameIt == sensorObject.end()) |
Ed Tanous | 82b286f | 2025-05-06 13:29:48 -0700 | [diff] [blame] | 210 | { |
| 211 | return; |
| 212 | } |
| 213 | const auto idIt = sensorObject.find("@odata.id"); |
James Athappilly | d9cca62 | 2025-06-13 08:59:46 -0700 | [diff] [blame] | 214 | if (idIt == sensorObject.end()) |
Ed Tanous | 82b286f | 2025-05-06 13:29:48 -0700 | [diff] [blame] | 215 | { |
| 216 | return; |
| 217 | } |
| 218 | const std::string* name = nameIt->get_ptr<const std::string*>(); |
| 219 | if (name == nullptr) |
| 220 | { |
| 221 | return; |
| 222 | } |
| 223 | const std::string* id = idIt->get_ptr<const std::string*>(); |
| 224 | if (id == nullptr) |
| 225 | { |
| 226 | return; |
| 227 | } |
| 228 | metadata->emplace_back(SensorData{*name, *id, dbusPath}); |
Adrian Ambrożewicz | a0ec28b | 2020-04-10 14:47:28 +0200 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | void updateUri(const std::string& name, const std::string& uri) |
| 232 | { |
| 233 | if (metadata) |
| 234 | { |
| 235 | for (auto& sensor : *metadata) |
| 236 | { |
| 237 | if (sensor.name == name) |
| 238 | { |
| 239 | sensor.uri = uri; |
| 240 | } |
| 241 | } |
| 242 | } |
| 243 | } |
| 244 | |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 245 | const std::shared_ptr<bmcweb::AsyncResp> asyncResp; |
Adrian Ambrożewicz | a0ec28b | 2020-04-10 14:47:28 +0200 | [diff] [blame] | 246 | const std::string chassisId; |
Ed Tanous | cf9e417 | 2022-12-21 09:30:16 -0800 | [diff] [blame] | 247 | const std::span<const std::string_view> types; |
Adrian Ambrożewicz | a0ec28b | 2020-04-10 14:47:28 +0200 | [diff] [blame] | 248 | const std::string chassisSubNode; |
Nan Zhou | 928fefb | 2022-03-28 08:45:00 -0700 | [diff] [blame] | 249 | const bool efficientExpand; |
Adrian Ambrożewicz | a0ec28b | 2020-04-10 14:47:28 +0200 | [diff] [blame] | 250 | |
| 251 | private: |
| 252 | std::optional<std::vector<SensorData>> metadata; |
| 253 | DataCompleteCb dataComplete; |
Lewanczyk, Dawid | 08777fb | 2018-03-22 23:33:49 +0100 | [diff] [blame] | 254 | }; |
| 255 | |
Janet Adkins | c956360 | 2024-08-28 11:37:46 -0500 | [diff] [blame] | 256 | using InventoryItem = sensor_utils::InventoryItem; |
Shawn McCarney | adc4f0d | 2019-07-24 09:21:50 -0500 | [diff] [blame] | 257 | |
| 258 | /** |
Richard Marian Thomaiyar | 413961d | 2019-02-01 00:43:39 +0530 | [diff] [blame] | 259 | * @brief Get objects with connection necessary for sensors |
Kowalski, Kamil | 588c3f0 | 2018-04-03 14:55:27 +0200 | [diff] [blame] | 260 | * @param SensorsAsyncResp Pointer to object holding response data |
Lewanczyk, Dawid | 08777fb | 2018-03-22 23:33:49 +0100 | [diff] [blame] | 261 | * @param sensorNames Sensors retrieved from chassis |
| 262 | * @param callback Callback for processing gathered connections |
| 263 | */ |
| 264 | template <typename Callback> |
Richard Marian Thomaiyar | 413961d | 2019-02-01 00:43:39 +0530 | [diff] [blame] | 265 | void getObjectsWithConnection( |
Ed Tanous | 81ce609 | 2020-12-17 16:54:55 +0000 | [diff] [blame] | 266 | const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp, |
Nan Zhou | fe04d49 | 2022-06-22 17:10:41 +0000 | [diff] [blame] | 267 | const std::shared_ptr<std::set<std::string>>& sensorNames, |
Richard Marian Thomaiyar | 413961d | 2019-02-01 00:43:39 +0530 | [diff] [blame] | 268 | Callback&& callback) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 269 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 270 | BMCWEB_LOG_DEBUG("getObjectsWithConnection enter"); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 271 | const std::string path = "/xyz/openbmc_project/sensors"; |
George Liu | e99073f | 2022-12-09 11:06:16 +0800 | [diff] [blame] | 272 | constexpr std::array<std::string_view, 1> interfaces = { |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 273 | "xyz.openbmc_project.Sensor.Value"}; |
Lewanczyk, Dawid | 08777fb | 2018-03-22 23:33:49 +0100 | [diff] [blame] | 274 | |
George Liu | e99073f | 2022-12-09 11:06:16 +0800 | [diff] [blame] | 275 | // Make call to ObjectMapper to find all sensors objects |
| 276 | dbus::utility::getSubTree( |
| 277 | path, 2, interfaces, |
Ed Tanous | 8cb2c02 | 2024-03-27 16:31:46 -0700 | [diff] [blame] | 278 | [callback = std::forward<Callback>(callback), sensorsAsyncResp, |
George Liu | e99073f | 2022-12-09 11:06:16 +0800 | [diff] [blame] | 279 | sensorNames](const boost::system::error_code& ec, |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 280 | const dbus::utility::MapperGetSubTreeResponse& subtree) { |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 281 | // Response handler for parsing objects subtree |
| 282 | BMCWEB_LOG_DEBUG("getObjectsWithConnection resp_handler enter"); |
| 283 | if (ec) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 284 | { |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 285 | messages::internalError(sensorsAsyncResp->asyncResp->res); |
| 286 | BMCWEB_LOG_ERROR( |
| 287 | "getObjectsWithConnection resp_handler: Dbus error {}", ec); |
| 288 | return; |
| 289 | } |
| 290 | |
| 291 | BMCWEB_LOG_DEBUG("Found {} subtrees", subtree.size()); |
| 292 | |
| 293 | // Make unique list of connections only for requested sensor types |
| 294 | // and found in the chassis |
| 295 | std::set<std::string> connections; |
| 296 | std::set<std::pair<std::string, std::string>> objectsWithConnection; |
| 297 | |
| 298 | BMCWEB_LOG_DEBUG("sensorNames list count: {}", sensorNames->size()); |
| 299 | for (const std::string& tsensor : *sensorNames) |
| 300 | { |
| 301 | BMCWEB_LOG_DEBUG("Sensor to find: {}", tsensor); |
| 302 | } |
| 303 | |
| 304 | for (const std::pair<std::string, |
| 305 | std::vector<std::pair< |
| 306 | std::string, std::vector<std::string>>>>& |
| 307 | object : subtree) |
| 308 | { |
Ed Tanous | 3d15864 | 2025-05-12 14:20:49 -0700 | [diff] [blame] | 309 | if (sensorNames->contains(object.first)) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 310 | { |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 311 | for (const std::pair<std::string, std::vector<std::string>>& |
| 312 | objData : object.second) |
| 313 | { |
| 314 | BMCWEB_LOG_DEBUG("Adding connection: {}", |
| 315 | objData.first); |
| 316 | connections.insert(objData.first); |
| 317 | objectsWithConnection.insert( |
| 318 | std::make_pair(object.first, objData.first)); |
| 319 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 320 | } |
| 321 | } |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 322 | BMCWEB_LOG_DEBUG("Found {} connections", connections.size()); |
| 323 | callback(std::move(connections), std::move(objectsWithConnection)); |
| 324 | BMCWEB_LOG_DEBUG("getObjectsWithConnection resp_handler exit"); |
| 325 | }); |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 326 | BMCWEB_LOG_DEBUG("getObjectsWithConnection exit"); |
Richard Marian Thomaiyar | 413961d | 2019-02-01 00:43:39 +0530 | [diff] [blame] | 327 | } |
| 328 | |
| 329 | /** |
| 330 | * @brief Create connections necessary for sensors |
| 331 | * @param SensorsAsyncResp Pointer to object holding response data |
| 332 | * @param sensorNames Sensors retrieved from chassis |
| 333 | * @param callback Callback for processing gathered connections |
| 334 | */ |
| 335 | template <typename Callback> |
Ed Tanous | daadfb2 | 2024-12-20 09:25:54 -0800 | [diff] [blame] | 336 | void getConnections(const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp, |
| 337 | const std::shared_ptr<std::set<std::string>>& sensorNames, |
Nan Zhou | fe04d49 | 2022-06-22 17:10:41 +0000 | [diff] [blame] | 338 | Callback&& callback) |
Richard Marian Thomaiyar | 413961d | 2019-02-01 00:43:39 +0530 | [diff] [blame] | 339 | { |
| 340 | auto objectsWithConnectionCb = |
Ed Tanous | 8cb2c02 | 2024-03-27 16:31:46 -0700 | [diff] [blame] | 341 | [callback = std::forward<Callback>(callback)]( |
| 342 | const std::set<std::string>& connections, |
| 343 | const std::set<std::pair<std::string, std::string>>& |
| 344 | /*objectsWithConnection*/) { callback(connections); }; |
Ed Tanous | 81ce609 | 2020-12-17 16:54:55 +0000 | [diff] [blame] | 345 | getObjectsWithConnection(sensorsAsyncResp, sensorNames, |
Richard Marian Thomaiyar | 413961d | 2019-02-01 00:43:39 +0530 | [diff] [blame] | 346 | std::move(objectsWithConnectionCb)); |
Lewanczyk, Dawid | 08777fb | 2018-03-22 23:33:49 +0100 | [diff] [blame] | 347 | } |
| 348 | |
| 349 | /** |
Johnathan Mantey | 49c53ac | 2019-05-02 09:22:38 -0700 | [diff] [blame] | 350 | * @brief Shrinks the list of sensors for processing |
| 351 | * @param SensorsAysncResp The class holding the Redfish response |
| 352 | * @param allSensors A list of all the sensors associated to the |
| 353 | * chassis element (i.e. baseboard, front panel, etc...) |
| 354 | * @param activeSensors A list that is a reduction of the incoming |
| 355 | * allSensors list. Eliminate Thermal sensors when a Power request is |
| 356 | * made, and eliminate Power sensors when a Thermal request is made. |
| 357 | */ |
Ed Tanous | 23a21a1 | 2020-07-25 04:45:05 +0000 | [diff] [blame] | 358 | inline void reduceSensorList( |
Ed Tanous | 7f1cc26 | 2022-08-09 13:33:57 -0700 | [diff] [blame] | 359 | crow::Response& res, std::string_view chassisSubNode, |
Ed Tanous | cf9e417 | 2022-12-21 09:30:16 -0800 | [diff] [blame] | 360 | std::span<const std::string_view> sensorTypes, |
Johnathan Mantey | 49c53ac | 2019-05-02 09:22:38 -0700 | [diff] [blame] | 361 | const std::vector<std::string>* allSensors, |
Nan Zhou | fe04d49 | 2022-06-22 17:10:41 +0000 | [diff] [blame] | 362 | const std::shared_ptr<std::set<std::string>>& activeSensors) |
Johnathan Mantey | 49c53ac | 2019-05-02 09:22:38 -0700 | [diff] [blame] | 363 | { |
Johnathan Mantey | 49c53ac | 2019-05-02 09:22:38 -0700 | [diff] [blame] | 364 | if ((allSensors == nullptr) || (activeSensors == nullptr)) |
| 365 | { |
Ed Tanous | 7f1cc26 | 2022-08-09 13:33:57 -0700 | [diff] [blame] | 366 | messages::resourceNotFound(res, chassisSubNode, |
Janet Adkins | 0c728b4 | 2024-08-29 11:09:10 -0500 | [diff] [blame] | 367 | chassisSubNode == sensors::thermalNodeStr |
Ed Tanous | 7f1cc26 | 2022-08-09 13:33:57 -0700 | [diff] [blame] | 368 | ? "Temperatures" |
| 369 | : "Voltages"); |
Johnathan Mantey | 49c53ac | 2019-05-02 09:22:38 -0700 | [diff] [blame] | 370 | |
| 371 | return; |
| 372 | } |
| 373 | if (allSensors->empty()) |
| 374 | { |
| 375 | // Nothing to do, the activeSensors object is also empty |
| 376 | return; |
| 377 | } |
| 378 | |
Ed Tanous | 7f1cc26 | 2022-08-09 13:33:57 -0700 | [diff] [blame] | 379 | for (std::string_view type : sensorTypes) |
Johnathan Mantey | 49c53ac | 2019-05-02 09:22:38 -0700 | [diff] [blame] | 380 | { |
| 381 | for (const std::string& sensor : *allSensors) |
| 382 | { |
Ed Tanous | 11ba397 | 2022-07-11 09:50:41 -0700 | [diff] [blame] | 383 | if (sensor.starts_with(type)) |
Johnathan Mantey | 49c53ac | 2019-05-02 09:22:38 -0700 | [diff] [blame] | 384 | { |
| 385 | activeSensors->emplace(sensor); |
| 386 | } |
| 387 | } |
| 388 | } |
| 389 | } |
| 390 | |
Ed Tanous | 7f1cc26 | 2022-08-09 13:33:57 -0700 | [diff] [blame] | 391 | /* |
| 392 | *Populates the top level collection for a given subnode. Populates |
| 393 | *SensorCollection, Power, or Thermal schemas. |
| 394 | * |
| 395 | * */ |
| 396 | inline void populateChassisNode(nlohmann::json& jsonValue, |
| 397 | std::string_view chassisSubNode) |
| 398 | { |
Janet Adkins | 0c728b4 | 2024-08-29 11:09:10 -0500 | [diff] [blame] | 399 | if (chassisSubNode == sensors::powerNodeStr) |
Ed Tanous | 7f1cc26 | 2022-08-09 13:33:57 -0700 | [diff] [blame] | 400 | { |
| 401 | jsonValue["@odata.type"] = "#Power.v1_5_2.Power"; |
| 402 | } |
Janet Adkins | 0c728b4 | 2024-08-29 11:09:10 -0500 | [diff] [blame] | 403 | else if (chassisSubNode == sensors::thermalNodeStr) |
Ed Tanous | 7f1cc26 | 2022-08-09 13:33:57 -0700 | [diff] [blame] | 404 | { |
| 405 | jsonValue["@odata.type"] = "#Thermal.v1_4_0.Thermal"; |
| 406 | jsonValue["Fans"] = nlohmann::json::array(); |
| 407 | jsonValue["Temperatures"] = nlohmann::json::array(); |
| 408 | } |
Janet Adkins | 0c728b4 | 2024-08-29 11:09:10 -0500 | [diff] [blame] | 409 | else if (chassisSubNode == sensors::sensorsNodeStr) |
Ed Tanous | 7f1cc26 | 2022-08-09 13:33:57 -0700 | [diff] [blame] | 410 | { |
| 411 | jsonValue["@odata.type"] = "#SensorCollection.SensorCollection"; |
| 412 | jsonValue["Description"] = "Collection of Sensors for this Chassis"; |
| 413 | jsonValue["Members"] = nlohmann::json::array(); |
| 414 | jsonValue["Members@odata.count"] = 0; |
| 415 | } |
| 416 | |
Janet Adkins | 0c728b4 | 2024-08-29 11:09:10 -0500 | [diff] [blame] | 417 | if (chassisSubNode != sensors::sensorsNodeStr) |
Ed Tanous | 7f1cc26 | 2022-08-09 13:33:57 -0700 | [diff] [blame] | 418 | { |
| 419 | jsonValue["Id"] = chassisSubNode; |
| 420 | } |
| 421 | jsonValue["Name"] = chassisSubNode; |
| 422 | } |
| 423 | |
Johnathan Mantey | 49c53ac | 2019-05-02 09:22:38 -0700 | [diff] [blame] | 424 | /** |
Lewanczyk, Dawid | 08777fb | 2018-03-22 23:33:49 +0100 | [diff] [blame] | 425 | * @brief Retrieves requested chassis sensors and redundancy data from DBus . |
Kowalski, Kamil | 588c3f0 | 2018-04-03 14:55:27 +0200 | [diff] [blame] | 426 | * @param SensorsAsyncResp Pointer to object holding response data |
Lewanczyk, Dawid | 08777fb | 2018-03-22 23:33:49 +0100 | [diff] [blame] | 427 | * @param callback Callback for next step in gathered sensor processing |
| 428 | */ |
| 429 | template <typename Callback> |
Ed Tanous | 7f1cc26 | 2022-08-09 13:33:57 -0700 | [diff] [blame] | 430 | void getChassis(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, |
| 431 | std::string_view chassisId, std::string_view chassisSubNode, |
Ed Tanous | cf9e417 | 2022-12-21 09:30:16 -0800 | [diff] [blame] | 432 | std::span<const std::string_view> sensorTypes, |
| 433 | Callback&& callback) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 434 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 435 | BMCWEB_LOG_DEBUG("getChassis enter"); |
George Liu | 7a1dbc4 | 2022-12-07 16:03:22 +0800 | [diff] [blame] | 436 | |
| 437 | // Get the Chassis Collection |
| 438 | dbus::utility::getSubTreePaths( |
Myung Bae | 3f95a27 | 2024-03-13 07:32:02 -0700 | [diff] [blame] | 439 | "/xyz/openbmc_project/inventory", 0, chassisInterfaces, |
Ed Tanous | 8cb2c02 | 2024-03-27 16:31:46 -0700 | [diff] [blame] | 440 | [callback = std::forward<Callback>(callback), asyncResp, |
Ed Tanous | 7f1cc26 | 2022-08-09 13:33:57 -0700 | [diff] [blame] | 441 | chassisIdStr{std::string(chassisId)}, |
Ed Tanous | 4e0d878 | 2024-09-06 15:16:41 -0700 | [diff] [blame] | 442 | chassisSubNode{std::string(chassisSubNode)}, |
| 443 | sensorTypes](const boost::system::error_code& ec, |
| 444 | const dbus::utility::MapperGetSubTreePathsResponse& |
| 445 | chassisPaths) mutable { |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 446 | BMCWEB_LOG_DEBUG("getChassis respHandler enter"); |
| 447 | if (ec) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 448 | { |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 449 | BMCWEB_LOG_ERROR("getChassis respHandler DBUS error: {}", ec); |
| 450 | messages::internalError(asyncResp->res); |
| 451 | return; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 452 | } |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 453 | const std::string* chassisPath = nullptr; |
| 454 | for (const std::string& chassis : chassisPaths) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 455 | { |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 456 | sdbusplus::message::object_path path(chassis); |
| 457 | std::string chassisName = path.filename(); |
| 458 | if (chassisName.empty()) |
Johnathan Mantey | 49c53ac | 2019-05-02 09:22:38 -0700 | [diff] [blame] | 459 | { |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 460 | BMCWEB_LOG_ERROR("Failed to find '/' in {}", chassis); |
| 461 | continue; |
| 462 | } |
| 463 | if (chassisName == chassisIdStr) |
| 464 | { |
| 465 | chassisPath = &chassis; |
| 466 | break; |
Johnathan Mantey | 49c53ac | 2019-05-02 09:22:38 -0700 | [diff] [blame] | 467 | } |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 468 | } |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 469 | if (chassisPath == nullptr) |
| 470 | { |
| 471 | messages::resourceNotFound(asyncResp->res, "Chassis", |
| 472 | chassisIdStr); |
| 473 | return; |
| 474 | } |
| 475 | populateChassisNode(asyncResp->res.jsonValue, chassisSubNode); |
| 476 | |
| 477 | asyncResp->res.jsonValue["@odata.id"] = boost::urls::format( |
| 478 | "/redfish/v1/Chassis/{}/{}", chassisIdStr, chassisSubNode); |
| 479 | |
| 480 | // Get the list of all sensors for this Chassis element |
| 481 | std::string sensorPath = *chassisPath + "/all_sensors"; |
| 482 | dbus::utility::getAssociationEndPoints( |
Ed Tanous | 4e0d878 | 2024-09-06 15:16:41 -0700 | [diff] [blame] | 483 | sensorPath, [asyncResp, chassisSubNode, sensorTypes, |
| 484 | callback = std::forward<Callback>(callback)]( |
| 485 | const boost::system::error_code& ec2, |
| 486 | const dbus::utility::MapperEndPoints& |
| 487 | nodeSensorList) mutable { |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 488 | if (ec2) |
| 489 | { |
| 490 | if (ec2.value() != EBADR) |
| 491 | { |
| 492 | messages::internalError(asyncResp->res); |
| 493 | return; |
| 494 | } |
| 495 | } |
| 496 | const std::shared_ptr<std::set<std::string>> |
| 497 | culledSensorList = |
| 498 | std::make_shared<std::set<std::string>>(); |
| 499 | reduceSensorList(asyncResp->res, chassisSubNode, |
| 500 | sensorTypes, &nodeSensorList, |
| 501 | culledSensorList); |
| 502 | BMCWEB_LOG_DEBUG("Finishing with {}", |
| 503 | culledSensorList->size()); |
| 504 | callback(culledSensorList); |
| 505 | }); |
George Liu | 7a1dbc4 | 2022-12-07 16:03:22 +0800 | [diff] [blame] | 506 | }); |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 507 | BMCWEB_LOG_DEBUG("getChassis exit"); |
Lewanczyk, Dawid | 08777fb | 2018-03-22 23:33:49 +0100 | [diff] [blame] | 508 | } |
| 509 | |
| 510 | /** |
Ed Tanous | 1d7c005 | 2022-08-09 12:32:26 -0700 | [diff] [blame] | 511 | * @brief Builds a json sensor representation of a sensor. |
| 512 | * @param sensorName The name of the sensor to be built |
| 513 | * @param sensorType The type (temperature, fan_tach, etc) of the sensor to |
| 514 | * build |
Ed Tanous | 8ece0e4 | 2024-01-02 13:16:50 -0800 | [diff] [blame] | 515 | * @param chassisSubNode The subnode (thermal, sensor, etc) of the sensor |
Ed Tanous | 1d7c005 | 2022-08-09 12:32:26 -0700 | [diff] [blame] | 516 | * @param interfacesDict A dictionary of the interfaces and properties of said |
| 517 | * interfaces to be built from |
| 518 | * @param sensorJson The json object to fill |
| 519 | * @param inventoryItem D-Bus inventory item associated with the sensor. Will |
| 520 | * be nullptr if no associated inventory item was found. |
| 521 | */ |
| 522 | inline void objectInterfacesToJson( |
| 523 | const std::string& sensorName, const std::string& sensorType, |
Janet Adkins | 0c728b4 | 2024-08-29 11:09:10 -0500 | [diff] [blame] | 524 | const sensor_utils::ChassisSubNode chassisSubNode, |
Michael Shen | 80f79a4 | 2023-08-24 13:41:53 +0000 | [diff] [blame] | 525 | const dbus::utility::DBusInterfacesMap& interfacesDict, |
Ed Tanous | 1d7c005 | 2022-08-09 12:32:26 -0700 | [diff] [blame] | 526 | nlohmann::json& sensorJson, InventoryItem* inventoryItem) |
| 527 | { |
Ed Tanous | 1d7c005 | 2022-08-09 12:32:26 -0700 | [diff] [blame] | 528 | for (const auto& [interface, valuesDict] : interfacesDict) |
| 529 | { |
Janet Adkins | c956360 | 2024-08-28 11:37:46 -0500 | [diff] [blame] | 530 | sensor_utils::objectPropertiesToJson( |
| 531 | sensorName, sensorType, chassisSubNode, valuesDict, sensorJson, |
| 532 | inventoryItem); |
Ed Tanous | 1d7c005 | 2022-08-09 12:32:26 -0700 | [diff] [blame] | 533 | } |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 534 | BMCWEB_LOG_DEBUG("Added sensor {}", sensorName); |
Ed Tanous | 1d7c005 | 2022-08-09 12:32:26 -0700 | [diff] [blame] | 535 | } |
| 536 | |
Ed Tanous | b5a7693 | 2020-09-29 16:16:58 -0700 | [diff] [blame] | 537 | inline void populateFanRedundancy( |
| 538 | const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp) |
James Feist | 8bd25cc | 2019-03-15 15:14:00 -0700 | [diff] [blame] | 539 | { |
George Liu | e99073f | 2022-12-09 11:06:16 +0800 | [diff] [blame] | 540 | constexpr std::array<std::string_view, 1> interfaces = { |
| 541 | "xyz.openbmc_project.Control.FanRedundancy"}; |
| 542 | dbus::utility::getSubTree( |
| 543 | "/xyz/openbmc_project/control", 2, interfaces, |
Ed Tanous | b9d36b4 | 2022-02-26 21:42:46 -0800 | [diff] [blame] | 544 | [sensorsAsyncResp]( |
George Liu | e99073f | 2022-12-09 11:06:16 +0800 | [diff] [blame] | 545 | const boost::system::error_code& ec, |
Ed Tanous | b9d36b4 | 2022-02-26 21:42:46 -0800 | [diff] [blame] | 546 | const dbus::utility::MapperGetSubTreeResponse& resp) { |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 547 | if (ec) |
James Feist | 8bd25cc | 2019-03-15 15:14:00 -0700 | [diff] [blame] | 548 | { |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 549 | return; // don't have to have this interface |
James Feist | 8bd25cc | 2019-03-15 15:14:00 -0700 | [diff] [blame] | 550 | } |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 551 | for (const std::pair<std::string, dbus::utility::MapperServiceMap>& |
| 552 | pathPair : resp) |
| 553 | { |
| 554 | const std::string& path = pathPair.first; |
| 555 | const dbus::utility::MapperServiceMap& objDict = |
| 556 | pathPair.second; |
| 557 | if (objDict.empty()) |
James Feist | 8bd25cc | 2019-03-15 15:14:00 -0700 | [diff] [blame] | 558 | { |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 559 | continue; // this should be impossible |
James Feist | 8bd25cc | 2019-03-15 15:14:00 -0700 | [diff] [blame] | 560 | } |
| 561 | |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 562 | const std::string& owner = objDict.begin()->first; |
| 563 | dbus::utility::getAssociationEndPoints( |
| 564 | path + "/chassis", |
| 565 | [path, owner, sensorsAsyncResp]( |
| 566 | const boost::system::error_code& ec2, |
| 567 | const dbus::utility::MapperEndPoints& endpoints) { |
| 568 | if (ec2) |
James Feist | 8bd25cc | 2019-03-15 15:14:00 -0700 | [diff] [blame] | 569 | { |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 570 | return; // if they don't have an association we |
| 571 | // can't tell what chassis is |
James Feist | 8bd25cc | 2019-03-15 15:14:00 -0700 | [diff] [blame] | 572 | } |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 573 | auto found = std::ranges::find_if( |
| 574 | endpoints, |
| 575 | [sensorsAsyncResp](const std::string& entry) { |
| 576 | return entry.find( |
| 577 | sensorsAsyncResp->chassisId) != |
| 578 | std::string::npos; |
| 579 | }); |
| 580 | |
| 581 | if (found == endpoints.end()) |
James Feist | 8bd25cc | 2019-03-15 15:14:00 -0700 | [diff] [blame] | 582 | { |
| 583 | return; |
| 584 | } |
Ed Tanous | deae6a7 | 2024-11-11 21:58:57 -0800 | [diff] [blame] | 585 | dbus::utility::getAllProperties( |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 586 | *crow::connections::systemBus, owner, path, |
| 587 | "xyz.openbmc_project.Control.FanRedundancy", |
| 588 | [path, sensorsAsyncResp]( |
| 589 | const boost::system::error_code& ec3, |
| 590 | const dbus::utility::DBusPropertiesMap& ret) { |
| 591 | if (ec3) |
| 592 | { |
| 593 | return; // don't have to have this |
| 594 | // interface |
| 595 | } |
James Feist | 8bd25cc | 2019-03-15 15:14:00 -0700 | [diff] [blame] | 596 | |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 597 | const uint8_t* allowedFailures = nullptr; |
| 598 | const std::vector<std::string>* collection = |
| 599 | nullptr; |
| 600 | const std::string* status = nullptr; |
James Feist | 8bd25cc | 2019-03-15 15:14:00 -0700 | [diff] [blame] | 601 | |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 602 | const bool success = |
| 603 | sdbusplus::unpackPropertiesNoThrow( |
| 604 | dbus_utils::UnpackErrorPrinter(), ret, |
| 605 | "AllowedFailures", allowedFailures, |
| 606 | "Collection", collection, "Status", |
| 607 | status); |
James Feist | 8bd25cc | 2019-03-15 15:14:00 -0700 | [diff] [blame] | 608 | |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 609 | if (!success) |
| 610 | { |
| 611 | messages::internalError( |
| 612 | sensorsAsyncResp->asyncResp->res); |
| 613 | return; |
| 614 | } |
| 615 | |
| 616 | if (allowedFailures == nullptr || |
| 617 | collection == nullptr || status == nullptr) |
| 618 | { |
| 619 | BMCWEB_LOG_ERROR( |
| 620 | "Invalid redundancy interface"); |
| 621 | messages::internalError( |
| 622 | sensorsAsyncResp->asyncResp->res); |
| 623 | return; |
| 624 | } |
| 625 | |
| 626 | sdbusplus::message::object_path objectPath( |
| 627 | path); |
| 628 | std::string name = objectPath.filename(); |
| 629 | if (name.empty()) |
| 630 | { |
| 631 | // this should be impossible |
| 632 | messages::internalError( |
| 633 | sensorsAsyncResp->asyncResp->res); |
| 634 | return; |
| 635 | } |
| 636 | std::ranges::replace(name, '_', ' '); |
| 637 | |
| 638 | std::string health; |
| 639 | |
| 640 | if (status->ends_with("Full")) |
| 641 | { |
| 642 | health = "OK"; |
| 643 | } |
| 644 | else if (status->ends_with("Degraded")) |
| 645 | { |
| 646 | health = "Warning"; |
| 647 | } |
| 648 | else |
| 649 | { |
| 650 | health = "Critical"; |
| 651 | } |
| 652 | nlohmann::json::array_t redfishCollection; |
| 653 | const auto& fanRedfish = |
| 654 | sensorsAsyncResp->asyncResp->res |
| 655 | .jsonValue["Fans"]; |
| 656 | for (const std::string& item : *collection) |
| 657 | { |
| 658 | sdbusplus::message::object_path itemPath( |
| 659 | item); |
| 660 | std::string itemName = itemPath.filename(); |
| 661 | if (itemName.empty()) |
| 662 | { |
| 663 | continue; |
| 664 | } |
| 665 | /* |
| 666 | todo(ed): merge patch that fixes the names |
| 667 | std::replace(itemName.begin(), |
| 668 | itemName.end(), '_', ' ');*/ |
| 669 | auto schemaItem = std::ranges::find_if( |
| 670 | fanRedfish, |
| 671 | [itemName](const nlohmann::json& fan) { |
| 672 | return fan["Name"] == itemName; |
| 673 | }); |
| 674 | if (schemaItem != fanRedfish.end()) |
| 675 | { |
| 676 | nlohmann::json::object_t collectionId; |
| 677 | collectionId["@odata.id"] = |
| 678 | (*schemaItem)["@odata.id"]; |
| 679 | redfishCollection.emplace_back( |
| 680 | std::move(collectionId)); |
| 681 | } |
| 682 | else |
| 683 | { |
| 684 | BMCWEB_LOG_ERROR( |
| 685 | "failed to find fan in schema"); |
| 686 | messages::internalError( |
| 687 | sensorsAsyncResp->asyncResp->res); |
| 688 | return; |
| 689 | } |
| 690 | } |
| 691 | |
| 692 | size_t minNumNeeded = |
| 693 | collection->empty() |
| 694 | ? 0 |
| 695 | : collection->size() - *allowedFailures; |
| 696 | nlohmann::json& jResp = |
| 697 | sensorsAsyncResp->asyncResp->res |
| 698 | .jsonValue["Redundancy"]; |
| 699 | |
| 700 | nlohmann::json::object_t redundancy; |
| 701 | boost::urls::url url = boost::urls::format( |
| 702 | "/redfish/v1/Chassis/{}/{}", |
| 703 | sensorsAsyncResp->chassisId, |
| 704 | sensorsAsyncResp->chassisSubNode); |
| 705 | url.set_fragment( |
| 706 | ("/Redundancy"_json_pointer / jResp.size()) |
| 707 | .to_string()); |
| 708 | redundancy["@odata.id"] = std::move(url); |
| 709 | redundancy["@odata.type"] = |
| 710 | "#Redundancy.v1_3_2.Redundancy"; |
| 711 | redundancy["MinNumNeeded"] = minNumNeeded; |
| 712 | redundancy["Mode"] = |
| 713 | redundancy::RedundancyType::NPlusM; |
| 714 | redundancy["Name"] = name; |
| 715 | redundancy["RedundancySet"] = redfishCollection; |
| 716 | redundancy["Status"]["Health"] = health; |
| 717 | redundancy["Status"]["State"] = |
| 718 | resource::State::Enabled; |
| 719 | |
| 720 | jResp.emplace_back(std::move(redundancy)); |
| 721 | }); |
| 722 | }); |
| 723 | } |
| 724 | }); |
James Feist | 8bd25cc | 2019-03-15 15:14:00 -0700 | [diff] [blame] | 725 | } |
| 726 | |
Patrick Williams | 504af5a | 2025-02-03 14:29:03 -0500 | [diff] [blame] | 727 | inline void sortJSONResponse( |
| 728 | const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp) |
Johnathan Mantey | 49c53ac | 2019-05-02 09:22:38 -0700 | [diff] [blame] | 729 | { |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 730 | nlohmann::json& response = sensorsAsyncResp->asyncResp->res.jsonValue; |
Johnathan Mantey | 49c53ac | 2019-05-02 09:22:38 -0700 | [diff] [blame] | 731 | std::array<std::string, 2> sensorHeaders{"Temperatures", "Fans"}; |
Janet Adkins | 0c728b4 | 2024-08-29 11:09:10 -0500 | [diff] [blame] | 732 | if (sensorsAsyncResp->chassisSubNode == sensors::powerNodeStr) |
Johnathan Mantey | 49c53ac | 2019-05-02 09:22:38 -0700 | [diff] [blame] | 733 | { |
| 734 | sensorHeaders = {"Voltages", "PowerSupplies"}; |
| 735 | } |
| 736 | for (const std::string& sensorGroup : sensorHeaders) |
| 737 | { |
| 738 | nlohmann::json::iterator entry = response.find(sensorGroup); |
Ed Tanous | 4e196b9 | 2024-09-27 17:45:09 -0700 | [diff] [blame] | 739 | if (entry == response.end()) |
Johnathan Mantey | 49c53ac | 2019-05-02 09:22:38 -0700 | [diff] [blame] | 740 | { |
Ed Tanous | 4e196b9 | 2024-09-27 17:45:09 -0700 | [diff] [blame] | 741 | continue; |
| 742 | } |
| 743 | nlohmann::json::array_t* arr = |
| 744 | entry->get_ptr<nlohmann::json::array_t*>(); |
| 745 | if (arr == nullptr) |
| 746 | { |
| 747 | continue; |
| 748 | } |
| 749 | json_util::sortJsonArrayByKey(*arr, "Name"); |
Johnathan Mantey | 49c53ac | 2019-05-02 09:22:38 -0700 | [diff] [blame] | 750 | |
Ed Tanous | 4e196b9 | 2024-09-27 17:45:09 -0700 | [diff] [blame] | 751 | // add the index counts to the end of each entry |
| 752 | size_t count = 0; |
| 753 | for (nlohmann::json& sensorJson : *entry) |
| 754 | { |
| 755 | nlohmann::json::iterator odata = sensorJson.find("@odata.id"); |
| 756 | if (odata == sensorJson.end()) |
Johnathan Mantey | 49c53ac | 2019-05-02 09:22:38 -0700 | [diff] [blame] | 757 | { |
Ed Tanous | 4e196b9 | 2024-09-27 17:45:09 -0700 | [diff] [blame] | 758 | continue; |
| 759 | } |
Ed Tanous | 82b286f | 2025-05-06 13:29:48 -0700 | [diff] [blame] | 760 | const auto nameIt = sensorJson.find("Name"); |
| 761 | if (nameIt == sensorJson.end()) |
Ed Tanous | 4e196b9 | 2024-09-27 17:45:09 -0700 | [diff] [blame] | 762 | { |
Ed Tanous | 82b286f | 2025-05-06 13:29:48 -0700 | [diff] [blame] | 763 | continue; |
Johnathan Mantey | 49c53ac | 2019-05-02 09:22:38 -0700 | [diff] [blame] | 764 | } |
Ed Tanous | 82b286f | 2025-05-06 13:29:48 -0700 | [diff] [blame] | 765 | std::string* value = odata->get_ptr<std::string*>(); |
| 766 | if (value == nullptr) |
| 767 | { |
| 768 | continue; |
| 769 | } |
| 770 | const std::string* name = nameIt->get_ptr<const std::string*>(); |
| 771 | if (name == nullptr) |
| 772 | { |
| 773 | continue; |
| 774 | } |
| 775 | *value += "/" + std::to_string(count); |
| 776 | sensorJson["MemberId"] = std::to_string(count); |
| 777 | count++; |
| 778 | sensorsAsyncResp->updateUri(*name, *value); |
Johnathan Mantey | 49c53ac | 2019-05-02 09:22:38 -0700 | [diff] [blame] | 779 | } |
| 780 | } |
| 781 | } |
| 782 | |
Lewanczyk, Dawid | 08777fb | 2018-03-22 23:33:49 +0100 | [diff] [blame] | 783 | /** |
Shawn McCarney | adc4f0d | 2019-07-24 09:21:50 -0500 | [diff] [blame] | 784 | * @brief Finds the inventory item with the specified object path. |
| 785 | * @param inventoryItems D-Bus inventory items associated with sensors. |
| 786 | * @param invItemObjPath D-Bus object path of inventory item. |
| 787 | * @return Inventory item within vector, or nullptr if no match found. |
Shawn McCarney | 8fb49dd | 2019-06-12 17:47:00 -0500 | [diff] [blame] | 788 | */ |
Ed Tanous | 23a21a1 | 2020-07-25 04:45:05 +0000 | [diff] [blame] | 789 | inline InventoryItem* findInventoryItem( |
Ed Tanous | b5a7693 | 2020-09-29 16:16:58 -0700 | [diff] [blame] | 790 | const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems, |
Shawn McCarney | adc4f0d | 2019-07-24 09:21:50 -0500 | [diff] [blame] | 791 | const std::string& invItemObjPath) |
Shawn McCarney | 8fb49dd | 2019-06-12 17:47:00 -0500 | [diff] [blame] | 792 | { |
Shawn McCarney | adc4f0d | 2019-07-24 09:21:50 -0500 | [diff] [blame] | 793 | for (InventoryItem& inventoryItem : *inventoryItems) |
Shawn McCarney | 8fb49dd | 2019-06-12 17:47:00 -0500 | [diff] [blame] | 794 | { |
Shawn McCarney | adc4f0d | 2019-07-24 09:21:50 -0500 | [diff] [blame] | 795 | if (inventoryItem.objectPath == invItemObjPath) |
Shawn McCarney | 8fb49dd | 2019-06-12 17:47:00 -0500 | [diff] [blame] | 796 | { |
Shawn McCarney | adc4f0d | 2019-07-24 09:21:50 -0500 | [diff] [blame] | 797 | return &inventoryItem; |
Shawn McCarney | 8fb49dd | 2019-06-12 17:47:00 -0500 | [diff] [blame] | 798 | } |
| 799 | } |
Shawn McCarney | 8fb49dd | 2019-06-12 17:47:00 -0500 | [diff] [blame] | 800 | return nullptr; |
| 801 | } |
| 802 | |
| 803 | /** |
Shawn McCarney | adc4f0d | 2019-07-24 09:21:50 -0500 | [diff] [blame] | 804 | * @brief Finds the inventory item associated with the specified sensor. |
| 805 | * @param inventoryItems D-Bus inventory items associated with sensors. |
| 806 | * @param sensorObjPath D-Bus object path of sensor. |
| 807 | * @return Inventory item within vector, or nullptr if no match found. |
Shawn McCarney | 8fb49dd | 2019-06-12 17:47:00 -0500 | [diff] [blame] | 808 | */ |
Ed Tanous | 23a21a1 | 2020-07-25 04:45:05 +0000 | [diff] [blame] | 809 | inline InventoryItem* findInventoryItemForSensor( |
Ed Tanous | b5a7693 | 2020-09-29 16:16:58 -0700 | [diff] [blame] | 810 | const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems, |
Shawn McCarney | adc4f0d | 2019-07-24 09:21:50 -0500 | [diff] [blame] | 811 | const std::string& sensorObjPath) |
| 812 | { |
| 813 | for (InventoryItem& inventoryItem : *inventoryItems) |
| 814 | { |
Ed Tanous | db0d36e | 2023-06-30 10:37:05 -0700 | [diff] [blame] | 815 | if (inventoryItem.sensors.contains(sensorObjPath)) |
Shawn McCarney | adc4f0d | 2019-07-24 09:21:50 -0500 | [diff] [blame] | 816 | { |
| 817 | return &inventoryItem; |
| 818 | } |
| 819 | } |
| 820 | return nullptr; |
| 821 | } |
| 822 | |
| 823 | /** |
Anthony Wilson | d500549 | 2019-07-31 16:34:17 -0500 | [diff] [blame] | 824 | * @brief Finds the inventory item associated with the specified led path. |
| 825 | * @param inventoryItems D-Bus inventory items associated with sensors. |
| 826 | * @param ledObjPath D-Bus object path of led. |
| 827 | * @return Inventory item within vector, or nullptr if no match found. |
| 828 | */ |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 829 | inline InventoryItem* findInventoryItemForLed( |
| 830 | std::vector<InventoryItem>& inventoryItems, const std::string& ledObjPath) |
Anthony Wilson | d500549 | 2019-07-31 16:34:17 -0500 | [diff] [blame] | 831 | { |
| 832 | for (InventoryItem& inventoryItem : inventoryItems) |
| 833 | { |
| 834 | if (inventoryItem.ledObjectPath == ledObjPath) |
| 835 | { |
| 836 | return &inventoryItem; |
| 837 | } |
| 838 | } |
| 839 | return nullptr; |
| 840 | } |
| 841 | |
| 842 | /** |
Shawn McCarney | adc4f0d | 2019-07-24 09:21:50 -0500 | [diff] [blame] | 843 | * @brief Adds inventory item and associated sensor to specified vector. |
| 844 | * |
| 845 | * Adds a new InventoryItem to the vector if necessary. Searches for an |
| 846 | * existing InventoryItem with the specified object path. If not found, one is |
| 847 | * added to the vector. |
| 848 | * |
| 849 | * Next, the specified sensor is added to the set of sensors associated with the |
| 850 | * InventoryItem. |
| 851 | * |
| 852 | * @param inventoryItems D-Bus inventory items associated with sensors. |
| 853 | * @param invItemObjPath D-Bus object path of inventory item. |
| 854 | * @param sensorObjPath D-Bus object path of sensor |
| 855 | */ |
Ed Tanous | b5a7693 | 2020-09-29 16:16:58 -0700 | [diff] [blame] | 856 | inline void addInventoryItem( |
| 857 | const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems, |
| 858 | const std::string& invItemObjPath, const std::string& sensorObjPath) |
Shawn McCarney | adc4f0d | 2019-07-24 09:21:50 -0500 | [diff] [blame] | 859 | { |
| 860 | // Look for inventory item in vector |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 861 | InventoryItem* inventoryItem = |
| 862 | findInventoryItem(inventoryItems, invItemObjPath); |
Shawn McCarney | adc4f0d | 2019-07-24 09:21:50 -0500 | [diff] [blame] | 863 | |
| 864 | // If inventory item doesn't exist in vector, add it |
| 865 | if (inventoryItem == nullptr) |
| 866 | { |
| 867 | inventoryItems->emplace_back(invItemObjPath); |
| 868 | inventoryItem = &(inventoryItems->back()); |
| 869 | } |
| 870 | |
| 871 | // Add sensor to set of sensors associated with inventory item |
| 872 | inventoryItem->sensors.emplace(sensorObjPath); |
| 873 | } |
| 874 | |
| 875 | /** |
| 876 | * @brief Stores D-Bus data in the specified inventory item. |
| 877 | * |
| 878 | * Finds D-Bus data in the specified map of interfaces. Stores the data in the |
| 879 | * specified InventoryItem. |
| 880 | * |
| 881 | * This data is later used to provide sensor property values in the JSON |
| 882 | * response. |
| 883 | * |
| 884 | * @param inventoryItem Inventory item where data will be stored. |
| 885 | * @param interfacesDict Map containing D-Bus interfaces and their properties |
| 886 | * for the specified inventory item. |
| 887 | */ |
Ed Tanous | 23a21a1 | 2020-07-25 04:45:05 +0000 | [diff] [blame] | 888 | inline void storeInventoryItemData( |
Shawn McCarney | adc4f0d | 2019-07-24 09:21:50 -0500 | [diff] [blame] | 889 | InventoryItem& inventoryItem, |
Michael Shen | 80f79a4 | 2023-08-24 13:41:53 +0000 | [diff] [blame] | 890 | const dbus::utility::DBusInterfacesMap& interfacesDict) |
Shawn McCarney | 8fb49dd | 2019-06-12 17:47:00 -0500 | [diff] [blame] | 891 | { |
Shawn McCarney | adc4f0d | 2019-07-24 09:21:50 -0500 | [diff] [blame] | 892 | // Get properties from Inventory.Item interface |
Ed Tanous | 711ac7a | 2021-12-20 09:34:41 -0800 | [diff] [blame] | 893 | |
Ed Tanous | 9eb808c | 2022-01-25 10:19:23 -0800 | [diff] [blame] | 894 | for (const auto& [interface, values] : interfacesDict) |
Shawn McCarney | 8fb49dd | 2019-06-12 17:47:00 -0500 | [diff] [blame] | 895 | { |
Ed Tanous | 711ac7a | 2021-12-20 09:34:41 -0800 | [diff] [blame] | 896 | if (interface == "xyz.openbmc_project.Inventory.Item") |
Shawn McCarney | 8fb49dd | 2019-06-12 17:47:00 -0500 | [diff] [blame] | 897 | { |
Ed Tanous | 9eb808c | 2022-01-25 10:19:23 -0800 | [diff] [blame] | 898 | for (const auto& [name, dbusValue] : values) |
Shawn McCarney | 8fb49dd | 2019-06-12 17:47:00 -0500 | [diff] [blame] | 899 | { |
Ed Tanous | 711ac7a | 2021-12-20 09:34:41 -0800 | [diff] [blame] | 900 | if (name == "Present") |
| 901 | { |
| 902 | const bool* value = std::get_if<bool>(&dbusValue); |
| 903 | if (value != nullptr) |
| 904 | { |
| 905 | inventoryItem.isPresent = *value; |
| 906 | } |
| 907 | } |
Shawn McCarney | adc4f0d | 2019-07-24 09:21:50 -0500 | [diff] [blame] | 908 | } |
| 909 | } |
Ed Tanous | 711ac7a | 2021-12-20 09:34:41 -0800 | [diff] [blame] | 910 | // Check if Inventory.Item.PowerSupply interface is present |
Shawn McCarney | adc4f0d | 2019-07-24 09:21:50 -0500 | [diff] [blame] | 911 | |
Ed Tanous | 711ac7a | 2021-12-20 09:34:41 -0800 | [diff] [blame] | 912 | if (interface == "xyz.openbmc_project.Inventory.Item.PowerSupply") |
Shawn McCarney | adc4f0d | 2019-07-24 09:21:50 -0500 | [diff] [blame] | 913 | { |
Ed Tanous | 711ac7a | 2021-12-20 09:34:41 -0800 | [diff] [blame] | 914 | inventoryItem.isPowerSupply = true; |
| 915 | } |
| 916 | |
| 917 | // Get properties from Inventory.Decorator.Asset interface |
| 918 | if (interface == "xyz.openbmc_project.Inventory.Decorator.Asset") |
| 919 | { |
Ed Tanous | 9eb808c | 2022-01-25 10:19:23 -0800 | [diff] [blame] | 920 | for (const auto& [name, dbusValue] : values) |
Shawn McCarney | adc4f0d | 2019-07-24 09:21:50 -0500 | [diff] [blame] | 921 | { |
Ed Tanous | 711ac7a | 2021-12-20 09:34:41 -0800 | [diff] [blame] | 922 | if (name == "Manufacturer") |
| 923 | { |
| 924 | const std::string* value = |
| 925 | std::get_if<std::string>(&dbusValue); |
| 926 | if (value != nullptr) |
| 927 | { |
| 928 | inventoryItem.manufacturer = *value; |
| 929 | } |
| 930 | } |
| 931 | if (name == "Model") |
| 932 | { |
| 933 | const std::string* value = |
| 934 | std::get_if<std::string>(&dbusValue); |
| 935 | if (value != nullptr) |
| 936 | { |
| 937 | inventoryItem.model = *value; |
| 938 | } |
| 939 | } |
| 940 | if (name == "SerialNumber") |
| 941 | { |
| 942 | const std::string* value = |
| 943 | std::get_if<std::string>(&dbusValue); |
| 944 | if (value != nullptr) |
| 945 | { |
| 946 | inventoryItem.serialNumber = *value; |
| 947 | } |
| 948 | } |
| 949 | if (name == "PartNumber") |
| 950 | { |
| 951 | const std::string* value = |
| 952 | std::get_if<std::string>(&dbusValue); |
| 953 | if (value != nullptr) |
| 954 | { |
| 955 | inventoryItem.partNumber = *value; |
| 956 | } |
| 957 | } |
Shawn McCarney | 8fb49dd | 2019-06-12 17:47:00 -0500 | [diff] [blame] | 958 | } |
| 959 | } |
| 960 | |
Ed Tanous | 711ac7a | 2021-12-20 09:34:41 -0800 | [diff] [blame] | 961 | if (interface == |
| 962 | "xyz.openbmc_project.State.Decorator.OperationalStatus") |
Shawn McCarney | 8fb49dd | 2019-06-12 17:47:00 -0500 | [diff] [blame] | 963 | { |
Ed Tanous | 9eb808c | 2022-01-25 10:19:23 -0800 | [diff] [blame] | 964 | for (const auto& [name, dbusValue] : values) |
Shawn McCarney | 8fb49dd | 2019-06-12 17:47:00 -0500 | [diff] [blame] | 965 | { |
Ed Tanous | 711ac7a | 2021-12-20 09:34:41 -0800 | [diff] [blame] | 966 | if (name == "Functional") |
| 967 | { |
| 968 | const bool* value = std::get_if<bool>(&dbusValue); |
| 969 | if (value != nullptr) |
| 970 | { |
| 971 | inventoryItem.isFunctional = *value; |
| 972 | } |
| 973 | } |
Shawn McCarney | 8fb49dd | 2019-06-12 17:47:00 -0500 | [diff] [blame] | 974 | } |
| 975 | } |
| 976 | } |
| 977 | } |
| 978 | |
| 979 | /** |
Shawn McCarney | adc4f0d | 2019-07-24 09:21:50 -0500 | [diff] [blame] | 980 | * @brief Gets D-Bus data for inventory items associated with sensors. |
Shawn McCarney | 8fb49dd | 2019-06-12 17:47:00 -0500 | [diff] [blame] | 981 | * |
Shawn McCarney | adc4f0d | 2019-07-24 09:21:50 -0500 | [diff] [blame] | 982 | * Uses the specified connections (services) to obtain D-Bus data for inventory |
| 983 | * items associated with sensors. Stores the resulting data in the |
| 984 | * inventoryItems vector. |
Shawn McCarney | 8fb49dd | 2019-06-12 17:47:00 -0500 | [diff] [blame] | 985 | * |
Shawn McCarney | adc4f0d | 2019-07-24 09:21:50 -0500 | [diff] [blame] | 986 | * This data is later used to provide sensor property values in the JSON |
| 987 | * response. |
| 988 | * |
| 989 | * Finds the inventory item data asynchronously. Invokes callback when data has |
| 990 | * been obtained. |
| 991 | * |
| 992 | * The callback must have the following signature: |
| 993 | * @code |
Anthony Wilson | d500549 | 2019-07-31 16:34:17 -0500 | [diff] [blame] | 994 | * callback(void) |
Shawn McCarney | adc4f0d | 2019-07-24 09:21:50 -0500 | [diff] [blame] | 995 | * @endcode |
| 996 | * |
| 997 | * This function is called recursively, obtaining data asynchronously from one |
| 998 | * connection in each call. This ensures the callback is not invoked until the |
| 999 | * last asynchronous function has completed. |
Shawn McCarney | 8fb49dd | 2019-06-12 17:47:00 -0500 | [diff] [blame] | 1000 | * |
| 1001 | * @param sensorsAsyncResp Pointer to object holding response data. |
Shawn McCarney | adc4f0d | 2019-07-24 09:21:50 -0500 | [diff] [blame] | 1002 | * @param inventoryItems D-Bus inventory items associated with sensors. |
| 1003 | * @param invConnections Connections that provide data for the inventory items. |
Shawn McCarney | 8fb49dd | 2019-06-12 17:47:00 -0500 | [diff] [blame] | 1004 | * implements ObjectManager. |
Shawn McCarney | adc4f0d | 2019-07-24 09:21:50 -0500 | [diff] [blame] | 1005 | * @param callback Callback to invoke when inventory data has been obtained. |
| 1006 | * @param invConnectionsIndex Current index in invConnections. Only specified |
| 1007 | * in recursive calls to this function. |
Shawn McCarney | 8fb49dd | 2019-06-12 17:47:00 -0500 | [diff] [blame] | 1008 | */ |
Shawn McCarney | adc4f0d | 2019-07-24 09:21:50 -0500 | [diff] [blame] | 1009 | template <typename Callback> |
Ed Tanous | 4ff0f1f | 2024-09-04 17:27:37 -0700 | [diff] [blame] | 1010 | void getInventoryItemsData( |
Ed Tanous | daadfb2 | 2024-12-20 09:25:54 -0800 | [diff] [blame] | 1011 | const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp, |
| 1012 | const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems, |
| 1013 | const std::shared_ptr<std::set<std::string>>& invConnections, |
| 1014 | Callback&& callback, size_t invConnectionsIndex = 0) |
Shawn McCarney | 8fb49dd | 2019-06-12 17:47:00 -0500 | [diff] [blame] | 1015 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 1016 | BMCWEB_LOG_DEBUG("getInventoryItemsData enter"); |
Shawn McCarney | 8fb49dd | 2019-06-12 17:47:00 -0500 | [diff] [blame] | 1017 | |
Shawn McCarney | adc4f0d | 2019-07-24 09:21:50 -0500 | [diff] [blame] | 1018 | // If no more connections left, call callback |
| 1019 | if (invConnectionsIndex >= invConnections->size()) |
Shawn McCarney | 8fb49dd | 2019-06-12 17:47:00 -0500 | [diff] [blame] | 1020 | { |
Anthony Wilson | d500549 | 2019-07-31 16:34:17 -0500 | [diff] [blame] | 1021 | callback(); |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 1022 | BMCWEB_LOG_DEBUG("getInventoryItemsData exit"); |
Shawn McCarney | adc4f0d | 2019-07-24 09:21:50 -0500 | [diff] [blame] | 1023 | return; |
| 1024 | } |
| 1025 | |
| 1026 | // Get inventory item data from current connection |
Nan Zhou | fe04d49 | 2022-06-22 17:10:41 +0000 | [diff] [blame] | 1027 | auto it = invConnections->begin(); |
| 1028 | std::advance(it, invConnectionsIndex); |
Shawn McCarney | adc4f0d | 2019-07-24 09:21:50 -0500 | [diff] [blame] | 1029 | if (it != invConnections->end()) |
| 1030 | { |
| 1031 | const std::string& invConnection = *it; |
| 1032 | |
George Liu | 5eb468d | 2023-06-20 17:03:24 +0800 | [diff] [blame] | 1033 | // Get all object paths and their interfaces for current connection |
| 1034 | sdbusplus::message::object_path path("/xyz/openbmc_project/inventory"); |
| 1035 | dbus::utility::getManagedObjects( |
| 1036 | invConnection, path, |
| 1037 | [sensorsAsyncResp, inventoryItems, invConnections, |
Ed Tanous | 8cb2c02 | 2024-03-27 16:31:46 -0700 | [diff] [blame] | 1038 | callback = std::forward<Callback>(callback), invConnectionsIndex]( |
George Liu | 5eb468d | 2023-06-20 17:03:24 +0800 | [diff] [blame] | 1039 | const boost::system::error_code& ec, |
Ed Tanous | 4e0d878 | 2024-09-06 15:16:41 -0700 | [diff] [blame] | 1040 | const dbus::utility::ManagedObjectType& resp) mutable { |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 1041 | BMCWEB_LOG_DEBUG("getInventoryItemsData respHandler enter"); |
| 1042 | if (ec) |
Shawn McCarney | 8fb49dd | 2019-06-12 17:47:00 -0500 | [diff] [blame] | 1043 | { |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 1044 | BMCWEB_LOG_ERROR( |
| 1045 | "getInventoryItemsData respHandler DBus error {}", ec); |
| 1046 | messages::internalError(sensorsAsyncResp->asyncResp->res); |
| 1047 | return; |
Shawn McCarney | 8fb49dd | 2019-06-12 17:47:00 -0500 | [diff] [blame] | 1048 | } |
Shawn McCarney | 8fb49dd | 2019-06-12 17:47:00 -0500 | [diff] [blame] | 1049 | |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 1050 | // Loop through returned object paths |
| 1051 | for (const auto& objDictEntry : resp) |
| 1052 | { |
| 1053 | const std::string& objPath = |
| 1054 | static_cast<const std::string&>(objDictEntry.first); |
Shawn McCarney | adc4f0d | 2019-07-24 09:21:50 -0500 | [diff] [blame] | 1055 | |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 1056 | // If this object path is one of the specified inventory |
| 1057 | // items |
| 1058 | InventoryItem* inventoryItem = |
| 1059 | findInventoryItem(inventoryItems, objPath); |
| 1060 | if (inventoryItem != nullptr) |
| 1061 | { |
| 1062 | // Store inventory data in InventoryItem |
| 1063 | storeInventoryItemData(*inventoryItem, |
| 1064 | objDictEntry.second); |
| 1065 | } |
| 1066 | } |
| 1067 | |
| 1068 | // Recurse to get inventory item data from next connection |
| 1069 | getInventoryItemsData(sensorsAsyncResp, inventoryItems, |
| 1070 | invConnections, std::move(callback), |
| 1071 | invConnectionsIndex + 1); |
| 1072 | |
| 1073 | BMCWEB_LOG_DEBUG("getInventoryItemsData respHandler exit"); |
| 1074 | }); |
Shawn McCarney | 8fb49dd | 2019-06-12 17:47:00 -0500 | [diff] [blame] | 1075 | } |
| 1076 | |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 1077 | BMCWEB_LOG_DEBUG("getInventoryItemsData exit"); |
Shawn McCarney | 8fb49dd | 2019-06-12 17:47:00 -0500 | [diff] [blame] | 1078 | } |
| 1079 | |
| 1080 | /** |
Shawn McCarney | adc4f0d | 2019-07-24 09:21:50 -0500 | [diff] [blame] | 1081 | * @brief Gets connections that provide D-Bus data for inventory items. |
Shawn McCarney | 8fb49dd | 2019-06-12 17:47:00 -0500 | [diff] [blame] | 1082 | * |
Shawn McCarney | adc4f0d | 2019-07-24 09:21:50 -0500 | [diff] [blame] | 1083 | * Gets the D-Bus connections (services) that provide data for the inventory |
| 1084 | * items that are associated with sensors. |
Shawn McCarney | 8fb49dd | 2019-06-12 17:47:00 -0500 | [diff] [blame] | 1085 | * |
| 1086 | * Finds the connections asynchronously. Invokes callback when information has |
| 1087 | * been obtained. |
| 1088 | * |
| 1089 | * The callback must have the following signature: |
| 1090 | * @code |
Nan Zhou | fe04d49 | 2022-06-22 17:10:41 +0000 | [diff] [blame] | 1091 | * callback(std::shared_ptr<std::set<std::string>> invConnections) |
Shawn McCarney | 8fb49dd | 2019-06-12 17:47:00 -0500 | [diff] [blame] | 1092 | * @endcode |
| 1093 | * |
| 1094 | * @param sensorsAsyncResp Pointer to object holding response data. |
Shawn McCarney | adc4f0d | 2019-07-24 09:21:50 -0500 | [diff] [blame] | 1095 | * @param inventoryItems D-Bus inventory items associated with sensors. |
Shawn McCarney | 8fb49dd | 2019-06-12 17:47:00 -0500 | [diff] [blame] | 1096 | * @param callback Callback to invoke when connections have been obtained. |
| 1097 | */ |
| 1098 | template <typename Callback> |
Ed Tanous | 4ff0f1f | 2024-09-04 17:27:37 -0700 | [diff] [blame] | 1099 | void getInventoryItemsConnections( |
Ed Tanous | b5a7693 | 2020-09-29 16:16:58 -0700 | [diff] [blame] | 1100 | const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp, |
| 1101 | const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems, |
Shawn McCarney | 8fb49dd | 2019-06-12 17:47:00 -0500 | [diff] [blame] | 1102 | Callback&& callback) |
| 1103 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 1104 | BMCWEB_LOG_DEBUG("getInventoryItemsConnections enter"); |
Shawn McCarney | 8fb49dd | 2019-06-12 17:47:00 -0500 | [diff] [blame] | 1105 | |
| 1106 | const std::string path = "/xyz/openbmc_project/inventory"; |
George Liu | e99073f | 2022-12-09 11:06:16 +0800 | [diff] [blame] | 1107 | constexpr std::array<std::string_view, 4> interfaces = { |
Shawn McCarney | 8fb49dd | 2019-06-12 17:47:00 -0500 | [diff] [blame] | 1108 | "xyz.openbmc_project.Inventory.Item", |
Shawn McCarney | adc4f0d | 2019-07-24 09:21:50 -0500 | [diff] [blame] | 1109 | "xyz.openbmc_project.Inventory.Item.PowerSupply", |
| 1110 | "xyz.openbmc_project.Inventory.Decorator.Asset", |
Shawn McCarney | 8fb49dd | 2019-06-12 17:47:00 -0500 | [diff] [blame] | 1111 | "xyz.openbmc_project.State.Decorator.OperationalStatus"}; |
| 1112 | |
George Liu | e99073f | 2022-12-09 11:06:16 +0800 | [diff] [blame] | 1113 | // Make call to ObjectMapper to find all inventory items |
| 1114 | dbus::utility::getSubTree( |
| 1115 | path, 0, interfaces, |
Ed Tanous | 8cb2c02 | 2024-03-27 16:31:46 -0700 | [diff] [blame] | 1116 | [callback = std::forward<Callback>(callback), sensorsAsyncResp, |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 1117 | inventoryItems]( |
George Liu | e99073f | 2022-12-09 11:06:16 +0800 | [diff] [blame] | 1118 | const boost::system::error_code& ec, |
Ed Tanous | 4e0d878 | 2024-09-06 15:16:41 -0700 | [diff] [blame] | 1119 | const dbus::utility::MapperGetSubTreeResponse& subtree) mutable { |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 1120 | // Response handler for parsing output from GetSubTree |
| 1121 | BMCWEB_LOG_DEBUG("getInventoryItemsConnections respHandler enter"); |
| 1122 | if (ec) |
Shawn McCarney | 8fb49dd | 2019-06-12 17:47:00 -0500 | [diff] [blame] | 1123 | { |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 1124 | messages::internalError(sensorsAsyncResp->asyncResp->res); |
| 1125 | BMCWEB_LOG_ERROR( |
| 1126 | "getInventoryItemsConnections respHandler DBus error {}", |
| 1127 | ec); |
| 1128 | return; |
| 1129 | } |
| 1130 | |
| 1131 | // Make unique list of connections for desired inventory items |
| 1132 | std::shared_ptr<std::set<std::string>> invConnections = |
| 1133 | std::make_shared<std::set<std::string>>(); |
| 1134 | |
| 1135 | // Loop through objects from GetSubTree |
| 1136 | for (const std::pair<std::string, |
| 1137 | std::vector<std::pair< |
| 1138 | std::string, std::vector<std::string>>>>& |
| 1139 | object : subtree) |
| 1140 | { |
| 1141 | // Check if object path is one of the specified inventory items |
| 1142 | const std::string& objPath = object.first; |
| 1143 | if (findInventoryItem(inventoryItems, objPath) != nullptr) |
Shawn McCarney | 8fb49dd | 2019-06-12 17:47:00 -0500 | [diff] [blame] | 1144 | { |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 1145 | // Store all connections to inventory item |
| 1146 | for (const std::pair<std::string, std::vector<std::string>>& |
| 1147 | objData : object.second) |
| 1148 | { |
| 1149 | const std::string& invConnection = objData.first; |
| 1150 | invConnections->insert(invConnection); |
| 1151 | } |
Shawn McCarney | 8fb49dd | 2019-06-12 17:47:00 -0500 | [diff] [blame] | 1152 | } |
| 1153 | } |
Anthony Wilson | d500549 | 2019-07-31 16:34:17 -0500 | [diff] [blame] | 1154 | |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 1155 | callback(invConnections); |
| 1156 | BMCWEB_LOG_DEBUG("getInventoryItemsConnections respHandler exit"); |
| 1157 | }); |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 1158 | BMCWEB_LOG_DEBUG("getInventoryItemsConnections exit"); |
Shawn McCarney | 8fb49dd | 2019-06-12 17:47:00 -0500 | [diff] [blame] | 1159 | } |
| 1160 | |
| 1161 | /** |
Shawn McCarney | adc4f0d | 2019-07-24 09:21:50 -0500 | [diff] [blame] | 1162 | * @brief Gets associations from sensors to inventory items. |
Shawn McCarney | 8fb49dd | 2019-06-12 17:47:00 -0500 | [diff] [blame] | 1163 | * |
| 1164 | * Looks for ObjectMapper associations from the specified sensors to related |
Anthony Wilson | d500549 | 2019-07-31 16:34:17 -0500 | [diff] [blame] | 1165 | * inventory items. Then finds the associations from those inventory items to |
| 1166 | * their LEDs, if any. |
Shawn McCarney | 8fb49dd | 2019-06-12 17:47:00 -0500 | [diff] [blame] | 1167 | * |
| 1168 | * Finds the inventory items asynchronously. Invokes callback when information |
| 1169 | * has been obtained. |
| 1170 | * |
| 1171 | * The callback must have the following signature: |
| 1172 | * @code |
Shawn McCarney | adc4f0d | 2019-07-24 09:21:50 -0500 | [diff] [blame] | 1173 | * callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems) |
Shawn McCarney | 8fb49dd | 2019-06-12 17:47:00 -0500 | [diff] [blame] | 1174 | * @endcode |
| 1175 | * |
| 1176 | * @param sensorsAsyncResp Pointer to object holding response data. |
| 1177 | * @param sensorNames All sensors within the current chassis. |
Shawn McCarney | 8fb49dd | 2019-06-12 17:47:00 -0500 | [diff] [blame] | 1178 | * implements ObjectManager. |
| 1179 | * @param callback Callback to invoke when inventory items have been obtained. |
| 1180 | */ |
| 1181 | template <typename Callback> |
Ed Tanous | 4ff0f1f | 2024-09-04 17:27:37 -0700 | [diff] [blame] | 1182 | void getInventoryItemAssociations( |
Ed Tanous | b5a7693 | 2020-09-29 16:16:58 -0700 | [diff] [blame] | 1183 | const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp, |
Nan Zhou | fe04d49 | 2022-06-22 17:10:41 +0000 | [diff] [blame] | 1184 | const std::shared_ptr<std::set<std::string>>& sensorNames, |
Shawn McCarney | 8fb49dd | 2019-06-12 17:47:00 -0500 | [diff] [blame] | 1185 | Callback&& callback) |
| 1186 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 1187 | BMCWEB_LOG_DEBUG("getInventoryItemAssociations enter"); |
Shawn McCarney | 8fb49dd | 2019-06-12 17:47:00 -0500 | [diff] [blame] | 1188 | |
George Liu | 5eb468d | 2023-06-20 17:03:24 +0800 | [diff] [blame] | 1189 | // Call GetManagedObjects on the ObjectMapper to get all associations |
| 1190 | sdbusplus::message::object_path path("/"); |
| 1191 | dbus::utility::getManagedObjects( |
| 1192 | "xyz.openbmc_project.ObjectMapper", path, |
Ed Tanous | 8cb2c02 | 2024-03-27 16:31:46 -0700 | [diff] [blame] | 1193 | [callback = std::forward<Callback>(callback), sensorsAsyncResp, |
Ed Tanous | 5e7e2dc | 2023-02-16 10:37:01 -0800 | [diff] [blame] | 1194 | sensorNames](const boost::system::error_code& ec, |
Ed Tanous | 4e0d878 | 2024-09-06 15:16:41 -0700 | [diff] [blame] | 1195 | const dbus::utility::ManagedObjectType& resp) mutable { |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 1196 | BMCWEB_LOG_DEBUG("getInventoryItemAssociations respHandler enter"); |
| 1197 | if (ec) |
Shawn McCarney | 8fb49dd | 2019-06-12 17:47:00 -0500 | [diff] [blame] | 1198 | { |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 1199 | BMCWEB_LOG_ERROR( |
| 1200 | "getInventoryItemAssociations respHandler DBus error {}", |
| 1201 | ec); |
| 1202 | messages::internalError(sensorsAsyncResp->asyncResp->res); |
| 1203 | return; |
| 1204 | } |
| 1205 | |
| 1206 | // Create vector to hold list of inventory items |
| 1207 | std::shared_ptr<std::vector<InventoryItem>> inventoryItems = |
| 1208 | std::make_shared<std::vector<InventoryItem>>(); |
| 1209 | |
| 1210 | // Loop through returned object paths |
| 1211 | std::string sensorAssocPath; |
| 1212 | sensorAssocPath.reserve(128); // avoid memory allocations |
| 1213 | for (const auto& objDictEntry : resp) |
| 1214 | { |
| 1215 | const std::string& objPath = |
| 1216 | static_cast<const std::string&>(objDictEntry.first); |
| 1217 | |
| 1218 | // If path is inventory association for one of the specified |
| 1219 | // sensors |
| 1220 | for (const std::string& sensorName : *sensorNames) |
Shawn McCarney | 8fb49dd | 2019-06-12 17:47:00 -0500 | [diff] [blame] | 1221 | { |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 1222 | sensorAssocPath = sensorName; |
| 1223 | sensorAssocPath += "/inventory"; |
| 1224 | if (objPath == sensorAssocPath) |
Shawn McCarney | 8fb49dd | 2019-06-12 17:47:00 -0500 | [diff] [blame] | 1225 | { |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 1226 | // Get Association interface for object path |
| 1227 | for (const auto& [interface, values] : |
| 1228 | objDictEntry.second) |
Shawn McCarney | 8fb49dd | 2019-06-12 17:47:00 -0500 | [diff] [blame] | 1229 | { |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 1230 | if (interface == "xyz.openbmc_project.Association") |
Shawn McCarney | 8fb49dd | 2019-06-12 17:47:00 -0500 | [diff] [blame] | 1231 | { |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 1232 | for (const auto& [valueName, value] : values) |
Ed Tanous | 711ac7a | 2021-12-20 09:34:41 -0800 | [diff] [blame] | 1233 | { |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 1234 | if (valueName == "endpoints") |
Ed Tanous | 711ac7a | 2021-12-20 09:34:41 -0800 | [diff] [blame] | 1235 | { |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 1236 | const std::vector<std::string>* |
| 1237 | endpoints = std::get_if< |
| 1238 | std::vector<std::string>>( |
| 1239 | &value); |
| 1240 | if ((endpoints != nullptr) && |
| 1241 | !endpoints->empty()) |
| 1242 | { |
| 1243 | // Add inventory item to vector |
| 1244 | const std::string& invItemPath = |
| 1245 | endpoints->front(); |
| 1246 | addInventoryItem(inventoryItems, |
| 1247 | invItemPath, |
| 1248 | sensorName); |
| 1249 | } |
Ed Tanous | 711ac7a | 2021-12-20 09:34:41 -0800 | [diff] [blame] | 1250 | } |
| 1251 | } |
Shawn McCarney | 8fb49dd | 2019-06-12 17:47:00 -0500 | [diff] [blame] | 1252 | } |
| 1253 | } |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 1254 | break; |
Shawn McCarney | 8fb49dd | 2019-06-12 17:47:00 -0500 | [diff] [blame] | 1255 | } |
Shawn McCarney | 8fb49dd | 2019-06-12 17:47:00 -0500 | [diff] [blame] | 1256 | } |
| 1257 | } |
Shawn McCarney | 8fb49dd | 2019-06-12 17:47:00 -0500 | [diff] [blame] | 1258 | |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 1259 | // Now loop through the returned object paths again, this time to |
| 1260 | // find the leds associated with the inventory items we just found |
| 1261 | std::string inventoryAssocPath; |
| 1262 | inventoryAssocPath.reserve(128); // avoid memory allocations |
| 1263 | for (const auto& objDictEntry : resp) |
Anthony Wilson | d500549 | 2019-07-31 16:34:17 -0500 | [diff] [blame] | 1264 | { |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 1265 | const std::string& objPath = |
| 1266 | static_cast<const std::string&>(objDictEntry.first); |
| 1267 | |
| 1268 | for (InventoryItem& inventoryItem : *inventoryItems) |
Anthony Wilson | d500549 | 2019-07-31 16:34:17 -0500 | [diff] [blame] | 1269 | { |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 1270 | inventoryAssocPath = inventoryItem.objectPath; |
| 1271 | inventoryAssocPath += "/leds"; |
| 1272 | if (objPath == inventoryAssocPath) |
Anthony Wilson | d500549 | 2019-07-31 16:34:17 -0500 | [diff] [blame] | 1273 | { |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 1274 | for (const auto& [interface, values] : |
| 1275 | objDictEntry.second) |
Anthony Wilson | d500549 | 2019-07-31 16:34:17 -0500 | [diff] [blame] | 1276 | { |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 1277 | if (interface == "xyz.openbmc_project.Association") |
Anthony Wilson | d500549 | 2019-07-31 16:34:17 -0500 | [diff] [blame] | 1278 | { |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 1279 | for (const auto& [valueName, value] : values) |
Ed Tanous | 711ac7a | 2021-12-20 09:34:41 -0800 | [diff] [blame] | 1280 | { |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 1281 | if (valueName == "endpoints") |
Ed Tanous | 711ac7a | 2021-12-20 09:34:41 -0800 | [diff] [blame] | 1282 | { |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 1283 | const std::vector<std::string>* |
| 1284 | endpoints = std::get_if< |
| 1285 | std::vector<std::string>>( |
| 1286 | &value); |
| 1287 | if ((endpoints != nullptr) && |
| 1288 | !endpoints->empty()) |
| 1289 | { |
| 1290 | // Add inventory item to vector |
| 1291 | // Store LED path in inventory item |
| 1292 | const std::string& ledPath = |
| 1293 | endpoints->front(); |
| 1294 | inventoryItem.ledObjectPath = |
| 1295 | ledPath; |
| 1296 | } |
Ed Tanous | 711ac7a | 2021-12-20 09:34:41 -0800 | [diff] [blame] | 1297 | } |
| 1298 | } |
Anthony Wilson | d500549 | 2019-07-31 16:34:17 -0500 | [diff] [blame] | 1299 | } |
| 1300 | } |
Ed Tanous | 711ac7a | 2021-12-20 09:34:41 -0800 | [diff] [blame] | 1301 | |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 1302 | break; |
| 1303 | } |
Anthony Wilson | d500549 | 2019-07-31 16:34:17 -0500 | [diff] [blame] | 1304 | } |
| 1305 | } |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 1306 | callback(inventoryItems); |
| 1307 | BMCWEB_LOG_DEBUG("getInventoryItemAssociations respHandler exit"); |
| 1308 | }); |
Shawn McCarney | 8fb49dd | 2019-06-12 17:47:00 -0500 | [diff] [blame] | 1309 | |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 1310 | BMCWEB_LOG_DEBUG("getInventoryItemAssociations exit"); |
Shawn McCarney | 8fb49dd | 2019-06-12 17:47:00 -0500 | [diff] [blame] | 1311 | } |
| 1312 | |
| 1313 | /** |
Anthony Wilson | d500549 | 2019-07-31 16:34:17 -0500 | [diff] [blame] | 1314 | * @brief Gets D-Bus data for inventory item leds associated with sensors. |
| 1315 | * |
| 1316 | * Uses the specified connections (services) to obtain D-Bus data for inventory |
| 1317 | * item leds associated with sensors. Stores the resulting data in the |
| 1318 | * inventoryItems vector. |
| 1319 | * |
| 1320 | * This data is later used to provide sensor property values in the JSON |
| 1321 | * response. |
| 1322 | * |
| 1323 | * Finds the inventory item led data asynchronously. Invokes callback when data |
| 1324 | * has been obtained. |
| 1325 | * |
| 1326 | * The callback must have the following signature: |
| 1327 | * @code |
Gunnar Mills | 42cbe53 | 2019-08-15 15:26:54 -0500 | [diff] [blame] | 1328 | * callback() |
Anthony Wilson | d500549 | 2019-07-31 16:34:17 -0500 | [diff] [blame] | 1329 | * @endcode |
| 1330 | * |
| 1331 | * This function is called recursively, obtaining data asynchronously from one |
| 1332 | * connection in each call. This ensures the callback is not invoked until the |
| 1333 | * last asynchronous function has completed. |
| 1334 | * |
| 1335 | * @param sensorsAsyncResp Pointer to object holding response data. |
| 1336 | * @param inventoryItems D-Bus inventory items associated with sensors. |
| 1337 | * @param ledConnections Connections that provide data for the inventory leds. |
| 1338 | * @param callback Callback to invoke when inventory data has been obtained. |
| 1339 | * @param ledConnectionsIndex Current index in ledConnections. Only specified |
| 1340 | * in recursive calls to this function. |
| 1341 | */ |
| 1342 | template <typename Callback> |
| 1343 | void getInventoryLedData( |
Ed Tanous | daadfb2 | 2024-12-20 09:25:54 -0800 | [diff] [blame] | 1344 | const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp, |
| 1345 | const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems, |
| 1346 | const std::shared_ptr<std::map<std::string, std::string>>& ledConnections, |
Anthony Wilson | d500549 | 2019-07-31 16:34:17 -0500 | [diff] [blame] | 1347 | Callback&& callback, size_t ledConnectionsIndex = 0) |
| 1348 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 1349 | BMCWEB_LOG_DEBUG("getInventoryLedData enter"); |
Anthony Wilson | d500549 | 2019-07-31 16:34:17 -0500 | [diff] [blame] | 1350 | |
| 1351 | // If no more connections left, call callback |
| 1352 | if (ledConnectionsIndex >= ledConnections->size()) |
| 1353 | { |
Gunnar Mills | 42cbe53 | 2019-08-15 15:26:54 -0500 | [diff] [blame] | 1354 | callback(); |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 1355 | BMCWEB_LOG_DEBUG("getInventoryLedData exit"); |
Anthony Wilson | d500549 | 2019-07-31 16:34:17 -0500 | [diff] [blame] | 1356 | return; |
| 1357 | } |
| 1358 | |
| 1359 | // Get inventory item data from current connection |
Nan Zhou | fe04d49 | 2022-06-22 17:10:41 +0000 | [diff] [blame] | 1360 | auto it = ledConnections->begin(); |
| 1361 | std::advance(it, ledConnectionsIndex); |
Anthony Wilson | d500549 | 2019-07-31 16:34:17 -0500 | [diff] [blame] | 1362 | if (it != ledConnections->end()) |
| 1363 | { |
| 1364 | const std::string& ledPath = (*it).first; |
| 1365 | const std::string& ledConnection = (*it).second; |
| 1366 | // Response handler for Get State property |
Jonathan Doman | 1e1e598 | 2021-06-11 09:36:17 -0700 | [diff] [blame] | 1367 | auto respHandler = |
| 1368 | [sensorsAsyncResp, inventoryItems, ledConnections, ledPath, |
Ed Tanous | 4e0d878 | 2024-09-06 15:16:41 -0700 | [diff] [blame] | 1369 | callback = std::forward<Callback>(callback), |
| 1370 | ledConnectionsIndex](const boost::system::error_code& ec, |
| 1371 | const std::string& state) mutable { |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 1372 | BMCWEB_LOG_DEBUG("getInventoryLedData respHandler enter"); |
| 1373 | if (ec) |
| 1374 | { |
| 1375 | BMCWEB_LOG_ERROR( |
| 1376 | "getInventoryLedData respHandler DBus error {}", ec); |
| 1377 | messages::internalError(sensorsAsyncResp->asyncResp->res); |
| 1378 | return; |
| 1379 | } |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 1380 | |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 1381 | BMCWEB_LOG_DEBUG("Led state: {}", state); |
| 1382 | // Find inventory item with this LED object path |
| 1383 | InventoryItem* inventoryItem = |
| 1384 | findInventoryItemForLed(*inventoryItems, ledPath); |
| 1385 | if (inventoryItem != nullptr) |
Jonathan Doman | 1e1e598 | 2021-06-11 09:36:17 -0700 | [diff] [blame] | 1386 | { |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 1387 | // Store LED state in InventoryItem |
| 1388 | if (state.ends_with("On")) |
| 1389 | { |
Janet Adkins | c956360 | 2024-08-28 11:37:46 -0500 | [diff] [blame] | 1390 | inventoryItem->ledState = sensor_utils::LedState::ON; |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 1391 | } |
| 1392 | else if (state.ends_with("Blink")) |
| 1393 | { |
Janet Adkins | c956360 | 2024-08-28 11:37:46 -0500 | [diff] [blame] | 1394 | inventoryItem->ledState = sensor_utils::LedState::BLINK; |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 1395 | } |
| 1396 | else if (state.ends_with("Off")) |
| 1397 | { |
Janet Adkins | c956360 | 2024-08-28 11:37:46 -0500 | [diff] [blame] | 1398 | inventoryItem->ledState = sensor_utils::LedState::OFF; |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 1399 | } |
| 1400 | else |
| 1401 | { |
Janet Adkins | c956360 | 2024-08-28 11:37:46 -0500 | [diff] [blame] | 1402 | inventoryItem->ledState = |
| 1403 | sensor_utils::LedState::UNKNOWN; |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 1404 | } |
Jonathan Doman | 1e1e598 | 2021-06-11 09:36:17 -0700 | [diff] [blame] | 1405 | } |
Anthony Wilson | d500549 | 2019-07-31 16:34:17 -0500 | [diff] [blame] | 1406 | |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 1407 | // Recurse to get LED data from next connection |
| 1408 | getInventoryLedData(sensorsAsyncResp, inventoryItems, |
| 1409 | ledConnections, std::move(callback), |
| 1410 | ledConnectionsIndex + 1); |
Anthony Wilson | d500549 | 2019-07-31 16:34:17 -0500 | [diff] [blame] | 1411 | |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 1412 | BMCWEB_LOG_DEBUG("getInventoryLedData respHandler exit"); |
| 1413 | }; |
Anthony Wilson | d500549 | 2019-07-31 16:34:17 -0500 | [diff] [blame] | 1414 | |
| 1415 | // Get the State property for the current LED |
Ed Tanous | deae6a7 | 2024-11-11 21:58:57 -0800 | [diff] [blame] | 1416 | dbus::utility::getProperty<std::string>( |
| 1417 | ledConnection, ledPath, "xyz.openbmc_project.Led.Physical", "State", |
Jonathan Doman | 1e1e598 | 2021-06-11 09:36:17 -0700 | [diff] [blame] | 1418 | std::move(respHandler)); |
Anthony Wilson | d500549 | 2019-07-31 16:34:17 -0500 | [diff] [blame] | 1419 | } |
| 1420 | |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 1421 | BMCWEB_LOG_DEBUG("getInventoryLedData exit"); |
Anthony Wilson | d500549 | 2019-07-31 16:34:17 -0500 | [diff] [blame] | 1422 | } |
| 1423 | |
| 1424 | /** |
| 1425 | * @brief Gets LED data for LEDs associated with given inventory items. |
| 1426 | * |
| 1427 | * Gets the D-Bus connections (services) that provide LED data for the LEDs |
| 1428 | * associated with the specified inventory items. Then gets the LED data from |
| 1429 | * each connection and stores it in the inventory item. |
| 1430 | * |
| 1431 | * This data is later used to provide sensor property values in the JSON |
| 1432 | * response. |
| 1433 | * |
| 1434 | * Finds the LED data asynchronously. Invokes callback when information has |
| 1435 | * been obtained. |
| 1436 | * |
| 1437 | * The callback must have the following signature: |
| 1438 | * @code |
Gunnar Mills | 42cbe53 | 2019-08-15 15:26:54 -0500 | [diff] [blame] | 1439 | * callback() |
Anthony Wilson | d500549 | 2019-07-31 16:34:17 -0500 | [diff] [blame] | 1440 | * @endcode |
| 1441 | * |
| 1442 | * @param sensorsAsyncResp Pointer to object holding response data. |
| 1443 | * @param inventoryItems D-Bus inventory items associated with sensors. |
| 1444 | * @param callback Callback to invoke when inventory items have been obtained. |
| 1445 | */ |
| 1446 | template <typename Callback> |
| 1447 | void getInventoryLeds( |
Ed Tanous | daadfb2 | 2024-12-20 09:25:54 -0800 | [diff] [blame] | 1448 | const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp, |
| 1449 | const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems, |
Anthony Wilson | d500549 | 2019-07-31 16:34:17 -0500 | [diff] [blame] | 1450 | Callback&& callback) |
| 1451 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 1452 | BMCWEB_LOG_DEBUG("getInventoryLeds enter"); |
Anthony Wilson | d500549 | 2019-07-31 16:34:17 -0500 | [diff] [blame] | 1453 | |
| 1454 | const std::string path = "/xyz/openbmc_project"; |
George Liu | e99073f | 2022-12-09 11:06:16 +0800 | [diff] [blame] | 1455 | constexpr std::array<std::string_view, 1> interfaces = { |
Anthony Wilson | d500549 | 2019-07-31 16:34:17 -0500 | [diff] [blame] | 1456 | "xyz.openbmc_project.Led.Physical"}; |
| 1457 | |
George Liu | e99073f | 2022-12-09 11:06:16 +0800 | [diff] [blame] | 1458 | // Make call to ObjectMapper to find all inventory items |
| 1459 | dbus::utility::getSubTree( |
| 1460 | path, 0, interfaces, |
Ed Tanous | 8cb2c02 | 2024-03-27 16:31:46 -0700 | [diff] [blame] | 1461 | [callback = std::forward<Callback>(callback), sensorsAsyncResp, |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 1462 | inventoryItems]( |
George Liu | e99073f | 2022-12-09 11:06:16 +0800 | [diff] [blame] | 1463 | const boost::system::error_code& ec, |
Ed Tanous | 4e0d878 | 2024-09-06 15:16:41 -0700 | [diff] [blame] | 1464 | const dbus::utility::MapperGetSubTreeResponse& subtree) mutable { |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 1465 | // Response handler for parsing output from GetSubTree |
| 1466 | BMCWEB_LOG_DEBUG("getInventoryLeds respHandler enter"); |
| 1467 | if (ec) |
Anthony Wilson | d500549 | 2019-07-31 16:34:17 -0500 | [diff] [blame] | 1468 | { |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 1469 | messages::internalError(sensorsAsyncResp->asyncResp->res); |
| 1470 | BMCWEB_LOG_ERROR("getInventoryLeds respHandler DBus error {}", |
| 1471 | ec); |
| 1472 | return; |
Anthony Wilson | d500549 | 2019-07-31 16:34:17 -0500 | [diff] [blame] | 1473 | } |
Anthony Wilson | d500549 | 2019-07-31 16:34:17 -0500 | [diff] [blame] | 1474 | |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 1475 | // Build map of LED object paths to connections |
| 1476 | std::shared_ptr<std::map<std::string, std::string>> ledConnections = |
| 1477 | std::make_shared<std::map<std::string, std::string>>(); |
| 1478 | |
| 1479 | // Loop through objects from GetSubTree |
| 1480 | for (const std::pair<std::string, |
| 1481 | std::vector<std::pair< |
| 1482 | std::string, std::vector<std::string>>>>& |
| 1483 | object : subtree) |
| 1484 | { |
| 1485 | // Check if object path is LED for one of the specified |
| 1486 | // inventory items |
| 1487 | const std::string& ledPath = object.first; |
| 1488 | if (findInventoryItemForLed(*inventoryItems, ledPath) != |
| 1489 | nullptr) |
| 1490 | { |
| 1491 | // Add mapping from ledPath to connection |
| 1492 | const std::string& connection = |
| 1493 | object.second.begin()->first; |
| 1494 | (*ledConnections)[ledPath] = connection; |
| 1495 | BMCWEB_LOG_DEBUG("Added mapping {} -> {}", ledPath, |
| 1496 | connection); |
| 1497 | } |
| 1498 | } |
| 1499 | |
| 1500 | getInventoryLedData(sensorsAsyncResp, inventoryItems, |
| 1501 | ledConnections, std::move(callback)); |
| 1502 | BMCWEB_LOG_DEBUG("getInventoryLeds respHandler exit"); |
| 1503 | }); |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 1504 | BMCWEB_LOG_DEBUG("getInventoryLeds exit"); |
Anthony Wilson | d500549 | 2019-07-31 16:34:17 -0500 | [diff] [blame] | 1505 | } |
| 1506 | |
| 1507 | /** |
Gunnar Mills | 42cbe53 | 2019-08-15 15:26:54 -0500 | [diff] [blame] | 1508 | * @brief Gets D-Bus data for Power Supply Attributes such as EfficiencyPercent |
| 1509 | * |
| 1510 | * Uses the specified connections (services) (currently assumes just one) to |
| 1511 | * obtain D-Bus data for Power Supply Attributes. Stores the resulting data in |
| 1512 | * the inventoryItems vector. Only stores data in Power Supply inventoryItems. |
| 1513 | * |
| 1514 | * This data is later used to provide sensor property values in the JSON |
| 1515 | * response. |
| 1516 | * |
| 1517 | * Finds the Power Supply Attributes data asynchronously. Invokes callback |
| 1518 | * when data has been obtained. |
| 1519 | * |
| 1520 | * The callback must have the following signature: |
| 1521 | * @code |
| 1522 | * callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems) |
| 1523 | * @endcode |
| 1524 | * |
| 1525 | * @param sensorsAsyncResp Pointer to object holding response data. |
| 1526 | * @param inventoryItems D-Bus inventory items associated with sensors. |
| 1527 | * @param psAttributesConnections Connections that provide data for the Power |
| 1528 | * Supply Attributes |
| 1529 | * @param callback Callback to invoke when data has been obtained. |
| 1530 | */ |
| 1531 | template <typename Callback> |
| 1532 | void getPowerSupplyAttributesData( |
Ed Tanous | b5a7693 | 2020-09-29 16:16:58 -0700 | [diff] [blame] | 1533 | const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp, |
Ed Tanous | daadfb2 | 2024-12-20 09:25:54 -0800 | [diff] [blame] | 1534 | const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems, |
Nan Zhou | fe04d49 | 2022-06-22 17:10:41 +0000 | [diff] [blame] | 1535 | const std::map<std::string, std::string>& psAttributesConnections, |
Gunnar Mills | 42cbe53 | 2019-08-15 15:26:54 -0500 | [diff] [blame] | 1536 | Callback&& callback) |
| 1537 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 1538 | BMCWEB_LOG_DEBUG("getPowerSupplyAttributesData enter"); |
Gunnar Mills | 42cbe53 | 2019-08-15 15:26:54 -0500 | [diff] [blame] | 1539 | |
| 1540 | if (psAttributesConnections.empty()) |
| 1541 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 1542 | BMCWEB_LOG_DEBUG("Can't find PowerSupplyAttributes, no connections!"); |
Gunnar Mills | 42cbe53 | 2019-08-15 15:26:54 -0500 | [diff] [blame] | 1543 | callback(inventoryItems); |
| 1544 | return; |
| 1545 | } |
| 1546 | |
| 1547 | // Assuming just one connection (service) for now |
Nan Zhou | fe04d49 | 2022-06-22 17:10:41 +0000 | [diff] [blame] | 1548 | auto it = psAttributesConnections.begin(); |
Gunnar Mills | 42cbe53 | 2019-08-15 15:26:54 -0500 | [diff] [blame] | 1549 | |
| 1550 | const std::string& psAttributesPath = (*it).first; |
| 1551 | const std::string& psAttributesConnection = (*it).second; |
| 1552 | |
| 1553 | // Response handler for Get DeratingFactor property |
Patrick Williams | 5a39f77 | 2023-10-20 11:20:21 -0500 | [diff] [blame] | 1554 | auto respHandler = [sensorsAsyncResp, inventoryItems, |
Ed Tanous | 8cb2c02 | 2024-03-27 16:31:46 -0700 | [diff] [blame] | 1555 | callback = std::forward<Callback>(callback)]( |
Patrick Williams | 5a39f77 | 2023-10-20 11:20:21 -0500 | [diff] [blame] | 1556 | const boost::system::error_code& ec, |
Ed Tanous | 4e0d878 | 2024-09-06 15:16:41 -0700 | [diff] [blame] | 1557 | uint32_t value) mutable { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 1558 | BMCWEB_LOG_DEBUG("getPowerSupplyAttributesData respHandler enter"); |
Gunnar Mills | 42cbe53 | 2019-08-15 15:26:54 -0500 | [diff] [blame] | 1559 | if (ec) |
| 1560 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 1561 | BMCWEB_LOG_ERROR( |
| 1562 | "getPowerSupplyAttributesData respHandler DBus error {}", ec); |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 1563 | messages::internalError(sensorsAsyncResp->asyncResp->res); |
Gunnar Mills | 42cbe53 | 2019-08-15 15:26:54 -0500 | [diff] [blame] | 1564 | return; |
| 1565 | } |
| 1566 | |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 1567 | BMCWEB_LOG_DEBUG("PS EfficiencyPercent value: {}", value); |
Jonathan Doman | 1e1e598 | 2021-06-11 09:36:17 -0700 | [diff] [blame] | 1568 | // Store value in Power Supply Inventory Items |
| 1569 | for (InventoryItem& inventoryItem : *inventoryItems) |
Gunnar Mills | 42cbe53 | 2019-08-15 15:26:54 -0500 | [diff] [blame] | 1570 | { |
Ed Tanous | 55f79e6 | 2022-01-25 11:26:16 -0800 | [diff] [blame] | 1571 | if (inventoryItem.isPowerSupply) |
Gunnar Mills | 42cbe53 | 2019-08-15 15:26:54 -0500 | [diff] [blame] | 1572 | { |
Jonathan Doman | 1e1e598 | 2021-06-11 09:36:17 -0700 | [diff] [blame] | 1573 | inventoryItem.powerSupplyEfficiencyPercent = |
| 1574 | static_cast<int>(value); |
Gunnar Mills | 42cbe53 | 2019-08-15 15:26:54 -0500 | [diff] [blame] | 1575 | } |
| 1576 | } |
Gunnar Mills | 42cbe53 | 2019-08-15 15:26:54 -0500 | [diff] [blame] | 1577 | |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 1578 | BMCWEB_LOG_DEBUG("getPowerSupplyAttributesData respHandler exit"); |
Gunnar Mills | 42cbe53 | 2019-08-15 15:26:54 -0500 | [diff] [blame] | 1579 | callback(inventoryItems); |
| 1580 | }; |
| 1581 | |
| 1582 | // Get the DeratingFactor property for the PowerSupplyAttributes |
| 1583 | // Currently only property on the interface/only one we care about |
Ed Tanous | deae6a7 | 2024-11-11 21:58:57 -0800 | [diff] [blame] | 1584 | dbus::utility::getProperty<uint32_t>( |
| 1585 | psAttributesConnection, psAttributesPath, |
Jonathan Doman | 1e1e598 | 2021-06-11 09:36:17 -0700 | [diff] [blame] | 1586 | "xyz.openbmc_project.Control.PowerSupplyAttributes", "DeratingFactor", |
| 1587 | std::move(respHandler)); |
Gunnar Mills | 42cbe53 | 2019-08-15 15:26:54 -0500 | [diff] [blame] | 1588 | |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 1589 | BMCWEB_LOG_DEBUG("getPowerSupplyAttributesData exit"); |
Gunnar Mills | 42cbe53 | 2019-08-15 15:26:54 -0500 | [diff] [blame] | 1590 | } |
| 1591 | |
| 1592 | /** |
| 1593 | * @brief Gets the Power Supply Attributes such as EfficiencyPercent |
| 1594 | * |
| 1595 | * Gets the D-Bus connection (service) that provides Power Supply Attributes |
| 1596 | * data. Then gets the Power Supply Attributes data from the connection |
| 1597 | * (currently just assumes 1 connection) and stores the data in the inventory |
| 1598 | * item. |
| 1599 | * |
| 1600 | * This data is later used to provide sensor property values in the JSON |
| 1601 | * response. DeratingFactor on D-Bus is mapped to EfficiencyPercent on Redfish. |
| 1602 | * |
| 1603 | * Finds the Power Supply Attributes data asynchronously. Invokes callback |
| 1604 | * when information has been obtained. |
| 1605 | * |
| 1606 | * The callback must have the following signature: |
| 1607 | * @code |
| 1608 | * callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems) |
| 1609 | * @endcode |
| 1610 | * |
| 1611 | * @param sensorsAsyncResp Pointer to object holding response data. |
| 1612 | * @param inventoryItems D-Bus inventory items associated with sensors. |
| 1613 | * @param callback Callback to invoke when data has been obtained. |
| 1614 | */ |
| 1615 | template <typename Callback> |
| 1616 | void getPowerSupplyAttributes( |
Ed Tanous | daadfb2 | 2024-12-20 09:25:54 -0800 | [diff] [blame] | 1617 | const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp, |
| 1618 | const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems, |
Gunnar Mills | 42cbe53 | 2019-08-15 15:26:54 -0500 | [diff] [blame] | 1619 | Callback&& callback) |
| 1620 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 1621 | BMCWEB_LOG_DEBUG("getPowerSupplyAttributes enter"); |
Gunnar Mills | 42cbe53 | 2019-08-15 15:26:54 -0500 | [diff] [blame] | 1622 | |
| 1623 | // Only need the power supply attributes when the Power Schema |
Janet Adkins | 0c728b4 | 2024-08-29 11:09:10 -0500 | [diff] [blame] | 1624 | if (sensorsAsyncResp->chassisSubNode != sensors::powerNodeStr) |
Gunnar Mills | 42cbe53 | 2019-08-15 15:26:54 -0500 | [diff] [blame] | 1625 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 1626 | BMCWEB_LOG_DEBUG("getPowerSupplyAttributes exit since not Power"); |
Gunnar Mills | 42cbe53 | 2019-08-15 15:26:54 -0500 | [diff] [blame] | 1627 | callback(inventoryItems); |
| 1628 | return; |
| 1629 | } |
| 1630 | |
George Liu | e99073f | 2022-12-09 11:06:16 +0800 | [diff] [blame] | 1631 | constexpr std::array<std::string_view, 1> interfaces = { |
Gunnar Mills | 42cbe53 | 2019-08-15 15:26:54 -0500 | [diff] [blame] | 1632 | "xyz.openbmc_project.Control.PowerSupplyAttributes"}; |
| 1633 | |
George Liu | e99073f | 2022-12-09 11:06:16 +0800 | [diff] [blame] | 1634 | // Make call to ObjectMapper to find the PowerSupplyAttributes service |
| 1635 | dbus::utility::getSubTree( |
| 1636 | "/xyz/openbmc_project", 0, interfaces, |
Ed Tanous | 8cb2c02 | 2024-03-27 16:31:46 -0700 | [diff] [blame] | 1637 | [callback = std::forward<Callback>(callback), sensorsAsyncResp, |
Ed Tanous | b9d36b4 | 2022-02-26 21:42:46 -0800 | [diff] [blame] | 1638 | inventoryItems]( |
George Liu | e99073f | 2022-12-09 11:06:16 +0800 | [diff] [blame] | 1639 | const boost::system::error_code& ec, |
Ed Tanous | 4e0d878 | 2024-09-06 15:16:41 -0700 | [diff] [blame] | 1640 | const dbus::utility::MapperGetSubTreeResponse& subtree) mutable { |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 1641 | // Response handler for parsing output from GetSubTree |
| 1642 | BMCWEB_LOG_DEBUG("getPowerSupplyAttributes respHandler enter"); |
| 1643 | if (ec) |
| 1644 | { |
| 1645 | messages::internalError(sensorsAsyncResp->asyncResp->res); |
| 1646 | BMCWEB_LOG_ERROR( |
| 1647 | "getPowerSupplyAttributes respHandler DBus error {}", ec); |
| 1648 | return; |
| 1649 | } |
| 1650 | if (subtree.empty()) |
| 1651 | { |
| 1652 | BMCWEB_LOG_DEBUG("Can't find Power Supply Attributes!"); |
| 1653 | callback(inventoryItems); |
| 1654 | return; |
| 1655 | } |
Gunnar Mills | 42cbe53 | 2019-08-15 15:26:54 -0500 | [diff] [blame] | 1656 | |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 1657 | // Currently we only support 1 power supply attribute, use this for |
| 1658 | // all the power supplies. Build map of object path to connection. |
| 1659 | // Assume just 1 connection and 1 path for now. |
| 1660 | std::map<std::string, std::string> psAttributesConnections; |
Gunnar Mills | 42cbe53 | 2019-08-15 15:26:54 -0500 | [diff] [blame] | 1661 | |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 1662 | if (subtree[0].first.empty() || subtree[0].second.empty()) |
| 1663 | { |
| 1664 | BMCWEB_LOG_DEBUG("Power Supply Attributes mapper error!"); |
| 1665 | callback(inventoryItems); |
| 1666 | return; |
| 1667 | } |
Gunnar Mills | 42cbe53 | 2019-08-15 15:26:54 -0500 | [diff] [blame] | 1668 | |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 1669 | const std::string& psAttributesPath = subtree[0].first; |
| 1670 | const std::string& connection = subtree[0].second.begin()->first; |
Gunnar Mills | 42cbe53 | 2019-08-15 15:26:54 -0500 | [diff] [blame] | 1671 | |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 1672 | if (connection.empty()) |
| 1673 | { |
| 1674 | BMCWEB_LOG_DEBUG("Power Supply Attributes mapper error!"); |
| 1675 | callback(inventoryItems); |
| 1676 | return; |
| 1677 | } |
Gunnar Mills | 42cbe53 | 2019-08-15 15:26:54 -0500 | [diff] [blame] | 1678 | |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 1679 | psAttributesConnections[psAttributesPath] = connection; |
| 1680 | BMCWEB_LOG_DEBUG("Added mapping {} -> {}", psAttributesPath, |
| 1681 | connection); |
Gunnar Mills | 42cbe53 | 2019-08-15 15:26:54 -0500 | [diff] [blame] | 1682 | |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 1683 | getPowerSupplyAttributesData(sensorsAsyncResp, inventoryItems, |
| 1684 | psAttributesConnections, |
| 1685 | std::move(callback)); |
| 1686 | BMCWEB_LOG_DEBUG("getPowerSupplyAttributes respHandler exit"); |
| 1687 | }); |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 1688 | BMCWEB_LOG_DEBUG("getPowerSupplyAttributes exit"); |
Gunnar Mills | 42cbe53 | 2019-08-15 15:26:54 -0500 | [diff] [blame] | 1689 | } |
| 1690 | |
| 1691 | /** |
Shawn McCarney | adc4f0d | 2019-07-24 09:21:50 -0500 | [diff] [blame] | 1692 | * @brief Gets inventory items associated with sensors. |
Shawn McCarney | 8fb49dd | 2019-06-12 17:47:00 -0500 | [diff] [blame] | 1693 | * |
| 1694 | * Finds the inventory items that are associated with the specified sensors. |
Shawn McCarney | adc4f0d | 2019-07-24 09:21:50 -0500 | [diff] [blame] | 1695 | * Then gets D-Bus data for the inventory items, such as presence and VPD. |
Shawn McCarney | 8fb49dd | 2019-06-12 17:47:00 -0500 | [diff] [blame] | 1696 | * |
Shawn McCarney | adc4f0d | 2019-07-24 09:21:50 -0500 | [diff] [blame] | 1697 | * This data is later used to provide sensor property values in the JSON |
| 1698 | * response. |
Shawn McCarney | 8fb49dd | 2019-06-12 17:47:00 -0500 | [diff] [blame] | 1699 | * |
Shawn McCarney | adc4f0d | 2019-07-24 09:21:50 -0500 | [diff] [blame] | 1700 | * Finds the inventory items asynchronously. Invokes callback when the |
| 1701 | * inventory items have been obtained. |
| 1702 | * |
| 1703 | * The callback must have the following signature: |
| 1704 | * @code |
| 1705 | * callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems) |
| 1706 | * @endcode |
Shawn McCarney | 8fb49dd | 2019-06-12 17:47:00 -0500 | [diff] [blame] | 1707 | * |
| 1708 | * @param sensorsAsyncResp Pointer to object holding response data. |
| 1709 | * @param sensorNames All sensors within the current chassis. |
Shawn McCarney | 8fb49dd | 2019-06-12 17:47:00 -0500 | [diff] [blame] | 1710 | * implements ObjectManager. |
Shawn McCarney | adc4f0d | 2019-07-24 09:21:50 -0500 | [diff] [blame] | 1711 | * @param callback Callback to invoke when inventory items have been obtained. |
Shawn McCarney | 8fb49dd | 2019-06-12 17:47:00 -0500 | [diff] [blame] | 1712 | */ |
Shawn McCarney | adc4f0d | 2019-07-24 09:21:50 -0500 | [diff] [blame] | 1713 | template <typename Callback> |
Patrick Williams | 504af5a | 2025-02-03 14:29:03 -0500 | [diff] [blame] | 1714 | inline void getInventoryItems( |
| 1715 | const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp, |
| 1716 | const std::shared_ptr<std::set<std::string>>& sensorNames, |
| 1717 | Callback&& callback) |
Shawn McCarney | 8fb49dd | 2019-06-12 17:47:00 -0500 | [diff] [blame] | 1718 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 1719 | BMCWEB_LOG_DEBUG("getInventoryItems enter"); |
Shawn McCarney | adc4f0d | 2019-07-24 09:21:50 -0500 | [diff] [blame] | 1720 | auto getInventoryItemAssociationsCb = |
Ed Tanous | 8cb2c02 | 2024-03-27 16:31:46 -0700 | [diff] [blame] | 1721 | [sensorsAsyncResp, callback = std::forward<Callback>(callback)]( |
Ed Tanous | daadfb2 | 2024-12-20 09:25:54 -0800 | [diff] [blame] | 1722 | const std::shared_ptr<std::vector<InventoryItem>>& |
Ed Tanous | 4e0d878 | 2024-09-06 15:16:41 -0700 | [diff] [blame] | 1723 | inventoryItems) mutable { |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 1724 | BMCWEB_LOG_DEBUG("getInventoryItemAssociationsCb enter"); |
| 1725 | auto getInventoryItemsConnectionsCb = |
| 1726 | [sensorsAsyncResp, inventoryItems, |
Ed Tanous | 4e0d878 | 2024-09-06 15:16:41 -0700 | [diff] [blame] | 1727 | callback = std::forward<Callback>(callback)]( |
Ed Tanous | daadfb2 | 2024-12-20 09:25:54 -0800 | [diff] [blame] | 1728 | const std::shared_ptr<std::set<std::string>>& |
Ed Tanous | 4e0d878 | 2024-09-06 15:16:41 -0700 | [diff] [blame] | 1729 | invConnections) mutable { |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 1730 | BMCWEB_LOG_DEBUG("getInventoryItemsConnectionsCb enter"); |
| 1731 | auto getInventoryItemsDataCb = |
| 1732 | [sensorsAsyncResp, inventoryItems, |
Ed Tanous | 4e0d878 | 2024-09-06 15:16:41 -0700 | [diff] [blame] | 1733 | callback = |
| 1734 | std::forward<Callback>(callback)]() mutable { |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 1735 | BMCWEB_LOG_DEBUG("getInventoryItemsDataCb enter"); |
Gunnar Mills | 42cbe53 | 2019-08-15 15:26:54 -0500 | [diff] [blame] | 1736 | |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 1737 | auto getInventoryLedsCb = |
| 1738 | [sensorsAsyncResp, inventoryItems, |
Ed Tanous | 4e0d878 | 2024-09-06 15:16:41 -0700 | [diff] [blame] | 1739 | callback = std::forward<Callback>( |
| 1740 | callback)]() mutable { |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 1741 | BMCWEB_LOG_DEBUG( |
| 1742 | "getInventoryLedsCb enter"); |
| 1743 | // Find Power Supply Attributes and get the |
| 1744 | // data |
| 1745 | getPowerSupplyAttributes( |
| 1746 | sensorsAsyncResp, inventoryItems, |
| 1747 | std::move(callback)); |
| 1748 | BMCWEB_LOG_DEBUG("getInventoryLedsCb exit"); |
| 1749 | }; |
| 1750 | |
| 1751 | // Find led connections and get the data |
| 1752 | getInventoryLeds(sensorsAsyncResp, inventoryItems, |
| 1753 | std::move(getInventoryLedsCb)); |
| 1754 | BMCWEB_LOG_DEBUG("getInventoryItemsDataCb exit"); |
| 1755 | }; |
| 1756 | |
| 1757 | // Get inventory item data from connections |
| 1758 | getInventoryItemsData(sensorsAsyncResp, inventoryItems, |
| 1759 | invConnections, |
| 1760 | std::move(getInventoryItemsDataCb)); |
| 1761 | BMCWEB_LOG_DEBUG("getInventoryItemsConnectionsCb exit"); |
Shawn McCarney | 8fb49dd | 2019-06-12 17:47:00 -0500 | [diff] [blame] | 1762 | }; |
| 1763 | |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 1764 | // Get connections that provide inventory item data |
| 1765 | getInventoryItemsConnections( |
| 1766 | sensorsAsyncResp, inventoryItems, |
| 1767 | std::move(getInventoryItemsConnectionsCb)); |
| 1768 | BMCWEB_LOG_DEBUG("getInventoryItemAssociationsCb exit"); |
Shawn McCarney | 8fb49dd | 2019-06-12 17:47:00 -0500 | [diff] [blame] | 1769 | }; |
| 1770 | |
Shawn McCarney | adc4f0d | 2019-07-24 09:21:50 -0500 | [diff] [blame] | 1771 | // Get associations from sensors to inventory items |
Ed Tanous | d009073 | 2022-10-04 17:22:56 -0700 | [diff] [blame] | 1772 | getInventoryItemAssociations(sensorsAsyncResp, sensorNames, |
Shawn McCarney | adc4f0d | 2019-07-24 09:21:50 -0500 | [diff] [blame] | 1773 | std::move(getInventoryItemAssociationsCb)); |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 1774 | BMCWEB_LOG_DEBUG("getInventoryItems exit"); |
Shawn McCarney | adc4f0d | 2019-07-24 09:21:50 -0500 | [diff] [blame] | 1775 | } |
| 1776 | |
| 1777 | /** |
| 1778 | * @brief Returns JSON PowerSupply object for the specified inventory item. |
| 1779 | * |
| 1780 | * Searches for a JSON PowerSupply object that matches the specified inventory |
| 1781 | * item. If one is not found, a new PowerSupply object is added to the JSON |
| 1782 | * array. |
| 1783 | * |
| 1784 | * Multiple sensors are often associated with one power supply inventory item. |
| 1785 | * As a result, multiple sensor values are stored in one JSON PowerSupply |
| 1786 | * object. |
| 1787 | * |
| 1788 | * @param powerSupplyArray JSON array containing Redfish PowerSupply objects. |
| 1789 | * @param inventoryItem Inventory item for the power supply. |
| 1790 | * @param chassisId Chassis that contains the power supply. |
| 1791 | * @return JSON PowerSupply object for the specified inventory item. |
| 1792 | */ |
Ed Tanous | 23a21a1 | 2020-07-25 04:45:05 +0000 | [diff] [blame] | 1793 | inline nlohmann::json& getPowerSupply(nlohmann::json& powerSupplyArray, |
Shawn McCarney | adc4f0d | 2019-07-24 09:21:50 -0500 | [diff] [blame] | 1794 | const InventoryItem& inventoryItem, |
| 1795 | const std::string& chassisId) |
| 1796 | { |
Ed Tanous | 18f8f60 | 2023-07-18 10:07:23 -0700 | [diff] [blame] | 1797 | std::string nameS; |
Alexander Hansen | 6f4bd29 | 2024-03-08 17:04:54 +0100 | [diff] [blame] | 1798 | nameS.resize(inventoryItem.name.size()); |
Ed Tanous | 18f8f60 | 2023-07-18 10:07:23 -0700 | [diff] [blame] | 1799 | std::ranges::replace_copy(inventoryItem.name, nameS.begin(), '_', ' '); |
Shawn McCarney | adc4f0d | 2019-07-24 09:21:50 -0500 | [diff] [blame] | 1800 | // Check if matching PowerSupply object already exists in JSON array |
| 1801 | for (nlohmann::json& powerSupply : powerSupplyArray) |
| 1802 | { |
Ed Tanous | 18f8f60 | 2023-07-18 10:07:23 -0700 | [diff] [blame] | 1803 | nlohmann::json::iterator nameIt = powerSupply.find("Name"); |
| 1804 | if (nameIt == powerSupply.end()) |
| 1805 | { |
| 1806 | continue; |
| 1807 | } |
| 1808 | const std::string* name = nameIt->get_ptr<std::string*>(); |
| 1809 | if (name == nullptr) |
| 1810 | { |
| 1811 | continue; |
| 1812 | } |
| 1813 | if (nameS == *name) |
Shawn McCarney | adc4f0d | 2019-07-24 09:21:50 -0500 | [diff] [blame] | 1814 | { |
| 1815 | return powerSupply; |
| 1816 | } |
| 1817 | } |
| 1818 | |
| 1819 | // Add new PowerSupply object to JSON array |
| 1820 | powerSupplyArray.push_back({}); |
| 1821 | nlohmann::json& powerSupply = powerSupplyArray.back(); |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 1822 | boost::urls::url url = |
| 1823 | boost::urls::format("/redfish/v1/Chassis/{}/Power", chassisId); |
Willy Tu | eddfc43 | 2022-09-26 16:46:38 +0000 | [diff] [blame] | 1824 | url.set_fragment(("/PowerSupplies"_json_pointer).to_string()); |
| 1825 | powerSupply["@odata.id"] = std::move(url); |
Ed Tanous | 18f8f60 | 2023-07-18 10:07:23 -0700 | [diff] [blame] | 1826 | std::string escaped; |
Alexander Hansen | 6f4bd29 | 2024-03-08 17:04:54 +0100 | [diff] [blame] | 1827 | escaped.resize(inventoryItem.name.size()); |
Ed Tanous | 18f8f60 | 2023-07-18 10:07:23 -0700 | [diff] [blame] | 1828 | std::ranges::replace_copy(inventoryItem.name, escaped.begin(), '_', ' '); |
| 1829 | powerSupply["Name"] = std::move(escaped); |
Shawn McCarney | adc4f0d | 2019-07-24 09:21:50 -0500 | [diff] [blame] | 1830 | powerSupply["Manufacturer"] = inventoryItem.manufacturer; |
| 1831 | powerSupply["Model"] = inventoryItem.model; |
| 1832 | powerSupply["PartNumber"] = inventoryItem.partNumber; |
| 1833 | powerSupply["SerialNumber"] = inventoryItem.serialNumber; |
Janet Adkins | f664fd8 | 2025-07-23 14:01:43 -0500 | [diff] [blame] | 1834 | if constexpr (BMCWEB_REDFISH_ALLOW_DEPRECATED_INDICATORLED) |
| 1835 | { |
| 1836 | sensor_utils::setLedState(powerSupply, &inventoryItem); |
| 1837 | } |
Shawn McCarney | adc4f0d | 2019-07-24 09:21:50 -0500 | [diff] [blame] | 1838 | |
Gunnar Mills | 42cbe53 | 2019-08-15 15:26:54 -0500 | [diff] [blame] | 1839 | if (inventoryItem.powerSupplyEfficiencyPercent >= 0) |
| 1840 | { |
| 1841 | powerSupply["EfficiencyPercent"] = |
| 1842 | inventoryItem.powerSupplyEfficiencyPercent; |
| 1843 | } |
| 1844 | |
Janet Adkins | c956360 | 2024-08-28 11:37:46 -0500 | [diff] [blame] | 1845 | powerSupply["Status"]["State"] = |
| 1846 | sensor_utils::getState(&inventoryItem, true); |
Shawn McCarney | adc4f0d | 2019-07-24 09:21:50 -0500 | [diff] [blame] | 1847 | const char* health = inventoryItem.isFunctional ? "OK" : "Critical"; |
| 1848 | powerSupply["Status"]["Health"] = health; |
| 1849 | |
| 1850 | return powerSupply; |
Shawn McCarney | 8fb49dd | 2019-06-12 17:47:00 -0500 | [diff] [blame] | 1851 | } |
| 1852 | |
| 1853 | /** |
Shawn McCarney | de629b6 | 2019-03-08 10:42:51 -0600 | [diff] [blame] | 1854 | * @brief Gets the values of the specified sensors. |
| 1855 | * |
| 1856 | * Stores the results as JSON in the SensorsAsyncResp. |
| 1857 | * |
| 1858 | * Gets the sensor values asynchronously. Stores the results later when the |
| 1859 | * information has been obtained. |
| 1860 | * |
Shawn McCarney | adc4f0d | 2019-07-24 09:21:50 -0500 | [diff] [blame] | 1861 | * The sensorNames set contains all requested sensors for the current chassis. |
Shawn McCarney | de629b6 | 2019-03-08 10:42:51 -0600 | [diff] [blame] | 1862 | * |
| 1863 | * To minimize the number of DBus calls, the DBus method |
| 1864 | * org.freedesktop.DBus.ObjectManager.GetManagedObjects() is used to get the |
| 1865 | * values of all sensors provided by a connection (service). |
| 1866 | * |
| 1867 | * The connections set contains all the connections that provide sensor values. |
| 1868 | * |
Shawn McCarney | adc4f0d | 2019-07-24 09:21:50 -0500 | [diff] [blame] | 1869 | * The InventoryItem vector contains D-Bus inventory items associated with the |
| 1870 | * sensors. Inventory item data is needed for some Redfish sensor properties. |
| 1871 | * |
Shawn McCarney | de629b6 | 2019-03-08 10:42:51 -0600 | [diff] [blame] | 1872 | * @param SensorsAsyncResp Pointer to object holding response data. |
Shawn McCarney | adc4f0d | 2019-07-24 09:21:50 -0500 | [diff] [blame] | 1873 | * @param sensorNames All requested sensors within the current chassis. |
Shawn McCarney | de629b6 | 2019-03-08 10:42:51 -0600 | [diff] [blame] | 1874 | * @param connections Connections that provide sensor values. |
Shawn McCarney | de629b6 | 2019-03-08 10:42:51 -0600 | [diff] [blame] | 1875 | * implements ObjectManager. |
Shawn McCarney | adc4f0d | 2019-07-24 09:21:50 -0500 | [diff] [blame] | 1876 | * @param inventoryItems Inventory items associated with the sensors. |
Shawn McCarney | de629b6 | 2019-03-08 10:42:51 -0600 | [diff] [blame] | 1877 | */ |
Ed Tanous | 23a21a1 | 2020-07-25 04:45:05 +0000 | [diff] [blame] | 1878 | inline void getSensorData( |
Ed Tanous | 81ce609 | 2020-12-17 16:54:55 +0000 | [diff] [blame] | 1879 | const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp, |
Nan Zhou | fe04d49 | 2022-06-22 17:10:41 +0000 | [diff] [blame] | 1880 | const std::shared_ptr<std::set<std::string>>& sensorNames, |
| 1881 | const std::set<std::string>& connections, |
Ed Tanous | b5a7693 | 2020-09-29 16:16:58 -0700 | [diff] [blame] | 1882 | const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems) |
Shawn McCarney | de629b6 | 2019-03-08 10:42:51 -0600 | [diff] [blame] | 1883 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 1884 | BMCWEB_LOG_DEBUG("getSensorData enter"); |
Shawn McCarney | de629b6 | 2019-03-08 10:42:51 -0600 | [diff] [blame] | 1885 | // Get managed objects from all services exposing sensors |
| 1886 | for (const std::string& connection : connections) |
| 1887 | { |
George Liu | 5eb468d | 2023-06-20 17:03:24 +0800 | [diff] [blame] | 1888 | sdbusplus::message::object_path sensorPath( |
| 1889 | "/xyz/openbmc_project/sensors"); |
| 1890 | dbus::utility::getManagedObjects( |
| 1891 | connection, sensorPath, |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 1892 | [sensorsAsyncResp, sensorNames, |
Ed Tanous | 5e7e2dc | 2023-02-16 10:37:01 -0800 | [diff] [blame] | 1893 | inventoryItems](const boost::system::error_code& ec, |
Ed Tanous | 02cad96 | 2022-06-30 16:50:15 -0700 | [diff] [blame] | 1894 | const dbus::utility::ManagedObjectType& resp) { |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 1895 | BMCWEB_LOG_DEBUG("getManagedObjectsCb enter"); |
| 1896 | if (ec) |
Shawn McCarney | de629b6 | 2019-03-08 10:42:51 -0600 | [diff] [blame] | 1897 | { |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 1898 | BMCWEB_LOG_ERROR("getManagedObjectsCb DBUS error: {}", ec); |
| 1899 | messages::internalError(sensorsAsyncResp->asyncResp->res); |
| 1900 | return; |
| 1901 | } |
Janet Adkins | 0c728b4 | 2024-08-29 11:09:10 -0500 | [diff] [blame] | 1902 | auto chassisSubNode = sensor_utils::chassisSubNodeFromString( |
| 1903 | sensorsAsyncResp->chassisSubNode); |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 1904 | // Go through all objects and update response with sensor data |
| 1905 | for (const auto& objDictEntry : resp) |
| 1906 | { |
| 1907 | const std::string& objPath = |
| 1908 | static_cast<const std::string&>(objDictEntry.first); |
| 1909 | BMCWEB_LOG_DEBUG("getManagedObjectsCb parsing object {}", |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 1910 | objPath); |
Shawn McCarney | de629b6 | 2019-03-08 10:42:51 -0600 | [diff] [blame] | 1911 | |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 1912 | std::vector<std::string> split; |
| 1913 | // Reserve space for |
| 1914 | // /xyz/openbmc_project/sensors/<name>/<subname> |
| 1915 | split.reserve(6); |
| 1916 | // NOLINTNEXTLINE |
| 1917 | bmcweb::split(split, objPath, '/'); |
| 1918 | if (split.size() < 6) |
Nan Zhou | 928fefb | 2022-03-28 08:45:00 -0700 | [diff] [blame] | 1919 | { |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 1920 | BMCWEB_LOG_ERROR("Got path that isn't long enough {}", |
| 1921 | objPath); |
| 1922 | continue; |
Nan Zhou | 928fefb | 2022-03-28 08:45:00 -0700 | [diff] [blame] | 1923 | } |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 1924 | // These indexes aren't intuitive, as split puts an empty |
| 1925 | // string at the beginning |
| 1926 | const std::string& sensorType = split[4]; |
| 1927 | const std::string& sensorName = split[5]; |
| 1928 | BMCWEB_LOG_DEBUG("sensorName {} sensorType {}", sensorName, |
| 1929 | sensorType); |
Ed Tanous | 3d15864 | 2025-05-12 14:20:49 -0700 | [diff] [blame] | 1930 | if (!sensorNames->contains(objPath)) |
Shawn McCarney | adc4f0d | 2019-07-24 09:21:50 -0500 | [diff] [blame] | 1931 | { |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 1932 | BMCWEB_LOG_DEBUG("{} not in sensor list ", sensorName); |
Anthony Wilson | 95a3eca | 2019-06-11 10:44:47 -0500 | [diff] [blame] | 1933 | continue; |
| 1934 | } |
| 1935 | |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 1936 | // Find inventory item (if any) associated with sensor |
| 1937 | InventoryItem* inventoryItem = |
| 1938 | findInventoryItemForSensor(inventoryItems, objPath); |
| 1939 | |
| 1940 | const std::string& sensorSchema = |
| 1941 | sensorsAsyncResp->chassisSubNode; |
| 1942 | |
| 1943 | nlohmann::json* sensorJson = nullptr; |
| 1944 | |
Janet Adkins | 0c728b4 | 2024-08-29 11:09:10 -0500 | [diff] [blame] | 1945 | if (sensorSchema == sensors::sensorsNodeStr && |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 1946 | !sensorsAsyncResp->efficientExpand) |
Nan Zhou | 928fefb | 2022-03-28 08:45:00 -0700 | [diff] [blame] | 1947 | { |
Janet Adkins | 1516c21 | 2024-08-14 13:22:41 -0500 | [diff] [blame] | 1948 | std::string sensorId = |
| 1949 | redfish::sensor_utils::getSensorId(sensorName, |
| 1950 | sensorType); |
Ed Tanous | 677bb75 | 2022-09-15 10:52:19 -0700 | [diff] [blame] | 1951 | |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 1952 | sensorsAsyncResp->asyncResp->res |
| 1953 | .jsonValue["@odata.id"] = boost::urls::format( |
Ed Tanous | ef4c65b | 2023-04-24 15:28:50 -0700 | [diff] [blame] | 1954 | "/redfish/v1/Chassis/{}/{}/{}", |
Ed Tanous | 677bb75 | 2022-09-15 10:52:19 -0700 | [diff] [blame] | 1955 | sensorsAsyncResp->chassisId, |
| 1956 | sensorsAsyncResp->chassisSubNode, sensorId); |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 1957 | sensorJson = |
| 1958 | &(sensorsAsyncResp->asyncResp->res.jsonValue); |
Nan Zhou | 928fefb | 2022-03-28 08:45:00 -0700 | [diff] [blame] | 1959 | } |
Anthony Wilson | 95a3eca | 2019-06-11 10:44:47 -0500 | [diff] [blame] | 1960 | else |
| 1961 | { |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 1962 | std::string fieldName; |
| 1963 | if (sensorsAsyncResp->efficientExpand) |
| 1964 | { |
| 1965 | fieldName = "Members"; |
| 1966 | } |
| 1967 | else if (sensorType == "temperature") |
| 1968 | { |
| 1969 | fieldName = "Temperatures"; |
| 1970 | } |
| 1971 | else if (sensorType == "fan" || |
| 1972 | sensorType == "fan_tach" || |
| 1973 | sensorType == "fan_pwm") |
| 1974 | { |
| 1975 | fieldName = "Fans"; |
| 1976 | } |
| 1977 | else if (sensorType == "voltage") |
| 1978 | { |
| 1979 | fieldName = "Voltages"; |
| 1980 | } |
| 1981 | else if (sensorType == "power") |
| 1982 | { |
| 1983 | if (sensorName == "total_power") |
| 1984 | { |
| 1985 | fieldName = "PowerControl"; |
| 1986 | } |
| 1987 | else if ((inventoryItem != nullptr) && |
| 1988 | (inventoryItem->isPowerSupply)) |
| 1989 | { |
| 1990 | fieldName = "PowerSupplies"; |
| 1991 | } |
| 1992 | else |
| 1993 | { |
| 1994 | // Other power sensors are in SensorCollection |
| 1995 | continue; |
| 1996 | } |
| 1997 | } |
| 1998 | else |
| 1999 | { |
| 2000 | BMCWEB_LOG_ERROR( |
| 2001 | "Unsure how to handle sensorType {}", |
| 2002 | sensorType); |
| 2003 | continue; |
| 2004 | } |
| 2005 | |
| 2006 | nlohmann::json& tempArray = |
| 2007 | sensorsAsyncResp->asyncResp->res |
| 2008 | .jsonValue[fieldName]; |
| 2009 | if (fieldName == "PowerControl") |
| 2010 | { |
| 2011 | if (tempArray.empty()) |
| 2012 | { |
| 2013 | // Put multiple "sensors" into a single |
| 2014 | // PowerControl. Follows MemberId naming and |
| 2015 | // naming in power.hpp. |
| 2016 | nlohmann::json::object_t power; |
| 2017 | boost::urls::url url = boost::urls::format( |
| 2018 | "/redfish/v1/Chassis/{}/{}", |
| 2019 | sensorsAsyncResp->chassisId, |
| 2020 | sensorsAsyncResp->chassisSubNode); |
| 2021 | url.set_fragment( |
| 2022 | (""_json_pointer / fieldName / "0") |
| 2023 | .to_string()); |
| 2024 | power["@odata.id"] = std::move(url); |
| 2025 | tempArray.emplace_back(std::move(power)); |
| 2026 | } |
| 2027 | sensorJson = &(tempArray.back()); |
| 2028 | } |
| 2029 | else if (fieldName == "PowerSupplies") |
| 2030 | { |
| 2031 | if (inventoryItem != nullptr) |
| 2032 | { |
| 2033 | sensorJson = &(getPowerSupply( |
| 2034 | tempArray, *inventoryItem, |
| 2035 | sensorsAsyncResp->chassisId)); |
| 2036 | } |
| 2037 | } |
| 2038 | else if (fieldName == "Members") |
| 2039 | { |
Janet Adkins | 1516c21 | 2024-08-14 13:22:41 -0500 | [diff] [blame] | 2040 | std::string sensorId = |
| 2041 | redfish::sensor_utils::getSensorId(sensorName, |
| 2042 | sensorType); |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 2043 | |
| 2044 | nlohmann::json::object_t member; |
| 2045 | member["@odata.id"] = boost::urls::format( |
| 2046 | "/redfish/v1/Chassis/{}/{}/{}", |
| 2047 | sensorsAsyncResp->chassisId, |
| 2048 | sensorsAsyncResp->chassisSubNode, sensorId); |
| 2049 | tempArray.emplace_back(std::move(member)); |
| 2050 | sensorJson = &(tempArray.back()); |
| 2051 | } |
| 2052 | else |
| 2053 | { |
| 2054 | nlohmann::json::object_t member; |
| 2055 | boost::urls::url url = boost::urls::format( |
| 2056 | "/redfish/v1/Chassis/{}/{}", |
| 2057 | sensorsAsyncResp->chassisId, |
| 2058 | sensorsAsyncResp->chassisSubNode); |
| 2059 | url.set_fragment( |
| 2060 | (""_json_pointer / fieldName).to_string()); |
| 2061 | member["@odata.id"] = std::move(url); |
| 2062 | tempArray.emplace_back(std::move(member)); |
| 2063 | sensorJson = &(tempArray.back()); |
| 2064 | } |
| 2065 | } |
| 2066 | |
| 2067 | if (sensorJson != nullptr) |
| 2068 | { |
Janet Adkins | 0c728b4 | 2024-08-29 11:09:10 -0500 | [diff] [blame] | 2069 | objectInterfacesToJson( |
| 2070 | sensorName, sensorType, chassisSubNode, |
| 2071 | objDictEntry.second, *sensorJson, inventoryItem); |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 2072 | |
| 2073 | std::string path = "/xyz/openbmc_project/sensors/"; |
| 2074 | path += sensorType; |
| 2075 | path += "/"; |
| 2076 | path += sensorName; |
| 2077 | sensorsAsyncResp->addMetadata(*sensorJson, path); |
Shawn McCarney | adc4f0d | 2019-07-24 09:21:50 -0500 | [diff] [blame] | 2078 | } |
Johnathan Mantey | 49c53ac | 2019-05-02 09:22:38 -0700 | [diff] [blame] | 2079 | } |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 2080 | if (sensorsAsyncResp.use_count() == 1) |
Shawn McCarney | adc4f0d | 2019-07-24 09:21:50 -0500 | [diff] [blame] | 2081 | { |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 2082 | sortJSONResponse(sensorsAsyncResp); |
Janet Adkins | 0c728b4 | 2024-08-29 11:09:10 -0500 | [diff] [blame] | 2083 | if (chassisSubNode == |
| 2084 | sensor_utils::ChassisSubNode::sensorsNode && |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 2085 | sensorsAsyncResp->efficientExpand) |
| 2086 | { |
| 2087 | sensorsAsyncResp->asyncResp->res |
| 2088 | .jsonValue["Members@odata.count"] = |
| 2089 | sensorsAsyncResp->asyncResp->res |
| 2090 | .jsonValue["Members"] |
| 2091 | .size(); |
| 2092 | } |
Janet Adkins | 0c728b4 | 2024-08-29 11:09:10 -0500 | [diff] [blame] | 2093 | else if (chassisSubNode == |
| 2094 | sensor_utils::ChassisSubNode::thermalNode) |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 2095 | { |
| 2096 | populateFanRedundancy(sensorsAsyncResp); |
| 2097 | } |
Shawn McCarney | adc4f0d | 2019-07-24 09:21:50 -0500 | [diff] [blame] | 2098 | } |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 2099 | BMCWEB_LOG_DEBUG("getManagedObjectsCb exit"); |
| 2100 | }); |
Ed Tanous | 23a21a1 | 2020-07-25 04:45:05 +0000 | [diff] [blame] | 2101 | } |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 2102 | BMCWEB_LOG_DEBUG("getSensorData exit"); |
Shawn McCarney | de629b6 | 2019-03-08 10:42:51 -0600 | [diff] [blame] | 2103 | } |
| 2104 | |
Patrick Williams | 504af5a | 2025-02-03 14:29:03 -0500 | [diff] [blame] | 2105 | inline void processSensorList( |
| 2106 | const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp, |
| 2107 | const std::shared_ptr<std::set<std::string>>& sensorNames) |
Anthony Wilson | 95a3eca | 2019-06-11 10:44:47 -0500 | [diff] [blame] | 2108 | { |
Nan Zhou | fe04d49 | 2022-06-22 17:10:41 +0000 | [diff] [blame] | 2109 | auto getConnectionCb = [sensorsAsyncResp, sensorNames]( |
| 2110 | const std::set<std::string>& connections) { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 2111 | BMCWEB_LOG_DEBUG("getConnectionCb enter"); |
Ed Tanous | d009073 | 2022-10-04 17:22:56 -0700 | [diff] [blame] | 2112 | auto getInventoryItemsCb = |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 2113 | [sensorsAsyncResp, sensorNames, connections]( |
| 2114 | const std::shared_ptr<std::vector<InventoryItem>>& |
Ed Tanous | 4e0d878 | 2024-09-06 15:16:41 -0700 | [diff] [blame] | 2115 | inventoryItems) mutable { |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 2116 | BMCWEB_LOG_DEBUG("getInventoryItemsCb enter"); |
| 2117 | // Get sensor data and store results in JSON |
| 2118 | getSensorData(sensorsAsyncResp, sensorNames, connections, |
| 2119 | inventoryItems); |
| 2120 | BMCWEB_LOG_DEBUG("getInventoryItemsCb exit"); |
| 2121 | }; |
Anthony Wilson | 95a3eca | 2019-06-11 10:44:47 -0500 | [diff] [blame] | 2122 | |
Ed Tanous | d009073 | 2022-10-04 17:22:56 -0700 | [diff] [blame] | 2123 | // Get inventory items associated with sensors |
| 2124 | getInventoryItems(sensorsAsyncResp, sensorNames, |
| 2125 | std::move(getInventoryItemsCb)); |
| 2126 | |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 2127 | BMCWEB_LOG_DEBUG("getConnectionCb exit"); |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 2128 | }; |
| 2129 | |
Anthony Wilson | 95a3eca | 2019-06-11 10:44:47 -0500 | [diff] [blame] | 2130 | // Get set of connections that provide sensor values |
Ed Tanous | 81ce609 | 2020-12-17 16:54:55 +0000 | [diff] [blame] | 2131 | getConnections(sensorsAsyncResp, sensorNames, std::move(getConnectionCb)); |
Anthony Wilson | 95a3eca | 2019-06-11 10:44:47 -0500 | [diff] [blame] | 2132 | } |
| 2133 | |
Shawn McCarney | de629b6 | 2019-03-08 10:42:51 -0600 | [diff] [blame] | 2134 | /** |
Lewanczyk, Dawid | 08777fb | 2018-03-22 23:33:49 +0100 | [diff] [blame] | 2135 | * @brief Entry point for retrieving sensors data related to requested |
| 2136 | * chassis. |
Kowalski, Kamil | 588c3f0 | 2018-04-03 14:55:27 +0200 | [diff] [blame] | 2137 | * @param SensorsAsyncResp Pointer to object holding response data |
Lewanczyk, Dawid | 08777fb | 2018-03-22 23:33:49 +0100 | [diff] [blame] | 2138 | */ |
Patrick Williams | 504af5a | 2025-02-03 14:29:03 -0500 | [diff] [blame] | 2139 | inline void getChassisData( |
| 2140 | const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 2141 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 2142 | BMCWEB_LOG_DEBUG("getChassisData enter"); |
Johnathan Mantey | 49c53ac | 2019-05-02 09:22:38 -0700 | [diff] [blame] | 2143 | auto getChassisCb = |
Ed Tanous | 81ce609 | 2020-12-17 16:54:55 +0000 | [diff] [blame] | 2144 | [sensorsAsyncResp]( |
Nan Zhou | fe04d49 | 2022-06-22 17:10:41 +0000 | [diff] [blame] | 2145 | const std::shared_ptr<std::set<std::string>>& sensorNames) { |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 2146 | BMCWEB_LOG_DEBUG("getChassisCb enter"); |
| 2147 | processSensorList(sensorsAsyncResp, sensorNames); |
| 2148 | BMCWEB_LOG_DEBUG("getChassisCb exit"); |
| 2149 | }; |
Nan Zhou | 928fefb | 2022-03-28 08:45:00 -0700 | [diff] [blame] | 2150 | // SensorCollection doesn't contain the Redundancy property |
Janet Adkins | 0c728b4 | 2024-08-29 11:09:10 -0500 | [diff] [blame] | 2151 | if (sensorsAsyncResp->chassisSubNode != sensors::sensorsNodeStr) |
Nan Zhou | 928fefb | 2022-03-28 08:45:00 -0700 | [diff] [blame] | 2152 | { |
| 2153 | sensorsAsyncResp->asyncResp->res.jsonValue["Redundancy"] = |
| 2154 | nlohmann::json::array(); |
| 2155 | } |
Shawn McCarney | 26f0389 | 2019-05-03 13:20:24 -0500 | [diff] [blame] | 2156 | // Get set of sensors in chassis |
Ed Tanous | 7f1cc26 | 2022-08-09 13:33:57 -0700 | [diff] [blame] | 2157 | getChassis(sensorsAsyncResp->asyncResp, sensorsAsyncResp->chassisId, |
| 2158 | sensorsAsyncResp->chassisSubNode, sensorsAsyncResp->types, |
| 2159 | std::move(getChassisCb)); |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 2160 | BMCWEB_LOG_DEBUG("getChassisData exit"); |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 2161 | } |
Lewanczyk, Dawid | 08777fb | 2018-03-22 23:33:49 +0100 | [diff] [blame] | 2162 | |
Richard Marian Thomaiyar | 413961d | 2019-02-01 00:43:39 +0530 | [diff] [blame] | 2163 | /** |
Johnathan Mantey | 49c53ac | 2019-05-02 09:22:38 -0700 | [diff] [blame] | 2164 | * @brief Find the requested sensorName in the list of all sensors supplied by |
| 2165 | * the chassis node |
| 2166 | * |
| 2167 | * @param sensorName The sensor name supplied in the PATCH request |
| 2168 | * @param sensorsList The list of sensors managed by the chassis node |
| 2169 | * @param sensorsModified The list of sensors that were found as a result of |
| 2170 | * repeated calls to this function |
| 2171 | */ |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 2172 | inline bool findSensorNameUsingSensorPath( |
| 2173 | std::string_view sensorName, const std::set<std::string>& sensorsList, |
| 2174 | std::set<std::string>& sensorsModified) |
Johnathan Mantey | 49c53ac | 2019-05-02 09:22:38 -0700 | [diff] [blame] | 2175 | { |
Nan Zhou | fe04d49 | 2022-06-22 17:10:41 +0000 | [diff] [blame] | 2176 | for (const auto& chassisSensor : sensorsList) |
Johnathan Mantey | 49c53ac | 2019-05-02 09:22:38 -0700 | [diff] [blame] | 2177 | { |
George Liu | 28aa8de | 2021-02-01 15:13:30 +0800 | [diff] [blame] | 2178 | sdbusplus::message::object_path path(chassisSensor); |
Ed Tanous | b00dcc2 | 2021-02-23 12:52:50 -0800 | [diff] [blame] | 2179 | std::string thisSensorName = path.filename(); |
George Liu | 28aa8de | 2021-02-01 15:13:30 +0800 | [diff] [blame] | 2180 | if (thisSensorName.empty()) |
Johnathan Mantey | 49c53ac | 2019-05-02 09:22:38 -0700 | [diff] [blame] | 2181 | { |
Johnathan Mantey | 49c53ac | 2019-05-02 09:22:38 -0700 | [diff] [blame] | 2182 | continue; |
| 2183 | } |
| 2184 | if (thisSensorName == sensorName) |
| 2185 | { |
| 2186 | sensorsModified.emplace(chassisSensor); |
| 2187 | return true; |
| 2188 | } |
| 2189 | } |
| 2190 | return false; |
| 2191 | } |
| 2192 | |
| 2193 | /** |
Richard Marian Thomaiyar | 413961d | 2019-02-01 00:43:39 +0530 | [diff] [blame] | 2194 | * @brief Entry point for overriding sensor values of given sensor |
| 2195 | * |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 2196 | * @param sensorAsyncResp response object |
Carol Wang | 4bb3dc3 | 2019-10-17 18:15:02 +0800 | [diff] [blame] | 2197 | * @param allCollections Collections extract from sensors' request patch info |
jayaprakash Mutyala | 91e130a | 2020-03-04 22:26:38 +0000 | [diff] [blame] | 2198 | * @param chassisSubNode Chassis Node for which the query has to happen |
Richard Marian Thomaiyar | 413961d | 2019-02-01 00:43:39 +0530 | [diff] [blame] | 2199 | */ |
Ed Tanous | 23a21a1 | 2020-07-25 04:45:05 +0000 | [diff] [blame] | 2200 | inline void setSensorsOverride( |
Ed Tanous | b5a7693 | 2020-09-29 16:16:58 -0700 | [diff] [blame] | 2201 | const std::shared_ptr<SensorsAsyncResp>& sensorAsyncResp, |
Ed Tanous | 0885057 | 2024-03-06 15:09:17 -0800 | [diff] [blame] | 2202 | std::unordered_map<std::string, std::vector<nlohmann::json::object_t>>& |
jayaprakash Mutyala | 397fd61 | 2020-02-06 23:33:34 +0000 | [diff] [blame] | 2203 | allCollections) |
Richard Marian Thomaiyar | 413961d | 2019-02-01 00:43:39 +0530 | [diff] [blame] | 2204 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 2205 | BMCWEB_LOG_INFO("setSensorsOverride for subNode{}", |
| 2206 | sensorAsyncResp->chassisSubNode); |
Richard Marian Thomaiyar | 413961d | 2019-02-01 00:43:39 +0530 | [diff] [blame] | 2207 | |
Ed Tanous | d02aad3 | 2024-02-13 14:43:34 -0800 | [diff] [blame] | 2208 | std::string_view propertyValueName; |
Richard Marian Thomaiyar | f65af9e | 2019-02-13 23:35:05 +0530 | [diff] [blame] | 2209 | std::unordered_map<std::string, std::pair<double, std::string>> overrideMap; |
Richard Marian Thomaiyar | 413961d | 2019-02-01 00:43:39 +0530 | [diff] [blame] | 2210 | std::string memberId; |
Ed Tanous | 543f440 | 2022-01-06 13:12:53 -0800 | [diff] [blame] | 2211 | double value = 0.0; |
Richard Marian Thomaiyar | f65af9e | 2019-02-13 23:35:05 +0530 | [diff] [blame] | 2212 | for (auto& collectionItems : allCollections) |
Richard Marian Thomaiyar | 413961d | 2019-02-01 00:43:39 +0530 | [diff] [blame] | 2213 | { |
Richard Marian Thomaiyar | f65af9e | 2019-02-13 23:35:05 +0530 | [diff] [blame] | 2214 | if (collectionItems.first == "Temperatures") |
| 2215 | { |
| 2216 | propertyValueName = "ReadingCelsius"; |
| 2217 | } |
| 2218 | else if (collectionItems.first == "Fans") |
| 2219 | { |
| 2220 | propertyValueName = "Reading"; |
| 2221 | } |
| 2222 | else |
| 2223 | { |
| 2224 | propertyValueName = "ReadingVolts"; |
| 2225 | } |
| 2226 | for (auto& item : collectionItems.second) |
| 2227 | { |
Patrick Williams | 504af5a | 2025-02-03 14:29:03 -0500 | [diff] [blame] | 2228 | if (!json_util::readJsonObject( // |
Myung Bae | afc474a | 2024-10-09 00:53:29 -0700 | [diff] [blame] | 2229 | item, sensorAsyncResp->asyncResp->res, // |
Patrick Williams | 504af5a | 2025-02-03 14:29:03 -0500 | [diff] [blame] | 2230 | "MemberId", memberId, // |
| 2231 | propertyValueName, value // |
Myung Bae | afc474a | 2024-10-09 00:53:29 -0700 | [diff] [blame] | 2232 | )) |
Richard Marian Thomaiyar | f65af9e | 2019-02-13 23:35:05 +0530 | [diff] [blame] | 2233 | { |
| 2234 | return; |
| 2235 | } |
| 2236 | overrideMap.emplace(memberId, |
| 2237 | std::make_pair(value, collectionItems.first)); |
| 2238 | } |
Richard Marian Thomaiyar | 413961d | 2019-02-01 00:43:39 +0530 | [diff] [blame] | 2239 | } |
Carol Wang | 4bb3dc3 | 2019-10-17 18:15:02 +0800 | [diff] [blame] | 2240 | |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 2241 | auto getChassisSensorListCb = [sensorAsyncResp, overrideMap, |
| 2242 | propertyValueNameStr = |
| 2243 | std::string(propertyValueName)]( |
| 2244 | const std::shared_ptr< |
| 2245 | std::set<std::string>>& sensorsList) { |
Johnathan Mantey | 49c53ac | 2019-05-02 09:22:38 -0700 | [diff] [blame] | 2246 | // Match sensor names in the PATCH request to those managed by the |
| 2247 | // chassis node |
Nan Zhou | fe04d49 | 2022-06-22 17:10:41 +0000 | [diff] [blame] | 2248 | const std::shared_ptr<std::set<std::string>> sensorNames = |
| 2249 | std::make_shared<std::set<std::string>>(); |
Richard Marian Thomaiyar | f65af9e | 2019-02-13 23:35:05 +0530 | [diff] [blame] | 2250 | for (const auto& item : overrideMap) |
| 2251 | { |
| 2252 | const auto& sensor = item.first; |
Ed Tanous | c71d612 | 2022-11-29 14:10:32 -0800 | [diff] [blame] | 2253 | std::pair<std::string, std::string> sensorNameType = |
Janet Adkins | 1516c21 | 2024-08-14 13:22:41 -0500 | [diff] [blame] | 2254 | redfish::sensor_utils::splitSensorNameAndType(sensor); |
Ed Tanous | c71d612 | 2022-11-29 14:10:32 -0800 | [diff] [blame] | 2255 | if (!findSensorNameUsingSensorPath(sensorNameType.second, |
| 2256 | *sensorsList, *sensorNames)) |
Richard Marian Thomaiyar | 413961d | 2019-02-01 00:43:39 +0530 | [diff] [blame] | 2257 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 2258 | BMCWEB_LOG_INFO("Unable to find memberId {}", item.first); |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 2259 | messages::resourceNotFound(sensorAsyncResp->asyncResp->res, |
Richard Marian Thomaiyar | f65af9e | 2019-02-13 23:35:05 +0530 | [diff] [blame] | 2260 | item.second.second, item.first); |
Richard Marian Thomaiyar | 413961d | 2019-02-01 00:43:39 +0530 | [diff] [blame] | 2261 | return; |
| 2262 | } |
Richard Marian Thomaiyar | f65af9e | 2019-02-13 23:35:05 +0530 | [diff] [blame] | 2263 | } |
| 2264 | // Get the connection to which the memberId belongs |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 2265 | auto getObjectsWithConnectionCb = [sensorAsyncResp, overrideMap, |
| 2266 | propertyValueNameStr]( |
| 2267 | const std::set< |
| 2268 | std::string>& /*connections*/, |
| 2269 | const std::set<std::pair< |
| 2270 | std::string, std::string>>& |
| 2271 | objectsWithConnection) { |
Jayaprakash Mutyala | 4f277b5 | 2021-12-08 22:46:49 +0000 | [diff] [blame] | 2272 | if (objectsWithConnection.size() != overrideMap.size()) |
| 2273 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 2274 | BMCWEB_LOG_INFO( |
| 2275 | "Unable to find all objects with proper connection {} requested {}", |
| 2276 | objectsWithConnection.size(), overrideMap.size()); |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 2277 | messages::resourceNotFound( |
| 2278 | sensorAsyncResp->asyncResp->res, |
Janet Adkins | 0c728b4 | 2024-08-29 11:09:10 -0500 | [diff] [blame] | 2279 | sensorAsyncResp->chassisSubNode == sensors::thermalNodeStr |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 2280 | ? "Temperatures" |
| 2281 | : "Voltages", |
| 2282 | "Count"); |
Jayaprakash Mutyala | 4f277b5 | 2021-12-08 22:46:49 +0000 | [diff] [blame] | 2283 | return; |
| 2284 | } |
| 2285 | for (const auto& item : objectsWithConnection) |
| 2286 | { |
| 2287 | sdbusplus::message::object_path path(item.first); |
| 2288 | std::string sensorName = path.filename(); |
| 2289 | if (sensorName.empty()) |
Richard Marian Thomaiyar | f65af9e | 2019-02-13 23:35:05 +0530 | [diff] [blame] | 2290 | { |
Jayaprakash Mutyala | 4f277b5 | 2021-12-08 22:46:49 +0000 | [diff] [blame] | 2291 | messages::internalError(sensorAsyncResp->asyncResp->res); |
Richard Marian Thomaiyar | f65af9e | 2019-02-13 23:35:05 +0530 | [diff] [blame] | 2292 | return; |
| 2293 | } |
Janet Adkins | 1516c21 | 2024-08-14 13:22:41 -0500 | [diff] [blame] | 2294 | std::string id = redfish::sensor_utils::getSensorId( |
| 2295 | sensorName, path.parent_path().filename()); |
Richard Marian Thomaiyar | f65af9e | 2019-02-13 23:35:05 +0530 | [diff] [blame] | 2296 | |
Ban Feng | 3f5eb75 | 2023-06-29 09:19:20 +0800 | [diff] [blame] | 2297 | const auto& iterator = overrideMap.find(id); |
Jayaprakash Mutyala | 4f277b5 | 2021-12-08 22:46:49 +0000 | [diff] [blame] | 2298 | if (iterator == overrideMap.end()) |
| 2299 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 2300 | BMCWEB_LOG_INFO("Unable to find sensor object{}", |
| 2301 | item.first); |
Jayaprakash Mutyala | 4f277b5 | 2021-12-08 22:46:49 +0000 | [diff] [blame] | 2302 | messages::internalError(sensorAsyncResp->asyncResp->res); |
| 2303 | return; |
| 2304 | } |
Ginu George | e93abac | 2024-06-14 17:35:27 +0530 | [diff] [blame] | 2305 | setDbusProperty(sensorAsyncResp->asyncResp, |
| 2306 | propertyValueNameStr, item.second, item.first, |
| 2307 | "xyz.openbmc_project.Sensor.Value", "Value", |
Ed Tanous | d02aad3 | 2024-02-13 14:43:34 -0800 | [diff] [blame] | 2308 | iterator->second.first); |
Jayaprakash Mutyala | 4f277b5 | 2021-12-08 22:46:49 +0000 | [diff] [blame] | 2309 | } |
| 2310 | }; |
Richard Marian Thomaiyar | f65af9e | 2019-02-13 23:35:05 +0530 | [diff] [blame] | 2311 | // Get object with connection for the given sensor name |
| 2312 | getObjectsWithConnection(sensorAsyncResp, sensorNames, |
| 2313 | std::move(getObjectsWithConnectionCb)); |
| 2314 | }; |
Richard Marian Thomaiyar | 413961d | 2019-02-01 00:43:39 +0530 | [diff] [blame] | 2315 | // get full sensor list for the given chassisId and cross verify the sensor. |
Ed Tanous | 7f1cc26 | 2022-08-09 13:33:57 -0700 | [diff] [blame] | 2316 | getChassis(sensorAsyncResp->asyncResp, sensorAsyncResp->chassisId, |
| 2317 | sensorAsyncResp->chassisSubNode, sensorAsyncResp->types, |
| 2318 | std::move(getChassisSensorListCb)); |
Richard Marian Thomaiyar | 413961d | 2019-02-01 00:43:39 +0530 | [diff] [blame] | 2319 | } |
| 2320 | |
Adrian Ambrożewicz | a0ec28b | 2020-04-10 14:47:28 +0200 | [diff] [blame] | 2321 | /** |
| 2322 | * @brief Retrieves mapping of Redfish URIs to sensor value property to D-Bus |
| 2323 | * path of the sensor. |
| 2324 | * |
| 2325 | * Function builds valid Redfish response for sensor query of given chassis and |
| 2326 | * node. It then builds metadata about Redfish<->D-Bus correlations and provides |
| 2327 | * it to caller in a callback. |
| 2328 | * |
| 2329 | * @param chassis Chassis for which retrieval should be performed |
Janet Adkins | c956360 | 2024-08-28 11:37:46 -0500 | [diff] [blame] | 2330 | * @param node Node (group) of sensors. See sensor_utils::node for supported |
| 2331 | * values |
Adrian Ambrożewicz | a0ec28b | 2020-04-10 14:47:28 +0200 | [diff] [blame] | 2332 | * @param mapComplete Callback to be called with retrieval result |
| 2333 | */ |
Ed Tanous | 931edc7 | 2023-11-01 12:09:07 -0700 | [diff] [blame] | 2334 | template <typename Callback> |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 2335 | inline void retrieveUriToDbusMap( |
| 2336 | const std::string& chassis, const std::string& node, Callback&& mapComplete) |
Adrian Ambrożewicz | a0ec28b | 2020-04-10 14:47:28 +0200 | [diff] [blame] | 2337 | { |
Ed Tanous | 02da7c5 | 2022-02-27 00:09:02 -0800 | [diff] [blame] | 2338 | decltype(sensors::paths)::const_iterator pathIt = |
| 2339 | std::find_if(sensors::paths.cbegin(), sensors::paths.cend(), |
| 2340 | [&node](auto&& val) { return val.first == node; }); |
| 2341 | if (pathIt == sensors::paths.cend()) |
Adrian Ambrożewicz | a0ec28b | 2020-04-10 14:47:28 +0200 | [diff] [blame] | 2342 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 2343 | BMCWEB_LOG_ERROR("Wrong node provided : {}", node); |
Ed Tanous | 6804b5c | 2023-10-31 14:50:03 -0700 | [diff] [blame] | 2344 | std::map<std::string, std::string> noop; |
| 2345 | mapComplete(boost::beast::http::status::bad_request, noop); |
Adrian Ambrożewicz | a0ec28b | 2020-04-10 14:47:28 +0200 | [diff] [blame] | 2346 | return; |
| 2347 | } |
Krzysztof Grobelny | d51e072 | 2021-04-16 13:15:21 +0000 | [diff] [blame] | 2348 | |
Nan Zhou | 72374eb | 2022-01-27 17:06:51 -0800 | [diff] [blame] | 2349 | auto asyncResp = std::make_shared<bmcweb::AsyncResp>(); |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 2350 | auto callback = |
| 2351 | [asyncResp, mapCompleteCb = std::forward<Callback>(mapComplete)]( |
| 2352 | const boost::beast::http::status status, |
| 2353 | const std::map<std::string, std::string>& uriToDbus) { |
| 2354 | mapCompleteCb(status, uriToDbus); |
| 2355 | }; |
Adrian Ambrożewicz | a0ec28b | 2020-04-10 14:47:28 +0200 | [diff] [blame] | 2356 | |
| 2357 | auto resp = std::make_shared<SensorsAsyncResp>( |
Krzysztof Grobelny | d51e072 | 2021-04-16 13:15:21 +0000 | [diff] [blame] | 2358 | asyncResp, chassis, pathIt->second, node, std::move(callback)); |
Adrian Ambrożewicz | a0ec28b | 2020-04-10 14:47:28 +0200 | [diff] [blame] | 2359 | getChassisData(resp); |
| 2360 | } |
| 2361 | |
Nan Zhou | bacb216 | 2022-04-06 11:28:32 -0700 | [diff] [blame] | 2362 | namespace sensors |
| 2363 | { |
Nan Zhou | 928fefb | 2022-03-28 08:45:00 -0700 | [diff] [blame] | 2364 | |
Nan Zhou | bacb216 | 2022-04-06 11:28:32 -0700 | [diff] [blame] | 2365 | inline void getChassisCallback( |
Ed Tanous | c1d019a | 2022-08-06 09:36:06 -0700 | [diff] [blame] | 2366 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, |
| 2367 | std::string_view chassisId, std::string_view chassisSubNode, |
Nan Zhou | fe04d49 | 2022-06-22 17:10:41 +0000 | [diff] [blame] | 2368 | const std::shared_ptr<std::set<std::string>>& sensorNames) |
Nan Zhou | bacb216 | 2022-04-06 11:28:32 -0700 | [diff] [blame] | 2369 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 2370 | BMCWEB_LOG_DEBUG("getChassisCallback enter "); |
Nan Zhou | bacb216 | 2022-04-06 11:28:32 -0700 | [diff] [blame] | 2371 | |
Ed Tanous | c1d019a | 2022-08-06 09:36:06 -0700 | [diff] [blame] | 2372 | nlohmann::json& entriesArray = asyncResp->res.jsonValue["Members"]; |
| 2373 | for (const std::string& sensor : *sensorNames) |
Nan Zhou | bacb216 | 2022-04-06 11:28:32 -0700 | [diff] [blame] | 2374 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 2375 | BMCWEB_LOG_DEBUG("Adding sensor: {}", sensor); |
Nan Zhou | bacb216 | 2022-04-06 11:28:32 -0700 | [diff] [blame] | 2376 | |
| 2377 | sdbusplus::message::object_path path(sensor); |
| 2378 | std::string sensorName = path.filename(); |
| 2379 | if (sensorName.empty()) |
| 2380 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 2381 | BMCWEB_LOG_ERROR("Invalid sensor path: {}", sensor); |
Ed Tanous | c1d019a | 2022-08-06 09:36:06 -0700 | [diff] [blame] | 2382 | messages::internalError(asyncResp->res); |
Nan Zhou | bacb216 | 2022-04-06 11:28:32 -0700 | [diff] [blame] | 2383 | return; |
| 2384 | } |
Ed Tanous | c1d019a | 2022-08-06 09:36:06 -0700 | [diff] [blame] | 2385 | std::string type = path.parent_path().filename(); |
Janet Adkins | 1516c21 | 2024-08-14 13:22:41 -0500 | [diff] [blame] | 2386 | std::string id = redfish::sensor_utils::getSensorId(sensorName, type); |
Ed Tanous | c1d019a | 2022-08-06 09:36:06 -0700 | [diff] [blame] | 2387 | |
Ed Tanous | 1476687 | 2022-03-15 10:44:42 -0700 | [diff] [blame] | 2388 | nlohmann::json::object_t member; |
Ed Tanous | ef4c65b | 2023-04-24 15:28:50 -0700 | [diff] [blame] | 2389 | member["@odata.id"] = boost::urls::format( |
| 2390 | "/redfish/v1/Chassis/{}/{}/{}", chassisId, chassisSubNode, id); |
Ed Tanous | c1d019a | 2022-08-06 09:36:06 -0700 | [diff] [blame] | 2391 | |
Patrick Williams | b2ba307 | 2023-05-12 10:27:39 -0500 | [diff] [blame] | 2392 | entriesArray.emplace_back(std::move(member)); |
Nan Zhou | bacb216 | 2022-04-06 11:28:32 -0700 | [diff] [blame] | 2393 | } |
| 2394 | |
Ed Tanous | c1d019a | 2022-08-06 09:36:06 -0700 | [diff] [blame] | 2395 | asyncResp->res.jsonValue["Members@odata.count"] = entriesArray.size(); |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 2396 | BMCWEB_LOG_DEBUG("getChassisCallback exit"); |
Nan Zhou | bacb216 | 2022-04-06 11:28:32 -0700 | [diff] [blame] | 2397 | } |
Nan Zhou | e6bd846 | 2022-06-01 04:35:35 +0000 | [diff] [blame] | 2398 | |
Ed Tanous | ac106bf | 2023-06-07 09:24:59 -0700 | [diff] [blame] | 2399 | inline void handleSensorCollectionGet( |
| 2400 | App& app, const crow::Request& req, |
| 2401 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, |
| 2402 | const std::string& chassisId) |
Nan Zhou | de167a6 | 2022-06-01 04:47:45 +0000 | [diff] [blame] | 2403 | { |
| 2404 | query_param::QueryCapabilities capabilities = { |
| 2405 | .canDelegateExpandLevel = 1, |
| 2406 | }; |
| 2407 | query_param::Query delegatedQuery; |
Ed Tanous | ac106bf | 2023-06-07 09:24:59 -0700 | [diff] [blame] | 2408 | if (!redfish::setUpRedfishRouteWithDelegation(app, req, asyncResp, |
Nan Zhou | de167a6 | 2022-06-01 04:47:45 +0000 | [diff] [blame] | 2409 | delegatedQuery, capabilities)) |
| 2410 | { |
| 2411 | return; |
| 2412 | } |
| 2413 | |
| 2414 | if (delegatedQuery.expandType != query_param::ExpandType::None) |
| 2415 | { |
| 2416 | // we perform efficient expand. |
Ed Tanous | ac106bf | 2023-06-07 09:24:59 -0700 | [diff] [blame] | 2417 | auto sensorsAsyncResp = std::make_shared<SensorsAsyncResp>( |
| 2418 | asyncResp, chassisId, sensors::dbus::sensorPaths, |
Janet Adkins | 0c728b4 | 2024-08-29 11:09:10 -0500 | [diff] [blame] | 2419 | sensors::sensorsNodeStr, |
Nan Zhou | de167a6 | 2022-06-01 04:47:45 +0000 | [diff] [blame] | 2420 | /*efficientExpand=*/true); |
Ed Tanous | ac106bf | 2023-06-07 09:24:59 -0700 | [diff] [blame] | 2421 | getChassisData(sensorsAsyncResp); |
Nan Zhou | de167a6 | 2022-06-01 04:47:45 +0000 | [diff] [blame] | 2422 | |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 2423 | BMCWEB_LOG_DEBUG( |
| 2424 | "SensorCollection doGet exit via efficient expand handler"); |
Nan Zhou | de167a6 | 2022-06-01 04:47:45 +0000 | [diff] [blame] | 2425 | return; |
Ed Tanous | 0bad320 | 2022-06-02 13:53:59 -0700 | [diff] [blame] | 2426 | } |
Nan Zhou | de167a6 | 2022-06-01 04:47:45 +0000 | [diff] [blame] | 2427 | |
Nan Zhou | de167a6 | 2022-06-01 04:47:45 +0000 | [diff] [blame] | 2428 | // We get all sensors as hyperlinkes in the chassis (this |
| 2429 | // implies we reply on the default query parameters handler) |
Janet Adkins | 0c728b4 | 2024-08-29 11:09:10 -0500 | [diff] [blame] | 2430 | getChassis(asyncResp, chassisId, sensors::sensorsNodeStr, dbus::sensorPaths, |
Ed Tanous | ac106bf | 2023-06-07 09:24:59 -0700 | [diff] [blame] | 2431 | std::bind_front(sensors::getChassisCallback, asyncResp, |
Janet Adkins | 0c728b4 | 2024-08-29 11:09:10 -0500 | [diff] [blame] | 2432 | chassisId, sensors::sensorsNodeStr)); |
Ed Tanous | c1d019a | 2022-08-06 09:36:06 -0700 | [diff] [blame] | 2433 | } |
Ed Tanous | 7f1cc26 | 2022-08-09 13:33:57 -0700 | [diff] [blame] | 2434 | |
Patrick Williams | 504af5a | 2025-02-03 14:29:03 -0500 | [diff] [blame] | 2435 | inline void getSensorFromDbus( |
| 2436 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, |
| 2437 | const std::string& sensorPath, |
| 2438 | const ::dbus::utility::MapperGetObject& mapperResponse) |
Ed Tanous | c1d019a | 2022-08-06 09:36:06 -0700 | [diff] [blame] | 2439 | { |
| 2440 | if (mapperResponse.size() != 1) |
| 2441 | { |
| 2442 | messages::internalError(asyncResp->res); |
| 2443 | return; |
| 2444 | } |
| 2445 | const auto& valueIface = *mapperResponse.begin(); |
| 2446 | const std::string& connectionName = valueIface.first; |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 2447 | BMCWEB_LOG_DEBUG("Looking up {}", connectionName); |
| 2448 | BMCWEB_LOG_DEBUG("Path {}", sensorPath); |
Krzysztof Grobelny | c1343bf | 2022-08-31 13:15:26 +0200 | [diff] [blame] | 2449 | |
Ed Tanous | deae6a7 | 2024-11-11 21:58:57 -0800 | [diff] [blame] | 2450 | ::dbus::utility::getAllProperties( |
Krzysztof Grobelny | c1343bf | 2022-08-31 13:15:26 +0200 | [diff] [blame] | 2451 | *crow::connections::systemBus, connectionName, sensorPath, "", |
Ed Tanous | c1d019a | 2022-08-06 09:36:06 -0700 | [diff] [blame] | 2452 | [asyncResp, |
Ed Tanous | 5e7e2dc | 2023-02-16 10:37:01 -0800 | [diff] [blame] | 2453 | sensorPath](const boost::system::error_code& ec, |
Ed Tanous | c1d019a | 2022-08-06 09:36:06 -0700 | [diff] [blame] | 2454 | const ::dbus::utility::DBusPropertiesMap& valuesDict) { |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 2455 | if (ec) |
| 2456 | { |
| 2457 | messages::internalError(asyncResp->res); |
| 2458 | return; |
| 2459 | } |
| 2460 | sdbusplus::message::object_path path(sensorPath); |
| 2461 | std::string name = path.filename(); |
| 2462 | path = path.parent_path(); |
| 2463 | std::string type = path.filename(); |
Janet Adkins | c956360 | 2024-08-28 11:37:46 -0500 | [diff] [blame] | 2464 | sensor_utils::objectPropertiesToJson( |
Janet Adkins | 0c728b4 | 2024-08-29 11:09:10 -0500 | [diff] [blame] | 2465 | name, type, sensor_utils::ChassisSubNode::sensorsNode, |
| 2466 | valuesDict, asyncResp->res.jsonValue, nullptr); |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 2467 | }); |
Nan Zhou | de167a6 | 2022-06-01 04:47:45 +0000 | [diff] [blame] | 2468 | } |
| 2469 | |
Nan Zhou | e6bd846 | 2022-06-01 04:35:35 +0000 | [diff] [blame] | 2470 | inline void handleSensorGet(App& app, const crow::Request& req, |
Ed Tanous | c1d019a | 2022-08-06 09:36:06 -0700 | [diff] [blame] | 2471 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, |
Ed Tanous | 677bb75 | 2022-09-15 10:52:19 -0700 | [diff] [blame] | 2472 | const std::string& chassisId, |
Ed Tanous | c1d019a | 2022-08-06 09:36:06 -0700 | [diff] [blame] | 2473 | const std::string& sensorId) |
Nan Zhou | e6bd846 | 2022-06-01 04:35:35 +0000 | [diff] [blame] | 2474 | { |
Ed Tanous | c1d019a | 2022-08-06 09:36:06 -0700 | [diff] [blame] | 2475 | if (!redfish::setUpRedfishRoute(app, req, asyncResp)) |
Nan Zhou | e6bd846 | 2022-06-01 04:35:35 +0000 | [diff] [blame] | 2476 | { |
| 2477 | return; |
| 2478 | } |
Ed Tanous | c71d612 | 2022-11-29 14:10:32 -0800 | [diff] [blame] | 2479 | std::pair<std::string, std::string> nameType = |
Janet Adkins | 1516c21 | 2024-08-14 13:22:41 -0500 | [diff] [blame] | 2480 | redfish::sensor_utils::splitSensorNameAndType(sensorId); |
Ed Tanous | c71d612 | 2022-11-29 14:10:32 -0800 | [diff] [blame] | 2481 | if (nameType.first.empty() || nameType.second.empty()) |
Ed Tanous | c1d019a | 2022-08-06 09:36:06 -0700 | [diff] [blame] | 2482 | { |
| 2483 | messages::resourceNotFound(asyncResp->res, sensorId, "Sensor"); |
| 2484 | return; |
| 2485 | } |
Ed Tanous | c71d612 | 2022-11-29 14:10:32 -0800 | [diff] [blame] | 2486 | |
Ed Tanous | ef4c65b | 2023-04-24 15:28:50 -0700 | [diff] [blame] | 2487 | asyncResp->res.jsonValue["@odata.id"] = boost::urls::format( |
| 2488 | "/redfish/v1/Chassis/{}/Sensors/{}", chassisId, sensorId); |
Ed Tanous | c1d019a | 2022-08-06 09:36:06 -0700 | [diff] [blame] | 2489 | |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 2490 | BMCWEB_LOG_DEBUG("Sensor doGet enter"); |
Nan Zhou | e6bd846 | 2022-06-01 04:35:35 +0000 | [diff] [blame] | 2491 | |
George Liu | 2b73119 | 2023-01-11 16:27:13 +0800 | [diff] [blame] | 2492 | constexpr std::array<std::string_view, 1> interfaces = { |
Nan Zhou | e6bd846 | 2022-06-01 04:35:35 +0000 | [diff] [blame] | 2493 | "xyz.openbmc_project.Sensor.Value"}; |
Ed Tanous | c71d612 | 2022-11-29 14:10:32 -0800 | [diff] [blame] | 2494 | std::string sensorPath = "/xyz/openbmc_project/sensors/" + nameType.first + |
| 2495 | '/' + nameType.second; |
Nan Zhou | e6bd846 | 2022-06-01 04:35:35 +0000 | [diff] [blame] | 2496 | // Get a list of all of the sensors that implement Sensor.Value |
| 2497 | // and get the path and service name associated with the sensor |
George Liu | 2b73119 | 2023-01-11 16:27:13 +0800 | [diff] [blame] | 2498 | ::dbus::utility::getDbusObject( |
| 2499 | sensorPath, interfaces, |
Myung Bae | aec0ec3 | 2023-05-31 15:10:01 -0500 | [diff] [blame] | 2500 | [asyncResp, sensorId, |
George Liu | 2b73119 | 2023-01-11 16:27:13 +0800 | [diff] [blame] | 2501 | sensorPath](const boost::system::error_code& ec, |
Ed Tanous | c1d019a | 2022-08-06 09:36:06 -0700 | [diff] [blame] | 2502 | const ::dbus::utility::MapperGetObject& subtree) { |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 2503 | BMCWEB_LOG_DEBUG("respHandler1 enter"); |
| 2504 | if (ec == boost::system::errc::io_error) |
| 2505 | { |
| 2506 | BMCWEB_LOG_WARNING("Sensor not found from getSensorPaths"); |
| 2507 | messages::resourceNotFound(asyncResp->res, sensorId, "Sensor"); |
| 2508 | return; |
| 2509 | } |
| 2510 | if (ec) |
| 2511 | { |
| 2512 | messages::internalError(asyncResp->res); |
| 2513 | BMCWEB_LOG_ERROR( |
| 2514 | "Sensor getSensorPaths resp_handler: Dbus error {}", ec); |
| 2515 | return; |
| 2516 | } |
| 2517 | getSensorFromDbus(asyncResp, sensorPath, subtree); |
| 2518 | BMCWEB_LOG_DEBUG("respHandler1 exit"); |
| 2519 | }); |
Nan Zhou | e6bd846 | 2022-06-01 04:35:35 +0000 | [diff] [blame] | 2520 | } |
| 2521 | |
Nan Zhou | bacb216 | 2022-04-06 11:28:32 -0700 | [diff] [blame] | 2522 | } // namespace sensors |
| 2523 | |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 2524 | inline void requestRoutesSensorCollection(App& app) |
Anthony Wilson | 95a3eca | 2019-06-11 10:44:47 -0500 | [diff] [blame] | 2525 | { |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 2526 | BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/Sensors/") |
Ed Tanous | ed39821 | 2021-06-09 17:05:54 -0700 | [diff] [blame] | 2527 | .privileges(redfish::privileges::getSensorCollection) |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 2528 | .methods(boost::beast::http::verb::get)( |
Nan Zhou | de167a6 | 2022-06-01 04:47:45 +0000 | [diff] [blame] | 2529 | std::bind_front(sensors::handleSensorCollectionGet, std::ref(app))); |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 2530 | } |
Anthony Wilson | 95a3eca | 2019-06-11 10:44:47 -0500 | [diff] [blame] | 2531 | |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 2532 | inline void requestRoutesSensor(App& app) |
| 2533 | { |
| 2534 | BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/Sensors/<str>/") |
Ed Tanous | ed39821 | 2021-06-09 17:05:54 -0700 | [diff] [blame] | 2535 | .privileges(redfish::privileges::getSensor) |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 2536 | .methods(boost::beast::http::verb::get)( |
Nan Zhou | e6bd846 | 2022-06-01 04:35:35 +0000 | [diff] [blame] | 2537 | std::bind_front(sensors::handleSensorGet, std::ref(app))); |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 2538 | } |
Anthony Wilson | 95a3eca | 2019-06-11 10:44:47 -0500 | [diff] [blame] | 2539 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 2540 | } // namespace redfish |