Deepak Kodihalli | 1bb0d38 | 2017-08-12 02:01:27 -0500 | [diff] [blame] | 1 | #pragma once |
| 2 | |
Dhruvaraj Subhashchandran | e0af720 | 2017-07-12 06:35:20 -0500 | [diff] [blame] | 3 | #include "types.hpp" |
| 4 | #include "host-ipmid/ipmid-api.h" |
| 5 | |
| 6 | namespace ipmi |
| 7 | { |
| 8 | namespace sensor |
| 9 | { |
| 10 | |
| 11 | using Assertion = uint16_t; |
| 12 | using Deassertion = uint16_t; |
| 13 | using AssertionSet = std::pair<Assertion, Deassertion>; |
| 14 | |
| 15 | using Service = std::string; |
| 16 | using Path = std::string; |
| 17 | using Interface = std::string; |
| 18 | |
| 19 | using ServicePath = std::pair<Path, Service>; |
| 20 | |
| 21 | using Interfaces = std::vector<Interface>; |
| 22 | |
| 23 | using MapperResponseType = std::map<Path, std::map<Service, Interfaces>>; |
| 24 | |
| 25 | /** @brief get the D-Bus service and service path |
| 26 | * @param[in] bus - The Dbus bus object |
| 27 | * @param[in] interface - interface to the service |
| 28 | * @param[in] path - interested path in the list of objects |
| 29 | * @return pair of service path and service |
| 30 | */ |
| 31 | ServicePath getServiceAndPath(sdbusplus::bus::bus& bus, |
| 32 | const std::string& interface, |
| 33 | const std::string& path = std::string()); |
| 34 | |
| 35 | /** @brief Make assertion set from input data |
| 36 | * @param[in] cmdData - Input sensor data |
| 37 | * @return pair of assertion and deassertion set |
| 38 | */ |
| 39 | AssertionSet getAssertionSet(const SetSensorReadingReq& cmdData); |
| 40 | |
| 41 | /** @brief send the message to DBus |
| 42 | * @param[in] msg - message to send |
| 43 | * @return failure status in IPMI error code |
| 44 | */ |
Dhruvaraj Subhashchandran | 2a444d0 | 2017-08-07 01:45:14 -0500 | [diff] [blame] | 45 | ipmi_ret_t updateToDbus(IpmiUpdateData& msg); |
Dhruvaraj Subhashchandran | e0af720 | 2017-07-12 06:35:20 -0500 | [diff] [blame] | 46 | |
Tom Joseph | 816e92b | 2017-09-06 19:23:00 +0530 | [diff] [blame] | 47 | namespace get |
| 48 | { |
| 49 | |
| 50 | /** |
| 51 | * @brief Helper function to map the dbus info to sensor's assertion status |
| 52 | * for the get sensor reading command. |
| 53 | * |
| 54 | * @param[in] sensorInfo - Dbus info related to sensor. |
| 55 | * @param[in] path - Dbus object path. |
| 56 | * @param[in] interface - Dbus interface. |
| 57 | * |
| 58 | * @return Response for get sensor reading command. |
| 59 | */ |
| 60 | GetSensorResponse mapDbusToAssertion(const Info& sensorInfo, |
| 61 | const InstancePath& path, |
| 62 | const DbusInterface& interface); |
| 63 | |
| 64 | /** |
| 65 | * @brief Map the Dbus info to sensor's assertion status in the Get sensor |
| 66 | * reading command response. |
| 67 | * |
| 68 | * @param[in] sensorInfo - Dbus info related to sensor. |
| 69 | * |
| 70 | * @return Response for get sensor reading command. |
| 71 | */ |
| 72 | GetSensorResponse assertion(const Info& sensorInfo); |
| 73 | |
Tom Joseph | e4014fc | 2017-09-06 23:57:36 +0530 | [diff] [blame^] | 74 | /** |
| 75 | * @brief Maps the Dbus info to the reading field in the Get sensor reading |
| 76 | * command response. |
| 77 | * |
| 78 | * @param[in] sensorInfo - Dbus info related to sensor. |
| 79 | * |
| 80 | * @return Response for get sensor reading command. |
| 81 | */ |
| 82 | GetSensorResponse eventdata2(const Info& sensorInfo); |
| 83 | |
Tom Joseph | 816e92b | 2017-09-06 19:23:00 +0530 | [diff] [blame] | 84 | } //namespace get |
| 85 | |
Dhruvaraj Subhashchandran | e0af720 | 2017-07-12 06:35:20 -0500 | [diff] [blame] | 86 | namespace set |
| 87 | { |
| 88 | |
| 89 | /** @brief Make a DBus message for a Dbus call |
| 90 | * @param[in] updateInterface - Interface name |
| 91 | * @param[in] sensorPath - Path of the sensor |
| 92 | * @param[in] command - command to be executed |
| 93 | * @param[in] sensorInterface - DBus interface of sensor |
| 94 | * @return a dbus message |
| 95 | */ |
| 96 | IpmiUpdateData makeDbusMsg(const std::string& updateInterface, |
| 97 | const std::string& sensorPath, |
| 98 | const std::string& command, |
| 99 | const std::string& sensorInterface); |
| 100 | |
Deepak Kodihalli | 1bb0d38 | 2017-08-12 02:01:27 -0500 | [diff] [blame] | 101 | /** @brief Update d-bus based on assertion type sensor data |
Dhruvaraj Subhashchandran | e0af720 | 2017-07-12 06:35:20 -0500 | [diff] [blame] | 102 | * @param[in] cmdData - input sensor data |
Deepak Kodihalli | 1bb0d38 | 2017-08-12 02:01:27 -0500 | [diff] [blame] | 103 | * @param[in] sensorInfo - sensor d-bus info |
Dhruvaraj Subhashchandran | e0af720 | 2017-07-12 06:35:20 -0500 | [diff] [blame] | 104 | * @return a IPMI error code |
| 105 | */ |
Deepak Kodihalli | 1bb0d38 | 2017-08-12 02:01:27 -0500 | [diff] [blame] | 106 | ipmi_ret_t assertion(const SetSensorReadingReq& cmdData, |
| 107 | const Info& sensorInfo); |
Dhruvaraj Subhashchandran | e0af720 | 2017-07-12 06:35:20 -0500 | [diff] [blame] | 108 | |
Deepak Kodihalli | 1bb0d38 | 2017-08-12 02:01:27 -0500 | [diff] [blame] | 109 | /** @brief Update d-bus based on a reading assertion |
| 110 | * @tparam T - type of d-bus property mapping this sensor |
| 111 | * @param[in] cmdData - input sensor data |
| 112 | * @param[in] sensorInfo - sensor d-bus info |
Dhruvaraj Subhashchandran | e0af720 | 2017-07-12 06:35:20 -0500 | [diff] [blame] | 113 | * @return a IPMI error code |
| 114 | */ |
Deepak Kodihalli | 1bb0d38 | 2017-08-12 02:01:27 -0500 | [diff] [blame] | 115 | template<typename T> |
| 116 | ipmi_ret_t readingAssertion(const SetSensorReadingReq& cmdData, |
| 117 | const Info& sensorInfo) |
| 118 | { |
| 119 | auto msg = makeDbusMsg( |
| 120 | "org.freedesktop.DBus.Properties", |
| 121 | sensorInfo.sensorPath, |
| 122 | "Set", |
| 123 | sensorInfo.sensorInterface); |
Dhruvaraj Subhashchandran | e0af720 | 2017-07-12 06:35:20 -0500 | [diff] [blame] | 124 | |
Deepak Kodihalli | 1bb0d38 | 2017-08-12 02:01:27 -0500 | [diff] [blame] | 125 | const auto& interface = sensorInfo.propertyInterfaces.begin(); |
| 126 | msg.append(interface->first); |
| 127 | for (const auto& property : interface->second) |
| 128 | { |
| 129 | msg.append(property.first); |
| 130 | sdbusplus::message::variant<T> value = |
| 131 | (cmdData.assertOffset8_14 << 8) | cmdData.assertOffset0_7; |
| 132 | msg.append(value); |
| 133 | } |
| 134 | return updateToDbus(msg); |
| 135 | } |
| 136 | |
Emily Shaffer | cc941e1 | 2017-06-14 13:06:26 -0700 | [diff] [blame] | 137 | /** @brief Update d-bus based on a discrete reading |
| 138 | * @param[in] cmdData - input sensor data |
| 139 | * @param[in] sensorInfo - sensor d-bus info |
| 140 | * @return an IPMI error code |
| 141 | */ |
| 142 | template<typename T> |
| 143 | ipmi_ret_t readingData(const SetSensorReadingReq& cmdData, |
| 144 | const Info& sensorInfo) |
| 145 | { |
| 146 | auto msg = makeDbusMsg( |
| 147 | "org.freedesktop.DBus.Properties", |
| 148 | sensorInfo.sensorPath, |
| 149 | "Set", |
| 150 | sensorInfo.sensorInterface); |
| 151 | |
| 152 | const auto& interface = sensorInfo.propertyInterfaces.begin(); |
| 153 | msg.append(interface->first); |
| 154 | |
| 155 | ipmi::sensor::Multiplier m = sensorInfo.coefficientM; |
| 156 | if (0 == m) |
| 157 | { |
| 158 | m = 1; // Avoid * 0 |
| 159 | } |
| 160 | |
| 161 | // TODO: Refactor this into a generated function depending on the type |
| 162 | // of conversion for the value between IPMI and dbus. |
| 163 | T raw_value = (m * cmdData.reading) + sensorInfo.scaledOffset; |
| 164 | |
| 165 | for (const auto& property : interface->second) |
| 166 | { |
| 167 | msg.append(property.first); |
| 168 | sdbusplus::message::variant<T> value = raw_value; |
| 169 | msg.append(value); |
| 170 | } |
| 171 | return updateToDbus(msg); |
| 172 | } |
Deepak Kodihalli | 1bb0d38 | 2017-08-12 02:01:27 -0500 | [diff] [blame] | 173 | |
| 174 | /** @brief Update d-bus based on eventdata type sensor data |
| 175 | * @param[in] cmdData - input sensor data |
| 176 | * @param[in] sensorInfo - sensor d-bus info |
Dhruvaraj Subhashchandran | e0af720 | 2017-07-12 06:35:20 -0500 | [diff] [blame] | 177 | * @return a IPMI error code |
| 178 | */ |
Deepak Kodihalli | 1bb0d38 | 2017-08-12 02:01:27 -0500 | [diff] [blame] | 179 | ipmi_ret_t eventdata(const SetSensorReadingReq& cmdData, |
| 180 | const Info& sensorInfo, |
| 181 | uint8_t data); |
| 182 | |
| 183 | /** @brief Update d-bus based on eventdata1 type sensor data |
| 184 | * @param[in] cmdData - input sensor data |
| 185 | * @param[in] sensorInfo - sensor d-bus info |
| 186 | * @return a IPMI error code |
| 187 | */ |
| 188 | inline ipmi_ret_t eventdata1(const SetSensorReadingReq& cmdData, |
| 189 | const Info& sensorInfo) |
| 190 | { |
| 191 | return eventdata(cmdData, sensorInfo, cmdData.eventData1); |
| 192 | } |
| 193 | |
| 194 | /** @brief Update d-bus based on eventdata2 type sensor data |
| 195 | * @param[in] cmdData - input sensor data |
| 196 | * @param[in] sensorInfo - sensor d-bus info |
| 197 | * @return a IPMI error code |
| 198 | */ |
| 199 | inline ipmi_ret_t eventdata2(const SetSensorReadingReq& cmdData, |
| 200 | const Info& sensorInfo) |
| 201 | { |
| 202 | return eventdata(cmdData, sensorInfo, cmdData.eventData2); |
| 203 | } |
| 204 | |
| 205 | /** @brief Update d-bus based on eventdata3 type sensor data |
| 206 | * @param[in] cmdData - input sensor data |
| 207 | * @param[in] sensorInfo - sensor d-bus info |
| 208 | * @return a IPMI error code |
| 209 | */ |
| 210 | inline ipmi_ret_t eventdata3(const SetSensorReadingReq& cmdData, |
| 211 | const Info& sensorInfo) |
| 212 | { |
| 213 | return eventdata(cmdData, sensorInfo, cmdData.eventData3); |
| 214 | } |
Dhruvaraj Subhashchandran | e0af720 | 2017-07-12 06:35:20 -0500 | [diff] [blame] | 215 | |
| 216 | }//namespace set |
| 217 | |
| 218 | namespace notify |
| 219 | { |
| 220 | |
| 221 | /** @brief Make a DBus message for a Dbus call |
| 222 | * @param[in] updateInterface - Interface name |
| 223 | * @param[in] sensorPath - Path of the sensor |
| 224 | * @param[in] command - command to be executed |
| 225 | * @param[in] sensorInterface - DBus interface of sensor |
| 226 | * @return a dbus message |
| 227 | */ |
| 228 | IpmiUpdateData makeDbusMsg(const std::string& updateInterface, |
| 229 | const std::string& sensorPath, |
| 230 | const std::string& command, |
| 231 | const std::string& sensorInterface); |
| 232 | |
Deepak Kodihalli | 1bb0d38 | 2017-08-12 02:01:27 -0500 | [diff] [blame] | 233 | /** @brief Update d-bus based on assertion type sensor data |
Dhruvaraj Subhashchandran | e0af720 | 2017-07-12 06:35:20 -0500 | [diff] [blame] | 234 | * @param[in] interfaceMap - sensor interface |
Dhruvaraj Subhashchandran | e0af720 | 2017-07-12 06:35:20 -0500 | [diff] [blame] | 235 | * @param[in] cmdData - input sensor data |
Deepak Kodihalli | 1bb0d38 | 2017-08-12 02:01:27 -0500 | [diff] [blame] | 236 | * @param[in] sensorInfo - sensor d-bus info |
Dhruvaraj Subhashchandran | e0af720 | 2017-07-12 06:35:20 -0500 | [diff] [blame] | 237 | * @return a IPMI error code |
| 238 | */ |
Deepak Kodihalli | 1bb0d38 | 2017-08-12 02:01:27 -0500 | [diff] [blame] | 239 | ipmi_ret_t assertion(const SetSensorReadingReq& cmdData, |
| 240 | const Info& sensorInfo); |
Dhruvaraj Subhashchandran | e0af720 | 2017-07-12 06:35:20 -0500 | [diff] [blame] | 241 | |
| 242 | }//namespace notify |
Tom Joseph | 816e92b | 2017-09-06 19:23:00 +0530 | [diff] [blame] | 243 | |
| 244 | namespace inventory |
| 245 | { |
| 246 | |
| 247 | namespace get |
| 248 | { |
| 249 | |
| 250 | /** |
| 251 | * @brief Map the Dbus info to sensor's assertion status in the Get sensor |
| 252 | * reading command response. |
| 253 | * |
| 254 | * @param[in] sensorInfo - Dbus info related to sensor. |
| 255 | * |
| 256 | * @return Response for get sensor reading command. |
| 257 | */ |
| 258 | GetSensorResponse assertion(const Info& sensorInfo); |
| 259 | |
| 260 | } // namespace get |
| 261 | |
| 262 | } // namespace inventory |
Dhruvaraj Subhashchandran | e0af720 | 2017-07-12 06:35:20 -0500 | [diff] [blame] | 263 | }//namespace sensor |
| 264 | }//namespace ipmi |