xref: /openbmc/phosphor-pid-control/sensors/host.cpp (revision 46a755fce8dc0bdd9c0c5ea09d55d3e5494f335f)
1 // SPDX-License-Identifier: Apache-2.0
2 // SPDX-FileCopyrightText: Copyright 2017 Google Inc
3 
4 #include "host.hpp"
5 
6 #include "failsafeloggers/failsafe_logger_utility.hpp"
7 #include "hoststatemonitor.hpp"
8 #include "interfaces.hpp"
9 #include "sensor.hpp"
10 
11 #include <sdbusplus/bus.hpp>
12 
13 #include <chrono>
14 #include <cmath>
15 #include <cstdint>
16 #include <memory>
17 #include <mutex>
18 #include <stdexcept>
19 #include <string>
20 #include <type_traits>
21 
22 namespace pid_control
23 {
24 
25 template <typename T>
scaleHelper(T & ptr,int64_t value)26 void scaleHelper(T& ptr, int64_t value)
27 {
28     if constexpr (std::is_same_v<ValueType, int64_t>)
29     {
30         ptr->scale(value);
31     }
32 }
33 
createTemp(const std::string & name,int64_t timeout,sdbusplus::bus_t & bus,const char * objPath,bool defer,bool ignoreFailIfHostOff)34 std::unique_ptr<Sensor> HostSensor::createTemp(
35     const std::string& name, int64_t timeout, sdbusplus::bus_t& bus,
36     const char* objPath, bool defer, bool ignoreFailIfHostOff)
37 {
38     auto sensor = std::make_unique<HostSensor>(name, timeout, bus, objPath,
39                                                defer, ignoreFailIfHostOff);
40     sensor->value(0);
41 
42     // DegreesC and value of 0 are the defaults at present, therefore testing
43     // this code only sees scale get updated as a property.
44 
45     // TODO(venture): Need to not hard-code that this is DegreesC and scale
46     // 10x-3 unless it is! :D
47     sensor->unit(ValueInterface::Unit::DegreesC);
48     scaleHelper(sensor, -3);
49     sensor->emit_object_added();
50     // emit_object_added() can be called twice, harmlessly, the second time it
51     // doesn't actually happen, but we don't want to call it before we set up
52     // the initial values, so we should not let someone call this with
53     // defer=false.
54 
55     /* TODO(venture): Need to set that _updated is set to epoch or something
56      * else.  what is the default value?
57      */
58     return sensor;
59 }
60 
61 template <typename T>
getScale(T * sensor)62 int64_t getScale(T* sensor)
63 {
64     if constexpr (std::is_same_v<ValueType, int64_t>)
65     {
66         return sensor->scale();
67     }
68     return 0;
69 }
70 
value(ValueType value)71 ValueType HostSensor::value(ValueType value)
72 {
73     std::lock_guard<std::mutex> guard(_lock);
74 
75     _updated = std::chrono::high_resolution_clock::now();
76     _value = value * pow(10, getScale(this)); /* scale value */
77 
78     return ValueObject::value(value);
79 }
80 
read(void)81 ReadReturn HostSensor::read(void)
82 {
83     std::lock_guard<std::mutex> guard(_lock);
84 
85     /* This doesn't sanity check anything, that's the caller's job. */
86     ReadReturn r = {_value, _updated};
87 
88     return r;
89 }
90 
write(double value)91 void HostSensor::write([[maybe_unused]] double value)
92 {
93     throw std::runtime_error("Not Implemented.");
94 }
95 
getFailed(void)96 bool HostSensor::getFailed(void)
97 {
98     if (std::isfinite(_value))
99     {
100         return false;
101     }
102 
103     if (getIgnoreFailIfHostOff())
104     {
105         auto& hostState = HostStateMonitor::getInstance();
106         if (!hostState.isPowerOn())
107         {
108             return false;
109         }
110     }
111 
112     outputFailsafeLogWithSensor(getName(), true, getName(),
113                                 "The sensor has invalid readings.");
114     return true;
115 }
116 
117 } // namespace pid_control
118