1 /**
2  * Copyright © 2021 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 #pragma once
17 
18 #include "power_state.hpp"
19 
20 #include <sdbusplus/bus.hpp>
21 #include <sdbusplus/bus/match.hpp>
22 #include <sdeventplus/event.hpp>
23 
24 namespace sensor::monitor
25 {
26 
27 using InterfaceName = std::string;
28 using PropertyName = std::string;
29 using ErrorName = std::string;
30 using ObjectPath = std::string;
31 using InterfaceKey = std::tuple<ObjectPath, InterfaceName>;
32 
33 /**
34  * @class ThresholdAlarmLogger
35  *
36  * This class watches the threshold interfaces
37  *    openbmc_project.Sensor.Threshold.Warning
38  *    openbmc_project.Sensor.Threshold.Critical
39  *    openbmc_project.Sensor.Threshold.PerformanceLoss
40  *
41  * and creates event logs when their high and low alarm
42  * properties set and clear.  The error names of the
43  * event logs are based on the sensor type and look like:
44  *
45  * xyz.openbmc_project.Sensor.Threshold.Error.TemperatureWarningHigh
46  * xyz.openbmc_project.Sensor.Threshold.Error.TemperatureWarningHighClear
47  *
48  * Event logs are only created when the power is on.
49  */
50 class ThresholdAlarmLogger
51 {
52   public:
53     ThresholdAlarmLogger() = delete;
54     ~ThresholdAlarmLogger() = default;
55     ThresholdAlarmLogger(const ThresholdAlarmLogger&) = default;
56     ThresholdAlarmLogger& operator=(const ThresholdAlarmLogger&) = default;
57     ThresholdAlarmLogger(ThresholdAlarmLogger&&) = default;
58     ThresholdAlarmLogger& operator=(ThresholdAlarmLogger&&) = default;
59 
60     /**
61      * @brief Constructor
62      *
63      * Looks for existing active threshold alarms.
64      *
65      * @param[in] bus - The sdbusplus bus object
66      * @param[in] event - The sdeventplus event object
67      * @param[in] powerState - The PowerState object
68      */
69     ThresholdAlarmLogger(sdbusplus::bus::bus& bus, sdeventplus::Event& event,
70                          std::shared_ptr<phosphor::fan::PowerState> powerState);
71 
72   private:
73     /**
74      * @brief The propertiesChanged handler for all of the thresholds
75      *        interfaces.
76      *
77      * Creates event logs for high/low alarm sets and clears.
78      *
79      * @param[in] msg - The signal message payload.
80      */
81     void propertiesChanged(sdbusplus::message::message& msg);
82 
83     /**
84      * @brief Checks for active alarms on the path and threshold interface
85      *        passed in and creates event logs if necessary.
86      *
87      * @param[in] interface - The threshold interface
88      * @param[in] sensorPath - The sensor D-Bus path
89      * @param[in] service - The D-Bus service that owns the interface
90      */
91     void checkThresholds(const std::string& interface,
92                          const std::string& sensorPath,
93                          const std::string& service);
94 
95     /**
96      * @brief Checks for all active alarms on all existing
97      *        threshold interfaces and creates event logs
98      *        if necessary.
99      */
100     void checkThresholds();
101 
102     /**
103      * @brief Creates an event log for the alarm set/clear
104      *
105      * @param[in] sensorPath - The sensor object path
106      * @param[in] interface - The threshold interface
107      * @param[in] alarmProperty - The alarm property name
108      * @param[in] alarmValue - The alarm value
109      */
110     void createEventLog(const std::string& sensorPath,
111                         const std::string& interface,
112                         const std::string& alarmProperty, bool alarmValue);
113 
114     /**
115      * @brief Returns the type of the sensor using the path segment
116      *        that precedes the sensor name.
117      *
118      * /xyz/openbmc_project/sensors/voltage/vout -> type == voltage
119      *
120      * @param[in] sensorPath - The sensor object path name
121      *
122      * @return std::string The type segment
123      */
124     std::string getSensorType(std::string sensorPath);
125 
126     /**
127      * @brief Allows for skipping event logs based on the sensor type.
128      *
129      * Specifically for the 'utilization' type because its provider
130      * doesn't support configurable thresholds yet.
131      *
132      * @param[in] type - The sensor type, like 'temperature'.
133      * @return bool - If it can be skipped or not.
134      */
135     bool skipSensorType(const std::string& type);
136 
137     /**
138      * @brief Returns the inventory path to use for a FRU callout
139      *        for the alarm exceeded errors.
140      *
141      * It finds the path by looking for 'inventory' or 'chassis'
142      * association objects on the sensor that point to a FRU.
143      *
144      * @param[in] std::string - The sensor object path
145      * @return std::string - The inventory path for the FRU callout.
146      *                       May be empty if none found.
147      */
148     std::string getCallout(const std::string& sensorPath);
149 
150     /**
151      * @brief The power state changed handler.
152      *
153      * Checks alarms when power is turned on.
154      *
155      * @param[in] powerStateOn - If the power is now on or off.
156      */
157     void powerStateChanged(bool powerStateOn);
158 
159     /**
160      * @brief The sdbusplus bus object
161      */
162     sdbusplus::bus::bus& bus;
163 
164     /**
165      * @brief The sdeventplus Event object
166      */
167     sdeventplus::Event& event;
168 
169     /**
170      * @brief The PowerState object to track power state changes.
171      */
172     std::shared_ptr<phosphor::fan::PowerState> _powerState;
173 
174     /**
175      * @brief The Warning interface match object
176      */
177     sdbusplus::bus::match::match warningMatch;
178 
179     /**
180      * @brief The Critical interface match object
181      */
182     sdbusplus::bus::match::match criticalMatch;
183 
184     /**
185      * @brief The PerformanceLoss interface match object
186      */
187     sdbusplus::bus::match::match perfLossMatch;
188 
189     /**
190      * @brief The current alarm values
191      */
192     std::map<InterfaceKey, std::map<PropertyName, bool>> alarms;
193 };
194 
195 } // namespace sensor::monitor
196