Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 1 | /** |
| 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 Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 17 | #include "host.hpp" |
| 18 | |
Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 19 | #include <cmath> |
| 20 | #include <iostream> |
| 21 | #include <memory> |
| 22 | #include <mutex> |
| 23 | |
James Feist | 8154e32 | 2020-06-22 17:12:18 -0700 | [diff] [blame^] | 24 | template <typename T> |
| 25 | void scaleHelper(T& ptr, int64_t value) |
| 26 | { |
| 27 | if constexpr (std::is_same_v<ValueType, int64_t>) |
| 28 | { |
| 29 | ptr->scale(value); |
| 30 | } |
| 31 | } |
| 32 | |
Patrick Venture | 563a356 | 2018-10-30 09:31:26 -0700 | [diff] [blame] | 33 | std::unique_ptr<Sensor> HostSensor::createTemp(const std::string& name, |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 34 | int64_t timeout, |
| 35 | sdbusplus::bus::bus& bus, |
| 36 | const char* objPath, bool defer) |
Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 37 | { |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 38 | auto sensor = |
| 39 | std::make_unique<HostSensor>(name, timeout, bus, objPath, defer); |
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 | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 86 | struct ReadReturn r = {_value, _updated}; |
Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 87 | |
| 88 | return r; |
| 89 | } |
| 90 | |
| 91 | void HostSensor::write(double value) |
| 92 | { |
| 93 | throw std::runtime_error("Not Implemented."); |
| 94 | } |