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 "fan.hpp"
17
18 #include "sdbusplus.hpp"
19
20 #include <string>
21
22 namespace phosphor
23 {
24 namespace fan
25 {
26 namespace control
27 {
28
29 // For throwing exception
30 using namespace phosphor::logging;
31
32 constexpr auto FAN_SENSOR_PATH = "/xyz/openbmc_project/sensors/fan_tach/";
33 constexpr auto FAN_TARGET_PROPERTY = "Target";
34
Fan(sdbusplus::bus_t & bus,const FanDefinition & def)35 Fan::Fan(sdbusplus::bus_t& bus, const FanDefinition& def) :
36 _bus(bus), _name(std::get<fanNamePos>(def)),
37 _interface(std::get<targetInterfacePos>(def))
38 {
39 std::string path;
40 auto sensors = std::get<sensorListPos>(def);
41 for (auto& s : sensors)
42 {
43 path = std::get<targetControlPathPos>(def) + s;
44 auto service = util::SDBusPlus::getService(bus, path, _interface);
45 _sensors[path] = service;
46 }
47 // All sensors associated with this fan are set to the same target speed,
48 // so only need to read target property from one.
49 if (!path.empty())
50 {
51 // Use getProperty with service lookup since each target sensor
52 // path given could have different services providing them
53 _targetSpeed = util::SDBusPlus::getProperty<uint64_t>(
54 bus, path, _interface, FAN_TARGET_PROPERTY);
55 }
56 }
57
setSpeed(uint64_t speed)58 void Fan::setSpeed(uint64_t speed)
59 {
60 for (auto& sensor : _sensors)
61 {
62 auto value = speed;
63 try
64 {
65 util::SDBusPlus::setProperty<uint64_t>(
66 _bus, sensor.second, sensor.first, _interface,
67 FAN_TARGET_PROPERTY, std::move(value));
68 }
69 catch (const sdbusplus::exception_t&)
70 {
71 throw util::DBusPropertyError{"DBus set property failed",
72 sensor.second, sensor.first,
73 _interface, FAN_TARGET_PROPERTY};
74 }
75 }
76
77 _targetSpeed = speed;
78 }
79
80 } // namespace control
81 } // namespace fan
82 } // namespace phosphor
83