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 17 #include <chrono> 18 #include <cmath> 19 #include <mutex> 20 21 #include "dbuspassive.hpp" 22 23 DbusPassive::DbusPassive( 24 sdbusplus::bus::bus& bus, 25 const std::string& type, 26 const std::string& id, 27 DbusHelperInterface *helper) 28 : ReadInterface(), 29 _bus(bus), 30 _signal(bus, GetMatch(type, id).c_str(), DbusHandleSignal, this), 31 _id(id), 32 _helper(helper) 33 { 34 /* Need to get the scale and initial value */ 35 auto tempBus = sdbusplus::bus::new_default(); 36 /* service == busname */ 37 std::string path = GetSensorPath(type, id); 38 std::string service = _helper->GetService(tempBus, sensorintf, path); 39 40 struct SensorProperties settings; 41 _helper->GetProperties(tempBus, service, path, &settings); 42 43 _scale = settings.scale; 44 _value = settings.value * pow(10, _scale); 45 _updated = std::chrono::high_resolution_clock::now(); 46 } 47 48 ReadReturn DbusPassive::read(void) 49 { 50 std::lock_guard<std::mutex> guard(_lock); 51 52 struct ReadReturn r = { 53 _value, 54 _updated 55 }; 56 57 return r; 58 } 59 60 void DbusPassive::setValue(double value) 61 { 62 std::lock_guard<std::mutex> guard(_lock); 63 64 _value = value; 65 _updated = std::chrono::high_resolution_clock::now(); 66 } 67 68 int64_t DbusPassive::getScale(void) 69 { 70 return _scale; 71 } 72 73 std::string DbusPassive::getId(void) 74 { 75 return _id; 76 } 77 78 int HandleSensorValue(sdbusplus::message::message& msg, DbusPassive* owner) 79 { 80 std::string msgSensor; 81 std::map<std::string, sdbusplus::message::variant<int64_t>> msgData; 82 83 msg.read(msgSensor, msgData); 84 85 if (msgSensor == "xyz.openbmc_project.Sensor.Value") 86 { 87 auto valPropMap = msgData.find("Value"); 88 if (valPropMap != msgData.end()) 89 { 90 int64_t rawValue = sdbusplus::message::variant_ns::get<int64_t>( 91 valPropMap->second); 92 93 double value = rawValue * std::pow(10, owner->getScale()); 94 95 owner->setValue(value); 96 } 97 } 98 99 return 0; 100 } 101 102 int DbusHandleSignal(sd_bus_message* msg, void* usrData, sd_bus_error* err) 103 { 104 auto sdbpMsg = sdbusplus::message::message(msg); 105 DbusPassive* obj = static_cast<DbusPassive*>(usrData); 106 107 return HandleSensorValue(sdbpMsg, obj); 108 } 109