blob: 48cd9c8b8c62c13568550677924a5c042b84cc08 [file] [log] [blame]
Vishwanatha Subbanna2180b2d2017-06-28 14:05:57 +05301#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"
Vishwanatha Subbanna307d80b2017-06-28 15:56:09 +05309#include "occ_status.hpp"
Vishwanatha Subbanna2180b2d2017-06-28 14:05:57 +053010#include "config.h"
Andrew Geissler52cf26a2017-07-06 12:56:32 -050011#include <powercap.hpp>
Vishwanatha Subbanna2180b2d2017-06-28 14:05:57 +053012
13namespace sdbusRule = sdbusplus::bus::match::rules;
14
15namespace open_power
16{
17namespace occ
18{
19
20/** @class Manager
21 * @brief Builds and manages OCC objects
22 */
23struct 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
Vishwanatha Subbanna307d80b2017-06-28 15:56:09 +053033 /** @brief Adds OCC pass-through and status objects on the bus
34 * when corresponding CPU inventory is created.
Vishwanatha Subbanna2180b2d2017-06-28 14:05:57 +053035 * @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;
Vishwanatha Subbanna307d80b2017-06-28 15:56:09 +053073 passThroughObjects.emplace_back(
Vishwanatha Subbanna2180b2d2017-06-28 14:05:57 +053074 std::make_unique<PassThrough>(
75 bus,
76 path.c_str()));
77
Vishwanatha Subbanna307d80b2017-06-28 15:56:09 +053078 statusObjects.emplace_back(
79 std::make_unique<Status>(
80 bus,
81 path.c_str()));
Andrew Geissler52cf26a2017-07-06 12:56:32 -050082
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 }
Vishwanatha Subbanna2180b2d2017-06-28 14:05:57 +053090 return 0;
91 }
92
93 private:
94 /** @brief reference to the bus */
95 sdbusplus::bus::bus& bus;
96
97 /** @brief OCC pass-through objects */
Vishwanatha Subbanna307d80b2017-06-28 15:56:09 +053098 std::vector<std::unique_ptr<PassThrough>> passThroughObjects;
99
100 /** @brief OCC Status objects */
101 std::vector<std::unique_ptr<Status>> statusObjects;
Vishwanatha Subbanna2180b2d2017-06-28 14:05:57 +0530102
Andrew Geissler52cf26a2017-07-06 12:56:32 -0500103 /** @brief Power cap monitor and occ notification object */
104 std::unique_ptr<open_power::occ::powercap::PowerCap> pcap;
105
Vishwanatha Subbanna2180b2d2017-06-28 14:05:57 +0530106 /** @brief sbdbusplus match objects */
107 std::vector<sdbusplus::bus::match_t> cpuMatches;
108};
109
110} // namespace occ
111} // namespace open_power