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          *  @param[in] bus - handle to the bus
36          */
37         Manager(sdbusplus::bus::bus& bus):
38             bus(bus)
39         {
40             for (auto id = 0; id < MAX_CPUS; ++id)
41             {
42                 auto path = std::string(CPU_PATH) + std::to_string(id);
43                 cpuMatches.emplace_back(
44                     bus,
45                     sdbusRule::interfacesAdded() +
46                     sdbusRule::argNpath(0, path),
47                     std::bind(std::mem_fn(&Manager::cpuCreated),
48                               this, std::placeholders::_1));
49             }
50         }
51 
52         /** @brief Callback that responds to cpu creation in the inventory -
53          *         by creating the occ passthrough and status objects.
54          *
55          *  @param[in] msg - bus message
56          *
57          *  @returns 0 to indicate success
58          */
59         int cpuCreated(sdbusplus::message::message& msg)
60         {
61             namespace fs = std::experimental::filesystem;
62 
63             sdbusplus::message::object_path o;
64             msg.read(o);
65             fs::path cpuPath(std::string(std::move(o)));
66             auto cpu = cpuPath.filename();
67 
68             std::string name{cpu.c_str()};
69             auto index = name.find(CPU_NAME);
70             name.replace(index, std::strlen(CPU_NAME), OCC_NAME);
71 
72             auto path = fs::path(OCC_CONTROL_ROOT) / name;
73             passThroughObjects.emplace_back(
74                 std::make_unique<PassThrough>(
75                     bus,
76                     path.c_str()));
77 
78             statusObjects.emplace_back(
79                 std::make_unique<Status>(
80                     bus,
81                     path.c_str()));
82 
83             // Create the power cap monitor object for master occ (0)
84             if(!pcap && (index == 0))
85             {
86                 pcap = std::make_unique<open_power::occ::powercap::PowerCap>(
87                                                         bus,
88                                                         *statusObjects[index]);
89             }
90             return 0;
91         }
92 
93     private:
94         /** @brief reference to the bus */
95         sdbusplus::bus::bus& bus;
96 
97         /** @brief OCC pass-through objects */
98         std::vector<std::unique_ptr<PassThrough>> passThroughObjects;
99 
100         /** @brief OCC Status objects */
101         std::vector<std::unique_ptr<Status>> statusObjects;
102 
103         /** @brief Power cap monitor and occ notification object */
104         std::unique_ptr<open_power::occ::powercap::PowerCap> pcap;
105 
106         /** @brief sbdbusplus match objects */
107         std::vector<sdbusplus::bus::match_t> cpuMatches;
108 };
109 
110 } // namespace occ
111 } // namespace open_power
112