xref: /openbmc/phosphor-fan-presence/control/fan.cpp (revision 44872b07392236c5aed8667c00bebbc2ea099a89)
1e2b25cb2SMatt Spinler /**
2e2b25cb2SMatt Spinler  * Copyright © 2017 IBM Corporation
3e2b25cb2SMatt Spinler  *
4e2b25cb2SMatt Spinler  * Licensed under the Apache License, Version 2.0 (the "License");
5e2b25cb2SMatt Spinler  * you may not use this file except in compliance with the License.
6e2b25cb2SMatt Spinler  * You may obtain a copy of the License at
7e2b25cb2SMatt Spinler  *
8e2b25cb2SMatt Spinler  *     http://www.apache.org/licenses/LICENSE-2.0
9e2b25cb2SMatt Spinler  *
10e2b25cb2SMatt Spinler  * Unless required by applicable law or agreed to in writing, software
11e2b25cb2SMatt Spinler  * distributed under the License is distributed on an "AS IS" BASIS,
12e2b25cb2SMatt Spinler  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13e2b25cb2SMatt Spinler  * See the License for the specific language governing permissions and
14e2b25cb2SMatt Spinler  * limitations under the License.
15e2b25cb2SMatt Spinler  */
16e2b25cb2SMatt Spinler #include "fan.hpp"
173e1bb274SMatthew Barth 
182b3db618SMatthew Barth #include "sdbusplus.hpp"
19e2b25cb2SMatt Spinler 
203e1bb274SMatthew Barth #include <string>
213e1bb274SMatthew Barth 
22e2b25cb2SMatt Spinler namespace phosphor
23e2b25cb2SMatt Spinler {
24e2b25cb2SMatt Spinler namespace fan
25e2b25cb2SMatt Spinler {
26e2b25cb2SMatt Spinler namespace control
27e2b25cb2SMatt Spinler {
28e2b25cb2SMatt Spinler 
29618027abSDinesh Chinari // For throwing exception
30618027abSDinesh Chinari using namespace phosphor::logging;
31618027abSDinesh Chinari 
32e2b25cb2SMatt Spinler constexpr auto FAN_SENSOR_PATH = "/xyz/openbmc_project/sensors/fan_tach/";
33e2b25cb2SMatt Spinler constexpr auto FAN_TARGET_PROPERTY = "Target";
34e2b25cb2SMatt Spinler 
Fan(sdbusplus::bus_t & bus,const FanDefinition & def)35cb356d48SPatrick Williams Fan::Fan(sdbusplus::bus_t& bus, const FanDefinition& def) :
363e1bb274SMatthew Barth     _bus(bus), _name(std::get<fanNamePos>(def)),
37069e4405SLei YU     _interface(std::get<targetInterfacePos>(def))
38e2b25cb2SMatt Spinler {
392b3db618SMatthew Barth     std::string path;
40e2b25cb2SMatt Spinler     auto sensors = std::get<sensorListPos>(def);
41e2b25cb2SMatt Spinler     for (auto& s : sensors)
42e2b25cb2SMatt Spinler     {
43*44872b07SChau Ly         path = std::get<targetControlPathPos>(def) + s;
443e1bb274SMatthew Barth         auto service = util::SDBusPlus::getService(bus, path, _interface);
451061cba1SMatthew Barth         _sensors[path] = service;
462b3db618SMatthew Barth     }
471061cba1SMatthew Barth     // All sensors associated with this fan are set to the same target speed,
482b3db618SMatthew Barth     // so only need to read target property from one.
492b3db618SMatthew Barth     if (!path.empty())
502b3db618SMatthew Barth     {
511061cba1SMatthew Barth         // Use getProperty with service lookup since each target sensor
521061cba1SMatthew Barth         // path given could have different services providing them
532b3db618SMatthew Barth         _targetSpeed = util::SDBusPlus::getProperty<uint64_t>(
543e1bb274SMatthew Barth             bus, path, _interface, FAN_TARGET_PROPERTY);
55e2b25cb2SMatt Spinler     }
56e2b25cb2SMatt Spinler }
57e2b25cb2SMatt Spinler 
setSpeed(uint64_t speed)58e2b25cb2SMatt Spinler void Fan::setSpeed(uint64_t speed)
59e2b25cb2SMatt Spinler {
60e2b25cb2SMatt Spinler     for (auto& sensor : _sensors)
61e2b25cb2SMatt Spinler     {
6226e96122SMatthew Barth         auto value = speed;
6386be476bSMatthew Barth         try
6486be476bSMatthew Barth         {
6526e96122SMatthew Barth             util::SDBusPlus::setProperty<uint64_t>(
663e1bb274SMatthew Barth                 _bus, sensor.second, sensor.first, _interface,
673e1bb274SMatthew Barth                 FAN_TARGET_PROPERTY, std::move(value));
68e2b25cb2SMatt Spinler         }
69cb356d48SPatrick Williams         catch (const sdbusplus::exception_t&)
7086be476bSMatthew Barth         {
713e1bb274SMatthew Barth             throw util::DBusPropertyError{"DBus set property failed",
723e1bb274SMatthew Barth                                           sensor.second, sensor.first,
733e1bb274SMatthew Barth                                           _interface, FAN_TARGET_PROPERTY};
7486be476bSMatthew Barth         }
7586be476bSMatthew Barth     }
762b3db618SMatthew Barth 
772b3db618SMatthew Barth     _targetSpeed = speed;
78e2b25cb2SMatt Spinler }
79e2b25cb2SMatt Spinler 
803e1bb274SMatthew Barth } // namespace control
813e1bb274SMatthew Barth } // namespace fan
823e1bb274SMatthew Barth } // namespace phosphor
83