Shawn McCarney | e0c6a2d | 2020-05-01 11:37:08 -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 | */ |
Matthew Barth | 29e9e38 | 2020-01-23 13:40:49 -0600 | [diff] [blame] | 16 | #pragma once |
| 17 | |
Shawn McCarney | e0c6a2d | 2020-05-01 11:37:08 -0500 | [diff] [blame^] | 18 | #include "system.hpp" |
| 19 | |
Matthew Barth | 29e9e38 | 2020-01-23 13:40:49 -0600 | [diff] [blame] | 20 | #include <interfaces/manager_interface.hpp> |
| 21 | #include <sdbusplus/bus.hpp> |
Matthew Barth | 250d0a9 | 2020-02-28 13:04:24 -0600 | [diff] [blame] | 22 | #include <sdbusplus/bus/match.hpp> |
Matthew Barth | 29e9e38 | 2020-01-23 13:40:49 -0600 | [diff] [blame] | 23 | #include <sdbusplus/server/object.hpp> |
Matthew Barth | f2bcf1f | 2020-01-29 14:42:47 -0600 | [diff] [blame] | 24 | #include <sdeventplus/event.hpp> |
Matthew Barth | 7cbc553 | 2020-01-29 15:13:13 -0600 | [diff] [blame] | 25 | #include <sdeventplus/source/signal.hpp> |
Matthew Barth | f2bcf1f | 2020-01-29 14:42:47 -0600 | [diff] [blame] | 26 | #include <sdeventplus/utility/timer.hpp> |
Matthew Barth | 29e9e38 | 2020-01-23 13:40:49 -0600 | [diff] [blame] | 27 | |
Matthew Barth | bbc7c58 | 2020-02-03 15:55:15 -0600 | [diff] [blame] | 28 | #include <algorithm> |
Shawn McCarney | e0c6a2d | 2020-05-01 11:37:08 -0500 | [diff] [blame^] | 29 | #include <filesystem> |
| 30 | #include <memory> |
| 31 | #include <string> |
| 32 | #include <vector> |
Matthew Barth | bbc7c58 | 2020-02-03 15:55:15 -0600 | [diff] [blame] | 33 | |
Matthew Barth | 29e9e38 | 2020-01-23 13:40:49 -0600 | [diff] [blame] | 34 | namespace phosphor::power::regulators |
| 35 | { |
| 36 | |
| 37 | constexpr auto busName = "xyz.openbmc_project.Power.Regulators"; |
| 38 | constexpr auto objPath = "/xyz/openbmc_project/power/regulators/manager"; |
Matthew Barth | 23a5e59 | 2020-01-30 13:11:08 -0600 | [diff] [blame] | 39 | constexpr auto sysDbusObj = "/xyz/openbmc_project/inventory"; |
| 40 | constexpr auto sysDbusPath = "/xyz/openbmc_project/inventory/system"; |
| 41 | constexpr auto sysDbusIntf = "xyz.openbmc_project.Inventory.Item.System"; |
| 42 | constexpr auto sysDbusProp = "Identifier"; |
Matthew Barth | 29e9e38 | 2020-01-23 13:40:49 -0600 | [diff] [blame] | 43 | |
Matthew Barth | f2bcf1f | 2020-01-29 14:42:47 -0600 | [diff] [blame] | 44 | using Timer = sdeventplus::utility::Timer<sdeventplus::ClockId::Monotonic>; |
| 45 | |
Matthew Barth | 29e9e38 | 2020-01-23 13:40:49 -0600 | [diff] [blame] | 46 | using ManagerObject = sdbusplus::server::object::object< |
| 47 | phosphor::power::regulators::interface::ManagerInterface>; |
| 48 | |
| 49 | class Manager : public ManagerObject |
| 50 | { |
| 51 | public: |
| 52 | Manager() = delete; |
| 53 | Manager(const Manager&) = delete; |
| 54 | Manager(Manager&&) = delete; |
| 55 | Manager& operator=(const Manager&) = delete; |
| 56 | Manager& operator=(Manager&&) = delete; |
| 57 | ~Manager() = default; |
| 58 | |
| 59 | /** |
| 60 | * Constructor |
| 61 | * Creates a manager over the regulators. |
| 62 | * |
Shawn McCarney | e0c6a2d | 2020-05-01 11:37:08 -0500 | [diff] [blame^] | 63 | * @param bus the dbus bus |
| 64 | * @param event the sdevent event |
Matthew Barth | 29e9e38 | 2020-01-23 13:40:49 -0600 | [diff] [blame] | 65 | */ |
Matthew Barth | f2bcf1f | 2020-01-29 14:42:47 -0600 | [diff] [blame] | 66 | Manager(sdbusplus::bus::bus& bus, const sdeventplus::Event& event); |
Matthew Barth | 29e9e38 | 2020-01-23 13:40:49 -0600 | [diff] [blame] | 67 | |
| 68 | /** |
Shawn McCarney | e0c6a2d | 2020-05-01 11:37:08 -0500 | [diff] [blame^] | 69 | * Overridden manager object's configure method |
Matthew Barth | 29e9e38 | 2020-01-23 13:40:49 -0600 | [diff] [blame] | 70 | */ |
| 71 | void configure() override; |
| 72 | |
| 73 | /** |
Shawn McCarney | e0c6a2d | 2020-05-01 11:37:08 -0500 | [diff] [blame^] | 74 | * Overridden manager object's monitor method |
Matthew Barth | 29e9e38 | 2020-01-23 13:40:49 -0600 | [diff] [blame] | 75 | * |
Shawn McCarney | e0c6a2d | 2020-05-01 11:37:08 -0500 | [diff] [blame^] | 76 | * @param enable Enable or disable regulator monitoring |
Matthew Barth | 29e9e38 | 2020-01-23 13:40:49 -0600 | [diff] [blame] | 77 | */ |
| 78 | void monitor(bool enable) override; |
| 79 | |
Matthew Barth | f2bcf1f | 2020-01-29 14:42:47 -0600 | [diff] [blame] | 80 | /** |
Shawn McCarney | e0c6a2d | 2020-05-01 11:37:08 -0500 | [diff] [blame^] | 81 | * Timer expired callback function |
Matthew Barth | f2bcf1f | 2020-01-29 14:42:47 -0600 | [diff] [blame] | 82 | */ |
| 83 | void timerExpired(); |
| 84 | |
Matthew Barth | 7cbc553 | 2020-01-29 15:13:13 -0600 | [diff] [blame] | 85 | /** |
Shawn McCarney | e0c6a2d | 2020-05-01 11:37:08 -0500 | [diff] [blame^] | 86 | * Callback function to handle receiving a HUP signal |
Matthew Barth | 7cbc553 | 2020-01-29 15:13:13 -0600 | [diff] [blame] | 87 | * to reload the configuration data. |
| 88 | * |
Shawn McCarney | e0c6a2d | 2020-05-01 11:37:08 -0500 | [diff] [blame^] | 89 | * @param sigSrc sd_event_source signal wrapper |
| 90 | * @param sigInfo signal info on signal fd |
Matthew Barth | 7cbc553 | 2020-01-29 15:13:13 -0600 | [diff] [blame] | 91 | */ |
| 92 | void sighupHandler(sdeventplus::source::Signal& sigSrc, |
| 93 | const struct signalfd_siginfo* sigInfo); |
| 94 | |
Matthew Barth | 250d0a9 | 2020-02-28 13:04:24 -0600 | [diff] [blame] | 95 | /** |
Shawn McCarney | e0c6a2d | 2020-05-01 11:37:08 -0500 | [diff] [blame^] | 96 | * Callback function to handle interfacesAdded dbus signals |
Matthew Barth | 250d0a9 | 2020-02-28 13:04:24 -0600 | [diff] [blame] | 97 | * |
Shawn McCarney | e0c6a2d | 2020-05-01 11:37:08 -0500 | [diff] [blame^] | 98 | * @param msg Expanded sdbusplus message data |
Matthew Barth | 250d0a9 | 2020-02-28 13:04:24 -0600 | [diff] [blame] | 99 | */ |
| 100 | void signalHandler(sdbusplus::message::message& msg); |
| 101 | |
Matthew Barth | 29e9e38 | 2020-01-23 13:40:49 -0600 | [diff] [blame] | 102 | private: |
| 103 | /** |
Shawn McCarney | e0c6a2d | 2020-05-01 11:37:08 -0500 | [diff] [blame^] | 104 | * Set the JSON configuration data filename |
Matthew Barth | bbc7c58 | 2020-02-03 15:55:15 -0600 | [diff] [blame] | 105 | * |
Shawn McCarney | e0c6a2d | 2020-05-01 11:37:08 -0500 | [diff] [blame^] | 106 | * @param fName filename without `.json` extension |
Matthew Barth | bbc7c58 | 2020-02-03 15:55:15 -0600 | [diff] [blame] | 107 | */ |
| 108 | inline void setFileName(const std::string& fName) |
| 109 | { |
| 110 | fileName = fName; |
| 111 | if (!fileName.empty()) |
| 112 | { |
| 113 | // Replace all spaces with underscores |
| 114 | std::replace(fileName.begin(), fileName.end(), ' ', '_'); |
| 115 | fileName.append(".json"); |
| 116 | } |
| 117 | }; |
| 118 | |
| 119 | /** |
Shawn McCarney | e0c6a2d | 2020-05-01 11:37:08 -0500 | [diff] [blame^] | 120 | * Get the JSON configuration data filename from dbus |
Matthew Barth | bbc7c58 | 2020-02-03 15:55:15 -0600 | [diff] [blame] | 121 | * |
Shawn McCarney | e0c6a2d | 2020-05-01 11:37:08 -0500 | [diff] [blame^] | 122 | * @return JSON configuration data filename |
Matthew Barth | bbc7c58 | 2020-02-03 15:55:15 -0600 | [diff] [blame] | 123 | */ |
| 124 | const std::string getFileNameDbus(); |
Shawn McCarney | e0c6a2d | 2020-05-01 11:37:08 -0500 | [diff] [blame^] | 125 | |
| 126 | /** |
| 127 | * Finds the JSON configuration file. |
| 128 | * |
| 129 | * Looks for the config file in the test directory and standard directory. |
| 130 | * |
| 131 | * Throws an exception if the file cannot be found or a file system error |
| 132 | * occurs. |
| 133 | * |
| 134 | * The base name of the config file must have already been obtained and |
| 135 | * stored in the fileName data member. |
| 136 | * |
| 137 | * @return absolute path to config file |
| 138 | */ |
| 139 | std::filesystem::path findConfigFile(); |
| 140 | |
| 141 | /** |
| 142 | * Loads the JSON configuration file. |
| 143 | * |
| 144 | * Looks for the config file in the test directory and standard directory. |
| 145 | * |
| 146 | * If the config file is found, it is parsed and the resulting information |
| 147 | * is stored in the system data member. |
| 148 | * |
| 149 | * If the config file cannot be found or parsing fails, an error is logged. |
| 150 | * |
| 151 | * The base name of the config file must have already been obtained and |
| 152 | * stored in the fileName data member. |
| 153 | */ |
| 154 | void loadConfigFile(); |
| 155 | |
| 156 | /** |
| 157 | * The dbus bus |
| 158 | */ |
| 159 | sdbusplus::bus::bus& bus; |
| 160 | |
| 161 | /** |
| 162 | * Event to loop on |
| 163 | */ |
| 164 | sdeventplus::Event eventLoop; |
| 165 | |
| 166 | /** |
| 167 | * List of event timers |
| 168 | */ |
| 169 | std::vector<Timer> timers{}; |
| 170 | |
| 171 | /** |
| 172 | * List of dbus signal matches |
| 173 | */ |
| 174 | std::vector<std::unique_ptr<sdbusplus::bus::match::match>> signals{}; |
| 175 | |
| 176 | /** |
| 177 | * JSON configuration file base name. |
| 178 | */ |
| 179 | std::string fileName{}; |
| 180 | |
| 181 | /** |
| 182 | * Computer system being controlled and monitored by the BMC. |
| 183 | * |
| 184 | * Contains the information loaded from the JSON configuration file. |
| 185 | * Contains nullptr if the configuration file has not been loaded. |
| 186 | */ |
| 187 | std::unique_ptr<System> system{}; |
Matthew Barth | 29e9e38 | 2020-01-23 13:40:49 -0600 | [diff] [blame] | 188 | }; |
| 189 | |
| 190 | } // namespace phosphor::power::regulators |