blob: d6fca5456b9e7daccb7ac5a33b5e7459f3e7bd71 [file] [log] [blame]
Matt Spinlere2b25cb2017-04-10 14:33:35 -05001/**
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 Spinlere2b25cb2017-04-10 14:33:35 -050016#include "fan.hpp"
Matthew Barth3e1bb272020-05-26 11:09:21 -050017
Matthew Barth2b3db612017-10-25 10:56:51 -050018#include "sdbusplus.hpp"
Matt Spinlere2b25cb2017-04-10 14:33:35 -050019
Matthew Barth3e1bb272020-05-26 11:09:21 -050020#include <string>
21
Matt Spinlere2b25cb2017-04-10 14:33:35 -050022namespace phosphor
23{
24namespace fan
25{
26namespace control
27{
28
Matt Spinlere2b25cb2017-04-10 14:33:35 -050029constexpr auto FAN_SENSOR_PATH = "/xyz/openbmc_project/sensors/fan_tach/";
Matt Spinlere2b25cb2017-04-10 14:33:35 -050030constexpr auto FAN_TARGET_PROPERTY = "Target";
31
Patrick Williamscb356d42022-07-22 19:26:53 -050032Fan::Fan(sdbusplus::bus_t& bus, const FanDefinition& def) :
Matthew Barth3e1bb272020-05-26 11:09:21 -050033 _bus(bus), _name(std::get<fanNamePos>(def)),
Lei YU069e4402018-01-31 16:47:37 +080034 _interface(std::get<targetInterfacePos>(def))
Matt Spinlere2b25cb2017-04-10 14:33:35 -050035{
Matthew Barth2b3db612017-10-25 10:56:51 -050036 std::string path;
Matt Spinlere2b25cb2017-04-10 14:33:35 -050037 auto sensors = std::get<sensorListPos>(def);
38 for (auto& s : sensors)
39 {
Chau Ly44872b02022-09-19 07:34:08 +000040 path = std::get<targetControlPathPos>(def) + s;
Matthew Barth3e1bb272020-05-26 11:09:21 -050041 auto service = util::SDBusPlus::getService(bus, path, _interface);
Matthew Barth1061cba2018-05-09 14:18:16 -050042 _sensors[path] = service;
Matthew Barth2b3db612017-10-25 10:56:51 -050043 }
Matthew Barth1061cba2018-05-09 14:18:16 -050044 // All sensors associated with this fan are set to the same target speed,
Matthew Barth2b3db612017-10-25 10:56:51 -050045 // so only need to read target property from one.
46 if (!path.empty())
47 {
Matthew Barth1061cba2018-05-09 14:18:16 -050048 // Use getProperty with service lookup since each target sensor
49 // path given could have different services providing them
Matthew Barth2b3db612017-10-25 10:56:51 -050050 _targetSpeed = util::SDBusPlus::getProperty<uint64_t>(
Matthew Barth3e1bb272020-05-26 11:09:21 -050051 bus, path, _interface, FAN_TARGET_PROPERTY);
Matt Spinlere2b25cb2017-04-10 14:33:35 -050052 }
53}
54
Matt Spinlere2b25cb2017-04-10 14:33:35 -050055void Fan::setSpeed(uint64_t speed)
56{
Matt Spinlere2b25cb2017-04-10 14:33:35 -050057 for (auto& sensor : _sensors)
58 {
Matthew Barth26e96122018-05-09 14:55:41 -050059 auto value = speed;
Matthew Barth86be4762018-07-17 10:51:36 -050060 try
61 {
62 util::SDBusPlus::setProperty<uint64_t>(
Matthew Barth3e1bb272020-05-26 11:09:21 -050063 _bus, sensor.second, sensor.first, _interface,
64 FAN_TARGET_PROPERTY, std::move(value));
Matthew Barth86be4762018-07-17 10:51:36 -050065 }
Patrick Williamscb356d42022-07-22 19:26:53 -050066 catch (const sdbusplus::exception_t&)
Matthew Barth86be4762018-07-17 10:51:36 -050067 {
Matthew Barth3e1bb272020-05-26 11:09:21 -050068 throw util::DBusPropertyError{"DBus set property failed",
69 sensor.second, sensor.first,
70 _interface, FAN_TARGET_PROPERTY};
Matthew Barth86be4762018-07-17 10:51:36 -050071 }
Matt Spinlere2b25cb2017-04-10 14:33:35 -050072 }
Matthew Barth2b3db612017-10-25 10:56:51 -050073
74 _targetSpeed = speed;
Matt Spinlere2b25cb2017-04-10 14:33:35 -050075}
76
Matthew Barth3e1bb272020-05-26 11:09:21 -050077} // namespace control
78} // namespace fan
79} // namespace phosphor