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