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