| Alexander Hansen | 46a755f | 2025-10-27 16:31:08 +0100 | [diff] [blame^] | 1 | // SPDX-License-Identifier: Apache-2.0 |
| 2 | // SPDX-FileCopyrightText: Copyright 2019 Google Inc |
| Patrick Venture | eeeb867 | 2019-02-08 11:47:42 -0800 | [diff] [blame] | 3 | |
| 4 | #include "sensors/buildjson.hpp" |
| 5 | |
| 6 | #include "conf.hpp" |
| 7 | #include "sensors/sensor.hpp" |
| 8 | |
| 9 | #include <nlohmann/json.hpp> |
| 10 | |
| Patrick Venture | a83a3ec | 2020-08-04 09:52:05 -0700 | [diff] [blame] | 11 | #include <cstdio> |
| Ed Tanous | f8b6e55 | 2025-06-27 13:27:50 -0700 | [diff] [blame] | 12 | #include <map> |
| Patrick Venture | a83a3ec | 2020-08-04 09:52:05 -0700 | [diff] [blame] | 13 | |
| Patrick Venture | eeeb867 | 2019-02-08 11:47:42 -0800 | [diff] [blame] | 14 | using json = nlohmann::json; |
| 15 | |
| Patrick Venture | a076487 | 2020-08-08 07:48:43 -0700 | [diff] [blame] | 16 | namespace pid_control |
| 17 | { |
| James Feist | f81f288 | 2019-02-26 11:26:36 -0800 | [diff] [blame] | 18 | namespace conf |
| 19 | { |
| 20 | void from_json(const json& j, conf::SensorConfig& s) |
| Patrick Venture | eeeb867 | 2019-02-08 11:47:42 -0800 | [diff] [blame] | 21 | { |
| 22 | j.at("type").get_to(s.type); |
| Patrick Venture | 69c5106 | 2019-02-11 09:46:03 -0800 | [diff] [blame] | 23 | j.at("readPath").get_to(s.readPath); |
| Patrick Venture | eeeb867 | 2019-02-08 11:47:42 -0800 | [diff] [blame] | 24 | |
| Patrick Venture | 69c5106 | 2019-02-11 09:46:03 -0800 | [diff] [blame] | 25 | /* The writePath field is optional in a configuration */ |
| 26 | auto writePath = j.find("writePath"); |
| 27 | if (writePath == j.end()) |
| Patrick Venture | eeeb867 | 2019-02-08 11:47:42 -0800 | [diff] [blame] | 28 | { |
| Patrick Venture | 69c5106 | 2019-02-11 09:46:03 -0800 | [diff] [blame] | 29 | s.writePath = ""; |
| Patrick Venture | eeeb867 | 2019-02-08 11:47:42 -0800 | [diff] [blame] | 30 | } |
| 31 | else |
| 32 | { |
| Patrick Venture | 69c5106 | 2019-02-11 09:46:03 -0800 | [diff] [blame] | 33 | j.at("writePath").get_to(s.writePath); |
| Patrick Venture | eeeb867 | 2019-02-08 11:47:42 -0800 | [diff] [blame] | 34 | } |
| 35 | |
| Patrick Venture | 6b9f599 | 2019-09-10 09:18:28 -0700 | [diff] [blame] | 36 | /* Default to not ignore dbus MinValue/MaxValue - only used by passive |
| 37 | * sensors. |
| 38 | */ |
| 39 | s.ignoreDbusMinMax = false; |
| Alex.Song | 8f73ad7 | 2021-10-07 00:18:27 +0800 | [diff] [blame] | 40 | s.unavailableAsFailed = true; |
| Potin Lai | e1fa859 | 2025-08-29 15:27:08 +0800 | [diff] [blame] | 41 | s.ignoreFailIfHostOff = false; |
| Patrick Venture | c7ab57e | 2019-08-29 09:35:19 -0700 | [diff] [blame] | 42 | s.min = 0; |
| 43 | s.max = 0; |
| 44 | |
| Patrick Venture | 6b9f599 | 2019-09-10 09:18:28 -0700 | [diff] [blame] | 45 | auto ignore = j.find("ignoreDbusMinMax"); |
| 46 | if (ignore != j.end()) |
| 47 | { |
| 48 | j.at("ignoreDbusMinMax").get_to(s.ignoreDbusMinMax); |
| 49 | } |
| 50 | |
| Alex.Song | 8f73ad7 | 2021-10-07 00:18:27 +0800 | [diff] [blame] | 51 | auto findunAsF = j.find("unavailableAsFailed"); |
| 52 | if (findunAsF != j.end()) |
| 53 | { |
| 54 | j.at("unavailableAsFailed").get_to(s.unavailableAsFailed); |
| 55 | } |
| 56 | |
| Potin Lai | e1fa859 | 2025-08-29 15:27:08 +0800 | [diff] [blame] | 57 | auto findIgnoreIfHostOff = j.find("ignoreFailIfHostOff"); |
| 58 | if (findIgnoreIfHostOff != j.end()) |
| 59 | { |
| 60 | j.at("ignoreFailIfHostOff").get_to(s.ignoreFailIfHostOff); |
| 61 | } |
| 62 | |
| Patrick Venture | eeeb867 | 2019-02-08 11:47:42 -0800 | [diff] [blame] | 63 | /* The min field is optional in a configuration. */ |
| 64 | auto min = j.find("min"); |
| Patrick Venture | c7ab57e | 2019-08-29 09:35:19 -0700 | [diff] [blame] | 65 | if (min != j.end()) |
| Patrick Venture | eeeb867 | 2019-02-08 11:47:42 -0800 | [diff] [blame] | 66 | { |
| Patrick Venture | 35906cc | 2019-08-29 10:06:29 -0700 | [diff] [blame] | 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 | } |
| Patrick Venture | eeeb867 | 2019-02-08 11:47:42 -0800 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | /* The max field is optional in a configuration. */ |
| 78 | auto max = j.find("max"); |
| Patrick Venture | c7ab57e | 2019-08-29 09:35:19 -0700 | [diff] [blame] | 79 | if (max != j.end()) |
| Patrick Venture | eeeb867 | 2019-02-08 11:47:42 -0800 | [diff] [blame] | 80 | { |
| Patrick Venture | 35906cc | 2019-08-29 10:06:29 -0700 | [diff] [blame] | 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 | } |
| Patrick Venture | eeeb867 | 2019-02-08 11:47:42 -0800 | [diff] [blame] | 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 | } |
| James Feist | f81f288 | 2019-02-26 11:26:36 -0800 | [diff] [blame] | 102 | } // namespace conf |
| Patrick Venture | eeeb867 | 2019-02-08 11:47:42 -0800 | [diff] [blame] | 103 | |
| Patrick Venture | 1df9e87 | 2020-10-08 15:35:01 -0700 | [diff] [blame] | 104 | std::map<std::string, conf::SensorConfig> buildSensorsFromJson(const json& data) |
| Patrick Venture | eeeb867 | 2019-02-08 11:47:42 -0800 | [diff] [blame] | 105 | { |
| Patrick Venture | 1df9e87 | 2020-10-08 15:35:01 -0700 | [diff] [blame] | 106 | std::map<std::string, conf::SensorConfig> config; |
| Patrick Venture | eeeb867 | 2019-02-08 11:47:42 -0800 | [diff] [blame] | 107 | auto sensors = data["sensors"]; |
| 108 | |
| Patrick Venture | 6f59cf2 | 2019-02-08 14:59:44 -0800 | [diff] [blame] | 109 | /* TODO: If no sensors, this is invalid, and we should except here or during |
| 110 | * parsing. |
| 111 | */ |
| Patrick Venture | eeeb867 | 2019-02-08 11:47:42 -0800 | [diff] [blame] | 112 | for (const auto& sensor : sensors) |
| 113 | { |
| Patrick Venture | 1df9e87 | 2020-10-08 15:35:01 -0700 | [diff] [blame] | 114 | config[sensor["name"]] = sensor.get<conf::SensorConfig>(); |
| Patrick Venture | eeeb867 | 2019-02-08 11:47:42 -0800 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | return config; |
| 118 | } |
| Patrick Venture | a076487 | 2020-08-08 07:48:43 -0700 | [diff] [blame] | 119 | } // namespace pid_control |