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