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