blob: 008bca5aad869868c949dbcb8aa65e0cb3d0ac87 [file] [log] [blame]
Matthew Barthfcfa0522020-08-24 16:40:24 -05001/**
2 * Copyright © 2020 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
Matthew Bartha3a8cc52021-01-15 12:40:25 -060018#include "sdbusplus.hpp"
19
20#include <fmt/format.h>
21
Matthew Barthfcfa0522020-08-24 16:40:24 -050022#include <nlohmann/json.hpp>
Matthew Barthbff10802020-08-25 10:07:57 -050023#include <phosphor-logging/log.hpp>
Matthew Barthfcfa0522020-08-24 16:40:24 -050024#include <sdbusplus/bus.hpp>
25
26namespace phosphor::fan::control::json
27{
28
29using json = nlohmann::json;
Matthew Barthbff10802020-08-25 10:07:57 -050030using namespace phosphor::logging;
Matthew Barthfcfa0522020-08-24 16:40:24 -050031
Matthew Bartha3a8cc52021-01-15 12:40:25 -060032constexpr auto FAN_SENSOR_PATH = "/xyz/openbmc_project/sensors/fan_tach/";
33constexpr auto FAN_TARGET_PROPERTY = "Target";
34
35Fan::Fan(sdbusplus::bus::bus& bus, const json& jsonObj) :
36 ConfigBase(jsonObj), _bus(bus)
Matthew Barthbff10802020-08-25 10:07:57 -050037{
Matthew Barthbff10802020-08-25 10:07:57 -050038 if (jsonObj.contains("profiles"))
39 {
40 for (const auto& profile : jsonObj["profiles"])
41 {
42 _profiles.emplace_back(profile.get<std::string>());
43 }
44 }
Matthew Barthce957262020-08-27 10:35:57 -050045 setInterface(jsonObj);
Matthew Bartha3a8cc52021-01-15 12:40:25 -060046 setSensors(jsonObj);
47 setZone(jsonObj);
48}
49
50void Fan::setInterface(const json& jsonObj)
51{
52 if (!jsonObj.contains("target_interface"))
53 {
54 log<level::ERR>("Missing required fan sensor target interface",
55 entry("JSON=%s", jsonObj.dump().c_str()));
56 throw std::runtime_error(
57 "Missing required fan sensor target interface");
58 }
59 _interface = jsonObj["target_interface"].get<std::string>();
60}
61
62void Fan::setSensors(const json& jsonObj)
63{
64 if (!jsonObj.contains("sensors"))
65 {
66 log<level::ERR>("Missing required fan sensors list",
67 entry("JSON=%s", jsonObj.dump().c_str()));
68 throw std::runtime_error("Missing required fan sensors list");
69 }
70 std::string path;
71 for (const auto& sensor : jsonObj["sensors"])
72 {
73 path = FAN_SENSOR_PATH + sensor.get<std::string>();
74 auto service = util::SDBusPlus::getService(_bus, path, _interface);
75 _sensors[path] = service;
76 }
77 // All sensors associated with this fan are set to the same target speed,
78 // so only need to read target property from one of them
79 if (!path.empty())
80 {
81 _target = util::SDBusPlus::getProperty<uint64_t>(
82 _bus, _sensors.at(path), path, _interface, FAN_TARGET_PROPERTY);
83 }
Matthew Barthbff10802020-08-25 10:07:57 -050084}
85
86void Fan::setZone(const json& jsonObj)
87{
88 if (!jsonObj.contains("zone"))
89 {
90 log<level::ERR>("Missing required fan zone",
91 entry("JSON=%s", jsonObj.dump().c_str()));
92 throw std::runtime_error("Missing required fan zone");
93 }
94 _zone = jsonObj["zone"].get<std::string>();
95}
96
Matthew Bartha3a8cc52021-01-15 12:40:25 -060097void Fan::setTarget(uint64_t target)
Matthew Barthbff10802020-08-25 10:07:57 -050098{
Matthew Bartha3a8cc52021-01-15 12:40:25 -060099 for (const auto& sensor : _sensors)
Matthew Barthbff10802020-08-25 10:07:57 -0500100 {
Matthew Bartha3a8cc52021-01-15 12:40:25 -0600101 auto value = target;
102 try
103 {
104 util::SDBusPlus::setProperty<uint64_t>(
105 _bus, sensor.second, sensor.first, _interface,
106 FAN_TARGET_PROPERTY, std::move(value));
107 }
108 catch (const sdbusplus::exception::SdBusError&)
109 {
110 throw util::DBusPropertyError{
111 fmt::format("Failed to set target for fan {}", _name).c_str(),
112 sensor.second, sensor.first, _interface, FAN_TARGET_PROPERTY};
113 }
Matthew Barthbff10802020-08-25 10:07:57 -0500114 }
Matthew Bartha3a8cc52021-01-15 12:40:25 -0600115 _target = target;
Matthew Barthbff10802020-08-25 10:07:57 -0500116}
Matthew Barthfcfa0522020-08-24 16:40:24 -0500117
118} // namespace phosphor::fan::control::json