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