blob: 7d494df84b33a2ed08d3b3634e26ceedf0666638 [file] [log] [blame]
Brad Bishop29dbfa62016-12-19 13:39:57 -05001/**
2 * Copyright © 2016 IBM Corporation
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 */
Patrick Williams3667cf32015-10-20 22:39:11 -050016#include <iostream>
17#include <memory>
18#include <thread>
Matthew Barth6292aee2016-10-06 10:15:48 -050019#include "argument.hpp"
20#include "sensorset.hpp"
21#include "sensorcache.hpp"
22#include "hwmon.hpp"
23#include "sysfs.hpp"
Patrick Williams3667cf32015-10-20 22:39:11 -050024
25static void exit_with_error(const char* err, char** argv)
26{
27 ArgumentParser::usage(argv);
28 std::cerr << std::endl;
29 std::cerr << "ERROR: " << err << std::endl;
30 exit(-1);
31}
32
33int main(int argc, char** argv)
34{
35 // Read arguments.
36 auto options = std::make_unique<ArgumentParser>(argc, argv);
37
38 // Parse out path argument.
39 auto path = (*options)["path"];
40 if (path == ArgumentParser::empty_string)
41 {
42 exit_with_error("Path not specified.", argv);
43 }
44
Vishwanatha Subbanna52b80282016-12-02 23:58:55 +053045 // Finished getting options out, so cleanup the parser.
46 options.reset();
Patrick Williams3667cf32015-10-20 22:39:11 -050047
48 // Check sysfs for available sensors.
49 auto sensors = std::make_unique<SensorSet>(path);
50 auto sensor_cache = std::make_unique<SensorCache>();
51
52 // TODO: Issue#3 - Need to make calls to the dbus sensor cache here to
53 // ensure the objects all exist?
54
55 // Polling loop.
56 while(true)
57 {
58 // Iterate through all the sensors.
59 for(auto& i : *sensors)
60 {
61 if (i.second.find(hwmon::entry::input) != i.second.end())
62 {
63 // Read value from sensor.
64 int value = 0;
65 read_sysfs(make_sysfs_path(path,
66 i.first.first, i.first.second,
67 hwmon::entry::input),
68 value);
69
70 // Update sensor cache.
71 if (sensor_cache->update(i.first, value))
72 {
73 // TODO: Issue#4 - dbus event here.
74 std::cout << i.first.first << i.first.second << " = "
75 << value << std::endl;
76 }
77 }
78 }
79
80 // Sleep until next interval.
81 // TODO: Issue#5 - Make this configurable.
82 // TODO: Issue#6 - Optionally look at polling interval sysfs entry.
83 {
84 using namespace std::literals::chrono_literals;
85 std::this_thread::sleep_for(1s);
86 }
87
88 // TODO: Issue#7 - Should probably periodically check the SensorSet
89 // for new entries.
90 }
91
92 return 0;
93}
94