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