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