Dhruvaraj Subhashchandran | e0af720 | 2017-07-12 06:35:20 -0500 | [diff] [blame] | 1 | #include <bitset> |
Tom Joseph | 816e92b | 2017-09-06 19:23:00 +0530 | [diff] [blame^] | 2 | #include <experimental/filesystem> |
Dhruvaraj Subhashchandran | e0af720 | 2017-07-12 06:35:20 -0500 | [diff] [blame] | 3 | #include <phosphor-logging/elog-errors.hpp> |
| 4 | #include <phosphor-logging/log.hpp> |
| 5 | #include "xyz/openbmc_project/Common/error.hpp" |
| 6 | #include "types.hpp" |
Tom Joseph | 816e92b | 2017-09-06 19:23:00 +0530 | [diff] [blame^] | 7 | #include "sensorhandler.h" |
Dhruvaraj Subhashchandran | e0af720 | 2017-07-12 06:35:20 -0500 | [diff] [blame] | 8 | #include "sensordatahandler.hpp" |
Tom Joseph | 816e92b | 2017-09-06 19:23:00 +0530 | [diff] [blame^] | 9 | #include "utils.hpp" |
Dhruvaraj Subhashchandran | e0af720 | 2017-07-12 06:35:20 -0500 | [diff] [blame] | 10 | |
| 11 | namespace ipmi |
| 12 | { |
| 13 | namespace sensor |
| 14 | { |
| 15 | |
| 16 | using namespace phosphor::logging; |
| 17 | using InternalFailure = |
| 18 | sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure; |
| 19 | |
| 20 | static constexpr auto MAPPER_BUSNAME = "xyz.openbmc_project.ObjectMapper"; |
| 21 | static constexpr auto MAPPER_PATH = "/xyz/openbmc_project/object_mapper"; |
| 22 | static constexpr auto MAPPER_INTERFACE = "xyz.openbmc_project.ObjectMapper"; |
| 23 | |
| 24 | /** @brief get the D-Bus service and service path |
| 25 | * @param[in] bus - The Dbus bus object |
| 26 | * @param[in] interface - interface to the service |
| 27 | * @param[in] path - interested path in the list of objects |
| 28 | * @return pair of service path and service |
| 29 | */ |
| 30 | ServicePath getServiceAndPath(sdbusplus::bus::bus& bus, |
| 31 | const std::string& interface, |
| 32 | const std::string& path) |
| 33 | { |
| 34 | auto depth = 0; |
| 35 | auto mapperCall = bus.new_method_call(MAPPER_BUSNAME, |
| 36 | MAPPER_PATH, |
| 37 | MAPPER_INTERFACE, |
| 38 | "GetSubTree"); |
| 39 | mapperCall.append("/"); |
| 40 | mapperCall.append(depth); |
| 41 | mapperCall.append(std::vector<Interface>({interface})); |
| 42 | |
| 43 | auto mapperResponseMsg = bus.call(mapperCall); |
| 44 | if (mapperResponseMsg.is_method_error()) |
| 45 | { |
Dhruvaraj Subhashchandran | 18e9999 | 2017-08-09 09:10:47 -0500 | [diff] [blame] | 46 | log<level::ERR>("Mapper GetSubTree failed", |
| 47 | entry("PATH=%s", path), |
| 48 | entry("INTERFACE=%s", interface)); |
Dhruvaraj Subhashchandran | e0af720 | 2017-07-12 06:35:20 -0500 | [diff] [blame] | 49 | elog<InternalFailure>(); |
| 50 | } |
| 51 | |
| 52 | MapperResponseType mapperResponse; |
| 53 | mapperResponseMsg.read(mapperResponse); |
| 54 | if (mapperResponse.empty()) |
| 55 | { |
Dhruvaraj Subhashchandran | 18e9999 | 2017-08-09 09:10:47 -0500 | [diff] [blame] | 56 | log<level::ERR>("Invalid mapper response", |
| 57 | entry("PATH=%s", path), |
| 58 | entry("INTERFACE=%s", interface)); |
Dhruvaraj Subhashchandran | e0af720 | 2017-07-12 06:35:20 -0500 | [diff] [blame] | 59 | elog<InternalFailure>(); |
| 60 | } |
| 61 | |
| 62 | if (path.empty()) |
| 63 | { |
| 64 | //Get the first one if the path is not in list. |
| 65 | return std::make_pair(mapperResponse.begin()->first, |
| 66 | mapperResponse.begin()->second.begin()->first); |
| 67 | } |
| 68 | const auto& iter = mapperResponse.find(path); |
| 69 | if (iter == mapperResponse.end()) |
| 70 | { |
Dhruvaraj Subhashchandran | 18e9999 | 2017-08-09 09:10:47 -0500 | [diff] [blame] | 71 | log<level::ERR>("Coudn't find d-bus path", |
| 72 | entry("PATH=%s", path), |
| 73 | entry("INTERFACE=%s", interface)); |
Dhruvaraj Subhashchandran | e0af720 | 2017-07-12 06:35:20 -0500 | [diff] [blame] | 74 | elog<InternalFailure>(); |
| 75 | } |
| 76 | return std::make_pair(iter->first, iter->second.begin()->first); |
| 77 | } |
| 78 | |
| 79 | AssertionSet getAssertionSet(const SetSensorReadingReq& cmdData) |
| 80 | { |
| 81 | Assertion assertionStates = |
| 82 | (static_cast<Assertion>(cmdData.assertOffset8_14)) << 8 | |
| 83 | cmdData.assertOffset0_7; |
| 84 | Deassertion deassertionStates = |
| 85 | (static_cast<Deassertion>(cmdData.deassertOffset8_14)) << 8 | |
| 86 | cmdData.deassertOffset0_7; |
| 87 | return std::make_pair(assertionStates, deassertionStates); |
| 88 | } |
| 89 | |
| 90 | ipmi_ret_t updateToDbus(IpmiUpdateData& msg) |
| 91 | { |
| 92 | sdbusplus::bus::bus bus{ipmid_get_sd_bus_connection()}; |
| 93 | try |
| 94 | { |
| 95 | auto serviceResponseMsg = bus.call(msg); |
| 96 | if (serviceResponseMsg.is_method_error()) |
| 97 | { |
| 98 | log<level::ERR>("Error in D-Bus call"); |
| 99 | return IPMI_CC_UNSPECIFIED_ERROR; |
| 100 | } |
| 101 | } |
| 102 | catch (InternalFailure& e) |
| 103 | { |
| 104 | commit<InternalFailure>(); |
| 105 | return IPMI_CC_UNSPECIFIED_ERROR; |
| 106 | } |
| 107 | return IPMI_CC_OK; |
| 108 | } |
| 109 | |
Tom Joseph | 816e92b | 2017-09-06 19:23:00 +0530 | [diff] [blame^] | 110 | namespace get |
| 111 | { |
| 112 | |
| 113 | GetSensorResponse mapDbusToAssertion(const Info& sensorInfo, |
| 114 | const InstancePath& path, |
| 115 | const DbusInterface& interface) |
| 116 | { |
| 117 | sdbusplus::bus::bus bus{ipmid_get_sd_bus_connection()}; |
| 118 | GetSensorResponse response {}; |
| 119 | auto responseData = reinterpret_cast<GetReadingResponse*>(response.data()); |
| 120 | |
| 121 | auto service = ipmi::getService(bus, interface, path); |
| 122 | |
| 123 | const auto& interfaceList = sensorInfo.propertyInterfaces; |
| 124 | |
| 125 | for (const auto& interface : interfaceList) |
| 126 | { |
| 127 | for (const auto& property : interface.second) |
| 128 | { |
| 129 | auto propValue = ipmi::getDbusProperty(bus, |
| 130 | service, |
| 131 | path, |
| 132 | interface.first, |
| 133 | property.first); |
| 134 | |
| 135 | for (const auto& value : property.second) |
| 136 | { |
| 137 | if (propValue == value.second.assert) |
| 138 | { |
| 139 | setOffset(value.first, responseData); |
| 140 | break; |
| 141 | } |
| 142 | |
| 143 | } |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | return response; |
| 148 | } |
| 149 | |
| 150 | GetSensorResponse assertion(const Info& sensorInfo) |
| 151 | { |
| 152 | return mapDbusToAssertion(sensorInfo, |
| 153 | sensorInfo.sensorPath, |
| 154 | sensorInfo.sensorInterface); |
| 155 | } |
| 156 | |
| 157 | } //namespace get |
| 158 | |
Dhruvaraj Subhashchandran | e0af720 | 2017-07-12 06:35:20 -0500 | [diff] [blame] | 159 | namespace set |
| 160 | { |
| 161 | |
| 162 | IpmiUpdateData makeDbusMsg(const std::string& updateInterface, |
| 163 | const std::string& sensorPath, |
| 164 | const std::string& command, |
| 165 | const std::string& sensorInterface) |
| 166 | { |
| 167 | sdbusplus::bus::bus bus{ipmid_get_sd_bus_connection()}; |
| 168 | using namespace std::string_literals; |
| 169 | |
| 170 | std::string dbusService; |
| 171 | std::string dbusPath; |
| 172 | |
| 173 | std::tie(dbusPath, dbusService) = getServiceAndPath(bus, |
| 174 | sensorInterface, |
| 175 | sensorPath); |
| 176 | return bus.new_method_call(dbusService.c_str(), |
| 177 | dbusPath.c_str(), |
| 178 | updateInterface.c_str(), |
| 179 | command.c_str()); |
| 180 | } |
| 181 | |
Deepak Kodihalli | 1bb0d38 | 2017-08-12 02:01:27 -0500 | [diff] [blame] | 182 | ipmi_ret_t eventdata(const SetSensorReadingReq& cmdData, |
| 183 | const Info& sensorInfo, |
| 184 | uint8_t data) |
Dhruvaraj Subhashchandran | e0af720 | 2017-07-12 06:35:20 -0500 | [diff] [blame] | 185 | { |
Deepak Kodihalli | 1bb0d38 | 2017-08-12 02:01:27 -0500 | [diff] [blame] | 186 | auto msg = makeDbusMsg( |
| 187 | "org.freedesktop.DBus.Properties", |
| 188 | sensorInfo.sensorPath, |
| 189 | "Set", |
| 190 | sensorInfo.sensorInterface); |
| 191 | |
| 192 | const auto& interface = sensorInfo.propertyInterfaces.begin(); |
Dhruvaraj Subhashchandran | e0af720 | 2017-07-12 06:35:20 -0500 | [diff] [blame] | 193 | msg.append(interface->first); |
| 194 | for (const auto& property : interface->second) |
| 195 | { |
| 196 | msg.append(property.first); |
| 197 | const auto& iter = property.second.find(data); |
| 198 | if (iter == property.second.end()) |
| 199 | { |
| 200 | log<level::ERR>("Invalid event data"); |
| 201 | return IPMI_CC_PARM_OUT_OF_RANGE; |
| 202 | } |
| 203 | msg.append(iter->second.assert); |
| 204 | } |
Deepak Kodihalli | 1bb0d38 | 2017-08-12 02:01:27 -0500 | [diff] [blame] | 205 | return updateToDbus(msg); |
Dhruvaraj Subhashchandran | e0af720 | 2017-07-12 06:35:20 -0500 | [diff] [blame] | 206 | } |
| 207 | |
Deepak Kodihalli | 1bb0d38 | 2017-08-12 02:01:27 -0500 | [diff] [blame] | 208 | ipmi_ret_t assertion(const SetSensorReadingReq& cmdData, |
| 209 | const Info& sensorInfo) |
Dhruvaraj Subhashchandran | e0af720 | 2017-07-12 06:35:20 -0500 | [diff] [blame] | 210 | { |
Deepak Kodihalli | 1bb0d38 | 2017-08-12 02:01:27 -0500 | [diff] [blame] | 211 | auto msg = makeDbusMsg( |
| 212 | "org.freedesktop.DBus.Properties", |
| 213 | sensorInfo.sensorPath, |
| 214 | "Set", |
| 215 | sensorInfo.sensorInterface); |
Dhruvaraj Subhashchandran | e0af720 | 2017-07-12 06:35:20 -0500 | [diff] [blame] | 216 | |
Dhruvaraj Subhashchandran | e0af720 | 2017-07-12 06:35:20 -0500 | [diff] [blame] | 217 | std::bitset<16> assertionSet(getAssertionSet(cmdData).first); |
| 218 | std::bitset<16> deassertionSet(getAssertionSet(cmdData).second); |
| 219 | |
Deepak Kodihalli | 1bb0d38 | 2017-08-12 02:01:27 -0500 | [diff] [blame] | 220 | const auto& interface = sensorInfo.propertyInterfaces.begin(); |
Dhruvaraj Subhashchandran | e0af720 | 2017-07-12 06:35:20 -0500 | [diff] [blame] | 221 | msg.append(interface->first); |
| 222 | for (const auto& property : interface->second) |
| 223 | { |
| 224 | msg.append(property.first); |
| 225 | for (const auto& value : property.second) |
| 226 | { |
| 227 | if (assertionSet.test(value.first)) |
| 228 | { |
| 229 | msg.append(value.second.assert); |
| 230 | } |
| 231 | if (deassertionSet.test(value.first)) |
| 232 | { |
| 233 | msg.append(value.second.deassert); |
| 234 | } |
| 235 | } |
| 236 | } |
Deepak Kodihalli | 1bb0d38 | 2017-08-12 02:01:27 -0500 | [diff] [blame] | 237 | return updateToDbus(msg); |
Dhruvaraj Subhashchandran | e0af720 | 2017-07-12 06:35:20 -0500 | [diff] [blame] | 238 | } |
Deepak Kodihalli | 1bb0d38 | 2017-08-12 02:01:27 -0500 | [diff] [blame] | 239 | |
Dhruvaraj Subhashchandran | e0af720 | 2017-07-12 06:35:20 -0500 | [diff] [blame] | 240 | }//namespace set |
| 241 | |
| 242 | namespace notify |
| 243 | { |
| 244 | |
| 245 | IpmiUpdateData makeDbusMsg(const std::string& updateInterface, |
| 246 | const std::string& sensorPath, |
| 247 | const std::string& command, |
| 248 | const std::string& sensorInterface) |
| 249 | { |
| 250 | sdbusplus::bus::bus bus{ipmid_get_sd_bus_connection()}; |
| 251 | using namespace std::string_literals; |
| 252 | |
| 253 | std::string dbusService; |
| 254 | std::string dbusPath; |
| 255 | |
| 256 | std::tie(dbusPath, dbusService) = getServiceAndPath(bus, |
| 257 | updateInterface); |
| 258 | |
| 259 | return bus.new_method_call(dbusService.c_str(), |
| 260 | dbusPath.c_str(), |
| 261 | updateInterface.c_str(), |
| 262 | command.c_str()); |
| 263 | } |
| 264 | |
Deepak Kodihalli | 1bb0d38 | 2017-08-12 02:01:27 -0500 | [diff] [blame] | 265 | ipmi_ret_t assertion(const SetSensorReadingReq& cmdData, |
| 266 | const Info& sensorInfo) |
Dhruvaraj Subhashchandran | e0af720 | 2017-07-12 06:35:20 -0500 | [diff] [blame] | 267 | { |
Deepak Kodihalli | 1bb0d38 | 2017-08-12 02:01:27 -0500 | [diff] [blame] | 268 | auto msg = makeDbusMsg( |
| 269 | sensorInfo.sensorInterface, |
| 270 | sensorInfo.sensorPath, |
| 271 | "Notify", |
| 272 | sensorInfo.sensorInterface); |
| 273 | |
Dhruvaraj Subhashchandran | e0af720 | 2017-07-12 06:35:20 -0500 | [diff] [blame] | 274 | std::bitset<16> assertionSet(getAssertionSet(cmdData).first); |
| 275 | std::bitset<16> deassertionSet(getAssertionSet(cmdData).second); |
| 276 | ipmi::sensor::ObjectMap objects; |
| 277 | ipmi::sensor::InterfaceMap interfaces; |
Deepak Kodihalli | 1bb0d38 | 2017-08-12 02:01:27 -0500 | [diff] [blame] | 278 | for (const auto& interface : sensorInfo.propertyInterfaces) |
Dhruvaraj Subhashchandran | e0af720 | 2017-07-12 06:35:20 -0500 | [diff] [blame] | 279 | { |
| 280 | for (const auto& property : interface.second) |
| 281 | { |
| 282 | ipmi::sensor::PropertyMap props; |
| 283 | bool valid = false; |
| 284 | for (const auto& value : property.second) |
| 285 | { |
| 286 | if (assertionSet.test(value.first)) |
| 287 | { |
| 288 | props.emplace(property.first, value.second.assert); |
| 289 | valid = true; |
| 290 | } |
| 291 | else if (deassertionSet.test(value.first)) |
| 292 | { |
| 293 | props.emplace(property.first, value.second.deassert); |
| 294 | valid = true; |
| 295 | } |
| 296 | } |
| 297 | if (valid) |
| 298 | { |
| 299 | interfaces.emplace(interface.first, std::move(props)); |
| 300 | } |
| 301 | } |
| 302 | } |
Deepak Kodihalli | 1bb0d38 | 2017-08-12 02:01:27 -0500 | [diff] [blame] | 303 | objects.emplace(sensorInfo.sensorPath, std::move(interfaces)); |
Dhruvaraj Subhashchandran | e0af720 | 2017-07-12 06:35:20 -0500 | [diff] [blame] | 304 | msg.append(std::move(objects)); |
Deepak Kodihalli | 1bb0d38 | 2017-08-12 02:01:27 -0500 | [diff] [blame] | 305 | return updateToDbus(msg); |
Dhruvaraj Subhashchandran | e0af720 | 2017-07-12 06:35:20 -0500 | [diff] [blame] | 306 | } |
Tom Joseph | 816e92b | 2017-09-06 19:23:00 +0530 | [diff] [blame^] | 307 | |
Dhruvaraj Subhashchandran | e0af720 | 2017-07-12 06:35:20 -0500 | [diff] [blame] | 308 | }//namespace notify |
Tom Joseph | 816e92b | 2017-09-06 19:23:00 +0530 | [diff] [blame^] | 309 | |
| 310 | namespace inventory |
| 311 | { |
| 312 | |
| 313 | namespace get |
| 314 | { |
| 315 | |
| 316 | GetSensorResponse assertion(const Info& sensorInfo) |
| 317 | { |
| 318 | namespace fs = std::experimental::filesystem; |
| 319 | |
| 320 | fs::path path{ipmi::sensor::inventoryRoot}; |
| 321 | path += sensorInfo.sensorPath; |
| 322 | |
| 323 | return ipmi::sensor::get::mapDbusToAssertion( |
| 324 | sensorInfo, |
| 325 | path.string(), |
| 326 | sensorInfo.propertyInterfaces.begin()->first); |
| 327 | } |
| 328 | |
| 329 | } //namespace get |
| 330 | |
| 331 | } // namespace inventory |
Dhruvaraj Subhashchandran | e0af720 | 2017-07-12 06:35:20 -0500 | [diff] [blame] | 332 | }//namespace sensor |
| 333 | }//namespace ipmi |