summaryrefslogtreecommitdiffstats
path: root/monitor/tach_sensor.cpp
blob: 5e985d065f4b5dc9e30d3760fa0ef2aac85629f2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
/**
 * Copyright © 2017 IBM Corporation
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#include <experimental/filesystem>
#include <phosphor-logging/log.hpp>
#include <phosphor-logging/elog.hpp>
#include "fan.hpp"
#include "sdbusplus.hpp"
#include "tach_sensor.hpp"
#include "utility.hpp"

namespace phosphor
{
namespace fan
{
namespace monitor
{

constexpr auto FAN_SENSOR_VALUE_INTF = "xyz.openbmc_project.Sensor.Value";
constexpr auto FAN_TARGET_PROPERTY = "Target";
constexpr auto FAN_VALUE_PROPERTY = "Value";

using namespace std::experimental::filesystem;
using InternalFailure = sdbusplus::xyz::openbmc_project::Common::
                            Error::InternalFailure;

/**
 * @brief Helper function to read a property
 *
 * @param[in] interface - the interface the property is on
 * @param[in] propertName - the name of the property
 * @param[in] path - the dbus path
 * @param[in] bus - the dbus object
 * @param[out] value - filled in with the property value
 */
template<typename T>
static void readProperty(const std::string& interface,
                         const std::string& propertyName,
                         const std::string& path,
                         sdbusplus::bus::bus& bus,
                         T& value)
{
    try
    {
        value = util::SDBusPlus::getProperty<T>(bus,
                                                path,
                                                interface,
                                                propertyName);
    }
    catch (std::exception& e)
    {
        phosphor::logging::log<phosphor::logging::level::ERR>(e.what());
    }
}


TachSensor::TachSensor(Mode mode,
                       sdbusplus::bus::bus& bus,
                       Fan& fan,
                       const std::string& id,
                       bool hasTarget,
                       size_t funcDelay,
                       const std::string& interface,
                       size_t factor,
                       size_t offset,
                       size_t timeout,
                       phosphor::fan::event::EventPtr& events) :
    _bus(bus),
    _fan(fan),
    _name(FAN_SENSOR_PATH + id),
    _invName(path(fan.getName()) / id),
    _hasTarget(hasTarget),
    _funcDelay(funcDelay),
    _interface(interface),
    _factor(factor),
    _offset(offset),
    _timeout(timeout),
    _timerMode(TimerMode::func),
    _timer(events, [this, &fan](){ fan.timerExpired(*this); })
{
    // Start from a known state of functional
    setFunctional(true);

    // Load in current Target and Input values when entering monitor mode
    if (mode != Mode::init)
    {
        try
        {
            // Use getProperty directly to allow a missing sensor object
            // to abort construction.
            _tachInput = util::SDBusPlus::getProperty<decltype(_tachInput)>(
                    _bus,
                    _name,
                    FAN_SENSOR_VALUE_INTF,
                    FAN_VALUE_PROPERTY);
        }
        catch (std::exception& e)
        {
            throw InvalidSensorError();
        }

        if (_hasTarget)
        {
            readProperty(_interface,
                         FAN_TARGET_PROPERTY,
                         _name,
                         _bus,
                         _tachTarget);
        }

        auto match = getMatchString(FAN_SENSOR_VALUE_INTF);

        tachSignal = std::make_unique<sdbusplus::server::match::match>(
                _bus,
                match.c_str(),
                [this](auto& msg){ this->handleTachChange(msg); });

        if (_hasTarget)
        {
            match = getMatchString(_interface);

            targetSignal = std::make_unique<sdbusplus::server::match::match>(
                    _bus,
                    match.c_str(),
                    [this](auto& msg){ this->handleTargetChange(msg); });
        }
    }
}

std::string TachSensor::getMatchString(const std::string& interface)
{
    return sdbusplus::bus::match::rules::propertiesChanged(
            _name, interface);
}

uint64_t TachSensor::getTarget() const
{
    if (!_hasTarget)
    {
        return _fan.findTargetSpeed();
    }
    return _tachTarget;
}

void TachSensor::setFunctional(bool functional)
{
    _functional = functional;
    updateInventory(_functional);
}

/**
 * @brief Reads a property from the input message and stores it in value.
 *        T is the value type.
 *
 *        Note: This can only be called once per message.
 *
 * @param[in] msg - the dbus message that contains the data
 * @param[in] interface - the interface the property is on
 * @param[in] propertName - the name of the property
 * @param[out] value - the value to store the property value in
 */
template<typename T>
static void readPropertyFromMessage(sdbusplus::message::message& msg,
                                    const std::string& interface,
                                    const std::string& propertyName,
                                    T& value)
{
    std::string sensor;
    std::map<std::string, sdbusplus::message::variant<T>> data;
    msg.read(sensor, data);

    if (sensor.compare(interface) == 0)
    {
        auto propertyMap = data.find(propertyName);
        if (propertyMap != data.end())
        {
            value = sdbusplus::message::variant_ns::get<T>(
                        propertyMap->second);
        }
    }
}


void TachSensor::handleTargetChange(sdbusplus::message::message& msg)
{
    readPropertyFromMessage(msg,
                            _interface,
                            FAN_TARGET_PROPERTY,
                            _tachTarget);

    //Check all tach sensors on the fan against the target
    _fan.tachChanged();
}


void TachSensor::handleTachChange(sdbusplus::message::message& msg)
{
   readPropertyFromMessage(msg,
                           FAN_SENSOR_VALUE_INTF,
                           FAN_VALUE_PROPERTY,
                           _tachInput);

   //Check just this sensor against the target
   _fan.tachChanged(*this);
}

void TachSensor::startTimer(TimerMode mode)
{
    if (!timerRunning())
    {
        _timer.start(
                getDelay(mode),
                util::Timer::TimerType::oneshot);
        _timerMode = mode;
    }
    else
    {
        if (mode != _timerMode)
        {
            _timer.stop();
            _timer.start(
                    getDelay(mode),
                    util::Timer::TimerType::oneshot);
            _timerMode = mode;
        }
    }
}

std::chrono::microseconds TachSensor::getDelay(TimerMode mode)
{
    using namespace std::chrono;

    switch(mode)
    {
        case TimerMode::nonfunc :
                return duration_cast<microseconds>(seconds(_timeout));
        case TimerMode::func :
                return duration_cast<microseconds>(seconds(_funcDelay));
        default :
                // Log an internal error for undefined timer mode
                log<level::ERR>("Undefined timer mode",
                        entry("TIMER_MODE=%u", mode));
                elog<InternalFailure>();
                return duration_cast<microseconds>(seconds(0));
    }
}

void TachSensor::updateInventory(bool functional)
{
    auto objectMap = util::getObjMap<bool>(
            _invName,
            util::OPERATIONAL_STATUS_INTF,
            util::FUNCTIONAL_PROPERTY,
            functional);
    auto response = util::SDBusPlus::lookupAndCallMethod(
            _bus,
            util::INVENTORY_PATH,
            util::INVENTORY_INTF,
            "Notify",
            objectMap);
    if (response.is_method_error())
    {
        log<level::ERR>("Error in notify update of tach sensor inventory");
    }
}

}
}
}
OpenPOWER on IntegriCloud