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