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