blob: 68f28341ced1c7491cd32944046b92554b7f1e24 [file] [log] [blame]
Alexander Hansen46a755f2025-10-27 16:31:08 +01001// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright 2017 Google Inc
Patrick Venture863b9242018-03-08 08:29:23 -08003
Patrick Ventureda4a5dd2018-08-31 09:42:48 -07004#include "host.hpp"
5
James Zheng6df8bb52024-11-27 23:38:47 +00006#include "failsafeloggers/failsafe_logger_utility.hpp"
Potin Laie1fa8592025-08-29 15:27:08 +08007#include "hoststatemonitor.hpp"
Ed Tanousf8b6e552025-06-27 13:27:50 -07008#include "interfaces.hpp"
9#include "sensor.hpp"
James Zheng6df8bb52024-11-27 23:38:47 +000010
Ed Tanousf8b6e552025-06-27 13:27:50 -070011#include <sdbusplus/bus.hpp>
12
13#include <chrono>
Patrick Venture863b9242018-03-08 08:29:23 -080014#include <cmath>
Ed Tanousf8b6e552025-06-27 13:27:50 -070015#include <cstdint>
Patrick Venture863b9242018-03-08 08:29:23 -080016#include <memory>
17#include <mutex>
Ed Tanousf8b6e552025-06-27 13:27:50 -070018#include <stdexcept>
19#include <string>
20#include <type_traits>
Patrick Venture863b9242018-03-08 08:29:23 -080021
Patrick Venturea0764872020-08-08 07:48:43 -070022namespace pid_control
23{
24
James Feist8154e322020-06-22 17:12:18 -070025template <typename T>
26void 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
Patrick Williamsbd63bca2024-08-16 15:21:10 -040034std::unique_ptr<Sensor> HostSensor::createTemp(
35 const std::string& name, int64_t timeout, sdbusplus::bus_t& bus,
Potin Laie1fa8592025-08-29 15:27:08 +080036 const char* objPath, bool defer, bool ignoreFailIfHostOff)
Patrick Venture863b9242018-03-08 08:29:23 -080037{
Potin Laie1fa8592025-08-29 15:27:08 +080038 auto sensor = std::make_unique<HostSensor>(name, timeout, bus, objPath,
39 defer, ignoreFailIfHostOff);
Patrick Venture863b9242018-03-08 08:29:23 -080040 sensor->value(0);
41
Patrick Venturef55349d2018-06-11 11:20:42 -070042 // DegreesC and value of 0 are the defaults at present, therefore testing
43 // this code only sees scale get updated as a property.
44
Patrick Venture863b9242018-03-08 08:29:23 -080045 // 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);
James Feist8154e322020-06-22 17:12:18 -070048 scaleHelper(sensor, -3);
Patrick Venture863b9242018-03-08 08:29:23 -080049 sensor->emit_object_added();
Patrick Venturef55349d2018-06-11 11:20:42 -070050 // 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.
Patrick Venture863b9242018-03-08 08:29:23 -080054
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
James Feist8154e322020-06-22 17:12:18 -070061template <typename T>
62int64_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
71ValueType HostSensor::value(ValueType value)
Patrick Venture863b9242018-03-08 08:29:23 -080072{
73 std::lock_guard<std::mutex> guard(_lock);
74
75 _updated = std::chrono::high_resolution_clock::now();
James Feist8154e322020-06-22 17:12:18 -070076 _value = value * pow(10, getScale(this)); /* scale value */
Patrick Venture863b9242018-03-08 08:29:23 -080077
78 return ValueObject::value(value);
79}
80
81ReadReturn 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. */
Patrick Venture1df9e872020-10-08 15:35:01 -070086 ReadReturn r = {_value, _updated};
Patrick Venture863b9242018-03-08 08:29:23 -080087
88 return r;
89}
90
Harvey.Wua1ae4fa2022-10-28 17:38:35 +080091void HostSensor::write([[maybe_unused]] double value)
Patrick Venture863b9242018-03-08 08:29:23 -080092{
93 throw std::runtime_error("Not Implemented.");
94}
Patrick Venturea0764872020-08-08 07:48:43 -070095
Harvey Wu17971ef2021-02-17 14:36:38 +080096bool HostSensor::getFailed(void)
97{
98 if (std::isfinite(_value))
99 {
100 return false;
101 }
102
Potin Laie1fa8592025-08-29 15:27:08 +0800103 if (getIgnoreFailIfHostOff())
104 {
105 auto& hostState = HostStateMonitor::getInstance();
106 if (!hostState.isPowerOn())
107 {
108 return false;
109 }
110 }
111
James Zheng6df8bb52024-11-27 23:38:47 +0000112 outputFailsafeLogWithSensor(getName(), true, getName(),
113 "The sensor has invalid readings.");
Harvey Wu17971ef2021-02-17 14:36:38 +0800114 return true;
115}
116
Patrick Venturea0764872020-08-08 07:48:43 -0700117} // namespace pid_control