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