blob: 44e83d82d53d67a06e31eacc7bf53c52fe2f277a [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
17#include <cmath>
18#include <iostream>
19#include <memory>
20#include <mutex>
21
22#include "host.hpp"
23
24std::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 // TODO(venture): Need to not hard-code that this is DegreesC and scale
35 // 10x-3 unless it is! :D
36 sensor->unit(ValueInterface::Unit::DegreesC);
37 sensor->scale(-3);
38 sensor->emit_object_added();
39
40 /* TODO(venture): Need to set that _updated is set to epoch or something
41 * else. what is the default value?
42 */
43 return sensor;
44}
45
46int64_t HostSensor::value(int64_t value)
47{
48 std::lock_guard<std::mutex> guard(_lock);
49
50 _updated = std::chrono::high_resolution_clock::now();
51 _value = value * pow(10, scale()); /* scale value */
52
53 return ValueObject::value(value);
54}
55
56ReadReturn HostSensor::read(void)
57{
58 std::lock_guard<std::mutex> guard(_lock);
59
60 /* This doesn't sanity check anything, that's the caller's job. */
61 struct ReadReturn r = {
62 _value,
63 _updated
64 };
65
66 return r;
67}
68
69void HostSensor::write(double value)
70{
71 throw std::runtime_error("Not Implemented.");
72}
73