blob: 0c6ed68fea8ff4f0b92dc6e1126772fe83de114e [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"
Patrick Venture5e929092018-06-08 10:55:23 -070024#include "sensors/builder.hpp"
25#include "sensors/manager.hpp"
26
27/*
28 * If there's a configuration file, we build from that, and it requires special
29 * parsing. I should just ditch the compile-time version to reduce the
30 * probability of sync bugs.
31 */
Patrick Venture7af157b2018-10-30 11:24:40 -070032SensorManager buildSensorsFromConfig(const std::string& path)
Patrick Venture5e929092018-06-08 10:55:23 -070033{
34 using namespace libconfig;
35
Patrick Venturef3252312018-10-30 08:42:53 -070036 std::map<std::string, struct SensorConfig> config;
Patrick Venture5e929092018-06-08 10:55:23 -070037 Config cfg;
38
39 std::cerr << "entered BuildSensorsFromConfig\n";
40
41 /* The load was modeled after the example source provided. */
42 try
43 {
44 cfg.readFile(path.c_str());
45 }
46 catch (const FileIOException& fioex)
47 {
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070048 std::cerr << "I/O error while reading file: " << fioex.what()
49 << std::endl;
Patrick Venture5e929092018-06-08 10:55:23 -070050 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;
Patrick Venturef3252312018-10-30 08:42:53 -070072 struct SensorConfig thisOne;
Patrick Venture5e929092018-06-08 10:55:23 -070073
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070074 /* Not a super fan of using this library for run-time configuration.
75 */
Patrick Venture5e929092018-06-08 10:55:23 -070076 name = sensor.lookup("name").c_str();
77 thisOne.type = sensor.lookup("type").c_str();
78 thisOne.readpath = sensor.lookup("readpath").c_str();
79 thisOne.writepath = sensor.lookup("writepath").c_str();
80
81 /* TODO: Document why this is wonky. The library probably doesn't
82 * like int64_t
83 */
84 int min = sensor.lookup("min");
85 thisOne.min = static_cast<int64_t>(min);
86 int max = sensor.lookup("max");
87 thisOne.max = static_cast<int64_t>(max);
88 int timeout = sensor.lookup("timeout");
89 thisOne.timeout = static_cast<int64_t>(timeout);
90
91 // leaving for verification for now. and yea the above is
92 // necessary.
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070093 std::cerr << "min: " << min << " max: " << max
94 << " savedmin: " << thisOne.min
95 << " savedmax: " << thisOne.max
96 << " timeout: " << thisOne.timeout << std::endl;
Patrick Venture5e929092018-06-08 10:55:23 -070097
98 config[name] = thisOne;
99 }
100 }
Patrick Ventureda4a5dd2018-08-31 09:42:48 -0700101 catch (const SettingTypeException& setex)
Patrick Venture5e929092018-06-08 10:55:23 -0700102 {
Patrick Ventureda4a5dd2018-08-31 09:42:48 -0700103 std::cerr << "Setting '" << setex.getPath() << "' type exception!"
104 << std::endl;
Patrick Venture5e929092018-06-08 10:55:23 -0700105 throw;
106 }
107 catch (const SettingNotFoundException& snex)
108 {
109 std::cerr << "Setting not found!" << std::endl;
110 throw;
111 }
112
Patrick Venture7af157b2018-10-30 11:24:40 -0700113 return buildSensors(config);
Patrick Venture5e929092018-06-08 10:55:23 -0700114}