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