blob: 40ec6b45c0464c570a5d67c2f0f779aafbabcd7b [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 */
16#include <phosphor-logging/log.hpp>
Dinesh Chinari618027a2017-06-26 23:26:50 -050017#include <phosphor-logging/elog.hpp>
18#include <phosphor-logging/elog-errors.hpp>
19#include <xyz/openbmc_project/Common/error.hpp>
Matt Spinlere2b25cb2017-04-10 14:33:35 -050020#include <string>
21#include "fan.hpp"
Matthew Barth2b3db612017-10-25 10:56:51 -050022#include "sdbusplus.hpp"
Matt Spinlere2b25cb2017-04-10 14:33:35 -050023
24namespace phosphor
25{
26namespace fan
27{
28namespace control
29{
30
Dinesh Chinari618027a2017-06-26 23:26:50 -050031// For throwing exception
32using namespace phosphor::logging;
33using InternalFailure = sdbusplus::xyz::openbmc_project::Common::
34 Error::InternalFailure;
35
Matt Spinlere2b25cb2017-04-10 14:33:35 -050036constexpr auto PROPERTY_INTERFACE = "org.freedesktop.DBus.Properties";
37constexpr auto FAN_SENSOR_PATH = "/xyz/openbmc_project/sensors/fan_tach/";
Matt Spinlere2b25cb2017-04-10 14:33:35 -050038constexpr auto FAN_TARGET_PROPERTY = "Target";
39
40
41Fan::Fan(sdbusplus::bus::bus& bus, const FanDefinition& def):
42 _bus(bus),
Lei YU069e4402018-01-31 16:47:37 +080043 _name(std::get<fanNamePos>(def)),
44 _interface(std::get<targetInterfacePos>(def))
Matt Spinlere2b25cb2017-04-10 14:33:35 -050045{
Matthew Barth2b3db612017-10-25 10:56:51 -050046 std::string path;
Matt Spinlere2b25cb2017-04-10 14:33:35 -050047 auto sensors = std::get<sensorListPos>(def);
48 for (auto& s : sensors)
49 {
Matthew Barth2b3db612017-10-25 10:56:51 -050050 path = FAN_SENSOR_PATH + s;
Matthew Barth1061cba2018-05-09 14:18:16 -050051 auto service = util::SDBusPlus::getService(
52 bus,
53 path,
54 _interface);
55 _sensors[path] = service;
Matthew Barth2b3db612017-10-25 10:56:51 -050056 }
Matthew Barth1061cba2018-05-09 14:18:16 -050057 // All sensors associated with this fan are set to the same target speed,
Matthew Barth2b3db612017-10-25 10:56:51 -050058 // so only need to read target property from one.
59 if (!path.empty())
60 {
Matthew Barth1061cba2018-05-09 14:18:16 -050061 // Use getProperty with service lookup since each target sensor
62 // path given could have different services providing them
Matthew Barth2b3db612017-10-25 10:56:51 -050063 _targetSpeed = util::SDBusPlus::getProperty<uint64_t>(
64 bus,
65 path,
Lei YU069e4402018-01-31 16:47:37 +080066 _interface,
Matthew Barth2b3db612017-10-25 10:56:51 -050067 FAN_TARGET_PROPERTY);
Matt Spinlere2b25cb2017-04-10 14:33:35 -050068 }
69}
70
Matt Spinlere2b25cb2017-04-10 14:33:35 -050071void Fan::setSpeed(uint64_t speed)
72{
73 sdbusplus::message::variant<uint64_t> value = speed;
74 std::string property{FAN_TARGET_PROPERTY};
75
76 for (auto& sensor : _sensors)
77 {
Matthew Barth1061cba2018-05-09 14:18:16 -050078 auto method = _bus.new_method_call(sensor.second.c_str(),
79 sensor.first.c_str(),
Matthew Barth20819722017-11-08 10:00:35 -060080 PROPERTY_INTERFACE,
81 "Set");
Lei YU069e4402018-01-31 16:47:37 +080082 method.append(_interface, property, value);
Matt Spinlere2b25cb2017-04-10 14:33:35 -050083
Dinesh Chinari618027a2017-06-26 23:26:50 -050084 auto response = _bus.call(method);
85 if (response.is_method_error())
Matt Spinlere2b25cb2017-04-10 14:33:35 -050086 {
Dinesh Chinari618027a2017-06-26 23:26:50 -050087 log<level::ERR>(
Matthew Barth1061cba2018-05-09 14:18:16 -050088 "Failed call to set fan speed ",
89 entry("SENSOR=%s", sensor.first));
Dinesh Chinari618027a2017-06-26 23:26:50 -050090 elog<InternalFailure>();
Matt Spinlere2b25cb2017-04-10 14:33:35 -050091 }
92 }
Matthew Barth2b3db612017-10-25 10:56:51 -050093
94 _targetSpeed = speed;
Matt Spinlere2b25cb2017-04-10 14:33:35 -050095}
96
97}
98}
99}