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