blob: 39eeb5f3f547fed368166ce5f4ea7c91f78f0ab7 [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"
20
Patrick Venture863b9242018-03-08 08:29:23 -080021#include <cmath>
22#include <iostream>
23#include <memory>
24#include <mutex>
25
Patrick Venturea0764872020-08-08 07:48:43 -070026namespace pid_control
27{
28
James Feist8154e322020-06-22 17:12:18 -070029template <typename T>
30void scaleHelper(T& ptr, int64_t value)
31{
32 if constexpr (std::is_same_v<ValueType, int64_t>)
33 {
34 ptr->scale(value);
35 }
36}
37
Patrick Williamsbd63bca2024-08-16 15:21:10 -040038std::unique_ptr<Sensor> HostSensor::createTemp(
39 const std::string& name, int64_t timeout, sdbusplus::bus_t& bus,
40 const char* objPath, bool defer)
Patrick Venture863b9242018-03-08 08:29:23 -080041{
Patrick Williamsbd63bca2024-08-16 15:21:10 -040042 auto sensor =
43 std::make_unique<HostSensor>(name, timeout, bus, objPath, defer);
Patrick Venture863b9242018-03-08 08:29:23 -080044 sensor->value(0);
45
Patrick Venturef55349d2018-06-11 11:20:42 -070046 // DegreesC and value of 0 are the defaults at present, therefore testing
47 // this code only sees scale get updated as a property.
48
Patrick Venture863b9242018-03-08 08:29:23 -080049 // TODO(venture): Need to not hard-code that this is DegreesC and scale
50 // 10x-3 unless it is! :D
51 sensor->unit(ValueInterface::Unit::DegreesC);
James Feist8154e322020-06-22 17:12:18 -070052 scaleHelper(sensor, -3);
Patrick Venture863b9242018-03-08 08:29:23 -080053 sensor->emit_object_added();
Patrick Venturef55349d2018-06-11 11:20:42 -070054 // emit_object_added() can be called twice, harmlessly, the second time it
55 // doesn't actually happen, but we don't want to call it before we set up
56 // the initial values, so we should not let someone call this with
57 // defer=false.
Patrick Venture863b9242018-03-08 08:29:23 -080058
59 /* TODO(venture): Need to set that _updated is set to epoch or something
60 * else. what is the default value?
61 */
62 return sensor;
63}
64
James Feist8154e322020-06-22 17:12:18 -070065template <typename T>
66int64_t getScale(T* sensor)
67{
68 if constexpr (std::is_same_v<ValueType, int64_t>)
69 {
70 return sensor->scale();
71 }
72 return 0;
73}
74
75ValueType HostSensor::value(ValueType value)
Patrick Venture863b9242018-03-08 08:29:23 -080076{
77 std::lock_guard<std::mutex> guard(_lock);
78
79 _updated = std::chrono::high_resolution_clock::now();
James Feist8154e322020-06-22 17:12:18 -070080 _value = value * pow(10, getScale(this)); /* scale value */
Patrick Venture863b9242018-03-08 08:29:23 -080081
82 return ValueObject::value(value);
83}
84
85ReadReturn HostSensor::read(void)
86{
87 std::lock_guard<std::mutex> guard(_lock);
88
89 /* This doesn't sanity check anything, that's the caller's job. */
Patrick Venture1df9e872020-10-08 15:35:01 -070090 ReadReturn r = {_value, _updated};
Patrick Venture863b9242018-03-08 08:29:23 -080091
92 return r;
93}
94
Harvey.Wua1ae4fa2022-10-28 17:38:35 +080095void HostSensor::write([[maybe_unused]] double value)
Patrick Venture863b9242018-03-08 08:29:23 -080096{
97 throw std::runtime_error("Not Implemented.");
98}
Patrick Venturea0764872020-08-08 07:48:43 -070099
Harvey Wu17971ef2021-02-17 14:36:38 +0800100bool HostSensor::getFailed(void)
101{
102 if (std::isfinite(_value))
103 {
104 return false;
105 }
106
James Zheng6df8bb52024-11-27 23:38:47 +0000107 outputFailsafeLogWithSensor(getName(), true, getName(),
108 "The sensor has invalid readings.");
Harvey Wu17971ef2021-02-17 14:36:38 +0800109 return true;
110}
111
Patrick Venturea0764872020-08-08 07:48:43 -0700112} // namespace pid_control