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 | */ |
| 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, |
Patrick Venture | 0df7c0f | 2018-06-13 09:02:13 -0700 | [diff] [blame^] | 26 | const std::string& id, |
| 27 | DbusHelperInterface *helper) |
Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 28 | : ReadInterface(), |
| 29 | _bus(bus), |
| 30 | _signal(bus, GetMatch(type, id).c_str(), DbusHandleSignal, this), |
Patrick Venture | 0df7c0f | 2018-06-13 09:02:13 -0700 | [diff] [blame^] | 31 | _id(id), |
| 32 | _helper(helper) |
Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 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); |
Patrick Venture | 0df7c0f | 2018-06-13 09:02:13 -0700 | [diff] [blame^] | 38 | std::string service = _helper->GetService(tempBus, sensorintf, path); |
Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 39 | |
| 40 | struct SensorProperties settings; |
Patrick Venture | 0df7c0f | 2018-06-13 09:02:13 -0700 | [diff] [blame^] | 41 | _helper->GetProperties(tempBus, service, path, &settings); |
Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 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 | |
Patrick Venture | d0c7566 | 2018-06-12 19:03:21 -0700 | [diff] [blame] | 78 | int HandleSensorValue(sdbusplus::message::message& msg, DbusPassive* owner) |
Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 79 | { |
Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 80 | std::string msgSensor; |
Patrick Venture | d0c7566 | 2018-06-12 19:03:21 -0700 | [diff] [blame] | 81 | std::map<std::string, sdbusplus::message::variant<int64_t>> msgData; |
| 82 | |
| 83 | msg.read(msgSensor, msgData); |
Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 84 | |
| 85 | if (msgSensor == "xyz.openbmc_project.Sensor.Value") |
| 86 | { |
| 87 | auto valPropMap = msgData.find("Value"); |
| 88 | if (valPropMap != msgData.end()) |
| 89 | { |
Patrick Venture | d0c7566 | 2018-06-12 19:03:21 -0700 | [diff] [blame] | 90 | int64_t rawValue = sdbusplus::message::variant_ns::get<int64_t>( |
| 91 | valPropMap->second); |
Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 92 | |
Patrick Venture | d0c7566 | 2018-06-12 19:03:21 -0700 | [diff] [blame] | 93 | double value = rawValue * std::pow(10, owner->getScale()); |
Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 94 | |
Patrick Venture | d0c7566 | 2018-06-12 19:03:21 -0700 | [diff] [blame] | 95 | owner->setValue(value); |
Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 96 | } |
| 97 | } |
| 98 | |
| 99 | return 0; |
| 100 | } |
Patrick Venture | d0c7566 | 2018-06-12 19:03:21 -0700 | [diff] [blame] | 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 | } |