xref: /openbmc/openpower-occ-control/occ_manager.hpp (revision b5259a1e7433e997caf9c90565d8b0012a6e5820)
1 #pragma once
2 
3 #include <cstring>
4 #include <vector>
5 #include <functional>
6 #include <sdbusplus/bus.hpp>
7 #include "occ_pass_through.hpp"
8 #include "occ_status.hpp"
9 #include "powercap.hpp"
10 
11 namespace sdbusRule = sdbusplus::bus::match::rules;
12 namespace open_power
13 {
14 namespace occ
15 {
16 
17 /** @class Manager
18  *  @brief Builds and manages OCC objects
19  */
20 struct Manager
21 {
22     public:
23         Manager() = delete;
24         Manager(const Manager&) = delete;
25         Manager& operator=(const Manager&) = delete;
26         Manager(Manager&&) = default;
27         Manager& operator=(Manager&&) = default;
28         ~Manager() = default;
29 
30         /** @brief Adds OCC pass-through and status objects on the bus
31          *         when corresponding CPU inventory is created.
32          *
33          *  @param[in] bus   - handle to the bus
34          *  @param[in] event - Unique ptr reference to sd_event
35          */
36         Manager(sdbusplus::bus::bus& bus,
37                 EventPtr& event) :
38             bus(bus),
39             event(event)
40         {
41 #ifdef I2C_OCC
42             // I2C OCC status objects are initialized directly
43             initStatusObjects();
44 #else
45             findAndCreateObjects();
46 #endif
47         }
48 
49     private:
50         /** @brief Checks if the CPU inventory is present and if so, creates
51          *         the occ D-Bus objects. Else, registers a handler to be
52          *         called when inventory is created.
53          */
54         void findAndCreateObjects();
55 
56         /** @brief Callback that responds to cpu creation in the inventory -
57          *         by creating the needed objects.
58          *
59          *  @param[in] msg - bus message
60          *
61          *  @returns 0 to indicate success
62          */
63         int cpuCreated(sdbusplus::message::message& msg);
64 
65         /** @brief Create child OCC objects.
66          *
67          *  @param[in] occ - the occ name, such as occ0.
68          */
69         void createObjects(const std::string& occ);
70 
71         /** @brief Callback handler invoked by Status object when the OccActive
72          *         property is changed. This is needed to make sure that the
73          *         error detection is started only after all the OCCs are bound.
74          *         Similarly, when one of the OCC gets its OccActive property
75          *         un-set, then the OCC error detection needs to be stopped on
76          *         all the OCCs
77          *
78          *  @param[in] status - OccActive status
79          */
80         void statusCallBack(bool status);
81 
82         /** @brief Sends a Heartbeat command to host control command handler */
83         void sendHeartBeat();
84 
85         /** @brief reference to the bus */
86         sdbusplus::bus::bus& bus;
87 
88         /** @brief reference to sd_event wrapped in unique_ptr */
89         EventPtr& event;
90 
91         /** @brief OCC pass-through objects */
92         std::vector<std::unique_ptr<PassThrough>> passThroughObjects;
93 
94         /** @brief OCC Status objects */
95         std::vector<std::unique_ptr<Status>> statusObjects;
96 
97         /** @brief Power cap monitor and occ notification object */
98         std::unique_ptr<open_power::occ::powercap::PowerCap> pcap;
99 
100         /** @brief sbdbusplus match objects */
101         std::vector<sdbusplus::bus::match_t> cpuMatches;
102 
103         /** @brief Number of OCCs that are bound */
104         uint8_t activeCount = 0;
105 
106 #ifdef I2C_OCC
107         /** @brief Init Status objects for I2C OCC devices
108          *
109          * It iterates in /sys/bus/i2c/devices, finds all occ hwmon devices
110          * and creates status objects.
111          */
112         void initStatusObjects();
113 #endif
114 };
115 
116 } // namespace occ
117 } // namespace open_power
118