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 | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 16 | #include <chrono> |
| 17 | #include <cmath> |
Patrick Venture | 0ef1faf | 2018-06-13 12:50:53 -0700 | [diff] [blame] | 18 | #include <memory> |
Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 19 | #include <mutex> |
Patrick Venture | 0ef1faf | 2018-06-13 12:50:53 -0700 | [diff] [blame] | 20 | #include <sdbusplus/bus.hpp> |
| 21 | #include <string> |
Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 22 | |
| 23 | #include "dbuspassive.hpp" |
Patrick Venture | 0ef1faf | 2018-06-13 12:50:53 -0700 | [diff] [blame] | 24 | #include "dbus/util.hpp" |
| 25 | |
| 26 | std::unique_ptr<ReadInterface> DbusPassive::CreateDbusPassive( |
| 27 | sdbusplus::bus::bus& bus, const std::string& type, |
| 28 | const std::string& id, DbusHelperInterface *helper) |
| 29 | { |
| 30 | if (helper == nullptr) |
| 31 | { |
| 32 | return nullptr; |
| 33 | } |
| 34 | if (!ValidType(type)) |
| 35 | { |
| 36 | return nullptr; |
| 37 | } |
| 38 | |
| 39 | return std::make_unique<DbusPassive>(bus, type, id, helper); |
| 40 | } |
Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 41 | |
| 42 | DbusPassive::DbusPassive( |
| 43 | sdbusplus::bus::bus& bus, |
| 44 | const std::string& type, |
Patrick Venture | 0df7c0f | 2018-06-13 09:02:13 -0700 | [diff] [blame] | 45 | const std::string& id, |
| 46 | DbusHelperInterface *helper) |
Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 47 | : ReadInterface(), |
| 48 | _bus(bus), |
| 49 | _signal(bus, GetMatch(type, id).c_str(), DbusHandleSignal, this), |
Patrick Venture | 0df7c0f | 2018-06-13 09:02:13 -0700 | [diff] [blame] | 50 | _id(id), |
| 51 | _helper(helper) |
Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 52 | { |
| 53 | /* Need to get the scale and initial value */ |
| 54 | auto tempBus = sdbusplus::bus::new_default(); |
| 55 | /* service == busname */ |
| 56 | std::string path = GetSensorPath(type, id); |
Patrick Venture | 0df7c0f | 2018-06-13 09:02:13 -0700 | [diff] [blame] | 57 | std::string service = _helper->GetService(tempBus, sensorintf, path); |
Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 58 | |
| 59 | struct SensorProperties settings; |
Patrick Venture | 0df7c0f | 2018-06-13 09:02:13 -0700 | [diff] [blame] | 60 | _helper->GetProperties(tempBus, service, path, &settings); |
Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 61 | |
| 62 | _scale = settings.scale; |
| 63 | _value = settings.value * pow(10, _scale); |
| 64 | _updated = std::chrono::high_resolution_clock::now(); |
| 65 | } |
| 66 | |
| 67 | ReadReturn DbusPassive::read(void) |
| 68 | { |
| 69 | std::lock_guard<std::mutex> guard(_lock); |
| 70 | |
| 71 | struct ReadReturn r = { |
| 72 | _value, |
| 73 | _updated |
| 74 | }; |
| 75 | |
| 76 | return r; |
| 77 | } |
| 78 | |
| 79 | void DbusPassive::setValue(double value) |
| 80 | { |
| 81 | std::lock_guard<std::mutex> guard(_lock); |
| 82 | |
| 83 | _value = value; |
| 84 | _updated = std::chrono::high_resolution_clock::now(); |
| 85 | } |
| 86 | |
| 87 | int64_t DbusPassive::getScale(void) |
| 88 | { |
| 89 | return _scale; |
| 90 | } |
| 91 | |
| 92 | std::string DbusPassive::getId(void) |
| 93 | { |
| 94 | return _id; |
| 95 | } |
| 96 | |
Patrick Venture | d0c7566 | 2018-06-12 19:03:21 -0700 | [diff] [blame] | 97 | int HandleSensorValue(sdbusplus::message::message& msg, DbusPassive* owner) |
Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 98 | { |
Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 99 | std::string msgSensor; |
James Feist | c065cf1 | 2018-07-05 10:23:11 -0700 | [diff] [blame] | 100 | std::map<std::string, sdbusplus::message::variant<int64_t, double>> msgData; |
Patrick Venture | d0c7566 | 2018-06-12 19:03:21 -0700 | [diff] [blame] | 101 | |
| 102 | msg.read(msgSensor, msgData); |
Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 103 | |
| 104 | if (msgSensor == "xyz.openbmc_project.Sensor.Value") |
| 105 | { |
| 106 | auto valPropMap = msgData.find("Value"); |
| 107 | if (valPropMap != msgData.end()) |
| 108 | { |
James Feist | c065cf1 | 2018-07-05 10:23:11 -0700 | [diff] [blame] | 109 | double value = mapbox::util::apply_visitor(VariantToDoubleVisitor(), |
| 110 | valPropMap->second); |
Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 111 | |
James Feist | c065cf1 | 2018-07-05 10:23:11 -0700 | [diff] [blame] | 112 | value *= std::pow(10, owner->getScale()); |
Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 113 | |
Patrick Venture | d0c7566 | 2018-06-12 19:03:21 -0700 | [diff] [blame] | 114 | owner->setValue(value); |
Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 115 | } |
| 116 | } |
| 117 | |
| 118 | return 0; |
| 119 | } |
Patrick Venture | d0c7566 | 2018-06-12 19:03:21 -0700 | [diff] [blame] | 120 | |
| 121 | int DbusHandleSignal(sd_bus_message* msg, void* usrData, sd_bus_error* err) |
| 122 | { |
| 123 | auto sdbpMsg = sdbusplus::message::message(msg); |
| 124 | DbusPassive* obj = static_cast<DbusPassive*>(usrData); |
| 125 | |
| 126 | return HandleSensorValue(sdbpMsg, obj); |
| 127 | } |