summaryrefslogtreecommitdiffstats
path: root/presence/tach.cpp
blob: a784f94d0d545a405bcd21e17e215c733c1a0d33 (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
/**
 * 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 <phosphor-logging/log.hpp>
#include <string>
#include <tuple>
#include <vector>
#include "tach.hpp"
#include "rpolicy.hpp"

namespace phosphor
{
namespace fan
{
namespace presence
{

using namespace phosphor::logging;
using namespace std::literals::string_literals;

static const auto tachNamespace = "/xyz/openbmc_project/sensors/fan_tach/"s;
static const auto tachIface = "xyz.openbmc_project.Sensor.Value"s;
static const auto tachProperty = "Value"s;

Tach::Tach(
        const std::vector<std::string>& sensors) : currentState(false)
{
    // Initialize state.
    for (const auto& s : sensors)
    {
        state.emplace_back(s, nullptr, 0);
    }
}

bool Tach::start()
{
    for (size_t i = 0; i < state.size(); ++i)
    {
        auto& s = state[i];
        auto tachPath = tachNamespace + std::get<std::string>(s);

        // Register for signal callbacks.
        std::get<1>(s) = std::make_unique<sdbusplus::bus::match::match>(
                util::SDBusPlus::getBus(),
                sdbusplus::bus::match::rules::propertiesChanged(
                    tachPath, tachIface),
                [this, i](auto& msg){ this->propertiesChanged(i, msg);});

        // Get an initial tach speed.
        try
        {
            std::get<int64_t>(s) = util::SDBusPlus::getProperty<int64_t>(
                    tachPath,
                    tachIface,
                    tachProperty);
        }
        catch (std::exception&)
        {
            // Assume not spinning.

            std::get<int64_t>(s) = 0;
            log<level::INFO>(
                    "Unable to read fan tach sensor.",
                    entry("SENSOR=%s", tachPath.c_str()));

        }
    }

    // Set the initial state of the sensor.
    currentState = std::any_of(
            state.begin(),
            state.end(),
            [](const auto & s)
            {
                return std::get<int64_t>(s) != 0;
            });

    return currentState;
}

void Tach::stop()
{
    for (auto& s : state)
    {
        // De-register signal callbacks.
        std::get<1>(s) = nullptr;
    }
}

bool Tach::present()
{
    // Live query the tach readings.
    std::vector<int64_t> values;
    for (const auto& s : state)
    {
        values.push_back(
                util::SDBusPlus::getProperty<int64_t>(
                        tachNamespace + std::get<std::string>(s),
                        tachIface,
                        tachProperty));
    }

    return std::any_of(
            values.begin(),
            values.end(),
            [](const auto & v) {return v != 0;});
}

void Tach::propertiesChanged(
        size_t sensor,
        sdbusplus::message::message& msg)
{
    std::string iface;
    util::Properties<int64_t> properties;
    msg.read(iface, properties);

    propertiesChanged(sensor, properties);
}

void Tach::propertiesChanged(
        size_t sensor,
        const util::Properties<int64_t>& props)
{
    auto& s = state[sensor];

    // Find the Value property containing the speed.
    auto it = props.find(tachProperty);
    if (it != props.end())
    {
        std::get<int64_t>(s) =
            sdbusplus::message::variant_ns::get<int64_t>(it->second);

        auto newState = std::any_of(
                state.begin(),
                state.end(),
                [](const auto & s)
                {
                    return std::get<int64_t>(s) != 0;
                });

        if (currentState != newState)
        {
            getPolicy().stateChanged(newState, *this);
            currentState = newState;
        }
    }
}

} // namespace presence
} // namespace fan
} // namespace phosphor
OpenPOWER on IntegriCloud