James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 1 | /* |
| 2 | // Copyright (c) 2018 Intel Corporation |
| 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 | |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 17 | #include "dbus/dbuswrite.hpp" |
| 18 | |
James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 19 | #include <iostream> |
Patrick Venture | f5e770b | 2018-10-30 12:28:53 -0700 | [diff] [blame] | 20 | #include <memory> |
Patrick Venture | 76ce5d7 | 2018-10-30 18:39:50 -0700 | [diff] [blame] | 21 | #include <phosphor-logging/log.hpp> |
James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 22 | #include <sdbusplus/bus.hpp> |
Patrick Venture | f5e770b | 2018-10-30 12:28:53 -0700 | [diff] [blame] | 23 | #include <string> |
| 24 | |
| 25 | constexpr const char* pwmInterface = "xyz.openbmc_project.Control.FanPwm"; |
James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 26 | |
Patrick Venture | 76ce5d7 | 2018-10-30 18:39:50 -0700 | [diff] [blame] | 27 | using namespace phosphor::logging; |
| 28 | |
Patrick Venture | f5e770b | 2018-10-30 12:28:53 -0700 | [diff] [blame] | 29 | std::unique_ptr<WriteInterface> |
| 30 | DbusWritePercent::createDbusWrite(const std::string& path, int64_t min, |
| 31 | int64_t max, DbusHelperInterface& helper) |
| 32 | { |
James Feist | 9fa90c1 | 2019-01-11 15:35:22 -0800 | [diff] [blame] | 33 | auto tempBus = sdbusplus::bus::new_system(); |
Patrick Venture | f5e770b | 2018-10-30 12:28:53 -0700 | [diff] [blame] | 34 | std::string connectionName; |
| 35 | |
| 36 | try |
| 37 | { |
| 38 | connectionName = helper.getService(tempBus, pwmInterface, path); |
| 39 | } |
| 40 | catch (const std::exception& e) |
| 41 | { |
| 42 | return nullptr; |
| 43 | } |
| 44 | |
| 45 | return std::make_unique<DbusWritePercent>(path, min, max, connectionName); |
| 46 | } |
| 47 | |
James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 48 | void DbusWritePercent::write(double value) |
| 49 | { |
Patrick Venture | 5f59c0f | 2018-11-11 12:55:14 -0800 | [diff] [blame] | 50 | double minimum = getMin(); |
| 51 | double maximum = getMax(); |
James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 52 | |
Patrick Venture | 5f59c0f | 2018-11-11 12:55:14 -0800 | [diff] [blame] | 53 | double range = maximum - minimum; |
| 54 | double offset = range * value; |
| 55 | double ovalue = offset + minimum; |
James Feist | cd9e109 | 2018-10-08 13:06:41 -0700 | [diff] [blame] | 56 | |
| 57 | if (oldValue == static_cast<int64_t>(ovalue)) |
| 58 | { |
| 59 | return; |
| 60 | } |
James Feist | 80b5d9a | 2019-01-15 12:51:09 -0800 | [diff] [blame] | 61 | auto writeBus = sdbusplus::bus::new_default(); |
James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 62 | auto mesg = |
James Feist | 80b5d9a | 2019-01-15 12:51:09 -0800 | [diff] [blame] | 63 | writeBus.new_method_call(connectionName.c_str(), path.c_str(), |
| 64 | "org.freedesktop.DBus.Properties", "Set"); |
James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 65 | mesg.append(pwmInterface, "Target", |
| 66 | sdbusplus::message::variant<uint64_t>(ovalue)); |
Patrick Venture | 76ce5d7 | 2018-10-30 18:39:50 -0700 | [diff] [blame] | 67 | |
| 68 | try |
James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 69 | { |
Patrick Venture | 76ce5d7 | 2018-10-30 18:39:50 -0700 | [diff] [blame] | 70 | // TODO: if we don't use the reply, call_noreply() |
James Feist | 80b5d9a | 2019-01-15 12:51:09 -0800 | [diff] [blame] | 71 | auto resp = writeBus.call(mesg); |
James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 72 | } |
Patrick Venture | 76ce5d7 | 2018-10-30 18:39:50 -0700 | [diff] [blame] | 73 | catch (const sdbusplus::exception::SdBusError& ex) |
| 74 | { |
| 75 | log<level::ERR>("Dbus Call Failure", entry("PATH=%s", path.c_str()), |
| 76 | entry("WHAT=%s", ex.what())); |
| 77 | } |
| 78 | |
James Feist | cd9e109 | 2018-10-08 13:06:41 -0700 | [diff] [blame] | 79 | oldValue = static_cast<int64_t>(ovalue); |
James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 80 | return; |
| 81 | } |
| 82 | |
Patrick Venture | f5e770b | 2018-10-30 12:28:53 -0700 | [diff] [blame] | 83 | std::unique_ptr<WriteInterface> |
| 84 | DbusWrite::createDbusWrite(const std::string& path, int64_t min, |
| 85 | int64_t max, DbusHelperInterface& helper) |
| 86 | { |
James Feist | 9fa90c1 | 2019-01-11 15:35:22 -0800 | [diff] [blame] | 87 | auto tempBus = sdbusplus::bus::new_system(); |
Patrick Venture | f5e770b | 2018-10-30 12:28:53 -0700 | [diff] [blame] | 88 | std::string connectionName; |
| 89 | |
| 90 | try |
| 91 | { |
| 92 | connectionName = helper.getService(tempBus, pwmInterface, path); |
| 93 | } |
| 94 | catch (const std::exception& e) |
| 95 | { |
| 96 | return nullptr; |
| 97 | } |
| 98 | |
| 99 | return std::make_unique<DbusWrite>(path, min, max, connectionName); |
| 100 | } |
| 101 | |
James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 102 | void DbusWrite::write(double value) |
| 103 | { |
James Feist | cd9e109 | 2018-10-08 13:06:41 -0700 | [diff] [blame] | 104 | if (oldValue == static_cast<int64_t>(value)) |
| 105 | { |
| 106 | return; |
| 107 | } |
James Feist | 80b5d9a | 2019-01-15 12:51:09 -0800 | [diff] [blame] | 108 | auto writeBus = sdbusplus::bus::new_default(); |
James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 109 | auto mesg = |
James Feist | 80b5d9a | 2019-01-15 12:51:09 -0800 | [diff] [blame] | 110 | writeBus.new_method_call(connectionName.c_str(), path.c_str(), |
| 111 | "org.freedesktop.DBus.Properties", "Set"); |
James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 112 | mesg.append(pwmInterface, "Target", |
| 113 | sdbusplus::message::variant<uint64_t>(value)); |
Patrick Venture | 76ce5d7 | 2018-10-30 18:39:50 -0700 | [diff] [blame] | 114 | |
| 115 | try |
James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 116 | { |
Patrick Venture | 76ce5d7 | 2018-10-30 18:39:50 -0700 | [diff] [blame] | 117 | // TODO: consider call_noreplly |
James Feist | 80b5d9a | 2019-01-15 12:51:09 -0800 | [diff] [blame] | 118 | auto resp = writeBus.call(mesg); |
James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 119 | } |
Patrick Venture | 76ce5d7 | 2018-10-30 18:39:50 -0700 | [diff] [blame] | 120 | catch (const sdbusplus::exception::SdBusError& ex) |
| 121 | { |
| 122 | log<level::ERR>("Dbus Call Failure", entry("PATH=%s", path.c_str()), |
| 123 | entry("WHAT=%s", ex.what())); |
| 124 | } |
| 125 | |
James Feist | cd9e109 | 2018-10-08 13:06:41 -0700 | [diff] [blame] | 126 | oldValue = static_cast<int64_t>(value); |
James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 127 | return; |
| 128 | } |