blob: 0bf78e0fd2ef2ca8bd02595c7247f13accd1e06c [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"
Ed Tanousf8b6e552025-06-27 13:27:50 -070020#include "interfaces.hpp"
21#include "sensor.hpp"
James Zheng6df8bb52024-11-27 23:38:47 +000022
Ed Tanousf8b6e552025-06-27 13:27:50 -070023#include <sdbusplus/bus.hpp>
24
25#include <chrono>
Patrick Venture863b9242018-03-08 08:29:23 -080026#include <cmath>
Ed Tanousf8b6e552025-06-27 13:27:50 -070027#include <cstdint>
Patrick Venture863b9242018-03-08 08:29:23 -080028#include <memory>
29#include <mutex>
Ed Tanousf8b6e552025-06-27 13:27:50 -070030#include <stdexcept>
31#include <string>
32#include <type_traits>
Patrick Venture863b9242018-03-08 08:29:23 -080033
Patrick Venturea0764872020-08-08 07:48:43 -070034namespace pid_control
35{
36
James Feist8154e322020-06-22 17:12:18 -070037template <typename T>
38void scaleHelper(T& ptr, int64_t value)
39{
40 if constexpr (std::is_same_v<ValueType, int64_t>)
41 {
42 ptr->scale(value);
43 }
44}
45
Patrick Williamsbd63bca2024-08-16 15:21:10 -040046std::unique_ptr<Sensor> HostSensor::createTemp(
47 const std::string& name, int64_t timeout, sdbusplus::bus_t& bus,
48 const char* objPath, bool defer)
Patrick Venture863b9242018-03-08 08:29:23 -080049{
Patrick Williamsbd63bca2024-08-16 15:21:10 -040050 auto sensor =
51 std::make_unique<HostSensor>(name, timeout, bus, objPath, defer);
Patrick Venture863b9242018-03-08 08:29:23 -080052 sensor->value(0);
53
Patrick Venturef55349d2018-06-11 11:20:42 -070054 // DegreesC and value of 0 are the defaults at present, therefore testing
55 // this code only sees scale get updated as a property.
56
Patrick Venture863b9242018-03-08 08:29:23 -080057 // TODO(venture): Need to not hard-code that this is DegreesC and scale
58 // 10x-3 unless it is! :D
59 sensor->unit(ValueInterface::Unit::DegreesC);
James Feist8154e322020-06-22 17:12:18 -070060 scaleHelper(sensor, -3);
Patrick Venture863b9242018-03-08 08:29:23 -080061 sensor->emit_object_added();
Patrick Venturef55349d2018-06-11 11:20:42 -070062 // emit_object_added() can be called twice, harmlessly, the second time it
63 // doesn't actually happen, but we don't want to call it before we set up
64 // the initial values, so we should not let someone call this with
65 // defer=false.
Patrick Venture863b9242018-03-08 08:29:23 -080066
67 /* TODO(venture): Need to set that _updated is set to epoch or something
68 * else. what is the default value?
69 */
70 return sensor;
71}
72
James Feist8154e322020-06-22 17:12:18 -070073template <typename T>
74int64_t getScale(T* sensor)
75{
76 if constexpr (std::is_same_v<ValueType, int64_t>)
77 {
78 return sensor->scale();
79 }
80 return 0;
81}
82
83ValueType HostSensor::value(ValueType value)
Patrick Venture863b9242018-03-08 08:29:23 -080084{
85 std::lock_guard<std::mutex> guard(_lock);
86
87 _updated = std::chrono::high_resolution_clock::now();
James Feist8154e322020-06-22 17:12:18 -070088 _value = value * pow(10, getScale(this)); /* scale value */
Patrick Venture863b9242018-03-08 08:29:23 -080089
90 return ValueObject::value(value);
91}
92
93ReadReturn HostSensor::read(void)
94{
95 std::lock_guard<std::mutex> guard(_lock);
96
97 /* This doesn't sanity check anything, that's the caller's job. */
Patrick Venture1df9e872020-10-08 15:35:01 -070098 ReadReturn r = {_value, _updated};
Patrick Venture863b9242018-03-08 08:29:23 -080099
100 return r;
101}
102
Harvey.Wua1ae4fa2022-10-28 17:38:35 +0800103void HostSensor::write([[maybe_unused]] double value)
Patrick Venture863b9242018-03-08 08:29:23 -0800104{
105 throw std::runtime_error("Not Implemented.");
106}
Patrick Venturea0764872020-08-08 07:48:43 -0700107
Harvey Wu17971ef2021-02-17 14:36:38 +0800108bool HostSensor::getFailed(void)
109{
110 if (std::isfinite(_value))
111 {
112 return false;
113 }
114
James Zheng6df8bb52024-11-27 23:38:47 +0000115 outputFailsafeLogWithSensor(getName(), true, getName(),
116 "The sensor has invalid readings.");
Harvey Wu17971ef2021-02-17 14:36:38 +0800117 return true;
118}
119
Patrick Venturea0764872020-08-08 07:48:43 -0700120} // namespace pid_control