Patrick Venture | 3a5071a | 2018-09-12 13:27:42 -0700 | [diff] [blame] | 1 | #include "sensordatahandler.hpp" |
| 2 | |
| 3 | #include "sensorhandler.hpp" |
Patrick Venture | 3a5071a | 2018-09-12 13:27:42 -0700 | [diff] [blame] | 4 | |
Dhruvaraj Subhashchandran | e0af720 | 2017-07-12 06:35:20 -0500 | [diff] [blame] | 5 | #include <bitset> |
Vernon Mauery | bdda800 | 2019-02-26 10:18:51 -0800 | [diff] [blame] | 6 | #include <filesystem> |
Vernon Mauery | 3325024 | 2019-03-12 16:49:26 -0700 | [diff] [blame] | 7 | #include <ipmid/types.hpp> |
Vernon Mauery | 6a98fe7 | 2019-03-11 15:57:48 -0700 | [diff] [blame] | 8 | #include <ipmid/utils.hpp> |
William A. Kennington III | 4c00802 | 2018-10-12 17:18:14 -0700 | [diff] [blame] | 9 | #include <optional> |
William A. Kennington III | 4c00802 | 2018-10-12 17:18:14 -0700 | [diff] [blame] | 10 | #include <sdbusplus/message/types.hpp> |
Patrick Venture | 3a5071a | 2018-09-12 13:27:42 -0700 | [diff] [blame] | 11 | #include <xyz/openbmc_project/Common/error.hpp> |
| 12 | |
Dhruvaraj Subhashchandran | e0af720 | 2017-07-12 06:35:20 -0500 | [diff] [blame] | 13 | namespace ipmi |
| 14 | { |
| 15 | namespace sensor |
| 16 | { |
| 17 | |
| 18 | using namespace phosphor::logging; |
| 19 | using InternalFailure = |
| 20 | sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure; |
| 21 | |
| 22 | static constexpr auto MAPPER_BUSNAME = "xyz.openbmc_project.ObjectMapper"; |
| 23 | static constexpr auto MAPPER_PATH = "/xyz/openbmc_project/object_mapper"; |
| 24 | static constexpr auto MAPPER_INTERFACE = "xyz.openbmc_project.ObjectMapper"; |
| 25 | |
| 26 | /** @brief get the D-Bus service and service path |
| 27 | * @param[in] bus - The Dbus bus object |
| 28 | * @param[in] interface - interface to the service |
| 29 | * @param[in] path - interested path in the list of objects |
| 30 | * @return pair of service path and service |
| 31 | */ |
| 32 | ServicePath getServiceAndPath(sdbusplus::bus::bus& bus, |
| 33 | const std::string& interface, |
| 34 | const std::string& path) |
| 35 | { |
| 36 | auto depth = 0; |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 37 | auto mapperCall = bus.new_method_call(MAPPER_BUSNAME, MAPPER_PATH, |
| 38 | MAPPER_INTERFACE, "GetSubTree"); |
Dhruvaraj Subhashchandran | e0af720 | 2017-07-12 06:35:20 -0500 | [diff] [blame] | 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", |
Joseph Reynolds | 510eb9c | 2018-05-30 11:51:28 -0500 | [diff] [blame] | 47 | entry("PATH=%s", path.c_str()), |
| 48 | entry("INTERFACE=%s", interface.c_str())); |
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", |
Joseph Reynolds | 510eb9c | 2018-05-30 11:51:28 -0500 | [diff] [blame] | 57 | entry("PATH=%s", path.c_str()), |
| 58 | entry("INTERFACE=%s", interface.c_str())); |
Dhruvaraj Subhashchandran | e0af720 | 2017-07-12 06:35:20 -0500 | [diff] [blame] | 59 | elog<InternalFailure>(); |
| 60 | } |
| 61 | |
| 62 | if (path.empty()) |
| 63 | { |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 64 | // Get the first one if the path is not in list. |
Dhruvaraj Subhashchandran | e0af720 | 2017-07-12 06:35:20 -0500 | [diff] [blame] | 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 | { |
Gunnar Mills | c9fa69e | 2018-04-08 16:35:25 -0500 | [diff] [blame] | 71 | log<level::ERR>("Couldn't find D-Bus path", |
Joseph Reynolds | 510eb9c | 2018-05-30 11:51:28 -0500 | [diff] [blame] | 72 | entry("PATH=%s", path.c_str()), |
| 73 | entry("INTERFACE=%s", interface.c_str())); |
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 | } |
Patrick Williams | a2ad2da | 2021-10-06 12:21:46 -0500 | [diff] [blame] | 102 | catch (const InternalFailure& e) |
Dhruvaraj Subhashchandran | e0af720 | 2017-07-12 06:35:20 -0500 | [diff] [blame] | 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 | |
Tom Joseph | b0adbcd | 2018-01-24 11:51:29 +0530 | [diff] [blame] | 113 | SensorName nameParentLeaf(const Info& sensorInfo) |
| 114 | { |
| 115 | const auto pos = sensorInfo.sensorPath.find_last_of('/'); |
| 116 | const auto leaf = sensorInfo.sensorPath.substr(pos + 1); |
| 117 | |
| 118 | const auto remaining = sensorInfo.sensorPath.substr(0, pos); |
| 119 | |
| 120 | const auto parentPos = remaining.find_last_of('/'); |
| 121 | auto parent = remaining.substr(parentPos + 1); |
| 122 | |
| 123 | parent += "_" + leaf; |
| 124 | return parent; |
| 125 | } |
| 126 | |
Tom Joseph | 816e92b | 2017-09-06 19:23:00 +0530 | [diff] [blame] | 127 | GetSensorResponse mapDbusToAssertion(const Info& sensorInfo, |
| 128 | const InstancePath& path, |
| 129 | const DbusInterface& interface) |
| 130 | { |
| 131 | sdbusplus::bus::bus bus{ipmid_get_sd_bus_connection()}; |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 132 | GetSensorResponse response{}; |
Tom Joseph | 816e92b | 2017-09-06 19:23:00 +0530 | [diff] [blame] | 133 | |
Jeremy Kerr | 3dc3558 | 2020-05-17 15:47:07 +0800 | [diff] [blame] | 134 | enableScanning(&response); |
| 135 | |
Tom Joseph | 816e92b | 2017-09-06 19:23:00 +0530 | [diff] [blame] | 136 | auto service = ipmi::getService(bus, interface, path); |
| 137 | |
| 138 | const auto& interfaceList = sensorInfo.propertyInterfaces; |
| 139 | |
| 140 | for (const auto& interface : interfaceList) |
| 141 | { |
| 142 | for (const auto& property : interface.second) |
| 143 | { |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 144 | auto propValue = ipmi::getDbusProperty( |
| 145 | bus, service, path, interface.first, property.first); |
Tom Joseph | 816e92b | 2017-09-06 19:23:00 +0530 | [diff] [blame] | 146 | |
Dhruvaraj Subhashchandran | e245e4e | 2017-10-03 03:58:05 -0500 | [diff] [blame] | 147 | for (const auto& value : std::get<OffsetValueMap>(property.second)) |
Tom Joseph | 816e92b | 2017-09-06 19:23:00 +0530 | [diff] [blame] | 148 | { |
| 149 | if (propValue == value.second.assert) |
| 150 | { |
Sui Chen | 4cc4255 | 2019-09-11 10:28:35 -0700 | [diff] [blame] | 151 | setOffset(value.first, &response); |
Tom Joseph | 816e92b | 2017-09-06 19:23:00 +0530 | [diff] [blame] | 152 | break; |
| 153 | } |
Tom Joseph | 816e92b | 2017-09-06 19:23:00 +0530 | [diff] [blame] | 154 | } |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | return response; |
| 159 | } |
| 160 | |
Lei YU | a8dc09b | 2021-10-13 18:06:46 +0800 | [diff] [blame] | 161 | GetSensorResponse mapDbusToEventdata2(const Info& sensorInfo) |
Tom Joseph | e4014fc | 2017-09-06 23:57:36 +0530 | [diff] [blame] | 162 | { |
| 163 | sdbusplus::bus::bus bus{ipmid_get_sd_bus_connection()}; |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 164 | GetSensorResponse response{}; |
Tom Joseph | e4014fc | 2017-09-06 23:57:36 +0530 | [diff] [blame] | 165 | |
Jeremy Kerr | 3dc3558 | 2020-05-17 15:47:07 +0800 | [diff] [blame] | 166 | enableScanning(&response); |
| 167 | |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 168 | auto service = ipmi::getService(bus, sensorInfo.sensorInterface, |
Tom Joseph | e4014fc | 2017-09-06 23:57:36 +0530 | [diff] [blame] | 169 | sensorInfo.sensorPath); |
| 170 | |
| 171 | const auto& interfaceList = sensorInfo.propertyInterfaces; |
| 172 | |
| 173 | for (const auto& interface : interfaceList) |
| 174 | { |
| 175 | for (const auto& property : interface.second) |
| 176 | { |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 177 | auto propValue = |
| 178 | ipmi::getDbusProperty(bus, service, sensorInfo.sensorPath, |
| 179 | interface.first, property.first); |
Tom Joseph | e4014fc | 2017-09-06 23:57:36 +0530 | [diff] [blame] | 180 | |
Dhruvaraj Subhashchandran | e245e4e | 2017-10-03 03:58:05 -0500 | [diff] [blame] | 181 | for (const auto& value : std::get<OffsetValueMap>(property.second)) |
Tom Joseph | e4014fc | 2017-09-06 23:57:36 +0530 | [diff] [blame] | 182 | { |
| 183 | if (propValue == value.second.assert) |
| 184 | { |
Sui Chen | 4cc4255 | 2019-09-11 10:28:35 -0700 | [diff] [blame] | 185 | setReading(value.first, &response); |
Tom Joseph | e4014fc | 2017-09-06 23:57:36 +0530 | [diff] [blame] | 186 | break; |
| 187 | } |
| 188 | } |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | return response; |
| 193 | } |
Lei YU | a8dc09b | 2021-10-13 18:06:46 +0800 | [diff] [blame] | 194 | |
| 195 | #ifndef FEATURE_SENSORS_CACHE |
| 196 | GetSensorResponse assertion(const Info& sensorInfo) |
| 197 | { |
| 198 | return mapDbusToAssertion(sensorInfo, sensorInfo.sensorPath, |
| 199 | sensorInfo.sensorInterface); |
| 200 | } |
| 201 | |
| 202 | GetSensorResponse eventdata2(const Info& sensorInfo) |
| 203 | { |
| 204 | return mapDbusToEventdata2(sensorInfo); |
| 205 | } |
Lei YU | 8c2c048 | 2021-09-16 17:28:28 +0800 | [diff] [blame] | 206 | #else |
| 207 | std::optional<GetSensorResponse> assertion(uint8_t id, const Info& sensorInfo, |
Lei YU | 8e8152c | 2021-12-06 20:11:08 +0800 | [diff] [blame] | 208 | const PropertyMap& /*properties*/) |
Lei YU | 8c2c048 | 2021-09-16 17:28:28 +0800 | [diff] [blame] | 209 | { |
Lei YU | ef960c0 | 2021-10-13 17:54:59 +0800 | [diff] [blame] | 210 | // The assertion may contain multiple properties |
| 211 | // So we have to get the properties from DBus anyway |
| 212 | auto response = mapDbusToAssertion(sensorInfo, sensorInfo.sensorPath, |
| 213 | sensorInfo.sensorInterface); |
| 214 | |
| 215 | if (!sensorCacheMap[id].has_value()) |
| 216 | { |
| 217 | sensorCacheMap[id] = SensorData{}; |
| 218 | } |
| 219 | sensorCacheMap[id]->response = response; |
| 220 | return response; |
Lei YU | 8c2c048 | 2021-09-16 17:28:28 +0800 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | std::optional<GetSensorResponse> eventdata2(uint8_t id, const Info& sensorInfo, |
Lei YU | 8e8152c | 2021-12-06 20:11:08 +0800 | [diff] [blame] | 224 | const PropertyMap& /*properties*/) |
Lei YU | 8c2c048 | 2021-09-16 17:28:28 +0800 | [diff] [blame] | 225 | { |
Lei YU | a8dc09b | 2021-10-13 18:06:46 +0800 | [diff] [blame] | 226 | // The eventdata2 may contain multiple properties |
| 227 | // So we have to get the properties from DBus anyway |
| 228 | auto response = mapDbusToEventdata2(sensorInfo); |
| 229 | |
| 230 | if (!sensorCacheMap[id].has_value()) |
| 231 | { |
| 232 | sensorCacheMap[id] = SensorData{}; |
| 233 | } |
| 234 | sensorCacheMap[id]->response = response; |
| 235 | return response; |
Lei YU | 8c2c048 | 2021-09-16 17:28:28 +0800 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | #endif // FEATURE_SENSORS_CACHE |
Tom Joseph | e4014fc | 2017-09-06 23:57:36 +0530 | [diff] [blame] | 239 | |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 240 | } // namespace get |
Tom Joseph | 816e92b | 2017-09-06 19:23:00 +0530 | [diff] [blame] | 241 | |
Dhruvaraj Subhashchandran | e0af720 | 2017-07-12 06:35:20 -0500 | [diff] [blame] | 242 | namespace set |
| 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 | |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 253 | auto dbusService = getService(bus, sensorInterface, sensorPath); |
Dhruvaraj Subhashchandran | e0af720 | 2017-07-12 06:35:20 -0500 | [diff] [blame] | 254 | |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 255 | return bus.new_method_call(dbusService.c_str(), sensorPath.c_str(), |
| 256 | updateInterface.c_str(), command.c_str()); |
Dhruvaraj Subhashchandran | e0af720 | 2017-07-12 06:35:20 -0500 | [diff] [blame] | 257 | } |
| 258 | |
Willy Tu | 11d6889 | 2022-01-20 10:37:34 -0800 | [diff] [blame] | 259 | ipmi_ret_t eventdata(const SetSensorReadingReq&, const Info& sensorInfo, |
Deepak Kodihalli | 1bb0d38 | 2017-08-12 02:01:27 -0500 | [diff] [blame] | 260 | uint8_t data) |
Dhruvaraj Subhashchandran | e0af720 | 2017-07-12 06:35:20 -0500 | [diff] [blame] | 261 | { |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 262 | auto msg = |
| 263 | makeDbusMsg("org.freedesktop.DBus.Properties", sensorInfo.sensorPath, |
| 264 | "Set", sensorInfo.sensorInterface); |
Deepak Kodihalli | 1bb0d38 | 2017-08-12 02:01:27 -0500 | [diff] [blame] | 265 | |
| 266 | const auto& interface = sensorInfo.propertyInterfaces.begin(); |
Dhruvaraj Subhashchandran | e0af720 | 2017-07-12 06:35:20 -0500 | [diff] [blame] | 267 | msg.append(interface->first); |
| 268 | for (const auto& property : interface->second) |
| 269 | { |
| 270 | msg.append(property.first); |
Dhruvaraj Subhashchandran | e245e4e | 2017-10-03 03:58:05 -0500 | [diff] [blame] | 271 | const auto& iter = std::get<OffsetValueMap>(property.second).find(data); |
| 272 | if (iter == std::get<OffsetValueMap>(property.second).end()) |
Dhruvaraj Subhashchandran | e0af720 | 2017-07-12 06:35:20 -0500 | [diff] [blame] | 273 | { |
| 274 | log<level::ERR>("Invalid event data"); |
| 275 | return IPMI_CC_PARM_OUT_OF_RANGE; |
| 276 | } |
| 277 | msg.append(iter->second.assert); |
| 278 | } |
Deepak Kodihalli | 1bb0d38 | 2017-08-12 02:01:27 -0500 | [diff] [blame] | 279 | return updateToDbus(msg); |
Dhruvaraj Subhashchandran | e0af720 | 2017-07-12 06:35:20 -0500 | [diff] [blame] | 280 | } |
| 281 | |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 282 | ipmi_ret_t assertion(const SetSensorReadingReq& cmdData, const Info& sensorInfo) |
Dhruvaraj Subhashchandran | e0af720 | 2017-07-12 06:35:20 -0500 | [diff] [blame] | 283 | { |
Brad Bishop | 06a0abf | 2018-02-26 20:46:00 -0500 | [diff] [blame] | 284 | std::bitset<16> assertionSet(getAssertionSet(cmdData).first); |
| 285 | std::bitset<16> deassertionSet(getAssertionSet(cmdData).second); |
| 286 | auto bothSet = assertionSet ^ deassertionSet; |
| 287 | |
| 288 | const auto& interface = sensorInfo.propertyInterfaces.begin(); |
| 289 | |
| 290 | for (const auto& property : interface->second) |
| 291 | { |
William A. Kennington III | 4c00802 | 2018-10-12 17:18:14 -0700 | [diff] [blame] | 292 | std::optional<Value> tmp; |
Brad Bishop | 06a0abf | 2018-02-26 20:46:00 -0500 | [diff] [blame] | 293 | for (const auto& value : std::get<OffsetValueMap>(property.second)) |
| 294 | { |
| 295 | if (bothSet.size() <= value.first || !bothSet.test(value.first)) |
| 296 | { |
| 297 | // A BIOS shouldn't do this but ignore if they do. |
| 298 | continue; |
| 299 | } |
| 300 | |
| 301 | if (assertionSet.test(value.first)) |
| 302 | { |
| 303 | tmp = value.second.assert; |
| 304 | break; |
| 305 | } |
| 306 | if (deassertionSet.test(value.first)) |
| 307 | { |
| 308 | tmp = value.second.deassert; |
| 309 | break; |
| 310 | } |
| 311 | } |
| 312 | |
William A. Kennington III | 4c00802 | 2018-10-12 17:18:14 -0700 | [diff] [blame] | 313 | if (tmp) |
Brad Bishop | 06a0abf | 2018-02-26 20:46:00 -0500 | [diff] [blame] | 314 | { |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 315 | auto msg = makeDbusMsg("org.freedesktop.DBus.Properties", |
| 316 | sensorInfo.sensorPath, "Set", |
| 317 | sensorInfo.sensorInterface); |
Brad Bishop | 06a0abf | 2018-02-26 20:46:00 -0500 | [diff] [blame] | 318 | msg.append(interface->first); |
| 319 | msg.append(property.first); |
William A. Kennington III | 4c00802 | 2018-10-12 17:18:14 -0700 | [diff] [blame] | 320 | msg.append(*tmp); |
Dhruvaraj Subhashchandran | e0af720 | 2017-07-12 06:35:20 -0500 | [diff] [blame] | 321 | |
Brad Bishop | 06a0abf | 2018-02-26 20:46:00 -0500 | [diff] [blame] | 322 | auto rc = updateToDbus(msg); |
| 323 | if (rc) |
Dhruvaraj Subhashchandran | e0af720 | 2017-07-12 06:35:20 -0500 | [diff] [blame] | 324 | { |
Brad Bishop | 06a0abf | 2018-02-26 20:46:00 -0500 | [diff] [blame] | 325 | return rc; |
Dhruvaraj Subhashchandran | e0af720 | 2017-07-12 06:35:20 -0500 | [diff] [blame] | 326 | } |
| 327 | } |
| 328 | } |
Brad Bishop | 06a0abf | 2018-02-26 20:46:00 -0500 | [diff] [blame] | 329 | |
| 330 | return IPMI_CC_OK; |
Dhruvaraj Subhashchandran | e0af720 | 2017-07-12 06:35:20 -0500 | [diff] [blame] | 331 | } |
Deepak Kodihalli | 1bb0d38 | 2017-08-12 02:01:27 -0500 | [diff] [blame] | 332 | |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 333 | } // namespace set |
Dhruvaraj Subhashchandran | e0af720 | 2017-07-12 06:35:20 -0500 | [diff] [blame] | 334 | |
| 335 | namespace notify |
| 336 | { |
| 337 | |
| 338 | IpmiUpdateData makeDbusMsg(const std::string& updateInterface, |
Willy Tu | 11d6889 | 2022-01-20 10:37:34 -0800 | [diff] [blame] | 339 | const std::string&, const std::string& command, |
| 340 | const std::string&) |
Dhruvaraj Subhashchandran | e0af720 | 2017-07-12 06:35:20 -0500 | [diff] [blame] | 341 | { |
| 342 | sdbusplus::bus::bus bus{ipmid_get_sd_bus_connection()}; |
| 343 | using namespace std::string_literals; |
| 344 | |
Dhruvaraj Subhashchandran | f915f85 | 2017-09-14 07:01:48 -0500 | [diff] [blame] | 345 | static const auto dbusPath = "/xyz/openbmc_project/inventory"s; |
| 346 | std::string dbusService = ipmi::getService(bus, updateInterface, dbusPath); |
Dhruvaraj Subhashchandran | e0af720 | 2017-07-12 06:35:20 -0500 | [diff] [blame] | 347 | |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 348 | return bus.new_method_call(dbusService.c_str(), dbusPath.c_str(), |
| 349 | updateInterface.c_str(), command.c_str()); |
Dhruvaraj Subhashchandran | e0af720 | 2017-07-12 06:35:20 -0500 | [diff] [blame] | 350 | } |
| 351 | |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 352 | ipmi_ret_t assertion(const SetSensorReadingReq& cmdData, const Info& sensorInfo) |
Dhruvaraj Subhashchandran | e0af720 | 2017-07-12 06:35:20 -0500 | [diff] [blame] | 353 | { |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 354 | auto msg = makeDbusMsg(sensorInfo.sensorInterface, sensorInfo.sensorPath, |
| 355 | "Notify", sensorInfo.sensorInterface); |
Deepak Kodihalli | 1bb0d38 | 2017-08-12 02:01:27 -0500 | [diff] [blame] | 356 | |
Dhruvaraj Subhashchandran | e0af720 | 2017-07-12 06:35:20 -0500 | [diff] [blame] | 357 | std::bitset<16> assertionSet(getAssertionSet(cmdData).first); |
| 358 | std::bitset<16> deassertionSet(getAssertionSet(cmdData).second); |
| 359 | ipmi::sensor::ObjectMap objects; |
| 360 | ipmi::sensor::InterfaceMap interfaces; |
Deepak Kodihalli | 1bb0d38 | 2017-08-12 02:01:27 -0500 | [diff] [blame] | 361 | for (const auto& interface : sensorInfo.propertyInterfaces) |
Dhruvaraj Subhashchandran | e0af720 | 2017-07-12 06:35:20 -0500 | [diff] [blame] | 362 | { |
Santosh Puranik | bbf8bd6 | 2019-05-01 19:02:52 +0530 | [diff] [blame] | 363 | // An interface with no properties - It is possible that the sensor |
| 364 | // object on DBUS implements a DBUS interface with no properties. |
| 365 | // Make sure we add the interface to the list if interfaces on the |
| 366 | // object with an empty property map. |
| 367 | if (interface.second.empty()) |
| 368 | { |
| 369 | interfaces.emplace(interface.first, ipmi::sensor::PropertyMap{}); |
| 370 | continue; |
| 371 | } |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 372 | // For a property like functional state the result will be |
| 373 | // calculated based on the true value of all conditions. |
Dhruvaraj Subhashchandran | e0af720 | 2017-07-12 06:35:20 -0500 | [diff] [blame] | 374 | for (const auto& property : interface.second) |
| 375 | { |
| 376 | ipmi::sensor::PropertyMap props; |
| 377 | bool valid = false; |
Dhruvaraj Subhashchandran | e245e4e | 2017-10-03 03:58:05 -0500 | [diff] [blame] | 378 | auto result = true; |
| 379 | for (const auto& value : std::get<OffsetValueMap>(property.second)) |
Dhruvaraj Subhashchandran | e0af720 | 2017-07-12 06:35:20 -0500 | [diff] [blame] | 380 | { |
| 381 | if (assertionSet.test(value.first)) |
| 382 | { |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 383 | // Skip update if skipOn is ASSERT |
Dhruvaraj Subhashchandran | e84841c | 2017-08-22 07:40:27 -0500 | [diff] [blame] | 384 | if (SkipAssertion::ASSERT == value.second.skip) |
| 385 | { |
| 386 | return IPMI_CC_OK; |
| 387 | } |
Vernon Mauery | f442e11 | 2019-04-09 11:44:36 -0700 | [diff] [blame] | 388 | result = result && std::get<bool>(value.second.assert); |
Dhruvaraj Subhashchandran | e0af720 | 2017-07-12 06:35:20 -0500 | [diff] [blame] | 389 | valid = true; |
| 390 | } |
| 391 | else if (deassertionSet.test(value.first)) |
| 392 | { |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 393 | // Skip update if skipOn is DEASSERT |
Dhruvaraj Subhashchandran | e84841c | 2017-08-22 07:40:27 -0500 | [diff] [blame] | 394 | if (SkipAssertion::DEASSERT == value.second.skip) |
| 395 | { |
| 396 | return IPMI_CC_OK; |
| 397 | } |
Vernon Mauery | f442e11 | 2019-04-09 11:44:36 -0700 | [diff] [blame] | 398 | result = result && std::get<bool>(value.second.deassert); |
Dhruvaraj Subhashchandran | e0af720 | 2017-07-12 06:35:20 -0500 | [diff] [blame] | 399 | valid = true; |
| 400 | } |
| 401 | } |
Dhruvaraj Subhashchandran | e245e4e | 2017-10-03 03:58:05 -0500 | [diff] [blame] | 402 | for (const auto& value : |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 403 | std::get<PreReqOffsetValueMap>(property.second)) |
Dhruvaraj Subhashchandran | e245e4e | 2017-10-03 03:58:05 -0500 | [diff] [blame] | 404 | { |
| 405 | if (assertionSet.test(value.first)) |
| 406 | { |
Vernon Mauery | f442e11 | 2019-04-09 11:44:36 -0700 | [diff] [blame] | 407 | result = result && std::get<bool>(value.second.assert); |
Dhruvaraj Subhashchandran | e245e4e | 2017-10-03 03:58:05 -0500 | [diff] [blame] | 408 | } |
| 409 | else if (deassertionSet.test(value.first)) |
| 410 | { |
Vernon Mauery | f442e11 | 2019-04-09 11:44:36 -0700 | [diff] [blame] | 411 | result = result && std::get<bool>(value.second.deassert); |
Dhruvaraj Subhashchandran | e245e4e | 2017-10-03 03:58:05 -0500 | [diff] [blame] | 412 | } |
| 413 | } |
Dhruvaraj Subhashchandran | e0af720 | 2017-07-12 06:35:20 -0500 | [diff] [blame] | 414 | if (valid) |
| 415 | { |
Dhruvaraj Subhashchandran | e245e4e | 2017-10-03 03:58:05 -0500 | [diff] [blame] | 416 | props.emplace(property.first, result); |
Dhruvaraj Subhashchandran | e0af720 | 2017-07-12 06:35:20 -0500 | [diff] [blame] | 417 | interfaces.emplace(interface.first, std::move(props)); |
| 418 | } |
| 419 | } |
| 420 | } |
Dhruvaraj Subhashchandran | e84841c | 2017-08-22 07:40:27 -0500 | [diff] [blame] | 421 | |
Deepak Kodihalli | 1bb0d38 | 2017-08-12 02:01:27 -0500 | [diff] [blame] | 422 | objects.emplace(sensorInfo.sensorPath, std::move(interfaces)); |
Dhruvaraj Subhashchandran | e0af720 | 2017-07-12 06:35:20 -0500 | [diff] [blame] | 423 | msg.append(std::move(objects)); |
Deepak Kodihalli | 1bb0d38 | 2017-08-12 02:01:27 -0500 | [diff] [blame] | 424 | return updateToDbus(msg); |
Dhruvaraj Subhashchandran | e0af720 | 2017-07-12 06:35:20 -0500 | [diff] [blame] | 425 | } |
Tom Joseph | 816e92b | 2017-09-06 19:23:00 +0530 | [diff] [blame] | 426 | |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 427 | } // namespace notify |
Tom Joseph | 816e92b | 2017-09-06 19:23:00 +0530 | [diff] [blame] | 428 | |
| 429 | namespace inventory |
| 430 | { |
| 431 | |
| 432 | namespace get |
| 433 | { |
| 434 | |
Lei YU | ff8c9b4 | 2021-10-15 14:20:57 +0800 | [diff] [blame] | 435 | #ifndef FEATURE_SENSORS_CACHE |
| 436 | |
Tom Joseph | 816e92b | 2017-09-06 19:23:00 +0530 | [diff] [blame] | 437 | GetSensorResponse assertion(const Info& sensorInfo) |
| 438 | { |
Vernon Mauery | 185b9f8 | 2018-07-20 10:52:36 -0700 | [diff] [blame] | 439 | namespace fs = std::filesystem; |
Tom Joseph | 816e92b | 2017-09-06 19:23:00 +0530 | [diff] [blame] | 440 | |
| 441 | fs::path path{ipmi::sensor::inventoryRoot}; |
| 442 | path += sensorInfo.sensorPath; |
| 443 | |
| 444 | return ipmi::sensor::get::mapDbusToAssertion( |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 445 | sensorInfo, path.string(), |
| 446 | sensorInfo.propertyInterfaces.begin()->first); |
Tom Joseph | 816e92b | 2017-09-06 19:23:00 +0530 | [diff] [blame] | 447 | } |
| 448 | |
Lei YU | ff8c9b4 | 2021-10-15 14:20:57 +0800 | [diff] [blame] | 449 | #else |
| 450 | |
| 451 | std::optional<GetSensorResponse> assertion(uint8_t id, const Info& sensorInfo, |
Lei YU | 8e8152c | 2021-12-06 20:11:08 +0800 | [diff] [blame] | 452 | const PropertyMap& /*properties*/) |
Lei YU | ff8c9b4 | 2021-10-15 14:20:57 +0800 | [diff] [blame] | 453 | { |
Lei YU | ff8c9b4 | 2021-10-15 14:20:57 +0800 | [diff] [blame] | 454 | // The assertion may contain multiple properties |
| 455 | // So we have to get the properties from DBus anyway |
| 456 | namespace fs = std::filesystem; |
| 457 | |
| 458 | fs::path path{ipmi::sensor::inventoryRoot}; |
| 459 | path += sensorInfo.sensorPath; |
| 460 | |
| 461 | auto response = ipmi::sensor::get::mapDbusToAssertion( |
| 462 | sensorInfo, path.string(), |
| 463 | sensorInfo.propertyInterfaces.begin()->first); |
| 464 | |
| 465 | if (!sensorCacheMap[id].has_value()) |
| 466 | { |
| 467 | sensorCacheMap[id] = SensorData{}; |
| 468 | } |
| 469 | sensorCacheMap[id]->response = response; |
| 470 | return response; |
| 471 | } |
| 472 | |
| 473 | #endif |
| 474 | |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 475 | } // namespace get |
Tom Joseph | 816e92b | 2017-09-06 19:23:00 +0530 | [diff] [blame] | 476 | |
| 477 | } // namespace inventory |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 478 | } // namespace sensor |
| 479 | } // namespace ipmi |