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