blob: b3507aedc4698a6c45a81914f6c8ccd741f8907a [file] [log] [blame]
Patrick Venture5e929092018-06-08 10:55:23 -07001/**
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 <iostream>
18#include <libconfig.h++>
19
20/* Configuration. */
21#include "conf.hpp"
22
23#include "sensors/builder.hpp"
24#include "sensors/manager.hpp"
25
26/*
27 * If there's a configuration file, we build from that, and it requires special
28 * parsing. I should just ditch the compile-time version to reduce the
29 * probability of sync bugs.
30 */
31std::shared_ptr<SensorManager> BuildSensorsFromConfig(const std::string& path)
32{
33 using namespace libconfig;
34
35 std::map<std::string, struct sensor> config;
36 Config cfg;
37
38 std::cerr << "entered BuildSensorsFromConfig\n";
39
40 /* The load was modeled after the example source provided. */
41 try
42 {
43 cfg.readFile(path.c_str());
44 }
45 catch (const FileIOException& fioex)
46 {
47 std::cerr << "I/O error while reading file: " << fioex.what() << std::endl;
48 throw;
49 }
50 catch (const ParseException& pex)
51 {
52 std::cerr << "Parse error at " << pex.getFile() << ":" << pex.getLine()
53 << " - " << pex.getError() << std::endl;
54 throw;
55 }
56
57 try
58 {
59 const Setting& root = cfg.getRoot();
60
61 /* Grab the list of sensors and create them all */
62 const Setting& sensors = root["sensors"];
63 int count = sensors.getLength();
64
65 for (int i = 0; i < count; ++i)
66 {
67 const Setting& sensor = sensors[i];
68
69 std::string name;
70 struct sensor thisOne;
71
72 /* Not a super fan of using this library for run-time configuration. */
73 name = sensor.lookup("name").c_str();
74 thisOne.type = sensor.lookup("type").c_str();
75 thisOne.readpath = sensor.lookup("readpath").c_str();
76 thisOne.writepath = sensor.lookup("writepath").c_str();
77
78 /* TODO: Document why this is wonky. The library probably doesn't
79 * like int64_t
80 */
81 int min = sensor.lookup("min");
82 thisOne.min = static_cast<int64_t>(min);
83 int max = sensor.lookup("max");
84 thisOne.max = static_cast<int64_t>(max);
85 int timeout = sensor.lookup("timeout");
86 thisOne.timeout = static_cast<int64_t>(timeout);
87
88 // leaving for verification for now. and yea the above is
89 // necessary.
90 std::cerr << "min: " << min
91 << " max: " << max
92 << " savedmin: " << thisOne.min
93 << " savedmax: " << thisOne.max
94 << " timeout: " << thisOne.timeout
95 << std::endl;
96
97 config[name] = thisOne;
98 }
99 }
100 catch (const SettingTypeException &setex)
101 {
102 std::cerr << "Setting '" << setex.getPath()
103 << "' type exception!" << std::endl;
104 throw;
105 }
106 catch (const SettingNotFoundException& snex)
107 {
108 std::cerr << "Setting not found!" << std::endl;
109 throw;
110 }
111
112 return BuildSensors(config);
113}
114