blob: 733fb944fff255d53f68f98195de74867c1386dc [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
Mike Cappsbf8e56f2022-06-29 14:23:07 -040020#include "dbus_paths.hpp"
Matthew Barth279183f2021-05-25 10:19:43 -050021#include "sdbusplus.hpp"
22#include "zone.hpp"
23
Matthew Barth70722372021-06-15 13:50:01 -050024#include <fmt/format.h>
25
Matthew Barth279183f2021-05-25 10:19:43 -050026#include <cereal/archives/json.hpp>
27#include <cereal/cereal.hpp>
28#include <phosphor-logging/log.hpp>
29
30#include <algorithm>
31#include <filesystem>
32#include <fstream>
33
34namespace phosphor::fan::control::json
35{
36
37using namespace phosphor::logging;
38namespace fs = std::filesystem;
39
40DBusZone::DBusZone(const Zone& zone) :
41 ThermalModeIntf(util::SDBusPlus::getBus(),
42 (fs::path{CONTROL_OBJPATH} /= zone.getName()).c_str(),
Patrick Williamsa3ed9b02022-04-05 16:24:52 -050043 ThermalModeIntf::action::defer_emit),
Matthew Barth279183f2021-05-25 10:19:43 -050044 _zone(zone)
45{}
46
47std::string DBusZone::current(std::string value)
48{
49 auto current = ThermalModeIntf::current();
50 std::transform(value.begin(), value.end(), value.begin(), toupper);
51
52 auto supported = ThermalModeIntf::supported();
53 auto isSupported =
54 std::any_of(supported.begin(), supported.end(), [&value](auto& s) {
55 std::transform(s.begin(), s.end(), s.begin(), toupper);
56 return value == s;
57 });
58
59 if (isSupported && value != current)
60 {
61 current = ThermalModeIntf::current(value);
62 if (_zone.isPersisted(thermalModeIntf, currentProp))
63 {
64 saveCurrentMode();
65 }
66 }
67
68 return current;
69}
70
71void DBusZone::restoreCurrentMode()
72{
73 auto current = ThermalModeIntf::current();
74 fs::path path{CONTROL_PERSIST_ROOT_PATH};
75 // Append this object's name and property description
76 path /= _zone.getName();
77 path /= "CurrentMode";
78 fs::create_directories(path.parent_path());
79
80 try
81 {
82 if (fs::exists(path))
83 {
84 std::ifstream ifs(path.c_str(), std::ios::in | std::ios::binary);
85 cereal::JSONInputArchive iArch(ifs);
86 iArch(current);
87 }
88 }
Patrick Williamsddb773b2021-10-06 11:24:49 -050089 catch (const std::exception& e)
Matthew Barth279183f2021-05-25 10:19:43 -050090 {
Matthew Barth70722372021-06-15 13:50:01 -050091 // Include possible exception when removing file, otherwise ec = 0
92 std::error_code ec;
93 fs::remove(path, ec);
94 log<level::ERR>(
95 fmt::format("Unable to restore persisted `Current` thermal mode "
96 "property ({}, ec: {})",
97 e.what(), ec.value())
98 .c_str());
Matthew Barth279183f2021-05-25 10:19:43 -050099 current = ThermalModeIntf::current();
100 }
101
102 this->current(current);
103}
104
105void DBusZone::saveCurrentMode()
106{
107 fs::path path{CONTROL_PERSIST_ROOT_PATH};
108 // Append this object's name and property description
109 path /= _zone.getName();
110 path /= "CurrentMode";
111 std::ofstream ofs(path.c_str(), std::ios::binary);
112 cereal::JSONOutputArchive oArch(ofs);
113 oArch(ThermalModeIntf::current());
114}
115
116} // namespace phosphor::fan::control::json