blob: 2aa16090a29699757e9c8d682a1185f6fff97834 [file] [log] [blame]
Matt Spinlerabf8da32017-04-27 14:08:45 -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 */
Matthew Barth4d982852017-11-17 09:37:13 -060016#include <experimental/filesystem>
Matt Spinlerabf8da32017-04-27 14:08:45 -050017#include <phosphor-logging/log.hpp>
18#include "fan.hpp"
Brad Bishop2a58e2c2017-07-30 13:49:09 -040019#include "sdbusplus.hpp"
Matt Spinlerabf8da32017-04-27 14:08:45 -050020#include "tach_sensor.hpp"
Matthew Barth4d982852017-11-17 09:37:13 -060021#include "utility.hpp"
Matt Spinlerabf8da32017-04-27 14:08:45 -050022
23namespace phosphor
24{
25namespace fan
26{
27namespace monitor
28{
29
Matt Spinlerebaae612017-04-27 14:21:48 -050030constexpr auto FAN_SENSOR_CONTROL_INTF = "xyz.openbmc_project.Control.FanSpeed";
31constexpr auto FAN_SENSOR_VALUE_INTF = "xyz.openbmc_project.Sensor.Value";
32constexpr auto FAN_TARGET_PROPERTY = "Target";
33constexpr auto FAN_VALUE_PROPERTY = "Value";
34
Matthew Barth4d982852017-11-17 09:37:13 -060035using namespace std::experimental::filesystem;
Matt Spinlerebaae612017-04-27 14:21:48 -050036
37/**
38 * @brief Helper function to read a property
39 *
40 * @param[in] interface - the interface the property is on
41 * @param[in] propertName - the name of the property
42 * @param[in] path - the dbus path
Matt Spinlerebaae612017-04-27 14:21:48 -050043 * @param[in] bus - the dbus object
44 * @param[out] value - filled in with the property value
45 */
46template<typename T>
47static void readProperty(const std::string& interface,
48 const std::string& propertyName,
49 const std::string& path,
Matt Spinlerebaae612017-04-27 14:21:48 -050050 sdbusplus::bus::bus& bus,
51 T& value)
52{
Matt Spinlerebaae612017-04-27 14:21:48 -050053 try
54 {
Brad Bishop2a58e2c2017-07-30 13:49:09 -040055 value = util::SDBusPlus::getProperty<T>(bus,
56 path,
57 interface,
58 propertyName);
Matt Spinlerebaae612017-04-27 14:21:48 -050059 }
60 catch (std::exception& e)
61 {
62 phosphor::logging::log<phosphor::logging::level::ERR>(e.what());
63 }
64}
Matt Spinlerabf8da32017-04-27 14:08:45 -050065
66
67TachSensor::TachSensor(sdbusplus::bus::bus& bus,
68 Fan& fan,
69 const std::string& id,
70 bool hasTarget,
Matt Spinlera9406a72017-04-27 14:29:24 -050071 size_t timeout,
Matt Spinlere824f982017-05-11 10:07:55 -050072 phosphor::fan::event::EventPtr& events) :
Matt Spinlerabf8da32017-04-27 14:08:45 -050073 _bus(bus),
74 _fan(fan),
75 _name(FAN_SENSOR_PATH + id),
Matthew Barth4d982852017-11-17 09:37:13 -060076 _invName(path(fan.getName()) / id),
Matt Spinlerabf8da32017-04-27 14:08:45 -050077 _hasTarget(hasTarget),
Matt Spinlera9406a72017-04-27 14:29:24 -050078 _timeout(timeout),
79 _timer(events, [this, &fan](){ fan.timerExpired(*this); })
Matt Spinlerabf8da32017-04-27 14:08:45 -050080{
Matthew Barthd199dcd2017-11-20 10:36:41 -060081 // Start from a known state of functional
Matthew Barth7f7df312018-01-15 16:27:50 -060082 setFunctional(true);
Brad Bishopedaeb312017-07-30 19:38:20 -040083
Matthew Barthd199dcd2017-11-20 10:36:41 -060084 // Load in starting Target and Input values
Brad Bishopedaeb312017-07-30 19:38:20 -040085 try
86 {
87 // Use getProperty directly to allow a missing sensor object
88 // to abort construction.
89 _tachInput = util::SDBusPlus::getProperty<decltype(_tachInput)>(
90 _bus,
91 _name,
92 FAN_SENSOR_VALUE_INTF,
93 FAN_VALUE_PROPERTY);
94 }
95 catch (std::exception& e)
96 {
97 throw InvalidSensorError();
98 }
Matt Spinlerebaae612017-04-27 14:21:48 -050099
100 if (_hasTarget)
101 {
102 readProperty(FAN_SENSOR_CONTROL_INTF,
103 FAN_TARGET_PROPERTY,
104 _name,
Matt Spinlerebaae612017-04-27 14:21:48 -0500105 _bus,
106 _tachTarget);
107 }
108
109 auto match = getMatchString(FAN_SENSOR_VALUE_INTF);
110
111 tachSignal = std::make_unique<sdbusplus::server::match::match>(
112 _bus,
113 match.c_str(),
Brad Bishop771659f2017-07-30 19:52:21 -0400114 [this](auto& msg){ this->handleTachChange(msg); });
Matt Spinlerebaae612017-04-27 14:21:48 -0500115
116 if (_hasTarget)
117 {
118 match = getMatchString(FAN_SENSOR_CONTROL_INTF);
119
120 targetSignal = std::make_unique<sdbusplus::server::match::match>(
121 _bus,
122 match.c_str(),
Brad Bishop771659f2017-07-30 19:52:21 -0400123 [this](auto& msg){ this->handleTargetChange(msg); });
Matt Spinlerebaae612017-04-27 14:21:48 -0500124 }
125
126}
127
Matt Spinlerebaae612017-04-27 14:21:48 -0500128std::string TachSensor::getMatchString(const std::string& interface)
129{
Brad Bishop78b58452017-07-30 19:44:49 -0400130 return sdbusplus::bus::match::rules::propertiesChanged(
131 _name, interface);
Matt Spinlerebaae612017-04-27 14:21:48 -0500132}
133
Matthew Barthf552ea52018-01-15 16:22:04 -0600134uint64_t TachSensor::getTarget() const
135{
136 if (!_hasTarget)
137 {
138 return _fan.findTargetSpeed();
139 }
140 return _tachTarget;
141}
142
Matthew Barthd199dcd2017-11-20 10:36:41 -0600143void TachSensor::setFunctional(bool functional)
144{
145 _functional = functional;
146 updateInventory(_functional);
147}
Matt Spinlerebaae612017-04-27 14:21:48 -0500148
Matt Spinlerebaae612017-04-27 14:21:48 -0500149/**
150 * @brief Reads a property from the input message and stores it in value.
151 * T is the value type.
152 *
153 * Note: This can only be called once per message.
154 *
155 * @param[in] msg - the dbus message that contains the data
156 * @param[in] interface - the interface the property is on
157 * @param[in] propertName - the name of the property
158 * @param[out] value - the value to store the property value in
159 */
160template<typename T>
161static void readPropertyFromMessage(sdbusplus::message::message& msg,
162 const std::string& interface,
163 const std::string& propertyName,
164 T& value)
165{
166 std::string sensor;
167 std::map<std::string, sdbusplus::message::variant<T>> data;
168 msg.read(sensor, data);
169
170 if (sensor.compare(interface) == 0)
171 {
172 auto propertyMap = data.find(propertyName);
173 if (propertyMap != data.end())
174 {
175 value = sdbusplus::message::variant_ns::get<T>(
176 propertyMap->second);
177 }
178 }
179}
180
181
Brad Bishop771659f2017-07-30 19:52:21 -0400182void TachSensor::handleTargetChange(sdbusplus::message::message& msg)
Matt Spinlerebaae612017-04-27 14:21:48 -0500183{
184 readPropertyFromMessage(msg,
185 FAN_SENSOR_CONTROL_INTF,
186 FAN_TARGET_PROPERTY,
187 _tachTarget);
188
189 //Check all tach sensors on the fan against the target
190 _fan.tachChanged();
191}
192
193
Brad Bishop771659f2017-07-30 19:52:21 -0400194void TachSensor::handleTachChange(sdbusplus::message::message& msg)
Matt Spinlerebaae612017-04-27 14:21:48 -0500195{
196 readPropertyFromMessage(msg,
197 FAN_SENSOR_VALUE_INTF,
198 FAN_VALUE_PROPERTY,
199 _tachInput);
200
201 //Check just this sensor against the target
202 _fan.tachChanged(*this);
Matt Spinlerabf8da32017-04-27 14:08:45 -0500203}
204
205
Matt Spinlera9406a72017-04-27 14:29:24 -0500206std::chrono::microseconds TachSensor::getTimeout()
207{
208 using namespace std::chrono;
209
210 return duration_cast<microseconds>(seconds(_timeout));
211}
212
Matthew Barth4d982852017-11-17 09:37:13 -0600213void TachSensor::updateInventory(bool functional)
214{
215 auto objectMap = util::getObjMap<bool>(
216 _invName,
217 util::OPERATIONAL_STATUS_INTF,
218 util::FUNCTIONAL_PROPERTY,
219 functional);
220 auto response = util::SDBusPlus::lookupAndCallMethod(
221 _bus,
222 util::INVENTORY_PATH,
223 util::INVENTORY_INTF,
224 "Notify",
225 objectMap);
226 if (response.is_method_error())
227 {
228 log<level::ERR>("Error in notify update of tach sensor inventory");
229 }
230}
Matt Spinlera9406a72017-04-27 14:29:24 -0500231
Matt Spinlerabf8da32017-04-27 14:08:45 -0500232}
233}
234}