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