xref: /openbmc/phosphor-power/phosphor-regulators/src/dbus_sensors.hpp (revision 5d4a9c78acf0d019b8dd083ac2aad4e0af241481)
1 /**
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 #pragma once
17 
18 #include "dbus_sensor.hpp"
19 #include "sensors.hpp"
20 
21 #include <sdbusplus/bus.hpp>
22 #include <sdbusplus/server/manager.hpp>
23 
24 #include <chrono>
25 #include <map>
26 #include <memory>
27 #include <string>
28 
29 namespace phosphor::power::regulators
30 {
31 
32 /**
33  * @class DBusSensors
34  *
35  * Implementation of the Sensors interface using D-Bus.
36  */
37 class DBusSensors : public Sensors
38 {
39   public:
40     // Specify which compiler-generated methods we want
41     DBusSensors() = delete;
42     DBusSensors(const DBusSensors&) = delete;
43     DBusSensors(DBusSensors&&) = delete;
44     DBusSensors& operator=(const DBusSensors&) = delete;
45     DBusSensors& operator=(DBusSensors&&) = delete;
46     virtual ~DBusSensors() = default;
47 
48     /**
49      * Constructor.
50      *
51      * @param bus D-Bus bus object
52      */
53     explicit DBusSensors(sdbusplus::bus::bus& bus) :
54         bus{bus}, manager{bus, sensorsObjectPath}
55     {
56     }
57 
58     /** @copydoc Sensors::enable() */
59     virtual void enable() override;
60 
61     /** @copydoc Sensors::endCycle() */
62     virtual void endCycle() override;
63 
64     /** @copydoc Sensors::endRail() */
65     virtual void endRail(bool errorOccurred) override;
66 
67     /** @copydoc Sensors::disable() */
68     virtual void disable() override;
69 
70     /** @copydoc Sensors::setValue() */
71     virtual void setValue(SensorType type, double value) override;
72 
73     /** @copydoc Sensors::startCycle() */
74     virtual void startCycle() override;
75 
76     /** @copydoc Sensors::startRail() */
77     virtual void startRail(const std::string& rail,
78                            const std::string& deviceInventoryPath,
79                            const std::string& chassisInventoryPath) override;
80 
81   private:
82     /**
83      * D-Bus bus object.
84      */
85     sdbusplus::bus::bus& bus;
86 
87     /**
88      * D-Bus object manager.
89      *
90      * Causes this application to implement the
91      * org.freedesktop.DBus.ObjectManager interface.
92      */
93     sdbusplus::server::manager_t manager;
94 
95     /**
96      * Map from sensor names to DBusSensor objects.
97      */
98     std::map<std::string, std::unique_ptr<DBusSensor>> sensors{};
99 
100     /**
101      * Time that current monitoring cycle started.
102      */
103     std::chrono::system_clock::time_point cycleStartTime{};
104 
105     /**
106      * Current voltage rail.
107      *
108      * This is set by startRail().
109      */
110     std::string rail{};
111 
112     /**
113      * Current device inventory path.
114      *
115      * This is set by startRail().
116      */
117     std::string deviceInventoryPath{};
118 
119     /**
120      * Current chassis inventory path.
121      *
122      * This is set by startRail().
123      */
124     std::string chassisInventoryPath{};
125 };
126 
127 } // namespace phosphor::power::regulators
128