Matt Spinler | e2b25cb | 2017-04-10 14:33:35 -0500 | [diff] [blame] | 1 | /** |
| 2 | * Copyright © 2017 IBM 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 | #include <phosphor-logging/log.hpp> |
Dinesh Chinari | 618027a | 2017-06-26 23:26:50 -0500 | [diff] [blame] | 17 | #include <phosphor-logging/elog.hpp> |
| 18 | #include <phosphor-logging/elog-errors.hpp> |
| 19 | #include <xyz/openbmc_project/Common/error.hpp> |
Matt Spinler | e2b25cb | 2017-04-10 14:33:35 -0500 | [diff] [blame] | 20 | #include <string> |
| 21 | #include "fan.hpp" |
| 22 | #include "utility.hpp" |
Matthew Barth | 2b3db61 | 2017-10-25 10:56:51 -0500 | [diff] [blame] | 23 | #include "sdbusplus.hpp" |
Matt Spinler | e2b25cb | 2017-04-10 14:33:35 -0500 | [diff] [blame] | 24 | |
| 25 | namespace phosphor |
| 26 | { |
| 27 | namespace fan |
| 28 | { |
| 29 | namespace control |
| 30 | { |
| 31 | |
Dinesh Chinari | 618027a | 2017-06-26 23:26:50 -0500 | [diff] [blame] | 32 | // For throwing exception |
| 33 | using namespace phosphor::logging; |
| 34 | using InternalFailure = sdbusplus::xyz::openbmc_project::Common:: |
| 35 | Error::InternalFailure; |
| 36 | |
Matt Spinler | e2b25cb | 2017-04-10 14:33:35 -0500 | [diff] [blame] | 37 | constexpr auto PROPERTY_INTERFACE = "org.freedesktop.DBus.Properties"; |
| 38 | constexpr auto FAN_SENSOR_PATH = "/xyz/openbmc_project/sensors/fan_tach/"; |
Matt Spinler | e2b25cb | 2017-04-10 14:33:35 -0500 | [diff] [blame] | 39 | constexpr auto FAN_TARGET_PROPERTY = "Target"; |
| 40 | |
| 41 | |
| 42 | Fan::Fan(sdbusplus::bus::bus& bus, const FanDefinition& def): |
| 43 | _bus(bus), |
Lei YU | 069e440 | 2018-01-31 16:47:37 +0800 | [diff] [blame^] | 44 | _name(std::get<fanNamePos>(def)), |
| 45 | _interface(std::get<targetInterfacePos>(def)) |
Matt Spinler | e2b25cb | 2017-04-10 14:33:35 -0500 | [diff] [blame] | 46 | { |
Matthew Barth | 2b3db61 | 2017-10-25 10:56:51 -0500 | [diff] [blame] | 47 | std::string path; |
Matt Spinler | e2b25cb | 2017-04-10 14:33:35 -0500 | [diff] [blame] | 48 | auto sensors = std::get<sensorListPos>(def); |
| 49 | for (auto& s : sensors) |
| 50 | { |
Matthew Barth | 2b3db61 | 2017-10-25 10:56:51 -0500 | [diff] [blame] | 51 | path = FAN_SENSOR_PATH + s; |
| 52 | _sensors.emplace_back(path); |
| 53 | } |
| 54 | // All sensors associated with this fan are set to same target speed, |
| 55 | // so only need to read target property from one. |
| 56 | if (!path.empty()) |
| 57 | { |
| 58 | _targetSpeed = util::SDBusPlus::getProperty<uint64_t>( |
| 59 | bus, |
| 60 | path, |
Lei YU | 069e440 | 2018-01-31 16:47:37 +0800 | [diff] [blame^] | 61 | _interface, |
Matthew Barth | 2b3db61 | 2017-10-25 10:56:51 -0500 | [diff] [blame] | 62 | FAN_TARGET_PROPERTY); |
Matt Spinler | e2b25cb | 2017-04-10 14:33:35 -0500 | [diff] [blame] | 63 | } |
| 64 | } |
| 65 | |
| 66 | |
| 67 | //TODO openbmc/openbmc#1524 Can cache this value when |
| 68 | //openbmc/openbmc#1496 is resolved. |
| 69 | std::string Fan::getService(const std::string& sensor) |
| 70 | { |
| 71 | return phosphor::fan::util::getService(sensor, |
Lei YU | 069e440 | 2018-01-31 16:47:37 +0800 | [diff] [blame^] | 72 | _interface, |
Matt Spinler | e2b25cb | 2017-04-10 14:33:35 -0500 | [diff] [blame] | 73 | _bus); |
| 74 | } |
| 75 | |
| 76 | |
| 77 | void Fan::setSpeed(uint64_t speed) |
| 78 | { |
| 79 | sdbusplus::message::variant<uint64_t> value = speed; |
| 80 | std::string property{FAN_TARGET_PROPERTY}; |
| 81 | |
| 82 | for (auto& sensor : _sensors) |
| 83 | { |
Dinesh Chinari | 618027a | 2017-06-26 23:26:50 -0500 | [diff] [blame] | 84 | auto service = getService(sensor); |
Matt Spinler | e2b25cb | 2017-04-10 14:33:35 -0500 | [diff] [blame] | 85 | |
Dinesh Chinari | 618027a | 2017-06-26 23:26:50 -0500 | [diff] [blame] | 86 | auto method = _bus.new_method_call(service.c_str(), |
Matthew Barth | 2081972 | 2017-11-08 10:00:35 -0600 | [diff] [blame] | 87 | sensor.c_str(), |
| 88 | PROPERTY_INTERFACE, |
| 89 | "Set"); |
Lei YU | 069e440 | 2018-01-31 16:47:37 +0800 | [diff] [blame^] | 90 | method.append(_interface, property, value); |
Matt Spinler | e2b25cb | 2017-04-10 14:33:35 -0500 | [diff] [blame] | 91 | |
Dinesh Chinari | 618027a | 2017-06-26 23:26:50 -0500 | [diff] [blame] | 92 | auto response = _bus.call(method); |
| 93 | if (response.is_method_error()) |
Matt Spinler | e2b25cb | 2017-04-10 14:33:35 -0500 | [diff] [blame] | 94 | { |
Dinesh Chinari | 618027a | 2017-06-26 23:26:50 -0500 | [diff] [blame] | 95 | log<level::ERR>( |
| 96 | "Failed call to set fan speed ", entry("SENSOR=%s", sensor)); |
| 97 | elog<InternalFailure>(); |
Matt Spinler | e2b25cb | 2017-04-10 14:33:35 -0500 | [diff] [blame] | 98 | } |
| 99 | } |
Matthew Barth | 2b3db61 | 2017-10-25 10:56:51 -0500 | [diff] [blame] | 100 | |
| 101 | _targetSpeed = speed; |
Matt Spinler | e2b25cb | 2017-04-10 14:33:35 -0500 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | } |
| 105 | } |
| 106 | } |