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