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