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