blob: 23bb201da3606c4496a7ed93b27312ded6ea6869 [file] [log] [blame]
Alexander Hansen46a755f2025-10-27 16:31:08 +01001// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright 2019 Google Inc
Patrick Ventureeeeb8672019-02-08 11:47:42 -08003
4#include "sensors/buildjson.hpp"
5
6#include "conf.hpp"
7#include "sensors/sensor.hpp"
8
9#include <nlohmann/json.hpp>
10
Patrick Venturea83a3ec2020-08-04 09:52:05 -070011#include <cstdio>
Ed Tanousf8b6e552025-06-27 13:27:50 -070012#include <map>
Patrick Venturea83a3ec2020-08-04 09:52:05 -070013
Patrick Ventureeeeb8672019-02-08 11:47:42 -080014using json = nlohmann::json;
15
Patrick Venturea0764872020-08-08 07:48:43 -070016namespace pid_control
17{
James Feistf81f2882019-02-26 11:26:36 -080018namespace conf
19{
20void from_json(const json& j, conf::SensorConfig& s)
Patrick Ventureeeeb8672019-02-08 11:47:42 -080021{
22 j.at("type").get_to(s.type);
Patrick Venture69c51062019-02-11 09:46:03 -080023 j.at("readPath").get_to(s.readPath);
Patrick Ventureeeeb8672019-02-08 11:47:42 -080024
Patrick Venture69c51062019-02-11 09:46:03 -080025 /* The writePath field is optional in a configuration */
26 auto writePath = j.find("writePath");
27 if (writePath == j.end())
Patrick Ventureeeeb8672019-02-08 11:47:42 -080028 {
Patrick Venture69c51062019-02-11 09:46:03 -080029 s.writePath = "";
Patrick Ventureeeeb8672019-02-08 11:47:42 -080030 }
31 else
32 {
Patrick Venture69c51062019-02-11 09:46:03 -080033 j.at("writePath").get_to(s.writePath);
Patrick Ventureeeeb8672019-02-08 11:47:42 -080034 }
35
Patrick Venture6b9f5992019-09-10 09:18:28 -070036 /* Default to not ignore dbus MinValue/MaxValue - only used by passive
37 * sensors.
38 */
39 s.ignoreDbusMinMax = false;
Alex.Song8f73ad72021-10-07 00:18:27 +080040 s.unavailableAsFailed = true;
Potin Laie1fa8592025-08-29 15:27:08 +080041 s.ignoreFailIfHostOff = false;
Patrick Venturec7ab57e2019-08-29 09:35:19 -070042 s.min = 0;
43 s.max = 0;
44
Patrick Venture6b9f5992019-09-10 09:18:28 -070045 auto ignore = j.find("ignoreDbusMinMax");
46 if (ignore != j.end())
47 {
48 j.at("ignoreDbusMinMax").get_to(s.ignoreDbusMinMax);
49 }
50
Alex.Song8f73ad72021-10-07 00:18:27 +080051 auto findunAsF = j.find("unavailableAsFailed");
52 if (findunAsF != j.end())
53 {
54 j.at("unavailableAsFailed").get_to(s.unavailableAsFailed);
55 }
56
Potin Laie1fa8592025-08-29 15:27:08 +080057 auto findIgnoreIfHostOff = j.find("ignoreFailIfHostOff");
58 if (findIgnoreIfHostOff != j.end())
59 {
60 j.at("ignoreFailIfHostOff").get_to(s.ignoreFailIfHostOff);
61 }
62
Patrick Ventureeeeb8672019-02-08 11:47:42 -080063 /* The min field is optional in a configuration. */
64 auto min = j.find("min");
Patrick Venturec7ab57e2019-08-29 09:35:19 -070065 if (min != j.end())
Patrick Ventureeeeb8672019-02-08 11:47:42 -080066 {
Patrick Venture35906cc2019-08-29 10:06:29 -070067 if (s.type == "fan")
68 {
69 j.at("min").get_to(s.min);
70 }
71 else
72 {
73 std::fprintf(stderr, "Non-fan types ignore min value specified\n");
74 }
Patrick Ventureeeeb8672019-02-08 11:47:42 -080075 }
76
77 /* The max field is optional in a configuration. */
78 auto max = j.find("max");
Patrick Venturec7ab57e2019-08-29 09:35:19 -070079 if (max != j.end())
Patrick Ventureeeeb8672019-02-08 11:47:42 -080080 {
Patrick Venture35906cc2019-08-29 10:06:29 -070081 if (s.type == "fan")
82 {
83 j.at("max").get_to(s.max);
84 }
85 else
86 {
87 std::fprintf(stderr, "Non-fan types ignore max value specified\n");
88 }
Patrick Ventureeeeb8672019-02-08 11:47:42 -080089 }
90
91 /* The timeout field is optional in a configuration. */
92 auto timeout = j.find("timeout");
93 if (timeout == j.end())
94 {
95 s.timeout = Sensor::getDefaultTimeout(s.type);
96 }
97 else
98 {
99 j.at("timeout").get_to(s.timeout);
100 }
101}
James Feistf81f2882019-02-26 11:26:36 -0800102} // namespace conf
Patrick Ventureeeeb8672019-02-08 11:47:42 -0800103
Patrick Venture1df9e872020-10-08 15:35:01 -0700104std::map<std::string, conf::SensorConfig> buildSensorsFromJson(const json& data)
Patrick Ventureeeeb8672019-02-08 11:47:42 -0800105{
Patrick Venture1df9e872020-10-08 15:35:01 -0700106 std::map<std::string, conf::SensorConfig> config;
Patrick Ventureeeeb8672019-02-08 11:47:42 -0800107 auto sensors = data["sensors"];
108
Patrick Venture6f59cf22019-02-08 14:59:44 -0800109 /* TODO: If no sensors, this is invalid, and we should except here or during
110 * parsing.
111 */
Patrick Ventureeeeb8672019-02-08 11:47:42 -0800112 for (const auto& sensor : sensors)
113 {
Patrick Venture1df9e872020-10-08 15:35:01 -0700114 config[sensor["name"]] = sensor.get<conf::SensorConfig>();
Patrick Ventureeeeb8672019-02-08 11:47:42 -0800115 }
116
117 return config;
118}
Patrick Venturea0764872020-08-08 07:48:43 -0700119} // namespace pid_control