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