blob: c86186f52d67d2bfa3fc326fceee1936ccbdde34 [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 <string>
17#include "fan.hpp"
Matthew Barth2b3db612017-10-25 10:56:51 -050018#include "sdbusplus.hpp"
Matt Spinlere2b25cb2017-04-10 14:33:35 -050019
20namespace phosphor
21{
22namespace fan
23{
24namespace control
25{
26
Dinesh Chinari618027a2017-06-26 23:26:50 -050027// For throwing exception
28using namespace phosphor::logging;
Dinesh Chinari618027a2017-06-26 23:26:50 -050029
Matt Spinlere2b25cb2017-04-10 14:33:35 -050030constexpr auto FAN_SENSOR_PATH = "/xyz/openbmc_project/sensors/fan_tach/";
Matt Spinlere2b25cb2017-04-10 14:33:35 -050031constexpr auto FAN_TARGET_PROPERTY = "Target";
32
Matt Spinlere2b25cb2017-04-10 14:33:35 -050033Fan::Fan(sdbusplus::bus::bus& bus, const FanDefinition& def):
34 _bus(bus),
Lei YU069e4402018-01-31 16:47:37 +080035 _name(std::get<fanNamePos>(def)),
36 _interface(std::get<targetInterfacePos>(def))
Matt Spinlere2b25cb2017-04-10 14:33:35 -050037{
Matthew Barth2b3db612017-10-25 10:56:51 -050038 std::string path;
Matt Spinlere2b25cb2017-04-10 14:33:35 -050039 auto sensors = std::get<sensorListPos>(def);
40 for (auto& s : sensors)
41 {
Matthew Barth2b3db612017-10-25 10:56:51 -050042 path = FAN_SENSOR_PATH + s;
Matthew Barth1061cba2018-05-09 14:18:16 -050043 auto service = util::SDBusPlus::getService(
44 bus,
45 path,
46 _interface);
47 _sensors[path] = service;
Matthew Barth2b3db612017-10-25 10:56:51 -050048 }
Matthew Barth1061cba2018-05-09 14:18:16 -050049 // All sensors associated with this fan are set to the same target speed,
Matthew Barth2b3db612017-10-25 10:56:51 -050050 // so only need to read target property from one.
51 if (!path.empty())
52 {
Matthew Barth1061cba2018-05-09 14:18:16 -050053 // Use getProperty with service lookup since each target sensor
54 // path given could have different services providing them
Matthew Barth2b3db612017-10-25 10:56:51 -050055 _targetSpeed = util::SDBusPlus::getProperty<uint64_t>(
56 bus,
57 path,
Lei YU069e4402018-01-31 16:47:37 +080058 _interface,
Matthew Barth2b3db612017-10-25 10:56:51 -050059 FAN_TARGET_PROPERTY);
Matt Spinlere2b25cb2017-04-10 14:33:35 -050060 }
61}
62
Matt Spinlere2b25cb2017-04-10 14:33:35 -050063void Fan::setSpeed(uint64_t speed)
64{
Matt Spinlere2b25cb2017-04-10 14:33:35 -050065 for (auto& sensor : _sensors)
66 {
Matthew Barth26e96122018-05-09 14:55:41 -050067 auto value = speed;
Matthew Barth86be4762018-07-17 10:51:36 -050068 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 Spinlere2b25cb2017-04-10 14:33:35 -050087 }
Matthew Barth2b3db612017-10-25 10:56:51 -050088
89 _targetSpeed = speed;
Matt Spinlere2b25cb2017-04-10 14:33:35 -050090}
91
92}
93}
94}