xref: /openbmc/openpower-occ-control/occ_manager.hpp (revision ee4d83dfc7b6cf3b5979541ab5b1918b68e6bbdb)
1 #pragma once
2 
3 #include <cstring>
4 #include <vector>
5 #include <experimental/filesystem>
6 #include <functional>
7 #include <sdbusplus/bus.hpp>
8 #include "occ_pass_through.hpp"
9 #include "occ_status.hpp"
10 #include "config.h"
11 #include <powercap.hpp>
12 
13 namespace sdbusRule = sdbusplus::bus::match::rules;
14 
15 namespace open_power
16 {
17 namespace occ
18 {
19 
20 /** @class Manager
21  *  @brief Builds and manages OCC objects
22  */
23 struct Manager
24 {
25     public:
26         Manager() = delete;
27         Manager(const Manager&) = delete;
28         Manager& operator=(const Manager&) = delete;
29         Manager(Manager&&) = default;
30         Manager& operator=(Manager&&) = default;
31         ~Manager() = default;
32 
33         /** @brief Adds OCC pass-through and status objects on the bus
34          *         when corresponding CPU inventory is created.
35          *
36          *  @param[in] bus   - handle to the bus
37          *  @param[in] event - Unique ptr reference to sd_event
38          */
39         Manager(sdbusplus::bus::bus& bus,
40                 EventPtr& event) :
41             bus(bus),
42             event(event)
43         {
44             for (auto id = 0; id < MAX_CPUS; ++id)
45             {
46                 auto path = std::string(CPU_PATH) + std::to_string(id);
47                 cpuMatches.emplace_back(
48                     bus,
49                     sdbusRule::interfacesAdded() +
50                     sdbusRule::argNpath(0, path),
51                     std::bind(std::mem_fn(&Manager::cpuCreated),
52                               this, std::placeholders::_1));
53             }
54         }
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             namespace fs = std::experimental::filesystem;
66 
67             sdbusplus::message::object_path o;
68             msg.read(o);
69             fs::path cpuPath(std::string(std::move(o)));
70             auto cpu = cpuPath.filename();
71 
72             std::string name{cpu.c_str()};
73             auto index = name.find(CPU_NAME);
74             name.replace(index, std::strlen(CPU_NAME), OCC_NAME);
75 
76             auto path = fs::path(OCC_CONTROL_ROOT) / name;
77             passThroughObjects.emplace_back(
78                 std::make_unique<PassThrough>(
79                     bus,
80                     path.c_str()));
81 
82             statusObjects.emplace_back(
83                 std::make_unique<Status>(
84                     bus,
85                     event,
86                     path.c_str()));
87 
88             // Create the power cap monitor object for master occ (0)
89             if(!pcap && (index == 0))
90             {
91                 pcap = std::make_unique<open_power::occ::powercap::PowerCap>(
92                                                         bus,
93                                                         *statusObjects[index]);
94             }
95             return 0;
96         }
97 
98     private:
99         /** @brief reference to the bus */
100         sdbusplus::bus::bus& bus;
101 
102         /** @brief reference to sd_event wrapped in unique_ptr */
103         EventPtr& event;
104 
105         /** @brief OCC pass-through objects */
106         std::vector<std::unique_ptr<PassThrough>> passThroughObjects;
107 
108         /** @brief OCC Status objects */
109         std::vector<std::unique_ptr<Status>> statusObjects;
110 
111         /** @brief Power cap monitor and occ notification object */
112         std::unique_ptr<open_power::occ::powercap::PowerCap> pcap;
113 
114         /** @brief sbdbusplus match objects */
115         std::vector<sdbusplus::bus::match_t> cpuMatches;
116 };
117 
118 } // namespace occ
119 } // namespace open_power
120