Deepak Kodihalli | 1bb0d38 | 2017-08-12 02:01:27 -0500 | [diff] [blame] | 1 | #pragma once |
| 2 | |
Tom Joseph | 13b87a3 | 2018-02-16 09:37:43 +0530 | [diff] [blame] | 3 | #include <math.h> |
Tom Joseph | 295f17e | 2017-09-07 00:09:46 +0530 | [diff] [blame] | 4 | #include "sensorhandler.h" |
Dhruvaraj Subhashchandran | e0af720 | 2017-07-12 06:35:20 -0500 | [diff] [blame] | 5 | #include "types.hpp" |
Tom Joseph | 295f17e | 2017-09-07 00:09:46 +0530 | [diff] [blame] | 6 | #include "utils.hpp" |
Dhruvaraj Subhashchandran | e0af720 | 2017-07-12 06:35:20 -0500 | [diff] [blame] | 7 | #include "host-ipmid/ipmid-api.h" |
| 8 | |
| 9 | namespace ipmi |
| 10 | { |
| 11 | namespace sensor |
| 12 | { |
| 13 | |
| 14 | using Assertion = uint16_t; |
| 15 | using Deassertion = uint16_t; |
| 16 | using AssertionSet = std::pair<Assertion, Deassertion>; |
| 17 | |
| 18 | using Service = std::string; |
| 19 | using Path = std::string; |
| 20 | using Interface = std::string; |
| 21 | |
| 22 | using ServicePath = std::pair<Path, Service>; |
| 23 | |
| 24 | using Interfaces = std::vector<Interface>; |
| 25 | |
| 26 | using MapperResponseType = std::map<Path, std::map<Service, Interfaces>>; |
| 27 | |
| 28 | /** @brief get the D-Bus service and service path |
| 29 | * @param[in] bus - The Dbus bus object |
| 30 | * @param[in] interface - interface to the service |
| 31 | * @param[in] path - interested path in the list of objects |
| 32 | * @return pair of service path and service |
| 33 | */ |
| 34 | ServicePath getServiceAndPath(sdbusplus::bus::bus& bus, |
| 35 | const std::string& interface, |
| 36 | const std::string& path = std::string()); |
| 37 | |
| 38 | /** @brief Make assertion set from input data |
| 39 | * @param[in] cmdData - Input sensor data |
| 40 | * @return pair of assertion and deassertion set |
| 41 | */ |
| 42 | AssertionSet getAssertionSet(const SetSensorReadingReq& cmdData); |
| 43 | |
| 44 | /** @brief send the message to DBus |
| 45 | * @param[in] msg - message to send |
| 46 | * @return failure status in IPMI error code |
| 47 | */ |
Dhruvaraj Subhashchandran | 2a444d0 | 2017-08-07 01:45:14 -0500 | [diff] [blame] | 48 | ipmi_ret_t updateToDbus(IpmiUpdateData& msg); |
Dhruvaraj Subhashchandran | e0af720 | 2017-07-12 06:35:20 -0500 | [diff] [blame] | 49 | |
Tom Joseph | 816e92b | 2017-09-06 19:23:00 +0530 | [diff] [blame] | 50 | namespace get |
| 51 | { |
| 52 | |
Tom Joseph | b0adbcd | 2018-01-24 11:51:29 +0530 | [diff] [blame] | 53 | /** @brief Populate sensor name from the D-Bus property associated with the |
| 54 | * sensor. In the example entry from the yaml, the name of the D-bus |
| 55 | * property "AttemptsLeft" is the sensor name. |
| 56 | * |
| 57 | * 0x07: |
| 58 | * sensorType: 195 |
| 59 | * path: /xyz/openbmc_project/state/host0 |
| 60 | * sensorReadingType: 0x6F |
| 61 | * serviceInterface: org.freedesktop.DBus.Properties |
| 62 | * readingType: readingAssertion |
| 63 | * sensorNamePattern: nameProperty |
| 64 | * interfaces: |
| 65 | * xyz.openbmc_project.Control.Boot.RebootAttempts: |
| 66 | * AttemptsLeft: |
| 67 | * Offsets: |
| 68 | * 0xFF: |
| 69 | * type: uint32_t |
| 70 | * |
| 71 | * |
| 72 | * @param[in] sensorInfo - Dbus info related to sensor. |
| 73 | * |
| 74 | * @return On success return the sensor name for the sensor. |
| 75 | */ |
| 76 | inline SensorName nameProperty(const Info& sensorInfo) |
| 77 | { |
| 78 | return sensorInfo.propertyInterfaces.begin()->second.begin()->first; |
| 79 | } |
| 80 | |
| 81 | /** @brief Populate sensor name from the D-Bus object associated with the |
| 82 | * sensor. If the object path is /system/chassis/motherboard/dimm0 then |
| 83 | * the leaf dimm0 is considered as the sensor name. |
| 84 | * |
| 85 | * @param[in] sensorInfo - Dbus info related to sensor. |
| 86 | * |
| 87 | * @return On success return the sensor name for the sensor. |
| 88 | */ |
| 89 | inline SensorName nameLeaf(const Info& sensorInfo) |
| 90 | { |
| 91 | return sensorInfo.sensorPath.substr( |
| 92 | sensorInfo.sensorPath.find_last_of('/') + 1, |
| 93 | sensorInfo.sensorPath.length()); |
| 94 | } |
| 95 | |
| 96 | /** @brief Populate sensor name from the D-Bus object associated with the |
| 97 | * sensor. If the object path is /system/chassis/motherboard/cpu0/core0 |
| 98 | * then the sensor name is cpu0_core0. The leaf and the parent is put |
| 99 | * together to get the sensor name. |
| 100 | * |
| 101 | * @param[in] sensorInfo - Dbus info related to sensor. |
| 102 | * |
| 103 | * @return On success return the sensor name for the sensor. |
| 104 | */ |
| 105 | SensorName nameParentLeaf(const Info& sensorInfo); |
| 106 | |
Tom Joseph | 816e92b | 2017-09-06 19:23:00 +0530 | [diff] [blame] | 107 | /** |
| 108 | * @brief Helper function to map the dbus info to sensor's assertion status |
| 109 | * for the get sensor reading command. |
| 110 | * |
| 111 | * @param[in] sensorInfo - Dbus info related to sensor. |
| 112 | * @param[in] path - Dbus object path. |
| 113 | * @param[in] interface - Dbus interface. |
| 114 | * |
| 115 | * @return Response for get sensor reading command. |
| 116 | */ |
| 117 | GetSensorResponse mapDbusToAssertion(const Info& sensorInfo, |
| 118 | const InstancePath& path, |
| 119 | const DbusInterface& interface); |
| 120 | |
| 121 | /** |
| 122 | * @brief Map the Dbus info to sensor's assertion status in the Get sensor |
| 123 | * reading command response. |
| 124 | * |
| 125 | * @param[in] sensorInfo - Dbus info related to sensor. |
| 126 | * |
| 127 | * @return Response for get sensor reading command. |
| 128 | */ |
| 129 | GetSensorResponse assertion(const Info& sensorInfo); |
| 130 | |
Tom Joseph | e4014fc | 2017-09-06 23:57:36 +0530 | [diff] [blame] | 131 | /** |
| 132 | * @brief Maps the Dbus info to the reading field in the Get sensor reading |
| 133 | * command response. |
| 134 | * |
| 135 | * @param[in] sensorInfo - Dbus info related to sensor. |
| 136 | * |
| 137 | * @return Response for get sensor reading command. |
| 138 | */ |
| 139 | GetSensorResponse eventdata2(const Info& sensorInfo); |
| 140 | |
Tom Joseph | 295f17e | 2017-09-07 00:09:46 +0530 | [diff] [blame] | 141 | /** |
| 142 | * @brief readingAssertion is a case where the entire assertion state field |
| 143 | * serves as the sensor value. |
| 144 | * |
| 145 | * @tparam T - type of the dbus property related to sensor. |
| 146 | * @param[in] sensorInfo - Dbus info related to sensor. |
| 147 | * |
| 148 | * @return Response for get sensor reading command. |
| 149 | */ |
| 150 | template<typename T> |
| 151 | GetSensorResponse readingAssertion(const Info& sensorInfo) |
| 152 | { |
| 153 | sdbusplus::bus::bus bus{ipmid_get_sd_bus_connection()}; |
| 154 | GetSensorResponse response {}; |
| 155 | auto responseData = reinterpret_cast<GetReadingResponse*>(response.data()); |
| 156 | |
| 157 | auto service = ipmi::getService(bus, |
| 158 | sensorInfo.sensorInterface, |
| 159 | sensorInfo.sensorPath); |
| 160 | |
| 161 | auto propValue = ipmi::getDbusProperty( |
| 162 | bus, |
| 163 | service, |
| 164 | sensorInfo.sensorPath, |
| 165 | sensorInfo.propertyInterfaces.begin()->first, |
| 166 | sensorInfo.propertyInterfaces.begin()->second.begin()->first); |
| 167 | |
| 168 | setAssertionBytes(static_cast<uint16_t>(propValue.get<T>()), responseData); |
| 169 | |
| 170 | return response; |
| 171 | } |
| 172 | |
Tom Joseph | e05b292 | 2017-09-07 00:43:16 +0530 | [diff] [blame] | 173 | /** @brief Map the Dbus info to the reading field in the Get sensor reading |
| 174 | * command response |
| 175 | * |
| 176 | * @tparam T - type of the dbus property related to sensor. |
| 177 | * @param[in] sensorInfo - Dbus info related to sensor. |
| 178 | * |
| 179 | * @return Response for get sensor reading command. |
| 180 | */ |
| 181 | template<typename T> |
| 182 | GetSensorResponse readingData(const Info& sensorInfo) |
| 183 | { |
| 184 | sdbusplus::bus::bus bus{ipmid_get_sd_bus_connection()}; |
| 185 | GetSensorResponse response {}; |
| 186 | auto responseData = reinterpret_cast<GetReadingResponse*>(response.data()); |
| 187 | |
| 188 | enableScanning(responseData); |
| 189 | |
| 190 | auto service = ipmi::getService(bus, |
| 191 | sensorInfo.sensorInterface, |
| 192 | sensorInfo.sensorPath); |
| 193 | |
| 194 | auto propValue = ipmi::getDbusProperty( |
| 195 | bus, |
| 196 | service, |
| 197 | sensorInfo.sensorPath, |
| 198 | sensorInfo.propertyInterfaces.begin()->first, |
| 199 | sensorInfo.propertyInterfaces.begin()->second.begin()->first); |
| 200 | |
Tom Joseph | 13b87a3 | 2018-02-16 09:37:43 +0530 | [diff] [blame] | 201 | double value = propValue.get<T>() * pow(10, |
| 202 | sensorInfo.scale - sensorInfo.exponentR); |
Tom Joseph | e05b292 | 2017-09-07 00:43:16 +0530 | [diff] [blame] | 203 | |
Tom Joseph | 13b87a3 | 2018-02-16 09:37:43 +0530 | [diff] [blame] | 204 | auto rawData = static_cast<uint8_t>( |
| 205 | (value - sensorInfo.scaledOffset) / sensorInfo.coefficientM); |
| 206 | |
| 207 | setReading(rawData, responseData); |
Tom Joseph | e05b292 | 2017-09-07 00:43:16 +0530 | [diff] [blame] | 208 | |
| 209 | return response; |
| 210 | } |
| 211 | |
Tom Joseph | 816e92b | 2017-09-06 19:23:00 +0530 | [diff] [blame] | 212 | } //namespace get |
| 213 | |
Dhruvaraj Subhashchandran | e0af720 | 2017-07-12 06:35:20 -0500 | [diff] [blame] | 214 | namespace set |
| 215 | { |
| 216 | |
| 217 | /** @brief Make a DBus message for a Dbus call |
| 218 | * @param[in] updateInterface - Interface name |
| 219 | * @param[in] sensorPath - Path of the sensor |
| 220 | * @param[in] command - command to be executed |
| 221 | * @param[in] sensorInterface - DBus interface of sensor |
| 222 | * @return a dbus message |
| 223 | */ |
| 224 | IpmiUpdateData makeDbusMsg(const std::string& updateInterface, |
| 225 | const std::string& sensorPath, |
| 226 | const std::string& command, |
| 227 | const std::string& sensorInterface); |
| 228 | |
Deepak Kodihalli | 1bb0d38 | 2017-08-12 02:01:27 -0500 | [diff] [blame] | 229 | /** @brief Update d-bus based on assertion type sensor data |
Dhruvaraj Subhashchandran | e0af720 | 2017-07-12 06:35:20 -0500 | [diff] [blame] | 230 | * @param[in] cmdData - input sensor data |
Deepak Kodihalli | 1bb0d38 | 2017-08-12 02:01:27 -0500 | [diff] [blame] | 231 | * @param[in] sensorInfo - sensor d-bus info |
Dhruvaraj Subhashchandran | e0af720 | 2017-07-12 06:35:20 -0500 | [diff] [blame] | 232 | * @return a IPMI error code |
| 233 | */ |
Deepak Kodihalli | 1bb0d38 | 2017-08-12 02:01:27 -0500 | [diff] [blame] | 234 | ipmi_ret_t assertion(const SetSensorReadingReq& cmdData, |
| 235 | const Info& sensorInfo); |
Dhruvaraj Subhashchandran | e0af720 | 2017-07-12 06:35:20 -0500 | [diff] [blame] | 236 | |
Deepak Kodihalli | 1bb0d38 | 2017-08-12 02:01:27 -0500 | [diff] [blame] | 237 | /** @brief Update d-bus based on a reading assertion |
| 238 | * @tparam T - type of d-bus property mapping this sensor |
| 239 | * @param[in] cmdData - input sensor data |
| 240 | * @param[in] sensorInfo - sensor d-bus info |
Dhruvaraj Subhashchandran | e0af720 | 2017-07-12 06:35:20 -0500 | [diff] [blame] | 241 | * @return a IPMI error code |
| 242 | */ |
Deepak Kodihalli | 1bb0d38 | 2017-08-12 02:01:27 -0500 | [diff] [blame] | 243 | template<typename T> |
| 244 | ipmi_ret_t readingAssertion(const SetSensorReadingReq& cmdData, |
| 245 | const Info& sensorInfo) |
| 246 | { |
| 247 | auto msg = makeDbusMsg( |
| 248 | "org.freedesktop.DBus.Properties", |
| 249 | sensorInfo.sensorPath, |
| 250 | "Set", |
| 251 | sensorInfo.sensorInterface); |
Dhruvaraj Subhashchandran | e0af720 | 2017-07-12 06:35:20 -0500 | [diff] [blame] | 252 | |
Deepak Kodihalli | 1bb0d38 | 2017-08-12 02:01:27 -0500 | [diff] [blame] | 253 | const auto& interface = sensorInfo.propertyInterfaces.begin(); |
| 254 | msg.append(interface->first); |
| 255 | for (const auto& property : interface->second) |
| 256 | { |
| 257 | msg.append(property.first); |
| 258 | sdbusplus::message::variant<T> value = |
| 259 | (cmdData.assertOffset8_14 << 8) | cmdData.assertOffset0_7; |
| 260 | msg.append(value); |
| 261 | } |
| 262 | return updateToDbus(msg); |
| 263 | } |
| 264 | |
Emily Shaffer | cc941e1 | 2017-06-14 13:06:26 -0700 | [diff] [blame] | 265 | /** @brief Update d-bus based on a discrete reading |
| 266 | * @param[in] cmdData - input sensor data |
| 267 | * @param[in] sensorInfo - sensor d-bus info |
| 268 | * @return an IPMI error code |
| 269 | */ |
| 270 | template<typename T> |
| 271 | ipmi_ret_t readingData(const SetSensorReadingReq& cmdData, |
| 272 | const Info& sensorInfo) |
| 273 | { |
| 274 | auto msg = makeDbusMsg( |
| 275 | "org.freedesktop.DBus.Properties", |
| 276 | sensorInfo.sensorPath, |
| 277 | "Set", |
| 278 | sensorInfo.sensorInterface); |
| 279 | |
| 280 | const auto& interface = sensorInfo.propertyInterfaces.begin(); |
| 281 | msg.append(interface->first); |
| 282 | |
| 283 | ipmi::sensor::Multiplier m = sensorInfo.coefficientM; |
| 284 | if (0 == m) |
| 285 | { |
| 286 | m = 1; // Avoid * 0 |
| 287 | } |
| 288 | |
| 289 | // TODO: Refactor this into a generated function depending on the type |
| 290 | // of conversion for the value between IPMI and dbus. |
| 291 | T raw_value = (m * cmdData.reading) + sensorInfo.scaledOffset; |
| 292 | |
| 293 | for (const auto& property : interface->second) |
| 294 | { |
| 295 | msg.append(property.first); |
| 296 | sdbusplus::message::variant<T> value = raw_value; |
| 297 | msg.append(value); |
| 298 | } |
| 299 | return updateToDbus(msg); |
| 300 | } |
Deepak Kodihalli | 1bb0d38 | 2017-08-12 02:01:27 -0500 | [diff] [blame] | 301 | |
| 302 | /** @brief Update d-bus based on eventdata type sensor data |
| 303 | * @param[in] cmdData - input sensor data |
| 304 | * @param[in] sensorInfo - sensor d-bus info |
Dhruvaraj Subhashchandran | e0af720 | 2017-07-12 06:35:20 -0500 | [diff] [blame] | 305 | * @return a IPMI error code |
| 306 | */ |
Deepak Kodihalli | 1bb0d38 | 2017-08-12 02:01:27 -0500 | [diff] [blame] | 307 | ipmi_ret_t eventdata(const SetSensorReadingReq& cmdData, |
| 308 | const Info& sensorInfo, |
| 309 | uint8_t data); |
| 310 | |
| 311 | /** @brief Update d-bus based on eventdata1 type sensor data |
| 312 | * @param[in] cmdData - input sensor data |
| 313 | * @param[in] sensorInfo - sensor d-bus info |
| 314 | * @return a IPMI error code |
| 315 | */ |
| 316 | inline ipmi_ret_t eventdata1(const SetSensorReadingReq& cmdData, |
| 317 | const Info& sensorInfo) |
| 318 | { |
| 319 | return eventdata(cmdData, sensorInfo, cmdData.eventData1); |
| 320 | } |
| 321 | |
| 322 | /** @brief Update d-bus based on eventdata2 type sensor data |
| 323 | * @param[in] cmdData - input sensor data |
| 324 | * @param[in] sensorInfo - sensor d-bus info |
| 325 | * @return a IPMI error code |
| 326 | */ |
| 327 | inline ipmi_ret_t eventdata2(const SetSensorReadingReq& cmdData, |
| 328 | const Info& sensorInfo) |
| 329 | { |
| 330 | return eventdata(cmdData, sensorInfo, cmdData.eventData2); |
| 331 | } |
| 332 | |
| 333 | /** @brief Update d-bus based on eventdata3 type sensor data |
| 334 | * @param[in] cmdData - input sensor data |
| 335 | * @param[in] sensorInfo - sensor d-bus info |
| 336 | * @return a IPMI error code |
| 337 | */ |
| 338 | inline ipmi_ret_t eventdata3(const SetSensorReadingReq& cmdData, |
| 339 | const Info& sensorInfo) |
| 340 | { |
| 341 | return eventdata(cmdData, sensorInfo, cmdData.eventData3); |
| 342 | } |
Dhruvaraj Subhashchandran | e0af720 | 2017-07-12 06:35:20 -0500 | [diff] [blame] | 343 | |
| 344 | }//namespace set |
| 345 | |
| 346 | namespace notify |
| 347 | { |
| 348 | |
| 349 | /** @brief Make a DBus message for a Dbus call |
| 350 | * @param[in] updateInterface - Interface name |
| 351 | * @param[in] sensorPath - Path of the sensor |
| 352 | * @param[in] command - command to be executed |
| 353 | * @param[in] sensorInterface - DBus interface of sensor |
| 354 | * @return a dbus message |
| 355 | */ |
| 356 | IpmiUpdateData makeDbusMsg(const std::string& updateInterface, |
| 357 | const std::string& sensorPath, |
| 358 | const std::string& command, |
| 359 | const std::string& sensorInterface); |
| 360 | |
Deepak Kodihalli | 1bb0d38 | 2017-08-12 02:01:27 -0500 | [diff] [blame] | 361 | /** @brief Update d-bus based on assertion type sensor data |
Dhruvaraj Subhashchandran | e0af720 | 2017-07-12 06:35:20 -0500 | [diff] [blame] | 362 | * @param[in] interfaceMap - sensor interface |
Dhruvaraj Subhashchandran | e0af720 | 2017-07-12 06:35:20 -0500 | [diff] [blame] | 363 | * @param[in] cmdData - input sensor data |
Deepak Kodihalli | 1bb0d38 | 2017-08-12 02:01:27 -0500 | [diff] [blame] | 364 | * @param[in] sensorInfo - sensor d-bus info |
Dhruvaraj Subhashchandran | e0af720 | 2017-07-12 06:35:20 -0500 | [diff] [blame] | 365 | * @return a IPMI error code |
| 366 | */ |
Deepak Kodihalli | 1bb0d38 | 2017-08-12 02:01:27 -0500 | [diff] [blame] | 367 | ipmi_ret_t assertion(const SetSensorReadingReq& cmdData, |
| 368 | const Info& sensorInfo); |
Dhruvaraj Subhashchandran | e0af720 | 2017-07-12 06:35:20 -0500 | [diff] [blame] | 369 | |
| 370 | }//namespace notify |
Tom Joseph | 816e92b | 2017-09-06 19:23:00 +0530 | [diff] [blame] | 371 | |
| 372 | namespace inventory |
| 373 | { |
| 374 | |
| 375 | namespace get |
| 376 | { |
| 377 | |
| 378 | /** |
| 379 | * @brief Map the Dbus info to sensor's assertion status in the Get sensor |
| 380 | * reading command response. |
| 381 | * |
| 382 | * @param[in] sensorInfo - Dbus info related to sensor. |
| 383 | * |
| 384 | * @return Response for get sensor reading command. |
| 385 | */ |
| 386 | GetSensorResponse assertion(const Info& sensorInfo); |
| 387 | |
| 388 | } // namespace get |
| 389 | |
| 390 | } // namespace inventory |
Dhruvaraj Subhashchandran | e0af720 | 2017-07-12 06:35:20 -0500 | [diff] [blame] | 391 | }//namespace sensor |
| 392 | }//namespace ipmi |