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  */
16 #pragma once
17 
18 #include "system.hpp"
19 
20 #include <interfaces/manager_interface.hpp>
21 #include <sdbusplus/bus.hpp>
22 #include <sdbusplus/bus/match.hpp>
23 #include <sdbusplus/server/object.hpp>
24 #include <sdeventplus/event.hpp>
25 #include <sdeventplus/source/signal.hpp>
26 #include <sdeventplus/utility/timer.hpp>
27 
28 #include <algorithm>
29 #include <filesystem>
30 #include <memory>
31 #include <string>
32 #include <vector>
33 
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";
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";
43 
44 using Timer = sdeventplus::utility::Timer<sdeventplus::ClockId::Monotonic>;
45 
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      *
63      * @param bus the dbus bus
64      * @param event the sdevent event
65      */
66     Manager(sdbusplus::bus::bus& bus, const sdeventplus::Event& event);
67 
68     /**
69      * Overridden manager object's configure method
70      */
71     void configure() override;
72 
73     /**
74      * Overridden manager object's monitor method
75      *
76      * @param enable Enable or disable regulator monitoring
77      */
78     void monitor(bool enable) override;
79 
80     /**
81      * Timer expired callback function
82      */
83     void timerExpired();
84 
85     /**
86      * Callback function to handle receiving a HUP signal
87      * to reload the configuration data.
88      *
89      * @param sigSrc sd_event_source signal wrapper
90      * @param sigInfo signal info on signal fd
91      */
92     void sighupHandler(sdeventplus::source::Signal& sigSrc,
93                        const struct signalfd_siginfo* sigInfo);
94 
95     /**
96      * Callback function to handle interfacesAdded dbus signals
97      *
98      * @param msg Expanded sdbusplus message data
99      */
100     void signalHandler(sdbusplus::message::message& msg);
101 
102   private:
103     /**
104      * Set the JSON configuration data filename
105      *
106      * @param fName filename without `.json` extension
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     /**
120      * Get the JSON configuration data filename from dbus
121      *
122      * @return JSON configuration data filename
123      */
124     const std::string getFileNameDbus();
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{};
188 };
189 
190 } // namespace phosphor::power::regulators
191