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