blob: 951c0ca4c04143fd3e360d08afb5d6fdd9773ea9 [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 Venture563a3562018-10-30 09:31:26 -070024std::unique_ptr<Sensor> HostSensor::createTemp(const std::string& name,
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070025 int64_t timeout,
26 sdbusplus::bus::bus& bus,
27 const char* objPath, bool defer)
Patrick Venture863b9242018-03-08 08:29:23 -080028{
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070029 auto sensor =
30 std::make_unique<HostSensor>(name, timeout, bus, objPath, defer);
Patrick Venture863b9242018-03-08 08:29:23 -080031 sensor->value(0);
32
Patrick Venturef55349d2018-06-11 11:20:42 -070033 // DegreesC and value of 0 are the defaults at present, therefore testing
34 // this code only sees scale get updated as a property.
35
Patrick Venture863b9242018-03-08 08:29:23 -080036 // TODO(venture): Need to not hard-code that this is DegreesC and scale
37 // 10x-3 unless it is! :D
38 sensor->unit(ValueInterface::Unit::DegreesC);
39 sensor->scale(-3);
40 sensor->emit_object_added();
Patrick Venturef55349d2018-06-11 11:20:42 -070041 // emit_object_added() can be called twice, harmlessly, the second time it
42 // doesn't actually happen, but we don't want to call it before we set up
43 // the initial values, so we should not let someone call this with
44 // defer=false.
Patrick Venture863b9242018-03-08 08:29:23 -080045
46 /* TODO(venture): Need to set that _updated is set to epoch or something
47 * else. what is the default value?
48 */
49 return sensor;
50}
51
52int64_t HostSensor::value(int64_t value)
53{
54 std::lock_guard<std::mutex> guard(_lock);
55
56 _updated = std::chrono::high_resolution_clock::now();
57 _value = value * pow(10, scale()); /* scale value */
58
59 return ValueObject::value(value);
60}
61
62ReadReturn HostSensor::read(void)
63{
64 std::lock_guard<std::mutex> guard(_lock);
65
66 /* This doesn't sanity check anything, that's the caller's job. */
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070067 struct ReadReturn r = {_value, _updated};
Patrick Venture863b9242018-03-08 08:29:23 -080068
69 return r;
70}
71
72void HostSensor::write(double value)
73{
74 throw std::runtime_error("Not Implemented.");
75}