| Alexander Hansen | 46a755f | 2025-10-27 16:31:08 +0100 | [diff] [blame^] | 1 | // SPDX-License-Identifier: Apache-2.0 |
| 2 | // SPDX-FileCopyrightText: Copyright 2017 Google Inc |
| Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 3 | |
| Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 4 | #include "host.hpp" |
| 5 | |
| James Zheng | 6df8bb5 | 2024-11-27 23:38:47 +0000 | [diff] [blame] | 6 | #include "failsafeloggers/failsafe_logger_utility.hpp" |
| Potin Lai | e1fa859 | 2025-08-29 15:27:08 +0800 | [diff] [blame] | 7 | #include "hoststatemonitor.hpp" |
| Ed Tanous | f8b6e55 | 2025-06-27 13:27:50 -0700 | [diff] [blame] | 8 | #include "interfaces.hpp" |
| 9 | #include "sensor.hpp" |
| James Zheng | 6df8bb5 | 2024-11-27 23:38:47 +0000 | [diff] [blame] | 10 | |
| Ed Tanous | f8b6e55 | 2025-06-27 13:27:50 -0700 | [diff] [blame] | 11 | #include <sdbusplus/bus.hpp> |
| 12 | |
| 13 | #include <chrono> |
| Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 14 | #include <cmath> |
| Ed Tanous | f8b6e55 | 2025-06-27 13:27:50 -0700 | [diff] [blame] | 15 | #include <cstdint> |
| Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 16 | #include <memory> |
| 17 | #include <mutex> |
| Ed Tanous | f8b6e55 | 2025-06-27 13:27:50 -0700 | [diff] [blame] | 18 | #include <stdexcept> |
| 19 | #include <string> |
| 20 | #include <type_traits> |
| Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 21 | |
| Patrick Venture | a076487 | 2020-08-08 07:48:43 -0700 | [diff] [blame] | 22 | namespace pid_control |
| 23 | { |
| 24 | |
| James Feist | 8154e32 | 2020-06-22 17:12:18 -0700 | [diff] [blame] | 25 | template <typename T> |
| 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 | |
| Patrick Williams | bd63bca | 2024-08-16 15:21:10 -0400 | [diff] [blame] | 34 | std::unique_ptr<Sensor> HostSensor::createTemp( |
| 35 | const std::string& name, int64_t timeout, sdbusplus::bus_t& bus, |
| Potin Lai | e1fa859 | 2025-08-29 15:27:08 +0800 | [diff] [blame] | 36 | const char* objPath, bool defer, bool ignoreFailIfHostOff) |
| Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 37 | { |
| Potin Lai | e1fa859 | 2025-08-29 15:27:08 +0800 | [diff] [blame] | 38 | auto sensor = std::make_unique<HostSensor>(name, timeout, bus, objPath, |
| 39 | defer, ignoreFailIfHostOff); |
| Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 40 | sensor->value(0); |
| 41 | |
| Patrick Venture | f55349d | 2018-06-11 11:20:42 -0700 | [diff] [blame] | 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 | |
| Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 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); |
| James Feist | 8154e32 | 2020-06-22 17:12:18 -0700 | [diff] [blame] | 48 | scaleHelper(sensor, -3); |
| Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 49 | sensor->emit_object_added(); |
| Patrick Venture | f55349d | 2018-06-11 11:20:42 -0700 | [diff] [blame] | 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. |
| Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 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 | |
| James Feist | 8154e32 | 2020-06-22 17:12:18 -0700 | [diff] [blame] | 61 | template <typename T> |
| 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 | |
| 71 | ValueType HostSensor::value(ValueType value) |
| Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 72 | { |
| 73 | std::lock_guard<std::mutex> guard(_lock); |
| 74 | |
| 75 | _updated = std::chrono::high_resolution_clock::now(); |
| James Feist | 8154e32 | 2020-06-22 17:12:18 -0700 | [diff] [blame] | 76 | _value = value * pow(10, getScale(this)); /* scale value */ |
| Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 77 | |
| 78 | return ValueObject::value(value); |
| 79 | } |
| 80 | |
| 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. */ |
| Patrick Venture | 1df9e87 | 2020-10-08 15:35:01 -0700 | [diff] [blame] | 86 | ReadReturn r = {_value, _updated}; |
| Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 87 | |
| 88 | return r; |
| 89 | } |
| 90 | |
| Harvey.Wu | a1ae4fa | 2022-10-28 17:38:35 +0800 | [diff] [blame] | 91 | void HostSensor::write([[maybe_unused]] double value) |
| Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 92 | { |
| 93 | throw std::runtime_error("Not Implemented."); |
| 94 | } |
| Patrick Venture | a076487 | 2020-08-08 07:48:43 -0700 | [diff] [blame] | 95 | |
| Harvey Wu | 17971ef | 2021-02-17 14:36:38 +0800 | [diff] [blame] | 96 | bool HostSensor::getFailed(void) |
| 97 | { |
| 98 | if (std::isfinite(_value)) |
| 99 | { |
| 100 | return false; |
| 101 | } |
| 102 | |
| Potin Lai | e1fa859 | 2025-08-29 15:27:08 +0800 | [diff] [blame] | 103 | if (getIgnoreFailIfHostOff()) |
| 104 | { |
| 105 | auto& hostState = HostStateMonitor::getInstance(); |
| 106 | if (!hostState.isPowerOn()) |
| 107 | { |
| 108 | return false; |
| 109 | } |
| 110 | } |
| 111 | |
| James Zheng | 6df8bb5 | 2024-11-27 23:38:47 +0000 | [diff] [blame] | 112 | outputFailsafeLogWithSensor(getName(), true, getName(), |
| 113 | "The sensor has invalid readings."); |
| Harvey Wu | 17971ef | 2021-02-17 14:36:38 +0800 | [diff] [blame] | 114 | return true; |
| 115 | } |
| 116 | |
| Patrick Venture | a076487 | 2020-08-08 07:48:43 -0700 | [diff] [blame] | 117 | } // namespace pid_control |