George Liu | 6f777cd | 2021-06-10 09:16:28 +0800 | [diff] [blame] | 1 | #include "occ_dbus.hpp" |
| 2 | |
| 3 | #include "utils.hpp" |
| 4 | |
Chris Cain | 37abe9b | 2024-10-31 17:20:31 -0500 | [diff] [blame] | 5 | #include <phosphor-logging/lg2.hpp> |
George Liu | 6f777cd | 2021-06-10 09:16:28 +0800 | [diff] [blame] | 6 | |
George Liu | b5ca101 | 2021-09-10 12:53:11 +0800 | [diff] [blame] | 7 | #include <iostream> |
| 8 | |
George Liu | 6f777cd | 2021-06-10 09:16:28 +0800 | [diff] [blame] | 9 | namespace open_power |
| 10 | { |
| 11 | namespace occ |
| 12 | { |
| 13 | namespace dbus |
| 14 | { |
| 15 | |
Matt Spinler | 5901abd | 2021-09-23 13:50:03 -0500 | [diff] [blame] | 16 | using namespace std::string_literals; |
| 17 | const auto defaultChassisPath = |
| 18 | "/xyz/openbmc_project/inventory/system/chassis"s; |
| 19 | const auto chassisInterface = "xyz.openbmc_project.Inventory.Item.Chassis"s; |
| 20 | |
George Liu | 0ad9dd8 | 2021-06-22 14:46:14 +0800 | [diff] [blame] | 21 | bool OccDBusSensors::setMaxValue(const std::string& path, double value) |
George Liu | 6f777cd | 2021-06-10 09:16:28 +0800 | [diff] [blame] | 22 | { |
George Liu | 0ad9dd8 | 2021-06-22 14:46:14 +0800 | [diff] [blame] | 23 | if (path.empty()) |
| 24 | { |
| 25 | return false; |
| 26 | } |
| 27 | |
George Liu | f3a4a69 | 2021-12-28 13:59:51 +0800 | [diff] [blame] | 28 | if (!sensors.contains(path)) |
George Liu | 6f777cd | 2021-06-10 09:16:28 +0800 | [diff] [blame] | 29 | { |
| 30 | sensors.emplace( |
| 31 | path, std::make_unique<SensorIntf>(utils::getBus(), path.c_str())); |
| 32 | } |
| 33 | |
| 34 | sensors.at(path)->maxValue(value); |
George Liu | 0ad9dd8 | 2021-06-22 14:46:14 +0800 | [diff] [blame] | 35 | return true; |
George Liu | 6f777cd | 2021-06-10 09:16:28 +0800 | [diff] [blame] | 36 | } |
| 37 | |
| 38 | double OccDBusSensors::getMaxValue(const std::string& path) const |
| 39 | { |
| 40 | if (sensors.find(path) != sensors.end()) |
| 41 | { |
| 42 | return sensors.at(path)->maxValue(); |
| 43 | } |
| 44 | |
| 45 | throw std::invalid_argument("Failed to get MaxValue property."); |
| 46 | } |
| 47 | |
George Liu | 0ad9dd8 | 2021-06-22 14:46:14 +0800 | [diff] [blame] | 48 | bool OccDBusSensors::setMinValue(const std::string& path, double value) |
George Liu | 6f777cd | 2021-06-10 09:16:28 +0800 | [diff] [blame] | 49 | { |
George Liu | 0ad9dd8 | 2021-06-22 14:46:14 +0800 | [diff] [blame] | 50 | if (path.empty()) |
| 51 | { |
| 52 | return false; |
| 53 | } |
| 54 | |
George Liu | f3a4a69 | 2021-12-28 13:59:51 +0800 | [diff] [blame] | 55 | if (!sensors.contains(path)) |
George Liu | 6f777cd | 2021-06-10 09:16:28 +0800 | [diff] [blame] | 56 | { |
| 57 | sensors.emplace( |
| 58 | path, std::make_unique<SensorIntf>(utils::getBus(), path.c_str())); |
| 59 | } |
| 60 | |
| 61 | sensors.at(path)->minValue(value); |
George Liu | 0ad9dd8 | 2021-06-22 14:46:14 +0800 | [diff] [blame] | 62 | return true; |
George Liu | 6f777cd | 2021-06-10 09:16:28 +0800 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | double OccDBusSensors::getMinValue(const std::string& path) const |
| 66 | { |
| 67 | if (sensors.find(path) != sensors.end()) |
| 68 | { |
| 69 | return sensors.at(path)->minValue(); |
| 70 | } |
| 71 | |
| 72 | throw std::invalid_argument("Failed to get MinValue property."); |
| 73 | } |
| 74 | |
George Liu | 0ad9dd8 | 2021-06-22 14:46:14 +0800 | [diff] [blame] | 75 | bool OccDBusSensors::setValue(const std::string& path, double value) |
George Liu | 6f777cd | 2021-06-10 09:16:28 +0800 | [diff] [blame] | 76 | { |
George Liu | 0ad9dd8 | 2021-06-22 14:46:14 +0800 | [diff] [blame] | 77 | if (path.empty()) |
| 78 | { |
| 79 | return false; |
| 80 | } |
| 81 | |
George Liu | f3a4a69 | 2021-12-28 13:59:51 +0800 | [diff] [blame] | 82 | if (!sensors.contains(path)) |
George Liu | 6f777cd | 2021-06-10 09:16:28 +0800 | [diff] [blame] | 83 | { |
| 84 | sensors.emplace( |
| 85 | path, std::make_unique<SensorIntf>(utils::getBus(), path.c_str())); |
| 86 | } |
| 87 | |
| 88 | sensors.at(path)->value(value); |
George Liu | 0ad9dd8 | 2021-06-22 14:46:14 +0800 | [diff] [blame] | 89 | return true; |
George Liu | 6f777cd | 2021-06-10 09:16:28 +0800 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | double OccDBusSensors::getValue(const std::string& path) const |
| 93 | { |
| 94 | if (sensors.find(path) != sensors.end()) |
| 95 | { |
| 96 | return sensors.at(path)->value(); |
| 97 | } |
| 98 | |
| 99 | throw std::invalid_argument("Failed to get Value property."); |
| 100 | } |
| 101 | |
George Liu | 0ad9dd8 | 2021-06-22 14:46:14 +0800 | [diff] [blame] | 102 | bool OccDBusSensors::setUnit(const std::string& path, const std::string& value) |
George Liu | 6f777cd | 2021-06-10 09:16:28 +0800 | [diff] [blame] | 103 | { |
George Liu | 0ad9dd8 | 2021-06-22 14:46:14 +0800 | [diff] [blame] | 104 | if (path.empty()) |
| 105 | { |
| 106 | return false; |
| 107 | } |
| 108 | |
George Liu | f3a4a69 | 2021-12-28 13:59:51 +0800 | [diff] [blame] | 109 | if (!sensors.contains(path)) |
George Liu | 6f777cd | 2021-06-10 09:16:28 +0800 | [diff] [blame] | 110 | { |
| 111 | sensors.emplace( |
| 112 | path, std::make_unique<SensorIntf>(utils::getBus(), path.c_str())); |
| 113 | } |
| 114 | |
| 115 | try |
| 116 | { |
| 117 | sensors.at(path)->unit(SensorIntf::convertUnitFromString(value)); |
| 118 | } |
| 119 | catch (const std::exception& e) |
| 120 | { |
Chris Cain | 37abe9b | 2024-10-31 17:20:31 -0500 | [diff] [blame] | 121 | lg2::error("set Unit propety failed: error={ERR}", "ERR", e.what()); |
George Liu | 0ad9dd8 | 2021-06-22 14:46:14 +0800 | [diff] [blame] | 122 | return false; |
George Liu | 6f777cd | 2021-06-10 09:16:28 +0800 | [diff] [blame] | 123 | } |
George Liu | 0ad9dd8 | 2021-06-22 14:46:14 +0800 | [diff] [blame] | 124 | |
| 125 | return true; |
George Liu | 6f777cd | 2021-06-10 09:16:28 +0800 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | std::string OccDBusSensors::getUnit(const std::string& path) const |
| 129 | { |
| 130 | if (sensors.find(path) != sensors.end()) |
| 131 | { |
| 132 | try |
| 133 | { |
| 134 | return SensorIntf::convertUnitToString(sensors.at(path)->unit()); |
| 135 | } |
| 136 | catch (const std::exception& e) |
| 137 | { |
Chris Cain | 37abe9b | 2024-10-31 17:20:31 -0500 | [diff] [blame] | 138 | lg2::error("get Unit propety failed: error={ERR}", "ERR", e.what()); |
George Liu | 6f777cd | 2021-06-10 09:16:28 +0800 | [diff] [blame] | 139 | } |
| 140 | } |
| 141 | |
| 142 | throw std::invalid_argument("Failed to get Unit property."); |
| 143 | } |
| 144 | |
George Liu | 0ad9dd8 | 2021-06-22 14:46:14 +0800 | [diff] [blame] | 145 | bool OccDBusSensors::setOperationalStatus(const std::string& path, bool value) |
George Liu | 6f777cd | 2021-06-10 09:16:28 +0800 | [diff] [blame] | 146 | { |
George Liu | 0ad9dd8 | 2021-06-22 14:46:14 +0800 | [diff] [blame] | 147 | if (path.empty()) |
| 148 | { |
| 149 | return false; |
| 150 | } |
| 151 | |
George Liu | f3a4a69 | 2021-12-28 13:59:51 +0800 | [diff] [blame] | 152 | if (!operationalStatus.contains(path)) |
George Liu | 6f777cd | 2021-06-10 09:16:28 +0800 | [diff] [blame] | 153 | { |
| 154 | operationalStatus.emplace(path, std::make_unique<OperationalStatusIntf>( |
| 155 | utils::getBus(), path.c_str())); |
| 156 | } |
| 157 | |
| 158 | operationalStatus.at(path)->functional(value); |
George Liu | 0ad9dd8 | 2021-06-22 14:46:14 +0800 | [diff] [blame] | 159 | return true; |
George Liu | 6f777cd | 2021-06-10 09:16:28 +0800 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | bool OccDBusSensors::getOperationalStatus(const std::string& path) const |
| 163 | { |
| 164 | if (operationalStatus.find(path) != operationalStatus.end()) |
| 165 | { |
| 166 | return operationalStatus.at(path)->functional(); |
| 167 | } |
| 168 | |
| 169 | throw std::invalid_argument("Failed to get OperationalStatus property."); |
| 170 | } |
| 171 | |
Chris Cain | 3523cc0 | 2024-10-30 17:19:09 -0500 | [diff] [blame] | 172 | void OccDBusSensors::setChassisAssociation( |
| 173 | const std::string& path, const std::vector<std::string>& fTypes) |
Matt Spinler | 5901abd | 2021-09-23 13:50:03 -0500 | [diff] [blame] | 174 | { |
| 175 | using AssociationsEntry = std::tuple<std::string, std::string, std::string>; |
| 176 | using AssociationsProperty = std::vector<AssociationsEntry>; |
| 177 | using PropVariant = sdbusplus::xyz::openbmc_project::Association::server:: |
| 178 | Definitions::PropertiesVariant; |
| 179 | |
| 180 | if (chassisPath.empty()) |
| 181 | { |
| 182 | chassisPath = getChassisPath(); |
| 183 | } |
| 184 | |
Chris Cain | 3523cc0 | 2024-10-30 17:19:09 -0500 | [diff] [blame] | 185 | AssociationsProperty associations; |
| 186 | for (const auto& fType : fTypes) |
| 187 | { |
| 188 | associations.emplace_back("chassis", fType, chassisPath); |
| 189 | } |
Matt Spinler | 5901abd | 2021-09-23 13:50:03 -0500 | [diff] [blame] | 190 | PropVariant value{std::move(associations)}; |
| 191 | |
| 192 | std::map<std::string, PropVariant> properties; |
| 193 | properties.emplace("Associations", std::move(value)); |
| 194 | |
| 195 | chassisAssociations.emplace( |
| 196 | path, std::make_unique<AssociationIntf>(utils::getBus(), path.c_str(), |
| 197 | properties)); |
| 198 | } |
| 199 | |
| 200 | std::string OccDBusSensors::getChassisPath() |
| 201 | { |
| 202 | try |
| 203 | { |
| 204 | auto paths = utils::getSubtreePaths(std::vector{chassisInterface}); |
| 205 | |
| 206 | // For now, support either 1 chassis, or multiple as long as one |
| 207 | // of them has the standard name, which we will use. If this ever |
| 208 | // fails, then someone would have to figure out how to identify the |
| 209 | // chassis the OCCs are on. |
| 210 | if (paths.size() == 1) |
| 211 | { |
| 212 | return paths[0]; |
| 213 | } |
| 214 | else if (std::find(paths.begin(), paths.end(), defaultChassisPath) == |
| 215 | paths.end()) |
| 216 | { |
Chris Cain | 37abe9b | 2024-10-31 17:20:31 -0500 | [diff] [blame] | 217 | lg2::error("Could not find a chassis out of {NUM} chassis objects", |
| 218 | "NUM", paths.size()); |
Matt Spinler | 5901abd | 2021-09-23 13:50:03 -0500 | [diff] [blame] | 219 | // Can't throw an exception here, the sdeventplus timer |
| 220 | // just catches it. |
| 221 | abort(); |
| 222 | } |
| 223 | } |
| 224 | catch (const std::exception& e) |
| 225 | { |
Chris Cain | 37abe9b | 2024-10-31 17:20:31 -0500 | [diff] [blame] | 226 | lg2::error("Error looking up chassis objects: {ERR}", "ERR", e.what()); |
Matt Spinler | 5901abd | 2021-09-23 13:50:03 -0500 | [diff] [blame] | 227 | abort(); |
| 228 | } |
| 229 | |
| 230 | return defaultChassisPath; |
| 231 | } |
| 232 | |
Matt Spinler | ace67d8 | 2021-10-18 13:41:57 -0500 | [diff] [blame] | 233 | bool OccDBusSensors::hasDvfsTemp(const std::string& path) const |
| 234 | { |
| 235 | return dvfsTemps.find(path) != dvfsTemps.end(); |
| 236 | } |
| 237 | |
| 238 | void OccDBusSensors::setDvfsTemp(const std::string& path, double value) |
| 239 | { |
Patrick Williams | d7542c8 | 2024-08-16 15:20:28 -0400 | [diff] [blame] | 240 | dvfsTemps[path] = |
| 241 | std::make_unique<SensorIntf>(utils::getBus(), path.c_str()); |
Matt Spinler | ace67d8 | 2021-10-18 13:41:57 -0500 | [diff] [blame] | 242 | dvfsTemps[path]->value(value); |
| 243 | } |
| 244 | |
George Liu | 6f777cd | 2021-06-10 09:16:28 +0800 | [diff] [blame] | 245 | } // namespace dbus |
| 246 | } // namespace occ |
| 247 | } // namespace open_power |