Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 1 | /** |
| 2 | * Copyright 2017 Google Inc. |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 16 | #include "dbuspassive.hpp" |
| 17 | |
Patrick Venture | aadb30d | 2020-08-10 09:17:11 -0700 | [diff] [blame] | 18 | #include "dbushelper_interface.hpp" |
James Feist | 98b704e | 2019-06-03 16:24:53 -0700 | [diff] [blame] | 19 | #include "dbuspassiveredundancy.hpp" |
Patrick Venture | aadb30d | 2020-08-10 09:17:11 -0700 | [diff] [blame] | 20 | #include "dbusutil.hpp" |
James Feist | 0c8223b | 2019-05-08 15:33:33 -0700 | [diff] [blame] | 21 | #include "util.hpp" |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 22 | |
Patrick Venture | a83a3ec | 2020-08-04 09:52:05 -0700 | [diff] [blame] | 23 | #include <sdbusplus/bus.hpp> |
| 24 | |
Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 25 | #include <chrono> |
| 26 | #include <cmath> |
Patrick Venture | 0ef1faf | 2018-06-13 12:50:53 -0700 | [diff] [blame] | 27 | #include <memory> |
Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 28 | #include <mutex> |
Patrick Venture | 0ef1faf | 2018-06-13 12:50:53 -0700 | [diff] [blame] | 29 | #include <string> |
James Feist | 1f802f5 | 2019-02-08 13:51:43 -0800 | [diff] [blame] | 30 | #include <variant> |
Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 31 | |
Patrick Venture | a076487 | 2020-08-08 07:48:43 -0700 | [diff] [blame] | 32 | namespace pid_control |
| 33 | { |
| 34 | |
Patrick Venture | 563a356 | 2018-10-30 09:31:26 -0700 | [diff] [blame] | 35 | std::unique_ptr<ReadInterface> DbusPassive::createDbusPassive( |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 36 | sdbusplus::bus::bus& bus, const std::string& type, const std::string& id, |
Patrick Venture | 8729eb9 | 2020-08-10 10:38:44 -0700 | [diff] [blame^] | 37 | std::unique_ptr<DbusHelperInterface> helper, const conf::SensorConfig* info, |
James Feist | 98b704e | 2019-06-03 16:24:53 -0700 | [diff] [blame] | 38 | const std::shared_ptr<DbusPassiveRedundancy>& redundancy) |
Patrick Venture | 0ef1faf | 2018-06-13 12:50:53 -0700 | [diff] [blame] | 39 | { |
| 40 | if (helper == nullptr) |
| 41 | { |
| 42 | return nullptr; |
| 43 | } |
Patrick Venture | 7af157b | 2018-10-30 11:24:40 -0700 | [diff] [blame] | 44 | if (!validType(type)) |
Patrick Venture | 0ef1faf | 2018-06-13 12:50:53 -0700 | [diff] [blame] | 45 | { |
| 46 | return nullptr; |
| 47 | } |
| 48 | |
Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 49 | /* Need to get the scale and initial value */ |
James Feist | 9fa90c1 | 2019-01-11 15:35:22 -0800 | [diff] [blame] | 50 | auto tempBus = sdbusplus::bus::new_system(); |
Patrick Venture | f8cb464 | 2018-10-30 12:02:53 -0700 | [diff] [blame] | 51 | |
Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 52 | /* service == busname */ |
Patrick Venture | 7af157b | 2018-10-30 11:24:40 -0700 | [diff] [blame] | 53 | std::string path = getSensorPath(type, id); |
Patrick Venture | 34ddc90 | 2018-10-30 11:05:17 -0700 | [diff] [blame] | 54 | |
Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 55 | struct SensorProperties settings; |
Patrick Venture | f8cb464 | 2018-10-30 12:02:53 -0700 | [diff] [blame] | 56 | bool failed; |
Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 57 | |
Patrick Venture | f8cb464 | 2018-10-30 12:02:53 -0700 | [diff] [blame] | 58 | try |
| 59 | { |
| 60 | std::string service = helper->getService(tempBus, sensorintf, path); |
| 61 | |
| 62 | helper->getProperties(tempBus, service, path, &settings); |
| 63 | failed = helper->thresholdsAsserted(tempBus, service, path); |
| 64 | } |
| 65 | catch (const std::exception& e) |
| 66 | { |
| 67 | return nullptr; |
| 68 | } |
| 69 | |
Patrick Venture | 6b9f599 | 2019-09-10 09:18:28 -0700 | [diff] [blame] | 70 | /* if these values are zero, they're ignored. */ |
| 71 | if (info->ignoreDbusMinMax) |
| 72 | { |
| 73 | settings.min = 0; |
| 74 | settings.max = 0; |
| 75 | } |
| 76 | |
Patrick Venture | 8729eb9 | 2020-08-10 10:38:44 -0700 | [diff] [blame^] | 77 | return std::make_unique<DbusPassive>(bus, type, id, std::move(helper), |
| 78 | settings, failed, path, redundancy); |
Patrick Venture | f8cb464 | 2018-10-30 12:02:53 -0700 | [diff] [blame] | 79 | } |
| 80 | |
James Feist | 98b704e | 2019-06-03 16:24:53 -0700 | [diff] [blame] | 81 | DbusPassive::DbusPassive( |
| 82 | sdbusplus::bus::bus& bus, const std::string& type, const std::string& id, |
Patrick Venture | 8729eb9 | 2020-08-10 10:38:44 -0700 | [diff] [blame^] | 83 | std::unique_ptr<DbusHelperInterface> helper, |
| 84 | const struct SensorProperties& settings, bool failed, |
| 85 | const std::string& path, |
James Feist | 98b704e | 2019-06-03 16:24:53 -0700 | [diff] [blame] | 86 | const std::shared_ptr<DbusPassiveRedundancy>& redundancy) : |
Patrick Venture | f8cb464 | 2018-10-30 12:02:53 -0700 | [diff] [blame] | 87 | ReadInterface(), |
| 88 | _bus(bus), _signal(bus, getMatch(type, id).c_str(), dbusHandleSignal, this), |
Patrick Venture | 8729eb9 | 2020-08-10 10:38:44 -0700 | [diff] [blame^] | 89 | _id(id), _helper(std::move(helper)), _failed(failed), path(path), |
James Feist | 98b704e | 2019-06-03 16:24:53 -0700 | [diff] [blame] | 90 | redundancy(redundancy) |
| 91 | |
Patrick Venture | f8cb464 | 2018-10-30 12:02:53 -0700 | [diff] [blame] | 92 | { |
Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 93 | _scale = settings.scale; |
| 94 | _value = settings.value * pow(10, _scale); |
James Feist | 75eb769 | 2019-02-25 12:50:02 -0800 | [diff] [blame] | 95 | _min = settings.min * pow(10, _scale); |
| 96 | _max = settings.max * pow(10, _scale); |
Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 97 | _updated = std::chrono::high_resolution_clock::now(); |
| 98 | } |
| 99 | |
| 100 | ReadReturn DbusPassive::read(void) |
| 101 | { |
| 102 | std::lock_guard<std::mutex> guard(_lock); |
| 103 | |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 104 | struct ReadReturn r = {_value, _updated}; |
Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 105 | |
| 106 | return r; |
| 107 | } |
| 108 | |
| 109 | void DbusPassive::setValue(double value) |
| 110 | { |
| 111 | std::lock_guard<std::mutex> guard(_lock); |
| 112 | |
| 113 | _value = value; |
| 114 | _updated = std::chrono::high_resolution_clock::now(); |
| 115 | } |
| 116 | |
James Feist | 36b7d8e | 2018-10-05 15:39:01 -0700 | [diff] [blame] | 117 | bool DbusPassive::getFailed(void) const |
| 118 | { |
James Feist | 98b704e | 2019-06-03 16:24:53 -0700 | [diff] [blame] | 119 | if (redundancy) |
| 120 | { |
| 121 | const std::set<std::string>& failures = redundancy->getFailed(); |
| 122 | if (failures.find(path) != failures.end()) |
| 123 | { |
| 124 | return true; |
| 125 | } |
| 126 | } |
James Feist | 4b36f26 | 2020-07-07 16:56:41 -0700 | [diff] [blame] | 127 | |
| 128 | return _failed || !_functional; |
James Feist | 36b7d8e | 2018-10-05 15:39:01 -0700 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | void DbusPassive::setFailed(bool value) |
| 132 | { |
| 133 | _failed = value; |
| 134 | } |
| 135 | |
James Feist | 4b36f26 | 2020-07-07 16:56:41 -0700 | [diff] [blame] | 136 | void DbusPassive::setFunctional(bool value) |
| 137 | { |
| 138 | _functional = value; |
| 139 | } |
| 140 | |
Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 141 | int64_t DbusPassive::getScale(void) |
| 142 | { |
| 143 | return _scale; |
| 144 | } |
| 145 | |
Patrick Venture | 563a356 | 2018-10-30 09:31:26 -0700 | [diff] [blame] | 146 | std::string DbusPassive::getID(void) |
Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 147 | { |
| 148 | return _id; |
| 149 | } |
| 150 | |
James Feist | 75eb769 | 2019-02-25 12:50:02 -0800 | [diff] [blame] | 151 | double DbusPassive::getMax(void) |
| 152 | { |
| 153 | return _max; |
| 154 | } |
| 155 | |
| 156 | double DbusPassive::getMin(void) |
| 157 | { |
| 158 | return _min; |
| 159 | } |
| 160 | |
Patrick Venture | 7af157b | 2018-10-30 11:24:40 -0700 | [diff] [blame] | 161 | int handleSensorValue(sdbusplus::message::message& msg, DbusPassive* owner) |
Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 162 | { |
Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 163 | std::string msgSensor; |
James Feist | 1f802f5 | 2019-02-08 13:51:43 -0800 | [diff] [blame] | 164 | std::map<std::string, std::variant<int64_t, double, bool>> msgData; |
Patrick Venture | d0c7566 | 2018-06-12 19:03:21 -0700 | [diff] [blame] | 165 | |
| 166 | msg.read(msgSensor, msgData); |
Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 167 | |
| 168 | if (msgSensor == "xyz.openbmc_project.Sensor.Value") |
| 169 | { |
| 170 | auto valPropMap = msgData.find("Value"); |
| 171 | if (valPropMap != msgData.end()) |
| 172 | { |
James Feist | 1f802f5 | 2019-02-08 13:51:43 -0800 | [diff] [blame] | 173 | double value = |
| 174 | std::visit(VariantToDoubleVisitor(), valPropMap->second); |
Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 175 | |
James Feist | c065cf1 | 2018-07-05 10:23:11 -0700 | [diff] [blame] | 176 | value *= std::pow(10, owner->getScale()); |
Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 177 | |
James Feist | 75eb769 | 2019-02-25 12:50:02 -0800 | [diff] [blame] | 178 | scaleSensorReading(owner->getMin(), owner->getMax(), value); |
| 179 | |
Patrick Venture | d0c7566 | 2018-06-12 19:03:21 -0700 | [diff] [blame] | 180 | owner->setValue(value); |
Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 181 | } |
| 182 | } |
James Feist | 36b7d8e | 2018-10-05 15:39:01 -0700 | [diff] [blame] | 183 | else if (msgSensor == "xyz.openbmc_project.Sensor.Threshold.Critical") |
| 184 | { |
| 185 | auto criticalAlarmLow = msgData.find("CriticalAlarmLow"); |
| 186 | auto criticalAlarmHigh = msgData.find("CriticalAlarmHigh"); |
| 187 | if (criticalAlarmHigh == msgData.end() && |
| 188 | criticalAlarmLow == msgData.end()) |
| 189 | { |
| 190 | return 0; |
| 191 | } |
| 192 | |
| 193 | bool asserted = false; |
| 194 | if (criticalAlarmLow != msgData.end()) |
| 195 | { |
James Feist | 1f802f5 | 2019-02-08 13:51:43 -0800 | [diff] [blame] | 196 | asserted = std::get<bool>(criticalAlarmLow->second); |
James Feist | 36b7d8e | 2018-10-05 15:39:01 -0700 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | // checking both as in theory you could de-assert one threshold and |
| 200 | // assert the other at the same moment |
| 201 | if (!asserted && criticalAlarmHigh != msgData.end()) |
| 202 | { |
James Feist | 1f802f5 | 2019-02-08 13:51:43 -0800 | [diff] [blame] | 203 | asserted = std::get<bool>(criticalAlarmHigh->second); |
James Feist | 36b7d8e | 2018-10-05 15:39:01 -0700 | [diff] [blame] | 204 | } |
| 205 | owner->setFailed(asserted); |
| 206 | } |
James Feist | 4b36f26 | 2020-07-07 16:56:41 -0700 | [diff] [blame] | 207 | else if (msgSensor == |
| 208 | "xyz.openbmc_project.State.Decorator.OperationalStatus") |
| 209 | { |
| 210 | auto functional = msgData.find("Functional"); |
| 211 | if (functional == msgData.end()) |
| 212 | { |
| 213 | return 0; |
| 214 | } |
| 215 | bool asserted = std::get<bool>(functional->second); |
| 216 | owner->setFunctional(asserted); |
| 217 | } |
Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 218 | |
| 219 | return 0; |
| 220 | } |
Patrick Venture | d0c7566 | 2018-06-12 19:03:21 -0700 | [diff] [blame] | 221 | |
Patrick Venture | 7af157b | 2018-10-30 11:24:40 -0700 | [diff] [blame] | 222 | int dbusHandleSignal(sd_bus_message* msg, void* usrData, sd_bus_error* err) |
Patrick Venture | d0c7566 | 2018-06-12 19:03:21 -0700 | [diff] [blame] | 223 | { |
| 224 | auto sdbpMsg = sdbusplus::message::message(msg); |
| 225 | DbusPassive* obj = static_cast<DbusPassive*>(usrData); |
| 226 | |
Patrick Venture | 7af157b | 2018-10-30 11:24:40 -0700 | [diff] [blame] | 227 | return handleSensorValue(sdbpMsg, obj); |
Patrick Venture | d0c7566 | 2018-06-12 19:03:21 -0700 | [diff] [blame] | 228 | } |
Patrick Venture | a076487 | 2020-08-08 07:48:43 -0700 | [diff] [blame] | 229 | |
| 230 | } // namespace pid_control |