blob: 4430d74f2b4a58c5aaecbd79be9d876331349711 [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 Subbannaee4d83d2017-06-29 18:35:00 +053035 *
36 * @param[in] bus - handle to the bus
37 * @param[in] event - Unique ptr reference to sd_event
Vishwanatha Subbanna2180b2d2017-06-28 14:05:57 +053038 */
Vishwanatha Subbannaee4d83d2017-06-29 18:35:00 +053039 Manager(sdbusplus::bus::bus& bus,
40 EventPtr& event) :
41 bus(bus),
42 event(event)
Vishwanatha Subbanna2180b2d2017-06-28 14:05:57 +053043 {
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 -
Vishwanatha Subbannaee4d83d2017-06-29 18:35:00 +053057 * by creating the needed objects.
Vishwanatha Subbanna2180b2d2017-06-28 14:05:57 +053058 *
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;
Vishwanatha Subbanna307d80b2017-06-28 15:56:09 +053077 passThroughObjects.emplace_back(
Vishwanatha Subbanna2180b2d2017-06-28 14:05:57 +053078 std::make_unique<PassThrough>(
79 bus,
80 path.c_str()));
81
Vishwanatha Subbanna307d80b2017-06-28 15:56:09 +053082 statusObjects.emplace_back(
83 std::make_unique<Status>(
84 bus,
Vishwanatha Subbannaee4d83d2017-06-29 18:35:00 +053085 event,
Vishwanatha Subbanna307d80b2017-06-28 15:56:09 +053086 path.c_str()));
Andrew Geissler52cf26a2017-07-06 12:56:32 -050087
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 }
Vishwanatha Subbanna2180b2d2017-06-28 14:05:57 +053095 return 0;
96 }
97
98 private:
99 /** @brief reference to the bus */
100 sdbusplus::bus::bus& bus;
101
Vishwanatha Subbannaee4d83d2017-06-29 18:35:00 +0530102 /** @brief reference to sd_event wrapped in unique_ptr */
103 EventPtr& event;
104
Vishwanatha Subbanna2180b2d2017-06-28 14:05:57 +0530105 /** @brief OCC pass-through objects */
Vishwanatha Subbanna307d80b2017-06-28 15:56:09 +0530106 std::vector<std::unique_ptr<PassThrough>> passThroughObjects;
107
108 /** @brief OCC Status objects */
109 std::vector<std::unique_ptr<Status>> statusObjects;
Vishwanatha Subbanna2180b2d2017-06-28 14:05:57 +0530110
Andrew Geissler52cf26a2017-07-06 12:56:32 -0500111 /** @brief Power cap monitor and occ notification object */
112 std::unique_ptr<open_power::occ::powercap::PowerCap> pcap;
113
Vishwanatha Subbanna2180b2d2017-06-28 14:05:57 +0530114 /** @brief sbdbusplus match objects */
115 std::vector<sdbusplus::bus::match_t> cpuMatches;
116};
117
118} // namespace occ
119} // namespace open_power