blob: 03cb0dab16ddc94946578d5fc08ae949545dcf87 [file] [log] [blame]
Matthew Barth279183f2021-05-25 10:19:43 -05001/**
2 * Copyright © 2021 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 "config.h"
17
18#include "dbus_zone.hpp"
19
20#include "sdbusplus.hpp"
21#include "zone.hpp"
22
23#include <cereal/archives/json.hpp>
24#include <cereal/cereal.hpp>
25#include <phosphor-logging/log.hpp>
26
27#include <algorithm>
28#include <filesystem>
29#include <fstream>
30
31namespace phosphor::fan::control::json
32{
33
34using namespace phosphor::logging;
35namespace fs = std::filesystem;
36
37DBusZone::DBusZone(const Zone& zone) :
38 ThermalModeIntf(util::SDBusPlus::getBus(),
39 (fs::path{CONTROL_OBJPATH} /= zone.getName()).c_str(),
40 true),
41 _zone(zone)
42{}
43
44std::string DBusZone::current(std::string value)
45{
46 auto current = ThermalModeIntf::current();
47 std::transform(value.begin(), value.end(), value.begin(), toupper);
48
49 auto supported = ThermalModeIntf::supported();
50 auto isSupported =
51 std::any_of(supported.begin(), supported.end(), [&value](auto& s) {
52 std::transform(s.begin(), s.end(), s.begin(), toupper);
53 return value == s;
54 });
55
56 if (isSupported && value != current)
57 {
58 current = ThermalModeIntf::current(value);
59 if (_zone.isPersisted(thermalModeIntf, currentProp))
60 {
61 saveCurrentMode();
62 }
63 }
64
65 return current;
66}
67
68void DBusZone::restoreCurrentMode()
69{
70 auto current = ThermalModeIntf::current();
71 fs::path path{CONTROL_PERSIST_ROOT_PATH};
72 // Append this object's name and property description
73 path /= _zone.getName();
74 path /= "CurrentMode";
75 fs::create_directories(path.parent_path());
76
77 try
78 {
79 if (fs::exists(path))
80 {
81 std::ifstream ifs(path.c_str(), std::ios::in | std::ios::binary);
82 cereal::JSONInputArchive iArch(ifs);
83 iArch(current);
84 }
85 }
86 catch (std::exception& e)
87 {
88 log<level::ERR>(e.what());
89 fs::remove(path);
90 current = ThermalModeIntf::current();
91 }
92
93 this->current(current);
94}
95
96void DBusZone::saveCurrentMode()
97{
98 fs::path path{CONTROL_PERSIST_ROOT_PATH};
99 // Append this object's name and property description
100 path /= _zone.getName();
101 path /= "CurrentMode";
102 std::ofstream ofs(path.c_str(), std::ios::binary);
103 cereal::JSONOutputArchive oArch(ofs);
104 oArch(ThermalModeIntf::current());
105}
106
107} // namespace phosphor::fan::control::json