1 #pragma once
2 
3 #include "dbusUtils.hpp"
4 
5 #include <xyz/openbmc_project/Sensor/Threshold/Critical/server.hpp>
6 #include <xyz/openbmc_project/Sensor/Threshold/HardShutdown/server.hpp>
7 #include <xyz/openbmc_project/Sensor/Threshold/PerformanceLoss/server.hpp>
8 #include <xyz/openbmc_project/Sensor/Threshold/SoftShutdown/server.hpp>
9 #include <xyz/openbmc_project/Sensor/Threshold/Warning/server.hpp>
10 
11 const constexpr char* entityManagerBusName =
12     "xyz.openbmc_project.EntityManager";
13 namespace phosphor::virtual_sensor
14 {
15 
16 template <typename... T>
17 using ServerObject = typename sdbusplus::server::object_t<T...>;
18 
19 namespace threshold_ns =
20     sdbusplus::xyz::openbmc_project::Sensor::Threshold::server;
21 using CriticalObject = ServerObject<threshold_ns::Critical>;
22 using WarningObject = ServerObject<threshold_ns::Warning>;
23 using SoftShutdownObject = ServerObject<threshold_ns::SoftShutdown>;
24 using HardShutdownObject = ServerObject<threshold_ns::HardShutdown>;
25 using PerformanceLossObject = ServerObject<threshold_ns::PerformanceLoss>;
26 
27 template <typename T>
28 struct Threshold;
29 
30 struct Hysteresis
31 {
32     double highHysteresis;
33     double lowHysteresis;
getHighHysteresisphosphor::virtual_sensor::Hysteresis34     auto getHighHysteresis()
35     {
36         return this->highHysteresis;
37     }
38 
getLowHysteresisphosphor::virtual_sensor::Hysteresis39     auto getLowHysteresis()
40     {
41         return this->lowHysteresis;
42     }
43 
setHighHysteresisphosphor::virtual_sensor::Hysteresis44     auto setHighHysteresis(double value)
45     {
46         this->highHysteresis = value;
47     }
48 
setLowHysteresisphosphor::virtual_sensor::Hysteresis49     auto setLowHysteresis(double value)
50     {
51         this->lowHysteresis = value;
52     }
53 };
54 
55 template <>
56 struct Threshold<WarningObject> : public WarningObject, public Hysteresis
57 {
58     static constexpr auto name = "Warning";
59     using WarningObject::WarningObject;
60     /** @brief sdbusplus bus client connection. */
61     sdbusplus::bus_t& bus;
62     std::string objPath;
63 
64     /** @brief Virtual sensor path/interface in entityManagerDbus.
65      * This 3 value is used to set thresholds
66      */
67     std::string entityPath;
68     std::string entityInterfaceHigh;
69     std::string entityInterfaceLow;
70 
71     /** @brief Constructor to put object onto bus at a dbus path.
72      *  @param[in] bus - Bus to attach to.
73      *  @param[in] path - Path to attach at.
74      */
Thresholdphosphor::virtual_sensor::Threshold75     Threshold(sdbusplus::bus_t& bus, const char* path) :
76         WarningObject(bus, path), bus(bus), objPath(std::string(path))
77     {}
78 
highphosphor::virtual_sensor::Threshold79     auto high()
80     {
81         return WarningObject::warningHigh();
82     }
lowphosphor::virtual_sensor::Threshold83     auto low()
84     {
85         return WarningObject::warningLow();
86     }
87 
88     template <typename... Args>
alarmHighphosphor::virtual_sensor::Threshold89     auto alarmHigh(Args... args)
90     {
91         return warningAlarmHigh(std::forward<Args>(args)...);
92     }
93 
94     template <typename... Args>
alarmLowphosphor::virtual_sensor::Threshold95     auto alarmLow(Args... args)
96     {
97         return warningAlarmLow(std::forward<Args>(args)...);
98     }
99 
100     template <typename... Args>
alarmHighSignalAssertedphosphor::virtual_sensor::Threshold101     auto alarmHighSignalAsserted(Args... args)
102     {
103         return warningHighAlarmAsserted(std::forward<Args>(args)...);
104     }
105 
106     template <typename... Args>
alarmHighSignalDeassertedphosphor::virtual_sensor::Threshold107     auto alarmHighSignalDeasserted(Args... args)
108     {
109         return warningHighAlarmDeasserted(std::forward<Args>(args)...);
110     }
111 
112     template <typename... Args>
alarmLowSignalAssertedphosphor::virtual_sensor::Threshold113     auto alarmLowSignalAsserted(Args... args)
114     {
115         return warningLowAlarmAsserted(std::forward<Args>(args)...);
116     }
117 
118     template <typename... Args>
alarmLowSignalDeassertedphosphor::virtual_sensor::Threshold119     auto alarmLowSignalDeasserted(Args... args)
120     {
121         return warningLowAlarmDeasserted(std::forward<Args>(args)...);
122     }
123 
124     /** @brief Set value of WarningHigh */
warningHighphosphor::virtual_sensor::Threshold125     virtual double warningHigh(double value)
126     {
127         if (!entityPath.empty() && !entityInterfaceHigh.empty())
128         {
129             // persistThreshold
130             setDbusProperty(bus, entityManagerBusName, entityPath,
131                             entityInterfaceHigh, "Value", value);
132         }
133         return WarningObject::warningHigh(value);
134     }
135 
136     /** @brief Set value of WarningLow */
warningLowphosphor::virtual_sensor::Threshold137     virtual double warningLow(double value)
138     {
139         if (!entityPath.empty() && !entityInterfaceLow.empty())
140         {
141             // persistThreshold
142             setDbusProperty(bus, entityManagerBusName, entityPath,
143                             entityInterfaceLow, "Value", value);
144         }
145         return WarningObject::warningLow(value);
146     }
147 
148     /** @brief Set the entitymanager interface corresponding to virtualsensor
149      * warningLow
150      */
setEntityInterfaceLowphosphor::virtual_sensor::Threshold151     void setEntityInterfaceLow(const std::string& interfaceLow)
152     {
153         entityInterfaceLow = interfaceLow;
154     }
155 
156     /** @brief Set the entitymanager interface corresponding to virtualsensor
157      * warningHigh
158      */
setEntityInterfaceHighphosphor::virtual_sensor::Threshold159     void setEntityInterfaceHigh(const std::string& interfaceHigh)
160     {
161         entityInterfaceHigh = interfaceHigh;
162     }
163 
164     /** @brief Set the entitymanager path corresponding to virtualsensor warning
165      */
setEntityPathphosphor::virtual_sensor::Threshold166     void setEntityPath(const std::string& path)
167     {
168         entityPath = path;
169     }
170 };
171 
172 template <>
173 struct Threshold<CriticalObject> : public CriticalObject, public Hysteresis
174 {
175     static constexpr auto name = "Critical";
176 
177     /** @brief sdbusplus bus client connection. */
178     sdbusplus::bus_t& bus;
179     std::string objPath;
180 
181     /** @brief Virtual sensor path/interface in entityManagerDbus.
182      * This 3 value is used to set thresholds
183      */
184     std::string entityPath;
185     std::string entityInterfaceHigh;
186     std::string entityInterfaceLow;
187 
188     using CriticalObject::CriticalObject;
189 
190     /** @brief Constructor to put object onto bus at a dbus path.
191      *  @param[in] bus - Bus to attach to.
192      *  @param[in] path - Path to attach at.
193      */
Thresholdphosphor::virtual_sensor::Threshold194     Threshold(sdbusplus::bus_t& bus, const char* path) :
195         CriticalObject(bus, path), bus(bus), objPath(std::string(path))
196     {}
197 
highphosphor::virtual_sensor::Threshold198     auto high()
199     {
200         return CriticalObject::criticalHigh();
201     }
lowphosphor::virtual_sensor::Threshold202     auto low()
203     {
204         return CriticalObject::criticalLow();
205     }
206 
207     template <typename... Args>
alarmHighphosphor::virtual_sensor::Threshold208     auto alarmHigh(Args... args)
209     {
210         return criticalAlarmHigh(std::forward<Args>(args)...);
211     }
212 
213     template <typename... Args>
alarmLowphosphor::virtual_sensor::Threshold214     auto alarmLow(Args... args)
215     {
216         return criticalAlarmLow(std::forward<Args>(args)...);
217     }
218 
219     template <typename... Args>
alarmHighSignalAssertedphosphor::virtual_sensor::Threshold220     auto alarmHighSignalAsserted(Args... args)
221     {
222         return criticalHighAlarmAsserted(std::forward<Args>(args)...);
223     }
224 
225     template <typename... Args>
alarmHighSignalDeassertedphosphor::virtual_sensor::Threshold226     auto alarmHighSignalDeasserted(Args... args)
227     {
228         return criticalHighAlarmDeasserted(std::forward<Args>(args)...);
229     }
230 
231     template <typename... Args>
alarmLowSignalAssertedphosphor::virtual_sensor::Threshold232     auto alarmLowSignalAsserted(Args... args)
233     {
234         return criticalLowAlarmAsserted(std::forward<Args>(args)...);
235     }
236 
237     template <typename... Args>
alarmLowSignalDeassertedphosphor::virtual_sensor::Threshold238     auto alarmLowSignalDeasserted(Args... args)
239     {
240         return criticalLowAlarmDeasserted(std::forward<Args>(args)...);
241     }
242 
243     /** @brief Set value of CriticalHigh */
criticalHighphosphor::virtual_sensor::Threshold244     virtual double criticalHigh(double value)
245     {
246         // persistThreshold
247         if (!entityPath.empty() && !entityInterfaceHigh.empty())
248         {
249             setDbusProperty(bus, entityManagerBusName, entityPath,
250                             entityInterfaceHigh, "Value", value);
251         }
252         return CriticalObject::criticalHigh(value);
253     }
254 
255     /** @brief Set value of CriticalLow */
criticalLowphosphor::virtual_sensor::Threshold256     virtual double criticalLow(double value)
257     {
258         if (!entityPath.empty() && !entityInterfaceLow.empty())
259         {
260             setDbusProperty(bus, entityManagerBusName, entityPath,
261                             entityInterfaceLow, "Value", value);
262         }
263         return CriticalObject::criticalLow(value);
264     }
265 
266     /** @brief Set the entitymanager interface corresponding to virtualsensor
267      * criticalLow
268      */
setEntityInterfaceLowphosphor::virtual_sensor::Threshold269     void setEntityInterfaceLow(const std::string& interfaceLow)
270     {
271         entityInterfaceLow = interfaceLow;
272     }
273 
274     /** @brief Set the entitymanager interface corresponding to virtualsensor
275      * criticalLow
276      */
setEntityInterfaceHighphosphor::virtual_sensor::Threshold277     void setEntityInterfaceHigh(const std::string& interfaceHigh)
278     {
279         entityInterfaceHigh = interfaceHigh;
280     }
281 
282     /** @brief Set the entitymanager path corresponding to virtualsensor warning
283      */
setEntityPathphosphor::virtual_sensor::Threshold284     void setEntityPath(const std::string& path)
285     {
286         entityPath = path;
287     }
288 };
289 
290 template <>
291 struct Threshold<SoftShutdownObject> :
292     public SoftShutdownObject,
293     public Hysteresis
294 {
295     static constexpr auto name = "SoftShutdown";
296     using SoftShutdownObject::SoftShutdownObject;
297 
highphosphor::virtual_sensor::Threshold298     auto high()
299     {
300         return softShutdownHigh();
301     }
lowphosphor::virtual_sensor::Threshold302     auto low()
303     {
304         return softShutdownLow();
305     }
306 
307     template <typename... Args>
alarmHighphosphor::virtual_sensor::Threshold308     auto alarmHigh(Args... args)
309     {
310         return softShutdownAlarmHigh(std::forward<Args>(args)...);
311     }
312 
313     template <typename... Args>
alarmLowphosphor::virtual_sensor::Threshold314     auto alarmLow(Args... args)
315     {
316         return softShutdownAlarmLow(std::forward<Args>(args)...);
317     }
318 
319     template <typename... Args>
alarmHighSignalAssertedphosphor::virtual_sensor::Threshold320     auto alarmHighSignalAsserted(Args... args)
321     {
322         return softShutdownHighAlarmAsserted(std::forward<Args>(args)...);
323     }
324 
325     template <typename... Args>
alarmHighSignalDeassertedphosphor::virtual_sensor::Threshold326     auto alarmHighSignalDeasserted(Args... args)
327     {
328         return softShutdownHighAlarmDeasserted(std::forward<Args>(args)...);
329     }
330 
331     template <typename... Args>
alarmLowSignalAssertedphosphor::virtual_sensor::Threshold332     auto alarmLowSignalAsserted(Args... args)
333     {
334         return softShutdownLowAlarmAsserted(std::forward<Args>(args)...);
335     }
336 
337     template <typename... Args>
alarmLowSignalDeassertedphosphor::virtual_sensor::Threshold338     auto alarmLowSignalDeasserted(Args... args)
339     {
340         return softShutdownLowAlarmDeasserted(std::forward<Args>(args)...);
341     }
342 };
343 
344 template <>
345 struct Threshold<HardShutdownObject> :
346     public HardShutdownObject,
347     public Hysteresis
348 {
349     static constexpr auto name = "HardShutdown";
350     using HardShutdownObject::HardShutdownObject;
351 
highphosphor::virtual_sensor::Threshold352     auto high()
353     {
354         return hardShutdownHigh();
355     }
lowphosphor::virtual_sensor::Threshold356     auto low()
357     {
358         return hardShutdownLow();
359     }
360 
361     template <typename... Args>
alarmHighphosphor::virtual_sensor::Threshold362     auto alarmHigh(Args... args)
363     {
364         return hardShutdownAlarmHigh(std::forward<Args>(args)...);
365     }
366 
367     template <typename... Args>
alarmLowphosphor::virtual_sensor::Threshold368     auto alarmLow(Args... args)
369     {
370         return hardShutdownAlarmLow(std::forward<Args>(args)...);
371     }
372 
373     template <typename... Args>
alarmHighSignalAssertedphosphor::virtual_sensor::Threshold374     auto alarmHighSignalAsserted(Args... args)
375     {
376         return hardShutdownHighAlarmAsserted(std::forward<Args>(args)...);
377     }
378 
379     template <typename... Args>
alarmHighSignalDeassertedphosphor::virtual_sensor::Threshold380     auto alarmHighSignalDeasserted(Args... args)
381     {
382         return hardShutdownHighAlarmDeasserted(std::forward<Args>(args)...);
383     }
384 
385     template <typename... Args>
alarmLowSignalAssertedphosphor::virtual_sensor::Threshold386     auto alarmLowSignalAsserted(Args... args)
387     {
388         return hardShutdownLowAlarmAsserted(std::forward<Args>(args)...);
389     }
390 
391     template <typename... Args>
alarmLowSignalDeassertedphosphor::virtual_sensor::Threshold392     auto alarmLowSignalDeasserted(Args... args)
393     {
394         return hardShutdownLowAlarmDeasserted(std::forward<Args>(args)...);
395     }
396 };
397 
398 template <>
399 struct Threshold<PerformanceLossObject> :
400     public PerformanceLossObject,
401     public Hysteresis
402 {
403     static constexpr auto name = "PerformanceLoss";
404     using PerformanceLossObject::PerformanceLossObject;
405     double performanceLossHighHysteresis;
406     double performanceLossLowHysteresis;
407 
highphosphor::virtual_sensor::Threshold408     auto high()
409     {
410         return performanceLossHigh();
411     }
lowphosphor::virtual_sensor::Threshold412     auto low()
413     {
414         return performanceLossLow();
415     }
416 
417     template <typename... Args>
alarmHighphosphor::virtual_sensor::Threshold418     auto alarmHigh(Args... args)
419     {
420         return performanceLossAlarmHigh(std::forward<Args>(args)...);
421     }
422 
423     template <typename... Args>
alarmLowphosphor::virtual_sensor::Threshold424     auto alarmLow(Args... args)
425     {
426         return performanceLossAlarmLow(std::forward<Args>(args)...);
427     }
428 
429     template <typename... Args>
alarmHighSignalAssertedphosphor::virtual_sensor::Threshold430     auto alarmHighSignalAsserted(Args... args)
431     {
432         return performanceLossHighAlarmAsserted(std::forward<Args>(args)...);
433     }
434 
435     template <typename... Args>
alarmHighSignalDeassertedphosphor::virtual_sensor::Threshold436     auto alarmHighSignalDeasserted(Args... args)
437     {
438         return performanceLossHighAlarmDeasserted(std::forward<Args>(args)...);
439     }
440 
441     template <typename... Args>
alarmLowSignalAssertedphosphor::virtual_sensor::Threshold442     auto alarmLowSignalAsserted(Args... args)
443     {
444         return performanceLossLowAlarmAsserted(std::forward<Args>(args)...);
445     }
446 
447     template <typename... Args>
alarmLowSignalDeassertedphosphor::virtual_sensor::Threshold448     auto alarmLowSignalDeasserted(Args... args)
449     {
450         return performanceLossLowAlarmDeasserted(std::forward<Args>(args)...);
451     }
452 };
453 
454 } // namespace phosphor::virtual_sensor
455