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