1 /**
2 * Copyright 2019 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 "sensors/buildjson.hpp"
18
19 #include "conf.hpp"
20 #include "sensors/sensor.hpp"
21
22 #include <nlohmann/json.hpp>
23
24 #include <cstdio>
25
26 using json = nlohmann::json;
27
28 namespace pid_control
29 {
30 namespace conf
31 {
from_json(const json & j,conf::SensorConfig & s)32 void from_json(const json& j, conf::SensorConfig& s)
33 {
34 j.at("type").get_to(s.type);
35 j.at("readPath").get_to(s.readPath);
36
37 /* The writePath field is optional in a configuration */
38 auto writePath = j.find("writePath");
39 if (writePath == j.end())
40 {
41 s.writePath = "";
42 }
43 else
44 {
45 j.at("writePath").get_to(s.writePath);
46 }
47
48 /* Default to not ignore dbus MinValue/MaxValue - only used by passive
49 * sensors.
50 */
51 s.ignoreDbusMinMax = false;
52 s.unavailableAsFailed = true;
53 s.min = 0;
54 s.max = 0;
55
56 auto ignore = j.find("ignoreDbusMinMax");
57 if (ignore != j.end())
58 {
59 j.at("ignoreDbusMinMax").get_to(s.ignoreDbusMinMax);
60 }
61
62 auto findunAsF = j.find("unavailableAsFailed");
63 if (findunAsF != j.end())
64 {
65 j.at("unavailableAsFailed").get_to(s.unavailableAsFailed);
66 }
67
68 /* The min field is optional in a configuration. */
69 auto min = j.find("min");
70 if (min != j.end())
71 {
72 if (s.type == "fan")
73 {
74 j.at("min").get_to(s.min);
75 }
76 else
77 {
78 std::fprintf(stderr, "Non-fan types ignore min value specified\n");
79 }
80 }
81
82 /* The max field is optional in a configuration. */
83 auto max = j.find("max");
84 if (max != j.end())
85 {
86 if (s.type == "fan")
87 {
88 j.at("max").get_to(s.max);
89 }
90 else
91 {
92 std::fprintf(stderr, "Non-fan types ignore max value specified\n");
93 }
94 }
95
96 /* The timeout field is optional in a configuration. */
97 auto timeout = j.find("timeout");
98 if (timeout == j.end())
99 {
100 s.timeout = Sensor::getDefaultTimeout(s.type);
101 }
102 else
103 {
104 j.at("timeout").get_to(s.timeout);
105 }
106 }
107 } // namespace conf
108
buildSensorsFromJson(const json & data)109 std::map<std::string, conf::SensorConfig> buildSensorsFromJson(const json& data)
110 {
111 std::map<std::string, conf::SensorConfig> config;
112 auto sensors = data["sensors"];
113
114 /* TODO: If no sensors, this is invalid, and we should except here or during
115 * parsing.
116 */
117 for (const auto& sensor : sensors)
118 {
119 config[sensor["name"]] = sensor.get<conf::SensorConfig>();
120 }
121
122 return config;
123 }
124 } // namespace pid_control
125