Matthew Barth | fcfa052 | 2020-08-24 16:40:24 -0500 | [diff] [blame] | 1 | /** |
| 2 | * Copyright © 2020 IBM Corporation |
| 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 | #include "fan.hpp" |
| 17 | |
| 18 | #include <nlohmann/json.hpp> |
Matthew Barth | bff1080 | 2020-08-25 10:07:57 -0500 | [diff] [blame^] | 19 | #include <phosphor-logging/log.hpp> |
Matthew Barth | fcfa052 | 2020-08-24 16:40:24 -0500 | [diff] [blame] | 20 | #include <sdbusplus/bus.hpp> |
| 21 | |
| 22 | namespace phosphor::fan::control::json |
| 23 | { |
| 24 | |
| 25 | using json = nlohmann::json; |
Matthew Barth | bff1080 | 2020-08-25 10:07:57 -0500 | [diff] [blame^] | 26 | using namespace phosphor::logging; |
Matthew Barth | fcfa052 | 2020-08-24 16:40:24 -0500 | [diff] [blame] | 27 | |
| 28 | Fan::Fan(sdbusplus::bus::bus& bus, const json& jsonObj) : |
| 29 | ConfigBase(jsonObj), _bus(bus) |
Matthew Barth | bff1080 | 2020-08-25 10:07:57 -0500 | [diff] [blame^] | 30 | { |
| 31 | setProfiles(jsonObj); |
| 32 | setZone(jsonObj); |
| 33 | setSensors(jsonObj); |
| 34 | setInterface(jsonObj); |
| 35 | } |
| 36 | |
| 37 | void Fan::setProfiles(const json& jsonObj) |
| 38 | { |
| 39 | _profiles = {}; |
| 40 | if (jsonObj.contains("profiles")) |
| 41 | { |
| 42 | for (const auto& profile : jsonObj["profiles"]) |
| 43 | { |
| 44 | _profiles.emplace_back(profile.get<std::string>()); |
| 45 | } |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | void Fan::setZone(const json& jsonObj) |
| 50 | { |
| 51 | if (!jsonObj.contains("zone")) |
| 52 | { |
| 53 | log<level::ERR>("Missing required fan zone", |
| 54 | entry("JSON=%s", jsonObj.dump().c_str())); |
| 55 | throw std::runtime_error("Missing required fan zone"); |
| 56 | } |
| 57 | _zone = jsonObj["zone"].get<std::string>(); |
| 58 | } |
| 59 | |
| 60 | void Fan::setSensors(const json& jsonObj) |
| 61 | { |
| 62 | if (!jsonObj.contains("sensors")) |
| 63 | { |
| 64 | log<level::ERR>("Missing required fan sensors list", |
| 65 | entry("JSON=%s", jsonObj.dump().c_str())); |
| 66 | throw std::runtime_error("Missing required fan sensors list"); |
| 67 | } |
| 68 | for (const auto& sensor : jsonObj["sensors"]) |
| 69 | { |
| 70 | _sensors.emplace_back(sensor.get<std::string>()); |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | void Fan::setInterface(const json& jsonObj) |
| 75 | { |
| 76 | if (!jsonObj.contains("target_interface")) |
| 77 | { |
| 78 | log<level::ERR>("Missing required fan sensor target interface", |
| 79 | entry("JSON=%s", jsonObj.dump().c_str())); |
| 80 | throw std::runtime_error( |
| 81 | "Missing required fan sensor target interface"); |
| 82 | } |
| 83 | _interface = jsonObj["target_interface"].get<std::string>(); |
| 84 | } |
Matthew Barth | fcfa052 | 2020-08-24 16:40:24 -0500 | [diff] [blame] | 85 | |
| 86 | } // namespace phosphor::fan::control::json |