blob: 98d49798ee44239c75d188034917904a26e092d8 [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
Dinesh Chinari618027a2017-06-26 23:26:50 -050029// For throwing exception
30using namespace phosphor::logging;
Dinesh Chinari618027a2017-06-26 23:26:50 -050031
Matt Spinlere2b25cb2017-04-10 14:33:35 -050032constexpr auto FAN_SENSOR_PATH = "/xyz/openbmc_project/sensors/fan_tach/";
Matt Spinlere2b25cb2017-04-10 14:33:35 -050033constexpr auto FAN_TARGET_PROPERTY = "Target";
34
Patrick Williamscb356d42022-07-22 19:26:53 -050035Fan::Fan(sdbusplus::bus_t& bus, const FanDefinition& def) :
Matthew Barth3e1bb272020-05-26 11:09:21 -050036 _bus(bus), _name(std::get<fanNamePos>(def)),
Lei YU069e4402018-01-31 16:47:37 +080037 _interface(std::get<targetInterfacePos>(def))
Matt Spinlere2b25cb2017-04-10 14:33:35 -050038{
Matthew Barth2b3db612017-10-25 10:56:51 -050039 std::string path;
Matt Spinlere2b25cb2017-04-10 14:33:35 -050040 auto sensors = std::get<sensorListPos>(def);
41 for (auto& s : sensors)
42 {
Chau Ly44872b02022-09-19 07:34:08 +000043 path = std::get<targetControlPathPos>(def) + s;
Matthew Barth3e1bb272020-05-26 11:09:21 -050044 auto service = util::SDBusPlus::getService(bus, path, _interface);
Matthew Barth1061cba2018-05-09 14:18:16 -050045 _sensors[path] = service;
Matthew Barth2b3db612017-10-25 10:56:51 -050046 }
Matthew Barth1061cba2018-05-09 14:18:16 -050047 // All sensors associated with this fan are set to the same target speed,
Matthew Barth2b3db612017-10-25 10:56:51 -050048 // so only need to read target property from one.
49 if (!path.empty())
50 {
Matthew Barth1061cba2018-05-09 14:18:16 -050051 // Use getProperty with service lookup since each target sensor
52 // path given could have different services providing them
Matthew Barth2b3db612017-10-25 10:56:51 -050053 _targetSpeed = util::SDBusPlus::getProperty<uint64_t>(
Matthew Barth3e1bb272020-05-26 11:09:21 -050054 bus, path, _interface, FAN_TARGET_PROPERTY);
Matt Spinlere2b25cb2017-04-10 14:33:35 -050055 }
56}
57
Matt Spinlere2b25cb2017-04-10 14:33:35 -050058void Fan::setSpeed(uint64_t speed)
59{
Matt Spinlere2b25cb2017-04-10 14:33:35 -050060 for (auto& sensor : _sensors)
61 {
Matthew Barth26e96122018-05-09 14:55:41 -050062 auto value = speed;
Matthew Barth86be4762018-07-17 10:51:36 -050063 try
64 {
65 util::SDBusPlus::setProperty<uint64_t>(
Matthew Barth3e1bb272020-05-26 11:09:21 -050066 _bus, sensor.second, sensor.first, _interface,
67 FAN_TARGET_PROPERTY, std::move(value));
Matthew Barth86be4762018-07-17 10:51:36 -050068 }
Patrick Williamscb356d42022-07-22 19:26:53 -050069 catch (const sdbusplus::exception_t&)
Matthew Barth86be4762018-07-17 10:51:36 -050070 {
Matthew Barth3e1bb272020-05-26 11:09:21 -050071 throw util::DBusPropertyError{"DBus set property failed",
72 sensor.second, sensor.first,
73 _interface, FAN_TARGET_PROPERTY};
Matthew Barth86be4762018-07-17 10:51:36 -050074 }
Matt Spinlere2b25cb2017-04-10 14:33:35 -050075 }
Matthew Barth2b3db612017-10-25 10:56:51 -050076
77 _targetSpeed = speed;
Matt Spinlere2b25cb2017-04-10 14:33:35 -050078}
79
Matthew Barth3e1bb272020-05-26 11:09:21 -050080} // namespace control
81} // namespace fan
82} // namespace phosphor