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 17 #include "host.hpp" 18 19 #include "failsafeloggers/failsafe_logger_utility.hpp" 20 21 #include <cmath> 22 #include <iostream> 23 #include <memory> 24 #include <mutex> 25 26 namespace pid_control 27 { 28 29 template <typename T> 30 void 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 38 std::unique_ptr<Sensor> HostSensor::createTemp( 39 const std::string& name, int64_t timeout, sdbusplus::bus_t& bus, 40 const char* objPath, bool defer) 41 { 42 auto sensor = 43 std::make_unique<HostSensor>(name, timeout, bus, objPath, defer); 44 sensor->value(0); 45 46 // DegreesC and value of 0 are the defaults at present, therefore testing 47 // this code only sees scale get updated as a property. 48 49 // 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); 52 scaleHelper(sensor, -3); 53 sensor->emit_object_added(); 54 // 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. 58 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 65 template <typename T> 66 int64_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 75 ValueType HostSensor::value(ValueType value) 76 { 77 std::lock_guard<std::mutex> guard(_lock); 78 79 _updated = std::chrono::high_resolution_clock::now(); 80 _value = value * pow(10, getScale(this)); /* scale value */ 81 82 return ValueObject::value(value); 83 } 84 85 ReadReturn 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. */ 90 ReadReturn r = {_value, _updated}; 91 92 return r; 93 } 94 95 void HostSensor::write([[maybe_unused]] double value) 96 { 97 throw std::runtime_error("Not Implemented."); 98 } 99 100 bool HostSensor::getFailed(void) 101 { 102 if (std::isfinite(_value)) 103 { 104 return false; 105 } 106 107 outputFailsafeLogWithSensor(getName(), true, getName(), 108 "The sensor has invalid readings."); 109 return true; 110 } 111 112 } // namespace pid_control 113