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 <cmath> 18 #include <iostream> 19 #include <memory> 20 #include <mutex> 21 22 #include "host.hpp" 23 24 std::unique_ptr<Sensor> HostSensor::CreateTemp( 25 const std::string& name, 26 int64_t timeout, 27 sdbusplus::bus::bus& bus, 28 const char* objPath, 29 bool defer) 30 { 31 auto sensor = std::make_unique<HostSensor>(name, timeout, bus, objPath, defer); 32 sensor->value(0); 33 34 // DegreesC and value of 0 are the defaults at present, therefore testing 35 // this code only sees scale get updated as a property. 36 37 // TODO(venture): Need to not hard-code that this is DegreesC and scale 38 // 10x-3 unless it is! :D 39 sensor->unit(ValueInterface::Unit::DegreesC); 40 sensor->scale(-3); 41 sensor->emit_object_added(); 42 // emit_object_added() can be called twice, harmlessly, the second time it 43 // doesn't actually happen, but we don't want to call it before we set up 44 // the initial values, so we should not let someone call this with 45 // defer=false. 46 47 /* TODO(venture): Need to set that _updated is set to epoch or something 48 * else. what is the default value? 49 */ 50 return sensor; 51 } 52 53 int64_t HostSensor::value(int64_t value) 54 { 55 std::lock_guard<std::mutex> guard(_lock); 56 57 _updated = std::chrono::high_resolution_clock::now(); 58 _value = value * pow(10, scale()); /* scale value */ 59 60 return ValueObject::value(value); 61 } 62 63 ReadReturn HostSensor::read(void) 64 { 65 std::lock_guard<std::mutex> guard(_lock); 66 67 /* This doesn't sanity check anything, that's the caller's job. */ 68 struct ReadReturn r = { 69 _value, 70 _updated 71 }; 72 73 return r; 74 } 75 76 void HostSensor::write(double value) 77 { 78 throw std::runtime_error("Not Implemented."); 79 } 80 81