blob: f38844396518032a37dd4b9c059f01063050a7b9 [file] [log] [blame]
Patrick Venture863b9242018-03-08 08:29:23 -08001/**
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
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070017#include "host.hpp"
18
James Zheng6df8bb52024-11-27 23:38:47 +000019#include "failsafeloggers/failsafe_logger_utility.hpp"
Potin Laie1fa8592025-08-29 15:27:08 +080020#include "hoststatemonitor.hpp"
Ed Tanousf8b6e552025-06-27 13:27:50 -070021#include "interfaces.hpp"
22#include "sensor.hpp"
James Zheng6df8bb52024-11-27 23:38:47 +000023
Ed Tanousf8b6e552025-06-27 13:27:50 -070024#include <sdbusplus/bus.hpp>
25
26#include <chrono>
Patrick Venture863b9242018-03-08 08:29:23 -080027#include <cmath>
Ed Tanousf8b6e552025-06-27 13:27:50 -070028#include <cstdint>
Patrick Venture863b9242018-03-08 08:29:23 -080029#include <memory>
30#include <mutex>
Ed Tanousf8b6e552025-06-27 13:27:50 -070031#include <stdexcept>
32#include <string>
33#include <type_traits>
Patrick Venture863b9242018-03-08 08:29:23 -080034
Patrick Venturea0764872020-08-08 07:48:43 -070035namespace pid_control
36{
37
James Feist8154e322020-06-22 17:12:18 -070038template <typename T>
39void 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
Patrick Williamsbd63bca2024-08-16 15:21:10 -040047std::unique_ptr<Sensor> HostSensor::createTemp(
48 const std::string& name, int64_t timeout, sdbusplus::bus_t& bus,
Potin Laie1fa8592025-08-29 15:27:08 +080049 const char* objPath, bool defer, bool ignoreFailIfHostOff)
Patrick Venture863b9242018-03-08 08:29:23 -080050{
Potin Laie1fa8592025-08-29 15:27:08 +080051 auto sensor = std::make_unique<HostSensor>(name, timeout, bus, objPath,
52 defer, ignoreFailIfHostOff);
Patrick Venture863b9242018-03-08 08:29:23 -080053 sensor->value(0);
54
Patrick Venturef55349d2018-06-11 11:20:42 -070055 // DegreesC and value of 0 are the defaults at present, therefore testing
56 // this code only sees scale get updated as a property.
57
Patrick Venture863b9242018-03-08 08:29:23 -080058 // 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);
James Feist8154e322020-06-22 17:12:18 -070061 scaleHelper(sensor, -3);
Patrick Venture863b9242018-03-08 08:29:23 -080062 sensor->emit_object_added();
Patrick Venturef55349d2018-06-11 11:20:42 -070063 // 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.
Patrick Venture863b9242018-03-08 08:29:23 -080067
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
James Feist8154e322020-06-22 17:12:18 -070074template <typename T>
75int64_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
84ValueType HostSensor::value(ValueType value)
Patrick Venture863b9242018-03-08 08:29:23 -080085{
86 std::lock_guard<std::mutex> guard(_lock);
87
88 _updated = std::chrono::high_resolution_clock::now();
James Feist8154e322020-06-22 17:12:18 -070089 _value = value * pow(10, getScale(this)); /* scale value */
Patrick Venture863b9242018-03-08 08:29:23 -080090
91 return ValueObject::value(value);
92}
93
94ReadReturn 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. */
Patrick Venture1df9e872020-10-08 15:35:01 -070099 ReadReturn r = {_value, _updated};
Patrick Venture863b9242018-03-08 08:29:23 -0800100
101 return r;
102}
103
Harvey.Wua1ae4fa2022-10-28 17:38:35 +0800104void HostSensor::write([[maybe_unused]] double value)
Patrick Venture863b9242018-03-08 08:29:23 -0800105{
106 throw std::runtime_error("Not Implemented.");
107}
Patrick Venturea0764872020-08-08 07:48:43 -0700108
Harvey Wu17971ef2021-02-17 14:36:38 +0800109bool HostSensor::getFailed(void)
110{
111 if (std::isfinite(_value))
112 {
113 return false;
114 }
115
Potin Laie1fa8592025-08-29 15:27:08 +0800116 if (getIgnoreFailIfHostOff())
117 {
118 auto& hostState = HostStateMonitor::getInstance();
119 if (!hostState.isPowerOn())
120 {
121 return false;
122 }
123 }
124
James Zheng6df8bb52024-11-27 23:38:47 +0000125 outputFailsafeLogWithSensor(getName(), true, getName(),
126 "The sensor has invalid readings.");
Harvey Wu17971ef2021-02-17 14:36:38 +0800127 return true;
128}
129
Patrick Venturea0764872020-08-08 07:48:43 -0700130} // namespace pid_control