blob: c5ef849f5d571162ea532d70da420c613d302f5a [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;
Patrick Venturec7ab57e2019-08-29 09:35:19 -070054 s.min = 0;
55 s.max = 0;
56
Patrick Venture6b9f5992019-09-10 09:18:28 -070057 auto ignore = j.find("ignoreDbusMinMax");
58 if (ignore != j.end())
59 {
60 j.at("ignoreDbusMinMax").get_to(s.ignoreDbusMinMax);
61 }
62
Alex.Song8f73ad72021-10-07 00:18:27 +080063 auto findunAsF = j.find("unavailableAsFailed");
64 if (findunAsF != j.end())
65 {
66 j.at("unavailableAsFailed").get_to(s.unavailableAsFailed);
67 }
68
Patrick Ventureeeeb8672019-02-08 11:47:42 -080069 /* The min field is optional in a configuration. */
70 auto min = j.find("min");
Patrick Venturec7ab57e2019-08-29 09:35:19 -070071 if (min != j.end())
Patrick Ventureeeeb8672019-02-08 11:47:42 -080072 {
Patrick Venture35906cc2019-08-29 10:06:29 -070073 if (s.type == "fan")
74 {
75 j.at("min").get_to(s.min);
76 }
77 else
78 {
79 std::fprintf(stderr, "Non-fan types ignore min value specified\n");
80 }
Patrick Ventureeeeb8672019-02-08 11:47:42 -080081 }
82
83 /* The max field is optional in a configuration. */
84 auto max = j.find("max");
Patrick Venturec7ab57e2019-08-29 09:35:19 -070085 if (max != j.end())
Patrick Ventureeeeb8672019-02-08 11:47:42 -080086 {
Patrick Venture35906cc2019-08-29 10:06:29 -070087 if (s.type == "fan")
88 {
89 j.at("max").get_to(s.max);
90 }
91 else
92 {
93 std::fprintf(stderr, "Non-fan types ignore max value specified\n");
94 }
Patrick Ventureeeeb8672019-02-08 11:47:42 -080095 }
96
97 /* The timeout field is optional in a configuration. */
98 auto timeout = j.find("timeout");
99 if (timeout == j.end())
100 {
101 s.timeout = Sensor::getDefaultTimeout(s.type);
102 }
103 else
104 {
105 j.at("timeout").get_to(s.timeout);
106 }
107}
James Feistf81f2882019-02-26 11:26:36 -0800108} // namespace conf
Patrick Ventureeeeb8672019-02-08 11:47:42 -0800109
Patrick Venture1df9e872020-10-08 15:35:01 -0700110std::map<std::string, conf::SensorConfig> buildSensorsFromJson(const json& data)
Patrick Ventureeeeb8672019-02-08 11:47:42 -0800111{
Patrick Venture1df9e872020-10-08 15:35:01 -0700112 std::map<std::string, conf::SensorConfig> config;
Patrick Ventureeeeb8672019-02-08 11:47:42 -0800113 auto sensors = data["sensors"];
114
Patrick Venture6f59cf22019-02-08 14:59:44 -0800115 /* TODO: If no sensors, this is invalid, and we should except here or during
116 * parsing.
117 */
Patrick Ventureeeeb8672019-02-08 11:47:42 -0800118 for (const auto& sensor : sensors)
119 {
Patrick Venture1df9e872020-10-08 15:35:01 -0700120 config[sensor["name"]] = sensor.get<conf::SensorConfig>();
Patrick Ventureeeeb8672019-02-08 11:47:42 -0800121 }
122
123 return config;
124}
Patrick Venturea0764872020-08-08 07:48:43 -0700125} // namespace pid_control