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