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