blob: ed6e35e2380355f47cd184a49d35d927247daa24 [file] [log] [blame]
Patrick Ventureeeeb8672019-02-08 11:47:42 -08001/**
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
Patrick Venturea83a3ec2020-08-04 09:52:05 -070024#include <cstdio>
Ed Tanousf8b6e552025-06-27 13:27:50 -070025#include <map>
Patrick Venturea83a3ec2020-08-04 09:52:05 -070026
Patrick Ventureeeeb8672019-02-08 11:47:42 -080027using json = nlohmann::json;
28
Patrick Venturea0764872020-08-08 07:48:43 -070029namespace pid_control
30{
James Feistf81f2882019-02-26 11:26:36 -080031namespace conf
32{
33void from_json(const json& j, conf::SensorConfig& s)
Patrick Ventureeeeb8672019-02-08 11:47:42 -080034{
35 j.at("type").get_to(s.type);
Patrick Venture69c51062019-02-11 09:46:03 -080036 j.at("readPath").get_to(s.readPath);
Patrick Ventureeeeb8672019-02-08 11:47:42 -080037
Patrick Venture69c51062019-02-11 09:46:03 -080038 /* The writePath field is optional in a configuration */
39 auto writePath = j.find("writePath");
40 if (writePath == j.end())
Patrick Ventureeeeb8672019-02-08 11:47:42 -080041 {
Patrick Venture69c51062019-02-11 09:46:03 -080042 s.writePath = "";
Patrick Ventureeeeb8672019-02-08 11:47:42 -080043 }
44 else
45 {
Patrick Venture69c51062019-02-11 09:46:03 -080046 j.at("writePath").get_to(s.writePath);
Patrick Ventureeeeb8672019-02-08 11:47:42 -080047 }
48
Patrick Venture6b9f5992019-09-10 09:18:28 -070049 /* Default to not ignore dbus MinValue/MaxValue - only used by passive
50 * sensors.
51 */
52 s.ignoreDbusMinMax = false;
Alex.Song8f73ad72021-10-07 00:18:27 +080053 s.unavailableAsFailed = true;
Potin Laie1fa8592025-08-29 15:27:08 +080054 s.ignoreFailIfHostOff = false;
Patrick Venturec7ab57e2019-08-29 09:35:19 -070055 s.min = 0;
56 s.max = 0;
57
Patrick Venture6b9f5992019-09-10 09:18:28 -070058 auto ignore = j.find("ignoreDbusMinMax");
59 if (ignore != j.end())
60 {
61 j.at("ignoreDbusMinMax").get_to(s.ignoreDbusMinMax);
62 }
63
Alex.Song8f73ad72021-10-07 00:18:27 +080064 auto findunAsF = j.find("unavailableAsFailed");
65 if (findunAsF != j.end())
66 {
67 j.at("unavailableAsFailed").get_to(s.unavailableAsFailed);
68 }
69
Potin Laie1fa8592025-08-29 15:27:08 +080070 auto findIgnoreIfHostOff = j.find("ignoreFailIfHostOff");
71 if (findIgnoreIfHostOff != j.end())
72 {
73 j.at("ignoreFailIfHostOff").get_to(s.ignoreFailIfHostOff);
74 }
75
Patrick Ventureeeeb8672019-02-08 11:47:42 -080076 /* The min field is optional in a configuration. */
77 auto min = j.find("min");
Patrick Venturec7ab57e2019-08-29 09:35:19 -070078 if (min != j.end())
Patrick Ventureeeeb8672019-02-08 11:47:42 -080079 {
Patrick Venture35906cc2019-08-29 10:06:29 -070080 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 }
Patrick Ventureeeeb8672019-02-08 11:47:42 -080088 }
89
90 /* The max field is optional in a configuration. */
91 auto max = j.find("max");
Patrick Venturec7ab57e2019-08-29 09:35:19 -070092 if (max != j.end())
Patrick Ventureeeeb8672019-02-08 11:47:42 -080093 {
Patrick Venture35906cc2019-08-29 10:06:29 -070094 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 }
Patrick Ventureeeeb8672019-02-08 11:47:42 -0800102 }
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}
James Feistf81f2882019-02-26 11:26:36 -0800115} // namespace conf
Patrick Ventureeeeb8672019-02-08 11:47:42 -0800116
Patrick Venture1df9e872020-10-08 15:35:01 -0700117std::map<std::string, conf::SensorConfig> buildSensorsFromJson(const json& data)
Patrick Ventureeeeb8672019-02-08 11:47:42 -0800118{
Patrick Venture1df9e872020-10-08 15:35:01 -0700119 std::map<std::string, conf::SensorConfig> config;
Patrick Ventureeeeb8672019-02-08 11:47:42 -0800120 auto sensors = data["sensors"];
121
Patrick Venture6f59cf22019-02-08 14:59:44 -0800122 /* TODO: If no sensors, this is invalid, and we should except here or during
123 * parsing.
124 */
Patrick Ventureeeeb8672019-02-08 11:47:42 -0800125 for (const auto& sensor : sensors)
126 {
Patrick Venture1df9e872020-10-08 15:35:01 -0700127 config[sensor["name"]] = sensor.get<conf::SensorConfig>();
Patrick Ventureeeeb8672019-02-08 11:47:42 -0800128 }
129
130 return config;
131}
Patrick Venturea0764872020-08-08 07:48:43 -0700132} // namespace pid_control