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_t& bus) :
54         bus{bus}, manager{bus, sensorsObjectPath}
55     {}
56 
57     /** @copydoc Sensors::enable() */
58     virtual void enable() override;
59 
60     /** @copydoc Sensors::endCycle() */
61     virtual void endCycle() override;
62 
63     /** @copydoc Sensors::endRail() */
64     virtual void endRail(bool errorOccurred) override;
65 
66     /** @copydoc Sensors::disable() */
67     virtual void disable() override;
68 
69     /** @copydoc Sensors::setValue() */
70     virtual void setValue(SensorType type, double value) override;
71 
72     /** @copydoc Sensors::startCycle() */
73     virtual void startCycle() override;
74 
75     /** @copydoc Sensors::startRail() */
76     virtual void startRail(const std::string& rail,
77                            const std::string& deviceInventoryPath,
78                            const std::string& chassisInventoryPath) override;
79 
80   private:
81     /**
82      * D-Bus bus object.
83      */
84     sdbusplus::bus_t& bus;
85 
86     /**
87      * D-Bus object manager.
88      *
89      * Causes this application to implement the
90      * org.freedesktop.DBus.ObjectManager interface.
91      */
92     sdbusplus::server::manager_t manager;
93 
94     /**
95      * Map from sensor names to DBusSensor objects.
96      */
97     std::map<std::string, std::unique_ptr<DBusSensor>> sensors{};
98 
99     /**
100      * Time that current monitoring cycle started.
101      */
102     std::chrono::system_clock::time_point cycleStartTime{};
103 
104     /**
105      * Current voltage rail.
106      *
107      * This is set by startRail().
108      */
109     std::string rail{};
110 
111     /**
112      * Current device inventory path.
113      *
114      * This is set by startRail().
115      */
116     std::string deviceInventoryPath{};
117 
118     /**
119      * Current chassis inventory path.
120      *
121      * This is set by startRail().
122      */
123     std::string chassisInventoryPath{};
124 };
125 
126 } // namespace phosphor::power::regulators
127