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