blob: 5e985d065f4b5dc9e30d3760fa0ef2aac85629f2 [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>
Matthew Barth3800ae72018-02-19 16:08:04 -060018#include <phosphor-logging/elog.hpp>
Matt Spinlerabf8da32017-04-27 14:08:45 -050019#include "fan.hpp"
Brad Bishop2a58e2c2017-07-30 13:49:09 -040020#include "sdbusplus.hpp"
Matt Spinlerabf8da32017-04-27 14:08:45 -050021#include "tach_sensor.hpp"
Matthew Barth4d982852017-11-17 09:37:13 -060022#include "utility.hpp"
Matt Spinlerabf8da32017-04-27 14:08:45 -050023
24namespace phosphor
25{
26namespace fan
27{
28namespace monitor
29{
30
Matt Spinlerebaae612017-04-27 14:21:48 -050031constexpr 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;
Matthew Barth3800ae72018-02-19 16:08:04 -060036using InternalFailure = sdbusplus::xyz::openbmc_project::Common::
37 Error::InternalFailure;
Matt Spinlerebaae612017-04-27 14:21:48 -050038
39/**
40 * @brief Helper function to read a property
41 *
42 * @param[in] interface - the interface the property is on
43 * @param[in] propertName - the name of the property
44 * @param[in] path - the dbus path
Matt Spinlerebaae612017-04-27 14:21:48 -050045 * @param[in] bus - the dbus object
46 * @param[out] value - filled in with the property value
47 */
48template<typename T>
49static void readProperty(const std::string& interface,
50 const std::string& propertyName,
51 const std::string& path,
Matt Spinlerebaae612017-04-27 14:21:48 -050052 sdbusplus::bus::bus& bus,
53 T& value)
54{
Matt Spinlerebaae612017-04-27 14:21:48 -050055 try
56 {
Brad Bishop2a58e2c2017-07-30 13:49:09 -040057 value = util::SDBusPlus::getProperty<T>(bus,
58 path,
59 interface,
60 propertyName);
Matt Spinlerebaae612017-04-27 14:21:48 -050061 }
62 catch (std::exception& e)
63 {
64 phosphor::logging::log<phosphor::logging::level::ERR>(e.what());
65 }
66}
Matt Spinlerabf8da32017-04-27 14:08:45 -050067
68
Matthew Barth0a9fe162018-01-26 12:53:15 -060069TachSensor::TachSensor(Mode mode,
70 sdbusplus::bus::bus& bus,
Matt Spinlerabf8da32017-04-27 14:08:45 -050071 Fan& fan,
72 const std::string& id,
73 bool hasTarget,
Matthew Barth9396bcc2018-02-19 14:13:20 -060074 size_t funcDelay,
Lei YU80f271b2018-01-31 15:24:46 +080075 const std::string& interface,
Lei YU8e5d1972018-01-26 17:14:00 +080076 size_t factor,
77 size_t offset,
Matt Spinlera9406a72017-04-27 14:29:24 -050078 size_t timeout,
Matt Spinlere824f982017-05-11 10:07:55 -050079 phosphor::fan::event::EventPtr& events) :
Matt Spinlerabf8da32017-04-27 14:08:45 -050080 _bus(bus),
81 _fan(fan),
82 _name(FAN_SENSOR_PATH + id),
Matthew Barth4d982852017-11-17 09:37:13 -060083 _invName(path(fan.getName()) / id),
Matt Spinlerabf8da32017-04-27 14:08:45 -050084 _hasTarget(hasTarget),
Matthew Barth9396bcc2018-02-19 14:13:20 -060085 _funcDelay(funcDelay),
Lei YU80f271b2018-01-31 15:24:46 +080086 _interface(interface),
Lei YU8e5d1972018-01-26 17:14:00 +080087 _factor(factor),
88 _offset(offset),
Matt Spinlera9406a72017-04-27 14:29:24 -050089 _timeout(timeout),
Matthew Barth3800ae72018-02-19 16:08:04 -060090 _timerMode(TimerMode::func),
Matt Spinlera9406a72017-04-27 14:29:24 -050091 _timer(events, [this, &fan](){ fan.timerExpired(*this); })
Matt Spinlerabf8da32017-04-27 14:08:45 -050092{
Matthew Barthd199dcd2017-11-20 10:36:41 -060093 // Start from a known state of functional
Matthew Barth7f7df312018-01-15 16:27:50 -060094 setFunctional(true);
Brad Bishopedaeb312017-07-30 19:38:20 -040095
Matthew Barth0a9fe162018-01-26 12:53:15 -060096 // Load in current Target and Input values when entering monitor mode
97 if (mode != Mode::init)
Brad Bishopedaeb312017-07-30 19:38:20 -040098 {
Matthew Barth0a9fe162018-01-26 12:53:15 -060099 try
100 {
101 // Use getProperty directly to allow a missing sensor object
102 // to abort construction.
103 _tachInput = util::SDBusPlus::getProperty<decltype(_tachInput)>(
104 _bus,
105 _name,
106 FAN_SENSOR_VALUE_INTF,
107 FAN_VALUE_PROPERTY);
108 }
109 catch (std::exception& e)
110 {
111 throw InvalidSensorError();
112 }
113
114 if (_hasTarget)
115 {
Lei YU80f271b2018-01-31 15:24:46 +0800116 readProperty(_interface,
Matthew Barth0a9fe162018-01-26 12:53:15 -0600117 FAN_TARGET_PROPERTY,
118 _name,
119 _bus,
120 _tachTarget);
121 }
122
123 auto match = getMatchString(FAN_SENSOR_VALUE_INTF);
124
125 tachSignal = std::make_unique<sdbusplus::server::match::match>(
Brad Bishopedaeb312017-07-30 19:38:20 -0400126 _bus,
Matthew Barth0a9fe162018-01-26 12:53:15 -0600127 match.c_str(),
128 [this](auto& msg){ this->handleTachChange(msg); });
129
130 if (_hasTarget)
131 {
Lei YU80f271b2018-01-31 15:24:46 +0800132 match = getMatchString(_interface);
Matthew Barth0a9fe162018-01-26 12:53:15 -0600133
134 targetSignal = std::make_unique<sdbusplus::server::match::match>(
135 _bus,
136 match.c_str(),
137 [this](auto& msg){ this->handleTargetChange(msg); });
138 }
Brad Bishopedaeb312017-07-30 19:38:20 -0400139 }
Matt Spinlerebaae612017-04-27 14:21:48 -0500140}
141
Matt Spinlerebaae612017-04-27 14:21:48 -0500142std::string TachSensor::getMatchString(const std::string& interface)
143{
Brad Bishop78b58452017-07-30 19:44:49 -0400144 return sdbusplus::bus::match::rules::propertiesChanged(
145 _name, interface);
Matt Spinlerebaae612017-04-27 14:21:48 -0500146}
147
Matthew Barthf552ea52018-01-15 16:22:04 -0600148uint64_t TachSensor::getTarget() const
149{
150 if (!_hasTarget)
151 {
152 return _fan.findTargetSpeed();
153 }
154 return _tachTarget;
155}
156
Matthew Barthd199dcd2017-11-20 10:36:41 -0600157void TachSensor::setFunctional(bool functional)
158{
159 _functional = functional;
160 updateInventory(_functional);
161}
Matt Spinlerebaae612017-04-27 14:21:48 -0500162
Matt Spinlerebaae612017-04-27 14:21:48 -0500163/**
164 * @brief Reads a property from the input message and stores it in value.
165 * T is the value type.
166 *
167 * Note: This can only be called once per message.
168 *
169 * @param[in] msg - the dbus message that contains the data
170 * @param[in] interface - the interface the property is on
171 * @param[in] propertName - the name of the property
172 * @param[out] value - the value to store the property value in
173 */
174template<typename T>
175static void readPropertyFromMessage(sdbusplus::message::message& msg,
176 const std::string& interface,
177 const std::string& propertyName,
178 T& value)
179{
180 std::string sensor;
181 std::map<std::string, sdbusplus::message::variant<T>> data;
182 msg.read(sensor, data);
183
184 if (sensor.compare(interface) == 0)
185 {
186 auto propertyMap = data.find(propertyName);
187 if (propertyMap != data.end())
188 {
189 value = sdbusplus::message::variant_ns::get<T>(
190 propertyMap->second);
191 }
192 }
193}
194
195
Brad Bishop771659f2017-07-30 19:52:21 -0400196void TachSensor::handleTargetChange(sdbusplus::message::message& msg)
Matt Spinlerebaae612017-04-27 14:21:48 -0500197{
198 readPropertyFromMessage(msg,
Lei YU80f271b2018-01-31 15:24:46 +0800199 _interface,
Matt Spinlerebaae612017-04-27 14:21:48 -0500200 FAN_TARGET_PROPERTY,
201 _tachTarget);
202
203 //Check all tach sensors on the fan against the target
204 _fan.tachChanged();
205}
206
207
Brad Bishop771659f2017-07-30 19:52:21 -0400208void TachSensor::handleTachChange(sdbusplus::message::message& msg)
Matt Spinlerebaae612017-04-27 14:21:48 -0500209{
210 readPropertyFromMessage(msg,
211 FAN_SENSOR_VALUE_INTF,
212 FAN_VALUE_PROPERTY,
213 _tachInput);
214
215 //Check just this sensor against the target
216 _fan.tachChanged(*this);
Matt Spinlerabf8da32017-04-27 14:08:45 -0500217}
218
Matthew Barth3800ae72018-02-19 16:08:04 -0600219void TachSensor::startTimer(TimerMode mode)
220{
221 if (!timerRunning())
222 {
223 _timer.start(
224 getDelay(mode),
225 util::Timer::TimerType::oneshot);
226 _timerMode = mode;
227 }
228 else
229 {
230 if (mode != _timerMode)
231 {
232 _timer.stop();
233 _timer.start(
234 getDelay(mode),
235 util::Timer::TimerType::oneshot);
236 _timerMode = mode;
237 }
238 }
239}
Matt Spinlerabf8da32017-04-27 14:08:45 -0500240
Matthew Barth3800ae72018-02-19 16:08:04 -0600241std::chrono::microseconds TachSensor::getDelay(TimerMode mode)
Matt Spinlera9406a72017-04-27 14:29:24 -0500242{
243 using namespace std::chrono;
244
Matthew Barth3800ae72018-02-19 16:08:04 -0600245 switch(mode)
246 {
247 case TimerMode::nonfunc :
248 return duration_cast<microseconds>(seconds(_timeout));
249 case TimerMode::func :
250 return duration_cast<microseconds>(seconds(_funcDelay));
251 default :
252 // Log an internal error for undefined timer mode
253 log<level::ERR>("Undefined timer mode",
254 entry("TIMER_MODE=%u", mode));
255 elog<InternalFailure>();
256 return duration_cast<microseconds>(seconds(0));
257 }
Matt Spinlera9406a72017-04-27 14:29:24 -0500258}
259
Matthew Barth4d982852017-11-17 09:37:13 -0600260void TachSensor::updateInventory(bool functional)
261{
262 auto objectMap = util::getObjMap<bool>(
263 _invName,
264 util::OPERATIONAL_STATUS_INTF,
265 util::FUNCTIONAL_PROPERTY,
266 functional);
267 auto response = util::SDBusPlus::lookupAndCallMethod(
268 _bus,
269 util::INVENTORY_PATH,
270 util::INVENTORY_INTF,
271 "Notify",
272 objectMap);
273 if (response.is_method_error())
274 {
275 log<level::ERR>("Error in notify update of tach sensor inventory");
276 }
277}
Matt Spinlera9406a72017-04-27 14:29:24 -0500278
Matt Spinlerabf8da32017-04-27 14:08:45 -0500279}
280}
281}