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