xref: /openbmc/phosphor-fan-presence/control/fan.cpp (revision 3e781064f534ae0c822ac4c4f5590635633aa394)
1 /**
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 <string>
17 #include "fan.hpp"
18 #include "sdbusplus.hpp"
19 
20 namespace phosphor
21 {
22 namespace fan
23 {
24 namespace control
25 {
26 
27 // For throwing exception
28 using namespace phosphor::logging;
29 
30 constexpr auto FAN_SENSOR_PATH = "/xyz/openbmc_project/sensors/fan_tach/";
31 constexpr auto FAN_TARGET_PROPERTY = "Target";
32 
33 Fan::Fan(sdbusplus::bus::bus& bus, const FanDefinition& def):
34     _bus(bus),
35     _name(std::get<fanNamePos>(def)),
36     _interface(std::get<targetInterfacePos>(def))
37 {
38     std::string path;
39     auto sensors = std::get<sensorListPos>(def);
40     for (auto& s : sensors)
41     {
42         path = FAN_SENSOR_PATH + s;
43         auto service = util::SDBusPlus::getService(
44                 bus,
45                 path,
46                 _interface);
47         _sensors[path] = service;
48     }
49     // All sensors associated with this fan are set to the same target speed,
50     // so only need to read target property from one.
51     if (!path.empty())
52     {
53         // Use getProperty with service lookup since each target sensor
54         // path given could have different services providing them
55         _targetSpeed = util::SDBusPlus::getProperty<uint64_t>(
56                 bus,
57                 path,
58                 _interface,
59                 FAN_TARGET_PROPERTY);
60     }
61 }
62 
63 void Fan::setSpeed(uint64_t speed)
64 {
65     for (auto& sensor : _sensors)
66     {
67         auto value = speed;
68         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         }
87     }
88 
89     _targetSpeed = speed;
90 }
91 
92 }
93 }
94 }
95