| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | // Copyright 2019 Juan Palacios <[email protected]> |
| 3 | |
| 4 | #include "cpufreqxmlparser.h" |
| 5 | |
| 6 | #include "core/profilepartxmlparserprovider.h" |
| 7 | #include "cpufreq.h" |
| 8 | #include <memory> |
| 9 | |
| 10 | class CPUFreqXMLParser::Initializer final : public CPUFreqProfilePart::Exporter |
| 11 | { |
| 12 | public: |
| 13 | Initializer(CPUFreqXMLParser &outer) noexcept |
| 14 | : outer_(outer) |
| 15 | { |
| 16 | } |
| 17 | |
| 18 | std::optional<std::reference_wrapper<Exportable::Exporter>> |
| 19 | provideExporter(Item const &) override |
| 20 | { |
| 21 | return {}; |
| 22 | } |
| 23 | |
| 24 | void takeActive(bool active) override; |
| 25 | void takeCPUFreqScalingGovernor(std::string const &governor) override; |
| 26 | void takeCPUFreqEPPHint(std::optional<std::string> const &hint) override; |
| 27 | |
| 28 | private: |
| 29 | CPUFreqXMLParser &outer_; |
| 30 | }; |
| 31 | |
| 32 | void CPUFreqXMLParser::Initializer::takeActive(bool active) |
| 33 | { |
| 34 | outer_.active_ = outer_.activeDefault_ = active; |
| 35 | } |
| 36 | |
| 37 | void CPUFreqXMLParser::Initializer::takeCPUFreqScalingGovernor( |
| 38 | std::string const &governor) |
| 39 | { |
| 40 | outer_.scalingGovernor_ = outer_.scalingGovernorDefault_ = governor; |
| 41 | } |
| 42 | |
| 43 | void CPUFreqXMLParser::Initializer::takeCPUFreqEPPHint( |
| 44 | std::optional<std::string> const &hint) |
| 45 | { |
| 46 | outer_.eppHint_ = outer_.eppHintDefault_ = hint; |
| 47 | } |
| 48 | |
| 49 | CPUFreqXMLParser::CPUFreqXMLParser() noexcept |
| 50 | : ProfilePartXMLParser(CPUFreq::ItemID, *this, *this) |
| 51 | { |
| 52 | } |
| 53 | |
| 54 | std::unique_ptr<Exportable::Exporter> |
| 55 | CPUFreqXMLParser::factory(IProfilePartXMLParserProvider const &) |
| 56 | { |
| 57 | return nullptr; |
| 58 | } |
| 59 | |
| 60 | std::unique_ptr<Exportable::Exporter> CPUFreqXMLParser::initializer() |
| 61 | { |
| 62 | return std::make_unique<CPUFreqXMLParser::Initializer>(*this); |
| 63 | } |
| 64 | |
| 65 | std::optional<std::reference_wrapper<Exportable::Exporter>> |
| 66 | CPUFreqXMLParser::provideExporter(Item const &) |
| 67 | { |
| 68 | return {}; |
| 69 | } |
| 70 | |
| 71 | std::optional<std::reference_wrapper<Importable::Importer>> |
| 72 | CPUFreqXMLParser::provideImporter(Item const &) |
| 73 | { |
| 74 | return {}; |
| 75 | } |
| 76 | |
| 77 | void CPUFreqXMLParser::takeActive(bool active) |
| 78 | { |
| 79 | active_ = active; |
| 80 | } |
| 81 | |
| 82 | bool CPUFreqXMLParser::provideActive() const |
| 83 | { |
| 84 | return active_; |
| 85 | } |
| 86 | |
| 87 | void CPUFreqXMLParser::takeCPUFreqScalingGovernor(std::string const &governor) |
| 88 | { |
| 89 | scalingGovernor_ = governor; |
| 90 | } |
| 91 | |
| 92 | std::string const &CPUFreqXMLParser::provideCPUFreqScalingGovernor() const |
| 93 | { |
| 94 | return scalingGovernor_; |
| 95 | } |
| 96 | |
| 97 | void CPUFreqXMLParser::takeCPUFreqEPPHint(std::optional<std::string> const &hint) |
| 98 | { |
| 99 | eppHint_ = hint; |
| 100 | } |
| 101 | |
| 102 | std::optional<std::string> const &CPUFreqXMLParser::provideCPUFreqEPPHint() const |
| 103 | { |
| 104 | return eppHint_; |
| 105 | } |
| 106 | |
| 107 | void CPUFreqXMLParser::appendTo(pugi::xml_node &parentNode) |
| 108 | { |
| 109 | auto node = parentNode.append_child(ID().c_str()); |
| 110 | node.append_attribute("active") = active_; |
| 111 | node.append_attribute("scalingGovernor") = scalingGovernor_.c_str(); |
| 112 | |
| 113 | if (eppHintDefault_) |
| 114 | node.append_attribute("eppHint") = eppHint_->c_str(); |
| 115 | } |
| 116 | |
| 117 | void CPUFreqXMLParser::resetAttributes() |
| 118 | { |
| 119 | active_ = activeDefault_; |
| 120 | scalingGovernor_ = scalingGovernorDefault_; |
| 121 | eppHint_ = eppHintDefault_; |
| 122 | } |
| 123 | |
| 124 | void CPUFreqXMLParser::loadPartFrom(pugi::xml_node const &parentNode) |
| 125 | { |
| 126 | auto node = parentNode.find_child( |
| 127 | [&](pugi::xml_node const &node) { return node.name() == ID(); }); |
| 128 | |
| 129 | active_ = node.attribute("active").as_bool(activeDefault_); |
| 130 | scalingGovernor_ = |
| 131 | node.attribute("scalingGovernor").as_string(scalingGovernorDefault_.c_str()); |
| 132 | |
| 133 | if (eppHintDefault_) |
| 134 | eppHint_ = |
| 135 | node.attribute("eppHint").as_string(scalingGovernorDefault_.c_str()); |
| 136 | } |
| 137 | |
| 138 | bool const CPUFreqXMLParser::registered_ = |
| 139 | ProfilePartXMLParserProvider::registerProvider( |
| 140 | CPUFreq::ItemID, []() { return std::make_unique<CPUFreqXMLParser>(); }); |