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