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 | |
| 18 | #include <math.h> |
| 19 | #include <dbus_singleton.hpp> |
| 20 | #include <boost/algorithm/string/predicate.hpp> |
| 21 | #include <boost/algorithm/string/split.hpp> |
| 22 | #include <boost/container/flat_map.hpp> |
| 23 | #include <boost/range/algorithm/replace_copy_if.hpp> |
Lewanczyk, Dawid | 08777fb | 2018-03-22 23:33:49 +0100 | [diff] [blame] | 24 | |
| 25 | namespace redfish { |
| 26 | |
James Feist | 03b5bae | 2018-07-06 12:08:09 -0700 | [diff] [blame] | 27 | constexpr const char* DBUS_SENSOR_PREFIX = "/xyz/openbmc_project/sensors/"; |
Lewanczyk, Dawid | 08777fb | 2018-03-22 23:33:49 +0100 | [diff] [blame] | 28 | |
| 29 | using GetSubTreeType = std::vector< |
| 30 | std::pair<std::string, |
| 31 | std::vector<std::pair<std::string, std::vector<std::string>>>>>; |
| 32 | |
Ed Tanous | aa2e59c | 2018-04-12 12:17:20 -0700 | [diff] [blame] | 33 | using SensorVariant = sdbusplus::message::variant<int64_t, double>; |
| 34 | |
Lewanczyk, Dawid | 08777fb | 2018-03-22 23:33:49 +0100 | [diff] [blame] | 35 | using ManagedObjectsVectorType = std::vector<std::pair< |
Ed Tanous | aa2e59c | 2018-04-12 12:17:20 -0700 | [diff] [blame] | 36 | sdbusplus::message::object_path, |
Lewanczyk, Dawid | 08777fb | 2018-03-22 23:33:49 +0100 | [diff] [blame] | 37 | boost::container::flat_map< |
Ed Tanous | aa2e59c | 2018-04-12 12:17:20 -0700 | [diff] [blame] | 38 | std::string, boost::container::flat_map<std::string, SensorVariant>>>>; |
Lewanczyk, Dawid | 08777fb | 2018-03-22 23:33:49 +0100 | [diff] [blame] | 39 | |
| 40 | /** |
Kowalski, Kamil | 588c3f0 | 2018-04-03 14:55:27 +0200 | [diff] [blame] | 41 | * SensorsAsyncResp |
Lewanczyk, Dawid | 08777fb | 2018-03-22 23:33:49 +0100 | [diff] [blame] | 42 | * Gathers data needed for response processing after async calls are done |
| 43 | */ |
Kowalski, Kamil | 588c3f0 | 2018-04-03 14:55:27 +0200 | [diff] [blame] | 44 | class SensorsAsyncResp { |
Lewanczyk, Dawid | 08777fb | 2018-03-22 23:33:49 +0100 | [diff] [blame] | 45 | public: |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 46 | SensorsAsyncResp(crow::Response& response, const std::string& chassisId, |
Kowalski, Kamil | 588c3f0 | 2018-04-03 14:55:27 +0200 | [diff] [blame] | 47 | const std::initializer_list<const char*> types) |
| 48 | : res(response), chassisId(chassisId), types(types) { |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 49 | res.jsonValue["@odata.id"] = |
Lewanczyk, Dawid | 08777fb | 2018-03-22 23:33:49 +0100 | [diff] [blame] | 50 | "/redfish/v1/Chassis/" + chassisId + "/Thermal"; |
| 51 | } |
| 52 | |
Kowalski, Kamil | 588c3f0 | 2018-04-03 14:55:27 +0200 | [diff] [blame] | 53 | ~SensorsAsyncResp() { |
Ed Tanous | e0d918b | 2018-03-27 17:41:04 -0700 | [diff] [blame] | 54 | if (res.result() == boost::beast::http::status::internal_server_error) { |
Lewanczyk, Dawid | 08777fb | 2018-03-22 23:33:49 +0100 | [diff] [blame] | 55 | // Reset the json object to clear out any data that made it in before the |
| 56 | // error happened |
| 57 | // todo(ed) handle error condition with proper code |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 58 | res.jsonValue = nlohmann::json::object(); |
Lewanczyk, Dawid | 08777fb | 2018-03-22 23:33:49 +0100 | [diff] [blame] | 59 | } |
| 60 | res.end(); |
| 61 | } |
Kowalski, Kamil | 588c3f0 | 2018-04-03 14:55:27 +0200 | [diff] [blame] | 62 | |
Lewanczyk, Dawid | 08777fb | 2018-03-22 23:33:49 +0100 | [diff] [blame] | 63 | void setErrorStatus() { |
Ed Tanous | e0d918b | 2018-03-27 17:41:04 -0700 | [diff] [blame] | 64 | res.result(boost::beast::http::status::internal_server_error); |
Lewanczyk, Dawid | 08777fb | 2018-03-22 23:33:49 +0100 | [diff] [blame] | 65 | } |
| 66 | |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 67 | crow::Response& res; |
Kowalski, Kamil | 588c3f0 | 2018-04-03 14:55:27 +0200 | [diff] [blame] | 68 | std::string chassisId{}; |
Lewanczyk, Dawid | 08777fb | 2018-03-22 23:33:49 +0100 | [diff] [blame] | 69 | const std::vector<const char*> types; |
| 70 | }; |
| 71 | |
| 72 | /** |
| 73 | * @brief Creates connections necessary for chassis sensors |
Kowalski, Kamil | 588c3f0 | 2018-04-03 14:55:27 +0200 | [diff] [blame] | 74 | * @param SensorsAsyncResp Pointer to object holding response data |
Lewanczyk, Dawid | 08777fb | 2018-03-22 23:33:49 +0100 | [diff] [blame] | 75 | * @param sensorNames Sensors retrieved from chassis |
| 76 | * @param callback Callback for processing gathered connections |
| 77 | */ |
| 78 | template <typename Callback> |
Kowalski, Kamil | 588c3f0 | 2018-04-03 14:55:27 +0200 | [diff] [blame] | 79 | void getConnections(std::shared_ptr<SensorsAsyncResp> SensorsAsyncResp, |
Lewanczyk, Dawid | 08777fb | 2018-03-22 23:33:49 +0100 | [diff] [blame] | 80 | const boost::container::flat_set<std::string>& sensorNames, |
| 81 | Callback&& callback) { |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 82 | BMCWEB_LOG_DEBUG << "getConnections enter"; |
James Feist | 03b5bae | 2018-07-06 12:08:09 -0700 | [diff] [blame] | 83 | const std::string path = "/xyz/openbmc_project/sensors"; |
Lewanczyk, Dawid | 08777fb | 2018-03-22 23:33:49 +0100 | [diff] [blame] | 84 | const std::array<std::string, 1> interfaces = { |
| 85 | "xyz.openbmc_project.Sensor.Value"}; |
Lewanczyk, Dawid | 08777fb | 2018-03-22 23:33:49 +0100 | [diff] [blame] | 86 | |
| 87 | // Response handler for parsing objects subtree |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 88 | auto respHandler = |
Kowalski, Kamil | 588c3f0 | 2018-04-03 14:55:27 +0200 | [diff] [blame] | 89 | [ callback{std::move(callback)}, SensorsAsyncResp, sensorNames ]( |
| 90 | const boost::system::error_code ec, const GetSubTreeType& subtree) { |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 91 | BMCWEB_LOG_DEBUG << "getConnections resp_handler enter"; |
Ed Tanous | e0d918b | 2018-03-27 17:41:04 -0700 | [diff] [blame] | 92 | if (ec) { |
Kowalski, Kamil | 588c3f0 | 2018-04-03 14:55:27 +0200 | [diff] [blame] | 93 | SensorsAsyncResp->setErrorStatus(); |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 94 | BMCWEB_LOG_ERROR << "getConnections resp_handler: Dbus error " << ec; |
Lewanczyk, Dawid | 08777fb | 2018-03-22 23:33:49 +0100 | [diff] [blame] | 95 | return; |
| 96 | } |
| 97 | |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 98 | BMCWEB_LOG_DEBUG << "Found " << subtree.size() << " subtrees"; |
Lewanczyk, Dawid | 08777fb | 2018-03-22 23:33:49 +0100 | [diff] [blame] | 99 | |
| 100 | // Make unique list of connections only for requested sensor types and |
| 101 | // found in the chassis |
| 102 | boost::container::flat_set<std::string> connections; |
| 103 | // Intrinsic to avoid malloc. Most systems will have < 8 sensor producers |
| 104 | connections.reserve(8); |
| 105 | |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 106 | BMCWEB_LOG_DEBUG << "sensorNames list count: " << sensorNames.size(); |
Lewanczyk, Dawid | 08777fb | 2018-03-22 23:33:49 +0100 | [diff] [blame] | 107 | for (const std::string& tsensor : sensorNames) { |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 108 | BMCWEB_LOG_DEBUG << "Sensor to find: " << tsensor; |
Lewanczyk, Dawid | 08777fb | 2018-03-22 23:33:49 +0100 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | for (const std::pair< |
| 112 | std::string, |
| 113 | std::vector<std::pair<std::string, std::vector<std::string>>>>& |
| 114 | object : subtree) { |
Kowalski, Kamil | 588c3f0 | 2018-04-03 14:55:27 +0200 | [diff] [blame] | 115 | for (const char* type : SensorsAsyncResp->types) { |
Lewanczyk, Dawid | 08777fb | 2018-03-22 23:33:49 +0100 | [diff] [blame] | 116 | if (boost::starts_with(object.first, type)) { |
| 117 | auto lastPos = object.first.rfind('/'); |
| 118 | if (lastPos != std::string::npos) { |
| 119 | std::string sensorName = object.first.substr(lastPos + 1); |
| 120 | |
| 121 | if (sensorNames.find(sensorName) != sensorNames.end()) { |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 122 | // For each Connection name |
Lewanczyk, Dawid | 08777fb | 2018-03-22 23:33:49 +0100 | [diff] [blame] | 123 | for (const std::pair<std::string, std::vector<std::string>>& |
| 124 | objData : object.second) { |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 125 | BMCWEB_LOG_DEBUG << "Adding connection: " << objData.first; |
Lewanczyk, Dawid | 08777fb | 2018-03-22 23:33:49 +0100 | [diff] [blame] | 126 | connections.insert(objData.first); |
| 127 | } |
| 128 | } |
| 129 | } |
| 130 | break; |
| 131 | } |
| 132 | } |
| 133 | } |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 134 | BMCWEB_LOG_DEBUG << "Found " << connections.size() << " connections"; |
Lewanczyk, Dawid | 08777fb | 2018-03-22 23:33:49 +0100 | [diff] [blame] | 135 | callback(std::move(connections)); |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 136 | BMCWEB_LOG_DEBUG << "getConnections resp_handler exit"; |
Lewanczyk, Dawid | 08777fb | 2018-03-22 23:33:49 +0100 | [diff] [blame] | 137 | }; |
| 138 | |
| 139 | // Make call to ObjectMapper to find all sensors objects |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 140 | crow::connections::systemBus->async_method_call( |
| 141 | std::move(respHandler), "xyz.openbmc_project.ObjectMapper", |
Ed Tanous | aa2e59c | 2018-04-12 12:17:20 -0700 | [diff] [blame] | 142 | "/xyz/openbmc_project/object_mapper", "xyz.openbmc_project.ObjectMapper", |
| 143 | "GetSubTree", path, 2, interfaces); |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 144 | BMCWEB_LOG_DEBUG << "getConnections exit"; |
Lewanczyk, Dawid | 08777fb | 2018-03-22 23:33:49 +0100 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | /** |
| 148 | * @brief Retrieves requested chassis sensors and redundancy data from DBus . |
Kowalski, Kamil | 588c3f0 | 2018-04-03 14:55:27 +0200 | [diff] [blame] | 149 | * @param SensorsAsyncResp Pointer to object holding response data |
Lewanczyk, Dawid | 08777fb | 2018-03-22 23:33:49 +0100 | [diff] [blame] | 150 | * @param callback Callback for next step in gathered sensor processing |
| 151 | */ |
| 152 | template <typename Callback> |
Kowalski, Kamil | 588c3f0 | 2018-04-03 14:55:27 +0200 | [diff] [blame] | 153 | void getChassis(std::shared_ptr<SensorsAsyncResp> SensorsAsyncResp, |
| 154 | Callback&& callback) { |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 155 | BMCWEB_LOG_DEBUG << "getChassis enter"; |
Lewanczyk, Dawid | 08777fb | 2018-03-22 23:33:49 +0100 | [diff] [blame] | 156 | // Process response from EntityManager and extract chassis data |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 157 | auto respHandler = [ callback{std::move(callback)}, SensorsAsyncResp ]( |
Lewanczyk, Dawid | 08777fb | 2018-03-22 23:33:49 +0100 | [diff] [blame] | 158 | const boost::system::error_code ec, ManagedObjectsVectorType& resp) { |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 159 | BMCWEB_LOG_DEBUG << "getChassis respHandler enter"; |
Lewanczyk, Dawid | 08777fb | 2018-03-22 23:33:49 +0100 | [diff] [blame] | 160 | if (ec) { |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 161 | BMCWEB_LOG_ERROR << "getChassis respHandler DBUS error: " << ec; |
Kowalski, Kamil | 588c3f0 | 2018-04-03 14:55:27 +0200 | [diff] [blame] | 162 | SensorsAsyncResp->setErrorStatus(); |
Lewanczyk, Dawid | 08777fb | 2018-03-22 23:33:49 +0100 | [diff] [blame] | 163 | return; |
| 164 | } |
| 165 | boost::container::flat_set<std::string> sensorNames; |
Lewanczyk, Dawid | 08777fb | 2018-03-22 23:33:49 +0100 | [diff] [blame] | 166 | |
Kowalski, Kamil | 588c3f0 | 2018-04-03 14:55:27 +0200 | [diff] [blame] | 167 | // SensorsAsyncResp->chassisId |
Ed Tanous | daf36e2 | 2018-04-20 16:01:36 -0700 | [diff] [blame] | 168 | bool foundChassis = false; |
| 169 | std::vector<std::string> split; |
| 170 | // Reserve space for |
| 171 | // /xyz/openbmc_project/inventory/<name>/<subname> + 3 subnames |
| 172 | split.reserve(8); |
| 173 | |
| 174 | for (const auto& objDictEntry : resp) { |
| 175 | const std::string& objectPath = |
| 176 | static_cast<const std::string&>(objDictEntry.first); |
| 177 | boost::algorithm::split(split, objectPath, boost::is_any_of("/")); |
| 178 | if (split.size() < 2) { |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 179 | BMCWEB_LOG_ERROR << "Got path that isn't long enough " << objectPath; |
Ed Tanous | daf36e2 | 2018-04-20 16:01:36 -0700 | [diff] [blame] | 180 | split.clear(); |
| 181 | continue; |
Lewanczyk, Dawid | 08777fb | 2018-03-22 23:33:49 +0100 | [diff] [blame] | 182 | } |
Ed Tanous | daf36e2 | 2018-04-20 16:01:36 -0700 | [diff] [blame] | 183 | const std::string& sensorName = split.end()[-1]; |
| 184 | const std::string& chassisName = split.end()[-2]; |
| 185 | |
Kowalski, Kamil | 588c3f0 | 2018-04-03 14:55:27 +0200 | [diff] [blame] | 186 | if (chassisName != SensorsAsyncResp->chassisId) { |
Ed Tanous | daf36e2 | 2018-04-20 16:01:36 -0700 | [diff] [blame] | 187 | split.clear(); |
| 188 | continue; |
| 189 | } |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 190 | BMCWEB_LOG_DEBUG << "New sensor: " << sensorName; |
Ed Tanous | daf36e2 | 2018-04-20 16:01:36 -0700 | [diff] [blame] | 191 | foundChassis = true; |
| 192 | sensorNames.emplace(sensorName); |
| 193 | split.clear(); |
Lewanczyk, Dawid | 08777fb | 2018-03-22 23:33:49 +0100 | [diff] [blame] | 194 | }; |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 195 | BMCWEB_LOG_DEBUG << "Found " << sensorNames.size() << " Sensor names"; |
Lewanczyk, Dawid | 08777fb | 2018-03-22 23:33:49 +0100 | [diff] [blame] | 196 | |
| 197 | if (!foundChassis) { |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 198 | BMCWEB_LOG_INFO << "Unable to find chassis named " |
| 199 | << SensorsAsyncResp->chassisId; |
Kowalski, Kamil | 588c3f0 | 2018-04-03 14:55:27 +0200 | [diff] [blame] | 200 | SensorsAsyncResp->res.result(boost::beast::http::status::not_found); |
Lewanczyk, Dawid | 08777fb | 2018-03-22 23:33:49 +0100 | [diff] [blame] | 201 | } else { |
| 202 | callback(sensorNames); |
| 203 | } |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 204 | BMCWEB_LOG_DEBUG << "getChassis respHandler exit"; |
Lewanczyk, Dawid | 08777fb | 2018-03-22 23:33:49 +0100 | [diff] [blame] | 205 | }; |
| 206 | |
| 207 | // Make call to EntityManager to find all chassis objects |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 208 | crow::connections::systemBus->async_method_call( |
| 209 | respHandler, "xyz.openbmc_project.EntityManager", "/", |
Lewanczyk, Dawid | 7885954 | 2018-06-11 16:58:40 +0200 | [diff] [blame] | 210 | "org.freedesktop.DBus.ObjectManager", "GetManagedObjects"); |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 211 | BMCWEB_LOG_DEBUG << "getChassis exit"; |
Lewanczyk, Dawid | 08777fb | 2018-03-22 23:33:49 +0100 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | /** |
| 215 | * @brief Builds a json sensor representation of a sensor. |
| 216 | * @param sensorName The name of the sensor to be built |
Gunnar Mills | 274fad5 | 2018-06-13 15:45:36 -0500 | [diff] [blame] | 217 | * @param sensorType The type (temperature, fan_tach, etc) of the sensor to |
Lewanczyk, Dawid | 08777fb | 2018-03-22 23:33:49 +0100 | [diff] [blame] | 218 | * build |
| 219 | * @param interfacesDict A dictionary of the interfaces and properties of said |
| 220 | * interfaces to be built from |
| 221 | * @param sensor_json The json object to fill |
| 222 | */ |
| 223 | void objectInterfacesToJson( |
| 224 | const std::string& sensorName, const std::string& sensorType, |
| 225 | const boost::container::flat_map< |
Ed Tanous | aa2e59c | 2018-04-12 12:17:20 -0700 | [diff] [blame] | 226 | std::string, boost::container::flat_map<std::string, SensorVariant>>& |
Lewanczyk, Dawid | 08777fb | 2018-03-22 23:33:49 +0100 | [diff] [blame] | 227 | interfacesDict, |
| 228 | nlohmann::json& sensor_json) { |
| 229 | // We need a value interface before we can do anything with it |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 230 | auto valueIt = interfacesDict.find("xyz.openbmc_project.Sensor.Value"); |
| 231 | if (valueIt == interfacesDict.end()) { |
| 232 | BMCWEB_LOG_ERROR << "Sensor doesn't have a value interface"; |
Lewanczyk, Dawid | 08777fb | 2018-03-22 23:33:49 +0100 | [diff] [blame] | 233 | return; |
| 234 | } |
| 235 | |
| 236 | // Assume values exist as is (10^0 == 1) if no scale exists |
| 237 | int64_t scaleMultiplier = 0; |
| 238 | |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 239 | auto scaleIt = valueIt->second.find("Scale"); |
Lewanczyk, Dawid | 08777fb | 2018-03-22 23:33:49 +0100 | [diff] [blame] | 240 | // If a scale exists, pull value as int64, and use the scaling. |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 241 | if (scaleIt != valueIt->second.end()) { |
| 242 | const int64_t* int64Value = mapbox::getPtr<const int64_t>(scaleIt->second); |
Lewanczyk, Dawid | 08777fb | 2018-03-22 23:33:49 +0100 | [diff] [blame] | 243 | if (int64Value != nullptr) { |
| 244 | scaleMultiplier = *int64Value; |
| 245 | } |
| 246 | } |
| 247 | |
| 248 | sensor_json["MemberId"] = sensorName; |
| 249 | sensor_json["Name"] = sensorName; |
| 250 | sensor_json["Status"]["State"] = "Enabled"; |
| 251 | sensor_json["Status"]["Health"] = "OK"; |
| 252 | |
| 253 | // Parameter to set to override the type we get from dbus, and force it to |
| 254 | // int, regardless of what is available. This is used for schemas like fan, |
| 255 | // that require integers, not floats. |
| 256 | bool forceToInt = false; |
| 257 | |
| 258 | const char* unit = "Reading"; |
| 259 | if (sensorType == "temperature") { |
| 260 | unit = "ReadingCelsius"; |
Lewanczyk, Dawid | 7885954 | 2018-06-11 16:58:40 +0200 | [diff] [blame] | 261 | sensor_json["@odata.type"] = "#Thermal.v1_3_0.Temperature"; |
Lewanczyk, Dawid | 08777fb | 2018-03-22 23:33:49 +0100 | [diff] [blame] | 262 | // TODO(ed) Documentation says that path should be type fan_tach, |
| 263 | // implementation seems to implement fan |
James Feist | 03b5bae | 2018-07-06 12:08:09 -0700 | [diff] [blame] | 264 | } else if (sensorType == "fan" || sensorType == "fan_tach") { |
Lewanczyk, Dawid | 08777fb | 2018-03-22 23:33:49 +0100 | [diff] [blame] | 265 | unit = "Reading"; |
| 266 | sensor_json["ReadingUnits"] = "RPM"; |
Lewanczyk, Dawid | 7885954 | 2018-06-11 16:58:40 +0200 | [diff] [blame] | 267 | sensor_json["@odata.type"] = "#Thermal.v1_3_0.Fan"; |
Lewanczyk, Dawid | 08777fb | 2018-03-22 23:33:49 +0100 | [diff] [blame] | 268 | forceToInt = true; |
| 269 | } else if (sensorType == "voltage") { |
| 270 | unit = "ReadingVolts"; |
Lewanczyk, Dawid | 7885954 | 2018-06-11 16:58:40 +0200 | [diff] [blame] | 271 | sensor_json["@odata.type"] = "#Power.v1_0_0.Voltage"; |
Lewanczyk, Dawid | 08777fb | 2018-03-22 23:33:49 +0100 | [diff] [blame] | 272 | } else { |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 273 | BMCWEB_LOG_ERROR << "Redfish cannot map object type for " << sensorName; |
Lewanczyk, Dawid | 08777fb | 2018-03-22 23:33:49 +0100 | [diff] [blame] | 274 | return; |
| 275 | } |
| 276 | // Map of dbus interface name, dbus property name and redfish property_name |
| 277 | std::vector<std::tuple<const char*, const char*, const char*>> properties; |
| 278 | properties.reserve(7); |
| 279 | |
| 280 | properties.emplace_back("xyz.openbmc_project.Sensor.Value", "Value", unit); |
| 281 | properties.emplace_back("xyz.openbmc_project.Sensor.Threshold.Warning", |
| 282 | "WarningHigh", "UpperThresholdNonCritical"); |
| 283 | properties.emplace_back("xyz.openbmc_project.Sensor.Threshold.Warning", |
| 284 | "WarningLow", "LowerThresholdNonCritical"); |
| 285 | properties.emplace_back("xyz.openbmc_project.Sensor.Threshold.Critical", |
| 286 | "CriticalHigh", "UpperThresholdCritical"); |
| 287 | properties.emplace_back("xyz.openbmc_project.Sensor.Threshold.Critical", |
| 288 | "CriticalLow", "LowerThresholdCritical"); |
| 289 | |
| 290 | if (sensorType == "temperature") { |
| 291 | properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MinValue", |
| 292 | "MinReadingRangeTemp"); |
| 293 | properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MaxValue", |
| 294 | "MaxReadingRangeTemp"); |
| 295 | } else { |
| 296 | properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MinValue", |
| 297 | "MinReadingRange"); |
| 298 | properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MaxValue", |
| 299 | "MaxReadingRange"); |
| 300 | } |
| 301 | |
| 302 | for (const std::tuple<const char*, const char*, const char*>& p : |
| 303 | properties) { |
| 304 | auto interfaceProperties = interfacesDict.find(std::get<0>(p)); |
| 305 | if (interfaceProperties != interfacesDict.end()) { |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 306 | auto valueIt = interfaceProperties->second.find(std::get<1>(p)); |
| 307 | if (valueIt != interfaceProperties->second.end()) { |
| 308 | const SensorVariant& valueVariant = valueIt->second; |
| 309 | nlohmann::json& valueIt = sensor_json[std::get<2>(p)]; |
Ed Tanous | aa2e59c | 2018-04-12 12:17:20 -0700 | [diff] [blame] | 310 | |
Lewanczyk, Dawid | 08777fb | 2018-03-22 23:33:49 +0100 | [diff] [blame] | 311 | // Attempt to pull the int64 directly |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 312 | const int64_t* int64Value = mapbox::getPtr<const int64_t>(valueVariant); |
Lewanczyk, Dawid | 08777fb | 2018-03-22 23:33:49 +0100 | [diff] [blame] | 313 | |
| 314 | if (int64Value != nullptr) { |
| 315 | if (forceToInt || scaleMultiplier >= 0) { |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 316 | valueIt = *int64Value * std::pow(10, scaleMultiplier); |
Lewanczyk, Dawid | 08777fb | 2018-03-22 23:33:49 +0100 | [diff] [blame] | 317 | } else { |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 318 | valueIt = *int64Value * |
| 319 | std::pow(10, static_cast<double>(scaleMultiplier)); |
Lewanczyk, Dawid | 08777fb | 2018-03-22 23:33:49 +0100 | [diff] [blame] | 320 | } |
| 321 | } |
| 322 | // Attempt to pull the float directly |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 323 | const double* doubleValue = mapbox::getPtr<const double>(valueVariant); |
Lewanczyk, Dawid | 08777fb | 2018-03-22 23:33:49 +0100 | [diff] [blame] | 324 | |
| 325 | if (doubleValue != nullptr) { |
| 326 | if (!forceToInt) { |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 327 | valueIt = *doubleValue * |
| 328 | std::pow(10, static_cast<double>(scaleMultiplier)); |
Lewanczyk, Dawid | 08777fb | 2018-03-22 23:33:49 +0100 | [diff] [blame] | 329 | } else { |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 330 | valueIt = static_cast<int64_t>(*doubleValue * |
| 331 | std::pow(10, scaleMultiplier)); |
Lewanczyk, Dawid | 08777fb | 2018-03-22 23:33:49 +0100 | [diff] [blame] | 332 | } |
| 333 | } |
| 334 | } |
| 335 | } |
| 336 | } |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 337 | BMCWEB_LOG_DEBUG << "Added sensor " << sensorName; |
Lewanczyk, Dawid | 08777fb | 2018-03-22 23:33:49 +0100 | [diff] [blame] | 338 | } |
| 339 | |
| 340 | /** |
| 341 | * @brief Entry point for retrieving sensors data related to requested |
| 342 | * chassis. |
Kowalski, Kamil | 588c3f0 | 2018-04-03 14:55:27 +0200 | [diff] [blame] | 343 | * @param SensorsAsyncResp Pointer to object holding response data |
Lewanczyk, Dawid | 08777fb | 2018-03-22 23:33:49 +0100 | [diff] [blame] | 344 | */ |
Kowalski, Kamil | 588c3f0 | 2018-04-03 14:55:27 +0200 | [diff] [blame] | 345 | void getChassisData(std::shared_ptr<SensorsAsyncResp> SensorsAsyncResp) { |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 346 | BMCWEB_LOG_DEBUG << "getChassisData enter"; |
Kowalski, Kamil | 588c3f0 | 2018-04-03 14:55:27 +0200 | [diff] [blame] | 347 | auto getChassisCb = [&, SensorsAsyncResp]( |
| 348 | boost::container::flat_set<std::string>& |
| 349 | sensorNames) { |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 350 | BMCWEB_LOG_DEBUG << "getChassisCb enter"; |
Kowalski, Kamil | 588c3f0 | 2018-04-03 14:55:27 +0200 | [diff] [blame] | 351 | auto getConnectionCb = |
| 352 | [&, SensorsAsyncResp, sensorNames]( |
| 353 | const boost::container::flat_set<std::string>& connections) { |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 354 | BMCWEB_LOG_DEBUG << "getConnectionCb enter"; |
Kowalski, Kamil | 588c3f0 | 2018-04-03 14:55:27 +0200 | [diff] [blame] | 355 | // Get managed objects from all services exposing sensors |
| 356 | for (const std::string& connection : connections) { |
| 357 | // Response handler to process managed objects |
| 358 | auto getManagedObjectsCb = [&, SensorsAsyncResp, sensorNames]( |
| 359 | const boost::system::error_code ec, |
| 360 | ManagedObjectsVectorType& resp) { |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 361 | BMCWEB_LOG_DEBUG << "getManagedObjectsCb enter"; |
Lewanczyk, Dawid | 7885954 | 2018-06-11 16:58:40 +0200 | [diff] [blame] | 362 | if (ec) { |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 363 | BMCWEB_LOG_ERROR << "getManagedObjectsCb DBUS error: " << ec; |
Lewanczyk, Dawid | 7885954 | 2018-06-11 16:58:40 +0200 | [diff] [blame] | 364 | SensorsAsyncResp->setErrorStatus(); |
| 365 | return; |
| 366 | } |
Kowalski, Kamil | 588c3f0 | 2018-04-03 14:55:27 +0200 | [diff] [blame] | 367 | // Go through all objects and update response with |
| 368 | // sensor data |
| 369 | for (const auto& objDictEntry : resp) { |
| 370 | const std::string& objPath = |
| 371 | static_cast<const std::string&>(objDictEntry.first); |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 372 | BMCWEB_LOG_DEBUG << "getManagedObjectsCb parsing object " |
| 373 | << objPath; |
Lewanczyk, Dawid | 08777fb | 2018-03-22 23:33:49 +0100 | [diff] [blame] | 374 | |
Kowalski, Kamil | 588c3f0 | 2018-04-03 14:55:27 +0200 | [diff] [blame] | 375 | std::vector<std::string> split; |
| 376 | // Reserve space for |
James Feist | 03b5bae | 2018-07-06 12:08:09 -0700 | [diff] [blame] | 377 | // /xyz/openbmc_project/sensors/<name>/<subname> |
Kowalski, Kamil | 588c3f0 | 2018-04-03 14:55:27 +0200 | [diff] [blame] | 378 | split.reserve(6); |
| 379 | boost::algorithm::split(split, objPath, boost::is_any_of("/")); |
| 380 | if (split.size() < 6) { |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 381 | BMCWEB_LOG_ERROR << "Got path that isn't long enough " |
| 382 | << objPath; |
Kowalski, Kamil | 588c3f0 | 2018-04-03 14:55:27 +0200 | [diff] [blame] | 383 | continue; |
| 384 | } |
| 385 | // These indexes aren't intuitive, as boost::split puts an empty |
| 386 | // string at the beggining |
| 387 | const std::string& sensorType = split[4]; |
| 388 | const std::string& sensorName = split[5]; |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 389 | BMCWEB_LOG_DEBUG << "sensorName " << sensorName |
| 390 | << " sensorType " << sensorType; |
Kowalski, Kamil | 588c3f0 | 2018-04-03 14:55:27 +0200 | [diff] [blame] | 391 | if (sensorNames.find(sensorName) == sensorNames.end()) { |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 392 | BMCWEB_LOG_ERROR << sensorName << " not in sensor list "; |
Kowalski, Kamil | 588c3f0 | 2018-04-03 14:55:27 +0200 | [diff] [blame] | 393 | continue; |
| 394 | } |
Lewanczyk, Dawid | 08777fb | 2018-03-22 23:33:49 +0100 | [diff] [blame] | 395 | |
Kowalski, Kamil | 588c3f0 | 2018-04-03 14:55:27 +0200 | [diff] [blame] | 396 | const char* fieldName = nullptr; |
| 397 | if (sensorType == "temperature") { |
| 398 | fieldName = "Temperatures"; |
| 399 | } else if (sensorType == "fan" || sensorType == "fan_tach") { |
| 400 | fieldName = "Fans"; |
| 401 | } else if (sensorType == "voltage") { |
| 402 | fieldName = "Voltages"; |
| 403 | } else if (sensorType == "current") { |
| 404 | fieldName = "PowerSupply"; |
| 405 | } else if (sensorType == "power") { |
| 406 | fieldName = "PowerSupply"; |
| 407 | } else { |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 408 | BMCWEB_LOG_ERROR << "Unsure how to handle sensorType " |
| 409 | << sensorType; |
Kowalski, Kamil | 588c3f0 | 2018-04-03 14:55:27 +0200 | [diff] [blame] | 410 | continue; |
| 411 | } |
Lewanczyk, Dawid | 08777fb | 2018-03-22 23:33:49 +0100 | [diff] [blame] | 412 | |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 413 | nlohmann::json& tempArray = |
| 414 | SensorsAsyncResp->res.jsonValue[fieldName]; |
Lewanczyk, Dawid | 08777fb | 2018-03-22 23:33:49 +0100 | [diff] [blame] | 415 | |
Kowalski, Kamil | 588c3f0 | 2018-04-03 14:55:27 +0200 | [diff] [blame] | 416 | // Create the array if it doesn't yet exist |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 417 | if (tempArray.is_array() == false) { |
| 418 | tempArray = nlohmann::json::array(); |
Kowalski, Kamil | 588c3f0 | 2018-04-03 14:55:27 +0200 | [diff] [blame] | 419 | } |
Lewanczyk, Dawid | 08777fb | 2018-03-22 23:33:49 +0100 | [diff] [blame] | 420 | |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 421 | tempArray.push_back( |
Kowalski, Kamil | 588c3f0 | 2018-04-03 14:55:27 +0200 | [diff] [blame] | 422 | {{"@odata.id", "/redfish/v1/Chassis/" + |
| 423 | SensorsAsyncResp->chassisId + |
| 424 | "/Thermal#/" + sensorName}}); |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 425 | nlohmann::json& sensorJson = tempArray.back(); |
Kowalski, Kamil | 588c3f0 | 2018-04-03 14:55:27 +0200 | [diff] [blame] | 426 | objectInterfacesToJson(sensorName, sensorType, |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 427 | objDictEntry.second, sensorJson); |
Kowalski, Kamil | 588c3f0 | 2018-04-03 14:55:27 +0200 | [diff] [blame] | 428 | } |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 429 | BMCWEB_LOG_DEBUG << "getManagedObjectsCb exit"; |
Kowalski, Kamil | 588c3f0 | 2018-04-03 14:55:27 +0200 | [diff] [blame] | 430 | }; |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 431 | crow::connections::systemBus->async_method_call( |
Lewanczyk, Dawid | 7885954 | 2018-06-11 16:58:40 +0200 | [diff] [blame] | 432 | getManagedObjectsCb, connection, "/", |
Kowalski, Kamil | 588c3f0 | 2018-04-03 14:55:27 +0200 | [diff] [blame] | 433 | "org.freedesktop.DBus.ObjectManager", "GetManagedObjects"); |
| 434 | }; |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 435 | BMCWEB_LOG_DEBUG << "getConnectionCb exit"; |
Lewanczyk, Dawid | 08777fb | 2018-03-22 23:33:49 +0100 | [diff] [blame] | 436 | }; |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 437 | // get connections and then pass it to get sensors |
Kowalski, Kamil | 588c3f0 | 2018-04-03 14:55:27 +0200 | [diff] [blame] | 438 | getConnections(SensorsAsyncResp, sensorNames, std::move(getConnectionCb)); |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 439 | BMCWEB_LOG_DEBUG << "getChassisCb exit"; |
Lewanczyk, Dawid | 08777fb | 2018-03-22 23:33:49 +0100 | [diff] [blame] | 440 | }; |
| 441 | |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 442 | // get chassis information related to sensors |
Kowalski, Kamil | 588c3f0 | 2018-04-03 14:55:27 +0200 | [diff] [blame] | 443 | getChassis(SensorsAsyncResp, std::move(getChassisCb)); |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 444 | BMCWEB_LOG_DEBUG << "getChassisData exit"; |
Lewanczyk, Dawid | 08777fb | 2018-03-22 23:33:49 +0100 | [diff] [blame] | 445 | }; |
| 446 | |
| 447 | } // namespace redfish |