| Alexander Hansen | 46a755f | 2025-10-27 16:31:08 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: Apache-2.0 |
| 2 | // SPDX-FileCopyrightText: Copyright 2017 Google Inc |
| Patrick Venture | aadb30d | 2020-08-10 09:17:11 -0700 | [diff] [blame] | 3 | |
| Pete O_o | 765a6d8 | 2025-07-23 21:44:14 -0700 | [diff] [blame] | 4 | #include "config.h" |
| 5 | |
| Patrick Venture | aadb30d | 2020-08-10 09:17:11 -0700 | [diff] [blame] | 6 | #include "dbushelper.hpp" |
| 7 | |
| 8 | #include "dbushelper_interface.hpp" |
| 9 | #include "dbusutil.hpp" |
| Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 10 | |
| Patrick Venture | a83a3ec | 2020-08-04 09:52:05 -0700 | [diff] [blame] | 11 | #include <phosphor-logging/log.hpp> |
| Patrick Venture | aadb30d | 2020-08-10 09:17:11 -0700 | [diff] [blame] | 12 | #include <sdbusplus/bus.hpp> |
| Ed Tanous | f8b6e55 | 2025-06-27 13:27:50 -0700 | [diff] [blame] | 13 | #include <sdbusplus/exception.hpp> |
| Alexander Hansen | 1ec3d13 | 2025-10-27 17:19:50 +0100 | [diff] [blame] | 14 | #include <xyz/openbmc_project/ObjectMapper/common.hpp> |
| Alexander Hansen | dae4a3a | 2025-11-11 17:12:01 +0100 | [diff] [blame^] | 15 | #include <xyz/openbmc_project/Sensor/Value/client.hpp> |
| Patrick Venture | a83a3ec | 2020-08-04 09:52:05 -0700 | [diff] [blame] | 16 | |
| Ed Tanous | f8b6e55 | 2025-06-27 13:27:50 -0700 | [diff] [blame] | 17 | #include <cstdint> |
| Patrick Venture | aadb30d | 2020-08-10 09:17:11 -0700 | [diff] [blame] | 18 | #include <map> |
| Ed Tanous | f8b6e55 | 2025-06-27 13:27:50 -0700 | [diff] [blame] | 19 | #include <stdexcept> |
| Patrick Venture | aadb30d | 2020-08-10 09:17:11 -0700 | [diff] [blame] | 20 | #include <string> |
| James Feist | 1f802f5 | 2019-02-08 13:51:43 -0800 | [diff] [blame] | 21 | #include <variant> |
| Patrick Venture | aadb30d | 2020-08-10 09:17:11 -0700 | [diff] [blame] | 22 | #include <vector> |
| 23 | |
| Alexander Hansen | 1ec3d13 | 2025-10-27 17:19:50 +0100 | [diff] [blame] | 24 | using ObjectMapper = sdbusplus::common::xyz::openbmc_project::ObjectMapper; |
| Alexander Hansen | dae4a3a | 2025-11-11 17:12:01 +0100 | [diff] [blame^] | 25 | using SensorValue = sdbusplus::common::xyz::openbmc_project::sensor::Value; |
| Alexander Hansen | 1ec3d13 | 2025-10-27 17:19:50 +0100 | [diff] [blame] | 26 | |
| Patrick Venture | aadb30d | 2020-08-10 09:17:11 -0700 | [diff] [blame] | 27 | namespace pid_control |
| 28 | { |
| Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 29 | |
| Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 30 | using Property = std::string; |
| James Feist | 1f802f5 | 2019-02-08 13:51:43 -0800 | [diff] [blame] | 31 | using Value = std::variant<int64_t, double, std::string, bool>; |
| Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 32 | using PropertyMap = std::map<Property, Value>; |
| 33 | |
| Patrick Venture | 34ddc90 | 2018-10-30 11:05:17 -0700 | [diff] [blame] | 34 | using namespace phosphor::logging; |
| 35 | |
| Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 36 | /* TODO(venture): Basically all phosphor apps need this, maybe it should be a |
| 37 | * part of sdbusplus. There is an old version in libmapper. |
| 38 | */ |
| Patrick Venture | 9b93692 | 2020-08-10 11:28:39 -0700 | [diff] [blame] | 39 | std::string DbusHelper::getService(const std::string& intf, |
| Patrick Venture | 0df7c0f | 2018-06-13 09:02:13 -0700 | [diff] [blame] | 40 | const std::string& path) |
| Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 41 | { |
| Alexander Hansen | 1ec3d13 | 2025-10-27 17:19:50 +0100 | [diff] [blame] | 42 | auto mapper = _bus.new_method_call( |
| 43 | ObjectMapper::default_service, ObjectMapper::instance_path, |
| 44 | ObjectMapper::interface, ObjectMapper::method_names::get_object); |
| Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 45 | |
| 46 | mapper.append(path); |
| 47 | mapper.append(std::vector<std::string>({intf})); |
| 48 | |
| Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 49 | std::map<std::string, std::vector<std::string>> response; |
| Patrick Venture | 34ddc90 | 2018-10-30 11:05:17 -0700 | [diff] [blame] | 50 | |
| 51 | try |
| 52 | { |
| Patrick Venture | 8729eb9 | 2020-08-10 10:38:44 -0700 | [diff] [blame] | 53 | auto responseMsg = _bus.call(mapper); |
| Patrick Venture | 34ddc90 | 2018-10-30 11:05:17 -0700 | [diff] [blame] | 54 | |
| 55 | responseMsg.read(response); |
| 56 | } |
| Patrick Williams | b228bc3 | 2022-07-22 19:26:56 -0500 | [diff] [blame] | 57 | catch (const sdbusplus::exception_t& ex) |
| Patrick Venture | 34ddc90 | 2018-10-30 11:05:17 -0700 | [diff] [blame] | 58 | { |
| 59 | log<level::ERR>("ObjectMapper call failure", |
| 60 | entry("WHAT=%s", ex.what())); |
| 61 | throw; |
| 62 | } |
| Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 63 | |
| 64 | if (response.begin() == response.end()) |
| 65 | { |
| 66 | throw std::runtime_error("Unable to find Object: " + path); |
| 67 | } |
| 68 | |
| 69 | return response.begin()->first; |
| 70 | } |
| 71 | |
| Patrick Venture | 9b93692 | 2020-08-10 11:28:39 -0700 | [diff] [blame] | 72 | void DbusHelper::getProperties(const std::string& service, |
| Patrick Venture | 1df9e87 | 2020-10-08 15:35:01 -0700 | [diff] [blame] | 73 | const std::string& path, SensorProperties* prop) |
| Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 74 | { |
| Patrick Venture | 8729eb9 | 2020-08-10 10:38:44 -0700 | [diff] [blame] | 75 | auto pimMsg = _bus.new_method_call(service.c_str(), path.c_str(), |
| 76 | propertiesintf, "GetAll"); |
| Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 77 | |
| Alexander Hansen | dae4a3a | 2025-11-11 17:12:01 +0100 | [diff] [blame^] | 78 | pimMsg.append(SensorValue::interface); |
| Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 79 | |
| Patrick Venture | 7dbc517 | 2018-10-30 12:18:45 -0700 | [diff] [blame] | 80 | PropertyMap propMap; |
| 81 | |
| 82 | try |
| Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 83 | { |
| Patrick Venture | 8729eb9 | 2020-08-10 10:38:44 -0700 | [diff] [blame] | 84 | auto valueResponseMsg = _bus.call(pimMsg); |
| Patrick Venture | 7dbc517 | 2018-10-30 12:18:45 -0700 | [diff] [blame] | 85 | valueResponseMsg.read(propMap); |
| 86 | } |
| Patrick Williams | b228bc3 | 2022-07-22 19:26:56 -0500 | [diff] [blame] | 87 | catch (const sdbusplus::exception_t& ex) |
| Patrick Venture | 7dbc517 | 2018-10-30 12:18:45 -0700 | [diff] [blame] | 88 | { |
| 89 | log<level::ERR>("GetAll Properties Failed", |
| 90 | entry("WHAT=%s", ex.what())); |
| 91 | throw; |
| Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | // The PropertyMap returned will look like this because it's always |
| 95 | // reading a Sensor.Value interface. |
| 96 | // a{sv} 3: |
| 97 | // "Value" x 24875 |
| 98 | // "Unit" s "xyz.openbmc_project.Sensor.Value.Unit.DegreesC" |
| 99 | // "Scale" x -3 |
| Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 100 | |
| Patrick Venture | 0d73b10 | 2018-05-09 10:29:42 -0700 | [diff] [blame] | 101 | // If no error was set, the values should all be there. |
| Alexander Hansen | dae4a3a | 2025-11-11 17:12:01 +0100 | [diff] [blame^] | 102 | auto findUnit = propMap.find(SensorValue::property_names::unit); |
| James Feist | c065cf1 | 2018-07-05 10:23:11 -0700 | [diff] [blame] | 103 | if (findUnit != propMap.end()) |
| 104 | { |
| James Feist | 1f802f5 | 2019-02-08 13:51:43 -0800 | [diff] [blame] | 105 | prop->unit = std::get<std::string>(findUnit->second); |
| James Feist | c065cf1 | 2018-07-05 10:23:11 -0700 | [diff] [blame] | 106 | } |
| Alexander Hansen | dae4a3a | 2025-11-11 17:12:01 +0100 | [diff] [blame^] | 107 | // TODO: in PDI there is no such 'Scale' property on the Sensor.Value |
| 108 | // interface |
| James Feist | c065cf1 | 2018-07-05 10:23:11 -0700 | [diff] [blame] | 109 | auto findScale = propMap.find("Scale"); |
| Alexander Hansen | dae4a3a | 2025-11-11 17:12:01 +0100 | [diff] [blame^] | 110 | auto findMax = propMap.find(SensorValue::property_names::max_value); |
| 111 | auto findMin = propMap.find(SensorValue::property_names::min_value); |
| James Feist | 75eb769 | 2019-02-25 12:50:02 -0800 | [diff] [blame] | 112 | |
| 113 | prop->min = 0; |
| 114 | prop->max = 0; |
| 115 | prop->scale = 0; |
| James Feist | c065cf1 | 2018-07-05 10:23:11 -0700 | [diff] [blame] | 116 | if (findScale != propMap.end()) |
| 117 | { |
| James Feist | 1f802f5 | 2019-02-08 13:51:43 -0800 | [diff] [blame] | 118 | prop->scale = std::get<int64_t>(findScale->second); |
| James Feist | c065cf1 | 2018-07-05 10:23:11 -0700 | [diff] [blame] | 119 | } |
| James Feist | 75eb769 | 2019-02-25 12:50:02 -0800 | [diff] [blame] | 120 | if (findMax != propMap.end()) |
| James Feist | c065cf1 | 2018-07-05 10:23:11 -0700 | [diff] [blame] | 121 | { |
| James Feist | 75eb769 | 2019-02-25 12:50:02 -0800 | [diff] [blame] | 122 | prop->max = std::visit(VariantToDoubleVisitor(), findMax->second); |
| 123 | } |
| 124 | if (findMin != propMap.end()) |
| 125 | { |
| 126 | prop->min = std::visit(VariantToDoubleVisitor(), findMin->second); |
| James Feist | c065cf1 | 2018-07-05 10:23:11 -0700 | [diff] [blame] | 127 | } |
| 128 | |
| Alexander Hansen | dae4a3a | 2025-11-11 17:12:01 +0100 | [diff] [blame^] | 129 | prop->value = std::visit(VariantToDoubleVisitor(), |
| 130 | propMap[SensorValue::property_names::value]); |
| Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 131 | |
| Alex.Song | 8f73ad7 | 2021-10-07 00:18:27 +0800 | [diff] [blame] | 132 | bool available = true; |
| 133 | try |
| 134 | { |
| 135 | getProperty(service, path, availabilityIntf, "Available", available); |
| 136 | } |
| Patrick Williams | b228bc3 | 2022-07-22 19:26:56 -0500 | [diff] [blame] | 137 | catch (const sdbusplus::exception_t& ex) |
| Alex.Song | 8f73ad7 | 2021-10-07 00:18:27 +0800 | [diff] [blame] | 138 | { |
| 139 | // unsupported Available property, leaving reading at 'True' |
| 140 | } |
| 141 | prop->available = available; |
| 142 | |
| Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 143 | return; |
| 144 | } |
| 145 | |
| Patrick Venture | 9b93692 | 2020-08-10 11:28:39 -0700 | [diff] [blame] | 146 | bool DbusHelper::thresholdsAsserted(const std::string& service, |
| James Feist | 36b7d8e | 2018-10-05 15:39:01 -0700 | [diff] [blame] | 147 | const std::string& path) |
| 148 | { |
| Patrick Venture | 8729eb9 | 2020-08-10 10:38:44 -0700 | [diff] [blame] | 149 | auto critical = _bus.new_method_call(service.c_str(), path.c_str(), |
| 150 | propertiesintf, "GetAll"); |
| James Feist | 36b7d8e | 2018-10-05 15:39:01 -0700 | [diff] [blame] | 151 | critical.append(criticalThreshInf); |
| 152 | PropertyMap criticalMap; |
| 153 | |
| 154 | try |
| 155 | { |
| Patrick Venture | 8729eb9 | 2020-08-10 10:38:44 -0700 | [diff] [blame] | 156 | auto msg = _bus.call(critical); |
| Patrick Venture | 4fd8cff | 2018-10-31 14:24:12 -0700 | [diff] [blame] | 157 | msg.read(criticalMap); |
| James Feist | 36b7d8e | 2018-10-05 15:39:01 -0700 | [diff] [blame] | 158 | } |
| Patrick Williams | 0001ee0 | 2021-10-06 14:44:22 -0500 | [diff] [blame] | 159 | catch (const sdbusplus::exception_t&) |
| James Feist | 36b7d8e | 2018-10-05 15:39:01 -0700 | [diff] [blame] | 160 | { |
| 161 | // do nothing, sensors don't have to expose critical thresholds |
| Jonico Eustaquio | af97d8e | 2024-01-02 14:35:07 -0600 | [diff] [blame] | 162 | #ifndef UNC_FAILSAFE |
| James Feist | 36b7d8e | 2018-10-05 15:39:01 -0700 | [diff] [blame] | 163 | return false; |
| Jonico Eustaquio | af97d8e | 2024-01-02 14:35:07 -0600 | [diff] [blame] | 164 | #endif |
| James Feist | 36b7d8e | 2018-10-05 15:39:01 -0700 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | auto findCriticalLow = criticalMap.find("CriticalAlarmLow"); |
| 168 | auto findCriticalHigh = criticalMap.find("CriticalAlarmHigh"); |
| 169 | |
| 170 | bool asserted = false; |
| 171 | if (findCriticalLow != criticalMap.end()) |
| 172 | { |
| James Feist | 1f802f5 | 2019-02-08 13:51:43 -0800 | [diff] [blame] | 173 | asserted = std::get<bool>(findCriticalLow->second); |
| James Feist | 36b7d8e | 2018-10-05 15:39:01 -0700 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | // as we are catching properties changed, a sensor could theoretically jump |
| 177 | // from one threshold to the other in one event, so check both thresholds |
| 178 | if (!asserted && findCriticalHigh != criticalMap.end()) |
| 179 | { |
| James Feist | 1f802f5 | 2019-02-08 13:51:43 -0800 | [diff] [blame] | 180 | asserted = std::get<bool>(findCriticalHigh->second); |
| James Feist | 36b7d8e | 2018-10-05 15:39:01 -0700 | [diff] [blame] | 181 | } |
| Jonico Eustaquio | af97d8e | 2024-01-02 14:35:07 -0600 | [diff] [blame] | 182 | #ifdef UNC_FAILSAFE |
| 183 | if (!asserted) |
| 184 | { |
| 185 | auto warning = _bus.new_method_call(service.c_str(), path.c_str(), |
| 186 | propertiesintf, "GetAll"); |
| 187 | warning.append(warningThreshInf); |
| 188 | PropertyMap warningMap; |
| 189 | |
| 190 | try |
| 191 | { |
| 192 | auto msg = _bus.call(warning); |
| 193 | msg.read(warningMap); |
| 194 | } |
| 195 | catch (const sdbusplus::exception_t&) |
| 196 | { |
| 197 | // sensors don't have to expose non-critical thresholds |
| 198 | return false; |
| 199 | } |
| 200 | auto findWarningHigh = warningMap.find("WarningAlarmHigh"); |
| 201 | |
| 202 | if (findWarningHigh != warningMap.end()) |
| 203 | { |
| 204 | asserted = std::get<bool>(findWarningHigh->second); |
| 205 | } |
| 206 | } |
| 207 | #endif |
| James Feist | 36b7d8e | 2018-10-05 15:39:01 -0700 | [diff] [blame] | 208 | return asserted; |
| 209 | } |
| 210 | |
| Patrick Venture | a076487 | 2020-08-08 07:48:43 -0700 | [diff] [blame] | 211 | } // namespace pid_control |