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 | */ |
Matt Spinler | e2b25cb | 2017-04-10 14:33:35 -0500 | [diff] [blame] | 16 | #include <string> |
| 17 | #include "fan.hpp" |
Matthew Barth | 2b3db61 | 2017-10-25 10:56:51 -0500 | [diff] [blame] | 18 | #include "sdbusplus.hpp" |
Matt Spinler | e2b25cb | 2017-04-10 14:33:35 -0500 | [diff] [blame] | 19 | |
| 20 | namespace phosphor |
| 21 | { |
| 22 | namespace fan |
| 23 | { |
| 24 | namespace control |
| 25 | { |
| 26 | |
Dinesh Chinari | 618027a | 2017-06-26 23:26:50 -0500 | [diff] [blame] | 27 | // For throwing exception |
| 28 | using namespace phosphor::logging; |
Dinesh Chinari | 618027a | 2017-06-26 23:26:50 -0500 | [diff] [blame] | 29 | |
Matt Spinler | e2b25cb | 2017-04-10 14:33:35 -0500 | [diff] [blame] | 30 | constexpr auto FAN_SENSOR_PATH = "/xyz/openbmc_project/sensors/fan_tach/"; |
Matt Spinler | e2b25cb | 2017-04-10 14:33:35 -0500 | [diff] [blame] | 31 | constexpr auto FAN_TARGET_PROPERTY = "Target"; |
| 32 | |
Matt Spinler | e2b25cb | 2017-04-10 14:33:35 -0500 | [diff] [blame] | 33 | Fan::Fan(sdbusplus::bus::bus& bus, const FanDefinition& def): |
| 34 | _bus(bus), |
Lei YU | 069e440 | 2018-01-31 16:47:37 +0800 | [diff] [blame] | 35 | _name(std::get<fanNamePos>(def)), |
| 36 | _interface(std::get<targetInterfacePos>(def)) |
Matt Spinler | e2b25cb | 2017-04-10 14:33:35 -0500 | [diff] [blame] | 37 | { |
Matthew Barth | 2b3db61 | 2017-10-25 10:56:51 -0500 | [diff] [blame] | 38 | std::string path; |
Matt Spinler | e2b25cb | 2017-04-10 14:33:35 -0500 | [diff] [blame] | 39 | auto sensors = std::get<sensorListPos>(def); |
| 40 | for (auto& s : sensors) |
| 41 | { |
Matthew Barth | 2b3db61 | 2017-10-25 10:56:51 -0500 | [diff] [blame] | 42 | path = FAN_SENSOR_PATH + s; |
Matthew Barth | 1061cba | 2018-05-09 14:18:16 -0500 | [diff] [blame] | 43 | auto service = util::SDBusPlus::getService( |
| 44 | bus, |
| 45 | path, |
| 46 | _interface); |
| 47 | _sensors[path] = service; |
Matthew Barth | 2b3db61 | 2017-10-25 10:56:51 -0500 | [diff] [blame] | 48 | } |
Matthew Barth | 1061cba | 2018-05-09 14:18:16 -0500 | [diff] [blame] | 49 | // 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] | 50 | // so only need to read target property from one. |
| 51 | if (!path.empty()) |
| 52 | { |
Matthew Barth | 1061cba | 2018-05-09 14:18:16 -0500 | [diff] [blame] | 53 | // Use getProperty with service lookup since each target sensor |
| 54 | // path given could have different services providing them |
Matthew Barth | 2b3db61 | 2017-10-25 10:56:51 -0500 | [diff] [blame] | 55 | _targetSpeed = util::SDBusPlus::getProperty<uint64_t>( |
| 56 | bus, |
| 57 | path, |
Lei YU | 069e440 | 2018-01-31 16:47:37 +0800 | [diff] [blame] | 58 | _interface, |
Matthew Barth | 2b3db61 | 2017-10-25 10:56:51 -0500 | [diff] [blame] | 59 | FAN_TARGET_PROPERTY); |
Matt Spinler | e2b25cb | 2017-04-10 14:33:35 -0500 | [diff] [blame] | 60 | } |
| 61 | } |
| 62 | |
Matt Spinler | e2b25cb | 2017-04-10 14:33:35 -0500 | [diff] [blame] | 63 | void Fan::setSpeed(uint64_t speed) |
| 64 | { |
Matt Spinler | e2b25cb | 2017-04-10 14:33:35 -0500 | [diff] [blame] | 65 | for (auto& sensor : _sensors) |
| 66 | { |
Matthew Barth | 26e9612 | 2018-05-09 14:55:41 -0500 | [diff] [blame] | 67 | auto value = speed; |
Matthew Barth | 86be476 | 2018-07-17 10:51:36 -0500 | [diff] [blame] | 68 | try |
| 69 | { |
| 70 | util::SDBusPlus::setProperty<uint64_t>( |
| 71 | _bus, |
| 72 | sensor.second, |
| 73 | sensor.first, |
| 74 | _interface, |
| 75 | FAN_TARGET_PROPERTY, |
| 76 | std::move(value)); |
| 77 | } |
| 78 | catch (const sdbusplus::exception::SdBusError&) |
| 79 | { |
| 80 | throw util::DBusPropertyError{ |
| 81 | "DBus set property failed", |
| 82 | sensor.second, |
| 83 | sensor.first, |
| 84 | _interface, |
| 85 | FAN_TARGET_PROPERTY}; |
| 86 | } |
Matt Spinler | e2b25cb | 2017-04-10 14:33:35 -0500 | [diff] [blame] | 87 | } |
Matthew Barth | 2b3db61 | 2017-10-25 10:56:51 -0500 | [diff] [blame] | 88 | |
| 89 | _targetSpeed = speed; |
Matt Spinler | e2b25cb | 2017-04-10 14:33:35 -0500 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | } |
| 93 | } |
| 94 | } |