xref: /openbmc/phosphor-pid-control/sensors/buildjson.cpp (revision 46a755fce8dc0bdd9c0c5ea09d55d3e5494f335f)
1 // SPDX-License-Identifier: Apache-2.0
2 // SPDX-FileCopyrightText: Copyright 2019 Google Inc
3 
4 #include "sensors/buildjson.hpp"
5 
6 #include "conf.hpp"
7 #include "sensors/sensor.hpp"
8 
9 #include <nlohmann/json.hpp>
10 
11 #include <cstdio>
12 #include <map>
13 
14 using json = nlohmann::json;
15 
16 namespace pid_control
17 {
18 namespace conf
19 {
from_json(const json & j,conf::SensorConfig & s)20 void from_json(const json& j, conf::SensorConfig& s)
21 {
22     j.at("type").get_to(s.type);
23     j.at("readPath").get_to(s.readPath);
24 
25     /* The writePath field is optional in a configuration */
26     auto writePath = j.find("writePath");
27     if (writePath == j.end())
28     {
29         s.writePath = "";
30     }
31     else
32     {
33         j.at("writePath").get_to(s.writePath);
34     }
35 
36     /* Default to not ignore dbus MinValue/MaxValue - only used by passive
37      * sensors.
38      */
39     s.ignoreDbusMinMax = false;
40     s.unavailableAsFailed = true;
41     s.ignoreFailIfHostOff = false;
42     s.min = 0;
43     s.max = 0;
44 
45     auto ignore = j.find("ignoreDbusMinMax");
46     if (ignore != j.end())
47     {
48         j.at("ignoreDbusMinMax").get_to(s.ignoreDbusMinMax);
49     }
50 
51     auto findunAsF = j.find("unavailableAsFailed");
52     if (findunAsF != j.end())
53     {
54         j.at("unavailableAsFailed").get_to(s.unavailableAsFailed);
55     }
56 
57     auto findIgnoreIfHostOff = j.find("ignoreFailIfHostOff");
58     if (findIgnoreIfHostOff != j.end())
59     {
60         j.at("ignoreFailIfHostOff").get_to(s.ignoreFailIfHostOff);
61     }
62 
63     /* The min field is optional in a configuration. */
64     auto min = j.find("min");
65     if (min != j.end())
66     {
67         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         }
75     }
76 
77     /* The max field is optional in a configuration. */
78     auto max = j.find("max");
79     if (max != j.end())
80     {
81         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         }
89     }
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 }
102 } // namespace conf
103 
buildSensorsFromJson(const json & data)104 std::map<std::string, conf::SensorConfig> buildSensorsFromJson(const json& data)
105 {
106     std::map<std::string, conf::SensorConfig> config;
107     auto sensors = data["sensors"];
108 
109     /* TODO: If no sensors, this is invalid, and we should except here or during
110      * parsing.
111      */
112     for (const auto& sensor : sensors)
113     {
114         config[sensor["name"]] = sensor.get<conf::SensorConfig>();
115     }
116 
117     return config;
118 }
119 } // namespace pid_control
120