blob: a1b5234906c48f41986b93daff2f47cecf5be4ac [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"
11
12namespace sdbusRule = sdbusplus::bus::match::rules;
13
14namespace open_power
15{
16namespace occ
17{
18
19/** @class Manager
20 * @brief Builds and manages OCC objects
21 */
22struct Manager
23{
24 public:
25 Manager() = delete;
26 Manager(const Manager&) = delete;
27 Manager& operator=(const Manager&) = delete;
28 Manager(Manager&&) = default;
29 Manager& operator=(Manager&&) = default;
30 ~Manager() = default;
31
Vishwanatha Subbanna307d80b2017-06-28 15:56:09 +053032 /** @brief Adds OCC pass-through and status objects on the bus
33 * when corresponding CPU inventory is created.
Vishwanatha Subbanna2180b2d2017-06-28 14:05:57 +053034 * @param[in] bus - handle to the bus
35 */
36 Manager(sdbusplus::bus::bus& bus):
37 bus(bus)
38 {
39 for (auto id = 0; id < MAX_CPUS; ++id)
40 {
41 auto path = std::string(CPU_PATH) + std::to_string(id);
42 cpuMatches.emplace_back(
43 bus,
44 sdbusRule::interfacesAdded() +
45 sdbusRule::argNpath(0, path),
46 std::bind(std::mem_fn(&Manager::cpuCreated),
47 this, std::placeholders::_1));
48 }
49 }
50
51 /** @brief Callback that responds to cpu creation in the inventory -
52 * by creating the occ passthrough and status objects.
53 *
54 * @param[in] msg - bus message
55 *
56 * @returns 0 to indicate success
57 */
58 int cpuCreated(sdbusplus::message::message& msg)
59 {
60 namespace fs = std::experimental::filesystem;
61
62 sdbusplus::message::object_path o;
63 msg.read(o);
64 fs::path cpuPath(std::string(std::move(o)));
65 auto cpu = cpuPath.filename();
66
67 std::string name{cpu.c_str()};
68 auto index = name.find(CPU_NAME);
69 name.replace(index, std::strlen(CPU_NAME), OCC_NAME);
70
71 auto path = fs::path(OCC_CONTROL_ROOT) / name;
Vishwanatha Subbanna307d80b2017-06-28 15:56:09 +053072 passThroughObjects.emplace_back(
Vishwanatha Subbanna2180b2d2017-06-28 14:05:57 +053073 std::make_unique<PassThrough>(
74 bus,
75 path.c_str()));
76
Vishwanatha Subbanna307d80b2017-06-28 15:56:09 +053077 statusObjects.emplace_back(
78 std::make_unique<Status>(
79 bus,
80 path.c_str()));
Vishwanatha Subbanna2180b2d2017-06-28 14:05:57 +053081 return 0;
82 }
83
84 private:
85 /** @brief reference to the bus */
86 sdbusplus::bus::bus& bus;
87
88 /** @brief OCC pass-through objects */
Vishwanatha Subbanna307d80b2017-06-28 15:56:09 +053089 std::vector<std::unique_ptr<PassThrough>> passThroughObjects;
90
91 /** @brief OCC Status objects */
92 std::vector<std::unique_ptr<Status>> statusObjects;
Vishwanatha Subbanna2180b2d2017-06-28 14:05:57 +053093
94 /** @brief sbdbusplus match objects */
95 std::vector<sdbusplus::bus::match_t> cpuMatches;
96};
97
98} // namespace occ
99} // namespace open_power