blob: 581d708672526c3a83d201afd90a31399dd49dc7 [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_TARGET_PROPERTY = "Target";
30
Patrick Williamscb356d42022-07-22 19:26:53 -050031Fan::Fan(sdbusplus::bus_t& bus, const FanDefinition& def) :
Matthew Barth3e1bb272020-05-26 11:09:21 -050032 _bus(bus), _name(std::get<fanNamePos>(def)),
Lei YU069e4402018-01-31 16:47:37 +080033 _interface(std::get<targetInterfacePos>(def))
Matt Spinlere2b25cb2017-04-10 14:33:35 -050034{
Matthew Barth2b3db612017-10-25 10:56:51 -050035 std::string path;
Matt Spinlere2b25cb2017-04-10 14:33:35 -050036 auto sensors = std::get<sensorListPos>(def);
37 for (auto& s : sensors)
38 {
Chau Ly44872b02022-09-19 07:34:08 +000039 path = std::get<targetControlPathPos>(def) + s;
Matthew Barth3e1bb272020-05-26 11:09:21 -050040 auto service = util::SDBusPlus::getService(bus, path, _interface);
Matthew Barth1061cba2018-05-09 14:18:16 -050041 _sensors[path] = service;
Matthew Barth2b3db612017-10-25 10:56:51 -050042 }
Matthew Barth1061cba2018-05-09 14:18:16 -050043 // All sensors associated with this fan are set to the same target speed,
Matthew Barth2b3db612017-10-25 10:56:51 -050044 // so only need to read target property from one.
45 if (!path.empty())
46 {
Matthew Barth1061cba2018-05-09 14:18:16 -050047 // Use getProperty with service lookup since each target sensor
48 // path given could have different services providing them
Matthew Barth2b3db612017-10-25 10:56:51 -050049 _targetSpeed = util::SDBusPlus::getProperty<uint64_t>(
Matthew Barth3e1bb272020-05-26 11:09:21 -050050 bus, path, _interface, FAN_TARGET_PROPERTY);
Matt Spinlere2b25cb2017-04-10 14:33:35 -050051 }
52}
53
Matt Spinlere2b25cb2017-04-10 14:33:35 -050054void Fan::setSpeed(uint64_t speed)
55{
Matt Spinlere2b25cb2017-04-10 14:33:35 -050056 for (auto& sensor : _sensors)
57 {
Matthew Barth26e96122018-05-09 14:55:41 -050058 auto value = speed;
Matthew Barth86be4762018-07-17 10:51:36 -050059 try
60 {
61 util::SDBusPlus::setProperty<uint64_t>(
Matthew Barth3e1bb272020-05-26 11:09:21 -050062 _bus, sensor.second, sensor.first, _interface,
63 FAN_TARGET_PROPERTY, std::move(value));
Matthew Barth86be4762018-07-17 10:51:36 -050064 }
Patrick Williamscb356d42022-07-22 19:26:53 -050065 catch (const sdbusplus::exception_t&)
Matthew Barth86be4762018-07-17 10:51:36 -050066 {
Matthew Barth3e1bb272020-05-26 11:09:21 -050067 throw util::DBusPropertyError{"DBus set property failed",
68 sensor.second, sensor.first,
69 _interface, FAN_TARGET_PROPERTY};
Matthew Barth86be4762018-07-17 10:51:36 -050070 }
Matt Spinlere2b25cb2017-04-10 14:33:35 -050071 }
Matthew Barth2b3db612017-10-25 10:56:51 -050072
73 _targetSpeed = speed;
Matt Spinlere2b25cb2017-04-10 14:33:35 -050074}
75
Matthew Barth3e1bb272020-05-26 11:09:21 -050076} // namespace control
77} // namespace fan
78} // namespace phosphor