blob: e05ac370f3e058a803a77183da8e3cd92c8a24c4 [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
Patrick Venture863b9242018-03-08 08:29:23 -080019#include <cmath>
20#include <iostream>
21#include <memory>
22#include <mutex>
23
Patrick Venturea0764872020-08-08 07:48:43 -070024namespace pid_control
25{
26
James Feist8154e322020-06-22 17:12:18 -070027template <typename T>
28void scaleHelper(T& ptr, int64_t value)
29{
30 if constexpr (std::is_same_v<ValueType, int64_t>)
31 {
32 ptr->scale(value);
33 }
34}
35
Patrick Venture563a3562018-10-30 09:31:26 -070036std::unique_ptr<Sensor> HostSensor::createTemp(const std::string& name,
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070037 int64_t timeout,
38 sdbusplus::bus::bus& bus,
39 const char* objPath, bool defer)
Patrick Venture863b9242018-03-08 08:29:23 -080040{
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070041 auto sensor =
42 std::make_unique<HostSensor>(name, timeout, bus, objPath, defer);
Patrick Venture863b9242018-03-08 08:29:23 -080043 sensor->value(0);
44
Patrick Venturef55349d2018-06-11 11:20:42 -070045 // DegreesC and value of 0 are the defaults at present, therefore testing
46 // this code only sees scale get updated as a property.
47
Patrick Venture863b9242018-03-08 08:29:23 -080048 // TODO(venture): Need to not hard-code that this is DegreesC and scale
49 // 10x-3 unless it is! :D
50 sensor->unit(ValueInterface::Unit::DegreesC);
James Feist8154e322020-06-22 17:12:18 -070051 scaleHelper(sensor, -3);
Patrick Venture863b9242018-03-08 08:29:23 -080052 sensor->emit_object_added();
Patrick Venturef55349d2018-06-11 11:20:42 -070053 // emit_object_added() can be called twice, harmlessly, the second time it
54 // doesn't actually happen, but we don't want to call it before we set up
55 // the initial values, so we should not let someone call this with
56 // defer=false.
Patrick Venture863b9242018-03-08 08:29:23 -080057
58 /* TODO(venture): Need to set that _updated is set to epoch or something
59 * else. what is the default value?
60 */
61 return sensor;
62}
63
James Feist8154e322020-06-22 17:12:18 -070064template <typename T>
65int64_t getScale(T* sensor)
66{
67 if constexpr (std::is_same_v<ValueType, int64_t>)
68 {
69 return sensor->scale();
70 }
71 return 0;
72}
73
74ValueType HostSensor::value(ValueType value)
Patrick Venture863b9242018-03-08 08:29:23 -080075{
76 std::lock_guard<std::mutex> guard(_lock);
77
78 _updated = std::chrono::high_resolution_clock::now();
James Feist8154e322020-06-22 17:12:18 -070079 _value = value * pow(10, getScale(this)); /* scale value */
Patrick Venture863b9242018-03-08 08:29:23 -080080
81 return ValueObject::value(value);
82}
83
84ReadReturn HostSensor::read(void)
85{
86 std::lock_guard<std::mutex> guard(_lock);
87
88 /* This doesn't sanity check anything, that's the caller's job. */
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070089 struct ReadReturn r = {_value, _updated};
Patrick Venture863b9242018-03-08 08:29:23 -080090
91 return r;
92}
93
94void HostSensor::write(double value)
95{
96 throw std::runtime_error("Not Implemented.");
97}
Patrick Venturea0764872020-08-08 07:48:43 -070098
99} // namespace pid_control