blob: c1cf891464cd13c4aafe119f082e9d66c736b739 [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
24#include <cereal/archives/json.hpp>
25#include <cereal/cereal.hpp>
Anwaar Hadi64b5ac22025-04-04 23:54:53 +000026#include <phosphor-logging/lg2.hpp>
Matthew Barth279183f2021-05-25 10:19:43 -050027
28#include <algorithm>
29#include <filesystem>
30#include <fstream>
31
32namespace phosphor::fan::control::json
33{
34
Matthew Barth279183f2021-05-25 10:19:43 -050035namespace fs = std::filesystem;
36
37DBusZone::DBusZone(const Zone& zone) :
38 ThermalModeIntf(util::SDBusPlus::getBus(),
39 (fs::path{CONTROL_OBJPATH} /= zone.getName()).c_str(),
Patrick Williamsa3ed9b02022-04-05 16:24:52 -050040 ThermalModeIntf::action::defer_emit),
Matthew Barth279183f2021-05-25 10:19:43 -050041 _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();
Patrick Williamsdfddd642024-08-16 15:21:51 -040050 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 });
Matthew Barth279183f2021-05-25 10:19:43 -050055
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 }
Patrick Williamsddb773b2021-10-06 11:24:49 -050086 catch (const std::exception& e)
Matthew Barth279183f2021-05-25 10:19:43 -050087 {
Matthew Barth70722372021-06-15 13:50:01 -050088 // Include possible exception when removing file, otherwise ec = 0
89 std::error_code ec;
90 fs::remove(path, ec);
Anwaar Hadi64b5ac22025-04-04 23:54:53 +000091 lg2::error("Unable to restore persisted `Current` thermal mode "
92 "property ({ERROR}, ec: {ERROR_CODE})",
93 "ERROR", e, "ERROR_CODE", ec.value());
Matthew Barth279183f2021-05-25 10:19:43 -050094 current = ThermalModeIntf::current();
95 }
96
97 this->current(current);
98}
99
100void DBusZone::saveCurrentMode()
101{
102 fs::path path{CONTROL_PERSIST_ROOT_PATH};
103 // Append this object's name and property description
104 path /= _zone.getName();
105 path /= "CurrentMode";
106 std::ofstream ofs(path.c_str(), std::ios::binary);
107 cereal::JSONOutputArchive oArch(ofs);
108 oArch(ThermalModeIntf::current());
109}
110
111} // namespace phosphor::fan::control::json