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