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