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